wgpu_rt_lidar 0.1.1

A hardware-accelerated ray tracing library for robotics and autonomy simulation, providing flexible LiDAR and depth camera models.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
use glam::Vec3;
use rand::Rng;
use wgpu::util::{BufferInitDescriptor, DeviceExt};

use crate::utils::get_raytracing_gpu;
use crate::RayTraceScene;

#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Debug)]
pub struct VoxelItem {
    pub position: Vec3,
    pub occupied: u32,
}

pub struct DenseVoxel {
    /// Size of the voxel grid.
    top_right: Vec3,
    /// Bottom left corner of the voxel grid.
    bottom_left: Vec3,
    /// Resolution of each cell
    resolution: f32,
    /// Max number of items in each cell.
    max_density: u32,
    /// Voxel data
    data_on_cpu: Vec<VoxelItem>,
}

impl DenseVoxel {
    pub fn new(top_right: Vec3, bottom_left: Vec3, resolution: f32, max_density: u32) -> Self {
        if top_right.x < bottom_left.x || top_right.y < bottom_left.y || top_right.z < bottom_left.z
        {
            panic!("Invalid voxel grid bounds");
        }
        let num_voxel_grids = (top_right - bottom_left) / resolution;
        let num_voxel_grids = num_voxel_grids.x.ceil() as usize
            * num_voxel_grids.y.ceil() as usize
            * num_voxel_grids.z.ceil() as usize
            * max_density as usize;
        let data_on_cpu = (0..num_voxel_grids)
            .map(|_| VoxelItem {
                position: Vec3::new(0.0, 0.0, 0.0),
                occupied: 0,
            })
            .collect();
        Self {
            top_right,
            bottom_left,
            resolution,
            max_density,
            data_on_cpu,
        }
    }

    pub fn width(&self) -> f32 {
        self.top_right.x - self.bottom_left.x
    }

    pub fn width_steps(&self) -> usize {
        (self.width() / self.resolution).ceil() as usize
    }

    pub fn height(&self) -> f32 {
        self.top_right.z - self.bottom_left.z
    }

    pub fn height_steps(&self) -> usize {
        (self.height() / self.resolution).ceil() as usize
    }

    pub fn length(&self) -> f32 {
        self.top_right.y - self.bottom_left.y
    }

    pub fn length_steps(&self) -> usize {
        (self.length() / self.resolution).ceil() as usize
    }

    pub fn capacity(&self) -> usize {
        self.width_steps() * self.height_steps() * self.length_steps() * self.max_density as usize
    }

    pub fn add_item(&mut self, item: VoxelItem) -> Result<usize, String> {
        if item.position.x < self.bottom_left.x
            || item.position.y < self.bottom_left.y
            || item.position.z < self.bottom_left.z
        {
            return Err("Out of voxel bounds".to_string());
        }
        if item.position.x > self.top_right.x
            || item.position.y > self.top_right.y
            || item.position.z > self.top_right.z
        {
            return Err("Out of voxel bounds".to_string());
        }

        let x = ((item.position.x - self.bottom_left.x) / self.resolution) as usize;
        let y = ((item.position.y - self.bottom_left.y) / self.resolution) as usize;
        let z = ((item.position.z - self.bottom_left.z) / self.resolution) as usize;

        let index = x + y * self.width_steps() + z * self.height_steps() * self.width_steps();
        let index = index * self.max_density as usize;

        for i in 0..self.max_density as usize {
            if self.data_on_cpu[index + i].occupied == 0 {
                self.data_on_cpu[index + i] = item;
                self.data_on_cpu[index + i].occupied = 1;
                return Ok(index + i);
            }
        }
        Err("No space in voxel grid".to_string())
    }

    fn index(&self, x: usize, y: usize, z: usize) -> usize {
        (x + y * self.width_steps() + z * self.height_steps() * self.width_steps())
            * self.max_density as usize
    }

