From 3d30b4f283a1cb1348b36507717f435c30b955ad Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sun, 26 Jan 2025 02:40:24 +0300 Subject: [PATCH] Implement aspect ratio adjustments for texture/movie overlays. --- UnleashedRecomp/CMakeLists.txt | 1 + .../gpu/shader/blend_color_alpha_ps.hlsl | 41 +++++++++++++ .../gpu/shader/enhanced_motion_blur_ps.hlsl | 10 ++-- UnleashedRecomp/gpu/video.cpp | 6 +- .../patches/aspect_ratio_patches.cpp | 59 ++++++++++++++++--- UnleashedRecompLib/config/SWA.toml | 13 +++- 6 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 UnleashedRecomp/gpu/shader/blend_color_alpha_ps.hlsl diff --git a/UnleashedRecomp/CMakeLists.txt b/UnleashedRecomp/CMakeLists.txt index 5136da44..de1b159f 100644 --- a/UnleashedRecomp/CMakeLists.txt +++ b/UnleashedRecomp/CMakeLists.txt @@ -361,6 +361,7 @@ function(compile_pixel_shader FILE_PATH) compile_shader(${FILE_PATH} ps_6_0) endfunction() +compile_pixel_shader(blend_color_alpha_ps) compile_vertex_shader(copy_vs) compile_pixel_shader(csd_filter_ps) compile_vertex_shader(csd_no_tex_vs) diff --git a/UnleashedRecomp/gpu/shader/blend_color_alpha_ps.hlsl b/UnleashedRecomp/gpu/shader/blend_color_alpha_ps.hlsl new file mode 100644 index 00000000..ea9c63b1 --- /dev/null +++ b/UnleashedRecomp/gpu/shader/blend_color_alpha_ps.hlsl @@ -0,0 +1,41 @@ +#include "../../../tools/XenosRecomp/XenosRecomp/shader_common.h" + +#ifdef __spirv__ + +#define g_SrcAlpha_DestAlpha vk::RawBufferLoad(g_PushConstants.PixelShaderConstants + 2400, 0x10) +#define s0_Texture2DDescriptorIndex vk::RawBufferLoad(g_PushConstants.SharedConstants + 0) +#define s0_SamplerDescriptorIndex vk::RawBufferLoad(g_PushConstants.SharedConstants + 192) + +#else + +cbuffer PixelShaderConstants : register(b1, space4) +{ + float4 g_SrcAlpha_DestAlpha : packoffset(c150); +}; + +cbuffer SharedConstants : register(b2, space4) +{ + uint s0_Texture2DDescriptorIndex : packoffset(c0.x); + uint s0_SamplerDescriptorIndex : packoffset(c12.x); + DEFINE_SHARED_CONSTANTS(); +}; + +#endif + +float4 main( + in float4 iPos : SV_Position, + in float4 iTexCoord0 : TEXCOORD0) : SV_Target0 +{ + Texture2D texture = g_Texture2DDescriptorHeap[s0_Texture2DDescriptorIndex]; + SamplerState samplerState = g_SamplerDescriptorHeap[s0_SamplerDescriptorIndex]; + + float4 color = texture.Sample(samplerState, iTexCoord0.xy); + + if (any(or(iTexCoord0.xy < 0.0, iTexCoord0.xy > 1.0))) + color = float4(0.0, 0.0, 0.0, 1.0); + + color.rgb *= color.a * g_SrcAlpha_DestAlpha.x; + color.a = g_SrcAlpha_DestAlpha.y + (1.0 - color.a) * g_SrcAlpha_DestAlpha.x; + + return color; +} diff --git a/UnleashedRecomp/gpu/shader/enhanced_motion_blur_ps.hlsl b/UnleashedRecomp/gpu/shader/enhanced_motion_blur_ps.hlsl index df707e7e..fd522da7 100644 --- a/UnleashedRecomp/gpu/shader/enhanced_motion_blur_ps.hlsl +++ b/UnleashedRecomp/gpu/shader/enhanced_motion_blur_ps.hlsl @@ -38,7 +38,7 @@ cbuffer SharedConstants : register(b2, space4) #endif -float4 main(in float4 position : SV_Position, in float2 texCoord : TEXCOORD0) : SV_Target +float4 main(in float4 position : SV_Position, in float4 texCoord : TEXCOORD0) : SV_Target { Texture2D sampColor = g_Texture2DDescriptorHeap[sampColor_Texture2DDescriptorIndex]; Texture2D sampVelocityMap = g_Texture2DDescriptorHeap[sampVelocityMap_Texture2DDescriptorIndex]; @@ -48,19 +48,19 @@ float4 main(in float4 position : SV_Position, in float2 texCoord : TEXCOORD0) : SamplerState sampVelocityMap_s = g_SamplerDescriptorHeap[sampVelocityMap_SamplerDescriptorIndex]; SamplerState sampZBuffer_s = g_SamplerDescriptorHeap[sampZBuffer_SamplerDescriptorIndex]; - float depth = sampZBuffer.SampleLevel(sampZBuffer_s, texCoord, 0).x; - float4 velocityMap = sampVelocityMap.SampleLevel(sampVelocityMap_s, texCoord, 0); + float depth = sampZBuffer.SampleLevel(sampZBuffer_s, texCoord.xy, 0).x; + float4 velocityMap = sampVelocityMap.SampleLevel(sampVelocityMap_s, texCoord.xy, 0); float2 velocity = (velocityMap.xz + velocityMap.yw / 255.0) * 2.0 - 1.0; int sampleCount = min(64, round(length(velocity * g_ViewportSize.xy))); float2 sampleOffset = velocity / (float) sampleCount; - float3 color = sampColor.SampleLevel(sampColor_s, texCoord, 0).rgb; + float3 color = sampColor.SampleLevel(sampColor_s, texCoord.xy, 0).rgb; int count = 1; for (int i = 1; i <= sampleCount; i++) { - float2 sampleCoord = texCoord + sampleOffset * i; + float2 sampleCoord = texCoord.xy + sampleOffset * i; float3 sampleColor = sampColor.SampleLevel(sampColor_s, sampleCoord, 0).rgb; float sampleDepth = sampZBuffer.SampleLevel(sampZBuffer_s, sampleCoord, 0).x; diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index d26bbfd0..f6814c72 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -37,6 +37,7 @@ #include "../../tools/XenosRecomp/XenosRecomp/shader_common.h" #ifdef UNLEASHED_RECOMP_D3D12 +#include "shader/blend_color_alpha_ps.hlsl.dxil.h" #include "shader/copy_vs.hlsl.dxil.h" #include "shader/csd_filter_ps.hlsl.dxil.h" #include "shader/csd_no_tex_vs.hlsl.dxil.h" @@ -56,6 +57,7 @@ #include "shader/resolve_msaa_depth_8x.hlsl.dxil.h" #endif +#include "shader/blend_color_alpha_ps.hlsl.spirv.h" #include "shader/copy_vs.hlsl.spirv.h" #include "shader/csd_filter_ps.hlsl.spirv.h" #include "shader/csd_no_tex_vs.hlsl.spirv.h" @@ -4233,7 +4235,9 @@ static GuestShader* CreateShader(const be* function, ResourceType reso { shader = g_userHeap.AllocPhysical(resourceType); - if (hash == 0xB1086A4947A797DE) + if (hash == 0x85ED723035ECF535) + shader->shader = CREATE_SHADER(blend_color_alpha_ps); + else if (hash == 0xB1086A4947A797DE) shader->shader = CREATE_SHADER(csd_no_tex_vs); else if (hash == 0xB4CAFC034A37C8A8) shader->shader = CREATE_SHADER(csd_vs); diff --git a/UnleashedRecomp/patches/aspect_ratio_patches.cpp b/UnleashedRecomp/patches/aspect_ratio_patches.cpp index b778fb89..a6c43c22 100644 --- a/UnleashedRecomp/patches/aspect_ratio_patches.cpp +++ b/UnleashedRecomp/patches/aspect_ratio_patches.cpp @@ -1250,22 +1250,63 @@ void InspireSubtitleMidAsmHook(PPCRegister& r3) *reinterpret_cast*>(g_memory.base + r3.u32 + 0x3C) = NARROW_OFFSET + (WIDE_OFFSET - NARROW_OFFSET) * g_aspectRatioNarrowScale; } -void FxFadeRenderQuadMidAsmHook(PPCRegister& r3, PPCRegister& r5, PPCRegister& r31) +enum class FadeTextureMode { - auto texCoord = reinterpret_cast*>(g_memory.base + r5.u32); + Unknown, + Letterbox, + SideCrop +}; - // Fade textures are slightly squashed in the original game at 4:3. - if (Config::AspectRatio == EAspectRatio::OriginalNarrow) +static FadeTextureMode g_fadeTextureMode; + +void FxFadePreRenderQuadMidAsmHook(PPCRegister& r31) +{ + g_fadeTextureMode = *(g_memory.base + r31.u32 + 0x44) ? FadeTextureMode::Letterbox : FadeTextureMode::SideCrop; +} + +void FxFadePostRenderQuadMidAsmHook() +{ + g_fadeTextureMode = FadeTextureMode::Unknown; +} + +void YggdrasillRenderQuadMidAsmHook(PPCRegister& r3, PPCRegister& r6) +{ + if (g_fadeTextureMode != FadeTextureMode::Unknown) { - if (*(g_memory.base + r31.u32 + 0x44)) + float scaleX = 1.0f; + float scaleY = 1.0f; + + // Fade textures are slightly squashed in the original game at 4:3. + if (Config::AspectRatio == EAspectRatio::OriginalNarrow) { - texCoord[1] = 0.0f + 0.125f; - texCoord[3] = 1.0f - 0.125f; + if (g_fadeTextureMode == FadeTextureMode::Letterbox) + scaleY = 1.25f; + else + scaleX = 0.8f; } else { - texCoord[0] = 0.0f - 0.125f; - texCoord[2] = 1.0f + 0.125f; + if (g_fadeTextureMode == FadeTextureMode::Letterbox && g_aspectRatio < WIDE_ASPECT_RATIO) + scaleY = WIDE_ASPECT_RATIO / g_aspectRatio; + else + scaleX = g_aspectRatio / WIDE_ASPECT_RATIO; + } + + struct Vertex + { + be x; + be y; + be z; + be u; + be v; + }; + + auto vertex = reinterpret_cast(g_memory.base + r6.u32); + + for (size_t i = 0; i < 6; i++) + { + vertex[i].u = (vertex[i].u - 0.5f) * scaleX + 0.5f; + vertex[i].v = (vertex[i].v - 0.5f) * scaleY + 0.5f; } } } diff --git a/UnleashedRecompLib/config/SWA.toml b/UnleashedRecompLib/config/SWA.toml index aafcfc11..7003028b 100644 --- a/UnleashedRecompLib/config/SWA.toml +++ b/UnleashedRecompLib/config/SWA.toml @@ -816,6 +816,15 @@ address = 0x824DB734 jump_address_on_true = 0x824DB738 [[midasm_hook]] -name = "FxFadeRenderQuadMidAsmHook" +name = "FxFadePreRenderQuadMidAsmHook" address = 0x82BA7D3C -registers = ["r3", "r5", "r31"] +registers = ["r31"] + +[[midasm_hook]] +name = "FxFadePostRenderQuadMidAsmHook" +address = 0x82BA7D40 + +[[midasm_hook]] +name = "YggdrasillRenderQuadMidAsmHook" +address = 0x82E9FBA0 +registers = ["r3", "r6"]