blob: 027a34e334500a0d98767d0c1a69386e2d5aa93c [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 Phillipsa91e0b72017-05-01 13:12:20 -0400180 bool isBad() const { return fIsBad; }
181
Brian Salomonab015ef2017-04-04 10:15:51 -0400182protected:
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400183 GrResourceIOProcessor() : fIsBad(false) {}
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 Phillips8a02f652017-05-12 14:49:16 -0400193 void addImageStorageAccess(GrResourceProvider* resourceProvider, 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
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400202 void markAsBad() { fIsBad = true; }
203
Brian Salomond61c9d92017-04-10 10:54:25 -0400204private:
Brian Salomonab015ef2017-04-04 10:15:51 -0400205 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
206 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
207 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400208 bool fIsBad;
Brian Salomonab015ef2017-04-04 10:15:51 -0400209
210 typedef GrProcessor INHERITED;
211};
212
Brian Salomon0bbecb22016-11-17 11:38:22 -0500213/**
Brian Salomonab015ef2017-04-04 10:15:51 -0400214 * Used to represent a texture that is required by a GrResourceIOProcessor. It holds a GrTexture
215 * along with an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to
216 * account for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500217 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400218class GrResourceIOProcessor::TextureSampler : public SkNoncopyable {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500219public:
220 /**
221 * Must be initialized before adding to a GrProcessor's texture access list.
222 */
223 TextureSampler();
224
Robert Phillipsc3757042017-05-17 13:00:14 +0000225 // MDB TODO: this is the last GrTexture-based reset call!
226 void reset(GrTexture*,
227 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
228 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
229 GrShaderFlags visibility = kFragment_GrShaderFlag);
230
Brian Osman32342f02017-03-04 08:12:46 -0500231 // MDB TODO: ultimately we shouldn't need the resource provider parameter
232 TextureSampler(GrResourceProvider*, sk_sp<GrTextureProxy>, const GrSamplerParams&);
233 explicit TextureSampler(GrResourceProvider*, sk_sp<GrTextureProxy>,
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500234 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
235 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
236 GrShaderFlags visibility = kFragment_GrShaderFlag);
Brian Osman32342f02017-03-04 08:12:46 -0500237 void reset(GrResourceProvider*, sk_sp<GrTextureProxy>, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500238 GrShaderFlags visibility = kFragment_GrShaderFlag);
Brian Osman32342f02017-03-04 08:12:46 -0500239 void reset(GrResourceProvider*, sk_sp<GrTextureProxy>,
Brian Salomon514baff2016-11-17 15:17:07 -0500240 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500241 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
242 GrShaderFlags visibility = kFragment_GrShaderFlag);
243
244 bool operator==(const TextureSampler& that) const {
Brian Salomondb4183d2016-11-17 12:48:40 -0500245 return this->texture() == that.texture() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500246 fParams == that.fParams &&
247 fVisibility == that.fVisibility;
248 }
249
250 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
251
Brian Salomondb4183d2016-11-17 12:48:40 -0500252 GrTexture* texture() const { return fTexture.get(); }
253 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500254 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500255
256 /**
257 * For internal use by GrProcessor.
258 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500259 const GrGpuResourceRef* programTexture() const { return &fTexture; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500260
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400261 bool isBad() const { return !fTexture.get(); }
262
Brian Salomon0bbecb22016-11-17 11:38:22 -0500263private:
264
265 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
266
267 ProgramTexture fTexture;
Brian Salomon514baff2016-11-17 15:17:07 -0500268 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500269 GrShaderFlags fVisibility;
270
271 typedef SkNoncopyable INHERITED;
272};
273
Brian Salomonb014cca2016-11-18 11:39:15 -0500274/**
Brian Salomonab015ef2017-04-04 10:15:51 -0400275 * Used to represent a texel buffer that will be read in a GrResourceIOProcessor. It holds a
276 * GrBuffer along with an associated offset and texel config.
Brian Salomonb014cca2016-11-18 11:39:15 -0500277 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400278class GrResourceIOProcessor::BufferAccess : public SkNoncopyable {
Brian Salomonb014cca2016-11-18 11:39:15 -0500279public:
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500280 BufferAccess() = default;
281 BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
282 GrShaderFlags visibility = kFragment_GrShaderFlag) {
283 this->reset(texelConfig, buffer, visibility);
284 }
Brian Salomonb014cca2016-11-18 11:39:15 -0500285 /**
286 * Must be initialized before adding to a GrProcessor's buffer access list.
287 */
288 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
289 GrShaderFlags visibility = kFragment_GrShaderFlag) {
290 fTexelConfig = texelConfig;
291 fBuffer.set(SkRef(buffer), kRead_GrIOType);
292 fVisibility = visibility;
293 }
294
295 bool operator==(const BufferAccess& that) const {
296 return fTexelConfig == that.fTexelConfig &&
297 this->buffer() == that.buffer() &&
298 fVisibility == that.fVisibility;
299 }
300
301 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
302
303 GrPixelConfig texelConfig() const { return fTexelConfig; }
304 GrBuffer* buffer() const { return fBuffer.get(); }
305 GrShaderFlags visibility() const { return fVisibility; }
306
307 /**
308 * For internal use by GrProcessor.
309 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500310 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500311
312private:
Brian Salomonab015ef2017-04-04 10:15:51 -0400313 GrPixelConfig fTexelConfig;
314 GrTGpuResourceRef<GrBuffer> fBuffer;
315 GrShaderFlags fVisibility;
Brian Salomonb014cca2016-11-18 11:39:15 -0500316
317 typedef SkNoncopyable INHERITED;
318};
319
Brian Salomonf9f45122016-11-29 11:59:17 -0500320/**
321 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
322 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
323 * Currently the format of the load/store data in the shader is inferred from the texture config,
324 * though it could be made explicit.
325 */
Brian Salomonab015ef2017-04-04 10:15:51 -0400326class GrResourceIOProcessor::ImageStorageAccess : public SkNoncopyable {
Brian Salomonf9f45122016-11-29 11:59:17 -0500327public:
Robert Phillips8a02f652017-05-12 14:49:16 -0400328 ImageStorageAccess(sk_sp<GrTextureProxy>, GrIOType, GrSLMemoryModel, GrSLRestrict,
Brian Salomonf9f45122016-11-29 11:59:17 -0500329 GrShaderFlags visibility = kFragment_GrShaderFlag);
330
331 bool operator==(const ImageStorageAccess& that) const {
Robert Phillips8a02f652017-05-12 14:49:16 -0400332 return this->proxy() == that.proxy() && fVisibility == that.fVisibility;
Brian Salomonf9f45122016-11-29 11:59:17 -0500333 }
334
335 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
336
Robert Phillips8a02f652017-05-12 14:49:16 -0400337 GrTexture* texture() const { return fProxyRef.getProxy()->priv().peekTexture(); }
338 GrTextureProxy* proxy() const { return fProxyRef.getProxy()->asTextureProxy(); }
Brian Salomonf9f45122016-11-29 11:59:17 -0500339 GrShaderFlags visibility() const { return fVisibility; }
Robert Phillips8a02f652017-05-12 14:49:16 -0400340 GrIOType ioType() const { return fProxyRef.ioType(); }
Brian Salomonf9f45122016-11-29 11:59:17 -0500341 GrImageStorageFormat format() const { return fFormat; }
342 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
343 GrSLRestrict restrict() const { return fRestrict; }
344
Robert Phillips8a02f652017-05-12 14:49:16 -0400345 // MDB: In the future this should be renamed instantiate
346 bool isBad(GrResourceProvider* resourceProvider) const {
347 return SkToBool(!fProxyRef.getProxy()->instantiate(resourceProvider));
348 }
349
Brian Salomonf9f45122016-11-29 11:59:17 -0500350 /**
351 * For internal use by GrProcessor.
352 */
Robert Phillips8a02f652017-05-12 14:49:16 -0400353 const GrSurfaceProxyRef* programProxy() const { return &fProxyRef; }
Brian Salomonf9f45122016-11-29 11:59:17 -0500354
355private:
Robert Phillips8a02f652017-05-12 14:49:16 -0400356 GrSurfaceProxyRef fProxyRef;
357 GrShaderFlags fVisibility;
Brian Salomonab015ef2017-04-04 10:15:51 -0400358 GrImageStorageFormat fFormat;
Robert Phillips8a02f652017-05-12 14:49:16 -0400359 GrSLMemoryModel fMemoryModel;
360 GrSLRestrict fRestrict;
Brian Salomonf9f45122016-11-29 11:59:17 -0500361 typedef SkNoncopyable INHERITED;
362};
363
tomhudson@google.com168e6342012-04-18 17:49:20 +0000364#endif