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 authored and GitHub committed 2026-10-29 13:58:49 +00:00
1 parent 6fdd58dec6
commit cb05e1e0f7
7 files changed
+50 -47

No files matched your search

@@ -87,7 +87,7 @@ fn run() -> Result<(), String> {
let gpu_host_start = Instant::now();
let policy = solidcraft_gpu::select::Policy::from_env().map_err(|e| e.to_string())?;
let (adapter, report) =
solidcraft_gpu::select::pick_vulkan(policy, |_| true).ok_or("no Vulkan adapter satisfies the requested GPU policy")?;
solidcraft_gpu::select::pick_path(policy, |_| true).ok_or("no Vulkan or Metal adapter satisfies the requested GPU policy")?;
session.register_render_job_host(solidcraft_gpu::path::host_on_adapter(&adapter).map_err(|e| e.to_string())?).map_err(|e| e.to_string())?;
(Some(report), Some(gpu_host_start.elapsed().as_secs_f64()))
} else {
+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(|| {
+14 -8
View File
@@ -201,18 +201,24 @@ pub fn survey(policy: Policy) -> Survey {
Survey { backends: format!("{:?}", native_backends()), adapters: found, chosen, lands_on }
}
/// The Vulkan adapter `policy` picks among those `keep` accepts, with its report; `None` where
/// the native backends (or `WGPU_BACKEND`) leave out Vulkan, as on macOS. The GPU path tracer
/// runs on Vulkan only (#3217): D3D12 was never faster in our measurements and needs minutes to
/// compile its shader at every start, where Vulkan keeps compiled pipelines between runs.
/// The backends the GPU path tracer may use among the native ones: Vulkan, and Metal on macOS
/// (D3D12 is out, #3217: it was never faster in our measurements and needs minutes to compile
/// its shader at every start, where Vulkan keeps compiled pipelines between runs).
pub fn path_backends() -> wgpu::Backends {
native_backends() & (wgpu::Backends::VULKAN | wgpu::Backends::METAL)
}
/// The path-tracer adapter `policy` picks among those `keep` accepts, with its report; `None`
/// where `path_backends` is empty (`WGPU_BACKEND=dx12`) or no adapter qualifies.
#[cfg(not(target_arch = "wasm32"))]
pub fn pick_vulkan(policy: Policy, keep: impl Fn(&wgpu::AdapterInfo) -> bool) -> Option<(wgpu::Adapter, AdapterReport)> {
if !native_backends().contains(wgpu::Backends::VULKAN) {
pub fn pick_path(policy: Policy, keep: impl Fn(&wgpu::AdapterInfo) -> bool) -> Option<(wgpu::Adapter, AdapterReport)> {
let backends = path_backends();
if backends.is_empty() {
return None;
}
let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env();
desc.backends = wgpu::Backends::VULKAN;
let mut all = pollster::block_on(wgpu::Instance::new(desc).enumerate_adapters(wgpu::Backends::VULKAN));
desc.backends = backends;
let mut all = pollster::block_on(wgpu::Instance::new(desc).enumerate_adapters(backends));
all.sort_by_key(|a| std::cmp::Reverse(rank(a.get_info().device_type)));
all.into_iter()
.filter(|a| keep(&a.get_info()))
+1 -2
View File
@@ -1308,8 +1308,7 @@ fn progressive_image_controls_route_to_the_registered_host() {
let host = match host {
Ok(host) => host,
Err(e) => {
let vulkan = egui_wgpu::wgpu::Backends::VULKAN;
let expected = solidcraft_gpu::select::native_backends().contains(vulkan);
let expected = !solidcraft_gpu::select::path_backends().is_empty();
assert!(!expected || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required GPU unavailable: {e}");
eprintln!("skipping progressive UI device test: {e}");
return;
+2 -2
View File
@@ -17,8 +17,8 @@ the pending/ready/failed host, and failures are not retried on each viewport ins
It opens its independent job device on the viewport's selected adapter,
so software fallback and safe mode do not rediscover a failing hardware driver.
An incompatible explicit GPU policy leaves progressive rendering unavailable there.
The path tracer runs on Vulkan only (#3217). A viewport on D3D12 is reopened on the
same GPU through Vulkan. Where there is no Vulkan driver (macOS for now, Windows over
The path tracer runs on Vulkan, or on Metal on macOS (#3217). A viewport on D3D12 is reopened on the
same GPU through Vulkan. Where there is no Vulkan or Metal driver (Windows over
Remote Desktop or on the WARP software adapter, older drivers), the path executor reports
itself unavailable and images render on the CPU. On an RTX 4090, D3D12 was never faster:
equal per sample with DXC and about 55% slower with FXC, and it compiles the path shader