In place: a texture over the runtime's pooled memory whose rows the device's linear image lays out otherwise takes a DRM_FORMAT_MOD_LINEAR image at Metal's pitch, where the device's check passed

A texture over a buffer of the runtime's own memory aliases it only where the device's own
linear image lies at the buffer's pitch. ANV lays linear rows tight, so a padded row (the
guest's device caches at 64-aligned pitches, rows ending mid-line) never aliases on the Iris
and every such texture is a shadow, copied in at each command buffer that uses it after a
write.

Where the device makes DRM_FORMAT_MOD_LINEAR images, such a texture now takes one with an
explicit plane at Metal's pitch (VkImageDrmFormatModifierExplicitCreateInfoEXT), no external
memory, bound in the buffer's memory. The image is made by the builder dma_buf.rs already has
(ModifierMemory gains Own: no external chain, no import check). Binding it to memory of no
external handle type is valid (VUID-VkImageCreateInfo-tiling-02261 asks only the modifier's
create info; VUID-vkBindImageMemory-image-01445 only a dedicated allocation where required,
which the runtime refuses), and a DRM_FORMAT_MOD_LINEAR image is linear, so in GENERAL it
aliases the buffer's bytes as host-accessible subresources do. It is made UNDEFINED
(VUID-VkImageCreateInfo-initialLayout-12478: PREINITIALIZED is for linear tiling alone), and a
move from UNDEFINED may discard the bytes: so the device's check gains a class for its pooled
memory (PagesClass::Pooled: a Shared buffer under half a pooled block that declares its padding
unobserved, its textures' images at Metal's pitch alone), and the image is taken only where its
verdicts pass for the texture's accesses, under HostPageTexturePolicy (Shadow turns it off).
CHECK_VERSION 11; the record holds 48 verdicts. The placement query answers a texture over the
device's own memory by the making's steps up to the bind (Device::alias_image_made): an
aliasing image (NotHostPages), or the shadow's cause. Counted: WorkCounts::pooled_pitched_images.

Verified so far: cargo fmt only; builds, tests and the device evidence follow.

(cherry picked from commit 25aa0cfda816f91a8148d4afbf81b19e7c870c07)
This commit is contained in:
Anees Iqbal committed 2026-11-08 03:29:50 -08:00
1 parent 7a562f0522
commit 99f47fb7e8
8 files changed
+342 -105

No files matched your search

