From 7da47da66e150e1d5bc1e43a37b3d9d9554b1c5f Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 15 Apr 2018 11:15:54 -0500 Subject: [PATCH 1/2] GPU: Use the buffer hints from the shader decompiler to upload only the necessary const buffers for each shader stage. --- .../renderer_opengl/gl_rasterizer.cpp | 63 +++++++++++-------- .../renderer_opengl/gl_rasterizer.h | 3 +- src/video_core/renderer_opengl/gl_state.h | 2 +- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 2a2268c20e..8709f9fc23 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -223,15 +223,19 @@ void RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr buffer_offset, size Memory::ReadBlock(cpu_address, program_code.data(), program_code.size() * sizeof(u64)); GLShader::ShaderSetup setup{std::move(program_code)}; + GLShader::ShaderEntries shader_resources; + switch (program) { case Maxwell::ShaderProgram::VertexB: { GLShader::MaxwellVSConfig vs_config{setup}; - shader_program_manager->UseProgrammableVertexShader(vs_config, setup); + shader_resources = + shader_program_manager->UseProgrammableVertexShader(vs_config, setup); break; } case Maxwell::ShaderProgram::Fragment: { GLShader::MaxwellFSConfig fs_config{setup}; - shader_program_manager->UseProgrammableFragmentShader(fs_config, setup); + shader_resources = + shader_program_manager->UseProgrammableFragmentShader(fs_config, setup); break; } default: @@ -239,6 +243,10 @@ void RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr buffer_offset, size shader_config.enable.Value(), shader_config.offset); UNREACHABLE(); } + + // Configure the const buffers for this shader stage. + SetupConstBuffers(static_cast(stage), + shader_resources.const_buffer_entries); } shader_program_manager->UseTrivialGeometryShader(); @@ -306,8 +314,6 @@ 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 @@ -537,36 +543,39 @@ void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntr } } -void RasterizerOpenGL::SetupConstBuffers() { - using Regs = Tegra::Engines::Maxwell3D::Regs; +void RasterizerOpenGL::SetupConstBuffers(Maxwell::ShaderStage stage, + const std::vector& entries) { auto& gpu = Core::System::GetInstance().GPU(); auto& maxwell3d = gpu.Get3DEngine(); + ASSERT_MSG(maxwell3d.IsShaderStageEnabled(stage), + "Attempted to upload constbuffer of disabled shader stage"); + + // Reset all buffer draw state for this stage. + for (auto& buffer : state.draw.const_buffers[static_cast(stage)]) { + buffer.bindpoint = 0; + buffer.enabled = false; + } + // 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(stage)); + auto& shader_stage = maxwell3d.state.shader_stages[static_cast(stage)]; - for (u32 buffer_id = 0; buffer_id < Regs::MaxConstBuffers; ++buffer_id) { - const auto& buffer = shader_stage.const_buffers[buffer_id]; + for (u32 bindpoint = 0; bindpoint < entries.size(); ++bindpoint) { + const auto& used_buffer = entries[bindpoint]; + const auto& buffer = shader_stage.const_buffers[used_buffer.GetIndex()]; + auto& buffer_draw_state = + state.draw.const_buffers[static_cast(stage)][used_buffer.GetIndex()]; - state.draw.const_buffers[stage][buffer_id].enabled = buffer.enabled && stage_enabled; + ASSERT_MSG(buffer.enabled, "Attempted to upload disabled constbuffer"); + buffer_draw_state.enabled = true; + buffer_draw_state.bindpoint = bindpoint; - 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; - } - } + VAddr addr = gpu.memory_manager->PhysicalToVirtualAddress(buffer.address); + const u8* data = Memory::GetPointer(addr); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer_draw_state.ssbo); + glBufferData(GL_SHADER_STORAGE_BUFFER, used_buffer.GetSize() * sizeof(float), data, + GL_DYNAMIC_DRAW); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); } state.Apply(); diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index bf3308aef8..1ea0dfa71d 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -88,7 +88,8 @@ private: void BindTextures(); /// Configures the current constbuffers to use for the draw command. - void SetupConstBuffers(); + void SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, + const std::vector& entries); /// Syncs the viewport to match the guest state void SyncViewport(const MathUtil::Rectangle& surfaces_rect, u16 res_scale); diff --git a/src/video_core/renderer_opengl/gl_state.h b/src/video_core/renderer_opengl/gl_state.h index 6a80e6a7da..75c08e645d 100644 --- a/src/video_core/renderer_opengl/gl_state.h +++ b/src/video_core/renderer_opengl/gl_state.h @@ -124,7 +124,7 @@ public: GLuint shader_program; // GL_CURRENT_PROGRAM GLuint program_pipeline; // GL_PROGRAM_PIPELINE_BINDING struct ConstBufferConfig { - bool enabled; + bool enabled = false; GLuint bindpoint; GLuint ssbo; }; From e128e90350a95bbeeebb3987c3513537e5e4289c Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 15 Apr 2018 11:18:09 -0500 Subject: [PATCH 2/2] GPU: Don't use GetPointer when uploading the constbuffer data to the GPU. --- src/video_core/renderer_opengl/gl_rasterizer.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 8709f9fc23..a778dfc640 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -571,10 +571,11 @@ void RasterizerOpenGL::SetupConstBuffers(Maxwell::ShaderStage stage, buffer_draw_state.bindpoint = bindpoint; VAddr addr = gpu.memory_manager->PhysicalToVirtualAddress(buffer.address); - const u8* data = Memory::GetPointer(addr); + std::vector data(used_buffer.GetSize() * sizeof(float)); + Memory::ReadBlock(addr, data.data(), data.size()); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer_draw_state.ssbo); - glBufferData(GL_SHADER_STORAGE_BUFFER, used_buffer.GetSize() * sizeof(float), data, - GL_DYNAMIC_DRAW); + glBufferData(GL_SHADER_STORAGE_BUFFER, data.size(), data.data(), GL_DYNAMIC_DRAW); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); }