Skip to main content

dynamis_gpu/
context.rs

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