From 6ee9a06f6727cae29c8ffe60d385292d151d8191 Mon Sep 17 00:00:00 2001 From: shinyobjectz Date: Wed, 18 Nov 2026 09:45:46 -0800 Subject: [PATCH 1/2] gpu/device: an Intel GPU on Windows runs on DX12, never meeting its Vulkan driver raea 0.1.7 crashed on a Windows 10 PC's Intel integrated graphics under Vulkan, and ran with WGPU_BACKEND=dx12. Asked for every backend, wgpu takes Vulkan's adapter for the same GPU. With no WGPU_BACKEND on Windows, DX12's pick is asked first now, and kept when it's Intel's own hardware (vendor 0x8086, not WARP); any other pick is dropped and every backend asked as before. NVIDIA and AMD, a PC with no GPU, a Mac or Linux, and a WGPU_BACKEND someone set (Windows' release check sets vulkan, for lavapipe) keep wgpu's own pick (dx12_first, tested). The page window, the stage and renders all take this device (runtime/view render_creation); only its fallbacks (Bevy's own device, systems/k when the shared device is off) don't. --- gpu/device/src/lib.rs | 71 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/gpu/device/src/lib.rs b/gpu/device/src/lib.rs index 9cad9430..ace4eee2 100644 --- a/gpu/device/src/lib.rs +++ b/gpu/device/src/lib.rs @@ -31,21 +31,56 @@ pub fn device() -> Result { SHARED.get_or_init(make).clone() } +/// Intel's PCI vendor id, as DXGI and Vulkan report it. +const INTEL: u32 = 0x8086; + +/// Whether the shared device runs on DX12 alone: on Windows, when WGPU_BACKEND isn't set and the GPU DX12 picks +/// (`dx12`: its vendor and kind) is Intel's own hardware (WARP, Windows' software DirectX, is a Cpu). raea 0.1.7 crashed +/// on an Intel integrated GPU under Vulkan on a Windows 10 PC, and ran with WGPU_BACKEND=dx12; asked for every backend, +/// wgpu takes Vulkan's adapter for the same GPU. NVIDIA and AMD, a PC with no GPU, a Mac or Linux, and a WGPU_BACKEND +/// someone set (Windows' check sets vulkan, for lavapipe) keep wgpu's own pick. +pub fn dx12_first(windows: bool, env: Option, dx12: Option<(u32, wgpu::DeviceType)>) -> bool { + windows && env.is_none() && matches!(dx12, Some((INTEL, kind)) if kind != wgpu::DeviceType::Cpu) +} + fn make() -> Result { // wgpu's indirect-draw validation off in release (it stays in debug builds; RAEA_WGPU_INDIRECT_VALIDATION=1 puts it // back): it checks each indirect draw's arguments in compute passes before drawing, and the indirect draws are // Bevy's own, written by its mesh preprocessing on this device. On arena3d it was ~7 extra passes and their blits a // frame, ~0.07 ms of GPU (lightspeed F2); pixels are the same. - // wgpu's own variables are read (WGPU_BACKEND, WGPU_DX12_COMPILER, WGPU_VALIDATION, ...; nothing changes when unset): + // wgpu's own variables are read (WGPU_BACKEND, WGPU_DX12_COMPILER, WGPU_VALIDATION, ...; nothing changes when unset, + // but for an Intel GPU on Windows, which goes on DX12: dx12_first): // Windows' check renders on Mesa's lavapipe with WGPU_BACKEND=vulkan, because WARP crashes compiling one of Bevy's // compute shaders on GitHub's GPU-less runner (d3d10warp ComputeShaderTransformer, PT20) - let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env(); - if !cfg!(debug_assertions) && std::env::var("RAEA_WGPU_INDIRECT_VALIDATION").as_deref() != Ok("1") { - desc.flags.remove(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL); - } - let instance = wgpu::Instance::new(desc); - let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() })) - .map_err(|e| format!("no GPU adapter: {e}"))?; + let instance_on = |backends: Option| { + let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env(); + if let Some(b) = backends { + desc.backends = b; + } + if !cfg!(debug_assertions) && std::env::var("RAEA_WGPU_INDIRECT_VALIDATION").as_deref() != Ok("1") { + desc.flags.remove(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL); + } + wgpu::Instance::new(desc) + }; + let options = wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() }; + // on Windows with no WGPU_BACKEND, DX12's pick is asked first, so an Intel GPU never meets its Vulkan driver + // (dx12_first); any other pick is dropped and every backend asked as before + let env = wgpu::Backends::from_env(); + let dx12 = if cfg!(windows) && env.is_none() { + let i = instance_on(Some(wgpu::Backends::DX12)); + let a = pollster::block_on(i.request_adapter(&options)).ok(); + a.map(|a| (i, a)) + } else { + None + }; + let (instance, adapter) = match dx12 { + Some((i, a)) if dx12_first(cfg!(windows), env, Some((a.get_info().vendor, a.get_info().device_type))) => (i, a), + _ => { + let i = instance_on(None); + let a = pollster::block_on(i.request_adapter(&options)).map_err(|e| format!("no GPU adapter: {e}"))?; + (i, a) + } + }; let mut features = adapter.features(); if adapter.get_info().device_type == wgpu::DeviceType::DiscreteGpu { features.remove(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS); @@ -71,3 +106,23 @@ fn make() -> Result { telemetry::watch(&device, "quads"); Ok((instance, adapter, device, queue)) } + +#[cfg(test)] +mod tests { + use super::*; + use wgpu::{Backends, DeviceType}; + + // an Intel GPU on Windows goes on DX12 unless WGPU_BACKEND says otherwise; every other GPU and system keeps wgpu's pick + #[test] + fn intel_on_windows_runs_on_dx12() { + assert!(dx12_first(true, None, Some((INTEL, DeviceType::IntegratedGpu))), "Intel's integrated GPU"); + assert!(dx12_first(true, None, Some((INTEL, DeviceType::DiscreteGpu))), "Intel Arc"); + assert!(!dx12_first(true, Some(Backends::VULKAN), Some((INTEL, DeviceType::IntegratedGpu))), "WGPU_BACKEND=vulkan is kept"); + assert!(!dx12_first(true, Some(Backends::DX12), Some((INTEL, DeviceType::IntegratedGpu))), "WGPU_BACKEND set: wgpu's own read of it"); + assert!(!dx12_first(true, None, Some((0x10de, DeviceType::DiscreteGpu))), "NVIDIA keeps wgpu's pick"); + assert!(!dx12_first(true, None, Some((0x1002, DeviceType::IntegratedGpu))), "AMD keeps wgpu's pick"); + assert!(!dx12_first(true, None, Some((0x1414, DeviceType::Cpu))), "WARP alone: wgpu's pick (lavapipe, when it's there)"); + assert!(!dx12_first(true, None, None), "no DX12 adapter"); + assert!(!dx12_first(false, None, Some((INTEL, DeviceType::IntegratedGpu))), "an Intel Mac or Linux"); + } +} From 61b832fcba56600277605e650495dbbf48894263 Mon Sep 17 00:00:00 2001 From: shinyobjectz Date: Wed, 18 Nov 2026 10:03:25 -0800 Subject: [PATCH 2/2] gpu: one backend rule for every wgpu instance, so Bevy's own device and the kernels take DX12 on an Intel GPU too #180's first commit chose DX12 for an Intel GPU only in the shared device. Bevy's own device (when the shared one fails) and systems/k's kernels asked wgpu for every backend, so they still met the Intel Vulkan driver that crashed 0.1.7 on Windows. raea_gpu_device::backends() is now the one answer for all three: WGPU_BACKEND when set; else, on Windows, DX12 alone when DX12's pick is Intel's own GPU (dx12_first); else every backend, wgpu's default. Found once a process. On a Mac nothing changes: the sample room renders at 10.0-10.1 ms a frame warm, against main's 10.1-10.5. --- gpu/device/src/lib.rs | 57 +++++++++++++++++++------------------- runtime/view/src/device.rs | 10 +++++-- systems/k/src/lib.rs | 4 +-- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/gpu/device/src/lib.rs b/gpu/device/src/lib.rs index ace4eee2..894dedc6 100644 --- a/gpu/device/src/lib.rs +++ b/gpu/device/src/lib.rs @@ -43,6 +43,26 @@ pub fn dx12_first(windows: bool, env: Option, dx12: Option<(u32, windows && env.is_none() && matches!(dx12, Some((INTEL, kind)) if kind != wgpu::DeviceType::Cpu) } +/// The backends every wgpu instance of raea's asks for (the shared device's, Bevy's own when it isn't there, systems/k's +/// kernels): WGPU_BACKEND when it's set; else, on Windows, DX12 alone when DX12's pick is an Intel GPU (dx12_first); +/// else every backend, wgpu's own default. Found once a process: on Windows it asks DX12 for its adapter first. +pub fn backends() -> wgpu::Backends { + static B: std::sync::OnceLock = std::sync::OnceLock::new(); + *B.get_or_init(|| { + let env = wgpu::Backends::from_env(); + if let Some(b) = env { + return b; + } + let dx12 = cfg!(windows).then(|| { + let i = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: wgpu::Backends::DX12, ..wgpu::InstanceDescriptor::new_without_display_handle() }); + pollster::block_on(i.request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() })) + .ok() + .map(|a| (a.get_info().vendor, a.get_info().device_type)) + }); + if dx12_first(cfg!(windows), env, dx12.flatten()) { wgpu::Backends::DX12 } else { wgpu::Backends::all() } + }) +} + fn make() -> Result { // wgpu's indirect-draw validation off in release (it stays in debug builds; RAEA_WGPU_INDIRECT_VALIDATION=1 puts it // back): it checks each indirect draw's arguments in compute passes before drawing, and the indirect draws are @@ -52,35 +72,14 @@ fn make() -> Result { // but for an Intel GPU on Windows, which goes on DX12: dx12_first): // Windows' check renders on Mesa's lavapipe with WGPU_BACKEND=vulkan, because WARP crashes compiling one of Bevy's // compute shaders on GitHub's GPU-less runner (d3d10warp ComputeShaderTransformer, PT20) - let instance_on = |backends: Option| { - let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env(); - if let Some(b) = backends { - desc.backends = b; - } - if !cfg!(debug_assertions) && std::env::var("RAEA_WGPU_INDIRECT_VALIDATION").as_deref() != Ok("1") { - desc.flags.remove(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL); - } - wgpu::Instance::new(desc) - }; - let options = wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() }; - // on Windows with no WGPU_BACKEND, DX12's pick is asked first, so an Intel GPU never meets its Vulkan driver - // (dx12_first); any other pick is dropped and every backend asked as before - let env = wgpu::Backends::from_env(); - let dx12 = if cfg!(windows) && env.is_none() { - let i = instance_on(Some(wgpu::Backends::DX12)); - let a = pollster::block_on(i.request_adapter(&options)).ok(); - a.map(|a| (i, a)) - } else { - None - }; - let (instance, adapter) = match dx12 { - Some((i, a)) if dx12_first(cfg!(windows), env, Some((a.get_info().vendor, a.get_info().device_type))) => (i, a), - _ => { - let i = instance_on(None); - let a = pollster::block_on(i.request_adapter(&options)).map_err(|e| format!("no GPU adapter: {e}"))?; - (i, a) - } - }; + let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env(); + desc.backends = backends(); + if !cfg!(debug_assertions) && std::env::var("RAEA_WGPU_INDIRECT_VALIDATION").as_deref() != Ok("1") { + desc.flags.remove(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL); + } + let instance = wgpu::Instance::new(desc); + let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() })) + .map_err(|e| format!("no GPU adapter: {e}"))?; let mut features = adapter.features(); if adapter.get_info().device_type == wgpu::DeviceType::DiscreteGpu { features.remove(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS); diff --git a/runtime/view/src/device.rs b/runtime/view/src/device.rs index 562fa1c0..8ea855e7 100644 --- a/runtime/view/src/device.rs +++ b/runtime/view/src/device.rs @@ -16,14 +16,20 @@ pub fn render_creation() -> bevy::render::settings::RenderCreation { RenderInstance::new(instance), ) } + // Bevy's own device asks for the backends raea's would (WGPU_BACKEND, else DX12 for an Intel GPU on Windows) Some(Err(e)) => { eprintln!("raea: the shared GPU device isn't there ({e}); Bevy makes its own"); - Default::default() + own() } - None => Default::default(), + None => own(), } } +/// Bevy's renderer making its own device, on the backends raea_gpu_device::backends says +fn own() -> bevy::render::settings::RenderCreation { + bevy::render::settings::RenderCreation::Automatic(Box::new(bevy::render::settings::WgpuSettings { backends: Some(raea_gpu_device::backends()), ..Default::default() })) +} + /// Set by a render that walks its frames in order (raea-render's render): the pool's budget drops to 32 MiB. pub static LINEAR_RENDER: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); diff --git a/systems/k/src/lib.rs b/systems/k/src/lib.rs index 11d006ef..33f44d15 100644 --- a/systems/k/src/lib.rs +++ b/systems/k/src/lib.rs @@ -43,14 +43,14 @@ impl Gpu { pub fn available() -> bool { static A: std::sync::OnceLock = std::sync::OnceLock::new(); *A.get_or_init(|| { - let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: raea_gpu_device::backends(), ..wgpu::InstanceDescriptor::new_without_display_handle() }); pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() })) .is_ok_and(|a| a.get_info().device_type != wgpu::DeviceType::Cpu) }) } pub fn new() -> Result { - let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: raea_gpu_device::backends(), ..wgpu::InstanceDescriptor::new_without_display_handle() }); let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() })) .map_err(|e| format!("no GPU adapter: {e}"))?; let info = adapter.get_info();