Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
98 changes: 65 additions & 33 deletions gpu/device/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,64 @@ pub fn device() -> Result<Handles, String> {
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<wgpu::Backends>, 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<wgpu::Backends>, 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<wgpu::Backends> = 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 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<Handles, String> {
// 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);
Expand Down Expand Up @@ -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");
}
}
2 changes: 1 addition & 1 deletion runtime/view/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading