Skip to content

Commit

Permalink
acceleration structure: Implement adding triangles
Browse files Browse the repository at this point in the history
  • Loading branch information
bwrsandman committed Apr 28, 2020
1 parent ebe867f commit 87701eb
Showing 1 changed file with 100 additions and 27 deletions.
127 changes: 100 additions & 27 deletions vulkano/src/acceleration_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct AccelerationStructure {
device: Arc<Device>,
nv_extension: bool,
top_level: Level,
aabb_bottom_level: Level,
bottom_level: Level,

// Memory allocated for the acceleration structure.
memory: PotentialDedicatedAllocation<StdMemoryPoolAlloc>,
Expand Down Expand Up @@ -90,7 +90,7 @@ impl AccelerationStructure {

#[inline]
pub fn bottom_level(&self) -> Level {
self.aabb_bottom_level.clone()
self.bottom_level.clone()
}

#[inline]
Expand All @@ -104,12 +104,21 @@ impl AccelerationStructure {
}
}

struct AccelerationStructureBuilderTriangles {
vertex_buffer: Arc<dyn BufferAccess + Send + Sync>,
vertex_stride: DeviceSize,
vertex_format: Format,
index_buffer: Arc<dyn BufferAccess + Send + Sync>,
index_type: IndexType,
}

/// Prototype of a `AccelerationStructure`.
///
/// See the docs of `AccelerationStructure` for an example.
pub struct AccelerationStructureBuilder {
nv_extension: bool,
// TODO: Associate `BuildAccelerationStructureFlags`
triangles: Vec<AccelerationStructureBuilderTriangles>,
aabbs: Vec<Box<dyn BufferAccess + Send + Sync>>,
}

Expand All @@ -118,6 +127,7 @@ impl AccelerationStructureBuilder {
pub fn nv() -> Self {
AccelerationStructureBuilder {
nv_extension: true,
triangles: vec![],
aabbs: vec![],
}
}
Expand All @@ -126,6 +136,7 @@ impl AccelerationStructureBuilder {
pub fn khr() -> Self {
AccelerationStructureBuilder {
nv_extension: false,
triangles: vec![],
aabbs: vec![],
}
}
Expand Down Expand Up @@ -187,9 +198,48 @@ impl AccelerationStructureBuilder {
flags: vk::GEOMETRY_OPAQUE_BIT_NV, // TODO
}
})
.chain(self.triangles.into_iter().map(|ref triangle| {
let index_stride = match triangle.index_type {
U16 => size_of::<u16>(),
U32 => size_of::<u32>(),
};
let geometry = vk::GeometryTrianglesNV {
sType: vk::STRUCTURE_TYPE_GEOMETRY_TRIANGLES_NV,
pNext: ptr::null(),
vertexData: triangle.vertex_buffer.inner().buffer.internal_object(),
vertexOffset: triangle.vertex_buffer.inner().offset as DeviceSize,
vertexCount: (triangle.vertex_buffer.size() / triangle.vertex_stride as usize)
as u32,
vertexStride: triangle.vertex_stride,
vertexFormat: triangle.vertex_format as u32,
indexData: triangle.index_buffer.inner().buffer.internal_object(),
indexOffset: triangle.index_buffer.inner().offset as DeviceSize,
indexCount: (triangle.index_buffer.size() / index_stride as usize) as u32,
indexType: vk::INDEX_TYPE_UINT16, //triangle.index_type as u32,
transformData: vk::NULL_HANDLE,
transformOffset: 0,
};
vk::GeometryNV {
sType: vk::STRUCTURE_TYPE_GEOMETRY_NV,
pNext: ptr::null(),
geometryType: vk::GEOMETRY_TYPE_TRIANGLES_NV,
geometry: vk::GeometryDataNV {
triangles: geometry,
aabbs: vk::GeometryAABBNV {
sType: vk::STRUCTURE_TYPE_GEOMETRY_AABB_NV,
pNext: ptr::null(),
aabbData: 0,
numAABBs: 0,
stride: 0,
offset: 0,
},
},
flags: vk::GEOMETRY_OPAQUE_BIT_NV, // TODO
}
}))
.collect();

let aabb_bottom_level = {
let bottom_level = {
let info = vk::AccelerationStructureInfoNV {
sType: vk::STRUCTURE_TYPE_ACCELERATION_STRUCTURE_INFO_NV,
pNext: ptr::null(),
Expand Down Expand Up @@ -259,7 +309,7 @@ impl AccelerationStructureBuilder {
}
};

let structures = [&top_level, &aabb_bottom_level];
let structures = [&top_level, &bottom_level];

// Get requirements
let memory_requirements: Vec<vk::MemoryRequirements2> = structures
Expand Down Expand Up @@ -347,11 +397,11 @@ impl AccelerationStructureBuilder {
};

// Get handles for lower level structures
let aabb_bottom_level_handle = unsafe {
let bottom_level_handle = unsafe {
let mut output: u64 = 0;
check_errors(vk.GetAccelerationStructureHandleNV(
device.internal_object(),
aabb_bottom_level.inner_object.clone(),
bottom_level.inner_object.clone(),
size_of::<u64>(),
&mut output as *mut u64 as *mut c_void,
))?;
Expand All @@ -364,7 +414,8 @@ impl AccelerationStructureBuilder {
.iter()
.map(|structure| unsafe {
let info = vk::AccelerationStructureMemoryRequirementsInfoNV {
sType: vk::STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV,
sType:
vk::STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV,
pNext: ptr::null(),
type_: vk::ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_BUILD_SCRATCH_NV,
accelerationStructure: structure.inner_object,
Expand Down Expand Up @@ -409,8 +460,8 @@ impl AccelerationStructureBuilder {
instanceCustomIndex: [0, 0, 0],
mask: 0xFF,
instanceShaderBindingTableRecordOffset: [0, 0, 0],
flags: 0,
accelerationStructureReference: aabb_bottom_level_handle,
flags: vk::GEOMETRY_INSTANCE_TRIANGLE_CULL_DISABLE_BIT_NV,
accelerationStructureReference: bottom_level_handle,
};
ImmutableBuffer::from_data(instance, BufferUsage::ray_tracing(), queue.clone()).unwrap()
};
Expand All @@ -425,7 +476,7 @@ impl AccelerationStructureBuilder {
device: device.clone(),
nv_extension: true,
top_level,
aabb_bottom_level,
bottom_level,
memory,
instance_buffer,
scratch_buffer,
Expand All @@ -445,14 +496,26 @@ impl AccelerationStructureBuilder {

/// Add a Triangle Mesh to the acceleration structure
#[inline]
pub fn add_triangles<T>(
mut self, buffer: T,
pub fn add_triangles<V, I>(
mut self,
vertex_buffer: Arc<V>,
vertex_stride: DeviceSize,
vertex_format: Format,
index_buffer: Arc<I>,
index_type: IndexType,
) -> Result<AccelerationStructureBuilder, AccelerationStructureCreationError>
where
T: BufferAccess + Send + Sync + 'static,
V: BufferAccess + Send + Sync + 'static,
I: BufferAccess + Send + Sync + 'static,
{
// TODO
unimplemented!("Adding triangles is not yet implemented")
self.triangles.push(AccelerationStructureBuilderTriangles {
vertex_buffer,
vertex_stride,
vertex_format,
index_buffer,
index_type,
});
Ok(self)
}

/// Add Custom Intersection Geometry to the acceleration structure
Expand Down Expand Up @@ -619,12 +682,17 @@ pub enum GeometryType {

#[derive(Copy, Clone)]
pub struct GeometryTrianglesData {
pub vertex_format: Format,
pub vertex_data: u64,
pub vertex_offset: DeviceSize,
pub vertex_count: u32,
pub vertex_stride: usize,
pub index_type: IndexType,
pub vertex_format: Format,
pub index_data: u64,
pub index_offset: DeviceSize,
pub index_count: u32,
pub index_type: IndexType,
pub transform_data: u64,
pub transform_offset: DeviceSize,
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -665,19 +733,24 @@ impl<'a> From<&'a vk::GeometryNV> for Geometry {
let geometry = match geometry_nv.geometryType {
vk::GEOMETRY_TYPE_TRIANGLES_KHR => GeometryData {
triangles: GeometryTrianglesData {
vertex_data: geometry_nv.geometry.triangles.vertexData,
vertex_offset: geometry_nv.geometry.triangles.vertexOffset,
vertex_count: geometry_nv.geometry.triangles.vertexCount,
vertex_stride: geometry_nv.geometry.triangles.vertexStride as usize,
vertex_format: Format::from_vulkan_num(
geometry_nv.geometry.triangles.vertexFormat,
)
.unwrap(),
vertex_data: geometry_nv.geometry.triangles.vertexData,
vertex_stride: geometry_nv.geometry.triangles.vertexStride as usize,
index_data: geometry_nv.geometry.triangles.indexData,
index_offset: geometry_nv.geometry.triangles.indexOffset,
index_count: geometry_nv.geometry.triangles.indexCount,
index_type: match geometry_nv.geometry.triangles.indexType {
vk::INDEX_TYPE_UINT16 => IndexType::U16,
vk::INDEX_TYPE_UINT32 => IndexType::U32,
_ => unreachable!(),
},
index_data: geometry_nv.geometry.triangles.indexData,
transform_data: geometry_nv.geometry.triangles.transformData,
transform_offset: geometry_nv.geometry.triangles.transformOffset,
},
},
vk::GEOMETRY_TYPE_AABBS_KHR => GeometryData {
Expand Down Expand Up @@ -721,19 +794,19 @@ impl<'a> From<&'a Geometry> for vk::GeometryNV {
sType: vk::STRUCTURE_TYPE_GEOMETRY_TRIANGLES_NV,
pNext: ptr::null(),
vertexData: geometry.geometry.triangles.vertex_data,
vertexOffset: 0, // TODO:
vertexCount: 0, // TODO:
vertexOffset: geometry.geometry.triangles.vertex_offset,
vertexCount: geometry.geometry.triangles.vertex_count,
vertexStride: geometry.geometry.triangles.vertex_stride as DeviceSize,
vertexFormat: geometry.geometry.triangles.vertex_format as u32,
indexData: geometry.geometry.triangles.index_data,
indexOffset: 0, // TODO:
indexCount: 0, // TODO:
indexOffset: geometry.geometry.triangles.index_offset,
indexCount: geometry.geometry.triangles.index_count,
indexType: match geometry.geometry.triangles.index_type {
IndexType::U16 => vk::INDEX_TYPE_UINT16,
IndexType::U32 => vk::INDEX_TYPE_UINT32,
},
transformData: geometry.geometry.triangles.transform_data,
transformOffset: 0, // TODO:
transformOffset: geometry.geometry.triangles.transform_offset,
},
aabbs: GeometryAABBNV {
sType: vk::STRUCTURE_TYPE_GEOMETRY_AABB_NV,
Expand Down Expand Up @@ -831,7 +904,7 @@ impl Drop for AccelerationStructure {
);
vk.DestroyAccelerationStructureNV(
self.device.internal_object(),
self.aabb_bottom_level.inner_object,
self.bottom_level.inner_object,
ptr::null(),
);
} else {
Expand All @@ -842,7 +915,7 @@ impl Drop for AccelerationStructure {
);
vk.DestroyAccelerationStructureKHR(
self.device.internal_object(),
self.aabb_bottom_level.inner_object,
self.bottom_level.inner_object,
ptr::null(),
);
}
Expand Down

0 comments on commit 87701eb

Please sign in to comment.