From 30b582021a7461c530908dfa697f63e28160eef9 Mon Sep 17 00:00:00 2001 From: Alan Tse Date: Fri, 18 Oct 2024 01:45:57 -0700 Subject: [PATCH] fix(VR): fix eye mismatch for screenspacegi --- package/Shaders/AmbientCompositeCS.hlsl | 21 +++++++++++++++++++-- package/Shaders/DeferredCompositeCS.hlsl | 21 +++++++++++++++++++-- src/Deferred.cpp | 16 ++++++++++++++-- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/package/Shaders/AmbientCompositeCS.hlsl b/package/Shaders/AmbientCompositeCS.hlsl index eaad02ada..1becc85d4 100644 --- a/package/Shaders/AmbientCompositeCS.hlsl +++ b/package/Shaders/AmbientCompositeCS.hlsl @@ -21,6 +21,10 @@ Texture2D DepthTexture : register(t2); Texture3D SkylightingProbeArray : register(t3); #endif +#if !defined(SKYLIGHTING) && defined(VR) // VR also needs a depthbuffer +Texture2D DepthTexture : register(t2); +#endif + #if defined(SSGI) Texture2D SSGITexture : register(t4); #endif @@ -34,8 +38,9 @@ RWTexture2D DiffuseAmbientRW : register(u1); [numthreads(8, 8, 1)] void main(uint3 dispatchID : SV_DispatchThreadID) { - half2 uv = half2(dispatchID.xy + 0.5) * BufferDim.zw * DynamicResolutionParams2.xy; + half2 uv = half2(dispatchID.xy + 0.5) * BufferDim.zw; uint eyeIndex = Stereo::GetEyeIndexFromTexCoord(uv); + uv *= DynamicResolutionParams2.xy; // adjust for dynamic res uv = Stereo::ConvertFromStereoUV(uv, eyeIndex); half3 normalGlossiness = NormalRoughnessTexture[dispatchID.xy]; @@ -64,7 +69,7 @@ RWTexture2D DiffuseAmbientRW : register(u1); float4 positionMS = mul(CameraViewProjInverse[eyeIndex], positionCS); positionMS.xyz = positionMS.xyz / positionMS.w; # if defined(VR) - positionMS.xyz += CameraPosAdjust[eyeIndex] - CameraPosAdjust[0]; + positionMS.xyz += CameraPosAdjust[eyeIndex].xyz - CameraPosAdjust[0].xyz; # endif sh2 skylighting = Skylighting::sample(skylightingSettings, SkylightingProbeArray, positionMS.xyz, normalWS); @@ -75,7 +80,19 @@ RWTexture2D DiffuseAmbientRW : register(u1); #endif #if defined(SSGI) +# if defined(VR) + float3 uvF = float3((dispatchID.xy + 0.5) * BufferDim.zw, DepthTexture[dispatchID.xy]); // calculate high precision uv of initial eye + float3 uv2 = Stereo::ConvertStereoUVToOtherEyeStereoUV(uvF, eyeIndex, false); // calculate other eye uv + float3 uv1Mono = Stereo::ConvertFromStereoUV(uvF, eyeIndex); + float3 uv2Mono = Stereo::ConvertFromStereoUV(uv2, (1 - eyeIndex)); + uint2 pixCoord2 = (uint2)(uv2.xy / BufferDim.zw - 0.5); +# endif + half4 ssgiDiffuse = SSGITexture[dispatchID.xy]; +# if defined(VR) + half4 ssgiDiffuse2 = SSGITexture[pixCoord2]; + ssgiDiffuse = Stereo::BlendEyeColors(uv1Mono, (float4)ssgiDiffuse, uv2Mono, (float4)ssgiDiffuse2); +# endif ssgiDiffuse.rgb *= linAlbedo; ssgiDiffuse.a = 1 - ssgiDiffuse.a; diff --git a/package/Shaders/DeferredCompositeCS.hlsl b/package/Shaders/DeferredCompositeCS.hlsl index 12639acaa..391bd66e4 100644 --- a/package/Shaders/DeferredCompositeCS.hlsl +++ b/package/Shaders/DeferredCompositeCS.hlsl @@ -24,6 +24,10 @@ TextureCube EnvReflectionsTexture : register(t8); SamplerState LinearSampler : register(s0); #endif +#if !defined(DYNAMIC_CUBEMAPS) && defined(VR) // VR also needs a depthbuffer +Texture2D DepthTexture : register(t5); +#endif + #if defined(SKYLIGHTING) # define SL_INCL_STRUCT # define SL_INCL_METHODS @@ -43,8 +47,9 @@ Texture2D SpecularSSGITexture : register(t10); [numthreads(8, 8, 1)] void main(uint3 dispatchID : SV_DispatchThreadID) { - half2 uv = half2(dispatchID.xy + 0.5) * BufferDim.zw * DynamicResolutionParams2.xy; + half2 uv = half2(dispatchID.xy + 0.5) * BufferDim.zw; uint eyeIndex = Stereo::GetEyeIndexFromTexCoord(uv); + uv *= DynamicResolutionParams2.xy; // adjust for dynamic res uv = Stereo::ConvertFromStereoUV(uv, eyeIndex); half3 normalGlossiness = NormalRoughnessTexture[dispatchID.xy]; @@ -99,7 +104,7 @@ Texture2D SpecularSSGITexture : register(t10); finalIrradiance += specularIrradiance; # elif defined(SKYLIGHTING) # if defined(VR) - float3 positionMS = positionWS + CameraPosAdjust[eyeIndex] - CameraPosAdjust[0]; + float3 positionMS = positionWS + CameraPosAdjust[eyeIndex].xyz - CameraPosAdjust[0].xyz; # else float3 positionMS = positionWS; # endif @@ -132,7 +137,19 @@ Texture2D SpecularSSGITexture : register(t10); # endif # if defined(SSGI) +# if defined(VR) + float3 uvF = float3((dispatchID.xy + 0.5) * BufferDim.zw, DepthTexture[dispatchID.xy]); // calculate high precision uv of initial eye + float3 uv2 = Stereo::ConvertStereoUVToOtherEyeStereoUV(uvF, eyeIndex, false); // calculate other eye uv + float3 uv1Mono = Stereo::ConvertFromStereoUV(uvF, eyeIndex); + float3 uv2Mono = Stereo::ConvertFromStereoUV(uv2, (1 - eyeIndex)); + uint2 pixCoord2 = (uint2)(uv2.xy / BufferDim.zw - 0.5); +# endif + half4 ssgiSpecular = SpecularSSGITexture[dispatchID.xy]; +# if defined(VR) + half4 ssgiSpecular2 = SpecularSSGITexture[pixCoord2]; + ssgiSpecular = Stereo::BlendEyeColors(uv1Mono, (float4)ssgiSpecular, uv2Mono, (float4)ssgiSpecular2); +# endif finalIrradiance = finalIrradiance * (1 - ssgiSpecular.a) + ssgiSpecular.rgb; # endif diff --git a/src/Deferred.cpp b/src/Deferred.cpp index d91167d78..a23244ca1 100644 --- a/src/Deferred.cpp +++ b/src/Deferred.cpp @@ -405,7 +405,7 @@ void Deferred::DeferredPasses() ID3D11ShaderResourceView* srvs[6]{ albedo.SRV, normalRoughness.SRV, - skylighting->loaded ? depth.depthSRV : nullptr, + skylighting->loaded || REL::Module::IsVR() ? depth.depthSRV : nullptr, skylighting->loaded ? skylighting->texProbeArray->srv.get() : nullptr, ssgi->settings.Enabled ? ssgi->texGI[ssgi->outputGIIdx]->srv.get() : nullptr, masks2.SRV, @@ -462,7 +462,7 @@ void Deferred::DeferredPasses() normalRoughness.SRV, masks.SRV, masks2.SRV, - dynamicCubemaps->loaded ? (terrainBlending->loaded ? terrainBlending->blendedDepthTexture16->srv.get() : depth.depthSRV) : nullptr, + dynamicCubemaps->loaded || REL::Module::IsVR() ? (terrainBlending->loaded ? terrainBlending->blendedDepthTexture16->srv.get() : depth.depthSRV) : nullptr, dynamicCubemaps->loaded ? reflectance.SRV : nullptr, dynamicCubemaps->loaded ? dynamicCubemaps->envTexture->srv.get() : nullptr, dynamicCubemaps->loaded ? dynamicCubemaps->envReflectionsTexture->srv.get() : nullptr, @@ -615,6 +615,9 @@ ID3D11ComputeShader* Deferred::GetComputeAmbientComposite() if (ScreenSpaceGI::GetSingleton()->loaded) defines.push_back({ "SSGI", nullptr }); + if (REL::Module::IsVR()) + defines.push_back({ "FRAMEBUFFER", nullptr }); + ambientCompositeCS = static_cast(Util::CompileShader(L"Data\\Shaders\\AmbientCompositeCS.hlsl", defines, "cs_5_0")); } return ambientCompositeCS; @@ -631,6 +634,9 @@ ID3D11ComputeShader* Deferred::GetComputeAmbientCompositeInterior() if (ScreenSpaceGI::GetSingleton()->loaded) defines.push_back({ "SSGI", nullptr }); + if (REL::Module::IsVR()) + defines.push_back({ "FRAMEBUFFER", nullptr }); + ambientCompositeInteriorCS = static_cast(Util::CompileShader(L"Data\\Shaders\\AmbientCompositeCS.hlsl", defines, "cs_5_0")); } return ambientCompositeInteriorCS; @@ -652,6 +658,9 @@ ID3D11ComputeShader* Deferred::GetComputeMainComposite() if (ScreenSpaceGI::GetSingleton()->loaded) defines.push_back({ "SSGI", nullptr }); + if (REL::Module::IsVR()) + defines.push_back({ "FRAMEBUFFER", nullptr }); + mainCompositeCS = static_cast(Util::CompileShader(L"Data\\Shaders\\DeferredCompositeCS.hlsl", defines, "cs_5_0")); } return mainCompositeCS; @@ -671,6 +680,9 @@ ID3D11ComputeShader* Deferred::GetComputeMainCompositeInterior() if (ScreenSpaceGI::GetSingleton()->loaded) defines.push_back({ "SSGI", nullptr }); + if (REL::Module::IsVR()) + defines.push_back({ "FRAMEBUFFER", nullptr }); + mainCompositeInteriorCS = static_cast(Util::CompileShader(L"Data\\Shaders\\DeferredCompositeCS.hlsl", defines, "cs_5_0")); } return mainCompositeInteriorCS;