From cd7958561adeb40e786c62f51ea10fd2b98e8639 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Tue, 6 Jan 2026 16:33:07 -0800 Subject: [PATCH] Restore tracing support in wgpu The loop/break changes in `play.rs` are reverting changes made in #4202. Fixes #5974 --- CHANGELOG.md | 1 + Cargo.lock | 2 +- examples/features/Cargo.toml | 10 ++++----- examples/features/src/hello_triangle/mod.rs | 5 ++++- naga/src/back/glsl/mod.rs | 5 ++--- naga/src/back/hlsl/mod.rs | 20 +++++++---------- player/src/bin/play.rs | 25 +++++++++++++++------ player/src/lib.rs | 15 +++++++++++++ wgpu-core/src/present.rs | 4 ++-- wgpu-types/src/device.rs | 3 --- wgpu/Cargo.toml | 3 +-- wgpu/src/backend/wgpu_core.rs | 18 --------------- 12 files changed, 56 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80cd23370..25771ea3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ Bottom level categories: - BREAKING: Migrated from the `maxInterStageShaderComponents` limit to `maxInterStageShaderVariables`, which changes validation in a way that should not affect most programs. This follows the latest changes of the WebGPU spec. By @ErichDonGubler in [#8652](https://github.com/gfx-rs/wgpu/pull/8652), [#8792](https://github.com/gfx-rs/wgpu/pull/8792). - Fixed validation of the texture format in GPUDepthStencilState when neither depth nor stencil is actually enabled. By @andyleiserson in [#8766](https://github.com/gfx-rs/wgpu/pull/8766). +- Tracing support has been restored. By @andyleiserson in [#8429](https://github.com/gfx-rs/wgpu/pull/8429). #### naga diff --git a/Cargo.lock b/Cargo.lock index 176f5b411..d489faf2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4816,6 +4816,7 @@ dependencies = [ "obj", "png", "pollster", + "tempfile", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", @@ -4823,7 +4824,6 @@ dependencies = [ "web-time 1.1.0", "wgpu", "wgpu-test", - "wgpu-types", "winit 0.29.15", ] diff --git a/examples/features/Cargo.toml b/examples/features/Cargo.toml index 710c1a21b..47f673f75 100644 --- a/examples/features/Cargo.toml +++ b/examples/features/Cargo.toml @@ -45,11 +45,8 @@ noise.workspace = true obj.workspace = true png.workspace = true pollster.workspace = true +tempfile.workspace = true web-time.workspace = true -wgpu-types = { workspace = true, features = [ - "std", - "trace", # TODO(#5974): this should be a dep on wgpu/trace and not wgpu-types at all -] } winit.workspace = true [dev-dependencies] @@ -57,7 +54,7 @@ wgpu-test.workspace = true [target.'cfg(not(target_arch = "wasm32"))'.dependencies] env_logger.workspace = true -wgpu.workspace = true +wgpu = { workspace = true, features = ["trace"] } [target.'cfg(target_arch = "wasm32")'.dependencies] console_error_panic_hook.workspace = true @@ -65,9 +62,10 @@ console_log.workspace = true fern.workspace = true wasm-bindgen.workspace = true wasm-bindgen-futures.workspace = true -wgpu = { path = "../../wgpu", default-features = false, features = [ +wgpu = { workspace = true, default-features = false, features = [ "wgsl", "std", + "trace", ] } # We need these features in the framework examples and tests web-sys = { workspace = true, features = [ diff --git a/examples/features/src/hello_triangle/mod.rs b/examples/features/src/hello_triangle/mod.rs index 3e299e92f..5a5348d91 100644 --- a/examples/features/src/hello_triangle/mod.rs +++ b/examples/features/src/hello_triangle/mod.rs @@ -1,4 +1,5 @@ use std::{borrow::Cow, sync::Arc}; +use tempfile::tempdir; use winit::{ event::{Event, WindowEvent}, event_loop::EventLoop, @@ -28,6 +29,8 @@ async fn run(event_loop: EventLoop<()>, window: Window) { .await .expect("Failed to find an appropriate adapter"); + let trace_dir = tempdir().expect("Failed to create temporary directory for trace"); + // Create the logical device and command queue let (device, queue) = adapter .request_device(&wgpu::DeviceDescriptor { @@ -38,7 +41,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) { .using_resolution(adapter.limits()), experimental_features: wgpu::ExperimentalFeatures::disabled(), memory_hints: wgpu::MemoryHints::MemoryUsage, - trace: wgpu::Trace::Off, + trace: wgpu::Trace::Directory(trace_dir.path().to_path_buf()), }) .await .expect("Failed to create device"); diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index 9f9e504a4..6f1d5a2fe 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -95,9 +95,8 @@ pub(crate) const FREXP_FUNCTION: &str = "naga_frexp"; // Must match code in glsl_built_in pub const FIRST_INSTANCE_BINDING: &str = "naga_vs_first_instance"; -#[cfg(any(feature = "serialize", feature = "deserialize"))] -#[cfg_attr(feature = "serialize", derive(serde::Serialize))] -#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] +#[cfg(feature = "deserialize")] +#[derive(serde::Deserialize)] struct BindingMapSerialization { resource_binding: crate::ResourceBinding, bind_target: u8, diff --git a/naga/src/back/hlsl/mod.rs b/naga/src/back/hlsl/mod.rs index 0230434a7..899b5dc94 100644 --- a/naga/src/back/hlsl/mod.rs +++ b/naga/src/back/hlsl/mod.rs @@ -216,9 +216,8 @@ pub struct OffsetsBindTarget { pub size: u32, } -#[cfg(any(feature = "serialize", feature = "deserialize"))] -#[cfg_attr(feature = "serialize", derive(serde::Serialize))] -#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] +#[cfg(feature = "deserialize")] +#[derive(serde::Deserialize)] struct BindingMapSerialization { resource_binding: crate::ResourceBinding, bind_target: BindTarget, @@ -337,9 +336,8 @@ impl Default for SamplerHeapBindTargets { } } -#[cfg(any(feature = "serialize", feature = "deserialize"))] -#[cfg_attr(feature = "serialize", derive(serde::Serialize))] -#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] +#[cfg(feature = "deserialize")] +#[derive(serde::Deserialize)] struct SamplerIndexBufferBindingSerialization { group: u32, bind_target: BindTarget, @@ -369,9 +367,8 @@ where pub type SamplerIndexBufferBindingMap = alloc::collections::BTreeMap; -#[cfg(any(feature = "serialize", feature = "deserialize"))] -#[cfg_attr(feature = "serialize", derive(serde::Serialize))] -#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] +#[cfg(feature = "deserialize")] +#[derive(serde::Deserialize)] struct DynamicStorageBufferOffsetTargetSerialization { index: u32, bind_target: OffsetsBindTarget, @@ -425,9 +422,8 @@ pub struct ExternalTextureBindTarget { pub params: BindTarget, } -#[cfg(any(feature = "serialize", feature = "deserialize"))] -#[cfg_attr(feature = "serialize", derive(serde::Serialize))] -#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] +#[cfg(feature = "deserialize")] +#[derive(serde::Deserialize)] struct ExternalTextureBindingMapSerialization { resource_binding: crate::ResourceBinding, bind_target: ExternalTextureBindTarget, diff --git a/player/src/bin/play.rs b/player/src/bin/play.rs index b808ab059..2af18c1cb 100644 --- a/player/src/bin/play.rs +++ b/player/src/bin/play.rs @@ -86,6 +86,8 @@ fn main() { ) } .unwrap(); + #[cfg(feature = "winit")] + let mut configured_surface_id = None; let (backends, device_desc) = match actions.pop_if(|action| matches!(action, trace::Action::Init { .. })) { @@ -149,9 +151,9 @@ fn main() { match event { Event::WindowEvent { event, .. } => match event { - WindowEvent::RedrawRequested if resize_config.is_none() => { + WindowEvent::RedrawRequested if resize_config.is_none() => loop { match actions.pop() { - Some(trace::Action::ConfigureSurface(_device_id, config)) => { + Some(trace::Action::ConfigureSurface(surface_id, config)) => { log::info!("Configuring the surface"); let current_size: (u32, u32) = window.inner_size().into(); let size = (config.width, config.height); @@ -163,24 +165,33 @@ fn main() { ), ); resize_config = Some(config); - target.exit(); + break; } else { let error = device.configure_surface(&surface, &config); + configured_surface_id = Some(surface_id); if let Some(e) = error { panic!("{e:?}"); } } } + Some(trace::Action::GetSurfaceTexture { id, parent }) => { + log::debug!("Get surface texture for frame {frame_count}"); + assert!( + configured_surface_id == Some(parent), + "rendering to an unexpected surface" + ); + player.get_surface_texture(id, &surface); + } Some(trace::Action::Present(_id)) => { frame_count += 1; log::debug!("Presenting frame {frame_count}"); surface.present().unwrap(); - target.exit(); + break; } Some(trace::Action::DiscardSurfaceTexture(_id)) => { log::debug!("Discarding frame {frame_count}"); surface.discard().unwrap(); - target.exit(); + break; } Some(action) => { player.process( @@ -195,10 +206,10 @@ fn main() { println!("Finished the end at frame {frame_count}"); done = true; } - target.exit(); + break; } } - } + }, WindowEvent::Resized(_) => { if let Some(config) = resize_config.take() { let error = device.configure_surface(&surface, &config); diff --git a/player/src/lib.rs b/player/src/lib.rs index 9418c71b3..f3062b2f6 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -428,6 +428,21 @@ impl Player { } } + // This one is a little strange because the surface is held by the + // `player` application but we want to insert the texture into our + // map so we can find it for rendering. + pub fn get_surface_texture( + &mut self, + id: wgc::id::PointerId, + surface: &wgc::instance::Surface, + ) { + let frame = surface + .get_current_texture() + .expect("get_current_texture error"); + let texture = frame.texture.expect("did not obtain a surface texture"); + self.textures.insert(id, texture); + } + pub fn resolve_buffer_id( &self, id: wgc::id::PointerId, diff --git a/wgpu-core/src/present.rs b/wgpu-core/src/present.rs index 0c28fb260..86b7c9ca4 100644 --- a/wgpu-core/src/present.rs +++ b/wgpu-core/src/present.rs @@ -368,6 +368,8 @@ impl Global { let fid = self.hub.textures.prepare(texture_id_in); + let output = surface.get_current_texture()?; + #[cfg(feature = "trace")] if let Some(present) = surface.presentation.lock().as_ref() { if let Some(ref mut trace) = *present.device.trace.lock() { @@ -380,8 +382,6 @@ impl Global { } } - let output = surface.get_current_texture()?; - let status = output.status; let texture_id = output .texture diff --git a/wgpu-types/src/device.rs b/wgpu-types/src/device.rs index 2035be012..fe6c6fbd1 100644 --- a/wgpu-types/src/device.rs +++ b/wgpu-types/src/device.rs @@ -83,9 +83,6 @@ pub enum MemoryHints { } /// Controls API call tracing and specifies where the trace is written. -/// -/// **Note:** Tracing is currently unavailable. -/// See [issue 5974](https://github.com/gfx-rs/wgpu/issues/5974) for updates. #[derive(Clone, Debug, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] // This enum must be non-exhaustive so that enabling the "trace" feature is not a semver break. diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 18377456a..5f07c26b1 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -120,9 +120,8 @@ strict_asserts = ["wgpu-core?/strict_asserts", "wgpu-types/strict_asserts"] ## Enables serialization via `serde` on common wgpu types. serde = ["wgpu-core?/serde", "wgpu-types/serde"] -# Uncomment once we get to https://github.com/gfx-rs/wgpu/issues/5974 # ## Allow writing of trace capture files. See [`Adapter::request_device`]. -# trace = ["serde", "wgpu-core/trace"] +trace = ["serde", "wgpu-core?/trace"] #! ### External libraries # -------------------------------------------------------------------- diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index fc85b09c1..a6c593d0a 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -110,15 +110,6 @@ impl ContextWgpuCore { hal_device: hal::OpenDevice, desc: &crate::DeviceDescriptor<'_>, ) -> Result<(CoreDevice, CoreQueue), crate::RequestDeviceError> { - if !matches!(desc.trace, wgt::Trace::Off) { - log::error!( - " - Feature 'trace' has been removed temporarily; \ - see https://github.com/gfx-rs/wgpu/issues/5974. \ - The `trace` parameter will have no effect." - ); - } - let (device_id, queue_id) = unsafe { self.0.create_device_from_hal( adapter.id, @@ -930,15 +921,6 @@ impl dispatch::AdapterInterface for CoreAdapter { &self, desc: &crate::DeviceDescriptor<'_>, ) -> Pin> { - if !matches!(desc.trace, wgt::Trace::Off) { - log::error!( - " - Feature 'trace' has been removed temporarily; \ - see https://github.com/gfx-rs/wgpu/issues/5974. \ - The `trace` parameter will have no effect." - ); - } - let res = self.context.0.adapter_request_device( self.id, &desc.map_label(|l| l.map(Borrowed)),