From 2fb2dbf65b7ff8eea72aaba19574bed55ba5bb07 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Tue, 15 Apr 2025 10:30:53 +0200 Subject: [PATCH 1/5] Add downcast to `Dyn*` Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu/src/backend/custom.rs | 9 +++++++++ wgpu/src/dispatch.rs | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/wgpu/src/backend/custom.rs b/wgpu/src/backend/custom.rs index 471ccb88ec2..f82e1150b28 100644 --- a/wgpu/src/backend/custom.rs +++ b/wgpu/src/backend/custom.rs @@ -18,6 +18,11 @@ macro_rules! dyn_type { pub(crate) fn new(t: T) -> Self { Self(Arc::new(t)) } + + #[allow(clippy::allow_attributes, dead_code)] + pub(crate) fn downcast(&self) -> Option<&T> { + self.0.as_ref().as_any().downcast_ref() + } } impl core::ops::Deref for $name { @@ -46,6 +51,10 @@ macro_rules! dyn_type { pub(crate) fn new(t: T) -> Self { Self(Arc::new(t)) } + + pub(crate) fn downcast(&self) -> Option<&T> { + self.0.as_ref().as_any().downcast_ref() + } } impl core::ops::Deref for $name { diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index 37cf420f48f..f802848c28a 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -54,8 +54,20 @@ pub type BufferMapCallback = Box) #[cfg(not(send_sync))] pub type BufferMapCallback = Box) + 'static>; +// remove when rust 1.86 +#[cfg_attr(not(custom), expect(dead_code))] +pub trait AsAny { + fn as_any(&self) -> &dyn Any; +} + +impl AsAny for T { + fn as_any(&self) -> &dyn Any { + self + } +} + // Common traits on all the interface traits -trait_alias!(CommonTraits: Any + Debug + WasmNotSendSync); +trait_alias!(CommonTraits: AsAny + Any + Debug + WasmNotSendSync); pub trait InstanceInterface: CommonTraits { fn new(desc: &crate::InstanceDescriptor) -> Self From 7fde0618529377cfa96b84537b10750606ef6cb3 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Tue, 15 Apr 2025 10:31:24 +0200 Subject: [PATCH 2/5] Add `as_custom` Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu/src/api/adapter.rs | 6 +++++ wgpu/src/api/bind_group.rs | 8 +++++++ wgpu/src/api/bind_group_layout.rs | 8 +++++++ wgpu/src/api/blas.rs | 6 +++++ wgpu/src/api/buffer.rs | 6 +++++ wgpu/src/api/command_buffer.rs | 8 +++++++ wgpu/src/api/command_encoder.rs | 8 +++++++ wgpu/src/api/compute_pass.rs | 6 +++++ wgpu/src/api/compute_pipeline.rs | 8 +++++++ wgpu/src/api/device.rs | 6 +++++ wgpu/src/api/instance.rs | 6 +++++ wgpu/src/api/pipeline_cache.rs | 6 +++++ wgpu/src/api/pipeline_layout.rs | 8 +++++++ wgpu/src/api/query_set.rs | 8 +++++++ wgpu/src/api/queue.rs | 32 ++++++++++++++++++++------- wgpu/src/api/render_bundle.rs | 8 +++++++ wgpu/src/api/render_bundle_encoder.rs | 8 +++++++ wgpu/src/api/render_pass.rs | 8 +++++++ wgpu/src/api/render_pipeline.rs | 8 +++++++ wgpu/src/api/sampler.rs | 8 +++++++ wgpu/src/api/shader_module.rs | 8 +++++++ wgpu/src/api/surface.rs | 6 +++++ wgpu/src/api/surface_texture.rs | 6 +++++ wgpu/src/api/texture.rs | 8 +++++++ wgpu/src/api/texture_view.rs | 8 +++++++ wgpu/src/api/tlas.rs | 6 +++++ wgpu/src/dispatch.rs | 20 +++++++++++++++++ 27 files changed, 224 insertions(+), 8 deletions(-) diff --git a/wgpu/src/api/adapter.rs b/wgpu/src/api/adapter.rs index 614f43bae6c..d1c3a7c296e 100644 --- a/wgpu/src/api/adapter.rs +++ b/wgpu/src/api/adapter.rs @@ -133,6 +133,12 @@ impl Adapter { } } + #[cfg(custom)] + /// Returns custom implementation of adapter (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } + #[cfg(custom)] /// Creates Adapter from custom implementation pub fn from_custom(adapter: T) -> Self { diff --git a/wgpu/src/api/bind_group.rs b/wgpu/src/api/bind_group.rs index d9c27bdc4c5..86c70dea932 100644 --- a/wgpu/src/api/bind_group.rs +++ b/wgpu/src/api/bind_group.rs @@ -17,6 +17,14 @@ static_assertions::assert_impl_all!(BindGroup: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(BindGroup => .inner); +impl BindGroup { + #[cfg(custom)] + /// Returns custom implementation of BindGroup (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + /// Resource to be bound by a [`BindGroup`] for use with a pipeline. /// /// The pipeline’s [`BindGroupLayout`] must contain a matching [`BindingType`]. diff --git a/wgpu/src/api/bind_group_layout.rs b/wgpu/src/api/bind_group_layout.rs index a55921ccc8b..554538ac539 100644 --- a/wgpu/src/api/bind_group_layout.rs +++ b/wgpu/src/api/bind_group_layout.rs @@ -18,6 +18,14 @@ pub struct BindGroupLayout { #[cfg(send_sync)] static_assertions::assert_impl_all!(BindGroupLayout: Send, Sync); +impl BindGroupLayout { + #[cfg(custom)] + /// Returns custom implementation of BindGroupLayout (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + crate::cmp::impl_eq_ord_hash_proxy!(BindGroupLayout => .inner); /// Describes a [`BindGroupLayout`]. diff --git a/wgpu/src/api/blas.rs b/wgpu/src/api/blas.rs index 3ab53098f75..777da660e04 100644 --- a/wgpu/src/api/blas.rs +++ b/wgpu/src/api/blas.rs @@ -174,6 +174,12 @@ impl Blas { hal_blas_callback(None) } } + + #[cfg(custom)] + /// Returns custom implementation of Blas (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } } /// Context version of [BlasTriangleGeometry]. diff --git a/wgpu/src/api/buffer.rs b/wgpu/src/api/buffer.rs index 089c743a08f..634e1913134 100644 --- a/wgpu/src/api/buffer.rs +++ b/wgpu/src/api/buffer.rs @@ -386,6 +386,12 @@ impl Buffer { ) -> BufferViewMut<'_> { self.slice(bounds).get_mapped_range_mut() } + + #[cfg(custom)] + /// Returns custom implementation of Buffer (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } } /// A slice of a [`Buffer`], to be mapped, used for vertex or index data, or the like. diff --git a/wgpu/src/api/command_buffer.rs b/wgpu/src/api/command_buffer.rs index 00c84af30df..56d6e32129c 100644 --- a/wgpu/src/api/command_buffer.rs +++ b/wgpu/src/api/command_buffer.rs @@ -13,3 +13,11 @@ pub struct CommandBuffer { } #[cfg(send_sync)] static_assertions::assert_impl_all!(CommandBuffer: Send, Sync); + +impl CommandBuffer { + #[cfg(custom)] + /// Returns custom implementation of CommandBuffer (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.buffer.as_custom_opt().and_then(|c| c.downcast()) + } +} diff --git a/wgpu/src/api/command_encoder.rs b/wgpu/src/api/command_encoder.rs index 1cdb9267d41..3014ed3fba9 100644 --- a/wgpu/src/api/command_encoder.rs +++ b/wgpu/src/api/command_encoder.rs @@ -26,6 +26,14 @@ static_assertions::assert_impl_all!(CommandEncoder: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(CommandEncoder => .inner); +impl CommandEncoder { + #[cfg(custom)] + /// Returns custom implementation of CommandEncoder (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + /// Describes a [`CommandEncoder`]. /// /// For use with [`Device::create_command_encoder`]. diff --git a/wgpu/src/api/compute_pass.rs b/wgpu/src/api/compute_pass.rs index c7fa7462f18..76c2b87f765 100644 --- a/wgpu/src/api/compute_pass.rs +++ b/wgpu/src/api/compute_pass.rs @@ -94,6 +94,12 @@ impl ComputePass<'_> { self.inner .dispatch_workgroups_indirect(&indirect_buffer.inner, indirect_offset); } + + #[cfg(custom)] + /// Returns custom implementation of ComputePass (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } } /// [`Features::PUSH_CONSTANTS`] must be enabled on the device in order to call these functions. diff --git a/wgpu/src/api/compute_pipeline.rs b/wgpu/src/api/compute_pipeline.rs index 325f0ba1ff2..1886da2ee79 100644 --- a/wgpu/src/api/compute_pipeline.rs +++ b/wgpu/src/api/compute_pipeline.rs @@ -15,6 +15,14 @@ static_assertions::assert_impl_all!(ComputePipeline: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(ComputePipeline => .inner); +impl ComputePipeline { + #[cfg(custom)] + /// Returns custom implementation of ComputePipeline (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + impl ComputePipeline { /// Get an object representing the bind group layout at a given index. /// diff --git a/wgpu/src/api/device.rs b/wgpu/src/api/device.rs index 44fb837a2c2..34dc28a400f 100644 --- a/wgpu/src/api/device.rs +++ b/wgpu/src/api/device.rs @@ -34,6 +34,12 @@ pub type DeviceDescriptor<'a> = wgt::DeviceDescriptor>; static_assertions::assert_impl_all!(DeviceDescriptor<'_>: Send, Sync); impl Device { + #[cfg(custom)] + /// Returns custom implementation of Device (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } + #[cfg(custom)] /// Creates Device from custom implementation pub fn from_custom(device: T) -> Self { diff --git a/wgpu/src/api/instance.rs b/wgpu/src/api/instance.rs index dc34a781782..8ed5649aae5 100644 --- a/wgpu/src/api/instance.rs +++ b/wgpu/src/api/instance.rs @@ -208,6 +208,12 @@ impl Instance { } } + #[cfg(custom)] + /// Returns custom implementation of Instance (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } + /// Retrieves all available [`Adapter`]s that match the given [`Backends`]. /// /// # Arguments diff --git a/wgpu/src/api/pipeline_cache.rs b/wgpu/src/api/pipeline_cache.rs index f1cd414f512..41194830b89 100644 --- a/wgpu/src/api/pipeline_cache.rs +++ b/wgpu/src/api/pipeline_cache.rs @@ -87,4 +87,10 @@ impl PipelineCache { pub fn get_data(&self) -> Option> { self.inner.get_data() } + + #[cfg(custom)] + /// Returns custom implementation of PipelineCache (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } } diff --git a/wgpu/src/api/pipeline_layout.rs b/wgpu/src/api/pipeline_layout.rs index 7baa91555c6..54aaf1bd4aa 100644 --- a/wgpu/src/api/pipeline_layout.rs +++ b/wgpu/src/api/pipeline_layout.rs @@ -15,6 +15,14 @@ static_assertions::assert_impl_all!(PipelineLayout: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(PipelineLayout => .inner); +impl PipelineLayout { + #[cfg(custom)] + /// Returns custom implementation of PipelineLayout (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + /// Describes a [`PipelineLayout`]. /// /// For use with [`Device::create_pipeline_layout`]. diff --git a/wgpu/src/api/query_set.rs b/wgpu/src/api/query_set.rs index 24b5a028097..c451bd27246 100644 --- a/wgpu/src/api/query_set.rs +++ b/wgpu/src/api/query_set.rs @@ -15,6 +15,14 @@ static_assertions::assert_impl_all!(QuerySet: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(QuerySet => .inner); +impl QuerySet { + #[cfg(custom)] + /// Returns custom implementation of QuerySet (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + /// Describes a [`QuerySet`]. /// /// For use with [`Device::create_query_set`]. diff --git a/wgpu/src/api/queue.rs b/wgpu/src/api/queue.rs index b1868738e98..a6a8c4b78c1 100644 --- a/wgpu/src/api/queue.rs +++ b/wgpu/src/api/queue.rs @@ -19,6 +19,22 @@ static_assertions::assert_impl_all!(Queue: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(Queue => .inner); +impl Queue { + #[cfg(custom)] + /// Returns custom implementation of Queue (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } + + #[cfg(custom)] + /// Creates Queue from custom implementation + pub fn from_custom(queue: T) -> Self { + Self { + inner: dispatch::DispatchQueue::custom(queue), + } + } +} + /// Identifier for a particular call to [`Queue::submit`]. Can be used /// as part of an argument to [`Device::poll`] to block for a particular /// submission to finish. @@ -52,6 +68,14 @@ pub struct QueueWriteBufferView<'a> { #[cfg(send_sync)] static_assertions::assert_impl_all!(QueueWriteBufferView<'_>: Send, Sync); +impl QueueWriteBufferView<'_> { + #[cfg(custom)] + /// Returns custom implementation of QueueWriteBufferView (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + impl Deref for QueueWriteBufferView<'_> { type Target = [u8]; @@ -82,14 +106,6 @@ impl Drop for QueueWriteBufferView<'_> { } impl Queue { - #[cfg(custom)] - /// Creates Queue from custom implementation - pub fn from_custom(queue: T) -> Self { - Self { - inner: dispatch::DispatchQueue::custom(queue), - } - } - /// Copies the bytes of `data` into `buffer` starting at `offset`. /// /// The data must be written fully in-bounds, that is, `offset + data.len() <= buffer.len()`. diff --git a/wgpu/src/api/render_bundle.rs b/wgpu/src/api/render_bundle.rs index 95de9762078..9635ae0ce43 100644 --- a/wgpu/src/api/render_bundle.rs +++ b/wgpu/src/api/render_bundle.rs @@ -18,6 +18,14 @@ static_assertions::assert_impl_all!(RenderBundle: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(RenderBundle => .inner); +impl RenderBundle { + #[cfg(custom)] + /// Returns custom implementation of RenderBundle (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + /// Describes a [`RenderBundle`]. /// /// For use with [`RenderBundleEncoder::finish`]. diff --git a/wgpu/src/api/render_bundle_encoder.rs b/wgpu/src/api/render_bundle_encoder.rs index bd64dbbc7cd..8b2560f5351 100644 --- a/wgpu/src/api/render_bundle_encoder.rs +++ b/wgpu/src/api/render_bundle_encoder.rs @@ -25,6 +25,14 @@ static_assertions::assert_not_impl_any!(RenderBundleEncoder<'_>: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(RenderBundleEncoder<'_> => .inner); +impl RenderBundleEncoder<'_> { + #[cfg(custom)] + /// Returns custom implementation of RenderBundleEncoder (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + /// Describes a [`RenderBundleEncoder`]. /// /// For use with [`Device::create_render_bundle_encoder`]. diff --git a/wgpu/src/api/render_pass.rs b/wgpu/src/api/render_pass.rs index 6c30543f624..61e212086b6 100644 --- a/wgpu/src/api/render_pass.rs +++ b/wgpu/src/api/render_pass.rs @@ -36,6 +36,14 @@ static_assertions::assert_impl_all!(RenderPass<'_>: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(RenderPass<'_> => .inner); +impl RenderPass<'_> { + #[cfg(custom)] + /// Returns custom implementation of RenderPass (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + impl RenderPass<'_> { /// Drops the lifetime relationship to the parent command encoder, making usage of /// the encoder while this pass is recorded a run-time error instead. diff --git a/wgpu/src/api/render_pipeline.rs b/wgpu/src/api/render_pipeline.rs index 5ef23ff0de7..12375cda73d 100644 --- a/wgpu/src/api/render_pipeline.rs +++ b/wgpu/src/api/render_pipeline.rs @@ -17,6 +17,14 @@ static_assertions::assert_impl_all!(RenderPipeline: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(RenderPipeline => .inner); +impl RenderPipeline { + #[cfg(custom)] + /// Returns custom implementation of RenderPipeline (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + impl RenderPipeline { /// Get an object representing the bind group layout at a given index. /// diff --git a/wgpu/src/api/sampler.rs b/wgpu/src/api/sampler.rs index 49c988e85f0..fde912b3c72 100644 --- a/wgpu/src/api/sampler.rs +++ b/wgpu/src/api/sampler.rs @@ -18,6 +18,14 @@ static_assertions::assert_impl_all!(Sampler: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(Sampler => .inner); +impl Sampler { + #[cfg(custom)] + /// Returns custom implementation of Sampler (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + /// Describes a [`Sampler`]. /// /// For use with [`Device::create_sampler`]. diff --git a/wgpu/src/api/shader_module.rs b/wgpu/src/api/shader_module.rs index 940d6363215..5c6dba421fc 100644 --- a/wgpu/src/api/shader_module.rs +++ b/wgpu/src/api/shader_module.rs @@ -24,6 +24,14 @@ static_assertions::assert_impl_all!(ShaderModule: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(ShaderModule => .inner); +impl ShaderModule { + #[cfg(custom)] + /// Returns custom implementation of ShaderModule (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + impl ShaderModule { /// Get the compilation info for the shader module. pub fn get_compilation_info(&self) -> impl Future + WasmNotSend { diff --git a/wgpu/src/api/surface.rs b/wgpu/src/api/surface.rs index bc94a4d21f7..4970b5c19c8 100644 --- a/wgpu/src/api/surface.rs +++ b/wgpu/src/api/surface.rs @@ -171,6 +171,12 @@ impl Surface<'_> { hal_surface_callback(None) } } + + #[cfg(custom)] + /// Returns custom implementation of Surface (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } } // This custom implementation is required because [`Surface::_surface`] doesn't diff --git a/wgpu/src/api/surface_texture.rs b/wgpu/src/api/surface_texture.rs index 7ceeffb9550..23dbf99b889 100644 --- a/wgpu/src/api/surface_texture.rs +++ b/wgpu/src/api/surface_texture.rs @@ -38,6 +38,12 @@ impl SurfaceTexture { self.presented = true; self.detail.present(); } + + #[cfg(custom)] + /// Returns custom implementation of SurfaceTexture (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.detail.as_custom_opt().and_then(|c| c.downcast()) + } } impl Drop for SurfaceTexture { diff --git a/wgpu/src/api/texture.rs b/wgpu/src/api/texture.rs index 8ac80e6505c..d0ac76eab0d 100644 --- a/wgpu/src/api/texture.rs +++ b/wgpu/src/api/texture.rs @@ -15,6 +15,14 @@ static_assertions::assert_impl_all!(Texture: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(Texture => .inner); +impl Texture { + #[cfg(custom)] + /// Returns custom implementation of Texture (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + impl Texture { /// Returns the inner hal Texture using a callback. The hal texture will be `None` if the /// backend type argument does not match with this wgpu Texture diff --git a/wgpu/src/api/texture_view.rs b/wgpu/src/api/texture_view.rs index 1fff6f86b7a..f7039502340 100644 --- a/wgpu/src/api/texture_view.rs +++ b/wgpu/src/api/texture_view.rs @@ -18,6 +18,14 @@ static_assertions::assert_impl_all!(TextureView: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(TextureView => .inner); +impl TextureView { + #[cfg(custom)] + /// Returns custom implementation of TextureView (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom_opt().and_then(|c| c.downcast()) + } +} + impl TextureView { /// Returns the inner hal `TextureView` using a callback. The hal texture will be `None` if the /// backend type argument does not match with this wgpu Texture diff --git a/wgpu/src/api/tlas.rs b/wgpu/src/api/tlas.rs index 5ea33620ffe..1149bd6c718 100644 --- a/wgpu/src/api/tlas.rs +++ b/wgpu/src/api/tlas.rs @@ -57,6 +57,12 @@ impl Tlas { hal_tlas_callback(None) } } + + #[cfg(custom)] + /// Returns custom implementation of Tlas (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.shared.inner.as_custom_opt().and_then(|c| c.downcast()) + } } /// Entry for a top level acceleration structure build. diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index f802848c28a..ca3bb4fba8c 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -587,6 +587,16 @@ macro_rules! dispatch_types { } } + #[cfg(custom)] + #[inline] + #[allow(clippy::allow_attributes, unused)] + pub(crate) fn as_custom_opt(&self) -> Option<&$custom_type> { + match self { + Self::Custom(value) => Some(value), + _ => None, + } + } + #[cfg(webgpu)] #[inline] #[allow(clippy::allow_attributes, unused)] @@ -705,6 +715,16 @@ macro_rules! dispatch_types { } } + #[cfg(custom)] + #[inline] + #[allow(clippy::allow_attributes, unused)] + pub(crate) fn as_custom_opt(&self) -> Option<&$custom_type> { + match self { + Self::Custom(value) => Some(value), + _ => None, + } + } + #[cfg(webgpu)] #[inline] #[allow(clippy::allow_attributes, unused)] From 4c3852222e690c44ef04ee2fa02c60934fcf2811 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Tue, 15 Apr 2025 10:31:35 +0200 Subject: [PATCH 3/5] Add tests as more advance example Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- .../standalone/custom_backend/src/custom.rs | 22 ++++++++++++++----- .../standalone/custom_backend/src/main.rs | 18 +++++++++++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/examples/standalone/custom_backend/src/custom.rs b/examples/standalone/custom_backend/src/custom.rs index c3815fbbab9..8192379ec3f 100644 --- a/examples/standalone/custom_backend/src/custom.rs +++ b/examples/standalone/custom_backend/src/custom.rs @@ -3,9 +3,9 @@ use std::pin::Pin; use std::sync::Arc; use wgpu::custom::{ - AdapterInterface, DeviceInterface, DispatchAdapter, DispatchDevice, DispatchQueue, - DispatchShaderModule, DispatchSurface, InstanceInterface, QueueInterface, RequestAdapterFuture, - ShaderModuleInterface, + AdapterInterface, ComputePipelineInterface, DeviceInterface, DispatchAdapter, DispatchDevice, + DispatchQueue, DispatchShaderModule, DispatchSurface, InstanceInterface, QueueInterface, + RequestAdapterFuture, ShaderModuleInterface, }; #[derive(Debug, Clone)] @@ -163,9 +163,10 @@ impl DeviceInterface for CustomDevice { fn create_compute_pipeline( &self, - _desc: &wgpu::ComputePipelineDescriptor<'_>, + desc: &wgpu::ComputePipelineDescriptor<'_>, ) -> wgpu::custom::DispatchComputePipeline { - unimplemented!() + let module = desc.module.as_custom::().unwrap(); + wgpu::custom::DispatchComputePipeline::custom(CustomComputePipeline(module.0.clone())) } unsafe fn create_pipeline_cache( @@ -262,7 +263,7 @@ impl DeviceInterface for CustomDevice { } #[derive(Debug)] -struct CustomShaderModule(Counter); +pub struct CustomShaderModule(pub Counter); impl ShaderModuleInterface for CustomShaderModule { fn get_compilation_info(&self) -> Pin> { @@ -343,3 +344,12 @@ impl QueueInterface for CustomQueue { unimplemented!() } } + +#[derive(Debug)] +pub struct CustomComputePipeline(pub Counter); + +impl ComputePipelineInterface for CustomComputePipeline { + fn get_bind_group_layout(&self, _index: u32) -> wgpu::custom::DispatchBindGroupLayout { + unimplemented!() + } +} diff --git a/examples/standalone/custom_backend/src/main.rs b/examples/standalone/custom_backend/src/main.rs index 11f05e98ea8..1582bcc7d6b 100644 --- a/examples/standalone/custom_backend/src/main.rs +++ b/examples/standalone/custom_backend/src/main.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use custom::Counter; +use custom::{Counter, CustomShaderModule}; use wgpu::{DeviceDescriptor, RequestAdapterOptions}; mod custom; @@ -31,12 +31,26 @@ async fn main() { .unwrap(); assert_eq!(counter.count(), 5); - let _module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("shader"), source: wgpu::ShaderSource::Dummy(PhantomData), }); + let custom_module = module.as_custom::().unwrap(); + assert_eq!(custom_module.0.count(), 6); + let _module_clone = module.clone(); assert_eq!(counter.count(), 6); + + let _pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: None, + layout: None, + module: &module, + entry_point: None, + compilation_options: Default::default(), + cache: None, + }); + + assert_eq!(counter.count(), 7); } assert_eq!(counter.count(), 1); } From 3e758ee17523cc04a3749ded92711a5b4ba8a557 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Thu, 17 Apr 2025 12:15:12 +0200 Subject: [PATCH 4/5] Expose as_custom on Dispatch types too Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu/src/api/adapter.rs | 2 +- wgpu/src/api/bind_group.rs | 2 +- wgpu/src/api/bind_group_layout.rs | 2 +- wgpu/src/api/blas.rs | 2 +- wgpu/src/api/buffer.rs | 2 +- wgpu/src/api/command_buffer.rs | 2 +- wgpu/src/api/command_encoder.rs | 2 +- wgpu/src/api/compute_pass.rs | 2 +- wgpu/src/api/compute_pipeline.rs | 2 +- wgpu/src/api/device.rs | 2 +- wgpu/src/api/instance.rs | 2 +- wgpu/src/api/pipeline_cache.rs | 2 +- wgpu/src/api/pipeline_layout.rs | 2 +- wgpu/src/api/query_set.rs | 2 +- wgpu/src/api/queue.rs | 4 ++-- wgpu/src/api/render_bundle.rs | 2 +- wgpu/src/api/render_bundle_encoder.rs | 2 +- wgpu/src/api/render_pass.rs | 2 +- wgpu/src/api/render_pipeline.rs | 2 +- wgpu/src/api/sampler.rs | 2 +- wgpu/src/api/shader_module.rs | 2 +- wgpu/src/api/surface.rs | 2 +- wgpu/src/api/surface_texture.rs | 2 +- wgpu/src/api/texture.rs | 2 +- wgpu/src/api/texture_view.rs | 2 +- wgpu/src/api/tlas.rs | 2 +- wgpu/src/dispatch.rs | 8 ++++---- 27 files changed, 31 insertions(+), 31 deletions(-) diff --git a/wgpu/src/api/adapter.rs b/wgpu/src/api/adapter.rs index d1c3a7c296e..3b83ccc0f46 100644 --- a/wgpu/src/api/adapter.rs +++ b/wgpu/src/api/adapter.rs @@ -136,7 +136,7 @@ impl Adapter { #[cfg(custom)] /// Returns custom implementation of adapter (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } #[cfg(custom)] diff --git a/wgpu/src/api/bind_group.rs b/wgpu/src/api/bind_group.rs index 86c70dea932..2f4ae007ff6 100644 --- a/wgpu/src/api/bind_group.rs +++ b/wgpu/src/api/bind_group.rs @@ -21,7 +21,7 @@ impl BindGroup { #[cfg(custom)] /// Returns custom implementation of BindGroup (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/bind_group_layout.rs b/wgpu/src/api/bind_group_layout.rs index 554538ac539..a7a50ca7270 100644 --- a/wgpu/src/api/bind_group_layout.rs +++ b/wgpu/src/api/bind_group_layout.rs @@ -22,7 +22,7 @@ impl BindGroupLayout { #[cfg(custom)] /// Returns custom implementation of BindGroupLayout (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/blas.rs b/wgpu/src/api/blas.rs index 777da660e04..82c976b98df 100644 --- a/wgpu/src/api/blas.rs +++ b/wgpu/src/api/blas.rs @@ -178,7 +178,7 @@ impl Blas { #[cfg(custom)] /// Returns custom implementation of Blas (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/buffer.rs b/wgpu/src/api/buffer.rs index 634e1913134..c962c8d65ca 100644 --- a/wgpu/src/api/buffer.rs +++ b/wgpu/src/api/buffer.rs @@ -390,7 +390,7 @@ impl Buffer { #[cfg(custom)] /// Returns custom implementation of Buffer (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/command_buffer.rs b/wgpu/src/api/command_buffer.rs index 56d6e32129c..eb1358946aa 100644 --- a/wgpu/src/api/command_buffer.rs +++ b/wgpu/src/api/command_buffer.rs @@ -18,6 +18,6 @@ impl CommandBuffer { #[cfg(custom)] /// Returns custom implementation of CommandBuffer (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.buffer.as_custom_opt().and_then(|c| c.downcast()) + self.buffer.as_custom() } } diff --git a/wgpu/src/api/command_encoder.rs b/wgpu/src/api/command_encoder.rs index 3014ed3fba9..b53d65168d1 100644 --- a/wgpu/src/api/command_encoder.rs +++ b/wgpu/src/api/command_encoder.rs @@ -30,7 +30,7 @@ impl CommandEncoder { #[cfg(custom)] /// Returns custom implementation of CommandEncoder (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/compute_pass.rs b/wgpu/src/api/compute_pass.rs index 76c2b87f765..18d2f30eeb6 100644 --- a/wgpu/src/api/compute_pass.rs +++ b/wgpu/src/api/compute_pass.rs @@ -98,7 +98,7 @@ impl ComputePass<'_> { #[cfg(custom)] /// Returns custom implementation of ComputePass (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/compute_pipeline.rs b/wgpu/src/api/compute_pipeline.rs index 1886da2ee79..86e39427baa 100644 --- a/wgpu/src/api/compute_pipeline.rs +++ b/wgpu/src/api/compute_pipeline.rs @@ -19,7 +19,7 @@ impl ComputePipeline { #[cfg(custom)] /// Returns custom implementation of ComputePipeline (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/device.rs b/wgpu/src/api/device.rs index 34dc28a400f..637ca9310e0 100644 --- a/wgpu/src/api/device.rs +++ b/wgpu/src/api/device.rs @@ -37,7 +37,7 @@ impl Device { #[cfg(custom)] /// Returns custom implementation of Device (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } #[cfg(custom)] diff --git a/wgpu/src/api/instance.rs b/wgpu/src/api/instance.rs index 8ed5649aae5..24815df64b0 100644 --- a/wgpu/src/api/instance.rs +++ b/wgpu/src/api/instance.rs @@ -211,7 +211,7 @@ impl Instance { #[cfg(custom)] /// Returns custom implementation of Instance (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } /// Retrieves all available [`Adapter`]s that match the given [`Backends`]. diff --git a/wgpu/src/api/pipeline_cache.rs b/wgpu/src/api/pipeline_cache.rs index 41194830b89..c6f161abaf2 100644 --- a/wgpu/src/api/pipeline_cache.rs +++ b/wgpu/src/api/pipeline_cache.rs @@ -91,6 +91,6 @@ impl PipelineCache { #[cfg(custom)] /// Returns custom implementation of PipelineCache (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/pipeline_layout.rs b/wgpu/src/api/pipeline_layout.rs index 54aaf1bd4aa..6bd71093ca0 100644 --- a/wgpu/src/api/pipeline_layout.rs +++ b/wgpu/src/api/pipeline_layout.rs @@ -19,7 +19,7 @@ impl PipelineLayout { #[cfg(custom)] /// Returns custom implementation of PipelineLayout (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/query_set.rs b/wgpu/src/api/query_set.rs index c451bd27246..dc89330f1b9 100644 --- a/wgpu/src/api/query_set.rs +++ b/wgpu/src/api/query_set.rs @@ -19,7 +19,7 @@ impl QuerySet { #[cfg(custom)] /// Returns custom implementation of QuerySet (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/queue.rs b/wgpu/src/api/queue.rs index a6a8c4b78c1..790643fbe9a 100644 --- a/wgpu/src/api/queue.rs +++ b/wgpu/src/api/queue.rs @@ -23,7 +23,7 @@ impl Queue { #[cfg(custom)] /// Returns custom implementation of Queue (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } #[cfg(custom)] @@ -72,7 +72,7 @@ impl QueueWriteBufferView<'_> { #[cfg(custom)] /// Returns custom implementation of QueueWriteBufferView (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/render_bundle.rs b/wgpu/src/api/render_bundle.rs index 9635ae0ce43..2ea592940d0 100644 --- a/wgpu/src/api/render_bundle.rs +++ b/wgpu/src/api/render_bundle.rs @@ -22,7 +22,7 @@ impl RenderBundle { #[cfg(custom)] /// Returns custom implementation of RenderBundle (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/render_bundle_encoder.rs b/wgpu/src/api/render_bundle_encoder.rs index 8b2560f5351..56b6f841620 100644 --- a/wgpu/src/api/render_bundle_encoder.rs +++ b/wgpu/src/api/render_bundle_encoder.rs @@ -29,7 +29,7 @@ impl RenderBundleEncoder<'_> { #[cfg(custom)] /// Returns custom implementation of RenderBundleEncoder (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/render_pass.rs b/wgpu/src/api/render_pass.rs index 61e212086b6..0af43c44009 100644 --- a/wgpu/src/api/render_pass.rs +++ b/wgpu/src/api/render_pass.rs @@ -40,7 +40,7 @@ impl RenderPass<'_> { #[cfg(custom)] /// Returns custom implementation of RenderPass (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/render_pipeline.rs b/wgpu/src/api/render_pipeline.rs index 12375cda73d..d607b2ad94a 100644 --- a/wgpu/src/api/render_pipeline.rs +++ b/wgpu/src/api/render_pipeline.rs @@ -21,7 +21,7 @@ impl RenderPipeline { #[cfg(custom)] /// Returns custom implementation of RenderPipeline (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/sampler.rs b/wgpu/src/api/sampler.rs index fde912b3c72..a8916829c86 100644 --- a/wgpu/src/api/sampler.rs +++ b/wgpu/src/api/sampler.rs @@ -22,7 +22,7 @@ impl Sampler { #[cfg(custom)] /// Returns custom implementation of Sampler (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/shader_module.rs b/wgpu/src/api/shader_module.rs index 5c6dba421fc..dec0d34e900 100644 --- a/wgpu/src/api/shader_module.rs +++ b/wgpu/src/api/shader_module.rs @@ -28,7 +28,7 @@ impl ShaderModule { #[cfg(custom)] /// Returns custom implementation of ShaderModule (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/surface.rs b/wgpu/src/api/surface.rs index 4970b5c19c8..773d0123d9a 100644 --- a/wgpu/src/api/surface.rs +++ b/wgpu/src/api/surface.rs @@ -175,7 +175,7 @@ impl Surface<'_> { #[cfg(custom)] /// Returns custom implementation of Surface (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/surface_texture.rs b/wgpu/src/api/surface_texture.rs index 23dbf99b889..6758e61a7bf 100644 --- a/wgpu/src/api/surface_texture.rs +++ b/wgpu/src/api/surface_texture.rs @@ -42,7 +42,7 @@ impl SurfaceTexture { #[cfg(custom)] /// Returns custom implementation of SurfaceTexture (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.detail.as_custom_opt().and_then(|c| c.downcast()) + self.detail.as_custom() } } diff --git a/wgpu/src/api/texture.rs b/wgpu/src/api/texture.rs index d0ac76eab0d..6217a57f3a8 100644 --- a/wgpu/src/api/texture.rs +++ b/wgpu/src/api/texture.rs @@ -19,7 +19,7 @@ impl Texture { #[cfg(custom)] /// Returns custom implementation of Texture (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/texture_view.rs b/wgpu/src/api/texture_view.rs index f7039502340..9978d3f8623 100644 --- a/wgpu/src/api/texture_view.rs +++ b/wgpu/src/api/texture_view.rs @@ -22,7 +22,7 @@ impl TextureView { #[cfg(custom)] /// Returns custom implementation of TextureView (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom_opt().and_then(|c| c.downcast()) + self.inner.as_custom() } } diff --git a/wgpu/src/api/tlas.rs b/wgpu/src/api/tlas.rs index 1149bd6c718..3a5e7659422 100644 --- a/wgpu/src/api/tlas.rs +++ b/wgpu/src/api/tlas.rs @@ -61,7 +61,7 @@ impl Tlas { #[cfg(custom)] /// Returns custom implementation of Tlas (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { - self.shared.inner.as_custom_opt().and_then(|c| c.downcast()) + self.shared.inner.as_custom() } } diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index ca3bb4fba8c..ca48feca147 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -590,9 +590,9 @@ macro_rules! dispatch_types { #[cfg(custom)] #[inline] #[allow(clippy::allow_attributes, unused)] - pub(crate) fn as_custom_opt(&self) -> Option<&$custom_type> { + pub fn as_custom(&self) -> Option<&T> { match self { - Self::Custom(value) => Some(value), + Self::Custom(value) => value.downcast(), _ => None, } } @@ -718,9 +718,9 @@ macro_rules! dispatch_types { #[cfg(custom)] #[inline] #[allow(clippy::allow_attributes, unused)] - pub(crate) fn as_custom_opt(&self) -> Option<&$custom_type> { + pub fn as_custom(&self) -> Option<&T> { match self { - Self::Custom(value) => Some(value), + Self::Custom(value) => value.downcast(), _ => None, } } From c82049e85fe6c33ac3ef66fba62ed1afe4cf0999 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Fri, 18 Apr 2025 07:09:02 +0200 Subject: [PATCH 5/5] join impls if possible Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu/src/api/bind_group_layout.rs | 4 ++-- wgpu/src/api/command_encoder.rs | 14 ++++++-------- wgpu/src/api/compute_pipeline.rs | 14 ++++++-------- wgpu/src/api/render_bundle_encoder.rs | 14 ++++++-------- wgpu/src/api/render_pass.rs | 14 ++++++-------- wgpu/src/api/render_pipeline.rs | 14 ++++++-------- wgpu/src/api/shader_module.rs | 12 +++++------- wgpu/src/api/texture.rs | 14 ++++++-------- wgpu/src/api/texture_view.rs | 14 ++++++-------- 9 files changed, 49 insertions(+), 65 deletions(-) diff --git a/wgpu/src/api/bind_group_layout.rs b/wgpu/src/api/bind_group_layout.rs index a7a50ca7270..62eb7dd4147 100644 --- a/wgpu/src/api/bind_group_layout.rs +++ b/wgpu/src/api/bind_group_layout.rs @@ -18,6 +18,8 @@ pub struct BindGroupLayout { #[cfg(send_sync)] static_assertions::assert_impl_all!(BindGroupLayout: Send, Sync); +crate::cmp::impl_eq_ord_hash_proxy!(BindGroupLayout => .inner); + impl BindGroupLayout { #[cfg(custom)] /// Returns custom implementation of BindGroupLayout (if custom backend and is internally T) @@ -26,8 +28,6 @@ impl BindGroupLayout { } } -crate::cmp::impl_eq_ord_hash_proxy!(BindGroupLayout => .inner); - /// Describes a [`BindGroupLayout`]. /// /// For use with [`Device::create_bind_group_layout`]. diff --git a/wgpu/src/api/command_encoder.rs b/wgpu/src/api/command_encoder.rs index b53d65168d1..6e91a452844 100644 --- a/wgpu/src/api/command_encoder.rs +++ b/wgpu/src/api/command_encoder.rs @@ -26,14 +26,6 @@ static_assertions::assert_impl_all!(CommandEncoder: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(CommandEncoder => .inner); -impl CommandEncoder { - #[cfg(custom)] - /// Returns custom implementation of CommandEncoder (if custom backend and is internally T) - pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom() - } -} - /// Describes a [`CommandEncoder`]. /// /// For use with [`Device::create_command_encoder`]. @@ -270,6 +262,12 @@ impl CommandEncoder { hal_command_encoder_callback(None) } } + + #[cfg(custom)] + /// Returns custom implementation of CommandEncoder (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom() + } } /// [`Features::TIMESTAMP_QUERY_INSIDE_ENCODERS`] must be enabled on the device in order to call these functions. diff --git a/wgpu/src/api/compute_pipeline.rs b/wgpu/src/api/compute_pipeline.rs index 86e39427baa..499f967b540 100644 --- a/wgpu/src/api/compute_pipeline.rs +++ b/wgpu/src/api/compute_pipeline.rs @@ -15,14 +15,6 @@ static_assertions::assert_impl_all!(ComputePipeline: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(ComputePipeline => .inner); -impl ComputePipeline { - #[cfg(custom)] - /// Returns custom implementation of ComputePipeline (if custom backend and is internally T) - pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom() - } -} - impl ComputePipeline { /// Get an object representing the bind group layout at a given index. /// @@ -35,6 +27,12 @@ impl ComputePipeline { let bind_group = self.inner.get_bind_group_layout(index); BindGroupLayout { inner: bind_group } } + + #[cfg(custom)] + /// Returns custom implementation of ComputePipeline (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom() + } } /// Describes a compute pipeline. diff --git a/wgpu/src/api/render_bundle_encoder.rs b/wgpu/src/api/render_bundle_encoder.rs index 56b6f841620..a33c3cb237b 100644 --- a/wgpu/src/api/render_bundle_encoder.rs +++ b/wgpu/src/api/render_bundle_encoder.rs @@ -25,14 +25,6 @@ static_assertions::assert_not_impl_any!(RenderBundleEncoder<'_>: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(RenderBundleEncoder<'_> => .inner); -impl RenderBundleEncoder<'_> { - #[cfg(custom)] - /// Returns custom implementation of RenderBundleEncoder (if custom backend and is internally T) - pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom() - } -} - /// Describes a [`RenderBundleEncoder`]. /// /// For use with [`Device::create_render_bundle_encoder`]. @@ -196,6 +188,12 @@ impl<'a> RenderBundleEncoder<'a> { self.inner .draw_indexed_indirect(&indirect_buffer.inner, indirect_offset); } + + #[cfg(custom)] + /// Returns custom implementation of RenderBundleEncoder (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom() + } } /// [`Features::PUSH_CONSTANTS`] must be enabled on the device in order to call these functions. diff --git a/wgpu/src/api/render_pass.rs b/wgpu/src/api/render_pass.rs index 0af43c44009..2a2d3bd08e2 100644 --- a/wgpu/src/api/render_pass.rs +++ b/wgpu/src/api/render_pass.rs @@ -36,14 +36,6 @@ static_assertions::assert_impl_all!(RenderPass<'_>: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(RenderPass<'_> => .inner); -impl RenderPass<'_> { - #[cfg(custom)] - /// Returns custom implementation of RenderPass (if custom backend and is internally T) - pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom() - } -} - impl RenderPass<'_> { /// Drops the lifetime relationship to the parent command encoder, making usage of /// the encoder while this pass is recorded a run-time error instead. @@ -332,6 +324,12 @@ impl RenderPass<'_> { self.inner .multi_draw_indexed_indirect(&indirect_buffer.inner, indirect_offset, count); } + + #[cfg(custom)] + /// Returns custom implementation of RenderPass (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom() + } } /// [`Features::MULTI_DRAW_INDIRECT_COUNT`] must be enabled on the device in order to call these functions. diff --git a/wgpu/src/api/render_pipeline.rs b/wgpu/src/api/render_pipeline.rs index d607b2ad94a..13b4f9b5f7b 100644 --- a/wgpu/src/api/render_pipeline.rs +++ b/wgpu/src/api/render_pipeline.rs @@ -17,14 +17,6 @@ static_assertions::assert_impl_all!(RenderPipeline: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(RenderPipeline => .inner); -impl RenderPipeline { - #[cfg(custom)] - /// Returns custom implementation of RenderPipeline (if custom backend and is internally T) - pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom() - } -} - impl RenderPipeline { /// Get an object representing the bind group layout at a given index. /// @@ -36,6 +28,12 @@ impl RenderPipeline { let layout = self.inner.get_bind_group_layout(index); BindGroupLayout { inner: layout } } + + #[cfg(custom)] + /// Returns custom implementation of RenderPipeline (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom() + } } /// Specifies an interpretation of the bytes of a vertex buffer as vertex attributes. diff --git a/wgpu/src/api/shader_module.rs b/wgpu/src/api/shader_module.rs index dec0d34e900..c4d7a5183e3 100644 --- a/wgpu/src/api/shader_module.rs +++ b/wgpu/src/api/shader_module.rs @@ -25,6 +25,11 @@ static_assertions::assert_impl_all!(ShaderModule: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(ShaderModule => .inner); impl ShaderModule { + /// Get the compilation info for the shader module. + pub fn get_compilation_info(&self) -> impl Future + WasmNotSend { + self.inner.get_compilation_info() + } + #[cfg(custom)] /// Returns custom implementation of ShaderModule (if custom backend and is internally T) pub fn as_custom(&self) -> Option<&T> { @@ -32,13 +37,6 @@ impl ShaderModule { } } -impl ShaderModule { - /// Get the compilation info for the shader module. - pub fn get_compilation_info(&self) -> impl Future + WasmNotSend { - self.inner.get_compilation_info() - } -} - /// Compilation information for a shader module. /// /// Corresponds to [WebGPU `GPUCompilationInfo`](https://gpuweb.github.io/gpuweb/#gpucompilationinfo). diff --git a/wgpu/src/api/texture.rs b/wgpu/src/api/texture.rs index 6217a57f3a8..6dff7b6a11e 100644 --- a/wgpu/src/api/texture.rs +++ b/wgpu/src/api/texture.rs @@ -15,14 +15,6 @@ static_assertions::assert_impl_all!(Texture: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(Texture => .inner); -impl Texture { - #[cfg(custom)] - /// Returns custom implementation of Texture (if custom backend and is internally T) - pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom() - } -} - impl Texture { /// Returns the inner hal Texture using a callback. The hal texture will be `None` if the /// backend type argument does not match with this wgpu Texture @@ -45,6 +37,12 @@ impl Texture { } } + #[cfg(custom)] + /// Returns custom implementation of Texture (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom() + } + /// Creates a view of this texture, specifying an interpretation of its texels and /// possibly a subset of its layers and mip levels. /// diff --git a/wgpu/src/api/texture_view.rs b/wgpu/src/api/texture_view.rs index 9978d3f8623..e5af89d62e7 100644 --- a/wgpu/src/api/texture_view.rs +++ b/wgpu/src/api/texture_view.rs @@ -18,14 +18,6 @@ static_assertions::assert_impl_all!(TextureView: Send, Sync); crate::cmp::impl_eq_ord_hash_proxy!(TextureView => .inner); -impl TextureView { - #[cfg(custom)] - /// Returns custom implementation of TextureView (if custom backend and is internally T) - pub fn as_custom(&self) -> Option<&T> { - self.inner.as_custom() - } -} - impl TextureView { /// Returns the inner hal `TextureView` using a callback. The hal texture will be `None` if the /// backend type argument does not match with this wgpu Texture @@ -48,6 +40,12 @@ impl TextureView { hal_texture_view_callback(None) } } + + #[cfg(custom)] + /// Returns custom implementation of TextureView (if custom backend and is internally T) + pub fn as_custom(&self) -> Option<&T> { + self.inner.as_custom() + } } /// Describes a [`TextureView`].