From 6225876692ec2de6ce7406f49adb90ec46e894ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 28 Jul 2017 09:43:45 -0700 Subject: [PATCH] If hash match reuse existing shaders instead creating. --- src/bgfx_p.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 7aa2e0640..e56314e1f 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -3056,11 +3056,23 @@ namespace bgfx return invalid; } + uint32_t shaderHash = bx::hashMurmur2A(_mem->data, _mem->size); + uint16_t idx = m_shaderHashMap.find(shaderHash); + if (kInvalidHandle != idx) + { + ShaderHandle handle = { idx }; + shaderIncRef(handle); + return handle; + } + ShaderHandle handle = { m_shaderHandle.alloc() }; BX_WARN(isValid(handle), "Failed to allocate shader handle."); if (isValid(handle) ) { + bool ok = m_shaderHashMap.insert(shaderHash, handle.idx); + BX_CHECK(ok, "Shader already exists!"); BX_UNUSED(ok); + uint32_t iohash; bx::read(&reader, iohash); @@ -3194,6 +3206,8 @@ namespace bgfx sr.m_uniforms = NULL; sr.m_num = 0; } + + m_shaderHashMap.removeByHandle(_handle.idx); } } @@ -4373,6 +4387,8 @@ namespace bgfx UniformHashMap m_uniformHashMap; UniformRef m_uniformRef[BGFX_CONFIG_MAX_UNIFORMS]; + typedef bx::HandleHashMapT ShaderHashMap; + ShaderHashMap m_shaderHashMap; ShaderRef m_shaderRef[BGFX_CONFIG_MAX_SHADERS]; typedef bx::HandleHashMapT ProgramHashMap;