open_async skipped the fallback request once any software adapter had been tried, even when wgpu's fallback adapter was a different one. It now skips only an adapter the ranked loop already tried.
craft-gpu-device
Choosing and opening a wgpu device for a Craft app: an adapter choice the user can set, fixes for
drivers that crash, a software fallback, a capability report, and device health with recovery
after a device loss. The crate depends on craft-gpu, wgpu 30, pollster and log, with
serde as an optional feature (on by default). It depends on no app crate.
It uses the workspace's wgpu, which turns on no native backend, and the WebGPU backend on
wasm32. The native-vulkan, native-dx12, native-metal and native-gles features turn on
wgpu's backends, as the features of the same names in craft-gpu-wgpu do. Cargo merges the
features, so an app can turn them on through either crate.
Licensed under MIT OR Apache-2.0.
Contents
| Item | What |
|---|---|
Candidate |
one listed adapter as plain data: name, backend, device type, PCI vendor, device and bus, driver, feature count, and whether it can draw to the window's surface |
list(instance, surface) |
every adapter the instance lists, as wgpu::Adapters and as Candidates |
native_instance, native_descriptor |
a wgpu instance over Vulkan, Metal and DirectX 12, plus OpenGL except on Windows; wgpu's WGPU_* flag variables apply |
choose(candidates, power) |
wgpu's own adapter rule with two corrections: the newest version of a driver that lists one GPU twice, and DirectX 12 instead of Vulkan on Intel Gen 9 to Gen 11 integrated GPUs (intel_gen9_to_gen11) |
Preference |
Auto (high performance), LowPower, Adapter(AdapterKey) or Software; serde form {"kind":"auto"}, {"kind":"adapter","name":…,"backend":…,"vendor":…,"device":…,"pciBusId":…,"driver":…} |
AdapterKey |
what identifies an adapter across runs: backend, PCI vendor, device and bus, the Vulkan driver's name, and the name when the backend reports no PCI ids |
select(candidates, preference) |
the adapter to draw with, as a Selection with its Notices |
ranked(candidates, preference) |
the order in which to try opening a device: the selection, then the other usable adapters by type with software after hardware, never an older version of a driver listed twice, or the Vulkan entry of an Intel Gen 9 to Gen 11 GPU on Intel's Windows driver |
open(instance, surface, preference, request) |
native only, like list: opens a device through ranked, then wgpu's fallback adapter; returns Opened (adapter, device, queue, Capabilities, DeviceHealth, notices) or OpenError |
list_async, open_async |
list and open as futures, for a wasm32 web worker as well as native; in a browser list_async gives the high-performance adapter, the one open_async opens for Auto and a named adapter |
pick(adapters, surface, preference) |
select over listed adapters, for egui-wgpu's native_adapter_selector; the error is an OpenError (convert it with map_err(|e| e.to_string()) for that selector) |
Capabilities, Vendor |
NV12 and P010 (sampled only in wgpu 30), 16-bit norm, f16 and timestamp support, the largest texture and buffer, the vendor, and whether the adapter is a software one, read from the craft_gpu::DeviceCaps it holds (its fields are readable through Deref; From converts both ways) |
DeviceHealth, Fault, WaitOutcome |
records a device loss or an uncaptured error instead of panicking; polls and bounded waits that do not touch a lost device |
Recovery, RecoveryPolicy |
the preference to open after a loss: the user's own, and Software once the device was lost 3 times in 10 minutes (both values can be set) |
device_caps(device), Opened::device_caps |
the craft_gpu::DeviceCaps of a device: its own limits, enabled feature names, vendor id and software flag, in the form craft-gpu-wgpu reports |
DeviceHealth::device_status |
a recorded loss as craft_gpu::GpuFault::DeviceLost, in the form of GpuContext::device_status; Fault converts to GpuFault (a loss to DeviceLost, an error to Operation) |
Notice |
the adapter asked for is missing, no software adapter is listed, only a software adapter can draw, an adapter failed to open, or drawing moved to software after losses; the Display text is English, for the log |
Failure cases
- With no usable adapter,
selectreturnsNone, andopenandpickreturnOpenError::NoAdapter, orOpenError::UnsafeDriverwhen only a driver that can end the process can draw. - In a browser, WebGPU offers one adapter for a power preference, so a named adapter cannot be
chosen.
open_asyncasks for the adapter withPreference::power()as the power preference, and gives the automatic choice withNotice::AdapterMissingfor a named adapter. wgpu 30 does not pass the fallback-adapter option to the browser, soPreference::Softwareopens the browser's adapter, withNotice::NoSoftwareAdapterunless that adapter is a software one, andRecoverycannot move drawing to software there.CapabilitiesandRecoveryotherwise work there as they do on native, and so doesDeviceHealthapart fromwait,wait_timeoutandwait_timeout_cancelable, which are native only. On the browser backend, wgpu 30 panics on an uncapturedGPUInternalErrorbefore the handlerDeviceHealth::watchinstalls runs, so such an error is not recorded. Validation and out-of-memory errors are. - An adapter that lacks
Request::required_features, whose limits are lower thanRequest::limits, or whoserequest_devicefails, adds aNotice::DeviceFailed, andopentries the next one. When every adapter fails, the error lists each failure. - A named adapter that is no longer listed, or can no longer draw to the surface, gives the
automatic choice and
Notice::AdapterMissing. - A driver that ends the process cannot be caught.
chooseandrankednever use an older version of a driver that lists one GPU twice, or the Vulkan entry of an Intel Gen 9 to Gen 11 GPU on Intel's Windows driver. When those are the only adapters that can draw and wgpu's fallback adapter does not open either,openreturnsOpenError::UnsafeDriverwith their names. When a driver reports no bus id, a second GPU of the same model on the same driver looks like an older version, and is used through its other backend (DirectX 12 on Windows, OpenGL elsewhere). - Only an error whose cause is wgpu's "Parent device is lost" counts as a loss. The same words in a resource's label do not count. An error about a destroyed buffer or texture is recorded as an error.
wait,wait_timeoutandwait_timeout_cancelablewait for one submission index. To wait for everything submitted so far, passqueue.submit([]); work submitted later does not extend the wait.waithas no deadline. It polls in slices of at most 100 ms, and a loss or an error other than out of memory that another clone of theDeviceHealthrecords ends it after the current slice.DeviceHealth::watchreplaces the device's uncaptured-error handler. Polls catch the panic thatDevice::pollraises on a lost device and record the loss.- wgpu keeps one device-lost callback per device, and a later
set_device_lost_callbackreplaces the oneDeviceHealth::watchset. Acraft-gpucontext on a watched device readsDeviceHealth::device_statusinstead of setting its own callback. - After an out-of-memory error, polls and waits keep running, so staging memory is reclaimed.
The app reads
DeviceHealth::out_of_memory_countwhen it handles the error, frees memory, and passes the count toDeviceHealth::clear_out_of_memory. The device is then healthy again, unless it was lost, another error was recorded, or a new out-of-memory error came in after the count was read.
After a device loss
DeviceHealth::device_status returns GpuFault::DeviceLost once a loss is recorded, and the
caller starts recovery then. DeviceHealth::device_lost turns true only when wgpu itself reported
the loss; it stays false for a loss the health layer recorded on its own, such as a bounded wait
that reached its deadline. Nothing made with the lost device can be used again, and wgpu keeps
none of its contents. The caller then:
- Drops the old
Openedand everything made with its device: buffers, textures and their views, samplers, bind group layouts and bind groups, shader modules, pipeline layouts and pipelines, query sets, unsubmitted command encoders, and any renderer that holds them (an egui-wgpuRenderer, for example). Work submitted before the loss does not complete, and waits on it returnfalse. - Calls
Recovery::lostwith the time and opens a new device withopenoropen_async, using the returned preference and the sameRequest. When the step has a notice (Notice::SoftwareAfterLosses), the app shows it only if the newOpened::caps.software()is true. Otherwise no software adapter opened (in a browser, or with none listed), and the newOpened::noticessay what was opened instead. - Reads the new
Opened::capsbefore choosing pipelines again. The new device can be another adapter, or a software one, with other features and limits: for example, without NV12 the planes are drawn intoR8UnormandRg8Unormtextures instead. - Configures each window surface again with the new device (
Surface::configure). The surface itself is kept when it was passed toopen, which then opens only an adapter that can draw to it. The new adapter may not support the old configuration (an HDR format, for example), so the caller readsSurface::get_capabilitieswith the new adapter and picks a format, present mode and alpha mode from it. - Builds the pipelines for the surface format picked in step 4, and the bind groups and renderers, again, and uploads the textures and buffers again from the app's own copies of their data.
- Makes any
craft-gpucontext again on the new device, with the newOpened::device_caps, and submits again the jobs and frames that did not complete. - Uses the new
Opened::health. The oldDeviceHealthkeeps reporting the loss.
Tests
The selection tests run on Candidate lists from real machines. The open tests use wgpu's noop
backend (the noop feature, enabled for tests only), so they do not need a GPU. One test compares
choose with wgpu's own request_adapter on the machine that runs it, and passes when no adapter
is listed.