+42 -19
View File
@@ -1286,10 +1286,10 @@ impl Device {
}))
}
/// A `DRM_FORMAT_MOD_LINEAR` image of `plan` for `memory` (dma-bufs, or host allocations),
/// viewed as `view_formats`, its plane laid out as `modifier` says (an explicit layout, or the
/// modifier listed for the driver to lay out), made `UNDEFINED` as Vulkan makes an image of
/// external memory; or the driver's refusal of it.
/// A `DRM_FORMAT_MOD_LINEAR` image of `plan` for `memory` (dma-bufs, host allocations, or the
/// runtime's own memory), viewed as `view_formats`, its plane laid out as `modifier` says (an
/// explicit layout, or the modifier listed for the driver to lay out), made `UNDEFINED`; or the
/// driver's refusal of it.
pub(super) fn drm_linear_image(
&self,
plan: &ImagePlan,
@@ -1298,18 +1298,23 @@ impl Device {
memory: ModifierMemory,
) -> Result<Result<vk::Image, vk::Result>, Error> {
let shared = self.shared();
let mut external =
vk::ExternalMemoryImageCreateInfo::default().handle_types(memory.handle_type());
let mut external = vk::ExternalMemoryImageCreateInfo::default();
let mut list = vk::ImageFormatListCreateInfo::default();
let info = plan
let mut info = plan
.create_info(shared.sharing(), view_formats, &mut list)
.tiling(vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT)
.initial_layout(vk::ImageLayout::UNDEFINED)
.push_next(&mut external)
.push_next(modifier);
if let Some(handle) = memory.handle_type() {
external = external.handle_types(handle);
info = info.push_next(&mut external);
}
// SAFETY: the device is live and enables the extension; `info` and its chain are valid
// and within the device's limits for such an image (`modifier_image_limits`), its one
// plane the modifier's one (`modifier_features`), made `UNDEFINED` for external memory.
// plane the modifier's one (`modifier_features`), made `UNDEFINED` as Vulkan makes an
// image of external memory and every image of a DRM format modifier's tiling
// (`VUID-VkImageCreateInfo-initialLayout-12478`: `PREINITIALIZED` is for linear tiling
// alone).
unsafe { crate::creation::image_or_refusal(shared.vk(), &info) }
}
@@ -1407,8 +1412,7 @@ impl Device {
.queue_family_indices(families),
false => modifier.sharing_mode(vk::SharingMode::EXCLUSIVE),
};
let mut external =
vk::PhysicalDeviceExternalImageFormatInfo::default().handle_type(memory.handle_type());
let mut external = vk::PhysicalDeviceExternalImageFormatInfo::default();
let mut list = vk::ImageFormatListCreateInfo::default().view_formats(view_formats);
let mut query = vk::PhysicalDeviceImageFormatInfo2::default()
.format(plan.format.into())
@@ -1416,8 +1420,11 @@ impl Device {
.tiling(vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT)
.usage(plan.usage.into())
.flags(plan.flags.into())
.push_next(&mut external)
.push_next(&mut modifier);
if let Some(handle) = memory.handle_type() {
external = external.handle_type(handle);
query = query.push_next(&mut external);
}
if !view_formats.is_empty() {
query = query.push_next(&mut list);
}
@@ -1446,6 +1453,10 @@ impl Device {
));
}
let limits = properties.image_format_properties;
// An image of the runtime's own memory imports none.
if memory.handle_type().is_none() {
return Ok(limits);
}
let features = imported.external_memory_properties.external_memory_features;
// An image for host allocations is bound at an offset of a buffer's import, which is no
// dedicated allocation.
@@ -1466,6 +1477,13 @@ impl Device {
}
impl Device {
/// Whether the device makes `DRM_FORMAT_MOD_LINEAR` images: it enables
/// `VK_EXT_image_drm_format_modifier`, which it does beside its dma-buf imports
/// (`HostImport::makes_dma_buf_images`).
pub(super) fn makes_modifier_images(&self) -> bool {
(self.shared().host_import.as_ref()).is_ok_and(|importer| importer.makes_dma_buf_images())
}
/// Why the device makes no `DRM_FORMAT_MOD_LINEAR` image of `plan` with an explicit plane,
/// where its driver renders such an image's rows past its height
/// ([`RuntimeDriverDefect::ExplicitPlaneRowsPastHeight`]) and the plan's height is no multiple
@@ -1485,28 +1503,33 @@ impl Device {
}
/// The memory a `DRM_FORMAT_MOD_LINEAR` image is made for: a dma-buf of its own pages
/// ([`Device::dma_buf_image`]), or host pages imported by their pointer, where a texture's image in
/// place lays its rows out at Metal's pitch (`texture/in_place_image.rs`).
/// ([`Device::dma_buf_image`]); host pages imported by their pointer, where a texture's image in
/// place lays its rows out at Metal's pitch (`texture/in_place_image.rs`); or a buffer's memory of
/// the runtime's own, no external memory, where a texture over the buffer lays its rows out at
/// Metal's pitch (`texture.rs`'s `alias_image`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ModifierMemory {
DmaBuf,
Host,
Own,
}
impl ModifierMemory {
/// The external memory handle type the image is made for.
fn handle_type(self) -> vk::ExternalMemoryHandleTypeFlags {
/// The external memory handle type the image is made for; none for the runtime's own memory.
fn handle_type(self) -> Option<vk::ExternalMemoryHandleTypeFlags> {
match self {
ModifierMemory::DmaBuf => DMA_BUF,
ModifierMemory::Host => vk::ExternalMemoryHandleTypeFlags::HOST_ALLOCATION_EXT,
ModifierMemory::DmaBuf => Some(DMA_BUF),
ModifierMemory::Host => Some(vk::ExternalMemoryHandleTypeFlags::HOST_ALLOCATION_EXT),
ModifierMemory::Own => None,
}
}
/// The memory, in words.
fn words(self) -> &'static str {
pub(super) fn words(self) -> &'static str {
match self {
ModifierMemory::DmaBuf => "dma-bufs",
ModifierMemory::Host => "host allocations",
ModifierMemory::Own => "the runtime's own memory",
}
}
}
+18 -4
View File
@@ -110,12 +110,24 @@ pub(crate) enum PagesClass {
/// A buffer of the runtime's own in fresh anonymous pages it imported by their pointer
/// (`fresh_pages.rs`): as [`Pointer`](Self::Pointer), in the memory type of the buffer's class.
Fresh,
/// A buffer of the runtime's own in a block of its pooled memory (`memory.rs`), no external
/// memory, where the device's linear image of the texture lays its rows out otherwise than
/// Metal (`texture.rs`'s `alias_image`): a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch
/// (`VkImageDrmFormatModifierExplicitCreateInfoEXT`), bound in the buffer's memory, whose
/// first move leaves `UNDEFINED`, as an image of a DRM format modifier's tiling is made in
/// (`VUID-VkImageCreateInfo-initialLayout-12478`: `PREINITIALIZED`, whose move keeps the
/// bytes, is for linear tiling alone).
Pooled,
}
impl PagesClass {
/// Every class, in the order the check runs them and the record holds their verdicts.
pub(crate) const ALL: [PagesClass; 3] =
[PagesClass::Pointer, PagesClass::Fresh, PagesClass::DmaBuf];
pub(crate) const ALL: [PagesClass; 4] = [
PagesClass::Pointer,
PagesClass::Fresh,
PagesClass::DmaBuf,
PagesClass::Pooled,
];
/// Its place in [`ALL`](Self::ALL).
pub(crate) fn index(self) -> usize {
@@ -123,6 +135,7 @@ impl PagesClass {
PagesClass::Pointer => 0,
PagesClass::Fresh => 1,
PagesClass::DmaBuf => 2,
PagesClass::Pooled => 3,
}
}
@@ -132,6 +145,7 @@ impl PagesClass {
PagesClass::Pointer => "host pages imported by their pointer",
PagesClass::DmaBuf => "host pages imported as dma-bufs",
PagesClass::Fresh => "fresh host pages the runtime imported for the buffer",
PagesClass::Pooled => "the runtime's pooled memory at Metal's pitch",
}
}
}
@@ -751,7 +765,7 @@ impl Drop for Waiting<'_> {
/// differs, and so does the version it holds). The runtime's own version is not in the record: a
/// release that changes none of these keeps the verdicts the driver gave, so a device made after
/// it reads them again rather than checking anew.
pub(crate) const CHECK_VERSION: u32 = 10;
pub(crate) const CHECK_VERSION: u32 = 11;
/// A record's first bytes.
const MAGIC: [u8; 8] = *b"M2VINPL\0";
@@ -828,7 +842,7 @@ pub(crate) fn record_name(identity: &DriverIdentity) -> String {
/// magic 8 bytes "M2VINPL\0"
/// version u32 CHECK_VERSION
/// identity 32 bytes DriverIdentity::digest
/// verdicts 36 bytes one per class, access and row ends, VERDICTS' order (verdict_index)
/// verdicts 48 bytes one per class, access and row ends, VERDICTS' order (verdict_index)
/// checksum 32 bytes SHA-256 of every byte before it
/// ```
///
@@ -402,6 +402,20 @@ impl Shape {
/// own lies in fresh pages (`fresh_pages.rs`), and room for the largest shape's window.
const BUFFER_BYTES: u64 = 40 << 20;
/// The bytes of the pooled class's buffer: within half a pooled block, so that it lies in a
/// block of the runtime's pooled memory as the product's buffers of that class do
/// (`fresh_pages::FRESH_PAGES_PAST`), with room for every shape's window but the 3840x2160
/// surface's, which such a buffer's texture takes no more than a smaller one's.
const POOLED_BYTES: u64 = 24 << 20;
/// The bytes of `class`'s buffer.
fn class_bytes(class: PagesClass) -> u64 {
match class {
PagesClass::Pooled => POOLED_BYTES,
PagesClass::Pointer | PagesClass::Fresh | PagesClass::DmaBuf => BUFFER_BYTES,
}
}
impl Device {
/// Start the device's check on a thread of its own, where it has not started and its policy
/// takes textures over host pages in place (`in_place.rs`). Where the thread cannot start,
@@ -585,7 +599,8 @@ fn check_class(
let page = host_page();
let line = device.shared().in_place.line();
let unchecked = || (vec![[Verdict::Unchecked; 2]; access.answered().len()], true);
let Some(pages) = ClassPages::make(device, class, BUFFER_BYTES) else {
let length = class_bytes(class);
let Some(pages) = ClassPages::make(device, class, length) else {
return Some(unchecked());
};
// The command queue an image that renders takes its passes on, the check's own, and the
@@ -624,7 +639,7 @@ fn check_class(
// The texel sizes of the shapes whose texels share a line made so far (`Shape::runs`).
let mut made_sharing = Vec::new();
for (shape, texel) in shapes {
if shape.window(page) > BUFFER_BYTES || !shape.runs(line, &made_sharing) {
if shape.window(page) > length || !shape.runs(line, &made_sharing) {
continue;
}
let rows = shape.rows(line);
@@ -1098,6 +1113,24 @@ impl ClassPages {
Some(ClassPages { buffer, start })
}
PagesClass::DmaBuf => dma_buf_pages(device, length),
// A buffer of the runtime's own in its pooled memory that declares the bytes between
// a texture's rows unobserved, as the product's buffers whose textures take such an
// image do (`texture.rs`'s `alias_image`): only where the device makes
// `DRM_FORMAT_MOD_LINEAR` images.
PagesClass::Pooled => {
if !device.makes_modifier_images() {
return None;
}
let shared = ResourceOptions::shared();
let outside = AddressPlacement::Outside;
let buffer = device.new_buffer_placed(length, shared, outside).ok()?;
if buffer.in_fresh_pages() {
return None;
}
buffer.declare_padding_unobserved();
let start = buffer.contents()?;
Some(ClassPages { buffer, start })
}
}
}
}
+182 -54
View File
@@ -81,7 +81,7 @@ use super::host_bytes::HostBytes;
use super::image_plan::{
check_image_limits, plan_image, DepthImageFacts, ImageFacts, ImagePlan, ImageRefusal,
};
use super::in_place::{self, PagesClass, ShadowCause};
use super::in_place::{self, PagesClass, Refusal, ShadowCause};
use super::key_hash::KeyMap;
use super::linear_plan::{aliases, check_linear_limits, linear_lack, LinearImage, LinearLack};
use super::memory::{MemoryClass, Placement};
@@ -2416,14 +2416,15 @@ impl Device {
// An aliasing image, counted where it took the feedback loop usage.
let alias = |image: Arc<Image>, texels: Arc<Texels>| {
let usage = image.plan().usage;
if usage.contains(ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT) {
let feedback = usage.contains(ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT);
if feedback && !in_place::checking() {
self.shared().counts.feedback_loop_alias_image();
}
Ok(Storage::Alias { image, texels })
};
// Where no image aliases the bytes, the reason the plan's attempt gives is the
// notice's.
let over = (&*texels, &holds);
let over = (&*texels, &holds, usage);
for &wider in &plans[..plans.len() - 1] {
let made = self.alias_image(wider.clone(), descriptor, over, &facts)?;
if let Ok(image) = made {
@@ -2432,6 +2433,13 @@ impl Device {
}
match self.alias_image(plan.clone(), descriptor, over, &facts)? {
Ok(image) => return alias(image, texels),
// The device's check of its pooled memory makes no shadow: its round is
// not made.
Err(_) if in_place::checking() => {
return Err(internal(
"the in-place check's texture takes no image over its pooled memory",
))
}
Err(why) => why,
}
}
@@ -2773,6 +2781,164 @@ impl Device {
}
}
/// The image [`alias_image`](Self::alias_image) binds over `texels` (of a texture of Metal
/// `usage`) in their buffer's memory, unbound: the device's own linear image, made
/// `PREINITIALIZED`, where it holds exactly Metal's texels there (`linear_plan::aliases`) in
/// memory of the buffer's type; else a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch, made
/// `UNDEFINED`, where the device makes one so and its check passed for its pooled memory
/// ([`Device::pooled_pitched_image`]). Why not: why the linear image is not, and why the
/// pitched image is not, its cause (a refused pitch, image or placement, or the policy and
/// the device's check), which the placement query answers. The device's check of its pooled
/// memory makes no linear image: it measures the pitched one.
fn alias_unbound(
&self,
plan: &ImagePlan,
texels: &Texels,
usage: TextureUsage,
facts: &ImageFacts<'_>,
) -> Result<Result<LinearMade, (String, ShadowCause)>, Error> {
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 unobserved = texels.buffer.padding_unobserved();
// 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.
let elsewhere = |made: &LinearMade| {
let image = LinearImage {
alignment: made.requirements.alignment,
size: made.requirements.size,
offset: made.report.offset,
subresource: made.report.size,
row_pitch: made.report.row_pitch,
array_pitch: made.report.array_pitch,
};
let lies = aliases(&texels.layout, memory_offset, room, &image, unobserved);
let typed = made.requirements.memory_type_bits & (1 << type_index) != 0;
(!lies || !typed).then(|| {
format!(
"it lays the texels out otherwise than Metal at their offset and row pitch, \
holds other bytes than them or takes no memory of the buffer's type (rows \
{} bytes apart, a subresource of {} bytes, aligned to {}, of the buffer's \
type {typed})",
made.report.row_pitch, made.report.size, made.requirements.alignment
)
})
};
let release_unbound = |made: LinearMade| {
// SAFETY: the image is live and unbound, and no command uses it.
unsafe { release::object(raw_device, made.raw) };
};
let linear = if in_place::checking() {
Err("the device's check of its pooled memory makes no linear image".to_owned())
} else if let Some(LinearLack { format, features }) = linear_lack(plan, facts) {
Err(LinearRefusal::Need(LinearNeed::Features { format, features }).describe(plan))
} else {
self.linear_image(plan)?
.map_err(|refusal| refusal.describe(plan))
};
let linear = match linear {
Ok(made) => match elsewhere(&made) {
None => return Ok(Ok(made)),
Some(why) => {
release_unbound(made);
format!("the device's linear image of the texture: {why}")
}
},
Err(why) => format!("the device makes no linear image of the texture: {why}"),
};
Ok(
match self.pooled_pitched_image(plan, texels, usage, facts)? {
Ok(made) => match elsewhere(&made) {
None => Ok(made),
Some(why) => {
release_unbound(made);
let why =
format!("its DRM_FORMAT_MOD_LINEAR image at Metal's pitch: {why}");
Err((linear, ShadowCause::Refused(Refusal::Pitch, why)))
}
},
Err(cause) => Err((linear, cause)),
},
)
}
/// Whether [`alias_image`](Self::alias_image) makes an image over `texels` (of a texture of
/// Metal `usage` and `plan`), by its steps up to the bind ([`Device::alias_unbound`]), the
/// image released unbound; else the cause of the shadow the texture is.
pub(crate) fn alias_image_made(
&self,
plan: &ImagePlan,
texels: &Texels,
usage: TextureUsage,
facts: &ImageFacts<'_>,
) -> Result<Result<(), ShadowCause>, Error> {
Ok(match self.alias_unbound(plan, texels, usage, facts)? {
Ok(made) => {
// SAFETY: the image is live and unbound, and no command uses it.
unsafe { release::object(self.shared().vk(), made.raw) };
Ok(())
}
Err((_, cause)) => Err(cause),
})
}
/// A `DRM_FORMAT_MOD_LINEAR` image of `plan` over `texels` in their buffer's pooled memory, its
/// one plane at Metal's pitch (`VkImageDrmFormatModifierExplicitCreateInfoEXT`), no external
/// memory (`dma_buf::ModifierMemory::Own`), unbound and made `UNDEFINED`
/// (`Device::pitched_image`), for a texture of Metal `usage` whose texels the device's own
/// linear image lays out otherwise: where the device makes such images, and its policy and
/// its check of the pooled class ([`PagesClass::Pooled`]) take each access the usage takes
/// for texels that lie in its lines as these do (`in_place::in_place`; a pending verdict
/// starts the check, and the texture is a shadow meanwhile); the check's own textures whatever
/// the verdicts. Why not.
///
/// Vulkan lets such an image be bound to memory of no external handle type: an image of a DRM
/// format modifier's tiling names its modifier and plane layout
/// (`VUID-VkImageCreateInfo-tiling-02261`), and of its memory only a dedicated allocation is
/// asked where the driver requires one (`VUID-vkBindImageMemory-image-01445`, which the
/// runtime refuses); and a `DRM_FORMAT_MOD_LINEAR` image is linear (the Glossary's Linear
/// Resource), so in `GENERAL` its texels and the buffer's bytes alias as two host-accessible
/// subresources do (Memory Aliasing). But it is made `UNDEFINED`, `PREINITIALIZED` being for
/// linear tiling alone (`VUID-VkImageCreateInfo-initialLayout-12478`), and of its first move
/// the Synchronization chapter says "If the old layout is VK_IMAGE_LAYOUT_UNDEFINED, the
/// contents of that range may be discarded": the buffer's bytes stay only where the driver
/// leaves them, which the device's check of the pooled class measures, as it measures the
/// moves of images over host pages (`in_place.rs`, "Relying on the driver, checked").
fn pooled_pitched_image(
&self,
plan: &ImagePlan,
texels: &Texels,
usage: TextureUsage,
facts: &ImageFacts<'_>,
) -> Result<Result<LinearMade, ShadowCause>, Error> {
if !self.makes_modifier_images() {
return Ok(Err(ShadowCause::Refused(
Refusal::Pitch,
"the device enables no VK_EXT_image_drm_format_modifier for an image at Metal's \
row pitch"
.to_owned(),
)));
}
if !in_place::checking() {
let line = self.shared().in_place.line();
let pages = (
PagesClass::Pooled,
texels.rows(line),
texels.texel_bytes(),
texels.padding(line),
);
let policy = self.compile_device().policy().host_page_textures;
let verdict = |class, access, rows| self.in_place_verdict(class, access, rows);
if let Err(cause) = in_place::in_place(policy, usage, pages, verdict) {
return Ok(Err(cause));
}
}
let own = crate::dma_buf::ModifierMemory::Own;
let made = self.pitched_image(plan, &texels.layout, facts, own)?;
Ok(made.map_err(|(refusal, why)| ShadowCause::Refused(refusal, why)))
}
/// The linear image of `plan` aliasing `texels` in their buffer's memory (the runtime's own),
/// in the `GENERAL` layout and holding the buffer's bytes, where the device makes one that
/// lies exactly on Metal's layout there (`linear_plan::linear_lack` and `aliases`); why not
@@ -2788,7 +2954,7 @@ impl Device {
&self,
plan: ImagePlan,
descriptor: &TextureDescriptor,
(texels, holds): (&Texels, &Result<(), String>),
(texels, holds, usage): (&Texels, &Result<(), String>, TextureUsage),
facts: &ImageFacts<'_>,
) -> Result<Aliased, Error> {
match texels.buffer.caller_memory() {
@@ -2807,58 +2973,18 @@ impl Device {
if let Err(why) = holds {
return Ok(Err(why.clone()));
}
let (memory, buffer_offset, type_index) = texels.buffer.memory();
let (memory, buffer_offset, _) = texels.buffer.memory();
let memory_offset = buffer_offset + texels.layout.offset;
let made = if let Some(LinearLack { format, features }) = linear_lack(&plan, facts) {
Err(LinearRefusal::Need(LinearNeed::Features { format, features }).describe(&plan))
} else {
self.linear_image(&plan)?
.map_err(|refusal| refusal.describe(&plan))
};
let LinearMade {
raw,
requirements,
report,
initial,
} = match made {
Ok(made) => made,
Err(why) => {
return Ok(Err(format!(
"the device makes no linear image of the texture: {why}"
)));
}
};
let raw_device = self.shared().vk();
let room = texels.buffer.padded_length() - texels.layout.offset;
let image = LinearImage {
alignment: requirements.alignment,
size: requirements.size,
offset: report.offset,
subresource: report.size,
row_pitch: report.row_pitch,
array_pitch: report.array_pitch,
let made = match self.alias_unbound(&plan, texels, usage, facts)? {
Ok(made) => made,
Err((linear, cause)) => return Ok(Err(format!("{linear}; and {}", cause.words()))),
};
let unobserved = texels.buffer.padding_unobserved();
let why = if !aliases(&texels.layout, memory_offset, room, &image, unobserved) {
Some(format!(
"the device's linear image of the texture lays its texels out otherwise than \
Metal at the texels' offset and row pitch, or holds other bytes than its texels \
(rows {} bytes apart, a subresource of {} bytes, aligned to {})",
report.row_pitch, report.size, requirements.alignment
))
} else if requirements.memory_type_bits & (1 << type_index) == 0 {
Some(
"the device's linear image of the texture takes no memory of the buffer's type"
.to_owned(),
)
} else {
None
};
if let Some(why) = why {
// SAFETY: the image is live and unbound, and no command uses it.
unsafe { release::object(raw_device, raw) };
return Ok(Err(why));
let checking = in_place::checking();
if made.initial == vk::ImageLayout::UNDEFINED && !checking {
self.shared().counts.pooled_pitched_image();
}
let LinearMade { raw, initial, .. } = made;
// SAFETY: the image is live and unbound; the memory is the buffer's, which outlives the
// texture holding it, in a type the image takes, at an offset `aliases` checked against
// the image's alignment and size.
@@ -2900,10 +3026,12 @@ impl Device {
staging_copies: false,
objects: ObjectsToken::default(),
};
// Leaving `PREINITIALIZED` keeps the bytes the image holds: the buffer's.
// Leaving `PREINITIALIZED` keeps the bytes the image holds: the buffer's; leaving
// `UNDEFINED`, as a `DRM_FORMAT_MOD_LINEAR` image does, keeps them where the device's
// check of its pooled memory showed it (`Device::pooled_pitched_image`).
let first = FirstMove::new(raw, image.plan().format, initial, None);
*lock(&image.first_move) = Some(first);
if unobserved && texels.layout.padded() {
if texels.buffer.padding_unobserved() && texels.layout.padded() && !checking {
self.shared().counts.unobserved_padding_alias();
}
Ok(Ok(image.shared()))
@@ -783,7 +783,8 @@ impl Device {
}
Err(refusal) => (refusal.describe(plan), false),
};
Ok(match self.host_pitched_image(plan, layout, facts)? {
let host = crate::dma_buf::ModifierMemory::Host;
Ok(match self.pitched_image(plan, layout, facts, host)? {
Ok(made) => Ok((made, true)),
// A linear image the driver lays out otherwise is refused for its pitch; else the
// pitched image's refusal names why.
@@ -843,26 +844,27 @@ impl Device {
}))
}
/// A `DRM_FORMAT_MOD_LINEAR` image of `plan` made for host allocations, unbound, whose one
/// plane lies at offset 0 with rows `layout.row_pitch` apart (and slices `layout.slice_pitch`,
/// for a plan of several layers), its requirements and its plane's layout as the driver reports
/// them, made `UNDEFINED`. Why not where the device does not enable the extension, makes no
/// such image for host allocations (`Device::modifier_image_checks`), refuses the layout or
/// lays it out otherwise, or asks a dedicated allocation of it: the runtime asks the driver and
/// takes its answer, never a rule of its own.
/// A `DRM_FORMAT_MOD_LINEAR` image of `plan` made for `memory` (host allocations, or the
/// runtime's own memory), unbound, whose one plane lies at offset 0 with rows
/// `layout.row_pitch` apart (and slices `layout.slice_pitch`, for a plan of several layers),
/// its requirements and its plane's layout as the driver reports them, made `UNDEFINED`
/// (`VUID-VkImageCreateInfo-initialLayout-12478`: `PREINITIALIZED` is for linear tiling alone).
/// Why not where the device does not enable the extension, makes no such image for `memory`
/// (`Device::modifier_image_checks`), refuses the layout or lays it out otherwise, or asks a
/// dedicated allocation of it: the runtime asks the driver and takes its answer, never a rule
/// of its own.
#[cfg(unix)]
fn host_pitched_image(
pub(super) fn pitched_image(
&self,
plan: &ImagePlan,
layout: &BufferLayout,
facts: &ImageFacts<'_>,
memory: crate::dma_buf::ModifierMemory,
) -> Result<Result<LinearMade, (Refusal, String)>, Error> {
use crate::dma_buf::{memory_plane_0, ModifierMemory, DRM_FORMAT_MOD_LINEAR};
use crate::dma_buf::{memory_plane_0, DRM_FORMAT_MOD_LINEAR};
let (row_pitch, slice_pitch) = (layout.row_pitch, layout.slice_pitch);
let raw_device = self.shared().vk();
let enabled = (self.shared().host_import.as_ref())
.is_ok_and(|importer| importer.makes_dma_buf_images());
if !enabled {
if !self.makes_modifier_images() {
return Ok(Err((
Refusal::Pitch,
"the device enables no VK_EXT_image_drm_format_modifier for an image at Metal's \
@@ -870,8 +872,7 @@ impl Device {
.to_owned(),
)));
}
let host = ModifierMemory::Host;
let view_formats = match self.modifier_image_checks(plan, facts, host)? {
let view_formats = match self.modifier_image_checks(plan, facts, memory)? {
Ok(view_formats) => view_formats,
Err(why) => return Ok(Err((Refusal::Image, why))),
};
@@ -889,14 +890,15 @@ impl Device {
let mut explicit = vk::ImageDrmFormatModifierExplicitCreateInfoEXT::default()
.drm_format_modifier(DRM_FORMAT_MOD_LINEAR)
.plane_layouts(&planes);
let raw = match self.drm_linear_image(plan, &view_formats, &mut explicit, host)? {
let raw = match self.drm_linear_image(plan, &view_formats, &mut explicit, memory)? {
Ok(raw) => raw,
Err(error) => {
return Ok(Err((
Refusal::Pitch,
format!(
"the driver refuses a DRM_FORMAT_MOD_LINEAR image for host allocations \
with rows {row_pitch} bytes apart ({error:?})"
"the driver refuses a DRM_FORMAT_MOD_LINEAR image for {} with rows \
{row_pitch} bytes apart ({error:?})",
memory.words()
),
)));
}
@@ -911,8 +913,9 @@ impl Device {
Refusal::Image,
format!(
"it asks a dedicated allocation of a DRM_FORMAT_MOD_LINEAR image of {:?} for \
host allocations, which one bound at an offset of the buffer's import is not",
plan.format
{}, which one bound at an offset of the buffer's memory is not",
plan.format,
memory.words()
),
))
} else if report.offset != 0
@@ -922,10 +925,13 @@ impl Device {
Some((
Refusal::Pitch,
format!(
"the driver lays a DRM_FORMAT_MOD_LINEAR plane for host allocations out at \
offset {}, rows {} and slices {} bytes apart, where the texels lie at offset \
0, rows {row_pitch} and slices {array_pitch} apart",
report.offset, report.row_pitch, report.array_pitch
"the driver lays a DRM_FORMAT_MOD_LINEAR plane for {} out at offset {}, rows \
{} and slices {} bytes apart, where the texels lie at offset 0, rows \
{row_pitch} and slices {array_pitch} apart",
memory.words(),
report.offset,
report.row_pitch,
report.array_pitch
),
))
} else {
@@ -946,11 +952,12 @@ impl Device {
/// No image is laid out by DRM format modifier on this system.
#[cfg(not(unix))]
fn host_pitched_image(
pub(super) fn pitched_image(
&self,
_plan: &ImagePlan,
_layout: &BufferLayout,
_facts: &ImageFacts<'_>,
_memory: crate::dma_buf::ModifierMemory,
) -> Result<Result<LinearMade, (Refusal, String)>, Error> {
Ok(Err((
Refusal::Pitch,
@@ -113,6 +113,15 @@ impl Device {
way,
}
}
// Over the device's own memory, whether the making's image aliases the bytes: the
// device's linear image, or one at Metal's pitch where its check passed for its pooled
// memory (`Device::alias_image_made`); else the cause of the shadow it is.
Route::Device { holds: Ok(()) } if texels.buffer.caller_memory().is_none() => {
if let Err(cause) = self.alias_image_made(&plan, texels, usage, &facts)? {
return Ok(cause.placement());
}
Route::Device { holds: Ok(()) }
}
route => route,
};
Ok(route.placement())
@@ -242,7 +242,14 @@ impl Route {
HostWay::Shadow(cause) => cause,
},
};
let reason = match *cause {
cause.placement()
}
}
impl ShadowCause {
/// Where a texture shadowed for this cause lies, as the query answers it.
pub(crate) fn placement(&self) -> TexturePlacement {
let reason = match *self {
ShadowCause::CheckPending(..) => return TexturePlacement::Pending,
ShadowCause::Exact => TextureShadowReason::Policy,
ShadowCause::StoreWidth(texel_bytes) => TextureShadowReason::StoreWidth { texel_bytes },
@@ -481,6 +481,7 @@ pub(crate) struct Counters {
stand_ins_after_pins: AtomicU64,
feedback_loop_alias_images: AtomicU64,
unobserved_padding_aliases: AtomicU64,
pooled_pitched_images: AtomicU64,
feedback_loop_shadow_images: AtomicU64,
depth_attachments_moved: AtomicU64,
depth_instances_held: AtomicU64,
@@ -3506,6 +3507,13 @@ impl Counters {
.fetch_add(1, Ordering::Relaxed);
}
/// The device made a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch aliasing a buffer's bytes
/// in its pooled memory, where its own linear image lays them out otherwise
/// (`Device::pooled_pitched_image`).
pub(crate) fn pooled_pitched_image(&self) {
self.pooled_pitched_images.fetch_add(1, Ordering::Relaxed);
}
/// The device made the image of a texture over a buffer's bytes that the encoders copy them
/// into and back (`shadow.rs`) with feedback loop usage (`texture::feedback_candidate`).
pub(crate) fn feedback_loop_shadow_image(&self) {
@@ -6656,6 +6664,13 @@ pub struct WorkCounts {
/// declaration the texture is a shadow (`shadowed_textures`). One per image made. A runtime
/// extension: Metal has none.
pub unobserved_padding_aliases: u64,
/// Images aliasing a buffer of the device's own memory that are `DRM_FORMAT_MOD_LINEAR` images
/// at Metal's row pitch (`VK_EXT_image_drm_format_modifier`), made where the device's own
/// linear image of the texture lays its rows out otherwise and the device's check of such
/// images over its pooled memory passed for the texture's accesses (docs/API.md, textures over
/// host pages); without one the texture is a shadow (`shadowed_textures`). One per image made.
/// A runtime extension: Metal has none.
pub pooled_pitched_images: u64,
/// Images of textures over a buffer's bytes that the encoders copy them into and back (the
/// texture's shadow, `shadow.rs`: a texture over host pages that renders, among others) that
/// the device made with feedback loop usage where it cost them nothing
@@ -7562,6 +7577,7 @@ impl Device {
stand_ins_after_pins: counters.stand_ins_after_pins.load(Ordering::Relaxed),
feedback_loop_alias_images: counters.feedback_loop_alias_images.load(Ordering::Relaxed),
unobserved_padding_aliases: counters.unobserved_padding_aliases.load(Ordering::Relaxed),
pooled_pitched_images: counters.pooled_pitched_images.load(Ordering::Relaxed),
feedback_loop_shadow_images: counters
.feedback_loop_shadow_images
.load(Ordering::Relaxed),