Skip to content

Commit 9461842

Browse files
committed
Create submodule of device::trace for recording support
1 parent 27ee35c commit 9461842

File tree

2 files changed

+82
-79
lines changed

2 files changed

+82
-79
lines changed

wgpu-core/src/device/trace.rs

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,23 @@
1+
#[cfg(feature = "trace")]
2+
mod record;
3+
14
use core::{convert::Infallible, ops::Range};
25

36
use alloc::{string::String, vec::Vec};
47
use macro_rules_attribute::apply;
58

6-
#[cfg(feature = "trace")]
7-
use {alloc::borrow::Cow, std::io::Write as _};
8-
9-
#[cfg(feature = "trace")]
10-
use crate::command::IdReferences;
119
use crate::{
1210
command::{serde_object_reference_struct, BasePass, Command, ReferenceType, RenderCommand},
1311
id,
1412
};
1513

14+
#[cfg(feature = "trace")]
15+
pub use record::*;
16+
1617
type FileName = String;
1718

1819
pub const FILE_NAME: &str = "trace.ron";
1920

20-
#[cfg(feature = "trace")]
21-
pub(crate) fn new_render_bundle_encoder_descriptor<'a>(
22-
label: crate::Label<'a>,
23-
context: &'a super::RenderPassContext,
24-
depth_read_only: bool,
25-
stencil_read_only: bool,
26-
) -> crate::command::RenderBundleEncoderDescriptor<'a> {
27-
crate::command::RenderBundleEncoderDescriptor {
28-
label,
29-
color_formats: Cow::Borrowed(&context.attachments.colors),
30-
depth_stencil: context.attachments.depth_stencil.map(|format| {
31-
wgt::RenderBundleDepthStencil {
32-
format,
33-
depth_read_only,
34-
stencil_read_only,
35-
}
36-
}),
37-
sample_count: context.sample_count,
38-
multiview: context.multiview,
39-
}
40-
}
41-
4221
#[allow(clippy::large_enum_variant)]
4322
#[derive(Debug)]
4423
#[apply(serde_object_reference_struct)]
@@ -163,55 +142,3 @@ pub enum Action<'a, R: ReferenceType> {
163142
},
164143
DestroyTlas(id::TlasId),
165144
}
166-
167-
#[cfg(feature = "trace")]
168-
#[derive(Debug)]
169-
pub struct Trace {
170-
path: std::path::PathBuf,
171-
file: std::fs::File,
172-
config: ron::ser::PrettyConfig,
173-
binary_id: usize,
174-
}
175-
176-
#[cfg(feature = "trace")]
177-
impl Trace {
178-
pub fn new(path: std::path::PathBuf) -> Result<Self, std::io::Error> {
179-
log::info!("Tracing into '{path:?}'");
180-
let mut file = std::fs::File::create(path.join(FILE_NAME))?;
181-
file.write_all(b"[\n")?;
182-
Ok(Self {
183-
path,
184-
file,
185-
config: ron::ser::PrettyConfig::default(),
186-
binary_id: 0,
187-
})
188-
}
189-
190-
pub fn make_binary(&mut self, kind: &str, data: &[u8]) -> String {
191-
self.binary_id += 1;
192-
let name = std::format!("data{}.{}", self.binary_id, kind);
193-
let _ = std::fs::write(self.path.join(&name), data);
194-
name
195-
}
196-
197-
pub(crate) fn add(&mut self, action: Action<'_, IdReferences>)
198-
where
199-
for<'a> Action<'a, IdReferences>: serde::Serialize,
200-
{
201-
match ron::ser::to_string_pretty(&action, self.config.clone()) {
202-
Ok(string) => {
203-
let _ = writeln!(self.file, "{string},");
204-
}
205-
Err(e) => {
206-
log::warn!("RON serialization failure: {e:?}");
207-
}
208-
}
209-
}
210-
}
211-
212-
#[cfg(feature = "trace")]
213-
impl Drop for Trace {
214-
fn drop(&mut self) {
215-
let _ = self.file.write_all(b"]");
216-
}
217-
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use alloc::{borrow::Cow, string::String};
2+
use std::io::Write as _;
3+
4+
use crate::command::IdReferences;
5+
6+
use super::{Action, FILE_NAME};
7+
8+
pub(crate) fn new_render_bundle_encoder_descriptor<'a>(
9+
label: crate::Label<'a>,
10+
context: &'a crate::device::RenderPassContext,
11+
depth_read_only: bool,
12+
stencil_read_only: bool,
13+
) -> crate::command::RenderBundleEncoderDescriptor<'a> {
14+
crate::command::RenderBundleEncoderDescriptor {
15+
label,
16+
color_formats: Cow::Borrowed(&context.attachments.colors),
17+
depth_stencil: context.attachments.depth_stencil.map(|format| {
18+
wgt::RenderBundleDepthStencil {
19+
format,
20+
depth_read_only,
21+
stencil_read_only,
22+
}
23+
}),
24+
sample_count: context.sample_count,
25+
multiview: context.multiview,
26+
}
27+
}
28+
29+
#[derive(Debug)]
30+
pub struct Trace {
31+
path: std::path::PathBuf,
32+
file: std::fs::File,
33+
config: ron::ser::PrettyConfig,
34+
binary_id: usize,
35+
}
36+
37+
impl Trace {
38+
pub fn new(path: std::path::PathBuf) -> Result<Self, std::io::Error> {
39+
log::info!("Tracing into '{path:?}'");
40+
let mut file = std::fs::File::create(path.join(FILE_NAME))?;
41+
file.write_all(b"[\n")?;
42+
Ok(Self {
43+
path,
44+
file,
45+
config: ron::ser::PrettyConfig::default(),
46+
binary_id: 0,
47+
})
48+
}
49+
50+
pub fn make_binary(&mut self, kind: &str, data: &[u8]) -> String {
51+
self.binary_id += 1;
52+
let name = std::format!("data{}.{}", self.binary_id, kind);
53+
let _ = std::fs::write(self.path.join(&name), data);
54+
name
55+
}
56+
57+
pub(crate) fn add(&mut self, action: Action<'_, IdReferences>)
58+
where
59+
for<'a> Action<'a, IdReferences>: serde::Serialize,
60+
{
61+
match ron::ser::to_string_pretty(&action, self.config.clone()) {
62+
Ok(string) => {
63+
let _ = writeln!(self.file, "{string},");
64+
}
65+
Err(e) => {
66+
log::warn!("RON serialization failure: {e:?}");
67+
}
68+
}
69+
}
70+
}
71+
72+
impl Drop for Trace {
73+
fn drop(&mut self) {
74+
let _ = self.file.write_all(b"]");
75+
}
76+
}

0 commit comments

Comments
 (0)