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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/* 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 http://mozilla.org/MPL/2.0/. */

use crate::{
    backend,
    device::Device,
    hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Input, Token},
    id::{AdapterId, DeviceId, SurfaceId},
    power, LifeGuard, PrivateFeatures, Stored, MAX_BIND_GROUPS,
};

use wgt::{Backend, BackendBit, DeviceDescriptor, PowerPreference, BIND_BUFFER_ALIGNMENT};

#[cfg(feature = "replay")]
use serde::Deserialize;
#[cfg(feature = "trace")]
use serde::Serialize;

use hal::{
    adapter::{AdapterInfo as HalAdapterInfo, DeviceType as HalDeviceType, PhysicalDevice as _},
    queue::QueueFamily as _,
    window::Surface as _,
    Instance as _,
};

#[repr(C)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trace", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct RequestAdapterOptions {
    pub power_preference: PowerPreference,
    pub compatible_surface: Option<SurfaceId>,
}

impl Default for RequestAdapterOptions {
    fn default() -> Self {
        RequestAdapterOptions {
            power_preference: PowerPreference::Default,
            compatible_surface: None,
        }
    }
}

#[derive(Debug)]
pub struct Instance {
    #[cfg(any(
        not(any(target_os = "ios", target_os = "macos")),
        feature = "gfx-backend-vulkan"
    ))]
    pub vulkan: Option<gfx_backend_vulkan::Instance>,
    #[cfg(any(target_os = "ios", target_os = "macos"))]
    pub metal: gfx_backend_metal::Instance,
    #[cfg(windows)]
    pub dx12: Option<gfx_backend_dx12::Instance>,
    #[cfg(windows)]
    pub dx11: gfx_backend_dx11::Instance,
}

impl Instance {
    pub fn new(name: &str, version: u32) -> Self {
        Instance {
            #[cfg(any(
                not(any(target_os = "ios", target_os = "macos")),
                feature = "gfx-backend-vulkan"
            ))]
            vulkan: gfx_backend_vulkan::Instance::create(name, version).ok(),
            #[cfg(any(target_os = "ios", target_os = "macos"))]
            metal: gfx_backend_metal::Instance::create(name, version).unwrap(),
            #[cfg(windows)]
            dx12: gfx_backend_dx12::Instance::create(name, version).ok(),
            #[cfg(windows)]
            dx11: gfx_backend_dx11::Instance::create(name, version).unwrap(),
        }
    }

    pub(crate) fn destroy_surface(&mut self, surface: Surface) {
        #[cfg(any(
            not(any(target_os = "ios", target_os = "macos")),
            feature = "gfx-backend-vulkan"
        ))]
        unsafe {
            if let Some(suf) = surface.vulkan {
                self.vulkan.as_mut().unwrap().destroy_surface(suf);
            }
        }
        #[cfg(any(target_os = "ios", target_os = "macos"))]
        unsafe {
            self.metal.destroy_surface(surface.metal);
        }
        #[cfg(windows)]
        unsafe {
            if let Some(suf) = surface.dx12 {
                self.dx12.as_mut().unwrap().destroy_surface(suf);
            }
            self.dx11.destroy_surface(surface.dx11);
        }
    }
}

type GfxSurface<B> = <B as hal::Backend>::Surface;

#[derive(Debug)]
pub struct Surface {
    #[cfg(any(
        not(any(target_os = "ios", target_os = "macos")),
        feature = "gfx-backend-vulkan"
    ))]
    pub vulkan: Option<GfxSurface<backend::Vulkan>>,
    #[cfg(any(target_os = "ios", target_os = "macos"))]
    pub metal: GfxSurface<backend::Metal>,
    #[cfg(windows)]
    pub dx12: Option<GfxSurface<backend::Dx12>>,
    #[cfg(windows)]
    pub dx11: GfxSurface<backend::Dx11>,
}

#[derive(Debug)]
pub struct Adapter<B: hal::Backend> {
    pub(crate) raw: hal::adapter::Adapter<B>,
    extensions: wgt::Extensions,
    limits: wgt::Limits,
    capabilities: wgt::Capabilities,
    unsafe_extensions: wgt::UnsafeExtensions,
    life_guard: LifeGuard,
}

