gfxStateBlock.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #pragma pack(push, 1)
  37. struct GFXSamplerStateDesc
  38. {
  39. GFXTextureAddressMode addressModeU;
  40. GFXTextureAddressMode addressModeV;
  41. GFXTextureAddressMode addressModeW;
  42. GFXTextureFilterType magFilter;
  43. GFXTextureFilterType minFilter;
  44. GFXTextureFilterType mipFilter;
  45. GFXCmpFunc samplerFunc;
  46. ColorI borderColor;
  47. /// The maximum anisotropy used when one of the filter types
  48. /// is set to anisotropic.
  49. ///
  50. /// Defaults to 1.
  51. ///
  52. /// @see GFXTextureFilterType
  53. U32 maxAnisotropy;
  54. /// Used to offset the mipmap selection by whole or
  55. /// fractional amounts either postively or negatively.
  56. ///
  57. /// Defaults to zero.
  58. F32 mipLODBias;
  59. GFXSamplerStateDesc();
  60. /// Returns an modulate, wrap, and linear sampled state.
  61. static GFXSamplerStateDesc getWrapLinear();
  62. /// Returns an modulate, wrap, and point sampled state.
  63. static GFXSamplerStateDesc getWrapPoint();
  64. /// Returns an modulate, clamp, and linear sampled state.
  65. static GFXSamplerStateDesc getClampLinear();
  66. /// Returns an modulate, clamp, and point sampled state.
  67. static GFXSamplerStateDesc getClampPoint();
  68. bool operator==(const GFXSamplerStateDesc &b) const
  69. {
  70. return !dMemcmp(this, &b, sizeof(GFXSamplerStateDesc));
  71. }
  72. };
  73. #pragma pack(pop)
  74. /// GFXStateBlockDesc defines a render state, which is then used to create a GFXStateBlock instance.
  75. #pragma pack(push, 1)
  76. struct GFXStateBlockDesc
  77. {
  78. // Blending
  79. bool blendDefined;
  80. bool blendEnable;
  81. GFXBlend blendSrc;
  82. GFXBlend blendDest;
  83. GFXBlendOp blendOp;
  84. /// @name Separate Alpha Blending
  85. /// @{
  86. bool separateAlphaBlendDefined;
  87. bool separateAlphaBlendEnable;
  88. GFXBlend separateAlphaBlendSrc;
  89. GFXBlend separateAlphaBlendDest;
  90. GFXBlendOp separateAlphaBlendOp;
  91. /// @}
  92. // Alpha test
  93. bool alphaDefined;
  94. bool alphaTestEnable;
  95. S32 alphaTestRef;
  96. GFXCmpFunc alphaTestFunc;
  97. // Color Writes
  98. bool colorWriteDefined;
  99. bool colorWriteRed;
  100. bool colorWriteBlue;
  101. bool colorWriteGreen;
  102. bool colorWriteAlpha;
  103. // Rasterizer
  104. bool cullDefined;
  105. GFXCullMode cullMode;
  106. // Depth
  107. bool zDefined;
  108. bool zEnable;
  109. bool zWriteEnable;
  110. GFXCmpFunc zFunc;
  111. F32 zBias;
  112. F32 zSlopeBias;
  113. // Stencil
  114. bool stencilDefined;
  115. bool stencilEnable;
  116. GFXStencilOp stencilFailOp;
  117. GFXStencilOp stencilZFailOp;
  118. GFXStencilOp stencilPassOp;
  119. GFXCmpFunc stencilFunc;
  120. U32 stencilRef;
  121. U32 stencilMask;
  122. U32 stencilWriteMask;
  123. bool vertexColorEnable;
  124. GFXFillMode fillMode;
  125. // Sampler states
  126. bool samplersDefined;
  127. GFXSamplerStateDesc samplers[GFX_TEXTURE_STAGE_COUNT];
  128. ColorI textureFactor;
  129. GFXStateBlockDesc();
  130. /// Returns the hash value of this state description
  131. U32 getHashValue() const;
  132. /// Adds data from desc to this description, uses *defined parameters in desc to figure out
  133. /// what blocks of state to actually copy from desc.
  134. void addDesc( const GFXStateBlockDesc& desc );
  135. /// Returns a string that describes the options set (used by GFXStateBlock::describeSelf)
  136. const String describeSelf() const;
  137. /// Utility functions to make setting up stateblock descriptions less wordy.
  138. void setCullMode( GFXCullMode m );
  139. /// Helpers for setting the fill modes.
  140. void setFillModePoint() { fillMode = GFXFillPoint; }
  141. void setFillModeWireframe() { fillMode = GFXFillWireframe; }
  142. void setFillModeSolid() { fillMode = GFXFillSolid; }
  143. void setZReadWrite( bool read, bool write = true );
  144. void setAlphaTest( bool enable,
  145. GFXCmpFunc func = GFXCmpGreaterEqual,
  146. S32 alphaRef = 0 );
  147. void setBlend( bool enable,
  148. GFXBlend src = GFXBlendSrcAlpha,
  149. GFXBlend dest = GFXBlendInvSrcAlpha,
  150. GFXBlendOp op = GFXBlendOpAdd );
  151. void setSeparateAlphaBlend( bool enable,
  152. GFXBlend src = GFXBlendOne,
  153. GFXBlend dest = GFXBlendZero,
  154. GFXBlendOp op = GFXBlendOpAdd );
  155. ///
  156. void setColorWrites( bool red, bool green, bool blue, bool alpha );
  157. };
  158. #pragma pack(pop)
  159. class GFXStateBlock : public StrongRefBase, public GFXResource
  160. {
  161. public:
  162. virtual ~GFXStateBlock() { }
  163. /// Returns the hash value of the desc that created this block
  164. virtual U32 getHashValue() const = 0;
  165. /// Returns a GFXStateBlockDesc that this block represents
  166. virtual const GFXStateBlockDesc& getDesc() const = 0;
  167. /// Default implementation for GFXResource::describeSelf
  168. const String describeSelf() const override;
  169. };
  170. typedef StrongRefPtr<GFXStateBlock> GFXStateBlockRef;
  171. #endif