gpu: draw GPU-written indirect arguments without a readback #1288
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1288
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1288/head"
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?
@NeySlim on GitHub · 2026-10-09 07:40 UTC · original
NeySlim:perf/gpu-indirect-draw→main· +1347 −74 · 15 files · 9 commits · closed without mergingProblem
DRAW_INDIRECT,DRAW_INDEX_INDIRECTand their_MULTIforms read their arguments and draw count on the CPU inCommandProcessor::DrawIndirectMulti. When a compute shader wrote them, as GPU-driven culling does, the read faults and the command thread waits for the GPU to finish everything recorded so far.In God of War Ragnarök (PPSA08332, 05.005) about 3% of the indirect draws (about 28 per second) have GPU-written arguments. All of them are single indexed draws of a regular vertex shader, with no user SGPR destination for the offsets. The command thread blocks on them 3.0 ms per frame in the opening cutscene and 1.7 ms per frame in the first gameplay section (scheduler traces, blocked time attributed by stack).
Change
CommandProcessor::DrawIndirectOnGpupasses the packet toRenderExecutor::DrawIndirectinstead of reading it. Arguments that are not GPU-written stay on the CPU path, which reads them without a readback.gpu_indirect_draw_prepare.comp) reads the guest arguments and writes host draw commands with the parameters the CPU path emits: index count, first index, and a zero base vertex and first instance for native vertex fetches (kept for rewritten fetches, which consume them through Vulkan). The draw count is the guest count bounded by the packet maximum. The draws are recorded withvkCmdDrawIndexedIndirectCount/vkCmdDrawIndirectCount.minStorageBufferOffsetAlignmentand the pass adds the remainder.INDEX_BUFFER_SIZEread as zero, on both paths, as on the hardware: the index fetcher returns 0 past the max size and the draw keeps its count (RADV and PAL rely on this; Vulkan gives the same result for a sized index buffer binding withrobustBufferAccess2). The GPU path binds the index buffer fromINDEX_BASEwith a size ofINDEX_BUFFER_SIZEindices (vkCmdBindIndexBuffer2KHR) and lets Vulkan apply the start index inside it. The CPU path used to clamp the count toINDEX_BUFFER_SIZEwithout the start index; it now draws the full count with the same sized binding, or with a host copy whenVK_KHR_maintenance5orrobustBufferAccess2is missing (and for 8-bit indices, as before). The copy reads only the indices insideINDEX_BUFFER_SIZE; the zeros after them are written straight into the stream buffer. A copy holds at most a quarter of the stream buffer: a longer zero tail is drawn by further draws over the zeros of the same copy, split on whole primitives for lists, one instance at a time so that each instance runs completely before the next. Mesh pipelines (NGG emulation), which fetch indices in the shader, keep the full count and get the readable limit in their draw constants: the index load is inactive, and reads 0, past it. The GPU path is only used for indexed draws when both are available.NUM_INSTANCESstays in GPU memory: the last executed draw sets it, and a draw whose GPU count is 0 leaves it unchanged (starting from the CPU value when the CPU knows it). It is read back only when a later direct draw uses it; aNUM_INSTANCESpacket or a CPU-path indirect draw replaces it.NUM_INSTANCESon the GPU, and no draw. On the CPU path these draws read their arguments (and wait for the GPU when a shader wrote them) only to latchNUM_INSTANCESand skip the draw.DB_RENDER_CONTROL), whose clear would otherwise apply even when the GPU count is 0.drawIndirectCount,multiDrawIndirect,drawIndirectFirstInstanceandVK_KHR_maintenance5when the device has them.Limitations
INDEX_BUFFER_SIZEalone exceed a quarter of the stream buffer still stops with an error, as 8-bit indices did before.INDEX_BUFFER_SIZE0 (never set) still leaves the indices unbounded on the CPU path, as before; the GPU path is not used then.INDEX_BUFFER_SIZEbut past mapped memory read as zero on the GPU path.Test
New
GpuIndirectDrawchecks in the rasterization test, throughRenderExecutor::DrawIndirectwith GPU-written arguments, with the storage offset alignment raised to 64 bytes and arguments and counts at unaligned addresses:INDEX_BUFFER_SIZEof 3, followed in memory by a stale index: it must draw indices 1, 2, 0;NUM_INSTANCEScomes from the last counted draw; a draw with a GPU count of 0 keeps the previous draw's value, or the CPU value it starts from;NUM_INSTANCES;DrawIndexwith the CPU-path limit draws indices pastINDEX_BUFFER_SIZEas zero through the sized binding, the host copy, and 8-bit indices; also with a count of 0x1200000 and anINDEX_BUFFER_SIZEof 2 on the host copy (32-bit and 8-bit), more than the stream buffer holds (exits without the split). A host check runs the split draws of 3 instances over a copy and requires the indices of the draw in order for each instance in turn, read from the copy only, with list primitives kept whole.TestMeshInputAssembly(shader_cfg_tests) checks that the mesh prolog reads index 0 at and past the limit and loads the index below it. The test device enablesVK_KHR_maintenance5androbustBufferAccess2when available, as the emulator does.The checks fail when the GPU path is disabled or when the matching change is reverted. The alignment check cannot fail on RADV, whose alignment is 4 bytes; it fails when the pass ignores the offset remainder.
Validation
Linux, AMD Radeon 8060S (RDNA 3.5), Mesa RADV 26.2.3.
shader_recompiler_compute_tests, one case per process, after the review changes: same results as a clean main build (600 of 610 pass; the 10 others fail on main too on this setup).shader_cfg_tests,resource_tracking_testsandresource_materialization_testspass.Not tested on Windows. The features used are core Vulkan 1.2, plus
VK_KHR_maintenance5(core in 1.4) androbustBufferAccess2for indexed draws; without them the CPU path is used.AI use
I used Claude Code (AI agent) to find which indirect draws block the command thread, write the change and the test, and run the measurements and tests above on my machine. I reviewed the diff and the results before opening this PR.
@coderabbitai[bot] on GitHub · 2026-10-09 07:40 UTC · original · edited 2026-10-21 16:29 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
67e32692-e78f-4487-8711-9cef1a99390c📥 Commits
Reviewing files that changed from the base of the PR and between e9a0880b7fa401fb300075d9b1e2a431145771de and 83058637c98e64f74099be95f9d6475e2039cc69.
📒 Files selected for processing (5)
CMakeLists.txtsrc/graphics/guest_gpu/graphicsRun.cppsrc/graphics/host_gpu/renderer/render.hsrc/graphics/presentation/window/vulkanWindow.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.
📝 Walkthrough
The changes add GPU-side preparation and submission for eligible indirect draws, with CPU fallback for unsupported requests. They track indirect-draw instance counts and add bounded index reads for indexed draws. Vulkan capability checks control indirect-count drawing and sized index-buffer bindings. The changes also add compute-shader build integration and tests for GPU and CPU draw paths.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
83058The reviewed draw paths have no identified issue that needs resolution before merge.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 12.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 50 functions across 11 files. (2 skipped: 1 unsupported, 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@NeySlim on GitHub · 2026-10-09 15:08 UTC · original
Rebased on main and bounded the host index copy of the CPU path (no
VK_KHR_maintenance5/robustBufferAccess2, or 8-bit indices):INDEX_BUFFER_SIZEare read and copied; the zero tail is written straight into the stream buffer, so no count-sized vector is built.INDEX_BUFFER_SIZEof 2 on the forced host copy (32-bit and 8-bit); before, the 32-bit one exited on the stream buffer. A host check runs the split draws over a copy and requires every index of the draw in order, read from the copy only. Both fail when the split or its chunk draws are removed.GPU suite one case per process: same 10 failures as a clean main build (610 cases). resource_* pass.
@coderabbitai[bot] on GitHub · 2026-10-09 15:17 UTC · original · edited 2026-10-09 15:40 UTC
On
src/graphics/host_gpu/renderer/renderDraw.cppline 1091 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 16804
Preserve instance-major ordering for split indexed draws.
When
draw.instance_count > 1, the current callback submits every zero-tail segment with the full instance count. This makes execution segment-major instead of instance-major. Overlapping primitives can therefore produce different results with blending or shared-target shader writes.Emit one instance per segment in an outer instance loop. Keep primitive-ID preservation as a separate correction.
Suggested fix
📝 Committable suggestion
✅ Confirmed as addressed by @NeySlim
@coderabbitai[bot] on GitHub · 2026-10-09 15:17 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
e7745353-f0ea-495f-a750-b48ba0526705📥 Commits
Reviewing files that changed from the base of the PR and between afbb50a37b710d5469042db7bb49808115774206 and eb2347e40762aef80192bb498aa034921eeaa933.
📒 Files selected for processing (3)
src/graphics/host_gpu/renderer/render.hsrc/graphics/host_gpu/renderer/renderDraw.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@NeySlim on GitHub · 2026-10-09 15:35 UTC · original
On
src/graphics/host_gpu/renderer/renderDraw.cppline 1091 (outdated):Fixed in 38a0df57. When the zero tail is split,
ZeroTailDraws::ForEachDrawnow loops over the instances outside the segments: each segment is drawn with one instance andfirst_instance + i, so each instance runs all its indices before the next one, as a single instanced draw does.gl_InstanceIndexincludes the base instance, so its values do not change. Unsplit draws still use one draw with all instances; only the split path (a host copy longer than a quarter of the stream buffer) pays for the extra draws. Quad lists on the split path follow the same order.The
ZeroTailDrawshost check now uses 3 instances and requires the (instance, index) sequence of one instanced draw; it fails with the previous segment-major order.Drawing the zero tail as one non-indexed draw would not be exact: its vertex IDs count up from the first vertex instead of all being index 0 plus the base vertex. So the split over the zeros of the copy stays. Primitive IDs still restart at each split draw, as listed in the PR limitations.
@NeySlim on GitHub · 2026-10-09 15:35 UTC · original
Mesh indexed draws past
INDEX_BUFFER_SIZE(CodeRabbit comment outside the diff,renderDraw.cpp1118-1119): fixed in c7037bfe. A mesh draw keeps the full index count for its workgroup count and push constant dword 0. The readable index limit goes in a new dword 6 (UINT32_MAXfor non-indexed draws), and the mesh prolog makes the index load inactive, which reads 0, when the input vertex is at or past it.TestMeshInputAssemblyinshader_cfg_testschecks a list lane at the limit (index 0), the same lane with the limit one higher (loaded), and a fan center with a limit of 0. It fails when the prolog ignores the limit.@coderabbitai[bot] on GitHub · 2026-10-09 15:40 UTC · original
On
src/graphics/host_gpu/renderer/renderDraw.cppline 1091 (outdated):🧩 Analysis chain
🏁 Script executed:
Length of output: 10565
@NeySlim, verified by code inspection.ZeroTailDraws::ForEachDrawnow processes all segments for one instance before the next. Both indexed emission paths, including quad lists, applyemit.first_instance + instance. Unsplit draws retain one draw with all instances.The host check uses three instances and checks the complete
(instance, index)sequence. I did not run the tests.Thanks for clarifying why a non-indexed zero-tail draw is not equivalent. Primitive-ID resets remain a separate limitation.
✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@NeySlim on GitHub · 2026-10-11 08:35 UTC · original
Rebased on main after the depth to color copy (
1029511b). Conflicts in graphicContext.h and renderDraw.cpp: both sides kept.CopyDepthToColorchecks the draw counts on the CPU, so indirect draws withCOPY_DEPTH_TO_COLORnow stay on the CPU path (new check in the GpuIndirectDraw test; without the gate it hits theEXIT_NOT_IMPLEMENTEDinCopyDepthToColor).Linux, Radeon 8060S (RADV): GPU suite one case per process and ctest match a clean main build.
@NeySlim on GitHub · 2026-10-13 20:53 UTC · original
Added 90df3b50: draws the CPU path does not draw (patches with tessellation off, line loops, adjacency, quad strips) now only latch
NUM_INSTANCESon the GPU, without reading the arguments back. In God of War that was a 6 ms per frame wait in the opening cutscene, 20.3 -> 21.7 fps. Description updated.@fozter on GitHub · 2026-10-19 01:30 UTC · original · edited 2026-10-19 07:23 UTC
Tested #1288 with NHL 27 (PPSA34063, 01.003.001) on Windows 11, NVIDIA GeForce RTX 3060 Ti (8 GB), driver 617.42.
Base: main
099de8deplus local patches that are not on main (among them #1271, #1032 with --tessellation, #1211, #1679, and a few NHL-specific fixes from #1101), so this is not a clean-main result.Tests: shader_cfg_tests and shader_recompiler_compute_tests (including the new GpuIndirectDraw checks) pass on this NVIDIA setup.
In game: with the GPU path active, black triangle fans appear on the ice during gameplay. They all start at center ice (the world origin) and stretch towards players, referees and the benches, as if some vertices of each triangle land at (0, 0, 0). They come and go from frame to frame (screenshots attached). One close-up of a player also showed a glowing, blown-out beard (screenshot attached).
Bisect: I added a local switch that returns false at the top of CommandProcessor::DrawIndirectOnGpu, which keeps every indirect draw on the CPU path but leaves the rest of the PR (including the INDEX_BUFFER_SIZE handling on the CPU path) in place. With the switch set, the fans are gone; without it, they are back. So it is the GPU path itself, not the index-size change.
Counts (counters at the exits of DrawIndirectOnGpu, about 3 minutes from boot into gameplay):
GPU path: 2924 draws
CPU path:
user-SGPR offsets: 0
arguments not GPU-written: 3512
RenderExecutor::DrawIndirect declined: 1756 (tessellation is enabled)
So none of NHL 27's indirect draws name user-SGPR destinations for the offsets, and about 2900 take the GPU path. I have not identified which pipelines the broken ones are yet. If it would help, I can capture a frame with RenderDoc and look at the base vertex / first instance the prepare pass writes for those draws, compared with the CPU path.
Written with AI assistance (Claude Code); the runs and observations are from my machine.


Edit: forgot to add the screenshot of the festive glowing beard i mentioned

@NeySlim on GitHub · 2026-10-19 02:59 UTC · original
I couldn't reproduce this here: for the same arguments the GPU path records the same draw parameters as the CPU path (a local test comparing both paths on a counted multi-draw with a 32-byte stride, 16-bit indices, a start index, base vertex and first instance gives identical images). What it does change is that the whole packet becomes one
vkCmdDrawIndexedIndirectCount, the index buffer is bound over the full INDEX_BUFFER_SIZE instead of the drawn range, and the CPU no longer waits for the GPU at these draws. Could you check whether the broken draws have a count above 1, whether the fans go away if you callGetScheduler().FlushAndWait()at the top ofDrawIndirectOnGpu(draws still on the GPU path), and whether the log has anyclamped buffer rangelines during gameplay? A RenderDoc capture of one bad draw (the prepared commands and the index buffer binding size) would also help.@fozter on GitHub · 2026-10-19 07:38 UTC · original
Thanks for looking into it. Answers from 4 more runs with the GPU path on (same setup otherwise):
I will try a RenderDoc capture of a bad frame (the prepared commands and the index buffer binding size) once it reproduces again.
@NeySlim on GitHub · 2026-10-19 11:44 UTC · original
Thanks, that narrows it down a lot. I went through the ordering for exactly that case (non-indexed, count 1, stride 16, arguments written by a compute dispatch) and added a local test that rewrites the arguments with a dispatch before each of several draws with no CPU wait in between: every draw reads its own arguments here, and the prepare pass sits between full memory barriers on the same queue, so I still can't see where it would read stale ones. If you catch it in RenderDoc, the useful part is the
Kyty.IndirectDrawCommandsbuffer at the offset of the badvkCmdDrawIndirectCount(draw count, vertex count, instance count, first vertex, first instance), compared with the 16 guest argument bytes in the game buffer the prepare dispatch just before it reads, plus the vertex buffers bound for that draw and their sizes. If the vertex count there looks sane, the zeros probably come from the vertex data rather than the arguments.@NeySlim on GitHub · 2026-10-21 16:19 UTC · original
Rebased on main and added
d7cbd4984: tessellated patch draws (LS, HS, TES) with GPU-written arguments now take the GPU path too, instead of staying on the CPU path.With
--tessellation, these were the only indirect draws still read back in the GoW opening cutscene: about 18 per second, each blocking the command thread for about 7 ms (it waits for all submitted GPU work). With the change there are none left there and the cutscene goes from about 18.5 to 19.6-19.8 fps here (Radeon 8060S, RADV 26.2.5, with my other open PRs). Sled section unchanged, rendering identical.New
GpuIndirectDrawcheck: an indexed and a non-indexed tessellated patch draw with GPU-written arguments, drawn without a readback, with the HS patch output reaching the target. It fails on the old code (the draw falls back to the CPU path) and when the GPU path records no draw. The old check that tessellated patches stay on the CPU path is gone.@NeySlim on GitHub · 2026-10-23 17:12 UTC · original
Closing this one.
c513a4a13,949de19bband86fed10b8cover what it was for: in GoW every indirect draw now takes the GPU path on main, with no readback left, and a rebased port of this branch measured the same or slightly slower.Two things from here still differ on main. Indices past INDEX_BUFFER_SIZE are read from memory instead of returning 0, and offsets without an SGPR destination still apply vertexOffset/firstInstance, which the hardware ignores. I'll send the index range as its own small PR.
Pull request closed