skia-rs-gpu 0.2.3

GPU backends for skia-rs
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Stencil-then-cover algorithm for complex path rendering.
//!
//! This module implements the stencil-then-cover technique for GPU rendering
//! of complex paths with correct winding rule handling.

use crate::tessellation::{TessIndex, TessMesh, TessVertex};
use skia_rs_core::{Point, Rect, Scalar};
use skia_rs_path::{FillType, Path, PathBuilder, PathElement};

/// Fill rule for stencil operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StencilFillRule {
    /// Non-zero winding rule.
    #[default]
    NonZero,
    /// Even-odd (parity) rule.
    EvenOdd,
}

impl From<FillType> for StencilFillRule {
    fn from(fill_type: FillType) -> Self {
        match fill_type {
            FillType::Winding | FillType::InverseWinding => StencilFillRule::NonZero,
            FillType::EvenOdd | FillType::InverseEvenOdd => StencilFillRule::EvenOdd,
        }
    }
}

/// Stencil operation for path rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StencilOp {
    /// Keep current value.
    Keep,
    /// Set to zero.
    Zero,
    /// Replace with reference value.
    Replace,
    /// Increment (saturate).
    IncrSat,
    /// Decrement (saturate).
    DecrSat,
    /// Increment (wrap).
    IncrWrap,
    /// Decrement (wrap).
    DecrWrap,
    /// Invert bits.
    Invert,
}

/// Stencil state configuration.
#[derive(Debug, Clone)]
pub struct StencilState {
    /// Stencil test enabled.
    pub enabled: bool,
    /// Stencil function for front faces.
    pub front_func: StencilFunc,
    /// Stencil operations for front faces.
    pub front_ops: StencilOps,
    /// Stencil function for back faces.
    pub back_func: StencilFunc,
    /// Stencil operations for back faces.
    pub back_ops: StencilOps,
    /// Reference value.
    pub reference: u32,
    /// Read mask.
    pub read_mask: u32,
    /// Write mask.
    pub write_mask: u32,
}

impl Default for StencilState {
    fn default() -> Self {
        Self {
            enabled: false,
            front_func: StencilFunc::Always,
            front_ops: StencilOps::default(),
            back_func: StencilFunc::Always,
            back_ops: StencilOps::default(),
            reference: 0,
            read_mask: 0xFF,
            write_mask: 0xFF,
        }
    }
}

/// Stencil comparison function.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StencilFunc {
    /// Never pass.
    Never,
    /// Always pass.
    #[default]
    Always,
    /// Pass if equal.
    Equal,
    /// Pass if not equal.
    NotEqual,
    /// Pass if less.
    Less,
    /// Pass if less or equal.
    LessEqual,
    /// Pass if greater.
    Greater,
    /// Pass if greater or equal.
    GreaterEqual,
}

/// Stencil operations for fail/pass conditions.
#[derive(Debug, Clone, Copy)]
pub struct StencilOps {
    /// Operation when stencil test fails.
    pub stencil_fail: StencilOp,
    /// Operation when stencil passes but depth fails.
    pub depth_fail: StencilOp,
    /// Operation when both stencil and depth pass.
    pub pass: StencilOp,
}

impl Default for StencilOps {
    fn default() -> Self {
        Self {
            stencil_fail: StencilOp::Keep,
            depth_fail: StencilOp::Keep,
            pass: StencilOp::Keep,
        }
    }
}

/// Configuration for stencil-then-cover rendering.
#[derive(Debug, Clone)]
pub struct StencilCoverConfig {
    /// Fill rule to use.
    pub fill_rule: StencilFillRule,
    /// Whether to use two-sided stencil (for non-zero winding).
    pub two_sided: bool,
}

impl Default for StencilCoverConfig {
    fn default() -> Self {
        Self {
            fill_rule: StencilFillRule::NonZero,
            two_sided: true,
        }
    }
}

/// Stencil pass data for GPU rendering.
#[derive(Debug, Clone)]
pub struct StencilPass {
    /// Mesh to render for stencil filling.
    pub mesh: TessMesh,
    /// Stencil state for this pass.
    pub stencil_state: StencilState,
    /// Whether color writes should be disabled.
    pub color_write_disabled: bool,
}

/// Cover pass data for GPU rendering.
#[derive(Debug, Clone)]
pub struct CoverPass {
    /// Mesh to render for covering.
    pub mesh: TessMesh,
    /// Stencil state for this pass.
    pub stencil_state: StencilState,
    /// Bounding rect of the path.
    pub bounds: Rect,
}

/// Result of stencil-then-cover preparation.
#[derive(Debug, Clone)]
pub struct StencilCoverResult {
    /// Stencil pass.
    pub stencil_pass: StencilPass,
    /// Cover pass.
    pub cover_pass: CoverPass,
}

/// Prepare stencil-then-cover data for a path.
pub fn prepare_stencil_cover(path: &Path, config: &StencilCoverConfig) -> StencilCoverResult {
    // Create stencil mesh from path triangulation
    let stencil_mesh = tessellate_path_for_stencil(path);

    // Create cover mesh (bounding box)
    let bounds = path.bounds();
    let cover_mesh = create_cover_mesh(bounds);

    // Configure stencil states based on fill rule
    let (stencil_state, cover_stencil_state) = match config.fill_rule {
        StencilFillRule::NonZero => create_nonzero_stencil_states(config.two_sided),
        StencilFillRule::EvenOdd => create_evenodd_stencil_states(),
    };

    StencilCoverResult {
        stencil_pass: StencilPass {
            mesh: stencil_mesh,
            stencil_state,
            color_write_disabled: true,
        },
        cover_pass: CoverPass {
            mesh: cover_mesh,
            stencil_state: cover_stencil_state,
            bounds,
        },
    }
}

/// Tessellate a path for stencil rendering.
fn tessellate_path_for_stencil(path: &Path) -> TessMesh {
    let mut mesh = TessMesh::new();

    // Use centroid as fan origin
    let bounds = path.bounds();
    let center = bounds.center();
    let fan_origin = Point::new(center.x, center.y);
    let origin_idx = mesh.add_vertex(TessVertex::from_point(fan_origin));

    let mut current_point = Point::zero();
    let mut prev_vertex: Option<TessIndex> = None;

    for element in path.iter() {
        match element {
            PathElement::Move(p) => {
                current_point = p;
                prev_vertex = Some(mesh.add_vertex(TessVertex::from_point(p)));
            }
            PathElement::Line(p) => {
                let curr_vertex = mesh.add_vertex(TessVertex::from_point(p));
                if let Some(prev) = prev_vertex {
                    mesh.add_triangle(origin_idx, prev, curr_vertex);
                }
                prev_vertex = Some(curr_vertex);
                current_point = p;
            }
            PathElement::Quad(ctrl, end) => {
                let steps = 8;
                for i in 1..=steps {
                    let t = i as Scalar / steps as Scalar;
                    let p = eval_quad(current_point, ctrl, end, t);
                    let curr_vertex = mesh.add_vertex(TessVertex::from_point(p));
                    if let Some(prev) = prev_vertex {
                        mesh.add_triangle(origin_idx, prev, curr_vertex);
                    }
                    prev_vertex = Some(curr_vertex);
                }
                current_point = end;
            }
            PathElement::Conic(ctrl, end, weight) => {
                let steps = 8;
                for i in 1..=steps {
                    let t = i as Scalar / steps as Scalar;
                    let p = eval_conic(current_point, ctrl, end, weight, t);
                    let curr_vertex = mesh.add_vertex(TessVertex::from_point(p));
                    if let Some(prev) = prev_vertex {
                        mesh.add_triangle(origin_idx, prev, curr_vertex);
                    }
                    prev_vertex = Some(curr_vertex);
                }
                current_point = end;
            }
            PathElement::Cubic(ctrl1, ctrl2, end) => {
                let steps = 12;
                for i in 1..=steps {
                    let t = i as Scalar / steps as Scalar;
                    let p = eval_cubic(current_point, ctrl1, ctrl2, end, t);
                    let curr_vertex = mesh.add_vertex(TessVertex::from_point(p));
                    if let Some(prev) = prev_vertex {
                        mesh.add_triangle(origin_idx, prev, curr_vertex);
                    }
                    prev_vertex = Some(curr_vertex);
                }
                current_point = end;
            }
            PathElement::Close => {
                prev_vertex = None;
            }
        }
    }

    mesh
}

/// Create a cover mesh from bounding rect.
fn create_cover_mesh(bounds: Rect) -> TessMesh {
    // Add small padding to ensure full coverage
    let padding = 1.0;
    let padded = Rect::new(
        bounds.left - padding,
        bounds.top - padding,
        bounds.right + padding,
        bounds.bottom + padding,
    );

    crate::tessellation::tessellate_rect(padded)
}

/// Create stencil states for non-zero winding rule.
fn create_nonzero_stencil_states(two_sided: bool) -> (StencilState, StencilState) {
    if two_sided {
        let stencil = StencilState {
            enabled: true,
            front_func: StencilFunc::Always,
            front_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::IncrWrap,
            },
            back_func: StencilFunc::Always,
            back_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::DecrWrap,
            },
            reference: 0,
            read_mask: 0xFF,
            write_mask: 0xFF,
        };

        let cover = StencilState {
            enabled: true,
            front_func: StencilFunc::NotEqual,
            front_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::Zero,
            },
            back_func: StencilFunc::NotEqual,
            back_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::Zero,
            },
            reference: 0,
            read_mask: 0xFF,
            write_mask: 0xFF,
        };

        (stencil, cover)
    } else {
        let stencil = StencilState {
            enabled: true,
            front_func: StencilFunc::Always,
            front_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::IncrWrap,
            },
            back_func: StencilFunc::Always,
            back_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::IncrWrap,
            },
            reference: 0,
            read_mask: 0xFF,
            write_mask: 0xFF,
        };

        let cover = StencilState {
            enabled: true,
            front_func: StencilFunc::NotEqual,
            front_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::Zero,
            },
            back_func: StencilFunc::NotEqual,
            back_ops: StencilOps {
                stencil_fail: StencilOp::Keep,
                depth_fail: StencilOp::Keep,
                pass: StencilOp::Zero,
            },
            reference: 0,
            read_mask: 0xFF,
            write_mask: 0xFF,
        };

        (stencil, cover)
    }
}

