From 2e9ff045e17126955608d355c2f28d988146bf04 Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Sat, 15 Aug 2015 09:58:13 +0200 Subject: [PATCH] gsdx-ogl: try another implementation for uniform buffer It is slower but keep it for a bindless trial --- plugins/GSdx/GSUniformBufferOGL.h | 75 ++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/plugins/GSdx/GSUniformBufferOGL.h b/plugins/GSdx/GSUniformBufferOGL.h index 288a21d633..2c1451b1e6 100644 --- a/plugins/GSdx/GSUniformBufferOGL.h +++ b/plugins/GSdx/GSUniformBufferOGL.h @@ -31,7 +31,7 @@ extern uint64 g_uniform_upload_byte; class GSUniformBufferOGL { GLuint buffer; // data object GLuint index; // GLSL slot - uint32 size; // size of the data + uint32 size; // size of the data public: GSUniformBufferOGL(GLuint index, uint32 size) : index(index) @@ -80,3 +80,76 @@ public: gl_DeleteBuffers(1, &buffer); } }; + +#define UBO_BUFFER_SIZE (4*1024*1024) + +class GSUniformBufferStorageOGL { + GLuint buffer; // data object + GLuint index; // GLSL slot + uint32 size; // size of the data + uint8* m_buffer_ptr; + uint32 m_offset; + +public: + GSUniformBufferStorageOGL(GLuint index, uint32 size) : index(index) + , size(size), m_offset(0) + { + gl_GenBuffers(1, &buffer); + bind(); + allocate(); + attach(); + } + + void bind() + { + if (GLState::ubo != buffer) { + GLState::ubo = buffer; + gl_BindBuffer(GL_UNIFORM_BUFFER, buffer); + } + } + + void allocate() + { + const GLbitfield common_flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT /*| GL_MAP_COHERENT_BIT */; + const GLbitfield map_flags = common_flags | GL_MAP_FLUSH_EXPLICIT_BIT; + const GLbitfield create_flags = common_flags /*| GL_CLIENT_STORAGE_BIT */; + + GLsizei buffer_size = UBO_BUFFER_SIZE; + gl_BufferStorage(GL_UNIFORM_BUFFER, buffer_size, NULL, create_flags); + m_buffer_ptr = (uint8*) gl_MapBufferRange(GL_UNIFORM_BUFFER, 0, buffer_size, map_flags); + ASSERT(m_buffer_ptr); + } + + void attach() + { + // From the opengl manpage: + // glBindBufferBase also binds buffer to the generic buffer binding point specified by target + GLState::ubo = buffer; + //gl_BindBufferBase(GL_UNIFORM_BUFFER, index, buffer); + gl_BindBufferRange(GL_UNIFORM_BUFFER, index, buffer, m_offset, size); + } + + void upload(const void* src) + { +#ifdef ENABLE_OGL_DEBUG_MEM_BW + g_uniform_upload_byte += size; +#endif + + memcpy(m_buffer_ptr + m_offset, src, size); + + attach(); + gl_FlushMappedBufferRange(GL_UNIFORM_BUFFER, m_offset, size); + + m_offset = (m_offset + size + 255u) & ~0xFF; + if (m_offset >= UBO_BUFFER_SIZE) + m_offset = 0; + } + + ~GSUniformBufferStorageOGL() { + bind(); + gl_UnmapBuffer(GL_UNIFORM_BUFFER); + gl_DeleteBuffers(1, &buffer); + } +}; + +#undef UBO_BUFFER_SIZE