Shaders, Loader: programs follow the pages the game remaps #8

Manually merged
github merged 3 commits from gh-pr/8/head into gh-pr/8/base 2026-11-27 03:13:30 +00:00
Owner

@⁠marquezinii on GitHub · 2026-11-24 18:15 UTC · original
marquezinii:shaders/address-map-remaps → main · +521 −187 · 18 files · 3 commits · merged 2026-11-24 19:47 UTC

The fix for #4. Translated programs now reach the frame the CPU sees after the game remaps a page under a placed buffer. Before, reads saw the old frame and writes changed whatever owned it next. Two commits:

Shaders: look up a V# access at base + offset. buf_view used to take the view of the base's 64 KiB page and add the access offset inside it. That works only while the following pages belong to the same desktop buffer and keep their frames. buf_view now takes the offset, adds it to the base in 64 bits (ptr_add) and looks that address up. Loads, stores, formatted accesses, atomics and s_buffer_load use the view of their own address.

  • The lookup count per instruction is the same as before.
  • For base_hi == 0 (heap-index V#s) the result is unchanged.
  • Range checks are untouched.
  • An access's later dwords continue in the view of its first byte's page.

Loader: the address map follows XMem remaps. Every map, remap and unmap now rewrites that page's map entry, at its GPU-only alias if it has one, else at its CPU address.

  • The entry names raw views over the section that holds the frame now: one buffer placed per 512 MiB window of the section, made on first use.
  • An unmapped page names the null views.
  • Pages mapped before the view heap exists are entered when the map is made.
  • Section heaps are now opened in one place (SectionHeaps) for slot 133 and the map. A section over 1 GiB takes a heap per 1 GiB window (the windows from 84e20e5, folded in), and each view stays inside its window.
  • Map entries are written as one 8-byte store, because programs on other queues read the map while a page is remapped.
  • ObservePages takes several observers. Before, the second registration replaced StaleGpuPages.

What still keeps the old frames: desktop views, copies and indirect arguments made on a slot-133 buffer. The [gpu pages] log from #5 still reports those. One related case changes behaviour: a slot-133 buffer that had no mapped page when it was made gets a committed buffer of its own. Once the game maps pages there, programs reach those pages, while desktop copies and views on the resource keep the committed memory. physical-memory.md describes both.

Tests, all on Windows with an RTX 5070 (driver 621.40):

  • cs_root_descriptor (WARP, dxc and the DXIL backend) now reads and writes across a page boundary into a second buffer. Same program text, so the assembled .bin is unchanged. It fails on main.
  • A_program_reaches_the_frame_the_game_remapped_under_a_placed_buffer (DxcFact, loader tests) compiles a program against rdna_prelude.hlsli and runs PtrStore/PtrLoad through the shim's map. A page is remapped under a slot-133 buffer and its old frame mapped into another range. Without the loader commit the load reads the old frame and the store is lost.
  • dotnet build has no warnings. dotnet test: 511 passed, 0 failed; the skipped tests need a dump or a full-wave GPU.

I have no dump, so this hasn't run in a boot.

Closes #4.

**@⁠marquezinii** on GitHub · 2026-11-24 18:15 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/8) `marquezinii:shaders/address-map-remaps` → `main` · +521 −187 · 18 files · 3 commits · merged 2026-11-24 19:47 UTC The fix for #4. Translated programs now reach the frame the CPU sees after the game remaps a page under a placed buffer. Before, reads saw the old frame and writes changed whatever owned it next. Two commits: **Shaders: look up a V# access at base + offset.** `buf_view` used to take the view of the base's 64 KiB page and add the access offset inside it. That works only while the following pages belong to the same desktop buffer and keep their frames. `buf_view` now takes the offset, adds it to the base in 64 bits (`ptr_add`) and looks that address up. Loads, stores, formatted accesses, atomics and `s_buffer_load` use the view of their own address. - The lookup count per instruction is the same as before. - For `base_hi == 0` (heap-index V#s) the result is unchanged. - Range checks are untouched. - An access's later dwords continue in the view of its first byte's page. **Loader: the address map follows XMem remaps.** Every map, remap and unmap now rewrites that page's map entry, at its GPU-only alias if it has one, else at its CPU address. - The entry names raw views over the section that holds the frame now: one buffer placed per 512 MiB window of the section, made on first use. - An unmapped page names the null views. - Pages mapped before the view heap exists are entered when the map is made. - Section heaps are now opened in one place (`SectionHeaps`) for slot 133 and the map. A section over 1 GiB takes a heap per 1 GiB window (the windows from 84e20e5, folded in), and each view stays inside its window. - Map entries are written as one 8-byte store, because programs on other queues read the map while a page is remapped. - `ObservePages` takes several observers. Before, the second registration replaced `StaleGpuPages`. What still keeps the old frames: desktop views, copies and indirect arguments made on a slot-133 buffer. The `[gpu pages]` log from #5 still reports those. One related case changes behaviour: a slot-133 buffer that had no mapped page when it was made gets a committed buffer of its own. Once the game maps pages there, programs reach those pages, while desktop copies and views on the resource keep the committed memory. physical-memory.md describes both. Tests, all on Windows with an RTX 5070 (driver 621.40): - `cs_root_descriptor` (WARP, dxc and the DXIL backend) now reads and writes across a page boundary into a second buffer. Same program text, so the assembled .bin is unchanged. It fails on main. - `A_program_reaches_the_frame_the_game_remapped_under_a_placed_buffer` (DxcFact, loader tests) compiles a program against `rdna_prelude.hlsli` and runs `PtrStore`/`PtrLoad` through the shim's map. A page is remapped under a slot-133 buffer and its old frame mapped into another range. Without the loader commit the load reads the old frame and the store is lost. - `dotnet build` has no warnings. `dotnet test`: 511 passed, 0 failed; the skipped tests need a dump or a full-wave GPU. I have no dump, so this hasn't run in a boot. Closes #4.
A buffer access through a V# with a game address took the view of the
base's 64 KiB page and added the offset inside it. That is right only while
the following pages belong to the same desktop buffer and keep the frames
it was placed over. The hardware translates every page, so an access past
the base's page reaches a neighbouring buffer, or the frames the game
remapped there. buf_view now takes the access offset, adds it to the base
in 64 bits and looks that address up; loads, stores, formatted accesses,
atomics and s_buffer_load use the view of their own address.

