1use anyhow::Context as _;
2use blade_graphics as gpu;
3use std::sync::Arc;
4use util::ResultExt;
5
6#[cfg_attr(target_os = "macos", derive(Clone))]
7pub struct BladeContext {
8 pub(super) gpu: Arc<gpu::Context>,
9}
10
11impl BladeContext {
12 pub fn new() -> anyhow::Result<Self> {
13 let device_id_forced = match std::env::var("ZED_DEVICE_ID") {
14 Ok(val) => val
15 .parse()
16 .context("Failed to parse device ID from `ZED_DEVICE_ID` environment variable")
17 .log_err(),
18 Err(std::env::VarError::NotPresent) => None,
19 err => {
20 err.context("Failed to read value of `ZED_DEVICE_ID` environment variable")
21 .log_err();
22 None
23 }
24 };
25 let gpu = Arc::new(
26 unsafe {
27 gpu::Context::init(gpu::ContextDesc {
28 presentation: true,
29 validation: false,
30 device_id: device_id_forced.unwrap_or(0),
31 ..Default::default()
32 })
33 }
34 .map_err(|e| anyhow::anyhow!("{:?}", e))?,
35 );
36 Ok(Self { gpu })
37 }
38}