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
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2023-10-04 04:25:56 +00:00
|
|
|
use deno_core::op2;
|
2022-03-19 05:31:10 +00:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::ResourceId;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
|
use super::error::WebGpuResult;
|
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
pub(crate) struct WebGpuComputePass(pub(crate) RefCell<wgpu_core::command::ComputePass>);
|
2021-09-03 17:23:35 +00:00
|
|
|
impl Resource for WebGpuComputePass {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUComputePass".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_set_pipeline(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] compute_pass_rid: ResourceId,
|
|
|
|
#[smi] 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
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_set_pipeline(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
compute_pipeline_resource.1,
|
|
|
|
)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
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,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] compute_pass_rid: ResourceId,
|
2021-09-03 17:23:35 +00:00
|
|
|
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
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_dispatch_workgroups(&mut compute_pass_resource.0.borrow_mut(), x, y, z)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
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,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] compute_pass_rid: ResourceId,
|
|
|
|
#[smi] indirect_buffer: ResourceId,
|
|
|
|
#[number] 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
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_dispatch_workgroups_indirect(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
buffer_resource.1,
|
|
|
|
indirect_offset,
|
|
|
|
)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-19 05:31:10 +00:00
|
|
|
pub fn op_webgpu_compute_pass_end(
|
2021-09-03 17:23:35 +00:00
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] 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
|
|
|
.take::<WebGpuComputePass>(compute_pass_rid)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_end(&mut compute_pass_resource.0.borrow_mut())?;
|
2024-05-14 20:05:17 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
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_compute_pass_set_bind_group(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] compute_pass_rid: ResourceId,
|
2021-09-03 17:23:35 +00:00
|
|
|
index: u32,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] bind_group: ResourceId,
|
|
|
|
#[buffer] dynamic_offsets_data: &[u32],
|
|
|
|
#[number] dynamic_offsets_data_start: usize,
|
|
|
|
#[number] dynamic_offsets_data_length: usize,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> 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
|
|
|
|
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];
|
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_set_bind_group(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
index,
|
2024-09-06 21:29:09 +00:00
|
|
|
Some(bind_group_resource.1),
|
2024-08-06 21:29:21 +00:00
|
|
|
dynamic_offsets_data,
|
|
|
|
)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_push_debug_group(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] compute_pass_rid: ResourceId,
|
|
|
|
#[string] group_label: &str,
|
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
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_push_debug_group(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
group_label,
|
|
|
|
0, // wgpu#975
|
|
|
|
)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_pop_debug_group(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] 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
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_pop_debug_group(&mut compute_pass_resource.0.borrow_mut())?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_compute_pass_insert_debug_marker(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] compute_pass_rid: ResourceId,
|
|
|
|
#[string] marker_label: &str,
|
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
|
|
|
|
2024-08-06 21:29:21 +00:00
|
|
|
state
|
|
|
|
.borrow::<wgpu_core::global::Global>()
|
|
|
|
.compute_pass_insert_debug_marker(
|
|
|
|
&mut compute_pass_resource.0.borrow_mut(),
|
|
|
|
marker_label,
|
|
|
|
0, // wgpu#975
|
|
|
|
)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
Ok(WebGpuResult::empty())
|
|
|
|
}
|