    fn from_index(&self, index: usize) -> (usize, usize, usize) {
        let z = index / (self.height_steps() * self.width_steps() * self.max_density as usize);
        let index =
            index - z * self.height_steps() * self.width_steps() * self.max_density as usize;
        let y = index / (self.width_steps() * self.max_density as usize);
        let index = index - y * self.width_steps() * self.max_density as usize;
        let x = index / self.max_density as usize;
        (x, y, z)
    }

    pub fn get_items_in_cell(&self, x: usize, y: usize, z: usize) -> Vec<VoxelItem> {
        let index = x + y * self.width_steps() + z * self.height_steps() * self.width_steps();
        let index = index * self.max_density as usize;
        let mut items = vec![];
        for i in 0..self.max_density as usize {
            if self.data_on_cpu[index + i].occupied == 1 {
                items.push(self.data_on_cpu[index + i]);
            }
        }
        items
    }

    pub fn get_items_in_cell_position(&self, position: Vec3) -> Vec<VoxelItem> {
        let x = ((position.x - self.bottom_left.x) / self.resolution) as usize;
        let y = ((position.y - self.bottom_left.y) / self.resolution) as usize;
        let z = ((position.z - self.bottom_left.z) / self.resolution) as usize;
        self.get_items_in_cell(x, y, z)
    }

    pub fn to_gpu_buffers(&self, device: &wgpu::Device) -> DenseVoxelGpuRepresentation {
        let data_on_gpu = device.create_buffer_init(&BufferInitDescriptor {
            label: Some("Voxel Grid Data"),
            contents: bytemuck::cast_slice(&self.data_on_cpu),
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        });
        let dense_parameters = DenseVoxelGpuParams {
            top_right: self.top_right,
            width_steps: self.width_steps() as u32,
            bottom_left: self.bottom_left,
            height_steps: self.height_steps() as u32,
            max_density: self.max_density,
            resolution: self.resolution,
            _padding: 0.0,
            _padding2: 0.0,
        };
        let parameters = device.create_buffer_init(&BufferInitDescriptor {
            label: Some("Voxel Grid Parameters"),
            contents: bytemuck::cast_slice(&[dense_parameters]),
            usage: wgpu::BufferUsages::UNIFORM
                | wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST,
        });
        DenseVoxelGpuRepresentation {
            data_on_gpu,
            parameters,
            cpu_parameters: dense_parameters,
            height_steps: self.height_steps() as u32,
            width_steps: self.width_steps() as u32,
            length_steps: self.length_steps() as u32,
        }
    }
}

#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Debug)]
struct DenseVoxelGpuParams {
    // Word 1
    top_right: Vec3,
    width_steps: u32,

    // Word 2
    bottom_left: Vec3,
    height_steps: u32,

    // Word 3
    max_density: u32,
    resolution: f32,
    _padding: f32,
    _padding2: f32,
}

pub struct DenseVoxelGpuRepresentation {
    data_on_gpu: wgpu::Buffer,
    parameters: wgpu::Buffer,
    cpu_parameters: DenseVoxelGpuParams,
    height_steps: u32,
    width_steps: u32,
    length_steps: u32,
}

impl DenseVoxelGpuRepresentation {
    fn prepare_query_points(&self, query_points: &Vec<Vec3>) -> DenseVoxel {
        let mut query_voxel = DenseVoxel::new(
            self.cpu_parameters.top_right,
            self.cpu_parameters.bottom_left,
            self.cpu_parameters.resolution,
            self.cpu_parameters.max_density,
        );
        query_points.iter().for_each(|point| {
            query_voxel
                .add_item(VoxelItem {
                    position: *point,
                    occupied: 1,
                })
                .unwrap();
        });
        query_voxel
    }
}

/// Queries an approximate nearest neighbour for each point in the `points` vector.
pub async fn query_nearest_neighbours(voxel: &DenseVoxel, points: Vec<Vec3>) -> Option<Vec<u32>> {
    let instance = wgpu::Instance::default();
    let (_adapter, device, queue) = get_raytracing_gpu(&instance).await;
    dense_voxel_nearest_neighbor(&device, &queue, voxel, &points).await
}

