Adds GLProgram::createXXX()

This commit is contained in:
Ricardo Quesada 2014-05-07 14:38:41 -07:00
parent 02405a8efe
commit 98454eeb69
7 changed files with 60 additions and 63 deletions

View File

@ -84,6 +84,30 @@ const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position";
const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord"; 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() GLProgram::GLProgram()
: _program(0) : _program(0)
, _vertShader(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) #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 GLProgram::initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{ {
bool haveProgram = false; bool haveProgram = false;

View File

@ -172,14 +172,8 @@ public:
static const char* ATTRIBUTE_NAME_COLOR; static const char* ATTRIBUTE_NAME_COLOR;
static const char* ATTRIBUTE_NAME_POSITION; static const char* ATTRIBUTE_NAME_POSITION;
static const char* ATTRIBUTE_NAME_TEX_COORD; static const char* ATTRIBUTE_NAME_TEX_COORD;
/**
* @js ctor
*/
GLProgram(); GLProgram();
/**
* @js NA
* @lua NA
*/
virtual ~GLProgram(); virtual ~GLProgram();
/** Initializes the GLProgram with a vertex and fragment with bytes array /** Initializes the GLProgram with a vertex and fragment with bytes array
* @js initWithString * @js initWithString
@ -188,6 +182,7 @@ public:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
/** Initializes the CCGLProgram with precompiled shader program */ /** Initializes the CCGLProgram with precompiled shader program */
static GLProgram* createWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
bool initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray); bool initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
#endif #endif
@ -195,12 +190,14 @@ public:
* @js initWithString * @js initWithString
* @lua initWithString * @lua initWithString
*/ */
static GLProgram* createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
bool initWithByteArrays(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 /** Initializes the GLProgram with a vertex and fragment with contents of filenames
* @js init * @js init
* @lua init * @lua init
*/ */
static GLProgram* createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename);
bool initWithFilenames(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); //void bindUniform(std::string uniformName, int value);

View File

