rosace-render 0.1.0

GPU/CPU hybrid renderer for ROSACE with dirty-region tracking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Built-in GPU shape pipelines (D109 / Phase 27 Step 3) — geometry,
//! uniform layout, and WGSL sources.
//!
//! Lives in `rosace-render` (Layer 4) so `SkiaCanvas::play_picture` can
//! convert shape `DrawCommand`s to GPU quads without an upward dependency;
//! `rosace-shader::builtin` (Layer 5) wraps these WGSL sources into
//! `ShaderSpec`s and registers them, and re-exports everything here for
//! app-facing use.
//!
//! The SDF fragment shaders that replace `tiny-skia`'s CPU rasterization
//! for ROSACE's built-in `DrawCommand` shapes. Five pipelines cover all
//! eight shape variants:
//!
//! | Pipeline        | Serves                                          |
//! |-----------------|-------------------------------------------------|
//! | `FILL_RRECT`    | `FillRect` (r=0), `FillRRect`, `FillCircle`     |
//! | `STROKE_RRECT`  | `StrokeRect` (r=0), `StrokeRRect`               |
//! | `GRADIENT`      | `FillGradient` (two-stop, axis-aligned, rounded)|
//! | `ARC`           | `FillArc` (ring segment, round caps)            |
//! | `SHADOW`        | `DrawShadow` (Gaussian-approx rounded shadow)   |
//!
//! Every conversion function here maps a shape in PHYSICAL pixels to
//! `(quad_rect, uniform_bytes)`: the quad is the shape's bounds inflated by
//! the AA/blur margin (an SDF's anti-aliasing ramp extends past the exact
//! bounds; an un-inflated quad would slice the ramp off), and the uniforms
//! carry the true geometry in quad-local px. Colors are sRGB u8 (what
//! `DrawCommand` records) converted to LINEAR premultiplied-ready f32 here
//! — the fragment outputs linear, the sRGB surface encodes on write (the
//! same one-correct-round-trip rule as the compositor's texture formats).
//!
//! All uniform structs share ONE layout (`BuiltinShapeUniforms`, 64 bytes)
//! so every pipeline binds identically; each interprets `params` its own
//! way, documented per conversion function.

use rosace_core::shader::ShaderUniforms;
use rosace_macros::ShaderUniforms;

/// Raw pipeline ids of the built-in pipelines — inside `PipelineId`'s
/// reserved built-in range (< 0x100). `rosace-shader::builtin` exposes the
/// typed constants.
pub const FILL_RRECT_ID:   u64 = 1;
pub const STROKE_RRECT_ID: u64 = 2;
pub const GRADIENT_ID:     u64 = 3;
pub const ARC_ID:          u64 = 4;
pub const SHADOW_ID:       u64 = 5;

/// The one uniform layout every built-in pipeline binds.
///
/// All geometry is in the RECORDING's units (quad-local), with `quad`
/// carrying the quad's size in those same units. The shader computes
/// `px_scale = rosace_quad.size_px / quad` and scales everything itself —
/// so a conversion done at logical px (widget recording, scaled ×DPR at
/// replay) and one done at physical px (scale 1:1) are BOTH correct, and
/// HiDPI can never desynchronize uniforms from quad placement.
#[derive(ShaderUniforms)]
pub struct BuiltinShapeUniforms {
    /// Primary color, LINEAR straight-alpha RGBA.
    pub color:  [f32; 4],
    /// Secondary color (gradient `to`), LINEAR straight-alpha RGBA.
    pub color2: [f32; 4],
    /// Shape geometry in quad-local units: rects (x, y, w, h); arcs
    /// (center_x, center_y, unused, unused).
    pub shape:  [f32; 4],
    /// Pipeline-specific parameters — see each conversion fn.
    pub params: [f32; 4],
    /// The quad's (w, h) in the same units as `shape`/`params` lengths.
    pub quad:   [f32; 2],
}

/// sRGB u8 → linear f32, the exact EOTF (not the 2.2 shortcut) — this is
/// the inverse of what the sRGB swapchain applies on write, so a shader
/// fill of `Color::rgb(43,45,48)` lands at (43,45,48), byte-identical to
/// the CPU path (the 2026-07-08 double-gamma bug is the cautionary tale).
fn srgb_to_linear(c: u8) -> f32 {
    let x = c as f32 / 255.0;
    if x <= 0.04045 { x / 12.92 } else { ((x + 0.055) / 1.055).powf(2.4) }
}

