2024-04-02 18:36:04 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::channel::oneshot;
|
2023-10-04 04:25:56 +00:00
|
|
|
use deno_core::op2;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
2024-07-04 10:46:24 +00:00
|
|
|
use std::ptr::NonNull;
|
2021-09-03 17:23:35 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::time::Duration;
|
2022-10-13 19:41:52 +00:00
|
|
|
use wgpu_core::resource::BufferAccessResult;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
use super::error::DomExceptionOperationError;
|
|
|
|
use super::error::WebGpuResult;
|
|
|
|
|
2023-03-03 17:18:01 +00:00
|
|
|
pub(crate) struct WebGpuBuffer(
|
|
|
|
pub(crate) super::Instance,
|
|
|
|
pub(crate) wgpu_core::id::BufferId,
|
|
|
|
);
|
2021-09-03 17:23:35 +00:00
|
|
|
impl Resource for WebGpuBuffer {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUBuffer".into()
|
|
|
|
}
|
2023-03-03 17:18:01 +00:00
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
2024-01-16 10:15:56 +00:00
|
|
|
gfx_select!(self.1 => self.0.buffer_drop(self.1, true));
|
2023-03-03 17:18:01 +00:00
|
|
|
}
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 10:46:24 +00:00
|
|
|
struct WebGpuBufferMapped(NonNull<u8>, usize);
|
2021-09-03 17:23:35 +00:00
|
|
|
impl Resource for WebGpuBufferMapped {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUBufferMapped".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub fn op_webgpu_create_buffer(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] device_rid: ResourceId,
|
|
|
|
#[string] label: Cow<str>,
|
|
|
|
#[number] size: u64,
|
2021-09-03 17:23:35 +00:00
|
|
|
usage: u32,
|
|
|
|
mapped_at_creation: bool,
|
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let device_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::WebGpuDevice>(device_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let device = device_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let descriptor = wgpu_core::resource::BufferDescriptor {
|
2023-10-04 04:25:56 +00:00
|
|
|
label: Some(label),
|
2022-03-24 17:20:10 +00:00
|
|
|
size,
|
|
|
|
usage: wgpu_types::BufferUsages::from_bits(usage)
|
2021-09-03 17:23:35 +00:00
|
|
|
.ok_or_else(|| type_error("usage is not valid"))?,
|
2022-03-24 17:20:10 +00:00
|
|
|
mapped_at_creation,
|
2021-09-03 17:23:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
gfx_put!(device => instance.device_create_buffer(
|
|
|
|
device,
|
|
|
|
&descriptor,
|
2024-01-29 14:37:57 +00:00
|
|
|
None
|
2021-09-03 17:23:35 +00:00
|
|
|
) => state, WebGpuBuffer)
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub async fn op_webgpu_buffer_get_map_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] buffer_rid: ResourceId,
|
|
|
|
#[smi] device_rid: ResourceId,
|
2021-09-03 17:23:35 +00:00
|
|
|
mode: u32,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[number] offset: u64,
|
|
|
|
#[number] size: u64,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2022-10-13 19:41:52 +00:00
|
|
|
let (sender, receiver) = oneshot::channel::<BufferAccessResult>();
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let device;
|
|
|
|
{
|
|
|
|
let state_ = state.borrow();
|
|
|
|
let instance = state_.borrow::<super::Instance>();
|
2022-03-24 17:20:10 +00:00
|
|
|
let buffer_resource = state_.resource_table.get::<WebGpuBuffer>(buffer_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let buffer = buffer_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
let device_resource = state_
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::WebGpuDevice>(device_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
device = device_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-05-31 15:22:21 +00:00
|
|
|
let callback = Box::new(move |status| {
|
2022-10-13 19:41:52 +00:00
|
|
|
sender.send(status).unwrap();
|
2022-05-31 15:22:21 +00:00
|
|
|
});
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
// TODO(lucacasonato): error handling
|
|
|
|
let maybe_err = gfx_select!(buffer => instance.buffer_map_async(
|
2022-05-31 15:22:21 +00:00
|
|
|
buffer,
|
2024-02-09 08:48:00 +00:00
|
|
|
offset,
|
|
|
|
Some(size),
|
2022-05-31 15:22:21 +00:00
|
|
|
wgpu_core::resource::BufferMapOperation {
|
|
|
|
host: match mode {
|
|
|
|
1 => wgpu_core::device::HostMap::Read,
|
|
|
|
2 => wgpu_core::device::HostMap::Write,
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2023-11-20 07:41:52 +00:00
|
|
|
callback: Some(wgpu_core::resource::BufferMapCallback::from_rust(callback)),
|
2022-05-31 15:22:21 +00:00
|
|
|
}
|
2021-09-03 17:23:35 +00:00
|
|
|
))
|
|
|
|
.err();
|
|
|
|
|
|
|
|
if maybe_err.is_some() {
|
|
|
|
return Ok(WebGpuResult::maybe_err(maybe_err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let done = Rc::new(RefCell::new(false));
|
|
|
|
let done_ = done.clone();
|
|
|
|
let device_poll_fut = async move {
|
|
|
|
while !*done.borrow() {
|
|
|
|
{
|
|
|
|
let state = state.borrow();
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
2024-01-13 17:34:51 +00:00
|
|
|
gfx_select!(device => instance.device_poll(device, wgpu_types::Maintain::wait()))
|
2022-07-20 16:47:10 +00:00
|
|
|
.unwrap();
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_buffer_get_mapped_range(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] buffer_rid: ResourceId,
|
|
|
|
#[number] offset: u64,
|
|
|
|
#[number] size: Option<u64>,
|
|
|
|
#[buffer] buf: &mut [u8],
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
2022-03-24 17:20:10 +00:00
|
|
|
let buffer_resource = state.resource_table.get::<WebGpuBuffer>(buffer_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let buffer = buffer_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let (slice_pointer, range_size) = gfx_select!(buffer => instance.buffer_get_mapped_range(
|
|
|
|
buffer,
|
2022-03-24 17:20:10 +00:00
|
|
|
offset,
|
|
|
|
size
|
2021-09-03 17:23:35 +00:00
|
|
|
))
|
|
|
|
.map_err(|e| DomExceptionOperationError::new(&e.to_string()))?;
|
|
|
|
|
2024-04-02 18:36:04 +00:00
|
|
|
// SAFETY: guarantee to be safe from wgpu
|
2024-07-04 10:46:24 +00:00
|
|
|
let slice =
|
|
|
|
unsafe { std::slice::from_raw_parts_mut(slice_pointer.as_ptr(), range_size as usize) };
|
2022-03-24 17:20:10 +00:00
|
|
|
buf.copy_from_slice(slice);
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add(WebGpuBufferMapped(slice_pointer, range_size as usize));
|
|
|
|
|
|
|
|
Ok(WebGpuResult::rid(rid))
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_buffer_unmap(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] buffer_rid: ResourceId,
|
|
|
|
#[smi] mapped_rid: ResourceId,
|
|
|
|
#[buffer] buf: Option<&[u8]>,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let mapped_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.take::<WebGpuBufferMapped>(mapped_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
let instance = state.borrow::<super::Instance>();
|
2022-03-24 17:20:10 +00:00
|
|
|
let buffer_resource = state.resource_table.get::<WebGpuBuffer>(buffer_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let buffer = buffer_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
if let Some(buf) = buf {
|
2024-04-02 18:36:04 +00:00
|
|
|
// SAFETY: guarantee to be safe from wgpu
|
2024-07-04 10:46:24 +00:00
|
|
|
let slice = unsafe {
|
|
|
|
std::slice::from_raw_parts_mut(mapped_resource.0.as_ptr(), mapped_resource.1)
|
|
|
|
};
|
2023-10-04 04:25:56 +00:00
|
|
|
slice.copy_from_slice(buf);
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx_ok!(buffer => instance.buffer_unmap(buffer))
|
|
|
|
}
|