From 0013c2eac20a9b91a6167db11ef6cd5026408fe3 Mon Sep 17 00:00:00 2001 From: shinyobjectz Date: Wed, 18 Nov 2026 17:49:56 -0800 Subject: [PATCH] Windows: an AMD GPU runs on DX12 too (#155), and DX12 draws meshes right #155: raea's app crashed before its window in AMD's OpenGL driver (atio6axx.dll, 0xc0000005) on a laptop with an AMD 780M beside an RTX 5060, under wgpu's every-backend default and under Vulkan, and ran with WGPU_BACKEND=dx12. #180 put an Intel GPU on DX12, but looked only at DX12's high-performance pick, which on that laptop is the NVIDIA GPU. Now dx12_first looks at every GPU DX12 lists: an Intel or AMD one (not WARP) puts the process on DX12, whichever GPU it then draws on. A PC with only NVIDIA GPUs, a Mac, Linux and a WGPU_BACKEND someone set keep wgpu's own pick. Testing that on the RTX 4070 (the laptop's pick) found DX12 drawing meshes wrong: a box and a sphere came out as one mangled shape. The shared device drops wgpu's indirect-draw validation in release builds, and on DX12 that pass is what writes each indirect draw's first vertex and first instance (wgpu-core's D3D12 special constants). With it on, the same frame matches Vulkan's. So the validation now stays wherever DX12 can be the backend (on Windows, unless WGPU_BACKEND leaves DX12 out); Mac, Linux and a Vulkan-only Windows process keep the speed-up. Without this, 0.1.8's #180 would have drawn every Intel GPU's 3D mangled. Known cost, unchanged here: DX12 compiles shaders with FXC (raea ships no dxcompiler.dll; DXC 1.9.2609 fails on bevy_hanabi's shaders), so a 3D render's first frame on DX12 took 22 s on the 4070 against Vulkan's 8 s. docs/settings.md gets a WGPU_BACKEND row (what raea picks, and the override). --- docs/settings.md | 1 + gpu/device/src/lib.rs | 98 +++++++++++++++++++++++++------------- runtime/view/src/device.rs | 2 +- 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 8087cb90..e3138f76 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -48,6 +48,7 @@ libraries (pianos, General MIDI instruments, orchestral and percussion samples, | Setting | What | |---|---| +| `WGPU_BACKEND` | The GPU driver API raea draws with: `dx12`, `vulkan`, `gl` (or several, comma-separated). Unset, raea picks: on Windows, DirectX 12 when the PC has an Intel or AMD GPU (also beside an NVIDIA one: their OpenGL and Vulkan drivers crashed raea there), else wgpu's own pick, which is Vulkan on an NVIDIA-only PC. Set it when the window or a render fails on a GPU, e.g. `WGPU_BACKEND=dx12` | | `RAEA_VELLO_BUMP=full` | Pages are drawn with Vello's own buffer sizes from the first frame, instead of raea's smaller ones (about 100 MB less GPU memory). A page past raea's sizes is found and drawn again at Vello's anyway, with a line on stderr, so this only saves that redraw | | `RAEA_PAGE_DRAWN_CHECK=0` | Pages are drawn without checking that Vello drew them. For measuring what the check costs (about 2% in one test): a page past Vello's buffers then shows the frame before it again, or nothing | | `RAEA_NOT_APPLIED=0` | A page's CSS is parsed without noting what stylo drops, so a render ends without its NOTE of what the page didn't apply (and its manifest's `not_applied` lists only loads that failed). For measuring what the note costs | diff --git a/gpu/device/src/lib.rs b/gpu/device/src/lib.rs index 894dedc6..03f59c79 100644 --- a/gpu/device/src/lib.rs +++ b/gpu/device/src/lib.rs @@ -31,21 +31,25 @@ pub fn device() -> Result { SHARED.get_or_init(make).clone() } -/// Intel's PCI vendor id, as DXGI and Vulkan report it. +/// Intel's and AMD's PCI vendor ids, as DXGI and Vulkan report them. const INTEL: u32 = 0x8086; +const AMD: u32 = 0x1002; -/// 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) +/// Whether the shared device runs on DX12 alone: on Windows, when WGPU_BACKEND isn't set and DX12 lists Intel's or +/// AMD's own hardware among its GPUs (`dx12`: each one's vendor and kind; WARP, Windows' software DirectX, is a Cpu), +/// whichever GPU it then picks. raea 0.1.7 crashed on an Intel integrated GPU under Vulkan on a Windows 10 PC, and +/// 0.1.3's app crashed before its window in AMD's OpenGL driver (atio6axx.dll, 0xc0000005) on a laptop with an AMD +/// 780M beside an RTX 5060, under wgpu's every-backend default and under Vulkan alike (#155); both ran with +/// WGPU_BACKEND=dx12. On that laptop DX12's high-performance pick is the NVIDIA GPU, so the rule looks at every GPU, +/// not the pick. A PC with only NVIDIA's GPUs, 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: &[(u32, wgpu::DeviceType)]) -> bool { + windows && env.is_none() && dx12.iter().any(|&(vendor, kind)| matches!(vendor, INTEL | AMD) && 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. +/// kernels): WGPU_BACKEND when it's set; else, on Windows, DX12 alone when DX12 lists an Intel or AMD GPU (dx12_first); +/// else every backend, wgpu's own default. Found once a process: on Windows it asks DX12 for its adapters first. pub fn backends() -> wgpu::Backends { static B: std::sync::OnceLock = std::sync::OnceLock::new(); *B.get_or_init(|| { @@ -53,28 +57,38 @@ pub fn backends() -> wgpu::Backends { if let Some(b) = env { return b; } - let dx12 = cfg!(windows).then(|| { + let dx12: Vec<(u32, wgpu::DeviceType)> = if cfg!(windows) { 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() } + pollster::block_on(i.enumerate_adapters(wgpu::Backends::DX12)).iter().map(|a| (a.get_info().vendor, a.get_info().device_type)).collect() + } else { + Vec::new() + }; + if dx12_first(cfg!(windows), env, &dx12) { wgpu::Backends::DX12 } else { wgpu::Backends::all() } }) } +/// Whether the shared device drops wgpu's indirect-draw validation: in a release build, unless +/// RAEA_WGPU_INDIRECT_VALIDATION=1 or the device can be DX12's. On DX12 that pass is also what writes each indirect +/// draw's first vertex and first instance (wgpu-core's D3D12 special constants), so without it Bevy's meshes are drawn +/// with each other's data: a box and a sphere came out as one mangled shape on an RTX 4070 under DX12, and whole with +/// the pass on (11-19), and DX12 is what an Intel or AMD GPU on Windows runs on (dx12_first). +pub fn indirect_validation_off(debug: bool, windows: bool, backends: wgpu::Backends, env: Option<&str>) -> bool { + !debug && env != Some("1") && !(windows && backends.contains(wgpu::Backends::DX12)) +} + 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 indirect-draw validation off in release (it stays in debug builds and wherever DX12 can be the backend, + // which needs it: indirect_validation_off; 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 on Metal and Vulkan. // 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): + // but for an Intel or AMD 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(); desc.backends = backends(); - if !cfg!(debug_assertions) && std::env::var("RAEA_WGPU_INDIRECT_VALIDATION").as_deref() != Ok("1") { + if indirect_validation_off(cfg!(debug_assertions), cfg!(windows), desc.backends, std::env::var("RAEA_WGPU_INDIRECT_VALIDATION").ok().as_deref()) { desc.flags.remove(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL); } let instance = wgpu::Instance::new(desc); @@ -111,17 +125,35 @@ 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 + // the indirect-draw validation stays wherever DX12 can draw: it writes D3D12's first vertex and first instance + #[test] + fn dx12_keeps_indirect_validation() { + let (all, dx12, vk) = (Backends::all(), Backends::DX12, Backends::VULKAN); + assert!(!indirect_validation_off(false, true, dx12, None), "Windows on DX12 (an Intel or AMD GPU)"); + assert!(!indirect_validation_off(false, true, all, None), "Windows on every backend: DX12 may be the pick"); + assert!(indirect_validation_off(false, true, vk, None), "Windows with WGPU_BACKEND=vulkan"); + assert!(indirect_validation_off(false, false, all, None), "a Mac or Linux release"); + assert!(!indirect_validation_off(false, false, all, Some("1")), "RAEA_WGPU_INDIRECT_VALIDATION=1"); + assert!(!indirect_validation_off(true, false, all, None), "a debug build"); + } + + // an Intel or AMD GPU on Windows goes on DX12 unless WGPU_BACKEND says otherwise; NVIDIA alone and every other system + // keep 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"); + fn intel_or_amd_on_windows_runs_on_dx12() { + let (igpu, dgpu, warp) = (DeviceType::IntegratedGpu, DeviceType::DiscreteGpu, (0x1414, DeviceType::Cpu)); + assert!(dx12_first(true, None, &[(INTEL, igpu), warp]), "Intel's integrated GPU"); + assert!(dx12_first(true, None, &[(INTEL, dgpu)]), "Intel Arc"); + assert!(dx12_first(true, None, &[(AMD, igpu), (0x10de, dgpu), warp]), "#155: an AMD 780M beside an RTX 5060 (DX12 picks the NVIDIA)"); + assert!(dx12_first(true, None, &[(0x10de, dgpu), (AMD, igpu)]), "the same laptop listed NVIDIA first"); + assert!(dx12_first(true, None, &[(AMD, dgpu)]), "an AMD Radeon card"); + assert!(dx12_first(true, None, &[(INTEL, igpu), (0x10de, dgpu)]), "an Intel laptop with an NVIDIA GPU"); + assert!(!dx12_first(true, Some(Backends::VULKAN), &[(INTEL, igpu)]), "WGPU_BACKEND=vulkan is kept"); + assert!(!dx12_first(true, Some(Backends::DX12), &[(AMD, igpu)]), "WGPU_BACKEND set: wgpu's own read of it"); + assert!(!dx12_first(true, None, &[(0x10de, dgpu), warp]), "NVIDIA alone keeps wgpu's pick"); + assert!(!dx12_first(true, None, &[warp]), "WARP alone: wgpu's pick (lavapipe, when it's there)"); + assert!(!dx12_first(true, None, &[]), "no DX12 adapter"); + assert!(!dx12_first(false, None, &[(INTEL, igpu)]), "an Intel Mac or Linux"); + assert!(!dx12_first(false, None, &[(AMD, dgpu)]), "an AMD GPU on Linux"); } } diff --git a/runtime/view/src/device.rs b/runtime/view/src/device.rs index 8ea855e7..ab5b6047 100644 --- a/runtime/view/src/device.rs +++ b/runtime/view/src/device.rs @@ -16,7 +16,7 @@ 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) + // Bevy's own device asks for the backends raea's would (WGPU_BACKEND, else DX12 for an Intel or AMD GPU on Windows) Some(Err(e)) => { eprintln!("raea: the shared GPU device isn't there ({e}); Bevy makes its own"); own()