Files
reims-vgpu/conformance/suite/cases/SelfCopyOverlap.swift
T
Anees IqbalandClaude Opus 5 596e37c7e3 conformance: an overlapping self-copy is not undefined, and this device drops it
Component: conformance/, test-only. Native oracle: Apple M4, macOS 15.6.1.
Guest: macos-13 rail, x86 macOS / Linux Vulkan pathway.

A driven Maps interaction on this rail issues `copyFromTexture:toTexture:`
naming one texture twice at different origins, several times a boot -- a
horizontal shift inside one row and a one-texel column shifted a dozen rows
down. This device refuses those as `t2t_overlap` and returns an error to a blit
encoder that, on real hardware, does not fail. The copy is dropped and the guest
treats it as complete.

The refusal's own comment called the shape "undefined in Metal", which is the
term this case exists to settle. It is not undefined on the oracle. Measured
there in three identical runs, over both live shapes and on both a buffer-backed
and an ordinary image, `copyFromTexture:toTexture:` with intersecting rectangles
produces source-snapshot semantics: the whole source region is read before any
destination byte is written. Row-at-a-time execution gives a different answer for
both shapes, and a disjoint copy -- where the two readings agree -- separates a
device that mishandles the overlap from one that refuses every self-copy.

Three cases, all `PASS` on the oracle and recorded in the tracked native
baseline:

- `self_copy_overlap_row_shift_14` -- 20 texels of one row copied 14 texels
  right, so the destination overwrites source texels the same row still reads.
- `self_copy_overlap_column_shift_12` -- a one-texel column, 17 rows tall,
  shifted 12 rows down, so a top-down loop overwrites rows 12 through 16 before
  reading them.
- `self_copy_disjoint_control` -- the same call shape, rectangles disjoint.

Raw guest result on the identified control (HEAD, no product change), reproduced
identically in two complete batteries: the control case **passes**, and both
overlap cases **fail** with `still_pre_copy` equal to the full copy extent --
20/20 texels and 17/17 texels still holding exactly what they held before the
copy. That is the signature of a dropped copy rather than a mis-ordered one, and
it is the refusal doing it.

Owner: `exec_copy_texture_to_texture` in `runtime/blit_exec`, which decides the
overlap. Not shader translation and not the guest driver, so neither rail
inventory takes these; they are this device's own defect and the next commit
repairs them.

Counts: 185 PASS / 79 FAIL / 29 SKIP on the guest, against the parent's
184/77/29 -- the disjoint control is a new pass and the two overlap cases are the
two new failures. Every other case is identical to the parent's envelope in both
runs. Native: 293 cases, 0 failures, 0 skipped.

An earlier draft of these cases declared the image over an `MTLBuffer`, and all
three failed on the guest with the readback dispatch refused
(`compute_buffer_texture_unsupported`) rather than on the copy. That would have
made the case a witness for a different gap, so the image is an ordinary one and
the control's pass is what proves the distinction holds. The buffer-backed
readback gap is real, unrelated, and not covered here.

Not verified: one rail and one pathway. The oracle result is Apple silicon only;
no Intel Mac was measured, and nothing here claims the semantics are architecture
independent. No product behavior changes in this commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-30 04:08:13 +03:00

135 lines
6.1 KiB
Swift

