blob: f933c9a9f7130eae83694d75b09cb38639ee9a8d [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,
Ethan Nicholasde4d3012017-01-19 16:58:02 -0500106 kSampleLocations_RequiredFeature = 1 << 0
cdalton87332102016-02-26 12:22:02 -0800107 };
108
109 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
110
111 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000112
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000113 void* operator new(size_t size);
114 void operator delete(void* target);
115
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000116 void* operator new(size_t size, void* placement) {
117 return ::operator new(size, placement);
118 }
119 void operator delete(void* target, void* placement) {
120 ::operator delete(target, placement);
121 }
122
Brian Salomonb014cca2016-11-18 11:39:15 -0500123 /** Helper for down-casting to a GrProcessor subclass */
joshualitt49586be2014-09-16 08:21:41 -0700124 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
125
joshualitteb2a6762014-12-04 11:35:33 -0800126 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
127
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000128protected:
cdalton87332102016-02-26 12:22:02 -0800129 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700130
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000131 /**
Brian Salomonf9f45122016-11-29 11:59:17 -0500132 * Subclasses call these from their constructor to register sampler/image sources. The processor
cdalton74b8d322016-04-11 14:47:28 -0700133 * subclass manages the lifetime of the objects (these functions only store pointers). The
Brian Salomonb014cca2016-11-18 11:39:15 -0500134 * TextureSampler and/or BufferAccess instances are typically member fields of the GrProcessor
135 * subclass. These must only be called from the constructor because GrProcessors are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000136 */
Brian Salomon0bbecb22016-11-17 11:38:22 -0500137 void addTextureSampler(const TextureSampler*);
Brian Salomonf9f45122016-11-29 11:59:17 -0500138 void addBufferAccess(const BufferAccess*);
139 void addImageStorageAccess(const ImageStorageAccess*);
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000140
Brian Salomonf9f45122016-11-29 11:59:17 -0500141 bool hasSameSamplersAndAccesses(const GrProcessor &) const;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000142
143 /**
cdalton28f45b92016-03-07 13:58:26 -0800144 * If the prcoessor will generate code that uses platform specific built-in features, then it
145 * must call these methods from its constructor. Otherwise, requests to use these features will
146 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000147 */
cdalton28f45b92016-03-07 13:58:26 -0800148 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800149
150 void combineRequiredFeatures(const GrProcessor& other) {
151 fRequiredFeatures |= other.fRequiredFeatures;
152 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000153
joshualitteb2a6762014-12-04 11:35:33 -0800154 template <typename PROC_SUBCLASS> void initClassID() {
155 static uint32_t kClassID = GenClassID();
156 fClassID = kClassID;
157 }
158
bsalomon0e08fc12014-10-15 08:19:04 -0700159private:
joshualitteb2a6762014-12-04 11:35:33 -0800160 static uint32_t GenClassID() {
161 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
162 // atomic inc returns the old value not the incremented value. So we add
163 // 1 to the returned value.
164 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
165 if (!id) {
166 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
167 "subclass.");
168 }
169 return id;
170 }
171
Brian Salomone57194f2017-01-09 15:30:02 -0500172 friend class GrProgramElement<GrProcessor>;
173 void addPendingIOs() const;
174 void removeRefs() const;
175 void pendingIOComplete() const;
176
joshualitteb2a6762014-12-04 11:35:33 -0800177 enum {
178 kIllegalProcessorClassID = 0,
179 };
180 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000181
Brian Salomonf9f45122016-11-29 11:59:17 -0500182 uint32_t fClassID;
183 RequiredFeatures fRequiredFeatures;
184 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
185 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
186 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000187
bsalomon95740982014-09-04 13:12:37 -0700188 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000189};
190
cdalton87332102016-02-26 12:22:02 -0800191GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
192
Brian Salomon0bbecb22016-11-17 11:38:22 -0500193/**
194 * Used to represent a texture that is required by a GrProcessor. It holds a GrTexture along with
Brian Salomonf9f45122016-11-29 11:59:17 -0500195 * an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to account
196 * for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500197 */
198class GrProcessor::TextureSampler : public SkNoncopyable {
199public:
200 /**
201 * Must be initialized before adding to a GrProcessor's texture access list.
202 */
203 TextureSampler();
204
Brian Salomon514baff2016-11-17 15:17:07 -0500205 TextureSampler(GrTexture*, const GrSamplerParams&);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500206
207 explicit TextureSampler(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500208 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500209 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
210 GrShaderFlags visibility = kFragment_GrShaderFlag);
211
Brian Salomon514baff2016-11-17 15:17:07 -0500212 void reset(GrTexture*, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500213 GrShaderFlags visibility = kFragment_GrShaderFlag);
214 void reset(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500215 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500216 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
217 GrShaderFlags visibility = kFragment_GrShaderFlag);
218
219 bool operator==(const TextureSampler& that) const {
Brian Salomondb4183d2016-11-17 12:48:40 -0500220 return this->texture() == that.texture() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500221 fParams == that.fParams &&
222 fVisibility == that.fVisibility;
223 }
224
225 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
226
Brian Salomondb4183d2016-11-17 12:48:40 -0500227 GrTexture* texture() const { return fTexture.get(); }
228 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500229 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500230
231 /**
232 * For internal use by GrProcessor.
233 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500234 const GrGpuResourceRef* programTexture() const { return &fTexture; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500235
236private:
237
238 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
239
240 ProgramTexture fTexture;
Brian Salomon514baff2016-11-17 15:17:07 -0500241 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500242 GrShaderFlags fVisibility;
243
244 typedef SkNoncopyable INHERITED;
245};
246
Brian Salomonb014cca2016-11-18 11:39:15 -0500247/**
248 * Used to represent a texel buffer that will be read in a GrProcessor. It holds a GrBuffer along
249 * with an associated offset and texel config.
250 */
251class GrProcessor::BufferAccess : public SkNoncopyable {
252public:
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500253 BufferAccess() = default;
254 BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
255 GrShaderFlags visibility = kFragment_GrShaderFlag) {
256 this->reset(texelConfig, buffer, visibility);
257 }
Brian Salomonb014cca2016-11-18 11:39:15 -0500258 /**
259 * Must be initialized before adding to a GrProcessor's buffer access list.
260 */
261 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
262 GrShaderFlags visibility = kFragment_GrShaderFlag) {
263 fTexelConfig = texelConfig;
264 fBuffer.set(SkRef(buffer), kRead_GrIOType);
265 fVisibility = visibility;
266 }
267
268 bool operator==(const BufferAccess& that) const {
269 return fTexelConfig == that.fTexelConfig &&
270 this->buffer() == that.buffer() &&
271 fVisibility == that.fVisibility;
272 }
273
274 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
275
276 GrPixelConfig texelConfig() const { return fTexelConfig; }
277 GrBuffer* buffer() const { return fBuffer.get(); }
278 GrShaderFlags visibility() const { return fVisibility; }
279
280 /**
281 * For internal use by GrProcessor.
282 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500283 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500284
285private:
286 GrPixelConfig fTexelConfig;
287 GrTGpuResourceRef<GrBuffer> fBuffer;
288 GrShaderFlags fVisibility;
289
290 typedef SkNoncopyable INHERITED;
291};
292
Brian Salomonf9f45122016-11-29 11:59:17 -0500293/**
294 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
295 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
296 * Currently the format of the load/store data in the shader is inferred from the texture config,
297 * though it could be made explicit.
298 */
299class GrProcessor::ImageStorageAccess : public SkNoncopyable {
300public:
301 ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, GrSLMemoryModel, GrSLRestrict,
302 GrShaderFlags visibility = kFragment_GrShaderFlag);
303
304 bool operator==(const ImageStorageAccess& that) const {
305 return this->texture() == that.texture() && fVisibility == that.fVisibility;
306 }
307
308 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
309
310 GrTexture* texture() const { return fTexture.get(); }
311 GrShaderFlags visibility() const { return fVisibility; }
312 GrIOType ioType() const { return fTexture.ioType(); }
313 GrImageStorageFormat format() const { return fFormat; }
314 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
315 GrSLRestrict restrict() const { return fRestrict; }
316
317 /**
318 * For internal use by GrProcessor.
319 */
320 const GrGpuResourceRef* programTexture() const { return &fTexture; }
321
322private:
323 GrTGpuResourceRef<GrTexture> fTexture;
324 GrShaderFlags fVisibility;
325 GrImageStorageFormat fFormat;
326 GrSLMemoryModel fMemoryModel;
327 GrSLRestrict fRestrict;
328 typedef SkNoncopyable INHERITED;
329};
330
tomhudson@google.com168e6342012-04-18 17:49:20 +0000331#endif