From 7ac663e02ccfa1f066909a14befbb86d1253da9a Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Tue, 4 Mar 2014 13:51:43 -0800 Subject: [PATCH 1/2] GL Program API fixes --- cocos/2d/CCDrawingPrimitives.cpp | 8 +- cocos/2d/CCGLProgram.cpp | 23 ++-- cocos/2d/CCGLProgram.h | 23 +++- cocos/2d/CCShaderCache.cpp | 100 +++++++++--------- .../CCControlExtension/CCControlSwitch.cpp | 22 ++-- tests/Classes/ShaderTest/ShaderTest.cpp | 56 +++++----- tests/Classes/ShaderTest/ShaderTest2.cpp | 38 ++++--- 7 files changed, 151 insertions(+), 119 deletions(-) diff --git a/cocos/2d/CCDrawingPrimitives.cpp b/cocos/2d/CCDrawingPrimitives.cpp index 9cd078877f..91f3bdcf52 100644 --- a/cocos/2d/CCDrawingPrimitives.cpp +++ b/cocos/2d/CCDrawingPrimitives.cpp @@ -102,10 +102,10 @@ static void lazy_init( void ) s_shader = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR); s_shader->retain(); - s_colorLocation = glGetUniformLocation( s_shader->getProgram(), "u_color"); - CHECK_GL_ERROR_DEBUG(); - s_pointSizeLocation = glGetUniformLocation( s_shader->getProgram(), "u_pointSize"); - CHECK_GL_ERROR_DEBUG(); + s_colorLocation = s_shader->getUniformLocation("u_color"); + CHECK_GL_ERROR_DEBUG(); + s_pointSizeLocation = s_shader->getUniformLocation("u_pointSize"); + CHECK_GL_ERROR_DEBUG(); s_initialized = true; } diff --git a/cocos/2d/CCGLProgram.cpp b/cocos/2d/CCGLProgram.cpp index fcfe4970d2..4597df6c95 100644 --- a/cocos/2d/CCGLProgram.cpp +++ b/cocos/2d/CCGLProgram.cpp @@ -113,7 +113,7 @@ GLProgram::~GLProgram() } } -bool GLProgram::initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) +bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) { _program = glCreateProgram(); CHECK_GL_ERROR_DEBUG(); @@ -154,12 +154,13 @@ bool GLProgram::initWithVertexShaderByteArray(const GLchar* vShaderByteArray, co return true; } -bool GLProgram::initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename) +bool GLProgram::initWithFilenames(const std::string &vShaderFilename, const std::string &fShaderFilename) { - std::string vertexSource = FileUtils::getInstance()->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(vShaderFilename).c_str()); - std::string fragmentSource = FileUtils::getInstance()->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(fShaderFilename).c_str()); + auto fileUtils = FileUtils::getInstance(); + std::string vertexSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(vShaderFilename)); + std::string fragmentSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(fShaderFilename)); - return initWithVertexShaderByteArray(vertexSource.c_str(), fragmentSource.c_str()); + return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str()); } std::string GLProgram::getDescription() const @@ -224,7 +225,17 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source return (status == GL_TRUE); } -void GLProgram::addAttribute(const char* attributeName, GLuint index) +GLint GLProgram::getAttribLocation(const char* attributeName) const +{ + return glGetAttribLocation(_program, attributeName); +} + +GLint GLProgram::getUniformLocation(const char* attributeName) const +{ + return glGetUniformLocation(_program, attributeName); +} + +void GLProgram::bindAttribLocation(const char* attributeName, GLuint index) const { glBindAttribLocation(_program, index, attributeName); } diff --git a/cocos/2d/CCGLProgram.h b/cocos/2d/CCGLProgram.h index b36e460fa3..9705eecb9c 100644 --- a/cocos/2d/CCGLProgram.h +++ b/cocos/2d/CCGLProgram.h @@ -123,14 +123,22 @@ public: * @js initWithString * @lua initWithString */ - bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray); + bool initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray); /** Initializes the GLProgram with a vertex and fragment with contents of filenames * @js init * @lua init */ - bool initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename); - /** It will add a new attribute to the shader */ - void addAttribute(const char* attributeName, GLuint index); + bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename); + + /** It will add a new attribute to the shader by calling glBindAttribLocation */ + void bindAttribLocation(const char* attributeName, GLuint index) const; + + /** calls glGetAttribLocation */ + GLint getAttribLocation(const char* attributeName) const; + + /** calls glGetUniformLocation() */ + GLint getUniformLocation(const char* attributeName) const; + /** links the glProgram */ bool link(); /** it will call glUseProgram() */ @@ -235,6 +243,13 @@ public: inline const GLuint getProgram() const { return _program; } + // DEPRECATED + CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) + { return initWithByteArrays(vShaderByteArray, fShaderByteArray); } + CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderFilename(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) + { return initWithFilenames(vShaderByteArray, fShaderByteArray); } + CC_DEPRECATED_ATTRIBUTE void addAttribute(const char* attributeName, GLuint index) const { return bindAttribLocation(attributeName, index); } + private: bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes); virtual std::string getDescription() const; diff --git a/cocos/2d/CCShaderCache.cpp b/cocos/2d/CCShaderCache.cpp index aeca686f3a..8ab253ce2c 100644 --- a/cocos/2d/CCShaderCache.cpp +++ b/cocos/2d/CCShaderCache.cpp @@ -273,109 +273,109 @@ void ShaderCache::loadDefaultShader(GLProgram *p, int type) { switch (type) { case kShaderType_PositionTextureColor: - p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColor_frag); + p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_PositionTextureColor_noMVP: - p->initWithVertexShaderByteArray(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag); + p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_PositionTextureColorAlphaTest: - p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); + p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_PositionColor: - p->initWithVertexShaderByteArray(ccPositionColor_vert ,ccPositionColor_frag); + p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); break; case kShaderType_PositionColor_noMVP: - p->initWithVertexShaderByteArray(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag); + p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); break; case kShaderType_PositionTexture: - p->initWithVertexShaderByteArray(ccPositionTexture_vert ,ccPositionTexture_frag); + p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_PositionTexture_uColor: - p->initWithVertexShaderByteArray(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); + p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_PositionTextureA8Color: - p->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); + p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_Position_uColor: - p->initWithVertexShaderByteArray(ccPosition_uColor_vert, ccPosition_uColor_frag); + p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag); - p->addAttribute("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); break; case kShaderType_PositionLengthTexureColor: - p->initWithVertexShaderByteArray(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); + p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); break; case kShaderType_LabelDistanceFieldNormal: - p->initWithVertexShaderByteArray(ccLabelDistanceFieldNormal_vert, ccLabelDistanceFieldNormal_frag); + p->initWithByteArrays(ccLabelDistanceFieldNormal_vert, ccLabelDistanceFieldNormal_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_LabelDistanceFieldGlow: - p->initWithVertexShaderByteArray(ccLabelDistanceFieldGlow_vert, ccLabelDistanceFieldGlow_frag); + p->initWithByteArrays(ccLabelDistanceFieldGlow_vert, ccLabelDistanceFieldGlow_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_LabelDistanceFieldOutline: - p->initWithVertexShaderByteArray(ccLabelDistanceFieldOutline_vert, ccLabelDistanceFieldOutline_frag); + p->initWithByteArrays(ccLabelDistanceFieldOutline_vert, ccLabelDistanceFieldOutline_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; case kShaderType_LabelDistanceFieldShadow: - p->initWithVertexShaderByteArray(ccLabelDistanceFieldShadow_vert, ccLabelDistanceFieldShadow_frag); + p->initWithByteArrays(ccLabelDistanceFieldShadow_vert, ccLabelDistanceFieldShadow_frag); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); break; default: diff --git a/extensions/GUI/CCControlExtension/CCControlSwitch.cpp b/extensions/GUI/CCControlExtension/CCControlSwitch.cpp index e37c76735f..d7a65a851b 100644 --- a/extensions/GUI/CCControlExtension/CCControlSwitch.cpp +++ b/extensions/GUI/CCControlExtension/CCControlSwitch.cpp @@ -204,26 +204,26 @@ bool ControlSwitchSprite::initWithMaskSprite( // Set up the mask with the Mask shader setMaskTexture(maskSprite->getTexture()); - GLProgram* pProgram = new GLProgram(); - pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccExSwitchMask_frag); - setShaderProgram(pProgram); - pProgram->release(); + GLProgram* program = new GLProgram(); + program->initWithByteArrays(ccPositionTextureColor_vert, ccExSwitchMask_frag); + setShaderProgram(program); + program->release(); CHECK_GL_ERROR_DEBUG(); - getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); CHECK_GL_ERROR_DEBUG(); - getShaderProgram()->link(); + program->link(); CHECK_GL_ERROR_DEBUG(); - getShaderProgram()->updateUniforms(); + program->updateUniforms(); CHECK_GL_ERROR_DEBUG(); - _textureLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "u_texture"); - _maskLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "u_mask"); + _textureLocation = program->getUniformLocation("u_texture"); + _maskLocation = program->getUniformLocation("u_mask"); CHECK_GL_ERROR_DEBUG(); setContentSize(_maskTexture->getContentSize()); diff --git a/tests/Classes/ShaderTest/ShaderTest.cpp b/tests/Classes/ShaderTest/ShaderTest.cpp index 21e4a9e43a..1d26582c3f 100644 --- a/tests/Classes/ShaderTest/ShaderTest.cpp +++ b/tests/Classes/ShaderTest/ShaderTest.cpp @@ -163,16 +163,16 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag) void ShaderNode::loadShaderVertex(const char *vert, const char *frag) { auto shader = new GLProgram(); - shader->initWithVertexShaderFilename(vert, frag); + shader->initWithFilenames(vert, frag); - shader->addAttribute("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); + shader->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); shader->link(); shader->updateUniforms(); - _uniformCenter = glGetUniformLocation(shader->getProgram(), "center"); - _uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution"); - _uniformTime = glGetUniformLocation(shader->getProgram(), "time"); + _uniformCenter = shader->getUniformLocation("center"); + _uniformResolution = shader->getUniformLocation("resolution"); + _uniformTime = shader->getUniformLocation("time"); this->setShaderProgram(shader); @@ -505,29 +505,29 @@ void SpriteBlur::initProgram() { GLchar * fragSource = (GLchar*) String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString(); - auto pProgram = new GLProgram(); - pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource); - setShaderProgram(pProgram); - pProgram->release(); + auto program = new GLProgram(); + program->initWithByteArrays(ccPositionTextureColor_vert, fragSource); + setShaderProgram(program); + program->release(); CHECK_GL_ERROR_DEBUG(); - getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + + CHECK_GL_ERROR_DEBUG(); + + program->link(); CHECK_GL_ERROR_DEBUG(); - getShaderProgram()->link(); + program->updateUniforms(); CHECK_GL_ERROR_DEBUG(); - getShaderProgram()->updateUniforms(); - - CHECK_GL_ERROR_DEBUG(); - - pixelSizeLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "onePixelSize"); - coefficientLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "gaussianCoefficient"); + pixelSizeLocation = program->getUniformLocation("onePixelSize"); + coefficientLocation = program->getUniformLocation("gaussianCoefficient"); CHECK_GL_ERROR_DEBUG(); } @@ -544,11 +544,12 @@ void SpriteBlur::onDraw() GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX ); BlendFunc blend = getBlendFunc(); GL::blendFunc(blend.src, blend.dst); - - getShaderProgram()->use(); - getShaderProgram()->setUniformsForBuiltins(); - getShaderProgram()->setUniformLocationWith2f(pixelSizeLocation, _pixelSize.x, _pixelSize.y); - getShaderProgram()->setUniformLocationWith4f(coefficientLocation, _samplingRadius, _scale,_cons,_weightSum); + + auto program = getShaderProgram(); + program->use(); + program->setUniformsForBuiltins(); + program->setUniformLocationWith2f(pixelSizeLocation, _pixelSize.x, _pixelSize.y); + program->setUniformLocationWith4f(coefficientLocation, _samplingRadius, _scale,_cons,_weightSum); GL::bindTexture2D( getTexture()->getName()); @@ -570,7 +571,6 @@ void SpriteBlur::onDraw() diff = offsetof( V3F_C4B_T2F, colors); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff)); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4); @@ -692,10 +692,10 @@ bool ShaderRetroEffect::init() GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh").c_str())->getCString(); auto p = new GLProgram(); - p->initWithVertexShaderByteArray(ccPositionTexture_vert, fragSource); + p->initWithByteArrays(ccPositionTexture_vert, fragSource); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); p->link(); p->updateUniforms(); diff --git a/tests/Classes/ShaderTest/ShaderTest2.cpp b/tests/Classes/ShaderTest/ShaderTest2.cpp index 1758def00a..d019d4b1be 100644 --- a/tests/Classes/ShaderTest/ShaderTest2.cpp +++ b/tests/Classes/ShaderTest/ShaderTest2.cpp @@ -151,15 +151,15 @@ void ShaderSprite::initShader() GLchar * fragSource = (GLchar*) String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString(); auto program = new GLProgram(); - program->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource); + program->initWithByteArrays(ccPositionTextureColor_vert, fragSource); setShaderProgram(program); program->release(); CHECK_GL_ERROR_DEBUG(); - program->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - program->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); - program->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); + program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); CHECK_GL_ERROR_DEBUG(); @@ -294,16 +294,17 @@ void BlurSprite::buildCustomUniforms() blur_ = Point(1/s.width, 1/s.height); sub_[0] = sub_[1] = sub_[2] = sub_[3] = 0; - - subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract"); - blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize"); + + auto program = getShaderProgram(); + subLocation = program->getUniformLocation("substract"); + blurLocation = program->getUniformLocation("blurSize"); } void BlurSprite::setCustomUniforms() { - - getShaderProgram()->setUniformLocationWith2f(blurLocation, blur_.x, blur_.y); - getShaderProgram()->setUniformLocationWith4fv(subLocation, sub_, 1); + auto program = getShaderProgram(); + program->setUniformLocationWith2f(blurLocation, blur_.x, blur_.y); + program->setUniformLocationWith4fv(subLocation, sub_, 1); } void BlurSprite::setBlurSize(float f) @@ -335,7 +336,8 @@ NoiseSprite::NoiseSprite() void NoiseSprite::buildCustomUniforms() { - _resolutionLoc = glGetUniformLocation( getShaderProgram()->getProgram(), "resolution"); + auto program = getShaderProgram(); + _resolutionLoc = program->getUniformLocation("resolution"); } void NoiseSprite::setCustomUniforms() @@ -367,7 +369,8 @@ EdgeDetectionSprite::EdgeDetectionSprite() void EdgeDetectionSprite::buildCustomUniforms() { - _resolutionLoc = glGetUniformLocation( getShaderProgram()->getProgram(), "resolution"); + auto program = getShaderProgram(); + _resolutionLoc = program->getUniformLocation("resolution"); } void EdgeDetectionSprite::setCustomUniforms() @@ -399,7 +402,8 @@ BloomSprite::BloomSprite() void BloomSprite::buildCustomUniforms() { - _resolutionLoc = glGetUniformLocation( getShaderProgram()->getProgram(), "resolution"); + auto program = getShaderProgram(); + _resolutionLoc = program->getUniformLocation("resolution"); } void BloomSprite::setCustomUniforms() @@ -431,7 +435,8 @@ CelShadingSprite::CelShadingSprite() void CelShadingSprite::buildCustomUniforms() { - _resolutionLoc = glGetUniformLocation( getShaderProgram()->getProgram(), "resolution"); + auto program = getShaderProgram(); + _resolutionLoc = program->getUniformLocation("resolution"); } void CelShadingSprite::setCustomUniforms() @@ -465,8 +470,9 @@ LensFlareSprite::LensFlareSprite() void LensFlareSprite::buildCustomUniforms() { - _resolutionLoc = glGetUniformLocation( getShaderProgram()->getProgram(), "resolution"); - _textureResolutionLoc = glGetUniformLocation( getShaderProgram()->getProgram(), "textureResolution"); + auto program = getShaderProgram(); + _resolutionLoc = program->getUniformLocation("resolution"); + _textureResolutionLoc = program->getUniformLocation("textureResolution"); } void LensFlareSprite::setCustomUniforms() From 0bbc1cf5c35e435016074fabc99866bba7fb848e Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Tue, 4 Mar 2014 14:00:32 -0800 Subject: [PATCH 2/2] fixes more warnings --- tests/Classes/ShaderTest/ShaderTest.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Classes/ShaderTest/ShaderTest.cpp b/tests/Classes/ShaderTest/ShaderTest.cpp index 1d26582c3f..216a19207e 100644 --- a/tests/Classes/ShaderTest/ShaderTest.cpp +++ b/tests/Classes/ShaderTest/ShaderTest.cpp @@ -781,10 +781,10 @@ gl_FragColor = colors[z] * texture2D(CC_Texture0, v_texCoord); \n\ ShaderFail::ShaderFail() { auto p = new GLProgram(); - p->initWithVertexShaderByteArray(ccPositionTexture_vert, shader_frag_fail); + p->initWithByteArrays(ccPositionTexture_vert, shader_frag_fail); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); - p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); p->link(); p->updateUniforms();