AGC: on Windows (NVIDIA) a new host import's pages are marked written after ImportWatched's window, so write-backs drop GPU results #1434

Closed
opened 2026-10-08 06:18:03 +00:00 by popcoder565 · 5 comments
popcoder565 commented 2026-10-08 06:18:03 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T06:18:03Z.


Summary

On Windows with an NVIDIA GPU, agc_driver_dcc_keys_first_write fails in about one run in three on main (5850a6cd):

AGC graphics: the first write: the write-back stored 0 written and 0 cleared bytes of 65536, expected 128 written

It always fails in the second Run, on the write-watched block (0x200000000, from the guest arena). With APS5_TRACE_FLUSH=1, the first write-back of that surface keeps its only block for the CPU:

[flush] image 0x200000000+0x10000 keeps 1 CPU-written 64 KiB blocks (first stored byte at +0x10000, 0 ranges stored, ...)

Nothing on the CPU writes the texels between the upload and that write-back.

What happens

I added a print to collectWrites for walks over the block, and one to createImport. A failing run and a passing run then take the same steps in the same order:

walk 200000000..200010000 gen=2  dirty=1        the test's memset
walk 200000000..200010000 gen=3  dirty=0        upload: layerGeneration = 3
walk 200010000..200011000 gen=4  dirty=1        keys
import 200000000+20000                          CopyKeysCode imports the whole 128 KiB allocation
walk 200010000..200011000 gen=9  dirty=1 / 0
walk 200011000..200012000 gen=10 dirty=1 / 0
walk 200000000..200010000 gen=11 dirty=1 / 0    writeBackLayers' uncached collect

In the failing run, the walks after the import find both the texel and the key pages written (the first value). In the passing run they find nothing. The texel block is then stamped at 11, after its layer generation of 3, so ChangedBlocks reports it written and writeBackLayers keeps it for the CPU.

GuestMemory::ImportWatched already walks the range right after createImport and stamps what it finds as ImportWindow. Here the pages are marked written after that walk. As an experiment, not a fix, I added Sleep(50) after createImport inside the ImportWatched callback. In the same session the test then passed 30 of 30 runs, against 21 of 30 without it.

So something marks the imported pages written in the write watch shortly after vkAllocateMemory returns, outside the window. Most likely it is the driver pinning them; the driver here is 616.92. On Windows, decideImportWatch keeps imports watched without a probe: ProbeImportWriteProtection returns "the Linux write watch".

Impact

This can happen to any write-watched range that is imported on Windows after a storage image over it was uploaded and before that image's write-back. The image's GPU results are lost the same way: the write-back keeps the guest bytes, and the image is uploaded again from them at its next use. In a title this would look like GPU-written textures falling back to older contents.

Possible directions

  • Settle a new import before ImportWatched's second walk, for example with a GPU read of it (submit and wait). The import sites hold only the host import mutex, and ImportWatched holds the tracker lock, so this needs care with the GPU lock.
  • A Windows variant of ProbeImportWriteProtection on a scratch arena range that looks for pages marked written some time after the import, and compares imported ranges (unwatchImports) when it finds them. The timing varies between runs, so a single probe could miss it.
  • Compare imported ranges on Windows by default.

Windows 10 (19045), NVIDIA GeForce RTX 3060 Laptop GPU (driver 616.92), Intel Core i7-12700H, MinGW-w64 GCC 15.2.0, Release.

