ferrotensor_backend/
context.rs1use crate::pool::BufferPool;
4use std::sync::Arc;
5use wgpu::{Adapter, Device, Instance, Queue};
6
7#[derive(Debug)]
9pub struct WgpuContext {
10 pub instance: Instance,
12 pub adapter: Adapter,
14 pub device: Arc<Device>,
16 pub queue: Arc<Queue>,
18 pub pool: Arc<BufferPool>,
20}
21
22impl WgpuContext {
23 pub async fn new() -> Self {
28 Self::new_with_options(wgpu::PowerPreference::HighPerformance, false).await
29 }
30
31 pub async fn new_with_options(
37 power_preference: wgpu::PowerPreference,
38 force_fallback_adapter: bool,
39 ) -> Self {
40 let instance = Instance::default();
41 let adapter = instance
42 .request_adapter(&wgpu::RequestAdapterOptions {
43 power_preference,
44 force_fallback_adapter,
45 compatible_surface: None,
46 })
47 .await
48 .expect("Failed to find adapter");
49
50 let (device, queue) = adapter
51 .request_device(
52 &wgpu::DeviceDescriptor {
53 label: Some("Ferrotensor Device"),
54 required_features: wgpu::Features::empty(),
55 required_limits: adapter.limits(),
59 },
60 None,
61 )
62 .await
63 .expect("Failed to create device");
64 let pool = Arc::new(BufferPool::new(
65 wgpu::BufferUsages::STORAGE
66 | wgpu::BufferUsages::COPY_DST
67 | wgpu::BufferUsages::COPY_SRC,
68 ));
69 Self {
70 instance,
71 adapter,
72 device: Arc::new(device),
73 queue: Arc::new(queue),
74 pool,
75 }
76 }
77
78 pub fn adapter_info(&self) -> String {
80 let info = self.adapter.get_info();
81 format!("{} ({:?})", info.name, info.backend)
82 }
83}