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