Wait for a readback's map callback when another thread's poll is running it

This commit is contained in:
Dennis Kobert authored and Keavon Chambers committed 2026-10-30 20:22:17 -07:00
1 parent 6264e7396a
commit 3e58246c0b
2 files changed
+11 -3

No files matched your search

+10 -2
View File
@@ -152,12 +152,20 @@ impl WgpuExecutor {
);
self.context().queue.lock().submit([encoder.finish()]);
let (sender, mut receiver) = futures::channel::oneshot::channel();
let (sender, receiver) = futures::channel::oneshot::channel();
buffer.slice(..).map_async(wgpu::MapMode::Read, move |result| {
let _ = sender.send(result);
});
let _ = device.poll(wgpu::wgt::PollType::wait_indefinitely());
let Ok(Some(mapping)) = receiver.try_recv() else {
// A poll on another thread can be the one running the callback, which may not have finished by the time this poll returns
#[cfg(not(target_family = "wasm"))]
let mapping = futures::executor::block_on(receiver).ok();
#[cfg(target_family = "wasm")]
let mapping = {
let mut receiver = receiver;
receiver.try_recv().ok().flatten()
};
let Some(mapping) = mapping else {
anyhow::bail!("The GPU didn't map the texture's pixels for reading");
};
mapping?;
@@ -12,7 +12,7 @@ fn executor() -> Option<&'static WgpuExecutor> {
EXECUTOR.get_or_init(|| futures::executor::block_on(WgpuExecutor::new())).as_ref()
}
/// Held while a test uses the shared GPU, since another test's work can delay or intercept a render's readback.
/// Held while a test uses the shared GPU, since another test's work can delay a render's readback.
fn take_turn() -> std::sync::MutexGuard<'static, ()> {
static TURN: std::sync::Mutex<()> = std::sync::Mutex::new(());
TURN.lock().unwrap_or_else(std::sync::PoisonError::into_inner)