Files
metal2vulkan/crates/metal2vulkan-runtime/src/mipmap_pick.rs
T
Anees Iqbal 395e5c3305 Runtime: a tiled texture's take makes every format's later levels, and its first level is the bytes to the CPU
A tiled texture of more than one level (Buffer::new_tiled_texture) is a copy of the buffer's
bytes taken at use. Two leftovers from that change against what two x86 Macs answer:

1. Its take made the later levels only by the device's blit chain. A format generate_mipmaps
   makes by the compute filter (an extended-range one, an 8-bit sRGB one held as its UNORM twin,
   one without blits or linear filtering) or by the integer picks kept zeros past its first level,
   where Metal makes every level. Now the making keeps generate_mipmaps' own plan of the format
   (shadow::LaterLevels: the blit chain, the compute filter or the picks, announced as that call
   announces them; Kept for a format it leaves or refuses, or a scratch past the device's
   limits). The encoder makes a take's filter or picks run as it plans the copy
   (EncoderCore::later_runs, beside sample_moves: the scratch, the kernels and sets), held by the
   command buffer, and the copy in records it after its copy into the first level, behind a
   barrier, as it records the blit chain: transfers of the image and dispatches over the scratch
   alone, outside any render pass, before the first command that uses the texture. A filter take
   counts in WorkCounts::mipmaps_filtered. mipmap_filter.rs and mipmap_pick.rs split their
   recording into FilterRun and PickRun, which generate_mipmaps and the take share; what
   generate_mipmaps records is unchanged. A run that cannot be made (no room for the scratch)
   fails the command buffer, as a heap texture's sample moves do.

2. Its CPU calls reached every level in the image. On the Macs, getBytes and replaceRegion of
   level 0 address the buffer's bytes, and a replaceRegion there makes the next use take the copy
   again, as didModifyData does. Now level 0 reads and writes the bytes (Texture::level_in_image),
   a write there marks the copy stale, and while the bytes are detached level 0 is refused
   (NotImplemented(DetachedTexelAccess)) as a one-level texture's is. Levels 1 and up stay in the
   image (the GPU reads a replace_region there until the next take), where the Macs' GPU never
   sees such a write: an accepted difference in docs/API.md and the rustdoc. Blits are unchanged.

