gfxGLUtils.h

Engine/source/gfx/gl/gfxGLUtils.h

More...

Classes:

class

Simple class which preserves a given GL integer.

Public Defines

define
_GET_BUFFER_BINDING(BINDING) BINDING == GL_ARRAY_BUFFER ? GL_ARRAY_BUFFER_BINDING : ( BINDING == GL_ELEMENT_ARRAY_BUFFER ?  GL_ELEMENT_ARRAY_BUFFER_BINDING : 0 )
define
_GET_TEXTURE_BINDING(binding) binding ==  ?  : (binding ==  ?   :  )
define
PRESERVE_1D_TEXTURE()  (preserve_, __LINE__) (, , ()glBindTexture)

ASSERT: Never call glActiveTexture for a "bind to modify" or in a PRESERVER_TEXTURE MACRO scope.

define
PRESERVE_2D_TEXTURE()  (preserve_, __LINE__) (, , ()glBindTexture)

Helper macro to preserve the current 2D texture binding.

define
PRESERVE_2D_TEXTURE_ARRAY()  (preserve_, __LINE__) (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, ()glBindTexture)
define
PRESERVE_3D_TEXTURE()  (preserve_, __LINE__) (, , ()glBindTexture)

Helper macro to preserve the current 3D texture binding.

define
PRESERVE_BUFFER(BINDING)  (preserve_, __LINE__) (BINDING, (BINDING), ()glBindBuffer)

Helper macro to preserve the current element array binding.

define
PRESERVE_CUBEMAP_ARRAY_TEXTURE()  (preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BINDING_CUBE_MAP_ARRAY, ()glBindTexture)
define
PRESERVE_CUBEMAP_TEXTURE()  (preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, ()glBindTexture)

Helper macro to preserve the current 3D texture binding.

define
PRESERVE_FRAMEBUFFER()  (preserve_, __LINE__) (GL_READ_FRAMEBUFFER, GL_READ_FRAMEBUFFER_BINDING, ()glBindFramebuffer);\
 (preserve2_, __LINE__) (GL_DRAW_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER_BINDING, ()glBindFramebuffer)
define
PRESERVE_INDEX_BUFFER()  (preserve_, __LINE__) (GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER_BINDING, ()glBindBuffer)

Helper macro to preserve the current element array binding.

define
PRESERVE_TEXTURE(binding)  (preserve_, __LINE__) (binding, (binding), ()glBindTexture)
define
PRESERVE_VERTEX_BUFFER()  (preserve_, __LINE__) (GL_ARRAY_BUFFER, GL_ARRAY_BUFFER_BINDING, ()glBindBuffer)

Helper macro to preserve the current VBO binding.

Public Functions

getCompressedSurfaceSize(GFXFormat format, U32 width, U32 height, U32 mipLevel)
getMaxMipmaps(U32 width, U32 height, U32 depth)
minificationFilter(U32 minFilter, U32 mipFilter, U32 )

Detailed Description

Public Defines

_GET_BUFFER_BINDING(BINDING) BINDING == GL_ARRAY_BUFFER ? GL_ARRAY_BUFFER_BINDING : ( BINDING == GL_ELEMENT_ARRAY_BUFFER ?  GL_ELEMENT_ARRAY_BUFFER_BINDING : 0 )
_GET_TEXTURE_BINDING(binding) binding ==  ?  : (binding ==  ?   :  )
CHECK_FRAMEBUFFER_STATUS() 
PRESERVE_1D_TEXTURE()  (preserve_, __LINE__) (, , ()glBindTexture)

ASSERT: Never call glActiveTexture for a "bind to modify" or in a PRESERVER_TEXTURE MACRO scope.

Helper macro to preserve the current 1D texture binding.

PRESERVE_2D_TEXTURE()  (preserve_, __LINE__) (, , ()glBindTexture)

Helper macro to preserve the current 2D texture binding.

PRESERVE_2D_TEXTURE_ARRAY()  (preserve_, __LINE__) (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, ()glBindTexture)
PRESERVE_3D_TEXTURE()  (preserve_, __LINE__) (, , ()glBindTexture)

