blob: 71456885f001cb053072a47254eaf8fdea7d31f2 [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"
joshualitt8072caa2015-02-12 14:20:52 -080015
Brian Salomonf7dcd762018-07-30 14:48:15 -040016class GrCoordTransform;
17
joshualitt8072caa2015-02-12 14:20:52 -080018/*
19 * The GrPrimitiveProcessor represents some kind of geometric primitive. This includes the shape
20 * of the primitive and the inherent color of the primitive. The GrPrimitiveProcessor is
21 * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through
22 * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
23 * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this
Brian Salomon09d994e2016-12-21 11:14:46 -050024 * functionality.
joshualitt8072caa2015-02-12 14:20:52 -080025 *
26 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
Brian Salomoncb30bb22017-02-12 09:28:54 -050027 * GrPrimitiveProcessor. These loops run on the CPU and to determine known properties of the final
28 * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve
29 * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its
Brian Salomona811b122017-03-30 08:21:32 -040030 * getProcessorAnalysisInputs implementation. These seed values are processed by the
Brian Salomon5298dc82017-02-22 11:52:03 -050031 * subsequent
Brian Salomon92aee3d2016-12-21 09:20:25 -050032 * stages of the rendering pipeline and the output is then fed back into the GrDrawOp in
33 * the applyPipelineOptimizations call, where the op can use the information to inform decisions
34 * about GrPrimitiveProcessor creation.
joshualitt8072caa2015-02-12 14:20:52 -080035 */
36
egdaniele659a582015-11-13 09:55:43 -080037class GrGLSLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080038
Brian Salomon7eae3e02018-08-07 14:02:38 +000039/**
joshualitt8072caa2015-02-12 14:20:52 -080040 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
41 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
42 * pipelines, and they must provide some notion of equality
Brian Salomon7eae3e02018-08-07 14:02:38 +000043 *
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.
joshualitt8072caa2015-02-12 14:20:52 -080046 */
Brian Salomon7eae3e02018-08-07 14:02:38 +000047class GrPrimitiveProcessor : public GrProcessor, public GrNonAtomicRef<GrPrimitiveProcessor> {
joshualitt8072caa2015-02-12 14:20:52 -080048public:
Brian Salomone782f842018-07-31 13:53:11 -040049 class TextureSampler;
50
Brian Salomon92be2f72018-06-19 14:33:47 -040051 /** Describes a vertex or instance attribute. */
Brian Salomon70132d02018-05-29 15:33:06 -040052 class Attribute {
53 public:
Brian Salomon70132d02018-05-29 15:33:06 -040054 constexpr Attribute() = default;
Brian Osmand4c29702018-09-14 16:16:55 -040055 constexpr Attribute(const char* name,
56 GrVertexAttribType cpuType,
57 GrSLType gpuType)
58 : fName(name), fCPUType(cpuType), fGPUType(gpuType) {}
Brian Salomon92be2f72018-06-19 14:33:47 -040059 constexpr Attribute(const Attribute&) = default;
Brian Salomon70132d02018-05-29 15:33:06 -040060
Brian Salomon92be2f72018-06-19 14:33:47 -040061 Attribute& operator=(const Attribute&) = default;
Brian Salomon70132d02018-05-29 15:33:06 -040062
Brian Salomon92be2f72018-06-19 14:33:47 -040063 constexpr bool isInitialized() const { return SkToBool(fName); }
64
65 constexpr const char* name() const { return fName; }
Brian Osmand4c29702018-09-14 16:16:55 -040066 constexpr GrVertexAttribType cpuType() const { return fCPUType; }
67 constexpr GrSLType gpuType() const { return fGPUType; }
Brian Salomon92be2f72018-06-19 14:33:47 -040068
69 inline constexpr size_t size() const;
70 constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); }
Brian Salomon70132d02018-05-29 15:33:06 -040071
72 GrShaderVar asShaderVar() const {
Brian Osmand4c29702018-09-14 16:16:55 -040073 return {fName, fGPUType, GrShaderVar::kIn_TypeModifier};
Brian Salomon70132d02018-05-29 15:33:06 -040074 }
75
76 private:
77 const char* fName = nullptr;
Brian Osmand4c29702018-09-14 16:16:55 -040078 GrVertexAttribType fCPUType = kFloat_GrVertexAttribType;
79 GrSLType fGPUType = kFloat_GrSLType;
joshualitt8072caa2015-02-12 14:20:52 -080080 };
81
Brian Osmanf04fb3c2018-11-12 15:34:00 -050082 class Iter {
83 public:
84 Iter() : fCurr(nullptr), fRemaining(0) {}
85 Iter(const Iter& iter) : fCurr(iter.fCurr), fRemaining(iter.fRemaining) {}
86 Iter& operator= (const Iter& iter) {
87 fCurr = iter.fCurr;
88 fRemaining = iter.fRemaining;
89 return *this;
90 }
91 Iter(const Attribute* attrs, int count) : fCurr(attrs), fRemaining(count) {
92 this->skipUninitialized();
93 }
94
95 bool operator!=(const Iter& that) const { return fCurr != that.fCurr; }
96 const Attribute& operator*() const { return *fCurr; }
97 void operator++() {
98 if (fRemaining) {
99 fRemaining--;
100 fCurr++;
101 this->skipUninitialized();
102 }
103 }
104
105 private:
106 void skipUninitialized() {
107 if (!fRemaining) {
108 fCurr = nullptr;
109 } else {
110 while (!fCurr->isInitialized()) {
111 ++fCurr;
112 }
113 }
114 }
115
116 const Attribute* fCurr;
117 int fRemaining;
118 };
119
120 class AttributeSet {
121 public:
122 Iter begin() const { return Iter(fAttributes, fCount); }
123 Iter end() const { return Iter(); }
124
125 private:
126 friend class GrPrimitiveProcessor;
127
128 void init(const Attribute* attrs, int count) {
129 fAttributes = attrs;
Brian Osmand3e71302018-12-06 11:17:35 -0500130 fRawCount = count;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500131 fCount = 0;
132 fStride = 0;
133 for (int i = 0; i < count; ++i) {
134 if (attrs[i].isInitialized()) {
135 fCount++;
136 fStride += attrs[i].sizeAlign4();
137 }
138 }
139 }
140
141 const Attribute* fAttributes = nullptr;
Brian Osmand3e71302018-12-06 11:17:35 -0500142 int fRawCount = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500143 int fCount = 0;
144 size_t fStride = 0;
145 };
146
Brian Salomon92be2f72018-06-19 14:33:47 -0400147 GrPrimitiveProcessor(ClassID);
Ethan Nicholasabff9562017-10-09 10:54:08 -0400148
Brian Salomone782f842018-07-31 13:53:11 -0400149 int numTextureSamplers() const { return fTextureSamplerCnt; }
150 const TextureSampler& textureSampler(int index) const;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500151 int numVertexAttributes() const { return fVertexAttributes.fCount; }
152 const AttributeSet& vertexAttributes() const { return fVertexAttributes; }
153 int numInstanceAttributes() const { return fInstanceAttributes.fCount; }
154 const AttributeSet& instanceAttributes() const { return fInstanceAttributes; }
joshualitt8072caa2015-02-12 14:20:52 -0800155
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500156 bool hasVertexAttributes() const { return SkToBool(fVertexAttributes.fCount); }
157 bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributes.fCount); }
Chris Dalton1d616352017-05-31 12:51:23 -0600158
159 /**
Brian Salomon92be2f72018-06-19 14:33:47 -0400160 * A common practice is to populate the the vertex/instance's memory using an implicit array of
161 * structs. In this case, it is best to assert that:
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500162 * stride == sizeof(struct)
Chris Dalton1d616352017-05-31 12:51:23 -0600163 */
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500164 size_t vertexStride() const { return fVertexAttributes.fStride; }
165 size_t instanceStride() const { return fInstanceAttributes.fStride; }
Chris Dalton1d616352017-05-31 12:51:23 -0600166
167 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
168 // we put these calls on the base class to prevent having to cast
169 virtual bool willUseGeoShader() const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800170
171 /**
wangyixa7f4c432015-08-20 07:25:02 -0700172 * Computes a transformKey from an array of coord transforms. Will only look at the first
173 * <numCoords> transforms in the array.
174 *
175 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800176 */
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400177 uint32_t getTransformKey(const SkTArray<GrCoordTransform*, true>& coords,
wangyixa7f4c432015-08-20 07:25:02 -0700178 int numCoords) const;
joshualitt8072caa2015-02-12 14:20:52 -0800179
180 /**
181 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
182 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -0700183 *
184 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800185 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500186 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800187
188
Brian Osmand3e71302018-12-06 11:17:35 -0500189 void getAttributeKey(GrProcessorKeyBuilder* b) const {
190 // Ensure that our CPU and GPU type fields fit together in a 32-bit value, and we never
191 // collide with the "uninitialized" value.
192 static_assert(kGrVertexAttribTypeCount < (1 << 8), "");
193 static_assert(kGrSLTypeCount < (1 << 8), "");
194
195 auto add_attributes = [=](const Attribute* attrs, int attrCount) {
196 for (int i = 0; i < attrCount; ++i) {
197 b->add32(attrs[i].isInitialized() ? (attrs[i].cpuType() << 16) | attrs[i].gpuType()
198 : ~0);
199 }
200 };
201 add_attributes(fVertexAttributes.fAttributes, fVertexAttributes.fRawCount);
202 add_attributes(fInstanceAttributes.fAttributes, fInstanceAttributes.fRawCount);
203 }
204
joshualitt8072caa2015-02-12 14:20:52 -0800205 /** Returns a new instance of the appropriate *GL* implementation class
206 for the given GrProcessor; caller is responsible for deleting
207 the object. */
Brian Salomon94efbf52016-11-29 13:43:05 -0500208 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800209
ethannicholas22793252016-01-30 09:59:10 -0800210 virtual bool isPathRendering() const { return false; }
joshualitt8072caa2015-02-12 14:20:52 -0800211
212protected:
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500213 void setVertexAttributes(const Attribute* attrs, int attrCount) {
214 fVertexAttributes.init(attrs, attrCount);
Brian Salomone782f842018-07-31 13:53:11 -0400215 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500216 void setInstanceAttributes(const Attribute* attrs, int attrCount) {
217 SkASSERT(attrCount >= 0);
218 fInstanceAttributes.init(attrs, attrCount);
Brian Salomone782f842018-07-31 13:53:11 -0400219 }
220 void setTextureSamplerCnt(int cnt) {
221 SkASSERT(cnt >= 0);
222 fTextureSamplerCnt = cnt;
223 }
224
225 /**
226 * Helper for implementing onTextureSampler(). E.g.:
227 * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler);
228 */
229 template <typename... Args>
230 static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0,
231 const Args&... samps) {
232 return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...);
233 }
234 inline static const TextureSampler& IthTextureSampler(int i);
joshualitt8072caa2015-02-12 14:20:52 -0800235
236private:
Brian Salomone782f842018-07-31 13:53:11 -0400237 virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); }
Chris Dalton1d616352017-05-31 12:51:23 -0600238
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500239 AttributeSet fVertexAttributes;
240 AttributeSet fInstanceAttributes;
241
Brian Salomone782f842018-07-31 13:53:11 -0400242 int fTextureSamplerCnt = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800243 typedef GrProcessor INHERITED;
244};
245
Brian Salomon92be2f72018-06-19 14:33:47 -0400246//////////////////////////////////////////////////////////////////////////////
247
248/**
Brian Salomone782f842018-07-31 13:53:11 -0400249 * Used to represent a texture that is required by a GrPrimitiveProcessor. It holds a GrTextureProxy
250 * along with an associated GrSamplerState. TextureSamplers don't perform any coord manipulation to
251 * account for texture origin.
252 */
253class GrPrimitiveProcessor::TextureSampler {
254public:
255 TextureSampler() = default;
256
Brian Salomon67529b22019-08-13 15:31:04 -0400257 TextureSampler(GrTextureType, const GrSamplerState&, const GrSwizzle&,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400258 uint32_t extraSamplerKey);
Brian Salomone782f842018-07-31 13:53:11 -0400259
Brian Salomon67529b22019-08-13 15:31:04 -0400260 explicit TextureSampler(GrTextureType, GrSamplerState::Filter,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400261 GrSamplerState::WrapMode wrapXAndY, const GrSwizzle&);
Brian Salomone782f842018-07-31 13:53:11 -0400262
263 TextureSampler(const TextureSampler&) = delete;
264 TextureSampler& operator=(const TextureSampler&) = delete;
265
Brian Salomon67529b22019-08-13 15:31:04 -0400266 void reset(GrTextureType, const GrSamplerState&, const GrSwizzle&,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400267 uint32_t extraSamplerKey = 0);
Brian Salomon67529b22019-08-13 15:31:04 -0400268 void reset(GrTextureType,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500269 GrSamplerState::Filter,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400270 GrSamplerState::WrapMode wrapXAndY,
271 const GrSwizzle& swizzle);
Brian Salomone782f842018-07-31 13:53:11 -0400272
Brian Salomon7eae3e02018-08-07 14:02:38 +0000273 GrTextureType textureType() const { return fTextureType; }
Brian Salomone782f842018-07-31 13:53:11 -0400274
Brian Salomone782f842018-07-31 13:53:11 -0400275 const GrSamplerState& samplerState() const { return fSamplerState; }
Greg Daniel2c3398d2019-06-19 11:58:01 -0400276 const GrSwizzle& swizzle() const { return fSwizzle; }
Brian Salomone782f842018-07-31 13:53:11 -0400277
Greg Daniel7a82edf2018-12-04 10:54:34 -0500278 uint32_t extraSamplerKey() const { return fExtraSamplerKey; }
279
Brian Salomon67529b22019-08-13 15:31:04 -0400280 bool isInitialized() const { return fIsInitialized; }
Brian Salomone782f842018-07-31 13:53:11 -0400281
282private:
Brian Salomone782f842018-07-31 13:53:11 -0400283 GrSamplerState fSamplerState;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400284 GrSwizzle fSwizzle;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000285 GrTextureType fTextureType = GrTextureType::k2D;
Greg Daniel7a82edf2018-12-04 10:54:34 -0500286 uint32_t fExtraSamplerKey = 0;
Brian Salomon67529b22019-08-13 15:31:04 -0400287 bool fIsInitialized = false;
Brian Salomone782f842018-07-31 13:53:11 -0400288};
289
290const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::IthTextureSampler(int i) {
291 SK_ABORT("Illegal texture sampler index");
292 static const TextureSampler kBogus;
293 return kBogus;
294}
295
296//////////////////////////////////////////////////////////////////////////////
297
298/**
Brian Salomon92be2f72018-06-19 14:33:47 -0400299 * Returns the size of the attrib type in bytes.
300 * This was moved from include/private/GrTypesPriv.h in service of Skia dependents that build
301 * with C++11.
302 */
303static constexpr inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
304 switch (type) {
305 case kFloat_GrVertexAttribType:
306 return sizeof(float);
307 case kFloat2_GrVertexAttribType:
308 return 2 * sizeof(float);
309 case kFloat3_GrVertexAttribType:
310 return 3 * sizeof(float);
311 case kFloat4_GrVertexAttribType:
312 return 4 * sizeof(float);
313 case kHalf_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400314 return sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400315 case kHalf2_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400316 return 2 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400317 case kHalf3_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400318 return 3 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400319 case kHalf4_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400320 return 4 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400321 case kInt2_GrVertexAttribType:
322 return 2 * sizeof(int32_t);
323 case kInt3_GrVertexAttribType:
324 return 3 * sizeof(int32_t);
325 case kInt4_GrVertexAttribType:
326 return 4 * sizeof(int32_t);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400327 case kByte_GrVertexAttribType:
328 return 1 * sizeof(char);
329 case kByte2_GrVertexAttribType:
330 return 2 * sizeof(char);
331 case kByte3_GrVertexAttribType:
332 return 3 * sizeof(char);
333 case kByte4_GrVertexAttribType:
334 return 4 * sizeof(char);
335 case kUByte_GrVertexAttribType:
336 return 1 * sizeof(char);
337 case kUByte2_GrVertexAttribType:
338 return 2 * sizeof(char);
339 case kUByte3_GrVertexAttribType:
340 return 3 * sizeof(char);
341 case kUByte4_GrVertexAttribType:
342 return 4 * sizeof(char);
Brian Salomon92be2f72018-06-19 14:33:47 -0400343 case kUByte_norm_GrVertexAttribType:
344 return 1 * sizeof(char);
345 case kUByte4_norm_GrVertexAttribType:
346 return 4 * sizeof(char);
347 case kShort2_GrVertexAttribType:
348 return 2 * sizeof(int16_t);
Brian Osmana5c578f2018-09-19 14:19:02 -0400349 case kShort4_GrVertexAttribType:
350 return 4 * sizeof(int16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400351 case kUShort2_GrVertexAttribType: // fall through
352 case kUShort2_norm_GrVertexAttribType:
353 return 2 * sizeof(uint16_t);
354 case kInt_GrVertexAttribType:
355 return sizeof(int32_t);
356 case kUint_GrVertexAttribType:
357 return sizeof(uint32_t);
Robert Phillipsfe18de52019-06-06 17:21:50 -0400358 case kUShort_norm_GrVertexAttribType:
359 return sizeof(uint16_t);
Robert Phillips66a46032019-06-18 08:00:42 -0400360 // Experimental (for Y416)
361 case kUShort4_norm_GrVertexAttribType:
362 return 4 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400363 }
364 // GCC fails because SK_ABORT evaluates to non constexpr. clang and cl.exe think this is
365 // unreachable and don't complain.
366#if defined(__clang__) || !defined(__GNUC__)
367 SK_ABORT("Unsupported type conversion");
368#endif
369 return 0;
370}
371
372constexpr size_t GrPrimitiveProcessor::Attribute::size() const {
Brian Osmand4c29702018-09-14 16:16:55 -0400373 return GrVertexAttribTypeSize(fCPUType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400374}
375
joshualitt8072caa2015-02-12 14:20:52 -0800376#endif