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-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2015-05-20 09:13:18 +08:00
|
|
|
#include "renderer/CCRenderState.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
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
static const int MAX_ATTRIBUTES = 16;
|
|
|
|
static const int MAX_ACTIVE_TEXTURE = 16;
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
namespace
|
|
|
|
{
|
2014-05-08 11:20:19 +08:00
|
|
|
static GLuint s_currentProjectionMatrix = -1;
|
2014-05-09 03:34:26 +08:00
|
|
|
static uint32_t s_attributeFlags = 0; // 32 attributes max
|
2014-05-08 11:20:19 +08:00
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
static GLuint s_currentShaderProgram = -1;
|
2014-05-09 03:34:26 +08:00
|
|
|
static GLuint s_currentBoundTexture[MAX_ACTIVE_TEXTURE] = {(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, };
|
2013-12-18 17:47:20 +08:00
|
|
|
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;
|
2014-05-08 11:20:19 +08:00
|
|
|
s_attributeFlags = 0;
|
|
|
|
|
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;
|
2014-05-09 03:34:26 +08:00
|
|
|
for( int i=0; i < MAX_ACTIVE_TEXTURE; 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);
|
2015-05-20 09:13:18 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setBlend(false);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(sfactor, dfactor);
|
2015-05-20 09:13:18 +08:00
|
|
|
|
|
|
|
RenderState::StateBlock::_defaultState->setBlend(true);
|
|
|
|
RenderState::StateBlock::_defaultState->setBlendSrc((RenderState::Blend)sfactor);
|
2016-06-01 17:46:27 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setBlendDst((RenderState::Blend)dfactor);
|
2015-05-20 09:13:18 +08:00
|
|
|
}
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-08-03 14:29:34 +08:00
|
|
|
void bindTexture2D(Texture2D* texture)
|
|
|
|
{
|
|
|
|
GL::bindTexture2DN(0, texture->getName());
|
|
|
|
auto alphaTexID = texture->getAlphaTextureName();
|
|
|
|
if (alphaTexID > 0) {
|
|
|
|
GL::bindTexture2DN(1, alphaTexID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:23:30 +08:00
|
|
|
void bindTexture2DN(GLuint textureUnit, GLuint textureId)
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
|
|
|
CCASSERT(textureUnit < MAX_ACTIVE_TEXTURE, "textureUnit is too big");
|
|
|
|
if (s_currentBoundTexture[textureUnit] != textureId)
|
|
|
|
{
|
|
|
|
s_currentBoundTexture[textureUnit] = textureId;
|
|
|
|
activeTexture(GL_TEXTURE0 + textureUnit);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureId);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureId);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void bindTextureN(GLuint textureUnit, GLuint textureId, GLuint textureType/* = GL_TEXTURE_2D*/)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2014-05-09 03:34:26 +08:00
|
|
|
CCASSERT(textureUnit < MAX_ACTIVE_TEXTURE, "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);
|
2015-01-29 09:28:14 +08:00
|
|
|
glBindTexture(textureType, 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);
|
2015-01-29 09:28:14 +08:00
|
|
|
glBindTexture(textureType, 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-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE
|
2014-06-12 15:22:41 +08:00
|
|
|
for (size_t i = 0; i < MAX_ACTIVE_TEXTURE; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-06-12 15:22:41 +08:00
|
|
|
if (s_currentBoundTexture[i] == textureId)
|
|
|
|
{
|
|
|
|
s_currentBoundTexture[i] = -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-06-12 15:22:41 +08:00
|
|
|
void deleteTextureN(GLuint textureUnit, GLuint textureId)
|
|
|
|
{
|
|
|
|
deleteTexture(textureId);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
void enableVertexAttribs(uint32_t flags)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
bindVAO(0);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
// hardcoded!
|
2014-05-09 03:34:26 +08:00
|
|
|
for(int i=0; i < MAX_ATTRIBUTES; i++) {
|
2014-05-08 11:20:19 +08:00
|
|
|
unsigned int bit = 1 << i;
|
2015-01-23 14:50:21 +08:00
|
|
|
//FIXME:Cache is disabled, try to enable cache as before
|
2015-01-23 09:02:33 +08:00
|
|
|
bool enabled = (flags & bit) != 0;
|
2015-01-23 18:19:25 +08:00
|
|
|
bool enabledBefore = (s_attributeFlags & bit) != 0;
|
|
|
|
if(enabled != enabledBefore)
|
2015-01-23 14:32:36 +08:00
|
|
|
{
|
2014-05-08 11:20:19 +08:00
|
|
|
if( enabled )
|
|
|
|
glEnableVertexAttribArray(i);
|
|
|
|
else
|
|
|
|
glDisableVertexAttribArray(i);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-05-08 11:20:19 +08:00
|
|
|
s_attributeFlags = flags;
|
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
|