From 27b78122151df92e8257db825ab40e8ac6ea6a43 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sat, 7 Dec 2024 21:40:29 +0300 Subject: [PATCH] Initial DoF fix experiments. --- UnleashedRecomp/CMakeLists.txt | 1 + .../gpu/shader/gaussian_blur_ps.hlsl | 60 +++++++++++++++++++ UnleashedRecomp/gpu/video.cpp | 12 ++++ 3 files changed, 73 insertions(+) create mode 100644 UnleashedRecomp/gpu/shader/gaussian_blur_ps.hlsl diff --git a/UnleashedRecomp/CMakeLists.txt b/UnleashedRecomp/CMakeLists.txt index 1ad18556..c268677a 100644 --- a/UnleashedRecomp/CMakeLists.txt +++ b/UnleashedRecomp/CMakeLists.txt @@ -284,6 +284,7 @@ function(compile_pixel_shader FILE_PATH) endfunction() compile_vertex_shader(copy_vs) +compile_pixel_shader(gaussian_blur_ps) compile_pixel_shader(gamma_correction_ps) compile_pixel_shader(imgui_ps) compile_vertex_shader(imgui_vs) diff --git a/UnleashedRecomp/gpu/shader/gaussian_blur_ps.hlsl b/UnleashedRecomp/gpu/shader/gaussian_blur_ps.hlsl new file mode 100644 index 00000000..e9303a03 --- /dev/null +++ b/UnleashedRecomp/gpu/shader/gaussian_blur_ps.hlsl @@ -0,0 +1,60 @@ +#include "../../../thirdparty/ShaderRecomp/ShaderRecomp/shader_common.h" + +#ifdef __spirv__ + +#define g_ViewportSize vk::RawBufferLoad(g_PushConstants.PixelShaderConstants + 384, 0x10) +#define g_offsets(INDEX) select((INDEX) < 74, vk::RawBufferLoad(g_PushConstants.PixelShaderConstants + (150 + min(INDEX, 73)) * 16, 0x10), 0.0) +#define g_weights vk::RawBufferLoad(g_PushConstants.PixelShaderConstants + 2656, 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_ViewportSize : packoffset(c24); + float4 g_offsets[2] : packoffset(c150); +#define g_offsets(INDEX) select((INDEX) < 74, g_offsets[min(INDEX, 73)], 0.0) + float4 g_weights : packoffset(c166); +}; + +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 iPosition : SV_Position, in float4 iTexCoord0 : TEXCOORD0) : SV_Target +{ + Texture2D texture = g_Texture2DDescriptorHeap[s0_Texture2DDescriptorIndex]; + SamplerState samplerState = g_SamplerDescriptorHeap[s0_SamplerDescriptorIndex]; + + float scaleFactor = g_ViewportSize.y / 360.0; + + float2 offsets[5]; + offsets[0] = g_offsets(0).xy * scaleFactor; + offsets[2] = g_offsets(0).zw * scaleFactor; + offsets[4] = g_offsets(1).xy * scaleFactor; + + offsets[1] = lerp(offsets[2], offsets[0], 0.5); + offsets[3] = lerp(offsets[2], offsets[4], 0.5); + + float weights[5]; + weights[0] = 0.1131226076; + weights[1] = 0.2360540033; + weights[2] = 0.3016467782; + weights[3] = 0.2360540033; + weights[4] = 0.1131226076; + + float4 c0 = texture.Sample(samplerState, iTexCoord0.xy + offsets[0]) * weights[0]; + float4 c1 = texture.Sample(samplerState, iTexCoord0.xy + offsets[1]) * weights[1]; + float4 c2 = texture.Sample(samplerState, iTexCoord0.xy + offsets[2]) * weights[2]; + float4 c3 = texture.Sample(samplerState, iTexCoord0.xy + offsets[3]) * weights[3]; + float4 c4 = texture.Sample(samplerState, iTexCoord0.xy + offsets[4]) * weights[4]; + + return c0 + c1 + c2 + c3 + c4; +} diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index 24970e22..31a8691b 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -34,6 +34,8 @@ #include "../../thirdparty/ShaderRecomp/ShaderRecomp/shader_common.h" #include "shader/copy_vs.hlsl.dxil.h" #include "shader/copy_vs.hlsl.spirv.h" +#include "shader/gaussian_blur_ps.hlsl.dxil.h" +#include "shader/gaussian_blur_ps.hlsl.spirv.h" #include "shader/gamma_correction_ps.hlsl.dxil.h" #include "shader/gamma_correction_ps.hlsl.spirv.h" #include "shader/imgui_ps.hlsl.dxil.h" @@ -1024,6 +1026,7 @@ static const std::pair g_setRenderStateFunctions[] = }; static std::unique_ptr g_resolveMsaaDepthPipelines[3]; +static std::unique_ptr g_gaussianBlurShader; #define CREATE_SHADER(NAME) \ g_device->createShader( \ @@ -1416,6 +1419,9 @@ void Video::CreateHostDevice() g_resolveMsaaDepthPipelines[i] = g_device->createGraphicsPipeline(desc); } + g_gaussianBlurShader = std::make_unique(ResourceType::PixelShader); + g_gaussianBlurShader->shader = CREATE_SHADER(gaussian_blur_ps); + CreateImGuiBackend(); auto gammaCorrectionShader = CREATE_SHADER(gamma_correction_ps); @@ -3836,6 +3842,12 @@ static GuestShader* CreatePixelShader(const be* function) static void SetPixelShader(GuestDevice* device, GuestShader* shader) { + if (shader != nullptr && shader->shaderCacheEntry != nullptr && shader->shaderCacheEntry->hash == 0x4294510C775F4EE8) + { + if (!(GetAsyncKeyState(VK_F4) & 0x8000)) + shader = g_gaussianBlurShader.get(); + } + RenderCommand cmd; cmd.type = RenderCommandType::SetPixelShader; cmd.setPixelShader.shader = shader;