Skip to content

Commit 338678a

Browse files
authored
Remove IDs from wgpu traits (#6134)
Remove `wgpu`'s `.global_id()` getters. Implement `PartialEq`, `Eq`, `Hash`, `PartialOrd` and `Ord` for wgpu resources.
1 parent c7e5d07 commit 338678a

31 files changed

+1273
-3592
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ which we're hoping to build performance improvements upon in the future.
5555

5656
By @wumpf in [#6069](https://github.com/gfx-rs/wgpu/pull/6069), [#6099](https://github.com/gfx-rs/wgpu/pull/6099), [#6100](https://github.com/gfx-rs/wgpu/pull/6100).
5757

58+
#### `wgpu`'s resources no longer have `.global_id()` getters
59+
60+
`wgpu-core`'s internals no longer use nor need IDs and we are moving towards removing IDs
61+
completely. This is a step in that direction.
62+
63+
Current users of `.global_id()` are encouraged to make use of the `PartialEq`, `Eq`, `Hash`, `PartialOrd` and `Ord`
64+
traits that have now been implemented for `wgpu` resources.
65+
66+
By @teoxoy [#6134](https://github.com/gfx-rs/wgpu/pull/6134).
67+
5868
### New Features
5969

6070
#### Naga

tests/tests/bind_group_layout_dedup.rs

Lines changed: 55 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -31,129 +31,77 @@ static BIND_GROUP_LAYOUT_DEDUPLICATION: GpuTestConfiguration = GpuTestConfigurat
3131
.run_async(bgl_dedupe);
3232

3333
async fn bgl_dedupe(ctx: TestingContext) {
34-
let entries_1 = &[];
35-
36-
let entries_2 = &[ENTRY];
37-
38-
// Block so we can force all resource to die.
39-
{
40-
let bgl_1a = ctx
41-
.device
42-
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
43-
label: None,
44-
entries: entries_1,
45-
});
46-
47-
let bgl_2 = ctx
48-
.device
49-
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
50-
label: None,
51-
entries: entries_2,
52-
});
53-
54-
let bgl_1b = ctx
55-
.device
56-
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
57-
label: None,
58-
entries: entries_1,
59-
});
60-
61-
let bg_1a = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
34+
let entries = &[];
35+
36+
let bgl_1a = ctx
37+
.device
38+
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
6239
label: None,
63-
layout: &bgl_1a,
64-
entries: &[],
40+
entries,
6541
});
6642

67-
let bg_1b = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
43+
let bgl_1b = ctx
44+
.device
45+
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
6846
label: None,
69-
layout: &bgl_1b,
70-
entries: &[],
47+
entries,
7148
});
7249

73-
let pipeline_layout = ctx
74-
.device
75-
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
76-
label: None,
77-
bind_group_layouts: &[&bgl_1b],
78-
push_constant_ranges: &[],
79-
});
80-
81-
let module = ctx
82-
.device
83-
.create_shader_module(wgpu::ShaderModuleDescriptor {
84-
label: None,
85-
source: wgpu::ShaderSource::Wgsl(SHADER_SRC.into()),
86-
});
87-
88-
let desc = wgpu::ComputePipelineDescriptor {
89-
label: None,
90-
layout: Some(&pipeline_layout),
91-
module: &module,
92-
entry_point: Some("no_resources"),
93-
compilation_options: Default::default(),
94-
cache: None,
95-
};
50+
let bg_1a = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
51+
label: None,
52+
layout: &bgl_1a,
53+
entries: &[],
54+
});
9655

97-
let pipeline = ctx.device.create_compute_pipeline(&desc);
56+
let bg_1b = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
57+
label: None,
58+
layout: &bgl_1b,
59+
entries: &[],
60+
});
9861

99-
let mut encoder = ctx.device.create_command_encoder(&Default::default());
62+
let pipeline_layout = ctx
63+
.device
64+
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
65+
label: None,
66+
bind_group_layouts: &[&bgl_1b],
67+
push_constant_ranges: &[],
68+
});
10069

101-
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
70+
let module = ctx
71+
.device
72+
.create_shader_module(wgpu::ShaderModuleDescriptor {
10273
label: None,
103-
timestamp_writes: None,
74+
source: wgpu::ShaderSource::Wgsl(SHADER_SRC.into()),
10475
});
10576

106-
pass.set_bind_group(0, &bg_1b, &[]);
107-
pass.set_pipeline(&pipeline);
108-
pass.dispatch_workgroups(1, 1, 1);
77+
let desc = wgpu::ComputePipelineDescriptor {
78+
label: None,
79+
layout: Some(&pipeline_layout),
80+
module: &module,
81+
entry_point: Some("no_resources"),
82+
compilation_options: Default::default(),
83+
cache: None,
84+
};
10985

