joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 1 | /* |
| 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 |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame^] | 28 | * 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. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 33 | class GrGLSLPrimitiveProcessor; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 34 | |
| 35 | struct GrInitInvariantOutput; |
| 36 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 37 | // Describes the state of pixel local storage with respect to the current draw. |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 38 | enum 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 47 | /* |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame^] | 48 | * 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. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 56 | */ |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame^] | 57 | class GrPipelineOptimizations { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 58 | public: |
| 59 | /** Does the pipeline require the GrPrimitiveProcessor's color? */ |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 60 | bool readsColor() const { return SkToBool(kReadsColor_Flag & fFlags); } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 61 | |
| 62 | /** Does the pipeline require the GrPrimitiveProcessor's coverage? */ |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 63 | bool readsCoverage() const { return |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 64 | SkToBool(kReadsCoverage_Flag & fFlags); } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 65 | |
| 66 | /** Does the pipeline require access to (implicit or explicit) local coordinates? */ |
| 67 | bool readsLocalCoords() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 68 | return SkToBool(kReadsLocalCoords_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one |
| 72 | color output ? */ |
| 73 | bool canTweakAlphaForCoverage() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 74 | return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if |
| 78 | so get the color)? */ |
| 79 | bool getOverrideColorIfSet(GrColor* overrideColor) const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 80 | if (SkToBool(kUseOverrideColor_Flag & fFlags)) { |
| 81 | SkASSERT(SkToBool(kReadsColor_Flag & fFlags)); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 82 | if (overrideColor) { |
| 83 | *overrideColor = fOverrideColor; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Returns true if the pipeline's color output will be affected by the existing render target |
| 92 | * destination pixel values (meaning we need to be careful with overlapping draws). Note that we |
| 93 | * can conflate coverage and color, so the destination color may still bleed into pixels that |
| 94 | * have partial coverage, even if this function returns false. |
| 95 | * |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame^] | 96 | * The above comment seems incorrect for the use case. This function is used to turn two |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 97 | * overlapping draws into a single draw (really to stencil multiple paths and do a single |
| 98 | * cover). It seems that what really matters is whether the dst is read for color OR for |
| 99 | * coverage. |
| 100 | */ |
| 101 | bool willColorBlendWithDst() const { return SkToBool(kWillColorBlendWithDst_Flag & fFlags); } |
| 102 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 103 | private: |
| 104 | enum { |
| 105 | // If this is not set the primitive processor need not produce a color output |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 106 | kReadsColor_Flag = 0x1, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 107 | |
| 108 | // If this is not set the primitive processor need not produce a coverage output |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 109 | kReadsCoverage_Flag = 0x2, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 110 | |
| 111 | // If this is not set the primitive processor need not produce local coordinates |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 112 | kReadsLocalCoords_Flag = 0x4, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 113 | |
| 114 | // If this flag is set then the primitive processor may produce color*coverage as |
| 115 | // its color output (and not output a separate coverage). |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 116 | kCanTweakAlphaForCoverage_Flag = 0x8, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 117 | |
| 118 | // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its |
| 119 | // output color. If not set fOverrideColor is to be ignored. |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 120 | kUseOverrideColor_Flag = 0x10, |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 121 | |
| 122 | kWillColorBlendWithDst_Flag = 0x20, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | uint32_t fFlags; |
| 126 | GrColor fOverrideColor; |
| 127 | |
| 128 | friend class GrPipeline; // To initialize this |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /* |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 132 | * GrPrimitiveProcessor defines an interface which all subclasses must implement. All |
| 133 | * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage |
| 134 | * pipelines, and they must provide some notion of equality |
| 135 | */ |
| 136 | class GrPrimitiveProcessor : public GrProcessor { |
| 137 | public: |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 138 | // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but |
| 139 | // we put these calls on the base class to prevent having to cast |
| 140 | virtual bool willUseGeoShader() const = 0; |
| 141 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 142 | struct Attribute { |
| 143 | Attribute() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 144 | : fName(nullptr) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 145 | , fType(kFloat_GrVertexAttribType) |
| 146 | , fOffset(0) {} |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 147 | Attribute(const char* name, GrVertexAttribType type, GrSLPrecision precision) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 148 | : fName(name) |
| 149 | , fType(type) |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 150 | , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) |
| 151 | , fPrecision(precision) {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 152 | const char* fName; |
| 153 | GrVertexAttribType fType; |
| 154 | size_t fOffset; |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 155 | GrSLPrecision fPrecision; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 158 | int numAttribs() const { return fAttribs.count(); } |
| 159 | const Attribute& getAttrib(int index) const { return fAttribs[index]; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 160 | |
| 161 | // Returns the vertex stride of the GP. A common use case is to request geometry from a |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 162 | // GrOpList based off of the stride, and to populate this memory using an implicit array of |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 163 | // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct). |
| 164 | size_t getVertexStride() const { return fVertexStride; } |
| 165 | |
| 166 | /** |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 167 | * Computes a transformKey from an array of coord transforms. Will only look at the first |
| 168 | * <numCoords> transforms in the array. |
| 169 | * |
| 170 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 171 | */ |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 172 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 173 | int numCoords) const; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 177 | * processor's GL backend implementation. |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 178 | * |
| 179 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 180 | */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 181 | virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 182 | |
| 183 | |
| 184 | /** Returns a new instance of the appropriate *GL* implementation class |
| 185 | for the given GrProcessor; caller is responsible for deleting |
| 186 | the object. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 187 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 188 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 189 | virtual bool isPathRendering() const { return false; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 190 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 191 | virtual GrPixelLocalStorageState getPixelLocalStorageState() const { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 192 | return kDisabled_GrPixelLocalStorageState; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor(). |
| 197 | */ |
| 198 | virtual const char* getDestColorOverride() const { return nullptr; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 199 | |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 200 | virtual float getSampleShading() const { |
| 201 | return 0.0; |
| 202 | } |
| 203 | |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 204 | /* Sub-class should override and return true if this primitive processor implements the distance |
| 205 | * vector field, a field of vectors to the nearest point in the edge of the shape. */ |
| 206 | virtual bool implementsDistanceVector() const { return false; } |
| 207 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 208 | protected: |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 209 | GrPrimitiveProcessor() : fVertexStride(0) {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 210 | |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 211 | enum { kPreallocAttribCnt = 8 }; |
| 212 | SkSTArray<kPreallocAttribCnt, Attribute> fAttribs; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 213 | size_t fVertexStride; |
| 214 | |
| 215 | private: |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 216 | void notifyRefCntIsZero() const final {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 217 | virtual bool hasExplicitLocalCoords() const = 0; |
| 218 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 219 | typedef GrProcessor INHERITED; |
| 220 | }; |
| 221 | |
| 222 | #endif |