Skip to main content

script_webgpu/
identityhub.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use webgpu_traits::IdentityManager;
6use webgpu_traits::id::*;
7use webgpu_traits::markers::*;
8
9#[derive(Debug)]
10pub struct IdentityHub {
11    adapters: IdentityManager<Adapter>,
12    devices: IdentityManager<Device>,
13    queues: IdentityManager<Queue>,
14    buffers: IdentityManager<Buffer>,
15    bind_groups: IdentityManager<BindGroup>,
16    bind_group_layouts: IdentityManager<BindGroupLayout>,
17    compute_pipelines: IdentityManager<ComputePipeline>,
18    pipeline_layouts: IdentityManager<PipelineLayout>,
19    shader_modules: IdentityManager<ShaderModule>,
20    command_encoders: IdentityManager<CommandEncoder>,
21    command_buffers: IdentityManager<CommandBuffer>,
22    textures: IdentityManager<Texture>,
23    texture_views: IdentityManager<TextureView>,
24    samplers: IdentityManager<Sampler>,
25    render_pipelines: IdentityManager<RenderPipeline>,
26    render_bundles: IdentityManager<RenderBundle>,
27    compute_passes: IdentityManager<ComputePassEncoder>,
28    render_passes: IdentityManager<RenderPassEncoder>,
29    query_sets: IdentityManager<QuerySet>,
30    external_textures: IdentityManager<ExternalTexture>,
31    render_bundle_encoders: IdentityManager<RenderBundleEncoder>,
32}
33
34impl Default for IdentityHub {
35    fn default() -> Self {
36        IdentityHub {
37            adapters: IdentityManager::new(),
38            devices: IdentityManager::new(),
39            queues: IdentityManager::new(),
40            buffers: IdentityManager::new(),
41            bind_groups: IdentityManager::new(),
42            bind_group_layouts: IdentityManager::new(),
43            compute_pipelines: IdentityManager::new(),
44            pipeline_layouts: IdentityManager::new(),
45            shader_modules: IdentityManager::new(),
46            command_encoders: IdentityManager::new(),
47            command_buffers: IdentityManager::new(),
48            textures: IdentityManager::new(),
49            texture_views: IdentityManager::new(),
50            samplers: IdentityManager::new(),
51            render_pipelines: IdentityManager::new(),
52            render_bundles: IdentityManager::new(),
53            compute_passes: IdentityManager::new(),
54            render_passes: IdentityManager::new(),
55            query_sets: IdentityManager::new(),
56            external_textures: IdentityManager::new(),
57            render_bundle_encoders: IdentityManager::new(),
58        }
59    }
60}
61
62impl IdentityHub {
63    pub fn create_device_id(&self) -> DeviceId {
64        self.devices.process()
65    }
66
67    pub fn free_device_id(&self, id: DeviceId) {
68        self.devices.free(id);
69    }
70
71    pub fn create_queue_id(&self) -> QueueId {
72        self.queues.process()
73    }
74
75    #[expect(unused)]
76    fn free_queue_id(&self, id: QueueId) {
77        self.queues.free(id);
78    }
79
80    pub fn create_adapter_id(&self) -> AdapterId {
81        self.adapters.process()
82    }
83
84    pub fn free_adapter_id(&self, id: AdapterId) {
85        self.adapters.free(id);
86    }
87
88    pub fn create_buffer_id(&self) -> BufferId {
89        self.buffers.process()
90    }
91
92    pub fn free_buffer_id(&self, id: BufferId) {
93        self.buffers.free(id);
94    }
95
96    pub fn create_bind_group_id(&self) -> BindGroupId {
97        self.bind_groups.process()
98    }
99
100    pub fn free_bind_group_id(&self, id: BindGroupId) {
101        self.bind_groups.free(id);
102    }
103
104    pub fn create_bind_group_layout_id(&self) -> BindGroupLayoutId {
105        self.bind_group_layouts.process()
106    }
107
108    pub fn free_bind_group_layout_id(&self, id: BindGroupLayoutId) {
109        self.bind_group_layouts.free(id);
110    }
111
112    pub fn create_compute_pipeline_id(&self) -> ComputePipelineId {
113        self.compute_pipelines.process()
114    }
115
116    pub fn free_compute_pipeline_id(&self, id: ComputePipelineId) {
117        self.compute_pipelines.free(id);
118    }
119
120    pub fn create_pipeline_layout_id(&self) -> PipelineLayoutId {
121        self.pipeline_layouts.process()
122    }
123
124    pub fn free_pipeline_layout_id(&self, id: PipelineLayoutId) {
125        self.pipeline_layouts.free(id);
126    }
127
128    pub fn create_shader_module_id(&self) -> ShaderModuleId {
129        self.shader_modules.process()
130    }
131
132    pub fn free_shader_module_id(&self, id: ShaderModuleId) {
133        self.shader_modules.free(id);
134    }
135
136    pub fn create_command_encoder_id(&self) -> CommandEncoderId {
137        self.command_encoders.process()
138    }
139
140    #[expect(unused)]
141    fn free_command_encoder_id(&self, id: CommandEncoderId) {
142        self.command_encoders.free(id);
143    }
144
145    pub fn create_command_buffer_id(&self) -> CommandBufferId {
146        self.command_buffers.process()
147    }
148
149    pub fn free_command_buffer_id(&self, id: CommandBufferId) {
150        self.command_buffers.free(id);
151    }
152
153    pub fn create_sampler_id(&self) -> SamplerId {
154        self.samplers.process()
155    }
156
157    pub fn free_sampler_id(&self, id: SamplerId) {
158        self.samplers.free(id);
159    }
160
161    pub fn create_render_pipeline_id(&self) -> RenderPipelineId {
162        self.render_pipelines.process()
163    }
164
165    pub fn free_render_pipeline_id(&self, id: RenderPipelineId) {
166        self.render_pipelines.free(id);
167    }
168
169    pub fn create_texture_id(&self) -> TextureId {
170        self.textures.process()
171    }
172
173    pub fn free_texture_id(&self, id: TextureId) {
174        self.textures.free(id);
175    }
176
177    pub fn create_texture_view_id(&self) -> TextureViewId {
178        self.texture_views.process()
179    }
180
181    pub fn free_texture_view_id(&self, id: TextureViewId) {
182        self.texture_views.free(id);
183    }
184
185    pub fn create_render_bundle_id(&self) -> RenderBundleId {
186        self.render_bundles.process()
187    }
188
189    pub fn free_render_bundle_id(&self, id: RenderBundleId) {
190        self.render_bundles.free(id);
191    }
192
193    pub fn create_compute_pass_id(&self) -> ComputePassEncoderId {
194        self.compute_passes.process()
195    }
196
197    pub fn free_compute_pass_id(&self, id: ComputePassEncoderId) {
198        self.compute_passes.free(id);
199    }
200
201    pub fn create_render_pass_id(&self) -> RenderPassEncoderId {
202        self.render_passes.process()
203    }
204
205    pub fn free_render_pass_id(&self, id: RenderPassEncoderId) {
206        self.render_passes.free(id);
207    }
208
209    pub fn create_query_set_id(&self) -> QuerySetId {
210        self.query_sets.process()
211    }
212
213    #[expect(unused)]
214    fn free_query_set_id(&self, id: QuerySetId) {
215        self.query_sets.free(id);
216    }
217
218    pub fn create_external_texture_id(&self) -> ExternalTextureId {
219        self.external_textures.process()
220    }
221
222    #[expect(unused)]
223    fn free_external_texture_id(&self, id: ExternalTextureId) {
224        self.external_textures.free(id);
225    }
226
227    pub fn create_render_bundle_encoder_id(&self) -> RenderBundleEncoderId {
228        self.render_bundle_encoders.process()
229    }
230
231    #[expect(unused)]
232    fn free_render_bundle_encoder_id(&self, id: RenderBundleEncoderId) {
233        self.render_bundle_encoders.free(id);
234    }
235}