blob: f3ae800b8030404edd9f3047b9ff2e9e32423d68 [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
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 GrGeometryProcessor_DEFINED
9#define GrGeometryProcessor_DEFINED
10
joshualitt2e3b3e32014-12-09 13:31:14 -080011#include "GrColor.h"
joshualittc07379d2014-11-20 14:50:39 -080012#include "GrGeometryData.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "GrProcessor.h"
bsalomon6251d172014-10-15 10:50:36 -070014#include "GrShaderVar.h"
joshualittb0a8a372014-09-23 09:50:21 -070015
joshualitt87f48d92014-12-04 10:41:40 -080016/*
joshualitt9b989322014-12-15 14:16:27 -080017 * The GrPrimitiveProcessor represents some kind of geometric primitive. This includes the shape
18 * of the primitive and the inherent color of the primitive. The GrPrimitiveProcessor is
19 * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through
20 * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
21 * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this
22 * functionality. We also use the GrPrimitiveProcessor to make batching decisions.
23 *
24 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
25 * GrPrimitiveProcessor. These loops run on the CPU and compute any invariant components which
26 * might be useful for correctness / optimization decisions. The GrPrimitiveProcessor seeds these
27 * loops, one with initial color and one with initial coverage, in its
28 * onComputeInvariantColor / Coverage calls. These seed values are processed by the subsequent
29 * stages of the rendering pipeline and the output is then fed back into the GrPrimitiveProcessor in
30 * the initBatchTracker call, where the GrPrimitiveProcessor can then initialize the GrBatchTracker
31 * struct with the appropriate values.
32 *
33 * We are evolving this system to move towards generating geometric meshes and their associated
34 * vertex data after we have batched and reordered draws. This system, known as 'deferred geometry'
35 * will allow the GrPrimitiveProcessor much greater control over how data is transmitted to shaders.
36 *
37 * In a deferred geometry world, the GrPrimitiveProcessor can always 'batch' To do this, each
38 * primitive type is associated with one GrPrimitiveProcessor, who has complete control of how
39 * it draws. Each primitive draw will bundle all required data to perform the draw, and these
40 * bundles of data will be owned by an instance of the associated GrPrimitiveProcessor. Bundles
41 * can be updated alongside the GrBatchTracker struct itself, ultimately allowing the
42 * GrPrimitiveProcessor complete control of how it gets data into the fragment shader as long as
43 * it emits the appropriate color, or none at all, as directed.
44 */
45
46/*
joshualitt87f48d92014-12-04 10:41:40 -080047 * A struct for tracking batching decisions. While this lives on GrOptState, it is managed
48 * entirely by the derived classes of the GP.
49 */
50class GrBatchTracker {
51public:
52 template <typename T> const T& cast() const {
53 SkASSERT(sizeof(T) <= kMaxSize);
joshualitt9b989322014-12-15 14:16:27 -080054 return *reinterpret_cast<const T*>(fData.get());
joshualitt87f48d92014-12-04 10:41:40 -080055 }
56
57 template <typename T> T* cast() {
58 SkASSERT(sizeof(T) <= kMaxSize);
joshualitt9b989322014-12-15 14:16:27 -080059 return reinterpret_cast<T*>(fData.get());
joshualitt87f48d92014-12-04 10:41:40 -080060 }
61
62 static const size_t kMaxSize = 32;
63
64private:
joshualitt9b989322014-12-15 14:16:27 -080065 SkAlignedSStorage<kMaxSize> fData;
joshualitt87f48d92014-12-04 10:41:40 -080066};
67
joshualitteb2a6762014-12-04 11:35:33 -080068class GrGLCaps;
69class GrGLGeometryProcessor;
joshualitt87f48d92014-12-04 10:41:40 -080070class GrOptDrawState;
71
joshualitt56995b52014-12-11 15:44:02 -080072struct GrInitInvariantOutput;
73
joshualitt9b989322014-12-15 14:16:27 -080074
joshualitt56995b52014-12-11 15:44:02 -080075/*
joshualitt9b989322014-12-15 14:16:27 -080076 * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders
77 * with vertex attributes / uniforms.
78 */
79enum GrGPInput {
80 kAllOnes_GrGPInput,
81 kAttribute_GrGPInput,
82 kUniform_GrGPInput,
83 kIgnored_GrGPInput,
84};
85
86/*
87 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
88 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
89 * pipelines, and they must provide some notion of equality
joshualitt56995b52014-12-11 15:44:02 -080090 */
91class GrPrimitiveProcessor : public GrProcessor {
92public:
joshualitt9b989322014-12-15 14:16:27 -080093 /*
94 * This struct allows the optstate to communicate requirements to the GrPrimitiveProcessor.
95 */
96 struct InitBT {
97 bool fColorIgnored;
98 bool fCoverageIgnored;
99 GrColor fOverrideColor;
100 };
101
102 virtual void initBatchTracker(GrBatchTracker*, const InitBT&) const = 0;
103
104 virtual bool canMakeEqual(const GrBatchTracker& mine,
105 const GrPrimitiveProcessor& that,
106 const GrBatchTracker& theirs) const = 0;
107
108 /*
109 * We always call canMakeEqual before makeEqual so there is no need to do any kind of equality
110 * testing here
111 * TODO make this pure virtual when primProcs can actually use it
112 */
113 virtual void makeEqual(GrBatchTracker*, const GrBatchTracker&) const {}
114
joshualitt56995b52014-12-11 15:44:02 -0800115 virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0;
116 virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0;
117
egdanielc2304142014-12-11 13:15:13 -0800118 /**
119 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
120 * processor's GL backend implementation.
121 */
joshualitteb2a6762014-12-04 11:35:33 -0800122 virtual void getGLProcessorKey(const GrBatchTracker& bt,
123 const GrGLCaps& caps,
124 GrProcessorKeyBuilder* b) const = 0;
125
126
127 /** Returns a new instance of the appropriate *GL* implementation class
128 for the given GrProcessor; caller is responsible for deleting
129 the object. */
130 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const = 0;
joshualittb0a8a372014-09-23 09:50:21 -0700131
joshualitt9b989322014-12-15 14:16:27 -0800132protected:
133 /*
134 * CanCombineOutput will return true if two draws are 'batchable' from a color perspective.
135 * TODO remove this when GPs can upgrade to attribute color
136 */
137 static bool CanCombineOutput(GrGPInput left, GrColor lColor, GrGPInput right, GrColor rColor) {
138 if (left != right) {
139 return false;
140 }
141
142 if (kUniform_GrGPInput == left && lColor != rColor) {
143 return false;
144 }
145
146 return true;
147 }
148
149private:
150 typedef GrProcessor INHERITED;
151};
152
153/**
154 * A GrGeometryProcessor is a flexible method for rendering a primitive. The GrGeometryProcessor
155 * has complete control over vertex attributes and uniforms(aside from the render target) but it
156 * must obey the same contract as any GrPrimitiveProcessor, specifically it must emit a color and
157 * coverage into the fragment shader. Where this color and coverage come from is completely the
158 * responsibility of the GrGeometryProcessor.
159 */
160class GrGeometryProcessor : public GrPrimitiveProcessor {
161public:
162 // TODO the Hint can be handled in a much more clean way when we have deferred geometry or
163 // atleast bundles
164 GrGeometryProcessor(GrColor color, bool opaqueVertexColors = false)
165 : fVertexStride(0)
166 , fColor(color)
167 , fOpaqueVertexColors(opaqueVertexColors)
168 , fWillUseGeoShader(false)
169 , fHasVertexColor(false)
170 , fHasLocalCoords(false) {}
171
172 virtual const char* name() const = 0;
173
joshualittb0a8a372014-09-23 09:50:21 -0700174 /*
joshualitt2dd1ae02014-12-03 06:24:10 -0800175 * This is a safeguard to prevent GPs from going beyond platform specific attribute limits.
176 * This number can almost certainly be raised if required.
joshualittb0a8a372014-09-23 09:50:21 -0700177 */
joshualitt2dd1ae02014-12-03 06:24:10 -0800178 static const int kMaxVertexAttribs = 6;
joshualittb0a8a372014-09-23 09:50:21 -0700179
joshualitt2dd1ae02014-12-03 06:24:10 -0800180 struct GrAttribute {
181 GrAttribute(const char* name, GrVertexAttribType type)
182 : fName(name)
183 , fType(type)
184 , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) {}
185 const char* fName;
186 GrVertexAttribType fType;
187 size_t fOffset;
188 };
189
190 typedef SkTArray<GrAttribute, true> VertexAttribArray;
191
192 const VertexAttribArray& getAttribs() const { return fAttribs; }
193
194 // Returns the vertex stride of the GP. A common use case is to request geometry from a
195 // drawtarget based off of the stride, and to populate this memory using an implicit array of
196 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
197 size_t getVertexStride() const { return fVertexStride; }
joshualittb0a8a372014-09-23 09:50:21 -0700198
joshualitt74077b92014-10-24 11:26:03 -0700199 bool willUseGeoShader() const { return fWillUseGeoShader; }
200
joshualitt9b989322014-12-15 14:16:27 -0800201 /*
202 * In an ideal world, two GrGeometryProcessors with the same class id and texture accesses
203 * would ALWAYS be able to batch together. If two GrGeometryProcesosrs are the same then we
204 * will only keep one of them. The remaining GrGeometryProcessor then updates its
205 * GrBatchTracker to incorporate the draw information from the GrGeometryProcessor we discard.
206 * Any bundles associated with the discarded GrGeometryProcessor will be attached to the
207 * remaining GrGeometryProcessor.
208 */
209 bool canMakeEqual(const GrBatchTracker& mine,
210 const GrPrimitiveProcessor& that,
211 const GrBatchTracker& theirs) const SK_OVERRIDE {
joshualitteb2a6762014-12-04 11:35:33 -0800212 if (this->classID() != that.classID() || !this->hasSameTextureAccesses(that)) {
bsalomon0e08fc12014-10-15 08:19:04 -0700213 return false;
214 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800215
joshualitt56995b52014-12-11 15:44:02 -0800216 // TODO remove the hint
joshualitt9b989322014-12-15 14:16:27 -0800217 const GrGeometryProcessor& other = that.cast<GrGeometryProcessor>();
218 if (fHasVertexColor && fOpaqueVertexColors != other.fOpaqueVertexColors) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800219 return false;
220 }
221
joshualitt9b989322014-12-15 14:16:27 -0800222 // TODO this equality test should really be broken up, some of this can live on the batch
223 // tracker test and some of this should be in bundles
224 if (!this->onIsEqual(other)) {
joshualitt56995b52014-12-11 15:44:02 -0800225 return false;
226 }
227
joshualitt9b989322014-12-15 14:16:27 -0800228 return this->onCanMakeEqual(mine, theirs);
bsalomon0e08fc12014-10-15 08:19:04 -0700229 }
230
joshualitt9b989322014-12-15 14:16:27 -0800231
232 // TODO we can remove color from the GrGeometryProcessor base class once we have bundles of
233 // primitive data
joshualitt56995b52014-12-11 15:44:02 -0800234 GrColor color() const { return fColor; }
joshualitt2e3b3e32014-12-09 13:31:14 -0800235
joshualitt9b989322014-12-15 14:16:27 -0800236 // TODO this is a total hack until the gp can do deferred geometry
joshualitt2dd1ae02014-12-03 06:24:10 -0800237 bool hasVertexColor() const { return fHasVertexColor; }
joshualitt9b989322014-12-15 14:16:27 -0800238
239 // TODO this is a total hack until gp can setup and manage local coords
joshualitt2dd1ae02014-12-03 06:24:10 -0800240 bool hasLocalCoords() const { return fHasLocalCoords; }
241
joshualitt56995b52014-12-11 15:44:02 -0800242 void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE;
243 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE;
joshualitt2e3b3e32014-12-09 13:31:14 -0800244
joshualittb0a8a372014-09-23 09:50:21 -0700245protected:
joshualitt9b989322014-12-15 14:16:27 -0800246 /*
247 * An optional simple helper function to determine by what means the GrGeometryProcessor should
248 * use to provide color. If we are given an override color(ie the given overridecolor is NOT
249 * GrColor_ILLEGAL) then we must always emit that color(currently overrides are only supported
250 * via uniform, but with deferred Geometry we could use attributes). Otherwise, if our color is
251 * ignored then we should not emit a color. Lastly, if we don't have vertex colors then we must
252 * emit a color via uniform
253 * TODO this function changes quite a bit with deferred geometry. There the GrGeometryProcessor
254 * can upload a new color via attribute if needed.
255 */
256 static GrGPInput GetColorInputType(GrColor* color, GrColor primitiveColor, const InitBT& init,
257 bool hasVertexColor) {
258 if (init.fColorIgnored) {
259 *color = GrColor_ILLEGAL;
260 return kIgnored_GrGPInput;
261 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
262 *color = init.fOverrideColor;
263 return kUniform_GrGPInput;
264 }
265
266 *color = primitiveColor;
267 if (hasVertexColor) {
268 return kAttribute_GrGPInput;
269 } else {
270 return kUniform_GrGPInput;
271 }
272 }
273
joshualittb0a8a372014-09-23 09:50:21 -0700274 /**
joshualitt2dd1ae02014-12-03 06:24:10 -0800275 * Subclasses call this from their constructor to register vertex attributes. Attributes
276 * will be padded to the nearest 4 bytes for performance reasons.
277 * TODO After deferred geometry, we should do all of this inline in GenerateGeometry alongside
joshualitt9b989322014-12-15 14:16:27 -0800278 * the struct used to actually populate the attributes. This is all extremely fragile, vertex
279 * attributes have to be added in the order they will appear in the struct which maps memory.
280 * The processor key should reflect the vertex attributes, or there lack thereof in the
281 * GrGeometryProcessor.
joshualittb0a8a372014-09-23 09:50:21 -0700282 */
joshualitt2dd1ae02014-12-03 06:24:10 -0800283 const GrAttribute& addVertexAttrib(const GrAttribute& attribute) {
284 fVertexStride += attribute.fOffset;
285 return fAttribs.push_back(attribute);
joshualittb0a8a372014-09-23 09:50:21 -0700286 }
287
joshualitt74077b92014-10-24 11:26:03 -0700288 void setWillUseGeoShader() { fWillUseGeoShader = true; }
289
joshualitt2dd1ae02014-12-03 06:24:10 -0800290 // TODO hack see above
291 void setHasVertexColor() { fHasVertexColor = true; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800292 void setHasLocalCoords() { fHasLocalCoords = true; }
293
joshualitt56995b52014-12-11 15:44:02 -0800294 virtual void onGetInvariantOutputColor(GrInitInvariantOutput*) const {}
295 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const = 0;
296
joshualittb0a8a372014-09-23 09:50:21 -0700297private:
joshualitt9b989322014-12-15 14:16:27 -0800298 virtual bool onCanMakeEqual(const GrBatchTracker& mine, const GrBatchTracker& theirs) const = 0;
299 // TODO delete this when we have more advanced equality testing via bundles and the BT
bsalomon0e08fc12014-10-15 08:19:04 -0700300 virtual bool onIsEqual(const GrGeometryProcessor&) const = 0;
301
joshualitt2dd1ae02014-12-03 06:24:10 -0800302 SkSTArray<kMaxVertexAttribs, GrAttribute, true> fAttribs;
303 size_t fVertexStride;
joshualitt2e3b3e32014-12-09 13:31:14 -0800304 GrColor fColor;
joshualitt56995b52014-12-11 15:44:02 -0800305 bool fOpaqueVertexColors;
joshualitt74077b92014-10-24 11:26:03 -0700306 bool fWillUseGeoShader;
joshualitt2dd1ae02014-12-03 06:24:10 -0800307 bool fHasVertexColor;
joshualitt2dd1ae02014-12-03 06:24:10 -0800308 bool fHasLocalCoords;
joshualittb0a8a372014-09-23 09:50:21 -0700309
310 typedef GrProcessor INHERITED;
311};
joshualitt56995b52014-12-11 15:44:02 -0800312
313/*
314 * The path equivalent of the GP. For now this just manages color. In the long term we plan on
315 * extending this class to handle all nvpr uniform / varying / program work.
316 */
317class GrPathProcessor : public GrPrimitiveProcessor {
318public:
319 static GrPathProcessor* Create(GrColor color) {
320 return SkNEW_ARGS(GrPathProcessor, (color));
321 }
joshualitt9b989322014-12-15 14:16:27 -0800322
323 void initBatchTracker(GrBatchTracker*, const InitBT&) const SK_OVERRIDE;
324
325 bool canMakeEqual(const GrBatchTracker& mine,
326 const GrPrimitiveProcessor& that,
327 const GrBatchTracker& theirs) const SK_OVERRIDE;
joshualitt56995b52014-12-11 15:44:02 -0800328
329 const char* name() const SK_OVERRIDE { return "PathProcessor"; }
joshualitt9b989322014-12-15 14:16:27 -0800330
331 GrColor color() const { return fColor; }
332
joshualitt56995b52014-12-11 15:44:02 -0800333 void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE;
334 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE;
335
joshualitt9b989322014-12-15 14:16:27 -0800336 virtual void getGLProcessorKey(const GrBatchTracker& bt,
337 const GrGLCaps& caps,
338 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
339
340 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
341
joshualitt56995b52014-12-11 15:44:02 -0800342private:
joshualitt9b989322014-12-15 14:16:27 -0800343 GrPathProcessor(GrColor color);
joshualitt56995b52014-12-11 15:44:02 -0800344 GrColor fColor;
345
346 typedef GrProcessor INHERITED;
347};
joshualittb0a8a372014-09-23 09:50:21 -0700348#endif