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