Runtime: synchronizeResource: of a texture over a Managed buffer copies its bytes to the host

On a device that keeps a device copy of each Managed buffer (a discrete card: rule 2's unified
memory form, decided by Vulkan's device type), a texture over a Managed buffer
(Buffer::new_texture) lies on the device copy, in place or in an image of its own that goes back to
it. Metal's texture over a buffer shares the buffer's storage, so a program that writes such a
texture with a kernel and then calls synchronizeResource: of the texture reads the texels in the
buffer's contents once the command buffer completes. The runtime recorded nothing for that call:
BlitCommandEncoder::read_back_texture (blit.rs) returned at Texture::named_bytes() (texture.rs),
which answers only a texture made by Device::new_managed_texture (Form::Managed), never a texture
over a buffer (Form::InPlace or Form::Copied). The consumer's bytes kept what the host had left
there, and the program read them.

Now read_back_texture first asks Texture::over_device_copy (new, crate-private): for a texture over
a buffer whose buffer keeps a device copy, the buffer and the bytes its texels lie in (from the
texture's offset through the last texel of its last row, within the buffer). The synchronize is
then the buffer's own, over that range: read_back is read_back_range over the whole buffer, and
read_back_range takes the copies back owed by textures in images of their own first
(managed_reach, managed_backs), then records one vkCmdCopyBuffer of the range of the device copy
into the consumer's bytes between the buffer synchronize's two barriers, marking the range written
for the host's coherence. This is the copy Metal itself makes at the program's synchronize; no new
query and no new form: the two-copy arm only, and a device with unified memory records nothing, as
before (its texture lies in the bytes the host reads).

Found by a recording of a guest compute benchmark whose Gaussian blur failed validation on the RX
7900 XTX and the RTX 3090 and passed on the Iris Xe: whole plays at 1db5cfb2b0 on the XTX (jobs
m3-max-75524 and m3-max-75526, R and R2 dumps equal) left the last stage's RGBA8 output, a texture
over a Managed no-copy buffer, all zero after the program's synchronizeResource: of it (job
m3-max-75541, a probe player that dumps a synchronized texture), with the five earlier stages
plausible.

Test: every_device_synchronizes_a_texture_over_a_managed_buffer (copy_over.rs, the commit before),
which runs on devices that keep a device copy; a device with unified memory prints its skip. The
runtime README's synchronize_resource line says what it records for such a texture.
This commit is contained in:
Anees Iqbal committed 2026-11-26 13:26:42 -08:00
1 parent e9f09c9a92
commit d0f117f2a8
3 files changed
+56 -15

No files matched your search

+8 -5
View File
@@ -2981,11 +2981,14 @@ while the driver's exit handler waited on a lock, with every runtime thread alre
- `synchronize_resource` is `synchronizeResource:`, counted in `Metrics::synchronizations` and
never a submission of its own. Of a Managed buffer with a device copy it records one
`vkCmdCopyBuffer` of the copy into the consumer's bytes between the same two barriers; of a
Managed texture with named bytes, on every device, one `vkCmdCopyImageToBuffer` of each level
and slice into its place in them, between one pair of barriers. Either marks the segment so its
end barrier reaches the host: the host reads the GPU's writes before the call once the command
buffer completes or it sees an event signaled after it. Of any other buffer, which the host
reads where shaders write it, and of a texture without named bytes, which has none the host
texture over such a buffer (`Buffer::new_texture`), whose texels are the buffer's bytes as in
Metal, the same copy of the bytes its texels lie in, from its offset through its last texel,
after its image of its own, where it has one, goes back to the copy; of a Managed texture with
named bytes, on every device, one `vkCmdCopyImageToBuffer` of each level and slice into its
place in them, between one pair of barriers. Each marks the segment so its end barrier reaches
the host: the host reads the GPU's writes before the call once the command buffer completes or
it sees an event signaled after it. Of any other buffer, which the host reads where shaders
write it, of a texture over one, and of a texture without named bytes, which has none the host
reads apart from its image, it records nothing. Metal's validation stops it on a resource
that is not Managed; the runtime keeps no storage mode for those and does not refuse it.
- `generate_mipmaps` is `generateMipmapsForTexture:`: each level below the texture's first made
+30 -10
View File
@@ -1027,12 +1027,17 @@ impl BlitCommandEncoder {
/// ([`Device::new_managed_texture`](crate::Device::new_managed_texture)), on every device, is
/// one `vkCmdCopyImageToBuffer` of each level and slice of its image into its place in them,
/// between one pair of a buffer copy's barriers, as
/// [`copy_from_texture_to_buffer`](Self::copy_from_texture_to_buffer) records. Either marks
/// [`copy_from_texture_to_buffer`](Self::copy_from_texture_to_buffer) records. A texture over
/// a Managed buffer with a device copy ([`Buffer::new_texture`](crate::Buffer::new_texture)),
/// whose texels are the buffer's bytes, as Metal's texture over a buffer shares the buffer's
/// storage, is the buffer's synchronize over the bytes its texels lie in: its image of its
/// own, where it has one, goes back to the device copy first, then one `vkCmdCopyBuffer` of
/// that range of the copy into the consumer's bytes. Each marks
/// the segment so its end barrier reaches the host, and the command buffer keeps the resource
/// until it retires. A buffer with no device copy is one the host reads where shaders write
/// it, and a texture without named bytes has none the host reads apart from its image: the
/// call records nothing for either, nor for an indirect command buffer, whose records the host
/// never reads.
/// until it retires. A buffer with no device copy, and a texture over one, are what the host
/// reads where shaders write them, and a texture without named bytes has none the host reads
/// apart from its image: the call records nothing for any of them, nor for an indirect command
/// buffer, whose records the host never reads.
///
/// Metal's validation stops the call on a resource that is not Managed (probe
/// `blit_synchronize_rules.m` in the conformance crate, an Apple M3 Max, macOS 27.0.1, job
@@ -1057,6 +1062,12 @@ impl BlitCommandEncoder {
/// A Managed texture's synchronize: each level and slice of its image into its place in the
/// named bytes, where it has them.
fn read_back_texture(&mut self, texture: &Texture) -> Result<(), Error> {
// A texture over a Managed buffer's device copy holds its texels in the buffer's bytes,
// in place or through an image of its own that goes back to the copy first: Metal's
// synchronize of it is the buffer's, over the bytes those texels lie in.
if let Some((buffer, start, length)) = texture.over_device_copy() {
return self.read_back_range(&buffer, start, length);
}
let Some((bytes, places)) = texture.named_bytes() else {
return Ok(());
};
@@ -1097,14 +1108,23 @@ impl BlitCommandEncoder {
/// A Managed buffer's synchronize: its device copy into the consumer's bytes, where it has
/// one.
fn read_back(&mut self, buffer: &Buffer) -> Result<(), Error> {
self.read_back_range(buffer, 0, buffer.length())
}
/// The bytes `[start, start + length)` of a Managed buffer's device copy into the consumer's
/// bytes, where it has one: a synchronize of the buffer (all of it) or of a texture over it
/// (the bytes the texture's texels lie in).
fn read_back_range(&mut self, buffer: &Buffer, start: u64, length: u64) -> Result<(), Error> {
let (core, building) = self.parts();
if let (block, Some(copy)) = buffer.held() {
let ((from, src_offset), (to, dst_offset)) =
(copy.range(0), block.memory.range(buffer.offset()));
let ((from, src_offset), (to, dst_offset)) = (
copy.range(start),
block.memory.range(buffer.offset() + start),
);
let region = vk::BufferCopy {
src_offset,
dst_offset,
size: buffer.length(),
size: length,
};
building.keep(buffer);
// The textures over the device copy go back to it first (`crate::over`).
@@ -1118,8 +1138,8 @@ impl BlitCommandEncoder {
if let Some(held) = building.coherence() {
held.wrote(crate::coherence::Range::of_block(
&block.memory,
buffer.offset(),
buffer.length(),
buffer.offset() + start,
length,
held.atom,
));
}
@@ -4188,6 +4188,24 @@ impl Texture {
Some((bytes, &named.places))
}
/// Of a texture over a Managed buffer that keeps a device copy ([`Buffer::new_texture`], in
/// place or in an image of its own): the buffer, and the bytes of it its texels lie in, from
/// the texture's offset through the last texel of its last row, within the buffer. `None` for
/// any other texture.
pub(crate) fn over_device_copy(&self) -> Option<(Buffer, u64, u64)> {
let inner = &self.inner;
let named = (inner.named.as_ref()).filter(|named| named.form != Form::Managed)?;
named.buffer.held().1?;
let unit = inner.unit?;
let place = named.places[0];
let d = &inner.descriptor;
let rows = u64::from(d.height.max(1) - 1) * place.bytes_per_row;
let span = rows + u64::from(d.width) * u64::from(unit.bytes);
let bytes = Buffer::of(Arc::clone(&named.buffer), self.device.clone());
let length = span.min(bytes.length().saturating_sub(place.offset));
Some((bytes, place.offset, length))
}
/// `newTextureViewWithPixelFormat:textureType:levels:slices:`: a texture over `levels` and
/// `slices` of this one's image, of `texture_type`, at `index`, with elements of set 1 of its
/// own and no copy; `newTextureViewWithPixelFormat:` is the call with the texture's own type,