From a19661daa0a87572ab618e48f2e8adc44f762b5d Mon Sep 17 00:00:00 2001 From: zack Date: Sat, 28 Dec 2024 21:46:32 -0500 Subject: [PATCH] add (profiler) --- Cargo.lock | 129 ++++++++++++++++++++ {crates/vk-rs/assets => assets}/suzanne.obj | 0 crates/vk-rs/Cargo.toml | 4 +- crates/vk-rs/src/main.rs | 43 ++++++- crates/vk-rs/src/renderer.rs | 11 +- 5 files changed, 179 insertions(+), 8 deletions(-) rename {crates/vk-rs/assets => assets}/suzanne.obj (100%) diff --git a/Cargo.lock b/Cargo.lock index 0e08796..3d0b52d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -216,6 +216,15 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -288,6 +297,12 @@ dependencies = [ "syn", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.9.0" @@ -543,6 +558,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + [[package]] name = "derive_more" version = "0.99.18" @@ -848,6 +872,15 @@ dependencies = [ "windows", ] +[[package]] +name = "gpu-profiler" +version = "0.1.0" +dependencies = [ + "ash", + "once_cell", + "puffin", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -1046,6 +1079,7 @@ checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", "hashbrown 0.15.2", + "serde", ] [[package]] @@ -1193,6 +1227,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3bd0dd2cd90571056fdb71f6275fada10131182f84899f4b2a916e565d81d86" +[[package]] +name = "lz4_flex" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75761162ae2b0e580d7e7c390558127e5f01b4194debd6221fd8c207fc80e3f5" + [[package]] name = "malloc_buf" version = "0.0.6" @@ -1235,6 +1275,12 @@ dependencies = [ "adler2", ] +[[package]] +name = "natord" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308d96db8debc727c3fd9744aac51751243420e46edf401010908da7f8d5e57c" + [[package]] name = "ndk" version = "0.8.0" @@ -1296,6 +1342,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-traits" version = "0.2.19" @@ -1685,6 +1737,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "presser" version = "0.3.1" @@ -1709,6 +1767,37 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "puffin" +version = "0.19.0" +source = "git+https://github.com/EmbarkStudios/puffin?rev=5ac4e54164ee89bd68c29f288c2b5613afc2c929#5ac4e54164ee89bd68c29f288c2b5613afc2c929" +dependencies = [ + "anyhow", + "bincode", + "byteorder", + "cfg-if", + "lz4_flex", + "once_cell", + "parking_lot", + "serde", +] + +[[package]] +name = "puffin_egui" +version = "0.25.0" +source = "git+https://github.com/EmbarkStudios/puffin?rev=5ac4e54164ee89bd68c29f288c2b5613afc2c929#5ac4e54164ee89bd68c29f288c2b5613afc2c929" +dependencies = [ + "egui", + "indexmap", + "natord", + "once_cell", + "parking_lot", + "puffin", + "time", + "vec1", + "web-time 0.2.4", +] + [[package]] name = "quick-xml" version = "0.36.2" @@ -2305,6 +2394,37 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.3.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tiny-skia" version = "0.11.4" @@ -2476,6 +2596,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vec1" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab68b56840f69efb0fefbe3ab6661499217ffdc58e2eef7c3f6f69835386322" + [[package]] name = "version_check" version = "0.9.5" @@ -2495,6 +2621,9 @@ dependencies = [ "egui-ash", "glam 0.22.0", "gpu-allocator", + "gpu-profiler", + "puffin", + "puffin_egui", "raw-window-handle 0.6.2", "shaders-shared", "spirv-builder", diff --git a/crates/vk-rs/assets/suzanne.obj b/assets/suzanne.obj similarity index 100% rename from crates/vk-rs/assets/suzanne.obj rename to assets/suzanne.obj diff --git a/crates/vk-rs/Cargo.toml b/crates/vk-rs/Cargo.toml index 8a144ae..b23dad3 100644 --- a/crates/vk-rs/Cargo.toml +++ b/crates/vk-rs/Cargo.toml @@ -16,8 +16,10 @@ egui-ash = { version = "0.4.0", features = ["gpu-allocator"] } tobj = "4.0.2" egui = "0.25.0" ash-window = "0.12.0" - shaders-shared = { path = "../shaders-shared" } +puffin = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" } +puffin_egui = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" } +gpu-profiler = { path = "../../../gpu-profiler", features = ["use-ash"] } [build-dependencies] spirv-builder.workspace = true diff --git a/crates/vk-rs/src/main.rs b/crates/vk-rs/src/main.rs index d2ba5e9..dd37e92 100644 --- a/crates/vk-rs/src/main.rs +++ b/crates/vk-rs/src/main.rs @@ -51,10 +51,13 @@ struct Game { last_fps_update: std::time::Instant, frame_count_since_last_update: i32, current_fps: f32, + + show_profiler: bool, } impl App for Game { fn ui(&mut self, ctx: &egui::Context) { + puffin::GlobalProfiler::lock().new_frame(); let now = std::time::Instant::now(); self.frame_count_since_last_update += 1; @@ -65,6 +68,10 @@ impl App for Game { self.last_fps_update = now; } + if self.show_profiler { + puffin_egui::profiler_window(ctx); + } + egui::SidePanel::left("my_side_panel").show(ctx, |ui| { ui.separator(); ui.horizontal(|ui| { @@ -97,6 +104,10 @@ impl App for Game { ui.label("Z:"); ui.add(egui::DragValue::new(&mut self.camera_position.z).speed(0.1)); }); + ui.separator(); + if ui.button("Show Profiler").clicked() { + self.show_profiler = !self.show_profiler; + } ui.label(format!("FPS: {:.1}", self.current_fps)); }); @@ -168,6 +179,7 @@ impl App for Game { } fn request_redraw(&mut self, _viewport_id: egui::ViewportId) -> HandleRedraw { + puffin::profile_function!(); HandleRedraw::Handle(Box::new({ let renderer = self.renderer.clone(); let rotate_y = self.rotate_y; @@ -333,10 +345,15 @@ impl MyAppCreator { surface_loader: &Surface, surface: vk::SurfaceKHR, required_device_extensions: &[CString], - ) -> (vk::PhysicalDevice, vk::PhysicalDeviceMemoryProperties, u32) { + ) -> ( + vk::PhysicalDevice, + vk::PhysicalDeviceProperties, + vk::PhysicalDeviceMemoryProperties, + u32, + ) { let mut queue_family_index: Option = None; - let (physical_device, physical_device_memory_properties) = { + let (physical_device, phsyical_device_properties, physical_device_memory_properties) = { let physical_devices = unsafe { instance .enumerate_physical_devices() @@ -414,11 +431,19 @@ impl MyAppCreator { let physical_device_memory_properties = unsafe { instance.get_physical_device_memory_properties(physical_device) }; - (physical_device, physical_device_memory_properties) + let physical_device_properties = + unsafe { instance.get_physical_device_properties(physical_device) }; + + ( + physical_device, + physical_device_properties, + physical_device_memory_properties, + ) }; ( physical_device, + phsyical_device_properties, physical_device_memory_properties, queue_family_index.unwrap() as u32, ) @@ -494,8 +519,12 @@ impl AppCreator>> for MyAppCreator { for ext in &cc.required_device_extensions { req_ext.push(ext.to_owned()); } - let (physical_device, _physical_device_memory_properties, queue_family_index) = - Self::create_physical_device(&instance, &surface_loader, surface, &req_ext); + let ( + physical_device, + physical_device_properties, + _physical_device_memory_properties, + queue_family_index, + ) = Self::create_physical_device(&instance, &surface_loader, surface, &req_ext); let (device, queue) = Self::create_device( &instance, physical_device, @@ -536,6 +565,8 @@ impl AppCreator>> for MyAppCreator { command_pool, allocator: ManuallyDrop::new(allocator.clone()), + show_profiler: false, + renderer: Renderer::new( physical_device, device, @@ -584,6 +615,8 @@ impl AppCreator>> for MyAppCreator { } fn main() -> std::process::ExitCode { + puffin::set_scopes_on(true); + egui_ash::run( "vulkan", MyAppCreator, diff --git a/crates/vk-rs/src/renderer.rs b/crates/vk-rs/src/renderer.rs index d19ad03..a9d7cd4 100644 --- a/crates/vk-rs/src/renderer.rs +++ b/crates/vk-rs/src/renderer.rs @@ -181,7 +181,7 @@ impl RendererInner { .queue_family_indices(&queue_family_indices) .pre_transform(surface_capabilities.current_transform) .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE) - .present_mode(vk::PresentModeKHR::FIFO) + .present_mode(vk::PresentModeKHR::MAILBOX) .clipped(true); let swapchain = unsafe { @@ -607,7 +607,7 @@ impl RendererInner { let mut allocator = allocator.lock().unwrap(); let vertices = { let model_obj = tobj::load_obj( - "./crates/vk-rs/assets/suzanne.obj", + "./assets/suzanne.obj", &tobj::LoadOptions { single_index: true, triangulate: true, @@ -1099,6 +1099,7 @@ impl RendererInner { } pub fn render(&mut self, width: u32, height: u32, mut egui_cmd: EguiCommand, rotate_y: f32) { + puffin::profile_function!(); if width == 0 || height == 0 { return; } @@ -1113,10 +1114,12 @@ impl RendererInner { || height != self.height || egui_cmd.swapchain_recreate_required() { + puffin::profile_scope!("recreate_swapchain"); self.recreate_swapchain(width, height, &mut egui_cmd); } let result = unsafe { + puffin::profile_scope!("acquire_next_image"); self.swapchain_loader.acquire_next_image( self.swapchain, u64::MAX, @@ -1134,6 +1137,7 @@ impl RendererInner { }; unsafe { + puffin::profile_scope!("wait_for_fences"); self.device.wait_for_fences( std::slice::from_ref(&self.in_flight_fences[self.current_frame]), true, @@ -1150,6 +1154,7 @@ impl RendererInner { .expect("Failed to reset fences"); let view = { + puffin::profile_scope!("calculate_view"); let (sin_pitch, cos_pitch) = self.camera_pitch.sin_cos(); let (sin_yaw, cos_yaw) = self.camera_yaw.sin_cos(); @@ -1185,6 +1190,7 @@ impl RendererInner { let command_buffer_begin_info = vk::CommandBufferBeginInfo::builder() .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); unsafe { + puffin::profile_scope!("render"); self.device .begin_command_buffer( self.command_buffers[self.current_frame], @@ -1287,6 +1293,7 @@ impl RendererInner { &self.render_finished_semaphores[self.current_frame], )); unsafe { + puffin::profile_scope!("queue_submit"); self.device .queue_submit( self.queue,