perf(shader): keep a binding's element flags inline #6970
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6970"
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 meh7an on 2026-10-16T22:55:31Z.
What
DescriptorBindingBuilder::Populateturns a stage's binding plan into the bindings of a materialized result, for every draw stage the memos miss and for every dispatch. It copies each plannedDescriptorBinding(auto physical = entry.descriptor). A binding keeps 12 per-element flag lists asstd::vector<bool>:samplerDepthCompare,imageWritten,imageDepthCompare,imageAtomic,imageAtomic64,bufferAtomic,bufferWritten,samplerUnnormalized,samplerIntegerBorder,imageUnnormalized,imageUnnormalizedBaseLevelandbufferRead. Every copy allocates one block per non-empty list, and releasing the result frees them again.ElementFlags(inRecompiler.hpp) replacesstd::vector<bool>for those lists:std::vector<bool>. A binding rarely has more than 64 elements, so a copy allocates nothing.std::vector<bool>does:size,empty,[](a reference proxy when written),at,back, iteration,push_back,assign,resize,clear, and==with another list or astd::vector<bool>. The code that fills and reads the lists stays as it is.ElementFlagsinstead ofstd::vector<bool>.ElementFlags.verifyElementFlags(shader disk cache tests) checks 70 flags, past the inline word. It covers reads, iteration, copies, writes through[]andback(),resizeboth ways,assign,clear, and a round trip through the disk cache.On the S2 page of Sonic Origins Plus's main menu, the queue worker spent, per frame (APS5 sampler, 15 s):
Populate: 1.28 to 0.91. In a profile of the baseline, 78 of its 151 directoperator newsamples were inlinedstd::vector<bool>copies (8 call sites), and 14 more were instd::vector<bool>'s copy constructor.RecompileResult's disposal): 0.76 to 0.57;operator new3.38 to 2.88,mi_free0.68 to 0.59;That is a few percent of the worker's frame at most. On current main, three paired runs below put the frame median about 2% lower.
Tested
Windows 11 (10.0.26300), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 2080 Ti, driver 617.14.
ShaderDiskCache.cpp's size asserts compile only on Linux (x86_64, libstdc++), so the Windows build above skips them.ElementFlagsis 56 bytes againststd::vector<bool>'s 40. Main'sDescriptorBindingnow has 13 such lists, withsamplerCoordinatesIgnored, so it grows from 656 to 864 bytes (+32%), and its assert says 864.sizeof(DescriptorBinding)is 864 with this change.7d728b109, grew it from 592 to 784 bytes (thanks @teepean).53a0a27bb(a9a338c37).ShaderInvocation(216) andRecompileResult(528).DescriptorBindingis 864. All four sizes were checked under MinGW's libstdc++ with aSize<sizeof(...)>probe: 216, 528, 864 andCompiledShaderArtifact280.agc_driver_image_array_window_past_layer_limitis not run: MinGW does not build itsalignas(65536).1d4164552.samplerCoordinatesIgnoredis anElementFlagsas well; main'sbufferExtentstays astd::vector<std::uint32_t>.agc_driver_image_array_window_past_layer_limitis not run: main does not compileImageArrayWindowPastLayerLimit.cppwith MinGW (alignas(65536)).agc_driver_pass_input_hazard(added inba4571a25) fails on plain main1d4164552as well, 0 of 4 runs alone.1d4164552in one session: three runs per side, interleaved, each aAPS5_TRACE_SECONDS=200:15capture with warm caches and the title FPS (median of six readings).Driver.Draw62.63 and 60.79 ms (-2.9%).Before this rebase:
7d728b109with this change, the full ctest suite passes 1338 of 1341 tests.agc_driver_gpu_indirect_arguments_misaligned_writer_aheadis not run: main itself does not compile it with MinGW (alignas(65536)).select_testsfails in its setup on Windows without the symlink privilege (WinError 1314fromsymlink_to), as on main.build_eligibilitytimed out once in the parallel run; alone it passes in 1.5 s.APS5_TRACE_SECONDS=200:15on the same machine in one session, caches warm. Traces attached.The title FPS during the two captures was 10.78 and 10.82 (median of six readings each).
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)Traces and summary lines (baseline b1909f43b, whose summary says 11882cc993f0; this change 245e35555, whose summary says b1909f43b4d1):
flags-traces.zip
Traces of the six re-measured runs on main
1d4164552, one zip per round with that round's main and this-change capture: flags-rebased-round1.zip, flags-rebased-round2.zip, flags-rebased-round3.zip.The re-measured median pair on main
1d4164552, withtools/trace_digest.pyfor each trace:Main
1d4164552, run 2 (summary commit1d41645527)seconds 200:15 from present 3075: 170 presents, frame p50 86.8 / p90 92.1 / p99 115.2 / max 117.7 ms, GPU busy 69.1 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
1d41645527, dropped: yesSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 68.5, Graphics.Draw 32.5, Graphics.Draw.draw_and_resource_release 28.0, VideoOut.Flip 24.8, Driver.Present 24.7, VideoOut.Flip.present 24.7, Driver.Present.render_fence_wait 24.0, Vulkan.Retire 24.0, Vulkan.Retire.wait 24.0, Driver.Packet 19.3
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
This change 25f8843b1, run 2 (summary commit 25f8843b15fe)
seconds 200:15 from present 3035: 171 presents, frame p50 85.2 / p90 98.0 / p99 116.4 / max 116.6 ms, GPU busy 70.6 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit 25f8843b15fe, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 70.5, Graphics.Draw 32.7, Graphics.Draw.draw_and_resource_release 28.2, Driver.Present 26.9, VideoOut.Flip 26.9, VideoOut.Flip.present 26.7, Driver.Present.render_fence_wait 26.1, Vulkan.Retire 26.1, Vulkan.Retire.wait 26.1, Driver.Packet 20.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Imported from boykopovar/AnyPS5. Originally posted by teepean on 2026-10-17T07:21:15Z.
On Linux (GCC 16.2.1, Release) the head doesn't compile. Merged onto
main6a32550fd,ShaderDiskCache.cppstops at:ElementFlagsholds astd::uint64_t, astd::size_tand astd::vector<bool>: 56 bytes against the 40 of thestd::vector<bool>it replaces. Twelve lists inDescriptorBindingmake that 12 × 16 = 192 bytes more, 784 in all. The PR's Linux job was skipped, so CI didn't catch it. The encoder writes the lists element by element, as the description says, so only the size in the assertion needs updating. With it set to 784 locally, the build runs Stray (PPSA02100), in a batch together with five other open perf PRs, without visible problems.AI-assisted: yes (analysis and text written with Claude Code; the build is from my machine).
Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-17T09:03:54Z.
Thanks for catching it. Fixed in 15bc2186b: the assert now says 784 (12 lists x 16 bytes more, as you worked out). I also compiled that Linux-only block under MinGW's libstdc++, whose layouts match: all 31 asserts hold with the change, and the old 592 fails exactly as you reported.
Imported from boykopovar/AnyPS5. Originally posted by keravyn-ops on 2026-10-17T10:16:30Z.
AMD / Windows run of this PR at 15bc2186b: Radeon 780M (RDNA 3, 0x15bf), AMD driver 25.10.20.01 (32.0.21020.1007), Windows 11 Home 10.0.26100, WinLibs GCC 15.2.0 r7, Release.
Every ctest entry built from the test targets this PR changes passes (57):
agc_pipeline_cache,agc_pipeline_cache_file,agc_pipeline_library,agc_pipeline_library_off,agc_pipeline_library_fast_only,agc_import_watch,agc_import_watch_other_family,agc_unchanged_cpu_stamps,agc_storage_kept_unit_settle,agc_storage_baseline_settled,agc_storage_integer_clear,agc_storage_upload_lifetime,agc_storage_upload_failure,agc_storage_written_buffer_order,agc_storage_split_writeback,agc_copied_direct_ordering,agc_recorder_read_list,agc_space_pending_writes,agc_host_import_unmap,agc_parked_import_unmap,agc_host_import_retirement,agc_sparse_run_import,agc_sparse_run_import_copied,agc_unit_shadow,agc_storage_cpu_snapshot,agc_descriptor_pool_recycling,agc_draw_snapshot_cap,agc_draw_snapshot_epoch,agc_multisample_guest_access,agc_draw_inputs_in_place,agc_pending_write_snapshot,agc_resident_present,agc_queued_write_groups,agc_tiled_resolve,agc_slice_dirty,agc_metadata_pass,agc_host_import_second_mapping,agc_pending_key_stores,agc_shadowed_key_stores,agc_sample_dumps,agc_driver_vertex_target_range_copied,agc_driver_vertex_target_range_imported,agc_driver_undithered_alpha_to_coverage_state,agc_driver_alpha_to_coverage_state,agc_driver_prepared_graphics_prebuild_unset,agc_driver_prepared_graphics_prebuild_0,agc_driver_prepared_graphics_prebuild_1,agc_shader_disk_cache,agc_driver_graphics,agc_driver_prepared_graphics,agc_driver_prepared_unregistered_graphics,agc_sparse_buffer_holes,agc_pipeline_journal,agc_descriptor_pool_reset_failure,agc_driver_address_space_teardown,agc_compute_descriptor_layout_flags,agc_driver_covered_centroid_stateAI-assisted: yes (runs made on the device above).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T12:40:08Z.
Review: CHANGES_REQUESTED
Thanks, the implementation reads correctly (the 64/65 boundary in
push_back/set/operator[],resizeshrinking back to inline, and the disk cache format are all consistent, andverifyElementFlagscovers the spilled case). Two things before this can go in:frame_ms.p50+0.4%,mean_frame_ms+0.8%,Driver.Draw+1.1%, FPS 10.81 → 10.56), and the profile gain (Populate1.28 → 0.91, worker 32.80 → 31.04 samples) comes from one sampled pair. The cost isDescriptorBindinggrowing 592 → 784 bytes (+32%) for every binding held in results and caches. Please post repeated paired runs (several captures per side, with the spread) where the worker or frame time improves outside the noise, or drop this change.main:DescriptorBindinggainedsamplerCoordinatesIgnoredandbufferExtent, and thesizeofasserts inShaderDiskCache.cppchanged (DescriptorBindingis 656 there now). After rebasing, the newsamplerCoordinatesIgnoredlist should be anElementFlagstoo (and its users checked), and the asserts recomputed.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T13:11:37Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-20T14:44:01Z.
Thanks. Rebased onto main
1d4164552and re-measured.samplerCoordinatesIgnoredis anElementFlagstoo. Its users compile and pass unchanged:push_back;packBitsand its flag list;ShaderResources.cpp;Flags;std::vector<bool>.DescriptorBindingnow holds 13 flag lists and grows from 656 to 864 bytes (+32%). The assert says 864.1d4164552: three interleaved runs per side, each a200:15trace with warm caches, plus the title FPS (median of six readings).Driver.DrawDriver.DrawDriver.Draw62.63 to 60.79 ms (-2.9%).Imported from boykopovar/AnyPS5. Originally posted by teepean on 2026-10-20T18:14:21Z.
@meh7an a Linux follow-up on the rebased head 25f8843b15. Merged with
main5f7e30233e, it compiles with GCC 16.2.1 (Release).agc_shader_disk_cache_testsandagc_driver_graphics_testspass with the validation layer, with no VUIDs.I also tried the commit on Stray (PPSA02100), on my local stack (
showcase/stray-stack,main64e2a810a9plus 84 commits). There every stage-instance hit copies a wholeRecompileResultwith its bindings. The setup: the start of the Dead City chapter, stockengine.ini, Linux, Ryzen 9 9950X, RTX 3090, tcmalloc preloaded. Four rounds in alternating order, FPS from the title bar (48 readings per run):The PR's run was faster in every round. I didn't measure
mainin the PR's form: one pair of runs there spreads more than the ~2 % you saw on Sonic.AI-assisted: yes (text with Claude Code; the runs are from my machine).
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-21T00:22:04Z.
@adriadam10 you asked for repeated paired runs where the worker or frame time improves outside the noise. Here are two titles, three alternating pairs each.
Repeated paired runs on two titles: this change is faster outside the noise on both. Linux (Ubuntu 26.10, kernel 7.3), Ryzen 9 5950X, RTX 3080 Ti (NVIDIA 615.71.09), Release. Base
main7e95bde79; candidate = thatmainwith this head 25f8843b1 merged (5df59fe43, one conflict: the threesizeofasserts,main's values kept andDescriptorBindingset to 864, which compiles). Each build relinked with its own relinker into a fresh runtime (shader cache empty for pair 1, warm after, the same on both sides), runs alternatingmain, PR,main, PR, …, presents per 10 s fromAPS5_PROFILE_DRAW.Sonic Origins Plus (PPSA05325, 02.002.000), main menu, Sonic 2 page. Scripted: X on the autosave notice, X on the title, Right twice, 30 s to settle, then 60 s measured with no input. Every run's last screenshot is on the Sonic 2 page.
main[draw] driver phasesrecompileAbout +8%, and no PR window is as low as any
mainwindow.Castlevania: Belmont's Curse (PPSA35399, 01.000.000), the boat scene, rendered by the engine and waiting on a dialogue line, no input (about 2,800 draws per frame, CPU-bound). Last 4 windows of 70 s,
APS5_HOST_IMPORT_MIB=16384on both sides:mainAbout +20%. Capture and the committer stay the same (~12 µs and ~29–31 µs per draw), so the time comes out of the recompile phase, where
Populatecopies the bindings.One aps5-summary.jsonl row (10 s) per side, pair 2 of each title
Sonic Origins Plus,
mainthen this PR:Castlevania,
mainthen this PR:AI-assisted; the runs are from my machine(s).
Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-21T08:01:28Z.
@adriadam10 Rebased onto main
53a0a27bb(a9a338c37). The only conflict was the size asserts: main'sShaderInvocation216 andRecompileResult528 stay, andDescriptorBindingis 864, checked under MinGW's libstdc++.Since your review, two independent sets of runs came in:
mainwindow, and the time comes out of the recompile phase, wherePopulatecopies the bindings.Thanks @teepean and @mrfelek for the runs. Could you take another look?