From a526fc5f92676f78923d759c501240bd4366963b Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 25 Jan 2013 13:20:42 +0100 Subject: [PATCH] ubo streaming --- .../Src/ProgramShaderCache.cpp | 29 ++++++++++++------- .../Plugin_VideoOGL/Src/ProgramShaderCache.h | 1 + 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp index db7e954c44..44906bdf0b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.cpp @@ -21,9 +21,12 @@ namespace OGL { +static const u32 UBO_LENGTH = 1024*1024; + GLuint ProgramShaderCache::CurrentProgram = 0; ProgramShaderCache::PCache ProgramShaderCache::pshaders; GLuint ProgramShaderCache::s_ps_vs_ubo; +u32 ProgramShaderCache::s_ubo_iterator; GLintptr ProgramShaderCache::s_vs_data_offset; float *ProgramShaderCache::s_ubo_buffer; u32 ProgramShaderCache::s_ubo_buffer_size; @@ -192,12 +195,23 @@ void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float void ProgramShaderCache::UploadConstants() { - if(s_ubo_dirty) - glBufferData(GL_UNIFORM_BUFFER, s_ubo_buffer_size, s_ubo_buffer, GL_STREAM_DRAW); + if(s_ubo_dirty) { + if(s_ubo_iterator + s_ubo_buffer_size >= UBO_LENGTH) { + glBufferData(GL_UNIFORM_BUFFER, UBO_LENGTH, NULL, GL_STREAM_DRAW); + s_ubo_iterator = 0; + } + void *ubo = glMapBufferRange(GL_UNIFORM_BUFFER, s_ubo_iterator, s_ubo_buffer_size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); + memcpy(ubo, s_ubo_buffer, s_ubo_buffer_size); + glUnmapBuffer(GL_UNIFORM_BUFFER); + + glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, s_ubo_iterator, s_vs_data_offset); + glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_ubo_iterator + s_vs_data_offset, s_ubo_buffer_size - s_vs_data_offset); + + s_ubo_iterator += s_ubo_buffer_size; + } s_ubo_dirty = false; } - GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; @@ -228,13 +242,8 @@ void ProgramShaderCache::Init(void) // Then once more to get bytes glGenBuffers(1, &s_ps_vs_ubo); glBindBuffer(GL_UNIFORM_BUFFER, s_ps_vs_ubo); - glBufferData(GL_UNIFORM_BUFFER, s_ubo_buffer_size, NULL, GL_STREAM_DRAW); - - // Now bind the buffer to the index point - // We know PS is 0 since we have it statically set in the shader - // Repeat for VS shader - glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, 0, ps_data_size); - glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size); + glBufferData(GL_UNIFORM_BUFFER, UBO_LENGTH, NULL, GL_STREAM_DRAW); + s_ubo_iterator = 0; s_ubo_buffer = new float[s_ubo_buffer_size/sizeof(float)]; memset(s_ubo_buffer, 0, s_ubo_buffer_size); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h index 890679380b..004f820def 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/ProgramShaderCache.h @@ -154,6 +154,7 @@ private: static ShaderUID CurrentShaderProgram; static GLuint s_ps_vs_ubo; + static u32 s_ubo_iterator; static GLintptr s_vs_data_offset; static float *s_ubo_buffer; static u32 s_ubo_buffer_size;