gfxStateBlock.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "gfx/gfxStateBlock.h"
  23. #include "core/crc.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "core/strings/stringFunctions.h"
  26. #include "gfx/gfxStringEnumTranslate.h"
  27. ///
  28. /// GFXStateBlock
  29. ///
  30. const String GFXStateBlock::describeSelf() const
  31. {
  32. return String::ToString("hashvalue: 0x%x", getDesc().getHashValue());
  33. }
  34. ///
  35. /// GFXStateBlockDesc
  36. ///
  37. GFXStateBlockDesc::GFXStateBlockDesc()
  38. {
  39. // Alpha blending
  40. blendDefined = false;
  41. blendEnable = false;
  42. blendSrc = GFXBlendOne;
  43. blendDest = GFXBlendZero;
  44. blendOp = GFXBlendOpAdd;
  45. // Separate alpha blending
  46. separateAlphaBlendDefined = false;
  47. separateAlphaBlendEnable = false;
  48. separateAlphaBlendSrc = GFXBlendOne;
  49. separateAlphaBlendDest = GFXBlendZero;
  50. separateAlphaBlendOp = GFXBlendOpAdd;
  51. // Alpha test
  52. alphaDefined = false;
  53. alphaTestEnable = false;
  54. alphaTestRef = 0;
  55. alphaTestFunc = GFXCmpGreaterEqual;
  56. // Color Writes
  57. colorWriteDefined = false;
  58. colorWriteRed = true;
  59. colorWriteBlue = true;
  60. colorWriteGreen = true;
  61. colorWriteAlpha = true;
  62. // Rasterizer
  63. cullDefined = false;
  64. cullMode = GFXCullCCW;
  65. // Depth
  66. zDefined = false;
  67. zEnable = true;
  68. zWriteEnable = true;
  69. zFunc = GFXCmpLessEqual;
  70. zBias = 0;
  71. zSlopeBias = 0;
  72. // Stencil
  73. stencilDefined = false;
  74. stencilEnable = false;
  75. stencilFailOp = GFXStencilOpKeep;
  76. stencilZFailOp = GFXStencilOpKeep;
  77. stencilPassOp = GFXStencilOpKeep;
  78. stencilFunc = GFXCmpNever;
  79. stencilRef = 0;
  80. stencilMask = 0xFFFFFFFF;
  81. stencilWriteMask = 0xFFFFFFFF;
  82. vertexColorEnable = false;
  83. fillMode = GFXFillSolid;
  84. samplersDefined = false;
  85. textureFactor.set( 255, 255, 255, 255 );
  86. }
  87. // This method just needs to return a unique value based on its contents.
  88. U32 GFXStateBlockDesc::getHashValue() const
  89. {
  90. return CRC::calculateCRC(this, sizeof(GFXStateBlockDesc));
  91. }
  92. /// Adds data from desc to this description, uses *defined parameters in desc to figure out
  93. /// what blocks of state to actually copy from desc.
  94. void GFXStateBlockDesc::addDesc(const GFXStateBlockDesc& desc)
  95. {
  96. // Alpha blending
  97. if (desc.blendDefined)
  98. {
  99. blendDefined = true;
  100. blendEnable = desc.blendEnable;
  101. blendSrc = desc.blendSrc;
  102. blendDest = desc.blendDest;
  103. blendOp = desc.blendOp;
  104. }
  105. // Separate alpha blending
  106. if ( desc.separateAlphaBlendDefined )
  107. {
  108. separateAlphaBlendDefined = true;
  109. separateAlphaBlendEnable = desc.separateAlphaBlendEnable;
  110. separateAlphaBlendSrc = desc.separateAlphaBlendSrc;
  111. separateAlphaBlendDest = desc.separateAlphaBlendDest;
  112. separateAlphaBlendOp = desc.separateAlphaBlendOp;
  113. }
  114. // Alpha test
  115. if (desc.alphaDefined)
  116. {
  117. alphaDefined = true;
  118. alphaTestEnable = desc.alphaTestEnable;
  119. alphaTestRef = desc.alphaTestRef;
  120. alphaTestFunc = desc.alphaTestFunc;
  121. }
  122. // Color Writes
  123. if (desc.colorWriteDefined)
  124. {
  125. colorWriteDefined = true;
  126. colorWriteRed = desc.colorWriteRed;
  127. colorWriteBlue = desc.colorWriteBlue;
  128. colorWriteGreen = desc.colorWriteGreen;
  129. colorWriteAlpha = desc.colorWriteAlpha;
  130. }
  131. // Rasterizer
  132. if (desc.cullDefined)
  133. {
  134. cullDefined = true;
  135. cullMode = desc.cullMode;
  136. }
  137. // Depth
  138. if (desc.zDefined)
  139. {
  140. zDefined = true;
  141. zEnable = desc.zEnable;
  142. zWriteEnable = desc.zWriteEnable;
  143. zFunc = desc.zFunc;
  144. zBias = desc.zBias;
  145. zSlopeBias = desc.zSlopeBias;
  146. }
  147. // Stencil
  148. if (desc.stencilDefined)
  149. {
  150. stencilDefined = true;
  151. stencilEnable = desc.stencilEnable;
  152. stencilFailOp = desc.stencilFailOp;
  153. stencilZFailOp = desc.stencilZFailOp;
  154. stencilPassOp = desc.stencilPassOp;
  155. stencilFunc = desc.stencilFunc;
  156. stencilRef = desc.stencilRef;
  157. stencilMask = desc.stencilMask;
  158. stencilWriteMask = desc.stencilWriteMask;
  159. }
  160. if (desc.samplersDefined)
  161. {
  162. samplersDefined = true;
  163. for (U32 i = 0; i < GFX_TEXTURE_STAGE_COUNT; i++)
  164. {
  165. samplers[i] = desc.samplers[i];
  166. }
  167. textureFactor = desc.textureFactor;
  168. }
  169. vertexColorEnable = desc.vertexColorEnable;
  170. fillMode = desc.fillMode;
  171. }
  172. /// Returns a string that describes the options set (used by GFXStateBlock::describeSelf)
  173. const String GFXStateBlockDesc::describeSelf() const
  174. {
  175. GFXStringEnumTranslate::init();
  176. String ret;
  177. ret = String::ToString(" AlphaBlend: %d, BlendSrc: %s, BlendDest: %s, BlendOp: %s\n",
  178. blendEnable, GFXStringBlend[blendSrc], GFXStringBlend[blendDest], GFXStringBlendOp[blendOp]);
  179. ret += String::ToString(" SeparateAlphaBlend: %d, SeparateAlphaBlendSrc: %s, SeparateAlphaBlendDest: %s, SeparateAlphaBlendOp: %s\n",
  180. separateAlphaBlendEnable, GFXStringBlend[separateAlphaBlendSrc], GFXStringBlend[separateAlphaBlendDest], GFXStringBlendOp[separateAlphaBlendOp]);
  181. ret += String::ToString(" AlphaTest: %d, AlphaTestFunc: %s, AlphaTestRef: %d\n",
  182. alphaTestEnable, GFXStringCmpFunc[alphaTestFunc], alphaTestRef);
  183. ret += String::ToString(" ColorWrites: r: %d g: %d b: %d a: %d",
  184. colorWriteRed, colorWriteGreen, colorWriteBlue, colorWriteAlpha);
  185. ret += String::ToString(" CullMode: %s\n", GFXStringCullMode[cullMode]);
  186. ret += String::ToString(" ZEnable: %d, ZWriteEnable: %d, ZFunc: %s, ZBias: %f, ZSlopeBias: %f\n",
  187. zEnable, zWriteEnable, GFXStringCmpFunc[zFunc], zBias, zSlopeBias);
  188. ret += String::ToString(" Stencil: %d, StencilFailOp: %s, StencilZFailOp: %s, StencilPassOp: %s, \n stencilFunc: %s, stencilRef: %d, stencilMask: 0x%x, stencilWriteMask: 0x%x\n",
  189. stencilEnable, GFXStringCmpFunc[stencilFailOp], GFXStringCmpFunc[stencilZFailOp], GFXStringCmpFunc[stencilPassOp],
  190. GFXStringCmpFunc[stencilFunc], stencilRef, stencilMask, stencilWriteMask);
  191. return ret;
  192. }
  193. //
  194. // Utility functions
  195. //
  196. void GFXStateBlockDesc::setCullMode( GFXCullMode m )
  197. {
  198. cullDefined = true;
  199. cullMode = m;
  200. }
  201. void GFXStateBlockDesc::setZReadWrite( bool read, bool write )
  202. {
  203. zDefined = true;
  204. zEnable = read;
  205. zWriteEnable = write;
  206. }
  207. void GFXStateBlockDesc::setAlphaTest( bool enable, GFXCmpFunc func, S32 alphaRef )
  208. {
  209. alphaDefined = true;
  210. alphaTestEnable = enable;
  211. alphaTestFunc = func;
  212. alphaTestRef = alphaRef;
  213. }
  214. void GFXStateBlockDesc::setBlend( bool enable, GFXBlend src, GFXBlend dest, GFXBlendOp op )
  215. {
  216. blendDefined = true;
  217. blendEnable = enable;
  218. blendSrc = src;
  219. blendDest = dest;
  220. blendOp = op;
  221. }
  222. void GFXStateBlockDesc::setSeparateAlphaBlend( bool enable, GFXBlend src, GFXBlend dest, GFXBlendOp op )
  223. {
  224. separateAlphaBlendDefined = true;
  225. separateAlphaBlendEnable = enable;
  226. separateAlphaBlendSrc = src;
  227. separateAlphaBlendDest = dest;
  228. separateAlphaBlendOp = op;
  229. }
  230. void GFXStateBlockDesc::setColorWrites( bool red, bool green, bool blue, bool alpha )
  231. {
  232. colorWriteDefined = true;
  233. colorWriteRed = red;
  234. colorWriteGreen = green;
  235. colorWriteBlue = blue;
  236. colorWriteAlpha = alpha;
  237. }
  238. GFXSamplerStateDesc::GFXSamplerStateDesc()
  239. {
  240. addressModeU = GFXAddressWrap;
  241. addressModeV = GFXAddressWrap;
  242. addressModeW = GFXAddressWrap;
  243. magFilter = GFXTextureFilterLinear;
  244. minFilter = GFXTextureFilterLinear;
  245. mipFilter = GFXTextureFilterLinear;
  246. samplerFunc = GFXCmpNever;
  247. maxAnisotropy = 1;
  248. mipLODBias = 0.0f;
  249. }
  250. GFXSamplerStateDesc GFXSamplerStateDesc::getWrapLinear()
  251. {
  252. // Linear with wrapping is already the default
  253. GFXSamplerStateDesc ssd;
  254. return ssd;
  255. }
  256. GFXSamplerStateDesc GFXSamplerStateDesc::getWrapPoint()
  257. {
  258. GFXSamplerStateDesc ssd;
  259. ssd.magFilter = GFXTextureFilterPoint;
  260. ssd.minFilter = GFXTextureFilterPoint;
  261. ssd.mipFilter = GFXTextureFilterPoint;
  262. return ssd;
  263. }
  264. GFXSamplerStateDesc GFXSamplerStateDesc::getClampLinear()
  265. {
  266. GFXSamplerStateDesc ssd;
  267. ssd.addressModeU = GFXAddressClamp;
  268. ssd.addressModeV = GFXAddressClamp;
  269. ssd.addressModeW = GFXAddressClamp;
  270. return ssd;
  271. }
  272. GFXSamplerStateDesc GFXSamplerStateDesc::getClampPoint()
  273. {
  274. GFXSamplerStateDesc ssd;
  275. ssd.addressModeU = GFXAddressClamp;
  276. ssd.addressModeV = GFXAddressClamp;
  277. ssd.addressModeW = GFXAddressClamp;
  278. ssd.magFilter = GFXTextureFilterPoint;
  279. ssd.minFilter = GFXTextureFilterPoint;
  280. ssd.mipFilter = GFXTextureFilterPoint;
  281. return ssd;
  282. }