Investigated with AI assistance.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5756940360 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1434). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-08T06:18:03Z. --- ### Summary On Windows with an NVIDIA GPU, `agc_driver_dcc_keys_first_write` fails in about one run in three on `main` (5850a6cd): ``` AGC graphics: the first write: the write-back stored 0 written and 0 cleared bytes of 65536, expected 128 written ``` It always fails in the second `Run`, on the write-watched block (0x200000000, from the guest arena). With `APS5_TRACE_FLUSH=1`, the first write-back of that surface keeps its only block for the CPU: ``` [flush] image 0x200000000+0x10000 keeps 1 CPU-written 64 KiB blocks (first stored byte at +0x10000, 0 ranges stored, ...) ``` Nothing on the CPU writes the texels between the upload and that write-back. ### What happens I added a print to `collectWrites` for walks over the block, and one to `createImport`. A failing run and a passing run then take the same steps in the same order: ``` walk 200000000..200010000 gen=2 dirty=1 the test's memset walk 200000000..200010000 gen=3 dirty=0 upload: layerGeneration = 3 walk 200010000..200011000 gen=4 dirty=1 keys import 200000000+20000 CopyKeysCode imports the whole 128 KiB allocation walk 200010000..200011000 gen=9 dirty=1 / 0 walk 200011000..200012000 gen=10 dirty=1 / 0 walk 200000000..200010000 gen=11 dirty=1 / 0 writeBackLayers' uncached collect ``` In the failing run, the walks after the import find both the texel and the key pages written (the first value). In the passing run they find nothing. The texel block is then stamped at 11, after its layer generation of 3, so `ChangedBlocks` reports it written and `writeBackLayers` keeps it for the CPU. `GuestMemory::ImportWatched` already walks the range right after `createImport` and stamps what it finds as `ImportWindow`. Here the pages are marked written after that walk. As an experiment, not a fix, I added `Sleep(50)` after `createImport` inside the `ImportWatched` callback. In the same session the test then passed 30 of 30 runs, against 21 of 30 without it. So something marks the imported pages written in the write watch shortly after `vkAllocateMemory` returns, outside the window. Most likely it is the driver pinning them; the driver here is 616.92. On Windows, `decideImportWatch` keeps imports watched without a probe: `ProbeImportWriteProtection` returns "the Linux write watch". ### Impact This can happen to any write-watched range that is imported on Windows after a storage image over it was uploaded and before that image's write-back. The image's GPU results are lost the same way: the write-back keeps the guest bytes, and the image is uploaded again from them at its next use. In a title this would look like GPU-written textures falling back to older contents. ### Possible directions - Settle a new import before `ImportWatched`'s second walk, for example with a GPU read of it (submit and wait). The import sites hold only the host import mutex, and `ImportWatched` holds the tracker lock, so this needs care with the GPU lock. - A Windows variant of `ProbeImportWriteProtection` on a scratch arena range that looks for pages marked written some time after the import, and compares imported ranges (`unwatchImports`) when it finds them. The timing varies between runs, so a single probe could miss it. - Compare imported ranges on Windows by default. Windows 10 (19045), NVIDIA GeForce RTX 3060 Laptop GPU (driver 616.92), Intel Core i7-12700H, MinGW-w64 GCC 15.2.0, Release. Investigated with AI assistance. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
popcoder565 commented 2026-10-08 06:35:45 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T06:35:45Z.


Two more findings.

The new agc_driver_srgb8_color_target (94cf99e3) fails the same way, and more often. On main 7a178e22 it fails in 7 of 10 runs here, with messages such as a draw over a fast-cleared 8_SRGB target: byte 0 inside the scissor is 0, expected 200 from 93. APS5_TRACE_FLUSH=1 shows keeps 2 CPU-written 64 KiB blocks (... 0 ranges stored ...). With the 50 ms sleep inside ImportWatched's callback it passed 20 of 20 runs, against 3 of 20 without it.

The driver marks every imported page twice. I polled GuestArenaCollectWrites without resetting it:

  • Right after createImport returns, all 32 of the import's 32 pages already read as written. The window's ImportWindow walk absorbs these, as intended.
  • About 1.2-2.0 ms after that walk, all 32 of 32 read as written again. The test thread was still inside ImportWatched then, so no GPU work had been submitted.
  • For some imports the second marking had not arrived after 150 ms of polling.

That second, whole-range marking is what the next collect stamps as a CPU write.

