mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 16:01:04 +01:00
fix(core): Lose device on errors during critical part of submission
(cherry picked from commit 390f05b69a)
This commit is contained in:
1 parent
ac2872b2b3
commit
7f6c33594a
3 files changed
+76
-32
No files matched your search
@@ -9,7 +9,7 @@ use hashbrown::hash_map::Entry;
|
||||
use crate::{
|
||||
device::{Device, DeviceError},
|
||||
init_tracker::*,
|
||||
resource::{DestroyedResourceError, ParentDevice, RawResourceAccess, Texture, Trackable},
|
||||
resource::{ParentDevice, RawResourceAccess, Texture, Trackable},
|
||||
snatch::SnatchGuard,
|
||||
track::{DeviceTracker, TextureTracker},
|
||||
FastHashMap,
|
||||
@@ -156,13 +156,22 @@ pub(crate) fn fixup_discarded_surfaces<InitIter: Iterator<Item = TextureSurfaceD
|
||||
}
|
||||
|
||||
impl BakedCommands {
|
||||
// inserts all buffer initializations that are going to be needed for
|
||||
// executing the commands and updates resource init states accordingly
|
||||
/// Initialize buffers.
|
||||
///
|
||||
/// Inserts all buffer initializations that are going to be needed for
|
||||
/// executing the commands, and updates resource init states accordingly.
|
||||
///
|
||||
/// The caller is responsible for checking that any buffer this may touch has not been
|
||||
/// destroyed, and must have done that check under the same snatch guard that is passed
|
||||
/// to this function.
|
||||
///
|
||||
/// # Panics
|
||||
/// If a destroyed buffer is encountered.
|
||||
pub(crate) fn initialize_buffer_memory(
|
||||
&mut self,
|
||||
device_tracker: &mut DeviceTracker,
|
||||
snatch_guard: &SnatchGuard<'_>,
|
||||
) -> Result<(), DestroyedResourceError> {
|
||||
) {
|
||||
profiling::scope!("initialize_buffer_memory");
|
||||
|
||||
// Gather init ranges for each buffer so we can collapse them.
|
||||
@@ -220,7 +229,9 @@ impl BakedCommands {
|
||||
.buffers
|
||||
.set_single(&buffer, wgt::BufferUses::COPY_DST);
|
||||
|
||||
let raw_buf = buffer.try_raw(snatch_guard)?;
|
||||
let raw_buf = buffer
|
||||
.try_raw(snatch_guard)
|
||||
.expect("attempt to initialize a destroyed buffer");
|
||||
|
||||
unsafe {
|
||||
self.encoder.raw.transition_buffers(
|
||||
@@ -251,19 +262,30 @@ impl BakedCommands {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// inserts all texture initializations that are going to be needed for
|
||||
// executing the commands and updates resource init states accordingly any
|
||||
// textures that are left discarded by this command buffer will be marked as
|
||||
// uninitialized
|
||||
/// Initialize textures.
|
||||
///
|
||||
/// Inserts all texture initializations that are going to be needed for
|
||||
/// executing the commands, and updates resource init states accordingly. Any
|
||||
/// textures that are left discarded by this command buffer will be marked as
|
||||
/// uninitialized.
|
||||
///
|
||||
/// The caller is responsible for checking that any texture this may touch has not been
|
||||
/// destroyed, and must have done that check under the same snatch guard that is passed
|
||||
/// to this function.
|
||||
///
|
||||
/// Note that any error returned from this function will become device loss in
|
||||
/// [`crate::device::queue::Queue::submit`].
|
||||
///
|
||||
/// # Panics
|
||||
/// If a destroyed texture is encountered.
|
||||
pub(crate) fn initialize_texture_memory(
|
||||
&mut self,
|
||||
device_tracker: &mut DeviceTracker,
|
||||
device: &Device,
|
||||
snatch_guard: &SnatchGuard<'_>,
|
||||
) -> Result<(), DestroyedResourceError> {
|
||||
) -> Result<(), ClearError> {
|
||||
profiling::scope!("initialize_texture_memory");
|
||||
|
||||
let mut ranges: Vec<TextureInitRange> = Vec::new();
|
||||
@@ -308,16 +330,14 @@ impl BakedCommands {
|
||||
device.instance_flags,
|
||||
);
|
||||
|
||||
// A Texture can be destroyed between the command recording
|
||||
// and now, this is out of our control so we have to handle
|
||||
// it gracefully.
|
||||
if let Err(ClearError::DestroyedResource(e)) = clear_result {
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
// Other errors are unexpected.
|
||||
if let Err(error) = clear_result {
|
||||
panic!("{error}");
|
||||
// We panic on destroyed textures for symmetry with buffer
|
||||
// initialization. It should not happen, but supposing it did,
|
||||
// it would also be fine to return the error and lose the
|
||||
// device in queue submit.
|
||||
if matches!(clear_result, Err(ClearError::DestroyedResource(_))) {
|
||||
panic!("attempt to initialize a destroyed texture");
|
||||
} else {
|
||||
clear_result?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1414,6 +1414,11 @@ impl Queue {
|
||||
.map_err(|(index, e)| (index, e.into()))?;
|
||||
let submit_index = submission.index;
|
||||
|
||||
// If we encounter an error after we have started updating global state and before
|
||||
// successful submission, we must lose the device to avoid continuing with
|
||||
// potentially inaccurate resource state.
|
||||
let mut lose_device_on_error = false;
|
||||
|
||||
let res = 'error: {
|
||||
let mut used_surface_textures = track::TextureUsageScope::default();
|
||||
let mut baked_command_buffers = Vec::with_capacity(command_buffers.len());
|
||||
@@ -1505,6 +1510,23 @@ impl Queue {
|
||||
break 'error Err(first_error);
|
||||
}
|
||||
|
||||
// At this point we have validated all the command buffers, and we start
|
||||
// making updates to global state. If we fail between here and successful
|
||||
// submission, we must lose the device, or else we could leave that global
|
||||
// state inaccurate or inconsistent.
|
||||
//
|
||||
// At time of writing, there were two kinds of errors that can occur in
|
||||
// this stage:
|
||||
// - `hal` command encoding errors. These produce device loss in
|
||||
// `handle_hal_error`, independent of what we do here.
|
||||
// - Errors from `initialize_texture_memory`. The error cases that
|
||||
// can actually occur should also be encoder errors, but we map
|
||||
// everything to device loss, just in case.
|
||||
lose_device_on_error = true;
|
||||
|
||||
// Note: locking the trackers has to be done after the storages
|
||||
let mut trackers = self.device.trackers.lock();
|
||||
|
||||
for mut baked in baked_command_buffers {
|
||||
profiling::scope!("process baked commands");
|
||||
|
||||
@@ -1516,19 +1538,16 @@ impl Queue {
|
||||
break 'error Err(e.into());
|
||||
}
|
||||
|
||||
//Note: locking the trackers has to be done after the storages
|
||||
let mut trackers = self.device.trackers.lock();
|
||||
if let Err(e) =
|
||||
baked.initialize_buffer_memory(&mut trackers, &submission.snatch_guard)
|
||||
{
|
||||
break 'error Err(e.into());
|
||||
}
|
||||
baked.initialize_buffer_memory(&mut trackers, &submission.snatch_guard);
|
||||
|
||||
if let Err(e) = baked.initialize_texture_memory(
|
||||
&mut trackers,
|
||||
&self.device,
|
||||
&submission.snatch_guard,
|
||||
) {
|
||||
break 'error Err(e.into());
|
||||
break 'error Err(QueueSubmitError::CommandEncoder(
|
||||
CommandEncoderError::Clear(e),
|
||||
));
|
||||
}
|
||||
|
||||
//Note: stateless trackers are not merged:
|
||||
@@ -1617,11 +1636,16 @@ impl Queue {
|
||||
};
|
||||
|
||||
Ok(closures)
|
||||
};
|
||||
}; // 'error
|
||||
|
||||
let callbacks = match res {
|
||||
Ok(ok) => ok,
|
||||
Err(e) => return Err((submit_index, e)),
|
||||
Err(e) => {
|
||||
if lose_device_on_error {
|
||||
self.device.lose("submission failed");
|
||||
}
|
||||
return Err((submit_index, e));
|
||||
}
|
||||
};
|
||||
|
||||
// the closures should execute with nothing locked!
|
||||
|
||||
@@ -5587,7 +5587,7 @@ impl Device {
|
||||
Ok(query_set)
|
||||
}
|
||||
|
||||
fn lose(&self, message: &str) {
|
||||
pub(crate) fn lose(&self, message: &str) {
|
||||
// Follow the steps at https://gpuweb.github.io/gpuweb/#lose-the-device.
|
||||
|
||||
// Mark the device explicitly as invalid. This is checked in various
|
||||
|
||||
Reference in new issue
Block a user