Skip to content

Commit

Permalink
Added temp file dir for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
facundo-villa committed Dec 23, 2023
1 parent 08da571 commit ffe0e99
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 92 deletions.
113 changes: 46 additions & 67 deletions src/ghi/graphics_hardware_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
";

Expand Down Expand Up @@ -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;
}
";

Expand Down Expand Up @@ -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;
}
";

Expand Down Expand Up @@ -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;
}
";

Expand Down Expand Up @@ -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;
}
";

Expand Down Expand Up @@ -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;
}
";

Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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));

{
Expand Down Expand Up @@ -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 },
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ghi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
23 changes: 9 additions & 14 deletions src/ghi/vulkan_ghi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1939,17 +1939,12 @@ impl Into<graphics_hardware_interface::Stages> 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()
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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);
}
}
29 changes: 19 additions & 10 deletions src/resource_manager/resource_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ impl From<polodb_core::Error> 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"),
Expand All @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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()?;

Expand All @@ -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
Expand Down Expand Up @@ -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.\
Expand Down Expand Up @@ -419,7 +428,7 @@ impl ResourceManager {
}

pub fn realize_asset_path(&self, url:&str) -> Option<std::path::PathBuf> {
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);

Expand Down

0 comments on commit ffe0e99

Please sign in to comment.