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::AnyError;
|
2022-03-19 05:31:10 +00:00
|
|
|
use deno_core::op;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::ResourceId;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
|
use super::error::WebGpuResult;
|
|
|
|
|
|
|
|
pub(crate) struct WebGpuComputePass(pub(crate) RefCell<wgpu_core::command::ComputePass>);
|
|
|
|
impl Resource for WebGpuComputePass {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUComputePass".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_set_pipeline(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
pipeline: ResourceId,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pipeline_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::pipeline::WebGpuComputePipeline>(pipeline)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_set_pipeline(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
2023-03-03 17:18:01 +00:00
|
|
|
compute_pipeline_resource.1,
|
2021-09-03 17:23:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
#[op]
|
2022-04-21 04:28:56 +00:00
|
|
|
pub fn op_webgpu_compute_pass_dispatch_workgroups(
|
2022-03-24 17:20:10 +00:00
|
|
|
state: &mut OpState,
|
2021-09-03 17:23:35 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
x: u32,
|
|
|
|
y: u32,
|
|
|
|
z: u32,
|
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-04-21 04:28:56 +00:00
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_dispatch_workgroups(
|
2021-09-03 17:23:35 +00:00
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
2022-03-24 17:20:10 +00:00
|
|
|
x,
|
|
|
|
y,
|
|
|
|
z,
|
2021-09-03 17:23:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2022-04-21 04:28:56 +00:00
|
|
|
pub fn op_webgpu_compute_pass_dispatch_workgroups_indirect(
|
2021-09-03 17:23:35 +00:00
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
indirect_buffer: ResourceId,
|
|
|
|
indirect_offset: u64,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let buffer_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::buffer::WebGpuBuffer>(indirect_buffer)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-04-21 04:28:56 +00:00
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_dispatch_workgroups_indirect(
|
2021-09-03 17:23:35 +00:00
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
2023-03-03 17:18:01 +00:00
|
|
|
buffer_resource.1,
|
2022-03-24 17:20:10 +00:00
|
|
|
indirect_offset,
|
2021-09-03 17:23:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_begin_pipeline_statistics_query(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
query_set: ResourceId,
|
|
|
|
query_index: u32,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
let query_set_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::WebGpuQuerySet>(query_set)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_begin_pipeline_statistics_query(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
2023-03-03 17:18:01 +00:00
|
|
|
query_set_resource.1,
|
2022-03-24 17:20:10 +00:00
|
|
|
query_index,
|
2021-09-03 17:23:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_end_pipeline_statistics_query(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_end_pipeline_statistics_query(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_write_timestamp(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
query_set: ResourceId,
|
|
|
|
query_index: u32,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
let query_set_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::WebGpuQuerySet>(query_set)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_write_timestamp(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
2023-03-03 17:18:01 +00:00
|
|
|
query_set_resource.1,
|
2022-03-24 17:20:10 +00:00
|
|
|
query_index,
|
2021-09-03 17:23:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
|
|
|
pub fn op_webgpu_compute_pass_end(
|
2021-09-03 17:23:35 +00:00
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
command_encoder_rid: ResourceId,
|
|
|
|
compute_pass_rid: ResourceId,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let command_encoder_resource =
|
|
|
|
state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::command_encoder::WebGpuCommandEncoder>(command_encoder_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let command_encoder = command_encoder_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.take::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
let compute_pass = &compute_pass_resource.0.borrow();
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
|
|
|
|
gfx_ok!(command_encoder => instance.command_encoder_run_compute_pass(
|
|
|
|
command_encoder,
|
|
|
|
compute_pass
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
#[op]
|
|
|
|
pub fn op_webgpu_compute_pass_set_bind_group(
|
|
|
|
state: &mut OpState,
|
2021-09-03 17:23:35 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
index: u32,
|
|
|
|
bind_group: ResourceId,
|
|
|
|
dynamic_offsets_data: ZeroCopyBuf,
|
|
|
|
dynamic_offsets_data_start: usize,
|
|
|
|
dynamic_offsets_data_length: usize,
|
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let bind_group_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::binding::WebGpuBindGroup>(bind_group)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
// Align the data
|
2022-03-24 17:20:10 +00:00
|
|
|
assert!(dynamic_offsets_data_start % std::mem::size_of::<u32>() == 0);
|
2021-09-03 17:23:35 +00:00
|
|
|
// SAFETY: A u8 to u32 cast is safe because we asserted that the length is a
|
|
|
|
// multiple of 4.
|
2022-03-24 17:20:10 +00:00
|
|
|
let (prefix, dynamic_offsets_data, suffix) = unsafe { dynamic_offsets_data.align_to::<u32>() };
|
2021-09-03 17:23:35 +00:00
|
|
|
assert!(prefix.is_empty());
|
|
|
|
assert!(suffix.is_empty());
|
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let start = dynamic_offsets_data_start;
|
|
|
|
let len = dynamic_offsets_data_length;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
unsafe {
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_set_bind_group(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
2022-03-24 17:20:10 +00:00
|
|
|
index,
|
2023-03-03 17:18:01 +00:00
|
|
|
bind_group_resource.1,
|
2021-09-03 17:23:35 +00:00
|
|
|
dynamic_offsets_data.as_ptr(),
|
|
|
|
dynamic_offsets_data.len(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_push_debug_group(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
group_label: String,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let label = std::ffi::CString::new(group_label).unwrap();
|
2021-09-03 17:23:35 +00:00
|
|
|
// SAFETY: the string the raw pointer points to lives longer than the below
|
|
|
|
// function invocation.
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_pop_debug_group(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
wgpu_core::command::compute_ffi::wgpu_compute_pass_pop_debug_group(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_insert_debug_marker(
|
|
|
|
state: &mut OpState,
|
2022-03-24 17:20:10 +00:00
|
|
|
compute_pass_rid: ResourceId,
|
|
|
|
marker_label: String,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let compute_pass_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let label = std::ffi::CString::new(marker_label).unwrap();
|
2021-09-03 17:23:35 +00:00
|
|
|
// SAFETY: the string the raw pointer points to lives longer than the below
|
|
|
|
// function invocation.
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|