Skip to content

Commit

Permalink
fix: add stenciltexture to fix vr arms and fixed pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FlayaN committed Mar 10, 2024
1 parent 1b6be83 commit 0cf20f1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ float2 ViewToUV(float3 position, bool is_position, uint a_eyeIndex)
float4 uv = mul(ProjMatrix[a_eyeIndex], float4(position, (float)is_position));
return (uv.xy / uv.w) * float2(0.5f, -0.5f) + 0.5f;
}

uint GetEyeIndexFromTexCoord(float2 texCoord)
{
#ifdef VR
return (texCoord.x >= 0.5) ? 1 : 0;
#endif // VR
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ float InverseProjectUV(float2 uv, uint a_eyeIndex)
#endif

float2 texCoord = (DTid.xy + 0.5) * RcpBufferDim;

#ifdef VR
uint eyeIndex = (texCoord.x >= 0.5) ? 1 : 0;
#else
uint eyeIndex = 0;
#endif // VR
uint eyeIndex = GetEyeIndexFromTexCoord(texCoord);

float startDepth = GetDepth(texCoord * 2 * DynamicRes.zw);
if (startDepth >= 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

Texture2D<float> ShadowTexture : register(t1);

// Needed to fix a bug in VR that caused the arm
// to have the "outline" of the VR headset canvas
// rendered into it and to not cast rays outside the eyes
#ifdef VR
Texture2D<uint2> StencilTexture : register(t89);

float GetStencil(float2 uv)
{
return StencilTexture.Load(int3(uv * BufferDim * DynamicRes.xy, 0)).g;
}

float GetStencil(float2 uv, uint a_eyeIndex)
{
uv = ConvertToStereoUV(uv, a_eyeIndex);
return GetStencil(uv);
}
#endif // VR

bool IsSaturated(float value) { return value == saturate(value); }
bool IsSaturated(float2 value) { return IsSaturated(value.x) && IsSaturated(value.y); }

Expand Down Expand Up @@ -31,16 +49,20 @@ float GetScreenDepth(float2 uv, uint a_eyeIndex)
return GetScreenDepth(depth);
}

float ScreenSpaceShadowsUV(float2 stereoTexCoord, uint eyeIndex)
float ScreenSpaceShadowsUV(float2 texcoord, float3 lightDirectionVS, uint eyeIndex)
{
float2 texcoord = ConvertFromStereoUV(stereoTexCoord, eyeIndex);
#ifdef VR
if (GetStencil(texcoord) != 0)
return 1;
#endif // VR

// Ignore the sky
float startDepth = GetDepth(stereoTexCoord);
float startDepth = GetDepth(texcoord);
if (startDepth >= 1)
return 1;

// Compute ray position in view-space
float3 rayPos = InverseProjectUVZ(texcoord, startDepth, eyeIndex);
float3 rayPos = InverseProjectUVZ(ConvertFromStereoUV(texcoord, eyeIndex), startDepth, eyeIndex);

// Blends effect variables between near, mid and far field
float blendFactorFar = smoothstep(ShadowDistance / 3, ShadowDistance / 2, rayPos.z);
Expand All @@ -56,10 +78,10 @@ float ScreenSpaceShadowsUV(float2 stereoTexCoord, uint eyeIndex)
float stepLength = maxDistance / (float)maxSteps;

// Compute ray step
float3 rayStep = InvDirLightDirectionVS.xyz * stepLength;
float3 rayStep = lightDirectionVS * stepLength;

// Offset starting position with interleaved gradient noise
float offset = InterleavedGradientNoise(stereoTexCoord * BufferDim);
float offset = InterleavedGradientNoise(texcoord * BufferDim);
rayPos += rayStep * offset;

float thickness = lerp(NearThickness, rayPos.z * FarThicknessScale, blendFactorFar);
Expand All @@ -80,6 +102,11 @@ float ScreenSpaceShadowsUV(float2 stereoTexCoord, uint eyeIndex)
if (!IsSaturated(rayUV))
break;

#ifdef VR
if (GetStencil(rayUV, eyeIndex) != 0)
break;
#endif // VR

// Compute the difference between the ray's and the camera's depth
float rayDepth = GetScreenDepth(rayUV, eyeIndex);

Expand Down Expand Up @@ -107,12 +134,6 @@ float ScreenSpaceShadowsUV(float2 stereoTexCoord, uint eyeIndex)
[numthreads(32, 32, 1)] void main(uint3 DTid
: SV_DispatchThreadID) {
float2 texCoord = (DTid.xy + 0.5) * RcpBufferDim * DynamicRes.zw;

#ifdef VR
uint eyeIndex = (texCoord.x >= 0.5) ? 1 : 0;
#else
uint eyeIndex = 0;
#endif // VR

OcclusionRW[DTid.xy] = ScreenSpaceShadowsUV(texCoord, eyeIndex);
uint eyeIndex = GetEyeIndexFromTexCoord(texCoord);
OcclusionRW[DTid.xy] = ScreenSpaceShadowsUV(texCoord, InvDirLightDirectionVS.xyz, eyeIndex);
}
2 changes: 1 addition & 1 deletion src/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ const std::vector<Feature*>& Feature::GetFeatureList()
};

static std::vector<Feature*> featuresVR = {
DynamicCubemaps::GetSingleton(),
GrassLighting::GetSingleton(),
GrassCollision::GetSingleton(),
ScreenSpaceShadows::GetSingleton(),
ExtendedMaterials::GetSingleton(),
WetnessEffects::GetSingleton(),
LightLimitFix::GetSingleton(),
DynamicCubemaps::GetSingleton(),
TerrainBlending::GetSingleton(),
WaterCaustics::GetSingleton(),
SubsurfaceScattering::GetSingleton()
Expand Down
13 changes: 12 additions & 1 deletion src/Features/ScreenSpaceShadows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ScreenSpaceShadows::DrawSettings()
}

if (ImGui::TreeNodeEx("Near Shadows", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::SliderFloat("Near Distance", &settings.NearDistance, 0, 128);
ImGui::SliderFloat("Near Distance", &settings.NearDistance, 0.25f, 128);
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text("Near Shadow Distance.");
}
Expand Down Expand Up @@ -339,6 +339,12 @@ void ScreenSpaceShadows::ModifyLighting(const RE::BSShader*, const uint32_t)
ID3D11ShaderResourceView* view = depth.depthSRV;
context->CSSetShaderResources(0, 1, &view);

ID3D11ShaderResourceView* stencilView = nullptr;
if (REL::Module::IsVR()) {
stencilView = depth.stencilSRV;
context->CSSetShaderResources(89, 1, &stencilView);
}

ID3D11UnorderedAccessView* uav = screenSpaceShadowsTexture->uav.get();
context->CSSetUnorderedAccessViews(0, 1, &uav, nullptr);

Expand All @@ -347,6 +353,11 @@ void ScreenSpaceShadows::ModifyLighting(const RE::BSShader*, const uint32_t)

context->Dispatch((uint32_t)std::ceil(resolutionX / 32.0f), (uint32_t)std::ceil(resolutionY / 32.0f), 1);

if (REL::Module::IsVR()) {
stencilView = nullptr;
context->CSSetShaderResources(89, 1, &stencilView);
}

// Filter
{
uav = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/Features/ScreenSpaceShadows.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct ScreenSpaceShadows : Feature

struct Settings
{
uint32_t MaxSamples = 24;
uint32_t MaxSamples = !REL::Module::IsVR() ? 24u : 12u;
float FarDistanceScale = 0.025f;
float FarThicknessScale = 0.025f;
float FarHardness = 8.0f;
Expand Down

0 comments on commit 0cf20f1

Please sign in to comment.