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.
This commit is contained in:
FredApps committed 2026-11-11 14:40:07 +01:00
1 parent 6f315a679c
commit 1897f901d2
6 files changed
+54 -1

No files matched your search

+4
View File
@@ -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.
Generated
+1
View File
@@ -312,6 +312,7 @@ dependencies = [
"bytemuck",
"pollster 1.0.1",
"rayon",
"naga",
"thiserror 2.0.21",
"web-time",
"wgpu",
+2
View File
@@ -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" }
+4
View File
@@ -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
+10 -1
View File
@@ -156,12 +156,21 @@ fn finite(v: f32) -> bool {
return (bitcast<u32>(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<i32>, repeat: bool, fx: vec4<f32>, tint: vec4<f32>) -> vec4<f32> {
var q = vec2<i32>(x, y);
if repeat {
q = ((q % size) + size) % size;
q = vec2<i32>(wrap(q.x, size.x), wrap(q.y, size.y));
}
q = clamp(q, vec2<i32>(0), size - vec2<i32>(1));
let c = texel(slot, q);
+33
View File
@@ -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.