Skip to content

Commit cb16fd0

Browse files
Vecvecdavnotdev
authored andcommitted
[wgpu-hal] Blas compaction (gfx-rs#7101)
1 parent 26054a3 commit cb16fd0

File tree

15 files changed

+314
-2
lines changed

15 files changed

+314
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Bottom level categories:
5050

5151
#### General
5252

53+
- Support BLAS compaction in wgpu-hal. By @Vecvec in [#7101](https://github.com/gfx-rs/wgpu/pull/7101).
5354
- Avoid using default features in many dependencies, etc. By Brody in [#7031](https://github.com/gfx-rs/wgpu/pull/7031)
5455
- Use `hashbrown` to simplify no-std support. By Brody in [#6938](https://github.com/gfx-rs/wgpu/pull/6938) & [#6925](https://github.com/gfx-rs/wgpu/pull/6925).
5556
- If you use Binding Arrays in a bind group, you may not use Dynamic Offset Buffers or Uniform Buffers in that bind group. By @cwfitzgerald in [#6811](https://github.com/gfx-rs/wgpu/pull/6811)

wgpu-core/src/device/ray_tracing.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ impl Device {
9898
label: blas_desc.label.as_deref(),
9999
size: size_info.acceleration_structure_size,
100100
format: hal::AccelerationStructureFormat::BottomLevel,
101+
// change this once compaction is implemented in wgpu-core
102+
allow_compaction: false,
101103
})
102104
}
103105
.map_err(DeviceError::from_hal)?;
@@ -158,6 +160,7 @@ impl Device {
158160
label: desc.label.as_deref(),
159161
size: size_info.acceleration_structure_size,
160162
format: hal::AccelerationStructureFormat::TopLevel,
163+
allow_compaction: false,
161164
})
162165
}
163166
.map_err(DeviceError::from_hal)?;

wgpu-hal/examples/ray-traced-triangle/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ impl<A: hal::Api> Example<A> {
525525
label: Some("blas"),
526526
size: blas_sizes.acceleration_structure_size,
527527
format: hal::AccelerationStructureFormat::BottomLevel,
528+
allow_compaction: false,
528529
})
529530
}
530531
.unwrap();
@@ -534,6 +535,7 @@ impl<A: hal::Api> Example<A> {
534535
label: Some("tlas"),
535536
size: tlas_sizes.acceleration_structure_size,
536537
format: hal::AccelerationStructureFormat::TopLevel,
538+
allow_compaction: false,
537539
})
538540
}
539541
.unwrap();

wgpu-hal/src/dx12/command.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ impl crate::CommandEncoder for super::CommandEncoder {
383383
},
384384
};
385385
self.temp.barriers.push(raw);
386-
} else if barrier.usage.from == wgt::BufferUses::STORAGE_READ_WRITE {
386+
} else if barrier.usage.from == wgt::BufferUses::STORAGE_READ_WRITE
387+
|| barrier.usage.from == wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY
388+
{
387389
let raw = Direct3D12::D3D12_RESOURCE_BARRIER {
388390
Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV,
389391
Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,
@@ -681,6 +683,29 @@ impl crate::CommandEncoder for super::CommandEncoder {
681683
)
682684
};
683685
}
686+
unsafe fn read_acceleration_structure_compact_size(
687+
&mut self,
688+
acceleration_structure: &super::AccelerationStructure,
689+
buf: &super::Buffer,
690+
) {
691+
let list = self
692+
.list
693+
.as_ref()
694+
.unwrap()
695+
.cast::<Direct3D12::ID3D12GraphicsCommandList4>()
696+
.unwrap();
697+
unsafe {
698+
list.EmitRaytracingAccelerationStructurePostbuildInfo(
699+
&Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC {
700+
DestBuffer: buf.resource.GetGPUVirtualAddress(),
701+
InfoType: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE,
702+
},
703+
&[
704+
acceleration_structure.resource.GetGPUVirtualAddress()
705+
],
706+
)
707+
}
708+
}
684709
unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) {
685710
// nothing to do here
686711
}
@@ -1505,4 +1530,25 @@ impl crate::CommandEncoder for super::CommandEncoder {
15051530
}])
15061531
}
15071532
}
1533+
1534+
unsafe fn copy_acceleration_structure_to_acceleration_structure(
1535+
&mut self,
1536+
src: &super::AccelerationStructure,
1537+
dst: &super::AccelerationStructure,
1538+
copy: wgt::AccelerationStructureCopy,
1539+
) {
1540+
let list = self
1541+
.list
1542+
.as_ref()
1543+
.unwrap()
1544+
.cast::<Direct3D12::ID3D12GraphicsCommandList4>()
1545+
.unwrap();
1546+
unsafe {
1547+
list.CopyRaytracingAccelerationStructure(
1548+
dst.resource.GetGPUVirtualAddress(),
1549+
src.resource.GetGPUVirtualAddress(),
1550+
conv::map_acceleration_structure_copy_mode(copy),
1551+
)
1552+
}
1553+
}
15081554
}

