Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix VR #328

Merged
merged 20 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/blur.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ float HistoryRadiusScaling(float accumFrames)
const uint numSamples = 8;

const float2 uv = (dtid + .5) * RcpOutFrameDim;
uint eyeIndex = GET_EYE_IDX(uv);
const float2 screenPos = ConvertToStereoUV(uv, eyeIndex);
uint eyeIndex = GetEyeIndexFromTexCoord(uv);
const float2 screenPos = ConvertFromStereoUV(uv, eyeIndex);

float depth = READ_DEPTH(srcDepth, dtid);
float3 pos = ScreenToViewPosition(screenPos, depth, eyeIndex);
Expand All @@ -63,7 +63,7 @@ float HistoryRadiusScaling(float accumFrames)
float2 pxOffset = radius * g_Poisson8[i].xy;
float2 pxSample = dtid + .5 + pxOffset;
float2 uvSample = pxSample * RcpOutFrameDim;
float2 screenPosSample = ConvertToStereoUV(uvSample, eyeIndex);
float2 screenPosSample = ConvertFromStereoUV(uvSample, eyeIndex);

if (any(screenPosSample < 0) || any(screenPosSample > 1))
continue;
Expand Down
6 changes: 0 additions & 6 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ SamplerState samplerLinearClamp : register(s1);
# define FULLRES_LOAD(tex, px, texCoord, samp) tex[px]
#endif

#ifdef VR
# define GET_EYE_IDX(uv) (uv.x > 0.5)
#else
# define GET_EYE_IDX(uv) (0)
#endif

///////////////////////////////////////////////////////////////////////////////

