From 1897f901d2cb0c817d61a278d4f32ff88f6aebef Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:40:07 +0100 Subject: [PATCH] fix(gpu): wrap repeating bitmaps without a signed remainder, undefined on GL (M3) The fine shader wrapped a repeating bitmap's taps with ((q % size) + size) % size. naga (30.0.1) writes WGSL's integer % as GLSL's, which is undefined for negative operands, and the bilinear tap left of or above the edge is -1. On the GL backend (llvmpipe in the FreeBSD VM, which has no Vulkan driver) the edge texels came out wrong: bitmap repeated max 79, filtered bitmap max 16, so FreeBSD x86_64 has failed since bitmap fills landed (0dae205). Vulkan, DX12 and Metal define the remainder, so Linux CI passed. wrap() takes the unsigned remainder of |i| instead, defined on every backend. shaders_take_no_signed_remainder parses every shader with naga and fails on a % whose left operand is a signed integer; it fails on fine.wgsl before this. --- CHANGELOG.md | 4 ++++ Cargo.lock | 1 + Cargo.toml | 2 ++ crates/gpu/Cargo.toml | 4 ++++ crates/gpu/src/fine.wgsl | 11 ++++++++++- crates/gpu/src/tests.rs | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f4393c8..064c6305 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* **Repeating bitmap fills on the GL backend** (M3): the GPU stage wrapped a repeating + bitmap with a signed remainder, which GLSL leaves undefined for negative values, so on + OpenGL (llvmpipe in the FreeBSD VM) the texels at the bitmap's left and top edges came out + wrong. It now wraps without one, and a test keeps every shader free of signed remainders. * **Library and Properties polish** (ADR 0053, M2). * **Drag Library items onto the stage**: symbols and bitmaps land at the pointer on the active layer at the playhead, a sound goes onto that layer's keyframe; drag several at once. diff --git a/Cargo.lock b/Cargo.lock index 83ed3ad2..c6dc21ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -312,6 +312,7 @@ dependencies = [ "bytemuck", "pollster 1.0.1", "rayon", + "naga", "thiserror 2.0.21", "web-time", "wgpu", diff --git a/Cargo.toml b/Cargo.toml index 15bb9237..778449e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,8 @@ anicraft-gpu = { path = "crates/gpu" } # The GPU stage (ADR 0019) uses the wgpu that eframe's egui-wgpu brings, with the same # (default) features, so the tree keeps one wgpu build. wgpu = "30" +# The WGSL front end wgpu already builds, for checking the shaders in tests. +naga = { version = "30", features = ["wgsl-in"] } pollster = "1" bytemuck = "1" anicraft-actions = { path = "crates/actions" } diff --git a/crates/gpu/Cargo.toml b/crates/gpu/Cargo.toml index 9a0a5325..925b51b6 100644 --- a/crates/gpu/Cargo.toml +++ b/crates/gpu/Cargo.toml @@ -21,5 +21,9 @@ rayon.workspace = true web-time.workspace = true thiserror.workspace = true +[dev-dependencies] +# The shaders' types, to keep them free of what a backend leaves undefined (wgpu's own naga). +naga.workspace = true + [lints] workspace = true diff --git a/crates/gpu/src/fine.wgsl b/crates/gpu/src/fine.wgsl index 374e5b20..b49a7473 100644 --- a/crates/gpu/src/fine.wgsl +++ b/crates/gpu/src/fine.wgsl @@ -156,12 +156,21 @@ fn finite(v: f32) -> bool { return (bitcast(v) & 0x7f800000u) != 0x7f800000u; } +// Rust's `rem_euclid` for i32 and `n` >= 1, without `%` on a negative operand: naga writes +// WGSL's `%` as GLSL's, which leaves negative operands undefined, so on the GL backend +// (llvmpipe in the FreeBSD VM) the texel left of or above a repeating bitmap's edge, -1, came +// out wrong. The unsigned remainder is defined everywhere. +fn wrap(i: i32, n: i32) -> i32 { + let r = i32(u32(abs(i)) % u32(n)); + return select(r, select(n - r, 0, r == 0), i < 0); +} + // One bitmap tap at integer (x, y) (wrapped when repeating, then clamped to the edges as // `Pixels::at` does), through the colour effect, premultiplied. fn tap(slot: u32, x: i32, y: i32, size: vec2, repeat: bool, fx: vec4, tint: vec4) -> vec4 { var q = vec2(x, y); if repeat { - q = ((q % size) + size) % size; + q = vec2(wrap(q.x, size.x), wrap(q.y, size.y)); } q = clamp(q, vec2(0), size - vec2(1)); let c = texel(slot, q); diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index 214cd49c..bcae9ad4 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -623,6 +623,39 @@ fn degenerate_geometry_bins_without_trouble() { // ----- the real GPU ---------------------------------------------------------------------------- +/// The shaders, as `pipelines` and the stage build them. +const SHADERS: [(&str, &str); 4] = [ + ("fine.wgsl", include_str!("fine.wgsl")), + ("bin.wgsl", include_str!("bin.wgsl")), + ("scan.wgsl", include_str!("scan.wgsl")), + ("filters.wgsl", include_str!("filters.wgsl")), +]; + +#[test] +fn shaders_take_no_signed_remainder() { + // naga writes WGSL's `%` on integers as GLSL's, which is undefined when an operand is + // negative: on the GL backend (llvmpipe in the FreeBSD VM) a repeating bitmap's wrap, + // `((q % size) + size) % size` at q = -1, picked the wrong texel at its edges (bitmap + // repeated: max 79). Unsigned remainders and float ones (written as `a - b * trunc(a / b)`) + // are defined on every backend. + for (name, src) in SHADERS { + let module = naga::front::wgsl::parse_str(src).unwrap_or_else(|e| panic!("{name}: {}", e.emit_to_string(src))); + let info = naga::valid::Validator::new(naga::valid::ValidationFlags::all(), naga::valid::Capabilities::all()) + .validate(&module) + .unwrap_or_else(|e| panic!("{name}: {e:?}")); + let functions = module.functions.iter().map(|(h, f)| (f, &info[h])); + let entries = module.entry_points.iter().enumerate().map(|(i, e)| (&e.function, info.get_entry_point(i))); + for (f, fi) in functions.chain(entries) { + for (h, e) in f.expressions.iter() { + if let naga::Expression::Binary { op: naga::BinaryOperator::Modulo, left, .. } = *e { + let kind = fi[left].ty.inner_with(&module.types).scalar_kind(); + assert_ne!(kind, Some(naga::ScalarKind::Sint), "{name}: `%` on a signed integer in {:?} ({h:?})", f.name); + } + } + } + } +} + /// One device for all tests: the graphics card, or else the software adapter (CI runners have /// lavapipe or WARP); `None` (with the reasons printed) when there is neither. /// `ANICRAFT_TEST_GPU=software` tests on the software adapter even where there is a card.