Skip to main content

spatialrust_render_wgpu/
geometry.rs

1use spatialrust_viz::{DeviceIdentity, VisualResidency};
2
3use crate::{runtime::BufferSlot, RenderResult, WgpuRenderer};
4
5/// Device-resident geometry topology.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7pub enum GpuGeometryKind {
8    /// Independent points.
9    Points,
10    /// Independent line segments.
11    Lines,
12    /// Indexed triangles.
13    Triangles,
14}
15
16/// Explicitly uploaded, device-resident visual geometry.
17///
18/// Dropping this value releases its buffers. Call [`Self::recycle`] to return
19/// them to the originating renderer's bounded reuse pool.
20pub struct GpuGeometry {
21    pub(crate) renderer_id: u64,
22    pub(crate) kind: GpuGeometryKind,
23    pub(crate) vertex_count: u32,
24    pub(crate) index_count: u32,
25    pub(crate) positions: Option<BufferSlot>,
26    pub(crate) rgb: Option<BufferSlot>,
27    pub(crate) scalar: Option<BufferSlot>,
28    pub(crate) indices: Option<BufferSlot>,
29    residency: VisualResidency,
30}
31
32impl GpuGeometry {
33    /// Geometry topology.
34    #[must_use]
35    pub const fn kind(&self) -> GpuGeometryKind {
36        self.kind
37    }
38
39    /// Number of uploaded vertices.
40    #[must_use]
41    pub const fn vertex_count(&self) -> u32 {
42        self.vertex_count
43    }
44
45    /// Number of uploaded indices.
46    #[must_use]
47    pub const fn index_count(&self) -> u32 {
48        self.index_count
49    }
50
51    /// Residency identifying the exact renderer runtime.
52    #[must_use]
53    pub const fn residency(&self) -> &VisualResidency {
54        &self.residency
55    }
56
57    /// Logical position-buffer bytes uploaded for this geometry.
58    #[must_use]
59    pub fn position_bytes(&self) -> u64 {
60        self.positions.as_ref().map_or(0, |slot| slot.logical_bytes)
61    }
62
63    /// Logical RGB-buffer bytes uploaded for this geometry.
64    #[must_use]
65    pub fn rgb_bytes(&self) -> u64 {
66        self.rgb.as_ref().map_or(0, |slot| slot.logical_bytes)
67    }
68
69    /// Logical scalar-buffer bytes uploaded for this geometry.
70    #[must_use]
71    pub fn scalar_bytes(&self) -> u64 {
72        self.scalar.as_ref().map_or(0, |slot| slot.logical_bytes)
73    }
74
75    /// Logical index-buffer bytes uploaded for this geometry.
76    #[must_use]
77    pub fn index_bytes(&self) -> u64 {
78        self.indices.as_ref().map_or(0, |slot| slot.logical_bytes)
79    }
80
81    /// Returns all buffers to the originating renderer's bounded reuse pool.
82    pub fn recycle(mut self, renderer: &WgpuRenderer) -> RenderResult<()> {
83        renderer.recycle_geometry(&mut self)
84    }
85
86    #[allow(clippy::too_many_arguments)]
87    pub(crate) fn new(
88        renderer_id: u64,
89        kind: GpuGeometryKind,
90        vertex_count: u32,
91        index_count: u32,
92        positions: Option<BufferSlot>,
93        rgb: Option<BufferSlot>,
94        scalar: Option<BufferSlot>,
95        indices: Option<BufferSlot>,
96        device: DeviceIdentity,
97    ) -> Self {
98        Self {
99            renderer_id,
100            kind,
101            vertex_count,
102            index_count,
103            positions,
104            rgb,
105            scalar,
106            indices,
107            residency: VisualResidency::Device(device),
108        }
109    }
110}