pub const SCATTER_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// scatter: the frame\'s changed records, each to its place in the pool (render3d\'s pattern: one buffer, one dispatch,\r\n// however scattered the places are). A free record is all zeros (style 0).\r\n\r\nstruct Change {\r\n at: u32,\r\n r0: u32,\r\n r1: u32,\r\n r2: u32,\r\n rec: Record,\r\n}\r\n\r\nstruct Push {\r\n count: u32,\r\n}\r\n\r\n@group(0) @binding(0) var<storage, read> changes: array<Change>;\r\n@group(0) @binding(1) var<storage, read_write> pool: array<Record>;\r\n// (common.wgsl\'s functions name it; this pass uses none of them, so it is no binding of its layout)\r\n@group(0) @binding(2) var<uniform> view: View;\r\nvar<immediate> pc: Push;\r\n\r\n@compute @workgroup_size(64)\r\nfn main(@builtin(global_invocation_id) gid: vec3u) {\r\n let i = gid.x;\r\n if i >= pc.count {\r\n return;\r\n }\r\n let c = changes[i];\r\n pool[c.at] = c.rec;\r\n}\r\n";