Skip to content

Commit

Permalink
Improve ergonomics of gfx_select! (#5069)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog authored Jan 16, 2024
1 parent 445fa60 commit 90b078f
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 239 deletions.
6 changes: 2 additions & 4 deletions deno_webgpu/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ impl Resource for WebGpuBindGroupLayout {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.bind_group_layout_drop(self.1));
gfx_select!(self.1 => self.0.bind_group_layout_drop(self.1));
}
}

Expand All @@ -36,8 +35,7 @@ impl Resource for WebGpuBindGroup {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.bind_group_drop(self.1));
gfx_select!(self.1 => self.0.bind_group_drop(self.1));
}
}

Expand Down
3 changes: 1 addition & 2 deletions deno_webgpu/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ impl Resource for WebGpuBuffer {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.buffer_drop(self.1, true));
gfx_select!(self.1 => self.0.buffer_drop(self.1, true));
}
}

Expand Down
3 changes: 1 addition & 2 deletions deno_webgpu/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl Resource for WebGpuRenderBundle {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.render_bundle_drop(self.1));
gfx_select!(self.1 => self.0.render_bundle_drop(self.1));
}
}

Expand Down
6 changes: 2 additions & 4 deletions deno_webgpu/command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ impl Resource for WebGpuCommandEncoder {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.command_encoder_drop(self.1));
gfx_select!(self.1 => self.0.command_encoder_drop(self.1));
}
}

Expand All @@ -39,8 +38,7 @@ impl Resource for WebGpuCommandBuffer {

fn close(self: Rc<Self>) {
if let Some(id) = *self.1.borrow() {
let instance = &self.0;
gfx_select!(id => instance.command_buffer_drop(id));
gfx_select!(id => self.0.command_buffer_drop(id));
}
}
}
Expand Down
27 changes: 16 additions & 11 deletions deno_webgpu/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,31 @@ use error::WebGpuResult;
#[macro_use]
mod macros {
macro_rules! gfx_select {
($id:expr => $global:ident.$method:ident( $($param:expr),* )) => {
($id:expr => $p0:ident.$p1:tt.$method:ident $params:tt) => {
gfx_select!($id => {$p0.$p1}, $method $params)
};

($id:expr => $p0:ident.$method:ident $params:tt) => {
gfx_select!($id => {$p0}, $method $params)
};

($id:expr => {$($c:tt)*}, $method:ident $params:tt) => {
match $id.backend() {
#[cfg(any(
all(not(target_arch = "wasm32"), not(target_os = "ios"), not(target_os = "macos")),
feature = "vulkan-portability"
))]
wgpu_types::Backend::Vulkan => $global.$method::<wgpu_core::api::Vulkan>( $($param),* ),
wgpu_types::Backend::Vulkan => $($c)*.$method::<wgpu_core::api::Vulkan> $params,
#[cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))]
wgpu_types::Backend::Metal => $global.$method::<wgpu_core::api::Metal>( $($param),* ),
wgpu_types::Backend::Metal => $($c)*.$method::<wgpu_core::api::Metal> $params,
#[cfg(all(not(target_arch = "wasm32"), windows))]
wgpu_types::Backend::Dx12 => $global.$method::<wgpu_core::api::Dx12>( $($param),* ),
wgpu_types::Backend::Dx12 => $($c)*.$method::<wgpu_core::api::Dx12> $params,
#[cfg(any(
all(unix, not(target_os = "macos"), not(target_os = "ios")),
feature = "angle",
target_arch = "wasm32"
))]
wgpu_types::Backend::Gl => $global.$method::<wgpu_core::api::Gles>( $($param),+ ),
wgpu_types::Backend::Gl => $($c)*.$method::<wgpu_core::api::Gles> $params,
other => panic!("Unexpected backend {:?}", other),
}
};
Expand Down Expand Up @@ -98,8 +106,7 @@ impl Resource for WebGpuAdapter {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.adapter_drop(self.1));
gfx_select!(self.1 => self.0.adapter_drop(self.1));
}
}

Expand All @@ -110,8 +117,7 @@ impl Resource for WebGpuDevice {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.device_drop(self.1));
gfx_select!(self.1 => self.0.device_drop(self.1));
}
}

Expand All @@ -122,8 +128,7 @@ impl Resource for WebGpuQuerySet {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.query_set_drop(self.1));
gfx_select!(self.1 => self.0.query_set_drop(self.1));
}
}

Expand Down
9 changes: 3 additions & 6 deletions deno_webgpu/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ impl Resource for WebGpuPipelineLayout {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.pipeline_layout_drop(self.1));
gfx_select!(self.1 => self.0.pipeline_layout_drop(self.1));
}
}

Expand All @@ -40,8 +39,7 @@ impl Resource for WebGpuComputePipeline {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.compute_pipeline_drop(self.1));
gfx_select!(self.1 => self.0.compute_pipeline_drop(self.1));
}
}

Expand All @@ -55,8 +53,7 @@ impl Resource for WebGpuRenderPipeline {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.render_pipeline_drop(self.1));
gfx_select!(self.1 => self.0.render_pipeline_drop(self.1));
}
}

