blob: 32995c308b60ebd04ac362787dcfdaefcf5bee41 [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 */
Brian Salomone57194f2017-01-09 15:30:02 -050062class GrProcessor : public GrProgramElement<GrProcessor> {
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
Brian Salomone57194f2017-01-09 15:30:02 -0500174 friend class GrProgramElement<GrProcessor>;
175 void addPendingIOs() const;
176 void removeRefs() const;
177 void pendingIOComplete() const;
178
joshualitteb2a6762014-12-04 11:35:33 -0800179 enum {
180 kIllegalProcessorClassID = 0,
181 };
182 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000183
Brian Salomonf9f45122016-11-29 11:59:17 -0500184 uint32_t fClassID;
185 RequiredFeatures fRequiredFeatures;
186 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
187 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
188 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000189
bsalomon95740982014-09-04 13:12:37 -0700190 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000191};
192
cdalton87332102016-02-26 12:22:02 -0800193GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
194
Brian Salomon0bbecb22016-11-17 11:38:22 -0500195/**
196 * Used to represent a texture that is required by a GrProcessor. It holds a GrTexture along with
Brian Salomonf9f45122016-11-29 11:59:17 -0500197 * an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to account
198 * for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500199 */
200class GrProcessor::TextureSampler : public SkNoncopyable {
201public:
202 /**
203 * Must be initialized before adding to a GrProcessor's texture access list.
204 */
205 TextureSampler();
206
Brian Salomon514baff2016-11-17 15:17:07 -0500207 TextureSampler(GrTexture*, const GrSamplerParams&);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500208
209 explicit TextureSampler(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500210 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500211 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
212 GrShaderFlags visibility = kFragment_GrShaderFlag);
213
Brian Salomon514baff2016-11-17 15:17:07 -0500214 void reset(GrTexture*, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500215 GrShaderFlags visibility = kFragment_GrShaderFlag);
216 void reset(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500217 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500218 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
219 GrShaderFlags visibility = kFragment_GrShaderFlag);
220
221 bool operator==(const TextureSampler& that) const {
Brian Salomondb4183d2016-11-17 12:48:40 -0500222 return this->texture() == that.texture() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500223 fParams == that.fParams &&
224 fVisibility == that.fVisibility;
225 }
226
227 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
228
Brian Salomondb4183d2016-11-17 12:48:40 -0500229 GrTexture* texture() const { return fTexture.get(); }
230 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500231 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500232
233 /**
234 * For internal use by GrProcessor.
235 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500236 const GrGpuResourceRef* programTexture() const { return &fTexture; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500237
238private:
239
240 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
241
242 ProgramTexture fTexture;
Brian Salomon514baff2016-11-17 15:17:07 -0500243 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500244 GrShaderFlags fVisibility;
245
246 typedef SkNoncopyable INHERITED;
247};
248
Brian Salomonb014cca2016-11-18 11:39:15 -0500249/**
250 * Used to represent a texel buffer that will be read in a GrProcessor. It holds a GrBuffer along
251 * with an associated offset and texel config.
252 */
253class GrProcessor::BufferAccess : public SkNoncopyable {
254public:
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500255 BufferAccess() = default;
256 BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
257 GrShaderFlags visibility = kFragment_GrShaderFlag) {
258 this->reset(texelConfig, buffer, visibility);
259 }
Brian Salomonb014cca2016-11-18 11:39:15 -0500260 /**
261 * Must be initialized before adding to a GrProcessor's buffer access list.
262 */
263 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
264 GrShaderFlags visibility = kFragment_GrShaderFlag) {
265 fTexelConfig = texelConfig;
266 fBuffer.set(SkRef(buffer), kRead_GrIOType);
267 fVisibility = visibility;
268 }
269
270 bool operator==(const BufferAccess& that) const {
271 return fTexelConfig == that.fTexelConfig &&
272 this->buffer() == that.buffer() &&
273 fVisibility == that.fVisibility;
274 }
275
276 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
277
278 GrPixelConfig texelConfig() const { return fTexelConfig; }
279 GrBuffer* buffer() const { return fBuffer.get(); }
280 GrShaderFlags visibility() const { return fVisibility; }
281
282 /**
283 * For internal use by GrProcessor.
284 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500285 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500286
287private:
288 GrPixelConfig fTexelConfig;
289 GrTGpuResourceRef<GrBuffer> fBuffer;
290 GrShaderFlags fVisibility;
291
292 typedef SkNoncopyable INHERITED;
293};
294
Brian Salomonf9f45122016-11-29 11:59:17 -0500295/**
296 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
297 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
298 * Currently the format of the load/store data in the shader is inferred from the texture config,
299 * though it could be made explicit.
300 */
301class GrProcessor::ImageStorageAccess : public SkNoncopyable {
302public:
303 ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, GrSLMemoryModel, GrSLRestrict,
304 GrShaderFlags visibility = kFragment_GrShaderFlag);
305
306 bool operator==(const ImageStorageAccess& that) const {
307 return this->texture() == that.texture() && fVisibility == that.fVisibility;
308 }
309
310 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
311
312 GrTexture* texture() const { return fTexture.get(); }
313 GrShaderFlags visibility() const { return fVisibility; }
314 GrIOType ioType() const { return fTexture.ioType(); }
315 GrImageStorageFormat format() const { return fFormat; }
316 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
317 GrSLRestrict restrict() const { return fRestrict; }
318
319 /**
320 * For internal use by GrProcessor.
321 */
322 const GrGpuResourceRef* programTexture() const { return &fTexture; }
323
324private:
325 GrTGpuResourceRef<GrTexture> fTexture;
326 GrShaderFlags fVisibility;
327 GrImageStorageFormat fFormat;
328 GrSLMemoryModel fMemoryModel;
329 GrSLRestrict fRestrict;
330 typedef SkNoncopyable INHERITED;
331};
332
tomhudson@google.com168e6342012-04-18 17:49:20 +0000333#endif