1 /*
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 "src/gpu/GrColor.h"
12 #include "src/gpu/GrNonAtomicRef.h"
13 #include "src/gpu/GrProcessor.h"
14 #include "src/gpu/GrShaderVar.h"
15 
16 class GrCoordTransform;
17 
18 /*
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
24  * functionality.
25  *
26  * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
27  * 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
30  * getProcessorAnalysisInputs implementation. These seed values are processed by the
31  * subsequent
32  * 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.
35  */
36 
37 class GrGLSLPrimitiveProcessor;
38 
39 /**
40  * 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
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.
46  */
47 class GrPrimitiveProcessor : public GrProcessor, public GrNonAtomicRef<GrPrimitiveProcessor> {
48 public:
49     class TextureSampler;
50 
51     /** Describes a vertex or instance attribute. */
52     class Attribute {
53     public:
54         constexpr Attribute() = default;
Attribute(const char * name,GrVertexAttribType cpuType,GrSLType gpuType)55         constexpr Attribute(const char* name,
56                             GrVertexAttribType cpuType,
57                             GrSLType gpuType)
58             : fName(name), fCPUType(cpuType), fGPUType(gpuType) {}
59         constexpr Attribute(const Attribute&) = default;
60 
61         Attribute& operator=(const Attribute&) = default;
62 
isInitialized()63         constexpr bool isInitialized() const { return SkToBool(fName); }
64 
name()65         constexpr const char* name() const { return fName; }
cpuType()66         constexpr GrVertexAttribType cpuType() const { return fCPUType; }
gpuType()67         constexpr GrSLType           gpuType() const { return fGPUType; }
68 
69         inline constexpr size_t size() const;
sizeAlign4()70         constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); }
71 
asShaderVar()72         GrShaderVar asShaderVar() const {
73             return {fName, fGPUType, GrShaderVar::kIn_TypeModifier};
74         }
75 
76     private:
77         const char* fName = nullptr;
78         GrVertexAttribType fCPUType = kFloat_GrVertexAttribType;
79         GrSLType fGPUType = kFloat_GrSLType;
80     };
81 
82     class Iter {
83     public:
Iter()84         Iter() : fCurr(nullptr), fRemaining(0) {}
Iter(const Iter & iter)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         }
Iter(const Attribute * attrs,int count)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:
skipUninitialized()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:
begin()122         Iter begin() const { return Iter(fAttributes, fCount); }
end()123         Iter end() const { return Iter(); }
124 
125     private:
126         friend class GrPrimitiveProcessor;
127 
init(const Attribute * attrs,int count)128         void init(const Attribute* attrs, int count) {
129             fAttributes = attrs;
130             fRawCount = count;
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              fRawCount = 0;
143         int              fCount = 0;
144         size_t           fStride = 0;
145     };
146 
147     GrPrimitiveProcessor(ClassID);
148 
numTextureSamplers()149     int numTextureSamplers() const { return fTextureSamplerCnt; }
150     const TextureSampler& textureSampler(int index) const;
numVertexAttributes()151     int numVertexAttributes() const { return fVertexAttributes.fCount; }
vertexAttributes()152     const AttributeSet& vertexAttributes() const { return fVertexAttributes; }
numInstanceAttributes()153     int numInstanceAttributes() const { return fInstanceAttributes.fCount; }
instanceAttributes()154     const AttributeSet& instanceAttributes() const { return fInstanceAttributes; }
155 
hasVertexAttributes()156     bool hasVertexAttributes() const { return SkToBool(fVertexAttributes.fCount); }
hasInstanceAttributes()157     bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributes.fCount); }
158 
159     /**
160      * 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:
162      *     stride == sizeof(struct)
163      */
vertexStride()164     size_t vertexStride() const { return fVertexAttributes.fStride; }
instanceStride()165     size_t instanceStride() const { return fInstanceAttributes.fStride; }
166 
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;
170 
171     /**
172      * 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".
176      */
177     uint32_t getTransformKey(const SkTArray<GrCoordTransform*, true>& coords,
178                              int numCoords) const;
179 
180     /**
181      * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
182      * processor's GL backend implementation.
183      *
184      * TODO: A better name for this function  would be "compute" instead of "get".
185      */
186     virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
187 
188 
getAttributeKey(GrProcessorKeyBuilder * b)189     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 
205     /** Returns a new instance of the appropriate *GL* implementation class
206         for the given GrProcessor; caller is responsible for deleting
207         the object. */
208     virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
209 
isPathRendering()210     virtual bool isPathRendering() const { return false; }
211 
212 protected:
setVertexAttributes(const Attribute * attrs,int attrCount)213     void setVertexAttributes(const Attribute* attrs, int attrCount) {
214         fVertexAttributes.init(attrs, attrCount);
215     }
setInstanceAttributes(const Attribute * attrs,int attrCount)216     void setInstanceAttributes(const Attribute* attrs, int attrCount) {
217         SkASSERT(attrCount >= 0);
218         fInstanceAttributes.init(attrs, attrCount);
219     }
setTextureSamplerCnt(int cnt)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>
IthTextureSampler(int i,const TextureSampler & samp0,const Args &...samps)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);
235 
236 private:
onTextureSampler(int)237     virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); }
238 
239     AttributeSet fVertexAttributes;
240     AttributeSet fInstanceAttributes;
241 
242     int fTextureSamplerCnt = 0;
243     typedef GrProcessor INHERITED;
244 };
245 
246 //////////////////////////////////////////////////////////////////////////////
247 
248 /**
249  * Used to capture the properties of the GrTextureProxies required/expected by a primitiveProcessor
250  * along with an associated GrSamplerState. The actual proxies used are stored in either the
251  * fixed or dynamic state arrays. TextureSamplers don't perform any coord manipulation to account
252  * for texture origin.
253  */
254 class GrPrimitiveProcessor::TextureSampler {
255 public:
256     TextureSampler() = default;
257 
258     TextureSampler(GrTextureType, const GrSamplerState&, const GrSwizzle&,
259                    uint32_t extraSamplerKey = 0);
260 
261     TextureSampler(const TextureSampler&) = delete;
262     TextureSampler& operator=(const TextureSampler&) = delete;
263 
264     void reset(GrTextureType, const GrSamplerState&, const GrSwizzle&,
265                uint32_t extraSamplerKey = 0);
266 
textureType()267     GrTextureType textureType() const { return fTextureType; }
268 
samplerState()269     const GrSamplerState& samplerState() const { return fSamplerState; }
swizzle()270     const GrSwizzle& swizzle() const { return fSwizzle; }
271 
extraSamplerKey()272     uint32_t extraSamplerKey() const { return fExtraSamplerKey; }
273 
isInitialized()274     bool isInitialized() const { return fIsInitialized; }
275 
276 private:
277     GrSamplerState fSamplerState;
278     GrSwizzle fSwizzle;
279     GrTextureType fTextureType = GrTextureType::k2D;
280     uint32_t fExtraSamplerKey = 0;
281     bool fIsInitialized = false;
282 };
283 
IthTextureSampler(int i)284 const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::IthTextureSampler(int i) {
285     SK_ABORT("Illegal texture sampler index");
286     static const TextureSampler kBogus;
287     return kBogus;
288 }
289 
290 //////////////////////////////////////////////////////////////////////////////
291 
292 /**
293  * Returns the size of the attrib type in bytes.
294  * This was moved from include/private/GrTypesPriv.h in service of Skia dependents that build
295  * with C++11.
296  */
GrVertexAttribTypeSize(GrVertexAttribType type)297 static constexpr inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
298     switch (type) {
299         case kFloat_GrVertexAttribType:
300             return sizeof(float);
301         case kFloat2_GrVertexAttribType:
302             return 2 * sizeof(float);
303         case kFloat3_GrVertexAttribType:
304             return 3 * sizeof(float);
305         case kFloat4_GrVertexAttribType:
306             return 4 * sizeof(float);
307         case kHalf_GrVertexAttribType:
308             return sizeof(uint16_t);
309         case kHalf2_GrVertexAttribType:
310             return 2 * sizeof(uint16_t);
311         case kHalf3_GrVertexAttribType:
312             return 3 * sizeof(uint16_t);
313         case kHalf4_GrVertexAttribType:
314             return 4 * sizeof(uint16_t);
315         case kInt2_GrVertexAttribType:
316             return 2 * sizeof(int32_t);
317         case kInt3_GrVertexAttribType:
318             return 3 * sizeof(int32_t);
319         case kInt4_GrVertexAttribType:
320             return 4 * sizeof(int32_t);
321         case kByte_GrVertexAttribType:
322             return 1 * sizeof(char);
323         case kByte2_GrVertexAttribType:
324             return 2 * sizeof(char);
325         case kByte3_GrVertexAttribType:
326             return 3 * sizeof(char);
327         case kByte4_GrVertexAttribType:
328             return 4 * sizeof(char);
329         case kUByte_GrVertexAttribType:
330             return 1 * sizeof(char);
331         case kUByte2_GrVertexAttribType:
332             return 2 * sizeof(char);
333         case kUByte3_GrVertexAttribType:
334             return 3 * sizeof(char);
335         case kUByte4_GrVertexAttribType:
336             return 4 * sizeof(char);
337         case kUByte_norm_GrVertexAttribType:
338             return 1 * sizeof(char);
339         case kUByte4_norm_GrVertexAttribType:
340             return 4 * sizeof(char);
341         case kShort2_GrVertexAttribType:
342             return 2 * sizeof(int16_t);
343         case kShort4_GrVertexAttribType:
344             return 4 * sizeof(int16_t);
345         case kUShort2_GrVertexAttribType: // fall through
346         case kUShort2_norm_GrVertexAttribType:
347             return 2 * sizeof(uint16_t);
348         case kInt_GrVertexAttribType:
349             return sizeof(int32_t);
350         case kUint_GrVertexAttribType:
351             return sizeof(uint32_t);
352         case kUShort_norm_GrVertexAttribType:
353             return sizeof(uint16_t);
354         case kUShort4_norm_GrVertexAttribType:
355             return 4 * sizeof(uint16_t);
356     }
357     // GCC fails because SK_ABORT evaluates to non constexpr. clang and cl.exe think this is
358     // unreachable and don't complain.
359 #if defined(__clang__) || !defined(__GNUC__)
360     SK_ABORT("Unsupported type conversion");
361 #endif
362     return 0;
363 }
364 
size()365 constexpr size_t GrPrimitiveProcessor::Attribute::size() const {
366     return GrVertexAttribTypeSize(fCPUType);
367 }
368 
369 #endif
370