blob: cb23897fb6442333872a83a95eb0e10a83b113a9 [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
egdaniele659a582015-11-13 09:55:43 -080045class GrGLSLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080046
47struct GrInitInvariantOutput;
48
halcanary9d524f22016-03-29 09:03:52 -070049// Describes the state of pixel local storage with respect to the current draw.
ethannicholas22793252016-01-30 09:59:10 -080050enum GrPixelLocalStorageState {
51 // The draw is actively updating PLS.
52 kDraw_GrPixelLocalStorageState,
53 // The draw is a "finish" operation which is reading from PLS and writing color.
54 kFinish_GrPixelLocalStorageState,
55 // The draw does not use PLS.
56 kDisabled_GrPixelLocalStorageState
57};
58
joshualitt8072caa2015-02-12 14:20:52 -080059/*
bsalomon7765a472015-07-08 11:26:37 -070060 * This class allows the GrPipeline to communicate information about the pipeline to a
bsalomon91d844d2015-08-10 10:47:29 -070061 * GrBatch which should be forwarded to the GrPrimitiveProcessor(s) created by the batch.
62 * These are not properly part of the pipeline because they assume the specific inputs
63 * that the batch provided when it created the pipeline. Identical pipelines may be
64 * created by different batches with different input assumptions and therefore different
65 * computed optimizations. It is the batch-specific optimizations that allow the pipelines
66 * to be equal.
joshualitt8072caa2015-02-12 14:20:52 -080067 */
ethannicholasff210322015-11-24 12:10:10 -080068class GrXPOverridesForBatch {
bsalomon7765a472015-07-08 11:26:37 -070069public:
70 /** Does the pipeline require the GrPrimitiveProcessor's color? */
bsalomonc6998732015-08-10 12:01:15 -070071 bool readsColor() const { return SkToBool(kReadsColor_Flag & fFlags); }
bsalomon7765a472015-07-08 11:26:37 -070072
73 /** Does the pipeline require the GrPrimitiveProcessor's coverage? */
bsalomon91d844d2015-08-10 10:47:29 -070074 bool readsCoverage() const { return
bsalomonc6998732015-08-10 12:01:15 -070075 SkToBool(kReadsCoverage_Flag & fFlags); }
bsalomon7765a472015-07-08 11:26:37 -070076
77 /** Does the pipeline require access to (implicit or explicit) local coordinates? */
78 bool readsLocalCoords() const {
bsalomonc6998732015-08-10 12:01:15 -070079 return SkToBool(kReadsLocalCoords_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070080 }
81
82 /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one
83 color output ? */
84 bool canTweakAlphaForCoverage() const {
bsalomonc6998732015-08-10 12:01:15 -070085 return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070086 }
87
88 /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if
89 so get the color)? */
90 bool getOverrideColorIfSet(GrColor* overrideColor) const {
bsalomonc6998732015-08-10 12:01:15 -070091 if (SkToBool(kUseOverrideColor_Flag & fFlags)) {
92 SkASSERT(SkToBool(kReadsColor_Flag & fFlags));
bsalomon7765a472015-07-08 11:26:37 -070093 if (overrideColor) {
94 *overrideColor = fOverrideColor;
95 }
96 return true;
97 }
98 return false;
99 }
100
bsalomon2d563032015-08-13 07:08:31 -0700101 /**
102 * Returns true if the pipeline's color output will be affected by the existing render target
103 * destination pixel values (meaning we need to be careful with overlapping draws). Note that we
104 * can conflate coverage and color, so the destination color may still bleed into pixels that
105 * have partial coverage, even if this function returns false.
106 *
107 * The above comment seems incorrect for the use case. This funciton is used to turn two
108 * overlapping draws into a single draw (really to stencil multiple paths and do a single
109 * cover). It seems that what really matters is whether the dst is read for color OR for
110 * coverage.
111 */
112 bool willColorBlendWithDst() const { return SkToBool(kWillColorBlendWithDst_Flag & fFlags); }
113
bsalomon7765a472015-07-08 11:26:37 -0700114private:
115 enum {
116 // If this is not set the primitive processor need not produce a color output
bsalomonc6998732015-08-10 12:01:15 -0700117 kReadsColor_Flag = 0x1,
bsalomon7765a472015-07-08 11:26:37 -0700118
119 // If this is not set the primitive processor need not produce a coverage output
bsalomonc6998732015-08-10 12:01:15 -0700120 kReadsCoverage_Flag = 0x2,
bsalomon7765a472015-07-08 11:26:37 -0700121
122 // If this is not set the primitive processor need not produce local coordinates
bsalomonc6998732015-08-10 12:01:15 -0700123 kReadsLocalCoords_Flag = 0x4,
bsalomon7765a472015-07-08 11:26:37 -0700124
125 // If this flag is set then the primitive processor may produce color*coverage as
126 // its color output (and not output a separate coverage).
bsalomonc6998732015-08-10 12:01:15 -0700127 kCanTweakAlphaForCoverage_Flag = 0x8,
bsalomon7765a472015-07-08 11:26:37 -0700128
129 // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its
130 // output color. If not set fOverrideColor is to be ignored.
bsalomonc6998732015-08-10 12:01:15 -0700131 kUseOverrideColor_Flag = 0x10,
bsalomon2d563032015-08-13 07:08:31 -0700132
133 kWillColorBlendWithDst_Flag = 0x20,
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/*
joshualitt8072caa2015-02-12 14:20:52 -0800143 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
144 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
145 * pipelines, and they must provide some notion of equality
146 */
147class GrPrimitiveProcessor : public GrProcessor {
148public:
joshualitt8072caa2015-02-12 14:20:52 -0800149 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
150 // we put these calls on the base class to prevent having to cast
151 virtual bool willUseGeoShader() const = 0;
152
joshualitt8072caa2015-02-12 14:20:52 -0800153 struct Attribute {
154 Attribute()
halcanary96fcdcc2015-08-27 07:41:13 -0700155 : fName(nullptr)
joshualitt8072caa2015-02-12 14:20:52 -0800156 , fType(kFloat_GrVertexAttribType)
157 , fOffset(0) {}
bsalomon6cb807b2016-08-17 11:33:39 -0700158 Attribute(const char* name, GrVertexAttribType type, GrSLPrecision precision)
joshualitt8072caa2015-02-12 14:20:52 -0800159 : fName(name)
160 , fType(type)
senorblancof2539d52015-05-20 14:03:42 -0700161 , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
162 , fPrecision(precision) {}
joshualitt8072caa2015-02-12 14:20:52 -0800163 const char* fName;
164 GrVertexAttribType fType;
165 size_t fOffset;
senorblancof2539d52015-05-20 14:03:42 -0700166 GrSLPrecision fPrecision;
joshualitt8072caa2015-02-12 14:20:52 -0800167 };
168
bsalomon7dbd45d2016-03-23 10:40:53 -0700169 int numAttribs() const { return fAttribs.count(); }
170 const Attribute& getAttrib(int index) const { return fAttribs[index]; }
joshualitt8072caa2015-02-12 14:20:52 -0800171
172 // Returns the vertex stride of the GP. A common use case is to request geometry from a
Robert Phillipsf2361d22016-10-25 14:20:06 -0400173 // GrOpList based off of the stride, and to populate this memory using an implicit array of
joshualitt8072caa2015-02-12 14:20:52 -0800174 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
175 size_t getVertexStride() const { return fVertexStride; }
176
177 /**
wangyixa7f4c432015-08-20 07:25:02 -0700178 * Computes a transformKey from an array of coord transforms. Will only look at the first
179 * <numCoords> transforms in the array.
180 *
181 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800182 */
wangyixa7f4c432015-08-20 07:25:02 -0700183 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
184 int numCoords) const;
joshualitt8072caa2015-02-12 14:20:52 -0800185
186 /**
187 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
188 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -0700189 *
190 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800191 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500192 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800193
194
195 /** Returns a new instance of the appropriate *GL* implementation class
196 for the given GrProcessor; caller is responsible for deleting
197 the object. */
Brian Salomon94efbf52016-11-29 13:43:05 -0500198 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800199
ethannicholas22793252016-01-30 09:59:10 -0800200 virtual bool isPathRendering() const { return false; }
joshualitt8072caa2015-02-12 14:20:52 -0800201
halcanary9d524f22016-03-29 09:03:52 -0700202 virtual GrPixelLocalStorageState getPixelLocalStorageState() const {
ethannicholas22793252016-01-30 09:59:10 -0800203 return kDisabled_GrPixelLocalStorageState;
204 }
205
206 /**
207 * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor().
208 */
209 virtual const char* getDestColorOverride() const { return nullptr; }
halcanary9d524f22016-03-29 09:03:52 -0700210
ethannicholas28ef4452016-03-25 09:26:03 -0700211 virtual float getSampleShading() const {
212 return 0.0;
213 }
214
dvonbeck9b03e7b2016-08-01 11:01:56 -0700215 /* Sub-class should override and return true if this primitive processor implements the distance
216 * vector field, a field of vectors to the nearest point in the edge of the shape. */
217 virtual bool implementsDistanceVector() const { return false; }
218
joshualitt8072caa2015-02-12 14:20:52 -0800219protected:
bsalomon7dbd45d2016-03-23 10:40:53 -0700220 GrPrimitiveProcessor() : fVertexStride(0) {}
joshualitt8072caa2015-02-12 14:20:52 -0800221
bsalomon7dbd45d2016-03-23 10:40:53 -0700222 enum { kPreallocAttribCnt = 8 };
223 SkSTArray<kPreallocAttribCnt, Attribute> fAttribs;
joshualitt8072caa2015-02-12 14:20:52 -0800224 size_t fVertexStride;
225
226private:
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400227 void notifyRefCntIsZero() const final {}
joshualitt8072caa2015-02-12 14:20:52 -0800228 virtual bool hasExplicitLocalCoords() const = 0;
229
joshualitt8072caa2015-02-12 14:20:52 -0800230 typedef GrProcessor INHERITED;
231};
232
233#endif