blob: a8e31f2c86653a654057584cc02995b7f636dd5a [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 Nicholasde4d3012017-01-19 16:58:02 -0500108 kSampleLocations_RequiredFeature = 1 << 0
cdalton87332102016-02-26 12:22:02 -0800109 };
110
111 GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
112
113 RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000114
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000115 void* operator new(size_t size);
116 void operator delete(void* target);
117
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000118 void* operator new(size_t size, void* placement) {
119 return ::operator new(size, placement);
120 }
121 void operator delete(void* target, void* placement) {
122 ::operator delete(target, placement);
123 }
124
Brian Salomonb014cca2016-11-18 11:39:15 -0500125 /** Helper for down-casting to a GrProcessor subclass */
joshualitt49586be2014-09-16 08:21:41 -0700126 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
127
joshualitteb2a6762014-12-04 11:35:33 -0800128 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
129
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000130protected:
cdalton87332102016-02-26 12:22:02 -0800131 GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
bsalomon420d7e92014-10-16 09:18:09 -0700132
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000133 /**
Brian Salomonf9f45122016-11-29 11:59:17 -0500134 * Subclasses call these from their constructor to register sampler/image sources. The processor
cdalton74b8d322016-04-11 14:47:28 -0700135 * subclass manages the lifetime of the objects (these functions only store pointers). The
Brian Salomonb014cca2016-11-18 11:39:15 -0500136 * TextureSampler and/or BufferAccess instances are typically member fields of the GrProcessor
137 * subclass. These must only be called from the constructor because GrProcessors are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000138 */
Brian Salomon0bbecb22016-11-17 11:38:22 -0500139 void addTextureSampler(const TextureSampler*);
Brian Salomonf9f45122016-11-29 11:59:17 -0500140 void addBufferAccess(const BufferAccess*);
141 void addImageStorageAccess(const ImageStorageAccess*);
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000142
Brian Salomonf9f45122016-11-29 11:59:17 -0500143 bool hasSameSamplersAndAccesses(const GrProcessor &) const;
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000144
145 /**
cdalton28f45b92016-03-07 13:58:26 -0800146 * If the prcoessor will generate code that uses platform specific built-in features, then it
147 * must call these methods from its constructor. Otherwise, requests to use these features will
148 * be denied.
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000149 */
cdalton28f45b92016-03-07 13:58:26 -0800150 void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
cdalton87332102016-02-26 12:22:02 -0800151
152 void combineRequiredFeatures(const GrProcessor& other) {
153 fRequiredFeatures |= other.fRequiredFeatures;
154 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000155
joshualitteb2a6762014-12-04 11:35:33 -0800156 template <typename PROC_SUBCLASS> void initClassID() {
157 static uint32_t kClassID = GenClassID();
158 fClassID = kClassID;
159 }
160
bsalomon0e08fc12014-10-15 08:19:04 -0700161private:
joshualitteb2a6762014-12-04 11:35:33 -0800162 static uint32_t GenClassID() {
163 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
164 // atomic inc returns the old value not the incremented value. So we add
165 // 1 to the returned value.
166 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
167 if (!id) {
168 SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
169 "subclass.");
170 }
171 return id;
172 }
173
Brian Salomone57194f2017-01-09 15:30:02 -0500174 friend class GrProgramElement<GrProcessor>;
175 void addPendingIOs() const;
176 void removeRefs() const;
177 void pendingIOComplete() const;
178
joshualitteb2a6762014-12-04 11:35:33 -0800179 enum {
180 kIllegalProcessorClassID = 0,
181 };
182 static int32_t gCurrProcessorClassID;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000183
Brian Salomonf9f45122016-11-29 11:59:17 -0500184 uint32_t fClassID;
185 RequiredFeatures fRequiredFeatures;
186 SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
187 SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
188 SkSTArray<1, const ImageStorageAccess*, true> fImageStorageAccesses;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000189
bsalomon95740982014-09-04 13:12:37 -0700190 typedef GrProgramElement INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000191};
192
cdalton87332102016-02-26 12:22:02 -0800193GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
194
Brian Salomon0bbecb22016-11-17 11:38:22 -0500195/**
196 * Used to represent a texture that is required by a GrProcessor. It holds a GrTexture along with
Brian Salomonf9f45122016-11-29 11:59:17 -0500197 * an associated GrSamplerParams. TextureSamplers don't perform any coord manipulation to account
198 * for texture origin.
Brian Salomon0bbecb22016-11-17 11:38:22 -0500199 */
200class GrProcessor::TextureSampler : public SkNoncopyable {
201public:
202 /**
203 * Must be initialized before adding to a GrProcessor's texture access list.
204 */
205 TextureSampler();
206
Brian Salomon514baff2016-11-17 15:17:07 -0500207 TextureSampler(GrTexture*, const GrSamplerParams&);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500208
209 explicit TextureSampler(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500210 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500211 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
212 GrShaderFlags visibility = kFragment_GrShaderFlag);
213
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500214 // MDB TODO: ultimately we shouldn't need the texProvider parameter
215 explicit TextureSampler(GrTextureProvider*, sk_sp<GrTextureProxy>,
216 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
217 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
218 GrShaderFlags visibility = kFragment_GrShaderFlag);
219
Brian Salomon514baff2016-11-17 15:17:07 -0500220 void reset(GrTexture*, const GrSamplerParams&,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500221 GrShaderFlags visibility = kFragment_GrShaderFlag);
222 void reset(GrTexture*,
Brian Salomon514baff2016-11-17 15:17:07 -0500223 GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500224 SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode,
225 GrShaderFlags visibility = kFragment_GrShaderFlag);
226
227 bool operator==(const TextureSampler& that) const {
Brian Salomondb4183d2016-11-17 12:48:40 -0500228 return this->texture() == that.texture() &&
Brian Salomon0bbecb22016-11-17 11:38:22 -0500229 fParams == that.fParams &&
230 fVisibility == that.fVisibility;
231 }
232
233 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
234
Brian Salomondb4183d2016-11-17 12:48:40 -0500235 GrTexture* texture() const { return fTexture.get(); }
236 GrShaderFlags visibility() const { return fVisibility; }
Brian Salomon514baff2016-11-17 15:17:07 -0500237 const GrSamplerParams& params() const { return fParams; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500238
239 /**
240 * For internal use by GrProcessor.
241 */
Brian Salomondb4183d2016-11-17 12:48:40 -0500242 const GrGpuResourceRef* programTexture() const { return &fTexture; }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500243
244private:
245
246 typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
247
248 ProgramTexture fTexture;
Brian Salomon514baff2016-11-17 15:17:07 -0500249 GrSamplerParams fParams;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500250 GrShaderFlags fVisibility;
251
252 typedef SkNoncopyable INHERITED;
253};
254
Brian Salomonb014cca2016-11-18 11:39:15 -0500255/**
256 * Used to represent a texel buffer that will be read in a GrProcessor. It holds a GrBuffer along
257 * with an associated offset and texel config.
258 */
259class GrProcessor::BufferAccess : public SkNoncopyable {
260public:
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500261 BufferAccess() = default;
262 BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
263 GrShaderFlags visibility = kFragment_GrShaderFlag) {
264 this->reset(texelConfig, buffer, visibility);
265 }
Brian Salomonb014cca2016-11-18 11:39:15 -0500266 /**
267 * Must be initialized before adding to a GrProcessor's buffer access list.
268 */
269 void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
270 GrShaderFlags visibility = kFragment_GrShaderFlag) {
271 fTexelConfig = texelConfig;
272 fBuffer.set(SkRef(buffer), kRead_GrIOType);
273 fVisibility = visibility;
274 }
275
276 bool operator==(const BufferAccess& that) const {
277 return fTexelConfig == that.fTexelConfig &&
278 this->buffer() == that.buffer() &&
279 fVisibility == that.fVisibility;
280 }
281
282 bool operator!=(const BufferAccess& that) const { return !(*this == that); }
283
284 GrPixelConfig texelConfig() const { return fTexelConfig; }
285 GrBuffer* buffer() const { return fBuffer.get(); }
286 GrShaderFlags visibility() const { return fVisibility; }
287
288 /**
289 * For internal use by GrProcessor.
290 */
Brian Salomonf9f45122016-11-29 11:59:17 -0500291 const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
Brian Salomonb014cca2016-11-18 11:39:15 -0500292
293private:
294 GrPixelConfig fTexelConfig;
295 GrTGpuResourceRef<GrBuffer> fBuffer;
296 GrShaderFlags fVisibility;
297
298 typedef SkNoncopyable INHERITED;
299};
300
Brian Salomonf9f45122016-11-29 11:59:17 -0500301/**
302 * This is used by a GrProcessor to access a texture using image load/store in its shader code.
303 * ImageStorageAccesses don't perform any coord manipulation to account for texture origin.
304 * Currently the format of the load/store data in the shader is inferred from the texture config,
305 * though it could be made explicit.
306 */
307class GrProcessor::ImageStorageAccess : public SkNoncopyable {
308public:
309 ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, GrSLMemoryModel, GrSLRestrict,
310 GrShaderFlags visibility = kFragment_GrShaderFlag);
311
312 bool operator==(const ImageStorageAccess& that) const {
313 return this->texture() == that.texture() && fVisibility == that.fVisibility;
314 }
315
316 bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
317
318 GrTexture* texture() const { return fTexture.get(); }
319 GrShaderFlags visibility() const { return fVisibility; }
320 GrIOType ioType() const { return fTexture.ioType(); }
321 GrImageStorageFormat format() const { return fFormat; }
322 GrSLMemoryModel memoryModel() const { return fMemoryModel; }
323 GrSLRestrict restrict() const { return fRestrict; }
324
325 /**
326 * For internal use by GrProcessor.
327 */
328 const GrGpuResourceRef* programTexture() const { return &fTexture; }
329
330private:
331 GrTGpuResourceRef<GrTexture> fTexture;
332 GrShaderFlags fVisibility;
333 GrImageStorageFormat fFormat;
334 GrSLMemoryModel fMemoryModel;
335 GrSLRestrict fRestrict;
336 typedef SkNoncopyable INHERITED;
337};
338
tomhudson@google.com168e6342012-04-18 17:49:20 +0000339#endif