blob: 95a669e64c18a7ebd8e93f3a874f5dcc6eda7b14 [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
joshualittb0a8a372014-09-23 09:50:21 -07008#ifndef GrProcessor_DEFINED
9#define GrProcessor_DEFINED
tomhudson@google.com168e6342012-04-18 17:49:20 +000010
bsalomon@google.com371e1052013-01-11 21:08:55 +000011#include "GrColor.h"
Brian Salomonb014cca2016-11-18 11:39:15 -050012#include "GrBuffer.h"
13#include "GrGpuResourceRef.h"
joshualittb0a8a372014-09-23 09:50:21 -070014#include "GrProcessorUnitTest.h"
bsalomon95740982014-09-04 13:12:37 -070015#include "GrProgramElement.h"
Brian Salomonb014cca2016-11-18 11:39:15 -050016#include "GrSamplerParams.h"
Brian Salomonf9f45122016-11-29 11:59:17 -050017#include "GrShaderVar.h"
egdaniel9e4d6d12014-10-15 13:49:02 -070018#include "SkMath.h"
robertphillipse004bfc2015-11-16 09:06:59 -080019#include "SkString.h"
mtklein59c12e32016-05-02 07:19:41 -070020#include "../private/SkAtomics.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000021
tomhudson@google.com168e6342012-04-18 17:49:20 +000022class GrContext;
bsalomon@google.com77af6802013-10-02 13:04:56 +000023class GrCoordTransform;
egdaniel605dd0f2014-11-12 08:35:25 -080024class GrInvariantOutput;
bsalomon95740982014-09-04 13:12:37 -070025
joshualitteb2a6762014-12-04 11:35:33 -080026/**
27 * Used by processors to build their keys. It incorporates each per-processor key into a larger
28 * shader key.
29 */
30class GrProcessorKeyBuilder {
31public:
32 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
33 SkASSERT(0 == fData->count() % sizeof(uint32_t));
34 }
35
36 void add32(uint32_t v) {
37 ++fCount;
38 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
39 }
40
41 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
42 add*() call. */
43 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
44 SkASSERT(count > 0);
45 fCount += count;
46 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
47 }
48
49 size_t size() const { return sizeof(uint32_t) * fCount; }
50
51private:
52 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
53 int fCount; // number of uint32_ts added to fData by the processor.
54};
55
bsalomon98b33eb2014-10-15 11:05:26 -070056/** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
57 immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000058
joshualittb0a8a372014-09-23 09:50:21 -070059 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
mdempsky38f1f6f2015-08-27 12:57:01 -070060 processor must reach 0 before the thread terminates and the pool is destroyed.
bsalomon98b33eb2014-10-15 11:05:26 -070061 */
joshualittb0a8a372014-09-23 09:50:21 -070062class GrProcessor : public GrProgramElement {
tomhudson@google.com168e6342012-04-18 17:49:20 +000063public:
Brian Salomon0bbecb22016-11-17 11:38:22 -050064 class TextureSampler;
Brian Salomonb014cca2016-11-18 11:39:15 -050065 class BufferAccess;
Brian Salomonf9f45122016-11-29 11:59:17 -050066 class ImageStorageAccess;
Brian Salomon0bbecb22016-11-17 11:38:22 -050067
joshualittb0a8a372014-09-23 09:50:21 -070068 virtual ~GrProcessor();
tomhudson@google.com168e6342012-04-18 17:49:20 +000069
Brian Salomonb014cca2016-11-18 11:39:15 -050070 /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader
71 code. */
joshualitteb2a6762014-12-04 11:35:33 -080072 virtual const char* name() const = 0;
bsalomon@google.com289efe02012-05-21 20:57:59 +000073
Brian Salomonb014cca2016-11-18 11:39:15 -050074 /** Human-readable dump of all information */
robertphillipse004bfc2015-11-16 09:06:59 -080075 virtual SkString dumpInfo() const {
76 SkString str;
77 str.appendf("Missing data");
78 return str;
79 }
80
Brian Salomon0bbecb22016-11-17 11:38:22 -050081 int numTextureSamplers() const { return fTextureSamplers.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000082
bsalomon@google.com6d003d12012-09-11 15:45:20 +000083 /** Returns the access pattern for the texture at index. index must be valid according to
Brian Salomon0bbecb22016-11-17 11:38:22 -050084 numTextureSamplers(). */
85 const TextureSampler& textureSampler(int index) const { return *fTextureSamplers[index]; }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000086
cdalton74b8d322016-04-11 14:47:28 -070087 int numBuffers() const { return fBufferAccesses.count(); }
88
89 /** Returns the access pattern for the buffer at index. index must be valid according to
90 numBuffers(). */
Brian Salomonb014cca2016-11-18 11:39:15 -050091 const BufferAccess& bufferAccess(int index) const { return *fBufferAccesses[index]; }
cdalton74b8d322016-04-11 14:47:28 -070092
Brian Salomonf9f45122016-11-29 11:59:17 -050093 int numImageStorages() const { return fImageStorageAccesses.count(); }
94
95 /** Returns the access object for the image at index. index must be valid according to
96 numImages(). */
97 const ImageStorageAccess& imageStorageAccess(int index) const {
98 return *fImageStorageAccesses[index];
99 }
100
101 /**
102 * Platform specific built-in features that a processor can request for the fragment shader.
103 */
cdalton87332102016-02-26 12:22:02 -0800104 enum RequiredFeatures {
105 kNone_RequiredFeatures = 0,
cdalton28f45b92016-03-07 13:58:26 -0800106 kFragmentPosition_RequiredFeature = 1 << 0,
107 kSampleLocations_RequiredFeature = 1 << 1
cdalton87332102016-02-26 12:22:02 -0800108 };
109
110 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
111
112 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000113
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000114 void* operator new(size_t size);
115 void operator delete(void* target);
116
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000117 void* operator new(size_t size, void* placement) {
118 return ::operator new(size, placement);
119 }
120 void operator delete(void* target, void* placement) {
121 ::operator delete(target, placement);
122 }
123
Brian Salomonb014cca2016-11-18 11:39:15 -0500124 /** Helper for down-casting to a GrProcessor subclass */
joshualitt49586be2014-09-16 08:21:41 -0700125 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
126
joshualitteb2a6762014-12-04 11:35:33 -0800127 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
128
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000129protected:
cdalton87332102016-02-26 12:22:02 -0800130 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700131
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000132 /**
Brian Salomonf9f45122016-11-29 11:59:17 -0500133 * Subclasses call these from their constructor to register sampler/image sources. The processor
cdalton74b8d322016-04-11 14:47:28 -0700134 * subclass manages the lifetime of the objects (these functions only store pointers). The
Brian Salomonb014cca2016-11-18 11:39:15 -0500135 * TextureSampler and/or BufferAccess instances are typically member fields of the GrProcessor
136 * subclass. These must only be called from the constructor because GrProcessors are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000137 */
Brian Salomon0bbecb22016-11-17 11:38:22 -0500138 void addTextureSampler(const TextureSampler*);
Brian Salomonf9f45122016-11-29 11:59:17 -0500139 void addBufferAccess(const BufferAccess*);
140 void addImageStorageAccess(const ImageStorageAccess*);
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000141
Brian Salomonf9f45122016-11-29 11:59:17 -0500142 bool hasSameSamplersAndAccesses(const GrProcessor &) const;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000143
144 /**
cdalton28f45b92016-03-07 13:58:26 -0800145 * If the prcoessor will generate code that uses platform specific built-in features, then it
146 * must call these methods from its constructor. Otherwise, requests to use these features will
147 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000148 */
cdalton87332102016-02-26 12:22:02 -0800149 void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; }
cdalton28f45b92016-03-07 13:58:26 -0800150 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800151
152 void combineRequiredFeatures(const GrProcessor& other) {
153 fRequiredFeatures |= other.fRequiredFeatures;
154 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000155
joshualitteb2a6762014-12-04 11:35:33 -0800156 template <typename PROC_SUBCLASS> void initClassID() {
157 static uint32_t kClassID = GenClassID();
158 fClassID = kClassID;
159 }
160
bsalomon0e08fc12014-10-15 08:19:04 -0700161private:
joshualitteb2a6762014-12-04 11:35:33 -0800162 static uint32_t GenClassID() {
163 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
164 // atomic inc returns the old value not the incremented value. So we add
165 // 1 to the returned value.
166 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
167 if (!id) {
168 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
169 "subclass.");
170 }
171 return id;
172 }
173
174 enum {
175 kIllegalProcessorClassID = 0,
176 };
177 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000178
Brian Salomonf9f45122016-11-29 11:59:17 -0500179 uint32_t fClassID;
180 RequiredFeatures fRequiredFeatures;
181 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
182 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
183 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000184
bsalomon95740982014-09-04 13:12:37 -0700185 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000186};
187
cdalton87332102016-02-26 12:22:02 -0800188GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
189
Brian Salomon0bbecb22016-11-17 11:38:22 -0500190/**
191 * Used to represent a texture that is required by a GrProcessor. It holds a GrTexture along with
Brian Salomonf9f45122016-11-29 11:59:17 -0500192 * an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to account
193 * for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500194 */
195class GrProcessor::TextureSampler : public SkNoncopyable {
196public:
197 /**
198 * Must be initialized before adding to a GrProcessor's texture access list.
199 */
200 TextureSampler();
201
Brian Salomon514baff2016-11-17 15:17:07 -0500202 TextureSampler(GrTexture*, const GrSamplerParams&);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500203
204 explicit TextureSampler(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500205 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500206 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
207 GrShaderFlags visibility = kFragment_GrShaderFlag);
208
Brian Salomon514baff2016-11-17 15:17:07 -0500209 void reset(GrTexture*, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500210 GrShaderFlags visibility = kFragment_GrShaderFlag);
211 void reset(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500212 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500213 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
214 GrShaderFlags visibility = kFragment_GrShaderFlag);
215
216 bool operator==(const TextureSampler& that) const {
Brian Salomondb4183d2016-11-17 12:48:40 -0500217 return this->texture() == that.texture() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500218 fParams == that.fParams &&
219 fVisibility == that.fVisibility;
220 }
221
222 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
223
Brian Salomondb4183d2016-11-17 12:48:40 -0500224 GrTexture* texture() const { return fTexture.get(); }
225 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500226 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500227
228 /**
229 * For internal use by GrProcessor.
230 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500231 const GrGpuResourceRef* programTexture() const { return &fTexture; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500232
233private:
234
235 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
236
237 ProgramTexture fTexture;
Brian Salomon514baff2016-11-17 15:17:07 -0500238 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500239 GrShaderFlags fVisibility;
240
241 typedef SkNoncopyable INHERITED;
242};
243
Brian Salomonb014cca2016-11-18 11:39:15 -0500244/**
245 * Used to represent a texel buffer that will be read in a GrProcessor. It holds a GrBuffer along
246 * with an associated offset and texel config.
247 */
248class GrProcessor::BufferAccess : public SkNoncopyable {
249public:
250 /**
251 * Must be initialized before adding to a GrProcessor's buffer access list.
252 */
253 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
254 GrShaderFlags visibility = kFragment_GrShaderFlag) {
255 fTexelConfig = texelConfig;
256 fBuffer.set(SkRef(buffer), kRead_GrIOType);
257 fVisibility = visibility;
258 }
259
260 bool operator==(const BufferAccess& that) const {
261 return fTexelConfig == that.fTexelConfig &&
262 this->buffer() == that.buffer() &&
263 fVisibility == that.fVisibility;
264 }
265
266 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
267
268 GrPixelConfig texelConfig() const { return fTexelConfig; }
269 GrBuffer* buffer() const { return fBuffer.get(); }
270 GrShaderFlags visibility() const { return fVisibility; }
271
272 /**
273 * For internal use by GrProcessor.
274 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500275 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500276
277private:
278 GrPixelConfig fTexelConfig;
279 GrTGpuResourceRef<GrBuffer> fBuffer;
280 GrShaderFlags fVisibility;
281
282 typedef SkNoncopyable INHERITED;
283};
284
Brian Salomonf9f45122016-11-29 11:59:17 -0500285/**
286 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
287 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
288 * Currently the format of the load/store data in the shader is inferred from the texture config,
289 * though it could be made explicit.
290 */
291class GrProcessor::ImageStorageAccess : public SkNoncopyable {
292public:
293 ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, GrSLMemoryModel, GrSLRestrict,
294 GrShaderFlags visibility = kFragment_GrShaderFlag);
295
296 bool operator==(const ImageStorageAccess& that) const {
297 return this->texture() == that.texture() && fVisibility == that.fVisibility;
298 }
299
300 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
301
302 GrTexture* texture() const { return fTexture.get(); }
303 GrShaderFlags visibility() const { return fVisibility; }
304 GrIOType ioType() const { return fTexture.ioType(); }
305 GrImageStorageFormat format() const { return fFormat; }
306 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
307 GrSLRestrict restrict() const { return fRestrict; }
308
309 /**
310 * For internal use by GrProcessor.
311 */
312 const GrGpuResourceRef* programTexture() const { return &fTexture; }
313
314private:
315 GrTGpuResourceRef<GrTexture> fTexture;
316 GrShaderFlags fVisibility;
317 GrImageStorageFormat fFormat;
318 GrSLMemoryModel fMemoryModel;
319 GrSLRestrict fRestrict;
320 typedef SkNoncopyable INHERITED;
321};
322
tomhudson@google.com168e6342012-04-18 17:49:20 +0000323#endif