gfxStateBlock.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // FF lighting
  83. ffLighting = false;
  84. vertexColorEnable = false;
  85. fillMode = GFXFillSolid;
  86. samplersDefined = false;
  87. textureFactor.set( 255, 255, 255, 255 );
  88. }
  89. // This method just needs to return a unique value based on its contents.
  90. U32 GFXStateBlockDesc::getHashValue() const
  91. {
  92. return CRC::calculateCRC(this, sizeof(GFXStateBlockDesc));
  93. }
  94. /// Adds data from desc to this description, uses *defined parameters in desc to figure out
  95. /// what blocks of state to actually copy from desc.
  96. void GFXStateBlockDesc::addDesc(const GFXStateBlockDesc& desc)
  97. {
  98. // Alpha blending
  99. if (desc.blendDefined)
  100. {
  101. blendDefined = true;
  102. blendEnable = desc.blendEnable;
  103. blendSrc = desc.blendSrc;
  104. blendDest = desc.blendDest;
  105. blendOp = desc.blendOp;
  106. }
  107. // Separate alpha blending
  108. if ( desc.separateAlphaBlendDefined )
  109. {
  110. separateAlphaBlendDefined = true;
  111. separateAlphaBlendEnable = desc.separateAlphaBlendEnable;
  112. separateAlphaBlendSrc = desc.separateAlphaBlendSrc;
  113. separateAlphaBlendDest = desc.separateAlphaBlendDest;
  114. separateAlphaBlendOp = desc.separateAlphaBlendOp;
  115. }
  116. // Alpha test
  117. if (desc.alphaDefined)
  118. {
  119. alphaDefined = true;
  120. alphaTestEnable = desc.alphaTestEnable;
  121. alphaTestRef = desc.alphaTestRef;
  122. alphaTestFunc = desc.alphaTestFunc;
  123. }
  124. // Color Writes
  125. if (desc.colorWriteDefined)
  126. {
  127. colorWriteDefined = true;
  128. colorWriteRed = desc.colorWriteRed;
  129. colorWriteBlue = desc.colorWriteBlue;
  130. colorWriteGreen = desc.colorWriteGreen;
  131. colorWriteAlpha = desc.colorWriteAlpha;
  132. }
  133. // Rasterizer
  134. if (desc.cullDefined)
  135. {
  136. cullDefined = true;
  137. cullMode = desc.cullMode;
  138. }
  139. // Depth
  140. if (desc.zDefined)
  141. {
  142. zDefined = true;
  143. zEnable = desc.zEnable;
  144. zWriteEnable = desc.zWriteEnable;
  145. zFunc = desc.zFunc;
  146. zBias = desc.zBias;
  147. zSlopeBias = desc.zSlopeBias;
  148. }
  149. // Stencil
  150. if (desc.stencilDefined)
  151. {
  152. stencilDefined = true;
  153. stencilEnable = desc.stencilEnable;
  154. stencilFailOp = desc.stencilFailOp;
  155. stencilZFailOp = desc.stencilZFailOp;
  156. stencilPassOp = desc.stencilPassOp;
  157. stencilFunc = desc.stencilFunc;
  158. stencilRef = desc.stencilRef;
  159. stencilMask = desc.stencilMask;
  160. stencilWriteMask = desc.stencilWriteMask;
  161. }
  162. if (desc.samplersDefined)
  163. {
  164. samplersDefined = true;
  165. for (U32 i = 0; i < TEXTURE_STAGE_COUNT; i++)
  166. {
  167. samplers[i] = desc.samplers[i];
  168. }
  169. textureFactor = desc.textureFactor;
  170. }
  171. vertexColorEnable = desc.vertexColorEnable;
  172. fillMode = desc.fillMode;
  173. }
  174. /// Returns a string that describes the options set (used by GFXStateBlock::describeSelf)
  175. const String GFXStateBlockDesc::describeSelf() const
  176. {
  177. GFXStringEnumTranslate::init();
  178. String ret;
  179. ret = String::ToString(" AlphaBlend: %d, BlendSrc: %s, BlendDest: %s, BlendOp: %s\n",
  180. blendEnable, GFXStringBlend[blendSrc], GFXStringBlend[blendDest], GFXStringBlendOp[blendOp]);
  181. ret += String::ToString(" SeparateAlphaBlend: %d, SeparateAlphaBlendSrc: %s, SeparateAlphaBlendDest: %s, SeparateAlphaBlendOp: %s\n",
  182. separateAlphaBlendEnable, GFXStringBlend[separateAlphaBlendSrc], GFXStringBlend[separateAlphaBlendDest], GFXStringBlendOp[separateAlphaBlendOp]);
  183. ret += String::ToString(" AlphaTest: %d, AlphaTestFunc: %s, AlphaTestRef: %d\n",
  184. alphaTestEnable, GFXStringCmpFunc[alphaTestFunc], alphaTestRef);
  185. ret += String::ToString(" ColorWrites: r: %d g: %d b: %d a: %d",
  186. colorWriteRed, colorWriteGreen, colorWriteBlue, colorWriteAlpha);
  187. ret += String::ToString(" CullMode: %s\n", GFXStringCullMode[cullMode]);
  188. ret += String::ToString(" ZEnable: %d, ZWriteEnable: %d, ZFunc: %s, ZBias: %f, ZSlopeBias: %f\n",
  189. zEnable, zWriteEnable, GFXStringCmpFunc[zFunc], zBias, zSlopeBias);
  190. ret += String::ToString(" Stencil: %d, StencilFailOp: %s, StencilZFailOp: %s, StencilPassOp: %s, \n stencilFunc: %s, stencilRef: %d, stencilMask: 0x%x, stencilWriteMask: 0x%x\n",
  191. stencilEnable, GFXStringCmpFunc[stencilFailOp], GFXStringCmpFunc[stencilZFailOp], GFXStringCmpFunc[stencilPassOp],
  192. GFXStringCmpFunc[stencilFunc], stencilRef, stencilMask, stencilWriteMask);
  193. ret += String::ToString(" FF Lighting: %d, VertexColors: %d, fillMode: %s",
  194. ffLighting, vertexColorEnable, GFXStringFillMode[fillMode]);
  195. return ret;
  196. }
  197. //
  198. // Utility functions
  199. //
  200. void GFXStateBlockDesc::setCullMode( GFXCullMode m )
  201. {
  202. cullDefined = true;
  203. cullMode = m;
  204. }
  205. void GFXStateBlockDesc::setZReadWrite( bool read, bool write )
  206. {
  207. zDefined = true;
  208. zEnable = read;
  209. zWriteEnable = write;
  210. }
  211. void GFXStateBlockDesc::setAlphaTest( bool enable, GFXCmpFunc func, S32 alphaRef )
  212. {
  213. alphaDefined = true;
  214. alphaTestEnable = enable;
  215. alphaTestFunc = func;
  216. alphaTestRef = alphaRef;
  217. }
  218. void GFXStateBlockDesc::setBlend( bool enable, GFXBlend src, GFXBlend dest, GFXBlendOp op )
  219. {
  220. blendDefined = true;
  221. blendEnable = enable;
  222. blendSrc = src;
  223. blendDest = dest;
  224. blendOp = op;
  225. }
  226. void GFXStateBlockDesc::setSeparateAlphaBlend( bool enable, GFXBlend src, GFXBlend dest, GFXBlendOp op )
  227. {
  228. separateAlphaBlendDefined = true;
  229. separateAlphaBlendEnable = enable;
  230. separateAlphaBlendSrc = src;
  231. separateAlphaBlendDest = dest;
  232. separateAlphaBlendOp = op;
  233. }
  234. void GFXStateBlockDesc::setColorWrites( bool red, bool green, bool blue, bool alpha )
  235. {
  236. colorWriteDefined = true;
  237. colorWriteRed = red;
  238. colorWriteGreen = green;
  239. colorWriteBlue = blue;
  240. colorWriteAlpha = alpha;
  241. }
  242. GFXSamplerStateDesc::GFXSamplerStateDesc()
  243. {
  244. textureColorOp = GFXTOPDisable;
  245. addressModeU = GFXAddressWrap;
  246. addressModeV = GFXAddressWrap;
  247. addressModeW = GFXAddressWrap;
  248. magFilter = GFXTextureFilterLinear;
  249. minFilter = GFXTextureFilterLinear;
  250. mipFilter = GFXTextureFilterLinear;
  251. maxAnisotropy = 1;
  252. alphaArg1 = GFXTATexture;
  253. alphaArg2 = GFXTADiffuse;
  254. alphaArg3 = GFXTACurrent;
  255. colorArg1 = GFXTACurrent;
  256. colorArg2 = GFXTATexture;
  257. colorArg3 = GFXTACurrent;
  258. alphaOp = GFXTOPModulate;
  259. textureTransform = GFXTTFFDisable;
  260. resultArg = GFXTACurrent;
  261. mipLODBias = 0.0f;
  262. }
  263. GFXSamplerStateDesc GFXSamplerStateDesc::getWrapLinear()
  264. {
  265. // Linear with wrapping is already the default
  266. GFXSamplerStateDesc ssd;
  267. ssd.textureColorOp = GFXTOPModulate;
  268. return ssd;
  269. }
  270. GFXSamplerStateDesc GFXSamplerStateDesc::getWrapPoint()
  271. {
  272. GFXSamplerStateDesc ssd;
  273. ssd.textureColorOp = GFXTOPModulate;
  274. ssd.magFilter = GFXTextureFilterPoint;
  275. ssd.minFilter = GFXTextureFilterPoint;
  276. ssd.mipFilter = GFXTextureFilterPoint;
  277. return ssd;
  278. }
  279. GFXSamplerStateDesc GFXSamplerStateDesc::getClampLinear()
  280. {
  281. GFXSamplerStateDesc ssd;
  282. ssd.textureColorOp = GFXTOPModulate;
  283. ssd.addressModeU = GFXAddressClamp;
  284. ssd.addressModeV = GFXAddressClamp;
  285. ssd.addressModeW = GFXAddressClamp;
  286. return ssd;
  287. }
  288. GFXSamplerStateDesc GFXSamplerStateDesc::getClampPoint()
  289. {
  290. GFXSamplerStateDesc ssd;
  291. ssd.textureColorOp = GFXTOPModulate;
  292. ssd.addressModeU = GFXAddressClamp;
  293. ssd.addressModeV = GFXAddressClamp;
  294. ssd.addressModeW = GFXAddressClamp;
  295. ssd.magFilter = GFXTextureFilterPoint;
  296. ssd.minFilter = GFXTextureFilterPoint;
  297. ssd.mipFilter = GFXTextureFilterPoint;
  298. return ssd;
  299. }