diff --git a/crates/gpu/src/binning.rs b/crates/gpu/src/binning.rs index 99fd546e..d7be84fe 100644 --- a/crates/gpu/src/binning.rs +++ b/crates/gpu/src/binning.rs @@ -16,12 +16,17 @@ //! a group opens before its first fill in the tile and closes (blending into what is below at //! its alpha and in its blend mode) when its run ends, as the oracle's band compositor does per //! band. +//! +//! Bitmap fills (ADR 0031) take one of [`MAX_IMAGES`] texture slots per dispatch, one per +//! distinct image; a filtered group's layer is drawn and filtered by the GPU before the pass and +//! copied into the paint buffer after the gradient tables, where its fill reads it. -use anicraft_doc::BlendMode; +use anicraft_doc::{BlendMode, Pixels}; use anicraft_geom::{FillRule, Vec2}; use anicraft_render::paint::Shader; use anicraft_render::{Fill, NeedsFull, Scene}; use rayon::prelude::*; +use std::sync::Arc; /// A `vec4` as the shader sees it. type Vec4 = [f32; 4]; @@ -41,10 +46,16 @@ pub const MAX_GROUP_DEPTH: usize = 8; /// artwork per clip switch). pub const MAX_CLIP_DEPTH: usize = 16; +/// Distinct bitmaps one dispatch samples (texture slots in `fine.wgsl`; every device has at +/// least 16 sampled textures per shader stage). +pub const MAX_IMAGES: usize = 16; + /// Paint kinds in an op header. const SOLID: u32 = 0; const LINEAR: u32 = 1; const RADIAL: u32 = 2; +const BITMAP: u32 = 3; +const IMAGE: u32 = 4; /// Op header flag: the fill is drawn inside the current clip. pub const CLIPPED: u32 = 1; @@ -91,8 +102,9 @@ pub fn blend_code(m: BlendMode) -> u32 { /// paints, flags]`. /// * `geo` (`vec4`): pieces `(x0, y0, x1, y1)` relative to their tile's top-left, in the /// edge's direction; then backdrops, four `vec4`s (16 rows) each. -/// * `paints` (`vec4`): per op its colour, then the inverse paint transform and gradient -/// parameters; then the gradient tables (256 premultiplied colours each). +/// * `paints` (`vec4`): per op its colour, then the inverse paint transform and gradient, +/// bitmap or layer parameters; then the gradient tables (256 premultiplied colours each); +/// then (filled in on the GPU, [`Binned::layer_at`]) the filtered layers' pixels. /// /// Ops are the scene's fills in drawing order, then every clip's artwork, clip by clip. pub struct Binned { @@ -103,6 +115,13 @@ pub struct Binned { pub ops_at: u32, pub geo: Vec<[f32; 4]>, pub paints: Vec<[f32; 4]>, + /// The paint buffer's whole length in `vec4`s: `paints`, then room for the layers. + pub paints_len: usize, + /// Per scene layer, where its pixels go in the paint buffer (`vec4`s); `None` when no fill + /// shows it (then it isn't drawn). + pub layer_at: Vec>, + /// The bitmaps, in texture-slot order. + pub images: Vec>, /// Entries (fills, artwork and control) and edge pieces in all tiles. pub entries: usize, pub pieces: usize, @@ -155,7 +174,8 @@ struct RowOut { } /// Bins `scene` for a `width × height` surface. `Err` when its nesting is deeper than the -/// shader holds ([`MAX_GROUP_DEPTH`], [`MAX_CLIP_DEPTH`]); the caller draws it on the CPU. +/// shader holds ([`MAX_GROUP_DEPTH`], [`MAX_CLIP_DEPTH`]) or it has more bitmaps than +/// [`MAX_IMAGES`]; the caller draws it on the CPU. pub fn bin(scene: &Scene, width: u32, height: u32) -> Result { if scene.groups.iter().any(|g| g.chain.len() > MAX_GROUP_DEPTH) { return Err(NeedsFull::Groups); @@ -272,30 +292,70 @@ pub fn bin(scene: &Scene, width: u32, height: u32) -> Result // Tens of thousands of small buffers: free them on the pool, off the frame's path. rayon::spawn(move || drop((locals, rows))); - // Paints: a fixed record per op, then the gradient tables. + // Paints: a fixed record per op, then the gradient tables, then the layers' pixels. let mut paints: Vec<[f32; 4]> = Vec::with_capacity(PAINT_VEC4S * ops.len()); let mut ramps: Vec<[f32; 4]> = Vec::new(); + let mut images: Vec> = Vec::new(); let ramps_at = (PAINT_VEC4S * ops.len()) as u32; + // Where each layer shown by a fill goes, after the tables (the tables' size is known first). + let table_len: usize = ops.iter().filter(|f| matches!(f.shader, Shader::Linear { .. } | Shader::Radial { .. }) && f.layer.is_none()).count() * RAMP; + let mut layer_at: Vec> = vec![None; scene.layers.len()]; + let mut cursor = PAINT_VEC4S * ops.len() + table_len; + for f in &ops { + let Some(l) = f.layer else { continue }; + let (Some(slot), Some(layer)) = (layer_at.get_mut(l as usize), scene.layers.get(l as usize)) else { continue }; + if slot.is_none() { + *slot = Some(cursor as u32); + cursor += layer.bounds.width() * layer.bounds.height(); + } + } + if cursor >= u32::MAX as usize { + return Err(NeedsFull::Filters); + } + let paints_len = cursor; + let u = f32::from_bits; for (op, f) in ops.iter().enumerate() { let rule = match f.rule { FillRule::NonZero => 0, FillRule::EvenOdd => 1, }; let ramp = ramps_at + ramps.len() as u32; - let (kind, rec) = match &f.shader { - Shader::Solid(c) => (SOLID, [*c, [0.0; 4], [0.0; 4], [0.0; 4]]), - Shader::Linear { inv, start, dir, lut } => { + let layer = f.layer.and_then(|l| Some((layer_at.get(l as usize).copied().flatten()?, scene.layers.get(l as usize)?.bounds))); + let (kind, rec) = match (&f.shader, layer) { + // A layer's pixels at `at`, `w × h`, its top-left at (x0, y0) in surface pixels. + (_, Some((at, b))) => (IMAGE, [[u(at), u(b.width() as u32), u(b.height() as u32), 0.0], [u(b.x0 as u32), u(b.y0 as u32), 0.0, 0.0], [0.0; 4], [0.0; 4]]), + (_, None) if f.layer.is_some() => (SOLID, [[0.0; 4]; 4]), + (Shader::Solid(c), _) => (SOLID, [*c, [0.0; 4], [0.0; 4], [0.0; 4]]), + (Shader::Linear { inv, start, dir, lut }, _) => { ramps.extend_from_slice(&lut[..]); let m = inv.m; (LINEAR, [[0.0; 4], [m[0], m[1], m[2], m[3]], [m[4], m[5], start.x, start.y], [dir.x, dir.y, 0.0, 0.0]]) } - Shader::Radial { inv, center, inv_radius, lut } => { + (Shader::Radial { inv, center, inv_radius, lut }, _) => { ramps.extend_from_slice(&lut[..]); let m = inv.m; (RADIAL, [[0.0; 4], [m[0], m[1], m[2], m[3]], [m[4], m[5], center.x, center.y], [0.0, 0.0, *inv_radius, 0.0]]) } + // The oracle's bilinear sample of a bitmap (crates/render/src/paint.rs › sample). + (Shader::Bitmap { inv, pixels, repeat, fx }, _) => { + if pixels.width == 0 || pixels.height == 0 { + (SOLID, [[0.0; 4]; 4]) + } else { + let slot = match images.iter().position(|p| Arc::ptr_eq(p, pixels)) { + Some(i) => i, + None if images.len() < MAX_IMAGES => { + images.push(pixels.clone()); + images.len() - 1 + } + None => return Err(NeedsFull::Bitmaps), + }; + let m = inv.m; + let (k, alpha, tint, keep) = colour_effect(fx); + (BITMAP, [[u(slot as u32), u(u32::from(*repeat)), u(pixels.width), u(pixels.height)], [m[0], m[1], m[2], m[3]], [m[4], m[5], k, alpha], [tint[0], tint[1], tint[2], u(keep)]]) + } + } // `prepare_scene` never hands these over; draw nothing rather than guess. - Shader::Bitmap { .. } | Shader::Image { .. } => (SOLID, [[0.0; 4]; 4]), + (Shader::Image { .. }, _) => (SOLID, [[0.0; 4]; 4]), }; paints.extend_from_slice(&rec); let flags = if op >= nf { @@ -311,8 +371,34 @@ pub fn bin(scene: &Scene, width: u32, height: u32) -> Result } } paints.extend_from_slice(&ramps); + if paints.len() != PAINT_VEC4S * ops.len() + table_len { + // The tables came out another size than counted; never let a layer overlap them. + return Err(NeedsFull::Filters); + } - Ok(Binned { tiles_x, tiles_y, index, entries_at: entries_at as u32, ops_at: ops_at as u32, geo, paints, entries: total, pieces, work }) + Ok(Binned { tiles_x, tiles_y, index, entries_at: entries_at as u32, ops_at: ops_at as u32, geo, paints, paints_len, layer_at, images, entries: total, pieces, work }) +} + +/// A colour effect as the shader applies it to each bitmap tap, the same arithmetic as +/// [`anicraft_doc::ColorEffect::apply`] then `premultiply`'s sanitizing: `(tint amount, alpha, +/// tint, kept channels)`. Each straight channel `c` becomes `c + (tint − c)·k`, alpha `a·alpha`, +/// clamped to `0..=1`. A NaN the oracle would carry into a channel (and its sanitizing turn +/// into 0) is a channel left out of `kept` (bit per channel, red first) instead. +fn colour_effect(fx: &anicraft_doc::ColorEffect) -> (f32, f32, [f32; 3], u32) { + let k = fx.tint_amount.clamp(0.0, 1.0); + let alpha = fx.alpha.clamp(0.0, 1.0); + let t = [fx.tint.r, fx.tint.g, fx.tint.b]; + let mut keep = 0u32; + let mut tint = [0.0f32; 3]; + for (i, (&v, out)) in t.iter().zip(tint.iter_mut()).enumerate() { + if v.is_finite() && k.is_finite() { + keep |= 1 << i; + *out = v; + } + } + // A NaN opacity makes every channel 0 (premultiplied by a sanitized alpha of 0). + let alpha = if alpha.is_finite() { alpha } else { 0.0 }; + (if k.is_finite() { k } else { 0.0 }, alpha, tint, keep) } /// Tile row `r`'s streams: per tile, its fills in drawing order with the clip and group diff --git a/crates/gpu/src/filters.wgsl b/crates/gpu/src/filters.wgsl new file mode 100644 index 00000000..813bfc04 --- /dev/null +++ b/crates/gpu/src/filters.wgsl @@ -0,0 +1,309 @@ +// AniCraft GPU stage: filters on a filtered group's layer (ADR 0031). +// +// The same steps as the CPU oracle's filters (crates/render/src/filters.rs), resolved by it +// (`filters::resolve`: box widths, whole-pixel offsets, premultiplied colours, ramp tables, the +// colour matrix), in f32 on premultiplied colour: +// +// * `box4` / `box1`: one box-blur pass along rows or columns, one invocation per line with the +// oracle's running sum (an even width covers one more pixel each side at half weight), from +// one buffer half or coverage slot into another. Outside the layer is transparent. +// * `coverage`: the layer's opacity (or its transparency) moved by (dx, dy) into a slot. +// * `shadow`, `bevel`, `gradient_glow`: compose the blurred coverage with the layer, per pixel. +// * `adjust`: the colour matrix on unpremultiplied colour. +// +// `img` holds two `w × h` halves (the layer is half 0); `cov` three `w × h` coverage slots. + +struct Params { + // width, height, box width, 1 along rows / 0 along columns + size: vec4, + // source, destination (halves or slots), flags, side (0 inner, 1 outer, 2 full) + io: vec4, + // dx, dy + offset: vec4, + // shadow colour (straight) or bevel highlight (premultiplied) + c0: vec4, + // bevel shadow (premultiplied) + c1: vec4, + // strength + k: vec4, + // the colour matrix's rows (r, g, b, a: columns r, g, b, a), then its offsets + m: array, 5>, +} + +@group(0) @binding(0) var params: Params; +@group(0) @binding(1) var img: array>; +@group(0) @binding(2) var cov: array; +@group(0) @binding(3) var ramp: array>; + +const INNER: u32 = 1u; +const KNOCKOUT: u32 = 2u; +const HIDE: u32 = 4u; +const INVERSE: u32 = 8u; +const RAMP: u32 = 16u; +const SIDE_INNER: u32 = 0u; +const SIDE_OUTER: u32 = 1u; +// Ramp table steps (filters.rs › RAMP_STEPS). +const RAMP_STEPS: f32 = 256.0; + +fn npx() -> u32 { + return params.size.x * params.size.y; +} + +// Pixel (x, y) of the layer, transparent outside. +fn alpha_at(x: i32, y: i32) -> f32 { + let w = i32(params.size.x); + let h = i32(params.size.y); + if x < 0 || y < 0 || x >= w || y >= h { + return 0.0; + } + return img[u32(y * w + x)].w; +} + +fn cov_at(slot: u32, x: i32, y: i32) -> f32 { + let w = i32(params.size.x); + let h = i32(params.size.y); + if x < 0 || y < 0 || x >= w || y >= h { + return 0.0; + } + return cov[slot * npx() + u32(y * w + x)]; +} + +// The line `line` and element `i` of a box pass, as an index into a `w × h` buffer. +fn line_index(line: u32, i: u32) -> u32 { + if params.size.w == 1u { + return line * params.size.x + i; + } + return i * params.size.x + line; +} + +fn lines() -> vec2 { + // (lines, length) + if params.size.w == 1u { + return vec2(params.size.y, params.size.x); + } + return vec2(params.size.x, params.size.y); +} + +@compute @workgroup_size(64) +fn box4(@builtin(global_invocation_id) gid: vec3) { + let ll = lines(); + let line = gid.x; + if line >= ll.x { + return; + } + let len = ll.y; + let src = params.io.x * npx(); + let dst = params.io.y * npx(); + let n = params.size.z; + if n <= 1u { + for (var i = 0u; i < len; i++) { + img[dst + line_index(line, i)] = img[src + line_index(line, i)]; + } + return; + } + let r = n / 2u; + let even = (n % 2u) == 0u; + let inv = 1.0 / f32(n); + var sum = vec4(0.0); + for (var i = 0u; i <= min(r, len - 1u); i++) { + sum += img[src + line_index(line, i)]; + } + for (var i = 0u; i < len; i++) { + var ends = vec4(0.0); + if even { + var lo = vec4(0.0); + var hi = vec4(0.0); + if i >= r { + lo = img[src + line_index(line, i - r)]; + } + if i + r < len { + hi = img[src + line_index(line, i + r)]; + } + ends = 0.5 * (lo + hi); + } + img[dst + line_index(line, i)] = (sum - ends) * inv; + if i + r + 1u < len { + sum += img[src + line_index(line, i + r + 1u)]; + } + if i >= r { + sum -= img[src + line_index(line, i - r)]; + } + } +} + +@compute @workgroup_size(64) +fn box1(@builtin(global_invocation_id) gid: vec3) { + let ll = lines(); + let line = gid.x; + if line >= ll.x { + return; + } + let len = ll.y; + let src = params.io.x * npx(); + let dst = params.io.y * npx(); + let n = params.size.z; + if n <= 1u { + for (var i = 0u; i < len; i++) { + cov[dst + line_index(line, i)] = cov[src + line_index(line, i)]; + } + return; + } + let r = n / 2u; + let even = (n % 2u) == 0u; + let inv = 1.0 / f32(n); + var sum = 0.0; + for (var i = 0u; i <= min(r, len - 1u); i++) { + sum += cov[src + line_index(line, i)]; + } + for (var i = 0u; i < len; i++) { + var ends = 0.0; + if even { + var lo = 0.0; + var hi = 0.0; + if i >= r { + lo = cov[src + line_index(line, i - r)]; + } + if i + r < len { + hi = cov[src + line_index(line, i + r)]; + } + ends = 0.5 * (lo + hi); + } + cov[dst + line_index(line, i)] = (sum - ends) * inv; + if i + r + 1u < len { + sum += cov[src + line_index(line, i + r + 1u)]; + } + if i >= r { + sum -= cov[src + line_index(line, i - r)]; + } + } +} + +// Pixel (x, y) of a per-pixel pass, or none past the layer's edge. +fn here(gid: vec3) -> bool { + return gid.x < params.size.x && gid.y < params.size.y; +} + +@compute @workgroup_size(16, 16) +fn coverage(@builtin(global_invocation_id) gid: vec3) { + if !here(gid) { + return; + } + let a = alpha_at(i32(gid.x) - params.offset.x, i32(gid.y) - params.offset.y); + let i = gid.y * params.size.x + gid.x; + cov[params.io.y * npx() + i] = select(a, 1.0 - a, (params.io.z & INVERSE) != 0u); +} + +@compute @workgroup_size(16, 16) +fn shadow(@builtin(global_invocation_id) gid: vec3) { + if !here(gid) { + return; + } + let i = gid.y * params.size.x + gid.x; + let flags = params.io.z; + let c = params.c0; + let sa = clamp(cov[i] * params.k.x, 0.0, 1.0) * c.a; + let src = img[i]; + let ba = src.a; + var out: vec4; + if (flags & INNER) != 0u { + // Inside the artwork, over it. + let a = sa * ba; + if (flags & KNOCKOUT) != 0u { + out = vec4(c.rgb * a, a); + } else { + out = vec4(c.rgb * a + src.rgb * (1.0 - sa), ba); + } + } else if (flags & HIDE) != 0u { + out = vec4(c.rgb * sa, sa); + } else if (flags & KNOCKOUT) != 0u { + let a = sa * (1.0 - ba); + out = vec4(c.rgb * a, a); + } else { + // Under the artwork. + let k = 1.0 - ba; + out = vec4(src.rgb + c.rgb * sa * k, ba + sa * k); + } + img[i] = out; +} + +fn ramp_at(t: f32) -> vec4 { + // Round half up, as Rust's `round` does for t >= 0. + let i = u32(floor(clamp(t, 0.0, 1.0) * RAMP_STEPS + 0.5)); + return ramp[min(i, u32(RAMP_STEPS))]; +} + +// An effect with the artwork: `ei` atop it (inside its edge), `eo` under it (outside), as the +// side says; Knockout keeps only the effect (filters.rs › composite). +fn compose(src: vec4, inner: vec4, outer: vec4) -> vec4 { + let side = params.io.w; + let ei = select(inner, vec4(0.0), side == SIDE_OUTER); + let eo = select(outer, vec4(0.0), side == SIDE_INNER); + let ba = src.a; + if (params.io.z & KNOCKOUT) != 0u { + return ei * ba + eo * (1.0 - ba); + } + return ei * ba + src * (1.0 - ei.a) + eo * (1.0 - ba); +} + +@compute @workgroup_size(16, 16) +fn bevel(@builtin(global_invocation_id) gid: vec3) { + if !here(gid) { + return; + } + let x = i32(gid.x); + let y = i32(gid.y); + let dx = params.offset.x; + let dy = params.offset.y; + let d = clamp(cov_at(0u, x + dx, y + dy) - cov_at(0u, x - dx, y - dy), -1.0, 1.0); + let v = clamp(d * params.k.x, -1.0, 1.0); + var effect: vec4; + if (params.io.z & RAMP) != 0u { + effect = ramp_at(0.5 - 0.5 * v); + } else if v >= 0.0 { + effect = params.c0 * v; + } else { + effect = params.c1 * -v; + } + let i = gid.y * params.size.x + gid.x; + img[i] = compose(img[i], effect, effect); +} + +@compute @workgroup_size(16, 16) +fn gradient_glow(@builtin(global_invocation_id) gid: vec3) { + if !here(gid) { + return; + } + let i = gid.y * params.size.x + gid.x; + let side = params.io.w; + var ei = vec4(0.0); + var eo = vec4(0.0); + if side != SIDE_OUTER { + ei = ramp_at(clamp(cov[i] * params.k.x, 0.0, 1.0)); + } + if side != SIDE_INNER { + eo = ramp_at(clamp(cov[npx() + i] * params.k.x, 0.0, 1.0)); + } + img[i] = compose(img[i], ei, eo); +} + +@compute @workgroup_size(16, 16) +fn adjust(@builtin(global_invocation_id) gid: vec3) { + if !here(gid) { + return; + } + let i = gid.y * params.size.x + gid.x; + let p = img[i]; + let a = p.a; + if a <= 1e-6 { + return; + } + let r = p.r / a; + let g = p.g / a; + let b = p.b / a; + var o: array; + for (var row = 0u; row < 4u; row++) { + o[row] = clamp(params.m[0][row] * r + params.m[1][row] * g + params.m[2][row] * b + params.m[3][row] * a + params.m[4][row], 0.0, 1.0); + } + let out_a = o[3]; + img[i] = vec4(o[0] * out_a, o[1] * out_a, o[2] * out_a, out_a); +} diff --git a/crates/gpu/src/fine.wgsl b/crates/gpu/src/fine.wgsl index db41909f..ebfadd5f 100644 --- a/crates/gpu/src/fine.wgsl +++ b/crates/gpu/src/fine.wgsl @@ -13,6 +13,12 @@ // so far and continues on transparent, and its close blends it into the colour below at the // group's alpha, in its blend mode (the W3C separable formulas, as the oracle's `blend`). // +// Bitmap fills (ADR 0031) sample one of 16 texture slots (straight RGBA8, one texture per +// image) with the oracle's bilinear filter, colour effect and repeat rules +// (crates/render/src/paint.rs › sample), by hand: hardware filtering rounds its weights. A +// filtered group's layer (drawn and filtered by filters.wgsl) sits in `paints` after the gradient +// tables, and its fill reads it pixel for pixel. +// // Two entry points: `main` writes a storage buffer (premultiplied RGBA8 or f32, for readback), // `main_texture` writes premultiplied RGBA8 into a texture the window shows directly. @@ -31,6 +37,22 @@ struct Params { @group(0) @binding(3) var paints: array>; @group(0) @binding(4) var out: array; @group(0) @binding(5) var out_texture: texture_storage_2d; +@group(0) @binding(6) var img0: texture_2d; +@group(0) @binding(7) var img1: texture_2d; +@group(0) @binding(8) var img2: texture_2d; +@group(0) @binding(9) var img3: texture_2d; +@group(0) @binding(10) var img4: texture_2d; +@group(0) @binding(11) var img5: texture_2d; +@group(0) @binding(12) var img6: texture_2d; +@group(0) @binding(13) var img7: texture_2d; +@group(0) @binding(14) var img8: texture_2d; +@group(0) @binding(15) var img9: texture_2d; +@group(0) @binding(16) var img10: texture_2d; +@group(0) @binding(17) var img11: texture_2d; +@group(0) @binding(18) var img12: texture_2d; +@group(0) @binding(19) var img13: texture_2d; +@group(0) @binding(20) var img14: texture_2d; +@group(0) @binding(21) var img15: texture_2d; const NONE: u32 = 0xffffffffu; const PAINT_VEC4S: u32 = 4u; @@ -88,11 +110,121 @@ fn fold(w: f32, rule: u32) -> f32 { return select(t, 2.0 - t, t > 1.0); } -fn shade(op: u32, kind: u32, ramp: u32, p: vec2) -> vec4 { +fn texel(slot: u32, p: vec2) -> vec4 { + switch slot { + case 0u: { return textureLoad(img0, p, 0); } + case 1u: { return textureLoad(img1, p, 0); } + case 2u: { return textureLoad(img2, p, 0); } + case 3u: { return textureLoad(img3, p, 0); } + case 4u: { return textureLoad(img4, p, 0); } + case 5u: { return textureLoad(img5, p, 0); } + case 6u: { return textureLoad(img6, p, 0); } + case 7u: { return textureLoad(img7, p, 0); } + case 8u: { return textureLoad(img8, p, 0); } + case 9u: { return textureLoad(img9, p, 0); } + case 10u: { return textureLoad(img10, p, 0); } + case 11u: { return textureLoad(img11, p, 0); } + case 12u: { return textureLoad(img12, p, 0); } + case 13u: { return textureLoad(img13, p, 0); } + case 14u: { return textureLoad(img14, p, 0); } + case 15u: { return textureLoad(img15, p, 0); } + default: { return vec4(0.0); } + } +} + +// Finite (not NaN or infinite), from the bits: comparisons with NaN may be optimized away. +fn finite(v: f32) -> bool { + return (bitcast(v) & 0x7f800000u) != 0x7f800000u; +} + +// 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 = clamp(q, vec2(0), size - vec2(1)); + let c = texel(slot, q); + let keep = bitcast(tint.w); + let k = fx.z; + var rgb = clamp(c.rgb + (tint.rgb - c.rgb) * k, vec3(0.0), vec3(1.0)); + rgb = select(vec3(0.0), rgb, vec3((keep & 1u) != 0u, (keep & 2u) != 0u, (keep & 4u) != 0u)); + let a = clamp(c.a * fx.w, 0.0, 1.0); + return vec4(rgb * a, a); +} + +// Rust's `rem_euclid` for f32. +fn rem_euclid(u: f32, n: f32) -> f32 { + let r = u - n * trunc(u / n); + return select(r, r + n, r < 0.0); +} + +// The oracle's bilinear sample of a bitmap at pixel centre `p` (paint.rs › sample). +fn bitmap(base: u32, p: vec2) -> vec4 { + let h0 = paints[base]; + let m = paints[base + 1u]; + let n = paints[base + 2u]; + let tint = paints[base + 3u]; + let slot = bitcast(h0.x); + let repeat = bitcast(h0.y) != 0u; + let size = vec2(i32(bitcast(h0.z)), i32(bitcast(h0.w))); + let w = f32(size.x); + let h = f32(size.y); + var u = m.x * p.x + m.z * p.y + n.x; + var v = m.y * p.x + m.w * p.y + n.y; + if !(finite(u) && finite(v)) || w < 1.0 || h < 1.0 { + return vec4(0.0); + } + if repeat { + u = rem_euclid(u, w); + v = rem_euclid(v, h); + } else if u < 0.0 || v < 0.0 || u >= w || v >= h { + return vec4(0.0); + } + let fx_ = u - 0.5; + let fy_ = v - 0.5; + let x0 = floor(fx_); + let y0 = floor(fy_); + let tx = fx_ - x0; + let ty = fy_ - y0; + let xi = i32(x0); + let yi = i32(y0); + let a = tap(slot, xi, yi, size, repeat, n, tint); + let b = tap(slot, xi + 1, yi, size, repeat, n, tint); + let c = tap(slot, xi, yi + 1, size, repeat, n, tint); + let d = tap(slot, xi + 1, yi + 1, size, repeat, n, tint); + let top = a + (b - a) * tx; + let bottom = c + (d - c) * tx; + return top + (bottom - top) * ty; +} + +// A filtered layer's pixel at (x, y): `w × h` premultiplied colours at `at` in `paints`, its +// top-left at (x0, y0); transparent outside (paint.rs › Shader::Image). +fn layer(base: u32, x: u32, y: u32) -> vec4 { + let h0 = paints[base]; + let o = paints[base + 1u]; + let dx = i32(x) - bitcast(o.x); + let dy = i32(y) - bitcast(o.y); + let w = i32(bitcast(h0.y)); + let h = i32(bitcast(h0.z)); + if dx < 0 || dy < 0 || dx >= w || dy >= h { + return vec4(0.0); + } + return paints[bitcast(h0.x) + u32(dy * w + dx)]; +} + +fn shade(op: u32, kind: u32, ramp: u32, p: vec2, x: u32, y: u32) -> vec4 { let base = op * PAINT_VEC4S; if kind == 0u { return paints[base]; } + if kind == 3u { + return bitmap(base, p); + } + if kind == 4u { + return layer(base, x, y); + } let m = paints[base + 1u]; let n = paints[base + 2u]; let g = paints[base + 3u]; @@ -246,7 +378,7 @@ fn pixel(wg: vec3, lid: vec3) -> vec4 { if cov <= 1e-4 { continue; } - let s = shade(op, index[h], index[h + 2u], centre); + let s = shade(op, index[h], index[h + 2u], centre, x, y); c = s * cov + c * (1.0 - s.a * cov); } return c; diff --git a/crates/gpu/src/images.rs b/crates/gpu/src/images.rs new file mode 100644 index 00000000..fa626500 --- /dev/null +++ b/crates/gpu/src/images.rs @@ -0,0 +1,114 @@ +//! Bitmaps on the GPU (ADR 0031): each image is uploaded once, as a straight RGBA8 texture, and +//! kept for later frames under a memory budget (least recently used first out). +//! +//! The key is the image itself (the `Arc` the display list holds, compared by pointer, +//! and kept alive by the cache so its address can't be reused): a replaced or re-imported +//! bitmap is a new image and never shows a stale texture, and two open documents with the same +//! bitmap id never share one. Textures are immutable once uploaded, so an entry evicted while a +//! submitted frame still samples it stays valid for that frame (wgpu keeps it alive). + +use crate::{GpuError, describe}; +use anicraft_doc::Pixels; +use anicraft_render::NeedsFull; +use std::collections::HashMap; +use std::sync::Arc; + +/// Graphics memory the cached bitmaps may hold (4 bytes a pixel). One frame may need more (its +/// own images are never evicted while it binds them); the rest go first. +pub const IMAGE_BUDGET: u64 = 256 << 20; + +struct Entry { + /// Kept so the pointer stays this image's for as long as the entry lives. + pixels: Arc, + view: wgpu::TextureView, + bytes: u64, + used: u64, +} + +#[derive(Default)] +pub(crate) struct Images { + map: HashMap, + bytes: u64, + clock: u64, +} + +impl Images { + /// Images and bytes held. + pub(crate) fn held(&self) -> (usize, u64) { + (self.map.len(), self.bytes) + } + + /// Texture views for `want` (in order), uploading what isn't cached yet. + pub(crate) fn views(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, max_edge: u32, want: &[Arc]) -> Result, GpuError> { + self.clock += 1; + let now = self.clock; + let mut out = Vec::with_capacity(want.len()); + for px in want { + let key = Arc::as_ptr(px) as usize; + if let Some(e) = self.map.get_mut(&key).filter(|e| Arc::ptr_eq(&e.pixels, px)) { + e.used = now; + out.push(e.view.clone()); + continue; + } + let (view, bytes) = upload(device, queue, max_edge, px)?; + self.bytes += bytes; + out.push(view.clone()); + self.map.insert(key, Entry { pixels: px.clone(), view, bytes, used: now }); + } + // Over budget: the least recently used first, never what this frame binds. + while self.bytes > IMAGE_BUDGET { + let Some((&key, _)) = self.map.iter().filter(|(_, e)| e.used != now).min_by_key(|(_, e)| e.used) else { break }; + if let Some(e) = self.map.remove(&key) { + self.bytes = self.bytes.saturating_sub(e.bytes); + } + } + Ok(out) + } +} + +/// One image as a texture, in its own error scopes: a failed upload is never cached. +fn upload(device: &wgpu::Device, queue: &wgpu::Queue, max_edge: u32, px: &Pixels) -> Result<(wgpu::TextureView, u64), GpuError> { + let (w, h) = (px.width, px.height); + if w == 0 || h == 0 || w > max_edge || h > max_edge { + return Err(GpuError::NeedsFull(NeedsFull::Bitmaps)); + } + let len = w as usize * h as usize * 4; + // A short buffer reads as transparent past its end, as `Pixels::at` does. + let padded; + let bytes: &[u8] = match px.rgba.get(..len) { + Some(b) => b, + None => { + let mut v = px.rgba.clone(); + v.resize(len, 0); + padded = v; + &padded + } + }; + let scopes = crate::Scopes::push(device); + let size = wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }; + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("anicraft-bitmap"), + size, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(4 * w), rows_per_image: Some(h) }, + size, + ); + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + let (oom, other) = scopes.pop_split(); + if let Some(e) = other { + return Err(GpuError::Device(describe(&e))); + } + if let Some(e) = oom { + return Err(GpuError::TooLarge(format!("no room on the GPU for a {w}×{h} bitmap: {e}"))); + } + Ok((view, len as u64)) +} diff --git a/crates/gpu/src/layers.rs b/crates/gpu/src/layers.rs new file mode 100644 index 00000000..b0df9249 --- /dev/null +++ b/crates/gpu/src/layers.rs @@ -0,0 +1,272 @@ +//! Filters on the GPU (ADR 0031): the host side of `filters.wgsl`. +//! +//! A filtered group's layer is drawn (premultiplied f32) into half 0 of its `img` buffer by the +//! fine pass, then each of its filters runs as compute passes over it, step for step as the CPU +//! oracle's `filters::apply` does, with the numbers `filters::resolve` gives: blurs as box +//! passes along rows and then columns (ping-ponging between the buffer's halves, or between a +//! coverage slot and slot 2), shadows, glows and bevels from moved and blurred coverage, the +//! colour matrix per pixel. The finished layer is copied into the paint buffer of the scene that +//! shows it. + +use anicraft_doc::FilterSide; +use anicraft_render::filters::{BevelPaint, BoxBlur, FilterOp}; +use anicraft_render::paint::Premul; +use wgpu::util::DeviceExt; + +/// The filter pipelines (one layout, one entry point each). +pub(crate) struct FilterPipelines { + layout: wgpu::BindGroupLayout, + box4: wgpu::ComputePipeline, + box1: wgpu::ComputePipeline, + coverage: wgpu::ComputePipeline, + shadow: wgpu::ComputePipeline, + bevel: wgpu::ComputePipeline, + gradient_glow: wgpu::ComputePipeline, + adjust: wgpu::ComputePipeline, +} + +const INNER: u32 = 1; +const KNOCKOUT: u32 = 2; +const HIDE: u32 = 4; +const INVERSE: u32 = 8; +const RAMP: u32 = 16; + +/// The coverage slot a blur uses as its other half. +const TEMP: u32 = 2; + +/// Words in the pass parameters (`filters.wgsl` › `Params`: 11 `vec4`s). +const PARAM_WORDS: usize = 44; + +/// Whether any of `ops` needs the coverage slots. +pub(crate) fn needs_coverage(ops: &[FilterOp]) -> bool { + ops.iter().any(|o| !matches!(o, FilterOp::Blur(_) | FilterOp::AdjustColor(_))) +} + +/// One pass's parameters. +#[derive(Clone, Copy)] +struct Params { + words: [u32; PARAM_WORDS], +} + +impl Params { + fn new(w: u32, h: u32) -> Params { + let mut words = [0u32; PARAM_WORDS]; + words[0] = w; + words[1] = h; + Params { words } + } + fn size(mut self, n: u32, horizontal: bool) -> Self { + self.words[2] = n; + self.words[3] = u32::from(horizontal); + self + } + fn io(mut self, src: u32, dst: u32, flags: u32, side: u32) -> Self { + self.words[4..8].copy_from_slice(&[src, dst, flags, side]); + self + } + fn offset(mut self, dx: i64, dy: i64) -> Self { + // Past ±2^24 every read is outside the layer either way (layers are far smaller). + let c = |v: i64| v.clamp(-(1 << 24), 1 << 24) as i32 as u32; + self.words[8] = c(dx); + self.words[9] = c(dy); + self + } + fn colors(mut self, c0: [f32; 4], c1: [f32; 4]) -> Self { + for (w, v) in self.words[12..16].iter_mut().zip(c0) { + *w = v.to_bits(); + } + for (w, v) in self.words[16..20].iter_mut().zip(c1) { + *w = v.to_bits(); + } + self + } + fn strength(mut self, k: f32) -> Self { + self.words[20] = k.to_bits(); + self + } + /// The 4 × 5 matrix as five columns. + fn matrix(mut self, m: &[[f32; 5]; 4]) -> Self { + for col in 0..5 { + for (row, r) in m.iter().enumerate() { + if let (Some(w), Some(v)) = (self.words.get_mut(24 + col * 4 + row), r.get(col)) { + *w = v.to_bits(); + } + } + } + self + } +} + +fn side_code(s: FilterSide) -> u32 { + match s { + FilterSide::Inner => 0, + FilterSide::Outer => 1, + FilterSide::Full => 2, + } +} + +impl FilterPipelines { + pub(crate) fn new(device: &wgpu::Device) -> FilterPipelines { + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("anicraft-filters"), source: wgpu::ShaderSource::Wgsl(include_str!("filters.wgsl").into()) }); + let entry = |binding: u32, ty: wgpu::BufferBindingType| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::COMPUTE, + ty: wgpu::BindingType::Buffer { ty, has_dynamic_offset: false, min_binding_size: None }, + count: None, + }; + let rw = wgpu::BufferBindingType::Storage { read_only: false }; + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("anicraft-filters"), + entries: &[entry(0, wgpu::BufferBindingType::Uniform), entry(1, rw), entry(2, rw), entry(3, wgpu::BufferBindingType::Storage { read_only: true })], + }); + let pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("anicraft-filters"), bind_group_layouts: &[Some(&layout)], immediate_size: 0 }); + let make = |name: &str| { + device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { label: Some(name), layout: Some(&pl), module: &module, entry_point: Some(name), compilation_options: Default::default(), cache: None }) + }; + FilterPipelines { + box4: make("box4"), + box1: make("box1"), + coverage: make("coverage"), + shadow: make("shadow"), + bevel: make("bevel"), + gradient_glow: make("gradient_glow"), + adjust: make("adjust"), + layout, + } + } +} + +/// Records one layer's filters: `img` holds the drawn layer in half 0 (`w × h`), `cov` three +/// coverage slots (or nothing when no filter needs them). +pub(crate) struct Recorder<'a> { + pub device: &'a wgpu::Device, + pub pipes: &'a FilterPipelines, + pub img: &'a wgpu::Buffer, + pub cov: &'a wgpu::Buffer, + pub w: u32, + pub h: u32, + /// Bound as the ramp where a pass reads none. + pub no_ramp: &'a wgpu::Buffer, +} + +/// Passes a filter chain dispatches (for the work bound): about one per box pass and step. +pub(crate) fn passes(ops: &[FilterOp]) -> u64 { + let blur = |b: &BoxBlur| 2 * u64::from(b.passes.max(1)) + 1; + ops.iter() + .map(|o| match o { + FilterOp::Blur(b) => blur(b), + FilterOp::Shadow { blur: b, .. } | FilterOp::Bevel { blur: b, .. } => blur(b) + 2, + FilterOp::GradientGlow { blur: b, .. } => 2 * (blur(b) + 1) + 1, + FilterOp::AdjustColor(_) => 1, + }) + .sum() +} + +impl Recorder<'_> { + fn dispatch(&self, enc: &mut wgpu::CommandEncoder, pipe: &wgpu::ComputePipeline, p: Params, ramp: &wgpu::Buffer, groups: (u32, u32)) { + let ubuf = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("filter-params"), contents: bytemuck::cast_slice(&p.words), usage: wgpu::BufferUsages::UNIFORM }); + let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("anicraft-filters"), + layout: &self.pipes.layout, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: ubuf.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 1, resource: self.img.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 2, resource: self.cov.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 3, resource: ramp.as_entire_binding() }, + ], + }); + let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { label: Some("anicraft-filter"), timestamp_writes: None }); + pass.set_pipeline(pipe); + pass.set_bind_group(0, &bind, &[]); + pass.dispatch_workgroups(groups.0, groups.1, 1); + } + + fn pixels(&self) -> (u32, u32) { + (self.w.div_ceil(16), self.h.div_ceil(16)) + } + + /// One box pass along rows or columns, `src` → `dst` (halves of `img`, or coverage slots). + fn box_pass(&self, enc: &mut wgpu::CommandEncoder, four: bool, n: usize, horizontal: bool, src: u32, dst: u32) { + let lines = if horizontal { self.h } else { self.w }; + let p = Params::new(self.w, self.h).size(u32::try_from(n).unwrap_or(u32::MAX), horizontal).io(src, dst, 0, 0); + self.dispatch(enc, if four { &self.pipes.box4 } else { &self.pipes.box1 }, p, self.no_ramp, (lines.div_ceil(64), 1)); + } + + /// The oracle's `blur_channels`: `passes` × (rows, then columns) over half 0 of `img` + /// (`four`) or coverage slot `slot`, ending where it started. Widths of 0 or 1 change + /// nothing and are skipped. + fn blur(&self, enc: &mut wgpu::CommandEncoder, four: bool, slot: u32, b: &BoxBlur) { + let other = if four { 1 } else { TEMP }; + let mut cur = slot; + for _ in 0..b.passes.max(1) { + for (n, horizontal) in [(b.nx, true), (b.ny, false)] { + if n > 1 { + let next = if cur == slot { other } else { slot }; + self.box_pass(enc, four, n, horizontal, cur, next); + cur = next; + } + } + } + if cur != slot { + // A width-1 pass copies. + self.box_pass(enc, four, 1, true, cur, slot); + } + } + + /// The layer's opacity (`inverse`: transparency) moved by `(dx, dy)`, into slot `dst`. + fn coverage(&self, enc: &mut wgpu::CommandEncoder, dx: i64, dy: i64, inverse: bool, dst: u32) { + let p = Params::new(self.w, self.h).io(0, dst, if inverse { INVERSE } else { 0 }, 0).offset(dx, dy); + self.dispatch(enc, &self.pipes.coverage, p, self.no_ramp, self.pixels()); + } + + /// Records `ops`, in order. + pub(crate) fn record(&self, enc: &mut wgpu::CommandEncoder, ops: &[FilterOp]) { + let dummy = self.no_ramp; + let ramp_buffer = |r: &[Premul]| self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("filter-ramp"), contents: bytemuck::cast_slice(r), usage: wgpu::BufferUsages::STORAGE }); + for op in ops { + match op { + FilterOp::Blur(b) => self.blur(enc, true, 0, b), + FilterOp::Shadow { blur, dx, dy, strength, color, inner, knockout, hide_object } => { + self.coverage(enc, *dx, *dy, *inner, 0); + self.blur(enc, false, 0, blur); + let flags = (u32::from(*inner) * INNER) | (u32::from(*knockout) * KNOCKOUT) | (u32::from(*hide_object) * HIDE); + let p = Params::new(self.w, self.h).io(0, 0, flags, 0).colors([color.r, color.g, color.b, color.a], [0.0; 4]).strength(*strength); + self.dispatch(enc, &self.pipes.shadow, p, dummy, self.pixels()); + } + FilterOp::Bevel { blur, dx, dy, strength, paint, side, knockout } => { + self.coverage(enc, 0, 0, false, 0); + self.blur(enc, false, 0, blur); + let flags = u32::from(*knockout) * KNOCKOUT; + let p = Params::new(self.w, self.h).offset(*dx, *dy).strength(*strength); + match paint { + BevelPaint::Colors { highlight, shadow } => { + self.dispatch(enc, &self.pipes.bevel, p.io(0, 0, flags, side_code(*side)).colors(*highlight, *shadow), dummy, self.pixels()); + } + BevelPaint::Ramp(r) => { + let ramp = ramp_buffer(r); + self.dispatch(enc, &self.pipes.bevel, p.io(0, 0, flags | RAMP, side_code(*side)), &ramp, self.pixels()); + } + } + } + FilterOp::GradientGlow { blur, dx, dy, strength, ramp, side, knockout } => { + // Inside: the transparency into slot 0; outside: the opacity into slot 1. + if *side != FilterSide::Outer { + self.coverage(enc, *dx, *dy, true, 0); + self.blur(enc, false, 0, blur); + } + if *side != FilterSide::Inner { + self.coverage(enc, *dx, *dy, false, 1); + self.blur(enc, false, 1, blur); + } + let ramp = ramp_buffer(ramp); + let p = Params::new(self.w, self.h).io(0, 0, u32::from(*knockout) * KNOCKOUT, side_code(*side)).strength(*strength); + self.dispatch(enc, &self.pipes.gradient_glow, p, &ramp, self.pixels()); + } + FilterOp::AdjustColor(m) => { + let p = Params::new(self.w, self.h).matrix(m); + self.dispatch(enc, &self.pipes.adjust, p, dummy, self.pixels()); + } + } + } + } +} diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index 37d79f54..d59c7f39 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -1,15 +1,19 @@ -//! AniCraft GPU stage renderer (layer L3, beside the engine; ADR 0019, ADR 0029). +//! AniCraft GPU stage renderer (layer L3, beside the engine; ADR 0019, ADR 0029, ADR 0031). //! //! Draws the same scene as the CPU oracle (`anicraft-render`) with wgpu compute: the oracle //! prepares it ([`anicraft_render::prepare_scene`]: flattening, strokes, paints and their //! gradient tables, clips and groups), [`binning`] sorts the edges into 16 × 16 tiles on the //! CPU, and one compute pass computes every pixel's exact area coverage and composites the //! fills in order, inside their masks and groups (alpha and blend modes) as the oracle's band -//! compositor does. The output is the oracle's within float rounding (the parity tests check -//! mean and maximum error against it). +//! compositor does. Bitmap fills are sampled from textures uploaded once per image +//! ([`images`]); a filtered symbol is drawn into a layer of its own, filtered by compute passes +//! that run the oracle's filter steps ([`layers`]), and composited like any fill (ADR 0031). +//! The output is the oracle's within float rounding (the parity tests check mean and maximum +//! error against it). //! -//! What it doesn't draw yet (filters, bitmaps, nesting deeper than the shader holds) is -//! reported as [`GpuError::NeedsFull`], and the caller renders that picture on the CPU. +//! What it doesn't draw (nesting deeper than the shaders hold, more bitmaps in one picture than +//! its texture slots) is reported as [`GpuError::NeedsFull`], and the caller renders that +//! picture on the CPU. //! //! Two ways to run: on a **headless device of its own** ([`GpuRenderer::new`]: tests, tools, //! and windows that run on OpenGL), reading frames back; or on the **window's device** @@ -18,12 +22,16 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] pub mod binning; +pub mod images; +mod layers; use anicraft_doc::DisplayList; use anicraft_render::{MAX_PIXELS, NeedsFull, Pixmap, RenderStats, View, paint::premultiply, prepare_scene}; +use rayon::prelude::*; use std::sync::{Arc, Mutex, PoisonError}; use std::time::Instant; use wgpu::util::DeviceExt; +pub use images::IMAGE_BUDGET; #[derive(Debug, thiserror::Error)] pub enum GpuError { @@ -104,11 +112,16 @@ pub const MAX_WORK: u64 = 1 << 25; /// Where the fine pass writes. enum Target<'a> { - Buffer(&'a wgpu::Buffer), + /// A buffer, its first `size` bytes. + Buffer(&'a wgpu::Buffer, u64), Texture(&'a wgpu::TextureView), } -/// The renderer: a device (its own, or the window's) and the fine pipelines. +/// The fine shader's first bitmap binding (`fine.wgsl`: `img0` … `img15`). +const IMAGE_BINDING: u32 = 6; + +/// The renderer: a device (its own, or the window's), the fine and filter pipelines, and the +/// bitmaps it has uploaded. pub struct GpuRenderer { device: wgpu::Device, queue: wgpu::Queue, @@ -116,6 +129,14 @@ pub struct GpuRenderer { layout: wgpu::BindGroupLayout, texture_pipeline: wgpu::ComputePipeline, texture_layout: wgpu::BindGroupLayout, + filters: layers::FilterPipelines, + /// Bound where a texture slot or a buffer is unused. + blank: wgpu::TextureView, + /// Placeholders for a filter pass's coverage (written) and ramp (read): one buffer can't be + /// both in a dispatch. + blank_coverage: wgpu::Buffer, + blank_ramp: wgpu::Buffer, + images: Mutex, limits: wgpu::Limits, /// Adapter name and backend, for the status bar and reports. pub adapter: String, @@ -134,6 +155,7 @@ fn check(adapter: &wgpu::Adapter, limits: &wgpu::Limits, name: &str) -> Result<( } if limits.max_storage_buffers_per_shader_stage < 4 || limits.max_storage_textures_per_shader_stage < 1 + || limits.max_sampled_textures_per_shader_stage < binning::MAX_IMAGES as u32 || limits.max_compute_invocations_per_workgroup < 256 || limits.max_compute_workgroup_size_x < 16 || limits.max_compute_workgroup_size_y < 16 @@ -146,6 +168,54 @@ fn check(adapter: &wgpu::Adapter, limits: &wgpu::Limits, name: &str) -> Result<( Ok(()) } +/// Deepest nesting of filtered symbols drawn on the GPU (each level is a layer drawn and +/// filtered inside its parent's); deeper goes to the CPU. +pub const MAX_FILTER_DEPTH: usize = 4; + +/// Most filtered-layer pixels one call draws (each costs 44 bytes of buffers while the frame +/// draws: two f32 halves and three coverage slots). A stage tile's layers are far smaller; a +/// call beyond this is drawn on the CPU. +pub const MAX_LAYER_PIXELS: u64 = 1 << 23; + +/// A scene binned for the fine pass, with the filtered layers it shows (each its own scene, +/// drawn and filtered before the pass). +struct Job { + binned: binning::Binned, + layers: Vec, +} + +struct LayerJob { + /// Where its pixels go in the parent's paint buffer (`vec4`s). + at: u32, + job: Box, + view: View, + ops: Vec, +} + +/// What a job tree costs, for the limits. +#[derive(Default)] +struct Cost { + work: u64, + layer_pixels: u64, + entries: usize, + pieces: usize, +} + +impl Job { + fn cost(&self, c: &mut Cost) { + c.work += self.binned.work; + c.entries += self.binned.entries; + c.pieces += self.binned.pieces; + for l in &self.layers { + let n = u64::from(l.view.width) * u64::from(l.view.height); + c.layer_pixels += n; + // Each filter pass walks the layer once (per pixel or per line). + c.work += n.div_ceil(256) * layers::passes(&l.ops); + l.job.cost(c); + } + } +} + impl GpuRenderer { /// Opens a headless device on the best adapter (high-performance first), builds the /// pipelines and checks them with a small known render. `Err` means "use the CPU". @@ -209,9 +279,11 @@ impl GpuRenderer { let entry = |binding: u32, ty: wgpu::BindingType| wgpu::BindGroupLayoutEntry { binding, visibility: wgpu::ShaderStages::COMPUTE, ty, count: None }; let buffer = |ty| wgpu::BindingType::Buffer { ty, has_dynamic_offset: false, min_binding_size: None }; let read = || buffer(wgpu::BufferBindingType::Storage { read_only: true }); - let common = [entry(0, buffer(wgpu::BufferBindingType::Uniform)), entry(1, read()), entry(2, read()), entry(3, read())]; + let mut common = vec![entry(0, buffer(wgpu::BufferBindingType::Uniform)), entry(1, read()), entry(2, read()), entry(3, read())]; + let image = wgpu::BindingType::Texture { sample_type: wgpu::TextureSampleType::Float { filterable: false }, view_dimension: wgpu::TextureViewDimension::D2, multisampled: false }; + common.extend((0..binning::MAX_IMAGES as u32).map(|i| entry(IMAGE_BINDING + i, image))); let make = |label: &str, out: wgpu::BindGroupLayoutEntry, entry_point: &str| { - let mut entries = common.to_vec(); + let mut entries = common.clone(); entries.push(out); let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some(label), entries: &entries }); let pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some(label), bind_group_layouts: &[Some(&layout)], immediate_size: 0 }); @@ -228,23 +300,56 @@ impl GpuRenderer { let (pipeline, layout) = make("anicraft-fine", entry(4, buffer(wgpu::BufferBindingType::Storage { read_only: false })), "main"); let storage_texture = wgpu::BindingType::StorageTexture { access: wgpu::StorageTextureAccess::WriteOnly, format: wgpu::TextureFormat::Rgba8Unorm, view_dimension: wgpu::TextureViewDimension::D2 }; let (texture_pipeline, texture_layout) = make("anicraft-fine-texture", entry(5, storage_texture), "main_texture"); + let filters = layers::FilterPipelines::new(&device); + let blank = device + .create_texture(&wgpu::TextureDescriptor { + label: Some("anicraft-blank"), + size: wgpu::Extent3d { width: 1, height: 1, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + .create_view(&wgpu::TextureViewDescriptor::default()); + let blank_buffer = |label| device.create_buffer(&wgpu::BufferDescriptor { label: Some(label), size: 16, usage: wgpu::BufferUsages::STORAGE, mapped_at_creation: false }); + let (blank_coverage, blank_ramp) = (blank_buffer("anicraft-blank-coverage"), blank_buffer("anicraft-blank-ramp")); if let Some(e) = scopes.pop() { - return Err(GpuError::Unavailable(format!("{name}: the stage shader did not build: {e}"))); + return Err(GpuError::Unavailable(format!("{name}: the stage shaders did not build: {}", describe(&e)))); } - let r = GpuRenderer { device, queue, pipeline, layout, texture_pipeline, texture_layout, limits, adapter: name, shared, broken }; + let r = GpuRenderer { + device, + queue, + pipeline, + layout, + texture_pipeline, + texture_layout, + filters, + blank, + blank_coverage, + blank_ramp, + images: Mutex::new(images::Images::default()), + limits, + adapter: name, + shared, + broken, + }; r.self_test()?; Ok(r) } - /// A 24 × 20 render of two known rectangles must come back exactly, read back and through a - /// texture: catches drivers that build the pipelines but compute garbage. + /// A 24 × 20 render must come back as the CPU oracle draws it, read back and through a + /// texture: two known rectangles exactly, and a bitmap fill and a blurred, colour-adjusted + /// symbol within the parity target. Catches drivers that build the pipelines but compute + /// garbage. fn self_test(&self) -> Result<(), GpuError> { - use anicraft_doc::{ColorEffect, DisplayItem, ElementId, LayerId, Paint, Rgba}; + use anicraft_doc::{ColorEffect, DisplayItem, ElementId, Filter, Group, LayerId, Paint, Pixels, Rgba}; use anicraft_geom::{Affine, FillRule, Path}; - let item = |x: f32, c: Rgba| DisplayItem { - path: Arc::new(Path::rect(x, 2.0, 8.0, 16.0)), + let item = |path: Path, fill: Paint| DisplayItem { + path: Arc::new(path), transform: Affine::IDENTITY, - fill: Some(Paint::Solid(c)), + fill: Some(fill), stroke: None, fill_rule: FillRule::NonZero, color: ColorEffect::default(), @@ -257,11 +362,20 @@ impl GpuRenderer { group: None, image: None, }; - let dl = DisplayList { items: vec![item(2.0, Rgba::new(1.0, 0.0, 0.0, 1.0)), item(14.0, Rgba::new(0.0, 0.0, 1.0, 0.5))], ..Default::default() }; + let solid = |x: f32, c: Rgba| item(Path::rect(x, 2.0, 8.0, 16.0), Paint::Solid(c)); + let mut bitmap = item(Path::rect(2.0, 18.0, 20.0, 2.0), Paint::Bitmap { bitmap: anicraft_doc::BitmapId(1), repeat: true, matrix: Affine::scale(3.0, 1.0) }); + bitmap.image = Some(Arc::new(Pixels { width: 2, height: 1, rgba: vec![255, 0, 0, 255, 0, 0, 255, 128] })); + let mut blurred = item(Path::rect(10.0, 0.0, 4.0, 4.0), Paint::Solid(Rgba::new(0.0, 0.6, 0.0, 1.0))); + blurred.group = Some(0); + let filters = Arc::from(vec![Filter::Blur { blur_x: 3.0, blur_y: 2.0, quality: 2 }, Filter::AdjustColor { brightness: 10.0, contrast: 0.0, saturation: -50.0, hue: 30.0 }]); + let groups = vec![Group { parent: None, alpha: 1.0, blend: anicraft_doc::BlendMode::Normal, filters, clip: None }]; + let dl = DisplayList { items: vec![solid(2.0, Rgba::new(1.0, 0.0, 0.0, 1.0)), solid(14.0, Rgba::new(0.0, 0.0, 1.0, 0.5)), bitmap, blurred], groups, ..Default::default() }; let view = View { transform: Affine::IDENTITY, width: 24, height: 20, background: Some(Rgba::WHITE) }; + let (oracle, _) = anicraft_render::render(&dl, &view).map_err(|e| GpuError::Unavailable(e.to_string()))?; + let oracle = oracle.to_premul_rgba8(); let ok = |bytes: &[u8]| { let px = |x: usize, y: usize| bytes.get((y * 24 + x) * 4..(y * 24 + x) * 4 + 4).map(<[u8]>::to_vec); - px(5, 10) == Some(vec![255, 0, 0, 255]) && px(18, 10) == Some(vec![128, 128, 255, 255]) && px(12, 1) == Some(vec![255; 4]) + px(5, 10) == Some(vec![255, 0, 0, 255]) && px(18, 10) == Some(vec![128, 128, 255, 255]) && compare_premul8(bytes, &oracle).is_some_and(|d| d.max <= 2 && d.mean <= 0.1) }; let f = self.render(&dl, &view, Output::Premul8)?; let t = self.render_texture(&dl, &view)?; @@ -281,22 +395,27 @@ impl GpuRenderer { self.broken.lock().unwrap_or_else(PoisonError::into_inner).get_or_insert_with(|| why.to_string()); } + /// Bitmaps uploaded and kept, and their bytes (at most about [`images::IMAGE_BUDGET`]). + pub fn cached_images(&self) -> (usize, u64) { + self.images.lock().unwrap_or_else(PoisonError::into_inner).held() + } + /// Renders a display list and reads it back. [`GpuError::NeedsFull`] means "draw this one /// on the CPU". pub fn render(&self, dl: &DisplayList, view: &View, output: Output) -> Result { self.guard(|| { - let (b, stats, mut timings) = self.prepare(dl, view)?; + let (job, stats, mut timings, cost) = self.prepare(dl, view)?; let t = Instant::now(); - let (premul8, pixmap) = self.run_readback(&b, view, output)?; + let (premul8, pixmap) = self.run_readback(&job, view, output)?; timings.gpu = ms(t); - Ok(GpuFrame { width: view.width, height: view.height, premul8, pixmap, stats, timings, entries: b.entries, pieces: b.pieces }) + Ok(GpuFrame { width: view.width, height: view.height, premul8, pixmap, stats, timings, entries: cost.entries, pieces: cost.pieces }) }) } /// Renders a display list into a new texture on this renderer's device (no readback). pub fn render_texture(&self, dl: &DisplayList, view: &View) -> Result { self.guard(|| { - let (b, stats, mut timings) = self.prepare(dl, view)?; + let (job, stats, mut timings, cost) = self.prepare(dl, view)?; let t = Instant::now(); let (w, h) = (view.width, view.height); let max = self.limits.max_texture_dimension_2d; @@ -315,11 +434,17 @@ impl GpuRenderer { view_formats: &[], }); let tex_view = texture.create_view(&wgpu::TextureViewDescriptor::default()); - let enc = self.encode(&b, view, Output::Premul8, Target::Texture(&tex_view))?; - self.queue.submit([enc]); + let mut enc = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("anicraft-stage") }); + let recorded = self.record(&mut enc, &job, view, Output::Premul8, Target::Texture(&tex_view)); + // Pop the scopes whatever happened while recording (they are this thread's). + if let Err(e) = recorded { + let _ = self.pop(scopes); + return Err(e); + } + self.queue.submit([enc.finish()]); self.pop(scopes)?; timings.gpu = ms(t); - Ok(GpuTexture { texture, view: tex_view, width: w, height: h, stats, timings, entries: b.entries, pieces: b.pieces }) + Ok(GpuTexture { texture, view: tex_view, width: w, height: h, stats, timings, entries: cost.entries, pieces: cost.pieces }) }) } @@ -364,33 +489,117 @@ impl GpuRenderer { r } - /// The oracle's preparation, then the binning; checked against the device's limits. - fn prepare(&self, dl: &DisplayList, view: &View) -> Result<(binning::Binned, RenderStats, Timings), GpuError> { + /// The oracle's preparation and the binning, for the scene and each filtered layer it shows + /// (in parallel); checked against the limits and the device's. + fn prepare(&self, dl: &DisplayList, view: &View) -> Result<(Job, RenderStats, Timings, Cost), GpuError> { let (w, h) = (view.width, view.height); if w == 0 || h == 0 || u64::from(w) * u64::from(h) > MAX_PIXELS { return Err(GpuError::TooLarge(format!("image {w}×{h} is outside the GPU stage's range"))); } let t0 = Instant::now(); - let (scene, stats) = prepare_scene(dl, view).map_err(GpuError::NeedsFull)?; - let t1 = Instant::now(); - let b = binning::bin(&scene, w, h).map_err(GpuError::NeedsFull)?; + let mut bin_ms = 0.0; + let (job, stats) = self.plan(dl, view, 0, &mut bin_ms)?; + let total = ms(t0); + let mut cost = Cost::default(); + job.cost(&mut cost); + if cost.work > MAX_WORK { + return Err(GpuError::TooLarge(format!("this frame is too much work for the GPU ({} M)", cost.work >> 20))); + } + if cost.layer_pixels > MAX_LAYER_PIXELS { + return Err(GpuError::TooLarge(format!("this frame's filters need {} M pixels of layers", cost.layer_pixels >> 20))); + } + self.check_sizes(&job)?; + Ok((job, stats, Timings { prepare: (total - bin_ms).max(0.0), bin: bin_ms, gpu: 0.0 }, cost)) + } + + /// One scene: prepared and binned, then its shown layers planned the same way. + fn plan(&self, dl: &DisplayList, view: &View, depth: usize, bin_ms: &mut f32) -> Result<(Job, RenderStats), GpuError> { + let (mut scene, stats) = prepare_scene(dl, view).map_err(GpuError::NeedsFull)?; + let t = Instant::now(); + let binned = binning::bin(&scene, view.width, view.height).map_err(GpuError::NeedsFull)?; + *bin_ms += ms(t); + let shown: Vec<(u32, anicraft_render::FilterLayer)> = std::mem::take(&mut scene.layers).into_iter().zip(&binned.layer_at).filter_map(|(l, at)| Some(((*at)?, l))).collect(); // Free the prepared polygons on the pool, off this frame's path (as the oracle does). rayon::spawn(move || drop(scene)); - if b.work > MAX_WORK { - return Err(GpuError::TooLarge(format!("this frame is too much work for one GPU pass ({} M)", b.work >> 20))); + if !shown.is_empty() && depth >= MAX_FILTER_DEPTH { + return Err(GpuError::NeedsFull(NeedsFull::Filters)); } + let planned: Vec> = shown + .into_par_iter() + .map(|(at, l)| { + let mut ms = 0.0; + let (job, _) = self.plan(&l.list, &l.view, depth + 1, &mut ms)?; + Ok((LayerJob { at, job: Box::new(job), view: l.view, ops: l.filters }, ms)) + }) + .collect(); + let mut layers = Vec::with_capacity(planned.len()); + for p in planned { + let (l, ms) = p?; + *bin_ms += ms; + layers.push(l); + } + Ok((Job { binned, layers }, stats)) + } + + /// Every buffer a job binds fits what the device binds. + fn check_sizes(&self, job: &Job) -> Result<(), GpuError> { let cap = self.limits.max_storage_buffer_binding_size.min(self.limits.max_buffer_size); - let sizes = [("tile lists", b.index.len() * 4), ("edges", b.geo.len() * 16), ("paints", b.paints.len() * 16)]; + let b = &job.binned; + let sizes = [("tile lists", b.index.len() as u64 * 4), ("edges", b.geo.len() as u64 * 16), ("paints and layers", b.paints_len as u64 * 16)]; for (what, n) in sizes { - if n as u64 > cap { + if n > cap { return Err(GpuError::TooLarge(format!("{what} need {} MB, more than this GPU binds ({} MB)", n >> 20, cap >> 20))); } } - Ok((b, stats, Timings { prepare: (t1 - t0).as_secs_f32() * 1000.0, bin: ms(t1), gpu: 0.0 })) + for l in &job.layers { + let n = u64::from(l.view.width) * u64::from(l.view.height); + if 32 * n > cap { + return Err(GpuError::TooLarge(format!("a filtered layer needs {} MB, more than this GPU binds ({} MB)", (32 * n) >> 20, cap >> 20))); + } + self.check_sizes(&l.job)?; + } + Ok(()) + } + + /// Records a job into `enc`: each shown layer drawn, filtered and copied into the paint + /// buffer, then the fine pass into `target`. + fn record(&self, enc: &mut wgpu::CommandEncoder, job: &Job, view: &View, output: Output, target: Target) -> Result<(), GpuError> { + let b = &job.binned; + // The CPU's paints, with room after them for the layers' pixels. + let pbuf = self.device.create_buffer(&wgpu::BufferDescriptor { + label: Some("paints"), + size: (b.paints_len.max(1) * 16) as u64, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: true, + }); + { + let bytes: &[u8] = bytemuck::cast_slice(&b.paints); + if !bytes.is_empty() { + pbuf.slice(..bytes.len() as u64).get_mapped_range_mut().map_err(|e| GpuError::Device(e.to_string()))?.copy_from_slice(bytes); + } + } + pbuf.unmap(); + for l in &job.layers { + let (w, h) = (l.view.width, l.view.height); + let n = u64::from(w) * u64::from(h); + let img = self.device.create_buffer(&wgpu::BufferDescriptor { label: Some("layer"), size: 32 * n, usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false }); + let cov = if layers::needs_coverage(&l.ops) { + self.device.create_buffer(&wgpu::BufferDescriptor { label: Some("layer-coverage"), size: 12 * n, usage: wgpu::BufferUsages::STORAGE, mapped_at_creation: false }) + } else { + self.blank_coverage.clone() + }; + self.record(enc, &l.job, &l.view, Output::Float, Target::Buffer(&img, 16 * n))?; + layers::Recorder { device: &self.device, pipes: &self.filters, img: &img, cov: &cov, w, h, no_ramp: &self.blank_ramp }.record(enc, &l.ops); + enc.copy_buffer_to_buffer(&img, 0, &pbuf, u64::from(l.at) * 16, 16 * n); + } + let views = self.images.lock().unwrap_or_else(PoisonError::into_inner).views(&self.device, &self.queue, self.limits.max_texture_dimension_2d, &b.images)?; + self.fine_pass(enc, b, &pbuf, &views, view, output, target); + Ok(()) } /// Uploads the binned scene and records the fine pass into `target`. - fn encode(&self, b: &binning::Binned, view: &View, output: Output, target: Target) -> Result { + #[allow(clippy::too_many_arguments)] + fn fine_pass(&self, enc: &mut wgpu::CommandEncoder, b: &binning::Binned, pbuf: &wgpu::Buffer, images: &[wgpu::TextureView], view: &View, output: Output, target: Target) { let bg = view.background.map_or([0.0; 4], premultiply); let mut uniform = [0u32; 12]; uniform[..4].copy_from_slice(&[view.width, view.height, b.tiles_x, u32::from(output == Output::Float)]); @@ -407,16 +616,19 @@ impl GpuRenderer { let ubuf = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("params"), contents: bytemuck::cast_slice(&uniform), usage: wgpu::BufferUsages::UNIFORM }); let ibuf = storage("index", bytemuck::cast_slice(&b.index)); let gbuf = storage("geo", bytemuck::cast_slice(&b.geo)); - let pbuf = storage("paints", bytemuck::cast_slice(&b.paints)); let mut entries = vec![ wgpu::BindGroupEntry { binding: 0, resource: ubuf.as_entire_binding() }, wgpu::BindGroupEntry { binding: 1, resource: ibuf.as_entire_binding() }, wgpu::BindGroupEntry { binding: 2, resource: gbuf.as_entire_binding() }, wgpu::BindGroupEntry { binding: 3, resource: pbuf.as_entire_binding() }, ]; + for i in 0..binning::MAX_IMAGES { + let v = images.get(i).unwrap_or(&self.blank); + entries.push(wgpu::BindGroupEntry { binding: IMAGE_BINDING + i as u32, resource: wgpu::BindingResource::TextureView(v) }); + } let (pipeline, layout) = match target { - Target::Buffer(out) => { - entries.push(wgpu::BindGroupEntry { binding: 4, resource: out.as_entire_binding() }); + Target::Buffer(out, size) => { + entries.push(wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { buffer: out, offset: 0, size: wgpu::BufferSize::new(size) }) }); (&self.pipeline, &self.layout) } Target::Texture(view) => { @@ -425,17 +637,13 @@ impl GpuRenderer { } }; let bind = self.device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("anicraft-fine"), layout, entries: &entries }); - let mut enc = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("anicraft-fine") }); - { - let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { label: Some("anicraft-fine"), timestamp_writes: None }); - pass.set_pipeline(pipeline); - pass.set_bind_group(0, &bind, &[]); - pass.dispatch_workgroups(b.tiles_x, b.tiles_y, 1); - } - Ok(enc.finish()) + let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { label: Some("anicraft-fine"), timestamp_writes: None }); + pass.set_pipeline(pipeline); + pass.set_bind_group(0, &bind, &[]); + pass.dispatch_workgroups(b.tiles_x, b.tiles_y, 1); } - fn run_readback(&self, b: &binning::Binned, view: &View, output: Output) -> Result<(Vec, Option), GpuError> { + fn run_readback(&self, job: &Job, view: &View, output: Output) -> Result<(Vec, Option), GpuError> { let (w, h) = (view.width, view.height); let out_bytes = u64::from(w) * u64::from(h) * if output == Output::Float { 16 } else { 4 }; let cap = self.limits.max_storage_buffer_binding_size.min(self.limits.max_buffer_size); @@ -445,10 +653,13 @@ impl GpuRenderer { let scopes = Scopes::push(&self.device); let obuf = self.device.create_buffer(&wgpu::BufferDescriptor { label: Some("out"), size: out_bytes, usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false }); let read = self.device.create_buffer(&wgpu::BufferDescriptor { label: Some("readback"), size: out_bytes, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false }); - let fine = self.encode(b, view, output, Target::Buffer(&obuf))?; - let mut enc = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("anicraft-readback") }); + let mut enc = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("anicraft-fine") }); + if let Err(e) = self.record(&mut enc, job, view, output, Target::Buffer(&obuf, out_bytes)) { + let _ = self.pop(scopes); + return Err(e); + } enc.copy_buffer_to_buffer(&obuf, 0, &read, 0, out_bytes); - self.queue.submit([fine, enc.finish()]); + self.queue.submit([enc.finish()]); let bytes = self.map(&read, scopes)?; Ok(match output { Output::Premul8 => (bytes, None), @@ -488,7 +699,7 @@ impl GpuRenderer { fn pop(&self, scopes: Scopes) -> Result<(), GpuError> { let (oom, other) = scopes.pop_split(); if let Some(e) = other { - return Err(GpuError::Device(e.to_string())); + return Err(GpuError::Device(describe(&e))); } if let Some(e) = oom { return Err(GpuError::TooLarge(format!("the GPU ran out of memory: {e}"))); @@ -527,6 +738,14 @@ impl Scopes { } } +/// A wgpu error with what it says happened (its `Display` alone is just "Validation Error"). +pub(crate) fn describe(e: &wgpu::Error) -> String { + match e { + wgpu::Error::Validation { description, .. } | wgpu::Error::Internal { description, .. } => description.trim().to_string(), + wgpu::Error::OutOfMemory { .. } => e.to_string(), + } +} + fn ms(t: Instant) -> f32 { t.elapsed().as_secs_f32() * 1000.0 } diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index 92a9eb82..39d0773d 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -532,8 +532,12 @@ fn degenerate_geometry_bins_without_trouble() { bounds: anicraft_render::raster::IRect { x0: 0, y0: 0, x1: 32, y1: 32 }, clip, group, + layer: None, }; - let scene = anicraft_render::Scene { fills: vec![fill(None, None), fill(Some(7), Some(9))], clips: Vec::new(), groups: Vec::new() }; + // A fill naming a layer that isn't there draws nothing. + let mut lost = fill(None, None); + lost.layer = Some(3); + let scene = anicraft_render::Scene { fills: vec![fill(None, None), fill(Some(7), Some(9)), lost], clips: Vec::new(), groups: Vec::new(), layers: Vec::new() }; let b = binning::bin(&scene, 32, 32).unwrap(); assert!(b.geo.iter().flatten().all(|v| v.is_finite())); } @@ -623,13 +627,23 @@ fn gpu_matches_the_oracle_on_many_shapes_and_tiles() { #[test] fn gpu_hands_back_what_it_does_not_draw_and_stays_usable() { let Some(g) = gpu() else { return }; - let mut filtered = in_group(item(Path::rect(0.0, 0.0, 8.0, 8.0), Paint::Solid(Rgba::BLACK)), 0); - filtered.color.alpha = 0.5; - let mut grp = group(None, 1.0, BlendMode::Normal); - grp.filters = Arc::from(vec![anicraft_doc::Filter::Blur { blur_x: 4.0, blur_y: 4.0, quality: 1 }]); - let dl = DisplayList { items: vec![filtered], clip_parents: Vec::new(), groups: vec![grp] }; - assert!(matches!(g.render(&dl, &view(8, 8, None), Output::Premul8), Err(GpuError::NeedsFull(NeedsFull::Filters)))); - assert!(matches!(g.render_texture(&dl, &view(8, 8, None)), Err(GpuError::NeedsFull(NeedsFull::Filters)))); + // Filtered symbols nested deeper than MAX_FILTER_DEPTH are the CPU's; one less is the GPU's. + let nested = |depth: usize| { + let groups: Vec = (0..depth) + .map(|i| { + let mut grp = group(i.checked_sub(1).map(|p| p as u32), 1.0, BlendMode::Normal); + grp.filters = Arc::from(vec![anicraft_doc::Filter::Blur { blur_x: 2.0, blur_y: 2.0, quality: 1 }]); + grp + }) + .collect(); + let mut it = in_group(item(Path::rect(2.0, 2.0, 8.0, 8.0), Paint::Solid(Rgba::BLACK)), depth as u32 - 1); + it.color.alpha = 0.5; + DisplayList { items: vec![it], clip_parents: Vec::new(), groups } + }; + let deep = nested(MAX_FILTER_DEPTH + 1); + assert!(matches!(g.render(&deep, &view(12, 12, None), Output::Premul8), Err(GpuError::NeedsFull(NeedsFull::Filters)))); + assert!(matches!(g.render_texture(&deep, &view(12, 12, None)), Err(GpuError::NeedsFull(NeedsFull::Filters)))); + assert_parity(g, "filters nested as deep as the GPU draws", &nested(MAX_FILTER_DEPTH), &view(12, 12, Some(Rgba::WHITE))); assert!(matches!(g.render(&list(vec![]), &view(0, 8, None), Output::Premul8), Err(GpuError::TooLarge(_)))); assert!(matches!(g.render(&list(vec![]), &view(100_000, 100_000, None), Output::Premul8), Err(GpuError::TooLarge(_)))); assert!(matches!(g.render_texture(&list(vec![]), &view(0, 8, None)), Err(GpuError::TooLarge(_)))); @@ -650,3 +664,233 @@ fn gpu_renders_from_many_threads_at_once() { let all: Vec> = (0..8).into_par_iter().map(|i| if i % 2 == 0 { g.render(&dl, &v, Output::Premul8).unwrap().premul8 } else { g.read_texture(&g.render_texture(&dl, &v).unwrap()).unwrap() }).collect(); assert!(all.iter().all(|a| *a == first), "deterministic under concurrency"); } + +// ----- bitmaps and filters (ADR 0031) --------------------------------------------------------- + +/// A `w × h` test image: a hue ramp with an alpha ramp, so every tap and every seam differs. +fn pixels(w: u32, h: u32, seed: u32) -> Arc { + let mut rgba = Vec::with_capacity(w as usize * h as usize * 4); + for y in 0..h { + for x in 0..w { + let v = x.wrapping_mul(37).wrapping_add(y.wrapping_mul(91)).wrapping_add(seed.wrapping_mul(53)); + rgba.extend_from_slice(&[(v % 256) as u8, ((v / 3 + 80) % 256) as u8, ((x * 255) / w.max(1)) as u8, (255 - (y * 200) / h.max(1)) as u8]); + } + } + Arc::new(anicraft_doc::Pixels { width: w, height: h, rgba }) +} + +fn bitmap_item(path: Path, px: &Arc, repeat: bool, matrix: Affine) -> DisplayItem { + let mut i = item(path, Paint::Bitmap { bitmap: anicraft_doc::BitmapId(1), repeat, matrix }); + i.image = Some(px.clone()); + i +} + +/// Bitmap fills: placed, scaled, turned and repeated; with a colour effect; clipped and +/// grouped; tiny and malformed images. +fn bitmap_scenes() -> Vec<(&'static str, DisplayList, View)> { + let a = pixels(13, 9, 1); + let b = pixels(1, 1, 2); + let c = pixels(40, 30, 3); + let white = Some(Rgba::WHITE); + let turned = Affine::translate(30.0, 12.0).mul(&Affine::rotate(0.4)).mul(&Affine::scale(3.3, 2.1)); + let mut out = vec![ + ("bitmap placed once", list(vec![bitmap_item(Path::rect(2.0, 3.0, 90.0, 70.0), &a, false, turned)]), view(100, 80, white)), + ("bitmap repeated", list(vec![bitmap_item(Path::ellipse(50.0, 40.0, 45.0, 35.0), &a, true, turned)]), view(100, 80, None)), + ("bitmap magnified, mirrored", list(vec![bitmap_item(Path::rect(0.0, 0.0, 64.0, 48.0), &c, false, Affine { m: [-1.7, 0.0, 0.0, 1.6, 66.0, -1.3] })]), view(64, 48, white)), + ("one-pixel bitmap", list(vec![bitmap_item(Path::rect(5.5, 5.5, 20.0, 20.0), &b, true, Affine::scale(7.0, 7.0))]), view(32, 32, white)), + ]; + // Colour effects: tint and alpha per tap, and a NaN tint (sanitized to 0 as the oracle does). + let mut tinted = bitmap_item(Path::rect(0.0, 0.0, 60.0, 40.0), &c, true, Affine::scale(0.7, 0.9)); + tinted.color = ColorEffect { alpha: 0.6, tint: Rgba::new(0.2, 0.9, 0.1, 1.0), tint_amount: 0.4 }; + let mut nan = bitmap_item(Path::rect(30.0, 10.0, 30.0, 30.0), &a, false, Affine::scale(2.0, 2.0)); + nan.color = ColorEffect { alpha: 1.0, tint: Rgba::new(f32::NAN, 0.5, 0.5, 1.0), tint_amount: 0.3 }; + out.push(("bitmap colour effects", list(vec![tinted, nan]), view(64, 48, Some(Rgba::new(0.1, 0.1, 0.3, 1.0))))); + // A short (malformed) buffer: transparent past its end; an empty image draws nothing. + let short = Arc::new(anicraft_doc::Pixels { width: 4, height: 4, rgba: vec![200; 20] }); + let empty = Arc::new(anicraft_doc::Pixels { width: 0, height: 3, rgba: Vec::new() }); + out.push(("malformed bitmaps", list(vec![bitmap_item(Path::rect(0.0, 0.0, 30.0, 30.0), &short, false, Affine::scale(7.0, 7.0)), bitmap_item(Path::rect(10.0, 0.0, 10.0, 30.0), &empty, true, Affine::IDENTITY)]), view(32, 32, white))); + // Inside a mask and a faded group, over a gradient. + let grad = item(Path::rect(0.0, 0.0, 80.0, 60.0), Paint::Linear { start: Vec2::new(0.0, 0.0), end: Vec2::new(80.0, 60.0), stops: stops(&[(0.0, Rgba::BLACK), (1.0, Rgba::new(1.0, 0.8, 0.0, 1.0))]) }); + let inside = in_group(clipped(bitmap_item(Path::rect(4.0, 4.0, 72.0, 52.0), &a, true, turned), 0), 0); + let dl = DisplayList { items: vec![grad, inside, mask(Path::ellipse(40.0, 30.0, 30.0, 22.0), 0)], clip_parents: vec![None], groups: vec![group(None, 0.7, BlendMode::Multiply)] }; + out.push(("bitmap masked and multiplied", dl, view(80, 60, white))); + out +} + +#[test] +fn gpu_matches_the_oracle_on_bitmap_fills() { + let Some(g) = gpu() else { return }; + for (name, dl, v) in bitmap_scenes() { + let d = assert_parity(g, name, &dl, &v); + eprintln!("{name}: mean {:.5} / max {} / {} of {} px differ", d.mean, d.max, d.differing, d.pixels); + } + // Each image is uploaded once and kept; drawing it again adds nothing. + let px = pixels(31, 17, 9); + let dl = list(vec![bitmap_item(Path::rect(0.0, 0.0, 40.0, 40.0), &px, true, Affine::IDENTITY)]); + g.render(&dl, &view(40, 40, None), Output::Premul8).unwrap(); + let held = g.cached_images(); + g.render(&dl, &view(40, 40, None), Output::Premul8).unwrap(); + assert_eq!(g.cached_images(), held); + assert!(held.1 <= IMAGE_BUDGET + 4 * 31 * 17); + // Sixteen images in one picture draw on the GPU; a seventeenth is the CPU's. + let many: Vec> = (0..17).map(|i| pixels(3, 2, 100 + i)).collect(); + let items = |n: usize| list(many.iter().take(n).enumerate().map(|(i, p)| bitmap_item(Path::rect(i as f32 * 4.0, 0.0, 4.0, 8.0), p, true, Affine::IDENTITY)).collect()); + assert_parity(g, "sixteen bitmaps", &items(16), &view(72, 8, Some(Rgba::WHITE))); + assert!(matches!(g.render(&items(17), &view(72, 8, None), Output::Premul8), Err(GpuError::NeedsFull(NeedsFull::Bitmaps)))); +} + +fn filtered_group(filters: Vec) -> Group { + Group { parent: None, alpha: 1.0, blend: BlendMode::Normal, filters: Arc::from(filters), clip: None } +} + +/// Every filter, its sides and switches, on a small symbol: a red square with a translucent +/// blue circle over it. +fn filters_to_test() -> Vec<(&'static str, anicraft_doc::Filter)> { + use anicraft_doc::{Filter as F, FilterSide as S}; + let ramp = stops(&[(0.0, Rgba::new(1.0, 1.0, 1.0, 0.0)), (0.4, Rgba::new(0.0, 1.0, 0.3, 0.8)), (1.0, Rgba::new(0.1, 0.0, 1.0, 1.0))]); + let bevel_ramp = stops(&[(0.0, Rgba::new(1.0, 1.0, 0.8, 1.0)), (0.5, Rgba::new(0.0, 0.0, 0.0, 0.0)), (1.0, Rgba::new(0.2, 0.0, 0.3, 1.0))]); + let shadow = |inner, knockout, hide_object| F::DropShadow { blur_x: 5.0, blur_y: 8.0, strength: 1.3, quality: 2, angle: 35.0, distance: 6.0, color: Rgba::new(0.1, 0.0, 0.2, 0.8), inner, knockout, hide_object }; + let bevel = |side, knockout| F::Bevel { blur_x: 4.0, blur_y: 4.0, strength: 1.5, quality: 1, angle: 45.0, distance: 3.0, shadow_color: Rgba::new(0.0, 0.0, 0.0, 0.9), highlight_color: Rgba::WHITE, side, knockout }; + vec![ + ("blur", F::Blur { blur_x: 7.0, blur_y: 4.0, quality: 3 }), + ("blur, odd and one-sided", F::Blur { blur_x: 5.0, blur_y: 0.0, quality: 1 }), + ("drop shadow", shadow(false, false, false)), + ("drop shadow, knockout", shadow(false, true, false)), + ("drop shadow, hide object", shadow(false, false, true)), + ("inner shadow", shadow(true, false, false)), + ("inner shadow, knockout", shadow(true, true, false)), + ("glow", F::Glow { blur_x: 10.0, blur_y: 10.0, strength: 2.0, quality: 2, color: Rgba::new(1.0, 0.8, 0.0, 1.0), inner: false, knockout: false }), + ("inner glow, knockout", F::Glow { blur_x: 6.0, blur_y: 9.0, strength: 1.0, quality: 3, color: Rgba::new(0.0, 1.0, 1.0, 0.7), inner: true, knockout: true }), + ("bevel, inner", bevel(S::Inner, false)), + ("bevel, outer", bevel(S::Outer, false)), + ("bevel, full, knockout", bevel(S::Full, true)), + ("gradient bevel", F::GradientBevel { blur_x: 5.0, blur_y: 3.0, strength: 1.0, quality: 2, angle: 120.0, distance: 4.0, stops: bevel_ramp, side: S::Full, knockout: false }), + ("gradient glow, outer", F::GradientGlow { blur_x: 8.0, blur_y: 6.0, strength: 1.2, quality: 2, angle: 0.0, distance: 3.0, stops: ramp.clone(), side: S::Outer, knockout: false }), + ("gradient glow, full, knockout", F::GradientGlow { blur_x: 4.0, blur_y: 4.0, strength: 1.0, quality: 1, angle: 210.0, distance: 2.0, stops: ramp, side: S::Full, knockout: true }), + ("adjust colour", F::AdjustColor { brightness: -20.0, contrast: 35.0, saturation: 60.0, hue: -70.0 }), + ] + .into_iter() + .map(|(n, f)| (n, f.sanitized())) + .collect() +} + +fn symbol_items(g: u32) -> Vec { + vec![ + in_group(item(Path::rect(14.0, 12.0, 30.0, 26.0), Paint::Solid(Rgba::new(0.9, 0.1, 0.1, 1.0))), g), + in_group(item(Path::ellipse(40.0, 36.0, 14.0, 14.0), Paint::Solid(Rgba::new(0.1, 0.3, 1.0, 0.6))), g), + ] +} + +#[test] +fn gpu_matches_the_oracle_on_every_filter() { + let Some(g) = gpu() else { return }; + let backdrop = item(Path::rect(0.0, 0.0, 70.0, 60.0), Paint::Linear { start: Vec2::new(0.0, 0.0), end: Vec2::new(70.0, 0.0), stops: stops(&[(0.0, Rgba::new(0.9, 0.9, 0.8, 1.0)), (1.0, Rgba::new(0.2, 0.4, 0.5, 1.0))]) }); + let mut worst: f64 = 0.0; + for (name, f) in filters_to_test() { + let mut items = vec![backdrop.clone()]; + items.extend(symbol_items(0)); + let dl = DisplayList { items, clip_parents: Vec::new(), groups: vec![filtered_group(vec![f])] }; + // At 1:1; zoomed, so sizes scale; and panned so the layer reaches past the surface. + for v in [view(70, 60, Some(Rgba::WHITE)), View { transform: Affine::scale(1.5, 1.5), width: 105, height: 90, background: None }, View { transform: Affine::translate(-20.0, -15.0), width: 40, height: 33, background: Some(Rgba::WHITE) }] { + let d = assert_parity(g, name, &dl, &v); + worst = worst.max(d.mean); + } + } + eprintln!("filters: worst mean {worst:.5}"); +} + +#[test] +fn gpu_matches_the_oracle_on_filtered_symbols_in_context() { + let Some(g) = gpu() else { return }; + use anicraft_doc::Filter as F; + let blur = F::Blur { blur_x: 4.0, blur_y: 4.0, quality: 2 }; + let shadow = F::DropShadow { blur_x: 4.0, blur_y: 4.0, strength: 1.0, quality: 1, angle: 45.0, distance: 5.0, color: Rgba::BLACK, inner: false, knockout: false, hide_object: false }; + let glow = F::Glow { blur_x: 6.0, blur_y: 6.0, strength: 1.5, quality: 1, color: Rgba::new(1.0, 0.9, 0.0, 1.0), inner: false, knockout: false }; + let white = Some(Rgba::WHITE); + let bg = item(Path::rect(0.0, 0.0, 80.0, 64.0), Paint::Solid(Rgba::new(0.3, 0.6, 0.4, 1.0))); + let mut scenes: Vec<(&str, DisplayList)> = Vec::new(); + // Several filters in a row, at part opacity, multiplied. + let mut grp = filtered_group(vec![shadow.clone(), glow.clone(), F::AdjustColor { brightness: 10.0, contrast: 0.0, saturation: -40.0, hue: 25.0 }]); + (grp.alpha, grp.blend) = (0.7, BlendMode::Multiply); + let mut items = vec![bg.clone()]; + items.extend(symbol_items(0)); + scenes.push(("filters in a row, faded and multiplied", DisplayList { items, clip_parents: Vec::new(), groups: vec![grp] })); + // A filtered symbol inside a filtered symbol, inside a faded one. + let mut outer = filtered_group(vec![shadow.clone()]); + outer.parent = Some(0); + let mut inner = filtered_group(vec![blur.clone()]); + inner.parent = Some(1); + let mut items = vec![bg.clone(), in_group(item(Path::rect(4.0, 40.0, 30.0, 16.0), Paint::Solid(Rgba::new(0.9, 0.6, 0.0, 1.0))), 1)]; + items.extend(symbol_items(2)); + scenes.push(("nested filtered symbols", DisplayList { items, clip_parents: Vec::new(), groups: vec![group(None, 0.8, BlendMode::Normal), outer, inner] })); + // Masked from outside (after filtering) and inside (before). + let mut clipped_grp = filtered_group(vec![glow.clone()]); + clipped_grp.clip = Some(0); + let mut items = vec![bg.clone()]; + items.extend(symbol_items(0).into_iter().map(|i| clipped(i, 0))); + items.push(mask(Path::ellipse(36.0, 30.0, 24.0, 20.0), 0)); + scenes.push(("masked after filtering", DisplayList { items, clip_parents: vec![None], groups: vec![clipped_grp] })); + let mut items = vec![bg.clone()]; + items.extend(symbol_items(0).into_iter().map(|i| clipped(i, 0))); + items.push(in_group(mask(Path::rect(20.0, 10.0, 16.0, 40.0), 0), 0)); + scenes.push(("masked inside the symbol", DisplayList { items, clip_parents: vec![None], groups: vec![filtered_group(vec![shadow.clone()])] })); + // A bitmap inside a filtered symbol. + let px = pixels(9, 7, 4); + let items = vec![bg.clone(), in_group(bitmap_item(Path::ellipse(40.0, 32.0, 26.0, 20.0), &px, true, Affine::scale(4.0, 4.0)), 0)]; + scenes.push(("filtered bitmap", DisplayList { items, clip_parents: Vec::new(), groups: vec![filtered_group(vec![blur, glow])] })); + for (name, dl) in scenes { + for v in [view(80, 64, white), View { transform: Affine::translate(-37.0, -5.0).mul(&Affine::scale(1.3, 1.3)), width: 48, height: 48, background: white }] { + let d = assert_parity(g, name, &dl, &v); + eprintln!("{name}: mean {:.5} / max {} / {} of {} px differ", d.mean, d.max, d.differing, d.pixels); + } + } +} + +/// The composited sample with what ADR 0031 adds, as artists use it: the ball casts a drop +/// shadow and is bevelled, the star glows, and a bitmap fills the sky. +fn filtered_sample() -> anicraft_doc::Document { + use anicraft_doc::Filter as F; + let mut doc = composited_sample(); + let png = anicraft_render::encode_png(48, 32, &pixels(48, 32, 7).rgba).unwrap(); + let bm = anicraft_doc::Bitmap::from_png(anicraft_doc::BitmapId(77), "sky", png).unwrap(); + doc.bitmaps.push(Arc::new(bm)); + let tl = doc.timeline_mut(0); + for (i, layer) in tl.layers.iter_mut().enumerate() { + let l = Arc::make_mut(layer); + for k in &mut l.keyframes { + for e in &mut Arc::make_mut(k).elements { + if let ElementKind::Symbol { filters, .. } = &mut Arc::make_mut(e).kind { + *filters = match i { + 0 => vec![F::DropShadow { blur_x: 8.0, blur_y: 8.0, strength: 0.8, quality: 2, angle: 45.0, distance: 10.0, color: Rgba::BLACK, inner: false, knockout: false, hide_object: false }, F::Bevel { blur_x: 5.0, blur_y: 5.0, strength: 1.0, quality: 1, angle: 45.0, distance: 4.0, shadow_color: Rgba::BLACK, highlight_color: Rgba::WHITE, side: anicraft_doc::FilterSide::Inner, knockout: false }], + _ => vec![F::Glow { blur_x: 12.0, blur_y: 12.0, strength: 1.4, quality: 2, color: Rgba::new(1.0, 0.85, 0.2, 1.0), inner: false, knockout: false }], + }; + } + } + } + } + let shape = anicraft_doc::Shape { path: Arc::new(Path::rect(0.0, 0.0, 1280.0, 300.0)), fill: Some(Paint::Bitmap { bitmap: anicraft_doc::BitmapId(77), repeat: true, matrix: Affine::scale(5.0, 5.0) }), stroke: None, fill_rule: FillRule::NonZero }; + let sky = anicraft_doc::Element { id: ElementId(9_100), name: "sky".into(), transform: Affine::IDENTITY, color: ColorEffect::default(), kind: ElementKind::Shape(shape) }; + let frames = doc.frame_count(); + let mut k = anicraft_doc::Keyframe::new(0, frames); + k.elements.push(Arc::new(sky)); + let mut layer = anicraft_doc::Layer::new(LayerId(9_101), "Sky"); + layer.keyframes = vec![Arc::new(k)]; + doc.timeline_mut(0).layers.push(Arc::new(layer)); + doc +} + +#[test] +fn gpu_matches_the_oracle_on_the_filtered_sample() { + let Some(g) = gpu() else { return }; + let doc = filtered_sample(); + let dl0 = anicraft_doc::evaluate(&doc, 0); + assert!(dl0.groups.iter().any(|g| !g.filters.is_empty()) && dl0.items.iter().any(|i| i.image.is_some()), "the sample has filters and a bitmap"); + for (f, s) in [(0u32, 1.0f32), (7, 0.5), (24, 1.5), (40, 0.37)] { + let dl = anicraft_doc::evaluate(&doc, f); + let d = assert_parity(g, "filtered sample", &dl, &View::stage(&doc, s)); + eprintln!("filtered sample {f} at {s}: mean {:.5} / max {} / {} of {} px differ", d.mean, d.max, d.differing, d.pixels); + // And a stage tile as the stage asks for it (512 px, zoomed and panned). + let tile = View { transform: Affine::translate(-300.0, -200.0).mul(&Affine::scale(s * 1.4, s * 1.4)), width: 512, height: 512, background: Some(doc.stage.background) }; + assert_parity(g, "filtered sample tile", &dl, &tile); + } +} diff --git a/crates/render/src/filters.rs b/crates/render/src/filters.rs index b25deda2..9a7e9947 100644 --- a/crates/render/src/filters.rs +++ b/crates/render/src/filters.rs @@ -18,57 +18,122 @@ use crate::paint::{Premul, premultiply}; use anicraft_doc::{Filter, FilterSide, GradientStop, Rgba, ramp_at}; -/// Applies `filters` in order. Sizes are stage pixels, multiplied by `scale` (the view's). -pub(crate) fn apply(buf: &mut [Premul], w: usize, h: usize, filters: &[Filter], scale: f32) { +/// Box widths of a blur: `passes` times a horizontal box `nx` wide, then a vertical one `ny` +/// tall (a width of 0 or 1 leaves the pixels as they are). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct BoxBlur { + pub nx: usize, + pub ny: usize, + /// At least 1. + pub passes: u8, +} + +impl BoxBlur { + fn new(bx: f32, by: f32, quality: u8) -> BoxBlur { + BoxBlur { nx: bx.round().max(0.0) as usize, ny: by.round().max(0.0) as usize, passes: quality.max(1) } + } +} + +/// How a bevel colours its field: two colours, or a ramp table (Gradient Bevel). +#[derive(Clone, Debug, PartialEq)] +pub enum BevelPaint { + /// Premultiplied highlight and shadow colours. + Colors { highlight: Premul, shadow: Premul }, + /// [`RAMP_STEPS`]` + 1` premultiplied colours, highlight end first. + Ramp(Vec), +} + +/// A filter resolved for one view scale, exactly as [`apply`] runs it: sizes as whole box +/// widths and pixel offsets, colours sanitized or premultiplied, ramps and colour adjustments as +/// tables. Other renderers (the GPU stage, ADR 0031) run these steps, so every number they use +/// is the oracle's own. +#[derive(Clone, Debug, PartialEq)] +pub enum FilterOp { + /// Blur all four channels. + Blur(BoxBlur), + /// Drop Shadow and Glow: the opacity (`inner`: the transparency) moved by `(dx, dy)`, + /// blurred, times `strength` (clamped to 1), in `color` (straight, sanitized); under the + /// artwork, or inside it over the artwork (`inner`). `knockout` keeps only the shadow; + /// `hide_object` (outer) only the whole shadow. + Shadow { blur: BoxBlur, dx: i64, dy: i64, strength: f32, color: Rgba, inner: bool, knockout: bool, hide_object: bool }, + /// Bevel and Gradient Bevel: the blurred opacity at `(x + dx, y + dy)` minus at + /// `(x − dx, y − dy)` (clamped to ±1), times `strength`, coloured by `paint` (a ramp is read + /// at `0.5 − 0.5 v`), with the artwork as `side` and `knockout` say. + Bevel { blur: BoxBlur, dx: i64, dy: i64, strength: f32, paint: BevelPaint, side: FilterSide, knockout: bool }, + /// Gradient Glow: the opacity moved by `(dx, dy)` (inside: the transparency), blurred, times + /// `strength`, read from `ramp` ([`RAMP_STEPS`]` + 1` premultiplied colours). + GradientGlow { blur: BoxBlur, dx: i64, dy: i64, strength: f32, ramp: Vec, side: FilterSide, knockout: bool }, + /// Adjust Color: a colour matrix on unpremultiplied colour. + AdjustColor(Matrix), +} + +/// `f` at view scale `scale` (stage pixels to surface pixels; a broken scale counts as 1). +pub fn resolve(f: &Filter, scale: f32) -> FilterOp { + let scale = if scale.is_finite() && scale > 0.0 { scale } else { 1.0 }; + match f.sanitized() { + Filter::Blur { blur_x, blur_y, quality } => FilterOp::Blur(BoxBlur::new(blur_x * scale, blur_y * scale, quality)), + Filter::DropShadow { blur_x, blur_y, strength, quality, angle, distance, color, inner, knockout, hide_object } => { + let a = angle.to_radians(); + let (dx, dy) = ((distance * scale * a.cos()).round() as i64, (distance * scale * a.sin()).round() as i64); + let blur = BoxBlur::new(blur_x * scale, blur_y * scale, quality); + FilterOp::Shadow { blur, dx, dy, strength, color: color.sanitized(), inner, knockout: knockout || (hide_object && inner), hide_object } + } + Filter::Glow { blur_x, blur_y, strength, quality, color, inner, knockout } => { + FilterOp::Shadow { blur: BoxBlur::new(blur_x * scale, blur_y * scale, quality), dx: 0, dy: 0, strength, color: color.sanitized(), inner, knockout, hide_object: false } + } + Filter::Bevel { blur_x, blur_y, strength, quality, angle, distance, shadow_color, highlight_color, side, knockout } => { + let (dx, dy) = offset(angle, distance * scale); + let paint = BevelPaint::Colors { highlight: premultiply(highlight_color), shadow: premultiply(shadow_color) }; + FilterOp::Bevel { blur: BoxBlur::new(blur_x * scale, blur_y * scale, quality), dx, dy, strength, paint, side, knockout } + } + Filter::GradientBevel { blur_x, blur_y, strength, quality, angle, distance, stops, side, knockout } => { + let (dx, dy) = offset(angle, distance * scale); + FilterOp::Bevel { blur: BoxBlur::new(blur_x * scale, blur_y * scale, quality), dx, dy, strength, paint: BevelPaint::Ramp(ramp_lut(&stops)), side, knockout } + } + Filter::GradientGlow { blur_x, blur_y, strength, quality, angle, distance, stops, side, knockout } => { + let (dx, dy) = offset(angle, distance * scale); + FilterOp::GradientGlow { blur: BoxBlur::new(blur_x * scale, blur_y * scale, quality), dx, dy, strength, ramp: ramp_lut(&stops), side, knockout } + } + Filter::AdjustColor { brightness, contrast, saturation, hue } => FilterOp::AdjustColor(color_matrix(brightness, contrast, saturation, hue)), + } +} + +/// Applies `filters` in order to premultiplied pixels, `w × h`, row by row. Sizes are stage +/// pixels, multiplied by `scale` (the view's). +pub fn apply(buf: &mut [Premul], w: usize, h: usize, filters: &[Filter], scale: f32) { if buf.len() != w * h || w == 0 || h == 0 { return; } - let scale = if scale.is_finite() && scale > 0.0 { scale } else { 1.0 }; for f in filters { - match f.sanitized() { - Filter::Blur { blur_x, blur_y, quality } => blur(buf, w, h, blur_x * scale, blur_y * scale, quality), - Filter::DropShadow { blur_x, blur_y, strength, quality, angle, distance, color, inner, knockout, hide_object } => { - let a = angle.to_radians(); - let (dx, dy) = ((distance * scale * a.cos()).round() as i64, (distance * scale * a.sin()).round() as i64); - shadow(buf, w, h, Shadow { blur_x: blur_x * scale, blur_y: blur_y * scale, strength, quality, dx, dy, color, inner, knockout: knockout || (hide_object && inner), hide_object }); - } - Filter::Glow { blur_x, blur_y, strength, quality, color, inner, knockout } => { - shadow(buf, w, h, Shadow { blur_x: blur_x * scale, blur_y: blur_y * scale, strength, quality, dx: 0, dy: 0, color, inner, knockout, hide_object: false }); - } - Filter::Bevel { blur_x, blur_y, strength, quality, angle, distance, shadow_color, highlight_color, side, knockout } => { - let (dx, dy) = offset(angle, distance * scale); - let field = bevel_field(buf, w, h, blur_x * scale, blur_y * scale, quality, dx, dy); - let (hl, sh) = (premultiply(highlight_color), premultiply(shadow_color)); - let effect: Vec = field - .iter() - .map(|&d| { - let v = (d * strength).clamp(-1.0, 1.0); - if v >= 0.0 { scaled(hl, v) } else { scaled(sh, -v) } - }) - .collect(); + match resolve(f, scale) { + FilterOp::Blur(b) => blur_channels(buf, w, h, b), + FilterOp::Shadow { blur, dx, dy, strength, color, inner, knockout, hide_object } => shadow(buf, w, h, Shadow { blur, strength, dx, dy, color, inner, knockout, hide_object }), + FilterOp::Bevel { blur, dx, dy, strength, paint, side, knockout } => { + let field = bevel_field(buf, w, h, blur, dx, dy); + let effect: Vec = match &paint { + BevelPaint::Colors { highlight, shadow } => field + .iter() + .map(|&d| { + let v = (d * strength).clamp(-1.0, 1.0); + if v >= 0.0 { scaled(*highlight, v) } else { scaled(*shadow, -v) } + }) + .collect(), + BevelPaint::Ramp(lut) => field.iter().map(|&d| lut_at(lut, 0.5 - 0.5 * (d * strength).clamp(-1.0, 1.0))).collect(), + }; composite(buf, &effect, &effect, side, knockout); } - Filter::GradientBevel { blur_x, blur_y, strength, quality, angle, distance, stops, side, knockout } => { - let (dx, dy) = offset(angle, distance * scale); - let field = bevel_field(buf, w, h, blur_x * scale, blur_y * scale, quality, dx, dy); - let lut = ramp_lut(&stops); - let effect: Vec = field.iter().map(|&d| lut_at(&lut, 0.5 - 0.5 * (d * strength).clamp(-1.0, 1.0))).collect(); - composite(buf, &effect, &effect, side, knockout); - } - Filter::GradientGlow { blur_x, blur_y, strength, quality, angle, distance, stops, side, knockout } => { - let (dx, dy) = offset(angle, distance * scale); - let lut = ramp_lut(&stops); + FilterOp::GradientGlow { blur, dx, dy, strength, ramp, side, knockout } => { let glow = |inner: bool| -> Vec { let mut cov = shifted_coverage(buf, w, h, dx, dy, inner); - blur_channels(&mut cov, w, h, blur_x * scale, blur_y * scale, quality); - cov.iter().map(|k| lut_at(&lut, (k[0] * strength).clamp(0.0, 1.0))).collect() + blur_channels(&mut cov, w, h, blur); + cov.iter().map(|k| lut_at(&ramp, (k[0] * strength).clamp(0.0, 1.0))).collect() }; let none = Vec::new(); let inner = if side == FilterSide::Outer { None } else { Some(glow(true)) }; let outer = if side == FilterSide::Inner { None } else { Some(glow(false)) }; composite(buf, inner.as_ref().unwrap_or(&none), outer.as_ref().unwrap_or(&none), side, knockout); } - Filter::AdjustColor { brightness, contrast, saturation, hue } => adjust(buf, &color_matrix(brightness, contrast, saturation, hue)), + FilterOp::AdjustColor(m) => adjust(buf, &m), } } } @@ -105,10 +170,9 @@ fn alpha_at(buf: &[Premul], w: usize, h: usize, x: i64, y: i64) -> f32 { /// The bevel's signed field in −1..=1: the blurred opacity a distance towards the light /// (`−(dx, dy)`) minus the blurred opacity a distance away from it. Positive where an edge faces /// the light (highlight), negative where it faces away (shadow), 0 where flat. -#[allow(clippy::too_many_arguments)] -fn bevel_field(buf: &[Premul], w: usize, h: usize, bx: f32, by: f32, quality: u8, dx: i64, dy: i64) -> Vec { +fn bevel_field(buf: &[Premul], w: usize, h: usize, blur: BoxBlur, dx: i64, dy: i64) -> Vec { let mut b: Vec<[f32; 1]> = buf.iter().map(|p| [p[3]]).collect(); - blur_channels(&mut b, w, h, bx, by, quality); + blur_channels(&mut b, w, h, blur); let at = |x: i64, y: i64| if x >= 0 && y >= 0 && (x as usize) < w && (y as usize) < h { b.get(y as usize * w + x as usize).map_or(0.0, |v| v[0]) } else { 0.0 }; let mut out = Vec::with_capacity(w * h); for y in 0..h as i64 { @@ -121,7 +185,7 @@ fn bevel_field(buf: &[Premul], w: usize, h: usize, bx: f32, by: f32, quality: u8 /// Steps in a gradient filter's ramp table: even, so 0, ½ and 1 (the ends and the bevel's /// flat middle) are samples exactly. -pub(crate) const RAMP_STEPS: usize = 256; +pub const RAMP_STEPS: usize = 256; /// A gradient filter's ramp (sanitized stops) as `RAMP_STEPS + 1` premultiplied colours. fn ramp_lut(stops: &[GradientStop]) -> Vec { @@ -201,24 +265,17 @@ fn box_pass(buf: &mut [[f32; C]], w: usize, h: usize, n: usize, } } -fn blur_channels(buf: &mut [[f32; C]], w: usize, h: usize, bx: f32, by: f32, passes: u8) { - let (nx, ny) = (bx.round().max(0.0) as usize, by.round().max(0.0) as usize); +fn blur_channels(buf: &mut [[f32; C]], w: usize, h: usize, b: BoxBlur) { let mut tmp = Vec::new(); - for _ in 0..passes.max(1) { - box_pass(buf, w, h, nx, true, &mut tmp); - box_pass(buf, w, h, ny, false, &mut tmp); + for _ in 0..b.passes.max(1) { + box_pass(buf, w, h, b.nx, true, &mut tmp); + box_pass(buf, w, h, b.ny, false, &mut tmp); } } -fn blur(buf: &mut [Premul], w: usize, h: usize, bx: f32, by: f32, passes: u8) { - blur_channels(buf, w, h, bx, by, passes); -} - struct Shadow { - blur_x: f32, - blur_y: f32, + blur: BoxBlur, strength: f32, - quality: u8, dx: i64, dy: i64, color: Rgba, @@ -239,8 +296,8 @@ fn shadow(buf: &mut [Premul], w: usize, h: usize, s: Shadow) { } } } - blur_channels(&mut cov, w, h, s.blur_x, s.blur_y, s.quality); - let c = s.color.sanitized(); + blur_channels(&mut cov, w, h, s.blur); + let c = s.color; for (p, k) in buf.iter_mut().zip(&cov) { let sa = (k[0] * s.strength).clamp(0.0, 1.0) * c.a; let src = *p; @@ -263,7 +320,7 @@ fn shadow(buf: &mut [Premul], w: usize, h: usize, s: Shadow) { } /// A 4 × 5 colour matrix (rows r, g, b, a; columns r, g, b, a, offset), straight colour 0..1. -type Matrix = [[f32; 5]; 4]; +pub type Matrix = [[f32; 5]; 4]; const IDENTITY: Matrix = [[1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0, 0.0]]; @@ -283,7 +340,7 @@ fn mul(a: &Matrix, b: &Matrix) -> Matrix { } /// Brightness, contrast and saturation in −100..=100, hue in degrees. -pub(crate) fn color_matrix(brightness: f32, contrast: f32, saturation: f32, hue: f32) -> Matrix { +pub fn color_matrix(brightness: f32, contrast: f32, saturation: f32, hue: f32) -> Matrix { let mut m = IDENTITY; // Brightness: an offset (100 adds the full range). let b = brightness / 100.0; diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 90cef66c..a08ea6b7 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -7,7 +7,7 @@ pub mod paint; pub mod raster; pub mod stroke; -mod filters; +pub mod filters; pub mod html; pub mod lottie; pub mod svg; @@ -153,12 +153,7 @@ pub fn render(dl: &DisplayList, view: &View) -> Result<(Pixmap, RenderStats), Re // Filtered groups are drawn on their own, filtered, and come back as one image each. let runs = filtered_runs(dl); - let mut in_run = vec![false; dl.items.len()]; - for r in &runs { - for f in in_run.iter_mut().take(r.end).skip(r.start) { - *f = true; - } - } + let in_run = in_runs(dl, &runs); // Phase 1 (parallel over items): cull, flatten, stroke and build shaders. Output keeps // display-list order. @@ -245,17 +240,18 @@ pub fn render(dl: &DisplayList, view: &View) -> Result<(Pixmap, RenderStats), Re Ok((pm, stats)) } -/// Why a display list needs the full renderer rather than a prepared [`Scene`] (see -/// [`prepare_scene`]). +/// Why a display list (or one stage tile of it) needs the full renderer: what another renderer +/// drawing a prepared [`Scene`] can't (see [`prepare_scene`] and, for the GPU stage's limits, +/// ADR 0031). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum NeedsFull { /// Mask layers nested deeper than the other renderer handles. Clips, /// Symbol instances nested deeper than the other renderer handles. Groups, - /// Filters (blurs, shadows, glows, bevels, colour adjustments) on a symbol instance. + /// Filtered symbols nested deeper than the other renderer handles. Filters, - /// Bitmap fills. + /// More bitmaps at once, or larger ones, than the other renderer handles. Bitmaps, } @@ -264,8 +260,8 @@ impl std::fmt::Display for NeedsFull { f.write_str(match self { NeedsFull::Clips => "deeply nested masks", NeedsFull::Groups => "deeply nested symbols", - NeedsFull::Filters => "filters", - NeedsFull::Bitmaps => "bitmap fills", + NeedsFull::Filters => "deeply nested filters", + NeedsFull::Bitmaps => "too many or too large bitmaps", }) } } @@ -284,6 +280,28 @@ pub struct Fill { pub clip: Option, /// Composited inside this group ([`Scene::groups`]) and the groups around it. pub group: Option, + /// The pixels of this filtered layer ([`Scene::layers`]), where it covers the fill (a + /// rectangle on whole pixels), instead of `shader` (then a transparent placeholder). As + /// [`render`] draws it: the layer's pixel at `(x, y)` is at `(x - x0, y - y0)` of its + /// `bounds`, transparent outside. + pub layer: Option, +} + +/// A filtered group (a symbol instance with filters) as [`render`] draws it: its items on +/// their own, into a transparent buffer of `bounds` (its artwork grown by how far its filters +/// reach, within the surface's room), then its filters in order. The result is composited as +/// one fill ([`Fill::layer`]) in the group, clipped by the clip around the instance. +pub struct FilterLayer { + /// The group's items, with the group neutral (its opacity and blend mode apply when the + /// layer is composited) and clips from outside it removed. It may hold filtered groups of + /// its own (nested symbols): prepare it as its own scene. + pub list: DisplayList, + /// Where `list` draws: `bounds`' size, transparent. + pub view: View, + /// The buffer's place, in surface pixels. + pub bounds: IRect, + /// The group's filters, resolved for the view's scale ([`filters::resolve`]). + pub filters: Vec, } /// A clip (a mask layer's area) as [`render`] composites it. @@ -317,32 +335,35 @@ pub struct Scene { pub fills: Vec, pub clips: Vec, pub groups: Vec, + /// Filtered groups, drawn and filtered before the fills that show them ([`Fill::layer`]). + pub layers: Vec, } -/// The display list as a [`Scene`], with the same culling and statistics as [`render`]; or why -/// it needs the full renderer (filters, bitmaps). +/// The display list as a [`Scene`], with the same culling and statistics as [`render`]. Bitmap +/// fills come as [`Shader::Bitmap`]; filtered groups as [`Scene::layers`]. It doesn't fail +/// today; the `Err` is for what a future display list could hold that only [`render`] draws. pub fn prepare_scene(dl: &DisplayList, view: &View) -> Result<(Scene, RenderStats), NeedsFull> { - if dl.groups.iter().any(|g| !g.filters.is_empty()) { - return Err(NeedsFull::Filters); - } - for it in &dl.items { - if !it.guide && it.outline.is_none() && it.masks.is_none() && matches!(it.fill, Some(Paint::Bitmap { .. })) { - return Err(NeedsFull::Bitmaps); + let surface = IRect { x0: 0, y0: 0, x1: view.width as i32, y1: view.height as i32 }; + let runs = filtered_runs(dl); + let in_run = in_runs(dl, &runs); + let mut prepared: Vec> = dl.items.par_iter().zip(&in_run).map(|(item, &skip)| if skip { Vec::new() } else { prepare(item, &view.transform, surface) }).collect(); + let mut layers = Vec::new(); + for r in &runs { + if let (Some(setup), Some(slot)) = (filter_setup(dl, r, view, surface), prepared.get_mut(r.start)) { + let mut op = setup.op(Shader::Solid([0.0; 4])); + op.layer = Some(layers.len() as u32); + slot.push(op); + layers.push(setup.layer); } } - let surface = IRect { x0: 0, y0: 0, x1: view.width as i32, y1: view.height as i32 }; - let prepared: Vec> = dl.items.par_iter().map(|item| prepare(item, &view.transform, surface)).collect(); let mut stats = RenderStats { items: dl.items.len(), ..Default::default() }; let mut ops: Vec = Vec::with_capacity(prepared.len()); let mut mask_ops: Vec = Vec::new(); - for p in prepared { - if p.is_empty() { + for (i, p) in prepared.into_iter().enumerate() { + if p.is_empty() && !in_run.get(i).copied().unwrap_or(false) { stats.culled += 1; } for op in p { - if matches!(op.shader, Shader::Bitmap { .. } | Shader::Image { .. }) { - return Err(NeedsFull::Bitmaps); - } if op.masks.is_some() { mask_ops.push(op) } else { ops.push(op) } } } @@ -353,7 +374,7 @@ pub fn prepare_scene(dl: &DisplayList, view: &View) -> Result<(Scene, RenderStat op.bounds = op.bounds.intersect(&clips.bounds(c)); !op.bounds.is_empty() }); - let fill = |op: Prepared| Fill { polys: op.polys, rule: op.rule, shader: op.shader, bounds: op.bounds, clip: op.clip, group: op.group }; + let fill = |op: Prepared| Fill { polys: op.polys, rule: op.rule, shader: op.shader, bounds: op.bounds, clip: op.clip, group: op.group, layer: op.layer }; let mut fills = Vec::with_capacity(ops.len()); for op in ops { stats.edges += op.polys.iter().map(Vec::len).sum::(); @@ -368,7 +389,7 @@ pub fn prepare_scene(dl: &DisplayList, view: &View) -> Result<(Scene, RenderStat .zip(&dl.groups) .map(|(chain, g)| GroupLayer { chain, alpha: if g.alpha.is_finite() { g.alpha.clamp(0.0, 1.0) } else { 1.0 }, blend: g.blend }) .collect(); - Ok((Scene { fills, clips, groups }, stats)) + Ok((Scene { fills, clips, groups, layers }, stats)) } /// A run of items inside a filtered group that has no filtered group around it. @@ -398,14 +419,51 @@ fn filtered_runs(dl: &DisplayList) -> Vec { out } +/// Which items are inside a filtered run (drawn with their group, not on their own). +fn in_runs(dl: &DisplayList, runs: &[FilteredRun]) -> Vec { + let mut in_run = vec![false; dl.items.len()]; + for r in runs { + for f in in_run.iter_mut().take(r.end).skip(r.start) { + *f = true; + } + } + in_run +} + /// Largest buffer a filtered group is drawn into (pixels): what [`render`] accepts. const MAX_FILTER_PIXELS: i64 = MAX_PIXELS as i64; -/// Draws a filtered group's items on their own (the group neutral, clips from outside it -/// left for later), applies its filters, and returns the result as one image op in the group, -/// clipped by the clip in force where the instance is. -fn filtered_group(dl: &DisplayList, run: &FilteredRun, view: &View, surface: IRect) -> Result, RenderError> { - let Some(group) = dl.groups.get(run.group as usize) else { return Ok(None) }; +/// A filtered group set up for drawing: the layer, and the part of it the surface shows, with +/// the clip and group it is composited in. +struct FilterSetup { + layer: FilterLayer, + shown: IRect, + clip: Option, + group: u32, +} + +impl FilterSetup { + /// The op that composites the layer (a rectangle on whole pixels), painted by `shader`. + fn op(&self, shader: Shader) -> Prepared { + let s = self.shown; + let (x0, y0, x1, y1) = (s.x0 as f32, s.y0 as f32, s.x1 as f32, s.y1 as f32); + Prepared { + polys: vec![vec![Vec2::new(x0, y0), Vec2::new(x1, y0), Vec2::new(x1, y1), Vec2::new(x0, y1)]], + rule: FillRule::NonZero, + shader, + bounds: s, + clip: self.clip, + masks: None, + group: Some(self.group), + layer: None, + } + } +} + +/// Where and how a filtered group's items are drawn on their own (the group neutral, clips from +/// outside it left for later); `None` when nothing of it can show. +fn filter_setup(dl: &DisplayList, run: &FilteredRun, view: &View, surface: IRect) -> Option { + let group = dl.groups.get(run.group as usize)?; let items = dl.items.get(run.start..run.end).unwrap_or(&[]); let scale = view.transform.mean_scale(); let margin = (group.filters.iter().map(anicraft_doc::Filter::margin).sum::() * scale).ceil() + 2.0; @@ -416,7 +474,7 @@ fn filtered_group(dl: &DisplayList, run: &FilteredRun, view: &View, surface: IRe r = r.union(&it.path.bounds().inflate(pad).transformed(&view.transform.mul(&it.transform))); } if r.is_empty() || !r.min.x.is_finite() || !r.max.y.is_finite() { - return Ok(None); + return None; } let reach = margin.clamp(0.0, (i32::MAX / 4) as f32) as i32; // How far past the surface it draws: the reach, but no further than keeps the buffer within @@ -434,7 +492,11 @@ fn filtered_group(dl: &DisplayList, run: &FilteredRun, view: &View, surface: IRe } .intersect(&limit); if b.is_empty() || (b.width() as i64) * (b.height() as i64) > MAX_FILTER_PIXELS { - return Ok(None); + return None; + } + let shown = b.intersect(&surface); + if shown.is_empty() { + return None; } // The group's own clips (masks inside the symbol) apply before filtering; others after. let inner: std::collections::HashSet = items.iter().filter_map(|i| i.masks).collect(); @@ -442,28 +504,25 @@ fn filtered_group(dl: &DisplayList, run: &FilteredRun, view: &View, surface: IRe if let Some(g) = groups.get_mut(run.group as usize) { *g = anicraft_doc::Group { parent: None, alpha: 1.0, blend: BlendMode::Normal, filters: Default::default(), clip: None }; } - let sub = DisplayList { + let list = DisplayList { items: items.iter().map(|i| DisplayItem { clip: i.clip.filter(|c| inner.contains(c)), ..i.clone() }).collect(), clip_parents: dl.clip_parents.iter().map(|p| p.filter(|c| inner.contains(c))).collect(), groups, }; let sub_view = View { transform: Affine::translate(-b.x0 as f32, -b.y0 as f32).mul(&view.transform), width: b.width() as u32, height: b.height() as u32, background: None }; - let (mut pm, _) = render(&sub, &sub_view)?; - filters::apply(&mut pm.data, b.width(), b.height(), &group.filters, scale); - let shown = b.intersect(&surface); - if shown.is_empty() { - return Ok(None); - } - let (x0, y0, x1, y1) = (shown.x0 as f32, shown.y0 as f32, shown.x1 as f32, shown.y1 as f32); - Ok(Some(Prepared { - polys: vec![vec![Vec2::new(x0, y0), Vec2::new(x1, y0), Vec2::new(x1, y1), Vec2::new(x0, y1)]], - rule: FillRule::NonZero, - shader: Shader::Image { data: std::sync::Arc::new(pm.data), x0: b.x0, y0: b.y0, w: b.width() }, - bounds: shown, - clip: group.clip, - masks: None, - group: Some(run.group), - })) + let filters = group.filters.iter().map(|f| filters::resolve(f, scale)).collect(); + Some(FilterSetup { layer: FilterLayer { list, view: sub_view, bounds: b, filters }, shown, clip: group.clip, group: run.group }) +} + +/// Draws a filtered group's items on their own, applies its filters, and returns the result as +/// one image op in the group, clipped by the clip in force where the instance is. +fn filtered_group(dl: &DisplayList, run: &FilteredRun, view: &View, surface: IRect) -> Result, RenderError> { + let Some(setup) = filter_setup(dl, run, view, surface) else { return Ok(None) }; + let Some(group) = dl.groups.get(run.group as usize) else { return Ok(None) }; + let b = setup.layer.bounds; + let (mut pm, _) = render(&setup.layer.list, &setup.layer.view)?; + filters::apply(&mut pm.data, b.width(), b.height(), &group.filters, view.transform.mean_scale()); + Ok(Some(setup.op(Shader::Image { data: std::sync::Arc::new(pm.data), x0: b.x0, y0: b.y0, w: b.width() }))) } /// A group being composited in one band: its own buffer (laid out like the band's rows) and @@ -586,6 +645,8 @@ struct Prepared { masks: Option, /// Composited inside this group (and its parents). group: Option, + /// A filtered layer's pixels instead of `shader` ([`Fill::layer`]; only in [`prepare_scene`]). + layer: Option, } /// The clips of a display list: their mask artwork and where each can be non-zero. @@ -714,7 +775,7 @@ fn prepare(item: &DisplayItem, view: &Affine, surface: IRect) -> Vec { return; } if let Some(b) = pixel_bounds(&polys, surface) { - out.push(Prepared { polys, rule, shader, bounds: b, clip, masks, group }); + out.push(Prepared { polys, rule, shader, bounds: b, clip, masks, group, layer: None }); } }; @@ -964,12 +1025,29 @@ mod tests { let (scene, _) = prepare_scene(&dl, &view(16, 16)).unwrap(); assert!(scene.fills.iter().all(|f| f.group == Some(1))); assert_eq!((scene.groups[1].chain.clone(), scene.groups[0].alpha, scene.groups[1].alpha, scene.groups[1].blend), (vec![0, 1], 1.0, 1.0, BlendMode::Multiply)); - // Filters and bitmaps need the full renderer. + // A filtered group is one layer, shown by one fill in the outermost filtered group, its + // filters resolved for the view; drawing the layer and filtering it is what `render` + // composites there. let mut filtered = dl.clone(); filtered.groups[0].filters = std::sync::Arc::from(vec![anicraft_doc::Filter::Blur { blur_x: 4.0, blur_y: 4.0, quality: 1 }]); - assert_eq!(prepare_scene(&filtered, &view(16, 16)).err(), Some(NeedsFull::Filters)); - let bm = item(Path::rect(0.0, 0.0, 4.0, 4.0), Paint::Bitmap { bitmap: anicraft_doc::BitmapId(1), repeat: false, matrix: Affine::IDENTITY }); - assert_eq!(prepare_scene(&list(vec![bm]), &view(16, 16)).err(), Some(NeedsFull::Bitmaps)); + let (scene, st) = prepare_scene(&filtered, &view(16, 16)).unwrap(); + let (pm, rst) = render(&filtered, &view(16, 16)).unwrap(); + assert_eq!((st.drawn, st.culled), (rst.drawn, rst.culled)); + assert_eq!(scene.layers.len(), 1); + let (f, l) = (&scene.fills[0], &scene.layers[0]); + assert_eq!((scene.fills.len(), f.layer, f.group, f.bounds), (1, Some(0), Some(0), raster::IRect { x0: 0, y0: 0, x1: 16, y1: 16 })); + assert_eq!(l.filters, vec![filters::FilterOp::Blur(filters::BoxBlur { nx: 4, ny: 4, passes: 1 })]); + assert_eq!((l.view.width, l.view.height), (l.bounds.width() as u32, l.bounds.height() as u32)); + let (mut layer, _) = render(&l.list, &l.view).unwrap(); + filters::apply(&mut layer.data, l.bounds.width(), l.bounds.height(), &filtered.groups[0].filters, 1.0); + let at = |x: i32, y: i32| layer.data[((y - l.bounds.y0) * l.bounds.width() as i32 + x - l.bounds.x0) as usize]; + assert_eq!(pm.pixel(5, 6).unwrap(), at(5, 6), "the group is opaque and normal: the layer as it is"); + // Bitmap fills come as bitmap shaders. + let px = std::sync::Arc::new(anicraft_doc::Pixels { width: 1, height: 1, rgba: vec![255; 4] }); + let mut bm = item(Path::rect(0.0, 0.0, 4.0, 4.0), Paint::Bitmap { bitmap: anicraft_doc::BitmapId(1), repeat: false, matrix: Affine::IDENTITY }); + bm.image = Some(px); + let (scene, _) = prepare_scene(&list(vec![bm]), &view(16, 16)).unwrap(); + assert!(matches!(scene.fills[0].shader, Shader::Bitmap { .. }) && scene.layers.is_empty()); } #[test]