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 | |
| 45 | /* |
| 46 | * A struct for tracking batching decisions. While this lives on GrOptState, it is managed |
| 47 | * entirely by the derived classes of the GP. |
| 48 | * // TODO this was an early attempt at handling out of order batching. It should be |
| 49 | * used carefully as it is being replaced by GrBatch |
| 50 | */ |
| 51 | class GrBatchTracker { |
| 52 | public: |
| 53 | template <typename T> const T& cast() const { |
| 54 | SkASSERT(sizeof(T) <= kMaxSize); |
| 55 | return *reinterpret_cast<const T*>(fData.get()); |
| 56 | } |
| 57 | |
| 58 | template <typename T> T* cast() { |
| 59 | SkASSERT(sizeof(T) <= kMaxSize); |
| 60 | return reinterpret_cast<T*>(fData.get()); |
| 61 | } |
| 62 | |
| 63 | static const size_t kMaxSize = 32; |
| 64 | |
| 65 | private: |
| 66 | SkAlignedSStorage<kMaxSize> fData; |
| 67 | }; |
| 68 | |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 69 | class GrGLSLCaps; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 70 | class GrGLPrimitiveProcessor; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 71 | |
| 72 | struct GrInitInvariantOutput; |
| 73 | |
| 74 | /* |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 75 | * This class allows the GrPipeline to communicate information about the pipeline to a |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 76 | * GrBatch which should be forwarded to the GrPrimitiveProcessor(s) created by the batch. |
| 77 | * These are not properly part of the pipeline because they assume the specific inputs |
| 78 | * that the batch provided when it created the pipeline. Identical pipelines may be |
| 79 | * created by different batches with different input assumptions and therefore different |
| 80 | * computed optimizations. It is the batch-specific optimizations that allow the pipelines |
| 81 | * to be equal. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 82 | */ |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 83 | class GrPipelineOptimizations { |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 84 | public: |
| 85 | /** Does the pipeline require the GrPrimitiveProcessor's color? */ |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 86 | bool readsColor() const { return SkToBool(kReadsColor_Flag & fFlags); } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 87 | |
| 88 | /** Does the pipeline require the GrPrimitiveProcessor's coverage? */ |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 89 | bool readsCoverage() const { return |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 90 | SkToBool(kReadsCoverage_Flag & fFlags); } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 91 | |
| 92 | /** Does the pipeline require access to (implicit or explicit) local coordinates? */ |
| 93 | bool readsLocalCoords() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 94 | return SkToBool(kReadsLocalCoords_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one |
| 98 | color output ? */ |
| 99 | bool canTweakAlphaForCoverage() const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 100 | return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if |
| 104 | so get the color)? */ |
| 105 | bool getOverrideColorIfSet(GrColor* overrideColor) const { |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 106 | if (SkToBool(kUseOverrideColor_Flag & fFlags)) { |
| 107 | SkASSERT(SkToBool(kReadsColor_Flag & fFlags)); |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 108 | if (overrideColor) { |
| 109 | *overrideColor = fOverrideColor; |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Returns true if the pipeline's color output will be affected by the existing render target |
| 118 | * destination pixel values (meaning we need to be careful with overlapping draws). Note that we |
| 119 | * can conflate coverage and color, so the destination color may still bleed into pixels that |
| 120 | * have partial coverage, even if this function returns false. |
| 121 | * |
| 122 | * The above comment seems incorrect for the use case. This funciton is used to turn two |
| 123 | * overlapping draws into a single draw (really to stencil multiple paths and do a single |
| 124 | * cover). It seems that what really matters is whether the dst is read for color OR for |
| 125 | * coverage. |
| 126 | */ |
| 127 | bool willColorBlendWithDst() const { return SkToBool(kWillColorBlendWithDst_Flag & fFlags); } |
| 128 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 129 | private: |
| 130 | enum { |
| 131 | // 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] | 132 | kReadsColor_Flag = 0x1, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 133 | |
| 134 | // 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] | 135 | kReadsCoverage_Flag = 0x2, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 136 | |
| 137 | // If this is not set the primitive processor need not produce local coordinates |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 138 | kReadsLocalCoords_Flag = 0x4, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 139 | |
| 140 | // If this flag is set then the primitive processor may produce color*coverage as |
| 141 | // its color output (and not output a separate coverage). |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 142 | kCanTweakAlphaForCoverage_Flag = 0x8, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 143 | |
| 144 | // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its |
| 145 | // output color. If not set fOverrideColor is to be ignored. |
bsalomon | c699873 | 2015-08-10 12:01:15 -0700 | [diff] [blame] | 146 | kUseOverrideColor_Flag = 0x10, |
bsalomon | 2d56303 | 2015-08-13 07:08:31 -0700 | [diff] [blame] | 147 | |
| 148 | kWillColorBlendWithDst_Flag = 0x20, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | uint32_t fFlags; |
| 152 | GrColor fOverrideColor; |
| 153 | |
| 154 | friend class GrPipeline; // To initialize this |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | /* |
| 158 | * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders |
| 159 | * with vertex attributes / uniforms. |
| 160 | */ |
| 161 | enum GrGPInput { |
| 162 | kAllOnes_GrGPInput, |
| 163 | kAttribute_GrGPInput, |
| 164 | kUniform_GrGPInput, |
| 165 | kIgnored_GrGPInput, |
| 166 | }; |
| 167 | |
| 168 | /* |
| 169 | * GrPrimitiveProcessor defines an interface which all subclasses must implement. All |
| 170 | * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage |
| 171 | * pipelines, and they must provide some notion of equality |
| 172 | */ |
| 173 | class GrPrimitiveProcessor : public GrProcessor { |
| 174 | public: |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 175 | virtual void initBatchTracker(GrBatchTracker*, const GrPipelineOptimizations&) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 176 | |
| 177 | virtual bool canMakeEqual(const GrBatchTracker& mine, |
| 178 | const GrPrimitiveProcessor& that, |
| 179 | const GrBatchTracker& theirs) const = 0; |
| 180 | |
| 181 | virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0; |
| 182 | virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0; |
| 183 | |
| 184 | // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but |
| 185 | // we put these calls on the base class to prevent having to cast |
| 186 | virtual bool willUseGeoShader() const = 0; |
| 187 | |
| 188 | /* |
| 189 | * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific |
| 190 | * attribute limits. This number can almost certainly be raised if required. |
| 191 | */ |
| 192 | static const int kMaxVertexAttribs = 6; |
| 193 | |
| 194 | struct Attribute { |
| 195 | Attribute() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | : fName(nullptr) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 197 | , fType(kFloat_GrVertexAttribType) |
| 198 | , fOffset(0) {} |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 199 | Attribute(const char* name, GrVertexAttribType type, |
| 200 | GrSLPrecision precision = kDefault_GrSLPrecision) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 201 | : fName(name) |
| 202 | , fType(type) |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 203 | , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) |
| 204 | , fPrecision(precision) {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 205 | const char* fName; |
| 206 | GrVertexAttribType fType; |
| 207 | size_t fOffset; |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 208 | GrSLPrecision fPrecision; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | int numAttribs() const { return fNumAttribs; } |
| 212 | const Attribute& getAttrib(int index) const { |
| 213 | SkASSERT(index < fNumAttribs); |
| 214 | return fAttribs[index]; |
| 215 | } |
| 216 | |
| 217 | // Returns the vertex stride of the GP. A common use case is to request geometry from a |
| 218 | // drawtarget based off of the stride, and to populate this memory using an implicit array of |
| 219 | // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct). |
| 220 | size_t getVertexStride() const { return fVertexStride; } |
| 221 | |
| 222 | /** |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 223 | * Computes a transformKey from an array of coord transforms. Will only look at the first |
| 224 | * <numCoords> transforms in the array. |
| 225 | * |
| 226 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 227 | */ |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 228 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 229 | int numCoords) const; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 233 | * processor's GL backend implementation. |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 234 | * |
| 235 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 236 | */ |
| 237 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 238 | const GrGLSLCaps& caps, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 239 | GrProcessorKeyBuilder* b) const = 0; |
| 240 | |
| 241 | |
| 242 | /** Returns a new instance of the appropriate *GL* implementation class |
| 243 | for the given GrProcessor; caller is responsible for deleting |
| 244 | the object. */ |
| 245 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 246 | const GrGLSLCaps& caps) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 247 | |
| 248 | bool isPathRendering() const { return fIsPathRendering; } |
| 249 | |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 250 | /** |
| 251 | * No Local Coord Transformation is needed in the shader, instead transformed local coords will |
| 252 | * be provided via vertex attribute. |
| 253 | */ |
| 254 | virtual bool hasTransformedLocalCoords() const = 0; |
| 255 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 256 | protected: |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 257 | GrPrimitiveProcessor(bool isPathRendering) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 258 | : fNumAttribs(0) |
| 259 | , fVertexStride(0) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 260 | , fIsPathRendering(isPathRendering) {} |
| 261 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 262 | Attribute fAttribs[kMaxVertexAttribs]; |
| 263 | int fNumAttribs; |
| 264 | size_t fVertexStride; |
| 265 | |
| 266 | private: |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame^] | 267 | void notifyRefCntIsZero() const final {}; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 268 | virtual bool hasExplicitLocalCoords() const = 0; |
| 269 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 270 | bool fIsPathRendering; |
| 271 | |
| 272 | typedef GrProcessor INHERITED; |
| 273 | }; |
| 274 | |
| 275 | #endif |