pub struct DenseVoxelNearestNeighbors {
    pipeline: wgpu::ComputePipeline,
    result_buffer: wgpu::Buffer,
}

impl DenseVoxelNearestNeighbors {
    fn new(device: &wgpu::Device, voxel: &DenseVoxel) -> Self {
        let cs_module = device.create_shader_module(wgpu::include_wgsl!("nn.wgsl"));
        let results = vec![0xFFFFu32; voxel.capacity()];
        let result_buffer = device.create_buffer_init(&BufferInitDescriptor {
            label: Some("Result"),
            contents: bytemuck::cast_slice(&results),
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
        });

        let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: None,
            layout: None,
            module: &cs_module,
            entry_point: Some("main"),
            compilation_options: Default::default(),
            cache: None,
        });

        Self {
            pipeline: compute_pipeline,
            result_buffer,
        }
    }
}

async fn dense_voxel_nearest_neighbor(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    voxel: &DenseVoxel,
    query_points: &Vec<Vec3>,
) -> Option<Vec<u32>> {
    // Loads the shader from WGSL
    let cs_module = device.create_shader_module(wgpu::include_wgsl!("nn.wgsl"));

    // Gets the size in bytes of the buffer.
    let size = (voxel.capacity() * 4) as wgpu::BufferAddress;

    let results = vec![0xFFFFu32; voxel.capacity()];
    let result_buffer = device.create_buffer_init(&BufferInitDescriptor {
        label: Some("Result"),
        contents: bytemuck::cast_slice(&results),
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
    });

    // Instantiates buffer without data.
    // `usage` of buffer specifies how it can be used:
    //   `BufferUsages::MAP_READ` allows it to be read (outside the shader).
    //   `BufferUsages::COPY_DST` allows it to be the destination of the copy.
    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });

    // A bind group defines how buffers are accessed by shaders.
    // It is to WebGPU what a descriptor set is to Vulkan.
    // `binding` here refers to the `binding` of a buffer in the shader (`layout(set = 0, binding = 0) buffer`).
    let base = voxel.to_gpu_buffers(device);
    let other = base
        .prepare_query_points(query_points)
        .to_gpu_buffers(device);
    // A pipeline specifies the operation of a shader

    // Instantiates the pipeline.
    let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: None,
        layout: None,
        module: &cs_module,
        entry_point: Some("main"),
        compilation_options: Default::default(),
        cache: None,
    });

    // Instantiates the bind group, once again specifying the binding of buffers.
    let bind_group_layout = compute_pipeline.get_bind_group_layout(0);
    println!("Bind group layout: {bind_group_layout:?}");
    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &bind_group_layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: base.data_on_gpu.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: base.parameters.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 2,
                resource: other.data_on_gpu.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 3,
                resource: result_buffer.as_entire_binding(),
            },
        ],
    });

    // A command encoder executes one or many pipelines.
    // It is to WebGPU what a command buffer is to Vulkan.
    let mut encoder =
        device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
    {
        let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: None,
            timestamp_writes: None,
        });
        cpass.set_pipeline(&compute_pipeline);
        cpass.set_bind_group(0, &bind_group, &[]);
        cpass.insert_debug_marker("compute collatz iterations");
        cpass.dispatch_workgroups(
            voxel.length_steps() as u32,
            voxel.width_steps() as u32,
            voxel.height_steps() as u32,
        ); // Number of cells to run, the (x,y,z) size of item being processed
    }
    // Sets adds copy operation to command encoder.
    // Will copy data from storage buffer on GPU to staging buffer on CPU.
    encoder.copy_buffer_to_buffer(&result_buffer, 0, &staging_buffer, 0, size);

    // Submits command encoder for processing
    queue.submit(Some(encoder.finish()));

    // Note that we're not calling `.await` here.
    let buffer_slice = staging_buffer.slice(..);
    // Sets the buffer up for mapping, sending over the result of the mapping back to us when it is finished.
    let (sender, receiver) = flume::bounded(1);
    buffer_slice.map_async(wgpu::MapMode::Read, move |v| sender.send(v).unwrap());

    // Poll the device in a blocking manner so that our future resolves.
    // In an actual application, `device.poll(...)` should
    // be called in an event loop or on another thread.
    device
        .poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        })
        .unwrap();

    // Awaits until `buffer_future` can be read from
    if let Ok(Ok(())) = receiver.recv_async().await {
        // Gets contents of buffer
        let data = buffer_slice.get_mapped_range();
        // Since contents are got in bytes, this converts these bytes back to u32
        let result = bytemuck::cast_slice(&data).to_vec();

        // With the current interface, we have to make sure all mapped views are
        // dropped before we unmap the buffer.
        drop(data);
        staging_buffer.unmap(); // Unmaps buffer from memory
                                // If you are familiar with C++ these 2 lines can be thought of similarly to:
                                //   delete myPointer;
                                //   myPointer = NULL;
                                // It effectively frees the memory

        // Returns data from buffer
        Some(result)
    } else {
        panic!("failed to run compute on gpu!")
    }
}

