Skip to main content

LIBRARY

Constant LIBRARY 

Source
pub const LIBRARY: &str = "// ultrafast op library (Slang).\n//\n// Every kernel ultrafast runs is generated from these functions: single-op\n// kernels (tier 0, compiled once) and fused kernels (one per fusion group,\n// compiled in the background on structural edits).\n//\n// Conventions\n//   - Working space: linear-light, premultiplied RGBA in float.\n//   - Pixel (0,0) is top-left; positions are in output pixels, centers at +0.5.\n//   - Params live in P. P[0..4] is the frame header (time, frame, width,\n//     height); each op reads a fixed-size block starting at offset `o`.\n//   - Ctx.px is the size of one output pixel in the current coordinate space.\n//     Warps scale it so antialiasing stays one output pixel wide.\n//\n// Bindings (must match ultrafast-gpu): OUT texture(0), T0.. texture(1..),\n// S sampler(0), P buffer(0), B1.. buffer(1..).\n\n// Output is write-only: faster, and works with display formats (BGRA8).\nWTexture2D<float4> OUT;\nTexture2D<float4> T0;\nTexture2D<float4> T1;\nTexture2D<float4> T2;\nTexture2D<float4> T3;\nTexture2D<float4> T4;\nTexture2D<float4> T5;\nTexture2D<float4> T6;\nTexture2D<float4> T7;\nTexture2D<float4> T8;\nTexture2D<float4> T9;\nTexture2D<float4> T10;\nTexture2D<float4> T11;\nTexture2D<float4> T12;\nTexture2D<float4> T13;\nTexture2D<float4> T14;\nTexture2D<float4> T15;\nSamplerState S;\nStructuredBuffer<float> P;\n// Coverage accumulator for the path rasterizer: (width + 2) x height cells\n// of 16.16 fixed point signed area.\nRWStructuredBuffer<int> B1;\n// Path segments: pairs of float4 (x0, y0, x1, y1), (weight, group, 0, 0).\nStructuredBuffer<float4> B2;\n\nstatic const float PI = 3.14159265358979;\n\nstruct Ctx {\n    float2 pos;\n    float px;\n};\n\nfloat pf(uint i) { return P[i]; }\nfloat2 pf2(uint i) { return float2(P[i], P[i + 1]); }\nfloat4 pf4(uint i) { return float4(P[i], P[i + 1], P[i + 2], P[i + 3]); }\nint pi(uint i) { return int(P[i] + 0.5); }\n\nfloat frame_time() { return P[0]; }\nbool in_rect(float2 p, float4 r) { return p.x >= r.x && p.y >= r.y && p.x <= r.z && p.y <= r.w; }\nfloat2 resolution() { return float2(P[2], P[3]); }\n\n// Header [1] is the size of one output texel in frame pixels: 1 at the\n// document\'s own size, less when the frame renders larger (0.5 at twice\n// its size), more when a group renders at reduced resolution. Positions\n// are always frame pixels; only the texel step changes.\nCtx root_ctx(uint2 gid) {\n    Ctx c;\n    c.pos = (float2(gid) + 0.5) * P[1];\n    c.px = P[1];\n    return c;\n}\n\nbool outside_frame(uint2 gid) {\n    return float(gid.x) * P[1] >= P[2] || float(gid.y) * P[1] >= P[3];\n}\n\n// Texture access. Every input texture is *placed*: it covers the frame\n// starting at an origin (P[o], P[o+1], frame px) and is transparent outside\n// its own extent. Frame-sized intermediates have origin (0, 0); rasterized\n// paths and text use textures sized to their bounds.\n// `load_o` is for the identity context (no resampling: the input\'s texels\n// are the output\'s, P[1] frame px each, its origin on their grid);\n// `sample_o` resamples at a pixel position.\nfloat4 load_o(Texture2D<float4> t, uint2 gid, uint o) {\n    int2 p = int2(gid) - int2(round(pf2(o) / P[1]));\n    uint w, h;\n    t.GetDimensions(w, h);\n    if (p.x < 0 || p.y < 0 || p.x >= int(w) || p.y >= int(h)) return float4(0.0);\n    return t.Load(int3(p, 0));\n}\nfloat4 sample_o(Texture2D<float4> t, float2 pos, uint o) {\n    uint w, h;\n    t.GetDimensions(w, h);\n    return t.SampleLevel(S, (pos - pf2(o)) / float2(w, h), 0);\n}\n// A fused kernel\'s inputs also carry a texel size, P[o+2] (frame px per\n// texel): the output\'s, or more for a blur kept at the resolution it was\n// computed at, which is sampled where a texel of the output\'s would be\n// loaded.\nfloat4 load_t(Texture2D<float4> t, uint2 gid, uint o) {\n    float s = P[o + 2];\n    if (s == P[1]) return load_o(t, gid, o);\n    uint w, h;\n    t.GetDimensions(w, h);\n    return t.SampleLevel(S, ((float2(gid) + 0.5) * P[1] - pf2(o)) / (float2(w, h) * s), 0);\n}\nfloat4 sample_t(Texture2D<float4> t, float2 pos, uint o) {\n    uint w, h;\n    t.GetDimensions(w, h);\n    return t.SampleLevel(S, (pos - pf2(o)) / (float2(w, h) * P[o + 2]), 0);\n}\nfloat4 load(Texture2D<float4> t, uint2 gid) { return t.Load(int3(int2(gid), 0)); }\nfloat4 sample_px(Texture2D<float4> t, float2 pos) {\n    return t.SampleLevel(S, pos / resolution(), 0);\n}\nfloat4 sample_uv(Texture2D<float4> t, float2 uv) { return t.SampleLevel(S, uv, 0); }\n\n// ---------------------------------------------------------------- color\n\nfloat srgb_to_linear1(float x) {\n    return x <= 0.04045 ? x / 12.92 : pow((x + 0.055) / 1.055, 2.4);\n}\nfloat linear_to_srgb1(float x) {\n    x = max(x, 0.0);\n    return x <= 0.0031308 ? 12.92 * x : 1.055 * pow(x, 1.0 / 2.4) - 0.055;\n}\nfloat3 srgb_to_linear(float3 c) { return float3(srgb_to_linear1(c.r), srgb_to_linear1(c.g), srgb_to_linear1(c.b)); }\nfloat3 linear_to_srgb(float3 c) { return float3(linear_to_srgb1(c.r), linear_to_srgb1(c.g), linear_to_srgb1(c.b)); }\n\nfloat3 unpremul(float4 c) { return c.a > 1e-6 ? c.rgb / c.a : float3(0.0); }\nfloat luma(float3 c) { return dot(c, float3(0.2126, 0.7152, 0.0722)); }\n\n// Premultiplied linear <-> premultiplied sRGB-encoded.\nfloat4 to_encoded(float4 c) { return float4(linear_to_srgb(unpremul(c)) * c.a, c.a); }\nfloat4 from_encoded(float4 c) { return float4(srgb_to_linear(unpremul(c)) * c.a, c.a); }\n\n// ---------------------------------------------------------------- generators\n\n// solid [4]: color\nfloat4 g_solid(Ctx c, uint o) { return pf4(o); }\n\n// Gradients [46]: [0] stop count, [1] space (0 linear, 1 sRGB-encoded),\n// [2..6] geometry, [6..46] 8 stops of (position, premultiplied rgba in the\n// interpolation space).\nfloat4 gradient_eval(float u, uint o) {\n    int n = clamp(pi(o), 1, 8);\n    uint s = o + 6;\n    float4 c = pf4(s + 1);\n    if (u > P[s]) {\n        c = pf4(s + uint(n - 1) * 5 + 1);\n        for (int k = 0; k < n - 1; k++) {\n            uint a = s + uint(k) * 5;\n            uint b = a + 5;\n            if (u <= P[b]) {\n                float t = saturate((u - P[a]) / max(P[b] - P[a], 1e-6));\n                c = lerp(pf4(a + 1), pf4(b + 1), t);\n                break;\n            }\n        }\n    }\n    return pi(o + 1) == 1 ? from_encoded(c) : c;\n}\n\n// linear geometry: start, end\nfloat4 g_gradient_linear(Ctx c, uint o) {\n    float2 s = pf2(o + 2), e = pf2(o + 4);\n    float2 d = e - s;\n    return gradient_eval(saturate(dot(c.pos - s, d) / max(dot(d, d), 1e-6)), o);\n}\n\n// radial geometry: center, radius\nfloat4 g_gradient_radial(Ctx c, uint o) {\n    return gradient_eval(saturate(length(c.pos - pf2(o + 2)) / max(pf(o + 4), 1e-3)), o);\n}\n\n// conic geometry: center, start angle (rad)\nfloat4 g_gradient_conic(Ctx c, uint o) {\n    float2 d = c.pos - pf2(o + 2);\n    return gradient_eval(frac((atan2(d.y, d.x) - pf(o + 4)) / (2.0 * PI)), o);\n}\n\nfloat sd_round_box(float2 q, float2 b, float r) {\n    r = min(r, min(b.x, b.y));\n    float2 d = abs(q) - b + r;\n    return length(max(d, float2(0.0))) + min(max(d.x, d.y), 0.0) - r;\n}\n\nfloat sd_ellipse(float2 q, float2 r) {\n    r = max(r, float2(1e-3));\n    float k0 = length(q / r);\n    float k1 = length(q / (r * r));\n    return k0 < 1e-6 ? -min(r.x, r.y) : k0 * (k0 - 1.0) / k1;\n}\n\n// shape [13]: color, center, half size, corner radius, kind (0 rect, 1\n// ellipse), softness (px), rotation (rad), stroke width (0 = fill)\nfloat4 g_shape(Ctx c, uint o) {\n    float2 q = c.pos - pf2(o + 4);\n    float2 hs = pf2(o + 6);\n    float soft = max(pf(o + 10), 1e-3) * c.px;\n    float stroke = pf(o + 12);\n    // Cheap reject outside the rotated bounding circle.\n    float reach = length(hs) + soft + stroke;\n    if (dot(q, q) > reach * reach) return float4(0.0);\n    float cr = cos(-pf(o + 11)), sr = sin(-pf(o + 11));\n    q = float2(cr * q.x - sr * q.y, sr * q.x + cr * q.y);\n    float d = pf(o + 9) > 0.5 ? sd_ellipse(q, hs) : sd_round_box(q, hs, pf(o + 8));\n    if (stroke > 0.0) d = abs(d + stroke * 0.5) - stroke * 0.5;\n    float cov = saturate(0.5 - d / soft);\n    // A fill thinner than the filter covers its thickness, not half a\n    // pixel: shapes growing from zero start invisible.\n    float m = min(hs.x, hs.y);\n    if (stroke <= 0.0 && 2.0 * m < soft) cov *= (2.0 * m / soft) / (0.5 + m / soft);\n    return pf4(o) * cov;\n}\n\n// checker [9]: a, b, cell size\nfloat4 g_checker(Ctx c, uint o) {\n    float2 cell = floor(c.pos / max(pf(o + 8), 1.0));\n    return (int(cell.x + cell.y) & 1) == 0 ? pf4(o) : pf4(o + 4);\n}\n\n// Simplex noise 3D (Gustavson / Ashima, MIT).\nfloat3 mod289(float3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\nfloat4 mod289_4(float4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }\nfloat4 permute(float4 x) { return mod289_4(((x * 34.0) + 10.0) * x); }\nfloat4 taylor_inv_sqrt(float4 r) { return 1.79284291400159 - 0.85373472095314 * r; }\n\nfloat snoise(float3 v) {\n    const float2 C = float2(1.0 / 6.0, 1.0 / 3.0);\n    float3 i = floor(v + dot(v, C.yyy));\n    float3 x0 = v - i + dot(i, C.xxx);\n    float3 g = step(x0.yzx, x0.xyz);\n    float3 l = 1.0 - g;\n    float3 i1 = min(g.xyz, l.zxy);\n    float3 i2 = max(g.xyz, l.zxy);\n    float3 x1 = x0 - i1 + C.xxx;\n    float3 x2 = x0 - i2 + C.yyy;\n    float3 x3 = x0 - 0.5;\n    i = mod289(i);\n    float4 p = permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0)) + i.y + float4(0.0, i1.y, i2.y, 1.0))\n                       + i.x + float4(0.0, i1.x, i2.x, 1.0));\n    float4 j = p - 49.0 * floor(p * (1.0 / 49.0));\n    float4 x_ = floor(j * (1.0 / 7.0));\n    float4 y_ = floor(j - 7.0 * x_);\n    float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0;\n    float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0;\n    float4 h = 1.0 - abs(x) - abs(y);\n    float4 b0 = float4(x.xy, y.xy);\n    float4 b1 = float4(x.zw, y.zw);\n    float4 s0 = floor(b0) * 2.0 + 1.0;\n    float4 s1 = floor(b1) * 2.0 + 1.0;\n    float4 sh = -step(h, float4(0.0));\n    float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;\n    float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;\n    float3 g0 = float3(a0.xy, h.x);\n    float3 g1 = float3(a0.zw, h.y);\n    float3 g2 = float3(a1.xy, h.z);\n    float3 g3 = float3(a1.zw, h.w);\n    float4 norm = taylor_inv_sqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3)));\n    g0 *= norm.x; g1 *= norm.y; g2 *= norm.z; g3 *= norm.w;\n    float4 m = max(0.5 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);\n    m = m * m;\n    return 105.0 * dot(m * m, float4(dot(g0, x0), dot(g1, x1), dot(g2, x2), dot(g3, x3)));\n}\n\n// noise [14]: a, b, scale (px per cell), offset, z (evolution), octaves\nfloat4 g_noise(Ctx c, uint o) {\n    float2 p = (c.pos + pf2(o + 10)) / max(pf2(o + 8), float2(1e-3));\n    float z = pf(o + 12);\n    int oct = clamp(pi(o + 13), 1, 8);\n    float sum = 0.0, amp = 0.5, norm = 0.0;\n    float3 q = float3(p, z);\n    for (int k = 0; k < oct; k++) {\n        sum += amp * snoise(q);\n        norm += amp;\n        q = q * 2.0 + float3(17.1, 31.7, 0.0);\n        amp *= 0.5;\n    }\n    float u = saturate(0.5 + 0.5 * sum / norm);\n    return lerp(pf4(o), pf4(o + 4), u);\n}\n\n// image [10]: dest rect (x, y, w, h) in px, opacity, interpolation space\n// (0 linear, 1 sRGB-encoded), the part of the image shown in the rect\n// (u0, v0, u1, v1; 0,0,1,1 is all of it). `t` is the prepared (linear,\n// premultiplied) image texture.\nfloat4 texel_encoded(Texture2D<float4> t, int2 p, uint2 dim) {\n    if (p.x < 0 || p.y < 0 || p.x >= int(dim.x) || p.y >= int(dim.y)) return float4(0.0);\n    return to_encoded(t.Load(int3(p, 0)));\n}\nfloat4 g_image(Ctx c, uint o, Texture2D<float4> t) {\n    float4 r = pf4(o);\n    float2 uv = (c.pos - r.xy) / max(r.zw, float2(1e-3));\n    float4 win = pf4(o + 6);\n    float cover = 1.0;\n    if (win.x > 0.0 || win.y > 0.0 || win.z < 1.0 || win.w < 1.0) {\n        // Part of the image (cover in a box): the rect crops it, its edges\n        // antialiased over a pixel as the image\'s own edges are.\n        float2 d = min(c.pos - r.xy, r.xy + r.zw - c.pos);\n        cover = saturate(min(d.x, d.y) + 0.5);\n        if (cover <= 0.0) return float4(0.0);\n        uv = win.xy + uv * (win.zw - win.xy);\n    }\n    if (pi(o + 5) == 1) {\n        // Bilinear on the encoded values, as browsers scale images.\n        uint w, h;\n        t.GetDimensions(w, h);\n        uint2 dim = uint2(w, h);\n        float2 p = uv * float2(dim) - 0.5;\n        int2 i = int2(floor(p));\n        float2 f = p - floor(p);\n        float4 a = lerp(texel_encoded(t, i, dim), texel_encoded(t, i + int2(1, 0), dim), f.x);\n        float4 b = lerp(texel_encoded(t, i + int2(0, 1), dim), texel_encoded(t, i + int2(1, 1), dim), f.x);\n        return from_encoded(lerp(a, b, f.y)) * (pf(o + 4) * cover);\n    }\n    return sample_uv(t, uv) * (pf(o + 4) * cover);\n}\n\n// ---------------------------------------------------------------- pointwise\n\n// opacity [1]\nfloat4 f_opacity(Ctx c, float4 a, uint o) { return a * pf(o); }\n\n// over [2]: fg opacity, space (0 linear, 1 sRGB-encoded)\nfloat4 f_over(Ctx c, float4 fg, float4 bg, uint o) {\n    fg *= pf(o);\n    if (pi(o + 1) == 1) {\n        float4 f = to_encoded(fg), b = to_encoded(bg);\n        return from_encoded(f + b * (1.0 - f.a));\n    }\n    return fg + bg * (1.0 - fg.a);\n}\n\nfloat3 set_lum(float3 c, float l) {\n    float d = l - luma(c);\n    c += d;\n    float lc = luma(c);\n    float n = min(min(c.r, c.g), c.b);\n    float x = max(max(c.r, c.g), c.b);\n    if (n < 0.0) c = lc + (c - lc) * lc / max(lc - n, 1e-6);\n    if (x > 1.0) c = lc + (c - lc) * (1.0 - lc) / max(x - lc, 1e-6);\n    return c;\n}\nfloat sat(float3 c) { return max(max(c.r, c.g), c.b) - min(min(c.r, c.g), c.b); }\nfloat3 set_sat(float3 c, float s) {\n    float mx = max(max(c.r, c.g), c.b);\n    float mn = min(min(c.r, c.g), c.b);\n    float range = mx - mn;\n    if (range <= 1e-6) return float3(0.0);\n    return (c - mn) * s / range;\n}\n\nfloat soft_light1(float b, float s) {\n    if (s <= 0.5) return b - (1.0 - 2.0 * s) * b * (1.0 - b);\n    float d = b <= 0.25 ? ((16.0 * b - 12.0) * b + 4.0) * b : sqrt(b);\n    return b + (2.0 * s - 1.0) * (d - b);\n}\nfloat dodge1(float b, float s) { return b <= 0.0 ? 0.0 : (s >= 1.0 ? 1.0 : min(1.0, b / (1.0 - s))); }\nfloat burn1(float b, float s) { return b >= 1.0 ? 1.0 : (s <= 0.0 ? 0.0 : 1.0 - min(1.0, (1.0 - b) / s)); }\nfloat hard1(float b, float s) { return s <= 0.5 ? b * 2.0 * s : 1.0 - (1.0 - b) * (1.0 - (2.0 * s - 1.0)); }\n\n// W3C compositing blend functions B(cb, cs), straight color.\nfloat3 blend_fn(int m, float3 b, float3 s) {\n    switch (m) {\n    case 1: return b * s;\n    case 2: return b + s - b * s;\n    case 3: return float3(hard1(s.r, b.r), hard1(s.g, b.g), hard1(s.b, b.b));\n    case 4: return min(b, s);\n    case 5: return max(b, s);\n    case 6: return float3(dodge1(b.r, s.r), dodge1(b.g, s.g), dodge1(b.b, s.b));\n    case 7: return float3(burn1(b.r, s.r), burn1(b.g, s.g), burn1(b.b, s.b));\n    case 8: return float3(hard1(b.r, s.r), hard1(b.g, s.g), hard1(b.b, s.b));\n    case 9: return float3(soft_light1(b.r, s.r), soft_light1(b.g, s.g), soft_light1(b.b, s.b));\n    case 10: return abs(b - s);\n    case 11: return b + s - 2.0 * b * s;\n    case 12: return set_lum(set_sat(s, sat(b)), luma(b));\n    case 13: return set_lum(set_sat(b, sat(s)), luma(b));\n    case 14: return set_lum(s, luma(b));\n    case 15: return set_lum(b, luma(s));\n    case 16: return b + s;\n    case 17: return max(b - s, float3(0.0));\n    case 18: return float3(s.r > 0 ? b.r / s.r : 1.0, s.g > 0 ? b.g / s.g : 1.0, s.b > 0 ? b.b / s.b : 1.0);\n    default: return s;\n    }\n}\n\n// blend [3]: mode, fg opacity, space (0 linear, 1 sRGB-encoded)\nfloat4 f_blend(Ctx c, float4 src, float4 dst, uint o) {\n    int mode = pi(o);\n    bool enc = pi(o + 2) == 1;\n    src *= pf(o + 1);\n    if (enc) { src = to_encoded(src); dst = to_encoded(dst); }\n    float3 cs = unpremul(src), cb = unpremul(dst);\n    float3 mixed = (1.0 - dst.a) * cs + dst.a * blend_fn(mode, cb, cs);\n    float4 r = float4(src.a * mixed + (1.0 - src.a) * dst.rgb, src.a + dst.a * (1.0 - src.a));\n    return enc ? from_encoded(r) : r;\n}\n\n// composite [1]: Porter-Duff op (0 in, 1 out, 2 atop, 3 xor, 4 plus, 5 over)\nfloat4 f_composite(Ctx c, float4 s, float4 d, uint o) {\n    switch (pi(o)) {\n    case 0: return s * d.a;\n    case 1: return s * (1.0 - d.a);\n    case 2: return s * d.a + d * (1.0 - s.a);\n    case 3: return s * (1.0 - d.a) + d * (1.0 - s.a);\n    case 4: return min(s + d, float4(1.0e4));\n    default: return s + d * (1.0 - s.a);\n    }\n}\n\n// mask [1]: mode (0 alpha, 1 inverted alpha, 2 luma, 3 inverted luma)\nfloat4 f_mask(Ctx c, float4 src, float4 m, uint o) {\n    int mode = pi(o);\n    float k = mode >= 2 ? luma(unpremul(m)) * m.a : m.a;\n    if (mode == 1 || mode == 3) k = 1.0 - k;\n    return src * k;\n}\n\n// mix [1]: t (0 = a, 1 = b)\nfloat4 f_mix(Ctx c, float4 a, float4 b, uint o) { return lerp(a, b, pf(o)); }\n\n// adjust [6]: exposure (stops, linear), contrast, saturation, hue (rad),\n// brightness, gamma. All but exposure act on sRGB-encoded straight color.\nfloat4 f_adjust(Ctx c, float4 v, uint o) {\n    if (v.a <= 0.0) return v;\n    float3 rgb = unpremul(v) * exp2(pf(o));\n    rgb = linear_to_srgb(rgb);\n    rgb = (rgb - 0.5) * pf(o + 1) + 0.5 + pf(o + 4);\n    float l = luma(rgb);\n    rgb = lerp(float3(l), rgb, pf(o + 2));\n    float h = pf(o + 3);\n    if (h != 0.0) {\n        // Rotate around the grey axis (Rodrigues).\n        float3 k = float3(0.57735);\n        float ch = cos(h), sh = sin(h);\n        rgb = rgb * ch + cross(k, rgb) * sh + k * dot(k, rgb) * (1.0 - ch);\n    }\n    rgb = pow(max(rgb, float3(0.0)), float3(1.0 / max(pf(o + 5), 1e-3)));\n    return float4(srgb_to_linear(saturate(rgb)) * v.a, v.a);\n}\n\n// levels [5]: in black, in white, gamma, out black, out white (encoded)\nfloat4 f_levels(Ctx c, float4 v, uint o) {\n    if (v.a <= 0.0) return v;\n    float3 e = linear_to_srgb(unpremul(v));\n    e = saturate((e - pf(o)) / max(pf(o + 1) - pf(o), 1e-4));\n    e = pow(e, float3(1.0 / max(pf(o + 2), 1e-3)));\n    e = lerp(float3(pf(o + 3)), float3(pf(o + 4)), e);\n    return float4(srgb_to_linear(e) * v.a, v.a);\n}\n\n// color_matrix [20]: 4x5 row-major on straight linear RGBA\nfloat4 f_color_matrix(Ctx c, float4 v, uint o) {\n    float4 s = float4(unpremul(v), v.a);\n    float4 r;\n    for (int i = 0; i < 4; i++) {\n        uint b = o + uint(i) * 5;\n        r[i] = dot(s, float4(P[b], P[b + 1], P[b + 2], P[b + 3])) + P[b + 4];\n    }\n    r.a = saturate(r.a);\n    return float4(max(r.rgb, float3(0.0)) * r.a, r.a);\n}\n\n// lut [3]: amount, size N, space (0: LUT expects sRGB-encoded values, 1:\n// linear). `t` holds the 3D LUT as N*N x N texels: blue slices side by side,\n// red along x, green along y. Trilinear: bilinear within two blue slices.\nfloat4 f_lut(Ctx c, float4 v, uint o, Texture2D<float4> t) {\n    if (v.a <= 0.0) return v;\n    float n = P[o + 1];\n    if (n < 2.0) return v;\n    bool encoded = P[o + 2] < 0.5;\n    float3 straight = unpremul(v);\n    float3 x = saturate(encoded ? linear_to_srgb(straight) : straight) * (n - 1.0);\n    float b0 = floor(x.b);\n    float b1 = min(b0 + 1.0, n - 1.0);\n    float fb = x.b - b0;\n    float2 size = float2(n * n, n);\n    float2 rg = float2(x.r, x.g) + 0.5;\n    float3 c0 = t.SampleLevel(S, (rg + float2(b0 * n, 0.0)) / size, 0).rgb;\n    float3 c1 = t.SampleLevel(S, (rg + float2(b1 * n, 0.0)) / size, 0).rgb;\n    float3 graded = lerp(c0, c1, fb);\n    if (encoded) graded = srgb_to_linear(saturate(graded));\n    return float4(lerp(straight, graded, P[o]) * v.a, v.a);\n}\n\n// invert [1]: amount (encoded rgb)\nfloat4 f_invert(Ctx c, float4 v, uint o) {\n    if (v.a <= 0.0) return v;\n    float3 e = linear_to_srgb(unpremul(v));\n    e = lerp(e, 1.0 - e, pf(o));\n    return float4(srgb_to_linear(e) * v.a, v.a);\n}\n\n// tint [8]: color for shadows, color for highlights (premultiplied linear;\n// only their rgb is used), mapped by luminance\nfloat4 f_tint(Ctx c, float4 v, uint o) {\n    float l = luma(linear_to_srgb(unpremul(v)));\n    float3 rgb = lerp(unpremul(pf4(o)), unpremul(pf4(o + 4)), l);\n    return float4(rgb * v.a, v.a);\n}\n\n// threshold [2]: level, softness (encoded luminance)\nfloat4 f_threshold(Ctx c, float4 v, uint o) {\n    float l = luma(linear_to_srgb(unpremul(v)));\n    float w = max(pf(o + 1), 1e-4);\n    float k = smoothstep(pf(o) - w, pf(o) + w, l);\n    return float4(float3(k) * v.a, v.a);\n}\n\n// crop [5]: x, y, w, h, softness (px)\nfloat4 f_crop(Ctx c, float4 v, uint o) {\n    float4 r = pf4(o);\n    float2 hs = r.zw * 0.5;\n    float d = sd_round_box(c.pos - (r.xy + hs), hs, 0.0);\n    return v * saturate(0.5 - d / (max(pf(o + 4), 1e-3) * c.px));\n}\n\n// ---------------------------------------------------------------- warps\n// A warp maps the output context to the context its input is evaluated in.\n\n// transform [7]: inverse affine (a, b, c, d, e, f): p\' = (a*x + c*y + e, b*x + d*y + f),\n// footprint scale\nCtx w_transform(Ctx c, uint o) {\n    Ctx r;\n    r.pos = float2(P[o] * c.pos.x + P[o + 2] * c.pos.y + P[o + 4],\n                   P[o + 1] * c.pos.x + P[o + 3] * c.pos.y + P[o + 5]);\n    r.px = c.px * P[o + 6];\n    return r;\n}\n\n// corner_pin [10]: inverse homography (row-major 3x3), footprint scale\nCtx w_corner_pin(Ctx c, uint o) {\n    float3 p = float3(c.pos, 1.0);\n    float3 q = float3(dot(float3(P[o], P[o + 1], P[o + 2]), p),\n                      dot(float3(P[o + 3], P[o + 4], P[o + 5]), p),\n                      dot(float3(P[o + 6], P[o + 7], P[o + 8]), p));\n    Ctx r;\n    // Points behind the projection map far outside (transparent).\n    r.pos = q.z > 1e-6 ? q.xy / q.z : float2(-1e9);\n    r.px = c.px * P[o + 9];\n    return r;\n}\n\n// wave [4]: amplitude (px), wavelength (px), phase (rad), direction (rad)\nCtx w_wave(Ctx c, uint o) {\n    float a = pf(o + 3);\n    float2 dir = float2(cos(a), sin(a));\n    float2 perp = float2(-dir.y, dir.x);\n    Ctx r = c;\n    r.pos += perp * pf(o) * sin(dot(c.pos, dir) * 2.0 * PI / max(pf(o + 1), 1e-3) + pf(o + 2));\n    return r;\n}\n\n// twirl [4]: center, radius, angle (rad)\nCtx w_twirl(Ctx c, uint o) {\n    float2 d = c.pos - pf2(o);\n    float r = length(d);\n    float k = saturate(1.0 - r / max(pf(o + 2), 1e-3));\n    float a = pf(o + 3) * k * k;\n    float ca = cos(a), sa = sin(a);\n    Ctx out_c = c;\n    out_c.pos = pf2(o) + float2(ca * d.x - sa * d.y, sa * d.x + ca * d.y);\n    return out_c;\n}\n\n// tile [4]: origin, size: repeats that region across the frame\nCtx w_tile(Ctx c, uint o) {\n    float2 org = pf2(o), size = max(pf2(o + 2), float2(1.0));\n    Ctx r = c;\n    float2 rel = c.pos - org;\n    r.pos = org + rel - floor(rel / size) * size;\n    return r;\n}\n\n// displace [2]: amount (px) along x and y, driven by a map\'s red and green\nCtx w_displace(Ctx c, float4 map, uint o) {\n    float3 m = unpremul(map);\n    Ctx r = c;\n    r.pos += (m.rg - 0.5) * 2.0 * pf2(o);\n    return r;\n}\n\n// ---------------------------------------------------------------- barriers\n\n// Separable gaussian blur. Params [4]: sigma (px), direction x, direction y,\n// unused; [8..10] input origin. Uses linear sampling to fetch two taps per\n// sample.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_blur(uint3 gid: SV_DispatchThreadID) {\n    if (outside_frame(gid.xy)) return;\n    float sigma = max(pf(4), 1e-3);\n    float2 dir = pf2(5);\n    float2 pos = float2(gid.xy) + 0.5;\n    int radius = int(ceil(sigma * 3.0));\n    float inv2s2 = 1.0 / (2.0 * sigma * sigma);\n    float4 acc = sample_o(T0, pos, 8);\n    float wsum = 1.0;\n    for (int i = 1; i <= radius; i += 2) {\n        float w0 = exp(-float(i * i) * inv2s2);\n        float w1 = exp(-float((i + 1) * (i + 1)) * inv2s2);\n        float w = w0 + w1;\n        float off = float(i) + w1 / max(w, 1e-8);\n        acc += (sample_o(T0, pos + dir * off, 8) + sample_o(T0, pos - dir * off, 8)) * w;\n        wsum += 2.0 * w;\n    }\n    OUT.Store(gid.xy, acc / wsum);\n}\n\n// Converts an uploaded RGBA8 sRGB straight-alpha image into the working\n// space. Params [1]: 1 if the source is already premultiplied.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_prep_srgb8(uint3 gid: SV_DispatchThreadID) {\n    uint w, h;\n    OUT.GetDimensions(w, h);\n    if (gid.x >= w || gid.y >= h) return;\n    float4 c = T0.Load(int3(int2(gid.xy), 0));\n    float3 rgb = pf(4) > 0.5 ? (c.a > 0.0 ? c.rgb / c.a : float3(0.0)) : c.rgb;\n    OUT.Store(gid.xy, float4(srgb_to_linear(rgb) * c.a, c.a));\n}\n\n// A decoded video frame -> working space, upright. P[4]: layout (0: the\n// texture T0, 1: BGRA bytes in B1, 2: 8-bit YCbCr 4:2:0 in B1, planes\n// tightly packed, 3: the same with Cb and Cr interleaved (NV12)), [5]\n// clockwise rotation in degrees, [6..8] coded size,\n// [8] Kr and [9] Kb of the YCbCr matrix, [10] 1 for full-range samples.\n// Chroma is sited left of its luma pair and between rows (H.264\'s default)\n// and interpolated bilinearly.\nuint video_byte(uint i) { return (uint(B1[i >> 2]) >> ((i & 3u) * 8u)) & 0xFFu; }\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_prep_video(uint3 gid: SV_DispatchThreadID) {\n    uint w, h;\n    OUT.GetDimensions(w, h);\n    if (gid.x >= w || gid.y >= h) return;\n    int rot = pi(5);\n    uint cw = uint(P[6]);\n    uint ch = uint(P[7]);\n    uint2 c = gid.xy;\n    if (rot == 90) c = uint2(gid.y, ch - 1 - gid.x);\n    else if (rot == 180) c = uint2(cw - 1 - gid.x, ch - 1 - gid.y);\n    else if (rot == 270) c = uint2(cw - 1 - gid.y, gid.x);\n    int layout = pi(4);\n    if (layout == 0) {\n        float4 t = T0.Load(int3(int2(c), 0));\n        OUT.Store(gid.xy, float4(srgb_to_linear(t.rgb) * t.a, t.a));\n        return;\n    }\n    if (layout == 1) {\n        uint v = uint(B1[c.y * cw + c.x]);\n        float4 t = float4(float((v >> 16) & 0xFFu), float((v >> 8) & 0xFFu), float(v & 0xFFu), float(v >> 24)) / 255.0;\n        OUT.Store(gid.xy, float4(srgb_to_linear(t.rgb) * t.a, t.a));\n        return;\n    }\n    uint cw2 = (cw + 1) / 2;\n    uint ch2 = (ch + 1) / 2;\n    uint u0 = cw * ch;\n    uint v0 = u0 + cw2 * ch2;\n    float fx = float(c.x) * 0.5;\n    float fy = max(float(c.y) * 0.5 - 0.25, 0.0);\n    uint x0 = min(uint(fx), cw2 - 1);\n    uint y0 = min(uint(fy), ch2 - 1);\n    uint x1 = min(x0 + 1, cw2 - 1);\n    uint y1 = min(y0 + 1, ch2 - 1);\n    float tx = fx - float(x0);\n    float ty = fy - float(y0);\n    // Byte offsets of a chroma sample\'s Cb and Cr.\n    bool nv12 = layout == 3;\n    uint ustep = nv12 ? 2u : 1u;\n    uint vbase = nv12 ? u0 + 1u : v0;\n    uint row = cw2 * ustep;\n    float2 c00 = float2(float(video_byte(u0 + y0 * row + x0 * ustep)), float(video_byte(vbase + y0 * row + x0 * ustep)));\n    float2 c10 = float2(float(video_byte(u0 + y0 * row + x1 * ustep)), float(video_byte(vbase + y0 * row + x1 * ustep)));\n    float2 c01 = float2(float(video_byte(u0 + y1 * row + x0 * ustep)), float(video_byte(vbase + y1 * row + x0 * ustep)));\n    float2 c11 = float2(float(video_byte(u0 + y1 * row + x1 * ustep)), float(video_byte(vbase + y1 * row + x1 * ustep)));\n    float2 cbcr = lerp(lerp(c00, c10, tx), lerp(c01, c11, tx), ty) - 128.0;\n    float y = float(video_byte(c.y * cw + c.x));\n    bool full = P[10] > 0.5;\n    y = full ? y / 255.0 : (y - 16.0) / 219.0;\n    cbcr /= full ? 255.0 : 224.0;\n    float kr = P[8];\n    float kb = P[9];\n    float r = y + 2.0 * (1.0 - kr) * cbcr.y;\n    float b = y + 2.0 * (1.0 - kb) * cbcr.x;\n    float g = (y - kr * r - kb * b) / (1.0 - kr - kb);\n    OUT.Store(gid.xy, float4(srgb_to_linear(saturate(float3(r, g, b))), 1.0));\n}\n\n// Final pass: working space -> sRGB-encoded straight-alpha RGBA (8-bit or\n// half float, whatever OUT is). Params [4..6]: input origin; [6] 0 =\n// straight alpha, 1 = flatten over black (opaque video), 2 = linear with\n// straight alpha (EXR: its encoder premultiplies), 3 = linear over black.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_encode_srgb8(uint3 gid: SV_DispatchThreadID) {\n    if (outside_frame(gid.xy)) return;\n    float4 c = load_o(T0, gid.xy, 4);\n    if (P[6] > 2.5) {\n        OUT.Store(gid.xy, float4(max(c.rgb, 0.0), 1.0));\n        return;\n    }\n    if (P[6] > 1.5) {\n        OUT.Store(gid.xy, float4(max(unpremul(c), 0.0), saturate(c.a)));\n        return;\n    }\n    if (P[6] > 0.5) {\n        OUT.Store(gid.xy, float4(linear_to_srgb(saturate(c.rgb)), 1.0));\n        return;\n    }\n    float a = saturate(c.a);\n    OUT.Store(gid.xy, float4(linear_to_srgb(saturate(unpremul(c))), a));\n}\n\n// Encodes the final image as NV12 (BT.709, limited range) into B1, the\n// layout hardware encoders read: a Y plane of `pitch` x `height` bytes,\n// then interleaved Cb Cr at half resolution. Values are sRGB-encoded first,\n// as on every other export path, but converted from float with no 8-bit\n// RGB step. One thread per 4x2 block writes whole 32-bit words.\n// P: header, origin (4, 5), _ (6), pitch (7), height rounded up to even (8).\nfloat3 rgb_to_ycbcr709(float3 c) {\n    float y = dot(c, float3(0.2126, 0.7152, 0.0722));\n    return float3(y, (c.b - y) / 1.8556, (c.r - y) / 1.5748);\n}\nuint pack4(float4 v) {\n    uint4 b = uint4(clamp(round(v), 0.0, 255.0));\n    return b.x | (b.y << 8) | (b.z << 16) | (b.w << 24);\n}\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_encode_nv12(uint3 gid: SV_DispatchThreadID) {\n    uint pitch = uint(P[7]);\n    uint h2 = uint(P[8]);\n    uint x0 = gid.x * 4;\n    uint y0 = gid.y * 2;\n    if (x0 >= pitch || y0 >= h2) return;\n    int2 last = int2(int(P[2]) - 1, int(P[3]) - 1);\n    float4 ys[2];\n    // Sums of the two 2x2 chroma blocks.\n    float3 left = float3(0.0);\n    float3 right = float3(0.0);\n    for (uint j = 0; j < 2; j++) {\n        float3 c4[4];\n        for (uint i = 0; i < 4; i++) {\n            uint2 p = uint2(min(int2(x0 + i, y0 + j), last));\n            // Opaque output: premultiplied color is the image over black.\n            c4[i] = linear_to_srgb(saturate(load_o(T0, p, 4).rgb));\n            ys[j][i] = 16.0 + 219.0 * dot(c4[i], float3(0.2126, 0.7152, 0.0722));\n        }\n        left += c4[0] + c4[1];\n        right += c4[2] + c4[3];\n    }\n    float3 a = rgb_to_ycbcr709(left * 0.25);\n    float3 b = rgb_to_ycbcr709(right * 0.25);\n    float4 uv = float4(128.0 + 224.0 * a.y, 128.0 + 224.0 * a.z, 128.0 + 224.0 * b.y, 128.0 + 224.0 * b.z);\n    B1[(y0 * pitch + x0) / 4] = int(pack4(ys[0]));\n    B1[((y0 + 1) * pitch + x0) / 4] = int(pack4(ys[1]));\n    B1[(h2 * pitch + (y0 / 2) * pitch + x0) / 4] = int(pack4(uv));\n}\n\n// Clears the output to transparent (used when a kernel is unavailable).\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_clear(uint3 gid: SV_DispatchThreadID) {\n    if (outside_frame(gid.xy)) return;\n    OUT.Store(gid.xy, float4(0.0));\n}\n\n// ---------------------------------------------------------------- paths\n// Exact-area coverage rasterization (the font-rs accumulation algorithm),\n// on the GPU: every segment deposits signed area into the accumulator\n// (one thread per segment), then each row is prefix-summed into coverage.\n// Fixed-point integer atomics keep it portable (no float atomics needed).\n\nstatic const float ACC_SCALE = 65536.0;\n\nvoid acc_add(int idx, float v) {\n    int dummy;\n    InterlockedAdd(B1[idx], int(round(v * ACC_SCALE)), dummy);\n}\n\n// Params: [4] segment count, [5..7] region size, [7..9] region origin in\n// the frame. The accumulator covers the region, which may extend past the\n// frame (content placed off-screen and moved in by later transforms), once\n// per color group (one plane after another).\n[shader(\"compute\")]\n[numthreads(64, 1, 1)]\nvoid k_raster_lines(uint3 tid: SV_DispatchThreadID) {\n    uint n = uint(P[4]);\n    if (tid.x >= n) return;\n    int w = int(P[5]);\n    int h = int(P[6]);\n    int stride = w + 2;\n    float4 seg = B2[tid.x * 2] - float4(P[7], P[8], P[7], P[8]);\n    float4 meta = B2[tid.x * 2 + 1];\n    float weight = meta.x;\n    int plane = int(meta.y + 0.5) * stride * h;\n    // Clamp x into the frame: area left of x = 0 lands in column 0, which\n    // the row prefix sum carries to every pixel on its right.\n    float2 p0 = float2(clamp(seg.x, 0.0, float(w)), seg.y);\n    float2 p1 = float2(clamp(seg.z, 0.0, float(w)), seg.w);\n    if (p0.y == p1.y) return;\n    float dir = weight;\n    if (p0.y > p1.y) {\n        float2 t = p0; p0 = p1; p1 = t;\n        dir = -weight;\n    }\n    if (p1.y <= 0.0 || p0.y >= float(h)) return;\n    float dxdy = (p1.x - p0.x) / (p1.y - p0.y);\n    float x = p0.x;\n    if (p0.y < 0.0) x -= p0.y * dxdy;\n    int y0 = max(int(floor(p0.y)), 0);\n    int y1 = min(int(ceil(p1.y)), h);\n    for (int y = y0; y < y1; y++) {\n        int line = plane + y * stride;\n        float dy = min(float(y + 1), p1.y) - max(float(y), p0.y);\n        float xnext = x + dxdy * dy;\n        float d = dy * dir;\n        float x0 = min(x, xnext);\n        float x1 = max(x, xnext);\n        float x0floor = floor(x0);\n        int x0i = int(x0floor);\n        float x1ceil = ceil(x1);\n        int x1i = int(x1ceil);\n        if (x1i <= x0i + 1) {\n            float xmf = 0.5 * (x + xnext) - x0floor;\n            acc_add(line + x0i, d - d * xmf);\n            acc_add(line + x0i + 1, d * xmf);\n        } else {\n            float s = 1.0 / (x1 - x0);\n            float x0f = x0 - x0floor;\n            float a0 = 0.5 * s * (1.0 - x0f) * (1.0 - x0f);\n            float x1f = x1 - x1ceil + 1.0;\n            float am = 0.5 * s * x1f * x1f;\n            acc_add(line + x0i, d * a0);\n            if (x1i == x0i + 2) {\n                acc_add(line + x0i + 1, d * (1.0 - a0 - am));\n            } else {\n                float a1 = s * (1.5 - x0f);\n                acc_add(line + x0i + 1, d * (a1 - a0));\n                for (int xi = x0i + 2; xi < x1i - 1; xi++) {\n                    acc_add(line + xi, d * s);\n                }\n                float a2 = a1 + float(x1i - x0i - 3) * s;\n                acc_add(line + x1i - 1, d * (1.0 - a2 - am));\n            }\n            acc_add(line + x1i, d * am);\n        }\n        x = xnext;\n    }\n}\n\n// Copies T0 into OUT at (P[0], P[1]): a color glyph bitmap into the glyph\n// atlas. P[2..4] is its size.\n[shader(\"compute\")]\n[numthreads(8, 8, 1)]\nvoid k_atlas_put(uint3 id: SV_DispatchThreadID) {\n    if (id.x >= uint(P[2]) || id.y >= uint(P[3])) return;\n    OUT.Store(id.xy + uint2(uint(P[0]), uint(P[1])), T0.Load(int3(id.xy, 0)));\n}\n\ngroupshared float row_sums[256];\n\n// Color glyphs (emoji) over resolved type at region texel (x, y). From\n// P[ib]: glyph count, raster origin (frame px), opacity. Per glyph, three\n// float4s in B2: the map from frame px to the bitmap\'s unit square (u\'s\n// x, y and constant terms, then weight; v\'s terms), and its rect in the\n// atlas T0 (x, y, w, h texels). Minified bitmaps are box filtered over\n// the pixel\'s footprint.\nfloat4 glyph_images(float4 res, int x, int y, int ib) {\n    int n = int(P[ib] + 0.5);\n    float3 p = float3(float(x) + 0.5 + P[ib + 1], float(y) + 0.5 + P[ib + 2], 1.0);\n    float alpha = P[ib + 3];\n    uint aw, ah;\n    T0.GetDimensions(aw, ah);\n    float2 inv = 1.0 / float2(aw, ah);\n    for (int i = 0; i < n; i++) {\n        float4 a = B2[i * 3];\n        float4 b = B2[i * 3 + 1];\n        float4 r = B2[i * 3 + 2];\n        float2 uv = float2(dot(a.xyz, p), dot(b.xyz, p));\n        // The pixel\'s footprint in the bitmap (unit square per frame px).\n        float2 dx = float2(a.x, b.x);\n        float2 dy = float2(a.y, b.y);\n        float2 reach = abs(dx) + abs(dy);\n        if (any(uv < -reach) || any(uv > 1.0 + reach)) continue;\n        float texels = max(max(abs(dx.x), abs(dy.x)) * r.z, max(abs(dx.y), abs(dy.y)) * r.w);\n        int taps = clamp(int(ceil(texels)), 1, 4);\n        float4 sum = float4(0.0);\n        for (int j = 0; j < taps; j++) {\n            for (int k = 0; k < taps; k++) {\n                float2 o = (float2(j, k) + 0.5) / float(taps) - 0.5;\n                float2 q = uv + o.x * dx + o.y * dy;\n                if (any(q < 0.0) || any(q > 1.0)) continue;\n                float2 t = clamp(r.xy + q * r.zw, r.xy + 0.5, r.xy + r.zw - 0.5);\n                sum += T0.SampleLevel(S, t * inv, 0);\n            }\n        }\n        float4 c = sum / float(taps * taps) * (a.w * alpha);\n        res = c + res * (1.0 - c.a);\n    }\n    return res;\n}\n\n// One threadgroup of 256 per row of the output texture, which holds the\n// raster region (accumulator texel (x, y) = texture texel (x, y)). Each\n// thread prefix-sums a chunk of the row, chunk totals are scanned in shared\n// memory, then coverage is written. Texels outside the region are zero, so\n// the whole texture is valid (it may be larger than the region).\n// Params: [4] fill rule (0 nonzero, 1 even-odd), [5..9] color (linear\n// premultiplied), [9..11] region size, [11] color groups, then a color per\n// group, then color glyphs (see glyph_images). With several groups each\n// plane\'s coverage is kept in place, then the groups are composited in\n// order, each over the ones before.\n[shader(\"compute\")]\n[numthreads(256, 1, 1)]\nvoid k_raster_resolve(uint3 gtid: SV_GroupThreadID, uint3 gid: SV_GroupID) {\n    int rw = int(P[9]);\n    int rh = int(P[10]);\n    int groups = max(int(P[11] + 0.5), 1);\n    int ib = 12 + groups * 4;\n    bool images = P[ib] > 0.5;\n    uint tw, th;\n    OUT.GetDimensions(tw, th);\n    int y = int(gid.y);\n    if (y >= int(th)) return;\n    bool row_ok = y < rh;\n    int stride = rw + 2;\n    int plane = stride * rh;\n    int chunk = (int(tw) + 255) / 256;\n    int start = int(gtid.x) * chunk;\n    int end = min(start + chunk, int(tw));\n    bool even_odd = P[4] > 0.5;\n    for (int k = 0; k < groups; k++) {\n        int base = k * plane + y * stride;\n        float local = 0.0;\n        if (row_ok) {\n            for (int x = start; x < min(end, rw); x++) local += float(B1[base + x]);\n        }\n        row_sums[gtid.x] = local;\n        GroupMemoryBarrierWithGroupSync();\n        // Hillis-Steele inclusive scan over the 256 chunk totals.\n        for (uint off = 1; off < 256; off <<= 1) {\n            float v = gtid.x >= off ? row_sums[gtid.x - off] : 0.0;\n            GroupMemoryBarrierWithGroupSync();\n            row_sums[gtid.x] += v;\n            GroupMemoryBarrierWithGroupSync();\n        }\n        float acc = gtid.x > 0 ? row_sums[gtid.x - 1] : 0.0;\n        GroupMemoryBarrierWithGroupSync();\n        float4 color = float4(P[5], P[6], P[7], P[8]);\n        for (int x = start; x < end; x++) {\n            bool ok = row_ok && x < rw;\n            if (ok) acc += float(B1[base + x]);\n            float a = abs(acc) / ACC_SCALE;\n            float cov = even_odd ? 1.0 - abs(1.0 - (a - 2.0 * floor(a * 0.5))) : min(a, 1.0);\n            if (groups == 1) {\n                float4 v = ok ? color * cov : float4(0.0);\n                if (ok && images) v = glyph_images(v, x, y, ib);\n                OUT.Store(uint2(x, y), v);\n            } else if (ok) {\n                // Only this thread reads these cells again.\n                B1[base + x] = int(round(cov * ACC_SCALE));\n            }\n        }\n    }\n    if (groups == 1) return;\n    for (int x = start; x < end; x++) {\n        float4 res = float4(0.0);\n        if (row_ok && x < rw) {\n            for (int k = 0; k < groups; k++) {\n                float cov = float(B1[k * plane + y * stride + x]) / ACC_SCALE;\n                int c = 12 + k * 4;\n                float4 col = float4(P[c], P[c + 1], P[c + 2], P[c + 3]) * cov;\n                res = col + res * (1.0 - col.a);\n            }\n            if (images) res = glyph_images(res, x, y, ib);\n        }\n        OUT.Store(uint2(x, y), res);\n    }\n}\n\n// Presents a placed frame into a display drawable (BGRA8, sRGB-encoded),\n// letterboxed. Params: [4..6] source origin, [6..10] destination rect in\n// drawable pixels (x, y, w, h), [10..12] drawable size, [12..15] backdrop\n// color (encoded).\n// The player\'s timeline, drawn over the presented frame (sRGB). Params:\n// [15] on, [16] playhead 0..1, [17] loop in, [18] loop out (in >= out: no\n// loop), [19] emphasis (0..1, hover or scrubbing), [20] playing.\nfloat3 present_hud(float2 p, float2 dsize, float3 o) {\n    if (P[15] < 0.5) return o;\n    float e = P[19];\n    float x0 = 24.0, x1 = dsize.x - 24.0;\n    float yc = dsize.y - 26.0;\n    float half_h = 2.0 + 2.0 * e;\n    // A soft dark scrim so the bar reads on any frame.\n    float scrim = smoothstep(dsize.y - 90.0, dsize.y, p.y) * 0.55;\n    o = lerp(o, float3(0.0), scrim);\n    float u = (p.x - x0) / (x1 - x0);\n    float on_bar = step(abs(p.y - yc), half_h) * step(0.0, u) * step(u, 1.0);\n    float3 track = float3(1.0) * 0.28;\n    float3 played = float3(1.0) * 0.92;\n    float3 col = u <= P[16] ? played : track;\n    if (P[17] < P[18] && u >= P[17] && u <= P[18]) col = lerp(col, float3(1.0, 0.78, 0.25), 0.65);\n    o = lerp(o, col, on_bar * 0.9);\n    // Playhead.\n    float2 head = float2(x0 + P[16] * (x1 - x0), yc);\n    float r = 5.0 + 3.0 * e;\n    float d = length(p - head);\n    o = lerp(o, float3(1.0), saturate(r - d + 0.5));\n    // Loop brackets.\n    if (P[17] < P[18]) {\n        for (int k = 0; k < 2; k++) {\n            float bx = x0 + P[17 + k] * (x1 - x0);\n            float m = step(abs(p.x - bx), 1.5) * step(abs(p.y - yc), 9.0);\n            o = lerp(o, float3(1.0, 0.78, 0.25), m);\n        }\n    }\n    // Agent edits: a fading outline around what just changed. Params: [21]\n    // count, then (x0, y0, x1, y1, alpha) each from [22].\n    for (int k = 0; k < min(int(P[21] + 0.5), 4); k++) {\n        uint m = 22 + uint(k) * 5;\n        float2 c = (pf2(m) + pf2(m + 2)) * 0.5;\n        float2 hs = (pf2(m + 2) - pf2(m)) * 0.5;\n        float2 q = abs(p - c) - hs + 10.0;\n        float sd = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 10.0;\n        float a = P[m + 4];\n        // A negative alpha is a selection: steady, dashed, in cyan.\n        float3 col = float3(1.0, 0.36, 0.48);\n        if (a < 0.0) {\n            a = -a;\n            col = float3(0.35, 0.86, 1.0);\n            a *= step(frac((p.x + p.y) / 14.0), 0.62);\n        }\n        float line = saturate(1.5 - abs(sd)) * a;\n        float halo = saturate(1.0 - abs(sd) / 14.0) * 0.25 * a;\n        o = lerp(o, col, saturate(line + halo));\n    }\n    // Paused: two bars at the left end.\n    if (P[20] < 0.5) {\n        float2 q = p - float2(x0, yc - 22.0);\n        float bars = (step(0.0, q.x) * step(q.x, 4.0) + step(8.0, q.x) * step(q.x, 12.0)) * step(abs(q.y), 7.0);\n        o = lerp(o, float3(1.0), saturate(bars));\n    }\n    return o;\n}\n\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_present(uint3 gid: SV_DispatchThreadID) {\n    float2 dsize = pf2(10);\n    if (gid.x >= uint(dsize.x) || gid.y >= uint(dsize.y)) return;\n    float4 r = pf4(6);\n    float2 p = float2(gid.xy) + 0.5;\n    float3 back = float3(P[12], P[13], P[14]);\n    float2 uv = (p - r.xy) / r.zw;\n    if (any(uv < 0.0) || any(uv > 1.0)) {\n        OUT.Store(gid.xy, float4(present_hud(p, dsize, back), 1.0));\n        return;\n    }\n    float4 c = sample_o(T0, uv * resolution(), 4);\n    // Composite over a checkerboard so transparency is visible.\n    float2 cell = floor(p / 12.0);\n    float check = (int(cell.x + cell.y) & 1) == 0 ? 0.18 : 0.24;\n    float3 lin = c.rgb + srgb_to_linear(float3(check)) * (1.0 - c.a);\n    float3 o = linear_to_srgb(saturate(lin));\n    OUT.Store(gid.xy, float4(present_hud(p, dsize, o), 1.0));\n}\n\n// Depth of field: T0 color, T1 camera distance (r). Params: [4] focus\n// distance, [5] aperture (blur radius in px far from focus), [6] taps.\n// A gather where each tap counts only if its own circle of confusion\n// reaches this pixel, so sharp foreground does not smear and blurred\n// foreground spills over what is behind it.\nfloat coc_px(float d) {\n    return min(P[5], P[5] * abs(d - P[4]) / max(d, 1e-3));\n}\n\n// The gather runs at reduced resolution (header [1] = output texel size in\n// frame px); k_dof_mix brings it back over the sharp image by each pixel\'s\n// own blur, so focused detail stays full resolution.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_dof(uint3 gid: SV_DispatchThreadID) {\n    if (outside_frame(gid.xy)) return;\n    float2 p = (float2(gid.xy) + 0.5) * P[1];\n    uint2 c0 = uint2(min(p, resolution() - 1.0));\n    float own_d = load(T1, c0).r;\n    float own = coc_px(own_d);\n    float4 acc = load(T0, c0);\n    float wsum = 1.0;\n    int n = int(P[6]);\n    // A per-pixel rotation turns the sparse disk\'s ghost images into fine\n    // noise.\n    float spin = frac(sin(dot(float2(gid.xy), float2(12.9898, 78.233))) * 43758.5453) * 6.2831853;\n    for (int k = 0; k < n; k++) {\n        float r = P[5] * sqrt((float(k) + 0.5) / float(n));\n        float a = float(k) * 2.39996 + spin;\n        int2 qi = int2(p + r * float2(cos(a), sin(a)));\n        if (any(qi < 0) || any(qi >= int2(resolution()))) continue;\n        float d = load(T1, uint2(qi)).r;\n        float c = coc_px(d);\n        // What is behind us blurs no further onto us than we are blurred.\n        if (d > own_d * 1.02) c = min(c, own);\n        float w = saturate(c - r + 1.0);\n        acc += load(T0, uint2(qi)) * w;\n        wsum += w;\n    }\n    OUT.Store(gid.xy, acc / wsum);\n}\n\n// T0 sharp, T1 blurred (texel size [4] frame px), T2 distance.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_dof_mix(uint3 gid: SV_DispatchThreadID) {\n    if (outside_frame(gid.xy)) return;\n    float c = coc_px(load(T2, gid.xy).r);\n    float4 sharp = load(T0, gid.xy);\n    float4 soft = sample_region(T1, float2(gid.xy) + 0.5, float2(0.0), P[7]);\n    OUT.Store(gid.xy, lerp(sharp, soft, smoothstep(0.6, 2.5, c)));\n}\n\n// Motion blur accumulation: OUT = (first ? 0 : T1) + T0 * weight, where T0\n// is a placed sample (origin [4..6]) and T1 the running sum. Params: [6]\n// weight, [7] first.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_accumulate(uint3 gid: SV_DispatchThreadID) {\n    if (outside_frame(gid.xy)) return;\n    float4 acc = P[7] > 0.5 ? float4(0.0) : load(T1, gid.xy);\n    OUT.Store(gid.xy, acc + load_o(T0, gid.xy, 4) * P[6]);\n}\n\n// ---------------------------------------------------------------- regions\n// Region-limited, multi-resolution blur. Textures here cover a region of the\n// frame: origin O (frame px) and texel size s (frame px per texel).\n\nfloat4 sample_region(Texture2D<float4> t, float2 p, float2 origin, float scale) {\n    uint w, h;\n    t.GetDimensions(w, h);\n    return t.SampleLevel(S, (p - origin) / scale / float2(w, h), 0);\n}\n\n// Separable gaussian over a region. Params: [4] sigma (frame px), [5..7]\n// direction, [7] output texel size, [8..10] output origin, [10..12] input\n// origin, [12] input texel size.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_blur_region(uint3 gid: SV_DispatchThreadID) {\n    uint w, h;\n    OUT.GetDimensions(w, h);\n    if (gid.x >= w || gid.y >= h) return;\n    float s_out = P[7];\n    float2 p = pf2(8) + (float2(gid.xy) + 0.5) * s_out;\n    float2 dir = pf2(5);\n    float2 in_o = pf2(10);\n    float s_in = P[12];\n    float sig = max(P[4] / s_out, 1e-3);\n    int radius = int(ceil(sig * 3.0));\n    float inv2s2 = 1.0 / (2.0 * sig * sig);\n    float4 acc = sample_region(T0, p, in_o, s_in);\n    float wsum = 1.0;\n    for (int i = 1; i <= radius; i += 2) {\n        float w0 = exp(-float(i * i) * inv2s2);\n        float w1 = exp(-float((i + 1) * (i + 1)) * inv2s2);\n        float wt = w0 + w1;\n        float off = (float(i) + w1 / max(wt, 1e-8)) * s_out;\n        acc += (sample_region(T0, p + dir * off, in_o, s_in) + sample_region(T0, p - dir * off, in_o, s_in)) * wt;\n        wsum += 2.0 * wt;\n    }\n    OUT.Store(gid.xy, acc / wsum);\n}\n\n// Resamples a region texture onto another grid, averaging n x n bilinear\n// taps per output texel (n = 1 for upsampling). Params: [4] output texel\n// size, [5..7] output origin, [7..9] input origin, [9] input texel size,\n// [10] n.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_resample(uint3 gid: SV_DispatchThreadID) {\n    uint w, h;\n    OUT.GetDimensions(w, h);\n    if (gid.x >= w || gid.y >= h) return;\n    float s_out = P[4];\n    float2 base = pf2(5) + float2(gid.xy) * s_out;\n    int n = max(int(P[10] + 0.5), 1);\n    float4 acc = float4(0.0);\n    for (int y = 0; y < n; y++) {\n        for (int x = 0; x < n; x++) {\n            float2 p = base + (float2(x, y) + 0.5) / float(n) * s_out;\n            acc += sample_region(T0, p, pf2(7), P[9]);\n        }\n    }\n    OUT.Store(gid.xy, acc / float(n * n));\n}\n\ngroupshared int diff_sums[256];\n\n// Squared error between the frame (T0, placed, origin [4..6]) and a\n// reference (T1, sRGB-encoded RGBA8, frame-sized), both composited over\n// black and compared in 8-bit sRGB units, as viewers see them. Each 16x16\n// threadgroup writes its partial sum to B1[group index].\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_diff(uint3 gid: SV_DispatchThreadID, uint3 gtid: SV_GroupThreadID, uint3 grp: SV_GroupID) {\n    uint li = gtid.y * 16 + gtid.x;\n    int e = 0;\n    if (!outside_frame(gid.xy)) {\n        float4 c = load_o(T0, gid.xy, 4);\n        float3 ours = round(linear_to_srgb(saturate(c.rgb)) * 255.0);\n        float4 r = load(T1, gid.xy);\n        float3 theirs = round(r.rgb * r.a * 255.0);\n        float3 d = ours - theirs;\n        e = int(dot(d, d));\n    }\n    diff_sums[li] = e;\n    GroupMemoryBarrierWithGroupSync();\n    for (uint s = 128; s > 0; s >>= 1) {\n        if (li < s) diff_sums[li] += diff_sums[li + s];\n        GroupMemoryBarrierWithGroupSync();\n    }\n    if (li == 0) {\n        uint groups_x = (uint(P[2]) + 15) / 16;\n        B1[grp.y * groups_x + grp.x] = diff_sums[0];\n    }\n}\n\n// ---------------------------------------------------------------- fluid\n// Stable-fluids solver on grids covering the frame. A grid texel spans\n// P[1] frame px (the output grid\'s); positions are frame px, velocities\n// frame px per second, y down. Common params: [4] dt, [5] velocity texel.\n\nbool outside_grid(uint2 gid) {\n    uint w, h;\n    OUT.GetDimensions(w, h);\n    return gid.x >= w || gid.y >= h;\n}\n\nfloat4 grid_at(Texture2D<float4> t, int2 c) {\n    uint w, h;\n    t.GetDimensions(w, h);\n    return t.Load(int3(clamp(c, int2(0), int2(w, h) - 1), 0));\n}\n\n// Vorticity: OUT.x = dvy/dx - dvx/dy of the velocity T0.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_fl_curl(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    int2 c = int2(gid.xy);\n    float r = grid_at(T0, c + int2(1, 0)).y, l = grid_at(T0, c - int2(1, 0)).y;\n    float d = grid_at(T0, c + int2(0, 1)).x, u = grid_at(T0, c - int2(0, 1)).x;\n    OUT.Store(gid.xy, float4((r - l - (d - u)) / (2.0 * P[1]), 0.0, 0.0, 0.0));\n}\n\n// Forces on the velocity T0: vorticity confinement (curl T1), curl-noise\n// turbulence, buoyancy of the dye (T2, texel [6]), and a push away from /\n// swirl around the edges of the stir image (T3, placed, origin [12..14]).\n// Params: [7] vorticity, [8] turbulence (px/s^2), [9] turbulence scale (px),\n// [10] noise time, [11] buoyancy (px/s^2 per unit dye alpha), [14] push,\n// [15] swirl, [16] stir connected, [17] stir texel size (frame px).\nfloat turb_potential(float2 q, float z) {\n    return snoise(float3(q, z)) + 0.5 * snoise(float3(q * 2.03 + 11.7, z * 1.3));\n}\n\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_fl_force(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    int2 c = int2(gid.xy);\n    float h = P[1];\n    float dt = P[4];\n    float2 p = (float2(gid.xy) + 0.5) * h;\n    float2 v = grid_at(T0, c).xy;\n    // Vorticity confinement: spin eddies back up. The gradient of |curl|\n    // is taken over two texels so grid-scale noise is not amplified.\n    float w = grid_at(T1, c).x;\n    float2 g = float2(abs(grid_at(T1, c + int2(2, 0)).x) - abs(grid_at(T1, c - int2(2, 0)).x),\n                      abs(grid_at(T1, c + int2(0, 2)).x) - abs(grid_at(T1, c - int2(0, 2)).x));\n    g /= length(g) + 1e-3 * abs(w) + 1e-5;\n    float2 f = 0.25 * P[7] * h * float2(g.y, -g.x) * w;\n    // Divergence-free turbulence from a noise potential.\n    if (P[8] != 0.0) {\n        float2 q = p / max(P[9], 1.0);\n        float e = 0.02;\n        float z = P[10];\n        float a = turb_potential(q + float2(0.0, e), z) - turb_potential(q - float2(0.0, e), z);\n        float b = turb_potential(q + float2(e, 0.0), z) - turb_potential(q - float2(e, 0.0), z);\n        f += P[8] * float2(a, -b) / (2.0 * e) * 0.3;\n    }\n    // Buoyancy: dye rises.\n    if (P[11] != 0.0) {\n        f.y -= P[11] * sample_region(T2, p, float2(0.0), P[6]).a;\n    }\n    // Stir: the edges of the stir image push the fluid out and around.\n    if (P[16] > 0.5) {\n        float ax = sample_region(T3, p + float2(h, 0.0), pf2(12), P[17]).a - sample_region(T3, p - float2(h, 0.0), pf2(12), P[17]).a;\n        float ay = sample_region(T3, p + float2(0.0, h), pf2(12), P[17]).a - sample_region(T3, p - float2(0.0, h), pf2(12), P[17]).a;\n        float2 ga = float2(ax, ay) * 0.5;\n        f += -P[14] * ga + P[15] * float2(-ga.y, ga.x);\n    }\n    OUT.Store(gid.xy, float4(v + f * dt, 0.0, 0.0));\n}\n\n// Divergence of the velocity T0.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_fl_div(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    int2 c = int2(gid.xy);\n    float r = grid_at(T0, c + int2(1, 0)).x, l = grid_at(T0, c - int2(1, 0)).x;\n    float d = grid_at(T0, c + int2(0, 1)).y, u = grid_at(T0, c - int2(0, 1)).y;\n    OUT.Store(gid.xy, float4((r - l + d - u) / (2.0 * P[1]), 0.0, 0.0, 0.0));\n}\n\n// One Jacobi iteration of the pressure Poisson equation: pressure T0,\n// divergence T1. [7] scales the previous pressure (warm start decay).\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_fl_jacobi(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    int2 c = int2(gid.xy);\n    float s = P[7];\n    float sum = grid_at(T0, c + int2(1, 0)).x + grid_at(T0, c - int2(1, 0)).x\n              + grid_at(T0, c + int2(0, 1)).x + grid_at(T0, c - int2(0, 1)).x;\n    float div = grid_at(T1, c).x;\n    OUT.Store(gid.xy, float4((sum * s - div * P[1] * P[1]) * 0.25, 0.0, 0.0, 0.0));\n}\n\n// Subtracts the pressure gradient (T1) from the velocity (T0).\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_fl_project(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    int2 c = int2(gid.xy);\n    float2 gp = float2(grid_at(T1, c + int2(1, 0)).x - grid_at(T1, c - int2(1, 0)).x,\n                       grid_at(T1, c + int2(0, 1)).x - grid_at(T1, c - int2(0, 1)).x) / (2.0 * P[1]);\n    OUT.Store(gid.xy, float4(grid_at(T0, c).xy - gp, 0.0, 0.0));\n}\n\n// Semi-Lagrangian advection of the field T1 (texel [7]) through the\n// velocity T0 plus wind [8..10], times [12] (dissipation). With [14] set,\n// the source T2 (placed, origin [10..12], texel size [15]) is laid over it\n// at rate [13]:\n// out = src * k + field * (1 - src.a * k).\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_fl_advect(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    float2 p = (float2(gid.xy) + 0.5) * P[1];\n    float2 v = sample_region(T0, p, float2(0.0), P[5]).xy + pf2(8);\n    float4 f = sample_region(T1, p - P[4] * v, float2(0.0), P[7]) * P[12];\n    if (P[14] > 0.5) {\n        float k = P[13];\n        float4 src = sample_region(T2, p, pf2(10), P[15]);\n        f = src * k + f * (1.0 - src.a * k);\n    }\n    OUT.Store(gid.xy, f);\n}\n\n// Catmull-Rom (bicubic) sample at texel coordinates `p`: repeated\n// resampling (feedback zooms and rotations) stays sharp instead of\n// diffusing like bilinear does.\nfloat4 sample_sharp(Texture2D<float4> t, float2 p) {\n    float2 q = p - 0.5;\n    float2 i = floor(q);\n    float2 f = q - i;\n    float2 w0 = f * (-0.5 + f * (1.0 - 0.5 * f));\n    float2 w1 = 1.0 + f * f * (-2.5 + 1.5 * f);\n    float2 w2 = f * (0.5 + f * (2.0 - 1.5 * f));\n    float2 w3 = f * f * (-0.5 + 0.5 * f);\n    float4 acc = float4(0.0);\n    float wx[4] = { w0.x, w1.x, w2.x, w3.x };\n    float wy[4] = { w0.y, w1.y, w2.y, w3.y };\n    uint w, h;\n    t.GetDimensions(w, h);\n    for (int y = 0; y < 4; y++) {\n        for (int x = 0; x < 4; x++) {\n            int2 c = int2(i) + int2(x - 1, y - 1);\n            float4 v = (any(c < 0) || c.x >= int(w) || c.y >= int(h)) ? float4(0.0) : t.Load(int3(c, 0));\n            acc += v * wx[x] * wy[y];\n        }\n    }\n    return acc;\n}\n\n// OUT = T0 over T1, both frame-sized and frame-aligned.\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_over_frames(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    float4 a = load(T0, gid.xy);\n    OUT.Store(gid.xy, a + load(T1, gid.xy) * (1.0 - a.a));\n}\n\n// Copies T0 into OUT texel for texel (checkpoints).\n[shader(\"compute\")]\n[numthreads(16, 16, 1)]\nvoid k_copy(uint3 gid: SV_DispatchThreadID) {\n    if (outside_grid(gid.xy)) return;\n    OUT.Store(gid.xy, load(T0, gid.xy));\n}\n";
Expand description

Op library prepended to every generated module.