• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<const 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 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  */
253 class GrPrimitiveProcessor::TextureSampler {
254 public:
255     TextureSampler() = default;
256 
257     TextureSampler(GrTextureType, const GrSamplerState&, const GrSwizzle&,
258                    uint32_t extraSamplerKey);
259 
260     explicit TextureSampler(GrTextureType, GrSamplerState::Filter,
261                             GrSamplerState::WrapMode wrapXAndY, const GrSwizzle&);
262 
263     TextureSampler(const TextureSampler&) = delete;
264     TextureSampler& operator=(const TextureSampler&) = delete;
265 
266     void reset(GrTextureType, const GrSamplerState&, const GrSwizzle&,
267                uint32_t extraSamplerKey = 0);
268     void reset(GrTextureType,
269                GrSamplerState::Filter,
270                GrSamplerState::WrapMode wrapXAndY,
271                const GrSwizzle& swizzle);
272 
textureType()273     GrTextureType textureType() const { return fTextureType; }
274 
samplerState()275     const GrSamplerState& samplerState() const { return fSamplerState; }
swizzle()276     const GrSwizzle& swizzle() const { return fSwizzle; }
277 
extraSamplerKey()278     uint32_t extraSamplerKey() const { return fExtraSamplerKey; }
279 
isInitialized()280     bool isInitialized() const { return fIsInitialized; }
281 
282 private:
283     GrSamplerState fSamplerState;
284     GrSwizzle fSwizzle;
285     GrTextureType fTextureType = GrTextureType::k2D;
286     uint32_t fExtraSamplerKey = 0;
287     bool fIsInitialized = false;
288 };
289 
IthTextureSampler(int i)290 const 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 /**
299  * 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  */
GrVertexAttribTypeSize(GrVertexAttribType type)303 static 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:
314             return sizeof(uint16_t);
315         case kHalf2_GrVertexAttribType:
316             return 2 * sizeof(uint16_t);
317         case kHalf3_GrVertexAttribType:
318             return 3 * sizeof(uint16_t);
319         case kHalf4_GrVertexAttribType:
320             return 4 * sizeof(uint16_t);
321         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);
327         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);
343         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);
349         case kShort4_GrVertexAttribType:
350             return 4 * sizeof(int16_t);
351         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);
358         case kUShort_norm_GrVertexAttribType:
359             return sizeof(uint16_t);
360         // Experimental (for Y416)
361         case kUShort4_norm_GrVertexAttribType:
362             return 4 * sizeof(uint16_t);
363     }
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 
size()372 constexpr size_t GrPrimitiveProcessor::Attribute::size() const {
373     return GrVertexAttribTypeSize(fCPUType);
374 }
375 
376 #endif
377