gfxGLUtils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef TORQUE_GFX_GL_GFXGLUTILS_H_
  23. #define TORQUE_GFX_GL_GFXGLUTILS_H_
  24. #include "core/util/preprocessorHelpers.h"
  25. #include "gfx/gl/gfxGLEnumTranslate.h"
  26. static inline GLenum minificationFilter(U32 minFilter, U32 mipFilter, U32 mipLevels)
  27. {
  28. if(mipLevels == 1)
  29. return GFXGLTextureFilter[minFilter];
  30. // the compiler should interpret this as array lookups
  31. switch( minFilter )
  32. {
  33. case GFXTextureFilterLinear:
  34. switch( mipFilter )
  35. {
  36. case GFXTextureFilterLinear:
  37. return GL_LINEAR_MIPMAP_LINEAR;
  38. case GFXTextureFilterPoint:
  39. return GL_LINEAR_MIPMAP_NEAREST;
  40. default:
  41. return GL_LINEAR;
  42. }
  43. default:
  44. switch( mipFilter ) {
  45. case GFXTextureFilterLinear:
  46. return GL_NEAREST_MIPMAP_LINEAR;
  47. case GFXTextureFilterPoint:
  48. return GL_NEAREST_MIPMAP_NEAREST;
  49. default:
  50. return GL_NEAREST;
  51. }
  52. }
  53. }
  54. /// Simple class which preserves a given GL integer.
  55. /// This class determines the integer to preserve on construction and restores
  56. /// it on destruction.
  57. class GFXGLPreserveInteger
  58. {
  59. public:
  60. typedef void(*BindFn)(GLenum, GLuint);
  61. /// Preserve the integer.
  62. /// @param binding The binding which should be set on destruction.
  63. /// @param getBinding The parameter to be passed to glGetIntegerv to determine
  64. /// the integer to be preserved.
  65. /// @param binder The gl function to call to restore the integer.
  66. GFXGLPreserveInteger(GLenum binding, GLint getBinding, BindFn binder) :
  67. mBinding(binding), mPreserved(0), mBinder(binder)
  68. {
  69. AssertFatal(mBinder, "GFXGLPreserveInteger - Need a valid binder function");
  70. glGetIntegerv(getBinding, &mPreserved);
  71. }
  72. /// Restores the integer.
  73. ~GFXGLPreserveInteger()
  74. {
  75. mBinder(mBinding, mPreserved);
  76. }
  77. private:
  78. GLenum mBinding;
  79. GLint mPreserved;
  80. BindFn mBinder;
  81. };
  82. /// Helper macro to preserve the current VBO binding.
  83. #define PRESERVE_VERTEX_BUFFER() \
  84. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ARRAY_BUFFER, GL_ARRAY_BUFFER_BINDING, glBindBuffer)
  85. /// Helper macro to preserve the current element array binding.
  86. #define PRESERVE_INDEX_BUFFER() \
  87. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER_BINDING, glBindBuffer)
  88. /// Helper macro to preserve the current 2D texture binding.
  89. #define PRESERVE_2D_TEXTURE() \
  90. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, glBindTexture)
  91. /// Helper macro to preserve the current 3D texture binding.
  92. #define PRESERVE_3D_TEXTURE() \
  93. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D, glBindTexture)
  94. #define PRESERVE_FRAMEBUFFER() \
  95. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_READ_FRAMEBUFFER_EXT, GL_READ_FRAMEBUFFER_BINDING_EXT, glBindFramebufferEXT);\
  96. GFXGLPreserveInteger TORQUE_CONCAT(preserve2_, __LINE__) (GL_DRAW_FRAMEBUFFER_EXT, GL_DRAW_FRAMEBUFFER_BINDING_EXT, glBindFramebufferEXT)
  97. #endif