blob: b7abf2fbe2ef8f3b5fa3ff92ad683606fb1d07e2 [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"
Robert Phillips8a02f652017-05-12 14:49:16 -040018#include "GrSurfaceProxyPriv.h"
egdaniel9e4d6d12014-10-15 13:49:02 -070019#include "SkMath.h"
robertphillipse004bfc2015-11-16 09:06:59 -080020#include "SkString.h"
mtklein59c12e32016-05-02 07:19:41 -070021#include "../private/SkAtomics.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000022
tomhudson@google.com168e6342012-04-18 17:49:20 +000023class GrContext;
bsalomon@google.com77af6802013-10-02 13:04:56 +000024class GrCoordTransform;
egdaniel605dd0f2014-11-12 08:35:25 -080025class GrInvariantOutput;
Brian Osman32342f02017-03-04 08:12:46 -050026class GrResourceProvider;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050027class GrTextureProxy;
bsalomon95740982014-09-04 13:12:37 -070028
joshualitteb2a6762014-12-04 11:35:33 -080029/**
30 * Used by processors to build their keys. It incorporates each per-processor key into a larger
31 * shader key.
32 */
33class GrProcessorKeyBuilder {
34public:
35 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
36 SkASSERT(0 == fData->count() % sizeof(uint32_t));
37 }
38
39 void add32(uint32_t v) {
40 ++fCount;
41 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
42 }
43
44 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
45 add*() call. */
46 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
47 SkASSERT(count > 0);
48 fCount += count;
49 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
50 }
51
52 size_t size() const { return sizeof(uint32_t) * fCount; }
53
54private:
55 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
56 int fCount; // number of uint32_ts added to fData by the processor.
57};
58
bsalomon98b33eb2014-10-15 11:05:26 -070059/** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
60 immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000061
joshualittb0a8a372014-09-23 09:50:21 -070062 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
mdempsky38f1f6f2015-08-27 12:57:01 -070063 processor must reach 0 before the thread terminates and the pool is destroyed.
bsalomon98b33eb2014-10-15 11:05:26 -070064 */
Brian Salomond61c9d92017-04-10 10:54:25 -040065class GrProcessor {
tomhudson@google.com168e6342012-04-18 17:49:20 +000066public:
Brian Salomonab015ef2017-04-04 10:15:51 -040067 virtual ~GrProcessor() = default;
tomhudson@google.com168e6342012-04-18 17:49:20 +000068
Brian Salomonb014cca2016-11-18 11:39:15 -050069 /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader
70 code. */
joshualitteb2a6762014-12-04 11:35:33 -080071 virtual const char* name() const = 0;
bsalomon@google.com289efe02012-05-21 20:57:59 +000072
Brian Salomonb014cca2016-11-18 11:39:15 -050073 /** Human-readable dump of all information */
robertphillipse004bfc2015-11-16 09:06:59 -080074 virtual SkString dumpInfo() const {
75 SkString str;
76 str.appendf("Missing data");
77 return str;
78 }
79
Brian Salomonf9f45122016-11-29 11:59:17 -050080 /**
81 * Platform specific built-in features that a processor can request for the fragment shader.
82 */
cdalton87332102016-02-26 12:22:02 -080083 enum RequiredFeatures {
84 kNone_RequiredFeatures = 0,
Ethan Nicholas38657112017-02-09 17:01:22 -050085 kSampleLocations_RequiredFeature = 1 << 0
cdalton87332102016-02-26 12:22:02 -080086 };
87
88 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
89
90 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000091
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000092 void* operator new(size_t size);
93 void operator delete(void* target);
94
bsalomon@google.comd42aca32013-04-23 15:37:27 +000095 void* operator new(size_t size, void* placement) {
96 return ::operator new(size, placement);
97 }
98 void operator delete(void* target, void* placement) {
99 ::operator delete(target, placement);
100 }
101
Brian Salomonb014cca2016-11-18 11:39:15 -0500102 /** Helper for down-casting to a GrProcessor subclass */
joshualitt49586be2014-09-16 08:21:41 -0700103 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
104
joshualitteb2a6762014-12-04 11:35:33 -0800105 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
106
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000107protected:
cdalton87332102016-02-26 12:22:02 -0800108 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700109
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000110 /**
cdalton28f45b92016-03-07 13:58:26 -0800111 * If the prcoessor will generate code that uses platform specific built-in features, then it
112 * must call these methods from its constructor. Otherwise, requests to use these features will
113 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000114 */
cdalton28f45b92016-03-07 13:58:26 -0800115 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800116
117 void combineRequiredFeatures(const GrProcessor& other) {
118 fRequiredFeatures |= other.fRequiredFeatures;
119 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000120
joshualitteb2a6762014-12-04 11:35:33 -0800121 template <typename PROC_SUBCLASS> void initClassID() {
122 static uint32_t kClassID = GenClassID();
123 fClassID = kClassID;
124 }
125
bsalomon0e08fc12014-10-15 08:19:04 -0700126private:
Brian Salomond61c9d92017-04-10 10:54:25 -0400127 GrProcessor(const GrProcessor&) = delete;
128 GrProcessor& operator=(const GrProcessor&) = delete;
129
joshualitteb2a6762014-12-04 11:35:33 -0800130 static uint32_t GenClassID() {
131 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
132 // atomic inc returns the old value not the incremented value. So we add
133 // 1 to the returned value.
134 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
135 if (!id) {
136 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
137 "subclass.");
138 }
139 return id;
140 }
141
142 enum {
143 kIllegalProcessorClassID = 0,
144 };
145 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000146
Brian Salomonf9f45122016-11-29 11:59:17 -0500147 uint32_t fClassID;
148 RequiredFeatures fRequiredFeatures;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000149};
150
cdalton87332102016-02-26 12:22:02 -0800151GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
152
Brian Salomonab015ef2017-04-04 10:15:51 -0400153/** A GrProcessor with the ability to access textures, buffers, and image storages. */
154class GrResourceIOProcessor : public GrProcessor {
155public:
156 class TextureSampler;
157 class BufferAccess;
158 class ImageStorageAccess;
159
160 int numTextureSamplers() const { return fTextureSamplers.count(); }
161
162 /** Returns the access pattern for the texture at index. index must be valid according to
163 numTextureSamplers(). */
164 const TextureSampler& textureSampler(int index) const { return *fTextureSamplers[index]; }
165
166 int numBuffers() const { return fBufferAccesses.count(); }
167
168 /** Returns the access pattern for the buffer at index. index must be valid according to
169 numBuffers(). */
170 const BufferAccess& bufferAccess(int index) const { return *fBufferAccesses[index]; }
171
172 int numImageStorages() const { return fImageStorageAccesses.count(); }
173
174 /** Returns the access object for the image at index. index must be valid according to
175 numImages(). */
176 const ImageStorageAccess& imageStorageAccess(int index) const {
177 return *fImageStorageAccesses[index];
178 }
179
Robert Phillips9bee2e52017-05-29 12:37:20 -0400180 bool instantiate(GrResourceProvider* resourceProvider) const;
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400181
Brian Salomonab015ef2017-04-04 10:15:51 -0400182protected:
Robert Phillips9bee2e52017-05-29 12:37:20 -0400183 GrResourceIOProcessor() {}
Brian Salomonab015ef2017-04-04 10:15:51 -0400184
185 /**
186 * Subclasses call these from their constructor to register sampler/image sources. The processor
187 * subclass manages the lifetime of the objects (these functions only store pointers). The
188 * TextureSampler and/or BufferAccess instances are typically member fields of the GrProcessor
189 * subclass. These must only be called from the constructor because GrProcessors are immutable.
190 */
191 void addTextureSampler(const TextureSampler*);
192 void addBufferAccess(const BufferAccess*);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400193 void addImageStorageAccess(const ImageStorageAccess*);
Brian Salomonab015ef2017-04-04 10:15:51 -0400194
195 bool hasSameSamplersAndAccesses(const GrResourceIOProcessor&) const;
196
Brian Salomond61c9d92017-04-10 10:54:25 -0400197 // These methods can be used by derived classes that also derive from GrProgramElement.
198 void addPendingIOs() const;
199 void removeRefs() const;
200 void pendingIOComplete() const;
Brian Salomonab015ef2017-04-04 10:15:51 -0400201
Brian Salomond61c9d92017-04-10 10:54:25 -0400202private:
Brian Salomonab015ef2017-04-04 10:15:51 -0400203 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
204 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
205 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
206
207 typedef GrProcessor INHERITED;
208};
209
Brian Salomon0bbecb22016-11-17 11:38:22 -0500210/**
Brian Salomonab015ef2017-04-04 10:15:51 -0400211 * Used to represent a texture that is required by a GrResourceIOProcessor. It holds a GrTexture
212 * along with an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to
213 * account for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500214 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400215class GrResourceIOProcessor::TextureSampler : public SkNoncopyable {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500216public:
217 /**
218 * Must be initialized before adding to a GrProcessor's texture access list.
219 */
220 TextureSampler();
221
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400222 TextureSampler(sk_sp<GrTextureProxy>, const GrSamplerParams&);
223 explicit TextureSampler(sk_sp<GrTextureProxy>,
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500224 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
225 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
226 GrShaderFlags visibility = kFragment_GrShaderFlag);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400227 void reset(sk_sp<GrTextureProxy>, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500228 GrShaderFlags visibility = kFragment_GrShaderFlag);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400229 void reset(sk_sp<GrTextureProxy>,
Brian Salomon514baff2016-11-17 15:17:07 -0500230 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500231 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
232 GrShaderFlags visibility = kFragment_GrShaderFlag);
233
234 bool operator==(const TextureSampler& that) const {
Robert Phillips18166ee2017-06-01 12:55:44 -0400235 return this->proxy() == that.proxy() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500236 fParams == that.fParams &&
237 fVisibility == that.fVisibility;
238 }
239
240 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
241
Robert Phillips9bee2e52017-05-29 12:37:20 -0400242 // 'instantiate' should only ever be called at flush time.
243 bool instantiate(GrResourceProvider* resourceProvider) const {
Robert Phillips18166ee2017-06-01 12:55:44 -0400244 return SkToBool(fProxyRef.get()->instantiate(resourceProvider));
Robert Phillips9bee2e52017-05-29 12:37:20 -0400245 }
246
247 // 'peekTexture' should only ever be called after a successful 'instantiate' call
248 GrTexture* peekTexture() const {
Robert Phillips18166ee2017-06-01 12:55:44 -0400249 SkASSERT(fProxyRef.get()->priv().peekTexture());
250 return fProxyRef.get()->priv().peekTexture();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400251 }
252
Robert Phillips18166ee2017-06-01 12:55:44 -0400253 GrTextureProxy* proxy() const { return fProxyRef.get()->asTextureProxy(); }
Brian Salomondb4183d2016-11-17 12:48:40 -0500254 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500255 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500256
Brian Salomon06e547c2017-06-09 16:11:32 -0400257 bool isInitialized() const { return SkToBool(fProxyRef.get()); }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500258 /**
259 * For internal use by GrProcessor.
260 */
Robert Phillips18166ee2017-06-01 12:55:44 -0400261 const GrSurfaceProxyRef* programProxy() const { return &fProxyRef; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500262
263private:
Robert Phillips18166ee2017-06-01 12:55:44 -0400264 GrSurfaceProxyRef fProxyRef;
Brian Salomon514baff2016-11-17 15:17:07 -0500265 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500266 GrShaderFlags fVisibility;
267
268 typedef SkNoncopyable INHERITED;
269};
270
Brian Salomonb014cca2016-11-18 11:39:15 -0500271/**
Brian Salomonab015ef2017-04-04 10:15:51 -0400272 * Used to represent a texel buffer that will be read in a GrResourceIOProcessor. It holds a
273 * GrBuffer along with an associated offset and texel config.
Brian Salomonb014cca2016-11-18 11:39:15 -0500274 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400275class GrResourceIOProcessor::BufferAccess : public SkNoncopyable {
Brian Salomonb014cca2016-11-18 11:39:15 -0500276public:
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500277 BufferAccess() = default;
278 BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
279 GrShaderFlags visibility = kFragment_GrShaderFlag) {
280 this->reset(texelConfig, buffer, visibility);
281 }
Brian Salomonb014cca2016-11-18 11:39:15 -0500282 /**
283 * Must be initialized before adding to a GrProcessor's buffer access list.
284 */
285 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
286 GrShaderFlags visibility = kFragment_GrShaderFlag) {
287 fTexelConfig = texelConfig;
288 fBuffer.set(SkRef(buffer), kRead_GrIOType);
289 fVisibility = visibility;
290 }
291
292 bool operator==(const BufferAccess& that) const {
293 return fTexelConfig == that.fTexelConfig &&
294 this->buffer() == that.buffer() &&
295 fVisibility == that.fVisibility;
296 }
297
298 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
299
300 GrPixelConfig texelConfig() const { return fTexelConfig; }
301 GrBuffer* buffer() const { return fBuffer.get(); }
302 GrShaderFlags visibility() const { return fVisibility; }
303
304 /**
305 * For internal use by GrProcessor.
306 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500307 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500308
309private:
Brian Salomonab015ef2017-04-04 10:15:51 -0400310 GrPixelConfig fTexelConfig;
311 GrTGpuResourceRef<GrBuffer> fBuffer;
312 GrShaderFlags fVisibility;
Brian Salomonb014cca2016-11-18 11:39:15 -0500313
314 typedef SkNoncopyable INHERITED;
315};
316
Brian Salomonf9f45122016-11-29 11:59:17 -0500317/**
318 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
319 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
320 * Currently the format of the load/store data in the shader is inferred from the texture config,
321 * though it could be made explicit.
322 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400323class GrResourceIOProcessor::ImageStorageAccess : public SkNoncopyable {
Brian Salomonf9f45122016-11-29 11:59:17 -0500324public:
Robert Phillips8a02f652017-05-12 14:49:16 -0400325 ImageStorageAccess(sk_sp<GrTextureProxy>, GrIOType, GrSLMemoryModel, GrSLRestrict,
Brian Salomonf9f45122016-11-29 11:59:17 -0500326 GrShaderFlags visibility = kFragment_GrShaderFlag);
327
328 bool operator==(const ImageStorageAccess& that) const {
Robert Phillips8a02f652017-05-12 14:49:16 -0400329 return this->proxy() == that.proxy() && fVisibility == that.fVisibility;
Brian Salomonf9f45122016-11-29 11:59:17 -0500330 }
331
332 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
333
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400334 GrTextureProxy* proxy() const { return fProxyRef.get()->asTextureProxy(); }
Brian Salomonf9f45122016-11-29 11:59:17 -0500335 GrShaderFlags visibility() const { return fVisibility; }
Robert Phillips8a02f652017-05-12 14:49:16 -0400336 GrIOType ioType() const { return fProxyRef.ioType(); }
Brian Salomonf9f45122016-11-29 11:59:17 -0500337 GrImageStorageFormat format() const { return fFormat; }
338 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
339 GrSLRestrict restrict() const { return fRestrict; }
340
Robert Phillips9bee2e52017-05-29 12:37:20 -0400341 // 'instantiate' should only ever be called at flush time.
342 bool instantiate(GrResourceProvider* resourceProvider) const {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400343 return SkToBool(fProxyRef.get()->instantiate(resourceProvider));
Robert Phillips9bee2e52017-05-29 12:37:20 -0400344 }
345 // 'peekTexture' should only ever be called after a successful 'instantiate' call
346 GrTexture* peekTexture() const {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400347 SkASSERT(fProxyRef.get()->priv().peekTexture());
348 return fProxyRef.get()->priv().peekTexture();
Robert Phillips8a02f652017-05-12 14:49:16 -0400349 }
350
Brian Salomonf9f45122016-11-29 11:59:17 -0500351 /**
352 * For internal use by GrProcessor.
353 */
Robert Phillips8a02f652017-05-12 14:49:16 -0400354 const GrSurfaceProxyRef* programProxy() const { return &fProxyRef; }
Brian Salomonf9f45122016-11-29 11:59:17 -0500355
356private:
Robert Phillips8a02f652017-05-12 14:49:16 -0400357 GrSurfaceProxyRef fProxyRef;
358 GrShaderFlags fVisibility;
Brian Salomonab015ef2017-04-04 10:15:51 -0400359 GrImageStorageFormat fFormat;
Robert Phillips8a02f652017-05-12 14:49:16 -0400360 GrSLMemoryModel fMemoryModel;
361 GrSLRestrict fRestrict;
Brian Salomonf9f45122016-11-29 11:59:17 -0500362 typedef SkNoncopyable INHERITED;
363};
364
tomhudson@google.com168e6342012-04-18 17:49:20 +0000365#endif