Skip to main content

strata_gpu_path/
compute.rs

1//! GPU Compute Buffer Layouts, Raw Curve Representations, and Compute Dispatchers.
2
3use crate::curve::{CubicBezier, Point2D, QuadraticBezier};
4
5/// Flag indicating that the path segment belongs to a filled polygon / region.
6pub const FLAG_FILL: u32 = 1 << 0;
7/// Flag indicating that the path segment is stroked with a line width.
8pub const FLAG_STROKE: u32 = 1 << 1;
9/// Flag indicating Even-Odd fill rule (default is Non-Zero winding rule).
10pub const FLAG_EVEN_ODD: u32 = 1 << 2;
11/// Flag indicating that the path segment is closed.
12pub const FLAG_CLOSED: u32 = 1 << 3;
13
14/// 64-byte aligned raw cubic curve input data fed into WebGPU compute pipelines.
15#[repr(C)]
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub struct RawCurveInput {
18    pub p0: [f32; 2],
19    pub p1: [f32; 2],
20    pub p2: [f32; 2],
21    pub p3: [f32; 2],
22    pub stroke_width: f32,
23    pub flags: u32,
24    pub z_index: f32,
25    pub _pad: f32,
26    pub color: [f32; 4],
27}
28
29impl RawCurveInput {
30    pub const BYTE_SIZE: usize = std::mem::size_of::<Self>();
31
32    pub fn new_cubic(
33        cubic: &CubicBezier,
34        stroke_width: f32,
35        flags: u32,
36        color: [f32; 4],
37        z_index: f32,
38    ) -> Self {
39        Self {
40            p0: [cubic.p0.x, cubic.p0.y],
41            p1: [cubic.p1.x, cubic.p1.y],
42            p2: [cubic.p2.x, cubic.p2.y],
43            p3: [cubic.p3.x, cubic.p3.y],
44            stroke_width,
45            flags,
46            z_index,
47            _pad: 0.0,
48            color,
49        }
50    }
51
52    pub fn new_quadratic(
53        quad: &QuadraticBezier,
54        stroke_width: f32,
55        flags: u32,
56        color: [f32; 4],
57        z_index: f32,
58    ) -> Self {
59        // Degree elevate or replicate control point for compute pipeline uniformity
60        Self {
61            p0: [quad.p0.x, quad.p0.y],
62            p1: [quad.p1.x, quad.p1.y],
63            p2: [quad.p1.x, quad.p1.y],
64            p3: [quad.p2.x, quad.p2.y],
65            stroke_width,
66            flags,
67            z_index,
68            _pad: 0.0,
69            color,
70        }
71    }
72
73    pub fn new_line(
74        p0: Point2D,
75        p1: Point2D,
76        stroke_width: f32,
77        flags: u32,
78        color: [f32; 4],
79        z_index: f32,
80    ) -> Self {
81        let mid = p0.lerp(p1, 0.5);
82        Self {
83            p0: [p0.x, p0.y],
84            p1: [mid.x, mid.y],
85            p2: [mid.x, mid.y],
86            p3: [p1.x, p1.y],
87            stroke_width,
88            flags,
89            z_index,
90            _pad: 0.0,
91            color,
92        }
93    }
94
95    #[inline]
96    pub fn as_bytes(&self) -> &[u8] {
97        unsafe { std::slice::from_raw_parts((self as *const Self) as *const u8, Self::BYTE_SIZE) }
98    }
99
100    #[inline]
101    pub fn slice_to_bytes(slice: &[Self]) -> &[u8] {
102        unsafe {
103            std::slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len() * Self::BYTE_SIZE)
104        }
105    }
106}
107
108/// 64-byte aligned output quadratic segment produced by compute shaders or CPU fallback.
109#[repr(C)]
110#[derive(Debug, Clone, Copy, PartialEq)]
111pub struct GpuPathSegment {
112    pub p0: [f32; 2],
113    pub p1: [f32; 2],
114    pub p2: [f32; 2],
115    pub stroke_width: f32,
116    pub flags: u32,
117    pub color: [f32; 4],
118    pub z_index: f32,
119    pub _pad0: f32,
120    pub _pad1: f32,
121    pub _pad2: f32,
122}
123
124impl GpuPathSegment {
125    pub const BYTE_SIZE: usize = std::mem::size_of::<Self>();
126
127    pub fn from_quadratic(
128        quad: &QuadraticBezier,
129        stroke_width: f32,
130        flags: u32,
131        color: [f32; 4],
132        z_index: f32,
133    ) -> Self {
134        Self {
135            p0: [quad.p0.x, quad.p0.y],
136            p1: [quad.p1.x, quad.p1.y],
137            p2: [quad.p2.x, quad.p2.y],
138            stroke_width,
139            flags,
140            color,
141            z_index,
142            _pad0: 0.0,
143            _pad1: 0.0,
144            _pad2: 0.0,
145        }
146    }
147
148    #[inline]
149    pub fn as_bytes(&self) -> &[u8] {
150        unsafe { std::slice::from_raw_parts((self as *const Self) as *const u8, Self::BYTE_SIZE) }
151    }
152
153    #[inline]
154    pub fn slice_to_bytes(slice: &[Self]) -> &[u8] {
155        unsafe {
156            std::slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len() * Self::BYTE_SIZE)
157        }
158    }
159}
160
161/// 16-byte aligned Uniform Buffer object for path compute operations.
162#[repr(C)]
163#[derive(Debug, Clone, Copy, PartialEq)]
164pub struct GpuPathUniforms {
165    pub viewport_size: [f32; 2],
166    pub tolerance: f32,
167    pub curve_count: u32,
168}
169
170impl GpuPathUniforms {
171    pub const BYTE_SIZE: usize = std::mem::size_of::<Self>();
172
173    pub fn new(
174        viewport_width: f32,
175        viewport_height: f32,
176        tolerance: f32,
177        curve_count: u32,
178    ) -> Self {
179        Self {
180            viewport_size: [viewport_width, viewport_height],
181            tolerance,
182            curve_count,
183        }
184    }
185
186    #[inline]
187    pub fn as_bytes(&self) -> &[u8] {
188        unsafe { std::slice::from_raw_parts((self as *const Self) as *const u8, Self::BYTE_SIZE) }
189    }
190}
191
192/// High-performance CPU dispatcher and simulator matching the WGSL compute pipeline layout.
193#[derive(Debug, Clone, Default)]
194pub struct CurveComputeDispatcher {
195    pub input_curves: Vec<RawCurveInput>,
196    pub tolerance: f32,
197}
198
199impl CurveComputeDispatcher {
200    pub fn new(tolerance: f32) -> Self {
201        Self {
202            input_curves: Vec::new(),
203            tolerance: if tolerance <= 0.0 { 0.25 } else { tolerance },
204        }
205    }
206
207    pub fn push_cubic(
208        &mut self,
209        cubic: &CubicBezier,
210        stroke_width: f32,
211        flags: u32,
212        color: [f32; 4],
213        z_index: f32,
214    ) {
215        self.input_curves.push(RawCurveInput::new_cubic(
216            cubic,
217            stroke_width,
218            flags,
219            color,
220            z_index,
221        ));
222    }
223
224    pub fn push_quadratic(
225        &mut self,
226        quad: &QuadraticBezier,
227        stroke_width: f32,
228        flags: u32,
229        color: [f32; 4],
230        z_index: f32,
231    ) {
232        self.input_curves.push(RawCurveInput::new_quadratic(
233            quad,
234            stroke_width,
235            flags,
236            color,
237            z_index,
238        ));
239    }
240
241    pub fn push_line(
242        &mut self,
243        p0: Point2D,
244        p1: Point2D,
245        stroke_width: f32,
246        flags: u32,
247        color: [f32; 4],
248        z_index: f32,
249    ) {
250        self.input_curves.push(RawCurveInput::new_line(
251            p0,
252            p1,
253            stroke_width,
254            flags,
255            color,
256            z_index,
257        ));
258    }
259
260    pub fn clear(&mut self) {
261        self.input_curves.clear();
262    }
263
264    pub fn len(&self) -> usize {
265        self.input_curves.len()
266    }
267
268    pub fn is_empty(&self) -> bool {
269        self.input_curves.is_empty()
270    }
271
272    pub fn input_bytes(&self) -> &[u8] {
273        RawCurveInput::slice_to_bytes(&self.input_curves)
274    }
275
276    /// Simulates the WGSL compute pass on CPU, adaptively decomposing cubics into quadratics.
277    pub fn dispatch_compute(&self) -> Vec<GpuPathSegment> {
278        let mut output = Vec::with_capacity(self.input_curves.len());
279
280        for raw in &self.input_curves {
281            let p0 = Point2D::new(raw.p0[0], raw.p0[1]);
282            let p1 = Point2D::new(raw.p1[0], raw.p1[1]);
283            let p2 = Point2D::new(raw.p2[0], raw.p2[1]);
284            let p3 = Point2D::new(raw.p3[0], raw.p3[1]);
285
286            let cubic = CubicBezier::new(p0, p1, p2, p3);
287            let quads = cubic.to_quadratics(self.tolerance);
288
289            for q in quads {
290                output.push(GpuPathSegment::from_quadratic(
291                    &q,
292                    raw.stroke_width,
293                    raw.flags,
294                    raw.color,
295                    raw.z_index,
296                ));
297            }
298        }
299
300        output
301    }
302}