diff --git a/cocos/2d/CCGLProgram.cpp b/cocos/2d/CCGLProgram.cpp index be0dd15435..3c0f696024 100644 --- a/cocos/2d/CCGLProgram.cpp +++ b/cocos/2d/CCGLProgram.cpp @@ -84,6 +84,30 @@ const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position"; const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord"; +GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) +{ + auto ret = new (std::nothrow) GLProgram(); + if(ret && ret->initWithByteArrays(vShaderByteArray, fShaderByteArray)) { + ret->autorelease(); + return ret; + } + + CC_SAFE_DELETE(ret); + return nullptr; +} + +GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename) +{ + auto ret = new (std::nothrow) GLProgram(); + if(ret && ret->initWithFilenames(vShaderFilename, fShaderFilename)) { + ret->autorelease(); + return ret; + } + + CC_SAFE_DELETE(ret); + return nullptr; +} + GLProgram::GLProgram() : _program(0) , _vertShader(0) @@ -178,6 +202,18 @@ bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* } #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) +GLProgram* GLProgram::createWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) +{ + auto ret = new (std::nothrow) GLProgram(); + if(ret && ret->initWithPrecompiledProgramByteArray(vShaderByteArray, fShaderByteArray)) { + ret->autorelease(); + return ret; + } + + CC_SAFE_DELETE(ret); + return nullptr; +} + bool GLProgram::initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) { bool haveProgram = false; diff --git a/cocos/2d/CCGLProgram.h b/cocos/2d/CCGLProgram.h index 5d2325134a..36f9c67cd6 100644 --- a/cocos/2d/CCGLProgram.h +++ b/cocos/2d/CCGLProgram.h @@ -172,14 +172,8 @@ public: static const char* ATTRIBUTE_NAME_COLOR; static const char* ATTRIBUTE_NAME_POSITION; static const char* ATTRIBUTE_NAME_TEX_COORD; - /** - * @js ctor - */ + GLProgram(); - /** - * @js NA - * @lua NA - */ virtual ~GLProgram(); /** Initializes the GLProgram with a vertex and fragment with bytes array * @js initWithString @@ -188,6 +182,7 @@ public: #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) /** Initializes the CCGLProgram with precompiled shader program */ + static GLProgram* createWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray); bool initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray); #endif @@ -195,12 +190,14 @@ public: * @js initWithString * @lua initWithString */ + static GLProgram* createWithByteArrays(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 */ + static GLProgram* createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename); bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename); //void bindUniform(std::string uniformName, int value); diff --git a/cocos/deprecated/CCString.cpp b/cocos/deprecated/CCString.cpp index 27218ca1d1..475d4f41f8 100644 --- a/cocos/deprecated/CCString.cpp +++ b/cocos/deprecated/CCString.cpp @@ -256,7 +256,7 @@ __String* __String::createWithFormat(const char* format, ...) return ret; } -__String* __String::createWithContentsOfFile(const char* filename) +__String* __String::createWithContentsOfFile(const std::string &filename) { std::string str = FileUtils::getInstance()->getStringFromFile(filename); return __String::create(std::move(str)); diff --git a/cocos/deprecated/CCString.h b/cocos/deprecated/CCString.h index d4917fb9ef..f4cd92b866 100644 --- a/cocos/deprecated/CCString.h +++ b/cocos/deprecated/CCString.h @@ -174,7 +174,7 @@ public: * it means that you needn't do a release operation unless you retain it. * @js NA */ - static __String* createWithContentsOfFile(const char* filename); + static __String* createWithContentsOfFile(const std::string& filename); /** * @js NA * @lua NA diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp index 7fa1bd644c..077897401f 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp @@ -1,8 +1,6 @@ #include "ShaderTest.h" #include "../testResource.h" #include "cocos2d.h" -#include "renderer/CCCustomCommand.h" -#include "renderer/CCRenderer.h" static int sceneIdx = -1; @@ -113,9 +111,6 @@ ShaderNode::ShaderNode() :_center(Vector2(0.0f, 0.0f)) ,_resolution(Vector2(0.0f, 0.0f)) ,_time(0.0f) -,_uniformCenter(0) -,_uniformResolution(0) -,_uniformTime(0) { } @@ -161,21 +156,14 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag) void ShaderNode::loadShaderVertex(const char *vert, const char *frag) { - auto shader = new GLProgram(); - shader->initWithFilenames(vert, frag); + auto shader = GLProgram::createWithFilenames(vert, frag); shader->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); shader->link(); shader->updateUniforms(); - _uniformCenter = shader->getUniformLocation("center"); - _uniformResolution = shader->getUniformLocation("resolution"); - _uniformTime = shader->getUniformLocation("time"); - this->setShaderProgram(shader); - - shader->release(); } void ShaderNode::update(float dt) @@ -202,12 +190,10 @@ void ShaderNode::onDraw(const Matrix &transform, bool transformUpdated) auto shader = getShaderProgram(); shader->use(); shader->setUniformsForBuiltins(transform); - shader->setUniformLocationWith2f(_uniformCenter, _center.x, _center.y); - shader->setUniformLocationWith2f(_uniformResolution, _resolution.x, _resolution.y); - - // time changes all the time, so it is Ok to call OpenGL directly, and not the "cached" version - glUniform1f(_uniformTime, _time); - + + shader->getUniform("center")->setValue(_center); + shader->getUniform("resolution")->setValue(_resolution); + GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION ); float w = SIZE_X, h = SIZE_Y; @@ -447,9 +433,6 @@ protected: float _cons; float _weightSum; - GLuint pixelSizeLocation; - GLuint coefficientLocation; - CustomCommand _customCommand; }; @@ -502,11 +485,9 @@ void SpriteBlur::initProgram() { GLchar * fragSource = (GLchar*) String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString(); - auto program = new GLProgram(); - program->initWithByteArrays(ccPositionTextureColor_vert, fragSource); + auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_vert, fragSource); setShaderProgram(program); - program->release(); - + CHECK_GL_ERROR_DEBUG(); program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); @@ -521,12 +502,7 @@ void SpriteBlur::initProgram() program->updateUniforms(); - CHECK_GL_ERROR_DEBUG(); - - pixelSizeLocation = program->getUniformLocation("onePixelSize"); - coefficientLocation = program->getUniformLocation("gaussianCoefficient"); - - CHECK_GL_ERROR_DEBUG(); + CHECK_GL_ERROR_DEBUG(); } void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) @@ -545,8 +521,9 @@ void SpriteBlur::onDraw(const Matrix &transform, bool transformUpdated) auto program = getShaderProgram(); program->use(); program->setUniformsForBuiltins(transform); - program->setUniformLocationWith2f(pixelSizeLocation, _pixelSize.x, _pixelSize.y); - program->setUniformLocationWith4f(coefficientLocation, _samplingRadius, _scale,_cons,_weightSum); + + program->getUniform("onePixelSize")->setValue(_pixelSize); + program->getUniform("gaussianCoefficient")->setValue(Vector4(_samplingRadius, _scale, _cons, _weightSum)); GL::bindTexture2D( getTexture()->getName()); @@ -687,9 +664,8 @@ bool ShaderRetroEffect::init() { if( ShaderTestDemo::init() ) { - GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh").c_str())->getCString(); - auto p = new GLProgram(); - p->initWithByteArrays(ccPositionTexture_vert, fragSource); + GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"))->getCString(); + auto p = GLProgram::createWithByteArrays(ccPositionTexture_vert, fragSource); p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); @@ -704,8 +680,6 @@ bool ShaderRetroEffect::init() _label->setAnchorPoint(Vector2::ANCHOR_MIDDLE); _label->setShaderProgram(p); - p->release(); - _label->setPosition(Vector2(s.width/2,s.height/2)); addChild(_label); diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h index 3ef09eae2a..c1dea61c66 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h @@ -131,7 +131,6 @@ protected: Vector2 _center; Vector2 _resolution; float _time; - GLuint _uniformCenter, _uniformResolution, _uniformTime; std::string _vertFileName; std::string _fragFileName; CustomCommand _customCommand; diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp b/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp index f615116ab8..ce1425acd1 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp @@ -166,11 +166,9 @@ void ShaderSprite::initShader() vertSource = fileUtiles->getStringFromFile(vertexFilePath); } - auto program = new GLProgram(); - program->initWithByteArrays(vertSource.c_str(), fragSource.c_str()); + auto program = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str()); setShaderProgram(program); - program->release(); - + CHECK_GL_ERROR_DEBUG(); program->link(); @@ -742,8 +740,7 @@ UniformSprite::~UniformSprite() void UniformSprite::initShader() { - auto shader = new GLProgram(); - shader->initWithFilenames(_vertSourceFile, _fragSourceFile); + auto shader = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile); shader->link(); shader->updateUniforms(); @@ -752,8 +749,6 @@ void UniformSprite::initShader() // std::string attribname ="a_position"; // shader->getAttrib(attribname)->size = 2; - - shader->release(); } void UniformSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) @@ -845,14 +840,10 @@ AttribSprite::~AttribSprite() void AttribSprite::initShader() { - auto shader = new GLProgram(); - - shader->initWithFilenames(_vertSourceFile, _fragSourceFile); + auto shader = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile); shader->link(); shader->updateUniforms(); - this->setShaderProgram(shader); - - shader->release(); + this->setShaderProgram(shader); } void AttribSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)