Helper macro to preserve the current 3D texture binding.

PRESERVE_BUFFER(BINDING)  (preserve_, __LINE__) (BINDING, (BINDING), ()glBindBuffer)

Helper macro to preserve the current element array binding.

PRESERVE_CUBEMAP_ARRAY_TEXTURE()  (preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BINDING_CUBE_MAP_ARRAY, ()glBindTexture)
PRESERVE_CUBEMAP_TEXTURE()  (preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, ()glBindTexture)

Helper macro to preserve the current 3D texture binding.

PRESERVE_FRAMEBUFFER()  (preserve_, __LINE__) (GL_READ_FRAMEBUFFER, GL_READ_FRAMEBUFFER_BINDING, ()glBindFramebuffer);\
 (preserve2_, __LINE__) (GL_DRAW_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER_BINDING, ()glBindFramebuffer)
PRESERVE_INDEX_BUFFER()  (preserve_, __LINE__) (GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER_BINDING, ()glBindBuffer)

Helper macro to preserve the current element array binding.

PRESERVE_TEXTURE(binding)  (preserve_, __LINE__) (binding, (binding), ()glBindTexture)
PRESERVE_VERTEX_BUFFER()  (preserve_, __LINE__) (GL_ARRAY_BUFFER, GL_ARRAY_BUFFER_BINDING, ()glBindBuffer)

Helper macro to preserve the current VBO binding.

Public Functions

getCompressedSurfaceSize(GFXFormat format, U32 width, U32 height, U32 mipLevel)

getMaxMipmaps(U32 width, U32 height, U32 depth)