impl<B: hal::Backend> Adapter<B> {
    fn new(raw: hal::adapter::Adapter<B>, unsafe_extensions: wgt::UnsafeExtensions) -> Self {
        let adapter_features = raw.physical_device.features();

        let mut extensions = wgt::Extensions::default() | wgt::Extensions::MAPPABLE_PRIMARY_BUFFERS;
        extensions.set(
            wgt::Extensions::ANISOTROPIC_FILTERING,
            adapter_features.contains(hal::Features::SAMPLER_ANISOTROPY),
        );
        extensions.set(
            wgt::Extensions::BINDING_INDEXING,
            adapter_features.intersects(
                hal::Features::SAMPLED_TEXTURE_DESCRIPTOR_INDEXING
                    | hal::Features::UNSIZED_DESCRIPTOR_ARRAY,
            ),
        );
        if unsafe_extensions.allowed() {
            // Unsafe extensions go here
        }

        let adapter_limits = raw.physical_device.limits();

        let limits = wgt::Limits {
            max_bind_groups: (adapter_limits.max_bound_descriptor_sets as u32)
                .min(MAX_BIND_GROUPS as u32),
            _non_exhaustive: unsafe { wgt::NonExhaustive::new() },
        };

        let mut capabilities = wgt::Capabilities::empty();

        capabilities.set(
            wgt::Capabilities::SAMPLED_TEXTURE_BINDING_ARRAY,
            adapter_features.contains(hal::Features::TEXTURE_DESCRIPTOR_ARRAY),
        );
        capabilities.set(
            wgt::Capabilities::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING,
            adapter_features.contains(hal::Features::SHADER_SAMPLED_IMAGE_ARRAY_DYNAMIC_INDEXING),
        );
        capabilities.set(
            wgt::Capabilities::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
            adapter_features.contains(hal::Features::SAMPLED_TEXTURE_DESCRIPTOR_INDEXING),
        );
        capabilities.set(
            wgt::Capabilities::UNSIZED_BINDING_ARRAY,
            adapter_features.contains(hal::Features::UNSIZED_DESCRIPTOR_ARRAY),
        );

        Adapter {
            raw,
            extensions,
            limits,
            capabilities,
            unsafe_extensions,
            life_guard: LifeGuard::new(),
        }
    }
}

/// Metadata about a backend adapter.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "trace", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct AdapterInfo {
    /// Adapter name
    pub name: String,
    /// Vendor PCI id of the adapter
    pub vendor: usize,
    /// PCI id of the adapter
    pub device: usize,
    /// Type of device
    pub device_type: DeviceType,
    /// Backend used for device
    pub backend: Backend,
}

impl AdapterInfo {
    fn from_gfx(adapter_info: HalAdapterInfo, backend: Backend) -> Self {
        let HalAdapterInfo {
            name,
            vendor,
            device,
            device_type,
        } = adapter_info;

        AdapterInfo {
            name,
            vendor,
            device,
            device_type: device_type.into(),
            backend,
        }
    }
}

/// Supported physical device types
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "trace", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub enum DeviceType {
    /// Other
    Other,
    /// Integrated
    IntegratedGpu,
    /// Discrete
    DiscreteGpu,
    /// Virtual / Hosted
    VirtualGpu,
    /// Cpu / Software Rendering
    Cpu,
}

impl From<HalDeviceType> for DeviceType {
    fn from(device_type: HalDeviceType) -> Self {
        match device_type {
            HalDeviceType::Other => Self::Other,
            HalDeviceType::IntegratedGpu => Self::IntegratedGpu,
            HalDeviceType::DiscreteGpu => Self::DiscreteGpu,
            HalDeviceType::VirtualGpu => Self::VirtualGpu,
            HalDeviceType::Cpu => Self::Cpu,
        }
    }
}

pub enum AdapterInputs<'a, I> {
    IdSet(&'a [I], fn(&I) -> Backend),
    Mask(BackendBit, fn(Backend) -> I),
}

impl<I: Clone> AdapterInputs<'_, I> {
    fn find(&self, b: Backend) -> Option<I> {
        match *self {
            AdapterInputs::IdSet(ids, ref fun) => ids.iter().find(|id| fun(id) == b).cloned(),
            AdapterInputs::Mask(bits, ref fun) => {
                if bits.contains(b.into()) {
                    Some(fun(b))
                } else {
                    None
                }
            }
        }
    }
}

