From 724191250a703347e9b77f6daa5fd65dcb4322a6 Mon Sep 17 00:00:00 2001 From: Anders Kaare Straadt Date: Mon, 18 Apr 2022 11:46:08 +0200 Subject: [PATCH 1/2] Show memory report in debug builds log::debug!() after wgpuSwapChainPresent(); assumed to be called roughly once per frame; useful for detecting memory leaks --- src/device.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/device.rs b/src/device.rs index 2ab91fdc..44b004b3 100644 --- a/src/device.rs +++ b/src/device.rs @@ -782,6 +782,8 @@ pub extern "C" fn wgpuSwapChainPresent(swap_chain: id::SurfaceId) { let device_id = get_device_from_surface(surface_id); gfx_select!(device_id => GLOBAL.surface_present(surface_id)) .expect("Unable to present swap chain"); + #[cfg(debug_assertions)] + log::debug!("Memory report: {:?}", GLOBAL.generate_report()); } #[no_mangle] From 6692ce08883e44598f3a41e929d883489a950ce5 Mon Sep 17 00:00:00 2001 From: Anders Kaare Date: Fri, 29 Apr 2022 15:48:46 +0200 Subject: [PATCH 2/2] wgpuGetResourceUsageString() Returns resource usage as C string --- ffi/wgpu.h | 3 +++ src/device.rs | 2 -- src/lib.rs | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ffi/wgpu.h b/ffi/wgpu.h index 49781794..356fe306 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -70,6 +70,9 @@ void wgpuSetLogLevel(WGPULogLevel level); uint32_t wgpuGetVersion(void); +// Returns resource usage C string; caller owns the string and must free() it +char* wgpuGetResourceUsageString(); + void wgpuRenderPassEncoderSetPushConstants(WGPURenderPassEncoder encoder, WGPUShaderStageFlags stages, uint32_t offset, uint32_t sizeBytes, void* const data); void wgpuBufferDrop(WGPUBuffer buffer); diff --git a/src/device.rs b/src/device.rs index a5b25da4..3ab05a57 100644 --- a/src/device.rs +++ b/src/device.rs @@ -779,8 +779,6 @@ pub extern "C" fn wgpuSwapChainPresent(swap_chain: id::SurfaceId) { let device_id = get_device_from_surface(surface_id); gfx_select!(device_id => GLOBAL.surface_present(surface_id)) .expect("Unable to present swap chain"); - #[cfg(debug_assertions)] - log::debug!("Memory report: {:?}", GLOBAL.generate_report()); } #[no_mangle] diff --git a/src/lib.rs b/src/lib.rs index 57012cd7..fd0fb758 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -213,6 +213,14 @@ pub extern "C" fn wgpuCreateInstance( 8 as native::WGPUInstance } +#[no_mangle] +pub extern "C" fn wgpuGetResourceUsageString() -> *const std::os::raw::c_char { + let c_string = CString::new(format!("{:?}", GLOBAL.generate_report())).unwrap(); + let ptr = c_string.as_ptr(); + std::mem::forget(c_string); + ptr +} + #[no_mangle] pub unsafe extern "C" fn wgpuInstanceCreateSurface( _: native::WGPUInstance,