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 |
| 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 | |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 45 | class GrGLSLCaps; |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame^] | 46 | class GrGLSLPrimitiveProcessor; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 47 | |
| 48 | struct GrInitInvariantOutput; |
| 49 | |
| 50 | /* |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 51 | * This class allows the GrPipeline to communicate information about the pipeline to a |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 52 | * 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. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 58 | */ |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 59 | class GrPipelineOptimizations { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 60 | public: |
| 61 | /** Does the pipeline require the GrPrimitiveProcessor's color? */ |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 62 | bool readsColor() const { return SkToBool(kReadsColor_Flag & fFlags); } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 63 | |
| 64 | /** Does the pipeline require the GrPrimitiveProcessor's coverage? */ |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 65 | bool readsCoverage() const { return |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 66 | SkToBool(kReadsCoverage_Flag & fFlags); } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 67 | |
| 68 | /** Does the pipeline require access to (implicit or explicit) local coordinates? */ |
| 69 | bool readsLocalCoords() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 70 | return SkToBool(kReadsLocalCoords_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one |
| 74 | color output ? */ |
| 75 | bool canTweakAlphaForCoverage() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 76 | return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 77 | } |
| 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 { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 82 | if (SkToBool(kUseOverrideColor_Flag & fFlags)) { |
| 83 | SkASSERT(SkToBool(kReadsColor_Flag & fFlags)); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 84 | if (overrideColor) { |
| 85 | *overrideColor = fOverrideColor; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 92 | /** |
| 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 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 105 | private: |
| 106 | enum { |
| 107 | // 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] | 108 | kReadsColor_Flag = 0x1, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 109 | |
| 110 | // 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] | 111 | kReadsCoverage_Flag = 0x2, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 112 | |
| 113 | // If this is not set the primitive processor need not produce local coordinates |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 114 | kReadsLocalCoords_Flag = 0x4, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 115 | |
| 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). |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 118 | kCanTweakAlphaForCoverage_Flag = 0x8, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 119 | |
| 120 | // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its |
| 121 | // output color. If not set fOverrideColor is to be ignored. |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 122 | kUseOverrideColor_Flag = 0x10, |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 123 | |
| 124 | kWillColorBlendWithDst_Flag = 0x20, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | uint32_t fFlags; |
| 128 | GrColor fOverrideColor; |
| 129 | |
| 130 | friend class GrPipeline; // To initialize this |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | /* |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 134 | * 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 | */ |
| 138 | class GrPrimitiveProcessor : public GrProcessor { |
| 139 | public: |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 140 | // 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() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | : fName(nullptr) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 153 | , fType(kFloat_GrVertexAttribType) |
| 154 | , fOffset(0) {} |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 155 | Attribute(const char* name, GrVertexAttribType type, |
| 156 | GrSLPrecision precision = kDefault_GrSLPrecision) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 157 | : fName(name) |
| 158 | , fType(type) |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 159 | , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) |
| 160 | , fPrecision(precision) {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 161 | const char* fName; |
| 162 | GrVertexAttribType fType; |
| 163 | size_t fOffset; |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 164 | GrSLPrecision fPrecision; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 165 | }; |
| 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 | /** |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 179 | * 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". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 183 | */ |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 184 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 185 | int numCoords) const; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 189 | * processor's GL backend implementation. |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 190 | * |
| 191 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 192 | */ |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 193 | virtual void getGLProcessorKey(const GrGLSLCaps& caps, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 194 | GrProcessorKeyBuilder* b) const = 0; |
| 195 | |
| 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. */ |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame^] | 200 | virtual GrGLSLPrimitiveProcessor* createGLInstance(const GrGLSLCaps& caps) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 201 | |
| 202 | bool isPathRendering() const { return fIsPathRendering; } |
| 203 | |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 204 | /** |
| 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 210 | protected: |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 211 | GrPrimitiveProcessor(bool isPathRendering) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 212 | : fNumAttribs(0) |
| 213 | , fVertexStride(0) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 214 | , fIsPathRendering(isPathRendering) {} |
| 215 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 216 | Attribute fAttribs[kMaxVertexAttribs]; |
| 217 | int fNumAttribs; |
| 218 | size_t fVertexStride; |
| 219 | |
| 220 | private: |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 221 | void notifyRefCntIsZero() const final {}; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 222 | virtual bool hasExplicitLocalCoords() const = 0; |
| 223 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 224 | bool fIsPathRendering; |
| 225 | |
| 226 | typedef GrProcessor INHERITED; |
| 227 | }; |
| 228 | |
| 229 | #endif |