From 22776dcd8840dad3018c0dc802058a877f042460 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 21 Oct 2014 17:29:24 +0800 Subject: [PATCH] Removes uthash in CCGLProgram --- cocos/renderer/CCGLProgram.cpp | 60 ++++++++++------------------------ cocos/renderer/CCGLProgram.h | 3 +- 2 files changed, 19 insertions(+), 44 deletions(-) diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index e484aceea4..2fa4b516fb 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -45,13 +45,6 @@ THE SOFTWARE. NS_CC_BEGIN -typedef struct _hashUniformEntry -{ - GLvoid* value; // value - unsigned int location; // Key - UT_hash_handle hh; // hash entry -} tHashUniformEntry; - const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR = "ShaderPositionTextureColor"; const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP = "ShaderPositionTextureColor_noMVP"; const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST = "ShaderPositionTextureColorAlphaTest"; @@ -136,7 +129,6 @@ GLProgram::GLProgram() : _program(0) , _vertShader(0) , _fragShader(0) -, _hashForUniforms(nullptr) , _flags() { memset(_builtInUniforms, 0, sizeof(_builtInUniforms)); @@ -163,15 +155,11 @@ GLProgram::~GLProgram() GL::deleteProgram(_program); } - tHashUniformEntry *current_element, *tmp; - - // Purge uniform hash - HASH_ITER(hh, _hashForUniforms, current_element, tmp) + for (auto e : _hashForUniforms) { - HASH_DEL(_hashForUniforms, current_element); - free(current_element->value); - free(current_element); + free(e.second); } + _hashForUniforms.clear(); } bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) @@ -222,7 +210,8 @@ bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* { glAttachShader(_program, _fragShader); } - _hashForUniforms = nullptr; + + _hashForUniforms.clear(); CHECK_GL_ERROR_DEBUG(); @@ -260,7 +249,7 @@ bool GLProgram::initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArr haveProgram = CCPrecompiledShaders::getInstance()->loadProgram(_program, vShaderByteArray, fShaderByteArray); CHECK_GL_ERROR_DEBUG(); - _hashForUniforms = nullptr; + _hashForUniforms.clear(); CHECK_GL_ERROR_DEBUG(); @@ -631,31 +620,23 @@ bool GLProgram::updateUniformLocation(GLint location, const GLvoid* data, unsign } bool updated = true; - tHashUniformEntry *element = nullptr; - HASH_FIND_INT(_hashForUniforms, &location, element); - - if (! element) + + auto element = _hashForUniforms.find(location); + if (element == _hashForUniforms.end()) { - element = (tHashUniformEntry*)malloc( sizeof(*element) ); - - // key - element->location = location; - - // value - element->value = malloc( bytes ); - memcpy(element->value, data, bytes ); - - HASH_ADD_INT(_hashForUniforms, location, element); + GLvoid* value = malloc(bytes); + memcpy(value, data, bytes ); + _hashForUniforms.insert(std::make_pair(location, value)); } else { - if (memcmp(element->value, data, bytes) == 0) + if (memcmp(element->second, data, bytes) == 0) { updated = false; } else { - memcpy(element->value, data, bytes); + memcpy(element->second, data, bytes); } } @@ -921,17 +902,12 @@ void GLProgram::reset() //GL::deleteProgram(_program); _program = 0; - - tHashUniformEntry *current_element, *tmp; - - // Purge uniform hash - HASH_ITER(hh, _hashForUniforms, current_element, tmp) + for (auto e: _hashForUniforms) { - HASH_DEL(_hashForUniforms, current_element); - free(current_element->value); - free(current_element); + free(e.second); } - _hashForUniforms = nullptr; + + _hashForUniforms.clear(); } NS_CC_END diff --git a/cocos/renderer/CCGLProgram.h b/cocos/renderer/CCGLProgram.h index fcb7670829..5f422722ad 100644 --- a/cocos/renderer/CCGLProgram.h +++ b/cocos/renderer/CCGLProgram.h @@ -45,7 +45,6 @@ NS_CC_BEGIN * @{ */ -struct _hashUniformEntry; class GLProgram; typedef void (*GLInfoFunction)(GLuint program, GLenum pname, GLint* params); @@ -340,7 +339,6 @@ protected: GLuint _vertShader; GLuint _fragShader; GLint _builtInUniforms[UNIFORM_MAX]; - struct _hashUniformEntry* _hashForUniforms; bool _hasShaderCompiler; #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || defined(WP8_SHADER_COMPILER) @@ -360,6 +358,7 @@ protected: std::unordered_map _userUniforms; std::unordered_map _vertexAttribs; + std::unordered_map _hashForUniforms; }; NS_CC_END