OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrOptDrawState_DEFINED | 8 #ifndef GrOptDrawState_DEFINED |
9 #define GrOptDrawState_DEFINED | 9 #define GrOptDrawState_DEFINED |
10 | 10 |
11 #include "GrDrawState.h" | 11 #include "GrDrawState.h" |
12 #include "GrRODrawState.h" | |
13 | 12 |
14 /** | 13 /** |
15 * Subclass of GrRODrawState that holds an optimized version of a GrDrawState. L
ike it's parent | 14 * Class that holds an optimized version of a GrDrawState. It is meant to be an
immutable class, |
16 * it is meant to be an immutable class, and simply adds a few helpful data memb
ers not in the | 15 * and contains all data needed to set the state for a gpu draw. |
17 * base class. | |
18 */ | 16 */ |
19 class GrOptDrawState : public GrRODrawState { | 17 class GrOptDrawState : public SkRefCnt { |
20 public: | 18 public: |
21 bool operator== (const GrOptDrawState& that) const; | 19 bool operator== (const GrOptDrawState& that) const; |
22 | 20 |
| 21 /////////////////////////////////////////////////////////////////////////// |
| 22 /// @name Vertex Attributes |
| 23 //// |
| 24 |
| 25 enum { |
| 26 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, |
| 27 }; |
| 28 |
| 29 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } |
| 30 int getVertexAttribCount() const { return fVACount; } |
| 31 |
| 32 size_t getVertexStride() const { return fVAStride; } |
| 33 |
| 34 /** |
| 35 * Getters for index into getVertexAttribs() for particular bindings. -1 is
returned if the |
| 36 * binding does not appear in the current attribs. These bindings should app
ear only once in |
| 37 * the attrib array. |
| 38 */ |
| 39 |
| 40 int positionAttributeIndex() const { |
| 41 return fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding
]; |
| 42 } |
| 43 int localCoordAttributeIndex() const { |
| 44 return fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBindi
ng]; |
| 45 } |
| 46 int colorVertexAttributeIndex() const { |
| 47 return fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding]; |
| 48 } |
| 49 int coverageVertexAttributeIndex() const { |
| 50 return fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding
]; |
| 51 } |
| 52 |
| 53 bool hasLocalCoordAttribute() const { |
| 54 return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttri
bBinding]; |
| 55 } |
| 56 bool hasColorVertexAttribute() const { |
| 57 return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBind
ing]; |
| 58 } |
| 59 bool hasCoverageVertexAttribute() const { |
| 60 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribB
inding]; |
| 61 } |
| 62 |
| 63 /// @} |
| 64 |
| 65 /////////////////////////////////////////////////////////////////////////// |
| 66 /// @name Color |
| 67 //// |
| 68 |
| 69 GrColor getColor() const { return fColor; } |
| 70 |
| 71 /// @} |
| 72 |
| 73 /////////////////////////////////////////////////////////////////////////// |
| 74 /// @name Coverage |
| 75 //// |
| 76 |
| 77 uint8_t getCoverage() const { return fCoverage; } |
| 78 |
| 79 GrColor getCoverageColor() const { |
| 80 return GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage); |
| 81 } |
| 82 |
| 83 /// @} |
| 84 |
| 85 /////////////////////////////////////////////////////////////////////////// |
| 86 /// @name Effect Stages |
| 87 /// Each stage hosts a GrProcessor. The effect produces an output color or c
overage in the |
| 88 /// fragment shader. Its inputs are the output from the previous stage as we
ll as some variables |
| 89 /// available to it in the fragment and vertex shader (e.g. the vertex posit
ion, the dst color, |
| 90 /// the fragment position, local coordinates). |
| 91 /// |
| 92 /// The stages are divided into two sets, color-computing and coverage-compu
ting. The final |
| 93 /// color stage produces the final pixel color. The coverage-computing stage
s function exactly |
| 94 /// as the color-computing but the output of the final coverage stage is tre
ated as a fractional |
| 95 /// pixel coverage rather than as input to the src/dst color blend step. |
| 96 /// |
| 97 /// The input color to the first color-stage is either the constant color or
interpolated |
| 98 /// per-vertex colors. The input to the first coverage stage is either a con
stant coverage |
| 99 /// (usually full-coverage) or interpolated per-vertex coverage. |
| 100 /// |
| 101 /// See the documentation of kCoverageDrawing_StateBit for information about
disabling the |
| 102 /// the color / coverage distinction. |
| 103 //// |
| 104 |
| 105 int numColorStages() const { return fColorStages.count(); } |
| 106 int numCoverageStages() const { return fCoverageStages.count(); } |
| 107 int numTotalStages() const { |
| 108 return this->numColorStages() + this->numCoverageStages() + |
| 109 (this->hasGeometryProcessor() ? 1 : 0); |
| 110 } |
| 111 |
| 112 bool hasGeometryProcessor() const { return SkToBool(fGeometryProcessor.get()
); } |
| 113 const GrGeometryStage* getGeometryProcessor() const { return fGeometryProces
sor.get(); } |
| 114 const GrFragmentStage& getColorStage(int idx) const { return fColorStages[id
x]; } |
| 115 const GrFragmentStage& getCoverageStage(int idx) const { return fCoverageSta
ges[idx]; } |
| 116 |
| 117 /// @} |
| 118 |
| 119 /////////////////////////////////////////////////////////////////////////// |
| 120 /// @name Blending |
| 121 //// |
| 122 |
| 123 GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlend; } |
| 124 GrBlendCoeff getDstBlendCoeff() const { return fDstBlend; } |
| 125 |
| 126 /** |
| 127 * Retrieves the last value set by setBlendConstant() |
| 128 * @return the blending constant value |
| 129 */ |
| 130 GrColor getBlendConstant() const { return fBlendConstant; } |
| 131 |
| 132 /// @} |
| 133 |
| 134 /////////////////////////////////////////////////////////////////////////// |
| 135 /// @name View Matrix |
| 136 //// |
| 137 |
| 138 /** |
| 139 * Retrieves the current view matrix |
| 140 * @return the current view matrix. |
| 141 */ |
| 142 const SkMatrix& getViewMatrix() const { return fViewMatrix; } |
| 143 |
| 144 /** |
| 145 * Retrieves the inverse of the current view matrix. |
| 146 * |
| 147 * If the current view matrix is invertible, return true, and if matrix |
| 148 * is non-null, copy the inverse into it. If the current view matrix is |
| 149 * non-invertible, return false and ignore the matrix parameter. |
| 150 * |
| 151 * @param matrix if not null, will receive a copy of the current inverse. |
| 152 */ |
| 153 bool getViewInverse(SkMatrix* matrix) const { |
| 154 SkMatrix inverse; |
| 155 if (fViewMatrix.invert(&inverse)) { |
| 156 if (matrix) { |
| 157 *matrix = inverse; |
| 158 } |
| 159 return true; |
| 160 } |
| 161 return false; |
| 162 } |
| 163 |
| 164 /// @} |
| 165 |
| 166 /////////////////////////////////////////////////////////////////////////// |
| 167 /// @name Render Target |
| 168 //// |
| 169 |
| 170 /** |
| 171 * Retrieves the currently set render-target. |
| 172 * |
| 173 * @return The currently set render target. |
| 174 */ |
| 175 GrRenderTarget* getRenderTarget() const { |
| 176 return static_cast<GrRenderTarget*>(fRenderTarget.getResource()); |
| 177 } |
| 178 |
| 179 /// @} |
| 180 |
| 181 /////////////////////////////////////////////////////////////////////////// |
| 182 /// @name Stencil |
| 183 //// |
| 184 |
| 185 const GrStencilSettings& getStencil() const { return fStencilSettings; } |
| 186 |
| 187 /// @} |
| 188 |
| 189 /////////////////////////////////////////////////////////////////////////// |
| 190 /// @name State Flags |
| 191 //// |
| 192 |
| 193 /** |
| 194 * Flags that affect rendering. Controlled using enable/disableState(). All |
| 195 * default to disabled. |
| 196 */ |
| 197 enum StateBits { |
| 198 /** |
| 199 * Perform dithering. TODO: Re-evaluate whether we need this bit |
| 200 */ |
| 201 kDither_StateBit = 0x01, |
| 202 /** |
| 203 * Perform HW anti-aliasing. This means either HW FSAA, if supported by
the render target, |
| 204 * or smooth-line rendering if a line primitive is drawn and line smooth
ing is supported by |
| 205 * the 3D API. |
| 206 */ |
| 207 kHWAntialias_StateBit = 0x02, |
| 208 /** |
| 209 * Draws will respect the clip, otherwise the clip is ignored. |
| 210 */ |
| 211 kClip_StateBit = 0x04, |
| 212 /** |
| 213 * Disables writing to the color buffer. Useful when performing stencil |
| 214 * operations. |
| 215 */ |
| 216 kNoColorWrites_StateBit = 0x08, |
| 217 |
| 218 /** |
| 219 * Usually coverage is applied after color blending. The color is blende
d using the coeffs |
| 220 * specified by setBlendFunc(). The blended color is then combined with
dst using coeffs |
| 221 * of src_coverage, 1-src_coverage. Sometimes we are explicitly drawing
a coverage mask. In |
| 222 * this case there is no distinction between coverage and color and the
caller needs direct |
| 223 * control over the blend coeffs. When set, there will be a single blend
step controlled by |
| 224 * setBlendFunc() which will use coverage*color as the src color. |
| 225 */ |
| 226 kCoverageDrawing_StateBit = 0x10, |
| 227 |
| 228 // Users of the class may add additional bits to the vector |
| 229 kDummyStateBit, |
| 230 kLastPublicStateBit = kDummyStateBit-1, |
| 231 }; |
| 232 |
| 233 bool isStateFlagEnabled(uint32_t stateBit) const { return 0 != (stateBit & f
FlagBits); } |
| 234 |
| 235 bool isDitherState() const { return 0 != (fFlagBits & kDither_StateBit); } |
| 236 bool isHWAntialiasState() const { return 0 != (fFlagBits & kHWAntialias_Stat
eBit); } |
| 237 bool isClipState() const { return 0 != (fFlagBits & kClip_StateBit); } |
| 238 bool isColorWriteDisabled() const { return 0 != (fFlagBits & kNoColorWrites_
StateBit); } |
| 239 bool isCoverageDrawing() const { return 0 != (fFlagBits & kCoverageDrawing_S
tateBit); } |
| 240 |
| 241 /// @} |
| 242 |
| 243 /////////////////////////////////////////////////////////////////////////// |
| 244 /// @name Face Culling |
| 245 //// |
| 246 |
| 247 enum DrawFace { |
| 248 kInvalid_DrawFace = -1, |
| 249 |
| 250 kBoth_DrawFace, |
| 251 kCCW_DrawFace, |
| 252 kCW_DrawFace, |
| 253 }; |
| 254 |
| 255 /** |
| 256 * Gets whether the target is drawing clockwise, counterclockwise, |
| 257 * or both faces. |
| 258 * @return the current draw face(s). |
| 259 */ |
| 260 DrawFace getDrawFace() const { return fDrawFace; } |
| 261 |
| 262 /// @} |
| 263 |
| 264 /////////////////////////////////////////////////////////////////////////// |
| 265 |
| 266 /** Return type for CombineIfPossible. */ |
| 267 enum CombinedState { |
| 268 /** The GrDrawStates cannot be combined. */ |
| 269 kIncompatible_CombinedState, |
| 270 /** Either draw state can be used in place of the other. */ |
| 271 kAOrB_CombinedState, |
| 272 /** Use the first draw state. */ |
| 273 kA_CombinedState, |
| 274 /** Use the second draw state. */ |
| 275 kB_CombinedState, |
| 276 }; |
| 277 |
23 bool inputColorIsUsed() const { return fInputColorIsUsed; } | 278 bool inputColorIsUsed() const { return fInputColorIsUsed; } |
24 bool inputCoverageIsUsed() const { return fInputCoverageIsUsed; } | 279 bool inputCoverageIsUsed() const { return fInputCoverageIsUsed; } |
25 | 280 |
26 bool readsDst() const { return fReadsDst; } | 281 bool readsDst() const { return fReadsDst; } |
27 bool readsFragPosition() const { return fReadsFragPosition; } | 282 bool readsFragPosition() const { return fReadsFragPosition; } |
28 bool requiresLocalCoordAttrib() const { return fRequiresLocalCoordAttrib; } | 283 bool requiresLocalCoordAttrib() const { return fRequiresLocalCoordAttrib; } |
29 | 284 |
30 /////////////////////////////////////////////////////////////////////////// | 285 /////////////////////////////////////////////////////////////////////////// |
31 /// @name Stage Output Types | 286 /// @name Stage Output Types |
32 //// | 287 //// |
(...skipping 24 matching lines...) Expand all Loading... |
57 kSecondaryOutputTypeCnt, | 312 kSecondaryOutputTypeCnt, |
58 }; | 313 }; |
59 | 314 |
60 PrimaryOutputType getPrimaryOutputType() const { return fPrimaryOutputType;
} | 315 PrimaryOutputType getPrimaryOutputType() const { return fPrimaryOutputType;
} |
61 SecondaryOutputType getSecondaryOutputType() const { return fSecondaryOutput
Type; } | 316 SecondaryOutputType getSecondaryOutputType() const { return fSecondaryOutput
Type; } |
62 | 317 |
63 /// @} | 318 /// @} |
64 | 319 |
65 private: | 320 private: |
66 /** | 321 /** |
| 322 * Optimizations for blending / coverage to that can be applied based on the
current state. |
| 323 */ |
| 324 enum BlendOptFlags { |
| 325 /** |
| 326 * No optimization |
| 327 */ |
| 328 kNone_BlendOpt = 0, |
| 329 /** |
| 330 * Don't draw at all |
| 331 */ |
| 332 kSkipDraw_BlendOptFlag = 0x1, |
| 333 /** |
| 334 * The coverage value does not have to be computed separately from alpha
, the the output |
| 335 * color can be the modulation of the two. |
| 336 */ |
| 337 kCoverageAsAlpha_BlendOptFlag = 0x2, |
| 338 /** |
| 339 * Instead of emitting a src color, emit coverage in the alpha channel a
nd r,g,b are |
| 340 * "don't cares". |
| 341 */ |
| 342 kEmitCoverage_BlendOptFlag = 0x4, |
| 343 /** |
| 344 * Emit transparent black instead of the src color, no need to compute c
overage. |
| 345 */ |
| 346 kEmitTransBlack_BlendOptFlag = 0x8, |
| 347 }; |
| 348 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| 349 |
| 350 /** |
67 * Constructs and optimized drawState out of a GrRODrawState. | 351 * Constructs and optimized drawState out of a GrRODrawState. |
68 */ | 352 */ |
69 GrOptDrawState(const GrDrawState& drawState, BlendOptFlags blendOptFlags, | 353 GrOptDrawState(const GrDrawState& drawState, BlendOptFlags blendOptFlags, |
70 GrBlendCoeff optSrcCoeff, GrBlendCoeff optDstCoeff, | 354 GrBlendCoeff optSrcCoeff, GrBlendCoeff optDstCoeff, |
71 const GrDrawTargetCaps& caps); | 355 const GrDrawTargetCaps& caps); |
72 | 356 |
73 /** | 357 /** |
74 * Loops through all the color stage effects to check if the stage will igno
re color input or | 358 * Loops through all the color stage effects to check if the stage will igno
re color input or |
75 * always output a constant color. In the ignore color input case we can ign
ore all previous | 359 * always output a constant color. In the ignore color input case we can ign
ore all previous |
76 * stages. In the constant color case, we can ignore all previous stages and | 360 * stages. In the constant color case, we can ignore all previous stages and |
(...skipping 30 matching lines...) Expand all Loading... |
107 */ | 391 */ |
108 void getStageStats(); | 392 void getStageStats(); |
109 | 393 |
110 /** | 394 /** |
111 * Calculates the primary and secondary output types of the shader. For cert
ain output types | 395 * Calculates the primary and secondary output types of the shader. For cert
ain output types |
112 * the function may adjust the blend coefficients. After this function is ca
lled the src and dst | 396 * the function may adjust the blend coefficients. After this function is ca
lled the src and dst |
113 * blend coeffs will represent those used by backend API. | 397 * blend coeffs will represent those used by backend API. |
114 */ | 398 */ |
115 void setOutputStateInfo(const GrDrawTargetCaps&); | 399 void setOutputStateInfo(const GrDrawTargetCaps&); |
116 | 400 |
| 401 bool isEqual(const GrOptDrawState& that) const; |
| 402 |
| 403 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== |
| 404 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget; |
| 405 ProgramRenderTarget fRenderTarget; |
| 406 GrColor fColor; |
| 407 SkMatrix fViewMatrix; |
| 408 GrColor fBlendConstant; |
| 409 uint32_t fFlagBits; |
| 410 const GrVertexAttrib* fVAPtr; |
| 411 int fVACount; |
| 412 size_t fVAStride; |
| 413 GrStencilSettings fStencilSettings; |
| 414 uint8_t fCoverage; |
| 415 DrawFace fDrawFace; |
| 416 GrBlendCoeff fSrcBlend; |
| 417 GrBlendCoeff fDstBlend; |
| 418 |
| 419 typedef SkSTArray<4, GrFragmentStage> FragmentStageArray; |
| 420 SkAutoTDelete<GrGeometryStage> fGeometryProcessor; |
| 421 FragmentStageArray fColorStages; |
| 422 FragmentStageArray fCoverageStages; |
| 423 |
| 424 // This is simply a different representation of info in fVertexAttribs and t
hus does |
| 425 // not need to be compared in op==. |
| 426 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt
]; |
| 427 |
117 // These flags are needed to protect the code from creating an unused unifor
m color/coverage | 428 // These flags are needed to protect the code from creating an unused unifor
m color/coverage |
118 // which will cause shader compiler errors. | 429 // which will cause shader compiler errors. |
119 bool fInputColorIsUsed; | 430 bool fInputColorIsUsed; |
120 bool fInputCoverageIsUsed; | 431 bool fInputCoverageIsUsed; |
121 | 432 |
122 // These flags give aggregated info on the effect stages that are used when
building programs. | 433 // These flags give aggregated info on the effect stages that are used when
building programs. |
123 bool fReadsDst; | 434 bool fReadsDst; |
124 bool fReadsFragPosition; | 435 bool fReadsFragPosition; |
125 bool fRequiresLocalCoordAttrib; | 436 bool fRequiresLocalCoordAttrib; |
126 | 437 |
127 SkAutoSTArray<4, GrVertexAttrib> fOptVA; | 438 SkAutoSTArray<4, GrVertexAttrib> fOptVA; |
128 | 439 |
129 BlendOptFlags fBlendOptFlags; | 440 BlendOptFlags fBlendOptFlags; |
130 | 441 |
131 // Fragment shader color outputs | 442 // Fragment shader color outputs |
132 PrimaryOutputType fPrimaryOutputType : 8; | 443 PrimaryOutputType fPrimaryOutputType : 8; |
133 SecondaryOutputType fSecondaryOutputType : 8; | 444 SecondaryOutputType fSecondaryOutputType : 8; |
134 | 445 |
135 friend GrOptDrawState* GrDrawState::createOptState(const GrDrawTargetCaps&)
const; | 446 friend GrOptDrawState* GrDrawState::createOptState(const GrDrawTargetCaps&)
const; |
136 typedef GrRODrawState INHERITED; | 447 typedef SkRefCnt INHERITED; |
137 }; | 448 }; |
138 | 449 |
| 450 GR_MAKE_BITFIELD_OPS(GrOptDrawState::BlendOptFlags); |
| 451 |
139 #endif | 452 #endif |
| 453 |
OLD | NEW |