#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Debug)]
pub struct Tree {
    /*x: i32,
    y: i32,
    z: i32,*/
    pub position: Vec3,
    pub parent: u32,
}

#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Debug)]
struct State {
    x: u32,
    y: u32,
    z: u32,
    w: u32,
}

pub async fn execute_experimental_gpu_rrt(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    voxel: &DenseVoxel,
    lidar: &RayTraceScene,
) -> Option<Vec<Tree>> {
    // Loads the shader from WGSL
    let cs_module = device.create_shader_module(wgpu::include_wgsl!("rrt.wgsl"));

    let results = vec![
        Tree {
            position: Vec3::new(0.0, 0.0, 0.0),
            parent: 50000,
        };
        voxel.capacity()
    ];

    let result_buffer = device.create_buffer_init(&BufferInitDescriptor {
        label: Some("Result"),
        contents: bytemuck::cast_slice(&results),
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
    });

    let found = device.create_buffer_init(&BufferInitDescriptor {
        label: Some("Result"),
        contents: bytemuck::cast_slice(&[0u32]),
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
    });

    // Instantiates buffer without data.
    // `usage` of buffer specifies how it can be used:
    //   `BufferUsages::MAP_READ` allows it to be read (outside the shader).
    //   `BufferUsages::COPY_DST` allows it to be the destination of the copy.
    let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: result_buffer.size(),
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });

    // A bind group defines how buffers are accessed by shaders.
    // It is to WebGPU what a descriptor set is to Vulkan.
    // `binding` here refers to the `binding` of a buffer in the shader (`layout(set = 0, binding = 0) buffer`).
    let base = voxel.to_gpu_buffers(device);

    let mut rng = rand::rng();
    let random_seed: Vec<_> = (0..voxel.capacity())
        .map(|_| State {
            x: rng.random(),
            y: rng.random(),
            z: rng.random(),
            w: rng.random(),
        })
        .collect();

    let random_seed = device.create_buffer_init(&BufferInitDescriptor {
        label: Some("Random seed"),
        contents: bytemuck::cast_slice(&random_seed),
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
    });
    // A pipeline specifies the operation of a shader

    // Instantiates the pipeline.
    let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: None,
        layout: None,
        module: &cs_module,
        entry_point: Some("main"),
        compilation_options: Default::default(),
        cache: None,
    });

    // Instantiates the bind group, once again specifying the binding of buffers.
    let bind_group_layout = compute_pipeline.get_bind_group_layout(0);
    println!("Bind group layout: {bind_group_layout:?}");
    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &bind_group_layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: base.data_on_gpu.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: base.parameters.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 2,
                resource: random_seed.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 3,
                resource: result_buffer.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 4,
                resource: wgpu::BindingResource::AccelerationStructure(&lidar.tlas_package),
            },
            wgpu::BindGroupEntry {
                binding: 5,
                resource: found.as_entire_binding(),
            },
        ],
    });
    let time = std::time::Instant::now();

    let mut encoder =
        device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
    {
        let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: None,
            timestamp_writes: None,
        });
        cpass.set_pipeline(&compute_pipeline);
        cpass.set_bind_group(0, &bind_group, &[]);
        cpass.insert_debug_marker("compute collatz iterations");
        cpass.dispatch_workgroups(
            2, //voxel.length_steps() as u32,
            2, //voxel.width_steps() as u32,
            2, //voxel.height_steps() as u32,
        ); // Number of cells to run, the (x,y,z) size of item being processed
    }

    // Sets adds copy operation to command encoder.
    // Will copy data from storage buffer on GPU to staging buffer on CPU.
    encoder.copy_buffer_to_buffer(&result_buffer, 0, &staging_buffer, 0, result_buffer.size());

    // Submits command encoder for processing
    queue.submit(Some(encoder.finish()));

    // Note that we're not calling `.await` here.
    let buffer_slice = staging_buffer.slice(..);
    // Sets the buffer up for mapping, sending over the result of the mapping back to us when it is finished.
    let (sender, receiver) = flume::bounded(1);
    buffer_slice.map_async(wgpu::MapMode::Read, move |v| sender.send(v).unwrap());

    // Poll the device in a blocking manner so that our future resolves.
    // In an actual application, `device.poll(...)` should
    // be called in an event loop or on another thread.
    device
        .poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        })
        .unwrap();

    // Awaits until `buffer_future` can be read from
    if let Ok(Ok(())) = receiver.recv_async().await {
        // Gets contents of buffer
        let data = buffer_slice.get_mapped_range();
        // Since contents are got in bytes, this converts these bytes back to u32
        let result = bytemuck::cast_slice(&data).to_vec();

        // With the current interface, we have to make sure all mapped views are
        // dropped before we unmap the buffer.
        drop(data);
        staging_buffer.unmap(); // Unmaps buffer from memory
                                // If you are familiar with C++ these 2 lines can be thought of similarly to:
                                //   delete myPointer;
                                //   myPointer = NULL;
                                // It effectively frees the memory

        println!("Time taken: {:?}", time.elapsed());

        // Returns data from buffer
        Some(result)
    } else {
        panic!("failed to run compute on gpu!")
    }
}

