Files
metal2vulkan/crates/metal2vulkan-runtime/src/shadow.rs
T
Anees Iqbal aa2256b6e7 Order host-alias uses across queues again: Metal orders textures over one IOSurface across queues
Reverts a5634d48db and 3926d2d8c9, which left the uses the host aliases add out of the
scheduler's order across queues on the strength of host_aliases.rs's line that Metal orders two
textures over one IOSurface across queues only as the program orders them. The conformance probe
probes/cross_queue_surface_order.m measured that case (queue A's long compute write committed
before queue B's blit read of the same bytes, no event between them, three runs each):

- two tracked textures over one IOSurface: B ran after A and read its writes in every run on an
  M3 Max (macOS 27.0.1, m3-max-21812, m3-max-21830), a Radeon Pro 5300M and a UHD 630 (macOS
  26.7.1, m3-max-21850, m3-max-21851); untracked, the same but one run on the 5300M;
- a texture of another surface (control): ran alongside A on the M3 and the 5300M;
- two no-copy buffers over one allocation: B ran alongside A, reading stale or mixed words, on
  all three (8 of 9 runs; the 630 ran it one after the other twice);
- one tracked buffer (control): ordered on the M3; the 5300M and the 630 each took B first once.

So the alias keys' order across queues is the surfaces' order, which Metal keeps; dropping it was
wrong for the consumer's surfaces, whose textures lie over per-texture no-copy buffers.
host_aliases.rs now states those measurements, the plain no-copy buffers' among them, in place of
the M4 line. The freeze the revert reopens (a held batch's making holding other queues' heads
over a shared surface) is answered by the next commit, which lets a batch held for its making give
way across queues, as the x86 Macs' GPUs may take a later commit first.

Until that commit, tests::hold_sites::a_head_over_the_same_host_bytes_through_another_buffer_goes_while_a_batch_is_held
fails as before a5634d48db (m3-max-21752).
2026-11-12 10:29:05 -08:00

4250 lines
203 KiB
Rust

//! Shadows: a 2D texture or array over a buffer that no linear image of the device aliases
//! (`linear_plan::aliases`) is an optimal image whose texels the encoders copy from the buffer and
//! back, so that every access Metal orders sees what the other view wrote. A texture buffer whose
//! offset in its buffer's `VkBuffer` misses the device's `minTexelBufferOffsetAlignment`, where
//! Vulkan views no texels, is likewise a staging buffer its bindings view, the texels at its start.
//! The rules below are the same for both shadows, an image or a staging buffer.
//!
//! - **The buffer is the truth.** CPU access through the texture reads and writes the buffer's
//! bytes (`buffer_texture.rs`), and between encoders the image holds nothing the buffer lacks.
//! - **Copy in** before the first command of a command buffer's encoders that uses the texture,
//! and again before a later one when a command wrote the bytes since: through the buffer, a
//! texture buffer, or another shadowed texture over them. A command buffer's encoders run after
//! its commit, so the first copy sees every CPU write made before then. Metal shows an encoder
//! the bytes as of its command buffer's commit: a CPU write of a `Shared` buffer's bytes made
//! while the command buffer ran reached neither a later dispatch of the running encoder nor a
//! later encoder of the command buffer, while the same write made before the commit reached
//! both, a command buffer committed before the write read the written bytes, and so did the
//! encoder after an event wait that another queue's write signalled. A write made once the
//! command buffer's own signal of an event was seen, while it ran, reached its reads after the
//! signal: in the encoder after it, a later dispatch of that encoder too, and in a later encoder
//! (an M3 Max under macOS 27.0.1, each answer every time;
//! `metal2vulkan-conformance/probes/cpu_write_after_a_signal.m`). So an encoder takes the image
//! its batch's earlier encoder left holding the truth and copies nothing in ([`ShadowCarry`]); an
//! encoder that starts another batch, after a signal, a wait or a host step, copies in again
//! before its first command that uses the texture, which runs after the commands before it; and
//! a later command buffer copies in the bytes Metal would read there, but where only the
//! runtime's commands write them (a tracked `Private` buffer's, `shadow_standing.rs`): there
//! its first copy in moves nothing where the image an earlier command buffer's end left
//! holding them still holds them as its batch is submitted. A copy in
//! of a one-level, one-slice texture's image moves only the texels inside the rectangle its
//! command reads and writes them in, where the command bounds that ([`ShadowUse::loads`]: a
//! pass's attachment it loads and takes no other way, inside its draws' scissors). The image's
//! other texels keep whatever it held, which no copy back moves and no command of the encoder
//! reads: a later command that needs them current takes a copy back of what was written and a
//! copy in of every texel first, in this encoder or a later one of the command buffer.
//! - **Copy in what changed.** An image whose bytes changed since its copy in only inside a
//! rectangle of its texels, by writes [`Within`] it (a render pass instance's to a color
//! attachment it loads, inside its draws' scissors), or by a copy back of a part of another
//! shadow laid over the bytes texel for texel as it is ([`ShadowCopies::same_texels`]), takes
//! a copy in of the texels inside the rectangle bounding those alone
//! (`WorkCounts::bounded_shadow_copies`): every other texel holds the bytes still. A command
//! that reads only inside a rectangle and writes none takes those of them inside it, and the
//! rest stay to copy in before the next command that reads them. Any other write of the bytes
//! leaves it to copy in as above; it holds no writes of its own, nor a keep.
//! - **Copy back** what a command wrote through the texture before the next command that touches
//! the bytes some other way, and at the end of the encoder (orchestrator, 2026-10-04: at the
//! end of the encoder that writes through the texture, not of the command buffer). A copy moves
//! only the texels' rows, never the bytes between them; a copy back of a one-level, one-slice
//! texture's image, only the texels inside the rectangle its writers since the last bound their
//! writes to, where each bounds them ([`ShadowUse::area`]: a pass's attachment it loads, inside
//! its draws' scissors), as the image's other texels are what the last copy in left. Where the
//! device runs it faster so, a copy back of such a texture's image is a compute dispatch that
//! stores the same bytes (`shadow_copy_back.rs`).
//! - **Merged back** ([`ShadowCopies::merges_back`]: a buffer's bytes another writer may write
//! while a command buffer runs, where a merge serves the shadow). A copy back would write every
//! texel it moves, those no command wrote too, with what the copy in read, over another writer's
//! writes to them while the command buffer ran, which Metal keeps: it writes only the texels a
//! command writes, and another writer writing bytes no command touches while a command buffer
//! runs is no race under its contract. The CPU writes a `Shared` or `Managed` buffer's bytes and
//! host pages so (an M3 Max under macOS 27.0.1 kept such a write, made inside a kernel's run,
//! every time, a `Managed` buffer's too), and another queue's commands an untracked buffer's,
//! which the program orders (the M3 Max ran another queue's fill of an untracked `Private`
//! buffer's other texels inside a kernel's or a render pass's run and kept it). A tracked
//! `Private` buffer's bytes have no such writer: the CPU writes none, and the tracker orders
//! another queue's commands that touch them before or after the encoder's command buffer, as
//! Metal does (the M3 Max ran another queue's fill of a tracked buffer after the kernel or the
//! pass ended, every time), so its texture's shadow is copied back. A merged-back image is kept,
//! after its copy in, before the first command since the last copy back that writes through it,
//! and each copy back of it is a merge (`shadow_merge.rs`): only the words the encoder's
//! commands changed through the texture since the keep reach the bytes. The keep holds the
//! rectangle the commands write in where they bound it ([`ShadowUse::area`]); a command that
//! writes outside it takes the merge first, and a keep of the rectangle bounding both. The image
//! still holds the bytes as the encoder's commands left them. A command that replaces every
//! texel ([`ShadowUse::overwrites`]) keeps nothing, as its copy back writes only texels it
//! wrote; that copy back goes before the keep of a later command that writes through the
//! texture, since the merge after the keep takes only the words changed since it. The keeps'
//! scratch is the encoder's, one per texture and rectangle. A run of render passes over the
//! texture, one after another in one batch with nothing between them that may touch its bytes
//! (`pass_runs.rs`), is kept before its first pass and merged after its last: each later pass
//! goes on over the image the one before left ([`ShadowSync::go_on`]), with no keep, and its
//! merge compares the first keep; a pass of the run that writes outside that keep merges it and
//! keeps every texel next, which every later pass of the run writes inside, and one that needs
//! texels beyond those the image holds (its copy in moved only a pass's scissors' texels) merges
//! what was kept and copies every texel in.
//! - **Overwrites over logged pages** ([`ShadowCopies::logged`]: host pages whose consumer logs
//! every write to them, `dirty_pages.rs`). A command that replaces every texel of such a shadow
//! takes the merge instead of the plain copy back: its copy in (which moves only the rows the
//! log reports changed since the image's last copy, and every row where the log cannot say),
//! the keep, the command, and the merge, which stores only the words the command changed
//! against the keep. The bytes are Metal's: the copy in is a gated one, decided once the log
//! published the stamp the command buffer asked at its commit (`DirtyPages::wait`), so the image
//! and then the keep hold the host bytes as of the commit, every write before it (the CPU's
//! among them) included, and a word the merge leaves already holds the value the command wrote
//! there. A byte another writer writes after the commit is one the command also writes, a race
//! Metal leaves unordered. Over host pages with no log the image may lag the CPU's writes, and a
//! word the command writes with the image's stale value would keep the CPU's: there the copy
//! back stays a plain copy of every texel the command wrote.
//! Where the merge is cut to the texels' rows (its words are texels' bytes alone, none of the
//! bytes between rows or past the last texel), it stores whole 256-byte blocks
//! (`bind::ShadowMergeStores::Blocks`, `WorkCounts::shadow_merge_dispatches_in_blocks`): each
//! block holding a changed word as one store without gaps, a block with none not at all. That
//! too is Metal's: the command wrote every byte of every such block, as the plain copy back
//! wrote them all, so a word it left as the keep held it is stored as the merge reads it, the
//! same value but for another writer's write since the commit, which races the command's own.
//! The scratch of such a merge is kept again only for another command whose merge stores whole
//! blocks; one whose merge does not takes a merge of the words alone, after the blocks' merge.
//! - **Stored attachments** ([`ShadowUse::tile_stores`]). A render pass that loads a color
//! attachment and stores it, and changes it only inside its draws' scissors
//! (`render/shadows.rs`: the rectangle [`ShadowUse::area`], inside the render area), merges
//! back in whole 256-byte blocks of the rectangle's rows too, over any merged-back bytes, logged
//! or not: each block holding a changed word as one store, a block with none not at all, and no
//! word outside the rectangle's rows (each widened to whole words). Metal's own store of such a
//! pass writes back every texel of each tile its draws reach, those no fragment changed among
//! them: on an M3 Max under macOS 27.0.1, a pass loading and storing a linear `RGBA8Unorm`
//! texture over a no-copy buffer and drawing one pixel rewrote the other 1023 texels of the
//! 32x32 tile holding the pixel with the values it loaded, so a CPU thread writing every texel
//! over and over through the passes lost its writes to each of them in 157 to 229 of 300 passes,
//! and to no texel of any other tile, inside the render area or past it; a kernel writing the
//! same pixel lost none of them. So a CPU write to a texel such a pass leaves inside its render
//! area, made while the pass runs, is no write Metal keeps, and a block stored with the value
//! the merge reads there is as Metal's answer. A block can reach past the tiles the draws reach
//! (a 256-byte block of a row holds 64 such texels, two of those tiles), never past the
//! rectangle; and the merge undoes only a write made between its read of the block and its
//! store, a shorter window than a tile's from its load to its store. A run of such passes
//! (`pass_runs.rs`) merges once over its keep, which holds every pass's rectangle (the first
//! pass's, or every texel once a pass wrote outside it): a block holding a word one pass changed
//! may reach past that pass's rectangle, by less than a block, never past the keep's, under the
//! same window. A use joined with another that writes through the texture otherwise (a draw's
//! write to it as a bound texture) stores the words alone.
//! - **Ordering.** A copy is a command of the encoder with uses of both the buffer's bytes and the
//! image, so the tracker orders it against the encoder's other commands and other encoders'
//! like any use: the texture and its buffer are one resource to it. In an encoder whose
//! commands Metal does not order (concurrent dispatches), each copy has a barrier before and
//! after it.
//! - **Staging.** Vulkan copies texels at multiples of their size of a `VkBuffer`, which a
//! no-copy buffer's bytes need not start at (`Buffer::base`). Such a 2D texture's copies go
//! through a staging buffer of its own ([`ShadowStaging`]): its bytes into the staging buffer and
//! on into the image, or the image into the staging buffer and its rows on into the buffer, as
//! one command with a barrier between the steps. That staging buffer needs no uses of its own:
//! every copy through it writes the image or the texture's bytes, so the tracker orders it after
//! every other copy of the texture. A texture buffer's staging buffer is its shadow, with uses of
//! its own, as an image's.
//! - **Rows that share bytes** (`linear_plan.rs`'s **Rows that overlap**). An IOSurface plane
//! whose GPU rows share 1 to 15 bytes is an image of its texels whose copies are two regions a
//! slice each way ([`plane_copy_regions`](super::buffer_texture::plane_copy_regions)): going
//! in each shared byte is read for both the rows that hold it, and going back it is
//! written once, from the earlier row, one of the rows Metal's own writes leave it from (its
//! order follows the command, the GPU pitch and the row count). A heap's one-plane YCbCr
//! texture whose rows share bytes (`texture/heap.rs`'s `heap_planes`) takes the same copies.
//! The rules above are the same;
//! no copy of such a shadow is cut to a rectangle, and none dispatches, both of which take one
//! region.
//! - **Render passes** (`render/shadows.rs`). A render pass instance is one command: its draws
//! record apart, where no copy goes, so the copies go before the instance and the uses are its
//! draws' and its attachments'.
//! - **Heap depth and stencil textures** (`texture/heap.rs`). On a driver that lays depth and
//! stencil images apart from their memory, a heap's texture of such a format is an image of its
//! own memory whose truth is the heap's bytes in the texture's range: each aspect's texels
//! tightly packed, level by level, in planes (`heap_planes`). Its copies are one region per
//! aspect and level, the region's aspect its own. The rules above are the same, and a blit
//! encoder follows them too (`blit.rs`): such a texture is an image to its blits, where a texture
//! over a buffer is its bytes.
//! - **Views of another component layout** (`texture/view_shadow.rs`). On a device whose Vulkan
//! views reinterpret no image's components, such a view of an image is an image of the view's
//! format whose truth is the viewed image's texels in the levels and layers it presents
//! ([`ShadowSource::Image`]). Its copies are image to image (`vkCmdCopyImage` between formats of
//! one copy unit, which keeps every bit), or through a buffer of its own where Vulkan copies no
//! texels between the two formats (an alpha format and another, `A8_UNORM` and `R8_UNORM`:
//! `image_plan::copies_between`), and the rules above are the same with the image in the
//! buffer's place, except that a command writing the image two ways (below) is refused: no merge
//! serves an image. A shadow of one level and slice copied image to image (a 2D texture's own
//! image's, `texture/usage_shadow.rs`, among them) has its copies in cut to rectangles as a
//! one-level, one-slice texture's over a buffer has, its texels the image's at their own
//! coordinates ([`ShadowCopies::cuts`]); its copies back move every texel, so a command that
//! writes through it takes every texel in first.
//! - **Rotated textures** (`rotated.rs`, `texture/rotation.rs`). A rotated texture renders through
//! an image of its pass's frame whose truth is the buffer's bytes in the frame Metal stores the
//! texture in, turned: its copies go through a staging buffer of the bytes as laid out and a
//! tight one of the image's texels, the rotation kernel moving them between the two
//! ([`ShadowTarget::Rotated`]). The rules above are the same, but for a command writing the bytes
//! two ways, which is refused: no merge serves a turned image. Its functions read and write an
//! image of the stored frame, unturned, whose writes Metal drops
//! ([`ShadowCopies::drops_writes`]): copied in as any shadow, never copied back, and copied in
//! again after a command that wrote through it.
//! - **Two ways** (`shadow_merge.rs`): one command that writes through a shadowed texture and also
//! writes its bytes another way (through the buffer, a texture buffer, or another shadowed
//! texture over them). A copy back would replace the other write's bytes with the shadow's, where
//! Metal keeps both writes when they touch different bytes, so the shadow is kept before the
//! command and merged after it: only the bytes the command changed through the texture reach the
//! buffer. The merge goes before the encoder's next command, whatever it touches, and leaves the
//! shadow to copy in again at its next use.
//! - **Converted texels** (`ycbcr.rs`): a two-plane YCbCr texture over a surface's two planes is an
//! image of the texels Metal reads from them ([`ShadowSource::Converted`]). Its copy in is a
//! conversion: the planes' bytes into a staging buffer of its own and a dispatch that writes
//! every texel, which runs no workgroup where the planes' words are those the last conversion
//! read (`ycbcr.rs`, **When it converts**). The rules above are the same with the conversion in
//! the copy's place; nothing
//! writes the image (Metal drops a function's writes, and a pass into it is refused), so nothing
//! is copied back.
//! - **YCbCr writes** (`ycbcr.rs`): a 4:4:4 two-plane YCbCr texture whose writes Metal stores into
//! its planes takes a second shadow at a storage binding, an image of the written texels and a
//! mark of each ([`ShadowSource::YCbCrWrite`]). Its copy in clears it, so no texel is marked; its
//! copy back stores the marked texels' codes into the planes, through a staging buffer of their
//! bytes and a dispatch of the encode kernel, and leaves every other byte as it was. The rules
//! above are the same: a copy back leaves the texture's converted texels stale, so they convert
//! again at their next use, and a command writing the planes two ways is refused, as no merge
//! serves the planes. A pass's color attachment of such a texture renders into its render shadow,
//! a texture whose shadow is one of these over floats: its copy in converts the planes into every
//! texel, as the texture reads them (the pass's load), and its copy back stores every texel's
//! codes, the marks not read, of the tiles of 32 texels a side that meet its area where it has
//! one ([`ShadowCopy::area`]: the pass loads the attachment and its draws' scissors bound what
//! it changes).
//! - **Two planes in a heap** (`texture/heap_two_planes.rs`): a heap's two-plane extended-range
//! texture is an image of its texels whose truth is its first level's planes in the heap's
//! bytes, as Metal lays them ([`ShadowTarget::Planes`]): its copies go through a staging buffer
//! of the bytes as laid out and a tight one of the image's texels, the two-plane kernel moving
//! them between the two. The rules above are the same, but for a command writing the bytes two
//! ways, which is refused: no merge serves the planes.
//! - **Tiles in a heap** (`texture/heap_tiled.rs`): a texture of a plain format in a heap over host
//! pages is an image of its elements whose truth is the heap's bytes, the elements tiled there as
//! Metal lays them ([`ShadowTarget::Tiled`]): its copies go through a staging buffer of the bytes
//! as laid out and a tight one of the image's elements, the tiled texel kernel moving them
//! between the two. The rules above are the same; a command writing the bytes two ways merges as
//! a buffer's texture does, the shadow's elements copied out tiled, each other byte as the last
//! copy in left it.
//! - **Copies taken at use** ([`ShadowCopies::levels`]): a tiled texture of more than one level
//! over a buffer's bytes (`Buffer::new_tiled_texture`) is one image of every level, a copy of the
//! bytes that the GPU takes once, at the texture's first use, and again at the first use after
//! each `Texture::did_modify_data`, as an x86 Mac's does (an AMD Radeon Pro 5300M and an Intel
//! UHD Graphics 630 under macOS 26.7.1 agreed on every form, storage mode and format measured: a
//! CPU write of the bytes made after the making, or after the call, and before the first use is
//! in every level that use reads; one made after it reaches none; and a blit into the first
//! level reaches neither the bytes nor the later levels. A GPU write of the bytes is not
//! measured: here it reaches no level either, as a CPU write). The
//! first command of an encoder that uses any of its levels after a making or a call (or a
//! `replace_region` of its first level, which writes the bytes) takes the copy ([`LevelsCopy`],
//! at that command's encoding): before the command, a copy in of every texel of the first
//! level, then each later level made from the first as `generate_mipmaps` makes it for the
//! format ([`LaterLevels`], `mipmap_plan.rs`): the device's blit chain, or the compute filter's
//! or the integer picks' run over a scratch of their own, made as the encoder takes the copy
//! ([`LaterRun`]), all of it transfers of the image and dispatches over the scratch alone,
//! between barriers. It runs on the GPU in its command buffer's order, so it reads the bytes as
//! they are when the command buffer runs, after its commit. Every other use copies nothing in,
//! whatever wrote the bytes, and nothing written through the image is copied back or merged: it
//! stays in the image, as a Mac's texture's own memory keeps it. A use whose encoding is left
//! out of its command buffer's work ([`ShadowSync::forget`]) leaves the copy to the next use. An
//! encoder a rebind encodes again (`command_buffer/reencode.rs`), whose first recording is never
//! submitted, takes the copy where its first encoding took it and leaves the mark as it finds it
//! ([`Takes`]). A take whose encoder runs nothing, its command buffer failed before it ran, is
//! given back: as its part is failed (committed as failed, an encoder a failure names, a
//! submission that runs nothing, `State::fail_part`), or as its batch's submission fails, a host
//! step's recording or the `vkQueueSubmit` among it (`State::give_back_from`). The next use
//! takes the copy, where that take is still the texture's latest and nothing set the mark since
//! ([`Takes::give_back`]). An encoder of
//! several parts (split at a host step) gives back every take of its own where any of its parts
//! runs nothing, so one that ran in an earlier part is taken again at the next use
//! (docs/FAST-PATHS.md, known gaps). A command buffer dropped without a commit gives back every
//! take of its ended encoders (`CommandBufferShared`'s drop). Work its commit recorded and its
//! host step records again (`CommandBufferShared::host_step`), the commit's recording never
//! submitted, takes the copy where the commit's recording took it, keeping that take, and
//! decides a use that recording did not meet by the mark; a take of that recording the step's
//! does not meet is given back ([`ShadowSync::over`], [`Takes::give_back_unheld`]).
//! For a format `generate_mipmaps` leaves as it is or refuses, the copy in moves the first level
//! alone and the later levels keep what they hold (docs/FAST-PATHS.md, known gaps).
use super::blit_texture::announce_mipmaps;
use super::buffer::{Buffer, CallerMemory, WeakBuffer};
use super::dirty_pages::{CopyInGate, ShadowLog};
use super::error::{Error, RuntimeGap};
use super::image_plan::{ImageFacts, ImagePlan};
use super::mipmap::record_blit;
use super::mipmap_filter::{filter_chain, FilterRun};
use super::mipmap_pick::{picks_fit, PickRun};
use super::mipmap_plan::{plan_mipmaps, MipBlit, MipmapPlan, PickPlan};
use super::residency::Residency;
use super::resource::ResourceKey;
use super::rotated::{RotatedCopies, Turn};
use super::row_copies::RowCopies;
use super::shadow_copy_back::{CopyBack, WordView};
use super::shadow_merge::{ScratchSpares, ShadowMerge, UnormView};
use super::shadow_standing::{Missed, StandingAcross};
use super::texture::copy_images::UseMaker;
use super::texture::heap_tiled::{HeapTiledCopies, SampleMoves};
use super::texture::heap_two_planes::{HeapPlaneCopies, TwoPlaneHeapLayout};
use super::texture::Texture;
use super::tracker::{Access, Barrier, Use, UseRange};
use super::values::StorageMode;
use super::view_plan::{reinterpretation_refused, ViewRecord};
use super::ycbcr::{YCbCrConversion, YCbCrWriteBack};
use super::Device;
use crate::device::functions::Vk;
use ash::vk;
use metal2vulkan::error::{Feature, Unsupported};
use metal2vulkan::metal::PixelFormat;
use std::ops::Range;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Weak};
/// What a shadowed texture's copies need: its truth, its shadow, and the copies between them.
pub(super) struct ShadowCopies {
/// What the shadow holds a copy of, and the copies.
pub(super) source: ShadowSource,
/// The shadow's identity: its image's, or its staging buffer's.
pub(super) shadow_key: ResourceKey,
/// Everything the shadow holds.
pub(super) shadow_range: UseRange,
/// The texture's hazard tracking, its truth's.
pub(super) tracked: bool,
/// Whether what a command writes through the shadow never reaches the truth: a rotated
/// texture's image its functions read and write, whose writes Metal drops. Such a shadow is
/// never copied back, and a command writing through it leaves it to copy in again at its next
/// use.
pub(super) drops_writes: bool,
/// What holds the truth, as the CPU may write it.
pub(super) backing: Backing,
/// The scratch buffers its merges are done with, kept for its next ones (`shadow_merge.rs`).
pub(super) merge_scratch: ScratchSpares,
/// The identity of the command buffer whose encoder last ended with the image holding the
/// truth ([`ShadowCarry::id`]), 0 for none ever: by which a first copy in that no carry could
/// serve is counted as one an earlier command buffer's standing stands behind, or as one of a
/// shadow that never held a standing ([`CarryTally`]).
pub(super) stood: AtomicU64,
/// Where only the runtime's commands write the truth, the image's standing across command
/// buffers (`shadow_standing.rs`).
pub(super) standing: StandingAcross,
/// Where the shadow is a copy taken at use (a tiled texture's of more than one level,
/// `Buffer::new_tiled_texture`): when it is taken and how its later levels are made. `None`
/// for every other shadow.
pub(super) levels: Option<LevelsCopy>,
}
/// A tiled texture's image of every level over a buffer's bytes (`Buffer::new_tiled_texture`), a
/// copy of them taken at use, as an x86 Mac's GPU takes it (the module's **Copies taken at
/// use**): the first command of an encoder that uses any of its levels after the texture was made,
/// or after `Texture::did_modify_data` (or a `replace_region` of its first level), copies the
/// bytes into its first level and makes every later level from the first as `generate_mipmaps`
/// makes them ([`LaterLevels`]), before that command; no other command copies anything in, and
/// nothing it writes is copied back.
pub(super) struct LevelsCopy {
/// Its mark and its takes: the lowest bit set where the next use takes the copy (as the
/// texture is made, by `Texture::did_modify_data` and by a `replace_region` of the first
/// level), cleared by the encoder whose command uses the texture first after, which takes it;
/// the bits above count the takes, so a take given back is known for the latest
/// ([`give_back`](Self::give_back)).
mark: AtomicU64,
/// How each take makes the later levels from the first after the copy.
later: LaterLevels,
/// The image's aspects.
aspects: vk::ImageAspectFlags,
}
/// The bit of [`LevelsCopy::mark`] set where the next use takes the copy.
const STALE: u64 = 1;
/// How a copy taken at use makes its later levels from its first, right after its copy into the
/// first: as `generate_mipmaps` makes them for the texture's format (`mipmap_plan.rs`).
pub(super) enum LaterLevels {
/// The device's blit chain, each level the linear blit of the one above (`mipmap.rs`).
Blits(Vec<MipBlit>),
/// The runtime's compute filter (`mipmap_filter.rs`) of the levels the record presents, its
/// first level copied into the scratch.
Filter(ViewRecord),
/// The texel picks of an integer format (`mipmap_pick.rs`) of the levels the record presents.
Picks(ViewRecord, PickPlan),
/// None: the later levels keep what they hold. A format `generate_mipmaps` leaves as it is (a
/// depth or stencil one) or refuses on the device, and a filter or picks whose scratch passes
/// the device's limits (docs/FAST-PATHS.md, known gaps).
Kept,
}
impl LaterLevels {
/// How a tiled texture presenting `record`, whose image is `image`, makes its later levels on
/// `device` with `facts`: `generate_mipmaps`' plan of it (`plan_mipmaps`), where its scratch
/// fits the device's limits, announced as that call announces it (`device-filtered-mipmaps`
/// for the blit chain and the compute filter; nothing for the picks, plain Metal's own
/// answer).
pub(super) fn of(
device: &Device,
record: &ViewRecord,
image: &ImagePlan,
facts: &ImageFacts<'_>,
) -> LaterLevels {
let defects = &device.properties().driver_defects;
let later = match plan_mipmaps(record, image, facts, defects) {
Ok(MipmapPlan::Blits(blits)) if !blits.is_empty() => LaterLevels::Blits(blits),
Ok(MipmapPlan::Filter) if filter_chain(device, record, None).is_some() => {
LaterLevels::Filter(*record)
}
Ok(MipmapPlan::Picks(picks)) if picks_fit(device, &picks) => {
LaterLevels::Picks(*record, picks)
}
_ => LaterLevels::Kept,
};
match &later {
LaterLevels::Blits(_) => announce_mipmaps(device, record, "the device's blit chain"),
LaterLevels::Filter(_) => {
announce_mipmaps(device, record, "the runtime's compute filter");
}
LaterLevels::Picks(..) | LaterLevels::Kept => {}
}
later
}
}
/// What one take of a copy taken at use records past its copy in where the compute filter or the
/// picks make its later levels ([`LaterLevels`]): their scratch and commands, made as the encoder
/// takes the copy (`EncoderCore::later_runs`) and held by its command buffer until it completes.
pub(super) enum LaterRun {
Filter(Box<FilterRun>),
Picks(PickRun),
}
impl LaterRun {
/// Record the run over `image`.
///
/// # Safety
///
/// As [`FilterRun::record`]'s and [`PickRun::record`]'s.
unsafe fn record(&self, device: &Vk, command_buffer: vk::CommandBuffer, image: vk::Image) {
// SAFETY: the caller's contract.
unsafe {
match self {
LaterRun::Filter(run) => run.record(device, command_buffer, image),
LaterRun::Picks(run) => run.record(device, command_buffer, image),
}
}
}
}
impl LevelsCopy {
/// A copy to take at the texture's first use, its later levels made as `later` says over
/// `aspects`.
pub(super) fn new(later: LaterLevels, aspects: vk::ImageAspectFlags) -> LevelsCopy {
LevelsCopy {
mark: AtomicU64::new(STALE),
later,
aspects,
}
}
/// Take the copy at the next use again (`Texture::did_modify_data`, and a `replace_region` of
/// the first level, which writes the bytes).
pub(super) fn mark_stale(&self) {
self.mark.fetch_or(STALE, Ordering::AcqRel);
}
/// Whether this use takes the copy: once after each [`mark_stale`](Self::mark_stale) (and
/// the making), which it clears. The take's number where it does, counted from 1.
fn take(&self) -> Option<u64> {
let mut mark = self.mark.load(Ordering::Acquire);
loop {
if mark & STALE == 0 {
return None;
}
let number = (mark >> 1) + 1;
match (self.mark).compare_exchange_weak(
mark,
number << 1,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => return Some(number),
Err(now) => mark = now,
}
}
}
/// Give take `number` back, its recording never run (`State::fail_part`, or left out,
/// [`ShadowSync::forget`]): the mark is set again, so the next use takes the copy, where that
/// take is still the latest and nothing set the mark since. A later take holds the copy its
/// own use takes; a mark set since asks the next use for it already.
pub(super) fn give_back(&self, number: u64) {
let taken = number << 1;
let _ =
(self.mark).compare_exchange(taken, taken | STALE, Ordering::AcqRel, Ordering::Acquire);
}
/// Whether a take records a run made for it ([`run`](Self::run)): the compute filter's or
/// the picks'.
pub(super) fn runs(&self) -> bool {
matches!(self.later, LaterLevels::Filter(_) | LaterLevels::Picks(..))
}
/// What a take records past its copy in where the compute filter or the picks make the later
/// levels, made now on `device`; `None` for the blit chain, or none. Refused where it cannot
/// be made: a compute filter whose scratch the device's memory has no room for
/// (`NotImplemented(MipmapGeneration)`, as `generate_mipmaps` refuses it), and the picks'
/// scratch as a buffer of the runtime's own is.
pub(super) fn run(&self, device: &Device) -> Result<Option<LaterRun>, Error> {
let gap = || Error::Unsupported(Unsupported::not_implemented(Feature::MipmapGeneration));
match &self.later {
LaterLevels::Filter(record) => {
let chain = filter_chain(device, record, None).ok_or_else(gap)?;
let run = FilterRun::new(device, &chain, record, self.aspects, None)?;
Ok(Some(LaterRun::Filter(Box::new(run.ok_or_else(gap)?))))
}
LaterLevels::Picks(record, plan) => {
let run = PickRun::new(device, plan, record, self.aspects)?;
Ok(Some(LaterRun::Picks(run)))
}
LaterLevels::Blits(_) | LaterLevels::Kept => Ok(None),
}
}
/// Record the later levels after the copy into the first, on `image`: the blit chain, or
/// `run` where the take made one ([`run`](Self::run); none where it could not be made, which
/// failed its command buffer).
///
/// # Safety
///
/// `command_buffer` is recording outside a render pass, right after the copy into the first
/// level; the image and `run` are live until it completes, the image in the `GENERAL`
/// layout, with the transfer usages and the features its format's plan needs (`plan_mipmaps`
/// planned it).
unsafe fn record_levels(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
image: vk::Image,
run: Option<&LaterRun>,
) {
if let LaterLevels::Blits(blits) = &self.later {
for blit in blits {
// SAFETY: the caller's contract; each blit reads the level the one before wrote.
unsafe { Barrier::everything().record(device, command_buffer) };
record_blit(device, command_buffer, image, self.aspects, blit);
}
return;
}
if let Some(run) = run {
// SAFETY: the caller's contract; the run reads the first level the copy just wrote.
unsafe {
Barrier::everything().record(device, command_buffer);
run.record(device, command_buffer, image);
}
}
}
}
/// The texels of a texture over an IOSurface's plane from which its shadow's copies in count
/// apart ([`ShadowCopies::large_surface`]): 2^20, which a 1920x1080 framebuffer's exceed.
pub(super) const LARGE_SURFACE_TEXELS: u64 = 1 << 20;
/// What holds a shadow's truth, as the CPU may write it, by which the encoders' first copies in
/// are counted (`WorkCounts::shadow_texels_first_copied_in`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Backing {
/// An image (a texture's own, or the image a view presents): only the runtime's commands and
/// calls write it.
Image,
/// A tracked image of private or memoryless storage the runtime made in memory of its own,
/// not shareable (a texture's, or the one a view presents): only the runtime's commands of
/// this process write it, as the CPU
/// reaches none of a `Private` texture's texels (`Texture::replace_region` refuses them).
PrivateImage,
/// A buffer of private or memoryless storage the runtime allocated: likewise.
PrivateBuffer,
/// A buffer of shared or managed storage the runtime allocated, which the CPU maps.
MappedBuffer,
/// Host pages a no-copy buffer imported, or the memory of the caller's `VkBuffer`.
HostPages,
/// Host pages that the texture's own image lies in too (an aliasing image, a pitched host
/// image among them), a shadow of which reads them as the image left them.
AliasedHostPages,
/// The bytes of an IOSurface's plane that the CPU may write: host pages, or a buffer it maps.
Surface,
/// The bytes of an IOSurface's plane in a buffer of private or memoryless storage the runtime
/// allocated (a consumer's device copy of the surface): only the runtime's commands write
/// them.
PrivateSurface,
}
impl Backing {
/// What holds the bytes of `buffer`, a texture's over an IOSurface's plane where `surface`.
pub(super) fn of(buffer: &Buffer, surface: bool) -> Backing {
let private = matches!(
buffer.resource_options().storage_mode(),
Ok(StorageMode::Private | StorageMode::Memoryless)
);
match (surface, buffer.caller_memory(), private) {
(true, None, true) => Backing::PrivateSurface,
(true, _, _) => Backing::Surface,
(false, Some(_), _) => Backing::HostPages,
(false, None, true) => Backing::PrivateBuffer,
(false, None, false) => Backing::MappedBuffer,
}
}
/// The backing of a shadow of a texture whose own image lies in the bytes too.
pub(super) fn aliased(self) -> Backing {
match self {
Backing::HostPages => Backing::AliasedHostPages,
other => other,
}
}
}
/// The image an image's shadow holds a copy of ([`ShadowSource::Image`]). Its copies name the
/// image's `VkImage` as each is recorded, not as the shadow was made: a texture's storage form
/// swaps its image's `VkImage` for one with `STORAGE` and destroys the first as the swapping
/// command buffer completes (`texture/storage_form.rs`), and a shadow made before the swap (a
/// slot binding's usage shadow, a view's) copies the image the texture holds from then on.
pub(super) trait ImageTruth: Send + Sync {
/// The image's `VkImage` now.
fn raw(&self) -> vk::Image;
/// Whether its `VkImage` was handed out (`Texture::vk_image`): the caller's own work may
/// write it from then on, unseen, so no shadow of it stands across command buffers
/// (`shadow_standing.rs`).
fn handed_out(&self) -> bool;
}
/// A shadow's truth: what the shadow holds a copy of between an encoder's copies (the module's
/// rules, with the truth in the buffer's place).
pub(super) enum ShadowSource {
/// A buffer's bytes: a 2D texture over a buffer, a staged texture buffer, a heap's depth or
/// stencil texture.
Bytes {
buffer: ShadowBuffer,
/// The buffer's identity.
key: ResourceKey,
/// The bytes the texture lies in, from its first texel to its last.
bytes: Range<u64>,
/// The shadow the texels are copied into and back from.
target: ShadowTarget,
/// The key of the host memory the buffer joined (`host_aliases.rs`), whose uses of the
/// same bytes touch the truth too.
alias: Option<ResourceKey>,
},
/// Another image's texels: a texture view of another component layout on a device whose
/// Vulkan views reinterpret no image's components (`texture/view_shadow.rs`). The shadow is an
/// image of the view's format and the image's shape; `regions` copy the levels and layers the
/// view presents between the two, the same subresources, texel for texel (formats of one copy
/// unit, whose copies keep every bit).
Image {
/// The image, whose `VkImage` each copy names as it is recorded ([`ImageTruth`]).
image: Arc<dyn ImageTruth>,
/// The image's identity.
key: ResourceKey,
/// The image's levels and layers the view presents.
range: UseRange,
shadow: vk::Image,
/// From `image` to `shadow`; the copies back swap the two.
regions: Vec<vk::ImageCopy>,
/// The buffer the copies go through where Vulkan copies no texels between the two
/// images' formats (an alpha format and another, `image_plan::copies_between`).
through: Option<ImageThrough>,
},
/// A surface's two planes in a buffer's bytes, which the shadow holds the texels Metal reads
/// of (`ycbcr.rs`): its copy in converts them, and it is never copied back.
Converted {
buffer: ShadowBuffer,
/// The buffer's identity.
key: ResourceKey,
/// The bytes the planes lie in, from the first plane's first byte to the last's last.
bytes: Range<u64>,
/// As `Bytes`'s.
alias: Option<ResourceKey>,
/// The conversion's staging buffer, kernel and dispatch, and the planes it converts
/// (boxed: the largest variant's fields by far).
conversion: Box<YCbCrConversion>,
/// The texture's format, which a refusal names.
pixel_format: PixelFormat,
},
/// A surface's two planes in a buffer's bytes, into which the texels a function writes to a
/// 4:4:4 two-plane YCbCr texture are stored (`ycbcr.rs`): the shadow is an image of the
/// written texels' bits and a mark of each, its copy in a clear of every texel, unmarked,
/// and its copy back the encoding of the marked texels' codes into the planes.
YCbCrWrite {
buffer: ShadowBuffer,
/// The buffer's identity.
key: ResourceKey,
/// The bytes the planes lie in, from the first plane's first byte to the last's last.
bytes: Range<u64>,
/// As `Bytes`'s.
alias: Option<ResourceKey>,
/// The shadow's image, which the copy in clears.
image: vk::Image,
/// The copy back's staging buffer, kernel and dispatch (boxed, as `Converted`'s).
write_back: Box<YCbCrWriteBack>,
/// The texture's format, which a refusal names.
pixel_format: PixelFormat,
},
}
/// The `VkBuffer` a shadow's bytes lie in.
pub(super) enum ShadowBuffer {
/// One for good: a buffer's the runtime made, or the caller's own `VkBuffer`; and where the
/// buffer's bytes end in it.
Fixed(vk::Buffer, u64),
/// A buffer over host pages, which a rebind gives other pages (`rebind.rs`): its `VkBuffer`
/// is read as each copy is recorded. A rebind keeps the bytes' offset in it (a heap's bytes
/// start at its first byte, and a no-copy buffer with a texture over it keeps its offset in
/// its page), so the copies' offsets hold. Held weakly: a copy is recorded only for a texture
/// its encoder names, whose objects keep the buffer (its texels', or its heap's), so a heap's
/// shadow image the runtime keeps after them keeps no no-copy heap's pages, range or token.
Rebound(WeakBuffer),
}
impl ShadowBuffer {
/// The `VkBuffer` `raw` of `buffer`'s bytes: read as each copy is recorded where a rebind
/// may give the buffer other pages.
pub(super) fn of(buffer: &Buffer, raw: vk::Buffer) -> ShadowBuffer {
match buffer.caller_memory() {
Some(CallerMemory::HostPages) => ShadowBuffer::Rebound(buffer.downgrade()),
_ => ShadowBuffer::Fixed(raw, buffer.start() + buffer.length()),
}
}
/// Where the buffer's bytes end in its `VkBuffer` (its byte 0's offset there,
/// [`Buffer::start`], past its length), which a rebind keeps: a merge in place reads and
/// writes whole words of them up to it (`shadow_merge.rs`). 0 once a rebound buffer went.
pub(super) fn end(&self) -> u64 {
match self {
ShadowBuffer::Fixed(_, end) => *end,
ShadowBuffer::Rebound(buffer) => buffer
.upgrade()
.map_or(0, |buffer| buffer.start() + buffer.length()),
}
}
/// The `VkBuffer` the bytes lie in now: the null handle once a rebound buffer went (a copy's
/// texture keeps its buffer), and while it is detached (`detach.rs`), when its `VkBuffer` is
/// the device's stand-in, which holds none of its bytes. No copy records it: an encoder
/// records nothing of a command that names detached bytes (`EncoderCore::skips`), and nothing
/// planned binds it (a copy back's dispatch, `CopyBack::new`; a merge's window,
/// `ShadowMerge::new`).
pub(super) fn raw(&self) -> vk::Buffer {
match self {
ShadowBuffer::Fixed(raw, _) => *raw,
ShadowBuffer::Rebound(buffer) => buffer
.upgrade()
.and_then(|buffer| buffer.attached_at(0))
.map_or(vk::Buffer::null(), |(raw, _)| raw),
}
}
}
/// A shadowed texture's shadow (see the module).
pub(super) enum ShadowTarget {
/// A 2D texture's image: `regions`, one per slice, copy its texels from the buffer, or from
/// `staging` where Vulkan cannot copy them in place, and `back` copies them out of the image
/// again.
Image {
image: vk::Image,
regions: Vec<vk::BufferImageCopy>,
/// The regions a copy back, and a merge's keep, take out of the image
/// ([`ShadowCopies::record_out`]): `regions` for every shadow but one over an IOSurface
/// plane whose rows share bytes, where going in reads each shared byte for both its rows
/// and going back writes it once, from the earlier row, one of the rows Metal's own
/// writes leave it from (`buffer_texture::plane_copy_regions`).
back: Vec<vk::BufferImageCopy>,
staging: Option<ShadowStaging>,
/// The bytes of one of its texels in the buffer, by which a copy back of a part of the
/// texture ([`ShadowCopy::area`]) offsets its region; `None` where no copy of it is
/// bounded (a heap's depth or stencil texture, which is no color attachment).
texel: Option<u64>,
/// The image's unsigned-integer view, where the device copies back faster by a dispatch
/// (`shadow_copy_back.rs`): through which its copies back dispatch and its merges read
/// the texels the commands left (`shadow_merge.rs`); `None` where they copy.
words: Option<WordView>,
/// The image's view of its own 8-bit unsigned normalized format, where it has no
/// unsigned-integer view: through which its merges read the texels the commands left
/// (`shadow_merge.rs`); `None` where they copy.
unorm: Option<UnormView>,
},
/// A texture buffer's staging buffer, which its bindings view.
Buffer(ShadowStaging),
/// A rotated texture's image of its pass's frame: `staging` holds the bytes as laid out, and
/// `turn` moves them to and from the image, turned (`rotated.rs`).
Rotated {
image: vk::Image,
staging: ShadowStaging,
turn: Box<RotatedCopies>,
},
/// A heap's two-plane extended-range texture's image: `staging` holds its two planes as
/// Metal lays them in the heap's bytes, and `planes` moves them to and from the image
/// (`texture/heap_two_planes.rs`).
Planes {
image: vk::Image,
staging: ShadowStaging,
planes: Box<HeapPlaneCopies>,
/// The texture's format, which a refusal names.
pixel_format: PixelFormat,
},
/// A heap texture's image over host pages: `staging` holds its elements as Metal tiles them
/// in the heap's bytes, and `tiles` moves them to and from the image
/// (`texture/heap_tiled.rs`).
Tiled {
image: vk::Image,
staging: ShadowStaging,
tiles: Box<HeapTiledCopies>,
},
}
impl ShadowTarget {
/// The regions a copy of a 2D texture's image over a buffer's bytes takes, going into the
/// image (`into_shadow`) or back out of it: `regions` in and `back` out, which differ only
/// for an IOSurface plane whose rows share bytes
/// ([`plane_copy_regions`](super::buffer_texture::plane_copy_regions): going in reads each
/// shared byte for both its rows, and going back writes it once, from the earlier row, one of
/// the rows Metal's own writes leave it from).
/// `None` for every other shadow.
pub(super) fn image_regions(&self, into_shadow: bool) -> Option<&[vk::BufferImageCopy]> {
let ShadowTarget::Image { regions, back, .. } = self else {
return None;
};
Some(match into_shadow {
true => regions,
false => back,
})
}
}
/// A shadowed texture's staging buffer (see the module): its bytes, from the first texel to the
/// last, laid out as in the texture's buffer from the staging buffer's start.
/// A runtime buffer the copies between a view's shadow and its image go through
/// ([`ShadowSource::Image`]), held for the shadow's life: each region of the image into it, then
/// out of it into the other image, the same subresources both ways.
pub(super) struct ImageThrough {
pub(super) buffer: Buffer,
pub(super) regions: Vec<vk::BufferImageCopy>,
}
pub(super) struct ShadowStaging {
/// A runtime buffer, held for the texture's life.
pub(super) buffer: Buffer,
/// The texture's bytes from its buffer into the staging buffer.
pub(super) into: vk::BufferCopy,
/// Each row of texels from the staging buffer back into the texture's buffer: one region for
/// rows that lie one after another.
pub(super) back: Vec<vk::BufferCopy>,
}
impl ShadowStaging {
/// The staging buffer's `VkBuffer`, which `into` and `back` address.
pub(super) fn raw(&self) -> vk::Buffer {
self.buffer.at(0).0
}
}
impl ShadowCopies {
/// The shadow of an image of `levels` levels whose copies in copy its first, as a copy taken
/// at use ([`LevelsCopy`]): its range every level, which the first use after a take writes,
/// its later levels made as `later` says.
pub(super) fn taken_at_use(self, levels: u32, later: LaterLevels) -> ShadowCopies {
let (shadow_range, aspects) = match self.shadow_range {
UseRange::Image {
aspects, layers, ..
} => (
UseRange::Image {
aspects,
levels: 0..levels,
layers,
},
aspects,
),
range => (range, vk::ImageAspectFlags::COLOR),
};
ShadowCopies {
shadow_range,
levels: Some(LevelsCopy::new(later, aspects)),
..self
}
}
/// Make what its copies in record where they record more than the texture holds from its
/// making: a conversion's staging buffer, sets and refresh (`ycbcr.rs`,
/// `YCbCrConversion::make`), which a texture whose planes every function reads in place never
/// needs. Refused as the conversion's making is.
pub(super) fn prepare(&self) -> Result<(), Error> {
match &self.source {
ShadowSource::Converted { conversion, .. } => conversion.make(),
_ => Ok(()),
}
}
/// The image a clear in of the shadow clears ([`ShadowCopy::clear`]): an image's shadow of
/// its own format (one copied image to image, not through a buffer), or a 2D texture's image
/// over a buffer's bytes; `None` for every other shadow (a staging buffer, a rotated, tiled or
/// planar image, a conversion's, a copy taken at use).
pub(super) fn cleared_image(&self) -> Option<vk::Image> {
// A copy taken at use keeps what its image holds between its takes.
if self.levels.is_some() {
return None;
}
match &self.source {
ShadowSource::Image {
shadow,
through: None,
..
} => Some(*shadow),
ShadowSource::Bytes {
target: ShadowTarget::Image { image, .. },
..
} => Some(*image),
_ => None,
}
}
/// Whether the shadow is a large IOSurface plane's, from which its copies in count apart
/// (`WorkCounts::shadow_texels_first_copied_in_large_surfaces` and kin): a texture over an
/// IOSurface's plane of at least [`LARGE_SURFACE_TEXELS`] texels.
pub(super) fn large_surface(&self) -> bool {
let surface = matches!(self.backing, Backing::Surface | Backing::PrivateSurface);
surface && self.texels() >= LARGE_SURFACE_TEXELS
}
/// Whether only the runtime's commands write the shadow's truth, which the tracker orders
/// against the commands of every queue: the bytes of a tracked buffer of private or
/// memoryless storage the runtime allocated, an IOSurface plane's among them, and a tracked
/// image of such storage the runtime made in memory of its own ([`Backing::PrivateImage`]).
/// The CPU reaches none of them (`buffer_texture.rs` refuses its access to a `Private`
/// buffer's bytes, `Texture::replace_region` a `Private` texture's texels).
pub(super) fn only_commands_write(&self) -> bool {
let private = matches!(
self.backing,
Backing::PrivateBuffer | Backing::PrivateSurface | Backing::PrivateImage
);
self.tracked && private
}
/// Whether a copy in of the shadow's texels is cut to the rectangle of a part of them
/// ([`ShadowCopy::area`]): a one-level, one-slice 2D texture's image over a buffer's bytes,
/// one region copied in place (no staging buffer, [`part_of`]), whose copies back are cut
/// too; or a shadow of one level and slice of another image, copied image to image
/// ([`image_part_of`]), whose copies back move every texel ([`cuts_back`](Self::cuts_back)).
pub(super) fn cuts(&self) -> bool {
match &self.source {
ShadowSource::Bytes {
target:
ShadowTarget::Image {
regions,
staging: None,
texel: Some(_),
..
},
..
} => regions.len() == 1,
ShadowSource::Image {
regions,
through: None,
..
} => one_image_copy(regions).is_some(),
_ => false,
}
}
/// Whether a copy back of the shadow's texels is cut to a rectangle as its copy in is: a
/// buffer's bytes' shadow that [`cuts`](Self::cuts). An image's shadow's copy back moves
/// every texel, so a command that writes through it takes every texel in first.
fn cuts_back(&self) -> bool {
self.cuts() && self.target().is_some()
}
/// Whether a command that writes every texel of the boxes `covered` writes every texel the
/// shadow holds: a one-level, one-slice 2D texture's image over a buffer's bytes, laid out
/// in rows or in Metal's tiles of a heap's bytes (`heap_tiled.rs`), its one region inside one
/// of the boxes, of every aspect it holds. Such a shadow's copy back moves its texels alone,
/// never the bytes between them (the module's rules; a tiled one's staging starts as the
/// heap's bytes), so the copy back of what the command wrote moves only texels it wrote.
/// `false` for every other shadow.
pub(super) fn covered_by(&self, covered: &[Covered]) -> bool {
let (image, regions) = match &self.source {
ShadowSource::Bytes {
target: ShadowTarget::Image { image, regions, .. },
..
} => (image, regions.as_slice()),
ShadowSource::Bytes {
target: ShadowTarget::Tiled { image, tiles, .. },
..
} => (image, tiles.regions()),
_ => return false,
};
let ([region], UseRange::Image { levels, layers, .. }) = (regions, &self.shadow_range)
else {
return false;
};
let held = region.image_subresource;
let one = levels.len() == 1 && layers.len() == 1 && held.layer_count == 1;
one && covered.iter().any(|written| {
let at = written.subresource;
written.image == *image
&& at.aspect_mask.contains(held.aspect_mask)
&& at.mip_level == held.mip_level
&& at.base_array_layer <= held.base_array_layer
&& u64::from(held.base_array_layer)
< u64::from(at.base_array_layer) + u64::from(at.layer_count)
&& within(
(region.image_offset, region.image_extent),
(written.offset, written.extent),
)
})
}
/// The texels the shadow holds: its every region's, every slice of each.
pub(super) fn texels(&self) -> u64 {
match &self.source {
ShadowSource::Image { regions, .. } => regions.iter().map(image_copy_texels).sum(),
ShadowSource::Bytes {
target: ShadowTarget::Image { regions, .. },
..
} => regions.iter().map(buffer_copy_texels).sum(),
ShadowSource::Bytes { .. }
| ShadowSource::Converted { .. }
| ShadowSource::YCbCrWrite { .. } => 0,
}
}
/// Whether `other`'s texels lie in the truth as this shadow's do, texel for texel: both cut
/// to rectangles ([`cuts`](Self::cuts)), of one buffer, their one regions starting at one
/// byte, rows as long, of one extent, of texels of one size; or of one image, their one
/// regions of one aspect, level and slice, at one offset, of one extent. A change of one's
/// texels inside a rectangle is then a change of the other's inside it alone.
pub(super) fn same_texels(&self, other: &ShadowCopies) -> bool {
let of_image = |copies: &ShadowCopies| match &copies.source {
ShadowSource::Image {
key,
regions,
through: None,
..
} => one_image_copy(regions).map(|region| (*key, *region)),
_ => None,
};
if let (Some((key, a)), Some((other_key, b))) = (of_image(self), of_image(other)) {
let (s, t) = (a.src_subresource, b.src_subresource);
return key == other_key
&& s.aspect_mask == t.aspect_mask
&& s.mip_level == t.mip_level
&& s.base_array_layer == t.base_array_layer
&& a.src_offset == b.src_offset
&& a.extent == b.extent;
}
let layout = |copies: &ShadowCopies| match &copies.source {
ShadowSource::Bytes {
key,
target:
ShadowTarget::Image {
regions,
staging: None,
texel: Some(texel),
..
},
..
} => match regions.as_slice() {
[region] => Some((*key, *region, *texel)),
_ => None,
},
_ => None,
};
let (Some((key, a, texel)), Some((other_key, b, other_texel))) =
(layout(self), layout(other))
else {
return false;
};
let row = |r: &vk::BufferImageCopy| match r.buffer_row_length {
0 => r.image_extent.width,
row => row,
};
key == other_key
&& texel == other_texel
&& a.buffer_offset == b.buffer_offset
&& row(&a) == row(&b)
&& a.image_offset == b.image_offset
&& a.image_extent == b.image_extent
}
/// Whether the shadow is a heap's one-level 2D two-plane texture's, whose truth is its two
/// planes in the heap's bytes ([`ShadowTarget::Planes`]) where Metal's blits address them
/// (`HeapPlaneCopies::layout`, `TwoPlaneHeapLayout::blit_rows`).
pub(super) fn holds_blit_rows(&self) -> bool {
self.plane_layout()
.is_some_and(TwoPlaneHeapLayout::blit_rows)
}
/// Where a heap's two-plane texture's first level lies in the heap's bytes, where the shadow
/// is such a texture's ([`ShadowTarget::Planes`]).
pub(super) fn plane_layout(&self) -> Option<&TwoPlaneHeapLayout> {
match self.target() {
Some(ShadowTarget::Planes { planes, .. }) => Some(planes.layout()),
_ => None,
}
}
/// The truth's identity and the range of it the shadow copies.
pub(super) fn truth(&self) -> (ResourceKey, UseRange) {
match &self.source {
ShadowSource::Bytes { key, bytes, .. }
| ShadowSource::Converted { key, bytes, .. }
| ShadowSource::YCbCrWrite { key, bytes, .. } => (*key, UseRange::Bytes(bytes.clone())),
ShadowSource::Image { key, range, .. } => (*key, range.clone()),
}
}
/// Whether its copy in converts its truth's bytes (`ShadowSource::Converted`,
/// `ShadowSource::YCbCrWrite`) rather than moving them: zero bytes there are not zero
/// bytes in the shadow.
pub(super) fn converts(&self) -> bool {
matches!(
self.source,
ShadowSource::Converted { .. } | ShadowSource::YCbCrWrite { .. }
)
}
/// The staging buffer a 2D texture's image over a buffer's bytes is copied through, or a
/// texture buffer's bindings view, where it has one ([`ShadowStaging`]): a texture's whose
/// texels run past the buffer's length, its tail, holds fewer of the buffer's bytes than the
/// texture spans (`buffer_texture.rs`).
pub(super) fn staged_bytes(&self) -> Option<&ShadowStaging> {
match self.target()? {
ShadowTarget::Image { staging, .. } => staging.as_ref(),
ShadowTarget::Buffer(staging) => Some(staging),
_ => None,
}
}
/// The shadow of a buffer's bytes, or `None` for an image's and for converted texels'.
pub(super) fn target(&self) -> Option<&ShadowTarget> {
match &self.source {
ShadowSource::Bytes { target, .. } => Some(target),
ShadowSource::Image { .. }
| ShadowSource::Converted { .. }
| ShadowSource::YCbCrWrite { .. } => None,
}
}
/// Whether a render pass that reaches no tile and loads this shadow's texture leaves it to
/// its truth, which keeps every texel: an image of a buffer's bytes (`ShadowTarget::Image`),
/// which the texture's own image holds, and a two-plane YCbCr texture's render shadow,
/// whose copy back would store the planes' codes again (`ycbcr.rs`), where Metal keeps them.
pub(super) fn left_untouched(&self) -> bool {
match &self.source {
ShadowSource::Bytes { target, .. } => {
matches!(target, ShadowTarget::Image { .. }) && self.writes_reach_truth()
}
ShadowSource::YCbCrWrite { write_back, .. } => write_back.loads(),
ShadowSource::Image { .. } | ShadowSource::Converted { .. } => false,
}
}
/// Whether a render pass that draws loads this shadow other than by the inverse of its
/// store: a rotated texture's frame of rotation 4 (`rotated.rs`).
pub(super) fn loads_apart(&self) -> bool {
matches!(self.target(), Some(ShadowTarget::Rotated { turn, .. }) if turn.loads_apart())
}
/// Whether the texture is tracked and its truth lies in host pages a consumer logs the writes
/// to (`HostMemory::with_dirty_pages`): its copies in of every texel are gated
/// (`dirty_pages.rs`), so none is cut to a rectangle. Not a copy taken at use, whose every copy
/// in moves every texel.
pub(super) fn logged(&self) -> bool {
let ShadowSource::Bytes {
buffer: ShadowBuffer::Rebound(buffer),
..
} = &self.source
else {
return false;
};
let pages = || buffer.upgrade().and_then(|buffer| buffer.host_pages());
let logged = pages().is_some_and(|pages| pages.dirty().is_some());
self.tracked && self.levels.is_none() && logged
}
/// Whether the rectangle `area` (`None` every texel) covers every texel of a one-region 2D
/// texture's image over a buffer's bytes, or is `None`.
pub(super) fn covers(&self, area: Option<[u32; 4]>) -> bool {
let Some([x0, y0, x1, y1]) = area else {
return true;
};
let regions = (self.target()).and_then(|target| target.image_regions(true));
let Some([region]) = regions else {
return false;
};
let (x, y) = (region.image_offset.x, region.image_offset.y);
let (Ok(x), Ok(y)) = (u32::try_from(x), u32::try_from(y)) else {
return false;
};
let extent = region.image_extent;
x0 <= x && y0 <= y && x1 >= x + extent.width && y1 >= y + extent.height
}
/// Every texel of a one-region 2D texture's image over a buffer's bytes, `[x0, y0, x1, y1)`, as
/// [`covers`](Self::covers) takes a rectangle; `None` for any other shadow.
pub(super) fn whole_area(&self) -> Option<[u32; 4]> {
let [region] = (self.target())?.image_regions(true)? else {
return None;
};
let x = u32::try_from(region.image_offset.x).ok()?;
let y = u32::try_from(region.image_offset.y).ok()?;
let extent = region.image_extent;
Some([x, y, x + extent.width, y + extent.height])
}
/// Whether `used` names the shadow where it holds a copy of the truth: its key, in a range
/// that meets [`shadow_range`](Self::shadow_range). A copy taken at use holds every level
/// (the module's **Copies taken at use**): a use of any of them takes it.
pub(super) fn holds(&self, used: &Use<ResourceKey>) -> bool {
used.key == self.shadow_key && used.range.overlaps(&self.shadow_range)
}
/// Whether `used` touches the texture's truth: its bytes, or its image's levels and layers.
pub(super) fn meets(&self, used: &Use<ResourceKey>) -> bool {
let (key, range) = self.truth();
(used.key == key || Some(used.key) == self.alias()) && used.range.overlaps(&range)
}
/// Why a command writing the truth both through this shadow and another way is refused: no
/// merge serves an image's shadow (`Unsupported`), nor a rotated texture's, whose texels lie
/// turned in its bytes (`NotImplemented(RotatedTexture)`); `None` for a shadow a merge serves.
fn unmerged(&self) -> Option<Error> {
match &self.source {
ShadowSource::Image { .. } => Some(Error::Unsupported(reinterpretation_refused())),
ShadowSource::Bytes {
target: ShadowTarget::Rotated { .. },
..
} => Some(Error::NotImplemented(RuntimeGap::RotatedTexture)),
// Its texels lie in two planes of its bytes, which no merge reaches.
ShadowSource::Bytes {
target: ShadowTarget::Planes { pixel_format, .. },
..
} => Some(Error::Unsupported(
Unsupported::not_implemented(Feature::Format).naming_format(*pixel_format),
)),
ShadowSource::Bytes { .. } => None,
// Nothing writes converted texels' image (`ycbcr.rs`): no command reaches here.
// Its writes are encoded into two planes, which no merge reaches.
ShadowSource::Converted { pixel_format, .. }
| ShadowSource::YCbCrWrite { pixel_format, .. } => Some(Error::Unsupported(
Unsupported::not_implemented(Feature::Format).naming_format(*pixel_format),
)),
}
}
/// Whether the image may keep a standing across command buffers (`shadow_standing.rs`): a
/// shadow of a buffer's bytes or of another image that only the runtime's commands write
/// ([`only_commands_write`](Self::only_commands_write)), whose writes reach them, over a
/// buffer of the runtime's own whose `VkBuffer` was never handed out, or an image of its own
/// whose `VkImage` was not.
pub(super) fn stands_across(&self) -> bool {
let truth = matches!(
self.source,
ShadowSource::Bytes { .. } | ShadowSource::Image { .. }
);
truth
&& self.only_commands_write()
&& self.writes_reach_truth()
&& self.standing.may_stand()
}
/// Whether the shadow's truth is a buffer's bytes as they lie (`ShadowSource::Bytes`): what an
/// image in place over them is too (`texture/storage_form.rs`).
pub(super) fn copies_bytes(&self) -> bool {
matches!(self.source, ShadowSource::Bytes { .. })
}
/// The keys a write of which lets go of the image's standing across command buffers: the
/// truth's, the host memory it joined, and the image's own.
pub(super) fn standing_keys(&self) -> Vec<ResourceKey> {
let mut keys = vec![self.truth().0, self.shadow_key];
keys.extend(self.alias());
keys
}
/// Whether what the commands write through the shadow goes back into its truth as a merge
/// (`shadow_merge.rs`): a buffer's bytes that another writer may write beside the commands'
/// writes while a command buffer runs, whose writes to texels the commands left Metal keeps;
/// never copied back over those texels (the module's rules). That is every buffer's bytes but
/// a tracked `Private` or `Memoryless` buffer's the runtime made, an IOSurface plane's in one
/// among them ([`only_commands_write`](Self::only_commands_write)): the CPU writes the others
/// (a `Shared` or `Managed` buffer's, host pages, the caller's own `VkBuffer`'s memory), and
/// another queue's commands an untracked buffer's, which the tracker leaves to the program. An
/// image's shadow, a turned one and two planes take no merge ([`unmerged`](Self::unmerged)),
/// and writes Metal drops, and those through a copy taken at use, go back no way.
pub(super) fn merges_back(&self) -> bool {
let bytes = matches!(&self.source, ShadowSource::Bytes { .. });
let other_writer = !self.only_commands_write();
bytes && other_writer && self.writes_reach_truth() && self.unmerged().is_none()
}
/// Whether what the commands write through the shadow goes back into its truth: not where
/// Metal drops those writes ([`drops_writes`](Self::drops_writes)), nor for a copy taken at
/// use ([`levels`](Self::levels)), whose writes stay in its image.
pub(super) fn writes_reach_truth(&self) -> bool {
!self.drops_writes && self.levels.is_none()
}
/// Whether two shadowed textures copy some of the same truth.
pub(super) fn shares_truth(&self, other: &ShadowCopies) -> bool {
let ((key, range), (other_key, other_range)) = (self.truth(), other.truth());
let mine = [Some(key), self.alias()];
let theirs = [Some(other_key), other.alias()];
mine.iter()
.flatten()
.any(|key| theirs.contains(&Some(*key)))
&& range.overlaps(&other_range)
}
/// The key of the host memory the truth's buffer joined (`host_aliases.rs`): a use of it
/// touches the same bytes as one of the buffer's own.
pub(super) fn alias(&self) -> Option<ResourceKey> {
match &self.source {
ShadowSource::Bytes { alias, .. }
| ShadowSource::Converted { alias, .. }
| ShadowSource::YCbCrWrite { alias, .. } => *alias,
ShadowSource::Image { .. } => None,
}
}
/// Where the texture's first byte lies in its buffer; `None` for an image's shadow.
pub(super) fn first_byte(&self) -> Option<u64> {
match self.target()? {
ShadowTarget::Image {
staging: Some(staging),
..
}
| ShadowTarget::Buffer(staging)
| ShadowTarget::Rotated { staging, .. }
| ShadowTarget::Planes { staging, .. }
| ShadowTarget::Tiled { staging, .. } => Some(staging.into.src_offset),
// Slice 0's region starts at the first texel.
ShadowTarget::Image { regions, .. } => {
Some(regions.first().map_or(0, |region| region.buffer_offset))
}
}
}
/// Whether a merge's keep and changed copy ([`record_kept`](Self::record_kept),
/// [`record_out`](Self::record_out)) each write every one of the first `length` bytes of its
/// part: a heap's tiled texture's do, as they copy the staging buffer's span of the texture's
/// bytes whole (`HeapTiledCopies::record_out`), or the heap's.
pub(super) fn copies_out_whole(&self, length: u64) -> bool {
match &self.source {
ShadowSource::Bytes {
target: ShadowTarget::Tiled { staging, .. },
..
} => staging.into.size >= length,
_ => false,
}
}
/// Whether a merge's keep right after this shadow's copy in, with no copy of the shadow
/// between, may copy the staging buffer that copy in filled
/// ([`record_kept_from_staging`](Self::record_kept_from_staging)): a heap's tiled texture's
/// whose moves give back each byte they took (`HeapTiledCopies::moves_bits`), or whose every
/// keep copies that buffer, a depth's (`HeapTiledCopies::takes_depth`,
/// [`record_kept`](Self::record_kept)).
pub(super) fn keeps_from_staging(&self) -> bool {
matches!(
&self.source,
ShadowSource::Bytes {
target: ShadowTarget::Tiled { tiles, .. },
..
} if tiles.moves_bits() || tiles.takes_depth()
)
}
/// Record a merge's keep into `to` from `at` as a copy of the staging buffer as the shadow's
/// last copy left it: right after the shadow's copy in filled it with the heap's bytes and
/// moved them into the image ([`keeps_from_staging`](Self::keeps_from_staging)), the
/// texture's bytes from its first as the copy in took them, which a keep's move out of the
/// image would leave there where the moves give back each byte they took, without the move;
/// and a depth's keep after any copy ([`record_kept`](Self::record_kept)). Nothing for any
/// other shadow.
///
/// # Safety
///
/// As `record_out`'s, after a barrier that orders the copy after the shadow's last copy, and
/// with no command between them that writes the shadow or its staging buffer.
pub(super) unsafe fn record_kept_from_staging(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
(to, at): (vk::Buffer, u64),
) {
let ShadowSource::Bytes {
target: ShadowTarget::Tiled { staging, .. },
..
} = &self.source
else {
return;
};
let region = vk::BufferCopy {
src_offset: staging.into.dst_offset,
dst_offset: at,
size: staging.into.size,
};
// SAFETY: the caller's contract; the staging buffer holds the texture's bytes from its
// start (`HeapTiledCopies::staging`), and `to` holds them from `at`.
unsafe { device.cmd_copy_buffer(command_buffer, staging.raw(), to, &[region]) };
}
/// Record a merge's keep of the shadow's texels into `to` from `at`, laid out as in the
/// buffer from the texture's first byte (`shadow_merge.rs`): as
/// [`record_out`](Self::record_out) copies them; but where a heap's tiled texture has a
/// depth aspect, whose moves into its image may change a value
/// (`HeapTiledCopies::takes_depth`: one outside `[0, 1]`, `restricted-depth-upload`, and
/// those `drawn-depth-copy` names), its staging buffer, after a barrier from everything
/// before. Every copy of the shadow fills that buffer whole (`HeapTiledCopies::record`,
/// `HeapTiledCopies::record_out`), and no command writes through the texture between the
/// last such copy and a keep: a copy in leaves it the heap's bytes it read, and a copy back
/// or a merge's copy of what changed the image's elements, which that copy back or merge
/// wrote into the heap. So the keep holds the heap's bytes as the image stands for them, and
/// the merge writes each byte the image holds otherwise: every byte a command wrote, and the
/// values the copy in changed (both announced), never a byte of the heap's in a value a
/// command wrote. A keep of the image would hold the changed value, and keep the heap's byte
/// in each byte a command wrote that equals it; one of the heap's bytes as the keep runs would
/// hold another writer's write since the copy in (the CPU's), which the merge would then undo
/// with the image's byte.
///
/// # Safety
///
/// As `record_out`'s.
pub(super) unsafe fn record_kept(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
(to, at): (vk::Buffer, u64),
moves: Option<&SampleMoves>,
) {
if let ShadowSource::Bytes {
target: ShadowTarget::Tiled { tiles, .. },
..
} = &self.source
{
if tiles.takes_depth() {
// SAFETY: the caller's contract; the barrier orders the copy after the shadow's
// last copy, which filled the staging buffer.
unsafe {
Barrier::everything().record(device, command_buffer);
self.record_kept_from_staging(device, command_buffer, (to, at));
}
return;
}
}
// SAFETY: the caller's contract.
unsafe { self.record_out(device, command_buffer, (to, at), moves) };
}
/// Record a copy of the shadow's texels into `to` from `at`, laid out as in the buffer from
/// the texture's first byte (`shadow_merge.rs`), a heap's multisample texture's through
/// `moves` out of its image (`ShadowCopy::plan_moves`).
///
/// # Safety
///
/// `command_buffer` is recording, outside a render pass; the shadow, the moves and `to` are
/// live until it completes, `to` holds the texture's bytes from `at`, a multiple of 16, and an
/// image is in the `GENERAL` layout.
pub(super) unsafe fn record_out(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
(to, at): (vk::Buffer, u64),
moves: Option<&SampleMoves>,
) {
// An image's shadow keeps no bytes, a rotated texture's keeps them turned, and a heap's
// two-plane texture's in two planes: no merge is made of any (`shadow_merge.rs`,
// `ShadowCopies::unmerged`).
let Some(target) = self.target() else {
return;
};
match target {
ShadowTarget::Rotated { .. } | ShadowTarget::Planes { .. } => {}
ShadowTarget::Tiled {
image,
staging,
tiles,
} => {
// SAFETY: the caller's contract, which the tiles' record takes.
unsafe {
tiles.record_out(device, command_buffer, (*image, moves), staging, (to, at));
}
}
ShadowTarget::Image { image, .. } => {
let back = target.image_regions(false).unwrap_or_default();
let origin = back.first().map_or(0, |region| region.buffer_offset);
let moved: Vec<vk::BufferImageCopy> = back
.iter()
.map(|region| vk::BufferImageCopy {
buffer_offset: region.buffer_offset - origin + at,
..*region
})
.collect();
// SAFETY: the caller's contract; the regions keep their offsets from the first
// texel, multiples of the texel's size, and `at` is one too.
unsafe {
device.cmd_copy_image_to_buffer(
command_buffer,
*image,
vk::ImageLayout::GENERAL,
to,
&moved,
);
}
}
ShadowTarget::Buffer(staging) => {
// Every byte of the texels': a tail's past the buffer's length too, past the ones
// its copy in takes from the buffer (`buffer_texture.rs`).
let size = match &self.source {
ShadowSource::Bytes { bytes, .. } => bytes.end - bytes.start,
_ => staging.into.size,
};
let region = vk::BufferCopy {
src_offset: staging.into.dst_offset,
dst_offset: at,
size,
};
// SAFETY: the caller's contract; the staging buffer holds the texture's bytes.
unsafe { device.cmd_copy_buffer(command_buffer, staging.raw(), to, &[region]) };
}
}
}
}
/// One step of a shadowed texture's copies (the module's rules), `M` the scratch of a merge
/// (`shadow_merge.rs`; another type in the unit tests).
#[derive(Clone)]
pub(super) enum Step<M> {
/// From the buffer into the shadow.
Into,
/// From the shadow back into the buffer.
Back,
/// The shadow kept, before a command that writes its bytes two ways, or before the first
/// command since the last copy back that writes through it where it merges back
/// ([`ShadowCopies::merges_back`]).
Keep(M),
/// After the command that wrote two ways, or in place of the next copy back, the bytes
/// changed through the texture since the keep merged into the buffer's.
Merge(M),
}
/// A shadow's copy in left by its encoder to its batch's submission (`Deferred::GatedCopyIn`):
/// over host pages a consumer logs (`dirty_pages.rs`), the commit asks the log for a stamp, and
/// the copy's regions are those the log says changed once that stamp is published, which only a
/// recording after the commit can know; a command buffer's first copy in of a shadow that may
/// stand across command buffers (`shadow_standing.rs`) moves nothing where the image stands as
/// the batch is submitted, in the scheduler's order. Recorded in a command buffer of its own
/// between the encoder's, behind a barrier from everything before it and before one to
/// everything after it, so it keeps its place among the encoder's commands.
pub(super) struct GatedCopyIn {
copy: ShadowCopy,
gate: CopyGate,
}
/// What decides a copy in as its batch is submitted.
pub(super) enum CopyGate {
/// Its regions: those its log says changed (`dirty_pages.rs`).
Log(Arc<CopyInGate>),
/// Whether it moves anything: nothing where the image stands across command buffers
/// (`shadow_standing.rs`).
Standing,
}
impl GatedCopyIn {
/// `copy`, decided by `gate`.
pub(super) fn new(copy: ShadowCopy, gate: CopyGate) -> GatedCopyIn {
GatedCopyIn { copy, gate }
}
/// The shadow whose standing decides it, where that does.
pub(super) fn standing(&self) -> Option<&Arc<ShadowCopies>> {
matches!(self.gate, CopyGate::Standing).then_some(&self.copy.copies)
}
/// The texels it moves where it moves every one it was planned to.
pub(super) fn texels(&self) -> u64 {
self.copy.texels_copied_in()
}
/// The copy it records where its standing decides it, as `stands` says
/// ([`record`](Self::record)): none where the image stands holding every texel the copy
/// moves (`Some(None)`, or a stale rectangle outside them), the copy cut to the stale
/// rectangle where it stands but for the texels writes within that changed since
/// (`Some(Some(stale))`), and the copy as planned where it does not stand (`None`).
fn standing_copy(&self, stands: Option<Option<[u32; 4]>>) -> Option<ShadowCopy> {
match stands {
None => Some(self.copy.clone()),
Some(None) => None,
Some(Some(stale)) => {
let area = match self.copy.area {
Some(needs) => meet(needs, stale)?,
None => stale,
};
Some(ShadowCopy {
area: Some(area),
..self.copy.clone()
})
}
}
}
/// The texels it moves where its standing decides it as `stands` says
/// ([`record`](Self::record)).
pub(super) fn texels_moved(&self, stands: Option<Option<[u32; 4]>>) -> u64 {
(self.standing_copy(stands)).map_or(0, |copy| copy.texels_copied_in())
}
/// Record it into `command_buffer`, decided now. A copy its standing decides moves nothing
/// where the image stands (`stands` is `Some(None)`), only the texels inside the rectangle
/// writes within it changed since it stood where it stands but for those (`Some(Some(_))`,
/// `shadow_standing.rs`), and every texel it was planned to where it does not (`None`).
///
/// # Safety
///
/// `command_buffer` is recording, outside a render pass; the copy's texture is live (its
/// encoder's command buffer holds it), its image in the `GENERAL` layout.
pub(super) unsafe fn record(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
stands: Option<Option<[u32; 4]>>,
) {
let (copy, gate) = match &self.gate {
CopyGate::Log(gate) => (self.copy.clone(), Some(Arc::clone(gate))),
CopyGate::Standing => match self.standing_copy(stands) {
Some(copy) => (copy, None),
None => return,
},
};
// SAFETY: the caller's.
unsafe {
Barrier::everything().record(device, command_buffer);
copy.record_gated(device, command_buffer, None, gate);
Barrier::everything().record(device, command_buffer);
}
}
}
/// One copy between a shadowed texture's buffer and its shadow.
#[derive(Clone)]
pub(super) struct ShadowCopy<M = Arc<ShadowMerge>> {
pub(super) copies: Arc<ShadowCopies>,
pub(super) step: Step<M>,
/// A copy in before a command that leaves the texels in place ([`ShadowUse::kept`]).
pub(super) kept: bool,
/// A copy back of the texels inside `[x0, y0, x1, y1)` of a one-level, one-slice texture's
/// image alone, where the commands since the last copy back wrote through it there and
/// nowhere else ([`ShadowUse::area`]), or a copy in of them alone, where the command reads
/// and writes no other texel of it ([`ShadowUse::loads`]); `None` for every texel.
pub(super) area: Option<[u32; 4]>,
/// Why a copy in is made, as `WorkCounts` counts its texels; `None` for every other step.
pub(super) cause: Option<CopyCause>,
/// A heap's multisample texture's moves between its image and Metal's tiles that its record
/// takes, planned as the encoder takes the copy (`EncoderCore::sample_moves`); `None` for
/// every other texture, and where none could be planned (failing the command buffer).
pub(super) moves: Option<Arc<SampleMoves>>,
/// A copy taken at use's later levels' run that a copy in of it records after its copy into
/// the first level (the compute filter's or the picks', [`LevelsCopy::run`]), made as the
/// encoder takes the copy (`EncoderCore::later_runs`); `None` for every other copy, and where
/// none could be made (failing the command buffer).
pub(super) later: Option<Arc<LaterRun>>,
/// A copy in made as a clear of the shadow's image ([`ShadowCopies::cleared_image`]) to this
/// value, its one level and slice, reading nothing of the truth ([`ShadowUse::cleared`]);
/// `None` for every other step.
pub(super) clear: Option<vk::ClearColorValue>,
}
/// Why a copy in is made, as `WorkCounts` counts its texels.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum CopyCause {
/// The encoder's first copy in of the texture: before the first command of the encoder that
/// uses it, or the first after work it records apart ([`ShadowSync::leave`]). Every later one
/// follows a change of the truth, or a command that needs texels the image does not hold
/// (`WorkCounts::shadow_texels_first_copied_in`).
First,
/// A later copy in before a command that needs texels outside the rectangle the image holds
/// (its last copy in's, [`ShadowUse::loads`]), the truth unchanged there or not
/// (`WorkCounts::shadow_texels_recopied_beyond_held`).
BeyondHeld,
/// The truth changed since the image's copy in, other than by writes within a rectangle that
/// left it stale there alone, for the reason given
/// (`WorkCounts::shadow_texels_recopied_after_changes`).
Changed(Lost),
/// A copy in of the texels writes [`Within`] a rectangle changed since the image's copy in,
/// [`ShadowCopy::area`] the rectangle bounding them (the module's rules,
/// `WorkCounts::shadow_texels_bounded_copied_in`).
Bounded,
}
/// Why a shadow's image no longer holds the truth as the encoder's commands left it, so the next
/// command that uses the texture copies it in again ([`CopyCause::Changed`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Lost {
/// Another shadowed texture over the same truth was copied back or merged.
OtherShadow,
/// Its own merge after a command that wrote the truth two ways.
TwoWays,
/// A command wrote the truth another way (its bytes, or an image's levels and layers).
Written,
/// A command wrote the truth within a rectangle ([`Within`]) where that leaves the image
/// stale nowhere alone: the writes' texture has no shadow, or the image is written, kept, or
/// its texels are laid out otherwise.
Within,
/// A command wrote through it where Metal drops the writes.
Dropped,
}
impl<M> ShadowCopy<M> {
/// The moves between a heap's multisample texture's image and Metal's tiles that the copy's
/// record takes (`texture/heap_tiled.rs`), planned now through `maker`: into the image for a
/// copy in, out of it for every other step (a copy back, a merge's keep and its copy of what
/// changed) but a depth's keep, which copies its staging buffer
/// ([`ShadowCopies::record_kept`]). `None` for every other texture. Refused as their images
/// are made.
pub(super) fn plan_moves(&self, maker: &UseMaker) -> Result<Option<SampleMoves>, Error> {
if !self.moves_samples() {
return Ok(None);
}
let Some(ShadowTarget::Tiled { tiles, .. }) = self.copies.target() else {
return Ok(None);
};
if matches!(self.step, Step::Keep(_)) && tiles.takes_depth() {
return Ok(None);
}
tiles.sample_moves(maker, matches!(self.step, Step::Into))
}
/// Whether the copy's record takes moves between a heap's multisample texture's image and
/// Metal's tiles ([`plan_moves`](Self::plan_moves)).
pub(super) fn moves_samples(&self) -> bool {
let tiled = self.copies.target();
matches!(tiled, Some(ShadowTarget::Tiled { tiles, .. }) if tiles.moves_samples())
}
/// Whether the copy writes the texture's truth: a copy back or a merge.
pub(super) fn writes_truth(&self) -> bool {
matches!(self.step, Step::Back | Step::Merge(_))
}
/// The texels a copy in moves, as `record` cuts it, of a 2D texture's image over a buffer's
/// bytes (as [`texels_in`](Self::texels_in) counts them) or of an image's shadow (its every
/// region's, or its one region's inside [`area`](Self::area)): 0 for every other copy.
pub(super) fn texels_copied_in(&self) -> u64 {
if self.clear.is_some() {
return 0;
}
match (&self.step, &self.copies.source) {
(Step::Into, ShadowSource::Image { regions, .. }) => {
match (self.area, self.copies.cuts(), one_image_copy(regions)) {
(Some(area), true, Some(region)) => image_part_of(region, area)
.as_ref()
.map_or(0, image_copy_texels),
_ => regions.iter().map(image_copy_texels).sum(),
}
}
(Step::Into, _) => self.texels_in(),
_ => 0,
}
}
/// The texels a copy back of a 2D texture's image over a buffer's bytes moves (its every
/// region's, or its one region's inside [`area`](Self::area)): 0 for every other copy.
pub(super) fn texels_back(&self) -> u64 {
match self.step {
Step::Back => self.image_texels(),
_ => 0,
}
}
/// The texels a copy in of a 2D texture's image over a buffer's bytes moves, as
/// [`texels_back`](Self::texels_back) counts them: 0 for every other copy.
pub(super) fn texels_in(&self) -> u64 {
match self.step {
Step::Into if self.clear.is_some() => 0,
Step::Into => self.image_texels(),
_ => 0,
}
}
/// The texels a copy in or back of a 2D texture's image over a buffer's bytes moves, as
/// `record` cuts its region: 0 for any other shadow.
fn image_texels(&self) -> u64 {
let Some(ShadowTarget::Image {
regions,
staging,
texel,
..
}) = self.copies.target()
else {
return 0;
};
// As `record` cuts the region.
match (self.area, staging, regions.as_slice(), texel) {
(Some(area), None, [region], Some(texel)) => part_of(region, area, *texel)
.as_ref()
.map_or(0, buffer_copy_texels),
_ => regions.iter().map(buffer_copy_texels).sum(),
}
}
}
/// The texels a copy of `region` moves between a buffer and an image.
fn buffer_copy_texels(region: &vk::BufferImageCopy) -> u64 {
let e = region.image_extent;
u64::from(e.width) * u64::from(e.height) * u64::from(e.depth)
}
/// The texels a copy of `region` moves between two images: every slice's.
fn image_copy_texels(region: &vk::ImageCopy) -> u64 {
let e = region.extent;
let layers = u64::from(region.src_subresource.layer_count);
u64::from(e.width) * u64::from(e.height) * u64::from(e.depth) * layers
}
impl ShadowCopy {
/// Whether the copy goes to its batch's submission, there to move nothing where the image
/// stands across command buffers (`shadow_standing.rs`): an encoder's first copy in of a
/// shadow that may stand, moving the truth's texels (no clear, no heap tile moves).
pub(super) fn left_to_standing(&self) -> bool {
let first = matches!(self.cause, Some(CopyCause::First));
let moves = self.clear.is_none() && self.moves.is_none();
matches!(self.step, Step::Into) && first && moves && self.copies.stands_across()
}
/// Whether the copy is an encoder's first copy in of the bytes of a buffer of private or
/// memoryless storage the device made (an IOSurface plane's among them), or of the texels of
/// a tracked image of such storage the device made ([`Backing::PrivateImage`]), moving texels
/// (no clear): as `WorkCounts::shadow_texels_first_copied_in_private_buffers`,
/// `shadow_texels_first_copied_in_private_surfaces` and
/// `shadow_texels_first_copied_in_private_images` count it, and where it moves them, as
/// `shadow_texels_unskipped_*` count why (`shadow_standing::Unskipped`).
pub(super) fn first_from_private_truth(&self) -> bool {
let first = matches!(self.cause, Some(CopyCause::First));
let private = matches!(
self.copies.backing,
Backing::PrivateBuffer | Backing::PrivateSurface | Backing::PrivateImage
);
matches!(self.step, Step::Into) && first && self.clear.is_none() && private
}
/// The log of a copy in of every texel of a tracked 2D texture's image straight from a
/// buffer over host pages a consumer logs (`dirty_pages.rs`), made or kept by `device`, and
/// the bytes of one texel: what may decide the copy's regions as its batch is submitted
/// ([`GatedCopyIn`]).
/// `None` for every other copy.
pub(super) fn gate_log(&self, device: &Device) -> Option<(Arc<ShadowLog>, u64)> {
let c = &self.copies;
let first = matches!(self.cause, Some(CopyCause::First));
if !matches!(self.step, Step::Into) || !c.covers(self.area) || !first || !c.tracked {
return None;
}
// A copy taken at use moves every texel: the image holds what its commands wrote since
// the last, which no log of the bytes reports.
if c.levels.is_some() {
return None;
}
let ShadowSource::Bytes {
buffer: ShadowBuffer::Rebound(buffer),
target:
ShadowTarget::Image {
regions,
staging: None,
texel: Some(texel),
..
},
..
} = &c.source
else {
return None;
};
let buffer = buffer.upgrade()?;
let pages = buffer.host_pages()?;
let source = pages.dirty()?;
let contents = buffer.mapped_contents()?.as_ptr() as u64;
let (raw, at) = buffer.attached_at(0)?;
// The `VkBuffer`'s byte `v` lies at the host address `v + base`.
let base = contents.wrapping_sub(at);
let truth = copied_bytes(regions, *texel)?;
let log = device.shared().dirty.log(c, source, raw, base, truth)?;
Some((log, *texel))
}
/// The log of a copy back or merge of a shadow whose copies in a log decides, and the
/// `VkBuffer` bytes it writes (`dirty_pages.rs`): reported to the log as the shadow's own.
pub(super) fn written_log(&self, device: &Device) -> Option<(Arc<ShadowLog>, Range<u64>)> {
let c = &self.copies;
if !matches!(self.step, Step::Back | Step::Merge(_)) {
return None;
}
let ShadowSource::Bytes {
target: ShadowTarget::Image { regions, texel, .. },
..
} = &c.source
else {
return None;
};
let log = device.shared().dirty.kept(c)?;
// A merge cut to a rectangle writes inside its rows alone.
let bytes = match &self.step {
Step::Merge(merge) => merge.written_in_buffer(),
_ => copied_bytes(regions, (*texel)?)?,
};
Some((log, bytes))
}
/// The dispatch that takes this copy where it is a copy back that dispatches
/// (`shadow_copy_back.rs`): a one-slice 2D texture's image over a buffer's bytes, copied in
/// place, with its integer view, of texels the copy moves, made on `device` over the buffer's
/// `VkBuffer` now. `None` where the copy takes it (any other copy, or one the dispatch does
/// not fit).
pub(super) fn dispatched(&self, device: &Device) -> Option<CopyBack> {
let ShadowSource::Bytes {
buffer,
target:
ShadowTarget::Image {
regions,
staging: None,
texel: Some(texel),
words: Some(words),
..
},
..
} = &self.copies.source
else {
return None;
};
let ([region], Step::Back) = (regions.as_slice(), &self.step) else {
return None;
};
let region = match self.area {
Some(area) => part_of(region, area, *texel)?,
None => *region,
};
CopyBack::new(device, *words, buffer.raw(), &region)
}
/// The texels a copy in of a one-region, one-slice 2D texture's image over a buffer's bytes
/// copied in place moves, as `record` cuts its region (where they lie in the buffer, and in
/// the image): what a merge's keep may take with it (`ShadowMerge::keep_in`). `None` for any
/// other copy, or one that moves no texel.
pub(super) fn copied_region(&self) -> Option<vk::BufferImageCopy> {
let (
Step::Into,
ShadowSource::Bytes {
target:
ShadowTarget::Image {
regions,
staging: None,
texel: Some(texel),
..
},
..
},
) = (&self.step, &self.copies.source)
else {
return None;
};
let [region] = regions.as_slice() else {
return None;
};
match self.area {
Some(area) => part_of(region, area, *texel),
None => Some(*region),
}
}
/// The uses of the dispatch that takes a copy back ([`dispatched`](Self::dispatched)): a
/// sampled read of the shadow and a storage write of the buffer's bytes, in the compute
/// stage.
pub(super) fn dispatched_uses(&self) -> Vec<Use<ResourceKey>> {
let c = &self.copies;
let (truth, range) = c.truth();
vec![
Use::reads(
c.shadow_key,
c.tracked,
c.shadow_range.clone(),
Access::new(
vk::PipelineStageFlags2::COMPUTE_SHADER,
vk::AccessFlags2::SHADER_SAMPLED_READ,
),
),
Use::writes(
truth,
c.tracked,
range,
Access::new(
vk::PipelineStageFlags2::COMPUTE_SHADER,
vk::AccessFlags2::SHADER_STORAGE_WRITE,
),
),
]
}
/// The copy of rows that moves this copy back's rows from its staging buffer into the
/// buffer's bytes, made on `device` over the buffer's `VkBuffer` now (`row_copies.rs`): a
/// copy back through a staging buffer, of a texture over a buffer's bytes, a texture
/// buffer's, a rotated texture's, or a heap's two-plane or tiled texture's. `None` for any
/// other copy.
pub(super) fn staged_back(&self, device: &Device) -> Option<RowCopies> {
let (ShadowSource::Bytes { buffer, target, .. }, Step::Back) =
(&self.copies.source, &self.step)
else {
return None;
};
let staging = match target {
ShadowTarget::Image {
staging: Some(staging),
..
}
| ShadowTarget::Buffer(staging)
| ShadowTarget::Rotated { staging, .. }
| ShadowTarget::Planes { staging, .. }
| ShadowTarget::Tiled { staging, .. } => staging,
ShadowTarget::Image { staging: None, .. } => return None,
};
let back = staging.back.clone();
Some(RowCopies::new(device, staging.raw(), buffer.raw(), back))
}
/// The copy's stages where its staged rows go back by `back` ([`staged_back`]): with the
/// compute stage where they dispatch.
///
/// [`staged_back`]: Self::staged_back
pub(super) fn stages_with(&self, back: Option<&RowCopies>) -> vk::PipelineStageFlags2 {
match back {
Some(rows) if rows.dispatched() => self.stages() | rows.stages(),
_ => self.stages(),
}
}
/// The copy's uses where its staged rows go back by `back` ([`staged_back`]): where they
/// dispatch, the write of the buffer's bytes is the dispatch's, and so is the read of a
/// texture buffer's staging buffer, its shadow; an image's shadow is read by its copy into
/// the staging buffer.
///
/// [`staged_back`]: Self::staged_back
pub(super) fn uses_with(&self, back: Option<&RowCopies>) -> Vec<Use<ResourceKey>> {
let mut uses = self.uses();
let (Some(rows), Step::Back, ShadowSource::Bytes { target, .. }) =
(back, &self.step, &self.copies.source)
else {
return uses;
};
if !rows.dispatched() {
return uses;
}
let shadow_read = matches!(target, ShadowTarget::Buffer(_));
for used in &mut uses {
if !used.write.is_empty() {
used.write = rows.write();
} else if shadow_read && !used.read.is_empty() {
used.read = rows.read();
}
}
uses
}
/// The stages the copy runs in: a rotated texture's copies dispatch the rotation kernel, a
/// heap's two-plane texture's the two-plane kernel, a tiled heap texture's the tiled texel
/// kernel (its keep too), a keep that dispatches the copy back's kernel
/// (`ShadowMerge::dispatches`) that kernel, and a converted texture's copy in its refresh and
/// its conversion, dispatched through indirect arguments.
pub(super) fn stages(&self) -> vk::PipelineStageFlags2 {
let tiled = matches!(self.copies.target(), Some(ShadowTarget::Tiled { .. }));
let rotated = tiled
|| matches!(
self.copies.target(),
Some(ShadowTarget::Rotated { .. } | ShadowTarget::Planes { .. })
);
let converted = matches!(self.copies.source, ShadowSource::Converted { .. });
let encoded = matches!(self.copies.source, ShadowSource::YCbCrWrite { .. });
// A render shadow's copy in loads it through the conversion kernel (`ycbcr.rs`).
let loaded = matches!(
&self.copies.source,
ShadowSource::YCbCrWrite { write_back, .. } if write_back.loads()
);
if self.clear.is_some() {
return vk::PipelineStageFlags2::CLEAR;
}
match self.step {
Step::Merge(_) => {
vk::PipelineStageFlags2::TRANSFER | vk::PipelineStageFlags2::COMPUTE_SHADER
}
Step::Into | Step::Back if rotated => {
vk::PipelineStageFlags2::TRANSFER | vk::PipelineStageFlags2::COMPUTE_SHADER
}
Step::Keep(ref merge) if tiled || merge.dispatches() => {
vk::PipelineStageFlags2::TRANSFER | vk::PipelineStageFlags2::COMPUTE_SHADER
}
// The refresh's dispatch reads the conversion's indirect arguments (`ycbcr.rs`).
Step::Into if converted => {
vk::PipelineStageFlags2::TRANSFER
| vk::PipelineStageFlags2::DRAW_INDIRECT
| vk::PipelineStageFlags2::COMPUTE_SHADER
}
Step::Back if encoded => {
vk::PipelineStageFlags2::TRANSFER | vk::PipelineStageFlags2::COMPUTE_SHADER
}
Step::Into if loaded => {
vk::PipelineStageFlags2::TRANSFER | vk::PipelineStageFlags2::COMPUTE_SHADER
}
Step::Into | Step::Back | Step::Keep(_) => vk::PipelineStageFlags2::TRANSFER,
}
}
/// The copy's uses: a read of one side and a write of the other, in the transfer stage; a
/// keep's and a merge's as `ShadowMerge` gives them.
pub(super) fn uses(&self) -> Vec<Use<ResourceKey>> {
let c = &self.copies;
let read = Access::new(
vk::PipelineStageFlags2::COPY,
vk::AccessFlags2::TRANSFER_READ,
);
let write = Access::new(
vk::PipelineStageFlags2::COPY,
vk::AccessFlags2::TRANSFER_WRITE,
);
// A clear of the shadow's image: nothing of the truth is read.
if self.clear.is_some() {
let clear = Access::new(
vk::PipelineStageFlags2::CLEAR,
vk::AccessFlags2::TRANSFER_WRITE,
);
return vec![Use::writes(
c.shadow_key,
c.tracked,
c.shadow_range.clone(),
clear,
)];
}
let (truth, range) = c.truth();
if let ShadowSource::Converted { conversion, .. } = &c.source {
// The planes' bytes are copied (a whole conversion) or read by the refresh's
// dispatch (`ycbcr.rs`).
let read = Access::new(
vk::PipelineStageFlags2::COPY | vk::PipelineStageFlags2::COMPUTE_SHADER,
vk::AccessFlags2::TRANSFER_READ | vk::AccessFlags2::SHADER_STORAGE_READ,
);
return match &self.step {
Step::Into => conversion.uses(Use::reads(truth, c.tracked, range, read), c),
// Nothing writes the image: no copy back, keep or merge is made of it.
Step::Back | Step::Keep(_) | Step::Merge(_) => Vec::new(),
};
}
if let ShadowSource::YCbCrWrite { write_back, .. } = &c.source {
return match &self.step {
// A render shadow's load converts the planes into every texel.
Step::Into if write_back.loads() => {
write_back.load_uses(Use::reads(truth, c.tracked, range, read), c)
}
// A clear of every texel: nothing of the truth is read.
Step::Into => vec![Use::writes(
c.shadow_key,
c.tracked,
c.shadow_range.clone(),
Access::new(
vk::PipelineStageFlags2::CLEAR,
vk::AccessFlags2::TRANSFER_WRITE,
),
)],
Step::Back => write_back.uses(
Use {
key: truth,
tracked: c.tracked,
range,
read,
write,
},
c,
),
// No merge serves the planes (`unmerged`): no keep or merge is made of it.
Step::Keep(_) | Step::Merge(_) => Vec::new(),
};
}
match &self.step {
Step::Into => vec![
Use::reads(truth, c.tracked, range, read),
Use::writes(c.shadow_key, c.tracked, c.shadow_range.clone(), write),
],
Step::Back => vec![
Use::reads(c.shadow_key, c.tracked, c.shadow_range.clone(), read),
Use::writes(truth, c.tracked, range, write),
],
Step::Keep(merge) => merge.keep_uses(c),
Step::Merge(merge) => merge.merge_uses(c),
}
}
/// Whether this keep may copy the staging buffer its texture's copy in just filled
/// ([`ShadowCopies::keeps_from_staging`]): a keep whose kept part a copy writes.
pub(super) fn keeps_from_staging(&self) -> bool {
matches!(&self.step, Step::Keep(merge) if merge.copied_kept_part().is_some())
&& self.copies.keeps_from_staging()
}
/// Record this keep as a copy of its texture's staging buffer
/// ([`ShadowCopies::record_kept_from_staging`]), where it
/// [`keeps_from_staging`](Self::keeps_from_staging); any other copy as
/// [`record`](Self::record) records it.
///
/// # Safety
///
/// As `record`'s, right after its texture's copy in, with no copy of its shadow between.
pub(super) unsafe fn record_keep_from_staging(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
) {
if let Step::Keep(merge) = &self.step {
if let Some(to) = merge.copied_kept_part() {
// SAFETY: the caller's contract.
return unsafe {
merge.record_zero(device, command_buffer);
(self.copies).record_kept_from_staging(device, command_buffer, to)
};
}
}
// SAFETY: the caller's contract.
unsafe { self.record(device, command_buffer, None) }
}
/// Record the copy into `command_buffer`, its staged rows back (where it has them) by `back`
/// ([`staged_back`](Self::staged_back)).
///
/// # Safety
///
/// `command_buffer` is recording, outside a render pass, after a barrier that orders the copy
/// after what precedes it; the buffer and the shadow are live until it completes, and an
/// image is in the `GENERAL` layout; `back` is this copy's `staged_back`, over the buffer's
/// `VkBuffer` as it is now.
pub(super) unsafe fn record(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
back: Option<&RowCopies>,
) {
// SAFETY: the caller's.
unsafe { self.record_gated(device, command_buffer, back, None) }
}
/// [`record`](Self::record), a copy in of a 2D texture's image straight from the buffer
/// recorded under `gate` where given: its regions decided now, as its batch is submitted
/// ([`GatedCopyIn`], `dirty_pages.rs`).
///
/// # Safety
///
/// As [`record`](Self::record).
pub(super) unsafe fn record_gated(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
back: Option<&RowCopies>,
gate: Option<Arc<CopyInGate>>,
) {
let c = &self.copies;
if let (Some(value), Some(image)) = (&self.clear, c.cleared_image()) {
let range = vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
base_mip_level: 0,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
};
// SAFETY: the caller's contract; the shadow takes transfer writes, as every copy in
// writes it, and the range is its one level and slice ([`ShadowUse::cleared`]).
unsafe {
device.cmd_clear_color_image(
command_buffer,
image,
vk::ImageLayout::GENERAL,
value,
&[range],
);
}
return;
}
let moves = self.moves.as_deref();
let into_shadow = match &self.step {
Step::Into => true,
Step::Back => false,
// SAFETY: the caller's contract, which the merge's records take.
Step::Keep(merge) => {
return unsafe { merge.record_keep(device, command_buffer, (c, moves)) }
}
// SAFETY: as above.
Step::Merge(merge) => {
return unsafe { merge.record_merge(device, command_buffer, (c, moves)) }
}
};
let (buffer, target) = match &c.source {
ShadowSource::Bytes { buffer, target, .. } => (buffer.raw(), target),
ShadowSource::Converted {
buffer, conversion, ..
} => {
if into_shadow {
// SAFETY: the caller's contract, which the conversion's record takes.
unsafe { conversion.record(device, command_buffer, buffer.raw()) };
}
return;
}
ShadowSource::YCbCrWrite {
buffer,
image,
write_back,
..
} => {
if into_shadow && write_back.loads() {
// SAFETY: the caller's contract, which the load's record takes.
unsafe { write_back.record_load(device, command_buffer, buffer.raw()) };
} else if into_shadow {
let range = vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
base_mip_level: 0,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
};
let zero = vk::ClearColorValue { uint32: [0; 4] };
// SAFETY: the caller's contract; the shadow takes transfer writes (made so,
// `texture/ycbcr_image.rs`) and the range is its one level and slice.
unsafe {
device.cmd_clear_color_image(
command_buffer,
*image,
vk::ImageLayout::GENERAL,
&zero,
&[range],
);
}
} else {
// SAFETY: the caller's contract, which the write-back's record takes.
unsafe { write_back.record(device, command_buffer, buffer.raw(), self.area) };
}
return;
}
ShadowSource::Image {
image,
shadow,
regions,
through,
..
} => {
let (from, to) = if into_shadow {
(image.raw(), *shadow)
} else {
(*shadow, image.raw())
};
if let Some(through) = through {
let (buffer, _) = through.buffer.at(0);
// SAFETY: the caller's contract; the regions lie in both images, which have
// the same shape, and in the buffer, which lives with the shadow; the
// barrier orders the copy out of the buffer after the copy into it.
unsafe {
device.cmd_copy_image_to_buffer(
command_buffer,
from,
vk::ImageLayout::GENERAL,
buffer,
&through.regions,
);
Barrier::everything().record(device, command_buffer);
device.cmd_copy_buffer_to_image(
command_buffer,
buffer,
to,
vk::ImageLayout::GENERAL,
&through.regions,
);
}
return;
}
// A copy in cut to a rectangle moves its texels alone; a copy back moves every
// texel (`ShadowCopies::cuts_back`).
let regions: Vec<vk::ImageCopy> = if into_shadow {
match (self.area, c.cuts(), one_image_copy(regions)) {
(Some(area), true, Some(region)) => match image_part_of(region, area) {
Some(part) => vec![part],
// The command reads no texel.
None => return,
},
_ => regions.clone(),
}
} else {
regions
.iter()
.map(|region| vk::ImageCopy {
src_subresource: region.dst_subresource,
src_offset: region.dst_offset,
dst_subresource: region.src_subresource,
dst_offset: region.src_offset,
extent: region.extent,
})
.collect()
};
// SAFETY: the caller's contract; the regions lie in both images, which have
// the same shape and formats of one copy unit (planned when the view was made).
unsafe {
device.cmd_copy_image(
command_buffer,
from,
vk::ImageLayout::GENERAL,
to,
vk::ImageLayout::GENERAL,
&regions,
);
}
return;
}
};
let (image, regions, staging) = match target {
ShadowTarget::Image {
image,
staging,
texel,
..
} => {
// Going back takes the regions that write each byte once (the variant's `back`).
let regions = target.image_regions(into_shadow).unwrap_or_default();
let regions = match (self.area, staging, regions, *texel) {
(Some(area), None, [region], Some(texel)) => {
match part_of(region, area, texel) {
Some(part) => std::borrow::Cow::Owned(vec![part]),
// The commands read or wrote no texel.
None => return,
}
}
_ => std::borrow::Cow::Borrowed(regions),
};
(*image, regions, staging.as_ref())
}
ShadowTarget::Rotated {
image,
staging,
turn,
} => {
let turned = match (into_shadow, self.kept) {
(true, false) => Turn::Load,
(true, true) => Turn::Keep,
(false, _) => Turn::Store,
};
// SAFETY: the caller's contract, which the turn's record takes.
unsafe {
turn.record(
device,
command_buffer,
*image,
(buffer, staging),
turned,
back,
);
}
return;
}
ShadowTarget::Planes {
image,
staging,
planes,
..
} => {
// SAFETY: the caller's contract, which the planes' record takes.
unsafe {
planes.record(
device,
command_buffer,
*image,
(buffer, staging),
into_shadow,
back,
);
}
return;
}
ShadowTarget::Tiled {
image,
staging,
tiles,
} => {
// SAFETY: the caller's contract, which the tiles' record takes; the moves are held
// with the copy.
unsafe {
tiles.record(
device,
command_buffer,
*image,
(buffer, staging),
(into_shadow, moves),
back,
);
}
return;
}
ShadowTarget::Buffer(staging) => {
// SAFETY: the caller's contract; the copies lie in both buffers (planned when
// the texture was made), and the staging buffer lives with the texture. A tail
// wholly past the buffer's length takes none of its bytes.
unsafe {
if into_shadow {
if staging.into.size > 0 {
device.cmd_copy_buffer(
command_buffer,
buffer,
staging.raw(),
&[staging.into],
);
}
} else {
record_back(device, command_buffer, (staging, buffer), back);
}
}
return;
}
};
let texels = staging.map_or(buffer, ShadowStaging::raw);
// SAFETY: the caller's contract; the regions lie in the buffers and the image (checked
// when the texture was made), and the staging buffer lives with the texture.
unsafe {
if into_shadow {
// A texture whose texels all lie past its buffer's length copies none of the
// buffer's bytes into its tail (`buffer_texture.rs`).
if let Some(staging) = staging.filter(|staging| staging.into.size > 0) {
device.cmd_copy_buffer(command_buffer, buffer, texels, &[staging.into]);
Barrier::everything().record(device, command_buffer);
}
if let Some(gate) = gate.filter(|_| staging.is_none()) {
let mut chosen = Vec::new();
gate.regions(&regions, &mut chosen);
if !chosen.is_empty() {
device.cmd_copy_buffer_to_image(
command_buffer,
texels,
image,
vk::ImageLayout::GENERAL,
&chosen,
);
}
return;
}
device.cmd_copy_buffer_to_image(
command_buffer,
texels,
image,
vk::ImageLayout::GENERAL,
&regions,
);
// A copy taken at use makes its later levels from the first it just copied.
if let Some(levels) = &c.levels {
levels.record_levels(device, command_buffer, image, self.later.as_deref());
}
} else {
device.cmd_copy_image_to_buffer(
command_buffer,
image,
vk::ImageLayout::GENERAL,
texels,
&regions,
);
if let Some(staging) = staging {
Barrier::everything().record(device, command_buffer);
record_back(device, command_buffer, (staging, buffer), back);
}
}
}
}
}
/// The `VkBuffer` bytes `regions` copy, `texel` bytes a texel, from the first region's first byte
/// to the last row's end; `None` for none, or a region of depth.
fn copied_bytes(regions: &[vk::BufferImageCopy], texel: u64) -> Option<Range<u64>> {
let mut bytes: Option<Range<u64>> = None;
for region in regions {
let extent = region.image_extent;
if extent.depth > 1 || extent.width == 0 || extent.height == 0 {
return None;
}
let row = match region.buffer_row_length {
0 => extent.width,
row => row,
};
let pitch = u64::from(row) * texel;
let start = region.buffer_offset;
let end = start + u64::from(extent.height - 1) * pitch + u64::from(extent.width) * texel;
bytes = Some(match bytes {
Some(b) => b.start.min(start)..b.end.max(end),
None => start..end,
});
}
bytes
}
/// `region` (a one-level, one-slice texture's every texel, `texel` bytes each in the buffer) cut
/// to the texels inside `[x0, y0, x1, y1)`: `None` where none lies there.
pub(super) fn part_of(
region: &vk::BufferImageCopy,
[x0, y0, x1, y1]: [u32; 4],
texel: u64,
) -> Option<vk::BufferImageCopy> {
let extent = region.image_extent;
let (x1, y1) = (x1.min(extent.width), y1.min(extent.height));
if x0 >= x1 || y0 >= y1 {
return None;
}
let row = match region.buffer_row_length {
0 => extent.width,
row => row,
};
Some(vk::BufferImageCopy {
buffer_offset: region.buffer_offset
+ (u64::from(y0) * u64::from(row) + u64::from(x0)) * texel,
buffer_row_length: row,
image_offset: vk::Offset3D {
x: region.image_offset.x + x0 as i32,
y: region.image_offset.y + y0 as i32,
z: region.image_offset.z,
},
image_extent: vk::Extent3D {
width: x1 - x0,
height: y1 - y0,
depth: extent.depth,
},
..*region
})
}
/// Record the rows of `staging` back into `buffer`: by `back` where the caller made it
/// ([`ShadowCopy::staged_back`]), else as one copy of every row. Rotated textures' and heap
/// textures' copies back end so too (`rotated.rs`, `texture/heap_two_planes.rs`,
/// `texture/heap_tiled.rs`).
///
/// # Safety
///
/// As [`ShadowCopy::record`], after a barrier from the staging buffer's writes.
pub(super) unsafe fn record_back(
device: &Vk,
command_buffer: vk::CommandBuffer,
(staging, buffer): (&ShadowStaging, vk::Buffer),
back: Option<&RowCopies>,
) {
// SAFETY: the caller's contract; the rows lie in the staging buffer and the buffer's bytes
// (planned when the texture was made), and the staging buffer lives with the texture.
unsafe {
match back {
Some(rows) => rows.record(device, command_buffer),
None if staging.back.is_empty() => {}
None => device.cmd_copy_buffer(command_buffer, staging.raw(), buffer, &staging.back),
}
}
}
/// The one region of `regions`, a shadow's of another image, where it is one level and one slice
/// of a 2D image: the texels a rectangle of the texture's own names.
fn one_image_copy(regions: &[vk::ImageCopy]) -> Option<&vk::ImageCopy> {
match regions {
[region] if region.src_subresource.layer_count == 1 && region.extent.depth == 1 => {
Some(region)
}
_ => None,
}
}
/// `region` (one level and one slice of an image, copied into the same texels of its shadow) cut
/// to the texels inside `[x0, y0, x1, y1)`: `None` where none lies there.
pub(super) fn image_part_of(
region: &vk::ImageCopy,
[x0, y0, x1, y1]: [u32; 4],
) -> Option<vk::ImageCopy> {
let extent = region.extent;
let (x1, y1) = (x1.min(extent.width), y1.min(extent.height));
if x0 >= x1 || y0 >= y1 {
return None;
}
let moved = |offset: vk::Offset3D| vk::Offset3D {
x: offset.x + x0 as i32,
y: offset.y + y0 as i32,
z: offset.z,
};
Some(vk::ImageCopy {
src_offset: moved(region.src_offset),
dst_offset: moved(region.dst_offset),
extent: vk::Extent3D {
width: x1 - x0,
height: y1 - y0,
depth: extent.depth,
},
..*region
})
}
/// Whether the box `inner` (an offset and extent of texels) lies inside the box `outer`.
fn within(
(offset, extent): (vk::Offset3D, vk::Extent3D),
(at, size): (vk::Offset3D, vk::Extent3D),
) -> bool {
let axes = [
(offset.x, extent.width, at.x, size.width),
(offset.y, extent.height, at.y, size.height),
(offset.z, extent.depth, at.z, size.depth),
];
axes.into_iter()
.all(|(start, length, outer, outer_length)| {
let (start, outer) = (i64::from(start), i64::from(outer));
outer <= start && start + i64::from(length) <= outer + i64::from(outer_length)
})
}
/// A box of an image's texels a command writes every texel of: a blit's copy region into the
/// image, in its aspects, level and slices.
#[derive(Clone, Copy)]
pub(super) struct Covered {
pub(super) image: vk::Image,
pub(super) subresource: vk::ImageSubresourceLayers,
pub(super) offset: vk::Offset3D,
pub(super) extent: vk::Extent3D,
}
impl Covered {
/// The boxes `regions` of copies into `image` write.
pub(super) fn into_image(image: vk::Image, regions: &[vk::BufferImageCopy]) -> Vec<Covered> {
(regions.iter())
.map(|region| Covered {
image,
subresource: region.image_subresource,
offset: region.image_offset,
extent: region.image_extent,
})
.collect()
}
/// The boxes `regions` of copies into the image `to` write.
pub(super) fn into_image_from_image(to: vk::Image, regions: &[vk::ImageCopy]) -> Vec<Covered> {
(regions.iter())
.map(|region| Covered {
image: to,
subresource: region.dst_subresource,
offset: region.dst_offset,
extent: region.extent,
})
.collect()
}
}
/// Mark the shadows of `shadows` a command with `uses` replaces ([`ShadowUse::overwrites`]): it
/// writes through the shadow, reads none of its texels, and writes every texel it holds inside
/// the boxes `covered` ([`ShadowCopies::covered_by`]), so it takes no copy in and no keep, and
/// its copy back moves the texels it wrote; but over logged host pages, a copy in, a keep and a
/// merge ([`overwrites_unkept`]).
pub(super) fn overwritten(
shadows: &mut [ShadowUse],
uses: &[Use<ResourceKey>],
covered: &[Covered],
) {
if covered.is_empty() {
return;
}
for shadow in shadows {
let key = shadow.copies.shadow_key;
let reads = (uses.iter()).any(|used| used.key == key && !used.read.is_empty());
shadow.overwrites = shadow.writes && !reads && shadow.copies.covered_by(covered);
}
}
/// Whether a command writes through `shadow` without replacing every texel, where it merges back
/// ([`ShadowCopies::merges_back`]): kept and merged back (the module's rules), as a command that
/// writes them two ways too ([`two_ways`]) is.
fn merged_back(shadow: &ShadowUse) -> bool {
shadow.writes && !overwrites_unkept(shadow) && shadow.copies.merges_back()
}
/// Whether the merge after a command that writes through `shadow` and merges back, keeping the
/// texels inside `kept` (`None` every texel), stores whole blocks where it is cut to the texels'
/// rows (`shadow_merge.rs`): the command replaces every texel ([`ShadowUse::overwrites`], over
/// logged pages, as [`merged_back`] takes it), or writes through it only by a render pass's
/// stores of the tiles of its area ([`ShadowUse::tile_stores`]) and the keep holds that area (it
/// is that area, or the rectangle bounding it and the areas of the passes of its run before it,
/// `pass_runs.rs`, each of which wrote through the texture by such stores alone).
fn stores_blocks(shadow: &ShadowUse, kept: Option<[u32; 4]>) -> bool {
let kept_area = kept.is_some() && shadow.area.is_some() && holds(kept, shadow.area);
shadow.overwrites || (shadow.tile_stores && kept_area)
}
/// Whether a command that replaces every texel of `shadow` ([`ShadowUse::overwrites`]) takes no
/// copy in and no keep, its copy back a plain copy of the texels it wrote: every such shadow but
/// one over logged host pages ([`ShadowCopies::logged`]), which is copied in, kept and merged back
/// as a command that leaves texels in place is (the module's **Overwrites over logged pages**).
fn overwrites_unkept(shadow: &ShadowUse) -> bool {
shadow.overwrites && !shadow.copies.logged()
}
/// Whether a command that uses the shadowed textures `shadows` and has `uses` writes the bytes
/// of `shadows[i]` two ways: through it, and through `uses` or another shadowed texture over
/// them (the module's rules).
fn two_ways(shadows: &[ShadowUse], i: usize, uses: &[Use<ResourceKey>]) -> bool {
// A shadow whose writes Metal drops, or a copy taken at use, writes the bytes no way.
let writes = |shadow: &ShadowUse| shadow.writes && shadow.copies.writes_reach_truth();
let Some(shadow) = shadows.get(i).filter(|shadow| writes(shadow)) else {
return false;
};
let bytes_written = uses
.iter()
.any(|used| !used.write.is_empty() && shadow.copies.meets(used));
let shadow_written = shadows.iter().enumerate().any(|(j, other)| {
j != i
&& writes(other)
&& other.copies.shadow_key != shadow.copies.shadow_key
&& other.copies.shares_truth(&shadow.copies)
});
bytes_written || shadow_written
}
/// Whether a command that uses the shadowed textures `shadows` and has `uses` writes the truth of
/// `shadow` apart from it: through `uses` (a render pass's color attachment over the bytes, which
/// its image renders in place, as the pass of a draw that reads the attachment's shadow does,
/// `texture/attached_reads.rs`), or through another shadowed texture over them. After such a
/// command the image holds the truth whole only where a later copy in takes again what it wrote,
/// so a first copy in of every texel for the image's standing across command buffers
/// (`shadow_standing.rs`) buys nothing: the copy moves what the command needs.
fn writes_truth_apart(
shadows: &[ShadowUse],
shadow: &ShadowUse,
uses: &[Use<ResourceKey>],
) -> bool {
let bytes_written = uses
.iter()
.any(|used| !used.write.is_empty() && shadow.copies.meets(used));
let shadow_written = shadows.iter().any(|other| {
other.writes
&& other.copies.writes_reach_truth()
&& other.copies.shadow_key != shadow.copies.shadow_key
&& other.copies.shares_truth(&shadow.copies)
});
bytes_written || shadow_written
}
/// The shadowed textures among `textures` (bound, made resident, or a pass's) a command with
/// `uses` uses, each once, writing through it when any of its uses writes.
pub(super) fn shadow_uses<'a>(
textures: impl Iterator<Item = &'a Texture>,
uses: &[Use<ResourceKey>],
) -> Vec<ShadowUse> {
shadow_uses_of(textures, &[], uses)
}
/// [`shadow_uses`] of `bound`, then of the textures whose copies `resident` lists (a
/// [`ResidentShadows`]'), in that order.
pub(super) fn shadow_uses_of<'a>(
bound: impl Iterator<Item = &'a Texture>,
resident: &[Arc<ShadowCopies>],
uses: &[Use<ResourceKey>],
) -> Vec<ShadowUse> {
let mut copies = Vec::new();
for texture in bound {
texture_copies(texture, &mut copies);
}
copies_used(copies.iter().chain(resident), uses)
}
/// A texture's own shadow's copies, and those of the one a binding beyond its usage made
/// (`texture/usage_shadow.rs`; a texture with faces has its attached reads' beside it,
/// `texture/faces.rs`), added to `copies`.
fn texture_copies(texture: &Texture, copies: &mut Vec<Arc<ShadowCopies>>) {
copies.extend(texture.shadow().cloned());
copies.extend(texture.usage_shadow_copies());
copies.extend(texture.usage_shadow_copies_beside());
}
/// The shadows' copies of the textures a residency made resident ([`texture_copies`] of each, in
/// its textures' order), kept by an encoder between its commands (a compute encoder's dispatches,
/// `compute/live.rs`; a render encoder's draws, `live_bind.rs`) and listed again only where the
/// residency's generation (`Residency::generation`), or the count of usage shadows its device
/// made (`DeviceShared::usage_shadows_made`), moved since: a texture's own shadow is made with
/// it, and a usage shadow is the one a texture makes later. A command's look at them
/// ([`shadow_uses_of`]) then costs their count, not the resident textures'.
#[derive(Default)]
pub(super) struct ResidentShadows {
listed_at: Option<(u64, u64)>,
copies: Vec<Arc<ShadowCopies>>,
}
impl ResidentShadows {
/// The copies of what `residency`, of `device`, made resident, listed again where they may
/// have changed.
pub(super) fn of(&mut self, device: &Device, residency: &Residency) -> &[Arc<ShadowCopies>] {
// Read before the textures: a shadow made meanwhile is counted once it can be read, so
// the next look lists it.
let made = (device.shared().usage_shadows_made).load(Ordering::Acquire);
let at = (residency.generation(), made);
if self.listed_at != Some(at) {
self.copies.clear();
for texture in residency.textures() {
texture_copies(texture, &mut self.copies);
}
self.listed_at = Some(at);
device.shared().counts.resident_textures_looked_at();
}
&self.copies
}
/// Hold none: an ended encoder's, kept for the next (`compute/live.rs`, `live_bind.rs`).
pub(super) fn let_go(&mut self) {
self.listed_at = None;
self.copies.clear();
}
}
/// The shadowed textures of `shadows` a command with `uses` uses where they hold a copy of their
/// truth ([`ShadowCopies::holds`]), each once, writing through it when any of those uses writes.
pub(super) fn copies_used<'a>(
shadows_of: impl Iterator<Item = &'a Arc<ShadowCopies>>,
uses: &[Use<ResourceKey>],
) -> Vec<ShadowUse> {
let mut shadows: Vec<ShadowUse> = Vec::new();
for copies in shadows_of {
if shadows
.iter()
.any(|shadow| shadow.copies.shadow_key == copies.shadow_key)
{
continue;
}
let mut used = uses.iter().filter(|used| copies.holds(used));
let Some(first) = used.next() else {
continue;
};
let writes = !first.write.is_empty() || used.any(|other| !other.write.is_empty());
shadows.push(ShadowUse {
copies: Arc::clone(copies),
writes,
overwrites: false,
kept: false,
area: None,
loads: None,
tile_stores: false,
cleared: None,
});
}
shadows
}
/// Add `shadow` to `shadows`: once per shadowed texture, writing through it when either use
/// writes (where both write, inside the rectangle bounding both areas, and by tile stores alone
/// only where both do), reading its texels inside the rectangle bounding where both read them
/// (anywhere where either does not bound that), and overwriting it, or leaving its texels in
/// place, only when both do.
pub(super) fn join(shadows: &mut Vec<ShadowUse>, shadow: &ShadowUse) {
match shadows
.iter_mut()
.find(|other| other.copies.shadow_key == shadow.copies.shadow_key)
{
Some(other) => {
other.area = match (other.writes, shadow.writes) {
(true, true) => bounding(other.area, shadow.area),
(false, true) => shadow.area,
(_, false) => other.area,
};
other.tile_stores = match (other.writes, shadow.writes) {
(true, true) => other.tile_stores && shadow.tile_stores,
(false, true) => shadow.tile_stores,
(_, false) => other.tile_stores,
};
other.loads = bounding(other.loads, shadow.loads);
other.writes |= shadow.writes;
other.overwrites &= shadow.overwrites;
other.kept &= shadow.kept;
other.cleared = other.cleared.or(shadow.cleared);
}
None => shadows.push(ShadowUse {
copies: Arc::clone(&shadow.copies),
writes: shadow.writes,
overwrites: shadow.overwrites,
kept: shadow.kept,
area: shadow.area,
loads: shadow.loads,
tile_stores: shadow.tile_stores,
cleared: shadow.cleared,
}),
}
}
/// Where a shadowed texture's image stands against its truth in an encoder.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Standing {
/// Not copied in since the encoder met the texture: its next copy in is the encoder's first.
Unread,
/// The image holds the buffer's bytes as the encoder's commands so far left them, inside
/// `held`.
Current,
/// The truth changed since the image's copy in, for the reason given.
Lost(Lost),
}
/// A shadowed texture an encoder used, and where its image stands.
struct Entry<M> {
copies: Arc<ShadowCopies>,
standing: Standing,
/// Where the image holds them when [`Standing::Current`]: the texels inside `[x0, y0, x1, y1)`, all its
/// last copy in moved ([`ShadowUse::loads`]); `None` for every texel. Its other texels are
/// whatever the image held before, which no command of the encoder reads and no copy back
/// moves: a command that needs them current copies back what was written and copies every
/// texel in first.
held: Option<[u32; 4]>,
/// A command wrote through the texture since the last copy back.
dirty: bool,
/// Where those commands wrote through it, where they bound it ([`ShadowUse::area`]).
area: Option<[u32; 4]>,
/// The scratch the image was kept in before a command that writes its bytes two ways, until
/// the merge after it, or before the first command since the last copy back that wrote
/// through a texture that merges back ([`ShadowCopies::merges_back`]), until the next copy
/// back, which is then a merge.
merge: Option<M>,
/// The keep in `merge` is a command's that writes the bytes two ways: merged before the
/// encoder's next command, after which the image no longer holds the bytes.
two_ways: bool,
/// The scratch of the texture's keeps where it merges back, made at the first
/// command of the encoder that writes through it, and kept in again at each keep after a
/// merge: each merge's dispatches end with a barrier to everything after them.
scratch: Option<M>,
/// Taken from the command buffer's carry and not used by a command of this encoder yet: its
/// first use's copy in is the one the carry takes off the run
/// (`WorkCounts::shadow_texels_first_copy_in_carried`).
carried: bool,
/// Its image goes on from a run of passes before this encoder's, kept in `merge` before the
/// run's first ([`ShadowSync::go_on`], `pass_runs.rs`): a command that writes outside what
/// that keep holds keeps every texel of the texture next, and one that needs texels beyond
/// what the image holds copies every texel in, so the run's later passes write inside the
/// keep and read inside the image.
run: bool,
/// When [`Standing::Current`], the rectangle bounding the texels whose bytes changed since the image's
/// copy in, where only writes [`Within`] it changed them (the module's rules): the next
/// command that uses the texture copies those in first, or those of them it reads where it
/// reads only inside a rectangle and writes none, the rest left here. `None` where none
/// changed.
stale: Option<[u32; 4]>,
/// The number of the take of the copy at use of a texture whose shadow is one
/// ([`ShadowCopies::levels`]) the encoder's use took by the mark ([`LevelsCopy::take`]):
/// given back where what it recorded is left out ([`ShadowSync::forget`]). `None` where it
/// took none so, an encoding again's take among them.
took: Option<u64>,
}
impl<M> Entry<M> {
/// Whether a change of the bytes of `texels`' texels inside a rectangle leaves the image
/// current but there ([`Entry::stale`]): it holds the bytes, and no write of its own or keep,
/// and its texels are `texels`' ([`ShadowCopies::same_texels`]).
fn stale_within(&self, texels: &ShadowCopies) -> bool {
self.standing == Standing::Current
&& !self.dirty
&& self.merge.is_none()
&& self.copies.same_texels(texels)
}
/// Note a change of the truth for `lost`: the image no longer holds it. One not copied in
/// yet stays so, and one that lost it already keeps the first reason. A copy taken at use
/// still holds what it took: no change of the truth reaches it.
fn lose(&mut self, lost: Lost) {
if self.standing == Standing::Current && self.copies.levels.is_none() {
self.standing = Standing::Lost(lost);
}
}
/// Note a change of the bytes of `texels`' texels inside `area` (`[x0, y0, x1, y1)`), or
/// anywhere where `area` is `None`, for `lost`: the image is stale there, or no longer
/// current.
fn changed(&mut self, texels: &ShadowCopies, area: Option<[u32; 4]>, lost: Lost) {
if self.copies.levels.is_some() {
return;
}
match area.filter(|_| self.stale_within(texels)) {
Some(area) => {
self.stale = Some(match self.stale {
Some(stale) => [
stale[0].min(area[0]),
stale[1].min(area[1]),
stale[2].max(area[2]),
stale[3].max(area[3]),
],
None => area,
});
}
None => self.lose(lost),
}
}
}
/// A command's writes of a texture's bytes that change none of its texels outside a rectangle:
/// a render pass instance's of a color attachment it loads, which only its draws change, inside
/// their scissors (`render/shadows.rs`). It borrows what it names, of the pass's writes.
#[derive(Clone, Copy)]
pub(super) struct Within<'a> {
/// The writes' uses of the bytes.
pub(super) uses: &'a [Use<ResourceKey>],
/// A shadow of the texture, whose texels are the attachment's pixels: an image whose texels
/// are laid out as its ([`ShadowCopies::same_texels`]) changed inside `area` alone. `None`
/// where the texture has none, and then every image over the bytes changed anywhere.
pub(super) texels: Option<&'a Arc<ShadowCopies>>,
/// The rectangle, `[x0, y0, x1, y1)` of the texels.
pub(super) area: [u32; 4],
}
/// A command's use of a shadowed texture: its copies, and whether it writes through it.
pub(super) struct ShadowUse {
pub(super) copies: Arc<ShadowCopies>,
pub(super) writes: bool,
/// The command replaces every texel of the texture before it reads any (a render pass's
/// attachment that clears each of its aspects over the whole of a one-level, one-slice
/// texture; a blit's copy into every texel of such a texture over a buffer's bytes,
/// [`overwritten`]): it takes no copy in and no keep. A render encoder's one pass is its only
/// command with attachments, so nothing earlier in the encoder wrote through the texture;
/// where the pass does not store it either, nothing is copied back, and the buffer keeps its
/// bytes, Metal's answer (a Metal probe on the M3 Max and an M4).
pub(super) overwrites: bool,
/// The command leaves the texture's texels in place, but where it overwrites them: a render
/// pass that draws nothing, of an attachment it renders. A rotated texture's copy in then
/// turns its texels by the inverse of its store (`rotated::Turn::Keep`), where Metal's pass
/// loads them otherwise. (An attachment such a pass loads, whose shadow is an image of a
/// buffer's bytes, takes no use at all: `render/shadows.rs`.)
pub(super) kept: bool,
/// Where the command writes through the texture, where it bounds that: the texels inside
/// `[x0, y0, x1, y1)` of a one-level, one-slice texture's image (a pass's attachment it
/// loads, which only its draws' scissors change, `render/shadows.rs`); `None` for anywhere.
pub(super) area: Option<[u32; 4]>,
/// Where the command reads the texture's texels, where it bounds that: the texels inside
/// `[x0, y0, x1, y1)` of a one-level, one-slice texture's image, whose others it reads at
/// most into the image alone, never into what it writes elsewhere or what a copy back moves
/// (a pass's attachment it loads and takes in no other way, whose fragments run only inside
/// its draws' scissors and read the attachment at their own pixel alone,
/// `render/shadows.rs`); `None` for anywhere. Its copy in moves only those texels.
pub(super) loads: Option<[u32; 4]>,
/// The command writes through the texture only as a render pass's color attachment it loads
/// and stores inside `area` (`render/shadows.rs`): Metal's own store of such a pass writes
/// back every texel of each tile its draws reach, those no fragment changed among them, so
/// its merge stores whole blocks of the rectangle's rows (the module's **Stored
/// attachments**).
pub(super) tile_stores: bool,
/// The command reads the texture's texels only as the clear a render pass's load action
/// writes over all of them ahead of every read: the first instance of a pass whose draws
/// sample a color it clears, the whole of a one-level, one-slice texture, after a memory
/// barrier before the pass's first draw and before any draw of it
/// (`texture/attached_reads.rs`). Its copy in is a clear of the shadow's image to this value
/// ([`ShadowCopy::clear`]), whatever the image held, after which the image holds the truth
/// nowhere, so its next use copies in again.
pub(super) cleared: Option<vk::ClearColorValue>,
}
/// A draw's use of a shadowed texture as a kept draw holds it (`render/draw_again.rs`): the
/// shadow, by its key, whether the draw writes through it, and where it bounds its writes and
/// reads, naming no object. A draw's use sets no other field ([`copies_used`], and the bounds of
/// its reads at its own pixels, `render.rs`'s `bound_own_pixel_reads`). Two uses so equal join as
/// one ([`join`]): a draw that uses the shadows a kept draw used, each as it used it, into the
/// kept draw's instance, gathers nothing the kept draw did not.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct UsedAs {
shadow: ResourceKey,
writes: bool,
area: Option<[u32; 4]>,
loads: Option<[u32; 4]>,
}
impl ShadowUse {
/// A draw's use as a kept draw holds it ([`UsedAs`]).
pub(super) fn used_as(&self) -> UsedAs {
debug_assert!(
!self.overwrites && !self.kept && !self.tile_stores && self.cleared.is_none(),
"a draw's use of a shadowed texture set a field of a pass's attachment"
);
UsedAs {
shadow: self.copies.shadow_key,
writes: self.writes,
area: self.area,
loads: self.loads,
}
}
}
/// The rectangle bounding `a` and `b`, each `[x0, y0, x1, y1)`; `None` (anywhere) where either is.
fn bounding(a: Option<[u32; 4]>, b: Option<[u32; 4]>) -> Option<[u32; 4]> {
let ([a0, a1, a2, a3], [b0, b1, b2, b3]) = (a?, b?);
Some([a0.min(b0), a1.min(b1), a2.max(b2), a3.max(b3)])
}
/// The texels inside both `a` and `b`, each `[x0, y0, x1, y1)`; `None` where they share none.
fn meet([a0, a1, a2, a3]: [u32; 4], [b0, b1, b2, b3]: [u32; 4]) -> Option<[u32; 4]> {
let r = [a0.max(b0), a1.max(b1), a2.min(b2), a3.min(b3)];
(r[0] < r[2] && r[1] < r[3]).then_some(r)
}
/// The rectangle bounding the texels of `a` outside `b`, each `[x0, y0, x1, y1)`; `None` where `b`
/// covers `a`.
fn outside(a: [u32; 4], b: [u32; 4]) -> Option<[u32; 4]> {
let ([a0, a1, a2, a3], [b0, b1, b2, b3]) = (a, b);
if meet(a, b).is_none() {
return Some(a);
}
let (across, down) = (b0 <= a0 && a2 <= b2, b1 <= a1 && a3 <= b3);
// Where `b` crosses `a` from side to side, the texels left are the rows above and below it;
// from top to bottom, the columns beside it. One of the two alone is a rectangle of its own.
let left = match (across, down) {
(true, true) => return None,
(true, false) => (a1 < b1, b3 < a3, [a0, a1, a2, b1], [a0, b3, a2, a3]),
(false, true) => (a0 < b0, b2 < a2, [a0, a1, b0, a3], [b2, a1, a2, a3]),
(false, false) => return Some(a),
};
match left {
(true, false, before, _) => Some(before),
(false, true, _, after) => Some(after),
_ => Some(a),
}
}
/// Whether an image holding the buffer's texels inside `held` (`None` every texel) holds those
/// inside `needs` (`None` every texel).
fn holds(held: Option<[u32; 4]>, needs: Option<[u32; 4]>) -> bool {
match (held, needs) {
(None, _) => true,
(Some(_), None) => false,
(Some([h0, h1, h2, h3]), Some([n0, n1, n2, n3])) => {
h0 <= n0 && h1 <= n1 && n2 <= h2 && n3 <= h3
}
}
}
/// The shadowed textures one encoder used, and the copies they need (the module's rules), `M`
/// the scratch of a merge.
pub(super) struct ShadowSync<M = Arc<ShadowMerge>> {
entries: Vec<Entry<M>>,
/// What the command buffer's earlier encoders left holding the truth, for the textures this
/// one has not met yet ([`ShadowCarry`]); [`entry`](Self::entry) takes a texture's out of it.
carry: ShadowCarry,
/// How the encoder's first copies in so far stood against their images' last standing, which
/// [`tally`](Self::tally) hands to the counters.
tally: CarryTally,
/// How its first uses of a copy taken at use decide whether they take it ([`TakeBy`]).
takes: TakeBy,
}
impl<M> Default for ShadowSync<M> {
/// No texture used yet, and nothing carried: every texture copies in at its first use.
fn default() -> Self {
ShadowSync::carrying(ShadowCarry::default())
}
}
/// What an encoder's first uses of textures whose shadows are copies taken at use
/// ([`ShadowCopies::levels`]) decided, in the order it met them: each texture's shadow key, and
/// the take where that use took the copy. A texture's first use in an encoder decides, and so
/// does its first use after work the encoder records apart ([`ShadowSync::leave`]).
#[derive(Clone, Debug, Default)]
pub(super) struct Takes(Vec<(ResourceKey, Option<Took>)>);
/// A take of a copy taken at use: the texture's copies, and the take's number
/// ([`LevelsCopy::take`]).
#[derive(Clone, Debug)]
pub(super) struct Took {
copies: Weak<ShadowCopies>,
number: u64,
}
impl Takes {
/// None decided: an encoder that met no copy taken at use.
pub(super) fn clear(&mut self) {
self.0.clear();
}
/// `other`'s, decided after these, joined after them.
pub(super) fn join(&mut self, other: Takes) {
self.0.extend(other.0);
}
/// Give back every take: the recording that carries them never runs (`State::fail_part`).
/// Each texture's next use takes the copy again, where its take here is still its latest
/// ([`LevelsCopy::give_back`]).
pub(super) fn give_back(&self) {
for took in self.0.iter().filter_map(|(_, took)| took.as_ref()) {
took.give_back();
}
}
/// Give back every take that `held` does not hold: these are a recording's that `held`'s
/// replaces, which never runs, and `held`'s recording kept the takes it met
/// ([`ShadowSync::over`]).
pub(super) fn give_back_unheld(&self, held: &Takes) {
let holds = |took: &Took| {
(held.0.iter().filter_map(|(_, other)| other.as_ref()))
.any(|other| other.number == took.number && other.copies.ptr_eq(&took.copies))
};
for took in self.0.iter().filter_map(|(_, took)| took.as_ref()) {
if !holds(took) {
took.give_back();
}
}
}
}
impl Took {
/// Give the take back, where it is still its texture's latest ([`LevelsCopy::give_back`]).
fn give_back(&self) {
let copies = self.copies.upgrade();
if let Some(levels) = copies.as_ref().and_then(|copies| copies.levels.as_ref()) {
levels.give_back(self.number);
}
}
}
/// How an encoder's first uses of a copy taken at use decide whether they take it.
enum TakeBy {
/// As the program encodes it: where the texture's mark asks ([`LevelsCopy::take`], which
/// clears it), each decision noted. Recorded in place of a recording that decided `replaced`
/// ([`ShadowSync::over`]), a use that one met decides as it did, keeping its take, and
/// noted so; only the others ask the mark.
Mark { replaced: Takes, noted: Takes },
/// Encoded again (`command_buffer/reencode.rs`): as the use in its place in the first
/// encoding decided, the first encoding's decisions not met yet held here. The first
/// recording is never submitted, so its take is this one's; the mark is left as it is found,
/// for the next use encoded after a later `Texture::did_modify_data`.
Again(Takes),
}
impl TakeBy {
/// Whether this first use in the encoder of `copies`, whose copy is `levels`, takes the copy,
/// and the take's number where it takes it by the mark or keeps the take of the recording it
/// replaces (none for an encoding again's).
fn take(&mut self, copies: &Arc<ShadowCopies>, levels: &LevelsCopy) -> (bool, Option<u64>) {
let key = copies.shadow_key;
match self {
TakeBy::Mark { replaced, noted } => {
let took = match replaced.0.iter().position(|(met, _)| *met == key) {
Some(at) => replaced.0.remove(at).1,
None => levels.take().map(|number| Took {
copies: Arc::downgrade(copies),
number,
}),
};
let number = took.as_ref().map(|took| took.number);
noted.0.push((key, took));
(number.is_some(), number)
}
// A use the first encoding did not meet takes nothing, as it did not.
TakeBy::Again(first) => match first.0.iter().position(|(met, _)| *met == key) {
Some(at) => (first.0.remove(at).1.is_some(), None),
None => (false, None),
},
}
}
}
impl<M> ShadowSync<M> {
/// An encoder of a command buffer that left `carry` holding shadowed textures' images: its
/// first use of one of them takes no copy in, where the image still stands there
/// ([`ShadowCarry`], [`entry`](Self::entry)).
pub(super) fn carrying(carry: ShadowCarry) -> ShadowSync<M> {
ShadowSync {
entries: Vec::new(),
carry,
tally: CarryTally::default(),
takes: TakeBy::Mark {
replaced: Takes::default(),
noted: Takes::default(),
},
}
}
/// A core that records work again in place of a recording that decided `replaced` of its
/// copies taken at use ([`Takes`]), which never runs: a host step's recording of work its
/// commit recorded (`CommandBufferShared::host_step`). Each of its first uses that recording
/// met takes the copy where it took it, keeping its take; any other where the texture's mark
/// asks. A take of `replaced` it never meets is given back as its recording replaces that
/// one ([`Takes::give_back_unheld`]). Nothing is carried: every other texture copies in at
/// its first use.
pub(super) fn over(replaced: Takes) -> ShadowSync<M> {
ShadowSync {
takes: TakeBy::Mark {
replaced,
noted: Takes::default(),
},
..ShadowSync::default()
}
}
/// An encoder encoded again (`command_buffer/reencode.rs`), whose first encoding decided
/// `first` of its copies taken at use ([`Takes`]): each of its first uses of such a texture
/// takes the copy where the use in its place there took it, the texture's mark untouched.
/// Nothing is carried: every other texture copies in at its first use.
pub(super) fn again(first: Takes) -> ShadowSync<M> {
ShadowSync {
takes: TakeBy::Again(first),
..ShadowSync::default()
}
}
/// What its first uses of copies taken at use decided, as the program encoded them or as a
/// recording in place of another kept them ([`over`](Self::over)), for an encoding of the
/// encoder again ([`again`](Self::again)) and for a give-back; none for an encoding again.
pub(super) fn into_takes(self) -> Takes {
match self.takes {
TakeBy::Mark { noted, .. } => noted,
TakeBy::Again(_) => Takes::default(),
}
}
/// How the encoder's first copies in since this was last called stood against their images'
/// last standing ([`CarryTally`]): the population the carry removes, the one Metal requires
/// of a later command buffer, and the one no rule avoids. It sizes the carry on a run without
/// a timing.
pub(super) fn tally(&mut self) -> CarryTally {
let tally = self.tally;
self.tally = CarryTally::default();
tally
}
/// Whether the encoder used no shadowed texture yet: then a command that uses none takes no
/// copy, and changes nothing here ([`before`](Self::before), [`after`](Self::after)). What
/// the command buffer carries is not among them: a command that drops one of those reaches
/// [`wrote`](Self::wrote) whatever the encoder used (`EncoderCore::track`).
pub(super) fn is_empty(&self) -> bool {
self.entries.is_empty()
}
/// Carry no texture's image on, and let every texture the encoder met go: the encoder's
/// commands from here on read another writer's bytes, which each texture copies in again to
/// hold, and its later encoders take none of the images this one leaves (`ShadowCarry`).
pub(super) fn not_carried(&mut self) {
self.carry.clear();
for entry in &mut self.entries {
entry.lose(Lost::Written);
}
}
/// Take the images of `copies` no further from what the command buffer's earlier encoders
/// left (`ShadowCarry::drop_copies`): a texture's faces' shadows, which its turn leaves
/// holding what is no longer its truth (`texture/faces.rs`).
pub(super) fn carry_no_further(&mut self, copies: &[Arc<ShadowCopies>]) {
self.carry.drop_copies(copies);
}
/// Note a recorded command with `uses`, whatever it touched: a write of a carried texture's
/// truth another way leaves its image stale, so the texture is no longer carried and copies
/// in at its next use. The encoder's own textures are [`after`](Self::after)'s, which holds
/// where each stands; this covers the commands of an encoder that meets none of them, whose
/// writes nothing else here would see.
pub(super) fn wrote(&mut self, uses: &[Use<ResourceKey>]) {
if self.carry.entries.is_empty() {
return;
}
let writes = |carried: &Carried| {
(uses.iter()).any(|used| !used.write.is_empty() && carried.copies.meets(used))
};
self.carry.drop_where(writes);
}
/// What the encoder leaves its command buffer's later encoders: every texture whose image
/// holds the truth as its commands left it, with where it holds it, and the textures it
/// never met that it still carried.
pub(super) fn ended(&mut self) -> ShadowCarry {
let mut carry = ShadowCarry {
id: self.carry.id,
entries: Vec::new(),
};
carry.entries.append(&mut self.carry.entries);
for entry in &self.entries {
if entry.standing != Standing::Current {
entry.copies.standing.missed(Missed::NotCurrent);
continue;
}
// The image holds the truth as this command buffer's commands left it.
(entry.copies.stood).store(carry.id, Ordering::Relaxed);
carry.entries.push(Carried {
copies: Arc::clone(&entry.copies),
held: entry.held,
stale: entry.stale,
});
}
self.entries.clear();
carry
}
/// How many of the textures the encoder used share their truth with one it used before
/// (another texture over the same bytes): each copies the texels in apart
/// (`WorkCounts::shadows_sharing_truth`).
pub(super) fn sharing_truth(&self) -> usize {
let entries = &self.entries;
(1..entries.len())
.filter(|&i| {
(entries[..i].iter()).any(|earlier| earlier.copies.shares_truth(&entries[i].copies))
})
.count()
}
}
/// Where a command buffer's earlier encoder left a shadowed texture's image, which its later
/// encoders take instead of copying the truth in again.
///
/// Metal shows an encoder the bytes as of its command buffer's commit (the module's **Copy in**
/// rule), so an image an earlier encoder of the same command buffer left holding the truth holds
/// what every later encoder of it reads. On the GPU nothing runs between two encoders of one
/// batch of the command buffer but its own commands: a queue's batches go in commit order, and
/// another queue's batch that conflicts with this one's uses of the texture (each copy in is a
/// tracked write of its image) runs wholly before or after it (`scheduler/mod.rs`, **The order**
/// and **Tracking**). Another queue's work comes between only where the command buffer's batches
/// split. So the carry is dropped wherever that no longer follows: the encoder that published it
/// did not end holding the truth, a command of the command buffer wrote the truth another way
/// ([`ShadowSync::wrote`]), a copy back or merge of another shadowed texture over the same bytes
/// wrote them (`ShadowSync::copy_back`), work the encoder records apart stands between
/// ([`ShadowSync::leave`]), an encoder waits for a fence (`EncoderCore::ordered_other_work`), or
/// the next encoder starts another batch: after an event or semaphore wait, after a signal, or
/// past a host step (`State::shadows_to_carry`), where another queue's batch may run first, as
/// Metal shows the encoder after a wait another queue's write. Another command buffer's encoder
/// ending in between, on this queue or another, drops nothing: it runs before or after the batch.
pub(super) struct ShadowCarry {
/// The command buffer's identity: a number no command buffer of the process had before and
/// none takes again, so a shadow can name the command buffer that last left its image
/// holding the truth ([`ShadowCopies::stood`]) without holding it. Metal names a command
/// buffer by its pointer, which another may take once it is freed.
id: u64,
entries: Vec<Carried>,
}
impl Default for ShadowCarry {
/// A command buffer carrying nothing, under an identity of its own.
fn default() -> ShadowCarry {
static NEXT: AtomicU64 = AtomicU64::new(1);
ShadowCarry {
id: NEXT.fetch_add(1, Ordering::Relaxed),
entries: Vec::new(),
}
}
}
/// How the large IOSurface planes an encoder's commands used stood against their images' last
/// standing, and what the carry took off the encoder ([`ShadowCarry`],
/// `WorkCounts::shadow_texels_first_copy_in_carried` and kin). The three large-surface figures
/// are exhaustive over an encoder's first copies in of such planes but for the ones a standing of
/// this command buffer's own could not serve, which
/// `WorkCounts::shadow_texels_first_copied_in_again` counts.
#[derive(Default, Clone, Copy)]
pub(super) struct CarryTally {
/// The texels no copy in moved, the encoder's command buffer holding the image already: what
/// carrying the image across its encoders takes off the run.
pub(super) carried: u64,
/// Of those, the large IOSurface planes'.
pub(super) carried_large: u64,
/// The texels an encoder's first copy in of a large plane moved where the image's last
/// standing was an earlier command buffer's: Metal shows a later command buffer a CPU write
/// made since the commit, so it requires this one where the CPU may write the plane; where
/// only the runtime's commands do, the standing across command buffers may take it off as
/// the batch is submitted (`shadow_standing.rs`).
pub(super) stood_earlier_large: u64,
/// The texels an encoder's first copy in of a large plane moved where the image never held a
/// standing: unavoidable under any rule.
pub(super) unstood_large: u64,
}
/// One shadowed texture's image a command buffer's earlier encoder left holding the truth
/// ([`ShadowCarry`]).
struct Carried {
copies: Arc<ShadowCopies>,
/// [`Entry::held`] and [`Entry::stale`] as it left them.
held: Option<[u32; 4]>,
stale: Option<[u32; 4]>,
}
impl ShadowCarry {
/// Carry nothing, under the same identity: every shadowed texture copies in again at its
/// next use, and a first copy in of one this command buffer stood behind still reads as that.
pub(super) fn clear(&mut self) {
self.drop_where(|_| true);
}
/// Carry no further the images of `copies` (`texture/faces.rs`: a texture's turn to its other
/// face moves its truth).
pub(super) fn drop_copies(&mut self, copies: &[Arc<ShadowCopies>]) {
let named = |carried: &Carried| {
(copies.iter()).any(|copies| copies.shadow_key == carried.copies.shadow_key)
};
self.drop_where(named);
}
/// Carry no further the images `drops` picks (`shadow_standing::Missed::Dropped`).
fn drop_where(&mut self, drops: impl Fn(&Carried) -> bool) {
self.entries.retain(|carried| {
let dropped = drops(carried);
if dropped {
carried.copies.standing.missed(Missed::Dropped);
}
!dropped
});
}
/// At the command buffer's last batch: where its end may leave shadows standing (`ends`),
/// those it carries holding every texel of the truth, each with the rectangle bounding the
/// texels writes within it changed since (`None` where none did, [`Entry::stale`]), which
/// the next copy in that the standing serves moves alone; each other one noted as held in
/// part; else none, each noted as met by an end that records no standing
/// (`shadow_standing::Missed`).
pub(super) fn standing_at_end(&self, ends: bool) -> Vec<(Arc<ShadowCopies>, Option<[u32; 4]>)> {
let mut held = Vec::new();
for carried in &self.entries {
let whole = carried.copies.covers(carried.held);
match (ends, whole) {
(true, true) => held.push((Arc::clone(&carried.copies), carried.stale)),
(true, false) => carried.copies.standing.missed(Missed::Partial),
(false, _) => carried.copies.standing.missed(Missed::End),
}
}
held
}
/// The shadowed textures whose images it carries holding every texel of the truth, none
/// stale (`dirty_pages.rs`: what a command buffer's end leaves a later one's copy in).
pub(super) fn held_whole(&self) -> Vec<Arc<ShadowCopies>> {
(self.entries.iter())
.filter(|carried| carried.stale.is_none() && carried.copies.covers(carried.held))
.map(|carried| Arc::clone(&carried.copies))
.collect()
}
}
/// What a merge's scratch keeps of a shadow's texels (`shadow_merge.rs`): the rectangle
/// `[x0, y0, x1, y1)` its keep, its copy of the changed texels and its merge are cut to, or `None`
/// for every texel.
pub(super) trait KeptArea {
fn kept_area(&self) -> Option<[u32; 4]>;
/// Whether the scratch was made for a command whose merge stores whole blocks where it can
/// ([`stores_blocks`], `shadow_merge.rs`): only such a command's keep takes it again.
fn in_blocks(&self) -> bool {
false
}
}
impl KeptArea for Arc<ShadowMerge> {
fn kept_area(&self) -> Option<[u32; 4]> {
self.area()
}
fn in_blocks(&self) -> bool {
ShadowMerge::in_blocks(self)
}
}
/// What a merge `ShadowSync::before` asks for is made for.
#[derive(Clone, Copy, Debug)]
pub(super) struct MergeFor {
/// A command that writes the bytes two ways ([`two_ways`]).
pub(super) two_ways: bool,
/// A command whose merge stores whole blocks where it is cut to the texels' rows
/// ([`stores_blocks`]): one that replaces every texel over logged pages
/// ([`overwrites_unkept`]), or a render pass that writes through the texture only by its
/// stores of the area's tiles ([`ShadowUse::tile_stores`]).
pub(super) blocks: bool,
}
impl<M: Clone + KeptArea> ShadowSync<M> {
/// Hold `copies`' image current, every texel of it, where the encoder has no entry of it: it
/// holds its truth's texels as a copy in now would leave them, the face a texture presented
/// until its pass's turn (`texture/faces.rs`). An entry the encoder has stays as it is.
pub(super) fn hold_ahead(&mut self, copies: &Arc<ShadowCopies>) {
let key = copies.shadow_key;
if self.entries.iter().any(|e| e.copies.shadow_key == key) {
return;
}
self.entries.push(Entry {
copies: Arc::clone(copies),
standing: Standing::Current,
held: None,
dirty: false,
area: None,
merge: None,
two_ways: false,
scratch: None,
stale: None,
carried: false,
run: false,
took: None,
});
}
/// The entry of `copies`, made when the encoder had none.
fn entry(&mut self, copies: &Arc<ShadowCopies>) -> usize {
match self
.entries
.iter()
.position(|e| e.copies.shadow_key == copies.shadow_key)
{
Some(i) => i,
None => {
// Carried from an earlier encoder of the command buffer: the image holds the truth
// where that one left it, and this encoder's first use of it takes no copy in
// ([`ShadowCarry`]).
let carried = (self.carry.entries.iter())
.position(|carried| carried.copies.shadow_key == copies.shadow_key)
.map(|at| self.carry.entries.swap_remove(at));
let (standing, held, stale) = match &carried {
Some(carried) => (Standing::Current, carried.held, carried.stale),
None => (Standing::Unread, None, None),
};
// A copy taken at use is copied in at the first use after its making or its
// `did_modify_data`, wherever it is; else its image holds what it took. Encoded
// again, where its first encoding took it ([`TakeBy`]).
let (took, number) = match &copies.levels {
Some(levels) => self.takes.take(copies, levels),
None => (false, None),
};
let (standing, held, stale) = match &copies.levels {
Some(_) if took => (Standing::Unread, None, None),
Some(_) => (Standing::Current, None, None),
None => (standing, held, stale),
};
self.entries.push(Entry {
copies: Arc::clone(copies),
standing,
held,
dirty: false,
area: None,
merge: None,
two_ways: false,
scratch: None,
stale,
carried: carried.is_some(),
run: false,
took: number,
});
self.entries.len() - 1
}
}
}
/// Copy back entry `i` (dirty): its image's bytes replace the buffer's, so every other texture
/// over them is no longer current, or, where the copy back covers a rectangle of texels laid
/// out as its own, stale there alone (the module's rules); one the encoder has not met that its
/// command buffer carries ([`ShadowCarry`]) is carried no further, and copies in at its next
/// use. One kept is merged instead: one kept before a command that wrote its bytes two ways is
/// then no longer current either, as the buffer holds the other way's writes too, and leaves
/// every other texture over the bytes no longer current; one kept as it merges back still holds
/// what the encoder's commands left in them, and its merge changes only words they changed
/// through it, inside the area they wrote in where they bound it, so it leaves the others stale
/// there alone, as a copy back of that area does. A copy back covers only the area the commands
/// wrote in, where they bound it: the image's other texels are what the last copy in left,
/// which the buffer still holds (a write of its bytes another way leaves the image stale, and
/// copies in again before the next use).
fn copy_back(&mut self, i: usize, copies: &mut Vec<ShadowCopy<M>>) {
let entry = &mut self.entries[i];
let two_ways = entry.two_ways;
let step = match &entry.merge {
Some(merge) => {
let merge = merge.clone();
entry.merge = None;
if two_ways {
entry.lose(Lost::TwoWays);
}
entry.two_ways = false;
Step::Merge(merge)
}
None => Step::Back,
};
let area = match step {
Step::Back => entry.area,
_ => None,
};
// Where the buffer's bytes may have changed for the other textures over them.
let changed = match step {
Step::Merge(_) if two_ways => None,
_ => entry.area,
};
entry.dirty = false;
entry.area = None;
let written = Arc::clone(&entry.copies);
copies.push(ShadowCopy {
copies: Arc::clone(&written),
step,
kept: false,
area,
cause: None,
moves: None,
later: None,
clear: None,
});
for (j, other) in self.entries.iter_mut().enumerate() {
if j != i && other.copies.shares_truth(&written) {
other.changed(&written, changed, Lost::OtherShadow);
}
}
// A carried image holds the bytes as they were before this write.
(self.carry).drop_where(|carried| carried.copies.shares_truth(&written));
}
/// Go on with a run of passes over the texture of `copies` (`pass_runs.rs`): its image, which
/// the encoder carries, as the run's passes left it, written through since `merge` kept it
/// before the run's first, as an earlier command of this encoder would have left it. So the
/// encoder's first command that writes through it takes no keep, and the copy back after its
/// last is a merge in `merge` (or in a scratch a later keep took, where a command writes
/// outside what `merge` keeps, after a merge in it: [`before`](Self::before)). Whether it
/// could: `false`, changing nothing, where the encoder carries no such image, or one stale
/// somewhere, or met the texture already.
pub(super) fn go_on(&mut self, copies: &Arc<ShadowCopies>, merge: M) -> bool {
let key = copies.shadow_key;
let met = self.entries.iter().any(|e| e.copies.shadow_key == key);
let carried = (self.carry.entries.iter())
.any(|carried| carried.copies.shadow_key == key && carried.stale.is_none());
if met || !carried {
return false;
}
let i = self.entry(copies);
let entry = &mut self.entries[i];
entry.dirty = true;
entry.area = None;
entry.merge = Some(merge.clone());
entry.scratch = Some(merge);
entry.run = true;
true
}
/// The copy back of what the encoder's commands wrote through the texture of `copies`, taken
/// now as the merge a run's pass leaves to its batch's submission (`pass_runs.rs`): where its
/// image was kept as it merges back and written through since, not two ways, its merge
/// ([`copy_back`](Self::copy_back)), after which the image holds the truth as the commands
/// left it, carried on as the encoder ends. `None`, changing nothing, for any other texture.
pub(super) fn leave_merge(&mut self, copies: &ShadowCopies) -> Option<ShadowCopy<M>> {
let key = copies.shadow_key;
let i = self
.entries
.iter()
.position(|e| e.copies.shadow_key == key)?;
let entry = &self.entries[i];
if !entry.dirty || entry.merge.is_none() || entry.two_ways {
return None;
}
let mut copies = Vec::new();
self.copy_back(i, &mut copies);
copies.pop()
}
/// The copies to record, in order, before a command that uses the shadowed textures
/// `shadows` and has `uses` (every use of the command, the textures' among them): merges of
/// the textures kept before the command before, copies back of what the command touches,
/// copies in of what it uses, a keep of each texture it writes through that merges back, where
/// none is kept since the last copy back, and a keep of each texture it writes two ways, each
/// in a scratch `merge` makes (told whether the command writes the bytes two ways, and the
/// rectangle of texels it is to keep, `None` for every texel). Where the texture merges back,
/// the scratch keeps the rectangle the command writes in, where
/// it bounds that and the copies are cut to rectangles ([`ShadowUse::area`]); a later command
/// that writes outside what the encoder's scratch keeps takes a scratch of the rectangle
/// bounding both, after a merge of what was kept in the last. Refused, changing nothing, as
/// `merge` refuses, and for a command that
/// writes an image two ways, through a view's shadow of it and another alias, which no merge
/// serves (`Unsupported`, `Feature::ViewFormatReinterpretation`), or a rotated texture's bytes
/// two ways (`NotImplemented(RotatedTexture)`).
pub(super) fn before(
&mut self,
shadows: &[ShadowUse],
uses: &[Use<ResourceKey>],
mut merge: impl FnMut(&ShadowCopies, MergeFor, Option<[u32; 4]>) -> Result<M, Error>,
) -> Result<Vec<ShadowCopy<M>>, Error> {
// A copy back of an image's shadow would replace the other alias's writes, and only a
// buffer's bytes laid out as the texture's are merged: such a command is refused before
// any merge is made (a rotated texture's too).
for (i, shadow) in shadows.iter().enumerate() {
if let Some(refused) = shadow.copies.unmerged() {
if two_ways(shadows, i, uses) {
return Err(refused);
}
}
}
let mut kept = Vec::new();
// The textures the command writes through, its bytes another way not, that merge back:
// kept in a scratch of the encoder's, made at the first such command.
let mut merged_kept = Vec::new();
let mut scratches = Vec::new();
for (i, shadow) in shadows.iter().enumerate() {
if two_ways(shadows, i, uses) {
let made_for = MergeFor {
two_ways: true,
blocks: false,
};
kept.push((
Arc::clone(&shadow.copies),
merge(&shadow.copies, made_for, None)?,
));
} else if merged_back(shadow) {
merged_kept.push(Arc::clone(&shadow.copies));
// The texels the command writes, where it bounds them and the copies are cut so.
let writes = shadow.area.filter(|_| shadow.copies.cuts_back());
let entry = self
.entries
.iter()
.find(|e| e.copies.shadow_key == shadow.copies.shadow_key);
let scratch = entry.and_then(|e| e.scratch.as_ref());
// A scratch made for a command of the other kind (one whose merge stores whole
// blocks, or one whose does not) is merged and kept again in a new one: only such
// a command's merge stores whole blocks (`shadow_merge.rs`).
let same = |scratch: &M| scratch.in_blocks() == stores_blocks(shadow, writes);
// A run's pass that writes outside its run's keep keeps every texel next, which
// every later pass of the run writes inside (`pass_runs.rs`).
let run = entry.is_some_and(|e| e.run);
let area = match scratch {
Some(scratch) if holds(scratch.kept_area(), writes) && same(scratch) => {
continue
}
Some(scratch) => match (run, writes, shadow.copies.whole_area()) {
(true, Some(_), Some(whole)) => Some(whole),
_ => bounding(scratch.kept_area(), writes),
},
None => writes,
};
let made_for = MergeFor {
two_ways: false,
blocks: stores_blocks(shadow, area),
};
scratches.push((
Arc::clone(&shadow.copies),
merge(&shadow.copies, made_for, area)?,
));
}
}
// A texture kept in a scratch that does not keep what the command writes: merged before
// its keep in the new one.
let mut regrown = Vec::new();
for (copies, scratch) in scratches {
let i = self.entry(&copies);
let entry = &mut self.entries[i];
if entry.merge.is_some() && !entry.two_ways {
regrown.push(copies.shadow_key);
}
entry.scratch = Some(scratch);
}
let mut copies = Vec::new();
// Merge what the command before wrote two ways; copy back what this one reads or writes
// another way: through the buffer's bytes, or through another texture over them that is
// copied in next.
for i in 0..self.entries.len() {
if !self.entries[i].dirty {
continue;
}
let entry = &self.entries[i].copies;
let touched = uses.iter().any(|used| entry.meets(used))
|| shadows.iter().any(|shadow| {
shadow.copies.shadow_key != entry.shadow_key
&& shadow.copies.shares_truth(entry)
});
let entry = &self.entries[i];
let regrows = regrown.contains(&entry.copies.shadow_key);
// Written since its last copy back with nothing kept (by a command that replaced
// every texel), and kept below: copied back first, as the merge after the keep takes
// only the words changed since it, which would leave those writes out.
let unkept = entry.merge.is_none()
&& (merged_kept.iter()).any(|kept| kept.shadow_key == entry.copies.shadow_key);
if touched || regrows || unkept || (entry.merge.is_some() && entry.two_ways) {
self.copy_back(i, &mut copies);
}
}
for shadow in shadows {
// Made here, or for a keep's scratch above: not copied in yet, so its copy in is the
// encoder's first.
let i = self.entry(&shadow.copies);
// Cleared to what the command reads instead (`ShadowUse::cleared`): the image holds
// the truth nowhere after, whatever it held, so no carry takes it and the next use
// copies in.
if let Some(value) = shadow
.cleared
.filter(|_| shadow.copies.cleared_image().is_some())
{
copies.push(ShadowCopy {
copies: Arc::clone(&shadow.copies),
step: Step::Into,
kept: false,
area: None,
cause: None,
moves: None,
later: None,
clear: Some(value),
});
let entry = &mut self.entries[i];
entry.carried = false;
entry.held = None;
entry.stale = None;
entry.lose(Lost::Written);
continue;
}
let two_ways = kept
.iter()
.any(|(copies, _)| copies.shadow_key == shadow.copies.shadow_key);
// A copy taken at use copies every texel in and makes its later levels whatever the
// command overwrites.
let taken_at_use = shadow.copies.levels.is_some();
let overwrites = overwrites_unkept(shadow) && !two_ways && !taken_at_use;
// The texels the command needs as the buffer holds them: those it reads, and those
// it writes (which a copy back moves), where it bounds both and the copies are cut to
// a rectangle; every texel where it writes through a shadow whose copies back move
// every texel, or writes the bytes two ways, as the merge after it compares every
// texel the keep took; every texel of a shadow over logged host pages, whose copy
// in of every texel moves only the rows the log reports changed, and leaves the image
// holding every texel for the next command buffer's (`dirty_pages.rs`); and every
// texel at the command buffer's first use of a shadow that may stand across command
// buffers, whose copy in moves nothing where the image stands, and leaves it holding
// every texel for this command buffer's later commands and the next command buffer's
// (`shadow_standing.rs`), but by a command that writes its truth apart from it
// ([`writes_truth_apart`]), after which the image holds the truth whole nowhere a
// command buffer's end could leave standing.
let first_use = self.entries[i].standing == Standing::Unread;
let standing = first_use
&& shadow.copies.stands_across()
&& !writes_truth_apart(shadows, shadow, uses);
let whole = two_ways
|| !shadow.copies.cuts()
|| shadow.copies.logged()
|| standing
|| taken_at_use;
let needs = match whole {
true => None,
false if shadow.writes && !shadow.copies.cuts_back() => None,
false if shadow.writes => bounding(shadow.loads, shadow.area),
false => shadow.loads,
};
// A rectangle of every texel (a pass whose draws no scissor bounds) is every texel:
// the image then holds them all, which a later command that reads anywhere finds
// held, in this encoder or one its command buffer carries the image to.
let needs = needs.filter(|&area| !shadow.copies.covers(Some(area)));
let entry = &self.entries[i];
let beyond_held =
entry.standing == Standing::Current && !overwrites && !holds(entry.held, needs);
// A pass of a run of passes that needs texels beyond those takes every texel in, so
// the run's later passes need none beyond them (`pass_runs.rs`).
let needs = match beyond_held && entry.run {
true => None,
false => needs,
};
// The image holds the buffer's texels only inside its last copy in's rectangle: what
// the encoder wrote through it goes back first, then the copy in below moves what
// this command needs.
if beyond_held && entry.dirty {
self.copy_back(i, &mut copies);
}
let entry = &mut self.entries[i];
let cause = match entry.standing {
_ if beyond_held => Some(CopyCause::BeyondHeld),
Standing::Current => None,
Standing::Unread => Some(CopyCause::First),
Standing::Lost(lost) => Some(CopyCause::Changed(lost)),
};
// The encoder's first use of a texture its command buffer already held: without the
// carry the entry would have been `Standing::Unread` and this a copy in of `needs`,
// so where none is made that copy is what the carry took off the run
// (`WorkCounts::shadow_texels_first_copy_in_carried`). Where one is made anyway (the
// command needs texels beyond what the carry held) it moves the same texels as it
// would have, and nothing is taken off; a copy in of what writes within a rectangle
// changed since (`CopyCause::Bounded`), which only a carried entry can take here, is
// counted as its own.
let carried = entry.carried;
entry.carried = false;
let took = carried && cause.is_none() && !overwrites;
if let Some(cause) = cause {
if !overwrites {
copies.push(ShadowCopy {
copies: Arc::clone(&shadow.copies),
step: Step::Into,
kept: shadow.kept,
area: needs,
cause: Some(cause),
moves: None,
later: None,
clear: None,
});
}
entry.standing = Standing::Current;
entry.held = needs;
entry.stale = None;
}
// The texels writes within a rectangle changed since the copy in: those alone. A
// command that reads only the texels inside a rectangle and writes none takes those of
// them inside it alone, and the rest stay changed, for the next command that reads
// them: the image holds the buffer's texels everywhere else it held them.
if let Some(stale) = entry.stale {
let area = match (shadow.writes, needs) {
(false, Some(reads)) => {
entry.stale = outside(stale, reads);
meet(stale, reads)
}
_ => {
entry.stale = None;
Some(stale)
}
};
if let (false, Some(area)) = (overwrites, area) {
copies.push(ShadowCopy {
copies: Arc::clone(&shadow.copies),
step: Step::Into,
kept: shadow.kept,
area: Some(area),
cause: Some(CopyCause::Bounded),
moves: None,
later: None,
clear: None,
});
}
}
// The command writes every texel before it reads any.
if overwrites {
entry.held = None;
}
// What the first copy in of this use moved, or would have moved where the carry took
// it off: the copy `before` pushes above, or the one it would have pushed.
let first = (took || (cause == Some(CopyCause::First) && !overwrites)).then(|| {
let copy: ShadowCopy<M> = ShadowCopy {
copies: Arc::clone(&shadow.copies),
step: Step::Into,
kept: shadow.kept,
area: needs,
cause: Some(CopyCause::First),
moves: None,
later: None,
clear: None,
};
copy.texels_copied_in()
});
if let Some(texels) = first {
let large = shadow.copies.large_surface();
let stood = shadow.copies.stood.load(Ordering::Relaxed);
match (took, large) {
(true, large) => {
self.tally.carried += texels;
self.tally.carried_large += u64::from(large) * texels;
}
// A standing of this command buffer's own that something let a writer past:
// `WorkCounts::shadow_texels_first_copied_in_again` counts those.
(false, true) if stood == self.carry.id => {}
(false, true) if stood != 0 => self.tally.stood_earlier_large += texels,
(false, true) => self.tally.unstood_large += texels,
(false, false) => {}
}
}
}
// Kept after its copy in, until the copy back after the command, or after a later one,
// which is then a merge: a copy back would write the texels no command wrote with what the
// copy in read, over what another writer may have written since.
for shadow in merged_kept {
let i = self.entry(&shadow);
let entry = &mut self.entries[i];
let (None, Some(scratch)) = (&entry.merge, &entry.scratch) else {
continue;
};
let scratch = scratch.clone();
entry.merge = Some(scratch.clone());
copies.push(ShadowCopy {
copies: shadow,
step: Step::Keep(scratch),
kept: false,
area: None,
cause: None,
moves: None,
later: None,
clear: None,
});
}
for (shadow, merge) in kept {
let i = self.entry(&shadow);
self.entries[i].merge = Some(merge.clone());
self.entries[i].two_ways = true;
copies.push(ShadowCopy {
copies: shadow,
step: Step::Keep(merge),
kept: false,
area: None,
cause: None,
moves: None,
later: None,
clear: None,
});
}
Ok(copies)
}
/// Note a recorded command that used `shadows` and had `uses`, and the writes `within`
/// (whose uses are not among `uses`): a write through a texture leaves its image to copy
/// back; a write of the bytes another way leaves the images over them stale, there alone
/// where it is one of `within` and the image's texels are laid out as its texture's (the
/// module's rules).
pub(super) fn after(
&mut self,
shadows: &[ShadowUse],
uses: &[Use<ResourceKey>],
within: &[Within<'_>],
) {
for entry in &mut self.entries {
// A texture's own uses are its image's, never its bytes'.
let copies = Arc::clone(&entry.copies);
let writes = |used: &Use<ResourceKey>| !used.write.is_empty() && copies.meets(used);
if uses.iter().any(writes) {
entry.lose(Lost::Written);
continue;
}
for part in within.iter().filter(|part| part.uses.iter().any(writes)) {
match &part.texels {
Some(texels) => entry.changed(texels, Some(part.area), Lost::Within),
None => entry.lose(Lost::Within),
}
}
}
// A copy taken at use keeps what is written through it: nothing goes back.
let written = |shadow: &&ShadowUse| shadow.writes && shadow.copies.levels.is_none();
for shadow in shadows.iter().filter(written) {
let i = self.entry(&shadow.copies);
// Writes Metal drops are not copied back, and the image no longer holds the bytes.
let entry = &mut self.entries[i];
match shadow.copies.drops_writes {
true => entry.lose(Lost::Dropped),
false => {
entry.area = match entry.dirty {
true => bounding(entry.area, shadow.area),
false => shadow.area,
};
entry.dirty = true;
}
}
}
}
/// The copies back before work the encoder records apart (an indirect command buffer's
/// commands, recorded at commit, with copies of their own): every texture written through
/// since its last. The encoder then forgets every texture, so a later command copies in what
/// that work may have written.
pub(super) fn leave(&mut self) -> Vec<ShadowCopy<M>> {
let copies = self.finish();
self.drop_entries();
self.carry.clear();
copies
}
/// Forget every texture, recording no copy: what the encoder recorded so far is left out of
/// its command buffer's work (`EncoderCore::leave_out_recorded`), so no image holds what a
/// copy in there moved, and nothing written through one there ran. A later command copies in
/// again.
pub(super) fn forget(&mut self) {
// A copy taken at use whose copy in is left out is taken at the next use instead. An
// encoding again took none by the mark, and gives none back to it.
for entry in &self.entries {
if let (Some(number), Some(levels)) = (entry.took, &entry.copies.levels) {
levels.give_back(number);
}
}
self.drop_entries();
self.carry.clear();
}
/// Let every texture the encoder met go, none of them carried on
/// (`shadow_standing::Missed::Dropped`).
fn drop_entries(&mut self) {
for entry in self.entries.drain(..) {
entry.copies.standing.missed(Missed::Dropped);
}
}
/// The copies back at the end of the encoder: every texture written through since its last
/// (merged where kept).
pub(super) fn finish(&mut self) -> Vec<ShadowCopy<M>> {
let mut copies = Vec::new();
for i in 0..self.entries.len() {
if self.entries[i].dirty {
self.copy_back(i, &mut copies);
}
}
copies
}
}