issue #1056: Rename ccGLState to ccGLStateCache, add some ccShader_***_.h, update CCGLProgram class.

This commit is contained in:
James Chen 2012-03-15 10:42:22 +08:00
parent 8aa3f7f9ae
commit 9e89171557
35 changed files with 546 additions and 135 deletions

View File

@ -49,7 +49,7 @@ THE SOFTWARE.
#include "CCTouch.h" #include "CCTouch.h"
#include "CCUserDefault.h" #include "CCUserDefault.h"
#include "extensions/CCNotificationCenter.h" #include "extensions/CCNotificationCenter.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "kazmath/kazmath.h" #include "kazmath/kazmath.h"
#include "kazmath/GL/matrix.h" #include "kazmath/GL/matrix.h"

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "ccMacros.h" #include "ccMacros.h"
#include "CCGL.h" #include "CCGL.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include <string.h> #include <string.h>

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "CCDirector.h" #include "CCDirector.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "support/TransformUtils.h" #include "support/TransformUtils.h"

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "support/ccUtils.h" #include "support/ccUtils.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCGL.h" #include "CCGL.h"
#include "CCPointExtension.h" #include "CCPointExtension.h"
#include "support/TransformUtils.h" #include "support/TransformUtils.h"

View File

@ -1,39 +1,56 @@
// /****************************************************************************
// Copyright 2011 Jeff Lamarche Copyright 2012 cocos2d-x.org
// Copyright 2011 Jeff Lamarche
// Redistribution and use in source and binary forms, with or without modification, are permitted provided Copyright 2012 Goffredo Marocchi
// that the following conditions are met: Copyright 2012 Ricardo Quesada
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
// the following disclaimer. http://www.cocos2d-x.org
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions Permission is hereby granted, free of charge, to any person obtaining a copy
// and the following disclaimer in the documentation and/or other materials provided with the of this software and associated documentation files (the "Software"), to deal
// distribution. in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED copies of the Software, and to permit persons to whom the Software is
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND furnished to do so, subject to the following conditions:
// 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 The above copyright notice and this permission notice shall be included in
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS all copies or substantial portions of the Software.
// 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 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// Adapted for cocos2d http://www.cocos2d-iphone.org 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 "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "ccMacros.h" #include "ccMacros.h"
#include "CCFileUtils.h" #include "CCFileUtils.h"
#include "support/data_support/uthash.h"
#include "CCString.h" #include "CCString.h"
// extern
#include "kazmath/GL/matrix.h"
#include "kazmath/kazmath.h"
NS_CC_BEGIN NS_CC_BEGIN
typedef struct _hashUniformEntry
{
GLvoid* value; // value
unsigned int location; // Key
UT_hash_handle hh; // hash entry
} tHashUniformEntry;
CCGLProgram::CCGLProgram() CCGLProgram::CCGLProgram()
: program_(0)
, vertShader_(0)
, fragShader_(0)
, hashForUniforms_(NULL)
{ {
memset(uniforms_, 0, sizeof(uniforms_));
} }
CCGLProgram::~CCGLProgram() CCGLProgram::~CCGLProgram()
@ -45,43 +62,62 @@ CCGLProgram::~CCGLProgram()
CCAssert( fragShader_ == 0, "Vertex Shaders should have been already deleted"); CCAssert( fragShader_ == 0, "Vertex Shaders should have been already deleted");
if (program_) if (program_)
{
ccGLDeleteProgram(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::initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename) bool CCGLProgram::initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{ {
program_ = glCreateProgram(); program_ = glCreateProgram();
vertShader_ = fragShader_ = 0; vertShader_ = fragShader_ = 0;
if( vShaderFilename ) { if( vShaderByteArray ) {
const char *fullname = CCFileUtils::fullPathFromRelativePath(vShaderFilename);
if (!compileShader(&vertShader_, GL_VERTEX_SHADER, fullname)) { if (!compileShader(&vertShader_, GL_VERTEX_SHADER, vShaderByteArray)) {
CCLOG("cocos2d: ERROR: Failed to compile vertex shader: %s", vShaderFilename); CCLOG("cocos2d: ERROR: Failed to compile vertex shader");
} }
} }
// Create and compile fragment shader // Create and compile fragment shader
if( fShaderFilename ) { if( fShaderByteArray ) {
const char *fullname = CCFileUtils::fullPathFromRelativePath(fShaderFilename);
if (!compileShader(&fragShader_, GL_FRAGMENT_SHADER, fullname)) { if (!compileShader(&fragShader_, GL_FRAGMENT_SHADER, fShaderByteArray)) {
CCLOG("cocos2d: ERROR: Failed to compile fragment shader: %s", fShaderFilename); CCLOG("cocos2d: ERROR: Failed to compile fragment shader");
} }
} }
if( vertShader_ ) if( vertShader_ ) {
glAttachShader(program_, vertShader_); glAttachShader(program_, vertShader_);
}
if( fragShader_ ) if( fragShader_ ) {
glAttachShader(program_, fragShader_); glAttachShader(program_, fragShader_);
}
hashForUniforms_ = NULL;
return true; return true;
} }
bool CCGLProgram::initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename)
{
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() const char* CCGLProgram::description()
{ {
static char strDescription[100] = {0}; static char strDescription[100] = {0};
@ -89,12 +125,10 @@ const char* CCGLProgram::description()
return strDescription; return strDescription;
} }
bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const char* file) bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source)
{ {
GLint status; GLint status;
const GLchar *source;
source = (GLchar *)CCString::stringWithContentsOfFile(file);
if (!source) if (!source)
return false; return false;
@ -106,12 +140,12 @@ bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const char* file)
if( ! status ) { if( ! status ) {
if( type == GL_VERTEX_SHADER ) if( type == GL_VERTEX_SHADER )
CCLOG("cocos2d: %s: %s", file, vertexShaderLog() ); CCLOG("cocos2d: %s", vertexShaderLog() );
else 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) void CCGLProgram::addAttribute(const char* attributeName, GLuint index)
@ -129,8 +163,8 @@ void CCGLProgram::updateUniforms()
uniforms_[kCCUniformSampler] = glGetUniformLocation(program_, kCCUniformSampler_s); uniforms_[kCCUniformSampler] = glGetUniformLocation(program_, kCCUniformSampler_s);
ccGLUseProgram( program_ ); this->use();
glUniform1i( uniforms_[kCCUniformSampler], 0 ); this->setUniformLocationWith1i( uniforms_[kCCUniformSampler], 0 );
} }
bool CCGLProgram::link() bool CCGLProgram::link()
@ -189,7 +223,7 @@ const char* CCGLProgram::logForOpenGLObject(GLuint object, GLInfoFunction infoFu
const char* CCGLProgram::vertexShaderLog() const char* CCGLProgram::vertexShaderLog()
{ {
return this->logForOpenGLObject(vertShader_, (GLInfoFunction)&glGetProgramiv, (GLLogFunction)&glGetProgramInfoLog); return this->logForOpenGLObject(vertShader_, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog);
} }
const char* CCGLProgram::fragmentShaderLog() const char* CCGLProgram::fragmentShaderLog()
@ -202,5 +236,126 @@ const char* CCGLProgram::programLog()
return this->logForOpenGLObject(program_, (GLInfoFunction)&glGetProgramiv, (GLLogFunction)&glGetProgramInfoLog); 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 NS_CC_END

View File

@ -23,7 +23,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "ccConfig.h" #include "ccConfig.h"
@ -54,7 +54,10 @@ static int _ccGLServerState = 0;
void ccGLInvalidateStateCache( void ) void ccGLInvalidateStateCache( void )
{ {
kmGLFreeAll(); kmGLFreeAll();
_ccCurrentProjectionMatrix = -1;
_vertexAttribPosition = false;
_vertexAttribColor = false;
_vertexAttribTexCoords = false;
#if CC_ENABLE_GL_STATE_CACHE #if CC_ENABLE_GL_STATE_CACHE
_ccCurrentShaderProgram = -1; _ccCurrentShaderProgram = -1;
for( int i=0; i < kCCMaxActiveTexture; i++ ) for( int i=0; i < kCCMaxActiveTexture; i++ )
@ -217,20 +220,6 @@ void ccGLEnableVertexAttribs( unsigned int flags )
//#pragma mark - GL Uniforms functions //#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 ) void ccSetProjectionMatrixDirty( void )
{ {
_ccCurrentProjectionMatrix = -1; _ccCurrentProjectionMatrix = -1;

View File

@ -1,27 +1,29 @@
// /****************************************************************************
// Copyright 2011 Jeff Lamarche Copyright 2012 cocos2d-x.org
// Copyright 2011 Jeff Lamarche
// Redistribution and use in source and binary forms, with or without modification, are permitted provided Copyright 2012 Goffredo Marocchi
// that the following conditions are met: Copyright 2012 Ricardo Quesada
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
// the following disclaimer. http://www.cocos2d-x.org
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions Permission is hereby granted, free of charge, to any person obtaining a copy
// and the following disclaimer in the documentation and/or other materials provided with the of this software and associated documentation files (the "Software"), to deal
// distribution. in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED copies of the Software, and to permit persons to whom the Software is
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND furnished to do so, subject to the following conditions:
// 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 The above copyright notice and this permission notice shall be included in
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS all copies or substantial portions of the Software.
// 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 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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,
// Adapted for cocos2d http://www.cocos2d-iphone.org OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCGLPROGRAM_H__ #ifndef __CCGLPROGRAM_H__
#define __CCGLPROGRAM_H__ #define __CCGLPROGRAM_H__
@ -52,6 +54,7 @@ enum {
#define kCCShader_PositionTexture "ShaderPositionTexture" #define kCCShader_PositionTexture "ShaderPositionTexture"
#define kCCShader_PositionTexture_uColor "ShaderPositionTexture_uColor" #define kCCShader_PositionTexture_uColor "ShaderPositionTexture_uColor"
#define kCCShader_PositionTextureA8Color "ShaderPositionTextureA8Color" #define kCCShader_PositionTextureA8Color "ShaderPositionTextureA8Color"
#define kCCShader_Position_uColor "ShaderPosition_uColor"
// uniform names // uniform names
#define kCCUniformMVPMatrix_s "u_MVPMatrix" #define kCCUniformMVPMatrix_s "u_MVPMatrix"
@ -63,6 +66,8 @@ enum {
#define kCCAttributeNamePosition "a_position" #define kCCAttributeNamePosition "a_position"
#define kCCAttributeNameTexCoord "a_texCoord" #define kCCAttributeNameTexCoord "a_texCoord"
struct _hashUniformEntry;
typedef void (*GLInfoFunction)(GLuint program, typedef void (*GLInfoFunction)(GLuint program,
GLenum pname, GLenum pname,
GLint* params); GLint* params);
@ -73,34 +78,75 @@ typedef void (*GLLogFunction) (GLuint program,
/** CCGLProgram /** CCGLProgram
Class that implements a glProgram Class that implements a glProgram
@since v2.0.0
*/ */
class CC_DLL CCGLProgram : public CCObject class CC_DLL CCGLProgram : public CCObject
{ {
public: public:
CCGLProgram(); CCGLProgram();
virtual ~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); bool initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename);
/** It will add a new attribute to the shader */
void addAttribute(const char* attributeName, GLuint index); void addAttribute(const char* attributeName, GLuint index);
/** links the glProgram */
bool link(); bool link();
/** it will call glUseProgram() */
void use(); void use();
/* It will create 3 uniforms: /** It will create 3 uniforms:
- kCCUniformPMatrix - kCCUniformPMatrix
- kCCUniformMVMatrix - kCCUniformMVMatrix
- kCCUniformSampler - kCCUniformSampler
And it will bind "kCCUniformSampler" to 0 And it will bind "kCCUniformSampler" to 0
@since v2.0.0
*/ */
void updateUniforms(); 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(); const char* vertexShaderLog();
/** returns the fragmentShader error log */
const char* fragmentShaderLog(); const char* fragmentShaderLog();
/** returns the program error log */
const char* programLog(); const char* programLog();
private: private:
bool updateUniformLocation(unsigned int location, GLvoid* data, unsigned int bytes);
const char* description(); 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); const char* logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc);
public: public:
@ -108,6 +154,8 @@ public:
GLuint vertShader_; GLuint vertShader_;
GLuint fragShader_; GLuint fragShader_;
GLint uniforms_[kCCUniform_MAX]; GLint uniforms_[kCCUniform_MAX];
private:
struct _hashUniformEntry* hashForUniforms_;
}; };
NS_CC_END NS_CC_END

View File

@ -32,7 +32,7 @@
#include "CCAffineTransform.h" #include "CCAffineTransform.h"
#include "CCArray.h" #include "CCArray.h"
#include "CCGL.h" #include "CCGL.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "kazmath/kazmath.h" #include "kazmath/kazmath.h"
namespace cocos2d { namespace cocos2d {

View File

@ -56,7 +56,7 @@ typedef enum {
} ccGLServerState; } ccGLServerState;
/** @file ccGLState.h /** @file ccGLStateCache.h
*/ */
/** Invalidates the GL state cache. /** Invalidates the GL state cache.
@ -83,11 +83,6 @@ void CC_DLL ccGLDeleteProgram( GLuint program );
*/ */
void CC_DLL ccGLBlendFunc(GLenum sfactor, GLenum dfactor); 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 /** sets the projection matrix as dirty
@since v2.0.0 @since v2.0.0
*/ */

View File

@ -118,7 +118,7 @@ THE SOFTWARE.
#include "extensions/CCNotificationCenter.h" #include "extensions/CCNotificationCenter.h"
// Shaders // Shaders
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
// //
// cocos2d macros // cocos2d macros

View File

@ -30,7 +30,7 @@ THE SOFTWARE.
#include "ccConfig.h" #include "ccConfig.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "support/TransformUtils.h" #include "support/TransformUtils.h"
// external // external

View File

@ -34,7 +34,7 @@ THE SOFTWARE.
#include "CCScriptSupport.h" #include "CCScriptSupport.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "support/TransformUtils.h" #include "support/TransformUtils.h"
// extern // extern
#include "kazmath/GL/matrix.h" #include "kazmath/GL/matrix.h"

View File

@ -24,7 +24,7 @@ THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#include "CCMotionStreak.h" #include "CCMotionStreak.h"
#include "CCTextureCache.h" #include "CCTextureCache.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "ccMacros.h" #include "ccMacros.h"

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "CCPointExtension.h" #include "CCPointExtension.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "support/TransformUtils.h" #include "support/TransformUtils.h"
#include "CCDrawingPrimitives.h" #include "CCDrawingPrimitives.h"

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "platform/platform.h" #include "platform/platform.h"
#include "CCImage.h" #include "CCImage.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCConfiguration.h" #include "CCConfiguration.h"
#include "support/ccUtils.h" #include "support/ccUtils.h"
#include "CCTextureCache.h" #include "CCTextureCache.h"

View File

@ -38,7 +38,7 @@
#include "CCParticleSystem.h" #include "CCParticleSystem.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "support/base64.h" #include "support/base64.h"
#include "support/zip_support/ZipUtils.h" #include "support/zip_support/ZipUtils.h"

View File

@ -35,7 +35,7 @@ THE SOFTWARE.
#include "CCTextureAtlas.h" #include "CCTextureAtlas.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "support/TransformUtils.h" #include "support/TransformUtils.h"

View File

@ -424,7 +424,7 @@
> >
</File> </File>
<File <File
RelativePath="..\include\ccGLState.h" RelativePath="..\include\ccGLStateCache.h"
> >
</File> </File>
<File <File
@ -1108,7 +1108,7 @@
> >
</File> </File>
<File <File
RelativePath="..\gles2\ccGLState.cpp" RelativePath="..\gles2\ccGLStateCache.cpp"
> >
</File> </File>
<File <File

View File

@ -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\
";

View File

@ -0,0 +1,17 @@
" \n\
attribute vec4 a_position; \n\
attribute vec4 a_color; \n\
uniform mat4 u_MVPMatrix; \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\
v_fragmentColor = a_color; \n\
} \n\
";

View File

@ -0,0 +1,16 @@
" \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 = vec4( v_fragmentColor.rgb, // RGB from uniform \n\
v_fragmentColor.a * texture2D(u_texture, v_texCoord).a // A from texture & uniform \n\
); \n\
} \n\
";

View File

@ -0,0 +1,21 @@
" \n\
attribute vec4 a_position; \n\
attribute vec2 a_texCoord; \n\
attribute vec4 a_color; \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\
";

View File

@ -0,0 +1,23 @@
" \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\
uniform float u_alpha_value; \n\
\n\
void main() \n\
{ \n\
vec4 texColor = texture2D(u_texture, v_texCoord); \n\
\n\
// mimic: glAlphaFunc(GL_GREATER) \n\
// pass if ( incoming_pixel >= 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\
";

View File

@ -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\
";

View File

@ -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\
";

View File

@ -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\
";

View File

@ -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\
";

View File

@ -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\
";

View File

@ -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\
";

View File

@ -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\
";

View File

@ -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\
";

View File

@ -34,7 +34,7 @@ THE SOFTWARE.
#include "CCTextureCache.h" #include "CCTextureCache.h"
#include "CCDrawingPrimitives.h" #include "CCDrawingPrimitives.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "CCPointExtension.h" #include "CCPointExtension.h"
@ -561,6 +561,10 @@ void CCSprite::draw(void)
{ {
ccGLBindTexture2D( m_pobTexture->getName() ); ccGLBindTexture2D( m_pobTexture->getName() );
} }
else
{
ccGLBindTexture2D(0);
}
// //
// Attributes // Attributes

View File

@ -33,7 +33,7 @@ THE SOFTWARE.
#include "CCPointExtension.h" #include "CCPointExtension.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "support/TransformUtils.h" #include "support/TransformUtils.h"
#include "support/CCProfiling.h" #include "support/CCProfiling.h"

View File

@ -43,7 +43,7 @@ THE SOFTWARE.
#include "CCTexturePVR.h" #include "CCTexturePVR.h"
#include "CCDirector.h" #include "CCDirector.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
#include "CCShaderCache.h" #include "CCShaderCache.h"
#if CC_ENABLE_CACHE_TEXTTURE_DATA #if CC_ENABLE_CACHE_TEXTTURE_DATA

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "CCTextureCache.h" #include "CCTextureCache.h"
#include "ccMacros.h" #include "ccMacros.h"
#include "CCGLProgram.h" #include "CCGLProgram.h"
#include "ccGLState.h" #include "ccGLStateCache.h"
// support // support
#include "CCTexture2D.h" #include "CCTexture2D.h"