Skip to content

Commit

Permalink
Move acceleration structure logic under the traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Feb 24, 2023
1 parent dd63f02 commit f135d8a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 103 deletions.
7 changes: 4 additions & 3 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,9 @@ impl crate::traits::TransferEncoder for super::PassEncoder<'_, ()> {
}
}

impl super::PassEncoder<'_, ()> {
pub fn build_bottom_level(
#[hidden_trait::expose]
impl crate::traits::AccelerationStructureEncoder for super::PassEncoder<'_, ()> {
fn build_bottom_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_meshes: &[crate::AccelerationStructureMesh],
Expand All @@ -314,7 +315,7 @@ impl super::PassEncoder<'_, ()> {
unimplemented!()
}

pub fn build_top_level(
fn build_top_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_instance_count: u32,
Expand Down
29 changes: 15 additions & 14 deletions blade-graphics/src/gles/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ impl super::Context {
) -> super::Buffer {
unimplemented!()
}

pub fn create_acceleration_structure(
&self,
_desc: crate::AccelerationStructureDesc,
) -> super::AccelerationStructure {
unimplemented!()
}

pub fn destroy_acceleration_structure(
&self,
_acceleration_structure: super::AccelerationStructure,
) {
unimplemented!()
}
}

#[hidden_trait::expose]
Expand All @@ -44,6 +30,7 @@ impl crate::traits::ResourceDevice for super::Context {
type Texture = super::Texture;
type TextureView = super::TextureView;
type Sampler = super::Sampler;
type AccelerationStructure = super::AccelerationStructure;

fn create_buffer(&self, desc: crate::BufferDesc) -> super::Buffer {
let gl = self.lock();
Expand Down Expand Up @@ -315,6 +302,20 @@ impl crate::traits::ResourceDevice for super::Context {
let gl = self.lock();
unsafe { gl.delete_sampler(sampler.raw) };
}

fn create_acceleration_structure(
&self,
_desc: crate::AccelerationStructureDesc,
) -> super::AccelerationStructure {
unimplemented!()
}

fn destroy_acceleration_structure(
&self,
_acceleration_structure: super::AccelerationStructure,
) {
unimplemented!()
}
}

fn map_filter_modes(
Expand Down
10 changes: 6 additions & 4 deletions blade-graphics/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,11 @@ impl Drop for super::TransferCommandEncoder<'_> {
}
}

