metal: typed buffer views that start inside a 16-byte unit read and write their own elements (#491) #501

Open
github-bot wants to merge 2 commits from github-bot/gh-497 into main AGit
Owner

@⁠Tarraga on GitHub · 2026-11-27 22:16 UTC · original
Tarraga:vibe/unaligned-typed-views-7636bf-pr → main · +776 −283 · 8 files · 2 commits · draft

Refs #495.

What this changes

Problem

Metal::write_typed_buffer gave a typed buffer view whose pool offset is not a multiple of Metal's texture-buffer alignment (16 bytes) a raw entry, and a raw entry has no texture (word 1 = 0). Every typed load of that view read 0 and every typed store was lost. The title makes such views: the existing fallback warning fires for an Rg32Float view 8 bytes into a 16-byte unit and an R16Uint view 2 bytes into one, both in the irradiance cache's passes (records that read as all zeros).

Fix

  • write_typed_buffer makes the texture buffer from the 16-byte floor below the view and puts the view's element offset (at most 255) in the entry's metadata, bits 32-39 (IRDescriptorTableSetBufferView's textureViewOffsetInElements). Word 0 stays the view's own address, so raw accesses are unchanged. A view that starts inside an element still falls back to a raw entry.
  • The converter ignores that offset unless told: IRCompilerSupportUnAlignedTypedBuffers(compiler, true). It is a C++ symbol in D3DMetal 4.0b3's converter (_Z38IRCompilerSupportUnAlignedTypedBuffersP10IRCompilerb), not in Apple's public header, so it is looked up with dlsym and nothing of Apple's is vendored. Msc turns it on only for modules with typed buffers; only their library cache keys change, the rest keep their cached libraries. VICTORY_MSC_UNALIGNED_TYPED=0 leaves it off (default on when the converter has the symbol).
  • The DXIL scan Binding used to flag typed-buffer modules moved to victory_backend::dxil so Msc can use it (a move, no change).
  • The fallback warning now counts every view by format and byte past the aligned start (info when taken, warn when it falls back; at the 1st, 2nd, 4th, ... and every 4096th).

An out-of-range index wrapped into another object's bytes (found after the first version)

The first version of this change had a bug. The converter adds the view's element offset to the index in 32 bits before the texture buffer's bounds check. So on a view that starts inside a 16-byte unit, index 0xffffffff (-1, "past the end" on the console) became element skip - 1: inside the bounds, the element just before the view, which belongs to another object. A store there overwrote that object's bytes and a load read them. On the console the store is dropped and the load returns 0. An aligned view (skip = 0) was never affected. A GPU test showed it with D3DMetal 4.0b3's converter; in the game this may be behind black planes, but that is not shown.

Fix (second commit): the translator holds every typed buffer load and store index of 2^31 and up at 2^31 - 1 (Lower::typed_index, one compare and select per access). That index is still past the end of any view (the host's views are at most 2^26 elements), so the converter's bounds check drops the store and returns 0 for the load, as the console does. It changes the DXIL of every module with typed buffer loads or stores (their cached libraries are made once again).

New test: heap_tests::typed_views_of_every_format_and_offset_keep_to_their_own_elements (victory-gpu, x86_64, VICTORY_MSC_DYLIB = D3DMetal 4.0b3's converter). 12 formats (R32/RG32 uint and float, R16/RG16 uint, R16/RG16/RGBA16 float, R8 and RGBA8 uint, RGBA8 unorm), every element start inside a 16-byte unit and one inside an element, stores at element 3, one past the end and at -1, loads of the first, last, past-the-end and -1 elements, every byte of the memory around compared. Without the clamp it reports 99 failures (each unaligned view at -1); with it, none. It also found nothing else wrong in the offset path (the offset is in elements for every format, at most 15, and the texture ends with the view).

Evidence

Numbers from the tests, x86_64 under Rosetta with D3DMetal 4.0b3's converter. heap_tests::typed_views_that_start_inside_a_16_byte_unit_read_and_write_the_titles_elements has three views of a 4-element buffer (R16 at byte 2, Rg32 at byte 8, R32 at byte 4), a shader that loads each view's elements and stores new ones.

loads stores
main element 0 and 1 of the R16 view read 00000000; both Rg32 elements read 0; the R32 view's element 1 reads 0 none land (R16 memory 22221111 44443333, Rg32 elements 2 and 3 stay 0)
this change, VICTORY_MSC_UNALIGNED_TYPED=0 the elements from the aligned start (R16 element 0 reads 1111, wanted 2222) land in the wrong elements (R16 abcd1111 44443333)
this change all elements, as wanted all land

I have not run the game on this tree, with or without the clamp, and do not claim a change in the picture; the fix is not yet confirmed in a game run. This may be part of the dark world (#258), but that is not shown here.

Overlap with #402: both change write_typed_buffer. #402 caches the texture it makes, this one changes which texture and entry it makes; the first to land is rebased on by the other (the cache key takes the aligned start).

How it was tested

  • heap_tests::typed_views_that_start_inside_a_16_byte_unit_read_and_write_the_titles_elements (victory-gpu, x86_64, VICTORY_MSC_DYLIB = D3DMetal 4.0b3's converter): fails on main's write_typed_buffer, fails with VICTORY_MSC_UNALIGNED_TYPED=0, passes with the change.
  • gfx::unaligned_typed_view_tests::a_typed_view_inside_a_16_byte_unit_names_a_texture_and_its_element_offset (victory-metal): the entry words of three views (address, texture, size | element offset << 32), and a raw entry for a view inside an element. Passes.
  • heap_tests::typed_views_of_every_format_and_offset_keep_to_their_own_elements: fails without the clamp (99 failures), passes with it.
  • Windows clippy (x86_64-pc-windows-gnullvm): not run locally (no such target on this Mac); left to CI. Host clippy shows no warning on the changed lines.
  • Full victory-gpu suite (x86_64, VICTORY_MSC_DYLIB, release): 413 passed, 0 failed, 6 ignored (main: 411 passed, 1 failed in the function-address test list::tests::unordered_access_clears_have_the_same_slots_in_both_layouts, which depends on the build and passed in this one).
    Re-run after the second commit (x86_64, VICTORY_MSC_DYLIB): victory-gpu 414 passed, 0 failed, 6 ignored; victory-shader 402 + 5 passed; cargo xtask policy ok.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md
  • New diagnostics are behind a VICTORY_* variable and documented (VICTORY_MSC_UNALIGNED_TYPED=0 is a switch to turn the new behaviour off)
**@⁠Tarraga** on GitHub · 2026-11-27 22:16 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/497) `Tarraga:vibe/unaligned-typed-views-7636bf-pr` → `main` · +776 −283 · 8 files · 2 commits · draft Refs #495. ## What this changes ### Problem `Metal::write_typed_buffer` gave a typed buffer view whose pool offset is not a multiple of Metal's texture-buffer alignment (16 bytes) a raw entry, and a raw entry has no texture (word 1 = 0). Every typed load of that view read 0 and every typed store was lost. The title makes such views: the existing fallback warning fires for an `Rg32Float` view 8 bytes into a 16-byte unit and an `R16Uint` view 2 bytes into one, both in the irradiance cache's passes (records that read as all zeros). ### Fix - `write_typed_buffer` makes the texture buffer from the 16-byte floor below the view and puts the view's element offset (at most 255) in the entry's metadata, bits 32-39 (`IRDescriptorTableSetBufferView`'s `textureViewOffsetInElements`). Word 0 stays the view's own address, so raw accesses are unchanged. A view that starts inside an element still falls back to a raw entry. - The converter ignores that offset unless told: `IRCompilerSupportUnAlignedTypedBuffers(compiler, true)`. It is a C++ symbol in D3DMetal 4.0b3's converter (`_Z38IRCompilerSupportUnAlignedTypedBuffersP10IRCompilerb`), not in Apple's public header, so it is looked up with `dlsym` and nothing of Apple's is vendored. `Msc` turns it on only for modules with typed buffers; only their library cache keys change, the rest keep their cached libraries. `VICTORY_MSC_UNALIGNED_TYPED=0` leaves it off (default on when the converter has the symbol). - The DXIL scan `Binding` used to flag typed-buffer modules moved to `victory_backend::dxil` so `Msc` can use it (a move, no change). - The fallback warning now counts every view by format and byte past the aligned start (info when taken, warn when it falls back; at the 1st, 2nd, 4th, ... and every 4096th). ### An out-of-range index wrapped into another object's bytes (found after the first version) The first version of this change had a bug. The converter adds the view's element offset to the index in 32 bits **before** the texture buffer's bounds check. So on a view that starts inside a 16-byte unit, index 0xffffffff (-1, "past the end" on the console) became element `skip - 1`: inside the bounds, the element just before the view, which belongs to another object. A store there overwrote that object's bytes and a load read them. On the console the store is dropped and the load returns 0. An aligned view (`skip` = 0) was never affected. A GPU test showed it with D3DMetal 4.0b3's converter; in the game this may be behind black planes, but that is not shown. Fix (second commit): the translator holds every typed buffer load and store index of 2^31 and up at 2^31 - 1 (`Lower::typed_index`, one compare and select per access). That index is still past the end of any view (the host's views are at most 2^26 elements), so the converter's bounds check drops the store and returns 0 for the load, as the console does. It changes the DXIL of every module with typed buffer loads or stores (their cached libraries are made once again). New test: `heap_tests::typed_views_of_every_format_and_offset_keep_to_their_own_elements` (victory-gpu, x86_64, `VICTORY_MSC_DYLIB` = D3DMetal 4.0b3's converter). 12 formats (R32/RG32 uint and float, R16/RG16 uint, R16/RG16/RGBA16 float, R8 and RGBA8 uint, RGBA8 unorm), every element start inside a 16-byte unit and one inside an element, stores at element 3, one past the end and at -1, loads of the first, last, past-the-end and -1 elements, every byte of the memory around compared. Without the clamp it reports 99 failures (each unaligned view at -1); with it, none. It also found nothing else wrong in the offset path (the offset is in elements for every format, at most 15, and the texture ends with the view). ## Evidence Numbers from the tests, x86_64 under Rosetta with D3DMetal 4.0b3's converter. `heap_tests::typed_views_that_start_inside_a_16_byte_unit_read_and_write_the_titles_elements` has three views of a 4-element buffer (R16 at byte 2, Rg32 at byte 8, R32 at byte 4), a shader that loads each view's elements and stores new ones. | | loads | stores | |---|---|---| | main | element 0 and 1 of the R16 view read `00000000`; both Rg32 elements read 0; the R32 view's element 1 reads 0 | none land (R16 memory `22221111 44443333`, Rg32 elements 2 and 3 stay 0) | | this change, `VICTORY_MSC_UNALIGNED_TYPED=0` | the elements from the aligned start (R16 element 0 reads `1111`, wanted `2222`) | land in the wrong elements (R16 `abcd1111 44443333`) | | this change | all elements, as wanted | all land | I have not run the game on this tree, with or without the clamp, and do not claim a change in the picture; the fix is not yet confirmed in a game run. This may be part of the dark world (#258), but that is not shown here. Overlap with #402: both change `write_typed_buffer`. #402 caches the texture it makes, this one changes which texture and entry it makes; the first to land is rebased on by the other (the cache key takes the aligned start). ## How it was tested - `heap_tests::typed_views_that_start_inside_a_16_byte_unit_read_and_write_the_titles_elements` (victory-gpu, x86_64, `VICTORY_MSC_DYLIB` = D3DMetal 4.0b3's converter): fails on main's `write_typed_buffer`, fails with `VICTORY_MSC_UNALIGNED_TYPED=0`, passes with the change. - `gfx::unaligned_typed_view_tests::a_typed_view_inside_a_16_byte_unit_names_a_texture_and_its_element_offset` (victory-metal): the entry words of three views (address, texture, size | element offset << 32), and a raw entry for a view inside an element. Passes. - `heap_tests::typed_views_of_every_format_and_offset_keep_to_their_own_elements`: fails without the clamp (99 failures), passes with it. - [ ] Windows clippy (`x86_64-pc-windows-gnullvm`): not run locally (no such target on this Mac); left to CI. Host clippy shows no warning on the changed lines. - Full victory-gpu suite (x86_64, `VICTORY_MSC_DYLIB`, release): 413 passed, 0 failed, 6 ignored (main: 411 passed, 1 failed in the function-address test `list::tests::unordered_access_clears_have_the_same_slots_in_both_layouts`, which depends on the build and passed in this one). Re-run after the second commit (x86_64, `VICTORY_MSC_DYLIB`): victory-gpu 414 passed, 0 failed, 6 ignored; victory-shader 402 + 5 passed; `cargo xtask policy` ok. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md - [x] New diagnostics are behind a `VICTORY_*` variable and documented (`VICTORY_MSC_UNALIGNED_TYPED=0` is a switch to turn the new behaviour off)
A typed view whose pool offset is not a multiple of Metal's texture-buffer
alignment (16 bytes) got a raw entry from write_typed_buffer, and a raw entry
has no texture: its typed loads read 0 and its typed stores were lost. The
irradiance cache's views are of this kind (an Rg32Float view at byte 8 of a
unit, an R16Uint view at byte 2).

- write_typed_buffer makes the texture buffer from the multiple of the
  alignment below the view and puts the view's element offset in the entry's
  metadata (bits 32-39, textureViewOffsetInElements, at most 255). Only a view
  that starts inside an element still falls back to a raw entry.
- The converter ignores that offset unless told:
  IRCompilerSupportUnAlignedTypedBuffers(compiler, true), a C++ symbol of
  D3DMetal 4.0b3's converter, not in the public header. Msc turns it on for
  modules with typed buffers (the DXIL scan Binding used, moved to
  victory_backend::dxil); only their library cache keys change.
  VICTORY_MSC_UNALIGNED_TYPED=0 leaves it off.
- The fallback warning now counts every view by format and byte past the
  aligned start (info when taken, warn when it falls back; 1st, 2nd, 4th, ...
  and every 4096th, with the count).

Tests: native::tests::heap_tests::typed_views_that_start_inside_a_16_byte_unit_read_and_write_the_titles_elements
(a Metal run through the converter: R16 at byte 2, Rg32 at byte 8 and R32 at byte 4
read 0 and lose their stores without the change) and
gfx::unaligned_typed_view_tests::a_typed_view_inside_a_16_byte_unit_names_a_texture_and_its_element_offset.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 22:17:31 +00:00
A view that starts inside a 16-byte unit gets a texture buffer from the
unit's start and an element offset in its entry. The converter
(IRCompilerSupportUnAlignedTypedBuffers) adds that offset to the index in 32
bits before the texture's bounds check. Index 0xffffffff (-1, "past the end"
on the console) then became element skip - 1, the element just before the
view: a store there overwrote another object's bytes and a load read them,
where the console drops the store and reads 0. The previous commit had this
bug.

The translator now holds every typed buffer load and store index of 2^31 and
up at 2^31 - 1 (Lower::typed_index, one compare and select per access), still
past the end of any view (the host's are at most 2^26 elements). This changes
the DXIL of every module with typed buffer loads or stores.

Test (x86_64, D3DMetal 4.0b3 converter):
heap_tests::typed_views_of_every_format_and_offset_keep_to_their_own_elements,
12 formats, every element start in a 16-byte unit and one inside an element,
stores at 3, past the end and at -1, loads of the first, last, past-the-end
and -1 elements, every byte around compared: 99 failures without the clamp,
none with it.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-28 02:00 UTC · original

Thanks for this PR! An offline check of 28a96270:

Measured offline on main 86cefd22 with examples/sweep + tools/corpus/compare.sh, on our 21,600-shader world corpus. The PR alone was swept over the 10,319 world shaders that the combination below changes. "Passes/frame" is an estimate from four traced world frames of an earlier preview run: the dropped passes whose only refused shader this PR translates.

#497 alone:

  • Newly translated: 0.
  • Outputs changed: 8,448 (5,398 vertex, 1,908 pixel, 1,142 compute).
  • Regressions: 0.

So the typed-view change reaches the shader code of most world programs, not only Metal's view setup.

GPU errors and fps: I can't isolate #501's effect, because there was no matching run without it. In the combination run, there were no GPU errors and the world ran at about 1.4 fps.

Merging:

  • Conflicts with main are only in heap_tests.rs and docs.
  • #501 and #402 conflict for real in crates/victory-metal/src/gfx.rs, with 3 hunks in the typed-view path. Each merges cleanly on main by itself, so whichever lands second needs a rebase. #402 was left out of the combination for that reason.

In combination with #422 #470 #424 #413 #452 #437 #499 #501 on 86cefd22 (a local integration build, plus the pass-trace fix from #513):

  • World corpus: 302 shaders newly translated and 1 regression (see #437).
  • Windowed run (6 min): no GPU errors, and the GPU memory drained afterwards. About 1.4 fps in the world. 351 passes dropped per world frame, against 456 in an earlier census of the preview branch; these are different runs, so not a clean A/B.
**@⁠Olima34** on GitHub · 2026-11-28 02:00 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/497#issuecomment-7232066143) Thanks for this PR! An offline check of 28a96270: Measured offline on main 86cefd22 with `examples/sweep` + `tools/corpus/compare.sh`, on our 21,600-shader world corpus. The PR alone was swept over the 10,319 world shaders that the combination below changes. "Passes/frame" is an estimate from four traced world frames of an earlier preview run: the dropped passes whose only refused shader this PR translates. **#497 alone:** - **Newly translated:** 0. - **Outputs changed:** 8,448 (5,398 vertex, 1,908 pixel, 1,142 compute). - **Regressions:** 0. So the typed-view change reaches the shader code of most world programs, not only Metal's view setup. **GPU errors and fps:** I can't isolate #501's effect, because there was no matching run without it. In the combination run, there were no GPU errors and the world ran at about 1.4 fps. **Merging:** - Conflicts with main are only in `heap_tests.rs` and docs. - #501 and #402 conflict for real in `crates/victory-metal/src/gfx.rs`, with 3 hunks in the typed-view path. Each merges cleanly on main by itself, so whichever lands second needs a rebase. #402 was left out of the combination for that reason. **In combination** with #422 #470 #424 #413 #452 #437 #499 #501 on 86cefd22 (a local integration build, plus the pass-trace fix from #513): - **World corpus:** 302 shaders newly translated and 1 regression (see #437). - **Windowed run (6 min):** no GPU errors, and the GPU memory drained afterwards. About 1.4 fps in the world. 351 passes dropped per world frame, against 456 in an earlier census of the preview branch; these are different runs, so not a clean A/B.
This pull request has changes conflicting with the target branch.
  • crates/victory-backend/src/lib.rs
  • crates/victory-gpu/src/native/heap_tests.rs
  • crates/victory-gpu/src/native/list.rs
  • crates/victory-metal/src/gfx.rs
  • docs/native-host.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/501/head:github-bot/gh-497'
git switch 'github-bot/gh-497'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-497'
git switch 'github-bot/gh-497'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-497'
git switch 'github-bot/gh-497'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-497'
git switch 'main'
git merge --squash 'github-bot/gh-497'
git switch 'main'
git merge --ff-only 'github-bot/gh-497'
git switch 'main'
git merge 'github-bot/gh-497'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.