import Metal
import Foundation
// ---------------------------------------------------------------------------
// One texture copied onto itself, with the source and destination rectangles
// overlapping.
//
// `copyFromTexture:toTexture:` naming one texture twice at different origins is
// a shape the guest issues: a driven Maps interaction on the macos-13 rail
// produces it several times a boot, both as a horizontal shift inside one row
// and as a one-texel column shifted down a dozen rows. Nothing in the encoder
// refuses it, so the guest treats the copy as complete.
//
// What the overlapping texels are afterwards is the question, and it has one
// answer rather than being undefined: measured on Apple silicon, the whole
// source region is read before any destination byte is written. A row-at-a-time
// implementation gets a different answer whenever a destination row precedes a
// source row it still has to read, which is exactly what both live shapes do.
//
// So the expectation below is computed against a pristine snapshot of the
// source. `disjoint` is the control: there the two readings agree, so a device
// that refuses every self-copy fails it too and a device that only mishandles
// the overlap does not — which is what tells the two apart.
// ---------------------------------------------------------------------------
private func texelPattern(_ x: Int, _ y: Int) -> UInt32 {
// Distinct per texel within the sizes used here, so a copy that lands the
// wrong source texel is visible rather than aliasing onto the right value.
pack(UInt8(x & 0xFF), UInt8(y & 0xFF), UInt8((x &* 7 &+ y &* 31) & 0xFF), 255)
}
private func selfCopyCase(_ label: String,
width: Int, height: Int,
src: MTLOrigin, dst: MTLOrigin, region: MTLSize) {
let bpr = width * 4
let d = MTLTextureDescriptor.texture2DDescriptor(
pixelFormat: .rgba8Unorm, width: width, height: height, mipmapped: false)
d.storageMode = .shared
d.usage = [.shaderRead, .shaderWrite]
guard let tex = dev.makeTexture(descriptor: d) else {
report(label, false, "makeTexture nil for the shared \(width)x\(height) image")
return
}
// An ordinary image, not one declared over a buffer: the copy below is the
// only thing this case asks the device to do with it, and the readback is
// the suite's usual one rather than a buffer-backed bind.
var seed = [UInt32](repeating: 0, count: width * height)
for y in 0..<height {
for x in 0..<width { seed[y * width + x] = texelPattern(x, y) }
}
seed.withUnsafeBytes { raw in
tex.replace(region: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0,
withBytes: raw.baseAddress!, bytesPerRow: bpr)
}
// Source-snapshot semantics: every destination texel takes the source as it
// stood before the copy, never a byte this copy has already written.
var want = [UInt32](repeating: 0, count: width * height)
for y in 0..<height {
for x in 0..<width { want[y * width + x] = texelPattern(x, y) }
}
for dy in 0..<region.height {
for dx in 0..<region.width {
want[(dst.y + dy) * width + dst.x + dx] = texelPattern(src.x + dx, src.y + dy)
}
}
guard let cb = queue.makeCommandBuffer(), let blit = cb.makeBlitCommandEncoder() else {
report(label, false, "no blit encoder"); return
}
blit.copy(from: tex, sourceSlice: 0, sourceLevel: 0, sourceOrigin: src, sourceSize: region,
to: tex, destinationSlice: 0, destinationLevel: 0, destinationOrigin: dst)
blit.endEncoding()
cb.commit()
cb.waitUntilCompleted()
if let e = cb.error {
report(label, false, "the blit encoder refused a copy the guest treats as "
+ "complete: \(e.localizedDescription)")
return
}
guard let got = readBack(readPipe, tex, width, height) else { refused(label); return }
var bad: [(Int, Int)] = []
var first = ""
var unmoved = 0
for y in 0..<height {
for x in 0..<width where got[y * width + x] != want[y * width + x] {
bad.append((x, y))
// The destination still holding what it held before the copy is the
// signature of a dropped copy, as against a mis-ordered one.
if got[y * width + x] == texelPattern(x, y) { unmoved += 1 }
if first.isEmpty {
first = "first=(\(x),\(y)) want=\(hex(want[y * width + x])) "
+ "got=\(hex(got[y * width + x]))"
}
}
}
report(label, bad.isEmpty,
bad.isEmpty
? "the overlapping copy read its source before writing its destination"
: "wrong=\(bad.count)/\(width * height) still_pre_copy=\(unmoved) \(first)"
+ badMap(bad, width, height))
}
/// A row copied onto itself 14 texels to the right — the destination overwrites
/// source texels the same row still has to read.
func selfCopyOverlapRowShiftCase() {
selfCopyCase("self_copy_overlap_row_shift_14",
width: 128, height: 32,
src: MTLOrigin(x: 0, y: 0, z: 0),
dst: MTLOrigin(x: 14, y: 0, z: 0),
region: MTLSize(width: 20, height: 1, depth: 1))
}
/// A one-texel column shifted twelve rows down — a top-down row loop overwrites
/// rows 12 through 16 before it reads them.
func selfCopyOverlapColumnShiftCase() {
selfCopyCase("self_copy_overlap_column_shift_12",
width: 256, height: 32,
src: MTLOrigin(x: 0, y: 0, z: 0),
dst: MTLOrigin(x: 0, y: 12, z: 0),
region: MTLSize(width: 1, height: 17, depth: 1))
}
/// The control: the same call shape with rectangles that do not intersect, where
/// both readings agree. A device that refuses every self-copy fails this too.
func selfCopyDisjointCase() {
selfCopyCase("self_copy_disjoint_control",
width: 128, height: 32,
src: MTLOrigin(x: 0, y: 0, z: 0),
dst: MTLOrigin(x: 64, y: 0, z: 0),
region: MTLSize(width: 20, height: 1, depth: 1))
}