/// sRGB u8 RGBA → linear f32 RGBA (alpha is linear already).
pub fn linear_rgba(rgba: [u8; 4]) -> [f32; 4] {
    [
        srgb_to_linear(rgba[0]),
        srgb_to_linear(rgba[1]),
        srgb_to_linear(rgba[2]),
        rgba[3] as f32 / 255.0,
    ]
}

/// AA margin: the SDF coverage ramp is 1px wide, centered on the edge.
const AA_MARGIN: f32 = 1.0;

/// A quad rect `(x, y, w, h)` inflated by `m` on every side.
fn inflate(rect: (f32, f32, f32, f32), m: f32) -> (f32, f32, f32, f32) {
    (rect.0 - m, rect.1 - m, rect.2 + 2.0 * m, rect.3 + 2.0 * m)
}

/// `FillRect`/`FillRRect`/`FillCircle` → `FILL_RRECT` quad.
/// params: (corner_radius, 0, 0, 0).
pub fn fill_rrect_quad(
    rect: (f32, f32, f32, f32), radius: f32, rgba: [u8; 4],
) -> ((f32, f32, f32, f32), Vec<u8>) {
    let quad = inflate(rect, AA_MARGIN);
    let r = radius.max(0.0).min(rect.2 / 2.0).min(rect.3 / 2.0);
    let u = BuiltinShapeUniforms {
        color:  linear_rgba(rgba),
        color2: [0.0; 4],
        shape:  [rect.0 - quad.0, rect.1 - quad.1, rect.2, rect.3],
        params: [r, 0.0, 0.0, 0.0],
        quad:   [quad.2, quad.3],
    };
    (quad, u.to_bytes())
}

/// `StrokeRect`/`StrokeRRect` → `STROKE_RRECT` quad. The stroke is centered
/// on the shape edge (tiny-skia `Stroke` convention).
/// params: (corner_radius, stroke_width, 0, 0).
pub fn stroke_rrect_quad(
    rect: (f32, f32, f32, f32), radius: f32, width: f32, rgba: [u8; 4],
) -> ((f32, f32, f32, f32), Vec<u8>) {
    let quad = inflate(rect, AA_MARGIN + width / 2.0);
    let r = radius.max(0.0).min(rect.2 / 2.0).min(rect.3 / 2.0);
    let u = BuiltinShapeUniforms {
        color:  linear_rgba(rgba),
        color2: [0.0; 4],
        shape:  [rect.0 - quad.0, rect.1 - quad.1, rect.2, rect.3],
        params: [r, width, 0.0, 0.0],
        quad:   [quad.2, quad.3],
    };
    (quad, u.to_bytes())
}

/// `FillGradient` → `GRADIENT` quad. Two stops, `from` at the rect's
/// top/left edge to `to` at the bottom/right (pad spread), masked by the
/// rounded rect. params: (corner_radius, vertical ? 1 : 0, 0, 0).
///
/// Colors are passed as sRGB (NOT pre-linearized like every other
/// pipeline): tiny-skia interpolates gradient stops in sRGB space, so the
/// shader must mix in sRGB and linearize AFTER — verified by A/B midpoint
/// sampling (linear-space mixing measured +16/255 red at the midpoint of
/// the violet→blue reference gradient vs the CPU path).
pub fn gradient_quad(
    rect: (f32, f32, f32, f32), radius: f32, from: [u8; 4], to: [u8; 4], vertical: bool,
) -> ((f32, f32, f32, f32), Vec<u8>) {
    let quad = inflate(rect, AA_MARGIN);
    let r = radius.max(0.0).min(rect.2 / 2.0).min(rect.3 / 2.0);
    let srgb = |c: [u8; 4]| [
        c[0] as f32 / 255.0, c[1] as f32 / 255.0,
        c[2] as f32 / 255.0, c[3] as f32 / 255.0,
    ];
    let u = BuiltinShapeUniforms {
        color:  srgb(from),
        color2: srgb(to),
        shape:  [rect.0 - quad.0, rect.1 - quad.1, rect.2, rect.3],
        params: [r, if vertical { 1.0 } else { 0.0 }, 0.0, 0.0],
        quad:   [quad.2, quad.3],
    };
    (quad, u.to_bytes())
}

