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
|
|
|
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "2d/ccGLStateCache.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCGLProgram.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccConfig.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
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;
|
2014-01-17 05:44:18 +08:00
|
|
|
static GLenum s_activeTexture = -1;
|
|
|
|
|
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
|
|
|
{
|
2014-04-03 17:40:14 +08:00
|
|
|
Director::getInstance()->resetMatrixStack();
|
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;
|
2014-01-17 05:44:18 +08:00
|
|
|
activeTexture(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
|
|
|
}
|
|
|
|
|
2014-01-17 05:44:18 +08:00
|
|
|
void activeTexture(GLenum texture)
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
|
|
|
if(s_activeTexture != texture) {
|
|
|
|
s_activeTexture = texture;
|
|
|
|
glActiveTexture(s_activeTexture);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
glActiveTexture(texture);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// GL Vertex Attrib functions
|
2012-03-12 15:22:03 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// 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
|