Skip to content

Commit

Permalink
SSR: Fixed broken reflections on OpenGL (close #151)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailGorobets authored and TheMostDiligent committed Apr 5, 2024
1 parent c0bfd10 commit 61663c9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
5 changes: 2 additions & 3 deletions Shaders/Common/private/ComputeReprojectedDepth.fx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Texture2D<float> g_TextureDepth;

float SampleDepth(int2 PixelCoord)
{
return DepthToNormalizedDeviceZ(g_TextureDepth.Load(int3(PixelCoord, 0)));
return g_TextureDepth.Load(int3(PixelCoord, 0));
}

float ComputeReprojectedDepthPS(in FullScreenTriangleVSOutput VSOut) : SV_Target0
Expand All @@ -26,6 +26,5 @@ float ComputeReprojectedDepthPS(in FullScreenTriangleVSOutput VSOut) : SV_Target
float3 WorldPosition = InvProjectPosition(CurrScreenCoord, g_CurrCamera.mViewProjInv);
float3 PrevScreenCoord = ProjectPosition(WorldPosition, g_PrevCamera.mViewProj);

return NormalizedDeviceZToDepth(PrevScreenCoord.z);

return PrevScreenCoord.z;
}
4 changes: 3 additions & 1 deletion Shaders/Common/public/PostFX_Common.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ float3 ProjectPosition(float3 Origin, float4x4 Transform)
float4 Projected = mul(float4(Origin, 1.0), Transform);
Projected.xyz /= Projected.w;
Projected.xy = NormalizedDeviceXYToTexUV(Projected.xy);
Projected.z = NormalizedDeviceZToDepth(Projected.z);
return Projected.xyz;
}

Expand All @@ -96,6 +97,7 @@ float3 ProjectDirection(float3 Origin, float3 Direction, float3 OriginSS, float4
float3 InvProjectPosition(float3 Coord, float4x4 Transform)
{
Coord.xy = TexUVToNormalizedDeviceXY(Coord.xy);
Coord.z = DepthToNormalizedDeviceZ(Coord.z);
float4 Projected = mul(float4(Coord, 1.0), Transform);
return Projected.xyz /= Projected.w;
}
Expand All @@ -120,7 +122,7 @@ float CameraZToDepth(in float fDepth, in float4x4 mProj)
return NormalizedDeviceZToDepth(z);
}

