perf: Demon's Souls (PPSA01342) sends almost every indirect draw to the CPU path (patched SGPR not folded) #5806

Open
opened 2026-10-14 11:49:58 +00:00 by btsouth · 6 comments
btsouth commented 2026-10-14 11:49:58 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by btsouth on 2026-10-14T11:49:58Z.


Note

Posted by an AI agent on behalf of Tyler.

Demon's Souls (PPSA01342, v1.005 US) runs its first level at about 1 FPS here, and the driver's queue thread is the limit. Almost every indirect draw takes the CPU path with "patched SGPR not folded". Before I work on a change I'd like to know which fix you would accept.

What the draws look like

With indirect tracing on, nearly every indirect draw in the level looks like this:

[draw] indirect 0x25 args 0x... stride 20 count 1 locs base=none inst=0x8e(s10) idx=none analyzer v=-1 i=-1 shared=0/0 conflict=0/0 indx=0 -> cpu (patched SGPR not folded)

Three traced level runs together logged 32,927 of these for opcode 0x25, 5,905 for 0x24 and 1,208 for 0x38 with count 3, against 337 indirect draws that went GPU side. In a 10 s window of the level, the draw summary reports 21 GPU side and 35,562 CPU side, all "patched SGPR not folded".

The packet patches the start instance into s10, but the analyzer finds no instance offset in the vertex shader (i=-1), so ClassifyIndirectDraw fails with NotFolded. ClassifyIndirectDraw on c0a272537 makes the same decision for this case.

Cost

In the level (Outpost Passage) each frame has about 6,300 draws and 2,600 dispatches. With APS5_FRAME_SUMMARY and APS5_PROFILE_GPU on, the queue thread spends 0.85 to 1.0 s of CPU per frame, 80 to 120 us per draw, while the GPU is busy about 120 ms per frame. Before some local caching changes it was about 1.5 s per frame.

Question

Would you take a recompiler change that reads the patched SGPR as gl_BaseInstance, so these draws stay on the GPU with firstInstance applied in place? Or would you rather handle it differently?

Setup

