blob: 3ab108a0c23bed03c10e313b3ff6cd5fcb6cf17d [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
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 GrGeometryProcessor_DEFINED
9#define GrGeometryProcessor_DEFINED
10
Robert Phillips787fd9d2021-03-22 14:48:09 -040011#include "src/gpu/GrColor.h"
12#include "src/gpu/GrNonAtomicRef.h"
13#include "src/gpu/GrProcessor.h"
14#include "src/gpu/GrShaderVar.h"
15#include "src/gpu/GrSwizzle.h"
16
Robert Phillipsf10535f2021-03-23 09:30:45 -040017class GrGLSLGeometryProcessor;
Robert Phillips787fd9d2021-03-22 14:48:09 -040018class GrGLSLUniformHandler;
19
joshualitt9b989322014-12-15 14:16:27 -080020/**
Robert Phillips787fd9d2021-03-22 14:48:09 -040021 * The GrGeometryProcessor represents some kind of geometric primitive. This includes the shape
22 * of the primitive and the inherent color of the primitive. The GrGeometryProcessor is
23 * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through
24 * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
25 * from the GrGeometryProcessor, so the GrGeometryProcessor must be able to support this
26 * functionality.
27 *
28 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
29 * GrGeometryProcessor. These loops run on the CPU and to determine known properties of the final
30 * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve
31 * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its
32 * getProcessorAnalysisInputs implementation. These seed values are processed by the
33 * subsequent stages of the rendering pipeline and the output is then fed back into the GrDrawOp
34 * in the applyPipelineOptimizations call, where the op can use the information to inform
35 * decisions about GrGeometryProcessor creation.
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050036 *
37 * Note that all derived classes should hide their constructors and provide a Make factory
Robert Phillips787fd9d2021-03-22 14:48:09 -040038 * function that takes an arena (except for Tesselation-specific classes). This is because
39 * geometry processors can be created in either the record-time or flush-time arenas which
40 * define their lifetimes (i.e., a DDLs life time in the first case and a single flush in
41 * the second case).
42 *
43 * TODO: This class does not really need to be ref counted. Instances should be allocated using
44 * GrOpFlushState's arena and destroyed when the arena is torn down.
joshualitt9b989322014-12-15 14:16:27 -080045 */
Robert Phillips787fd9d2021-03-22 14:48:09 -040046class GrGeometryProcessor : public GrProcessor, public GrNonAtomicRef<GrGeometryProcessor> {
joshualitt9b989322014-12-15 14:16:27 -080047public:
Robert Phillips787fd9d2021-03-22 14:48:09 -040048 class TextureSampler;
49
50 /** Describes a vertex or instance attribute. */
51 class Attribute {
52 public:
53 constexpr Attribute() = default;
54 constexpr Attribute(const char* name,
55 GrVertexAttribType cpuType,
56 GrSLType gpuType)
57 : fName(name), fCPUType(cpuType), fGPUType(gpuType) {
58 SkASSERT(name && gpuType != kVoid_GrSLType);
59 }
60 constexpr Attribute(const Attribute&) = default;
61
62 Attribute& operator=(const Attribute&) = default;
63
64 constexpr bool isInitialized() const { return fGPUType != kVoid_GrSLType; }
65
66 constexpr const char* name() const { return fName; }
67 constexpr GrVertexAttribType cpuType() const { return fCPUType; }
68 constexpr GrSLType gpuType() const { return fGPUType; }
69
70 inline constexpr size_t size() const;
71 constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); }
72
73 GrShaderVar asShaderVar() const {
74 return {fName, fGPUType, GrShaderVar::TypeModifier::In};
75 }
76
77 private:
78 const char* fName = nullptr;
79 GrVertexAttribType fCPUType = kFloat_GrVertexAttribType;
80 GrSLType fGPUType = kVoid_GrSLType;
81 };
82
83 class Iter {
84 public:
85 Iter() : fCurr(nullptr), fRemaining(0) {}
86 Iter(const Iter& iter) : fCurr(iter.fCurr), fRemaining(iter.fRemaining) {}
87 Iter& operator= (const Iter& iter) {
88 fCurr = iter.fCurr;
89 fRemaining = iter.fRemaining;
90 return *this;
91 }
92 Iter(const Attribute* attrs, int count) : fCurr(attrs), fRemaining(count) {
93 this->skipUninitialized();
94 }
95
96 bool operator!=(const Iter& that) const { return fCurr != that.fCurr; }
97 const Attribute& operator*() const { return *fCurr; }
98 void operator++() {
99 if (fRemaining) {
100 fRemaining--;
101 fCurr++;
102 this->skipUninitialized();
103 }
104 }
105
106 private:
107 void skipUninitialized() {
108 if (!fRemaining) {
109 fCurr = nullptr;
110 } else {
111 while (!fCurr->isInitialized()) {
112 ++fCurr;
113 }
114 }
115 }
116
117 const Attribute* fCurr;
118 int fRemaining;
119 };
120
121 class AttributeSet {
122 public:
123 Iter begin() const { return Iter(fAttributes, fCount); }
124 Iter end() const { return Iter(); }
125
126 int count() const { return fCount; }
127 size_t stride() const { return fStride; }
128
129 private:
130 friend class GrGeometryProcessor;
131
132 void init(const Attribute* attrs, int count) {
133 fAttributes = attrs;
134 fRawCount = count;
135 fCount = 0;
136 fStride = 0;
137 for (int i = 0; i < count; ++i) {
138 if (attrs[i].isInitialized()) {
139 fCount++;
140 fStride += attrs[i].sizeAlign4();
141 }
142 }
143 }
144
145 const Attribute* fAttributes = nullptr;
146 int fRawCount = 0;
147 int fCount = 0;
148 size_t fStride = 0;
149 };
150
151 GrGeometryProcessor(ClassID);
152
153 int numTextureSamplers() const { return fTextureSamplerCnt; }
154 const TextureSampler& textureSampler(int index) const;
155 int numVertexAttributes() const { return fVertexAttributes.fCount; }
156 const AttributeSet& vertexAttributes() const { return fVertexAttributes; }
157 int numInstanceAttributes() const { return fInstanceAttributes.fCount; }
158 const AttributeSet& instanceAttributes() const { return fInstanceAttributes; }
159
160 bool hasVertexAttributes() const { return SkToBool(fVertexAttributes.fCount); }
161 bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributes.fCount); }
162
163 /**
164 * A common practice is to populate the the vertex/instance's memory using an implicit array of
165 * structs. In this case, it is best to assert that:
166 * stride == sizeof(struct)
167 */
168 size_t vertexStride() const { return fVertexAttributes.fStride; }
169 size_t instanceStride() const { return fInstanceAttributes.fStride; }
170
171 bool willUseTessellationShaders() const {
172 return fShaders & (kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag);
173 }
174
175 bool willUseGeoShader() const {
176 return fShaders & kGeometry_GrShaderFlag;
177 }
178
179 /**
180 * Computes a key for the transforms owned by an FP based on the shader code that will be
181 * emitted by the primitive processor to implement them.
182 */
183 static uint32_t ComputeCoordTransformsKey(const GrFragmentProcessor& fp);
184
185 static constexpr int kCoordTransformKeyBits = 4;
186
187 /**
188 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
189 * processor's GL backend implementation.
190 *
191 * TODO: A better name for this function would be "compute" instead of "get".
192 */
193 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
194
195
196 void getAttributeKey(GrProcessorKeyBuilder* b) const {
197 // Ensure that our CPU and GPU type fields fit together in a 32-bit value, and we never
198 // collide with the "uninitialized" value.
199 static_assert(kGrVertexAttribTypeCount < (1 << 8), "");
200 static_assert(kGrSLTypeCount < (1 << 8), "");
201
202 auto add_attributes = [=](const Attribute* attrs, int attrCount) {
203 for (int i = 0; i < attrCount; ++i) {
204 const Attribute& attr = attrs[i];
205 b->appendComment(attr.isInitialized() ? attr.name() : "unusedAttr");
206 b->addBits(8, attr.isInitialized() ? attr.cpuType() : 0xff, "attrType");
207 b->addBits(8, attr.isInitialized() ? attr.gpuType() : 0xff, "attrGpuType");
208 }
209 };
210 b->add32(fVertexAttributes.fRawCount, "numVertexAttributes");
211 add_attributes(fVertexAttributes.fAttributes, fVertexAttributes.fRawCount);
212 b->add32(fInstanceAttributes.fRawCount, "numInstanceAttributes");
213 add_attributes(fInstanceAttributes.fAttributes, fInstanceAttributes.fRawCount);
214 }
215
216 /** Returns a new instance of the appropriate *GL* implementation class
217 for the given GrProcessor; caller is responsible for deleting
218 the object. */
Robert Phillipsf10535f2021-03-23 09:30:45 -0400219 virtual GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
Robert Phillips787fd9d2021-03-22 14:48:09 -0400220
221 // We use these methods as a temporary back door to inject OpenGL tessellation code. Once
222 // tessellation is supported by SkSL we can remove these.
Robert Phillipsf10535f2021-03-23 09:30:45 -0400223 virtual SkString getTessControlShaderGLSL(const GrGLSLGeometryProcessor*,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400224 const char* versionAndExtensionDecls,
225 const GrGLSLUniformHandler&,
226 const GrShaderCaps&) const {
227 SK_ABORT("Not implemented.");
228 }
Robert Phillipsf10535f2021-03-23 09:30:45 -0400229 virtual SkString getTessEvaluationShaderGLSL(const GrGLSLGeometryProcessor*,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400230 const char* versionAndExtensionDecls,
231 const GrGLSLUniformHandler&,
232 const GrShaderCaps&) const {
233 SK_ABORT("Not implemented.");
234 }
joshualitt74077b92014-10-24 11:26:03 -0700235
joshualittb0a8a372014-09-23 09:50:21 -0700236protected:
Brian Osman2715bf52019-12-06 14:38:47 -0500237 // GPs that need to use either float or ubyte colors can just call this to get a correctly
Brian Osmanc906d252018-12-04 11:17:46 -0500238 // configured Attribute struct
239 static Attribute MakeColorAttribute(const char* name, bool wideColor) {
240 return { name,
Brian Osman2715bf52019-12-06 14:38:47 -0500241 wideColor ? kFloat4_GrVertexAttribType : kUByte4_norm_GrVertexAttribType,
Brian Osmanc906d252018-12-04 11:17:46 -0500242 kHalf4_GrSLType };
243 }
244
Robert Phillips787fd9d2021-03-22 14:48:09 -0400245 void setVertexAttributes(const Attribute* attrs, int attrCount) {
246 fVertexAttributes.init(attrs, attrCount);
247 }
248 void setInstanceAttributes(const Attribute* attrs, int attrCount) {
249 SkASSERT(attrCount >= 0);
250 fInstanceAttributes.init(attrs, attrCount);
251 }
252 void setWillUseTessellationShaders() {
253 fShaders |= kTessControl_GrShaderFlag | kTessEvaluation_GrShaderFlag;
254 }
255 void setWillUseGeoShader() { fShaders |= kGeometry_GrShaderFlag; }
256 void setTextureSamplerCnt(int cnt) {
257 SkASSERT(cnt >= 0);
258 fTextureSamplerCnt = cnt;
259 }
260
261 /**
262 * Helper for implementing onTextureSampler(). E.g.:
263 * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler);
264 */
265 template <typename... Args>
266 static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0,
267 const Args&... samps) {
268 return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...);
269 }
270 inline static const TextureSampler& IthTextureSampler(int i);
271
joshualittb0a8a372014-09-23 09:50:21 -0700272private:
Robert Phillips787fd9d2021-03-22 14:48:09 -0400273 virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); }
274
275 GrShaderFlags fShaders = kVertex_GrShaderFlag | kFragment_GrShaderFlag;
276
277 AttributeSet fVertexAttributes;
278 AttributeSet fInstanceAttributes;
279
280 int fTextureSamplerCnt = 0;
281 using INHERITED = GrProcessor;
joshualittb0a8a372014-09-23 09:50:21 -0700282};
joshualitt56995b52014-12-11 15:44:02 -0800283
Robert Phillips787fd9d2021-03-22 14:48:09 -0400284//////////////////////////////////////////////////////////////////////////////
285
286/**
287 * Used to capture the properties of the GrTextureProxies required/expected by a primitiveProcessor
288 * along with an associated GrSamplerState. The actual proxies used are stored in either the
289 * fixed or dynamic state arrays. TextureSamplers don't perform any coord manipulation to account
290 * for texture origin.
291 */
292class GrGeometryProcessor::TextureSampler {
293public:
294 TextureSampler() = default;
295
296 TextureSampler(GrSamplerState, const GrBackendFormat&, const GrSwizzle&);
297
298 TextureSampler(const TextureSampler&) = delete;
299 TextureSampler& operator=(const TextureSampler&) = delete;
300
301 void reset(GrSamplerState, const GrBackendFormat&, const GrSwizzle&);
302
303 const GrBackendFormat& backendFormat() const { return fBackendFormat; }
304 GrTextureType textureType() const { return fBackendFormat.textureType(); }
305
306 GrSamplerState samplerState() const { return fSamplerState; }
307 const GrSwizzle& swizzle() const { return fSwizzle; }
308
309 bool isInitialized() const { return fIsInitialized; }
310
311private:
312 GrSamplerState fSamplerState;
313 GrBackendFormat fBackendFormat;
314 GrSwizzle fSwizzle;
315 bool fIsInitialized = false;
316};
317
318const GrGeometryProcessor::TextureSampler& GrGeometryProcessor::IthTextureSampler(int i) {
319 SK_ABORT("Illegal texture sampler index");
320 static const TextureSampler kBogus;
321 return kBogus;
322}
323
324//////////////////////////////////////////////////////////////////////////////
325
326/**
327 * Returns the size of the attrib type in bytes.
328 * This was moved from include/private/GrTypesPriv.h in service of Skia dependents that build
329 * with C++11.
330 */
331static constexpr inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
332 switch (type) {
333 case kFloat_GrVertexAttribType:
334 return sizeof(float);
335 case kFloat2_GrVertexAttribType:
336 return 2 * sizeof(float);
337 case kFloat3_GrVertexAttribType:
338 return 3 * sizeof(float);
339 case kFloat4_GrVertexAttribType:
340 return 4 * sizeof(float);
341 case kHalf_GrVertexAttribType:
342 return sizeof(uint16_t);
343 case kHalf2_GrVertexAttribType:
344 return 2 * sizeof(uint16_t);
345 case kHalf4_GrVertexAttribType:
346 return 4 * sizeof(uint16_t);
347 case kInt2_GrVertexAttribType:
348 return 2 * sizeof(int32_t);
349 case kInt3_GrVertexAttribType:
350 return 3 * sizeof(int32_t);
351 case kInt4_GrVertexAttribType:
352 return 4 * sizeof(int32_t);
353 case kByte_GrVertexAttribType:
354 return 1 * sizeof(char);
355 case kByte2_GrVertexAttribType:
356 return 2 * sizeof(char);
357 case kByte4_GrVertexAttribType:
358 return 4 * sizeof(char);
359 case kUByte_GrVertexAttribType:
360 return 1 * sizeof(char);
361 case kUByte2_GrVertexAttribType:
362 return 2 * sizeof(char);
363 case kUByte4_GrVertexAttribType:
364 return 4 * sizeof(char);
365 case kUByte_norm_GrVertexAttribType:
366 return 1 * sizeof(char);
367 case kUByte4_norm_GrVertexAttribType:
368 return 4 * sizeof(char);
369 case kShort2_GrVertexAttribType:
370 return 2 * sizeof(int16_t);
371 case kShort4_GrVertexAttribType:
372 return 4 * sizeof(int16_t);
373 case kUShort2_GrVertexAttribType: // fall through
374 case kUShort2_norm_GrVertexAttribType:
375 return 2 * sizeof(uint16_t);
376 case kInt_GrVertexAttribType:
377 return sizeof(int32_t);
378 case kUint_GrVertexAttribType:
379 return sizeof(uint32_t);
380 case kUShort_norm_GrVertexAttribType:
381 return sizeof(uint16_t);
382 case kUShort4_norm_GrVertexAttribType:
383 return 4 * sizeof(uint16_t);
384 }
385 // GCC fails because SK_ABORT evaluates to non constexpr. clang and cl.exe think this is
386 // unreachable and don't complain.
387#if defined(__clang__) || !defined(__GNUC__)
388 SK_ABORT("Unsupported type conversion");
389#endif
390 return 0;
391}
392
393constexpr size_t GrGeometryProcessor::Attribute::size() const {
394 return GrVertexAttribTypeSize(fCPUType);
395}
396
joshualittb0a8a372014-09-23 09:50:21 -0700397#endif