A contained fix, Windows only, in GuestMemory.cpp, which I can send as a PR unless you'd rather go another way:

  • ImportWatched remembers the imported range as awaiting its pin.
  • Before a walk overlaps such a range, the range is probed without a reset. When every page of it reads as written, the whole range is walked with StampKind::ImportWindow and forgotten. Those blocks then read as maybe-written, as the import window's own stamps do, so writeBackLayers stores the GPU results.
  • When only some of its pages read as written, it is an ordinary write and is stamped as one.

The risk is the one the import window already takes: a CPU store to every page of a freshly imported range, before the driver's second marking, would be taken for that marking.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6053984398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1434#issuecomment-6053984398). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-08T06:35:45Z. --- Two more findings. **The new `agc_driver_srgb8_color_target` (94cf99e3) fails the same way, and more often.** On `main` 7a178e22 it fails in 7 of 10 runs here, with messages such as `a draw over a fast-cleared 8_SRGB target: byte 0 inside the scissor is 0, expected 200 from 93`. `APS5_TRACE_FLUSH=1` shows `keeps 2 CPU-written 64 KiB blocks (... 0 ranges stored ...)`. With the 50 ms sleep inside `ImportWatched`'s callback it passed 20 of 20 runs, against 3 of 20 without it. **The driver marks every imported page twice.** I polled `GuestArenaCollectWrites` without resetting it: - Right after `createImport` returns, all 32 of the import's 32 pages already read as written. The window's `ImportWindow` walk absorbs these, as intended. - About 1.2-2.0 ms after that walk, all 32 of 32 read as written again. The test thread was still inside `ImportWatched` then, so no GPU work had been submitted. - For some imports the second marking had not arrived after 150 ms of polling. That second, whole-range marking is what the next collect stamps as a CPU write. A contained fix, Windows only, in `GuestMemory.cpp`, which I can send as a PR unless you'd rather go another way: - `ImportWatched` remembers the imported range as awaiting its pin. - Before a walk overlaps such a range, the range is probed without a reset. When every page of it reads as written, the whole range is walked with `StampKind::ImportWindow` and forgotten. Those blocks then read as maybe-written, as the import window's own stamps do, so `writeBackLayers` stores the GPU results. - When only some of its pages read as written, it is an ordinary write and is stamped as one. The risk is the one the import window already takes: a CPU store to every page of a freshly imported range, before the driver's second marking, would be taken for that marking.
fisherjoey commented 2026-10-08 16:37:05 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T16:37:05Z.


Linux NVIDIA data for scoping the fix to Windows.

RTX 5070 Ti, NVIDIA 595.91.07, Nobara 44 (Linux 7.2), GCC 16.2.1, Release. agc_driver_dcc_keys_first_write passed in 13 of 13 ctest -R agc_driver -j1 runs, each on main df16c4c2 + #1329 + a different PR under test (none of them touch the write watch). At one failure in three, 13 passes in a row would happen about 0.5% of the time. agc_driver_srgb8_color_target only ran once here and passed.

On this driver the startup probe reads no (0 of 4 scratch pages written after a GPU read, 4 after the import); imported ranges stay watched, so Linux NVIDIA also marks the pages at the import, and ImportWatched's window absorbs that. I haven't polled for a second marking after the window the way you did, but the test results suggest it doesn't happen on Linux, which fits a Windows-only fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6064545991 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1434#issuecomment-6064545991). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-08T16:37:05Z. --- Linux NVIDIA data for scoping the fix to Windows. RTX 5070 Ti, NVIDIA 595.91.07, Nobara 44 (Linux 7.2), GCC 16.2.1, Release. `agc_driver_dcc_keys_first_write` passed in 13 of 13 `ctest -R agc_driver -j1` runs, each on `main` df16c4c2 + #1329 + a different PR under test (none of them touch the write watch). At one failure in three, 13 passes in a row would happen about 0.5% of the time. `agc_driver_srgb8_color_target` only ran once here and passed. On this driver the startup probe reads `no (0 of 4 scratch pages written after a GPU read, 4 after the import); imported ranges stay watched`, so Linux NVIDIA also marks the pages at the import, and `ImportWatched`'s window absorbs that. I haven't polled for a second marking after the window the way you did, but the test results suggest it doesn't happen on Linux, which fits a Windows-only fix.
JAB-dev commented 2026-10-08 21:46:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by JAB-dev on 2026-10-08T21:46:09Z.


