blob: 75af35aabce81da3425bc0e9f01308fd73c5d382 [file] [log] [blame]
joshualitt8072caa2015-02-12 14:20:52 -08001/*
2 * Copyright 2013 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
8#ifndef GrPrimitiveProcessor_DEFINED
9#define GrPrimitiveProcessor_DEFINED
10
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrNonAtomicRef.h"
13#include "src/gpu/GrProcessor.h"
14#include "src/gpu/GrShaderVar.h"
Brian Salomondf1bd6d2020-03-26 20:37:01 -040015#include "src/gpu/GrSwizzle.h"
joshualitt8072caa2015-02-12 14:20:52 -080016
Brian Salomonf7dcd762018-07-30 14:48:15 -040017class GrCoordTransform;
18
joshualitt8072caa2015-02-12 14:20:52 -080019/*
20 * The GrPrimitiveProcessor represents some kind of geometric primitive. This includes the shape
21 * of the primitive and the inherent color of the primitive. The GrPrimitiveProcessor is
22 * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through
23 * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
24 * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this
Brian Salomon09d994e2016-12-21 11:14:46 -050025 * functionality.
joshualitt8072caa2015-02-12 14:20:52 -080026 *
27 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
Brian Salomoncb30bb22017-02-12 09:28:54 -050028 * GrPrimitiveProcessor. These loops run on the CPU and to determine known properties of the final
29 * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve
30 * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its
Brian Salomona811b122017-03-30 08:21:32 -040031 * getProcessorAnalysisInputs implementation. These seed values are processed by the
Brian Salomon5298dc82017-02-22 11:52:03 -050032 * subsequent
Brian Salomon92aee3d2016-12-21 09:20:25 -050033 * stages of the rendering pipeline and the output is then fed back into the GrDrawOp in
34 * the applyPipelineOptimizations call, where the op can use the information to inform decisions
35 * about GrPrimitiveProcessor creation.
joshualitt8072caa2015-02-12 14:20:52 -080036 */
37
egdaniele659a582015-11-13 09:55:43 -080038class GrGLSLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080039
Brian Salomon7eae3e02018-08-07 14:02:38 +000040/**
joshualitt8072caa2015-02-12 14:20:52 -080041 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
42 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
43 * pipelines, and they must provide some notion of equality
Brian Salomon7eae3e02018-08-07 14:02:38 +000044 *
45 * TODO: This class does not really need to be ref counted. Instances should be allocated using
46 * GrOpFlushState's arena and destroyed when the arena is torn down.
joshualitt8072caa2015-02-12 14:20:52 -080047 */
Brian Salomon7eae3e02018-08-07 14:02:38 +000048class GrPrimitiveProcessor : public GrProcessor, public GrNonAtomicRef<GrPrimitiveProcessor> {
joshualitt8072caa2015-02-12 14:20:52 -080049public:
Brian Salomone782f842018-07-31 13:53:11 -040050 class TextureSampler;
51
Brian Salomon92be2f72018-06-19 14:33:47 -040052 /** Describes a vertex or instance attribute. */
Brian Salomon70132d02018-05-29 15:33:06 -040053 class Attribute {
54 public:
Brian Salomon70132d02018-05-29 15:33:06 -040055 constexpr Attribute() = default;
Brian Osmand4c29702018-09-14 16:16:55 -040056 constexpr Attribute(const char* name,
57 GrVertexAttribType cpuType,
58 GrSLType gpuType)
Michael Ludwig553db622020-06-19 10:47:30 -040059 : fName(name), fCPUType(cpuType), fGPUType(gpuType) {
60 SkASSERT(name && gpuType != kVoid_GrSLType);
61 }
Brian Salomon92be2f72018-06-19 14:33:47 -040062 constexpr Attribute(const Attribute&) = default;
Brian Salomon70132d02018-05-29 15:33:06 -040063
Brian Salomon92be2f72018-06-19 14:33:47 -040064 Attribute& operator=(const Attribute&) = default;
Brian Salomon70132d02018-05-29 15:33:06 -040065
Michael Ludwig553db622020-06-19 10:47:30 -040066 constexpr bool isInitialized() const { return fGPUType != kVoid_GrSLType; }
Brian Salomon92be2f72018-06-19 14:33:47 -040067
68 constexpr const char* name() const { return fName; }
Brian Osmand4c29702018-09-14 16:16:55 -040069 constexpr GrVertexAttribType cpuType() const { return fCPUType; }
70 constexpr GrSLType gpuType() const { return fGPUType; }
Brian Salomon92be2f72018-06-19 14:33:47 -040071
72 inline constexpr size_t size() const;
73 constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); }
Brian Salomon70132d02018-05-29 15:33:06 -040074
75 GrShaderVar asShaderVar() const {
Ben Wagnerdabd98c2020-03-25 15:17:18 -040076 return {fName, fGPUType, GrShaderVar::TypeModifier::In};
Brian Salomon70132d02018-05-29 15:33:06 -040077 }
78
79 private:
80 const char* fName = nullptr;
Brian Osmand4c29702018-09-14 16:16:55 -040081 GrVertexAttribType fCPUType = kFloat_GrVertexAttribType;
Michael Ludwig553db622020-06-19 10:47:30 -040082 GrSLType fGPUType = kVoid_GrSLType;
joshualitt8072caa2015-02-12 14:20:52 -080083 };
84
Brian Osmanf04fb3c2018-11-12 15:34:00 -050085 class Iter {
86 public:
87 Iter() : fCurr(nullptr), fRemaining(0) {}
88 Iter(const Iter& iter) : fCurr(iter.fCurr), fRemaining(iter.fRemaining) {}
89 Iter& operator= (const Iter& iter) {
90 fCurr = iter.fCurr;
91 fRemaining = iter.fRemaining;
92 return *this;
93 }
94 Iter(const Attribute* attrs, int count) : fCurr(attrs), fRemaining(count) {
95 this->skipUninitialized();
96 }
97
98 bool operator!=(const Iter& that) const { return fCurr != that.fCurr; }
99 const Attribute& operator*() const { return *fCurr; }
100 void operator++() {
101 if (fRemaining) {
102 fRemaining--;
103 fCurr++;
104 this->skipUninitialized();
105 }
106 }
107
108 private:
109 void skipUninitialized() {
110 if (!fRemaining) {
111 fCurr = nullptr;
112 } else {
113 while (!fCurr->isInitialized()) {
114 ++fCurr;
115 }
116 }
117 }
118
119 const Attribute* fCurr;
120 int fRemaining;
121 };
122
123 class AttributeSet {
124 public:
125 Iter begin() const { return Iter(fAttributes, fCount); }
126 Iter end() const { return Iter(); }
127
128 private:
129 friend class GrPrimitiveProcessor;
130
131 void init(const Attribute* attrs, int count) {
132 fAttributes = attrs;
Brian Osmand3e71302018-12-06 11:17:35 -0500133 fRawCount = count;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500134 fCount = 0;
135 fStride = 0;
136 for (int i = 0; i < count; ++i) {
137 if (attrs[i].isInitialized()) {
138 fCount++;
139 fStride += attrs[i].sizeAlign4();
140 }
141 }
142 }
143
144 const Attribute* fAttributes = nullptr;
Brian Osmand3e71302018-12-06 11:17:35 -0500145 int fRawCount = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500146 int fCount = 0;
147 size_t fStride = 0;
148 };
149
Brian Salomon92be2f72018-06-19 14:33:47 -0400150 GrPrimitiveProcessor(ClassID);
Ethan Nicholasabff9562017-10-09 10:54:08 -0400151
Brian Salomone782f842018-07-31 13:53:11 -0400152 int numTextureSamplers() const { return fTextureSamplerCnt; }
153 const TextureSampler& textureSampler(int index) const;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500154 int numVertexAttributes() const { return fVertexAttributes.fCount; }
155 const AttributeSet& vertexAttributes() const { return fVertexAttributes; }
156 int numInstanceAttributes() const { return fInstanceAttributes.fCount; }
157 const AttributeSet& instanceAttributes() const { return fInstanceAttributes; }
joshualitt8072caa2015-02-12 14:20:52 -0800158
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500159 bool hasVertexAttributes() const { return SkToBool(fVertexAttributes.fCount); }
160 bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributes.fCount); }
Chris Dalton1d616352017-05-31 12:51:23 -0600161
162 /**
Brian Salomon92be2f72018-06-19 14:33:47 -0400163 * A common practice is to populate the the vertex/instance's memory using an implicit array of
164 * structs. In this case, it is best to assert that:
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500165 * stride == sizeof(struct)
Chris Dalton1d616352017-05-31 12:51:23 -0600166 */
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500167 size_t vertexStride() const { return fVertexAttributes.fStride; }
168 size_t instanceStride() const { return fInstanceAttributes.fStride; }
Chris Dalton1d616352017-05-31 12:51:23 -0600169
Chris Dalton5a2f9622019-12-27 14:56:38 -0700170 bool willUseTessellationShaders() const {
171 return fShaders & (kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag);
172 }
173
174 bool willUseGeoShader() const {
175 return fShaders & kGeometry_GrShaderFlag;
176 }
joshualitt8072caa2015-02-12 14:20:52 -0800177
178 /**
Brian Salomon7eabfe82019-12-02 14:20:20 -0500179 * Computes a key for the transforms owned by an FP based on the shader code that will be
180 * emitted by the primitive processor to implement them.
joshualitt8072caa2015-02-12 14:20:52 -0800181 */
Brian Salomon7eabfe82019-12-02 14:20:20 -0500182 uint32_t computeCoordTransformsKey(const GrFragmentProcessor& fp) const;
joshualitt8072caa2015-02-12 14:20:52 -0800183
184 /**
185 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
186 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -0700187 *
188 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800189 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500190 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800191
192
Brian Osmand3e71302018-12-06 11:17:35 -0500193 void getAttributeKey(GrProcessorKeyBuilder* b) const {
194 // Ensure that our CPU and GPU type fields fit together in a 32-bit value, and we never
195 // collide with the "uninitialized" value.
196 static_assert(kGrVertexAttribTypeCount < (1 << 8), "");
197 static_assert(kGrSLTypeCount < (1 << 8), "");
198
199 auto add_attributes = [=](const Attribute* attrs, int attrCount) {
200 for (int i = 0; i < attrCount; ++i) {
201 b->add32(attrs[i].isInitialized() ? (attrs[i].cpuType() << 16) | attrs[i].gpuType()
202 : ~0);
203 }
204 };
205 add_attributes(fVertexAttributes.fAttributes, fVertexAttributes.fRawCount);
206 add_attributes(fInstanceAttributes.fAttributes, fInstanceAttributes.fRawCount);
207 }
208
joshualitt8072caa2015-02-12 14:20:52 -0800209 /** Returns a new instance of the appropriate *GL* implementation class
210 for the given GrProcessor; caller is responsible for deleting
211 the object. */
Brian Salomon94efbf52016-11-29 13:43:05 -0500212 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800213
ethannicholas22793252016-01-30 09:59:10 -0800214 virtual bool isPathRendering() const { return false; }
joshualitt8072caa2015-02-12 14:20:52 -0800215
Chris Dalton5a2f9622019-12-27 14:56:38 -0700216 // We use these methods as a temporary back door to inject OpenGL tessellation code. Once
217 // tessellation is supported by SkSL we can remove these.
218 virtual SkString getTessControlShaderGLSL(const char* versionAndExtensionDecls,
219 const GrShaderCaps&) const {
220 SK_ABORT("Not implemented.");
221 }
222 virtual SkString getTessEvaluationShaderGLSL(const char* versionAndExtensionDecls,
223 const GrShaderCaps&) const {
224 SK_ABORT("Not implemented.");
225 }
226
joshualitt8072caa2015-02-12 14:20:52 -0800227protected:
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500228 void setVertexAttributes(const Attribute* attrs, int attrCount) {
229 fVertexAttributes.init(attrs, attrCount);
Brian Salomone782f842018-07-31 13:53:11 -0400230 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500231 void setInstanceAttributes(const Attribute* attrs, int attrCount) {
232 SkASSERT(attrCount >= 0);
233 fInstanceAttributes.init(attrs, attrCount);
Brian Salomone782f842018-07-31 13:53:11 -0400234 }
Chris Dalton5a2f9622019-12-27 14:56:38 -0700235 void setWillUseTessellationShaders() {
236 fShaders |= kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag;
237 }
238 void setWillUseGeoShader() { fShaders |= kGeometry_GrShaderFlag; }
Brian Salomone782f842018-07-31 13:53:11 -0400239 void setTextureSamplerCnt(int cnt) {
240 SkASSERT(cnt >= 0);
241 fTextureSamplerCnt = cnt;
242 }
243
244 /**
245 * Helper for implementing onTextureSampler(). E.g.:
246 * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler);
247 */
248 template <typename... Args>
249 static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0,
250 const Args&... samps) {
251 return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...);
252 }
253 inline static const TextureSampler& IthTextureSampler(int i);
joshualitt8072caa2015-02-12 14:20:52 -0800254
255private:
Brian Salomone782f842018-07-31 13:53:11 -0400256 virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); }
Chris Dalton1d616352017-05-31 12:51:23 -0600257
Chris Dalton5a2f9622019-12-27 14:56:38 -0700258 GrShaderFlags fShaders = kVertex_GrShaderFlag | kFragment_GrShaderFlag;
259
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500260 AttributeSet fVertexAttributes;
261 AttributeSet fInstanceAttributes;
262
Brian Salomone782f842018-07-31 13:53:11 -0400263 int fTextureSamplerCnt = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800264 typedef GrProcessor INHERITED;
265};
266
Brian Salomon92be2f72018-06-19 14:33:47 -0400267//////////////////////////////////////////////////////////////////////////////
268
269/**
Robert Phillips7d7aaf42019-10-14 11:34:16 -0400270 * Used to capture the properties of the GrTextureProxies required/expected by a primitiveProcessor
271 * along with an associated GrSamplerState. The actual proxies used are stored in either the
272 * fixed or dynamic state arrays. TextureSamplers don't perform any coord manipulation to account
273 * for texture origin.
Brian Salomone782f842018-07-31 13:53:11 -0400274 */
275class GrPrimitiveProcessor::TextureSampler {
276public:
277 TextureSampler() = default;
278
Brian Salomonccb61422020-01-09 10:46:36 -0500279 TextureSampler(GrSamplerState, const GrBackendFormat&, const GrSwizzle&);
Brian Salomone782f842018-07-31 13:53:11 -0400280
281 TextureSampler(const TextureSampler&) = delete;
282 TextureSampler& operator=(const TextureSampler&) = delete;
283
Brian Salomonccb61422020-01-09 10:46:36 -0500284 void reset(GrSamplerState, const GrBackendFormat&, const GrSwizzle&);
Brian Salomone782f842018-07-31 13:53:11 -0400285
Robert Phillipsf272bea2019-10-17 08:56:16 -0400286 const GrBackendFormat& backendFormat() const { return fBackendFormat; }
287 GrTextureType textureType() const { return fBackendFormat.textureType(); }
Brian Salomone782f842018-07-31 13:53:11 -0400288
Brian Salomonccb61422020-01-09 10:46:36 -0500289 GrSamplerState samplerState() const { return fSamplerState; }
Greg Daniel2c3398d2019-06-19 11:58:01 -0400290 const GrSwizzle& swizzle() const { return fSwizzle; }
Brian Salomone782f842018-07-31 13:53:11 -0400291
Brian Salomon67529b22019-08-13 15:31:04 -0400292 bool isInitialized() const { return fIsInitialized; }
Brian Salomone782f842018-07-31 13:53:11 -0400293
294private:
Robert Phillipsf272bea2019-10-17 08:56:16 -0400295 GrSamplerState fSamplerState;
296 GrBackendFormat fBackendFormat;
297 GrSwizzle fSwizzle;
Robert Phillipsf272bea2019-10-17 08:56:16 -0400298 bool fIsInitialized = false;
Brian Salomone782f842018-07-31 13:53:11 -0400299};
300
301const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::IthTextureSampler(int i) {
302 SK_ABORT("Illegal texture sampler index");
303 static const TextureSampler kBogus;
304 return kBogus;
305}
306
307//////////////////////////////////////////////////////////////////////////////
308
309/**
Brian Salomon92be2f72018-06-19 14:33:47 -0400310 * Returns the size of the attrib type in bytes.
311 * This was moved from include/private/GrTypesPriv.h in service of Skia dependents that build
312 * with C++11.
313 */
314static constexpr inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
315 switch (type) {
316 case kFloat_GrVertexAttribType:
317 return sizeof(float);
318 case kFloat2_GrVertexAttribType:
319 return 2 * sizeof(float);
320 case kFloat3_GrVertexAttribType:
321 return 3 * sizeof(float);
322 case kFloat4_GrVertexAttribType:
323 return 4 * sizeof(float);
324 case kHalf_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400325 return sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400326 case kHalf2_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400327 return 2 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400328 case kHalf4_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400329 return 4 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400330 case kInt2_GrVertexAttribType:
331 return 2 * sizeof(int32_t);
332 case kInt3_GrVertexAttribType:
333 return 3 * sizeof(int32_t);
334 case kInt4_GrVertexAttribType:
335 return 4 * sizeof(int32_t);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400336 case kByte_GrVertexAttribType:
337 return 1 * sizeof(char);
338 case kByte2_GrVertexAttribType:
339 return 2 * sizeof(char);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400340 case kByte4_GrVertexAttribType:
341 return 4 * sizeof(char);
342 case kUByte_GrVertexAttribType:
343 return 1 * sizeof(char);
344 case kUByte2_GrVertexAttribType:
345 return 2 * sizeof(char);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400346 case kUByte4_GrVertexAttribType:
347 return 4 * sizeof(char);
Brian Salomon92be2f72018-06-19 14:33:47 -0400348 case kUByte_norm_GrVertexAttribType:
349 return 1 * sizeof(char);
350 case kUByte4_norm_GrVertexAttribType:
351 return 4 * sizeof(char);
352 case kShort2_GrVertexAttribType:
353 return 2 * sizeof(int16_t);
Brian Osmana5c578f2018-09-19 14:19:02 -0400354 case kShort4_GrVertexAttribType:
355 return 4 * sizeof(int16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400356 case kUShort2_GrVertexAttribType: // fall through
357 case kUShort2_norm_GrVertexAttribType:
358 return 2 * sizeof(uint16_t);
359 case kInt_GrVertexAttribType:
360 return sizeof(int32_t);
361 case kUint_GrVertexAttribType:
362 return sizeof(uint32_t);
Robert Phillipsfe18de52019-06-06 17:21:50 -0400363 case kUShort_norm_GrVertexAttribType:
364 return sizeof(uint16_t);
Robert Phillips66a46032019-06-18 08:00:42 -0400365 case kUShort4_norm_GrVertexAttribType:
366 return 4 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400367 }
368 // GCC fails because SK_ABORT evaluates to non constexpr. clang and cl.exe think this is
369 // unreachable and don't complain.
370#if defined(__clang__) || !defined(__GNUC__)
371 SK_ABORT("Unsupported type conversion");
372#endif
373 return 0;
374}
375
376constexpr size_t GrPrimitiveProcessor::Attribute::size() const {
Brian Osmand4c29702018-09-14 16:16:55 -0400377 return GrVertexAttribTypeSize(fCPUType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400378}
379
joshualitt8072caa2015-02-12 14:20:52 -0800380#endif