GS: Use stream buffer for VS/GS/FS uniforms

This commit is contained in:
Connor McLaughlin 2021-10-11 17:02:32 +10:00 committed by refractionpcsx2
parent 62b40b516c
commit c09240de3b
5 changed files with 28 additions and 100 deletions

View File

@ -37,8 +37,6 @@ namespace GLState
GLenum stencil_func;
GLenum stencil_pass;
GLuint ubo;
GLuint ps_ss;
GLuint rt;
@ -75,8 +73,6 @@ namespace GLState
stencil_func = 0;
stencil_pass = 0xFFFF; // Note 0 is valid (GL_ZERO)
ubo = 0;
ps_ss = 0;
rt = 0;

View File

@ -39,8 +39,6 @@ namespace GLState
extern GLenum stencil_func;
extern GLenum stencil_pass;
extern GLuint ubo; // uniform buffer object
extern GLuint ps_ss; // sampler
extern GLuint rt; // render target

View File

@ -44,6 +44,8 @@ static constexpr uint32 g_ps_cb_index = 21;
static constexpr u32 VERTEX_BUFFER_SIZE = 32 * 1024 * 1024;
static constexpr u32 INDEX_BUFFER_SIZE = 16 * 1024 * 1024;
static constexpr u32 VERTEX_UNIFORM_BUFFER_SIZE = 8 * 1024 * 1024;
static constexpr u32 FRAGMENT_UNIFORM_BUFFER_SIZE = 8 * 1024 * 1024;
bool GSDeviceOGL::m_debug_gl_call = false;
int GSDeviceOGL::m_shader_inst = 0;
@ -56,8 +58,6 @@ GSDeviceOGL::GSDeviceOGL()
, m_fbo_read(0)
, m_apitrace(0)
, m_palette_ss(0)
, m_vs_cb(NULL)
, m_ps_cb(NULL)
, m_shader(NULL)
{
memset(&m_merge_obj, 0, sizeof(m_merge_obj));
@ -137,8 +137,8 @@ GSDeviceOGL::~GSDeviceOGL()
glDeleteFramebuffers(1, &m_fbo_read);
// Delete HW FX
delete m_vs_cb;
delete m_ps_cb;
m_vertex_uniform_stream_buffer.reset();
m_fragment_uniform_stream_buffer.reset();
glDeleteSamplers(1, &m_palette_ss);
m_ps.clear();
@ -391,9 +391,12 @@ bool GSDeviceOGL::Create(const WindowInfo& wi)
m_vertex_stream_buffer = GL::StreamBuffer::Create(GL_ARRAY_BUFFER, VERTEX_BUFFER_SIZE);
m_index_stream_buffer = GL::StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, INDEX_BUFFER_SIZE);
if (!m_vertex_stream_buffer || !m_index_stream_buffer)
m_vertex_uniform_stream_buffer = GL::StreamBuffer::Create(GL_UNIFORM_BUFFER, VERTEX_UNIFORM_BUFFER_SIZE);
m_fragment_uniform_stream_buffer = GL::StreamBuffer::Create(GL_UNIFORM_BUFFER, FRAGMENT_UNIFORM_BUFFER_SIZE);
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &m_uniform_buffer_alignment);
if (!m_vertex_stream_buffer || !m_index_stream_buffer || !m_vertex_uniform_stream_buffer || !m_fragment_uniform_stream_buffer)
{
Console.Error("Failed to create vertex/index streaming buffers");
Console.Error("Failed to create vertex/index/uniform streaming buffers");
return false;
}
@ -641,9 +644,6 @@ void GSDeviceOGL::CreateTextureFX()
{
GL_PUSH("GSDeviceOGL::CreateTextureFX");
m_vs_cb = new GSUniformBufferOGL("HW VS UBO", g_vs_cb_index, sizeof(VSConstantBuffer));
m_ps_cb = new GSUniformBufferOGL("HW PS UBO", g_ps_cb_index, sizeof(PSConstantBuffer));
theApp.LoadResource(IDR_TFX_VGS_GLSL, m_shader_tfx_vgs);
theApp.LoadResource(IDR_TFX_FS_GLSL, m_shader_tfx_fs);
@ -1938,17 +1938,29 @@ void GSDeviceOGL::OMSetRenderTargets(GSTexture* rt, GSTexture* ds, const GSVecto
}
}
__fi static void WriteToStreamBuffer(GL::StreamBuffer* sb, u32 index, u32 align, const void* data, u32 size)
{
const auto res = sb->Map(align, size);
std::memcpy(res.pointer, data, size);
sb->Unmap(size);
glBindBufferRange(GL_UNIFORM_BUFFER, index, sb->GetGLBufferId(), res.buffer_offset, size);
}
void GSDeviceOGL::SetupCB(const VSConstantBuffer* vs_cb, const PSConstantBuffer* ps_cb)
{
GL_PUSH("UBO");
if (m_vs_cb_cache.Update(vs_cb))
{
m_vs_cb->upload(vs_cb);
WriteToStreamBuffer(m_vertex_uniform_stream_buffer.get(), g_vs_cb_index,
m_uniform_buffer_alignment, vs_cb, sizeof(VSConstantBuffer));
}
if (m_ps_cb_cache.Update(ps_cb))
{
m_ps_cb->upload(ps_cb);
WriteToStreamBuffer(m_fragment_uniform_stream_buffer.get(), g_ps_cb_index,
m_uniform_buffer_alignment, ps_cb, sizeof(PSConstantBuffer));
}
}

