From ffe0e993cd518f366dd001c44df4e2d55109e51d Mon Sep 17 00:00:00 2001 From: Facundo Villa Date: Sat, 23 Dec 2023 17:29:32 -0300 Subject: [PATCH] Added temp file dir for tests. --- src/ghi/graphics_hardware_interface.rs | 113 +++++++++-------------- src/ghi/mod.rs | 2 +- src/ghi/vulkan_ghi.rs | 23 ++--- src/resource_manager/resource_manager.rs | 29 ++++-- 4 files changed, 75 insertions(+), 92 deletions(-) diff --git a/src/ghi/graphics_hardware_interface.rs b/src/ghi/graphics_hardware_interface.rs index a8b7f244..acc9cdbc 100644 --- a/src/ghi/graphics_hardware_interface.rs +++ b/src/ghi/graphics_hardware_interface.rs @@ -334,6 +334,12 @@ impl ShaderBindingDescriptor { } } +/// Configuration for which features to request from the underlying API. +pub struct Features { + pub validation: bool, + pub ray_tracing: bool, +} + pub trait GraphicsHardwareInterface { /// Returns whether the underlying API has encountered any errors. Used during tests to assert whether the validation layers have caught any errors. fn has_errors(&self) -> bool; @@ -1010,6 +1016,7 @@ pub(super) mod tests { void main() { out_color = in_color; gl_Position = vec4(in_position, 1.0); + gl_Position.y *= -1.0; } "; @@ -1144,6 +1151,7 @@ pub(super) mod tests { void main() { out_color = in_color; gl_Position = vec4(in_position, 1.0); + gl_Position.y *= -1.0; } "; @@ -1269,6 +1277,7 @@ pub(super) mod tests { void main() { out_color = in_color; gl_Position = vec4(in_position, 1.0); + gl_Position.y *= -1.0; } "; @@ -1395,6 +1404,7 @@ pub(super) mod tests { void main() { out_color = in_color; gl_Position = vec4(in_position, 1.0); + gl_Position.y *= -1.0; } "; @@ -1531,6 +1541,7 @@ pub(super) mod tests { void main() { out_color = in_color; gl_Position = push_constants.matrix * vec4(in_position, 1.0); + gl_Position.y *= -1.0; } "; @@ -1693,6 +1704,7 @@ pub(super) mod tests { void main() { out_color = in_color; gl_Position = vec4(in_position, 1.0); + gl_Position.y *= -1.0; } "; @@ -1781,15 +1793,6 @@ pub(super) mod tests { command_buffer_recording.write_image_data(sampled_texture, &pixels); - // command_buffer_recording.consume_resources(&[ - // Consumption{ - // handle: Handle::Image(sampled_texture), - // stages: Stages::FRAGMENT, - // access: AccessPolicies::READ, - // layout: Layouts::Read, - // } - // ]); - let attachments = [ AttachmentInformation { image: render_target, @@ -1801,12 +1804,12 @@ pub(super) mod tests { } ]; - command_buffer_recording.start_render_pass(extent, &attachments); - command_buffer_recording.bind_raster_pipeline(&pipeline); command_buffer_recording.bind_descriptor_sets(&pipeline_layout, &[descriptor_set]); + command_buffer_recording.start_render_pass(extent, &attachments); + command_buffer_recording.draw_mesh(&mesh); command_buffer_recording.end_render_pass(); @@ -2050,8 +2053,6 @@ void main() { renderer.start_frame_capture(); - // 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)); { @@ -2097,44 +2098,38 @@ void main() { command_buffer_recording.bind_descriptor_sets(&pipeline_layout, &[descriptor_set]); - // unsafe { command_buffer_recording.consume_resources(&[ - // Consumption { - // handle: Handle::Image(render_target), - // stages: Stages::RAYGEN, - // access: AccessPolicies::WRITE, - // layout: Layouts::General, - // }, - // Consumption { - // handle: Handle::TopLevelAccelerationStructure(top_level_acceleration_structure), - // stages: Stages::RAYGEN, - // 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, - // }, - // ]) }; + unsafe { command_buffer_recording.consume_resources(&[ + Consumption { + handle: Handle::TopLevelAccelerationStructure(top_level_acceleration_structure), + stages: Stages::RAYGEN, + 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 { raygen: BufferStridedRange { buffer: raygen_sbt_buffer, offset: 0, stride: 64, size: 64 }, @@ -2143,33 +2138,17 @@ void main() { callable: None, }, 1920, 1080, 1); - // 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,*/], &[], render_finished_synchronizer); - // renderer.present(image_index, &[swapchain], render_finished_synchronizer); - renderer.end_frame_capture(); - // renderer.wait(render_finished_synchronizer); - assert!(!renderer.has_errors()); 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(); -// - // let mut encoder = png::Encoder::new(&mut file, extent.width, extent.height); -// - // encoder.set_color(png::ColorType::Rgba); - // encoder.set_depth(png::BitDepth::Eight); -// - // 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); } diff --git a/src/ghi/mod.rs b/src/ghi/mod.rs index 63ad92aa..0d63f964 100644 --- a/src/ghi/mod.rs +++ b/src/ghi/mod.rs @@ -9,5 +9,5 @@ pub mod vulkan_ghi; pub use crate::ghi::graphics_hardware_interface::*; pub fn create() -> impl GraphicsHardwareInterface { - vulkan_ghi::VulkanGHI::new(&self::vulkan_ghi::Settings{ validation: true, ray_tracing: false }) + vulkan_ghi::VulkanGHI::new(&graphics_hardware_interface::Features{ validation: true, ray_tracing: false }) } \ No newline at end of file diff --git a/src/ghi/vulkan_ghi.rs b/src/ghi/vulkan_ghi.rs index 9ed9dace..c10c6b11 100644 --- a/src/ghi/vulkan_ghi.rs +++ b/src/ghi/vulkan_ghi.rs @@ -1939,17 +1939,12 @@ impl Into for graphics_hardware_interface:: } } -pub struct Settings { - pub validation: bool, - pub ray_tracing: bool, -} - struct DebugCallbackData { error_count: u64, } impl VulkanGHI { - pub fn new(settings: &Settings) -> VulkanGHI { + pub fn new(settings: &Features) -> VulkanGHI { let entry: ash::Entry = Entry::linked(); let application_info = vk::ApplicationInfo::default() @@ -2217,7 +2212,7 @@ impl VulkanGHI { } pub fn new_as_system() -> orchestrator::EntityReturn<'static, VulkanGHI> { - let settings = Settings { + let settings = Features { validation: true, ray_tracing: true, }; @@ -4085,43 +4080,43 @@ mod tests { #[test] fn render_triangle() { - let mut ghi = VulkanGHI::new(&Settings { validation: true, ray_tracing: false }); + let mut ghi = VulkanGHI::new(&Features { validation: true, ray_tracing: false }); graphics_hardware_interface::tests::render_triangle(&mut ghi); } #[test] fn present() { - let mut ghi = VulkanGHI::new(&Settings { validation: true, ray_tracing: false }); + let mut ghi = VulkanGHI::new(&Features { validation: true, ray_tracing: false }); graphics_hardware_interface::tests::present(&mut ghi); } #[test] fn multiframe_present() { - let mut ghi = VulkanGHI::new(&Settings { validation: true, ray_tracing: false }); + let mut ghi = VulkanGHI::new(&Features { validation: true, ray_tracing: false }); // TODO: investigate graphical corruption, most likely has to do with synchronization graphics_hardware_interface::tests::multiframe_present(&mut ghi); } #[test] fn multiframe_rendering() { - let mut ghi = VulkanGHI::new(&Settings { validation: true, ray_tracing: false }); + let mut ghi = VulkanGHI::new(&Features { validation: true, ray_tracing: false }); graphics_hardware_interface::tests::multiframe_rendering(&mut ghi); } #[test] fn dynamic_data() { - let mut ghi = VulkanGHI::new(&Settings { validation: true, ray_tracing: false }); + let mut ghi = VulkanGHI::new(&Features { validation: true, ray_tracing: false }); graphics_hardware_interface::tests::dynamic_data(&mut ghi); } #[test] fn descriptor_sets() { - let mut ghi = VulkanGHI::new(&Settings { validation: true, ray_tracing: false }); + let mut ghi = VulkanGHI::new(&Features { validation: true, ray_tracing: false }); graphics_hardware_interface::tests::descriptor_sets(&mut ghi); } #[test] fn ray_tracing() { - let mut ghi = VulkanGHI::new(&Settings { validation: true, ray_tracing: true }); + let mut ghi = VulkanGHI::new(&Features { validation: true, ray_tracing: true }); graphics_hardware_interface::tests::ray_tracing(&mut ghi); } } \ No newline at end of file diff --git a/src/resource_manager/resource_manager.rs b/src/resource_manager/resource_manager.rs index 050771d6..56cce9aa 100644 --- a/src/resource_manager/resource_manager.rs +++ b/src/resource_manager/resource_manager.rs @@ -40,14 +40,14 @@ impl From for super::LoadResults { impl ResourceManager { /// Creates a new resource manager. pub fn new() -> Self { - if let Err(error) = std::fs::create_dir_all("assets") { + if let Err(error) = std::fs::create_dir_all(Self::resolve_asset_path(std::path::Path::new(""))) { match error.kind() { std::io::ErrorKind::AlreadyExists => {}, _ => panic!("Could not create assets directory"), } } - if let Err(error) = std::fs::create_dir_all("resources") { + if let Err(error) = std::fs::create_dir_all(Self::resolve_resource_path(std::path::Path::new(""))) { match error.kind() { std::io::ErrorKind::AlreadyExists => {}, _ => panic!("Could not create resources directory"), @@ -63,7 +63,7 @@ impl ResourceManager { } let db_res = if !memory_only { - polodb_core::Database::open_file("resources/resources.db") + polodb_core::Database::open_file(Self::resolve_resource_path(std::path::Path::new("resources.db"))) } else { info!("Using memory database instead of file database."); polodb_core::Database::open_memory() @@ -73,11 +73,11 @@ impl ResourceManager { Ok(db) => db, Err(_) => { // Delete file and try again - std::fs::remove_file("resources/resources.db").unwrap(); + std::fs::remove_file(Self::resolve_resource_path(std::path::Path::new("resources.db"))).unwrap(); warn!("Database file was corrupted, deleting and trying again."); - let db_res = polodb_core::Database::open_file("resources/resources.db"); + let db_res = polodb_core::Database::open_file(Self::resolve_resource_path(std::path::Path::new("resources.db"))); match db_res { Ok(db) => db, @@ -313,7 +313,7 @@ impl ResourceManager { let resource_id = insert_result.inserted_id.as_object_id()?; - let resource_path = self.resolve_resource_path(resource_id.to_string().as_str()); + let resource_path = Self::resolve_resource_path(std::path::Path::new(&resource_id.to_string())); let mut file = std::fs::File::create(resource_path).ok()?; @@ -332,7 +332,7 @@ impl ResourceManager { let resources = request.resources.into_iter().map(|resource_container| { let native_db_resource_id = resource_container._id.to_string(); - let mut file = match std::fs::File::open(self.resolve_resource_path(&native_db_resource_id)) { + let mut file = match std::fs::File::open(Self::resolve_resource_path(std::path::Path::new(&native_db_resource_id))) { Ok(it) => it, Err(reason) => { match reason { // TODO: handle specific errors @@ -373,8 +373,17 @@ impl ResourceManager { return Ok(Response { resources }); } - fn resolve_resource_path(&self, path: &str) -> String { "resources/".to_string() + path } - fn resolve_asset_path(&self, path: &str) -> String { "assets/".to_string() + path } + fn resolve_resource_path(path: &std::path::Path) -> std::path::PathBuf { + if cfg!(test) { + std::env::temp_dir().join("resources").join(path) + } else { + std::path::PathBuf::from("resources/").join(path) + } + } + + fn resolve_asset_path(path: &std::path::Path) -> std::path::PathBuf { + std::path::PathBuf::from("assets/").join(path) + } /// Loads an asset from source.\ /// Expects an asset name in the form of a path relative to the assets directory, or a network address.\ @@ -419,7 +428,7 @@ impl ResourceManager { } pub fn realize_asset_path(&self, url:&str) -> Option { - let path = std::path::Path::new("assets/"); + let path = Self::resolve_asset_path(std::path::Path::new("")); let url_as_path = std::path::Path::new(url);