gfxStateBlock.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GFXSTATEBLOCK_H_
  23. #define _GFXSTATEBLOCK_H_
  24. #ifndef _REFBASE_H_
  25. #include "core/util/refBase.h"
  26. #endif
  27. #ifndef _GFXENUMS_H_
  28. #include "gfx/gfxEnums.h"
  29. #endif
  30. #ifndef _GFXRESOURCE_H_
  31. #include "gfx/gfxResource.h"
  32. #endif
  33. #ifndef _COLOR_H_
  34. #include "core/color.h"
  35. #endif
  36. struct GFXSamplerStateDesc
  37. {
  38. GFXTextureAddressMode addressModeU;
  39. GFXTextureAddressMode addressModeV;
  40. GFXTextureAddressMode addressModeW;
  41. GFXTextureFilterType magFilter;
  42. GFXTextureFilterType minFilter;
  43. GFXTextureFilterType mipFilter;
  44. GFXCmpFunc samplerFunc;
  45. ColorI borderColor;
  46. /// The maximum anisotropy used when one of the filter types
  47. /// is set to anisotropic.
  48. ///
  49. /// Defaults to 1.
  50. ///
  51. /// @see GFXTextureFilterType
  52. U32 maxAnisotropy;
  53. /// Used to offset the mipmap selection by whole or
  54. /// fractional amounts either postively or negatively.
  55. ///
  56. /// Defaults to zero.
  57. F32 mipLODBias;
  58. GFXSamplerStateDesc();
  59. /// Returns an modulate, wrap, and linear sampled state.
  60. static GFXSamplerStateDesc getWrapLinear();
  61. /// Returns an modulate, wrap, and point sampled state.
  62. static GFXSamplerStateDesc getWrapPoint();
  63. /// Returns an modulate, clamp, and linear sampled state.
  64. static GFXSamplerStateDesc getClampLinear();
  65. /// Returns an modulate, clamp, and point sampled state.
  66. static GFXSamplerStateDesc getClampPoint();
  67. bool operator==(const GFXSamplerStateDesc &b) const
  68. {
  69. return !dMemcmp(this, &b, sizeof(GFXSamplerStateDesc));
  70. }
  71. };
  72. /// GFXStateBlockDesc defines a render state, which is then used to create a GFXStateBlock instance.
  73. struct GFXStateBlockDesc
  74. {
  75. // Blending
  76. bool blendDefined;
  77. bool blendEnable;
  78. GFXBlend blendSrc;
  79. GFXBlend blendDest;
  80. GFXBlendOp blendOp;
  81. /// @name Separate Alpha Blending
  82. /// @{
  83. bool separateAlphaBlendDefined;
  84. bool separateAlphaBlendEnable;
  85. GFXBlend separateAlphaBlendSrc;
  86. GFXBlend separateAlphaBlendDest;
  87. GFXBlendOp separateAlphaBlendOp;
  88. /// @}
  89. // Alpha test
  90. bool alphaDefined;
  91. bool alphaTestEnable;
  92. S32 alphaTestRef;
  93. GFXCmpFunc alphaTestFunc;
  94. // Color Writes
  95. bool colorWriteDefined;
  96. bool colorWriteRed;
  97. bool colorWriteBlue;
  98. bool colorWriteGreen;
  99. bool colorWriteAlpha;
  100. // Rasterizer
  101. bool cullDefined;
  102. GFXCullMode cullMode;
  103. // Depth
  104. bool zDefined;
  105. bool zEnable;
  106. bool zWriteEnable;
  107. GFXCmpFunc zFunc;
  108. F32 zBias;
  109. F32 zSlopeBias;
  110. // Stencil
  111. bool stencilDefined;
  112. bool stencilEnable;
  113. GFXStencilOp stencilFailOp;
  114. GFXStencilOp stencilZFailOp;
  115. GFXStencilOp stencilPassOp;
  116. GFXCmpFunc stencilFunc;
  117. U32 stencilRef;
  118. U32 stencilMask;
  119. U32 stencilWriteMask;
  120. bool vertexColorEnable;
  121. GFXFillMode fillMode;
  122. // Sampler states
  123. bool samplersDefined;
  124. GFXSamplerStateDesc samplers[GFX_TEXTURE_STAGE_COUNT];
  125. ColorI textureFactor;
  126. GFXStateBlockDesc();
  127. /// Returns the hash value of this state description
  128. U32 getHashValue() const;
  129. /// Adds data from desc to this description, uses *defined parameters in desc to figure out
  130. /// what blocks of state to actually copy from desc.
  131. void addDesc( const GFXStateBlockDesc& desc );
  132. /// Returns a string that describes the options set (used by GFXStateBlock::describeSelf)
  133. const String describeSelf() const;
  134. /// Utility functions to make setting up stateblock descriptions less wordy.
  135. void setCullMode( GFXCullMode m );
  136. /// Helpers for setting the fill modes.
  137. void setFillModePoint() { fillMode = GFXFillPoint; }
  138. void setFillModeWireframe() { fillMode = GFXFillWireframe; }
  139. void setFillModeSolid() { fillMode = GFXFillSolid; }
  140. void setZReadWrite( bool read, bool write = true );
  141. void setAlphaTest( bool enable,
  142. GFXCmpFunc func = GFXCmpGreaterEqual,
  143. S32 alphaRef = 0 );
  144. void setBlend( bool enable,
  145. GFXBlend src = GFXBlendSrcAlpha,
  146. GFXBlend dest = GFXBlendInvSrcAlpha,
  147. GFXBlendOp op = GFXBlendOpAdd );
  148. void setSeparateAlphaBlend( bool enable,
  149. GFXBlend src = GFXBlendOne,
  150. GFXBlend dest = GFXBlendZero,
  151. GFXBlendOp op = GFXBlendOpAdd );
  152. ///
  153. void setColorWrites( bool red, bool green, bool blue, bool alpha );
  154. };
  155. class GFXStateBlock : public StrongRefBase, public GFXResource
  156. {
  157. public:
  158. virtual ~GFXStateBlock() { }
  159. /// Returns the hash value of the desc that created this block
  160. virtual U32 getHashValue() const = 0;
  161. /// Returns a GFXStateBlockDesc that this block represents
  162. virtual const GFXStateBlockDesc& getDesc() const = 0;
  163. /// Default implementation for GFXResource::describeSelf
  164. const String describeSelf() const override;
  165. };
  166. typedef StrongRefPtr<GFXStateBlock> GFXStateBlockRef;
  167. #endif