Skip to main content

spatialrust_gpu/kernels/
voxel_pipeline.rs

1use spatialrust_core::SpatialResult;
2
3use crate::kernels::gpu_segments::GpuVoxelSegments;
4use crate::kernels::voxel_gather::gather_voxel_first_xyz_gpu_buffers;
5use crate::kernels::voxel_keys::{compute_voxel_keys_gpu_buffers, GpuVoxelKeyBuffers};
6use crate::kernels::voxel_reduce::reduce_voxel_centroids_xyz_gpu_buffers;
7use crate::kernels::voxel_sort::build_voxel_segments_gpu_from_keys_buffer;
8use crate::runtime::WgpuRuntime;
9
10/// GPU-resident centroid downsample result.
11pub struct VoxelCentroidGpuResult {
12    /// Averaged x coordinates per voxel cell.
13    pub out_x: Vec<f32>,
14    /// Averaged y coordinates per voxel cell.
15    pub out_y: Vec<f32>,
16    /// Averaged z coordinates per voxel cell.
17    pub out_z: Vec<f32>,
18    /// Segment metadata kept on the GPU for follow-up reductions.
19    pub segments: GpuVoxelSegments,
20    /// Source position buffers kept on the GPU.
21    pub positions: GpuVoxelKeyBuffers,
22}
23
24/// Runs the chained GPU voxel centroid pipeline without intermediate key/segment readbacks.
25pub fn downsample_voxel_centroid_gpu(
26    runtime: &WgpuRuntime,
27    x: &[f32],
28    y: &[f32],
29    z: &[f32],
30    origin: [f32; 3],
31    inv_leaf: f32,
32) -> SpatialResult<VoxelCentroidGpuResult> {
33    let positions = compute_voxel_keys_gpu_buffers(runtime, x, y, z, origin, inv_leaf)?;
34    let point_count = positions.point_count();
35    let segments = build_voxel_segments_gpu_from_keys_buffer(
36        runtime,
37        positions.keys_buffer(),
38        point_count,
39        point_count,
40    )?;
41    let (out_x, out_y, out_z) = reduce_voxel_centroids_xyz_gpu_buffers(
42        runtime,
43        positions.x_buffer(),
44        positions.y_buffer(),
45        positions.z_buffer(),
46        &segments,
47    )?;
48
49    Ok(VoxelCentroidGpuResult { out_x, out_y, out_z, segments, positions })
50}
51
52/// GPU-resident approximate-first downsample result.
53pub struct VoxelApproximateFirstGpuResult {
54    /// First-point x coordinates per voxel cell.
55    pub out_x: Vec<f32>,
56    /// First-point y coordinates per voxel cell.
57    pub out_y: Vec<f32>,
58    /// First-point z coordinates per voxel cell.
59    pub out_z: Vec<f32>,
60    /// Segment metadata kept on the GPU for follow-up gathers/reductions.
61    pub segments: GpuVoxelSegments,
62    /// Source position buffers kept on the GPU.
63    pub positions: GpuVoxelKeyBuffers,
64}
65
66/// Runs the chained GPU approximate-first pipeline without intermediate readbacks.
67pub fn downsample_voxel_approximate_first_gpu(
68    runtime: &WgpuRuntime,
69    x: &[f32],
70    y: &[f32],
71    z: &[f32],
72    origin: [f32; 3],
73    inv_leaf: f32,
74) -> SpatialResult<VoxelApproximateFirstGpuResult> {
75    let positions = compute_voxel_keys_gpu_buffers(runtime, x, y, z, origin, inv_leaf)?;
76    let point_count = positions.point_count();
77    let segments = build_voxel_segments_gpu_from_keys_buffer(
78        runtime,
79        positions.keys_buffer(),
80        point_count,
81        point_count,
82    )?;
83    let (out_x, out_y, out_z) = gather_voxel_first_xyz_gpu_buffers(
84        runtime,
85        positions.x_buffer(),
86        positions.y_buffer(),
87        positions.z_buffer(),
88        &segments,
89    )?;
90
91    Ok(VoxelApproximateFirstGpuResult { out_x, out_y, out_z, segments, positions })
92}
93
94#[cfg(test)]
95mod tests {
96    use super::{downsample_voxel_approximate_first_gpu, downsample_voxel_centroid_gpu};
97    use crate::kernels::voxel_reduce::reduce_voxel_centroids_xyz;
98    use crate::kernels::voxel_segments::build_voxel_segments;
99    use crate::runtime::WgpuRuntime;
100
101    #[test]
102    fn chained_pipeline_matches_staged_gpu_reference() {
103        let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
104        let x = [0.0_f32, 0.1, 1.0, 1.1];
105        let y = [0.0_f32, 0.0, 0.0, 0.0];
106        let z = [0.0_f32, 0.0, 0.0, 0.0];
107        let origin = [0.0_f32, 0.0, 0.0];
108        let inv_leaf = 2.0_f32;
109
110        let chained = downsample_voxel_centroid_gpu(&runtime, &x, &y, &z, origin, inv_leaf)
111            .expect("chained pipeline");
112
113        let keys: Vec<(i64, i64, i64)> = x
114            .iter()
115            .zip(y.iter())
116            .zip(z.iter())
117            .map(|((x, y), z)| {
118                (
119                    ((x - origin[0]) * inv_leaf).floor() as i64,
120                    ((y - origin[1]) * inv_leaf).floor() as i64,
121                    ((z - origin[2]) * inv_leaf).floor() as i64,
122                )
123            })
124            .collect();
125        let segments = build_voxel_segments(&keys);
126        let (ref_x, ref_y, ref_z) =
127            reduce_voxel_centroids_xyz(&runtime, &x, &y, &z, &segments).expect("staged reduce");
128
129        assert!((chained.out_x[0] - ref_x[0]).abs() < 1e-5);
130        assert!((chained.out_x[1] - ref_x[1]).abs() < 1e-5);
131        assert_eq!(chained.out_y, ref_y);
132        assert_eq!(chained.out_z, ref_z);
133    }
134
135    #[test]
136    fn approximate_first_pipeline_keeps_first_point() {
137        let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
138        let x = [0.0_f32, 0.1];
139        let y = [0.0_f32, 0.0];
140        let z = [0.0_f32, 0.0];
141        let origin = [0.0_f32, 0.0, 0.0];
142        let inv_leaf = 1.0_f32;
143
144        let result = downsample_voxel_approximate_first_gpu(&runtime, &x, &y, &z, origin, inv_leaf)
145            .expect("approximate-first pipeline");
146
147        assert_eq!(result.out_x.len(), 1);
148        assert!((result.out_x[0] - 0.0).abs() < 1e-5);
149    }
150
151    #[test]
152    fn centroid_pipeline_handles_non_pot_point_count() {
153        let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
154        let point_count = 100_000_usize;
155        let mut x = Vec::with_capacity(point_count);
156        let mut y = Vec::with_capacity(point_count);
157        let mut z = Vec::with_capacity(point_count);
158        for index in 0..point_count {
159            let t = index as f32;
160            x.push((t * 0.013).fract() * 100.0);
161            y.push(((index % 97) as f32) * 0.017);
162            z.push(((index % 53) as f32) * 0.019);
163        }
164
165        downsample_voxel_centroid_gpu(&runtime, &x, &y, &z, [0.0; 3], 0.25)
166            .expect("centroid pipeline for 100k points");
167    }
168}