gfxGLStateBlock.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "core/crc.h"
  28. namespace DictHash
  29. {
  30. inline U32 hash(const GFXSamplerStateDesc &data)
  31. {
  32. return CRC::calculateCRC(&data, sizeof(GFXSamplerStateDesc));;
  33. }
  34. }
  35. GFXGLStateBlock::GFXGLStateBlock(const GFXStateBlockDesc& desc) :
  36. mDesc(desc),
  37. mCachedHashValue(desc.getHashValue())
  38. {
  39. static Map<GFXSamplerStateDesc, U32> mSamplersMap;
  40. for(int i = 0; i < GFX_TEXTURE_STAGE_COUNT; ++i)
  41. {
  42. GLuint &id = mSamplerObjects[i];
  43. GFXSamplerStateDesc &ssd = mDesc.samplers[i];
  44. Map<GFXSamplerStateDesc, U32>::Iterator itr = mSamplersMap.find(ssd);
  45. if(itr == mSamplersMap.end())
  46. {
  47. glGenSamplers(1, &id);
  48. glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 1) );
  49. glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
  50. glSamplerParameteri(id, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
  51. glSamplerParameteri(id, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
  52. glSamplerParameteri(id, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
  53. //compare modes
  54. const bool comparison = ssd.samplerFunc != GFXCmpNever;
  55. glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, comparison ? GL_COMPARE_R_TO_TEXTURE_ARB : GL_NONE );
  56. glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, GFXGLCmpFunc[ssd.samplerFunc]);
  57. if (static_cast< GFXGLDevice* >(GFX)->supportsAnisotropic())
  58. glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
  59. mSamplersMap[ssd] = id;
  60. }
  61. else
  62. id = itr->value;
  63. }
  64. }
  65. GFXGLStateBlock::~GFXGLStateBlock()
  66. {
  67. }
  68. /// Returns the hash value of the desc that created this block
  69. U32 GFXGLStateBlock::getHashValue() const
  70. {
  71. return mCachedHashValue;
  72. }
  73. /// Returns a GFXStateBlockDesc that this block represents
  74. const GFXStateBlockDesc& GFXGLStateBlock::getDesc() const
  75. {
  76. return mDesc;
  77. }
  78. /// Called by OpenGL device to active this state block.
  79. /// @param oldState The current state, used to make sure we don't set redundant states on the device. Pass NULL to reset all states.
  80. void GFXGLStateBlock::activate(const GFXGLStateBlock* oldState)
  81. {
  82. PROFILE_SCOPE(GFXGLStateBlock_Activate);
  83. // Big scary warning copied from Apple docs
  84. // http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_performance/chapter_13_section_2.html#//apple_ref/doc/uid/TP40001987-CH213-SW12
  85. // Don't set a state that's already set. Once a feature is enabled, it does not need to be enabled again.
  86. // Calling an enable function more than once does nothing except waste time because OpenGL does not check
  87. // the state of a feature when you call glEnable or glDisable. For instance, if you call glEnable(GL_LIGHTING)
  88. // more than once, OpenGL does not check to see if the lighting state is already enabled. It simply updates
  89. // the state value even if that value is identical to the current value.
  90. #define STATE_CHANGE(state) (!oldState || oldState->mDesc.state != mDesc.state)
  91. #define TOGGLE_STATE(state, enum) if(mDesc.state) { glEnable(enum); } else { glDisable(enum); }
  92. #define CHECK_TOGGLE_STATE(state, enum) if(!oldState || oldState->mDesc.state != mDesc.state) { if(mDesc.state) { glEnable(enum); } else { glDisable(enum); }}
  93. // Blending
  94. CHECK_TOGGLE_STATE(blendEnable, GL_BLEND);
  95. if(STATE_CHANGE(blendSrc) || STATE_CHANGE(blendDest))
  96. glBlendFunc(GFXGLBlend[mDesc.blendSrc], GFXGLBlend[mDesc.blendDest]);
  97. if(STATE_CHANGE(blendOp))
  98. glBlendEquation(GFXGLBlendOp[mDesc.blendOp]);
  99. if (mDesc.separateAlphaBlendEnable == true)
  100. {
  101. if (STATE_CHANGE(separateAlphaBlendSrc) || STATE_CHANGE(separateAlphaBlendDest))
  102. glBlendFuncSeparate(GFXGLBlend[mDesc.blendSrc], GFXGLBlend[mDesc.blendDest], GFXGLBlend[mDesc.separateAlphaBlendSrc], GFXGLBlend[mDesc.separateAlphaBlendDest]);
  103. if (STATE_CHANGE(separateAlphaBlendOp))
  104. glBlendEquationSeparate(GFXGLBlendOp[mDesc.blendOp], GFXGLBlendOp[mDesc.separateAlphaBlendOp]);
  105. }
  106. // Color write masks
  107. if(STATE_CHANGE(colorWriteRed) || STATE_CHANGE(colorWriteBlue) || STATE_CHANGE(colorWriteGreen) || STATE_CHANGE(colorWriteAlpha))
  108. glColorMask(mDesc.colorWriteRed, mDesc.colorWriteBlue, mDesc.colorWriteGreen, mDesc.colorWriteAlpha);
  109. // Culling
  110. if(STATE_CHANGE(cullMode))
  111. {
  112. TOGGLE_STATE(cullMode, GL_CULL_FACE);
  113. glCullFace(GFXGLCullMode[mDesc.cullMode]);
  114. }
  115. // Depth
  116. CHECK_TOGGLE_STATE(zEnable, GL_DEPTH_TEST);
  117. if(STATE_CHANGE(zFunc))
  118. glDepthFunc(GFXGLCmpFunc[mDesc.zFunc]);
  119. if (STATE_CHANGE(zBias))
  120. {
  121. if (mDesc.zBias == 0)
  122. {
  123. glDisable(GL_POLYGON_OFFSET_FILL);
  124. }
  125. else
  126. {
  127. //this assumes 24bit depth
  128. const F32 depthMul = F32((1 << 24) - 1);
  129. glEnable(GL_POLYGON_OFFSET_FILL);
  130. glPolygonOffset(mDesc.zSlopeBias, mDesc.zBias * depthMul);
  131. }
  132. }
  133. if(STATE_CHANGE(zWriteEnable))
  134. glDepthMask(mDesc.zWriteEnable);
  135. // Stencil
  136. CHECK_TOGGLE_STATE(stencilEnable, GL_STENCIL_TEST);
  137. if(STATE_CHANGE(stencilFunc) || STATE_CHANGE(stencilRef) || STATE_CHANGE(stencilMask))
  138. glStencilFunc(GFXGLCmpFunc[mDesc.stencilFunc], mDesc.stencilRef, mDesc.stencilMask);
  139. if(STATE_CHANGE(stencilFailOp) || STATE_CHANGE(stencilZFailOp) || STATE_CHANGE(stencilPassOp))
  140. glStencilOp(GFXGLStencilOp[mDesc.stencilFailOp], GFXGLStencilOp[mDesc.stencilZFailOp], GFXGLStencilOp[mDesc.stencilPassOp]);
  141. if(STATE_CHANGE(stencilWriteMask))
  142. glStencilMask(mDesc.stencilWriteMask);
  143. if(STATE_CHANGE(fillMode))
  144. glPolygonMode(GL_FRONT_AND_BACK, GFXGLFillMode[mDesc.fillMode]);
  145. #undef CHECK_STATE
  146. #undef TOGGLE_STATE
  147. #undef CHECK_TOGGLE_STATE
  148. //sampler objects
  149. for (U32 i = 0; i < getMin(getOwningDevice()->getNumSamplers(), (U32) GFX_TEXTURE_STAGE_COUNT); i++)
  150. {
  151. if(!oldState || oldState->mSamplerObjects[i] != mSamplerObjects[i])
  152. glBindSampler(i, mSamplerObjects[i] );
  153. }
  154. // TODO: states added for detail blend
  155. }