blob: 67b00840f061d5ba4fe1b787f1bbef2312a39dce [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
bsalomon91d844d2015-08-10 10:47:29 -070076 * GrBatch which should be forwarded to the GrPrimitiveProcessor(s) created by the batch.
77 * These are not properly part of the pipeline because they assume the specific inputs
78 * that the batch provided when it created the pipeline. Identical pipelines may be
79 * created by different batches with different input assumptions and therefore different
80 * computed optimizations. It is the batch-specific optimizations that allow the pipelines
81 * to be equal.
joshualitt8072caa2015-02-12 14:20:52 -080082 */
bsalomon91d844d2015-08-10 10:47:29 -070083class GrPipelineOptimizations {
bsalomon7765a472015-07-08 11:26:37 -070084public:
85 /** Does the pipeline require the GrPrimitiveProcessor's color? */
bsalomonc6998732015-08-10 12:01:15 -070086 bool readsColor() const { return SkToBool(kReadsColor_Flag & fFlags); }
bsalomon7765a472015-07-08 11:26:37 -070087
88 /** Does the pipeline require the GrPrimitiveProcessor's coverage? */
bsalomon91d844d2015-08-10 10:47:29 -070089 bool readsCoverage() const { return
bsalomonc6998732015-08-10 12:01:15 -070090 SkToBool(kReadsCoverage_Flag & fFlags); }
bsalomon7765a472015-07-08 11:26:37 -070091
92 /** Does the pipeline require access to (implicit or explicit) local coordinates? */
93 bool readsLocalCoords() const {
bsalomonc6998732015-08-10 12:01:15 -070094 return SkToBool(kReadsLocalCoords_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070095 }
96
97 /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one
98 color output ? */
99 bool canTweakAlphaForCoverage() const {
bsalomonc6998732015-08-10 12:01:15 -0700100 return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -0700101 }
102
103 /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if
104 so get the color)? */
105 bool getOverrideColorIfSet(GrColor* overrideColor) const {
bsalomonc6998732015-08-10 12:01:15 -0700106 if (SkToBool(kUseOverrideColor_Flag & fFlags)) {
107 SkASSERT(SkToBool(kReadsColor_Flag & fFlags));
bsalomon7765a472015-07-08 11:26:37 -0700108 if (overrideColor) {
109 *overrideColor = fOverrideColor;
110 }
111 return true;
112 }
113 return false;
114 }
115
116private:
117 enum {
118 // If this is not set the primitive processor need not produce a color output
bsalomonc6998732015-08-10 12:01:15 -0700119 kReadsColor_Flag = 0x1,
bsalomon7765a472015-07-08 11:26:37 -0700120
121 // If this is not set the primitive processor need not produce a coverage output
bsalomonc6998732015-08-10 12:01:15 -0700122 kReadsCoverage_Flag = 0x2,
bsalomon7765a472015-07-08 11:26:37 -0700123
124 // If this is not set the primitive processor need not produce local coordinates
bsalomonc6998732015-08-10 12:01:15 -0700125 kReadsLocalCoords_Flag = 0x4,
bsalomon7765a472015-07-08 11:26:37 -0700126
127 // If this flag is set then the primitive processor may produce color*coverage as
128 // its color output (and not output a separate coverage).
bsalomonc6998732015-08-10 12:01:15 -0700129 kCanTweakAlphaForCoverage_Flag = 0x8,
bsalomon7765a472015-07-08 11:26:37 -0700130
131 // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its
132 // output color. If not set fOverrideColor is to be ignored.
bsalomonc6998732015-08-10 12:01:15 -0700133 kUseOverrideColor_Flag = 0x10,
bsalomon7765a472015-07-08 11:26:37 -0700134 };
135
136 uint32_t fFlags;
137 GrColor fOverrideColor;
138
139 friend class GrPipeline; // To initialize this
joshualitt8072caa2015-02-12 14:20:52 -0800140};
141
142/*
143 * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders
144 * with vertex attributes / uniforms.
145 */
146enum GrGPInput {
147 kAllOnes_GrGPInput,
148 kAttribute_GrGPInput,
149 kUniform_GrGPInput,
150 kIgnored_GrGPInput,
151};
152
153/*
154 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
155 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
156 * pipelines, and they must provide some notion of equality
157 */
158class GrPrimitiveProcessor : public GrProcessor {
159public:
bsalomon91d844d2015-08-10 10:47:29 -0700160 virtual void initBatchTracker(GrBatchTracker*, const GrPipelineOptimizations&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800161
162 virtual bool canMakeEqual(const GrBatchTracker& mine,
163 const GrPrimitiveProcessor& that,
164 const GrBatchTracker& theirs) const = 0;
165
166 virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0;
167 virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0;
168
169 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
170 // we put these calls on the base class to prevent having to cast
171 virtual bool willUseGeoShader() const = 0;
172
173 /*
174 * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific
175 * attribute limits. This number can almost certainly be raised if required.
176 */
177 static const int kMaxVertexAttribs = 6;
178
179 struct Attribute {
180 Attribute()
181 : fName(NULL)
182 , fType(kFloat_GrVertexAttribType)
183 , fOffset(0) {}
senorblancof2539d52015-05-20 14:03:42 -0700184 Attribute(const char* name, GrVertexAttribType type,
185 GrSLPrecision precision = kDefault_GrSLPrecision)
joshualitt8072caa2015-02-12 14:20:52 -0800186 : fName(name)
187 , fType(type)
senorblancof2539d52015-05-20 14:03:42 -0700188 , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
189 , fPrecision(precision) {}
joshualitt8072caa2015-02-12 14:20:52 -0800190 const char* fName;
191 GrVertexAttribType fType;
192 size_t fOffset;
senorblancof2539d52015-05-20 14:03:42 -0700193 GrSLPrecision fPrecision;
joshualitt8072caa2015-02-12 14:20:52 -0800194 };
195
196 int numAttribs() const { return fNumAttribs; }
197 const Attribute& getAttrib(int index) const {
198 SkASSERT(index < fNumAttribs);
199 return fAttribs[index];
200 }
201
202 // Returns the vertex stride of the GP. A common use case is to request geometry from a
203 // drawtarget based off of the stride, and to populate this memory using an implicit array of
204 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
205 size_t getVertexStride() const { return fVertexStride; }
206
207 /**
208 * Gets a transformKey from an array of coord transforms
209 */
210 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>&) const;
211
212 /**
213 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
214 * processor's GL backend implementation.
215 */
216 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700217 const GrGLSLCaps& caps,
joshualitt8072caa2015-02-12 14:20:52 -0800218 GrProcessorKeyBuilder* b) const = 0;
219
220
221 /** Returns a new instance of the appropriate *GL* implementation class
222 for the given GrProcessor; caller is responsible for deleting
223 the object. */
224 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700225 const GrGLSLCaps& caps) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800226
227 bool isPathRendering() const { return fIsPathRendering; }
228
joshualittb2aa7cb2015-08-05 11:05:22 -0700229 /**
230 * No Local Coord Transformation is needed in the shader, instead transformed local coords will
231 * be provided via vertex attribute.
232 */
233 virtual bool hasTransformedLocalCoords() const = 0;
234
joshualitt8072caa2015-02-12 14:20:52 -0800235protected:
joshualitte3ababe2015-05-15 07:56:07 -0700236 GrPrimitiveProcessor(bool isPathRendering)
joshualitt8072caa2015-02-12 14:20:52 -0800237 : fNumAttribs(0)
238 , fVertexStride(0)
joshualitt8072caa2015-02-12 14:20:52 -0800239 , fIsPathRendering(isPathRendering) {}
240
joshualitt8072caa2015-02-12 14:20:52 -0800241 Attribute fAttribs[kMaxVertexAttribs];
242 int fNumAttribs;
243 size_t fVertexStride;
244
245private:
246 virtual bool hasExplicitLocalCoords() const = 0;
247
joshualitt8072caa2015-02-12 14:20:52 -0800248 bool fIsPathRendering;
249
250 typedef GrProcessor INHERITED;
251};
252
253#endif