2023-01-14 16:07:46 +00:00
|
|
|
// Copyright 2018-2023 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;
|
2022-03-19 05:31:10 +00:00
|
|
|
use deno_core::op;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
2022-03-19 05:31:10 +00:00
|
|
|
use std::convert::TryFrom;
|
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>) {
|
|
|
|
let instance = &self.0;
|
|
|
|
gfx_select!(self.1 => instance.buffer_drop(self.1, true));
|
|
|
|
}
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct WebGpuBufferMapped(*mut u8, usize);
|
|
|
|
impl Resource for WebGpuBufferMapped {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUBufferMapped".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
#[op]
|
|
|
|
pub fn op_webgpu_create_buffer(
|
|
|
|
state: &mut OpState,
|
2021-09-03 17:23:35 +00:00
|
|
|
device_rid: ResourceId,
|
|
|
|
label: Option<String>,
|
|
|
|
size: u64,
|
|
|
|
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 {
|
2022-03-24 17:20:10 +00:00
|
|
|
label: label.map(Cow::from),
|
|
|
|
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,
|
2022-09-02 22:37:21 +00:00
|
|
|
()
|
2021-09-03 17:23:35 +00:00
|
|
|
) => state, WebGpuBuffer)
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
#[op]
|
|
|
|
pub async fn op_webgpu_buffer_get_map_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-09-03 17:23:35 +00:00
|
|
|
buffer_rid: ResourceId,
|
|
|
|
device_rid: ResourceId,
|
|
|
|
mode: u32,
|
|
|
|
offset: u64,
|
|
|
|
size: u64,
|
|
|
|
) -> 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,
|
|
|
|
offset..(offset + size),
|
|
|
|
wgpu_core::resource::BufferMapOperation {
|
|
|
|
host: match mode {
|
|
|
|
1 => wgpu_core::device::HostMap::Read,
|
|
|
|
2 => wgpu_core::device::HostMap::Write,
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
callback: wgpu_core::resource::BufferMapCallback::from_rust(callback),
|
|
|
|
}
|
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>();
|
2022-07-20 16:47:10 +00:00
|
|
|
gfx_select!(device => instance.device_poll(device, wgpu_types::Maintain::Wait))
|
|
|
|
.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())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_buffer_get_mapped_range(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
buffer_rid: ResourceId,
|
|
|
|
offset: u64,
|
|
|
|
size: Option<u64>,
|
|
|
|
mut buf: ZeroCopyBuf,
|
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()))?;
|
|
|
|
|
|
|
|
let slice = unsafe { std::slice::from_raw_parts_mut(slice_pointer, 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))
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_buffer_unmap(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
buffer_rid: ResourceId,
|
|
|
|
mapped_rid: ResourceId,
|
|
|
|
buf: Option<ZeroCopyBuf>,
|
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 {
|
|
|
|
let slice = unsafe { std::slice::from_raw_parts_mut(mapped_resource.0, mapped_resource.1) };
|
|
|
|
slice.copy_from_slice(&buf);
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx_ok!(buffer => instance.buffer_unmap(buffer))
|
|
|
|
}
|