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 | |
| 69 | class GrIndexBufferAllocPool; |
| 70 | class GrGLCaps; |
| 71 | class GrGLPrimitiveProcessor; |
| 72 | class GrVertexBufferAllocPool; |
| 73 | |
| 74 | struct GrInitInvariantOutput; |
| 75 | |
| 76 | /* |
| 77 | * This struct allows the GrPipeline to communicate information about the pipeline. Most of this |
| 78 | * is overrides, but some of it is general information. Logically it should live in GrPipeline.h, |
| 79 | * but this is problematic due to circular dependencies. |
| 80 | */ |
| 81 | struct GrPipelineInfo { |
| 82 | bool fColorIgnored; |
| 83 | bool fCoverageIgnored; |
| 84 | GrColor fOverrideColor; |
| 85 | bool fUsesLocalCoords; |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders |
| 90 | * with vertex attributes / uniforms. |
| 91 | */ |
| 92 | enum GrGPInput { |
| 93 | kAllOnes_GrGPInput, |
| 94 | kAttribute_GrGPInput, |
| 95 | kUniform_GrGPInput, |
| 96 | kIgnored_GrGPInput, |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * GrPrimitiveProcessor defines an interface which all subclasses must implement. All |
| 101 | * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage |
| 102 | * pipelines, and they must provide some notion of equality |
| 103 | */ |
| 104 | class GrPrimitiveProcessor : public GrProcessor { |
| 105 | public: |
| 106 | // TODO let the PrimProc itself set this in its setData call, this should really live on the |
| 107 | // bundle of primitive data |
| 108 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 109 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
| 110 | |
| 111 | virtual void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const = 0; |
| 112 | |
| 113 | virtual bool canMakeEqual(const GrBatchTracker& mine, |
| 114 | const GrPrimitiveProcessor& that, |
| 115 | const GrBatchTracker& theirs) const = 0; |
| 116 | |
| 117 | virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0; |
| 118 | virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0; |
| 119 | |
| 120 | // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but |
| 121 | // we put these calls on the base class to prevent having to cast |
| 122 | virtual bool willUseGeoShader() const = 0; |
| 123 | |
| 124 | /* |
| 125 | * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific |
| 126 | * attribute limits. This number can almost certainly be raised if required. |
| 127 | */ |
| 128 | static const int kMaxVertexAttribs = 6; |
| 129 | |
| 130 | struct Attribute { |
| 131 | Attribute() |
| 132 | : fName(NULL) |
| 133 | , fType(kFloat_GrVertexAttribType) |
| 134 | , fOffset(0) {} |
| 135 | Attribute(const char* name, GrVertexAttribType type) |
| 136 | : fName(name) |
| 137 | , fType(type) |
| 138 | , fOffset(SkAlign4(GrVertexAttribTypeSize(type))) {} |
| 139 | const char* fName; |
| 140 | GrVertexAttribType fType; |
| 141 | size_t fOffset; |
| 142 | }; |
| 143 | |
| 144 | int numAttribs() const { return fNumAttribs; } |
| 145 | const Attribute& getAttrib(int index) const { |
| 146 | SkASSERT(index < fNumAttribs); |
| 147 | return fAttribs[index]; |
| 148 | } |
| 149 | |
| 150 | // Returns the vertex stride of the GP. A common use case is to request geometry from a |
| 151 | // drawtarget based off of the stride, and to populate this memory using an implicit array of |
| 152 | // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct). |
| 153 | size_t getVertexStride() const { return fVertexStride; } |
| 154 | |
| 155 | /** |
| 156 | * Gets a transformKey from an array of coord transforms |
| 157 | */ |
| 158 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>&) const; |
| 159 | |
| 160 | /** |
| 161 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 162 | * processor's GL backend implementation. |
| 163 | */ |
| 164 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
| 165 | const GrGLCaps& caps, |
| 166 | GrProcessorKeyBuilder* b) const = 0; |
| 167 | |
| 168 | |
| 169 | /** Returns a new instance of the appropriate *GL* implementation class |
| 170 | for the given GrProcessor; caller is responsible for deleting |
| 171 | the object. */ |
| 172 | virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt, |
| 173 | const GrGLCaps& caps) const = 0; |
| 174 | |
| 175 | bool isPathRendering() const { return fIsPathRendering; } |
| 176 | |
| 177 | protected: |
| 178 | GrPrimitiveProcessor(const SkMatrix& viewMatrix, const SkMatrix& localMatrix, |
| 179 | bool isPathRendering) |
| 180 | : fNumAttribs(0) |
| 181 | , fVertexStride(0) |
| 182 | , fViewMatrix(viewMatrix) |
| 183 | , fLocalMatrix(localMatrix) |
| 184 | , fIsPathRendering(isPathRendering) {} |
| 185 | |
| 186 | /* |
| 187 | * CanCombineOutput will return true if two draws are 'batchable' from a color perspective. |
| 188 | * TODO remove this when GPs can upgrade to attribute color |
| 189 | */ |
| 190 | static bool CanCombineOutput(GrGPInput left, GrColor lColor, GrGPInput right, GrColor rColor) { |
| 191 | if (left != right) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (kUniform_GrGPInput == left && lColor != rColor) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | static bool CanCombineLocalMatrices(const GrPrimitiveProcessor& left, |
| 203 | bool leftUsesLocalCoords, |
| 204 | const GrPrimitiveProcessor& right, |
| 205 | bool rightUsesLocalCoords) { |
| 206 | if (leftUsesLocalCoords != rightUsesLocalCoords) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | if (leftUsesLocalCoords && !left.localMatrix().cheapEqualTo(right.localMatrix())) { |
| 211 | return false; |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | Attribute fAttribs[kMaxVertexAttribs]; |
| 217 | int fNumAttribs; |
| 218 | size_t fVertexStride; |
| 219 | |
| 220 | private: |
| 221 | virtual bool hasExplicitLocalCoords() const = 0; |
| 222 | |
| 223 | const SkMatrix fViewMatrix; |
| 224 | SkMatrix fLocalMatrix; |
| 225 | bool fIsPathRendering; |
| 226 | |
| 227 | typedef GrProcessor INHERITED; |
| 228 | }; |
| 229 | |
| 230 | #endif |