blob: d66aed1ddc78fbd4bdd85b80711ff70bc1c39cd8 [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
69class GrIndexBufferAllocPool;
jvanverthe9c0fc62015-04-29 11:18:05 -070070class GrGLSLCaps;
joshualitt8072caa2015-02-12 14:20:52 -080071class GrGLPrimitiveProcessor;
72class GrVertexBufferAllocPool;
73
74struct GrInitInvariantOutput;
75
76/*
77 * This struct allows the GrPipeline to communicate information about the pipeline. Most of this
78 * is overrides, but some of it is general information. Logically it should live in GrPipeline.h,
79 * but this is problematic due to circular dependencies.
80 */
81struct GrPipelineInfo {
82 bool fColorIgnored;
83 bool fCoverageIgnored;
84 GrColor fOverrideColor;
85 bool fUsesLocalCoords;
egdanielf7c2d552015-02-13 12:11:00 -080086 bool fCanTweakAlphaForCoverage;
joshualitt8072caa2015-02-12 14:20:52 -080087};
88
89/*
90 * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders
91 * with vertex attributes / uniforms.
92 */
93enum GrGPInput {
94 kAllOnes_GrGPInput,
95 kAttribute_GrGPInput,
96 kUniform_GrGPInput,
97 kIgnored_GrGPInput,
98};
99
100/*
101 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
102 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
103 * pipelines, and they must provide some notion of equality
104 */
105class GrPrimitiveProcessor : public GrProcessor {
106public:
107 // TODO let the PrimProc itself set this in its setData call, this should really live on the
108 // bundle of primitive data
109 const SkMatrix& viewMatrix() const { return fViewMatrix; }
110 const SkMatrix& localMatrix() const { return fLocalMatrix; }
111
112 virtual void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const = 0;
113
114 virtual bool canMakeEqual(const GrBatchTracker& mine,
115 const GrPrimitiveProcessor& that,
116 const GrBatchTracker& theirs) const = 0;
117
118 virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0;
119 virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0;
120
121 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
122 // we put these calls on the base class to prevent having to cast
123 virtual bool willUseGeoShader() const = 0;
124
125 /*
126 * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific
127 * attribute limits. This number can almost certainly be raised if required.
128 */
129 static const int kMaxVertexAttribs = 6;
130
131 struct Attribute {
132 Attribute()
133 : fName(NULL)
134 , fType(kFloat_GrVertexAttribType)
135 , fOffset(0) {}
136 Attribute(const char* name, GrVertexAttribType type)
137 : fName(name)
138 , fType(type)
139 , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) {}
140 const char* fName;
141 GrVertexAttribType fType;
142 size_t fOffset;
143 };
144
145 int numAttribs() const { return fNumAttribs; }
146 const Attribute& getAttrib(int index) const {
147 SkASSERT(index < fNumAttribs);
148 return fAttribs[index];
149 }
150
151 // Returns the vertex stride of the GP. A common use case is to request geometry from a
152 // drawtarget based off of the stride, and to populate this memory using an implicit array of
153 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
154 size_t getVertexStride() const { return fVertexStride; }
155
156 /**
157 * Gets a transformKey from an array of coord transforms
158 */
159 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>&) const;
160
161 /**
162 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
163 * processor's GL backend implementation.
164 */
165 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700166 const GrGLSLCaps& caps,
joshualitt8072caa2015-02-12 14:20:52 -0800167 GrProcessorKeyBuilder* b) const = 0;
168
169
170 /** Returns a new instance of the appropriate *GL* implementation class
171 for the given GrProcessor; caller is responsible for deleting
172 the object. */
173 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700174 const GrGLSLCaps& caps) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800175
176 bool isPathRendering() const { return fIsPathRendering; }
177
178protected:
179 GrPrimitiveProcessor(const SkMatrix& viewMatrix, const SkMatrix& localMatrix,
180 bool isPathRendering)
181 : fNumAttribs(0)
182 , fVertexStride(0)
183 , fViewMatrix(viewMatrix)
184 , fLocalMatrix(localMatrix)
185 , fIsPathRendering(isPathRendering) {}
186
187 /*
188 * CanCombineOutput will return true if two draws are 'batchable' from a color perspective.
189 * TODO remove this when GPs can upgrade to attribute color
190 */
191 static bool CanCombineOutput(GrGPInput left, GrColor lColor, GrGPInput right, GrColor rColor) {
192 if (left != right) {
193 return false;
194 }
195
196 if (kUniform_GrGPInput == left && lColor != rColor) {
197 return false;
198 }
199
200 return true;
201 }
202
203 static bool CanCombineLocalMatrices(const GrPrimitiveProcessor& left,
204 bool leftUsesLocalCoords,
205 const GrPrimitiveProcessor& right,
206 bool rightUsesLocalCoords) {
207 if (leftUsesLocalCoords != rightUsesLocalCoords) {
208 return false;
209 }
210
211 if (leftUsesLocalCoords && !left.localMatrix().cheapEqualTo(right.localMatrix())) {
212 return false;
213 }
214 return true;
215 }
216
217 Attribute fAttribs[kMaxVertexAttribs];
218 int fNumAttribs;
219 size_t fVertexStride;
220
221private:
222 virtual bool hasExplicitLocalCoords() const = 0;
223
224 const SkMatrix fViewMatrix;
225 SkMatrix fLocalMatrix;
226 bool fIsPathRendering;
227
228 typedef GrProcessor INHERITED;
229};
230
231#endif