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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
use {
crate::{
AnyAccelerationStructureNode, AnyResource, Node,
driver::{
accel_struct::{
AccelerationStructureGeometry, AccelerationStructureGeometryInfo,
DeviceOrHostAddress,
},
device::Device,
},
},
ash::vk,
log::trace,
std::{cell::RefCell, ops::Deref},
};
#[cfg(debug_assertions)]
use crate::Execution;
/// Recording interface for general Vulkan commands.
///
/// This structure provides a strongly-typed set of methods which allow acceleration structures to
/// be built and updated.
///
/// # Examples
///
/// Basic usage:
///
/// ```no_run
/// # use ash::vk;
/// # use vk_graph::Graph;
/// # fn main() {
/// # let mut my_graph = Graph::default();
/// my_graph.begin_cmd()
/// .record_cmd(move |cmd| {
/// // Use provided command buffer functions or native calls
/// assert_ne!(cmd.handle, vk::CommandBuffer::null());
/// });
/// # }
/// ```
#[derive(Clone, Copy)]
pub struct CommandRef<'a> {
cmd: &'a crate::driver::cmd_buf::CommandBuffer,
#[cfg(debug_assertions)]
exec: &'a Execution,
resources: &'a [AnyResource],
}
impl<'a> CommandRef<'a> {
pub(crate) fn new(
cmd: &'a crate::driver::cmd_buf::CommandBuffer,
resources: &'a [AnyResource],
#[cfg(debug_assertions)] exec: &'a Execution,
) -> Self {
Self {
cmd,
#[cfg(debug_assertions)]
exec,
resources,
}
}
/// Build acceleration structures.
///
/// There is no ordering or synchronization implied between any of the individual acceleration
/// structure builds.
///
/// Requires a scratch buffer which was created with the following requirements:
///
/// - Flags must include [`vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS`]
/// - Size must be equal to or greater than the `build_size` value returned by
/// `AccelerationStructure::size_of`, aligned to `min_accel_struct_scratch_offset_alignment`
/// of `PhysicalDevice::accel_struct_properties`.
///
/// # Examples
///
/// Basic usage:
///
/// ```no_run
/// # use ash::vk;
/// # use vk_graph::cmd::BuildAccelerationStructureInfo;
/// # use vk_graph::driver::{sync::AccessType, DriverError};
/// # use vk_graph::driver::device::{Device, DeviceInfo};
/// # use vk_graph::driver::accel_struct::{
/// # AccelerationStructure,
/// # AccelerationStructureGeometry,
/// # AccelerationStructureGeometryData,
/// # AccelerationStructureGeometryInfo,
/// # AccelerationStructureInfo,
/// # DeviceOrHostAddress,
/// # };
/// # use vk_graph::driver::buffer::{Buffer, BufferInfo};
/// # use vk_graph::Graph;
/// # use vk_graph::driver::shader::Shader;
/// # fn main() -> Result<(), DriverError> {
/// # let device = Device::new(DeviceInfo::default())?;
/// # let mut my_graph = Graph::default();
/// # let info = AccelerationStructureInfo::blas(1);
/// # let blas_accel_struct = AccelerationStructure::create(&device, info)?;
/// # let blas_node = my_graph.bind_resource(blas_accel_struct);
/// # let scratch_buf_info =
/// # BufferInfo::device_mem(8, vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS);
/// # let scratch_buf = Buffer::create(&device, scratch_buf_info)?;
/// # let scratch_buf = my_graph.bind_resource(scratch_buf);
/// # let buf_info = BufferInfo::device_mem(8, vk::BufferUsageFlags::INDEX_BUFFER);
/// # let my_idx_buf = Buffer::create(&device, buf_info)?;
/// # let buf_info = BufferInfo::device_mem(8, vk::BufferUsageFlags::VERTEX_BUFFER);
/// # let my_vtx_buf = Buffer::create(&device, buf_info)?;
/// # let index_buf = my_graph.bind_resource(my_idx_buf);
/// # let vertex_buf = my_graph.bind_resource(my_vtx_buf);
/// my_graph.begin_cmd()
/// .resource_access(index_buf, AccessType::IndexBuffer)
/// .resource_access(vertex_buf, AccessType::VertexBuffer)
/// .resource_access(scratch_buf, AccessType::AccelerationStructureBufferWrite)
/// .resource_access(blas_node, AccessType::AccelerationStructureBuildWrite)
/// .record_cmd(move |cmd| {
/// let scratch_addr = cmd.resource(scratch_buf).device_address();
/// let geom = AccelerationStructureGeometry {
/// max_primitive_count: 64,
/// flags: vk::GeometryFlagsKHR::OPAQUE,
/// geometry: AccelerationStructureGeometryData::Triangles {
/// index_addr: DeviceOrHostAddress::DeviceAddress(
/// cmd.resource(index_buf).device_address()
/// ),
/// index_type: vk::IndexType::UINT32,
/// max_vertex: 42,
/// transform_addr: None,
/// vertex_addr: DeviceOrHostAddress::DeviceAddress(
/// cmd.resource(vertex_buf).device_address(),
/// ),
/// vertex_format: vk::Format::R32G32B32_SFLOAT,
/// vertex_stride: 12,
/// },
/// };
/// let build_range = vk::AccelerationStructureBuildRangeInfoKHR {
/// first_vertex: 0,
/// primitive_count: 1,
/// primitive_offset: 0,
/// transform_offset: 0,
/// };
/// let info = AccelerationStructureGeometryInfo::blas([(geom, build_range)]);
///
/// cmd.build_accel_struct(&[
/// BuildAccelerationStructureInfo::new(blas_node, scratch_addr, info)
/// ]);
/// });
/// # Ok(()) }
/// ```
///
/// See also:
///
/// - [`examples/ray_omni.rs`](/examples/ray_omni.rs)
/// - [`examples/ray_trace.rs`](/examples/ray_trace.rs)
/// - [`examples/rt_triangle.rs`](/examples/rt_triangle.rs)
pub fn build_accel_struct(&self, infos: &[BuildAccelerationStructureInfo]) -> &Self {
#[derive(Default)]
struct Tls {
geometries: Vec<vk::AccelerationStructureGeometryKHR<'static>>,
ranges: Vec<vk::AccelerationStructureBuildRangeInfoKHR>,
}
thread_local! {
static TLS: RefCell<Tls> = Default::default();
}
TLS.with_borrow_mut(|tls| {
tls.geometries.clear();
tls.geometries.extend(infos.iter().flat_map(|info| {
info.build_data.geometries.iter().map(|(geometry, _)| {
<&AccelerationStructureGeometry as Into<
vk::AccelerationStructureGeometryKHR,
>>::into(geometry)
})
}));
tls.ranges.clear();
tls.ranges.extend(
infos
.iter()
.flat_map(|info| info.build_data.geometries.iter().map(|(_, range)| *range)),
);
let vk_ranges = {
let mut start = 0;
let mut vk_ranges = Vec::with_capacity(infos.len());
for info in infos {
let end = start + info.build_data.geometries.len();
vk_ranges.push(&tls.ranges[start..end]);
start = end;
}
vk_ranges
};
let vk_infos = {
let mut start = 0;
let mut vk_infos = Vec::with_capacity(infos.len());
for info in infos {
let end = start + info.build_data.geometries.len();
vk_infos.push(
vk::AccelerationStructureBuildGeometryInfoKHR::default()
.ty(info.build_data.ty)
.flags(info.build_data.flags)
.mode(vk::BuildAccelerationStructureModeKHR::BUILD)
.dst_acceleration_structure(self.resource(info.accel_struct).handle)
.geometries(&tls.geometries[start..end])
.scratch_data(info.scratch_addr.into()),
);
start = end;
}
vk_infos
};
unsafe {
Device::expect_accel_struct_ext(&self.cmd.device)
.cmd_build_acceleration_structures(self.cmd.handle, &vk_infos, &vk_ranges);
}
});
self
}
/// Builds acceleration structures with some parameters provided on the device.
///
/// There is no ordering or synchronization implied between any of the individual acceleration
/// structure builds.
///
/// Each [`BuildAccelerationStructureIndirectInfo::range_base`] is a buffer device address which
/// points to an array of [`vk::AccelerationStructureBuildRangeInfoKHR`] structures defining
/// dynamic offsets to the addresses where geometry data is stored.
pub fn build_accel_struct_indirect(
&self,
infos: &[BuildAccelerationStructureIndirectInfo],
) -> &Self {
#[derive(Default)]
struct Tls {
geometries: Vec<vk::AccelerationStructureGeometryKHR<'static>>,
max_primitive_counts: Vec<u32>,
range_bases: Vec<vk::DeviceAddress>,
range_strides: Vec<u32>,
}
thread_local! {
static TLS: RefCell<Tls> = Default::default();
}
TLS.with_borrow_mut(|tls| {
tls.geometries.clear();
tls.geometries.extend(infos.iter().flat_map(|info| {
info.build_data.geometries.iter().map(
<&AccelerationStructureGeometry as Into<
vk::AccelerationStructureGeometryKHR,
>>::into,
)
}));
tls.max_primitive_counts.clear();
tls.max_primitive_counts
.extend(infos.iter().flat_map(|info| {
info.build_data
.geometries
.iter()
.map(|geometry| geometry.max_primitive_count)
}));
tls.range_bases.clear();
tls.range_strides.clear();
let (vk_infos, vk_max_primitive_counts) = {
let mut start = 0;
let mut vk_infos = Vec::with_capacity(infos.len());
let mut vk_max_primitive_counts = Vec::with_capacity(infos.len());
for info in infos {
let end = start + info.build_data.geometries.len();
vk_infos.push(
vk::AccelerationStructureBuildGeometryInfoKHR::default()
.ty(info.build_data.ty)
.flags(info.build_data.flags)
.mode(vk::BuildAccelerationStructureModeKHR::BUILD)
.dst_acceleration_structure(self.resource(info.accel_struct).handle)
.geometries(&tls.geometries[start..end])
.scratch_data(info.scratch_data.into()),
);
vk_max_primitive_counts.push(&tls.max_primitive_counts[start..end]);
start = end;
tls.range_bases.push(info.range_base);
tls.range_strides.push(info.range_stride);
}
(vk_infos, vk_max_primitive_counts)
};
unsafe {
Device::expect_accel_struct_ext(&self.cmd.device)
.cmd_build_acceleration_structures_indirect(
self.cmd.handle,
&vk_infos,
&tls.range_bases,
&tls.range_strides,
&vk_max_primitive_counts,
);
}
});
self
}
pub(crate) fn cmd_push_constants(
&self,
layout: vk::PipelineLayout,
push_consts: &[vk::PushConstantRange],
offset: u32,
data: &[u8],
) {
for push_const in push_consts {
let push_const_end = push_const.offset + push_const.size;
let data_end = offset + data.len() as u32;
let end = data_end.min(push_const_end);
let start = offset.max(push_const.offset);
if end > start {
trace!(
" push constants {:?} {}..{}",
push_const.stage_flags, start, end
);
unsafe {
self.device.cmd_push_constants(
self.handle,
layout,
push_const.stage_flags,
start,
&data[(start - offset) as usize..(end - offset) as usize],
);
}
}
}
}
/// Update acceleration structures.
///
/// There is no ordering or synchronization implied between any of the individual acceleration
/// structure updates.
///
/// Requires a scratch buffer which was created with the following requirements:
///
/// - Flags must include [`vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS`]
/// - Size must be equal to or greater than the `update_size` value returned by
/// `AccelerationStructure::size_of`, aligned to `min_accel_struct_scratch_offset_alignment`
/// of `PhysicalDevice::accel_struct_properties`.
pub fn update_accel_struct(&self, infos: &[UpdateAccelerationStructureInfo]) -> &Self {
#[derive(Default)]
struct Tls {
geometries: Vec<vk::AccelerationStructureGeometryKHR<'static>>,
ranges: Vec<vk::AccelerationStructureBuildRangeInfoKHR>,
}
thread_local! {
static TLS: RefCell<Tls> = Default::default();
}
TLS.with_borrow_mut(|tls| {
tls.geometries.clear();
tls.geometries.extend(infos.iter().flat_map(|info| {
info.update_data.geometries.iter().map(|(geometry, _)| {
<&AccelerationStructureGeometry as Into<
vk::AccelerationStructureGeometryKHR,
>>::into(geometry)
})
}));
tls.ranges.clear();
tls.ranges.extend(
infos
.iter()
.flat_map(|info| info.update_data.geometries.iter().map(|(_, range)| *range)),
);
let vk_ranges = {
let mut start = 0;
let mut vk_ranges = Vec::with_capacity(infos.len());
for info in infos {
let end = start + info.update_data.geometries.len();
vk_ranges.push(&tls.ranges[start..end]);
start = end;
}
vk_ranges
};
let vk_infos = {
let mut start = 0;
let mut vk_infos = Vec::with_capacity(infos.len());
for info in infos {
let end = start + info.update_data.geometries.len();
vk_infos.push(
vk::AccelerationStructureBuildGeometryInfoKHR::default()
.ty(info.update_data.ty)
.flags(info.update_data.flags)
.mode(vk::BuildAccelerationStructureModeKHR::UPDATE)
.dst_acceleration_structure(self.resource(info.dst_accel_struct).handle)
.src_acceleration_structure(self.resource(info.src_accel_struct).handle)
.geometries(&tls.geometries[start..end])
.scratch_data(info.scratch_addr.into()),
);
start = end;
}
vk_infos
};
unsafe {
Device::expect_accel_struct_ext(&self.cmd.device)
.cmd_build_acceleration_structures(self.cmd.handle, &vk_infos, &vk_ranges);
}
});
self
}
/// Updates acceleration structures with some parameters provided on the device.
///
/// There is no ordering or synchronization implied between any of the individual acceleration
/// structure updates.
///
/// Each [`UpdateAccelerationStructureIndirectInfo::range_base`] is a buffer device address
/// which points to an array of [`vk::AccelerationStructureBuildRangeInfoKHR`] structures
/// defining dynamic offsets to the addresses where geometry data is stored.
pub fn update_accel_struct_indirect(
&self,
infos: &[UpdateAccelerationStructureIndirectInfo],
) -> &Self {
#[derive(Default)]
struct Tls {
geometries: Vec<vk::AccelerationStructureGeometryKHR<'static>>,
max_primitive_counts: Vec<u32>,
range_bases: Vec<vk::DeviceAddress>,
range_strides: Vec<u32>,
}
thread_local! {
static TLS: RefCell<Tls> = Default::default();
}
TLS.with_borrow_mut(|tls| {
tls.geometries.clear();
tls.geometries.extend(infos.iter().flat_map(|info| {
info.update_data.geometries.iter().map(
<&AccelerationStructureGeometry as Into<
vk::AccelerationStructureGeometryKHR,
>>::into,
)
}));
tls.max_primitive_counts.clear();
tls.max_primitive_counts
.extend(infos.iter().flat_map(|info| {
info.update_data
.geometries
.iter()
.map(|geometry| geometry.max_primitive_count)
}));
tls.range_bases.clear();
tls.range_strides.clear();
let (vk_infos, vk_max_primitive_counts) = {
let mut start = 0;
let mut vk_infos = Vec::with_capacity(infos.len());
let mut vk_max_primitive_counts = Vec::with_capacity(infos.len());
for info in infos {
let end = start + info.update_data.geometries.len();
vk_infos.push(
vk::AccelerationStructureBuildGeometryInfoKHR::default()
.ty(info.update_data.ty)
.flags(info.update_data.flags)
.mode(vk::BuildAccelerationStructureModeKHR::UPDATE)
.src_acceleration_structure(self.resource(info.src_accel_struct).handle)
.dst_acceleration_structure(self.resource(info.dst_accel_struct).handle)
.geometries(&tls.geometries[start..end])
.scratch_data(info.scratch_addr.into()),
);
vk_max_primitive_counts.push(&tls.max_primitive_counts[start..end]);
start = end;
tls.range_bases.push(info.range_base);
tls.range_strides.push(info.range_stride);
}
(vk_infos, vk_max_primitive_counts)
};
unsafe {
Device::expect_accel_struct_ext(&self.cmd.device)
.cmd_build_acceleration_structures_indirect(
self.cmd.handle,
&vk_infos,
&tls.range_bases,
&tls.range_strides,
&vk_max_primitive_counts,
);
}
});
self
}
/// Returns a borrow of the original Vulkan resource (buffer, image or acceleration structure)
/// which the given bound resource node represents.
pub fn resource<N>(&self, resource_node: N) -> &N::Resource
where
N: Node,
{
// You must have called an access function for this node on this execution before borrowing
// the resource!
//
// Why: Code that attempts to access this function is attempting to get access to the Vulkan
// resource (buffer, image, or acceleration structure). In order to access any resources the
// access type must first be specified so the correct barriers may be added.
//
// See: https://attackgoat.github.io/vk-graph/pipeline_sync.html
#[cfg(debug_assertions)]
assert!(
self.exec.accesses.contains_key(&resource_node.index()),
"unexpected node access: call an access function first"
);
resource_node.borrow(self.resources)
}
}
impl<'a> Deref for CommandRef<'a> {
type Target = crate::driver::cmd_buf::CommandBuffer;
fn deref(&self) -> &Self::Target {
self.cmd
}
}
/// Specifies the information and data used to build an acceleration structure.
///
/// See [`vkCmdBuildAccelerationStructuresKHR`](https://registry.khronos.org/vulkan/specs/latest/man/html/vkCmdBuildAccelerationStructuresKHR.html).
#[derive(Clone, Debug)]
pub struct BuildAccelerationStructureInfo {
/// The acceleration structure to be written.
pub accel_struct: AnyAccelerationStructureNode,
/// Specifies the geometry data to use when building the acceleration structure.
pub build_data: AccelerationStructureGeometryInfo<(
AccelerationStructureGeometry,
vk::AccelerationStructureBuildRangeInfoKHR,
)>,
/// The temporary buffer or host address (with enough capacity per
/// `AccelerationStructure::size_of`).
pub scratch_addr: DeviceOrHostAddress,
}
impl BuildAccelerationStructureInfo {
/// Constructs new acceleration structure build information.
pub fn new(
accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_addr: impl Into<DeviceOrHostAddress>,
build_data: AccelerationStructureGeometryInfo<(
AccelerationStructureGeometry,
vk::AccelerationStructureBuildRangeInfoKHR,
)>,
) -> Self {
let accel_struct = accel_struct.into();
let scratch_addr = scratch_addr.into();
Self {
accel_struct,
build_data,
scratch_addr,
}
}
}
/// Specifies the information and data used to build an acceleration structure with some parameters
/// sourced on the device.
///
/// See [`vkCmdBuildAccelerationStructuresKHR`](https://registry.khronos.org/vulkan/specs/latest/man/html/vkCmdBuildAccelerationStructuresKHR.html).
#[derive(Clone, Debug)]
pub struct BuildAccelerationStructureIndirectInfo {
/// The acceleration structure to be written.
pub accel_struct: AnyAccelerationStructureNode,
/// Specifies the geometry data to use when building the acceleration structure.
pub build_data: AccelerationStructureGeometryInfo<AccelerationStructureGeometry>,
/// A buffer device addresses which points to `data.geometry.len()`
/// [vk::AccelerationStructureBuildRangeInfoKHR] structures defining dynamic offsets to the
/// addresses where geometry data is stored.
pub range_base: vk::DeviceAddress,
/// Byte stride between elements of [`Self::range_base`].
pub range_stride: u32,
/// The temporary buffer or host address (with enough capacity per
/// `AccelerationStructure::size_of`).
pub scratch_data: DeviceOrHostAddress,
}
impl BuildAccelerationStructureIndirectInfo {
/// Constructs new acceleration structure indirect build information.
pub fn new(
accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_data: impl Into<DeviceOrHostAddress>,
build_data: AccelerationStructureGeometryInfo<AccelerationStructureGeometry>,
range_base: vk::DeviceAddress,
range_stride: u32,
) -> Self {
let accel_struct = accel_struct.into();
let scratch_data = scratch_data.into();
Self {
accel_struct,
build_data,
range_base,
range_stride,
scratch_data,
}
}
}
/// Specifies the information and data used to update an acceleration structure with some parameters
/// sourced on the device.
///
/// See [`vkCmdBuildAccelerationStructuresKHR`](https://registry.khronos.org/vulkan/specs/latest/man/html/vkCmdBuildAccelerationStructuresKHR.html).
#[derive(Clone, Debug)]
pub struct UpdateAccelerationStructureIndirectInfo {
/// The acceleration structure to be written.
pub dst_accel_struct: AnyAccelerationStructureNode,
/// A buffer device addresses which points to `data.geometry.len()`
/// [vk::AccelerationStructureBuildRangeInfoKHR] structures defining dynamic offsets to the
/// addresses where geometry data is stored.
pub range_base: vk::DeviceAddress,
/// Byte stride between elements of [`Self::range_base`].
pub range_stride: u32,
/// The temporary buffer or host address (with enough capacity per
/// `AccelerationStructure::size_of`).
pub scratch_addr: DeviceOrHostAddress,
/// The source acceleration structure to be read.
pub src_accel_struct: AnyAccelerationStructureNode,
/// Specifies the geometry data to use when building the acceleration structure.
pub update_data: AccelerationStructureGeometryInfo<AccelerationStructureGeometry>,
}
impl UpdateAccelerationStructureIndirectInfo {
/// Constructs new acceleration structure indirect update information.
pub fn new(
src_accel_struct: impl Into<AnyAccelerationStructureNode>,
dst_accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_addr: impl Into<DeviceOrHostAddress>,
update_data: AccelerationStructureGeometryInfo<AccelerationStructureGeometry>,
range_base: vk::DeviceAddress,
range_stride: u32,
) -> Self {
let src_accel_struct = src_accel_struct.into();
let dst_accel_struct = dst_accel_struct.into();
let scratch_addr = scratch_addr.into();
Self {
dst_accel_struct,
range_base,
range_stride,
scratch_addr,
src_accel_struct,
update_data,
}
}
}
/// Specifies the information and data used to update an acceleration structure.
#[derive(Clone, Debug)]
pub struct UpdateAccelerationStructureInfo {
/// The acceleration structure to be written.
pub dst_accel_struct: AnyAccelerationStructureNode,
/// The temporary buffer or host address (with enough capacity per
/// `AccelerationStructure::size_of`).
pub scratch_addr: DeviceOrHostAddress,
/// The source acceleration structure to be read.
pub src_accel_struct: AnyAccelerationStructureNode,
/// Specifies the geometry data to use when updating the acceleration structure.
pub update_data: AccelerationStructureGeometryInfo<(
AccelerationStructureGeometry,
vk::AccelerationStructureBuildRangeInfoKHR,
)>,
}
impl UpdateAccelerationStructureInfo {
/// Constructs new acceleration structure update information.
pub fn new(
src_accel_struct: impl Into<AnyAccelerationStructureNode>,
dst_accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_addr: impl Into<DeviceOrHostAddress>,
update_data: AccelerationStructureGeometryInfo<(
AccelerationStructureGeometry,
vk::AccelerationStructureBuildRangeInfoKHR,
)>,
) -> Self {
let src_accel_struct = src_accel_struct.into();
let dst_accel_struct = dst_accel_struct.into();
let scratch_addr = scratch_addr.into();
Self {
dst_accel_struct,
scratch_addr,
src_accel_struct,
update_data,
}
}
}
#[allow(missing_docs)]
#[allow(deprecated)]
mod deprecated {
use ash::vk;
use crate::{
cmd::{
BuildAccelerationStructureIndirectInfo, BuildAccelerationStructureInfo, CommandRef,
UpdateAccelerationStructureIndirectInfo, UpdateAccelerationStructureInfo,
},
driver::accel_struct::{
AccelerationStructureGeometry, AccelerationStructureGeometryInfo, DeviceOrHostAddress,
},
graph::pass_ref::{
AccelerationStructureBuildInfo, AccelerationStructureIndirectBuildInfo,
AccelerationStructureIndirectUpdateInfo, AccelerationStructureUpdateInfo,
},
node::AnyAccelerationStructureNode,
};
impl<'a> CommandRef<'a> {
#[deprecated = "use build_accel_struct function"]
#[doc(hidden)]
pub fn build_structure(
&self,
info: &AccelerationStructureGeometryInfo<(
AccelerationStructureGeometry,
vk::AccelerationStructureBuildRangeInfoKHR,
)>,
accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_addr: impl Into<DeviceOrHostAddress>,
) -> &Self {
self.build_accel_struct(&[BuildAccelerationStructureInfo {
accel_struct: accel_struct.into(),
build_data: info.clone(),
scratch_addr: scratch_addr.into(),
}])
}
#[deprecated = "use build_accel_struct_indirect function"]
#[doc(hidden)]
pub fn build_structure_indirect(
&self,
info: &AccelerationStructureGeometryInfo<AccelerationStructureGeometry>,
accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_addr: impl Into<DeviceOrHostAddress>,
range_base: vk::DeviceAddress,
range_stride: u32,
) -> &Self {
self.build_accel_struct_indirect(&[BuildAccelerationStructureIndirectInfo {
accel_struct: accel_struct.into(),
build_data: info.clone(),
range_base,
range_stride,
scratch_data: scratch_addr.into(),
}])
}
#[deprecated = "use build_accel_struct function"]
#[doc(hidden)]
pub fn build_structures(&self, infos: &[AccelerationStructureBuildInfo]) -> &Self {
for info in infos {
self.build_structure(&info.build_data, info.accel_struct, info.scratch_addr);
}
self
}
#[deprecated = "use build_accel_struct_indirect function"]
#[doc(hidden)]
pub fn build_structures_indirect(
&self,
infos: &[AccelerationStructureIndirectBuildInfo],
) -> &Self {
for info in infos {
self.build_structure_indirect(
&info.build_data,
info.accel_struct,
info.scratch_data,
info.range_base,
info.range_stride,
);
}
self
}
#[deprecated = "use update_accel_struct function"]
#[doc(hidden)]
pub fn update_structure(
&self,
info: &AccelerationStructureGeometryInfo<(
AccelerationStructureGeometry,
vk::AccelerationStructureBuildRangeInfoKHR,
)>,
src_accel_struct: impl Into<AnyAccelerationStructureNode>,
dst_accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_addr: impl Into<DeviceOrHostAddress>,
) -> &Self {
self.update_accel_struct(&[UpdateAccelerationStructureInfo {
src_accel_struct: src_accel_struct.into(),
dst_accel_struct: dst_accel_struct.into(),
update_data: info.clone(),
scratch_addr: scratch_addr.into(),
}])
}
#[deprecated = "use update_accel_struct_indirect function"]
#[doc(hidden)]
pub fn update_structure_indirect(
&self,
info: &AccelerationStructureGeometryInfo<AccelerationStructureGeometry>,
src_accel_struct: impl Into<AnyAccelerationStructureNode>,
dst_accel_struct: impl Into<AnyAccelerationStructureNode>,
scratch_addr: impl Into<DeviceOrHostAddress>,
range_base: vk::DeviceAddress,
range_stride: u32,
) -> &Self {
self.update_accel_struct_indirect(&[UpdateAccelerationStructureIndirectInfo {
src_accel_struct: src_accel_struct.into(),
dst_accel_struct: dst_accel_struct.into(),
update_data: info.clone(),
range_base,
range_stride,
scratch_addr: scratch_addr.into(),
}])
}
#[deprecated = "use update_accel_struct function"]
#[doc(hidden)]
pub fn update_structures(&self, infos: &[AccelerationStructureUpdateInfo]) -> &Self {
for info in infos {
self.update_structure(
&info.update_data,
info.src_accel_struct,
info.dst_accel_struct,
info.scratch_addr,
);
}
self
}
#[deprecated = "use update_accel_struct_indirect function"]
#[doc(hidden)]
pub fn update_structures_indirect(
&self,
infos: &[AccelerationStructureIndirectUpdateInfo],
) -> &Self {
for info in infos {
self.update_structure_indirect(
&info.update_data,
info.src_accel_struct,
info.dst_accel_struct,
info.scratch_addr,
info.range_base,
info.range_stride,
);
}
self
}
}
}