impl<G: GlobalIdentityHandlerFactory> Global<G> {
    #[cfg(feature = "raw-window-handle")]
    pub fn instance_create_surface(
        &self,
        handle: &impl raw_window_handle::HasRawWindowHandle,
        id_in: Input<G, SurfaceId>,
    ) -> SurfaceId {
        let surface = unsafe {
            Surface {
                #[cfg(any(
                    windows,
                    all(unix, not(any(target_os = "ios", target_os = "macos"))),
                    feature = "gfx-backend-vulkan",
                ))]
                vulkan: self
                    .instance
                    .vulkan
                    .as_ref()
                    .and_then(|inst| inst.create_surface(handle).ok()),
                #[cfg(any(target_os = "ios", target_os = "macos"))]
                metal: self.instance.metal.create_surface(handle).unwrap(),
                #[cfg(windows)]
                dx12: self
                    .instance
                    .dx12
                    .as_ref()
                    .and_then(|inst| inst.create_surface(handle).ok()),
                #[cfg(windows)]
                dx11: self.instance.dx11.create_surface(handle).unwrap(),
            }
        };

        let mut token = Token::root();
        self.surfaces.register_identity(id_in, surface, &mut token)
    }

    pub fn enumerate_adapters(
        &self,
        unsafe_extensions: wgt::UnsafeExtensions,
        inputs: AdapterInputs<Input<G, AdapterId>>,
    ) -> Vec<AdapterId> {
        let instance = &self.instance;
        let mut token = Token::root();
        let mut adapters = Vec::new();

        #[cfg(any(
            not(any(target_os = "ios", target_os = "macos")),
            feature = "gfx-backend-vulkan"
        ))]
        {
            if let Some(ref inst) = instance.vulkan {
                if let Some(id_vulkan) = inputs.find(Backend::Vulkan) {
                    for raw in inst.enumerate_adapters() {
                        let adapter = Adapter::new(raw, unsafe_extensions);
                        log::info!("Adapter Vulkan {:?}", adapter.raw.info);
                        adapters.push(backend::Vulkan::hub(self).adapters.register_identity(
                            id_vulkan.clone(),
                            adapter,
                            &mut token,
                        ));
                    }
                }
            }
        }
        #[cfg(any(target_os = "ios", target_os = "macos"))]
        {
            if let Some(id_metal) = inputs.find(Backend::Metal) {
                for raw in instance.metal.enumerate_adapters() {
                    let adapter = Adapter::new(raw, unsafe_extensions);
                    log::info!("Adapter Metal {:?}", adapter.raw.info);
                    adapters.push(backend::Metal::hub(self).adapters.register_identity(
                        id_metal.clone(),
                        adapter,
                        &mut token,
                    ));
                }
            }
        }
        #[cfg(windows)]
        {
            if let Some(ref inst) = instance.dx12 {
                if let Some(id_dx12) = inputs.find(Backend::Dx12) {
                    for raw in inst.enumerate_adapters() {
                        let adapter = Adapter::new(raw, unsafe_extensions);
                        log::info!("Adapter Dx12 {:?}", adapter.raw.info);
                        adapters.push(backend::Dx12::hub(self).adapters.register_identity(
                            id_dx12.clone(),
                            adapter,
                            &mut token,
                        ));
                    }
                }
            }

            if let Some(id_dx11) = inputs.find(Backend::Dx11) {
                for raw in instance.dx11.enumerate_adapters() {
                    let adapter = Adapter::new(raw, unsafe_extensions);
                    log::info!("Adapter Dx11 {:?}", adapter.raw.info);
                    adapters.push(backend::Dx11::hub(self).adapters.register_identity(
                        id_dx11.clone(),
                        adapter,
                        &mut token,
                    ));
                }
            }
        }

        adapters
    }

    pub fn pick_adapter(
        &self,
        desc: &RequestAdapterOptions,
        unsafe_extensions: wgt::UnsafeExtensions,
        inputs: AdapterInputs<Input<G, AdapterId>>,
    ) -> Option<AdapterId> {
        let instance = &self.instance;
        let mut token = Token::root();
        let (surface_guard, mut token) = self.surfaces.read(&mut token);
        let compatible_surface = desc.compatible_surface.map(|id| &surface_guard[id]);
        let mut device_types = Vec::new();

        let id_vulkan = inputs.find(Backend::Vulkan);
        let id_metal = inputs.find(Backend::Metal);
        let id_dx12 = inputs.find(Backend::Dx12);
        let id_dx11 = inputs.find(Backend::Dx11);

        #[cfg(any(
            not(any(target_os = "ios", target_os = "macos")),
            feature = "gfx-backend-vulkan"
        ))]
        let mut adapters_vk = match instance.vulkan {
            Some(ref inst) if id_vulkan.is_some() => {
                let mut adapters = inst.enumerate_adapters();
                if let Some(&Surface {
                    vulkan: Some(ref surface),
                    ..
                }) = compatible_surface
                {
                    adapters.retain(|a| {
                        a.queue_families
                            .iter()
                            .find(|qf| qf.queue_type().supports_graphics())
                            .map_or(false, |qf| surface.supports_queue_family(qf))
                    });
                }
                device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone()));
                adapters
            }
            _ => Vec::new(),
        };
        #[cfg(any(target_os = "ios", target_os = "macos"))]
        let mut adapters_mtl = if id_metal.is_some() {
            let mut adapters = instance.metal.enumerate_adapters();
            if let Some(surface) = compatible_surface {
                adapters.retain(|a| {
                    a.queue_families
                        .iter()
                        .find(|qf| qf.queue_type().supports_graphics())
                        .map_or(false, |qf| surface.metal.supports_queue_family(qf))
                });
            }
            device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone()));
            adapters
        } else {
            Vec::new()
        };
        #[cfg(windows)]
        let mut adapters_dx12 = match instance.dx12 {
            Some(ref inst) if id_dx12.is_some() => {
                let mut adapters = inst.enumerate_adapters();
                if let Some(&Surface {
                    dx12: Some(ref surface),
                    ..
                }) = compatible_surface
                {
                    adapters.retain(|a| {
                        a.queue_families
                            .iter()
                            .find(|qf| qf.queue_type().supports_graphics())
                            .map_or(false, |qf| surface.supports_queue_family(qf))
                    });
                }
                device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone()));
                adapters
            }
            _ => Vec::new(),
        };
        #[cfg(windows)]
        let mut adapters_dx11 = if id_dx11.is_some() {
            let mut adapters = instance.dx11.enumerate_adapters();
            if let Some(surface) = compatible_surface {
                adapters.retain(|a| {
                    a.queue_families
                        .iter()
                        .find(|qf| qf.queue_type().supports_graphics())
                        .map_or(false, |qf| surface.dx11.supports_queue_family(qf))
                });
            }
            device_types.extend(adapters.iter().map(|ad| ad.info.device_type.clone()));
            adapters
        } else {
            Vec::new()
        };

        if device_types.is_empty() {
            log::warn!("No adapters are available!");
            return None;
        }

        let (mut integrated, mut discrete, mut virt, mut other) = (None, None, None, None);

        for (i, ty) in device_types.into_iter().enumerate() {
            match ty {
                hal::adapter::DeviceType::IntegratedGpu => {
                    integrated = integrated.or(Some(i));
                }
                hal::adapter::DeviceType::DiscreteGpu => {
                    discrete = discrete.or(Some(i));
                }
                hal::adapter::DeviceType::VirtualGpu => {
                    virt = virt.or(Some(i));
                }
                _ => {
                    other = other.or(Some(i));
                }
            }
        }

        let preferred_gpu = match desc.power_preference {
            PowerPreference::Default => match power::is_battery_discharging() {
                Ok(false) => discrete.or(integrated).or(other).or(virt),
                Ok(true) => integrated.or(discrete).or(other).or(virt),
                Err(err) => {
                    log::debug!(
                        "Power info unavailable, preferring integrated gpu ({})",
                        err
                    );
                    integrated.or(discrete).or(other).or(virt)
                }
            },
            PowerPreference::LowPower => integrated.or(other).or(discrete).or(virt),
            PowerPreference::HighPerformance => discrete.or(other).or(integrated).or(virt),
        };

        let mut selected = preferred_gpu.unwrap_or(0);
        #[cfg(any(
            not(any(target_os = "ios", target_os = "macos")),
            feature = "gfx-backend-vulkan"
        ))]
        {
            if selected < adapters_vk.len() {
                let adapter = Adapter::new(adapters_vk.swap_remove(selected), unsafe_extensions);
                log::info!("Adapter Vulkan {:?}", adapter.raw.info);
                let id = backend::Vulkan::hub(self).adapters.register_identity(
                    id_vulkan.unwrap(),
                    adapter,
                    &mut token,
                );
                return Some(id);
            }
            selected -= adapters_vk.len();
        }
        #[cfg(any(target_os = "ios", target_os = "macos"))]
        {
            if selected < adapters_mtl.len() {
                let adapter = Adapter::new(adapters_mtl.swap_remove(selected), unsafe_extensions);
                log::info!("Adapter Metal {:?}", adapter.raw.info);
                let id = backend::Metal::hub(self).adapters.register_identity(
                    id_metal.unwrap(),
                    adapter,
                    &mut token,
                );
                return Some(id);
            }
            selected -= adapters_mtl.len();
        }
        #[cfg(windows)]
        {
            if selected < adapters_dx12.len() {
                let adapter = Adapter::new(adapters_dx12.swap_remove(selected), unsafe_extensions);
                log::info!("Adapter Dx12 {:?}", adapter.raw.info);
                let id = backend::Dx12::hub(self).adapters.register_identity(
                    id_dx12.unwrap(),
                    adapter,
                    &mut token,
                );
                return Some(id);
            }
            selected -= adapters_dx12.len();
            if selected < adapters_dx11.len() {
                let adapter = Adapter::new(adapters_dx11.swap_remove(selected), unsafe_extensions);
                log::info!("Adapter Dx11 {:?}", adapter.raw.info);
                let id = backend::Dx11::hub(self).adapters.register_identity(
                    id_dx11.unwrap(),
                    adapter,
                    &mut token,
                );
                return Some(id);
            }
            selected -= adapters_dx11.len();
        }

        let _ = (selected, id_vulkan, id_metal, id_dx12, id_dx11);
        log::warn!("Some adapters are present, but enumerating them failed!");
        None
    }

    pub fn adapter_get_info<B: GfxBackend>(&self, adapter_id: AdapterId) -> AdapterInfo {
        let hub = B::hub(self);
        let mut token = Token::root();
        let (adapter_guard, _) = hub.adapters.read(&mut token);
        let adapter = &adapter_guard[adapter_id];
        AdapterInfo::from_gfx(adapter.raw.info.clone(), adapter_id.backend())
    }

    pub fn adapter_extensions<B: GfxBackend>(&self, adapter_id: AdapterId) -> wgt::Extensions {
        let hub = B::hub(self);
        let mut token = Token::root();
        let (adapter_guard, _) = hub.adapters.read(&mut token);
        let adapter = &adapter_guard[adapter_id];

        adapter.extensions
    }

    pub fn adapter_limits<B: GfxBackend>(&self, adapter_id: AdapterId) -> wgt::Limits {
        let hub = B::hub(self);
        let mut token = Token::root();
        let (adapter_guard, _) = hub.adapters.read(&mut token);
        let adapter = &adapter_guard[adapter_id];

        adapter.limits.clone()
    }

    pub fn adapter_capabilities<B: GfxBackend>(&self, adapter_id: AdapterId) -> wgt::Capabilities {
        let hub = B::hub(self);
        let mut token = Token::root();
        let (adapter_guard, _) = hub.adapters.read(&mut token);
        let adapter = &adapter_guard[adapter_id];

        adapter.capabilities
    }

    pub fn adapter_destroy<B: GfxBackend>(&self, adapter_id: AdapterId) {
        let hub = B::hub(self);
        let mut token = Token::root();
        let (mut guard, _) = hub.adapters.write(&mut token);

        if guard[adapter_id]
            .life_guard
            .ref_count
            .take()
            .unwrap()
            .load()
            == 1
        {
            hub.adapters.free_id(adapter_id);
            let _adapter = guard.remove(adapter_id).unwrap();
        }
    }
}