cs_root_descriptor now reads and writes across a page boundary into a
second buffer; it fails without the change.
The address map named, for each page, views over the desktop buffer placed
there by slot 133, so a page the game remapped afterwards (the streaming
pool, CopyPageMappingsBatchX) kept its old frame for every translated
program: reads saw stale data and writes changed whatever owned that frame
next. Every map, remap and unmap now points the page, where the GPU
addresses it (the GPU-only alias, else the CPU address), into raw views
over the section that holds its frame now, one buffer per 512 MiB window
of the section; an unmapped page names the null views. Pages mapped before
the view heap exists are entered when the map is made.

Section heaps are opened once for slot 133 and the map alike
(SectionHeaps), and XMem takes several page observers.

A DxcFact test runs the translator's PtrStore and PtrLoad through the map
after the game remaps a page under a placed buffer; without the change the
load reads the old frame and the store is lost.
Each access now looks up its own page, so lanes of one wave can reach
different views. Buffer loads, stores, formatted accesses and atomics
through a V#, and pointer loads, stores and atomics, index the heap with
NonUniformResourceIndex, without which a driver may use one lane's
descriptor for the whole wave.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:13:29 +00:00
github manually merged commit 052407da27 into gh-pr/8/base 2026-11-27 03:13:30 +00:00
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-24 18:52 UTC · original

Thanks for this one, it's the proper fix for #4 and I like that each access now looks up its own page like the hardware does. It merges cleanly onto main, builds without warnings and everything passes here too.

One thing before we merge. The heap index in buf_view (and in gpu_rw for the pointer paths) can now differ between lanes of a wave, whenever lanes land in different views. HLSL needs that marked with NonUniformResourceIndex, otherwise NVIDIA and AMD are free to use one lane's descriptor for the whole wave. WARP won't show it. The pointer path already had this gap before your change, so it isn't new, but now it covers every buffer access. Could you wrap those indices? If you'd rather not, I'm happy to add it on top.

