Skip to main content

dawn_wgpu/
lib.rs

1#![allow(unexpected_cfgs, dead_code, unreachable_patterns)]
2
3mod backend;
4mod compat;
5mod dispatch;
6mod error;
7mod future;
8mod mapping;
9mod types;
10
11pub use compat::{DawnIntoWgpu, DawnTextureIntoWgpu};
12pub use types::*;
13
14#[cfg(feature = "wire")]
15pub mod wire_backend;
16
17#[cfg(not(feature = "wire"))]
18pub fn create_instance(desc: &wgpu::InstanceDescriptor) -> wgpu::Instance {
19    use wgpu::custom::InstanceInterface;
20    let custom = types::DawnInstance::new(desc);
21    wgpu::Instance::from_custom(custom)
22}
23
24#[cfg(feature = "wire")]
25pub fn create_instance(_desc: &wgpu::InstanceDescriptor) -> wgpu::Instance {
26    let socket_name = std::env::var("DAWN_WGPU_WIRE_SOCKET")
27        .unwrap_or_else(|_| "dawn-wgpu-wire-default".to_string());
28    let connect_attempts = std::env::var("DAWN_WGPU_WIRE_CONNECT_ATTEMPTS")
29        .ok()
30        .and_then(|v| v.parse::<usize>().ok())
31        .unwrap_or(5);
32    let connect_delay_ms = std::env::var("DAWN_WGPU_WIRE_CONNECT_DELAY_MS")
33        .ok()
34        .and_then(|v| v.parse::<u64>().ok())
35        .unwrap_or(10);
36    let opts = wire_backend::WireInitOptions {
37        reserve_surface: true,
38        connect_attempts,
39        connect_delay: std::time::Duration::from_millis(connect_delay_ms),
40    };
41    create_wire_instance_with_options(&socket_name, opts).unwrap_or_else(|err| {
42        panic!("wire feature enabled: failed to create wire instance (socket={socket_name}): {err}")
43    })
44}
45
46#[cfg(feature = "wire")]
47fn create_wire_instance_with_options(
48    name: &str,
49    opts: wire_backend::WireInitOptions,
50) -> Result<wgpu::Instance, wire_backend::WireBackendError> {
51    let backend = wire_backend::IpcWireBackend::connect_name_with_options(name, opts)?;
52    let (instance, handle) = backend.into_instance_and_handle();
53    let custom = types::DawnInstance::from_factory(move || instance, Some(handle));
54    Ok(wgpu::Instance::from_custom(custom))
55}