Skip to main content

CULL_WGSL

Constant CULL_WGSL 

Source
pub const CULL_WGSL: &str = "// common -- what cull.wgsl and stroke.wgsl share (lib.rs puts it in front of both): the view, records, styles, the\r\n// coordinates relative to the camera, how far a segment\'s stroke reaches, double-f32 arithmetic and where a dash\r\n// pattern stands (docs/RENDER2D.md)\r\n//\r\n// Coordinates (decision 28): a point is f64 on the CPU and a pair of f32 per axis here, hi = f32(x) and lo = f32(x -\r\n// hi).  The camera\'s centre comes the same way, and what f32 cannot hold of it (`rest`, at most 2^-26 of a unit at\r\n// 1e7) is taken off afterwards.  rel() subtracts the camera first: hi - hi and lo - lo each through an exact two-sum,\r\n// so a point near the camera comes out to a few 1e-15 units; a point far from it as a double-f32 pair (rel2), whose\r\n// rounded part has f32\'s 24 bits of its distance.  Pixels are then y down from the target\'s top left corner.\r\n//\r\n// Dash patterns: a record holds the length along its entity at its end (f64 as three f32, exact), the record before\r\n// it the length at its start.  The pattern\'s place at a length L is frac(L k + p0) periods, k the periods per drawing\r\n// unit (the style\'s, from the CPU in f64 as three f32: per frame for patterns in pixels) and p0 the phase; L k can be\r\n// 1e13 and more (1e7 units at 1e-6 units per pixel), beyond f32 and beyond double-f32 products, so `periods` takes\r\n// the exact pieces of the products (two_prod) and adds only their fractions, in double-f32.\r\n\r\nstruct View {\r\n    // the camera\'s centre: hi.x, hi.y, lo.x, lo.y (drawing units)\r\n    center: vec4f,\r\n    // what hi + lo leaves of it (x, y), 1 / scale (pixels per unit), scale (units per pixel)\r\n    rest: vec4f,\r\n    // cos and sin of the rotation, the target\'s width and height (px)\r\n    turn: vec4f,\r\n    // x: 1 when culling is off; y: 0, the opaque zero of opq; the rest unused\r\n    misc: vec4f,\r\n    // the linear map to pixels before rest.z and y down: (lin.x x + lin.y y, lin.z x + lin.w y); the camera\'s rotation\r\n    // (cos, -sin, sin, cos), in an insert\'s space the insert\'s own map with the camera\'s (G6b2: block.wgsl space_view)\r\n    lin: vec4f,\r\n}\r\n\r\n// a record, 64 bytes (scene.rs records()).  A segment of a polyline: its start and end; an arc\'s piece: its circle\'s\r\n// centre and its end on the circle (its start is the end of the record before it: the arc\'s head or the piece before)\r\nstruct Record {\r\n    // a segment\'s start, an arc piece\'s centre: hi.x, hi.y, lo.x, lo.y\r\n    a: vec4f,\r\n    // its end (an arc\'s head: the arc\'s start)\r\n    b: vec4f,\r\n    // x, y: a segment\'s end minus its start as two f32 (the CPU rounded the f64 difference once: exactly the negated\r\n    // vector to the previous point of the next record and the vector to the next point of the one before; the vertex\r\n    // shader reads its neighbours\' for a join\'s bisector); an arc piece: its radius, hi and lo.  z, w: the length along\r\n    // the entity at the end (drawing units), the first two of three f32\r\n    v: vec4u,\r\n    // x: the length\'s third f32; y: a closed polyline\'s first and last record: how far the other one lies, a circle\'s\r\n    // first and last piece likewise; z: style + 1 (0: a free record, an arc\'s head); w: flags (F_*) | kind << 8\r\n    w: vec4u,\r\n}\r\n\r\nconst K_SEG: u32 = 0u;\r\nconst K_ARC: u32 = 1u;\r\n// a fill\'s boundary edge (a ring: a closed polyline of them, a start, an end) and triangle (a.xyz: how far back the\r\n// edge records of its corners lie, as u32)\r\nconst K_EDGE: u32 = 3u;\r\nconst K_TRI: u32 = 4u;\r\n// a segment: its previous point is the polyline\'s first (a cap there); its next point is the last\r\nconst F_CAP_PREV: u32 = 1u;\r\nconst F_CAP_NEXT: u32 = 2u;\r\n// a segment joined at its start, at its end\r\nconst F_PREV: u32 = 4u;\r\nconst F_NEXT: u32 = 8u;\r\n// the first record of its entity: the length along it starts at 0\r\nconst F_FIRST: u32 = 16u;\r\n// a closed polyline\'s first record (the previous one lies w.y records on) and its last (the next one lies w.y back)\r\nconst F_WRAP_PREV: u32 = 32u;\r\nconst F_WRAP_NEXT: u32 = 64u;\r\n// an arc piece: its start is the arc\'s start (a cap), its end the arc\'s end\r\nconst F_CAP_A: u32 = 1u;\r\nconst F_CAP_B: u32 = 2u;\r\n// the arc turns counter-clockwise (the drawing\'s y up)\r\nconst F_CCW: u32 = 4u;\r\n// a circle\'s first piece (its start is where the dash pattern starts again) and its last\r\nconst F_RESTART_A: u32 = 32u;\r\nconst F_RESTART_B: u32 = 64u;\r\n\r\nfn kind_of_record(r: Record) -> u32 {\r\n    return (r.w.w >> 8u) & 15u;\r\n}\r\n\r\n// the length along the entity at a record\'s end (three f32)\r\nfn length_of(r: Record) -> vec3f {\r\n    return vec3f(bitcast<f32>(r.v.z), bitcast<f32>(r.v.w), bitcast<f32>(r.w.x));\r\n}\r\n\r\n// a style, 128 bytes (scene.rs Style::words)\r\nstruct Style {\r\n    // 0xAARRGGBB, sRGB codes, straight alpha\r\n    color: u32,\r\n    width: f32,\r\n    // unit | cap << 4 | join << 8 | dash entries (0 ..= 16, an odd pattern twice) << 12 | the dash unit << 20\r\n    flags: u32,\r\n    // miter limit: the longest miter over the width\r\n    miter: f32,\r\n    // the dash pattern\'s periods per drawing unit at this frame (three f32), where it starts (periods)\r\n    kappa: vec3f,\r\n    phase0: f32,\r\n    // its period (dash units), the share of a period on (without caps), the dashes per period\r\n    period: f32,\r\n    duty: f32,\r\n    ons: f32,\r\n    // the style\'s place in the hatch table + 1 (fill.wgsl; 0: no hatch)\r\n    hatch: u32,\r\n    // the entries\' ends from the period\'s start (dash units), 16\r\n    ends: array<vec4f, 4>,\r\n    r1: vec4u,\r\n}\r\n\r\nconst UNIT_DRAWING: u32 = 1u;\r\nconst CAP_BUTT: u32 = 0u;\r\nconst CAP_ROUND: u32 = 1u;\r\nconst CAP_SQUARE: u32 = 2u;\r\nconst JOIN_MITER: u32 = 0u;\r\nconst JOIN_ROUND: u32 = 1u;\r\nconst JOIN_BEVEL: u32 = 2u;\r\n// records per page: what a workgroup of culling looks at\r\nconst PAGE: u32 = 256u;\r\n\r\n// x as it is, through a door no compiler sees through (view.misc.y is 0; its bits xor\'ed in): the NVIDIA driver\r\n// simplified (a + b) - a to b in a two-sum despite NoContraction, and an error term became 0 (a circle of radius 1e7\r\n// then lay 0.02 units from its place); where a sum or product passes this, no algebra crosses it\r\nfn opq(x: f32) -> f32 {\r\n    return bitcast<f32>(bitcast<u32>(x) ^ bitcast<u32>(view.misc.y));\r\n}\r\n\r\n// a + b exactly as s + e (Knuth\'s two-sum; round to nearest, nothing reassociated, nothing simplified: opq).  Every\r\n// step goes through opq, the error\'s last sum too (G6e3): Firefox\'s WebGPU compiles HLSL without IEEE strictness (DXC\r\n// without -Gis: fast math), and it regrouped (a - x) + (b - bb), the error lost its bits, far lines moved and vanished\r\nfn two_sum(a: f32, b: f32) -> vec2f {\r\n    let s = opq(a + b);\r\n    let bb = opq(s - a);\r\n    let e = opq(a - opq(s - bb)) + opq(b - bb);\r\n    return vec2f(s, e);\r\n}\r\n\r\n// a + b as s + e when |a| >= |b| (Dekker\'s fast two-sum)\r\nfn quick_sum(a: f32, b: f32) -> vec2f {\r\n    let s = opq(a + b);\r\n    return vec2f(s, b - opq(s - a));\r\n}\r\n\r\n// a * b exactly as p + e: Dekker\'s product of the halves (12 bits each: their products are exact, a fused\r\n// multiply-add of them changes nothing).  Not fma: SPIR-V\'s Fma is one rounding only where it carries NoContraction,\r\n// which lib/shader gives the arithmetic operations alone\r\nfn split_f(a: f32) -> vec2f {\r\n    let t = opq(4097.0 * a);\r\n    let hi = opq(t - opq(t - a));\r\n    return vec2f(hi, a - hi);\r\n}\r\n\r\n// (the error\'s partial sums each through opq, in this order: x.x y.x - p cancels exactly first; a fused multiply-add\r\n// of them gives the same, the products of the halves are exact)\r\nfn two_prod(a: f32, b: f32) -> vec2f {\r\n    let p = opq(a * b);\r\n    let x = split_f(a);\r\n    let y = split_f(b);\r\n    let e1 = opq(x.x * y.x - p);\r\n    let e2 = opq(e1 + x.x * y.y);\r\n    let e3 = opq(e2 + x.y * y.x);\r\n    return vec2f(p, e3 + x.y * y.y);\r\n}\r\n\r\n// double-f32 (hi, lo): sums and products to about 2^-47 (only in the passes compiled exact: lib.rs)\r\nfn dd_add(a: vec2f, b: vec2f) -> vec2f {\r\n    let s = two_sum(a.x, b.x);\r\n    return quick_sum(s.x, s.y + opq(a.y + b.y));\r\n}\r\n\r\nfn dd_add_f(a: vec2f, b: f32) -> vec2f {\r\n    let s = two_sum(a.x, b);\r\n    return quick_sum(s.x, s.y + a.y);\r\n}\r\n\r\nfn dd_mul(a: vec2f, b: vec2f) -> vec2f {\r\n    let p = two_prod(a.x, b.x);\r\n    return quick_sum(p.x, p.y + opq(a.x * b.y + a.y * b.x));\r\n}\r\n\r\nfn dd_mul_f(a: vec2f, b: f32) -> vec2f {\r\n    let p = two_prod(a.x, b);\r\n    return quick_sum(p.x, p.y + a.y * b);\r\n}\r\n\r\nfn dd_sqrt(a: vec2f) -> vec2f {\r\n    if a.x <= 0.0 {\r\n        return vec2f(0.0);\r\n    }\r\n    let x = sqrt(a.x);\r\n    let p = two_prod(x, x);\r\n    return quick_sum(x, (opq(a.x - p.x) - p.y + a.y) / (2.0 * x));\r\n}\r\n\r\nfn dd_div(a: vec2f, b: vec2f) -> vec2f {\r\n    let q1 = a.x / b.x;\r\n    let r = dd_add(a, -dd_mul_f(b, q1));\r\n    return quick_sum(q1, r.x / b.x);\r\n}\r\n\r\n// a point (hi.x, hi.y, lo.x, lo.y) minus the camera\'s centre as a double-f32 pair per axis: (x, y) rounded, (x, y)\r\n// what the rounding left (drawing units)\r\nfn rel2(p: vec4f, view: View) -> vec4f {\r\n    let x1 = two_sum(p.x, -view.center.x);\r\n    let y1 = two_sum(p.y, -view.center.y);\r\n    let x2 = two_sum(p.z, -view.center.z);\r\n    let y2 = two_sum(p.w, -view.center.w);\r\n    let xs = two_sum(x1.x, x2.x);\r\n    let ys = two_sum(y1.x, y2.x);\r\n    let x = two_sum(xs.x, opq(opq(xs.y + x1.y) + x2.y) - view.rest.x);\r\n    let y = two_sum(ys.x, opq(opq(ys.y + y1.y) + y2.y) - view.rest.y);\r\n    return vec4f(x.x, y.x, x.y, y.y);\r\n}\r\n\r\n// drawing units relative to the centre -> pixels relative to the target\'s middle, y down (the drawing turns\r\n// counter-clockwise by the rotation).  (The camera\'s lin is (cos, -sin, sin, cos): c x + (-s) y is c x - s y bit for bit,\r\n// the images of G6b1 stay as they were; an insert\'s space maps its block\'s units, mirrored or scaled unevenly too)\r\nfn to_px(v: vec2f, view: View) -> vec2f {\r\n    let r = vec2f(view.lin.x * v.x + view.lin.y * v.y, view.lin.z * v.x + view.lin.w * v.y) * view.rest.z;\r\n    return vec2f(r.x, -r.y);\r\n}\r\n\r\n// half the stroke\'s width in pixels: drawing units scaled, never below half a pixel (thinner lines are drawn 1 px\r\n// wide, as a hairline)\r\nfn half_width(st: Style, view: View) -> f32 {\r\n    var w = st.width;\r\n    if (st.flags & 15u) == UNIT_DRAWING {\r\n        w = w * view.rest.z;\r\n    }\r\n    return max(0.5 * w, 0.5);\r\n}\r\n\r\n// the join of a style: miter, round or bevel\r\nfn join_of(st: Style) -> u32 {\r\n    return (st.flags >> 8u) & 15u;\r\n}\r\n\r\nfn cap_of(st: Style) -> u32 {\r\n    return (st.flags >> 4u) & 15u;\r\n}\r\n\r\n// the entries of a style\'s dash pattern (an odd pattern counted twice; 0: solid)\r\nfn dash_count(st: Style) -> u32 {\r\n    return (st.flags >> 12u) & 31u;\r\n}\r\n\r\n// drawing units per pixel of a dash pattern\'s unit\r\nfn dash_scale(st: Style, view: View) -> f32 {\r\n    return select(1.0, view.rest.w, ((st.flags >> 20u) & 15u) == UNIT_DRAWING);\r\n}\r\n\r\n// a miter fits the limit: 1 / cos(phi / 2) = sqrt(2 / (1 + c)) <= limit, c = cos(phi) of the turn (lib/gfx dash.rs\'s test)\r\nfn miter_ok(c: f32, limit: f32) -> bool {\r\n    let d1 = c + 1.0;\r\n    return d1 > 1e-9 && 2.0 <= limit * limit * d1;\r\n}\r\n\r\n// the farthest any style\'s stroke reaches from its segment, in half widths (culling\'s bound: the turn\'s own angle\r\n// is not looked at): a miter up to its limit, h tan(phi / 2) = h sqrt(limit^2 - 1), else a half width\r\nfn reach_bound(st: Style) -> f32 {\r\n    if join_of(st) == JOIN_MITER {\r\n        return max(1.0, sqrt(max(st.miter * st.miter - 1.0, 0.0)));\r\n    }\r\n    return 1.0;\r\n}\r\n\r\n// how far an arc\'s piece bulges beyond its chord (px): the chord\'s half c, the radius r\r\nfn sagitta(c: f32, r: f32) -> f32 {\r\n    return c * c / (r + sqrt(max(r * r - c * c, 0.0)));\r\n}\r\n\r\n// an f32\'s fraction, exact\r\nfn fr(x: f32) -> f32 {\r\n    return x - floor(x);\r\n}\r\n\r\nfn acc(s: vec2f, x: f32) -> vec2f {\r\n    return dd_add_f(s, fr(x));\r\n}\r\n\r\n// the dash pattern\'s place at the length l (three f32, exact) plus t (double-f32) along the entity: frac((l + t) k +\r\n// p0) in periods, as double-f32 in [0, 1).  The products\' exact pieces (two_prod) each lose their whole part (exact\r\n// for an f32) and only the fractions are added: this holds for (l + t) k far beyond f32 (see the head)\r\nfn periods(l: vec3f, t: vec2f, k: vec3f, p0: f32) -> vec2f {\r\n    var s = vec2f(p0, 0.0);\r\n    let a = two_prod(l.x, k.x);\r\n    s = acc(s, a.x);\r\n    s = acc(s, a.y);\r\n    let b = two_prod(l.x, k.y);\r\n    s = acc(s, b.x);\r\n    s = acc(s, b.y);\r\n    s = acc(s, l.x * k.z);\r\n    let c = two_prod(l.y, k.x);\r\n    s = acc(s, c.x);\r\n    s = acc(s, c.y);\r\n    s = acc(s, l.y * k.y);\r\n    s = acc(s, l.z * k.x);\r\n    let d = two_prod(t.x, k.x);\r\n    s = acc(s, d.x);\r\n    s = acc(s, d.y);\r\n    let e = two_prod(t.x, k.y);\r\n    s = acc(s, e.x);\r\n    s = acc(s, e.y);\r\n    s = acc(s, t.x * k.z);\r\n    let f = two_prod(t.y, k.x);\r\n    s = acc(s, f.x);\r\n    s = acc(s, f.y);\r\n    s = acc(s, t.y * k.y);\r\n    var r = quick_sum(s.x - floor(s.x), s.y);\r\n    if r.x < 0.0 {\r\n        r = dd_add_f(r, 1.0);\r\n    }\r\n    if r.x >= 1.0 {\r\n        r = dd_add_f(r, -1.0);\r\n    }\r\n    return r;\r\n}\r\n\r\n// beyond this many pixels from the target\'s middle at both ends, a segment\'s line is placed by far_foot\r\nconst FAR: f32 = 131072.0;\r\n\r\n// the foot of the camera\'s centre on the line through two points far from it (r0, r1: rel2\'s pairs): their cross\r\n// product in double-f32 over their distance is the line\'s signed distance from the centre (px from the middle).  In an\r\n// insert\'s space (vw: block.wgsl) the foot is that of its units, mapped: a point of the line near the centre all the same\r\nfn far_foot(r0: vec4f, r1: vec4f, vw: View) -> vec2f {\r\n    let a = two_prod(r0.x, r1.y);\r\n    let b = two_prod(r0.y, r1.x);\r\n    let s = two_sum(a.x, -b.x);\r\n    let rest = (a.y - b.y) + ((r0.x * r1.w + r0.z * r1.y) - (r0.y * r1.z + r0.w * r1.x));\r\n    let cr = s.x + (s.y + rest);\r\n    let dv = vec2f(r1.x - r0.x, r1.y - r0.y);\r\n    let lu = length(dv);\r\n    let du = dv / lu;\r\n    return to_px(-(cr / lu) * vec2f(-du.y, du.x), vw);\r\n}\r\n\r\n// a fill\'s edge from r0 to r1 (rel2\'s pairs) in pixels of the target (from its top left corner), clipped to the target\r\n// and 8 px around it; both ends beyond FAR: its line placed by far_foot (as strokes); clipped away: far outside.  The count\r\n// pass writes it per edge record (cull.wgsl), and every record of the fill reads those bits; vw: the view, or an insert\'s\r\n// space (block.wgsl)\r\nfn edge_px(r0: vec4f, r1: vec4f, vw: View) -> vec4f {\r\n    let half = 0.5 * vw.turn.zw;\r\n    let p0 = to_px(r0.xy, vw);\r\n    let p1 = to_px(r1.xy, vw);\r\n    let lo = -half - vec2f(8.0);\r\n    let hi = half + vec2f(8.0);\r\n    // (wholly beside the target on one side, with what f32 may round far points to: far outside at once)\r\n    let mg = 1.0 + 4e-6 * (length(p0) + length(p1));\r\n    if max(p0.x, p1.x) < lo.x - mg || min(p0.x, p1.x) > hi.x + mg || max(p0.y, p1.y) < lo.y - mg || min(p0.y, p1.y) > hi.y + mg {\r\n        return vec4f(-1e6);\r\n    }\r\n    let dv = p1 - p0;\r\n    let len0 = length(dv);\r\n    var d = vec2f(1.0, 0.0);\r\n    if len0 > 0.0 {\r\n        d = dv / len0;\r\n    }\r\n    var base = p0;\r\n    var s0 = 0.0;\r\n    var s1 = len0;\r\n    if max(abs(p1.x), abs(p1.y)) < max(abs(p0.x), abs(p0.y)) {\r\n        base = p1;\r\n        s0 = -len0;\r\n        s1 = 0.0;\r\n    }\r\n    let far = min(max(abs(p0.x), abs(p0.y)), max(abs(p1.x), abs(p1.y))) > FAR;\r\n    if far {\r\n        if !(length(r1.xy - r0.xy) > 0.0) {\r\n            return vec4f(-1e6);\r\n        }\r\n        base = far_foot(r0, r1, vw);\r\n        s0 = dot(p0 - base, d);\r\n        s1 = dot(p1 - base, d);\r\n    }\r\n    var sa = s0;\r\n    var sb = s1;\r\n    for (var k = 0u; k < 2u; k++) {\r\n        let b = base[k];\r\n        let dk = d[k];\r\n        if abs(dk) < 1e-12 {\r\n            if b < lo[k] || b > hi[k] {\r\n                return vec4f(-1e6);\r\n            }\r\n        } else {\r\n            let ta = (lo[k] - b) / dk;\r\n            let tb = (hi[k] - b) / dk;\r\n            sa = max(sa, min(ta, tb));\r\n            sb = min(sb, max(ta, tb));\r\n        }\r\n    }\r\n    if sa > sb {\r\n        return vec4f(-1e6);\r\n    }\r\n    var q0 = p0;\r\n    var q1 = p1;\r\n    if sa > s0 || far {\r\n        q0 = base + sa * d;\r\n    }\r\n    if sb < s1 || far {\r\n        q1 = base + sb * d;\r\n    }\r\n    return vec4f(q0 + half, q1 + half);\r\n}\r\n// block -- what the visibility and the vertex shaders share about blocks and their inserts (G6b2; docs/RENDER2D.md\r\n// \"Bl\u{f6}cke\"); lib.rs puts it behind common.wgsl into cull.wgsl and stroke.wgsl (both declare `view`, `styles`, `spaces`).\r\n//\r\n// A block\'s records lie once in the pool, in its block-local units (relative to its base point).  An insert is an\r\n// instance: scene.rs uploads its transform (Inst, cull.wgsl), and each frame the instance pass computes its space from\r\n// the camera: where the camera\'s centre lies in the block\'s units (three f32 per axis: as precise as the camera\'s own\r\n// hi, lo and rest), the linear map from the block\'s units to pixels, the pixels per block unit, its visibility.  A space\r\n// is a View for rel2 and to_px (space_view): every shader that places a record places a block\'s record through its\r\n// insert\'s space with the same code, so that both owners of a pixel compute it from the same bits.\r\n//\r\n// An entry of the visibility (einfo, cull.wgsl) names a page, its instance + 1 (0: the page of a layer), the records of\r\n// the page it covers (first | (count - 1) << 8 | (pack - 1) << 16: `pack` instances one after the other, each of `count`\r\n// records from `first`: many small inserts of one block share a workgroup) and what is added to a record\'s place to find\r\n// its fill edge in epx.  A visible record of an insert is ITEM_BLOCK | its place in the pool, and beside it in vinst its\r\n// instance (cull.wgsl write); any other is its place in the pool.\r\n\r\nstruct Space {\r\n    // the camera\'s centre in the block\'s units: hi.x, hi.y, lo.x, lo.y; rest.xy what hi + lo leaves of it, rest.z pixels\r\n    // per block unit (widths and patterns in units scale with the insert), rest.w block units per pixel\r\n    center: vec4f,\r\n    rest: vec4f,\r\n    // the block\'s units to pixels (View.lin)\r\n    lin: vec4f,\r\n    // the insert\'s scale for widths and patterns (hi, lo): its uniform scale, or sqrt |det| of an uneven one\r\n    sw: vec4f,\r\n    // SP_* flags, the style its entities take by block (BY_BLOCK_*), its first row of the hatch table, what is added to a\r\n    // record\'s place for its fill edge in epx\r\n    u: vec4u,\r\n}\r\n\r\nconst ITEM_BLOCK: u32 = 0x80000000u;\r\n\r\n// an entry\'s records per instance, its instances\r\nfn entry_count(ei: vec4u) -> u32 {\r\n    return ((ei.z >> 8u) & 255u) + 1u;\r\n}\r\n\r\nfn entry_pack(ei: vec4u) -> u32 {\r\n    return ((ei.z >> 16u) & 255u) + 1u;\r\n}\r\nconst SP_VISIBLE: u32 = 1u;\r\nconst SP_MIRROR: u32 = 2u;\r\n// Style.r1.y: what an entity of a block takes from its insert\'s style (scene.rs BY_BLOCK_*)\r\nconst BB_COLOR: u32 = 1u;\r\nconst BB_WIDTH: u32 = 2u;\r\nconst BB_DASH: u32 = 4u;\r\n// Style.r1.x: the transparency in percent, or these\r\nconst T_BY_LAYER: u32 = 0xFFFFFFFFu;\r\nconst T_BY_BLOCK: u32 = 0xFFFFFFFEu;\r\n\r\n// an insert\'s space as a View: rel2 and to_px with the block\'s units; turn.xy holds its scale for widths (sw)\r\nfn space_view(sp: Space) -> View {\r\n    var v = view;\r\n    v.center = sp.center;\r\n    v.rest = sp.rest;\r\n    v.lin = sp.lin;\r\n    v.turn = vec4f(sp.sw.xy, view.turn.zw);\r\n    return v;\r\n}\r\n\r\n// a record\'s style: its own, inside an insert (blk) with what it takes by block from the insert\'s (ins): the colour, the\r\n// width and its unit, the dash pattern, the transparency.  Outside a block an entity\'s style is its own\r\nfn style_in(own: u32, ins: u32, blk: bool) -> Style {\r\n    var s = styles[own];\r\n    if blk {\r\n        let f = s.r1.y;\r\n        if f != 0u || s.r1.x == T_BY_BLOCK {\r\n            let b = styles[ins];\r\n            if (f & BB_COLOR) != 0u {\r\n                s.color = b.color;\r\n            }\r\n            if (f & BB_WIDTH) != 0u {\r\n                s.width = b.width;\r\n                s.flags = (s.flags & ~15u) | (b.flags & 15u);\r\n            }\r\n            if (f & BB_DASH) != 0u {\r\n                s.flags = (s.flags & ~0xF1F000u) | (b.flags & 0xF1F000u);\r\n                s.kappa = b.kappa;\r\n                s.phase0 = b.phase0;\r\n                s.period = b.period;\r\n                s.duty = b.duty;\r\n                s.ons = b.ons;\r\n                s.ends = b.ends;\r\n            }\r\n            if s.r1.x == T_BY_BLOCK {\r\n                s.r1.x = b.r1.x;\r\n            }\r\n        }\r\n    }\r\n    return s;\r\n}\r\n\r\n// the style whose dash pattern the fragment shader reads (misc.z): the insert\'s where the pattern is by block\r\nfn dash_style(own: u32, ins: u32, blk: bool) -> u32 {\r\n    if blk && (styles[own].r1.y & BB_DASH) != 0u {\r\n        return ins;\r\n    }\r\n    return own;\r\n}\r\n\r\n// periods per block unit of a pattern in pixels inside an insert: the camera\'s periods per drawing unit (three f32,\r\n// exact) times the insert\'s scale (hi, lo), the product\'s exact pieces summed again into three f32\r\nfn kappa_scaled(k: vec3f, s: vec2f) -> vec3f {\r\n    let a = two_prod(k.x, s.x);\r\n    let b = two_prod(k.x, s.y);\r\n    let c = two_prod(k.y, s.x);\r\n    let x = two_sum(b.x, c.x);\r\n    let y = two_sum(a.y, x.x);\r\n    let low = ((x.y + y.y) + (b.y + c.y)) + (k.y * s.y + k.z * s.x);\r\n    // (a sum of three f32 to 2^-72 of it, each well below the one before: what periods() takes)\r\n    return vec3f(a.x, y.x, low);\r\n}\r\n\r\n// a colour with its transparency (percent, AutoCAD\'s 0 to 90; T_BY_LAYER: the layer\'s, from lists.z) multiplied into its\r\n// alpha, in whole numbers (0: the colour as it is)\r\nfn ink(color: u32, t: u32, layer_t: u32) -> u32 {\r\n    var p = t;\r\n    if p == T_BY_LAYER {\r\n        p = layer_t;\r\n    }\r\n    if p == 0u || p > 100u {\r\n        return color;\r\n    }\r\n    let a = color >> 24u;\r\n    return (color & 0xFFFFFFu) | (((a * (100u - p) + 50u) / 100u) << 24u);\r\n}\r\n\r\n// textc -- what the visibility and the text shaders share about text records (G6c; docs/RENDER2D.md \"Text\"); lib.rs puts\r\n// it behind block.wgsl into cull.wgsl and text.wgsl.\r\n//\r\n// A glyph\'s record (K_GLYPH): a: its text\'s origin (hi, lo; every glyph of a text the same bits); b: the text\'s map from em\r\n// units to drawing units (or pixels, G_PX), m00 m01 m10 m11; v: its pen (em from the origin, x and y), its row of the glyph\r\n// table, the right end of its cell along the baseline (em; the next glyph\'s pen); w: its quad\'s box from its pen (em, two\r\n// pairs of f16: x0 y0, x1 y1), style + 1, flags | K_GLYPH << 8 | how many records back its previous glyph lies << 16 |\r\n// how many on its next << 24 (0: none).  A bar\'s (K_BAR): a, b as a glyph\'s; v: x0, x1, y0, y1 (em from the origin); w.x\r\n// the share of the bar its words\' ink covers.\r\n//\r\n// Mode: a text whose em is at least T_BAR pixels shows glyphs, below that bars; the em in pixels is taken at the scale the\r\n// CPU last decided by (view.misc.z = scale / that scale), which moves only when the scale leaves a band of HYST around it:\r\n// zooming does not flicker between the two.\r\n\r\nconst K_GLYPH: u32 = 5u;\r\nconst K_BAR: u32 = 6u;\r\n// a mark (no cell of its own: it draws its ink alone); a map to pixels (a style in pixels)\r\nconst G_MARK: u32 = 1u;\r\nconst G_PX: u32 = 2u;\r\n// below this many pixels per em a text is drawn as bars\r\nconst T_BAR: f32 = 7.0;\r\n\r\n// the em -> pixel map of a text record: the pixel offsets of one em along its x and y (y down)\r\nfn text_map(r: Record, vw: View) -> mat2x2f {\r\n    let a0 = to_px(vec2f(r.b.x, r.b.z), vw);\r\n    let a1 = to_px(vec2f(r.b.y, r.b.w), vw);\r\n    if (r.w.w & G_PX) != 0u {\r\n        return mat2x2f(a0 * vw.rest.w, a1 * vw.rest.w);\r\n    }\r\n    return mat2x2f(a0, a1);\r\n}\r\n\r\n// glyphs (false: bars) at this map\r\nfn text_glyphs(a: mat2x2f) -> bool {\r\n    return length(a[1]) * view.misc.z >= T_BAR;\r\n}\r\n\r\n// the record\'s box: a glyph\'s quad from its pen, a bar\'s (em from the text\'s origin: x0, y0, x1, y1)\r\nfn text_box(r: Record) -> vec4f {\r\n    if kind_of_record(r) == K_BAR {\r\n        return vec4f(bitcast<f32>(r.v.x), bitcast<f32>(r.v.z), bitcast<f32>(r.v.y), bitcast<f32>(r.v.w));\r\n    }\r\n    let pen = bitcast<vec2f>(r.v.xy);\r\n    return vec4f(pen + unpack2x16float(r.w.x), pen + unpack2x16float(r.w.y));\r\n}\r\n// cull: the visible lists of a frame, in draw order, in three passes (docs/RENDER2D.md \"Sichtbarkeit\").  The records\r\n// lie in pages of 256; the pages of the visible layers, in their draw order, are the entries of `einfo` (scene.rs builds\r\n// it), one workgroup each: a layer\'s pages, and where an insert of a block sits in its layer the pages of the block\'s\r\n// records for that insert (an entry covers the records `first` to `first + count` of its page and names the insert\'s\r\n// instance; block.wgsl).  The lists must keep the painter\'s order (layer order, then the order of insertion, a block\'s\r\n// entities in theirs within each insert): a translucent stroke drawn before the one under it would blend wrongly.  So no\r\n// atomic counter per list as in render3d, but a stable compaction:\r\n//\r\n//   instances  (G6b2, before the others) per instance of a block: its space for this frame (where the camera lies in the\r\n//           block\'s units, the map to pixels) and whether its block\'s box, grown by its strokes\' reach, meets the target\r\n//   count   every record of a page against the target (an insert\'s record through its instance\'s space, none of an\r\n//           instance out of view): a bit per record (`masks`, 8 words per entry), the page\'s count; a fill\'s edge: its\r\n//           segment in pixels to `epx`\r\n//   scan    one workgroup: the exclusive sum of the counts in entry order (`offsets`), then per visible layer its first\r\n//           place and its count (`lists`) and its indirect draw (`draws`): 6 indices per quad, at most BATCH quads per\r\n//           instance (u16 indices), as many instances as that takes; first_instance names the layer on Voll\r\n//   write   every visible record to its place: the page\'s offset plus the bits before its own (an insert\'s record as\r\n//           ITEM_BLOCK | its place in the pool, its instance beside it in `vinst`: the vertex shader loads both at once)\r\n//\r\n// The test of a record (shown): the box around its segment, grown by the stroke\'s reach (half the width times its\r\n// join\'s bound, reach_bound, where it has a neighbour, else half the width; and a pixel for the smoothed edge) and by\r\n// what f32 may round points far away to, against the target\'s rectangle, by separating axes.  An arc\'s piece: the\r\n// box around its chord (its start from the record before it), grown as well by how far the piece bulges beyond the\r\n// chord (its sagitta).  A fill\'s edge: its box grown by its quad\'s reach (2 px); a fill\'s triangle: the box of its three\r\n// corners and 2 px.  Free records and arcs\' heads (style 0) never pass; with culling off every live record does.  An\r\n// insert\'s records take its style where they take theirs by block (block.wgsl style_in), widths in units scaled with it.\r\n\r\nconst BATCH: u32 = 16384u;\r\n\r\nstruct Push {\r\n    // entries of `einfo` (the instance pass: instances), visible layers, 1 on Voll (one multi-draw: first_instance = layer\r\n    // << 16)\r\n    entries: u32,\r\n    layers: u32,\r\n    multi: u32,\r\n    reserved: u32,\r\n}\r\n\r\n@group(0) @binding(0) var<uniform> view: View;\r\n@group(0) @binding(1) var<storage, read> pool: array<Record>;\r\n@group(0) @binding(2) var<storage, read> styles: array<Style>;\r\n// per entry: the page it looks at, its instance + 1 (0: a layer\'s page), its records (first | count << 16), the offset of\r\n// its fill edges in epx (block.wgsl)\r\n@group(0) @binding(3) var<storage, read> einfo: array<vec4u>;\r\n// per entry: 8 words, a bit per record of its page\r\n@group(0) @binding(4) var<storage, read_write> masks: array<u32>;\r\n// per entry: its visible records\r\n@group(0) @binding(5) var<storage, read_write> counts: array<u32>;\r\n// per visible layer, in draw order: its first entry, its entries, its transparency (percent, G6b2)\r\n@group(0) @binding(6) var<storage, read> layers: array<vec4u>;\r\n// per entry: the place of its first visible record\r\n@group(0) @binding(7) var<storage, read_write> offsets: array<u32>;\r\n// per visible layer: its first place in `visible`, its count, its transparency (stroke.wgsl reads them)\r\n@group(0) @binding(8) var<storage, read_write> lists: array<vec4u>;\r\n// per visible layer: VkDrawIndexedIndirectCommand (5 words)\r\n@group(0) @binding(9) var<storage, read_write> draws: array<u32>;\r\n@group(0) @binding(10) var<storage, read_write> visible: array<u32>;\r\n// per record: a fill edge\'s segment in pixels of the target (fill.wgsl reads it; count writes it for every edge of the\r\n// visible layers, in view or not)\r\n@group(0) @binding(11) var<storage, read_write> epx: array<vec4f>;\r\n// per instance of a block: its space this frame (the instance pass writes it, count and the vertex shaders read it)\r\n@group(0) @binding(12) var<storage, read_write> spaces: array<Space>;\r\n// per instance: its transform and box (scene.rs instance words, uploaded when an insert changes)\r\n@group(0) @binding(13) var<storage, read> insts: array<Inst>;\r\n// per visible record of an insert: its instance (write writes it, the vertex shaders read it)\r\n@group(0) @binding(14) var<storage, read_write> vinst: array<u32>;\r\nvar<immediate> pc: Push;\r\n\r\nvar<workgroup> wmask: array<atomic<u32>, 8>;\r\nvar<workgroup> partial: array<u32, 256>;\r\n\r\n// record i is in the frame: live, and its stroke\'s box meets the target (see above); vw: the view, or its insert\'s space\r\n// (blk, its style by block ins)\r\nfn shown(i: u32, vw: View, blk: bool, ins: u32) -> bool {\r\n    let r = pool[i];\r\n    if r.w.z == 0u {\r\n        return false;\r\n    }\r\n    if view.misc.x != 0.0 {\r\n        return true;\r\n    }\r\n    if kind_of_record(r) >= K_GLYPH {\r\n        return text_shown(r, vw);\r\n    }\r\n    let st = style_in(r.w.z - 1u, ins, blk);\r\n    let kind = kind_of_record(r);\r\n    if kind == K_TRI {\r\n        // a fill\'s triangle: the box of its corners (the starts of the edge records they name) and 2 px, against the target\r\n        let base = i - bitcast<u32>(r.a.x);\r\n        let t0 = to_px(rel2(pool[base + bitcast<u32>(r.a.y)].a, vw).xy, vw);\r\n        let t1 = to_px(rel2(pool[base + bitcast<u32>(r.a.z)].a, vw).xy, vw);\r\n        let t2 = to_px(rel2(pool[base + bitcast<u32>(r.a.w)].a, vw).xy, vw);\r\n        let m = 2.0 + 1e-3 + 4e-6 * (length(t0) + length(t1) + length(t2));\r\n        let lo = min(min(t0, t1), t2) - vec2f(m);\r\n        let hi = max(max(t0, t1), t2) + vec2f(m);\r\n        let hv = 0.5 * view.turn.zw;\r\n        return all(lo <= hv) && all(hi >= -hv);\r\n    }\r\n    let fill_edge = kind == K_EDGE;\r\n    var h = half_width(st, vw);\r\n    if fill_edge {\r\n        // (a fill\'s edge: its smoothed edge reaches FILL_R on either side, its quad a pixel more)\r\n        h = 1.0;\r\n    }\r\n    let arc = kind == K_ARC;\r\n    // (an arc piece: from its start, the end of the record before it, to its end)\r\n    var pa = r.a;\r\n    if arc {\r\n        pa = pool[i - 1u].b;\r\n    }\r\n    let p0 = to_px(rel2(pa, vw).xy, vw);\r\n    let p1 = to_px(rel2(r.b, vw).xy, vw);\r\n    // (a miter\'s bound only where the record has a neighbour: a lone segment\'s caps reach half its width)\r\n    let joined = (r.w.w & (F_PREV | F_NEXT)) != 0u && !arc && !fill_edge;\r\n    var bulge = 0.0;\r\n    if arc {\r\n        bulge = sagitta(0.5 * length(p1 - p0), (bitcast<f32>(r.v.x) + bitcast<f32>(r.v.y)) * vw.rest.z);\r\n        // (a square cap follows the tangent at its end, which turns up to ARC_STEP / 2 away from the chord: its corner\r\n        // reaches h (cos + sin) of that along and across it, below 1.18 h)\r\n        if cap_of(st) == CAP_SQUARE {\r\n            bulge += 0.18 * h;\r\n        }\r\n    }\r\n    // (f32 holds 24 bits of a point far away: what that may cost, many times over)\r\n    let reach = h * select(1.0, reach_bound(st), joined) + bulge + 1.0 + 1e-3 + 4e-6 * (length(p0) + length(p1));\r\n    let half = 0.5 * view.turn.zw;\r\n    let dv = p1 - p0;\r\n    let len = length(dv);\r\n    var d = vec2f(1.0, 0.0);\r\n    if len > 0.0 {\r\n        d = dv / len;\r\n    }\r\n    let n = vec2f(-d.y, d.x);\r\n    let c = 0.5 * (p0 + p1);\r\n    let e = 0.5 * len + reach;\r\n    if abs(c.x) > half.x + e * abs(d.x) + reach * abs(n.x) {\r\n        return false;\r\n    }\r\n    if abs(c.y) > half.y + e * abs(d.y) + reach * abs(n.y) {\r\n        return false;\r\n    }\r\n    if abs(dot(c, d)) > e + half.x * abs(d.x) + half.y * abs(d.y) {\r\n        return false;\r\n    }\r\n    if abs(dot(c, n)) > reach + half.x * abs(n.x) + half.y * abs(n.y) {\r\n        return false;\r\n    }\r\n    return true;\r\n}\r\n\r\n// a glyph\'s or a bar\'s record (G6c, textc.wgsl): drawn in this mode (glyphs or bars) and its box, mapped to pixels (the\r\n// parallelogram\'s box), grown by 2 px (its quad\'s pixel and the smoothed edge) and by what f32 may round far points to,\r\n// meets the target\r\nfn text_shown(r: Record, vw: View) -> bool {\r\n    let a = text_map(r, vw);\r\n    if text_glyphs(a) == (kind_of_record(r) == K_BAR) {\r\n        return false;\r\n    }\r\n    let b = text_box(r);\r\n    let o = to_px(rel2(r.a, vw).xy, vw);\r\n    let p0 = o + a * b.xy;\r\n    let p1 = o + a * b.zy;\r\n    let p2 = o + a * b.xw;\r\n    let p3 = o + a * b.zw;\r\n    let m = 2.0 + 1e-3 + 4e-6 * (length(p0) + length(p1) + length(p2) + length(p3));\r\n    let lo = min(min(p0, p1), min(p2, p3)) - vec2f(m);\r\n    let hi = max(max(p0, p1), max(p2, p3)) + vec2f(m);\r\n    let hv = 0.5 * view.turn.zw;\r\n    return all(lo <= hv) && all(hi >= -hv);\r\n}\r\n\r\n@compute @workgroup_size(256)\r\nfn count(@builtin(local_invocation_index) li: u32, @builtin(workgroup_id) wid: vec3u) {\r\n    let w = wid.x;\r\n    let ei = einfo[w];\r\n    // (the records of the page this entry covers, `pack` instances of them one after the other; an insert\'s only while\r\n    // its instance is in view: no early return before the barrier, every invocation goes on)\r\n    let cnt = entry_count(ei);\r\n    let pack = entry_pack(ei);\r\n    let k = li / cnt;\r\n    let i = ei.x * PAGE + (ei.z & 255u) + (li - k * cnt);\r\n    var on = k < pack;\r\n    var vw = view;\r\n    var blk = false;\r\n    var ins = 0u;\r\n    var mir = false;\r\n    if ei.y != 0u {\r\n        let sp = spaces[ei.y - 1u + min(k, pack - 1u)];\r\n        vw = space_view(sp);\r\n        blk = true;\r\n        ins = sp.u.y;\r\n        mir = (sp.u.x & SP_MIRROR) != 0u;\r\n        on = on && (sp.u.x & SP_VISIBLE) != 0u;\r\n    }\r\n    let r = pool[i];\r\n    if on && kind_of_record(r) == K_EDGE && r.w.z != 0u {\r\n        // (a mirrored insert\'s edges turned around: the inside stays on their right in pixels, fill.wgsl)\r\n        var e = edge_px(rel2(r.a, vw), rel2(r.b, vw), vw);\r\n        if mir {\r\n            e = e.zwxy;\r\n        }\r\n        epx[i + ei.w] = e;\r\n    }\r\n    if on && shown(i, vw, blk, ins) {\r\n        atomicOr(&wmask[li >> 5u], 1u << (li & 31u));\r\n    }\r\n    workgroupBarrier();\r\n    if li < 8u {\r\n        masks[w * 8u + li] = atomicLoad(&wmask[li]);\r\n    }\r\n    if li == 0u {\r\n        var n = 0u;\r\n        for (var k = 0u; k < 8u; k++) {\r\n            n += countOneBits(atomicLoad(&wmask[k]));\r\n        }\r\n        counts[w] = n;\r\n    }\r\n}\r\n\r\n@compute @workgroup_size(256)\r\nfn scan(@builtin(local_invocation_index) li: u32) {\r\n    let n = pc.entries;\r\n    let per = (n + 255u) / 256u;\r\n    let start = min(li * per, n);\r\n    let end = min(start + per, n);\r\n    var sum = 0u;\r\n    for (var e = start; e < end; e++) {\r\n        sum += counts[e];\r\n    }\r\n    partial[li] = sum;\r\n    workgroupBarrier();\r\n    // the 256 sums, inclusive (Hillis-Steele)\r\n    for (var off = 1u; off < 256u; off = off << 1u) {\r\n        var v = 0u;\r\n        if li >= off {\r\n            v = partial[li - off];\r\n        }\r\n        workgroupBarrier();\r\n        partial[li] += v;\r\n        workgroupBarrier();\r\n    }\r\n    var run = partial[li] - sum;\r\n    for (var e = start; e < end; e++) {\r\n        offsets[e] = run;\r\n        run += counts[e];\r\n    }\r\n    let total = partial[255];\r\n    // (the offsets of every invocation before the layers read them)\r\n    storageBarrier();\r\n    for (var k = li; k < pc.layers; k += 256u) {\r\n        let l = layers[k];\r\n        var first = total;\r\n        if l.x < n {\r\n            first = offsets[l.x];\r\n        }\r\n        var last = total;\r\n        if l.x + l.y < n {\r\n            last = offsets[l.x + l.y];\r\n        }\r\n        let c = last - first;\r\n        lists[k] = vec4u(first, c, l.z, 0u);\r\n        draws[k * 5u] = 6u * min(c, BATCH);\r\n        draws[k * 5u + 1u] = (c + BATCH - 1u) / BATCH;\r\n        draws[k * 5u + 2u] = 0u;\r\n        draws[k * 5u + 3u] = 0u;\r\n        draws[k * 5u + 4u] = select(0u, k << 16u, pc.multi != 0u);\r\n    }\r\n}\r\n\r\n@compute @workgroup_size(256)\r\nfn write(@builtin(local_invocation_index) li: u32, @builtin(workgroup_id) wid: vec3u) {\r\n    let w = wid.x;\r\n    let word = masks[w * 8u + (li >> 5u)];\r\n    let bit = li & 31u;\r\n    if ((word >> bit) & 1u) == 0u {\r\n        return;\r\n    }\r\n    var at = offsets[w];\r\n    for (var k = 0u; k < (li >> 5u); k++) {\r\n        at += countOneBits(masks[w * 8u + k]);\r\n    }\r\n    at += countOneBits(word & ((1u << bit) - 1u));\r\n    let ei = einfo[w];\r\n    let cnt = entry_count(ei);\r\n    let k = li / cnt;\r\n    let i = ei.x * PAGE + (ei.z & 255u) + (li - k * cnt);\r\n    if ei.y != 0u {\r\n        visible[at] = ITEM_BLOCK | i;\r\n        vinst[at] = ei.y - 1u + k;\r\n    } else {\r\n        visible[at] = i;\r\n    }\r\n}\r\n\r\n// ---------------------------------------------------------------- the instances of blocks (G6b2)\r\n\r\n// an instance of a block (scene.rs Xf::words): its transform world = t + M local, t as three f32 per axis, the inverse\r\n// of M as three f32 per entry, M itself; the block\'s box (local, as points: hi.x, hi.y, lo.x, lo.y); how far its\r\n// strokes reach beyond it (px, block units); flags, the style by block, the first row of its hatches\r\nstruct Inst {\r\n    // t.x as three f32, t.y\'s first\r\n    t: vec4f,\r\n    // t.y\'s other two, the insert\'s scale for widths (hi, lo: block.wgsl Space.sw)\r\n    t2: vec4f,\r\n    // M^-1: n00 (three f32), n01 (three), n10 (three), n11 (three)\r\n    n0: vec4f,\r\n    n1: vec4f,\r\n    n2: vec4f,\r\n    // M (f32): m00, m01, m10, m11\r\n    m: vec4f,\r\n    blo: vec4f,\r\n    bhi: vec4f,\r\n    // px, block units\r\n    reach: vec4f,\r\n    // SP_MIRROR, the style by block, the hatch table\'s first row, what is added to a record\'s place for its fill edge in epx\r\n    u: vec4u,\r\n}\r\n\r\n// three f32 whose sum is a + b + c, each about below half an ulp of the one before\r\nfn renorm3(a: f32, b: f32, c: f32) -> vec3f {\r\n    let x = two_sum(b, c);\r\n    let y = two_sum(a, x.x);\r\n    let z = two_sum(y.y, x.y);\r\n    return vec3f(y.x, z.x, z.y);\r\n}\r\n\r\n// sums and products of three f32 to about 2^-70: where the camera lies in a block\'s units must be as precise as the\r\n// camera\'s own three f32, whatever the insert\'s place and scale (docs/RENDER2D.md \"Bl\u{f6}cke\")\r\nfn t3_add(a: vec3f, b: vec3f) -> vec3f {\r\n    let s = two_sum(a.x, b.x);\r\n    let t = two_sum(a.y, b.y);\r\n    let u = two_sum(s.y, t.x);\r\n    return renorm3(s.x, u.x, (t.y + u.y) + (a.z + b.z));\r\n}\r\n\r\nfn t3_mul(a: vec3f, b: vec3f) -> vec3f {\r\n    let p = two_prod(a.x, b.x);\r\n    let q = two_prod(a.x, b.y);\r\n    let r = two_prod(a.y, b.x);\r\n    let s1 = two_sum(q.x, r.x);\r\n    let s2 = two_sum(p.y, s1.x);\r\n    return renorm3(p.x, s2.x, ((s1.y + s2.y) + (q.y + r.y)) + ((a.x * b.z + a.y * b.y) + a.z * b.x));\r\n}\r\n\r\n@compute @workgroup_size(64)\r\nfn instances(@builtin(global_invocation_id) gid: vec3u) {\r\n    let k = gid.x;\r\n    if k >= pc.entries {\r\n        return;\r\n    }\r\n    let it = insts[k];\r\n    // the camera\'s centre minus t, then through M^-1: the camera in the block\'s units, three f32 per axis\r\n    let dx = t3_add(vec3f(view.center.x, view.center.z, view.rest.x), -vec3f(it.t.x, it.t.y, it.t.z));\r\n    let dy = t3_add(vec3f(view.center.y, view.center.w, view.rest.y), -vec3f(it.t.w, it.t2.x, it.t2.y));\r\n    let cx = t3_add(t3_mul(it.n0.xyz, dx), t3_mul(vec3f(it.n0.w, it.n1.x, it.n1.y), dy));\r\n    let cy = t3_add(t3_mul(vec3f(it.n1.z, it.n1.w, it.n2.x), dx), t3_mul(it.n2.yzw, dy));\r\n    // the map to pixels: the camera\'s rotation times M over the insert\'s scale (a rotation, mirrored or not, where the\r\n    // insert scales evenly); pixels per block unit: that scale over the camera\'s\r\n    let sw = it.t2.z + it.t2.w;\r\n    let c = view.lin;\r\n    let m = it.m;\r\n    var sp: Space;\r\n    sp.center = vec4f(cx.x, cy.x, cx.y, cy.y);\r\n    sp.rest = vec4f(cx.z, cy.z, sw * view.rest.z, view.rest.w / sw);\r\n    sp.lin = vec4f(c.x * m.x + c.y * m.z, c.x * m.y + c.y * m.w, c.z * m.x + c.w * m.z, c.z * m.y + c.w * m.w) / sw;\r\n    sp.sw = vec4f(it.t2.z, it.t2.w, 0.0, 0.0);\r\n    sp.u = vec4u(it.u.x & SP_MIRROR, it.u.y, it.u.z, it.u.w);\r\n    // in view: the box\'s corners from the camera in the block\'s units (as rel2 places points), grown by the reach and by\r\n    // what f32 may round far corners to, against the target\r\n    var vis = view.misc.x != 0.0;\r\n    if !vis {\r\n        let vw = space_view(sp);\r\n        let p0 = to_px(rel2(it.blo, vw).xy, vw);\r\n        let p1 = to_px(rel2(it.bhi, vw).xy, vw);\r\n        let p2 = to_px(rel2(vec4f(it.bhi.x, it.blo.y, it.bhi.z, it.blo.w), vw).xy, vw);\r\n        let p3 = to_px(rel2(vec4f(it.blo.x, it.bhi.y, it.blo.z, it.bhi.w), vw).xy, vw);\r\n        let reach = it.reach.x + it.reach.y * sp.rest.z + 2.0 + 1e-3 + 4e-6 * (length(p0) + length(p1) + length(p2) + length(p3));\r\n        let lo = min(min(p0, p1), min(p2, p3)) - vec2f(reach);\r\n        let hi = max(max(p0, p1), max(p2, p3)) + vec2f(reach);\r\n        let hv = 0.5 * view.turn.zw;\r\n        vis = all(lo <= hv) && all(hi >= -hv);\r\n    }\r\n    if vis {\r\n        sp.u.x |= SP_VISIBLE;\r\n    }\r\n    spaces[k] = sp;\r\n}\r\n";