gfxGLUtils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include "gfx/gl/gfxGLStateCache.h"
  27. inline U32 getMaxMipmaps(U32 width, U32 height, U32 depth)
  28. {
  29. return getMax( getBinLog2(depth), getMax(getBinLog2(width), getBinLog2(height)));
  30. }
  31. inline GLenum minificationFilter(U32 minFilter, U32 mipFilter, U32 /*mipLevels*/)
  32. {
  33. // the compiler should interpret this as array lookups
  34. switch( minFilter )
  35. {
  36. case GFXTextureFilterLinear:
  37. switch( mipFilter )
  38. {
  39. case GFXTextureFilterLinear:
  40. return GL_LINEAR_MIPMAP_LINEAR;
  41. case GFXTextureFilterPoint:
  42. return GL_LINEAR_MIPMAP_NEAREST;
  43. default:
  44. return GL_LINEAR;
  45. }
  46. default:
  47. switch( mipFilter ) {
  48. case GFXTextureFilterLinear:
  49. return GL_NEAREST_MIPMAP_LINEAR;
  50. case GFXTextureFilterPoint:
  51. return GL_NEAREST_MIPMAP_NEAREST;
  52. default:
  53. return GL_NEAREST;
  54. }
  55. }
  56. }
  57. // Check if format is compressed format.
  58. // Even though dxt2/4 are not supported, they are included because they are a compressed format.
  59. // Assert checks on supported formats are done elsewhere.
  60. inline bool isCompressedFormat( GFXFormat format )
  61. {
  62. bool compressed = false;
  63. if(format == GFXFormatDXT1 || format == GFXFormatDXT2
  64. || format == GFXFormatDXT3
  65. || format == GFXFormatDXT4
  66. || format == GFXFormatDXT5 )
  67. {
  68. compressed = true;
  69. }
  70. return compressed;
  71. }
  72. //Get the surface size of a compressed mip map level - see ddsLoader.cpp
  73. inline U32 getCompressedSurfaceSize(GFXFormat format,U32 width, U32 height, U32 mipLevel=0 )
  74. {
  75. if(!isCompressedFormat(format))
  76. return 0;
  77. // Bump by the mip level.
  78. height = getMax(U32(1), height >> mipLevel);
  79. width = getMax(U32(1), width >> mipLevel);
  80. U32 sizeMultiple = 0;
  81. if(format == GFXFormatDXT1)
  82. sizeMultiple = 8;
  83. else
  84. sizeMultiple = 16;
  85. return getMax(U32(1), width/4) * getMax(U32(1), height/4) * sizeMultiple;
  86. }
  87. /// Simple class which preserves a given GL integer.
  88. /// This class determines the integer to preserve on construction and restores
  89. /// it on destruction.
  90. class GFXGLPreserveInteger
  91. {
  92. public:
  93. typedef void(STDCALL *BindFn)(GLenum, GLuint);
  94. /// Preserve the integer.
  95. /// @param binding The binding which should be set on destruction.
  96. /// @param getBinding The parameter to be passed to glGetIntegerv to determine
  97. /// the integer to be preserved.
  98. /// @param binder The gl function to call to restore the integer.
  99. GFXGLPreserveInteger(GLenum binding, GLint getBinding, BindFn binder) :
  100. mBinding(binding), mPreserved(0), mBinder(binder)
  101. {
  102. AssertFatal(mBinder, "GFXGLPreserveInteger - Need a valid binder function");
  103. mPreserved = GFXGL->getOpenglCache()->getCacheBinded(mBinding);
  104. #if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
  105. GLint bindedOnOpenglDriver;
  106. glGetIntegerv(getBinding, &bindedOnOpenglDriver);
  107. AssertFatal( mPreserved == bindedOnOpenglDriver, "GFXGLPreserveInteger - GFXGLDevice/OpenGL mismatch on cache binded resource.");
  108. #endif
  109. }
  110. /// Restores the integer.
  111. ~GFXGLPreserveInteger()
  112. {
  113. mBinder(mBinding, mPreserved);
  114. }
  115. private:
  116. GLenum mBinding;
  117. GLint mPreserved;
  118. BindFn mBinder;
  119. };
  120. class GFXGLPreserveTexture
  121. {
  122. public:
  123. typedef void(STDCALL *BindFn)(GLenum, GLuint);
  124. GFXGLPreserveTexture(GLenum binding, GLint getBinding, BindFn binder) :
  125. mBinding(binding), mPreserved(0), mBinder(binder)
  126. {
  127. AssertFatal(mBinder, "GFXGLPreserveTexture - Need a valid binder function");
  128. GFXGLDevice *gfx = GFXGL;
  129. mPreserved = gfx->getOpenglCache()->getCacheBinded(mBinding);
  130. mActiveTexture = gfx->getOpenglCache()->getCacheActiveTexture();
  131. #if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
  132. GLint activeTextureOnOpenglDriver, bindedTextureOnOpenglDriver;
  133. glGetIntegerv(getBinding, &bindedTextureOnOpenglDriver);
  134. glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureOnOpenglDriver);
  135. activeTextureOnOpenglDriver -= GL_TEXTURE0;
  136. AssertFatal( mPreserved == bindedTextureOnOpenglDriver, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache binded resource.");
  137. AssertFatal( activeTextureOnOpenglDriver == mActiveTexture, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache binded resource.");
  138. #endif
  139. }
  140. /// Restores the texture.
  141. ~GFXGLPreserveTexture()
  142. {
  143. #if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
  144. GLint activeTextureOnOpenglDriver;
  145. glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureOnOpenglDriver);
  146. activeTextureOnOpenglDriver -= GL_TEXTURE0;
  147. GLint cacheActiveTexture = GFXGL->getOpenglCache()->getCacheActiveTexture();
  148. AssertFatal( cacheActiveTexture == activeTextureOnOpenglDriver, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache ActiveTexture.");
  149. #endif
  150. mBinder(mBinding, mPreserved);
  151. }
  152. private:
  153. GLenum mBinding;
  154. GLint mPreserved;
  155. BindFn mBinder;
  156. S16 mActiveTexture;
  157. };
  158. /// Helper macro to preserve the current VBO binding.
  159. #define PRESERVE_VERTEX_BUFFER() \
  160. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ARRAY_BUFFER, GL_ARRAY_BUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindBuffer)
  161. /// Helper macro to preserve the current element array binding.
  162. #define PRESERVE_INDEX_BUFFER() \
  163. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindBuffer)
  164. #define _GET_BUFFER_BINDING( BINDING ) \
  165. BINDING == GL_ARRAY_BUFFER ? GL_ARRAY_BUFFER_BINDING : ( BINDING == GL_ELEMENT_ARRAY_BUFFER ? GL_ELEMENT_ARRAY_BUFFER_BINDING : 0 )
  166. /// Helper macro to preserve the current element array binding.
  167. #define PRESERVE_BUFFER( BINDING ) \
  168. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (BINDING, _GET_BUFFER_BINDING(BINDING), (GFXGLPreserveInteger::BindFn)glBindBuffer)
  169. /// ASSERT: Never call glActiveTexture for a "bind to modify" or in a PRESERVER_TEXTURE MACRO scope.
  170. /// Helper macro to preserve the current 1D texture binding.
  171. #define PRESERVE_1D_TEXTURE() \
  172. GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D, (GFXGLPreserveInteger::BindFn)glBindTexture)
  173. /// Helper macro to preserve the current 2D texture binding.
  174. #define PRESERVE_2D_TEXTURE() \
  175. GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, (GFXGLPreserveInteger::BindFn)glBindTexture)
  176. /// Helper macro to preserve the current 3D texture binding.
  177. #define PRESERVE_3D_TEXTURE() \
  178. GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D, (GFXGLPreserveInteger::BindFn)glBindTexture)
  179. /// Helper macro to preserve the current 3D texture binding.
  180. #define PRESERVE_CUBEMAP_TEXTURE() \
  181. GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, (GFXGLPreserveInteger::BindFn)glBindTexture)
  182. #define _GET_TEXTURE_BINDING(binding) \
  183. binding == GL_TEXTURE_2D ? GL_TEXTURE_BINDING_2D : (binding == GL_TEXTURE_3D ? GL_TEXTURE_BINDING_3D : GL_TEXTURE_BINDING_1D )
  184. #define PRESERVE_TEXTURE(binding) \
  185. GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (binding, _GET_TEXTURE_BINDING(binding), (GFXGLPreserveInteger::BindFn)glBindTexture)
  186. #define PRESERVE_FRAMEBUFFER() \
  187. GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_READ_FRAMEBUFFER, GL_READ_FRAMEBUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindFramebuffer);\
  188. GFXGLPreserveInteger TORQUE_CONCAT(preserve2_, __LINE__) (GL_DRAW_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindFramebuffer)
  189. #if TORQUE_DEBUG
  190. // Handy macro for checking the status of a framebuffer. Framebuffers can fail in
  191. // all sorts of interesting ways, these are just the most common. Further, no existing GL profiling
  192. // tool catches framebuffer errors when the framebuffer is created, so we actually need this.
  193. #define CHECK_FRAMEBUFFER_STATUS()\
  194. {\
  195. GLenum status;\
  196. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);\
  197. switch(status) {\
  198. case GL_FRAMEBUFFER_COMPLETE:\
  199. break;\
  200. case GL_FRAMEBUFFER_UNSUPPORTED:\
  201. AssertFatal(false, "Unsupported FBO");\
  202. break;\
  203. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:\
  204. AssertFatal(false, "Incomplete FBO Attachment");\
  205. break;\
  206. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:\
  207. AssertFatal(false, "Incomplete FBO Missing Attachment");\
  208. break;\
  209. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:\
  210. AssertFatal(false, "Incomplete FBO Draw buffer");\
  211. break;\
  212. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:\
  213. AssertFatal(false, "Incomplete FBO Read buffer");\
  214. break;\
  215. default:\
  216. /* programming error; will fail on all hardware */\
  217. AssertFatal(false, "Something really bad happened with an FBO");\
  218. }\
  219. }
  220. #else
  221. #define CHECK_FRAMEBUFFER_STATUS()
  222. #endif //TORQUE_DEBUG
  223. #endif