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