What: on a device whose driver has DriverFact::ImageUsesMissBufferWritesInOneCommandBuffer (NVIDIA's 620.70.06 on the RTX 3090), an inline pass's last instance may now stay open, and the next render encoder may continue it, as on every other device. Before it continues, EncoderCore::continue_pass checks the group's byte writes (FoldOrder::byte_writes, the leader's summary or the group's order) against every image the continuing pass uses: its draws' images (AliasWrites' images noted apart), its attachments, and the uses of the copies and steps before its later instances (render/inline_splits.rs, waiting_uses), all of which record into the tail after what the group wrote there. Where one meets, the pass begins its own instance in its own VkCommandBuffer, the measured form, counted in WorkCounts::command_buffers_split_at_aliases. Where none meets, it continues and takes the group's writes as its own (AliasWrites::continues), so what it records into the tail after its pass is checked as a joining encoder's is. Why: continuation cut the glass-panel frame's GPU time 11-26% on the 780M and Iris but was off on the 3090. The open instance itself never holds a buffer write: its draws touch only attachments, and anything else that records into the tail ends it first. The tail before it can hold one, though (the leader's earlier inline instances and what records between them), so the check is what the fact needs. Also: render_fold's Lane::continuing (622368ab37) goes, since its lanes continue on every device now. A test-only DeviceShared::aliases_split_forced lets lavapipe take the 3090's path. New tests: tests::alias_split's continues unit test; tests::alias_split_device's count case, a copy out of a texture after a continuation, a later instance of one pass reading an earlier one's buffer write, and a texture over another buffer of the same host pages (the last two are byte cases for two possible gaps on the 3090). Also e818e7b442: Continued passes: a pass's uses of bytes count in the alias check, as an aliasing texture's are. What: AliasWrites notes the bytes of buffers that the commands noted apart use (bytes_apart). AliasWrites::continues now refuses a continuation where those bytes, or a Bytes use among the pass's other uses, meet the group's byte writes, besides its images. Why: a texture whose linear image aliases a buffer's bytes (Storage::Alias) names them as the buffer's bytes (Texture::image_use), not as an image range. The lavapipe run with the split forced (m3-max-35002) showed a pass sampling such a texture over bytes the leader had just written continuing anyway: (1, 1, 1, 2, 0) where (2, 1, 0, 3, 1) was expected. A continuation is refused rather than guessed at: a draw that reads, through a buffer, bytes the group wrote also begins its own instance on such a device. Tests: the count case now reads the written bytes through a texture over a buffer and And dae7c2cad8: alias_split_device: the counted and copy-after cases read a texture over a buffer alone. Verified in the lane (m3-max-35085, msi, at dae7c2cad8): clippy -p metal2vulkan-runtime --all-targets --all-features -D warnings exit 0; lavapipe (m2v_cpu_icd) alias_split and render_fold 36/36; the count test forced: written read (2,1,0,3,1), quiet read (1,1,1,2,0), unforced (1,1,1,2,0), texels right. Still running when landed: the 3090 correctness job (alias_split, render_fold; F1-F3 byte cases), PHOENIX render suites, clippy m2v_gpu and m2v_cpu_icd, rustdoc, the 3090 glass A/B. New Degradation kinds: none.
251 lines
13 KiB
Rust
251 lines
13 KiB
Rust
//! A use of an image after a write through a buffer over its memory, in a new `VkCommandBuffer`,
|
|
//! on a driver whose image uses miss such writes inside one
|
|
//! (`DriverFact::ImageUsesMissBufferWritesInOneCommandBuffer`).
|
|
//!
|
|
//! Vulkan makes a write through a buffer visible to a later use of a linear image in `GENERAL`
|
|
//! over the same memory once a memory dependency orders the two: both are host-accessible
|
|
//! aliases. The tracker orders them so (`tracker.rs`): one key names a heap's bytes and the
|
|
//! images bound in them, a buffer's bytes and the images over them, and a write of a range of
|
|
//! them orders every later use that meets it, inside an encoder and between the encoders of a
|
|
//! group that share a `VkCommandBuffer` (`encoder_fold.rs`). On NVIDIA's 620.70.06 on the RTX
|
|
//! 3090 a copy out of such an image, behind that global memory barrier in the same
|
|
//! `VkCommandBuffer` as the buffer's write, read every texel as it was before the write, where
|
|
//! the same two commands in two `VkCommandBuffer`s of one submission, the barrier between them,
|
|
//! read the write (the fact's measurement, `driver_facts.rs`).
|
|
//!
|
|
//! - **The rule.** On such a device an encoder notes the bytes each of its commands writes into
|
|
//! the `VkCommandBuffer` it records into ([`AliasWrites`]); an encoder that joined a group of
|
|
//! encoders starts from what the group's earlier encoders wrote there, its order's writes of
|
|
//! bytes ([`FoldOrder::byte_writes`]). A command that uses an image of a key whose noted bytes
|
|
//! meet the image's place ([`UseRange::shares_memory`]) goes in a new `VkCommandBuffer`: the
|
|
//! encoder ends the one it records into and goes on in a new one, the command's barrier first
|
|
//! (`EncoderCore::split_at_aliases`), which notes none. An encoder that joined a group goes on
|
|
//! in a `VkCommandBuffer` of its own after the group's: its part's submission orders it after
|
|
//! the group's first part, the tracker's barrier between them in a command buffer of barriers,
|
|
//! the form measured to read the write. Counted: `WorkCounts::command_buffers_split_at_aliases`.
|
|
//! - **Render encoders.** A pass records no barrier, nor a new `VkCommandBuffer`, inside it, so
|
|
//! the split goes before it. One whose pass records at its end joins a group as any encoder
|
|
//! (`State::join_fold`). Its draws, recorded into its secondary, run after every command it
|
|
//! records into the `VkCommandBuffer` before the pass, whichever came first: each draw's image
|
|
//! uses are noted apart ([`AliasWrites::noted_apart`]), and where one meets a noted write, or a
|
|
//! write noted later meets one ([`AliasWrites::note`]), the encoder owes a split. The first
|
|
//! command it then records into the `VkCommandBuffer` (a shadow's copy before a later draw, or
|
|
//! at its end the steps before its pass and the pass, whose uses hold its attachments') goes in
|
|
//! a new one where it owes one, or where its own uses meet the writes: so every image the pass
|
|
//! uses is checked against every write before it before the pass records. The images noted
|
|
//! apart stay noted in the new one, whose later writes their draws still follow. One whose
|
|
//! first instance records inline joins no group (`encoder_fold.rs`): its draws record from its
|
|
//! first on, before its later calls are known. It may continue the render pass instance the
|
|
//! encoder before it left open in the group's tail (`encoder_fold.rs`'s **Continued passes**),
|
|
//! which records what its pass records after what the group's encoders wrote there: it does so
|
|
//! only where no memory its draws use (noted apart), nor its attachments', nor that of the
|
|
//! copies and steps before its later instances, meets the group's writes of bytes
|
|
//! ([`AliasWrites::continues`]), and then notes those writes as its own, as a joining encoder
|
|
//! does; else its pass begins its own instance in a `VkCommandBuffer` of its own, counted as
|
|
//! a split. Its uses of bytes count there with its images': a texture whose image aliases a
|
|
//! buffer's bytes names them as the buffer's (`Texture::image_use`), which no use tells from a
|
|
//! use through the buffer.
|
|
//! - **Scope.** The measured direction alone: a use through an image after a write through a
|
|
//! buffer. A use through a buffer after a write through an image is not measured and takes no
|
|
//! split. A render pass's draws record inside its pass, where no `VkCommandBuffer` ends; what
|
|
//! they write a later encoder's commands meet through the group's order, as any encoder's.
|
|
//! - **Elsewhere** nothing is noted and nothing splits: the device's encoders carry no
|
|
//! [`AliasWrites`].
|
|
|
|
use crate::encoder_fold::FoldOrder;
|
|
use crate::resource::ResourceKey;
|
|
use crate::tracker::{Use, UseRange};
|
|
use metal2vulkan::device::DriverFact;
|
|
use std::collections::BTreeSet;
|
|
use std::ops::Range;
|
|
|
|
/// Whether a device whose driver has `facts` records a use of an image after a write through a
|
|
/// buffer over its memory in a new `VkCommandBuffer` (the module docs).
|
|
pub(crate) fn splits_at_aliases(facts: &BTreeSet<DriverFact>) -> bool {
|
|
facts.contains(&DriverFact::ImageUsesMissBufferWritesInOneCommandBuffer)
|
|
}
|
|
|
|
/// The most keys an [`AliasWrites`] notes the bytes of apart, in its writes and in its images
|
|
/// noted apart each: past them a new key makes every range meet, so the next image use, or the
|
|
/// next write, splits.
|
|
const KEPT: usize = 32;
|
|
|
|
/// Ranges of bytes of keys' memory: at most [`KEPT`] keys', a key's ranges joined into the range
|
|
/// that holds them all, and past that every key's.
|
|
#[derive(Default)]
|
|
struct Noted {
|
|
ranges: Vec<(ResourceKey, Range<u64>)>,
|
|
/// A range of more keys than [`KEPT`] was noted: every range meets one.
|
|
every: bool,
|
|
}
|
|
|
|
impl Noted {
|
|
/// Note `bytes` of `key`: joined into the key's noted range where it has one, else apart
|
|
/// while fewer than [`KEPT`] keys are.
|
|
fn add(&mut self, key: ResourceKey, bytes: Range<u64>) {
|
|
if self.every {
|
|
return;
|
|
}
|
|
if let Some((_, range)) = self.ranges.iter_mut().find(|(noted, _)| *noted == key) {
|
|
range.start = range.start.min(bytes.start);
|
|
range.end = range.end.max(bytes.end);
|
|
} else if self.ranges.len() < KEPT {
|
|
self.ranges.push((key, bytes));
|
|
} else {
|
|
self.every = true;
|
|
}
|
|
}
|
|
|
|
/// Whether `bytes` of `key` meet a noted range.
|
|
fn meets(&self, key: ResourceKey, bytes: &Range<u64>) -> bool {
|
|
self.every
|
|
|| (self.ranges.iter()).any(|(noted, range)| {
|
|
*noted == key && range.start < bytes.end && bytes.start < range.end
|
|
})
|
|
}
|
|
|
|
/// Whether a range of `other` meets a noted range: none where either noted nothing.
|
|
fn meets_any(&self, other: &Noted) -> bool {
|
|
let empty = |noted: &Noted| !noted.every && noted.ranges.is_empty();
|
|
if empty(self) || empty(other) {
|
|
return false;
|
|
}
|
|
self.every
|
|
|| other.every
|
|
|| (other.ranges.iter()).any(|(key, bytes)| self.meets(*key, bytes))
|
|
}
|
|
|
|
/// Note every range of `other`.
|
|
fn join(&mut self, other: Noted) {
|
|
if other.every {
|
|
self.every = true;
|
|
self.ranges.clear();
|
|
return;
|
|
}
|
|
for (key, bytes) in other.ranges {
|
|
self.add(key, bytes);
|
|
}
|
|
}
|
|
|
|
fn clear(&mut self) {
|
|
self.ranges.clear();
|
|
self.every = false;
|
|
}
|
|
}
|
|
|
|
/// What an encoder's commands on a device that splits at aliases noted (the module docs): the
|
|
/// bytes of each key the commands recorded into its current `VkCommandBuffer` wrote, and the
|
|
/// memory the images of its commands recorded apart use, which run after them (a pass's draws).
|
|
/// Each holds at most [`KEPT`] keys' ranges, and past that every key's.
|
|
#[derive(Default)]
|
|
pub(crate) struct AliasWrites {
|
|
writes: Noted,
|
|
/// The bytes the images of the commands recorded outside the `VkCommandBuffer`, which a
|
|
/// later command there executes, use ([`noted_apart`](Self::noted_apart)): kept from one
|
|
/// `VkCommandBuffer` to the next, as those commands run after what either records before them.
|
|
apart: Noted,
|
|
/// The bytes of the keys the commands noted apart use through their bytes
|
|
/// ([`noted_apart`](Self::noted_apart)): what a texture whose image aliases a buffer's bytes
|
|
/// names of them too (`Texture::image_use`), which [`continues`](Self::continues) checks.
|
|
bytes_apart: Noted,
|
|
/// A command noted apart uses an image whose memory meets a noted write, or a write noted
|
|
/// since meets an image one uses: the next command recorded into the `VkCommandBuffer` goes
|
|
/// in a new one.
|
|
owed: bool,
|
|
}
|
|
|
|
impl AliasWrites {
|
|
/// What the earlier encoders of the group an encoder joined wrote into its `VkCommandBuffer`,
|
|
/// the group's tail: their order's writes of bytes; nothing where it joined none.
|
|
pub(crate) fn joining(order: Option<&FoldOrder<ResourceKey>>) -> AliasWrites {
|
|
let mut writes = AliasWrites::default();
|
|
for (key, bytes) in order.into_iter().flat_map(FoldOrder::byte_writes) {
|
|
writes.writes.add(key, bytes);
|
|
}
|
|
writes
|
|
}
|
|
|
|
/// Note a command recorded into the `VkCommandBuffer` with `uses`: the bytes it writes, and
|
|
/// whether one meets the memory of an image a command noted apart uses, which runs after it
|
|
/// ([`noted_apart`](Self::noted_apart)): the next command recorded there goes in a new one.
|
|
pub(crate) fn note(&mut self, uses: &[Use<ResourceKey>]) {
|
|
for used in uses.iter().filter(|used| !used.write.is_empty()) {
|
|
if let UseRange::Bytes(bytes) = &used.range {
|
|
self.owed |= self.apart.meets(used.key, bytes);
|
|
self.writes.add(used.key, bytes.clone());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Note a command recorded outside the `VkCommandBuffer` with `uses`, which a later command
|
|
/// there executes (a draw of a pass recorded at its end, into its secondary): the memory its
|
|
/// images use, and whether it meets a noted write. Where it does, now or once a later write
|
|
/// meets it ([`note`](Self::note)), the next command recorded there goes in a new one
|
|
/// ([`meets`](Self::meets)).
|
|
pub(crate) fn noted_apart(&mut self, uses: &[Use<ResourceKey>]) {
|
|
for used in uses {
|
|
match &used.range {
|
|
UseRange::Bytes(bytes) => self.bytes_apart.add(used.key, bytes.clone()),
|
|
range => {
|
|
if let Some(bytes) = range.image_bytes() {
|
|
self.owed |= self.writes.meets(used.key, &bytes);
|
|
self.apart.add(used.key, bytes);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Whether a command with `uses` uses an image whose memory meets bytes a command before it
|
|
/// in the `VkCommandBuffer` wrote, or follows a command noted apart whose images do
|
|
/// ([`noted_apart`](Self::noted_apart)): it goes in a new one (the module docs).
|
|
pub(crate) fn meets(&self, uses: &[Use<ResourceKey>]) -> bool {
|
|
self.owed
|
|
|| (uses.iter()).any(|used| {
|
|
(used.range.image_bytes()).is_some_and(|bytes| self.writes.meets(used.key, &bytes))
|
|
})
|
|
}
|
|
|
|
/// Whether the encoder's pass may continue the render pass instance the tail of the group
|
|
/// `order` describes holds open (`encoder_fold.rs`'s **Continued passes**), which puts what
|
|
/// its pass records into that `VkCommandBuffer`, after what the group's encoders wrote there,
|
|
/// its order's writes of bytes: where no memory its commands noted apart use (its draws',
|
|
/// [`noted_apart`](Self::noted_apart)) nor that of one of `uses` (what else its pass records
|
|
/// there: its attachments, and the copies and steps before its later instances) meets them.
|
|
/// Their uses of bytes count with their images': a texture whose image aliases a buffer's
|
|
/// bytes names them as the buffer's (`Texture::image_use`), as a use through the buffer does,
|
|
/// so either may be an image's. Then those writes are noted as written into the
|
|
/// `VkCommandBuffer` the encoder records into, the tail, as for an encoder that joined the
|
|
/// group ([`joining`](Self::joining)), so a command it records there after its pass that uses
|
|
/// an image over them goes in a new one; else nothing changes, and its pass begins its own
|
|
/// instance in a `VkCommandBuffer` of its own.
|
|
pub(crate) fn continues(
|
|
&mut self,
|
|
order: &FoldOrder<ResourceKey>,
|
|
uses: &[Use<ResourceKey>],
|
|
) -> bool {
|
|
let group = AliasWrites::joining(Some(order)).writes;
|
|
let meets = group.meets_any(&self.apart)
|
|
|| group.meets_any(&self.bytes_apart)
|
|
|| (uses.iter()).any(|used| {
|
|
let bytes = match &used.range {
|
|
UseRange::Bytes(bytes) => Some(bytes.clone()),
|
|
range => range.image_bytes(),
|
|
};
|
|
bytes.is_some_and(|bytes| group.meets(used.key, &bytes))
|
|
});
|
|
if !meets {
|
|
self.writes.join(group);
|
|
}
|
|
!meets
|
|
}
|
|
|
|
/// The encoder went on in a new `VkCommandBuffer`: no command wrote anything there yet, and
|
|
/// nothing is owed. The images noted apart stay: their commands run after what the new one
|
|
/// records before them.
|
|
pub(crate) fn begun(&mut self) {
|
|
self.writes.clear();
|
|
self.owed = false;
|
|
}
|
|
}
|