I'll also run a boot with it on our side, since you don't have a dump. For what it's worth, the [gpu pages] check from #5 hasn't seen a remap under a live buffer in any boot so far, so this is mostly about being correct rather than fixing a crash we see right now.

**@⁠ThatRecompDev** on GitHub · 2026-11-24 18:52 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/8#issuecomment-7141268127) Thanks for this one, it's the proper fix for #4 and I like that each access now looks up its own page like the hardware does. It merges cleanly onto main, builds without warnings and everything passes here too. One thing before we merge. The heap index in `buf_view` (and in `gpu_rw` for the pointer paths) can now differ between lanes of a wave, whenever lanes land in different views. HLSL needs that marked with `NonUniformResourceIndex`, otherwise NVIDIA and AMD are free to use one lane's descriptor for the whole wave. WARP won't show it. The pointer path already had this gap before your change, so it isn't new, but now it covers every buffer access. Could you wrap those indices? If you'd rather not, I'm happy to add it on top. I'll also run a boot with it on our side, since you don't have a dump. For what it's worth, the `[gpu pages]` check from #5 hasn't seen a remap under a live buffer in any boot so far, so this is mostly about being correct rather than fixing a crash we see right now.
Author
Owner

@⁠marquezinii on GitHub · 2026-11-24 19:05 UTC · original

Done in 894035d: every heap access through a view the map hands out is now NonUniformResourceIndex. That covers BufLoad, BufLoadRW, BufStore, FmtLoad, FmtStore, PtrLoad, PtrStore, and the buffer and global atomics in the emitter (the gpu_rw path). The DXIL backend already lowers the intrinsic, and the tests pass with both it and dxc.

I also ran cs_root_descriptor on an RTX 5070 instead of WARP, both with and without the marking. It passes both ways there, so this NVIDIA driver didn't show the problem. The marking is what the spec requires, though, and other drivers are free to differ.

Thanks for running a boot with it.

**@⁠marquezinii** on GitHub · 2026-11-24 19:05 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/8#issuecomment-7141586845) Done in 894035d: every heap access through a view the map hands out is now `NonUniformResourceIndex`. That covers `BufLoad`, `BufLoadRW`, `BufStore`, `FmtLoad`, `FmtStore`, `PtrLoad`, `PtrStore`, and the buffer and global atomics in the emitter (the `gpu_rw` path). The DXIL backend already lowers the intrinsic, and the tests pass with both it and dxc. I also ran `cs_root_descriptor` on an RTX 5070 instead of WARP, both with and without the marking. It passes both ways there, so this NVIDIA driver didn't show the problem. The marking is what the spec requires, though, and other drivers are free to differ. Thanks for running a boot with it.
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-24 19:29 UTC · original

Thanks for adding the NonUniformResourceIndex markers, that's exactly what I meant.

Bad timing on our side though: main moved under you in the same spot. We just merged a change that opens sections bigger than 1 GiB as heaps through 1 GiB windows, plus read-only buffers over mapped pages that nothing covers, all in XboxPlacedResources (84e20e5). Your SectionHeaps does the same job with 512 MiB windows, so it'd be great to fold ours into yours rather than have two. Could you rebase onto current main?

One more reason to get this in: with that change, the [gpu pages] check now sees 500+ remaps under live buffers during the story load, so your fix matters for us right now. I'll boot it as soon as it's rebased.

**@⁠ThatRecompDev** on GitHub · 2026-11-24 19:29 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/8#issuecomment-7142172342) Thanks for adding the NonUniformResourceIndex markers, that's exactly what I meant. Bad timing on our side though: main moved under you in the same spot. We just merged a change that opens sections bigger than 1 GiB as heaps through 1 GiB windows, plus read-only buffers over mapped pages that nothing covers, all in `XboxPlacedResources` (84e20e5). Your `SectionHeaps` does the same job with 512 MiB windows, so it'd be great to fold ours into yours rather than have two. Could you rebase onto current main? One more reason to get this in: with that change, the `[gpu pages]` check now sees 500+ remaps under live buffers during the story load, so your fix matters for us right now. I'll boot it as soon as it's rebased.
Author
Owner

