1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// SPDX-License-Identifier: AGPL-3.0-or-later
//! GPU submission pipeline — submit, poll, and panic recovery.
//!
//! All `device.poll()` and `queue.submit()` calls go through these methods
//! for consistent gpu-lock serialization and device-lost panic handling.
//! Extracted from `mod.rs` to keep the device facade under 600 lines.
use std::sync::atomic::Ordering;
use super::{WgpuDevice, poll_timeout};
use crate::error::Result;
impl WgpuDevice {
/// Poll the device for completed work, catching device-lost panics.
/// Every `device.poll(PollType::Wait)` call site in barracuda should
/// go through this method (or its `_nonblocking` variant) so that a
/// driver-level device loss is consistently caught, flagged, and
/// converted to `Err` instead of panicking the caller's thread.
/// Serialized via `gpu_lock`: wgpu-core's internal resource tracking
/// on software rasterizers can race under concurrent submit/poll.
/// # Errors
/// Returns [`Err`] if the device is lost or poll fails (e.g. driver crash).
pub fn poll_safe(&self) -> Result<()> {
if self.is_lost() {
return Err(crate::error::BarracudaError::device_lost("device lost"));
}
let _guard = self
.gpu_lock
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
self.brief_encoder_wait();
let device = self.device.clone();
let timeout = poll_timeout();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
match device.poll(wgpu::PollType::Wait {
submission_index: None,
timeout,
}) {
Ok(status) => status.is_queue_empty(),
Err(_) => false,
}
}));
match result {
Ok(true) => Ok(()),
Ok(false) => {
tracing::warn!(
"poll_safe: GPU poll timed out after {}s — treating as stall",
timeout.map_or(0, |d| d.as_secs())
);
Err(crate::error::BarracudaError::execution_failed(
"GPU poll timed out — driver stall or instrumentation overhead",
))
}
Err(payload) => {
if Self::is_device_lost_panic(&payload) {
self.lost.store(true, Ordering::Release);
tracing::warn!("poll_safe: device lost: {}", Self::panic_message(&payload));
return Err(crate::error::BarracudaError::device_lost("device lost"));
}
tracing::warn!(
"poll_safe: wgpu panic (non-fatal): {}",
Self::panic_message(&payload)
);
Ok(())
}
}
}
/// Submit command buffers without polling for completion.
/// Serialized via `gpu_lock` to prevent wgpu-core storage races on
/// software rasterizers. Use when the caller will poll separately
/// (e.g. via `poll_safe()` or `map_staging_buffer()`).
pub fn submit_commands(&self, commands: impl IntoIterator<Item = wgpu::CommandBuffer>) {
if self.is_lost() {
return;
}
let _guard = self
.gpu_lock
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
self.brief_encoder_wait();
let queue = self.queue.clone();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
queue.submit(commands);
}));
if let Err(payload) = result {
self.handle_device_lost_panic(payload, "submit_commands");
}
}
/// Non-blocking poll variant for drain/housekeeping paths.
/// Serialized via `gpu_lock` to prevent wgpu-core cleanup races.
pub fn poll_nonblocking(&self) {
if self.is_lost() {
return;
}
if self.active_encoders.load(Ordering::Acquire) > 0 {
return;
}
let Ok(_guard) = self.gpu_lock.try_lock() else {
return;
};
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _ = self.device.poll(wgpu::PollType::Poll);
}));
}
fn panic_message(payload: &Box<dyn std::any::Any + Send>) -> &str {
payload
.downcast_ref::<String>()
.map(std::string::String::as_str)
.or_else(|| payload.downcast_ref::<&str>().copied())
.unwrap_or("unknown")
}
fn is_device_lost_panic(payload: &Box<dyn std::any::Any + Send>) -> bool {
let msg = Self::panic_message(payload);
msg.contains("lost") || msg.contains("Lost") || msg.contains("Parent device")
}
fn handle_device_lost_panic(&self, payload: Box<dyn std::any::Any + Send>, context: &str) {
if Self::is_device_lost_panic(&payload) {
self.lost.store(true, Ordering::Release);
tracing::warn!("{context}: device lost: {}", Self::panic_message(&payload));
return;
}
std::panic::resume_unwind(payload);
}
/// Submit without acquiring a dispatch permit.
/// Use when the caller already holds a `DispatchPermit`.
///
/// Submit and poll use **separate** lock acquisitions so other threads
/// can interleave their submits while we poll. `poll(Wait)` processes
/// ALL pending work, so interleaved submits from other threads are
/// completed too — no work is lost.
pub(crate) fn submit_and_poll_inner(
&self,
commands: impl IntoIterator<Item = wgpu::CommandBuffer>,
) {
self.submit_commands_inner(commands);
self.poll_wait_inner();
}
fn submit_commands_inner(&self, commands: impl IntoIterator<Item = wgpu::CommandBuffer>) {
if self.is_lost() {
return;
}
let _guard = self
.gpu_lock
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
self.brief_encoder_wait();
let queue = self.queue.clone();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
queue.submit(commands);
}));
if let Err(payload) = result {
self.handle_device_lost_panic(payload, "submit_commands_inner");
}
}
fn poll_wait_inner(&self) {
if self.is_lost() {
return;
}
let _guard = self
.gpu_lock
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
self.brief_encoder_wait();
let device = self.device.clone();
let timeout = poll_timeout();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let queue_empty = match device.poll(wgpu::PollType::Wait {
submission_index: None,
timeout,
}) {
Ok(status) => status.is_queue_empty(),
Err(_) => false,
};
if !queue_empty {
tracing::warn!(
"poll_wait_inner: GPU poll timed out after {}s",
timeout.map_or(0, |d| d.as_secs())
);
}
}));
if let Err(payload) = result {
self.handle_device_lost_panic(payload, "poll_wait_inner");
}
}
}