#1847 replaces the always-compare approach from #1441, which drops PPSA22520 from about 59 to 7.5 FPS on Windows. Imports stay watched, and a page marked in the second after an import only counts as a CPU store if its bytes changed.

On Windows, agc_driver_dcc_keys_first_write now reproduces this issue on every driver: it rewrites the imported block with its own bytes between the import and the write-back. It fails in every run with plain watching (APS5_WRITE_WATCH_IMPORTS=watch) and passes with #1847.

My driver (610.88) doesn't show the late marking by itself. @popcoder565, could you run #1847 on 616.92, for example agc_driver_dcc_keys_first_write and agc_driver_srgb8_color_target a few times each? That would confirm it against the real late marks.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069662359 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1434#issuecomment-6069662359). Originally posted by [JAB-dev](https://github.com/JAB-dev) on 2026-10-08T21:46:09Z. --- #1847 replaces the always-compare approach from #1441, which drops PPSA22520 from about 59 to 7.5 FPS on Windows. Imports stay watched, and a page marked in the second after an import only counts as a CPU store if its bytes changed. On Windows, `agc_driver_dcc_keys_first_write` now reproduces this issue on every driver: it rewrites the imported block with its own bytes between the import and the write-back. It fails in every run with plain watching (`APS5_WRITE_WATCH_IMPORTS=watch`) and passes with #1847. My driver (610.88) doesn't show the late marking by itself. @popcoder565, could you run #1847 on 616.92, for example `agc_driver_dcc_keys_first_write` and `agc_driver_srgb8_color_target` a few times each? That would confirm it against the real late marks.
popcoder565 commented 2026-10-09 08:22:27 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T08:22:27Z.


@JAB-dev ran #1847 on 616.92: both tests pass 30/30 by default, and with plain watching srgb8_color_target still fails 11 of 20 runs from the driver's own late marks. Details: https://github.com/boykopovar/AnyPS5/pull/1847#issuecomment-6077240672

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077240933 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1434#issuecomment-6077240933). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T08:22:27Z. --- @JAB-dev ran #1847 on 616.92: both tests pass 30/30 by default, and with plain watching `srgb8_color_target` still fails 11 of 20 runs from the driver's own late marks. Details: https://github.com/boykopovar/AnyPS5/pull/1847#issuecomment-6077240672 _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
popcoder565 commented 2026-10-09 15:49:47 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T15:49:47Z.


Status on main d70b8998, after #1441 and #2096, on the same machine (Windows 10 19045, RTX 3060 Laptop, NVIDIA 616.92):

  • agc_driver_dcc_keys_first_write: 30 of 30
  • agc_driver_srgb8_color_target: 30 of 30

Direct host imports on Windows are now compared rather than watched, and #2096 keeps write tracking on the separate shared aliases. So the driver's late marks no longer drop GPU results here. #1847 was closed for the same reason.

Closing. I'll reopen if it shows up again.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084326059 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1434#issuecomment-6084326059). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T15:49:47Z. --- Status on `main` d70b8998, after #1441 and #2096, on the same machine (Windows 10 19045, RTX 3060 Laptop, NVIDIA 616.92): - `agc_driver_dcc_keys_first_write`: 30 of 30 - `agc_driver_srgb8_color_target`: 30 of 30 Direct host imports on Windows are now compared rather than watched, and #2096 keeps write tracking on the separate shared aliases. So the driver's late marks no longer drop GPU results here. #1847 was closed for the same reason. Closing. I'll reopen if it shows up again. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
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
boykopovar/AnyPS5#1434
No description provided.