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
56 changes: 55 additions & 1 deletion gpu/device/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,49 @@ pub fn device() -> Result<Handles, String> {
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<wgpu::Backends>, dx12: Option<(u32, wgpu::DeviceType)>) -> bool {
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<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 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<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 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();
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);
}
Expand Down Expand Up @@ -71,3 +105,23 @@ fn make() -> Result<Handles, String> {
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");
}
}
10 changes: 8 additions & 2 deletions runtime/view/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions systems/k/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ impl Gpu {
pub fn available() -> bool {
static A: std::sync::OnceLock<bool> = 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<Gpu, String> {
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();
Expand Down
Loading