/// Create stencil states for even-odd rule.
fn create_evenodd_stencil_states() -> (StencilState, StencilState) {
    let stencil = StencilState {
        enabled: true,
        front_func: StencilFunc::Always,
        front_ops: StencilOps {
            stencil_fail: StencilOp::Keep,
            depth_fail: StencilOp::Keep,
            pass: StencilOp::Invert,
        },
        back_func: StencilFunc::Always,
        back_ops: StencilOps {
            stencil_fail: StencilOp::Keep,
            depth_fail: StencilOp::Keep,
            pass: StencilOp::Invert,
        },
        reference: 0,
        read_mask: 0xFF,
        write_mask: 0x01,
    };

    let cover = StencilState {
        enabled: true,
        front_func: StencilFunc::NotEqual,
        front_ops: StencilOps {
            stencil_fail: StencilOp::Keep,
            depth_fail: StencilOp::Keep,
            pass: StencilOp::Zero,
        },
        back_func: StencilFunc::NotEqual,
        back_ops: StencilOps {
            stencil_fail: StencilOp::Keep,
            depth_fail: StencilOp::Keep,
            pass: StencilOp::Zero,
        },
        reference: 0,
        read_mask: 0x01,
        write_mask: 0xFF,
    };

    (stencil, cover)
}

// Curve evaluation helpers

fn eval_quad(p0: Point, p1: Point, p2: Point, t: Scalar) -> Point {
    let mt = 1.0 - t;
    let mt2 = mt * mt;
    let t2 = t * t;
    Point::new(
        mt2 * p0.x + 2.0 * mt * t * p1.x + t2 * p2.x,
        mt2 * p0.y + 2.0 * mt * t * p1.y + t2 * p2.y,
    )
}

fn eval_conic(p0: Point, p1: Point, p2: Point, w: Scalar, t: Scalar) -> Point {
    let mt = 1.0 - t;
    let mt2 = mt * mt;
    let t2 = t * t;
    let wt = 2.0 * w * mt * t;
    let denom = mt2 + wt + t2;
    Point::new(
        (mt2 * p0.x + wt * p1.x + t2 * p2.x) / denom,
        (mt2 * p0.y + wt * p1.y + t2 * p2.y) / denom,
    )
}

fn eval_cubic(p0: Point, p1: Point, p2: Point, p3: Point, t: Scalar) -> Point {
    let mt = 1.0 - t;
    let mt2 = mt * mt;
    let mt3 = mt2 * mt;
    let t2 = t * t;
    let t3 = t2 * t;
    Point::new(
        mt3 * p0.x + 3.0 * mt2 * t * p1.x + 3.0 * mt * t2 * p2.x + t3 * p3.x,
        mt3 * p0.y + 3.0 * mt2 * t * p1.y + 3.0 * mt * t2 * p2.y + t3 * p3.y,
    )
}

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

    #[test]
    fn test_stencil_fill_rule_conversion() {
        assert_eq!(
            StencilFillRule::from(FillType::Winding),
            StencilFillRule::NonZero
        );
        assert_eq!(
            StencilFillRule::from(FillType::EvenOdd),
            StencilFillRule::EvenOdd
        );
    }

    #[test]
    fn test_stencil_state_default() {
        let state = StencilState::default();
        assert!(!state.enabled);
        assert_eq!(state.reference, 0);
        assert_eq!(state.read_mask, 0xFF);
    }

    #[test]
    fn test_prepare_stencil_cover() {
        let mut builder = PathBuilder::new();
        builder
            .move_to(0.0, 0.0)
            .line_to(100.0, 0.0)
            .line_to(100.0, 100.0)
            .line_to(0.0, 100.0)
            .close();
        let path = builder.build();

        let result = prepare_stencil_cover(&path, &StencilCoverConfig::default());

        assert!(!result.stencil_pass.mesh.is_empty());
        assert!(result.stencil_pass.color_write_disabled);
        assert!(result.stencil_pass.stencil_state.enabled);

        assert!(!result.cover_pass.mesh.is_empty());
        assert!(result.cover_pass.stencil_state.enabled);
    }

    #[test]
    fn test_nonzero_stencil_states() {
        let (stencil, cover) = create_nonzero_stencil_states(true);

        assert!(stencil.enabled);
        assert_eq!(stencil.front_ops.pass, StencilOp::IncrWrap);
        assert_eq!(stencil.back_ops.pass, StencilOp::DecrWrap);

        assert!(cover.enabled);
        assert_eq!(cover.front_func, StencilFunc::NotEqual);
    }

    #[test]
    fn test_evenodd_stencil_states() {
        let (stencil, cover) = create_evenodd_stencil_states();

        assert!(stencil.enabled);
        assert_eq!(stencil.front_ops.pass, StencilOp::Invert);
        assert_eq!(stencil.write_mask, 0x01);

        assert!(cover.enabled);
        assert_eq!(cover.read_mask, 0x01);
    }
}