From 9a8462a585274eba858aa590007ba225da5e1baa Mon Sep 17 00:00:00 2001 From: FlayaN Date: Sun, 18 Aug 2024 18:41:40 +0200 Subject: [PATCH] refactor: add namespace to ScreenSpaceShadows --- .../ScreenSpaceShadows.hlsli | 61 ++++++++++--------- package/Shaders/DistantTree.hlsl | 2 +- package/Shaders/Lighting.hlsl | 2 +- package/Shaders/RunGrass.hlsl | 2 +- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/features/Screen-Space Shadows/Shaders/ScreenSpaceShadows/ScreenSpaceShadows.hlsli b/features/Screen-Space Shadows/Shaders/ScreenSpaceShadows/ScreenSpaceShadows.hlsli index 2d1c073a6..6a0d26e40 100644 --- a/features/Screen-Space Shadows/Shaders/ScreenSpaceShadows/ScreenSpaceShadows.hlsli +++ b/features/Screen-Space Shadows/Shaders/ScreenSpaceShadows/ScreenSpaceShadows.hlsli @@ -1,42 +1,45 @@ Texture2D 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; -} \ No newline at end of file + return shadow; + } +} diff --git a/package/Shaders/DistantTree.hlsl b/package/Shaders/DistantTree.hlsl index 7a8b4cb50..d9ad23391 100644 --- a/package/Shaders/DistantTree.hlsl +++ b/package/Shaders/DistantTree.hlsl @@ -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) diff --git a/package/Shaders/Lighting.hlsl b/package/Shaders/Lighting.hlsl index a6554315e..a0cb928f0 100644 --- a/package/Shaders/Lighting.hlsl +++ b/package/Shaders/Lighting.hlsl @@ -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)) diff --git a/package/Shaders/RunGrass.hlsl b/package/Shaders/RunGrass.hlsl index a0700426d..c0107113f 100644 --- a/package/Shaders/RunGrass.hlsl +++ b/package/Shaders/RunGrass.hlsl @@ -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 }