pub const STROKE_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// stroke: segments and arcs as quads with analytic coverage (decision 25; docs/RENDER2D.md \"Der Shader\"). No vertex\r\n// buffer: a draw of the visible list of one layer, BATCH quads (four corners, u16 indices 0 1 2 2 1 3) per instance.\r\n//\r\n// The vertex shader places a segment relative to the camera (common.wgsl: rel2), in pixels, and clips it to the\r\n// target grown by its reach: an end beyond that is open (no cap, no join, nothing of it shows). Points far from the\r\n// camera have f32\'s 24 bits of their distance only, so the line between two such points would wander; when both ends\r\n// lie beyond FAR pixels, the line\'s place comes from the exact cross product of its ends in double-f32 (far_foot).\r\n// The quad covers the segment\'s rectangle and how far its join\'s or cap\'s piece reaches beyond its ends, grown by a\r\n// pixel for the smoothed edge.\r\n//\r\n// The fragment shader computes the share of the pixel (a unit square) inside the stroke, exactly: the area of the\r\n// square inside convex pieces bounded by straight lines. One edge cutting the pixel gives that half plane\'s share\r\n// (cov, the box filter of a straight edge at any angle), the strip\'s two parallel edges alone cov(left) + cov(right)\r\n// - 1 (a hairline too), more edges the area by Green\'s theorem (area6, up to six half planes). A circle is cut by its\r\n// tangent at the pixel\'s centre. Most pixels lie in the body (the strip alone, no end near: two cov).\r\n//\r\n// Near a join the stroke is cut into tiles along the join\'s bisector: this segment\'s strip on its side of the\r\n// bisector, the neighbour\'s on the other, a bevel\'s line cutting both; a round join\'s tile ends at the line at right\r\n// angles through the vertex, the circle\'s sector beyond it is a piece of its own. The tiles do not overlap and fill\r\n// the stroke as long as the bisector\'s chord (h tan(phi / 2) along each segment) lies within both segments; then the\r\n// shares add up exactly, and the outer side gets the miter\'s point, the bevel\'s corner or the round arc by itself.\r\n// A sharper turn of a shorter segment takes the union instead: the rectangles, the join\'s piece beyond both their\r\n// ends, |a| + |w| + |b| - |a and b| (a short segment near both its joins: both unions, the neighbours\' rectangles\r\n// meeting each other left out). Caps: the rectangle grown by half a width (square), the half circle beyond its end\r\n// (round). The pieces near the pixel are bits of a mask, and one loop adds their shares, so that area6 sits in the\r\n// solid path once: inlined at forty places it made the shader so long that the GPU spent its time loading code.\r\n//\r\n// Overlap (docs/RENDER2D.md, \"\u{dc}berlappung\"): every pixel near a join lies in both segments\' quads, and a translucent\r\n// polyline must not blend twice there. No depth and no stencil: a pixel near a join belongs to the nearer of the two\r\n// segments (their centre lines; near the vertex that is the bisector, beyond a short neighbour\'s end the longer\r\n// one), and the other discards it. Both compute the two distances from the same bits (the vertex from the same\r\n// hi/lo, the segments as vectors the CPU rounded once: Record.v), so exactly one of them draws, with the union of\r\n// both pieces. Where a polyline comes back over itself away from a join its pieces blend each, as two entities\r\n// would.\r\n//\r\n// Short thin strokes (TINY_LEN, TINY_H: mostly what a drawing shows of its details from far away) take a fast way near\r\n// their ends: one segment\'s tile as the product of the strip\'s share and the share on its side of the bisector (the\r\n// neighbour\'s piece likewise), instead of the exact area (docs/RENDER2D.md \"Der schnelle Weg\"); longer or wider strokes\r\n// stay exact.\r\n//\r\n// Arcs (vs_arc, fs_arc; docs/RENDER2D.md \"B\u{f6}gen und Kreise\"): an arc is a head and pieces of at most 22.5 degrees, each\r\n// a record with its circle\'s centre and its end, its start the end of the record before. The circle near the camera\r\n// comes from |v|^2 - r^2 in double-f32 (v the camera minus the centre): F, the point of the circle nearest the camera\'s\r\n// centre, its outward unit u, the radius in pixels; the fragment shader measures from F, (|q|^2 + 2 r q.u) / (|q + r u|\r\n// + r) for q = pixel - F, which stays exact in f32 for any radius. The quad of a piece lies between the chord at the\r\n// inner radius and the tangent at the outer one (over a circle much larger than the target: only the part over it),\r\n// so the pixels it shades follow the stroke. A pixel near a joint of two pieces is the one\'s on whose side of the\r\n// joint\'s radial line its centre lies (both from the same bits), and that one takes the whole ring there.\r\n//\r\n// Dash patterns (common.wgsl\'s head): the vertex shader finds the pattern\'s place at a reference point (a segment: the\r\n// camera centre\'s foot on its line, kept within it; an arc: F kept within the piece) from the nearer end, in\r\n// double-f32; the fragment shader walks the pattern from there, in dash units relative to that place (so that a period\r\n// of a million pixels keeps 1/16 px), and adds each dash that meets the pixel as a piece: the strip between its two ends\r\n// (a square cap half a width further, a round one a half circle), at a join the neighbour\'s dashes and the join\'s piece\r\n// where a dash runs through the vertex. Patterns whose period comes near a pixel turn into a flat tone of their duty\r\n// (DASH_FLAT, DASH_EXACT).\r\n//\r\n// Colours are sRGB codes blended as they are (premultiplied), as lib/gfx blends them; a transparency (the entity\'s, its\r\n// layer\'s or its insert\'s, G6b2) is multiplied into the colour\'s alpha here (block.wgsl ink).\r\n//\r\n// Blocks (G6b2, block.wgsl): a visible record of an insert comes as ITEM_BLOCK | its place in the pool, its instance\r\n// beside it in vinst; it is placed through its instance\'s space (a View of the block\'s units) by the same code as every record, so that both\r\n// owners of a pixel compute it from the same bits.\r\n\r\nconst BATCH: u32 = 16384u;\r\nconst OPEN: u32 = 0u;\r\nconst CAP: u32 = 1u;\r\nconst JOIN: u32 = 2u;\r\n// segments up to this long (px) and strokes up to this half width (px) take the fast way near their ends\r\nconst TINY_LEN: f32 = 3.0;\r\nconst TINY_H: f32 = 0.75;\r\n// a pixel near a join whose two squared distances (to the incoming and the outgoing segment) are equal up to this share is\r\n// the incoming segment\'s, as each of the two decides it (the outgoing one keeps a pixel only when it is nearer by more): the\r\n// two decide apart, a GPU\'s compiler may round the two sides differently, and at a near tie both gave the pixel up (Intel\'s\r\n// Meteor Lake driver, a turn back in the changes test, docs/RENDER2D.md)\r\nconst JOIN_TIE: f32 = 1e-4;\r\n// a dash pattern of a period up to DASH_FLAT pixels is drawn as a flat tone of its duty, from DASH_EXACT on exactly,\r\n// mixed in between\r\nconst DASH_FLAT: f32 = 2.0;\r\nconst DASH_EXACT: f32 = 4.0;\r\n// beyond this many pixels of arc from a piece\'s nearer end, the pattern\'s place at the reference point is computed from\r\n// the chord in double-f32\r\nconst ARC_FAR: f32 = 100000.0;\r\n// misc.x of VsOut: start kind | end kind << 2 | cap << 4 | join << 8 | the polyline\'s ends << 12 (an arc: its own\r\n// ends), and these\r\nconst M_DASHED: u32 = 16384u;\r\nconst M_RESTART_S: u32 = 32768u;\r\nconst M_RESTART_E: u32 = 65536u;\r\nconst M_ARC: u32 = 131072u;\r\n\r\nstruct Push {\r\n // the visible layer this draw shows (Basis; 0 on Voll, where first_instance carries it in its high 16 bits)\r\n layer: 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@group(0) @binding(3) var<storage, read> visible: array<u32>;\r\n@group(0) @binding(4) var<storage, read> lists: array<vec4u>;\r\n// per visible record of an insert its instance (cull.wgsl write); per instance its space this frame\r\n@group(0) @binding(7) var<storage, read> vinst: array<u32>;\r\n@group(0) @binding(8) var<storage, read> spaces: array<Space>;\r\nvar<immediate> pc: Push;\r\n\r\nstruct VsOut {\r\n @builtin(position) pos: vec4f,\r\n // a segment: its ends in pixels of the target (clipped where they lay beyond it); an arc\'s piece: F (px of the\r\n // target) and the outward unit there\r\n @location(0) @interpolate(flat) seg: vec4f,\r\n // a segment, in pixels: the previous point minus the start, the next point minus the end; an arc\'s piece: its start\r\n // and end (px of the target)\r\n @location(1) @interpolate(flat) nb: vec4f,\r\n // half width (px); a segment: the miter limit, the end minus the start in pixels (from Record.v, as the neighbours\r\n // have it); an arc\'s piece: the radius (px), the turn\'s sign in pixels, the dash reference\'s angle from F\r\n @location(2) @interpolate(flat) par: vec4f,\r\n // flags (see M_*), colour, style\r\n @location(3) @interpolate(flat) misc: vec4u,\r\n // a dashed stroke: the pattern\'s place at the reference point (dash units, hi and lo); a segment: where that point\r\n // lies (px along it from its drawn start); the place of the other side where the pattern starts again (a closed\r\n // polyline\'s first point, a circle\'s start; dash units)\r\n @location(4) @interpolate(flat) dash: vec4f,\r\n}\r\n\r\n// a visible record (cull.wgsl write): its place in the pool, the view or its insert\'s space (block.wgsl), whether it is an\r\n// insert\'s, the style its entities take by block, mirrored, its entry\'s epx offset, its first hatch row\r\nstruct Item {\r\n idx: u32,\r\n vw: View,\r\n blk: bool,\r\n ins: u32,\r\n mir: bool,\r\n eoff: u32,\r\n hb: u32,\r\n}\r\n\r\n// the visible record at place j of the visible list: v its word there (an insert\'s instance beside it in vinst)\r\nfn item_of(v: u32, j: u32) -> Item {\r\n var it: Item;\r\n it.idx = v;\r\n it.vw = view;\r\n it.blk = false;\r\n it.ins = 0u;\r\n it.mir = false;\r\n it.eoff = 0u;\r\n it.hb = 0u;\r\n if (v & ITEM_BLOCK) != 0u {\r\n let sp = spaces[vinst[j]];\r\n it.idx = v & 0x7fffffffu;\r\n it.vw = space_view(sp);\r\n it.blk = true;\r\n it.ins = sp.u.y;\r\n it.mir = (sp.u.x & SP_MIRROR) != 0u;\r\n it.eoff = sp.u.w;\r\n it.hb = sp.u.z;\r\n }\r\n return it;\r\n}\r\n\r\n// a record\'s style (block.wgsl style_in); a pattern in pixels inside an insert: its periods per block unit\r\nfn item_style(r: Record, it: Item) -> Style {\r\n var st = style_in(r.w.z - 1u, it.ins, it.blk);\r\n if it.blk && dash_count(st) > 0u && ((st.flags >> 20u) & 15u) != UNIT_DRAWING {\r\n st.kappa = kappa_scaled(st.kappa, it.vw.turn.xy);\r\n }\r\n return st;\r\n}\r\n\r\nfn unit_or_zero(v: vec2f) -> vec2f {\r\n let l = length(v);\r\n if l > 0.0 {\r\n return v / l;\r\n }\r\n return vec2f(0.0);\r\n}\r\n\r\n// how far the pixels a segment draws reach beyond an end along it (px; the smoothed edge\'s pixel comes on top): a cap\r\n// its half width (none for a butt cap); a join only at the segment\'s end, where it comes in: the join\'s piece\r\n// beyond the end belongs to it (both segments\' nearest point there is the vertex, and the incoming one wins), as far\r\n// as that piece\'s farthest corner: the miter point h tan(phi / 2) or the outgoing outer corner h sin(phi), a bevel\'s\r\n// corner h sin(phi), a circle h sin(phi) up to a right angle, then h. dir points out of the segment, nbdir along the\r\n// neighbour away from the end (a straight continuation: the same direction), c = cos(phi) of the turn\r\nfn end_reach(dir: vec2f, nbdir: vec2f, joined: bool, incoming: bool, h: f32, st: Style) -> f32 {\r\n if !joined {\r\n return select(h, 0.0, cap_of(st) == CAP_BUTT);\r\n }\r\n if !incoming {\r\n return 0.0;\r\n }\r\n let c = clamp(dot(dir, nbdir), -1.0, 1.0);\r\n let sn = sqrt(max(1.0 - c * c, 0.0));\r\n let j = join_of(st);\r\n if j == JOIN_MITER && miter_ok(c, st.miter) {\r\n return h * max(sn, sqrt(max(1.0 - c, 0.0) / max(1.0 + c, 1e-12)));\r\n }\r\n if j == JOIN_ROUND && c < 0.0 {\r\n return h;\r\n }\r\n return h * sn;\r\n}\r\n\r\n// a dashed segment: the pattern\'s place at its reference point (the camera centre\'s foot on its line, kept within the\r\n// segment) from the nearer end, in double-f32 (dash units); where that point lies along the segment from its drawn\r\n// start q0 (px); the place of the other side at a closed polyline\'s first point. r0, r1: rel2 of its ends, d its\r\n// direction in pixels; vw: the view or the insert\'s space (an insert\'s lengths and its pattern\'s periods are its block\'s units)\r\nfn seg_dash(idx: u32, r: Record, st: Style, r0: vec4f, r1: vec4f, q0: vec2f, d: vec2f, vw: View) -> vec4f {\r\n let ownu = bitcast<vec2f>(r.v.xy);\r\n let le = length_of(r);\r\n var ls = vec3f(0.0);\r\n if (r.w.w & F_FIRST) == 0u {\r\n ls = length_of(pool[idx - 1u]);\r\n }\r\n let lu = length(ownu);\r\n var du = vec2f(1.0, 0.0);\r\n if lu > 0.0 {\r\n du = ownu / lu;\r\n }\r\n // the foot\'s distance from the start along the segment (drawing units): (camera - start) . du\r\n let ta = dd_add(dd_mul_f(vec2f(-r0.x, -r0.z), du.x), dd_mul_f(vec2f(-r0.y, -r0.w), du.y));\r\n var base = ls;\r\n var t = vec2f(0.0);\r\n var rp = to_px(r0.xy, vw);\r\n if ta.x >= lu {\r\n base = le;\r\n rp = to_px(r1.xy, vw);\r\n } else if ta.x > 0.0 {\r\n // (the foot itself: near the camera, a small number out of large ones)\r\n let fx = dd_add(vec2f(r0.x, r0.z), dd_mul_f(ta, du.x));\r\n let fy = dd_add(vec2f(r0.y, r0.w), dd_mul_f(ta, du.y));\r\n rp = to_px(vec2f(fx.x + fx.y, fy.x + fy.y), vw);\r\n if ta.x <= 0.5 * lu {\r\n t = ta;\r\n } else {\r\n base = le;\r\n t = dd_add(dd_mul_f(vec2f(-r1.x, -r1.z), du.x), dd_mul_f(vec2f(-r1.y, -r1.w), du.y));\r\n }\r\n }\r\n let ph = dd_mul_f(periods(base, t, st.kappa, st.phase0), st.period);\r\n // the other side of a closed polyline\'s first point: its last record ends there, the length all around\r\n var other = 0.0;\r\n if (r.w.w & F_WRAP_PREV) != 0u {\r\n other = periods(length_of(pool[idx + r.w.y]), vec2f(0.0), st.kappa, st.phase0).x * st.period;\r\n } else if (r.w.w & F_WRAP_NEXT) != 0u {\r\n other = st.phase0 * st.period;\r\n }\r\n return vec4f(ph.x, ph.y, dot(rp - q0, d), other);\r\n}\r\n\r\n// the vertex shaders of both pipelines (lib.rs): vs for solid segments only, vs_full also for arcs and dash patterns\r\n// (apart, so that their code weighs nothing on the many solid segments)\r\n@vertex\r\nfn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VsOut {\r\n let l = lists[pc.layer + (ii >> 16u)];\r\n let j = (ii & 0xffffu) * BATCH + (vi >> 2u);\r\n if j >= l.y {\r\n return empty_out();\r\n }\r\n return vertex(vi, item_of(visible[l.x + j], l.x + j), l.z, false);\r\n}\r\n\r\n@vertex\r\nfn vs_full(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VsOut {\r\n let l = lists[pc.layer + (ii >> 16u)];\r\n let j = (ii & 0xffffu) * BATCH + (vi >> 2u);\r\n if j >= l.y {\r\n return empty_out();\r\n }\r\n return vertex(vi, item_of(visible[l.x + j], l.x + j), l.z, true);\r\n}\r\n\r\n// a quad of nothing: all four corners in one point\r\nfn empty_out() -> VsOut {\r\n var o: VsOut;\r\n o.pos = vec4f(2.0, 2.0, 0.5, 1.0);\r\n o.seg = vec4f(0.0);\r\n o.nb = vec4f(0.0);\r\n o.par = vec4f(0.0);\r\n o.misc = vec4u(0u);\r\n o.dash = vec4f(0.0);\r\n return o;\r\n}\r\n\r\n// a visible record\'s quad (it: the record, an insert\'s through its space; layer_t: its layer\'s transparency)\r\nfn vertex(vi: u32, it: Item, layer_t: u32, full: bool) -> VsOut {\r\n var o = empty_out();\r\n let idx = it.idx;\r\n let vw = it.vw;\r\n let r = pool[idx];\r\n let st = item_style(r, it);\r\n if kind_of_record(r) == K_ARC {\r\n // (a layer with arcs takes vs_full)\r\n if full {\r\n return vs_arc(vi, r, st, it, layer_t);\r\n }\r\n return o;\r\n }\r\n let h = half_width(st, vw);\r\n let half = 0.5 * vw.turn.zw;\r\n let r0 = rel2(r.a, vw);\r\n let r1 = rel2(r.b, vw);\r\n // pixels from the target\'s middle\r\n let p0 = to_px(r0.xy, vw);\r\n let p1 = to_px(r1.xy, vw);\r\n let cap = cap_of(st);\r\n let info = r.w.w;\r\n // the neighbours\' own vectors: the previous point minus the start, the next point minus the end (scene.rs: the bits\r\n // of this record\'s own vector where they meet)\r\n var nu = vec4f(0.0);\r\n if (info & F_PREV) != 0u {\r\n nu = vec4f(-bitcast<vec2f>(pool[select(idx - 1u, idx + r.w.y, (info & F_WRAP_PREV) != 0u)].v.xy), 0.0, 0.0);\r\n }\r\n if (info & F_NEXT) != 0u {\r\n nu = vec4f(nu.xy, bitcast<vec2f>(pool[select(idx + 1u, idx - r.w.y, (info & F_WRAP_NEXT) != 0u)].v.xy));\r\n }\r\n var sk = select(CAP, JOIN, (info & F_PREV) != 0u);\r\n var ek = select(CAP, JOIN, (info & F_NEXT) != 0u);\r\n let va = to_px(nu.xy, vw);\r\n let vb = to_px(nu.zw, vw);\r\n let own = to_px(bitcast<vec2f>(r.v.xy), vw);\r\n let len0 = length(p1 - p0);\r\n var d = unit_or_zero(own);\r\n if !any(d != vec2f(0.0)) {\r\n d = vec2f(1.0, 0.0);\r\n }\r\n // a point without a cap: nothing\r\n if len0 == 0.0 && cap == CAP_BUTT && sk == CAP && ek == CAP {\r\n return o;\r\n }\r\n let dashed = full && dash_count(st) > 0u;\r\n var e0 = end_reach(-d, unit_or_zero(va), sk == JOIN, false, h, st);\r\n var e1 = end_reach(d, unit_or_zero(vb), ek == JOIN, true, h, st);\r\n if dashed {\r\n // (a dash\'s cap near a vertex reaches into the neighbour\'s room; the pixel\'s owner draws it, its quad holds it)\r\n let ce = select(h, 0.0, cap == CAP_BUTT);\r\n e0 = max(e0, ce);\r\n e1 = max(e1, ce);\r\n }\r\n let m = max(h, max(e0, e1)) + 2.0;\r\n let lo = -half - vec2f(m);\r\n let hi = half + vec2f(m);\r\n // the line as base + s d, s from s0 to s1: the base its nearer end, or (both ends far) the foot of the centre\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 && max(hi.x, hi.y) < 0.25 * FAR;\r\n if far {\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 // clipped to the target and its margin (Liang-Barsky on base + s d)\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 o;\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 o;\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 sk = OPEN;\r\n }\r\n if sb < s1 || far {\r\n q1 = base + sb * d;\r\n ek = OPEN;\r\n }\r\n let len = length(q1 - q0);\r\n let ext0 = select(e0 + 1.0, 0.0, sk == OPEN);\r\n let ext1 = select(e1 + 1.0, 0.0, ek == OPEN);\r\n let w = h + 1.0;\r\n let n = vec2f(-d.y, d.x);\r\n let u = select(-ext0, len + ext1, (vi & 1u) != 0u);\r\n let v = select(-w, w, (vi & 2u) != 0u);\r\n let p = q0 + u * d + v * n;\r\n o.pos = vec4f(p.x / half.x, -p.y / half.y, 0.5, 1.0);\r\n o.seg = vec4f(q0 + half, q1 + half);\r\n o.nb = vec4f(va, vb);\r\n o.par = vec4f(h, st.miter, own);\r\n // (misc.z: the style whose pattern the fragment shader walks; misc.w: dash units per pixel of a pattern in units, the\r\n // block\'s inside an insert)\r\n o.misc = vec4u(sk | (ek << 2u) | (cap << 4u) | (join_of(st) << 8u) | ((info & 3u) << 12u), ink(st.color, st.r1.x, layer_t), dash_style(r.w.z - 1u, it.ins, it.blk),\r\n bitcast<u32>(vw.rest.w));\r\n if dashed {\r\n o.misc.x |= M_DASHED | select(0u, M_RESTART_S, (info & F_WRAP_PREV) != 0u) | select(0u, M_RESTART_E, (info & F_WRAP_NEXT) != 0u);\r\n o.dash = seg_dash(idx, r, st, r0, r1, q0, d, vw);\r\n }\r\n return o;\r\n}\r\n\r\n// the arc length (drawing units, double-f32) from a point x of the circle (rel2) to F (relative to the camera\'s\r\n// centre, drawing units), along a circle of radius rr (hi, lo): 2 r asin(c / 2r) of the chord c, asin by one Newton\r\n// step from f32 with the sine in double-f32 (the half angle stays below 0.1: a piece\'s half)\r\nfn arc_from(x: vec4f, fu: vec2f, rr: vec2f) -> vec2f {\r\n let cx = dd_add_f(vec2f(-x.x, -x.z), fu.x);\r\n let cy = dd_add_f(vec2f(-x.y, -x.w), fu.y);\r\n let c = dd_sqrt(dd_add(dd_mul(cx, cx), dd_mul(cy, cy)));\r\n let y = dd_div(c, dd_mul_f(rr, 2.0));\r\n // (asin by its series: the GPU\'s own asin and cos need not be exact, Vulkan allows 4096 ulp and 2^-11)\r\n let y2 = y.x * y.x;\r\n let t0 = y.x * (1.0 + y2 * (1.0 / 6.0 + y2 * (3.0 / 40.0 + y2 * (5.0 / 112.0))));\r\n let t2 = two_prod(t0, t0);\r\n let t3 = dd_mul_f(t2, t0);\r\n let t5 = dd_mul(t3, t2);\r\n var s = dd_add(dd_mul(t3, vec2f(-0.1666666716337204, 4.967053879312289e-09)), dd_mul(t5, vec2f(0.008333333767950535, -4.34617203337595e-10)));\r\n s = dd_add_f(s, t5.x * t2.x * (-1.0 / 5040.0 + t2.x / 362880.0));\r\n s = dd_add_f(s, t0);\r\n // (G6e3: both differences through opq, the first cancels exactly; regrouped, y.y and s.y were lost)\r\n let th = dd_add_f(vec2f(t0, 0.0), (opq(y.x - s.x) + opq(y.y - s.y)) / cos_p(t0));\r\n return dd_mul(dd_mul_f(th, 2.0), rr);\r\n}\r\n\r\n// sine and cosine exact also for small angles, which a huge circle\'s are (the GPU\'s own may be off by 2^-11 absolute,\r\n// Vulkan\'s bound, a million pixels at a radius of 1e13 px): their series below 0.5, beyond the GPU\'s\r\nfn sin_p(x: f32) -> f32 {\r\n if abs(x) < 0.5 {\r\n let x2 = x * x;\r\n return x * (1.0 - x2 / 6.0 * (1.0 - x2 / 20.0 * (1.0 - x2 / 42.0 * (1.0 - x2 / 72.0))));\r\n }\r\n return sin(x);\r\n}\r\n\r\n// atan2 exact also for the small angles of a huge circle (the GPU\'s own may be 4096 ulp off): the series of y / x below\r\n// a tenth\r\nfn atan2_p(y: f32, x: f32) -> f32 {\r\n if x > 0.0 && abs(y) < 0.1 * x {\r\n let z = y / x;\r\n let z2 = z * z;\r\n return z * (1.0 - z2 * (1.0 / 3.0 - z2 * (1.0 / 5.0 - z2 * (1.0 / 7.0 - z2 / 9.0))));\r\n }\r\n return atan2(y, x);\r\n}\r\n\r\nfn cos_p(x: f32) -> f32 {\r\n if abs(x) < 0.5 {\r\n let x2 = x * x;\r\n return 1.0 - x2 / 2.0 * (1.0 - x2 / 12.0 * (1.0 - x2 / 30.0 * (1.0 - x2 / 56.0)));\r\n }\r\n return cos(x);\r\n}\r\n\r\n// an arc\'s piece (the head above): its quad, F and the radius, its ends, its dash pattern\'s place; it: the record (an\r\n// insert\'s through its space: a mirrored one turns the other way), layer_t: its layer\'s transparency\r\nfn vs_arc(vi: u32, r: Record, st: Style, it: Item, layer_t: u32) -> VsOut {\r\n let idx = it.idx;\r\n let vw = it.vw;\r\n var o: VsOut;\r\n o.pos = vec4f(2.0, 2.0, 0.5, 1.0);\r\n o.seg = vec4f(0.0);\r\n o.nb = vec4f(0.0);\r\n o.par = vec4f(0.0);\r\n o.misc = vec4u(0u);\r\n o.dash = vec4f(0.0);\r\n let h = half_width(st, vw);\r\n let half = 0.5 * vw.turn.zw;\r\n let cap = cap_of(st);\r\n let info = r.w.w;\r\n let pa = pool[idx - 1u];\r\n let rr = vec2f(bitcast<f32>(r.v.x), bitcast<f32>(r.v.y));\r\n let rc = rel2(r.a, vw);\r\n // the camera\'s distance from the circle: (|v|^2 - r^2) / (|v| + r) for v = camera - centre, the difference of the\r\n // squares in double-f32 (both may be 1e14 and more, the distance a thousandth)\r\n let vx = vec2f(-rc.x, -rc.z);\r\n let vy = vec2f(-rc.y, -rc.w);\r\n let v2 = dd_add(dd_mul(vx, vx), dd_mul(vy, vy));\r\n let num = dd_add(v2, -dd_mul(rr, rr));\r\n let vl = sqrt(v2.x);\r\n let d0 = (num.x + num.y) / (vl + rr.x);\r\n var uu = vec2f(1.0, 0.0);\r\n if vl > 0.0 {\r\n uu = vec2f(vx.x, vy.x) / vl;\r\n }\r\n // in pixels: F, the outward unit there, the radius; the turn\'s sign (y points down here: a counter-clockwise arc\r\n // turns the other way), the arc\'s direction at F\r\n let up = unit_or_zero(to_px(uu, vw));\r\n let fu = -d0 * uu;\r\n let f = to_px(fu, vw);\r\n let rp = (rr.x + rr.y) * vw.rest.z;\r\n let orient = select(1.0, -1.0, (info & F_CCW) != 0u) * select(1.0, -1.0, it.mir);\r\n let tf = orient * vec2f(-up.y, up.x);\r\n let ra = rel2(pa.b, vw);\r\n let ap = to_px(ra.xy, vw);\r\n let bp = to_px(rel2(r.b, vw).xy, vw);\r\n // the piece\'s ends as angles from F along the arc\r\n let wa = ap - f;\r\n let wb = bp - f;\r\n let ba = atan2_p(dot(wa, tf), rp + dot(wa, up));\r\n var bb = atan2_p(dot(wb, tf), rp + dot(wb, up));\r\n if bb < ba {\r\n bb += 6.2831853;\r\n }\r\n // a cap\'s reach beyond the arc\'s own ends\r\n let ce = select(h, 0.0, cap == CAP_BUTT);\r\n var b0 = ba;\r\n var b1 = bb;\r\n let mg = half + vec2f(h + ce + 3.0);\r\n if rp > length(mg) + h + 1.0 {\r\n // a circle larger than the target and its margin: only the angles at which its ring can meet them (whatever of it\r\n // lies there is within a quarter turn of F, the point nearest the target\'s middle: asin of the projection along\r\n // the arc\'s direction at F)\r\n var xmin = 3.4e38;\r\n var xmax = -3.4e38;\r\n for (var k = 0u; k < 4u; k++) {\r\n let x = dot(vec2f(select(-mg.x, mg.x, (k & 1u) != 0u), select(-mg.y, mg.y, (k & 2u) != 0u)) - f, tf);\r\n xmin = min(xmin, x);\r\n xmax = max(xmax, x);\r\n }\r\n let ri = max(rp - h - 1.0, 1.0);\r\n let ro = rp + h + 1.0;\r\n b0 = max(b0, min(asin(clamp(xmin / ri, -1.0, 1.0)), asin(clamp(xmin / ro, -1.0, 1.0))));\r\n b1 = min(b1, max(asin(clamp(xmax / ri, -1.0, 1.0)), asin(clamp(xmax / ro, -1.0, 1.0))));\r\n if b0 > b1 {\r\n return o;\r\n }\r\n }\r\n // the quad: from the chord at the inner radius to the tangent at the outer one, pushed out along the arc by a pixel\r\n // at its ends (by a cap\'s reach at the arc\'s own ends): radial offsets from the circle, exact for any radius\r\n let al = 0.5 * (b1 - b0);\r\n let sal = sin_p(0.5 * al);\r\n let dout = ((h + 1.0) + 2.0 * rp * sal * sal) / cos_p(al);\r\n let din = -min(h + 1.0, rp);\r\n let e0 = select(1.0, 1.0 + ce, (info & F_CAP_A) != 0u && b0 == ba);\r\n let e1 = select(1.0, 1.0 + ce, (info & F_CAP_B) != 0u && b1 == bb);\r\n let be = select(b0, b1, (vi & 1u) != 0u);\r\n let dr = select(din, dout, (vi & 2u) != 0u);\r\n let sb = sin_p(be);\r\n let cb = cos_p(be);\r\n let sh = sin_p(0.5 * be);\r\n // a point at angle be and radius rp + dr, relative to F; then along the arc\'s direction there\r\n var p = f + (dr * cb - 2.0 * rp * sh * sh) * up + (rp + dr) * sb * tf;\r\n p += select(-e0, e1, (vi & 1u) != 0u) * (cb * tf - sb * up);\r\n o.pos = vec4f(p.x / half.x, -p.y / half.y, 0.5, 1.0);\r\n o.seg = vec4f(f + half, up);\r\n o.nb = vec4f(ap + half, bp + half);\r\n o.par = vec4f(h, rp, orient, 0.0);\r\n o.misc = vec4u(M_ARC | (cap << 4u) | ((info & 3u) << 12u), ink(st.color, st.r1.x, layer_t), dash_style(r.w.z - 1u, it.ins, it.blk), bitcast<u32>(vw.rest.w));\r\n if dash_count(st) > 0u {\r\n o.misc.x |= M_DASHED | select(0u, M_RESTART_S, (info & F_RESTART_A) != 0u) | select(0u, M_RESTART_E, (info & F_RESTART_B) != 0u);\r\n // the reference point: F kept within the piece; the pattern\'s place there from the nearer end\r\n let br = clamp(0.0, ba, bb);\r\n let la = length_of(pa);\r\n let lb = length_of(r);\r\n var base = la;\r\n var t = vec2f(0.0);\r\n if br >= bb {\r\n base = lb;\r\n } else if br > ba {\r\n // (beyond ARC_FAR pixels f32\'s angle is too coarse: the chord in double-f32)\r\n let ru = rr.x + rr.y;\r\n let near_a = -ba <= bb;\r\n let ang = select(bb, -ba, near_a);\r\n if rp * ang > ARC_FAR {\r\n t = arc_from(select(rel2(r.b, vw), ra, near_a), fu, rr);\r\n } else {\r\n t = vec2f(ru * ang, 0.0);\r\n }\r\n if !near_a {\r\n base = lb;\r\n t = -t;\r\n }\r\n }\r\n let ph = dd_mul_f(periods(base, t, st.kappa, st.phase0), st.period);\r\n var other = 0.0;\r\n if (info & F_RESTART_A) != 0u {\r\n other = periods(length_of(pool[idx + r.w.y]), vec2f(0.0), st.kappa, st.phase0).x * st.period;\r\n } else if (info & F_RESTART_B) != 0u {\r\n other = st.phase0 * st.period;\r\n }\r\n o.par.w = br;\r\n o.dash = vec4f(ph.x, ph.y, 0.0, other);\r\n }\r\n return o;\r\n}\r\n\r\n// the share of the pixel (a unit square around its centre) inside a half plane whose edge lies s from the centre (s >\r\n// 0: the centre is outside), m the edge\'s outward unit normal: t = dot(x - centre, m) over the square is the sum of\r\n// two uniform parts, |m.x| and |m.y| wide; this is its distribution at -s, exact for a straight edge\r\nfn cov(s: f32, m: vec2f) -> f32 {\r\n let a = min(abs(m.x), abs(m.y));\r\n let b = max(abs(m.x), abs(m.y));\r\n let x = -s;\r\n let outer = 0.5 * (a + b);\r\n if x >= outer {\r\n return 1.0;\r\n }\r\n if x <= -outer {\r\n return 0.0;\r\n }\r\n if a < 1e-4 {\r\n return clamp(0.5 + x / b, 0.0, 1.0);\r\n }\r\n let inner = 0.5 * (b - a);\r\n if x < -inner {\r\n let t = x + outer;\r\n return t * t / (2.0 * a * b);\r\n }\r\n if x > inner {\r\n let t = outer - x;\r\n return 1.0 - t * t / (2.0 * a * b);\r\n }\r\n return 0.5 + x / b;\r\n}\r\n\r\n// (written by tools/gen_area6.py from the description below; unrolled on purpose: loops over planes and arrays of them cost a\r\n// GPU a hundred times more here)\r\n//\r\n// the area of the pixel (the unit square around its centre) inside up to six half planes (m, s; unused ones ALL):\r\n// none cutting it 1, one exactly by cov, the strip\'s two alone by cov + cov - 1, more by Green\'s theorem: the\r\n// boundary is pieces of the square\'s sides and of the cutting planes\' lines, each a segment narrowed to all the\r\n// others, and the area half the sum of cross(start, end) over them, counter-clockwise; two lines on one line: one\r\n// bounds the region when they face the same way, none when they face each other\r\nfn area6(p0: vec3f, p1: vec3f, p2: vec3f, p3: vec3f, p4: vec3f, p5: vec3f) -> f32 {\r\n let k0 = kind_of(p0);\r\n let k1 = kind_of(p1);\r\n let k2 = kind_of(p2);\r\n let k3 = kind_of(p3);\r\n let k4 = kind_of(p4);\r\n let k5 = kind_of(p5);\r\n if k0 == 2u || k1 == 2u || k2 == 2u || k3 == 2u || k4 == 2u || k5 == 2u {\r\n return 0.0;\r\n }\r\n var n = 0u;\r\n var c0 = ALL;\r\n var c1 = ALL;\r\n var c2 = ALL;\r\n var c3 = ALL;\r\n var c4 = ALL;\r\n var c5 = ALL;\r\n if k0 == 1u {\r\n c0 = select(c0, p0, n == 0u);\r\n c1 = select(c1, p0, n == 1u);\r\n c2 = select(c2, p0, n == 2u);\r\n c3 = select(c3, p0, n == 3u);\r\n c4 = select(c4, p0, n == 4u);\r\n c5 = select(c5, p0, n == 5u);\r\n n += 1u;\r\n }\r\n if k1 == 1u {\r\n c0 = select(c0, p1, n == 0u);\r\n c1 = select(c1, p1, n == 1u);\r\n c2 = select(c2, p1, n == 2u);\r\n c3 = select(c3, p1, n == 3u);\r\n c4 = select(c4, p1, n == 4u);\r\n c5 = select(c5, p1, n == 5u);\r\n n += 1u;\r\n }\r\n if k2 == 1u {\r\n c0 = select(c0, p2, n == 0u);\r\n c1 = select(c1, p2, n == 1u);\r\n c2 = select(c2, p2, n == 2u);\r\n c3 = select(c3, p2, n == 3u);\r\n c4 = select(c4, p2, n == 4u);\r\n c5 = select(c5, p2, n == 5u);\r\n n += 1u;\r\n }\r\n if k3 == 1u {\r\n c0 = select(c0, p3, n == 0u);\r\n c1 = select(c1, p3, n == 1u);\r\n c2 = select(c2, p3, n == 2u);\r\n c3 = select(c3, p3, n == 3u);\r\n c4 = select(c4, p3, n == 4u);\r\n c5 = select(c5, p3, n == 5u);\r\n n += 1u;\r\n }\r\n if k4 == 1u {\r\n c0 = select(c0, p4, n == 0u);\r\n c1 = select(c1, p4, n == 1u);\r\n c2 = select(c2, p4, n == 2u);\r\n c3 = select(c3, p4, n == 3u);\r\n c4 = select(c4, p4, n == 4u);\r\n c5 = select(c5, p4, n == 5u);\r\n n += 1u;\r\n }\r\n if k5 == 1u {\r\n c0 = select(c0, p5, n == 0u);\r\n c1 = select(c1, p5, n == 1u);\r\n c2 = select(c2, p5, n == 2u);\r\n c3 = select(c3, p5, n == 3u);\r\n c4 = select(c4, p5, n == 4u);\r\n c5 = select(c5, p5, n == 5u);\r\n n += 1u;\r\n }\r\n if n == 0u {\r\n return 1.0;\r\n }\r\n if n == 1u {\r\n return cov(c0.z, c0.xy);\r\n }\r\n if n == 2u && dot(c0.xy, c1.xy) < -0.99999 {\r\n return max(cov(c0.z, c0.xy) + cov(c1.z, c1.xy) - 1.0, 0.0);\r\n }\r\n var a = 0.0;\r\n // the square\'s sides\r\n {\r\n let sp = vec2f(-0.5, -0.5);\r\n let sd = vec2f(1.0, 0.0);\r\n var t = vec2f(0.0, 1.0);\r\n if n > 0u {\r\n t = narrow(sp, sd, c0, t);\r\n }\r\n if n > 1u {\r\n t = narrow(sp, sd, c1, t);\r\n }\r\n if n > 2u {\r\n t = narrow(sp, sd, c2, t);\r\n }\r\n if n > 3u {\r\n t = narrow(sp, sd, c3, t);\r\n }\r\n if n > 4u {\r\n t = narrow(sp, sd, c4, t);\r\n }\r\n if n > 5u {\r\n t = narrow(sp, sd, c5, t);\r\n }\r\n a += piece(sp, sd, t);\r\n }\r\n {\r\n let sp = vec2f(0.5, -0.5);\r\n let sd = vec2f(0.0, 1.0);\r\n var t = vec2f(0.0, 1.0);\r\n if n > 0u {\r\n t = narrow(sp, sd, c0, t);\r\n }\r\n if n > 1u {\r\n t = narrow(sp, sd, c1, t);\r\n }\r\n if n > 2u {\r\n t = narrow(sp, sd, c2, t);\r\n }\r\n if n > 3u {\r\n t = narrow(sp, sd, c3, t);\r\n }\r\n if n > 4u {\r\n t = narrow(sp, sd, c4, t);\r\n }\r\n if n > 5u {\r\n t = narrow(sp, sd, c5, t);\r\n }\r\n a += piece(sp, sd, t);\r\n }\r\n {\r\n let sp = vec2f(0.5, 0.5);\r\n let sd = vec2f(-1.0, 0.0);\r\n var t = vec2f(0.0, 1.0);\r\n if n > 0u {\r\n t = narrow(sp, sd, c0, t);\r\n }\r\n if n > 1u {\r\n t = narrow(sp, sd, c1, t);\r\n }\r\n if n > 2u {\r\n t = narrow(sp, sd, c2, t);\r\n }\r\n if n > 3u {\r\n t = narrow(sp, sd, c3, t);\r\n }\r\n if n > 4u {\r\n t = narrow(sp, sd, c4, t);\r\n }\r\n if n > 5u {\r\n t = narrow(sp, sd, c5, t);\r\n }\r\n a += piece(sp, sd, t);\r\n }\r\n {\r\n let sp = vec2f(-0.5, 0.5);\r\n let sd = vec2f(0.0, -1.0);\r\n var t = vec2f(0.0, 1.0);\r\n if n > 0u {\r\n t = narrow(sp, sd, c0, t);\r\n }\r\n if n > 1u {\r\n t = narrow(sp, sd, c1, t);\r\n }\r\n if n > 2u {\r\n t = narrow(sp, sd, c2, t);\r\n }\r\n if n > 3u {\r\n t = narrow(sp, sd, c3, t);\r\n }\r\n if n > 4u {\r\n t = narrow(sp, sd, c4, t);\r\n }\r\n if n > 5u {\r\n t = narrow(sp, sd, c5, t);\r\n }\r\n a += piece(sp, sd, t);\r\n }\r\n // the cutting planes\' lines\r\n if n > 0u {\r\n let lp = -c0.z * c0.xy;\r\n let ld = vec2f(-c0.y, c0.x);\r\n var t = vec2f(-1.0, 1.0);\r\n t = narrow(lp, ld, vec3f(1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(-1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, 1.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, -1.0, -0.5), t);\r\n if n > 1u {\r\n t = narrow_line(lp, ld, c0.xy, c1, false, t);\r\n }\r\n if n > 2u {\r\n t = narrow_line(lp, ld, c0.xy, c2, false, t);\r\n }\r\n if n > 3u {\r\n t = narrow_line(lp, ld, c0.xy, c3, false, t);\r\n }\r\n if n > 4u {\r\n t = narrow_line(lp, ld, c0.xy, c4, false, t);\r\n }\r\n if n > 5u {\r\n t = narrow_line(lp, ld, c0.xy, c5, false, t);\r\n }\r\n a += piece(lp, ld, t);\r\n }\r\n if n > 1u {\r\n let lp = -c1.z * c1.xy;\r\n let ld = vec2f(-c1.y, c1.x);\r\n var t = vec2f(-1.0, 1.0);\r\n t = narrow(lp, ld, vec3f(1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(-1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, 1.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, -1.0, -0.5), t);\r\n if n > 0u {\r\n t = narrow_line(lp, ld, c1.xy, c0, true, t);\r\n }\r\n if n > 2u {\r\n t = narrow_line(lp, ld, c1.xy, c2, false, t);\r\n }\r\n if n > 3u {\r\n t = narrow_line(lp, ld, c1.xy, c3, false, t);\r\n }\r\n if n > 4u {\r\n t = narrow_line(lp, ld, c1.xy, c4, false, t);\r\n }\r\n if n > 5u {\r\n t = narrow_line(lp, ld, c1.xy, c5, false, t);\r\n }\r\n a += piece(lp, ld, t);\r\n }\r\n if n > 2u {\r\n let lp = -c2.z * c2.xy;\r\n let ld = vec2f(-c2.y, c2.x);\r\n var t = vec2f(-1.0, 1.0);\r\n t = narrow(lp, ld, vec3f(1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(-1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, 1.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, -1.0, -0.5), t);\r\n if n > 0u {\r\n t = narrow_line(lp, ld, c2.xy, c0, true, t);\r\n }\r\n if n > 1u {\r\n t = narrow_line(lp, ld, c2.xy, c1, true, t);\r\n }\r\n if n > 3u {\r\n t = narrow_line(lp, ld, c2.xy, c3, false, t);\r\n }\r\n if n > 4u {\r\n t = narrow_line(lp, ld, c2.xy, c4, false, t);\r\n }\r\n if n > 5u {\r\n t = narrow_line(lp, ld, c2.xy, c5, false, t);\r\n }\r\n a += piece(lp, ld, t);\r\n }\r\n if n > 3u {\r\n let lp = -c3.z * c3.xy;\r\n let ld = vec2f(-c3.y, c3.x);\r\n var t = vec2f(-1.0, 1.0);\r\n t = narrow(lp, ld, vec3f(1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(-1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, 1.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, -1.0, -0.5), t);\r\n if n > 0u {\r\n t = narrow_line(lp, ld, c3.xy, c0, true, t);\r\n }\r\n if n > 1u {\r\n t = narrow_line(lp, ld, c3.xy, c1, true, t);\r\n }\r\n if n > 2u {\r\n t = narrow_line(lp, ld, c3.xy, c2, true, t);\r\n }\r\n if n > 4u {\r\n t = narrow_line(lp, ld, c3.xy, c4, false, t);\r\n }\r\n if n > 5u {\r\n t = narrow_line(lp, ld, c3.xy, c5, false, t);\r\n }\r\n a += piece(lp, ld, t);\r\n }\r\n if n > 4u {\r\n let lp = -c4.z * c4.xy;\r\n let ld = vec2f(-c4.y, c4.x);\r\n var t = vec2f(-1.0, 1.0);\r\n t = narrow(lp, ld, vec3f(1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(-1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, 1.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, -1.0, -0.5), t);\r\n if n > 0u {\r\n t = narrow_line(lp, ld, c4.xy, c0, true, t);\r\n }\r\n if n > 1u {\r\n t = narrow_line(lp, ld, c4.xy, c1, true, t);\r\n }\r\n if n > 2u {\r\n t = narrow_line(lp, ld, c4.xy, c2, true, t);\r\n }\r\n if n > 3u {\r\n t = narrow_line(lp, ld, c4.xy, c3, true, t);\r\n }\r\n if n > 5u {\r\n t = narrow_line(lp, ld, c4.xy, c5, false, t);\r\n }\r\n a += piece(lp, ld, t);\r\n }\r\n if n > 5u {\r\n let lp = -c5.z * c5.xy;\r\n let ld = vec2f(-c5.y, c5.x);\r\n var t = vec2f(-1.0, 1.0);\r\n t = narrow(lp, ld, vec3f(1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(-1.0, 0.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, 1.0, -0.5), t);\r\n t = narrow(lp, ld, vec3f(0.0, -1.0, -0.5), t);\r\n if n > 0u {\r\n t = narrow_line(lp, ld, c5.xy, c0, true, t);\r\n }\r\n if n > 1u {\r\n t = narrow_line(lp, ld, c5.xy, c1, true, t);\r\n }\r\n if n > 2u {\r\n t = narrow_line(lp, ld, c5.xy, c2, true, t);\r\n }\r\n if n > 3u {\r\n t = narrow_line(lp, ld, c5.xy, c3, true, t);\r\n }\r\n if n > 4u {\r\n t = narrow_line(lp, ld, c5.xy, c4, true, t);\r\n }\r\n a += piece(lp, ld, t);\r\n }\r\n return clamp(0.5 * a, 0.0, 1.0);\r\n}\r\n\r\n// a half plane with everything inside: an unused place\r\nconst ALL: vec3f = vec3f(1.0, 0.0, -1e30);\r\n// how close to a pixel\'s corner a half plane\'s edge must come to count as cutting it (a line along a pixel\'s side by a\r\n// rounding error does not)\r\nconst EDGE_EPS: f32 = 1e-5;\r\n\r\n// what a half plane does to the pixel: 0 holds it all, 1 cuts it, 2 holds nothing of it\r\nfn kind_of(p: vec3f) -> u32 {\r\n let r = 0.5 * (abs(p.x) + abs(p.y)) - EDGE_EPS;\r\n if p.z >= r {\r\n return 2u;\r\n }\r\n if p.z > -r {\r\n return 1u;\r\n }\r\n return 0u;\r\n}\r\n\r\n// a segment p + t d (t from t.x to t.y) narrowed to the half plane dot(x, m) + s <= 0 (pl); empty: t.x >= t.y\r\nfn narrow(p: vec2f, d: vec2f, pl: vec3f, t: vec2f) -> vec2f {\r\n let num = dot(p, pl.xy) + pl.z;\r\n let den = dot(d, pl.xy);\r\n if den == 0.0 {\r\n if num > 0.0 {\r\n return vec2f(1.0, 0.0);\r\n }\r\n return t;\r\n }\r\n let tc = -num / den;\r\n if den > 0.0 {\r\n return vec2f(t.x, min(t.y, tc));\r\n }\r\n return vec2f(max(t.x, tc), t.y);\r\n}\r\n\r\n// the same for a plane\'s own line (normal m) against another plane o: on one line, it bounds the region only when it\r\n// faces o\'s way and comes first (earlier false), never when they face each other\r\nfn narrow_line(p: vec2f, d: vec2f, m: vec2f, o: vec3f, earlier: bool, t: vec2f) -> vec2f {\r\n if abs(dot(d, o.xy)) < 1e-6 && abs(dot(p, o.xy) + o.z) < 1e-5 {\r\n if dot(m, o.xy) < 0.0 || earlier {\r\n return vec2f(1.0, 0.0);\r\n }\r\n return t;\r\n }\r\n return narrow(p, d, o, t);\r\n}\r\n\r\n// cross(start, end) of a boundary piece p + t d, t from t.x to t.y (0 when empty)\r\nfn piece(p: vec2f, d: vec2f, t: vec2f) -> f32 {\r\n if t.y <= t.x {\r\n return 0.0;\r\n }\r\n let a = p + t.x * d;\r\n let b = p + t.y * d;\r\n return a.x * b.y - b.x * a.y;\r\n}\r\n\r\n// the tangent of the circle of radius hw around c at pixel q, as a half plane\r\nfn tangent(q: vec2f, c: vec2f, hw: f32) -> vec3f {\r\n let r = q - c;\r\n let l = length(r);\r\n var m = vec2f(1.0, 0.0);\r\n if l > 1e-6 {\r\n m = r / l;\r\n }\r\n return vec3f(m, l - hw);\r\n}\r\n\r\n// the line at right angles through point c with the inside towards -dir (the half plane dot(x - c, dir) <= 0)\r\nfn before(q: vec2f, c: vec2f, dir: vec2f) -> vec3f {\r\n return vec3f(dir, dot(q - c, dir));\r\n}\r\n\r\n// a cap at end point c, dir pointing out of the segment: the line that ends its rectangle (a square cap half a width\r\n// further), and (a round cap) the half circle beyond it as its own piece\r\nfn cap_line(q: vec2f, c: vec2f, dir: vec2f, cap: u32, h: f32) -> vec3f {\r\n let p = before(q, c, dir);\r\n return select(p, p - vec3f(0.0, 0.0, h), cap == CAP_SQUARE);\r\n}\r\n\r\n// a join at vertex v between the segment coming in (din) and the one going out (dout), unit directions\r\nstruct Join {\r\n v: vec2f,\r\n din: vec2f,\r\n dout: vec2f,\r\n // the bisector\'s normal: the incoming tile is where dot(q - v, m) <= 0\r\n m: vec2f,\r\n // the bisector of the outer normals, along which a bevel\'s line lies off from v\r\n w: vec2f,\r\n off: f32,\r\n // JOIN_MITER (within its limit), JOIN_ROUND, JOIN_BEVEL (also a miter beyond its limit)\r\n kind: u32,\r\n // the bisector\'s chord reaches h tan(phi / 2) along each segment from v\r\n reach: f32,\r\n}\r\n\r\nfn make_join(v: vec2f, din: vec2f, dout: vec2f, kind: u32, limit: f32, h: f32) -> Join {\r\n var j: Join;\r\n j.v = v;\r\n j.din = din;\r\n j.dout = dout;\r\n let c = clamp(dot(din, dout), -1.0, 1.0);\r\n // (a turn to the left of din, or back: the outer side is the right one)\r\n let left = din.x * dout.y - din.y * dout.x >= 0.0;\r\n let s = din + dout;\r\n let ls = length(s);\r\n // (a turn back: the centre line splits the two strips, the incoming tile on the outer side)\r\n j.m = select(vec2f(-din.y, din.x), s / ls, ls > 1e-6);\r\n j.kind = kind;\r\n if kind == JOIN_MITER && !miter_ok(c, limit) {\r\n j.kind = JOIN_BEVEL;\r\n }\r\n let sg = select(1.0, -1.0, left);\r\n let so = sg * (vec2f(-din.y, din.x) + vec2f(-dout.y, dout.x));\r\n let lo = length(so);\r\n j.w = select(din, so / lo, lo > 1e-6);\r\n j.off = 0.5 * h * lo;\r\n j.reach = h * sqrt(max(1.0 - c, 0.0) / max(1.0 + c, 1e-12));\r\n return j;\r\n}\r\n\r\n// a join\'s bisector as a half plane: the incoming tile\'s side, or the outgoing one\'s\r\nfn bisector(q: vec2f, j: Join, incoming: bool) -> vec3f {\r\n let b = vec3f(j.m, dot(q - j.v, j.m));\r\n return select(-b, b, incoming);\r\n}\r\n\r\n// a join\'s outer cut of both tiles: a bevel\'s line (else ALL)\r\nfn bevel(q: vec2f, j: Join) -> vec3f {\r\n return select(ALL, vec3f(j.w, dot(q - j.v, j.w) - j.off), j.kind == JOIN_BEVEL);\r\n}\r\n\r\n// a join\'s piece beyond both rectangles\' ends: a miter\'s quadrilateral, a bevel\'s triangle, a round join\'s sector\r\nstruct Planes4 {\r\n a: vec3f,\r\n b: vec3f,\r\n c: vec3f,\r\n d: vec3f,\r\n}\r\n\r\nfn wedge(q: vec2f, j: Join, h: f32) -> Planes4 {\r\n let r = q - j.v;\r\n var w3 = vec3f(j.w, dot(r, j.w) - j.off);\r\n var w4 = ALL;\r\n if j.kind == JOIN_ROUND {\r\n w3 = tangent(q, j.v, h);\r\n } else if j.kind == JOIN_MITER {\r\n let sg = select(1.0, -1.0, j.din.x * j.dout.y - j.din.y * j.dout.x >= 0.0);\r\n let oi = sg * vec2f(-j.din.y, j.din.x);\r\n let oo = sg * vec2f(-j.dout.y, j.dout.x);\r\n w3 = vec3f(oi, dot(r, oi) - h);\r\n w4 = vec3f(oo, dot(r, oo) - h);\r\n }\r\n return Planes4(vec3f(-j.din, -dot(r, j.din)), vec3f(j.dout, dot(r, j.dout)), w3, w4);\r\n}\r\n\r\n// the squared distance of a point r (relative to an end of a segment) from the segment from there along w; both\r\n// segments at a join compute it from the same bits (r from the vertex, w from vectors the CPU rounded once)\r\nfn dist_seg(r: vec2f, w: vec2f) -> f32 {\r\n let ww = dot(w, w);\r\n var t = 0.0;\r\n if ww > 0.0 {\r\n t = clamp(dot(r, w) / ww, 0.0, 1.0);\r\n }\r\n let x = r - t * w;\r\n return dot(x, x);\r\n}\r\n\r\n// the strip of a segment through point o along d: its two lines\r\nfn strip_l(q: vec2f, o: vec2f, d: vec2f, h: f32) -> vec3f {\r\n let n = vec2f(-d.y, d.x);\r\n return vec3f(n, dot(q - o, n) - h);\r\n}\r\n\r\nfn strip_r(q: vec2f, o: vec2f, d: vec2f, h: f32) -> vec3f {\r\n let n = vec2f(-d.y, d.x);\r\n return vec3f(-n, -dot(q - o, n) - h);\r\n}\r\n\r\n// the pieces of the stroke near a pixel (the fragment shader\'s mask): this segment\'s rectangle, the previous and the\r\n// next one\'s, round joins\' circles beyond the lines at right angles (this side and the neighbour\'s of each vertex), the\r\n// joins\' pieces of the union, the rectangles meeting (taken away), round caps\' half circles (own ends, the neighbours\'\r\n// far ends)\r\nconst S_OWN: u32 = 1u;\r\nconst S_PREV: u32 = 2u;\r\nconst S_NEXT: u32 = 4u;\r\nconst S_SECT_S_OWN: u32 = 8u;\r\nconst S_SECT_E_OWN: u32 = 16u;\r\nconst S_SECT_S_PREV: u32 = 32u;\r\nconst S_SECT_E_NEXT: u32 = 64u;\r\nconst S_WEDGE_S: u32 = 128u;\r\nconst S_WEDGE_E: u32 = 256u;\r\nconst S_X_PREV: u32 = 512u;\r\nconst S_X_NEXT: u32 = 1024u;\r\nconst S_HD_S: u32 = 2048u;\r\nconst S_HD_E: u32 = 4096u;\r\nconst S_HD_A: u32 = 8192u;\r\nconst S_HD_B: u32 = 16384u;\r\n\r\n// how a dash side\'s end ends (pieces): open (clipped by the target: no cut), cut (a join where the dash runs on), with\r\n// the style\'s cap (a dash\'s end, the polyline\'s or arc\'s own end)\r\nconst E_OPEN: u32 = 0u;\r\nconst E_CUT: u32 = 1u;\r\nconst E_CAP: u32 = 2u;\r\n\r\n// the dashes of a pattern near a point\r\nstruct Ivals {\r\n n: u32,\r\n i0: vec2f,\r\n i1: vec2f,\r\n i2: vec2f,\r\n}\r\n\r\n// where entry j of a style\'s dash pattern ends, from the period\'s start (dash units)\r\n// (the fragment shader reads a style\'s fields from the buffer, by its index: a copy of the struct with its array of\r\n// ends indexed at run time would lie in local memory, and every pixel would pay for it)\r\nfn dash_end(si: u32, j: u32) -> f32 {\r\n return styles[si].ends[j >> 2u][j & 3u];\r\n}\r\n\r\nfn dash_n(si: u32) -> u32 {\r\n return (styles[si].flags >> 12u) & 31u;\r\n}\r\n\r\n// the dashes (on entries) of a style\'s pattern within w px of a point at its place xh + xl (dash units), as px along\r\n// the stroke from the point (sc dash units per px), at most three. Relative to the point, so that the ends near it are\r\n// exact also when the period is a million pixels (xh a place in the period, xl a small offset). From the entry that\r\n// holds the place w px before the point, forward, each entry\'s start carried along: no index j - 1 and no step back.\r\n// (Stepping back from the point\'s entry as j = select(j - 1u, n - 1u, j == 0u), then reading the entry\'s length with a\r\n// test j == 0, Mesa\'s Intel compiler (26.0, HD 610 and Iris Xe) took that test for the step\'s own j == 0: a dash\r\n// shorter than its gap lost the pixels it covers whole, docs/RENDER2D.md)\r\nfn ivals(si: u32, xh: f32, xl: f32, sc: f32, w: f32) -> Ivals {\r\n var o: Ivals;\r\n o.n = 0u;\r\n o.i0 = vec2f(0.0);\r\n o.i1 = vec2f(0.0);\r\n o.i2 = vec2f(0.0);\r\n let n = dash_n(si);\r\n let p = styles[si].period;\r\n let kp = floor((xh + xl) / p);\r\n let x0 = xh - kp * p;\r\n let wd = w * sc;\r\n // the entry that holds the place wd before the point: the first whose end lies beyond it, in the period before when\r\n // the period starts after it; compared relative to the point (the place x0 + xl itself is rounded to the period\'s\r\n // size), and where that entry starts in the period\r\n var back = 0.0;\r\n if (0.0 - x0) - xl > -wd {\r\n back = p;\r\n }\r\n var j = 0u;\r\n var cs = 0.0;\r\n loop {\r\n if j + 1u >= n {\r\n break;\r\n }\r\n let c1 = dash_end(si, j);\r\n if ((c1 - back) - x0) - xl > -wd {\r\n break;\r\n }\r\n cs = c1;\r\n j += 1u;\r\n }\r\n var start = ((cs - back) - x0) - xl;\r\n var g = 0u;\r\n loop {\r\n if start > wd || g >= 32u || o.n >= 3u {\r\n break;\r\n }\r\n let ce = dash_end(si, j);\r\n let e = ce - cs;\r\n if (j & 1u) == 0u && start + e >= -wd {\r\n let v = vec2f(start, start + e) / sc;\r\n if o.n == 0u {\r\n o.i0 = v;\r\n } else if o.n == 1u {\r\n o.i1 = v;\r\n } else {\r\n o.i2 = v;\r\n }\r\n o.n += 1u;\r\n }\r\n start += e;\r\n cs = ce;\r\n j += 1u;\r\n if j >= n {\r\n j = 0u;\r\n cs = 0.0;\r\n }\r\n g += 1u;\r\n }\r\n return o;\r\n}\r\n\r\n// the pattern\'s entry that holds its place xh + xl: how far it reaches back and forward from there (px; sc dash units\r\n// per px), exact near its ends as in ivals\r\nfn entry_span(si: u32, xh: f32, xl: f32, sc: f32) -> vec2f {\r\n let n = dash_n(si);\r\n let p = styles[si].period;\r\n let kp = floor((xh + xl) / p);\r\n let x0 = xh - kp * p;\r\n let x = x0 + xl;\r\n var j = 0u;\r\n var c0 = 0.0;\r\n var c1 = dash_end(si, 0u);\r\n loop {\r\n if j + 1u >= n || x < c1 {\r\n break;\r\n }\r\n j += 1u;\r\n c0 = c1;\r\n c1 = dash_end(si, j);\r\n }\r\n return vec2f(-((c0 - x0) - xl), (c1 - x0) - xl) / sc;\r\n}\r\n\r\n// the pattern is on at its place xh + xl\r\nfn on_at(si: u32, xh: f32, xl: f32) -> bool {\r\n let n = dash_n(si);\r\n let p = styles[si].period;\r\n let kp = floor((xh + xl) / p);\r\n let x = (xh - kp * p) + xl;\r\n var j = 0u;\r\n loop {\r\n if j + 1u >= n {\r\n break;\r\n }\r\n if x < dash_end(si, j) {\r\n break;\r\n }\r\n j += 1u;\r\n }\r\n return (j & 1u) == 0u;\r\n}\r\n\r\n// no dash (clip_iv): its start beyond its end\r\nconst IV_NONE: vec4f = vec4f(1e30, -1e30, 0.0, 0.0);\r\n\r\n// a dash (px along its side, relative to the side\'s origin) clipped to the side\'s extent lim (ends k: E_*): (z0, z1, how\r\n// each end ends: 0 open, 1 cut, 2 the style\'s cap; an end within 1/1000 px of the extent\'s: there); none: IV_NONE\r\nfn clip_iv(v: vec2f, uq: f32, lim: vec2f, k: vec2u) -> vec4f {\r\n var z0 = v.x + uq;\r\n var z1 = v.y + uq;\r\n if z1 < lim.x || z0 > lim.y {\r\n return IV_NONE;\r\n }\r\n var a = 2.0;\r\n var b = 2.0;\r\n if z0 <= lim.x + 1e-3 {\r\n z0 = select(lim.x, z0, k.x == E_OPEN);\r\n a = select(select(1.0, 2.0, k.x == E_CAP), 0.0, k.x == E_OPEN);\r\n }\r\n if z1 >= lim.y - 1e-3 {\r\n z1 = select(lim.y, z1, k.y == E_OPEN);\r\n b = select(select(1.0, 2.0, k.y == E_CAP), 0.0, k.y == E_OPEN);\r\n }\r\n return vec4f(z0, z1, a, b);\r\n}\r\n\r\n// the geometry a dashed stroke\'s pieces are cut from, at the pixel q: a segment side (origin o, direction dd) or the ring\r\n// of an arc (at the pixel: its outward unit m, its direction t, the pixel\'s distance from the circle, the radius)\r\nstruct Ring {\r\n m: vec2f,\r\n t: vec2f,\r\n dist: f32,\r\n r: f32,\r\n}\r\n\r\n// a point of a side at z px along it (relative to its origin; an arc: arc length from the pixel\'s own place) and the\r\n// side\'s direction there (x, y: the point; z, w: the direction)\r\nfn side_at(q: vec2f, arc: bool, o: vec2f, dd: vec2f, g: Ring, z: f32) -> vec4f {\r\n if !arc {\r\n return vec4f(o + z * dd, dd);\r\n }\r\n let a = z / g.r;\r\n let sa = sin_p(a);\r\n let sh = sin_p(0.5 * a);\r\n let ca = cos_p(a);\r\n return vec4f(q + (-2.0 * g.r * sh * sh - g.dist) * g.m + g.r * sa * g.t, ca * g.t - sa * g.m);\r\n}\r\n\r\n// a dashed segment\'s or an arc\'s coverage of the pixel q, its owner decided (fs): the dashes of each side that meet the\r\n// pixel as pieces (the strip between the dash\'s ends, cut by a square cap half a width further, a round cap\'s half\r\n// circle a piece of its own, round caps of neighbouring dashes that meet taken once: |a| + |b| - |a and b|), near a join\r\n// the neighbour\'s dashes and the join\'s piece where a dash runs through the vertex, the rectangles meeting there taken\r\n// away (the union of docs/RENDER2D.md). Sides: 0 this segment or the arc, 1 the previous segment (an arc: the other side\r\n// where the pattern starts again at its start), 2 the next one (at its end), 3 the joins. solid: one dash all along\r\n// (an arc\'s own caps; the flat tone)\r\nfn pieces(i: VsOut, q: vec2f, arc: bool, solid: bool, si: u32, sc: f32, d: vec2f, dp: vec2f, dn: vec2f, near_s: bool, near_e: bool, g: Ring) -> f32 {\r\n let fl = i.misc.x;\r\n let h = i.par.x;\r\n let cap = (fl >> 4u) & 15u;\r\n let sk = fl & 3u;\r\n let ek = (fl >> 2u) & 3u;\r\n let jk = (fl >> 8u) & 15u;\r\n let cp = (fl & 4096u) != 0u;\r\n let cn = (fl & 8192u) != 0u;\r\n let rs = (fl & M_RESTART_S) != 0u;\r\n let re = (fl & M_RESTART_E) != 0u;\r\n let w = 0.71 + select(h, 0.0, cap == CAP_BUTT);\r\n // this side\'s own extent and the pattern along it: a segment from s (its place at s: xh + xl), an arc from the pixel\'s\r\n // own place on it (z = 0) to its ends A and B\r\n var s = i.seg.xy;\r\n var e = i.seg.zw;\r\n var lens = length(e - s);\r\n var xh = i.dash.x;\r\n var xl = i.dash.y - i.dash.z * sc;\r\n var za = 0.0;\r\n var zb = lens;\r\n var ka = select(select(E_CAP, E_CUT, sk == JOIN), E_OPEN, sk == OPEN);\r\n var kb = select(select(E_CAP, E_CUT, ek == JOIN), E_OPEN, ek == OPEN);\r\n if arc {\r\n let f = i.seg.xy;\r\n let up = i.seg.zw;\r\n let rp = i.par.y;\r\n let orient = i.par.z;\r\n let wv = (q - f) + rp * up;\r\n let da = unit_or_zero((i.nb.xy - f) + rp * up);\r\n let db = unit_or_zero((i.nb.zw - f) + rp * up);\r\n let ta = orient * vec2f(-da.y, da.x);\r\n let tb = orient * vec2f(-db.y, db.x);\r\n // the arc lengths from the pixel\'s place to A and to B, and from the dash reference to the pixel\'s place\r\n za = -rp * atan2_p(dot(wv, ta), dot(wv, da));\r\n zb = -rp * atan2_p(dot(wv, tb), dot(wv, db));\r\n let br = i.par.w;\r\n let cr = cos_p(br);\r\n let sr = sin_p(br);\r\n let tf = orient * vec2f(-up.y, up.x);\r\n let dr = cr * up + sr * tf;\r\n let tr = cr * tf - sr * up;\r\n let srp = rp * atan2_p(dot(q - f, tr) - rp * sr, dot(q - f, dr) + rp * cr);\r\n xh = i.dash.x;\r\n xl = i.dash.y + srp * sc;\r\n let capa = cp;\r\n let capb = cn;\r\n ka = select(select(E_OPEN, E_CUT, rs), E_CAP, capa);\r\n kb = select(select(E_OPEN, E_CUT, re), E_CAP, capb);\r\n s = q;\r\n }\r\n // on at the ends: this side\'s place, the other side\'s (the same unless the pattern starts again there); a cut end\r\n // whose other side is off takes a cap\r\n let on_s = solid || on_at(si, xh, xl + za * sc);\r\n let on_e = solid || on_at(si, xh, xl + zb * sc);\r\n let ps_h = select(xh, i.dash.w, rs);\r\n let ps_l = select(xl + za * sc, 0.0, rs);\r\n let ne_h = select(xh, i.dash.w, re);\r\n let ne_l = select(xl + zb * sc, 0.0, re);\r\n let pon_s = solid || on_at(si, ps_h, ps_l);\r\n let non_e = solid || on_at(si, ne_h, ne_l);\r\n if ka == E_CUT && !pon_s {\r\n ka = E_CAP;\r\n }\r\n if kb == E_CUT && !non_e {\r\n kb = E_CAP;\r\n }\r\n let lp = length(i.nb.xy);\r\n let ln = length(i.nb.zw);\r\n let joins_s = !arc && near_s && sk == JOIN && on_s && pon_s;\r\n let joins_e = !arc && near_e && ek == JOIN && on_e && non_e;\r\n // the sides that may hold pieces, as bits (loops over bits, not over a count the compiler could unroll)\r\n var sides = 1u;\r\n if arc {\r\n sides |= select(0u, 2u, rs && !solid) | select(0u, 4u, re && !solid);\r\n } else {\r\n sides |= select(0u, 2u, near_s && sk == JOIN) | select(0u, 4u, near_e && ek == JOIN) | select(0u, 8u, joins_s || joins_e);\r\n }\r\n var c = 0.0;\r\n // how far the dashes that run through a join reach from the vertex, within their segments (the joins\' overlap is cut\r\n // there): this segment\'s from S and to E, the previous one\'s to S, the next one\'s from E; solid: the segments (the\r\n // neighbours\' far ends open as above)\r\n var ls_own = lens;\r\n var le_own = lens;\r\n var ls_nb = select(1e30, lp, cp || near_e);\r\n var le_nb = select(1e30, ln, cn || near_s);\r\n if !solid && !arc {\r\n let sq = select(0.0, h, cap == CAP_SQUARE);\r\n if joins_s {\r\n ls_own = min(entry_span(si, xh, xl, sc).y + sq, lens);\r\n ls_nb = min(entry_span(si, ps_h, ps_l, sc).x + sq, lp);\r\n }\r\n if joins_e {\r\n le_own = min(entry_span(si, xh, xl + zb * sc, sc).x + sq, lens);\r\n le_nb = min(entry_span(si, ne_h, ne_l, sc).y + sq, ln);\r\n }\r\n }\r\n loop {\r\n if sides == 0u {\r\n break;\r\n }\r\n let side = firstTrailingBit(sides);\r\n sides &= sides - 1u;\r\n // the side: origin, direction, the pattern\'s place at the origin, its extent and how its ends end\r\n var o = s;\r\n var dd = d;\r\n var ph = xh;\r\n var pl = xl;\r\n var lim = vec2f(select(za, -1e30, ka == E_OPEN), select(zb, 1e30, kb == E_OPEN));\r\n var k = vec2u(ka, kb);\r\n if side == 1u {\r\n if arc {\r\n // the other side at A where the pattern starts again: it runs up to A\r\n ph = ps_h;\r\n pl = ps_l - za * sc;\r\n lim = vec2f(-1e30, za);\r\n k = vec2u(E_OPEN, select(E_CAP, E_CUT, on_s));\r\n } else {\r\n dd = dp;\r\n ph = ps_h;\r\n pl = ps_l;\r\n lim = vec2f(-lp, 0.0);\r\n k = vec2u(select(select(E_OPEN, E_CUT, near_e), E_CAP, cp), select(E_CAP, E_CUT, on_s));\r\n lim.x = select(-lp, -1e30, k.x == E_OPEN);\r\n }\r\n } else if side == 2u {\r\n if arc {\r\n ph = ne_h;\r\n pl = ne_l - zb * sc;\r\n lim = vec2f(zb, 1e30);\r\n k = vec2u(select(E_CAP, E_CUT, on_e), E_OPEN);\r\n } else {\r\n o = e;\r\n dd = dn;\r\n ph = ne_h;\r\n pl = ne_l;\r\n lim = vec2f(0.0, ln);\r\n k = vec2u(select(E_CAP, E_CUT, on_e), select(select(E_OPEN, E_CUT, near_s), E_CAP, cn));\r\n lim.y = select(ln, 1e30, k.y == E_OPEN);\r\n }\r\n }\r\n // the side\'s dashes near the pixel, clipped to its extent; square caps lengthen them and dashes that then meet\r\n // become one\r\n let uq = select(dot(q - o, dd), 0.0, arc);\r\n var iv: Ivals;\r\n if solid {\r\n iv.n = 1u;\r\n iv.i0 = vec2f(-1e30, 1e30);\r\n } else if side < 3u {\r\n iv = ivals(si, ph, pl + uq * sc, sc, w);\r\n }\r\n var v0 = clip_iv(iv.i0, uq, lim, k);\r\n var v1 = clip_iv(iv.i1, uq, lim, k);\r\n var v2 = clip_iv(iv.i2, uq, lim, k);\r\n let nv = select(iv.n, 0u, side == 3u);\r\n if nv < 3u {\r\n v2 = IV_NONE;\r\n }\r\n if nv < 2u {\r\n v1 = IV_NONE;\r\n }\r\n if nv < 1u {\r\n v0 = IV_NONE;\r\n }\r\n // (a segment\'s square caps lengthen its strip; an arc\'s are rectangles along its direction at the dash\'s end: pieces\r\n // of their own, as round caps)\r\n if cap == CAP_SQUARE && !arc {\r\n v0 = select(v0, vec4f(v0.x - h * f32(v0.z == 2.0), v0.y + h * f32(v0.w == 2.0), min(v0.z, 1.0), min(v0.w, 1.0)), v0.x <= v0.y);\r\n v1 = select(v1, vec4f(v1.x - h * f32(v1.z == 2.0), v1.y + h * f32(v1.w == 2.0), min(v1.z, 1.0), min(v1.w, 1.0)), v1.x <= v1.y);\r\n v2 = select(v2, vec4f(v2.x - h * f32(v2.z == 2.0), v2.y + h * f32(v2.w == 2.0), min(v2.z, 1.0), min(v2.w, 1.0)), v2.x <= v2.y);\r\n if v1.x <= v1.y && v2.x <= v2.y && v1.y >= v2.x {\r\n v1 = vec4f(v1.x, v2.y, v1.z, v2.w);\r\n v2 = IV_NONE;\r\n }\r\n if v0.x <= v0.y && v1.x <= v1.y && v0.y >= v1.x {\r\n v0 = vec4f(v0.x, v1.y, v0.z, v1.w);\r\n v1 = v2;\r\n v2 = IV_NONE;\r\n }\r\n }\r\n let round = cap == CAP_ROUND || (cap == CAP_SQUARE && arc);\r\n let square = cap == CAP_SQUARE;\r\n // the pieces as bits: per dash j bits 3j (its strip), 3j + 1 and 3j + 2 (its caps as pieces: round, an arc\'s square);\r\n // per pair of neighbouring dashes whose caps meet bits 9 + 3j (the caps), 10 + 3j and 11 + 3j (a cap and the other\r\n // dash: taken away); side 3: the join\'s piece and the rectangles meeting at the start (bits 0, 1) and at the end (2, 3)\r\n var pm = 0u;\r\n if side == 3u {\r\n pm = select(0u, 3u, joins_s) | select(0u, 12u, joins_e);\r\n } else {\r\n pm = select(0u, 1u, v0.x <= v0.y) | select(0u, 8u, v1.x <= v1.y) | select(0u, 64u, v2.x <= v2.y);\r\n if round {\r\n pm |= select(0u, 2u, v0.x <= v0.y && v0.z == 2.0) | select(0u, 4u, v0.x <= v0.y && v0.w == 2.0);\r\n pm |= select(0u, 16u, v1.x <= v1.y && v1.z == 2.0) | select(0u, 32u, v1.x <= v1.y && v1.w == 2.0);\r\n pm |= select(0u, 128u, v2.x <= v2.y && v2.z == 2.0) | select(0u, 256u, v2.x <= v2.y && v2.w == 2.0);\r\n let g01 = v1.x - v0.y;\r\n if v0.x <= v0.y && v1.x <= v1.y && v0.w == 2.0 && v1.z == 2.0 && g01 < 2.0 * h {\r\n pm |= select(512u, 3584u, g01 < h);\r\n }\r\n let g12 = v2.x - v1.y;\r\n if v1.x <= v1.y && v2.x <= v2.y && v1.w == 2.0 && v2.z == 2.0 && g12 < 2.0 * h {\r\n pm |= select(4096u, 28672u, g12 < h);\r\n }\r\n }\r\n }\r\n var stl = strip_l(q, o, dd, h);\r\n var str = strip_r(q, o, dd, h);\r\n if arc {\r\n stl = vec3f(g.m, g.dist - h);\r\n str = select(ALL, vec3f(-g.m, -g.dist - h), g.r > h);\r\n }\r\n loop {\r\n if pm == 0u {\r\n break;\r\n }\r\n let kk = firstTrailingBit(pm);\r\n pm &= pm - 1u;\r\n var x0 = ALL;\r\n var x1 = ALL;\r\n var x2 = ALL;\r\n var x3 = ALL;\r\n var x4 = ALL;\r\n var x5 = ALL;\r\n var wt = 1.0;\r\n if side == 3u {\r\n let at_s = kk < 2u;\r\n var jj: Join;\r\n if at_s {\r\n jj = make_join(s, dp, d, jk, i.par.y, h);\r\n } else {\r\n jj = make_join(e, d, dn, jk, i.par.y, h);\r\n }\r\n if (kk & 1u) == 0u {\r\n let wd = wedge(q, jj, h);\r\n x0 = wd.a;\r\n x1 = wd.b;\r\n x2 = wd.c;\r\n x3 = wd.d;\r\n } else {\r\n // the rectangles meeting on the inner side of the turn: between the two strips\' inner lines and the lines at\r\n // right angles through the vertex, cut where the dashes through it end (their outer lines lie beyond)\r\n let nbd = select(dn, dp, at_s);\r\n let vtx = select(e, s, at_s);\r\n let din = select(d, dp, at_s);\r\n let dout = select(dn, d, at_s);\r\n let left = din.x * dout.y - din.y * dout.x >= 0.0;\r\n x0 = select(strip_r(q, s, d, h), strip_l(q, s, d, h), left);\r\n x1 = select(strip_r(q, vtx, nbd, h), strip_l(q, vtx, nbd, h), left);\r\n x2 = before(q, vtx, din);\r\n x3 = before(q, vtx, -dout);\r\n let lin = select(le_own, ls_nb, at_s);\r\n let lout = select(le_nb, ls_own, at_s);\r\n x4 = select(before(q, vtx - lin * din, -din), ALL, lin > 1e29);\r\n x5 = select(before(q, vtx + lout * dout, dout), ALL, lout > 1e29);\r\n wt = -1.0;\r\n }\r\n } else if kk < 9u {\r\n let iv_k = kk / 3u;\r\n let v = select(select(v2, v1, iv_k == 1u), v0, iv_k == 0u);\r\n let sub = kk % 3u;\r\n if sub == 0u {\r\n let pa = side_at(q, arc, o, dd, g, v.x);\r\n let pb = side_at(q, arc, o, dd, g, v.y);\r\n x0 = stl;\r\n x1 = str;\r\n x2 = select(ALL, before(q, pa.xy, -pa.zw), v.z > 0.5);\r\n x3 = select(ALL, before(q, pb.xy, pb.zw), v.w > 0.5);\r\n } else {\r\n // (a cap beyond the dash\'s end, outward along ot: a half circle, or a rectangle half a width long)\r\n let pc = side_at(q, arc, o, dd, g, select(v.y, v.x, sub == 1u));\r\n let ot = select(pc.zw, -pc.zw, sub == 1u);\r\n x0 = tangent(q, pc.xy, h);\r\n x1 = before(q, pc.xy, -ot);\r\n if square {\r\n x0 = strip_l(q, pc.xy, ot, h);\r\n x2 = strip_r(q, pc.xy, ot, h);\r\n x3 = vec3f(ot, dot(q - pc.xy, ot) - h);\r\n }\r\n }\r\n } else {\r\n // dashes j and j + 1 whose caps meet: |caps|, |cap of j and dash j + 1|, |dash j and cap of j + 1| (an arc\'s square\r\n // caps: their rectangles, both cut by one of their strips, the near cap\'s: six planes)\r\n let pj = (kk - 9u) / 3u;\r\n let va = select(v1, v0, pj == 0u);\r\n let vb = select(v2, v1, pj == 0u);\r\n let ea = side_at(q, arc, o, dd, g, va.y);\r\n let eb = side_at(q, arc, o, dd, g, vb.x);\r\n let sub = (kk - 9u) % 3u;\r\n wt = -1.0;\r\n // (cap j beyond ea along ea.zw, cap j + 1 before eb, against eb.zw)\r\n let ha0 = select(tangent(q, ea.xy, h), strip_l(q, ea.xy, ea.zw, h), square);\r\n let ha1 = select(ALL, strip_r(q, ea.xy, ea.zw, h), square);\r\n let ha2 = before(q, ea.xy, -ea.zw);\r\n let ha3 = select(ALL, vec3f(ea.zw, dot(q - ea.xy, ea.zw) - h), square);\r\n let hb0 = select(tangent(q, eb.xy, h), strip_l(q, eb.xy, eb.zw, h), square);\r\n let hb1 = select(ALL, strip_r(q, eb.xy, eb.zw, h), square);\r\n let hb2 = before(q, eb.xy, eb.zw);\r\n let hb3 = select(ALL, vec3f(-eb.zw, dot(q - eb.xy, -eb.zw) - h), square);\r\n if sub == 0u {\r\n x0 = ha0;\r\n x1 = ha1;\r\n x2 = ha2;\r\n x3 = ha3;\r\n x4 = select(hb0, hb2, square);\r\n x5 = select(hb2, hb3, square);\r\n } else if sub == 1u {\r\n let s_b = side_at(q, arc, o, dd, g, vb.y);\r\n x0 = ha0;\r\n x1 = ha2;\r\n x2 = select(stl, ha1, square);\r\n x3 = select(str, ha3, square);\r\n x4 = before(q, eb.xy, -eb.zw);\r\n x5 = select(ALL, before(q, s_b.xy, s_b.zw), vb.w > 0.5);\r\n } else {\r\n let s_a = side_at(q, arc, o, dd, g, va.x);\r\n x0 = select(stl, hb1, square);\r\n x1 = select(str, hb3, square);\r\n x2 = select(ALL, before(q, s_a.xy, -s_a.zw), va.z > 0.5);\r\n x3 = before(q, ea.xy, ea.zw);\r\n x4 = hb0;\r\n x5 = hb2;\r\n }\r\n }\r\n c += wt * area6(x0, x1, x2, x3, x4, x5);\r\n }\r\n }\r\n return clamp(c, 0.0, 1.0);\r\n}\r\n\r\n@fragment\r\nfn fs(i: VsOut) -> @location(0) vec4f {\r\n let q = i.pos.xy;\r\n let s = i.seg.xy;\r\n let e = i.seg.zw;\r\n let h = i.par.x;\r\n let sk = i.misc.x & 3u;\r\n let ek = (i.misc.x >> 2u) & 3u;\r\n let cap = (i.misc.x >> 4u) & 15u;\r\n let jk = (i.misc.x >> 8u) & 15u;\r\n let ends = (i.misc.x >> 12u) & 3u;\r\n let own = i.par.zw;\r\n // a pixel near a join is the nearer segment\'s (the incoming one\'s when equal, up to JOIN_TIE): see the head\r\n var dprev = 3.4e38;\r\n var dnext = 3.4e38;\r\n if sk == JOIN {\r\n dprev = dist_seg(q - s, i.nb.xy);\r\n if !(dist_seg(q - s, own) < dprev - JOIN_TIE * max(dprev, 1.0)) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n }\r\n if ek == JOIN {\r\n dnext = dist_seg(q - e, i.nb.zw);\r\n if !(dist_seg(q - e, -own) <= dnext + JOIN_TIE * max(dnext, 1.0)) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n }\r\n var d = unit_or_zero(own);\r\n if !any(d != vec2f(0.0)) {\r\n d = vec2f(1.0, 0.0);\r\n }\r\n let dp = unit_or_zero(-i.nb.xy);\r\n let dn = unit_or_zero(i.nb.zw);\r\n // which pieces may touch the pixel: a neighbour\'s rectangle (a square cap too) lies within h sqrt(2) of its segment,\r\n // a join\'s piece within its farthest corner\'s distance from the vertex (a miter\'s point h / cos(phi / 2), else h),\r\n // an own cap within h of its end; a pixel reaches 0.71 beyond its centre\r\n let rr = h * 1.415 + 0.71;\r\n var near_s = false;\r\n var near_e = false;\r\n if sk == JOIN {\r\n let c = dot(dp, d);\r\n let wr = h * select(1.0, max(sqrt(2.0 / max(1.0 + c, 1e-6)), 1.0), jk == JOIN_MITER && miter_ok(c, i.par.y)) + 0.71;\r\n near_s = dprev < rr * rr || length(q - s) < wr;\r\n }\r\n if ek == JOIN {\r\n let c = dot(d, dn);\r\n let wr = h * select(1.0, max(sqrt(2.0 / max(1.0 + c, 1e-6)), 1.0), jk == JOIN_MITER && miter_ok(c, i.par.y)) + 0.71;\r\n near_e = dnext < rr * rr || length(q - e) < wr;\r\n }\r\n let u0 = dot(q - s, d);\r\n let u1 = dot(q - e, d);\r\n // (a pixel beyond a join\'s end is never the body\'s: the join decides there)\r\n near_s = near_s || (sk == JOIN && u0 < 0.71);\r\n near_e = near_e || (ek == JOIN && u1 > -0.71);\r\n let lim = 0.71 + select(0.0, h, cap != CAP_BUTT);\r\n let near_cs = sk == CAP && u0 < lim;\r\n let near_ce = ek == CAP && u1 > -lim;\r\n let sl = strip_l(q, s, d, h);\r\n let sr = strip_r(q, s, d, h);\r\n if !near_s && !near_e && !near_cs && !near_ce {\r\n // the body: the strip alone (most pixels)\r\n let c0 = max(cov(sl.z, sl.xy) + cov(sr.z, sr.xy) - 1.0, 0.0);\r\n if !(c0 > 0.0) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n let col = unpack4x8unorm(i.misc.y);\r\n return vec4f(vec3f(col.z, col.y, col.x) * (col.w * c0), col.w * c0);\r\n }\r\n // short thin strokes near their ends: the tile of each piece as a product (the strip\'s share times the share on its side\r\n // of the bisector), ownership and the near tests as above\r\n if h <= TINY_H && length(own) <= TINY_LEN {\r\n var cn = 0.0;\r\n var weight = 1.0;\r\n if near_s {\r\n let sm = dp + d;\r\n let ls = length(sm);\r\n let m = select(vec2f(-dp.y, dp.x), sm / ls, ls > 1e-6);\r\n let b = cov(-dot(q - s, m), m);\r\n let np = vec2f(-dp.y, dp.x);\r\n let vp = dot(q - s, np);\r\n cn += (1.0 - b) * max(cov(vp - h, np) + cov(-vp - h, -np) - 1.0, 0.0);\r\n weight *= b;\r\n }\r\n if near_e {\r\n let sm = d + dn;\r\n let ls = length(sm);\r\n let m = select(vec2f(-d.y, d.x), sm / ls, ls > 1e-6);\r\n let b = cov(dot(q - e, m), -m);\r\n let nx = vec2f(-dn.y, dn.x);\r\n let vn = dot(q - e, nx);\r\n cn += (1.0 - b) * max(cov(vn - h, nx) + cov(-vn - h, -nx) - 1.0, 0.0);\r\n weight *= b;\r\n }\r\n var o = max(cov(sl.z, sl.xy) + cov(sr.z, sr.xy) - 1.0, 0.0);\r\n // caps: a butt line at the end, a square one half a width on, a round one where a line would keep its area\r\n let ext = select(select(0.0, h, cap == CAP_SQUARE), 0.7854 * h, cap == CAP_ROUND);\r\n if sk == CAP {\r\n o *= cov(-u0 - ext, -d);\r\n }\r\n if ek == CAP {\r\n o *= cov(u1 - ext, d);\r\n }\r\n let ct = clamp(cn + weight * o, 0.0, 1.0);\r\n if !(ct > 0.0) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n let col = unpack4x8unorm(i.misc.y);\r\n return vec4f(vec3f(col.z, col.y, col.x) * (col.w * ct), col.w * ct);\r\n }\r\n // its own ends\' lines: a cap\'s, a join\'s line at right angles (that join not near), none where open\r\n let ps = select(select(cap_line(q, s, -d, cap, h), before(q, s, -d), sk == JOIN), ALL, sk == OPEN);\r\n let pe = select(select(cap_line(q, e, d, cap, h), before(q, e, d), ek == JOIN), ALL, ek == OPEN);\r\n let lens = length(own);\r\n let a = s + i.nb.xy;\r\n let b = e + i.nb.zw;\r\n let cp = (ends & 1u) != 0u;\r\n let cn = (ends & 2u) != 0u;\r\n let round = cap == CAP_ROUND;\r\n var js: Join;\r\n var je: Join;\r\n var ok = true;\r\n if near_s {\r\n js = make_join(s, dp, d, jk, i.par.y, h);\r\n ok = ok && js.reach <= min(lens, length(i.nb.xy));\r\n }\r\n if near_e {\r\n je = make_join(e, d, dn, jk, i.par.y, h);\r\n ok = ok && je.reach <= min(lens, length(i.nb.zw));\r\n }\r\n // the pieces that may touch the pixel, as bits of a mask (S_* below), and the cuts that depend on the case: this\r\n // segment\'s rectangle [strip, o2, o3, o4, o5], the previous one\'s [strip, pf, pn, p4], the next one\'s [strip, nn,\r\n // nf, n4]; the loop below adds (or takes away) each piece\'s share, so that area6 sits in the shader once (written\r\n // out at every place it was 40 times, and the GPU spent its time loading code)\r\n var o2 = ps;\r\n var o3 = pe;\r\n var o4 = ALL;\r\n var o5 = ALL;\r\n var pf = ALL;\r\n var pn = ALL;\r\n var p4 = ALL;\r\n var nn = ALL;\r\n var nf = ALL;\r\n var n4 = ALL;\r\n var mask = S_OWN;\r\n let jround = jk == JOIN_ROUND;\r\n if round {\r\n mask |= select(0u, S_HD_S, near_cs) | select(0u, S_HD_E, near_ce);\r\n }\r\n if ok && (near_s || near_e) {\r\n // the tiles beside the near joins (their bisectors\' chords within both segments: they do not overlap and fill\r\n // the stroke there): this segment\'s, the neighbours\'; a round join\'s circle beyond the line at right angles\r\n // through the vertex, a bevel\'s line\r\n if near_s {\r\n o2 = bisector(q, js, false);\r\n o4 = select(bevel(q, js), before(q, s, -d), jround);\r\n pf = select(ALL, cap_line(q, a, -dp, cap, h), cp);\r\n pn = bisector(q, js, true);\r\n p4 = select(bevel(q, js), before(q, s, dp), jround);\r\n mask |= S_PREV | select(0u, S_SECT_S_OWN | S_SECT_S_PREV, jround);\r\n }\r\n if near_e {\r\n o3 = bisector(q, je, true);\r\n o5 = select(bevel(q, je), before(q, e, d), jround);\r\n nn = bisector(q, je, false);\r\n nf = select(ALL, cap_line(q, b, dn, cap, h), cn);\r\n n4 = select(bevel(q, je), before(q, e, -dn), jround);\r\n mask |= S_NEXT | select(0u, S_SECT_E_OWN | S_SECT_E_NEXT, jround);\r\n }\r\n } else if near_s || near_e {\r\n // a join whose bisector\'s chord is longer than a segment (a sharp turn of a short one): the union of the\r\n // rectangles and the joins\' pieces (beyond both rectangles\' ends: they meet none), |a| + |w| + |b| - |a and b|,\r\n // where a short segment near both joins leaves out the neighbours\' rectangles meeting each other\r\n if near_s {\r\n o2 = before(q, s, -d);\r\n pf = select(select(ALL, before(q, a, -dp), near_e), cap_line(q, a, -dp, cap, h), cp);\r\n pn = before(q, s, dp);\r\n mask |= S_PREV | S_WEDGE_S | S_X_PREV;\r\n }\r\n if near_e {\r\n o3 = before(q, e, d);\r\n nn = before(q, e, -dn);\r\n nf = select(select(ALL, before(q, b, dn), near_s), cap_line(q, b, dn, cap, h), cn);\r\n mask |= S_NEXT | S_WEDGE_E | S_X_NEXT;\r\n }\r\n }\r\n // (a round cap at the polyline\'s end, on a near neighbour)\r\n if round {\r\n mask |= select(0u, S_HD_A, near_s && cp && length(q - a) < h + 0.71) | select(0u, S_HD_B, near_e && cn && length(q - b) < h + 0.71);\r\n }\r\n var c = 0.0;\r\n var ao = 0.0;\r\n var ap = 0.0;\r\n var an = 0.0;\r\n var m = mask;\r\n loop {\r\n if m == 0u {\r\n break;\r\n }\r\n let k = firstTrailingBit(m);\r\n m &= m - 1u;\r\n var x0 = ALL;\r\n var x1 = ALL;\r\n var x2 = ALL;\r\n var x3 = ALL;\r\n var x4 = ALL;\r\n var x5 = ALL;\r\n var w = 1.0;\r\n switch k {\r\n case 0u {\r\n x0 = sl;\r\n x1 = sr;\r\n x2 = o2;\r\n x3 = o3;\r\n x4 = o4;\r\n x5 = o5;\r\n }\r\n case 1u {\r\n x0 = strip_l(q, s, dp, h);\r\n x1 = strip_r(q, s, dp, h);\r\n x2 = pf;\r\n x3 = pn;\r\n x4 = p4;\r\n }\r\n case 2u {\r\n x0 = strip_l(q, e, dn, h);\r\n x1 = strip_r(q, e, dn, h);\r\n x2 = nn;\r\n x3 = nf;\r\n x4 = n4;\r\n }\r\n case 3u {\r\n x0 = -before(q, s, -d);\r\n x1 = bisector(q, js, false);\r\n x2 = tangent(q, s, h);\r\n }\r\n case 4u {\r\n x0 = -before(q, e, d);\r\n x1 = bisector(q, je, true);\r\n x2 = tangent(q, e, h);\r\n }\r\n case 5u {\r\n x0 = -before(q, s, dp);\r\n x1 = bisector(q, js, true);\r\n x2 = tangent(q, s, h);\r\n }\r\n case 6u {\r\n x0 = -before(q, e, -dn);\r\n x1 = bisector(q, je, false);\r\n x2 = tangent(q, e, h);\r\n }\r\n case 7u, 8u {\r\n var j = js;\r\n if k == 8u {\r\n j = je;\r\n }\r\n let wd = wedge(q, j, h);\r\n x0 = wd.a;\r\n x1 = wd.b;\r\n x2 = wd.c;\r\n x3 = wd.d;\r\n }\r\n case 9u {\r\n // (the rectangles meeting: this one\'s and the previous one\'s beside the vertex)\r\n w = select(0.0, -1.0, ao > 0.0 && ap > 0.0);\r\n x0 = sl;\r\n x1 = sr;\r\n x2 = before(q, s, -d);\r\n x3 = strip_l(q, s, dp, h);\r\n x4 = strip_r(q, s, dp, h);\r\n x5 = before(q, s, dp);\r\n }\r\n case 10u {\r\n w = select(0.0, -1.0, ao > 0.0 && an > 0.0);\r\n x0 = sl;\r\n x1 = sr;\r\n x2 = before(q, e, d);\r\n x3 = strip_l(q, e, dn, h);\r\n x4 = strip_r(q, e, dn, h);\r\n x5 = before(q, e, -dn);\r\n }\r\n default {\r\n // a round cap\'s half circle: at either own end, at the previous or next segment\'s far end\r\n let ctr = select(select(b, a, k == 13u), select(e, s, k == 11u), k <= 12u);\r\n let out = select(select(dn, -dp, k == 13u), select(d, -d, k == 11u), k <= 12u);\r\n x0 = tangent(q, ctr, h);\r\n x1 = before(q, ctr, -out);\r\n }\r\n }\r\n if w != 0.0 {\r\n let x = area6(x0, x1, x2, x3, x4, x5);\r\n if k == 0u {\r\n ao = x;\r\n } else if k == 1u {\r\n ap = x;\r\n } else if k == 2u {\r\n an = x;\r\n }\r\n c += w * x;\r\n }\r\n }\r\n c = clamp(c, 0.0, 1.0);\r\n if !(c > 0.0) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n // (the colour unpacked where it is returned, as in full_body)\r\n let col = unpack4x8unorm(i.misc.y);\r\n let al = col.w * c;\r\n return vec4f(vec3f(col.z, col.y, col.x) * al, al);\r\n}\r\n\r\n// the fragment shader of layers that hold arcs or dashed strokes (lib.rs: the second pipeline; fs draws the others): a\r\n// pixel\'s owner decided as in fs (an arc\'s pieces at their joints: by the side of the joint\'s radial line), the\r\n// coverage from pieces(): the pattern exactly, the solid stroke near its ends (the union at joins: area6 has one call\r\n// in this shader, a second one made the driver call it as a function, and every pixel three times slower), mixed where\r\n// the pattern\'s period nears a pixel. Solid segments here as in fs except that near their joins the union stands for\r\n// the tiles. Its body is in full_body.wgsl: lib.rs puts it into the entry fs_full here and once more into the function\r\n// full_body, with which fs_fill draws the strokes of its layers (a discard there, and the caller returns at once). As a\r\n// function called from fs_full too, the NVIDIA driver made every pixel of the second pipeline 20 % slower\r\n\n@fragment\nfn fs_full(i: VsOut) -> @location(0) vec4f {\n // (the body of fs_full and of full_body, written once: lib.rs puts it into both; see fs_full\'s head in stroke.wgsl)\r\n let q = i.pos.xy;\r\n let h = i.par.x;\r\n let fl = i.misc.x;\r\n let cap = (fl >> 4u) & 15u;\r\n let dashed = (fl & M_DASHED) != 0u;\r\n let arc = (fl & M_ARC) != 0u;\r\n let si = i.misc.z;\r\n // the pattern drawn exactly, as a flat tone of its duty (its caps counted as the area they add), or mixed (tf; a\r\n // solid arc: 1)\r\n var sc = 1.0;\r\n var tf = 1.0;\r\n var duty = 1.0;\r\n // the pattern\'s pass (pieces below), decided here where tf is computed: tested after this block as dashed && tf < 1.0,\r\n // Intel\'s Windows driver (UHD 615, 31.0.101.2135) found it false in fs_fill with tf 0, also as two ifs (the dashes of\r\n // layers with a fill were gone, docs/RENDER2D.md; the cause: lib/wgsl\'s && through a bool variable, a u32 since)\r\n var pat = 0u;\r\n if dashed {\r\n // (dash units per pixel: the vertex shader\'s, the block\'s units inside an insert)\r\n sc = select(1.0, bitcast<f32>(i.misc.w), ((styles[si].flags >> 20u) & 15u) == UNIT_DRAWING);\r\n let period = styles[si].period;\r\n tf = clamp((DASH_EXACT - period / sc) / (DASH_EXACT - DASH_FLAT), 0.0, 1.0);\r\n let capl = select(select(0.0, 2.0 * h, cap == CAP_SQUARE), 1.5707963 * h, cap == CAP_ROUND) * sc;\r\n duty = min(1.0, styles[si].duty + styles[si].ons * capl / period);\r\n pat = select(0u, 1u, tf < 1.0);\r\n }\r\n var g: Ring;\r\n g.m = vec2f(1.0, 0.0);\r\n g.t = vec2f(0.0, 1.0);\r\n g.dist = 0.0;\r\n g.r = 1.0;\r\n var d = vec2f(1.0, 0.0);\r\n var dp = vec2f(0.0);\r\n var dn = vec2f(0.0);\r\n var near_s = false;\r\n var near_e = false;\r\n // the solid stroke\'s coverage where no end is near (its body): the strip alone\r\n var body = -1.0;\r\n if arc {\r\n // the circle from F (see the head)\r\n let f = i.seg.xy;\r\n let up = i.seg.zw;\r\n let rp = i.par.y;\r\n let orient = i.par.z;\r\n let qf = q - f;\r\n let wv = qf + rp * up;\r\n let lw = length(wv);\r\n g.dist = (dot(qf, qf) + 2.0 * rp * dot(qf, up)) / (lw + rp);\r\n if lw > 0.0 {\r\n g.m = wv / lw;\r\n } else {\r\n g.m = up;\r\n }\r\n g.t = orient * vec2f(-g.m.y, g.m.x);\r\n g.r = rp;\r\n let da = unit_or_zero((i.nb.xy - f) + rp * up);\r\n let db = unit_or_zero((i.nb.zw - f) + rp * up);\r\n let sa = dot(q - i.nb.xy, orient * vec2f(-da.y, da.x));\r\n let sb = dot(q - i.nb.zw, orient * vec2f(-db.y, db.x));\r\n let capa = (fl & 4096u) != 0u;\r\n let capb = (fl & 8192u) != 0u;\r\n if !capa && sa < 0.0 {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n if !capb && sb >= 0.0 {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n let reach = 0.71 + select(h, 0.0, cap == CAP_BUTT);\r\n if !((capa && sa < reach) || (capb && sb > -reach)) {\r\n body = max(cov(g.dist - h, g.m) + select(1.0, cov(-g.dist - h, -g.m), rp > h) - 1.0, 0.0);\r\n }\r\n } else {\r\n let s = i.seg.xy;\r\n let e = i.seg.zw;\r\n let sk = fl & 3u;\r\n let ek = (fl >> 2u) & 3u;\r\n let jk = (fl >> 8u) & 15u;\r\n let own = i.par.zw;\r\n // a pixel near a join is the nearer segment\'s (the incoming one\'s when equal, up to JOIN_TIE): see the head\r\n var dprev = 3.4e38;\r\n var dnext = 3.4e38;\r\n if sk == JOIN {\r\n dprev = dist_seg(q - s, i.nb.xy);\r\n if !(dist_seg(q - s, own) < dprev - JOIN_TIE * max(dprev, 1.0)) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n }\r\n if ek == JOIN {\r\n dnext = dist_seg(q - e, i.nb.zw);\r\n if !(dist_seg(q - e, -own) <= dnext + JOIN_TIE * max(dnext, 1.0)) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n }\r\n d = unit_or_zero(own);\r\n if !any(d != vec2f(0.0)) {\r\n d = vec2f(1.0, 0.0);\r\n }\r\n dp = unit_or_zero(-i.nb.xy);\r\n dn = unit_or_zero(i.nb.zw);\r\n // which pieces may touch the pixel (as in fs)\r\n let rr = h * 1.415 + 0.71;\r\n if sk == JOIN {\r\n let cs = dot(dp, d);\r\n let wr = h * select(1.0, max(sqrt(2.0 / max(1.0 + cs, 1e-6)), 1.0), jk == JOIN_MITER && miter_ok(cs, i.par.y)) + 0.71;\r\n near_s = dprev < rr * rr || length(q - s) < wr;\r\n }\r\n if ek == JOIN {\r\n let cs = dot(d, dn);\r\n let wr = h * select(1.0, max(sqrt(2.0 / max(1.0 + cs, 1e-6)), 1.0), jk == JOIN_MITER && miter_ok(cs, i.par.y)) + 0.71;\r\n near_e = dnext < rr * rr || length(q - e) < wr;\r\n }\r\n let u0 = dot(q - s, d);\r\n let u1 = dot(q - e, d);\r\n near_s = near_s || (sk == JOIN && u0 < 0.71);\r\n near_e = near_e || (ek == JOIN && u1 > -0.71);\r\n let lim = 0.71 + select(0.0, h, cap != CAP_BUTT);\r\n let sl = strip_l(q, s, d, h);\r\n let sr = strip_r(q, s, d, h);\r\n if !near_s && !near_e && !(sk == CAP && u0 < lim) && !(ek == CAP && u1 > -lim) {\r\n body = max(cov(sl.z, sl.xy) + cov(sr.z, sr.xy) - 1.0, 0.0);\r\n } else if !dashed && h <= TINY_H && length(own) <= TINY_LEN {\r\n // short thin strokes near their ends: as in fs\r\n var cn = 0.0;\r\n var weight = 1.0;\r\n if near_s {\r\n let sm = dp + d;\r\n let ls = length(sm);\r\n let m = select(vec2f(-dp.y, dp.x), sm / ls, ls > 1e-6);\r\n let b = cov(-dot(q - s, m), m);\r\n let np = vec2f(-dp.y, dp.x);\r\n let vp = dot(q - s, np);\r\n cn += (1.0 - b) * max(cov(vp - h, np) + cov(-vp - h, -np) - 1.0, 0.0);\r\n weight *= b;\r\n }\r\n if near_e {\r\n let sm = d + dn;\r\n let ls = length(sm);\r\n let m = select(vec2f(-d.y, d.x), sm / ls, ls > 1e-6);\r\n let b = cov(dot(q - e, m), -m);\r\n let nx = vec2f(-dn.y, dn.x);\r\n let vn = dot(q - e, nx);\r\n cn += (1.0 - b) * max(cov(vn - h, nx) + cov(-vn - h, -nx) - 1.0, 0.0);\r\n weight *= b;\r\n }\r\n var o = max(cov(sl.z, sl.xy) + cov(sr.z, sr.xy) - 1.0, 0.0);\r\n let ext = select(select(0.0, h, cap == CAP_SQUARE), 0.7854 * h, cap == CAP_ROUND);\r\n if sk == CAP {\r\n o *= cov(-u0 - ext, -d);\r\n }\r\n if ek == CAP {\r\n o *= cov(u1 - ext, d);\r\n }\r\n body = clamp(cn + weight * o, 0.0, 1.0);\r\n }\r\n }\r\n // the pattern (pass 0), the solid stroke near its ends (pass 1): one call of pieces (inlined once)\r\n var c = max(body, 0.0);\r\n var cd = 0.0;\r\n var passes = pat | select(0u, 2u, tf > 0.0 && body < 0.0);\r\n loop {\r\n if passes == 0u {\r\n break;\r\n }\r\n let pz = firstTrailingBit(passes);\r\n passes &= passes - 1u;\r\n let v = pieces(i, q, arc, pz == 1u, si, sc, d, dp, dn, near_s, near_e, g);\r\n if pz == 0u {\r\n cd = v;\r\n } else {\r\n c = v;\r\n }\r\n }\r\n if dashed {\r\n c = mix(cd, c * duty, tf);\r\n }\r\n if !(c > 0.0) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n // (the colour unpacked here, not at the top: alive through the whole body, Mesa\'s Intel compiler (26.0) gave one of\r\n // its parts to another value in some pixels, in fs_fill: a channel or the pixel wrong at random, docs/RENDER2D.md)\r\n let col = unpack4x8unorm(i.misc.y);\r\n let al = col.w * c;\r\n return vec4f(vec3f(col.z, col.y, col.x) * al, al);\r\n}\n\nfn full_body(i: VsOut) -> vec4f {\n // (the body of fs_full and of full_body, written once: lib.rs puts it into both; see fs_full\'s head in stroke.wgsl)\r\n let q = i.pos.xy;\r\n let h = i.par.x;\r\n let fl = i.misc.x;\r\n let cap = (fl >> 4u) & 15u;\r\n let dashed = (fl & M_DASHED) != 0u;\r\n let arc = (fl & M_ARC) != 0u;\r\n let si = i.misc.z;\r\n // the pattern drawn exactly, as a flat tone of its duty (its caps counted as the area they add), or mixed (tf; a\r\n // solid arc: 1)\r\n var sc = 1.0;\r\n var tf = 1.0;\r\n var duty = 1.0;\r\n // the pattern\'s pass (pieces below), decided here where tf is computed: tested after this block as dashed && tf < 1.0,\r\n // Intel\'s Windows driver (UHD 615, 31.0.101.2135) found it false in fs_fill with tf 0, also as two ifs (the dashes of\r\n // layers with a fill were gone, docs/RENDER2D.md; the cause: lib/wgsl\'s && through a bool variable, a u32 since)\r\n var pat = 0u;\r\n if dashed {\r\n // (dash units per pixel: the vertex shader\'s, the block\'s units inside an insert)\r\n sc = select(1.0, bitcast<f32>(i.misc.w), ((styles[si].flags >> 20u) & 15u) == UNIT_DRAWING);\r\n let period = styles[si].period;\r\n tf = clamp((DASH_EXACT - period / sc) / (DASH_EXACT - DASH_FLAT), 0.0, 1.0);\r\n let capl = select(select(0.0, 2.0 * h, cap == CAP_SQUARE), 1.5707963 * h, cap == CAP_ROUND) * sc;\r\n duty = min(1.0, styles[si].duty + styles[si].ons * capl / period);\r\n pat = select(0u, 1u, tf < 1.0);\r\n }\r\n var g: Ring;\r\n g.m = vec2f(1.0, 0.0);\r\n g.t = vec2f(0.0, 1.0);\r\n g.dist = 0.0;\r\n g.r = 1.0;\r\n var d = vec2f(1.0, 0.0);\r\n var dp = vec2f(0.0);\r\n var dn = vec2f(0.0);\r\n var near_s = false;\r\n var near_e = false;\r\n // the solid stroke\'s coverage where no end is near (its body): the strip alone\r\n var body = -1.0;\r\n if arc {\r\n // the circle from F (see the head)\r\n let f = i.seg.xy;\r\n let up = i.seg.zw;\r\n let rp = i.par.y;\r\n let orient = i.par.z;\r\n let qf = q - f;\r\n let wv = qf + rp * up;\r\n let lw = length(wv);\r\n g.dist = (dot(qf, qf) + 2.0 * rp * dot(qf, up)) / (lw + rp);\r\n if lw > 0.0 {\r\n g.m = wv / lw;\r\n } else {\r\n g.m = up;\r\n }\r\n g.t = orient * vec2f(-g.m.y, g.m.x);\r\n g.r = rp;\r\n let da = unit_or_zero((i.nb.xy - f) + rp * up);\r\n let db = unit_or_zero((i.nb.zw - f) + rp * up);\r\n let sa = dot(q - i.nb.xy, orient * vec2f(-da.y, da.x));\r\n let sb = dot(q - i.nb.zw, orient * vec2f(-db.y, db.x));\r\n let capa = (fl & 4096u) != 0u;\r\n let capb = (fl & 8192u) != 0u;\r\n if !capa && sa < 0.0 {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n if !capb && sb >= 0.0 {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n let reach = 0.71 + select(h, 0.0, cap == CAP_BUTT);\r\n if !((capa && sa < reach) || (capb && sb > -reach)) {\r\n body = max(cov(g.dist - h, g.m) + select(1.0, cov(-g.dist - h, -g.m), rp > h) - 1.0, 0.0);\r\n }\r\n } else {\r\n let s = i.seg.xy;\r\n let e = i.seg.zw;\r\n let sk = fl & 3u;\r\n let ek = (fl >> 2u) & 3u;\r\n let jk = (fl >> 8u) & 15u;\r\n let own = i.par.zw;\r\n // a pixel near a join is the nearer segment\'s (the incoming one\'s when equal, up to JOIN_TIE): see the head\r\n var dprev = 3.4e38;\r\n var dnext = 3.4e38;\r\n if sk == JOIN {\r\n dprev = dist_seg(q - s, i.nb.xy);\r\n if !(dist_seg(q - s, own) < dprev - JOIN_TIE * max(dprev, 1.0)) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n }\r\n if ek == JOIN {\r\n dnext = dist_seg(q - e, i.nb.zw);\r\n if !(dist_seg(q - e, -own) <= dnext + JOIN_TIE * max(dnext, 1.0)) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n }\r\n d = unit_or_zero(own);\r\n if !any(d != vec2f(0.0)) {\r\n d = vec2f(1.0, 0.0);\r\n }\r\n dp = unit_or_zero(-i.nb.xy);\r\n dn = unit_or_zero(i.nb.zw);\r\n // which pieces may touch the pixel (as in fs)\r\n let rr = h * 1.415 + 0.71;\r\n if sk == JOIN {\r\n let cs = dot(dp, d);\r\n let wr = h * select(1.0, max(sqrt(2.0 / max(1.0 + cs, 1e-6)), 1.0), jk == JOIN_MITER && miter_ok(cs, i.par.y)) + 0.71;\r\n near_s = dprev < rr * rr || length(q - s) < wr;\r\n }\r\n if ek == JOIN {\r\n let cs = dot(d, dn);\r\n let wr = h * select(1.0, max(sqrt(2.0 / max(1.0 + cs, 1e-6)), 1.0), jk == JOIN_MITER && miter_ok(cs, i.par.y)) + 0.71;\r\n near_e = dnext < rr * rr || length(q - e) < wr;\r\n }\r\n let u0 = dot(q - s, d);\r\n let u1 = dot(q - e, d);\r\n near_s = near_s || (sk == JOIN && u0 < 0.71);\r\n near_e = near_e || (ek == JOIN && u1 > -0.71);\r\n let lim = 0.71 + select(0.0, h, cap != CAP_BUTT);\r\n let sl = strip_l(q, s, d, h);\r\n let sr = strip_r(q, s, d, h);\r\n if !near_s && !near_e && !(sk == CAP && u0 < lim) && !(ek == CAP && u1 > -lim) {\r\n body = max(cov(sl.z, sl.xy) + cov(sr.z, sr.xy) - 1.0, 0.0);\r\n } else if !dashed && h <= TINY_H && length(own) <= TINY_LEN {\r\n // short thin strokes near their ends: as in fs\r\n var cn = 0.0;\r\n var weight = 1.0;\r\n if near_s {\r\n let sm = dp + d;\r\n let ls = length(sm);\r\n let m = select(vec2f(-dp.y, dp.x), sm / ls, ls > 1e-6);\r\n let b = cov(-dot(q - s, m), m);\r\n let np = vec2f(-dp.y, dp.x);\r\n let vp = dot(q - s, np);\r\n cn += (1.0 - b) * max(cov(vp - h, np) + cov(-vp - h, -np) - 1.0, 0.0);\r\n weight *= b;\r\n }\r\n if near_e {\r\n let sm = d + dn;\r\n let ls = length(sm);\r\n let m = select(vec2f(-d.y, d.x), sm / ls, ls > 1e-6);\r\n let b = cov(dot(q - e, m), -m);\r\n let nx = vec2f(-dn.y, dn.x);\r\n let vn = dot(q - e, nx);\r\n cn += (1.0 - b) * max(cov(vn - h, nx) + cov(-vn - h, -nx) - 1.0, 0.0);\r\n weight *= b;\r\n }\r\n var o = max(cov(sl.z, sl.xy) + cov(sr.z, sr.xy) - 1.0, 0.0);\r\n let ext = select(select(0.0, h, cap == CAP_SQUARE), 0.7854 * h, cap == CAP_ROUND);\r\n if sk == CAP {\r\n o *= cov(-u0 - ext, -d);\r\n }\r\n if ek == CAP {\r\n o *= cov(u1 - ext, d);\r\n }\r\n body = clamp(cn + weight * o, 0.0, 1.0);\r\n }\r\n }\r\n // the pattern (pass 0), the solid stroke near its ends (pass 1): one call of pieces (inlined once)\r\n var c = max(body, 0.0);\r\n var cd = 0.0;\r\n var passes = pat | select(0u, 2u, tf > 0.0 && body < 0.0);\r\n loop {\r\n if passes == 0u {\r\n break;\r\n }\r\n let pz = firstTrailingBit(passes);\r\n passes &= passes - 1u;\r\n let v = pieces(i, q, arc, pz == 1u, si, sc, d, dp, dn, near_s, near_e, g);\r\n if pz == 0u {\r\n cd = v;\r\n } else {\r\n c = v;\r\n }\r\n }\r\n if dashed {\r\n c = mix(cd, c * duty, tf);\r\n }\r\n if !(c > 0.0) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n // (the colour unpacked here, not at the top: alive through the whole body, Mesa\'s Intel compiler (26.0) gave one of\r\n // its parts to another value in some pixels, in fs_fill: a channel or the pixel wrong at random, docs/RENDER2D.md)\r\n let col = unpack4x8unorm(i.misc.y);\r\n let al = col.w * c;\r\n return vec4f(vec3f(col.z, col.y, col.x) * al, al);\r\n}\n\r\n// fill: fills and hatches (decision 26; docs/RENDER2D.md \"Fl\u{e4}chen\", \"Schraffuren\"), the third pipeline (vs_fill,\r\n// fs_fill) for the layers that hold a fill; the strokes of such a layer it draws as fs_full does (full_body).\r\n//\r\n// A fill\'s records: its rings\' edges (K_EDGE, each ring a closed polyline of them, the inside on the right in pixels:\r\n// scene.rs orients the rings) and its triangles (K_TRI, their corners the starts of edge records). The count pass\r\n// writes every edge\'s segment in pixels to `epx` (cull.wgsl: edge_px), so that whoever needs an edge reads the same\r\n// bits. Each record carries the edges near it (scene.rs, fill.rs near: within a pixel at the coarse end of the zoom\r\n// band): a triangle up to 20, an edge up to 4 besides its two neighbours. The rasterizer only brings candidates\r\n// (conservative quads); the shader decides every pixel, so that each one blends once:\r\n// - a pixel within MU of an edge, and a pixel outside the fill whose square still touches it (within FILL_R), belongs to\r\n// the nearest edge (among an edge\'s own, its neighbours and its list; ties to the incoming neighbour, else to the\r\n// record placed first)\r\n// - every other pixel inside belongs to the triangle that holds its centre: three lines per triangle, each computed by\r\n// tri_line from the same corners in the same order by both triangles at a diagonal, a centre on it to the one on its\r\n// positive side\r\n// The owner computes the pixel\'s share inside the fill exactly, by Green\'s theorem over the edges it knows: W(ref) for\r\n// a point ref of the pixel whose side is sure, plus per edge -\u{222b} (x - x_left) dy along its part inside the pixel and the\r\n// crossings of the path from ref to the pixel\'s right side and along that side (fill_contrib). Exact where the owner\r\n// knows every edge through the pixel: everywhere but where more edges than its list holds come that near (details far\r\n// below a pixel); a triangle\'s pixel farther than FILL_R from its edges is simply covered.\r\n//\r\n// Inside an insert of a block (G6b2, block.wgsl) a fill\'s records are placed through the insert\'s space, its edges\' bits\r\n// in epx lie at their place plus the entry\'s offset (ix.w; each instance its own), a mirrored insert\'s edges turned around\r\n// (cull.wgsl count) and their neighbours swapped, so that the inside stays on their right in pixels; its hatch is a row of\r\n// the table for that instance (scene.rs: the hatch\'s lines through the insert\'s transform).\r\n//\r\n// Hatches (the style\'s place in the hatch table + 1 in misc.w): per family the pixel\'s distance from the nearest line\r\n// and its place along it, from what scene.rs computed at the camera in f64, so that the numbers here stay small (no\r\n// drift at 1e7 and 1e-6 units per pixel): the line\'s strip by cov, its dashes along it by cov, the families together\r\n// as 1 - prod(1 - c); below LINE_FLAT px between lines (DASH_FLAT for a dash period) a flat tone of the lines\' share,\r\n// exact from LINE_EXACT (DASH_EXACT), mixed between. The fill\'s coverage times the hatch\'s.\r\n\r\n// a pixel this close (px) to an edge belongs to the edges; beyond FILL_R of all, a pixel\'s square touches none\r\nconst MU: f32 = 0.015625;\r\nconst FILL_R: f32 = 0.7072;\r\n// hatch lines closer than LINE_FLAT px: a flat tone; from LINE_EXACT on exactly; mixed between\r\nconst LINE_FLAT: f32 = 2.0;\r\nconst LINE_EXACT: f32 = 4.0;\r\n// misc.x of VsFill: a fill\'s record, a triangle\r\nconst M_FILL: u32 = 262144u;\r\nconst M_TRI: u32 = 524288u;\r\n\r\n// a hatched style\'s entry (scene.rs hatch_words): x the families, y the lines\' half width (px); per family f from\r\n// v[1 + 6 f]: (normal in px, the camera centre\'s distance from the nearest line px, the spacing px), (direction in px,\r\n// the place along that line at the centre\'s foot, the dash period; drawing units, period 0: solid), (the shift between\r\n// lines, the share on, the entries), then up to 12 ends of the entries from the period\'s start (drawing units)\r\nstruct Hatch {\r\n v: array<vec4f, 32>,\r\n}\r\n\r\n@group(0) @binding(5) var<storage, read> hatches: array<Hatch>;\r\n// per record of the pool: a fill edge\'s segment in pixels of the target (cull.wgsl count writes it)\r\n@group(0) @binding(6) var<storage, read> epx: array<vec4f>;\r\n\r\nstruct VsFill {\r\n @builtin(position) pos: vec4f,\r\n // strokes: as VsOut\r\n @location(0) @interpolate(flat) seg: vec4f,\r\n @location(1) @interpolate(flat) nb: vec4f,\r\n @location(2) @interpolate(flat) par: vec4f,\r\n // flags (M_*), colour, style, hatch + 1\r\n @location(3) @interpolate(flat) misc: vec4u,\r\n @location(4) @interpolate(flat) dash: vec4f,\r\n // a triangle\'s lines: normal, offset, its side (+1, -1; 0: none)\r\n @location(5) @interpolate(flat) l0: vec4f,\r\n @location(6) @interpolate(flat) l1: vec4f,\r\n @location(7) @interpolate(flat) l2: vec4f,\r\n // the record, an edge\'s previous and next one, what is added to a record\'s place for its edge in epx\r\n @location(8) @interpolate(flat) ix: vec4u,\r\n}\r\n\r\nfn fill_out() -> VsFill {\r\n var o: VsFill;\r\n o.pos = vec4f(2.0, 2.0, 0.5, 1.0);\r\n o.seg = vec4f(0.0);\r\n o.nb = vec4f(0.0);\r\n o.par = vec4f(0.0);\r\n o.misc = vec4u(0u);\r\n o.dash = vec4f(0.0);\r\n o.l0 = vec4f(0.0);\r\n o.l1 = vec4f(0.0);\r\n o.l2 = vec4f(0.0);\r\n o.ix = vec4u(0u);\r\n return o;\r\n}\r\n\r\n// word j of a record (0 to 15)\r\nfn rec_word(r: Record, j: u32) -> u32 {\r\n let q = j >> 2u;\r\n var v = r.w;\r\n if q == 0u {\r\n v = bitcast<vec4u>(r.a);\r\n } else if q == 1u {\r\n v = bitcast<vec4u>(r.b);\r\n } else if q == 2u {\r\n v = r.v;\r\n }\r\n return v[j & 3u];\r\n}\r\n\r\n// entry k of a list of u16 from word j0 on\r\nfn rec_u16(r: Record, j0: u32, k: u32) -> u32 {\r\n return (rec_word(r, j0 + (k >> 1u)) >> (16u * (k & 1u))) & 0xffffu;\r\n}\r\n\r\n// the line of a triangle\'s edge from r0 to r1 (rel2\'s pairs) in pixels of the target: its unit normal and offset\r\n// (dot(q, n) = offset on it), precise near the camera (far_foot\'s foot where both ends lie far); the triangles at a\r\n// diagonal call it with the same corners in the same order; vw: the view or the insert\'s space\r\nfn tri_line(r0: vec4f, r1: vec4f, vw: View) -> vec3f {\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 dd = vec2f((r1.x - r0.x) + (r1.z - r0.z), (r1.y - r0.y) + (r1.w - r0.w));\r\n let dp = to_px(dd, vw);\r\n let n = unit_or_zero(vec2f(-dp.y, dp.x));\r\n var foot = p0;\r\n if min(max(abs(p0.x), abs(p0.y)), max(abs(p1.x), abs(p1.y))) > FAR && length(r1.xy - r0.xy) > 0.0 {\r\n foot = far_foot(r0, r1, vw);\r\n }\r\n return vec3f(n, dot(foot + half, n));\r\n}\r\n\r\n// the line with the side the triangle\'s third corner w (px of the target) lies on\r\nfn tri_side(l: vec3f, w: vec2f) -> vec4f {\r\n let g = dot(w, l.xy) - l.z;\r\n return vec4f(l, select(select(0.0, -1.0, g < 0.0), 1.0, g > 0.0));\r\n}\r\n\r\n// the centre q on the triangle\'s side of a line (on the line: the triangle on its positive side)\r\nfn in_line(l: vec4f, q: vec2f) -> bool {\r\n let g = dot(q, l.xy) - l.z;\r\n return l.w * g > 0.0 || (g == 0.0 && l.w > 0.0);\r\n}\r\n\r\n// a fill\'s record: an edge\'s quad (its segment and FILL_R + 1 px around), a triangle\'s (its box along its longest side\r\n// and a pixel, or near a far corner the box of its corners within the target), its lines; it: the record (an insert\'s\r\n// through its space), layer_t: its layer\'s transparency\r\nfn fill_vertex(vi: u32, it: Item, r: Record, st: Style, layer_t: u32) -> VsFill {\r\n var o = fill_out();\r\n let idx = it.idx;\r\n let vw = it.vw;\r\n let half = 0.5 * vw.turn.zw;\r\n // (the colour with its transparency; the hatch: an insert\'s fill its instance\'s row of the table, the block\'s own\r\n // place of the style + 1 in the record\'s top byte)\r\n let color = ink(st.color, st.r1.x, layer_t);\r\n let local = r.w.w >> 24u;\r\n let hatch = select(st.hatch, it.hb + local, it.blk && local != 0u);\r\n if kind_of_record(r) == K_EDGE {\r\n var ip = select(idx - 1u, idx + r.w.y, (r.w.w & F_WRAP_PREV) != 0u);\r\n var inx = select(idx + 1u, idx - r.w.y, (r.w.w & F_WRAP_NEXT) != 0u);\r\n if it.mir {\r\n // (a mirrored insert\'s ring runs the other way in pixels: count turned its edges around)\r\n let t = ip;\r\n ip = inx;\r\n inx = t;\r\n }\r\n let e1 = epx[idx + it.eoff];\r\n if e1.x == -1e6 && e1.z == -1e6 {\r\n return o;\r\n }\r\n let a = e1.xy - half;\r\n let dv = e1.zw - e1.xy;\r\n let l = length(dv);\r\n var d = vec2f(1.0, 0.0);\r\n if l > 0.0 {\r\n d = dv / l;\r\n }\r\n let n = vec2f(-d.y, d.x);\r\n let w = FILL_R + 1.0;\r\n let p = a + select(-w, l + w, (vi & 1u) != 0u) * d + select(-w, w, (vi & 2u) != 0u) * n;\r\n o.pos = vec4f(p.x / half.x, -p.y / half.y, 0.5, 1.0);\r\n o.misc = vec4u(M_FILL, color, r.w.z - 1u, hatch);\r\n o.ix = vec4u(idx, ip, inx, it.eoff);\r\n return o;\r\n }\r\n // a triangle: its corners are the starts of edge records (a.yzw from the fill\'s first record, a.x back)\r\n let base = idx - bitcast<u32>(r.a.x);\r\n let ia = base + bitcast<u32>(r.a.y);\r\n let ib = base + bitcast<u32>(r.a.z);\r\n let ic = base + bitcast<u32>(r.a.w);\r\n let ra = rel2(pool[ia].a, vw);\r\n let rb = rel2(pool[ib].a, vw);\r\n let rc = rel2(pool[ic].a, vw);\r\n let qa = to_px(ra.xy, vw);\r\n let qb = to_px(rb.xy, vw);\r\n let qc = to_px(rc.xy, vw);\r\n // the lines, each from its corners in the order of their records\r\n var lab = tri_line(ra, rb, vw);\r\n if ib < ia {\r\n lab = tri_line(rb, ra, vw);\r\n }\r\n var lbc = tri_line(rb, rc, vw);\r\n if ic < ib {\r\n lbc = tri_line(rc, rb, vw);\r\n }\r\n var lca = tri_line(rc, ra, vw);\r\n if ia < ic {\r\n lca = tri_line(ra, rc, vw);\r\n }\r\n o.l0 = tri_side(lab, qc + half);\r\n o.l1 = tri_side(lbc, qa + half);\r\n o.l2 = tri_side(lca, qb + half);\r\n o.misc = vec4u(M_FILL | M_TRI, color, r.w.z - 1u, hatch);\r\n o.ix = vec4u(idx, 0u, 0u, it.eoff);\r\n var p: vec2f;\r\n if max(max(max(abs(qa.x), abs(qa.y)), max(abs(qb.x), abs(qb.y))), max(abs(qc.x), abs(qc.y))) < 4096.0 {\r\n // the box along its longest side, a pixel around\r\n var u = qa;\r\n var v = qb;\r\n var w = qc;\r\n if dot(qc - qb, qc - qb) > dot(v - u, v - u) {\r\n u = qb;\r\n v = qc;\r\n w = qa;\r\n }\r\n if dot(qa - qc, qa - qc) > dot(v - u, v - u) {\r\n u = qc;\r\n v = qa;\r\n w = qb;\r\n }\r\n let e = v - u;\r\n let le = length(e);\r\n if !(le > 0.0) {\r\n return o;\r\n }\r\n let d = e / le;\r\n var n = vec2f(-d.y, d.x);\r\n var ht = dot(w - u, n);\r\n if ht < 0.0 {\r\n n = -n;\r\n ht = -ht;\r\n }\r\n let tw = dot(w - u, d);\r\n p = u + select(min(0.0, tw) - 1.0, max(le, tw) + 1.0, (vi & 1u) != 0u) * d + select(-1.0, ht + 1.0, (vi & 2u) != 0u) * n;\r\n } else {\r\n // (a corner far away: its place in f32 is coarse; the box of the corners within the target and 2 px)\r\n let lo = max(min(min(qa, qb), qc) - vec2f(2.0), -half - vec2f(2.0));\r\n let hi = min(max(max(qa, qb), qc) + vec2f(2.0), half + vec2f(2.0));\r\n if any(lo > hi) {\r\n return o;\r\n }\r\n p = vec2f(select(lo.x, hi.x, (vi & 1u) != 0u), select(lo.y, hi.y, (vi & 2u) != 0u));\r\n }\r\n o.pos = vec4f(p.x / half.x, -p.y / half.y, 0.5, 1.0);\r\n return o;\r\n}\r\n\r\n@vertex\r\nfn vs_fill(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VsFill {\r\n let l = lists[pc.layer + (ii >> 16u)];\r\n let j = (ii & 0xffffu) * BATCH + (vi >> 2u);\r\n if j >= l.y {\r\n return fill_out();\r\n }\r\n // (decoded once: a fill\'s record here, any other as vs_full draws it)\r\n let it = item_of(visible[l.x + j], l.x + j);\r\n let r = pool[it.idx];\r\n let k = kind_of_record(r);\r\n if k == K_EDGE || k == K_TRI {\r\n return fill_vertex(vi, it, r, style_in(r.w.z - 1u, it.ins, it.blk), l.z);\r\n }\r\n let s = vertex(vi, it, l.z, true);\r\n var o = fill_out();\r\n o.pos = s.pos;\r\n o.seg = s.seg;\r\n o.nb = s.nb;\r\n o.par = s.par;\r\n o.misc = s.misc;\r\n o.dash = s.dash;\r\n return o;\r\n}\r\n\r\n// the record of a fill\'s pixel\'s k-th edge: a triangle\'s list (words 4 to 13, u16 from the fill\'s first record); an\r\n// edge\'s previous one, itself, its next one, then its list (words 11, 12)\r\nfn fill_cand(tri: bool, rec: Record, ix: vec4u, base: u32, k: u32) -> u32 {\r\n if tri {\r\n return base + rec_u16(rec, 4u, k);\r\n }\r\n if k == 0u {\r\n return ix.y;\r\n }\r\n if k == 1u {\r\n return ix.x;\r\n }\r\n if k == 2u {\r\n return ix.z;\r\n }\r\n return base + rec_u16(rec, 11u, k - 3u);\r\n}\r\n\r\n// a + t (p, q) narrowed to p t <= q (Liang-Barsky)\r\nfn lb(p: f32, q: f32, t: vec2f) -> vec2f {\r\n if p == 0.0 {\r\n if q < 0.0 {\r\n return vec2f(1.0, 0.0);\r\n }\r\n return t;\r\n }\r\n let r = q / p;\r\n if p < 0.0 {\r\n return vec2f(max(t.x, r), t.y);\r\n }\r\n return vec2f(t.x, min(t.y, r));\r\n}\r\n\r\n// an edge\'s part of the pixel\'s share (see the head; everything relative to the pixel\'s centre, the pixel [-0.5, 0.5]^2,\r\n// ref the point whose side is known): -\u{222b} (x + 0.5) dy along the edge inside the pixel, the crossing of the path from ref\r\n// to the right side (a downward edge enters the fill: +1 for all of the side), the crossing of the right side at y\r\n// (from ref.y outwards the side\'s inside changes by the edge\'s direction)\r\nfn fill_contrib(r: vec2f, a: vec2f, b: vec2f) -> f32 {\r\n let d = b - a;\r\n var t = vec2f(0.0, 1.0);\r\n t = lb(-d.x, a.x + 0.5, t);\r\n t = lb(d.x, 0.5 - a.x, t);\r\n t = lb(-d.y, a.y + 0.5, t);\r\n t = lb(d.y, 0.5 - a.y, t);\r\n var s = 0.0;\r\n if t.y > t.x {\r\n let p = a + t.x * d;\r\n let e = a + t.y * d;\r\n s = -(0.5 * (p.x + e.x) + 0.5) * (e.y - p.y);\r\n }\r\n if (a.y > r.y) != (b.y > r.y) {\r\n let xi = a.x + (r.y - a.y) * d.x / d.y;\r\n if xi > r.x && xi <= 0.5 {\r\n s += sign(d.y);\r\n }\r\n }\r\n if (a.x > 0.5) != (b.x > 0.5) {\r\n let ye = a.y + (0.5 - a.x) * d.y / d.x;\r\n if ye > r.y && ye <= 0.5 {\r\n s -= sign(d.x) * (0.5 - ye);\r\n } else if ye >= -0.5 && ye < r.y {\r\n s += sign(d.x) * (ye + 0.5);\r\n }\r\n }\r\n return s;\r\n}\r\n\r\n// a hatch family\'s line j at distance x (px) from the pixel\'s centre: its strip, times its dashes along it\r\nfn hatch_line(x: f32, j: f32, qc: vec2f, n: vec2f, hw: f32, b: vec4f, c: vec4f, slot: u32, f: u32) -> f32 {\r\n let across = max(cov(x - hw, n) + cov(-x - hw, -n) - 1.0, 0.0);\r\n let per = b.w;\r\n if across <= 0.0 || per <= 0.0 {\r\n return across;\r\n }\r\n let sc = view.rest.w;\r\n let t = b.xy;\r\n // the pixel\'s place along the line, drawing units, within [0, per)\r\n let x0 = b.z + (dot(qc, t) * sc - j * c.x);\r\n let xr = x0 - floor(x0 / per) * per;\r\n let tfa = clamp((DASH_EXACT - per / sc) / (DASH_EXACT - DASH_FLAT), 0.0, 1.0);\r\n var al = 0.0;\r\n if tfa < 1.0 {\r\n let m = u32(c.z);\r\n var a0 = 0.0;\r\n for (var k = 0u; k < m; k += 2u) {\r\n let a1 = hatches[slot].v[4u + 6u * f + (k >> 2u)][k & 3u];\r\n for (var cp = -1.0; cp < 1.5; cp += 1.0) {\r\n var ua = (a0 + cp * per - xr) / sc;\r\n var ub = (a1 + cp * per - xr) / sc;\r\n if ub - ua < 2.0 * hw {\r\n // (a dot: as long as the line is wide)\r\n let mid = 0.5 * (ua + ub);\r\n ua = mid - hw;\r\n ub = mid + hw;\r\n }\r\n al += max(cov(-ub, t) + cov(ua, -t) - 1.0, 0.0);\r\n }\r\n if k + 1u < m {\r\n a0 = hatches[slot].v[4u + 6u * f + ((k + 1u) >> 2u)][(k + 1u) & 3u];\r\n }\r\n }\r\n }\r\n return across * mix(min(al, 1.0), c.y, tfa);\r\n}\r\n\r\n// a hatch\'s coverage of the pixel at q (see the head)\r\nfn hatch_cov(slot: u32, q: vec2f) -> f32 {\r\n let qc = q - 0.5 * view.turn.zw;\r\n let hd = hatches[slot].v[0];\r\n let nf = u32(hd.x);\r\n let hw = hd.y;\r\n var keep = 1.0;\r\n for (var f = 0u; f < nf; f++) {\r\n let a = hatches[slot].v[1u + 6u * f];\r\n let b = hatches[slot].v[2u + 6u * f];\r\n let c = hatches[slot].v[3u + 6u * f];\r\n let sp = a.w;\r\n let dl = a.z + dot(qc, a.xy);\r\n let tf = clamp((LINE_EXACT - sp) / (LINE_EXACT - LINE_FLAT), 0.0, 1.0);\r\n let tone = min(1.0, 2.0 * hw / sp) * c.y;\r\n var ex = 0.0;\r\n if tf < 1.0 {\r\n let j = round(dl / sp);\r\n let x = dl - j * sp;\r\n let j2 = j + select(-1.0, 1.0, x > 0.0);\r\n ex = min(1.0, hatch_line(x, j, qc, a.xy, hw, b, c, slot, f) + hatch_line(dl - j2 * sp, j2, qc, a.xy, hw, b, c, slot, f));\r\n }\r\n keep *= 1.0 - mix(ex, tone, tf);\r\n }\r\n return 1.0 - keep;\r\n}\r\n\r\n// a fill\'s pixel (see the head)\r\nfn fill_body(i: VsFill) -> vec4f {\r\n let q = i.pos.xy;\r\n let tri = (i.misc.x & M_TRI) != 0u;\r\n let idx = i.ix.x;\r\n let rec = pool[idx];\r\n var base = 0u;\r\n var n = 0u;\r\n if tri {\r\n if !(in_line(i.l0, q) && in_line(i.l1, q) && in_line(i.l2, q)) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n base = idx - bitcast<u32>(rec.a.x);\r\n n = (rec.w.w >> 16u) & 255u;\r\n } else {\r\n base = idx - rec.v.z;\r\n n = 3u + ((rec.w.w >> 16u) & 255u);\r\n }\r\n // the squared distances from the edges, at one place for every owner (both sides of each comparison compute them alike)\r\n var dm = 3.4e38;\r\n var dp = 0.0;\r\n var dn = 0.0;\r\n var d1 = 0.0;\r\n var beaten = false;\r\n for (var k = 0u; k < n; k++) {\r\n let e = fill_cand(tri, rec, i.ix, base, k);\r\n let s = epx[e + i.ix.w];\r\n let d = dist_seg(q - s.xy, s.zw - s.xy);\r\n dm = min(dm, d);\r\n if k == 0u {\r\n dp = d;\r\n } else if k == 1u {\r\n d1 = d;\r\n } else if k == 2u {\r\n dn = d;\r\n } else if d < d1 || (d == d1 && e < idx) {\r\n // (an edge of another part of the outline, nearer: its pixel)\r\n beaten = true;\r\n }\r\n }\r\n var w = 1.0;\r\n var r = vec2f(0.0);\r\n var m = n;\r\n if tri {\r\n if dm <= MU * MU {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n if dm > FILL_R * FILL_R {\r\n // (no edge touches the pixel)\r\n m = 0u;\r\n }\r\n } else {\r\n // the nearest edge (ties to the incoming neighbour), within reach\r\n if !(d1 < dp && d1 <= dn) || beaten || d1 > FILL_R * FILL_R {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n // outside: by the side of the edge, at its end by the sum of both edges\' outer normals (the inside on the right)\r\n let s1 = epx[idx + i.ix.w];\r\n let a = s1.xy;\r\n let ab = s1.zw - a;\r\n let ll = dot(ab, ab);\r\n var t = 0.0;\r\n if ll > 0.0 {\r\n t = dot(q - a, ab) / ll;\r\n }\r\n var nrm = vec2f(-ab.y, ab.x);\r\n var o = q - a;\r\n if t >= 1.0 {\r\n let s2 = epx[i.ix.z + i.ix.w];\r\n let bn = s2.zw - s2.xy;\r\n nrm = unit_or_zero(nrm) + unit_or_zero(vec2f(-bn.y, bn.x));\r\n o = q - s1.zw;\r\n }\r\n let outside = dot(o, nrm) > 0.0;\r\n if !(outside || d1 <= MU * MU) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n w = select(1.0, 0.0, outside);\r\n if d1 < 1e-6 {\r\n // (on the edge: a point a little outside it is the one whose side is sure)\r\n r = 2e-3 * unit_or_zero(nrm);\r\n w = 0.0;\r\n }\r\n }\r\n var c = w;\r\n for (var k = 0u; k < m; k++) {\r\n let s = epx[fill_cand(tri, rec, i.ix, base, k) + i.ix.w];\r\n c += fill_contrib(r, s.xy - q, s.zw - q);\r\n }\r\n c = clamp(c, 0.0, 1.0);\r\n if i.misc.w != 0u {\r\n c *= hatch_cov(i.misc.w - 1u, q);\r\n }\r\n if !(c > 0.0) {\r\n discard;\r\n return vec4f(0.0);\r\n }\r\n // (the colour unpacked where it is returned, as in full_body)\r\n let col = unpack4x8unorm(i.misc.y);\r\n let al = col.w * c;\r\n return vec4f(vec3f(col.z, col.y, col.x) * al, al);\r\n}\r\n\r\n@fragment\r\nfn fs_fill(i: VsFill) -> @location(0) vec4f {\r\n if (i.misc.x & M_FILL) != 0u {\r\n return fill_body(i);\r\n }\r\n var o: VsOut;\r\n o.pos = i.pos;\r\n o.seg = i.seg;\r\n o.nb = i.nb;\r\n o.par = i.par;\r\n o.misc = i.misc;\r\n o.dash = i.dash;\r\n return full_body(o);\r\n}\r\n";