/// `FillArc` → `ARC` quad: ring segment of `thickness` along the circle of
/// `radius` centered at `center`, from `start_deg` sweeping `sweep_deg`
/// clockwise (0° = 3 o'clock, y-down), ROUND caps (the CPU path strokes
/// with `LineCap::Round`). shape: (center in quad-local px);
/// params: (radius, thickness, start_rad, sweep_rad) — sweep normalized
/// non-negative here so the shader needs no sign handling.
pub fn arc_quad(
    center: (f32, f32), radius: f32, thickness: f32,
    start_deg: f32, sweep_deg: f32, rgba: [u8; 4],
) -> ((f32, f32, f32, f32), Vec<u8>) {
    let reach = radius + thickness / 2.0;
    let quad = inflate(
        (center.0 - reach, center.1 - reach, reach * 2.0, reach * 2.0),
        AA_MARGIN,
    );
    let (start, sweep) = if sweep_deg < 0.0 {
        (start_deg + sweep_deg, -sweep_deg)
    } else {
        (start_deg, sweep_deg)
    };
    let u = BuiltinShapeUniforms {
        color:  linear_rgba(rgba),
        color2: [0.0; 4],
        shape:  [center.0 - quad.0, center.1 - quad.1, 0.0, 0.0],
        params: [radius, thickness, start.to_radians(), sweep.min(360.0).to_radians()],
        quad:   [quad.2, quad.3],
    };
    (quad, u.to_bytes())
}

/// Blur margin multiplier: the visible falloff of the CPU path's
/// triple-box-blur mask extends roughly 1.5×blur past the rect (its mask
/// allocates `margin = blur` on each side plus the AA edge); 2× is safely
/// past visually-zero for the Gaussian approximation too.
const SHADOW_MARGIN: f32 = 2.0;

/// `DrawShadow` → `SHADOW` quad: Gaussian-approximate drop shadow of the
/// rounded rect. params: (corner_radius, sigma, 0, 0). Sigma maps from the
/// CPU path's box-blur `blur` parameter: three box passes of width b
/// approximate a Gaussian with σ ≈ b/2 — tuned against the real
/// `build_shadow_mask` output in the A/B demo, not derived on paper.
pub fn shadow_quad(
    rect: (f32, f32, f32, f32), radius: f32, blur: f32, rgba: [u8; 4],
) -> ((f32, f32, f32, f32), Vec<u8>) {
    let quad = inflate(rect, AA_MARGIN + blur.max(0.0) * SHADOW_MARGIN);
    let r = radius.max(0.0).min(rect.2 / 2.0).min(rect.3 / 2.0);
    let u = BuiltinShapeUniforms {
        color:  linear_rgba(rgba),
        color2: [0.0; 4],
        shape:  [rect.0 - quad.0, rect.1 - quad.1, rect.2, rect.3],
        params: [r, (blur * 0.5).max(0.25), 0.0, 0.0],
        quad:   [quad.2, quad.3],
    };
    (quad, u.to_bytes())
}

/// Shared WGSL: uniform struct + SDF library, prepended to every built-in
/// fragment. (The framework's vertex stage + `rosace_quad` come from the
/// compositor's own header — see shader_quad_header.wgsl.)
const SDF_LIB: &str = r#"
struct BuiltinShapeUniforms {
    color:  vec4<f32>,
    color2: vec4<f32>,
    shape:  vec4<f32>,
    params: vec4<f32>,
    quad:   vec2<f32>,
};
@group(0) @binding(1) var<uniform> u: BuiltinShapeUniforms;

// Signed distance to a rounded rect centered at the origin with half-size
// `half` and corner radius `r`. Negative inside.
fn sd_rrect(p: vec2<f32>, half: vec2<f32>, r: f32) -> f32 {
    let q = abs(p) - half + vec2<f32>(r, r);
    return length(max(q, vec2<f32>(0.0, 0.0))) + min(max(q.x, q.y), 0.0) - r;
}

// 1px-wide coverage ramp centered on the edge (d in px).
fn aa_cov(d: f32) -> f32 {
    return clamp(0.5 - d, 0.0, 1.0);
}

