Skip to content

Commit

Permalink
fix deno issues -> move DynComputePass into wgc
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Apr 21, 2024
1 parent 3c28c19 commit b976ff1
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 199 deletions.
5 changes: 3 additions & 2 deletions deno_webgpu/command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,14 @@ pub fn op_webgpu_command_encoder_begin_compute_pass(
None
};

let instance = state.borrow::<super::Instance>();
let command_encoder = &command_encoder_resource.1;
let descriptor = wgpu_core::command::ComputePassDescriptor {
label: Some(label),
timestamp_writes: timestamp_writes.as_ref(),
};

let compute_pass =
wgpu_core::command::ComputePass::new(command_encoder_resource.1, &descriptor);
let compute_pass = gfx_select!(command_encoder => instance.command_encoder_create_compute_pass_dyn(*command_encoder, &descriptor));

let rid = state
.resource_table
Expand Down
67 changes: 29 additions & 38 deletions deno_webgpu/compute_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use std::cell::RefCell;

use super::error::WebGpuResult;

pub(crate) struct WebGpuComputePass(pub(crate) RefCell<wgpu_core::command::ComputePass>);
pub(crate) struct WebGpuComputePass(
pub(crate) RefCell<Box<dyn wgpu_core::command::DynComputePass>>,
);
impl Resource for WebGpuComputePass {
fn name(&self) -> Cow<str> {
"webGPUComputePass".into()
Expand All @@ -31,10 +33,10 @@ pub fn op_webgpu_compute_pass_set_pipeline(
.resource_table
.get::<WebGpuComputePass>(compute_pass_rid)?;

wgpu_core::command::compute_commands::wgpu_compute_pass_set_pipeline(
&mut compute_pass_resource.0.borrow_mut(),
compute_pipeline_resource.1,
);
compute_pass_resource
.0
.borrow_mut()
.set_pipeline(state.borrow(), compute_pipeline_resource.1)?;

Ok(WebGpuResult::empty())
}
Expand All @@ -52,12 +54,10 @@ pub fn op_webgpu_compute_pass_dispatch_workgroups(
.resource_table
.get::<WebGpuComputePass>(compute_pass_rid)?;

wgpu_core::command::compute_commands::wgpu_compute_pass_dispatch_workgroups(
&mut compute_pass_resource.0.borrow_mut(),
x,
y,
z,
);
compute_pass_resource
.0
.borrow_mut()
.dispatch_workgroups(state.borrow(), x, y, z);

Ok(WebGpuResult::empty())
}
Expand All @@ -77,11 +77,10 @@ pub fn op_webgpu_compute_pass_dispatch_workgroups_indirect(
.resource_table
.get::<WebGpuComputePass>(compute_pass_rid)?;

wgpu_core::command::compute_commands::wgpu_compute_pass_dispatch_workgroups_indirect(
&mut compute_pass_resource.0.borrow_mut(),
buffer_resource.1,
indirect_offset,
);
compute_pass_resource
.0
.borrow_mut()
.dispatch_workgroups_indirect(state.borrow(), buffer_resource.1, indirect_offset)?;

Ok(WebGpuResult::empty())
}
Expand All @@ -90,24 +89,15 @@ pub fn op_webgpu_compute_pass_dispatch_workgroups_indirect(
#[serde]
pub fn op_webgpu_compute_pass_end(
state: &mut OpState,
#[smi] command_encoder_rid: ResourceId,
#[smi] compute_pass_rid: ResourceId,
) -> Result<WebGpuResult, AnyError> {
let command_encoder_resource =
state
.resource_table
.get::<super::command_encoder::WebGpuCommandEncoder>(command_encoder_rid)?;
let command_encoder = command_encoder_resource.1;
let compute_pass_resource = state
.resource_table
.take::<WebGpuComputePass>(compute_pass_rid)?;
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
))
compute_pass_resource.0.borrow_mut().run(state.borrow())?;

Ok(WebGpuResult::empty())
}

#[op2]
Expand Down Expand Up @@ -137,12 +127,12 @@ pub fn op_webgpu_compute_pass_set_bind_group(

let dynamic_offsets_data: &[u32] = &dynamic_offsets_data[start..start + len];

wgpu_core::command::compute_commands::wgpu_compute_pass_set_bind_group(
&mut compute_pass_resource.0.borrow_mut(),
compute_pass_resource.0.borrow_mut().set_bind_group(
state.borrow(),
index,
bind_group_resource.1,
dynamic_offsets_data,
);
)?;

Ok(WebGpuResult::empty())
}
Expand All @@ -158,8 +148,8 @@ pub fn op_webgpu_compute_pass_push_debug_group(
.resource_table
.get::<WebGpuComputePass>(compute_pass_rid)?;

wgpu_core::command::compute_commands::wgpu_compute_pass_push_debug_group(
&mut compute_pass_resource.0.borrow_mut(),
compute_pass_resource.0.borrow_mut().push_debug_group(
state.borrow(),
group_label,
0, // wgpu#975
);
Expand All @@ -177,9 +167,10 @@ pub fn op_webgpu_compute_pass_pop_debug_group(
.resource_table
.get::<WebGpuComputePass>(compute_pass_rid)?;

wgpu_core::command::compute_commands::wgpu_compute_pass_pop_debug_group(
&mut compute_pass_resource.0.borrow_mut(),
);
compute_pass_resource
.0
.borrow_mut()
.pop_debug_group(state.borrow());

Ok(WebGpuResult::empty())
}
Expand All @@ -195,8 +186,8 @@ pub fn op_webgpu_compute_pass_insert_debug_marker(
.resource_table
.get::<WebGpuComputePass>(compute_pass_rid)?;

wgpu_core::command::compute_commands::wgpu_compute_pass_insert_debug_marker(
&mut compute_pass_resource.0.borrow_mut(),
compute_pass_resource.0.borrow_mut().insert_debug_marker(
state.borrow(),
marker_label,
0, // wgpu#975
);
Expand Down
4 changes: 4 additions & 0 deletions tests/tests/compute_pass_resource_ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ async fn compute_pass_resource_ownership(ctx: TestingContext) {
cpass.set_bind_group(0, &bind_group, &[]);
cpass.dispatch_workgroups_indirect(&indirect_buffer, 0);

// TODO:
// write_timestamp
// begin_pipeline_statistics_query

// Now drop all resources we set. Then do a device poll to make sure the resources are really not dropped too early, no matter what.
drop(pipeline);
drop(bind_group);
Expand Down
8 changes: 8 additions & 0 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ impl Global {
ComputePass::new(parent_id, desc)
}

pub fn command_encoder_create_compute_pass_dyn<A: HalApi>(
&self,
parent_id: id::CommandEncoderId,
desc: &ComputePassDescriptor,
) -> Box<dyn super::DynComputePass> {
Box::new(ComputePass::<A>::new(parent_id, desc))
}

pub fn command_encoder_run_compute_pass<A: HalApi>(
&self,
pass: &ComputePass<A>,
Expand Down
136 changes: 136 additions & 0 deletions wgpu-core/src/command/dyn_compute_pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use wgt::WasmNotSendSync;

use crate::{global, hal_api::HalApi, id};

use super::{ComputePass, ComputePassError};

/// Trait for type erasing ComputePass.
// TODO(#5124): wgpu-core's ComputePass trait should not be hal type dependent.
// Practically speaking this allows us merge gfx_select with type erasure:
// The alternative would be to introduce ComputePassId which then first needs to be looked up and then dispatch via gfx_select.
pub trait DynComputePass: std::fmt::Debug + WasmNotSendSync {
fn run(&mut self, context: &global::Global) -> Result<(), ComputePassError>;
fn set_bind_group(
&mut self,
context: &global::Global,
index: u32,
bind_group_id: id::BindGroupId,
offsets: &[wgt::DynamicOffset],
) -> Result<(), ComputePassError>;
fn set_pipeline(
&mut self,
context: &global::Global,
pipeline_id: id::ComputePipelineId,
) -> Result<(), ComputePassError>;
fn set_push_constant(&mut self, context: &global::Global, offset: u32, data: &[u8]);
fn dispatch_workgroups(
&mut self,
context: &global::Global,
groups_x: u32,
groups_y: u32,
groups_z: u32,
);
fn dispatch_workgroups_indirect(
&mut self,
context: &global::Global,
buffer_id: id::BufferId,
offset: wgt::BufferAddress,
) -> Result<(), ComputePassError>;
fn push_debug_group(&mut self, context: &global::Global, label: &str, color: u32);
fn pop_debug_group(&mut self, context: &global::Global);
fn insert_debug_marker(&mut self, context: &global::Global, label: &str, color: u32);
fn write_timestamp(
&mut self,
context: &global::Global,
query_set_id: id::QuerySetId,
query_index: u32,
) -> Result<(), ComputePassError>;
fn begin_pipeline_statistics_query(
&mut self,
context: &global::Global,
query_set_id: id::QuerySetId,
query_index: u32,
) -> Result<(), ComputePassError>;
fn end_pipeline_statistics_query(&mut self, context: &global::Global);
}

impl<A: HalApi> DynComputePass for ComputePass<A> {
fn run(&mut self, context: &global::Global) -> Result<(), ComputePassError> {
context.command_encoder_run_compute_pass(self)
}

fn set_bind_group(
&mut self,
context: &global::Global,
index: u32,
bind_group_id: id::BindGroupId,
offsets: &[wgt::DynamicOffset],
) -> Result<(), ComputePassError> {
context.compute_pass_set_bind_group(self, index, bind_group_id, offsets)
}

fn set_pipeline(
&mut self,
context: &global::Global,
pipeline_id: id::ComputePipelineId,
) -> Result<(), ComputePassError> {
context.compute_pass_set_pipeline(self, pipeline_id)
}

fn set_push_constant(&mut self, context: &global::Global, offset: u32, data: &[u8]) {
context.compute_pass_set_push_constant(self, offset, data)
}

fn dispatch_workgroups(
&mut self,
context: &global::Global,
groups_x: u32,
groups_y: u32,
groups_z: u32,
) {
context.compute_pass_dispatch_workgroups(self, groups_x, groups_y, groups_z)
}

fn dispatch_workgroups_indirect(
&mut self,
context: &global::Global,
buffer_id: id::BufferId,
offset: wgt::BufferAddress,
) -> Result<(), ComputePassError> {
context.compute_pass_dispatch_workgroups_indirect(self, buffer_id, offset)
}

fn push_debug_group(&mut self, context: &global::Global, label: &str, color: u32) {
context.compute_pass_push_debug_group(self, label, color)
}

fn pop_debug_group(&mut self, context: &global::Global) {
context.compute_pass_pop_debug_group(self)
}

fn insert_debug_marker(&mut self, context: &global::Global, label: &str, color: u32) {
context.compute_pass_insert_debug_marker(self, label, color)
}

fn write_timestamp(
&mut self,
context: &global::Global,
query_set_id: id::QuerySetId,
query_index: u32,
) -> Result<(), ComputePassError> {
context.compute_pass_write_timestamp(self, query_set_id, query_index)
}

fn begin_pipeline_statistics_query(
&mut self,
context: &global::Global,
query_set_id: id::QuerySetId,
query_index: u32,
) -> Result<(), ComputePassError> {
context.compute_pass_begin_pipeline_statistics_query(self, query_set_id, query_index)
}

fn end_pipeline_statistics_query(&mut self, context: &global::Global) {
context.compute_pass_end_pipeline_statistics_query(self)
}
}
5 changes: 3 additions & 2 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod clear;
mod compute;
mod compute_command;
mod draw;
mod dyn_compute_pass;
mod memory_init;
mod query;
mod render;
Expand All @@ -14,8 +15,8 @@ use std::sync::Arc;

pub(crate) use self::clear::clear_texture;
pub use self::{
bundle::*, clear::ClearError, compute::*, compute_command::ComputeCommand, draw::*, query::*,
render::*, transfer::*,
bundle::*, clear::ClearError, compute::*, compute_command::ComputeCommand, draw::*,
dyn_compute_pass::DynComputePass, query::*, render::*, transfer::*,
};
pub(crate) use allocator::CommandAllocator;

Expand Down
11 changes: 0 additions & 11 deletions wgpu-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,6 @@ macro_rules! gfx_select {
other => panic!("Unexpected backend {:?}", other),
}
};

($id:expr => $method:ident $params:tt) => {
match $id.backend() {
wgt::Backend::Vulkan => $crate::gfx_if_vulkan!($method::<$crate::api::Vulkan> $params),
wgt::Backend::Metal => $crate::gfx_if_metal!($method::<$crate::api::Metal> $params),
wgt::Backend::Dx12 => $crate::gfx_if_dx12!($method::<$crate::api::Dx12> $params),
wgt::Backend::Gl => $crate::gfx_if_gles!($method::<$crate::api::Gles> $params),
wgt::Backend::Empty => $crate::gfx_if_empty!($method::<$crate::api::Empty> $params),
other => panic!("Unexpected backend {:?}", other),
}
};
}

#[cfg(feature = "api_log_info")]
Expand Down

0 comments on commit b976ff1

Please sign in to comment.