2012-05-13 17:09:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2011 Gregory hainaut
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
2012-09-09 18:16:11 +00:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
2012-05-13 17:09:18 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2013-08-05 20:25:25 +00:00
|
|
|
#include "GLState.h"
|
|
|
|
|
2015-04-25 11:00:03 +00:00
|
|
|
#ifdef ENABLE_OGL_DEBUG_MEM_BW
|
2015-04-25 12:18:21 +00:00
|
|
|
extern uint64 g_uniform_upload_byte;
|
2015-04-25 11:00:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-05-13 17:09:18 +00:00
|
|
|
class GSUniformBufferOGL {
|
2016-06-08 19:08:11 +00:00
|
|
|
GLuint m_buffer; // data object
|
|
|
|
GLuint m_index; // GLSL slot
|
|
|
|
uint32 m_size; // size of the data
|
|
|
|
uint8* m_cache; // content of the previous upload
|
2012-05-13 17:09:18 +00:00
|
|
|
|
|
|
|
public:
|
2016-06-07 18:30:56 +00:00
|
|
|
GSUniformBufferOGL(const string& pretty_name, GLuint index, uint32 size)
|
2016-06-08 19:08:11 +00:00
|
|
|
: m_index(index), m_size(size)
|
2012-05-13 17:09:18 +00:00
|
|
|
{
|
2016-06-08 19:08:11 +00:00
|
|
|
glGenBuffers(1, &m_buffer);
|
2012-05-13 17:09:18 +00:00
|
|
|
bind();
|
2016-06-08 19:08:11 +00:00
|
|
|
glObjectLabel(GL_BUFFER, m_buffer, pretty_name.size(), pretty_name.c_str());
|
2012-05-13 17:09:18 +00:00
|
|
|
allocate();
|
|
|
|
attach();
|
2016-06-08 19:08:11 +00:00
|
|
|
m_cache = (uint8*)_aligned_malloc(m_size, 32);
|
|
|
|
memset(m_cache, 0, m_size);
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bind()
|
|
|
|
{
|
2016-06-08 19:08:11 +00:00
|
|
|
if (GLState::ubo != m_buffer) {
|
|
|
|
GLState::ubo = m_buffer;
|
|
|
|
glBindBuffer(GL_UNIFORM_BUFFER, m_buffer);
|
2013-08-05 20:25:25 +00:00
|
|
|
}
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void allocate()
|
|
|
|
{
|
2016-06-08 19:08:11 +00:00
|
|
|
glBufferData(GL_UNIFORM_BUFFER, m_size, NULL, GL_DYNAMIC_DRAW);
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void attach()
|
|
|
|
{
|
2014-10-07 17:58:35 +00:00
|
|
|
// From the opengl manpage:
|
|
|
|
// glBindBufferBase also binds buffer to the generic buffer binding point specified by target
|
2016-06-08 19:08:11 +00:00
|
|
|
GLState::ubo = m_buffer;
|
|
|
|
glBindBufferBase(GL_UNIFORM_BUFFER, m_index, m_buffer);
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void upload(const void* src)
|
|
|
|
{
|
2013-08-05 20:25:25 +00:00
|
|
|
bind();
|
2013-06-07 19:16:27 +00:00
|
|
|
// glMapBufferRange allow to set various parameter but the call is
|
|
|
|
// synchronous whereas glBufferSubData could be asynchronous.
|
|
|
|
// TODO: investigate the extension ARB_invalidate_subdata
|
2016-06-08 19:08:11 +00:00
|
|
|
glBufferSubData(GL_UNIFORM_BUFFER, 0, m_size, src);
|
2015-04-25 11:00:03 +00:00
|
|
|
#ifdef ENABLE_OGL_DEBUG_MEM_BW
|
2016-06-08 19:08:11 +00:00
|
|
|
g_uniform_upload_byte += m_size;
|
2015-04-25 11:00:03 +00:00
|
|
|
#endif
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 16:53:17 +00:00
|
|
|
void cache_upload(const void* src)
|
|
|
|
{
|
2016-06-08 19:08:11 +00:00
|
|
|
if (memcmp(m_cache, src, m_size) != 0) {
|
|
|
|
memcpy(m_cache, src, m_size);
|
2016-05-28 16:53:17 +00:00
|
|
|
upload(src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~GSUniformBufferOGL()
|
|
|
|
{
|
2016-06-08 19:08:11 +00:00
|
|
|
glDeleteBuffers(1, &m_buffer);
|
|
|
|
_aligned_free(m_cache);
|
2012-05-13 17:09:18 +00:00
|
|
|
}
|
|
|
|
};
|
2015-08-15 07:58:13 +00:00
|
|
|
|
|
|
|
#define UBO_BUFFER_SIZE (4*1024*1024)
|
|
|
|
|
|
|
|
class GSUniformBufferStorageOGL {
|
2016-06-08 19:08:11 +00:00
|
|
|
GLuint m_buffer; // data object
|
|
|
|
GLuint m_index; // GLSL slot
|
|
|
|
uint32 m_size; // size of the data
|
2015-08-15 07:58:13 +00:00
|
|
|
uint8* m_buffer_ptr;
|
|
|
|
uint32 m_offset;
|
|
|
|
|
|
|
|
public:
|
2016-06-08 19:08:11 +00:00
|
|
|
GSUniformBufferStorageOGL(GLuint index, uint32 size) : m_index(index)
|
|
|
|
, m_size(size), m_offset(0)
|
2015-08-15 07:58:13 +00:00
|
|
|
{
|
2016-06-08 19:08:11 +00:00
|
|
|
glGenBuffers(1, &m_buffer);
|
2015-08-15 07:58:13 +00:00
|
|
|
bind();
|
|
|
|
allocate();
|
|
|
|
attach();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bind()
|
|
|
|
{
|
2016-06-08 19:08:11 +00:00
|
|
|
if (GLState::ubo != m_buffer) {
|
|
|
|
GLState::ubo = m_buffer;
|
|
|
|
glBindBuffer(GL_UNIFORM_BUFFER, m_buffer);
|
2015-08-15 07:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-10-17 15:05:15 +00:00
|
|
|
glBufferStorage(GL_UNIFORM_BUFFER, buffer_size, NULL, create_flags);
|
|
|
|
m_buffer_ptr = (uint8*) glMapBufferRange(GL_UNIFORM_BUFFER, 0, buffer_size, map_flags);
|
2015-08-15 07:58:13 +00:00
|
|
|
ASSERT(m_buffer_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void attach()
|
|
|
|
{
|
|
|
|
// From the opengl manpage:
|
|
|
|
// glBindBufferBase also binds buffer to the generic buffer binding point specified by target
|
2016-06-08 19:08:11 +00:00
|
|
|
GLState::ubo = m_buffer;
|
|
|
|
//glBindBufferBase(GL_UNIFORM_BUFFER, m_index, m_buffer);
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, m_index, m_buffer, m_offset, m_size);
|
2015-08-15 07:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void upload(const void* src)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_OGL_DEBUG_MEM_BW
|
2016-06-08 19:08:11 +00:00
|
|
|
g_uniform_upload_byte += m_size;
|
2015-08-15 07:58:13 +00:00
|
|
|
#endif
|
|
|
|
|
2016-06-08 19:08:11 +00:00
|
|
|
memcpy(m_buffer_ptr + m_offset, src, m_size);
|
2015-08-15 07:58:13 +00:00
|
|
|
|
|
|
|
attach();
|
2016-06-08 19:08:11 +00:00
|
|
|
glFlushMappedBufferRange(GL_UNIFORM_BUFFER, m_offset, m_size);
|
2015-08-15 07:58:13 +00:00
|
|
|
|
2016-06-08 19:08:11 +00:00
|
|
|
m_offset = (m_offset + m_size + 255u) & ~0xFF;
|
2015-08-15 07:58:13 +00:00
|
|
|
if (m_offset >= UBO_BUFFER_SIZE)
|
|
|
|
m_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~GSUniformBufferStorageOGL() {
|
|
|
|
bind();
|
2015-10-17 15:05:15 +00:00
|
|
|
glUnmapBuffer(GL_UNIFORM_BUFFER);
|
2016-06-08 19:08:11 +00:00
|
|
|
glDeleteBuffers(1, &m_buffer);
|
2015-08-15 07:58:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef UBO_BUFFER_SIZE
|