// Quad-local position in physical px.
fn local_px(uv: vec2<f32>) -> vec2<f32> {
    return uv * rosace_quad.size_px;
}

// Recording-units -> physical-px scale (the DPR when uniforms were built
// from logical px; 1.0 when built from physical px). x == y in practice.
fn px_scale() -> vec2<f32> {
    return rosace_quad.size_px / max(u.quad, vec2<f32>(1e-6, 1e-6));
}

// Premultiply a straight-alpha linear color by coverage.
fn out_color(c: vec4<f32>, cov: f32) -> vec4<f32> {
    let a = c.a * cov;
    return vec4<f32>(c.rgb * a, a);
}
"#;

const FILL_RRECT_FS: &str = r#"
@fragment
fn fs_main(in: RosaceVsOut) -> @location(0) vec4<f32> {
    let sc = px_scale();
    let p = local_px(in.uv) - (u.shape.xy + u.shape.zw * 0.5) * sc;
    let d = sd_rrect(p, u.shape.zw * 0.5 * sc, u.params.x * sc.x);
    return out_color(u.color, aa_cov(d));
}
"#;

const STROKE_RRECT_FS: &str = r#"
@fragment
fn fs_main(in: RosaceVsOut) -> @location(0) vec4<f32> {
    let sc = px_scale();
    let p = local_px(in.uv) - (u.shape.xy + u.shape.zw * 0.5) * sc;
    let d = abs(sd_rrect(p, u.shape.zw * 0.5 * sc, u.params.x * sc.x)) - u.params.y * sc.x * 0.5;
    return out_color(u.color, aa_cov(d));
}
"#;

const GRADIENT_FS: &str = r#"
@fragment
fn fs_main(in: RosaceVsOut) -> @location(0) vec4<f32> {
    let sc = px_scale();
    let lp = local_px(in.uv) - u.shape.xy * sc;
    var t: f32;
    if u.params.y > 0.5 {
        t = clamp(lp.y / max(u.shape.w * sc.y, 1e-6), 0.0, 1.0);
    } else {
        t = clamp(lp.x / max(u.shape.z * sc.x, 1e-6), 0.0, 1.0);
    }
    // Mix in sRGB (tiny-skia's convention), then linearize for output —
    // the surface re-encodes to sRGB on write.
    let c_srgb = mix(u.color, u.color2, t);
    let lo = c_srgb.rgb / 12.92;
    let hi = pow((c_srgb.rgb + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4));
    let c = vec4<f32>(select(hi, lo, c_srgb.rgb <= vec3<f32>(0.04045)), c_srgb.a);
    let p = local_px(in.uv) - (u.shape.xy + u.shape.zw * 0.5) * sc;
    let d = sd_rrect(p, u.shape.zw * 0.5 * sc, u.params.x * sc.x);
    return out_color(c, aa_cov(d));
}
"#;

const ARC_FS: &str = r#"
const TAU: f32 = 6.28318530718;

@fragment
fn fs_main(in: RosaceVsOut) -> @location(0) vec4<f32> {
    let sc = px_scale();
    let p = local_px(in.uv) - u.shape.xy * sc;
    let radius = u.params.x * sc.x;
    let start  = u.params.z;
    let sweep  = u.params.w;

    // Angle of this pixel, wrapped relative to the arc start.
    var rel = atan2(p.y, p.x) - start;
    rel = rel - floor(rel / TAU) * TAU; // wrap to [0, TAU)

    var d: f32;
    if rel <= sweep {
        // Within the swept angle: distance to the arc's centerline circle.
        d = abs(length(p) - radius);
    } else {
        // Outside: distance to the nearer endpoint — round caps for free.
        let e0 = vec2<f32>(cos(start), sin(start)) * radius;
        let a1 = start + sweep;
        let e1 = vec2<f32>(cos(a1), sin(a1)) * radius;
        d = min(distance(p, e0), distance(p, e1));
    }
    return out_color(u.color, aa_cov(d - u.params.y * sc.x * 0.5));
}
"#;

