Clustered lighting + Sheen: `computeClusteredLighting` uses out-of-scope `normalW` (WGSL compile error / GLSL error)

Version: reproduced on 9.32.0, loaded from the CDN the Playground uses, with the WebGPU engine. It was found on 9.29.0, and the same code is on master today.

What happens

A PBRMaterial with sheen.isEnabled = true, lit by a light in a ClusteredLightContainer, fails to compile its fragment shader on WebGPU. In our verified run it failed on frame 4:

WebGPU uncaptured error (1): [object GPUValidationError] - Error while parsing WGSL: :964:41 error: unresolved value 'normalW'
info.sheen=computeSheenLighting(preInfo,normalW,sheenOut.sheenColor,specularEnvironmentR90,AARoughnessFactor,light.vLightDiffuse.rgb);
[Invalid ShaderModule "fragment"] is invalid due to a previous error.
[Invalid RenderPipeline "RenderPipeline_bgra8unorm_depth24plus-stencil8_samples4_textureState1"] is invalid due to a previous error.
[Invalid CommandBuffer from CommandEncoder "[2|2] - RenderEncoder"] is invalid due to a previous error.

Every following frame’s command buffer is rejected. In our app the whole canvas went black (a glTF crystal ball using KHR_materials_sheen, in a scene lit by clustered lights).

Playground (select the WebGPU engine)

export const createScene = function (eng) {
    const engine = eng ?? window.engine;
    const scene = new BABYLON.Scene(engine);
    const camera = new BABYLON.ArcRotateCamera("cam", 0, 1.2, 4, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(true);
    const light = new BABYLON.PointLight("p", new BABYLON.Vector3(0, 2, -2), scene);
    new BABYLON.ClusteredLightContainer("clustered", [light], scene);
    const mat = new BABYLON.PBRMaterial("m", scene);
    mat.metallic = 0;
    mat.roughness = 0.5;
    mat.sheen.isEnabled = true;
    mat.sheen.color = new BABYLON.Color3(1, 0.8, 1);
    BABYLON.MeshBuilder.CreateSphere("s", { diameter: 1.5 }, scene).material = mat;
    return scene;
};

PLAYGROUND LINK: Babylon.js Playground

Cause

In packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrClusteredLightingFunctions.fx (line 121), and the GLSL twin Shaders/ShadersInclude/pbrClusteredLightingFunctions.fx (line 124), the #ifdef SHEEN branch inside computeClusteredLighting(...) calls:

info.sheen = computeSheenLighting(preInfo, normalW, sheenOut.sheenColor, specularEnvironmentR90, AARoughnessFactor, light.vLightDiffuse.rgb);

normalW is a local of the fragment main(), so it is not in scope inside this function. The function’s normal parameter is N, which every other lighting call there already uses (computeSpecularLighting(preInfo, N, ...) and so on).

Fix (one word, in both files)

- info.sheen = computeSheenLighting(preInfo, normalW, sheenOut.sheenColor, specularEnvironmentR90, AARoughnessFactor, light.vLightDiffuse.rgb);
+ info.sheen = computeSheenLighting(preInfo, N, sheenOut.sheenColor, specularEnvironmentR90, AARoughnessFactor, light.vLightDiffuse.rgb);

We ship this as a patch-package patch in production, and it resolves the error: the sheen material compiles and renders under clustered lights.

PR: Clustered lighting: sheen uses the out-of-scope normalW (WGSL/GLSL compile error) by increasinglyHuman · Pull Request #19049 · BabylonJS/Babylon.js · GitHub

cc @Evgeni_Popov

I commented on the PR.