float3 FastReconstructPosition(float3 Coord, float4x4 Transform)
float3 ScreenXYDepthToViewSpace(float3 Coord, float4x4 Transform)
{
float3 NDC = float3(TexUVToNormalizedDeviceXY(Coord.xy), DepthToCameraZ(Coord.z, Transform));
return float3(NDC.z * NDC.x / MATRIX_ELEMENT(Transform, 0, 0), NDC.z * NDC.y / MATRIX_ELEMENT(Transform, 1, 1), NDC.z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ float ComputeAmbientOcclusionPS(in FullScreenTriangleVSOutput VSOut) : SV_Target
#endif

float3 NormalVS = mul(float4(SampleNormalWS(ScreenCoordUV), 0.0), g_Camera.mView).xyz;
float3 PositionVS = FastReconstructPosition(PositionSS, g_Camera.mProj);
float3 PositionVS = ScreenXYDepthToViewSpace(PositionSS, g_Camera.mProj);
float3 ViewVS = -normalize(PositionVS);
float2 Xi = SampleRandomVector2D(int2(Position));

Expand Down Expand Up @@ -138,8 +138,8 @@ float ComputeAmbientOcclusionPS(in FullScreenTriangleVSOutput VSOut) : SV_Target
float2 SamplePositionSS1 = PositionSS.xy - SampleOffset;

float MipLevel = clamp(log2(length(SampleOffset * g_Camera.f4ViewportSize.xy)) - g_SSAOAttribs.DepthMIPSamplingOffset, 0.0, float(SSAO_DEPTH_PREFILTERED_MAX_MIP));
float3 SamplePositionVS0 = FastReconstructPosition(float3(SamplePositionSS0, SamplePrefilteredDepth(SamplePositionSS0, MipLevel)), g_Camera.mProj);
float3 SamplePositionVS1 = FastReconstructPosition(float3(SamplePositionSS0, SamplePrefilteredDepth(SamplePositionSS1, MipLevel)), g_Camera.mProj);
float3 SamplePositionVS0 = ScreenXYDepthToViewSpace(float3(SamplePositionSS0, SamplePrefilteredDepth(SamplePositionSS0, MipLevel)), g_Camera.mProj);
float3 SamplePositionVS1 = ScreenXYDepthToViewSpace(float3(SamplePositionSS0, SamplePrefilteredDepth(SamplePositionSS1, MipLevel)), g_Camera.mProj);

float3 SampleDifference0 = SamplePositionVS0 - PositionVS;
float3 SampleDifference1 = SamplePositionVS1 - PositionVS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ float ComputeResampledHistoryPS(in FullScreenTriangleVSOutput VSOut) : SV_Target

int MipLevel = int(float(SSAO_DEPTH_HISTORY_CONVOLUTED_MAX_MIP) * (1.0 - saturate(AccumulationFactor)));
float3 PositionSS = float3(Position.xy * g_Camera.f4ViewportSize.zw, Depth);
float3 PositionVS = FastReconstructPosition(PositionSS, g_Camera.mProj);
float3 PositionVS = ScreenXYDepthToViewSpace(PositionSS, g_Camera.mProj);
float3 NormalVS = mul(float4(SampleNormalWS(int2(Position.xy)), 0.0), g_Camera.mView).xyz;
float PlaneNormalFactor = 10.0 / (1.0 + DepthToCameraZ(Depth, g_Camera.mProj));

Expand Down Expand Up @@ -101,7 +101,7 @@ float ComputeResampledHistoryPS(in FullScreenTriangleVSOutput VSOut) : SV_Target
float SampledOcclusion = SampleOcclusionPoint(Texcoord, MipLevel);

float3 SamplePositionSS = float3(Texcoord, SampledDepth);
float3 SamplePositionVS = FastReconstructPosition(SamplePositionSS, g_Camera.mProj);
float3 SamplePositionVS = ScreenXYDepthToViewSpace(SamplePositionSS, g_Camera.mProj);

float WeightS = Weight[SampleIdx];
float WeightZ = ComputeGeometryWeight(PositionVS, SamplePositionVS, NormalVS, PlaneNormalFactor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ float ComputeSpatialReconstructionPS(in FullScreenTriangleVSOutput VSOut) : SV_T
return SampleOcclusion(int2(Position.xy));

float3 PositionSS = float3(Position.xy * g_Camera.f4ViewportSize.zw, Depth);
float3 PositionVS = FastReconstructPosition(PositionSS, g_Camera.mProj);
float3 PositionVS = ScreenXYDepthToViewSpace(PositionSS, g_Camera.mProj);
float3 NormalVS = mul(float4(SampleNormalWS(int2(Position.xy)), 0.0), g_Camera.mView).xyz;
float4 Rotator = ComputeBlurKernelRotation(uint2(Position.xy), g_Camera.uiFrameIndex);
float Radius = lerp(0.0, g_SSAOAttribs.SpatialReconstructionRadius, 1.0 - saturate(AccumulationFactor));
Expand All @@ -86,7 +86,7 @@ float ComputeSpatialReconstructionPS(in FullScreenTriangleVSOutput VSOut) : SV_T
float SampledOcclusion = SampleOcclusion(SampleCoord);

float3 SamplePositionSS = float3((float2(SampleCoord) + 0.5) * g_Camera.f4ViewportSize.zw, SampledDepth);
float3 SamplePositionVS = FastReconstructPosition(SamplePositionSS, g_Camera.mProj);
float3 SamplePositionVS = ScreenXYDepthToViewSpace(SamplePositionSS, g_Camera.mProj);

float WeightS = ComputeSpatialWeight(Poisson[SampleIdx].z * Poisson[SampleIdx].z, SSAO_SPATIAL_RECONSTRUCTION_SIGMA);
float WeightZ = ComputeGeometryWeight(PositionVS, SamplePositionVS, NormalVS, PlaneNormalFactor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ float3 SampleNormalWS(int2 PixelCoord)

float SampleDepthHierarchy(int2 PixelCoord, int MipLevel)
{
return DepthToNormalizedDeviceZ(g_TextureDepthHierarchy.Load(int3(PixelCoord, MipLevel)));
return g_TextureDepthHierarchy.Load(int3(PixelCoord, MipLevel));
}

float2 SampleMotion(int2 PixelCoord)
Expand Down Expand Up @@ -214,8 +214,8 @@ float ValidateHit(float3 Hit, float2 ScreenCoordUV, float3 RayDirectionWS, float
if (dot(HitNormalWS, RayDirectionWS) > 0.0)
return 0.0;

float3 SurfaceVS = FastReconstructPosition(float3(Hit.xy, SurfaceDepth), g_Camera.mProj);
float3 HitVS = FastReconstructPosition(Hit, g_Camera.mProj);
float3 SurfaceVS = ScreenXYDepthToViewSpace(float3(Hit.xy, SurfaceDepth), g_Camera.mProj);
float3 HitVS = ScreenXYDepthToViewSpace(Hit, g_Camera.mProj);
float Distance = distance(SurfaceVS, HitVS);

// Fade out hits near the screen borders
Expand Down Expand Up @@ -289,15 +289,15 @@ PSOutput ComputeIntersectionPS(in FullScreenTriangleVSOutput VSOut)
float2 MipResolution = GetMipResolution(g_Camera.f4ViewportSize.xy, MostDetailedMip);

float3 RayOriginSS = float3(ScreenCoordUV, SampleDepthHierarchy(int2(ScreenCoordUV * MipResolution), MostDetailedMip));
float3 RayOriginVS = FastReconstructPosition(RayOriginSS, g_Camera.mProj);
float3 RayOriginVS = ScreenXYDepthToViewSpace(RayOriginSS, g_Camera.mProj);

float4 RayDirectionVS = SampleReflectionVector(-normalize(RayOriginVS), NormalVS, Roughness, int2(VSOut.f4PixelPos.xy));
float3 RayDirectionSS = ProjectDirection(RayOriginVS, RayDirectionVS.xyz, RayOriginSS, g_Camera.mProj);
float3 RayDirectionWS = mul(float4(RayDirectionVS.xyz, 0.0), g_Camera.mViewInv).xyz;

bool ValidHit = false;
float3 SurfaceHitSS = HierarchicalRaymarch(RayOriginSS, RayDirectionSS, g_Camera.f4ViewportSize.xy, MostDetailedMip, g_SSRAttribs.MaxTraversalIntersections, ValidHit);
float3 SurfaceHitVS = FastReconstructPosition(SurfaceHitSS, g_Camera.mProj);
float3 SurfaceHitVS = ScreenXYDepthToViewSpace(SurfaceHitSS, g_Camera.mProj);

#if SSR_OPTION_PREVIOUS_FRAME
float2 Motion = SampleMotion(int2(g_Camera.f4ViewportSize.xy * SurfaceHitSS.xy));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ float SamplePrevVarianceLinear(float2 PixelCoord)
float2 ComputeReflectionHitPosition(int2 PixelCoord, float Depth)
{
float2 Texcoord = (float2(PixelCoord) + 0.5) * g_CurrCamera.f4ViewportSize.zw + F3NDC_XYZ_TO_UVD_SCALE.xy * g_CurrCamera.f2Jitter;
float3 PositionWS = InvProjectPosition(float3(Texcoord, DepthToNormalizedDeviceZ(Depth)), g_CurrCamera.mViewProjInv);
float3 PositionWS = InvProjectPosition(float3(Texcoord, Depth), g_CurrCamera.mViewProjInv);
float3 PrevCoordUV = ProjectPosition(PositionWS, g_PrevCamera.mViewProj);
return (PrevCoordUV.xy - F3NDC_XYZ_TO_UVD_SCALE.xy * g_PrevCamera.f2Jitter) * g_CurrCamera.f4ViewportSize.xy;
}
Expand Down

0 comments on commit 61663c9

Please sign in to comment.