1use bytemuck::{Pod, Zeroable};
2use spatialrust_core::{SpatialError, SpatialResult};
3use wgpu::util::DeviceExt;
4
5use crate::kernels::gpu_segments::GpuVoxelSegments;
6use crate::kernels::voxel_centroid_first_fused::record_fused_voxel_centroid_first;
7use crate::kernels::voxel_reduce_fused::{plan_fused_voxel_average, record_fused_voxel_average};
8use crate::kernels::voxel_segments::VoxelSegments;
9use crate::readback::{
10 packed_u8_output_staging_bytes, read_staging_f32, read_staging_f32_channels, read_staging_xyz,
11 read_staging_xyz_and_packed_attributes, u8_storage_upload_contents,
12};
13use crate::runtime::WgpuRuntime;
14
15const WORKGROUP_SIZE: u32 = 256;
16
17#[repr(C)]
18#[derive(Clone, Copy, Debug, Pod, Zeroable)]
19struct ReduceUniform {
20 cell_count: u32,
21 point_count: u32,
22 _pad0: u32,
23 _pad1: u32,
24}
25
26pub fn reduce_voxel_average_f32(
28 runtime: &WgpuRuntime,
29 values: &[f32],
30 segments: &VoxelSegments,
31) -> SpatialResult<Vec<f32>> {
32 if segments.is_empty() {
33 return Ok(Vec::new());
34 }
35 if values.is_empty() {
36 return Err(SpatialError::InvalidArgument("cannot reduce empty value buffer".to_owned()));
37 }
38
39 let device = runtime.device();
40 let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
41 label: Some("voxel-reduce-values"),
42 contents: bytemuck::cast_slice(values),
43 usage: wgpu::BufferUsages::STORAGE,
44 });
45 let indices_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
46 label: Some("voxel-reduce-indices"),
47 contents: bytemuck::cast_slice(&segments.point_indices),
48 usage: wgpu::BufferUsages::STORAGE,
49 });
50 let starts_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
51 label: Some("voxel-reduce-starts"),
52 contents: bytemuck::cast_slice(&segments.cell_starts),
53 usage: wgpu::BufferUsages::STORAGE,
54 });
55
56 dispatch_voxel_reduce_f32(
57 runtime,
58 &values_buffer,
59 &indices_buffer,
60 &starts_buffer,
61 segments.len() as u32,
62 segments.point_indices.len() as u32,
63 )
64}
65
66pub fn reduce_voxel_average_f32_gpu_buffers(
68 runtime: &WgpuRuntime,
69 values: &wgpu::Buffer,
70 segments: &GpuVoxelSegments,
71) -> SpatialResult<Vec<f32>> {
72 dispatch_voxel_reduce_f32(
73 runtime,
74 values,
75 segments.point_indices_buffer(),
76 segments.cell_starts_buffer(),
77 segments.cell_count(),
78 segments.point_count(),
79 )
80}
81
82pub fn reduce_voxel_average_f32_gpu(
84 runtime: &WgpuRuntime,
85 values: &[f32],
86 segments: &GpuVoxelSegments,
87) -> SpatialResult<Vec<f32>> {
88 if segments.cell_count() == 0 {
89 return Ok(Vec::new());
90 }
91 if values.len() != segments.point_count() as usize {
92 return Err(SpatialError::BufferLengthMismatch {
93 expected: segments.point_count() as usize,
94 found: values.len(),
95 });
96 }
97
98 let device = runtime.device();
99 let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
100 label: Some("voxel-reduce-values-upload"),
101 contents: bytemuck::cast_slice(values),
102 usage: wgpu::BufferUsages::STORAGE,
103 });
104 reduce_voxel_average_f32_gpu_buffers(runtime, &values_buffer, segments)
105}
106
107pub fn reduce_voxel_average_f32_multi_gpu(
109 runtime: &WgpuRuntime,
110 channels: &[&[f32]],
111 segments: &GpuVoxelSegments,
112) -> SpatialResult<Vec<Vec<f32>>> {
113 if channels.is_empty() {
114 return Ok(Vec::new());
115 }
116 if segments.cell_count() == 0 {
117 return Ok(vec![Vec::new(); channels.len()]);
118 }
119
120 let point_count = segments.point_count() as usize;
121 for channel in channels {
122 if channel.len() != point_count {
123 return Err(SpatialError::BufferLengthMismatch {
124 expected: point_count,
125 found: channel.len(),
126 });
127 }
128 }
129
130 if let Some(result) =
131 super::voxel_average_batch::try_reduce_voxel_average_batched(runtime, channels, segments)
132 {
133 return result;
134 }
135
136 let device = runtime.device();
137 let queue = runtime.queue();
138 let cell_count = segments.cell_count();
139 let cells = cell_count as usize;
140 let channel_len = cells * std::mem::size_of::<f32>();
141 let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
142 label: Some("voxel-reduce-multi-staging"),
143 size: (channel_len * channels.len()) as u64,
144 usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
145 mapped_at_creation: false,
146 });
147
148 let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
149 label: Some("voxel-reduce-multi-encoder"),
150 });
151
152 for (channel_index, channel) in channels.iter().enumerate() {
153 let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
154 label: Some("voxel-reduce-multi-values"),
155 contents: bytemuck::cast_slice(channel),
156 usage: wgpu::BufferUsages::STORAGE,
157 });
158 let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
159 label: Some("voxel-reduce-multi-output"),
160 size: channel_len as u64,
161 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
162 mapped_at_creation: false,
163 });
164
165 record_voxel_reduce_f32_pass(
166 &mut encoder,
167 runtime,
168 &values_buffer,
169 segments.point_indices_buffer(),
170 segments.cell_starts_buffer(),
171 cell_count,
172 segments.point_count(),
173 &output_buffer,
174 )?;
175
176 encoder.copy_buffer_to_buffer(
177 &output_buffer,
178 0,
179 &staging_buffer,
180 (channel_len * channel_index) as u64,
181 channel_len as u64,
182 );
183 }
184
185 queue.submit(Some(encoder.finish()));
186
187 read_staging_f32_channels(device, &staging_buffer, channels.len(), cells)
188}
189
190pub fn reduce_voxel_centroids_xyz_and_average_multi_gpu(
192 runtime: &WgpuRuntime,
193 x: &wgpu::Buffer,
194 y: &wgpu::Buffer,
195 z: &wgpu::Buffer,
196 attribute_channels: &[&[f32]],
197 u8_attribute_channels: &[&[u8]],
198 segments: &GpuVoxelSegments,
199) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>, Vec<Vec<f32>>, Vec<Vec<u8>>)> {
200 let attribute_count = attribute_channels.len();
201 let u8_attribute_count = u8_attribute_channels.len();
202 if segments.cell_count() == 0 {
203 return Ok((
204 Vec::new(),
205 Vec::new(),
206 Vec::new(),
207 vec![Vec::new(); attribute_count],
208 vec![Vec::new(); u8_attribute_count],
209 ));
210 }
211
212 let point_count = segments.point_count() as usize;
213 for channel in attribute_channels {
214 if channel.len() != point_count {
215 return Err(SpatialError::BufferLengthMismatch {
216 expected: point_count,
217 found: channel.len(),
218 });
219 }
220 }
221 for channel in u8_attribute_channels {
222 if channel.len() != point_count {
223 return Err(SpatialError::BufferLengthMismatch {
224 expected: point_count,
225 found: channel.len(),
226 });
227 }
228 }
229
230 let device = runtime.device();
231 let queue = runtime.queue();
232 let cell_count = segments.cell_count();
233 let cells = cell_count as usize;
234 let channel_len = cells * std::mem::size_of::<f32>();
235 let f32_channel_count = 3 + attribute_count;
236 let u8_staging_len = packed_u8_output_staging_bytes(cells, u8_attribute_count);
237 let u8_channel_len = packed_u8_output_staging_bytes(cells, 1);
238 let fused_plan = plan_fused_voxel_average(&device.limits(), cell_count, attribute_count);
239 let staging_size = channel_len * f32_channel_count + u8_staging_len;
240
241 let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
242 label: Some("voxel-reduce-xyz-attrs-staging"),
243 size: staging_size as u64,
244 usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
245 mapped_at_creation: false,
246 });
247
248 let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
249 label: Some("voxel-reduce-xyz-attrs-encoder"),
250 });
251
252 if let Some(plan) = fused_plan {
253 let output = device.create_buffer(&wgpu::BufferDescriptor {
254 label: Some("voxel-fused-average-output"),
255 size: plan.output_bytes,
256 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
257 mapped_at_creation: false,
258 });
259 let attributes: Vec<_> = attribute_channels
260 .iter()
261 .map(|channel| {
262 device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
263 label: Some("voxel-reduce-xyz-attrs-values"),
264 contents: bytemuck::cast_slice(channel),
265 usage: wgpu::BufferUsages::STORAGE,
266 })
267 })
268 .collect();
269 record_fused_voxel_average(
270 &mut encoder,
271 runtime,
272 [x, y, z],
273 &attributes,
274 segments,
275 &output,
276 );
277 encoder.copy_buffer_to_buffer(&output, 0, &staging_buffer, 0, plan.output_bytes);
278 } else if !super::voxel_wide_average::try_record_wide_voxel_average(
279 &mut encoder,
280 runtime,
281 [x, y, z],
282 attribute_channels,
283 segments,
284 &staging_buffer,
285 super::voxel_xyz_packed::XyzMode::Centroid,
286 )? {
287 if !super::voxel_xyz_packed::try_record_packed_xyz_to_staging(
288 &mut encoder,
289 runtime,
290 [x, y, z],
291 segments,
292 &staging_buffer,
293 0,
294 super::voxel_xyz_packed::XyzMode::Centroid,
295 ) {
296 let output_x = device.create_buffer(&wgpu::BufferDescriptor {
297 label: Some("voxel-reduce-xyz-out-x"),
298 size: channel_len as u64,
299 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
300 mapped_at_creation: false,
301 });
302 let output_y = device.create_buffer(&wgpu::BufferDescriptor {
303 label: Some("voxel-reduce-xyz-out-y"),
304 size: channel_len as u64,
305 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
306 mapped_at_creation: false,
307 });
308 let output_z = device.create_buffer(&wgpu::BufferDescriptor {
309 label: Some("voxel-reduce-xyz-out-z"),
310 size: channel_len as u64,
311 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
312 mapped_at_creation: false,
313 });
314 record_voxel_reduce_xyz_pass(
315 &mut encoder,
316 runtime,
317 x,
318 y,
319 z,
320 segments.point_indices_buffer(),
321 segments.cell_starts_buffer(),
322 cell_count,
323 segments.point_count(),
324 &output_x,
325 &output_y,
326 &output_z,
327 )?;
328
329 encoder.copy_buffer_to_buffer(&output_x, 0, &staging_buffer, 0, channel_len as u64);
330 encoder.copy_buffer_to_buffer(
331 &output_y,
332 0,
333 &staging_buffer,
334 channel_len as u64,
335 channel_len as u64,
336 );
337 encoder.copy_buffer_to_buffer(
338 &output_z,
339 0,
340 &staging_buffer,
341 (channel_len * 2) as u64,
342 channel_len as u64,
343 );
344 }
345
346 if !super::voxel_average_batch::try_record_voxel_attribute_averages_to_staging(
347 &mut encoder,
348 runtime,
349 attribute_channels,
350 segments,
351 &staging_buffer,
352 (channel_len * 3) as u64,
353 )? {
354 for (attribute_index, channel) in attribute_channels.iter().enumerate() {
355 let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
356 label: Some("voxel-reduce-xyz-attrs-values"),
357 contents: bytemuck::cast_slice(channel),
358 usage: wgpu::BufferUsages::STORAGE,
359 });
360 let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
361 label: Some("voxel-reduce-xyz-attrs-output"),
362 size: channel_len as u64,
363 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
364 mapped_at_creation: false,
365 });
366
367 record_voxel_reduce_f32_pass(
368 &mut encoder,
369 runtime,
370 &values_buffer,
371 segments.point_indices_buffer(),
372 segments.cell_starts_buffer(),
373 cell_count,
374 segments.point_count(),
375 &output_buffer,
376 )?;
377
378 encoder.copy_buffer_to_buffer(
379 &output_buffer,
380 0,
381 &staging_buffer,
382 (channel_len * (3 + attribute_index)) as u64,
383 channel_len as u64,
384 );
385 }
386 }
387 }
388
389 let u8_region_offset = (channel_len * f32_channel_count) as u64;
390 if !super::voxel_u8_wide::try_record_wide_voxel_u8_to_staging(
391 &mut encoder,
392 runtime,
393 u8_attribute_channels,
394 segments,
395 &staging_buffer,
396 u8_region_offset,
397 true,
398 )? && !crate::kernels::voxel_u8_batch::try_record_voxel_u8_batch_to_staging(
399 &mut encoder,
400 runtime,
401 u8_attribute_channels,
402 segments,
403 &staging_buffer,
404 u8_region_offset,
405 true,
406 ) {
407 for (attribute_index, channel) in u8_attribute_channels.iter().enumerate() {
408 let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
409 label: Some("voxel-reduce-xyz-u8-values"),
410 contents: &u8_storage_upload_contents(channel),
411 usage: wgpu::BufferUsages::STORAGE,
412 });
413 let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
414 label: Some("voxel-reduce-xyz-u8-output"),
415 size: u8_channel_len as u64,
416 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
417 mapped_at_creation: false,
418 });
419
420 record_voxel_reduce_u8_packed_pass(
421 &mut encoder,
422 runtime,
423 &values_buffer,
424 segments.point_indices_buffer(),
425 segments.cell_starts_buffer(),
426 cell_count,
427 segments.point_count(),
428 &output_buffer,
429 )?;
430
431 encoder.copy_buffer_to_buffer(
432 &output_buffer,
433 0,
434 &staging_buffer,
435 u8_region_offset + attribute_index as u64 * u8_channel_len as u64,
436 u8_channel_len as u64,
437 );
438 }
439 }
440
441 queue.submit(Some(encoder.finish()));
442
443 let output = read_staging_xyz_and_packed_attributes(
444 device,
445 &staging_buffer,
446 cells,
447 attribute_count,
448 u8_attribute_count,
449 )?;
450 Ok(output)
451}
452
453pub fn reduce_voxel_centroids_xyz_and_gather_first_multi_gpu(
455 runtime: &WgpuRuntime,
456 x: &wgpu::Buffer,
457 y: &wgpu::Buffer,
458 z: &wgpu::Buffer,
459 attribute_channels: &[&[f32]],
460 u8_attribute_channels: &[&[u8]],
461 segments: &GpuVoxelSegments,
462) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>, Vec<Vec<f32>>, Vec<Vec<u8>>)> {
463 use crate::kernels::voxel_gather::record_gather_f32_attribute_channels_to_staging;
464 use crate::kernels::voxel_gather::record_voxel_gather_u8_packed_pass;
465
466 let attribute_count = attribute_channels.len();
467 let u8_attribute_count = u8_attribute_channels.len();
468 if segments.cell_count() == 0 {
469 return Ok((
470 Vec::new(),
471 Vec::new(),
472 Vec::new(),
473 vec![Vec::new(); attribute_count],
474 vec![Vec::new(); u8_attribute_count],
475 ));
476 }
477
478 let point_count = segments.point_count() as usize;
479 for channel in attribute_channels {
480 if channel.len() != point_count {
481 return Err(SpatialError::BufferLengthMismatch {
482 expected: point_count,
483 found: channel.len(),
484 });
485 }
486 }
487 for channel in u8_attribute_channels {
488 if channel.len() != point_count {
489 return Err(SpatialError::BufferLengthMismatch {
490 expected: point_count,
491 found: channel.len(),
492 });
493 }
494 }
495
496 let device = runtime.device();
497 let queue = runtime.queue();
498 let cell_count = segments.cell_count();
499 let cells = cell_count as usize;
500 let channel_len = cells * std::mem::size_of::<f32>();
501 let f32_channel_count = 3 + attribute_count;
502 let u8_staging_len = packed_u8_output_staging_bytes(cells, u8_attribute_count);
503 let u8_channel_len = packed_u8_output_staging_bytes(cells, 1);
504 let fused_plan = plan_fused_voxel_average(&device.limits(), cell_count, attribute_count);
505 let staging_size = channel_len * f32_channel_count + u8_staging_len;
506 let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
507 label: Some("voxel-reduce-xyz-gather-attrs-staging"),
508 size: staging_size as u64,
509 usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
510 mapped_at_creation: false,
511 });
512 let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
513 label: Some("voxel-reduce-xyz-gather-attrs-encoder"),
514 });
515 let mut upload_recycle = Vec::new();
516 if let Some(plan) = fused_plan {
517 let output = device.create_buffer(&wgpu::BufferDescriptor {
518 label: Some("voxel-fused-centroid-first-output"),
519 size: plan.output_bytes,
520 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
521 mapped_at_creation: false,
522 });
523 for channel in attribute_channels {
524 upload_recycle
525 .push(runtime.upload_f32_storage("voxel-gather-xyz-attrs-values", channel)?);
526 }
527 record_fused_voxel_centroid_first(
528 &mut encoder,
529 runtime,
530 [x, y, z],
531 &upload_recycle,
532 segments,
533 &output,
534 );
535 encoder.copy_buffer_to_buffer(&output, 0, &staging_buffer, 0, plan.output_bytes);
536 } else if !super::voxel_wide_first::try_record_wide_voxel_first(
537 &mut encoder,
538 runtime,
539 [x, y, z],
540 attribute_channels,
541 segments,
542 &staging_buffer,
543 super::voxel_xyz_packed::XyzMode::Centroid,
544 &mut upload_recycle,
545 )? {
546 if !super::voxel_xyz_packed::try_record_packed_xyz_to_staging(
547 &mut encoder,
548 runtime,
549 [x, y, z],
550 segments,
551 &staging_buffer,
552 0,
553 super::voxel_xyz_packed::XyzMode::Centroid,
554 ) {
555 let output_x = device.create_buffer(&wgpu::BufferDescriptor {
556 label: Some("voxel-reduce-xyz-out-x"),
557 size: channel_len as u64,
558 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
559 mapped_at_creation: false,
560 });
561 let output_y = device.create_buffer(&wgpu::BufferDescriptor {
562 label: Some("voxel-reduce-xyz-out-y"),
563 size: channel_len as u64,
564 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
565 mapped_at_creation: false,
566 });
567 let output_z = device.create_buffer(&wgpu::BufferDescriptor {
568 label: Some("voxel-reduce-xyz-out-z"),
569 size: channel_len as u64,
570 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
571 mapped_at_creation: false,
572 });
573
574 record_voxel_reduce_xyz_pass(
575 &mut encoder,
576 runtime,
577 x,
578 y,
579 z,
580 segments.point_indices_buffer(),
581 segments.cell_starts_buffer(),
582 cell_count,
583 segments.point_count(),
584 &output_x,
585 &output_y,
586 &output_z,
587 )?;
588 encoder.copy_buffer_to_buffer(&output_x, 0, &staging_buffer, 0, channel_len as u64);
589 encoder.copy_buffer_to_buffer(
590 &output_y,
591 0,
592 &staging_buffer,
593 channel_len as u64,
594 channel_len as u64,
595 );
596 encoder.copy_buffer_to_buffer(
597 &output_z,
598 0,
599 &staging_buffer,
600 (channel_len * 2) as u64,
601 channel_len as u64,
602 );
603 }
604
605 record_gather_f32_attribute_channels_to_staging(
606 &mut encoder,
607 runtime,
608 attribute_channels,
609 segments,
610 &staging_buffer,
611 channel_len as u64,
612 &mut upload_recycle,
613 )?;
614 }
615
616 let u8_region_offset = (channel_len * f32_channel_count) as u64;
617 if !super::voxel_u8_wide::try_record_wide_voxel_u8_to_staging(
618 &mut encoder,
619 runtime,
620 u8_attribute_channels,
621 segments,
622 &staging_buffer,
623 u8_region_offset,
624 false,
625 )? && !crate::kernels::voxel_u8_batch::try_record_voxel_u8_batch_to_staging(
626 &mut encoder,
627 runtime,
628 u8_attribute_channels,
629 segments,
630 &staging_buffer,
631 u8_region_offset,
632 false,
633 ) {
634 for (attribute_index, channel) in u8_attribute_channels.iter().enumerate() {
635 let values_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
636 label: Some("voxel-gather-xyz-u8-values"),
637 contents: &u8_storage_upload_contents(channel),
638 usage: wgpu::BufferUsages::STORAGE,
639 });
640 let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
641 label: Some("voxel-gather-xyz-u8-output"),
642 size: u8_channel_len as u64,
643 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
644 mapped_at_creation: false,
645 });
646 record_voxel_gather_u8_packed_pass(
647 &mut encoder,
648 runtime,
649 &values_buffer,
650 segments.point_indices_buffer(),
651 segments.cell_starts_buffer(),
652 cell_count,
653 segments.point_count(),
654 &output_buffer,
655 )?;
656 encoder.copy_buffer_to_buffer(
657 &output_buffer,
658 0,
659 &staging_buffer,
660 u8_region_offset + attribute_index as u64 * u8_channel_len as u64,
661 u8_channel_len as u64,
662 );
663 }
664 }
665
666 queue.submit(Some(encoder.finish()));
667 let output = read_staging_xyz_and_packed_attributes(
668 device,
669 &staging_buffer,
670 cells,
671 attribute_count,
672 u8_attribute_count,
673 )?;
674 for buffer in upload_recycle {
675 runtime.recycle_storage(buffer.size(), buffer);
676 }
677 Ok(output)
678}
679
680pub(crate) fn record_voxel_reduce_f32_pass(
681 encoder: &mut wgpu::CommandEncoder,
682 runtime: &WgpuRuntime,
683 values: &wgpu::Buffer,
684 point_indices: &wgpu::Buffer,
685 cell_starts: &wgpu::Buffer,
686 cell_count: u32,
687 point_count: u32,
688 output_buffer: &wgpu::Buffer,
689) -> SpatialResult<()> {
690 if cell_count == 0 {
691 return Ok(());
692 }
693
694 let device = runtime.device();
695 let pipelines = runtime.pipelines();
696 let uniform = ReduceUniform { cell_count, point_count, _pad0: 0, _pad1: 0 };
697 let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
698 label: Some("voxel-reduce-uniform"),
699 contents: bytemuck::bytes_of(&uniform),
700 usage: wgpu::BufferUsages::UNIFORM,
701 });
702
703 let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
704 label: Some("voxel-reduce-bind-group"),
705 layout: &pipelines.voxel_reduce.bind_group_layout,
706 entries: &[
707 wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
708 wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
709 wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
710 wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
711 wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
712 ],
713 });
714
715 let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
716 label: Some("voxel-reduce-pass"),
717 timestamp_writes: None,
718 });
719 pass.set_pipeline(&pipelines.voxel_reduce.pipeline);
720 pass.set_bind_group(0, &bind_group, &[]);
721 pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
722 Ok(())
723}
724
725pub(crate) fn record_voxel_reduce_u8_packed_pass(
726 encoder: &mut wgpu::CommandEncoder,
727 runtime: &WgpuRuntime,
728 values: &wgpu::Buffer,
729 point_indices: &wgpu::Buffer,
730 cell_starts: &wgpu::Buffer,
731 cell_count: u32,
732 point_count: u32,
733 output_buffer: &wgpu::Buffer,
734) -> SpatialResult<()> {
735 if cell_count == 0 {
736 return Ok(());
737 }
738
739 let device = runtime.device();
740 let pipelines = runtime.pipelines();
741 let uniform = ReduceUniform { cell_count, point_count, _pad0: 0, _pad1: 0 };
742 let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
743 label: Some("voxel-reduce-u8-uniform"),
744 contents: bytemuck::bytes_of(&uniform),
745 usage: wgpu::BufferUsages::UNIFORM,
746 });
747
748 let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
749 label: Some("voxel-reduce-u8-bind-group"),
750 layout: &pipelines.voxel_reduce.u8_bind_group_layout,
751 entries: &[
752 wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
753 wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
754 wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
755 wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
756 wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
757 ],
758 });
759
760 let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
761 label: Some("voxel-reduce-u8-pass"),
762 timestamp_writes: None,
763 });
764 pass.set_pipeline(pipelines.voxel_reduce.packed_u8_pipeline(device));
765 pass.set_bind_group(0, &bind_group, &[]);
766 pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
767 Ok(())
768}
769
770#[cfg(all(test, not(target_arch = "wasm32")))]
771pub(crate) fn record_voxel_reduce_u8_pass(
772 encoder: &mut wgpu::CommandEncoder,
773 runtime: &WgpuRuntime,
774 values: &wgpu::Buffer,
775 point_indices: &wgpu::Buffer,
776 cell_starts: &wgpu::Buffer,
777 cell_count: u32,
778 point_count: u32,
779 output_buffer: &wgpu::Buffer,
780) -> SpatialResult<()> {
781 if cell_count == 0 {
782 return Ok(());
783 }
784
785 let device = runtime.device();
786 let pipelines = runtime.pipelines();
787 let uniform = ReduceUniform { cell_count, point_count, _pad0: 0, _pad1: 0 };
788 let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
789 label: Some("voxel-reduce-u8-uniform"),
790 contents: bytemuck::bytes_of(&uniform),
791 usage: wgpu::BufferUsages::UNIFORM,
792 });
793
794 let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
795 label: Some("voxel-reduce-u8-bind-group"),
796 layout: &pipelines.voxel_reduce.u8_bind_group_layout,
797 entries: &[
798 wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
799 wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
800 wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
801 wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
802 wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
803 ],
804 });
805
806 let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
807 label: Some("voxel-reduce-u8-pass"),
808 timestamp_writes: None,
809 });
810 pass.set_pipeline(&pipelines.voxel_reduce.u8_pipeline);
811 pass.set_bind_group(0, &bind_group, &[]);
812 pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
813 Ok(())
814}
815
816#[allow(clippy::too_many_arguments)]
817pub(crate) fn record_voxel_reduce_xyz_pass(
818 encoder: &mut wgpu::CommandEncoder,
819 runtime: &WgpuRuntime,
820 values_x: &wgpu::Buffer,
821 values_y: &wgpu::Buffer,
822 values_z: &wgpu::Buffer,
823 point_indices: &wgpu::Buffer,
824 cell_starts: &wgpu::Buffer,
825 cell_count: u32,
826 point_count: u32,
827 output_x: &wgpu::Buffer,
828 output_y: &wgpu::Buffer,
829 output_z: &wgpu::Buffer,
830) -> SpatialResult<()> {
831 if cell_count == 0 {
832 return Ok(());
833 }
834
835 let device = runtime.device();
836 let pipelines = runtime.pipelines();
837 let uniform = ReduceUniform { cell_count, point_count, _pad0: 0, _pad1: 0 };
838 let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
839 label: Some("voxel-reduce-xyz-uniform"),
840 contents: bytemuck::bytes_of(&uniform),
841 usage: wgpu::BufferUsages::UNIFORM,
842 });
843
844 let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
845 label: Some("voxel-reduce-xyz-bind-group"),
846 layout: &pipelines.voxel_reduce.xyz_bind_group_layout,
847 entries: &[
848 wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
849 wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
850 wgpu::BindGroupEntry { binding: 2, resource: cell_starts.as_entire_binding() },
851 wgpu::BindGroupEntry { binding: 3, resource: values_x.as_entire_binding() },
852 wgpu::BindGroupEntry { binding: 4, resource: values_y.as_entire_binding() },
853 wgpu::BindGroupEntry { binding: 5, resource: values_z.as_entire_binding() },
854 wgpu::BindGroupEntry { binding: 6, resource: output_x.as_entire_binding() },
855 wgpu::BindGroupEntry { binding: 7, resource: output_y.as_entire_binding() },
856 wgpu::BindGroupEntry { binding: 8, resource: output_z.as_entire_binding() },
857 ],
858 });
859
860 let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
861 label: Some("voxel-reduce-xyz-pass"),
862 timestamp_writes: None,
863 });
864 pass.set_pipeline(&pipelines.voxel_reduce.xyz_pipeline);
865 pass.set_bind_group(0, &bind_group, &[]);
866 pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
867 Ok(())
868}
869
870fn dispatch_voxel_reduce_f32(
871 runtime: &WgpuRuntime,
872 values: &wgpu::Buffer,
873 point_indices: &wgpu::Buffer,
874 cell_starts: &wgpu::Buffer,
875 cell_count: u32,
876 point_count: u32,
877) -> SpatialResult<Vec<f32>> {
878 if cell_count == 0 {
879 return Ok(Vec::new());
880 }
881
882 let device = runtime.device();
883 let queue = runtime.queue();
884
885 let uniform = ReduceUniform { cell_count, point_count, _pad0: 0, _pad1: 0 };
886
887 let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
888 label: Some("voxel-reduce-uniform"),
889 contents: bytemuck::bytes_of(&uniform),
890 usage: wgpu::BufferUsages::UNIFORM,
891 });
892
893 let output_len = cell_count as usize * std::mem::size_of::<f32>();
894 let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
895 label: Some("voxel-reduce-output"),
896 size: output_len as u64,
897 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
898 mapped_at_creation: false,
899 });
900 let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
901 label: Some("voxel-reduce-staging"),
902 size: output_len as u64,
903 usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
904 mapped_at_creation: false,
905 });
906
907 let pipelines = runtime.pipelines();
908
909 let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
910 label: Some("voxel-reduce-bind-group"),
911 layout: &pipelines.voxel_reduce.bind_group_layout,
912 entries: &[
913 wgpu::BindGroupEntry { binding: 0, resource: uniform_buffer.as_entire_binding() },
914 wgpu::BindGroupEntry { binding: 1, resource: point_indices.as_entire_binding() },
915 wgpu::BindGroupEntry { binding: 2, resource: values.as_entire_binding() },
916 wgpu::BindGroupEntry { binding: 3, resource: cell_starts.as_entire_binding() },
917 wgpu::BindGroupEntry { binding: 4, resource: output_buffer.as_entire_binding() },
918 ],
919 });
920
921 let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
922 label: Some("voxel-reduce-encoder"),
923 });
924
925 {
926 let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
927 label: Some("voxel-reduce-pass"),
928 timestamp_writes: None,
929 });
930 pass.set_pipeline(&pipelines.voxel_reduce.pipeline);
931 pass.set_bind_group(0, &bind_group, &[]);
932 pass.dispatch_workgroups(cell_count.div_ceil(WORKGROUP_SIZE), 1, 1);
933 }
934
935 encoder.copy_buffer_to_buffer(&output_buffer, 0, &staging_buffer, 0, output_len as u64);
936 queue.submit(Some(encoder.finish()));
937
938 read_staging_f32(device, &staging_buffer, cell_count as usize)
939}
940
941pub fn reduce_voxel_centroids_xyz(
943 runtime: &WgpuRuntime,
944 x: &[f32],
945 y: &[f32],
946 z: &[f32],
947 segments: &VoxelSegments,
948) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>)> {
949 if x.len() != y.len() || x.len() != z.len() {
950 return Err(SpatialError::BufferLengthMismatch { expected: x.len(), found: y.len() });
951 }
952
953 if let Some(result) = super::voxel_host_xyz::try_reduce_host_xyz(runtime, [x, y, z], segments) {
954 return result;
955 }
956
957 let out_x = reduce_voxel_average_f32(runtime, x, segments)?;
958 let out_y = reduce_voxel_average_f32(runtime, y, segments)?;
959 let out_z = reduce_voxel_average_f32(runtime, z, segments)?;
960 Ok((out_x, out_y, out_z))
961}
962
963pub fn reduce_voxel_centroids_xyz_gpu_buffers(
965 runtime: &WgpuRuntime,
966 x: &wgpu::Buffer,
967 y: &wgpu::Buffer,
968 z: &wgpu::Buffer,
969 segments: &GpuVoxelSegments,
970) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>)> {
971 if let Some(result) = super::voxel_xyz_packed::try_voxel_xyz_packed(
972 runtime,
973 [x, y, z],
974 segments,
975 super::voxel_xyz_packed::XyzMode::Centroid,
976 ) {
977 return result;
978 }
979
980 dispatch_voxel_reduce_xyz_f32(
981 runtime,
982 x,
983 y,
984 z,
985 segments.point_indices_buffer(),
986 segments.cell_starts_buffer(),
987 segments.cell_count(),
988 segments.point_count(),
989 )
990}
991
992fn dispatch_voxel_reduce_xyz_f32(
993 runtime: &WgpuRuntime,
994 values_x: &wgpu::Buffer,
995 values_y: &wgpu::Buffer,
996 values_z: &wgpu::Buffer,
997 point_indices: &wgpu::Buffer,
998 cell_starts: &wgpu::Buffer,
999 cell_count: u32,
1000 point_count: u32,
1001) -> SpatialResult<(Vec<f32>, Vec<f32>, Vec<f32>)> {
1002 if cell_count == 0 {
1003 return Ok((Vec::new(), Vec::new(), Vec::new()));
1004 }
1005
1006 let device = runtime.device();
1007 let queue = runtime.queue();
1008 let channel_len = cell_count as usize * std::mem::size_of::<f32>();
1009 let output_x = device.create_buffer(&wgpu::BufferDescriptor {
1010 label: Some("voxel-reduce-xyz-out-x"),
1011 size: channel_len as u64,
1012 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
1013 mapped_at_creation: false,
1014 });
1015 let output_y = device.create_buffer(&wgpu::BufferDescriptor {
1016 label: Some("voxel-reduce-xyz-out-y"),
1017 size: channel_len as u64,
1018 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
1019 mapped_at_creation: false,
1020 });
1021 let output_z = device.create_buffer(&wgpu::BufferDescriptor {
1022 label: Some("voxel-reduce-xyz-out-z"),
1023 size: channel_len as u64,
1024 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
1025 mapped_at_creation: false,
1026 });
1027 let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
1028 label: Some("voxel-reduce-xyz-staging"),
1029 size: (channel_len * 3) as u64,
1030 usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
1031 mapped_at_creation: false,
1032 });
1033
1034 let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
1035 label: Some("voxel-reduce-xyz-encoder"),
1036 });
1037 record_voxel_reduce_xyz_pass(
1038 &mut encoder,
1039 runtime,
1040 values_x,
1041 values_y,
1042 values_z,
1043 point_indices,
1044 cell_starts,
1045 cell_count,
1046 point_count,
1047 &output_x,
1048 &output_y,
1049 &output_z,
1050 )?;
1051 encoder.copy_buffer_to_buffer(&output_x, 0, &staging_buffer, 0, channel_len as u64);
1052 encoder.copy_buffer_to_buffer(
1053 &output_y,
1054 0,
1055 &staging_buffer,
1056 channel_len as u64,
1057 channel_len as u64,
1058 );
1059 encoder.copy_buffer_to_buffer(
1060 &output_z,
1061 0,
1062 &staging_buffer,
1063 (channel_len * 2) as u64,
1064 channel_len as u64,
1065 );
1066 queue.submit(Some(encoder.finish()));
1067
1068 read_staging_xyz(device, &staging_buffer, cell_count as usize)
1069}
1070
1071#[cfg(test)]
1072mod tests {
1073 use super::{
1074 reduce_voxel_average_f32, reduce_voxel_average_f32_multi_gpu, reduce_voxel_centroids_xyz,
1075 reduce_voxel_centroids_xyz_and_average_multi_gpu,
1076 reduce_voxel_centroids_xyz_and_gather_first_multi_gpu,
1077 };
1078 use crate::kernels::voxel_segments::build_voxel_segments;
1079 use crate::runtime::WgpuRuntime;
1080
1081 #[test]
1082 fn gpu_centroid_reduction_matches_cpu_reference() {
1083 let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
1084 let x = [0.0_f32, 0.1, 1.0, 1.1];
1085 let y = [0.0_f32, 0.0, 0.0, 0.0];
1086 let z = [0.0_f32, 0.0, 0.0, 0.0];
1087 let keys = vec![(0, 0, 0), (0, 0, 0), (2, 0, 0), (2, 0, 0)];
1088 let segments = build_voxel_segments(&keys);
1089
1090 let (gpu_x, gpu_y, gpu_z) =
1091 reduce_voxel_centroids_xyz(&runtime, &x, &y, &z, &segments).expect("gpu reduce");
1092
1093 assert!((gpu_x[0] - 0.05).abs() < 1e-5);
1094 assert!((gpu_x[1] - 1.05).abs() < 1e-5);
1095 assert_eq!(gpu_y, vec![0.0, 0.0]);
1096 assert_eq!(gpu_z, vec![0.0, 0.0]);
1097
1098 let intensity = [0.2_f32, 0.8, 10.0, 20.0];
1099 let gpu_i = reduce_voxel_average_f32(&runtime, &intensity, &segments).expect("gpu average");
1100 assert!((gpu_i[0] - 0.5).abs() < 1e-5);
1101 assert!((gpu_i[1] - 15.0).abs() < 1e-5);
1102 }
1103
1104 #[test]
1105 fn gpu_multi_reduce_matches_single_channel_reference() {
1106 use crate::kernels::voxel_sort::build_voxel_segments_gpu_from_keys;
1107
1108 let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
1109 let intensity = [0.2_f32, 0.8, 10.0, 20.0];
1110 let classification = [1.0_f32, 2.0, 3.0, 4.0];
1111 let keys = vec![(0, 0, 0), (0, 0, 0), (2, 0, 0), (2, 0, 0)];
1112 let segments = build_voxel_segments_gpu_from_keys(&runtime, &keys).expect("gpu segments");
1113
1114 let multi =
1115 reduce_voxel_average_f32_multi_gpu(&runtime, &[&intensity, &classification], &segments)
1116 .expect("multi reduce");
1117
1118 assert!((multi[0][0] - 0.5).abs() < 1e-5);
1119 assert!((multi[0][1] - 15.0).abs() < 1e-5);
1120 assert!((multi[1][0] - 1.5).abs() < 1e-5);
1121 assert!((multi[1][1] - 3.5).abs() < 1e-5);
1122 }
1123
1124 #[test]
1125 fn unified_xyz_and_attribute_readback_matches_staged_reference() {
1126 use crate::kernels::voxel_keys::compute_voxel_keys_gpu_buffers;
1127 use crate::kernels::voxel_sort::build_voxel_segments_gpu_from_keys_buffer;
1128
1129 let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
1130 let x = [0.0_f32, 0.1, 1.0, 1.1];
1131 let y = [0.0_f32, 0.0, 0.0, 0.0];
1132 let z = [0.0_f32, 0.0, 0.0, 0.0];
1133 let intensity = [0.2_f32, 0.8, 10.0, 20.0];
1134 let positions =
1135 compute_voxel_keys_gpu_buffers(&runtime, &x, &y, &z, [0.0; 3], 2.0).expect("keys");
1136 let segments = build_voxel_segments_gpu_from_keys_buffer(
1137 &runtime,
1138 positions.keys_buffer(),
1139 positions.point_count(),
1140 4,
1141 )
1142 .expect("segments");
1143
1144 let (out_x, out_y, out_z, attrs, _) = reduce_voxel_centroids_xyz_and_average_multi_gpu(
1145 &runtime,
1146 positions.x_buffer(),
1147 positions.y_buffer(),
1148 positions.z_buffer(),
1149 &[&intensity],
1150 &[],
1151 &segments,
1152 )
1153 .expect("unified reduce");
1154
1155 assert!((out_x[0] - 0.05).abs() < 1e-5);
1156 assert!((out_x[1] - 1.05).abs() < 1e-5);
1157 assert_eq!(out_y, vec![0.0, 0.0]);
1158 assert_eq!(out_z, vec![0.0, 0.0]);
1159 assert!((attrs[0][0] - 0.5).abs() < 1e-5);
1160 assert!((attrs[0][1] - 15.0).abs() < 1e-5);
1161
1162 let (_, _, _, gathered, _) = reduce_voxel_centroids_xyz_and_gather_first_multi_gpu(
1163 &runtime,
1164 positions.x_buffer(),
1165 positions.y_buffer(),
1166 positions.z_buffer(),
1167 &[&intensity],
1168 &[],
1169 &segments,
1170 )
1171 .expect("unified gather attrs");
1172 assert!((gathered[0][0] - 0.2).abs() < 1e-5);
1173 assert!((gathered[0][1] - 10.0).abs() < 1e-5);
1174 }
1175
1176 #[test]
1177 fn unified_xyz_and_u8_attribute_readback_matches_reference() {
1178 use crate::kernels::voxel_keys::compute_voxel_keys_gpu_buffers;
1179 use crate::kernels::voxel_sort::build_voxel_segments_gpu_from_keys_buffer;
1180
1181 let runtime = WgpuRuntime::new_headless().expect("wgpu runtime");
1182 let x = [0.0_f32, 0.1, 1.0, 1.1];
1183 let y = [0.0_f32, 0.0, 0.0, 0.0];
1184 let z = [0.0_f32, 0.0, 0.0, 0.0];
1185 let red = [10_u8, 20, 100, 200];
1186 let green = [30_u8, 40, 50, 60];
1187 let positions =
1188 compute_voxel_keys_gpu_buffers(&runtime, &x, &y, &z, [0.0; 3], 2.0).expect("keys");
1189 let segments = build_voxel_segments_gpu_from_keys_buffer(
1190 &runtime,
1191 positions.keys_buffer(),
1192 positions.point_count(),
1193 4,
1194 )
1195 .expect("segments");
1196
1197 let (_, _, _, _, u8_attrs) = reduce_voxel_centroids_xyz_and_average_multi_gpu(
1198 &runtime,
1199 positions.x_buffer(),
1200 positions.y_buffer(),
1201 positions.z_buffer(),
1202 &[],
1203 &[&red, &green],
1204 &segments,
1205 )
1206 .expect("unified u8 reduce");
1207
1208 assert_eq!(u8_attrs[0], vec![15, 150]);
1209 assert_eq!(u8_attrs[1], vec![35, 55]);
1210
1211 let (_, _, _, _, gathered_u8) = reduce_voxel_centroids_xyz_and_gather_first_multi_gpu(
1212 &runtime,
1213 positions.x_buffer(),
1214 positions.y_buffer(),
1215 positions.z_buffer(),
1216 &[],
1217 &[&red, &green],
1218 &segments,
1219 )
1220 .expect("unified u8 gather");
1221 assert_eq!(gathered_u8[0], vec![10, 100]);
1222 assert_eq!(gathered_u8[1], vec![30, 50]);
1223 }
1224}