use once_cell::sync::Lazy;
use parking_lot::{Mutex, MutexGuard};
use std::sync::OnceLock;
static WGPU_PROFILER: OnceLock<Mutex<wgpu_profiler::GpuProfiler>> = OnceLock::new();
pub static PUFFIN_GPU_PROFILER: Lazy<Mutex<puffin::GlobalProfiler>> =
Lazy::new(|| Mutex::new(puffin::GlobalProfiler::default()));
pub fn init_wgpu_profiler(device: &wgpu::Device) {
match wgpu_profiler::GpuProfiler::new(device, wgpu_profiler::GpuProfilerSettings::default()) {
Ok(profiler) => {
if WGPU_PROFILER.set(Mutex::new(profiler)).is_err() {
tracing::warn!("WGPU profiler was already initialized");
}
}
Err(err) => tracing::error!("Could not create the WGPU profiler: {err}"),
}
}
#[allow(clippy::expect_used)]
pub fn wgpu_profiler() -> MutexGuard<'static, wgpu_profiler::GpuProfiler> {
WGPU_PROFILER
.get()
.expect("WGPU profiler is initialized when the render thread starts")
.lock()
}