2021-03-01 05:31:13 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use deno_core::error::bad_resource_id;
|
2021-04-02 09:47:57 -04:00
|
|
|
use deno_core::error::null_opbuf;
|
2021-03-01 05:31:13 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::channel::oneshot;
|
|
|
|
use deno_core::OpState;
|
2021-04-02 09:47:57 -04:00
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2021-03-01 05:31:13 -05:00
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
use super::error::DomExceptionOperationError;
|
2021-04-05 12:40:24 -04:00
|
|
|
use super::error::WebGpuResult;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
pub(crate) struct WebGpuBuffer(pub(crate) wgpu_core::id::BufferId);
|
|
|
|
impl Resource for WebGpuBuffer {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUBuffer".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
struct WebGpuBufferMapped(*mut u8, usize);
|
|
|
|
impl Resource for WebGpuBufferMapped {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUBufferMapped".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CreateBufferArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
device_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
label: Option<String>,
|
|
|
|
size: u64,
|
|
|
|
usage: u32,
|
|
|
|
mapped_at_creation: Option<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_webgpu_create_buffer(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: CreateBufferArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let device_resource = state
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.get::<super::WebGpuDevice>(args.device_rid)
|
2021-03-01 05:31:13 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let device = device_resource.0;
|
|
|
|
|
|
|
|
let descriptor = wgpu_core::resource::BufferDescriptor {
|
|
|
|
label: args.label.map(Cow::from),
|
|
|
|
size: args.size,
|
|
|
|
usage: wgpu_types::BufferUsage::from_bits(args.usage).unwrap(),
|
|
|
|
mapped_at_creation: args.mapped_at_creation.unwrap_or(false),
|
|
|
|
};
|
|
|
|
|
2021-05-03 10:42:59 -04:00
|
|
|
gfx_put!(device => instance.device_create_buffer(
|
2021-03-01 05:31:13 -05:00
|
|
|
device,
|
|
|
|
&descriptor,
|
|
|
|
std::marker::PhantomData
|
2021-05-03 10:42:59 -04:00
|
|
|
) => state, WebGpuBuffer)
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct BufferGetMapAsyncArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
buffer_rid: ResourceId,
|
|
|
|
device_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
mode: u32,
|
|
|
|
offset: u64,
|
|
|
|
size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn op_webgpu_buffer_get_map_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
args: BufferGetMapAsyncArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_bufs: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let (sender, receiver) = oneshot::channel::<Result<(), AnyError>>();
|
|
|
|
|
|
|
|
let device;
|
|
|
|
{
|
|
|
|
let state_ = state.borrow();
|
|
|
|
let instance = state_.borrow::<super::Instance>();
|
|
|
|
let buffer_resource = state_
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.get::<WebGpuBuffer>(args.buffer_rid)
|
2021-03-01 05:31:13 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let buffer = buffer_resource.0;
|
|
|
|
let device_resource = state_
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.get::<super::WebGpuDevice>(args.device_rid)
|
2021-03-01 05:31:13 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
device = device_resource.0;
|
|
|
|
|
|
|
|
let boxed_sender = Box::new(sender);
|
|
|
|
let sender_ptr = Box::into_raw(boxed_sender) as *mut u8;
|
|
|
|
|
|
|
|
extern "C" fn buffer_map_future_wrapper(
|
|
|
|
status: wgpu_core::resource::BufferMapAsyncStatus,
|
|
|
|
user_data: *mut u8,
|
|
|
|
) {
|
|
|
|
let sender_ptr = user_data as *mut oneshot::Sender<Result<(), AnyError>>;
|
|
|
|
let boxed_sender = unsafe { Box::from_raw(sender_ptr) };
|
|
|
|
boxed_sender
|
|
|
|
.send(match status {
|
|
|
|
wgpu_core::resource::BufferMapAsyncStatus::Success => Ok(()),
|
|
|
|
_ => unreachable!(), // TODO
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(lucacasonato): error handling
|
|
|
|
gfx_select!(buffer => instance.buffer_map_async(
|
|
|
|
buffer,
|
|
|
|
args.offset..(args.offset + args.size),
|
|
|
|
wgpu_core::resource::BufferMapOperation {
|
|
|
|
host: match args.mode {
|
|
|
|
1 => wgpu_core::device::HostMap::Read,
|
|
|
|
2 => wgpu_core::device::HostMap::Write,
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
callback: buffer_map_future_wrapper,
|
|
|
|
user_data: sender_ptr,
|
|
|
|
}
|
|
|
|
))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
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>();
|
|
|
|
gfx_select!(device => instance.device_poll(device, false)).unwrap()
|
|
|
|
}
|
|
|
|
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)?;
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(WebGpuResult::empty())
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct BufferGetMappedRangeArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
buffer_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
offset: u64,
|
|
|
|
size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_webgpu_buffer_get_mapped_range(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: BufferGetMappedRangeArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-04-02 09:47:57 -04:00
|
|
|
let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let buffer_resource = state
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.get::<WebGpuBuffer>(args.buffer_rid)
|
2021-03-01 05:31:13 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let buffer = buffer_resource.0;
|
|
|
|
|
|
|
|
let slice_pointer = gfx_select!(buffer => instance.buffer_get_mapped_range(
|
|
|
|
buffer,
|
|
|
|
args.offset,
|
|
|
|
std::num::NonZeroU64::new(args.size)
|
|
|
|
))
|
2021-03-25 14:17:37 -04:00
|
|
|
.map_err(|e| DomExceptionOperationError::new(&e.to_string()))?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
let slice = unsafe {
|
|
|
|
std::slice::from_raw_parts_mut(slice_pointer, args.size as usize)
|
|
|
|
};
|
2021-04-02 09:47:57 -04:00
|
|
|
zero_copy.copy_from_slice(slice);
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
let rid = state
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.add(WebGpuBufferMapped(slice_pointer, args.size as usize));
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(WebGpuResult::rid(rid))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct BufferUnmapArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
buffer_rid: ResourceId,
|
|
|
|
mapped_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_webgpu_buffer_unmap(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: BufferUnmapArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let mapped_resource = state
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.take::<WebGpuBufferMapped>(args.mapped_rid)
|
2021-03-01 05:31:13 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let buffer_resource = state
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.get::<WebGpuBuffer>(args.buffer_rid)
|
2021-03-01 05:31:13 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let buffer = buffer_resource.0;
|
|
|
|
|
|
|
|
let slice_pointer = mapped_resource.0;
|
|
|
|
let size = mapped_resource.1;
|
|
|
|
|
2021-04-02 09:47:57 -04:00
|
|
|
if let Some(buffer) = zero_copy {
|
2021-03-01 05:31:13 -05:00
|
|
|
let slice = unsafe { std::slice::from_raw_parts_mut(slice_pointer, size) };
|
|
|
|
slice.copy_from_slice(&buffer);
|
|
|
|
}
|
|
|
|
|
2021-05-03 10:42:59 -04:00
|
|
|
gfx_ok!(buffer => instance.buffer_unmap(buffer))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|