From a502e73c1d7ac4c930e72b59eae8ecbc66bac61f Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Thu, 2 Oct 2025 13:40:14 -0700 Subject: [PATCH] Create submodule of `device::trace` for recording support --- wgpu-core/src/device/trace.rs | 85 ++-------------------------- wgpu-core/src/device/trace/record.rs | 76 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 79 deletions(-) create mode 100644 wgpu-core/src/device/trace/record.rs diff --git a/wgpu-core/src/device/trace.rs b/wgpu-core/src/device/trace.rs index 6142802a8..9fe69327b 100644 --- a/wgpu-core/src/device/trace.rs +++ b/wgpu-core/src/device/trace.rs @@ -1,44 +1,23 @@ +#[cfg(feature = "trace")] +mod record; + use core::{convert::Infallible, ops::Range}; use alloc::{string::String, vec::Vec}; use macro_rules_attribute::apply; -#[cfg(feature = "trace")] -use {alloc::borrow::Cow, std::io::Write as _}; - -#[cfg(feature = "trace")] -use crate::command::IdReferences; use crate::{ command::{serde_object_reference_struct, BasePass, Command, ReferenceType, RenderCommand}, id, }; +#[cfg(feature = "trace")] +pub use record::*; + type FileName = String; pub const FILE_NAME: &str = "trace.ron"; -#[cfg(feature = "trace")] -pub(crate) fn new_render_bundle_encoder_descriptor<'a>( - label: crate::Label<'a>, - context: &'a super::RenderPassContext, - depth_read_only: bool, - stencil_read_only: bool, -) -> crate::command::RenderBundleEncoderDescriptor<'a> { - crate::command::RenderBundleEncoderDescriptor { - label, - color_formats: Cow::Borrowed(&context.attachments.colors), - depth_stencil: context.attachments.depth_stencil.map(|format| { - wgt::RenderBundleDepthStencil { - format, - depth_read_only, - stencil_read_only, - } - }), - sample_count: context.sample_count, - multiview: context.multiview, - } -} - #[allow(clippy::large_enum_variant)] #[derive(Debug)] #[apply(serde_object_reference_struct)] @@ -163,55 +142,3 @@ pub enum Action<'a, R: ReferenceType> { }, DestroyTlas(id::TlasId), } - -#[cfg(feature = "trace")] -#[derive(Debug)] -pub struct Trace { - path: std::path::PathBuf, - file: std::fs::File, - config: ron::ser::PrettyConfig, - binary_id: usize, -} - -#[cfg(feature = "trace")] -impl Trace { - pub fn new(path: std::path::PathBuf) -> Result { - log::info!("Tracing into '{path:?}'"); - let mut file = std::fs::File::create(path.join(FILE_NAME))?; - file.write_all(b"[\n")?; - Ok(Self { - path, - file, - config: ron::ser::PrettyConfig::default(), - binary_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); - let _ = std::fs::write(self.path.join(&name), data); - name - } - - pub(crate) fn add(&mut self, action: Action<'_, IdReferences>) - where - for<'a> Action<'a, IdReferences>: serde::Serialize, - { - match ron::ser::to_string_pretty(&action, self.config.clone()) { - Ok(string) => { - let _ = writeln!(self.file, "{string},"); - } - Err(e) => { - log::warn!("RON serialization failure: {e:?}"); - } - } - } -} - -#[cfg(feature = "trace")] -impl Drop for Trace { - fn drop(&mut self) { - let _ = self.file.write_all(b"]"); - } -} diff --git a/wgpu-core/src/device/trace/record.rs b/wgpu-core/src/device/trace/record.rs new file mode 100644 index 000000000..319757f6d --- /dev/null +++ b/wgpu-core/src/device/trace/record.rs @@ -0,0 +1,76 @@ +use alloc::{borrow::Cow, string::String}; +use std::io::Write as _; + +use crate::command::IdReferences; + +use super::{Action, FILE_NAME}; + +pub(crate) fn new_render_bundle_encoder_descriptor<'a>( + label: crate::Label<'a>, + context: &'a crate::device::RenderPassContext, + depth_read_only: bool, + stencil_read_only: bool, +) -> crate::command::RenderBundleEncoderDescriptor<'a> { + crate::command::RenderBundleEncoderDescriptor { + label, + color_formats: Cow::Borrowed(&context.attachments.colors), + depth_stencil: context.attachments.depth_stencil.map(|format| { + wgt::RenderBundleDepthStencil { + format, + depth_read_only, + stencil_read_only, + } + }), + sample_count: context.sample_count, + multiview: context.multiview, + } +} + +#[derive(Debug)] +pub struct Trace { + path: std::path::PathBuf, + file: std::fs::File, + config: ron::ser::PrettyConfig, + binary_id: usize, +} + +impl Trace { + pub fn new(path: std::path::PathBuf) -> Result { + log::info!("Tracing into '{path:?}'"); + let mut file = std::fs::File::create(path.join(FILE_NAME))?; + file.write_all(b"[\n")?; + Ok(Self { + path, + file, + config: ron::ser::PrettyConfig::default(), + binary_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); + let _ = std::fs::write(self.path.join(&name), data); + name + } + + pub(crate) fn add(&mut self, action: Action<'_, IdReferences>) + where + for<'a> Action<'a, IdReferences>: serde::Serialize, + { + match ron::ser::to_string_pretty(&action, self.config.clone()) { + Ok(string) => { + let _ = writeln!(self.file, "{string},"); + } + Err(e) => { + log::warn!("RON serialization failure: {e:?}"); + } + } + } +} + +impl Drop for Trace { + fn drop(&mut self) { + let _ = self.file.write_all(b"]"); + } +}