Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize_adapter_from_env_or_default ignores backend_bits if WGPU_ADAPTER_NAME is not set #3792

Closed
kurtschelfthout opened this issue May 21, 2023 · 1 comment
Labels
area: api Issues related to API surface help required We need community help to make this happen. type: bug Something isn't working

Comments

@kurtschelfthout
Copy link

kurtschelfthout commented May 21, 2023

Description
initialize_adapter_from_env_or_default ignores WGPU_BACKEND if WGPU_ADAPTER_NAME is not set.

Repro steps

        let instance = wgpu::Instance::default();

        let backends = wgpu::util::backend_bits_from_env().unwrap_or_else(wgpu::Backends::all);
        println!("backends: {backends:?}");
        let adapters = instance.enumerate_adapters(backends);

        for adapter in adapters {
            let info = adapter.get_info();
            println!("adapter: {:?}", info);
        }

        let adapter = wgpu::util::initialize_adapter_from_env_or_default(&instance, backends, None)
            .await
            .expect("No suitable GPU adapters found on the system!");
        let info = adapter.get_info();
        println!(
            "Using {:#?} {} with {:#?} backend",
            info.device_type, info.name, info.backend
        );

I have an Intel and a discrete Nvidia GPU. When I run this with env variables like:

WGPU_POWER_PREF="high"
WGPU_BACKEND = "DX12"

It prints:

backends: Backends(DX12)
adapter: AdapterInfo { name: "NVIDIA GeForce RTX 2070 with Max-Q Design", vendor: 4318, device: 7956, device_type: DiscreteGpu, driver: "", driver_info: "", backend: Dx12 }
adapter: AdapterInfo { name: "Intel(R) UHD Graphics", vendor: 32902, device: 39876, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Dx12 }
adapter: AdapterInfo { name: "Microsoft Basic Render Driver", vendor: 5140, device: 140, device_type: Cpu, driver: "", driver_info: "", backend: Dx12 }     
Using DiscreteGpu NVIDIA GeForce RTX 2070 with Max-Q Design with Vulkan backend (Env vars: WGPU_POWER_PREF (low|high), WGPU_ADAPTER_NAME, WGPU_BACKEND).  

Expected vs observed behavior
Despite setting the backend to DX12, wgpu uses Vulkan.

Extra materials
This happens because initialize_adapter_from_env_or_default calls initialize_adapter_from_env, which returns None if WGPU_ADAPTER_NAME is not set:

pub fn initialize_adapter_from_env(instance: &Instance, backend_bits: Backends) -> Option<Adapter> {
    let desired_adapter_name = std::env::var("WGPU_ADAPTER_NAME")
        .as_deref()
        .map(str::to_lowercase)
        .ok()?;
   // snip

Now initialize_adapter_from_env_or_default ignores the backend (but does respect power preference):

pub async fn initialize_adapter_from_env_or_default(
    instance: &Instance,
    backend_bits: wgt::Backends,
    compatible_surface: Option<&Surface>,
) -> Option<Adapter> {
    match initialize_adapter_from_env(instance, backend_bits) {
        Some(a) => Some(a),
        None => {
            instance
                .request_adapter(&RequestAdapterOptions {
                    power_preference: power_preference_from_env().unwrap_or_default(),
                    force_fallback_adapter: false,
                    compatible_surface,
                })
                .await
        }
    }
}

Platform
Windows 10, wgpu 0.16.

@teoxoy teoxoy changed the title initialize_adapter_from_env_or_default ignores WGPU_BACKEND if WGPU_ADAPTER_NAME is not set initialize_adapter_from_env_or_default ignores backend_bits if WGPU_ADAPTER_NAME is not set May 25, 2023
@teoxoy teoxoy added type: bug Something isn't working help required We need community help to make this happen. area: api Issues related to API surface labels May 25, 2023
@Wumpf
Copy link
Member

Wumpf commented Jul 5, 2023

Resolved by #3904

@Wumpf Wumpf closed this as completed Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Issues related to API surface help required We need community help to make this happen. type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants