Torque3D Documentation / _generateds / gfxGLStateCache.h

gfxGLStateCache.h

Engine/source/gfx/gl/gfxGLStateCache.h

More...

Classes:

class

GFXGLStateCache store OpenGL state to avoid performance penalities of glGet* calls GL_TEXTURE_1D/2D/3D, GL_FRAMEBUFFER, GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER.

Detailed Description

  1
  2#ifndef GFX_GL_STATE_CACHE
  3#define GFX_GL_STATE_CACHE
  4
  5
  6/// GFXGLStateCache store OpenGL state to avoid performance penalities of glGet* calls
  7/// GL_TEXTURE_1D/2D/3D, GL_FRAMEBUFFER, GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER
  8class GFXGLStateCache
  9{
 10public:
 11   GFXGLStateCache()
 12   {      
 13      mActiveTexture = 0;      
 14      mBindedVBO = 0;
 15      mBindedIBO = 0;
 16      mBindedFBO_W = 0;
 17      mBindedFBO_R = 0;
 18      mVertexAttribActive = 0;
 19   }
 20
 21   class TextureUnit
 22   {
 23   public:
 24      TextureUnit() :  mTexture1D(0), mTexture2D(0), mTexture3D(0), mTextureCube(0), mTextureCubeArray(0), mTextureArray(0)
 25      {
 26
 27      }
 28      GLuint mTexture1D, mTexture2D, mTexture3D, mTextureCube, mTextureCubeArray, mTextureArray;
 29   };
 30
 31   /// after glBindTexture
 32   void setCacheBindedTex(U32 texUnit, GLenum biding, GLuint handle)
 33   { 
 34      mActiveTexture = texUnit;
 35      switch (biding)
 36      {
 37      case GL_TEXTURE_2D:
 38         mTextureUnits[mActiveTexture].mTexture2D = handle;
 39         break;
 40      case GL_TEXTURE_3D:
 41         mTextureUnits[mActiveTexture].mTexture3D = handle;
 42         break;
 43      case GL_TEXTURE_1D:
 44         mTextureUnits[mActiveTexture].mTexture1D = handle;
 45         break;
 46      case GL_TEXTURE_CUBE_MAP:
 47         mTextureUnits[mActiveTexture].mTextureCube = handle;
 48         break;
 49      case GL_TEXTURE_CUBE_MAP_ARRAY:
 50         mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
 51         break;
 52      case GL_TEXTURE_2D_ARRAY:
 53         mTextureUnits[mActiveTexture].mTextureArray = handle;
 54         break;
 55      default:
 56         AssertFatal(0, avar("GFXGLStateCache::setCacheBindedTex - binding (%x) not supported.", biding) );
 57         return;
 58      }
 59   }
 60
 61   /// after opengl object binded
 62   void setCacheBinded(GLenum biding, GLuint handle) 
 63   { 
 64      switch (biding)
 65      {
 66      case GL_TEXTURE_2D:
 67         mTextureUnits[mActiveTexture].mTexture2D = handle;
 68         break;
 69      case GL_TEXTURE_3D:
 70         mTextureUnits[mActiveTexture].mTexture3D = handle;
 71         break;
 72      case GL_TEXTURE_1D:
 73         mTextureUnits[mActiveTexture].mTexture1D = handle;
 74         break;
 75      case GL_TEXTURE_CUBE_MAP:
 76         mTextureUnits[mActiveTexture].mTextureCube = handle;
 77         break;
 78      case GL_TEXTURE_CUBE_MAP_ARRAY:
 79         mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
 80         break;
 81      case GL_TEXTURE_2D_ARRAY:
 82         mTextureUnits[mActiveTexture].mTextureArray = handle;
 83         break;
 84      case GL_FRAMEBUFFER:
 85         mBindedFBO_W = mBindedFBO_R = handle;
 86         break;
 87      case GL_DRAW_FRAMEBUFFER:
 88         mBindedFBO_W = handle;
 89         break;
 90      case GL_READ_FRAMEBUFFER:
 91         mBindedFBO_R = handle;
 92         break;
 93      case GL_ARRAY_BUFFER:
 94         mBindedVBO = handle;
 95         break;
 96      case GL_ELEMENT_ARRAY_BUFFER:
 97         mBindedIBO = handle;
 98         break;
 99      default:
100         AssertFatal(0, avar("GFXGLStateCache::setCacheBinded - binding (%x) not supported.", biding) );
101         break;
102      }
103   }
104
105   GLuint getCacheBinded(GLenum biding) const
106   {
107      switch (biding)
108      {
109      case GL_TEXTURE_2D:
110         return mTextureUnits[mActiveTexture].mTexture2D;
111      case GL_TEXTURE_3D:
112         return mTextureUnits[mActiveTexture].mTexture3D;
113      case GL_TEXTURE_1D:
114         return mTextureUnits[mActiveTexture].mTexture1D;
115      case GL_TEXTURE_CUBE_MAP:
116         return mTextureUnits[mActiveTexture].mTextureCube;
117      case GL_TEXTURE_CUBE_MAP_ARRAY:
118         return mTextureUnits[mActiveTexture].mTextureCubeArray;
119      case GL_TEXTURE_2D_ARRAY:
120         return mTextureUnits[mActiveTexture].mTextureArray;
121      case GL_DRAW_FRAMEBUFFER:
122         return mBindedFBO_W;
123      case GL_READ_FRAMEBUFFER:
124         return mBindedFBO_R;
125      case GL_ARRAY_BUFFER:
126         return mBindedVBO;
127      case GL_ELEMENT_ARRAY_BUFFER:
128         return mBindedIBO;
129      default:
130         AssertFatal(0, avar("GFXGLStateCache::getCacheBinded - binding (%x) not supported.", biding) );
131         return 0;
132      }
133   }
134
135   /// after glActiveTexture
136   void setCacheActiveTexture(U32 unit) { mActiveTexture = unit; }
137   U32 getCacheActiveTexture() const { return mActiveTexture;  }
138
139   /// for cache glEnableVertexAttribArray / glDisableVertexAttribArray
140   void setCacheVertexAttribActive(U32 activeMask) { mVertexAttribActive = activeMask; }
141   U32 getCacheVertexAttribActive() const { return mVertexAttribActive;  }
142
143protected:   
144   GLuint mActiveTexture, mBindedVBO, mBindedIBO, mBindedFBO_W, mBindedFBO_R;
145   TextureUnit mTextureUnits[GFX_TEXTURE_STAGE_COUNT];
146   U32 mVertexAttribActive;
147};
148
149
150#endif
151