@ -256,7 +256,7 @@ __String* __String::createWithFormat(const char* format, ...)
return ret; return ret;
} }
__String* __String::createWithContentsOfFile(const char* filename) __String* __String::createWithContentsOfFile(const std::string &filename)
{ {
std::string str = FileUtils::getInstance()->getStringFromFile(filename); std::string str = FileUtils::getInstance()->getStringFromFile(filename);
return __String::create(std::move(str)); return __String::create(std::move(str));

View File

@ -174,7 +174,7 @@ public:
* it means that you needn't do a release operation unless you retain it. * it means that you needn't do a release operation unless you retain it.
* @js NA * @js NA
*/ */
static __String* createWithContentsOfFile(const char* filename); static __String* createWithContentsOfFile(const std::string& filename);
/** /**
* @js NA * @js NA
* @lua NA * @lua NA

View File

@ -1,8 +1,6 @@
#include "ShaderTest.h" #include "ShaderTest.h"
#include "../testResource.h" #include "../testResource.h"
#include "cocos2d.h" #include "cocos2d.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCRenderer.h"
static int sceneIdx = -1; static int sceneIdx = -1;
@ -113,9 +111,6 @@ ShaderNode::ShaderNode()
:_center(Vector2(0.0f, 0.0f)) :_center(Vector2(0.0f, 0.0f))
,_resolution(Vector2(0.0f, 0.0f)) ,_resolution(Vector2(0.0f, 0.0f))
,_time(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) void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
{ {
auto shader = new GLProgram(); auto shader = GLProgram::createWithFilenames(vert, frag);
shader->initWithFilenames(vert, frag);
shader->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); shader->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
shader->link(); shader->link();
shader->updateUniforms(); shader->updateUniforms();
_uniformCenter = shader->getUniformLocation("center");
_uniformResolution = shader->getUniformLocation("resolution");
_uniformTime = shader->getUniformLocation("time");
this->setShaderProgram(shader); this->setShaderProgram(shader);
shader->release();
} }
void ShaderNode::update(float dt) void ShaderNode::update(float dt)
@ -202,12 +190,10 @@ void ShaderNode::onDraw(const Matrix &transform, bool transformUpdated)
auto shader = getShaderProgram(); auto shader = getShaderProgram();
shader->use(); shader->use();
shader->setUniformsForBuiltins(transform); shader->setUniformsForBuiltins(transform);
shader->setUniformLocationWith2f(_uniformCenter, _center.x, _center.y);
shader->setUniformLocationWith2f(_uniformResolution, _resolution.x, _resolution.y); shader->getUniform("center")->setValue(_center);
shader->getUniform("resolution")->setValue(_resolution);
// time changes all the time, so it is Ok to call OpenGL directly, and not the "cached" version
glUniform1f(_uniformTime, _time);
GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION ); GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
float w = SIZE_X, h = SIZE_Y; float w = SIZE_X, h = SIZE_Y;
@ -447,9 +433,6 @@ protected:
float _cons; float _cons;
float _weightSum; float _weightSum;
GLuint pixelSizeLocation;
GLuint coefficientLocation;
CustomCommand _customCommand; CustomCommand _customCommand;
}; };
@ -502,11 +485,9 @@ void SpriteBlur::initProgram()
{ {
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile( GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString(); FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
auto program = new GLProgram(); auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_vert, fragSource);
program->initWithByteArrays(ccPositionTextureColor_vert, fragSource);
setShaderProgram(program); setShaderProgram(program);
program->release();
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
@ -521,12 +502,7 @@ void SpriteBlur::initProgram()
program->updateUniforms(); program->updateUniforms();
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
pixelSizeLocation = program->getUniformLocation("onePixelSize");
coefficientLocation = program->getUniformLocation("gaussianCoefficient");
CHECK_GL_ERROR_DEBUG();
} }
void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) 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(); auto program = getShaderProgram();
program->use(); program->use();
program->setUniformsForBuiltins(transform); 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()); GL::bindTexture2D( getTexture()->getName());
@ -687,9 +664,8 @@ bool ShaderRetroEffect::init()
{ {
if( ShaderTestDemo::init() ) { if( ShaderTestDemo::init() ) {
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh").c_str())->getCString(); GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"))->getCString();
auto p = new GLProgram(); auto p = GLProgram::createWithByteArrays(ccPositionTexture_vert, fragSource);
p->initWithByteArrays(ccPositionTexture_vert, fragSource);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); 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_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
@ -704,8 +680,6 @@ bool ShaderRetroEffect::init()
_label->setAnchorPoint(Vector2::ANCHOR_MIDDLE); _label->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
_label->setShaderProgram(p); _label->setShaderProgram(p);
p->release();
_label->setPosition(Vector2(s.width/2,s.height/2)); _label->setPosition(Vector2(s.width/2,s.height/2));
addChild(_label); addChild(_label);

View File

@ -131,7 +131,6 @@ protected:
Vector2 _center; Vector2 _center;
Vector2 _resolution; Vector2 _resolution;
float _time; float _time;
GLuint _uniformCenter, _uniformResolution, _uniformTime;
std::string _vertFileName; std::string _vertFileName;
std::string _fragFileName; std::string _fragFileName;
CustomCommand _customCommand; CustomCommand _customCommand;

View File

@ -166,11 +166,9 @@ void ShaderSprite::initShader()
vertSource = fileUtiles->getStringFromFile(vertexFilePath); vertSource = fileUtiles->getStringFromFile(vertexFilePath);
} }
auto program = new GLProgram(); auto program = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
program->initWithByteArrays(vertSource.c_str(), fragSource.c_str());
setShaderProgram(program); setShaderProgram(program);
program->release();
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
program->link(); program->link();
@ -742,8 +740,7 @@ UniformSprite::~UniformSprite()
void UniformSprite::initShader() void UniformSprite::initShader()
{ {
auto shader = new GLProgram(); auto shader = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile);
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
shader->link(); shader->link();
shader->updateUniforms(); shader->updateUniforms();
@ -752,8 +749,6 @@ void UniformSprite::initShader()
// std::string attribname ="a_position"; // std::string attribname ="a_position";
// shader->getAttrib(attribname)->size = 2; // shader->getAttrib(attribname)->size = 2;
shader->release();
} }
void UniformSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) void UniformSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -845,14 +840,10 @@ AttribSprite::~AttribSprite()
void AttribSprite::initShader() void AttribSprite::initShader()
{ {
auto shader = new GLProgram(); auto shader = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile);
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
shader->link(); shader->link();
shader->updateUniforms(); shader->updateUniforms();
this->setShaderProgram(shader); this->setShaderProgram(shader);
shader->release();
} }
void AttribSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) void AttribSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)