GPU path tracer: run on Metal on macOS (#7183) (#7185)

* claim: #7183

* GPU path tracer runs on Metal on macOS, not only Vulkan (#3217)
This commit is contained in:
Andrzej Błasik
2026-10-29 13:58:49 +00:00
committed by GitHub
parent 6fdd58dec6
commit cb05e1e0f7
7 changed files with 50 additions and 47 deletions
+4 -4
View File
@@ -144,10 +144,10 @@ fn context() -> Option<Arc<Context>> {
let (_, device, queue) = match open_headless() {
Ok(v) => v,
Err(e) => {
// The path tracer runs on Vulkan only: where the backends in use leave Vulkan out
// (macOS, or `WGPU_BACKEND=dx12`), it is unavailable by design, not missing.
let vulkan = crate::select::native_backends().contains(wgpu::Backends::VULKAN);
assert!(!vulkan || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required GPU unavailable: {e}");
// The path tracer runs on Vulkan or Metal only: where the backends in use leave both out
// (`WGPU_BACKEND=dx12`), it is unavailable by design, not missing.
let available = !crate::select::path_backends().is_empty();
assert!(!available || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required GPU unavailable: {e}");
return None;
}
};
+13 -13
View File
@@ -92,24 +92,24 @@ pub fn host() -> Result<ImageHost> {
/// Open an independent job device on the viewport's selected adapter. In particular,
/// software fallback and safe mode must not rediscover a failing hardware driver.
/// An explicit incompatible SOLIDCRAFT_GPU policy leaves this executor unavailable.
/// The path tracer runs on Vulkan only. On Windows it must keep the viewport's backend:
/// opening Vulkan drivers that startup avoided can crash inside native driver code.
/// Elsewhere a viewport on another backend is reopened on the same GPU through Vulkan.
/// Without a usable Vulkan adapter the executor is unavailable; CPU image methods remain usable.
/// The path tracer runs on Vulkan, or Metal on macOS. On Windows it must keep the viewport's
/// backend: opening Vulkan drivers that startup avoided can crash inside native driver code.
/// Elsewhere a viewport on another backend is reopened on the same GPU through one of them.
/// Without a usable adapter the executor is unavailable; CPU image methods remain usable.
pub fn host_on_adapter(adapter: &wgpu::Adapter) -> Result<ImageHost> {
let policy = crate::select::Policy::from_env().map_err(gpu)?;
let info = adapter.get_info();
if !viewport_path_backends(info.backend, cfg!(target_os = "windows")).contains(wgpu::Backends::VULKAN) {
if !viewport_path_backends(info.backend, cfg!(target_os = "windows")).intersects(crate::select::path_backends()) {
return Err(gpu(format!("{VULKAN_ONLY}; the viewport uses {:?}", info.backend)));
}
if !policy.admits(crate::select::tier_of(info.device_type)) {
return Err(gpu(format!("viewport adapter does not satisfy SOLIDCRAFT_GPU={}", policy.name())));
}
if info.backend != wgpu::Backend::Vulkan {
let (vulkan, _) = crate::select::pick_vulkan(policy, |i| i.vendor == info.vendor && i.device == info.device)
if !matches!(info.backend, wgpu::Backend::Vulkan | wgpu::Backend::Metal) {
let (picked, _) = crate::select::pick_path(policy, |i| i.vendor == info.vendor && i.device == info.device)
.ok_or_else(|| gpu(format!("{VULKAN_ONLY}; the viewport uses {:?}", info.backend)))?;
let (device, queue) = open_device(&vulkan)?;
log::info!("gpu-path viewport GPU: {} through Vulkan", info.name);
let (device, queue) = open_device(&picked)?;
log::info!("gpu-path viewport GPU: {} through Vulkan or Metal", info.name);
return ImageHost::with_executor(Arc::new(Executor::new(device, queue)?));
}
let (device, queue) = open_device(adapter)?;
@@ -118,11 +118,11 @@ pub fn host_on_adapter(adapter: &wgpu::Adapter) -> Result<ImageHost> {
}
// D3D12 shader compilation is avoided; the user can select a CPU image method instead.
const VULKAN_ONLY: &str = "the GPU path tracer requires a Vulkan driver; select a CPU image method when Vulkan is unavailable";
const VULKAN_ONLY: &str = "the GPU path tracer requires a Vulkan driver (Metal on macOS); select a CPU image method when none is available";
/// Backends a viewport's path device may open, before any driver is loaded.
fn viewport_path_backends(backend: wgpu::Backend, windows: bool) -> wgpu::Backends {
if windows { wgpu::Backends::from(backend) } else { wgpu::Backends::VULKAN }
if windows { wgpu::Backends::from(backend) } else { crate::select::path_backends() }
}
// Path resources need six storage buffers (geometry, BVH, accumulation, HDRI,
@@ -155,8 +155,8 @@ fn open_device(adapter: &wgpu::Adapter) -> Result<(wgpu::Device, wgpu::Queue)> {
}
fn open_headless() -> Result<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
let policy = crate::select::Policy::from_env().map_err(gpu)?;
let (adapter, _) = crate::select::pick_vulkan(policy, |_| true)
.ok_or_else(|| gpu(format!("{VULKAN_ONLY}; no Vulkan adapter satisfies SOLIDCRAFT_GPU={}", policy.name())))?;
let (adapter, _) = crate::select::pick_path(policy, |_| true)
.ok_or_else(|| gpu(format!("{VULKAN_ONLY}; no Vulkan or Metal adapter satisfies SOLIDCRAFT_GPU={}", policy.name())))?;
let (device, queue) = open_device(&adapter)?;
Ok((adapter, device, queue))
}
+15 -17
View File
@@ -6,10 +6,10 @@ fn executor() -> Option<Executor> {
let (adapter, device, queue) = match open_headless() {
Ok(device) => device,
Err(e) => {
// The path tracer runs on Vulkan only: where the backends in use leave Vulkan out
// (macOS, or `WGPU_BACKEND=dx12`), it is unavailable by design, not missing.
let vulkan = crate::select::native_backends().contains(wgpu::Backends::VULKAN);
assert!(!vulkan || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required GPU unavailable: {e}");
// The path tracer runs on Vulkan or Metal only: where the backends in use leave both out
// (`WGPU_BACKEND=dx12`), it is unavailable by design, not missing.
let available = !crate::select::path_backends().is_empty();
assert!(!available || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required GPU unavailable: {e}");
eprintln!("gpu-path device test skipped: {e}");
return None;
}
@@ -538,16 +538,14 @@ fn render_lights_path_visibility_uniform_keeps_the_shader_contract() {
assert!(request.studio_weights.is_none());
}
/// #3217: the path tracer opens Vulkan adapters only, and none where the platform has no
/// Vulkan backend (macOS).
/// #3217: the path tracer opens Vulkan adapters, or Metal on macOS, never D3D12 or GL.
#[test]
fn path_devices_are_vulkan_only() {
let picked = crate::select::pick_vulkan(crate::select::Policy::Auto, |_| true);
if let Some((adapter, report)) = &picked {
assert_eq!(adapter.get_info().backend, wgpu::Backend::Vulkan);
assert_eq!(report.backend, "Vulkan");
fn path_devices_are_vulkan_or_metal_only() {
let picked = crate::select::pick_path(crate::select::Policy::Auto, |_| true);
if let Some((adapter, _)) = &picked {
assert!(matches!(adapter.get_info().backend, wgpu::Backend::Vulkan | wgpu::Backend::Metal));
}
if cfg!(target_os = "macos") || !crate::select::native_backends().contains(wgpu::Backends::VULKAN) {
if crate::select::path_backends().is_empty() {
assert!(picked.is_none());
}
}
@@ -564,7 +562,7 @@ fn viewport_path_backends_keep_windows_on_its_selected_backend() {
#[test]
fn viewport_path_backends_preserve_other_platforms() {
for backend in [wgpu::Backend::Vulkan, wgpu::Backend::Gl, wgpu::Backend::Metal] {
assert_eq!(viewport_path_backends(backend, false), wgpu::Backends::VULKAN);
assert_eq!(viewport_path_backends(backend, false), crate::select::path_backends());
}
}
@@ -3255,8 +3253,8 @@ fn display_bloom_slices_refuse_invalid_ranges_without_panicking() {
fn cancelled_environment_preparation_unwinds_gpu_error_scopes() {
use solidcraft_render::lighting::environment::EnvironmentSource;
let Ok((_, device, _queue)) = open_headless() else {
let vulkan = crate::select::native_backends().contains(wgpu::Backends::VULKAN);
assert!(!vulkan || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required Vulkan device unavailable");
let available = !crate::select::path_backends().is_empty();
assert!(!available || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required Vulkan device unavailable");
return;
};
let mut image = request(1);
@@ -3281,8 +3279,8 @@ fn cancelled_environment_preparation_unwinds_gpu_error_scopes() {
#[test]
fn failed_resource_creation_unwinds_gpu_error_scopes() {
let Ok((_, device, _queue)) = open_headless() else {
let vulkan = crate::select::native_backends().contains(wgpu::Backends::VULKAN);
assert!(!vulkan || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required Vulkan device unavailable");
let available = !crate::select::path_backends().is_empty();
assert!(!available || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required Vulkan device unavailable");
return;
};
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {