vgpu: the compute rail refused a buffer-backed texture the draw rail executes

Component: crates/reims-vgpu, runtime/compute_exec. Pathway x86 macOS / Linux
Vulkan, rail macos-13.

API contract. `newTextureWithBuffer:descriptor:offset:bytesPerRow:` is a
persistent texture view over a type-1 buffer at an explicit byte offset and row
pitch; the texels *are* the buffer's bytes read through the embedded texture
descriptor. This tree already recovered that contract:
`decode/resource::decode_buffer_texture_descriptor` parses the opcode-9 and
opcode-9-wide records, and `runtime::draw::load_buffer_texture_rgba` executes
them for the draw rail.

`compute_exec::stage_texture_raw` refused the identical record outright with
`compute_buffer_texture_unsupported`. Two execution arms consuming one wire
form and disagreeing about it -- the shape AGENTS.md names directly ("Compare
every execution arm consuming the same wire form; do not read one in
isolation").

Behavior changed. A new `stage_buffer_texture` decodes the record through the
same decoder and stages tight raw texels: `bytes_per_row == 0` means tight rows
(the draw twin's reading of the same field), a pitch shorter than tight is
refused, the writeback debt is paid under the texture ref as the draw twin does
because a debt may be armed under either of the two references over the one
allocation, and rows are de-pitched so row padding never reaches the image.

Two deliberate differences from the draw twin, both stated at the site:
`StagedTexture` carries `pixel_format` beside `bytes`, so the native format
survives rather than being narrowed to RGBA8; and the read span is
`bpr*(h-1) + tight` rather than `bpr*h`, so a texture whose last row ends the
allocation is not refused for want of a trailing pitch.

Scope held to the evidence. A *storage* binding of a buffer-backed texture
still refuses, under its own new name
`compute_buffer_texture_storage_unsupported`. Writing back through the buffer
is a separate destination contract, no case in the battery binds one writable,
and inventing a writeback would widen the repair past what it can show.

Owner-level test.
`a_buffer_backed_texture_stages_its_texels_without_the_row_padding` builds a
4x3 BGRA8 texture at pitch = tight+8 with padding 0xEE and asserts tight
output, no padding byte present, exact per-byte position, native format
preserved, and that a storage binding refuses by its own name.

Red witness probed twice by hand and restored by `cp` each time (no git
checkout/restore): reading rows one byte off fails the per-byte assertion;
striding the destination at `tight` instead of `bpr` fails on "row padding must
not reach the image".

Three `BUF_TEX_DESC_*` offset constants and `BUF_TEX_WIDE_DESC_BODY` widened to
`pub(crate)` so a sibling module's test can construct the wire record. No
behavior attaches to that visibility.

Verification actually run. Full serial Vulkan/host-window suite: 2269 lib unit
tests plus every integration binary, exit 0. `cargo fmt --all` then `--check`
clean. All three clippy arms clean at `-D warnings`.

Complete macos-13 conformance battery, import-on, snapshot base, 293 cases
(journal/runs/buftex-cand-1). Scored against control journal/runs/07ec-control-2,
which is byte-identical to controls -3 and -4 at parent fa1f3214:

  control    187 PASS / 77 FAIL / 29 SKIP
  candidate  221 PASS / 55 FAIL / 17 SKIP

  22 FAIL -> PASS
  12 SKIP -> PASS
   0 regressions -- no case moved out of PASS, none missing or duplicated

The 22 repaired failures are the `linear_<fmt>_<w>x<h>_pitch_*` family across
r8/rg8/rgba8/bgra8, the `incremental_*_compute_first_read` family, and
`offset_alias_rgba8Unorm_32x32_x3`. The 12 SKIPs that now run and pass are the
rest of the `incremental_*` lifecycle (`append_visible`, `rewrite_visible`,
`untouched_stable`), which had been skipping because their first read refused.

`compute_buffer_texture_unsupported` appears 0 times in the candidate device
log, against 43 in the control. No `vk_engine_fence_wedged` and no
`vk_engine_fence_timeout`; `stamp_wait_timeout` at 6, inside the control range
of 6-10.

Not verified. One rail, one pathway, import-on only; the
REIMS_VGPU_GUEST_IMPORT=off cell was not run. One candidate boot, not three --
this is a deterministic decode-and-copy path with no timing component, but the
rate is unmeasured. No visual Maps gate was run on this candidate. Nothing here
is evidence about arm64 / Metal, which was cross-compiled and linted only. The
storage-destination half of the contract remains unimplemented and refuses.
This commit is contained in:
Anees Iqbal committed 2026-08-30 05:03:03 +03:00
1 parent 8fa11f267e
commit 6026f037d0
3 files changed
+303 -11

No files matched your search

@@ -1929,6 +1929,138 @@ pub(crate) fn resident_serve(
.then_some(ResidentServe::Sample(key, mirror_generation))
}
/// Stage an opcode-9 buffer-backed texture: tight raw texels read out of the
/// type-1 buffer the guest named, at its declared offset and row pitch.
///
/// The contract is `newTextureWithBuffer:descriptor:offset:bytesPerRow:` — the
/// texels *are* the buffer's bytes, reinterpreted through the embedded texture
/// descriptor. [`crate::runtime::draw`] executes the same record for the draw
/// rail; this is its compute twin and reads the same fields the same way,
/// because one wire form with two disagreeing readers is the defect shape this
/// repository keeps finding. This arm used to refuse the form outright.
///
/// Unlike the draw twin this does **not** convert to RGBA8. [`StagedTexture`]
/// carries `pixel_format` beside `bytes`, so the native texels survive; the
/// draw arm narrows because its consumer takes RGBA8, and reports the loss as
/// `buftex_narrowed`. Here there is no loss to report.
///
/// De-pitching is the whole of the work: the guest's rows are `bytes_per_row`
/// apart and only the leading tight row is texels. The rest is padding the
/// guest may have written anything into, and folding it into the image is
/// exactly the failure the conformance battery fills padding with a distinct
/// pattern to catch.
fn stage_buffer_texture<M: HostMemory + HostOps>(
state: &mut DeviceState,
host: &mut M,
task_id: u32,
texture_ref: u32,
binding: u32,
is_storage: bool,
bt: &crate::runtime::decode::resource::BufferTextureDescriptor,
) -> Result<StagedTexture, ComputeStatus> {
let (width, height) = (bt.desc.width, bt.desc.height);
if width == 0 || height == 0 {
crate::observe::fail(format!(
"compute_stage_tex buftex_fail reason=zero_geom ref={texture_ref} buf={} {width}x{height}",
bt.buffer_ref
));
return Err(ComputeStatus::Unsupported("compute_buftex_zero_geom"));
}
// A storage binding would have to write *back* through the buffer, which is
// a destination contract this arm has no evidence for: no case in the
// battery binds a buffer-backed texture writable, and inventing a writeback
// here would widen the repair past what it can show. Refused under its own
// name so the two questions stay separable in the log.
if is_storage {
crate::observe::fail(format!(
"compute_stage_tex buftex_fail reason=storage_destination ref={texture_ref} buf={} {width}x{height}",
bt.buffer_ref
));
return Err(ComputeStatus::Unsupported(
"compute_buffer_texture_storage_unsupported",
));
}
let format = if bt.desc.pixel_format != 0 {
bt.desc.pixel_format
} else {
crate::contract::pixel_format::MTL_FORMAT_BGRA8_UNORM
};
let Some(tight) = pixel_format::tight_row_bytes(width, format) else {
crate::observe::fail(format!(
"compute_stage_tex buftex_fail reason=unknown_fmt ref={texture_ref} buf={} fmt={format:#x} {width}x{height}",
bt.buffer_ref
));
return Err(ComputeStatus::Unsupported("compute_buftex_fmt"));
};
// A declared `bytesPerRow` of 0 means tight rows — the API default a
// single-row or unpadded texture serializes as. Same reading as the draw
// twin; the two arms must not differ on it.
let bpr = if bt.bytes_per_row == 0 {
u64::from(tight)
} else {
bt.bytes_per_row
};
if bpr < u64::from(tight) {
crate::observe::fail(format!(
"compute_stage_tex buftex_fail reason=bpr_short ref={texture_ref} buf={} bpr={bpr} tight={tight} {width}x{height} fmt={format:#x}",
bt.buffer_ref
));
return Err(ComputeStatus::Unsupported("compute_buftex_bpr_short"));
}
// The span the guest's rows actually occupy. Every row is `bpr` apart, but
// the last one only needs its texels: demanding a full trailing pitch would
// refuse a texture whose final row sits at the very end of the allocation.
let Some(span) = bpr
.checked_mul(u64::from(height) - 1)
.and_then(|s| s.checked_add(u64::from(tight)))
.and_then(|s| usize::try_from(s).ok())
else {
crate::observe::fail(format!(
"compute_stage_tex buftex_fail reason=span_overflow ref={texture_ref} buf={} bpr={bpr} {width}x{height}",
bt.buffer_ref
));
return Err(ComputeStatus::Unsupported("compute_buftex_span"));
};
// A buffer-backed texture is two contract references over one allocation —
// the type-8 texture the guest binds and the type-1 buffer that owns the
// storage — and a debt may be armed under either. The draw twin pays for
// both; so does this.
crate::runtime::writeback_debt::pay_for_texture(state, host, task_id, texture_ref);
let raw = read_buffer_window(state, host, task_id, bt.buffer_ref, bt.offset, span)?;
let tight = tight as usize;
let bpr = bpr as usize;
let mut bytes = vec![0u8; tight * height as usize];
for y in 0..height as usize {
let src = y * bpr;
bytes[y * tight..(y + 1) * tight].copy_from_slice(&raw[src..src + tight]);
}
crate::observe::off(format!(
"compute_stage_tex buftex_ok ref={texture_ref} buf={} fmt={format:#x} {width}x{height} off={} bpr={bpr} tight={tight}",
bt.buffer_ref, bt.offset
));
Ok(StagedTexture {
binding,
#[cfg(feature = "backend-vulkan")]
array_element: 0,
#[cfg(feature = "backend-vulkan")]
descriptor_count: 1,
#[cfg(all(feature = "backend-metal", target_os = "macos"))]
texture_ref,
pixel_format: format,
storage_selector: pixel_format::storage_selector(format),
width,
height,
bytes,
is_storage,
#[cfg(feature = "backend-vulkan")]
residency: None,
#[cfg(feature = "backend-vulkan")]
serve: None,
writeback: TextureWriteback::None,
})
}
/// Load tight raw texels for a compute texture binding (type-2/3, type-5→surface, or type-11).
///
/// Type-5 (`RefTextureHandle`) is the live CI wallpaper path (`compute_stage_tex … ot=5`).
@@ -1954,6 +2086,8 @@ pub(crate) fn stage_texture_raw<M: HostMemory + HostOps>(
let mut view_level = 0;
let mut view_pixel_format = None;
let mut heap_texture = None;
let mut buffer_texture: Option<crate::runtime::decode::resource::BufferTextureDescriptor> =
None;
// A linear texture object (type-2/3) must resolve through its own
// descriptor, never through the mapping registry: its numeric ref shares
// the id space with type-4 surface mids, so the `mappings.contains(ref)`
@@ -2039,13 +2173,24 @@ pub(crate) fn stage_texture_raw<M: HostMemory + HostOps>(
} else if opcode == TEXTURE_VIEW_OPCODE_BUFFER_TEXTURE
|| opcode == TEXTURE_VIEW_OPCODE_BUFFER_TEXTURE_WIDE
{
crate::observe::fail(format!(
"compute_stage_tex view_fail reason=buffer_texture_unsupported ref={texture_ref} opcode={opcode} desc_len={}",
desc.len()
));
return Err(ComputeStatus::Unsupported(
"compute_buffer_texture_unsupported",
));
// The same wire form `runtime::draw` decodes and executes, read
// through the same decoder. This arm used to refuse it.
let bt =
match crate::runtime::decode::resource::decode_buffer_texture_descriptor(&desc)
{
Ok(bt) => bt,
Err(error) => {
crate::observe::Emit::decline("compute_stage_tex_buftex", &error)
.field("ref", texture_ref)
.field("opcode", format!("{opcode:#x}"))
.field("len", desc.len())
.fail();
return Err(ComputeStatus::MissingTexture(
"compute_stage_tex_buftex_desc",
));
}
};
buffer_texture = Some(bt);
} else {
let view = match crate::runtime::draw::resolve_texture_view_reasoned(
state,
@@ -2083,6 +2228,9 @@ pub(crate) fn stage_texture_raw<M: HostMemory + HostOps>(
}
}
}
if let Some(bt) = buffer_texture {
return stage_buffer_texture(state, host, task_id, texture_ref, binding, is_storage, &bt);
}
if let Some((heap_ref, use_offset, offset, descriptor)) = heap_texture {
if descriptor.texture_type != 2
|| descriptor.depth != 1
@@ -2911,3 +2911,144 @@ fn a_sampled_image_the_kernel_uses_and_the_guest_left_empty_gets_a_neutral_textu
// invented back into the list.
assert_eq!(neutral_sampled_image_bindings(&spirv, &[99]), vec![33]);
}
/// A buffer-backed texture (opcode 9) bound to a compute *read* is staged from
/// the type-1 buffer's own bytes, de-pitched to tight rows in its native
/// format.
///
/// This arm refused the whole wire form until now, while `runtime::draw`
/// decoded and executed it — two execution arms disagreeing about one record.
///
/// The row padding carries a value that appears nowhere in the texels, because
/// the failure this guards against is not "no bytes" but "the padding folded
/// into the image", which produces a plausible picture that is wrong by one
/// stride per row. The conformance battery fills padding the same way and for
/// the same reason.
#[test]
fn a_buffer_backed_texture_stages_its_texels_without_the_row_padding() {
use crate::contract::endian::st16;
use crate::contract::pixel_format::MTL_FORMAT_BGRA8_UNORM;
use crate::runtime::decode::resource::{
BUF_TEX_DESC_BUFFER_REF, BUF_TEX_DESC_BYTES_PER_ROW, BUF_TEX_DESC_OFFSET,
BUF_TEX_WIDE_DESC_BODY, BUF_TEX_WIDE_LEN, TEXTURE_VIEW_DESC_LEN, TEXTURE_VIEW_DESC_OPCODE,
TEXTURE_VIEW_DESC_TEXTURE_REF, TEXTURE_VIEW_OPCODE_BUFFER_TEXTURE_WIDE,
};
use reims_vgpu_wire::ops::texture::WideTextureDescriptorBody as W;
const W_TEXELS: usize = 4;
const H_ROWS: usize = 3;
const BPP: usize = 4; // BGRA8Unorm
const TIGHT: usize = W_TEXELS * BPP;
const PITCH: usize = TIGHT + 8; // eight bytes of padding per row
const PAD: u8 = 0xEE;
let mut host = FakeHost::new();
let mut state = DeviceState::new(DeviceId(1), PAGE_SHIFT_ARM64E);
gva_mem::define_task_pages_arm64e(&mut host, &mut state, 4, 8);
assert!(state.set_object_list(1, 0, 32));
// Texel bytes are position-dependent so a row served from the wrong offset
// is a different value, not merely a shifted one.
let mut pixels = vec![PAD; PITCH * H_ROWS];
for y in 0..H_ROWS {
for x in 0..TIGHT {
pixels[y * PITCH + x] = (y * 0x10 + x) as u8;
}
}
let buf_gva = 5u64 << RESOURCE_PAGE_SHIFT;
write_task_gva_arm64e(&mut host, &state.tasks[1], buf_gva, &pixels);
// Type-1 buffer object naming that storage.
let mut bdesc = vec![0u8; 16];
st64(&mut bdesc[0..], (PITCH * H_ROWS) as u64);
st32(&mut bdesc[8..], 5);
let bdesc_gva = 0x180u64;
write_task_gva_arm64e(&mut host, &state.tasks[1], bdesc_gva, &bdesc);
{
let off = list_object_entry_offset(7, 32).unwrap();
let mut le = [0u8; OBJECT_LIST_ENTRY_LEN];
st32(&mut le[0..], (OBJECT_TYPE_BUFFER as u32) | (16u32 << 8));
le[4..12].copy_from_slice(&bdesc_gva.to_le_bytes());
write_task_gva_arm64e(&mut host, &state.tasks[1], off, &le);
}
// Type-8 buffer-backed texture record over it.
let mut body = vec![0u8; crate::runtime::heap_query::WIDE_TEXTURE_BODY_LEN];
body[std::mem::offset_of!(W, type_and_flags)] = 0x02; // 2D
st16(
&mut body[std::mem::offset_of!(W, pixel_format)..],
MTL_FORMAT_BGRA8_UNORM,
);
st32(&mut body[std::mem::offset_of!(W, width)..], W_TEXELS as u32);
st32(&mut body[std::mem::offset_of!(W, height)..], H_ROWS as u32);
st32(&mut body[std::mem::offset_of!(W, depth)..], 1);
st16(&mut body[std::mem::offset_of!(W, mipmap_level_count)..], 1);
st16(&mut body[std::mem::offset_of!(W, sample_count)..], 1);
st16(&mut body[std::mem::offset_of!(W, array_length)..], 1);
let mut rec = vec![0u8; BUF_TEX_WIDE_LEN];
st32(
&mut rec[TEXTURE_VIEW_DESC_OPCODE..],
TEXTURE_VIEW_OPCODE_BUFFER_TEXTURE_WIDE,
);
st32(&mut rec[TEXTURE_VIEW_DESC_LEN..], BUF_TEX_WIDE_LEN as u32);
st32(&mut rec[TEXTURE_VIEW_DESC_TEXTURE_REF..], 21);
st32(&mut rec[BUF_TEX_DESC_BUFFER_REF..], 7);
rec[BUF_TEX_DESC_OFFSET..BUF_TEX_DESC_OFFSET + 8].copy_from_slice(&0u64.to_le_bytes());
rec[BUF_TEX_DESC_BYTES_PER_ROW..BUF_TEX_DESC_BYTES_PER_ROW + 8]
.copy_from_slice(&(PITCH as u64).to_le_bytes());
rec[BUF_TEX_WIDE_DESC_BODY..].copy_from_slice(&body);
let rec_gva = 0x280u64;
write_task_gva_arm64e(&mut host, &state.tasks[1], rec_gva, &rec);
{
let off = list_object_entry_offset(21, 32).unwrap();
let mut le = [0u8; OBJECT_LIST_ENTRY_LEN];
st32(
&mut le[0..],
(crate::runtime::decode::resource::OBJECT_TYPE_TEXTURE_VIEW as u32)
| ((rec.len() as u32) << 8),
);
le[4..12].copy_from_slice(&rec_gva.to_le_bytes());
write_task_gva_arm64e(&mut host, &state.tasks[1], off, &le);
}
let staged = stage_texture_raw(&mut state, &mut host, 1, 21, 0, false)
.expect("a buffer-backed texture is a wire form this device decodes");
assert_eq!(staged.width, W_TEXELS as u32);
assert_eq!(staged.height, H_ROWS as u32);
assert_eq!(
staged.pixel_format, MTL_FORMAT_BGRA8_UNORM,
"the native format survives; this arm does not narrow to RGBA8"
);
assert_eq!(
staged.bytes.len(),
TIGHT * H_ROWS,
"the staged image is tight, not the guest's padded span"
);
assert!(
!staged.bytes.contains(&PAD),
"row padding must not reach the image"
);
for y in 0..H_ROWS {
for x in 0..TIGHT {
assert_eq!(
staged.bytes[y * TIGHT + x],
(y * 0x10 + x) as u8,
"row {y} byte {x} came from the wrong offset"
);
}
}
// The destination half is a separate contract with no evidence behind it,
// so a writable binding of the same record still refuses, under its own
// name rather than the retired blanket one.
match stage_texture_raw(&mut state, &mut host, 1, 21, 0, true) {
Err(ComputeStatus::Unsupported(slug)) => {
assert_eq!(slug, "compute_buffer_texture_storage_unsupported")
}
Err(other) => panic!("a storage binding must refuse by name, got {other:?}"),
Ok(_) => panic!("a storage binding must refuse; this arm has no destination contract"),
}
}
@@ -1090,11 +1090,13 @@ const HEAP_TEXTURE_WIDE_OFFSET: usize = OP_HDR + offset_of!(w_heap::NewHeapTextu
// and the body is {u64 offset, u64 bytesPerRow, embedded MTLTextureDescriptor}.
pub const TEXTURE_VIEW_OPCODE_BUFFER_TEXTURE: u32 = w_backed::OPCODE_BUFFER_TEXTURE;
#[cfg(test)]
const BUF_TEX_DESC_BUFFER_REF: usize = OP_HDR + offset_of!(w_backed::BufferTextureBody, buffer_ref);
pub(crate) const BUF_TEX_DESC_BUFFER_REF: usize =
OP_HDR + offset_of!(w_backed::BufferTextureBody, buffer_ref);
#[cfg(test)]
const BUF_TEX_DESC_OFFSET: usize = OP_HDR + offset_of!(w_backed::BufferTextureBody, offset);
pub(crate) const BUF_TEX_DESC_OFFSET: usize =
OP_HDR + offset_of!(w_backed::BufferTextureBody, offset);
#[cfg(test)]
const BUF_TEX_DESC_BYTES_PER_ROW: usize =
pub(crate) const BUF_TEX_DESC_BYTES_PER_ROW: usize =
OP_HDR + offset_of!(w_backed::BufferTextureBody, bytes_per_row);
// The embedded `PGSerializedTextureDescriptor` is not named here at all: there
// is one decoder for it and everything inside it is at that decoder's own
@@ -1107,7 +1109,8 @@ pub const BUF_TEX_MIN_LEN: usize = w_backed::BUFFER_TEXTURE_TOTAL_LEN as usize;
// the descriptor keep their offsets; only the descriptor widens.
pub const TEXTURE_VIEW_OPCODE_BUFFER_TEXTURE_WIDE: u32 = w_backed::OPCODE_BUFFER_TEXTURE_WIDE;
#[cfg(test)]
const BUF_TEX_WIDE_DESC_BODY: usize = OP_HDR + offset_of!(w_backed::BufferTextureWideBody, desc);
pub(crate) const BUF_TEX_WIDE_DESC_BODY: usize =
OP_HDR + offset_of!(w_backed::BufferTextureWideBody, desc);
pub const BUF_TEX_WIDE_LEN: usize = w_backed::BUFFER_TEXTURE_WIDE_TOTAL_LEN as usize;
// MTLTextureType values (Metal.framework Headers/MTLTextureType.h).
pub const TEXTURE_VIEW_MTL_TYPE_1D: u16 = 0;