Expand Down
3 changes: 1 addition & 2 deletions deno_webgpu/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ impl Resource for WebGpuSampler {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.sampler_drop(self.1));
gfx_select!(self.1 => self.0.sampler_drop(self.1));
}
}

Expand Down
3 changes: 1 addition & 2 deletions deno_webgpu/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ impl Resource for WebGpuShaderModule {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.shader_module_drop(self.1));
gfx_select!(self.1 => self.0.shader_module_drop(self.1));
}
}

Expand Down
3 changes: 1 addition & 2 deletions deno_webgpu/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl Resource for WebGpuTextureView {
}

fn close(self: Rc<Self>) {
let instance = &self.0;
gfx_select!(self.1 => instance.texture_view_drop(self.1, true)).unwrap();
gfx_select!(self.1 => self.0.texture_view_drop(self.1, true)).unwrap();
}
}

Expand Down
30 changes: 10 additions & 20 deletions wgpu-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,27 @@ impl<'a> ErrorFormatter<'a> {
}

pub fn bind_group_label(&mut self, id: &crate::id::BindGroupId) {
let global = self.global;
let label: String = gfx_select!(id => global.bind_group_label(*id));
let label: String = gfx_select!(id => self.global.bind_group_label(*id));
self.label("bind group", &label);
}

pub fn bind_group_layout_label(&mut self, id: &crate::id::BindGroupLayoutId) {
let global = self.global;
let label: String = gfx_select!(id => global.bind_group_layout_label(*id));
let label: String = gfx_select!(id => self.global.bind_group_layout_label(*id));
self.label("bind group layout", &label);
}

pub fn render_pipeline_label(&mut self, id: &crate::id::RenderPipelineId) {
let global = self.global;
let label: String = gfx_select!(id => global.render_pipeline_label(*id));
let label: String = gfx_select!(id => self.global.render_pipeline_label(*id));
self.label("render pipeline", &label);
}

pub fn compute_pipeline_label(&mut self, id: &crate::id::ComputePipelineId) {
let global = self.global;
let label: String = gfx_select!(id => global.compute_pipeline_label(*id));
let label: String = gfx_select!(id => self.global.compute_pipeline_label(*id));
self.label("compute pipeline", &label);
}

pub fn buffer_label_with_key(&mut self, id: &crate::id::BufferId, key: &str) {
let global = self.global;
let label: String = gfx_select!(id => global.buffer_label(*id));
let label: String = gfx_select!(id => self.global.buffer_label(*id));
self.label(key, &label);
}

Expand All @@ -58,8 +53,7 @@ impl<'a> ErrorFormatter<'a> {
}

pub fn texture_label_with_key(&mut self, id: &crate::id::TextureId, key: &str) {
let global = self.global;
let label: String = gfx_select!(id => global.texture_label(*id));
let label: String = gfx_select!(id => self.global.texture_label(*id));
self.label(key, &label);
}

Expand All @@ -68,8 +62,7 @@ impl<'a> ErrorFormatter<'a> {
}

pub fn texture_view_label_with_key(&mut self, id: &crate::id::TextureViewId, key: &str) {
let global = self.global;
let label: String = gfx_select!(id => global.texture_view_label(*id));
let label: String = gfx_select!(id => self.global.texture_view_label(*id));
self.label(key, &label);
}

Expand All @@ -78,20 +71,17 @@ impl<'a> ErrorFormatter<'a> {
}

pub fn sampler_label(&mut self, id: &crate::id::SamplerId) {
let global = self.global;
let label: String = gfx_select!(id => global.sampler_label(*id));
let label: String = gfx_select!(id => self.global.sampler_label(*id));
self.label("sampler", &label);
}

pub fn command_buffer_label(&mut self, id: &crate::id::CommandBufferId) {
let global = self.global;
let label: String = gfx_select!(id => global.command_buffer_label(*id));
let label: String = gfx_select!(id => self.global.command_buffer_label(*id));
self.label("command buffer", &label);
}

pub fn query_set_label(&mut self, id: &crate::id::QuerySetId) {
let global = self.global;
let label: String = gfx_select!(id => global.query_set_label(*id));
let label: String = gfx_select!(id => self.global.query_set_label(*id));
self.label("query set", &label);
}
}
Expand Down
22 changes: 16 additions & 6 deletions wgpu-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,23 @@ define_backend_caller! { gfx_if_empty, gfx_if_empty_hidden, "empty" if all(
/// [`Id`]: id::Id
#[macro_export]
macro_rules! gfx_select {
($id:expr => $global:ident.$method:ident( $($param:expr),* )) => {
// Simple two-component expression, like `self.0.method(..)`.
($id:expr => $c0:ident.$c1:tt.$method:ident $params:tt) => {
$crate::gfx_select!($id => {$c0.$c1}, $method $params)
};

// Simple identifier-only expression, like `global.method(..)`.
($id:expr => $c0:ident.$method:ident $params:tt) => {
$crate::gfx_select!($id => {$c0}, $method $params)
};

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

0 comments on commit 90b078f

Please sign in to comment.