From 1bb794dd39987ee26fd08cb104a3539013ac8a0c Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 15 Sep 2019 01:18:58 +1000 Subject: [PATCH] GPU: Use max vertex count based on buffer size --- src/pse/gpu_hw.cpp | 4 +++- src/pse/gpu_hw.h | 5 +++-- src/pse/gpu_hw_opengl.cpp | 12 ++++-------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/pse/gpu_hw.cpp b/src/pse/gpu_hw.cpp index 60f1e8145..1cc25c35e 100644 --- a/src/pse/gpu_hw.cpp +++ b/src/pse/gpu_hw.cpp @@ -379,7 +379,9 @@ void GPU_HW::DispatchRenderCommand(RenderCommand rc, u32 num_vertices) // flush when the command changes if (!m_batch_vertices.empty()) { - if (m_batch_vertices.size() >= MAX_BATCH_VERTEX_COUNT || m_batch_command.bits != rc.bits) + // including the degenerate triangles for strips + const u32 max_added_vertices = num_vertices + 2; + if ((m_batch_vertices.size() + max_added_vertices) >= MAX_BATCH_VERTEX_COUNT || m_batch_command.bits != rc.bits) FlushRender(); } diff --git a/src/pse/gpu_hw.h b/src/pse/gpu_hw.h index a84c3a99e..1d8708d23 100644 --- a/src/pse/gpu_hw.h +++ b/src/pse/gpu_hw.h @@ -11,8 +11,6 @@ public: virtual ~GPU_HW(); protected: - static constexpr u32 MAX_BATCH_VERTEX_COUNT = 1024; - struct HWVertex { s32 x; @@ -28,6 +26,9 @@ protected: static constexpr u16 EncodeTexcoord(u8 x, u8 y) { return ZeroExtend16(x) | (ZeroExtend16(y) << 8); } }; + static constexpr u32 VERTEX_BUFFER_SIZE = 1 * 1024 * 1024; + static constexpr u32 MAX_BATCH_VERTEX_COUNT = VERTEX_BUFFER_SIZE / sizeof(HWVertex); + static constexpr std::tuple RGBA8ToFloat(u32 rgba) { return std::make_tuple(static_cast(rgba & UINT32_C(0xFF)) * (1.0f / 255.0f), diff --git a/src/pse/gpu_hw_opengl.cpp b/src/pse/gpu_hw_opengl.cpp index 7bfbaa958..0b14b5cd3 100644 --- a/src/pse/gpu_hw_opengl.cpp +++ b/src/pse/gpu_hw_opengl.cpp @@ -81,7 +81,7 @@ void GPU_HW_OpenGL::CreateVertexBuffer() { glGenBuffers(1, &m_vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, 128, nullptr, GL_STREAM_DRAW); + glBufferData(GL_ARRAY_BUFFER, VERTEX_BUFFER_SIZE, nullptr, GL_STREAM_DRAW); glGenVertexArrays(1, &m_vao_id); glBindVertexArray(m_vao_id); @@ -339,14 +339,10 @@ void GPU_HW_OpenGL::FlushRender() glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id); glBindVertexArray(m_vao_id); + Assert((m_batch_vertices.size() * sizeof(HWVertex)) <= VERTEX_BUFFER_SIZE); glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, static_cast(sizeof(HWVertex) * m_batch_vertices.size()), - m_batch_vertices.data(), GL_STREAM_DRAW); - glVertexAttribIPointer(0, 2, GL_INT, sizeof(HWVertex), reinterpret_cast(offsetof(HWVertex, x))); - glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, sizeof(HWVertex), - reinterpret_cast(offsetof(HWVertex, color))); - glVertexAttribPointer(2, 2, GL_UNSIGNED_BYTE, true, sizeof(HWVertex), - reinterpret_cast(offsetof(HWVertex, texcoord))); + glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast(sizeof(HWVertex) * m_batch_vertices.size()), + m_batch_vertices.data()); const bool is_strip = ((m_batch_command.primitive == Primitive::Polygon && m_batch_command.quad_polygon) || m_batch_command.primitive == Primitive::Rectangle);