Skip to main content

dynamis_gpu/
context.rs

1use wgpu::{Adapter, Device, Instance, InstanceDescriptor, MemoryHints, PowerPreference, Queue};
2
3pub struct GpuContext {
4    adapter: Adapter,
5    device: Device,
6    queue: Queue,
7}
8
9impl GpuContext {
10    pub async fn new() -> Self {
11        let instance = Instance::new(InstanceDescriptor::new_without_display_handle());
12        let adapter = instance
13            .request_adapter(&wgpu::RequestAdapterOptions {
14                power_preference: PowerPreference::HighPerformance,
15                force_fallback_adapter: false,
16                compatible_surface: None,
17                ..Default::default()
18            })
19            .await
20            .expect("no compatible GPU adapter found");
21        let (device, queue) = adapter
22            .request_device(&wgpu::DeviceDescriptor {
23                label: Some("dynamis device"),
24                required_features: wgpu::Features::empty(),
25                required_limits: wgpu::Limits::default(),
26                memory_hints: MemoryHints::default(),
27                ..Default::default()
28            })
29            .await
30            .expect("failed to create GPU device");
31        Self {
32            adapter,
33            device,
34            queue,
35        }
36    }
37
38    pub fn device(&self) -> &Device {
39        &self.device
40    }
41
42    pub fn queue(&self) -> &Queue {
43        &self.queue
44    }
45
46    pub fn adapter_info(&self) -> wgpu::AdapterInfo {
47        self.adapter.get_info()
48    }
49}