Skip to content

Commit

Permalink
refactor: add namespace to ScreenSpaceShadows
Browse files Browse the repository at this point in the history
  • Loading branch information
FlayaN committed Aug 18, 2024
1 parent 5ff8fcb commit 9a8462a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
Texture2D<unorm half> ScreenSpaceShadowsTexture : register(t17);

float GetScreenSpaceShadow(float2 a_uv, float a_noise, float3 a_viewPosition, uint a_eyeIndex)
namespace ScreenSpaceShadows
{
a_noise *= 2.0 * M_PI;
float GetScreenSpaceShadow(float2 a_uv, float a_noise, float3 a_viewPosition, uint a_eyeIndex)
{
a_noise *= 2.0 * M_PI;

half2x2 rotationMatrix = half2x2(cos(a_noise), sin(a_noise), -sin(a_noise), cos(a_noise));
half2x2 rotationMatrix = half2x2(cos(a_noise), sin(a_noise), -sin(a_noise), cos(a_noise));

float weight = 0;
float shadow = 0;
float weight = 0;
float shadow = 0;

static const float2 BlurOffsets[4] = {
float2(0.381664f, 0.89172f),
float2(0.491409f, 0.216926f),
float2(0.937803f, 0.734825f),
float2(0.00921659f, 0.0562151f),
};
static const float2 BlurOffsets[4] = {
float2(0.381664f, 0.89172f),
float2(0.491409f, 0.216926f),
float2(0.937803f, 0.734825f),
float2(0.00921659f, 0.0562151f),
};

for (uint i = 0; i < 4; i++) {
float2 offset = mul(BlurOffsets[i], rotationMatrix) * 0.0025;
for (uint i = 0; i < 4; i++) {
float2 offset = mul(BlurOffsets[i], rotationMatrix) * 0.0025;

float2 sampleUV = a_uv + offset;
sampleUV = saturate(sampleUV);
int3 sampleCoord = ConvertUVToSampleCoord(sampleUV, a_eyeIndex);
float2 sampleUV = a_uv + offset;
sampleUV = saturate(sampleUV);
int3 sampleCoord = ConvertUVToSampleCoord(sampleUV, a_eyeIndex);

float rawDepth = TexDepthSampler.Load(sampleCoord).x;
float linearDepth = GetScreenDepth(rawDepth);
float rawDepth = TexDepthSampler.Load(sampleCoord).x;
float linearDepth = GetScreenDepth(rawDepth);

float attenuation = 1.0 - saturate(100.0 * abs(linearDepth - a_viewPosition.z) / a_viewPosition.z);
if (attenuation > 0.0) {
shadow += ScreenSpaceShadowsTexture.Load(sampleCoord).x * attenuation;
weight += attenuation;
float attenuation = 1.0 - saturate(100.0 * abs(linearDepth - a_viewPosition.z) / a_viewPosition.z);
if (attenuation > 0.0) {
shadow += ScreenSpaceShadowsTexture.Load(sampleCoord).x * attenuation;
weight += attenuation;
}
}
}

if (weight > 0.0)
shadow /= weight;
else
shadow = ScreenSpaceShadowsTexture.Load(ConvertUVToSampleCoord(a_uv, a_eyeIndex)).x;
if (weight > 0.0)
shadow /= weight;
else
shadow = ScreenSpaceShadowsTexture.Load(ConvertUVToSampleCoord(a_uv, a_eyeIndex)).x;

return shadow;
}
return shadow;
}
}
2 changes: 1 addition & 1 deletion package/Shaders/DistantTree.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ PS_OUTPUT main(PS_INPUT input)
float dirShadow = 1;

# if defined(SCREEN_SPACE_SHADOWS)
dirShadow = lerp(0.2, 1.0, GetScreenSpaceShadow(screenUV, screenNoise, viewPosition, eyeIndex));
dirShadow = lerp(0.2, 1.0, ScreenSpaceShadows::GetScreenSpaceShadow(screenUV, screenNoise, viewPosition, eyeIndex));
# endif

# if defined(TERRA_OCC)
Expand Down
2 changes: 1 addition & 1 deletion package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if defined(SOFT_LIGHTING) || defined(BACK_LIGHTING) || defined(RIM_LIGHTING)
if (dirLightAngle > 0.0)
# endif
dirDetailShadow = GetScreenSpaceShadow(screenUV, screenNoise, viewPosition, eyeIndex);
dirDetailShadow = ScreenSpaceShadows::GetScreenSpaceShadow(screenUV, screenNoise, viewPosition, eyeIndex);
# endif

# if defined(EMAT) && (defined(SKINNED) || !defined(MODELSPACENORMALS))
Expand Down
2 changes: 1 addition & 1 deletion package/Shaders/RunGrass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
if (shadowColor.x > 0.0) {
if (dirLightAngle > 0.0) {
# if defined(SCREEN_SPACE_SHADOWS)
dirDetailShadow = GetScreenSpaceShadow(screenUV, screenNoise, viewPosition, eyeIndex);
dirDetailShadow = ScreenSpaceShadows::GetScreenSpaceShadow(screenUV, screenNoise, viewPosition, eyeIndex);
# endif // SCREEN_SPACE_SHADOWS
}

Expand Down

0 comments on commit 9a8462a

Please sign in to comment.