gfxGLStateBlock.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/gl/gfxGLStateBlock.h"
  23. #include "gfx/gl/gfxGLDevice.h"
  24. #include "gfx/gl/gfxGLEnumTranslate.h"
  25. #include "gfx/gl/gfxGLUtils.h"
  26. #include "gfx/gl/gfxGLTextureObject.h"
  27. GFXGLStateBlock::GFXGLStateBlock(const GFXStateBlockDesc& desc) :
  28. mDesc(desc),
  29. mCachedHashValue(desc.getHashValue())
  30. {
  31. }
  32. GFXGLStateBlock::~GFXGLStateBlock()
  33. {
  34. }
  35. /// Returns the hash value of the desc that created this block
  36. U32 GFXGLStateBlock::getHashValue() const
  37. {
  38. return mCachedHashValue;
  39. }
  40. /// Returns a GFXStateBlockDesc that this block represents
  41. const GFXStateBlockDesc& GFXGLStateBlock::getDesc() const
  42. {
  43. return mDesc;
  44. }
  45. /// Called by OpenGL device to active this state block.
  46. /// @param oldState The current state, used to make sure we don't set redundant states on the device. Pass NULL to reset all states.
  47. void GFXGLStateBlock::activate(const GFXGLStateBlock* oldState)
  48. {
  49. // Big scary warning copied from Apple docs
  50. // http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_performance/chapter_13_section_2.html#//apple_ref/doc/uid/TP40001987-CH213-SW12
  51. // Don't set a state that's already set. Once a feature is enabled, it does not need to be enabled again.
  52. // Calling an enable function more than once does nothing except waste time because OpenGL does not check
  53. // the state of a feature when you call glEnable or glDisable. For instance, if you call glEnable(GL_LIGHTING)
  54. // more than once, OpenGL does not check to see if the lighting state is already enabled. It simply updates
  55. // the state value even if that value is identical to the current value.
  56. #define STATE_CHANGE(state) (!oldState || oldState->mDesc.state != mDesc.state)
  57. #define TOGGLE_STATE(state, enum) if(mDesc.state) glEnable(enum); else glDisable(enum)
  58. #define CHECK_TOGGLE_STATE(state, enum) if(!oldState || oldState->mDesc.state != mDesc.state) if(mDesc.state) glEnable(enum); else glDisable(enum)
  59. // Blending
  60. CHECK_TOGGLE_STATE(blendEnable, GL_BLEND);
  61. if(STATE_CHANGE(blendSrc) || STATE_CHANGE(blendDest))
  62. glBlendFunc(GFXGLBlend[mDesc.blendSrc], GFXGLBlend[mDesc.blendDest]);
  63. if(STATE_CHANGE(blendOp))
  64. glBlendEquation(GFXGLBlendOp[mDesc.blendOp]);
  65. // Alpha testing
  66. CHECK_TOGGLE_STATE(alphaTestEnable, GL_ALPHA_TEST);
  67. if(STATE_CHANGE(alphaTestFunc) || STATE_CHANGE(alphaTestRef))
  68. glAlphaFunc(GFXGLCmpFunc[mDesc.alphaTestFunc], (F32) mDesc.alphaTestRef * 1.0f/255.0f);
  69. // Color write masks
  70. if(STATE_CHANGE(colorWriteRed) || STATE_CHANGE(colorWriteBlue) || STATE_CHANGE(colorWriteGreen) || STATE_CHANGE(colorWriteAlpha))
  71. glColorMask(mDesc.colorWriteRed, mDesc.colorWriteBlue, mDesc.colorWriteGreen, mDesc.colorWriteAlpha);
  72. // Culling
  73. if(STATE_CHANGE(cullMode))
  74. {
  75. TOGGLE_STATE(cullMode, GL_CULL_FACE);
  76. glCullFace(GFXGLCullMode[mDesc.cullMode]);
  77. }
  78. // Depth
  79. CHECK_TOGGLE_STATE(zEnable, GL_DEPTH_TEST);
  80. if(STATE_CHANGE(zFunc))
  81. glDepthFunc(GFXGLCmpFunc[mDesc.zFunc]);
  82. if(STATE_CHANGE(zBias))
  83. {
  84. if (mDesc.zBias == 0)
  85. {
  86. glDisable(GL_POLYGON_OFFSET_FILL);
  87. } else {
  88. F32 bias = mDesc.zBias * 10000.0f;
  89. glEnable(GL_POLYGON_OFFSET_FILL);
  90. glPolygonOffset(bias, bias);
  91. }
  92. }
  93. if(STATE_CHANGE(zWriteEnable))
  94. glDepthMask(mDesc.zWriteEnable);
  95. // Stencil
  96. CHECK_TOGGLE_STATE(stencilEnable, GL_STENCIL_TEST);
  97. if(STATE_CHANGE(stencilFunc) || STATE_CHANGE(stencilRef) || STATE_CHANGE(stencilMask))
  98. glStencilFunc(GFXGLCmpFunc[mDesc.stencilFunc], mDesc.stencilRef, mDesc.stencilMask);
  99. if(STATE_CHANGE(stencilFailOp) || STATE_CHANGE(stencilZFailOp) || STATE_CHANGE(stencilPassOp))
  100. glStencilOp(GFXGLStencilOp[mDesc.stencilFailOp], GFXGLStencilOp[mDesc.stencilZFailOp], GFXGLStencilOp[mDesc.stencilPassOp]);
  101. if(STATE_CHANGE(stencilWriteMask))
  102. glStencilMask(mDesc.stencilWriteMask);
  103. // "Misc"
  104. CHECK_TOGGLE_STATE(ffLighting, GL_LIGHTING);
  105. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  106. CHECK_TOGGLE_STATE(vertexColorEnable, GL_COLOR_MATERIAL);
  107. if(STATE_CHANGE(fillMode))
  108. glPolygonMode(GL_FRONT_AND_BACK, GFXGLFillMode[mDesc.fillMode]);
  109. #undef CHECK_STATE
  110. #undef TOGGLE_STATE
  111. #undef CHECK_TOGGLE_STATE
  112. // TODO: states added for detail blend
  113. // Non per object texture mode states
  114. for (U32 i = 0; i < getMin(getOwningDevice()->getNumSamplers(), (U32) TEXTURE_STAGE_COUNT); i++)
  115. {
  116. GFXGLTextureObject* tex = static_cast<GFXGLTextureObject*>(getOwningDevice()->getCurrentTexture(i));
  117. const GFXSamplerStateDesc &ssd = mDesc.samplers[i];
  118. bool updateTexParam = true;
  119. glActiveTexture(GL_TEXTURE0 + i);
  120. switch (ssd.textureColorOp)
  121. {
  122. case GFXTOPDisable :
  123. if(!tex)
  124. break;
  125. glDisable(GL_TEXTURE_2D);
  126. updateTexParam = false;
  127. break;
  128. case GFXTOPModulate :
  129. glEnable(GL_TEXTURE_2D);
  130. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  131. break;
  132. case GFXTOPAdd :
  133. glEnable(GL_TEXTURE_2D);
  134. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
  135. break;
  136. default :
  137. glEnable(GL_TEXTURE_2D);
  138. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  139. break;
  140. }
  141. #define SSF(state, enum, value, tex) if(!oldState || oldState->mDesc.samplers[i].state != mDesc.samplers[i].state) glTexParameteri(tex->getBinding(), enum, value)
  142. #define SSW(state, enum, value, tex) if(!oldState || oldState->mDesc.samplers[i].state != mDesc.samplers[i].state) glTexParameteri(tex->getBinding(), enum, !tex->mIsNPoT2 ? value : GL_CLAMP_TO_EDGE)
  143. // Per object texture mode states.
  144. // TODO: Check dirty flag of samplers[i] and don't do this if it's dirty (it'll happen in the texture bind)
  145. if (updateTexParam && tex)
  146. {
  147. SSF(minFilter, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, tex->mMipLevels), tex);
  148. SSF(mipFilter, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, tex->mMipLevels), tex);
  149. SSF(magFilter, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter], tex);
  150. SSW(addressModeU, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU], tex);
  151. SSW(addressModeV, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV], tex);
  152. if( ( !oldState || oldState->mDesc.samplers[i].maxAnisotropy != ssd.maxAnisotropy ) &&
  153. static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
  154. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  155. if( ( !oldState || oldState->mDesc.samplers[i].mipLODBias != ssd.mipLODBias ) )
  156. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, ssd.mipLODBias);
  157. }
  158. }
  159. #undef SSF
  160. #undef SSW
  161. }