Skip to main content

spatialrust_gpu/kernels/
voxel_gather.rs

1use bytemuck::{Pod, Zeroable};
2use spatialrust_core::{SpatialError, SpatialResult};
3use wgpu::util::DeviceExt;
4
5use crate::kernels::gpu_segments::GpuVoxelSegments;
6use crate::kernels::voxel_first_average_fused::record_fused_voxel_first_average;
7use crate::kernels::voxel_first_gather_fused::record_fused_voxel_first_gather;
8use crate::kernels::voxel_reduce_fused::plan_fused_voxel_average;
9use crate::kernels::voxel_segments::VoxelSegments;
10use crate::readback::{
11    packed_u8_output_staging_bytes, read_staging_f32, read_staging_f32_channels, read_staging_xyz,
12    read_staging_xyz_and_packed_attributes, u8_storage_upload_contents,
13};
14use crate::runtime::WgpuRuntime;
15
16const WORKGROUP_SIZE: u32 = 256;
17const MULTI2_CHANNELS: usize = 2;
18const MULTI4_CHANNELS: usize = 4;
19
20#[repr(C)]
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Pod, Zeroable)]
22struct GatherUniform {
23    cell_count: u32,
24    point_count: u32,
25    channel_count: u32,
26    _pad: u32,
27}
28
29#[derive(Default)]
30pub(crate) struct LegacyGatherControls {
31    current: Option<(GatherUniform, &'static str, wgpu::Buffer)>,
32}
33
34impl LegacyGatherControls {
35    fn get(
36        &mut self,
37        device: &wgpu::Device,
38        uniform: GatherUniform,
39        label: &'static str,
40    ) -> &wgpu::Buffer {
41        if self.current.as_ref().map(|(key, old_label, _)| (*key, *old_label))
42            != Some((uniform, label))
43        {
44            // Earlier encoded passes retain their original immutable control.
45            let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
46                label: Some(label),
47                contents: bytemuck::bytes_of(&uniform),
48                usage: wgpu::BufferUsages::UNIFORM,
49            });
50            self.current = Some((uniform, label, buffer));
51        }
52        &self.current.as_ref().expect("legacy gather control initialized").2
53    }
54}
55
56/// Gathers the first point's `f32` value within each voxel cell on the GPU.
57pub fn gather_voxel_first_f32(
58    runtime: &WgpuRuntime,
59    values: &[f32],
60    segments: &VoxelSegments,
61) -> SpatialResult<Vec<f32>> {
62    if segments.is_empty() {
63        return Ok(Vec::new());
64    }
65    if values.is_empty() {
66        return Err(SpatialError::InvalidArgument(
67            "cannot gather from empty value buffer".to_owned(),
68        ));
69    }
70
71    let device = runtime.device();
72    let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
73        label: Some("voxel-gather-values"),
74        contents: bytemuck::cast_slice(values),
75        usage: wgpu::BufferUsages::STORAGE,
76    });
77    let indices_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
78        label: Some("voxel-gather-indices"),
79        contents: bytemuck::cast_slice(&segments.point_indices),
80        usage: wgpu::BufferUsages::STORAGE,
81    });
82    let starts_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
83        label: Some("voxel-gather-starts"),
84        contents: bytemuck::cast_slice(&segments.cell_starts),
85        usage: wgpu::BufferUsages::STORAGE,
86    });
87
88    dispatch_voxel_gather_f32(
89        runtime,
90        &values_buffer,
91        &indices_buffer,
92        &starts_buffer,
93        segments.len() as u32,
94        segments.point_indices.len() as u32,
95    )
96}
97
98/// Gathers the first point's `f32` value using GPU-resident segment buffers.
99pub fn gather_voxel_first_f32_gpu_buffers(
100    runtime: &WgpuRuntime,
101    values: &wgpu::Buffer,
102    segments: &GpuVoxelSegments,
103) -> SpatialResult<Vec<f32>> {
104    dispatch_voxel_gather_f32(
105        runtime,
106        values,
107        segments.point_indices_buffer(),
108        segments.cell_starts_buffer(),
109        segments.cell_count(),
110        segments.point_count(),
111    )
112}
113
114/// Uploads `f32` values and gathers the first point per GPU-resident voxel segment.
115pub fn gather_voxel_first_f32_gpu(
116    runtime: &WgpuRuntime,
117    values: &[f32],
118    segments: &GpuVoxelSegments,
119) -> SpatialResult<Vec<f32>> {
120    if segments.cell_count() == 0 {
121        return Ok(Vec::new());
122    }
123    if values.len() != segments.point_count() as usize {
124        return Err(SpatialError::BufferLengthMismatch {
125            expected: segments.point_count() as usize,
126            found: values.len(),
127        });
128    }
129
130    let device = runtime.device();
131    let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
132        label: Some("voxel-gather-values-upload"),
133        contents: bytemuck::cast_slice(values),
134        usage: wgpu::BufferUsages::STORAGE,
135    });
136    gather_voxel_first_f32_gpu_buffers(runtime, &values_buffer, segments)
137}
138
139/// Gathers multiple `f32` channels in one or more GPU dispatches.
140pub fn gather_voxel_first_f32_multi_gpu(
141    runtime: &WgpuRuntime,
142    channels: &[&[f32]],
143    segments: &GpuVoxelSegments,
144) -> SpatialResult<Vec<Vec<f32>>> {
145    if channels.is_empty() {
146        return Ok(Vec::new());
147    }
148    if segments.cell_count() == 0 {
149        return Ok(vec![Vec::new(); channels.len()]);
150    }
151
152    let point_count = segments.point_count() as usize;
153    for channel in channels {
154        if channel.len() != point_count {
155            return Err(SpatialError::BufferLengthMismatch {
156                expected: point_count,
157                found: channel.len(),
158            });
159        }
160    }
161
162    if let Some(result) = super::voxel_standalone_packed::try_gather_voxel_channels_packed(
163        runtime, channels, segments,
164    ) {
165        return result;
166    }
167
168    if let Some(result) =
169        super::voxel_gather_submit::try_gather_voxel_channels_coalesced(runtime, channels, segments)
170    {
171        return result;
172    }
173
174    let max_channels = runtime.max_gather_channels() as usize;
175    let device = runtime.device();
176    let empty = empty_storage_buffer(device)?;
177    let mut gathered = Vec::with_capacity(channels.len());
178
179    for chunk in channels.chunks(max_channels) {
180        let mut value_buffers = [None, None, None, None];
181        for (index, channel) in chunk.iter().enumerate() {
182            value_buffers[index] =
183                Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
184                    label: Some("voxel-gather-multi-values"),
185                    contents: bytemuck::cast_slice(channel),
186                    usage: wgpu::BufferUsages::STORAGE,
187                }));
188        }
189
190        let value_refs: [&wgpu::Buffer; MULTI4_CHANNELS] = std::array::from_fn(|index| {
191            if index < chunk.len() {
192                value_buffers[index].as_ref().expect("value buffer")
193            } else {
194                &empty
195            }
196        });
197
198        gathered.extend(dispatch_voxel_gather_multi_gpu_buffers(
199            runtime,
200            &value_refs,
201            segments,
202            chunk.len() as u32,
203        )?);
204    }
205
206    Ok(gathered)
207}
208
209/// Gathers xyz and multiple f32/u8 attribute channels with one GPU submit/readback.
210pub fn gather_voxel_first_xyz_and_multi_gpu(
211    runtime: &WgpuRuntime,
212    x: &wgpu::Buffer,
213    y: &wgpu::Buffer,
214    z: &wgpu::Buffer,
215    attribute_channels: &[&[f32]],
216    u8_attribute_channels: &[&[u8]],
217    segments: &GpuVoxelSegments,
218) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>, Vec<Vec<f32>>, Vec<Vec<u8>>)> {
219    let attribute_count = attribute_channels.len();
220    let u8_attribute_count = u8_attribute_channels.len();
221    if segments.cell_count() == 0 {
222        return Ok((
223            Vec::new(),
224            Vec::new(),
225            Vec::new(),
226            vec![Vec::new(); attribute_count],
227            vec![Vec::new(); u8_attribute_count],
228        ));
229    }
230
231    let point_count = segments.point_count() as usize;
232    for channel in attribute_channels {
233        if channel.len() != point_count {
234            return Err(SpatialError::BufferLengthMismatch {
235                expected: point_count,
236                found: channel.len(),
237            });
238        }
239    }
240    for channel in u8_attribute_channels {
241        if channel.len() != point_count {
242            return Err(SpatialError::BufferLengthMismatch {
243                expected: point_count,
244                found: channel.len(),
245            });
246        }
247    }
248
249    let device = runtime.device();
250    let queue = runtime.queue();
251    let cell_count = segments.cell_count();
252    let cells = cell_count as usize;
253    let channel_len = cells * std::mem::size_of::<f32>();
254    let f32_channel_count = 3 + attribute_count;
255    let u8_staging_len = packed_u8_output_staging_bytes(cells, u8_attribute_count);
256    let u8_channel_len = packed_u8_output_staging_bytes(cells, 1);
257    let fused_plan = plan_fused_voxel_average(&device.limits(), cell_count, attribute_count);
258    let staging_size = channel_len * f32_channel_count + u8_staging_len;
259    let fused_xyz_attrs4 = attribute_count == 4
260        && u8_attribute_count == 0
261        && runtime.pipelines().voxel_gather.xyz_attrs4_pipeline.is_some();
262
263    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
264        label: Some("voxel-gather-xyz-attrs-staging"),
265        size: staging_size as u64,
266        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
267        mapped_at_creation: false,
268    });
269
270    let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
271        label: Some("voxel-gather-xyz-attrs-encoder"),
272    });
273    let mut upload_recycle = Vec::new();
274
275    if fused_xyz_attrs4 {
276        for channel in attribute_channels {
277            upload_recycle
278                .push(runtime.upload_f32_storage("voxel-gather-xyz-attrs4-values", channel)?);
279        }
280        let attr_refs: [&wgpu::Buffer; MULTI4_CHANNELS] =
281            std::array::from_fn(|index| &upload_recycle[index]);
282        let packed_output = device.create_buffer(&wgpu::BufferDescriptor {
283            label: Some("voxel-gather-xyz-attrs4-packed-output"),
284            size: (channel_len * f32_channel_count) as u64,
285            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
286            mapped_at_creation: false,
287        });
288        record_voxel_gather_xyz_and_attrs4_packed_pass(
289            &mut encoder,
290            runtime,
291            x,
292            y,
293            z,
294            &attr_refs,
295            segments,
296            &packed_output,
297        )?;
298        encoder.copy_buffer_to_buffer(
299            &packed_output,
300            0,
301            &staging_buffer,
302            0,
303            (channel_len * f32_channel_count) as u64,
304        );
305    } else if let Some(plan) = fused_plan {
306        let output = device.create_buffer(&wgpu::BufferDescriptor {
307            label: Some("voxel-fused-first-gather-output"),
308            size: plan.output_bytes,
309            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
310            mapped_at_creation: false,
311        });
312        for channel in attribute_channels {
313            upload_recycle
314                .push(runtime.upload_f32_storage("voxel-gather-xyz-attrs-values", channel)?);
315        }
316        record_fused_voxel_first_gather(
317            &mut encoder,
318            runtime,
319            [x, y, z],
320            &upload_recycle,
321            segments,
322            &output,
323        );
324        encoder.copy_buffer_to_buffer(&output, 0, &staging_buffer, 0, plan.output_bytes);
325    } else if !super::voxel_wide_first::try_record_wide_voxel_first(
326        &mut encoder,
327        runtime,
328        [x, y, z],
329        attribute_channels,
330        segments,
331        &staging_buffer,
332        super::voxel_xyz_packed::XyzMode::First,
333        &mut upload_recycle,
334    )? {
335        if !super::voxel_xyz_packed::try_record_packed_xyz_to_staging(
336            &mut encoder,
337            runtime,
338            [x, y, z],
339            segments,
340            &staging_buffer,
341            0,
342            super::voxel_xyz_packed::XyzMode::First,
343        ) {
344            let output_x = device.create_buffer(&wgpu::BufferDescriptor {
345                label: Some("voxel-gather-xyz-out-x"),
346                size: channel_len as u64,
347                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
348                mapped_at_creation: false,
349            });
350            let output_y = device.create_buffer(&wgpu::BufferDescriptor {
351                label: Some("voxel-gather-xyz-out-y"),
352                size: channel_len as u64,
353                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
354                mapped_at_creation: false,
355            });
356            let output_z = device.create_buffer(&wgpu::BufferDescriptor {
357                label: Some("voxel-gather-xyz-out-z"),
358                size: channel_len as u64,
359                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
360                mapped_at_creation: false,
361            });
362
363            record_voxel_gather_xyz_pass(
364                &mut encoder,
365                runtime,
366                x,
367                y,
368                z,
369                segments,
370                &output_x,
371                &output_y,
372                &output_z,
373            )?;
374
375            encoder.copy_buffer_to_buffer(&output_x, 0, &staging_buffer, 0, channel_len as u64);
376            encoder.copy_buffer_to_buffer(
377                &output_y,
378                0,
379                &staging_buffer,
380                channel_len as u64,
381                channel_len as u64,
382            );
383            encoder.copy_buffer_to_buffer(
384                &output_z,
385                0,
386                &staging_buffer,
387                (channel_len * 2) as u64,
388                channel_len as u64,
389            );
390        }
391
392        record_gather_f32_attribute_channels_to_staging(
393            &mut encoder,
394            runtime,
395            attribute_channels,
396            segments,
397            &staging_buffer,
398            channel_len as u64,
399            &mut upload_recycle,
400        )?;
401    }
402
403    let u8_region_offset = (channel_len * f32_channel_count) as u64;
404    if !super::voxel_u8_wide::try_record_wide_voxel_u8_to_staging(
405        &mut encoder,
406        runtime,
407        u8_attribute_channels,
408        segments,
409        &staging_buffer,
410        u8_region_offset,
411        false,
412    )? && !crate::kernels::voxel_u8_batch::try_record_voxel_u8_batch_to_staging(
413        &mut encoder,
414        runtime,
415        u8_attribute_channels,
416        segments,
417        &staging_buffer,
418        u8_region_offset,
419        false,
420    ) {
421        for (attribute_index, channel) in u8_attribute_channels.iter().enumerate() {
422            let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
423                label: Some("voxel-gather-xyz-u8-values"),
424                contents: &u8_storage_upload_contents(channel),
425                usage: wgpu::BufferUsages::STORAGE,
426            });
427            let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
428                label: Some("voxel-gather-xyz-u8-output"),
429                size: u8_channel_len as u64,
430                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
431                mapped_at_creation: false,
432            });
433
434            record_voxel_gather_u8_packed_pass(
435                &mut encoder,
436                runtime,
437                &values_buffer,
438                segments.point_indices_buffer(),
439                segments.cell_starts_buffer(),
440                cell_count,
441                segments.point_count(),
442                &output_buffer,
443            )?;
444
445            encoder.copy_buffer_to_buffer(
446                &output_buffer,
447                0,
448                &staging_buffer,
449                u8_region_offset + attribute_index as u64 * u8_channel_len as u64,
450                u8_channel_len as u64,
451            );
452        }
453    }
454
455    queue.submit(Some(encoder.finish()));
456
457    let output = read_staging_xyz_and_packed_attributes(
458        device,
459        &staging_buffer,
460        cells,
461        attribute_count,
462        u8_attribute_count,
463    )?;
464    for buffer in upload_recycle {
465        runtime.recycle_storage(buffer.size(), buffer);
466    }
467    Ok(output)
468}
469
470/// Gathers xyz and averages f32/u8 attribute channels with one GPU submit/readback.
471pub fn gather_voxel_first_xyz_and_average_multi_gpu(
472    runtime: &WgpuRuntime,
473    x: &wgpu::Buffer,
474    y: &wgpu::Buffer,
475    z: &wgpu::Buffer,
476    attribute_channels: &[&[f32]],
477    u8_attribute_channels: &[&[u8]],
478    segments: &GpuVoxelSegments,
479) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>, Vec<Vec<f32>>, Vec<Vec<u8>>)> {
480    use crate::kernels::voxel_reduce::record_voxel_reduce_f32_pass;
481    use crate::kernels::voxel_reduce::record_voxel_reduce_u8_packed_pass;
482
483    let attribute_count = attribute_channels.len();
484    let u8_attribute_count = u8_attribute_channels.len();
485    if segments.cell_count() == 0 {
486        return Ok((
487            Vec::new(),
488            Vec::new(),
489            Vec::new(),
490            vec![Vec::new(); attribute_count],
491            vec![Vec::new(); u8_attribute_count],
492        ));
493    }
494
495    let point_count = segments.point_count() as usize;
496    for channel in attribute_channels {
497        if channel.len() != point_count {
498            return Err(SpatialError::BufferLengthMismatch {
499                expected: point_count,
500                found: channel.len(),
501            });
502        }
503    }
504    for channel in u8_attribute_channels {
505        if channel.len() != point_count {
506            return Err(SpatialError::BufferLengthMismatch {
507                expected: point_count,
508                found: channel.len(),
509            });
510        }
511    }
512
513    let device = runtime.device();
514    let queue = runtime.queue();
515    let cell_count = segments.cell_count();
516    let cells = cell_count as usize;
517    let channel_len = cells * std::mem::size_of::<f32>();
518    let f32_channel_count = 3 + attribute_count;
519    let u8_staging_len = packed_u8_output_staging_bytes(cells, u8_attribute_count);
520    let u8_channel_len = packed_u8_output_staging_bytes(cells, 1);
521    let fused_plan = plan_fused_voxel_average(&device.limits(), cell_count, attribute_count);
522    let staging_size = channel_len * f32_channel_count + u8_staging_len;
523    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
524        label: Some("voxel-gather-xyz-reduce-attrs-staging"),
525        size: staging_size as u64,
526        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
527        mapped_at_creation: false,
528    });
529    let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
530        label: Some("voxel-gather-xyz-reduce-attrs-encoder"),
531    });
532    if let Some(plan) = fused_plan {
533        let output = device.create_buffer(&wgpu::BufferDescriptor {
534            label: Some("voxel-fused-first-average-output"),
535            size: plan.output_bytes,
536            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
537            mapped_at_creation: false,
538        });
539        let attributes: Vec<_> = attribute_channels
540            .iter()
541            .map(|channel| {
542                device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
543                    label: Some("voxel-reduce-xyz-attrs-values"),
544                    contents: bytemuck::cast_slice(channel),
545                    usage: wgpu::BufferUsages::STORAGE,
546                })
547            })
548            .collect();
549        record_fused_voxel_first_average(
550            &mut encoder,
551            runtime,
552            [x, y, z],
553            &attributes,
554            segments,
555            &output,
556        );
557        encoder.copy_buffer_to_buffer(&output, 0, &staging_buffer, 0, plan.output_bytes);
558    } else if !super::voxel_wide_average::try_record_wide_voxel_average(
559        &mut encoder,
560        runtime,
561        [x, y, z],
562        attribute_channels,
563        segments,
564        &staging_buffer,
565        super::voxel_xyz_packed::XyzMode::First,
566    )? {
567        if !super::voxel_xyz_packed::try_record_packed_xyz_to_staging(
568            &mut encoder,
569            runtime,
570            [x, y, z],
571            segments,
572            &staging_buffer,
573            0,
574            super::voxel_xyz_packed::XyzMode::First,
575        ) {
576            let output_x = device.create_buffer(&wgpu::BufferDescriptor {
577                label: Some("voxel-gather-xyz-out-x"),
578                size: channel_len as u64,
579                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
580                mapped_at_creation: false,
581            });
582            let output_y = device.create_buffer(&wgpu::BufferDescriptor {
583                label: Some("voxel-gather-xyz-out-y"),
584                size: channel_len as u64,
585                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
586                mapped_at_creation: false,
587            });
588            let output_z = device.create_buffer(&wgpu::BufferDescriptor {
589                label: Some("voxel-gather-xyz-out-z"),
590                size: channel_len as u64,
591                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
592                mapped_at_creation: false,
593            });
594
595            record_voxel_gather_xyz_pass(
596                &mut encoder,
597                runtime,
598                x,
599                y,
600                z,
601                segments,
602                &output_x,
603                &output_y,
604                &output_z,
605            )?;
606            encoder.copy_buffer_to_buffer(&output_x, 0, &staging_buffer, 0, channel_len as u64);
607            encoder.copy_buffer_to_buffer(
608                &output_y,
609                0,
610                &staging_buffer,
611                channel_len as u64,
612                channel_len as u64,
613            );
614            encoder.copy_buffer_to_buffer(
615                &output_z,
616                0,
617                &staging_buffer,
618                (channel_len * 2) as u64,
619                channel_len as u64,
620            );
621        }
622
623        if !super::voxel_average_batch::try_record_voxel_attribute_averages_to_staging(
624            &mut encoder,
625            runtime,
626            attribute_channels,
627            segments,
628            &staging_buffer,
629            (channel_len * 3) as u64,
630        )? {
631            for (attribute_index, channel) in attribute_channels.iter().enumerate() {
632                let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
633                    label: Some("voxel-reduce-xyz-attrs-values"),
634                    contents: bytemuck::cast_slice(channel),
635                    usage: wgpu::BufferUsages::STORAGE,
636                });
637                let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
638                    label: Some("voxel-reduce-xyz-attrs-output"),
639                    size: channel_len as u64,
640                    usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
641                    mapped_at_creation: false,
642                });
643                record_voxel_reduce_f32_pass(
644                    &mut encoder,
645                    runtime,
646                    &values_buffer,
647                    segments.point_indices_buffer(),
648                    segments.cell_starts_buffer(),
649                    cell_count,
650                    segments.point_count(),
651                    &output_buffer,
652                )?;
653                encoder.copy_buffer_to_buffer(
654                    &output_buffer,
655                    0,
656                    &staging_buffer,
657                    (channel_len * (3 + attribute_index)) as u64,
658                    channel_len as u64,
659                );
660            }
661        }
662    }
663
664    let u8_region_offset = (channel_len * f32_channel_count) as u64;
665    if !super::voxel_u8_wide::try_record_wide_voxel_u8_to_staging(
666        &mut encoder,
667        runtime,
668        u8_attribute_channels,
669        segments,
670        &staging_buffer,
671        u8_region_offset,
672        true,
673    )? && !crate::kernels::voxel_u8_batch::try_record_voxel_u8_batch_to_staging(
674        &mut encoder,
675        runtime,
676        u8_attribute_channels,
677        segments,
678        &staging_buffer,
679        u8_region_offset,
680        true,
681    ) {
682        for (attribute_index, channel) in u8_attribute_channels.iter().enumerate() {
683            let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
684                label: Some("voxel-reduce-xyz-u8-values"),
685                contents: &u8_storage_upload_contents(channel),
686                usage: wgpu::BufferUsages::STORAGE,
687            });
688            let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
689                label: Some("voxel-reduce-xyz-u8-output"),
690                size: u8_channel_len as u64,
691                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
692                mapped_at_creation: false,
693            });
694            record_voxel_reduce_u8_packed_pass(
695                &mut encoder,
696                runtime,
697                &values_buffer,
698                segments.point_indices_buffer(),
699                segments.cell_starts_buffer(),
700                cell_count,
701                segments.point_count(),
702                &output_buffer,
703            )?;
704            encoder.copy_buffer_to_buffer(
705                &output_buffer,
706                0,
707                &staging_buffer,
708                u8_region_offset + attribute_index as u64 * u8_channel_len as u64,
709                u8_channel_len as u64,
710            );
711        }
712    }
713
714    queue.submit(Some(encoder.finish()));
715    let output = read_staging_xyz_and_packed_attributes(
716        device,
717        &staging_buffer,
718        cells,
719        attribute_count,
720        u8_attribute_count,
721    )?;
722    Ok(output)
723}
724
725/// Gathers xyz coordinates of the first point within each voxel cell on the GPU.
726pub fn gather_voxel_first_xyz_gpu_buffers(
727    runtime: &WgpuRuntime,
728    x: &wgpu::Buffer,
729    y: &wgpu::Buffer,
730    z: &wgpu::Buffer,
731    segments: &GpuVoxelSegments,
732) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>)> {
733    if segments.cell_count() == 0 {
734        return Ok((Vec::new(), Vec::new(), Vec::new()));
735    }
736
737    if let Some(result) = super::voxel_xyz_packed::try_voxel_xyz_packed(
738        runtime,
739        [x, y, z],
740        segments,
741        super::voxel_xyz_packed::XyzMode::First,
742    ) {
743        return result;
744    }
745
746    let device = runtime.device();
747    let queue = runtime.queue();
748    let cell_count = segments.cell_count();
749    let channel_len = cell_count as usize * std::mem::size_of::<f32>();
750    let output_x = device.create_buffer(&wgpu::BufferDescriptor {
751        label: Some("voxel-gather-xyz-out-x"),
752        size: channel_len as u64,
753        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
754        mapped_at_creation: false,
755    });
756    let output_y = device.create_buffer(&wgpu::BufferDescriptor {
757        label: Some("voxel-gather-xyz-out-y"),
758        size: channel_len as u64,
759        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
760        mapped_at_creation: false,
761    });
762    let output_z = device.create_buffer(&wgpu::BufferDescriptor {
763        label: Some("voxel-gather-xyz-out-z"),
764        size: channel_len as u64,
765        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
766        mapped_at_creation: false,
767    });
768    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
769        label: Some("voxel-gather-xyz-staging"),
770        size: (channel_len * 3) as u64,
771        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
772        mapped_at_creation: false,
773    });
774
775    let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
776        label: Some("voxel-gather-xyz-encoder"),
777    });
778    record_voxel_gather_xyz_pass(
779        &mut encoder,
780        runtime,
781        x,
782        y,
783        z,
784        segments,
785        &output_x,
786        &output_y,
787        &output_z,
788    )?;
789    encoder.copy_buffer_to_buffer(&output_x, 0, &staging_buffer, 0, channel_len as u64);
790    encoder.copy_buffer_to_buffer(
791        &output_y,
792        0,
793        &staging_buffer,
794        channel_len as u64,
795        channel_len as u64,
796    );
797    encoder.copy_buffer_to_buffer(
798        &output_z,
799        0,
800        &staging_buffer,
801        (channel_len * 2) as u64,
802        channel_len as u64,
803    );
804    queue.submit(Some(encoder.finish()));
805
806    read_staging_xyz(device, &staging_buffer, cell_count as usize)
807}
808
809pub(crate) fn record_voxel_gather_xyz_and_attrs4_packed_pass(
810    encoder: &mut wgpu::CommandEncoder,
811    runtime: &WgpuRuntime,
812    x: &wgpu::Buffer,
813    y: &wgpu::Buffer,
814    z: &wgpu::Buffer,
815    attribute_buffers: &[&wgpu::Buffer; MULTI4_CHANNELS],
816    segments: &GpuVoxelSegments,
817    packed_output: &wgpu::Buffer,
818) -> SpatialResult<()> {
819    if segments.cell_count() == 0 {
820        return Ok(());
821    }
822
823    let device = runtime.device();
824    let pipelines = runtime.pipelines();
825    let xyz_attrs4_pipeline =
826        pipelines.voxel_gather.xyz_attrs4_pipeline.as_ref().ok_or_else(|| {
827            SpatialError::InvalidArgument(
828                "fused xyz+4-attribute gather pipeline is unavailable on this gpu adapter"
829                    .to_owned(),
830            )
831        })?;
832    let xyz_attrs4_layout =
833        pipelines.voxel_gather.xyz_attrs4_bind_group_layout.as_ref().expect("xyz attrs4 layout");
834    let cell_count = segments.cell_count();
835    let uniform = GatherUniform {
836        cell_count,
837        point_count: segments.point_count(),
838        channel_count: 0,
839        _pad: 0,
840    };
841    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
842        label: Some("voxel-gather-xyz-attrs4-uniform"),
843        contents: bytemuck::bytes_of(&uniform),
844        usage: wgpu::BufferUsages::UNIFORM,
845    });
846    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
847        label: Some("voxel-gather-xyz-attrs4-bind-group"),
848        layout: xyz_attrs4_layout,
849        entries: &[
850            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
851            wgpu::BindGroupEntry {
852                binding: 1,
853                resource: segments.point_indices_buffer().as_entire_binding(),
854            },
855            wgpu::BindGroupEntry {
856                binding: 2,
857                resource: segments.cell_starts_buffer().as_entire_binding(),
858            },
859            wgpu::BindGroupEntry { binding: 3, resource: x.as_entire_binding() },
860            wgpu::BindGroupEntry { binding: 4, resource: y.as_entire_binding() },
861            wgpu::BindGroupEntry { binding: 5, resource: z.as_entire_binding() },
862            wgpu::BindGroupEntry { binding: 6, resource: attribute_buffers[0].as_entire_binding() },
863            wgpu::BindGroupEntry { binding: 7, resource: attribute_buffers[1].as_entire_binding() },
864            wgpu::BindGroupEntry { binding: 8, resource: attribute_buffers[2].as_entire_binding() },
865            wgpu::BindGroupEntry { binding: 9, resource: attribute_buffers[3].as_entire_binding() },
866            wgpu::BindGroupEntry { binding: 10, resource: packed_output.as_entire_binding() },
867        ],
868    });
869    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
870        label: Some("voxel-gather-xyz-attrs4-pass"),
871        timestamp_writes: None,
872    });
873    pass.set_pipeline(xyz_attrs4_pipeline);
874    pass.set_bind_group(0, &bind_group, &[]);
875    pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
876    Ok(())
877}
878
879/// Records batched first-point f32 attribute gathers into a unified xyz staging buffer.
880pub(crate) fn record_gather_f32_attribute_channels_to_staging(
881    encoder: &mut wgpu::CommandEncoder,
882    runtime: &WgpuRuntime,
883    attribute_channels: &[&[f32]],
884    segments: &GpuVoxelSegments,
885    staging_buffer: &wgpu::Buffer,
886    channel_len: u64,
887    upload_recycle: &mut Vec<wgpu::Buffer>,
888) -> SpatialResult<()> {
889    if attribute_channels.is_empty() {
890        return Ok(());
891    }
892
893    if super::voxel_gather_packed::try_record_packed_voxel_attributes(
894        encoder,
895        runtime,
896        attribute_channels,
897        segments,
898        staging_buffer,
899        channel_len * 3,
900        upload_recycle,
901    )? {
902        return Ok(());
903    }
904
905    record_gather_f32_attribute_fallback_to_staging(
906        encoder,
907        runtime,
908        attribute_channels,
909        segments,
910        staging_buffer,
911        channel_len,
912        upload_recycle,
913    )
914}
915
916fn record_gather_f32_attribute_fallback_to_staging(
917    encoder: &mut wgpu::CommandEncoder,
918    runtime: &WgpuRuntime,
919    attribute_channels: &[&[f32]],
920    segments: &GpuVoxelSegments,
921    staging_buffer: &wgpu::Buffer,
922    channel_len: u64,
923    upload_recycle: &mut Vec<wgpu::Buffer>,
924) -> SpatialResult<()> {
925    let device = runtime.device();
926    let max_channels = runtime.max_gather_channels().max(1) as usize;
927    let reusable_outputs: Vec<wgpu::Buffer> = (0..attribute_channels.len().min(max_channels))
928        .map(|_| {
929            device.create_buffer(&wgpu::BufferDescriptor {
930                label: Some("voxel-gather-xyz-attrs-output"),
931                size: channel_len,
932                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
933                mapped_at_creation: false,
934            })
935        })
936        .collect();
937
938    for chunk_start in (0..attribute_channels.len()).step_by(max_channels) {
939        let chunk_end = (chunk_start + max_channels).min(attribute_channels.len());
940        let chunk = &attribute_channels[chunk_start..chunk_end];
941        let channels_in_chunk = chunk.len();
942
943        let chunk_upload_start = upload_recycle.len();
944        for channel in chunk {
945            upload_recycle
946                .push(runtime.upload_f32_storage("voxel-gather-xyz-attrs-values", channel)?);
947        }
948        let value_buffers = &upload_recycle[chunk_upload_start..];
949
950        let output_buffers = &reusable_outputs[..channels_in_chunk];
951
952        match channels_in_chunk {
953            1 => record_voxel_gather_f32_pass(
954                encoder,
955                runtime,
956                &value_buffers[0],
957                segments.point_indices_buffer(),
958                segments.cell_starts_buffer(),
959                segments.cell_count(),
960                segments.point_count(),
961                &output_buffers[0],
962            )?,
963            2 => {
964                let values: [&wgpu::Buffer; MULTI2_CHANNELS] =
965                    [&value_buffers[0], &value_buffers[1]];
966                let outputs: [&wgpu::Buffer; MULTI2_CHANNELS] =
967                    [&output_buffers[0], &output_buffers[1]];
968                record_voxel_gather_multi2_pass(encoder, runtime, &values, segments, &outputs, 2)?;
969            }
970            _ => {
971                if runtime.pipelines().voxel_gather.multi4_pipeline.is_some() {
972                    record_voxel_gather_multi4_active_outputs(
973                        encoder,
974                        runtime,
975                        value_buffers,
976                        segments,
977                        output_buffers,
978                        channel_len,
979                    )?;
980                } else {
981                    for (local_index, channel) in chunk.iter().enumerate() {
982                        let chunk_upload_start = upload_recycle.len();
983                        upload_recycle.push(
984                            runtime.upload_f32_storage("voxel-gather-xyz-attrs-values", channel)?,
985                        );
986                        record_voxel_gather_f32_pass(
987                            encoder,
988                            runtime,
989                            &upload_recycle[chunk_upload_start],
990                            segments.point_indices_buffer(),
991                            segments.cell_starts_buffer(),
992                            segments.cell_count(),
993                            segments.point_count(),
994                            &output_buffers[local_index],
995                        )?;
996                    }
997                }
998            }
999        }
1000
1001        // Preserve every active plane before the next batch overwrites it.
1002        for (local_index, output_buffer) in output_buffers.iter().enumerate() {
1003            encoder.copy_buffer_to_buffer(
1004                output_buffer,
1005                0,
1006                staging_buffer,
1007                channel_len * (3 + chunk_start + local_index) as u64,
1008                channel_len,
1009            );
1010        }
1011    }
1012
1013    Ok(())
1014}
1015
1016fn record_voxel_gather_multi4_active_outputs(
1017    encoder: &mut wgpu::CommandEncoder,
1018    runtime: &WgpuRuntime,
1019    value_buffers: &[wgpu::Buffer],
1020    segments: &GpuVoxelSegments,
1021    output_buffers: &[wgpu::Buffer],
1022    channel_len: u64,
1023) -> SpatialResult<()> {
1024    let device = runtime.device();
1025    let channel_count = output_buffers.len();
1026    let empty = empty_storage_buffer(device)?;
1027    let dummy_outputs: Vec<wgpu::Buffer> = (channel_count..MULTI4_CHANNELS)
1028        .map(|_| {
1029            device.create_buffer(&wgpu::BufferDescriptor {
1030                label: Some("voxel-gather-xyz-attrs-dummy-output"),
1031                size: channel_len,
1032                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
1033                mapped_at_creation: false,
1034            })
1035        })
1036        .collect();
1037    let values: [&wgpu::Buffer; MULTI4_CHANNELS] =
1038        std::array::from_fn(
1039            |index| {
1040                if index < channel_count {
1041                    &value_buffers[index]
1042                } else {
1043                    &empty
1044                }
1045            },
1046        );
1047    let outputs: [&wgpu::Buffer; MULTI4_CHANNELS] = std::array::from_fn(|index| {
1048        if index < channel_count {
1049            &output_buffers[index]
1050        } else {
1051            &dummy_outputs[index - channel_count]
1052        }
1053    });
1054    record_voxel_gather_multi4_pass(
1055        encoder,
1056        runtime,
1057        &values,
1058        segments,
1059        &outputs,
1060        channel_count as u32,
1061    )?;
1062    Ok(())
1063}
1064
1065pub(crate) fn record_voxel_gather_multi2_pass(
1066    encoder: &mut wgpu::CommandEncoder,
1067    runtime: &WgpuRuntime,
1068    values: &[&wgpu::Buffer; MULTI2_CHANNELS],
1069    segments: &GpuVoxelSegments,
1070    outputs: &[&wgpu::Buffer; MULTI2_CHANNELS],
1071    channel_count: u32,
1072) -> SpatialResult<()> {
1073    record_voxel_gather_multi2_pass_with_controls(
1074        encoder,
1075        runtime,
1076        values,
1077        segments,
1078        outputs,
1079        channel_count,
1080        &mut LegacyGatherControls::default(),
1081    )
1082}
1083
1084pub(crate) fn record_voxel_gather_multi2_pass_with_controls(
1085    encoder: &mut wgpu::CommandEncoder,
1086    runtime: &WgpuRuntime,
1087    values: &[&wgpu::Buffer; MULTI2_CHANNELS],
1088    segments: &GpuVoxelSegments,
1089    outputs: &[&wgpu::Buffer; MULTI2_CHANNELS],
1090    channel_count: u32,
1091    controls: &mut LegacyGatherControls,
1092) -> SpatialResult<()> {
1093    let device = runtime.device();
1094    let pipelines = runtime.pipelines();
1095    let cell_count = segments.cell_count();
1096    let uniform =
1097        GatherUniform { cell_count, point_count: segments.point_count(), channel_count, _pad: 0 };
1098    let uniform_buffer = controls.get(device, uniform, "voxel-gather-multi-uniform");
1099    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1100        label: Some("voxel-gather-multi-bind-group"),
1101        layout: &pipelines.voxel_gather.multi_bind_group_layout,
1102        entries: &[
1103            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1104            wgpu::BindGroupEntry {
1105                binding: 1,
1106                resource: segments.point_indices_buffer().as_entire_binding(),
1107            },
1108            wgpu::BindGroupEntry {
1109                binding: 2,
1110                resource: segments.cell_starts_buffer().as_entire_binding(),
1111            },
1112            wgpu::BindGroupEntry { binding: 3, resource: values[0].as_entire_binding() },
1113            wgpu::BindGroupEntry { binding: 4, resource: values[1].as_entire_binding() },
1114            wgpu::BindGroupEntry { binding: 5, resource: outputs[0].as_entire_binding() },
1115            wgpu::BindGroupEntry { binding: 6, resource: outputs[1].as_entire_binding() },
1116        ],
1117    });
1118    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1119        label: Some("voxel-gather-multi-pass"),
1120        timestamp_writes: None,
1121    });
1122    pass.set_pipeline(&pipelines.voxel_gather.multi_pipeline);
1123    pass.set_bind_group(0, &bind_group, &[]);
1124    pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1125    Ok(())
1126}
1127
1128pub(crate) fn record_voxel_gather_multi4_pass(
1129    encoder: &mut wgpu::CommandEncoder,
1130    runtime: &WgpuRuntime,
1131    values: &[&wgpu::Buffer; MULTI4_CHANNELS],
1132    segments: &GpuVoxelSegments,
1133    outputs: &[&wgpu::Buffer; MULTI4_CHANNELS],
1134    channel_count: u32,
1135) -> SpatialResult<()> {
1136    record_voxel_gather_multi4_pass_with_controls(
1137        encoder,
1138        runtime,
1139        values,
1140        segments,
1141        outputs,
1142        channel_count,
1143        &mut LegacyGatherControls::default(),
1144    )
1145}
1146
1147pub(crate) fn record_voxel_gather_multi4_pass_with_controls(
1148    encoder: &mut wgpu::CommandEncoder,
1149    runtime: &WgpuRuntime,
1150    values: &[&wgpu::Buffer; MULTI4_CHANNELS],
1151    segments: &GpuVoxelSegments,
1152    outputs: &[&wgpu::Buffer; MULTI4_CHANNELS],
1153    channel_count: u32,
1154    controls: &mut LegacyGatherControls,
1155) -> SpatialResult<()> {
1156    let device = runtime.device();
1157    let pipelines = runtime.pipelines();
1158    let multi4_pipeline = pipelines.voxel_gather.multi4_pipeline.as_ref().ok_or_else(|| {
1159        SpatialError::InvalidArgument(
1160            "4-channel gather pipeline is unavailable on this gpu adapter".to_owned(),
1161        )
1162    })?;
1163    let multi4_layout =
1164        pipelines.voxel_gather.multi4_bind_group_layout.as_ref().expect("multi4 layout");
1165    let cell_count = segments.cell_count();
1166    let uniform =
1167        GatherUniform { cell_count, point_count: segments.point_count(), channel_count, _pad: 0 };
1168    let uniform_buffer = controls.get(device, uniform, "voxel-gather-multi4-uniform");
1169    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1170        label: Some("voxel-gather-multi4-bind-group"),
1171        layout: multi4_layout,
1172        entries: &[
1173            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1174            wgpu::BindGroupEntry {
1175                binding: 1,
1176                resource: segments.point_indices_buffer().as_entire_binding(),
1177            },
1178            wgpu::BindGroupEntry {
1179                binding: 2,
1180                resource: segments.cell_starts_buffer().as_entire_binding(),
1181            },
1182            wgpu::BindGroupEntry { binding: 3, resource: values[0].as_entire_binding() },
1183            wgpu::BindGroupEntry { binding: 4, resource: values[1].as_entire_binding() },
1184            wgpu::BindGroupEntry { binding: 5, resource: values[2].as_entire_binding() },
1185            wgpu::BindGroupEntry { binding: 6, resource: values[3].as_entire_binding() },
1186            wgpu::BindGroupEntry { binding: 7, resource: outputs[0].as_entire_binding() },
1187            wgpu::BindGroupEntry { binding: 8, resource: outputs[1].as_entire_binding() },
1188            wgpu::BindGroupEntry { binding: 9, resource: outputs[2].as_entire_binding() },
1189            wgpu::BindGroupEntry { binding: 10, resource: outputs[3].as_entire_binding() },
1190        ],
1191    });
1192    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1193        label: Some("voxel-gather-multi4-pass"),
1194        timestamp_writes: None,
1195    });
1196    pass.set_pipeline(multi4_pipeline);
1197    pass.set_bind_group(0, &bind_group, &[]);
1198    pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1199    Ok(())
1200}
1201
1202pub(crate) fn record_voxel_gather_f32_pass(
1203    encoder: &mut wgpu::CommandEncoder,
1204    runtime: &WgpuRuntime,
1205    values: &wgpu::Buffer,
1206    point_indices: &wgpu::Buffer,
1207    cell_starts: &wgpu::Buffer,
1208    cell_count: u32,
1209    point_count: u32,
1210    output_buffer: &wgpu::Buffer,
1211) -> SpatialResult<()> {
1212    if cell_count == 0 {
1213        return Ok(());
1214    }
1215
1216    let device = runtime.device();
1217    let pipelines = runtime.pipelines();
1218    let uniform = GatherUniform { cell_count, point_count, channel_count: 1, _pad: 0 };
1219    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
1220        label: Some("voxel-gather-uniform"),
1221        contents: bytemuck::bytes_of(&uniform),
1222        usage: wgpu::BufferUsages::UNIFORM,
1223    });
1224
1225    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1226        label: Some("voxel-gather-bind-group"),
1227        layout: &pipelines.voxel_gather.bind_group_layout,
1228        entries: &[
1229            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1230            wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
1231            wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
1232            wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
1233            wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
1234        ],
1235    });
1236
1237    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1238        label: Some("voxel-gather-pass"),
1239        timestamp_writes: None,
1240    });
1241    pass.set_pipeline(&pipelines.voxel_gather.pipeline);
1242    pass.set_bind_group(0, &bind_group, &[]);
1243    pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1244    Ok(())
1245}
1246
1247pub(crate) fn record_voxel_gather_u8_packed_pass(
1248    encoder: &mut wgpu::CommandEncoder,
1249    runtime: &WgpuRuntime,
1250    values: &wgpu::Buffer,
1251    point_indices: &wgpu::Buffer,
1252    cell_starts: &wgpu::Buffer,
1253    cell_count: u32,
1254    point_count: u32,
1255    output_buffer: &wgpu::Buffer,
1256) -> SpatialResult<()> {
1257    if cell_count == 0 {
1258        return Ok(());
1259    }
1260
1261    let device = runtime.device();
1262    let pipelines = runtime.pipelines();
1263    let uniform = GatherUniform { cell_count, point_count, channel_count: 1, _pad: 0 };
1264    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
1265        label: Some("voxel-gather-u8-uniform"),
1266        contents: bytemuck::bytes_of(&uniform),
1267        usage: wgpu::BufferUsages::UNIFORM,
1268    });
1269
1270    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1271        label: Some("voxel-gather-u8-bind-group"),
1272        layout: &pipelines.voxel_gather.u8_bind_group_layout,
1273        entries: &[
1274            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1275            wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
1276            wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
1277            wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
1278            wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
1279        ],
1280    });
1281
1282    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1283        label: Some("voxel-gather-u8-pass"),
1284        timestamp_writes: None,
1285    });
1286    pass.set_pipeline(pipelines.voxel_gather.packed_u8_pipeline(device));
1287    pass.set_bind_group(0, &bind_group, &[]);
1288    pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1289    Ok(())
1290}
1291
1292#[cfg(all(test, not(target_arch = "wasm32")))]
1293pub(crate) fn record_voxel_gather_u8_pass(
1294    encoder: &mut wgpu::CommandEncoder,
1295    runtime: &WgpuRuntime,
1296    values: &wgpu::Buffer,
1297    point_indices: &wgpu::Buffer,
1298    cell_starts: &wgpu::Buffer,
1299    cell_count: u32,
1300    point_count: u32,
1301    output_buffer: &wgpu::Buffer,
1302) -> SpatialResult<()> {
1303    if cell_count == 0 {
1304        return Ok(());
1305    }
1306
1307    let device = runtime.device();
1308    let pipelines = runtime.pipelines();
1309    let uniform = GatherUniform { cell_count, point_count, channel_count: 1, _pad: 0 };
1310    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
1311        label: Some("voxel-gather-u8-uniform"),
1312        contents: bytemuck::bytes_of(&uniform),
1313        usage: wgpu::BufferUsages::UNIFORM,
1314    });
1315
1316    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1317        label: Some("voxel-gather-u8-bind-group"),
1318        layout: &pipelines.voxel_gather.u8_bind_group_layout,
1319        entries: &[
1320            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1321            wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
1322            wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
1323            wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
1324            wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
1325        ],
1326    });
1327
1328    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1329        label: Some("voxel-gather-u8-pass"),
1330        timestamp_writes: None,
1331    });
1332    pass.set_pipeline(&pipelines.voxel_gather.u8_pipeline);
1333    pass.set_bind_group(0, &bind_group, &[]);
1334    pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1335    Ok(())
1336}
1337
1338#[allow(clippy::too_many_arguments)]
1339pub(crate) fn record_voxel_gather_xyz_pass(
1340    encoder: &mut wgpu::CommandEncoder,
1341    runtime: &WgpuRuntime,
1342    x: &wgpu::Buffer,
1343    y: &wgpu::Buffer,
1344    z: &wgpu::Buffer,
1345    segments: &GpuVoxelSegments,
1346    output_x: &wgpu::Buffer,
1347    output_y: &wgpu::Buffer,
1348    output_z: &wgpu::Buffer,
1349) -> SpatialResult<()> {
1350    if segments.cell_count() == 0 {
1351        return Ok(());
1352    }
1353
1354    let device = runtime.device();
1355    let pipelines = runtime.pipelines();
1356    let cell_count = segments.cell_count();
1357    let point_count = segments.point_count();
1358    let uniform = GatherUniform { cell_count, point_count, channel_count: 0, _pad: 0 };
1359    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
1360        label: Some("voxel-gather-xyz-uniform"),
1361        contents: bytemuck::bytes_of(&uniform),
1362        usage: wgpu::BufferUsages::UNIFORM,
1363    });
1364
1365    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1366        label: Some("voxel-gather-xyz-bind-group"),
1367        layout: &pipelines.voxel_gather.xyz_bind_group_layout,
1368        entries: &[
1369            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1370            wgpu::BindGroupEntry {
1371                binding: 1,
1372                resource: segments.point_indices_buffer().as_entire_binding(),
1373            },
1374            wgpu::BindGroupEntry {
1375                binding: 2,
1376                resource: segments.cell_starts_buffer().as_entire_binding(),
1377            },
1378            wgpu::BindGroupEntry { binding: 3, resource: x.as_entire_binding() },
1379            wgpu::BindGroupEntry { binding: 4, resource: y.as_entire_binding() },
1380            wgpu::BindGroupEntry { binding: 5, resource: z.as_entire_binding() },
1381            wgpu::BindGroupEntry { binding: 6, resource: output_x.as_entire_binding() },
1382            wgpu::BindGroupEntry { binding: 7, resource: output_y.as_entire_binding() },
1383            wgpu::BindGroupEntry { binding: 8, resource: output_z.as_entire_binding() },
1384        ],
1385    });
1386
1387    let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1388        label: Some("voxel-gather-xyz-pass"),
1389        timestamp_writes: None,
1390    });
1391    pass.set_pipeline(&pipelines.voxel_gather.xyz_pipeline);
1392    pass.set_bind_group(0, &bind_group, &[]);
1393    pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1394    Ok(())
1395}
1396
1397fn dispatch_voxel_gather_f32(
1398    runtime: &WgpuRuntime,
1399    values: &wgpu::Buffer,
1400    point_indices: &wgpu::Buffer,
1401    cell_starts: &wgpu::Buffer,
1402    cell_count: u32,
1403    point_count: u32,
1404) -> SpatialResult<Vec<f32>> {
1405    if cell_count == 0 {
1406        return Ok(Vec::new());
1407    }
1408
1409    let device = runtime.device();
1410    let queue = runtime.queue();
1411    let pipelines = runtime.pipelines();
1412
1413    let uniform = GatherUniform { cell_count, point_count, channel_count: 1, _pad: 0 };
1414
1415    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
1416        label: Some("voxel-gather-uniform"),
1417        contents: bytemuck::bytes_of(&uniform),
1418        usage: wgpu::BufferUsages::UNIFORM,
1419    });
1420
1421    let output_len = cell_count as usize * std::mem::size_of::<f32>();
1422    let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
1423        label: Some("voxel-gather-output"),
1424        size: output_len as u64,
1425        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
1426        mapped_at_creation: false,
1427    });
1428    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
1429        label: Some("voxel-gather-staging"),
1430        size: output_len as u64,
1431        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
1432        mapped_at_creation: false,
1433    });
1434
1435    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1436        label: Some("voxel-gather-bind-group"),
1437        layout: &pipelines.voxel_gather.bind_group_layout,
1438        entries: &[
1439            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1440            wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
1441            wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
1442            wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
1443            wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
1444        ],
1445    });
1446
1447    let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
1448        label: Some("voxel-gather-encoder"),
1449    });
1450
1451    {
1452        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1453            label: Some("voxel-gather-pass"),
1454            timestamp_writes: None,
1455        });
1456        pass.set_pipeline(&pipelines.voxel_gather.pipeline);
1457        pass.set_bind_group(0, &bind_group, &[]);
1458        pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1459    }
1460
1461    encoder.copy_buffer_to_buffer(&output_buffer, 0, &staging_buffer, 0, output_len as u64);
1462    queue.submit(Some(encoder.finish()));
1463
1464    read_staging_f32(device, &staging_buffer, cell_count as usize)
1465}
1466
1467fn dispatch_voxel_gather_multi_gpu_buffers(
1468    runtime: &WgpuRuntime,
1469    values: &[&wgpu::Buffer; MULTI4_CHANNELS],
1470    segments: &GpuVoxelSegments,
1471    channel_count: u32,
1472) -> SpatialResult<Vec<Vec<f32>>> {
1473    let pipelines = runtime.pipelines();
1474    if channel_count > 2 && pipelines.voxel_gather.multi4_pipeline.is_some() {
1475        dispatch_voxel_gather_multi4_gpu_buffers(runtime, values, segments, channel_count)
1476    } else {
1477        if channel_count > MULTI2_CHANNELS as u32 {
1478            return Err(SpatialError::InvalidArgument(format!(
1479                "gpu adapter supports only {} channels per gather dispatch",
1480                pipelines.voxel_gather.multi_max_channels
1481            )));
1482        }
1483        dispatch_voxel_gather_multi2_gpu_buffers(runtime, values, segments, channel_count)
1484    }
1485}
1486
1487fn dispatch_voxel_gather_multi2_gpu_buffers(
1488    runtime: &WgpuRuntime,
1489    values: &[&wgpu::Buffer; MULTI4_CHANNELS],
1490    segments: &GpuVoxelSegments,
1491    channel_count: u32,
1492) -> SpatialResult<Vec<Vec<f32>>> {
1493    let device = runtime.device();
1494    let queue = runtime.queue();
1495    let pipelines = runtime.pipelines();
1496    let cell_count = segments.cell_count();
1497    let point_count = segments.point_count();
1498    let channels = channel_count as usize;
1499
1500    let uniform = GatherUniform { cell_count, point_count, channel_count, _pad: 0 };
1501    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
1502        label: Some("voxel-gather-multi-uniform"),
1503        contents: bytemuck::bytes_of(&uniform),
1504        usage: wgpu::BufferUsages::UNIFORM,
1505    });
1506
1507    let channel_len = cell_count as usize * std::mem::size_of::<f32>();
1508    let mut outputs = [None, None];
1509    for output in outputs.iter_mut() {
1510        *output = Some(device.create_buffer(&wgpu::BufferDescriptor {
1511            label: Some("voxel-gather-multi-output"),
1512            size: channel_len as u64,
1513            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
1514            mapped_at_creation: false,
1515        }));
1516    }
1517    let output_refs: [&wgpu::Buffer; MULTI2_CHANNELS] =
1518        std::array::from_fn(|index| outputs[index].as_ref().expect("output buffer"));
1519
1520    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
1521        label: Some("voxel-gather-multi-staging"),
1522        size: (channel_len * channels) as u64,
1523        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
1524        mapped_at_creation: false,
1525    });
1526
1527    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1528        label: Some("voxel-gather-multi-bind-group"),
1529        layout: &pipelines.voxel_gather.multi_bind_group_layout,
1530        entries: &[
1531            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1532            wgpu::BindGroupEntry {
1533                binding: 1,
1534                resource: segments.point_indices_buffer().as_entire_binding(),
1535            },
1536            wgpu::BindGroupEntry {
1537                binding: 2,
1538                resource: segments.cell_starts_buffer().as_entire_binding(),
1539            },
1540            wgpu::BindGroupEntry { binding: 3, resource: values[0].as_entire_binding() },
1541            wgpu::BindGroupEntry { binding: 4, resource: values[1].as_entire_binding() },
1542            wgpu::BindGroupEntry { binding: 5, resource: output_refs[0].as_entire_binding() },
1543            wgpu::BindGroupEntry { binding: 6, resource: output_refs[1].as_entire_binding() },
1544        ],
1545    });
1546
1547    let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
1548        label: Some("voxel-gather-multi-encoder"),
1549    });
1550    {
1551        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1552            label: Some("voxel-gather-multi-pass"),
1553            timestamp_writes: None,
1554        });
1555        pass.set_pipeline(&pipelines.voxel_gather.multi_pipeline);
1556        pass.set_bind_group(0, &bind_group, &[]);
1557        pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1558    }
1559
1560    for (index, output) in output_refs.iter().take(channels).enumerate() {
1561        let offset = (channel_len * index) as u64;
1562        encoder.copy_buffer_to_buffer(output, 0, &staging_buffer, offset, channel_len as u64);
1563    }
1564    queue.submit(Some(encoder.finish()));
1565
1566    read_staging_f32_channels(device, &staging_buffer, channels, cell_count as usize)
1567}
1568
1569fn dispatch_voxel_gather_multi4_gpu_buffers(
1570    runtime: &WgpuRuntime,
1571    values: &[&wgpu::Buffer; MULTI4_CHANNELS],
1572    segments: &GpuVoxelSegments,
1573    channel_count: u32,
1574) -> SpatialResult<Vec<Vec<f32>>> {
1575    let device = runtime.device();
1576    let queue = runtime.queue();
1577    let pipelines = runtime.pipelines();
1578    let multi4_pipeline = pipelines.voxel_gather.multi4_pipeline.as_ref().ok_or_else(|| {
1579        SpatialError::InvalidArgument(
1580            "4-channel gather pipeline is unavailable on this gpu adapter".to_owned(),
1581        )
1582    })?;
1583    let multi4_layout =
1584        pipelines.voxel_gather.multi4_bind_group_layout.as_ref().expect("multi4 layout");
1585
1586    let cell_count = segments.cell_count();
1587    let point_count = segments.point_count();
1588    let channels = channel_count as usize;
1589
1590    let uniform = GatherUniform { cell_count, point_count, channel_count, _pad: 0 };
1591    let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
1592        label: Some("voxel-gather-multi4-uniform"),
1593        contents: bytemuck::bytes_of(&uniform),
1594        usage: wgpu::BufferUsages::UNIFORM,
1595    });
1596
1597    let channel_len = cell_count as usize * std::mem::size_of::<f32>();
1598    let mut outputs = [None, None, None, None];
1599    for output in outputs.iter_mut() {
1600        *output = Some(device.create_buffer(&wgpu::BufferDescriptor {
1601            label: Some("voxel-gather-multi4-output"),
1602            size: channel_len as u64,
1603            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
1604            mapped_at_creation: false,
1605        }));
1606    }
1607    let output_refs: [&wgpu::Buffer; MULTI4_CHANNELS] =
1608        std::array::from_fn(|index| outputs[index].as_ref().expect("output buffer"));
1609
1610    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
1611        label: Some("voxel-gather-multi4-staging"),
1612        size: (channel_len * channels) as u64,
1613        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
1614        mapped_at_creation: false,
1615    });
1616
1617    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
1618        label: Some("voxel-gather-multi4-bind-group"),
1619        layout: multi4_layout,
1620        entries: &[
1621            wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
1622            wgpu::BindGroupEntry {
1623                binding: 1,
1624                resource: segments.point_indices_buffer().as_entire_binding(),
1625            },
1626            wgpu::BindGroupEntry {
1627                binding: 2,
1628                resource: segments.cell_starts_buffer().as_entire_binding(),
1629            },
1630            wgpu::BindGroupEntry { binding: 3, resource: values[0].as_entire_binding() },
1631            wgpu::BindGroupEntry { binding: 4, resource: values[1].as_entire_binding() },
1632            wgpu::BindGroupEntry { binding: 5, resource: values[2].as_entire_binding() },
1633            wgpu::BindGroupEntry { binding: 6, resource: values[3].as_entire_binding() },
1634            wgpu::BindGroupEntry { binding: 7, resource: output_refs[0].as_entire_binding() },
1635            wgpu::BindGroupEntry { binding: 8, resource: output_refs[1].as_entire_binding() },
1636            wgpu::BindGroupEntry { binding: 9, resource: output_refs[2].as_entire_binding() },
1637            wgpu::BindGroupEntry { binding: 10, resource: output_refs[3].as_entire_binding() },
1638        ],
1639    });
1640
1641    let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
1642        label: Some("voxel-gather-multi4-encoder"),
1643    });
1644    {
1645        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
1646            label: Some("voxel-gather-multi4-pass"),
1647            timestamp_writes: None,
1648        });
1649        pass.set_pipeline(multi4_pipeline);
1650        pass.set_bind_group(0, &bind_group, &[]);
1651        pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
1652    }
1653
1654    for (index, output) in output_refs.iter().take(channels).enumerate() {
1655        let offset = (channel_len * index) as u64;
1656        encoder.copy_buffer_to_buffer(output, 0, &staging_buffer, offset, channel_len as u64);
1657    }
1658    queue.submit(Some(encoder.finish()));
1659
1660    read_staging_f32_channels(device, &staging_buffer, channels, cell_count as usize)
1661}
1662
1663fn empty_storage_buffer(device: &wgpu::Device) -> SpatialResult<wgpu::Buffer> {
1664    Ok(device.create_buffer(&wgpu::BufferDescriptor {
1665        label: Some("voxel-gather-empty"),
1666        size: 4,
1667        usage: wgpu::BufferUsages::STORAGE,
1668        mapped_at_creation: false,
1669    }))
1670}
1671
1672#[cfg(test)]
1673mod tests {
1674    use super::{gather_voxel_first_f32, gather_voxel_first_f32_multi_gpu};
1675    use crate::kernels::gpu_segments::GpuVoxelSegments;
1676    use crate::kernels::voxel_segments::build_voxel_segments;
1677    use crate::runtime::WgpuRuntime;
1678
1679    fn gpu_segments_from_keys(runtime: &WgpuRuntime, keys: &[(i64, i64, i64)]) -> GpuVoxelSegments {
1680        use crate::kernels::voxel_sort::build_voxel_segments_gpu_from_keys;
1681        build_voxel_segments_gpu_from_keys(runtime, keys).expect("gpu segments")
1682    }
1683
1684    #[test]
1685    fn gpu_first_gather_matches_cpu_reference() {
1686        let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
1687        let values = [0.2_f32, 0.9, 10.0, 20.0];
1688        let keys = vec![(0, 0, 0), (0, 0, 0), (2, 0, 0), (2, 0, 0)];
1689        let segments = build_voxel_segments(&keys);
1690
1691        let gpu = gather_voxel_first_f32(&runtime, &values, &segments).expect("gpu gather");
1692        assert!((gpu[0] - 0.2).abs() < 1e-5);
1693        assert!((gpu[1] - 10.0).abs() < 1e-5);
1694    }
1695
1696    #[test]
1697    fn gpu_multi_gather_matches_single_channel_reference() {
1698        let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
1699        let intensity = [0.2_f32, 0.9, 10.0, 20.0];
1700        let classification = [1.0_f32, 2.0, 3.0, 4.0];
1701        let keys = vec![(0, 0, 0), (0, 0, 0), (2, 0, 0), (2, 0, 0)];
1702        let segments = gpu_segments_from_keys(&runtime, &keys);
1703
1704        let multi =
1705            gather_voxel_first_f32_multi_gpu(&runtime, &[&intensity, &classification], &segments)
1706                .expect("multi gather");
1707
1708        assert!((multi[0][0] - 0.2).abs() < 1e-5);
1709        assert!((multi[0][1] - 10.0).abs() < 1e-5);
1710        assert!((multi[1][0] - 1.0).abs() < 1e-5);
1711        assert!((multi[1][1] - 3.0).abs() < 1e-5);
1712    }
1713
1714    #[test]
1715    fn gpu_multi4_gather_handles_four_channels_when_supported() {
1716        let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
1717        if runtime.max_gather_channels() < 4 {
1718            return;
1719        }
1720
1721        let c0 = [0.2_f32, 0.9, 10.0, 20.0];
1722        let c1 = [1.0_f32, 2.0, 3.0, 4.0];
1723        let c2 = [5.0_f32, 6.0, 7.0, 8.0];
1724        let c3 = [9.0_f32, 10.0, 11.0, 12.0];
1725        let keys = vec![(0, 0, 0), (0, 0, 0), (2, 0, 0), (2, 0, 0)];
1726        let segments = gpu_segments_from_keys(&runtime, &keys);
1727
1728        let multi = gather_voxel_first_f32_multi_gpu(&runtime, &[&c0, &c1, &c2, &c3], &segments)
1729            .expect("multi4 gather");
1730
1731        assert_eq!(multi.len(), 4);
1732        assert!((multi[0][0] - 0.2).abs() < 1e-5);
1733        assert!((multi[0][1] - 10.0).abs() < 1e-5);
1734        assert!((multi[1][0] - 1.0).abs() < 1e-5);
1735        assert!((multi[1][1] - 3.0).abs() < 1e-5);
1736        assert!((multi[2][0] - 5.0).abs() < 1e-5);
1737        assert!((multi[2][1] - 7.0).abs() < 1e-5);
1738        assert!((multi[3][0] - 9.0).abs() < 1e-5);
1739        assert!((multi[3][1] - 11.0).abs() < 1e-5);
1740    }
1741}
1742
1743#[cfg(all(test, not(target_arch = "wasm32")))]
1744#[path = "voxel_gather_dummy_tests.rs"]
1745mod dummy_tests;
1746
1747#[cfg(all(test, not(target_arch = "wasm32")))]
1748#[path = "voxel_combined_reuse_tests.rs"]
1749mod combined_reuse_tests;
1750
1751#[cfg(all(test, not(target_arch = "wasm32")))]
1752#[path = "voxel_legacy_control_tests.rs"]
1753mod legacy_control_tests;