diff --git a/cocos/2d/CCDrawNode.cpp b/cocos/2d/CCDrawNode.cpp index 48408b3133..cce3539a07 100644 --- a/cocos/2d/CCDrawNode.cpp +++ b/cocos/2d/CCDrawNode.cpp @@ -178,8 +178,8 @@ bool DrawNode::init() glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -241,7 +241,7 @@ void DrawNode::onDraw(const Matrix &transform, bool transformUpdated) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); // texcood - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); } glDrawArrays(GL_TRIANGLES, 0, _bufferCount); diff --git a/cocos/2d/CCGLProgram.cpp b/cocos/2d/CCGLProgram.cpp index 0daa935b77..18972df2d2 100644 --- a/cocos/2d/CCGLProgram.cpp +++ b/cocos/2d/CCGLProgram.cpp @@ -82,6 +82,7 @@ const char* GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE = "CC_alpha_value"; const char* GLProgram::ATTRIBUTE_NAME_COLOR = "a_color"; const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position"; const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord"; +const char* GLProgram::ATTRIBUTE_NAME_NORMAL = "a_normal"; GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) @@ -243,6 +244,26 @@ bool GLProgram::initWithFilenames(const std::string &vShaderFilename, const std: return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str()); } +void GLProgram::bindPredefinedVertexAttribs() +{ + static const struct { + const char *attributeName; + int location; + } attribute_locations[] = + { + {GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION}, + {GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR}, + {GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD}, + {GLProgram::ATTRIBUTE_NAME_NORMAL, GLProgram::VERTEX_ATTRIB_NORMAL}, + }; + + const int size = sizeof(attribute_locations) / sizeof(attribute_locations[0]); + + for(int i=0; i callback) +void VertexAttribValue::setCallback(const std::function callback) { _value.callback = callback; _useCallback = true; } -void VertexAttribValue::setValue(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer) +void VertexAttribValue::setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer) { _value.pointer.size = size; _value.pointer.type = type; @@ -199,8 +205,6 @@ void VertexAttribValue::setValue(GLint size, GLenum type, GLboolean normalized, GLProgramState* GLProgramState::create(GLProgram *glprogram) { - CCASSERT(glprogram, "invalid shader"); - auto ret = new (std::nothrow) GLProgramState; if(ret && ret->init(glprogram)) { ret->autorelease(); @@ -210,6 +214,12 @@ GLProgramState* GLProgramState::create(GLProgram *glprogram) return nullptr; } +GLProgramState::GLProgramState() +: _vertexAttribsFlags(0) +, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED) +{ +} + GLProgramState::~GLProgramState() { _glprogram->release(); @@ -217,6 +227,8 @@ GLProgramState::~GLProgramState() bool GLProgramState::init(GLProgram* glprogram) { + CCASSERT(glprogram, "invalid shader"); + _glprogram = glprogram; _glprogram->retain(); @@ -232,13 +244,22 @@ bool GLProgramState::init(GLProgram* glprogram) return true; } -void GLProgramState::apply() + +void GLProgramState::resetGLProgram() +{ + CC_SAFE_RELEASE(_glprogram); + _uniforms.clear(); + _attributes.clear(); +} + +void GLProgramState::apply(const Matrix& modelView) { CCASSERT(_glprogram, "invalid glprogram"); // set shader _glprogram->use(); + _glprogram->setUniformsForBuiltins(modelView); // set texture int i = 0; @@ -250,15 +271,36 @@ void GLProgramState::apply() GL::blendFunc(_blendFunc.src, _blendFunc.dst); - // set uniforms - for(auto& uniform : _uniforms) { - uniform.second.apply(); - } + // enable/disable vertex attribs + GL::enableVertexAttribs(_vertexAttribsFlags); // set attributes for(auto &attribute : _attributes) { attribute.second.apply(); } + + // set uniforms + for(auto& uniform : _uniforms) { + uniform.second.apply(); + } +} + +void GLProgramState::setGLProgram(GLProgram *glprogram) +{ + CCASSERT(glprogram, "invalid GLProgram"); + + if( _glprogram != glprogram) { + resetGLProgram(); + init(glprogram); + } +} + +void GLProgramState::setTexture(cocos2d::Texture2D *texture) +{ + if(_textures.size()>0) + _textures.replace(0, texture); + else + _textures.pushBack(texture); } UniformValue* GLProgramState::getUniformValue(const std::string &name) @@ -277,4 +319,19 @@ VertexAttribValue* GLProgramState::getVertexAttribValue(const std::string &name) return nullptr; } +void GLProgramState::setVertexAttribCallback(const std::string &name, const std::function callback) +{ + VertexAttribValue *v = getVertexAttribValue(name); + v->setCallback(callback); + _vertexAttribsFlags |= 1 << v->_vertexAttrib->_index; +} + +void GLProgramState::setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer) +{ + auto v = getVertexAttribValue(name); + v->setPointer(size, type, normalized, stride, pointer); + _vertexAttribsFlags |= 1 << v->_vertexAttrib->_index; +} + + NS_CC_END diff --git a/cocos/2d/CCGLProgramState.h b/cocos/2d/CCGLProgramState.h index 9b150dbd7c..ab54ca3230 100644 --- a/cocos/2d/CCGLProgramState.h +++ b/cocos/2d/CCGLProgramState.h @@ -95,14 +95,15 @@ protected: class VertexAttribValue { friend class GLProgram; + friend class GLProgramState; public: VertexAttribValue(VertexAttrib *vertexAttrib); VertexAttribValue(); ~VertexAttribValue(); - void setValue(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer); - void setValue(const std::function callback); + void setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer); + void setCallback(const std::function callback); void apply(); protected: @@ -137,26 +138,36 @@ protected: class GLProgramState : public Ref { public: - GLProgramState* create(GLProgram* glprogram); + static GLProgramState* create(GLProgram* glprogram); - void apply(); - void setTexture(Texture2D *texture) { _textures.insert(0, texture); } + void apply(const Matrix& modelView); + + void setGLProgram(GLProgram* glprogram); + GLProgram* getGLProgram() const { return _glprogram; } + + void setTexture(Texture2D *texture); Texture2D* getTexture() const { return _textures.at(0); } void setBlendFunc(const BlendFunc& blendFunc) { _blendFunc = blendFunc; } const BlendFunc& getBlendFunc() const { return _blendFunc; } + void setVertexAttribCallback(const std::string &name, const std::function callback); + void setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer); + UniformValue* getUniformValue(const std::string &uniformName); - VertexAttribValue* getVertexAttribValue(const std::string &attributeName); protected: GLProgramState(); ~GLProgramState(); bool init(GLProgram* program); + void resetGLProgram(); + VertexAttribValue* getVertexAttribValue(const std::string &attributeName); std::unordered_map _uniforms; std::unordered_map _attributes; - + + uint32_t _vertexAttribsFlags; + GLProgram *_glprogram; Vector _textures; BlendFunc _blendFunc; diff --git a/cocos/2d/CCGrid.cpp b/cocos/2d/CCGrid.cpp index 968a0dccd2..f6ce871961 100644 --- a/cocos/2d/CCGrid.cpp +++ b/cocos/2d/CCGrid.cpp @@ -330,7 +330,7 @@ void Grid3D::blit(void) // texCoords setGLBufferData(_texCoordinates, numOfPoints * sizeof(Vector2), 1); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0); setGLIndexData(_indices, n * 12, 0); glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0); @@ -339,7 +339,7 @@ void Grid3D::blit(void) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices); // texCoords - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates); glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, _indices); #endif // EMSCRIPTEN @@ -545,7 +545,7 @@ void TiledGrid3D::blit(void) // texCoords setGLBufferData(_texCoordinates, (numQuads*4*sizeof(Vector2)), 1); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0); setGLIndexData(_indices, n * 12, 0); glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0); @@ -554,7 +554,7 @@ void TiledGrid3D::blit(void) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices); // texCoords - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates); glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, _indices); #endif // EMSCRIPTEN diff --git a/cocos/2d/CCMotionStreak.cpp b/cocos/2d/CCMotionStreak.cpp index 24782265fe..0e163dfcba 100644 --- a/cocos/2d/CCMotionStreak.cpp +++ b/cocos/2d/CCMotionStreak.cpp @@ -389,13 +389,13 @@ void MotionStreak::onDraw(const Matrix &transform, bool transformUpdated) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); setGLBufferData(_texCoords, (sizeof(Tex2F) * _maxPoints * 2), 1); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0); setGLBufferData(_colorPointer, (sizeof(GLubyte) * _maxPoints * 2 * 4), 2); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoords); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer); #endif // EMSCRIPTEN diff --git a/cocos/2d/CCParticleSystemQuad.cpp b/cocos/2d/CCParticleSystemQuad.cpp index 283e6692cf..297c273084 100644 --- a/cocos/2d/CCParticleSystemQuad.cpp +++ b/cocos/2d/CCParticleSystemQuad.cpp @@ -478,8 +478,8 @@ void ParticleSystemQuad::setupVBOandVAO() glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors)); // tex coords - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW); diff --git a/cocos/2d/CCProgressTimer.cpp b/cocos/2d/CCProgressTimer.cpp index c71b43ba4b..fea8de6573 100644 --- a/cocos/2d/CCProgressTimer.cpp +++ b/cocos/2d/CCProgressTimer.cpp @@ -521,10 +521,10 @@ void ProgressTimer::onDraw(const Matrix &transform, bool transformUpdated) glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid*)offset); offset += sizeof(Color4B); - glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid*)offset); + glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid*)offset); #else glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]) , &_vertexData[0].vertices); - glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]), &_vertexData[0].texCoords); + glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]), &_vertexData[0].texCoords); glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(_vertexData[0]), &_vertexData[0].colors); #endif // EMSCRIPTEN diff --git a/cocos/2d/CCShaderCache.cpp b/cocos/2d/CCShaderCache.cpp index c92bd71565..ef0ef30277 100644 --- a/cocos/2d/CCShaderCache.cpp +++ b/cocos/2d/CCShaderCache.cpp @@ -283,118 +283,51 @@ void ShaderCache::loadDefaultShader(GLProgram *p, int type) switch (type) { case kShaderType_PositionTextureColor: p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag); - - 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->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag); - - 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->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); - - 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_PositionTextureColorAlphaTestNoMV: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag); - - 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->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag); - - 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->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag); - - 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->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag); - - 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->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); - - 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->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); - - 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->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag); - p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); - break; case kShaderType_PositionLengthTexureColor: p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); - - 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->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag); - - 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->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag); - - 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_LabelNormal: p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag); - - 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_LabelOutline: p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag); - - 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: CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__); diff --git a/cocos/2d/CCTexture2D.cpp b/cocos/2d/CCTexture2D.cpp index 520fafc409..562005d5f0 100644 --- a/cocos/2d/CCTexture2D.cpp +++ b/cocos/2d/CCTexture2D.cpp @@ -1181,10 +1181,10 @@ void Texture2D::drawAtPoint(const Vector2& point) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, coordinates); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates); #endif // EMSCRIPTEN glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); @@ -1214,10 +1214,10 @@ void Texture2D::drawInRect(const Rect& rect) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0); #else glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, coordinates); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates); #endif // EMSCRIPTEN glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } diff --git a/cocos/2d/CCTextureAtlas.cpp b/cocos/2d/CCTextureAtlas.cpp index 68299ce028..51670313ef 100644 --- a/cocos/2d/CCTextureAtlas.cpp +++ b/cocos/2d/CCTextureAtlas.cpp @@ -274,8 +274,8 @@ void TextureAtlas::setupVBOandVAO() glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors)); // tex coords - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _capacity * 6, _indices, GL_STATIC_DRAW); @@ -673,7 +673,7 @@ void TextureAtlas::drawNumberOfQuads(ssize_t numberOfQuads, ssize_t start) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors)); // tex coords - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); diff --git a/cocos/2d/ccGLStateCache.cpp b/cocos/2d/ccGLStateCache.cpp index 466e202189..3974b26dad 100644 --- a/cocos/2d/ccGLStateCache.cpp +++ b/cocos/2d/ccGLStateCache.cpp @@ -33,17 +33,18 @@ THE SOFTWARE. NS_CC_BEGIN +static const int MAX_ATTRIBUTES = 16; +static const int MAX_ACTIVE_TEXTURE = 16; + namespace { static GLuint s_currentProjectionMatrix = -1; - static unsigned int s_attributeFlags = 0; + static uint32_t s_attributeFlags = 0; // 32 attributes max #if CC_ENABLE_GL_STATE_CACHE - -#define kMaxActiveTexture 16 - + static GLuint s_currentShaderProgram = -1; - static GLuint s_currentBoundTexture[kMaxActiveTexture] = {(GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, }; + static GLuint s_currentBoundTexture[MAX_ACTIVE_TEXTURE] = {(GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, }; static GLenum s_blendingSource = -1; static GLenum s_blendingDest = -1; static int s_GLServerState = 0; @@ -65,7 +66,7 @@ void invalidateStateCache( void ) #if CC_ENABLE_GL_STATE_CACHE s_currentShaderProgram = -1; - for( int i=0; i < kMaxActiveTexture; i++ ) + for( int i=0; i < MAX_ACTIVE_TEXTURE; i++ ) { s_currentBoundTexture[i] = -1; } @@ -147,7 +148,7 @@ void bindTexture2D(GLuint textureId) void bindTexture2DN(GLuint textureUnit, GLuint textureId) { #if CC_ENABLE_GL_STATE_CACHE - CCASSERT(textureUnit < kMaxActiveTexture, "textureUnit is too big"); + CCASSERT(textureUnit < MAX_ACTIVE_TEXTURE, "textureUnit is too big"); if (s_currentBoundTexture[textureUnit] != textureId) { s_currentBoundTexture[textureUnit] = textureId; @@ -210,12 +211,12 @@ void bindVAO(GLuint vaoId) // GL Vertex Attrib functions -void enableVertexAttribs( unsigned int flags ) +void enableVertexAttribs(uint32_t flags) { bindVAO(0); // hardcoded! - for(int i=0; i < 16; i++) { + for(int i=0; i < MAX_ATTRIBUTES; i++) { unsigned int bit = 1 << i; bool enabled = flags & bit; bool enabledBefore = s_attributeFlags & bit; diff --git a/cocos/2d/ccGLStateCache.h b/cocos/2d/ccGLStateCache.h index 9709032c67..dcc3cb0ec9 100644 --- a/cocos/2d/ccGLStateCache.h +++ b/cocos/2d/ccGLStateCache.h @@ -102,7 +102,7 @@ void CC_DLL setProjectionMatrixDirty(void); @since v2.0.0 */ -void CC_DLL enableVertexAttribs(unsigned int flags); +void CC_DLL enableVertexAttribs(uint32_t flags); /** If the texture is not already bound to texture unit 0, it binds it. If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly. diff --git a/cocos/deprecated/CCDeprecated.h b/cocos/deprecated/CCDeprecated.h index 525a72942b..38cc1c4999 100644 --- a/cocos/deprecated/CCDeprecated.h +++ b/cocos/deprecated/CCDeprecated.h @@ -806,7 +806,7 @@ CC_DEPRECATED_ATTRIBUTE typedef void* CCZone; CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_Position = GLProgram::VERTEX_ATTRIB_POSITION; CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_Color = GLProgram::VERTEX_ATTRIB_COLOR; -CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_TexCoords = GLProgram::VERTEX_ATTRIB_TEX_COORDS; +CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_TexCoords = GLProgram::VERTEX_ATTRIB_TEX_COORD; CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_MAX = GLProgram::VERTEX_ATTRIB_MAX; CC_DEPRECATED_ATTRIBUTE const int kCCUniformPMatrix = GLProgram::UNIFORM_P_MATRIX; diff --git a/cocos/renderer/CCRenderer.cpp b/cocos/renderer/CCRenderer.cpp index f89f5cc79d..fe0b5e2286 100644 --- a/cocos/renderer/CCRenderer.cpp +++ b/cocos/renderer/CCRenderer.cpp @@ -202,8 +202,8 @@ void Renderer::setupVBOAndVAO() glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, colors)); // tex coords - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * VBO_SIZE * 6, _indices, GL_STATIC_DRAW); @@ -444,7 +444,7 @@ void Renderer::drawBatchedQuads() glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors)); // tex coords - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); } diff --git a/cocos/scripting/lua-bindings/script/Cocos2dConstants.lua b/cocos/scripting/lua-bindings/script/Cocos2dConstants.lua index 67b3978b4c..78027543f5 100644 --- a/cocos/scripting/lua-bindings/script/Cocos2dConstants.lua +++ b/cocos/scripting/lua-bindings/script/Cocos2dConstants.lua @@ -131,7 +131,7 @@ cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 0x4 cc.VERTEX_ATTRIB_COLOR = 0x1 cc.VERTEX_ATTRIB_MAX = 0x3 cc.VERTEX_ATTRIB_POSITION = 0x0 -cc.VERTEX_ATTRIB_TEX_COORDS = 0x2 +cc.VERTEX_ATTRIB_TEX_COORD = 0x2 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 0x2 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 0x1 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0x0 diff --git a/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp b/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp index 11248cfbed..970aff65ce 100644 --- a/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp +++ b/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp @@ -1027,7 +1027,7 @@ void MySprite::onDraw(const Matrix &transform, bool transformUpdated) // texCoods diff = offsetof( V3F_C4B_T2F, texCoords); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); // color diff = offsetof( V3F_C4B_T2F, colors); diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp index 5a816b4152..9dbb8d7e93 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp @@ -116,6 +116,7 @@ ShaderNode::ShaderNode() ShaderNode::~ShaderNode() { + CC_SAFE_RELEASE(_glProgramState); } ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag) @@ -142,6 +143,7 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag) _time = 0; _resolution = Vector2(SIZE_X, SIZE_Y); + _glProgramState->getUniformValue("resolution")->setValue(_resolution); scheduleUpdate(); @@ -164,6 +166,9 @@ void ShaderNode::loadShaderVertex(const char *vert, const char *frag) shader->updateUniforms(); this->setShaderProgram(shader); + + _glProgramState = GLProgramState::create(shader); + _glProgramState->retain(); } void ShaderNode::update(float dt) @@ -176,6 +181,7 @@ void ShaderNode::setPosition(const Vector2 &newPosition) Node::setPosition(newPosition); auto position = getPosition(); _center = Vector2(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR()); + _glProgramState->getUniformValue("center")->setValue(_center); } void ShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) @@ -187,25 +193,16 @@ void ShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transfor void ShaderNode::onDraw(const Matrix &transform, bool transformUpdated) { - auto shader = getShaderProgram(); - shader->use(); - shader->setUniformsForBuiltins(transform); - - // TODO: riq FIXME -// 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; GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h}; - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); - + _glProgramState->setVertexAttribPointer("a_position", 2, GL_FLOAT, GL_FALSE, 0, vertices); + _glProgramState->apply(transform); + glDrawArrays(GL_TRIANGLES, 0, 6); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6); - } /// ShaderMonjori @@ -435,10 +432,12 @@ protected: float _weightSum; CustomCommand _customCommand; + GLProgramState *_glProgramState; }; SpriteBlur::~SpriteBlur() { + CC_SAFE_RELEASE(_glProgramState); } SpriteBlur* SpriteBlur::create(const char *pszFileName) @@ -473,9 +472,12 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect) auto s = getTexture()->getContentSizeInPixels(); _pixelSize = Vector2(1/s.width, 1/s.height); + _samplingRadius = 0; this->initProgram(); + _glProgramState->getUniformValue("onePixelSize")->setValue(_pixelSize); + return true; } @@ -490,20 +492,30 @@ void SpriteBlur::initProgram() setShaderProgram(program); CHECK_GL_ERROR_DEBUG(); - - 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(); program->updateUniforms(); - CHECK_GL_ERROR_DEBUG(); + CHECK_GL_ERROR_DEBUG(); + + _glProgramState = GLProgramState::create(program); + _glProgramState->retain(); + +#define kQuadSize sizeof(_quad.bl) + size_t offset = (size_t)&_quad; + + // position + int diff = offsetof( V3F_C4B_T2F, vertices); + _glProgramState->setVertexAttribPointer("a_position", 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff)); + // texcoord + diff = offsetof( V3F_C4B_T2F, texCoords); + _glProgramState->setVertexAttribPointer("a_texCoord", 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); + // color + diff = offsetof( V3F_C4B_T2F, colors); + _glProgramState->setVertexAttribPointer("a_color", 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff)); } void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) @@ -515,38 +527,11 @@ void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transfor void SpriteBlur::onDraw(const Matrix &transform, bool transformUpdated) { - GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX ); - BlendFunc blend = getBlendFunc(); - GL::blendFunc(blend.src, blend.dst); + _glProgramState->setTexture(getTexture()); + _glProgramState->setBlendFunc(getBlendFunc()); + + _glProgramState->apply(transform); - auto program = getShaderProgram(); - program->use(); - program->setUniformsForBuiltins(transform); - - // TODO: riq FIXME -// program->getUniform("onePixelSize")->setValue(_pixelSize); -// program->getUniform("gaussianCoefficient")->setValue(Vector4(_samplingRadius, _scale, _cons, _weightSum)); - - GL::bindTexture2D( getTexture()->getName()); - - // - // Attributes - // - #define kQuadSize sizeof(_quad.bl) - size_t offset = (size_t)&_quad; - - // vertex - int diff = offsetof( V3F_C4B_T2F, vertices); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff)); - - // texCoods - diff = offsetof( V3F_C4B_T2F, texCoords); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); - - // color - 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); @@ -585,6 +570,8 @@ void SpriteBlur::setBlurSize(float f) } } log("_blurRadius:%d",_blurRadius); + + _glProgramState->getUniformValue("gaussianCoefficient")->setValue(Vector4(_samplingRadius, _scale, _cons, _weightSum)); } // ShaderBlur @@ -670,7 +657,7 @@ bool ShaderRetroEffect::init() 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); + p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD); p->link(); p->updateUniforms(); diff --git a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h index c1dea61c66..0a5cc13b80 100644 --- a/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h +++ b/tests/cpp-tests/Classes/ShaderTest/ShaderTest.h @@ -5,6 +5,8 @@ #include "extensions/cocos-ext.h" #include "../BaseTest.h" +#include "2d/CCGLProgramState.h" + USING_NS_CC_EXT; class ShaderTestDemo : public BaseTest @@ -134,6 +136,7 @@ protected: std::string _vertFileName; std::string _fragFileName; CustomCommand _customCommand; + GLProgramState *_glProgramState; }; class ShaderTestScene : public TestScene diff --git a/tests/lua-tests/src/OpenGLTest/OpenGLTest.lua b/tests/lua-tests/src/OpenGLTest/OpenGLTest.lua index b76c19bb4d..ec37d20617 100644 --- a/tests/lua-tests/src/OpenGLTest/OpenGLTest.lua +++ b/tests/lua-tests/src/OpenGLTest/OpenGLTest.lua @@ -158,7 +158,7 @@ local function OpenGLTestMainLayer() InitTitle(RetroEffectlayer) local program = cc.GLProgram:create("Shaders/example_ColorBars.vsh", "Shaders/example_ColorBars.fsh") program:bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION) - program:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS) + program:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORD) program:link() program:updateUniforms() @@ -653,7 +653,7 @@ local function OpenGLTestMainLayer() gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureBuffer.buffer_id) - gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS,2,gl.FLOAT,false,0,0) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORD,2,gl.FLOAT,false,0,0) gl.drawArrays(gl.TRIANGLE_STRIP,0,4)