rendering: formalize main light shadow params

This commit is contained in:
2026-04-13 01:06:09 +08:00
parent 64212a53c7
commit 2ee74e7761
8 changed files with 94 additions and 61 deletions

View File

@@ -25,6 +25,21 @@ Shader "Builtin Forward Lit"
float4 spotAnglesAndFlags;
};
struct ShadowMapMetrics
{
float2 inverseMapSize;
float worldTexelSize;
float padding;
};
struct ShadowSamplingData
{
float enabled;
float receiverDepthBias;
float normalBiasScale;
float shadowStrength;
};
cbuffer LightingConstants
{
float4 gMainLightDirectionAndIntensity;
@@ -42,8 +57,8 @@ Shader "Builtin Forward Lit"
cbuffer ShadowReceiverConstants
{
float4x4 gWorldToShadowMatrix;
float4 gShadowBiasAndTexelSize;
float4 gShadowOptions;
ShadowMapMetrics gShadowMapMetrics;
ShadowSamplingData gShadowSampling;
};
Texture2D BaseColorTexture;
@@ -83,12 +98,13 @@ Shader "Builtin Forward Lit"
#ifndef XC_MAIN_LIGHT_SHADOWS
return 1.0f;
#else
if (gShadowOptions.x < 0.5f) {
if (gShadowSampling.enabled < 0.5f) {
return 1.0f;
}
const float nDotL = saturate(dot(normalize(normalWS), normalize(lightDirectionWS)));
const float normalBiasWorld = gShadowOptions.y * gShadowOptions.z * (1.0f - nDotL);
const float normalBiasWorld =
gShadowMapMetrics.worldTexelSize * gShadowSampling.normalBiasScale * (1.0f - nDotL);
const float3 shadowPositionWS = positionWS + normalize(normalWS) * normalBiasWorld;
const float4 shadowClip = mul(gWorldToShadowMatrix, float4(shadowPositionWS, 1.0f));
if (shadowClip.w <= 0.0f) {
@@ -109,7 +125,7 @@ Shader "Builtin Forward Lit"
return 1.0f;
}
const float receiverDepth = shadowNdc.z * 0.5f + 0.5f - gShadowBiasAndTexelSize.x;
const float receiverDepth = shadowNdc.z * 0.5f + 0.5f - gShadowSampling.receiverDepthBias;
#else
if (shadowUv.x < 0.0f || shadowUv.x > 1.0f ||
shadowUv.y < 0.0f || shadowUv.y > 1.0f ||
@@ -117,9 +133,9 @@ Shader "Builtin Forward Lit"
return 1.0f;
}
const float receiverDepth = shadowNdc.z - gShadowBiasAndTexelSize.x;
const float receiverDepth = shadowNdc.z - gShadowSampling.receiverDepthBias;
#endif
const float2 shadowTexelSize = gShadowBiasAndTexelSize.yz;
const float2 shadowTexelSize = gShadowMapMetrics.inverseMapSize;
float visibility = 0.0f;
[unroll]
for (int offsetY = -1; offsetY <= 1; ++offsetY) {
@@ -139,7 +155,7 @@ Shader "Builtin Forward Lit"
}
visibility *= (1.0f / 9.0f);
const float shadowStrength = saturate(gShadowBiasAndTexelSize.w);
const float shadowStrength = saturate(gShadowSampling.shadowStrength);
return lerp(1.0f - shadowStrength, 1.0f, visibility);
#endif
}