gfxGLStateBlock.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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( !gglHasExtension(ARB_sampler_objects) )
  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. // Big scary warning copied from Apple docs
  81. // http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_performance/chapter_13_section_2.html#//apple_ref/doc/uid/TP40001987-CH213-SW12
  82. // Don't set a state that's already set. Once a feature is enabled, it does not need to be enabled again.
  83. // Calling an enable function more than once does nothing except waste time because OpenGL does not check
  84. // the state of a feature when you call glEnable or glDisable. For instance, if you call glEnable(GL_LIGHTING)
  85. // more than once, OpenGL does not check to see if the lighting state is already enabled. It simply updates
  86. // the state value even if that value is identical to the current value.
  87. #define STATE_CHANGE(state) (!oldState || oldState->mDesc.state != mDesc.state)
  88. #define TOGGLE_STATE(state, enum) if(mDesc.state) glEnable(enum); else glDisable(enum)
  89. #define CHECK_TOGGLE_STATE(state, enum) if(!oldState || oldState->mDesc.state != mDesc.state) if(mDesc.state) glEnable(enum); else glDisable(enum)
  90. // Blending
  91. CHECK_TOGGLE_STATE(blendEnable, GL_BLEND);
  92. if(STATE_CHANGE(blendSrc) || STATE_CHANGE(blendDest))
  93. glBlendFunc(GFXGLBlend[mDesc.blendSrc], GFXGLBlend[mDesc.blendDest]);
  94. if(STATE_CHANGE(blendOp))
  95. glBlendEquation(GFXGLBlendOp[mDesc.blendOp]);
  96. // Color write masks
  97. if(STATE_CHANGE(colorWriteRed) || STATE_CHANGE(colorWriteBlue) || STATE_CHANGE(colorWriteGreen) || STATE_CHANGE(colorWriteAlpha))
  98. glColorMask(mDesc.colorWriteRed, mDesc.colorWriteBlue, mDesc.colorWriteGreen, mDesc.colorWriteAlpha);
  99. // Culling
  100. if(STATE_CHANGE(cullMode))
  101. {
  102. TOGGLE_STATE(cullMode, GL_CULL_FACE);
  103. glCullFace(GFXGLCullMode[mDesc.cullMode]);
  104. }
  105. // Depth
  106. CHECK_TOGGLE_STATE(zEnable, GL_DEPTH_TEST);
  107. if(STATE_CHANGE(zFunc))
  108. glDepthFunc(GFXGLCmpFunc[mDesc.zFunc]);
  109. if(STATE_CHANGE(zBias))
  110. {
  111. if (mDesc.zBias == 0)
  112. {
  113. glDisable(GL_POLYGON_OFFSET_FILL);
  114. } else {
  115. F32 bias = mDesc.zBias * 10000.0f;
  116. glEnable(GL_POLYGON_OFFSET_FILL);
  117. glPolygonOffset(bias, bias);
  118. }
  119. }
  120. if(STATE_CHANGE(zWriteEnable))
  121. glDepthMask(mDesc.zWriteEnable);
  122. // Stencil
  123. CHECK_TOGGLE_STATE(stencilEnable, GL_STENCIL_TEST);
  124. if(STATE_CHANGE(stencilFunc) || STATE_CHANGE(stencilRef) || STATE_CHANGE(stencilMask))
  125. glStencilFunc(GFXGLCmpFunc[mDesc.stencilFunc], mDesc.stencilRef, mDesc.stencilMask);
  126. if(STATE_CHANGE(stencilFailOp) || STATE_CHANGE(stencilZFailOp) || STATE_CHANGE(stencilPassOp))
  127. glStencilOp(GFXGLStencilOp[mDesc.stencilFailOp], GFXGLStencilOp[mDesc.stencilZFailOp], GFXGLStencilOp[mDesc.stencilPassOp]);
  128. if(STATE_CHANGE(stencilWriteMask))
  129. glStencilMask(mDesc.stencilWriteMask);
  130. if(STATE_CHANGE(fillMode))
  131. glPolygonMode(GL_FRONT_AND_BACK, GFXGLFillMode[mDesc.fillMode]);
  132. #undef CHECK_STATE
  133. #undef TOGGLE_STATE
  134. #undef CHECK_TOGGLE_STATE
  135. //sampler objects
  136. if( gglHasExtension(ARB_sampler_objects) )
  137. {
  138. for (U32 i = 0; i < getMin(getOwningDevice()->getNumSamplers(), (U32) TEXTURE_STAGE_COUNT); i++)
  139. {
  140. if(!oldState || oldState->mSamplerObjects[i] != mSamplerObjects[i])
  141. glBindSampler(i, mSamplerObjects[i] );
  142. }
  143. }
  144. // TODO: states added for detail blend
  145. }