#[cfg(test)]
#[tokio::test]
async fn test_voxel_nn() {
    let mut voxel_grid =
        DenseVoxel::new(Vec3::new(5.0, 5.0, 5.0), Vec3::new(0.0, 0.0, 0.0), 0.5, 10);

    voxel_grid
        .add_item(VoxelItem {
            position: Vec3::new(0.5, 0.5, 0.5),
            occupied: 0,
        })
        .unwrap();

    voxel_grid
        .add_item(VoxelItem {
            position: Vec3::new(1.55, 1.55, 1.55),
            occupied: 0,
        })
        .unwrap();

    let target = voxel_grid
        .add_item(VoxelItem {
            position: Vec3::new(1.6, 1.6, 1.6),
            occupied: 0,
        })
        .unwrap();

    let items = voxel_grid.get_items_in_cell(1, 1, 1);
    assert_eq!(items.len(), 1);

    let items = voxel_grid.get_items_in_cell_position(Vec3::new(1.6, 1.6, 1.6));
    assert_eq!(items.len(), 2);

    for i in 0..5 {
        for j in 0..5 {
            for k in 0..5 {
                let internal_index = voxel_grid.index(i, j, k);
                let (x, y, z) = voxel_grid.from_index(internal_index);
                assert_eq!(i, x);
                assert_eq!(j, y);
                assert_eq!(k, z);
            }
        }
    }

    let queries = vec![Vec3::new(1.65, 1.65, 1.65)];
    let times_now = std::time::Instant::now();
    let result = query_nearest_neighbours(&voxel_grid, queries)
        .await
        .unwrap();
    println!("Time taken: {:?}", times_now.elapsed());
    let result: Vec<_> = result.iter().filter(|p| **p != 0xFFFFu32).collect();
    assert_eq!(result.len(), 1);
    assert_eq!(*result[0], target as u32);
    //run().await;
}

