Skip to main content

graphics_core/batch2d/
mod.rs

1//! 2D high-volume batch rendering foundations (lines, cubic beziers, grids, and proxy quads).
2
3/// GPU instanced line segment.
4#[repr(C)]
5#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
6pub struct LineInstance {
7    pub start: [f32; 2],
8    pub end: [f32; 2],
9    pub width: f32,
10    pub _padding: f32,
11    pub color: [f32; 4],
12}
13
14impl LineInstance {
15    pub fn new(start: [f32; 2], end: [f32; 2], width: f32, color: [f32; 4]) -> Self {
16        Self {
17            start,
18            end,
19            width,
20            _padding: 0.0,
21            color,
22        }
23    }
24}
25
26/// GPU instanced cubic Bezier curve for node graphs and flow connections.
27#[repr(C)]
28#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
29pub struct CubicBezierInstance {
30    pub p0: [f32; 2],
31    pub p1: [f32; 2],
32    pub p2: [f32; 2],
33    pub p3: [f32; 2],
34    pub stroke_width: f32,
35    pub _padding: f32,
36    pub color: [f32; 4],
37}
38
39impl CubicBezierInstance {
40    pub fn new(
41        p0: [f32; 2],
42        p1: [f32; 2],
43        p2: [f32; 2],
44        p3: [f32; 2],
45        stroke_width: f32,
46        color: [f32; 4],
47    ) -> Self {
48        Self {
49            p0,
50            p1,
51            p2,
52            p3,
53            stroke_width,
54            _padding: 0.0,
55            color,
56        }
57    }
58}
59
60/// GPU instanced 2D quad for zoomed-out node LOD proxy rendering.
61#[repr(C)]
62#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
63pub struct QuadInstance {
64    pub min: [f32; 2],
65    pub max: [f32; 2],
66    pub color: [f32; 4],
67    pub corner_radius: f32,
68    pub _padding: [f32; 3],
69}
70
71impl QuadInstance {
72    pub fn new(min: [f32; 2], max: [f32; 2], color: [f32; 4], corner_radius: f32) -> Self {
73        Self {
74            min,
75            max,
76            color,
77            corner_radius,
78            _padding: [0.0; 3],
79        }
80    }
81}
82
83/// Uniform configuration for background grid rendering.
84#[repr(C)]
85#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
86pub struct GridConfig {
87    pub cell_size: f32,
88    pub line_width: f32,
89    pub _padding: [f32; 2],
90    pub color: [f32; 4],
91}
92
93impl Default for GridConfig {
94    fn default() -> Self {
95        Self {
96            cell_size: 20.0,
97            line_width: 1.0,
98            _padding: [0.0; 2],
99            color: [0.3, 0.3, 0.3, 1.0],
100        }
101    }
102}