Skip to main content

spatialrust_gpu/kernels/
gpu_segments.rs

1//! GPU-resident voxel segment buffers used across chained kernels.
2
3use spatialrust_core::SpatialResult;
4
5use crate::kernels::voxel_compact::{finalize_segments_from_readback, read_segment_metadata};
6use crate::kernels::voxel_segments::VoxelSegments;
7use crate::runtime::WgpuRuntime;
8
9/// Voxel segment metadata kept on the GPU between sort/compact and reduce passes.
10pub struct GpuVoxelSegments {
11    cell_count: u32,
12    point_count: u32,
13    construction_readback_bytes: u64,
14    keys: wgpu::Buffer,
15    point_indices: wgpu::Buffer,
16    cell_starts: wgpu::Buffer,
17}
18
19impl GpuVoxelSegments {
20    /// Creates GPU segment buffers.
21    pub(crate) fn new(
22        cell_count: u32,
23        point_count: u32,
24        keys: wgpu::Buffer,
25        point_indices: wgpu::Buffer,
26        cell_starts: wgpu::Buffer,
27        construction_readback_bytes: u64,
28    ) -> Self {
29        Self {
30            cell_count,
31            point_count,
32            construction_readback_bytes,
33            keys,
34            point_indices,
35            cell_starts,
36        }
37    }
38
39    /// Returns bytes read back while constructing these GPU segments.
40    ///
41    /// Nonempty compaction synchronizes a four-byte cell count. This excludes
42    /// later explicit metadata readbacks and transfers of the caller's inputs.
43    #[must_use]
44    pub const fn construction_readback_bytes(&self) -> u64 {
45        self.construction_readback_bytes
46    }
47
48    /// Returns the number of occupied voxel cells.
49    #[must_use]
50    pub fn cell_count(&self) -> u32 {
51        self.cell_count
52    }
53
54    /// Returns the source point count represented by these segments.
55    #[must_use]
56    pub fn point_count(&self) -> u32 {
57        self.point_count
58    }
59
60    /// Returns the GPU buffer of unique voxel keys.
61    #[must_use]
62    pub fn keys_buffer(&self) -> &wgpu::Buffer {
63        &self.keys
64    }
65
66    /// Returns the GPU buffer of sorted point indices grouped by cell.
67    #[must_use]
68    pub fn point_indices_buffer(&self) -> &wgpu::Buffer {
69        &self.point_indices
70    }
71
72    /// Returns the GPU buffer of per-cell start offsets into `point_indices`.
73    #[must_use]
74    pub fn cell_starts_buffer(&self) -> &wgpu::Buffer {
75        &self.cell_starts
76    }
77
78    /// Readbacks segment metadata into the CPU-side representation.
79    pub fn to_voxel_segments(&self, runtime: &WgpuRuntime) -> SpatialResult<VoxelSegments> {
80        if self.cell_count == 0 {
81            return Ok(VoxelSegments {
82                keys: Vec::new(),
83                point_indices: Vec::new(),
84                cell_starts: Vec::new(),
85                cell_counts: Vec::new(),
86            });
87        }
88
89        let (keys, cell_starts, cell_counts, point_indices) =
90            read_segment_metadata(runtime, self, self.cell_count as usize)?;
91
92        Ok(finalize_segments_from_readback(keys, point_indices, cell_starts, cell_counts))
93    }
94}