From 179ee963db1c8310e533daa4457b02b7a8141539 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Mon, 7 Jan 2019 01:11:03 -0300 Subject: [PATCH 1/3] gl_rasterizer_cache: Use dirty flags for color buffers --- src/video_core/engines/maxwell_3d.cpp | 9 +++++++++ src/video_core/engines/maxwell_3d.h | 3 +++ .../renderer_opengl/gl_rasterizer_cache.cpp | 14 ++++++++++---- .../renderer_opengl/gl_rasterizer_cache.h | 2 ++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp index 0ed7bc5d8..88483eab9 100644 --- a/src/video_core/engines/maxwell_3d.cpp +++ b/src/video_core/engines/maxwell_3d.cpp @@ -135,6 +135,15 @@ void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) { if (regs.reg_array[method_call.method] != method_call.argument) { regs.reg_array[method_call.method] = method_call.argument; + // Color buffers + constexpr u32 first_rt_reg = MAXWELL3D_REG_INDEX(rt); + constexpr u32 registers_per_rt = sizeof(regs.rt[0]) / sizeof(u32); + if (method_call.method >= first_rt_reg && + method_call.method < first_rt_reg + registers_per_rt * Regs::NumRenderTargets) { + const std::size_t rt_index = (method_call.method - first_rt_reg) / registers_per_rt; + dirty_flags.color_buffer |= 1u << static_cast(rt_index); + } + // Shader constexpr u32 shader_registers_count = sizeof(regs.shader_config[0]) * Regs::MaxShaderProgram / sizeof(u32); diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h index d50e5a126..8d0e18d80 100644 --- a/src/video_core/engines/maxwell_3d.h +++ b/src/video_core/engines/maxwell_3d.h @@ -1089,12 +1089,15 @@ public: MemoryManager& memory_manager; struct DirtyFlags { + u8 color_buffer = 0xFF; + bool shaders = true; bool vertex_attrib_format = true; u32 vertex_array = 0xFFFFFFFF; void OnMemoryWrite() { + color_buffer = 0xFF; shaders = true; vertex_array = 0xFFFFFFFF; } diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp index d3dcb9a46..0059b336a 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp @@ -933,21 +933,27 @@ Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) { } Surface RasterizerCacheOpenGL::GetColorBufferSurface(std::size_t index, bool preserve_contents) { - const auto& regs{Core::System::GetInstance().GPU().Maxwell3D().regs}; + auto& gpu{Core::System::GetInstance().GPU().Maxwell3D()}; + const auto& regs{gpu.regs}; + + if ((gpu.dirty_flags.color_buffer & (1u << static_cast(index))) == 0) { + return last_color_buffers[index]; + } + gpu.dirty_flags.color_buffer &= ~(1u << static_cast(index)); ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets); if (index >= regs.rt_control.count) { - return {}; + return last_color_buffers[index] = {}; } if (regs.rt[index].Address() == 0 || regs.rt[index].format == Tegra::RenderTargetFormat::NONE) { - return {}; + return last_color_buffers[index] = {}; } const SurfaceParams color_params{SurfaceParams::CreateForFramebuffer(index)}; - return GetSurface(color_params, preserve_contents); + return last_color_buffers[index] = GetSurface(color_params, preserve_contents); } void RasterizerCacheOpenGL::LoadSurface(const Surface& surface) { diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h index 7223700c4..3bdd141a7 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h @@ -396,6 +396,8 @@ private: /// Use a Pixel Buffer Object to download the previous texture and then upload it to the new one /// using the new format. OGLBuffer copy_pbo; + + std::array last_color_buffers; }; } // namespace OpenGL From b683e41fca8f88bf21085ccb3eee1dd6a91e293e Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Mon, 7 Jan 2019 02:09:39 -0300 Subject: [PATCH 2/3] gl_rasterizer_cache: Use dirty flags for the depth buffer --- src/video_core/engines/maxwell_3d.cpp | 10 ++++++++++ src/video_core/engines/maxwell_3d.h | 2 ++ .../renderer_opengl/gl_rasterizer_cache.cpp | 13 ++++++++++--- .../renderer_opengl/gl_rasterizer_cache.h | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp index 88483eab9..d64a5080b 100644 --- a/src/video_core/engines/maxwell_3d.cpp +++ b/src/video_core/engines/maxwell_3d.cpp @@ -144,6 +144,16 @@ void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) { dirty_flags.color_buffer |= 1u << static_cast(rt_index); } + // Zeta buffer + constexpr u32 registers_in_zeta = sizeof(regs.zeta) / sizeof(u32); + if (method_call.method == MAXWELL3D_REG_INDEX(zeta_enable) || + method_call.method == MAXWELL3D_REG_INDEX(zeta_width) || + method_call.method == MAXWELL3D_REG_INDEX(zeta_height) || + (method_call.method >= MAXWELL3D_REG_INDEX(zeta) && + method_call.method < MAXWELL3D_REG_INDEX(zeta) + registers_in_zeta)) { + dirty_flags.zeta_buffer = true; + } + // Shader constexpr u32 shader_registers_count = sizeof(regs.shader_config[0]) * Regs::MaxShaderProgram / sizeof(u32); diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h index 8d0e18d80..1f76aa670 100644 --- a/src/video_core/engines/maxwell_3d.h +++ b/src/video_core/engines/maxwell_3d.h @@ -1090,6 +1090,7 @@ public: struct DirtyFlags { u8 color_buffer = 0xFF; + bool zeta_buffer = true; bool shaders = true; @@ -1098,6 +1099,7 @@ public: void OnMemoryWrite() { color_buffer = 0xFF; + zeta_buffer = true; shaders = true; vertex_array = 0xFFFFFFFF; } diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp index 0059b336a..d26a5f4de 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp @@ -919,9 +919,16 @@ Surface RasterizerCacheOpenGL::GetTextureSurface(const Tegra::Texture::FullTextu } Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) { - const auto& regs{Core::System::GetInstance().GPU().Maxwell3D().regs}; + auto& gpu{Core::System::GetInstance().GPU().Maxwell3D()}; + const auto& regs{gpu.regs}; + + if (!gpu.dirty_flags.zeta_buffer) { + return last_depth_buffer; + } + gpu.dirty_flags.zeta_buffer = false; + if (!regs.zeta.Address() || !regs.zeta_enable) { - return {}; + return last_depth_buffer = {}; } SurfaceParams depth_params{SurfaceParams::CreateForDepthBuffer( @@ -929,7 +936,7 @@ Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) { regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height, regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)}; - return GetSurface(depth_params, preserve_contents); + return last_depth_buffer = GetSurface(depth_params, preserve_contents); } Surface RasterizerCacheOpenGL::GetColorBufferSurface(std::size_t index, bool preserve_contents) { diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h index 3bdd141a7..37611c4fc 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h @@ -398,6 +398,7 @@ private: OGLBuffer copy_pbo; std::array last_color_buffers; + Surface last_depth_buffer; }; } // namespace OpenGL From 19cf9952252fc0f9c31037676b56b6d7716ffd2f Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Mon, 7 Jan 2019 02:22:00 -0300 Subject: [PATCH 3/3] gl_rasterizer: Skip framebuffer configuration if rendertargets have not been changed --- .../renderer_opengl/gl_rasterizer.cpp | 14 +++++++++++++- src/video_core/renderer_opengl/gl_rasterizer.h | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 37f01d4f7..aa8dab0c2 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -488,7 +488,19 @@ void RasterizerOpenGL::ConfigureFramebuffers(OpenGLState& current_state, bool us bool using_depth_fb, bool preserve_contents, std::optional single_color_target) { MICROPROFILE_SCOPE(OpenGL_Framebuffer); - const auto& regs = Core::System::GetInstance().GPU().Maxwell3D().regs; + const auto& gpu = Core::System::GetInstance().GPU().Maxwell3D(); + const auto& regs = gpu.regs; + + const FramebufferConfigState fb_config_state{using_color_fb, using_depth_fb, preserve_contents, + single_color_target}; + if (fb_config_state == current_framebuffer_config_state && gpu.dirty_flags.color_buffer == 0 && + !gpu.dirty_flags.zeta_buffer) { + // Only skip if the previous ConfigureFramebuffers call was from the same kind (multiple or + // single color targets). This is done because the guest registers may not change but the + // host framebuffer may contain different attachments + return; + } + current_framebuffer_config_state = fb_config_state; Surface depth_surface; if (using_depth_fb) { diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index 8a891ffc7..45c988816 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -94,6 +94,23 @@ private: float max_anisotropic = 1.0f; }; + struct FramebufferConfigState { + bool using_color_fb{}; + bool using_depth_fb{}; + bool preserve_contents{}; + std::optional single_color_target; + + bool operator==(const FramebufferConfigState& rhs) const { + return std::tie(using_color_fb, using_depth_fb, preserve_contents, + single_color_target) == std::tie(rhs.using_color_fb, rhs.using_depth_fb, + rhs.preserve_contents, + rhs.single_color_target); + } + bool operator!=(const FramebufferConfigState& rhs) const { + return !operator==(rhs); + } + }; + /** * Configures the color and depth framebuffer states. * @param use_color_fb If true, configure color framebuffers. @@ -197,6 +214,7 @@ private: vertex_array_cache; std::map framebuffer_cache; + FramebufferConfigState current_framebuffer_config_state; std::array texture_samplers;