2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-05-06 10:48:45 -04:00
|
|
|
use std::num::NonZeroU32;
|
|
|
|
|
2021-03-01 05:31:13 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::OpState;
|
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;
|
|
|
|
|
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
|
|
|
type WebGpuQueue = super::WebGpuDevice;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct QueueSubmitArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
queue_rid: ResourceId,
|
2021-08-24 14:32:25 -04:00
|
|
|
command_buffers: Vec<ResourceId>,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_webgpu_queue_submit(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: QueueSubmitArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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>();
|
2021-08-15 07:29:19 -04:00
|
|
|
let queue_resource =
|
|
|
|
state.resource_table.get::<WebGpuQueue>(args.queue_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let queue = queue_resource.0;
|
|
|
|
|
|
|
|
let mut ids = vec![];
|
|
|
|
|
|
|
|
for rid in args.command_buffers {
|
2021-08-15 07:29:19 -04:00
|
|
|
let buffer_resource =
|
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.get::<super::command_encoder::WebGpuCommandBuffer>(rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
ids.push(buffer_resource.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let maybe_err =
|
|
|
|
gfx_select!(queue => instance.queue_submit(queue, &ids)).err();
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(WebGpuResult::maybe_err(maybe_err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-25 14:17:37 -04:00
|
|
|
struct GpuImageDataLayout {
|
2021-08-24 14:32:25 -04:00
|
|
|
offset: u64,
|
2021-03-01 05:31:13 -05:00
|
|
|
bytes_per_row: Option<u32>,
|
|
|
|
rows_per_image: Option<u32>,
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:32:25 -04:00
|
|
|
impl From<GpuImageDataLayout> for wgpu_types::ImageDataLayout {
|
|
|
|
fn from(layout: GpuImageDataLayout) -> Self {
|
|
|
|
wgpu_types::ImageDataLayout {
|
|
|
|
offset: layout.offset,
|
|
|
|
bytes_per_row: NonZeroU32::new(layout.bytes_per_row.unwrap_or(0)),
|
|
|
|
rows_per_image: NonZeroU32::new(layout.rows_per_image.unwrap_or(0)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 05:31:13 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct QueueWriteBufferArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
queue_rid: ResourceId,
|
2021-08-24 14:32:25 -04:00
|
|
|
buffer: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
buffer_offset: u64,
|
|
|
|
data_offset: usize,
|
|
|
|
size: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_webgpu_write_buffer(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: QueueWriteBufferArgs,
|
2021-10-05 16:38:27 -04:00
|
|
|
zero_copy: 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 buffer_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<super::buffer::WebGpuBuffer>(args.buffer)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let buffer = buffer_resource.0;
|
2021-08-15 07:29:19 -04:00
|
|
|
let queue_resource =
|
|
|
|
state.resource_table.get::<WebGpuQueue>(args.queue_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let queue = queue_resource.0;
|
|
|
|
|
|
|
|
let data = match args.size {
|
2021-04-02 09:47:57 -04:00
|
|
|
Some(size) => &zero_copy[args.data_offset..(args.data_offset + size)],
|
|
|
|
None => &zero_copy[args.data_offset..],
|
2021-03-01 05:31:13 -05:00
|
|
|
};
|
|
|
|
let maybe_err = gfx_select!(queue => instance.queue_write_buffer(
|
|
|
|
queue,
|
|
|
|
buffer,
|
|
|
|
args.buffer_offset,
|
|
|
|
data
|
|
|
|
))
|
|
|
|
.err();
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(WebGpuResult::maybe_err(maybe_err))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct QueueWriteTextureArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
queue_rid: ResourceId,
|
2021-03-25 14:17:37 -04:00
|
|
|
destination: super::command_encoder::GpuImageCopyTexture,
|
|
|
|
data_layout: GpuImageDataLayout,
|
2022-01-20 09:23:53 -05:00
|
|
|
size: super::texture::GpuExtent3D,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_webgpu_write_texture(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: QueueWriteTextureArgs,
|
2021-10-05 16:38:27 -04:00
|
|
|
zero_copy: 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 texture_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<super::texture::WebGpuTexture>(args.destination.texture)?;
|
|
|
|
let queue_resource =
|
|
|
|
state.resource_table.get::<WebGpuQueue>(args.queue_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let queue = queue_resource.0;
|
|
|
|
|
2021-05-06 10:48:45 -04:00
|
|
|
let destination = wgpu_core::command::ImageCopyTexture {
|
2021-03-01 05:31:13 -05:00
|
|
|
texture: texture_resource.0,
|
2021-08-24 14:32:25 -04:00
|
|
|
mip_level: args.destination.mip_level,
|
2022-01-20 09:23:53 -05:00
|
|
|
origin: args.destination.origin.into(),
|
|
|
|
aspect: args.destination.aspect.into(),
|
2021-03-01 05:31:13 -05:00
|
|
|
};
|
2021-08-24 14:32:25 -04:00
|
|
|
let data_layout = args.data_layout.into();
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-05-03 10:42:59 -04:00
|
|
|
gfx_ok!(queue => instance.queue_write_texture(
|
2021-03-01 05:31:13 -05:00
|
|
|
queue,
|
|
|
|
&destination,
|
2021-04-02 09:47:57 -04:00
|
|
|
&*zero_copy,
|
2021-03-01 05:31:13 -05:00
|
|
|
&data_layout,
|
2022-01-20 09:23:53 -05:00
|
|
|
&args.size.into()
|
2021-03-01 05:31:13 -05:00
|
|
|
))
|
|
|
|
}
|