RUNTIME 2355: Textures over buffers: a buffer of 16 KiB or more holds Metal's allocation, a heap's buffer its heap

A texture over a buffer whose texels run past the length was always a
copy: a shadow image or a staged texture buffer whose bytes past the
length lay in a tail of its own. Metal's texels there are the
allocation's: a buffer of 16 KiB or more holds its length rounded up to
16 KiB (allocatedSize), zero when made, and a heap's buffer reaches its
heap's end, whose bytes are the heap's.

Now a buffer the device makes in the Metal address space with a length
of 16 KiB or more takes that allocation (buffer.rs's metal_allocation):
its VkBuffer and placement are its length rounded up to 16 KiB, which
its first fill clears with the rest (Buffer::clear_first: the host's
clear of a Shared or Managed buffer, the device's fill of a Private
one), with no command of its own. A texture keeps in place the bytes the
buffer's memory holds for it (Buffer::texel_reach: the allocation, a
heap's buffer's heap to its end, a no-copy buffer's length), so such a
texture is made as one inside the length is: an image over the buffer's
memory where the device makes one, a texture buffer viewed in place. The
shader-visible ranges, robustness, copy reach (a blit past the length is
still refused) and exported sizes stay at the length. Heap, no-copy,
wrapped and exportable buffers, and every buffer under 16 KiB, take
their length; past what the memory holds a texture keeps its tail, as
before. The runtime has no buffer allocatedSize; a heap's size and a heap
texture's allocated_size are unchanged.

The tails conformance cases now lie in place: their comments say so,
and the texture buffer case drops its staged-texture-buffer allowance,
which no device can fire there any more (its view starts at 16384).

Tests: buffer_tail_device's last-texel case lies as over a buffer that
holds it inside its length, with no tail; a new case over a 20000-byte
buffer (64x128 packed, ending at 32768) reads zero in the padding as
made, lies as inside the length, lands a kernel's writes past the length
in the buffer's memory, takes no copy where it is an image over the
buffer, and refuses a blit past the length; the heap case lies in the
heap, its kernel writes landing in the heap's other buffer; a new case
over a heap's second buffer keeps its texels past the heap's end in a
tail; the no-copy and 4096-byte cases keep their tails.

Docs: buffer_texture.rs's Tails, linear_plan.rs's Length, docs/API.md's
accepted difference and StagedTextureBuffer entry, and a FAST-PATHS
cost-of-matching-Metal entry for the padding.

Checks: the jobs that compiled and ran this commit are named in the
lane's report.

Evidence (lane jobs): m3-max-32551 (msi): clippy runtime+conformance -D warnings 0; 2632/2632 runtime+conformance; fast_paths +
api_surface 34/34. m3-max-32578 (lavapipe) at the tip: touched 56/56 (buffer_tail_device incl. the allocation, heap and
heap-end cases, texture_placement_device, linear_plan, buffer_texture, degradation_catalogue); runtime+validation partition
1/4 1572/1572. VkMemoryRequirements on lavapipe for a 16385-byte buffer: 32768 (alignment 64). The padding is cleared by the
buffer's existing first fill (no new command). allocatedSize: no buffer API; heap sizes unchanged. The PHOENIX run, the
release creation bench and lavapipe partitions 2-4 follow in the lane. Conformance: buffer-tails cases now in place; the M3
venue's cost records for buffer-tails-* re-bless on development.

Lane commits: d2ee5bca5b (bench), a8f3796f59, ee54952340 (heap cases in a placement heap).
This commit is contained in:
Anees Iqbal committed 2026-11-15 08:14:12 -08:00
1 parent b3113687a8
commit a1175662e7
24 files changed
+644 -203

No files matched your search

@@ -9,9 +9,10 @@
//! holds before anything writes it is the allocation's: over a buffer of 4096 bytes, whose
//! `allocatedSize` is its length, it is the buffer allocated next, which no case can state; over
//! a buffer of 20000 bytes, whose `allocatedSize` is 32768, it is the rest of the buffer's own
//! allocation, and it reads zero. The runtime keeps those texels in a tail of the texture's own
//! (`metal2vulkan-runtime`'s `buffer_texture.rs`), zero when made, so this family asks the second
//! shape, where Metal's answer is one a case can compare: a 16 by 256 `RGBA8Uint` texture at
//! allocation, and it reads zero. The runtime's lie in the buffer's memory too, as a buffer of
//! 16 KiB or more holds its allocation, its length rounded up to 16 KiB, zero when made
//! (`metal2vulkan-runtime`'s `buffer_texture.rs`), so this family asks the second shape, where
//! Metal's answer is one a case can compare: a 16 by 256 `RGBA8Uint` texture at
//! offset 4096, its rows 64 bytes apart, over 20000 bytes. Its rows 0 to 247 lie inside the
//! length, row 248 crosses it 32 bytes (8 texels) in, and rows 249 to 255 lie past it.
//!
@@ -23,10 +24,11 @@
//! (wholly past it), copies rows 240 to 255 into a texture of its own, and reads every one of
//! those back, with the buffer's bytes.
//!
//! Every byte compares exactly. What differs is the announcing: the runtime keeps a texture with
//! a tail as a shadow image and announces `shadowed-texture`, which Metal never does, and a
//! device without unified memory announces `shared-in-system-memory` for the `Shared` buffers the
//! answers are read back through. Nothing here can fault a GPU: one small texture, one thread a
//! Every byte compares exactly. What differs is the announcing: where the device makes no image
//! over the buffer's memory on Metal's layout, the runtime keeps the texture as a shadow image and
//! announces `shadowed-texture`, which Metal never does, and a device without unified memory
//! announces `shared-in-system-memory` for the `Shared` buffers the answers are read back
//! through. Nothing here can fault a GPU: one small texture, one thread a
//! texel, no loop.
use crate::allowance::Allowance;
@@ -84,8 +86,9 @@ fn written(n: u64, set: u64) -> Vec<u8> {
/// What both cases loosen: the notices, and nothing else.
fn allowances() -> Vec<Allowance> {
// The runtime keeps a texture whose texels run past its buffer's length as a shadow image of
// the buffer's bytes and its tail, and announces `shadowed-texture`, which Metal never does.
// Where the device makes no image over the buffer's memory on Metal's layout, the runtime
// keeps the texture as a shadow image of the buffer's bytes, and announces
// `shadowed-texture`, which Metal never does.
// Every case reads its answers back through `Shared` buffers, so a device without unified
// memory announces `shared-in-system-memory` (`crate::replay::shared_memory_notice`).
vec![
@@ -9,9 +9,10 @@
//! (`probes/texture_buffer_past_length.m`, an Apple M3 Max under macOS 27.0.1). Over a 4096-byte
//! buffer, whose `allocatedSize` is its length, that memory is the buffer allocated next, which no
//! case can state; over a 20000-byte buffer, whose `allocatedSize` is 32768, it is the rest of the
//! buffer's own allocation, and it reads zero. The runtime keeps those texels in a tail of the
//! texture buffer's own, zero when made, which its functions view. So the case asks the second
//! shape: 1024 `R32Uint` texels from byte 16384 of a 20000-byte buffer, the first 904 inside the
//! buffer's own allocation, and it reads zero. The runtime's lie in the buffer's memory too, as a
//! buffer of 16 KiB or more holds its allocation, its length rounded up to 16 KiB, zero when
//! made, which its functions view in place. So the case asks the second shape: 1024 `R32Uint`
//! texels from byte 16384 of a 20000-byte buffer, the first 904 inside the
//! length and the last 120 past it.
//!
//! `buffer-tails-texture-buffer` reads texels through `ob1_texel_buffer` (the owned fixture whose
@@ -22,10 +23,9 @@
//! reads it into a buffer as made, writes texels 880 to 943 by a blit from a buffer, and reads it
//! back by a blit and by `getBytes:`, with the buffer's bytes.
//!
//! Every byte compares exactly. What differs is the announcing: the runtime's functions view such
//! a texture buffer's tail as a staging buffer and it announces `staged-texture-buffer`, which
//! Metal never does, and a device without unified memory announces `shared-in-system-memory` for
//! the `Shared` buffers the answers are read back through. Nothing here can fault a GPU: one
//! Every byte compares exactly. What differs is the announcing: a device without unified memory
//! announces `shared-in-system-memory` for the `Shared` buffers the answers are read back
//! through. Nothing here can fault a GPU: one
//! small texture buffer, eight threads, no loop.
use crate::allowance::Allowance;
@@ -298,17 +298,9 @@ impl Case for TextureBufferTails {
}
fn allowances(&self) -> Vec<Allowance> {
// The runtime's functions view a texture buffer whose texels run past its buffer's length
// through its tail, a staging buffer, and it announces `staged-texture-buffer`, which
// Metal never does. Every answer is read back through `Shared` buffers, so a device
// without unified memory announces `shared-in-system-memory`
// (`crate::replay::shared_memory_notice`).
vec![
crate::replay::shared_memory_notice(),
crate::replay::notice_allowance(
metal2vulkan_runtime::Degradation::StagedTextureBuffer.code(),
),
]
// Every answer is read back through `Shared` buffers, so a device without unified memory
// announces `shared-in-system-memory` (`crate::replay::shared_memory_notice`).
vec![crate::replay::shared_memory_notice()]
}
fn shaders(&self) -> Result<Vec<ShaderRef>, String> {
+5 -4
View File
@@ -718,15 +718,16 @@ impl BlitCommandEncoder {
/// The copy of a `Managed` texture's bytes over a buffer to the buffer's host copy, for
/// `synchronize_resource` and `synchronize_texture`: where the GPU or the CPU finds the texels
/// (an IOSurface plane's two layouts), in the buffer. Those past its length lie in the
/// texture's `Shared` tail, which takes no synchronization (`buffer_texture.rs`), and a texture
/// of an image alone takes none, its CPU access copying from its image.
/// (an IOSurface plane's two layouts), in the buffer's memory, past its length too where that
/// holds them (`Texels::kept`). Those past it lie in the texture's `Shared` tail, which takes
/// no synchronization (`buffer_texture.rs`), and a texture of an image alone takes none, its
/// CPU access copying from its image.
pub(super) fn synchronize_texels(&mut self, texture: &Texture) -> Result<(), Error> {
let Some(texels) = texture.buffer_texels() else {
return Ok(());
};
let span = texels.span();
let end = span.end.min(texels.buffer.length());
let end = span.end.min(texels.kept());
self.synchronize_bytes(&texels.buffer, span.start.min(end)..end)
}
+2 -1
View File
@@ -921,7 +921,8 @@ impl RowsCopy {
}
/// This copy cut where a side's bytes pass `src_cut` or `dst_cut` (a texture's whose texels
/// run past its buffer's length, whose bytes past it lie in its tail, `buffer_texture.rs`):
/// run past those it keeps in its buffer, whose bytes past them lie in its tail,
/// `buffer_texture.rs`):
/// up to four copies, each `(whether its source rows lie past the source's cut, whether its
/// destination rows lie past the destination's, the copy)`, every row in exactly one of
/// them, cut where it crosses either cut. A side with no cut lies before it whole.
@@ -104,8 +104,8 @@ use std::sync::Arc;
/// One side of a copy of rows (`BlitCommandEncoder::rows_copy`): the buffer whose bytes its uses
/// name, and the buffer its rows lie in, `rebase` bytes before their place in the first one's
/// space: the buffer itself, or for a texture's bytes past its buffer's length the texture's tail
/// (`buffer_texture::Past`).
/// space: the buffer itself, or for a texture's bytes past those it keeps in its buffer the
/// texture's tail (`buffer_texture::Past`).
#[derive(Clone, Copy)]
struct RowsSide<'a> {
uses: &'a Buffer,
@@ -1966,8 +1966,9 @@ impl BlitCommandEncoder {
};
let layout =
texel_layout(&texels.layout, &unit, bytes, bytes.layer + layer);
// Texels past the buffer's length lie in its tail: the rows go through a
// scratch buffer of the encoder, as rows of the bytes they lie in.
// Texels past those kept in the buffer lie in its tail: the rows go
// through a scratch buffer of the encoder, as rows of the bytes they lie
// in.
if let Some(past) = texels.past() {
let [width, height, _] = bytes.size;
let row = u64::from(width) * u64::from(unit.bytes);
@@ -2022,7 +2023,7 @@ impl BlitCommandEncoder {
&region,
&image_unit,
&layout,
buffer.length(),
texels.kept(),
buffer.start(),
into_texture,
)
@@ -2041,7 +2042,7 @@ impl BlitCommandEncoder {
}
/// Record `copy` from `source`'s bytes into `destination`'s, as [`rows_copy`](Self::rows_copy)
/// does, where a side's bytes past its buffer's length lie in a texture's tail (`past`,
/// does, where a side's bytes past those kept in its buffer lie in a texture's tail (`past`,
/// `buffer_texture.rs`): the rows cut there, each part from and into the bytes it lies in,
/// its uses those of the buffer's bytes it stands for.
fn texels_rows_copy(
+77 -8
View File
@@ -38,6 +38,7 @@ use super::host_aliases::Alias;
use super::host_memory::{HeldBack, HostParts, KeepAlive, Release};
use super::host_ram::RamOnDevice;
use super::host_residency::{Again, Residency};
use super::linear_plan::METAL_PAGE;
use super::managed_copy::{CallerCopy, SyncTarget};
use super::memory::{
BoundMemory, Holds, Mapped, MemoryClass, Placement, BLOCK_BYTES, FD_BLOCKS_HANDLE,
@@ -99,6 +100,12 @@ struct BufferShared {
/// Its identity (`object_id`).
object_id: ObjectId,
length: u64,
/// The bytes from byte 0 that Metal's allocation of the buffer holds as its own, which a
/// texture over it reaches in place ([`Buffer::texel_reach`]): for a buffer of at least
/// 16 KiB the program makes of the runtime's memory in the Metal address space, its length
/// rounded up to 16 KiB (Metal's `allocatedSize`), its `VkBuffer` and placement that long and
/// zero when made (`Device::placed_new_buffer`); its length for every other buffer.
allocation: u64,
/// The options with the hazard tracking mode resolved, as `resourceOptions` reports them.
options: ResourceOptions,
address: GpuAddress,
@@ -2754,6 +2761,11 @@ pub(super) struct BufferParts {
pub(super) offset: u64,
pub(super) base: u64,
pub(super) length: u64,
/// The bytes from byte 0 that Metal's allocation of it holds as its own (`BufferShared`'s
/// `allocation`): its length, or for a buffer of at least 16 KiB the program makes of the
/// runtime's memory in the Metal address space, its length rounded up to 16 KiB, which its
/// `VkBuffer` holds.
pub(super) allocation: u64,
/// The bytes from byte 0 a copy reads in the `VkBuffer` ([`Buffer::copy_reach`]): the
/// length, or a no-copy buffer's bytes to the end of its `VkBuffer` in its pages.
pub(super) reach: u64,
@@ -3296,8 +3308,10 @@ impl Device {
let lead = aligned.map_or(0, |byte| {
(LINEAR_IMAGE_ALIGNMENT - byte % LINEAR_IMAGE_ALIGNMENT) % LINEAR_IMAGE_ALIGNMENT
});
let allocation =
metal_allocation(length, of, placement, self.properties().max_buffer_length);
// Padded to 16 bytes, so a device fill clears every byte too.
let size = length
let size = allocation
.checked_add(lead)
.and_then(|size| size.checked_next_multiple_of(16))
.ok_or_else(|| nil(call, Problem::BufferLength { length }))?;
@@ -3477,6 +3491,7 @@ impl Device {
offset: lead,
base: lead,
length,
allocation,
reach: length,
modes,
address,
@@ -3677,6 +3692,7 @@ impl Device {
offset: 0,
base: 0,
length: required.size,
allocation: required.size,
reach: required.size,
modes,
address,
@@ -3699,6 +3715,7 @@ impl Device {
offset,
base,
length,
allocation,
reach,
modes,
address,
@@ -3747,7 +3764,7 @@ impl Device {
mirror,
range_token: KeptToken::default(),
};
self.buffer_lying(own, length, modes, address, naming)
self.buffer_lying(own, (length, allocation), modes, address, naming)
}
/// The import of a no-copy buffer's `pages`: one the device releases while idle where
@@ -3800,7 +3817,7 @@ impl Device {
mirror: None,
range_token: KeptToken::default(),
};
self.buffer_lying(own, length, modes, address, naming)
self.buffer_lying(own, (length, length), modes, address, naming)
}
/// A no-copy buffer's lie, of `key`: pages of its own in `import`, released to `release`
@@ -3836,11 +3853,12 @@ impl Device {
Lie::Host(cell)
}
/// The buffer of `own`'s bytes, `length` long, of `modes`, at `address`, named by `naming`.
/// The buffer of `own`'s bytes, `length` long, Metal's allocation of it holding `allocation`
/// bytes, of `modes`, at `address`, named by `naming`.
fn buffer_lying(
&self,
own: OwnBytes,
length: u64,
(length, allocation): (u64, u64),
modes: Modes,
address: GpuAddress,
naming: Naming,
@@ -3861,6 +3879,7 @@ impl Device {
generation: std::ptr::from_ref(self.shared()).addr(),
object_id,
length,
allocation,
options,
address,
attributes,
@@ -4098,6 +4117,31 @@ pub(super) enum NewBytes {
Zero,
}
/// The bytes Metal's allocation of a buffer of `length` made for `of` at `placement` holds as its
/// own (`BufferShared`'s `allocation`): a buffer of at least 16 KiB the program makes of the
/// runtime's memory (`NewBufferOf::Runtime(FreshPages::WherePaid)`, `NewBufferOf::Unobserved`) at
/// a range of the Metal address space (`AddressPlacement::Any` or a pin) holds its length rounded
/// up to 16 KiB, its `allocatedSize` on Metal, where that stays within `max_length`; every other
/// buffer its length: a shorter one, whose Metal allocation is its length (or a slot of a pool),
/// a heap's own, the runtime's own outside the space, an indirect command buffer's records and
/// the in-place check's.
fn metal_allocation(length: u64, of: NewBufferOf, placement: Place, max_length: u64) -> u64 {
let program = matches!(
of,
NewBufferOf::Runtime(FreshPages::WherePaid) | NewBufferOf::Unobserved
);
let in_space = matches!(
placement,
Place::At(AddressPlacement::Any | AddressPlacement::At(_))
);
match program && in_space && length >= METAL_PAGE {
true => (length.checked_next_multiple_of(METAL_PAGE))
.filter(|&allocation| allocation <= max_length)
.unwrap_or(length),
false => length,
}
}
/// The memory class of a buffer of `modes`' storage and CPU cache modes, for `call`: refused for
/// `Memoryless`, which is no buffer's. `Shared` storage is `MemoryClass::WriteCombined` in the
/// write-combined CPU cache mode, which the host does not read.
@@ -4128,6 +4172,7 @@ impl Heap {
generation: std::ptr::from_ref(heap_bytes.generation().shared()).addr(),
object_id: ObjectId::new(),
length,
allocation: length,
options,
address: GpuAddress(heap_bytes.gpu_address().0 + offset),
attributes: ResourceAttributes::new(),
@@ -4289,9 +4334,31 @@ impl Buffer {
}
}
/// The length padded to 16 bytes: the bytes the `VkBuffer` holds from [`Buffer::base`].
/// Metal's allocation of the buffer padded to 16 bytes (its length, or its length rounded
/// up to 16 KiB, `BufferShared`'s `allocation`): the bytes the `VkBuffer` holds from
/// [`Buffer::base`], which its first fill clears.
pub(super) fn padded_length(&self) -> u64 {
self.s().length.next_multiple_of(16)
self.s().allocation.next_multiple_of(16)
}
/// The bytes from byte 0 that a texture over the buffer reaches in place in its memory
/// (`buffer_texture.rs`'s **Tails**; past them its texels lie in its tail): Metal's
/// allocation of a buffer of its own (its length, or its length rounded up to 16 KiB,
/// `BufferShared`'s `allocation`), a no-copy buffer's length, whose pages past it are the
/// caller's, and a heap's buffer's heap to its end, as Metal's texels there are the heap's
/// bytes.
pub(super) fn texel_reach(&self) -> u64 {
match &self.s().bytes {
Bytes::Own(_) => self.s().allocation,
Bytes::Heap(bytes) => bytes.bytes.texel_reach().saturating_sub(bytes.offset),
}
}
/// The bytes from byte 0 an image over a texture's texels in the buffer's own memory may
/// take (`texture.rs`'s aliasing image): those a texture reaches
/// ([`texel_reach`](Self::texel_reach)) and the `VkBuffer`'s padding to 16 bytes past them.
pub(super) fn texel_room(&self) -> u64 {
self.padded_length().max(self.texel_reach())
}
/// The bytes of the buffer's memory from its byte 0 ([`Buffer::memory`]'s offset) to the end
@@ -5625,7 +5692,7 @@ impl Buffer {
let modes = Modes::of(last.options).map_err(|_| Left::Failed)?;
let class = buffer_class("carry", modes).map_err(|_| Left::Failed)?;
let (length, lead, reach) = (last.length, fixed.pages.offset, fixed.pages.reach);
let size = (length + lead).next_multiple_of(16);
let size = (last.allocation + lead).next_multiple_of(16);
let at = Place::Shared {
address: last.address,
held: own.reserved,
@@ -5751,6 +5818,7 @@ impl Buffer {
generation: std::ptr::from_ref(to.shared()).addr(),
object_id: last.object_id,
length,
allocation: last.allocation,
options: last.options,
address: last.address,
attributes: last.attributes.carried(),
@@ -5839,6 +5907,7 @@ impl Buffer {
generation: std::ptr::from_ref(to.shared()).addr(),
object_id: last.object_id,
length: last.length,
allocation: last.allocation,
options: last.options,
address: last.address,
attributes: last.attributes.carried(),
@@ -1146,6 +1146,7 @@ impl Device {
offset: window.offset,
base: buffer.base,
length,
allocation: length,
reach: buffer.size - buffer.base,
modes,
address,
@@ -2470,6 +2471,7 @@ impl Device {
offset: 0,
base: 0,
length,
allocation: length,
reach: length,
modes,
address,
@@ -5,26 +5,33 @@
//! - **Metal's rules** decide which textures a buffer backs and where they lie in it
//! (`linear_plan.rs`); what Metal stops is refused `Invalid`, and an IOSurface plane past its
//! surface of a shape that keeps no tail (below), which Metal makes, `NotImplemented`.
//! - **Tails.** A 2D texture, an array or a texture buffer whose texels run past its buffer's
//! length, which plain Metal makes up to the buffer's allocation (`linear_plan.rs`'s
//! **Length**), and a colour texture of an IOSurface plane whose texels run past the buffer
//! standing for its surface (`linear_plan.rs`'s **Beyond the plane's rows**: announced
//! `SurfaceTexelsTail`), keep the texels' bytes past the length in a tail of their own
//! ([`Texels::past`]): a runtime `Shared` buffer of its bytes from the first to the last (a
//! texture buffer's from its functions' first, its offset rounded down to 16), zero when made.
//! Its texels inside the length are the buffer's bytes, as every texture's over a buffer are.
//! A 2D texture or array is a shadow image whose copies go through the tail, as a staging
//! buffer: in, the buffer's bytes inside the length into the tail and the tail into the image;
//! back, the image into the tail and the tail's rows inside the length into the buffer. A
//! texture buffer's bindings view the tail, as a staged texture buffer's view its staging
//! buffer, with the same copies in and back but the image. A merge back takes its current
//! bytes from both and gives them back to both (`shadow_merge.rs`). The CPU's `replace_region` and `get_bytes`, and a
//! blit's rows, take each row's bytes inside the length from the buffer and the rest from the
//! tail; a copy between such texels and an image goes through a scratch buffer of the
//! encoder, as those rows (`blit_texture.rs`). The tail's uses are the buffer's bytes past its
//! length, by the same key, so the tracker orders them as the texels' own. Metal's texels there
//! are the memory after the buffer's, another allocation's bytes (docs/API.md's accepted
//! differences); the tail answers every path alike and reaches no allocation but its own.
//! - **Tails.** A texture's texels lie in place up to the bytes its buffer's memory holds for
//! them ([`Texels::kept`], `Buffer::texel_reach`): Metal's allocation of a buffer of the
//! runtime's memory, which for one of at least 16 KiB the program makes in the Metal address
//! space is its length rounded up to 16 KiB, as long a `VkBuffer` and placement, zero when made
//! (`Device::placed_new_buffer`); a heap's buffer's heap to its end, Metal's texels there being
//! the heap's bytes; a no-copy buffer's length, past which its pages are the caller's. A 2D
//! texture, an array or a texture buffer whose texels run past those bytes, which plain Metal
//! makes up to the buffer's allocation (`linear_plan.rs`'s **Length**), and a colour texture of
//! an IOSurface plane whose texels run past the buffer standing for its surface
//! (`linear_plan.rs`'s **Beyond the plane's rows**: announced `SurfaceTexelsTail`), keep the
//! texels' bytes past them in a tail of their own ([`Texels::past`]): a runtime `Shared` buffer
//! of its bytes from the first to the last (a texture buffer's from its functions' first, its
//! offset rounded down to 16), zero when made. Its texels kept in place are the buffer's bytes,
//! as every texture's over a buffer are. A 2D texture or array is a shadow image whose copies
//! go through the tail, as a staging buffer: in, the buffer's bytes kept in place into the tail
//! and the tail into the image; back, the image into the tail and the tail's rows kept in place
//! into the buffer. A texture buffer's bindings view the tail, as a staged texture buffer's view
//! its staging buffer, with the same copies in and back but the image. A merge back takes its
//! current bytes from both and gives them back to both (`shadow_merge.rs`). The CPU's
//! `replace_region` and `get_bytes`, and a blit's rows, take each row's bytes kept in place
//! from the buffer and the rest from the tail; a copy between such texels and an image goes
//! through a scratch buffer of the encoder, as those rows (`blit_texture.rs`). The tail's uses
//! are the buffer's bytes past those kept in place, by the same key, so the tracker orders them
//! as the texels' own. Metal's texels there are memory past the buffer's own: a no-copy
//! buffer's caller pages, or the allocation Metal gives a buffer under 16 KiB (docs/API.md's
//! accepted differences); the tail answers every path alike and reaches no allocation but its
//! own.
//! - **Texture buffers** are the buffer's bytes ([`Texels`]), viewed as a texel buffer where a
//! function binds them: in the format the binding plan names, over the range it gives, made at
//! the first bind that needs it and kept with the texture. Their uses are uses of the buffer's
@@ -146,25 +153,30 @@ pub(super) struct Texels {
pub(super) objects: ObjectsToken,
/// The texels lie in an IOSurface's plane (`Buffer::new_texture_with_iosurface_plane`).
surface: bool,
/// The tail of a 2D texture, an array or a texture buffer whose texels run past the buffer's
/// length (`linear_plan.rs`'s **Length**), which holds the texels' bytes there: a runtime
/// `Shared` buffer of the bytes from the texels' first to their last, laid out as in the
/// buffer from its start (`Texels::tail_base`), zero when made. Its bytes inside the buffer's
/// length mirror the buffer's for the shadow's copies, which go through it
/// The tail of a 2D texture, an array or a texture buffer whose texels run past the bytes
/// they keep in place (`kept`; `linear_plan.rs`'s **Length**), which holds the texels' bytes
/// there: a runtime `Shared` buffer of the bytes from the texels' first to their last, laid
/// out as in the buffer from its start (`Texels::tail_base`), zero when made. Its bytes
/// before `kept` mirror the buffer's for the shadow's copies, which go through it
/// ([`Texels::shadow_copies`], `into_texture_buffer`); every other access to the texels (the
/// CPU's, a blit's rows) takes the bytes inside the length from the buffer and those past it
/// from here ([`Texels::past`]). `None` for texels inside their buffer.
/// CPU's, a blit's rows) takes the bytes kept in place from the buffer and those past them
/// from here ([`Texels::past`]). `None` for texels kept in place whole.
tail: Option<Buffer>,
/// Where the texels' bytes in place end: the buffer's length, but for a 2D array of more than
/// one slice over an IOSurface plane, whose slices past the first lie in the tail from their
/// first byte on (`linear_plan::Placed::kept`). The tail holds the texels' bytes from here.
/// Where the texels' bytes in place end: the bytes a texture over the buffer reaches in its
/// memory (`Buffer::texel_reach`: Metal's allocation of a buffer of its own, which for one
/// of at least 16 KiB runs past its length, a no-copy buffer's length, a heap's buffer's heap
/// to its end), but the buffer's length for an IOSurface plane's, and for a 2D array of more
/// than one slice over a plane, whose slices past the first lie in the tail from their first
/// byte on, that slice's first byte (`linear_plan::Placed::kept`). The tail holds the texels'
/// bytes from here.
kept: u64,
}
/// Where a texture's texels past its buffer's length lie ([`Texels::past`]): the buffer's bytes
/// from `length` on are the tail's, byte `v` of the buffer's space at `v - rebase` of the tail.
/// `length` is the buffer's own, but a 2D array's over an IOSurface plane whose slices past the
/// first lie in the tail ([`Texels::kept`]), where it is the second slice's first byte.
/// Where a texture's texels past the bytes they keep in place lie ([`Texels::past`]): the
/// buffer's bytes from `length` on are the tail's, byte `v` of the buffer's space at `v - rebase`
/// of the tail. `length` is where the texels in place end ([`Texels::kept`]): the bytes the
/// buffer's memory holds for them, its length for an IOSurface plane's, and a 2D array's over a
/// plane whose slices past the first lie in the tail, the second slice's first byte.
#[derive(Clone, Copy)]
pub(super) struct Past<'a> {
pub(super) tail: &'a Buffer,
@@ -438,12 +450,15 @@ impl Buffer {
/// `BufferTextureProblem::PastAllocation`): the buffer's `allocatedSize` (its length rounded
/// up to 16 KiB), a heap's buffer's heap's size, a no-copy buffer's pages (its pointer's
/// offset in its 16 KiB page plus its length, rounded up to 16 KiB), and no bound under
/// 16 KiB, is made: its texels inside the length are the buffer's bytes, and those past it lie
/// in a tail the texture keeps, zero when made, which its functions, blits and CPU calls read
/// and write alike. Metal's texels there are the
/// memory after the buffer's, another allocation's bytes, which its writes there change
/// (docs/API.md's accepted differences). Such a 2D texture is a shadow image, announced
/// `shadowed-texture`; such a texture buffer's functions view its tail, announced
/// 16 KiB, is made. Its texels are the buffer's memory where that holds them: a buffer of at
/// least 16 KiB this device made holds its whole allocation, zero when made, and a heap's
/// buffer reaches the rest of its heap, so such a texture is made as one inside the length
/// is, its writes past the length landing in that memory as Metal's do. Past a no-copy
/// buffer's length, and past a shorter buffer's, its texels lie in a tail the texture keeps,
/// zero when made, which its functions, blits and CPU calls read and write alike; Metal's
/// texels there are the caller's pages, or memory past the buffer's own, which its writes
/// there change (docs/API.md's accepted differences). Such a 2D texture is a shadow image,
/// announced `shadowed-texture`; such a texture buffer's functions view its tail, announced
/// `staged-texture-buffer`.
pub fn new_texture(
&self,
@@ -496,8 +511,7 @@ impl Buffer {
/// Refused as [`new_texture`](Self::new_texture) is; an array counts its rounded slices
/// against the buffer's allocation, not its last texel (`Nil`, as Metal answers nil, once its
/// texels run past the length). An array whose texels run past the buffer's length short of
/// that is made as `new_texture` makes a 2D texture there, its texels past the length in its
/// tail.
/// that is made as `new_texture` makes a 2D texture there.
pub fn new_linear_texture(
&self,
descriptor: &TextureDescriptor,
@@ -1080,7 +1094,7 @@ impl Buffer {
.with_options(options)
.over_surface(surface);
// Where it would lie: the route its making takes (`texture/route.rs`), with no tail
// made for texels past the buffer's length, which the route asks alone.
// made for texels past those kept in place, which the route asks alone.
if goal == Goal::Ask {
let past = texels.runs_past();
let asked = (descriptor, &layout);
@@ -1099,7 +1113,7 @@ impl Buffer {
}
return Ok(Over::Made(texture));
}
// Texels past the buffer's length lie in a tail of their own (`linear_plan.rs`).
// Texels past those kept in place lie in a tail of their own (the module's **Tails**).
let texels = texels.with_tail()?;
let tail = surface && texels.tail.is_some();
let texture = self
@@ -2128,7 +2142,7 @@ impl Texels {
/// without the declaration a plane's padding is kept.
pub(super) fn padding_owned(&self) -> bool {
let unobserved = self.buffer.padding_unobserved();
owns_padding(&self.layout, self.surface, unobserved, self.buffer.length())
owns_padding(&self.layout, self.surface, unobserved, self.kept)
}
/// The texture's resource options (the `options` field), its hazard tracking mode resolved.
@@ -2149,9 +2163,25 @@ impl Texels {
Texels { options, ..self }
}
/// These texels, in an IOSurface's plane where `surface`.
/// These texels, in an IOSurface's plane where `surface`: their bytes in place end at the
/// buffer's length, past which a plane's lie in its tail whatever the buffer's memory holds
/// (`linear_plan.rs`'s **Beyond the plane's rows**).
fn over_surface(self, surface: bool) -> Texels {
Texels { surface, ..self }
let kept = match surface {
true => self.kept.min(self.buffer.length()),
false => self.kept,
};
Texels {
surface,
kept,
..self
}
}
/// Where the texels' bytes in place end, in bytes of the buffer's space ([`Texels::kept`]):
/// those from here lie in their tail.
pub(super) fn kept(&self) -> u64 {
self.kept
}
/// Every byte of the buffer the texels lie in, for the GPU or the CPU: from the first to the
@@ -2166,10 +2196,10 @@ impl Texels {
}
}
/// Whether the texels lie in the buffer, or past its length in their tail (the module's
/// Whether the texels lie in the buffer's memory, or past it in their tail (the module's
/// **Tails**): every texture's but a rotated one's whose rows run past it (`rotated_rows`).
pub(super) fn reached(&self) -> bool {
self.tail.is_some() || self.span().end <= self.buffer.length()
self.tail.is_some() || self.span().end <= self.kept
}
/// Whether Metal's GPU reads and writes two of the texels' rows over bytes they share: an
@@ -2217,7 +2247,7 @@ impl Texels {
objects: ObjectsToken::default(),
surface: false,
tail: None,
kept: buffer.length(),
kept: buffer.texel_reach(),
buffer,
}
}
@@ -2308,8 +2338,9 @@ impl Texels {
/// device's `minTexelBufferOffsetAlignment`, which the binding plan holds a view's offset to,
/// divides the functions' offset in the buffer's `VkBuffer`; else through a staging buffer of
/// their own, at its start (a no-copy buffer's bytes, or Metal's offset, need not lie at a
/// multiple of it), and through their tail where they run past the buffer's length, which no
/// view of the buffer reaches. Refused as `new_buffer` refuses the staging buffer.
/// multiple of it), and through their tail where they run past the bytes they keep in place
/// ([`Texels::kept`]), which no view of the buffer reaches. Refused as `new_buffer` refuses
/// the staging buffer.
fn into_texture_buffer(self) -> Result<Texels, Error> {
let mut texels = self;
let placed = texels.layout;
@@ -2317,7 +2348,7 @@ impl Texels {
offset: placed.offset - placed.offset % LINEAR_ALIGNMENT,
..placed
};
// Texels past the buffer's length lie in a tail of their own (the module's **Tails**),
// Texels past those kept in place lie in a tail of their own (the module's **Tails**),
// which their bindings view, as a staging buffer's: no view of the buffer reaches them.
let mut texels = texels.with_tail()?;
let (raw, at) = (texels.buffer).at_for(texels.shader.offset, ReadApart::Texture);
@@ -2356,7 +2387,7 @@ impl Texels {
texels.layout.offset,
limits.min_texel_buffer_offset_alignment,
);
let length = texels.buffer.length();
let kept = texels.kept;
texels
.buffer
.generation()
@@ -2364,8 +2395,9 @@ impl Texels {
let object = format!("texture buffer over a buffer at offset {offset}");
let reason = match past {
true => format!(
"its texels run past the buffer's {length} bytes, where no view of the \
buffer's VkBuffer reaches: those lie in a tail of the texture's own"
"its texels run past byte {kept} of the buffer, where its memory ends for \
them and no view of the buffer's VkBuffer reaches: those lie in a tail of \
the texture's own"
),
false => format!(
"its texels start at byte {at} of the buffer's VkBuffer, and the device \
@@ -2754,9 +2786,10 @@ impl Texels {
.buffer
.mapped_contents()
.ok_or_else(|| invalid(call, Problem::Copy(CopyProblem::CpuAccessPrivate)))?;
// The texture lies in the buffer, and so do the rows of a region checked in its level.
// The texture lies in the buffer's memory, and so do the rows of a region checked in its
// level.
let end = len.and_then(|len| start.checked_add(len));
let Some(end) = end.filter(|&end| end <= self.buffer.length()) else {
let Some(end) = end.filter(|&end| end <= self.buffer.texel_reach()) else {
return Err(internal("a texture's rows past its buffer"));
};
// SAFETY: `start` lies in the buffer's mapped bytes (checked above).
@@ -312,7 +312,8 @@ degradations! {
copied in only at its first use and the first after each did_modify_data, and never \
back)";
/// A texture buffer is viewed through a staging buffer: its texels start where the
/// device views none (`minTexelBufferOffsetAlignment`), or run past its buffer's length,
/// device views none (`minTexelBufferOffsetAlignment`), or run past the bytes its
/// buffer's memory holds for them (a no-copy buffer's or a buffer's under 16 KiB length),
/// where its staging buffer is its tail.
StagedTextureBuffer = "staged-texture-buffer", Performance,
"its texels are copied into a staging buffer before each use and back after each \
@@ -444,6 +444,7 @@ impl Device {
offset: lead,
base: lead,
length,
allocation: length,
reach: length,
modes,
address,
@@ -72,10 +72,12 @@
//! (`probes/texture_buffer_past_length.m`, 64 `RGBA8Unorm` texels ending 128 to 256 bytes past
//! a 4096-byte buffer, and 96 past a 20000-byte one), its kernels from its offset rounded down
//! to 16 bytes and its blits and `getBytes` from the offset itself, as inside the length. The
//! runtime makes such a 2D texture, array or texture buffer as Metal does: its texels inside
//! the length are the buffer's bytes, and those past it lie in a tail of the texture's own
//! (`buffer_texture.rs`), zero when made, which every path reads and writes alike and which is
//! no other allocation's memory; the nil one it answers as Metal's nil
//! runtime makes such a 2D texture, array or texture buffer as Metal does, its texels the
//! buffer's memory as far as that holds them (`buffer_texture.rs`'s **Tails**): a buffer of at
//! least 16 KiB it made holds its whole allocation, zero when made, and a heap's buffer reaches
//! its heap's end; past a no-copy buffer's length and past a shorter buffer's, the texels lie
//! in a tail of the texture's own, zero when made, which every path reads and writes alike and
//! which is no other allocation's memory. The nil one it answers as Metal's nil
//! ([`BufferTextureProblem::PastAllocation`]).
//!
//! **IOSurface planes** (`Buffer::new_texture_with_iosurface_plane`, the stand-in for
@@ -1184,8 +1186,8 @@ pub(super) fn plan_buffer_texture(
// Metal's validation counts the slices' rows as if contiguous against the length and stops
// the texture there; plain Metal reads where the buffer's allocation ends alone, a 2D
// texture's and a texture buffer's last texel against it and an array's rounded slices (the
// module's **Length**), so the runtime does too. A 2D texture, an array or a texture buffer
// whose texels run past the length keeps those texels in its tail (`buffer_texture.rs`).
// module's **Length**), so the runtime does too: as far as the buffer's memory holds them,
// in place, and past that in the texture's tail (`buffer_texture.rs`'s **Tails**).
if placed.end() > buffer.length {
let end = match array {
true => offset.saturating_add(slice_pitch.saturating_mul(slices)),
@@ -896,6 +896,7 @@ impl Device {
offset: base,
base,
length,
allocation: length,
reach,
modes,
address,
+7 -6
View File
@@ -700,7 +700,7 @@ pub(super) enum Conversion {
/// 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.
/// bytes a texture over the buffer reaches in it end (`Buffer::texel_reach`).
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
@@ -717,19 +717,20 @@ impl ShadowBuffer {
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()),
_ => ShadowBuffer::Fixed(raw, buffer.start() + buffer.texel_reach()),
}
}
/// 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.
/// Where the bytes a texture over the buffer reaches end in its `VkBuffer` (its byte 0's offset
/// there, [`Buffer::start`], past [`Buffer::texel_reach`]), 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()),
.map_or(0, |buffer| buffer.start() + buffer.texel_reach()),
}
}
@@ -37,8 +37,9 @@
//! another writer's write to the word's other bytes during the merge itself is a race the merge
//! does not order.
//! - **Merged through a copy** elsewhere (a texture's first byte inside a word, through a staging
//! buffer, a last word past its buffer, or bytes in its tail: texels past the buffer's length, or
//! a 2D array's slices past its first over an IOSurface plane, `buffer_texture.rs`; never
//! buffer, a last word past its buffer, or bytes in its tail: texels past those the buffer's
//! memory holds, or a 2D array's slices past its first over an IOSurface plane,
//! `buffer_texture.rs`; never
//! recorded over detached bytes): the shadow's texels are copied into the *changed* part, and the
//! buffer's bytes from the texture's first to its last into the *current* part. One dispatch of
//! the merge kernel then takes, in each byte of the current part, the changed part's byte where
@@ -276,11 +277,11 @@ struct Merged {
/// from the rectangle's first word to its last (its kernel writes words of the rectangle
/// alone, the module docs), else every byte of the texture's.
written: Range<u64>,
/// Where the texture's bytes past those it keeps in its buffer lie (past the buffer's length,
/// or a 2D array's second slice on over an IOSurface plane, `Texels::kept`), where it has
/// such bytes: its tail, the staging buffer its copies go through (`buffer_texture.rs`). A
/// merge through a copy takes the current part's bytes past those from there and gives them
/// back there.
/// Where the texture's bytes past those it keeps in its buffer lie (past those the buffer's
/// memory holds, or a 2D array's second slice on over an IOSurface plane, `Texels::kept`),
/// where it has such bytes: its tail, the staging buffer its copies go through
/// (`buffer_texture.rs`). A merge through a copy takes the current part's bytes past those
/// from there and gives them back there.
tail: Option<MergeTail>,
}
@@ -296,8 +297,8 @@ struct MergeTail {
impl Merged {
/// The copies between the texture's bytes (`span` of them from its first) and `current`, a
/// merge's current part, each `(the bytes' VkBuffer, the copy)` as the bytes go into the
/// part: one of every byte, or, where its texels run past its buffer's length, one of those
/// inside it and one of those in its tail. A copy back swaps each one's ends.
/// part: one of every byte, or, where its texels run past those it keeps in its buffer, one
/// of those kept there and one of those in its tail. A copy back swaps each one's ends.
fn current_copies(&self, current: Part, span: u64) -> Vec<(vk::Buffer, vk::BufferCopy)> {
let Some(tail) = self.tail else {
let copy = vk::BufferCopy {
@@ -0,0 +1,131 @@
//! What new buffers of 16 KiB or more cost to make: a batch of [`COUNT`] buffers whose lengths
//! spread geometrically from 16 KiB + 1 to 4 MiB, `Shared` and `Private`, made, their first fills
//! run by one empty command buffer committed and waited for (a `Private` buffer's zeros are a
//! device fill its device's next submission records first, `texture/first_move.rs`; a `Shared`
//! one's are cleared on the host as it is made), and dropped, [`ROUNDS`] times. Printed as
//! `BUFFER-MAKING` lines: each round's making and fills, in milliseconds, the bytes the device
//! allocated (`WorkCounts::device_memory_allocated`) and the host cleared
//! (`new_buffer_bytes_cleared`), and the batch's lengths and `VkMemoryRequirements.size` summed;
//! the first round makes every buffer in new memory, the later ones take parked buffers
//! (`recycle.rs`). And `BUFFER-REQUIREMENTS` lines: a 16385-byte buffer's `VkBuffer`'s memory
//! requirements, and a plain `VkBuffer`'s of 16400 and of 32768 bytes on the same device. Run it
//! on two trees to compare them. Ignored: run it in release under the GPU safety cfg.
//!
//! Device tests: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on the kind
//! of device the cfg selects.
use super::blit_holds::{device, Scratch};
use crate::{Buffer, Device, ResourceOptions, WorkCounts};
use ash::vk;
use std::time::{Duration, Instant};
const WAIT: Duration = Duration::from_secs(120);
/// The buffers a batch makes, and the rounds.
const COUNT: u32 = 1000;
const ROUNDS: usize = 5;
/// The shortest and longest length of a batch.
const SHORTEST: u64 = (16 << 10) + 1;
const LONGEST: u64 = 4 << 20;
/// The batch's lengths: geometric from [`SHORTEST`] to [`LONGEST`].
fn lengths() -> Vec<u64> {
let ratio = (LONGEST as f64 / SHORTEST as f64).powf(1.0 / f64::from(COUNT - 1));
(0..COUNT)
.map(|at| ((SHORTEST as f64) * ratio.powi(at as i32)).round() as u64)
.map(|length| length.clamp(SHORTEST, LONGEST))
.collect()
}
/// `buffer`'s `VkBuffer`'s memory requirements.
fn requirements(device: &Device, buffer: &Buffer) -> vk::MemoryRequirements {
let (raw, _) = buffer.at(0);
// SAFETY: `raw` is the live buffer's `VkBuffer`, of this device.
unsafe { device.shared().vk().get_buffer_memory_requirements(raw) }
}
/// A plain `VkBuffer` of `size` bytes' memory requirements, of storage and transfer usage.
fn plain_requirements(device: &Device, size: u64) -> vk::MemoryRequirements {
let raw = device.shared().vk();
let usage = vk::BufferUsageFlags::STORAGE_BUFFER
| vk::BufferUsageFlags::TRANSFER_SRC
| vk::BufferUsageFlags::TRANSFER_DST;
let info = vk::BufferCreateInfo::default().size(size).usage(usage);
// SAFETY: the device is live and `info` valid.
let buffer = unsafe { crate::creation::buffer(raw, &info) }.unwrap();
// SAFETY: `buffer` is a live buffer of the device, unbound and unused.
let requirements = unsafe { raw.get_buffer_memory_requirements(buffer) };
// SAFETY: as above.
unsafe { raw.destroy_buffer(buffer, None) };
requirements
}
/// One round of `options`: the batch made, its fills run, then dropped.
fn round(device: &Device, options: ResourceOptions, lengths: &[u64]) -> String {
let queue = device.new_command_queue().unwrap();
let before = device.work_counts();
let started = Instant::now();
let batch: Vec<Buffer> = (lengths.iter())
.map(|&length| device.new_buffer(length, options).unwrap())
.collect();
let made = started.elapsed();
let started = Instant::now();
let command_buffer = queue.command_buffer().unwrap();
command_buffer.commit().unwrap();
command_buffer.wait_until_completed_for(WAIT).unwrap();
let filled = started.elapsed();
let after = device.work_counts();
let required: u64 = (batch.iter())
.map(|buffer| requirements(device, buffer).size)
.sum();
let lengths: u64 = batch.iter().map(Buffer::length).sum();
drop(batch);
let delta = |of: fn(&WorkCounts) -> u64| of(&after) - of(&before);
format!(
"made {:.2} ms, fills {:.2} ms, allocated {} bytes, host cleared {} bytes, lengths {} \
bytes, required {} bytes",
made.as_secs_f64() * 1e3,
filled.as_secs_f64() * 1e3,
delta(|counts| counts.device_memory_allocated),
delta(|counts| counts.new_buffer_bytes_cleared),
lengths,
required,
)
}
#[test]
#[ignore = "a release benchmark: run with --ignored --nocapture"]
fn new_buffers_of_16_kib_or_more() {
let scratch = Scratch::new("buffer-making-bench");
let device = device(&scratch.0);
let name = device.name();
for (storage, options) in [
("Shared", ResourceOptions::shared()),
("Private", ResourceOptions::private()),
] {
let buffer = device.new_buffer(SHORTEST, options).unwrap();
let made = requirements(&device, &buffer);
println!(
"BUFFER-REQUIREMENTS on {name}: a {SHORTEST}-byte {storage} buffer's VkBuffer: size \
{}, alignment {}",
made.size, made.alignment
);
}
for size in [SHORTEST.next_multiple_of(16), 32768] {
let plain = plain_requirements(&device, size);
println!(
"BUFFER-REQUIREMENTS on {name}: a plain VkBuffer of {size} bytes: size {}, \
alignment {}",
plain.size, plain.alignment
);
}
let lengths = lengths();
for (storage, options) in [
("Shared", ResourceOptions::shared()),
("Private", ResourceOptions::private()),
] {
for at in 0..ROUNDS {
let line = round(&device, options, &lengths);
println!("BUFFER-MAKING on {name}: {COUNT} {storage} buffers, round {at}: {line}");
}
}
}
@@ -2,8 +2,9 @@
//! **Tails**): plain Metal makes it, and every path of Metal's takes the texels past the length
//! as one memory, which each reads what each wrote to (an Apple M3 Max under macOS 27.0.1,
//! `metal2vulkan-conformance/probes/texels_past_buffer_length.m`). The runtime makes it too: its
//! texels inside the length are the buffer's bytes, and those past it lie in the texture's tail,
//! zero when made.
//! texels are the buffer's memory as far as that holds them (a buffer of 16 KiB or more holds its
//! allocation, its length rounded up to 16 KiB, zero when made; a heap's buffer its heap to its
//! end), and those past it lie in the texture's tail, zero when made.
//!
//! Each case writes the texels through one path and reads them back through every other: a
//! kernel (`invert_texels`, the owned fixture `validation/fixtures/public/examples/
@@ -11,10 +12,10 @@
//! into its second), a blit, and the CPU (`replace_region`, `get_bytes`). The buffer's bytes are
//! read through its `contents`, or by a blit of the buffer for a `Private` one. Buffers made
//! around the texture's are filled with a pattern no write here makes, and keep it: no write
//! past the length reaches memory but the tail. The shapes are reims-vgpu's (64 by 17 at rows of
//! 256 over 4096 bytes, and 64 by 16 from offset 2048), one whose last row crosses the length
//! (64 by 80 over 20000 bytes, whose row 78 holds 32 of the buffer's bytes), and one wholly past
//! it (64 by 1 from offset 4096).
//! past the length reaches memory but the tail and the buffer's own. The shapes are reims-vgpu's
//! (64 by 17 at rows of 256 over 4096 bytes, and 64 by 16 from offset 2048), one whose last row
//! crosses the length (64 by 80 over 20000 bytes, whose row 78 holds 32 of the buffer's bytes,
//! which its allocation holds whole), and one wholly past it (64 by 1 from offset 4096).
//!
//! Device tests: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`,
//! `m2v_cpu_icd`), on the kind of device the cfg selects.
@@ -26,6 +27,7 @@ use crate::{
BlitOption, Buffer, BufferTextureProblem, CommandQueue, ComputePipelineState, Degradation,
Device, Error, HeapDescriptor, HeapType, HostMemory, IoSurfacePlane, Nil, Origin,
PipelineOptions, Problem, Region, ResourceOptions, Size, Texture, TextureDescriptor,
TexturePlacement, TextureShadowReason,
};
use metal2vulkan::metal::{PixelFormat, TextureUsage};
use std::ptr::NonNull;
@@ -279,6 +281,33 @@ impl Tail {
})
}
/// The bytes `range` of the buffer's memory from its start, through its `contents`: past its
/// length, those its allocation or its heap holds there.
fn memory_bytes(&self, range: std::ops::Range<u64>) -> Vec<u8> {
let contents = self.buffer.contents().unwrap().as_ptr();
let len = (range.end - range.start) as usize;
// SAFETY: `range` lies in the memory mapped from `contents` (the buffer's allocation, or
// its heap from its start), which no command uses now.
unsafe { std::slice::from_raw_parts(contents.add(range.start as usize), len) }.to_vec()
}
/// The last byte of the texture's texels, past which none of the buffer's is the texture's.
fn end(&self) -> u64 {
self.offset + (self.height - 1) * self.pitch + ROW
}
/// Row `y`'s bytes as the buffer's memory holds them before `kept`, those from it on zero.
fn row_kept(&self, y: u64, kept: u64) -> Vec<u8> {
let start = self.offset + y * self.pitch;
let inside = kept.clamp(start, start + ROW);
let mut bytes = match inside > start {
true => self.memory_bytes(start..inside),
false => Vec::new(),
};
bytes.resize(ROW as usize, 0);
bytes
}
/// Row `y`'s bytes as the buffer holds them, those past its length zero.
fn made_row(&self, y: u64) -> Vec<u8> {
let start = self.offset + y * self.pitch;
@@ -298,6 +327,8 @@ fn a_texture_past_its_buffer_s_length_keeps_those_texels_in_its_tail() {
let scratch = Scratch::new("buffer-tail");
let device = device(&scratch.0);
let tail = Tail::new(&device, (4096, ResourceOptions::shared()), 0, 17);
let texels = tail.texture.buffer_texels().unwrap();
assert!(texels.past().is_some(), "a buffer under 16 KiB: a tail");
// As made: rows 0 to 15 the buffer's bytes, row 16 zero, on every path.
for y in [0, 7, 15, 16] {
let want = tail.made_row(y);
@@ -341,9 +372,10 @@ fn a_texture_past_its_buffer_s_length_keeps_those_texels_in_its_tail() {
}
/// The other shapes: 64 by 16 from offset 2048 (8 rows past), 64 by 80 over 20000 bytes (row 78
/// crosses the length 32 bytes in, and its first 8 texels are the buffer's), and 64 by 1 wholly
/// past a 4096-byte buffer. Each reads as made on every path, and a kernel's write of every row
/// reads back on every path, the bytes inside the length in the buffer.
/// crosses the length 32 bytes in, and its first 8 texels are the buffer's; the buffer's
/// allocation holds it whole), and 64 by 1 wholly past a 4096-byte buffer. Each reads as made on
/// every path, and a kernel's write of every row reads back on every path, the bytes inside the
/// length in the buffer.
#[test]
fn every_shape_past_the_length_reads_its_tail_and_its_buffer() {
let scratch = Scratch::new("buffer-tail-shapes");
@@ -1057,16 +1089,23 @@ fn a_texture_buffer_past_its_buffer_s_length_keeps_those_texels_in_its_tail() {
}
}
/// Every path reads `tail`'s texture as made (`rows` among its rows), and a kernel's write of
/// every row reads back on every path, its texels inside the buffer's length landing in the
/// buffer's bytes; the CPU's write of the buffer's bytes under the first row, through its
/// Every path reads `tail`'s texture as made (`rows` among its rows): its bytes before `kept`, where
/// the buffer's memory holding the texels ends, as that memory holds them, and those past it zero;
/// and a kernel's write of every row reads back on every path, its texels before `kept` landing in
/// the buffer's memory; the CPU's write of the buffer's bytes under the first row, through its
/// `contents`, every path then reads through the texture; and no write reaches another buffer.
/// The texture is a shadow image whose copies in and back go through its tail
/// (`buffer_texture.rs`'s **Tails**), counted in `shadow_copies`.
fn coherent_past_length(device: &Device, tail: &Tail, rows: [u64; 4], what: &str) {
/// Texels past `kept` lie in a tail, the texture a shadow image whose copies in and back go
/// through it (`buffer_texture.rs`'s **Tails**), counted in `shadow_copies`; a texture that ends
/// at `kept` or short of it keeps none. The kernel's rows, as written.
fn coherent_past_length(
device: &Device,
tail: &Tail,
rows: [u64; 4],
(what, kept): (&str, u64),
) -> Vec<Vec<u8>> {
let before = device.work_counts().shadow_copies;
for y in rows {
let want = tail.made_row(y);
let want = tail.row_kept(y, kept);
let cpu = tail.cpu_row(&tail.texture, y);
assert_eq!(cpu, want, "{what}: as made, get_bytes {y}");
assert_eq!(tail.blit_row(y), want, "{what}: as made, blit {y}");
@@ -1087,15 +1126,14 @@ fn coherent_past_length(device: &Device, tail: &Tail, rows: [u64; 4], what: &str
"{what}: the kernel's, kernel {y}"
);
}
// The texels inside the length the kernel wrote: the buffer's own bytes.
let length = tail.buffer.length();
// The texels before `kept` the kernel wrote: the buffer's memory's bytes.
for (y, written) in (0..tail.height).zip(&written) {
let start = tail.offset + y * tail.pitch;
let inside = length.min(start + ROW);
let inside = kept.min(start + ROW);
if inside > start {
let want = &written[..(inside - start) as usize];
let bytes = tail.buffer_bytes(start..inside);
assert_eq!(bytes, want, "{what}: the buffer's bytes of row {y}");
let bytes = tail.memory_bytes(start..inside);
assert_eq!(bytes, want, "{what}: the buffer's memory of row {y}");
}
}
// The CPU writes the buffer's bytes under row 0, inside the length: the texture reads them.
@@ -1122,11 +1160,27 @@ fn coherent_past_length(device: &Device, tail: &Tail, rows: [u64; 4], what: &str
"{what}: the buffer's CPU write, kernel"
);
let copies = device.work_counts().shadow_copies - before;
assert!(copies > 0, "{what}: a shadow, its copies through its tail");
let texels = tail.texture.buffer_texels().unwrap();
match tail.end() > kept {
true => {
let past = texels.past().map(|past| past.length);
assert_eq!(
past,
Some(kept),
"{what}: its texels past {kept} in its tail"
);
assert!(copies > 0, "{what}: a shadow, its copies through its tail");
}
false => assert!(
texels.past().is_none(),
"{what}: no tail, its texels in the buffer's memory"
),
}
assert!(
tail.around_kept(),
"{what}: no write reaches another buffer"
);
written
}
/// Metal's nil for a texture over a buffer that ends at `end`, past where the buffer's
@@ -1136,16 +1190,38 @@ fn past_allocation(end: u64, allocation: u64) -> Error {
Error::Nil(Nil::new("new_texture", Problem::BufferTexture(problem)))
}
/// `tail`'s texture's descriptor, `height` rows tall.
fn shaped(tail: &Tail, height: u64) -> TextureDescriptor {
let mut descriptor =
TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, WIDTH, height, false);
descriptor.usage = TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE;
descriptor.resource_options = tail.buffer.resource_options();
descriptor
}
/// `tail`'s texture again, `height` rows tall, at its offset and pitch.
fn taller(tail: &Tail, height: u64) -> Result<Texture, Error> {
let descriptor = TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, WIDTH, height, false);
(tail.buffer).new_texture(&descriptor, tail.offset, tail.pitch)
(tail.buffer).new_texture(&shaped(tail, height), tail.offset, tail.pitch)
}
/// Where `tail`'s texture lies, as its making answers it: over a buffer `long` bytes long, which
/// holds it inside its length, where `long` is `Some`; over its own buffer where `None`.
fn placement(device: &Device, tail: &Tail, long: Option<u64>) -> TexturePlacement {
let shape = shaped(tail, tail.height);
let buffer = match long {
Some(long) => device
.new_buffer(long, tail.buffer.resource_options())
.unwrap(),
None => tail.buffer.clone(),
};
(buffer.texture_placement(&shape, tail.offset, tail.pitch)).unwrap()
}
/// A 2D texture whose last texel ends at its buffer's allocation, its last row's padding past it
/// (`linear_plan.rs`'s **Length**: 64 by 64 from offset 256, rows 512 apart, over 20000 bytes,
/// the last texel at 32768 and the rows' end at 33024), is made as an Apple M3 Max under macOS
/// 27.0.1 makes it, its texels past the length in its tail; a row more ends past the allocation,
/// 27.0.1 makes it, in the buffer's allocation, which holds it whole: no tail, and where it lies
/// as it would over a buffer holding it inside its length. A row more ends past the allocation,
/// Metal's nil.
#[test]
fn a_texture_whose_last_texel_ends_at_the_allocation_is_made() {
@@ -1153,42 +1229,137 @@ fn a_texture_whose_last_texel_ends_at_the_allocation_is_made() {
let device = device(&scratch.0);
let target = || device.new_buffer(20000, ResourceOptions::shared()).unwrap();
let tail = Tail::over(&device, target, (256, 64, 512));
coherent_past_length(&device, &tail, [0, 38, 39, 63], "last texel");
let inside = placement(&device, &tail, Some(33024));
assert_eq!(
placement(&device, &tail, None),
inside,
"as inside the length"
);
coherent_past_length(&device, &tail, [0, 38, 39, 63], ("last texel", 32768));
assert_eq!(taller(&tail, 65).err(), Some(past_allocation(33280, 32768)));
}
/// A texture over a buffer of 16 KiB or more that the device made, whose texels run past its
/// length into its allocation (64 by 128 over 20000 bytes, packed rows, ending at 32768): Metal's
/// texels there are the allocation's, zero when made, which every path reads and writes alike, and
/// the runtime's lie there too, in the buffer's memory, its `VkBuffer` its length rounded up to
/// 16 KiB (`Device::placed_new_buffer`). It lies as it would over a buffer holding it inside its
/// length, with no tail; where that is an image over the buffer's memory, no copy is taken. The
/// buffer's own reach stays its length: a blit of its bytes past the length is refused.
#[test]
fn a_buffer_s_texture_to_its_allocation_lies_in_its_memory() {
let scratch = Scratch::new("buffer-tail-allocation");
let device = device(&scratch.0);
let before = device.work_counts();
let target = || device.new_buffer(20000, ResourceOptions::shared()).unwrap();
let tail = Tail::over(&device, target, (0, 128, ROW));
let padding = tail.memory_bytes(20000..32768);
assert!(padding.iter().all(|&byte| byte == 0), "zero when made");
let inside = placement(&device, &tail, Some(32768));
let asked = placement(&device, &tail, None);
assert_eq!(asked, inside, "as inside the length");
let past = TexturePlacement::Shadow(TextureShadowReason::PastLength);
assert_ne!(asked, past, "no tail");
let rows = [0, 77, 78, 127];
coherent_past_length(&device, &tail, rows, ("allocation", 32768));
let after = device.work_counts();
let aliases = tail.texture.aliases_buffer();
eprintln!(
"BUFFER-TAIL on {}: a texture to its buffer's allocation: asked {asked:?}, an image over \
the buffer's memory {aliases}, {} shadowed, {} copies",
device.name(),
after.shadowed_textures - before.shadowed_textures,
after.shadow_copies - before.shadow_copies,
);
if aliases {
assert_eq!(
after.shadow_copies - before.shadow_copies,
0,
"in place: no copy"
);
}
let read = device.new_buffer(32, ResourceOptions::shared()).unwrap();
let command_buffer = tail.queue.command_buffer().unwrap();
let mut blit = command_buffer.blit_command_encoder().unwrap();
let copy = blit.copy_from_buffer_to_buffer(&tail.buffer, 20000 - 16, &read, 0, 32);
assert!(copy.is_err(), "a blit past the buffer's length: {copy:?}");
blit.end_encoding().unwrap();
}
/// A buffer of a heap takes a texture to its heap's size from its own start, as an Apple M3 Max
/// under macOS 27.0.1 makes one (`linear_plan.rs`'s **Length**): 64 by 256 over a 16384-byte
/// buffer of a 64 KiB heap, ending 65536 bytes from the buffer's start, its rows past the
/// length in its tail, which no other buffer of the heap shares; a row more is Metal's nil.
/// buffer at the start of a 64 KiB placement heap, ending 65536 bytes from the buffer's start.
/// Its texels past the length are the heap's bytes, as Metal's are: those of the heap's other
/// buffer, at 32768, read through the texture as that buffer holds them, and the kernel's writes
/// over them land in it. No tail. A row more is Metal's nil.
#[test]
fn a_heap_s_buffer_takes_a_texture_to_its_heap_s_size() {
const HEAP: u64 = 64 << 10;
let scratch = Scratch::new("buffer-tail-heap");
let device = device(&scratch.0);
let descriptor = HeapDescriptor::new(HeapType::Automatic, HEAP, ResourceOptions::shared());
let descriptor = HeapDescriptor::new(HeapType::Placement, HEAP, ResourceOptions::shared());
let heap = device.new_heap(&descriptor).unwrap();
assert_eq!(heap.size(), HEAP);
let target = || heap.new_buffer(16384, ResourceOptions::shared()).unwrap();
let shared = ResourceOptions::shared();
let target = || heap.new_buffer_at_offset(16384, shared, 0).unwrap();
let tail = Tail::over(&device, target, (0, HEAP / ROW, ROW));
let neighbour = heap.new_buffer(4096, ResourceOptions::shared()).unwrap();
let at = 32768;
let neighbour = heap.new_buffer_at_offset(4096, shared, at).unwrap();
let neighbour_at = neighbour.contents().unwrap().as_ptr();
// SAFETY: the neighbour's 4096 mapped bytes, which no command uses.
unsafe { std::ptr::write_bytes(neighbour_at, AROUND, 4096) };
coherent_past_length(&device, &tail, [0, 63, 64, HEAP / ROW - 1], "heap");
let rows = [0, 63, at / ROW, HEAP / ROW - 1];
let written = coherent_past_length(&device, &tail, rows, ("heap", HEAP));
// SAFETY: as above.
let neighbour_bytes = unsafe { std::slice::from_raw_parts(neighbour_at, 4096) };
let kept = neighbour_bytes.iter().all(|&byte| byte == AROUND);
assert!(kept, "no write reaches the heap's other buffer");
let texels = written.concat();
let at = at as usize;
assert_eq!(
neighbour_bytes,
&texels[at..at + 4096],
"the heap's other buffer holds the texels written over its bytes"
);
let nil = past_allocation(HEAP + ROW, HEAP);
assert_eq!(taller(&tail, HEAP / ROW + 1).err(), Some(nil));
}
/// A buffer of a heap past its start takes a texture to the heap's size from its own start, past
/// the heap's end (64 by 256 over a 16384-byte buffer at 16384 of a 64 KiB placement heap, ending
/// 65536 bytes from that buffer's start): its texels inside the heap are the heap's bytes, and those
/// past its end lie in the texture's tail, zero when made, which no other memory shares: the
/// heap's first buffer keeps its bytes.
#[test]
fn a_texture_past_its_heap_s_end_keeps_those_texels_in_its_tail() {
const HEAP: u64 = 64 << 10;
let scratch = Scratch::new("buffer-tail-heap-end");
let device = device(&scratch.0);
let descriptor = HeapDescriptor::new(HeapType::Placement, HEAP, ResourceOptions::shared());
let heap = device.new_heap(&descriptor).unwrap();
let shared = ResourceOptions::shared();
let first = heap.new_buffer_at_offset(16384, shared, 0).unwrap();
let first_at = first.contents().unwrap().as_ptr();
// SAFETY: the first buffer's 16384 mapped bytes, which no command uses.
unsafe { std::ptr::write_bytes(first_at, AROUND, 16384) };
let start = 16384;
let target = || heap.new_buffer_at_offset(16384, shared, start).unwrap();
let tail = Tail::over(&device, target, (0, HEAP / ROW, ROW));
let kept = HEAP - start;
let rows = [0, 63, kept / ROW - 1, kept / ROW];
coherent_past_length(&device, &tail, rows, ("past the heap's end", kept));
// SAFETY: as above.
let first_bytes = unsafe { std::slice::from_raw_parts(first_at, 16384) };
assert!(
first_bytes.iter().all(|&byte| byte == AROUND),
"no write reaches the heap's first buffer"
);
}
/// A no-copy buffer takes a texture to its pages from its own start, its pointer's offset in its
/// 16 KiB page plus its length rounded up to 16 KiB, as an Apple M3 Max under macOS 27.0.1 makes
/// one (`linear_plan.rs`'s **Length**): 64 by 128 over 16384 bytes starting 4096 bytes short of
/// a 16 KiB page's end, ending 32768 bytes from the buffer's start, its rows past the length in
/// its tail: no write reaches the caller's bytes around the buffer. A row more is Metal's nil.
/// its tail, as the pages past it are the caller's: no write reaches the caller's bytes around
/// the buffer. A row more is Metal's nil.
#[test]
fn a_no_copy_buffer_takes_a_texture_to_its_pages() {
const MAPPING: usize = 64 << 10;
@@ -1208,7 +1379,7 @@ fn a_no_copy_buffer_takes_a_texture_to_its_pages() {
unsafe { device.new_buffer_with_bytes_no_copy(memory, 16384, options, None) }.unwrap()
};
let tail = Tail::over(&device, target, (0, 128, ROW));
coherent_past_length(&device, &tail, [0, 63, 64, 127], "no-copy");
coherent_past_length(&device, &tail, [0, 63, 64, 127], ("no-copy", 16384));
// SAFETY: the mapping's bytes, which no command uses now.
let all = unsafe { std::slice::from_raw_parts(start, MAPPING) };
let around = all[..lead].iter().chain(&all[lead + 16384..]);
@@ -53,6 +53,8 @@ mod bound_inline;
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
mod buffer_carved_reads;
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
mod buffer_making_bench;
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
mod buffer_tail_device;
mod buffer_texture;
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
+5 -4
View File
@@ -2850,8 +2850,9 @@ impl Device {
taken at its first use and after each did_modify_data, its later levels made from \
the first"
),
Route::Past => "its texels run past the buffer's length, which no image over the \
buffer's memory reaches: the texture's tail holds them"
Route::Past => "its texels run past the bytes the buffer's memory holds for them, \
which no image over that memory reaches: the texture's tail holds \
them"
.to_owned(),
Route::Host {
way: HostWay::DeviceCopy,
@@ -3484,7 +3485,7 @@ impl Device {
let (_, buffer_offset, type_index) = texels.buffer.memory();
let memory_offset = buffer_offset + texels.layout.offset;
let raw_device = self.shared().vk();
let room = texels.buffer.padded_length() - texels.layout.offset;
let room = texels.buffer.texel_room() - texels.layout.offset;
let held = between.held();
// Why an image the device made does not hold exactly Metal's texels in the buffer's
// memory: `None` where it does (`aliases`) and takes memory of the buffer's type.
@@ -3740,7 +3741,7 @@ impl Device {
row_pitch: report.row_pitch,
array_pitch: report.array_pitch,
};
let room = texels.buffer.padded_length() - texels.layout.offset;
let room = texels.buffer.texel_room() - texels.layout.offset;
let unobserved = texels.buffer.padding_unobserved();
if !aliases(&texels.layout, 0, room, &image, unobserved) {
// SAFETY: the image is live and unbound, and no command uses it.
@@ -32,8 +32,9 @@ pub(crate) enum Route {
/// over the buffer's memory holds its levels past the first, so the shadow, one image of every
/// level, a copy of the bytes taken at use (`shadow.rs`'s **Copies taken at use**).
Levels(u32),
/// Its texels run past the buffer's length, which no image over the buffer's memory reaches:
/// the shadow, its tail holding them.
/// Its texels run past the bytes the buffer's memory holds for them (`Texels::kept`: a
/// no-copy buffer's or a buffer's under 16 KiB length), which no image over that memory
/// reaches: the shadow, its tail holding them.
Past,
/// The buffer's bytes are no host pages: an image aliasing the buffer's memory where `holds`
/// (an image over the texels may hold them, and what besides, `linear_plan::holding`: their
@@ -96,9 +97,10 @@ pub(crate) enum HostWay {
Shadow(ShadowCause),
}
/// The route of a texture of `levels` whose texels run past its buffer's length where `past`,
/// whatever holds its bytes: the shadow for a tiled texture of more than one level, and for texels
/// past the length ([`Device::route_over_buffer`]); `None` for any other.
/// The route of a texture of `levels` whose texels run past the bytes its buffer's memory holds
/// for them where `past` (`Texels::kept`), whatever holds its bytes: the shadow for a tiled
/// texture of more than one level, and for texels past those bytes
/// ([`Device::route_over_buffer`]); `None` for any other.
fn levels_or_past((levels, past): (u32, bool)) -> Option<Route> {
if levels > 1 {
return Some(Route::Levels(levels));
@@ -121,8 +121,11 @@ pub enum TextureShadowReason {
/// The texture's level count.
levels: u32,
},
/// The texels run past the buffer's length, which no image over the buffer's memory reaches:
/// the texture's tail holds those past it, zero when made.
/// The texels run past the bytes the buffer's memory holds for them, which no image over
/// that memory reaches: past a no-copy buffer's length, or a buffer's under 16 KiB (one of at
/// least 16 KiB the device made holds its allocation, its length rounded up to 16 KiB, and a
/// heap's buffer its heap to its end). The texture's tail holds those past them, zero when
/// made.
PastLength,
/// Over a buffer of the device's own memory ([`TexturePlacement::NotHostPages`]'s): its rows
/// (or slices) lie with bytes of the buffer between them that are not its texels, or share
@@ -268,8 +271,9 @@ impl TextureShadowReason {
"it has {levels} levels, and no image over the buffer's memory holds levels past \
the first"
),
TextureShadowReason::PastLength => "its texels run past the buffer's length, which no \
image over the buffer's memory reaches"
TextureShadowReason::PastLength => "its texels run past the bytes the buffer's memory \
holds for them, which no image over that memory \
reaches"
.to_owned(),
TextureShadowReason::BytesBetweenRows => "its rows lie with bytes of the buffer \
between them, which the buffer does not \
@@ -317,6 +317,7 @@ impl Device {
offset: 0,
base: 0,
length,
allocation: length,
reach: length,
modes,
address,
+1 -1
View File
@@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523;
/// changes no behaviour, and then updates that hash alone with the commit saying why, or changes
/// behaviour and also bumps this version. The commit that bumps it says what changed; this comment
/// keeps no history.
pub const RUNTIME_VERSION: u32 = 2354;
pub const RUNTIME_VERSION: u32 = 2355;
/// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex,
/// of the manifest, the build script, and exactly the files the crate roots' module tree compiles
+36 -27
View File
@@ -2433,8 +2433,9 @@ bound.
first reach of its image, and one only ever bound through its rows gives none.
- `metal2vulkan-degrade-staged-texture-buffer` (performance). `StagedTextureBuffer`: A texture
buffer is viewed through a staging buffer: its texels start where the device views none
(`minTexelBufferOffsetAlignment`), or run past its buffer's length, where its staging buffer is
its tail. Cost: its texels are copied into a staging buffer before each use and back after each
(`minTexelBufferOffsetAlignment`), or run past the bytes its buffer's memory holds for them (a
no-copy buffer's or a buffer's under 16 KiB length), where its staging buffer is its tail.
Cost: its texels are copied into a staging buffer before each use and back after each
write.
- `metal2vulkan-degrade-shadow-merge` (performance). `ShadowMerge`: One command writes a buffer's
bytes through a shadowed texture or a staged texture buffer and also another way. Cost: the
@@ -5867,36 +5868,44 @@ each item named states the rule in full.
(`probes/one_plane_ycbcr_heap_rows.m`), the even rows' at 10 x 3, 12 x 3, 20 x 3 and 13 x 7,
the later row's at 33 x 3 and the earlier row's at 100 x 30. The runtime's GPU writes leave
the earlier row's, one of those, the same every time.
- **A texture over a buffer keeps its texels past the buffer's length in a tail of its own.**
Plain Metal makes a 2D texture, an array or a texture buffer over a buffer whose texels run
past its length, up to where the buffer's allocation ends, counted from the buffer's start
(nil past it): its `allocatedSize` (its length rounded up to 16 KiB), a heap's buffer's heap's
size, a no-copy buffer's pages (its pointer's offset in its 16 KiB page plus its length,
rounded up to 16 KiB). A 2D texture and a texture buffer count to their last texel, an array
to its slices' end, each slice rounded up to 128 bytes (`BufferTextureProblem::PastAllocation`;
an Apple M3 Max under macOS 27.0.1, each kind and form bisected). The runtime makes more than
the M3 Max in two cases, as an AMD Radeon Pro 5300M and an Intel UHD 630 under macOS 26.7.1
do (each end measured made, to 16 MiB): a buffer under 16 KiB takes a texture however far
past its length, where the M3 Max answers nil past 131072 bytes of a buffer of its own, past
its pages of a no-copy buffer and past its heap's size of a heap's buffer; and an array whose
last texel lies inside the length is made whatever its slices' end. Such a texture is a copy:
a 2D texture or an array is a shadow image whose copies in and back go through its tail
(`shadowed-texture`), and a texture buffer's functions view its tail, which copies in and back
too (`staged-texture-buffer`); its texels inside the length are the buffer's bytes on every
path. Metal's texels past the length are the memory after the buffer's: on an Apple M3 Max
under macOS 27.0.1
- **A texture over a buffer keeps its texels past what the buffer's memory holds in a tail of
its own.** Plain Metal makes a 2D texture, an array or a texture buffer
over a buffer whose texels run past its length, up to where the buffer's allocation ends,
counted from the buffer's start (nil past it): its `allocatedSize` (its length rounded up to
16 KiB), a heap's buffer's heap's size, a no-copy buffer's pages (its pointer's offset in its
16 KiB page plus its length, rounded up to 16 KiB). A 2D texture and a texture buffer count to
their last texel, an array to its slices' end, each slice rounded up to 128 bytes
(`BufferTextureProblem::PastAllocation`; an Apple M3 Max under macOS 27.0.1, each kind and form
bisected). The runtime makes more than the M3 Max in two cases, as an AMD Radeon Pro 5300M and
an Intel UHD 630 under macOS 26.7.1 do (each end measured made, to 16 MiB): a buffer under
16 KiB takes a texture however far past its length, where the M3 Max answers nil past 131072
bytes of a buffer of its own, past its pages of a no-copy buffer and past its heap's size of a
heap's buffer; and an array whose last texel lies inside the length is made whatever its slices'
end. Where the buffer's memory holds the texels, the texture lies there as one inside the length
does, as Metal's does: a buffer of 16 KiB or more that the device makes (`new_buffer`,
`new_buffer_with_bytes`, `new_buffer_with_descriptor` but for an exportable one) holds its
allocation, its `VkBuffer` its length rounded up to 16 KiB, zero when made; and a heap's buffer
reaches to its heap's end, its texels past its length the heap's bytes there, another
resource's where one lies there, as on Metal. The buffer itself still ends at its length: a
blit of its bytes past it is refused, as Metal's validation refuses it. Past a no-copy buffer's
length (its pages past it are the caller's), past a wrapped `VkBuffer`'s or an exportable
buffer's, past a buffer's under 16 KiB, and past a heap's end, the texture is a copy: a 2D texture or an array is a shadow image
whose copies in and back go through its tail (`shadowed-texture`), and a texture buffer's
functions view its tail, which copies in and back too (`staged-texture-buffer`); its texels the
buffer's memory holds are the buffer's bytes on every path. Metal's texels past the length are
the memory after the buffer's: on an Apple M3 Max under macOS 27.0.1
(`probes/texels_past_buffer_length.m`, `probes/texture_buffer_past_length.m`) a kernel's
`read()` and `sample()`, a blit and `getBytes` read the next allocation's bytes there, and a
kernel's `write()`, a blit and `replaceRegion` write them, changing that allocation; past a
20000-byte buffer's length, inside its allocation, they read zero. On both, what one path writes
there every path reads, and the texels inside the length are the buffer's bytes. The runtime's
texels past the length lie in the texture's tail (`Buffer::new_texture`): zero when made, read
and written alike by its functions, blits and CPU calls, and no other allocation's bytes; two
textures over one buffer past its length keep a tail each. A colour texture over a plane of an
IOSurface with planes keeps its texels past the surface in a tail of its own too, announced
(`metal2vulkan-degrade-surface-texels-tail`), where Metal lays them in no one place; the plane
shapes that keep no tail (a two-plane YCbCr, two-plane extended-range, 4:2:2, depth, stencil,
BC or rotated texture) stay `NotImplemented(TexelsPastBufferLength)` there.
texels past what the buffer's memory holds lie in the texture's tail (`Buffer::new_texture`):
zero when made, read and written alike by its functions, blits and CPU calls, and no other
allocation's bytes; two textures over one buffer past it keep a tail each. A colour texture
over a plane of an IOSurface with planes keeps its texels past the surface in a tail of its own
too, announced (`metal2vulkan-degrade-surface-texels-tail`), where Metal lays them in no one
place; the plane shapes that keep no tail (a two-plane YCbCr, two-plane extended-range, 4:2:2,
depth, stencil, BC or rotated texture) stay `NotImplemented(TexelsPastBufferLength)` there.
- **A 2D array of several slices over an IOSurface plane keeps its slices past the first in a
tail of its own.** Over a plane of a surface with planes Metal makes a 2D array of any slices.
On an Apple M3 Max under macOS 27.0.1 (`probes/surface_texture_envelope.m`) its first slice
+11
View File
@@ -567,6 +567,17 @@ recorded here.
before at 11.3-11.4 GB/s, and at 395.6-396.0 GB/s on the RTX 2080 (NVIDIA's driver), 2.7 GB/s
before; a `Shared` no-copy buffer, which stays in its pages, ran at 11.3-11.4 and 2.7 GB/s both
times, and a `Private` one at 865.0-867.3 and 395.6-396.0 GB/s.
- **A buffer of 16 KiB or more holds Metal's allocation.** Metal's `allocatedSize` of such a
buffer is its length rounded up to 16 KiB, and a texture over the buffer whose texels run past
the length lies there (docs/API.md's accepted differences). A buffer the device makes in the
Metal address space with a length of 16 KiB or more takes as much (`buffer.rs`'s
`metal_allocation`): its `VkBuffer` and placement are its length rounded up to 16 KiB, so such a
texture is the buffer's memory, in place where the device makes an image over it, where it would
otherwise be a copy through a tail of its own. Each such buffer takes up to 16383 bytes of memory
more than its length, which its first fill clears with the rest (the host's clear of a `Shared`
or `Managed` buffer, the device's fill of a `Private` one, `Buffer::clear_first`), with no
command of its own. A heap's buffer, a no-copy, wrapped or exportable buffer, and every buffer
under 16 KiB take their length.
- **A command buffer's copies in and new buffers' fills come after the work before it**, as Metal's
copy of a `Managed` buffer's modified range and a new buffer's contents come before the commands
that take them and after those before. On a device whose family of transfers alone has two queues