110-
pass.set_bind_group(0, &bg_1a, &[]);
111-
pass.dispatch_workgroups(1, 1, 1);
86+
let pipeline = ctx.device.create_compute_pipeline(&desc);
11287

113-
drop(pass);
88+
let mut encoder = ctx.device.create_command_encoder(&Default::default());
11489

115-
ctx.queue.submit(Some(encoder.finish()));
90+
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
91+
label: None,
92+
timestamp_writes: None,
93+
});
11694

117-
// Abuse the fact that global_id is really just the bitpacked ids when targeting wgpu-core.
118-
if ctx.adapter_info.backend != wgt::Backend::BrowserWebGpu {
119-
let bgl_1a_idx = bgl_1a.global_id().inner() & 0xFFFF_FFFF;
120-
assert_eq!(bgl_1a_idx, 0);
121-
let bgl_2_idx = bgl_2.global_id().inner() & 0xFFFF_FFFF;
122-
assert_eq!(bgl_2_idx, 1);
123-
let bgl_1b_idx = bgl_1b.global_id().inner() & 0xFFFF_FFFF;
124-
assert_eq!(bgl_1b_idx, 2);
125-
}
126-
}
127-
128-
ctx.async_poll(wgpu::Maintain::wait())
129-
.await
130-
.panic_on_timeout();
131-
132-
if ctx.adapter_info.backend != wgt::Backend::BrowserWebGpu {
133-
// Indices are made reusable as soon as the handle is dropped so we keep them around
134-
// for the duration of the loop.
135-
let mut bgls = Vec::new();
136-
let mut indices = Vec::new();
137-
// Now all of the BGL ids should be dead, so we should get the same ids again.
138-
for _ in 0..=2 {
139-
let test_bgl = ctx
140-
.device
141-
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
142-
label: None,
143-
entries: entries_1,
144-
});
145-
146-
let test_bgl_idx = test_bgl.global_id().inner() & 0xFFFF_FFFF;
147-
bgls.push(test_bgl);
148-
indices.push(test_bgl_idx);
149-
}
150-
// We don't guarantee that the IDs will appear in the same order. Sort them
151-
// and check that they all appear exactly once.
152-
indices.sort();
153-
for (i, index) in indices.iter().enumerate() {
154-
assert_eq!(*index, i as u64);
155-
}
156-
}
95+
pass.set_bind_group(0, &bg_1b, &[]);
96+
pass.set_pipeline(&pipeline);
97+
pass.dispatch_workgroups(1, 1, 1);
98+
99+
pass.set_bind_group(0, &bg_1a, &[]);
100+
pass.dispatch_workgroups(1, 1, 1);
101+
102+
drop(pass);
103+
104+
ctx.queue.submit(Some(encoder.finish()));
157105
}
158106

159107
#[gpu_test]

wgpu/src/api/adapter.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{future::Future, sync::Arc, thread};
22

3-
use crate::context::{DeviceRequest, DynContext, ObjectId};
3+
use crate::context::{DeviceRequest, DynContext};
44
use crate::*;
55

66
/// Handle to a physical graphics and/or compute device.
@@ -14,7 +14,6 @@ use crate::*;
1414
#[derive(Debug)]
1515
pub struct Adapter {
1616
pub(crate) context: Arc<C>,
17-
pub(crate) id: ObjectId,
1817
pub(crate) data: Box<Data>,
1918
}
2019
#[cfg(send_sync)]
@@ -23,7 +22,7 @@ static_assertions::assert_impl_all!(Adapter: Send, Sync);
2322
impl Drop for Adapter {
2423
fn drop(&mut self) {
2524
if !thread::panicking() {
26-
self.context.adapter_drop(&self.id, self.data.as_ref())
25+
self.context.adapter_drop(self.data.as_ref())
2726
}
2827
}
2928
}
@@ -40,14 +39,6 @@ pub type RequestAdapterOptions<'a, 'b> = RequestAdapterOptionsBase<&'a Surface<'
4039
static_assertions::assert_impl_all!(RequestAdapterOptions<'_, '_>: Send, Sync);
4140

