perf(shader): read a read-only buffer's base misalignment at run time #27812
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27812"
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 boykopovar/AnyPS5. Originally posted by DerKarussellbremser on 2026-11-27T13:36:51Z.
What
A buffer V# whose base is not on a DWORD boundary is bound from the DWORD below, and every access shifts by the base's low two bits. Those two bits are a specialization constant of the module (
BufferBase + index * BufferWords + 3). A program is therefore specialized, and its pipeline compiled by the driver, once per combination of base alignments of its buffers.Horizon Zero Dawn Remastered (PPSA13428, 01.008.001) binds read-only buffers at bases with
address & 3of 0, 1 and 2 to the same compute programs. In a 900 s run from an empty shader cache onmain, 166 of 382 specialized compute modules were a second or later variant of a program, and 155 of those differed from another variant of the same program only in this constant. Each module is about 200 KiB of SPIR-V and takes the driver about a second to compile, on the thread that executes the dispatches.For a buffer the program neither writes nor uses atomically, the module now takes the two bits from the buffer's byte offset (
memoryByteOffsets), which the driver already passes for every bound buffer. Its low two bits are the base's:GuestBufferMemory::Descriptorrequires the bound view to start on a DWORD and sets the offset to the distance from there. The binding plan no longer emits the constant for those buffers, so descriptors that differ only in base alignment share one module. The value is available inside the formatted-load helper function as well, where the byte offset is a parameter.Written and atomic buffers keep the constant. An unaligned store is a compare-exchange loop on two words, and #4152 (item 15) found that leaving those loops in every module hung an RTX 4090. A read has no loop: it is a second load and two shifts, selected by the two bits.
Tested
Windows 11 Home 10.0.26200, i7-10750H, 16 GB, NVIDIA GeForce RTX 2060 6 GB, driver 610.91, WinLibs GCC 15.2.0 r7 UCRT, Release.
agc_shader_disk_cachechecks that the binding plan of a read-only buffer whose base is 2 bytes off a DWORD has no misalignment constant. The check next to it, that an atomic buffer records its misalignment, is unchanged.mainit fails witha read-only V# off a DWORD boundary specialized its module on the misalignment.agc_guarded_memory_loadstraces the loads of an emitted module for a given misalignment. Its evaluator returned 0 for every push-data load and supplied the misalignment through the constant only; it now returns the misalignment for the push-data load too, which is where a read-only buffer takes it from. Without that the unaligned cases read one word instead of two (plain guest reads: 2; expected: 2 3).ctest -R "unaligned|buffer_format|formatted|runtime_buffer_spans|guarded_memory_loads|shader_disk_cache|^agc_driver_graphics"onmainf73d791b3dwith this change: 40 of 40 pass. That includesagc_formatted_buffer_once, which checks that registration form has no more compare-exchanges than the specialized form, and the device tests for unaligned bases (agc_driver_buffer_unaligned_base,agc_driver_formatted_store_unaligned,agc_driver_buffer_atomic_unaligned).agc_driver_dispatch_move(review,2ba7d8b57b, test only): one compiled vector program runs with its input two bytes past a DWORD, one byte past it and back, over data whose bytes all differ; every lane reads the four bytes at its own offset, and the copy of the template from one base onto the other is refused (checked 1, moved 0, refused 1). With the validation layer loadedagc_driver_dispatch_moveandagc_driver_dispatch_move_depthpass, noVUID-line.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branch perf/shader-read-misalignment-at-run-time: 40 open PRs touch one of the five files, 22 of them the three recompiler files. I compared those 22 from their fetched heads: none changes where the direct path takes the base misalignment from. #21971 and #22071 callbaseMisalignmentand work with either source. #20248 (opt-in native buffer descriptor tables) reads the two bits from the V# word in its own table path.spirv-val --target-env vulkan1.3on 120 modules the title produced with this change (16 vertex, 23 pixel, 81 compute): all valid.main00b15fcbb0against the same commit with this change:mainThe title compiles about as many pipelines in the same time, but 37 % more of them are programs it had not compiled yet. It does not get the title past this phase: both runs are still compiling new programs when they end (one present every 40 to 90 s), and on an earlier base a second run on the warm cache found 244 more programs. The 70 remaining variants differ in image swizzle constants (a null T# against an R-only one), which this PR does not touch.
00b15fcbb0. The branch is onf73d791b3d; I rebuilt it there and ran the ctest set above, but did not repeat the title comparison on that commit.APS5_TRACE_SECONDS=100:30, no other trace or profile switch, log not redirected, the same scripted input. Each side ran the scene once before (200 s) on its own shader cache and was restarted.main69d574e9a2against the same commit with this branch merged (768548584f, pushed ascapture/pr-27812-on-main-69d574e, because the branch itself sits onf73d791b3d):main69d574e9a2768548584fDriver.Dispatchper frameThe median frame is the title's 30 fps limit on both sides; the difference is the long frames.
mainhas five frames of 270 to 630 ms in the window, the merged build none above 65 ms. I have not traced what those frames onmainwait for, and a 200 s warm-up does not guarantee that every variant of the window was already in the cache, so I read this as consistent with fewer variants to specialize, not as proof of it. One capture per side.Summary lines
The two traces (
aps5-trace-69d574e9a2cc-74352.json.gz,aps5-trace-768548584fc3-87116.json.gz) follow in a comment.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3
Summary by CodeRabbit
Bug Fixes
Tests
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T14:00:32Z.
Review: COMMENTED
Reviewed — no blocking findings
The targeted code review found no verified blocking defect; execution checks were unavailable in this review session.
What I ran
Reviewed head
e6396891f0b530c0209db7bc248cee316d7904b6using the prepared review bundle and pinned-head reader. No build or tests were run here.Results
Findings
Blocking
None verified in the inspected code.
Non-blocking
None.
Sources
Not verified
No tests, build, or runtime checks were performed. Read the PR description, commits, manifest, diff, and relevant ranges in all five changed files; full file text beyond those ranges was not read.
files.jsonwas not available through the reader; the manifest lists all five changed files as included, with no excluded changed files shown.To get this to a good state
Nothing is required. Optional: run the PR's reported checks on this head to independently confirm the reported results.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T06:25:35Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the review supports merging.
What I ran
Reviewed head
49ef7c2d06aeccd81ad4c65a0b166f6db7c87bc4. I read the prepared description, commit list, manifest, diff, all six included changed files, and the excluded changed file’s relevant functions throughrepo_read.py. No test or build commands were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Tests, builds, and hardware behavior were not verified in this review.
ShaderResources.cppwas excluded from the prepared full-text bundle; only the relevant moved-buffer functions were read through the reader. All other changed files listed in the manifest were read. No changed files were excluded byfiles.jsonrules beyond that file’s size limit.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T12:37:57Z.
Review: APPROVED
I read the diff at
49ef7c2d06. Windows 11, GCC 15.2, merged intomainatdaa61711afetogether with other open PRs (so not an isolated build), withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1andVK_LAYER_KHRONOS_validationon an RTX 5080 (621.24) and a Radeon iGPU (26.11.2).agc_shader_disk_cache,agc_driver_dispatch_moveandagc_guarded_memory_loads: pass on both GPUs. The loader log shows the layer inserted and no "was not found" line, and neither run has aVUID-orSYNC-HAZARDline.agc_shader_disk_cacheandagc_driver_dispatch_movefail.Not checked: the pipeline counts in the body, and a title.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T20:55:18Z.
Review: COMMENTED
Can the dispatch fixture also reuse the same compiled shader for a nonzero-to-nonzero change, for example base +2 to base +1, with asymmetric data and a vector read that spans two words? The added scalar case moves from +2 to an aligned base and reads the DWORD below the descriptor. That covers the move refusal and scalar alignment, while the proposed case exercises live offset changes and the shifted load through the same module.
Source review at
49ef7c2d06. The head trial-merges onto6ef4f2f73b. Focused Linux checks are queued.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T00:34:30Z.
Review: APPROVED
Reviewed — looks good
The reviewed change removes read-only buffer base misalignment from pipeline specialization while preserving the runtime offset and refusing unsafe dispatch-template moves.
What I ran
Reviewed head
2ba7d8b57b960fd124df8727146478d4d491e058againstmainusing the pinnedrepo_read.pyreader and the prepared diff. No merged-preview check was run. No tests or builds were run; test and performance results in the description are author-reported. The review environment was limited to the reader and shell; no compiler or test toolchain was used.Read all seven changed files listed under
reviewedinfiles.json, including changed lines inShaderResources.cppthrough the reader.files.jsonlists no excluded files.Results
Negative control
Not run; no tests were run during this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
I did not run the tests or independently verify the described title runs, performance captures, or console behavior. Those results are reported by the author. All changed source files were read; no files were excluded by
files.json.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T09:07:00Z.
Review: APPROVED
Reviewed — looks good
The reviewed change removes read-only buffer base misalignment from pipeline specialization and obtains it from descriptor offsets at run time; I found no verified blocking defects.
What I ran
Reviewed head
1298bfe8d0aca0ea52664b557ee7b9e348968720. I inspected the prepared diff and manifest, then usedrepo_read.pyto read relevant source ranges and search descriptor offset usage. I did not run tests or compile anything. The test and performance results in the PR description are author-reported.Results
Negative control
Not run by me. The PR description reports a red-on-base check for the specialization change.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
I did not independently verify the author-reported test runs, SPIR-V validation, performance captures, or behavior on other hardware. The six changed files included in the review folder were read in relevant sections; the excluded
ShaderResources.cppwas read throughrepo_read.pyat the changed code and surrounding logic.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-04T16:23:08Z.
Review: APPROVED
Re-verified head
1298bfe8d0on Windows 11 with GCC 15.2, applied alone to main484b0368d3. The new commit adds the moving unaligned-base case toDispatchMove.cpp. Run withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationandVK_LOADER_DEBUG=layer(layer 1.4.363).agc_shader_disk_cache,agc_driver_dispatch_move,agc_guarded_memory_loadsdispatch_moveandshader_disk_cachefail)Each log shows the loader inserting the layer, and none has a
VUID-.Not checked: a title, or a timing comparison.
AI-assisted: yes (Claude Code)
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.