Shadow mapping works 99%

This commit is contained in:
2026-02-24 22:52:21 +01:00
parent 2f146445a4
commit 2e63b3cb83
2 changed files with 11 additions and 8 deletions
+7 -4
View File
@@ -46,13 +46,16 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
{ {
uint cascadeIndex = 0; uint cascadeIndex = 0;
for (uint c = 0; c < NUM_CASCADES - 1; ++c) { for (uint c = 0; c < NUM_CASCADES - 1; ++c) {
if (params.position_VS.z > pShadowMapping.cascadeSplits[c]) { if (-params.position_VS.z > pShadowMapping.cascadeSplits[c]) {
cascadeIndex = c + 1; cascadeIndex = c + 1;
} }
} }
float4 lightSpacePos = mul(pShadowMapping.lightSpaceMatrices[cascadeIndex][i], float4(params.position_WS, 1)); float4 lightSpacePos = mul(pShadowMapping.lightSpaceMatrices[cascadeIndex][i], float4(params.position_WS, 1));
lightSpacePos = float4(lightSpacePos.xy * 0.5 + 0.5, lightSpacePos.zw); // Negate Y to compensate for orthographic projection's Y negation + viewport Y-flip double-inversion
lightSpacePos = float4(lightSpacePos.x * 0.5 + 0.5, -lightSpacePos.y * 0.5 + 0.5, lightSpacePos.zw);
float4 shadowCoord = lightSpacePos; float4 shadowCoord = lightSpacePos;
// Convert NDC z to viewport depth space (inverse depth: near=1, far=0)
float currentDepth = 1.0 - shadowCoord.z;
int3 texDim; int3 texDim;
pShadowMapping.shadowMaps[cascadeIndex].GetDimensions(texDim.x, texDim.y, texDim.z); pShadowMapping.shadowMaps[cascadeIndex].GetDimensions(texDim.x, texDim.y, texDim.z);
float scale = 1.5f; float scale = 1.5f;
@@ -65,10 +68,10 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
//for (int x = -range; x <= range; ++x) { //for (int x = -range; x <= range; ++x) {
// for (int y = -range; y <= range; ++y) { // for (int y = -range; y <= range; ++y) {
float shadow = 1.0f; float shadow = 1.0f;
if (shadowCoord.z > 0.0 && shadowCoord.z < 1.0) if (currentDepth > 0.0 && currentDepth < 1.0)
{ {
float dist = pShadowMapping.shadowMaps[cascadeIndex].Sample(pShadowMapping.shadowSampler, float3(shadowCoord.xy, i)).r; float dist = pShadowMapping.shadowMaps[cascadeIndex].Sample(pShadowMapping.shadowSampler, float3(shadowCoord.xy, i)).r;
if (shadowCoord.w > 0 && dist < shadowCoord.z) if (shadowCoord.w > 0 && currentDepth < dist)
{ {
shadow = 0; shadow = 0;
} }
@@ -64,7 +64,7 @@ void ShadowPass::beginFrame(const Component::Camera& camera, const Component::Tr
float uniform = minZ + range * p; float uniform = minZ + range * p;
float d = cascadeSplitLambda * (log - uniform) + uniform; float d = cascadeSplitLambda * (log - uniform) + uniform;
cascadeSplits[i] = (d - nearClip) / clipRange; cascadeSplits[i] = (d - nearClip) / clipRange;
splitDepths[i] = farClip - cascadeSplits[i] * clipRange; splitDepths[i] = d;
cascades[i].viewParams.clear(); cascades[i].viewParams.clear();
} }
cascadeSplitsBuffer->updateContents(0, sizeof(float) * NUM_CASCADES, splitDepths); cascadeSplitsBuffer->updateContents(0, sizeof(float) * NUM_CASCADES, splitDepths);
@@ -76,8 +76,8 @@ void ShadowPass::beginFrame(const Component::Camera& camera, const Component::Tr
float splitDist = cascadeSplits[i]; float splitDist = cascadeSplits[i];
Array<Vector> frustumCorners = { Array<Vector> frustumCorners = {
Vector(-1.0f, 1.0f, 1.0f), Vector(1.0f, 1.0f, 1.0f), Vector(1.0f, -1.0f, 1.0f), Vector(-1.0f, -1.0f, 1.0f),
Vector(-1.0f, 1.0f, 0.0f), Vector(1.0f, 1.0f, 0.0f), Vector(1.0f, -1.0f, 0.0f), Vector(-1.0f, -1.0f, 0.0f), Vector(-1.0f, 1.0f, 0.0f), Vector(1.0f, 1.0f, 0.0f), Vector(1.0f, -1.0f, 0.0f), Vector(-1.0f, -1.0f, 0.0f),
Vector(-1.0f, 1.0f, 1.0f), Vector(1.0f, 1.0f, 1.0f), Vector(1.0f, -1.0f, 1.0f), Vector(-1.0f, -1.0f, 1.0f),
}; };
for (auto& c : frustumCorners) { for (auto& c : frustumCorners) {
@@ -111,7 +111,7 @@ void ShadowPass::beginFrame(const Component::Camera& camera, const Component::Tr
Vector cameraPos = frustumCenter - lightDir * -minExtents.z; Vector cameraPos = frustumCenter - lightDir * -minExtents.z;
Matrix4 viewMatrix = glm::lookAt(cameraPos, frustumCenter, Vector(0, 1, 0)); Matrix4 viewMatrix = glm::lookAt(cameraPos, frustumCenter, Vector(0, 1, 0));
Matrix4 projectionMatrix = Matrix4 projectionMatrix =
orthographicProjection(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, maxExtents.z - minExtents.z, 0.0f); orthographicProjection(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, 0.0f, maxExtents.z - minExtents.z);
Matrix4 viewProjectionMatrix = projectionMatrix * viewMatrix; Matrix4 viewProjectionMatrix = projectionMatrix * viewMatrix;
viewParams = { viewParams = {
.viewMatrix = viewMatrix, .viewMatrix = viewMatrix,
@@ -194,7 +194,7 @@ void ShadowPass::render() {
.rasterizationState = .rasterizationState =
{ {
.cullMode = Gfx::SE_CULL_MODE_NONE, .cullMode = Gfx::SE_CULL_MODE_NONE,
.depthBiasEnable = false, .depthBiasEnable = true,
.depthBiasConstantFactor = depthBiasConstant, .depthBiasConstantFactor = depthBiasConstant,
.depthBiasSlopeFactor = depthBiasSlope, .depthBiasSlopeFactor = depthBiasSlope,
}, },