wgpu-hal/src/dx12/conv.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ pub fn map_buffer_usage_to_resource_flags(
44
usage: wgt::BufferUses,
55
) -> Direct3D12::D3D12_RESOURCE_FLAGS {
66
let mut flags = Direct3D12::D3D12_RESOURCE_FLAG_NONE;
7-
if usage.contains(wgt::BufferUses::STORAGE_READ_WRITE) {
7+
if usage.contains(wgt::BufferUses::STORAGE_READ_WRITE)
8+
|| usage.contains(wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY)
9+
{
810
flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
911
}
1012
flags
@@ -141,6 +143,9 @@ pub fn map_buffer_usage_to_state(usage: wgt::BufferUses) -> Direct3D12::D3D12_RE
141143
if usage.intersects(Bu::INDIRECT) {
142144
state |= Direct3D12::D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
143145
}
146+
if usage.intersects(Bu::ACCELERATION_STRUCTURE_QUERY) {
147+
state |= Direct3D12::D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
148+
}
144149
state
145150
}
146151

@@ -398,3 +403,16 @@ pub(crate) fn map_acceleration_structure_geometry_flags(
398403
}
399404
d3d_flags
400405
}
406+
407+
pub(crate) fn map_acceleration_structure_copy_mode(
408+
mode: wgt::AccelerationStructureCopy,
409+
) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE {
410+
match mode {
411+
wgt::AccelerationStructureCopy::Clone => {
412+
Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE
413+
}
414+
wgt::AccelerationStructureCopy::Compact => {
415+
Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT
416+
}
417+
}
418+
}

wgpu-hal/src/dynamic/command.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ pub trait DynCommandEncoder: DynResource + std::fmt::Debug {
179179
&mut self,
180180
barrier: AccelerationStructureBarrier,
181181
);
182+
183+
unsafe fn copy_acceleration_structure_to_acceleration_structure(
184+
&mut self,
185+
src: &dyn DynAccelerationStructure,
186+
dst: &dyn DynAccelerationStructure,
187+
copy: wgt::AccelerationStructureCopy,
188+
);
189+
unsafe fn read_acceleration_structure_compact_size(
190+
&mut self,
191+
acceleration_structure: &dyn DynAccelerationStructure,
192+
buf: &dyn DynBuffer,
193+
);
182194
}
183195

184196
impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
@@ -611,6 +623,26 @@ impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
611623
) {
612624
unsafe { C::place_acceleration_structure_barrier(self, barrier) };
613625
}
626+
627+
unsafe fn copy_acceleration_structure_to_acceleration_structure(
628+
&mut self,
629+
src: &dyn DynAccelerationStructure,
630+
dst: &dyn DynAccelerationStructure,
631+
copy: wgt::AccelerationStructureCopy,
632+
) {
633+
let src = src.expect_downcast_ref();
634+
let dst = dst.expect_downcast_ref();
635+
unsafe { C::copy_acceleration_structure_to_acceleration_structure(self, src, dst, copy) };
636+
}
637+
unsafe fn read_acceleration_structure_compact_size(
638+
&mut self,
639+
acceleration_structure: &dyn DynAccelerationStructure,
640+
buf: &dyn DynBuffer,
641+
) {
642+
let acceleration_structure = acceleration_structure.expect_downcast_ref();
643+
let buf = buf.expect_downcast_ref();
644+
unsafe { C::read_acceleration_structure_compact_size(self, acceleration_structure, buf) }
645+
}
614646
}
615647

