Files
metal2vulkan/crates/metal2vulkan-runtime/src/linear_merge.rs
T
Anees Iqbal e5c37ecc62 Read a self-read through a snapshot of a color's own image where its image lacks STORAGE, pinning the instances through it
On the Iris Xe (m3-max-5702, the self_reads lane) a surface in place over host pages split every
reading draw: 30 splits a frame, 224M texels copied in, 19.5 to 26 ms of GPU a frame, where its
shadow took the snapshot (4.2 to 4.8 ms). Its image lacks STORAGE (the device's check failed
storage writes over its pages), so snapshot_wanted fell back: a later framebuffer fetch of the
color would render it through a stand-in for the whole pass, copied in before the pass, which a
snapshot draw reading the texture's own image would not see.

A draw now reads such a color through a snapshot of its own image all the same, pinning it
(RenderLowering::pins_own_attachment), where nothing the pass's end decides from the whole pass
changes what the instances through the draw render: the pass's first instance recorded inline,
no stand-in, memoryless color or depth/stencil attachment in the pass, every color stored Store,
no tile dispatch, execution, color groups or extended-range shadows, a draw that fetches nothing
and needs no hidden depth. A stand-in arriving after a pin freezes the pins (no later draw pins),
and the pass's end then renders the instances through the last pinned one with the colors' own
images, takes each stand-in's copy of its attachment after the last of them (and a merged
color's keep after that copy), keeps the reloads after them alone and joins no run of passes:
as after an instance recorded inline (render/inline_first.rs).

Counted: WorkCounts::reading_draws_pinned, WorkCounts::stand_ins_after_pins.

Tests: tests::self_read_storage: a private target without STORAGE pins its first pass's reader
and then takes its storage form; a pass whose pinned reader is followed by a fetch of the target
keeps the reader's pixels and halves the fetched ones (a stand-in for the whole pass would leave
the reader the red before the pass).

The self-read cost lane (runtime_lane::attachment_read_costs::self_reads) prints the readers
pinned per frame.

Verified:
- Iris Xe (ANV, Mesa 26.2.6), m3-max-6011: tests::self_read_storage passed both. A private
  BGRA8Unorm target without STORAGE gave (splits, snapshotted, pinned, stand-ins after pins,
  storage forms) = [0, 1, 1, 0, 1] in its first pass and [0, 1, 0, 0, 0] in later passes. A
  fetch after a pinned reader gave [0, 1, 1, 1, 1] in pass 0, with the reader's pixels the pass's
  red and the fetched ones halved.
- lavapipe takes no snapshot path. There, both tests check the split branch ([1, 0, 0, 0, 0]);
  193 filtered lavapipe tests passed in m3-max-5964.
- Clippy plain, --cfg m2v_cpu_icd and --cfg m2v_gpu, and rustdoc -D warnings, for runtime and
  validation: clean (m3-max-5964).
- The Iris lane frame: the device's render-target check now fails over host pages (the next
  commits), so the surface is a shadow and its readers take the snapshot unpinned: 3.94-3.97
  ms/frame on development and on this commit alike (m3-max-6068, three rounds each, each process
  checking on a fresh cache). Pins apply there to a private target without STORAGE and to a
  render target over pages whose check passed.

(cherry picked from commit e0799938e01c14d6b087ce7442bf9acfe3171b1c)
2026-11-07 19:55:49 -08:00

559 lines
26 KiB
Rust

