1use async_lock::OnceCell;
2use wgpu::{Adapter, Backends, Instance, InstanceDescriptor};
3
4static RUNTIME: OnceCell<GpuRuntime> = OnceCell::new();
5
6pub struct GpuRuntime {
7 instance: Instance,
8 adapters: Vec<Adapter>,
9}
10
11impl GpuRuntime {
12 pub async fn shared() -> &'static Self {
13 RUNTIME
14 .get_or_init(|| async {
15 let instance = Instance::new(InstanceDescriptor {
16 backends: Backends::all(),
17 ..InstanceDescriptor::new_without_display_handle()
18 });
19 let adapters = instance.enumerate_adapters(Backends::all()).await;
20 Self { instance, adapters }
21 })
22 .await
23 }
24
25 pub fn instance(&self) -> &Instance {
26 &self.instance
27 }
28
29 pub(crate) fn adapters(&self, backends: Backends) -> Vec<Adapter> {
30 self.adapters
31 .iter()
32 .filter(|adapter| backends.contains(adapter.get_info().backend.into()))
33 .cloned()
34 .collect()
35 }
36}