From 5dc08c7fe0497fe20f485ba8100f7d96fb5135dc Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 25 Feb 2024 17:12:43 -0500 Subject: [PATCH] video_core: fix draw texture scaling and origin mode --- src/video_core/engines/draw_manager.cpp | 11 ++---- .../renderer_opengl/gl_rasterizer.cpp | 29 ++++++++------ .../renderer_vulkan/vk_rasterizer.cpp | 38 +++++++++++++------ .../renderer_vulkan/vk_texture_cache.cpp | 7 ++-- .../renderer_vulkan/vk_texture_cache.h | 5 +++ 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/video_core/engines/draw_manager.cpp b/src/video_core/engines/draw_manager.cpp index d77ff455b0..971025cb55 100644 --- a/src/video_core/engines/draw_manager.cpp +++ b/src/video_core/engines/draw_manager.cpp @@ -216,14 +216,11 @@ void DrawManager::DrawTexture() { const bool lower_left{regs.window_origin.mode != Maxwell3D::Regs::WindowOrigin::Mode::UpperLeft}; if (lower_left) { - draw_texture_state.dst_y0 -= dst_height; + draw_texture_state.dst_y0 = + static_cast(regs.surface_clip.height) - draw_texture_state.dst_y0; } - draw_texture_state.dst_x1 = - draw_texture_state.dst_x0 + - static_cast(Settings::values.resolution_info.ScaleUp(static_cast(dst_width))); - draw_texture_state.dst_y1 = - draw_texture_state.dst_y0 + - static_cast(Settings::values.resolution_info.ScaleUp(static_cast(dst_height))); + draw_texture_state.dst_x1 = draw_texture_state.dst_x0 + dst_width; + draw_texture_state.dst_y1 = draw_texture_state.dst_y0 + dst_height; draw_texture_state.src_x0 = static_cast(regs.draw_texture.src_x0) / 4096.f; draw_texture_state.src_y0 = static_cast(regs.draw_texture.src_y0) / 4096.f; draw_texture_state.src_x1 = diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 16af8e6bdc..d376d86d85 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -370,27 +370,32 @@ void RasterizerOpenGL::DrawTexture() { const auto& sampler = texture_cache.GetGraphicsSampler(draw_texture_state.src_sampler); const auto& texture = texture_cache.GetImageView(draw_texture_state.src_texture); + const auto Scale = [&](auto dim) -> s32 { + return Settings::values.resolution_info.ScaleUp(static_cast(dim)); + }; + + Region2D dst_region = { + Offset2D{.x = Scale(draw_texture_state.dst_x0), .y = Scale(draw_texture_state.dst_y0)}, + Offset2D{.x = Scale(draw_texture_state.dst_x1), .y = Scale(draw_texture_state.dst_y1)}}; + Region2D src_region = { + Offset2D{.x = Scale(draw_texture_state.src_x0), .y = Scale(draw_texture_state.src_y0)}, + Offset2D{.x = Scale(draw_texture_state.src_x1), .y = Scale(draw_texture_state.src_y1)}}; + Extent3D src_size = {static_cast(Scale(texture.size.width)), + static_cast(Scale(texture.size.height)), texture.size.depth}; + if (device.HasDrawTexture()) { state_tracker.BindFramebuffer(texture_cache.GetFramebuffer()->Handle()); - glDrawTextureNV(texture.DefaultHandle(), sampler->Handle(), draw_texture_state.dst_x0, - draw_texture_state.dst_y0, draw_texture_state.dst_x1, - draw_texture_state.dst_y1, 0, + glDrawTextureNV(texture.DefaultHandle(), sampler->Handle(), + static_cast(dst_region.start.x), static_cast(dst_region.start.y), + static_cast(dst_region.end.x), static_cast(dst_region.end.y), 0, draw_texture_state.src_x0 / static_cast(texture.size.width), draw_texture_state.src_y0 / static_cast(texture.size.height), draw_texture_state.src_x1 / static_cast(texture.size.width), draw_texture_state.src_y1 / static_cast(texture.size.height)); } else { - Region2D dst_region = {Offset2D{.x = static_cast(draw_texture_state.dst_x0), - .y = static_cast(draw_texture_state.dst_y0)}, - Offset2D{.x = static_cast(draw_texture_state.dst_x1), - .y = static_cast(draw_texture_state.dst_y1)}}; - Region2D src_region = {Offset2D{.x = static_cast(draw_texture_state.src_x0), - .y = static_cast(draw_texture_state.src_y0)}, - Offset2D{.x = static_cast(draw_texture_state.src_x1), - .y = static_cast(draw_texture_state.src_y1)}}; blit_image.BlitColor(texture_cache.GetFramebuffer()->Handle(), texture.DefaultHandle(), - sampler->Handle(), dst_region, src_region, texture.size); + sampler->Handle(), dst_region, src_region, src_size); state_tracker.InvalidateState(); } diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp index 35679d65e6..2c24b0613e 100644 --- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp +++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp @@ -320,17 +320,33 @@ void RasterizerVulkan::DrawTexture() { const auto& draw_texture_state = maxwell3d->draw_manager->GetDrawTextureState(); const auto& sampler = texture_cache.GetGraphicsSampler(draw_texture_state.src_sampler); const auto& texture = texture_cache.GetImageView(draw_texture_state.src_texture); - Region2D dst_region = {Offset2D{.x = static_cast(draw_texture_state.dst_x0), - .y = static_cast(draw_texture_state.dst_y0)}, - Offset2D{.x = static_cast(draw_texture_state.dst_x1), - .y = static_cast(draw_texture_state.dst_y1)}}; - Region2D src_region = {Offset2D{.x = static_cast(draw_texture_state.src_x0), - .y = static_cast(draw_texture_state.src_y0)}, - Offset2D{.x = static_cast(draw_texture_state.src_x1), - .y = static_cast(draw_texture_state.src_y1)}}; - blit_image.BlitColor(texture_cache.GetFramebuffer(), texture.RenderTarget(), - texture.ImageHandle(), sampler->Handle(), dst_region, src_region, - texture.size); + const auto* framebuffer = texture_cache.GetFramebuffer(); + + const bool src_rescaling = texture_cache.IsRescaling() && texture.IsRescaled(); + const bool dst_rescaling = texture_cache.IsRescaling() && framebuffer->IsRescaled(); + + const auto ScaleSrc = [&](auto dim_f) -> s32 { + auto dim = static_cast(dim_f); + return src_rescaling ? Settings::values.resolution_info.ScaleUp(dim) : dim; + }; + + const auto ScaleDst = [&](auto dim_f) -> s32 { + auto dim = static_cast(dim_f); + return dst_rescaling ? Settings::values.resolution_info.ScaleUp(dim) : dim; + }; + + Region2D dst_region = {Offset2D{.x = ScaleDst(draw_texture_state.dst_x0), + .y = ScaleDst(draw_texture_state.dst_y0)}, + Offset2D{.x = ScaleDst(draw_texture_state.dst_x1), + .y = ScaleDst(draw_texture_state.dst_y1)}}; + Region2D src_region = {Offset2D{.x = ScaleSrc(draw_texture_state.src_x0), + .y = ScaleSrc(draw_texture_state.src_y0)}, + Offset2D{.x = ScaleSrc(draw_texture_state.src_x1), + .y = ScaleSrc(draw_texture_state.src_y1)}}; + Extent3D src_size = {static_cast(ScaleSrc(texture.size.width)), + static_cast(ScaleSrc(texture.size.height)), texture.size.depth}; + blit_image.BlitColor(framebuffer, texture.RenderTarget(), texture.ImageHandle(), + sampler->Handle(), dst_region, src_region, src_size); } void RasterizerVulkan::Clear(u32 layer_count) { diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.cpp b/src/video_core/renderer_vulkan/vk_texture_cache.cpp index 832b5e2b17..6d4deb0ebf 100644 --- a/src/video_core/renderer_vulkan/vk_texture_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_texture_cache.cpp @@ -1962,21 +1962,22 @@ Framebuffer::Framebuffer(TextureCacheRuntime& runtime, std::span color_buffers{color_buffer}; - CreateFramebuffer(runtime, color_buffers, depth_buffer, is_rescaled); + CreateFramebuffer(runtime, color_buffers, depth_buffer, is_rescaled_); } Framebuffer::~Framebuffer() = default; void Framebuffer::CreateFramebuffer(TextureCacheRuntime& runtime, std::span color_buffers, - ImageView* depth_buffer, bool is_rescaled) { + ImageView* depth_buffer, bool is_rescaled_) { boost::container::small_vector attachments; RenderPassKey renderpass_key{}; s32 num_layers = 1; + is_rescaled = is_rescaled_; const auto& resolution = runtime.resolution; u32 width = std::numeric_limits::max(); diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.h b/src/video_core/renderer_vulkan/vk_texture_cache.h index aaeb5ef934..8501ec384b 100644 --- a/src/video_core/renderer_vulkan/vk_texture_cache.h +++ b/src/video_core/renderer_vulkan/vk_texture_cache.h @@ -361,6 +361,10 @@ public: return has_stencil; } + [[nodiscard]] bool IsRescaled() const noexcept { + return is_rescaled; + } + private: vk::Framebuffer framebuffer; VkRenderPass renderpass{}; @@ -373,6 +377,7 @@ private: std::array rt_map{}; bool has_depth{}; bool has_stencil{}; + bool is_rescaled{}; }; struct TextureCacheParams {