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-2013 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 __GLES2StateCacheManagerImp_H__
30 #define __GLES2StateCacheManagerImp_H__
31 
32 #include "OgreGLES2Prerequisites.h"
33 
34 typedef Ogre::GeneralAllocatedObject StateCacheAlloc;
35 
36 namespace Ogre
37 {
38     /** An in memory cache of the OpenGL ES2 state.
39      @see GLES2StateCacheManager
40      */
41     class _OgreGLES2Export GLES2StateCacheManagerImp : public StateCacheAlloc
42     {
43     private:
44         typedef HashMap<GLenum, GLuint> BindBufferMap;
45         typedef HashMap<GLenum, GLint> TexParameteriMap;
46         typedef HashMap<GLenum, GLfloat> TexParameterfMap;
47 
48         struct TextureUnitParams
49         {
~TextureUnitParamsTextureUnitParams50             ~TextureUnitParams()
51             {
52                 mTexParameteriMap.clear();
53                 mTexParameterfMap.clear();
54             }
55 
56             TexParameteriMap mTexParameteriMap;
57             TexParameterfMap mTexParameterfMap;
58         };
59 
60         typedef HashMap<GLuint, TextureUnitParams> TexUnitsMap;
61 
62         /* These variables are used for caching OpenGL state.
63          They are cached because state changes can be quite expensive,
64          which is especially important on mobile or embedded systems.
65          */
66 
67         /// A map of different buffer types and the currently bound buffer for each type
68         BindBufferMap mActiveBufferMap;
69         /// A map of texture parameters for each texture unit
70         TexUnitsMap mTexUnitsMap;
71         /// Array of each OpenGL feature that is enabled i.e. blending, depth test, etc.
72         vector<GLenum>::type mEnableVector;
73         /// Stores the current clear colour
74         vector<GLclampf>::type mClearColour;
75         /// Stores the current colour write mask
76         vector<GLboolean>::type mColourMask;
77         /// Stores the currently enabled vertex attributes
78         vector<GLuint>::type mEnabledVertexAttribs;
79         /// Stores the current depth write mask
80         GLboolean mDepthMask;
81         /// Stores the current polygon rendering mode
82         GLenum mPolygonMode;
83         /// Stores the current blend equation
84         GLenum mBlendEquation;
85         /// Stores the current blend source function
86         GLenum mBlendFuncSource;
87         /// Stores the current blend destination function
88         GLenum mBlendFuncDest;
89         /// Stores the current face culling setting
90         GLenum mCullFace;
91         /// Stores the current depth test function
92         GLenum mDepthFunc;
93         /// Stores the current stencil mask
94         GLuint mStencilMask;
95 		/// Stores the last bound texture id
96         GLuint mLastBoundTexID;
97         /// Stores the currently active texture unit
98         GLenum mActiveTextureUnit;
99         /// Mask of buffers who contents can be discarded if GL_EXT_discard_framebuffer is supported
100         unsigned int mDiscardBuffers;
101         /// Stores the current depth clearing colour
102         GLclampf mClearDepth;
103 
104     public:
105         GLES2StateCacheManagerImp(void);
106         ~GLES2StateCacheManagerImp(void);
107 
108         /// See GLES2StateCacheManager.initializeCache.
109         void initializeCache();
110 
111         /// See GLES2StateCacheManager.clearCache.
112         void clearCache();
113 
114         /// See GLES2StateCacheManager.bindGLBuffer.
115         void bindGLBuffer(GLenum target, GLuint buffer, GLenum attach = 0, bool force = false);
116 
117         /// See GLES2StateCacheManager.deleteGLBuffer.
118         void deleteGLBuffer(GLenum target, GLuint buffer, GLenum attach = 0, bool force = false);
119 
120         /// See GLES2StateCacheManager.bindGLTexture.
121         void bindGLTexture(GLenum target, GLuint texture);
122 
123         /// See GLES2StateCacheManager.setTexParameteri.
124         void setTexParameteri(GLenum target, GLenum pname, GLint param);
125 
126         /// See GLES2StateCacheManager.setTexParameterf.
127         void setTexParameterf(GLenum target, GLenum pname, GLfloat params);
128 
129         /// See GLES2StateCacheManager.getTexParameterfv.
130         void getTexParameterfv(GLenum target, GLenum pname, GLfloat *params);
131 
132         /// See GLES2StateCacheManager.invalidateStateForTexture.
133         void invalidateStateForTexture(GLuint texture);
134 
135         /// See GLES2StateCacheManager.activateGLTextureUnit.
136         bool activateGLTextureUnit(size_t unit);
137 
138         /// See GLES2StateCacheManager.getBlendEquation.
getBlendEquation(void)139         GLenum getBlendEquation(void) const { return mBlendEquation; }
140 
141         /// See GLES2StateCacheManager.setBlendEquation.
142         void setBlendEquation(GLenum eq);
143 
144         /// See GLES2StateCacheManager.setBlendFunc.
145         void setBlendFunc(GLenum source, GLenum dest);
146 
147         /// See GLES2StateCacheManager.getDepthMask.
getDepthMask(void)148         GLboolean getDepthMask(void) const { return mDepthMask; }
149 
150         /// See GLES2StateCacheManager.setDepthMask.
151         void setDepthMask(GLboolean mask);
152 
153         /// See GLES2StateCacheManager.getDepthFunc.
getDepthFunc(void)154         GLenum getDepthFunc(void) const { return mDepthFunc; }
155 
156         /// See GLES2StateCacheManager.setDepthFunc.
157         void setDepthFunc(GLenum func);
158 
159         /// See GLES2StateCacheManager.getClearDepth.
getClearDepth(void)160         GLclampf getClearDepth(void) const { return mClearDepth; }
161 
162         /// See GLES2StateCacheManager.setClearDepth.
163         void setClearDepth(GLclampf depth);
164 
165         /// See GLES2StateCacheManager.setClearColour.
166         void setClearColour(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
167 
168         /// See GLES2StateCacheManager.getColourMask.
getColourMask(void)169         vector<GLboolean>::type & getColourMask(void) { return mColourMask; }
170 
171         /// See GLES2StateCacheManager.setColourMask.
172         void setColourMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
173 
174         /// See GLES2StateCacheManager.getStencilMask.
getStencilMask(void)175         GLuint getStencilMask(void) const { return mStencilMask; }
176 
177         /// See GLES2StateCacheManager.setStencilMask.
178         void setStencilMask(GLuint mask);
179 
180         /// See GLES2StateCacheManager.setEnabled.
181         void setEnabled(GLenum flag);
182 
183         /// See GLES2StateCacheManager.setDisabled.
184         void setDisabled(GLenum flag);
185 
186         /// See GLES2StateCacheManager.setVertexAttribEnabled.
187         void setVertexAttribEnabled(GLuint attrib);
188 
189         /// See GLES2StateCacheManager.setVertexAttribDisabled.
190         void setVertexAttribDisabled(GLuint attrib);
191 
192         /// See GLES2StateCacheManager.getDiscardBuffers.
getDiscardBuffers(void)193         unsigned int getDiscardBuffers(void) const { return mDiscardBuffers; }
194 
195         /// See GLES2StateCacheManager.setDiscardBuffers.
setDiscardBuffers(unsigned int flags)196         void setDiscardBuffers(unsigned int flags) { mDiscardBuffers = flags; }
197 
198         /// See GLES2StateCacheManager.getPolygonMode.
getPolygonMode(void)199         GLenum getPolygonMode(void) const { return mPolygonMode; }
200 
201         /// See GLES2StateCacheManager.setPolygonMode.
setPolygonMode(GLenum mode)202         void setPolygonMode(GLenum mode) { mPolygonMode = mode; }
203 
204         /// See GLES2StateCacheManager.getCullFace.
getCullFace(void)205         GLenum getCullFace(void) const { return mCullFace; }
206 
207         /// See GLES2StateCacheManager.setCullFace.
208         void setCullFace(GLenum face);
209     };
210 }
211 
212 #endif
213