Skip to content

Commit 9a3bba6

Browse files
feat(tracing): Trace failed create_*_pipeline calls (gfx-rs#9209)
1 parent e6854ec commit 9a3bba6

9 files changed

Lines changed: 63 additions & 28 deletions

File tree

player/src/lib.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use wgc::{
1414
binding_model::BindingResource,
1515
command::{ArcCommand, ArcReferences, BasePass, Command, PointerReferences},
1616
device::trace::{self, DataKind, DataLoader},
17-
id::PointerId,
17+
id::{Marker, PointerId},
1818
};
1919

2020
pub struct Player {
@@ -88,6 +88,28 @@ impl Default for Player {
8888
}
8989
}
9090

91+
fn process_result<T: Marker, U>(
92+
op: &str,
93+
map: &mut HashMap<PointerId<T>, U>,
94+
id: Option<PointerId<T>>,
95+
value: Result<U, impl std::error::Error>,
96+
) {
97+
match (id, value) {
98+
(Some(id), Ok(value)) => {
99+
map.insert(id, value);
100+
}
101+
(Some(_), Err(err)) => {
102+
panic!("{op} succeeded when recording, but failed on playback: {err}");
103+
}
104+
(None, Ok(_)) => {
105+
panic!("{op} failed when recording, but succeeded on playback");
106+
}
107+
(None, Err(err)) => {
108+
panic!("{op} failed when recording, and failed on playback: {err}");
109+
}
110+
}
111+
}
112+
91113
impl Player {
92114
pub fn process(
93115
&mut self,
@@ -323,10 +345,13 @@ impl Player {
323345
}
324346
Action::CreateComputePipeline { id, desc } => {
325347
let resolved_desc = self.resolve_compute_pipeline_descriptor(desc);
326-
let pipeline = device
327-
.create_compute_pipeline(resolved_desc)
328-
.expect("create_compute_pipeline error");
329-
self.compute_pipelines.insert(id, pipeline);
348+
let pipeline = device.create_compute_pipeline(resolved_desc);
349+
process_result(
350+
"create_compute_pipeline",
351+
&mut self.compute_pipelines,
352+
id,
353+
pipeline,
354+
);
330355
}
331356
Action::DestroyComputePipeline(id) => {
332357
self.compute_pipelines
@@ -338,10 +363,13 @@ impl Player {
338363
// pipeline descriptor that can represent either a conventional
339364
// pipeline or a mesh shading pipeline.
340365
let resolved_desc = self.resolve_render_pipeline_descriptor(desc);
341-
let pipeline = device
342-
.create_render_pipeline(resolved_desc)
343-
.expect("create_render_pipeline error");
344-
self.render_pipelines.insert(id, pipeline);
366+
let pipeline = device.create_render_pipeline(resolved_desc);
367+
process_result(
368+
"create_render_pipeline",
369+
&mut self.render_pipelines,
370+
id,
371+
pipeline,
372+
);
345373
}
346374
Action::DestroyRenderPipeline(id) => {
347375
self.render_pipelines

player/tests/player/data/bind-group.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
data: File("empty.wgsl"),
5151
),
5252
CreateComputePipeline(
53-
id: PointerId(0x10),
53+
id: Some(PointerId(0x10)),
5454
desc: (
5555
label: None,
5656
layout: Some(PointerId(0x10)),

player/tests/player/data/pipeline-statistics-query.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
data: File("empty.wgsl"),
2424
),
2525
CreateComputePipeline(
26-
id: PointerId(0x10),
26+
id: Some(PointerId(0x10)),
2727
desc: (
2828
label: None,
2929
layout: Some(PointerId(0x10)),

player/tests/player/data/quad.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
immediate_size: 0,
5151
)),
5252
CreateGeneralRenderPipeline(
53-
id: PointerId(0x10),
53+
id: Some(PointerId(0x10)),
5454
desc: (
5555
label: None,
5656
layout: Some(PointerId(0x10)),

player/tests/player/data/zero-init-buffer.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
immediate_size: 0,
126126
)),
127127
CreateComputePipeline(
128-
id: PointerId(0x10),
128+
id: Some(PointerId(0x10)),
129129
desc: (
130130
label: None,
131131
layout: Some(PointerId(0x10)),

player/tests/player/data/zero-init-texture-binding.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
data: File("zero-init-texture-binding.wgsl"),
129129
),
130130
CreateComputePipeline(
131-
id: PointerId(0x10),
131+
id: Some(PointerId(0x10)),
132132
desc: (
133133
label: None,
134134
layout: Some(PointerId(0x10)),

wgpu-core/src/device/global.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,19 +1538,21 @@ impl Global {
15381538
#[cfg(feature = "trace")]
15391539
let trace_desc = desc.clone().into_trace();
15401540

1541-
let pipeline = match device.create_render_pipeline(desc) {
1542-
Ok(pair) => pair,
1543-
Err(e) => break 'error e,
1544-
};
1541+
let res = device.create_render_pipeline(desc);
15451542

15461543
#[cfg(feature = "trace")]
15471544
if let Some(ref mut trace) = *device.trace.lock() {
15481545
trace.add(trace::Action::CreateGeneralRenderPipeline {
1549-
id: pipeline.to_trace(),
1546+
id: res.as_ref().ok().map(IntoTrace::to_trace),
15501547
desc: trace_desc,
15511548
});
15521549
}
15531550

1551+
let pipeline = match res {
1552+
Ok(pair) => pair,
1553+
Err(e) => break 'error e,
1554+
};
1555+
15541556
let id = fid.assign(Fallible::Valid(pipeline));
15551557
api_log!("Device::create_render_pipeline -> {id:?}");
15561558

@@ -1687,19 +1689,21 @@ impl Global {
16871689
#[cfg(feature = "trace")]
16881690
let trace_desc = desc.clone().into_trace();
16891691

1690-
let pipeline = match device.create_compute_pipeline(desc) {
1691-
Ok(pair) => pair,
1692-
Err(e) => break 'error e,
1693-
};
1692+
let res = device.create_compute_pipeline(desc);
16941693

16951694
#[cfg(feature = "trace")]
16961695
if let Some(ref mut trace) = *device.trace.lock() {
16971696
trace.add(trace::Action::CreateComputePipeline {
1698-
id: pipeline.to_trace(),
1697+
id: res.as_ref().ok().map(IntoTrace::to_trace),
16991698
desc: trace_desc,
17001699
});
17011700
}
17021701

1702+
let pipeline = match res {
1703+
Ok(pair) => pair,
1704+
Err(e) => break 'error e,
1705+
};
1706+
17031707
let id = fid.assign(Fallible::Valid(pipeline));
17041708
api_log!("Device::create_compute_pipeline -> {id:?}");
17051709

wgpu-core/src/device/trace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ pub enum Action<'a, R: ReferenceType> {
193193
},
194194
DestroyShaderModule(PointerId<markers::ShaderModule>),
195195
CreateComputePipeline {
196-
id: PointerId<markers::ComputePipeline>,
196+
id: Option<PointerId<markers::ComputePipeline>>,
197197
desc: TraceComputePipelineDescriptor<'a>,
198198
},
199199
DestroyComputePipeline(PointerId<markers::ComputePipeline>),
200200
CreateGeneralRenderPipeline {
201-
id: PointerId<markers::RenderPipeline>,
201+
id: Option<PointerId<markers::RenderPipeline>>,
202202
desc: TraceGeneralRenderPipelineDescriptor<'a>,
203203
},
204204
DestroyRenderPipeline(PointerId<markers::RenderPipeline>),

wgpu-core/src/id.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl TryFrom<SerialId> for RawId {
134134
#[derive(Debug, serde::Serialize, serde::Deserialize)]
135135
pub enum PointerId<T: Marker> {
136136
// The only variant forces RON to not ignore "Id"
137-
PointerId(usize, #[serde(skip)] PhantomData<T>),
137+
PointerId(core::num::NonZeroUsize, #[serde(skip)] PhantomData<T>),
138138
}
139139

140140
#[cfg(feature = "serde")]
@@ -177,7 +177,10 @@ impl<T: crate::storage::StorageItem> From<&alloc::sync::Arc<T>> for PointerId<T:
177177
// data, not to the `ArcInner`. The `ArcInner` stores the reference
178178
// counts before the data, so the machine code for this conversion has
179179
// to add an offset to the pointer.
180-
PointerId::PointerId(alloc::sync::Arc::as_ptr(arc) as usize, PhantomData)
180+
PointerId::PointerId(
181+
core::num::NonZeroUsize::new(alloc::sync::Arc::as_ptr(arc) as usize).unwrap(),
182+
PhantomData,
183+
)
181184
}
182185
}
183186

0 commit comments

Comments
 (0)