Skip to content

Commit

Permalink
fix(VR): fix eye mismatch for screenspacegi
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Oct 19, 2024
1 parent 5a4bf1b commit 30b5820
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
21 changes: 19 additions & 2 deletions package/Shaders/AmbientCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Texture2D<unorm float> DepthTexture : register(t2);
Texture3D<sh2> SkylightingProbeArray : register(t3);
#endif

#if !defined(SKYLIGHTING) && defined(VR) // VR also needs a depthbuffer
Texture2D<unorm float> DepthTexture : register(t2);
#endif

#if defined(SSGI)
Texture2D<half4> SSGITexture : register(t4);
#endif
Expand All @@ -34,8 +38,9 @@ RWTexture2D<half3> 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];
Expand Down Expand Up @@ -64,7 +69,7 @@ RWTexture2D<half3> 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);
Expand All @@ -75,7 +80,19 @@ RWTexture2D<half3> 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;

Expand Down
21 changes: 19 additions & 2 deletions package/Shaders/DeferredCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ TextureCube<half3> EnvReflectionsTexture : register(t8);
SamplerState LinearSampler : register(s0);
#endif

#if !defined(DYNAMIC_CUBEMAPS) && defined(VR) // VR also needs a depthbuffer
Texture2D<float> DepthTexture : register(t5);
#endif

#if defined(SKYLIGHTING)
# define SL_INCL_STRUCT
# define SL_INCL_METHODS
Expand All @@ -43,8 +47,9 @@ Texture2D<half4> 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];
Expand Down Expand Up @@ -99,7 +104,7 @@ Texture2D<half4> 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
Expand Down Expand Up @@ -132,7 +137,19 @@ Texture2D<half4> 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

Expand Down
16 changes: 14 additions & 2 deletions src/Deferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<ID3D11ComputeShader*>(Util::CompileShader(L"Data\\Shaders\\AmbientCompositeCS.hlsl", defines, "cs_5_0"));
}
return ambientCompositeCS;
Expand All @@ -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<ID3D11ComputeShader*>(Util::CompileShader(L"Data\\Shaders\\AmbientCompositeCS.hlsl", defines, "cs_5_0"));
}
return ambientCompositeInteriorCS;
Expand All @@ -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<ID3D11ComputeShader*>(Util::CompileShader(L"Data\\Shaders\\DeferredCompositeCS.hlsl", defines, "cs_5_0"));
}
return mainCompositeCS;
Expand All @@ -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<ID3D11ComputeShader*>(Util::CompileShader(L"Data\\Shaders\\DeferredCompositeCS.hlsl", defines, "cs_5_0"));
}
return mainCompositeInteriorCS;
Expand Down

0 comments on commit 30b5820

Please sign in to comment.