From bb419719aa6b6c63301837366710ded8a0232b78 Mon Sep 17 00:00:00 2001 From: lvlong Date: Mon, 26 Jan 2015 18:40:00 +0800 Subject: [PATCH 1/2] fix bug: about caching uniform. --- cocos/renderer/CCGLProgram.cpp | 16 +++++++++++----- cocos/renderer/CCGLProgram.h | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index b80e40d6ae..3d2c3a3aec 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -159,7 +159,7 @@ GLProgram::~GLProgram() for (auto e : _hashForUniforms) { - free(e.second); + free(e.second.first); } _hashForUniforms.clear(); } @@ -662,17 +662,23 @@ bool GLProgram::updateUniformLocation(GLint location, const GLvoid* data, unsign { GLvoid* value = malloc(bytes); memcpy(value, data, bytes ); - _hashForUniforms.insert(std::make_pair(location, value)); + _hashForUniforms.insert(std::make_pair(location, std::make_pair(value, bytes))); } else { - if (memcmp(element->second, data, bytes) == 0) + if (memcmp(element->second.first, data, bytes) == 0) { updated = false; } else { - memcpy(element->second, data, bytes); + if (element->second.second != bytes){ + free(element->second.first); + GLvoid* value = malloc(bytes); + memcpy(value, data, bytes ); + _hashForUniforms[location] = std::make_pair(value, bytes); + }else + memcpy(element->second.first, data, bytes); } } @@ -933,7 +939,7 @@ void GLProgram::reset() for (auto e: _hashForUniforms) { - free(e.second); + free(e.second.first); } _hashForUniforms.clear(); diff --git a/cocos/renderer/CCGLProgram.h b/cocos/renderer/CCGLProgram.h index 4577311262..36054e1d58 100644 --- a/cocos/renderer/CCGLProgram.h +++ b/cocos/renderer/CCGLProgram.h @@ -357,7 +357,7 @@ protected: std::unordered_map _userUniforms; std::unordered_map _vertexAttribs; - std::unordered_map _hashForUniforms; + std::unordered_map> _hashForUniforms; //cached director pointer for calling Director* _director; }; From 57970da3989615ebbc39fca562f9b7bd1cb91cc5 Mon Sep 17 00:00:00 2001 From: lvlong Date: Tue, 27 Jan 2015 15:50:05 +0800 Subject: [PATCH 2/2] Do some optimize --- cocos/renderer/CCGLProgram.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index 3d2c3a3aec..ac5ee3ecfe 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -672,9 +672,8 @@ bool GLProgram::updateUniformLocation(GLint location, const GLvoid* data, unsign } else { - if (element->second.second != bytes){ - free(element->second.first); - GLvoid* value = malloc(bytes); + if (element->second.second < bytes){ + GLvoid* value = realloc(element->second.first, bytes); memcpy(value, data, bytes ); _hashForUniforms[location] = std::make_pair(value, bytes); }else