blob: be3d86ab5c4cd0c36e0808c9eb1791086a34f3d6 [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
11#include "GrColor.h"
Brian Salomon7eae3e02018-08-07 14:02:38 +000012#include "GrNonAtomicRef.h"
joshualitt8072caa2015-02-12 14:20:52 -080013#include "GrProcessor.h"
Brian Salomonee783962018-08-01 09:55:10 -040014#include "GrProxyRef.h"
joshualitt8072caa2015-02-12 14:20:52 -080015#include "GrShaderVar.h"
16
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)
59 : fName(name), fCPUType(cpuType), fGPUType(gpuType) {}
Brian Salomon92be2f72018-06-19 14:33:47 -040060 constexpr Attribute(const Attribute&) = default;
Brian Salomon70132d02018-05-29 15:33:06 -040061
Brian Salomon92be2f72018-06-19 14:33:47 -040062 Attribute& operator=(const Attribute&) = default;
Brian Salomon70132d02018-05-29 15:33:06 -040063
Brian Salomon92be2f72018-06-19 14:33:47 -040064 constexpr bool isInitialized() const { return SkToBool(fName); }
65
66 constexpr const char* name() const { return fName; }
Brian Osmand4c29702018-09-14 16:16:55 -040067 constexpr GrVertexAttribType cpuType() const { return fCPUType; }
68 constexpr GrSLType gpuType() const { return fGPUType; }
Brian Salomon92be2f72018-06-19 14:33:47 -040069
70 inline constexpr size_t size() const;
71 constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); }
Brian Salomon70132d02018-05-29 15:33:06 -040072
73 GrShaderVar asShaderVar() const {
Brian Osmand4c29702018-09-14 16:16:55 -040074 return {fName, fGPUType, GrShaderVar::kIn_TypeModifier};
Brian Salomon70132d02018-05-29 15:33:06 -040075 }
76
77 private:
78 const char* fName = nullptr;
Brian Osmand4c29702018-09-14 16:16:55 -040079 GrVertexAttribType fCPUType = kFloat_GrVertexAttribType;
80 GrSLType fGPUType = kFloat_GrSLType;
joshualitt8072caa2015-02-12 14:20:52 -080081 };
82
Brian Osmanf04fb3c2018-11-12 15:34:00 -050083 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 private:
127 friend class GrPrimitiveProcessor;
128
129 void init(const Attribute* attrs, int count) {
130 fAttributes = attrs;
131 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;
142 int fCount = 0;
143 size_t fStride = 0;
144 };
145
Brian Salomon92be2f72018-06-19 14:33:47 -0400146 GrPrimitiveProcessor(ClassID);
Ethan Nicholasabff9562017-10-09 10:54:08 -0400147
Brian Salomone782f842018-07-31 13:53:11 -0400148 int numTextureSamplers() const { return fTextureSamplerCnt; }
149 const TextureSampler& textureSampler(int index) const;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500150 int numVertexAttributes() const { return fVertexAttributes.fCount; }
151 const AttributeSet& vertexAttributes() const { return fVertexAttributes; }
152 int numInstanceAttributes() const { return fInstanceAttributes.fCount; }
153 const AttributeSet& instanceAttributes() const { return fInstanceAttributes; }
joshualitt8072caa2015-02-12 14:20:52 -0800154
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500155 bool hasVertexAttributes() const { return SkToBool(fVertexAttributes.fCount); }
156 bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributes.fCount); }
Chris Dalton1d616352017-05-31 12:51:23 -0600157
158 /**
Brian Salomon92be2f72018-06-19 14:33:47 -0400159 * A common practice is to populate the the vertex/instance's memory using an implicit array of
160 * structs. In this case, it is best to assert that:
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500161 * stride == sizeof(struct)
Chris Dalton1d616352017-05-31 12:51:23 -0600162 */
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500163 size_t vertexStride() const { return fVertexAttributes.fStride; }
164 size_t instanceStride() const { return fInstanceAttributes.fStride; }
Chris Dalton1d616352017-05-31 12:51:23 -0600165
166 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
167 // we put these calls on the base class to prevent having to cast
168 virtual bool willUseGeoShader() const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800169
170 /**
wangyixa7f4c432015-08-20 07:25:02 -0700171 * Computes a transformKey from an array of coord transforms. Will only look at the first
172 * <numCoords> transforms in the array.
173 *
174 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800175 */
wangyixa7f4c432015-08-20 07:25:02 -0700176 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
177 int numCoords) const;
joshualitt8072caa2015-02-12 14:20:52 -0800178
179 /**
180 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
181 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -0700182 *
183 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800184 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500185 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800186
187
188 /** Returns a new instance of the appropriate *GL* implementation class
189 for the given GrProcessor; caller is responsible for deleting
190 the object. */
Brian Salomon94efbf52016-11-29 13:43:05 -0500191 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800192
ethannicholas22793252016-01-30 09:59:10 -0800193 virtual bool isPathRendering() const { return false; }
joshualitt8072caa2015-02-12 14:20:52 -0800194
ethannicholas22793252016-01-30 09:59:10 -0800195 /**
196 * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor().
197 */
198 virtual const char* getDestColorOverride() const { return nullptr; }
halcanary9d524f22016-03-29 09:03:52 -0700199
Brian Salomon92be2f72018-06-19 14:33:47 -0400200 virtual float getSampleShading() const { return 0.0; }
ethannicholas28ef4452016-03-25 09:26:03 -0700201
joshualitt8072caa2015-02-12 14:20:52 -0800202protected:
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500203 void setVertexAttributes(const Attribute* attrs, int attrCount) {
204 fVertexAttributes.init(attrs, attrCount);
Brian Salomone782f842018-07-31 13:53:11 -0400205 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500206 void setInstanceAttributes(const Attribute* attrs, int attrCount) {
207 SkASSERT(attrCount >= 0);
208 fInstanceAttributes.init(attrs, attrCount);
Brian Salomone782f842018-07-31 13:53:11 -0400209 }
210 void setTextureSamplerCnt(int cnt) {
211 SkASSERT(cnt >= 0);
212 fTextureSamplerCnt = cnt;
213 }
214
215 /**
216 * Helper for implementing onTextureSampler(). E.g.:
217 * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler);
218 */
219 template <typename... Args>
220 static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0,
221 const Args&... samps) {
222 return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...);
223 }
224 inline static const TextureSampler& IthTextureSampler(int i);
joshualitt8072caa2015-02-12 14:20:52 -0800225
226private:
Brian Salomone782f842018-07-31 13:53:11 -0400227 virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); }
Chris Dalton1d616352017-05-31 12:51:23 -0600228
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500229 AttributeSet fVertexAttributes;
230 AttributeSet fInstanceAttributes;
231
Brian Salomone782f842018-07-31 13:53:11 -0400232 int fTextureSamplerCnt = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800233 typedef GrProcessor INHERITED;
234};
235
Brian Salomon92be2f72018-06-19 14:33:47 -0400236//////////////////////////////////////////////////////////////////////////////
237
238/**
Brian Salomone782f842018-07-31 13:53:11 -0400239 * Used to represent a texture that is required by a GrPrimitiveProcessor. It holds a GrTextureProxy
240 * along with an associated GrSamplerState. TextureSamplers don't perform any coord manipulation to
241 * account for texture origin.
242 */
243class GrPrimitiveProcessor::TextureSampler {
244public:
245 TextureSampler() = default;
246
Greg Daniel7a82edf2018-12-04 10:54:34 -0500247 TextureSampler(GrTextureType, GrPixelConfig, const GrSamplerState&, uint32_t extraSamplerKey);
Brian Salomone782f842018-07-31 13:53:11 -0400248
Brian Salomon7eae3e02018-08-07 14:02:38 +0000249 explicit TextureSampler(GrTextureType, GrPixelConfig,
Brian Salomone782f842018-07-31 13:53:11 -0400250 GrSamplerState::Filter = GrSamplerState::Filter::kNearest,
Greg Daniel0f70be82018-10-08 17:35:08 +0000251 GrSamplerState::WrapMode wrapXAndY = GrSamplerState::WrapMode::kClamp);
Brian Salomone782f842018-07-31 13:53:11 -0400252
253 TextureSampler(const TextureSampler&) = delete;
254 TextureSampler& operator=(const TextureSampler&) = delete;
255
Greg Daniel7a82edf2018-12-04 10:54:34 -0500256 void reset(GrTextureType, GrPixelConfig, const GrSamplerState&, uint32_t extraSamplerKey = 0);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000257 void reset(GrTextureType, GrPixelConfig,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500258 GrSamplerState::Filter,
259 GrSamplerState::WrapMode wrapXAndY);
Brian Salomone782f842018-07-31 13:53:11 -0400260
Brian Salomon7eae3e02018-08-07 14:02:38 +0000261 GrTextureType textureType() const { return fTextureType; }
262 GrPixelConfig config() const { return fConfig; }
Brian Salomone782f842018-07-31 13:53:11 -0400263
Brian Salomone782f842018-07-31 13:53:11 -0400264 const GrSamplerState& samplerState() const { return fSamplerState; }
265
Greg Daniel7a82edf2018-12-04 10:54:34 -0500266 uint32_t extraSamplerKey() const { return fExtraSamplerKey; }
267
Brian Salomon7eae3e02018-08-07 14:02:38 +0000268 bool isInitialized() const { return fConfig != kUnknown_GrPixelConfig; }
Brian Salomone782f842018-07-31 13:53:11 -0400269
270private:
Brian Salomone782f842018-07-31 13:53:11 -0400271 GrSamplerState fSamplerState;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000272 GrTextureType fTextureType = GrTextureType::k2D;
273 GrPixelConfig fConfig = kUnknown_GrPixelConfig;
Greg Daniel7a82edf2018-12-04 10:54:34 -0500274 uint32_t fExtraSamplerKey = 0;
Brian Salomone782f842018-07-31 13:53:11 -0400275};
276
277const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::IthTextureSampler(int i) {
278 SK_ABORT("Illegal texture sampler index");
279 static const TextureSampler kBogus;
280 return kBogus;
281}
282
283//////////////////////////////////////////////////////////////////////////////
284
285/**
Brian Salomon92be2f72018-06-19 14:33:47 -0400286 * Returns the size of the attrib type in bytes.
287 * This was moved from include/private/GrTypesPriv.h in service of Skia dependents that build
288 * with C++11.
289 */
290static constexpr inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
291 switch (type) {
292 case kFloat_GrVertexAttribType:
293 return sizeof(float);
294 case kFloat2_GrVertexAttribType:
295 return 2 * sizeof(float);
296 case kFloat3_GrVertexAttribType:
297 return 3 * sizeof(float);
298 case kFloat4_GrVertexAttribType:
299 return 4 * sizeof(float);
300 case kHalf_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400301 return sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400302 case kHalf2_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400303 return 2 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400304 case kHalf3_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400305 return 3 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400306 case kHalf4_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -0400307 return 4 * sizeof(uint16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400308 case kInt2_GrVertexAttribType:
309 return 2 * sizeof(int32_t);
310 case kInt3_GrVertexAttribType:
311 return 3 * sizeof(int32_t);
312 case kInt4_GrVertexAttribType:
313 return 4 * sizeof(int32_t);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400314 case kByte_GrVertexAttribType:
315 return 1 * sizeof(char);
316 case kByte2_GrVertexAttribType:
317 return 2 * sizeof(char);
318 case kByte3_GrVertexAttribType:
319 return 3 * sizeof(char);
320 case kByte4_GrVertexAttribType:
321 return 4 * sizeof(char);
322 case kUByte_GrVertexAttribType:
323 return 1 * sizeof(char);
324 case kUByte2_GrVertexAttribType:
325 return 2 * sizeof(char);
326 case kUByte3_GrVertexAttribType:
327 return 3 * sizeof(char);
328 case kUByte4_GrVertexAttribType:
329 return 4 * sizeof(char);
Brian Salomon92be2f72018-06-19 14:33:47 -0400330 case kUByte_norm_GrVertexAttribType:
331 return 1 * sizeof(char);
332 case kUByte4_norm_GrVertexAttribType:
333 return 4 * sizeof(char);
334 case kShort2_GrVertexAttribType:
335 return 2 * sizeof(int16_t);
Brian Osmana5c578f2018-09-19 14:19:02 -0400336 case kShort4_GrVertexAttribType:
337 return 4 * sizeof(int16_t);
Brian Salomon92be2f72018-06-19 14:33:47 -0400338 case kUShort2_GrVertexAttribType: // fall through
339 case kUShort2_norm_GrVertexAttribType:
340 return 2 * sizeof(uint16_t);
341 case kInt_GrVertexAttribType:
342 return sizeof(int32_t);
343 case kUint_GrVertexAttribType:
344 return sizeof(uint32_t);
345 }
346 // GCC fails because SK_ABORT evaluates to non constexpr. clang and cl.exe think this is
347 // unreachable and don't complain.
348#if defined(__clang__) || !defined(__GNUC__)
349 SK_ABORT("Unsupported type conversion");
350#endif
351 return 0;
352}
353
354constexpr size_t GrPrimitiveProcessor::Attribute::size() const {
Brian Osmand4c29702018-09-14 16:16:55 -0400355 return GrVertexAttribTypeSize(fCPUType);
Brian Salomon92be2f72018-06-19 14:33:47 -0400356}
357
joshualitt8072caa2015-02-12 14:20:52 -0800358#endif