[5.9 Preview][Wasm/WebGPU] Masked materials on VT-lightmapped meshes: black specks (deferred) / white fireflies and bloom discs (forward) along the cut-outs. Cause and a tested 2-line shader fix
Engine / platform: UE 5.9.0-60351404 (Launcher), Wasm target, WebGPU RHI, WGSL shader format, mobile renderer, both web tiers (deferred and forward). Chrome 157, Windows 11, NVIDIA RTX 4080 SUPER. A Win64 (D3D12) package of the same project is clean.
Symptom. Masked materials (opacity mask from a texture alpha, e.g. foliage-style cut-out decals) on static meshes with virtual-textured lightmaps show specks along the cut-out edges: black on the deferred tier; very bright on the forward tier, where bloom turns them into large white discs. Turning off lights, sky light, reflections or GI does not remove them; they remain with only emissive left.
Repro (from any template). r.VirtualTextures=1, r.VirtualTexturedLightmaps=1, static lighting built, a lightmapped mesh with a masked material whose mask is a texture alpha with a fine pattern. Package for Wasm and look at the mask edges in Chrome (forward tier shows it most).
Cause (engine source, 5.9.0)
Engine/Shaders/Private/MobileBasePassPixelShader.usf:406-409: the masked clip (GetMaterialCoverageAndClipping) runs near the top of the shader for every compiler except GLES; :1318-1321 moves it to the end only for COMPILER_GLSL_ES3_1 (an Adreno workaround). COMPILER_WGSL (set at ShaderFormatWGSL.cpp:3777) is tested in neither block.
- The VT lightmap lookup comes after the clip and takes screen derivatives of the lightmap UV:
VirtualTextureCommon.ush:169-180 (MipLevelAniso2D), 0.5f * log2(min(px, py)) / 0.5f * log2(max(px, py)).
- In WGSL the clip becomes
discard; return;, and derivatives in a partly discarded 2x2 quad are undefined (the module also carries diagnostic(off, derivative_uniformity), so nothing warns). A zero or infinite derivative gives -inf - -inf (or inf - inf) = NaN in the mip fraction; the NaN blends the two lightmap mips and flows through the whole indirect term (LightmapCommon.ush:78-129). NaN * 0 stays NaN, which is why no lighting toggle removes it.
- Forward:
SafeGetOutColor (MobileLightingCommon.ush:68-75) does min(OutColor, Max111110BitsFloat3 * 0.5f), which turns NaN into a huge finite value (firefly, then bloom). Deferred has no clamp and shows black.
Desktop BasePassPixelShader.usf does the VT lightmap lookup (:947-953) before its clip (:1005-1012), which explains why Win64 is clean (from source; DXIL not inspected).
Cooked WGSL of an affected forward permutation (measured; condensed excerpt, nothing between the discard and the first dpdx derives):
if (((em.w - 0.3333f) < 0.0f)) { discard; return; }
...
let ev = dpdx(z); let ce = (dpdy(z) * x); // lightmap UV derivatives
let ew = (0.5f * log2(min(cf, cg))); let ch = (0.5f * log2(max(cf, cg)));
Measured (interior scene, 14 fixed camera poses, Win64 package as reference)
- Forward: largest near-white blob on frames with the cut-out decals up to 142,976 px (Win64: 2,064-5,792 px at the same poses). With bloom off and only emissive left, 15,870 px >= 100 in one decal crop.
- Deferred: 385 exact-black (0,0,0) pixels in the same crop (Win64: 0).
- Shader census of our cooked libraries: 266 pixel shaders in 34 shader maps (forward) and 60 in 30 (deferred) have a texture-alpha
discard before a derivative, so other masked cut-outs (labels, logos) have the same pattern; only the decals were frame-checked.
Proposed fix (treat WGSL like GLES; tested by us):
-#if !EARLY_Z_PASS_ONLY_MATERIAL_MASKING && !COMPILER_GLSL_ES3_1
+#if !EARLY_Z_PASS_ONLY_MATERIAL_MASKING && !COMPILER_GLSL_ES3_1 && !COMPILER_WGSL
//Clip if the blend mode requires it.
...
-#if !EARLY_Z_PASS_ONLY_MATERIAL_MASKING && COMPILER_GLSL_ES3_1
+#if !EARLY_Z_PASS_ONLY_MATERIAL_MASKING && (COMPILER_GLSL_ES3_1 || COMPILER_WGSL)
Only MobileBasePassPixelShader.usf changes; we applied it to a copy of the 5.9.0 Launcher engine (shader text only, so no C++ rebuild) and recooked the mobile shaders. Result, same scene, one load per arm, unpatched build loaded in the same session as control:
- Census: clip-before-derivative pixel shaders 266 → 0 (forward), 60 → 0 (deferred).
- Forward emissive-only crop: 15,870 → 0 px. Largest forward blobs 143,120 / 54,144 / 59,456 → 48 / 3,152 / 4,432 px (Win64 2,064 / 3,488 / 5,792).
- Deferred black specks: 135 → 0 px (one crop), 1,086 → 1 px (another pose).
- No regression seen: mean CIEDE2000 vs Win64 7.25 → 6.67 (deferred), 9.45 → 6.35 (forward); no surface missing in a 14-pose eye check; cut-outs keep their shape. Per-pose differences were dominated by camera framing between loads.
Optional hardening: log2(max(..., 1e-30)) in MipLevelAniso2D (catches a zero derivative, not an infinite one), and a NaN-safe SafeGetOutColor (min(max(OutColor, 0), ...)), which would only hide the defect.
Not a good workaround (measured): r.Mobile.EarlyZPass=2 removes the base-pass discard and the specks (15,889 → 0 px), but in our scene several large opaque surfaces disappeared with it (mean CIEDE2000 vs Win64 6.5 → 16.6). We did not investigate why.
Not measured / inferred: the actual derivative value Dawn returns in a partly discarded quad (we infer 0 or Inf; a finite wrong value would only give a wrong mip); Safari / iPadOS, Firefox and Android. Content-side alternatives (translucent cut-outs, or those meshes off VT lightmaps) were not tested.
Happy to share more data. Thank you!