diff --git a/cocos2dx/CCDirector.cpp b/cocos2dx/CCDirector.cpp index 3acd22b785..9fa0b5e2e4 100644 --- a/cocos2dx/CCDirector.cpp +++ b/cocos2dx/CCDirector.cpp @@ -49,7 +49,7 @@ THE SOFTWARE. #include "CCTouch.h" #include "CCUserDefault.h" #include "extensions/CCNotificationCenter.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCShaderCache.h" #include "kazmath/kazmath.h" #include "kazmath/GL/matrix.h" diff --git a/cocos2dx/CCDrawingPrimitives.cpp b/cocos2dx/CCDrawingPrimitives.cpp index a181cf3821..4d5545f7fb 100644 --- a/cocos2dx/CCDrawingPrimitives.cpp +++ b/cocos2dx/CCDrawingPrimitives.cpp @@ -29,7 +29,7 @@ THE SOFTWARE. #include "ccMacros.h" #include "CCGL.h" #include "CCDirector.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCShaderCache.h" #include "CCGLProgram.h" #include diff --git a/cocos2dx/base_nodes/CCAtlasNode.cpp b/cocos2dx/base_nodes/CCAtlasNode.cpp index 1d6d964d7b..e2b991337c 100644 --- a/cocos2dx/base_nodes/CCAtlasNode.cpp +++ b/cocos2dx/base_nodes/CCAtlasNode.cpp @@ -29,7 +29,7 @@ THE SOFTWARE. #include "CCDirector.h" #include "CCGLProgram.h" #include "CCShaderCache.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCDirector.h" #include "support/TransformUtils.h" diff --git a/cocos2dx/effects/CCGrid.cpp b/cocos2dx/effects/CCGrid.cpp index c24d7a8edd..27e1b89384 100644 --- a/cocos2dx/effects/CCGrid.cpp +++ b/cocos2dx/effects/CCGrid.cpp @@ -29,7 +29,7 @@ THE SOFTWARE. #include "support/ccUtils.h" #include "CCGLProgram.h" #include "CCShaderCache.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCGL.h" #include "CCPointExtension.h" #include "support/TransformUtils.h" diff --git a/cocos2dx/gles2/CCGLProgram.cpp b/cocos2dx/gles2/CCGLProgram.cpp index 241c424dbb..d472e527c5 100644 --- a/cocos2dx/gles2/CCGLProgram.cpp +++ b/cocos2dx/gles2/CCGLProgram.cpp @@ -1,39 +1,56 @@ -// -// Copyright 2011 Jeff Lamarche -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided -// that the following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and -// the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions -// and the following disclaimer in the documentation and/or other materials provided with the -// distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT -// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED -// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Adapted for cocos2d http://www.cocos2d-iphone.org +/**************************************************************************** +Copyright 2012 cocos2d-x.org +Copyright 2011 Jeff Lamarche +Copyright 2012 Goffredo Marocchi +Copyright 2012 Ricardo Quesada + +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 false 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. +****************************************************************************/ #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "ccMacros.h" #include "CCFileUtils.h" +#include "support/data_support/uthash.h" #include "CCString.h" +// extern +#include "kazmath/GL/matrix.h" +#include "kazmath/kazmath.h" NS_CC_BEGIN +typedef struct _hashUniformEntry +{ + GLvoid* value; // value + unsigned int location; // Key + UT_hash_handle hh; // hash entry +} tHashUniformEntry; CCGLProgram::CCGLProgram() +: program_(0) +, vertShader_(0) +, fragShader_(0) +, hashForUniforms_(NULL) { - + memset(uniforms_, 0, sizeof(uniforms_)); } CCGLProgram::~CCGLProgram() @@ -44,42 +61,61 @@ CCGLProgram::~CCGLProgram() CCAssert( vertShader_ == 0, "Vertex Shaders should have been already deleted"); CCAssert( fragShader_ == 0, "Vertex Shaders should have been already deleted"); - if (program_) + if (program_) + { ccGLDeleteProgram(program_); + } + tHashUniformEntry *current_element, *tmp; + + // Purge uniform hash + HASH_ITER(hh, hashForUniforms_, current_element, tmp) { + HASH_DEL(hashForUniforms_, current_element); + free(current_element->value); + free(current_element); + } +} + +bool CCGLProgram::initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) +{ + program_ = glCreateProgram(); + + vertShader_ = fragShader_ = 0; + + if( vShaderByteArray ) { + + if (!compileShader(&vertShader_, GL_VERTEX_SHADER, vShaderByteArray)) { + CCLOG("cocos2d: ERROR: Failed to compile vertex shader"); + } + + } + + // Create and compile fragment shader + if( fShaderByteArray ) { + + if (!compileShader(&fragShader_, GL_FRAGMENT_SHADER, fShaderByteArray)) { + CCLOG("cocos2d: ERROR: Failed to compile fragment shader"); + } + } + + if( vertShader_ ) { + glAttachShader(program_, vertShader_); + } + + if( fragShader_ ) { + glAttachShader(program_, fragShader_); + } + hashForUniforms_ = NULL; + + return true; } bool CCGLProgram::initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename) { - program_ = glCreateProgram(); - - vertShader_ = fragShader_ = 0; - - if( vShaderFilename ) { - const char *fullname = CCFileUtils::fullPathFromRelativePath(vShaderFilename); - - if (!compileShader(&vertShader_, GL_VERTEX_SHADER, fullname)) { - CCLOG("cocos2d: ERROR: Failed to compile vertex shader: %s", vShaderFilename); - } - - } - - // Create and compile fragment shader - if( fShaderFilename ) { - const char *fullname = CCFileUtils::fullPathFromRelativePath(fShaderFilename); - - if (!compileShader(&fragShader_, GL_FRAGMENT_SHADER, fullname)) { - CCLOG("cocos2d: ERROR: Failed to compile fragment shader: %s", fShaderFilename); - } - } - - if( vertShader_ ) - glAttachShader(program_, vertShader_); - - if( fragShader_ ) - glAttachShader(program_, fragShader_); - - return true; + const GLchar * vertexSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(vShaderFilename)); + const GLchar * fragmentSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(fShaderFilename)); + + return initWithVertexShaderByteArray(vertexSource, fragmentSource); } const char* CCGLProgram::description() @@ -89,12 +125,10 @@ const char* CCGLProgram::description() return strDescription; } -bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const char* file) +bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source) { GLint status; - const GLchar *source; - - source = (GLchar *)CCString::stringWithContentsOfFile(file); + if (!source) return false; @@ -106,12 +140,12 @@ bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const char* file) if( ! status ) { if( type == GL_VERTEX_SHADER ) - CCLOG("cocos2d: %s: %s", file, vertexShaderLog() ); + CCLOG("cocos2d: %s", vertexShaderLog() ); else - CCLOG("cocos2d: %s: %s", file, fragmentShaderLog() ); + CCLOG("cocos2d: %s", fragmentShaderLog() ); } - return status == GL_TRUE; + return ( status == GL_TRUE ); } void CCGLProgram::addAttribute(const char* attributeName, GLuint index) @@ -129,8 +163,8 @@ void CCGLProgram::updateUniforms() uniforms_[kCCUniformSampler] = glGetUniformLocation(program_, kCCUniformSampler_s); - ccGLUseProgram( program_ ); - glUniform1i( uniforms_[kCCUniformSampler], 0 ); + this->use(); + this->setUniformLocationWith1i( uniforms_[kCCUniformSampler], 0 ); } bool CCGLProgram::link() @@ -189,7 +223,7 @@ const char* CCGLProgram::logForOpenGLObject(GLuint object, GLInfoFunction infoFu const char* CCGLProgram::vertexShaderLog() { - return this->logForOpenGLObject(vertShader_, (GLInfoFunction)&glGetProgramiv, (GLLogFunction)&glGetProgramInfoLog); + return this->logForOpenGLObject(vertShader_, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog); } const char* CCGLProgram::fragmentShaderLog() @@ -202,5 +236,126 @@ const char* CCGLProgram::programLog() return this->logForOpenGLObject(program_, (GLInfoFunction)&glGetProgramiv, (GLLogFunction)&glGetProgramInfoLog); } +// Uniform cache + +bool CCGLProgram::updateUniformLocation(unsigned int location, GLvoid* data, unsigned int bytes) +{ + bool updated = true; + tHashUniformEntry *element = NULL; + HASH_FIND_INT(hashForUniforms_, &location, element); + + if( ! element ) { + + element = (tHashUniformEntry*)malloc( sizeof(*element) ); + + // key + element->location = location; + + // value + element->value = malloc( bytes ); + memcpy(element->value, data, bytes ); + + HASH_ADD_INT(hashForUniforms_, location, element); + } + else + { + if( memcmp( element->value, data, bytes) == 0 ) + updated = false; + else + memcpy( element->value, data, bytes ); + } + + return updated; +} + +void CCGLProgram::setUniformLocationWith1i(unsigned int location, GLint i1) +{ + bool updated = updateUniformLocation(location, &i1, sizeof(i1)*1); + + if( updated ) + glUniform1i( (GLint)location, i1); +} + +void CCGLProgram::setUniformLocationWith1f(unsigned int location, GLfloat f1) +{ + bool updated = updateUniformLocation(location, &f1, sizeof(f1)*1); + + if( updated ) + glUniform1f( (GLint)location, f1); +} + +void CCGLProgram::setUniformLocationWith2f(unsigned int location, GLfloat f1, GLfloat f2) +{ + GLfloat floats[2] = {f1,f2}; + bool updated = updateUniformLocation(location, floats, sizeof(floats)); + + if( updated ) + glUniform2f( (GLint)location, f1, f2); +} + +void CCGLProgram::setUniformLocationWith3f(unsigned int location, GLfloat f1, GLfloat f2, GLfloat f3) +{ + GLfloat floats[3] = {f1,f2,f3}; + bool updated = updateUniformLocation(location, floats, sizeof(floats)); + + if( updated ) + glUniform3f( (GLint)location, f1, f2, f3); +} + +void CCGLProgram::setUniformLocationWith4f(unsigned int location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4) +{ + GLfloat floats[4] = {f1,f2,f3,f4}; + bool updated = updateUniformLocation(location, floats, sizeof(floats)); + + if( updated ) + glUniform4f( (GLint)location, f1, f2, f3,f4); +} + +void CCGLProgram::setUniformLocationWith2fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays) +{ + bool updated = updateUniformLocation(location, floats, sizeof(float)*2*numberOfArrays); + + if( updated ) + glUniform2fv( (GLint)location, (GLsizei)numberOfArrays, floats ); +} + +void CCGLProgram::setUniformLocationWith3fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays) +{ + bool updated = updateUniformLocation(location, floats, sizeof(float)*3*numberOfArrays); + + if( updated ) + glUniform3fv( (GLint)location, (GLsizei)numberOfArrays, floats ); +} + +void CCGLProgram::setUniformLocationWith4fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays) +{ + bool updated = updateUniformLocation(location, floats, sizeof(float)*4*numberOfArrays); + + if( updated ) + glUniform4fv( (GLint)location, (GLsizei)numberOfArrays, floats ); +} + + +void CCGLProgram::setUniformLocationwithMatrix4fv(unsigned int location, GLfloat* matrixArray, unsigned int numberOfMatrices) +{ + bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*16*numberOfMatrices); + + if( updated ) + glUniformMatrix4fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray); +} + +void CCGLProgram::setUniformForModelViewProjectionMatrix() +{ + kmMat4 matrixP; + kmMat4 matrixMV; + kmMat4 matrixMVP; + + kmGLGetMatrix(KM_GL_PROJECTION, &matrixP ); + kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV ); + + kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV); + + setUniformLocationwithMatrix4fv(uniforms_[kCCUniformMVPMatrix], matrixMVP.mat, 1); +} NS_CC_END diff --git a/cocos2dx/gles2/ccGLState.cpp b/cocos2dx/gles2/ccGLStateCache.cpp similarity index 93% rename from cocos2dx/gles2/ccGLState.cpp rename to cocos2dx/gles2/ccGLStateCache.cpp index e6f795fd92..758547eb3f 100644 --- a/cocos2dx/gles2/ccGLState.cpp +++ b/cocos2dx/gles2/ccGLStateCache.cpp @@ -23,7 +23,7 @@ * THE SOFTWARE. */ -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCGLProgram.h" #include "CCDirector.h" #include "ccConfig.h" @@ -54,7 +54,10 @@ static int _ccGLServerState = 0; void ccGLInvalidateStateCache( void ) { kmGLFreeAll(); - + _ccCurrentProjectionMatrix = -1; + _vertexAttribPosition = false; + _vertexAttribColor = false; + _vertexAttribTexCoords = false; #if CC_ENABLE_GL_STATE_CACHE _ccCurrentShaderProgram = -1; for( int i=0; i < kCCMaxActiveTexture; i++ ) @@ -217,20 +220,6 @@ void ccGLEnableVertexAttribs( unsigned int flags ) //#pragma mark - GL Uniforms functions -void ccGLUniformModelViewProjectionMatrix( CCGLProgram *shaderProgram ) -{ - kmMat4 matrixP; - kmMat4 matrixMV; - kmMat4 matrixMVP; - - kmGLGetMatrix(KM_GL_PROJECTION, &matrixP ); - kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV ); - - kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV); - - glUniformMatrix4fv( shaderProgram->uniforms_[kCCUniformMVPMatrix], 1, GL_FALSE, matrixMVP.mat); -} - void ccSetProjectionMatrixDirty( void ) { _ccCurrentProjectionMatrix = -1; diff --git a/cocos2dx/include/CCGLProgram.h b/cocos2dx/include/CCGLProgram.h index ff99748d4a..8397c01322 100644 --- a/cocos2dx/include/CCGLProgram.h +++ b/cocos2dx/include/CCGLProgram.h @@ -1,27 +1,29 @@ -// -// Copyright 2011 Jeff Lamarche -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided -// that the following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and -// the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions -// and the following disclaimer in the documentation and/or other materials provided with the -// distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT -// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED -// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// -// Adapted for cocos2d http://www.cocos2d-iphone.org +/**************************************************************************** +Copyright 2012 cocos2d-x.org +Copyright 2011 Jeff Lamarche +Copyright 2012 Goffredo Marocchi +Copyright 2012 Ricardo Quesada + +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. +****************************************************************************/ #ifndef __CCGLPROGRAM_H__ #define __CCGLPROGRAM_H__ @@ -52,6 +54,7 @@ enum { #define kCCShader_PositionTexture "ShaderPositionTexture" #define kCCShader_PositionTexture_uColor "ShaderPositionTexture_uColor" #define kCCShader_PositionTextureA8Color "ShaderPositionTextureA8Color" +#define kCCShader_Position_uColor "ShaderPosition_uColor" // uniform names #define kCCUniformMVPMatrix_s "u_MVPMatrix" @@ -63,6 +66,8 @@ enum { #define kCCAttributeNamePosition "a_position" #define kCCAttributeNameTexCoord "a_texCoord" +struct _hashUniformEntry; + typedef void (*GLInfoFunction)(GLuint program, GLenum pname, GLint* params); @@ -71,36 +76,77 @@ typedef void (*GLLogFunction) (GLuint program, GLsizei* length, GLchar* infolog); -/** CCGLProgram - Class that implements a glProgram +/** CCGLProgram + Class that implements a glProgram + + + @since v2.0.0 */ class CC_DLL CCGLProgram : public CCObject { public: CCGLProgram(); virtual ~CCGLProgram(); + /** Initializes the CCGLProgram with a vertex and fragment with bytes array */ + bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray); + /** Initializes the CCGLProgram with a vertex and fragment with contents of filenames */ bool initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename); + /** It will add a new attribute to the shader */ void addAttribute(const char* attributeName, GLuint index); + /** links the glProgram */ bool link(); + /** it will call glUseProgram() */ void use(); -/* It will create 3 uniforms: +/** It will create 3 uniforms: - kCCUniformPMatrix - kCCUniformMVMatrix - kCCUniformSampler And it will bind "kCCUniformSampler" to 0 - @since v2.0.0 */ void updateUniforms(); + /** calls glUniform1i only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith1i(unsigned int location, GLint i1); + + /** calls glUniform1f only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith1f(unsigned int location, GLfloat f1); + + /** calls glUniform2f only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith2f(unsigned int location, GLfloat f1, GLfloat f2); + + /** calls glUniform3f only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith3f(unsigned int location, GLfloat f1, GLfloat f2, GLfloat f3); + + /** calls glUniform4f only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith4f(unsigned int location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4); + + /** calls glUniform2fv only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith2fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays); + + /** calls glUniform3fv only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith3fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays); + + /** calls glUniform4fv only if the values are different than the previous call for this same shader program. */ + void setUniformLocationWith4fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays); + + /** calls glUniformMatrix4fv only if the values are different than the previous call for this same shader program. */ + void setUniformLocationwithMatrix4fv(unsigned int location, GLfloat* matrixArray, unsigned int numberOfMatrices); + + /** will update the MVP matrix on the MVP uniform if it is different than the previous call for this same shader program. */ + void setUniformForModelViewProjectionMatrix(); + /** returns the vertexShader error log */ const char* vertexShaderLog(); + /** returns the fragmentShader error log */ const char* fragmentShaderLog(); + /** returns the program error log */ const char* programLog(); private: + bool updateUniformLocation(unsigned int location, GLvoid* data, unsigned int bytes); const char* description(); - bool compileShader(GLuint * shader, GLenum type, const char* file); + bool compileShader(GLuint * shader, GLenum type, const GLchar* source); const char* logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc); public: @@ -108,6 +154,8 @@ public: GLuint vertShader_; GLuint fragShader_; GLint uniforms_[kCCUniform_MAX]; +private: + struct _hashUniformEntry* hashForUniforms_; }; NS_CC_END diff --git a/cocos2dx/include/CCNode.h b/cocos2dx/include/CCNode.h index 1d7a61360b..fffa033a05 100755 --- a/cocos2dx/include/CCNode.h +++ b/cocos2dx/include/CCNode.h @@ -32,7 +32,7 @@ #include "CCAffineTransform.h" #include "CCArray.h" #include "CCGL.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "kazmath/kazmath.h" namespace cocos2d { diff --git a/cocos2dx/include/ccGLState.h b/cocos2dx/include/ccGLStateCache.h similarity index 96% rename from cocos2dx/include/ccGLState.h rename to cocos2dx/include/ccGLStateCache.h index 3449c048fe..37f5601a27 100644 --- a/cocos2dx/include/ccGLState.h +++ b/cocos2dx/include/ccGLStateCache.h @@ -56,7 +56,7 @@ typedef enum { } ccGLServerState; -/** @file ccGLState.h +/** @file ccGLStateCache.h */ /** Invalidates the GL state cache. @@ -83,11 +83,6 @@ void CC_DLL ccGLDeleteProgram( GLuint program ); */ void CC_DLL ccGLBlendFunc(GLenum sfactor, GLenum dfactor); -/** sets the ModelViewProjection Matrix in the GL program - @since v2.0.0 - */ -void CC_DLL ccGLUniformModelViewProjectionMatrix( CCGLProgram *shaderProgram ); - /** sets the projection matrix as dirty @since v2.0.0 */ diff --git a/cocos2dx/include/cocos2d.h b/cocos2dx/include/cocos2d.h index 3d98e815fc..17b9c9b185 100755 --- a/cocos2dx/include/cocos2d.h +++ b/cocos2dx/include/cocos2d.h @@ -118,7 +118,7 @@ THE SOFTWARE. #include "extensions/CCNotificationCenter.h" // Shaders #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCShaderCache.h" // // cocos2d macros diff --git a/cocos2dx/label_nodes/CCLabelAtlas.cpp b/cocos2dx/label_nodes/CCLabelAtlas.cpp index 1657af2fa0..757b1ae6b6 100644 --- a/cocos2dx/label_nodes/CCLabelAtlas.cpp +++ b/cocos2dx/label_nodes/CCLabelAtlas.cpp @@ -30,7 +30,7 @@ THE SOFTWARE. #include "ccConfig.h" #include "CCShaderCache.h" #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCDirector.h" #include "support/TransformUtils.h" // external diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 4a17af9a70..7490b07041 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -34,7 +34,7 @@ THE SOFTWARE. #include "CCScriptSupport.h" #include "CCShaderCache.h" #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "support/TransformUtils.h" // extern #include "kazmath/GL/matrix.h" diff --git a/cocos2dx/misc_nodes/CCMotionStreak.cpp b/cocos2dx/misc_nodes/CCMotionStreak.cpp index 837d7abe72..856528b46b 100644 --- a/cocos2dx/misc_nodes/CCMotionStreak.cpp +++ b/cocos2dx/misc_nodes/CCMotionStreak.cpp @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ #include "CCMotionStreak.h" #include "CCTextureCache.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCGLProgram.h" #include "CCShaderCache.h" #include "ccMacros.h" diff --git a/cocos2dx/misc_nodes/CCProgressTimer.cpp b/cocos2dx/misc_nodes/CCProgressTimer.cpp index f705186eea..68338f5b73 100644 --- a/cocos2dx/misc_nodes/CCProgressTimer.cpp +++ b/cocos2dx/misc_nodes/CCProgressTimer.cpp @@ -29,7 +29,7 @@ THE SOFTWARE. #include "CCPointExtension.h" #include "CCGLProgram.h" #include "CCShaderCache.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCDirector.h" #include "support/TransformUtils.h" #include "CCDrawingPrimitives.h" diff --git a/cocos2dx/misc_nodes/CCRenderTexture.cpp b/cocos2dx/misc_nodes/CCRenderTexture.cpp index 64893ed848..c8e928cd4d 100644 --- a/cocos2dx/misc_nodes/CCRenderTexture.cpp +++ b/cocos2dx/misc_nodes/CCRenderTexture.cpp @@ -29,7 +29,7 @@ THE SOFTWARE. #include "platform/platform.h" #include "CCImage.h" #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCConfiguration.h" #include "support/ccUtils.h" #include "CCTextureCache.h" diff --git a/cocos2dx/particle_nodes/CCParticleBatchNode.cpp b/cocos2dx/particle_nodes/CCParticleBatchNode.cpp index 823d0863d3..1cd47f4e5f 100644 --- a/cocos2dx/particle_nodes/CCParticleBatchNode.cpp +++ b/cocos2dx/particle_nodes/CCParticleBatchNode.cpp @@ -38,7 +38,7 @@ #include "CCParticleSystem.h" #include "CCShaderCache.h" #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "support/base64.h" #include "support/zip_support/ZipUtils.h" diff --git a/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp b/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp index e235febb84..32c234d6c2 100644 --- a/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp +++ b/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp @@ -35,7 +35,7 @@ THE SOFTWARE. #include "CCTextureAtlas.h" #include "CCDirector.h" #include "CCShaderCache.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCGLProgram.h" #include "support/TransformUtils.h" diff --git a/cocos2dx/proj.win32/cocos2d-win32.vcproj b/cocos2dx/proj.win32/cocos2d-win32.vcproj index 09d9fe7a45..a3ce2b03fa 100644 --- a/cocos2dx/proj.win32/cocos2d-win32.vcproj +++ b/cocos2dx/proj.win32/cocos2d-win32.vcproj @@ -424,7 +424,7 @@ > = u_alpha_value ) => fail if incoming_pixel < u_alpha_value \n\ + \n\ + if ( texColor.a <= u_alpha_value ) \n\ + discard; \n\ + \n\ + gl_FragColor = texColor * v_fragmentColor; \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_PositionTextureColor_frag.h b/cocos2dx/shaders/ccShader_PositionTextureColor_frag.h new file mode 100644 index 0000000000..aba7e21728 --- /dev/null +++ b/cocos2dx/shaders/ccShader_PositionTextureColor_frag.h @@ -0,0 +1,14 @@ +" \n\ +#ifdef GL_ES \n\ +precision lowp float; \n\ +#endif \n\ + \n\ +varying vec4 v_fragmentColor; \n\ +varying vec2 v_texCoord; \n\ +uniform sampler2D u_texture; \n\ + \n\ +void main() \n\ +{ \n\ + gl_FragColor = v_fragmentColor * texture2D(u_texture, v_texCoord); \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_PositionTextureColor_vert.h b/cocos2dx/shaders/ccShader_PositionTextureColor_vert.h new file mode 100644 index 0000000000..cf2361deb6 --- /dev/null +++ b/cocos2dx/shaders/ccShader_PositionTextureColor_vert.h @@ -0,0 +1,22 @@ +" \n\ +attribute vec4 a_position; \n\ +attribute vec2 a_texCoord; \n\ +attribute vec4 a_color; \n\ + \n\ +uniform mat4 u_MVPMatrix; \n\ + \n\ +#ifdef GL_ES \n\ +varying lowp vec4 v_fragmentColor; \n\ +varying mediump vec2 v_texCoord; \n\ +#else \n\ +varying vec4 v_fragmentColor; \n\ +varying vec2 v_texCoord; \n\ +#endif \n\ + \n\ +void main() \n\ +{ \n\ + gl_Position = u_MVPMatrix * a_position; \n\ + v_fragmentColor = a_color; \n\ + v_texCoord = a_texCoord; \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_PositionTexture_frag.h b/cocos2dx/shaders/ccShader_PositionTexture_frag.h new file mode 100644 index 0000000000..d7af10efab --- /dev/null +++ b/cocos2dx/shaders/ccShader_PositionTexture_frag.h @@ -0,0 +1,13 @@ +" \n\ +#ifdef GL_ES \n\ +precision lowp float; \n\ +#endif \n\ + \n\ +varying vec2 v_texCoord; \n\ +uniform sampler2D u_texture; \n\ + \n\ +void main() \n\ +{ \n\ + gl_FragColor = texture2D(u_texture, v_texCoord); \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_PositionTexture_uColor_frag.h b/cocos2dx/shaders/ccShader_PositionTexture_uColor_frag.h new file mode 100644 index 0000000000..019f2ef017 --- /dev/null +++ b/cocos2dx/shaders/ccShader_PositionTexture_uColor_frag.h @@ -0,0 +1,16 @@ +" \n\ +#ifdef GL_ES \n\ +precision lowp float; \n\ +#endif \n\ + \n\ +uniform vec4 u_color; \n\ + \n\ +varying vec2 v_texCoord; \n\ + \n\ +uniform sampler2D u_texture; \n\ + \n\ +void main() \n\ +{ \n\ + gl_FragColor = texture2D(u_texture, v_texCoord) * u_color; \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_PositionTexture_uColor_vert.h b/cocos2dx/shaders/ccShader_PositionTexture_uColor_vert.h new file mode 100644 index 0000000000..ca36ff0f91 --- /dev/null +++ b/cocos2dx/shaders/ccShader_PositionTexture_uColor_vert.h @@ -0,0 +1,18 @@ +" \n\ +attribute vec4 a_position; \n\ +attribute vec2 a_texCoord; \n\ + \n\ +uniform mat4 u_MVPMatrix; \n\ + \n\ +#ifdef GL_ES \n\ +varying mediump vec2 v_texCoord; \n\ +#else \n\ +varying vec2 v_texCoord; \n\ +#endif \n\ + \n\ +void main() \n\ +{ \n\ + gl_Position = u_MVPMatrix * a_position; \n\ + v_texCoord = a_texCoord; \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_PositionTexture_vert.h b/cocos2dx/shaders/ccShader_PositionTexture_vert.h new file mode 100644 index 0000000000..98ceab5a8d --- /dev/null +++ b/cocos2dx/shaders/ccShader_PositionTexture_vert.h @@ -0,0 +1,17 @@ +" \n\ +attribute vec4 a_position; \n\ +attribute vec2 a_texCoord; \n\ +uniform mat4 u_MVPMatrix; \n\ + \n\ +#ifdef GL_ES \n\ +varying mediump vec2 v_texCoord; \n\ +#else \n\ +varying vec2 v_texCoord; \n\ +#endif \n\ + \n\ +void main() \n\ +{ \n\ + gl_Position = u_MVPMatrix * a_position; \n\ + v_texCoord = a_texCoord; \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_Position_uColor_frag.h b/cocos2dx/shaders/ccShader_Position_uColor_frag.h new file mode 100644 index 0000000000..48e5ae6966 --- /dev/null +++ b/cocos2dx/shaders/ccShader_Position_uColor_frag.h @@ -0,0 +1,12 @@ +" \n\ +#ifdef GL_ES \n\ +precision lowp float; \n\ +#endif \n\ + \n\ +varying vec4 v_fragmentColor; \n\ + \n\ +void main() \n\ +{ \n\ + gl_FragColor = v_fragmentColor; \n\ +} \n\ +"; diff --git a/cocos2dx/shaders/ccShader_Position_uColor_vert.h b/cocos2dx/shaders/ccShader_Position_uColor_vert.h new file mode 100644 index 0000000000..9f1ba380bb --- /dev/null +++ b/cocos2dx/shaders/ccShader_Position_uColor_vert.h @@ -0,0 +1,19 @@ +" \n\ +attribute vec4 a_position; \n\ +uniform mat4 u_MVPMatrix; \n\ +uniform vec4 u_color; \n\ +uniform float u_pointSize; \n\ + \n\ +#ifdef GL_ES \n\ +varying lowp vec4 v_fragmentColor; \n\ +#else \n\ +varying vec4 v_fragmentColor; \n\ +#endif \n\ + \n\ +void main() \n\ +{ \n\ + gl_Position = u_MVPMatrix * a_position; \n\ + gl_PointSize = u_pointSize; \n\ + v_fragmentColor = u_color; \n\ +} \n\ +"; diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 2e81176cee..3a52e1a703 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -34,7 +34,7 @@ THE SOFTWARE. #include "CCTextureCache.h" #include "CCDrawingPrimitives.h" #include "CCShaderCache.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCGLProgram.h" #include "CCDirector.h" #include "CCPointExtension.h" @@ -561,6 +561,10 @@ void CCSprite::draw(void) { ccGLBindTexture2D( m_pobTexture->getName() ); } + else + { + ccGLBindTexture2D(0); + } // // Attributes diff --git a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp index 97baccc09a..a990ac8728 100644 --- a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp @@ -33,7 +33,7 @@ THE SOFTWARE. #include "CCPointExtension.h" #include "CCShaderCache.h" #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCDirector.h" #include "support/TransformUtils.h" #include "support/CCProfiling.h" diff --git a/cocos2dx/textures/CCTexture2D.cpp b/cocos2dx/textures/CCTexture2D.cpp index c6935c42ed..1f4232672b 100644 --- a/cocos2dx/textures/CCTexture2D.cpp +++ b/cocos2dx/textures/CCTexture2D.cpp @@ -43,7 +43,7 @@ THE SOFTWARE. #include "CCTexturePVR.h" #include "CCDirector.h" #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" #include "CCShaderCache.h" #if CC_ENABLE_CACHE_TEXTTURE_DATA diff --git a/cocos2dx/textures/CCTextureAtlas.cpp b/cocos2dx/textures/CCTextureAtlas.cpp index 544c4ef68c..1588a70614 100644 --- a/cocos2dx/textures/CCTextureAtlas.cpp +++ b/cocos2dx/textures/CCTextureAtlas.cpp @@ -29,7 +29,7 @@ THE SOFTWARE. #include "CCTextureCache.h" #include "ccMacros.h" #include "CCGLProgram.h" -#include "ccGLState.h" +#include "ccGLStateCache.h" // support #include "CCTexture2D.h"