rt: a blit encoder synchronizes a Managed texture, each level and slice of its image into its named bytes
synchronize_resource of a Managed texture with named bytes (Device::new_managed_texture, on every device) records one vkCmdCopyImageToBuffer of each level and slice of its image into its place in the named buffer, between one pair of a buffer copy's barriers, and marks the segment so its end barrier reaches the host. A texture without named bytes still records nothing; every call is counted in Metrics::synchronizations. texture.rs gains one read hook, Texture::named_bytes (the named buffer and its per-level/slice places), which main agreed to. The blit encoder's buffer copy now records through record_buffer_copies, which takes a list of checked copies and puts each copy's level and slice in the same two barriers; one copy records exactly as before. new_managed_texture's doc names the synchronize. Test (device cfgs): every_device_synchronizes_a_managed_texture_into_its_named_bytes: a 2-level 4x4 RGBA8Unorm Managed texture with places 256-byte rows apart; copies into its image alone leave the named bytes zero; the synchronize puts level 0 at offset 0 and level 1 at 1024, the bytes between the rows kept, at 1 submission, 3 barriers, 0 bytes copied, 1 synchronization, 1 host reach. Level 0 is plain Metal's: probe blit_synchronize_rules.m, job m3-max-43667, getBytes of a Managed texture copied into then synchronized hashes a918a78fc82cbba5 (the managed-buffer case still 5d28bc84b408472d).
This commit is contained in:
1 parent
b025e83119
commit
9771d45fdb
5 files changed
+218
-38
No files matched your search
@@ -436,7 +436,6 @@ that touch blocks first.
|
||||
resource IDs it made up, with no fault. The consumer treats every byte on the device as
|
||||
guest-writable and keeps nothing there that it parses or trusts.
|
||||
- Presenting (the binary semaphore a command buffer signals for a present) comes with presenting.
|
||||
- A Managed texture's `synchronizeResource` comes with blit encoders.
|
||||
- An indirect dispatch's threadgroup counts are the GPU's to read, so a count above the device's
|
||||
`maxComputeWorkGroupCount` is not refused; Vulkan leaves such a dispatch undefined. A direct
|
||||
dispatch past it is split into dispatches of at most that many, each told its first
|
||||
@@ -550,11 +549,13 @@ that touch blocks first.
|
||||
on whole 4-byte words of the bytes shaders reach, which Metal fills byte by byte.
|
||||
- `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, marking
|
||||
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, which has no bytes the
|
||||
host reads apart from its image, it records nothing. Metal's validation stops it on a resource
|
||||
`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
|
||||
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.
|
||||
- The slice-and-level and whole-texture forms of `copyFromTexture`,
|
||||
`synchronizeTexture:slice:level:` (no recorded stream calls it), and the other blit commands
|
||||
|
||||
@@ -17,8 +17,10 @@
|
||||
//! as a copy's, the second making its writes visible to every later command.
|
||||
//!
|
||||
//! A synchronize of a Managed buffer with a device copy is one `vkCmdCopyBuffer` of the copy into
|
||||
//! the consumer's bytes between the same two barriers, marking the segment so its end reaches the
|
||||
//! host; of any other resource it records nothing.
|
||||
//! the consumer's bytes between the same two barriers, and one of a Managed texture with named
|
||||
//! bytes a `vkCmdCopyImageToBuffer` of each level and slice into its place, between one pair of
|
||||
//! them; either marks the segment so its end reaches the host. Of any other resource it records
|
||||
//! nothing.
|
||||
|
||||
use crate::buffer::Buffer;
|
||||
use crate::command::{Building, CommandBuffer};
|
||||
@@ -26,7 +28,7 @@ use crate::counters::Metric;
|
||||
use crate::encoder::Resource;
|
||||
use crate::error::{Error, Gap, Refusal};
|
||||
use crate::record::{memory_barrier, Reach};
|
||||
use crate::texture::{place, CopyUnit, Placed, Region, Texture, TextureDescriptor};
|
||||
use crate::texture::{place, BytesPlace, CopyUnit, Placed, Region, Texture, TextureDescriptor};
|
||||
use ash::vk;
|
||||
use metal2vulkan::metal::TextureType;
|
||||
use std::ops::Range;
|
||||
@@ -237,13 +239,12 @@ impl BlitCommandEncoder {
|
||||
layout: BufferLayout,
|
||||
into_image: bool,
|
||||
) -> Result<(), Error> {
|
||||
let building = self.building();
|
||||
let core = Arc::clone(building.device.core());
|
||||
let core = Arc::clone(self.building().device.core());
|
||||
if !texture.is_of(&core) || !buffer.is_of(&core) {
|
||||
return Err(Error::Refused(Refusal::OtherDevice));
|
||||
}
|
||||
// A Managed buffer's GPU side is its device copy, where it has one.
|
||||
let (vk_buffer, base) = buffer.device_range();
|
||||
let base = buffer.device_range().1;
|
||||
let lies = Lies {
|
||||
base,
|
||||
len: buffer.length(),
|
||||
@@ -252,6 +253,22 @@ impl BlitCommandEncoder {
|
||||
let Some(copy) = buffer_copy(&Side::of(texture), at, size, lies, into_image)? else {
|
||||
return Ok(());
|
||||
};
|
||||
self.record_buffer_copies(texture, buffer, &[copy], into_image)
|
||||
}
|
||||
|
||||
/// Records `copies` between `texture` and `buffer`, checked, between two barriers: the first
|
||||
/// after every earlier command, moving each copy's level and slice to a transfer layout; the
|
||||
/// second moving them home, a write into the buffer visible to every later command.
|
||||
fn record_buffer_copies(
|
||||
&mut self,
|
||||
texture: &Texture,
|
||||
buffer: &Buffer,
|
||||
copies: &[BufferCopy],
|
||||
into_image: bool,
|
||||
) -> Result<(), Error> {
|
||||
let building = self.building();
|
||||
let core = Arc::clone(building.device.core());
|
||||
let vk_buffer = buffer.device_range().0;
|
||||
building.keep_texture(texture);
|
||||
building.keep(buffer);
|
||||
let command = building.ordered_command()?;
|
||||
@@ -269,8 +286,12 @@ impl BlitCommandEncoder {
|
||||
false => (vk::ImageLayout::TRANSFER_SRC_OPTIMAL, Access::TRANSFER_READ),
|
||||
};
|
||||
let earlier = [memory_barrier(Reach::Commands)];
|
||||
let away = [texture.away(©.placed, layout, access)];
|
||||
let back = [texture.back(©.placed, layout)];
|
||||
let away: Vec<_> = (copies.iter())
|
||||
.map(|copy| texture.away(©.placed, layout, access))
|
||||
.collect();
|
||||
let back: Vec<_> = (copies.iter())
|
||||
.map(|copy| texture.back(©.placed, layout))
|
||||
.collect();
|
||||
let written = [written()];
|
||||
let written = match into_image {
|
||||
true => &written[..0],
|
||||
@@ -278,7 +299,7 @@ impl BlitCommandEncoder {
|
||||
};
|
||||
let device = &core.device;
|
||||
// SAFETY: the command buffer records outside any render pass and keeps the image and the
|
||||
// buffer until it retires; the subresource is in its home layout between
|
||||
// buffer until it retires; each copy's level and slice is in its home layout between
|
||||
// commands and in `layout` between the barriers; each region lies in the image and, by
|
||||
// `buffer_copy`, in the buffer's device range, its offset aligned as the command
|
||||
// requires.
|
||||
@@ -289,7 +310,7 @@ impl BlitCommandEncoder {
|
||||
.memory_barriers(&earlier)
|
||||
.image_memory_barriers(&away),
|
||||
);
|
||||
for region in copy.regions() {
|
||||
for region in copies.iter().flat_map(BufferCopy::regions) {
|
||||
match into_image {
|
||||
true => device.cmd_copy_buffer_to_image(
|
||||
command,
|
||||
@@ -383,27 +404,81 @@ impl BlitCommandEncoder {
|
||||
/// A Managed buffer with a device copy
|
||||
/// ([`Device::new_managed_buffer`](crate::Device::new_managed_buffer) on a device whose
|
||||
/// Shared memory is not device-local) is one `vkCmdCopyBuffer` of the copy into the
|
||||
/// consumer's bytes, between a fill's two barriers, the segment marked so its end barrier
|
||||
/// reaches the host; the command buffer keeps the buffer until it retires. A buffer with no
|
||||
/// device copy is one the host reads where shaders write it, and a texture has no bytes the
|
||||
/// host reads apart from its image: the call records nothing for either.
|
||||
/// consumer's bytes, between a fill's two barriers. A Managed texture with named bytes
|
||||
/// ([`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
|
||||
/// 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.
|
||||
///
|
||||
/// 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
|
||||
/// m3-max-43632); the runtime keeps no storage mode for a buffer without a device copy or for a
|
||||
/// texture, and records nothing there.
|
||||
/// texture without named bytes, and records nothing there.
|
||||
///
|
||||
/// Err: [`Refusal::OtherDevice`] for another device's resource; [`Error::Vulkan`] where the
|
||||
/// command buffer cannot record.
|
||||
pub fn synchronize_resource(&mut self, resource: Resource<'_>) -> Result<(), Error> {
|
||||
let core = Arc::clone(self.building().device.core());
|
||||
match resource {
|
||||
Resource::Buffer(buffer) if buffer.is_of(&core) => self.read_back(buffer)?,
|
||||
Resource::Texture(texture) if texture.is_of(&core) => {
|
||||
self.read_back_texture(texture)?
|
||||
}
|
||||
_ => return Err(Error::Refused(Refusal::OtherDevice)),
|
||||
}
|
||||
core.counters.count(Metric::Synchronizations);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
let Some((bytes, places)) = texture.named_bytes() else {
|
||||
return Ok(());
|
||||
};
|
||||
let d = texture.descriptor();
|
||||
let slices = places.len() as u32 / d.mipmap_level_count;
|
||||
let side = Side::of(texture);
|
||||
let base = bytes.device_range().1;
|
||||
let mut copies = Vec::with_capacity(places.len());
|
||||
for (at, &place) in (0..).zip(places) {
|
||||
let BytesPlace {
|
||||
offset,
|
||||
bytes_per_row,
|
||||
bytes_per_image,
|
||||
} = place;
|
||||
let layout = BufferLayout {
|
||||
offset,
|
||||
bytes_per_row,
|
||||
bytes_per_image,
|
||||
};
|
||||
let lies = Lies {
|
||||
base,
|
||||
len: bytes.length(),
|
||||
layout,
|
||||
};
|
||||
let level = at / slices;
|
||||
let at = TexturePlace {
|
||||
slice: at % slices,
|
||||
level,
|
||||
origin: [0; 3],
|
||||
};
|
||||
let size = [d.width, d.height, d.depth].map(|extent| (extent >> level).max(1));
|
||||
copies.extend(buffer_copy(&side, at, size, lies, false)?);
|
||||
}
|
||||
self.record_buffer_copies(texture, bytes, &copies, false)
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
let building = self.building();
|
||||
let core = Arc::clone(building.device.core());
|
||||
let buffer = match resource {
|
||||
Resource::Buffer(buffer) if buffer.is_of(&core) => Some(buffer),
|
||||
Resource::Texture(texture) if texture.is_of(&core) => None,
|
||||
_ => return Err(Error::Refused(Refusal::OtherDevice)),
|
||||
};
|
||||
if let Some((buffer, (block, Some(copy)))) = buffer.map(|buffer| (buffer, buffer.held())) {
|
||||
if let (block, Some(copy)) = buffer.held() {
|
||||
let (from, to) = (copy.buffer(), block.memory.buffer);
|
||||
let region = vk::BufferCopy {
|
||||
src_offset: copy.offset(),
|
||||
@@ -434,7 +509,6 @@ impl BlitCommandEncoder {
|
||||
core.counters.count(Metric::Barriers);
|
||||
core.counters.count(Metric::Barriers);
|
||||
}
|
||||
core.counters.count(Metric::Synchronizations);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -300,8 +300,9 @@ pub struct Metrics {
|
||||
/// Calls that waited for an upload to complete before they recorded: the upload at its
|
||||
/// bound of calls or bytes, or the staging region with no room for a copy.
|
||||
pub upload_waits: u64,
|
||||
/// `synchronizeResource:` calls: each one copy into the host's bytes of a Managed buffer with
|
||||
/// a device copy, and nothing for any other resource; none a submission of its own.
|
||||
/// `synchronizeResource:` calls: each a copy into the host's bytes of a Managed buffer with a
|
||||
/// device copy or of a Managed texture with named bytes, and nothing for any other resource;
|
||||
/// none a submission of its own.
|
||||
pub synchronizations: u64,
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,11 @@
|
||||
//! a fill before it, to plain Metal's bytes (probe `blit_synchronize_rules.m`, job
|
||||
//! m3-max-43632): by one copy between two barriers where the buffer has a device copy, which
|
||||
//! the fill alone does not reach; by nothing where it has none. Each is counted and is no
|
||||
//! submission of its own; one of a texture records nothing. A blit's segment ends reaching the
|
||||
//! host exactly where it wrote Shared memory or synchronized a device copy.
|
||||
//! submission of its own; one of a texture without named bytes records nothing. A synchronize
|
||||
//! of a Managed texture copies each level of its image into its place in the named bytes,
|
||||
//! level 0 plain Metal's getBytes after a synchronize (job m3-max-43667), between one pair of
|
||||
//! barriers. A blit's segment ends reaching the host exactly where it wrote Shared memory or
|
||||
//! synchronized a device copy or a Managed texture.
|
||||
|
||||
use crate::blit::{
|
||||
buffer_copy, fill_range, texture_copy, BufferCopy, BufferLayout, ImageCopy, Lies, Side,
|
||||
@@ -740,9 +743,9 @@ fn a_fill_takes_whole_words_of_a_range_inside_the_buffer() {
|
||||
mod on_device {
|
||||
use crate::tests::command::on_device::{each_device, finished};
|
||||
use crate::{
|
||||
BlitCommandEncoder, Buffer, BufferLayout, CommandBuffer, Device, DispatchType, Error, Gap,
|
||||
IndexClass, MemoryClass, PixelFormat, Refusal, Region, Resource, StorageMode, Texture,
|
||||
TextureDescriptor, TexturePlace, TextureType, TextureUsage,
|
||||
BlitCommandEncoder, Buffer, BufferLayout, BytesPlace, CommandBuffer, Device, DispatchType,
|
||||
Error, Gap, IndexClass, MemoryClass, PixelFormat, Refusal, Region, Resource, StorageMode,
|
||||
Texture, TextureDescriptor, TexturePlace, TextureType, TextureUsage,
|
||||
};
|
||||
|
||||
/// A byte pattern of `len` bytes, different at every offset of one call and between calls.
|
||||
@@ -1482,7 +1485,7 @@ mod on_device {
|
||||
/// alone leaves the host's bytes as they were, and the synchronize is one copy between two
|
||||
/// barriers; where it has none the host reads the fill's bytes at once and the synchronize
|
||||
/// records nothing. Either way it is counted, is no submission of its own, and the segment's
|
||||
/// end reaches the host. A synchronize of a texture records nothing.
|
||||
/// end reaches the host. A synchronize of a texture without named bytes records nothing.
|
||||
#[test]
|
||||
fn every_device_synchronizes_a_managed_buffer() {
|
||||
each_device("blit_synchronize", |name, _, device| {
|
||||
@@ -1551,7 +1554,7 @@ mod on_device {
|
||||
"{name}: the synchronized fill (device copy: {copied})"
|
||||
);
|
||||
|
||||
// A texture's synchronize records nothing.
|
||||
// A synchronize of a texture without named bytes records nothing.
|
||||
device
|
||||
.grant_indices(IndexClass::Texture, 4096, 1)
|
||||
.expect("a grant");
|
||||
@@ -1649,4 +1652,96 @@ mod on_device {
|
||||
assert_eq!(read(&shared), vec![1; 256], "{name}: the bytes");
|
||||
});
|
||||
}
|
||||
|
||||
/// A synchronize of a Managed texture copies each level of its image into its place in the
|
||||
/// consumer's named bytes, rows 256 bytes apart, everything between them kept, level 0 plain
|
||||
/// Metal's: one submission, two barriers and the segment's end reaching the host, no byte of
|
||||
/// the runtime's own. Copies into the texture alone leave the named bytes as they were.
|
||||
#[test]
|
||||
fn every_device_synchronizes_a_managed_texture_into_its_named_bytes() {
|
||||
each_device("blit_synchronize_texture", |name, _, device| {
|
||||
let block = (device.take_block(MemoryClass::Shared, 8192))
|
||||
.expect("a Shared block")
|
||||
.id;
|
||||
let named = (device.new_buffer(block, 0, 2048, true)).expect("a buffer");
|
||||
let source = (device.new_buffer(block, 4096, 128, true)).expect("a buffer");
|
||||
let (level0, level1) = (pattern(64, 21), pattern(16, 99));
|
||||
write(&source, &[level0.clone(), level1.clone()].concat());
|
||||
let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 4, 4);
|
||||
d.mipmap_level_count = 2;
|
||||
d.usage = TextureUsage::RENDER_TARGET;
|
||||
let place = |offset| BytesPlace {
|
||||
offset,
|
||||
bytes_per_row: 256,
|
||||
bytes_per_image: 0,
|
||||
};
|
||||
let texture = (device.new_managed_texture(&d, None, &named, &[place(0), place(1024)]))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
let queue = device.new_command_queue(4).expect("a queue");
|
||||
let counters = device.counters();
|
||||
|
||||
// The copies into the image alone.
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
let mut encoder = command_buffer.blit_command_encoder().expect("an encoder");
|
||||
for (offset, bytes_per_row, side, level) in [(0, 16, 4, 0), (64, 8, 2, 1)] {
|
||||
let layout = BufferLayout {
|
||||
offset,
|
||||
bytes_per_row,
|
||||
bytes_per_image: 0,
|
||||
};
|
||||
let at = TexturePlace {
|
||||
level,
|
||||
..TexturePlace::default()
|
||||
};
|
||||
encoder
|
||||
.copy_from_buffer(&source, layout, [side, side, 1], &texture, at)
|
||||
.unwrap_or_else(|error| panic!("{name}: level {level}: {error}"));
|
||||
}
|
||||
encoder.end_encoding();
|
||||
run(name, &command_buffer);
|
||||
assert_eq!(read(&named), vec![0; 2048], "{name}: the copies alone");
|
||||
|
||||
// The synchronize.
|
||||
let metrics = counters.metrics();
|
||||
let reaches = counters.host_reaches();
|
||||
let command_buffer = queue.command_buffer().expect("a command buffer");
|
||||
let mut encoder = command_buffer.blit_command_encoder().expect("an encoder");
|
||||
encoder
|
||||
.synchronize_resource(Resource::Texture(&texture))
|
||||
.unwrap_or_else(|error| panic!("{name}: {error}"));
|
||||
encoder.end_encoding();
|
||||
run(name, &command_buffer);
|
||||
let after = counters.metrics();
|
||||
assert_eq!(
|
||||
(
|
||||
after.submissions - metrics.submissions,
|
||||
after.barriers - metrics.barriers,
|
||||
after.bytes_copied - metrics.bytes_copied,
|
||||
after.synchronizations - metrics.synchronizations,
|
||||
counters.host_reaches() - reaches,
|
||||
),
|
||||
(1, 3, 0, 1, 1),
|
||||
"{name}: submissions, barriers, bytes copied, synchronizations, host reaches"
|
||||
);
|
||||
let got = read(&named);
|
||||
let rows = |at: usize, row: usize, count: usize| -> Vec<u8> {
|
||||
(0..count)
|
||||
.flat_map(|y| got[at + y * 256..at + y * 256 + row].to_vec())
|
||||
.collect()
|
||||
};
|
||||
assert_eq!(
|
||||
rows(0, 16, 4),
|
||||
metal(level0, 0xa918_a78f_c82c_bba5),
|
||||
"{name}: level 0"
|
||||
);
|
||||
assert_eq!(rows(1024, 8, 2), level1, "{name}: level 1");
|
||||
let mut between = got.clone();
|
||||
for (at, row, count) in [(0, 16, 4), (1024, 8, 2)] {
|
||||
for y in 0..count {
|
||||
between[at + y * 256..at + y * 256 + row].fill(0);
|
||||
}
|
||||
}
|
||||
assert_eq!(between, vec![0; 2048], "{name}: the bytes between the rows");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -406,7 +406,9 @@ impl Device {
|
||||
/// consumer's bytes of the texture named in `bytes`, a buffer the host reaches, at `places`:
|
||||
/// one for each level and, within it, each slice in order (a cube's six faces each a slice, a
|
||||
/// 3D level one slice of several images). The consumer writes them; [`Texture::upload_region`]
|
||||
/// copies a region of them into the image. The texture keeps the buffer.
|
||||
/// copies a region of them into the image, and a blit encoder's
|
||||
/// [`synchronize_resource`](crate::BlitCommandEncoder::synchronize_resource) copies the image
|
||||
/// back into them. The texture keeps the buffer.
|
||||
///
|
||||
/// Metal keeps a Managed texture's bytes beside its image itself; here the consumer lays them
|
||||
/// out, and the device copies straight from them, so each place's rows are a whole number of
|
||||
@@ -1562,6 +1564,13 @@ impl Texture {
|
||||
self.inner.unit
|
||||
}
|
||||
|
||||
/// A Managed texture's named bytes ([`Device::new_managed_texture`]): the buffer and its
|
||||
/// places, one for each level and, within it, each slice. `None` for any other texture.
|
||||
pub(crate) fn named_bytes(&self) -> Option<(&Buffer, &[BytesPlace])> {
|
||||
let (bytes, places) = self.inner.managed.as_ref()?;
|
||||
Some((bytes, places))
|
||||
}
|
||||
|
||||
/// The barrier that moves `placed` from home to the transfer `layout` for a copy's
|
||||
/// `access`, after every earlier command's writes.
|
||||
pub(crate) fn away(
|
||||
|
||||
Reference in new issue
Block a user