blob: 9f726d242bf6453595cabcf232a868cca9d0b299 [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
Brian Salomon09d994e2016-12-21 11:14:46 -050021 * functionality.
joshualitt8072caa2015-02-12 14:20:52 -080022 *
23 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
Brian Salomoncb30bb22017-02-12 09:28:54 -050024 * GrPrimitiveProcessor. These loops run on the CPU and to determine known properties of the final
25 * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve
26 * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its
27 * getPipelineAnalysisInput implementation. These seed values are processed by the subsequent
Brian Salomon92aee3d2016-12-21 09:20:25 -050028 * stages of the rendering pipeline and the output is then fed back into the GrDrawOp in
29 * the applyPipelineOptimizations call, where the op can use the information to inform decisions
30 * about GrPrimitiveProcessor creation.
joshualitt8072caa2015-02-12 14:20:52 -080031 */
32
egdaniele659a582015-11-13 09:55:43 -080033class GrGLSLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080034
35struct GrInitInvariantOutput;
36
halcanary9d524f22016-03-29 09:03:52 -070037// Describes the state of pixel local storage with respect to the current draw.
ethannicholas22793252016-01-30 09:59:10 -080038enum GrPixelLocalStorageState {
39 // The draw is actively updating PLS.
40 kDraw_GrPixelLocalStorageState,
41 // The draw is a "finish" operation which is reading from PLS and writing color.
42 kFinish_GrPixelLocalStorageState,
43 // The draw does not use PLS.
44 kDisabled_GrPixelLocalStorageState
45};
46
joshualitt8072caa2015-02-12 14:20:52 -080047/*
Brian Salomon92aee3d2016-12-21 09:20:25 -050048 * This class allows the GrPipeline to communicate information about the pipeline to a GrOp which
49 * inform its decisions for GrPrimitiveProcessor setup. These are not properly part of the pipeline
50 * because they reflect the specific inputs that the op provided to perform the analysis (e.g. that
51 * the GrGeometryProcessor would output an opaque color).
52 *
53 * The pipeline analysis that produced this may have decided to elide some GrProcessors. However,
54 * those elisions may depend upon changing the color output by the GrGeometryProcessor used by the
55 * GrDrawOp. The op must check getOverrideColorIfSet() for this.
joshualitt8072caa2015-02-12 14:20:52 -080056 */
Brian Salomon92aee3d2016-12-21 09:20:25 -050057class GrPipelineOptimizations {
bsalomon7765a472015-07-08 11:26:37 -070058public:
bsalomon7765a472015-07-08 11:26:37 -070059 /** Does the pipeline require access to (implicit or explicit) local coordinates? */
60 bool readsLocalCoords() const {
bsalomonc6998732015-08-10 12:01:15 -070061 return SkToBool(kReadsLocalCoords_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070062 }
63
64 /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one
65 color output ? */
66 bool canTweakAlphaForCoverage() const {
bsalomonc6998732015-08-10 12:01:15 -070067 return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070068 }
69
70 /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if
71 so get the color)? */
72 bool getOverrideColorIfSet(GrColor* overrideColor) const {
bsalomonc6998732015-08-10 12:01:15 -070073 if (SkToBool(kUseOverrideColor_Flag & fFlags)) {
bsalomon7765a472015-07-08 11:26:37 -070074 if (overrideColor) {
75 *overrideColor = fOverrideColor;
76 }
77 return true;
78 }
79 return false;
80 }
81
bsalomon2d563032015-08-13 07:08:31 -070082 /**
83 * Returns true if the pipeline's color output will be affected by the existing render target
84 * destination pixel values (meaning we need to be careful with overlapping draws). Note that we
85 * can conflate coverage and color, so the destination color may still bleed into pixels that
86 * have partial coverage, even if this function returns false.
87 *
Brian Salomon92aee3d2016-12-21 09:20:25 -050088 * The above comment seems incorrect for the use case. This function is used to turn two
bsalomon2d563032015-08-13 07:08:31 -070089 * overlapping draws into a single draw (really to stencil multiple paths and do a single
90 * cover). It seems that what really matters is whether the dst is read for color OR for
91 * coverage.
92 */
93 bool willColorBlendWithDst() const { return SkToBool(kWillColorBlendWithDst_Flag & fFlags); }
94
bsalomon7765a472015-07-08 11:26:37 -070095private:
96 enum {
bsalomon7765a472015-07-08 11:26:37 -070097 // If this is not set the primitive processor need not produce local coordinates
Brian Salomonbfd51832017-01-04 13:22:08 -050098 kReadsLocalCoords_Flag = 0x1,
bsalomon7765a472015-07-08 11:26:37 -070099
100 // If this flag is set then the primitive processor may produce color*coverage as
101 // its color output (and not output a separate coverage).
Brian Salomonbfd51832017-01-04 13:22:08 -0500102 kCanTweakAlphaForCoverage_Flag = 0x2,
bsalomon7765a472015-07-08 11:26:37 -0700103
104 // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its
105 // output color. If not set fOverrideColor is to be ignored.
Brian Salomonbfd51832017-01-04 13:22:08 -0500106 kUseOverrideColor_Flag = 0x4,
bsalomon2d563032015-08-13 07:08:31 -0700107
Brian Salomonbfd51832017-01-04 13:22:08 -0500108 kWillColorBlendWithDst_Flag = 0x8,
bsalomon7765a472015-07-08 11:26:37 -0700109 };
110
111 uint32_t fFlags;
112 GrColor fOverrideColor;
113
114 friend class GrPipeline; // To initialize this
joshualitt8072caa2015-02-12 14:20:52 -0800115};
116
117/*
joshualitt8072caa2015-02-12 14:20:52 -0800118 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
119 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
120 * pipelines, and they must provide some notion of equality
121 */
122class GrPrimitiveProcessor : public GrProcessor {
123public:
joshualitt8072caa2015-02-12 14:20:52 -0800124 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
125 // we put these calls on the base class to prevent having to cast
126 virtual bool willUseGeoShader() const = 0;
127
joshualitt8072caa2015-02-12 14:20:52 -0800128 struct Attribute {
129 Attribute()
halcanary96fcdcc2015-08-27 07:41:13 -0700130 : fName(nullptr)
joshualitt8072caa2015-02-12 14:20:52 -0800131 , fType(kFloat_GrVertexAttribType)
132 , fOffset(0) {}
bsalomon6cb807b2016-08-17 11:33:39 -0700133 Attribute(const char* name, GrVertexAttribType type, GrSLPrecision precision)
joshualitt8072caa2015-02-12 14:20:52 -0800134 : fName(name)
135 , fType(type)
senorblancof2539d52015-05-20 14:03:42 -0700136 , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
137 , fPrecision(precision) {}
joshualitt8072caa2015-02-12 14:20:52 -0800138 const char* fName;
139 GrVertexAttribType fType;
140 size_t fOffset;
senorblancof2539d52015-05-20 14:03:42 -0700141 GrSLPrecision fPrecision;
joshualitt8072caa2015-02-12 14:20:52 -0800142 };
143
bsalomon7dbd45d2016-03-23 10:40:53 -0700144 int numAttribs() const { return fAttribs.count(); }
145 const Attribute& getAttrib(int index) const { return fAttribs[index]; }
joshualitt8072caa2015-02-12 14:20:52 -0800146
147 // Returns the vertex stride of the GP. A common use case is to request geometry from a
Robert Phillipsf2361d22016-10-25 14:20:06 -0400148 // GrOpList based off of the stride, and to populate this memory using an implicit array of
joshualitt8072caa2015-02-12 14:20:52 -0800149 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
150 size_t getVertexStride() const { return fVertexStride; }
151
152 /**
wangyixa7f4c432015-08-20 07:25:02 -0700153 * Computes a transformKey from an array of coord transforms. Will only look at the first
154 * <numCoords> transforms in the array.
155 *
156 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800157 */
wangyixa7f4c432015-08-20 07:25:02 -0700158 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
159 int numCoords) const;
joshualitt8072caa2015-02-12 14:20:52 -0800160
161 /**
162 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
163 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -0700164 *
165 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800166 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500167 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800168
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. */
Brian Salomon94efbf52016-11-29 13:43:05 -0500173 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800174
ethannicholas22793252016-01-30 09:59:10 -0800175 virtual bool isPathRendering() const { return false; }
joshualitt8072caa2015-02-12 14:20:52 -0800176
halcanary9d524f22016-03-29 09:03:52 -0700177 virtual GrPixelLocalStorageState getPixelLocalStorageState() const {
ethannicholas22793252016-01-30 09:59:10 -0800178 return kDisabled_GrPixelLocalStorageState;
179 }
180
181 /**
182 * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor().
183 */
184 virtual const char* getDestColorOverride() const { return nullptr; }
halcanary9d524f22016-03-29 09:03:52 -0700185
ethannicholas28ef4452016-03-25 09:26:03 -0700186 virtual float getSampleShading() const {
187 return 0.0;
188 }
189
dvonbeck9b03e7b2016-08-01 11:01:56 -0700190 /* Sub-class should override and return true if this primitive processor implements the distance
191 * vector field, a field of vectors to the nearest point in the edge of the shape. */
192 virtual bool implementsDistanceVector() const { return false; }
193
joshualitt8072caa2015-02-12 14:20:52 -0800194protected:
bsalomon7dbd45d2016-03-23 10:40:53 -0700195 GrPrimitiveProcessor() : fVertexStride(0) {}
joshualitt8072caa2015-02-12 14:20:52 -0800196
bsalomon7dbd45d2016-03-23 10:40:53 -0700197 enum { kPreallocAttribCnt = 8 };
198 SkSTArray<kPreallocAttribCnt, Attribute> fAttribs;
joshualitt8072caa2015-02-12 14:20:52 -0800199 size_t fVertexStride;
200
201private:
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400202 void notifyRefCntIsZero() const final {}
joshualitt8072caa2015-02-12 14:20:52 -0800203 virtual bool hasExplicitLocalCoords() const = 0;
204
joshualitt8072caa2015-02-12 14:20:52 -0800205 typedef GrProcessor INHERITED;
206};
207
208#endif