From 1be5dadfae599ed7dc52ed1db10e5a6d673a3118 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev <71594357+Snowiiii@users.noreply.github.com> Date: Thu, 4 Jul 2024 19:44:56 +0200 Subject: [PATCH] Remove [vent-common] Expand Project structure --- Cargo.lock | 28 +++---------- crates/vent-common/Cargo.toml | 31 -------------- crates/vent-common/README.md | 12 ------ crates/vent-common/src/lib.rs | 2 - crates/vent-common/src/util/mod.rs | 1 - crates/vent-editor/Cargo.toml | 1 - crates/vent-rendering/src/instance.rs | 42 ++++++++++++++++--- crates/vent-runtime/Cargo.toml | 11 +++-- crates/vent-runtime/src/lib.rs | 24 +++++++---- .../src/project/mod.rs | 23 ++++++++-- crates/vent-runtime/src/render/camera/mod.rs | 2 +- crates/vent-runtime/src/render/mod.rs | 35 +++++++++------- .../mod.rs => vent-runtime/src/util/crash.rs} | 10 +---- crates/vent-runtime/src/util/mod.rs | 2 + crates/vent-runtime/src/util/version.rs | 22 ++++++++++ crates/vent-window/Cargo.toml | 4 ++ crates/vent-window/src/lib.rs | 4 +- test/runtime-sandbox/Cargo.toml | 1 - 18 files changed, 139 insertions(+), 116 deletions(-) delete mode 100644 crates/vent-common/Cargo.toml delete mode 100644 crates/vent-common/README.md delete mode 100644 crates/vent-common/src/lib.rs delete mode 100644 crates/vent-common/src/util/mod.rs rename crates/{vent-common => vent-runtime}/src/project/mod.rs (58%) rename crates/{vent-common/src/util/crash/mod.rs => vent-runtime/src/util/crash.rs} (87%) create mode 100644 crates/vent-runtime/src/util/mod.rs create mode 100644 crates/vent-runtime/src/util/version.rs diff --git a/Cargo.lock b/Cargo.lock index 1ad3e33..e7c062c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -620,12 +620,6 @@ dependencies = [ "weezl", ] -[[package]] -name = "glam" -version = "0.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "779ae4bf7e8421cf91c0b3b64e7e8b40b862fba4d393f59150042de7c4965a94" - [[package]] name = "gltf" version = "1.4.1" @@ -1416,7 +1410,6 @@ dependencies = [ name = "runtime-sandbox" version = "0.1.0" dependencies = [ - "vent-common", "vent-runtime", ] @@ -1801,18 +1794,6 @@ dependencies = [ "vent-sdk", ] -[[package]] -name = "vent-common" -version = "0.1.0" -dependencies = [ - "chrono", - "image", - "log", - "serde", - "serde_json", - "sysinfo", -] - [[package]] name = "vent-ecs" version = "0.1.0" @@ -1824,7 +1805,6 @@ dependencies = [ "ash", "log", "pollster", - "vent-common", "vent-rendering", "vent-runtime", ] @@ -1865,14 +1845,16 @@ dependencies = [ "android-activity", "android_logger", "ash", + "chrono", "downcast-rs", "fs_extra", - "glam", "log", "ndk", "pollster", + "serde", + "serde_json", + "sysinfo", "vent-assets", - "vent-common", "vent-ecs", "vent-logging", "vent-math", @@ -1891,6 +1873,8 @@ dependencies = [ "log", "raw-window-handle", "sctk-adwaita", + "serde", + "serde_json", "smithay-client-toolkit", "wayland-backend", "wayland-client", diff --git a/crates/vent-common/Cargo.toml b/crates/vent-common/Cargo.toml deleted file mode 100644 index 3b4e003..0000000 --- a/crates/vent-common/Cargo.toml +++ /dev/null @@ -1,31 +0,0 @@ -[package] -name = "vent-common" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -sysinfo = "0.30" -chrono = "0.4" - -image = "0.25" - -log = "0.4" - -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" - -# rfd = { version="0.11.4", default-features = false, features = ["xdg-portal"]} - -# [target.'cfg(target_arch = "wasm32")'.dependencies] -# console_error_panic_hook = "0.1.7" -# console_log = "1" -# wasm-bindgen = "0.2.87" -# wasm-bindgen-futures = "0.4.37" -# web-sys = { version = "0.3.64", features = [ -# "Document", -# "Window", -# "Element", -# "Location" -# ]} \ No newline at end of file diff --git a/crates/vent-common/README.md b/crates/vent-common/README.md deleted file mode 100644 index 69bb9ae..0000000 --- a/crates/vent-common/README.md +++ /dev/null @@ -1,12 +0,0 @@ -### Vent-Common - -Vent-Common is a temporary crate that serves as a code delivery mechanism to be used in other crates, such as Vent-Editor or Vent-Runtime. In the future, we plan to move this code to separate crates to simplify usage and reduce compile time. - -## Contributing - -If you're interested in contributing to Vent-Common, please fork the repository and submit a pull request. We welcome -contributions of all kinds, including bug fixes, new features, and documentation improvements. - -## License - -Vent-Common is licensed under the [Apache 2.0 License](../../LICENSE) diff --git a/crates/vent-common/src/lib.rs b/crates/vent-common/src/lib.rs deleted file mode 100644 index e0994d3..0000000 --- a/crates/vent-common/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod project; -pub mod util; diff --git a/crates/vent-common/src/util/mod.rs b/crates/vent-common/src/util/mod.rs deleted file mode 100644 index ee4e3bf..0000000 --- a/crates/vent-common/src/util/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod crash; diff --git a/crates/vent-editor/Cargo.toml b/crates/vent-editor/Cargo.toml index 6bcae7d..e74fdd3 100644 --- a/crates/vent-editor/Cargo.toml +++ b/crates/vent-editor/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] vent-runtime = { path = "../vent-runtime"} -vent-common = { path = "../vent-common"} vent-rendering = { path = "../vent-rendering"} diff --git a/crates/vent-rendering/src/instance.rs b/crates/vent-rendering/src/instance.rs index 7e52f11..8a3784f 100644 --- a/crates/vent-rendering/src/instance.rs +++ b/crates/vent-rendering/src/instance.rs @@ -58,6 +58,8 @@ pub struct VulkanInstance { pub in_flight_fences: Vec, pub images_in_flight: Vec, + pub vsync: bool, + frame: usize, #[cfg(debug_assertions)] @@ -69,17 +71,27 @@ pub struct VulkanInstance { } impl VulkanInstance { - pub fn new(application_name: &str, window: &vent_window::Window) -> Self { + pub fn new( + application_name: &String, + application_version: u32, + vsync: bool, + window: &vent_window::Window, + ) -> Self { let entry = Entry::linked(); - let engine_version: u32 = env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(); + let engine_version = vk::make_api_version( + 0, + env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(), + env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(), + env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(), + ); let app_info = unsafe { vk::ApplicationInfo::default() .application_name(CStr::from_bytes_with_nul_unchecked( application_name.as_bytes(), )) - .application_version(0) // TODO + .application_version(application_version) // TODO .engine_name(CStr::from_bytes_with_nul_unchecked(b"Vent-Engine\0")) .engine_version(engine_version) .api_version(vk::API_VERSION_1_3) @@ -162,6 +174,7 @@ impl VulkanInstance { surface_format, &surface_loader, pdevice, + vsync, surface, ( window.width().try_into().unwrap(), @@ -236,6 +249,7 @@ impl VulkanInstance { image_available_semaphores, render_finished_semaphores, in_flight_fences, + vsync, images_in_flight, frame: 0, } @@ -261,7 +275,11 @@ impl VulkanInstance { } } - pub fn recreate_swap_chain(&mut self, new_size: (u32, u32)) { + pub fn recreate_swap_chain(&mut self, new_size: Option<(u32, u32)>) { + let new_size = new_size.unwrap_or(( + self.surface_resolution.width, + self.surface_resolution.height, + )); unsafe { self.device.device_wait_idle().unwrap(); @@ -270,6 +288,7 @@ impl VulkanInstance { self.surface_format, &self.surface_loader, self.physical_device, + self.vsync, self.surface, new_size, Some(self.swapchain), @@ -515,6 +534,7 @@ impl VulkanInstance { surface_format: vk::SurfaceFormatKHR, surface_loader: &khr::surface::Instance, pdevice: vk::PhysicalDevice, + vsync: bool, surface: vk::SurfaceKHR, size: (u32, u32), old_swapchain: Option, @@ -556,10 +576,20 @@ impl VulkanInstance { let present_modes = unsafe { surface_loader.get_physical_device_surface_present_modes(pdevice, surface) } .unwrap(); + + let wanted_mode = if vsync { + vk::PresentModeKHR::FIFO + } else { + vk::PresentModeKHR::IMMEDIATE + }; + let present_mode = present_modes .iter() - .find(|&mode| *mode == vk::PresentModeKHR::IMMEDIATE) - .unwrap_or(&vk::PresentModeKHR::FIFO); + .find(|&mode| *mode == wanted_mode) + .unwrap_or({ + log::warn!("Swapchain: Wanted mode is not supported, Using FIFO"); + &vk::PresentModeKHR::FIFO + }); let swapchain_create_info = vk::SwapchainCreateInfoKHR::default() .surface(surface) diff --git a/crates/vent-runtime/Cargo.toml b/crates/vent-runtime/Cargo.toml index 1bc149a..262a9fb 100644 --- a/crates/vent-runtime/Cargo.toml +++ b/crates/vent-runtime/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -vent-common = { path = "../vent-common"} vent-rendering = { path = "../vent-rendering"} vent-assets = { path = "../vent-assets" } vent-ecs = { path = "../vent-ecs"} @@ -14,13 +13,19 @@ vent-window = { path = "../vent-window"} vent-logging = { path = "../vent-logging"} vent-math = { path = "../vent-math"} +# Rendering ash = { version= "0.38", default-features = false, features = ["linked"] } -glam = "0.28" - pollster = "0.3.0" log = "0.4" +sysinfo = "0.30" +chrono = "0.4" + +# serde +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" + downcast-rs = "1.2.0" [target.'cfg(target_os = "android")'.dependencies] diff --git a/crates/vent-runtime/src/lib.rs b/crates/vent-runtime/src/lib.rs index 630fcb3..c42e2dd 100644 --- a/crates/vent-runtime/src/lib.rs +++ b/crates/vent-runtime/src/lib.rs @@ -2,14 +2,16 @@ use std::process::exit; use crate::render::Dimension; +use project::{RenderSettings, VentApplicationProject}; use render::{camera::camera_controller3d::CameraController3D, DefaultRuntimeRenderer}; -use vent_common::project::VentApplicationProject; -use vent_common::util::crash::init_panic_hook; +use util::{crash::init_panic_hook, version::Version}; use vent_logging::Logger; use vent_window::{EventLoop, Window, WindowAttribs, WindowEvent}; +pub mod project; pub mod render; +pub mod util; pub struct VentApplication { project: VentApplicationProject, @@ -22,7 +24,12 @@ impl VentApplication { let project = VentApplicationProject { name: "Placeholder".to_string(), - version: "1.0.0".to_string(), + version: Version::new(1, 0, 0), + window_settings: WindowAttribs::default().with_title("Placeholder".to_string()), + render_settings: RenderSettings { + dimension: Dimension::D3, + vsync: false, + }, }; let app = VentApplication::new(project); app.start(); @@ -33,18 +40,17 @@ impl VentApplication { } pub fn start(self) { - let window_size = (800, 600); + let project = self.project; let mut event_loop = EventLoop::new(); - let attribs = WindowAttribs::default().with_title(self.project.name); - let vent_window = Window::new(attribs); + let app_window = Window::new(project.window_settings.clone()); // TODO - let mut renderer = DefaultRuntimeRenderer::new(Dimension::D3, &vent_window); + let mut renderer = DefaultRuntimeRenderer::new(&project, &app_window); let mut controller = CameraController3D::new(1000.0, 10.0); let mut delta_time = 0.0; - event_loop.add_window(vent_window); + event_loop.add_window(app_window); event_loop.poll(move |event| { match event { @@ -66,7 +72,7 @@ impl VentApplication { } => { renderer.resize((new_width, new_height)); } - WindowEvent::Draw => delta_time = renderer.render(window_size), // Default, + WindowEvent::Draw => delta_time = renderer.render(), // Default, } }); diff --git a/crates/vent-common/src/project/mod.rs b/crates/vent-runtime/src/project/mod.rs similarity index 58% rename from crates/vent-common/src/project/mod.rs rename to crates/vent-runtime/src/project/mod.rs index 76795b8..dfbd466 100644 --- a/crates/vent-common/src/project/mod.rs +++ b/crates/vent-runtime/src/project/mod.rs @@ -1,11 +1,27 @@ use serde::{Deserialize, Serialize}; use std::fs::{File, OpenOptions}; +use vent_window::WindowAttribs; +use crate::{render::Dimension, util::version::Version}; + +/// Basic Project Information's #[derive(Serialize, Deserialize)] -// Basic Project Information's pub struct VentApplicationProject { + // Name of the Application pub name: String, - pub version: String, + // Version of the Application + pub version: Version, + // Inital Window settings, can be changed later + pub window_settings: WindowAttribs, + // Inital Render settings, can be changed later + pub render_settings: RenderSettings, +} + +#[derive(Serialize, Deserialize)] +pub struct RenderSettings { + // Inital vsync setting, can be changed later + pub dimension: Dimension, + pub vsync: bool, } impl VentApplicationProject { @@ -17,7 +33,7 @@ impl VentApplicationProject { .truncate(true) .create(true) .open(path_str)?; - + log::debug!("Saving {}", path); serde_json::to_writer(file, self)?; Ok(()) @@ -26,6 +42,7 @@ impl VentApplicationProject { // Serialize the project data from a .vent file pub fn serialize(path: &str) -> Result { let path_str = format!("{}/project.vent", path); + log::debug!("Loading {}", path); let file = File::open(path_str)?; let project = serde_json::from_reader(file)?; diff --git a/crates/vent-runtime/src/render/camera/mod.rs b/crates/vent-runtime/src/render/camera/mod.rs index ef69ad7..26fb30d 100644 --- a/crates/vent-runtime/src/render/camera/mod.rs +++ b/crates/vent-runtime/src/render/camera/mod.rs @@ -83,7 +83,7 @@ impl Camera for Camera3D { fn recreate_projection(&mut self, aspect_ratio: f32) { self.ubo.projection = Mat4::perspective_rh(self.fovy.to_radians(), aspect_ratio, self.znear, self.zfar); - // Flip the cameras prospective upside down as glam assumes that the renderer we are using renders top to bottom, vulkan is the opposite + // Flip the cameras prospective upside down self.ubo.projection.y_axis.y *= -1.0; } } diff --git a/crates/vent-runtime/src/render/mod.rs b/crates/vent-runtime/src/render/mod.rs index eeea17c..c898d86 100644 --- a/crates/vent-runtime/src/render/mod.rs +++ b/crates/vent-runtime/src/render/mod.rs @@ -2,8 +2,11 @@ use std::time::{Duration, Instant}; use ash::vk; use gui::gui_renderer::GuiRenderer; +use serde::{Deserialize, Serialize}; use vent_rendering::instance::VulkanInstance; +use crate::project::{RenderSettings, VentApplicationProject}; + use self::camera::{from_dimension, Camera}; use self::d2::Renderer2D; use self::d3::Renderer3D; @@ -25,10 +28,16 @@ pub(crate) struct DefaultRuntimeRenderer { } impl DefaultRuntimeRenderer { - pub(crate) fn new(dimension: Dimension, window: &vent_window::Window) -> Self { - let instance = VulkanInstance::new("TODO", window); + pub(crate) fn new(settings: &VentApplicationProject, window: &vent_window::Window) -> Self { + let instance = VulkanInstance::new( + &settings.name, + settings.version.parse(), + settings.render_settings.vsync, + window, + ); + let dimension = &settings.render_settings.dimension; let window_size = window.size(); - let mut camera = from_dimension(window_size.0 as f32 / window_size.1 as f32, &dimension); + let mut camera = from_dimension(window_size.0 as f32 / window_size.1 as f32, dimension); let runtime_renderer = RawRuntimeRenderer::new(dimension, &instance, camera.as_mut()); Self { instance, @@ -41,9 +50,9 @@ impl DefaultRuntimeRenderer { self.runtime_renderer.progress_event(event); } - pub(crate) fn render(&mut self, window_size: (u32, u32)) -> f32 { + pub(crate) fn render(&mut self) -> f32 { self.runtime_renderer - .render(&mut self.instance, window_size, self.camera.as_mut()) + .render(&mut self.instance, self.camera.as_mut()) } pub(crate) fn resize(&mut self, new_size: (u32, u32)) { @@ -66,6 +75,7 @@ impl Drop for DefaultRuntimeRenderer { } } +#[derive(Serialize, Deserialize)] pub enum Dimension { D2, D3, @@ -100,7 +110,7 @@ pub struct RawRuntimeRenderer { } impl RawRuntimeRenderer { - pub fn new(dimension: Dimension, instance: &VulkanInstance, camera: &mut dyn Camera) -> Self { + pub fn new(dimension: &Dimension, instance: &VulkanInstance, camera: &mut dyn Camera) -> Self { let multi_renderer: Box = match dimension { Dimension::D2 => Box::new(Renderer2D::init(instance, camera)), Dimension::D3 => Box::new(Renderer3D::init(instance, camera)), @@ -123,12 +133,7 @@ impl RawRuntimeRenderer { } } - pub fn render( - &mut self, - instance: &mut VulkanInstance, - window_size: (u32, u32), // TODO - camera: &mut dyn Camera, - ) -> f32 { + pub fn render(&mut self, instance: &mut VulkanInstance, camera: &mut dyn Camera) -> f32 { let frame_start = Instant::now(); let image = instance.next_image(); @@ -139,11 +144,11 @@ impl RawRuntimeRenderer { let result = instance.submit(image_index); if let Err(vk::Result::ERROR_OUT_OF_DATE_KHR | vk::Result::SUBOPTIMAL_KHR) = result { - instance.recreate_swap_chain(window_size); + instance.recreate_swap_chain(None); } } Err(vk::Result::ERROR_OUT_OF_DATE_KHR) => { - instance.recreate_swap_chain(window_size); + instance.recreate_swap_chain(None); } Err(_) => {} } @@ -207,7 +212,7 @@ impl RawRuntimeRenderer { camera: &mut dyn Camera, ) { // Uses the NEW Resized config - instance.recreate_swap_chain(new_size); + instance.recreate_swap_chain(Some(new_size)); self.multi_renderer.resize(instance, new_size, camera) } diff --git a/crates/vent-common/src/util/crash/mod.rs b/crates/vent-runtime/src/util/crash.rs similarity index 87% rename from crates/vent-common/src/util/crash/mod.rs rename to crates/vent-runtime/src/util/crash.rs index de73788..445b980 100644 --- a/crates/vent-common/src/util/crash/mod.rs +++ b/crates/vent-runtime/src/util/crash.rs @@ -4,6 +4,8 @@ use std::io::Write; use std::panic::{self, PanicInfo}; use sysinfo::System; +// Crash Handler + #[inline] pub fn init_panic_hook() { panic::set_hook(Box::new(panic_handler)); @@ -63,11 +65,3 @@ fn log_crash(pi: &PanicInfo) -> std::io::Result<()> { writeln!(&mut f, "Crash Info: {:?}", format!("{:?}", pi))?; Ok(()) } - -// fn show_error_dialog(pi: &PanicInfo) { -// rfd::MessageDialog::new() -// .set_level(MessageLevel::Error) -// .set_title("Application Error") -// .set_description(format!("{:?}", pi).as_str()) -// .show(); -// } diff --git a/crates/vent-runtime/src/util/mod.rs b/crates/vent-runtime/src/util/mod.rs new file mode 100644 index 0000000..f6aa4f5 --- /dev/null +++ b/crates/vent-runtime/src/util/mod.rs @@ -0,0 +1,2 @@ +pub mod crash; +pub mod version; diff --git a/crates/vent-runtime/src/util/version.rs b/crates/vent-runtime/src/util/version.rs new file mode 100644 index 0000000..08b8689 --- /dev/null +++ b/crates/vent-runtime/src/util/version.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub struct Version { + pub major: u32, + pub minor: u32, + pub patch: u32, +} + +impl Version { + pub const fn new(major: u32, minor: u32, patch: u32) -> Self { + Version { + major, + minor, + patch, + } + } + + pub fn parse(&self) -> u32 { + ((self.major) << 22) | ((self.minor) << 12) | (self.patch) + } +} diff --git a/crates/vent-window/Cargo.toml b/crates/vent-window/Cargo.toml index a90bce0..977146b 100644 --- a/crates/vent-window/Cargo.toml +++ b/crates/vent-window/Cargo.toml @@ -20,6 +20,10 @@ raw-window-handle = { version = "0.6", features = [ "std", ]} +# serde +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" + [target.'cfg(all(unix, not(any(target_os = "redox", target_family = "wasm", target_os = "android", target_os = "ios", target_os = "macos"))))'.dependencies] wayland-backend = { version = "0.3", features = ["client_system"] } wayland-client = { version = "0.31" } diff --git a/crates/vent-window/src/lib.rs b/crates/vent-window/src/lib.rs index 655a85a..a8e4a94 100644 --- a/crates/vent-window/src/lib.rs +++ b/crates/vent-window/src/lib.rs @@ -1,6 +1,7 @@ use std::num::NonZeroU32; use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle}; +use serde::{Deserialize, Serialize}; pub mod keyboard; pub mod mouse; pub mod platform; @@ -25,7 +26,7 @@ pub enum WindowEvent { enum WindowError {} -#[derive(PartialEq)] +#[derive(Serialize, Deserialize, PartialEq, Clone)] pub enum WindowMode { Default, FullScreen, @@ -33,6 +34,7 @@ pub enum WindowMode { Minimized, } +#[derive(Serialize, Deserialize, Clone)] pub struct WindowAttribs { title: String, app_id: String, diff --git a/test/runtime-sandbox/Cargo.toml b/test/runtime-sandbox/Cargo.toml index 771ecac..77704ee 100644 --- a/test/runtime-sandbox/Cargo.toml +++ b/test/runtime-sandbox/Cargo.toml @@ -7,4 +7,3 @@ edition = "2021" [dependencies] vent-runtime = { path = "../../crates/vent-runtime"} -vent-common = { path = "../../crates/vent-common"}