minificationFilter(U32 minFilter, U32 mipFilter, U32 )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef TORQUE_GFX_GL_GFXGLUTILS_H_
 25#define TORQUE_GFX_GL_GFXGLUTILS_H_
 26
 27#include "core/util/preprocessorHelpers.h"
 28#include "gfx/gl/gfxGLEnumTranslate.h"
 29#include "gfx/gl/gfxGLStateCache.h"
 30#include "gfx/bitmap/imageUtils.h"
 31
 32inline U32 getMaxMipmaps(U32 width, U32 height, U32 depth)
 33{
 34   return getMax( getBinLog2(depth), getMax(getBinLog2(width), getBinLog2(height))) + 1;
 35}
 36
 37inline GLenum minificationFilter(U32 minFilter, U32 mipFilter, U32 /*mipLevels*/)
 38{
 39   // the compiler should interpret this as array lookups
 40   switch( minFilter ) 
 41   {
 42      case GFXTextureFilterLinear:
 43         switch( mipFilter ) 
 44         {
 45         case GFXTextureFilterLinear:
 46            return GL_LINEAR_MIPMAP_LINEAR;
 47         case GFXTextureFilterPoint:
 48            return GL_LINEAR_MIPMAP_NEAREST;
 49         default: 
 50            return GL_LINEAR;
 51         }
 52      default:
 53         switch( mipFilter ) {
 54      case GFXTextureFilterLinear:
 55         return GL_NEAREST_MIPMAP_LINEAR;
 56      case GFXTextureFilterPoint:
 57         return GL_NEAREST_MIPMAP_NEAREST;
 58      default:
 59         return GL_NEAREST;
 60         }
 61   }
 62}
 63
 64//Get the surface size of a compressed mip map level - see ddsLoader.cpp
 65inline U32 getCompressedSurfaceSize(GFXFormat format,U32 width, U32 height, U32 mipLevel=0 )
 66{
 67   if(!ImageUtil::isCompressedFormat(format))
 68      return 0;
 69
 70   // Bump by the mip level.
 71   height = getMax(U32(1), height >> mipLevel);
 72   width = getMax(U32(1), width >> mipLevel);
 73
 74   U32 sizeMultiple = 0;
 75   if(format == GFXFormatBC1 || format == GFXFormatBC1_SRGB)
 76      sizeMultiple = 8;
 77   else
 78      sizeMultiple = 16;
 79
 80   return getMax(U32(1), width/4) * getMax(U32(1), height/4) * sizeMultiple;
 81}
 82
 83/// Simple class which preserves a given GL integer.
 84/// This class determines the integer to preserve on construction and restores 
 85/// it on destruction.
 86class GFXGLPreserveInteger
 87{
 88public:
 89   typedef void(STDCALL *BindFn)(GLenum, GLuint);
 90
 91   /// Preserve the integer.
 92   /// @param binding The binding which should be set on destruction.
 93   /// @param getBinding The parameter to be passed to glGetIntegerv to determine
 94   /// the integer to be preserved.
 95   /// @param binder The gl function to call to restore the integer.
 96   GFXGLPreserveInteger(GLenum binding, GLint getBinding, BindFn binder) :
 97      mBinding(binding), mPreserved(0), mBinder(binder)
 98   {
 99      AssertFatal(mBinder, "GFXGLPreserveInteger - Need a valid binder function");
100      mPreserved = GFXGL->getOpenglCache()->getCacheBinded(mBinding);
101#if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
102      GLint bindedOnOpenglDriver;
103      glGetIntegerv(getBinding, &bindedOnOpenglDriver);
104      AssertFatal( mPreserved == bindedOnOpenglDriver, "GFXGLPreserveInteger - GFXGLDevice/OpenGL mismatch on cache binded resource.");
105#endif
106   }
107   
108   /// Restores the integer.
109   ~GFXGLPreserveInteger()
110   {
111      mBinder(mBinding, mPreserved);
112   }
113
114private:
115   GLenum mBinding;
116   GLint mPreserved;
117   BindFn mBinder;
118};
119
120class GFXGLPreserveTexture
121{
122public:
123   typedef void(STDCALL *BindFn)(GLenum, GLuint);
124   
125   GFXGLPreserveTexture(GLenum binding, GLint getBinding, BindFn binder) :
126      mBinding(binding), mPreserved(0), mBinder(binder)
127   {
128      AssertFatal(mBinder, "GFXGLPreserveTexture - Need a valid binder function");
129      GFXGLDevice *gfx = GFXGL;
130      mPreserved = gfx->getOpenglCache()->getCacheBinded(mBinding);
131      mActiveTexture = gfx->getOpenglCache()->getCacheActiveTexture();
132#if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
133      GLint activeTextureOnOpenglDriver, bindedTextureOnOpenglDriver;
134      glGetIntegerv(getBinding, &bindedTextureOnOpenglDriver);
135      glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureOnOpenglDriver);
136      activeTextureOnOpenglDriver -= GL_TEXTURE0;
137      AssertFatal( mPreserved == bindedTextureOnOpenglDriver, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache binded resource.");
138      AssertFatal( activeTextureOnOpenglDriver == mActiveTexture, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache binded resource.");
139#endif
140   }
141   
142   /// Restores the texture.
143   ~GFXGLPreserveTexture()
144   {
145#if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
146      GLint activeTextureOnOpenglDriver;
147      glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureOnOpenglDriver);
148      activeTextureOnOpenglDriver -= GL_TEXTURE0;
149      GLint cacheActiveTexture = GFXGL->getOpenglCache()->getCacheActiveTexture();
150      AssertFatal( cacheActiveTexture == activeTextureOnOpenglDriver, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache ActiveTexture.");
151#endif
152      mBinder(mBinding, mPreserved);
153   }
154
155private:
156   GLenum mBinding;
157   GLint mPreserved;
158   BindFn mBinder;
159   S16 mActiveTexture;
160};
161
162/// Helper macro to preserve the current VBO binding.
163#define PRESERVE_VERTEX_BUFFER() \
164GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ARRAY_BUFFER, GL_ARRAY_BUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindBuffer)
165
166/// Helper macro to preserve the current element array binding.
167#define PRESERVE_INDEX_BUFFER() \
168GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindBuffer)
169
170#define _GET_BUFFER_BINDING( BINDING ) \
171BINDING == GL_ARRAY_BUFFER ? GL_ARRAY_BUFFER_BINDING : ( BINDING == GL_ELEMENT_ARRAY_BUFFER ?  GL_ELEMENT_ARRAY_BUFFER_BINDING : 0 )
172
173/// Helper macro to preserve the current element array binding.
174#define PRESERVE_BUFFER( BINDING ) \
175GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (BINDING, _GET_BUFFER_BINDING(BINDING), (GFXGLPreserveInteger::BindFn)glBindBuffer)
176
177/// ASSERT: Never call glActiveTexture for a "bind to modify" or in a PRESERVER_TEXTURE MACRO scope.
178
179/// Helper macro to preserve the current 1D texture binding.
180#define PRESERVE_1D_TEXTURE() \
181GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D, (GFXGLPreserveInteger::BindFn)glBindTexture)
182
183/// Helper macro to preserve the current 2D texture binding.
184#define PRESERVE_2D_TEXTURE() \
185GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, (GFXGLPreserveInteger::BindFn)glBindTexture)
186
187/// Helper macro to preserve the current 3D texture binding.
188#define PRESERVE_3D_TEXTURE() \
189GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D, (GFXGLPreserveInteger::BindFn)glBindTexture)
190
191/// Helper macro to preserve the current 3D texture binding.
192#define PRESERVE_CUBEMAP_TEXTURE() \
193GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, (GFXGLPreserveInteger::BindFn)glBindTexture)
194
195#define PRESERVE_CUBEMAP_ARRAY_TEXTURE() \
196GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BINDING_CUBE_MAP_ARRAY, (GFXGLPreserveInteger::BindFn)glBindTexture)
197
198#define PRESERVE_2D_TEXTURE_ARRAY() \
199GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, (GFXGLPreserveInteger::BindFn)glBindTexture)
200
201#define _GET_TEXTURE_BINDING(binding) \
202binding == GL_TEXTURE_2D ? GL_TEXTURE_BINDING_2D : (binding == GL_TEXTURE_3D ?  GL_TEXTURE_BINDING_3D : GL_TEXTURE_BINDING_1D )
203
204#define PRESERVE_TEXTURE(binding) \
205GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (binding, _GET_TEXTURE_BINDING(binding), (GFXGLPreserveInteger::BindFn)glBindTexture)
206
207#define PRESERVE_FRAMEBUFFER() \
208GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_READ_FRAMEBUFFER, GL_READ_FRAMEBUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindFramebuffer);\
209GFXGLPreserveInteger TORQUE_CONCAT(preserve2_, __LINE__) (GL_DRAW_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindFramebuffer)
210
211
212#if TORQUE_DEBUG
213
214    // Handy macro for checking the status of a framebuffer.  Framebuffers can fail in 
215    // all sorts of interesting ways, these are just the most common.  Further, no existing GL profiling 
216    // tool catches framebuffer errors when the framebuffer is created, so we actually need this.
217    #define CHECK_FRAMEBUFFER_STATUS()\
218    {\
219    GLenum status;\
220    status = glCheckFramebufferStatus(GL_FRAMEBUFFER);\
221    switch(status) {\
222    case GL_FRAMEBUFFER_COMPLETE:\
223    break;\
224    case GL_FRAMEBUFFER_UNSUPPORTED:\
225    AssertFatal(false, "Unsupported FBO");\
226    break;\
227    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:\
228    AssertFatal(false, "Incomplete FBO Attachment");\
229    break;\
230    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:\
231    AssertFatal(false, "Incomplete FBO Missing Attachment");\
232    break;\
233    case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:\
234    AssertFatal(false, "Incomplete FBO Draw buffer");\
235    break;\
236    case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:\
237    AssertFatal(false, "Incomplete FBO Read buffer");\
238    break;\
239    default:\
240    /* programming error; will fail on all hardware */\
241    AssertFatal(false, "Something really bad happened with an FBO");\
242    }\
243    }
244#else
245    #define CHECK_FRAMEBUFFER_STATUS()
246#endif //TORQUE_DEBUG
247
248#endif
249