The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.
Python
Add transparent layer compositing
apoogdk3f205d4cc935
diff --git a/rust/engine/src/blend.wgsl b/rust/engine/src/blend.wgslnew file mode 100644index 00000000..32299fff--- /dev/null+++ b/rust/engine/src/blend.wgsl@@ -0,0 +1,585 @@+// The raster pipeline: point clouds and meshes (paths are drawn exactly: see `vector.wgsl`), a 3D+// view's into its z-buffered base, a 2D view's each alone into its rectangle of a raster atlas.+// Every object is drawn from resident shapes: clip = C1·S1 + C2·S2 (term 2 only while morphing),+// where Ck = camera · Mk is composed on the CPU once per object: the camera is one more affine map+// in the blend, so a vertex costs one 4×4 per term and no branches. Fixed-in-frame objects are just+// another matrix; depth is real depth.+//+// Kinds differ only in how a shape's vertices become triangles:+// points a camera-facing disk per point, sized in scene units; one with no opacity makes no+// fragments.+// meshes their triangles, optionally textured; a surface's faces' edges as screen-space ribbons+// with miter joins.+// Colors are per object (flat); only gradients are evaluated per fragment, in their own shaders.+// A tween's paint is two paints and how far the second is mixed in: colors are mixed once per+// object on the CPU, rows (gradient stops, per-vertex colors) here — so a tween uploads nothing.++struct View {+ pixels: vec4<f32>, // width, height (pixels), samples per pixel, unused+ light: vec4<f32>, // CE's light position+ toward: vec4<f32>, // unit vector toward the viewer (two-sided lighting); w: stroke depth bias+ eye: vec4<f32>, // where it sees from (w = 1: a 3D view's perspective)+ lighting: vec4<f32>, // its lights (`light.wgsl`), how many; the exposure; the tone mapping (1: AgX); its bloom+ lights: array<Light, 8>,+ harmonics: array<vec4<f32>, 9>, // its environment's diffuse light (`environment.rs`)+ occlusion: vec4<f32>, // its ambient occlusion: how much (0: none), how far (`occlusion.rs`)+};++struct Instance {+ c1_0: vec4<f32>, c1_1: vec4<f32>, c1_2: vec4<f32>, c1_3: vec4<f32>, // clip matrix of term 1+ c2_0: vec4<f32>, c2_1: vec4<f32>, c2_2: vec4<f32>, c2_3: vec4<f32>, // clip matrix of term 2+ m1_0: vec4<f32>, m1_1: vec4<f32>, m1_2: vec4<f32>, // world matrices (lit meshes)+ m2_0: vec4<f32>, m2_1: vec4<f32>, m2_2: vec4<f32>,+ params: vec4<f32>, // window lo, window hi, stroke width px, background width px+ extra: vec4<f32>, // point radius px at w = 1, light amount (gradients)+ ids: vec4<u32>, // vertex base of term 1, of term 2 (NONE), flags (from bit 8: the record's place in its view)+ fill: vec4<f32>, // the paint (`Paint`): solid colors (alpha −1: a gradient)+ stroke: vec4<f32>,+ background: vec4<f32>,+ gradient: vec4<f32>, // gradient axis on screen: origin (px), direction / |direction|² (px)+ brush: vec4<u32>, // fill rows (offset, count), stroke rows (offset, count)+ brush2: vec4<u32>, // paint 2's fill rows, stroke rows (offsets, counts as paint 1's); how far it is mixed in (f32 bits, 0: not)+ material: vec4<f32>, // metallic, roughness, reflectance (an instance with MATERIAL: lit by the view's lights)+};++const NONE: u32 = 0xffffffffu;+const LIT: u32 = 4u;+const TEXTURED: u32 = 8u;+const NEAREST: u32 = 16u;+const CUBIC: u32 = 32u;+const MATERIAL: u32 = 64u;++// The rest it reads (the store's arrays, the objects) comes through its head's loaders (`blend_buffers.wgsl`); a 3D+// view's see-through lists' entries are in `blend_lists.wgsl`.+@group(0) @binding(0) var<uniform> view: View;+// the view's lights' shadow maps (`shadow.rs`), a layer each, and how they are compared (bilinearly): a group of+// their own, which the passes that draw them do not bind+@group(3) @binding(0) var shadow_maps: texture_depth_2d_array;+@group(3) @binding(1) var shadow_compare: sampler_comparison;+@group(3) @binding(2) var dfg_table: texture_2d<f32>;+@group(3) @binding(3) var dfg_linear: sampler;+@group(3) @binding(4) var environment: texture_cube<f32>;+@group(3) @binding(5) var occlusion: texture_2d<f32>; // its ambient occlusion (`occlusion.rs`; 1x1 where it has none)+@group(1) @binding(0) var image: texture_2d<f32>;+@group(1) @binding(1) var image_sampler: sampler;++fn light_count() -> u32 {+ return u32(view.lighting.x);+}++fn light_at(i: u32) -> Light {+ return view.lights[i];+}++// The shadow maps, as `light.wgsl`'s `shadowed` reads them: a layer's comparison at uv (bilinear), a texel's size.+fn shadow_compare_at(uv: vec2<f32>, layer: i32, depth: f32) -> f32 {+ return textureSampleCompareLevel(shadow_maps, shadow_compare, uv, layer, depth);+}++fn shadow_texel() -> vec2<f32> {+ return 1.0 / vec2<f32>(textureDimensions(shadow_maps));+}++// The DFG table at (NoV, perceptual roughness), as `light.wgsl`'s `reflected` reads it.+fn dfg_at(NoV: f32, perceptual: f32) -> vec2<f32> {+ return textureSampleLevel(dfg_table, dfg_linear, vec2<f32>(NoV, perceptual), 0.0).xy;+}++// The environment, as `light.wgsl`'s `reflected` reads it: its cube's light toward d at a level, and its diffuse+// light's harmonics.+fn environment_at(d: vec3<f32>, level: f32) -> vec3<f32> {+ return textureSampleLevel(environment, dfg_linear, d, level).rgb;+}++fn harmonic(i: u32) -> vec3<f32> {+ return view.harmonics[i].xyz;+}++// The clip position of vertex `index` of object `i`.+fn clip(i: u32, index: u32) -> vec4<f32> {+ let h = vec4<f32>(vertex(index).xyz, 1.0);+ var c = vec4<f32>(dot(c1_0_of(i), h), dot(c1_1_of(i), h), dot(c1_2_of(i), h), dot(c1_3_of(i), h));+ let base2 = ids_of(i).y;+ if (base2 != NONE) {+ let g = vec4<f32>(vertex(base2 + (index - ids_of(i).x)).xyz, 1.0);+ c += vec4<f32>(dot(c2_0_of(i), g), dot(c2_1_of(i), g), dot(c2_2_of(i), g), dot(c2_3_of(i), g));+ }+ return c;+}++// Where a direction `d` from vertex `index` (and its morph's `d2` from term 2's) moves its clip position: so `clip`+// plus this is the clip position of the point that far along.+fn clip_along(i: u32, d: vec3<f32>, d2: vec3<f32>) -> vec4<f32> {+ let h = vec4<f32>(d, 0.0);+ var c = vec4<f32>(dot(c1_0_of(i), h), dot(c1_1_of(i), h), dot(c1_2_of(i), h), dot(c1_3_of(i), h));+ if (ids_of(i).y != NONE) {+ let g = vec4<f32>(d2, 0.0);+ c += vec4<f32>(dot(c2_0_of(i), g), dot(c2_1_of(i), g), dot(c2_2_of(i), g), dot(c2_3_of(i), g));+ }+ return c;+}++// A mesh face's depth over the screen at its edge from vertex `at` toward vertex `to`: the plane tangent to it there+// (its normal at `at`: a flat face's own, a refined surface's), as (its depth gradient in pixels, its depth at `at`),+// from `at`, `to` and the point across the edge in that plane; none (a zero gradient, `at`'s own depth) where the face+// has no normal or is seen edge on. So the face's edge drawn by it lies on it, to the depth's digits.+fn face_plane(i: u32, at: u32, to: u32) -> vec3<f32> {+ let sa = screen(clip(i, at));+ let n = vertex_extra(at).xyz;+ let edge = vertex(to).xyz - vertex(at).xyz;+ var across2 = vec3<f32>(0.0);+ if (ids_of(i).y != NONE) {+ let k = ids_of(i).y - ids_of(i).x;+ across2 = cross(vertex_extra(at + k).xyz, vertex(to + k).xyz - vertex(at + k).xyz);+ }+ let sb = screen(clip(i, to));+ let sc = screen(clip(i, at) + clip_along(i, cross(n, edge), across2));+ let d1 = sb.xy - sa.xy;+ let d2 = sc.xy - sa.xy;+ let det = d1.x * d2.y - d1.y * d2.x;+ if (dot(n, n) < 1e-24 || abs(det) < 1e-6 * (dot(d1, d1) + dot(d2, d2)) || sb.z < 0.0 || sc.z < 0.0) {+ return vec3<f32>(0.0, 0.0, sa.z);+ }+ let dz1 = sb.z - sa.z;+ let dz2 = sc.z - sa.z;+ return vec3<f32>(dz1 * d2.y - dz2 * d1.y, dz2 * d1.x - dz1 * d2.x, 0.0) / det + vec3<f32>(0.0, 0.0, sa.z);+}++// How much nearer a face's edge is drawn than its face: a share of its depth (16 of its f32 steps) and its depth slope+// times a share of a pixel (4 times the half step the rasterizer snaps corners to: Direct3D's, Metal's, Vulkan's 1/256).+// `depth.wgsl` takes a face's pixels with its edges on them as the face's (its `STROKES`).+const PULL: vec2<f32> = vec2<f32>(16.0 * 1.2e-7, 4.0 / 512.0);++// Clip → (pixel x, pixel y, depth); points behind the camera get depth -1 (clipped away).+fn screen(c: vec4<f32>) -> vec3<f32> {+ if (c.w <= 1e-6) {+ return vec3<f32>(0.0, 0.0, -1.0);+ }+ return vec3<f32>((c.xy / c.w * 0.5 + 0.5) * view.pixels.xy, c.z / c.w);+}++fn to_clip(s: vec3<f32>) -> vec4<f32> {+ return vec4<f32>(s.xy / view.pixels.xy * 2.0 - 1.0, s.z, 1.0);+}++// The gradient at t along its axis (`stops`), lit like the object.+fn gradient(i: u32, first: u32, second: u32, count: u32, t: f32, light: f32) -> vec4<f32> {+ let c = stops(first, second, count, bitcast<f32>(brush2_of(i).z), t);+ return vec4<f32>(clamp(c.rgb + light, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);+}++// Position along the object's gradient axis (both on screen).+fn along(i: u32, px: vec2<f32>) -> f32 {+ let g = gradient_of(i);+ return dot(px - g.xy, g.zw);+}++// ── edges ─────────────────────────────────────────────────────────────────────++// A count writes only the stencil: the least fragment work a pass with a color target allows.+@fragment+fn fs_none() -> @location(0) vec4<f32> {+ return vec4<f32>(0.0);+}++struct Stroked {+ @builtin(position) @invariant position: vec4<f32>, // counted and covered alike, to the bit+ @location(0) @interpolate(flat) color: vec4<f32>,+ @location(1) u: f32,+ @location(2) t: f32,+ @location(3) @interpolate(flat) window: vec2<f32>,+ @location(4) @interpolate(flat) i: u32,+};++// Offset of a joint between a segment with normal n and its neighbour with normal m (miter, limit 10).+fn joint(n: vec2<f32>, m: vec2<f32>, half: f32) -> vec2<f32> {+ let s = n + m;+ let l = length(s);+ if (l < 1e-6) {+ return n * half;+ }+ let dir = s / l;+ return dir * (half / max(dot(dir, n), 0.1));+}++fn normal(a: vec2<f32>, b: vec2<f32>) -> vec2<f32> {+ let d = b - a;+ let l = length(d);+ return select(vec2<f32>(0.0, 0.0), vec2<f32>(-d.y, d.x) / l, l > 1e-6);+}++// code: segment << 3 | corner (two triangles: a−, a+, b+ / a−, b+, b−).+fn ribbon(code: u32, i: u32, width: f32, color: vec4<f32>) -> Stroked {+ let a = code >> 3u;+ let corner = code & 7u;+ let b = a + 1u;+ let sa = screen(clip(i, a));+ let sb = screen(clip(i, b));+ let n = normal(sa.xy, sb.xy);+ let half = 0.5 * width;+ let prev = link(a).x;+ let next = link(b).y;+ var oa = n * half;+ var ob = n * half;+ if (prev != a && dot(n, n) > 0.5) {+ oa = joint(n, normal(screen(clip(i, prev)).xy, sa.xy), half);+ }+ if (next != b && dot(n, n) > 0.5) {+ ob = joint(n, normal(sb.xy, screen(clip(i, next)).xy), half);+ }+ let at_b = corner == 2u || corner == 4u || corner == 5u;+ let plus = corner == 1u || corner == 2u || corner == 4u;+ let offset = select(oa, ob, at_b);+ let s = select(sa, sb, at_b);+ let px = s.xy + select(-offset, offset, plus);+ var out: Stroked;+ // in 3D a face's edge lies on its face: each corner at the face's depth where it is (the plane tangent to the face+ // at the corner's end), pulled nearer by what rounding and the rasterizer's snapping can part them by (`PULL`), so+ // the face hides none of it, in any pass (its own see-through face sorts behind it); never nearer than depth 1+ var z = s.z;+ if (s.z >= 0.0) {+ let plane = select(face_plane(i, a, b), face_plane(i, b, a), at_b);+ z = plane.z + dot(plane.xy, px - s.xy);+ z = min(z + PULL.x * z + PULL.y * (abs(plane.x) + abs(plane.y)), max(s.z, 1.0));+ }+ out.position = to_clip(vec3<f32>(px, z));+ out.color = color;+ out.u = select(vertex(a).w, vertex(b).w, at_b);+ out.t = 0.0;+ if (color.a < 0.0) {+ out.t = along(i, px);+ }+ out.window = params_of(i).xy;+ out.i = i;+ return out;+}++@vertex+fn vs_stroke(@builtin(vertex_index) code: u32, @builtin(instance_index) i: u32) -> Stroked {+ return ribbon(code, i, params_of(i).z, stroke_of(i));+}++++fn stroked(in: Stroked) -> vec4<f32> {+ if (in.u < in.window.x || in.u > in.window.y) {+ discard;+ }+ return premultiplied(in.color);+}++fn stroked_gradient(in: Stroked) -> vec4<f32> {+ if (in.u < in.window.x || in.u > in.window.y) {+ discard;+ }+ return premultiplied(gradient(in.i, brush_of(in.i).z, brush2_of(in.i).y, brush_of(in.i).w, in.t, extra_of(in.i).y));+}++@fragment+fn fs_stroke(in: Stroked) -> @location(0) vec4<f32> {+ return stroked(in);+}++@fragment+fn fs_stroke_gradient(in: Stroked) -> @location(0) vec4<f32> {+ return stroked_gradient(in);+}++@fragment+fn fs_stroke_with_light(in: Stroked) -> Base {+ return painted(stroked(in));+}++@fragment+fn fs_stroke_gradient_with_light(in: Stroked) -> Base {+ return painted(stroked_gradient(in));+}++// ── points ────────────────────────────────────────────────────────────────────++struct Dotted {+ @builtin(position) @invariant position: vec4<f32>, // counted and covered alike, to the bit+ @location(0) local: vec2<f32>,+ @location(1) @interpolate(flat) color: vec4<f32>,+ @location(2) @interpolate(flat) radius: f32,+ @location(3) @interpolate(flat) i: u32,+};++// A point's or vertex's color: its own row when the brush has one per vertex.+fn vertex_color(i: u32, index: u32) -> vec4<f32> {+ let brush = brush_of(i);+ if (brush.y <= 1u) {+ return fill_of(i);+ }+ return row(brush.x, brush2_of(i).x, min(index - ids_of(i).x, brush.y - 1u), bitcast<f32>(brush2_of(i).z));+}++// Corner (0–5: two triangles over its square) of the disk of point `index` of object `i`. A+// point with no opacity is put outside the view: it makes no fragments.+fn disk(i: u32, index: u32, corner: u32) -> Dotted {+ let c = clip(i, index);+ let s = screen(c);+ let radius = extra_of(i).x / max(c.w, 1e-6);+ let local = vec2<f32>(+ select(-1.0, 1.0, corner == 1u || corner == 2u || corner == 4u),+ select(-1.0, 1.0, corner == 2u || corner == 4u || corner == 5u),+ );+ var out: Dotted;+ out.color = vertex_color(i, index);+ out.position = to_clip(vec3<f32>(s.xy + local * (radius + 1.0), select(s.z, -1.0, out.color.a <= 0.0)));+ out.local = local * (radius + 1.0) / max(radius, 1e-6);+ out.radius = radius;+ out.i = i;+ return out;+}++// code: point << 3 | corner.+@vertex+fn vs_points(@builtin(vertex_index) code: u32, @builtin(instance_index) i: u32) -> Dotted {+ return disk(i, code >> 3u, code & 7u);+}++// A point's disk, its edge antialiased.+fn dot_color(in: Dotted) -> vec4<f32> {+ let coverage = clamp((1.0 - length(in.local)) * in.radius + 0.5, 0.0, 1.0);+ return vec4<f32>(in.color.rgb, in.color.a * coverage);+}++fn dotted(in: Dotted) -> vec4<f32> {+ let c = dot_color(in);+ if (c.a <= 0.0) {+ discard;+ }+ return premultiplied(c);+}++@fragment+fn fs_points(in: Dotted) -> @location(0) vec4<f32> {+ return dotted(in);+}++@fragment+fn fs_points_with_light(in: Dotted) -> Base {+ return painted(dotted(in));+}++// ── meshes ────────────────────────────────────────────────────────────────────++struct Shaded {+ @builtin(position) @invariant position: vec4<f32>, // counted and covered alike, to the bit+ @location(0) color: vec4<f32>,+ @location(1) uv: vec2<f32>,+ @location(2) @interpolate(flat) textured: u32,+ @location(3) light: f32, // a lit texture's light (a colored mesh's is in its color)+ @location(4) @interpolate(flat) i: u32,+ @location(5) world: vec3<f32>, // a mesh with a material: where it is+ @location(6) normal: vec3<f32>, // and which way it faces (scene coordinates)+};++fn affine(r0: vec4<f32>, r1: vec4<f32>, r2: vec4<f32>, p: vec3<f32>) -> vec3<f32> {+ let h = vec4<f32>(p, 1.0);+ return vec3<f32>(dot(r0, h), dot(r1, h), dot(r2, h));+}++// Rows of the cofactor of a matrix's linear part: how it carries normals.+fn carry(r0: vec4<f32>, r1: vec4<f32>, r2: vec4<f32>, n: vec3<f32>) -> vec3<f32> {+ return vec3<f32>(dot(cross(r1.xyz, r2.xyz), n), dot(cross(r2.xyz, r0.xyz), n), dot(cross(r0.xyz, r1.xyz), n));+}++// CE's light: half the cube of the cosine toward the light, halved again when facing away.+fn shade(normal: vec3<f32>, p: vec3<f32>) -> f32 {+ let n = normalize(select(normal, -normal, dot(view.toward.xyz, normal) < 0.0));+ let l = normalize(view.light.xyz - p);+ let amount = 0.5 * pow(dot(n, l), 3.0);+ return select(amount, amount * 0.5, amount < 0.0);+}++// code: the vertex itself.+@vertex+fn vs_mesh(@builtin(vertex_index) index: u32, @builtin(instance_index) i: u32) -> Shaded {+ var color = vertex_color(i, index);+ let extra = vertex_extra(index);+ let flags = ids_of(i).z;+ var light = 0.0;+ var p = vec3<f32>(0.0);+ var n = vec3<f32>(0.0);+ if ((flags & (LIT | MATERIAL)) != 0u) {+ p = affine(m1_0_of(i), m1_1_of(i), m1_2_of(i), vertex(index).xyz);+ n = carry(m1_0_of(i), m1_1_of(i), m1_2_of(i), extra.xyz);+ let base2 = ids_of(i).y;+ if (base2 != NONE) {+ let k = base2 + (index - ids_of(i).x);+ p += affine(m2_0_of(i), m2_1_of(i), m2_2_of(i), vertex(k).xyz);+ n += carry(m2_0_of(i), m2_1_of(i), m2_2_of(i), vertex_extra(k).xyz);+ }+ // CE's light, once a vertex (a material's lights are the fragment's)+ if ((flags & LIT) != 0u && dot(n, n) > 1e-20) {+ light = shade(n, p);+ color = vec4<f32>(clamp(color.rgb + light, vec3<f32>(0.0), vec3<f32>(1.0)), color.a);+ }+ }+ var out: Shaded;+ out.position = clip(i, index);+ out.color = color;+ out.uv = vec2<f32>(extra.w, vertex(index).w);+ out.textured = select(0u, flags & (TEXTURED | NEAREST | CUBIC), (flags & TEXTURED) != 0u);+ out.light = light;+ out.i = i;+ out.world = p;+ out.normal = n;+ return out;+}++// A mesh's depth from a light (`shadow_light`: its slot in the view), into the light's shadow map.+override shadow_light: u32 = 0u;++@vertex+fn vs_shadow(@builtin(vertex_index) index: u32, @builtin(instance_index) i: u32) -> @builtin(position) vec4<f32> {+ var p = affine(m1_0_of(i), m1_1_of(i), m1_2_of(i), vertex(index).xyz);+ let base2 = ids_of(i).y;+ if (base2 != NONE) {+ p += affine(m2_0_of(i), m2_1_of(i), m2_2_of(i), vertex(base2 + (index - ids_of(i).x)).xyz);+ }+ return view.lights[shadow_light].shadow * vec4<f32>(p, 1.0);+}++// A mesh's depth as its view sees it, into the view's opaque depth (its ambient occlusion's: `occlusion.rs`).+@vertex+fn vs_depth(@builtin(vertex_index) index: u32, @builtin(instance_index) i: u32) -> @builtin(position) @invariant vec4<f32> {+ return clip(i, index);+}++// A picture's pixel (i, j), its edge held beyond its border.+fn pixel(p: vec2<i32>, size: vec2<i32>) -> vec4<f32> {+ return textureLoad(image, clamp(p, vec2<i32>(0), size - vec2<i32>(1)), 0);+}++// Keys' cubic convolution (a = -1/2): the weights of the pixels at -1, 0, 1, 2 from t.+fn keys(t: f32) -> vec4<f32> {+ let t2 = t * t;+ let t3 = t2 * t;+ return vec4<f32>(+ -0.5 * t3 + t2 - 0.5 * t,+ 1.5 * t3 - 2.5 * t2 + 1.0,+ -1.5 * t3 + 2.0 * t2 + 0.5 * t,+ 0.5 * t3 - 0.5 * t2,+ );+}++// The picture at uv reconstructed from its pixels (centred at (i + 1/2) / size): the nearest+// pixel, or cubic, clamped to what a premultiplied color can be (the kernel overshoots).+fn reconstructed(uv: vec2<f32>, kernel: u32) -> vec4<f32> {+ let size = vec2<i32>(textureDimensions(image));+ let x = uv * vec2<f32>(size) - vec2<f32>(0.5);+ if ((kernel & NEAREST) != 0u) {+ return pixel(vec2<i32>(floor(x + vec2<f32>(0.5))), size);+ }+ let base = floor(x);+ let wx = keys(x.x - base.x);+ let wy = keys(x.y - base.y);+ let b = vec2<i32>(base);+ var sum = vec4<f32>(0.0);+ for (var j = 0; j < 4; j++) {+ var row = vec4<f32>(0.0);+ for (var i = 0; i < 4; i++) {+ row += wx[i] * pixel(b + vec2<i32>(i - 1, j - 1), size);+ }+ sum += wy[j] * row;+ }+ let a = clamp(sum.a, 0.0, 1.0);+ return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);+}++// A texel lit like a color (premultiplied: its light scaled by its coverage), at the mesh's+// opacity.+fn lit_texel(texel: vec4<f32>, light: f32, opacity: f32) -> vec4<f32> {+ return vec4<f32>(clamp(texel.rgb + light * texel.a, vec3<f32>(0.0), vec3<f32>(texel.a)), texel.a) * opacity;+}++// A mesh's picture at the fragment: textures are premultiplied; an image is reconstructed by its resampling algorithm:+// linear (and a camera's view, a colormap) by the sampler, nearest and cubic here.+fn texel_of(in: Shaded) -> vec4<f32> {+ if ((in.textured & (NEAREST | CUBIC)) != 0u) {+ return reconstructed(in.uv, in.textured);+ }+ return textureSampleLevel(image, image_sampler, in.uv, 0.0); // (one level)+}++// A mesh without a material: its colour; a textured one keeps its pixels (lit, if it is: a surface's colormap) and+// takes only its opacity.+fn unlit(in: Shaded, texel: vec4<f32>) -> vec4<f32> {+ return select(premultiplied(in.color), lit_texel(texel, in.light, in.color.a), in.textured != 0u);+}++fn shaded(in: Shaded) -> vec4<f32> {+ let texel = texel_of(in);+ if ((ids_of(in.i).z & MATERIAL) != 0u) {+ return lit(in, texel);+ }+ return unlit(in, texel);+}++// A mesh with a material: its color (its picture's, where it has one) the base color of a surface lit by the view's+// lights: the light it sends toward the eye, exposed (`light.wgsl`), and its opacity; its roughness widened by how far+// its normal turns across the pixel (`over_pixel`: a highlight's light averaged over the pixel). Where it is opaque, the+// light from all around reaches it as the view's ambient occlusion says (what is seen through is not in the depth that+// is found from). (Its normal's derivatives are taken where it has a material: an instance's, the same for every+// fragment of a triangle, so a pixel quad's fragments, all of one triangle, take them together.)+@diagnostic(off, derivative_uniformity)+fn lit_light(in: Shaded, texel: vec4<f32>) -> vec4<f32> {+ var base = in.color;+ if (in.textured != 0u) {+ base = vec4<f32>(texel.rgb / max(texel.a, 1e-6), texel.a * in.color.a);+ }+ let v = select(view.toward.xyz, normalize(view.eye.xyz - in.world), view.eye.w > 0.5);+ let m = material_of(in.i);+ var ao = 1.0;+ if (view.occlusion.x > 0.0 && base.a >= 1.0 - 1e-5) {+ ao = textureLoad(occlusion, vec2<i32>(in.position.xy), 0).x;+ }+ let n = normalize(in.normal);+ let across = dpdx(n);+ let down = dpdy(n);+ let roughness = over_pixel(m.y, dot(across, across) + dot(down, down));+ let radiance = reflected(linear(base.rgb), m.x, roughness, m.z, n, in.world, v, ao);+ return vec4<f32>(radiance * view.lighting.y, base.a);+}++// A mesh with a material, shown on its own as the view's tone mapping shows light, at the mesh's opacity.+fn lit(in: Shaded, texel: vec4<f32>) -> vec4<f32> {+ let l = lit_light(in, texel);+ return premultiplied(vec4<f32>(toned(l.rgb, u32(view.lighting.z + 0.5)), l.a));+}++@fragment+fn fs_mesh(in: Shaded) -> @location(0) vec4<f32> {+ return shaded(in);+}++// ── a view with lit meshes: its raster base in two targets ────────────────────────────────────++// A fragment as the raster base of a view with lit meshes keeps it: display paint in target 0 (premultiplied, its+// alpha the lit layers' coverage), exposed light in target 1 (premultiplied, its alpha every layer's coverage), each+// fragment its own in its target and nothing in the other, its coverage the alpha of both. The `with_light` pipelines'+// blend states lay it over both (target 0's alpha adds the blend constant's share of it: 1 a lit mesh's, else 0), so a+// pixel's samples average display paint and light apart, and the composite mixes them on (`vector.wgsl`'s `Mix`) and+// shows them as the paint plus the light tone mapped once (`seen`): a camera's integration of what is lit.+struct Base {+ @location(0) paint: vec4<f32>,+ @location(1) light: vec4<f32>,+};++// Display paint (premultiplied).+fn painted(c: vec4<f32>) -> Base {+ return Base(c, vec4<f32>(0.0, 0.0, 0.0, c.a));+}++@fragment+fn fs_mesh_with_light(in: Shaded) -> Base {+ let texel = texel_of(in);+ if ((ids_of(in.i).z & MATERIAL) != 0u) {+ let l = lit_light(in, texel);+ return Base(vec4<f32>(0.0, 0.0, 0.0, l.a), vec4<f32>(min(l.rgb, vec3<f32>(LIGHT_MOST)) * l.a, l.a));+ }+ return painted(unlit(in, texel));+}diff --git a/rust/engine/src/blend_buffers.wgsl b/rust/engine/src/blend_buffers.wgslnew file mode 100644index 00000000..03f1945e--- /dev/null+++ b/rust/engine/src/blend_buffers.wgsl@@ -0,0 +1,49 @@+// What `blend.wgsl` reads, from storage buffers.++@group(0) @binding(1) var<storage, read> vertices: array<vec4<f32>>;+@group(0) @binding(2) var<storage, read> links: array<vec2<u32>>;+@group(0) @binding(4) var<storage, read> instances: array<Instance>;+@group(0) @binding(5) var<storage, read> rows: array<vec4<f32>>;+@group(0) @binding(6) var<storage, read> extras: array<vec4<f32>>; // mesh vertex: normal xyz, u++fn row_at(i: u32) -> vec4<f32> {+ return rows[i];+}++// The body's loaders: an object's fields one at a time, then each array's element.+fn c1_0_of(i: u32) -> vec4<f32> { return instances[i].c1_0; }+fn c1_1_of(i: u32) -> vec4<f32> { return instances[i].c1_1; }+fn c1_2_of(i: u32) -> vec4<f32> { return instances[i].c1_2; }+fn c1_3_of(i: u32) -> vec4<f32> { return instances[i].c1_3; }+fn c2_0_of(i: u32) -> vec4<f32> { return instances[i].c2_0; }+fn c2_1_of(i: u32) -> vec4<f32> { return instances[i].c2_1; }+fn c2_2_of(i: u32) -> vec4<f32> { return instances[i].c2_2; }+fn c2_3_of(i: u32) -> vec4<f32> { return instances[i].c2_3; }+fn m1_0_of(i: u32) -> vec4<f32> { return instances[i].m1_0; }+fn m1_1_of(i: u32) -> vec4<f32> { return instances[i].m1_1; }+fn m1_2_of(i: u32) -> vec4<f32> { return instances[i].m1_2; }+fn m2_0_of(i: u32) -> vec4<f32> { return instances[i].m2_0; }+fn m2_1_of(i: u32) -> vec4<f32> { return instances[i].m2_1; }+fn m2_2_of(i: u32) -> vec4<f32> { return instances[i].m2_2; }+fn params_of(i: u32) -> vec4<f32> { return instances[i].params; }+fn extra_of(i: u32) -> vec4<f32> { return instances[i].extra; }+fn ids_of(i: u32) -> vec4<u32> { return instances[i].ids; }+fn fill_of(i: u32) -> vec4<f32> { return instances[i].fill; }+fn stroke_of(i: u32) -> vec4<f32> { return instances[i].stroke; }+fn background_of(i: u32) -> vec4<f32> { return instances[i].background; }+fn gradient_of(i: u32) -> vec4<f32> { return instances[i].gradient; }+fn brush_of(i: u32) -> vec4<u32> { return instances[i].brush; }+fn brush2_of(i: u32) -> vec4<u32> { return instances[i].brush2; }+fn material_of(i: u32) -> vec4<f32> { return instances[i].material; }++fn vertex(k: u32) -> vec4<f32> {+ return vertices[k];+}++fn link(k: u32) -> vec2<u32> {+ return links[k];+}++fn vertex_extra(k: u32) -> vec4<f32> {+ return extras[k];+}diff --git a/rust/engine/src/blend_lists.wgsl b/rust/engine/src/blend_lists.wgslnew file mode 100644index 00000000..af0a3635--- /dev/null+++ b/rust/engine/src/blend_lists.wgsl@@ -0,0 +1,149 @@+// `blend.wgsl`'s see-through lists (where there are storage buffers to append them to): a 3D view's+// see-through fragments, appended per pixel in its raster passes, read by the composite.++@group(0) @binding(7) var<storage, read> sprites: array<vec2<u32>>; // a 3D view's see-through points, far to near: (object, vertex)++// A 3D view's see-through points, six vertices each, in the order `sprites` lists them.+@vertex+fn vs_sprites(@builtin(vertex_index) v: u32) -> Dotted {+ let s = sprites[v / 6u];+ return disk(s.x, s.y, v % 6u);+}++// ── 3D compositing: see-through fragments in depth order, per pixel ────────────────────+//+// A 3D view with see-through layers draws its opaque layers first (depth tested and written).+// Each see-through layer then appends its fragments to a list per pixel: a node per fragment,+// its depth and color at the pixel's center and the samples it covers in front of the opaque+// depth (each tested at its own depth on the fragment's plane). The appends run per sample+// (Metal runs a fragment shader with side effects per sample anyway) and the invocation at a+// fragment's lowest covered sample appends it; a backend that gives each sample only its own+// bit gets a node per sample, the same composite. The view's composite (`vector.wgsl`) lays each+// list's nodes in depth order among its paths and its opaque base: the nodes between two of its+// layers composited sample by sample (a node over the samples it covers), their samples' mean one+// layer. So a pixel shows the depth-ordered composite of every surface on its rays, in any draw+// order: surfaces through surfaces, lines inside a translucent sphere, a cube through a+// translucent floor; exact but where see-through surfaces cross one another inside a pixel+// (ordered there by its center), or where one's edge crosses an opaque edge inside a pixel. One+// object's layer counts once per sample: a stroke's overlapping ribbons as one.+//+// See-through points need no lists to be in depth order: disks facing the camera, sorted far to+// near (`sprites`: among equal depths, the view's order), they composite exactly by blending in+// that order, however many a pixel stacks. So only where the other see-through layers can paint+// (their footprints together) or its paths can (their boxes) are they appended too, and there the+// lists hold every see-through fragment; everywhere else they blend over the opaque samples. A+// view whose only see-through layers are points has no lists.++struct Node {+ depth: f32,+ color: u32, // straight RGBA8+ key: u32, // record << 11 | samples covered << 3 | winding sign << 2 | kind (0 mesh or points, 3 a mesh's edges)+ next: u32, // the next node + 1 (0: none)+};++@group(2) @binding(0) var<storage, read_write> heads: array<atomic<u32>>;+@group(2) @binding(1) var<storage, read_write> nodes: array<Node>;+@group(2) @binding(2) var<storage, read_write> appended: atomic<u32>;+@group(2) @binding(3) var opaque_depth: texture_depth_multisampled_2d;+@group(2) @binding(4) var<storage, read> heads_in: array<u32>;+@group(2) @binding(5) var<storage, read> nodes_in: array<Node>;++// Where sample s of n lies from its pixel's center (the standard patterns; others: the center).+fn sample_offset(n: u32, s: u32) -> vec2<f32> {+ if (n == 4u) {+ var four = array<vec2<f32>, 4>(vec2(-2.0, -6.0), vec2(6.0, -2.0), vec2(-6.0, 2.0), vec2(2.0, 6.0));+ return four[s] / 16.0;+ }+ if (n == 8u) {+ var eight = array<vec2<f32>, 8>(vec2(1.0, -3.0), vec2(-1.0, 3.0), vec2(5.0, 1.0), vec2(-3.0, -5.0), vec2(-5.0, 5.0), vec2(-7.0, -1.0), vec2(3.0, 7.0), vec2(7.0, -7.0));+ return eight[s] / 16.0;+ }+ return vec2<f32>(0.0);+}++// A fragment's depth across its pixel (its plane's slopes), taken in uniform control flow.+fn slopes(pos: vec4<f32>) -> vec2<f32> {+ return vec2<f32>(dpdx(pos.z), dpdy(pos.z));+}++// Append a see-through fragment (straight color) over the samples of `mask` it covers in front+// of the opaque depth: once, by the invocation at its lowest covered sample.+fn append(i: u32, pos: vec4<f32>, slope: vec2<f32>, mask: u32, sample: u32, color: vec4<f32>, kind: u32, positive: bool) {+ if (sample != firstTrailingBit(mask) || color.a <= 0.0) {+ return;+ }+ let pixel = vec2<u32>(pos.xy);+ // its depth at the pixel's center (the position may be a sample's)+ let center = pos.z - dot(slope, pos.xy - floor(pos.xy) - vec2<f32>(0.5));+ let n = u32(view.pixels.z);+ var covered = 0u;+ for (var s = 0u; s < n; s += 1u) {+ if ((mask & (1u << s)) != 0u && center + dot(slope, sample_offset(n, s)) > textureLoad(opaque_depth, pixel, s)) {+ covered |= 1u << s;+ }+ }+ if (covered == 0u) {+ return;+ }+ let k = atomicAdd(&appended, 1u);+ if (k >= arrayLength(&nodes)) {+ return; // full: the count tells the player to draw the frame again with more room+ }+ let key = ((ids_of(i).z >> 8u) << 11u) | (covered << 3u) | select(0u, 4u, positive) | kind;+ let next = atomicExchange(&heads[pixel.y * u32(view.pixels.x) + pixel.x], k + 1u);+ nodes[k] = Node(center, pack4x8unorm(color), key, next);+}++fn straight(c: vec4<f32>) -> vec4<f32> {+ return vec4<f32>(c.rgb / max(c.a, 1e-6), c.a);+}++@fragment+fn fs_stroke_append(in: Stroked, @builtin(sample_mask) mask: u32, @builtin(sample_index) sample: u32) {+ let slope = slopes(in.position);+ if (in.u < in.window.x || in.u > in.window.y) {+ return;+ }+ append(in.i, in.position, slope, mask, sample, in.color, 3u, true);+}++@fragment+fn fs_stroke_gradient_append(in: Stroked, @builtin(sample_mask) mask: u32, @builtin(sample_index) sample: u32) {+ let slope = slopes(in.position);+ if (in.u < in.window.x || in.u > in.window.y) {+ return;+ }+ let c = gradient(in.i, brush_of(in.i).z, brush2_of(in.i).y, brush_of(in.i).w, in.t, extra_of(in.i).y);+ append(in.i, in.position, slope, mask, sample, c, 3u, true);+}++@fragment+fn fs_mesh_append(in: Shaded, @builtin(sample_mask) mask: u32, @builtin(sample_index) sample: u32) {+ append(in.i, in.position, slopes(in.position), mask, sample, straight(shaded(in)), 0u, true);+}++@fragment+fn fs_points_append(in: Dotted, @builtin(sample_mask) mask: u32, @builtin(sample_index) sample: u32) {+ append(in.i, in.position, slopes(in.position), mask, sample, dot_color(in), 0u, true);+}++// A sprite where no see-through fragment is listed: blended over whatever is farther (a read of+// the heads, not an atomic: a shader with side effects would run per sample).+fn sprite_unlisted(in: Dotted) -> vec4<f32> {+ let c = dot_color(in);+ let pixel = vec2<u32>(in.position.xy);+ if (c.a <= 0.0 || heads_in[pixel.y * u32(view.pixels.x) + pixel.x] != 0u) {+ discard;+ }+ return premultiplied(c);+}++@fragment+fn fs_sprites_unlisted(in: Dotted) -> @location(0) vec4<f32> {+ return sprite_unlisted(in);+}++@fragment+fn fs_sprites_unlisted_with_light(in: Dotted) -> Base {+ return painted(sprite_unlisted(in));+}diff --git a/rust/engine/src/render.rs b/rust/engine/src/render.rsindex f02d6233..e83f4396 100644--- a/rust/engine/src/render.rs+++ b/rust/engine/src/render.rs@@ -10,7 +10,8 @@//! inside it, and composited per pixel in depth order (a 2D view's: its draw order). Point clouds//! and meshes are drawn by the raster pipeline (`blend.wgsl`): a 2D view's into layers laid in their//! place in the order, a 3D view's into its z-buffered base, which the composite lays at its depth-//! (its see-through fragments too, through per-pixel lists). Per object the camera is folded into+//! (its see-through meshes' fragments too, through per-pixel lists, and its see-through points,+//! through slabs between its layers). Per object the camera is folded into//! the blend (C = camera · M, once per object on the CPU): a vertex costs one 4×4 per term.//!//! The same drawing runs natively (read back, or encoded to MP4: see `export`) and in the@@ -58,7 +59,7 @@ pub(crate) const VIEW_FLOATS: usize = 44; // projection, overlay (column-major),const VIEW_LIGHTING: usize = VIEW_FLOATS + 4;const LIGHTS: usize = 8;const VIEW_OCCLUSION: usize = VIEW_LIGHTING + 8 + 16 * LIGHTS; // its ambient occlusion: how much (0: none), how far-const VIEW_LENGTH: usize = VIEW_OCCLUSION + 4;+pub(crate) const VIEW_LENGTH: usize = VIEW_OCCLUSION + 4;type Mat4 = [[f32; 4]; 4]; // rowspub(crate) type Mat34 = [[f32; 4]; 3];@@ -168,8 +169,7 @@ struct Shape {curves: Curves,base: u32,count: u32,- fill: Range<u32>, // mesh triangles- stroke: Range<u32>, // point quads; a mesh's faces' edges+ raster: Raster,centroid: [f32; 3], // mean of the control points (a mean commutes with every affine map)area: [f32; 3], // Newell area vector (carried by the cofactor of the linear part)lo: [f32; 3], // bounds of what it draws in its own space (a path's control points: its curves lie within)@@ -191,6 +191,31 @@ struct Curves {miter: f32, // how far a stroke reaches past the path, in half-widths (its sharpest corner's miter)}+/// A point cloud's or mesh's index codes, in the order it is drawn: `count` points, triangles or+/// (a surface's) faces, u's steps as a path's curves are, each laid out with all it draws, one+/// after another, in every layer.+#[derive(Clone, Default)]+struct Raster {+ count: u32,+ fill: Range<u32>, // mesh triangles+ stroke: Range<u32>, // point quads; a mesh's faces' edges+}++impl Raster {+ /// What a window over u shows: every step it reaches into, whole (a point, a triangle, a face+ /// with its edges), of every layer the same.+ fn shown(&self, [lo, hi]: [f32; 2]) -> (Range<u32>, Range<u32>) {+ let n = self.count;+ let a = (lo.max(0.0).floor() as u32).min(n);+ let b = (hi.max(0.0).ceil() as u32).clamp(a, n);+ let cut = |layer: &Range<u32>| {+ let per = layer.len() as u32 / n.max(1);+ layer.start + a * per..layer.start + b * per+ };+ (cut(&self.fill), cut(&self.stroke))+ }+}+struct Brush {offset: u32,count: u32,@@ -350,7 +375,7 @@ impl Store {/// mesh's vertices and index codes.#[cfg(any(feature = "python", feature = "player"))]fn held(s: &Shape) -> usize {- s.count as usize * VERTEX_BYTES + (s.fill.len() + s.stroke.len()) * 4 + s.curves.room as usize * CURVE_BYTES + s.curves.subpaths as usize * 16+ s.count as usize * VERTEX_BYTES + (s.raster.fill.len() + s.raster.stroke.len()) * 4 + s.curves.room as usize * CURVE_BYTES + s.curves.subpaths as usize * 16}/// Forget shapes and brushes; their bytes stay until the arrays are packed.@@ -376,7 +401,7 @@ impl Store {let mut shapes: Vec<(&u64, &Shape)> = old.shapes.iter().collect();shapes.sort_by_key(|(_, s)| (s.curves.first, s.base)); // keep their order: packing is a stable movefor (&key, s) in shapes {- let mut shape = Shape { base: 0, count: 0, fill: 0..0, stroke: 0..0, ..*s };+ let mut shape = Shape { base: 0, count: 0, raster: Raster::default(), ..*s };if s.kind == Kind::Path {let c = s.curves;let (a, b) = (4 * c.first as usize, 4 * (c.first + c.count) as usize);@@ -388,14 +413,16 @@ impl Store {let (a, b) = (s.base as usize, (s.base + s.count) as usize);let base = self.push_vertices(&old.vertices[a..b], Some(&old.extras[a..b]));let moved = |v: u32| v - s.base + base;- let linked = s.kind == Kind::Mesh && !s.stroke.is_empty();+ let r = &s.raster;+ let linked = s.kind == Kind::Mesh && !r.stroke.is_empty();self.links.extend(old.links[a..b].iter().map(|l| if linked { [moved(l[0]), moved(l[1])] } else { [0, 0] }));let fill_start = self.indices.len() as u32;- self.indices.extend(old.indices[s.fill.start as usize..s.fill.end as usize].iter().map(|&i| moved(i)));+ self.indices.extend(old.indices[r.fill.start as usize..r.fill.end as usize].iter().map(|&i| moved(i)));let stroke_start = self.indices.len() as u32;- self.indices.extend(old.indices[s.stroke.start as usize..s.stroke.end as usize].iter().map(|&code| (moved(code >> 3) << 3) | (code & 7)));+ self.indices.extend(old.indices[r.stroke.start as usize..r.stroke.end as usize].iter().map(|&code| (moved(code >> 3) << 3) | (code & 7)));let end = self.indices.len() as u32;- shape = Shape { base, count: s.count, fill: fill_start..stroke_start, stroke: stroke_start..end, ..shape };+ let raster = Raster { count: r.count, fill: fill_start..stroke_start, stroke: stroke_start..end };+ shape = Shape { base, count: s.count, raster, ..shape };}self.shapes.insert(key, shape);}@@ -419,7 +446,7 @@ impl Store {let curves = Curves { first, count, room: count, first_subpath, subpaths: ranges.len() as u32, flatness, miter: miter(points, ranges) };let (lo, hi) = points.iter().fold(([f32::MAX; 3], [f32::MIN; 3]), |(lo, hi), p| ([0, 1, 2].map(|i| lo[i].min(p[i] as f32)), [0, 1, 2].map(|i| hi[i].max(p[i] as f32))));let (plane, planar) = plane(points);- self.shapes.insert(key, Shape { kind: Kind::Path, curves, base: 0, count: 0, fill: 0..0, stroke: 0..0, centroid, area, lo, hi, middle: middle((lo, hi)), plane, planar });+ self.shapes.insert(key, Shape { kind: Kind::Path, curves, base: 0, count: 0, raster: Raster::default(), centroid, area, lo, hi, middle: middle((lo, hi)), plane, planar });}/// Append curves to a path's last subpath (`closed`: whether it now ends where it begins),@@ -465,12 +492,14 @@ impl Store {let base = self.push_vertices(vertices, None);let start = self.indices.len() as u32;self.indices.extend((0..vertices.len() as u32).flat_map(|i| (0..6).map(move |corner| ((base + i) << 3) | corner)));- self.add_raster(key, Kind::Points, vertices, base, start..start, start..self.indices.len() as u32);+ let count = vertices.len() as u32;+ self.add_raster(key, Kind::Points, vertices, base, Raster { count, fill: start..start, stroke: start..self.indices.len() as u32 });}- /// A mesh; with `outline` > 2 its vertices are also blocks of `block` (a surface's faces),- /// each beginning with a closed loop of `outline` (the last repeating the first) — the- /// face's edges, stroked like a path's.+ /// A mesh, drawn triangle by triangle; with `outline` > 2 face by face: its vertices are+ /// blocks of `block` (a surface's faces), each beginning with a closed loop of `outline` (the+ /// last repeating the first) — the face's edges, stroked like a path's — and its triangles+ /// are the faces' own, face after face.#[allow(clippy::too_many_arguments)]fn add_mesh(&mut self, key: u64, vertices: &[[f32; 4]], extras: &[[f32; 4]], triangles: &[u32], outline: u32, block: u32) {let base = self.push_vertices(vertices, Some(extras));@@ -485,17 +514,18 @@ impl Store {self.indices.extend((first..last).flat_map(|v| (0..6).map(move |corner| (v << 3) | corner)));}}- self.add_raster(key, Kind::Mesh, vertices, base, start..end, end..self.indices.len() as u32);+ let count = if outline > 2 { vertices.len() as u32 / block } else { (triangles.len() / 3) as u32 };+ self.add_raster(key, Kind::Mesh, vertices, base, Raster { count, fill: start..end, stroke: end..self.indices.len() as u32 });}- /// A point cloud or mesh, its vertices stored from `base` and its index codes at `fill` and- /// `stroke` (a vertex without neighbours links nowhere).- fn add_raster(&mut self, key: u64, kind: Kind, vertices: &[[f32; 4]], base: u32, fill: Range<u32>, stroke: Range<u32>) {+ /// A point cloud or mesh, its vertices stored from `base` and its index codes `raster` (a+ /// vertex without neighbours links nowhere).+ fn add_raster(&mut self, key: u64, kind: Kind, vertices: &[[f32; 4]], base: u32, raster: Raster) {self.links.resize(self.vertices.len(), [0, 0]);let (lo, hi) = bounds(vertices);let n = vertices.len().max(1) as f32;let centroid = [0, 1, 2].map(|k| vertices.iter().map(|v| v[k]).sum::<f32>() / n);- self.shapes.insert(key, Shape { kind, curves: Curves::default(), base, count: vertices.len() as u32, fill, stroke, centroid, area: [0.0; 3], lo, hi, middle: middle((lo, hi)), plane: [[0.0; 3]; 3], planar: true });+ self.shapes.insert(key, Shape { kind, curves: Curves::default(), base, count: vertices.len() as u32, raster, centroid, area: [0.0; 3], lo, hi, middle: middle((lo, hi)), plane: [[0.0; 3]; 3], planar: true });}fn add_rows(&mut self, key: u64, rows: &[[f32; 4]]) {@@ -554,23 +584,16 @@ fn world_center(shape: &Shape, m: &Mat34) -> [f32; 3] {affine(m, shape.middle)}-/// The part of a points or mesh range a reveal shows: whole points, triangles rounded outward.-fn revealed(range: &Range<u32>, per: u32, [lo, hi]: [f32; 2], ceil: bool) -> Range<u32> {- let n = (range.end - range.start) / per;- let a = ((lo.clamp(0.0, 1.0) * n as f32) as u32).min(n);- let b = if ceil { (hi.clamp(0.0, 1.0) * n as f32).ceil() as u32 } else { (hi.clamp(0.0, 1.0) * n as f32) as u32 }.clamp(a, n);- range.start + a * per..range.start + b * per-}--/// CE's light: half the cube of the cosine toward the light, halved again when facing away.-fn shade(normal: [f32; 3], point: [f32; 3], light: [f32; 3], toward: [f32; 3]) -> f32 {+/// CE's light: half the cube of the cosine toward the light, halved again when facing away, on the side seen from+/// `seen` (a direction from the point toward the viewer).+fn shade(normal: [f32; 3], point: [f32; 3], light: [f32; 3], seen: [f32; 3]) -> f32 {let length = dot(normal, normal).sqrt();let to_light = sub(light, point);let distance = dot(to_light, to_light).sqrt();if length < 1e-12 || distance < 1e-12 {return 0.0;}- let facing = if dot(toward, normal) < 0.0 { -1.0 } else { 1.0 };+ let facing = if dot(seen, normal) < 0.0 { -1.0 } else { 1.0 };let cosine = facing * dot(normal, to_light) / (length * distance);let amount = 0.5 * cosine.powi(3);if amount < 0.0 { amount * 0.5 } else { amount }@@ -647,6 +670,7 @@ pub(crate) struct Camera {light: [f32; 3],three_d: bool,toward: [f32; 3],+ eye: [f32; 4], // where it sees from (w = 1: a 3D view's perspective; else it sees along `toward` from everywhere)bias: f32, // the share of a depth within which a path lies on the raster base (a few of the depth's f32 steps)far: [f32; 4], // its depth from the far plane as a row of its projection: -z (a view's: reversed Z), z - w (`far_row`)}@@ -680,6 +704,7 @@ impl Camera {light: [view[36], view[37], view[38]],three_d: view[39] > 0.5,toward: [view[40], view[41], view[42]],+ eye: [0, 1, 2, 3].map(|k| view[VIEW_LIGHTING + k]),bias: view[43],}}@@ -696,10 +721,16 @@ impl Camera {light: s.toward,three_d: true,toward: s.toward,+ eye: [0.0; 4], // (what its paths cast is their depth alone: no light)bias: 0.0,}}+ /// From a point in the scene toward the viewer: toward its eye in a 3D view's perspective, else its direction.+ fn toward_viewer(&self, p: [f32; 3]) -> [f32; 3] {+ if self.eye[3] > 0.5 { sub([self.eye[0], self.eye[1], self.eye[2]], p) } else { self.toward }+ }+fn pixels(&self, c: [f32; 4]) -> [f32; 2] {[(c[0] / c[3] * 0.5 + 0.5) * self.size[0], (c[1] / c[3] * 0.5 + 0.5) * self.size[1]]}@@ -815,8 +846,8 @@ impl Store {Some([lo[0] - margin, camera.size[1] - hi[1] - margin, hi[0] + margin, camera.size[1] - lo[1] + margin])}- /// CE's light on a record in a 3D view: a lit path's, once for its whole area at its centroid; none for anything- /// else (a mesh is lit per vertex).+ /// CE's light on a record in a 3D view: a lit path's, once for its whole area at its centroid, on the side the eye+ /// sees there (fixed in the frame: the side toward the view); none for anything else (a mesh is lit per vertex).pub(crate) fn light(&self, camera: &Camera, r: &Record) -> Result<f32, String> {if !camera.three_d || r.flags & LIT == 0 {return Ok(0.0);@@ -832,7 +863,8 @@ impl Store {centroid = [0, 1, 2].map(|i| centroid[i] + c2[i]);area = [0, 1, 2].map(|i| area[i] + n2[i]);}- Ok(shade(area, centroid, camera.light, camera.toward))+ let seen = if r.flags & OVERLAY != 0 { camera.toward } else { camera.toward_viewer(centroid) };+ Ok(shade(area, centroid, camera.light, seen))}/// A record's paint, seen through `eye` (see `Paint`); its solid colors unlit.@@ -917,13 +949,13 @@ struct Pipelines {}/// A 3D view's listed see-through layers, each appending its fragments to the lists once (see-/// `blend.wgsl`; its composite resolves them), and its sprites: appended where the listed layers or-/// its paths can paint, blended where nothing is listed.+/// `blend.wgsl`; its composite resolves them), and its sprites among its see-through layers (see+/// `blend_lists.wgsl`): blended into the base where none lies behind them, else into their slabs.struct Appends {stroke: [wgpu::RenderPipeline; 2],mesh: wgpu::RenderPipeline,sprites: wgpu::RenderPipeline,- unlisted: wgpu::RenderPipeline,+ slabs: wgpu::RenderPipeline,}pub(crate) struct Gpu {@@ -1084,11 +1116,12 @@ impl Gpu {let cube = entry(4, wgpu::BindingType::Texture { sample_type: wgpu::TextureSampleType::Float { filterable: true }, view_dimension: wgpu::TextureViewDimension::Cube, multisampled: false });let occlusion = entry(5, wgpu::BindingType::Texture { sample_type: wgpu::TextureSampleType::Float { filterable: false }, view_dimension: wgpu::TextureViewDimension::D2, multisampled: false });let shadows = layout("lighting", &[maps, compare, dfg, linear, cube, occlusion]);- // the scene's arrays; a 3D view's see-through lists, written (with the opaque depth) and read+ // the scene's arrays; a 3D view's see-through lists, written (with the opaque depth), and its slab boundslet written = |binding| wgpu::BindGroupLayoutEntry { binding, visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Storage { read_only: false }, has_dynamic_offset: false, min_binding_size: None }, count: None };let depth = entry(3, wgpu::BindingType::Texture { sample_type: wgpu::TextureSampleType::Depth, view_dimension: wgpu::TextureViewDimension::D2, multisampled: true });- let lists = [layout("append", &[written(0), written(1), written(2), depth]), layout("resolve", &[storage(4), storage(5)])];- let raster = RasterLayouts { scene: layout("scene", &[view, storage(1), storage(2), storage(4), storage(5), storage(6), storage(7)]), lists, shadows };+ let bounds = entry(0, wgpu::BindingType::Texture { sample_type: wgpu::TextureSampleType::Float { filterable: false }, view_dimension: wgpu::TextureViewDimension::D2, multisampled: false });+ let (lists, slabs) = (layout("append", &[written(0), written(1), written(2), depth]), [layout("slab bounds", &[bounds]), layout("slab bounds and depth", &[bounds, depth])]);+ let raster = RasterLayouts { scene: layout("scene", &[view, storage(1), storage(2), storage(4), storage(5), storage(6), storage(7)]), lists, slabs, shadows };let sampler = device.create_sampler(&wgpu::SamplerDescriptor {label: Some("image"),mag_filter: wgpu::FilterMode::Linear,@@ -1102,7 +1135,7 @@ impl Gpu {compare: Some(wgpu::CompareFunction::LessEqual),..Default::default()});- // the split sum's DFG terms (Filament's, with its multiple-scattering term: scripts/dfg_table.py), NoV across,+ // the split sum's DFG terms (Filament's, with its multiple-scattering term: scripts/engine/dfg_table.py), NoV across,// perceptual roughness down: 64 KB, made oncelet size = wgpu::Extent3d { width: 128, height: 128, depth_or_array_layers: 1 };let table = device.create_texture(&wgpu::TextureDescriptor { label: Some("dfg"), size, mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rg16Float, usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, view_formats: &[] });@@ -1182,21 +1215,21 @@ impl Gpu {})};let stroke = |vs, fs| Passes {- count: p(vs, Some(fs), Stencil::Coverage, false, false), // the reveal window discards+ count: p(vs, Some("fs_none"), Stencil::Coverage, false, false),cover: p(vs, Some(fs), Stencil::Cover, true, true),};// the see-through layers' appends test the opaque depth themselves (read-only here) and- // write no color; what reads the lists blends: the resolve over the opaque samples, a- // sprite where nothing is listed over whatever is farther (depth tested)- let lists_layout = |group| device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[Some(&raster.scene), Some(&self.image_layout), Some(group), Some(&raster.shadows)], immediate_size: 0 });+ // write no color; the sprites blended into the base where no see-through layer lies behind them+ // are depth tested+ let group_layout = |group| device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[Some(&raster.scene), Some(&self.image_layout), Some(group), Some(&raster.shadows)], immediate_size: 0 });let blended = targets_for(wgpu::ColorWrites::ALL);- let compositing = |layouts: &[wgpu::PipelineLayout; 2], vs: &str, fs: &str, reads: bool, depth_compare: C| {- let fs = if reads { entry(fs) } else { fs.to_string() };+ let compositing = |layout: &wgpu::PipelineLayout, vs: &str, fs: &str, blends: bool, depth_compare: C| {+ let fs = if blends { entry(fs) } else { fs.to_string() };device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {label: Some(&fs),- layout: Some(&layouts[reads as usize]),+ layout: Some(layout),vertex: wgpu::VertexState { module: &shader, entry_point: Some(vs), compilation_options: Default::default(), buffers: &[] },- fragment: Some(wgpu::FragmentState { module: &shader, entry_point: Some(&fs), compilation_options: Default::default(), targets: if reads { &blended } else { &[] } }),+ fragment: Some(wgpu::FragmentState { module: &shader, entry_point: Some(&fs), compilation_options: Default::default(), targets: if blends { &blended } else { &[] } }),primitive: wgpu::PrimitiveState::default(),depth_stencil: Some(wgpu::DepthStencilState { format: DEPTH, depth_write_enabled: Some(false), depth_compare: Some(depth_compare), stencil: Default::default(), bias: Default::default() }),multisample: wgpu::MultisampleState { count: samples, ..Default::default() },@@ -1205,14 +1238,27 @@ impl Gpu {})};let appends = (samples > 1).then(|| {- let [append, resolve] = &raster.lists;- let layouts = [lists_layout(append), lists_layout(resolve)];- let append = |vs: &str, fs: &str| compositing(&layouts, vs, fs, false, C::Always);+ let (append, based) = (group_layout(&raster.lists), group_layout(&raster.slabs[0]));+ let appended = |vs: &str, fs: &str| compositing(&append, vs, fs, false, C::Always);+ // the slabs, a target each, over the share of each pixel's samples in front of the opaque depth (their+ // pass tests it, on a single sample)+ let slab = Some(wgpu::ColorTargetState { format: COLOR, blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), write_mask: wgpu::ColorWrites::ALL });+ let slabs = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {+ label: Some("fs_sprites_slabs"),+ layout: Some(&device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[Some(&raster.scene), None, Some(&raster.slabs[1])], immediate_size: 0 })),+ vertex: wgpu::VertexState { module: &shader, entry_point: Some("vs_sprites"), compilation_options: Default::default(), buffers: &[] },+ fragment: Some(wgpu::FragmentState { module: &shader, entry_point: Some("fs_sprites_slabs"), compilation_options: Default::default(), targets: &vec![slab; vector::SLABS] }),+ primitive: wgpu::PrimitiveState::default(),+ depth_stencil: None,+ multisample: wgpu::MultisampleState::default(),+ multiview_mask: None,+ cache: None,+ });Appends {- stroke: [append("vs_stroke", "fs_stroke_append"), append("vs_stroke", "fs_stroke_gradient_append")],- mesh: append("vs_mesh", "fs_mesh_append"),- sprites: append("vs_sprites", "fs_points_append"),- unlisted: compositing(&layouts, "vs_sprites", "fs_sprites_unlisted", true, C::GreaterEqual),+ stroke: [appended("vs_stroke", "fs_stroke_append"), appended("vs_stroke", "fs_stroke_gradient_append")],+ mesh: appended("vs_mesh", "fs_mesh_append"),+ sprites: compositing(&based, "vs_sprites", "fs_sprites_base", true, C::GreaterEqual),+ slabs,}});let pipelines = Pipelines {@@ -1309,6 +1355,17 @@ pub(crate) struct Attachments {pub(crate) depth: wgpu::TextureView,pub(crate) depth_only: wgpu::TextureView, // its depth, as a see-through layer's appends read itpub(crate) append: Option<(u64, wgpu::BindGroup)>, // the appends' bind group, for the lists' generation+ slabs: Option<Slabs>, // a 3D view's sprites' slabs, once they have see-through layers behind them+}++/// A 3D view's sprites among its other see-through layers (see `blend_lists.wgsl`): the depths of each pixel's nearest+/// layers (`vector::Out::Bounds`), and the slabs between them, a target each and all together (as the composite reads+/// them); the bounds as its sprites' passes read them (into the base; into the slabs, with the opaque depth).+struct Slabs {+ bounds: wgpu::TextureView,+ layers: [wgpu::TextureView; vector::SLABS],+ slabs: wgpu::TextureView,+ groups: [wgpu::BindGroup; 2],}/// How a raster pass uses depth and stencil: fresh (cleared, then dropped), kept for the passes@@ -1322,12 +1379,13 @@ enum Depth {Shared, // the kept depth, kept again with the samples for the composite to read}-/// The raster pipeline's bind group layouts: the scene's arrays, and a 3D view's see-through lists: written (with the-/// opaque depth), and read.+/// The raster pipeline's bind group layouts: the scene's arrays, a 3D view's see-through lists (written, with the+/// opaque depth) and its slab bounds (as its sprites read them: into the base; into the slabs, with the opaque depth).struct RasterLayouts {scene: wgpu::BindGroupLayout,- lists: [wgpu::BindGroupLayout; 2], // append, resolve- shadows: wgpu::BindGroupLayout, // what the lit passes read: the shadow maps and their comparison, the DFG table+ lists: wgpu::BindGroupLayout,+ slabs: [wgpu::BindGroupLayout; 2],+ shadows: wgpu::BindGroupLayout, // what the lit passes read: the shadow maps and their comparison, the DFG table}/// A pass's color cleared to a premultiplied color.@@ -1360,6 +1418,7 @@ impl Canvas {depth_only: depth.create_view(&wgpu::TextureViewDescriptor { aspect: wgpu::TextureAspect::DepthOnly, ..Default::default() }),depth: depth.create_view(&Default::default()),append: None,+ slabs: None,}});if light && samples > 1 && attached.msaa_light.is_none() {@@ -1367,6 +1426,25 @@ impl Canvas {}}+ /// Give it a 3D view's slabs (once; it has the raster pipeline's attachments).+ fn attach_slabs(&mut self, gpu: &Gpu) {+ let attached = self.raster.as_mut().expect("the raster pipeline's attachments");+ if attached.slabs.is_some() {+ return;+ }+ let size = wgpu::Extent3d { width: self.width, height: self.height, depth_or_array_layers: 1 };+ let texture = |label, size, format, usage| gpu.device.create_texture(&wgpu::TextureDescriptor { label: Some(label), size, mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, format, usage, view_formats: &[] });+ let bounds = texture("slab bounds", size, wgpu::TextureFormat::Rgba32Float, wgpu::TextureUsages::STORAGE_BINDING | wgpu::TextureUsages::TEXTURE_BINDING).create_view(&Default::default());+ let slabs = texture("slabs", wgpu::Extent3d { depth_or_array_layers: vector::SLABS as u32, ..size }, COLOR, wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING);+ let layers = std::array::from_fn(|k| slabs.create_view(&wgpu::TextureViewDescriptor { dimension: Some(wgpu::TextureViewDimension::D2), base_array_layer: k as u32, array_layer_count: Some(1), ..Default::default() }));+ let read = wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&bounds) };+ let depth = wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::TextureView(&attached.depth_only) };+ let group = |layout, entries: &[wgpu::BindGroupEntry]| gpu.device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("slab bounds"), layout, entries });+ let groups = [group(&gpu.raster.slabs[0], std::slice::from_ref(&read)), group(&gpu.raster.slabs[1], &[read, depth])];+ let slabs = slabs.create_view(&wgpu::TextureViewDescriptor { dimension: Some(wgpu::TextureViewDimension::D2Array), ..Default::default() });+ attached.slabs = Some(Slabs { bounds, layers, slabs, groups });+ }+/// A raster pass over it: its color loaded as `color` says, and resolved into the image, or `into`/// another (a 3D view's base, for its composite) — with multisamples only the resolved image is/// kept, the samples stay on tile — or its samples kept; none for the appends, which only read@@ -1427,7 +1505,6 @@ pub(crate) struct Lists {pub(crate) pixels: u64,pub(crate) capacity: u64,pub(crate) generation: u64, // made anew this many times (the canvases' groups follow)- pub(crate) resolve: wgpu::BindGroup,}const NODE_BYTES: u64 = 16;@@ -1574,12 +1651,12 @@ enum Drawing {Raster {offset: u32,draws: Vec<Draw>,- listed: [u32; 4], // where its listed see-through layers and its paths can paint (x, y, width, height)- sprites: Range<u32>, // its sprites among the frame's, far to near- appended: Range<u32>, // those that can reach there- lists: bool, // whether its listed layers and those sprites are appended to the lists- light: bool, // whether it has lit meshes, their light kept apart in its base (`blend.wgsl`'s `Base`)- glow: Option<f32>, // its bloom, where it has lit content to spread (`bloom`)+ listed: [u32; 4], // where its listed see-through layers can paint (x, y, width, height)+ sprites: Range<u32>, // its sprites among the frame's, far to near+ lists: bool, // whether its listed layers are appended to the lists+ slabs: bool, // whether its sprites are blended between its other see-through layers, in slabs+ light: bool, // whether it has lit meshes, their light kept apart in its base (`blend.wgsl`'s `Base`)+ glow: Option<f32>, // its bloom, where it has lit content to spread (`bloom`)// its composite's plan: its paths, exact, laid in depth order with its raster base and listed fragments// (none: the raster base is the view)plan: Option<vector::Plan>,@@ -1810,9 +1887,9 @@ impl Player {// pixels per scene unit at w = 1, for points sized in scene unitslet scale = (eye[0][0] * eye[0][0] + eye[0][1] * eye[0][1] + eye[0][2] * eye[0][2]).sqrt() * camera.size[0] / 2.0;// lit by its material where it has one (a 3D view's mesh, in the scene: not fixed in the frame), else by CE's- // light+ // light (on the side the eye sees; fixed in the frame, the side toward the view)let material = camera.three_d && a.kind == Kind::Mesh && r.material[3] > 0.0 && r.flags & OVERLAY == 0;- let flags = if material { MATERIAL } else if lit && a.kind == Kind::Mesh { LIT } else { 0 } | if r.texture != 0 { TEXTURED | (r.flags & (NEAREST | CUBIC)) } else { 0 };+ let flags = if material { MATERIAL } else if lit && a.kind == Kind::Mesh { LIT } else { 0 } | if r.texture != 0 { TEXTURED | (r.flags & (NEAREST | CUBIC)) } else { 0 } | (r.flags & OVERLAY);let instance = Instance {c1,c2,@@ -1825,11 +1902,7 @@ impl Player {material: if material { r.material } else { [0.0; 4] },};// which passes change pixels- let window = [r.params[0], r.params[1]];- let (fill, stroke) = match a.kind {- Kind::Points => (a.fill.clone(), revealed(&a.stroke, 6, window, false)),- _ => (revealed(&a.fill, 3, window, true), a.stroke.clone()), // (a path has no raster form: nothing)- };+ let (fill, stroke) = a.raster.shown([r.params[0], r.params[1]]); // (a path has no raster form: nothing)// a mesh's faces' edges blend and write depth once per pixel, by the stencil: even opaque,// where blending twice is invisible, the first fragment's depth must be the one keptlet stroked = a.kind == Kind::Mesh && !stroke.is_empty() && r.params[2] > 0.0 && (paint.stroke[3] > 0.0 || many_stroke);@@ -1857,15 +1930,11 @@ impl Player {/// A 3D view's sprites, appended to `sprites`: every see-through point of `draws` (their/// instances from `offset`) that can be seen, as (instance, vertex), far to near — the order- /// they blend in; among equal depths, the view's order — and after them again those whose disk- /// can reach `listed` (x, y, width, height, pixels from the top left), to be appended there.- /// Their two ranges.- fn sprites(&self, camera: &Camera, instances: &[Instance], draws: &[Draw], offset: u32, listed: [u32; 4], sprites: &mut Vec<[u32; 2]>) -> (Range<u32>, Range<u32>) {+ /// they blend in; among equal depths, the view's order. Their range.+ fn sprites(&self, instances: &[Instance], draws: &[Draw], offset: u32, sprites: &mut Vec<[u32; 2]>) -> Range<u32> {// the clouds in the view's order: their records' placeslet mut clouds: Vec<(u32, usize)> = (0..draws.len()).filter(|&j| draws[j].sprites()).map(|j| (instances[j].ids[2] >> PLACE, j)).collect();clouds.sort_unstable();- let [x0, y0, x1, y1] = [listed[0], listed[1], listed[0] + listed[2], listed[1] + listed[3]].map(|p| p as f32);- let listing = listed[2] > 0 && listed[3] > 0;let mut far_first = Vec::new();for (_, j) in clouds {let quads = &self.store.indices[draws[j].stroke.start as usize..draws[j].stroke.end as usize];@@ -1876,20 +1945,13 @@ impl Player {if c[3] <= 1e-6 || !(0.0..=1.0).contains(&depth) {continue; // behind the camera, or nearer than the near plane or farther than the far one}- // its disk's square, as `disk` in `blend.wgsl` draws it, and a pixel more- let [x, y] = camera.pixels(c);- let (y, r) = (camera.size[1] - y, instances[j].extra[0] / c[3] + 2.0);- let reaches = listing && x + r > x0 && x - r < x1 && y + r > y0 && y - r < y1;// keyed far first (reversed Z: far is small): a depth's bits order it once −0 is +0- far_first.push((depth.abs().to_bits(), ([offset + j as u32, vertex], reaches)));+ far_first.push((depth.abs().to_bits(), [offset + j as u32, vertex]));}}- let sorted = radix_sort(far_first);let first = sprites.len() as u32;- sprites.extend(sorted.iter().map(|(_, (sprite, _))| *sprite));- let blended = first..sprites.len() as u32;- sprites.extend(sorted.iter().filter(|(_, (_, reaches))| *reaches).map(|(_, (sprite, _))| *sprite));- (blended.clone(), blended.end..sprites.len() as u32)+ sprites.extend(radix_sort(far_first).into_iter().map(|(_, sprite)| sprite));+ first..sprites.len() as u32}/// Encode views in order — camera views first, the frame (key 0) last; each view is@@ -1925,13 +1987,10 @@ impl Player {let mut draws = Vec::with_capacity(f.records.len());let background = [0, 1, 2, 3].map(|k| f.view[VIEW_FLOATS + k]);let mut plan = if paths.is_empty() { None } else { Some(gpu.vector.plan(&self.store, &camera, &f.records, &paths, background)?) };- // where its listed see-through layers and its paths can paint: their footprints and boxes- // together (the whole view where one cannot be bounded)+ // where its listed see-through layers can paint: their footprints together (the whole view where one+ // cannot be bounded)let mut listed: Option<[f32; 4]> = None;let join = |a: Option<[f32; 4]>, b: [f32; 4]| Some(a.map_or(b, |a| [a[0].min(b[0]), a[1].min(b[1]), a[2].max(b[2]), a[3].max(b[3])]));- for o in plan.iter().flat_map(|p| p.boxes()) {- listed = join(listed, o);- }// what casts shadows (its meshes and paths, but those fixed in the frame) and what they fall on (those// with a material): their boxes in the worldlet (mut casters, mut receivers): (Option<Box3>, Option<Box3>) = (None, None);@@ -1977,14 +2036,11 @@ impl Player {// its see-through points are sprites, unless the listed layers need lists it// cannot have (one sample): then everything is drawn as givenlet at = sprites.len() as u32;- let (sprites_here, appended) = if samples > 1 || !draws.iter().any(Draw::listed) {- self.sprites(&camera, &instances[offset as usize..], &draws, offset, listed, &mut sprites)- } else {- (at..at, at..at)- };- // its listed layers, and the sprites that reach where they or its paths paint, are composited per- // pixel, in depth order, through the lists (with multisamples), by its composite- let lists = samples > 1 && (draws.iter().any(Draw::listed) || !appended.is_empty());+ let sprites_here = if samples > 1 || !draws.iter().any(Draw::listed) { self.sprites(&instances[offset as usize..], &draws, offset, &mut sprites) } else { at..at };+ // its listed layers are composited per pixel, in depth order, through the lists (with multisamples),+ // by its composite; and among them and its paths in the scene, its sprites, through slabs+ let lists = samples > 1 && draws.iter().any(Draw::listed);+ let slabs = samples > 1 && !sprites_here.is_empty() && (lists || !casting.is_empty());// its lit meshes' light, kept apart from its paint in its base and shown once by its composite (a// camera's integration of what is lit)let light = draws.iter().any(|d| d.lit && !d.overlay);@@ -1996,7 +2052,7 @@ impl Player {}// its glow, spread from its lit content's light (without any, there is none to spread)let glow = bloom_of(&f.view).filter(|_| light || plan.as_ref().is_some_and(|p| p.lit));- Drawing::Raster { offset, draws, listed, sprites: sprites_here, appended, lists, light, glow, plan, shadows, cast }+ Drawing::Raster { offset, draws, listed, sprites: sprites_here, lists, slabs, light, glow, plan, shadows, cast }} else {let background = [0, 1, 2, 3].map(|k| f.view[VIEW_FLOATS + k]);let plan = gpu.vector.plan(&self.store, &camera, &f.records, &self.order(&camera, &f.records)?, background)?;@@ -2046,9 +2102,12 @@ impl Player {let mut need = [0u32; 2];for (f, d) in frames.iter().zip(&drawings) {match d {- Drawing::Raster { light, glow, .. } => {+ Drawing::Raster { light, glow, slabs, .. } => {let canvas = if f.key == 0 { &mut self.targets.as_mut().expect("targets").frame } else { self.canvases.get_mut(&f.key).expect("canvas") };canvas.attach(gpu, samples, *light);+ if *slabs {+ canvas.attach_slabs(gpu);+ }// and where its ambient occlusion is found, if it has someif occlusion_of(&f.view).is_some() && canvas.occluded.is_none() {let occlusion = gpu.occlusion.get_or_insert_with(|| occlusion::Occlusion::new(&gpu.device, gpu.blend.as_ref().expect("the raster pipeline's shader"), &gpu.raster.scene));@@ -2082,7 +2141,7 @@ impl Player {if attached.append.as_ref().is_none_or(|(generation, _)| *generation != lists.generation) {let group = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {label: Some("append"),- layout: &gpu.raster.lists[0],+ layout: &gpu.raster.lists,entries: &[whole(0, &lists.heads), whole(1, &lists.nodes), whole(2, &lists.appended), wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::TextureView(&attached.depth_only) }],});attached.append = Some((lists.generation, group));@@ -2147,7 +2206,7 @@ impl Player {let canvas = if f.key == 0 { &targets.frame } else { &self.canvases[&f.key] };let slot = [(i * VIEW_STRIDE) as u32];match d {- Drawing::Raster { offset, draws, listed, sprites, appended, light, glow, plan, shadows, cast, .. } => {+ Drawing::Raster { offset, draws, listed, sprites, lists: listing, slabs, light, glow, plan, shadows, cast } => {// its lights' shadow maps first: its meshes' depth seen from each, then its paths' where nearerfor (layer, s) in shadows.iter().enumerate() {let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {@@ -2228,13 +2287,14 @@ impl Player {// listed fragments; then, where it glows, its glow spread from the light it leaves, and its pixels// shown with itlet bloomed = canvas.bloomed.as_ref().zip(*glow);- let layered = |encoder: &mut wgpu::CommandEncoder, vector: &mut vector::Vector, listed: Option<&vector::Listed>| {+ let layered = |encoder: &mut wgpu::CommandEncoder, vector: &mut vector::Vector, listed: Option<&vector::Listed>, slabs: Option<&Slabs>| {let Some(plan) = plan else { return };let statics = buffers.vector_statics();let (target, light_out) = bloomed.map_or((&canvas.color, None), |(b, _)| (&b.paint, Some(&b.light)));for g in 0..plan.groups.len() {let lighting = vector::Lighting { maps: &buffers.shadow_maps.array, compare: &gpu.shadow_compare, dfg: &gpu.dfg, linear: &gpu.linear, environment: &cube, occlusion };- let out = vector::Out::Color { rasters: None, target, base: base.as_ref(), light: base_light.as_ref(), listed, lighting, light_out };+ let slabs = slabs.map(|s| [&s.bounds, &s.slabs]);+ let out = vector::Out::Color { rasters: None, target, base: base.as_ref(), light: base_light.as_ref(), listed, slabs, lighting, light_out };vector.encode(&gpu.device, &gpu.queue, encoder, plan, g, &statics, out);}if let Some((b, strength)) = bloomed {@@ -2242,7 +2302,7 @@ impl Player {gpu.bloom.as_ref().expect("the bloom's passes").encode(&gpu.queue, encoder, b, strength, tone);}};- let (Some(lists), Some(appends), true) = (lists, pipelines.appends.as_ref(), composite[i]) else {+ let (Some(appends), true) = (pipelines.appends.as_ref(), *listing || *slabs) else {if rasterized || plan.is_none() {let mut pass = canvas.pass(&mut encoder, Some((cleared, true)), if plan.is_some() { Depth::Kept } else { Depth::Fresh }, base.as_ref(), base_light.as_ref());pass.set_bind_group(0, buffers.group.as_ref(), &slot);@@ -2260,48 +2320,64 @@ impl Player {let attached = canvas.raster.as_ref().expect("the raster pipeline's attachments");gpu.vector.resolve_depth(&gpu.device, &mut encoder, &attached.depth_only, samples, [canvas.width, canvas.height]);}- layered(&mut encoder, &mut gpu.vector, None);+ layered(&mut encoder, &mut gpu.vector, None, None);continue;};- // the opaque layers, their samples and depth kept; the listed see-through- // layers appended to the lists, and the sprites where those or the paths can paint- // (only there are heads cleared and read); the sprites blended where nothing is- // listed, and what is fixed in the frame drawn over them; then the composite+ // the opaque layers, their samples and depth kept{let mut pass = canvas.pass(&mut encoder, Some((cleared, false)), Depth::Kept, base.as_ref(), base_light.as_ref());pass.set_bind_group(0, buffers.group.as_ref(), &slot);pass.set_bind_group(3, lighting_group, &[]);self.draw(&mut pass, pipelines, indices, draws, *offset, Phase::Opaque);}- let ([_, y, _, h], row) = (*listed, canvas.width as u64 * 4);- if h > 0 {- encoder.clear_buffer(&lists.heads, y as u64 * row, Some(h as u64 * row));- }- {- let append = &canvas.raster.as_ref().and_then(|a| a.append.as_ref()).expect("the appends' group").1;+ let attached = canvas.raster.as_ref().expect("the raster pipeline's attachments");+ let colors = attached.msaa.as_ref().expect("multisamples");+ let [_, y, _, h] = *listed;+ let listed = lists.filter(|_| *listing).map(|l| vector::Listed { heads: &l.heads, nodes: &l.nodes, rows: [y, y + h], samples, colors, depths: &attached.depth_only, lights: attached.msaa_light.as_ref() });+ // the listed see-through layers appended to the lists (only in their rows are heads cleared and read)+ if let Some(l) = &listed {+ let row = canvas.width as u64 * 4;+ if h > 0 {+ encoder.clear_buffer(l.heads, y as u64 * row, Some(h as u64 * row));+ }+ let append = &attached.append.as_ref().expect("the appends' group").1;let mut pass = canvas.pass(&mut encoder, None, Depth::Read, None, None);pass.set_bind_group(0, buffers.group.as_ref(), &slot);pass.set_bind_group(3, lighting_group, &[]);pass.set_bind_group(2, append, &[]);self.draw(&mut pass, pipelines, indices, draws, *offset, Phase::SeeThrough);- let [lx, ly, lw, lh] = *listed;- pass.set_scissor_rect(lx, ly, lw, lh);- draw_sprites(&mut pass, &appends.sprites, appended);}- // sprites blend depth tested, against the opaque depth kept for them (and kept again, with the- // samples, for the composite)+ // the sprites among its other see-through layers: the depths of each pixel's nearest found (its+ // paths' and its listed fragments'), then each sprite blended into the slab between them it lies in+ let slabs = attached.slabs.as_ref().filter(|_| *slabs);+ if let Some(s) = slabs {+ let plan = plan.as_ref().expect("a plan: its paths', or its lists'");+ let statics = buffers.vector_statics();+ gpu.vector.encode(&gpu.device, &gpu.queue, &mut encoder, plan, 0, &statics, vector::Out::Bounds { out: &s.bounds, listed: listed.as_ref() });+ let targets: Vec<_> = s.layers.iter().map(|view| Some(wgpu::RenderPassColorAttachment { view, depth_slice: None, resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), store: wgpu::StoreOp::Store } })).collect();+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("slabs"), color_attachments: &targets, depth_stencil_attachment: None, occlusion_query_set: None, timestamp_writes: None, multiview_mask: None });+ pass.set_bind_group(0, buffers.group.as_ref(), &slot);+ pass.set_bind_group(2, &s.groups[1], &[]);+ draw_sprites(&mut pass, &appends.slabs, sprites);+ }+ // the sprites behind no other see-through layer blended into the base, depth tested against the+ // opaque depth kept for them (and kept again, with the samples, for the composite), and what is+ // fixed in the frame drawn over them; then the compositelet mut pass = canvas.pass(&mut encoder, Some((wgpu::LoadOp::Load, true)), Depth::Shared, base.as_ref(), base_light.as_ref());pass.set_bind_group(0, buffers.group.as_ref(), &slot);pass.set_bind_group(3, lighting_group, &[]);pass.set_bind_group(1, &self.image_groups[&0], &[]);- pass.set_bind_group(2, &lists.resolve, &[]);- draw_sprites(&mut pass, &appends.unlisted, sprites);+ match slabs {+ Some(s) => {+ pass.set_bind_group(2, &s.groups[0], &[]);+ draw_sprites(&mut pass, &appends.sprites, sprites);+ }+ None => draw_sprites(&mut pass, &pipelines.sprites, sprites),+ }self.draw(&mut pass, pipelines, indices, draws, *offset, Phase::Overlay);drop(pass);- let attached = canvas.raster.as_ref().expect("the raster pipeline's attachments");gpu.vector.resolve_depth(&gpu.device, &mut encoder, &attached.depth_only, samples, [canvas.width, canvas.height]);- let colors = attached.msaa.as_ref().expect("multisamples");- layered(&mut encoder, &mut gpu.vector, Some(&vector::Listed { heads: &lists.heads, nodes: &lists.nodes, rows: [y, y + h], samples, colors, depths: &attached.depth_only, lights: attached.msaa_light.as_ref() }));+ layered(&mut encoder, &mut gpu.vector, listed.as_ref(), slabs);}Drawing::Vector(plan, groups) => {grouped |= f.key == 0 && plan.groups.len() > 1;@@ -2320,7 +2396,7 @@ impl Player {}let statics = buffers.vector_statics();let lighting = vector::Lighting { maps: &buffers.shadow_maps.array, compare: &gpu.shadow_compare, dfg: &gpu.dfg, linear: &gpu.linear, environment: &cube, occlusion: &gpu.no_occlusion };- let out = vector::Out::Color { rasters: atlas.map(|a| &a.color), target: &canvas.color, base: None, light: None, listed: None, lighting, light_out: None };+ let out = vector::Out::Color { rasters: atlas.map(|a| &a.color), target: &canvas.color, base: None, light: None, listed: None, slabs: None, lighting, light_out: None };gpu.vector.encode(&gpu.device, &gpu.queue, &mut encoder, plan, g, &statics, out);}}@@ -2342,9 +2418,8 @@ impl Player {let heads = buffer("heads", pixels * 4, wgpu::BufferUsages::COPY_DST);let nodes = buffer("nodes", self.list_capacity * NODE_BYTES, wgpu::BufferUsages::empty());let appended = buffer("appended", 16, wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC);- let resolve = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("resolve"), layout: &gpu.raster.lists[1], entries: &[whole(4, &heads), whole(5, &nodes)] });let generation = self.lists.as_ref().map_or(0, |l| l.generation + 1);- self.lists = Some(Lists { heads, nodes, appended, pixels, capacity: self.list_capacity, generation, resolve });+ self.lists = Some(Lists { heads, nodes, appended, pixels, capacity: self.list_capacity, generation });}/// A frame's lists held `appended` nodes: whether they overflowed (then they are given room@@ -2517,6 +2592,12 @@ impl Player {if outline > 2 && (block < outline || !p.len().is_multiple_of(block as usize)) {return Err("an outlined mesh is whole faces of vertices, each beginning with its loop".into());}+ // a reveal shows a surface face by face (`Raster`): each face's triangles are its own, in its turn+ let faces = (p.len() / block.max(1) as usize).max(1);+ let own = |(k, face): (usize, &[u32])| face.iter().all(|&i| i as usize / block as usize == k);+ if outline > 2 && (!t.len().is_multiple_of(3 * faces) || (!t.is_empty() && !t.chunks(t.len() / faces).enumerate().all(own))) {+ return Err("an outlined mesh's triangles are its faces' own, face after face, as many each".into());+ }// what the shaders read: (x, y, z, v) and (normal, u)let v: Vec<[f32; 4]> = p.iter().zip(&uv).map(|(p, uv)| [p[0] as f32, p[1] as f32, p[2] as f32, uv[1] as f32]).collect();let e: Vec<[f32; 4]> = n.iter().zip(&uv).map(|(n, uv)| [n[0] as f32, n[1] as f32, n[2] as f32, uv[0] as f32]).collect();