From 3c64f0c6165f7f998952c9a4829970b143f930cb Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 28 Apr 2019 16:53:16 +1000 Subject: [PATCH] Renderer: Adjust source rectangle when crop would draw off screen This prevents us from requiring an oversized and/or negative viewport by shrinking the source rectangle instead. --- Source/Core/VideoBackends/D3D/Render.cpp | 8 +++-- Source/Core/VideoBackends/OGL/Render.cpp | 8 +++-- Source/Core/VideoCommon/RenderBase.cpp | 44 +++++++++++++++++++++--- Source/Core/VideoCommon/RenderBase.h | 7 ++++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/Render.cpp b/Source/Core/VideoBackends/D3D/Render.cpp index fc417962a0..4b27358e61 100644 --- a/Source/Core/VideoBackends/D3D/Render.cpp +++ b/Source/Core/VideoBackends/D3D/Render.cpp @@ -338,12 +338,14 @@ void Renderer::RenderXFBToScreen(const AbstractTexture* texture, const MathUtil: // Render to staging texture which is double the width of the backbuffer SetAndClearFramebuffer(m_3d_vision_framebuffer.get()); - const auto target_rc = GetTargetRectangle(); - m_post_processor->BlitFromTexture(target_rc, rc, texture, 0); + auto adjusted_rc = rc; + auto target_rc = GetTargetRectangle(); + AdjustRectanglesToFitBounds(&target_rc, &adjusted_rc, m_backbuffer_width, m_backbuffer_height); + m_post_processor->BlitFromTexture(target_rc, adjusted_rc, texture, 0); m_post_processor->BlitFromTexture( MathUtil::Rectangle(target_rc.left + m_backbuffer_width, target_rc.top, target_rc.right + m_backbuffer_width, target_rc.bottom), - rc, texture, 1); + adjusted_rc, texture, 1); // Copy the left eye to the backbuffer, if Nvidia 3D Vision is enabled it should // recognize the signature and automatically include the right eye frame. diff --git a/Source/Core/VideoBackends/OGL/Render.cpp b/Source/Core/VideoBackends/OGL/Render.cpp index 546556c440..152b192abe 100644 --- a/Source/Core/VideoBackends/OGL/Render.cpp +++ b/Source/Core/VideoBackends/OGL/Render.cpp @@ -984,13 +984,15 @@ void Renderer::RenderXFBToScreen(const AbstractTexture* texture, const MathUtil: if (g_ActiveConfig.stereo_mode != StereoMode::QuadBuffer) return ::Renderer::RenderXFBToScreen(texture, rc); - const auto target_rc = GetTargetRectangle(); + auto adjusted_rc = rc; + auto target_rc = GetTargetRectangle(); + AdjustRectanglesToFitBounds(&target_rc, &adjusted_rc, m_backbuffer_width, m_backbuffer_height); glDrawBuffer(GL_BACK_LEFT); - m_post_processor->BlitFromTexture(target_rc, rc, texture, 0); + m_post_processor->BlitFromTexture(target_rc, adjusted_rc, texture, 0); glDrawBuffer(GL_BACK_RIGHT); - m_post_processor->BlitFromTexture(target_rc, rc, texture, 1); + m_post_processor->BlitFromTexture(target_rc, adjusted_rc, texture, 1); glDrawBuffer(GL_BACK); } diff --git a/Source/Core/VideoCommon/RenderBase.cpp b/Source/Core/VideoCommon/RenderBase.cpp index 523a681884..039da1f189 100644 --- a/Source/Core/VideoCommon/RenderBase.cpp +++ b/Source/Core/VideoCommon/RenderBase.cpp @@ -561,6 +561,40 @@ float Renderer::CalculateDrawAspectRatio() const } } +void Renderer::AdjustRectanglesToFitBounds(MathUtil::Rectangle* target_rect, + MathUtil::Rectangle* source_rect, int fb_width, + int fb_height) +{ + const int orig_target_width = target_rect->GetWidth(); + const int orig_target_height = target_rect->GetHeight(); + const int orig_source_width = source_rect->GetWidth(); + const int orig_source_height = source_rect->GetHeight(); + if (target_rect->left < 0) + { + const int offset = -target_rect->left; + target_rect->left = 0; + source_rect->left += offset * orig_source_width / orig_target_width; + } + if (target_rect->right > fb_width) + { + const int offset = target_rect->right - fb_width; + target_rect->right -= offset; + source_rect->right -= offset * orig_source_width / orig_target_width; + } + if (target_rect->top < 0) + { + const int offset = -target_rect->top; + target_rect->top = 0; + source_rect->top += offset * orig_source_height / orig_target_height; + } + if (target_rect->bottom > fb_height) + { + const int offset = target_rect->bottom - fb_height; + target_rect->right -= offset; + source_rect->right -= offset * orig_source_height / orig_target_height; + } +} + bool Renderer::IsHeadless() const { return true; @@ -1287,19 +1321,21 @@ void Renderer::Swap(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height, u6 void Renderer::RenderXFBToScreen(const AbstractTexture* texture, const MathUtil::Rectangle& rc) { - const auto target_rc = GetTargetRectangle(); + auto adjusted_rc = rc; + auto target_rc = GetTargetRectangle(); + AdjustRectanglesToFitBounds(&target_rc, &adjusted_rc, m_backbuffer_width, m_backbuffer_height); if (g_ActiveConfig.stereo_mode == StereoMode::SBS || g_ActiveConfig.stereo_mode == StereoMode::TAB) { MathUtil::Rectangle left_rc, right_rc; std::tie(left_rc, right_rc) = ConvertStereoRectangle(target_rc); - m_post_processor->BlitFromTexture(left_rc, rc, texture, 0); - m_post_processor->BlitFromTexture(right_rc, rc, texture, 1); + m_post_processor->BlitFromTexture(left_rc, adjusted_rc, texture, 0); + m_post_processor->BlitFromTexture(right_rc, adjusted_rc, texture, 1); } else { - m_post_processor->BlitFromTexture(target_rc, rc, texture, 0); + m_post_processor->BlitFromTexture(target_rc, adjusted_rc, texture, 0); } } diff --git a/Source/Core/VideoCommon/RenderBase.h b/Source/Core/VideoCommon/RenderBase.h index c10d8d155b..ce31059ee2 100644 --- a/Source/Core/VideoCommon/RenderBase.h +++ b/Source/Core/VideoCommon/RenderBase.h @@ -171,6 +171,13 @@ public: const MathUtil::Rectangle& GetTargetRectangle() const { return m_target_rectangle; } float CalculateDrawAspectRatio() const; + // Crops the target rectangle to the framebuffer dimensions, reducing the size of the source + // rectangle if it is greater. Works even if the source and target rectangles don't have a + // 1:1 pixel mapping, scaling as appropriate. + void AdjustRectanglesToFitBounds(MathUtil::Rectangle* target_rect, + MathUtil::Rectangle* source_rect, int fb_width, + int fb_height); + std::tuple ScaleToDisplayAspectRatio(int width, int height) const; void UpdateDrawRectangle();