@⁠marquezinii on GitHub · 2026-11-24 19:38 UTC · original

Rebased onto current main, and your windows are folded in. SectionHeaps is now the one place that opens heaps over XMem sections:

  • A section up to 1 GiB is still one heap over the store's whole view.
  • A larger one takes a heap per 1 GiB window through its own MapViewOfFile3 view, as your WindowHeap did.

XboxPlacedResources asks it for the heap holding a run's offset and refuses a run that crosses the window end or sits off a 64 KiB boundary, so your two checks are kept. CoverMappedPages keeps your run limits, reading the window size from SectionHeaps.WindowBytes. The address map's section views go through the same heaps, and each view ends with its window.

That last part matters for the 6 GiB pool. My earlier version opened whole sections, so the remapped pages there would have read as zeros. I checked it with a local run of the DxcFact test that allocates a 17,000-page section, places a buffer over a frame in window 0, and remaps the page to a frame past 1 GiB: the program reads and writes the new frame. I left that variant out of the suite because it commits over 1 GiB.

dotnet test: 511 passed, 0 failed on WARP (Windows, RTX 5070 machine), no build warnings. Good to hear [gpu pages] sees the remaps; I'm curious what the boot shows.

**@⁠marquezinii** on GitHub · 2026-11-24 19:38 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/8#issuecomment-7142377322) Rebased onto current main, and your windows are folded in. `SectionHeaps` is now the one place that opens heaps over XMem sections: - A section up to 1 GiB is still one heap over the store's whole view. - A larger one takes a heap per 1 GiB window through its own `MapViewOfFile3` view, as your `WindowHeap` did. `XboxPlacedResources` asks it for the heap holding a run's offset and refuses a run that crosses the window end or sits off a 64 KiB boundary, so your two checks are kept. `CoverMappedPages` keeps your run limits, reading the window size from `SectionHeaps.WindowBytes`. The address map's section views go through the same heaps, and each view ends with its window. That last part matters for the 6 GiB pool. My earlier version opened whole sections, so the remapped pages there would have read as zeros. I checked it with a local run of the DxcFact test that allocates a 17,000-page section, places a buffer over a frame in window 0, and remaps the page to a frame past 1 GiB: the program reads and writes the new frame. I left that variant out of the suite because it commits over 1 GiB. `dotnet test`: 511 passed, 0 failed on WARP (Windows, RTX 5070 machine), no build warnings. Good to hear `[gpu pages]` sees the remaps; I'm curious what the boot shows.
Author
Owner

@⁠marquezinii on GitHub · 2026-11-24 19:39 UTC · original

Main moved again while I was posting (1ea5bce), so this is rebased once more. With CoverMappedPages gone, my note about its run limits no longer applies; the rest stands. Slot 133 and the address map share SectionHeaps with your 1 GiB windows, and 511 tests pass on the new main.

On the constant pools: with this PR, programs that read those pages through root descriptors, V#s or pointers follow the remaps via the address map. The CBV copy in the upload ring from 1ea5bce is separate and still needed, since desktop CBVs don't go through the map.

**@⁠marquezinii** on GitHub · 2026-11-24 19:39 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/8#issuecomment-7142412321) Main moved again while I was posting (1ea5bce), so this is rebased once more. With `CoverMappedPages` gone, my note about its run limits no longer applies; the rest stands. Slot 133 and the address map share `SectionHeaps` with your 1 GiB windows, and 511 tests pass on the new main. On the constant pools: with this PR, programs that read those pages through root descriptors, V#s or pointers follow the remaps via the address map. The CBV copy in the upload ring from 1ea5bce is separate and still needed, since desktop CBVs don't go through the map.
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-24 19:47 UTC · original

Merged, thanks a lot! Really nice work, and thanks for rebasing twice while main kept moving under you. This lands right when we need it, since the story load now remaps pages under live buffers.

**@⁠ThatRecompDev** on GitHub · 2026-11-24 19:47 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/8#issuecomment-7142588322) Merged, thanks a lot! Really nice work, and thanks for rebasing twice while main kept moving under you. This lands right when we need it, since the story load now remaps pages under live buffers.
This discussion has been locked. Commenting is limited to contributors.
No description provided.