1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

chore(ext/webgpu): clean up buffer_get_map_async (#24087)

This commit is contained in:
Luca Casonato 2024-06-04 12:44:17 +02:00 committed by GitHub
parent 3341c50b6a
commit d625b296d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,7 +2,6 @@
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::futures::channel::oneshot;
use deno_core::op2; use deno_core::op2;
use deno_core::OpState; use deno_core::OpState;
use deno_core::Resource; use deno_core::Resource;
@ -10,8 +9,9 @@ use deno_core::ResourceId;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration; use std::time::Duration;
use wgpu_core::resource::BufferAccessResult;
use super::error::DomExceptionOperationError; use super::error::DomExceptionOperationError;
use super::error::WebGpuResult; use super::error::WebGpuResult;
@ -78,9 +78,8 @@ pub async fn op_webgpu_buffer_get_map_async(
#[number] offset: u64, #[number] offset: u64,
#[number] size: u64, #[number] size: u64,
) -> Result<WebGpuResult, AnyError> { ) -> Result<WebGpuResult, AnyError> {
let (sender, receiver) = oneshot::channel::<BufferAccessResult>();
let device; let device;
let done = Arc::new(Mutex::new(None));
{ {
let state_ = state.borrow(); let state_ = state.borrow();
let instance = state_.borrow::<super::Instance>(); let instance = state_.borrow::<super::Instance>();
@ -92,56 +91,48 @@ pub async fn op_webgpu_buffer_get_map_async(
.get::<super::WebGpuDevice>(device_rid)?; .get::<super::WebGpuDevice>(device_rid)?;
device = device_resource.1; device = device_resource.1;
let done_ = done.clone();
let callback = Box::new(move |status| { let callback = Box::new(move |status| {
sender.send(status).unwrap(); *done_.lock().unwrap() = Some(status);
}); });
// TODO(lucacasonato): error handling
let maybe_err = gfx_select!(buffer => instance.buffer_map_async( let maybe_err = gfx_select!(buffer => instance.buffer_map_async(
buffer, buffer,
offset, offset,
Some(size), Some(size),
wgpu_core::resource::BufferMapOperation { wgpu_core::resource::BufferMapOperation {
host: match mode { host: match mode {
1 => wgpu_core::device::HostMap::Read, 1 => wgpu_core::device::HostMap::Read,
2 => wgpu_core::device::HostMap::Write, 2 => wgpu_core::device::HostMap::Write,
_ => unreachable!(), _ => unreachable!(),
}, },
callback: Some(wgpu_core::resource::BufferMapCallback::from_rust(callback)), callback: Some(wgpu_core::resource::BufferMapCallback::from_rust(callback)),
} }
)) ))
.err(); .err();
if maybe_err.is_some() { if maybe_err.is_some() {
return Ok(WebGpuResult::maybe_err(maybe_err)); return Ok(WebGpuResult::maybe_err(maybe_err));
} }
} }
let done = Rc::new(RefCell::new(false)); loop {
let done_ = done.clone(); let result = done.lock().unwrap().take();
let device_poll_fut = async move { match result {
while !*done.borrow() { Some(Ok(())) => return Ok(WebGpuResult::empty()),
{ Some(Err(e)) => {
let state = state.borrow(); return Err(DomExceptionOperationError::new(&e.to_string()).into())
let instance = state.borrow::<super::Instance>(); }
gfx_select!(device => instance.device_poll(device, wgpu_types::Maintain::wait())) None => {
.unwrap(); {
let state = state.borrow();
let instance = state.borrow::<super::Instance>();
gfx_select!(device => instance.device_poll(device, wgpu_types::Maintain::Poll)).unwrap();
}
tokio::time::sleep(Duration::from_millis(10)).await;
} }
tokio::time::sleep(Duration::from_millis(10)).await;
} }
Ok::<(), AnyError>(()) }
};
let receiver_fut = async move {
receiver.await??;
let mut done = done_.borrow_mut();
*done = true;
Ok::<(), AnyError>(())
};
tokio::try_join!(device_poll_fut, receiver_fut)?;
Ok(WebGpuResult::empty())
} }
#[op2] #[op2]