#define ISNAN(x) (!(x < 0.f || x > 0.f || x == 0.f))
Expand Down
8 changes: 4 additions & 4 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/gi.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ void CalculateGI(
const float2 srcScale = SrcFrameDim * RcpTexDim;
const float2 outScale = OutFrameDim * RcpTexDim;

uint eyeIndex = GET_EYE_IDX(uv);
float2 normalizedScreenPos = ConvertToStereoUV(uv, eyeIndex);
uint eyeIndex = GetEyeIndexFromTexCoord(uv);
float2 normalizedScreenPos = ConvertFromStereoUV(uv, eyeIndex);

const float rcpNumSlices = rcp(NumSlices);
const float rcpNumSteps = rcp(NumSteps);
Expand Down Expand Up @@ -171,7 +171,7 @@ void CalculateGI(

float2 samplePxCoord = dtid + .5 + sampleOffset * sideSign;
float2 sampleUV = samplePxCoord * RcpOutFrameDim;
float2 sampleScreenPos = ConvertToStereoUV(sampleUV, eyeIndex);
float2 sampleScreenPos = ConvertFromStereoUV(sampleUV, eyeIndex);
[branch] if (any(sampleScreenPos > 1.0) || any(sampleScreenPos < 0.0)) break;

float sampleOffsetLength = length(sampleOffset);
Expand Down Expand Up @@ -358,7 +358,7 @@ void CalculateGI(
const float2 outScale = OutFrameDim * RcpTexDim;

float2 uv = (dtid + .5f) * RcpOutFrameDim;
uint eyeIndex = GET_EYE_IDX(uv);
uint eyeIndex = GetEyeIndexFromTexCoord(uv);

float viewspaceZ = READ_DEPTH(srcWorkingDepth, dtid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void readHistory(
const float2 srcScale = SrcFrameDim * RcpTexDim;

const float2 uv = (pixCoord + .5) * RcpOutFrameDim;
const float2 screen_pos = ConvertToStereoUV(uv, eyeIndex);
const float2 screen_pos = ConvertFromStereoUV(uv, eyeIndex);
if (any(screen_pos < 0) || any(screen_pos > 1))
return;

Expand Down Expand Up @@ -62,14 +62,14 @@ void readHistory(
const float2 outScale = OutFrameDim * RcpTexDim;

const float2 uv = (pixCoord + .5) * RcpOutFrameDim;
uint eyeIndex = GET_EYE_IDX(uv);
const float2 screen_pos = ConvertToStereoUV(uv, eyeIndex);
uint eyeIndex = GetEyeIndexFromTexCoord(uv);
const float2 screen_pos = ConvertFromStereoUV(uv, eyeIndex);

float2 prev_uv = uv;
#ifdef REPROJECTION
prev_uv += FULLRES_LOAD(srcMotionVec, pixCoord, uv * srcScale, samplerLinearClamp).xy;
#endif
float2 prev_screen_pos = ConvertToStereoUV(prev_uv, eyeIndex);
float2 prev_screen_pos = ConvertFromStereoUV(prev_uv, eyeIndex);

half4 prev_gi_albedo = 0;
half4 prev_gi = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ float GetScreenSpaceShadow(float2 a_uv, float a_noise, float3 a_viewPosition, ui
if (weight > 0.0)
shadow /= weight;
else
shadow = ScreenSpaceShadowsTexture.Load(int3(a_uv * BufferDim.xy, 0)).x;
shadow = ScreenSpaceShadowsTexture.Load(int3(GetDynamicResolutionAdjustedScreenPosition(ConvertToStereoUV(a_uv, a_eyeIndex)) * BufferDim.xy, 0)).x;

return shadow;
}
10 changes: 4 additions & 6 deletions features/Skylighting/Shaders/Skylighting/SkylightingCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ struct PerGeometry
float4 AlphaTestRef;
float4 ShadowLightParam; // Falloff in x, ShadowDistance squared in z
float4x3 FocusShadowMapProj[4];
#if !defined(VR)
float4x3 ShadowMapProj[1][3];
float4x4 CameraViewProjInverse2[1];
#else
// Since PerGeometry is passed between c++ and hlsl, can't have different defines due to strong typing
float4x3 ShadowMapProj[2][3];
float4x4 CameraViewProjInverse2[2];
#endif // VR
float4x4 CameraViewProjInverse[2];
};

Texture2DArray<unorm float> TexShadowMapSampler : register(t1);
Expand Down Expand Up @@ -61,6 +57,7 @@ half GetScreenDepth(half depth)
: SV_DispatchThreadID) {
float2 uv = float2(globalId.xy + 0.5) * BufferDim.zw * DynamicResolutionParams2.xy;
uint eyeIndex = GetEyeIndexFromTexCoord(uv);
uv = ConvertFromStereoUV(uv, eyeIndex);

half3 normalGlossiness = NormalRoughnessTexture[globalId.xy];
half3 normalVS = DecodeNormal(normalGlossiness.xy);
Expand Down Expand Up @@ -165,6 +162,7 @@ half GetScreenDepth(half depth)
: SV_DispatchThreadID) {
float2 uv = float2(globalId.xy + 0.5) * BufferDim.zw * DynamicResolutionParams2.xy;
uint eyeIndex = GetEyeIndexFromTexCoord(uv);
uv = ConvertFromStereoUV(uv, eyeIndex);

half3 normalGlossiness = NormalRoughnessTexture[globalId.xy];
half3 normalVS = DecodeNormal(normalGlossiness.xy);
Expand Down
23 changes: 12 additions & 11 deletions package/Shaders/Common/ShadowSampling.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ struct PerGeometry
float4 AlphaTestRef;
float4 ShadowLightParam; // Falloff in x, ShadowDistance squared in z
float4x3 FocusShadowMapProj[4];
float4x3 ShadowMapProj[4];
float4x4 CameraViewProjInverse;
// Since PerGeometry is passed between c++ and hlsl, can't have different defines due to strong typing
float4x3 ShadowMapProj[2][3];
float4x4 CameraViewProjInverse[2];
};

Texture2DArray<float4> TexShadowMapSampler : register(t25);
Expand All @@ -31,7 +32,7 @@ cbuffer PerWaterType : register(b7)
uint pad1water;
};

float3 GetShadow(float3 positionWS)
float3 GetShadow(float3 positionWS, uint a_eyeIndex = 0)
{
PerGeometry sD = perShadow[0];
sD.EndSplitDistances.x = GetScreenDepth(sD.EndSplitDistances.x);
Expand All @@ -42,18 +43,18 @@ float3 GetShadow(float3 positionWS)
float shadowMapDepth = length(positionWS.xyz);

half cascadeIndex = 0;
half4x3 lightProjectionMatrix = sD.ShadowMapProj[0];
half4x3 lightProjectionMatrix = sD.ShadowMapProj[a_eyeIndex][0];
half shadowMapThreshold = sD.AlphaTestRef.y;

[flatten] if (2.5 < sD.EndSplitDistances.w && sD.EndSplitDistances.y < shadowMapDepth)
{
lightProjectionMatrix = sD.ShadowMapProj[2];
lightProjectionMatrix = sD.ShadowMapProj[a_eyeIndex][2];
shadowMapThreshold = sD.AlphaTestRef.z;
cascadeIndex = 2;
}
else if (sD.EndSplitDistances.x < shadowMapDepth)
{
lightProjectionMatrix = sD.ShadowMapProj[1];
lightProjectionMatrix = sD.ShadowMapProj[a_eyeIndex][1];
shadowMapThreshold = sD.AlphaTestRef.z;
cascadeIndex = 1;
}
Expand All @@ -74,7 +75,7 @@ float3 GetShadow(float3 positionWS)
return shadow;
}

float GetVL(float3 startPosWS, float3 endPosWS, float2 screenPosition)
float GetVL(float3 startPosWS, float3 endPosWS, float2 screenPosition, uint a_eyeIndex = 0)
{
const static uint nSteps = 16;
const static float step = 1.0 / float(nSteps);
Expand Down Expand Up @@ -125,21 +126,21 @@ float GetVL(float3 startPosWS, float3 endPosWS, float2 screenPosition)
float shadowMapDepth = length(samplePositionWS.xyz);

half cascadeIndex = 0;
half4x3 lightProjectionMatrix = sD.ShadowMapProj[0];
half4x3 lightProjectionMatrix = sD.ShadowMapProj[a_eyeIndex][0];
half shadowMapThreshold = sD.AlphaTestRef.y;

half shadowRange = sD.EndSplitDistances.x;

[flatten] if (2.5 < sD.EndSplitDistances.w && sD.EndSplitDistances.y < shadowMapDepth)
{
lightProjectionMatrix = sD.ShadowMapProj[2];
lightProjectionMatrix = sD.ShadowMapProj[a_eyeIndex][2];
shadowMapThreshold = sD.AlphaTestRef.z;
cascadeIndex = 2;
shadowRange = sD.EndSplitDistances.z - sD.EndSplitDistances.y;
}
else if (sD.EndSplitDistances.x < shadowMapDepth)
{
lightProjectionMatrix = sD.ShadowMapProj[1];
lightProjectionMatrix = sD.ShadowMapProj[a_eyeIndex][1];
shadowMapThreshold = sD.AlphaTestRef.z;
cascadeIndex = 1;
shadowRange = sD.EndSplitDistances.y - sD.EndSplitDistances.x;
Expand All @@ -158,7 +159,7 @@ float GetVL(float3 startPosWS, float3 endPosWS, float2 screenPosition)
if (shadow > 0.0) {
float terrainShadow = 1;
float terrainAo = 1;
GetTerrainOcclusion(samplePositionWS + CameraPosAdjust[0], length(samplePositionWS), LinearSampler, terrainShadow, terrainAo);
GetTerrainOcclusion(samplePositionWS + CameraPosAdjust[a_eyeIndex], length(samplePositionWS), LinearSampler, terrainShadow, terrainAo);
shadow *= terrainShadow;
}
# endif
Expand Down
17 changes: 10 additions & 7 deletions package/Shaders/DeferredCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RWTexture2D<half4> NormalTAAMaskSpecularMaskRW : register(u1);
RWTexture2D<half2> SnowParametersRW : register(u2);

#if defined(DYNAMIC_CUBEMAPS)
Texture2D<unorm float> DepthTexture : register(t5);
Texture2D<float> DepthTexture : register(t5);
Texture2D<unorm half3> ReflectanceTexture : register(t6);
TextureCube<half3> EnvTexture : register(t7);
TextureCube<half3> EnvReflectionsTexture : register(t8);
Expand All @@ -32,6 +32,7 @@ Texture2D<half2> SkylightingTexture : register(t9);
: SV_DispatchThreadID) {
half2 uv = half2(dispatchID.xy + 0.5) * BufferDim.zw;
uint eyeIndex = GetEyeIndexFromTexCoord(uv);
uv = ConvertFromStereoUV(uv, eyeIndex);

half3 normalGlossiness = NormalRoughnessTexture[dispatchID.xy];
half3 normalVS = DecodeNormal(normalGlossiness.xy);
Expand Down Expand Up @@ -60,7 +61,7 @@ Texture2D<half2> SkylightingTexture : register(t9);
half depth = DepthTexture[dispatchID.xy];

half4 positionCS = half4(2 * half2(uv.x, -uv.y + 1) - 1, depth, 1);
positionCS = mul(CameraViewInverse[eyeIndex], positionCS);
positionCS = mul(CameraViewProjInverse[eyeIndex], positionCS);
positionCS.xyz = positionCS.xyz / positionCS.w;

half3 positionWS = positionCS;
Expand Down Expand Up @@ -100,14 +101,16 @@ Texture2D<half2> SkylightingTexture : register(t9);

#if defined(DEBUG)

half2 texCoord = half2(dispatchID.xy) / BufferDim.xy;
# if defined(VR)
uv.x += (eyeIndex ? 0.1 : -0.1);
# endif // VR

if (texCoord.x < 0.5 && texCoord.y < 0.5) {
if (uv.x < 0.5 && uv.y < 0.5) {
color = color;
} else if (texCoord.x < 0.5) {
} else if (uv.x < 0.5) {
color = albedo;
} else if (texCoord.y < 0.5) {
color = normalWS;
} else if (uv.y < 0.5) {
color = normalVS;
} else {
color = glossiness;
}
Expand Down
6 changes: 4 additions & 2 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1503,9 +1503,11 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
float maxOcclusion = 1;
float minWetnessAngle = 0;
minWetnessAngle = saturate(max(minWetnessValue, worldSpaceNormal.z));

# if defined(SKYLIGHTING)
float skylight = GetSkylightOcclusion(input.WorldPosition + worldSpaceNormal, screenNoise);

# else
float skylight = 1.0;
# endif // SKYLIGHTING
bool raindropOccluded = false;

float4 raindropInfo = float4(0, 0, 1, 0);
Expand Down
Loading
Loading