const SHADOW_FS: &str = r#"
// Gaussian CDF via an Abramowitz-Stegun-style erf approximation — the
// rounded-rect SDF pushed through the CDF gives the blurred coverage
// (exact along straight edges, slightly tighter than a true 2D blur at
// corners; visually verified against the CPU box-blur mask in the A/B
// demo).
fn erf_approx(x: f32) -> f32 {
    let s = sign(x);
    let a = abs(x);
    var t = 1.0 + (0.278393 + (0.230389 + 0.078108 * a * a) * a) * a;
    t = t * t;
    return s - s / (t * t);
}

@fragment
fn fs_main(in: RosaceVsOut) -> @location(0) vec4<f32> {
    let sc = px_scale();
    let p = local_px(in.uv) - (u.shape.xy + u.shape.zw * 0.5) * sc;
    let d = sd_rrect(p, u.shape.zw * 0.5 * sc, u.params.x * sc.x);
    let sigma = max(u.params.y * sc.x, 0.25);
    let cov = 0.5 - 0.5 * erf_approx(d / (sigma * 1.41421356));
    return out_color(u.color, cov);
}
"#;

/// The five built-in pipelines as `(raw_id, full_wgsl_fragment_source)` —
/// consumed by `rosace-shader::builtin::register_builtins()`, which wraps
/// them in `ShaderSpec`s (a Layer-5 type this crate cannot name).
pub fn builtin_wgsl_sources() -> [(u64, String); 5] {
    let src = |fs: &str| format!("{SDF_LIB}\n{fs}");
    [
        (FILL_RRECT_ID,   src(FILL_RRECT_FS)),
        (STROKE_RRECT_ID, src(STROKE_RRECT_FS)),
        (GRADIENT_ID,     src(GRADIENT_FS)),
        (ARC_ID,          src(ARC_FS)),
        (SHADOW_ID,       src(SHADOW_FS)),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fill_quad_inflates_by_aa_margin_and_offsets_shape_into_quad_space() {
        let (quad, bytes) = fill_rrect_quad((10.0, 20.0, 100.0, 50.0), 8.0, [255, 0, 0, 255]);
        assert_eq!(quad, (9.0, 19.0, 102.0, 52.0));
        assert_eq!(bytes.len(), 80, "4×vec4 + vec2 quad size, rounded to 16");
        // shape.xy = true rect origin in quad-local px = (1, 1).
        assert_eq!(&bytes[32..40], &[1.0f32.to_le_bytes(), 1.0f32.to_le_bytes()].concat()[..]);
    }

    #[test]
    fn stroke_quad_margin_covers_half_the_stroke_width() {
        let (quad, _) = stroke_rrect_quad((100.0, 100.0, 50.0, 50.0), 0.0, 6.0, [0, 0, 0, 255]);
        // Inflation = 1 (AA) + 3 (width/2) = 4 per side.
        assert_eq!(quad, (96.0, 96.0, 58.0, 58.0));
    }

    #[test]
    fn negative_sweep_normalizes_to_positive_from_shifted_start() {
        let (_, bytes) = arc_quad((50.0, 50.0), 20.0, 4.0, 90.0, -90.0, [0, 0, 0, 255]);
        let start = f32::from_le_bytes(bytes[56..60].try_into().unwrap());
        let sweep = f32::from_le_bytes(bytes[60..64].try_into().unwrap());
        assert!((start - 0.0f32.to_radians()).abs() < 1e-6, "start must shift back: {start}");
        assert!((sweep - 90.0f32.to_radians()).abs() < 1e-6, "sweep must be positive: {sweep}");
    }

    #[test]
    fn srgb_conversion_round_trips_the_known_gamma_bug_color() {
        // The D109 gamma discipline test color: #2B2D30 must come back as
        // itself after linear → sRGB-surface encode. Linearizing 43/255
        // then re-encoding must round-trip to 43.
        let lin = srgb_to_linear(43);
        let re = if lin <= 0.0031308 { lin * 12.92 } else { 1.055 * lin.powf(1.0 / 2.4) - 0.055 };
        assert_eq!((re * 255.0).round() as u8, 43);
    }

    #[test]
    fn radius_clamps_to_half_extent_like_the_cpu_path() {
        let (_, bytes) = fill_rrect_quad((0.0, 0.0, 20.0, 10.0), 99.0, [0, 0, 0, 255]);
        let r = f32::from_le_bytes(bytes[48..52].try_into().unwrap());
        assert_eq!(r, 5.0, "radius must clamp to min(w,h)/2");
    }
}