blob: b9fe7b2c8bc2b57ce63c7cbde63e2ac4bd829e61 [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
Brian Salomon5298dc82017-02-22 11:52:03 -050027 * getFragmentProcessorAnalysisInputs implementation. These seed values are processed by the
28 * subsequent
Brian Salomon92aee3d2016-12-21 09:20:25 -050029 * stages of the rendering pipeline and the output is then fed back into the GrDrawOp in
30 * the applyPipelineOptimizations call, where the op can use the information to inform decisions
31 * about GrPrimitiveProcessor creation.
joshualitt8072caa2015-02-12 14:20:52 -080032 */
33
egdaniele659a582015-11-13 09:55:43 -080034class GrGLSLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080035
36struct GrInitInvariantOutput;
37
halcanary9d524f22016-03-29 09:03:52 -070038// Describes the state of pixel local storage with respect to the current draw.
ethannicholas22793252016-01-30 09:59:10 -080039enum GrPixelLocalStorageState {
40 // The draw is actively updating PLS.
41 kDraw_GrPixelLocalStorageState,
42 // The draw is a "finish" operation which is reading from PLS and writing color.
43 kFinish_GrPixelLocalStorageState,
44 // The draw does not use PLS.
45 kDisabled_GrPixelLocalStorageState
46};
47
joshualitt8072caa2015-02-12 14:20:52 -080048/*
Brian Salomon92aee3d2016-12-21 09:20:25 -050049 * This class allows the GrPipeline to communicate information about the pipeline to a GrOp which
50 * inform its decisions for GrPrimitiveProcessor setup. These are not properly part of the pipeline
51 * because they reflect the specific inputs that the op provided to perform the analysis (e.g. that
52 * the GrGeometryProcessor would output an opaque color).
53 *
54 * The pipeline analysis that produced this may have decided to elide some GrProcessors. However,
55 * those elisions may depend upon changing the color output by the GrGeometryProcessor used by the
56 * GrDrawOp. The op must check getOverrideColorIfSet() for this.
joshualitt8072caa2015-02-12 14:20:52 -080057 */
Brian Salomon92aee3d2016-12-21 09:20:25 -050058class GrPipelineOptimizations {
bsalomon7765a472015-07-08 11:26:37 -070059public:
bsalomon7765a472015-07-08 11:26:37 -070060 /** Does the pipeline require access to (implicit or explicit) local coordinates? */
61 bool readsLocalCoords() const {
bsalomonc6998732015-08-10 12:01:15 -070062 return SkToBool(kReadsLocalCoords_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070063 }
64
65 /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one
66 color output ? */
67 bool canTweakAlphaForCoverage() const {
bsalomonc6998732015-08-10 12:01:15 -070068 return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags);
bsalomon7765a472015-07-08 11:26:37 -070069 }
70
71 /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if
72 so get the color)? */
73 bool getOverrideColorIfSet(GrColor* overrideColor) const {
bsalomonc6998732015-08-10 12:01:15 -070074 if (SkToBool(kUseOverrideColor_Flag & fFlags)) {
bsalomon7765a472015-07-08 11:26:37 -070075 if (overrideColor) {
76 *overrideColor = fOverrideColor;
77 }
78 return true;
79 }
80 return false;
81 }
82
bsalomon2d563032015-08-13 07:08:31 -070083 /**
Brian Salomon9a514982017-02-14 10:28:22 -050084 * Returns true if the color written to the output pixel depends on the pixels previous value.
bsalomon2d563032015-08-13 07:08:31 -070085 */
Brian Salomon9a514982017-02-14 10:28:22 -050086 bool xpReadsDst() const { return SkToBool(kXPReadsDst_Flag & fFlags); }
bsalomon2d563032015-08-13 07:08:31 -070087
bsalomon7765a472015-07-08 11:26:37 -070088private:
89 enum {
bsalomon7765a472015-07-08 11:26:37 -070090 // If this is not set the primitive processor need not produce local coordinates
Brian Salomonbfd51832017-01-04 13:22:08 -050091 kReadsLocalCoords_Flag = 0x1,
bsalomon7765a472015-07-08 11:26:37 -070092
93 // If this flag is set then the primitive processor may produce color*coverage as
94 // its color output (and not output a separate coverage).
Brian Salomonbfd51832017-01-04 13:22:08 -050095 kCanTweakAlphaForCoverage_Flag = 0x2,
bsalomon7765a472015-07-08 11:26:37 -070096
97 // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its
98 // output color. If not set fOverrideColor is to be ignored.
Brian Salomonbfd51832017-01-04 13:22:08 -050099 kUseOverrideColor_Flag = 0x4,
bsalomon2d563032015-08-13 07:08:31 -0700100
Brian Salomon9a514982017-02-14 10:28:22 -0500101 kXPReadsDst_Flag = 0x8,
bsalomon7765a472015-07-08 11:26:37 -0700102 };
103
104 uint32_t fFlags;
105 GrColor fOverrideColor;
106
107 friend class GrPipeline; // To initialize this
joshualitt8072caa2015-02-12 14:20:52 -0800108};
109
110/*
joshualitt8072caa2015-02-12 14:20:52 -0800111 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
112 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
113 * pipelines, and they must provide some notion of equality
114 */
115class GrPrimitiveProcessor : public GrProcessor {
116public:
joshualitt8072caa2015-02-12 14:20:52 -0800117 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
118 // we put these calls on the base class to prevent having to cast
119 virtual bool willUseGeoShader() const = 0;
120
joshualitt8072caa2015-02-12 14:20:52 -0800121 struct Attribute {
122 Attribute()
halcanary96fcdcc2015-08-27 07:41:13 -0700123 : fName(nullptr)
joshualitt8072caa2015-02-12 14:20:52 -0800124 , fType(kFloat_GrVertexAttribType)
125 , fOffset(0) {}
bsalomon6cb807b2016-08-17 11:33:39 -0700126 Attribute(const char* name, GrVertexAttribType type, GrSLPrecision precision)
joshualitt8072caa2015-02-12 14:20:52 -0800127 : fName(name)
128 , fType(type)
senorblancof2539d52015-05-20 14:03:42 -0700129 , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
130 , fPrecision(precision) {}
joshualitt8072caa2015-02-12 14:20:52 -0800131 const char* fName;
132 GrVertexAttribType fType;
133 size_t fOffset;
senorblancof2539d52015-05-20 14:03:42 -0700134 GrSLPrecision fPrecision;
joshualitt8072caa2015-02-12 14:20:52 -0800135 };
136
bsalomon7dbd45d2016-03-23 10:40:53 -0700137 int numAttribs() const { return fAttribs.count(); }
138 const Attribute& getAttrib(int index) const { return fAttribs[index]; }
joshualitt8072caa2015-02-12 14:20:52 -0800139
140 // Returns the vertex stride of the GP. A common use case is to request geometry from a
Robert Phillipsf2361d22016-10-25 14:20:06 -0400141 // GrOpList based off of the stride, and to populate this memory using an implicit array of
joshualitt8072caa2015-02-12 14:20:52 -0800142 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
143 size_t getVertexStride() const { return fVertexStride; }
144
145 /**
wangyixa7f4c432015-08-20 07:25:02 -0700146 * Computes a transformKey from an array of coord transforms. Will only look at the first
147 * <numCoords> transforms in the array.
148 *
149 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800150 */
wangyixa7f4c432015-08-20 07:25:02 -0700151 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
152 int numCoords) const;
joshualitt8072caa2015-02-12 14:20:52 -0800153
154 /**
155 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
156 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -0700157 *
158 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -0800159 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500160 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800161
162
163 /** Returns a new instance of the appropriate *GL* implementation class
164 for the given GrProcessor; caller is responsible for deleting
165 the object. */
Brian Salomon94efbf52016-11-29 13:43:05 -0500166 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -0800167
ethannicholas22793252016-01-30 09:59:10 -0800168 virtual bool isPathRendering() const { return false; }
joshualitt8072caa2015-02-12 14:20:52 -0800169
halcanary9d524f22016-03-29 09:03:52 -0700170 virtual GrPixelLocalStorageState getPixelLocalStorageState() const {
ethannicholas22793252016-01-30 09:59:10 -0800171 return kDisabled_GrPixelLocalStorageState;
172 }
173
174 /**
175 * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor().
176 */
177 virtual const char* getDestColorOverride() const { return nullptr; }
halcanary9d524f22016-03-29 09:03:52 -0700178
ethannicholas28ef4452016-03-25 09:26:03 -0700179 virtual float getSampleShading() const {
180 return 0.0;
181 }
182
dvonbeck9b03e7b2016-08-01 11:01:56 -0700183 /* Sub-class should override and return true if this primitive processor implements the distance
184 * vector field, a field of vectors to the nearest point in the edge of the shape. */
185 virtual bool implementsDistanceVector() const { return false; }
186
joshualitt8072caa2015-02-12 14:20:52 -0800187protected:
bsalomon7dbd45d2016-03-23 10:40:53 -0700188 GrPrimitiveProcessor() : fVertexStride(0) {}
joshualitt8072caa2015-02-12 14:20:52 -0800189
bsalomon7dbd45d2016-03-23 10:40:53 -0700190 enum { kPreallocAttribCnt = 8 };
191 SkSTArray<kPreallocAttribCnt, Attribute> fAttribs;
joshualitt8072caa2015-02-12 14:20:52 -0800192 size_t fVertexStride;
193
194private:
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400195 void notifyRefCntIsZero() const final {}
joshualitt8072caa2015-02-12 14:20:52 -0800196 virtual bool hasExplicitLocalCoords() const = 0;
197
joshualitt8072caa2015-02-12 14:20:52 -0800198 typedef GrProcessor INHERITED;
199};
200
201#endif