Files
metal2vulkan/crates/metal2vulkan-runtime/src/resource.rs
T
Anees Iqbal 54a8b32154 Bind a memoryless texture as plain Metal does, and fail what uses it
One work packet, landed as 2 commits (6c0070d57d..a759d0e88f on archive/development-linear-2026-10-28), in order:

* Bind a memoryless texture as plain Metal does, and fail what uses it

A memoryless texture bound at a texture slot was refused at the setter (`StorageMode`), as
Metal's API validation layer refuses it. Plain Metal takes the binding, so a program that binds
one died here at `setFragmentTextures:withRange:` where it runs on Metal. Seen on an RX 580
under RADV, where the device is told framebuffer reads are unsupported.

Measured on an M3 Max under macOS 27.0.1, plain Metal (probes committed under
crates/metal2vulkan-conformance/probes/memoryless_*.m):
- Every setter returns. A command that uses the texture fails the command buffer with
  MTLCommandBufferErrorInvalidResource (9). That covers a draw, a dispatch or a tile dispatch
  whose function statically uses the slot, an empty one included, and one that reads the slot
  only behind a branch that is never taken. It also covers useResource of the texture, with or
  without a later command, and a blit into or out of it (a zero-size copy included), and
  generateMipmaps of more than one level.
- Nothing fails for a slot no function uses, a slot rebound before the draw, a pass with no
  draw, a memory barrier, the optimize hints, synchronizeResource, a copy of no slices, or the
  mipmaps of one level.
- Granularity: the submission that holds the command runs nothing, as Metal splits the command
  buffer (a render pass alone, a run of blit or compute encoders with no event command between
  them). Neither does anything after it, and its signals do not fire. The submissions before it
  run and their signals fire. The queue runs on.
- The CPU calls of a memoryless 2D texture move nothing; a memoryless texture buffer's fault
  the process. Every view of one is nil.

The runtime now:
- binds it;
- fails the command buffer with the new CommandBufferError::InvalidResource from the first
  encoder of the Metal submission (State::fail_submission_from, checks::submission_start);
- records nothing for the failing command;
- moves nothing in the CPU calls, still refusing the texture buffer's;
- answers nil (`Nil`, `StorageMode`) for views.

The check per command is one emptiness test of a per-table set of the slots that hold a
memoryless texture.

An execution's command whose function uses such a slot is the runtime's gap: Metal's answer
there is not measured. That is IndirectRenderCommands for render, and CommandReplay for an
inherited compute table.

RUNTIME_VERSION 1646. The runtime-sources pin moves with it. docs/REFUSALS.md is regenerated,
api_signatures is blessed for the new variant, and the degrade catalogue's
memoryless-texture-buffer entry is updated.

Verified on MoltenVK over this M3 Max under the validation layer:
- runtime_lane::memoryless_uses: draw shapes (read, empty, branch never taken, earlier draw,
  use_resource, unused, rebound, rebound to nil, no draw); the probe's encoder sequences (CcC,
  BbB, CbC, BcB, BSbB, bSB, CSc); compute and blit uses; CPU access and views.
- runtime_lane::memoryless, and the runtime's unit suite (plain cfg).
- tests::command_buffer::a_memoryless_use_fails_from_its_submission_s_first_encoder.
- Negative controls:
  - failing per encoder instead of per submission fails the sequence and blit/compute tests;
  - dropping the draw check fails the draw test (the draw is refused).
- Clippy in both cfgs, rustdoc -D warnings for the runtime, and the pin guard.

Not run: lavapipe, RADV, and the full validation and conformance suites.

* Re-pin the runtime sources at the landing tip

The landed packet changed a runtime-sources input, so the committed digest no

longer described the sources and `the_runtime_sources_are_the_committed_ones`

would fail. Recomputed over the same inputs and committed by land-pkt.sh.
2026-10-28 17:24:19 +02:00

156 lines
5.4 KiB
Rust