//! A linear attachment's pass rendered through the image its texture keeps, merged back into the
//! surface's bytes word by word (`linear_attachment.rs`).
//!
//! A pass that renders a bound linear attachment through the image its texture keeps copies the
//! surface's box into it and renders there. A copy back of the box would write every pixel of the
//! box, those no draw wrote too, with what the copy in read: where anything else wrote the
//! surface meanwhile (the CPU over its host pages, another queue), the copy back would put the
//! old bytes back, where Metal on a Mac family GPU never writes a pixel no draw wrote (the
//! consumer's x86 guest, its alert's corners and hover fills). So the pass merges instead, with the
//! shadow merge's kernel (`bind::shadow_merge_module`, `shadow_merge.rs`):
//!
//! - **Kept.** After the copy in, the box's texels of the kept image are copied into the *kept*
//! part of a scratch buffer, row after row (each layer's from a multiple of 256 bytes).
//! - **Merged.** After the pass, they are copied into the *changed* part, laid out alike, and the
//! kernel binds the surface's own bytes as its current buffer, over the box's rows: a word whose
//! kept and changed words are equal is neither read nor written; in a word the pass changed,
//! each byte it changed becomes the pass's, and the others keep the surface's. A byte the pass
//! changed and another writer wrote meanwhile is a race Metal leaves unordered.
//! - **Words.** The device's linear rows and their offset are whole words
//! (`VK_NV_linear_color_attachment`'s images). (the 3090's `padded_loads` lane) A texel is 1, 2,
//! 4, 8 or 16 bytes: an `R10UnormX6` or `R8Unorm` box's row may start and end inside a word,
//! where the form refused every pass of it (internal, "a linear attachment whose rows are not
//! whole words"). Each row is taken from its first byte's word to its last byte's, the scratch's
//! rows laid out alike: the copies write the box's texels at their place in those words, and the
//! bytes before and after them are zero in both parts (the scratch is filled with zero once,
//! before the first keep), so the merge keeps the surface's there. A word the pass changed is
//! read and written whole, as one whose texel's channels it changed in part: another writer's
//! write to its other bytes during the merge is a race the merge does not order.
//! - **Words across the invocations.** The kernel gives an invocation four words a workgroup
//! apart, not four in a row, so one store's words are consecutive across the invocations that
//! run together (`bind::shadow_merge`'s module docs). Its dispatches and the words they examine
//! are counted (`WorkCounts::shadow_merge_dispatches_planned`,
//! `WorkCounts::shadow_merge_words_examined`).
//! - **Windows.** The current buffer is bound from the box's first byte rounded down to 256 (the
//! largest `minStorageBufferOffsetAlignment`), its first word pushed; each layer's rows in bands
//! no binding of which passes `maxStorageBufferRange` and no dispatch of which passes
//! `maxComputeWorkGroupCount[0]` workgroups, each band's kept rows starting at a multiple of
//! 256 bytes. One dispatch per band.
//! - **The scratch** (`Device::scratch_buffer`, no zeroing: every word the kernel reads, the
//! copies write) is retained until its command buffer completes, and lent to the merges of its
//! later passes once this pass's merge is recorded (`merge_scratch.rs`): a command buffer of
//! hundreds of passes over one attachment makes a few, not one a pass
//! (`WorkCounts::linear_merge_scratches_made`, `linear_merge_scratches_reused`).
//! - **Runs.** Passes over one attachment one after another, with nothing between them that may
//! touch its bytes or the kept image, keep once and merge once, after the last
//! (`pass_runs.rs`): one copy in, keep and merge a run, not one a pass. A pass of a run that a
//! rebind encodes again alone copies the run's box in again and keeps it after its own merge,
//! for the run's later passes ([`LinearMerge::record_reload`]).
//!
//! - **Reloaded at a barrier.** A draw after a memory barrier inside the pass may read the
//! surface's bytes (a sampled texture over them, a buffer): before the render pass instance that
//! follows the barrier, the words the instances before changed are merged back, the box is
//! copied in again from the surface, and kept again ([`LinearMerge::reload_in`]), so that draw
//! reads what the pass drew, and the merge after the pass writes only what the pass changed
//! since (`render.rs`'s `reload_at`, `render/pass_steps.rs`).
//!
//! It costs the scratch, the copy into the kept part, the copy into the changed part and the
//! dispatches, in place of one copy back of the box; and, at each barrier, a merge, a copy in
//! and a keep.
use super::command_buffer::CommandBuffer;
use super::encoder::EncoderCore;
use super::error::{internal, Error};
use super::merge_scratch::{MergeScratch, Scratch, ScratchKind};
use super::owned_kernel::{OwnedBinds, OwnedKernel, OwnedModule, OwnedSet};
use super::render_pass::Target;
use super::resource::ResourceKey;
use super::tracker::{Access, Barrier, Use};
use crate::device::functions::Vk;
use ash::vk;
use metal2vulkan::bind::{ShadowMergeModule, ShadowMergeStores};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
/// What the offsets of the bindings are rounded to: a multiple of every
/// `minStorageBufferOffsetAlignment`.
const ALIGNMENT: u64 = 256;
/// The stages of a merge's commands: its copy into the changed part and its dispatches.
pub(super) const MERGE_STAGES: vk::PipelineStageFlags2 = vk::PipelineStageFlags2::from_raw(
vk::PipelineStageFlags2::COPY.as_raw() | vk::PipelineStageFlags2::COMPUTE_SHADER.as_raw(),
);
/// One dispatch of the merge: its workgroups and push bytes.
struct Dispatch {
groups: u32,
shape: [u8; 16],
}
/// The merge of one linear attachment's pass (the module docs).
pub(super) struct LinearMerge {
/// The image the pass renders, and where in it the box lies.
backing: vk::Image,
/// The surface's own image, and the copy of the box from it into `backing` (a reload's).
surface: vk::Image,
copy_in: vk::ImageCopy,
/// The kept part's copies, then the changed part's: the same regions, `parts` bytes apart.
regions: Vec<vk::BufferImageCopy>,
/// The scratch's `VkBuffer`, and where its changed part starts past its kept one's.
scratch: vk::Buffer,
part: u64,
/// Where the scratch's two parts lie in its `VkBuffer`, filled with zero before the first
/// keep, where a row of the box starts or ends inside a word (the module's words).
padding: Option<(u64, u64)>,
/// The scratch, and the pool of its command buffer it goes back to as the merge is recorded,
/// once (`lent`).
held: Arc<Scratch>,
pool: Arc<MergeScratch>,
lent: AtomicBool,
kernel: OwnedKernel,
/// One set 0 per dispatch, each binding its band of the kept part, of the changed part and of
/// the surface's bytes; pushed where the kernel's set 0 is pushed (`OwnedBinds`).
binds: OwnedBinds,
dispatches: Vec<Dispatch>,
/// The keep's uses: a read of the image the pass renders.
keep_uses: Vec<Use<ResourceKey>>,
/// The merge's uses: that read, and a read and a write of the surface's bytes.
merge_uses: Vec<Use<ResourceKey>>,
}
impl LinearMerge {
/// The merge of the box `[x0, y0, x1, y1]` (half-open, inside the render area) of the pass
/// rendering `own`, a bound linear attachment's target, through `backing`, the image its
/// texture keeps: its scratch (lent by `maker`'s pool, else made), binds and dispatches on
/// `maker`'s device. Refused (internal) for an attachment whose rows or their offset are not
/// whole words, whose texel is not 1, 2, 4, 8 or 16 bytes, or whose bands no binding holds,
/// which no device's linear attachment is; and as `scratch_buffer` refuses the scratch.
pub(super) fn new(
maker: &CommandBuffer,
backing: &Target,
own: &Target,
[x0, y0, x1, y1]: [u32; 4],
) -> Result<LinearMerge, Error> {
let texels = own
.texture
.buffer_texels()
.ok_or_else(|| internal("a linear attachment merged without its buffer's bytes"))?;
let texel = texels.texel_bytes();
let layout = &texels.layout;
let (width, rows) = (u64::from(x1 - x0), u64::from(y1 - y0));
// Each row from its first byte's word to its last byte's (the module's words): `lead`
// bytes before the box's first texel, and the row's bytes rounded up to a word. A whole
// word holds a whole number of texels of 1, 2 or 4 bytes, and one of 8 or 16 bytes is
// whole words, so `lead` and `row_bytes` are whole texels.
let lead = u64::from(x0) * texel % 4;
let row_bytes = (lead + width * texel).next_multiple_of(4);
if own.level != 0
|| ![1, 2, 4, 8, 16].contains(&texel)
|| !layout.row_pitch.is_multiple_of(4)
|| !layout.offset.is_multiple_of(4)
|| !layout.slice_pitch.is_multiple_of(4)
|| width == 0
|| rows == 0
{
return Err(internal(
"a linear attachment whose rows are not whole words",
));
}
let device = maker.device();
let layers = u64::from(own.layers.max(1));
let layer_part = (rows * row_bytes).next_multiple_of(ALIGNMENT);
let part = layers * layer_part;
// The scratch of the box of every pixel of the target: a box's rows, `lead` bytes and all,
// are no longer than the target's row of whole words, and no more.
let whole_row = (u64::from(own.extent[0]) * texel).next_multiple_of(4);
let whole = 2 * layers * (u64::from(own.extent[1]) * whole_row).next_multiple_of(ALIGNMENT);
let pool = maker.merge_scratch();
let counts = &device.shared().counts;
// An encoder encoded again (`command_buffer/reencode.rs`) borrows none: it takes the
// place of one among the command buffer's encoders, where a run of passes holding the
// scratch they lent may span it (`pass_runs.rs`).
let lent = match crate::replay::is_replaying() {
true => None,
false => pool.take(ScratchKind::Linear, 2 * part),
};
let held = match lent {
Some(lent) => {
counts.linear_merge_scratch(false);
lent
}
None => {
let length = pool.grown(ScratchKind::Linear, 2 * part, whole);
let made = device.scratch_buffer(length)?;
counts.linear_merge_scratch(true);
Scratch::new(made, ScratchKind::Linear, None)
}
};
let (scratch, base) = held.buffer().at(0);
let padding = (lead > 0 || row_bytes > width * texel).then_some((base, 2 * part));
let limits = &device.properties().description.limits;
let range = u64::from(limits.max_storage_buffer_range);
let groups = u64::from(limits.max_compute_work_group_count[0]);
let per_group = u64::from(ShadowMergeModule::LOCAL_SIZE * ShadowMergeModule::WORDS);
let words = row_bytes / 4;
let pitch = layout.row_pitch;
// A band's rows: within every limit, and a whole number of steps, so the next band's kept
// rows start at a multiple of the alignment.
let step = ALIGNMENT / gcd(row_bytes, ALIGNMENT);
let by_current = match range.checked_sub(ALIGNMENT + row_bytes) {
Some(rest) => rest / pitch + 1,
None => 0,
};
let by_kept = range / row_bytes;
let by_groups = groups * per_group / words;
let most = rows.min(by_current).min(by_kept).min(by_groups);
let band = match most == rows {
true => rows,
false => most / step * step,
};
if band == 0 {
return Err(internal("a linear attachment's rows no merge window holds"));
}
let mut regions = Vec::with_capacity(layers as usize);
let mut sets = Vec::new();
let mut dispatches = Vec::new();
// The words the dispatches examine (`WorkCounts::shadow_merge_words_examined`).
let mut examined = 0;
let u32_of = |value: u64| {
u32::try_from(value).map_err(|_| internal("a linear merge past u32 words"))
};
for layer in 0..layers {
let kept_at = base + layer * layer_part;
regions.push(vk::BufferImageCopy {
buffer_offset: kept_at + lead,
buffer_row_length: u32_of(row_bytes / texel)?,
buffer_image_height: y1 - y0,
image_subresource: vk::ImageSubresourceLayers {
aspect_mask: vk::ImageAspectFlags::COLOR,
mip_level: backing.level,
base_array_layer: backing.layer + layer as u32,
layer_count: 1,
},
image_offset: vk::Offset3D {
x: x0 as i32,
y: y0 as i32,
z: 0,
},
image_extent: vk::Extent3D {
width: x1 - x0,
height: y1 - y0,
depth: 1,
},
});
let slice = layout.offset + (u64::from(own.layer) + layer) * layout.slice_pitch;
let mut start = 0;
while start < rows {
let count = band.min(rows - start);
let first = slice + (u64::from(y0) + start) * pitch + u64::from(x0) * texel - lead;
let (surface, at) = texels.buffer.at(first);
let bound = at / ALIGNMENT * ALIGNMENT;
let skip = at - bound;
let kept = kept_at + start * row_bytes;
let size = count * row_bytes;
sets.push(OwnedSet {
images: Vec::new(),
storage_images: Vec::new(),
buffers: vec![
(scratch, kept, size),
(scratch, kept + part, size),
(surface, bound, skip + (count - 1) * pitch + row_bytes),
],
});
let total = count * words;
dispatches.push(Dispatch {
groups: ShadowMergeModule::groups(u32_of(total)?),
shape: ShadowMergeModule::shape(
u32_of(words)?,
u32_of(total)?,
u32_of(pitch / 4)?,
u32_of(skip / 4)?,
),
});
examined += total;
start += count;
}
}
(device.shared().counts).shadow_merge_planned(dispatches.len(), examined);
let kernel = device.owned_kernel(OwnedModule::ShadowMerge(ShadowMergeStores::Words))?;
let binds = OwnedBinds::new(device, &kernel, &sets)?;
let levels = backing.level..backing.level + 1;
let backing_layers = backing.layer..backing.layer + backing.layers.max(1);
let read = Access::new(
vk::PipelineStageFlags2::COPY,
vk::AccessFlags2::TRANSFER_READ,
);
let backing_read = backing
.texture
.image_use(levels, backing_layers, read, false)?;
let storage = |access| Access::new(vk::PipelineStageFlags2::COMPUTE_SHADER, access);
let surface = own.texture.bound_use(
storage(vk::AccessFlags2::SHADER_STORAGE_READ),
storage(vk::AccessFlags2::SHADER_STORAGE_WRITE),
);
let layers_of = |target: &Target| vk::ImageSubresourceLayers {
aspect_mask: vk::ImageAspectFlags::COLOR,
mip_level: target.level,
base_array_layer: target.layer,
layer_count: own.layers.max(1),
};
let offset = vk::Offset3D {
x: x0 as i32,
y: y0 as i32,
z: 0,
};
let copy_in = vk::ImageCopy {
src_subresource: layers_of(own),
src_offset: offset,
dst_subresource: layers_of(backing),
dst_offset: offset,
extent: vk::Extent3D {
width: x1 - x0,
height: y1 - y0,
depth: 1,
},
};
Ok(LinearMerge {
backing: backing.texture.raw_image()?,
surface: own.texture.raw_image()?,
copy_in,
regions,
scratch,
part,
padding,
held,
pool: Arc::clone(pool),
lent: AtomicBool::new(false),
kernel,
binds,
dispatches,
keep_uses: vec![backing_read.clone()],
merge_uses: vec![backing_read, surface],
})
}
/// Record the keep in `core`'s command buffer: after the copy in, before the pass, between
/// barriers from everything before and to everything after; first, where rows start or end
/// inside a word, the scratch's fill with zero.
pub(super) fn record_keep(&self, core: &mut EncoderCore) {
if let Some((offset, size)) = self.padding {
core.command(vk::PipelineStageFlags2::COPY, &[], |device, raw| {
// SAFETY: the encoder's command buffer is recording outside a render pass; the
// scratch is the merge's, retained with it, a transfer destination, and the
// range is its two parts, whole words.
unsafe { device.cmd_fill_buffer(raw, self.scratch, offset, size, 0) }
});
}
core.barrier(Barrier::everything());
core.command(
vk::PipelineStageFlags2::COPY,
&self.keep_uses,
|device, raw| {
// SAFETY: the encoder's command buffer is recording outside a render pass, and
// the encoder retains the merge and the textures until it completes.
unsafe { self.keep_in(device, raw) }
},
);
core.barrier(Barrier::everything());
}
/// Record the merge in `core`'s command buffer: after the pass, in place of its copy back,
/// between barriers from everything before and to everything after; then lend the scratch to
/// the command buffer's later merges, whose commands that barrier precedes
/// (`merge_scratch.rs`).
pub(super) fn record_merge(&self, core: &mut EncoderCore) {
core.barrier(Barrier::everything());
core.command(MERGE_STAGES, &self.merge_uses, |device, raw| {
// SAFETY: as in `record_keep`.
unsafe { self.merge_in(device, raw) }
});
core.barrier(Barrier::everything());
core.device().shared().counts.linear_merge_recorded();
self.lend();
}
/// Record in `core`'s command buffer, after a pass of the merge's run encoded again alone that
/// a later pass of the run went on over (`pass_runs.rs`'s **Encoded again**), the box's copy in
/// from the surface, whose bytes that pass's merge and the merge before it left holding what
/// every pass before wrote, and its keep, between barriers: the run's later passes render
/// over what Metal's would load, and its merge writes only what they change. Its uses are
/// that pass's own copy in's, keep's and merge's: the same images.
pub(super) fn record_reload(&self, core: &mut EncoderCore) {
core.barrier(Barrier::everything());
core.command(vk::PipelineStageFlags2::COPY, &[], |device, raw| {
// SAFETY: the encoder's command buffer is recording outside a render pass; the copy's
// region lies in both images (the box, inside the render area), which rest in
// `GENERAL` with the transfer usage the copies in take, and which the merge's run
// retains until it completes.
unsafe {
device.cmd_copy_image(
raw,
self.surface,
vk::ImageLayout::GENERAL,
self.backing,
vk::ImageLayout::GENERAL,
&[self.copy_in],
);
}
});
self.record_keep(core);
core.device().shared().counts.linear_run_reloaded();
}
/// Lend the scratch to the command buffer's later merges, once: the last command that
/// touches it lies before every command recorded from here on (`merge_scratch.rs`).
pub(super) fn lend(&self) {
if !self.lent.swap(true, Ordering::AcqRel) {
self.pool.give(&self.held);
}
}
/// The merge's uses, which a merge left to its batch's submission notes as its pass ends
/// (`pass_runs.rs`): a read of the image the pass renders, and a read and a write of the
/// surface's bytes.
pub(super) fn merge_uses(&self) -> &[Use<ResourceKey>] {
&self.merge_uses
}
/// Record the merge into `command_buffer` on its own, between barriers from everything
/// before and to everything after: a run's, at its batch's submission (`pass_runs.rs`).
///
/// # Safety
///
/// As for [`LinearMerge::reload_in`].
pub(super) unsafe fn record_alone(&self, device: &Vk, command_buffer: vk::CommandBuffer) {
// SAFETY: the caller's contract.
unsafe {
Barrier::everything().record(device, command_buffer);
self.merge_in(device, command_buffer);
Barrier::everything().record(device, command_buffer);
}
}
/// Record, into `command_buffer` between two render pass instances of the pass, the merge of
/// what the instances before changed, the box's copy in from the surface again, and its
/// keep, each between barriers (the module docs). The pass's uses hold the keep's and the
/// merge's: the steps of its first copy in, keep and last merge.
///
/// # Safety
///
/// `command_buffer` is recording outside a render pass, and only the caller records it; the
/// merge, the images and the scratch live until it completes, and every image rests in
/// `GENERAL`.
pub(super) unsafe fn reload_in(&self, device: &Vk, command_buffer: vk::CommandBuffer) {
// SAFETY: the caller's contract; the copy's region lies in both images (the box, inside
// the render area), whose transfer usage the copies in already take.
unsafe {
Barrier::everything().record(device, command_buffer);
self.merge_in(device, command_buffer);
Barrier::everything().record(device, command_buffer);
device.cmd_copy_image(
command_buffer,
self.surface,
vk::ImageLayout::GENERAL,
self.backing,
vk::ImageLayout::GENERAL,
&[self.copy_in],
);
Barrier::everything().record(device, command_buffer);
self.keep_in(device, command_buffer);
Barrier::everything().record(device, command_buffer);
}
}
/// Record into `command_buffer`, between two render pass instances of the pass, the keep
/// that follows its stand-in's copy in there, where the instances before rendered the
/// attachment itself (`render/snapshot.rs`'s pins), as [`record_keep`](Self::record_keep)
/// records it before the pass: first, where it has one, the scratch's fill with zero; each
/// between barriers. The pass's uses hold the keep's ([`keep_uses`](Self::keep_uses)).
///
/// # Safety
///
/// As for [`LinearMerge::reload_in`].
pub(super) unsafe fn keep_in_pass(&self, device: &Vk, command_buffer: vk::CommandBuffer) {
// SAFETY: the caller's contract; the scratch is the merge's, a transfer destination, and
// the filled range is its two parts, whole words.
unsafe {
Barrier::everything().record(device, command_buffer);
if let Some((offset, size)) = self.padding {
device.cmd_fill_buffer(command_buffer, self.scratch, offset, size, 0);
Barrier::everything().record(device, command_buffer);
}
self.keep_in(device, command_buffer);
Barrier::everything().record(device, command_buffer);
}
}
/// The keep's uses: the kept image's read.
pub(super) fn keep_uses(&self) -> &[Use<ResourceKey>] {
&self.keep_uses
}
/// The keep's copy.
///
/// # Safety
///
/// As for [`LinearMerge::reload_in`].
unsafe fn keep_in(&self, device: &ash::Device, raw: vk::CommandBuffer) {
// SAFETY: the caller's contract; the image rests in `GENERAL` with transfer source usage
// (the kept image's), and each region lies in it and in the scratch's kept part.
unsafe {
device.cmd_copy_image_to_buffer(
raw,
self.backing,
vk::ImageLayout::GENERAL,
self.scratch,
&self.regions,
);
}
}
/// The merge's copy into the changed part and its dispatches, a barrier between.
///
/// # Safety
///
/// As for [`LinearMerge::reload_in`].
unsafe fn merge_in(&self, device: &Vk, raw: vk::CommandBuffer) {
let changed: Vec<vk::BufferImageCopy> = self
.regions
.iter()
.map(|region| vk::BufferImageCopy {
buffer_offset: region.buffer_offset + self.part,
..*region
})
.collect();
// SAFETY: as in `keep_in`; the binds name bands inside the scratch's parts and the
// surface's bytes, one per dispatch, and the kernel's push range holds the shape.
unsafe {
device.cmd_copy_image_to_buffer(
raw,
self.backing,
vk::ImageLayout::GENERAL,
self.scratch,
&changed,
);
Barrier::everything().record(device, raw);
device.cmd_bind_pipeline(raw, vk::PipelineBindPoint::COMPUTE, self.kernel.pipeline);
for (index, dispatch) in self.dispatches.iter().enumerate() {
self.binds.bind(device, raw, &self.kernel, index);
device.cmd_push_constants(
raw,
self.kernel.layout,
vk::ShaderStageFlags::COMPUTE,
0,
&dispatch.shape,
);
device.cmd_dispatch(raw, dispatch.groups, 1, 1);
}
}
}
}
/// The greatest common divisor of `a` and `b`.
pub(super) fn gcd(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
(a, b) = (b, a % b);
}
a
}