Babylon.js icon indicating copy to clipboard operation
Babylon.js copied to clipboard

WebGPU / WGSL Cascaded Shadow Map Shader Generation Syntax Error

Open codeCraft-Ritik opened this issue 2 months ago • 3 comments

Issue : WebGPU / WGSL Cascaded Shadow Map Shader Generation Syntax Error

  • Package: @babylonjs/core
  • Subsystem: WebGPU Rendering Engine / Cascaded Shadow Maps (CSM)
  • Affected File: packages/dev/core/src/ShadersWGSL/ShadersInclude/lightFragment.fx (Line 345)
  • Function Definition: packages/dev/core/src/ShadersWGSL/ShadersInclude/shadowsFragmentFunctions.fx (Line 259)
  • Status: Unresolved on master branch

1. Problem Description

When rendering shadows via CascadedShadowGenerator on the WebGPU backend, enabling Percentage-Closer Filtering (PCF) with filteringQuality = ShadowGenerator.QUALITY_LOW causes every shadow-receiving material to emit invalid WGSL code.

Specifically, the shader generator emits two consecutive commas , , where the shadow depth texture argument should be passed. The browser's WebGPU shader parser immediately aborts with a syntax error:

WGSL parse error: expected expression for function call, found ','

Because the shader fails to compile, the entire GPURenderPipeline creation fails, the command buffer is rejected, and rendering halts or drops all frames for the scene.

2. Root Cause Analysis

In packages/dev/core/src/ShadersWGSL/ShadersInclude/shadowsFragmentFunctions.fx (line 259), computeShadowWithCSMPCF1 is declared with 7 arguments:

fn computeShadowWithCSMPCF1(
    layer: i32,
    vPositionFromLight: vec4f,
    depthMetric: f32,
    shadowTexture: texture_depth_2d_array,
    shadowSampler: sampler_comparison,
    darkness: f32,
    frustumEdgeFalloff: f32
) -> f32

In packages/dev/core/src/ShadersWGSL/ShadersInclude/lightFragment.fx, the primary cascade branch at line 314 correctly passes all arguments:

// Line 314: Correct invocation
shadow = computeShadowWithCSMPCF1(index{X}, vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowTexture{X}, shadowTexture{X}Sampler, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);

However, when cascade blending is enabled (which is the default via cascadeBlendPercentage = 0.1), the secondary cascade branch at line 345 omits the 4th argument:

// Line 345: BUG — missing shadowTexture{X} between commas
nextShadow = computeShadowWithCSMPCF1(index{X}, vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], , shadowTexture{X}Sampler, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);

Notice the empty slot between , ,. The WGSL parser fails at token resolution, breaking all PBR and Standard materials that receive CSM shadows under low-quality PCF.

3. Step-by-Step Reproduction

import { WebGPUEngine } from "@babylonjs/core/Engines/webgpuEngine";
import { Scene } from "@babylonjs/core/scene";
import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight";
import { CascadedShadowGenerator } from "@babylonjs/core/Lights/Shadows/cascadedShadowGenerator";
import { ShadowGenerator } from "@babylonjs/core/Lights/Shadows/shadowGenerator";
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";

async function createScene() {
    const canvas = document.createElement("canvas");
    const engine = new WebGPUEngine(canvas);
    await engine.initAsync();
    const scene = new Scene(engine);

    const light = new DirectionalLight("dirLight", new Vector3(-1, -2, -1), scene);
    const csm = new CascadedShadowGenerator(1024, light);

    // Trigger condition: QUALITY_LOW with non-zero cascade blend percentage
    csm.filteringQuality = ShadowGenerator.QUALITY_LOW;
    csm.cascadeBlendPercentage = 0.1; // Default value

    const ground = MeshBuilder.CreateGround("ground", { width: 10, height: 10 }, scene);
    ground.receiveShadows = true;

    // Error triggers on render: WGSL parse error on line 345 of lightFragment.fx
    engine.runRenderLoop(() => scene.render());
}

4. Code Fix & Patch

--- a/packages/dev/core/src/ShadersWGSL/ShadersInclude/lightFragment.fx
+++ b/packages/dev/core/src/ShadersWGSL/ShadersInclude/lightFragment.fx
@@ -342,7 +342,7 @@
                         #else
                             #if defined(SHADOWLOW{X})
-                                nextShadow = computeShadowWithCSMPCF1(index{X}, vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], , shadowTexture{X}Sampler, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
+                                nextShadow = computeShadowWithCSMPCF1(index{X}, vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowTexture{X}, shadowTexture{X}Sampler, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
                             #elif defined(SHADOWMEDIUM{X})
                                 nextShadow = computeShadowWithCSMPCF3(index{X}, vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowTexture{X}, shadowTexture{X}Sampler, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w, filteringInfo{X});
                             #else

5. Contributor Value & Profile Impact

  • Why it matters: WebGPU is the headline focus of Babylon.js v7 and v8. Cascaded Shadow Maps are standard in AAA scenes. A hard crash in basic shadow quality degrades production adoption.
  • Skills demonstrated: WGSL shader syntax, WebGPU pipeline compilation debugging, shader include preprocessor pipelines.

codeCraft-Ritik avatar Sep 18 '26 04:09 codeCraft-Ritik

Thanks for your investigation!

Can you create a PR for it? It will be easier for us to review and give you feedbacks.

Thanks!

Popov72 avatar Sep 22 '26 17:09 Popov72

Hi @codeCraft-Ritik - Would you have to create a PR by any chance?

thomlucc avatar Sep 29 '26 17:09 thomlucc

Fixed by #18920

Popov72 avatar Oct 13 '26 17:10 Popov72