Viewport: the shader compiles in browsers again, so the web app's 3D view isn't black (#11080) (#11107)
* claim: #11080 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Viewport: the shader compiles in browsers again (#11080) The ambient occlusion turn added in #9335 mixed `<<` and `|` without parentheses. naga accepts that, so the desktop app ran, but browsers' WGSL compiler refuses it: the sc_viewport module was invalid, every pipeline built from it failed, and the web app drew nothing once a design was open. The line gets its parentheses, and a test checks every shader in the repository for the rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -413,7 +413,8 @@ fn fs_ao_blur(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
|
||||
fn ao_turn(pos: vec2<f32>) -> f32 {
|
||||
let x = u32(pos.x) & 3u;
|
||||
let y = u32(pos.y) & 3u;
|
||||
let v = ((x ^ y) & 1u) << 3u | (y & 1u) << 2u | ((x ^ y) >> 1u) << 1u | (y >> 1u);
|
||||
// WGSL won't mix shifts and `|` without parentheses; naga takes it, browsers refuse it (#11080).
|
||||
let v = (((x ^ y) & 1u) << 3u) | ((y & 1u) << 2u) | (((x ^ y) >> 1u) << 1u) | (y >> 1u);
|
||||
return (f32(v) + 0.5) / 16.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,6 +96,98 @@ fn the_hlsl_for_fxc_has_no_arrays_fxc_must_unroll_loops_for() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Browsers compile WGSL with Tint, which keeps a rule of the WGSL grammar that naga doesn't: a
|
||||
/// shift, `&`, `|` and `^` don't mix in one expression without parentheses (`a << 3u | b` is
|
||||
/// refused, `(a << 3u) | b` isn't). A shader that breaks it runs on the desktop and leaves the
|
||||
/// web app's 3D view black (#11080). Every shader in the repository keeps to it.
|
||||
#[test]
|
||||
fn no_shader_mixes_shifts_and_bitwise_operators_without_parentheses() {
|
||||
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
|
||||
let mut shaders = Vec::new();
|
||||
let mut dirs = vec![root.join("assets"), root.join("crates"), root.join("vendor")];
|
||||
while let Some(dir) = dirs.pop() {
|
||||
for entry in ok(std::fs::read_dir(&dir)) {
|
||||
let path = ok(entry).path();
|
||||
if path.is_dir() {
|
||||
if !path.ends_with("target") {
|
||||
dirs.push(path);
|
||||
}
|
||||
} else if path.extension().is_some_and(|e| e == "wgsl") {
|
||||
shaders.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(shaders.iter().any(|p| p.ends_with("assets/shaders/viewport.wgsl")), "{shaders:?}");
|
||||
let mut found = Vec::new();
|
||||
for path in &shaders {
|
||||
for (n, line) in ok(std::fs::read_to_string(path)).lines().enumerate() {
|
||||
if let Some(ops) = mixed_bitwise(line) {
|
||||
found.push(format!("{}:{}: {ops}: {}", path.display(), n + 1, line.trim()));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(found.is_empty(), "{}", found.join("\n"));
|
||||
// The check finds the line #11080 reported, and takes it parenthesized.
|
||||
assert_eq!(mixed_bitwise("let v = ((x ^ y) & 1u) << 3u | (y & 1u) << 2u;").as_deref(), Some("shift and |"));
|
||||
assert_eq!(mixed_bitwise("let v = (((x ^ y) & 1u) << 3u) | ((y & 1u) << 2u);"), None);
|
||||
assert_eq!(mixed_bitwise("let a = f(&p, x & 1u, y | 2u) && b || c; // x << 1u | y"), None);
|
||||
assert_eq!(mixed_bitwise("var<storage> m: array<vec2<f32>>; let b = a & 1u ^ c;").as_deref(), Some("& and ^"));
|
||||
}
|
||||
|
||||
/// The bitwise operators (a shift, `&`, `|`, `^`) that one parenthesized group of a WGSL line
|
||||
/// mixes, when it mixes more than one: what WGSL needs parentheses for. `&&`, `||`, an
|
||||
/// address-of `&` and a `>>` closing two generic brackets (`vec2<f32>>`) aren't counted.
|
||||
fn mixed_bitwise(line: &str) -> Option<String> {
|
||||
let chars: Vec<char> = line.split("//").next().unwrap_or("").chars().collect();
|
||||
let at = |i: usize| chars.get(i).copied().unwrap_or(' ');
|
||||
let mixed = |g: &Vec<&str>| (g.len() > 1).then(|| g.join(" and "));
|
||||
let mut groups: Vec<Vec<&str>> = vec![Vec::new()];
|
||||
let (mut i, mut prev) = (0, ' ');
|
||||
while i < chars.len() {
|
||||
let (c, next) = (at(i), at(i + 1));
|
||||
let mut op = None;
|
||||
match c {
|
||||
'(' | '[' => groups.push(Vec::new()),
|
||||
')' | ']' | ',' | ';' | '{' | '}' => {
|
||||
if let Some(m) = groups.last().and_then(mixed) {
|
||||
return Some(m);
|
||||
}
|
||||
if matches!(c, ')' | ']') && groups.len() > 1 {
|
||||
groups.pop();
|
||||
} else if let Some(g) = groups.last_mut() {
|
||||
g.clear();
|
||||
}
|
||||
}
|
||||
'<' if next == '<' => {
|
||||
op = Some("shift");
|
||||
i += 1;
|
||||
}
|
||||
'>' if next == '>' => {
|
||||
let after = at(i + 2);
|
||||
if after == ' ' || after == '_' || after.is_alphanumeric() {
|
||||
op = Some("shift");
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
'&' | '|' if next == c => i += 1,
|
||||
'&' if prev == '_' || prev == ')' || prev == ']' || prev.is_alphanumeric() => op = Some("&"),
|
||||
'|' => op = Some("|"),
|
||||
'^' => op = Some("^"),
|
||||
_ => {}
|
||||
}
|
||||
if let (Some(op), Some(g)) = (op, groups.last_mut())
|
||||
&& !g.contains(&op)
|
||||
{
|
||||
g.push(op);
|
||||
}
|
||||
if !at(i).is_whitespace() {
|
||||
prev = at(i);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
groups.iter().find_map(mixed)
|
||||
}
|
||||
|
||||
fn ok<T, E: std::fmt::Debug>(r: std::result::Result<T, E>) -> T {
|
||||
match r {
|
||||
Ok(v) => v,
|
||||
|
||||
Reference in New Issue
Block a user