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 |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 21 | * functionality. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 22 | * |
| 23 | * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the |
Brian Salomon | cb30bb2 | 2017-02-12 09:28:54 -0500 | [diff] [blame] | 24 | * 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 Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame^] | 27 | * getFragmentProcessorAnalysisInputs implementation. These seed values are processed by the |
| 28 | * subsequent |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 29 | * 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. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 32 | */ |
| 33 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 34 | class GrGLSLPrimitiveProcessor; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 35 | |
| 36 | struct GrInitInvariantOutput; |
| 37 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 38 | // Describes the state of pixel local storage with respect to the current draw. |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 39 | enum 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 48 | /* |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 49 | * 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. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 57 | */ |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 58 | class GrPipelineOptimizations { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 59 | public: |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 60 | /** Does the pipeline require access to (implicit or explicit) local coordinates? */ |
| 61 | bool readsLocalCoords() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 62 | return SkToBool(kReadsLocalCoords_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one |
| 66 | color output ? */ |
| 67 | bool canTweakAlphaForCoverage() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 68 | return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 69 | } |
| 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 { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 74 | if (SkToBool(kUseOverrideColor_Flag & fFlags)) { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 75 | if (overrideColor) { |
| 76 | *overrideColor = fOverrideColor; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 83 | /** |
Brian Salomon | 9a51498 | 2017-02-14 10:28:22 -0500 | [diff] [blame] | 84 | * Returns true if the color written to the output pixel depends on the pixels previous value. |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 85 | */ |
Brian Salomon | 9a51498 | 2017-02-14 10:28:22 -0500 | [diff] [blame] | 86 | bool xpReadsDst() const { return SkToBool(kXPReadsDst_Flag & fFlags); } |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 87 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 88 | private: |
| 89 | enum { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 90 | // If this is not set the primitive processor need not produce local coordinates |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 91 | kReadsLocalCoords_Flag = 0x1, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 92 | |
| 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 Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 95 | kCanTweakAlphaForCoverage_Flag = 0x2, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 96 | |
| 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 Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 99 | kUseOverrideColor_Flag = 0x4, |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 100 | |
Brian Salomon | 9a51498 | 2017-02-14 10:28:22 -0500 | [diff] [blame] | 101 | kXPReadsDst_Flag = 0x8, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | uint32_t fFlags; |
| 105 | GrColor fOverrideColor; |
| 106 | |
| 107 | friend class GrPipeline; // To initialize this |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /* |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 111 | * 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 | */ |
| 115 | class GrPrimitiveProcessor : public GrProcessor { |
| 116 | public: |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 117 | // 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 121 | struct Attribute { |
| 122 | Attribute() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | : fName(nullptr) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 124 | , fType(kFloat_GrVertexAttribType) |
| 125 | , fOffset(0) {} |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 126 | Attribute(const char* name, GrVertexAttribType type, GrSLPrecision precision) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 127 | : fName(name) |
| 128 | , fType(type) |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 129 | , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) |
| 130 | , fPrecision(precision) {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 131 | const char* fName; |
| 132 | GrVertexAttribType fType; |
| 133 | size_t fOffset; |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 134 | GrSLPrecision fPrecision; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 137 | int numAttribs() const { return fAttribs.count(); } |
| 138 | const Attribute& getAttrib(int index) const { return fAttribs[index]; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 139 | |
| 140 | // 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] | 141 | // 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] | 142 | // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct). |
| 143 | size_t getVertexStride() const { return fVertexStride; } |
| 144 | |
| 145 | /** |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 146 | * 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". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 150 | */ |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 151 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 152 | int numCoords) const; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 156 | * processor's GL backend implementation. |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 157 | * |
| 158 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 159 | */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 160 | virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 161 | |
| 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 Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 166 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 167 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 168 | virtual bool isPathRendering() const { return false; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 169 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 170 | virtual GrPixelLocalStorageState getPixelLocalStorageState() const { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 171 | 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; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 178 | |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 179 | virtual float getSampleShading() const { |
| 180 | return 0.0; |
| 181 | } |
| 182 | |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 183 | /* 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 187 | protected: |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 188 | GrPrimitiveProcessor() : fVertexStride(0) {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 189 | |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 190 | enum { kPreallocAttribCnt = 8 }; |
| 191 | SkSTArray<kPreallocAttribCnt, Attribute> fAttribs; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 192 | size_t fVertexStride; |
| 193 | |
| 194 | private: |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 195 | void notifyRefCntIsZero() const final {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 196 | virtual bool hasExplicitLocalCoords() const = 0; |
| 197 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 198 | typedef GrProcessor INHERITED; |
| 199 | }; |
| 200 | |
| 201 | #endif |