The cutover's last mechanical step. The old runtime is deleted, so its name is free: the crate moves to crates/metal2vulkan-runtime, its package is metal2vulkan-runtime and its library metal2vulkan_runtime, and every reference in the tree follows (sources, tests, scripts, CI, generated tables and docs). It is now a published crate, as AGENTS.md said it would be once it took the name: version 0.2.0 with the other two, the translator pinned exactly (=0.2.0, as the translator pins the AIR front end), documentation, keywords and categories, and copies of the repository's four license files, which the published-crates test now holds equal to the root's for three crates. CI packages it with the others. Prose that said it was unpublished, or counted two published crates, says three. AGENTS.md: facts only. The crate table row, the published-crates and version sentences, and the paragraph on the old runtime, whose name the runtime now carries (listed for the owner). Made by one script run on this tip. Verified by the job named in the landing note.
711 lines
30 KiB
Rust
711 lines
30 KiB
Rust
//! Textures over a buffer in an image of their own: where the device's linear image does not hold
|
|
//! a texture over a buffer in place ([`Buffer::new_texture`](crate::Buffer::new_texture)), its
|
|
//! texels live in an optimal image of the runtime's, and the device copies the bytes shaders
|
|
//! reach (a Managed buffer's device copy, where it has one) into it and back, one
|
|
//! `vkCmdCopyBufferToImage` or `vkCmdCopyImageToBuffer` region a texture, with no CPU copy. Over a
|
|
//! Managed buffer's device copy, only Metal's Managed events copy ([`ManagedOvers`]); over any
|
|
//! other buffer, the Shared rules:
|
|
//!
|
|
//! - **In, once a submission.** The upload that leads a submission copies the bytes into the
|
|
//! image of every such texture a segment of the submission uses before any command of the
|
|
//! segment wrote the buffer (the segment leans on the leading copy), all between one pair of
|
|
//! barriers. Metal reads Shared
|
|
//! memory when the command buffer executes; every member of a group was committed before the
|
|
//! group was submitted and none has been seen complete, so a CPU write between two members'
|
|
//! commits races an uncompleted command buffer, and Metal does not define which bytes it sees;
|
|
//! the bytes at the group's execution are one of the allowed outcomes. A guest that waits for a
|
|
//! completion before writing has, by that fact, a command buffer not in a held group.
|
|
//! - **In again, after a write of the buffer.** A command that uses the texture after a command
|
|
//! of its segment wrote the buffer copies the bytes in first, as it is encoded. A segment that
|
|
//! leans on the leading copy for a buffer an earlier segment of the held group wrote leads a new
|
|
//! group, the one held going first (`submit`), so its leading copy follows the write.
|
|
//! - **Back, after a write of the texture.** A command that wrote the texture leaves its image to
|
|
//! be copied back: before the next command of the segment that reaches the buffer, that uses
|
|
//! another such texture over bytes it holds, or that reaches memory through an address outside
|
|
//! its resources, and at the segment's end, whose end barrier then reaches the host for a
|
|
//! Shared buffer. A segment's end is a submission's edge, so the next submission's leading copy
|
|
//! reads what the image held.
|
|
//! - **Across textures over the same bytes.** Two textures over one buffer whose bytes meet alias,
|
|
//! as in Metal: a write through one makes the other's image stale, and the other's next use
|
|
//! takes the first's copy back, then its own copy in. Textures over bytes that do not meet owe
|
|
//! each other nothing.
|
|
//!
|
|
//! Each copy is counted (`Metrics::over_copies`, and its bytes in `Metrics::over_copy_bytes`
|
|
//! and `Metrics::bytes_copied`; a copy over a buffer in an imported block in
|
|
//! `Metrics::over_copies_imported` and `Metrics::over_copy_bytes_imported` too). A command's
|
|
//! write of a buffer is tracked by buffer: a write of any of its bytes makes every such texture
|
|
//! over it stale.
|
|
//!
|
|
//! A command hands over what it reaches ([`Reached`]): each texture says itself whether it is such
|
|
//! a texture, and a buffer it only reads owes nothing while no texture the segment used has an
|
|
//! image ahead of its buffer ([`Overs::ahead`]), so a command looks at nothing else. What a
|
|
//! segment wrote is kept in sets by id, so a write costs one lookup however many the segment made.
|
|
|
|
use crate::buffer::BufferObject;
|
|
use crate::copy_out;
|
|
use crate::counters::{Counters, Metric};
|
|
use crate::device::lock;
|
|
use crate::hazard::Access;
|
|
use crate::kept::AddressHasher;
|
|
use crate::record::{memory_barrier, Reach};
|
|
use crate::texture::{Image, OverCopy, TextureInner, TextureObject};
|
|
use ash::vk;
|
|
use std::collections::HashSet;
|
|
use std::hash::BuildHasherDefault;
|
|
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
/// Buffer ids, each once (an id hashes as an object's address does).
|
|
type Ids = HashSet<usize, BuildHasherDefault<AddressHasher>>;
|
|
|
|
/// A resource a command reaches, as the copies read it: a texture, which says itself whether it
|
|
/// lives in an image of its own over a buffer, or a buffer, by its id.
|
|
#[derive(Clone, Copy)]
|
|
pub(crate) enum Reached<'a> {
|
|
/// A texture: one in place over a buffer reaches the buffer.
|
|
Texture(&'a Arc<TextureInner>),
|
|
/// A buffer, by its id ([`BufferObject::id`]).
|
|
Buffer(u32),
|
|
}
|
|
|
|
/// 1 for a texture over a buffer in an image of its own that the Shared rules copy, 0 for any
|
|
/// other or none: what an encoder's count of the ones bound takes for a slot.
|
|
pub(crate) fn copied(texture: Option<&Arc<TextureInner>>) -> u32 {
|
|
u32::from(texture.is_some_and(|texture| texture.copied_shared()))
|
|
}
|
|
|
|
/// What a segment does to its textures over a buffer in an image of their own. Once it has
|
|
/// ended ([`Overs::end`]), what the submit thread reads of it: the copies the leading copy makes,
|
|
/// and the buffers it wrote.
|
|
#[derive(Default)]
|
|
pub(crate) struct Overs {
|
|
/// The textures it used; once it has ended, those that lean on the leading copy alone.
|
|
used: Vec<Used>,
|
|
/// The buffers the used textures lie over, by id.
|
|
under: Ids,
|
|
/// The buffers the segment's commands wrote other than by a copy back, by id.
|
|
wrote: Ids,
|
|
/// The buffers copies back wrote, by id, each with the texture whose copy it was: each pair
|
|
/// once.
|
|
backed: Vec<(u32, u32)>,
|
|
/// A command of the segment wrote through an address outside its resources, or was recorded
|
|
/// while no such texture lived, its writes untracked.
|
|
unbound: bool,
|
|
/// The textures used whose images a command wrote since their bytes last went back
|
|
/// (`Used::buffer_stale`).
|
|
ahead: usize,
|
|
}
|
|
|
|
/// A texture over a buffer in an image of its own that a segment used.
|
|
struct Used {
|
|
/// The texture's id, which its views share.
|
|
texture: u32,
|
|
/// The copy between its buffer and its image.
|
|
copy: OverCopy,
|
|
/// Its buffer's id.
|
|
buffer: u32,
|
|
/// Its first use in the segment came before any write of the buffer in it: the image holds
|
|
/// the bytes of the submission's leading copy.
|
|
leans: bool,
|
|
/// A command wrote the buffer, or another such texture's image over bytes this one holds,
|
|
/// after the image last took its bytes.
|
|
image_stale: bool,
|
|
/// A command wrote the image after its bytes last went back.
|
|
buffer_stale: bool,
|
|
}
|
|
|
|
/// What a held group's ended segments wrote, as [`Overs`] keeps it.
|
|
#[derive(Default)]
|
|
pub(crate) struct Leans {
|
|
wrote: Ids,
|
|
backed: Vec<(u32, u32)>,
|
|
unbound: bool,
|
|
}
|
|
|
|
impl Leans {
|
|
/// Whether `later`, an ended segment after these in one submission, leans on the leading copy
|
|
/// for a buffer these wrote (a texture's own copy back aside): where so, it leads a new group.
|
|
pub(crate) fn conflicts(&self, later: &Overs) -> bool {
|
|
(later.used.iter()).any(|used| {
|
|
self.unbound
|
|
|| self.wrote.contains(&(used.buffer as usize))
|
|
|| (self.backed.iter())
|
|
.any(|&(buffer, by)| buffer == used.buffer && by != used.texture)
|
|
})
|
|
}
|
|
|
|
/// Adds the writes of `later`, an ended segment.
|
|
pub(crate) fn join(&mut self, later: &Overs) {
|
|
self.wrote.extend(&later.wrote);
|
|
self.backed.extend_from_slice(&later.backed);
|
|
self.unbound |= later.unbound;
|
|
}
|
|
}
|
|
|
|
/// The copies a command needs first: each stale buffer back from its image, then each stale image
|
|
/// in from its buffer.
|
|
#[derive(Default)]
|
|
pub(crate) struct Copies {
|
|
pub(crate) back: Vec<OverCopy>,
|
|
pub(crate) into: Vec<OverCopy>,
|
|
}
|
|
|
|
impl Copies {
|
|
pub(crate) fn is_empty(&self) -> bool {
|
|
self.back.is_empty() && self.into.is_empty()
|
|
}
|
|
}
|
|
|
|
impl Overs {
|
|
/// The copies a command that reaches `reached` needs first, in order: each texture over a
|
|
/// buffer in an image of its own taking its image in where a command of the segment wrote its
|
|
/// buffer since, and each buffer reached (a texture in place over one reaches it) taking back
|
|
/// the images of the textures over it a command wrote since, every one where `unbound` (the
|
|
/// command reaches memory through an address outside its resources). While no image is ahead
|
|
/// of its buffer ([`ahead`](Self::ahead)), a buffer reached owes nothing.
|
|
pub(crate) fn before<'a>(
|
|
&mut self,
|
|
reached: impl IntoIterator<Item = (Reached<'a>, Access)>,
|
|
unbound: bool,
|
|
) -> Copies {
|
|
let mut copies = Copies::default();
|
|
for (reached, _) in reached {
|
|
let id = match reached {
|
|
Reached::Texture(texture) => match self.uses(texture, &mut copies) {
|
|
true => continue,
|
|
false => texture.hazard_id(),
|
|
},
|
|
Reached::Buffer(id) => id,
|
|
};
|
|
if self.ahead > 0 {
|
|
self.backs(&mut copies.back, |used| unbound || used.buffer == id);
|
|
}
|
|
}
|
|
if unbound && self.ahead > 0 {
|
|
self.backs(&mut copies.back, |_| true);
|
|
}
|
|
copies
|
|
}
|
|
|
|
/// A command uses `texture`: where it is a texture over a buffer in an image of its own, first
|
|
/// the copy back of each other such texture whose image over bytes this one holds a command
|
|
/// wrote since they last went back, then its copy in where its image is stale, or, at its
|
|
/// first use in the segment, where a command of the segment wrote its buffer; false for any
|
|
/// other texture.
|
|
fn uses(&mut self, texture: &Arc<TextureInner>, copies: &mut Copies) -> bool {
|
|
if !texture.copied_shared() {
|
|
return false;
|
|
}
|
|
let id = texture.id();
|
|
let found = self.used.iter().rposition(|used| used.texture == id);
|
|
if self.ahead > 0 {
|
|
let (buffer, range) = match found {
|
|
Some(at) => (self.used[at].buffer, self.used[at].copy.range),
|
|
None => match texture.copied_over() {
|
|
Some((buffer, copy)) => (buffer.id(), copy.range),
|
|
None => return false,
|
|
},
|
|
};
|
|
// The other image goes back to the buffer, which the copy in reads: this texture's
|
|
// image is stale since that write (`after`), or takes its first copy in after it.
|
|
self.backs(&mut copies.back, |used| {
|
|
used.texture != id && used.buffer == buffer && meets(used.copy.range, range)
|
|
});
|
|
}
|
|
if let Some(at) = found {
|
|
let used = &mut self.used[at];
|
|
if used.image_stale {
|
|
copies.into.push(used.copy);
|
|
used.image_stale = false;
|
|
}
|
|
return true;
|
|
}
|
|
let Some((buffer, copy)) = texture.copied_over() else {
|
|
return false;
|
|
};
|
|
let buffer = buffer.id();
|
|
let wrote = self.unbound
|
|
|| self.wrote.contains(&(buffer as usize))
|
|
|| self.backed.iter().any(|&(backed, _)| backed == buffer);
|
|
if wrote {
|
|
copies.into.push(copy);
|
|
}
|
|
self.under.insert(buffer as usize);
|
|
self.used.push(Used {
|
|
texture: id,
|
|
copy,
|
|
buffer,
|
|
leans: !wrote,
|
|
image_stale: false,
|
|
buffer_stale: false,
|
|
});
|
|
true
|
|
}
|
|
|
|
/// Each used texture's copy back, into `back`, where its image is ahead of its buffer and
|
|
/// `reaches` holds of it.
|
|
fn backs(&mut self, back: &mut Vec<OverCopy>, reaches: impl Fn(&Used) -> bool) {
|
|
for used in &mut self.used {
|
|
if used.buffer_stale && reaches(used) {
|
|
back.push(used.copy);
|
|
used.buffer_stale = false;
|
|
self.ahead -= 1;
|
|
let pair = (used.buffer, used.texture);
|
|
if !self.backed.contains(&pair) {
|
|
self.backed.push(pair);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// After a command that reached `reached`: a texture it wrote is to go back to its buffer,
|
|
/// and every other such texture whose bytes meet its is to take them in again; a buffer it
|
|
/// wrote (a texture in place over one writes it) is to go into every texture over it, all
|
|
/// where it wrote through an address outside its resources (`unbound`).
|
|
pub(crate) fn after<'a>(
|
|
&mut self,
|
|
reached: impl IntoIterator<Item = (Reached<'a>, Access)>,
|
|
unbound: bool,
|
|
) {
|
|
for (reached, access) in reached {
|
|
if !access.writes() {
|
|
continue;
|
|
}
|
|
let buffer = match reached {
|
|
Reached::Texture(texture) => {
|
|
let id = texture.id();
|
|
if texture.copied_shared() {
|
|
if let Some(at) = self.used.iter().rposition(|used| used.texture == id) {
|
|
let (buffer, range) = (self.used[at].buffer, self.used[at].copy.range);
|
|
if !self.used[at].buffer_stale {
|
|
self.used[at].buffer_stale = true;
|
|
self.ahead += 1;
|
|
}
|
|
// The other images over these bytes hold them no longer.
|
|
for used in &mut self.used {
|
|
if used.texture != id
|
|
&& used.buffer == buffer
|
|
&& meets(used.copy.range, range)
|
|
{
|
|
used.image_stale = true;
|
|
}
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
// A texture of an image of its own writes no buffer.
|
|
match texture.hazard_id() {
|
|
hazard if hazard == id => continue,
|
|
buffer => buffer,
|
|
}
|
|
}
|
|
Reached::Buffer(id) => id,
|
|
};
|
|
if self.under.contains(&(buffer as usize)) {
|
|
for used in &mut self.used {
|
|
if used.buffer == buffer {
|
|
used.image_stale = true;
|
|
}
|
|
}
|
|
}
|
|
self.wrote.insert(buffer as usize);
|
|
}
|
|
if unbound {
|
|
self.unbound = true;
|
|
for used in &mut self.used {
|
|
used.image_stale = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Whether a texture the segment used has an image ahead of its buffer: a command wrote it
|
|
/// since its bytes last went back. While none has, a command's reads of buffers owe nothing.
|
|
pub(crate) fn ahead(&self) -> bool {
|
|
self.ahead > 0
|
|
}
|
|
|
|
/// At the segment's end: every image a command wrote goes back to its buffer (the copies,
|
|
/// which the caller records), and the textures kept are those that lean on the leading copy.
|
|
pub(crate) fn end(&mut self) -> Vec<OverCopy> {
|
|
let mut back = Vec::new();
|
|
self.backs(&mut back, |_| true);
|
|
self.used.retain(|used| used.leans);
|
|
back
|
|
}
|
|
|
|
/// Of an ended segment: the copies of the textures whose images the leading copy fills.
|
|
pub(crate) fn leaning(&self) -> impl Iterator<Item = OverCopy> + '_ {
|
|
self.used.iter().map(|used| used.copy)
|
|
}
|
|
}
|
|
|
|
/// Records `copies` into `command`, after every earlier command and before every later one:
|
|
/// first each buffer's bytes back from its image, then each image's bytes in from its buffer, each
|
|
/// set between a pair of barriers; counted. Whether a copy back wrote a Shared buffer, which the
|
|
/// host reads, is the answer.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// `command` is recording outside any render pass, and holds each image and buffer until it
|
|
/// retires; each image is in its home layout between commands.
|
|
pub(crate) unsafe fn record(
|
|
device: &ash::Device,
|
|
command: vk::CommandBuffer,
|
|
copies: &Copies,
|
|
counters: &Counters,
|
|
) -> bool {
|
|
// SAFETY: the caller's contract.
|
|
unsafe {
|
|
record_side(device, command, &copies.back, false, counters);
|
|
record_side(device, command, &copies.into, true, counters);
|
|
}
|
|
copies.back.iter().any(|copy| copy.shared)
|
|
}
|
|
|
|
/// Records `copies` into `command`: each buffer's bytes into its image (`into`), or back, between
|
|
/// one pair of barriers.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// As [`record`].
|
|
pub(crate) unsafe fn record_side(
|
|
device: &ash::Device,
|
|
command: vk::CommandBuffer,
|
|
copies: &[OverCopy],
|
|
into: bool,
|
|
counters: &Counters,
|
|
) {
|
|
use vk::AccessFlags2 as A;
|
|
use vk::PipelineStageFlags2 as S;
|
|
if copies.is_empty() {
|
|
return;
|
|
}
|
|
let (layout, access) = match into {
|
|
true => (vk::ImageLayout::TRANSFER_DST_OPTIMAL, A::TRANSFER_WRITE),
|
|
false => (vk::ImageLayout::TRANSFER_SRC_OPTIMAL, A::TRANSFER_READ),
|
|
};
|
|
let image = |copy: &OverCopy, old, new| {
|
|
vk::ImageMemoryBarrier2::default()
|
|
.old_layout(old)
|
|
.new_layout(new)
|
|
.src_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
|
|
.dst_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
|
|
.image(copy.image)
|
|
.subresource_range(vk::ImageSubresourceRange {
|
|
aspect_mask: copy.aspects,
|
|
base_mip_level: 0,
|
|
level_count: 1,
|
|
base_array_layer: 0,
|
|
layer_count: 1,
|
|
})
|
|
};
|
|
let away: Vec<_> = (copies.iter())
|
|
.map(|copy| {
|
|
image(copy, copy.home, layout)
|
|
.src_stage_mask(S::ALL_COMMANDS)
|
|
.src_access_mask(A::MEMORY_WRITE)
|
|
.dst_stage_mask(S::COPY)
|
|
.dst_access_mask(access)
|
|
})
|
|
.collect();
|
|
let back: Vec<_> = (copies.iter())
|
|
.map(|copy| {
|
|
image(copy, layout, copy.home)
|
|
.src_stage_mask(S::COPY)
|
|
.src_access_mask(A::TRANSFER_WRITE)
|
|
.dst_stage_mask(S::ALL_COMMANDS)
|
|
.dst_access_mask(A::MEMORY_READ | A::MEMORY_WRITE)
|
|
})
|
|
.collect();
|
|
// Every earlier command runs first, its writes visible to the copies and to every later
|
|
// command, as a blit's first barrier makes them; the copies' writes are visible to every
|
|
// later command.
|
|
let earlier = [memory_barrier(Reach::Commands)];
|
|
let later = [vk::MemoryBarrier2::default()
|
|
.src_stage_mask(S::COPY)
|
|
.src_access_mask(A::TRANSFER_WRITE)
|
|
.dst_stage_mask(S::ALL_COMMANDS)
|
|
.dst_access_mask(A::MEMORY_READ | A::MEMORY_WRITE)];
|
|
// SAFETY: the caller's contract; each region lies in its buffer and image, checked at the
|
|
// texture's creation.
|
|
unsafe {
|
|
device.cmd_pipeline_barrier2(
|
|
command,
|
|
&vk::DependencyInfo::default()
|
|
.memory_barriers(&earlier)
|
|
.image_memory_barriers(&away),
|
|
);
|
|
for copy in copies {
|
|
match into {
|
|
true => device.cmd_copy_buffer_to_image(
|
|
command,
|
|
copy.buffer,
|
|
copy.image,
|
|
layout,
|
|
&[copy.region],
|
|
),
|
|
false => device.cmd_copy_image_to_buffer(
|
|
command,
|
|
copy.image,
|
|
layout,
|
|
copy.buffer,
|
|
copy_out::regions(©.unit, copy.region).as_slice(),
|
|
),
|
|
}
|
|
counters.over_copied(copy.bytes, copy.imported, copy.cause);
|
|
}
|
|
device.cmd_pipeline_barrier2(
|
|
command,
|
|
&vk::DependencyInfo::default()
|
|
.memory_barriers(&later)
|
|
.image_memory_barriers(&back),
|
|
);
|
|
}
|
|
counters.count(Metric::Barriers);
|
|
counters.count(Metric::Barriers);
|
|
}
|
|
|
|
/// A copy back a reader of a Managed buffer's device copy takes ([`ManagedOvers::backs`]): the
|
|
/// texture's id, its copy and its image, which the reader's command buffer or upload keeps.
|
|
pub(crate) type Back = (u32, OverCopy, Arc<Image>);
|
|
|
|
/// A texture over a Managed buffer's device copy in an image of its own, as its buffer keeps it
|
|
/// ([`ManagedOvers`]): the copies Metal's Managed events owe it.
|
|
struct Entry {
|
|
/// The texture's id.
|
|
texture: u32,
|
|
/// Its image, held while the entry lives.
|
|
image: Arc<Image>,
|
|
copy: OverCopy,
|
|
/// Its bytes in the buffer: `start..end`.
|
|
range: (u64, u64),
|
|
/// An event named its range since its last copy in: its making, or submitted device work that
|
|
/// wrote the buffer or another texture over a range that meets its.
|
|
into: bool,
|
|
/// Submitted device work wrote it since its last copy back: the upload that leads a
|
|
/// submission may take the copy.
|
|
back: bool,
|
|
/// Device work wrote it since a reader last took its copy back, submitted or not: a reader
|
|
/// recorded after the write takes the copy in its own commands.
|
|
fresh: bool,
|
|
/// The segments that wrote it and are not yet submitted. The upload that leads a submission
|
|
/// runs before them, so it takes no copy back for their writes; each one's submission makes
|
|
/// the copy back owed to it, unless a reader recorded after the write took it. One whose
|
|
/// command buffer is never committed leaves the entry until the buffer is released.
|
|
pending: u32,
|
|
/// The texture is released: the entry lives for its copy back alone, and its id may name a
|
|
/// texture made since, which a lookup by id finds instead.
|
|
released: bool,
|
|
}
|
|
|
|
impl Entry {
|
|
/// Whether it is live texture `texture`'s entry.
|
|
fn of(&self, texture: u32) -> bool {
|
|
self.texture == texture && !self.released
|
|
}
|
|
}
|
|
|
|
/// A Managed buffer's textures over its device copy in an image of their own (`Entry`), each owing
|
|
/// copies only at Metal's own Managed events, with no tracking of commands:
|
|
///
|
|
/// - **In** after a didModifyRange over its range: the capture of the range at the next commit
|
|
/// (`crate::capture`) makes it, after its copies into the device copy, in the same command
|
|
/// buffer. And where device work wrote the buffer (a binding whose compiled interface writes it,
|
|
/// a blit or fill into it) or another texture over a range that meets its, from the submission
|
|
/// after that work's: the upload that leads a submission using the texture makes it. Both copy
|
|
/// from the device copy, never from the consumer's bytes a second time. Apple GPUs may alias
|
|
/// Managed memory; the documented contract, not one GPU's answer, is the rule here.
|
|
/// - **Back** where device work wrote the texture since the last copy back: before the next
|
|
/// reader of the device copy (a binding whose interface reads the buffer, a blit from it, a copy
|
|
/// into another texture over a range that meets its, a synchronize, a capture of a range over
|
|
/// it), not after each write. A reader recorded after the write takes it in its own commands; the
|
|
/// upload that leads a submission takes it only once the write's segment was submitted, as the
|
|
/// upload runs before the submission's commands. A texture released while its copy back is owed
|
|
/// leaves its image with the buffer until a reader takes it or the buffer is released, which
|
|
/// drops it uncopied.
|
|
#[derive(Default)]
|
|
pub(crate) struct ManagedOvers {
|
|
entries: Mutex<Vec<Entry>>,
|
|
/// The entries' count, read without the lock.
|
|
count: AtomicUsize,
|
|
}
|
|
|
|
/// Whether two ranges meet.
|
|
fn meets(a: (u64, u64), b: (u64, u64)) -> bool {
|
|
a.0 < b.1 && b.0 < a.1
|
|
}
|
|
|
|
/// Whether `range` meets one of `ranges`.
|
|
fn meets_any(ranges: &[(u64, u64)], range: (u64, u64)) -> bool {
|
|
ranges.iter().any(|&other| meets(other, range))
|
|
}
|
|
|
|
impl ManagedOvers {
|
|
/// Whether the buffer has an entry.
|
|
pub(crate) fn any(&self) -> bool {
|
|
self.count.load(Relaxed) > 0
|
|
}
|
|
|
|
/// Keeps texture `texture`'s entry: its image, its copy and its bytes' `range` in the buffer,
|
|
/// owing its first copy in.
|
|
pub(crate) fn add(&self, texture: u32, image: Arc<Image>, copy: OverCopy, range: (u64, u64)) {
|
|
let mut entries = lock(&self.entries);
|
|
entries.push(Entry {
|
|
texture,
|
|
image,
|
|
copy,
|
|
range,
|
|
into: true,
|
|
back: false,
|
|
fresh: false,
|
|
pending: 0,
|
|
released: false,
|
|
});
|
|
self.count.store(entries.len(), Relaxed);
|
|
}
|
|
|
|
/// Texture `texture` is released: its entry goes, but for a copy back owed.
|
|
pub(crate) fn release(&self, texture: u32) {
|
|
let mut entries = lock(&self.entries);
|
|
entries.retain_mut(|entry| {
|
|
if entry.of(texture) {
|
|
entry.released = true;
|
|
}
|
|
!entry.released || entry.back || entry.pending > 0
|
|
});
|
|
self.count.store(entries.len(), Relaxed);
|
|
}
|
|
|
|
/// A segment not yet submitted wrote texture `texture`; `first` where it had not written it
|
|
/// before, which its submission settles ([`ManagedOvers::settle`]).
|
|
pub(crate) fn pend(&self, texture: u32, first: bool) {
|
|
if let Some(entry) = lock(&self.entries).iter_mut().find(|e| e.of(texture)) {
|
|
entry.pending += u32::from(first);
|
|
entry.fresh = true;
|
|
}
|
|
}
|
|
|
|
/// A segment that wrote texture `texture` was submitted: the texture owes its copy back where
|
|
/// no reader recorded after the write took it, and each other texture whose range meets its
|
|
/// owes its copy in, which takes that copy back first.
|
|
pub(crate) fn settle(&self, texture: u32) {
|
|
let mut entries = lock(&self.entries);
|
|
let Some(range) = (entries.iter_mut())
|
|
.find(|entry| entry.of(texture))
|
|
.map(|entry| {
|
|
entry.pending = entry.pending.saturating_sub(1);
|
|
entry.back |= entry.fresh;
|
|
entry.range
|
|
})
|
|
else {
|
|
return;
|
|
};
|
|
for entry in entries.iter_mut() {
|
|
if !entry.of(texture) && meets(entry.range, range) {
|
|
entry.into = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Device work wrote the buffer: each texture owes its copy in.
|
|
pub(crate) fn written(&self) {
|
|
for entry in lock(&self.entries).iter_mut() {
|
|
entry.into = true;
|
|
}
|
|
}
|
|
|
|
/// A reader of the device copy comes, over `range` of the buffer (all of it where `None`):
|
|
/// each copy back owed by a texture whose range meets it, taken, into `backs`; with `pending`,
|
|
/// a reader recorded after the writes, also those of writes not yet submitted. A released
|
|
/// texture's entry goes once nothing is owed.
|
|
pub(crate) fn backs(&self, range: Option<(u64, u64)>, pending: bool, backs: &mut Vec<Back>) {
|
|
let mut entries = lock(&self.entries);
|
|
for entry in entries.iter_mut() {
|
|
let owed = entry.back || (pending && entry.fresh);
|
|
if owed && range.is_none_or(|range| meets(entry.range, range)) {
|
|
backs.push((entry.texture, entry.copy, Arc::clone(&entry.image)));
|
|
entry.back = false;
|
|
// A write not yet submitted stays fresh for the readers recorded after it, where
|
|
// the upload took the copy before it runs.
|
|
entry.fresh &= !pending && entry.pending > 0;
|
|
}
|
|
}
|
|
entries.retain(|entry| !entry.released || entry.back || entry.pending > 0);
|
|
self.count.store(entries.len(), Relaxed);
|
|
}
|
|
|
|
/// A capture is to copy `ranges` of the consumer's bytes into the device copy
|
|
/// (`crate::capture`): the copies back owed by submitted writes of textures whose ranges meet
|
|
/// one of `ranges`, or meet a live texture's that does, taken into `backs`, to go ahead of the
|
|
/// capture's copies, so the consumer's bytes land over them.
|
|
pub(crate) fn backs_before(&self, ranges: &[(u64, u64)], backs: &mut Vec<Back>) {
|
|
let mut entries = lock(&self.entries);
|
|
if !entries.iter().any(|entry| entry.back) {
|
|
return;
|
|
}
|
|
let targets: Vec<(u64, u64)> = (entries.iter())
|
|
.filter(|entry| !entry.released && meets_any(ranges, entry.range))
|
|
.map(|entry| entry.range)
|
|
.collect();
|
|
for entry in entries.iter_mut() {
|
|
if entry.back && (meets_any(ranges, entry.range) || meets_any(&targets, entry.range)) {
|
|
backs.push((entry.texture, entry.copy, Arc::clone(&entry.image)));
|
|
entry.back = false;
|
|
// A write not yet submitted stays fresh for the readers recorded after it.
|
|
entry.fresh &= entry.pending > 0;
|
|
}
|
|
}
|
|
entries.retain(|entry| !entry.released || entry.back || entry.pending > 0);
|
|
self.count.store(entries.len(), Relaxed);
|
|
}
|
|
|
|
/// A capture copied `ranges` of the consumer's bytes into the device copy (`crate::capture`):
|
|
/// each live texture whose range meets one of them takes its copy in after those copies, in
|
|
/// the same command buffer, and owes none for the events before it, its copy and image going
|
|
/// to `into`.
|
|
pub(crate) fn copies_in_after(
|
|
&self,
|
|
ranges: &[(u64, u64)],
|
|
into: &mut Vec<(OverCopy, Arc<Image>)>,
|
|
) {
|
|
for entry in lock(&self.entries).iter_mut() {
|
|
if !entry.released && meets_any(ranges, entry.range) {
|
|
entry.into = false;
|
|
into.push((entry.copy, Arc::clone(&entry.image)));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The copy in texture `texture` owes, taken, for the upload that leads a submission: after
|
|
/// the copies back owed by submitted writes of textures whose ranges meet its, its own among
|
|
/// them, into `backs`.
|
|
pub(crate) fn take_into(&self, texture: u32, backs: &mut Vec<Back>) -> Option<OverCopy> {
|
|
let (range, copy) = {
|
|
let mut entries = lock(&self.entries);
|
|
let entry = entries.iter_mut().find(|entry| entry.of(texture))?;
|
|
if !entry.into {
|
|
return None;
|
|
}
|
|
entry.into = false;
|
|
(entry.range, entry.copy)
|
|
};
|
|
self.backs(Some(range), false, backs);
|
|
Some(copy)
|
|
}
|
|
}
|
|
|
|
/// The copies back owed to readers of `buffer`'s device copy over `range` (all of it where
|
|
/// `None`), where it has textures over it in images of their own; with `pending`, a reader
|
|
/// recorded after the writes, also those of writes not yet submitted.
|
|
pub(crate) fn backs(
|
|
buffer: &impl BufferObject,
|
|
range: Option<(u64, u64)>,
|
|
pending: bool,
|
|
) -> Vec<Back> {
|
|
let mut backs = Vec::new();
|
|
if let Some(overs) = buffer.managed_overs() {
|
|
overs.backs(range, pending, &mut backs);
|
|
}
|
|
backs
|
|
}
|