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

Open
Tarraga wants to merge 2 commits from upstream-pr/497 into main
Tarraga commented 2026-11-27 22:16:03 +00:00 (Migrated from github.com)

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


Refs #491.

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 #398: both change write_typed_buffer. #398 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)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5533798882 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/497). Originally posted by [Tarraga](https://github.com/Tarraga) on 2026-11-27T22:16:03Z. --- Refs #491. ## 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 #398: both change `write_typed_buffer`. #398 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)
Olima34 commented 2026-11-28 02:00:38 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T02:00:38Z.


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 #497'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.
  • #497 and #398 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. #398 was left out of the combination for that reason.

In combination with #418 #466 #420 #409 #448 #433 #495 #497 on 86cefd22 (a local integration build, plus the pass-trace fix from #509):

  • World corpus: 302 shaders newly translated and 1 regression (see #433).
  • 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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7232066143 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/497#issuecomment-7232066143). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T02:00:38Z. --- 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 #497'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. - #497 and #398 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. #398 was left out of the combination for that reason. **In combination** with #418 #466 #420 #409 #448 #433 #495 #497 on 86cefd22 (a local integration build, plus the pass-trace fix from #509): - **World corpus:** 302 shaders newly translated and 1 regression (see #433). - **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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/497:upstream-pr/497'
git switch 'upstream-pr/497'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/497'
git switch 'upstream-pr/497'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/497'
git switch 'upstream-pr/497'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/497'
git switch 'main'
git merge --squash 'upstream-pr/497'
git switch 'main'
git merge --ff-only 'upstream-pr/497'
git switch 'main'
git merge 'upstream-pr/497'
git push origin 'main'
Sign in to join this conversation.
No reviewers
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!497
No description provided.