1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
mod adapter;
mod command;
mod device;
mod instance;
mod queue;
mod surface;

pub use adapter::{DynAdapter, DynOpenDevice};
pub use command::DynCommandEncoder;
pub use device::DynDevice;
pub use instance::{DynExposedAdapter, DynInstance};
pub use queue::DynQueue;
pub use surface::{DynAcquiredSurfaceTexture, DynSurface};

use std::any::Any;

use wgt::WasmNotSendSync;

use crate::{
    AccelerationStructureAABBs, AccelerationStructureEntries, AccelerationStructureInstances,
    AccelerationStructureTriangleIndices, AccelerationStructureTriangleTransform,
    AccelerationStructureTriangles, BufferBinding, ProgrammableStage, TextureBinding,
};

/// Base trait for all resources, allows downcasting via [`Any`].
pub trait DynResource: Any + WasmNotSendSync + 'static {
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// Utility macro for implementing `DynResource` for a list of types.
macro_rules! impl_dyn_resource {
    ($($type:ty),*) => {
        $(
            impl crate::DynResource for $type {
                fn as_any(&self) -> &dyn ::std::any::Any {
                    self
                }

                fn as_any_mut(&mut self) -> &mut dyn ::std::any::Any {
                    self
                }
            }
        )*
    };
}
pub(crate) use impl_dyn_resource;

/// Extension trait for `DynResource` used by implementations of various dynamic resource traits.
trait DynResourceExt {
    /// # Panics
    ///
    /// - Panics if `self` is not downcastable to `T`.
    fn expect_downcast_ref<T: DynResource>(&self) -> &T;
    /// # Panics
    ///
    /// - Panics if `self` is not downcastable to `T`.
    fn expect_downcast_mut<T: DynResource>(&mut self) -> &mut T;

    /// Unboxes a `Box<dyn DynResource>` to a concrete type.
    ///
    /// # Safety
    ///
    /// - `self` must be the correct concrete type.
    unsafe fn unbox<T: DynResource + 'static>(self: Box<Self>) -> T;
}

impl<R: DynResource + ?Sized> DynResourceExt for R {
    fn expect_downcast_ref<'a, T: DynResource>(&'a self) -> &'a T {
        self.as_any()
            .downcast_ref()
            .expect("Resource doesn't have the expected backend type.")
    }

    fn expect_downcast_mut<'a, T: DynResource>(&'a mut self) -> &'a mut T {
        self.as_any_mut()
            .downcast_mut()
            .expect("Resource doesn't have the expected backend type.")
    }

    unsafe fn unbox<T: DynResource + 'static>(self: Box<Self>) -> T {
        debug_assert!(
            <Self as Any>::type_id(self.as_ref()) == std::any::TypeId::of::<T>(),
            "Resource doesn't have the expected type, expected {:?}, got {:?}",
            std::any::TypeId::of::<T>(),
            <Self as Any>::type_id(self.as_ref())
        );

        let casted_ptr = Box::into_raw(self).cast::<T>();
        // SAFETY: This is adheres to the safety contract of `Box::from_raw` because:
        //
        // - We are casting the value of a previously `Box`ed value, which guarantees:
        //   - `casted_ptr` is not null.
        //   - `casted_ptr` is valid for reads and writes, though by itself this does not mean
        //     valid reads and writes for `T` (read on for that).
        // - We don't change the allocator.
        // - The contract of `Box::from_raw` requires that an initialized and aligned `T` is stored
        //   within `casted_ptr`.
        *unsafe { Box::from_raw(casted_ptr) }
    }
}

pub trait DynAccelerationStructure: DynResource + std::fmt::Debug {}
pub trait DynBindGroup: DynResource + std::fmt::Debug {}
pub trait DynBindGroupLayout: DynResource + std::fmt::Debug {}
pub trait DynBuffer: DynResource + std::fmt::Debug {}
pub trait DynCommandBuffer: DynResource + std::fmt::Debug {}
pub trait DynComputePipeline: DynResource + std::fmt::Debug {}
pub trait DynFence: DynResource + std::fmt::Debug {}
pub trait DynPipelineCache: DynResource + std::fmt::Debug {}
pub trait DynPipelineLayout: DynResource + std::fmt::Debug {}
pub trait DynQuerySet: DynResource + std::fmt::Debug {}
pub trait DynRenderPipeline: DynResource + std::fmt::Debug {}
pub trait DynSampler: DynResource + std::fmt::Debug {}
pub trait DynShaderModule: DynResource + std::fmt::Debug {}
pub trait DynSurfaceTexture:
    DynResource + std::borrow::Borrow<dyn DynTexture> + std::fmt::Debug
{
}
pub trait DynTexture: DynResource + std::fmt::Debug {}
pub trait DynTextureView: DynResource + std::fmt::Debug {}

impl<'a> BufferBinding<'a, dyn DynBuffer> {
    pub fn expect_downcast<B: DynBuffer>(self) -> BufferBinding<'a, B> {
        BufferBinding {
            buffer: self.buffer.expect_downcast_ref(),
            offset: self.offset,
            size: self.size,
        }
    }
}

impl<'a> TextureBinding<'a, dyn DynTextureView> {
    pub fn expect_downcast<T: DynTextureView>(self) -> TextureBinding<'a, T> {
        TextureBinding {
            view: self.view.expect_downcast_ref(),
            usage: self.usage,
        }
    }
}

impl<'a> ProgrammableStage<'a, dyn DynShaderModule> {
    fn expect_downcast<T: DynShaderModule>(self) -> ProgrammableStage<'a, T> {
        ProgrammableStage {
            module: self.module.expect_downcast_ref(),
            entry_point: self.entry_point,
            constants: self.constants,
            zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory,
        }
    }
}

impl<'a> AccelerationStructureEntries<'a, dyn DynBuffer> {
    fn expect_downcast<B: DynBuffer>(&self) -> AccelerationStructureEntries<'a, B> {
        match self {
            AccelerationStructureEntries::Instances(instances) => {
                AccelerationStructureEntries::Instances(AccelerationStructureInstances {
                    buffer: instances.buffer.map(|b| b.expect_downcast_ref()),
                    offset: instances.offset,
                    count: instances.count,
                })
            }
            AccelerationStructureEntries::Triangles(triangles) => {
                AccelerationStructureEntries::Triangles(
                    triangles
                        .iter()
                        .map(|t| AccelerationStructureTriangles {
                            vertex_buffer: t.vertex_buffer.map(|b| b.expect_downcast_ref()),
                            vertex_format: t.vertex_format,
                            first_vertex: t.first_vertex,
                            vertex_count: t.vertex_count,
                            vertex_stride: t.vertex_stride,
                            indices: t.indices.as_ref().map(|i| {
                                AccelerationStructureTriangleIndices {
                                    buffer: i.buffer.map(|b| b.expect_downcast_ref()),
                                    format: i.format,
                                    offset: i.offset,
                                    count: i.count,
                                }
                            }),
                            transform: t.transform.as_ref().map(|t| {
                                AccelerationStructureTriangleTransform {
                                    buffer: t.buffer.expect_downcast_ref(),
                                    offset: t.offset,
                                }
                            }),
                            flags: t.flags,
                        })
                        .collect(),
                )
            }
            AccelerationStructureEntries::AABBs(entries) => AccelerationStructureEntries::AABBs(
                entries
                    .iter()
                    .map(|e| AccelerationStructureAABBs {
                        buffer: e.buffer.map(|b| b.expect_downcast_ref()),
                        offset: e.offset,
                        count: e.count,
                        stride: e.stride,
                        flags: e.flags,
                    })
                    .collect(),
            ),
        }
    }
}