gfxGLStateBlock.cpp 7.8 KB

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