impl<'a> super::AccelerationStructureCommandEncoder<'a> {
//TODO: move into the trait
pub fn build_bottom_level(
#[hidden_trait::expose]
impl crate::traits::AccelerationStructureEncoder
for super::AccelerationStructureCommandEncoder<'_>
{
fn build_bottom_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_meshes: &[crate::AccelerationStructureMesh],
Expand All @@ -312,7 +314,7 @@ impl<'a> super::AccelerationStructureCommandEncoder<'a> {
unimplemented!()
}

pub fn build_top_level(
fn build_top_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_instance_count: u32,
Expand Down
29 changes: 15 additions & 14 deletions blade-graphics/src/metal/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,6 @@ impl super::Context {
) -> super::Buffer {
unimplemented!()
}

pub fn create_acceleration_structure(
&self,
_desc: crate::AccelerationStructureDesc,
) -> super::AccelerationStructure {
unimplemented!()
}

pub fn destroy_acceleration_structure(
&self,
_acceleration_structure: super::AccelerationStructure,
) {
unimplemented!()
}
}

#[hidden_trait::expose]
Expand All @@ -107,6 +93,7 @@ impl crate::traits::ResourceDevice for super::Context {
type Texture = super::Texture;
type TextureView = super::TextureView;
type Sampler = super::Sampler;
type AccelerationStructure = super::AccelerationStructure;

fn create_buffer(&self, desc: crate::BufferDesc) -> super::Buffer {
let options = match desc.memory {
Expand Down Expand Up @@ -277,4 +264,18 @@ impl crate::traits::ResourceDevice for super::Context {
let () = msg_send![sampler.raw, release];
}
}

fn create_acceleration_structure(
&self,
_desc: crate::AccelerationStructureDesc,
) -> super::AccelerationStructure {
unimplemented!()
}

fn destroy_acceleration_structure(
&self,
_acceleration_structure: super::AccelerationStructure,
) {
unimplemented!()
}
}
23 changes: 23 additions & 0 deletions blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub trait ResourceDevice {
type Texture: Clone + Copy + Debug + Hash + PartialEq;
type TextureView: Clone + Copy + Debug + Hash + PartialEq;
type Sampler: Clone + Copy + Debug + Hash + PartialEq;
type AccelerationStructure: Clone + Copy + Debug + Hash + PartialEq;

fn create_buffer(&self, desc: super::BufferDesc) -> Self::Buffer;
fn sync_buffer(&self, buffer: Self::Buffer);
Expand All @@ -14,6 +15,11 @@ pub trait ResourceDevice {
fn destroy_texture_view(&self, view: Self::TextureView);
fn create_sampler(&self, desc: super::SamplerDesc) -> Self::Sampler;
fn destroy_sampler(&self, sampler: Self::Sampler);
fn create_acceleration_structure(
&self,
desc: super::AccelerationStructureDesc,
) -> Self::AccelerationStructure;
fn destroy_acceleration_structure(&self, acceleration_structure: Self::AccelerationStructure);
}

pub trait CommandDevice {
Expand Down Expand Up @@ -58,6 +64,23 @@ pub trait TransferEncoder {
);
}

pub trait AccelerationStructureEncoder {
fn build_bottom_level(
&mut self,
acceleration_structure: crate::AccelerationStructure,
meshes: &[super::AccelerationStructureMesh],
scratch_data: super::BufferPiece,
);

fn build_top_level(
&mut self,
acceleration_structure: crate::AccelerationStructure,
instance_count: u32,
instance_data: super::BufferPiece,
scratch_data: super::BufferPiece,
);
}

pub trait PipelineEncoder {
fn bind<D: super::ShaderData>(&mut self, group: u32, data: &D);
}
Expand Down
6 changes: 4 additions & 2 deletions blade-graphics/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,10 @@ impl crate::traits::TransferEncoder for super::TransferCommandEncoder<'_> {
}
}

impl<'a> super::AccelerationStructureCommandEncoder<'a> {
//TODO: move into the trait
#[hidden_trait::expose]
impl crate::traits::AccelerationStructureEncoder
for super::AccelerationStructureCommandEncoder<'_>
{
pub fn build_bottom_level(
&mut self,
acceleration_structure: super::AccelerationStructure,
Expand Down
131 changes: 65 additions & 66 deletions blade-graphics/src/vulkan/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl super::Context {
}
}

//TODO: move these into `ResourceDevice` trait when ready
pub fn get_bottom_level_acceleration_structure_sizes(
&self,
meshes: &[crate::AccelerationStructureMesh],
Expand Down Expand Up @@ -171,72 +172,6 @@ impl super::Context {
}
buffer
}

pub fn create_acceleration_structure(
&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
}
crate::AccelerationStructureType::BottomLevel => {
vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL
}
};
let vk_info = vk::AccelerationStructureCreateInfoKHR::builder()
.ty(raw_ty)
.buffer(buffer)
.size(desc.size);

let rt = self.device.ray_tracing.as_ref().unwrap();
let raw = unsafe {
rt.acceleration_structure
.create_acceleration_structure(&vk_info, None)
.unwrap()
};

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(
&self,
acceleration_structure: super::AccelerationStructure,
) {
let rt = self.device.ray_tracing.as_ref().unwrap();
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);
}
}

#[hidden_trait::expose]
Expand All @@ -245,6 +180,7 @@ impl crate::traits::ResourceDevice for super::Context {
type Texture = super::Texture;
type TextureView = super::TextureView;
type Sampler = super::Sampler;
type AccelerationStructure = super::AccelerationStructure;

fn create_buffer(&self, desc: crate::BufferDesc) -> super::Buffer {
use vk::BufferUsageFlags as Buf;
Expand Down Expand Up @@ -407,6 +343,69 @@ impl crate::traits::ResourceDevice for super::Context {
fn destroy_sampler(&self, sampler: super::Sampler) {
unsafe { self.device.core.destroy_sampler(sampler.raw, None) };
}

fn create_acceleration_structure(
&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
}
crate::AccelerationStructureType::BottomLevel => {
vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL
}
};
let vk_info = vk::AccelerationStructureCreateInfoKHR::builder()
.ty(raw_ty)
.buffer(buffer)
.size(desc.size);

let rt = self.device.ray_tracing.as_ref().unwrap();
let raw = unsafe {
rt.acceleration_structure
.create_acceleration_structure(&vk_info, None)
.unwrap()
};

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,
}
}

fn destroy_acceleration_structure(&self, acceleration_structure: super::AccelerationStructure) {
let rt = self.device.ray_tracing.as_ref().unwrap();
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);
}
}

fn map_texture_dimension(dimension: crate::TextureDimension) -> vk::ImageType {
Expand Down

0 comments on commit f135d8a

Please sign in to comment.