strata_gpu_path/
buffer.rs1use crate::curve::QuadraticBezier;
4use crate::path::Path;
5
6#[repr(C)]
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub struct GpuCurveInstance {
10 pub p0_x: f32,
11 pub p0_y: f32,
12 pub p1_x: f32,
13 pub p1_y: f32,
14 pub p2_x: f32,
15 pub p2_y: f32,
16 pub flags: u32,
17 pub z_index: f32,
18}
19
20impl GpuCurveInstance {
21 pub const FLAG_FILL: u32 = 1 << 0;
22 pub const FLAG_STROKE: u32 = 1 << 1;
23
24 pub fn from_bezier(curve: &QuadraticBezier, flags: u32, z_index: f32) -> Self {
25 Self {
26 p0_x: curve.p0.x,
27 p0_y: curve.p0.y,
28 p1_x: curve.p1.x,
29 p1_y: curve.p1.y,
30 p2_x: curve.p2.x,
31 p2_y: curve.p2.y,
32 flags,
33 z_index,
34 }
35 }
36}
37
38#[derive(Debug, Default)]
40pub struct PathCommandBuffer {
41 pub instances: Vec<GpuCurveInstance>,
42 pub total_draw_calls: usize,
43}
44
45impl PathCommandBuffer {
46 pub fn new() -> Self {
47 Self::default()
48 }
49
50 pub fn append_path(&mut self, path: &Path, tolerance: f32, z_index: f32) {
52 let quads = path.to_quadratics(tolerance);
53 let mut flags = 0;
54 if path.fill_rule.is_some() {
55 flags |= GpuCurveInstance::FLAG_FILL;
56 }
57 if path.stroke.is_some() {
58 flags |= GpuCurveInstance::FLAG_STROKE;
59 }
60
61 for q in quads {
62 self.instances
63 .push(GpuCurveInstance::from_bezier(&q, flags, z_index));
64 }
65 self.total_draw_calls += 1;
66 }
67
68 pub fn clear(&mut self) {
69 self.instances.clear();
70 self.total_draw_calls = 0;
71 }
72
73 pub fn total_curves(&self) -> usize {
74 self.instances.len()
75 }
76
77 pub fn byte_size(&self) -> usize {
78 self.instances.len() * std::mem::size_of::<GpuCurveInstance>()
79 }
80}