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_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_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_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_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)

isCompressedFormat(GFXFormat format)

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