Tests (tests/tiled_texture_device.rs), each failing before its fix: RGBA8Uint's later levels are
plain Metal's picks exactly after a blit first use, and after a pass into level 1 as the first use
(level 1 cleared over the picks, the rest picked); BGR10XR's later levels are within the compute
filter's bound of the level above, the take counted once in mipmaps_filtered; level 0's get_bytes
reads the bytes before the first use, after a GPU write into level 0 (which the GPU reads) and
after a CPU write of them (which no level sees); a level-0 replace_region writes the bytes (the
rows' padding kept) and the next use takes the copy again, every level made from it. The detached
test now holds level 0 refused while detached and reads the new pages' bytes after a rebind.

Docs: new_tiled_texture's, replace_region's, get_bytes's and did_modify_data's rustdoc,
DetachedTexelAccess, WorkCounts::mipmaps_filtered, shadow.rs's Copies taken at use, the filter and
pick modules, docs/API.md (the tiled texture and an accepted difference), docs/RUNTIME-PORTING.md,
docs/FAST-PATHS.md's known gap narrowed to formats generate_mipmaps leaves or refuses, and
docs/REFUSALS.md regenerated (the MipmapGeneration sites moved). RUNTIME_VERSION and the
runtime-sources pin are left to the landing.

Checked here: cargo fmt. Clippy in three configurations, rustdoc and the device tests run as
cluster jobs on this commit.
2026-11-11 08:37:22 -08:00

206 lines
7.9 KiB
Rust

//! `generate_mipmaps` of the integer formats, as plain Metal makes them: each level's texels picked
//! from the level above, which copies give exactly on every device ([`PickPlan`] lays the copies
//! out).
//!
//! **The command.** One blit command, between barriers from the commands before it and to the
//! ones after it ([`BlitCommandEncoder::command`]), over a scratch buffer of the runtime's own:
//! the view's first level, over its layers, copied into the scratch, then every later level copied
//! out of it, a column (or a row's texel) a region. The scratch is retained until the command
//! buffer completes. Where the scratch would pass the device's `max_buffer_length`, the call is
//! refused (`NotImplemented(MipmapGeneration)`): a blit's nearest filter puts its sample on the
//! edge between two texels, which the device's precision decides.
//!
//! **A tiled texture's take** (`Buffer::new_tiled_texture`, `shadow.rs`'s **Copies taken at
//! use**) records the same run ([`PickRun`]) of the texture's levels right after its copy into
//! the first level, inside the copy.
use super::blit::BlitCommandEncoder;
use super::buffer::Buffer;
use super::error::Error;
use super::mipmap_plan::PickPlan;
use super::sparse::emulated::Written;
use super::texture::{texture_aspects, Texture};
use super::tracker::{Access, Barrier};
use super::values::ResourceOptions;
use super::view_plan::ViewRecord;
use super::Device;
use crate::device::functions::Vk;
use ash::vk;
use metal2vulkan::error::{Feature, Unsupported};
use metal2vulkan::metal::TextureType;
impl BlitCommandEncoder {
/// Record `texture`'s mipmaps as `plan`'s picks. The caller checked Metal's rules, and
/// `plan_mipmaps` the image's transfers and texel.
pub(super) fn pick_mipmaps(
&mut self,
texture: &Texture,
plan: &PickPlan,
read: Access,
write: Access,
) -> Result<(), Error> {
let record = texture.record();
let device = self.core().device().clone();
let aspects = texture_aspects(texture.image_plan()?);
let run = PickRun::new(&device, plan, record, aspects)?;
let (base_level, base_layer, layers) =
(record.base_level, record.base_layer, record.layer_count);
let all_layers = base_layer..base_layer + layers;
let uses = [
texture.image_use(base_level..base_level + 1, all_layers.clone(), read, false)?,
texture.image_use(
base_level + 1..base_level + record.level_count,
all_layers.clone(),
write,
true,
)?,
];
let image = texture.raw_image()?;
self.command(&uses, |device, command_buffer| {
// SAFETY: the command buffer is recording, outside a render pass; the run and the
// texture are retained until it completes; the image is in the `GENERAL` layout with
// transfer usages and features (`plan_mipmaps` checked them).
unsafe { run.record(device, command_buffer, image) };
})?;
self.core().retain(run);
self.core().retain(texture.clone());
let first = first_extent(record);
let written = (1..record.level_count)
.map(|n| {
let written = Written {
level: base_level + n,
layers: all_layers.clone(),
origin: [0; 3],
size: first.map(|axis| (axis >> n).max(1)),
};
(aspects, written)
})
.collect();
self.scrub(texture, written)
}
}
/// The first level's extent of the levels `record` presents: its depth 1 but a 3D texture's.
fn first_extent(record: &ViewRecord) -> [u32; 3] {
let mut first = record.extent();
if record.texture_type != TextureType::Type3D {
first[2] = 1;
}
first
}
/// Whether `plan`'s scratch fits `device`'s `max_buffer_length`, which [`PickRun::new`] refuses
/// past.
pub(super) fn picks_fit(device: &Device, plan: &PickPlan) -> bool {
plan.bytes <= device.properties().max_buffer_length
}
/// One recording of a [`PickPlan`] (the module's **The command**): its scratch buffer, the copy of
/// the first level into it and the copies of the later levels' columns out of it. The command
/// buffer that records it holds it until it completes.
pub(super) struct PickRun {
scratch: Buffer,
copy_in: vk::BufferImageCopy,
picks: Vec<vk::BufferImageCopy>,
}
impl PickRun {
/// The run of `plan`'s picks over the levels and layers `record` presents of an image of
/// `aspects` on `device`. Refused where the scratch would pass the device's
/// `max_buffer_length` (`NotImplemented(MipmapGeneration)`, [`picks_fit`]), or as a buffer of
/// the runtime's own is.
pub(super) fn new(
device: &Device,
plan: &PickPlan,
record: &ViewRecord,
aspects: vk::ImageAspectFlags,
) -> Result<PickRun, Error> {
if !picks_fit(device, plan) {
return Err(Error::Unsupported(Unsupported::not_implemented(
Feature::MipmapGeneration,
)));
}
let scratch = device.own_buffer(plan.bytes, ResourceOptions::private())?;
let at = scratch.at(0).1;
let (base_layer, layers) = (record.base_layer, record.layer_count);
let subresource = |level: u32| vk::ImageSubresourceLayers {
aspect_mask: aspects,
mip_level: level,
base_array_layer: base_layer,
layer_count: layers,
};
let extent = |[width, height, depth]: [u32; 3]| vk::Extent3D {
width,
height,
depth,
};
let copy_in = vk::BufferImageCopy {
buffer_offset: at,
buffer_row_length: 0,
buffer_image_height: plan.image_height,
image_subresource: subresource(record.base_level),
image_offset: vk::Offset3D::default(),
image_extent: extent(first_extent(record)),
};
let picks = plan
.picks
.iter()
.map(|pick| vk::BufferImageCopy {
buffer_offset: at + pick.buffer_offset,
buffer_row_length: pick.row_length,
buffer_image_height: pick.image_height,
image_subresource: subresource(pick.level),
image_offset: vk::Offset3D {
x: pick.origin[0] as i32,
y: pick.origin[1] as i32,
z: pick.origin[2] as i32,
},
image_extent: extent(pick.extent),
})
.collect();
Ok(PickRun {
scratch,
copy_in,
picks,
})
}
/// Record the run over `image`.
///
/// # Safety
///
/// `command_buffer` is recording, outside a render pass, after a barrier that orders the run
/// after every earlier write of the first level; the run and the image are live until it
/// completes; the image is in the `GENERAL` layout with transfer usages and features. Every
/// region lies inside the scratch and its level (`plan_picks`), and the picks read what the
/// copy in wrote, past a barrier.
pub(super) unsafe fn record(
&self,
device: &Vk,
command_buffer: vk::CommandBuffer,
image: vk::Image,
) {
let raw = self.scratch.at(0).0;
// SAFETY: the caller's contract.
unsafe {
device.cmd_copy_image_to_buffer(
command_buffer,
image,
vk::ImageLayout::GENERAL,
raw,
&[self.copy_in],
);
Barrier::everything().record(device, command_buffer);
if !self.picks.is_empty() {
device.cmd_copy_buffer_to_image(
command_buffer,
raw,
image,
vk::ImageLayout::GENERAL,
&self.picks,
);
}
}
}
}