//! The identity every resource carries: a key no other resource of the process ever has, and
//! whether Metal tracks the resource's hazards. Hazard tracking keys on it; each resource kind
//! answers it from its own file (`Buffer::identity` in buffer.rs).
use super::values::{HazardTrackingMode, StorageMode};
use super::{Buffer, Device, IndirectCommandBuffer, Texture};
use std::sync::atomic::{AtomicU64, Ordering};
/// A resource's key: unique in the process, never reused.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(super) struct ResourceKey(u64);
impl ResourceKey {
/// A key no resource had before.
pub(super) fn new() -> ResourceKey {
static NEXT: AtomicU64 = AtomicU64::new(1);
ResourceKey(NEXT.fetch_add(1, Ordering::Relaxed))
}
}
/// A buffer's, texture's or heap's identity (`Buffer::object_id`, `Texture::object_id`,
/// `Heap::object_id`): a runtime extension, no Metal type. Metal names an object by its pointer
/// (`id`), which another object may take once it is freed, and by nothing else where it has no
/// address (a sparse heap's `gpuAddress` is 0). This number is unique among the process's
/// buffers, textures and heaps, is the object's for its whole life (a clone is the same object),
/// and is never given to another, so a program can key what it knows of an object by it without
/// holding the object.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ObjectId(u64);
impl ObjectId {
/// An identity no object had before.
pub(super) fn new() -> ObjectId {
static NEXT: AtomicU64 = AtomicU64::new(1);
ObjectId(NEXT.fetch_add(1, Ordering::Relaxed))
}
/// The number, never 0.
pub fn get(self) -> u64 {
self.0
}
}
/// What hazard tracking knows of a resource.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(super) struct Identity {
pub(super) key: ResourceKey,
/// Metal orders the resource's accesses between encoders and between dispatches of a serial
/// encoder; an untracked resource is ordered by the program.
pub(super) tracked: bool,
}
impl Identity {
/// A new resource's identity for its hazard tracking `mode`: `Default` is tracked for a
/// resource made outside a heap and untracked for one made in a heap, as in Metal.
pub(super) fn new(mode: HazardTrackingMode, in_heap: bool) -> Identity {
let tracked = match mode {
HazardTrackingMode::Tracked => true,
HazardTrackingMode::Untracked => false,
HazardTrackingMode::Default => !in_heap,
};
Identity {
key: ResourceKey::new(),
tracked,
}
}
/// The resolved hazard tracking mode: `Tracked` or `Untracked`.
pub(super) fn mode(self) -> HazardTrackingMode {
if self.tracked {
HazardTrackingMode::Tracked
} else {
HazardTrackingMode::Untracked
}
}
}
/// Any Metal resource, for the calls that take a list of them (`memoryBarrierWithResources:`).
/// Each resource kind adds its variant and its arm in the helpers below.
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub enum ResourceRef<'a> {
/// A buffer.
Buffer(&'a Buffer),
/// A texture, or a texture view (its texture's resource).
Texture(&'a Texture),
/// An indirect command buffer: a function encodes commands into it once the encoder made it
/// resident for writing.
IndirectCommandBuffer(&'a IndirectCommandBuffer),
}
impl<'a> From<&'a Buffer> for ResourceRef<'a> {
fn from(buffer: &'a Buffer) -> ResourceRef<'a> {
ResourceRef::Buffer(buffer)
}
}
impl<'a> From<&'a Texture> for ResourceRef<'a> {
fn from(texture: &'a Texture) -> ResourceRef<'a> {
ResourceRef::Texture(texture)
}
}
impl<'a> From<&'a IndirectCommandBuffer> for ResourceRef<'a> {
fn from(icb: &'a IndirectCommandBuffer) -> ResourceRef<'a> {
ResourceRef::IndirectCommandBuffer(icb)
}
}
impl Buffer {
/// The buffer as a resource of a resource list.
pub fn as_resource(&self) -> ResourceRef<'_> {
ResourceRef::Buffer(self)
}
}
impl Texture {
/// The texture as a resource of a resource list.
pub fn as_resource(&self) -> ResourceRef<'_> {
ResourceRef::Texture(self)
}
}
impl IndirectCommandBuffer {
/// The indirect command buffer as a resource of a resource list.
pub fn as_resource(&self) -> ResourceRef<'_> {
ResourceRef::IndirectCommandBuffer(self)
}
}
impl ResourceRef<'_> {
/// The device that made the resource.
pub(super) fn device(&self) -> &Device {
match self {
ResourceRef::Buffer(buffer) => buffer.device(),
ResourceRef::Texture(texture) => texture.device(),
ResourceRef::IndirectCommandBuffer(icb) => icb.device(),
}
}
/// The resource's storage mode.
pub(super) fn storage_mode(&self) -> StorageMode {
match self {
ResourceRef::Buffer(buffer) => buffer.storage_mode(),
ResourceRef::Texture(texture) => texture.storage_mode(),
ResourceRef::IndirectCommandBuffer(icb) => icb.storage_mode(),
}
}
/// Whether the resource is memoryless: a memoryless texture or texture buffer, whose use
/// fails Metal's command buffer (`CommandBufferError::InvalidResource`).
pub(super) fn is_memoryless(&self) -> bool {
self.storage_mode() == StorageMode::Memoryless
}
}