4241
impl Adapter {
43-
/// Returns a globally-unique identifier for this `Adapter`.
44-
///
45-
/// Calling this method multiple times on the same object will always return the same value.
46-
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
47-
pub fn global_id(&self) -> Id<Self> {
48-
Id::new(self.id)
49-
}
50-
5142
/// Requests a connection to a physical device, creating a logical device.
5243
///
5344
/// Returns the [`Device`] together with a [`Queue`] that executes command buffers.
@@ -80,28 +71,23 @@ impl Adapter {
8071
let context = Arc::clone(&self.context);
8172
let device = DynContext::adapter_request_device(
8273
&*self.context,
83-
&self.id,
8474
self.data.as_ref(),
8575
desc,
8676
trace_path,
8777
);
8878
async move {
8979
device.await.map(
9080
|DeviceRequest {
91-
device_id,
9281
device_data,
93-
queue_id,
9482
queue_data,
9583
}| {
9684
(
9785
Device {
9886
context: Arc::clone(&context),
99-
id: device_id,
10087
data: device_data,
10188
},
10289
Queue {
10390
context,
104-
id: queue_id,
10591
data: queue_data,
10692
},
10793
)
@@ -131,18 +117,21 @@ impl Adapter {
131117
// Part of the safety requirements is that the device was generated from the same adapter.
132118
// Therefore, unwrap is fine here since only WgpuCoreContext based adapters have the ability to create hal devices.
133119
.unwrap()
134-
.create_device_from_hal(&self.id.into(), hal_device, desc, trace_path)
120+
.create_device_from_hal(
121+
crate::context::downcast_ref(&self.data),
122+
hal_device,
123+
desc,
124+
trace_path,
125+
)
135126
}
136127
.map(|(device, queue)| {
137128
(
138129
Device {
139130
context: Arc::clone(&context),
140-
id: device.id().into(),
141131
data: Box::new(device),
142132
},
143133
Queue {
144134
context,
145-
id: queue.id().into(),
146135
data: Box::new(queue),
147136
},
148137
)
@@ -178,7 +167,12 @@ impl Adapter {
178167
.as_any()
179168
.downcast_ref::<crate::backend::ContextWgpuCore>()
180169
{
181-
unsafe { ctx.adapter_as_hal::<A, F, R>(self.id.into(), hal_adapter_callback) }
170+
unsafe {
171+
ctx.adapter_as_hal::<A, F, R>(
172+
crate::context::downcast_ref(&self.data),
173+
hal_adapter_callback,
174+
)
175+
}
182176
} else {
183177
hal_adapter_callback(None)
184178
}
@@ -188,44 +182,37 @@ impl Adapter {
188182
pub fn is_surface_supported(&self, surface: &Surface<'_>) -> bool {
189183
DynContext::adapter_is_surface_supported(
190184
&*self.context,
191-
&self.id,
192185
self.data.as_ref(),
193-
&surface.id,
194186
surface.surface_data.as_ref(),
195187
)
196188
}
197189

198190
/// The features which can be used to create devices on this adapter.
199191
pub fn features(&self) -> Features {
200-
DynContext::adapter_features(&*self.context, &self.id, self.data.as_ref())
192+
DynContext::adapter_features(&*self.context, self.data.as_ref())
201193
}
202194

203195
/// The best limits which can be used to create devices on this adapter.
204196
pub fn limits(&self) -> Limits {
205-
DynContext::adapter_limits(&*self.context, &self.id, self.data.as_ref())
197+
DynContext::adapter_limits(&*self.context, self.data.as_ref())
206198
}
207199

208200
/// Get info about the adapter itself.
209201
pub fn get_info(&self) -> AdapterInfo {
210-
DynContext::adapter_get_info(&*self.context, &self.id, self.data.as_ref())
202+
DynContext::adapter_get_info(&*self.context, self.data.as_ref())
211203
}
212204

213205
/// Get info about the adapter itself.
214206
pub fn get_downlevel_capabilities(&self) -> DownlevelCapabilities {
215-
DynContext::adapter_downlevel_capabilities(&*self.context, &self.id, self.data.as_ref())
207+
DynContext::adapter_downlevel_capabilities(&*self.context, self.data.as_ref())
216208
}
217209

218210
/// Returns the features supported for a given texture format by this adapter.
219211
///
220212
/// Note that the WebGPU spec further restricts the available usages/features.
221213
/// To disable these restrictions on a device, request the [`Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES`] feature.
222214
pub fn get_texture_format_features(&self, format: TextureFormat) -> TextureFormatFeatures {
223-
DynContext::adapter_get_texture_format_features(
224-
&*self.context,
225-
&self.id,
226-
self.data.as_ref(),
227-
format,
228-
)
215+
DynContext::adapter_get_texture_format_features(&*self.context, self.data.as_ref(), format)
229216
}
230217

231218
/// Generates a timestamp using the clock used by the presentation engine.
@@ -250,6 +237,6 @@ impl Adapter {
250237
//
251238
/// [Instant]: std::time::Instant
252239
pub fn get_presentation_timestamp(&self) -> PresentationTimestamp {
253-
DynContext::adapter_get_presentation_timestamp(&*self.context, &self.id, self.data.as_ref())
240+
DynContext::adapter_get_presentation_timestamp(&*self.context, self.data.as_ref())
254241
}
255242
}

0 commit comments

Comments
 (0)