A texture an attached read samples takes two faces, its own image and a second of the same plan (the shadow the copy took); a pass that renders it whole without loading it renders the face it did not present while its draws read the one it did. Counted: texture_faces_made, attached_read_swaps, attached_read_swap_reads/texels, attached_read_swap_fixes/texels, and the copies no turn took off by cause (attached_read_unswapped_*). Not built yet: this commit is for the first compile job; fixes follow.
3649 lines
168 KiB
Rust
3649 lines
168 KiB
Rust
//! The core every encoder shares: its command buffer, its own `VkCommandBuffer`, and the rule of
|
|
//! one open encoder per command buffer.
|
|
//!
|
|
//! - **Recording.** `open` allocates a primary `VkCommandBuffer` from the command buffer's pool
|
|
//! and begins it; `end` ends it and appends it to the command buffer's recordings, which
|
|
//! `commit` submits in that order.
|
|
//! - **Segments**. An indirect command buffer's commands may change until the command buffer is
|
|
//! committed, so an encoder that executes them [`defer`](EncoderCore::defer)s them: it ends its
|
|
//! `VkCommandBuffer` there and goes on in a new one, and the segment's own is recorded later
|
|
//! ([`EncoderCore::segment`]), behind a barrier from everything before it and with one to
|
|
//! everything after it, and goes between the two as its batch is submitted, its uses joined to
|
|
//! its part's summary. `commit` records it from the records as they stand; a buffer the GPU may
|
|
//! write (`IndirectCommandBuffer::gpu_written`) has its commands known only once the work before
|
|
//! them completed, so its segment starts a part of the encoder of its own, in a batch of its own,
|
|
//! which the scheduler's host step records (`indirect_execute.rs`).
|
|
//! - **Ordering** ([`tracker`](super::tracker)). Each command goes through [`EncoderCore::command`]
|
|
//! with its uses: in an encoder whose commands Metal runs in order (blits, serial dispatches) it
|
|
//! gets the barrier those uses need after the earlier commands. `end` hands the encoder's summary
|
|
//! to the command buffer; the scheduler records the barriers between encoders, batches and queues
|
|
//! as it submits each batch, each in a command buffer of its own it begins before the encoder's.
|
|
//! - **Encoders past [`PLANNED_BARRIERS`]** of their command buffer begin their own
|
|
//! `VkCommandBuffer` with a barrier from everything before ([`EncoderCore::open`]), and the
|
|
//! scheduler begins none before them where that one still leads the encoder's
|
|
//! (`command_buffer/submit.rs`): a batch begins at most that many command buffers for its
|
|
//! encoders' barriers however many encoders it holds. A driver may hold device memory of a
|
|
//! fixed pool for each command buffer until it is begun again, reset or freed: ANV below
|
|
//! Xe-HP (Mesa 26.2.6, `anv_device.c`) holds a 64 KiB block of its 1 GiB binding-table pool
|
|
//! for each, 16,384 at most, and on ms01's Iris Xe a submission that began command buffers
|
|
//! past them crashed in the driver as it recorded a clear (`tests::many_passes`). The barrier
|
|
//! from everything orders all the encoder's commands after all before it on the queue, so it
|
|
//! holds every barrier the scheduler would plan before the encoder; it also orders commands
|
|
//! that need no barrier. Counted: `WorkCounts::encoders_barriered_at_open`.
|
|
//! - **Shadowed textures** (`shadow.rs`). The encoder copies a 2D texture or array over a buffer,
|
|
//! or a texture buffer viewed through a staging buffer, from the buffer before the commands
|
|
//! that use it, and back after the ones that write it, as commands of its own; `end` copies
|
|
//! back what is left.
|
|
//! - **Abandoned encoders.** An encoder dropped without `end_encoding` marks its command buffer
|
|
//! abandoned: its commands are lost, so `commit` refuses rather than run without them.
|
|
//! - Encoders are `Send` but not `Sync`: every method takes `&mut self`, and the core holds a
|
|
//! `!Sync` marker so no encoder is shared between threads by reference.
|
|
|
|
use super::binding::{self, LastSetZero, ReachingPrefix, Write, WrittenSet};
|
|
use super::buffer::Buffer;
|
|
use super::churn_counts::KeepBy;
|
|
use super::command_buffer::rebound_use::Unlogged;
|
|
use super::command_buffer::{recording_error, CommandBuffer};
|
|
use super::deferred::{Deferred, StepInputs};
|
|
use super::degradation::NoticesFor;
|
|
use super::error::{internal, invalid, vulkan, CommandBufferError, Error};
|
|
use super::execution::Execution;
|
|
use super::fresh_unwritten::{FreshRead, FreshUnwritten};
|
|
use super::heap::Heap;
|
|
use super::host_aliases::Quiet;
|
|
use super::index_bounds::{BoundsRunRequest, BoundsSlot};
|
|
use super::index_rewrite::{
|
|
self, CommandedList, GatherHeld, GatherRequest, GatherRuns, GatheredList,
|
|
IndirectGatherRequest, RewriteRunRequest,
|
|
};
|
|
use super::kept_commands::{Held, KeptEffects};
|
|
use super::noted_apart::{Moved, NotedApart, NotedInto};
|
|
use super::pass_runs::RunKeep;
|
|
use super::pending_dispatch::PendingDispatch;
|
|
use super::pipeline::VariantObjects;
|
|
use super::rebind::{Holds, Taken};
|
|
use super::render::face_swap::Turns;
|
|
use super::replay::Replay;
|
|
use super::residency::{Residency, Used};
|
|
use super::resource::ResourceKey;
|
|
use super::retained::Retained;
|
|
use super::row_copies::RowCopies;
|
|
use super::shadow::{
|
|
CopyCause, CopyGate, GatedCopyIn, LaterRun, LevelsCopy, ShadowCopies, ShadowCopy, ShadowSync,
|
|
ShadowUse, Step, Takes, Within,
|
|
};
|
|
use super::shadow_copy_in::CopyIn;
|
|
use super::shadow_merge::{MergeLease, ShadowMerge};
|
|
use super::shadow_standing::Unskipped;
|
|
use super::sparse::encoder::SparseUpdate;
|
|
use super::texture::decoded_twin::TwinWrite;
|
|
use super::tracker::{Barrier, EncoderUses, Reach, Summary, Use};
|
|
use super::unique_identifier::ScheduledWork;
|
|
use super::used_resources::EncoderKind;
|
|
use super::write_routes::Routes;
|
|
use super::Device;
|
|
use crate::device::functions::Vk;
|
|
use crate::device::{lock, DeviceShared};
|
|
use crate::shards::Tally;
|
|
use crate::work_counts::{Begun, Counters, Counting};
|
|
use ash::vk;
|
|
use metal2vulkan::bind::ReplayFaults;
|
|
use std::any::Any;
|
|
use std::collections::BTreeSet;
|
|
use std::marker::PhantomData;
|
|
use std::mem::MaybeUninit;
|
|
use std::panic::Location;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
use std::sync::{Arc, Mutex, Weak};
|
|
|
|
/// The next core's [`EncoderCore::id`]: from 1, so none is `retained::HeldBy`'s 0.
|
|
static CORES: AtomicU64 = AtomicU64::new(1);
|
|
|
|
/// The most emptied core boxes a device keeps ([`SpareCores`]): a render encoder that opens while
|
|
/// more than these are open makes a box of its own.
|
|
const SPARE_CORES: usize = 4;
|
|
|
|
/// The box of an ended render encoder's core, emptied: room for the next encoder's core.
|
|
struct CoreRoom(Box<MaybeUninit<EncoderCore>>);
|
|
|
|
/// The encoders of a command buffer whose barriers the scheduler plans and records before each in
|
|
/// a command buffer of barriers of its own (the module docs): each later encoder's own begins
|
|
/// with a barrier from everything before. Past twice the 706 render passes of a frame split at
|
|
/// every indexed draw, in one command buffer (`tests::linear_runs`'s `CHUNK_PASSES`).
|
|
pub(crate) const PLANNED_BARRIERS: usize = 2048;
|
|
|
|
/// The boxes of a device's render encoders' cores, emptied as their encoders end, which the next
|
|
/// render encoders it opens take. A render encoder holds its core in a box (`render.rs`), so its
|
|
/// end hands the core on through each step as a pointer rather than its state; its open takes the
|
|
/// box an ended encoder gave back, so the box costs no allocation once the device has one.
|
|
#[derive(Default)]
|
|
pub(crate) struct SpareCores(Mutex<Vec<CoreRoom>>);
|
|
|
|
impl SpareCores {
|
|
/// Room for a core: the latest box an ended encoder gave back, else a new one.
|
|
fn take(&self) -> Box<MaybeUninit<EncoderCore>> {
|
|
let room = lock(&self.0).pop();
|
|
room.map_or_else(Box::new_uninit, |CoreRoom(room)| room)
|
|
}
|
|
|
|
/// Keep `room`, an ended encoder's core's box, emptied, where fewer than [`SPARE_CORES`] are
|
|
/// kept; else it goes.
|
|
fn give(&self, room: Box<MaybeUninit<EncoderCore>>) {
|
|
let mut spare = lock(&self.0);
|
|
if spare.len() < SPARE_CORES {
|
|
spare.push(CoreRoom(room));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One ended encoder's recording, or one part of it (`EncoderUses::split`), in its command
|
|
/// buffer's encoding order.
|
|
pub(super) struct Recorded {
|
|
/// The encoder's place among its command buffer's ended encoders (`State::encoder_ended`),
|
|
/// which each of its parts carries.
|
|
pub(super) encoder: usize,
|
|
/// Its `VkCommandBuffer`s, in order; once its batch is submitted, each segment's among them.
|
|
pub(super) raws: Vec<vk::CommandBuffer>,
|
|
/// The encoder's own `VkCommandBuffer` that began with a barrier from everything before
|
|
/// (`EncoderCore::open`): where it is still the first of `raws`, its batch records no
|
|
/// command buffer of barriers before the part (`command_buffer/submit.rs`).
|
|
pub(super) barriered: Option<vk::CommandBuffer>,
|
|
/// Its segments, in order, until its batch is submitted.
|
|
pub(super) segments: Vec<Segment>,
|
|
/// What the encoder used, for the barriers its submission records; each segment's uses join
|
|
/// it as its batch is submitted.
|
|
pub(super) summary: Summary<ResourceKey>,
|
|
/// A resource state encoder's sparse mapping updates, which its batch binds before its work
|
|
/// (`sparse/`); none for any other encoder.
|
|
pub(super) sparse: Vec<SparseUpdate>,
|
|
/// A blit encoder's writes into ASTC carriers, which its batch's submission decodes into
|
|
/// their second decoded twins after its work (`texture/decoded_twin.rs`); none for any other
|
|
/// encoder.
|
|
pub(super) twin_writes: Vec<TwinWrite>,
|
|
}
|
|
|
|
/// Commands an encoder left to record later (`deferred.rs`).
|
|
pub(super) struct Segment {
|
|
/// Where its `VkCommandBuffer` goes: after this many of its part's own.
|
|
pub(super) at: usize,
|
|
/// Where its dispatches go among the command buffer's executions: after this many, the
|
|
/// segments' placed before it not counted.
|
|
pub(super) executions_at: usize,
|
|
pub(super) work: Deferred,
|
|
/// The GPU may write what the work reads (`Deferred::gpu_written` at the call): the segment
|
|
/// starts a part of its own, and the host step records it.
|
|
pub(super) host: bool,
|
|
/// What `commit` left for its host step: one it foresaw, or one it did not (what the work
|
|
/// reads written by the GPU only since).
|
|
pub(super) inputs: Option<StepInputs>,
|
|
/// Its recording until its batch is submitted: `commit`'s, or the host step's.
|
|
pub(super) recorded: Option<RecordedSegment>,
|
|
/// The host step recorded it.
|
|
pub(super) stepped: bool,
|
|
/// `commit` left its recording to the host step for the pipelines it records, which were
|
|
/// being made then (`render/pending.rs`).
|
|
pub(super) pipelines: bool,
|
|
/// `commit` recorded it on the GPU alone, for good (`Prepared::for_good`), and no host step
|
|
/// recorded it since: its resident set and what its commands named were resolved then, so a
|
|
/// rebind before the command buffer's first submission records it again
|
|
/// (`command_buffer/reencode.rs`).
|
|
pub(super) gpu_alone: bool,
|
|
/// Where `commit` left it to a host step for what the GPU may write, what the step's
|
|
/// recording may use that Metal orders (`Execute::bound`, `DeferredPass::bound`): its place
|
|
/// in the scheduler's order across queues until the step records it. `None` where `commit`
|
|
/// could not bound it, which orders it before every later batch that makes a tracked use.
|
|
pub(super) bound: Option<Summary<ResourceKey>>,
|
|
}
|
|
|
|
impl Segment {
|
|
/// Whether the host step records it before its batch is submitted: the GPU may write what
|
|
/// its work reads, whether it could at the call or only since (`Deferred::gpu_written` is
|
|
/// for good), or `commit` left it for the pipelines it records, or recorded it from records
|
|
/// the CPU wrote since (`Deferred::rewritten`), or leaves every such work to its step
|
|
/// (`Deferred::records_at_its_step`); or `commit`'s recording on the
|
|
/// GPU alone must be made again ([`rerecords`](Self::rerecords)); or `commit` recorded it on
|
|
/// the GPU for good, and a route encoded since may write more than it decided on, or gives an
|
|
/// execution's check something to see (`Deferred::outgrown`); or its step left work its batch
|
|
/// waits for (`Deferred::holds`).
|
|
pub(super) fn needs_step(&self) -> bool {
|
|
(!self.stepped
|
|
&& (self.host
|
|
|| self.pipelines
|
|
|| self.work.gpu_written()
|
|
|| self.work.rewritten()
|
|
|| self.work.records_at_its_step()))
|
|
|| self.rerecords()
|
|
|| self.work.outgrown()
|
|
|| self.work.holds()
|
|
}
|
|
|
|
/// Whether `commit` recorded it on the GPU alone with no barrier between its commands and its
|
|
/// batch's submission records it again with them, waiting for no work on the GPU
|
|
/// (`gpu_execute::Rerecord`): a route encoded since may set a barrier in a record.
|
|
pub(super) fn rerecords(&self) -> bool {
|
|
self.inputs.as_ref().is_some_and(StepInputs::rerecords)
|
|
}
|
|
|
|
/// Whether its host step waits for the work submitted before it to complete: the GPU may
|
|
/// write what its work reads. A step for its pipelines alone waits for them alone.
|
|
pub(super) fn steps_after_the_gpu(&self) -> bool {
|
|
(!self.stepped && (self.host || self.work.gpu_written())) || self.work.outgrown()
|
|
}
|
|
|
|
/// Whether its host step waits for a pipeline still being made, or its batch for what its
|
|
/// step left (`Deferred::holds`), which wakes the scheduler as it ends.
|
|
pub(super) fn steps_after_pipelines(&self) -> bool {
|
|
(!self.stepped && self.work.making()) || self.work.holds()
|
|
}
|
|
|
|
/// [`steps_after_pipelines`](Self::steps_after_pipelines) once its command buffer's bound
|
|
/// passed (`CommandBuffer::making_deadline`): its step records a draw or dispatch whose
|
|
/// pipeline is still being made as nothing, so its batch waits only for what its step left
|
|
/// (`Deferred::holds`).
|
|
pub(super) fn holds(&self) -> bool {
|
|
self.work.holds()
|
|
}
|
|
|
|
/// Whether it is a dispatch whose pipelines were being made (`pending_dispatch.rs`): an
|
|
/// encoder encoded again (`replay.rs`) leaves one of its own in its place, prepared as the
|
|
/// commit prepared this one, and waits for no making.
|
|
pub(super) fn pends(&self) -> bool {
|
|
matches!(self.work, Deferred::Pending(_))
|
|
}
|
|
|
|
/// Whether an encoder encoded again (`replay.rs`) leaves it as a segment of its own, prepared
|
|
/// as the commit prepared the first encoding's ([`Deferred::prepare`]), rather than recording
|
|
/// it on the GPU alone: a dispatch whose pipelines were being made ([`pends`](Self::pends)),
|
|
/// and a render pass of a draw's or a tile dispatch's pipeline still being made, or whose
|
|
/// making failed (`render/pending.rs`, `render/pending_tile.rs`), which recording on the GPU
|
|
/// alone would wait for, and make itself, on the program's thread
|
|
/// (`DeferredPass::record_on_gpu`).
|
|
pub(super) fn keeps_pipelines(&self) -> bool {
|
|
self.pends() || self.work.making() || self.work.failed()
|
|
}
|
|
|
|
/// Whether an encoder encoded again (`replay.rs`) records its place: a dispatch whose
|
|
/// pipelines were being made ([`pends`](Self::pends)), left as a segment again, work a
|
|
/// commit may record on the GPU alone, recorded there again (`Deferred::record_on_gpu`), or a
|
|
/// run's pass's merge, which the pass encoded again records after itself as a pass alone
|
|
/// does, leaving its run (`pass_runs.rs`), or a staged gather, which the encoding again
|
|
/// copies through the imports in its place (`Deferred::recorded_by_an_encoding_again`). Its
|
|
/// log is kept only where every segment does.
|
|
pub(super) fn records_again(&self) -> bool {
|
|
self.pends()
|
|
|| self.work.may_run_on_gpu_alone()
|
|
|| self.work.joins_a_run()
|
|
|| self.work.recorded_by_an_encoding_again()
|
|
}
|
|
|
|
/// What its recording will use, on `device`, while `commit` left it to the host step for the
|
|
/// pipelines it records alone, or as such work always is (`Deferred::records_at_its_step`),
|
|
/// and not for what the GPU may write: its work's uses, which the encoder knew, only the
|
|
/// `VkPipeline` missing (`Deferred::known_summary`). `None` where the step or the submission
|
|
/// finds them.
|
|
pub(super) fn known_summary(&self, device: &Device) -> Option<Summary<ResourceKey>> {
|
|
let left = self.pipelines || self.work.records_at_its_step();
|
|
match left && !self.steps_after_the_gpu() {
|
|
true => self.work.known_summary(device),
|
|
false => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The `VkCommandBuffer` an encoder goes on in after a segment, begun
|
|
/// ([`EncoderCore::continuation`]), and whether it goes on inside a predicated loop's laying.
|
|
pub(super) struct Continuation(vk::CommandBuffer, Laid);
|
|
|
|
/// Whether a [`Continuation`] goes on inside a predicated loop's laying (`predication.rs`).
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
enum Laid {
|
|
Outside,
|
|
Inside,
|
|
}
|
|
|
|
/// A segment recorded: its `VkCommandBuffer`s, ended, in order, and what it used and ran. One,
|
|
/// but for a render pass that executes render commands whose first instance recorded inline
|
|
/// (`render/executions.rs`): what the pass records before itself, the first instance's own, then
|
|
/// the rest ([`cut_before`](EncoderCore::cut_before)).
|
|
pub(super) struct RecordedSegment {
|
|
pub(super) raws: Vec<vk::CommandBuffer>,
|
|
pub(super) summary: Summary<ResourceKey>,
|
|
pub(super) retained: Retained,
|
|
pub(super) executions: Vec<Execution>,
|
|
/// The keys its commands' uses named (`used_resources.rs`).
|
|
pub(super) keys: BTreeSet<ResourceKey>,
|
|
/// What its commands took of buffers over host pages, and which they named (`rebind.rs`).
|
|
pub(super) taken: Taken,
|
|
/// The resident-set misses its recording found (`indirect_execute.rs`), which the command
|
|
/// buffer's execution report counts.
|
|
pub(super) resident_misses: u32,
|
|
/// Why some of its commands ran nothing: records the host step could not run
|
|
/// (`indirect_execute.rs`), which the execution report states.
|
|
pub(super) faults: ReplayFaults,
|
|
/// The copies taken at use its recording took (`shadow::Takes`), given back where its part
|
|
/// runs nothing (`State::fail_part`).
|
|
pub(super) takes: Takes,
|
|
}
|
|
|
|
impl RecordedSegment {
|
|
/// A segment its batch's submission recorded into `raws` (`command_buffer/submit.rs`): of
|
|
/// uses its encoder noted already, or none.
|
|
pub(super) fn submitted(raws: Vec<vk::CommandBuffer>) -> RecordedSegment {
|
|
RecordedSegment {
|
|
raws,
|
|
summary: EncoderUses::new(true).finish(),
|
|
retained: Retained::default(),
|
|
executions: Vec::new(),
|
|
keys: Default::default(),
|
|
taken: Default::default(),
|
|
resident_misses: 0,
|
|
faults: ReplayFaults::default(),
|
|
takes: Takes::default(),
|
|
}
|
|
}
|
|
|
|
/// `other`, recorded after it, joined into it: one segment of both.
|
|
pub(super) fn join(&mut self, other: RecordedSegment) {
|
|
self.raws.extend(other.raws);
|
|
self.summary.join(other.summary);
|
|
self.retained.join(other.retained);
|
|
self.executions.extend(other.executions);
|
|
self.keys.extend(other.keys);
|
|
self.taken.join(other.taken);
|
|
self.resident_misses = self.resident_misses.wrapping_add(other.resident_misses);
|
|
self.faults = ReplayFaults(self.faults.0 | other.faults.0);
|
|
self.takes.join(other.takes);
|
|
}
|
|
}
|
|
|
|
/// The state every open encoder carries.
|
|
pub(super) struct EncoderCore {
|
|
open: OpenGuard,
|
|
/// The `VkCommandBuffer` its commands record into; null while a pass holds the one before
|
|
/// (`pass`) and no command came since, which begins another ([`recording`](Self::recording)).
|
|
raw: vk::CommandBuffer,
|
|
/// Something recorded into `raw` that a command recorded before a pass must follow: any
|
|
/// command of the encoder's, not the first encoder's start (`gpu_time.rs`), which a pass may
|
|
/// hold the command buffer of ([`hold_pass`](Self::hold_pass)).
|
|
recorded: bool,
|
|
/// The `VkCommandBuffer` a render pass instance records its draws inline in, from the pass's
|
|
/// first draw to its end (`render.rs`, [`hold_pass`](Self::hold_pass)): the encoder's
|
|
/// commands meanwhile record into `raw`, which goes before it in submission order, as they go
|
|
/// before a pass recorded at its end.
|
|
pass: Option<vk::CommandBuffer>,
|
|
/// The `VkCommandBuffer` the encoder opened in, which [`open`](Self::open) began with a
|
|
/// barrier from everything before: past the first [`PLANNED_BARRIERS`] encoders of its
|
|
/// command buffer. Not `recorded`: a pass may hold it, and what records before that pass
|
|
/// goes in another before it, which leads the encoder's then.
|
|
barriered: Option<vk::CommandBuffer>,
|
|
/// The encoder's `VkCommandBuffer`s ended before a segment, in order.
|
|
ended: Vec<vk::CommandBuffer>,
|
|
/// The segments it left for commit, `executions_at` counting its own executions.
|
|
segments: Vec<Segment>,
|
|
/// Its first parts, which a command-predication record left out of the command buffer's
|
|
/// work ([`leave_out_recorded`](Self::leave_out_recorded)): each ends running nothing.
|
|
left_out: usize,
|
|
/// The order of the encoder's commands, and its summary.
|
|
uses: EncoderUses<ResourceKey>,
|
|
/// The uses of the command [`note_reaching`](Self::note_reaching) notes, with those of the
|
|
/// host memory beside them ([`aliased`](Self::aliased)): kept, so a draw makes no vector.
|
|
aliased_uses: Vec<Use<ResourceKey>>,
|
|
/// The keys its commands' uses named, each with the uses of the host memory's it takes, while
|
|
/// nothing joined, moved or went since (`host_aliases::Quiet`): a command over those takes no
|
|
/// lock.
|
|
aliases_quiet: Quiet,
|
|
/// The open runs of the index gather pass ([`gather`](Self::gather)), which record before
|
|
/// the encoder's next command, and before its `VkCommandBuffer` ends.
|
|
gathers: GatherRuns,
|
|
/// What the encoder's commands use; `end` hands it to the command buffer.
|
|
retained: Retained,
|
|
/// What its dispatches and draws executed; `end` hands it to the command buffer.
|
|
executions: Vec<Execution>,
|
|
/// Where the shadowed textures the encoder used stand.
|
|
shadows: ShadowSync,
|
|
/// A render encoder's turns of its pass's colors' textures to their other faces, its pass's
|
|
/// open's (`render/face_swap.rs`); none for any other encoder.
|
|
turns: Turns,
|
|
/// The keys its commands' uses named, and its bindings' (`used_resources.rs`): `end` finds the
|
|
/// heaps among them, and hands them to the command buffer.
|
|
keys: BTreeSet<ResourceKey>,
|
|
/// The key of each use the last command [`touch_reaching`](Self::touch_reaching) touched took,
|
|
/// in order, and whether it named it (`Taken::name`): both only grow while the encoder is
|
|
/// open, so a use at that place of the same key, named only where that one was, is kept and
|
|
/// named already.
|
|
touched: Vec<(ResourceKey, bool)>,
|
|
/// The buffers the encoder made resident since its last command (`made_resident`), which its
|
|
/// next command holds.
|
|
resident_unheld: Vec<ResourceKey>,
|
|
/// The shadowed textures its pass took as attachments (`render/shadows.rs`): `end` notes them
|
|
/// in the command buffer (`WorkCounts::shadow_attachment_passes`).
|
|
attachment_shadows: Vec<ResourceKey>,
|
|
/// Its first copies in of shadowed textures, each copy's texture and texels: `end` notes them
|
|
/// in the command buffer (`WorkCounts::shadow_texels_first_copied_in_again`).
|
|
first_copies: Vec<(ResourceKey, u64)>,
|
|
/// Its holds on the kept images its merges took from its queue (`texture/kept_image.rs`),
|
|
/// and on the scratch they took from its command buffer (`merge_scratch.rs`), each with the
|
|
/// merge that took it: no keep of another encoder takes one before this one ends, its merges
|
|
/// done, or a run of passes that took it over ends (`pass_runs.rs`).
|
|
kept_leases: Vec<(Weak<ShadowMerge>, MergeLease)>,
|
|
/// The heaps its commands used through what the encoder made resident; `end` hands them,
|
|
/// and those of `keys`, to the command buffer, whose commit marks them used.
|
|
heaps: Vec<Heap>,
|
|
/// Its holds on the buffers over host pages its commands used (`rebind.rs`): `end` hands
|
|
/// them to the command buffer, whose commit lets go of them.
|
|
holds: Holds,
|
|
/// The pages its commands took of those buffers, and the keys a command named: `end` hands
|
|
/// them to the command buffer, which holds the pages until it completes (weakly from its
|
|
/// commit to its first submission).
|
|
taken: Taken,
|
|
/// Metal fails the command buffer from this encoder on with this error (a render pass
|
|
/// without attachments past 16384 a side, or at a default sample count Metal fails it at):
|
|
/// `end` says so to the command buffer, whose commit runs nothing from it on
|
|
/// (`State::fails_from`).
|
|
fails_command_buffer: Option<CommandBufferError>,
|
|
/// Metal's GPU faults this encoder, with this error (an execution of commands that inherit a
|
|
/// pipeline state made without `supportIndirectCommandBuffers`, an indexed or indirect draw
|
|
/// of primitive type 9): `end` says so to the command buffer, whose commit runs nothing of it
|
|
/// (`State::fault`).
|
|
faults: Option<CommandBufferError>,
|
|
/// Its writes into ASTC carriers ([`wrote_twin`](Self::wrote_twin)): `end` hands them to the
|
|
/// command buffer with its last part.
|
|
twin_writes: Vec<TwinWrite>,
|
|
/// The commands it records now were called while no buffer of the device was detached: a
|
|
/// render encoder lowers calls it logged (`render/deferral.rs`), whose detachment they judge
|
|
/// as of their calls ([`judges_unattached`](Self::judges_unattached)).
|
|
unattached_as_called: bool,
|
|
/// The core's own number in the process ([`id`](Self::id)).
|
|
id: u64,
|
|
/// It encodes an ended encoder's calls again, its command buffer committed (`replay.rs`), or
|
|
/// records again work a commit recorded (`command_buffer/reencode.rs`): its commands take
|
|
/// what they use without holding it (`Rebinds::pin`).
|
|
replaying: bool,
|
|
/// The set 0 its last command allocated and wrote, which a later command that writes the same
|
|
/// into a set of the same layout binds again ([`set_zero`](Self::set_zero)).
|
|
last_set_zero: LastSetZero,
|
|
/// What it made resident for writing, set and executed that decides whether the GPU may
|
|
/// write an indirect command buffer's records, which its command buffer's commit reads
|
|
/// (`write_routes.rs`).
|
|
pub(super) routes: Routes,
|
|
/// Where its draws note the uses of what their encoder made resident
|
|
/// ([`note_draw`](Self::note_draw)).
|
|
resident_notes: ResidentNotes,
|
|
/// The last [`ResidentPart`] it noted, by identity, and the state it noted it into
|
|
/// ([`note_resident_part`](Self::note_resident_part)).
|
|
resident_noted: Option<(ReachingPrefix, NotedInto)>,
|
|
/// The tally its commands count into while it is open (`shards.rs`), given back as it drops:
|
|
/// [`counts`](Self::counts).
|
|
tally: Tally,
|
|
_not_sync: PhantomData<std::cell::Cell<()>>,
|
|
}
|
|
|
|
/// The uses of what a render encoder made resident that begin the uses of the draws of an
|
|
/// execution's chunks recorded apart (`render/executions/kept.rs`), by identity
|
|
/// (`binding::ReachingPrefix`: two draws of the same begin with the same uses and reach the same
|
|
/// keys through the resident set alone), with those keys. The core of each chunk notes its
|
|
/// draws' uses past them (a core of [`EncoderCore::capture_apart`]), and the core of the chunks'
|
|
/// pass notes them once ([`EncoderCore::absorb`], [`EncoderCore::run_kept`]), as the core of an
|
|
/// execution recorded inline notes them at its first draw alone (`noted_apart.rs`).
|
|
pub(super) struct ResidentPart {
|
|
reaching: ReachingPrefix,
|
|
uses: Vec<Use<ResourceKey>>,
|
|
resident: BTreeSet<ResourceKey>,
|
|
}
|
|
|
|
/// Where a core's draws note the uses of what their encoder made resident.
|
|
enum ResidentNotes {
|
|
/// In the core.
|
|
Here,
|
|
/// In the core of the pass whose execution's chunk it records
|
|
/// ([`EncoderCore::capture_apart`]): those of the part, or of the first draw's that begin
|
|
/// with some, which becomes the part; a draw that begins with others notes them here.
|
|
Apart(Option<Arc<ResidentPart>>),
|
|
}
|
|
|
|
/// An encoder encoded again (`EncoderCore::replaying`), ended: what its place among its command
|
|
/// buffer's recordings takes in place of what the encoder recorded (`State::splice_replay`).
|
|
pub(super) struct Replayed {
|
|
/// Its parts, as its first encoding's: a segment the GPU may write what it reads for starts
|
|
/// one of its own.
|
|
pub(super) parts: Vec<ReplayedPart>,
|
|
/// The images it holds in their attachment layout (`texture/held_depth.rs`).
|
|
pub(super) held: Vec<ResourceKey>,
|
|
pub(super) retained: Retained,
|
|
pub(super) executions: Vec<Execution>,
|
|
pub(super) keys: BTreeSet<ResourceKey>,
|
|
pub(super) taken: Taken,
|
|
pub(super) heaps: Vec<Heap>,
|
|
}
|
|
|
|
/// One part of an encoder encoded again ([`Replayed::parts`]): its `VkCommandBuffer`s, what they
|
|
/// use, and its segments, each recorded at once on the GPU alone, its place among the
|
|
/// executions counted from the encoder's first.
|
|
pub(super) struct ReplayedPart {
|
|
pub(super) raws: Vec<vk::CommandBuffer>,
|
|
pub(super) summary: Summary<ResourceKey>,
|
|
pub(super) segments: Vec<Segment>,
|
|
}
|
|
|
|
/// An ended encoder's `VkCommandBuffer`s `raws`, in order, and its `segments`, in parts: a segment
|
|
/// the GPU may write what it reads for starts a part of its own (the module's segments), each
|
|
/// segment's place counted among its part's `VkCommandBuffer`s, and its place among the
|
|
/// executions from `base`, the encoder's first.
|
|
fn into_parts(
|
|
raws: Vec<vk::CommandBuffer>,
|
|
segments: Vec<Segment>,
|
|
base: usize,
|
|
) -> Vec<(Vec<vk::CommandBuffer>, Vec<Segment>)> {
|
|
let mut raws = raws.into_iter();
|
|
// The raws the parts before the current one took.
|
|
let mut taken = 0;
|
|
let mut parts = Vec::new();
|
|
let mut current = Vec::new();
|
|
for mut segment in segments {
|
|
segment.executions_at += base;
|
|
if segment.host {
|
|
let before = segment.at - taken;
|
|
let finished = std::mem::take(&mut current);
|
|
parts.push((raws.by_ref().take(before).collect::<Vec<_>>(), finished));
|
|
taken = segment.at;
|
|
}
|
|
segment.at -= taken;
|
|
current.push(segment);
|
|
}
|
|
parts.push((raws.collect(), current));
|
|
parts
|
|
}
|
|
|
|
/// The open encoder's hold on its command buffer: dropped before `end` ran, it marks the command
|
|
/// buffer abandoned. It is apart from [`EncoderCore`] so `end` can take the core apart, and the
|
|
/// resource state encoder holds one too.
|
|
pub(super) struct OpenGuard {
|
|
pub(super) command_buffer: CommandBuffer,
|
|
/// `end` ran: the drop does not abandon the command buffer.
|
|
pub(super) ended: bool,
|
|
}
|
|
|
|
impl Drop for OpenGuard {
|
|
fn drop(&mut self) {
|
|
if !self.ended {
|
|
let mut state = self.command_buffer.state();
|
|
state.encoder_open = false;
|
|
state.abandoned = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Refuse an object of `other` in Metal's `call` of an encoder of `device` unless `other` is
|
|
/// `device`: an encoder's check ([`EncoderCore::check_device`]), and a render encoder's at the call
|
|
/// (`render/call_checks.rs`). Inlined, its refusal out of line (`Device::foreign`): a bind
|
|
/// makes it.
|
|
#[inline(always)]
|
|
pub(super) fn check_device(
|
|
device: &Device,
|
|
call: &'static str,
|
|
other: &Device,
|
|
) -> Result<(), Error> {
|
|
match device.same(other) {
|
|
true => Ok(()),
|
|
false => Err(other.foreign(call, device)),
|
|
}
|
|
}
|
|
|
|
impl EncoderCore {
|
|
/// The core in a box its device kept from an ended encoder's ([`SpareCores`]), else a new one.
|
|
pub(super) fn boxed(self) -> Box<EncoderCore> {
|
|
let room = self.device().shared().cores.take();
|
|
Box::write(room, self)
|
|
}
|
|
|
|
/// The core out of its box, `core`, which its device keeps emptied for the next
|
|
/// ([`SpareCores`]).
|
|
pub(super) fn unboxed(core: Box<EncoderCore>) -> EncoderCore {
|
|
let raw = Box::into_raw(core);
|
|
// SAFETY: `raw` is a box's pointer, its core live and read out once, here; the box is then
|
|
// made again over the same allocation as room for a core (`MaybeUninit<EncoderCore>` has
|
|
// `EncoderCore`'s layout), so the core is neither dropped through it nor read again.
|
|
let (core, room) = unsafe {
|
|
(
|
|
raw.read(),
|
|
Box::from_raw(raw.cast::<MaybeUninit<EncoderCore>>()),
|
|
)
|
|
};
|
|
core.device().shared().cores.give(room);
|
|
core
|
|
}
|
|
|
|
/// Open an encoder on `command_buffer` for Metal's `call`, `ordered` when Metal runs its
|
|
/// commands in encoded order (a blit or serial compute encoder). Refused when the command
|
|
/// buffer is committed, has an open encoder, or has an abandoned one.
|
|
pub(super) fn open(
|
|
command_buffer: &CommandBuffer,
|
|
call: &'static str,
|
|
kind: EncoderKind,
|
|
ordered: bool,
|
|
) -> Result<EncoderCore, Error> {
|
|
// The last render encoder's end, where its call left it to the device's lowering thread.
|
|
command_buffer.settle_end();
|
|
let mut state = command_buffer.state();
|
|
if let Some(problem) = state.refusal() {
|
|
return Err(invalid(call, problem));
|
|
}
|
|
// The pool's primary begun ahead where it holds one (`spares.rs`).
|
|
let (raw, begun) = command_buffer.encoder_primary(&mut state)?;
|
|
let shared = command_buffer.device().shared();
|
|
if !begun {
|
|
let begin = vk::CommandBufferBeginInfo::default()
|
|
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);
|
|
// SAFETY: the command buffer was just handed out of the pool, under the state lock.
|
|
unsafe { shared.vk().begin_command_buffer(raw, &begin) }.map_err(vulkan)?;
|
|
}
|
|
shared.counts.encoder_command_buffer_begun(Begun::Encoder);
|
|
if begun {
|
|
shared.counts.encoder_primary_begun_ahead();
|
|
}
|
|
// The first encoder of a timed command buffer writes its start first (`gpu_time.rs`).
|
|
command_buffer.open_start(&mut state, raw);
|
|
// Past the encoders whose barriers the scheduler plans, a barrier from everything before
|
|
// leads the encoder's commands (the module docs).
|
|
let barriered = (state.encoders_ended() >= PLANNED_BARRIERS).then(|| {
|
|
// SAFETY: the command buffer was just begun, outside a render pass, under the state
|
|
// lock; only this encoder records it.
|
|
unsafe { Barrier::everything().record(shared.vk(), raw) };
|
|
shared.counts.encoder_barriered_at_open();
|
|
raw
|
|
});
|
|
state.encoder_open = true;
|
|
// Made under the protection options set now (`command_buffer/protection.rs`).
|
|
state.protected_made();
|
|
// What the command buffer's earlier encoders left shadowed textures' images holding,
|
|
// which this one's first uses of them take instead of copying the truth in again
|
|
// (`shadow::ShadowCarry`).
|
|
let carry = state.shadows_to_carry();
|
|
let routes = state.routes.carried();
|
|
drop(state);
|
|
command_buffer
|
|
.device()
|
|
.shared()
|
|
.identifiers
|
|
.encoder_opened(kind);
|
|
Ok(EncoderCore {
|
|
open: OpenGuard {
|
|
command_buffer: command_buffer.clone(),
|
|
ended: false,
|
|
},
|
|
raw,
|
|
recorded: false,
|
|
pass: None,
|
|
barriered,
|
|
ended: Vec::new(),
|
|
segments: Vec::new(),
|
|
left_out: 0,
|
|
uses: EncoderUses::new(ordered),
|
|
retained: Retained::default(),
|
|
executions: Vec::new(),
|
|
shadows: ShadowSync::carrying(carry),
|
|
turns: Turns::default(),
|
|
keys: BTreeSet::new(),
|
|
touched: Vec::new(),
|
|
resident_unheld: Vec::new(),
|
|
attachment_shadows: Vec::new(),
|
|
first_copies: Vec::new(),
|
|
kept_leases: Vec::new(),
|
|
heaps: Vec::new(),
|
|
holds: Holds::new(command_buffer.device()),
|
|
taken: Taken::default(),
|
|
fails_command_buffer: None,
|
|
faults: None,
|
|
twin_writes: Vec::new(),
|
|
unattached_as_called: false,
|
|
aliased_uses: Vec::new(),
|
|
// The keys an ended encoder found, which hold here while the version stands.
|
|
aliases_quiet: command_buffer.generation().aliases.quiet(),
|
|
gathers: GatherRuns::default(),
|
|
id: CORES.fetch_add(1, Ordering::Relaxed),
|
|
replaying: false,
|
|
last_set_zero: LastSetZero::default(),
|
|
routes,
|
|
resident_notes: ResidentNotes::Here,
|
|
resident_noted: None,
|
|
tally: Tally::take(),
|
|
_not_sync: PhantomData,
|
|
})
|
|
}
|
|
|
|
/// The core a segment of `command_buffer` records into at its commit, `ordered` as the
|
|
/// encoder that left it: a new `VkCommandBuffer` of its pool, begun, its first command a
|
|
/// barrier from everything recorded before. Recorded, it ends with
|
|
/// [`end_segment`](Self::end_segment); dropped, it abandons nothing.
|
|
pub(super) fn segment(
|
|
command_buffer: &CommandBuffer,
|
|
ordered: bool,
|
|
) -> Result<EncoderCore, Error> {
|
|
let mut core = EncoderCore::segment_raw(command_buffer, ordered)?;
|
|
// SAFETY: the command buffer is recording, and only this core records it.
|
|
unsafe { Barrier::everything().record(core.device().shared().vk(), core.raw) };
|
|
core.recorded = true;
|
|
Ok(core)
|
|
}
|
|
|
|
/// The core an ended encoder of `command_buffer`, committed and not yet first submitted, is
|
|
/// encoded again into (`replay.rs`), `ordered` as that encoder: a new `VkCommandBuffer` of
|
|
/// its pool, begun, as [`open`](Self::open) begins one, without its refusals. Ended with
|
|
/// [`finish_replay`](Self::finish_replay); dropped, it abandons nothing.
|
|
pub(super) fn replaying(
|
|
command_buffer: &CommandBuffer,
|
|
ordered: bool,
|
|
) -> Result<EncoderCore, Error> {
|
|
let mut core = EncoderCore::segment_raw(command_buffer, ordered)?;
|
|
core.replaying = true;
|
|
// Its first uses of copies taken at use take them where its first encoding's did, whose
|
|
// recording is never submitted (`shadow::Takes`, `command_buffer/reencode.rs`).
|
|
let first = command_buffer.state().replaying_takes.clone();
|
|
core.shadows = ShadowSync::again(first);
|
|
Ok(core)
|
|
}
|
|
|
|
/// A core of `command_buffer` over a new `VkCommandBuffer` of its pool, begun, that abandons
|
|
/// nothing when dropped.
|
|
fn segment_raw(command_buffer: &CommandBuffer, ordered: bool) -> Result<EncoderCore, Error> {
|
|
let (raw, keeping, replaced) = {
|
|
let mut state = command_buffer.state();
|
|
let raw = command_buffer.allocate_raw(&mut state)?;
|
|
(raw, state.keeping(), state.step_takes.clone())
|
|
};
|
|
let device = command_buffer.device().shared().vk();
|
|
// A chunk kept for later command buffers (`kept_commands.rs`) may run in several at once.
|
|
let submits = match keeping {
|
|
true => vk::CommandBufferUsageFlags::SIMULTANEOUS_USE,
|
|
false => vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT,
|
|
};
|
|
let begin = vk::CommandBufferBeginInfo::default().flags(submits);
|
|
// SAFETY: the command buffer was just allocated from the pool, and only this core
|
|
// records it.
|
|
unsafe { device.begin_command_buffer(raw, &begin) }.map_err(vulkan)?;
|
|
command_buffer
|
|
.device()
|
|
.shared()
|
|
.counts
|
|
.encoder_command_buffer_begun(Begun::Segment);
|
|
let mut core = EncoderCore::over(command_buffer, raw, ordered);
|
|
// A host step's recording keeps the takes of the recording it replaces
|
|
// (`State::step_takes`); none elsewhere.
|
|
core.shadows = ShadowSync::over(replaced);
|
|
Ok(core)
|
|
}
|
|
|
|
/// A core that records apart for a segment of `command_buffer`, unordered (`kept_commands.rs`):
|
|
/// a render execution's chunk, its draws into a secondary of their own, which notes, holds and
|
|
/// counts apart from the segment's core until that core takes what it did
|
|
/// ([`absorb`](Self::absorb)). It has no `VkCommandBuffer` until a command records outside the
|
|
/// draws' secondaries; dropped, it abandons nothing.
|
|
pub(super) fn capture(command_buffer: &CommandBuffer) -> EncoderCore {
|
|
EncoderCore::over(command_buffer, vk::CommandBuffer::null(), false)
|
|
}
|
|
|
|
/// A [`capture`](Self::capture) core for a chunk of a render execution's commands recorded
|
|
/// apart, whose draws leave the uses of what their encoder made resident to the core that
|
|
/// takes what it did ([`ResidentPart`]): `resident`'s, the chunk before's, or where it is
|
|
/// `None` those its first draw that begins with some begins with.
|
|
pub(super) fn capture_apart(
|
|
command_buffer: &CommandBuffer,
|
|
resident: Option<Arc<ResidentPart>>,
|
|
) -> EncoderCore {
|
|
let mut core = EncoderCore::capture(command_buffer);
|
|
core.resident_notes = ResidentNotes::Apart(resident);
|
|
core
|
|
}
|
|
|
|
/// The uses of what its encoder made resident its draws left to the core that takes what it
|
|
/// did, where they left some ([`capture_apart`](Self::capture_apart)).
|
|
pub(super) fn resident_part(&self) -> Option<Arc<ResidentPart>> {
|
|
match &self.resident_notes {
|
|
ResidentNotes::Apart(part) => part.clone(),
|
|
ResidentNotes::Here => None,
|
|
}
|
|
}
|
|
|
|
/// Note `part`'s uses, those of what an encoder made resident that the cores of an
|
|
/// execution's chunks left to this one ([`ResidentPart`]): once while it notes into the same
|
|
/// state (`NotedInto`), as a draw noted after one that began with the same notes them no
|
|
/// more (`noted_apart.rs`; `WorkCounts::resident_uses_noted` counts the ones walked).
|
|
fn note_resident_part(&mut self, part: &ResidentPart) -> Result<(), Error> {
|
|
let into = self.noted_into();
|
|
if self.resident_noted == Some((part.reaching, into)) {
|
|
return Ok(());
|
|
}
|
|
let stages = part.reaching.stages;
|
|
self.note_reaching(stages, (&part.uses, &part.resident), None)?;
|
|
self.counts().resident_uses_noted(part.uses.len() as u64);
|
|
self.resident_noted = Some((part.reaching, into));
|
|
Ok(())
|
|
}
|
|
|
|
/// A core of `command_buffer` over `raw`, begun, or none (null), recording nothing yet, that
|
|
/// abandons nothing when dropped.
|
|
fn over(command_buffer: &CommandBuffer, raw: vk::CommandBuffer, ordered: bool) -> EncoderCore {
|
|
EncoderCore {
|
|
open: OpenGuard {
|
|
command_buffer: command_buffer.clone(),
|
|
ended: true,
|
|
},
|
|
raw,
|
|
recorded: false,
|
|
pass: None,
|
|
barriered: None,
|
|
ended: Vec::new(),
|
|
segments: Vec::new(),
|
|
left_out: 0,
|
|
uses: EncoderUses::new(ordered),
|
|
retained: Retained::default(),
|
|
executions: Vec::new(),
|
|
shadows: ShadowSync::default(),
|
|
turns: Turns::default(),
|
|
keys: BTreeSet::new(),
|
|
touched: Vec::new(),
|
|
resident_unheld: Vec::new(),
|
|
attachment_shadows: Vec::new(),
|
|
first_copies: Vec::new(),
|
|
kept_leases: Vec::new(),
|
|
heaps: Vec::new(),
|
|
holds: Holds::new(command_buffer.device()),
|
|
taken: Taken::default(),
|
|
fails_command_buffer: None,
|
|
faults: None,
|
|
twin_writes: Vec::new(),
|
|
unattached_as_called: false,
|
|
aliased_uses: Vec::new(),
|
|
aliases_quiet: Quiet::default(),
|
|
gathers: GatherRuns::default(),
|
|
id: CORES.fetch_add(1, Ordering::Relaxed),
|
|
// A segment a rebind records again, its command buffer committed.
|
|
replaying: crate::replay::is_replaying(),
|
|
last_set_zero: LastSetZero::default(),
|
|
routes: Routes::default(),
|
|
resident_notes: ResidentNotes::Here,
|
|
resident_noted: None,
|
|
tally: Tally::take(),
|
|
_not_sync: PhantomData,
|
|
}
|
|
}
|
|
|
|
/// The device.
|
|
pub(super) fn device(&self) -> &Device {
|
|
self.open.command_buffer.device()
|
|
}
|
|
|
|
/// The state of its device's generation (`CommandBuffer::generation`): what its commands
|
|
/// reach, at one load.
|
|
#[inline]
|
|
pub(super) fn generation(&self) -> &DeviceShared {
|
|
self.open.command_buffer.generation()
|
|
}
|
|
|
|
/// The counts its commands add, into its tally (`work_counts::CommandCounters`).
|
|
#[inline]
|
|
pub(super) fn counts(&self) -> Counting<'_> {
|
|
self.generation().counts.of(&self.tally)
|
|
}
|
|
|
|
/// Set 0 of a command of `objects`'s variant that writes `writes` (set 0's), and `offsets`, the
|
|
/// dynamic offsets the command binds it with: the writes of the bindings the variant makes
|
|
/// dynamic made so first (`binding::dynamic_writes`), each at offset 0 and its offset among
|
|
/// `offsets`, so commands that differ in those offsets alone write the same set. The set the
|
|
/// encoder's last command allocated, where it is of the same layout and holds the same writes
|
|
/// (`binding::LastSetZero`), else such a set an earlier command of the command buffer
|
|
/// allocated (`binding::SetCache`), else one allocated from the command buffer's pools and
|
|
/// written, which the caller keeps for later commands once the command records
|
|
/// ([`keep_set_zero`](Self::keep_set_zero)). `None` where the variant allocates none
|
|
/// (`VariantObjects::set_zero`), its writes then refused as `binding::write_sets` refuses
|
|
/// them.
|
|
pub(super) fn set_zero(
|
|
&mut self,
|
|
objects: &VariantObjects,
|
|
(writes, offsets): (&mut [Write], &mut Vec<u32>),
|
|
) -> Result<(Option<vk::DescriptorSet>, Option<WrittenSet>), Error> {
|
|
binding::dynamic_writes(writes, objects.dynamic_bindings(), offsets);
|
|
self.set_zero_of(objects, writes)
|
|
}
|
|
|
|
/// [`set_zero`](Self::set_zero)'s set for `writes`, made dynamic already
|
|
/// (`binding::dynamic_writes`).
|
|
pub(super) fn set_zero_of(
|
|
&mut self,
|
|
objects: &VariantObjects,
|
|
writes: &mut [Write],
|
|
) -> Result<(Option<vk::DescriptorSet>, Option<WrittenSet>), Error> {
|
|
let layout = objects.set_zero_layout();
|
|
if let Some(layout) = layout {
|
|
if let Some(set) = self.last_set_zero.holds(layout, writes) {
|
|
self.counts().set_zero_set_reused();
|
|
return Ok((Some(set), None));
|
|
}
|
|
let found = (self.command_buffer().state().descriptors).find(layout, writes);
|
|
if let Some(set) = found {
|
|
self.counts().set_zero_set_found();
|
|
self.last_set_zero.keep(layout, set, writes);
|
|
return Ok((Some(set), None));
|
|
}
|
|
}
|
|
let set = objects.set_zero(&mut self.command_buffer().state().descriptors)?;
|
|
// SAFETY: the set was just allocated and no command uses it yet; every buffer, view and
|
|
// sampler written is live (the command buffer retains them, and a refusal leaves the set
|
|
// unused in its pool).
|
|
unsafe { binding::write_sets(self.device(), set.as_slice(), writes) }?;
|
|
let written = match (layout, set) {
|
|
(Some(layout), Some(set)) => Some(WrittenSet { layout, set }),
|
|
_ => None,
|
|
};
|
|
Ok((set, written))
|
|
}
|
|
|
|
/// The set 0 the encoder's last command took (`binding::LastSetZero`), where it is of `layout`
|
|
/// and holds `writes` (builds with debug assertions).
|
|
#[cfg(debug_assertions)]
|
|
pub(super) fn last_set_zero(
|
|
&self,
|
|
layout: vk::DescriptorSetLayout,
|
|
writes: &[Write],
|
|
) -> Option<vk::DescriptorSet> {
|
|
self.last_set_zero.holds(layout, writes)
|
|
}
|
|
|
|
/// Keep `written`, the set 0 a command that records allocated and wrote with `writes`
|
|
/// ([`set_zero`](Self::set_zero)), for the encoder's next command (`binding::LastSetZero`)
|
|
/// and the command buffer's later ones (`binding::SetCache`): kept only once the command
|
|
/// records, so every object its writes name is held until the command buffer completes.
|
|
pub(super) fn keep_set_zero(&mut self, written: WrittenSet, writes: &[Write]) {
|
|
self.last_set_zero.keep(written.layout, written.set, writes);
|
|
(self.command_buffer().state().descriptors).keep(written.layout, written.set, writes);
|
|
}
|
|
|
|
/// The queue flags of the family its command buffer records for: its queue's `VkQueue`'s.
|
|
pub(super) fn queue_flags(&self) -> vk::QueueFlags {
|
|
let queue = self.open.command_buffer.queue_place().vk_queue();
|
|
self.device().shared().queue_flags_of(queue)
|
|
}
|
|
|
|
/// The core's own number, which no other core of the process has: what an argument table
|
|
/// retained into it holds until it changes (`ArgumentTable::retain`).
|
|
pub(super) fn id(&self) -> u64 {
|
|
self.id
|
|
}
|
|
|
|
/// The command buffer the encoder records into.
|
|
pub(super) fn command_buffer(&self) -> &CommandBuffer {
|
|
&self.open.command_buffer
|
|
}
|
|
|
|
/// This thread doing the work of the command buffer the encoder records into until the guard
|
|
/// goes: the notices announced meanwhile name it (`CommandBuffer::notices_for`).
|
|
pub(super) fn notices_for(&self) -> NoticesFor {
|
|
self.open.command_buffer.notices_for()
|
|
}
|
|
|
|
/// Refuse an object of `device` in Metal's `call` unless `device` is the encoder's.
|
|
#[inline(always)]
|
|
pub(super) fn check_device(&self, call: &'static str, device: &Device) -> Result<(), Error> {
|
|
check_device(self.device(), call, device)
|
|
}
|
|
|
|
/// Hold `object` until the command buffer completes: the encoder's commands use it.
|
|
pub(super) fn retain(&mut self, object: impl Any + Send + Sync) {
|
|
self.retained.boxed(Box::new(object));
|
|
}
|
|
|
|
/// A command of the encoder wrote `write` into an ASTC carrier's blocks: its batch's
|
|
/// submission decodes it into the image's second decoded twin (`texture/decoded_twin.rs`).
|
|
pub(super) fn wrote_twin(&mut self, write: TwinWrite) {
|
|
self.twin_writes.push(write);
|
|
}
|
|
|
|
/// Hold `object`, a shared one (`Buffer::held` and its kin), until the command buffer
|
|
/// completes: by another reference to it, without a box of its own (`retained.rs`).
|
|
pub(super) fn retain_shared(&mut self, object: Arc<dyn Any + Send + Sync>) {
|
|
self.retained.shared(object);
|
|
}
|
|
|
|
/// `buffer`'s `VkBuffer`, and where its byte `offset` lies in it, for a blit's command
|
|
/// ([`Buffer::blit_at`]): a no-copy buffer's import the device released while idle is
|
|
/// imported again first, and what the command reads of it held until the command buffer
|
|
/// completes (`host_residency.rs`). Refused where the device cannot import it again.
|
|
///
|
|
/// The buffer itself is held until the command buffer completes too, whatever path records
|
|
/// the command (a per-sample copy's plan holds only the raw `VkBuffer`): its `VkBuffer`, and a
|
|
/// no-copy buffer's import of the caller's pages, go only once nothing holds the buffer
|
|
/// (`HostImport`'s drop), so a program that lets go of it once it committed (as Metal lets it,
|
|
/// a command buffer retaining what its commands use) frees nothing a command in flight writes.
|
|
pub(super) fn blit_at(
|
|
&mut self,
|
|
buffer: &Buffer,
|
|
offset: u64,
|
|
) -> Result<(vk::Buffer, u64), Error> {
|
|
let (at, hold) = buffer.blit_at(offset)?;
|
|
if let Some(hold) = hold {
|
|
self.taken.hold_import(hold);
|
|
}
|
|
self.retain_shared(buffer.held());
|
|
Ok(at)
|
|
}
|
|
|
|
/// Add a dispatch or draw the encoder took to the command buffer's execution record, as
|
|
/// `execution` builds it. Only where the device keeps records
|
|
/// ([`records_executions`](Self::records_executions)): elsewhere nothing is built.
|
|
pub(super) fn executed(&mut self, execution: impl FnOnce() -> Execution) {
|
|
if self.records_executions() {
|
|
self.executions.push(execution());
|
|
}
|
|
}
|
|
|
|
/// Whether the device keeps execution records (`DeviceOptions::record_executions`).
|
|
pub(super) fn records_executions(&self) -> bool {
|
|
self.device().shared().record_executions
|
|
}
|
|
|
|
/// Record a barrier the program asked for (a compute encoder's memory barriers).
|
|
pub(super) fn barrier(&mut self, barrier: Barrier) {
|
|
let Some(raw) = self.recording() else {
|
|
return;
|
|
};
|
|
// SAFETY: the encoder's command buffer is recording, and only this encoder records it.
|
|
unsafe { barrier.record(self.device().shared().vk(), raw) };
|
|
}
|
|
|
|
/// `request`'s direct indexed draw joins an open run of the index gather pass, which records
|
|
/// before the encoder's next command (`index_rewrite.rs`): where its list lies, or `None`
|
|
/// where no run takes it. Its reads `uses` are tracked here, the run's barriers before and
|
|
/// after it ordering them. Refused as the run's objects are refused.
|
|
pub(super) fn gather(
|
|
&mut self,
|
|
request: GatherRequest<'_>,
|
|
uses: &[Use<ResourceKey>],
|
|
) -> Result<Option<GatheredList>, Error> {
|
|
if self.gathers.full() {
|
|
self.flush_gathers();
|
|
}
|
|
let device = self.device().clone();
|
|
let joined = {
|
|
let mut state = self.open.command_buffer.state();
|
|
self.gathers.join(&device, &mut state.uploads, request)?
|
|
};
|
|
let Some((list, held)) = joined else {
|
|
return Ok(None);
|
|
};
|
|
self.joined(held, uses);
|
|
self.counts().gathered_draw();
|
|
Ok(Some(list))
|
|
}
|
|
|
|
/// `request`'s indirect draw joins an open indirect run of the index gather pass, which
|
|
/// records before the encoder's next command (`index_rewrite.rs`): where its list and command
|
|
/// lie, or `None` where no run takes it. Its reads `uses` are tracked here, the run's barriers
|
|
/// before and after it ordering them. Refused as the run's objects, the command buffer's
|
|
/// index pool and its status words are refused.
|
|
pub(super) fn gather_indirect(
|
|
&mut self,
|
|
request: IndirectGatherRequest<'_>,
|
|
uses: &[Use<ResourceKey>],
|
|
) -> Result<Option<CommandedList>, Error> {
|
|
if self.gathers.full() {
|
|
self.flush_gathers();
|
|
}
|
|
let device = self.device().clone();
|
|
let queue = self.open.command_buffer.queue_place().id();
|
|
let joined = {
|
|
let mut state = self.open.command_buffer.state();
|
|
let pool = state.index_pool(&device, queue)?;
|
|
let status = state.status_words(&device)?;
|
|
self.gathers
|
|
.join_indirect(&device, (&mut state.uploads, &status, &pool), request)?
|
|
};
|
|
let Some((list, held)) = joined else {
|
|
return Ok(None);
|
|
};
|
|
self.joined(held, uses);
|
|
self.counts().gathered_draw();
|
|
Ok(Some(list))
|
|
}
|
|
|
|
/// `request`'s direct indexed draw joins an open run of the index rewrite pass, which records
|
|
/// before the encoder's next command (`index_rewrite.rs`): where its list and command lie, or
|
|
/// `None` where no run takes it. Its reads `uses` are tracked here, the run's barriers before
|
|
/// and after it ordering them. Refused as the run's objects and the command buffer's status
|
|
/// words are refused.
|
|
pub(super) fn rewrite_run(
|
|
&mut self,
|
|
request: RewriteRunRequest<'_>,
|
|
uses: &[Use<ResourceKey>],
|
|
) -> Result<Option<CommandedList>, Error> {
|
|
if self.gathers.full() {
|
|
self.flush_gathers();
|
|
}
|
|
let device = self.device().clone();
|
|
let joined = {
|
|
let mut state = self.open.command_buffer.state();
|
|
let status = state.status_words(&device)?;
|
|
self.gathers
|
|
.join_rewrite(&device, (&mut state.uploads, &status), request)?
|
|
};
|
|
let Some((list, held)) = joined else {
|
|
return Ok(None);
|
|
};
|
|
self.joined(held, uses);
|
|
self.counts().gathered_draw();
|
|
Ok(Some(list))
|
|
}
|
|
|
|
/// `request`'s indexed indirect draw joins an open run of the index bounds pass, which
|
|
/// records before the encoder's next command (`index_bounds.rs`): its slot, or `None` where
|
|
/// no run takes it. Its reads `uses` are tracked here, the run's barriers before and after it
|
|
/// ordering them. Refused as the run's objects, the command buffer's index pool and its
|
|
/// status words are refused.
|
|
pub(super) fn bounds_run(
|
|
&mut self,
|
|
request: BoundsRunRequest<'_>,
|
|
uses: &[Use<ResourceKey>],
|
|
) -> Result<Option<BoundsSlot>, Error> {
|
|
if self.gathers.full() {
|
|
self.flush_gathers();
|
|
}
|
|
let device = self.device().clone();
|
|
let queue = self.open.command_buffer.queue_place().id();
|
|
let joined = {
|
|
let mut state = self.open.command_buffer.state();
|
|
let pool = state.index_pool(&device, queue)?;
|
|
let status = state.status_words(&device)?;
|
|
self.gathers
|
|
.join_bounds(&device, (&mut state.uploads, &status, &pool), request)?
|
|
};
|
|
let Some((slot, held)) = joined else {
|
|
return Ok(None);
|
|
};
|
|
self.joined(held, uses);
|
|
self.counts().bounded_run_draw();
|
|
Ok(Some(slot))
|
|
}
|
|
|
|
/// A draw joined a run: hold what a new run's command buffer holds, and track the draw's
|
|
/// reads `uses` (the run's barrier from everything before orders them: the one tracking
|
|
/// returns goes unrecorded).
|
|
fn joined(&mut self, held: Option<GatherHeld>, uses: &[Use<ResourceKey>]) {
|
|
if let Some(held) = held {
|
|
if let Some(output) = held.output {
|
|
self.retain(output);
|
|
}
|
|
for sets in held.sets {
|
|
self.retain(sets);
|
|
}
|
|
}
|
|
let _ = self.command_later(
|
|
vk::PipelineStageFlags2::COMPUTE_SHADER,
|
|
uses,
|
|
&BTreeSet::new(),
|
|
);
|
|
}
|
|
|
|
/// Record the open gather runs, where any is open, into the encoder's `VkCommandBuffer`.
|
|
fn flush_gathers(&mut self) {
|
|
if !self.gathers.is_empty() {
|
|
self.recording();
|
|
}
|
|
}
|
|
|
|
/// Record the open gather runs into `raw`, the encoder's `VkCommandBuffer`.
|
|
fn record_gathers(&mut self, raw: vk::CommandBuffer) {
|
|
if self.gathers.is_empty() {
|
|
return;
|
|
}
|
|
let runs = self.gathers.take();
|
|
let device = self.open.command_buffer.device().shared().vk();
|
|
// SAFETY: `raw` is the encoder's, recording outside a render pass (a pass records into
|
|
// the one it holds), and only this encoder records it; each run's kernel is the device's,
|
|
// and its set and output are held by the command buffer (`gather`), as its table's region
|
|
// is the command buffer's.
|
|
unsafe { index_rewrite::record_runs(device, raw, &runs) };
|
|
}
|
|
|
|
/// The `VkCommandBuffer` the encoder's next command records into: `raw`, or where a pass
|
|
/// holds the one before and no command came since, a new one of the command buffer's pool,
|
|
/// begun, which goes before the pass's; the open gather runs record into it first
|
|
/// ([`gather`](Self::gather)). `None` where none can be made, which abandons the command
|
|
/// buffer (as a failure to end one does, [`defer`](Self::defer)).
|
|
fn recording(&mut self) -> Option<vk::CommandBuffer> {
|
|
let raw = self.raw_recording()?;
|
|
self.record_gathers(raw);
|
|
Some(raw)
|
|
}
|
|
|
|
/// [`recording`](Self::recording) before the open gather runs record into it.
|
|
fn raw_recording(&mut self) -> Option<vk::CommandBuffer> {
|
|
if self.raw == vk::CommandBuffer::null() {
|
|
match self.continuation() {
|
|
Ok(next) => self.raw = next.0,
|
|
Err(_) => {
|
|
self.open.command_buffer.state().abandoned = true;
|
|
return None;
|
|
}
|
|
}
|
|
}
|
|
self.recorded = true;
|
|
Some(self.raw)
|
|
}
|
|
|
|
/// Hold a `VkCommandBuffer` for a render pass instance whose draws record inline in it from
|
|
/// now to the pass's end (`render.rs`): the encoder's own where nothing recorded into it
|
|
/// yet, else a new one of the pool, begun. Until [`release_pass`](Self::release_pass), the
|
|
/// encoder's commands record into another, which goes before it in submission order: where
|
|
/// they go before a pass recorded at its end. Refused where one is held, or none can be made.
|
|
///
|
|
/// The first encoder's own holds its command buffer's start, written first
|
|
/// (`gpu_time.rs`): where a command then records before the pass, in a command buffer that
|
|
/// goes before it, the batch's first command buffer is no longer the one that wrote it, and
|
|
/// the batch reads the start its command buffer of barriers writes before everything instead
|
|
/// (`command_buffer/submit.rs`, which compares the batch's first command buffer with the one
|
|
/// that wrote the start).
|
|
pub(super) fn hold_pass(&mut self) -> Result<vk::CommandBuffer, Error> {
|
|
if self.pass.is_some() {
|
|
return Err(internal("a render pass held twice"));
|
|
}
|
|
self.flush_gathers();
|
|
let held = match self.recorded {
|
|
true => self.continuation()?.0,
|
|
false => {
|
|
let own = self.raw;
|
|
self.raw = vk::CommandBuffer::null();
|
|
own
|
|
}
|
|
};
|
|
self.pass = Some(held);
|
|
Ok(held)
|
|
}
|
|
|
|
/// End the hold [`hold_pass`](Self::hold_pass) took: the render pass instance recorded in
|
|
/// the held command buffer ends there, whatever the encoder's commands record from here on
|
|
/// (a command that names a detached buffer records nothing, [`skips`](Self::skips)); the
|
|
/// command buffer of the commands before the pass, where any recorded, ends and goes before
|
|
/// it; and the encoder's commands record on into the pass's, after it. Refused only where
|
|
/// that one does not end, which abandons the command buffer.
|
|
pub(super) fn release_pass(&mut self) -> Result<(), Error> {
|
|
self.flush_gathers();
|
|
let Some(pass) = self.pass.take() else {
|
|
return Ok(());
|
|
};
|
|
// SAFETY: the held command buffer is recording inside the render pass instance its pass
|
|
// began, and only this encoder records it.
|
|
unsafe { self.device().shared().vk().cmd_end_render_pass(pass) };
|
|
if self.raw != vk::CommandBuffer::null() {
|
|
// SAFETY: as in `barrier`.
|
|
if let Err(error) = unsafe { self.open.command_buffer.end_raw(self.raw) } {
|
|
self.open.command_buffer.state().abandoned = true;
|
|
return Err(vulkan(error));
|
|
}
|
|
self.ended.push(self.raw);
|
|
}
|
|
self.raw = pass;
|
|
self.recorded = true;
|
|
Ok(())
|
|
}
|
|
|
|
/// After [`release_pass`](Self::release_pass), hand the pass's command buffer, where its first
|
|
/// instance and what followed it recorded, over to the segment the pass records later in
|
|
/// (`render/executions.rs`), ended: it goes between what the segment records before the pass
|
|
/// and the pass's later instances ([`cut_before`](Self::cut_before)), so nothing the segment
|
|
/// records before the pass at the commit runs after the first instance. The encoder's next
|
|
/// command begins another command buffer. Refused only where it does not end, which abandons
|
|
/// the command buffer.
|
|
pub(super) fn hand_over_pass(&mut self) -> Result<vk::CommandBuffer, Error> {
|
|
self.flush_gathers();
|
|
let pass = self.raw;
|
|
if pass == vk::CommandBuffer::null() {
|
|
return Err(internal(
|
|
"a render pass handed over without its command buffer",
|
|
));
|
|
}
|
|
// SAFETY: as in `barrier`.
|
|
if let Err(error) = unsafe { self.open.command_buffer.end_raw(pass) } {
|
|
self.open.command_buffer.state().abandoned = true;
|
|
return Err(vulkan(error));
|
|
}
|
|
self.raw = vk::CommandBuffer::null();
|
|
self.recorded = false;
|
|
Ok(pass)
|
|
}
|
|
|
|
/// In a [`segment`](Self::segment): place `before`, an ended command buffer handed over to it
|
|
/// ([`hand_over_pass`](Self::hand_over_pass)), after what the segment recorded so far, between
|
|
/// barriers from everything before and to everything after, and go on in a new command buffer.
|
|
/// Refused where the current one does not end or the new one cannot be begun.
|
|
pub(super) fn cut_before(&mut self, before: vk::CommandBuffer) -> Result<(), Error> {
|
|
self.barrier(Barrier::everything());
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { self.open.command_buffer.end_raw(self.raw) }.map_err(vulkan)?;
|
|
self.ended.push(self.raw);
|
|
self.ended.push(before);
|
|
self.raw = self.continuation()?.0;
|
|
self.barrier(Barrier::everything());
|
|
Ok(())
|
|
}
|
|
|
|
/// Record one command that ran in `stages` with `uses`: the barrier the uses need after the
|
|
/// encoder's earlier commands (in an ordered encoder), then the command through `record`.
|
|
/// Call it once every check of the command passed: recording cannot fail.
|
|
pub(super) fn command(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
record: impl FnOnce(&Vk, vk::CommandBuffer),
|
|
) {
|
|
self.command_reaching(stages, uses, &BTreeSet::new(), record);
|
|
}
|
|
|
|
/// [`command`](Self::command) of a command whose uses name every byte of untracked memory it
|
|
/// touches (`tracker::Reach::Named`): a blit encoder's command, a shadow's copy. In an
|
|
/// ordered encoder it orders after the earlier such commands per key and range alone, and
|
|
/// after every untracked access of the others. It reads the boxes `reads` of textures'
|
|
/// images (a blit's sources): a read of a new texture's unwritten pattern there is announced,
|
|
/// at the commit, only where a box reaches an unwritten texel (`fresh_unwritten.rs`); its
|
|
/// other reads as
|
|
/// `command`'s.
|
|
pub(super) fn command_named(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
reads: &[FreshRead],
|
|
record: impl FnOnce(&Vk, vk::CommandBuffer),
|
|
) {
|
|
let none = BTreeSet::new();
|
|
self.command_observing(stages, uses, (&none, Reach::Named), reads, record);
|
|
}
|
|
|
|
/// `command` of a dispatch or draw whose uses of the keys `resident` reach
|
|
/// their buffers through the resident set alone (`binding::CommandUses`): the command does
|
|
/// not name them.
|
|
pub(super) fn command_reaching(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
resident: &BTreeSet<ResourceKey>,
|
|
record: impl FnOnce(&Vk, vk::CommandBuffer),
|
|
) {
|
|
self.command_observing(stages, uses, (resident, Reach::Unnamed), &[], record);
|
|
}
|
|
|
|
/// [`command_reaching`](Self::command_reaching) reading the boxes `reads`
|
|
/// ([`command_named`](Self::command_named)), whose uses say what untracked memory it touches
|
|
/// as `reach` does.
|
|
fn command_observing(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
(resident, reach): (&BTreeSet<ResourceKey>, Reach),
|
|
reads: &[FreshRead],
|
|
record: impl FnOnce(&Vk, vk::CommandBuffer),
|
|
) {
|
|
let Some(barrier) = self.track(stages, uses, (resident, reach), reads) else {
|
|
return;
|
|
};
|
|
let Some(raw) = self.recording() else {
|
|
return;
|
|
};
|
|
let device = self.open.command_buffer.device().shared().vk();
|
|
if let Some(barrier) = barrier {
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { barrier.record(device, raw) };
|
|
}
|
|
record(device, raw);
|
|
}
|
|
|
|
/// [`command_reaching`](Self::command_reaching) of a command recorded later, into a segment
|
|
/// of its own (a dispatch whose pipelines are being made, `pending_dispatch.rs`): everything
|
|
/// of it but its recording, here, in the encoder's order. The barrier its uses need after the
|
|
/// encoder's earlier commands is returned for its recording to record first: the segment
|
|
/// lies between the encoder's `VkCommandBuffer`s in submission order, so the barriers of the
|
|
/// encoder's later commands order them after it as they would in one. `None` where it
|
|
/// records nothing, as `command_reaching` (a detached buffer).
|
|
pub(super) fn command_later(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
resident: &BTreeSet<ResourceKey>,
|
|
) -> Option<Option<Barrier>> {
|
|
self.track(stages, uses, (resident, Reach::Unnamed), &[])
|
|
}
|
|
|
|
/// Everything of a command [`command_observing`](Self::command_observing) records but its
|
|
/// recording: its reads of new textures announced, its uses tracked, and their keys kept and
|
|
/// held. The barrier the command records first, or `None` where it records nothing.
|
|
fn track(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
(resident, reach): (&BTreeSet<ResourceKey>, Reach),
|
|
reads: &[FreshRead],
|
|
) -> Option<Option<Barrier>> {
|
|
// A command that names a detached buffer records nothing, nor does any after it.
|
|
let named = uses
|
|
.iter()
|
|
.map(|used| used.key)
|
|
.filter(|key| !resident.contains(key));
|
|
if self.skips(named) {
|
|
self.touch_reaching(uses, resident);
|
|
return None;
|
|
}
|
|
// A command reading a new texture's levels that read zero where Metal reads its pattern,
|
|
// before a clear covered them, announces it at its commit (`fresh_unwritten.rs`).
|
|
self.observe_fresh(uses, reads);
|
|
// Hazard tracking takes the uses with their buffers' host memory's; the keys kept and
|
|
// held are the uses' own.
|
|
let aliased = self.aliased(uses);
|
|
let tracked = aliased.as_deref().unwrap_or(uses);
|
|
// A write of a carried shadowed texture's truth another way leaves its image stale: the
|
|
// texture is no longer carried, whatever this encoder uses (`shadow::ShadowCarry`).
|
|
self.shadows.wrote(tracked);
|
|
self.open.command_buffer.note_dirty(tracked);
|
|
let barrier = self.uses.order(stages, tracked, reach);
|
|
if barrier.is_some() {
|
|
self.device().shared().counts.command_barrier();
|
|
}
|
|
self.touch_reaching(uses, resident);
|
|
Some(barrier)
|
|
}
|
|
|
|
/// Log for the commit each texture `uses` read in a level and layer, where a new texture's
|
|
/// unwritten pattern may lie (`FreshUnwritten::observe_reads`).
|
|
fn observe_fresh(&self, uses: &[Use<ResourceKey>], reads: &[FreshRead]) {
|
|
FreshUnwritten::observe_reads(self.command_buffer(), uses, reads);
|
|
}
|
|
|
|
/// `uses` with a use of the host memory's key beside each use of a no-copy buffer that
|
|
/// joined another's memory (`host_aliases.rs`), which hazard tracking and the shadows take;
|
|
/// `None` where no use names one.
|
|
fn aliased(&self, uses: &[Use<ResourceKey>]) -> Option<Vec<Use<ResourceKey>>> {
|
|
self.device().shared().aliases.augment(uses)
|
|
}
|
|
|
|
/// Keep the keys `uses` name (`keys`); and hold each buffer over host pages among them the
|
|
/// first time (`rebind.rs`), retaining the pages the commands took until the command buffer
|
|
/// completes.
|
|
fn touch(&mut self, uses: &[Use<ResourceKey>]) {
|
|
self.touch_reaching(uses, &BTreeSet::new());
|
|
}
|
|
|
|
/// `touch` `uses`, of which those of the keys `resident` reach their buffers
|
|
/// through the resident set alone: every other key is named (`Taken`), where the device has
|
|
/// a buffer over host pages.
|
|
fn touch_reaching(&mut self, uses: &[Use<ResourceKey>], resident: &BTreeSet<ResourceKey>) {
|
|
self.hold_resident();
|
|
let rebinds = self.generation().rebinds.any();
|
|
for (at, used) in uses.iter().enumerate() {
|
|
let key = used.key;
|
|
let named = rebinds && !resident.contains(&key);
|
|
let before = self.touched.get(at).copied();
|
|
if before.map(|(touched, _)| touched) != Some(key) && self.keys.insert(key) && rebinds {
|
|
self.hold(key);
|
|
}
|
|
if named && before != Some((key, true)) {
|
|
self.taken.name(key);
|
|
}
|
|
match self.touched.get_mut(at) {
|
|
Some(touched) => *touched = (key, named),
|
|
None => self.touched.push((key, named)),
|
|
}
|
|
}
|
|
self.touched.truncate(uses.len());
|
|
}
|
|
|
|
/// [`touch_reaching`](Self::touch_reaching) of a draw's `uses` past the first `past`, those
|
|
/// of what the encoder made resident it touched already (`NotedApart::touched`), while the
|
|
/// device's buffers over host pages stand as `rebinds` says.
|
|
fn touch_draw(
|
|
&mut self,
|
|
(uses, resident): (&[Use<ResourceKey>], &BTreeSet<ResourceKey>),
|
|
past: usize,
|
|
rebinds: bool,
|
|
) {
|
|
for (at, used) in uses.iter().enumerate().skip(past) {
|
|
let key = used.key;
|
|
let named = rebinds && !resident.contains(&key);
|
|
let before = self.touched.get(at).copied();
|
|
if before.map(|(touched, _)| touched) != Some(key) && self.keys.insert(key) && rebinds {
|
|
self.hold(key);
|
|
}
|
|
if named && before != Some((key, true)) {
|
|
self.taken.name(key);
|
|
}
|
|
match self.touched.get_mut(at) {
|
|
Some(touched) => *touched = (key, named),
|
|
None => self.touched.push((key, named)),
|
|
}
|
|
}
|
|
self.touched.truncate(uses.len());
|
|
// Each key the uses take is kept, and named where touching them whole names it.
|
|
#[cfg(debug_assertions)]
|
|
for used in uses {
|
|
let named = rebinds && !resident.contains(&used.key);
|
|
assert!(
|
|
self.keys.contains(&used.key) && (!named || self.taken.named().contains(&used.key)),
|
|
"a draw touched by its uses' identity less than by its uses"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Keep `keys`, those of resources a command uses (its own uses', or its bindings', which
|
|
/// it may not read), holding each buffer over host pages among them as `touch` does.
|
|
pub(super) fn touch_keys(&mut self, keys: impl IntoIterator<Item = ResourceKey>) {
|
|
let rebinds = self.device().shared().rebinds.any();
|
|
for key in keys {
|
|
if self.keys.insert(key) && rebinds {
|
|
self.hold(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `used`, a resource the encoder first made resident (`use_resource`, `use_heap`), which its
|
|
/// residency holds until the command buffer completes (`Residency::use_resource`): where it is
|
|
/// a buffer over host pages or a heap whose bytes are, hold it from the encoder's next command
|
|
/// on, whether or not a command reaches it, as Metal holds every buffer an encoder made
|
|
/// resident from the submission of a command it encoded after, and fails that submission where
|
|
/// the buffer is detached by then (code 8). An encoder that encodes no command after it holds
|
|
/// nothing of it, as Metal's.
|
|
pub(super) fn made_resident(&mut self, used: Used) {
|
|
if let Some(key) = used.bytes_key() {
|
|
if self.device().shared().rebinds.any() {
|
|
self.resident_unheld.push(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Hold the buffers the encoder made resident since its last command (`made_resident`), as
|
|
/// its commands' uses are held: once each, the keys kept.
|
|
fn hold_resident(&mut self) {
|
|
while let Some(key) = self.resident_unheld.pop() {
|
|
if self.keys.insert(key) {
|
|
self.hold(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Hold each buffer over host pages among `keys` from now on, as a command that names it
|
|
/// does (`touch_keys`), without keeping the keys: the bytes a render pass's attachments lie
|
|
/// in, which it holds from its open (`render.rs`). Free while the device has no buffer over
|
|
/// host pages.
|
|
pub(super) fn hold_bytes(&mut self, keys: impl IntoIterator<Item = ResourceKey>) {
|
|
if !self.device().shared().rebinds.any() {
|
|
return;
|
|
}
|
|
for key in keys {
|
|
self.hold(key);
|
|
}
|
|
}
|
|
|
|
/// Hold `key` where it names a buffer over host pages (`rebind.rs`).
|
|
fn hold(&mut self, key: ResourceKey) {
|
|
// Encoded again after the commit: nothing refuses a rebind for it.
|
|
if self.replaying {
|
|
if let Some(pinned) = self.holds.pin(key) {
|
|
self.taken.pin(key, pinned);
|
|
}
|
|
return;
|
|
}
|
|
if let Some(pinned) = self.holds.hold(key) {
|
|
self.taken.pin(key, pinned);
|
|
}
|
|
}
|
|
|
|
/// Whether the commands it records from now on were called while no buffer of the device was
|
|
/// detached (`unattached_as_called`): `true` while a render encoder lowers calls it logged,
|
|
/// whose [`skips`](Self::skips) then skips nothing a detach after their calls would.
|
|
pub(super) fn judges_unattached(&mut self, unattached: bool) {
|
|
self.unattached_as_called = unattached;
|
|
}
|
|
|
|
/// Whether the encoder records nothing for a command that names `keys`: one of them is a
|
|
/// detached buffer's (`detach.rs`), or an earlier command of the encoder named one. From
|
|
/// that command on the encoder records nothing (`Taken::die`): its submission fails at
|
|
/// the command buffer's first submission, `CommandBufferError::DetachedBacking` while the
|
|
/// buffer is detached and `EncodedUseRebound` where it lies in pages again by then, so
|
|
/// nothing it would record runs. The keys are kept, held and named as a recorded command's
|
|
/// are. Free while no buffer is detached. Call it once every check of the command passed,
|
|
/// before any Vulkan call that takes one of the buffers.
|
|
pub(super) fn skips(&mut self, keys: impl IntoIterator<Item = ResourceKey> + Clone) -> bool {
|
|
if !self.would_skip(keys.clone()) {
|
|
return false;
|
|
}
|
|
if !self.taken.dead() {
|
|
self.taken.die();
|
|
}
|
|
self.touch_keys(keys.clone());
|
|
for key in keys {
|
|
self.taken.name(key);
|
|
}
|
|
true
|
|
}
|
|
|
|
/// Whether [`skips`](Self::skips) skips a command that names `keys`, changing nothing.
|
|
pub(super) fn would_skip(&self, keys: impl IntoIterator<Item = ResourceKey>) -> bool {
|
|
if self.taken.dead() {
|
|
return true;
|
|
}
|
|
!self.unattached_as_called && self.generation().rebinds.any_detached(keys)
|
|
}
|
|
|
|
/// The keys its commands named so far, their uses' and their bindings' (`touch`).
|
|
pub(super) fn named_keys(&self) -> &BTreeSet<ResourceKey> {
|
|
&self.keys
|
|
}
|
|
|
|
/// Whether its commands reached a heap so far: one they made resident, or the heap of a
|
|
/// resource they named.
|
|
pub(super) fn reached_heaps(&self) -> bool {
|
|
let heap_keys = &self.device().shared().heap_keys;
|
|
!self.heaps.is_empty() || !heap_keys.among(self.keys.iter().copied()).is_empty()
|
|
}
|
|
|
|
/// Note `heaps`, which a command the encoder recorded reached through what it made resident.
|
|
pub(super) fn touch_heaps(&mut self, heaps: impl IntoIterator<Item = Heap>) {
|
|
let rebinds = self.device().shared().rebinds.any();
|
|
for heap in heaps {
|
|
if !self.heaps.contains(&heap) {
|
|
// A heap over host pages is held as its resources' uses are.
|
|
let key = heap.bytes().map(|bytes| bytes.identity().key);
|
|
if let Some(key) = key.filter(|&key| rebinds && self.keys.insert(key)) {
|
|
self.hold(key);
|
|
}
|
|
self.heaps.push(heap);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Note one command recorded outside the encoder's own `VkCommandBuffer` (a render encoder's
|
|
/// draw, in its secondary command buffer) that runs in `stages` with `uses`. Only an
|
|
/// unordered encoder's commands are recorded so: they need no barrier between them. Call it
|
|
/// before recording the command; it refuses (internal) an ordered encoder. A draw noted with
|
|
/// the same uses, reaching the same keys through the resident set alone, as the last one
|
|
/// `noted` keeps (a render encoder's draws'), into the same state, is noted already
|
|
/// (`noted_apart.rs`); a command with no `noted` (a tile dispatch) is noted whole.
|
|
pub(super) fn note_reaching(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
(uses, resident): (&[Use<ResourceKey>], &BTreeSet<ResourceKey>),
|
|
noted: Option<&mut NotedApart>,
|
|
) -> Result<(), Error> {
|
|
if self.uses.ordered() {
|
|
return Err(super::error::internal(
|
|
"a command recorded apart in an ordered encoder",
|
|
));
|
|
}
|
|
self.observe_fresh(uses, &[]);
|
|
self.hold_resident();
|
|
let Some(noted) = noted else {
|
|
self.record_aliased(stages, uses);
|
|
self.touch_reaching(uses, resident);
|
|
return Ok(());
|
|
};
|
|
// Read before the host aliases' uses are worked out, so a change made since counts.
|
|
let into = self.noted_into();
|
|
if noted.holds(stages, (uses, resident), into) {
|
|
self.counts().command_noted_as_before();
|
|
return Ok(());
|
|
}
|
|
let record = |fresh: &[Use<ResourceKey>]| self.record_aliased(stages, fresh);
|
|
noted.note_fresh(stages, (uses, resident), into, record);
|
|
if noted.covered() > 0 {
|
|
self.counts().uses_noted_covered(noted.covered() as u64);
|
|
}
|
|
self.touch_reaching(uses, resident);
|
|
Ok(())
|
|
}
|
|
|
|
/// [`note_reaching`](Self::note_reaching) a draw whose uses begin with those of what the
|
|
/// encoder made resident of the identities `prefix` (`binding::ReachingPrefix`), with
|
|
/// `noted`: those are compared, recorded and touched once while the identities hold
|
|
/// (`noted_apart.rs`; `WorkCounts::resident_uses_noted` counts the ones walked).
|
|
pub(super) fn note_draw(
|
|
&mut self,
|
|
(uses, resident, prefix): (&[Use<ResourceKey>], &BTreeSet<ResourceKey>, ReachingPrefix),
|
|
noted: &mut NotedApart,
|
|
) -> Result<(), Error> {
|
|
if self.uses.ordered() {
|
|
return Err(super::error::internal(
|
|
"a command recorded apart in an ordered encoder",
|
|
));
|
|
}
|
|
let len = prefix.prefix.len;
|
|
// A chunk's core notes the uses past those its pass's core notes ([`ResidentPart`]).
|
|
let apart = match &mut self.resident_notes {
|
|
ResidentNotes::Here => false,
|
|
ResidentNotes::Apart(Some(part)) => part.reaching == prefix,
|
|
ResidentNotes::Apart(part) => {
|
|
*part = Some(Arc::new(ResidentPart {
|
|
reaching: prefix,
|
|
uses: uses[..len].to_vec(),
|
|
resident: resident.clone(),
|
|
}));
|
|
true
|
|
}
|
|
};
|
|
if apart {
|
|
let past = &uses[len..];
|
|
let stages = (past.iter()).fold(vk::PipelineStageFlags2::empty(), |all, used| {
|
|
all | used.read.stages | used.write.stages
|
|
});
|
|
return self.note_reaching(stages, (past, resident), Some(noted));
|
|
}
|
|
self.observe_fresh(uses, &[]);
|
|
self.hold_resident();
|
|
// Its stages: those of the uses of what the encoder made resident, and the rest's.
|
|
let stages = (uses[len..].iter()).fold(prefix.stages, |all, used| {
|
|
all | used.read.stages | used.write.stages
|
|
});
|
|
// Read before the host aliases' uses are worked out, so a change made since counts.
|
|
let into = self.noted_into();
|
|
if noted.holds_draw(stages, (uses, resident, prefix), into) {
|
|
self.counts().command_noted_as_before();
|
|
return Ok(());
|
|
}
|
|
let record = |fresh: &[Use<ResourceKey>]| {
|
|
self.record_aliased(stages, fresh);
|
|
fresh.len()
|
|
};
|
|
let recorded = noted.note_draw_fresh(stages, (uses, resident, prefix), into, record);
|
|
if noted.covered() > 0 {
|
|
self.counts().uses_noted_covered(noted.covered() as u64);
|
|
}
|
|
let rebinds = self.device().shared().rebinds.any();
|
|
let touched = noted.touched(prefix.prefix, (into.core, rebinds));
|
|
self.touch_draw((uses, resident), touched, rebinds);
|
|
// The uses of what the encoder made resident it walked: recorded whole
|
|
// (`note_draw_fresh` gave them all), or touched.
|
|
if touched == 0 || recorded == uses.len() {
|
|
self.counts().resident_uses_noted(len as u64);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// [`note_reaching`](Self::note_reaching) a draw with `uses`, of which it reaches the keys
|
|
/// `resident` through the resident set alone, whose uses differ from those of the last
|
|
/// command `noted` keeps only at the positions `at`, each of the same key and access types
|
|
/// there (a draw whose calls moved buffer offsets alone: `render/draw_again/rebound.rs`):
|
|
/// where the last command was noted into the encoder's state now, with as many uses
|
|
/// (`NotedApart::moves`), only those of the uses at `at` that differ are compared and
|
|
/// recorded, with the host aliases' uses beside them, as `note_reaching` records the uses that
|
|
/// differ; and none is touched, each key being the one the last command touched at its
|
|
/// position. Where `inside`, each use at `at` lies inside the bytes of the last command's at
|
|
/// its place (`Use::covers`, or equals it), which `noted` keeps or keeps one covering
|
|
/// (`noted_apart.rs`): none is compared, and the kept uses stay
|
|
/// (`WorkCounts::draws_moved_inside`). Whether it noted the draw; else it noted nothing, and
|
|
/// the caller notes it as `note_reaching` does.
|
|
pub(super) fn note_moved(
|
|
&mut self,
|
|
(uses, resident, at): (&[Use<ResourceKey>], &BTreeSet<ResourceKey>, &[usize]),
|
|
inside: bool,
|
|
noted: &mut NotedApart,
|
|
) -> Result<bool, Error> {
|
|
if self.uses.ordered() {
|
|
return Err(super::error::internal(
|
|
"a command recorded apart in an ordered encoder",
|
|
));
|
|
}
|
|
// Read before the host aliases' uses are worked out, so a change made since counts.
|
|
let into = self.noted_into();
|
|
if !noted.moves((uses, resident), into) {
|
|
return Ok(false);
|
|
}
|
|
self.observe_fresh(uses, &[]);
|
|
self.hold_resident();
|
|
match inside {
|
|
// Noted already: each moved use is in the part inside the kept use at its place.
|
|
true => {
|
|
#[cfg(debug_assertions)]
|
|
assert!(
|
|
noted.covers_at(uses, at),
|
|
"a draw noted inside has a moved use outside the kept use at its place"
|
|
);
|
|
self.counts().draw_moved_inside();
|
|
}
|
|
false => {
|
|
let record =
|
|
|stages, fresh: &[Use<ResourceKey>]| self.record_aliased(stages, fresh);
|
|
let covered = match noted.note_moved((uses, resident), at, record) {
|
|
Moved::AsBefore => {
|
|
self.counts().command_noted_as_before();
|
|
0
|
|
}
|
|
Moved::Covered(covered) | Moved::Recorded((), covered) => covered,
|
|
};
|
|
if covered > 0 {
|
|
self.counts().uses_noted_covered(covered as u64);
|
|
}
|
|
}
|
|
}
|
|
// Touched already: each key at its position, named as touching it names it.
|
|
#[cfg(debug_assertions)]
|
|
{
|
|
let rebinds = self.device().shared().rebinds.any();
|
|
for (place, used) in uses.iter().enumerate() {
|
|
let named = rebinds && !resident.contains(&used.key);
|
|
assert!(
|
|
self.touched.get(place) == Some(&(used.key, named)),
|
|
"a draw noted moved touches a key its last command did not touch there"
|
|
);
|
|
}
|
|
assert_eq!(self.touched.len(), uses.len());
|
|
}
|
|
Ok(true)
|
|
}
|
|
|
|
/// The encoder's state a command noted apart now is noted into (`NotedInto`).
|
|
pub(super) fn noted_into(&self) -> NotedInto {
|
|
let shared = self.generation();
|
|
NotedInto {
|
|
core: self.id,
|
|
parts: self.uses.parts(),
|
|
held: self.uses.held().len(),
|
|
rebinds: shared.rebinds.any(),
|
|
aliases: shared.aliases.version(),
|
|
}
|
|
}
|
|
|
|
/// Whether the command apart `noted` keeps, the last one [`note_reaching`](Self::note_reaching)
|
|
/// noted, was noted into the encoder's state now: then a command of its stages and uses,
|
|
/// reaching the keys it reached through the resident set alone, is noted already, and noting
|
|
/// it notes only its reads of fresh textures and what the encoder made resident since
|
|
/// ([`note_again`](Self::note_again)).
|
|
pub(super) fn noted_now(&self, noted: &NotedApart) -> bool {
|
|
!self.uses.ordered() && noted.noted_into(self.noted_into())
|
|
}
|
|
|
|
/// Note again a command apart noted already ([`noted_now`](Self::noted_now)) with `uses`, as
|
|
/// [`note_reaching`](Self::note_reaching) notes it, but for its count
|
|
/// (`WorkCounts::commands_noted_as_before`), which its caller counts: its reads of fresh
|
|
/// textures, and the buffers the encoder made resident since its last command. `uses` may
|
|
/// leave out the uses that read no image other than as an attachment, which note nothing.
|
|
pub(super) fn note_again(&mut self, uses: &[Use<ResourceKey>]) {
|
|
self.observe_fresh(uses, &[]);
|
|
self.hold_resident();
|
|
}
|
|
|
|
/// Whether noting a command's reads of fresh textures may announce one
|
|
/// (`fresh_unwritten.rs`): the device has a texture still unwritten since it was made.
|
|
pub(super) fn observes_fresh(&self) -> bool {
|
|
self.device().shared().fresh.any()
|
|
}
|
|
|
|
/// How many holds the encoder was asked for (each kept, or found among those held lately:
|
|
/// `retained.rs`) and execution records it keeps for its command buffer (builds with debug
|
|
/// assertions: `render/draw_again.rs`'s check).
|
|
#[cfg(debug_assertions)]
|
|
pub(super) fn retained_holds(&self) -> (usize, usize) {
|
|
(self.retained.asked(), self.executions.len())
|
|
}
|
|
|
|
/// Record one command apart that runs in `stages` with `uses`, and the uses of the host
|
|
/// memory beside them ([`aliased`](Self::aliased)), worked out into the vector the encoder
|
|
/// keeps for them.
|
|
fn record_aliased(&mut self, stages: vk::PipelineStageFlags2, uses: &[Use<ResourceKey>]) {
|
|
let aliases = &self.open.command_buffer.generation().aliases;
|
|
let quiet = Some(&mut self.aliases_quiet);
|
|
let all = match aliases.augment_into(uses, &mut self.aliased_uses, quiet) {
|
|
true => &self.aliased_uses[..],
|
|
false => uses,
|
|
};
|
|
self.open.command_buffer.note_dirty(all);
|
|
self.uses.record(stages, all, None);
|
|
}
|
|
|
|
/// The copies a command that uses the shadowed textures `shadows` and has `uses` needs
|
|
/// before it, which the encoder then counts as recorded: the scratch of a merge (`shadow.rs`,
|
|
/// `shadow_merge.rs`) retained until the command buffer completes. Refused, changing
|
|
/// nothing, as the scratch is. Call it once every other check of the command passed, and
|
|
/// record the copies with [`shadow_copies`](Self::shadow_copies), then the command.
|
|
#[track_caller]
|
|
pub(super) fn plan_shadows(
|
|
&mut self,
|
|
shadows: &[ShadowUse],
|
|
uses: &[Use<ResourceKey>],
|
|
) -> Result<Vec<ShadowCopy>, Error> {
|
|
// What a copy in records beyond what the texture holds from its making: a converted
|
|
// two-plane YCbCr texture's conversion, made at its first use of its image.
|
|
for shadow in shadows {
|
|
shadow.copies.prepare()?;
|
|
}
|
|
let device = self.device().clone();
|
|
let aliased = self.aliased(uses);
|
|
let uses = aliased.as_deref().unwrap_or(uses);
|
|
// The kept images its merges take are held to the encoder's end, those of a refused
|
|
// command let go of here. An encoder encoded again borrows none, nor its command buffer's
|
|
// scratch: its place may lie inside a run of passes that holds one (`pass_runs.rs`).
|
|
let images = self.open.command_buffer.queue_place().kept_images();
|
|
let pool = self.open.command_buffer.merge_scratch();
|
|
let lent = !self.replaying && !crate::replay::is_replaying();
|
|
let held = self.kept_leases.len();
|
|
let leases = &mut self.kept_leases;
|
|
let planned = self
|
|
.shadows
|
|
.before(shadows, uses, |copies, made_for, area| {
|
|
let merging = (made_for.two_ways, made_for.blocks, area);
|
|
let made = ShadowMerge::new(&device, copies, merging, (images, pool), lent)?;
|
|
let (merge, lease) = (Arc::new(made.0), made.1);
|
|
if let Some(lease) = lease {
|
|
leases.push((Arc::downgrade(&merge), lease));
|
|
}
|
|
Ok(merge)
|
|
});
|
|
let copies = match planned {
|
|
Ok(copies) => copies,
|
|
Err(refused) => {
|
|
self.kept_leases.truncate(held);
|
|
return Err(refused);
|
|
}
|
|
};
|
|
let copies = self.sample_moves(copies);
|
|
let copies = self.later_runs(copies);
|
|
self.count_shadow_copies(&copies);
|
|
// How this command's first copies in stood against their images' last standing, which
|
|
// sizes the carry on a run without a timing (`shadow::CarryTally`).
|
|
let tally = self.shadows.tally();
|
|
(device.shared().counts).shadow_first_copies_in_tallied(tally);
|
|
let by = match Location::caller().file() {
|
|
file if file.ends_with("compute.rs") => KeepBy::Compute,
|
|
file if file.ends_with("blit.rs") => KeepBy::Blit,
|
|
_ => KeepBy::Render,
|
|
};
|
|
for copy in &copies {
|
|
if let Step::Keep(merge) = ©.step {
|
|
let churn = &device.shared().counts.churn;
|
|
churn.kept(by, merge.cuts(), merge.texels_kept());
|
|
self.retain(Arc::clone(merge));
|
|
}
|
|
if copy.cause == Some(CopyCause::First) {
|
|
(self.first_copies).push((copy.copies.shadow_key, copy.texels_copied_in()));
|
|
}
|
|
}
|
|
Ok(copies)
|
|
}
|
|
|
|
/// `copies` with the run each copy in of a copy taken at use records after its copy into the
|
|
/// first level where the compute filter or the picks make its later levels
|
|
/// (`shadow::LevelsCopy::run`, `Buffer::new_tiled_texture`), made now, each held until the
|
|
/// command buffer completes; a filter's counted as `generate_mipmaps`' is
|
|
/// (`WorkCounts::mipmaps_filtered`). Where one cannot be made (the device's memory), the
|
|
/// command buffer fails from the encoder on, as [`sample_moves`](Self::sample_moves) fails it,
|
|
/// and the copy makes no later level.
|
|
fn later_runs(&mut self, mut copies: Vec<ShadowCopy>) -> Vec<ShadowCopy> {
|
|
let runs = |copy: &ShadowCopy| {
|
|
let into = matches!(copy.step, Step::Into);
|
|
into && copy.copies.levels.as_ref().is_some_and(LevelsCopy::runs)
|
|
};
|
|
if !copies.iter().any(runs) {
|
|
return copies;
|
|
}
|
|
let device = self.device().clone();
|
|
let mut made = Vec::new();
|
|
let mut failed = None;
|
|
for copy in copies.iter_mut().filter(|copy| runs(copy)) {
|
|
let Some(levels) = ©.copies.levels else {
|
|
continue;
|
|
};
|
|
match levels.run(&device) {
|
|
Ok(Some(run)) => {
|
|
if matches!(run, LaterRun::Filter(_)) {
|
|
device.shared().counts.mipmaps_filtered();
|
|
}
|
|
let run = Arc::new(run);
|
|
made.push(Arc::clone(&run));
|
|
copy.later = Some(run);
|
|
}
|
|
Ok(None) => {}
|
|
Err(error) => {
|
|
failed.get_or_insert(error);
|
|
}
|
|
}
|
|
}
|
|
for run in made {
|
|
self.retain_shared(run);
|
|
}
|
|
if let Some(error) = failed {
|
|
self.fail_command_buffer(recording_error(&device, error));
|
|
}
|
|
copies
|
|
}
|
|
|
|
/// `copies` with the moves between each heap's multisample texture's image and Metal's tiles
|
|
/// their records take (`ShadowCopy::plan_moves`, `texture/heap_tiled.rs`), planned now from
|
|
/// the images the encoder's queue keeps (`texture/copy_images.rs`), each held until the
|
|
/// command buffer completes. Where they cannot be made (the device's memory, a broken
|
|
/// invariant), the command buffer fails from the encoder on, as a recording that fails does
|
|
/// (`command_buffer::recording_error`), and the copies record no moves.
|
|
fn sample_moves(&mut self, mut copies: Vec<ShadowCopy>) -> Vec<ShadowCopy> {
|
|
if !copies.iter().any(ShadowCopy::moves_samples) {
|
|
return copies;
|
|
}
|
|
let device = self.device().clone();
|
|
let mut planned = Vec::new();
|
|
let mut failed = None;
|
|
{
|
|
let images = self.open.command_buffer.queue_place().copy_images();
|
|
let maker = images.maker(&device);
|
|
for copy in &mut copies {
|
|
match copy.plan_moves(&maker) {
|
|
Ok(Some(moves)) => {
|
|
let moves = Arc::new(moves);
|
|
planned.push(Arc::clone(&moves));
|
|
copy.moves = Some(moves);
|
|
}
|
|
Ok(None) => {}
|
|
Err(error) => {
|
|
failed.get_or_insert(error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for moves in planned {
|
|
self.retain_shared(moves);
|
|
}
|
|
if let Some(error) = failed {
|
|
self.fail_command_buffer(recording_error(&device, error));
|
|
}
|
|
copies
|
|
}
|
|
|
|
/// Take `turns`, the turns of its pass's colors' textures to their other faces its pass's
|
|
/// open made (`render/face_swap.rs`).
|
|
pub(super) fn take_turns(&mut self, turns: Turns) {
|
|
self.turns = turns;
|
|
}
|
|
|
|
/// The turns its pass's open made (`render/face_swap.rs`).
|
|
pub(super) fn turns(&self) -> &Turns {
|
|
&self.turns
|
|
}
|
|
|
|
/// Take the images of `copies`, a texture's faces' shadows, no further from what its command
|
|
/// buffer's earlier encoders left (`render/face_swap.rs`, `shadow::ShadowCarry`).
|
|
pub(super) fn carry_no_further(&mut self, copies: &[Arc<ShadowCopies>]) {
|
|
self.shadows.carry_no_further(copies);
|
|
}
|
|
|
|
/// At its pass's first plan of its copies, before the copies of a command that uses the
|
|
/// shadowed textures `shadows`: hold the attached reads' shadows its pass's turns left holding
|
|
/// the texels from before the pass current (`render/face_swap.rs`), but those whose copy in
|
|
/// `clears` says is a clear; each `shadows` reads counted
|
|
/// (`WorkCounts::attached_read_swap_reads`).
|
|
pub(super) fn hold_turned(
|
|
&mut self,
|
|
shadows: &[ShadowUse],
|
|
clears: impl Fn(ResourceKey) -> bool,
|
|
) {
|
|
for copies in self.turns.hold() {
|
|
let key = copies.shadow_key;
|
|
if clears(key) {
|
|
continue;
|
|
}
|
|
self.shadows.hold_ahead(&copies);
|
|
if shadows.iter().any(|shadow| shadow.copies.shadow_key == key) {
|
|
let counts = &self.device().shared().counts;
|
|
counts.attached_read_swap_read(copies.texels());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Note that the encoder's pass takes the shadowed texture `key` as an attachment.
|
|
pub(super) fn shadow_attached(&mut self, key: ResourceKey) {
|
|
if !self.attachment_shadows.contains(&key) {
|
|
self.attachment_shadows.push(key);
|
|
}
|
|
}
|
|
|
|
/// Count shadowed textures' `copies` as planned (`WorkCounts::shadow_copies`), the texels
|
|
/// their copies in and back of buffers' textures' images move, each copy in's texels by why
|
|
/// it is made and what holds its truth (`WorkCounts::shadow_texels_first_copied_in` and kin),
|
|
/// those copies in of what changed within a rectangle alone
|
|
/// (`WorkCounts::bounded_shadow_copies`), and the keeps and merges whose copies of the shadow
|
|
/// into the scratch dispatch (`WorkCounts::shadow_merge_copies_dispatched`), and the texels the
|
|
/// keeps copy (`WorkCounts::shadow_texels_kept`).
|
|
fn count_shadow_copies(&self, copies: &[ShadowCopy]) {
|
|
count_shadow_copies(&self.device().shared().counts, copies);
|
|
}
|
|
|
|
/// Note that the encoder's work from here on waits for another writer the program ordered
|
|
/// before it (a fence's update, `fence.rs`): Metal shows it that writer's bytes, so no
|
|
/// shadowed texture's image is carried on and each copies its truth in again at its next use
|
|
/// (`shadow::ShadowCarry`).
|
|
pub(super) fn ordered_other_work(&mut self) {
|
|
self.shadows.not_carried();
|
|
}
|
|
|
|
/// Whether the encoder used a shadowed texture so far: where it did not, a command that uses
|
|
/// none takes no copy and notes no use ([`plan_shadows`](Self::plan_shadows),
|
|
/// [`shadows_used`](Self::shadows_used)).
|
|
pub(super) fn used_shadows(&self) -> bool {
|
|
!self.shadows.is_empty()
|
|
}
|
|
|
|
/// Note a recorded command that used the shadowed textures `shadows` and had `uses`, and
|
|
/// whose writes `within` change the bytes of a texture inside a rectangle of its texels alone
|
|
/// (`shadow::Within`).
|
|
pub(super) fn shadows_used(
|
|
&mut self,
|
|
shadows: &[ShadowUse],
|
|
uses: &[Use<ResourceKey>],
|
|
within: &[Within<'_>],
|
|
) {
|
|
let aliased = self.aliased(uses);
|
|
let uses = aliased.as_deref().unwrap_or(uses);
|
|
// Each part's uses as the device's host aliases augment them, where they augment any:
|
|
// else the parts as they are.
|
|
let augmented: Vec<Option<Vec<Use<ResourceKey>>>> =
|
|
within.iter().map(|part| self.aliased(part.uses)).collect();
|
|
if augmented.iter().all(Option::is_none) {
|
|
self.shadows.after(shadows, uses, within);
|
|
return;
|
|
}
|
|
let within: Vec<Within<'_>> = (within.iter().zip(&augmented))
|
|
.map(|(part, augmented)| Within {
|
|
uses: augmented.as_deref().unwrap_or(part.uses),
|
|
..*part
|
|
})
|
|
.collect();
|
|
self.shadows.after(shadows, uses, &within);
|
|
}
|
|
|
|
/// Record shadowed textures' `copies` as commands of the encoder: ordered as any command
|
|
/// is, in an encoder Metal orders; in one whose commands Metal leaves unordered, as one run
|
|
/// ([`ShadowRun`]) between a barrier from everything before its first copy and one to
|
|
/// everything after its last. A copy back the device runs faster as a dispatch is that
|
|
/// dispatch (`shadow_copy_back.rs`), a merged texture's copy in and its keep are one dispatch
|
|
/// at the copy in where the device takes it (`shadow_copy_in.rs`), and the rows a copy back
|
|
/// moves out of a staging buffer are a copy of rows (`row_copies.rs`); the command buffer
|
|
/// retains each until it completes.
|
|
pub(super) fn shadow_copies(&mut self, copies: &[ShadowCopy]) {
|
|
let device = self.device().clone();
|
|
let mut run = ShadowRun::default();
|
|
// The keeps a dispatch at their copy in took, which record nothing, and those that copy
|
|
// the staging buffer their copy in filled.
|
|
let mut taken = Vec::new();
|
|
let mut staged = Vec::new();
|
|
for (i, copy) in copies.iter().enumerate() {
|
|
if taken.contains(&i) {
|
|
continue;
|
|
}
|
|
// A copy in made as a clear of the shadow's image (`ShadowUse::cleared`).
|
|
if copy.clear.is_some() {
|
|
let uses = copy.uses();
|
|
let record = |device: &Vk, raw: vk::CommandBuffer| {
|
|
// SAFETY: the encoder's command buffer is recording outside a render pass;
|
|
// its commands retain the shadowed texture (and its image) until it
|
|
// completes, and every image rests in the `GENERAL` layout.
|
|
unsafe { copy.record_gated(device, raw, None, None) }
|
|
};
|
|
self.shadow_command(&mut run, (copy.stages(), &uses, false), record);
|
|
continue;
|
|
}
|
|
if let Some((keep, both, uses)) = keep_with_copy_in(&device, copies, i) {
|
|
taken.push(keep);
|
|
let record = |device: &Vk, raw: vk::CommandBuffer| {
|
|
// SAFETY: the encoder's command buffer is recording outside a render pass;
|
|
// its commands retain the shadowed texture (and its view), the merge's scratch
|
|
// and the dispatch until it completes, and every image rests in the `GENERAL`
|
|
// layout.
|
|
unsafe { both.record(device, raw) }
|
|
};
|
|
let stages = vk::PipelineStageFlags2::COMPUTE_SHADER;
|
|
self.shadow_command(&mut run, (stages, &uses, false), record);
|
|
device.shared().counts.shadow_copy_in_dispatched();
|
|
self.retain(both);
|
|
continue;
|
|
}
|
|
// A copy back or merge into host pages a consumer logs is reported to the log as the
|
|
// shadow's own at the commit (`dirty_pages.rs`).
|
|
if let Some((log, bytes)) = copy.written_log(&device) {
|
|
self.open.command_buffer.dirty().wrote_back(log, bytes);
|
|
}
|
|
// A shadow takes the view a dispatch reads on a device that dispatches alone.
|
|
let dispatched = copy.dispatched(&device);
|
|
let rows = match dispatched {
|
|
Some(_) => None,
|
|
None => copy.staged_back(&device),
|
|
};
|
|
let (stages, uses) = match &dispatched {
|
|
Some(_) => (
|
|
vk::PipelineStageFlags2::COMPUTE_SHADER,
|
|
copy.dispatched_uses(),
|
|
),
|
|
None => (
|
|
copy.stages_with(rows.as_ref()),
|
|
copy.uses_with(rows.as_ref()),
|
|
),
|
|
};
|
|
// Why the copy moves its texels whatever its shadow's standing across command buffers
|
|
// says (`shadow_standing.rs`), where it does: it is no copy in a standing may skip,
|
|
// a command of the encoder wrote what it reads, or one of an encoder of the command
|
|
// buffer that ended before it did, as its batch's submission would find
|
|
// (`submit.rs`'s `stands`), so it is recorded here, ungated, and splits no
|
|
// `VkCommandBuffer` there. `None` where the standing decides.
|
|
let unstood = match copy.left_to_standing() {
|
|
false => Some(Unskipped::Unstanding),
|
|
true if self.uses.written(&uses) => Some(Unskipped::AfterEncoderWrites),
|
|
true if self.ended_encoders_wrote(copy) => Some(Unskipped::CommandBuffer),
|
|
true => None,
|
|
};
|
|
// A copy in over host pages a consumer logs takes the regions its gate decides as its
|
|
// batch is submitted (`dirty_pages.rs`, `shadow::GatedCopyIn`); a command buffer's
|
|
// first copy in of a shadow that may stand across command buffers, before any command
|
|
// of the command buffer wrote what it reads, moves nothing where the image stands
|
|
// then (`shadow_standing.rs`).
|
|
let gate = match &dispatched {
|
|
Some(_) => None,
|
|
None => match copy.gate_log(&device) {
|
|
Some((log, texel)) => {
|
|
let shadows = &device.shared().dirty;
|
|
let mut dirty = self.open.command_buffer.dirty();
|
|
Some(CopyGate::Log(
|
|
dirty.gate((log, ©.copies, texel), shadows),
|
|
))
|
|
}
|
|
None => unstood.is_none().then_some(CopyGate::Standing),
|
|
},
|
|
};
|
|
// An encoder's first copy in of a shadow whose truth only the runtime's commands write
|
|
// that no batch's submission decides moves the texels: counted by why.
|
|
if gate.is_none() && copy.first_from_private_truth() {
|
|
let at = unstood.unwrap_or(Unskipped::AfterEncoderWrites);
|
|
self.copy_in_unskipped(copy, at);
|
|
}
|
|
// A keep may copy the staging buffer its copy in filled only where that copy in
|
|
// copies the texture's bytes whole: recorded here, ungated.
|
|
if dispatched.is_none() && gate.is_none() {
|
|
staged.extend(staged_keep(copies, i));
|
|
}
|
|
let from_staging = staged.contains(&i);
|
|
if let Some(gate) = gate {
|
|
// Tracked here as the copy, recorded at its batch's submission.
|
|
let named = match self.defers_apart() {
|
|
true => self.track_deferred(stages, &uses),
|
|
false => {
|
|
let named = std::cell::Cell::new(false);
|
|
let tracked = |_: &Vk, _: vk::CommandBuffer| named.set(true);
|
|
let writes = copy.writes_truth();
|
|
self.shadow_command(&mut run, (stages, &uses, writes), tracked);
|
|
named.get()
|
|
}
|
|
};
|
|
if named {
|
|
// Its segment's barriers order it after everything before and before
|
|
// everything after (`GatedCopyIn::record`): the run goes on as after one.
|
|
if self.defer_copy_in(copy, gate) {
|
|
run = ShadowRun::after_barrier();
|
|
}
|
|
}
|
|
if let Some(sets) = rows.and_then(RowCopies::retained) {
|
|
self.retain(sets);
|
|
}
|
|
continue;
|
|
}
|
|
let kept = std::cell::Cell::new(false);
|
|
let record = |device: &Vk, raw: vk::CommandBuffer| match &dispatched {
|
|
// SAFETY: the encoder's command buffer is recording outside a render pass; its
|
|
// commands retain the shadowed textures (and their views) and the dispatch until
|
|
// it completes, and every image rests in the `GENERAL` layout.
|
|
Some(back) => unsafe { back.record(device, raw) },
|
|
None if from_staging => {
|
|
// SAFETY: as above; the keep follows its texture's copy in, after the barrier
|
|
// its read of the shadow takes from that copy's write, with no copy of the
|
|
// shadow between them (`staged_keep`).
|
|
unsafe { copy.record_keep_from_staging(device, raw) };
|
|
kept.set(true);
|
|
}
|
|
// SAFETY: as above; the rows are the copy's own, made just now.
|
|
None => unsafe { copy.record_gated(device, raw, rows.as_ref(), None) },
|
|
};
|
|
self.shadow_command(&mut run, (stages, &uses, copy.writes_truth()), record);
|
|
if kept.get() {
|
|
device.shared().counts.shadow_keep_from_staging();
|
|
}
|
|
if let Some(back) = dispatched {
|
|
device.shared().counts.shadow_copy_back_dispatched();
|
|
self.retain(back);
|
|
}
|
|
if let Some(sets) = rows.and_then(RowCopies::retained) {
|
|
self.retain(sets);
|
|
}
|
|
}
|
|
if run.recorded() {
|
|
self.barrier(Barrier::everything());
|
|
}
|
|
}
|
|
|
|
/// Record one shadowed texture's copy, of `uses` in `stages`, writing its truth where
|
|
/// `writes_truth`, by `record`: as any command in an encoder Metal orders, else in `run`,
|
|
/// after a barrier from everything before where the run takes one ([`ShadowRun::before`]).
|
|
/// A copy of a detached buffer's bytes records nothing, nor does what follows
|
|
/// ([`skips`](Self::skips)).
|
|
fn shadow_command(
|
|
&mut self,
|
|
run: &mut ShadowRun,
|
|
(stages, uses, writes_truth): (vk::PipelineStageFlags2, &[Use<ResourceKey>], bool),
|
|
record: impl FnOnce(&Vk, vk::CommandBuffer),
|
|
) {
|
|
if self.uses.ordered() {
|
|
self.command_named(stages, uses, &[], record);
|
|
return;
|
|
}
|
|
if self.skips(uses.iter().map(|used| used.key)) {
|
|
return;
|
|
}
|
|
let aliased = self.aliased(uses);
|
|
let tracked = aliased.as_deref().unwrap_or(uses);
|
|
if run.before(tracked, writes_truth) {
|
|
self.barrier(Barrier::everything());
|
|
}
|
|
let Some(raw) = self.recording() else {
|
|
return;
|
|
};
|
|
record(self.device().shared().vk(), raw);
|
|
self.open.command_buffer.note_dirty(tracked);
|
|
self.uses.record(stages, tracked, None);
|
|
self.touch(uses);
|
|
}
|
|
|
|
/// Leave `copy`, a shadow's copy in over host pages a consumer logs that the encoder tracked
|
|
/// already ([`shadow_command`](Self::shadow_command)), to its batch's submission, its regions
|
|
/// decided by `gate` then (`shadow::GatedCopyIn`), the encoder going on in a new
|
|
/// `VkCommandBuffer`. A render pass held for an instance recorded inline
|
|
/// ([`hold_pass`](Self::hold_pass)) stays held: the copies of its first instance go before
|
|
/// the pass, so the segment goes between the command buffers of the commands before it,
|
|
/// ahead of the pass's draws that read the copy: there the encoder begins no
|
|
/// `VkCommandBuffer` to go on in until a command records before the pass
|
|
/// ([`defers_apart`](Self::defers_apart)). Recorded whole here instead inside a predicated
|
|
/// loop's laying, which its check may submit again, or where no new `VkCommandBuffer` can be
|
|
/// made. Whether it went apart so, with no barrier of the encoder's before it.
|
|
fn defer_copy_in(&mut self, copy: &ShadowCopy, gate: CopyGate) -> bool {
|
|
let laying = (self.open.command_buffer.state().predication_mut()).laying();
|
|
let apart = self.defers_apart();
|
|
let next = match (laying, apart) {
|
|
(true, _) => None,
|
|
(false, true) => Some(None),
|
|
(false, false) => self.continuation().ok().map(Some),
|
|
};
|
|
let Some(next) = next else {
|
|
if matches!(gate, CopyGate::Standing) {
|
|
self.copy_in_unskipped(copy, Unskipped::RecordedWhole);
|
|
}
|
|
if let Some(raw) = self.recording() {
|
|
// SAFETY: the encoder's command buffer is recording outside a render pass; its
|
|
// commands retain the shadowed texture until it completes, and its image rests in
|
|
// the `GENERAL` layout.
|
|
unsafe { copy.record(self.device().shared().vk(), raw, None) };
|
|
}
|
|
return false;
|
|
};
|
|
let work = Deferred::GatedCopyIn(Arc::new(GatedCopyIn::new(copy.clone(), gate)));
|
|
// Refused only where the current `VkCommandBuffer` does not end, which abandons the
|
|
// command buffer.
|
|
let _abandoned = self.defer_into(next, work);
|
|
apart
|
|
}
|
|
|
|
/// Whether a shadow's gated copy in goes apart from the encoder's commands with no
|
|
/// `VkCommandBuffer` of the encoder's around it: where a render pass is held for an instance
|
|
/// recorded inline ([`hold_pass`](Self::hold_pass)), whose release gives the encoder its
|
|
/// command buffer to go on in, in an encoder whose commands Metal leaves unordered, outside a
|
|
/// predicated loop's laying. Its segment records a barrier from everything before it and one
|
|
/// to everything after it (`GatedCopyIn::record`), which order it among the encoder's
|
|
/// commands as the encoder's own barriers around it would: so the encoder records neither,
|
|
/// nor begins a `VkCommandBuffer` for them, before the pass or after the copy
|
|
/// (`WorkCounts::command_buffers_begun_continuations`).
|
|
fn defers_apart(&mut self) -> bool {
|
|
self.pass.is_some()
|
|
&& !self.uses.ordered()
|
|
&& !(self.open.command_buffer.state().predication_mut()).laying()
|
|
}
|
|
|
|
/// Track a gated copy in, in `stages` with `uses`, that goes apart
|
|
/// ([`defers_apart`](Self::defers_apart)), as [`shadow_command`](Self::shadow_command) tracks
|
|
/// a copy it records, recording nothing: whether it is to be made (a copy of a detached
|
|
/// buffer's bytes is not, nor what follows, [`skips`](Self::skips)).
|
|
fn track_deferred(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
) -> bool {
|
|
if self.skips(uses.iter().map(|used| used.key)) {
|
|
return false;
|
|
}
|
|
let aliased = self.aliased(uses);
|
|
let tracked = aliased.as_deref().unwrap_or(uses);
|
|
self.open.command_buffer.note_dirty(tracked);
|
|
self.uses.record(stages, tracked, None);
|
|
self.touch(uses);
|
|
true
|
|
}
|
|
|
|
/// Whether a command of an encoder of the command buffer that ended before this one wrote the
|
|
/// truth of `copy`'s shadow, the memory it joined or its image (`ShadowCopies::standing_keys`),
|
|
/// which its batch's submission would find (`submit.rs`'s `stands`). `false` as an ended
|
|
/// encoder is encoded again (`replay.rs`), the encoders after it among those ended: its
|
|
/// batch's submission decides.
|
|
fn ended_encoders_wrote(&self, copy: &ShadowCopy) -> bool {
|
|
if self.replaying {
|
|
return false;
|
|
}
|
|
let keys = copy.copies.standing_keys();
|
|
(self.open.command_buffer.state()).ended_encoders_write_any(&keys)
|
|
}
|
|
|
|
/// Count `copy`, an encoder's first copy in of a shadow whose truth only the runtime's
|
|
/// commands write, as moving its texels, decided `at` as the encoder records it
|
|
/// (`shadow_standing.rs`): of one that may stand, by what its standing held by the batches
|
|
/// submitted so far.
|
|
fn copy_in_unskipped(&self, copy: &ShadowCopy, at: Unskipped) {
|
|
let shared = self.device().shared();
|
|
let let_go = match at {
|
|
Unskipped::Unstanding => None,
|
|
_ => shared.standings.let_go(©.copies),
|
|
};
|
|
let missed = copy.copies.standing.last_missed();
|
|
(shared.counts).shadow_copy_in_unskipped(at, let_go, missed, copy.texels_copied_in());
|
|
}
|
|
|
|
/// The `VkCommandBuffer` a render pass holds ([`hold_pass`](Self::hold_pass)), until its
|
|
/// release.
|
|
pub(super) fn held_pass(&self) -> Option<vk::CommandBuffer> {
|
|
self.pass
|
|
}
|
|
|
|
/// Record shadowed textures' `copies` into the command buffer a render pass holds
|
|
/// ([`hold_pass`](Self::hold_pass)), between two of its render pass instances, as the pass's
|
|
/// end records them there (`render/inline_splits.rs`): as one run ([`ShadowRun`]) after the
|
|
/// barrier from everything before that the caller records at the instance's end, with one
|
|
/// to everything after its last copy; their uses the encoder's, an unordered one's. A copy of
|
|
/// a detached buffer's bytes records nothing, nor does what follows ([`skips`](Self::skips)).
|
|
/// Nothing where no pass holds one.
|
|
pub(super) fn held_shadow_copies(&mut self, copies: &[ShadowCopy]) {
|
|
let Some(held) = self.pass else {
|
|
return;
|
|
};
|
|
let device = self.device().clone();
|
|
let raw = device.shared().vk();
|
|
let mut run = ShadowRun::after_barrier();
|
|
for copy in copies {
|
|
let rows = copy.staged_back(&device);
|
|
let uses = copy.uses_with(rows.as_ref());
|
|
if self.skips(uses.iter().map(|used| used.key)) {
|
|
continue;
|
|
}
|
|
// An encoder's first copy in of a shadow whose truth only the runtime's commands write
|
|
// moves the texels here, decided by no batch's submission: counted by why.
|
|
if copy.first_from_private_truth() {
|
|
let at = match copy.left_to_standing() {
|
|
true => Unskipped::BetweenInstances,
|
|
false => Unskipped::Unstanding,
|
|
};
|
|
self.copy_in_unskipped(copy, at);
|
|
}
|
|
let aliased = self.aliased(&uses);
|
|
let tracked = aliased.as_deref().unwrap_or(&uses);
|
|
let before = run.before(tracked, copy.writes_truth());
|
|
// SAFETY: the held command buffer is recording outside a render pass instance (the
|
|
// caller ended the one before), and only this encoder records it; its commands retain
|
|
// the shadowed textures (and their views) and the rows' set until it completes, and
|
|
// every image rests in the `GENERAL` layout.
|
|
unsafe {
|
|
if before {
|
|
Barrier::everything().record(raw, held);
|
|
}
|
|
copy.record(raw, held, rows.as_ref());
|
|
}
|
|
let stages = copy.stages_with(rows.as_ref());
|
|
self.open.command_buffer.note_dirty(tracked);
|
|
self.uses.record(stages, tracked, None);
|
|
self.touch(&uses);
|
|
if let Some(sets) = rows.and_then(RowCopies::retained) {
|
|
self.retain(sets);
|
|
}
|
|
}
|
|
if run.recorded() {
|
|
// SAFETY: as above.
|
|
unsafe { Barrier::everything().record(raw, held) };
|
|
}
|
|
}
|
|
|
|
/// The encoder's uses, for its fence waits and updates.
|
|
pub(super) fn uses(&mut self) -> &mut EncoderUses<ResourceKey> {
|
|
&mut self.uses
|
|
}
|
|
|
|
/// Leave `work` to record here later: the shadowed textures written since their last copy
|
|
/// back are copied back where the work leaves them (`Deferred::leaves_shadows`), the
|
|
/// encoder's `VkCommandBuffer` ends, and its later commands go into a new one. Where the GPU
|
|
/// may write what the work reads, the segment starts a part of the encoder of its own (the
|
|
/// module's segments). Refused, changing nothing, when the new one cannot be made; a failure
|
|
/// to end the current one abandons the command buffer.
|
|
pub(super) fn defer(&mut self, work: Deferred) -> Result<(), Error> {
|
|
self.release_pass()?;
|
|
let next = self.continuation()?;
|
|
self.defer_into(Some(next), work)
|
|
}
|
|
|
|
/// Note the uses `uses`, in `stages`, of work a segment left to a later recording records
|
|
/// there (a linear attachment's merge left to its batch's submission, `pass_runs.rs`), as
|
|
/// [`command`](Self::command) notes a command's: the tracker orders the commands after the
|
|
/// segment after them, and the encoder's summary holds them. The barrier the tracker plans
|
|
/// before them goes unrecorded: the work records one from every command before it. Whether
|
|
/// they were noted: not where they name a detached buffer, after which nothing records
|
|
/// ([`skips`](Self::skips)).
|
|
pub(super) fn note_left_uses(
|
|
&mut self,
|
|
stages: vk::PipelineStageFlags2,
|
|
uses: &[Use<ResourceKey>],
|
|
) -> bool {
|
|
let none = BTreeSet::new();
|
|
self.track(stages, uses, (&none, Reach::Unnamed), &[])
|
|
.is_some()
|
|
}
|
|
|
|
/// Whether this core encodes an ended encoder again (`command_buffer/reencode.rs`).
|
|
pub(super) fn encodes_again(&self) -> bool {
|
|
self.replaying
|
|
}
|
|
|
|
/// Go on with a run of passes over the shadowed texture of `copies`, kept in `merge` before
|
|
/// the run's first (`pass_runs.rs`, `ShadowSync::go_on`): whether the encoder carries its
|
|
/// image, and took it so.
|
|
pub(super) fn go_on_shadow(
|
|
&mut self,
|
|
copies: &Arc<ShadowCopies>,
|
|
merge: Arc<ShadowMerge>,
|
|
) -> bool {
|
|
self.shadows.go_on(copies, merge)
|
|
}
|
|
|
|
/// The merge of what the encoder's commands wrote through the shadowed texture of `copies`,
|
|
/// which a run's pass leaves to its batch's submission (`pass_runs.rs`,
|
|
/// `ShadowSync::leave_merge`).
|
|
pub(super) fn leave_shadow_merge(&mut self, copies: &ShadowCopies) -> Option<ShadowCopy> {
|
|
self.shadows.leave_merge(copies)
|
|
}
|
|
|
|
/// The encoder's hold on what `merge` took (its scratch, or its kept image), handed to a run
|
|
/// of passes that holds it past the encoder's end (`pass_runs.rs`); `None` where it holds
|
|
/// none for it.
|
|
pub(super) fn take_lease(&mut self, merge: &Arc<ShadowMerge>) -> Option<MergeLease> {
|
|
let merge = Arc::downgrade(merge);
|
|
let at = (self.kept_leases.iter()).position(|(of, _)| of.ptr_eq(&merge))?;
|
|
Some(self.kept_leases.swap_remove(at).1)
|
|
}
|
|
|
|
/// The new `VkCommandBuffer` the encoder's commands go on in after a segment
|
|
/// ([`defer`](Self::defer)), allocated from the command buffer's pool and begun. Left unused
|
|
/// on a refusal, it goes with the pool.
|
|
pub(super) fn continuation(&self) -> Result<Continuation, Error> {
|
|
let command_buffer = &self.open.command_buffer;
|
|
let (next, laying) = {
|
|
let mut state = command_buffer.state();
|
|
let next = command_buffer.allocate_raw(&mut state)?;
|
|
(next, state.predication_mut().laying())
|
|
};
|
|
if !laying {
|
|
self.begin(next, vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT)?;
|
|
return Ok(Continuation(next, Laid::Outside));
|
|
}
|
|
// Inside a predicated loop's laying, which its check may submit again
|
|
// (`command_buffer/rerun.rs`): begun to be submitted again, behind a barrier from
|
|
// everything before it, the work left apart before it among them.
|
|
self.begin(next, vk::CommandBufferUsageFlags::empty())?;
|
|
// SAFETY: just begun, and only this encoder records it.
|
|
unsafe { Barrier::everything().record(self.device().shared().vk(), next) };
|
|
command_buffer.state().predication_mut().laid_into(next);
|
|
Ok(Continuation(next, Laid::Inside))
|
|
}
|
|
|
|
/// End the `VkCommandBuffer` the encoder records into and go on in a new one, begun to be
|
|
/// submitted again, for a predicated loop's laying (`predication.rs`), which its check may
|
|
/// run again: the textures it shadows copied back first, so the laying copies in again what
|
|
/// it reads, and a barrier from everything before at its start. Its handle. Refused where the
|
|
/// current one does not end, or none can be made, which abandons the command buffer.
|
|
pub(super) fn cut_reusable(&mut self) -> Result<vk::CommandBuffer, Error> {
|
|
let copies = self.shadows.leave();
|
|
let copies = self.sample_moves(copies);
|
|
self.shadow_copies(&copies);
|
|
self.barrier(Barrier::everything());
|
|
let next = {
|
|
let command_buffer = &self.open.command_buffer;
|
|
let mut state = command_buffer.state();
|
|
command_buffer.allocate_raw(&mut state)
|
|
};
|
|
let ended = next.and_then(|next| {
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { self.open.command_buffer.end_raw(self.raw) }.map_err(vulkan)?;
|
|
Ok(next)
|
|
});
|
|
let next = match ended {
|
|
Ok(next) => next,
|
|
Err(error) => {
|
|
self.open.command_buffer.state().abandoned = true;
|
|
return Err(error);
|
|
}
|
|
};
|
|
self.ended.push(self.raw);
|
|
self.raw = next;
|
|
if let Err(error) = self.begin(next, vk::CommandBufferUsageFlags::empty()) {
|
|
self.open.command_buffer.state().abandoned = true;
|
|
return Err(error);
|
|
}
|
|
self.barrier(Barrier::everything());
|
|
Ok(next)
|
|
}
|
|
|
|
/// End a predicated loop's laying (`predication.rs`) with its check, `work`: the textures it
|
|
/// shadows copied back, a barrier from everything before to every later command and to the
|
|
/// host, which reads the unit's halt word; the encoder goes on after the check.
|
|
pub(super) fn end_laying(&mut self, work: Deferred) -> Result<(), Error> {
|
|
let copies = self.shadows.leave();
|
|
let copies = self.sample_moves(copies);
|
|
self.shadow_copies(&copies);
|
|
self.barrier(Barrier::to_all());
|
|
self.defer(work)
|
|
}
|
|
|
|
/// Leave what the encoder recorded so far out of the command buffer's work, as a
|
|
/// command-predication record that drops it does (`predication.rs`): its `VkCommandBuffer`s so
|
|
/// far, ended, are never submitted; the work it left apart is never recorded, and is held as
|
|
/// what its commands use is; the textures it shadows are forgotten unread, so nothing is
|
|
/// copied back and a later command copies in again; and its later commands go on in a new
|
|
/// `VkCommandBuffer`, in a part of their own, which needs no barrier after the commands left
|
|
/// out. Its parts so far end as a failed encoder's do, with their fences' waits alone
|
|
/// ([`end_with_replay`](Self::end_with_replay)). Refused where no new one can be made, or the
|
|
/// current one does not end, which abandons the command buffer.
|
|
pub(super) fn leave_out_recorded(&mut self) -> Result<(), Error> {
|
|
self.release_pass()?;
|
|
self.flush_gathers();
|
|
let next = self.continuation()?;
|
|
if self.raw != vk::CommandBuffer::null() {
|
|
// SAFETY: as in `barrier`.
|
|
if let Err(error) = unsafe { self.open.command_buffer.end_raw(self.raw) } {
|
|
self.open.command_buffer.state().abandoned = true;
|
|
return Err(vulkan(error));
|
|
}
|
|
}
|
|
self.raw = next.0;
|
|
self.recorded = false;
|
|
self.ended.clear();
|
|
// A segment the GPU may write what it reads for started a part of its own.
|
|
self.left_out += 1 + self.segments.iter().filter(|segment| segment.host).count();
|
|
for segment in self.segments.drain(..) {
|
|
self.retained
|
|
.boxed(Box::new((segment.work, segment.inputs)));
|
|
}
|
|
self.uses.split();
|
|
self.shadows.forget();
|
|
Ok(())
|
|
}
|
|
|
|
/// Begin `raw`, a `VkCommandBuffer` the command buffer's pool just handed out, with `flags`.
|
|
fn begin(
|
|
&self,
|
|
raw: vk::CommandBuffer,
|
|
flags: vk::CommandBufferUsageFlags,
|
|
) -> Result<(), Error> {
|
|
let begin = vk::CommandBufferBeginInfo::default().flags(flags);
|
|
// SAFETY: the command buffer was just allocated from the pool; only this encoder
|
|
// records it.
|
|
unsafe {
|
|
self.device()
|
|
.shared()
|
|
.vk()
|
|
.begin_command_buffer(raw, &begin)
|
|
}
|
|
.map_err(vulkan)?;
|
|
let counts = &self.device().shared().counts;
|
|
counts.encoder_command_buffer_begun(Begun::Continuation);
|
|
Ok(())
|
|
}
|
|
|
|
/// [`defer`](Self::defer) `work`, the encoder going on in `next`, or where none, in one begun
|
|
/// at its next command ([`raw_recording`](Self::raw_recording)): refused only where the
|
|
/// current `VkCommandBuffer` does not end, which abandons the command buffer.
|
|
fn defer_into(&mut self, next: Option<Continuation>, work: Deferred) -> Result<(), Error> {
|
|
self.flush_gathers();
|
|
if work.leaves_shadows() {
|
|
let copies = self.shadows.leave();
|
|
let copies = self.sample_moves(copies);
|
|
self.shadow_copies(&copies);
|
|
}
|
|
// None where a pass handed its command buffer over to the work
|
|
// ([`hand_over_pass`](Self::hand_over_pass)) and no command came since.
|
|
if self.raw != vk::CommandBuffer::null() {
|
|
// SAFETY: as in `barrier`.
|
|
if let Err(error) = unsafe { self.open.command_buffer.end_raw(self.raw) } {
|
|
self.open.command_buffer.state().abandoned = true;
|
|
return Err(vulkan(error));
|
|
}
|
|
self.ended.push(self.raw);
|
|
}
|
|
self.raw = next
|
|
.as_ref()
|
|
.map_or(vk::CommandBuffer::null(), |next| next.0);
|
|
self.recorded = false;
|
|
// Inside a predicated loop's laying, the work goes among its pieces, before the command
|
|
// buffer the encoder goes on in (`command_buffer/rerun.rs`).
|
|
if next.is_some_and(|next| next.1 == Laid::Inside) {
|
|
(self.open.command_buffer.state().predication_mut()).laid_apart(&work);
|
|
}
|
|
let host = work.gpu_written();
|
|
if host {
|
|
self.uses.split();
|
|
}
|
|
self.segments.push(Segment {
|
|
at: self.ended.len(),
|
|
executions_at: self.executions.len(),
|
|
work,
|
|
host,
|
|
inputs: None,
|
|
recorded: None,
|
|
stepped: false,
|
|
pipelines: false,
|
|
gpu_alone: false,
|
|
bound: None,
|
|
});
|
|
Ok(())
|
|
}
|
|
|
|
/// Leave `dispatch`, whose pipelines are being made and which the encoder tracked already
|
|
/// ([`command_later`](Self::command_later)), to record later (`pending_dispatch.rs`), the
|
|
/// encoder going on in `next`. Refused only where the current `VkCommandBuffer` does not
|
|
/// end, which abandons the command buffer.
|
|
pub(super) fn pend(
|
|
&mut self,
|
|
next: Continuation,
|
|
dispatch: PendingDispatch,
|
|
) -> Result<(), Error> {
|
|
self.defer_into(Some(next), Deferred::Pending(Arc::new(dispatch)))
|
|
}
|
|
|
|
/// Metal fails the command buffer from this encoder on with `error`, as it commits: the
|
|
/// encoder and every one after it run nothing (`State::fail_from`).
|
|
pub(super) fn fail_command_buffer(&mut self, error: CommandBufferError) {
|
|
self.fails_command_buffer = Some(error);
|
|
}
|
|
|
|
/// A command of this encoder uses a memoryless texture's memory, which it has none of: plain
|
|
/// Metal fails the command buffer from the submission that holds the encoder on
|
|
/// (`CommandBufferError::InvalidResource`, `State::fail_submission_from`), its GPU running
|
|
/// none of that submission, so neither a hang nor a discard there happens. Another failure the
|
|
/// encoder found first keeps its error.
|
|
pub(super) fn fail_memoryless_use(&mut self) {
|
|
match self.fails_command_buffer {
|
|
None | Some(CommandBufferError::GpuHang | CommandBufferError::GpuRecoveryVictim) => {
|
|
self.fails_command_buffer = Some(CommandBufferError::InvalidResource);
|
|
}
|
|
Some(_) => {}
|
|
}
|
|
}
|
|
|
|
/// Metal's GPU hangs at this encoder (a direct draw of primitive type 9, or a pass of no draw
|
|
/// call over a mutable rasterization-rate map, `rate_mapped_pass.rs`), as the command
|
|
/// buffer commits: it fails from the encoder on with `CommandBufferError::GpuHang`, unless
|
|
/// the encoder fails it otherwise first, and holds what follows (`State::hang_from`).
|
|
pub(super) fn hang(&mut self) {
|
|
self.fails_command_buffer
|
|
.get_or_insert(CommandBufferError::GpuHang);
|
|
}
|
|
|
|
/// Metal's GPU discards the command buffer at this encoder (an object grid past what it runs
|
|
/// within its watchdog), as it commits: it fails from the encoder on with
|
|
/// `CommandBufferError::GpuRecoveryVictim`, unless the encoder fails it otherwise first.
|
|
pub(super) fn discard(&mut self) {
|
|
self.fails_command_buffer
|
|
.get_or_insert(CommandBufferError::GpuRecoveryVictim);
|
|
}
|
|
|
|
/// Metal's GPU faults this encoder, as the command buffer commits: the encoder runs nothing,
|
|
/// and the command buffer completes with `error` (`CommandBufferError::PageFault`, or
|
|
/// `GpuRecoveryVictim`), the first faulted encoder's (`State::fault`).
|
|
pub(super) fn fault(&mut self, error: CommandBufferError) {
|
|
self.faults.get_or_insert(error);
|
|
}
|
|
|
|
/// Note that its render pass is over `map`, which it took as `state`: a reset of the map
|
|
/// before its command buffer's submission has it encoded again (`rate_mapped_pass.rs`).
|
|
pub(super) fn take_rate_map(
|
|
&mut self,
|
|
map: crate::rate_map::RasterizationRateMap,
|
|
state: std::sync::Arc<crate::rate_map::MapState>,
|
|
) {
|
|
self.taken.take_rate_map(map, state);
|
|
}
|
|
|
|
/// End the encoder, one of `kind` that made `residency` resident (a compute or render
|
|
/// encoder's): its shadowed textures written since their last copy back are copied back,
|
|
/// it joins the command buffer's ended encoders (`State::encoder_ended`), and its recording
|
|
/// and what it used join the command buffer's, in order, one part at a time, each part
|
|
/// after the first in a batch of its own.
|
|
/// The command buffer keeps `replay`, the encoder's log (`replay.rs`), where a rebind or a
|
|
/// reset before its first submission may need the encoder encoded again (a command of it
|
|
/// named a buffer over host pages, detached ones included, or its pass is over a
|
|
/// rasterization-rate map) and the encoder can be (it left no segment for
|
|
/// later but one an encoding again records at once, `Segment::records_again`, and fails
|
|
/// nothing as it commits); else the log goes here.
|
|
pub(super) fn end_with_replay(
|
|
mut self,
|
|
kind: EncoderKind,
|
|
residency: Option<Residency>,
|
|
replay: Option<Replay>,
|
|
) -> Result<(), Error> {
|
|
// A pass ended holds no command buffer (`render.rs`); this one ends here if one did.
|
|
self.release_pass()?;
|
|
// What Metal's identifier allocations follow as the command buffer is scheduled
|
|
// (`unique_identifier.rs`): a compute or blit encoder that encoded a command, and any
|
|
// render encoder.
|
|
let worked = self.recorded || !self.executions.is_empty() || !self.segments.is_empty();
|
|
let work = ScheduledWork {
|
|
dispatch: worked && matches!(kind, EncoderKind::Compute | EncoderKind::Blit),
|
|
render: kind == EncoderKind::Render,
|
|
};
|
|
self.open.command_buffer.note_scheduled_work(work);
|
|
let sharing = self.shadows.sharing_truth();
|
|
if sharing > 0 {
|
|
self.device().shared().counts.shadows_sharing_truth(sharing);
|
|
}
|
|
let copies = self.shadows.finish();
|
|
let copies = self.sample_moves(copies);
|
|
self.count_shadow_copies(&copies);
|
|
self.shadow_copies(&copies);
|
|
// Whether it keeps its log, once its last commands, the copies back above among them,
|
|
// named what they name: a copy back may be the only command that names a texture's
|
|
// bytes over host pages (a render pass that clears a shadowed target copies nothing in).
|
|
// Why it keeps none, where a command named a buffer over host pages: the rebound notice
|
|
// says (`command_buffer/rebound_use.rs`).
|
|
let unlogged = match &replay {
|
|
_ if !self.taken.encodes_again() => None,
|
|
None => Some(Unlogged::NoCalls),
|
|
Some(_) if !self.segments.iter().all(Segment::records_again) => {
|
|
Some(Unlogged::Segments)
|
|
}
|
|
Some(_) if self.fails_command_buffer.is_some() || self.faults.is_some() => {
|
|
Some(Unlogged::Fails)
|
|
}
|
|
Some(_) => None,
|
|
};
|
|
// A log not kept goes back to the device's spares (`replay.rs`).
|
|
let replay = replay.and_then(|log| {
|
|
let keeps = self.segments.iter().all(Segment::records_again)
|
|
&& self.fails_command_buffer.is_none()
|
|
&& self.faults.is_none()
|
|
&& self.taken.encodes_again();
|
|
if !keeps {
|
|
self.device().shared().scheduler.spare_logs().give(log);
|
|
return None;
|
|
}
|
|
Some(log)
|
|
});
|
|
// Its commands were recorded for one layout of each image it holds
|
|
// (`texture/held_depth.rs`).
|
|
if self.uses.broke_hold() {
|
|
return Err(internal("a held depth used but as its attachment"));
|
|
}
|
|
// Its last `VkCommandBuffer` stays recording, the command buffer's tail: its batch's end
|
|
// may go into it (`CommandBufferShared::close_tail`).
|
|
let EncoderCore {
|
|
mut open,
|
|
raw,
|
|
barriered,
|
|
mut ended,
|
|
segments,
|
|
left_out,
|
|
uses,
|
|
retained,
|
|
executions,
|
|
mut shadows,
|
|
keys,
|
|
attachment_shadows,
|
|
first_copies,
|
|
mut heaps,
|
|
holds,
|
|
taken,
|
|
fails_command_buffer,
|
|
faults,
|
|
twin_writes,
|
|
routes,
|
|
aliases_quiet,
|
|
..
|
|
} = self;
|
|
ended.push(raw);
|
|
// The keys it found, for the device's next encoder (`host_aliases::Quiet`).
|
|
(open.command_buffer.generation().aliases).give_quiet(aliases_quiet);
|
|
let keyed = open
|
|
.command_buffer
|
|
.device()
|
|
.shared()
|
|
.heap_keys
|
|
.among(keys.iter().copied());
|
|
heaps.extend(keyed);
|
|
let mut state = open.command_buffer.state();
|
|
if !attachment_shadows.is_empty() {
|
|
let first = state.shadows_attached(&attachment_shadows);
|
|
(open.command_buffer.device().shared().counts)
|
|
.shadow_attachments(attachment_shadows.len(), first);
|
|
}
|
|
// What the encoder leaves the command buffer's later encoders (`shadow::ShadowCarry`).
|
|
state.shadows_carried(shadows.ended());
|
|
// What its first uses of copies taken at use decided, for an encoding of it again and
|
|
// for a part of it that runs nothing to give back (`shadow::Takes`).
|
|
let takes = shadows.into_takes();
|
|
if !first_copies.is_empty() {
|
|
let again = state.shadows_copied_in(&first_copies);
|
|
if again > 0 {
|
|
(open.command_buffer.device().shared().counts)
|
|
.shadow_texels_first_copied_in_again(again);
|
|
}
|
|
}
|
|
state.use_heaps(heaps);
|
|
state.routes.join(routes);
|
|
let encoder = state.encoder_ended(kind, keys, residency, (taken, takes));
|
|
if let Some(why) = unlogged {
|
|
state.encoder_unlogged(encoder, why);
|
|
}
|
|
state.encoder_holds(encoder, uses.held().to_vec());
|
|
match fails_command_buffer {
|
|
Some(CommandBufferError::GpuHang) => {
|
|
state.hang_from(encoder, open.command_buffer.device())?;
|
|
}
|
|
// A memoryless texture's use fails Metal's whole submission (`InvalidResource`).
|
|
Some(error @ CommandBufferError::InvalidResource) => {
|
|
state.fail_submission_from(encoder, error);
|
|
}
|
|
Some(error) => state.fail_from(encoder, error),
|
|
None => {}
|
|
}
|
|
if let Some(error) = faults {
|
|
state.fault(encoder, error, open.command_buffer.device())?;
|
|
}
|
|
let base = state.executions.len();
|
|
// One part, its executions these: a log not kept comes back, to go without the lock.
|
|
let logged = replay.as_ref().map(Replay::bytes);
|
|
let executed = base..base + executions.len();
|
|
let unkept = state.keep_replay(encoder, replay, executed);
|
|
if let (Some(bytes), None) = (logged, &unkept) {
|
|
open.command_buffer
|
|
.device()
|
|
.shared()
|
|
.counts
|
|
.replay_log_kept(bytes);
|
|
}
|
|
let mut summaries = uses.finish_parts().into_iter();
|
|
// The parts a command-predication record left out run nothing, each with its fences'
|
|
// waits alone (`leave_out_recorded`), in the batch the encoder's first part joins.
|
|
for _ in 0..left_out {
|
|
let summary = summaries
|
|
.next()
|
|
.ok_or_else(|| internal("an encoder's part without its summary"))?;
|
|
state.end_encoder(
|
|
&open.command_buffer,
|
|
Recorded {
|
|
encoder,
|
|
raws: Vec::new(),
|
|
barriered,
|
|
segments: Vec::new(),
|
|
summary: summary.fences_only(),
|
|
sparse: Vec::new(),
|
|
twin_writes: Vec::new(),
|
|
},
|
|
);
|
|
}
|
|
let parts = into_parts(ended, segments, base);
|
|
// The writes into ASTC carriers go with the last part (a blit encoder's only one).
|
|
let count = parts.len();
|
|
let mut twin_writes = Some(twin_writes);
|
|
for (at, (raws, segments)) in parts.into_iter().enumerate() {
|
|
let summary = summaries
|
|
.next()
|
|
.ok_or_else(|| internal("an encoder's part without its summary"))?;
|
|
if at > 0 {
|
|
state.host_step();
|
|
}
|
|
let twin_writes = match at + 1 == count {
|
|
true => twin_writes.take().unwrap_or_default(),
|
|
false => Vec::new(),
|
|
};
|
|
state.end_encoder(
|
|
&open.command_buffer,
|
|
Recorded {
|
|
encoder,
|
|
raws,
|
|
barriered,
|
|
segments,
|
|
summary,
|
|
sparse: Vec::new(),
|
|
twin_writes,
|
|
},
|
|
);
|
|
}
|
|
state.tail = Some(raw);
|
|
state.retained.extend(retained.into_entry());
|
|
state.executions.extend(executions);
|
|
if !holds.is_empty() {
|
|
state.holds.push(holds);
|
|
}
|
|
drop(state);
|
|
if let Some(log) = unkept {
|
|
let device = open.command_buffer.device();
|
|
device.shared().scheduler.spare_logs().give(log);
|
|
}
|
|
open.ended = true;
|
|
Ok(())
|
|
}
|
|
|
|
/// End a core made by [`replaying`](Self::replaying): its shadowed textures written since
|
|
/// their last copy back are copied back, and what it recorded comes back for its encoder's
|
|
/// place (`State::splice_replay`), in its parts, each segment it left recorded at once on the
|
|
/// GPU alone but one whose pipelines are being made or failed to make, left as the first
|
|
/// encoding left it ([`Segment::keeps_pipelines`]). Refused (internal) where the encoder, encoded again, failed or faulted its
|
|
/// command buffer, or met a detached buffer, which its first encoding kept none of, or left
|
|
/// work that no longer runs on the GPU alone: its place stays as it was.
|
|
pub(super) fn finish_replay(mut self) -> Result<Replayed, Error> {
|
|
let copies = self.shadows.finish();
|
|
let copies = self.sample_moves(copies);
|
|
self.count_shadow_copies(&copies);
|
|
self.shadow_copies(&copies);
|
|
// A pass of a run that a later pass went on over keeps the run's scratch again, from the
|
|
// image it left (a shadowed texture's), or from its box copied in again (a linear
|
|
// attachment's), for that pass's run (`pass_runs.rs`); the encoding again clears them as
|
|
// it ends (`command_buffer/reencode.rs`).
|
|
let keeps = (self.open.command_buffer.state().replaying_keeps).clone();
|
|
let mut shadow_keeps = Vec::new();
|
|
for keep in keeps {
|
|
match keep {
|
|
RunKeep::Shadow(copy) => shadow_keeps.push(copy),
|
|
RunKeep::Linear(merge) => {
|
|
merge.record_reload(&mut self);
|
|
self.retain(merge);
|
|
}
|
|
}
|
|
}
|
|
if !shadow_keeps.is_empty() {
|
|
self.count_shadow_copies(&shadow_keeps);
|
|
self.shadow_copies(&shadow_keeps);
|
|
}
|
|
if self.uses.broke_hold() {
|
|
return Err(internal("a held depth used but as its attachment"));
|
|
}
|
|
self.flush_gathers();
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { self.open.command_buffer.end_raw(self.raw) }.map_err(vulkan)?;
|
|
let EncoderCore {
|
|
open,
|
|
raw,
|
|
mut ended,
|
|
segments,
|
|
uses,
|
|
retained,
|
|
executions,
|
|
keys,
|
|
heaps,
|
|
taken,
|
|
fails_command_buffer,
|
|
faults,
|
|
..
|
|
} = self;
|
|
if fails_command_buffer.is_some() || faults.is_some() || taken.dead() {
|
|
return Err(internal(
|
|
"an encoder encoded again differs from its first encoding",
|
|
));
|
|
}
|
|
let held = uses.held().to_vec();
|
|
let summaries = uses.finish_parts();
|
|
ended.push(raw);
|
|
let split = into_parts(ended, segments, 0);
|
|
if split.len() != summaries.len() {
|
|
return Err(internal("an encoder's part without its summary"));
|
|
}
|
|
// Its segments recorded at once on the GPU alone, as its commit recorded the first
|
|
// encoding's (`command_buffer/reencode.rs`): work that no longer runs there refuses the
|
|
// encoding again, but work whose pipelines are being made, which keeps its segment.
|
|
let mut parts = Vec::with_capacity(split.len());
|
|
for ((raws, mut segments), summary) in split.into_iter().zip(summaries) {
|
|
for segment in &mut segments {
|
|
// Work whose pipelines are being made, or whose making failed, is left as the
|
|
// first encoding left it, prepared as its commit prepared that one
|
|
// (`Deferred::prepare`): recorded here where they are made, else by the host step
|
|
// once they are (`pending_dispatch.rs`, `render/pending_tile.rs`). The rebind's
|
|
// call waits for no making, and makes none itself.
|
|
if segment.keeps_pipelines() {
|
|
let prepared = segment.work.prepare(&open.command_buffer, false)?;
|
|
segment.recorded = prepared.recorded;
|
|
segment.inputs = Some(prepared.inputs);
|
|
segment.pipelines = prepared.pipelines;
|
|
segment.host |= prepared.held;
|
|
continue;
|
|
}
|
|
let recorded = segment.work.record_on_gpu(&open.command_buffer)?;
|
|
let Some((recorded, inputs)) = recorded else {
|
|
return Err(internal(
|
|
"an encoder encoded again whose work no longer runs on the GPU alone",
|
|
));
|
|
};
|
|
segment.recorded = Some(recorded);
|
|
segment.inputs = Some(inputs);
|
|
segment.stepped = true;
|
|
segment.gpu_alone = true;
|
|
}
|
|
parts.push(ReplayedPart {
|
|
raws,
|
|
summary,
|
|
segments,
|
|
});
|
|
}
|
|
Ok(Replayed {
|
|
parts,
|
|
held,
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
heaps,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl EncoderCore {
|
|
/// A segment that records nothing and takes no `VkCommandBuffer`, for work of an encoder
|
|
/// whose submission runs nothing: dead as one whose command named a detached buffer is
|
|
/// ([`skips`](Self::skips), `Taken::die`), so the submission never places it
|
|
/// (`State::place`) and no part of it reaches a `VkQueue`
|
|
/// (`State::runs_nothing_unrunnable`). Its caller has read that the encoder's submission
|
|
/// fails (`CommandBuffer::encoder_rebound`), which no later judging unreads: pages a
|
|
/// command took are compared by identity, and a buffer never lies in the pages it left.
|
|
pub(super) fn unrecorded_segment() -> RecordedSegment {
|
|
let mut taken = Taken::default();
|
|
taken.die();
|
|
RecordedSegment {
|
|
raws: Vec::new(),
|
|
summary: EncoderUses::new(false).finish(),
|
|
retained: Retained::default(),
|
|
executions: Vec::new(),
|
|
keys: BTreeSet::new(),
|
|
taken,
|
|
resident_misses: 0,
|
|
faults: ReplayFaults::default(),
|
|
takes: Takes::default(),
|
|
}
|
|
}
|
|
|
|
/// A segment of `command_buffer` that `record` records alone into a new `VkCommandBuffer` of
|
|
/// its pool, retaining `retained`: no barrier around it and nothing tracked, its encoder
|
|
/// having tracked its commands where it left them ([`command_later`](Self::command_later)),
|
|
/// so it uses nothing past its encoder's summary.
|
|
pub(super) fn untracked_segment(
|
|
command_buffer: &CommandBuffer,
|
|
record: impl FnOnce(&Vk, vk::CommandBuffer),
|
|
retained: Box<dyn Any + Send + Sync>,
|
|
) -> Result<RecordedSegment, Error> {
|
|
let mut core = EncoderCore::segment_raw(command_buffer, false)?;
|
|
let device = core.device().shared().vk();
|
|
record(device, core.raw);
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { core.open.command_buffer.end_raw(core.raw) }.map_err(vulkan)?;
|
|
core.retained.boxed(retained);
|
|
let EncoderCore {
|
|
raw,
|
|
uses,
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
..
|
|
} = core;
|
|
Ok(RecordedSegment {
|
|
raws: vec![raw],
|
|
summary: uses.finish(),
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
resident_misses: 0,
|
|
faults: ReplayFaults::default(),
|
|
takes: Takes::default(),
|
|
})
|
|
}
|
|
|
|
/// End a [`segment`](Self::segment): its shadowed textures written since their last copy
|
|
/// back are copied back, then a barrier to everything recorded after it.
|
|
pub(super) fn end_segment(mut self) -> Result<RecordedSegment, Error> {
|
|
let copies = self.shadows.finish();
|
|
// Its commands wrote the images of the shadowed textures it used, so no command buffer
|
|
// carries one of them on (`shadow::ShadowCarry`): a segment's work lies where the encoder
|
|
// left it, which carries nothing past it either (`shadow::ShadowSync::leave`).
|
|
drop(self.shadows.ended());
|
|
let copies = self.sample_moves(copies);
|
|
self.count_shadow_copies(&copies);
|
|
self.shadow_copies(&copies);
|
|
self.barrier(Barrier::everything());
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { self.open.command_buffer.end_raw(self.raw) }.map_err(vulkan)?;
|
|
let EncoderCore {
|
|
raw,
|
|
mut ended,
|
|
uses,
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
shadows,
|
|
..
|
|
} = self;
|
|
ended.push(raw);
|
|
Ok(RecordedSegment {
|
|
raws: ended,
|
|
summary: uses.finish(),
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
resident_misses: 0,
|
|
faults: ReplayFaults::default(),
|
|
takes: shadows.into_takes(),
|
|
})
|
|
}
|
|
|
|
/// End a core of [`capture`](Self::capture): the gather runs left open recorded, and what it
|
|
/// recorded outside the draws' secondaries, if anything, ended. Refused (internal) where it
|
|
/// deferred work, held a render pass or used a shadowed texture, which no execution's chunk
|
|
/// does.
|
|
pub(super) fn finish_capture(mut self) -> Result<Captured, Error> {
|
|
if !self.segments.is_empty() || self.pass.is_some() || self.used_shadows() {
|
|
return Err(internal(
|
|
"a recording apart that deferred work, held a pass or used a shadowed texture",
|
|
));
|
|
}
|
|
self.flush_gathers();
|
|
if self.raw != vk::CommandBuffer::null() {
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { self.open.command_buffer.end_raw(self.raw) }.map_err(vulkan)?;
|
|
self.ended.push(self.raw);
|
|
}
|
|
let EncoderCore {
|
|
ended,
|
|
uses,
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
holds,
|
|
heaps,
|
|
kept_leases,
|
|
attachment_shadows,
|
|
first_copies,
|
|
twin_writes,
|
|
fails_command_buffer,
|
|
faults,
|
|
resident_unheld,
|
|
resident_notes,
|
|
..
|
|
} = self;
|
|
let resident = match resident_notes {
|
|
ResidentNotes::Apart(part) => part,
|
|
ResidentNotes::Here => None,
|
|
};
|
|
Ok(Captured {
|
|
raws: ended,
|
|
summary: uses.finish(),
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
holds,
|
|
heaps,
|
|
kept_leases,
|
|
attachment_shadows,
|
|
first_copies,
|
|
twin_writes,
|
|
fails_command_buffer,
|
|
faults,
|
|
resident_unheld,
|
|
resident,
|
|
})
|
|
}
|
|
|
|
/// Take what a core of [`capture`](Self::capture) did, as if this core had done it (an
|
|
/// unordered one: a render pass's segment's): its uses noted, its holds, executions, keys,
|
|
/// pages and heaps; and what it recorded outside its draws' secondaries placed after what
|
|
/// this core recorded so far, between barriers from everything before and to everything
|
|
/// after, this core going on in a new command buffer. Refused where the current one does not
|
|
/// end or the new one cannot be begun.
|
|
pub(super) fn absorb(&mut self, captured: Captured) -> Result<(), Error> {
|
|
let Captured {
|
|
raws,
|
|
summary,
|
|
retained,
|
|
executions,
|
|
keys,
|
|
taken,
|
|
holds,
|
|
heaps,
|
|
kept_leases,
|
|
attachment_shadows,
|
|
first_copies,
|
|
twin_writes,
|
|
fails_command_buffer,
|
|
faults,
|
|
resident_unheld,
|
|
resident,
|
|
} = captured;
|
|
if !raws.is_empty() {
|
|
self.flush_gathers();
|
|
self.barrier(Barrier::everything());
|
|
if self.raw != vk::CommandBuffer::null() {
|
|
// SAFETY: as in `barrier`.
|
|
unsafe { self.open.command_buffer.end_raw(self.raw) }.map_err(vulkan)?;
|
|
self.ended.push(self.raw);
|
|
}
|
|
self.ended.extend(raws);
|
|
self.raw = self.continuation()?.0;
|
|
self.recorded = true;
|
|
self.barrier(Barrier::everything());
|
|
}
|
|
self.uses.record(summary.stages(), summary.uses(), None);
|
|
if let Some(part) = resident {
|
|
self.note_resident_part(&part)?;
|
|
}
|
|
self.retained.join(retained);
|
|
self.executions.extend(executions);
|
|
self.keys.extend(keys);
|
|
self.taken.join(taken);
|
|
self.holds.join(holds);
|
|
for heap in heaps {
|
|
if !self.heaps.contains(&heap) {
|
|
self.heaps.push(heap);
|
|
}
|
|
}
|
|
self.kept_leases.extend(kept_leases);
|
|
self.attachment_shadows.extend(attachment_shadows);
|
|
self.first_copies.extend(first_copies);
|
|
self.twin_writes.extend(twin_writes);
|
|
if self.fails_command_buffer.is_none() {
|
|
self.fails_command_buffer = fails_command_buffer;
|
|
}
|
|
if self.faults.is_none() {
|
|
self.faults = faults;
|
|
}
|
|
self.resident_unheld.extend(resident_unheld);
|
|
Ok(())
|
|
}
|
|
|
|
/// Do what a kept chunk's recording did (`kept_commands.rs`), which this core's command
|
|
/// buffer runs again: note its uses, those of keys it did not name as reached through the
|
|
/// resident set alone (`note_reaching`, which holds the pages of buffers over host pages and
|
|
/// names the rest, as its commands did); keep its keys; hold `held`, what it held, upgraded;
|
|
/// bind `heaps` again, the heap set versions its draws bound (`heap_sets::HeapUse`); and add
|
|
/// its execution records where the device keeps them. An unordered core's alone.
|
|
pub(super) fn run_kept(
|
|
&mut self,
|
|
effects: &KeptEffects,
|
|
(held, heaps): Held,
|
|
) -> Result<(), Error> {
|
|
let summary = &effects.summary;
|
|
let resident: BTreeSet<ResourceKey> = summary
|
|
.keys()
|
|
.filter(|key| !effects.named.contains(key))
|
|
.collect();
|
|
self.note_reaching(summary.stages(), (summary.uses(), &resident), None)?;
|
|
if let Some(part) = &effects.resident {
|
|
self.note_resident_part(part)?;
|
|
}
|
|
self.touch_keys(effects.keys.iter().copied());
|
|
for object in held {
|
|
self.retain_shared(object);
|
|
}
|
|
if !heaps.is_empty() {
|
|
self.command_buffer().state().heaps.bind_again(heaps);
|
|
}
|
|
if self.records_executions() {
|
|
self.executions.extend(effects.executions.iter().cloned());
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// What a core of [`EncoderCore::capture`] did, its recording ended
|
|
/// ([`finish_capture`](EncoderCore::finish_capture)): what its segment's core takes
|
|
/// ([`absorb`](EncoderCore::absorb)), and, of a plain one, what a kept recording does again
|
|
/// (`kept_commands.rs`).
|
|
pub(super) struct Captured {
|
|
/// What it recorded outside the draws' secondaries, ended, in order.
|
|
raws: Vec<vk::CommandBuffer>,
|
|
pub(super) summary: Summary<ResourceKey>,
|
|
pub(super) retained: Retained,
|
|
pub(super) executions: Vec<Execution>,
|
|
pub(super) keys: BTreeSet<ResourceKey>,
|
|
pub(super) taken: Taken,
|
|
holds: Holds,
|
|
heaps: Vec<Heap>,
|
|
kept_leases: Vec<(Weak<ShadowMerge>, MergeLease)>,
|
|
attachment_shadows: Vec<ResourceKey>,
|
|
first_copies: Vec<(ResourceKey, u64)>,
|
|
twin_writes: Vec<TwinWrite>,
|
|
fails_command_buffer: Option<CommandBufferError>,
|
|
faults: Option<CommandBufferError>,
|
|
resident_unheld: Vec<ResourceKey>,
|
|
/// The uses of what its encoder made resident its draws left to the core that takes what it
|
|
/// did ([`ResidentPart`]), where they left some.
|
|
pub(super) resident: Option<Arc<ResidentPart>>,
|
|
}
|
|
|
|
impl Captured {
|
|
/// Whether it only noted uses, kept keys, held shared objects, recorded executions and took
|
|
/// pages of buffers over host pages, which a command buffer that runs its recording again
|
|
/// does again as it notes the uses ([`EncoderCore::run_kept`]): nothing recorded outside the
|
|
/// draws' secondaries, no heap reached, no shadowed texture's work, no failure or fault, no
|
|
/// detached buffer named and no rasterization-rate map taken.
|
|
pub(super) fn plain(&self) -> bool {
|
|
self.raws.is_empty()
|
|
&& self.heaps.is_empty()
|
|
&& self.kept_leases.is_empty()
|
|
&& self.attachment_shadows.is_empty()
|
|
&& self.first_copies.is_empty()
|
|
&& self.twin_writes.is_empty()
|
|
&& self.fails_command_buffer.is_none()
|
|
&& self.faults.is_none()
|
|
&& self.resident_unheld.is_empty()
|
|
&& !self.taken.dead()
|
|
&& self.taken.rate_map_id().is_none()
|
|
}
|
|
|
|
/// A segment of what it recorded, then `raws` (a kept chunk's: `indirect_execute/kept.rs`),
|
|
/// with what it noted, held, executed, kept and took. Refused (internal) where it did more
|
|
/// than a compute chunk's kept recording does again ([`EncoderCore::run_kept`]): it reached
|
|
/// a heap, a shadowed texture's work or a rate map, or failed.
|
|
pub(super) fn into_segment(
|
|
self,
|
|
raws: Vec<vk::CommandBuffer>,
|
|
) -> Result<RecordedSegment, Error> {
|
|
let more = !self.heaps.is_empty()
|
|
|| !self.kept_leases.is_empty()
|
|
|| !self.attachment_shadows.is_empty()
|
|
|| !self.first_copies.is_empty()
|
|
|| !self.twin_writes.is_empty()
|
|
|| self.fails_command_buffer.is_some()
|
|
|| self.faults.is_some()
|
|
|| self.taken.rate_map_id().is_some()
|
|
|| self.resident.is_some();
|
|
if more {
|
|
return Err(internal(
|
|
"a kept chunk's run that did more than its recording",
|
|
));
|
|
}
|
|
let mut recorded = self.raws;
|
|
recorded.extend(raws);
|
|
Ok(RecordedSegment {
|
|
raws: recorded,
|
|
summary: self.summary,
|
|
retained: self.retained,
|
|
executions: self.executions,
|
|
keys: self.keys,
|
|
taken: self.taken,
|
|
resident_misses: 0,
|
|
faults: ReplayFaults::default(),
|
|
takes: Takes::default(),
|
|
})
|
|
}
|
|
}
|
|
|
|
/// A run of shadowed textures' copies in an encoder whose commands Metal leaves unordered
|
|
/// (`EncoderCore::shadow_copies`, `held_shadow_copies`): one barrier from everything before its
|
|
/// first copy and one to everything after its last order the run against the encoder's other
|
|
/// commands, as a barrier around each copy did, a global barrier's first scope being every
|
|
/// command before it and its second every command after. Inside the run a copy goes after a
|
|
/// barrier only where it needs one:
|
|
///
|
|
/// - **Writes of the truth.** A copy back or a merge writes the texture's truth: a buffer's bytes
|
|
/// (another key's too, where buffers over the same memory are not one key,
|
|
/// `host_aliases.rs`) or a texture's image. It takes a barrier before it and one after it, as
|
|
/// every copy did.
|
|
/// - **The others** (copies in and keeps) write only memory the runtime made for the texture
|
|
/// alone: its shadow, a merge's scratch or kept image, each of a key of its own. One goes after
|
|
/// a barrier where a use of it and a use of a copy since the last barrier name the same key
|
|
/// (aliases added) over ranges that share memory, and one of the two writes it: the tracker's
|
|
/// rule for two commands that must run in order. Copies in of different textures (reads of
|
|
/// their truths, writes of their own shadows) run together; a keep after the copy in of its
|
|
/// texture (the shadow written, then read) takes its barrier.
|
|
///
|
|
/// Every image rests in `GENERAL`, so no barrier the run leaves out carried a layout transition.
|
|
#[derive(Default)]
|
|
struct ShadowRun {
|
|
/// The uses of the copies since the run's last barrier (aliases added); `None` before its
|
|
/// first.
|
|
since: Option<Vec<Use<ResourceKey>>>,
|
|
/// Whether the last copy wrote its truth: the next copy takes a barrier.
|
|
fenced: bool,
|
|
/// Whether a copy was recorded in the run.
|
|
recorded: bool,
|
|
}
|
|
|
|
impl ShadowRun {
|
|
/// A run whose first copy follows a barrier from everything before, which the caller records.
|
|
fn after_barrier() -> ShadowRun {
|
|
ShadowRun {
|
|
since: Some(Vec::new()),
|
|
fenced: false,
|
|
recorded: false,
|
|
}
|
|
}
|
|
|
|
/// Whether the next copy, of `uses` (aliases added), writing its truth where `writes_truth`,
|
|
/// goes after a barrier (the type's rules); noted as recorded.
|
|
fn before(&mut self, uses: &[Use<ResourceKey>], writes_truth: bool) -> bool {
|
|
let barrier = match &self.since {
|
|
None => true,
|
|
Some(since) => self.fenced || writes_truth || meets(since, uses),
|
|
};
|
|
let since = self.since.get_or_insert_with(Vec::new);
|
|
if barrier {
|
|
since.clear();
|
|
}
|
|
since.extend(uses.iter().cloned());
|
|
self.fenced = writes_truth;
|
|
self.recorded = true;
|
|
barrier
|
|
}
|
|
|
|
/// Whether a copy was recorded in the run, which then takes a barrier to everything after.
|
|
fn recorded(&self) -> bool {
|
|
self.recorded
|
|
}
|
|
}
|
|
|
|
/// Whether a use of `later` and one of `earlier` name the same key over ranges that share memory,
|
|
/// and one of the two writes it.
|
|
fn meets(earlier: &[Use<ResourceKey>], later: &[Use<ResourceKey>]) -> bool {
|
|
later.iter().any(|used| {
|
|
earlier.iter().any(|before| {
|
|
before.key == used.key
|
|
&& before.range.overlaps(&used.range)
|
|
&& !(before.write.is_empty() && used.write.is_empty())
|
|
})
|
|
})
|
|
}
|
|
|
|
/// The place in `copies` of the keep that copies the staging buffer the copy in at `copies[i]`
|
|
/// fills (`ShadowCopy::keeps_from_staging`): its texture's next copy, the copies between them
|
|
/// other textures' shadows', which leave its shadow and staging buffer as the copy in left them.
|
|
/// `None` where the keep moves its texels out of the image, or no keep follows.
|
|
fn staged_keep(copies: &[ShadowCopy], i: usize) -> Option<usize> {
|
|
let copy = &copies[i];
|
|
if !matches!(copy.step, Step::Into) {
|
|
return None;
|
|
}
|
|
let shadow = copy.copies.shadow_key;
|
|
let (j, keep) = (copies.iter().enumerate().skip(i + 1))
|
|
.find(|(_, later)| later.copies.shadow_key == shadow)?;
|
|
(Arc::ptr_eq(&keep.copies, ©.copies) && keep.keeps_from_staging()).then_some(j)
|
|
}
|
|
|
|
/// The dispatch that takes the copy in at `copies[i]` and its texture's keep after it together
|
|
/// (`ShadowMerge::keep_in`, `shadow_copy_in.rs`), where `device` takes one: the keep's place in
|
|
/// `copies`, the dispatch and its uses. The copies between them are of other textures' shadows,
|
|
/// so the keep's texels are the copy in's wherever it is recorded. `None` where the two stay
|
|
/// apart.
|
|
fn keep_with_copy_in(
|
|
device: &Device,
|
|
copies: &[ShadowCopy],
|
|
i: usize,
|
|
) -> Option<(usize, CopyIn, Vec<Use<ResourceKey>>)> {
|
|
let copy = &copies[i];
|
|
let region = copy.copied_region()?;
|
|
let shadow = copy.copies.shadow_key;
|
|
let (j, merge) = copies
|
|
.iter()
|
|
.enumerate()
|
|
.skip(i + 1)
|
|
.find(|(_, later)| later.copies.shadow_key == shadow)
|
|
.and_then(|(j, later)| match &later.step {
|
|
Step::Keep(merge) => Some((j, merge)),
|
|
_ => None,
|
|
})?;
|
|
let both = merge.keep_in(device, ©.copies, ®ion)?;
|
|
Some((j, both, merge.keep_in_uses(©.copies)))
|
|
}
|
|
|
|
/// Count shadowed textures' `copies` as planned into `counts` ([`EncoderCore`]'s
|
|
/// `count_shadow_copies`): also a run's merge as its batch's submission records it
|
|
/// (`pass_runs.rs`).
|
|
pub(super) fn count_shadow_copies(counts: &Counters, copies: &[ShadowCopy]) {
|
|
let texels_in = copies.iter().map(ShadowCopy::texels_in).sum();
|
|
let texels_back = copies.iter().map(ShadowCopy::texels_back).sum();
|
|
// A clear in copies nothing (`ShadowUse::cleared`).
|
|
let copied = copies.iter().filter(|copy| copy.clear.is_none()).count();
|
|
counts.shadow_copies_planned(copied, texels_in, texels_back);
|
|
for copy in copies {
|
|
counts.shadow_copy_in_planned(copy);
|
|
}
|
|
let bounded = (copies.iter())
|
|
.filter(|copy| copy.cause == Some(CopyCause::Bounded))
|
|
.count();
|
|
if bounded > 0 {
|
|
counts.bounded_shadow_copies_planned(bounded);
|
|
}
|
|
let dispatched = copies
|
|
.iter()
|
|
.filter(|copy| match ©.step {
|
|
Step::Keep(merge) => merge.dispatches(),
|
|
Step::Merge(merge) => merge.changed_dispatches(),
|
|
Step::Into | Step::Back => false,
|
|
})
|
|
.count();
|
|
if dispatched > 0 {
|
|
counts.shadow_merge_copies_dispatched(dispatched);
|
|
}
|
|
let kept: u64 = copies
|
|
.iter()
|
|
.filter_map(|copy| match ©.step {
|
|
Step::Keep(merge) => Some(merge.texels_kept()),
|
|
_ => None,
|
|
})
|
|
.sum();
|
|
if kept > 0 {
|
|
counts.shadow_texels_kept(kept);
|
|
}
|
|
}
|