616648
impl<'a> PassTimestampWrites<'a, dyn DynQuerySet> {

wgpu-hal/src/empty.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ impl crate::CommandEncoder for Encoder {
378378
unsafe fn begin_query(&mut self, set: &Resource, index: u32) {}
379379
unsafe fn end_query(&mut self, set: &Resource, index: u32) {}
380380
unsafe fn write_timestamp(&mut self, set: &Resource, index: u32) {}
381+
unsafe fn read_acceleration_structure_compact_size(
382+
&mut self,
383+
acceleration_structure: &Resource,
384+
buf: &Resource,
385+
) {
386+
}
381387
unsafe fn reset_queries(&mut self, set: &Resource, range: Range<u32>) {}
382388
unsafe fn copy_query_results(
383389
&mut self,
@@ -510,4 +516,12 @@ impl crate::CommandEncoder for Encoder {
510516
_barriers: crate::AccelerationStructureBarrier,
511517
) {
512518
}
519+
520+
unsafe fn copy_acceleration_structure_to_acceleration_structure(
521+
&mut self,
522+
src: &Resource,
523+
dst: &Resource,
524+
copy: wgt::AccelerationStructureCopy,
525+
) {
526+
}
513527
}

wgpu-hal/src/gles/command.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,4 +1209,21 @@ impl crate::CommandEncoder for super::CommandEncoder {
12091209
) {
12101210
unimplemented!()
12111211
}
1212+
1213+
unsafe fn copy_acceleration_structure_to_acceleration_structure(
1214+
&mut self,
1215+
_src: &super::AccelerationStructure,
1216+
_dst: &super::AccelerationStructure,
1217+
_copy: wgt::AccelerationStructureCopy,
1218+
) {
1219+
unimplemented!()
1220+
}
1221+
1222+
unsafe fn read_acceleration_structure_compact_size(
1223+
&mut self,
1224+
_acceleration_structure: &super::AccelerationStructure,
1225+
_buf: &super::Buffer,
1226+
) {
1227+
unimplemented!()
1228+
}
12121229
}

wgpu-hal/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,12 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
12491249
) where
12501250
T: Iterator<Item = BufferTextureCopy>;
12511251

1252+
unsafe fn copy_acceleration_structure_to_acceleration_structure(
1253+
&mut self,
1254+
src: &<Self::A as Api>::AccelerationStructure,
1255+
dst: &<Self::A as Api>::AccelerationStructure,
1256+
copy: wgt::AccelerationStructureCopy,
1257+
);
12521258
// pass common
12531259

12541260
/// Sets the bind group at `index` to `group`.
@@ -1509,6 +1515,12 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
15091515
&mut self,
15101516
barrier: AccelerationStructureBarrier,
15111517
);
1518+
// modeled off dx12, because this is able to be polyfilled in vulkan as opposed to the other way round
1519+
unsafe fn read_acceleration_structure_compact_size(
1520+
&mut self,
1521+
acceleration_structure: &<Self::A as Api>::AccelerationStructure,
1522+
buf: &<Self::A as Api>::Buffer,
1523+
);
15121524
}
15131525

15141526
bitflags!(
@@ -2311,6 +2323,7 @@ pub struct AccelerationStructureDescriptor<'a> {
23112323
pub label: Label<'a>,
23122324
pub size: wgt::BufferAddress,
23132325
pub format: AccelerationStructureFormat,
2326+
pub allow_compaction: bool,
23142327
}
23152328

23162329
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -2397,6 +2410,11 @@ pub struct AccelerationStructureAABBs<'a, B: DynBuffer + ?Sized> {
23972410
pub flags: AccelerationStructureGeometryFlags,
23982411
}
23992412

2413+
pub struct AccelerationStructureCopy {
2414+
pub copy_flags: wgt::AccelerationStructureCopy,
2415+
pub type_flags: wgt::AccelerationStructureType,
2416+
}
2417+
24002418
/// * `offset` - offset in bytes
24012419
#[derive(Clone, Debug)]
24022420
pub struct AccelerationStructureInstances<'a, B: DynBuffer + ?Sized> {
@@ -2433,6 +2451,12 @@ bitflags::bitflags! {
24332451
const BUILD_OUTPUT = 1 << 1;
24342452
// Tlas used in a shader
24352453
const SHADER_INPUT = 1 << 2;
2454+
// Blas used to query compacted size
2455+
const QUERY_INPUT = 1 << 3;
2456+
// BLAS used as a src for a copy operation
2457+
const COPY_SRC = 1 << 4;
2458+
// BLAS used as a dst for a copy operation
2459+
const COPY_DST = 1 << 5;
24362460
}
24372461
}
24382462

wgpu-hal/src/metal/command.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,15 @@ impl crate::CommandEncoder for super::CommandEncoder {
392392
}
393393
}
394394

395+
unsafe fn copy_acceleration_structure_to_acceleration_structure(
396+
&mut self,
397+
_src: &super::AccelerationStructure,
398+
_dst: &super::AccelerationStructure,
399+
_copy: wgt::AccelerationStructureCopy,
400+
) {
401+
unimplemented!()
402+
}
403+
395404
unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) {
396405
match set.ty {
397406
wgt::QueryType::Occlusion => {
@@ -1292,6 +1301,14 @@ impl crate::CommandEncoder for super::CommandEncoder {
12921301
) {
12931302
unimplemented!()
12941303
}
1304+
1305+
unsafe fn read_acceleration_structure_compact_size(
1306+
&mut self,
1307+
_acceleration_structure: &super::AccelerationStructure,
1308+
_buf: &super::Buffer,
1309+
) {
1310+
unimplemented!()
1311+
}
12951312
}
12961313

12971314
impl Drop for super::CommandEncoder {

0 commit comments

Comments
 (0)