blob: 467200a86d098316a9226c5c6dcc56db7e14426d [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
jvanverthe9c0fc62015-04-29 11:18:05 -070045class GrGLSLCaps;
egdaniele659a582015-11-13 09:55:43 -080046class GrGLSLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080047
48struct GrInitInvariantOutput;
49
50/*
bsalomon7765a472015-07-08 11:26:37 -070051 * This class allows the GrPipeline to communicate information about the pipeline to a
bsalomon91d844d2015-08-10 10:47:29 -070052 * GrBatch which should be forwarded to the GrPrimitiveProcessor(s) created by the batch.
53 * These are not properly part of the pipeline because they assume the specific inputs
54 * that the batch provided when it created the pipeline. Identical pipelines may be
55 * created by different batches with different input assumptions and therefore different
56 * computed optimizations. It is the batch-specific optimizations that allow the pipelines
57 * to be equal.
joshualitt8072caa2015-02-12 14:20:52 -080058 */
ethannicholasff210322015-11-24 12:10:10 -080059class GrXPOverridesForBatch {
bsalomon7765a472015-07-08 11:26:37 -070060public:
61 /** Does the pipeline require the GrPrimitiveProcessor's color? */
bsalomonc6998732015-08-10 12:01:15 -070062 bool readsColor() const { return SkToBool(kReadsColor_Flag & fFlags); }
bsalomon7765a472015-07-08 11:26:37 -070063
64 /** Does the pipeline require the GrPrimitiveProcessor's coverage? */
bsalomon91d844d2015-08-10 10:47:29 -070065 bool readsCoverage() const { return
bsalomonc6998732015-08-10 12:01:15 -070066 SkToBool(kReadsCoverage_Flag & fFlags); }
bsalomon7765a472015-07-08 11:26:37 -070067
68 /** Does the pipeline require access to (implicit or explicit) local coordinates? */
69 bool readsLocalCoords() const {
bsalomonc6998732015-08-10 12:01:15 -070070 return SkToBool(kReadsLocalCoords_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070071 }
72
73 /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one
74 color output ? */
75 bool canTweakAlphaForCoverage() const {
bsalomonc6998732015-08-10 12:01:15 -070076 return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070077 }
78
79 /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if
80 so get the color)? */
81 bool getOverrideColorIfSet(GrColor* overrideColor) const {
bsalomonc6998732015-08-10 12:01:15 -070082 if (SkToBool(kUseOverrideColor_Flag & fFlags)) {
83 SkASSERT(SkToBool(kReadsColor_Flag & fFlags));
bsalomon7765a472015-07-08 11:26:37 -070084 if (overrideColor) {
85 *overrideColor = fOverrideColor;
86 }
87 return true;
88 }
89 return false;
90 }
91
bsalomon2d563032015-08-13 07:08:31 -070092 /**
93 * Returns true if the pipeline's color output will be affected by the existing render target
94 * destination pixel values (meaning we need to be careful with overlapping draws). Note that we
95 * can conflate coverage and color, so the destination color may still bleed into pixels that
96 * have partial coverage, even if this function returns false.
97 *
98 * The above comment seems incorrect for the use case. This funciton is used to turn two
99 * overlapping draws into a single draw (really to stencil multiple paths and do a single
100 * cover). It seems that what really matters is whether the dst is read for color OR for
101 * coverage.
102 */
103 bool willColorBlendWithDst() const { return SkToBool(kWillColorBlendWithDst_Flag & fFlags); }
104
bsalomon7765a472015-07-08 11:26:37 -0700105private:
106 enum {
107 // If this is not set the primitive processor need not produce a color output
bsalomonc6998732015-08-10 12:01:15 -0700108 kReadsColor_Flag = 0x1,
bsalomon7765a472015-07-08 11:26:37 -0700109
110 // If this is not set the primitive processor need not produce a coverage output
bsalomonc6998732015-08-10 12:01:15 -0700111 kReadsCoverage_Flag = 0x2,
bsalomon7765a472015-07-08 11:26:37 -0700112
113 // If this is not set the primitive processor need not produce local coordinates
bsalomonc6998732015-08-10 12:01:15 -0700114 kReadsLocalCoords_Flag = 0x4,
bsalomon7765a472015-07-08 11:26:37 -0700115
116 // If this flag is set then the primitive processor may produce color*coverage as
117 // its color output (and not output a separate coverage).
bsalomonc6998732015-08-10 12:01:15 -0700118 kCanTweakAlphaForCoverage_Flag = 0x8,
bsalomon7765a472015-07-08 11:26:37 -0700119
120 // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its
121 // output color. If not set fOverrideColor is to be ignored.
bsalomonc6998732015-08-10 12:01:15 -0700122 kUseOverrideColor_Flag = 0x10,
bsalomon2d563032015-08-13 07:08:31 -0700123
124 kWillColorBlendWithDst_Flag = 0x20,
bsalomon7765a472015-07-08 11:26:37 -0700125 };
126
127 uint32_t fFlags;
128 GrColor fOverrideColor;
129
130 friend class GrPipeline; // To initialize this
joshualitt8072caa2015-02-12 14:20:52 -0800131};
132
133/*
joshualitt8072caa2015-02-12 14:20:52 -0800134 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
135 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
136 * pipelines, and they must provide some notion of equality
137 */
138class GrPrimitiveProcessor : public GrProcessor {
139public:
joshualitt8072caa2015-02-12 14:20:52 -0800140 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
141 // we put these calls on the base class to prevent having to cast
142 virtual bool willUseGeoShader() const = 0;
143
144 /*
145 * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific
146 * attribute limits. This number can almost certainly be raised if required.
147 */
148 static const int kMaxVertexAttribs = 6;
149
150 struct Attribute {
151 Attribute()
halcanary96fcdcc2015-08-27 07:41:13 -0700152 : fName(nullptr)
joshualitt8072caa2015-02-12 14:20:52 -0800153 , fType(kFloat_GrVertexAttribType)
154 , fOffset(0) {}
senorblancof2539d52015-05-20 14:03:42 -0700155 Attribute(const char* name, GrVertexAttribType type,
156 GrSLPrecision precision = kDefault_GrSLPrecision)
joshualitt8072caa2015-02-12 14:20:52 -0800157 : fName(name)
158 , fType(type)
senorblancof2539d52015-05-20 14:03:42 -0700159 , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
160 , fPrecision(precision) {}
joshualitt8072caa2015-02-12 14:20:52 -0800161 const char* fName;
162 GrVertexAttribType fType;
163 size_t fOffset;
senorblancof2539d52015-05-20 14:03:42 -0700164 GrSLPrecision fPrecision;
joshualitt8072caa2015-02-12 14:20:52 -0800165 };
166
167 int numAttribs() const { return fNumAttribs; }
168 const Attribute& getAttrib(int index) const {
169 SkASSERT(index < fNumAttribs);
170 return fAttribs[index];
171 }
172
173 // Returns the vertex stride of the GP. A common use case is to request geometry from a
174 // drawtarget based off of the stride, and to populate this memory using an implicit array of
175 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
176 size_t getVertexStride() const { return fVertexStride; }
177
178 /**
wangyixa7f4c432015-08-20 07:25:02 -0700179 * Computes a transformKey from an array of coord transforms. Will only look at the first
180 * <numCoords> transforms in the array.
181 *
182 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800183 */
wangyixa7f4c432015-08-20 07:25:02 -0700184 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
185 int numCoords) const;
joshualitt8072caa2015-02-12 14:20:52 -0800186
187 /**
188 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
189 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -0700190 *
191 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800192 */
egdaniel57d3b032015-11-13 11:57:27 -0800193 virtual void getGLSLProcessorKey(const GrGLSLCaps& caps,
194 GrProcessorKeyBuilder* b) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800195
196
197 /** Returns a new instance of the appropriate *GL* implementation class
198 for the given GrProcessor; caller is responsible for deleting
199 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800200 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps& caps) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800201
ethannicholas5366a092016-01-22 09:45:47 -0800202 bool isPathRendering() const { return fIsPathRendering; }
joshualitt8072caa2015-02-12 14:20:52 -0800203
joshualittb2aa7cb2015-08-05 11:05:22 -0700204 /**
205 * No Local Coord Transformation is needed in the shader, instead transformed local coords will
206 * be provided via vertex attribute.
207 */
208 virtual bool hasTransformedLocalCoords() const = 0;
209
joshualitt8072caa2015-02-12 14:20:52 -0800210protected:
ethannicholas5366a092016-01-22 09:45:47 -0800211 GrPrimitiveProcessor(bool isPathRendering)
joshualitt8072caa2015-02-12 14:20:52 -0800212 : fNumAttribs(0)
ethannicholas5366a092016-01-22 09:45:47 -0800213 , fVertexStride(0)
214 , fIsPathRendering(isPathRendering) {}
joshualitt8072caa2015-02-12 14:20:52 -0800215
joshualitt8072caa2015-02-12 14:20:52 -0800216 Attribute fAttribs[kMaxVertexAttribs];
217 int fNumAttribs;
218 size_t fVertexStride;
219
220private:
bsalomon42048002015-08-27 16:43:48 -0700221 void notifyRefCntIsZero() const final {};
joshualitt8072caa2015-02-12 14:20:52 -0800222 virtual bool hasExplicitLocalCoords() const = 0;
223
ethannicholas5366a092016-01-22 09:45:47 -0800224 bool fIsPathRendering;
225
joshualitt8072caa2015-02-12 14:20:52 -0800226 typedef GrProcessor INHERITED;
227};
228
229#endif