gfxStateBlockData.cpp 15 KB

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