| 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 "GrColor.h" | 11 #include "GrColor.h" |
| 12 #include "GrGpu.h" | 12 #include "GrGpu.h" |
| 13 #include "GrProcessorStage.h" | 13 #include "GrProcessorStage.h" |
| 14 #include "GrProgramDesc.h" |
| 14 #include "GrStencil.h" | 15 #include "GrStencil.h" |
| 15 #include "GrTypesPriv.h" | 16 #include "GrTypesPriv.h" |
| 16 #include "SkMatrix.h" | 17 #include "SkMatrix.h" |
| 17 #include "SkRefCnt.h" | 18 #include "SkRefCnt.h" |
| 18 | 19 |
| 20 class GrDeviceCoordTexture; |
| 19 class GrDrawState; | 21 class GrDrawState; |
| 20 | 22 |
| 21 /** | 23 /** |
| 22 * Class that holds an optimized version of a GrDrawState. It is meant to be an
immutable class, | 24 * Class that holds an optimized version of a GrDrawState. It is meant to be an
immutable class, |
| 23 * and contains all data needed to set the state for a gpu draw. | 25 * and contains all data needed to set the state for a gpu draw. |
| 24 */ | 26 */ |
| 25 class GrOptDrawState : public SkRefCnt { | 27 class GrOptDrawState : public SkRefCnt { |
| 26 public: | 28 public: |
| 27 /** | 29 /** |
| 28 * Returns a snapshot of the current optimized state. If the current drawSta
te has a valid | 30 * Returns a snapshot of the current optimized state. If the current drawSta
te has a valid |
| 29 * cached optimiezed state it will simply return a pointer to it otherwise i
t will create a new | 31 * cached optimiezed state it will simply return a pointer to it otherwise i
t will create a new |
| 30 * GrOptDrawState. In all cases the GrOptDrawState is reffed and ownership i
s given to the | 32 * GrOptDrawState. In all cases the GrOptDrawState is reffed and ownership i
s given to the |
| 31 * caller. | 33 * caller. |
| 32 */ | 34 */ |
| 33 static GrOptDrawState* Create(const GrDrawState& drawState, const GrDrawTarg
etCaps& caps, | 35 static GrOptDrawState* Create(const GrDrawState& drawState, GrGpu*, |
| 34 GrGpu::DrawType drawType); | 36 const GrDeviceCoordTexture* dstCopy, GrGpu::Dr
awType drawType); |
| 35 | 37 |
| 36 bool operator== (const GrOptDrawState& that) const; | 38 bool operator== (const GrOptDrawState& that) const; |
| 37 | 39 |
| 38 /////////////////////////////////////////////////////////////////////////// | 40 /////////////////////////////////////////////////////////////////////////// |
| 39 /// @name Vertex Attributes | 41 /// @name Vertex Attributes |
| 40 //// | 42 //// |
| 41 | 43 |
| 42 enum { | 44 enum { |
| 43 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, | 45 kMaxVertexAttribCnt = kLast_GrVertexAttribBinding + 4, |
| 44 }; | 46 }; |
| 45 | 47 |
| 46 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } | 48 const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } |
| 47 int getVertexAttribCount() const { return fVACount; } | 49 int getVertexAttribCount() const { return fVACount; } |
| 48 | 50 |
| 49 size_t getVertexStride() const { return fVAStride; } | 51 size_t getVertexStride() const { return fVAStride; } |
| 50 | 52 |
| 51 /** | |
| 52 * Getters for index into getVertexAttribs() for particular bindings. -1 is
returned if the | |
| 53 * binding does not appear in the current attribs. These bindings should app
ear only once in | |
| 54 * the attrib array. | |
| 55 */ | |
| 56 | |
| 57 int positionAttributeIndex() const { | |
| 58 return fFixedFunctionVertexAttribIndices[kPosition_GrVertexAttribBinding
]; | |
| 59 } | |
| 60 int localCoordAttributeIndex() const { | |
| 61 return fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttribBindi
ng]; | |
| 62 } | |
| 63 int colorVertexAttributeIndex() const { | |
| 64 return fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBinding]; | |
| 65 } | |
| 66 int coverageVertexAttributeIndex() const { | |
| 67 return fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribBinding
]; | |
| 68 } | |
| 69 | |
| 70 bool hasLocalCoordAttribute() const { | |
| 71 return -1 != fFixedFunctionVertexAttribIndices[kLocalCoord_GrVertexAttri
bBinding]; | |
| 72 } | |
| 73 bool hasColorVertexAttribute() const { | |
| 74 return -1 != fFixedFunctionVertexAttribIndices[kColor_GrVertexAttribBind
ing]; | |
| 75 } | |
| 76 bool hasCoverageVertexAttribute() const { | |
| 77 return -1 != fFixedFunctionVertexAttribIndices[kCoverage_GrVertexAttribB
inding]; | |
| 78 } | |
| 79 | |
| 80 /// @} | 53 /// @} |
| 81 | 54 |
| 82 /////////////////////////////////////////////////////////////////////////// | 55 /////////////////////////////////////////////////////////////////////////// |
| 83 /// @name Color | 56 /// @name Color |
| 84 //// | 57 //// |
| 85 | 58 |
| 86 GrColor getColor() const { return fColor; } | 59 GrColor getColor() const { return fColor; } |
| 87 | 60 |
| 88 /// @} | 61 /// @} |
| 89 | 62 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 /** The GrDrawStates cannot be combined. */ | 265 /** The GrDrawStates cannot be combined. */ |
| 293 kIncompatible_CombinedState, | 266 kIncompatible_CombinedState, |
| 294 /** Either draw state can be used in place of the other. */ | 267 /** Either draw state can be used in place of the other. */ |
| 295 kAOrB_CombinedState, | 268 kAOrB_CombinedState, |
| 296 /** Use the first draw state. */ | 269 /** Use the first draw state. */ |
| 297 kA_CombinedState, | 270 kA_CombinedState, |
| 298 /** Use the second draw state. */ | 271 /** Use the second draw state. */ |
| 299 kB_CombinedState, | 272 kB_CombinedState, |
| 300 }; | 273 }; |
| 301 | 274 |
| 302 bool inputColorIsUsed() const { return fInputColorIsUsed; } | 275 /// @} |
| 303 bool inputCoverageIsUsed() const { return fInputCoverageIsUsed; } | |
| 304 | 276 |
| 305 bool readsDst() const { return fReadsDst; } | 277 const GrProgramDesc& programDesc() const { return fDesc; } |
| 306 bool readsFragPosition() const { return fReadsFragPosition; } | |
| 307 bool requiresLocalCoordAttrib() const { return fRequiresLocalCoordAttrib; } | |
| 308 | |
| 309 /////////////////////////////////////////////////////////////////////////// | |
| 310 /// @name Stage Output Types | |
| 311 //// | |
| 312 | |
| 313 enum PrimaryOutputType { | |
| 314 // Modulate color and coverage, write result as the color output. | |
| 315 kModulate_PrimaryOutputType, | |
| 316 // Combines the coverage, dst, and color as coverage * color + (1 - cove
rage) * dst. This | |
| 317 // can only be set if fDstReadKey is non-zero. | |
| 318 kCombineWithDst_PrimaryOutputType, | |
| 319 | |
| 320 kPrimaryOutputTypeCnt, | |
| 321 }; | |
| 322 | |
| 323 enum SecondaryOutputType { | |
| 324 // There is no secondary output | |
| 325 kNone_SecondaryOutputType, | |
| 326 // Writes coverage as the secondary output. Only set if dual source blen
ding is supported | |
| 327 // and primary output is kModulate. | |
| 328 kCoverage_SecondaryOutputType, | |
| 329 // Writes coverage * (1 - colorA) as the secondary output. Only set if d
ual source blending | |
| 330 // is supported and primary output is kModulate. | |
| 331 kCoverageISA_SecondaryOutputType, | |
| 332 // Writes coverage * (1 - colorRGBA) as the secondary output. Only set i
f dual source | |
| 333 // blending is supported and primary output is kModulate. | |
| 334 kCoverageISC_SecondaryOutputType, | |
| 335 | |
| 336 kSecondaryOutputTypeCnt, | |
| 337 }; | |
| 338 | |
| 339 PrimaryOutputType getPrimaryOutputType() const { return fPrimaryOutputType;
} | |
| 340 SecondaryOutputType getSecondaryOutputType() const { return fSecondaryOutput
Type; } | |
| 341 | |
| 342 /// @} | |
| 343 | 278 |
| 344 private: | 279 private: |
| 345 /** | 280 /** |
| 346 * Optimizations for blending / coverage to that can be applied based on the
current state. | 281 * Optimizations for blending / coverage to that can be applied based on the
current state. |
| 347 */ | 282 */ |
| 348 enum BlendOptFlags { | 283 enum BlendOptFlags { |
| 349 /** | 284 /** |
| 350 * No optimization | 285 * No optimization |
| 351 */ | 286 */ |
| 352 kNone_BlendOpt = 0, | 287 kNone_BlendOpt = 0, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 369 */ | 304 */ |
| 370 kEmitTransBlack_BlendOptFlag = 0x8, | 305 kEmitTransBlack_BlendOptFlag = 0x8, |
| 371 }; | 306 }; |
| 372 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); | 307 GR_DECL_BITFIELD_OPS_FRIENDS(BlendOptFlags); |
| 373 | 308 |
| 374 /** | 309 /** |
| 375 * Constructs and optimized drawState out of a GrRODrawState. | 310 * Constructs and optimized drawState out of a GrRODrawState. |
| 376 */ | 311 */ |
| 377 GrOptDrawState(const GrDrawState& drawState, BlendOptFlags blendOptFlags, | 312 GrOptDrawState(const GrDrawState& drawState, BlendOptFlags blendOptFlags, |
| 378 GrBlendCoeff optSrcCoeff, GrBlendCoeff optDstCoeff, | 313 GrBlendCoeff optSrcCoeff, GrBlendCoeff optDstCoeff, |
| 379 const GrDrawTargetCaps& caps); | 314 GrGpu*, const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType)
; |
| 380 | 315 |
| 381 /** | 316 /** |
| 382 * Loops through all the color stage effects to check if the stage will igno
re color input or | 317 * Loops through all the color stage effects to check if the stage will igno
re color input or |
| 383 * always output a constant color. In the ignore color input case we can ign
ore all previous | 318 * always output a constant color. In the ignore color input case we can ign
ore all previous |
| 384 * stages. In the constant color case, we can ignore all previous stages and | 319 * stages. In the constant color case, we can ignore all previous stages and |
| 385 * the current one and set the state color to the constant color. | 320 * the current one and set the state color to the constant color. |
| 386 */ | 321 */ |
| 387 void computeEffectiveColorStages(const GrDrawState& ds, int* firstColorStage
Idx, | 322 void computeEffectiveColorStages(const GrDrawState& ds, GrProgramDesc::DescI
nfo*, |
| 388 uint8_t* fixFunctionVAToRemove); | 323 int* firstColorStageIdx, uint8_t* fixFuncti
onVAToRemove); |
| 389 | 324 |
| 390 /** | 325 /** |
| 391 * Loops through all the coverage stage effects to check if the stage will i
gnore color input. | 326 * Loops through all the coverage stage effects to check if the stage will i
gnore color input. |
| 392 * If a coverage stage will ignore input, then we can ignore all coverage st
ages before it. We | 327 * If a coverage stage will ignore input, then we can ignore all coverage st
ages before it. We |
| 393 * loop to determine the first effective coverage stage. | 328 * loop to determine the first effective coverage stage. |
| 394 */ | 329 */ |
| 395 void computeEffectiveCoverageStages(const GrDrawState& ds, int* firstCoverag
eStageIdx); | 330 void computeEffectiveCoverageStages(const GrDrawState& ds, GrProgramDesc::De
scInfo* descInfo, |
| 331 int* firstCoverageStageIdx); |
| 396 | 332 |
| 397 /** | 333 /** |
| 398 * This function takes in a flag and removes the corresponding fixed functio
n vertex attributes. | 334 * This function takes in a flag and removes the corresponding fixed functio
n vertex attributes. |
| 399 * The flags are in the same order as GrVertexAttribBinding array. If bit i
of removeVAFlags is | 335 * The flags are in the same order as GrVertexAttribBinding array. If bit i
of removeVAFlags is |
| 400 * set, then vertex attributes with binding (GrVertexAttribute)i will be rem
oved. | 336 * set, then vertex attributes with binding (GrVertexAttribute)i will be rem
oved. |
| 401 */ | 337 */ |
| 402 void removeFixedFunctionVertexAttribs(uint8_t removeVAFlags); | 338 void removeFixedFunctionVertexAttribs(uint8_t removeVAFlags, GrProgramDesc::
DescInfo*); |
| 403 | 339 |
| 404 /** | 340 /** |
| 405 * Alter the OptDrawState (adjusting stages, vertex attribs, flags, etc.) ba
sed on the | 341 * Alter the OptDrawState (adjusting stages, vertex attribs, flags, etc.) ba
sed on the |
| 406 * BlendOptFlags. | 342 * BlendOptFlags. |
| 407 */ | 343 */ |
| 408 void adjustFromBlendOpts(const GrDrawState& ds, int* firstColorStageIdx, | 344 void adjustFromBlendOpts(const GrDrawState& ds, GrProgramDesc::DescInfo*, |
| 409 int* firstCoverageStageIdx, uint8_t* fixedFunctionV
AToRemove); | 345 int* firstColorStageIdx, int* firstCoverageStageIdx
, |
| 346 uint8_t* fixedFunctionVAToRemove); |
| 410 | 347 |
| 411 /** | 348 /** |
| 412 * Loop over the effect stages to determine various info like what data they
will read and what | 349 * Loop over the effect stages to determine various info like what data they
will read and what |
| 413 * shaders they require. | 350 * shaders they require. |
| 414 */ | 351 */ |
| 415 void getStageStats(const GrDrawState& ds, int firstColorStageIdx, int firstC
overageStageIdx); | 352 void getStageStats(const GrDrawState& ds, int firstColorStageIdx, int firstC
overageStageIdx, |
| 353 GrProgramDesc::DescInfo*); |
| 416 | 354 |
| 417 /** | 355 /** |
| 418 * Calculates the primary and secondary output types of the shader. For cert
ain output types | 356 * Calculates the primary and secondary output types of the shader. For cert
ain output types |
| 419 * the function may adjust the blend coefficients. After this function is ca
lled the src and dst | 357 * the function may adjust the blend coefficients. After this function is ca
lled the src and dst |
| 420 * blend coeffs will represent those used by backend API. | 358 * blend coeffs will represent those used by backend API. |
| 421 */ | 359 */ |
| 422 void setOutputStateInfo(const GrDrawState& ds, const GrDrawTargetCaps&, | 360 void setOutputStateInfo(const GrDrawState& ds, const GrDrawTargetCaps&, |
| 423 int firstCoverageStageIdx, bool* separateCoverageFro
mColor); | 361 int firstCoverageStageIdx, GrProgramDesc::DescInfo*, |
| 362 bool* separateCoverageFromColor); |
| 424 | 363 |
| 425 bool isEqual(const GrOptDrawState& that) const; | 364 bool isEqual(const GrOptDrawState& that) const; |
| 426 | 365 |
| 427 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== | 366 // These fields are roughly sorted by decreasing likelihood of being differe
nt in op== |
| 428 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget; | 367 typedef GrTGpuResourceRef<GrRenderTarget> ProgramRenderTarget; |
| 429 ProgramRenderTarget fRenderTarget; | 368 ProgramRenderTarget fRenderTarget; |
| 430 GrColor fColor; | 369 GrColor fColor; |
| 431 SkMatrix fViewMatrix; | 370 SkMatrix fViewMatrix; |
| 432 GrColor fBlendConstant; | 371 GrColor fBlendConstant; |
| 433 uint32_t fFlagBits; | 372 uint32_t fFlagBits; |
| 434 const GrVertexAttrib* fVAPtr; | 373 const GrVertexAttrib* fVAPtr; |
| 435 int fVACount; | 374 int fVACount; |
| 436 size_t fVAStride; | 375 size_t fVAStride; |
| 437 GrStencilSettings fStencilSettings; | 376 GrStencilSettings fStencilSettings; |
| 438 uint8_t fCoverage; | 377 uint8_t fCoverage; |
| 439 DrawFace fDrawFace; | 378 DrawFace fDrawFace; |
| 440 GrBlendCoeff fSrcBlend; | 379 GrBlendCoeff fSrcBlend; |
| 441 GrBlendCoeff fDstBlend; | 380 GrBlendCoeff fDstBlend; |
| 442 | 381 |
| 443 typedef SkSTArray<8, GrFragmentStage> FragmentStageArray; | 382 typedef SkSTArray<8, GrFragmentStage> FragmentStageArray; |
| 444 typedef GrProgramElementRef<const GrGeometryProcessor> ProgramGeometryProces
sor; | 383 typedef GrProgramElementRef<const GrGeometryProcessor> ProgramGeometryProces
sor; |
| 445 ProgramGeometryProcessor fGeometryProcessor; | 384 ProgramGeometryProcessor fGeometryProcessor; |
| 446 FragmentStageArray fFragmentStages; | 385 FragmentStageArray fFragmentStages; |
| 447 | 386 |
| 448 // This function is equivalent to the offset into fFragmentStages where cove
rage stages begin. | 387 // This function is equivalent to the offset into fFragmentStages where cove
rage stages begin. |
| 449 int fNumColorStages; | 388 int fNumColorStages; |
| 450 | 389 |
| 451 // This is simply a different representation of info in fVertexAttribs and t
hus does | |
| 452 // not need to be compared in op==. | |
| 453 int fFixedFunctionVertexAttribIndices[kGrFixedFunctionVertexAttribBindingCnt
]; | |
| 454 | |
| 455 // These flags are needed to protect the code from creating an unused unifor
m color/coverage | |
| 456 // which will cause shader compiler errors. | |
| 457 bool fInputColorIsUsed; | |
| 458 bool fInputCoverageIsUsed; | |
| 459 | |
| 460 // These flags give aggregated info on the effect stages that are used when
building programs. | |
| 461 bool fReadsDst; | |
| 462 bool fReadsFragPosition; | |
| 463 bool fRequiresLocalCoordAttrib; | |
| 464 | |
| 465 SkAutoSTArray<4, GrVertexAttrib> fOptVA; | 390 SkAutoSTArray<4, GrVertexAttrib> fOptVA; |
| 466 | 391 |
| 467 BlendOptFlags fBlendOptFlags; | 392 BlendOptFlags fBlendOptFlags; |
| 468 | 393 |
| 469 // Fragment shader color outputs | 394 GrProgramDesc fDesc; |
| 470 PrimaryOutputType fPrimaryOutputType : 8; | |
| 471 SecondaryOutputType fSecondaryOutputType : 8; | |
| 472 | 395 |
| 473 typedef SkRefCnt INHERITED; | 396 typedef SkRefCnt INHERITED; |
| 474 }; | 397 }; |
| 475 | 398 |
| 476 GR_MAKE_BITFIELD_OPS(GrOptDrawState::BlendOptFlags); | 399 GR_MAKE_BITFIELD_OPS(GrOptDrawState::BlendOptFlags); |
| 477 | 400 |
| 478 #endif | 401 #endif |
| 479 | 402 |
| OLD | NEW |