1 /* 2 ----------------------------------------------------------------------------- 3 This source file is part of OGRE 4 (Object-oriented Graphics Rendering Engine) 5 For the latest info, see http://www.ogre3d.org/ 6 7 Copyright (c) 2000-2014 Torus Knot Software Ltd 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ----------------------------------------------------------------------------- 27 */ 28 29 #ifndef __GL3PlusStateCacheManager_H__ 30 #define __GL3PlusStateCacheManager_H__ 31 32 #include "OgreGL3PlusPrerequisites.h" 33 #include "OgreGLStateCacheManagerCommon.h" 34 #include "OgreStdHeaders.h" 35 #include "OgreIteratorWrappers.h" 36 37 namespace Ogre 38 { 39 class _OgreGL3PlusExport GL3PlusStateCacheManager : public GLStateCacheManagerCommon 40 { 41 protected: 42 struct TextureUnitParams 43 { 44 TexParameteriMap mTexParameteriMap; 45 }; 46 47 typedef std::unordered_map<GLuint, TextureUnitParams> TexUnitsMap; 48 49 /* These variables are used for caching OpenGL state. 50 They are cached because state changes can be quite expensive, 51 which is especially important on mobile or embedded systems. 52 */ 53 54 /// Stores textures currently bound to each texture stage 55 std::unordered_map <GLenum, GLuint> mBoundTextures; 56 57 struct TexGenParams 58 { 59 std::set<GLenum> mEnabled; 60 }; 61 /// Stores the currently enabled texcoord generation types per texture unit 62 std::unordered_map <GLenum, TexGenParams> mTextureCoordGen; 63 64 /// Stores the currently bound draw frame buffer value 65 GLuint mActiveDrawFrameBuffer; 66 /// Stores the currently bound read frame buffer value 67 GLuint mActiveReadFrameBuffer; 68 /// Stores the currently bound vertex array object 69 GLuint mActiveVertexArray; 70 /// A map of texture parameters for each texture unit 71 TexUnitsMap mTexUnitsMap; 72 /// Stores the current polygon rendering mode 73 GLenum mPolygonMode; 74 /// Stores the last bound texture id 75 GLuint mLastBoundTexID; 76 /// Stores the currently bound separable program pipeline 77 GLuint mActiveProgramPipeline; 78 79 GLfloat mPointSize; 80 public: 81 GL3PlusStateCacheManager(void); 82 83 /// See GL3PlusStateCacheManager.initializeCache. 84 void initializeCache(); 85 86 /** Clears all cached values 87 */ 88 void clearCache(); 89 90 91 /** Bind an OpenGL frame buffer. 92 @param target The buffer target. 93 @param buffer The buffer ID. 94 @param force Optional parameter to force an update. 95 */ 96 void bindGLFrameBuffer(GLenum target,GLuint buffer, bool force = false); 97 98 /** Bind an OpenGL frame buffer. 99 @param buffer The buffer ID. 100 @param force Optional parameter to force an update. 101 */ 102 void bindGLRenderBuffer(GLuint buffer, bool force = false); 103 104 /** Bind an OpenGL buffer of any type. 105 @param target The buffer target. 106 @param buffer The buffer ID. 107 @param force Optional parameter to force an update. 108 */ 109 void bindGLBuffer(GLenum target, GLuint buffer, bool force = false); 110 111 /** Delete an OpenGL frame buffer. 112 @param target The buffer target. 113 @param buffer The buffer ID. 114 */ 115 void deleteGLFrameBuffer(GLenum target, GLuint buffer); 116 117 /** Delete an OpenGL render buffer. 118 @param buffer The buffer ID. 119 */ 120 void deleteGLRenderBuffer(GLuint buffer); 121 /** Delete an OpenGL buffer of any type. 122 @param target The buffer target. 123 @param buffer The buffer ID. 124 */ 125 void deleteGLBuffer(GLenum target, GLuint buffer); 126 127 /** Bind an OpenGL Vertex array object. 128 @param vao The vertex array object ID. 129 */ 130 void bindGLVertexArray(GLuint vao); 131 132 /** Bind an OpenGL texture of any type. 133 @param target The texture target. 134 @param texture The texture ID. 135 */ 136 void bindGLTexture(GLenum target, GLuint texture); 137 138 /** Invalidates the state associated with a particular texture ID. 139 @param texture The texture ID. 140 */ 141 void invalidateStateForTexture(GLuint texture); 142 143 /** Sets an integer parameter value per texture target. 144 @param target The texture target. 145 @param pname The parameter name. 146 @param param The parameter value. 147 */ 148 void setTexParameteri(GLenum target, GLenum pname, GLint param); 149 150 /** Activate an OpenGL texture unit. 151 @param unit The texture unit to activate. 152 @return Whether or not the texture unit was successfully activated. 153 */ 154 bool activateGLTextureUnit(size_t unit); 155 156 /// Set the blend equation for RGB and alpha separately. 157 void setBlendEquation(GLenum eqRGB, GLenum eqA); 158 159 /// Set the blend function for RGB and alpha separately. 160 void setBlendFunc(GLenum source, GLenum dest, GLenum sourceA, GLenum destA); 161 162 /** Sets the current depth mask setting. 163 @param mask The depth mask to use. 164 */ 165 void setDepthMask(GLboolean mask); 166 167 /** Gets the current depth test function. 168 @return The current depth test function. 169 */ getDepthFunc(void)170 GLenum getDepthFunc(void) const { return mDepthFunc; } 171 172 /** Sets the current depth test function. 173 @param func The depth test function to use. 174 */ 175 void setDepthFunc(GLenum func); 176 177 /** Gets the clear depth in the range from [0..1]. 178 @return The current clearing depth. 179 */ getClearDepth(void)180 GLclampf getClearDepth(void) const { return mClearDepth; } 181 182 /** Sets the clear depth in the range from [0..1]. 183 @param depth The clear depth to use. 184 */ 185 void setClearDepth(GLclampf depth); 186 187 /** Sets the color to clear to. 188 @param red The red component. 189 @param green The green component. 190 @param blue The blue component. 191 @param alpha The alpha component. 192 */ 193 void setClearColour(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); 194 195 /** Sets the current colour mask. 196 @param red The red component. 197 @param green The green component. 198 @param blue The blue component. 199 @param alpha The alpha component. 200 */ 201 void setColourMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); 202 203 /** Sets the stencil mask. 204 @param mask The stencil mask to use 205 */ 206 void setStencilMask(GLuint mask); 207 208 /** Enables a piece of OpenGL functionality. 209 @param flag The function to enable. 210 */ 211 void setEnabled(GLenum flag, bool enabled); 212 213 /** Gets the current polygon rendering mode, fill, wireframe, points, etc. 214 @return The current polygon rendering mode. 215 */ getPolygonMode(void)216 GLenum getPolygonMode(void) const { return mPolygonMode; } 217 218 /** Sets the current polygon rendering mode. 219 @param mode The polygon mode to use. 220 */ 221 void setPolygonMode(GLenum mode); 222 223 /** Sets the face culling mode. 224 @return The current face culling mode 225 */ getCullFace(void)226 GLenum getCullFace(void) const { return mCullFace; } 227 228 /** Sets the face culling setting. 229 @param face The face culling mode to use. 230 */ 231 void setCullFace(GLenum face); 232 233 /** Bind an OpenGL separable program pipeline 234 @param handle The handle to the program pipeline 235 */ 236 void bindGLProgramPipeline(GLuint handle); 237 238 /// Enable the specified texture coordinate generation option for the currently active texture unit 239 void enableTextureCoordGen(GLenum type); 240 /// Disable the specified texture coordinate generation option for the currently active texture unit 241 void disableTextureCoordGen(GLenum type); 242 243 void setPointSize(GLfloat size); 244 245 void setViewport(GLint x, GLint y, GLsizei width, GLsizei height); 246 }; 247 } 248 249 #endif 250