Skip to content

Commit 4a00b62

Browse files
[player] Support specifying a trace file directly (#7982)
Also provide a default initialization if the trace does not contain one.
1 parent 381932c commit 4a00b62

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

player/src/bin/play.rs

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn main() {
1212
use std::{
1313
fs,
1414
path::{Path, PathBuf},
15+
process::exit,
1516
};
1617

1718
#[cfg(feature = "winit")]
@@ -29,13 +30,28 @@ fn main() {
2930
//TODO: setting for the backend bits
3031
//TODO: setting for the target frame, or controls
3132

32-
let dir = match std::env::args().nth(1) {
33-
Some(arg) if Path::new(&arg).is_dir() => PathBuf::from(arg),
34-
_ => panic!("Provide the dir path as the parameter"),
33+
const HELP: &str = "\
34+
Usage: play <trace directory> | <trace file>\n\
35+
\n\
36+
Play a wgpu trace from the specified file or directory. If the trace contains\n\
37+
buffers, textures, or shaders, the directory form must be used.\n";
38+
39+
let (dir, trace) = match std::env::args().nth(1) {
40+
Some(arg) if Path::new(&arg).is_dir() => (
41+
PathBuf::from(arg.clone()),
42+
PathBuf::from(arg).join(trace::FILE_NAME),
43+
),
44+
Some(arg) if Path::new(&arg).is_file() => {
45+
(PathBuf::from("/nonexistent"), PathBuf::from(arg))
46+
}
47+
_ => {
48+
eprintln!("{HELP}");
49+
exit(1);
50+
}
3551
};
3652

37-
log::info!("Loading trace '{dir:?}'");
38-
let file = fs::File::open(dir.join(trace::FILE_NAME)).unwrap();
53+
log::info!("Loading trace '{trace:?}'");
54+
let file = fs::File::open(trace).unwrap();
3955
let mut actions: Vec<trace::Action> = ron::de::from_reader(file).unwrap();
4056
actions.reverse(); // allows us to pop from the top
4157
log::info!("Found {} actions", actions.len());
@@ -66,37 +82,39 @@ fn main() {
6682
}
6783
.unwrap();
6884

69-
let (device, queue) = match actions.pop() {
70-
Some(trace::Action::Init { desc, backend }) => {
71-
log::info!("Initializing the device for backend: {backend:?}");
72-
let adapter = global
73-
.request_adapter(
74-
&wgc::instance::RequestAdapterOptions {
75-
power_preference: wgt::PowerPreference::None,
76-
force_fallback_adapter: false,
77-
#[cfg(feature = "winit")]
78-
compatible_surface: Some(surface),
79-
#[cfg(not(feature = "winit"))]
80-
compatible_surface: None,
81-
},
82-
wgt::Backends::from(backend),
83-
Some(wgc::id::AdapterId::zip(0, 1)),
84-
)
85-
.expect("Unable to find an adapter for selected backend");
86-
87-
let info = global.adapter_get_info(adapter);
88-
log::info!("Picked '{}'", info.name);
89-
let device_id = wgc::id::Id::zip(0, 1);
90-
let queue_id = wgc::id::Id::zip(0, 1);
91-
let res =
92-
global.adapter_request_device(adapter, &desc, Some(device_id), Some(queue_id));
93-
if let Err(e) = res {
94-
panic!("{e:?}");
85+
let (backends, device_desc) =
86+
match actions.pop_if(|action| matches!(action, trace::Action::Init { .. })) {
87+
Some(trace::Action::Init { desc, backend }) => {
88+
log::info!("Initializing the device for backend: {backend:?}");
89+
(wgt::Backends::from(backend), desc)
9590
}
96-
(device_id, queue_id)
97-
}
98-
_ => panic!("Expected Action::Init"),
99-
};
91+
Some(_) => unreachable!(),
92+
None => (wgt::Backends::all(), wgt::DeviceDescriptor::default()),
93+
};
94+
95+
let adapter = global
96+
.request_adapter(
97+
&wgc::instance::RequestAdapterOptions {
98+
#[cfg(feature = "winit")]
99+
compatible_surface: Some(surface),
100+
#[cfg(not(feature = "winit"))]
101+
compatible_surface: None,
102+
..Default::default()
103+
},
104+
backends,
105+
Some(wgc::id::AdapterId::zip(0, 1)),
106+
)
107+
.expect("Unable to obtain an adapter");
108+
109+
let info = global.adapter_get_info(adapter);
110+
log::info!("Using '{}'", info.name);
111+
112+
let device = wgc::id::Id::zip(0, 1);
113+
let queue = wgc::id::Id::zip(0, 1);
114+
let res = global.adapter_request_device(adapter, &device_desc, Some(device), Some(queue));
115+
if let Err(e) = res {
116+
panic!("{e:?}");
117+
}
100118

101119
log::info!("Executing actions");
102120
#[cfg(not(feature = "winit"))]

0 commit comments

Comments
 (0)