gfxStateBlock.h 6.4 KB

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