use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
static HAPPENED: AtomicBool = AtomicBool::new(false);
pub(super) fn happened() -> bool {
HAPPENED.load(Ordering::Acquire)
}
fn say(lost: &AtomicBool) {
let first = !lost.swap(true, Ordering::AcqRel);
HAPPENED.store(true, Ordering::Release);
#[cfg(not(target_arch = "wasm32"))]
if first {
card::lost_on_the_card_of(lost);
super::forget_instances_after_a_loss();
#[cfg(all(windows, feature = "raster"))]
super::card_frames::a_device_is_lost();
}
#[cfg(target_arch = "wasm32")]
let _ = first;
}
#[cfg(not(target_arch = "wasm32"))]
pub(super) use card::{Again, a_lost_device_is_held, the_card_again, the_card_gave, the_card_gives};
#[cfg(not(target_arch = "wasm32"))]
mod card {
use std::{
sync::{
Arc, Mutex, Weak,
atomic::{AtomicBool, Ordering},
},
time::{Duration, Instant},
};
#[derive(Clone, Debug, PartialEq, Eq)]
struct Card {
backend: wgpu::Backend,
vendor: u32,
device: u32,
name: String,
}
impl Card {
fn of(adapter: &wgpu::Adapter) -> Self {
let info = adapter.get_info();
Self {
backend: info.backend,
vendor: info.vendor,
device: info.device,
name: info.name,
}
}
}
const WAIT: Duration = Duration::from_secs(3);
type Watched = (Weak<AtomicBool>, Card, Arc<AtomicBool>);
struct Wanted {
card: Card,
at: Instant,
gone: Vec<Arc<AtomicBool>>,
}
impl Wanted {
fn held(&self) -> bool {
self.gone.iter().any(|gone| !gone.load(Ordering::Acquire))
}
}
static WATCHED: Mutex<Vec<Watched>> = Mutex::new(Vec::new());
static WANTED: Mutex<Option<Wanted>> = Mutex::new(None);
static GIVES: AtomicBool = AtomicBool::new(false);
#[cfg(test)]
pub(in super::super) static REFUSES: AtomicBool = AtomicBool::new(false);
pub(super) fn remember(device: &wgpu::Device, lost: &Arc<AtomicBool>, adapter: &wgpu::Adapter) {
let gone = destroyed_teller(device);
let mut watched = WATCHED.lock().unwrap();
watched.retain(|(known, ..)| known.strong_count() > 0);
watched.push((Arc::downgrade(lost), Card::of(adapter), gone));
}
#[cfg(windows)]
fn destroyed_teller(device: &wgpu::Device) -> Arc<AtomicBool> {
use windows::{Win32::Graphics::Direct3D::ID3DDestructionNotifier, core::Interface};
unsafe extern "system" fn destroyed(told: *mut core::ffi::c_void) {
let gone = unsafe { Arc::from_raw(told.cast_const().cast::<AtomicBool>()) };
gone.store(true, Ordering::Release);
}
let gone = Arc::new(AtomicBool::new(false));
let notifier = unsafe { device.as_hal::<wgpu::hal::api::Dx12>() }
.and_then(|hal| hal.raw_device().cast::<ID3DDestructionNotifier>().ok());
let told = Arc::into_raw(gone.clone());
let registered = notifier
.is_some_and(|notifier| unsafe { notifier.RegisterDestructionCallback(Some(destroyed), told.cast()) }.is_ok());
if !registered {
drop(unsafe { Arc::from_raw(told) });
gone.store(true, Ordering::Release);
}
gone
}
#[cfg(not(windows))]
fn destroyed_teller(_device: &wgpu::Device) -> Arc<AtomicBool> {
Arc::new(AtomicBool::new(true))
}
pub(in super::super) fn a_lost_device_is_held() -> bool {
WANTED.lock().unwrap().as_ref().is_some_and(Wanted::held)
}
pub(super) fn lost_on_the_card_of(lost: &AtomicBool) {
let watched = WATCHED
.lock()
.unwrap()
.iter()
.find(|(known, ..)| std::ptr::eq(known.as_ptr(), lost))
.map(|(_, card, gone)| (card.clone(), gone.clone()));
let Some((card, gone)) = watched else {
return;
};
let mut wanted = WANTED.lock().unwrap();
if wanted.as_ref().is_none_or(|wanted| wanted.card == card) {
GIVES.store(false, Ordering::Release);
}
match &mut *wanted {
Some(wanted) if wanted.card == card => {
wanted.at = Instant::now();
wanted.gone.retain(|gone| !gone.load(Ordering::Acquire));
wanted.gone.push(gone);
}
Some(_) => {}
None => {
*wanted = Some(Wanted {
card,
at: Instant::now(),
gone: vec![gone],
});
}
}
}
pub(in super::super) enum Again {
Card(wgpu::Adapter, wgpu::Device, wgpu::Queue, bool),
Wait,
Elsewhere(wgpu::Adapter, wgpu::Device, wgpu::Queue),
Unasked,
}
pub(in super::super) async fn the_card_again(
instance: &wgpu::Instance,
surface: Option<&wgpu::Surface<'_>>,
describe: impl Fn(&wgpu::Adapter) -> wgpu::DeviceDescriptor<'static>,
) -> Again {
let wanted = WANTED
.lock()
.unwrap()
.as_ref()
.map(|wanted| (wanted.card.clone(), wanted.at, wanted.held()));
let Some((card, at, held)) = wanted else {
return Again::Unasked;
};
if held && at.elapsed() < WAIT {
return Again::Wait;
}
match on_the_card(instance, &card, surface, &describe).await {
Some((adapter, device, queue)) => Again::Card(adapter, device, queue, held),
None if at.elapsed() < WAIT => Again::Wait,
None => match on_another_card(instance, &card, surface, &describe).await {
Some((adapter, device, queue)) => {
GIVES.store(false, Ordering::Release);
Again::Elsewhere(adapter, device, queue)
}
None => Again::Wait,
},
}
}
pub(in super::super) async fn the_card_gives(
instance: &wgpu::Instance,
describe: impl Fn(&wgpu::Adapter) -> wgpu::DeviceDescriptor<'static>,
) -> bool {
let wanted = WANTED.lock().unwrap().as_ref().map(|wanted| wanted.card.clone());
let gives = match wanted {
Some(card) => on_the_card(instance, &card, None, &describe).await.is_some(),
None => false,
};
GIVES.store(gives, Ordering::Release);
gives
}
pub(in super::super) fn the_card_gave() -> bool {
GIVES.load(Ordering::Acquire)
}
async fn on_the_card(
instance: &wgpu::Instance,
card: &Card,
surface: Option<&wgpu::Surface<'_>>,
describe: &impl Fn(&wgpu::Adapter) -> wgpu::DeviceDescriptor<'static>,
) -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
#[cfg(test)]
if REFUSES.load(Ordering::Acquire) {
return None;
}
for adapter in instance.enumerate_adapters(wgpu::Backends::all()).await {
if Card::of(&adapter) != *card || surface.is_some_and(|surface| !adapter.is_surface_supported(surface)) {
continue;
}
if let Ok((device, queue)) = adapter.request_device(&describe(&adapter)).await {
return Some((adapter, device, queue));
}
}
None
}
async fn on_another_card(
instance: &wgpu::Instance,
card: &Card,
surface: Option<&wgpu::Surface<'_>>,
describe: &impl Fn(&wgpu::Adapter) -> wgpu::DeviceDescriptor<'static>,
) -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
let mut others: Vec<wgpu::Adapter> = instance
.enumerate_adapters(wgpu::Backends::all())
.await
.into_iter()
.filter(|adapter| Card::of(adapter) != *card)
.filter(|adapter| surface.is_none_or(|surface| adapter.is_surface_supported(surface)))
.collect();
others.sort_by_key(|adapter| match adapter.get_info().device_type {
wgpu::DeviceType::DiscreteGpu => 0,
wgpu::DeviceType::IntegratedGpu => 1,
wgpu::DeviceType::VirtualGpu => 2,
wgpu::DeviceType::Other => 3,
wgpu::DeviceType::Cpu => 4,
});
for adapter in others {
if let Ok((device, queue)) = adapter.request_device(&describe(&adapter)).await {
return Some((adapter, device, queue));
}
}
None
}
}
pub(super) fn watch(
device: &wgpu::Device,
lost: &Arc<AtomicBool>,
adapter: &wgpu::Adapter,
untold: &super::app_errors::Untold,
) {
#[cfg(not(target_arch = "wasm32"))]
card::remember(device, lost, adapter);
#[cfg(target_arch = "wasm32")]
let _ = adapter;
device.set_device_lost_callback({
let lost = lost.clone();
move |_, _| say(&lost)
});
let removed = removed_asker(device);
let lost = lost.clone();
let untold = untold.clone();
device.on_uncaptured_error(Arc::new(move |error: wgpu::Error| {
if lost.load(Ordering::Acquire) || removed() {
say(&lost);
return;
}
#[cfg(not(target_arch = "wasm32"))]
if !super::app_errors::is_own() {
let error = super::WgpuAppError::from(error);
super::app_errors::log(&error);
untold.push(error);
return;
}
#[cfg(target_arch = "wasm32")]
let _ = &untold;
panic!("wgpu error: {error}");
}));
}
pub(super) fn taken<T>(device: &wgpu::Device, make: impl FnOnce() -> T) -> Option<T> {
#[cfg(not(target_arch = "wasm32"))]
{
let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
let made = make();
pollster::block_on(scope.pop()).is_none().then_some(made)
}
#[cfg(target_arch = "wasm32")]
{
let _ = device;
Some(make())
}
}
pub(super) fn asked(device: &wgpu::Device, lost: Option<&AtomicBool>, how: wgpu::PollType) -> bool {
if lost.is_some_and(|lost| lost.load(Ordering::Acquire)) {
return false;
}
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| device.poll(how))) {
Ok(answered) => answered.is_ok(),
Err(_) => {
if let Some(lost) = lost {
say(lost);
}
false
}
}
}
pub(super) fn is_lost(device: &wgpu::Device, lost: &AtomicBool) -> bool {
if lost.load(Ordering::Acquire) {
return true;
}
asked(device, Some(lost), wgpu::PollType::Poll);
if lost.load(Ordering::Acquire) {
return true;
}
let removed = removed_asker(device)();
if removed {
say(lost);
}
removed
}
#[cfg(windows)]
fn removed_asker(device: &wgpu::Device) -> impl Fn() -> bool + Send + Sync + 'static {
let raw = unsafe { device.as_hal::<wgpu::hal::api::Dx12>() }.map(|hal| hal.raw_device().clone());
move || {
raw
.as_ref()
.is_some_and(|raw| unsafe { raw.GetDeviceRemovedReason() }.is_err())
}
}
#[cfg(not(windows))]
fn removed_asker(_device: &wgpu::Device) -> impl Fn() -> bool + Send + Sync + 'static {
|| false
}
#[cfg(test)]
mod refused {
fn two_devices() -> Option<(wgpu::Device, wgpu::Device)> {
let instance = super::super::new_instance(super::super::default_backends());
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())).ok()?;
let device = || {
pollster::block_on(adapter.request_device(&super::super::device_descriptor(
&adapter,
wgpu::Features::empty(),
&wgpu::Limits::default(),
)))
.ok()
.map(|(device, _)| device)
};
Some((device()?, device()?))
}
fn view(device: &wgpu::Device) -> wgpu::TextureView {
device
.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 4,
height: 4,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
})
.create_view(&Default::default())
}
#[test]
fn a_view_of_another_device_is_refused_and_ends_nothing() {
let Some((first, second)) = two_devices() else {
eprintln!("skipped: no card to make two devices on");
return;
};
let layout = second.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
}],
});
let bound = |view: &wgpu::TextureView| {
let bind = || {
second.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(view),
}],
})
};
super::taken(&second, bind)
};
assert!(bound(&view(&first)).is_none(), "a view of another device is bound");
assert!(bound(&view(&second)).is_some());
}
}
#[cfg(test)]
mod told_by_wgpu {
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
#[test]
#[ignore = "a loss is the whole test program's: run alone, with --ignored"]
fn a_vulkan_device_that_wgpu_says_is_lost_is_lost_and_ends_nothing() {
let instance = super::super::new_instance(wgpu::Backends::VULKAN);
let Ok(adapter) = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) else {
eprintln!("skipped: no card with Vulkan here");
return;
};
let (device, queue) = pollster::block_on(adapter.request_device(&super::super::device_descriptor(
&adapter,
wgpu::Features::empty(),
&wgpu::Limits::default(),
)))
.expect("a device");
let lost = Arc::new(AtomicBool::new(false));
super::watch(&device, &lost, &adapter, &Default::default());
assert!(!super::is_lost(&device, &lost));
queue.submit([device.create_command_encoder(&Default::default()).finish()]);
let _ = device.poll(wgpu::PollType::wait_indefinitely());
assert!(!super::is_lost(&device, &lost));
device.destroy();
assert!(
!lost.load(Ordering::Acquire),
"wgpu tells of a device that was destroyed only when it is asked"
);
assert!(
super::is_lost(&device, &lost),
"asked before the frame, wgpu tells of the loss"
);
assert!(lost.load(Ordering::Acquire));
assert!(super::happened());
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 16,
height: 16,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let _view = texture.create_view(&Default::default());
queue.submit([device.create_command_encoder(&Default::default()).finish()]);
let _ = device.poll(wgpu::PollType::Poll);
}
}
#[cfg(all(test, windows))]
mod tests {
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use windows::{Win32::Graphics::Direct3D12::ID3D12Device5, core::Interface};
#[test]
#[ignore = "removes the card's device from the whole test program: run alone, with --ignored"]
fn a_device_that_is_removed_is_lost_and_its_errors_end_nothing() {
let instance = wgpu::Instance::new(super::super::instance_descriptor(wgpu::Backends::DX12));
let Ok(adapter) = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) else {
eprintln!("skipped: no card with Direct3D 12 here");
return;
};
let (device, queue) = pollster::block_on(adapter.request_device(&super::super::device_descriptor(
&adapter,
wgpu::Features::empty(),
&wgpu::Limits::default(),
)))
.expect("a device");
let lost = Arc::new(AtomicBool::new(false));
super::watch(&device, &lost, &adapter, &Default::default());
assert!(!super::is_lost(&device, &lost));
unsafe {
device
.as_hal::<wgpu::hal::api::Dx12>()
.expect("a device of Direct3D 12")
.raw_device()
.cast::<ID3D12Device5>()
.expect("a device that can be removed")
.RemoveDevice();
}
assert!(super::is_lost(&device, &lost));
assert!(lost.load(Ordering::Acquire));
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 16,
height: 16,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let _view = texture.create_view(&Default::default());
queue.submit([device.create_command_encoder(&Default::default()).finish()]);
let _ = device.poll(wgpu::PollType::Poll);
}
#[test]
#[ignore = "removes the card's device from the whole test program: run alone, with --ignored"]
fn a_device_made_ahead_is_made_again_for_all_that_share_it() {
let Ok(given) = pollster::block_on(super::super::WgpuDevice::request_with(wgpu::Backends::DX12)) else {
eprintln!("skipped: no card with Direct3D 12 here");
return;
};
let shared = given.clone();
let first = given.current().expect("the device that was asked for");
assert!(!super::is_lost(&first.device, &first.lost));
unsafe {
first
.device
.as_hal::<wgpu::hal::api::Dx12>()
.expect("a device of Direct3D 12")
.raw_device()
.cast::<ID3D12Device5>()
.expect("a device that can be removed")
.RemoveDevice();
}
assert!(super::is_lost(&first.device, &first.lost));
assert!(
shared.current().is_none(),
"a device is made while the lost one is held"
);
let card = first.adapter.get_info().name;
drop(first);
let started = std::time::Instant::now();
let again = loop {
if let Some(again) = shared.current() {
break again;
}
assert!(
started.elapsed() < std::time::Duration::from_secs(2),
"no device, though the lost one is let go of"
);
std::thread::sleep(std::time::Duration::from_millis(20));
};
assert!(!super::is_lost(&again.device, &again.lost));
assert_eq!(
again.adapter.get_info().name,
card,
"on the card the device was lost on"
);
assert!(!again.while_held && !again.displaced);
let same = given.current().expect("the device, for what shares it");
assert!(
std::sync::Arc::ptr_eq(&again.lost, &same.lost),
"one device for all that share it"
);
let texture = again.device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 4,
height: 4,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
again.queue.write_texture(
texture.as_image_copy(),
&[7_u8; 64],
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(16),
rows_per_image: None,
},
texture.size(),
);
again.queue.submit([]);
again
.device
.poll(wgpu::PollType::wait_indefinitely())
.expect("the card answers");
assert!(!super::is_lost(&again.device, &again.lost));
}
#[test]
#[ignore = "removes the card's device from the whole test program: run alone, with --ignored"]
fn a_card_that_makes_no_device_in_time_is_left_for_another_and_gone_back_to() {
use std::{
sync::atomic::Ordering,
time::{Duration, Instant},
};
fn describe(adapter: &wgpu::Adapter) -> wgpu::DeviceDescriptor<'static> {
super::super::device_descriptor(adapter, wgpu::Features::empty(), &wgpu::Limits::default())
}
let Ok(given) = pollster::block_on(super::super::WgpuDevice::request_with(wgpu::Backends::DX12)) else {
eprintln!("skipped: no card with Direct3D 12 here");
return;
};
let first = given.current().expect("the device that was asked for");
let card = first.adapter.get_info().name;
unsafe {
first
.device
.as_hal::<wgpu::hal::api::Dx12>()
.expect("a device of Direct3D 12")
.raw_device()
.cast::<ID3D12Device5>()
.expect("a device that can be removed")
.RemoveDevice();
}
assert!(super::is_lost(&first.device, &first.lost));
drop(first);
super::card::REFUSES.store(true, Ordering::Release);
let lost_at = Instant::now();
assert!(
given.current().is_none(),
"another card is taken before the first was waited for"
);
assert!(lost_at.elapsed() < Duration::from_secs(2));
std::thread::sleep(Duration::from_millis(3200));
let Some(other) = given.current() else {
super::card::REFUSES.store(false, Ordering::Release);
eprintln!("the rest is not run: no other card makes a device here");
return;
};
let elsewhere = other.adapter.get_info().name;
assert_ne!(elsewhere, card, "the card that makes none is not the one taken");
assert!(other.displaced && !other.while_held);
assert!(!super::is_lost(&other.device, &other.lost));
assert!(std::sync::Arc::ptr_eq(
&given.clone().current().unwrap().lost,
&other.lost
));
assert!(!pollster::block_on(super::the_card_gives(&other.instance, describe)));
assert!(!super::the_card_gave());
super::card::REFUSES.store(false, Ordering::Release);
assert!(pollster::block_on(super::the_card_gives(&other.instance, describe)));
assert!(super::the_card_gave());
assert!(given.current().is_some_and(|made| made.displaced));
given.back.store(true, Ordering::Release);
let back = given.current().expect("a device of the first card");
assert_eq!(
back.adapter.get_info().name,
card,
"the device is of the card it was lost on again"
);
assert!(!back.displaced && !back.while_held);
assert!(!super::is_lost(&back.device, &back.lost));
eprintln!("lost on {card:?}, went to {elsewhere:?}, and came back");
}
}