Skip to main content

wgpu/backend/
custom.rs

1//! Provides wrappers custom backend implementations
2
3#![allow(ambiguous_wide_pointer_comparisons)]
4
5pub use crate::dispatch::*;
6
7use crate::cmp::AtomicU64;
8use alloc::boxed::Box;
9use alloc::sync::Arc;
10use core::sync::atomic::Ordering;
11
12macro_rules! dyn_type {
13    // cloning of arc forbidden
14    // but we still use it to provide Eq,Ord,Hash implementations
15    (pub mut struct $name:ident(dyn $interface:tt)) => {
16        #[derive(Debug)]
17        pub(crate) struct $name(Arc<dyn $interface>);
18        crate::cmp::impl_eq_ord_hash_arc_address!($name => .0);
19
20        impl $name {
21            pub(crate) fn new<T: $interface>(t: T) -> Self {
22                Self(Arc::new(t))
23            }
24
25            #[allow(clippy::allow_attributes, dead_code)]
26            pub(crate) fn downcast<T: $interface>(&self) -> Option<&T> {
27                self.0.as_ref().as_any().downcast_ref()
28            }
29        }
30
31        impl core::ops::Deref for $name {
32            type Target = dyn $interface;
33
34            #[inline]
35            fn deref(&self) -> &Self::Target {
36                self.0.as_ref()
37            }
38        }
39
40        impl core::ops::DerefMut for $name {
41            #[inline]
42            fn deref_mut(&mut self) -> &mut Self::Target {
43                Arc::get_mut(&mut self.0).expect("")
44            }
45        }
46    };
47    // cloning of arc is allowed
48    (pub ref struct $name:ident(dyn $interface:tt)) => {
49        #[derive(Debug, Clone)]
50        pub(crate) struct $name(Arc<dyn $interface>);
51        crate::cmp::impl_eq_ord_hash_arc_address!($name => .0);
52
53        impl $name {
54            pub(crate) fn new<T: $interface>(t: T) -> Self {
55                Self(Arc::new(t))
56            }
57
58            pub(crate) fn downcast<T: $interface>(&self) -> Option<&T> {
59                self.0.as_ref().as_any().downcast_ref()
60            }
61        }
62
63        impl core::ops::Deref for $name {
64            type Target = dyn $interface;
65
66            #[inline]
67            fn deref(&self) -> &Self::Target {
68                self.0.as_ref()
69            }
70        }
71    };
72}
73
74dyn_type!(pub ref struct DynContext(dyn InstanceInterface));
75dyn_type!(pub ref struct DynAdapter(dyn AdapterInterface));
76dyn_type!(pub ref struct DynDevice(dyn DeviceInterface));
77dyn_type!(pub ref struct DynQueue(dyn QueueInterface));
78dyn_type!(pub ref struct DynShaderModule(dyn ShaderModuleInterface));
79dyn_type!(pub ref struct DynBindGroupLayout(dyn BindGroupLayoutInterface));
80dyn_type!(pub ref struct DynBindGroup(dyn BindGroupInterface));
81dyn_type!(pub ref struct DynTextureView(dyn TextureViewInterface));
82dyn_type!(pub ref struct DynSampler(dyn SamplerInterface));
83dyn_type!(pub ref struct DynBuffer(dyn BufferInterface));
84dyn_type!(pub ref struct DynTexture(dyn TextureInterface));
85dyn_type!(pub ref struct DynExternalTexture(dyn ExternalTextureInterface));
86dyn_type!(pub ref struct DynBlas(dyn BlasInterface));
87dyn_type!(pub ref struct DynTlas(dyn TlasInterface));
88dyn_type!(pub ref struct DynQuerySet(dyn QuerySetInterface));
89dyn_type!(pub ref struct DynPipelineLayout(dyn PipelineLayoutInterface));
90dyn_type!(pub ref struct DynRenderPipeline(dyn RenderPipelineInterface));
91dyn_type!(pub ref struct DynComputePipeline(dyn ComputePipelineInterface));
92dyn_type!(pub ref struct DynRayTracingPipeline(dyn RayTracingPipelineInterface));
93dyn_type!(pub ref struct DynPipelineCache(dyn PipelineCacheInterface));
94dyn_type!(pub mut struct DynCommandEncoder(dyn CommandEncoderInterface));
95dyn_type!(pub mut struct DynComputePass(dyn ComputePassInterface));
96dyn_type!(pub mut struct DynRenderPass(dyn RenderPassInterface));
97dyn_type!(pub mut struct DynRayTracingPass(dyn RayTracingPassInterface));
98dyn_type!(pub mut struct DynCommandBuffer(dyn CommandBufferInterface));
99
100static NEXT_RENDER_BUNDLE_ENCODER_ID: AtomicU64 = AtomicU64::new(0);
101
102// DynRenderBundleEncoder uses Box instead of Arc so that finish_boxed(self: Box<Self>)
103// can be dispatched through the trait object (consuming the encoder).
104#[derive(Debug)]
105pub(crate) struct DynRenderBundleEncoder {
106    // Unique identity for Eq/Ord/Hash. The data pointer of the boxed trait object is
107    // not safe to use for identity because ZST impls share the same dangling address.
108    id: u64,
109    inner: Box<dyn RenderBundleEncoderInterface>,
110}
111
112impl DynRenderBundleEncoder {
113    pub(crate) fn new<T: RenderBundleEncoderInterface>(t: T) -> Self {
114        Self {
115            id: NEXT_RENDER_BUNDLE_ENCODER_ID.fetch_add(1, Ordering::Relaxed),
116            inner: Box::new(t),
117        }
118    }
119
120    pub(crate) fn downcast<T: RenderBundleEncoderInterface>(&self) -> Option<&T> {
121        self.inner.as_ref().as_any().downcast_ref()
122    }
123
124    pub(crate) fn finish_boxed(
125        self,
126        desc: &crate::RenderBundleDescriptor<'_>,
127    ) -> crate::dispatch::DispatchRenderBundle {
128        self.inner.finish_boxed(desc)
129    }
130}
131
132impl core::ops::Deref for DynRenderBundleEncoder {
133    type Target = dyn RenderBundleEncoderInterface;
134    #[inline]
135    fn deref(&self) -> &Self::Target {
136        self.inner.as_ref()
137    }
138}
139
140impl core::ops::DerefMut for DynRenderBundleEncoder {
141    #[inline]
142    fn deref_mut(&mut self) -> &mut Self::Target {
143        self.inner.as_mut()
144    }
145}
146
147// Eq/Ord/Hash for DynRenderBundleEncoder are based on a unique id assigned at construction.
148//
149// These impls are not semantically meaningful (we never sort or deduplicate encoders by
150// "value") but are required to satisfy bounds imposed by the dispatch enum machinery.
151impl PartialEq for DynRenderBundleEncoder {
152    fn eq(&self, other: &Self) -> bool {
153        self.id == other.id
154    }
155}
156impl Eq for DynRenderBundleEncoder {}
157
158impl PartialOrd for DynRenderBundleEncoder {
159    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
160        Some(self.cmp(other))
161    }
162}
163impl Ord for DynRenderBundleEncoder {
164    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
165        self.id.cmp(&other.id)
166    }
167}
168
169impl core::hash::Hash for DynRenderBundleEncoder {
170    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
171        self.id.hash(state);
172    }
173}
174
175dyn_type!(pub ref struct DynRenderBundle(dyn RenderBundleInterface));
176dyn_type!(pub ref struct DynSurface(dyn SurfaceInterface));
177dyn_type!(pub ref struct DynSurfaceOutputDetail(dyn SurfaceOutputDetailInterface));
178dyn_type!(pub mut struct DynQueueWriteBuffer(dyn QueueWriteBufferInterface));
179dyn_type!(pub mut struct DynBufferMappedRange(dyn BufferMappedRangeInterface));