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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use smallvec::SmallVec;
use webgpu::wgc::id::markers::{
    Adapter, BindGroup, BindGroupLayout, Buffer, CommandEncoder, ComputePipeline, Device,
    PipelineLayout, RenderBundle, RenderPipeline, Sampler, ShaderModule, Texture, TextureView,
};
use webgpu::wgc::id::{
    AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandEncoderId, ComputePipelineId,
    DeviceId, PipelineLayoutId, RenderBundleId, RenderPipelineId, SamplerId, ShaderModuleId,
    TextureId, TextureViewId,
};
use webgpu::wgc::identity::IdentityManager;
use webgpu::wgt::Backend;

#[derive(Debug)]
pub struct IdentityHub {
    adapters: IdentityManager<Adapter>,
    devices: IdentityManager<Device>,
    buffers: IdentityManager<Buffer>,
    bind_groups: IdentityManager<BindGroup>,
    bind_group_layouts: IdentityManager<BindGroupLayout>,
    compute_pipelines: IdentityManager<ComputePipeline>,
    pipeline_layouts: IdentityManager<PipelineLayout>,
    shader_modules: IdentityManager<ShaderModule>,
    command_encoders: IdentityManager<CommandEncoder>,
    textures: IdentityManager<Texture>,
    texture_views: IdentityManager<TextureView>,
    samplers: IdentityManager<Sampler>,
    render_pipelines: IdentityManager<RenderPipeline>,
    render_bundles: IdentityManager<RenderBundle>,
}

impl IdentityHub {
    fn new() -> Self {
        IdentityHub {
            adapters: IdentityManager::new(),
            devices: IdentityManager::new(),
            buffers: IdentityManager::new(),
            bind_groups: IdentityManager::new(),
            bind_group_layouts: IdentityManager::new(),
            compute_pipelines: IdentityManager::new(),
            pipeline_layouts: IdentityManager::new(),
            shader_modules: IdentityManager::new(),
            command_encoders: IdentityManager::new(),
            textures: IdentityManager::new(),
            texture_views: IdentityManager::new(),
            samplers: IdentityManager::new(),
            render_pipelines: IdentityManager::new(),
            render_bundles: IdentityManager::new(),
        }
    }
}

#[derive(Debug)]
pub struct Identities {
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    vk_hub: IdentityHub,
    #[cfg(target_os = "windows")]
    dx12_hub: IdentityHub,
    #[cfg(any(target_os = "ios", target_os = "macos"))]
    metal_hub: IdentityHub,
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    gl_hub: IdentityHub,
    dummy_hub: IdentityHub,
}

impl Identities {
    pub fn new() -> Self {
        Identities {
            #[cfg(any(target_os = "linux", target_os = "windows"))]
            vk_hub: IdentityHub::new(),
            #[cfg(target_os = "windows")]
            dx12_hub: IdentityHub::new(),
            #[cfg(any(target_os = "ios", target_os = "macos"))]
            metal_hub: IdentityHub::new(),
            #[cfg(any(target_os = "linux", target_os = "windows"))]
            gl_hub: IdentityHub::new(),
            dummy_hub: IdentityHub::new(),
        }
    }

    fn select(&mut self, backend: Backend) -> &mut IdentityHub {
        match backend {
            #[cfg(any(target_os = "linux", target_os = "windows"))]
            Backend::Vulkan => &mut self.vk_hub,
            #[cfg(target_os = "windows")]
            Backend::Dx12 => &mut self.dx12_hub,
            #[cfg(any(target_os = "ios", target_os = "macos"))]
            Backend::Metal => &mut self.metal_hub,
            #[cfg(any(target_os = "linux", target_os = "windows"))]
            Backend::Gl => &mut self.gl_hub,
            _ => &mut self.dummy_hub,
        }
    }

    fn hubs(&mut self) -> Vec<(&mut IdentityHub, Backend)> {
        vec![
            #[cfg(any(target_os = "linux", target_os = "windows"))]
            (&mut self.vk_hub, Backend::Vulkan),
            #[cfg(target_os = "windows")]
            (&mut self.dx12_hub, Backend::Dx12),
            #[cfg(any(target_os = "ios", target_os = "macos"))]
            (&mut self.metal_hub, Backend::Metal),
            #[cfg(any(target_os = "linux", target_os = "windows"))]
            (&mut self.gl_hub, Backend::Gl),
            (&mut self.dummy_hub, Backend::Empty),
        ]
    }

