gfxStateBlockData.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "platform/platform.h"
  23. #include "gfx/sim/gfxStateBlockData.h"
  24. #include "console/consoleTypes.h"
  25. #include "gfx/gfxStringEnumTranslate.h"
  26. IMPLEMENT_CONOBJECT( GFXStateBlockData );
  27. ConsoleDocClass( GFXStateBlockData,
  28. "@brief A state block description for rendering.\n\n"
  29. "This object is used with ShaderData in CustomMaterial and PostEffect to define the "
  30. "render state.\n"
  31. "@tsexample\n"
  32. "singleton GFXStateBlockData( PFX_DOFDownSampleStateBlock )\n"
  33. "{\n"
  34. " zDefined = true;\n"
  35. " zEnable = false;\n"
  36. " zWriteEnable = false;\n"
  37. "\n"
  38. " samplersDefined = true;\n"
  39. " samplerStates[0] = SamplerClampLinear;\n"
  40. " samplerStates[1] = SamplerClampPoint;\n"
  41. "\n"
  42. " // Copy the clamped linear sampler, but change\n"
  43. " // the u coord to wrap for this special case.\n"
  44. " samplerStates[2] = new GFXSamplerStateData( : SamplerClampLinear )\n"
  45. " {\n"
  46. " addressModeU = GFXAddressWrap;\n"
  47. " };\n"
  48. "};\n"
  49. "@endtsexample\n"
  50. "@note The 'xxxxDefined' fields are used to know what groups of fields are modified "
  51. "when combining multiple state blocks in material processing. You should take care to "
  52. "enable the right ones when setting values.\n"
  53. "@ingroup GFX\n" );
  54. GFXStateBlockData::GFXStateBlockData()
  55. {
  56. for (U32 i = 0; i < GFX_TEXTURE_STAGE_COUNT; i++)
  57. mSamplerStates[i] = NULL;
  58. }
  59. void GFXStateBlockData::initPersistFields()
  60. {
  61. addGroup( "Alpha Blending" );
  62. addField( "blendDefined", TypeBool, Offset(mState.blendDefined, GFXStateBlockData),
  63. "Set to true if the alpha blend state is not all defaults." );
  64. addField( "blendEnable", TypeBool, Offset(mState.blendEnable, GFXStateBlockData),
  65. "Enables alpha blending. The default is false." );
  66. addField( "blendSrc", TypeGFXBlend, Offset(mState.blendSrc, GFXStateBlockData),
  67. "The source blend state. The default is GFXBlendOne." );
  68. addField("blendDest", TypeGFXBlend, Offset(mState.blendDest, GFXStateBlockData),
  69. "The destination blend state. The default is GFXBlendZero." );
  70. addField("blendOp", TypeGFXBlendOp, Offset(mState.blendOp, GFXStateBlockData),
  71. "The arithmetic operation applied to alpha blending. The default is GFXBlendOpAdd." );
  72. endGroup( "Alpha Blending" );
  73. addGroup( "Separate Alpha Blending" );
  74. addField( "separateAlphaBlendDefined", TypeBool, Offset(mState.separateAlphaBlendDefined, GFXStateBlockData),
  75. "Set to true if the seperate alpha blend state is not all defaults." );
  76. addField( "separateAlphaBlendEnable", TypeBool, Offset(mState.separateAlphaBlendEnable, GFXStateBlockData),
  77. "Enables the separate blend mode for the alpha channel. The default is false." );
  78. addField( "separateAlphaBlendSrc", TypeGFXBlend, Offset(mState.separateAlphaBlendSrc, GFXStateBlockData),
  79. "The source blend state. The default is GFXBlendOne." );
  80. addField( "separateAlphaBlendDest", TypeGFXBlend, Offset(mState.separateAlphaBlendDest, GFXStateBlockData),
  81. "The destination blend state. The default is GFXBlendZero." );
  82. addField( "separateAlphaBlendOp", TypeGFXBlendOp, Offset(mState.separateAlphaBlendOp, GFXStateBlockData),
  83. "The arithmetic operation applied to separate alpha blending. The default is GFXBlendOpAdd." );
  84. endGroup( "Separate Alpha Blending" );
  85. addGroup( "Alpha Test" );
  86. addField( "alphaDefined", TypeBool, Offset(mState.alphaDefined, GFXStateBlockData),
  87. "Set to true if the alpha test state is not all defaults." );
  88. addField( "alphaTestEnable", TypeBool, Offset(mState.alphaTestEnable, GFXStateBlockData),
  89. "Enables per-pixel alpha testing. The default is false." );
  90. addField( "alphaTestFunc", TypeGFXCmpFunc, Offset(mState.alphaTestFunc, GFXStateBlockData),
  91. "The test function used to accept or reject a pixel based on its alpha value. The default is GFXCmpGreaterEqual." );
  92. addField( "alphaTestRef", TypeS32, Offset(mState.alphaTestRef, GFXStateBlockData),
  93. "The reference alpha value against which pixels are tested. The default is zero." );
  94. endGroup( "Alpha Test" );
  95. addGroup( "Color Write" );
  96. addField( "colorWriteDefined", TypeBool, Offset(mState.colorWriteDefined, GFXStateBlockData),
  97. "Set to true if the color write state is not all defaults." );
  98. addField( "colorWriteRed", TypeBool, Offset(mState.colorWriteRed, GFXStateBlockData),
  99. "Enables red channel writes. The default is true." );
  100. addField( "colorWriteBlue", TypeBool, Offset(mState.colorWriteBlue, GFXStateBlockData),
  101. "Enables blue channel writes. The default is true." );
  102. addField( "colorWriteGreen", TypeBool, Offset(mState.colorWriteGreen, GFXStateBlockData),
  103. "Enables green channel writes. The default is true." );
  104. addField( "colorWriteAlpha", TypeBool, Offset(mState.colorWriteAlpha, GFXStateBlockData),
  105. "Enables alpha channel writes. The default is true." );
  106. endGroup( "Color Write" );
  107. addGroup( "Culling" );
  108. addField("cullDefined", TypeBool, Offset(mState.cullDefined, GFXStateBlockData),
  109. "Set to true if the culling state is not all defaults." );
  110. addField("cullMode", TypeGFXCullMode, Offset(mState.cullMode, GFXStateBlockData),
  111. "Defines how back facing triangles are culled if at all. The default is GFXCullCCW." );
  112. endGroup( "Culling" );
  113. addGroup( "Depth" );
  114. addField( "zDefined", TypeBool, Offset(mState.zDefined, GFXStateBlockData),
  115. "Set to true if the depth state is not all defaults." );
  116. addField( "zEnable", TypeBool, Offset(mState.zEnable, GFXStateBlockData),
  117. "Enables z-buffer reads. The default is true." );
  118. addField( "zWriteEnable", TypeBool, Offset(mState.zWriteEnable, GFXStateBlockData),
  119. "Enables z-buffer writes. The default is true." );
  120. addField( "zFunc", TypeGFXCmpFunc, Offset(mState.zFunc, GFXStateBlockData),
  121. "The depth comparision function which a pixel must pass to be written to the z-buffer. The default is GFXCmpLessEqual." );
  122. addField( "zBias", TypeF32, Offset(mState.zBias, GFXStateBlockData),
  123. "A floating-point bias used when comparing depth values. The default is zero." );
  124. addField( "zSlopeBias", TypeF32, Offset(mState.zSlopeBias, GFXStateBlockData),
  125. "An additional floating-point bias based on the maximum depth slop of the triangle being rendered. The default is zero." );
  126. endGroup( "Depth" );
  127. addGroup( "Stencil" );
  128. addField( "stencilDefined", TypeBool, Offset(mState.stencilDefined, GFXStateBlockData),
  129. "Set to true if the stencil state is not all defaults." );
  130. addField( "stencilEnable", TypeBool, Offset(mState.stencilEnable, GFXStateBlockData),
  131. "Enables stenciling. The default is false." );
  132. addField( "stencilFailOp", TypeGFXStencilOp, Offset(mState.stencilFailOp, GFXStateBlockData),
  133. "The stencil operation to perform if the stencil test fails. The default is GFXStencilOpKeep." );
  134. addField( "stencilZFailOp", TypeGFXStencilOp, Offset(mState.stencilZFailOp, GFXStateBlockData),
  135. "The stencil operation to perform if the stencil test passes and the depth test fails. The default is GFXStencilOpKeep." );
  136. addField( "stencilPassOp", TypeGFXStencilOp, Offset(mState.stencilPassOp, GFXStateBlockData),
  137. "The stencil operation to perform if both the stencil and the depth tests pass. The default is GFXStencilOpKeep." );
  138. addField( "stencilFunc", TypeGFXCmpFunc, Offset(mState.stencilFunc, GFXStateBlockData),
  139. "The comparison function to test the reference value to a stencil buffer entry. The default is GFXCmpNever." );
  140. addField( "stencilRef", TypeS32, Offset(mState.stencilRef, GFXStateBlockData),
  141. "The reference value for the stencil test. The default is zero." );
  142. addField( "stencilMask", TypeS32, Offset(mState.stencilMask, GFXStateBlockData),
  143. "The mask applied to the reference value and each stencil buffer entry to determine the significant bits for the stencil test. The default is 0xFFFFFFFF." );
  144. addField( "stencilWriteMask", TypeS32, Offset(mState.stencilWriteMask, GFXStateBlockData),
  145. "The write mask applied to values written into the stencil buffer. The default is 0xFFFFFFFF." );
  146. endGroup( "Stencil" );
  147. addGroup( "Fixed Function" );
  148. addField( "vertexColorEnable", TypeBool, Offset(mState.vertexColorEnable, GFXStateBlockData),
  149. "Enables fixed function vertex coloring when rendering without a shader. The default is false." );
  150. endGroup( "Fixed Function" );
  151. addGroup( "Sampler States" );
  152. addField( "samplersDefined", TypeBool, Offset(mState.samplersDefined, GFXStateBlockData),
  153. "Set to true if the sampler states are not all defaults." );
  154. addField( "samplerStates", TYPEID<GFXSamplerStateData>(), Offset(mSamplerStates, GFXStateBlockData), GFX_TEXTURE_STAGE_COUNT,
  155. "The array of texture sampler states.\n"
  156. "@note Not all graphics devices support 16 samplers. In general "
  157. "all systems support 4 samplers with most modern cards doing 8." );
  158. addField( "textureFactor", TypeColorI, Offset(mState.textureFactor, GFXStateBlockData),
  159. "The color used for multiple-texture blending with the GFXTATFactor texture-blending argument or "
  160. "the GFXTOPBlendFactorAlpha texture-blending operation. The default is opaque white (255, 255, 255, 255)." );
  161. endGroup( "Sampler States" );
  162. Parent::initPersistFields();
  163. }
  164. bool GFXStateBlockData::onAdd()
  165. {
  166. if (!Parent::onAdd())
  167. return false;
  168. for (U32 i = 0; i < GFX_TEXTURE_STAGE_COUNT; i++)
  169. {
  170. if (mSamplerStates[i])
  171. mSamplerStates[i]->setSamplerState(mState.samplers[i]);
  172. }
  173. return true;
  174. }
  175. IMPLEMENT_CONOBJECT( GFXSamplerStateData );
  176. ConsoleDocClass( GFXSamplerStateData,
  177. "@brief A sampler state used by GFXStateBlockData.\n\n"
  178. "The samplers define how a texture will be sampled when used from the shader "
  179. "or fixed function device\n"
  180. "@tsexample\n"
  181. "singleton GFXSamplerStateData(SamplerClampLinear)\n"
  182. "{\n"
  183. " textureColorOp = GFXTOPModulate;\n"
  184. " addressModeU = GFXAddressClamp;\n"
  185. " addressModeV = GFXAddressClamp;\n"
  186. " addressModeW = GFXAddressClamp;\n"
  187. " magFilter = GFXTextureFilterLinear;\n"
  188. " minFilter = GFXTextureFilterLinear;\n"
  189. " mipFilter = GFXTextureFilterLinear;\n"
  190. "};\n"
  191. "@endtsexample\n"
  192. "There are a few predefined samplers in the core scripts which you can use with "
  193. "GFXStateBlockData for the most common rendering cases:\n"
  194. " - SamplerClampLinear\n"
  195. " - SamplerClampPoint\n"
  196. " - SamplerWrapLinear\n"
  197. " - SamplerWrapPoint\n"
  198. "\n"
  199. "@see GFXStateBlockData\n"
  200. "@ingroup GFX\n" );
  201. void GFXSamplerStateData::initPersistFields()
  202. {
  203. Parent::initPersistFields();
  204. addGroup( "Address Mode" );
  205. addField("addressModeU", TypeGFXTextureAddressMode, Offset(mState.addressModeU, GFXSamplerStateData),
  206. "The texture address mode for the u coordinate. The default is GFXAddressWrap." );
  207. addField("addressModeV", TypeGFXTextureAddressMode, Offset(mState.addressModeV, GFXSamplerStateData),
  208. "The texture address mode for the v coordinate. The default is GFXAddressWrap." );
  209. addField("addressModeW", TypeGFXTextureAddressMode, Offset(mState.addressModeW, GFXSamplerStateData),
  210. "The texture address mode for the w coordinate. The default is GFXAddressWrap." );
  211. endGroup( "Address Mode" );
  212. addGroup( "Filter State" );
  213. addField("magFilter", TypeGFXTextureFilterType, Offset(mState.magFilter, GFXSamplerStateData),
  214. "The texture magnification filter. The default is GFXTextureFilterLinear." );
  215. addField("minFilter", TypeGFXTextureFilterType, Offset(mState.minFilter, GFXSamplerStateData),
  216. "The texture minification filter. The default is GFXTextureFilterLinear." );
  217. addField("mipFilter", TypeGFXTextureFilterType, Offset(mState.mipFilter, GFXSamplerStateData),
  218. "The texture mipmap filter used during minification. The default is GFXTextureFilterLinear." );
  219. addField("mipLODBias", TypeF32, Offset(mState.mipLODBias, GFXSamplerStateData),
  220. "The mipmap level of detail bias. The default value is zero." );
  221. addField("maxAnisotropy", TypeS32, Offset(mState.maxAnisotropy, GFXSamplerStateData),
  222. "The maximum texture anisotropy. The default value is 1." );
  223. endGroup( "Filter State" );
  224. addField("samplerFunc", TypeGFXCmpFunc, Offset(mState.samplerFunc, GFXSamplerStateData),
  225. "Compares sampled data against existing sampled data. The default is GFXCmpNever.");
  226. }
  227. /// Copies the data of this object into desc
  228. void GFXSamplerStateData::setSamplerState(GFXSamplerStateDesc& desc)
  229. {
  230. desc = mState;
  231. }