From 01bdf39f1b0bbd3b8cf19962ca3b70ca8df09920 Mon Sep 17 00:00:00 2001 From: khhjoe Date: Sat, 10 Oct 2026 04:22:30 +0900 Subject: [PATCH] gpu: write F32_MAX exactly so the compositor shader builds on WebGPU (#1982) The decimal 3.40282347e38 is just above f32::MAX; naga rounds it but browsers' Tint rejects it, so the web app had no GPU compositor. Now a hex literal, with a test that every decimal float literal in the shader fits f32. Co-Authored-By: Claude Opus 5.5 --- crates/gpu/src/compose.wgsl | 4 ++- crates/gpu/src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 1d6c019b7e..91b36920f1 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -153,7 +153,9 @@ fn vivid_light_generic(cb: f32, cs: f32) -> f32 { return min(cb / (2.0 * (1.0 - cs)), 1.0); } -const F32_MAX: f32 = 3.40282347e38; +// f32::MAX exactly. The decimal 3.40282347e38 is just above it: naga rounds it down, but browsers' +// WebGPU compiler rejects it, which left the web app without a GPU compositor. +const F32_MAX: f32 = 0x1.fffffep+127f; fn blend_channel(mode: i32, cb: f32, cs: f32) -> f32 { // Linear Dodge / Divide clip at 1 at integer depths, at f32::MAX in 32-bit documents. diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index fe92d30c28..a656892cfa 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -2019,6 +2019,61 @@ mod tests { } } + /// The decimal float literals in `src` (comments skipped; hex literals are exact, so skipped), + /// with their line numbers. + fn decimal_float_literals(src: &str) -> Vec<(usize, String)> { + let mut out = Vec::new(); + for (n, line) in src.lines().enumerate() { + let code = line.split("//").next().unwrap_or("").as_bytes(); + let mut i = 0; + while i < code.len() { + let digit_or_dot = code[i].is_ascii_digit() || (code[i] == b'.' && code.get(i + 1).is_some_and(u8::is_ascii_digit)); + let word_before = i > 0 && (code[i - 1].is_ascii_alphanumeric() || code[i - 1] == b'_' || code[i - 1] == b'.'); + if !digit_or_dot || word_before { + i += 1; + continue; + } + if code[i] == b'0' && matches!(code.get(i + 1), Some(b'x' | b'X')) { + i += 2; + while i < code.len() && (code[i].is_ascii_hexdigit() || matches!(code[i], b'.' | b'p' | b'P' | b'+' | b'-')) { + i += 1; + } + continue; + } + let start = i; + while i < code.len() && (code[i].is_ascii_digit() || code[i] == b'.') { + i += 1; + } + if i < code.len() && matches!(code[i], b'e' | b'E') { + let mut j = i + 1; + if j < code.len() && matches!(code[j], b'+' | b'-') { + j += 1; + } + if j < code.len() && code[j].is_ascii_digit() { + i = j; + while i < code.len() && code[i].is_ascii_digit() { + i += 1; + } + } + } + out.push((n + 1, String::from_utf8_lossy(&code[start..i]).into_owned())); + } + } + out + } + + /// Browsers' WebGPU shader compiler (Tint) rejects a float literal beyond f32's finite range + /// even when it would round to `f32::MAX` (naga, used natively, rounds it), and then no + /// compositor pipeline builds and the web app falls back to the CPU renderer. `3.40282347e38` + /// was such a literal. + #[test] + fn shader_float_literals_fit_f32() { + let lits = decimal_float_literals(SHADER); + assert!(lits.iter().any(|(_, l)| l == "1.0"), "the scanner finds literals: {lits:?}"); + let bad: Vec<_> = lits.iter().filter(|(_, l)| l.parse::().map_or(true, |v| !v.is_finite() || v.abs() > f64::from(f32::MAX))).collect(); + assert!(bad.is_empty(), "float literals outside f32's finite range (line, literal): {bad:?}"); + } + #[test] fn op_record_fits_the_uniform() { let p = plan::Pass::new(Kernel::FxPaint, 0);