Skip to main content

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