View File

@ -489,6 +489,10 @@ private:
u32 m_index_buffer_offset = 0;
GLenum m_draw_topology = 0;
std::unique_ptr<GL::StreamBuffer> m_vertex_uniform_stream_buffer;
std::unique_ptr<GL::StreamBuffer> m_fragment_uniform_stream_buffer;
GLint m_uniform_buffer_alignment = 0;
struct
{
GLuint ps[2]; // program object
@ -552,9 +556,6 @@ private:
GLuint m_palette_ss;
GSUniformBufferOGL* m_vs_cb;
GSUniformBufferOGL* m_ps_cb;
VSConstantBuffer m_vs_cb_cache;
PSConstantBuffer m_ps_cb_cache;
MiscConstantBuffer m_misc_cb_cache;

View File

@ -44,11 +44,7 @@ public:
void bind()
{
if (GLState::ubo != m_buffer)
{
GLState::ubo = m_buffer;
glBindBuffer(GL_UNIFORM_BUFFER, m_buffer);
}
glBindBuffer(GL_UNIFORM_BUFFER, m_buffer);
}
void allocate()
@ -60,7 +56,6 @@ public:
{
// From the opengl manpage:
// glBindBufferBase also binds buffer to the generic buffer binding point specified by target
GLState::ubo = m_buffer;
glBindBufferBase(GL_UNIFORM_BUFFER, m_index, m_buffer);
}
@ -91,77 +86,3 @@ public:
_aligned_free(m_cache);
}
};
#define UBO_BUFFER_SIZE (4 * 1024 * 1024)
class GSUniformBufferStorageOGL
{
GLuint m_buffer; // data object
GLuint m_index; // GLSL slot
uint32 m_size; // size of the data
uint8* m_buffer_ptr;
uint32 m_offset;
public:
GSUniformBufferStorageOGL(GLuint index, uint32 size)
: m_index(index) , m_size(size) , m_offset(0)
{
glGenBuffers(1, &m_buffer);
bind();
allocate();
attach();
}
void bind()
{
if (GLState::ubo != m_buffer)
{
GLState::ubo = m_buffer;
glBindBuffer(GL_UNIFORM_BUFFER, m_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;
glBufferStorage(GL_UNIFORM_BUFFER, buffer_size, NULL, create_flags);
m_buffer_ptr = (uint8*)glMapBufferRange(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 = m_buffer;
//glBindBufferBase(GL_UNIFORM_BUFFER, m_index, m_buffer);
glBindBufferRange(GL_UNIFORM_BUFFER, m_index, m_buffer, m_offset, m_size);
}
void upload(const void* src)
{
#ifdef ENABLE_OGL_DEBUG_MEM_BW
g_uniform_upload_byte += m_size;
#endif
memcpy(m_buffer_ptr + m_offset, src, m_size);
attach();
glFlushMappedBufferRange(GL_UNIFORM_BUFFER, m_offset, m_size);
m_offset = (m_offset + m_size + 255u) & ~0xFF;
if (m_offset >= UBO_BUFFER_SIZE)
m_offset = 0;
}
~GSUniformBufferStorageOGL()
{
glDeleteBuffers(1, &m_buffer);
}
};
#undef UBO_BUFFER_SIZE