Merge pull request #5548 from ricardoquesada/glprogram_api_fixes

Glprogram api fixes
This commit is contained in:
Ricardo Quesada 2014-03-04 14:38:32 -08:00
commit 273a42a234
7 changed files with 154 additions and 122 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

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

View File

@ -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:

View File

@ -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());

View File

@ -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();
@ -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();

View File

@ -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()