From c9b511da084bcf1655a7e844ee0d80e46ce681c9 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sat, 14 Apr 2018 11:50:15 -0500
Subject: [PATCH] GPU: Upload the entirety of each constbuffer for each shader
 stage as SSBOs.

We're going to need the shader generator to give us a mapping of the actual used const buffers to properly bind them to the shader.
---
 .../renderer_opengl/gl_rasterizer.cpp         | 39 ++++++++++++++++++-
 .../renderer_opengl/gl_rasterizer.h           |  7 +++-
 .../renderer_opengl/gl_shader_manager.cpp     |  9 +----
 .../renderer_opengl/gl_shader_manager.h       |  7 ++--
 4 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index 71b862114..2a2268c20 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -306,6 +306,8 @@ void RasterizerOpenGL::DrawArrays() {
 
     // Sync and bind the texture surfaces
     BindTextures();
+    // Configure the constant buffer objects
+    SetupConstBuffers();
 
     // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable
     // scissor test to prevent drawing outside of the framebuffer region
@@ -388,7 +390,7 @@ void RasterizerOpenGL::DrawArrays() {
 
 void RasterizerOpenGL::BindTextures() {
     using Regs = Tegra::Engines::Maxwell3D::Regs;
-    auto maxwell3d = Core::System::GetInstance().GPU().Get3DEngine();
+    auto& maxwell3d = Core::System::GetInstance().GPU().Get3DEngine();
 
     // Each Maxwell shader stage can have an arbitrary number of textures, but we're limited to a
     // certain number in OpenGL. We try to only use the minimum amount of host textures by not
@@ -535,6 +537,41 @@ void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntr
     }
 }
 
+void RasterizerOpenGL::SetupConstBuffers() {
+    using Regs = Tegra::Engines::Maxwell3D::Regs;
+    auto& gpu = Core::System::GetInstance().GPU();
+    auto& maxwell3d = gpu.Get3DEngine();
+
+    // Upload only the enabled buffers from the 16 constbuffers of each shader stage
+    u32 current_bindpoint = 0;
+    for (u32 stage = 0; stage < Regs::MaxShaderStage; ++stage) {
+        auto& shader_stage = maxwell3d.state.shader_stages[stage];
+        bool stage_enabled = maxwell3d.IsShaderStageEnabled(static_cast<Regs::ShaderStage>(stage));
+
+        for (u32 buffer_id = 0; buffer_id < Regs::MaxConstBuffers; ++buffer_id) {
+            const auto& buffer = shader_stage.const_buffers[buffer_id];
+
+            state.draw.const_buffers[stage][buffer_id].enabled = buffer.enabled && stage_enabled;
+
+            if (buffer.enabled && stage_enabled) {
+                state.draw.const_buffers[stage][buffer_id].bindpoint = current_bindpoint;
+                current_bindpoint++;
+
+                VAddr addr = gpu.memory_manager->PhysicalToVirtualAddress(buffer.address);
+                const u8* data = Memory::GetPointer(addr);
+                glBindBuffer(GL_SHADER_STORAGE_BUFFER,
+                             state.draw.const_buffers[stage][buffer_id].ssbo);
+                glBufferData(GL_SHADER_STORAGE_BUFFER, buffer.size, data, GL_DYNAMIC_DRAW);
+                glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
+            } else {
+                state.draw.const_buffers[stage][buffer_id].bindpoint = -1;
+            }
+        }
+    }
+
+    state.Apply();
+}
+
 void RasterizerOpenGL::BindFramebufferSurfaces(const Surface& color_surface,
                                                const Surface& depth_surface, bool has_stencil) {
     state.draw.draw_framebuffer = framebuffer.handle;
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 527866e8a..bf3308aef 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -87,6 +87,9 @@ private:
     /// Binds the required textures to OpenGL before drawing a batch.
     void BindTextures();
 
+    /// Configures the current constbuffers to use for the draw command.
+    void SetupConstBuffers();
+
     /// Syncs the viewport to match the guest state
     void SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect, u16 res_scale);
 
@@ -129,7 +132,9 @@ private:
     std::array<bool, 16> hw_vao_enabled_attributes;
 
     std::array<SamplerInfo, GLShader::NumTextureSamplers> texture_samplers;
-    std::array<std::array<OGLBuffer, 16>, 5> ssbos;
+    std::array<std::array<OGLBuffer, Tegra::Engines::Maxwell3D::Regs::MaxConstBuffers>,
+               Tegra::Engines::Maxwell3D::Regs::MaxShaderStage>
+        ssbos;
 
     static constexpr size_t VERTEX_BUFFER_SIZE = 128 * 1024 * 1024;
     std::unique_ptr<OGLStreamBuffer> vertex_buffer;
diff --git a/src/video_core/renderer_opengl/gl_shader_manager.cpp b/src/video_core/renderer_opengl/gl_shader_manager.cpp
index 7fceedce8..85b838faa 100644
--- a/src/video_core/renderer_opengl/gl_shader_manager.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_manager.cpp
@@ -53,13 +53,6 @@ void SetShaderSamplerBindings(GLuint shader) {
 
 } // namespace Impl
 
-void MaxwellUniformData::SetFromRegs(const Maxwell3D::State::ShaderStageInfo& shader_stage) {
-    const auto& memory_manager = Core::System().GetInstance().GPU().memory_manager;
-    for (unsigned index = 0; index < shader_stage.const_buffers.size(); ++index) {
-        const auto& const_buffer = shader_stage.const_buffers[index];
-        const VAddr vaddr = memory_manager->PhysicalToVirtualAddress(const_buffer.address);
-        Memory::ReadBlock(vaddr, const_buffers[index].data(), sizeof(ConstBuffer));
-    }
-}
+void MaxwellUniformData::SetFromRegs(const Maxwell3D::State::ShaderStageInfo& shader_stage) {}
 
 } // namespace GLShader
diff --git a/src/video_core/renderer_opengl/gl_shader_manager.h b/src/video_core/renderer_opengl/gl_shader_manager.h
index 5c8560cf5..f003ce532 100644
--- a/src/video_core/renderer_opengl/gl_shader_manager.h
+++ b/src/video_core/renderer_opengl/gl_shader_manager.h
@@ -30,11 +30,10 @@ void SetShaderSamplerBindings(GLuint shader);
 //       Not following that rule will cause problems on some AMD drivers.
 struct MaxwellUniformData {
     void SetFromRegs(const Maxwell3D::State::ShaderStageInfo& shader_stage);
-
-    using ConstBuffer = std::array<GLvec4, 4>;
-    alignas(16) std::array<ConstBuffer, Maxwell3D::Regs::MaxConstBuffers> const_buffers;
+    // TODO(Subv): Use this for something.
 };
-static_assert(sizeof(MaxwellUniformData) == 1024, "MaxwellUniformData structure size is incorrect");
+// static_assert(sizeof(MaxwellUniformData) == 1024, "MaxwellUniformData structure size is
+// incorrect");
 static_assert(sizeof(MaxwellUniformData) < 16384,
               "MaxwellUniformData structure must be less than 16kb as per the OpenGL spec");