metal: typed buffer views that start inside a 16-byte unit read 0 and lose their stores #495

Open
opened 2026-11-27 22:11:29 +00:00 by github-bot · 2 comments
Owner

@⁠Tarraga on GitHub · 2026-11-27 22:09 UTC · original

What went wrong

Metal::write_typed_buffer (crates/victory-metal/src/gfx.rs) gives a typed buffer view whose pool offset is not a multiple of minimumTextureBufferAlignmentForPixelFormat (16 bytes on Apple Silicon) a raw buffer entry. A raw entry has no texture (word 1 = 0), so every typed load of that view reads 0 and every typed store is lost. Raw accesses through word 0 still work, which hides it.

The title does make such views: in a run to the world, the warning "typed buffer view offset is not aligned for MTLTextureTypeTextureBuffer; falling back to raw buffer entry" fires at least for an Rg32Float view 8 bytes into a 16-byte unit and an R16Uint view 2 bytes into one (the warning is logged once, so there is no count of how many more). Both belong to the irradiance cache's passes, whose records then read as all zeros.

A second thing keeps the obvious fix from working: the converter ignores the element offset in the entry's metadata (bits 32-39, textureViewOffsetInElements) unless IRCompilerSupportUnAlignedTypedBuffers(compiler, true) is called. It is a C++ symbol in D3DMetal 4.0b3's copy of the converter (_Z38IRCompilerSupportUnAlignedTypedBuffersP10IRCompilerb), not in the public header. Without it a typed index counts from the texture buffer's aligned start, so the view reads the elements before its own.

Steps to reproduce

A unit test on Metal, on x86_64 with the 4.0b3 converter (VICTORY_MSC_DYLIB=.../libmetalirconverter.dylib): three typed views (R16_UINT at byte 2, R32G32_FLOAT at byte 8, R32_UINT at byte 4 of a 16-byte unit), read and written by translated shaders. On main every load reads 0 and every store is lost.

Plan

Build the texture buffer from the 16-byte floor below the view, put the element offset (at most 255) in the entry's metadata, and call the converter's switch for modules that use typed buffers (off with VICTORY_MSC_UNALIGNED_TYPED=0). A view that starts inside an element keeps the raw fallback. I will open the pull request with the test. This may be part of the dark world (#258), but I have not shown that in a run, and I do not claim it.

Mac, macOS version and commit

M4 Pro, 48 GB, macOS 27.0, main b998bb65

**@⁠Tarraga** on GitHub · 2026-11-27 22:09 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/491) ### What went wrong `Metal::write_typed_buffer` (crates/victory-metal/src/gfx.rs) gives a typed buffer view whose pool offset is not a multiple of `minimumTextureBufferAlignmentForPixelFormat` (16 bytes on Apple Silicon) a raw buffer entry. A raw entry has no texture (word 1 = 0), so every typed load of that view reads 0 and every typed store is lost. Raw accesses through word 0 still work, which hides it. The title does make such views: in a run to the world, the warning "typed buffer view offset is not aligned for MTLTextureTypeTextureBuffer; falling back to raw buffer entry" fires at least for an `Rg32Float` view 8 bytes into a 16-byte unit and an `R16Uint` view 2 bytes into one (the warning is logged once, so there is no count of how many more). Both belong to the irradiance cache's passes, whose records then read as all zeros. A second thing keeps the obvious fix from working: the converter ignores the element offset in the entry's metadata (bits 32-39, `textureViewOffsetInElements`) unless `IRCompilerSupportUnAlignedTypedBuffers(compiler, true)` is called. It is a C++ symbol in D3DMetal 4.0b3's copy of the converter (`_Z38IRCompilerSupportUnAlignedTypedBuffersP10IRCompilerb`), not in the public header. Without it a typed index counts from the texture buffer's aligned start, so the view reads the elements before its own. ### Steps to reproduce A unit test on Metal, on x86_64 with the 4.0b3 converter (`VICTORY_MSC_DYLIB=.../libmetalirconverter.dylib`): three typed views (`R16_UINT` at byte 2, `R32G32_FLOAT` at byte 8, `R32_UINT` at byte 4 of a 16-byte unit), read and written by translated shaders. On main every load reads 0 and every store is lost. ### Plan Build the texture buffer from the 16-byte floor below the view, put the element offset (at most 255) in the entry's metadata, and call the converter's switch for modules that use typed buffers (off with `VICTORY_MSC_UNALIGNED_TYPED=0`). A view that starts inside an element keeps the raw fallback. I will open the pull request with the test. This may be part of the dark world (#258), but I have not shown that in a run, and I do not claim it. ### Mac, macOS version and commit M4 Pro, 48 GB, macOS 27.0, main b998bb65
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 22:11:30 +00:00
Author
Owner

@⁠Tarraga on GitHub · 2026-11-27 22:09 UTC · original

/claim

**@⁠Tarraga** on GitHub · 2026-11-27 22:09 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/491#issuecomment-7229085164) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-27 22:10 UTC · original

Claimed by @⁠Tarraga. Open a draft pull request with Refs #491 in its description within 24 hours, or the claim expires.

**@⁠github-actions[bot]** on GitHub · 2026-11-27 22:10 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/491#issuecomment-7229087993) Claimed by @⁠Tarraga. Open a draft pull request with `Refs #491` in its description within 24 hours, or the claim expires.
This discussion has been locked. Commenting is limited to contributors.
No description provided.