wgpu/backend/
custom.rs

1//! Provides wrappers custom backend implementations
2
3#![allow(ambiguous_wide_pointer_comparisons)]
4
5pub use crate::dispatch::*;
6
7use alloc::sync::Arc;
8
9macro_rules! dyn_type {
10    // cloning of arc forbidden
11    // but we still use it to provide Eq,Ord,Hash implementations
12    (pub mut struct $name:ident(dyn $interface:tt)) => {
13        #[derive(Debug)]
14        pub(crate) struct $name(Arc<dyn $interface>);
15        crate::cmp::impl_eq_ord_hash_arc_address!($name => .0);
16
17        impl $name {
18            pub(crate) fn new<T: $interface>(t: T) -> Self {
19                Self(Arc::new(t))
20            }
21
22            #[allow(clippy::allow_attributes, dead_code)]
23            pub(crate) fn downcast<T: $interface>(&self) -> Option<&T> {
24                self.0.as_ref().as_any().downcast_ref()
25            }
26        }
27
28        impl core::ops::Deref for $name {
29            type Target = dyn $interface;
30
31            #[inline]
32            fn deref(&self) -> &Self::Target {
33                self.0.as_ref()
34            }
35        }
36
37        impl core::ops::DerefMut for $name {
38            #[inline]
39            fn deref_mut(&mut self) -> &mut Self::Target {
40                Arc::get_mut(&mut self.0).expect("")
41            }
42        }
43    };
44    // cloning of arc is allowed
45    (pub ref struct $name:ident(dyn $interface:tt)) => {
46        #[derive(Debug, Clone)]
47        pub(crate) struct $name(Arc<dyn $interface>);
48        crate::cmp::impl_eq_ord_hash_arc_address!($name => .0);
49
50        impl $name {
51            pub(crate) fn new<T: $interface>(t: T) -> Self {
52                Self(Arc::new(t))
53            }
54
55            pub(crate) fn downcast<T: $interface>(&self) -> Option<&T> {
56                self.0.as_ref().as_any().downcast_ref()
57            }
58        }
59
60        impl core::ops::Deref for $name {
61            type Target = dyn $interface;
62
63            #[inline]
64            fn deref(&self) -> &Self::Target {
65                self.0.as_ref()
66            }
67        }
68    };
69}
70
71dyn_type!(pub ref struct DynContext(dyn InstanceInterface));
72dyn_type!(pub ref struct DynAdapter(dyn AdapterInterface));
73dyn_type!(pub ref struct DynDevice(dyn DeviceInterface));
74dyn_type!(pub ref struct DynQueue(dyn QueueInterface));
75dyn_type!(pub ref struct DynShaderModule(dyn ShaderModuleInterface));
76dyn_type!(pub ref struct DynBindGroupLayout(dyn BindGroupLayoutInterface));
77dyn_type!(pub ref struct DynBindGroup(dyn BindGroupInterface));
78dyn_type!(pub ref struct DynTextureView(dyn TextureViewInterface));
79dyn_type!(pub ref struct DynSampler(dyn SamplerInterface));
80dyn_type!(pub ref struct DynBuffer(dyn BufferInterface));
81dyn_type!(pub ref struct DynTexture(dyn TextureInterface));
82dyn_type!(pub ref struct DynExternalTexture(dyn ExternalTextureInterface));
83dyn_type!(pub ref struct DynBlas(dyn BlasInterface));
84dyn_type!(pub ref struct DynTlas(dyn TlasInterface));
85dyn_type!(pub ref struct DynQuerySet(dyn QuerySetInterface));
86dyn_type!(pub ref struct DynPipelineLayout(dyn PipelineLayoutInterface));
87dyn_type!(pub ref struct DynRenderPipeline(dyn RenderPipelineInterface));
88dyn_type!(pub ref struct DynComputePipeline(dyn ComputePipelineInterface));
89dyn_type!(pub ref struct DynPipelineCache(dyn PipelineCacheInterface));
90dyn_type!(pub mut struct DynCommandEncoder(dyn CommandEncoderInterface));
91dyn_type!(pub mut struct DynComputePass(dyn ComputePassInterface));
92dyn_type!(pub mut struct DynRenderPass(dyn RenderPassInterface));
93dyn_type!(pub ref struct DynCommandBuffer(dyn CommandBufferInterface));
94dyn_type!(pub mut struct DynRenderBundleEncoder(dyn RenderBundleEncoderInterface));
95dyn_type!(pub ref struct DynRenderBundle(dyn RenderBundleInterface));
96dyn_type!(pub ref struct DynSurface(dyn SurfaceInterface));
97dyn_type!(pub ref struct DynSurfaceOutputDetail(dyn SurfaceOutputDetailInterface));
98dyn_type!(pub mut struct DynQueueWriteBuffer(dyn QueueWriteBufferInterface));
99dyn_type!(pub mut struct DynBufferMappedRange(dyn BufferMappedRangeInterface));