impl<G: GlobalIdentityHandlerFactory> Global<G> {
    pub fn adapter_request_device<B: GfxBackend>(
        &self,
        adapter_id: AdapterId,
        desc: &DeviceDescriptor,
        trace_path: Option<&std::path::Path>,
        id_in: Input<G, DeviceId>,
    ) -> DeviceId {
        let hub = B::hub(self);
        let mut token = Token::root();
        let device = {
            let (adapter_guard, _) = hub.adapters.read(&mut token);
            let adapter = &adapter_guard[adapter_id];
            let phd = &adapter.raw.physical_device;

            // Verify all extensions were exposed by the adapter
            if !adapter.unsafe_extensions.allowed() {
                assert!(
                    !desc.extensions.intersects(wgt::Extensions::ALL_UNSAFE),
                    "Cannot enable unsafe extensions without passing UnsafeExtensions::allow() when getting an adapter. Enabled unsafe extensions: {:?}",
                    desc.extensions & wgt::Extensions::ALL_UNSAFE
                )
            }
            assert!(
                adapter.extensions.contains(desc.extensions),
                "Cannot enable extensions that adapter doesn't support. Unsupported extensions: {:?}",
                desc.extensions - adapter.extensions
            );

            // Verify extension preconditions
            if desc
                .extensions
                .contains(wgt::Extensions::MAPPABLE_PRIMARY_BUFFERS)
                && adapter.raw.info.device_type == hal::adapter::DeviceType::DiscreteGpu
            {
                log::warn!("Extension MAPPABLE_PRIMARY_BUFFERS enabled on a discrete gpu. This is a massive performance footgun and likely not what you wanted");
            }

            let available_features = adapter.raw.physical_device.features();

            // Check features that are always needed
            let wishful_features = hal::Features::VERTEX_STORES_AND_ATOMICS
                | hal::Features::FRAGMENT_STORES_AND_ATOMICS
                | hal::Features::NDC_Y_UP;
            let mut enabled_features = available_features & wishful_features;
            if enabled_features != wishful_features {
                log::warn!(
                    "Missing features: {:?}",
                    wishful_features - enabled_features
                );
            }

            // Extensions
            enabled_features.set(
                hal::Features::SAMPLER_ANISOTROPY,
                desc.extensions
                    .contains(wgt::Extensions::ANISOTROPIC_FILTERING),
            );

            let mut enabled_capabilities = adapter.capabilities & wgt::Capabilities::ALL_BUILT_IN;

            // Capabilities without extension gates
            enabled_features.set(
                hal::Features::TEXTURE_DESCRIPTOR_ARRAY,
                adapter
                    .capabilities
                    .contains(wgt::Capabilities::SAMPLED_TEXTURE_BINDING_ARRAY),
            );
            enabled_features.set(
                hal::Features::SHADER_SAMPLED_IMAGE_ARRAY_DYNAMIC_INDEXING,
                adapter
                    .capabilities
                    .contains(wgt::Capabilities::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING),
            );

            // Capabilities behind BINDING_INDEXING
            if desc.extensions.contains(wgt::Extensions::BINDING_INDEXING) {
                enabled_capabilities
                    .insert(adapter.capabilities & wgt::Capabilities::ALL_BINDING_INDEXING);
                enabled_features.set(
                    hal::Features::SAMPLED_TEXTURE_DESCRIPTOR_INDEXING,
                    adapter
                        .capabilities
                        .contains(wgt::Capabilities::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING),
                );
                enabled_features.set(
                    hal::Features::UNSIZED_DESCRIPTOR_ARRAY,
                    adapter
                        .capabilities
                        .contains(wgt::Capabilities::UNSIZED_BINDING_ARRAY),
                );
            }

            let family = adapter
                .raw
                .queue_families
                .iter()
                .find(|family| family.queue_type().supports_graphics())
                .unwrap();
            let mut gpu = unsafe { phd.open(&[(family, &[1.0])], enabled_features).unwrap() };

            let limits = phd.limits();
            assert_eq!(
                0,
                BIND_BUFFER_ALIGNMENT % limits.min_storage_buffer_offset_alignment,
                "Adapter storage buffer offset alignment not compatible with WGPU"
            );
            assert_eq!(
                0,
                BIND_BUFFER_ALIGNMENT % limits.min_uniform_buffer_offset_alignment,
                "Adapter uniform buffer offset alignment not compatible with WGPU"
            );
            if limits.max_bound_descriptor_sets == 0 {
                log::warn!("max_bind_groups limit is missing");
            } else {
                assert!(
                    u32::from(limits.max_bound_descriptor_sets) >= desc.limits.max_bind_groups,
                    "Adapter does not support the requested max_bind_groups"
                );
            }

            let mem_props = phd.memory_properties();
            if !desc.shader_validation {
                log::warn!("Shader validation is disabled");
            }
            let private_features = PrivateFeatures {
                shader_validation: desc.shader_validation,
                texture_d24_s8: phd
                    .format_properties(Some(hal::format::Format::D24UnormS8Uint))
                    .optimal_tiling
                    .contains(hal::format::ImageFeature::DEPTH_STENCIL_ATTACHMENT),
            };

            Device::new(
                gpu.device,
                Stored {
                    value: adapter_id,
                    ref_count: adapter.life_guard.add_ref(),
                },
                gpu.queue_groups.swap_remove(0),
                mem_props,
                limits,
                private_features,
                desc,
                enabled_capabilities,
                trace_path,
            )
        };

        hub.devices.register_identity(id_in, device, &mut token)
    }
}