2012-03-16 13:42:53 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
// extern
|
|
|
|
#include "kazmath/GL/matrix.h"
|
|
|
|
#include "kazmath/kazmath.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
static GLuint s_uCurrentProjectionMatrix = -1;
|
|
|
|
static bool s_bVertexAttribPosition = false;
|
|
|
|
static bool s_bVertexAttribColor = false;
|
|
|
|
static bool s_bVertexAttribTexCoords = false;
|
2012-03-16 17:56:19 +08:00
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-03-16 17:56:19 +08:00
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
#define kCCMaxActiveTexture 16
|
2012-03-16 17:56:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
static GLuint s_uCurrentShaderProgram = -1;
|
2012-07-09 13:37:19 +08:00
|
|
|
static GLuint s_uCurrentBoundTexture[kCCMaxActiveTexture] = {(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, };
|
2012-04-19 14:35:52 +08:00
|
|
|
static GLenum s_eCurrentActiveTexture = (GL_TEXTURE0 - GL_TEXTURE0);
|
|
|
|
static GLenum s_eBlendingSource = -1;
|
|
|
|
static GLenum s_eBlendingDest = -1;
|
2012-03-16 17:56:19 +08:00
|
|
|
static int s_eGLServerState = 0;
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
// GL State Cache functions
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
void ccGLInvalidateStateCache( void )
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLFreeAll();
|
|
|
|
s_uCurrentProjectionMatrix = -1;
|
|
|
|
s_bVertexAttribPosition = false;
|
|
|
|
s_bVertexAttribColor = false;
|
|
|
|
s_bVertexAttribTexCoords = false;
|
2012-03-12 15:22:03 +08:00
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
s_uCurrentShaderProgram = -1;
|
|
|
|
for( int i=0; i < kCCMaxActiveTexture; i++ )
|
|
|
|
{
|
|
|
|
s_uCurrentBoundTexture[i] = -1;
|
|
|
|
}
|
|
|
|
s_eCurrentActiveTexture = (GL_TEXTURE0 - GL_TEXTURE0);
|
|
|
|
s_eBlendingSource = -1;
|
|
|
|
s_eBlendingDest = -1;
|
|
|
|
s_eGLServerState = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ccGLDeleteProgram( GLuint program )
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
if( program == s_uCurrentShaderProgram )
|
|
|
|
s_uCurrentShaderProgram = -1;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ccGLUseProgram( GLuint program )
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
if( program != s_uCurrentShaderProgram ) {
|
|
|
|
s_uCurrentShaderProgram = program;
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ccGLBlendFunc(GLenum sfactor, GLenum dfactor)
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
if( sfactor != s_eBlendingSource || dfactor != s_eBlendingDest ) {
|
|
|
|
s_eBlendingSource = sfactor;
|
|
|
|
s_eBlendingDest = dfactor;
|
|
|
|
glBlendFunc( sfactor, dfactor );
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
glBlendFunc( sfactor, dfactor );
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif // CC_ENABLE_GL_STATE_CACHE
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum ccGLGetActiveTexture( void )
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
return s_eCurrentActiveTexture + GL_TEXTURE0;
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
GLenum activeTexture;
|
|
|
|
glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&activeTexture);
|
|
|
|
return activeTexture;
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ccGLActiveTexture( GLenum textureEnum )
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( (textureEnum - GL_TEXTURE0) < kCCMaxActiveTexture, "cocos2d ERROR: Increase kCCMaxActiveTexture to kCCMaxActiveTexture!");
|
|
|
|
if( (textureEnum - GL_TEXTURE0) != s_eCurrentActiveTexture ) {
|
|
|
|
s_eCurrentActiveTexture = (textureEnum - GL_TEXTURE0);
|
|
|
|
glActiveTexture( textureEnum );
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
glActiveTexture( textureEnum );
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ccGLBindTexture2D( GLuint textureId )
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
if( s_uCurrentBoundTexture[ s_eCurrentActiveTexture ] != textureId )
|
|
|
|
{
|
|
|
|
s_uCurrentBoundTexture[ s_eCurrentActiveTexture ] = textureId;
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureId );
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
glBindTexture(GL_TEXTURE_2D, textureId );
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ccGLDeleteTexture( GLuint textureId )
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2012-04-19 14:35:52 +08:00
|
|
|
if( textureId == s_uCurrentBoundTexture[ s_eCurrentActiveTexture ] )
|
|
|
|
s_uCurrentBoundTexture[ s_eCurrentActiveTexture ] = -1;
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
glDeleteTextures(1, &textureId );
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ccGLEnable( ccGLServerState flags )
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
|
|
|
|
2012-05-29 17:11:33 +08:00
|
|
|
int enabled = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* GL_BLEND */
|
2012-05-29 17:11:33 +08:00
|
|
|
if( (enabled = (flags & CC_GL_BLEND)) != (s_eGLServerState & CC_GL_BLEND) ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
if( enabled ) {
|
|
|
|
glEnable( GL_BLEND );
|
|
|
|
s_eGLServerState |= CC_GL_BLEND;
|
|
|
|
} else {
|
|
|
|
glDisable( GL_BLEND );
|
|
|
|
s_eGLServerState &= ~CC_GL_BLEND;
|
|
|
|
}
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
if( flags & CC_GL_BLEND )
|
|
|
|
glEnable( GL_BLEND );
|
|
|
|
else
|
|
|
|
glDisable( GL_BLEND );
|
2012-03-12 15:22:03 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - GL Vertex Attrib functions
|
|
|
|
|
|
|
|
void ccGLEnableVertexAttribs( unsigned int flags )
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
/* Position */
|
|
|
|
bool enablePosition = flags & kCCVertexAttribFlag_Position;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( enablePosition != s_bVertexAttribPosition ) {
|
|
|
|
if( enablePosition )
|
|
|
|
glEnableVertexAttribArray( kCCVertexAttrib_Position );
|
|
|
|
else
|
|
|
|
glDisableVertexAttribArray( kCCVertexAttrib_Position );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
s_bVertexAttribPosition = enablePosition;
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* Color */
|
|
|
|
bool enableColor = (flags & kCCVertexAttribFlag_Color) != 0 ? true : false;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( enableColor != s_bVertexAttribColor ) {
|
|
|
|
if( enableColor )
|
|
|
|
glEnableVertexAttribArray( kCCVertexAttrib_Color );
|
|
|
|
else
|
|
|
|
glDisableVertexAttribArray( kCCVertexAttrib_Color );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
s_bVertexAttribColor = enableColor;
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* Tex Coords */
|
|
|
|
bool enableTexCoords = (flags & kCCVertexAttribFlag_TexCoords) != 0 ? true : false;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( enableTexCoords != s_bVertexAttribTexCoords ) {
|
|
|
|
if( enableTexCoords )
|
|
|
|
glEnableVertexAttribArray( kCCVertexAttrib_TexCoords );
|
|
|
|
else
|
|
|
|
glDisableVertexAttribArray( kCCVertexAttrib_TexCoords );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
s_bVertexAttribTexCoords = enableTexCoords;
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - GL Uniforms functions
|
|
|
|
|
|
|
|
void ccSetProjectionMatrixDirty( void )
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
s_uCurrentProjectionMatrix = -1;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|