    pub fn create_device_id(&mut self, backend: Backend) -> DeviceId {
        self.select(backend).devices.process(backend)
    }

    pub fn kill_device_id(&mut self, id: DeviceId) {
        self.select(id.backend()).devices.free(id);
    }

    pub fn create_adapter_ids(&mut self) -> SmallVec<[AdapterId; 4]> {
        let mut ids = SmallVec::new();
        for hubs in self.hubs() {
            ids.push(hubs.0.adapters.process(hubs.1));
        }
        ids
    }

    pub fn kill_adapter_id(&mut self, id: AdapterId) {
        self.select(id.backend()).adapters.free(id);
    }

    pub fn create_buffer_id(&mut self, backend: Backend) -> BufferId {
        self.select(backend).buffers.process(backend)
    }

    pub fn kill_buffer_id(&mut self, id: BufferId) {
        self.select(id.backend()).buffers.free(id);
    }

    pub fn create_bind_group_id(&mut self, backend: Backend) -> BindGroupId {
        self.select(backend).bind_groups.process(backend)
    }

    pub fn kill_bind_group_id(&mut self, id: BindGroupId) {
        self.select(id.backend()).bind_groups.free(id);
    }

    pub fn create_bind_group_layout_id(&mut self, backend: Backend) -> BindGroupLayoutId {
        self.select(backend).bind_group_layouts.process(backend)
    }

    pub fn kill_bind_group_layout_id(&mut self, id: BindGroupLayoutId) {
        self.select(id.backend()).bind_group_layouts.free(id);
    }

    pub fn create_compute_pipeline_id(&mut self, backend: Backend) -> ComputePipelineId {
        self.select(backend).compute_pipelines.process(backend)
    }

    pub fn kill_compute_pipeline_id(&mut self, id: ComputePipelineId) {
        self.select(id.backend()).compute_pipelines.free(id);
    }

    pub fn create_pipeline_layout_id(&mut self, backend: Backend) -> PipelineLayoutId {
        self.select(backend).pipeline_layouts.process(backend)
    }

    pub fn kill_pipeline_layout_id(&mut self, id: PipelineLayoutId) {
        self.select(id.backend()).pipeline_layouts.free(id);
    }

    pub fn create_shader_module_id(&mut self, backend: Backend) -> ShaderModuleId {
        self.select(backend).shader_modules.process(backend)
    }

    pub fn kill_shader_module_id(&mut self, id: ShaderModuleId) {
        self.select(id.backend()).shader_modules.free(id);
    }

    pub fn create_command_encoder_id(&mut self, backend: Backend) -> CommandEncoderId {
        self.select(backend).command_encoders.process(backend)
    }

    pub fn kill_command_buffer_id(&mut self, id: CommandEncoderId) {
        self.select(id.backend()).command_encoders.free(id);
    }

    pub fn create_sampler_id(&mut self, backend: Backend) -> SamplerId {
        self.select(backend).samplers.process(backend)
    }

    pub fn kill_sampler_id(&mut self, id: SamplerId) {
        self.select(id.backend()).samplers.free(id);
    }

    pub fn create_render_pipeline_id(&mut self, backend: Backend) -> RenderPipelineId {
        self.select(backend).render_pipelines.process(backend)
    }

    pub fn kill_render_pipeline_id(&mut self, id: RenderPipelineId) {
        self.select(id.backend()).render_pipelines.free(id);
    }

    pub fn create_texture_id(&mut self, backend: Backend) -> TextureId {
        self.select(backend).textures.process(backend)
    }

    pub fn kill_texture_id(&mut self, id: TextureId) {
        self.select(id.backend()).textures.free(id);
    }

    pub fn create_texture_view_id(&mut self, backend: Backend) -> TextureViewId {
        self.select(backend).texture_views.process(backend)
    }

    pub fn kill_texture_view_id(&mut self, id: TextureViewId) {
        self.select(id.backend()).texture_views.free(id);
    }

    pub fn create_render_bundle_id(&mut self, backend: Backend) -> RenderBundleId {
        self.select(backend).render_bundles.process(backend)
    }

    pub fn kill_render_bundle_id(&mut self, id: RenderBundleId) {
        self.select(id.backend()).render_bundles.free(id);
    }
}

impl Default for Identities {
    fn default() -> Self {
        Self::new()
    }
}