blob: fa6574dc8d30e57aff47c316d73651b4339d645b [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;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050025class GrTextureProvider;
26class GrTextureProxy;
bsalomon95740982014-09-04 13:12:37 -070027
joshualitteb2a6762014-12-04 11:35:33 -080028/**
29 * Used by processors to build their keys. It incorporates each per-processor key into a larger
30 * shader key.
31 */
32class GrProcessorKeyBuilder {
33public:
34 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
35 SkASSERT(0 == fData->count() % sizeof(uint32_t));
36 }
37
38 void add32(uint32_t v) {
39 ++fCount;
40 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
41 }
42
43 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
44 add*() call. */
45 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
46 SkASSERT(count > 0);
47 fCount += count;
48 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
49 }
50
51 size_t size() const { return sizeof(uint32_t) * fCount; }
52
53private:
54 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
55 int fCount; // number of uint32_ts added to fData by the processor.
56};
57
bsalomon98b33eb2014-10-15 11:05:26 -070058/** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
59 immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000060
joshualittb0a8a372014-09-23 09:50:21 -070061 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
mdempsky38f1f6f2015-08-27 12:57:01 -070062 processor must reach 0 before the thread terminates and the pool is destroyed.
bsalomon98b33eb2014-10-15 11:05:26 -070063 */
Brian Salomone57194f2017-01-09 15:30:02 -050064class GrProcessor : public GrProgramElement<GrProcessor> {
tomhudson@google.com168e6342012-04-18 17:49:20 +000065public:
Brian Salomon0bbecb22016-11-17 11:38:22 -050066 class TextureSampler;
Brian Salomonb014cca2016-11-18 11:39:15 -050067 class BufferAccess;
Brian Salomonf9f45122016-11-29 11:59:17 -050068 class ImageStorageAccess;
Brian Salomon0bbecb22016-11-17 11:38:22 -050069
joshualittb0a8a372014-09-23 09:50:21 -070070 virtual ~GrProcessor();
tomhudson@google.com168e6342012-04-18 17:49:20 +000071
Brian Salomonb014cca2016-11-18 11:39:15 -050072 /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader
73 code. */
joshualitteb2a6762014-12-04 11:35:33 -080074 virtual const char* name() const = 0;
bsalomon@google.com289efe02012-05-21 20:57:59 +000075
Brian Salomonb014cca2016-11-18 11:39:15 -050076 /** Human-readable dump of all information */
robertphillipse004bfc2015-11-16 09:06:59 -080077 virtual SkString dumpInfo() const {
78 SkString str;
79 str.appendf("Missing data");
80 return str;
81 }
82
Brian Salomon0bbecb22016-11-17 11:38:22 -050083 int numTextureSamplers() const { return fTextureSamplers.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000084
bsalomon@google.com6d003d12012-09-11 15:45:20 +000085 /** Returns the access pattern for the texture at index. index must be valid according to
Brian Salomon0bbecb22016-11-17 11:38:22 -050086 numTextureSamplers(). */
87 const TextureSampler& textureSampler(int index) const { return *fTextureSamplers[index]; }
twiz@google.coma5e65ec2012-08-02 15:15:16 +000088
cdalton74b8d322016-04-11 14:47:28 -070089 int numBuffers() const { return fBufferAccesses.count(); }
90
91 /** Returns the access pattern for the buffer at index. index must be valid according to
92 numBuffers(). */
Brian Salomonb014cca2016-11-18 11:39:15 -050093 const BufferAccess& bufferAccess(int index) const { return *fBufferAccesses[index]; }
cdalton74b8d322016-04-11 14:47:28 -070094
Brian Salomonf9f45122016-11-29 11:59:17 -050095 int numImageStorages() const { return fImageStorageAccesses.count(); }
96
97 /** Returns the access object for the image at index. index must be valid according to
98 numImages(). */
99 const ImageStorageAccess& imageStorageAccess(int index) const {
100 return *fImageStorageAccesses[index];
101 }
102
103 /**
104 * Platform specific built-in features that a processor can request for the fragment shader.
105 */
cdalton87332102016-02-26 12:22:02 -0800106 enum RequiredFeatures {
107 kNone_RequiredFeatures = 0,
Ethan Nicholascae3a4c2017-02-02 10:43:58 -0500108 kFragmentPosition_RequiredFeature = 1 << 0,
109 kSampleLocations_RequiredFeature = 1 << 1
cdalton87332102016-02-26 12:22:02 -0800110 };
111
112 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
113
114 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000115
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000116 void* operator new(size_t size);
117 void operator delete(void* target);
118
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000119 void* operator new(size_t size, void* placement) {
120 return ::operator new(size, placement);
121 }
122 void operator delete(void* target, void* placement) {
123 ::operator delete(target, placement);
124 }
125
Brian Salomonb014cca2016-11-18 11:39:15 -0500126 /** Helper for down-casting to a GrProcessor subclass */
joshualitt49586be2014-09-16 08:21:41 -0700127 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
128
joshualitteb2a6762014-12-04 11:35:33 -0800129 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
130
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000131protected:
cdalton87332102016-02-26 12:22:02 -0800132 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700133
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000134 /**
Brian Salomonf9f45122016-11-29 11:59:17 -0500135 * Subclasses call these from their constructor to register sampler/image sources. The processor
cdalton74b8d322016-04-11 14:47:28 -0700136 * subclass manages the lifetime of the objects (these functions only store pointers). The
Brian Salomonb014cca2016-11-18 11:39:15 -0500137 * TextureSampler and/or BufferAccess instances are typically member fields of the GrProcessor
138 * subclass. These must only be called from the constructor because GrProcessors are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000139 */
Brian Salomon0bbecb22016-11-17 11:38:22 -0500140 void addTextureSampler(const TextureSampler*);
Brian Salomonf9f45122016-11-29 11:59:17 -0500141 void addBufferAccess(const BufferAccess*);
142 void addImageStorageAccess(const ImageStorageAccess*);
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000143
Brian Salomonf9f45122016-11-29 11:59:17 -0500144 bool hasSameSamplersAndAccesses(const GrProcessor &) const;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000145
146 /**
cdalton28f45b92016-03-07 13:58:26 -0800147 * If the prcoessor will generate code that uses platform specific built-in features, then it
148 * must call these methods from its constructor. Otherwise, requests to use these features will
149 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000150 */
Ethan Nicholascae3a4c2017-02-02 10:43:58 -0500151 void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; }
cdalton28f45b92016-03-07 13:58:26 -0800152 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800153
154 void combineRequiredFeatures(const GrProcessor& other) {
155 fRequiredFeatures |= other.fRequiredFeatures;
156 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000157
joshualitteb2a6762014-12-04 11:35:33 -0800158 template <typename PROC_SUBCLASS> void initClassID() {
159 static uint32_t kClassID = GenClassID();
160 fClassID = kClassID;
161 }
162
bsalomon0e08fc12014-10-15 08:19:04 -0700163private:
joshualitteb2a6762014-12-04 11:35:33 -0800164 static uint32_t GenClassID() {
165 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
166 // atomic inc returns the old value not the incremented value. So we add
167 // 1 to the returned value.
168 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
169 if (!id) {
170 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
171 "subclass.");
172 }
173 return id;
174 }
175
Brian Salomone57194f2017-01-09 15:30:02 -0500176 friend class GrProgramElement<GrProcessor>;
177 void addPendingIOs() const;
178 void removeRefs() const;
179 void pendingIOComplete() const;
180
joshualitteb2a6762014-12-04 11:35:33 -0800181 enum {
182 kIllegalProcessorClassID = 0,
183 };
184 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000185
Brian Salomonf9f45122016-11-29 11:59:17 -0500186 uint32_t fClassID;
187 RequiredFeatures fRequiredFeatures;
188 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
189 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
190 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000191
bsalomon95740982014-09-04 13:12:37 -0700192 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000193};
194
cdalton87332102016-02-26 12:22:02 -0800195GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
196
Brian Salomon0bbecb22016-11-17 11:38:22 -0500197/**
198 * Used to represent a texture that is required by a GrProcessor. It holds a GrTexture along with
Brian Salomonf9f45122016-11-29 11:59:17 -0500199 * an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to account
200 * for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500201 */
202class GrProcessor::TextureSampler : public SkNoncopyable {
203public:
204 /**
205 * Must be initialized before adding to a GrProcessor's texture access list.
206 */
207 TextureSampler();
208
Brian Salomon514baff2016-11-17 15:17:07 -0500209 TextureSampler(GrTexture*, const GrSamplerParams&);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500210
211 explicit TextureSampler(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
Robert Phillips901f29a2017-01-24 16:24:41 -0500216 TextureSampler(GrTextureProvider*, sk_sp<GrTextureProxy>, const GrSamplerParams&);
217
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500218 // MDB TODO: ultimately we shouldn't need the texProvider parameter
219 explicit TextureSampler(GrTextureProvider*, sk_sp<GrTextureProxy>,
220 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
221 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
222 GrShaderFlags visibility = kFragment_GrShaderFlag);
223
Brian Salomon514baff2016-11-17 15:17:07 -0500224 void reset(GrTexture*, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500225 GrShaderFlags visibility = kFragment_GrShaderFlag);
226 void reset(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500227 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500228 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
229 GrShaderFlags visibility = kFragment_GrShaderFlag);
230
231 bool operator==(const TextureSampler& that) const {
Brian Salomondb4183d2016-11-17 12:48:40 -0500232 return this->texture() == that.texture() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500233 fParams == that.fParams &&
234 fVisibility == that.fVisibility;
235 }
236
237 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
238
Brian Salomondb4183d2016-11-17 12:48:40 -0500239 GrTexture* texture() const { return fTexture.get(); }
240 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500241 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500242
243 /**
244 * For internal use by GrProcessor.
245 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500246 const GrGpuResourceRef* programTexture() const { return &fTexture; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500247
248private:
249
250 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
251
252 ProgramTexture fTexture;
Brian Salomon514baff2016-11-17 15:17:07 -0500253 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500254 GrShaderFlags fVisibility;
255
256 typedef SkNoncopyable INHERITED;
257};
258
Brian Salomonb014cca2016-11-18 11:39:15 -0500259/**
260 * Used to represent a texel buffer that will be read in a GrProcessor. It holds a GrBuffer along
261 * with an associated offset and texel config.
262 */
263class GrProcessor::BufferAccess : public SkNoncopyable {
264public:
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500265 BufferAccess() = default;
266 BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
267 GrShaderFlags visibility = kFragment_GrShaderFlag) {
268 this->reset(texelConfig, buffer, visibility);
269 }
Brian Salomonb014cca2016-11-18 11:39:15 -0500270 /**
271 * Must be initialized before adding to a GrProcessor's buffer access list.
272 */
273 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
274 GrShaderFlags visibility = kFragment_GrShaderFlag) {
275 fTexelConfig = texelConfig;
276 fBuffer.set(SkRef(buffer), kRead_GrIOType);
277 fVisibility = visibility;
278 }
279
280 bool operator==(const BufferAccess& that) const {
281 return fTexelConfig == that.fTexelConfig &&
282 this->buffer() == that.buffer() &&
283 fVisibility == that.fVisibility;
284 }
285
286 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
287
288 GrPixelConfig texelConfig() const { return fTexelConfig; }
289 GrBuffer* buffer() const { return fBuffer.get(); }
290 GrShaderFlags visibility() const { return fVisibility; }
291
292 /**
293 * For internal use by GrProcessor.
294 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500295 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500296
297private:
298 GrPixelConfig fTexelConfig;
299 GrTGpuResourceRef<GrBuffer> fBuffer;
300 GrShaderFlags fVisibility;
301
302 typedef SkNoncopyable INHERITED;
303};
304
Brian Salomonf9f45122016-11-29 11:59:17 -0500305/**
306 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
307 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
308 * Currently the format of the load/store data in the shader is inferred from the texture config,
309 * though it could be made explicit.
310 */
311class GrProcessor::ImageStorageAccess : public SkNoncopyable {
312public:
313 ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, GrSLMemoryModel, GrSLRestrict,
314 GrShaderFlags visibility = kFragment_GrShaderFlag);
315
316 bool operator==(const ImageStorageAccess& that) const {
317 return this->texture() == that.texture() && fVisibility == that.fVisibility;
318 }
319
320 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
321
322 GrTexture* texture() const { return fTexture.get(); }
323 GrShaderFlags visibility() const { return fVisibility; }
324 GrIOType ioType() const { return fTexture.ioType(); }
325 GrImageStorageFormat format() const { return fFormat; }
326 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
327 GrSLRestrict restrict() const { return fRestrict; }
328
329 /**
330 * For internal use by GrProcessor.
331 */
332 const GrGpuResourceRef* programTexture() const { return &fTexture; }
333
334private:
335 GrTGpuResourceRef<GrTexture> fTexture;
336 GrShaderFlags fVisibility;
337 GrImageStorageFormat fFormat;
338 GrSLMemoryModel fMemoryModel;
339 GrSLRestrict fRestrict;
340 typedef SkNoncopyable INHERITED;
341};
342
tomhudson@google.com168e6342012-04-18 17:49:20 +0000343#endif