Skip to main content

rustial_renderer_wgpu/gpu/
line_vertex.rs

1//! Vertex type for dedicated line rendering with dash-pattern and SDF
2//! cap/join antialiasing support.
3
4use bytemuck::{Pod, Zeroable};
5
6/// A vertex for the dedicated line GPU pipeline.
7///
8/// Extends the basic vector vertex with per-vertex distance along the
9/// polyline centreline, an extrusion normal, and a cap/join flag.
10/// The fragment shader uses the distance for dash-pattern evaluation
11/// and the cap/join flag to switch between linear edge AA (ribbon body)
12/// and SDF circle-based AA (round caps/joins).
13#[repr(C)]
14#[derive(Debug, Clone, Copy, Pod, Zeroable)]
15pub struct LineVertex {
16    /// Position `[x, y, z]` in camera-relative meters.
17    pub position: [f32; 3],
18    /// Per-vertex RGBA color.
19    pub color: [f32; 4],
20    /// Extrusion normal `[nx, ny]` perpendicular to the line centreline.
21    ///
22    /// For ribbon body vertices the magnitude is 1.0.  For round cap/join
23    /// fan vertices the magnitude is slightly > 1.0 (circumscribed) so
24    /// that the SDF clip at magnitude 1.0 traces a perfect circle.
25    pub line_normal: [f32; 2],
26    /// Distance along the polyline centreline (world-space meters).
27    pub line_distance: f32,
28    /// Cap/join flag: `1.0` for round cap/join fan vertices, `0.0` for
29    /// ribbon body.  When > 0.5, the fragment shader uses SDF circle AA
30    /// instead of linear edge AA and discards fragments beyond the
31    /// ideal circle boundary.
32    pub cap_join: f32,
33}
34
35impl LineVertex {
36    /// Vertex buffer layout for the line pipeline.
37    pub fn layout() -> wgpu::VertexBufferLayout<'static> {
38        wgpu::VertexBufferLayout {
39            array_stride: std::mem::size_of::<LineVertex>() as wgpu::BufferAddress,
40            step_mode: wgpu::VertexStepMode::Vertex,
41            attributes: &[
42                // position
43                wgpu::VertexAttribute {
44                    offset: 0,
45                    shader_location: 0,
46                    format: wgpu::VertexFormat::Float32x3,
47                },
48                // color
49                wgpu::VertexAttribute {
50                    offset: 12,
51                    shader_location: 1,
52                    format: wgpu::VertexFormat::Float32x4,
53                },
54                // line_normal
55                wgpu::VertexAttribute {
56                    offset: 28,
57                    shader_location: 2,
58                    format: wgpu::VertexFormat::Float32x2,
59                },
60                // line_distance
61                wgpu::VertexAttribute {
62                    offset: 36,
63                    shader_location: 3,
64                    format: wgpu::VertexFormat::Float32,
65                },
66                // cap_join
67                wgpu::VertexAttribute {
68                    offset: 40,
69                    shader_location: 4,
70                    format: wgpu::VertexFormat::Float32,
71                },
72            ],
73        }
74    }
75}