From 9aa8327d9c6f48e337ceddab93a024cb328c0a65 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Tue, 25 Mar 2014 16:54:29 +0800 Subject: [PATCH 1/2] closed #4552: separate material ID and openGL ProgramID --- cocos/2d/CCGLProgram.cpp | 35 +++++++++++++++++++++++++++++ cocos/2d/CCGLProgram.h | 20 ++++++++++++++++- cocos/2d/renderer/CCQuadCommand.cpp | 6 ++--- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/cocos/2d/CCGLProgram.cpp b/cocos/2d/CCGLProgram.cpp index ee6b53cb42..c30609c16a 100644 --- a/cocos/2d/CCGLProgram.cpp +++ b/cocos/2d/CCGLProgram.cpp @@ -80,6 +80,38 @@ 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"; +#define CC_GLPROGRAM_MAX_MATERIAL_ID_NUMBER 1024 + +GLProgram::MaterialProgramIDAllocator::MaterialProgramIDAllocator() +{ + for(GLuint id = 1; id < CC_GLPROGRAM_MAX_MATERIAL_ID_NUMBER; ++id) + { + _freeIDs.insert(id); + } +} + +GLProgram::MaterialProgramIDAllocator::~MaterialProgramIDAllocator() +{ + //do nothing +} + +GLuint GLProgram::MaterialProgramIDAllocator::allocID() +{ + CCASSERT(_freeIDs.size()!=0, "There are no allocated ID"); + GLuint id = *(_freeIDs.begin()); + _freeIDs.erase(id); + return id; +} + +void GLProgram::MaterialProgramIDAllocator::freeID(GLuint id) +{ + CCASSERT(_freeIDs.find(id) == _freeIDs.end() && id != 0, "free id is 0 or a duplicated id"); + _freeIDs.insert(id); +} + +GLProgram::MaterialProgramIDAllocator GLProgram::_idAllocator; +const GLuint GLProgram::_maxMaterialIDNumber = CC_GLPROGRAM_MAX_MATERIAL_ID_NUMBER; + GLProgram::GLProgram() : _program(0) , _vertShader(0) @@ -88,10 +120,13 @@ GLProgram::GLProgram() , _flags() { memset(_uniforms, 0, sizeof(_uniforms)); + _materialProgramID = _idAllocator.allocID(); } GLProgram::~GLProgram() { + _idAllocator.freeID(_materialProgramID); + _materialProgramID = 0; CCLOGINFO("%s %d deallocing GLProgram: %p", __FUNCTION__, __LINE__, this); // there is no need to delete the shaders. They should have been already deleted. diff --git a/cocos/2d/CCGLProgram.h b/cocos/2d/CCGLProgram.h index c111c0c724..9cbaba0228 100644 --- a/cocos/2d/CCGLProgram.h +++ b/cocos/2d/CCGLProgram.h @@ -34,6 +34,7 @@ THE SOFTWARE. #include "CCRef.h" #include "CCGL.h" #include "kazmath/kazmath.h" +#include NS_CC_BEGIN @@ -245,7 +246,7 @@ public: void reset(); inline const GLuint getProgram() const { return _program; } - + inline const GLuint getMaterialProgramID() const { return _materialProgramID; } // DEPRECATED CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) { return initWithByteArrays(vShaderByteArray, fShaderByteArray); } @@ -261,6 +262,8 @@ private: std::string logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const; private: + //ID used for renderer material, _materialProgramID maybe different from _program + GLuint _materialProgramID; GLuint _program; GLuint _vertShader; GLuint _fragShader; @@ -277,6 +280,21 @@ private: // handy way to initialize the bitfield flag_struct() { memset(this, 0, sizeof(*this)); } } _flags; +private: + class MaterialProgramIDAllocator + { + public: + MaterialProgramIDAllocator(); + ~MaterialProgramIDAllocator(); + GLuint allocID(); + void freeID(GLuint id); + private: + std::set _freeIDs; + }; + + static MaterialProgramIDAllocator _idAllocator; +public: + static const GLuint _maxMaterialIDNumber; }; // end of shaders group diff --git a/cocos/2d/renderer/CCQuadCommand.cpp b/cocos/2d/renderer/CCQuadCommand.cpp index 5c75d43665..2004730bd8 100644 --- a/cocos/2d/renderer/CCQuadCommand.cpp +++ b/cocos/2d/renderer/CCQuadCommand.cpp @@ -61,9 +61,7 @@ void QuadCommand::generateMaterialID() { //Generate Material ID //TODO fix shader ID generation - CCASSERT(_shader->getProgram() < pow(2,10), "ShaderID is greater than 2^10"); - //TODO fix texture ID generation - CCASSERT(_textureID < pow(2,18), "TextureID is greater than 2^18"); + CCASSERT(_shader->getMaterialProgramID() < GLProgram::_maxMaterialIDNumber, "ShaderID is greater than 2^10"); //TODO fix blend id generation int blendID = 0; @@ -96,7 +94,7 @@ void QuadCommand::generateMaterialID() // | Shader ID (10 bits) | Blend ID (4 bits) | empty (18bits) | Texture ID (32 bits) | // +---------------------+-------------------+----------------------------------------+ - _materialID = (uint64_t)_shader->getProgram() << 54 + _materialID = (uint64_t)_shader->getMaterialProgramID() << 54 | (uint64_t)blendID << 50 | (uint64_t)_textureID << 0; } From bc15ac14158920a18cb3999d035b1ba4ec9826be Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 26 Mar 2014 10:07:50 +0800 Subject: [PATCH 2/2] closed #4552: change assert log message --- cocos/2d/renderer/CCQuadCommand.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/renderer/CCQuadCommand.cpp b/cocos/2d/renderer/CCQuadCommand.cpp index 2004730bd8..f4e8317f6e 100644 --- a/cocos/2d/renderer/CCQuadCommand.cpp +++ b/cocos/2d/renderer/CCQuadCommand.cpp @@ -61,7 +61,7 @@ void QuadCommand::generateMaterialID() { //Generate Material ID //TODO fix shader ID generation - CCASSERT(_shader->getMaterialProgramID() < GLProgram::_maxMaterialIDNumber, "ShaderID is greater than 2^10"); + CCASSERT(_shader->getMaterialProgramID() < GLProgram::_maxMaterialIDNumber, "ShaderID is greater than Id limitation"); //TODO fix blend id generation int blendID = 0;