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 |
| 76 | * GrPrimitiveProcessor that will be used in conjunction with the GrPipeline. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 77 | */ |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 78 | class GrPipelineInfo { |
| 79 | public: |
| 80 | /** Does the pipeline require the GrPrimitiveProcessor's color? */ |
| 81 | bool readsColor() const { return SkToBool(kReadsColor_GrPipelineInfoFlag & fFlags); } |
| 82 | |
| 83 | /** Does the pipeline require the GrPrimitiveProcessor's coverage? */ |
| 84 | bool readsCoverage() const { return SkToBool(kReadsCoverage_GrPipelineInfoFlag & fFlags); } |
| 85 | |
| 86 | /** Does the pipeline require access to (implicit or explicit) local coordinates? */ |
| 87 | bool readsLocalCoords() const { |
| 88 | return SkToBool(kReadsLocalCoords_GrPipelineInfoFlag & fFlags); |
| 89 | } |
| 90 | |
| 91 | /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one |
| 92 | color output ? */ |
| 93 | bool canTweakAlphaForCoverage() const { |
| 94 | return SkToBool(kCanTweakAlphaForCoverage_GrPipelineInfoFlag & fFlags); |
| 95 | } |
| 96 | |
| 97 | /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if |
| 98 | so get the color)? */ |
| 99 | bool getOverrideColorIfSet(GrColor* overrideColor) const { |
| 100 | if (SkToBool(kUseOverrideColor_GrPipelineInfoFlag & fFlags)) { |
| 101 | SkASSERT(SkToBool(kReadsColor_GrPipelineInfoFlag & fFlags)); |
| 102 | if (overrideColor) { |
| 103 | *overrideColor = fOverrideColor; |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | enum { |
| 112 | // If this is not set the primitive processor need not produce a color output |
| 113 | kReadsColor_GrPipelineInfoFlag = 0x1, |
| 114 | |
| 115 | // If this is not set the primitive processor need not produce a coverage output |
| 116 | kReadsCoverage_GrPipelineInfoFlag = 0x2, |
| 117 | |
| 118 | // If this is not set the primitive processor need not produce local coordinates |
| 119 | kReadsLocalCoords_GrPipelineInfoFlag = 0x4, |
| 120 | |
| 121 | // If this flag is set then the primitive processor may produce color*coverage as |
| 122 | // its color output (and not output a separate coverage). |
| 123 | kCanTweakAlphaForCoverage_GrPipelineInfoFlag = 0x8, |
| 124 | |
| 125 | // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its |
| 126 | // output color. If not set fOverrideColor is to be ignored. |
| 127 | kUseOverrideColor_GrPipelineInfoFlag = 0x10, |
| 128 | }; |
| 129 | |
| 130 | uint32_t fFlags; |
| 131 | GrColor fOverrideColor; |
| 132 | |
| 133 | friend class GrPipeline; // To initialize this |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders |
| 138 | * with vertex attributes / uniforms. |
| 139 | */ |
| 140 | enum GrGPInput { |
| 141 | kAllOnes_GrGPInput, |
| 142 | kAttribute_GrGPInput, |
| 143 | kUniform_GrGPInput, |
| 144 | kIgnored_GrGPInput, |
| 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * GrPrimitiveProcessor defines an interface which all subclasses must implement. All |
| 149 | * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage |
| 150 | * pipelines, and they must provide some notion of equality |
| 151 | */ |
| 152 | class GrPrimitiveProcessor : public GrProcessor { |
| 153 | public: |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 154 | virtual void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const = 0; |
| 155 | |
| 156 | virtual bool canMakeEqual(const GrBatchTracker& mine, |
| 157 | const GrPrimitiveProcessor& that, |
| 158 | const GrBatchTracker& theirs) const = 0; |
| 159 | |
| 160 | virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0; |
| 161 | virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0; |
| 162 | |
| 163 | // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but |
| 164 | // we put these calls on the base class to prevent having to cast |
| 165 | virtual bool willUseGeoShader() const = 0; |
| 166 | |
| 167 | /* |
| 168 | * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific |
| 169 | * attribute limits. This number can almost certainly be raised if required. |
| 170 | */ |
| 171 | static const int kMaxVertexAttribs = 6; |
| 172 | |
| 173 | struct Attribute { |
| 174 | Attribute() |
| 175 | : fName(NULL) |
| 176 | , fType(kFloat_GrVertexAttribType) |
| 177 | , fOffset(0) {} |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 178 | Attribute(const char* name, GrVertexAttribType type, |
| 179 | GrSLPrecision precision = kDefault_GrSLPrecision) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 180 | : fName(name) |
| 181 | , fType(type) |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 182 | , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) |
| 183 | , fPrecision(precision) {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 184 | const char* fName; |
| 185 | GrVertexAttribType fType; |
| 186 | size_t fOffset; |
senorblanco | f2539d5 | 2015-05-20 14:03:42 -0700 | [diff] [blame] | 187 | GrSLPrecision fPrecision; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | int numAttribs() const { return fNumAttribs; } |
| 191 | const Attribute& getAttrib(int index) const { |
| 192 | SkASSERT(index < fNumAttribs); |
| 193 | return fAttribs[index]; |
| 194 | } |
| 195 | |
| 196 | // Returns the vertex stride of the GP. A common use case is to request geometry from a |
| 197 | // drawtarget based off of the stride, and to populate this memory using an implicit array of |
| 198 | // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct). |
| 199 | size_t getVertexStride() const { return fVertexStride; } |
| 200 | |
| 201 | /** |
| 202 | * Gets a transformKey from an array of coord transforms |
| 203 | */ |
| 204 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>&) const; |
| 205 | |
| 206 | /** |
| 207 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 208 | * processor's GL backend implementation. |
| 209 | */ |
| 210 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 211 | const GrGLSLCaps& caps, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 212 | GrProcessorKeyBuilder* b) const = 0; |
| 213 | |
| 214 | |
| 215 | /** Returns a new instance of the appropriate *GL* implementation class |
| 216 | for the given GrProcessor; caller is responsible for deleting |
| 217 | the object. */ |
| 218 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 219 | const GrGLSLCaps& caps) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 220 | |
| 221 | bool isPathRendering() const { return fIsPathRendering; } |
| 222 | |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame^] | 223 | /** |
| 224 | * No Local Coord Transformation is needed in the shader, instead transformed local coords will |
| 225 | * be provided via vertex attribute. |
| 226 | */ |
| 227 | virtual bool hasTransformedLocalCoords() const = 0; |
| 228 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 229 | protected: |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 230 | GrPrimitiveProcessor(bool isPathRendering) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 231 | : fNumAttribs(0) |
| 232 | , fVertexStride(0) |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 233 | , fIsPathRendering(isPathRendering) {} |
| 234 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 235 | Attribute fAttribs[kMaxVertexAttribs]; |
| 236 | int fNumAttribs; |
| 237 | size_t fVertexStride; |
| 238 | |
| 239 | private: |
| 240 | virtual bool hasExplicitLocalCoords() const = 0; |
| 241 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 242 | bool fIsPathRendering; |
| 243 | |
| 244 | typedef GrProcessor INHERITED; |
| 245 | }; |
| 246 | |
| 247 | #endif |