blob: 54691812e1535f7aa84b42dee010d4dddff75d4a [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;
Brian Osman32342f02017-03-04 08:12:46 -050025class GrResourceProvider;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050026class 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 Salomonab015ef2017-04-04 10:15:51 -040066 virtual ~GrProcessor() = default;
tomhudson@google.com168e6342012-04-18 17:49:20 +000067
Brian Salomonb014cca2016-11-18 11:39:15 -050068 /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader
69 code. */
joshualitteb2a6762014-12-04 11:35:33 -080070 virtual const char* name() const = 0;
bsalomon@google.com289efe02012-05-21 20:57:59 +000071
Brian Salomonb014cca2016-11-18 11:39:15 -050072 /** Human-readable dump of all information */
robertphillipse004bfc2015-11-16 09:06:59 -080073 virtual SkString dumpInfo() const {
74 SkString str;
75 str.appendf("Missing data");
76 return str;
77 }
78
Brian Salomonf9f45122016-11-29 11:59:17 -050079 /**
80 * Platform specific built-in features that a processor can request for the fragment shader.
81 */
cdalton87332102016-02-26 12:22:02 -080082 enum RequiredFeatures {
83 kNone_RequiredFeatures = 0,
Ethan Nicholas38657112017-02-09 17:01:22 -050084 kSampleLocations_RequiredFeature = 1 << 0
cdalton87332102016-02-26 12:22:02 -080085 };
86
87 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
88
89 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000090
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000091 void* operator new(size_t size);
92 void operator delete(void* target);
93
bsalomon@google.comd42aca32013-04-23 15:37:27 +000094 void* operator new(size_t size, void* placement) {
95 return ::operator new(size, placement);
96 }
97 void operator delete(void* target, void* placement) {
98 ::operator delete(target, placement);
99 }
100
Brian Salomonb014cca2016-11-18 11:39:15 -0500101 /** Helper for down-casting to a GrProcessor subclass */
joshualitt49586be2014-09-16 08:21:41 -0700102 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
103
joshualitteb2a6762014-12-04 11:35:33 -0800104 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
105
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000106protected:
cdalton87332102016-02-26 12:22:02 -0800107 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700108
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000109 /**
cdalton28f45b92016-03-07 13:58:26 -0800110 * If the prcoessor will generate code that uses platform specific built-in features, then it
111 * must call these methods from its constructor. Otherwise, requests to use these features will
112 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000113 */
cdalton28f45b92016-03-07 13:58:26 -0800114 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800115
116 void combineRequiredFeatures(const GrProcessor& other) {
117 fRequiredFeatures |= other.fRequiredFeatures;
118 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000119
joshualitteb2a6762014-12-04 11:35:33 -0800120 template <typename PROC_SUBCLASS> void initClassID() {
121 static uint32_t kClassID = GenClassID();
122 fClassID = kClassID;
123 }
124
bsalomon0e08fc12014-10-15 08:19:04 -0700125private:
joshualitteb2a6762014-12-04 11:35:33 -0800126 static uint32_t GenClassID() {
127 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
128 // atomic inc returns the old value not the incremented value. So we add
129 // 1 to the returned value.
130 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
131 if (!id) {
132 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
133 "subclass.");
134 }
135 return id;
136 }
137
Brian Salomone57194f2017-01-09 15:30:02 -0500138 friend class GrProgramElement<GrProcessor>;
Brian Salomonab015ef2017-04-04 10:15:51 -0400139 virtual void addPendingIOs() const {}
140 virtual void removeRefs() const {}
141 virtual void pendingIOComplete() const {}
Brian Salomone57194f2017-01-09 15:30:02 -0500142
joshualitteb2a6762014-12-04 11:35:33 -0800143 enum {
144 kIllegalProcessorClassID = 0,
145 };
146 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000147
Brian Salomonf9f45122016-11-29 11:59:17 -0500148 uint32_t fClassID;
149 RequiredFeatures fRequiredFeatures;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000150
bsalomon95740982014-09-04 13:12:37 -0700151 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000152};
153
cdalton87332102016-02-26 12:22:02 -0800154GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
155
Brian Salomonab015ef2017-04-04 10:15:51 -0400156/** A GrProcessor with the ability to access textures, buffers, and image storages. */
157class GrResourceIOProcessor : public GrProcessor {
158public:
159 class TextureSampler;
160 class BufferAccess;
161 class ImageStorageAccess;
162
163 int numTextureSamplers() const { return fTextureSamplers.count(); }
164
165 /** Returns the access pattern for the texture at index. index must be valid according to
166 numTextureSamplers(). */
167 const TextureSampler& textureSampler(int index) const { return *fTextureSamplers[index]; }
168
169 int numBuffers() const { return fBufferAccesses.count(); }
170
171 /** Returns the access pattern for the buffer at index. index must be valid according to
172 numBuffers(). */
173 const BufferAccess& bufferAccess(int index) const { return *fBufferAccesses[index]; }
174
175 int numImageStorages() const { return fImageStorageAccesses.count(); }
176
177 /** Returns the access object for the image at index. index must be valid according to
178 numImages(). */
179 const ImageStorageAccess& imageStorageAccess(int index) const {
180 return *fImageStorageAccesses[index];
181 }
182
183protected:
184 GrResourceIOProcessor() = default;
185
186 /**
187 * Subclasses call these from their constructor to register sampler/image sources. The processor
188 * subclass manages the lifetime of the objects (these functions only store pointers). The
189 * TextureSampler and/or BufferAccess instances are typically member fields of the GrProcessor
190 * subclass. These must only be called from the constructor because GrProcessors are immutable.
191 */
192 void addTextureSampler(const TextureSampler*);
193 void addBufferAccess(const BufferAccess*);
194 void addImageStorageAccess(const ImageStorageAccess*);
195
196 bool hasSameSamplersAndAccesses(const GrResourceIOProcessor&) const;
197
198private:
199 friend class GrProgramElement<GrProcessor>;
200 void addPendingIOs() const override;
201 void removeRefs() const override;
202 void pendingIOComplete() const override;
203
204 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
205 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
206 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
207
208 typedef GrProcessor INHERITED;
209};
210
Brian Salomon0bbecb22016-11-17 11:38:22 -0500211/**
Brian Salomonab015ef2017-04-04 10:15:51 -0400212 * Used to represent a texture that is required by a GrResourceIOProcessor. It holds a GrTexture
213 * along with an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to
214 * account for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500215 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400216class GrResourceIOProcessor::TextureSampler : public SkNoncopyable {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500217public:
218 /**
219 * Must be initialized before adding to a GrProcessor's texture access list.
220 */
221 TextureSampler();
222
Brian Salomon514baff2016-11-17 15:17:07 -0500223 TextureSampler(GrTexture*, const GrSamplerParams&);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500224 explicit TextureSampler(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500225 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500226 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
227 GrShaderFlags visibility = kFragment_GrShaderFlag);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500228 void reset(GrTexture*, const GrSamplerParams&,
229 GrShaderFlags visibility = kFragment_GrShaderFlag);
230 void reset(GrTexture*,
231 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
232 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
233 GrShaderFlags visibility = kFragment_GrShaderFlag);
Robert Phillips901f29a2017-01-24 16:24:41 -0500234
Brian Osman32342f02017-03-04 08:12:46 -0500235 // MDB TODO: ultimately we shouldn't need the resource provider parameter
236 TextureSampler(GrResourceProvider*, sk_sp<GrTextureProxy>, const GrSamplerParams&);
237 explicit TextureSampler(GrResourceProvider*, sk_sp<GrTextureProxy>,
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500238 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
239 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
240 GrShaderFlags visibility = kFragment_GrShaderFlag);
Brian Osman32342f02017-03-04 08:12:46 -0500241 void reset(GrResourceProvider*, sk_sp<GrTextureProxy>, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500242 GrShaderFlags visibility = kFragment_GrShaderFlag);
Brian Osman32342f02017-03-04 08:12:46 -0500243 void reset(GrResourceProvider*, sk_sp<GrTextureProxy>,
Brian Salomon514baff2016-11-17 15:17:07 -0500244 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500245 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
246 GrShaderFlags visibility = kFragment_GrShaderFlag);
247
248 bool operator==(const TextureSampler& that) const {
Brian Salomondb4183d2016-11-17 12:48:40 -0500249 return this->texture() == that.texture() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500250 fParams == that.fParams &&
251 fVisibility == that.fVisibility;
252 }
253
254 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
255
Brian Salomondb4183d2016-11-17 12:48:40 -0500256 GrTexture* texture() const { return fTexture.get(); }
257 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500258 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500259
260 /**
261 * For internal use by GrProcessor.
262 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500263 const GrGpuResourceRef* programTexture() const { return &fTexture; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500264
265private:
266
267 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
268
269 ProgramTexture fTexture;
Brian Salomon514baff2016-11-17 15:17:07 -0500270 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500271 GrShaderFlags fVisibility;
272
273 typedef SkNoncopyable INHERITED;
274};
275
Brian Salomonb014cca2016-11-18 11:39:15 -0500276/**
Brian Salomonab015ef2017-04-04 10:15:51 -0400277 * Used to represent a texel buffer that will be read in a GrResourceIOProcessor. It holds a
278 * GrBuffer along with an associated offset and texel config.
Brian Salomonb014cca2016-11-18 11:39:15 -0500279 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400280class GrResourceIOProcessor::BufferAccess : public SkNoncopyable {
Brian Salomonb014cca2016-11-18 11:39:15 -0500281public:
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500282 BufferAccess() = default;
283 BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
284 GrShaderFlags visibility = kFragment_GrShaderFlag) {
285 this->reset(texelConfig, buffer, visibility);
286 }
Brian Salomonb014cca2016-11-18 11:39:15 -0500287 /**
288 * Must be initialized before adding to a GrProcessor's buffer access list.
289 */
290 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
291 GrShaderFlags visibility = kFragment_GrShaderFlag) {
292 fTexelConfig = texelConfig;
293 fBuffer.set(SkRef(buffer), kRead_GrIOType);
294 fVisibility = visibility;
295 }
296
297 bool operator==(const BufferAccess& that) const {
298 return fTexelConfig == that.fTexelConfig &&
299 this->buffer() == that.buffer() &&
300 fVisibility == that.fVisibility;
301 }
302
303 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
304
305 GrPixelConfig texelConfig() const { return fTexelConfig; }
306 GrBuffer* buffer() const { return fBuffer.get(); }
307 GrShaderFlags visibility() const { return fVisibility; }
308
309 /**
310 * For internal use by GrProcessor.
311 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500312 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500313
314private:
Brian Salomonab015ef2017-04-04 10:15:51 -0400315 GrPixelConfig fTexelConfig;
316 GrTGpuResourceRef<GrBuffer> fBuffer;
317 GrShaderFlags fVisibility;
Brian Salomonb014cca2016-11-18 11:39:15 -0500318
319 typedef SkNoncopyable INHERITED;
320};
321
Brian Salomonf9f45122016-11-29 11:59:17 -0500322/**
323 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
324 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
325 * Currently the format of the load/store data in the shader is inferred from the texture config,
326 * though it could be made explicit.
327 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400328class GrResourceIOProcessor::ImageStorageAccess : public SkNoncopyable {
Brian Salomonf9f45122016-11-29 11:59:17 -0500329public:
330 ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, GrSLMemoryModel, GrSLRestrict,
331 GrShaderFlags visibility = kFragment_GrShaderFlag);
332
333 bool operator==(const ImageStorageAccess& that) const {
334 return this->texture() == that.texture() && fVisibility == that.fVisibility;
335 }
336
337 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
338
339 GrTexture* texture() const { return fTexture.get(); }
340 GrShaderFlags visibility() const { return fVisibility; }
341 GrIOType ioType() const { return fTexture.ioType(); }
342 GrImageStorageFormat format() const { return fFormat; }
343 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
344 GrSLRestrict restrict() const { return fRestrict; }
345
346 /**
347 * For internal use by GrProcessor.
348 */
349 const GrGpuResourceRef* programTexture() const { return &fTexture; }
350
351private:
Brian Salomonab015ef2017-04-04 10:15:51 -0400352 GrTGpuResourceRef<GrTexture> fTexture;
353 GrShaderFlags fVisibility;
354 GrImageStorageFormat fFormat;
355 GrSLMemoryModel fMemoryModel;
356 GrSLRestrict fRestrict;
Brian Salomonf9f45122016-11-29 11:59:17 -0500357 typedef SkNoncopyable INHERITED;
358};
359
tomhudson@google.com168e6342012-04-18 17:49:20 +0000360#endif