A branch on bab7b3285 with Demon's Souls bring-up changes (#4049, #757, #758 and some local fixes), Release, GCC 16.2.1. Arch Linux 7.2.8, i7-14700F, RTX 4070 SUPER on 615.78.08, 31 GiB RAM. Main from 10-13 (665ad7da7) ran out of memory under an 18 GiB cap while preparing shaders at registration during boot, so I haven't measured on main yet, and the base is too old for APS5_TRACE_FRAMES.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5847238477 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5806). Originally posted by [btsouth](https://github.com/btsouth) on 2026-10-14T11:49:58Z. --- > [!NOTE] > Posted by an AI agent on behalf of Tyler. Demon's Souls (PPSA01342, v1.005 US) runs its first level at about 1 FPS here, and the driver's queue thread is the limit. Almost every indirect draw takes the CPU path with "patched SGPR not folded". Before I work on a change I'd like to know which fix you would accept. **What the draws look like** With indirect tracing on, nearly every indirect draw in the level looks like this: ``` [draw] indirect 0x25 args 0x... stride 20 count 1 locs base=none inst=0x8e(s10) idx=none analyzer v=-1 i=-1 shared=0/0 conflict=0/0 indx=0 -> cpu (patched SGPR not folded) ``` Three traced level runs together logged 32,927 of these for opcode 0x25, 5,905 for 0x24 and 1,208 for 0x38 with count 3, against 337 indirect draws that went GPU side. In a 10 s window of the level, the draw summary reports 21 GPU side and 35,562 CPU side, all "patched SGPR not folded". The packet patches the start instance into s10, but the analyzer finds no instance offset in the vertex shader (i=-1), so ClassifyIndirectDraw fails with NotFolded. ClassifyIndirectDraw on c0a272537 makes the same decision for this case. **Cost** In the level (Outpost Passage) each frame has about 6,300 draws and 2,600 dispatches. With APS5_FRAME_SUMMARY and APS5_PROFILE_GPU on, the queue thread spends 0.85 to 1.0 s of CPU per frame, 80 to 120 us per draw, while the GPU is busy about 120 ms per frame. Before some local caching changes it was about 1.5 s per frame. **Question** Would you take a recompiler change that reads the patched SGPR as `gl_BaseInstance`, so these draws stay on the GPU with firstInstance applied in place? Or would you rather handle it differently? **Setup** A branch on bab7b3285 with Demon's Souls bring-up changes (#4049, #757, #758 and some local fixes), Release, GCC 16.2.1. Arch Linux 7.2.8, i7-14700F, RTX 4070 SUPER on 615.78.08, 31 GiB RAM. Main from 10-13 (665ad7da7) ran out of memory under an 18 GiB cap while preparing shaders at registration during boot, so I haven't measured on main yet, and the base is too old for APS5_TRACE_FRAMES.
MarcZX10 commented 2026-10-14 12:08:30 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T12:08:30Z.


I traced this against current main (344afad05). The CPU fallback is deliberate when an offset SGPR is shared or the recompiler cannot prove that it is used only by the vertex fetch: ClassifyIndirectDraw accepts the GPU path only when the indirect packet's SGPR matches the recompiler's folded SGPR and the result is not marked shared/conflicting. For this trace, i=-1 means the recompiler has not identified an instance-fetch offset to fold, so mapping the packet-patched s10 to gl_BaseInstance would need evidence that every use of that SGPR is equivalent to Vulkan's instance index. Otherwise it would change guest shader reads as well as fetch addressing.

The trace does identify the immediate branch point: the packet names s10, but result.instanceOffsetSgpr is -1. A small shader capture showing how s10 reaches the fetch (and whether it has any other readers) would distinguish a missing analyzer pattern from a genuinely shared SGPR. Until then I would keep the CPU path for correctness; the proposed unconditional base-instance rewrite is not established by the current evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6167980817 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5806#issuecomment-6167980817). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T12:08:30Z. --- I traced this against current `main` (`344afad05`). The CPU fallback is deliberate when an offset SGPR is shared or the recompiler cannot prove that it is used only by the vertex fetch: `ClassifyIndirectDraw` accepts the GPU path only when the indirect packet's SGPR matches the recompiler's folded SGPR and the result is not marked shared/conflicting. For this trace, `i=-1` means the recompiler has not identified an instance-fetch offset to fold, so mapping the packet-patched `s10` to `gl_BaseInstance` would need evidence that every use of that SGPR is equivalent to Vulkan's instance index. Otherwise it would change guest shader reads as well as fetch addressing. The trace does identify the immediate branch point: the packet names `s10`, but `result.instanceOffsetSgpr` is `-1`. A small shader capture showing how `s10` reaches the fetch (and whether it has any other readers) would distinguish a missing analyzer pattern from a genuinely shared SGPR. Until then I would keep the CPU path for correctness; the proposed unconditional base-instance rewrite is not established by the current evidence.
Sam-mon7 commented 2026-10-14 14:29:20 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Sam-mon7 on 2026-10-14T14:29:20Z.


On measuring this on main: #5872 should fix the boot out-of-memory you hit while shaders were prepared at registration.

  1. Apply #5872 and set APS5_LAZY_SHADER_PREPARE=1. Shaders then get prepared on first use instead of all at boot.
  2. On Windows, main first aborts on a 358 MiB address copy (an address-based build copies 358 MiB ... past 64 MiB). #5870 fixes that. If Linux doesn't hit it, you won't need it.

Demon's Souls (PPSA01341, EU), Windows 11, RTX 4070, 32 GB RAM, main 63fb0cd92:

Build Peak private Result
main + #5870 17.1 GB Out of RAM at frame 2
main + #5870 + #5872, opt-in on 12.2 GB Reaches the intro cutscene

That should get you a measurement on current main for the indirect-draw question.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6170734632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5806#issuecomment-6170734632). Originally posted by [Sam-mon7](https://github.com/Sam-mon7) on 2026-10-14T14:29:20Z. --- On measuring this on `main`: #5872 should fix the boot out-of-memory you hit while shaders were prepared at registration. 1. Apply #5872 and set `APS5_LAZY_SHADER_PREPARE=1`. Shaders then get prepared on first use instead of all at boot. 2. On Windows, `main` first aborts on a 358 MiB address copy (`an address-based build copies 358 MiB ... past 64 MiB`). #5870 fixes that. If Linux doesn't hit it, you won't need it. Demon's Souls (PPSA01341, EU), Windows 11, RTX 4070, 32 GB RAM, `main` `63fb0cd92`: | Build | Peak private | Result | |---|---|---| | `main` + #5870 | 17.1 GB | Out of RAM at frame 2 | | `main` + #5870 + #5872, opt-in on | 12.2 GB | Reaches the intro cutscene | That should get you a measurement on current `main` for the indirect-draw question.
pedroMartinsMJ commented 2026-10-14 14:46:44 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by pedroMartinsMJ on 2026-10-14T14:46:44Z.


I'm going to run a test, I'll change a few things in the code and see if the error disappears

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6171089571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5806#issuecomment-6171089571). Originally posted by [pedroMartinsMJ](https://github.com/pedroMartinsMJ) on 2026-10-14T14:46:44Z. --- I'm going to run a test, I'll change a few things in the code and see if the error disappears
EagleBearerYS commented 2026-10-14 17:56:15 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T17:56:15Z.


Resident Evil Village (PPSA01556, 01.210.000) shows the same pattern. Setup: Windows 11, RTX 3070 Laptop; main at 23be5a02b plus our Village stack: #5851, #5703, #5815, lazy shader preparation at first use (APS5_RE8_LAZY_PREPARE=1, APS5_NO_GPL=1) and a few local rendering experiments; APS5_PROFILE_DRAW=1. At the title screen, the [draws] line for one 10 s window:

[draws] 7220 draws over 10 s ... indirect: gpu-side 0 (0 rewritten), cpu-side 6373 ( patched SGPR not folded 3089 non-vertex path 3284), argument reads 407.7 ms
  • All of them on the CPU path. All 6,373 indirect draws of that window take it, out of 7,220 draws in all. About half are "patched SGPR not folded" and half "non-vertex path".
  • The cost. Reading their arguments takes about 0.41 s of every 10 s on the queue-0 worker. A Tracy capture of the same title shows that thread 100% busy, so the time comes straight off the frame rate.
  • Not traced yet: which SGPRs and stages these draws use.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174821217 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5806#issuecomment-6174821217). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-14T17:56:15Z. --- Resident Evil Village (PPSA01556, 01.210.000) shows the same pattern. Setup: Windows 11, RTX 3070 Laptop; `main` at 23be5a02b plus our Village stack: #5851, #5703, #5815, lazy shader preparation at first use (`APS5_RE8_LAZY_PREPARE=1`, `APS5_NO_GPL=1`) and a few local rendering experiments; `APS5_PROFILE_DRAW=1`. At the title screen, the `[draws]` line for one 10 s window: ``` [draws] 7220 draws over 10 s ... indirect: gpu-side 0 (0 rewritten), cpu-side 6373 ( patched SGPR not folded 3089 non-vertex path 3284), argument reads 407.7 ms ``` - **All of them on the CPU path.** All 6,373 indirect draws of that window take it, out of 7,220 draws in all. About half are "patched SGPR not folded" and half "non-vertex path". - **The cost.** Reading their arguments takes about 0.41 s of every 10 s on the queue-0 worker. A Tracy capture of the same title shows that thread 100% busy, so the time comes straight off the frame rate. - **Not traced yet:** which SGPRs and stages these draws use.
btsouth commented 2026-10-18 18:53:56 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by btsouth on 2026-10-18T18:53:56Z.


Note

Posted by an AI agent on behalf of Tyler.

Thanks for tracing it, and sorry for the slow reply. Agreed: the CPU path should stay unless the analyzer can show s10 only feeds the instance fetch, and I don't have a capture that shows that.

I'm also withdrawing the proposal for Demon's Souls. On my branch, once these CPU-path draws were cached, the indirect path turned out to be a small part of the cost: over 10 s in Outpost Passage, 57,307 draws took it and reading their arguments cost 38.4 ms in total. Most of the ~100 us per draw went to capture, lookup and Graphics::Draw, which every draw pays. Main doesn't have that caching, so argument reads may cost more there, as the RE Village numbers above (~0.41 s per 10 s) suggest. I'll leave the issue open for that case unless you'd rather close it.

These numbers are from a local branch on bab7b3285, not main. On 10-14, main with #5872 still went past the 18 GiB cap I run the game under during this level's first load.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253794047 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5806#issuecomment-6253794047). Originally posted by [btsouth](https://github.com/btsouth) on 2026-10-18T18:53:56Z. --- > [!NOTE] > Posted by an AI agent on behalf of Tyler. Thanks for tracing it, and sorry for the slow reply. Agreed: the CPU path should stay unless the analyzer can show `s10` only feeds the instance fetch, and I don't have a capture that shows that. I'm also withdrawing the proposal for Demon's Souls. On my branch, once these CPU-path draws were cached, the indirect path turned out to be a small part of the cost: over 10 s in Outpost Passage, 57,307 draws took it and reading their arguments cost 38.4 ms in total. Most of the ~100 us per draw went to capture, lookup and `Graphics::Draw`, which every draw pays. Main doesn't have that caching, so argument reads may cost more there, as the RE Village numbers above (~0.41 s per 10 s) suggest. I'll leave the issue open for that case unless you'd rather close it. These numbers are from a local branch on bab7b3285, not main. On 10-14, main with #5872 still went past the 18 GiB cap I run the game under during this level's first load.
ponhei9502 commented 2026-10-20 03:50:53 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by ponhei9502 on 2026-10-20T03:50:53Z.


We captured the Demon's Souls (PPSA01341 v1.07) draws that take the CPU path. All 138 vertex shaders behind them start with v_add_nc_u32_sdwa vD, v8, s10 src1_sel:WORD_0: vD = instance id + low16(patched start instance), and vD then indexes per-instance buffer_load_dwordx4 rows. s10 is overwritten by a s_load_dwordx16 right after, with no read or branch between, and v8 has no other reader.

Why the analyzer misses it (EmbeddedVertexFetch.cpp): it accepts only VAddI32/VSadU32, not VAddNcU32 (VOP2 0x25 SDWA); it requires the add to write v8 itself (DeS writes v4); and the shared/observed checks are not def-use aware.

Folding s10 as userdata would be wrong here: the start instance reaches 917,966 in our captures (197 of 8,000 records >= 65,536) while the shader uses only its low 16 bits. The exact variant is the BaseInstance builtin for the instance dimension (s10 := gl_BaseInstance, v8 := gl_InstanceIndex - gl_BaseInstance).

We can attach the 138 captured requests if useful.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6282934621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5806#issuecomment-6282934621). Originally posted by [ponhei9502](https://github.com/ponhei9502) on 2026-10-20T03:50:53Z. --- We captured the Demon's Souls (PPSA01341 v1.07) draws that take the CPU path. All 138 vertex shaders behind them start with `v_add_nc_u32_sdwa vD, v8, s10 src1_sel:WORD_0`: vD = instance id + low16(patched start instance), and vD then indexes per-instance `buffer_load_dwordx4` rows. s10 is overwritten by a `s_load_dwordx16` right after, with no read or branch between, and v8 has no other reader. Why the analyzer misses it (EmbeddedVertexFetch.cpp): it accepts only VAddI32/VSadU32, not VAddNcU32 (VOP2 0x25 SDWA); it requires the add to write v8 itself (DeS writes v4); and the shared/observed checks are not def-use aware. Folding s10 as userdata would be wrong here: the start instance reaches 917,966 in our captures (197 of 8,000 records >= 65,536) while the shader uses only its low 16 bits. The exact variant is the BaseInstance builtin for the instance dimension (s10 := gl_BaseInstance, v8 := gl_InstanceIndex - gl_BaseInstance). We can attach the 138 captured requests if useful.
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#5806
No description provided.