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
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
use crate::command_encoder::WebGpuCommandBuffer;
|
2024-04-02 18:36:04 +00:00
|
|
|
use crate::Instance;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::error::AnyError;
|
2023-10-04 04:25:56 +00:00
|
|
|
use deno_core::op2;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::OpState;
|
2023-10-04 04:25:56 +00:00
|
|
|
use deno_core::Resource;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::ResourceId;
|
|
|
|
use serde::Deserialize;
|
2024-04-02 18:36:04 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::rc::Rc;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
use super::error::WebGpuResult;
|
|
|
|
|
2024-04-02 18:36:04 +00:00
|
|
|
pub struct WebGpuQueue(pub Instance, pub wgpu_core::id::QueueId);
|
|
|
|
impl Resource for WebGpuQueue {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUQueue".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
2024-08-06 21:17:47 +00:00
|
|
|
self.0.queue_drop(self.1);
|
2024-04-02 18:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_queue_submit(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] queue_rid: ResourceId,
|
|
|
|
#[serde] command_buffers: Vec<ResourceId>,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2024-04-02 18:36:04 +00:00
|
|
|
let instance = state.borrow::<Instance>();
|
2022-03-24 17:20:10 +00:00
|
|
|
let queue_resource = state.resource_table.get::<WebGpuQueue>(queue_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let queue = queue_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let ids = command_buffers
|
|
|
|
.iter()
|
|
|
|
.map(|rid| {
|
2023-10-04 04:25:56 +00:00
|
|
|
let buffer_resource = state.resource_table.get::<WebGpuCommandBuffer>(*rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let mut id = buffer_resource.1.borrow_mut();
|
|
|
|
Ok(id.take().unwrap())
|
2022-03-24 17:20:10 +00:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, AnyError>>()?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2024-08-06 21:17:47 +00:00
|
|
|
let maybe_err = instance.queue_submit(queue, &ids).err();
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
for rid in command_buffers {
|
2023-10-04 04:25:56 +00:00
|
|
|
let resource = state.resource_table.take::<WebGpuCommandBuffer>(rid)?;
|
|
|
|
resource.close();
|
2022-02-02 13:52:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 17:23:35 +00:00
|
|
|
Ok(WebGpuResult::maybe_err(maybe_err))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub struct GpuImageDataLayout {
|
2021-09-03 17:23:35 +00:00
|
|
|
offset: u64,
|
|
|
|
bytes_per_row: Option<u32>,
|
|
|
|
rows_per_image: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GpuImageDataLayout> for wgpu_types::ImageDataLayout {
|
|
|
|
fn from(layout: GpuImageDataLayout) -> Self {
|
|
|
|
wgpu_types::ImageDataLayout {
|
|
|
|
offset: layout.offset,
|
2023-03-06 21:44:36 +00:00
|
|
|
bytes_per_row: layout.bytes_per_row,
|
|
|
|
rows_per_image: layout.rows_per_image,
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub fn op_webgpu_write_buffer(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] queue_rid: ResourceId,
|
|
|
|
#[smi] buffer: ResourceId,
|
|
|
|
#[number] buffer_offset: u64,
|
|
|
|
#[number] data_offset: usize,
|
|
|
|
#[number] size: Option<usize>,
|
|
|
|
#[buffer] buf: &[u8],
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2024-04-02 18:36:04 +00:00
|
|
|
let instance = state.borrow::<Instance>();
|
2021-09-03 17:23:35 +00:00
|
|
|
let buffer_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::buffer::WebGpuBuffer>(buffer)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let buffer = buffer_resource.1;
|
2022-03-24 17:20:10 +00:00
|
|
|
let queue_resource = state.resource_table.get::<WebGpuQueue>(queue_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let queue = queue_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let data = match size {
|
|
|
|
Some(size) => &buf[data_offset..(data_offset + size)],
|
|
|
|
None => &buf[data_offset..],
|
2021-09-03 17:23:35 +00:00
|
|
|
};
|
2024-08-06 21:17:47 +00:00
|
|
|
let maybe_err = instance
|
|
|
|
.queue_write_buffer(queue, buffer, buffer_offset, data)
|
|
|
|
.err();
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::maybe_err(maybe_err))
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub fn op_webgpu_write_texture(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] queue_rid: ResourceId,
|
|
|
|
#[serde] destination: super::command_encoder::GpuImageCopyTexture,
|
|
|
|
#[serde] data_layout: GpuImageDataLayout,
|
|
|
|
#[serde] size: wgpu_types::Extent3d,
|
|
|
|
#[buffer] buf: &[u8],
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2024-04-02 18:36:04 +00:00
|
|
|
let instance = state.borrow::<Instance>();
|
2021-09-03 17:23:35 +00:00
|
|
|
let texture_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::texture::WebGpuTexture>(destination.texture)?;
|
|
|
|
let queue_resource = state.resource_table.get::<WebGpuQueue>(queue_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let queue = queue_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let destination = wgpu_core::command::ImageCopyTexture {
|
2023-03-03 17:18:01 +00:00
|
|
|
texture: texture_resource.id,
|
2022-03-24 17:20:10 +00:00
|
|
|
mip_level: destination.mip_level,
|
|
|
|
origin: destination.origin,
|
|
|
|
aspect: destination.aspect,
|
2021-09-03 17:23:35 +00:00
|
|
|
};
|
2022-03-24 17:20:10 +00:00
|
|
|
let data_layout = data_layout.into();
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2024-08-06 21:17:47 +00:00
|
|
|
gfx_ok!(instance.queue_write_texture(queue, &destination, buf, &data_layout, &size))
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|