gfxStateBlock.h 6.0 KB

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