|
1 | 1 | use std::sync::Arc;
|
2 |
| -use wgpu::{Device, Instance, Queue}; |
| 2 | +use wgpu::{Adapter, Backends, Device, Features, Instance, Queue}; |
3 | 3 |
|
4 | 4 | #[derive(Debug, Clone)]
|
5 | 5 | pub struct Context {
|
6 | 6 | pub device: Arc<Device>,
|
7 | 7 | pub queue: Arc<Queue>,
|
8 | 8 | pub instance: Arc<Instance>,
|
9 |
| - pub adapter: Arc<wgpu::Adapter>, |
| 9 | + pub adapter: Arc<Adapter>, |
10 | 10 | }
|
11 | 11 |
|
12 | 12 | impl Context {
|
13 | 13 | pub async fn new() -> Option<Self> {
|
14 |
| - let instance_descriptor = wgpu::InstanceDescriptor { |
15 |
| - backends: wgpu::Backends::all(), |
16 |
| - ..Default::default() |
17 |
| - }; |
18 |
| - let instance = Instance::new(&instance_descriptor); |
| 14 | + ContextBuilder::new().build().await |
| 15 | + } |
| 16 | +} |
19 | 17 |
|
20 |
| - let adapter_options = wgpu::RequestAdapterOptions { |
21 |
| - power_preference: wgpu::PowerPreference::HighPerformance, |
22 |
| - compatible_surface: None, |
23 |
| - force_fallback_adapter: false, |
| 18 | +#[derive(Default)] |
| 19 | +pub struct ContextBuilder { |
| 20 | + backends: Backends, |
| 21 | + features: Features, |
| 22 | +} |
| 23 | +impl ContextBuilder { |
| 24 | + pub fn new() -> Self { |
| 25 | + Self { |
| 26 | + backends: Backends::all(), |
| 27 | + features: Features::empty(), |
| 28 | + } |
| 29 | + } |
| 30 | + pub fn with_backends(mut self, backends: Backends) -> Self { |
| 31 | + self.backends = backends; |
| 32 | + self |
| 33 | + } |
| 34 | + pub fn with_features(mut self, features: Features) -> Self { |
| 35 | + self.features = features; |
| 36 | + self |
| 37 | + } |
| 38 | + pub async fn build(self) -> Option<Context> { |
| 39 | + self.build_with_adapter_selection_inner(None::<WgpuAdapterSelectorFn>).await |
| 40 | + } |
| 41 | + pub async fn build_with_adapter_selection<S: WgpuAdapterSelector>(self, select: S) -> Option<Context> { |
| 42 | + self.build_with_adapter_selection_inner(Some(select)).await |
| 43 | + } |
| 44 | + async fn build_with_adapter_selection_inner<S: WgpuAdapterSelector>(self, select: Option<S>) -> Option<Context> { |
| 45 | + let instance = self.build_instance(); |
| 46 | + |
| 47 | + let selected_adapter = if let Some(select) = select { |
| 48 | + self.select_adapter(&instance, select) |
| 49 | + } else if cfg!(target_os = "windows") { |
| 50 | + self.select_adapter(&instance, |adapters: &mut Vec<Adapter>| { |
| 51 | + adapters.iter().position(|a| a.get_info().backend == wgpu::Backend::Dx12).map(|i| adapters.remove(i)) |
| 52 | + }) |
| 53 | + } else { |
| 54 | + None |
24 | 55 | };
|
25 | 56 |
|
26 |
| - let adapter = instance.request_adapter(&adapter_options).await.ok()?; |
| 57 | + let adapter = if let Some(adapter) = selected_adapter { adapter } else { self.request_adapter(&instance).await? }; |
27 | 58 |
|
28 |
| - Self::new_with_instance_and_adapter(instance, adapter).await |
| 59 | + let (device, queue) = self.request_device(&adapter).await?; |
| 60 | + Some(Context { |
| 61 | + device: Arc::new(device), |
| 62 | + queue: Arc::new(queue), |
| 63 | + adapter: Arc::new(adapter), |
| 64 | + instance: Arc::new(instance), |
| 65 | + }) |
| 66 | + } |
| 67 | + pub async fn available_adapters_fmt(&self) -> impl std::fmt::Display { |
| 68 | + let instance = self.build_instance(); |
| 69 | + let adapters = instance.enumerate_adapters(self.backends); |
| 70 | + AvailableAdaptersFormatter(adapters) |
29 | 71 | }
|
30 | 72 |
|
31 |
| - pub async fn new_with_instance_and_adapter(instance: wgpu::Instance, adapter: wgpu::Adapter) -> Option<Self> { |
32 |
| - let required_limits = adapter.limits(); |
33 |
| - let (device, queue) = adapter |
| 73 | + fn build_instance(&self) -> Instance { |
| 74 | + Instance::new(&wgpu::InstanceDescriptor { |
| 75 | + backends: self.backends, |
| 76 | + ..Default::default() |
| 77 | + }) |
| 78 | + } |
| 79 | + async fn request_adapter(&self, instance: &Instance) -> Option<Adapter> { |
| 80 | + instance |
| 81 | + .request_adapter(&wgpu::RequestAdapterOptions { |
| 82 | + power_preference: wgpu::PowerPreference::HighPerformance, |
| 83 | + compatible_surface: None, |
| 84 | + force_fallback_adapter: false, |
| 85 | + }) |
| 86 | + .await |
| 87 | + .ok() |
| 88 | + } |
| 89 | + fn select_adapter<S: WgpuAdapterSelector>(&self, instance: &Instance, select: S) -> Option<Adapter> { |
| 90 | + select(&mut instance.enumerate_adapters(self.backends)) |
| 91 | + } |
| 92 | + async fn request_device(&self, adapter: &Adapter) -> Option<(Device, Queue)> { |
| 93 | + adapter |
34 | 94 | .request_device(&wgpu::DeviceDescriptor {
|
35 | 95 | label: None,
|
36 |
| - #[cfg(target_family = "wasm")] |
37 |
| - required_features: wgpu::Features::empty(), |
38 |
| - #[cfg(not(target_family = "wasm"))] |
39 |
| - required_features: wgpu::Features::PUSH_CONSTANTS, |
40 |
| - required_limits, |
| 96 | + required_features: self.features, |
| 97 | + required_limits: adapter.limits(), |
41 | 98 | memory_hints: Default::default(),
|
42 | 99 | trace: wgpu::Trace::Off,
|
43 | 100 | })
|
44 | 101 | .await
|
45 |
| - .ok()?; |
| 102 | + .ok() |
| 103 | + } |
| 104 | +} |
46 | 105 |
|
47 |
| - Some(Self { |
48 |
| - device: Arc::new(device), |
49 |
| - queue: Arc::new(queue), |
50 |
| - adapter: Arc::new(adapter), |
51 |
| - instance: Arc::new(instance), |
52 |
| - }) |
| 106 | +pub trait WgpuAdapterSelector: FnOnce(&mut Vec<Adapter>) -> Option<Adapter> {} |
| 107 | +impl<F> WgpuAdapterSelector for F where F: FnOnce(&mut Vec<Adapter>) -> Option<Adapter> {} |
| 108 | +type WgpuAdapterSelectorFn = fn(&mut Vec<Adapter>) -> Option<Adapter>; |
| 109 | + |
| 110 | +struct AvailableAdaptersFormatter(Vec<Adapter>); |
| 111 | +impl std::fmt::Display for AvailableAdaptersFormatter { |
| 112 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 113 | + for (i, adapter) in self.0.iter().enumerate() { |
| 114 | + let info = adapter.get_info(); |
| 115 | + writeln!( |
| 116 | + f, |
| 117 | + "[{}] {:?} {:?} (Name: {}, Driver: {}, Device: {})", |
| 118 | + i, info.backend, info.device_type, info.name, info.driver, info.device, |
| 119 | + )?; |
| 120 | + } |
| 121 | + Ok(()) |
53 | 122 | }
|
54 | 123 | }
|
0 commit comments