Skip to content

Commit

Permalink
rt: abstract away the buffer backing of acceleration structures
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Feb 24, 2023
1 parent 99d56b8 commit dd63f02
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
5 changes: 0 additions & 5 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub mod limits {
pub const PLAIN_DATA_SIZE: u32 = 256;
pub const RESOURCES_IN_GROUP: u32 = 8;
pub const STORAGE_BUFFER_ALIGNMENT: u64 = 256;
pub const ACCELERATION_STRUCTURE_BUFFER_ALIGNMENT: u64 = 256;
pub const ACCELERATION_STRUCTURE_SCRATCH_ALIGNMENT: u64 = 256;
}

Expand Down Expand Up @@ -349,10 +348,6 @@ pub enum AccelerationStructureType {
pub struct AccelerationStructureDesc<'a> {
pub name: &'a str,
pub ty: AccelerationStructureType,
pub buffer: Buffer,
/// Offset into the buffer where AS is placed.
/// Must be a multiple of `limits::ACCELERATION_STRUCTURE_BUFFER_ALIGNMENT`.
pub offset: u64,
pub size: u64,
}

Expand Down
2 changes: 2 additions & 0 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ pub struct Sampler {
#[derive(Clone, Copy, Debug, Hash, PartialEq)]
pub struct AccelerationStructure {
raw: vk::AccelerationStructureKHR,
buffer: vk::Buffer,
memory_handle: usize,
}

#[derive(Debug, Default)]
Expand Down
39 changes: 33 additions & 6 deletions blade-graphics/src/vulkan/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ impl super::Context {
&self,
desc: crate::AccelerationStructureDesc,
) -> super::AccelerationStructure {
let buffer_info = vk::BufferCreateInfo::builder()
.size(desc.size)
.usage(vk::BufferUsageFlags::ACCELERATION_STRUCTURE_STORAGE_KHR)
.sharing_mode(vk::SharingMode::EXCLUSIVE);

let buffer = unsafe { self.device.core.create_buffer(&buffer_info, None).unwrap() };
let requirements = unsafe { self.device.core.get_buffer_memory_requirements(buffer) };
let allocation = self.allocate_memory(requirements, crate::Memory::Device);

unsafe {
self.device
.core
.bind_buffer_memory(buffer, allocation.memory, allocation.offset)
.unwrap()
};

let raw_ty = match desc.ty {
crate::AccelerationStructureType::TopLevel => {
vk::AccelerationStructureTypeKHR::TOP_LEVEL
Expand All @@ -186,8 +202,7 @@ impl super::Context {
};
let vk_info = vk::AccelerationStructureCreateInfoKHR::builder()
.ty(raw_ty)
.buffer(desc.buffer.raw)
.offset(desc.offset)
.buffer(buffer)
.size(desc.size);

let rt = self.device.ray_tracing.as_ref().unwrap();
Expand All @@ -196,7 +211,16 @@ impl super::Context {
.create_acceleration_structure(&vk_info, None)
.unwrap()
};
super::AccelerationStructure { raw }

if !desc.name.is_empty() {
self.set_object_name(vk::ObjectType::BUFFER, buffer, desc.name);
self.set_object_name(vk::ObjectType::ACCELERATION_STRUCTURE_KHR, raw, desc.name);
}
super::AccelerationStructure {
raw,
buffer,
memory_handle: allocation.handle,
}
}

pub fn destroy_acceleration_structure(
Expand All @@ -207,7 +231,11 @@ impl super::Context {
unsafe {
rt.acceleration_structure
.destroy_acceleration_structure(acceleration_structure.raw, None);
self.device
.core
.destroy_buffer(acceleration_structure.buffer, None);
}
self.free_memory(acceleration_structure.memory_handle);
}
}

Expand All @@ -232,9 +260,8 @@ impl crate::traits::ResourceDevice for super::Context {
)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
if self.device.ray_tracing.is_some() {
vk_info.usage |= Buf::SHADER_DEVICE_ADDRESS
| Buf::ACCELERATION_STRUCTURE_STORAGE_KHR
| Buf::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR;
vk_info.usage |=
Buf::SHADER_DEVICE_ADDRESS | Buf::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR;
}

let raw = unsafe { self.device.core.create_buffer(&vk_info, None).unwrap() };
Expand Down
21 changes: 1 addition & 20 deletions examples/ray-trace/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ struct ShaderData {
struct Example {
start_time: time::Instant,
blas: blade::AccelerationStructure,
blas_buffer: blade::Buffer,
tlas: blade::AccelerationStructure,
tlas_buffer: blade::Buffer,
pipeline: blade::RenderPipeline,
command_encoder: blade::CommandEncoder,
prev_sync_point: Option<blade::SyncPoint>,
Expand Down Expand Up @@ -122,16 +120,9 @@ impl Example {
is_opaque: true,
}];
let blas_sizes = context.get_bottom_level_acceleration_structure_sizes(&meshes);
let blas_buffer = context.create_buffer(blade::BufferDesc {
name: "BLAS",
size: blas_sizes.data,
memory: blade::Memory::Device,
});
let blas = context.create_acceleration_structure(blade::AccelerationStructureDesc {
name: "triangle",
ty: blade::AccelerationStructureType::BottomLevel,
buffer: blas_buffer,
offset: 0,
size: blas_sizes.data,
});

Expand Down Expand Up @@ -162,16 +153,9 @@ impl Example {
];
let tlas_sizes = context.get_top_level_acceleration_structure_sizes(instances.len() as u32);
let instance_buffer = context.create_acceleration_structure_instance_buffer(&instances);
let tlas_buffer = context.create_buffer(blade::BufferDesc {
name: "TLAS",
size: tlas_sizes.data,
memory: blade::Memory::Device,
});
let tlas = context.create_acceleration_structure(blade::AccelerationStructureDesc {
name: "TLAS",
ty: blade::AccelerationStructureType::TopLevel,
buffer: tlas_buffer,
offset: 0,
size: tlas_sizes.data,
});
let tlas_scratch_offset = (blas_sizes.scratch
Expand Down Expand Up @@ -211,9 +195,7 @@ impl Example {
Self {
start_time: time::Instant::now(),
blas,
blas_buffer,
tlas,
tlas_buffer,
pipeline,
command_encoder,
prev_sync_point: None,
Expand All @@ -227,8 +209,7 @@ impl Example {
self.context.wait_for(&sp, !0);
}
self.context.destroy_acceleration_structure(self.blas);
self.context.destroy_buffer(self.blas_buffer);
self.context.destroy_buffer(self.tlas_buffer);
self.context.destroy_acceleration_structure(self.tlas);
}

fn render(&mut self) {
Expand Down

0 comments on commit dd63f02

Please sign in to comment.