Skip to content

Commit

Permalink
RT tests working.
Browse files Browse the repository at this point in the history
  • Loading branch information
facundo-villa committed Nov 4, 2023
1 parent c0ad7ad commit 58225e9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 35 deletions.
67 changes: 48 additions & 19 deletions src/rendering/render_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub enum Handle {
Swapchain(SwapchainHandle),
Allocation(AllocationHandle),
TextureCopy(TextureCopyHandle),
BottomLevelAccelerationStructure(BottomLevelAccelerationStructureHandle),
}

// HANDLES
Expand Down Expand Up @@ -1918,13 +1919,13 @@ pub(super) mod tests {
const FRAMES_IN_FLIGHT: usize = 2;

let mut window_system = window_system::WindowSystem::new();
// let mut window_system = window_system::WindowSystem::new();

// Use and odd width to make sure there is a middle/center pixel
let extent = crate::Extent { width: 1920, height: 1080, depth: 1 };

let window_handle = window_system.create_window("Renderer Test", extent, "test");
let swapchain = renderer.bind_to_window(&window_system.get_os_handles_2(&window_handle));
// let window_handle = window_system.create_window("Renderer Test", extent, "test");
// let swapchain = renderer.bind_to_window(&window_system.get_os_handles_2(&window_handle));

let positions: [f32; 3 * 3] = [
0.0, 1.0, 0.0,
Expand All @@ -1938,11 +1939,6 @@ pub(super) mod tests {
0.0, 0.0, 1.0, 1.0,
];

let vertex_layout = [
VertexElement{ name: "POSITION".to_string(), format: DataTypes::Float3, binding: 0 },
VertexElement{ name: "COLOR".to_string(), format: DataTypes::Float4, binding: 0 },
];

let vertex_positions_buffer = renderer.create_buffer(None, positions.len() * 4, Uses::Storage | Uses::AccelerationStructureBuild, DeviceAccesses::CpuWrite | DeviceAccesses::GpuRead, UseCases::STATIC);
let vertex_colors_buffer = renderer.create_buffer(None, colors.len() * 4, Uses::Storage | Uses::AccelerationStructureBuild, DeviceAccesses::CpuWrite | DeviceAccesses::GpuRead, UseCases::STATIC);
let index_buffer = renderer.create_buffer(None, 3 * 2, Uses::Storage | Uses::AccelerationStructureBuild, DeviceAccesses::CpuWrite | DeviceAccesses::GpuRead, UseCases::STATIC);
Expand Down Expand Up @@ -2115,8 +2111,8 @@ void main() {
let building_command_buffer_handle = renderer.create_command_buffer();
let rendering_command_buffer_handle = renderer.create_command_buffer();

let render_finished_synchronizer = renderer.create_synchronizer(true);
let image_ready = renderer.create_synchronizer(true);
let render_finished_synchronizer = renderer.create_synchronizer(false);
// let image_ready = renderer.create_synchronizer(true);

let instances_buffer = renderer.create_acceleration_structure_instance_buffer(None, 1);

Expand All @@ -2134,9 +2130,7 @@ void main() {
renderer.write_sbt_entry(miss_sbt_buffer, 0, pipeline, miss_shader);
renderer.write_sbt_entry(hit_sbt_buffer, 0, pipeline, closest_hit_shader);

for i in 0..FRAMES_IN_FLIGHT * 1000 {
renderer.wait(render_finished_synchronizer);

for i in 0..FRAMES_IN_FLIGHT * 10 {
// {
// renderer.wait(build_sync);

Expand Down Expand Up @@ -2171,7 +2165,7 @@ void main() {

renderer.start_frame_capture();

let image_index = renderer.acquire_swapchain_image(swapchain, image_ready);
// let image_index = renderer.acquire_swapchain_image(swapchain, image_ready);

let mut command_buffer_recording = renderer.create_command_buffer_recording(rendering_command_buffer_handle, Some(i as u32));

Expand All @@ -2193,6 +2187,15 @@ void main() {
scratch_buffer: BufferDescriptor { buffer: scratch_buffer, offset: 0, range: 1024 * 512, slot: 0 },
}]);

command_buffer_recording.consume_resources(&[
Consumption {
handle: Handle::BottomLevelAccelerationStructure(bottom_level_acceleration_structure),
stages: Stages::RAYGEN,
access: AccessPolicies::READ,
layout: Layouts::General,
}
]);

command_buffer_recording.build_top_level_acceleration_structure(&TopLevelAccelerationStructureBuild {
acceleration_structure: top_level_acceleration_structure,
description: TopLevelAccelerationStructureBuildDescriptions::Instance {
Expand Down Expand Up @@ -2222,6 +2225,30 @@ void main() {
access: AccessPolicies::READ,
layout: Layouts::General,
},
Consumption {
handle: Handle::BottomLevelAccelerationStructure(bottom_level_acceleration_structure),
stages: Stages::RAYGEN,
access: AccessPolicies::READ,
layout: Layouts::General,
},
Consumption {
handle: Handle::Buffer(raygen_sbt_buffer),
stages: Stages::RAYGEN,
access: AccessPolicies::READ,
layout: Layouts::General,
},
Consumption {
handle: Handle::Buffer(miss_sbt_buffer),
stages: Stages::RAYGEN,
access: AccessPolicies::READ,
layout: Layouts::General,
},
Consumption {
handle: Handle::Buffer(hit_sbt_buffer),
stages: Stages::RAYGEN,
access: AccessPolicies::READ,
layout: Layouts::General,
},
]);

command_buffer_recording.trace_rays(BindingTables {
Expand All @@ -2231,21 +2258,23 @@ void main() {
callable: None,
}, 1920, 1080, 1);

command_buffer_recording.copy_to_swapchain(render_target, image_index, swapchain);
// command_buffer_recording.copy_to_swapchain(render_target, image_index, swapchain);

let texure_copy_handles = command_buffer_recording.sync_textures(&[render_target]);

command_buffer_recording.execute(&[/*build_sync,*/image_ready], &[render_finished_synchronizer], render_finished_synchronizer);
command_buffer_recording.execute(&[/*build_sync,*/], &[], render_finished_synchronizer);

renderer.present(image_index, &[swapchain], render_finished_synchronizer);
// renderer.present(image_index, &[swapchain], render_finished_synchronizer);

renderer.end_frame_capture();

// renderer.wait(render_finished_synchronizer);

assert!(!renderer.has_errors());

// let pixels = unsafe { std::slice::from_raw_parts(renderer.get_image_data(texure_copy_handles[0]).as_ptr() as *const RGBAu8, (extent.width * extent.height) as usize) };
renderer.wait(render_finished_synchronizer);

let pixels = unsafe { std::slice::from_raw_parts(renderer.get_image_data(texure_copy_handles[0]).as_ptr() as *const RGBAu8, (extent.width * extent.height) as usize) };

// let mut file = std::fs::File::create("test.png").unwrap();
//
Expand All @@ -2257,7 +2286,7 @@ void main() {
// let mut writer = encoder.write_header().unwrap();
// writer.write_image_data(unsafe { std::slice::from_raw_parts(pixels.as_ptr() as *const u8, pixels.len() * 4) }).unwrap();

// check_triangle(pixels, extent);
check_triangle(pixels, extent);
}
}
}
44 changes: 28 additions & 16 deletions src/rendering/vulkan_render_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,7 +2751,7 @@ impl render_system::CommandBufferRecording for VulkanCommandBufferRecording<'_>
}

fn build_top_level_acceleration_structure(&mut self, acceleration_structure_build: &render_system::TopLevelAccelerationStructureBuild) {
let (_, acceleration_structure) = self.get_top_level_acceleration_structure(acceleration_structure_build.acceleration_structure);
let (acceleration_structure_handle, acceleration_structure) = self.get_top_level_acceleration_structure(acceleration_structure_build.acceleration_structure);

let (as_geometries, offsets) = match acceleration_structure_build.description {
render_system::TopLevelAccelerationStructureBuildDescriptions::Instance { instances_buffer, instance_count } => {
Expand Down Expand Up @@ -2797,6 +2797,12 @@ impl render_system::CommandBufferRecording for VulkanCommandBufferRecording<'_>
})
/* .build() */;

self.states.insert(render_system::Handle::TopLevelAccelerationStructure(acceleration_structure_handle), TransitionState {
stage: vk::PipelineStageFlags2::ACCELERATION_STRUCTURE_BUILD_KHR,
access: vk::AccessFlags2::ACCELERATION_STRUCTURE_WRITE_KHR,
layout: vk::ImageLayout::UNDEFINED,
});

let infos = vec![build_geometry_info];
let build_range_infos = vec![offsets];
let geometries = vec![as_geometries];
Expand All @@ -2815,7 +2821,7 @@ impl render_system::CommandBufferRecording for VulkanCommandBufferRecording<'_>
fn build_bottom_level_acceleration_structures(&mut self, acceleration_structure_builds: &[render_system::BottomLevelAccelerationStructureBuild]) {
fn visit(this: &mut VulkanCommandBufferRecording, acceleration_structure_builds: &[render_system::BottomLevelAccelerationStructureBuild], mut infos: Vec<vk::AccelerationStructureBuildGeometryInfoKHR>, mut geometries: Vec<Vec<vk::AccelerationStructureGeometryKHR>>, mut build_range_infos: Vec<Vec<vk::AccelerationStructureBuildRangeInfoKHR>>,) {
if let Some(build) = acceleration_structure_builds.first() {
let (_, acceleration_structure) = this.get_bottom_level_acceleration_structure(build.acceleration_structure);
let (acceleration_structure_handle, acceleration_structure) = this.get_bottom_level_acceleration_structure(build.acceleration_structure);

let (as_geometries, offsets) = match &build.description {
render_system::BottomLevelAccelerationStructureBuildDescriptions::AABB { aabb_buffer, transform_buffer, transform_count } => {
Expand Down Expand Up @@ -2892,6 +2898,12 @@ impl render_system::CommandBufferRecording for VulkanCommandBufferRecording<'_>
device_address: scratch_buffer_address,
})
/* .build() */;

this.states.insert(render_system::Handle::BottomLevelAccelerationStructure(acceleration_structure_handle), TransitionState {
stage: vk::PipelineStageFlags2::ACCELERATION_STRUCTURE_BUILD_KHR,
access: vk::AccessFlags2::ACCELERATION_STRUCTURE_WRITE_KHR,
layout: vk::ImageLayout::UNDEFINED,
});

infos.push(build_geometry_info);
build_range_infos.push(offsets);
Expand Down Expand Up @@ -3086,21 +3098,21 @@ impl render_system::CommandBufferRecording for VulkanCommandBufferRecording<'_>

buffer_memory_barriers.push(buffer_memory_barrier);

let memory_barrier = if let Some(source) = self.states.get(&consumption.handle) {
vk::MemoryBarrier2::default()
.src_stage_mask(source.stage)
.src_access_mask(source.access)
} else {
vk::MemoryBarrier2::default()
.src_stage_mask(vk::PipelineStageFlags2::empty())
.src_access_mask(vk::AccessFlags2KHR::empty())
}
.dst_stage_mask(new_stage_mask)
.dst_access_mask(new_access_mask);

memory_barriers.push(memory_barrier);
// let memory_barrier = if let Some(source) = self.states.get(&consumption.handle) {
// vk::MemoryBarrier2::default()
// .src_stage_mask(source.stage)
// .src_access_mask(source.access)
// } else {
// vk::MemoryBarrier2::default()
// .src_stage_mask(vk::PipelineStageFlags2::empty())
// .src_access_mask(vk::AccessFlags2KHR::empty())
// }
// .dst_stage_mask(new_stage_mask)
// .dst_access_mask(new_access_mask);

// memory_barriers.push(memory_barrier);
},
render_system::Handle::TopLevelAccelerationStructure(handle) => {
render_system::Handle::TopLevelAccelerationStructure(_) | render_system::Handle::BottomLevelAccelerationStructure(_)=> {
// let (handle, acceleration_structure) = self.get_top_level_acceleration_structure(handle);

let memory_barrier = if let Some(source) = self.states.get(&consumption.handle) {
Expand Down

0 comments on commit 58225e9

Please sign in to comment.