metal: typed buffer views that start inside a 16-byte unit read and write their own elements (#491) #497
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/497"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_buffergave 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 anRg32Floatview 8 bytes into a 16-byte unit and anR16Uintview 2 bytes into one, both in the irradiance cache's passes (records that read as all zeros).Fix
write_typed_buffermakes 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'stextureViewOffsetInElements). 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.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 withdlsymand nothing of Apple's is vendored.Mscturns it on only for modules with typed buffers; only their library cache keys change, the rest keep their cached libraries.VICTORY_MSC_UNALIGNED_TYPED=0leaves it off (default on when the converter has the symbol).Bindingused to flag typed-buffer modules moved tovictory_backend::dxilsoMsccan use it (a move, no change).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_elementshas 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.00000000; both Rg32 elements read 0; the R32 view's element 1 reads 022221111 44443333, Rg32 elements 2 and 3 stay 0)VICTORY_MSC_UNALIGNED_TYPED=01111, wanted2222)abcd1111 44443333)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'swrite_typed_buffer, fails withVICTORY_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.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.VICTORY_MSC_DYLIB, release): 413 passed, 0 failed, 6 ignored (main: 411 passed, 1 failed in the function-address testlist::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 policyok.Checklist
VICTORY_*variable and documented (VICTORY_MSC_UNALIGNED_TYPED=0is a switch to turn the new behaviour off)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
86cefd22withexamples/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:
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:
heap_tests.rsand docs.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):View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.