123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #include "renderer/ccGLStateCache.h"
- #include "renderer/CCGLProgram.h"
- #include "renderer/CCRenderState.h"
- #include "base/CCDirector.h"
- #include "base/ccConfig.h"
- #include "base/CCConfiguration.h"
- NS_CC_BEGIN
- static const int MAX_ATTRIBUTES = 16;
- static const int MAX_ACTIVE_TEXTURE = 16;
- namespace
- {
- static GLuint s_currentProjectionMatrix = -1;
- static uint32_t s_attributeFlags = 0;
- #if CC_ENABLE_GL_STATE_CACHE
- static GLuint s_currentShaderProgram = -1;
- 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, };
- static GLenum s_blendingSource = -1;
- static GLenum s_blendingDest = -1;
- static int s_GLServerState = 0;
- static GLuint s_VAO = 0;
- static GLenum s_activeTexture = -1;
- #endif
- }
- namespace GL {
- void invalidateStateCache( void )
- {
- Director::getInstance()->resetMatrixStack();
- s_currentProjectionMatrix = -1;
- s_attributeFlags = 0;
- #if CC_ENABLE_GL_STATE_CACHE
- s_currentShaderProgram = -1;
- for( int i=0; i < MAX_ACTIVE_TEXTURE; i++ )
- {
- s_currentBoundTexture[i] = -1;
- }
- s_blendingSource = -1;
- s_blendingDest = -1;
- s_GLServerState = 0;
- s_VAO = 0;
-
- #endif
- }
- void deleteProgram( GLuint program )
- {
- #if CC_ENABLE_GL_STATE_CACHE
- if(program == s_currentShaderProgram)
- {
- s_currentShaderProgram = -1;
- }
- #endif
- glDeleteProgram( program );
- }
- void useProgram( GLuint program )
- {
- #if CC_ENABLE_GL_STATE_CACHE
- if( program != s_currentShaderProgram ) {
- s_currentShaderProgram = program;
- glUseProgram(program);
- }
- #else
- glUseProgram(program);
- #endif
- }
- static void SetBlending(GLenum sfactor, GLenum dfactor)
- {
- if (sfactor == GL_ONE && dfactor == GL_ZERO)
- {
- glDisable(GL_BLEND);
- RenderState::StateBlock::_defaultState->setBlend(false);
- }
- else
- {
- glEnable(GL_BLEND);
- glBlendFunc(sfactor, dfactor);
- RenderState::StateBlock::_defaultState->setBlend(true);
- RenderState::StateBlock::_defaultState->setBlendSrc((RenderState::Blend)sfactor);
- RenderState::StateBlock::_defaultState->setBlendDst((RenderState::Blend)dfactor);
- }
- }
- void blendFunc(GLenum sfactor, GLenum dfactor)
- {
- #if CC_ENABLE_GL_STATE_CACHE
- if (sfactor != s_blendingSource || dfactor != s_blendingDest)
- {
- s_blendingSource = sfactor;
- s_blendingDest = dfactor;
- SetBlending(sfactor, dfactor);
- }
- #else
- SetBlending( sfactor, dfactor );
- #endif
- }
- void blendResetToCache(void)
- {
- glBlendEquation(GL_FUNC_ADD);
- #if CC_ENABLE_GL_STATE_CACHE
- SetBlending(s_blendingSource, s_blendingDest);
- #else
- SetBlending(CC_BLEND_SRC, CC_BLEND_DST);
- #endif
- }
- void bindTexture2D(GLuint textureId)
- {
- GL::bindTexture2DN(0, textureId);
- }
- void bindTexture2D(Texture2D* texture)
- {
- GL::bindTexture2DN(0, texture->getName());
- auto alphaTexID = texture->getAlphaTextureName();
- if (alphaTexID > 0) {
- GL::bindTexture2DN(1, alphaTexID);
- }
- }
- 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)
- {
- #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(textureType, textureId);
- }
- #else
- glActiveTexture(GL_TEXTURE0 + textureUnit);
- glBindTexture(textureType, textureId);
- #endif
- }
- void deleteTexture(GLuint textureId)
- {
- #if CC_ENABLE_GL_STATE_CACHE
- for (size_t i = 0; i < MAX_ACTIVE_TEXTURE; ++i)
- {
- if (s_currentBoundTexture[i] == textureId)
- {
- s_currentBoundTexture[i] = -1;
- }
- }
- #endif
-
- glDeleteTextures(1, &textureId);
- }
- void deleteTextureN(GLuint , GLuint textureId)
- {
- deleteTexture(textureId);
- }
- void activeTexture(GLenum texture)
- {
- #if CC_ENABLE_GL_STATE_CACHE
- if(s_activeTexture != texture) {
- s_activeTexture = texture;
- glActiveTexture(s_activeTexture);
- }
- #else
- glActiveTexture(texture);
- #endif
- }
- void bindVAO(GLuint vaoId)
- {
- if (Configuration::getInstance()->supportsShareableVAO())
- {
-
- #if CC_ENABLE_GL_STATE_CACHE
- if (s_VAO != vaoId)
- {
- s_VAO = vaoId;
- glBindVertexArray(vaoId);
- }
- #else
- glBindVertexArray(vaoId);
- #endif
-
- }
- }
- void enableVertexAttribs(uint32_t flags)
- {
- bindVAO(0);
-
- for(int i=0; i < MAX_ATTRIBUTES; i++) {
- unsigned int bit = 1 << i;
-
- bool enabled = (flags & bit) != 0;
- bool enabledBefore = (s_attributeFlags & bit) != 0;
- if(enabled != enabledBefore)
- {
- if( enabled )
- glEnableVertexAttribArray(i);
- else
- glDisableVertexAttribArray(i);
- }
- }
- s_attributeFlags = flags;
- }
- void setProjectionMatrixDirty( void )
- {
- s_currentProjectionMatrix = -1;
- }
- }
- NS_CC_END
|