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

Open
opened 2026-11-27 22:09:41 +00:00 by Tarraga · 2 comments
Tarraga commented 2026-11-27 22:09:41 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Tarraga on 2026-11-27T22:09:41Z.


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6655244190 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/491). Originally posted by [Tarraga](https://github.com/Tarraga) on 2026-11-27T22:09:41Z. --- ### 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 commented 2026-11-27 22:09:57 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Tarraga on 2026-11-27T22:09:57Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229085164 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/491#issuecomment-7229085164). Originally posted by [Tarraga](https://github.com/Tarraga) on 2026-11-27T22:09:57Z. --- /claim
github-actions[bot] commented 2026-11-27 22:10:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T22:10:09Z.


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229087993 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/491#issuecomment-7229087993). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T22:10:09Z. --- Claimed by @Tarraga. Open a draft pull request with `Refs #491` in its description within 24 hours, or the claim expires.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#491
No description provided.