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 "OgreGLES2NullStateCacheManagerImp.h"
31 #include "OgreGLES2RenderSystem.h"
32 #include "OgreLogManager.h"
33 #include "OgreRoot.h"
34 
35 namespace Ogre {
36 
GLES2StateCacheManagerImp(void)37     GLES2StateCacheManagerImp::GLES2StateCacheManagerImp(void)
38     {
39         clearCache();
40     }
41 
~GLES2StateCacheManagerImp(void)42     GLES2StateCacheManagerImp::~GLES2StateCacheManagerImp(void)
43     {
44     }
45 
initializeCache()46     void GLES2StateCacheManagerImp::initializeCache()
47     {
48         OGRE_CHECK_GL_ERROR(glBlendEquation(GL_FUNC_ADD));
49 
50         OGRE_CHECK_GL_ERROR(glBlendFunc(GL_ONE, GL_ZERO));
51 
52         OGRE_CHECK_GL_ERROR(glCullFace(mCullFace));
53 
54         OGRE_CHECK_GL_ERROR(glDepthFunc(mDepthFunc));
55 
56         OGRE_CHECK_GL_ERROR(glDepthMask(mDepthMask));
57 
58         OGRE_CHECK_GL_ERROR(glStencilMask(mStencilMask));
59 
60         OGRE_CHECK_GL_ERROR(glClearDepthf(mClearDepth));
61 
62         OGRE_CHECK_GL_ERROR(glBindTexture(GL_TEXTURE_2D, 0));
63 
64         OGRE_CHECK_GL_ERROR(glBindBuffer(GL_ARRAY_BUFFER, 0));
65 
66         OGRE_CHECK_GL_ERROR(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
67 
68         OGRE_CHECK_GL_ERROR(glBindFramebuffer(GL_FRAMEBUFFER, 0));
69 
70         OGRE_CHECK_GL_ERROR(glBindRenderbuffer(GL_RENDERBUFFER, 0));
71 
72         OGRE_CHECK_GL_ERROR(glActiveTexture(GL_TEXTURE0));
73 
74         OGRE_CHECK_GL_ERROR(glClearColor(mClearColour[0], mClearColour[1], mClearColour[2], mClearColour[3]));
75 
76         OGRE_CHECK_GL_ERROR(glColorMask(mColourMask[0], mColourMask[1], mColourMask[2], mColourMask[3]));
77     }
78 
clearCache()79     void GLES2StateCacheManagerImp::clearCache()
80     {
81         mDepthMask = GL_TRUE;
82         mPolygonMode = GL_FILL;
83         mBlendEquation = GL_FUNC_ADD;
84         mCullFace = GL_BACK;
85         mDepthFunc = GL_LESS;
86         mStencilMask = 0xFFFFFFFF;
87         mActiveTextureUnit = 0;
88         mDiscardBuffers = 0;
89         mClearDepth = 1.0f;
90 
91 		mClearColour.resize(4);
92         mClearColour[0] = mClearColour[1] = mClearColour[2] = mClearColour[3] = 0.0f;
93 
94         mColourMask.resize(4);
95         mColourMask[0] = mColourMask[1] = mColourMask[2] = mColourMask[3] = GL_TRUE;
96     }
97 
bindGLBuffer(GLenum target,GLuint buffer,bool force)98     void GLES2StateCacheManagerImp::bindGLBuffer(GLenum target, GLuint buffer, bool force)
99     {
100         // Update GL
101         if(target == GL_FRAMEBUFFER)
102         {
103             OGRE_CHECK_GL_ERROR(glBindFramebuffer(target, buffer));
104         }
105         else if(target == GL_RENDERBUFFER)
106         {
107             OGRE_CHECK_GL_ERROR(glBindRenderbuffer(target, buffer));
108         }
109         else
110         {
111             OGRE_CHECK_GL_ERROR(glBindBuffer(target, buffer));
112         }
113     }
114 
deleteGLBuffer(GLenum target,GLuint buffer,bool force)115     void GLES2StateCacheManagerImp::deleteGLBuffer(GLenum target, GLuint buffer, bool force)
116     {
117         // Buffer name 0 is reserved and we should never try to delete it
118         if(buffer == 0)
119             return;
120 
121         if(target == GL_FRAMEBUFFER)
122         {
123             OGRE_CHECK_GL_ERROR(glDeleteFramebuffers(1, &buffer));
124         }
125         else if(target == GL_RENDERBUFFER)
126         {
127             OGRE_CHECK_GL_ERROR(glDeleteRenderbuffers(1, &buffer));
128         }
129         else
130         {
131             OGRE_CHECK_GL_ERROR(glDeleteBuffers(1, &buffer));
132         }
133     }
134 
setTexParameteri(GLenum target,GLenum pname,GLint param)135     void GLES2StateCacheManagerImp::setTexParameteri(GLenum target, GLenum pname, GLint param)
136     {
137         // Update GL
138         OGRE_CHECK_GL_ERROR(glTexParameteri(target, pname, param));
139     }
140 
setTexParameterf(GLenum target,GLenum pname,GLfloat param)141     void GLES2StateCacheManagerImp::setTexParameterf(GLenum target, GLenum pname, GLfloat param)
142     {
143         OGRE_CHECK_GL_ERROR(glTexParameterf(target, pname, param));
144     }
145 
getTexParameterfv(GLenum target,GLenum pname,GLfloat * params)146     void GLES2StateCacheManagerImp::getTexParameterfv(GLenum target, GLenum pname, GLfloat *params)
147     {
148         OGRE_CHECK_GL_ERROR(glGetTexParameterfv(target, pname, params));
149     }
150 
invalidateStateForTexture(GLuint texture)151     void GLES2StateCacheManagerImp::invalidateStateForTexture(GLuint texture) { }
152 
bindGLTexture(GLenum target,GLuint texture)153     void GLES2StateCacheManagerImp::bindGLTexture(GLenum target, GLuint texture)
154     {
155         // Update GL
156         OGRE_CHECK_GL_ERROR(glBindTexture(target, texture));
157     }
158 
activateGLTextureUnit(unsigned char unit)159     bool GLES2StateCacheManagerImp::activateGLTextureUnit(unsigned char unit)
160 	{
161         // Always return true for the currently bound texture unit
162         if (mActiveTextureUnit == unit)
163             return true;
164 
165         if (unit < dynamic_cast<GLES2RenderSystem*>(Root::getSingleton().getRenderSystem())->getCapabilities()->getNumTextureUnits())
166         {
167             OGRE_CHECK_GL_ERROR(glActiveTexture(GL_TEXTURE0 + unit));
168 
169             mActiveTextureUnit = unit;
170 
171             return true;
172         }
173         else
174         {
175             return false;
176         }
177 	}
178 
setBlendFunc(GLenum source,GLenum dest)179     void GLES2StateCacheManagerImp::setBlendFunc(GLenum source, GLenum dest)
180     {
181         OGRE_CHECK_GL_ERROR(glBlendFunc(source, dest));
182     }
183 
setBlendEquation(GLenum eq)184     void GLES2StateCacheManagerImp::setBlendEquation(GLenum eq)
185     {
186         mBlendEquation = eq;
187         OGRE_CHECK_GL_ERROR(glBlendEquation(eq));
188     }
189 
setDepthMask(GLboolean mask)190     void GLES2StateCacheManagerImp::setDepthMask(GLboolean mask)
191     {
192         if(mDepthMask != mask)
193         {
194             mDepthMask = mask;
195 
196             OGRE_CHECK_GL_ERROR(glDepthMask(mask));
197         }
198     }
199 
setDepthFunc(GLenum func)200     void GLES2StateCacheManagerImp::setDepthFunc(GLenum func)
201     {
202         if(mDepthFunc != func)
203         {
204             mDepthFunc = func;
205 
206             OGRE_CHECK_GL_ERROR(glDepthFunc(func));
207         }
208     }
209 
setClearDepth(GLclampf depth)210     void GLES2StateCacheManagerImp::setClearDepth(GLclampf depth)
211     {
212         if(mClearDepth != depth)
213         {
214             mClearDepth = depth;
215 
216             OGRE_CHECK_GL_ERROR(glClearDepthf(depth));
217         }
218     }
219 
setClearColour(GLclampf red,GLclampf green,GLclampf blue,GLclampf alpha)220     void GLES2StateCacheManagerImp::setClearColour(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
221     {
222         if((mClearColour[0] != red) ||
223            (mClearColour[1] != green) ||
224            (mClearColour[2] != blue) ||
225            (mClearColour[3] != alpha))
226         {
227             mClearColour[0] = red;
228             mClearColour[1] = green;
229             mClearColour[2] = blue;
230             mClearColour[3] = alpha;
231 
232             OGRE_CHECK_GL_ERROR(glClearColor(mClearColour[0], mClearColour[1], mClearColour[2], mClearColour[3]));
233         }
234     }
235 
setColourMask(GLboolean red,GLboolean green,GLboolean blue,GLboolean alpha)236     void GLES2StateCacheManagerImp::setColourMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
237     {
238         if((mColourMask[0] != red) ||
239            (mColourMask[1] != green) ||
240            (mColourMask[2] != blue) ||
241            (mColourMask[3] != alpha))
242         {
243             mColourMask[0] = red;
244             mColourMask[1] = green;
245             mColourMask[2] = blue;
246             mColourMask[3] = alpha;
247 
248             OGRE_CHECK_GL_ERROR(glColorMask(mColourMask[0], mColourMask[1], mColourMask[2], mColourMask[3]));
249         }
250     }
251 
setStencilMask(GLuint mask)252     void GLES2StateCacheManagerImp::setStencilMask(GLuint mask)
253     {
254         if(mStencilMask != mask)
255         {
256             mStencilMask = mask;
257 
258             OGRE_CHECK_GL_ERROR(glStencilMask(mask));
259         }
260     }
261 
setEnabled(GLenum flag)262     void GLES2StateCacheManagerImp::setEnabled(GLenum flag)
263     {
264         OGRE_CHECK_GL_ERROR(glEnable(flag));
265     }
266 
setDisabled(GLenum flag)267     void GLES2StateCacheManagerImp::setDisabled(GLenum flag)
268     {
269         OGRE_CHECK_GL_ERROR(glDisable(flag));
270     }
271 
setVertexAttribEnabled(GLuint attrib)272     void GLES2StateCacheManagerImp::setVertexAttribEnabled(GLuint attrib)
273     {
274         OGRE_CHECK_GL_ERROR(glEnableVertexAttribArray(attrib));
275     }
276 
setVertexAttribDisabled(GLuint attrib)277     void GLES2StateCacheManagerImp::setVertexAttribDisabled(GLuint attrib)
278     {
279         OGRE_CHECK_GL_ERROR(glDisableVertexAttribArray(attrib));
280     }
281 
setCullFace(GLenum face)282     void GLES2StateCacheManagerImp::setCullFace(GLenum face)
283     {
284         if(mCullFace != face)
285         {
286             mCullFace = face;
287 
288             OGRE_CHECK_GL_ERROR(glCullFace(face));
289         }
290     }
291 }
292