From 985da29bcf69a3a2df7e800a909f7e931e8ef985 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Fri, 1 Aug 2025 11:56:04 -0700 Subject: [PATCH] Add in-memory tracing and inline data in traces --- player/src/bin/play.rs | 9 +- player/src/lib.rs | 83 +++----- player/tests/player/data/bind-group.ron | 2 +- player/tests/player/data/buffer-copy.ron | 2 +- .../player/data/clear-buffer-texture.ron | 4 +- .../player/data/pipeline-statistics-query.ron | 2 +- player/tests/player/data/quad.ron | 2 +- player/tests/player/data/zero-init-buffer.ron | 4 +- .../player/data/zero-init-texture-binding.ron | 2 +- player/tests/player/main.rs | 4 +- wgpu-core/src/device/global.rs | 67 +++--- wgpu-core/src/device/queue.rs | 13 +- wgpu-core/src/device/resource.rs | 58 ++++-- wgpu-core/src/device/trace.rs | 97 ++++++++- wgpu-core/src/device/trace/record.rs | 197 ++++++++++++++++-- wgpu-core/src/device/trace/replay.rs | 51 +++++ wgpu-core/src/resource.rs | 8 +- wgpu-types/src/device.rs | 6 +- 18 files changed, 461 insertions(+), 150 deletions(-) create mode 100644 wgpu-core/src/device/trace/replay.rs diff --git a/player/src/bin/play.rs b/player/src/bin/play.rs index d6213077f..b808ab059 100644 --- a/player/src/bin/play.rs +++ b/player/src/bin/play.rs @@ -127,7 +127,7 @@ fn main() { unsafe { device.start_graphics_debugger_capture() }; while let Some(action) = actions.pop() { - player.process(&device, &queue, action, &dir); + player.process(&device, &queue, action, trace::DiskTraceLoader::new(&dir)); } unsafe { device.stop_graphics_debugger_capture() }; @@ -183,7 +183,12 @@ fn main() { target.exit(); } Some(action) => { - player.process(&device, &queue, action, &dir); + player.process( + &device, + &queue, + action, + trace::DiskTraceLoader::new(&dir), + ); } None => { if !done { diff --git a/player/src/lib.rs b/player/src/lib.rs index 3602594eb..9418c71b3 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -6,14 +6,14 @@ extern crate wgpu_core as wgc; extern crate wgpu_types as wgt; -use std::{borrow::Cow, convert::Infallible, fs, path::Path, sync::Arc}; +use std::{borrow::Cow, convert::Infallible, sync::Arc}; use hashbrown::HashMap; use wgc::{ binding_model::BindingResource, command::{ArcCommand, ArcReferences, BasePass, Command, PointerReferences}, - device::trace, + device::trace::{self, DataKind, DataLoader}, id::PointerId, }; @@ -94,7 +94,7 @@ impl Player { device: &Arc, queue: &Arc, action: trace::Action, - dir: &Path, + loader: impl DataLoader, ) { use wgc::device::trace::Action; log::debug!("action {action:?}"); @@ -226,15 +226,17 @@ impl Player { let _bind_group = self.bind_groups.remove(&id).expect("invalid bind group"); } Action::CreateShaderModule { id, desc, data } => { - log::debug!("Creating shader from {data}"); - let code = fs::read_to_string(dir.join(&data)).unwrap(); - let source = if data.ends_with(".wgsl") { - wgc::pipeline::ShaderModuleSource::Wgsl(Cow::Owned(code.clone())) - } else if data.ends_with(".ron") { + let code = loader.load_utf8(&data); + let source = if data.kind() == DataKind::Wgsl { + wgc::pipeline::ShaderModuleSource::Wgsl(code.clone()) + } else if data.kind() == DataKind::Ron { let module = ron::de::from_str(&code).unwrap(); wgc::pipeline::ShaderModuleSource::Naga(module) } else { - panic!("Unknown shader {data}"); + panic!( + "Unknown data kind for CreateShaderModule: {:?}", + data.kind() + ); }; match device.create_shader_module(&desc, source) { Ok(module) => self.shader_modules.insert(id, module), @@ -250,8 +252,8 @@ impl Player { runtime_checks, } => { let spirv = data.iter().find_map(|a| { - if a.ends_with(".spv") { - let data = fs::read(dir.join(a)).unwrap(); + if a.kind() == DataKind::Spv { + let data = loader.load(a); assert!(data.len().is_multiple_of(4)); Some(Cow::Owned(bytemuck::pod_collect_to_vec(&data))) @@ -259,46 +261,21 @@ impl Player { None } }); - let dxil = data.iter().find_map(|a| { - if a.ends_with(".dxil") { - let vec = std::fs::read(dir.join(a)).unwrap(); - Some(Cow::Owned(vec)) - } else { - None - } - }); - let hlsl = data.iter().find_map(|a| { - if a.ends_with(".hlsl") { - let code = fs::read_to_string(dir.join(a)).unwrap(); - Some(Cow::Owned(code)) - } else { - None - } - }); - let msl = data.iter().find_map(|a| { - if a.ends_with(".msl") { - let code = fs::read_to_string(dir.join(a)).unwrap(); - Some(Cow::Owned(code)) - } else { - None - } - }); - let glsl = data.iter().find_map(|a| { - if a.ends_with(".glsl") { - let code = fs::read_to_string(dir.join(a)).unwrap(); - Some(Cow::Owned(code)) - } else { - None - } - }); - let wgsl = data.iter().find_map(|a| { - if a.ends_with(".wgsl") { - let code = fs::read_to_string(dir.join(a)).unwrap(); - Some(Cow::Owned(code)) - } else { - None - } - }); + let dxil = data + .iter() + .find_map(|a| (a.kind() == DataKind::Dxil).then(|| loader.load(a))); + let hlsl = data + .iter() + .find_map(|a| (a.kind() == DataKind::Hlsl).then(|| loader.load_utf8(a))); + let msl = data + .iter() + .find_map(|a| (a.kind() == DataKind::Msl).then(|| loader.load_utf8(a))); + let glsl = data + .iter() + .find_map(|a| (a.kind() == DataKind::Glsl).then(|| loader.load_utf8(a))); + let wgsl = data + .iter() + .find_map(|a| (a.kind() == DataKind::Wgsl).then(|| loader.load_utf8(a))); let desc = wgt::CreateShaderModuleDescriptorPassthrough { entry_point, label, @@ -382,7 +359,7 @@ impl Player { queued, } => { let buffer = self.resolve_buffer_id(id); - let bin = std::fs::read(dir.join(data)).unwrap(); + let bin = loader.load(&data); let size = (range.end - range.start) as usize; if queued { queue @@ -401,7 +378,7 @@ impl Player { size, } => { let to = self.resolve_texel_copy_texture_info(to); - let bin = std::fs::read(dir.join(data)).unwrap(); + let bin = loader.load(&data); queue .write_texture(to, &bin, &layout, &size) .expect("Queue::write_texture error"); diff --git a/player/tests/player/data/bind-group.ron b/player/tests/player/data/bind-group.ron index edc72cd7b..d90098cba 100644 --- a/player/tests/player/data/bind-group.ron +++ b/player/tests/player/data/bind-group.ron @@ -47,7 +47,7 @@ label: None, flags: (bits: 3), ), - data: "empty.wgsl", + data: File("empty.wgsl"), ), CreateComputePipeline( id: PointerId(0x10), diff --git a/player/tests/player/data/buffer-copy.ron b/player/tests/player/data/buffer-copy.ron index 93b868c90..641e78f1f 100644 --- a/player/tests/player/data/buffer-copy.ron +++ b/player/tests/player/data/buffer-copy.ron @@ -20,7 +20,7 @@ ), WriteBuffer( id: PointerId(0x10), - data: "data1.bin", + data: File("data1.bin"), range: ( start: 0, end: 16, diff --git a/player/tests/player/data/clear-buffer-texture.ron b/player/tests/player/data/clear-buffer-texture.ron index 489e1d663..01922105d 100644 --- a/player/tests/player/data/clear-buffer-texture.ron +++ b/player/tests/player/data/clear-buffer-texture.ron @@ -40,7 +40,7 @@ mip_level: 0, array_layer: 0, ), - data: "quad.bin", + data: File("quad.bin"), layout: ( offset: 0, bytes_per_row: Some(256), @@ -73,7 +73,7 @@ // Make sure there is something in the buffer, otherwise it might be just zero init! WriteBuffer( id: PointerId(0x110), - data: "data1.bin", + data: File("data1.bin"), range: ( start: 0, end: 16, diff --git a/player/tests/player/data/pipeline-statistics-query.ron b/player/tests/player/data/pipeline-statistics-query.ron index eea8cfbe1..e1bffea38 100644 --- a/player/tests/player/data/pipeline-statistics-query.ron +++ b/player/tests/player/data/pipeline-statistics-query.ron @@ -20,7 +20,7 @@ label: None, flags: (bits: 3), ), - data: "empty.wgsl", + data: File("empty.wgsl"), ), CreateComputePipeline( id: PointerId(0x10), diff --git a/player/tests/player/data/quad.ron b/player/tests/player/data/quad.ron index 1bef1ddd4..aaf15b633 100644 --- a/player/tests/player/data/quad.ron +++ b/player/tests/player/data/quad.ron @@ -15,7 +15,7 @@ label: None, flags: (bits: 3), ), - data: "quad.wgsl", + data: File("quad.wgsl"), ), CreateTexture(PointerId(0x10), ( label: Some("Output Texture"), diff --git a/player/tests/player/data/zero-init-buffer.ron b/player/tests/player/data/zero-init-buffer.ron index 508651d9e..b1d079ff8 100644 --- a/player/tests/player/data/zero-init-buffer.ron +++ b/player/tests/player/data/zero-init-buffer.ron @@ -67,7 +67,7 @@ ), WriteBuffer( id: PointerId(0x120), - data: "data1.bin", + data: File("data1.bin"), range: ( start: 4, end: 20, @@ -80,7 +80,7 @@ label: None, flags: (bits: 3), ), - data: "zero-init-buffer-for-binding.wgsl", + data: File("zero-init-buffer-for-binding.wgsl"), ), CreateBuffer(PointerId(0x130), ( label: Some("used in binding"), diff --git a/player/tests/player/data/zero-init-texture-binding.ron b/player/tests/player/data/zero-init-texture-binding.ron index 6fa104cfa..76fd60a9f 100644 --- a/player/tests/player/data/zero-init-texture-binding.ron +++ b/player/tests/player/data/zero-init-texture-binding.ron @@ -125,7 +125,7 @@ label: None, flags: (bits: 3), ), - data: "zero-init-texture-binding.wgsl", + data: File("zero-init-texture-binding.wgsl"), ), CreateComputePipeline( id: PointerId(0x10), diff --git a/player/tests/player/main.rs b/player/tests/player/main.rs index 9a9c04836..51ae1898b 100644 --- a/player/tests/player/main.rs +++ b/player/tests/player/main.rs @@ -21,7 +21,7 @@ use std::{ slice, sync::Arc, }; -use wgc::command::PointerReferences; +use wgc::{command::PointerReferences, device::trace::DiskTraceLoader}; #[derive(serde::Deserialize)] enum ExpectedData { @@ -98,7 +98,7 @@ impl Test<'_> { println!("\t\t\tRunning..."); for action in self.actions { - player.process(&device, &queue, action, dir); + player.process(&device, &queue, action, DiskTraceLoader::new(dir)); } println!("\t\t\tMapping..."); for expect in &self.expectations { diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 977f175b4..0897778a9 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -997,27 +997,31 @@ impl Global { let device = self.hub.devices.get(device_id); #[cfg(feature = "trace")] - let data = device.trace.lock().as_mut().map(|trace| match source { - #[cfg(feature = "wgsl")] - pipeline::ShaderModuleSource::Wgsl(ref code) => { - trace.make_binary("wgsl", code.as_bytes()) - } - #[cfg(feature = "glsl")] - pipeline::ShaderModuleSource::Glsl(ref code, _) => { - trace.make_binary("glsl", code.as_bytes()) - } - #[cfg(feature = "spirv")] - pipeline::ShaderModuleSource::SpirV(ref code, _) => { - trace.make_binary("spirv", bytemuck::cast_slice::(code)) - } - pipeline::ShaderModuleSource::Naga(ref module) => { - let string = - ron::ser::to_string_pretty(module, ron::ser::PrettyConfig::default()) - .unwrap(); - trace.make_binary("ron", string.as_bytes()) - } - pipeline::ShaderModuleSource::Dummy(_) => { - panic!("found `ShaderModuleSource::Dummy`") + let data = device.trace.lock().as_mut().map(|trace| { + use crate::device::trace::DataKind; + + match source { + #[cfg(feature = "wgsl")] + pipeline::ShaderModuleSource::Wgsl(ref code) => { + trace.make_binary(DataKind::Wgsl, code.as_bytes()) + } + #[cfg(feature = "glsl")] + pipeline::ShaderModuleSource::Glsl(ref code, _) => { + trace.make_binary(DataKind::Glsl, code.as_bytes()) + } + #[cfg(feature = "spirv")] + pipeline::ShaderModuleSource::SpirV(ref code, _) => { + trace.make_binary(DataKind::Spv, bytemuck::cast_slice::(code)) + } + pipeline::ShaderModuleSource::Naga(ref module) => { + let string = + ron::ser::to_string_pretty(module, ron::ser::PrettyConfig::default()) + .unwrap(); + trace.make_binary(DataKind::Ron, string.as_bytes()) + } + pipeline::ShaderModuleSource::Dummy(_) => { + panic!("found `ShaderModuleSource::Dummy`") + } } }); @@ -1081,17 +1085,22 @@ impl Global { #[cfg(feature = "trace")] if let Some(ref mut trace) = *device.trace.lock() { + use crate::device::trace::DataKind; + let mut file_names = Vec::new(); - for (data, ext) in [ - (desc.spirv.as_ref().map(|a| bytemuck::cast_slice(a)), "spv"), - (desc.dxil.as_deref(), "dxil"), - (desc.hlsl.as_ref().map(|a| a.as_bytes()), "hlsl"), - (desc.msl.as_ref().map(|a| a.as_bytes()), "msl"), - (desc.glsl.as_ref().map(|a| a.as_bytes()), "glsl"), - (desc.wgsl.as_ref().map(|a| a.as_bytes()), "wgsl"), + for (data, kind) in [ + ( + desc.spirv.as_ref().map(|a| bytemuck::cast_slice(a)), + DataKind::Spv, + ), + (desc.dxil.as_deref(), DataKind::Dxil), + (desc.hlsl.as_ref().map(|a| a.as_bytes()), DataKind::Hlsl), + (desc.msl.as_ref().map(|a| a.as_bytes()), DataKind::Msl), + (desc.glsl.as_ref().map(|a| a.as_bytes()), DataKind::Glsl), + (desc.wgsl.as_ref().map(|a| a.as_bytes()), DataKind::Wgsl), ] { if let Some(data) = data { - file_names.push(trace.make_binary(ext, data)); + file_names.push(trace.make_binary(kind, data)); } } trace.add(trace::Action::CreateShaderModulePassthrough { diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 58916ad3f..a75ec46d1 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -1611,11 +1611,13 @@ impl Global { #[cfg(feature = "trace")] if let Some(ref mut trace) = *queue.device.trace.lock() { - let data_path = trace.make_binary("bin", data); + use crate::device::trace::DataKind; + let range = buffer_offset..buffer_offset + data.len() as u64; + let data = trace.make_binary(DataKind::Bin, data); trace.add(Action::WriteBuffer { id: buffer.to_trace(), - data: data_path, - range: buffer_offset..buffer_offset + data.len() as u64, + data, + range, queued: true, }); } @@ -1682,10 +1684,11 @@ impl Global { #[cfg(feature = "trace")] if let Some(ref mut trace) = *queue.device.trace.lock() { - let data_path = trace.make_binary("bin", data); + use crate::device::trace::DataKind; + let data = trace.make_binary(DataKind::Bin, data); trace.add(Action::WriteTexture { to: destination.to_trace(), - data: data_path, + data, layout: *data_layout, size: *size, }); diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index a6a29ddbe..e7bb2c2dc 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -272,7 +272,7 @@ pub struct Device { pub(crate) default_external_texture_params_buffer: ManuallyDrop>, // needs to be dropped last #[cfg(feature = "trace")] - pub(crate) trace: Mutex>, + pub(crate) trace: Mutex>>, } pub(crate) enum DeferredDestroy { @@ -385,9 +385,41 @@ impl Device { } }; #[cfg(feature = "trace")] - let trace_dir_name: Option<&std::path::PathBuf> = match &desc.trace { + let trace: Option> = match &desc.trace { wgt::Trace::Off => None, - wgt::Trace::Directory(d) => Some(d), + wgt::Trace::Directory(dir) => match trace::DiskTrace::new(dir.clone()) { + Ok(mut trace) => { + trace::Trace::add( + &mut trace, + trace::Action::Init { + desc: wgt::DeviceDescriptor { + trace: wgt::Trace::Off, + ..desc.clone() + }, + backend: adapter.backend(), + }, + ); + Some(Box::new(trace)) + } + Err(e) => { + log::error!("Unable to start a trace in '{dir:?}': {e}"); + None + } + }, + wgt::Trace::Memory => { + let mut trace = trace::MemoryTrace::new(); + trace::Trace::add( + &mut trace, + trace::Action::Init { + desc: wgt::DeviceDescriptor { + trace: wgt::Trace::Off, + ..desc.clone() + }, + backend: adapter.backend(), + }, + ); + Some(Box::new(trace)) + } // The enum is non_exhaustive, so we must have a fallback arm (that should be // unreachable in practice). t => { @@ -483,25 +515,7 @@ impl Device { tracker_indices: TrackerIndexAllocators::new(), bgl_pool: ResourcePool::new(), #[cfg(feature = "trace")] - trace: Mutex::new( - rank::DEVICE_TRACE, - trace_dir_name.and_then(|path| match trace::Trace::new(path.clone()) { - Ok(mut trace) => { - trace.add(trace::Action::Init { - desc: wgt::DeviceDescriptor { - trace: wgt::Trace::Off, - ..desc.clone() - }, - backend: adapter.backend(), - }); - Some(trace) - } - Err(e) => { - log::error!("Unable to start a trace in '{path:?}': {e}"); - None - } - }), - ), + trace: Mutex::new(rank::DEVICE_TRACE, trace), alignments, limits: desc.required_limits.clone(), features: desc.required_features, diff --git a/wgpu-core/src/device/trace.rs b/wgpu-core/src/device/trace.rs index 6e7d629fc..063d30c49 100644 --- a/wgpu-core/src/device/trace.rs +++ b/wgpu-core/src/device/trace.rs @@ -1,5 +1,7 @@ #[cfg(feature = "trace")] mod record; +#[cfg(feature = "replay")] +mod replay; use core::{convert::Infallible, ops::Range}; @@ -14,11 +16,98 @@ use crate::{ #[cfg(feature = "trace")] pub use record::*; +#[cfg(feature = "replay")] +pub use replay::*; type FileName = String; pub const FILE_NAME: &str = "trace.ron"; +#[derive(Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum Data { + File(FileName), + String(DataKind, String), + Binary(DataKind, Vec), +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "lowercase") +)] +pub enum DataKind { + Bin, + Wgsl, + + /// IR of Naga module, serialized in RON format + Ron, + Spv, + Dxil, + Hlsl, + Msl, + Glsl, +} + +impl core::fmt::Display for DataKind { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let s = match self { + DataKind::Bin => "bin", + DataKind::Wgsl => "wgsl", + DataKind::Ron => "ron", + DataKind::Spv => "spv", + DataKind::Dxil => "dxil", + DataKind::Hlsl => "hlsl", + DataKind::Msl => "msl", + DataKind::Glsl => "glsl", + }; + write!(f, "{s}") + } +} + +impl DataKind { + #[cfg(feature = "replay")] + fn is_string(&self) -> bool { + match *self { + DataKind::Wgsl | DataKind::Ron | DataKind::Hlsl | DataKind::Msl | DataKind::Glsl => { + true + } + DataKind::Bin | DataKind::Spv | DataKind::Dxil => false, + } + } +} + +impl Data { + pub fn kind(&self) -> DataKind { + match self { + Data::File(file) => { + if file.ends_with(".bin") { + DataKind::Bin + } else if file.ends_with(".wgsl") { + DataKind::Wgsl + } else if file.ends_with(".ron") { + DataKind::Ron + } else if file.ends_with(".spv") { + DataKind::Spv + } else if file.ends_with(".dxil") { + DataKind::Dxil + } else if file.ends_with(".hlsl") { + DataKind::Hlsl + } else if file.ends_with(".msl") { + DataKind::Msl + } else if file.ends_with(".glsl") { + DataKind::Glsl + } else { + panic!("unknown data file extension: {file}"); + } + } + Data::String(kind, _) => *kind, + Data::Binary(kind, _) => *kind, + } + } +} + #[allow(clippy::large_enum_variant)] #[derive(Debug)] #[apply(serde_object_reference_struct)] @@ -79,11 +168,11 @@ pub enum Action<'a, R: ReferenceType> { CreateShaderModule { id: PointerId, desc: crate::pipeline::ShaderModuleDescriptor<'a>, - data: FileName, + data: Data, }, CreateShaderModulePassthrough { id: PointerId, - data: Vec, + data: Vec, entry_point: String, label: crate::Label<'a>, @@ -119,13 +208,13 @@ pub enum Action<'a, R: ReferenceType> { DestroyQuerySet(PointerId), WriteBuffer { id: R::Buffer, - data: FileName, + data: Data, range: Range, queued: bool, }, WriteTexture { to: wgt::TexelCopyTextureInfo, - data: FileName, + data: Data, layout: wgt::TexelCopyBufferLayout, size: wgt::Extent3d, }, diff --git a/wgpu-core/src/device/trace/record.rs b/wgpu-core/src/device/trace/record.rs index 89639c9af..a745cd7e0 100644 --- a/wgpu-core/src/device/trace/record.rs +++ b/wgpu-core/src/device/trace/record.rs @@ -1,9 +1,4 @@ -use alloc::{ - borrow::Cow, - string::{String, ToString}, - sync::Arc, - vec::Vec, -}; +use alloc::{borrow::Cow, string::ToString, sync::Arc, vec::Vec}; use core::convert::Infallible; use std::io::Write as _; @@ -13,6 +8,7 @@ use crate::{ BasePass, ColorAttachments, Command, ComputeCommand, PointerReferences, RenderCommand, RenderPassColorAttachment, ResolvedRenderPassDepthStencilAttachment, }, + device::trace::{Data, DataKind}, id::{markers, PointerId}, storage::StorageItem, }; @@ -43,15 +39,25 @@ pub(crate) fn new_render_bundle_encoder_descriptor( } } +pub trait Trace: Send + Sync { + fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data; + + fn make_string(&mut self, kind: DataKind, data: &str) -> Data; + + fn add(&mut self, action: Action<'_, PointerReferences>) + where + for<'a> Action<'a, PointerReferences>: serde::Serialize; +} + #[derive(Debug)] -pub struct Trace { +pub struct DiskTrace { path: std::path::PathBuf, file: std::fs::File, config: ron::ser::PrettyConfig, - binary_id: usize, + data_id: usize, } -impl Trace { +impl DiskTrace { pub fn new(path: std::path::PathBuf) -> Result { log::debug!("Tracing into '{path:?}'"); let mut file = std::fs::File::create(path.join(FILE_NAME))?; @@ -60,18 +66,32 @@ impl Trace { path, file, config: ron::ser::PrettyConfig::default(), - binary_id: 0, + data_id: 0, }) } +} - pub fn make_binary(&mut self, kind: &str, data: &[u8]) -> String { - self.binary_id += 1; - let name = std::format!("data{}.{}", self.binary_id, kind); +impl Trace for DiskTrace { + /// Store `[u8]` data in the trace. + /// + /// Using a string `kind` is probably a bug, but should work as long as the + /// data is UTF-8. + fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data { + self.data_id += 1; + let name = std::format!("data{}.{}", self.data_id, kind); let _ = std::fs::write(self.path.join(&name), data); - name + Data::File(name) } - pub(crate) fn add(&mut self, action: Action<'_, PointerReferences>) + /// Store `str` data in the trace. + /// + /// Using a binary `kind` is fine, but it's not clear why not use + /// `make_binary` instead. + fn make_string(&mut self, kind: DataKind, data: &str) -> Data { + self.make_binary(kind, data.as_bytes()) + } + + fn add(&mut self, action: Action<'_, PointerReferences>) where for<'a> Action<'a, PointerReferences>: serde::Serialize, { @@ -86,12 +106,52 @@ impl Trace { } } -impl Drop for Trace { +impl Drop for DiskTrace { fn drop(&mut self) { let _ = self.file.write_all(b"]"); } } +#[derive(Default)] +pub struct MemoryTrace { + actions: Vec>, +} + +impl MemoryTrace { + pub fn new() -> Self { + Self::default() + } + + pub fn actions(&self) -> &[Action<'static, PointerReferences>] { + &self.actions + } +} + +impl Trace for MemoryTrace { + /// Store `[u8]` data in the trace. + /// + /// Using a string `kind` is probably a bug, but should work as long as the + /// data is UTF-8. + fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data { + Data::Binary(kind, data.to_vec()) + } + + /// Store `str` data in the trace. + /// + /// Using a binary `kind` is fine, but it's not clear why not use + /// `make_binary` instead. + fn make_string(&mut self, kind: DataKind, data: &str) -> Data { + Data::String(kind, data.to_string()) + } + + fn add(&mut self, action: Action<'_, PointerReferences>) + where + for<'a> Action<'a, PointerReferences>: serde::Serialize, + { + self.actions.push(action_to_owned(action)) + } +} + pub(crate) trait IntoTrace { type Output; fn into_trace(self) -> Self::Output; @@ -565,9 +625,11 @@ impl IntoTrace for ArcRenderCommand { } } -impl<'a> IntoTrace for crate::binding_model::ResolvedPipelineLayoutDescriptor<'a> { - type Output = - crate::binding_model::PipelineLayoutDescriptor<'a, PointerId>; +impl IntoTrace for crate::binding_model::ResolvedPipelineLayoutDescriptor<'_> { + type Output = crate::binding_model::PipelineLayoutDescriptor< + 'static, + PointerId, + >; fn into_trace(self) -> Self::Output { crate::binding_model::PipelineLayoutDescriptor { label: self.label.map(|l| Cow::Owned(l.into_owned())), @@ -756,3 +818,100 @@ impl IntoTrace for Option { self.map(|v| v.into_trace()) } } + +/// For selected `Action`s (mostly actions containing only owned data), return a +/// copy with `'static` lifetime. +/// +/// This is used for in-memory tracing. +/// +/// # Panics +/// +/// If `action` is not supported for in-memory tracing (likely because it +/// contains borrowed data). +fn action_to_owned(action: Action<'_, PointerReferences>) -> Action<'static, PointerReferences> { + use Action as A; + match action { + A::ConfigureSurface(surface, config) => A::ConfigureSurface(surface, config), + A::CreateBuffer(buffer, desc) => A::CreateBuffer( + buffer, + wgt::BufferDescriptor { + label: desc.label.map(|l| Cow::Owned(l.into_owned())), + size: desc.size, + usage: desc.usage, + mapped_at_creation: desc.mapped_at_creation, + }, + ), + A::FreeBuffer(buffer) => A::FreeBuffer(buffer), + A::DestroyBuffer(buffer) => A::DestroyBuffer(buffer), + A::FreeTexture(texture) => A::FreeTexture(texture), + A::DestroyTexture(texture) => A::DestroyTexture(texture), + A::DestroyTextureView(texture_view) => A::DestroyTextureView(texture_view), + A::FreeExternalTexture(external_texture) => A::FreeExternalTexture(external_texture), + A::DestroyExternalTexture(external_texture) => A::DestroyExternalTexture(external_texture), + A::DestroySampler(sampler) => A::DestroySampler(sampler), + A::GetSurfaceTexture { id, parent } => A::GetSurfaceTexture { id, parent }, + A::Present(surface) => A::Present(surface), + A::DiscardSurfaceTexture(surface) => A::DiscardSurfaceTexture(surface), + A::DestroyBindGroupLayout(layout) => A::DestroyBindGroupLayout(layout), + A::DestroyPipelineLayout(layout) => A::DestroyPipelineLayout(layout), + A::DestroyBindGroup(bind_group) => A::DestroyBindGroup(bind_group), + A::DestroyShaderModule(shader_module) => A::DestroyShaderModule(shader_module), + A::DestroyComputePipeline(pipeline) => A::DestroyComputePipeline(pipeline), + A::DestroyRenderPipeline(pipeline) => A::DestroyRenderPipeline(pipeline), + A::DestroyPipelineCache(cache) => A::DestroyPipelineCache(cache), + A::DestroyRenderBundle(render_bundle) => A::DestroyRenderBundle(render_bundle), + A::DestroyQuerySet(query_set) => A::DestroyQuerySet(query_set), + A::WriteBuffer { + id, + data, + range, + queued, + } => A::WriteBuffer { + id, + data, + range, + queued, + }, + A::WriteTexture { + to, + data, + layout, + size, + } => A::WriteTexture { + to, + data, + layout, + size, + }, + A::Submit(index, commands) => A::Submit(index, commands), + A::FailedCommands { + commands, + failed_at_submit, + error, + } => A::FailedCommands { + commands, + failed_at_submit, + error, + }, + A::DestroyBlas(blas) => A::DestroyBlas(blas), + A::DestroyTlas(tlas) => A::DestroyTlas(tlas), + + A::Init { .. } + | A::CreateTexture(..) + | A::CreateTextureView { .. } + | A::CreateExternalTexture { .. } + | A::CreateSampler(..) + | A::CreateBindGroupLayout(..) + | A::CreatePipelineLayout(..) + | A::CreateBindGroup(..) + | A::CreateShaderModule { .. } + | A::CreateShaderModulePassthrough { .. } + | A::CreateComputePipeline { .. } + | A::CreateGeneralRenderPipeline { .. } + | A::CreatePipelineCache { .. } + | A::CreateRenderBundle { .. } + | A::CreateQuerySet { .. } + | A::CreateBlas { .. } + | A::CreateTlas { .. } => panic!("Unsupported action for tracing: {action:?}"), + } +} diff --git a/wgpu-core/src/device/trace/replay.rs b/wgpu-core/src/device/trace/replay.rs new file mode 100644 index 000000000..17c98c63b --- /dev/null +++ b/wgpu-core/src/device/trace/replay.rs @@ -0,0 +1,51 @@ +use alloc::borrow::Cow; + +use super::Data; + +pub trait DataLoader { + fn load<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, [u8]>; + fn load_utf8<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, str>; +} + +#[cfg(feature = "std")] +pub struct DiskTraceLoader<'a>(&'a std::path::Path); + +#[cfg(feature = "std")] +impl<'a> DiskTraceLoader<'a> { + pub fn new(path: &'a std::path::Path) -> DiskTraceLoader<'a> { + DiskTraceLoader(path) + } +} + +impl DataLoader for DiskTraceLoader<'_> { + fn load<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, [u8]> { + match data { + Data::File(file) => { + Cow::from(std::fs::read(self.0.join(file)).expect("Failed to read data file")) + } + Data::String(_, s) => Cow::from(s.as_bytes()), + Data::Binary(_, b) => Cow::from(b), + } + } + + /// Load UTF-8 string data + /// + /// # Panics + /// + /// If the data kind is not a string format or the data is not valid UTF-8 + fn load_utf8<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, str> { + match data { + Data::File(file) => Cow::from( + std::fs::read_to_string(self.0.join(file)).expect("Failed to read data file"), + ), + Data::String(kind, s) => { + assert!(kind.is_string(), "{kind:?} cannot be loaded as a string"); + Cow::from(s) + } + Data::Binary(kind, b) => { + assert!(kind.is_string(), "{kind:?} cannot be loaded as a string"); + Cow::from(core::str::from_utf8(b).unwrap()) + } + } + } +} diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index d8fae8f37..e6230e422 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -815,9 +815,9 @@ impl Buffer { BufferMapState::Init { staging_buffer } => { #[cfg(feature = "trace")] if let Some(ref mut trace) = *device.trace.lock() { - use crate::device::trace::IntoTrace; + use crate::device::trace::{DataKind, IntoTrace}; - let data = trace.make_binary("bin", staging_buffer.get_data()); + let data = trace.make_binary(DataKind::Bin, staging_buffer.get_data()); trace.add(trace::Action::WriteBuffer { id: self.to_trace(), data, @@ -878,10 +878,10 @@ impl Buffer { if host == HostMap::Write { #[cfg(feature = "trace")] if let Some(ref mut trace) = *device.trace.lock() { - use crate::device::trace::IntoTrace; + use crate::device::trace::{DataKind, IntoTrace}; let size = range.end - range.start; - let data = trace.make_binary("bin", unsafe { + let data = trace.make_binary(DataKind::Bin, unsafe { core::slice::from_raw_parts(mapping.ptr.as_ptr(), size as usize) }); trace.add(trace::Action::WriteBuffer { diff --git a/wgpu-types/src/device.rs b/wgpu-types/src/device.rs index c38d326eb..2035be012 100644 --- a/wgpu-types/src/device.rs +++ b/wgpu-types/src/device.rs @@ -95,9 +95,13 @@ pub enum Trace { #[default] Off, - /// Tracing enabled. + /// Write trace to disk. #[cfg(feature = "trace")] // This must be owned rather than `&'a Path`, because if it were that, then the lifetime // parameter would be unused when the "trace" feature is disabled, which is prohibited. Directory(std::path::PathBuf), + + /// Store trace in memory. + #[cfg(feature = "trace")] + Memory, }