blob: e5635a823bf4d7feb3710a127a6d6d2126c30d73 [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"
12#include "GrProcessor.h"
13#include "GrShaderVar.h"
14
15/*
16 * The GrPrimitiveProcessor represents some kind of geometric primitive. This includes the shape
17 * of the primitive and the inherent color of the primitive. The GrPrimitiveProcessor is
18 * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through
19 * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
20 * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this
21 * functionality. We also use the GrPrimitiveProcessor to make batching decisions.
22 *
23 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
24 * GrPrimitiveProcessor. These loops run on the CPU and compute any invariant components which
25 * might be useful for correctness / optimization decisions. The GrPrimitiveProcessor seeds these
26 * loops, one with initial color and one with initial coverage, in its
27 * onComputeInvariantColor / Coverage calls. These seed values are processed by the subsequent
28 * stages of the rendering pipeline and the output is then fed back into the GrPrimitiveProcessor in
29 * the initBatchTracker call, where the GrPrimitiveProcessor can then initialize the GrBatchTracker
30 * struct with the appropriate values.
31 *
32 * We are evolving this system to move towards generating geometric meshes and their associated
33 * vertex data after we have batched and reordered draws. This system, known as 'deferred geometry'
34 * will allow the GrPrimitiveProcessor much greater control over how data is transmitted to shaders.
35 *
36 * In a deferred geometry world, the GrPrimitiveProcessor can always 'batch' To do this, each
37 * primitive type is associated with one GrPrimitiveProcessor, who has complete control of how
38 * it draws. Each primitive draw will bundle all required data to perform the draw, and these
39 * bundles of data will be owned by an instance of the associated GrPrimitiveProcessor. Bundles
40 * can be updated alongside the GrBatchTracker struct itself, ultimately allowing the
41 * GrPrimitiveProcessor complete control of how it gets data into the fragment shader as long as
42 * it emits the appropriate color, or none at all, as directed.
43 */
44
45/*
46 * A struct for tracking batching decisions. While this lives on GrOptState, it is managed
47 * entirely by the derived classes of the GP.
48 * // TODO this was an early attempt at handling out of order batching. It should be
49 * used carefully as it is being replaced by GrBatch
50 */
51class GrBatchTracker {
52public:
53 template <typename T> const T& cast() const {
54 SkASSERT(sizeof(T) <= kMaxSize);
55 return *reinterpret_cast<const T*>(fData.get());
56 }
57
58 template <typename T> T* cast() {
59 SkASSERT(sizeof(T) <= kMaxSize);
60 return reinterpret_cast<T*>(fData.get());
61 }
62
63 static const size_t kMaxSize = 32;
64
65private:
66 SkAlignedSStorage<kMaxSize> fData;
67};
68
jvanverthe9c0fc62015-04-29 11:18:05 -070069class GrGLSLCaps;
joshualitt8072caa2015-02-12 14:20:52 -080070class GrGLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080071
72struct GrInitInvariantOutput;
73
74/*
bsalomon7765a472015-07-08 11:26:37 -070075 * This class allows the GrPipeline to communicate information about the pipeline to a
76 * GrPrimitiveProcessor that will be used in conjunction with the GrPipeline.
joshualitt8072caa2015-02-12 14:20:52 -080077 */
bsalomon7765a472015-07-08 11:26:37 -070078class GrPipelineInfo {
79public:
80 /** Does the pipeline require the GrPrimitiveProcessor's color? */
81 bool readsColor() const { return SkToBool(kReadsColor_GrPipelineInfoFlag & fFlags); }
82
83 /** Does the pipeline require the GrPrimitiveProcessor's coverage? */
84 bool readsCoverage() const { return SkToBool(kReadsCoverage_GrPipelineInfoFlag & fFlags); }
85
86 /** Does the pipeline require access to (implicit or explicit) local coordinates? */
87 bool readsLocalCoords() const {
88 return SkToBool(kReadsLocalCoords_GrPipelineInfoFlag & fFlags);
89 }
90
91 /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one
92 color output ? */
93 bool canTweakAlphaForCoverage() const {
94 return SkToBool(kCanTweakAlphaForCoverage_GrPipelineInfoFlag & fFlags);
95 }
96
97 /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if
98 so get the color)? */
99 bool getOverrideColorIfSet(GrColor* overrideColor) const {
100 if (SkToBool(kUseOverrideColor_GrPipelineInfoFlag & fFlags)) {
101 SkASSERT(SkToBool(kReadsColor_GrPipelineInfoFlag & fFlags));
102 if (overrideColor) {
103 *overrideColor = fOverrideColor;
104 }
105 return true;
106 }
107 return false;
108 }
109
110private:
111 enum {
112 // If this is not set the primitive processor need not produce a color output
113 kReadsColor_GrPipelineInfoFlag = 0x1,
114
115 // If this is not set the primitive processor need not produce a coverage output
116 kReadsCoverage_GrPipelineInfoFlag = 0x2,
117
118 // If this is not set the primitive processor need not produce local coordinates
119 kReadsLocalCoords_GrPipelineInfoFlag = 0x4,
120
121 // If this flag is set then the primitive processor may produce color*coverage as
122 // its color output (and not output a separate coverage).
123 kCanTweakAlphaForCoverage_GrPipelineInfoFlag = 0x8,
124
125 // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its
126 // output color. If not set fOverrideColor is to be ignored.
127 kUseOverrideColor_GrPipelineInfoFlag = 0x10,
128 };
129
130 uint32_t fFlags;
131 GrColor fOverrideColor;
132
133 friend class GrPipeline; // To initialize this
joshualitt8072caa2015-02-12 14:20:52 -0800134};
135
136/*
137 * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders
138 * with vertex attributes / uniforms.
139 */
140enum GrGPInput {
141 kAllOnes_GrGPInput,
142 kAttribute_GrGPInput,
143 kUniform_GrGPInput,
144 kIgnored_GrGPInput,
145};
146
147/*
148 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
149 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
150 * pipelines, and they must provide some notion of equality
151 */
152class GrPrimitiveProcessor : public GrProcessor {
153public:
joshualitt8072caa2015-02-12 14:20:52 -0800154 virtual void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const = 0;
155
156 virtual bool canMakeEqual(const GrBatchTracker& mine,
157 const GrPrimitiveProcessor& that,
158 const GrBatchTracker& theirs) const = 0;
159
160 virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0;
161 virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0;
162
163 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
164 // we put these calls on the base class to prevent having to cast
165 virtual bool willUseGeoShader() const = 0;
166
167 /*
168 * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific
169 * attribute limits. This number can almost certainly be raised if required.
170 */
171 static const int kMaxVertexAttribs = 6;
172
173 struct Attribute {
174 Attribute()
175 : fName(NULL)
176 , fType(kFloat_GrVertexAttribType)
177 , fOffset(0) {}
senorblancof2539d52015-05-20 14:03:42 -0700178 Attribute(const char* name, GrVertexAttribType type,
179 GrSLPrecision precision = kDefault_GrSLPrecision)
joshualitt8072caa2015-02-12 14:20:52 -0800180 : fName(name)
181 , fType(type)
senorblancof2539d52015-05-20 14:03:42 -0700182 , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
183 , fPrecision(precision) {}
joshualitt8072caa2015-02-12 14:20:52 -0800184 const char* fName;
185 GrVertexAttribType fType;
186 size_t fOffset;
senorblancof2539d52015-05-20 14:03:42 -0700187 GrSLPrecision fPrecision;
joshualitt8072caa2015-02-12 14:20:52 -0800188 };
189
190 int numAttribs() const { return fNumAttribs; }
191 const Attribute& getAttrib(int index) const {
192 SkASSERT(index < fNumAttribs);
193 return fAttribs[index];
194 }
195
196 // Returns the vertex stride of the GP. A common use case is to request geometry from a
197 // drawtarget based off of the stride, and to populate this memory using an implicit array of
198 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
199 size_t getVertexStride() const { return fVertexStride; }
200
201 /**
202 * Gets a transformKey from an array of coord transforms
203 */
204 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>&) const;
205
206 /**
207 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
208 * processor's GL backend implementation.
209 */
210 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700211 const GrGLSLCaps& caps,
joshualitt8072caa2015-02-12 14:20:52 -0800212 GrProcessorKeyBuilder* b) const = 0;
213
214
215 /** Returns a new instance of the appropriate *GL* implementation class
216 for the given GrProcessor; caller is responsible for deleting
217 the object. */
218 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700219 const GrGLSLCaps& caps) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800220
221 bool isPathRendering() const { return fIsPathRendering; }
222
223protected:
joshualitte3ababe2015-05-15 07:56:07 -0700224 GrPrimitiveProcessor(bool isPathRendering)
joshualitt8072caa2015-02-12 14:20:52 -0800225 : fNumAttribs(0)
226 , fVertexStride(0)
joshualitt8072caa2015-02-12 14:20:52 -0800227 , fIsPathRendering(isPathRendering) {}
228
joshualitt8072caa2015-02-12 14:20:52 -0800229 Attribute fAttribs[kMaxVertexAttribs];
230 int fNumAttribs;
231 size_t fVertexStride;
232
233private:
234 virtual bool hasExplicitLocalCoords() const = 0;
235
joshualitt8072caa2015-02-12 14:20:52 -0800236 bool fIsPathRendering;
237
238 typedef GrProcessor INHERITED;
239};
240
241#endif