add (profiler)

This commit is contained in:
zack 2024-12-28 21:46:32 -05:00
parent ab8e78e1b1
commit a19661daa0
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35
5 changed files with 179 additions and 8 deletions

129
Cargo.lock generated
View file

@ -216,6 +216,15 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -288,6 +297,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.9.0" version = "1.9.0"
@ -543,6 +558,15 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991"
[[package]]
name = "deranged"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
dependencies = [
"powerfmt",
]
[[package]] [[package]]
name = "derive_more" name = "derive_more"
version = "0.99.18" version = "0.99.18"
@ -848,6 +872,15 @@ dependencies = [
"windows", "windows",
] ]
[[package]]
name = "gpu-profiler"
version = "0.1.0"
dependencies = [
"ash",
"once_cell",
"puffin",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.5" version = "0.14.5"
@ -1046,6 +1079,7 @@ checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown 0.15.2", "hashbrown 0.15.2",
"serde",
] ]
[[package]] [[package]]
@ -1193,6 +1227,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3bd0dd2cd90571056fdb71f6275fada10131182f84899f4b2a916e565d81d86" checksum = "b3bd0dd2cd90571056fdb71f6275fada10131182f84899f4b2a916e565d81d86"
[[package]]
name = "lz4_flex"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75761162ae2b0e580d7e7c390558127e5f01b4194debd6221fd8c207fc80e3f5"
[[package]] [[package]]
name = "malloc_buf" name = "malloc_buf"
version = "0.0.6" version = "0.0.6"
@ -1235,6 +1275,12 @@ dependencies = [
"adler2", "adler2",
] ]
[[package]]
name = "natord"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308d96db8debc727c3fd9744aac51751243420e46edf401010908da7f8d5e57c"
[[package]] [[package]]
name = "ndk" name = "ndk"
version = "0.8.0" version = "0.8.0"
@ -1296,6 +1342,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.19" version = "0.2.19"
@ -1685,6 +1737,12 @@ dependencies = [
"windows-sys 0.59.0", "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]] [[package]]
name = "presser" name = "presser"
version = "0.3.1" version = "0.3.1"
@ -1709,6 +1767,37 @@ dependencies = [
"unicode-ident", "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]] [[package]]
name = "quick-xml" name = "quick-xml"
version = "0.36.2" version = "0.36.2"
@ -2305,6 +2394,37 @@ dependencies = [
"once_cell", "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]] [[package]]
name = "tiny-skia" name = "tiny-skia"
version = "0.11.4" version = "0.11.4"
@ -2476,6 +2596,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vec1"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eab68b56840f69efb0fefbe3ab6661499217ffdc58e2eef7c3f6f69835386322"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.5" version = "0.9.5"
@ -2495,6 +2621,9 @@ dependencies = [
"egui-ash", "egui-ash",
"glam 0.22.0", "glam 0.22.0",
"gpu-allocator", "gpu-allocator",
"gpu-profiler",
"puffin",
"puffin_egui",
"raw-window-handle 0.6.2", "raw-window-handle 0.6.2",
"shaders-shared", "shaders-shared",
"spirv-builder", "spirv-builder",

View file

@ -16,8 +16,10 @@ egui-ash = { version = "0.4.0", features = ["gpu-allocator"] }
tobj = "4.0.2" tobj = "4.0.2"
egui = "0.25.0" egui = "0.25.0"
ash-window = "0.12.0" ash-window = "0.12.0"
shaders-shared = { path = "../shaders-shared" } 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] [build-dependencies]
spirv-builder.workspace = true spirv-builder.workspace = true

View file

@ -51,10 +51,13 @@ struct Game {
last_fps_update: std::time::Instant, last_fps_update: std::time::Instant,
frame_count_since_last_update: i32, frame_count_since_last_update: i32,
current_fps: f32, current_fps: f32,
show_profiler: bool,
} }
impl App for Game { impl App for Game {
fn ui(&mut self, ctx: &egui::Context) { fn ui(&mut self, ctx: &egui::Context) {
puffin::GlobalProfiler::lock().new_frame();
let now = std::time::Instant::now(); let now = std::time::Instant::now();
self.frame_count_since_last_update += 1; self.frame_count_since_last_update += 1;
@ -65,6 +68,10 @@ impl App for Game {
self.last_fps_update = now; self.last_fps_update = now;
} }
if self.show_profiler {
puffin_egui::profiler_window(ctx);
}
egui::SidePanel::left("my_side_panel").show(ctx, |ui| { egui::SidePanel::left("my_side_panel").show(ctx, |ui| {
ui.separator(); ui.separator();
ui.horizontal(|ui| { ui.horizontal(|ui| {
@ -97,6 +104,10 @@ impl App for Game {
ui.label("Z:"); ui.label("Z:");
ui.add(egui::DragValue::new(&mut self.camera_position.z).speed(0.1)); 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)); 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 { fn request_redraw(&mut self, _viewport_id: egui::ViewportId) -> HandleRedraw {
puffin::profile_function!();
HandleRedraw::Handle(Box::new({ HandleRedraw::Handle(Box::new({
let renderer = self.renderer.clone(); let renderer = self.renderer.clone();
let rotate_y = self.rotate_y; let rotate_y = self.rotate_y;
@ -333,10 +345,15 @@ impl MyAppCreator {
surface_loader: &Surface, surface_loader: &Surface,
surface: vk::SurfaceKHR, surface: vk::SurfaceKHR,
required_device_extensions: &[CString], required_device_extensions: &[CString],
) -> (vk::PhysicalDevice, vk::PhysicalDeviceMemoryProperties, u32) { ) -> (
vk::PhysicalDevice,
vk::PhysicalDeviceProperties,
vk::PhysicalDeviceMemoryProperties,
u32,
) {
let mut queue_family_index: Option<usize> = None; let mut queue_family_index: Option<usize> = None;
let (physical_device, physical_device_memory_properties) = { let (physical_device, phsyical_device_properties, physical_device_memory_properties) = {
let physical_devices = unsafe { let physical_devices = unsafe {
instance instance
.enumerate_physical_devices() .enumerate_physical_devices()
@ -414,11 +431,19 @@ impl MyAppCreator {
let physical_device_memory_properties = let physical_device_memory_properties =
unsafe { instance.get_physical_device_memory_properties(physical_device) }; 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, physical_device,
phsyical_device_properties,
physical_device_memory_properties, physical_device_memory_properties,
queue_family_index.unwrap() as u32, queue_family_index.unwrap() as u32,
) )
@ -494,8 +519,12 @@ impl AppCreator<Arc<Mutex<Allocator>>> for MyAppCreator {
for ext in &cc.required_device_extensions { for ext in &cc.required_device_extensions {
req_ext.push(ext.to_owned()); req_ext.push(ext.to_owned());
} }
let (physical_device, _physical_device_memory_properties, queue_family_index) = let (
Self::create_physical_device(&instance, &surface_loader, surface, &req_ext); 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( let (device, queue) = Self::create_device(
&instance, &instance,
physical_device, physical_device,
@ -536,6 +565,8 @@ impl AppCreator<Arc<Mutex<Allocator>>> for MyAppCreator {
command_pool, command_pool,
allocator: ManuallyDrop::new(allocator.clone()), allocator: ManuallyDrop::new(allocator.clone()),
show_profiler: false,
renderer: Renderer::new( renderer: Renderer::new(
physical_device, physical_device,
device, device,
@ -584,6 +615,8 @@ impl AppCreator<Arc<Mutex<Allocator>>> for MyAppCreator {
} }
fn main() -> std::process::ExitCode { fn main() -> std::process::ExitCode {
puffin::set_scopes_on(true);
egui_ash::run( egui_ash::run(
"vulkan", "vulkan",
MyAppCreator, MyAppCreator,

View file

@ -181,7 +181,7 @@ impl RendererInner {
.queue_family_indices(&queue_family_indices) .queue_family_indices(&queue_family_indices)
.pre_transform(surface_capabilities.current_transform) .pre_transform(surface_capabilities.current_transform)
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE) .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
.present_mode(vk::PresentModeKHR::FIFO) .present_mode(vk::PresentModeKHR::MAILBOX)
.clipped(true); .clipped(true);
let swapchain = unsafe { let swapchain = unsafe {
@ -607,7 +607,7 @@ impl RendererInner {
let mut allocator = allocator.lock().unwrap(); let mut allocator = allocator.lock().unwrap();
let vertices = { let vertices = {
let model_obj = tobj::load_obj( let model_obj = tobj::load_obj(
"./crates/vk-rs/assets/suzanne.obj", "./assets/suzanne.obj",
&tobj::LoadOptions { &tobj::LoadOptions {
single_index: true, single_index: true,
triangulate: 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) { pub fn render(&mut self, width: u32, height: u32, mut egui_cmd: EguiCommand, rotate_y: f32) {
puffin::profile_function!();
if width == 0 || height == 0 { if width == 0 || height == 0 {
return; return;
} }
@ -1113,10 +1114,12 @@ impl RendererInner {
|| height != self.height || height != self.height
|| egui_cmd.swapchain_recreate_required() || egui_cmd.swapchain_recreate_required()
{ {
puffin::profile_scope!("recreate_swapchain");
self.recreate_swapchain(width, height, &mut egui_cmd); self.recreate_swapchain(width, height, &mut egui_cmd);
} }
let result = unsafe { let result = unsafe {
puffin::profile_scope!("acquire_next_image");
self.swapchain_loader.acquire_next_image( self.swapchain_loader.acquire_next_image(
self.swapchain, self.swapchain,
u64::MAX, u64::MAX,
@ -1134,6 +1137,7 @@ impl RendererInner {
}; };
unsafe { unsafe {
puffin::profile_scope!("wait_for_fences");
self.device.wait_for_fences( self.device.wait_for_fences(
std::slice::from_ref(&self.in_flight_fences[self.current_frame]), std::slice::from_ref(&self.in_flight_fences[self.current_frame]),
true, true,
@ -1150,6 +1154,7 @@ impl RendererInner {
.expect("Failed to reset fences"); .expect("Failed to reset fences");
let view = { let view = {
puffin::profile_scope!("calculate_view");
let (sin_pitch, cos_pitch) = self.camera_pitch.sin_cos(); let (sin_pitch, cos_pitch) = self.camera_pitch.sin_cos();
let (sin_yaw, cos_yaw) = self.camera_yaw.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() let command_buffer_begin_info = vk::CommandBufferBeginInfo::builder()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);
unsafe { unsafe {
puffin::profile_scope!("render");
self.device self.device
.begin_command_buffer( .begin_command_buffer(
self.command_buffers[self.current_frame], self.command_buffers[self.current_frame],
@ -1287,6 +1293,7 @@ impl RendererInner {
&self.render_finished_semaphores[self.current_frame], &self.render_finished_semaphores[self.current_frame],
)); ));
unsafe { unsafe {
puffin::profile_scope!("queue_submit");
self.device self.device
.queue_submit( .queue_submit(
self.queue, self.queue,