2012-03-16 13:42:53 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-03-16 13:42:53 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (C) 2013-2014 Chukong Technologies Inc.
|
2012-03-16 13:42:53 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
#include "ccGLStateCache.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
#include "CCGLProgram.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "ccConfig.h"
|
2013-10-13 11:47:51 +08:00
|
|
|
#include "CCConfiguration.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
// extern
|
|
|
|
#include "kazmath/GL/matrix.h"
|
|
|
|
#include "kazmath/kazmath.h"
|
|
|
|
|
2012-11-15 18:34:23 +08:00
|
|
|
NS_CC_BEGIN
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
static GLuint s_currentProjectionMatrix = -1;
|
|
|
|
static bool s_vertexAttribPosition = false;
|
|
|
|
static bool s_vertexAttribColor = false;
|
|
|
|
static bool s_vertexAttribTexCoords = false;
|
|
|
|
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
#define kMaxActiveTexture 16
|
2013-12-18 17:47:20 +08:00
|
|
|
|
|
|
|
static GLuint s_currentShaderProgram = -1;
|
|
|
|
static GLuint s_currentBoundTexture[kMaxActiveTexture] = {(GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, };
|
|
|
|
static GLenum s_blendingSource = -1;
|
|
|
|
static GLenum s_blendingDest = -1;
|
|
|
|
static int s_GLServerState = 0;
|
|
|
|
static GLuint s_VAO = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
// GL State Cache functions
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
namespace GL {
|
|
|
|
|
|
|
|
void invalidateStateCache( void )
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLFreeAll();
|
2012-11-13 11:06:32 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
s_currentProjectionMatrix = -1;
|
|
|
|
s_vertexAttribPosition = false;
|
|
|
|
s_vertexAttribColor = false;
|
|
|
|
s_vertexAttribTexCoords = false;
|
2012-11-13 11:06:32 +08:00
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
s_currentShaderProgram = -1;
|
2013-06-20 14:13:12 +08:00
|
|
|
for( int i=0; i < kMaxActiveTexture; i++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
s_currentBoundTexture[i] = -1;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
s_blendingSource = -1;
|
|
|
|
s_blendingDest = -1;
|
|
|
|
s_GLServerState = 0;
|
|
|
|
s_VAO = 0;
|
2013-07-17 12:53:30 +08:00
|
|
|
|
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void deleteProgram( GLuint program )
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
if(program == s_currentShaderProgram)
|
2012-11-13 11:06:32 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
s_currentShaderProgram = -1;
|
2012-11-13 11:06:32 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDeleteProgram( program );
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void useProgram( GLuint program )
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
if( program != s_currentShaderProgram ) {
|
|
|
|
s_currentShaderProgram = program;
|
2012-04-19 14:35:52 +08:00
|
|
|
glUseProgram(program);
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
glUseProgram(program);
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
|
|
|
}
|
|
|
|
|
2012-11-09 12:08:18 +08:00
|
|
|
static void SetBlending(GLenum sfactor, GLenum dfactor)
|
|
|
|
{
|
|
|
|
if (sfactor == GL_ONE && dfactor == GL_ZERO)
|
|
|
|
{
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(sfactor, dfactor);
|
|
|
|
}
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void blendFunc(GLenum sfactor, GLenum dfactor)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
if (sfactor != s_blendingSource || dfactor != s_blendingDest)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
s_blendingSource = sfactor;
|
|
|
|
s_blendingDest = dfactor;
|
2012-11-09 12:08:18 +08:00
|
|
|
SetBlending(sfactor, dfactor);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-11-09 12:08:18 +08:00
|
|
|
SetBlending( sfactor, dfactor );
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void blendResetToCache(void)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-11-09 12:08:18 +08:00
|
|
|
glBlendEquation(GL_FUNC_ADD);
|
2012-03-12 15:22:03 +08:00
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
SetBlending(s_blendingSource, s_blendingDest);
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-11-09 12:08:18 +08:00
|
|
|
SetBlending(CC_BLEND_SRC, CC_BLEND_DST);
|
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void bindTexture2D(GLuint textureId)
|
2012-11-13 11:06:32 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2DN(0, textureId);
|
2012-11-13 11:06:32 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void bindTexture2DN(GLuint textureUnit, GLuint textureId)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(textureUnit < kMaxActiveTexture, "textureUnit is too big");
|
2013-12-18 17:47:20 +08:00
|
|
|
if (s_currentBoundTexture[textureUnit] != textureId)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
s_currentBoundTexture[textureUnit] = textureId;
|
2012-11-09 12:08:18 +08:00
|
|
|
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
2012-11-13 11:06:32 +08:00
|
|
|
glBindTexture(GL_TEXTURE_2D, textureId);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-11-09 12:08:18 +08:00
|
|
|
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
2012-11-13 11:06:32 +08:00
|
|
|
glBindTexture(GL_TEXTURE_2D, textureId);
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void deleteTexture(GLuint textureId)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
deleteTextureN(0, textureId);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void deleteTextureN(GLuint textureUnit, GLuint textureId)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
if (s_currentBoundTexture[textureUnit] == textureId)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
s_currentBoundTexture[textureUnit] = -1;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-11-09 12:08:18 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
|
|
|
|
2012-11-13 11:06:32 +08:00
|
|
|
glDeleteTextures(1, &textureId);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void bindVAO(GLuint vaoId)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2012-11-16 14:23:14 +08:00
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2013-12-18 17:47:20 +08:00
|
|
|
if (s_VAO != vaoId)
|
2013-10-13 11:47:51 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
s_VAO = vaoId;
|
2013-10-13 11:47:51 +08:00
|
|
|
glBindVertexArray(vaoId);
|
|
|
|
}
|
2012-11-09 12:08:18 +08:00
|
|
|
#else
|
2013-10-13 11:47:51 +08:00
|
|
|
glBindVertexArray(vaoId);
|
2012-11-16 14:23:14 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
|
|
|
|
2013-10-13 11:47:51 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - GL Vertex Attrib functions
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void enableVertexAttribs( unsigned int flags )
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
bindVAO(0);
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* Position */
|
2013-07-25 15:04:13 +08:00
|
|
|
bool enablePosition = flags & VERTEX_ATTRIB_FLAG_POSITION;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if( enablePosition != s_vertexAttribPosition ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
if( enablePosition )
|
2013-07-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray( GLProgram::VERTEX_ATTRIB_POSITION );
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2013-07-25 17:48:22 +08:00
|
|
|
glDisableVertexAttribArray( GLProgram::VERTEX_ATTRIB_POSITION );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
s_vertexAttribPosition = enablePosition;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* Color */
|
2013-07-25 15:04:13 +08:00
|
|
|
bool enableColor = (flags & VERTEX_ATTRIB_FLAG_COLOR) != 0 ? true : false;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if( enableColor != s_vertexAttribColor ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
if( enableColor )
|
2013-07-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray( GLProgram::VERTEX_ATTRIB_COLOR );
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2013-07-25 17:48:22 +08:00
|
|
|
glDisableVertexAttribArray( GLProgram::VERTEX_ATTRIB_COLOR );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
s_vertexAttribColor = enableColor;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* Tex Coords */
|
2013-07-25 15:04:13 +08:00
|
|
|
bool enableTexCoords = (flags & VERTEX_ATTRIB_FLAG_TEX_COORDS) != 0 ? true : false;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if( enableTexCoords != s_vertexAttribTexCoords ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
if( enableTexCoords )
|
2013-07-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray( GLProgram::VERTEX_ATTRIB_TEX_COORDS );
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2013-07-25 17:48:22 +08:00
|
|
|
glDisableVertexAttribArray( GLProgram::VERTEX_ATTRIB_TEX_COORDS );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
s_vertexAttribTexCoords = enableTexCoords;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - GL Uniforms functions
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void setProjectionMatrixDirty( void )
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
s_currentProjectionMatrix = -1;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
2012-11-15 18:34:23 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
} // Namespace GL
|
|
|
|
|
2012-11-15 18:34:23 +08:00
|
|
|
NS_CC_END
|