Metal's MTLDevice survives a GPU recovery; the runtime's device refused every call with DeviceLost from the first loss on, and the consumer ends its VM at the first refusal. Now a device the runtime made is a line of generations (device/line.rs), each a DeviceShared over its own VkDevice, and the first call that needs the device after a loss makes a new VkDevice of the same physical device, options, queue families and extensions. - Two kinds of handle. The program's device (and its clones) is a root handle: it holds the line, pins no generation, and every public Device call goes to the generation that serves (each public method forwards to its *_now body). Objects hold generation handles, so their Vulkan calls, drops included, reach their own VkDevice alone. A generation handle's check is one relaxed load of the line's generation (Device::generation_serves). - A draw pays nothing for it. A command buffer takes its generation once, as it is made (CommandBuffer::generation); its encoders' per-draw reaches (work counts, fresh-unwritten and alias records, sampler-slot changes, attachment read notices) read it there, not through a device handle's kind. A bind's device check is one compare of the cells the two handles hold, inlined, its refusal (Device::foreign) out of line. - The state machine (Live, Lost, Failed): the claiming call recovers, every other call waits (a notice past 10 s, waiting on); threads command buffers' completion depends on never wait. Recovery: the lost generation's command buffers in flight fail through its completion path, its queues drain under their locks, create_on makes the next VkDevice, the queues are retired. - Command-buffer answers, as a real loss is one more hang: the oldest incomplete batch's command buffer fails GpuHang (exact on one VkQueue; on two or more the queue whose oldest incomplete batch was submitted first, said so in the notice), counted against its queue so the second revokes it (AccessRevoked); the others GpuRecoveryVictim; one committed on the hang's queue before the recovery runs nothing and completes with no error; one made before and committed after fails GpuRecoveryVictim. One loss names one hang: a batch that went to the driver just before the loss and reached the lost device's completion after the loss took the pending batches fails with it, as a victim (its own wait need not fail: the test hook's VkDevice runs it), and no later failure of the lost device names a second hang. - Command queues survive: a queue is a root handle and a cell of its place on one generation (VkQueue, slots, pools, hangs), which a recovery retires and the next command buffer re-makes; label and GPU-error count go on. - Objects made before a recovery are refused where they meet the new VkDevice or need their lost one: RuntimeGap::MadeBeforeRecovery, at the 38 lost-device checks (Device::lost_refusal) and at every foreign-object check of one line (Device::foreign). DeviceLost stays for a device the consumer made. - Carried across generations: the address space, resource IDs and the passing holds a making waits out before it takes one, unique identifiers, counters and the announced notice kinds. - A device handle is an enum of the two kinds (24 bytes, was 16): the shadow merge's copy-back dispatch, which holds handles, is boxed (clippy's large_enum_variant). - Device::recover_for_test (public, documented) runs the whole path with no GPU fault; RuntimeGap::AdoptedDeviceRecovery on an adopted device. - Degradation::DeviceRecovered (device-recovered), announced at each recovery with the culprit and its time. WorkCounts::vk_devices_live, vk_devices_made, device_recoveries: two VkDevices while objects of the lost one live, one after. Docs: docs/API.md catalogue entry, the runtime README's device-loss section, FAST-PATHS known gap (a make whose own submission first meets the loss is refused DeviceLost), docs/REFUSALS.md regenerated (refusal_surface.py names a forwarded body's sites by its public method), api_signatures blessed. Tests: runtime_lane::device_recovery (hang and victims, command buffers on their way to the driver at a recovery, MadeBeforeRecovery and the VkDevice bound, revocation at the second hang, a command buffer made before fails as victim, all under the validation layer; a recovery that finds nothing of the queue scheduled names no hang, its command buffers victims), tests::device_recovery (the hold of the hang's queue between a loss and the recovery), detach_lost and the MoltenVK lost-device lanes updated to the recovery's answers. Verified: - lavapipe: runtime suite and touched validation tests 1850/1850, surface 19/19 (m3-max-5695, gem10, before the rebase onto7e039f062a); on7e039f062a, before the one-hang fix, 1853/1853, surface 19/19, clippy clean (m3-max-6150, ms01). This tree: clippy clean (m3-max-6278, msi). - RADV RX 7900 XTX: 81/81 touched device tests (m3-max-5709). Iris Xe (ANV): 80/81 (m3-max-5766), the 81st kept_images, which fails so at development too and the last of these three commits fixes; the recovery tests alone 3 rounds of 3 (m3-max-5852). RTX 5080 Laptop GPU: 80/81 (b5080 entry devloss-devloss-tip1-nine-163410), the 81st the test's own 20 s bound on the recovery, which took 39.5 s beside another entry; the test now prints the recovery beside the device's first making and bounds neither. - MoltenVK on the M3 Max: the device_recovery tests 8 rounds of 8 (m3-max-6135); with the one-hang fix 12 of 12 (m3-max-6242), where without it 3 rounds of about 20 failed with a command buffer Completed after the hang (m3-max-6219, m3-max-6233). The RTX 5080 failed the second-hang test so in 2 runs of 3 (devloss-devloss-rec-alone-170457). - Recovery time, alone, under the validation layer: llvmpipe 29.9 ms, RX 7900 XTX 20.4 ms, Iris Xe 15 to 21 ms (its first making 30 to 41 ms), M3 Max (MoltenVK) 35 to 40 ms (42 to 50 ms), RTX 5080 530 to 559 ms (602 to 635 ms). VK_EXT_device_fault listed: llvmpipe 0, XTX 1, Iris 0, 5080 1. - Per draw (draw_costs, release, --heap, M3 Max, base7e039f062aand the tip alternating, 4 rounds, m3-max-6121): instructions a draw ws +0.03%, many -0.05%, 10k -0.31%; draw_primitives +10, +8 and -26 of 17,172, 11,245 and 7,736 a call; each set_* call +3 to +4 (of 425 to 860); a command buffer's creation +170 to +370, an encoder's end +120 to +390. (cherry picked from commit 3d7758c074fc9895d5d6c9766604c6c4eae9166d)
241 lines
14 KiB
Rust
241 lines
14 KiB
Rust
//! Color attachments a render pass does not store: a texture over a buffer keeps the buffer's
|
|
//! bytes, and under `DontCareColorPolicy::Kept` a texture's own image (`DontCare`) its earlier
|
|
//! texels.
|
|
//!
|
|
//! **Metal's answer** (the DontCare-store probe, an M3 Max under macOS 27.0.1, each case 20 times
|
|
//! alike): after a pass whose color attachment over a buffer is stored `DontCare`, the buffer
|
|
//! holds the bytes it held before the pass in every texel; Metal writes neither the rendered nor
|
|
//! the clear value, and no byte between the rows. So it was in 660 passes over a texture at its
|
|
//! first use, 72 after a pass that stored it (which keep that pass's bytes), 72 beside a second
|
|
//! attachment stored with `Store` (which holds the rendered value), 98 that clear and draw
|
|
//! nothing, and 24 over IOSurfaces: 16x16, 333x77, 1307x512 at rows of 5248 bytes, and 2048x2048,
|
|
//! tight and padded; `BGRA8Unorm`, `RGBA8Unorm`, `BGRA8Unorm_sRGB`, `RGBA16Float` and `R8Unorm`;
|
|
//! shared, no-copy and IOSurface buffers; every load action; a draw over the whole texture, half
|
|
//! of it, a corner, or none. And so it was with a memory barrier between two draws (the barrier
|
|
//! probe, the same GPU, 20 times alike): over buffers, render targets, textures or the buffer
|
|
//! alone, from the fragment stage to the vertex or the fragment stage, where a Vulkan pass splits
|
|
//! or MoltenVK restarts its Metal pass. A `Store` there leaves the rendered texels, and outside the
|
|
//! buffer's (`Load`) or the clear value (`Clear`). The one exception: the first pass in a process
|
|
//! to overflow Apple's tiler (some 2 to 4 million triangle-tile entries) leaves the rendered
|
|
//! texels, in every texel, all or nothing; the driver then grows its tiler's buffer for the rest of
|
|
//! the process. No call predicts or detects it, and the runtime does not model it: its answer is
|
|
//! the buffer's bytes, exactly. `MultisampleResolve`, which stores nothing of the attachment,
|
|
//! leaves it the same way: a texture over a buffer is never multisampled, so such an attachment is
|
|
//! a single-sampled one whose texels are copied into its resolve texture (`resolve.rs`).
|
|
//!
|
|
//! - **A shadowed texture** (`shadow.rs`) renders into its shadow, which the pass copies back only
|
|
//! where it stores it (`render/shadows.rs`): the buffer keeps its bytes. So does a planar
|
|
//! surface's depth or stencil texture, which renders through its tiled twin, a shadow
|
|
//! (`texture/surface_depth.rs`), and any texture over a buffer the pass renders through a
|
|
//! stand-in (`attachment_stand_in.rs`), which copies back only what Metal stores.
|
|
//! - **A texture whose image lies over its buffer** (`Texture::image_over_buffer`: a linear image
|
|
//! over the buffer's memory, an ordinary buffer's, a no-copy buffer's host pages, a dma-buf's,
|
|
//! a surface's, a texture buffer's attachment twin among them; or, on MoltenVK, a Metal texture
|
|
//! over a no-copy or surface buffer's `MTLBuffer`) would have the pass render into the buffer's
|
|
//! bytes. Where the pass does not store it, it renders through the image its texture keeps for
|
|
//! the queue instead (`attachment_stand_in::kept_backing`, [`Cause::KeptBytes`]): a `Load`
|
|
//! copies the buffer's texels over the render area into that image before the pass; a `Clear`
|
|
//! or a `DontCare` load is the image's own first operation, by the rules a `DontCare` load
|
|
//! follows elsewhere; nothing is copied back, and the image's own store is `DONT_CARE`
|
|
//! (`render_target.rs`): nothing reads what a pass leaves there. A pass that stores the
|
|
//! attachment renders it in place, as before, at no cost. Announced once (`dont-care-stand-in`,
|
|
//! performance), and counted per pass (`WorkCounts::kept_bytes_passes`).
|
|
//! - **Handed to Metal's own `DontCare`**: on a driver that stores `DONT_CARE` as Metal's own on
|
|
//! the same GPU (`DriverFact::DontCareStoreIsMetals`, MoltenVK on the M4 and the M3 Max), a pass
|
|
//! recorded as one render pass instance with nothing that restarts the driver's Metal pass
|
|
//! hands the attachment to Metal, which keeps the buffer's bytes itself (`HandedToMetal`): it
|
|
//! renders in place, at no cost, and is neither announced nor counted. A pass split into
|
|
//! instances, or restarted, stores the attachment between them, so it renders through the kept
|
|
//! image as on any other driver. One that runs one more instance after its last to resolve
|
|
//! (`render.rs`), which this cannot see, stays in place, and `dont-care-store` announces it.
|
|
//! - **Decided as the pass ends** ([`settle`]), once its store actions are known (a late one from
|
|
//! `set_color_store_action`, and `Unknown`, which stores as `DontCare`): the draws record against
|
|
//! the compatible pass, which a stand-in of the attachment's format and samples keeps, and the
|
|
//! framebuffer is made then. But where a draw of the pass took a view of the attachment's image
|
|
//! (a framebuffer fetch reading it in place, as an input attachment or a storage image, its
|
|
//! sample planes, a draw rendering it in another format through a view, its extended-range
|
|
//! shadow), where the pass executes render commands (whose draws record against the encoder's
|
|
//! layout as the execution was encoded, `render/executions.rs`), where the device renders the
|
|
//! texture's image only beside other linear ones (`VK_NV_linear_color_attachment`'s rule,
|
|
//! `linear_attachment.rs`) and a draw took a view of another such attachment in place, which
|
|
//! would then have to render apart, and where its image or its format copies no texels and the
|
|
//! pass loads it: there the pass renders in place, and `dont-care-store` announces the texels it
|
|
//! rendered there, Metal's answer where its tiler overflows.
|
|
//!
|
|
//! **A texture's own image** keeps its earlier texels too, under the device policy's
|
|
//! `dont_care_colors = DontCareColorPolicy::Kept`, which `DevicePolicy::exact_answers()` selects;
|
|
//! under the default, `Driver`, the pass stores it as the driver stores `DONT_CARE`
|
|
//! (`render_target.rs`), announced as `dont-care-store`. Metal's documentation leaves them open
|
|
//! (`MTLStoreAction.dontCare`: "You need to assume that GPU discarded the texture's contents"), and
|
|
//! what Apple's GPU leaves is measured: the attachment's texels from before the pass, the pass's
|
|
//! clear and its draws both gone (the conformance case `pass-sequence-color-actions`, an M3 Max
|
|
//! under macOS 27.0.1, eight runs alike, whose two passes over one texture the host wrote first
|
|
//! read the host's texels back after a `DontCare` store under either load action of the first and
|
|
//! either load action and render area of the second; and `dont_care_stores`' passes on MoltenVK,
|
|
//! which hands the store to Metal). Vulkan's `DONT_CARE` leaves the texels undefined, and a driver
|
|
//! may leave neither answer: NVIDIA's left a pass's clear under its draw. So under `Kept` a pass
|
|
//! that stores a color attachment `DontCare` renders it, where the texture's image is its own (not
|
|
//! a buffer's bytes, nor a shadow of them), through a backing its queue keeps for its passes of
|
|
//! the attachment's shape (`attachment_stand_in::pooled_backing`, [`Cause::KeptTexels`]): a `Load`
|
|
//! copies the texture's texels over the render area into it before the pass (what the pass's
|
|
//! blending and tile dispatches read there), a `Clear` or a `DontCare` load is its own first
|
|
//! operation, as above, and nothing is copied back. Its store is `DONT_CARE`
|
|
//! (`render_target.rs`). Not where the pass hands the attachment to Metal's own
|
|
//! `DontCare` (`HandedToMetal`, at no cost), nor in the cases above that render in place, nor for a
|
|
//! memoryless texture (none of its texels outlive a pass in Metal), nor one the pass resolves
|
|
//! (`MultisampleResolve`, which `resolve::keep_samples` keeps where its resolve reads the samples),
|
|
//! nor where a format of the device's makes no stand-in image or, loaded, copies none: those
|
|
//! render in place, and `dont-care-store` announces them. The texture keeps its texels, Metal's
|
|
//! answer, so nothing is announced; each such pass is counted (`WorkCounts::kept_texels_passes`).
|
|
//! The cost is the backing's memory, one image of the attachment's shape per queue (bounded with
|
|
//! the queue's memoryless backings, `attachment_stand_in::BackingPool`), and a `Load`'s copy.
|
|
|
|
use super::attachment_stand_in::{self, Cause, StandIn};
|
|
use super::command_buffer::CommandBuffer;
|
|
use super::degradation::Degradation;
|
|
use super::error::{Error, RuntimeGap};
|
|
use super::linear_attachment;
|
|
use super::render_pass::ColorTarget;
|
|
use super::render_values::{LoadAction, StoreAction};
|
|
use metal2vulkan::device::DontCareColorPolicy;
|
|
|
|
/// Whether a pass stores nothing of `color`: `DontCare`, or `MultisampleResolve` (as `Unknown`
|
|
/// stores once the pass ends).
|
|
fn unstored(color: &ColorTarget) -> bool {
|
|
matches!(
|
|
color.store,
|
|
StoreAction::DontCare | StoreAction::MultisampleResolve
|
|
)
|
|
}
|
|
|
|
/// Whether `color`, a texture's own image (neither over a buffer's bytes nor a shadow of them), is
|
|
/// one a pass renders through a backing so that it keeps its texels (the module docs): under
|
|
/// `policy`'s `Kept`, stored `DontCare`, resolving nothing, and not memoryless.
|
|
fn keeps_texels(color: &ColorTarget, policy: DontCareColorPolicy) -> bool {
|
|
policy == DontCareColorPolicy::Kept
|
|
&& color.store == StoreAction::DontCare
|
|
&& !color.memoryless
|
|
&& color.resolve.is_none()
|
|
&& color.copied_resolve.is_none()
|
|
&& !color.target.texture.over_buffer()
|
|
}
|
|
|
|
/// Render each color attachment among `colors`, a pass's as it ends, that the pass does not
|
|
/// store and whose texture's image lies over its buffer's bytes, through the image its texture
|
|
/// keeps for `maker`'s queue, and under the device's `DontCareColorPolicy::Kept` each the pass
|
|
/// stores `DontCare` whose texture's image is its own, through a backing the queue keeps (the
|
|
/// module docs): but where the pass hands it to the driver's `DONT_CARE` as Metal's own
|
|
/// `DontCare` (`handed`, `HandedToMetal`), a draw took a view of its image (`viewed`), the pass
|
|
/// executes render commands (`executes`), or another color in place is bound by
|
|
/// `VK_NV_linear_color_attachment`'s rule and the pass cannot render it apart (`taken`, as
|
|
/// `linear_attachment::settle` asks it). The first announced; both counted.
|
|
pub(super) fn settle(
|
|
maker: &CommandBuffer,
|
|
colors: &mut [Option<ColorTarget>],
|
|
executes: bool,
|
|
handed: impl Fn(usize) -> bool,
|
|
viewed: impl Fn(usize) -> bool,
|
|
taken: impl Fn(usize) -> bool,
|
|
) -> Result<(), Error> {
|
|
if executes {
|
|
return Ok(());
|
|
}
|
|
let device = maker.device();
|
|
// The colors a draw took a view of that render in place under the extension's rule: one
|
|
// rendered through a kept image beside them would leave them to render apart, which
|
|
// `linear_attachment::settle` refuses.
|
|
let mut held = Vec::new();
|
|
for (index, color) in colors.iter().enumerate() {
|
|
let Some(color) = color else {
|
|
continue;
|
|
};
|
|
if color.stand_in.is_none()
|
|
&& taken(index)
|
|
&& linear_attachment::bound(device, &color.target)?
|
|
{
|
|
held.push(index);
|
|
}
|
|
}
|
|
let queue = maker.queue_place().id();
|
|
let policy = device.compile_device().policy().dont_care_colors;
|
|
let (mut kept, mut kept_texels) = (false, false);
|
|
for index in 0..colors.len() {
|
|
// The images the pass renders its other colors into, which no backing of this one is.
|
|
let taken_images: Vec<_> = (colors.iter().flatten())
|
|
.filter(|color| color.stand_in.is_some())
|
|
.map(|color| color.target.texture.clone())
|
|
.collect();
|
|
let Some(color) = colors[index].as_mut() else {
|
|
continue;
|
|
};
|
|
let over_buffer = color.target.texture.image_over_buffer();
|
|
if !unstored(color)
|
|
|| color.stand_in.is_some()
|
|
|| !(over_buffer || keeps_texels(color, policy))
|
|
|| handed(index)
|
|
|| viewed(index)
|
|
|| held.iter().any(|&other| other != index)
|
|
{
|
|
continue;
|
|
}
|
|
let loads = color.load == LoadAction::Load;
|
|
if loads && attachment_stand_in::check_copies(&color.target).is_err() {
|
|
continue;
|
|
}
|
|
if !over_buffer {
|
|
let own = color.target.clone();
|
|
// A format the device makes no stand-in of keeps the pass in place.
|
|
let backing =
|
|
match attachment_stand_in::pooled_backing(maker, &own, &taken_images, loads) {
|
|
Ok(backing) => backing,
|
|
Err(Error::NotImplemented(RuntimeGap::Render)) => continue,
|
|
Err(error) => return Err(error),
|
|
};
|
|
color.target = backing;
|
|
color.stand_in = Some(StandIn {
|
|
own,
|
|
cause: Cause::KeptTexels,
|
|
area: None,
|
|
});
|
|
kept_texels = true;
|
|
continue;
|
|
}
|
|
let own = color.target.clone();
|
|
// A format the device copies no image of keeps the pass in place.
|
|
let backing = match attachment_stand_in::kept_backing(maker, &own, queue, true) {
|
|
Ok(backing) => backing,
|
|
Err(Error::NotImplemented(RuntimeGap::Render)) => continue,
|
|
Err(error) => return Err(error),
|
|
};
|
|
let (format, store) = (own.texture.pixel_format(), color.store);
|
|
device.degrade(Degradation::DontCareStandIn, || {
|
|
(
|
|
format!(
|
|
"a {format:?} texture over a buffer's bytes, color attachment {index} of a \
|
|
render pass"
|
|
),
|
|
format!(
|
|
"the pass stores it with `{store:?}`, after which Metal's texture over a \
|
|
buffer holds the buffer's bytes, and the texture's image lies over them, so \
|
|
the pass renders into an image the texture keeps"
|
|
),
|
|
)
|
|
});
|
|
color.target = backing;
|
|
color.stand_in = Some(StandIn {
|
|
own,
|
|
cause: Cause::KeptBytes,
|
|
area: None,
|
|
});
|
|
held.retain(|&other| other != index);
|
|
kept = true;
|
|
}
|
|
if kept {
|
|
device.shared().counts.kept_bytes_pass();
|
|
}
|
|
if kept_texels {
|
|
device.shared().counts.kept_texels_pass();
|
|
}
|
|
Ok(())
|
|
}
|