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
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
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 deno_core::{OpState, Resource};
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
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 WebGpuComputePass(
|
2021-03-01 05:31:13 -05:00
|
|
|
pub(crate) RefCell<wgpu_core::command::ComputePass>,
|
|
|
|
);
|
2021-03-25 14:17:37 -04:00
|
|
|
impl Resource for WebGpuComputePass {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUComputePass".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ComputePassSetPipelineArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-08-24 14:32:25 -04:00
|
|
|
pipeline: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_set_pipeline(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassSetPipelineArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-08-15 07:29:19 -04:00
|
|
|
let compute_pipeline_resource =
|
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.get::<super::pipeline::WebGpuComputePipeline>(args.pipeline)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_set_pipeline(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
compute_pipeline_resource.0,
|
|
|
|
);
|
|
|
|
|
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 ComputePassDispatchArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
x: u32,
|
|
|
|
y: u32,
|
|
|
|
z: u32,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_dispatch(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassDispatchArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_dispatch(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
args.x,
|
|
|
|
args.y,
|
|
|
|
args.z,
|
|
|
|
);
|
|
|
|
|
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 ComputePassDispatchIndirectArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-08-24 14:32:25 -04:00
|
|
|
indirect_buffer: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
indirect_offset: u64,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_dispatch_indirect(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassDispatchIndirectArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let buffer_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<super::buffer::WebGpuBuffer>(args.indirect_buffer)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_dispatch_indirect(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
buffer_resource.0,
|
|
|
|
args.indirect_offset,
|
|
|
|
);
|
|
|
|
|
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 ComputePassBeginPipelineStatisticsQueryArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-08-24 14:32:25 -04:00
|
|
|
query_set: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
query_index: u32,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_begin_pipeline_statistics_query(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassBeginPipelineStatisticsQueryArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let query_set_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<super::WebGpuQuerySet>(args.query_set)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-05-06 10:48:45 -04:00
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_begin_pipeline_statistics_query(
|
2022-01-24 17:47:05 -05:00
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
query_set_resource.0,
|
|
|
|
args.query_index,
|
|
|
|
);
|
2021-03-01 05:31:13 -05:00
|
|
|
|
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 ComputePassEndPipelineStatisticsQueryArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_end_pipeline_statistics_query(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassEndPipelineStatisticsQueryArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-05-06 10:48:45 -04:00
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_end_pipeline_statistics_query(
|
2022-01-24 17:47:05 -05:00
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
);
|
2021-03-01 05:31:13 -05:00
|
|
|
|
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 ComputePassWriteTimestampArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-08-24 14:32:25 -04:00
|
|
|
query_set: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
query_index: u32,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_write_timestamp(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassWriteTimestampArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let query_set_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<super::WebGpuQuerySet>(args.query_set)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-05-06 10:48:45 -04:00
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_write_timestamp(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
query_set_resource.0,
|
|
|
|
args.query_index,
|
|
|
|
);
|
2021-03-01 05:31:13 -05:00
|
|
|
|
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 ComputePassEndPassArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
command_encoder_rid: ResourceId,
|
|
|
|
compute_pass_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_end_pass(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassEndPassArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let command_encoder_resource = state
|
|
|
|
.resource_table
|
2021-03-25 14:17:37 -04:00
|
|
|
.get::<super::command_encoder::WebGpuCommandEncoder>(
|
2021-08-15 07:29:19 -04:00
|
|
|
args.command_encoder_rid,
|
|
|
|
)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let command_encoder = command_encoder_resource.0;
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.take::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass = &compute_pass_resource.0.borrow();
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
|
2021-05-03 10:42:59 -04:00
|
|
|
gfx_ok!(command_encoder => instance.command_encoder_run_compute_pass(
|
|
|
|
command_encoder,
|
|
|
|
compute_pass
|
|
|
|
))
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ComputePassSetBindGroupArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
index: u32,
|
2021-08-24 14:32:25 -04:00
|
|
|
bind_group: ResourceId,
|
|
|
|
dynamic_offsets_data: ZeroCopyBuf,
|
2021-03-01 05:31:13 -05:00
|
|
|
dynamic_offsets_data_start: usize,
|
|
|
|
dynamic_offsets_data_length: usize,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_set_bind_group(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassSetBindGroupArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-08-15 07:29:19 -04:00
|
|
|
let bind_group_resource =
|
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.get::<super::binding::WebGpuBindGroup>(args.bind_group)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-08-24 14:32:25 -04:00
|
|
|
// Align the data
|
|
|
|
assert!(args.dynamic_offsets_data_start % std::mem::size_of::<u32>() == 0);
|
|
|
|
let (prefix, dynamic_offsets_data, suffix) =
|
2022-07-01 09:28:06 -04:00
|
|
|
// SAFETY: A u8 to u32 cast is safe because we asserted that the length is a
|
|
|
|
// multiple of 4.
|
2021-08-24 14:32:25 -04:00
|
|
|
unsafe { args.dynamic_offsets_data.align_to::<u32>() };
|
|
|
|
assert!(prefix.is_empty());
|
|
|
|
assert!(suffix.is_empty());
|
|
|
|
|
|
|
|
let start = args.dynamic_offsets_data_start;
|
|
|
|
let len = args.dynamic_offsets_data_length;
|
|
|
|
|
|
|
|
// Assert that length and start are both in bounds
|
|
|
|
assert!(start <= dynamic_offsets_data.len());
|
|
|
|
assert!(len <= dynamic_offsets_data.len() - start);
|
|
|
|
|
|
|
|
let dynamic_offsets_data: &[u32] = &dynamic_offsets_data[start..start + len];
|
|
|
|
|
|
|
|
// SAFETY: the raw pointer and length are of the same slice, and that slice
|
|
|
|
// lives longer than the below function invocation.
|
2021-03-01 05:31:13 -05:00
|
|
|
unsafe {
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_set_bind_group(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
args.index,
|
|
|
|
bind_group_resource.0,
|
2021-08-24 14:32:25 -04:00
|
|
|
dynamic_offsets_data.as_ptr(),
|
|
|
|
dynamic_offsets_data.len(),
|
2021-03-01 05:31:13 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 ComputePassPushDebugGroupArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
group_label: String,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_push_debug_group(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassPushDebugGroupArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-08-24 14:32:25 -04:00
|
|
|
let label = std::ffi::CString::new(args.group_label).unwrap();
|
|
|
|
// SAFETY: the string the raw pointer points to lives longer than the below
|
|
|
|
// function invocation.
|
2021-03-01 05:31:13 -05:00
|
|
|
unsafe {
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_push_debug_group(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
label.as_ptr(),
|
|
|
|
0, // wgpu#975
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 ComputePassPopDebugGroupArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_pop_debug_group(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassPopDebugGroupArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_pop_debug_group(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
);
|
|
|
|
|
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 ComputePassInsertDebugMarkerArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
marker_label: String,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-03-01 05:31:13 -05:00
|
|
|
pub fn op_webgpu_compute_pass_insert_debug_marker(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ComputePassInsertDebugMarkerArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<WebGpuComputePass>(args.compute_pass_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-08-24 14:32:25 -04:00
|
|
|
let label = std::ffi::CString::new(args.marker_label).unwrap();
|
|
|
|
// SAFETY: the string the raw pointer points to lives longer than the below
|
|
|
|
// function invocation.
|
2021-03-01 05:31:13 -05:00
|
|
|
unsafe {
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_insert_debug_marker(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
label.as_ptr(),
|
|
|
|
0, // wgpu#975
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(WebGpuResult::empty())
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|