#[cfg(test)]
#[tokio::test]
async fn test_voxel_nn_far_away() {
    let mut voxel_grid =
        DenseVoxel::new(Vec3::new(5.0, 5.0, 5.0), Vec3::new(0.0, 0.0, 0.0), 0.5, 10);

    voxel_grid
        .add_item(VoxelItem {
            position: Vec3::new(0.5, 0.5, 0.5),
            occupied: 0,
        })
        .unwrap();

    voxel_grid
        .add_item(VoxelItem {
            position: Vec3::new(1.55, 1.55, 1.55),
            occupied: 0,
        })
        .unwrap();

    let target = voxel_grid
        .add_item(VoxelItem {
            position: Vec3::new(1.6, 1.6, 1.6),
            occupied: 0,
        })
        .unwrap();

    let items = voxel_grid.get_items_in_cell(1, 1, 1);
    assert_eq!(items.len(), 1);

    let items = voxel_grid.get_items_in_cell_position(Vec3::new(1.6, 1.6, 1.6));
    assert_eq!(items.len(), 2);

    for i in 0..5 {
        for j in 0..5 {
            for k in 0..5 {
                let internal_index = voxel_grid.index(i, j, k);
                let (x, y, z) = voxel_grid.from_index(internal_index);
                assert_eq!(i, x);
                assert_eq!(j, y);
                assert_eq!(k, z);
            }
        }
    }

    let queries = vec![Vec3::new(2.1, 1.6, 1.6)];
    let times_now = std::time::Instant::now();
    let result = query_nearest_neighbours(&voxel_grid, queries)
        .await
        .unwrap();
    println!("Time taken: {:?}", times_now.elapsed());
    let result: Vec<_> = result.iter().filter(|p| **p != 0xFFFFu32).collect();
    assert_eq!(result.len(), 1);
    assert_eq!(*result[0], target as u32);
    //run().await;
}

#[cfg(test)]
#[tokio::test]
async fn test_voxel_rrt() {
    use crate::utils::{create_cube, get_raytracing_gpu};

    let mut voxel_grid =
        DenseVoxel::new(Vec3::new(5.0, 5.0, 5.0), Vec3::new(0.0, 0.0, 0.0), 0.5, 10);

    voxel_grid
        .add_item(VoxelItem {
            position: Vec3::new(0.5, 0.5, 0.5),
            occupied: 0,
        })
        .unwrap();

    for i in 0..5 {
        for j in 0..5 {
            for k in 0..5 {
                let internal_index = voxel_grid.index(i, j, k);
                let (x, y, z) = voxel_grid.from_index(internal_index);
                assert_eq!(i, x);
                assert_eq!(j, y);
                assert_eq!(k, z);
            }
        }
    }

    let instance = wgpu::Instance::default();

    let (_, device, queue) = get_raytracing_gpu(&instance).await;
    let cube = create_cube(0.2);
    let instances: Vec<_> = (0..4)
        .flat_map(|x| {
            (0..4).flat_map(move |y| {
                (2..4).map(move |z| crate::Instance {
                    asset_mesh_index: 0,
                    transform: glam::Affine3A::from_rotation_translation(
                        glam::Quat::from_rotation_y(0.0),
                        glam::Vec3 {
                            x: x as f32 + 0.5,
                            y: y as f32 + 0.5,
                            z: z as f32 + 0.5,
                        },
                    ),
                })
            })
        })
        .collect();

    let scene = RayTraceScene::new(&device, &queue, &vec![cube], &instances).await;
    let one = execute_experimental_gpu_rrt(&device, &queue, &voxel_grid, &scene)
        .await
        .unwrap();
    println!("{:?}", one.len());
    let result: Vec<_> = one.iter().filter(|p| p.parent > 50000).collect();
    println!("Valid end goals: {:?}", result.len());

    let result: Vec<_> = one.iter().filter(|p| p.parent != 50000).collect();
    println!("States expanded {:?}", result.len());
}