gfxGLStateBlock.cpp 7.1 KB

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