Skip to content

Commit

Permalink
Fix for ray trace examples not working in release mode (#47)
Browse files Browse the repository at this point in the history
* v0.7.1
* Fix instance_slice function
* Add support for minimum acceleration structure scratch buffer alignment
  • Loading branch information
attackgoat authored Dec 18, 2022
1 parent a636993 commit e2b7350
Show file tree
Hide file tree
Showing 18 changed files with 186 additions and 70 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.7.1] - 2022-12-17

### Fixed

- Soundness issue in `AccelerationStructure::instance_slice` helper function

### Added

Expand Down Expand Up @@ -289,4 +293,5 @@ _See [#25](https://github.com/attackgoat/screen-13/pull/25) for migration detail
[0.6.3]: https://crates.io/crates/screen-13/0.6.3
[0.6.4]: https://crates.io/crates/screen-13/0.6.4
[0.6.5]: https://crates.io/crates/screen-13/0.6.5
[0.7.0]: https://crates.io/crates/screen-13/0.7.0
[0.7.0]: https://crates.io/crates/screen-13/0.7.0
[0.7.1]: https://crates.io/crates/screen-13/0.7.1
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "screen-13"
version = "0.7.0"
version = "0.7.1"
authors = ["John Wells <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
8 changes: 4 additions & 4 deletions contrib/screen-13-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ impl Egui {

let num_indices = mesh.indices.len() as u32;

let clip_x = (clip_rect.min.x as f32 * pixels_per_point) as i32;
let clip_y = (clip_rect.min.y as f32 * pixels_per_point) as i32;
let clip_x = (clip_rect.min.x * pixels_per_point) as i32;
let clip_y = (clip_rect.min.y * pixels_per_point) as i32;

let clip_width =
((clip_rect.max.x - clip_rect.min.x) as f32 * pixels_per_point) as u32;
((clip_rect.max.x - clip_rect.min.x) * pixels_per_point) as u32;
let clip_height =
((clip_rect.max.y - clip_rect.min.y) as f32 * pixels_per_point) as u32;
((clip_rect.max.y - clip_rect.min.y) * pixels_per_point) as u32;

render_graph
.begin_pass("Egui pass")
Expand Down
2 changes: 1 addition & 1 deletion contrib/screen-13-fx/src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl ImageLoader {

//trace!("{bitmap_width}x{bitmap_height} Stride={bitmap_stride}");

let pixel_buf_stride = align_up_u32(stride as u32, 12);
let pixel_buf_stride = align_up_u32(stride, 12);
let pixel_buf_len = (pixel_buf_stride * height) as vk::DeviceSize;

//trace!("pixel_buf_len={pixel_buf_len} pixel_buf_stride={pixel_buf_stride}");
Expand Down
2 changes: 1 addition & 1 deletion contrib/screen-13-fx/src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl TransitionPipeline {
let a_image = a_image.into();
let b_image = b_image.into();
let dest_image = dest_image.into();
let progress = progress.max(0.0).min(1.0);
let progress = progress.clamp(0.0, 1.0);

let dest_info = render_graph.node_info(dest_image);

Expand Down
17 changes: 13 additions & 4 deletions examples/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
)
.unwrap();

let scratch_buf_padding = frame
.device
.accel_struct_properties
.as_ref()
.unwrap()
.min_accel_struct_scratch_offset_alignment as vk::DeviceSize;

// Lease and bind a bunch of bottom-level acceleration structures and add to instance buffer
let mut blas_nodes = Vec::with_capacity(BLAS_COUNT as _);
for idx in 0..BLAS_COUNT {
Expand All @@ -167,7 +174,7 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
Buffer::copy_from_slice(
&mut instance_buf,
idx * instance_len,
AccelerationStructure::instance_slice(vk::AccelerationStructureInstanceKHR {
AccelerationStructure::instance_slice(&[vk::AccelerationStructureInstanceKHR {
transform: vk::TransformMatrixKHR {
matrix: [
1.0, 0.0, 0.0, 0.0, //
Expand All @@ -183,13 +190,13 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
acceleration_structure_reference: vk::AccelerationStructureReferenceKHR {
device_handle: AccelerationStructure::device_address(&blas),
},
}),
}]),
);

let blas_node = frame.render_graph.bind_node(blas);
let scratch_buf = frame.render_graph.bind_node(
pool.lease(BufferInfo::new(
blas_size.build_size,
blas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS | vk::BufferUsageFlags::STORAGE_BUFFER,
))
.unwrap(),
Expand All @@ -211,6 +218,7 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
},
}],
};
let instance_buf = frame.render_graph.bind_node(instance_buf);
let tlas_size = AccelerationStructure::size_of(frame.device, &tlas_geometry_info);
let tlas = pool
.lease(AccelerationStructureInfo {
Expand All @@ -221,7 +229,7 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
let tlas_node = frame.render_graph.bind_node(tlas);
let tlas_scratch_buf = frame.render_graph.bind_node(
pool.lease(BufferInfo::new(
tlas_size.build_size,
tlas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS | vk::BufferUsageFlags::STORAGE_BUFFER,
))
.unwrap(),
Expand Down Expand Up @@ -269,6 +277,7 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
pass.access_node_mut(blas_node, AccessType::AccelerationStructureBuildRead);
}

pass.access_node_mut(instance_buf, AccessType::AccelerationStructureBuildRead);
pass.access_node_mut(
tlas_scratch_buf,
AccessType::AccelerationStructureBufferWrite,
Expand Down
36 changes: 22 additions & 14 deletions examples/ray_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ fn main() -> anyhow::Result<()> {
// Create an instance buffer, which is just one instance for the single BLAS
// ------------------------------------------------------------------------------------------ //

let instance = AccelerationStructure::instance_slice(vk::AccelerationStructureInstanceKHR {
let instances = [vk::AccelerationStructureInstanceKHR {
transform: vk::TransformMatrixKHR {
matrix: [
1.0, 0.0, 0.0, 0.0, //
Expand All @@ -613,17 +613,18 @@ fn main() -> anyhow::Result<()> {
acceleration_structure_reference: vk::AccelerationStructureReferenceKHR {
device_handle: blas_device_address,
},
});
}];
let instance_data = AccelerationStructure::instance_slice(&instances);
let instance_buf = Arc::new({
let mut buffer = Buffer::create(
&event_loop.device,
BufferInfo::new_mappable(
instance.len() as _,
instance_data.len() as _,
vk::BufferUsageFlags::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR
| vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS,
),
)?;
Buffer::copy_from_slice(&mut buffer, 0, instance);
Buffer::copy_from_slice(&mut buffer, 0, instance_data);

buffer
});
Expand Down Expand Up @@ -658,6 +659,13 @@ fn main() -> anyhow::Result<()> {
// ------------------------------------------------------------------------------------------ //

{
let scratch_buf_padding = event_loop
.device
.accel_struct_properties
.as_ref()
.unwrap()
.min_accel_struct_scratch_offset_alignment
as vk::DeviceSize;
let mut render_graph = RenderGraph::new();
let index_node = render_graph.bind_node(&index_buf);
let vertex_node = render_graph.bind_node(&vertex_buf);
Expand All @@ -667,18 +675,18 @@ fn main() -> anyhow::Result<()> {
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
blas_size.build_size,
blas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
)?);

render_graph
.begin_pass("Build BLAS")
.read_node(index_node)
.read_node(vertex_node)
.write_node(blas_node)
.write_node(scratch_buf)
.access_node(index_node, AccessType::AccelerationStructureBuildRead)
.access_node(vertex_node, AccessType::AccelerationStructureBuildRead)
.access_node(scratch_buf, AccessType::AccelerationStructureBufferWrite)
.access_node(blas_node, AccessType::AccelerationStructureBuildWrite)
.record_acceleration(move |accel, _| {
accel.build_structure(
blas_node,
Expand All @@ -698,7 +706,7 @@ fn main() -> anyhow::Result<()> {
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
tlas_size.build_size,
tlas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
Expand All @@ -708,10 +716,10 @@ fn main() -> anyhow::Result<()> {

render_graph
.begin_pass("Build TLAS")
.read_node(blas_node)
.read_node(instance_node)
.write_node(scratch_buf)
.write_node(tlas_node)
.access_node(blas_node, AccessType::AccelerationStructureBuildRead)
.access_node(instance_node, AccessType::AccelerationStructureBuildRead)
.access_node(scratch_buf, AccessType::AccelerationStructureBufferWrite)
.access_node(tlas_node, AccessType::AccelerationStructureBuildWrite)
.record_acceleration(move |accel, _| {
accel.build_structure(
tlas_node,
Expand Down
40 changes: 24 additions & 16 deletions examples/rt_triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn main() -> anyhow::Result<()> {
// Create an instance buffer, which is just one instance for the single BLAS
// ------------------------------------------------------------------------------------------ //

let instance = AccelerationStructure::instance_slice(vk::AccelerationStructureInstanceKHR {
let instances = [vk::AccelerationStructureInstanceKHR {
transform: vk::TransformMatrixKHR {
matrix: [
1.0, 0.0, 0.0, 0.0, //
Expand All @@ -278,17 +278,18 @@ fn main() -> anyhow::Result<()> {
acceleration_structure_reference: vk::AccelerationStructureReferenceKHR {
device_handle: blas_device_address,
},
});
}];
let instance_data = AccelerationStructure::instance_slice(&instances);
let instance_buf = Arc::new({
let mut buffer = Buffer::create(
&event_loop.device,
BufferInfo::new_mappable(
instance.len() as _,
instance_data.len() as _,
vk::BufferUsageFlags::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR
| vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS,
),
)?;
Buffer::copy_from_slice(&mut buffer, 0, instance);
Buffer::copy_from_slice(&mut buffer, 0, instance_data);

buffer
});
Expand Down Expand Up @@ -323,6 +324,13 @@ fn main() -> anyhow::Result<()> {
// ------------------------------------------------------------------------------------------ //

{
let scratch_buf_padding = event_loop
.device
.accel_struct_properties
.as_ref()
.unwrap()
.min_accel_struct_scratch_offset_alignment
as vk::DeviceSize;
let mut render_graph = RenderGraph::new();
let index_node = render_graph.bind_node(&index_buf);
let vertex_node = render_graph.bind_node(&vertex_buf);
Expand All @@ -332,18 +340,18 @@ fn main() -> anyhow::Result<()> {
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
blas_size.build_size,
blas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
)?);

render_graph
.begin_pass("Build BLAS")
.read_node(index_node)
.read_node(vertex_node)
.write_node(blas_node)
.write_node(scratch_buf)
.access_node(index_node, AccessType::AccelerationStructureBuildRead)
.access_node(vertex_node, AccessType::AccelerationStructureBuildRead)
.access_node(scratch_buf, AccessType::AccelerationStructureBufferWrite)
.access_node(blas_node, AccessType::AccelerationStructureBuildWrite)
.record_acceleration(move |accel, _| {
accel.build_structure(
blas_node,
Expand All @@ -355,28 +363,28 @@ fn main() -> anyhow::Result<()> {
primitive_offset: 0,
transform_offset: 0,
}],
)
);
});
}

{
let instance_node = render_graph.bind_node(instance_buf);
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
tlas_size.build_size,
tlas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
)?);
let instance_node = render_graph.bind_node(&instance_buf);
let tlas_node = render_graph.bind_node(&tlas);

render_graph
.begin_pass("Build TLAS")
.read_node(blas_node)
.read_node(instance_node)
.write_node(scratch_buf)
.write_node(tlas_node)
.access_node(blas_node, AccessType::AccelerationStructureBuildRead)
.access_node(instance_node, AccessType::AccelerationStructureBuildRead)
.access_node(scratch_buf, AccessType::AccelerationStructureBufferWrite)
.access_node(tlas_node, AccessType::AccelerationStructureBuildWrite)
.record_acceleration(move |accel, _| {
accel.build_structure(
tlas_node,
Expand Down
2 changes: 1 addition & 1 deletion examples/shader-toy/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn compile_glsl(path: impl AsRef<Path>, kind: ShaderKind) -> anyhow::Result<(Pat
"{}.spirv",
source_path.extension().unwrap().to_string_lossy()
));
write(&spirv_path, &spirv).context("Unable to write SPIR-V binary")?;
write(&spirv_path, spirv).context("Unable to write SPIR-V binary")?;

Ok((source_path, spirv_path))
}
Expand Down
6 changes: 3 additions & 3 deletions src/driver/accel_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ impl AccelerationStructure {
}

/// Helper function which is used to prepare instance buffers.
pub fn instance_slice<'a>(instance: vk::AccelerationStructureInstanceKHR) -> &'a [u8] {
pub fn instance_slice(instances: &[vk::AccelerationStructureInstanceKHR]) -> &[u8] {
unsafe {
std::slice::from_raw_parts(
&instance as *const _ as *const _,
size_of::<vk::AccelerationStructureInstanceKHR>(),
instances.as_ptr() as *const _,
instances.len() * size_of::<vk::AccelerationStructureInstanceKHR>(),
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/driver/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ impl ComputePipeline {
let descriptor_info = PipelineDescriptorInfo::create(&device, &descriptor_bindings)?;
let descriptor_set_layouts = descriptor_info
.layouts
.iter()
.map(|(_, descriptor_set_layout)| **descriptor_set_layout)
.values()
.map(|descriptor_set_layout| **descriptor_set_layout)
.collect::<Box<[_]>>();

unsafe {
Expand Down
Loading

0 comments on commit e2b7350

Please sign in to comment.