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 #include "OgreStableHeaders.h"
30 #include "OgreGLStateCacheManager.h"
31 #include "OgreGLRenderSystem.h"
32 #include "OgreLogManager.h"
33 #include "OgreRoot.h"
34 
35 #if OGRE_NO_GL_STATE_CACHE_SUPPORT == 0
36 #   include "OgreGLStateCacheManagerImp.h"
37 #else
38 #   include "OgreGLNullStateCacheManagerImp.h"
39 #endif
40 
41 namespace Ogre {
42 
GLStateCacheManager()43     GLStateCacheManager::GLStateCacheManager()
44     {
45         mImp = 0;
46     }
47 
~GLStateCacheManager()48     GLStateCacheManager::~GLStateCacheManager()
49     {
50         for (CachesMap::iterator it = mCaches.begin(); it != mCaches.end(); ++it)
51             OGRE_DELETE it->second;
52     }
53 
switchContext(intptr_t id)54     void GLStateCacheManager::switchContext(intptr_t id)
55     {
56         CachesMap::iterator it = mCaches.find(id);
57         if (it != mCaches.end())
58         {
59             // Already have a cache for this context
60             mImp = it->second;
61         }
62         else
63         {
64             // No cache for this context yet
65             mImp = OGRE_NEW GLStateCacheManagerImp();
66             mImp->initializeCache();
67             mCaches[id] = mImp;
68         }
69     }
70 
unregisterContext(intptr_t id)71     void GLStateCacheManager::unregisterContext(intptr_t id)
72     {
73         CachesMap::iterator it = mCaches.find(id);
74         if (it != mCaches.end())
75         {
76             if (mImp == it->second)
77                 mImp = NULL;
78             OGRE_DELETE it->second;
79             mCaches.erase(it);
80         }
81 
82         // Always keep a valid cache, even if no contexts are left.
83         // This is needed due to the way GLRenderSystem::shutdown works -
84         // HardwareBufferManager destructor may call deleteGLBuffer even after all contexts
85         // have been deleted
86         if (mImp == NULL)
87         {
88             // Therefore we add a "dummy" cache if none are left
89             if (!mCaches.size())
90                 mCaches[0] = OGRE_NEW GLStateCacheManagerImp();
91             mImp = mCaches.begin()->second;
92         }
93     }
94 
clearCache()95     void GLStateCacheManager::clearCache()
96     {
97         mImp->clearCache();
98     }
99 
bindGLBuffer(GLenum target,GLuint buffer,bool force)100     void GLStateCacheManager::bindGLBuffer(GLenum target, GLuint buffer, bool force)
101     {
102         mImp->bindGLBuffer(target, buffer, force);
103     }
104 
deleteGLBuffer(GLenum target,GLuint buffer,bool force)105     void GLStateCacheManager::deleteGLBuffer(GLenum target, GLuint buffer, bool force)
106     {
107         mImp->deleteGLBuffer(target, buffer, force);
108     }
109 
setTexParameteri(GLenum target,GLenum pname,GLint param)110     void GLStateCacheManager::setTexParameteri(GLenum target, GLenum pname, GLint param)
111     {
112         mImp->setTexParameteri(target, pname, param);
113     }
114 
invalidateStateForTexture(GLuint texture)115     void GLStateCacheManager::invalidateStateForTexture(GLuint texture)
116     {
117         mImp->invalidateStateForTexture(texture);
118     }
119 
bindGLTexture(GLenum target,GLuint texture)120     void GLStateCacheManager::bindGLTexture(GLenum target, GLuint texture)
121     {
122         mImp->bindGLTexture(target, texture);
123     }
124 
activateGLTextureUnit(size_t unit)125     bool GLStateCacheManager::activateGLTextureUnit(size_t unit)
126 	{
127         return mImp->activateGLTextureUnit(unit);
128 	}
129 
setBlendFunc(GLenum source,GLenum dest)130     void GLStateCacheManager::setBlendFunc(GLenum source, GLenum dest)
131     {
132         mImp->setBlendFunc(source, dest);
133     }
134 
setBlendEquation(GLenum eq)135     void GLStateCacheManager::setBlendEquation(GLenum eq)
136     {
137         mImp->setBlendEquation(eq);
138     }
139 
setDepthMask(GLboolean mask)140     void GLStateCacheManager::setDepthMask(GLboolean mask)
141     {
142         mImp->setDepthMask(mask);
143     }
144 
setDepthFunc(GLenum func)145     void GLStateCacheManager::setDepthFunc(GLenum func)
146     {
147         mImp->setDepthFunc(func);
148     }
149 
setClearDepth(GLclampf depth)150     void GLStateCacheManager::setClearDepth(GLclampf depth)
151     {
152         mImp->setClearDepth(depth);
153     }
154 
setClearColour(GLclampf red,GLclampf green,GLclampf blue,GLclampf alpha)155     void GLStateCacheManager::setClearColour(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
156     {
157         mImp->setClearColour(red, green, blue, alpha);
158     }
159 
setColourMask(GLboolean red,GLboolean green,GLboolean blue,GLboolean alpha)160     void GLStateCacheManager::setColourMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
161     {
162         mImp->setColourMask(red, green, blue, alpha);
163     }
164 
setStencilMask(GLuint mask)165     void GLStateCacheManager::setStencilMask(GLuint mask)
166     {
167         mImp->setStencilMask(mask);
168     }
169 
setEnabled(GLenum flag)170     void GLStateCacheManager::setEnabled(GLenum flag)
171     {
172         mImp->setEnabled(flag);
173     }
174 
setDisabled(GLenum flag)175     void GLStateCacheManager::setDisabled(GLenum flag)
176     {
177         mImp->setDisabled(flag);
178     }
179 
setCullFace(GLenum face)180     void GLStateCacheManager::setCullFace(GLenum face)
181     {
182         mImp->setCullFace(face);
183     }
184 
getBlendEquation() const185     GLenum GLStateCacheManager::getBlendEquation() const
186     {
187         return mImp->getBlendEquation();
188     }
189 
getDepthMask() const190     GLboolean GLStateCacheManager::getDepthMask() const
191     {
192         return mImp->getDepthMask();
193     }
194 
getDepthFunc() const195     GLenum GLStateCacheManager::getDepthFunc() const
196     {
197         return mImp->getDepthFunc();
198     }
199 
getClearDepth() const200     GLclampf GLStateCacheManager::getClearDepth() const
201     {
202         return mImp->getClearDepth();
203     }
204 
getColourMask() const205     vector<GLboolean>::type & GLStateCacheManager::getColourMask() const
206     {
207         return mImp->getColourMask();
208     }
209 
getStencilMask() const210     GLuint GLStateCacheManager::getStencilMask() const
211     {
212         return mImp->getStencilMask();
213     }
214 
getDiscardBuffers() const215     unsigned int GLStateCacheManager::getDiscardBuffers() const
216     {
217         return mImp->getDiscardBuffers();
218     }
219 
setDiscardBuffers(unsigned int flags)220     void GLStateCacheManager::setDiscardBuffers(unsigned int flags)
221     {
222         mImp->setDiscardBuffers(flags);
223     }
224 
getPolygonMode() const225     GLenum GLStateCacheManager::getPolygonMode() const
226     {
227         return mImp->getPolygonMode();
228     }
229 
setPolygonMode(GLenum mode)230     void GLStateCacheManager::setPolygonMode(GLenum mode)
231     {
232         mImp->setPolygonMode(mode);
233     }
234 
getCullFace() const235     GLenum GLStateCacheManager::getCullFace() const
236     {
237         return mImp->getCullFace();
238     }
239 
setViewport(GLint x,GLint y,GLsizei width,GLsizei height)240     void GLStateCacheManager::setViewport(GLint x, GLint y, GLsizei width, GLsizei height)
241     {
242         mImp->setViewport(x, y, width, height);
243     }
244 
getViewport(int * array)245     void GLStateCacheManager::getViewport(int *array)
246     {
247         mImp->getViewport(array);
248     }
249 
setPointSize(GLfloat size)250     void GLStateCacheManager::setPointSize(GLfloat size)
251     {
252         mImp->setPointSize(size);
253     }
254 
setLightAmbient(GLfloat r,GLfloat g,GLfloat b)255     void GLStateCacheManager::setLightAmbient(GLfloat r, GLfloat g, GLfloat b)
256     {
257         mImp->setLightAmbient(r, g, b);
258     }
259 
setShadeModel(GLenum model)260     void GLStateCacheManager::setShadeModel(GLenum model)
261     {
262         mImp->setShadeModel(model);
263     }
264 
enableTextureCoordGen(GLenum type)265     void GLStateCacheManager::enableTextureCoordGen(GLenum type)
266     {
267         mImp->enableTextureCoordGen(type);
268     }
269 
disableTextureCoordGen(GLenum type)270     void GLStateCacheManager::disableTextureCoordGen(GLenum type)
271     {
272         mImp->disableTextureCoordGen(type);
273     }
274 
setMaterialShininess(GLfloat shininess)275     void GLStateCacheManager::setMaterialShininess(GLfloat shininess)
276     {
277         mImp->setMaterialShininess(shininess);
278     }
279 
setMaterialSpecular(GLfloat r,GLfloat g,GLfloat b,GLfloat a)280     void GLStateCacheManager::setMaterialSpecular(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
281     {
282         mImp->setMaterialSpecular(r, g, b, a);
283     }
284 
setMaterialEmissive(GLfloat r,GLfloat g,GLfloat b,GLfloat a)285     void GLStateCacheManager::setMaterialEmissive(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
286     {
287         mImp->setMaterialEmissive(r, g, b, a);
288     }
289 
setMaterialAmbient(GLfloat r,GLfloat g,GLfloat b,GLfloat a)290     void GLStateCacheManager::setMaterialAmbient(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
291     {
292         mImp->setMaterialAmbient(r, g, b, a);
293     }
294 
setMaterialDiffuse(GLfloat r,GLfloat g,GLfloat b,GLfloat a)295     void GLStateCacheManager::setMaterialDiffuse(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
296     {
297         mImp->setMaterialDiffuse(r, g, b, a);
298     }
299 
setBlendEquation(GLenum eqRGB,GLenum eqAlpha)300     void GLStateCacheManager::setBlendEquation(GLenum eqRGB, GLenum eqAlpha)
301     {
302         mImp->setBlendEquation(eqRGB, eqAlpha);
303     }
304 
setPointParameters(GLfloat * attenuation,float minSize,float maxSize)305     void GLStateCacheManager::setPointParameters(GLfloat *attenuation, float minSize, float maxSize)
306     {
307         mImp->setPointParameters(attenuation, minSize, maxSize);
308     }
309 }
310