Skip to main content

vello_encoding/
clip.rs

1// Copyright 2023 the Vello Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use bytemuck::{Pod, Zeroable};
5
6/// Clip stack element.
7///
8/// This is the bicyclic semigroup, a monoid useful for representing
9/// stack depth. There is considerably more detail in the draft paper
10///  [Fast GPU bounding boxes on tree-structured scenes].
11///
12/// [Fast GPU bounding boxes on tree-structured scenes]: https://arxiv.org/abs/2205.11659
13#[derive(Copy, Clone, Pod, Zeroable, Debug, Default)]
14#[repr(C)]
15pub struct ClipBic {
16    /// When interpreted as a stack operation, the number of pop operations.
17    pub a: u32,
18    /// When interpreted as a stack operation, the number of push operations.
19    pub b: u32,
20}
21
22/// Clip element.
23#[derive(Copy, Clone, Pod, Zeroable, Debug, Default)]
24#[repr(C)]
25#[expect(
26    clippy::partial_pub_fields,
27    reason = "Padding is meaningless to manipulate directly"
28)]
29pub struct ClipElement {
30    pub parent_ix: u32,
31    _padding: [u8; 12],
32    pub bbox: [f32; 4],
33}
34
35/// Clip resolution.
36///
37/// This is an intermediate element used to match clips to associated paths
38/// and is also used to connect begin and end clip pairs.
39#[derive(Copy, Clone, Pod, Zeroable, Debug, Default)]
40#[repr(C)]
41pub struct Clip {
42    /// Index of the draw object.
43    pub ix: u32,
44    /// This is a packed encoding of an enum with the sign bit as the tag. If positive,
45    /// this entry is a `BeginClip` and contains the associated path index. If negative,
46    /// it is an `EndClip` and contains the bitwise-not of the `EndClip` draw object index.    
47    pub path_ix: i32,
48}
49
50/// Clip bounding box.
51#[derive(Copy, Clone, Pod, Zeroable, Debug, Default)]
52#[repr(C)]
53pub struct ClipBbox {
54    pub bbox: [f32; 4],
55}
56
57impl ClipBic {
58    pub fn new(a: u32, b: u32) -> Self {
59        Self { a, b }
60    }
61
62    /// The bicyclic semigroup operation.
63    ///
64    /// This operation is associative. When interpreted as a stack
65    /// operation, it represents doing the pops of `self`, the pushes of
66    /// `self`, the pops of `other`, and the pushes of `other`. The middle
67    /// two can cancel each other out.
68    #[must_use]
69    pub fn combine(self, other: Self) -> Self {
70        let m = self.b.min(other.a);
71        Self::new(self.a + other.a - m, self.b + other.b - m)
72    }
73}