From e7db87818818f66550e0fb189461f14deeb73bdb Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 31 May 2022 19:29:40 +1000 Subject: [PATCH] GS: Clamp tex-in-rt source rectangle to rt bounds Fixes GPU crash for DX12 in Ridge Racer V. Game still has broken car textures. --- pcsx2/GS/Renderers/HW/GSTextureCache.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pcsx2/GS/Renderers/HW/GSTextureCache.cpp b/pcsx2/GS/Renderers/HW/GSTextureCache.cpp index bedecdacb4..e43fc85997 100644 --- a/pcsx2/GS/Renderers/HW/GSTextureCache.cpp +++ b/pcsx2/GS/Renderers/HW/GSTextureCache.cpp @@ -1313,17 +1313,23 @@ GSTextureCache::Source* GSTextureCache::CreateSource(const GIFRegTEX0& TEX0, con if (dst && (x_offset != 0 || y_offset != 0)) { - GSVector2 scale = dst->m_texture->GetScale(); - int x = (int)(scale.x * x_offset); - int y = (int)(scale.y * y_offset); - int w = (int)(scale.x * tw); - int h = (int)(scale.y * th); + const GSVector2 scale(dst->m_texture->GetScale()); + const int x = static_cast(scale.x * x_offset); + const int y = static_cast(scale.y * y_offset); + const int w = static_cast(scale.x * tw); + const int h = static_cast(scale.y * th); + // if we have a source larger than the target (from tex-in-rt), we need to clear it, otherwise we'll read junk + const bool outside_target = ((x + w) > dst->m_texture->GetWidth() || (y + h) > dst->m_texture->GetHeight()); GSTexture* sTex = dst->m_texture; - GSTexture* dTex = g_gs_device->CreateTexture(w, h, false, GSTexture::Format::Color, true); + GSTexture* dTex = outside_target ? + g_gs_device->CreateRenderTarget(w, h, GSTexture::Format::Color, true) : + g_gs_device->CreateTexture(w, h, false, GSTexture::Format::Color, true); - GSVector4i area(x, y, x + w, y + h); - g_gs_device->CopyRect(sTex, dTex, area, 0, 0); + // copy the rt in + const GSVector4i area(GSVector4i(x, y, x + w, y + h).rintersect(GSVector4i(sTex->GetSize()).zwxy())); + if (!area.rempty()) + g_gs_device->CopyRect(sTex, dTex, area, 0, 0); // Keep a trace of origin of the texture src->m_texture = dTex;