Skip to content

fix(agc): select coherent buffer heaps using available budget - #4446

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
Scramblrrrr:codex/buffer-heap-fallback-main
Oct 14, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
Scramblrrrr:codex/buffer-heap-fallback-main

Conversation

@Scramblrrrr

@Scramblrrrr Scramblrrrr commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Coherent host buffers prefer a cached memory type even when its heap's reported available budget cannot cover the allocation. Select a compatible type on another heap before allocating, using VK_EXT_memory_budget; keep every required property and memoryTypeBits restriction.

The check uses the full slab allocation size for pooled buffers. It keeps the preferred type when budget information is unavailable, when it has room, or when no suitable alternate heap has room. Explicit HOST_CACHED and DEVICE_LOCAL requirements remain required. The existing upstream BufferPool allocation/error behavior is preserved; this revision adds no allocation retry after an error. An alternate CPU-visible device heap can make CPU reads slower, so it is selected only when the preferred heap has insufficient reported budget.

This replaces the earlier post-failure fallback, following maintainer review. Rebased base: main 88107b7. Head: 9528749 (0a3f392 selection change plus allocation-level regression tests).

Tested

Validation on Windows / RTX 5070 Ti / Release GCC / SPIRV-Tools OFF:

  • agc_device_memory_fallback, agc_buffer_pool, and full mocked agc_driver_graphics: 3/3 passed (5.67 s). Focused allocation-placement and the preserved sparse-buffer-holes modes also passed.
  • Earlier negative control on base 510a218: removing only the production budget selection while retaining tests makes both selection and real Buffer allocation cases fail; buffer-pool control passes. Restored production passes all three.
  • Allocation cases cover a 512-byte buffer that requires a full 2 MiB slab, a direct 3 MiB buffer, mapping/write-through, cleanup, and allocation errors with one request on the selected heap in a fresh pool.
  • Selection cases cover exact fit, insufficient/unknown budgets, unavailable budget query, allowed masks, same-heap types, and explicit property requirements.
  • Scoped conventions and diff checks pass. Linux/macOS not run locally. Independent Linux x86-64/lavapipe cross-test by coltken95 on main 5d50886: five native controls pass; parent compile mismatch is not counted as a production negative.

The older 007 integration's allocation failure motivated the investigation, but it is not current-main evidence. Independent Windows title evidence by EagleBearerYS: Resident Evil Village on main 18c438b plus its required open patches, same save/inputs and warm cache, one run per side. Without this PR, one 8 MiB allocation first failed with -2 and succeeded after existing pool reclamation; with it, no allocation failed and compatible type 5 allocations were used. Heap 1 at title screen was 7,485 vs 7,236 MiB (reported budget 7,237 MiB); title-screen time 138 vs 131 s. This is integration-stack, single-run evidence, not a clean-main A/B or an established FPS improvement. Details: #4446 (comment) . Game OOM recovery is not established. This remains a draft pending maintainer assessment of the generic change and relevant title evidence. No integration stack, title code, assets, or captures included.

Related: #4108 (image budget selection), #290 (host import/pool allocation behavior).

Checklist

  • Based on main; one topic
  • Depends on: none
  • No new code comments or investigation artifacts
  • Required properties and explicit failures retained
  • No new third-party code
  • Generic behavior
  • AI-assisted: yes (Codex); author responsible for review

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

First observed runtime use of the alternate-heap path, on the older 007 integration artifact f8956be/SHA61BF39CB (same production code as diagnostic027090d37; standalone PR4446 itself has not been staged): after original type3 allocations fail with -2, logs show recovery for58720256bytes and75497472bytes on memoryType4/originalType3, reclaimed0, attempts2. This is the compatible device-local host-visible/coherent heap on this RTX5070Ti. Same run separately recovers18MiB/16MiB allocations after trimming/retrying on original type3. Uses existing APS5_HOST_IMPORT_WINDOW_MIB=16, host-import budget6144; no other executable change. This supports the recovery mechanism on real allocations, not an FPS/playability claim or a proof that all OOMs are fixed. Current run still has known unsupported runtime-table operations and high retained memory. No title data uploaded. AI-assisted: yes (Codex).

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Acknowledged #4472 overlap. Its production change reads the VRAM budget and changes Put's retention of released device-tier buffers; #4446 changes allocation failure retries for ordinary coherent host-visible buffers. They are separate mechanisms and neither requires the other. The reported conflict is the shared BufferPool mock fields/test registration; it should be resolved by retaining both test sets, not dropping either. I have reviewed #4472's mock diff but have not built the combined heads yet, so this is source-level compatibility only. My next game cross-test is the collaborator's merged-hull/sparse-index stack; I will keep any VRAM-budget performance experiment separate. AI-assisted: yes (Codex).

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Source-level compatibility branch prepared for #4446 head55e666483 and #4472 head2d90e03e3: https://github.com/Scramblrrrr/AnyPS5/tree/codex/buffer-fallback-vram-budget-cross-test head89329b39b26b1b50550a694e9773768a0d51acaa. The two conflicts were only the mock fields and test registrations in tests/BufferPool.cpp; both sets are retained. Production merges cleanly. Conventions pass. Not built/staged yet, while the separate007 ordering test runs; will report combined native checks afterwards. This remains separate from the game shader/mapping integration, with no FPS claim. AI-assisted: Codex.

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Combined native validation is now complete for head89329b39b26b1b50550a694e9773768a0d51acaa: #4446 head55e666483 + credited #4472 head2d90e03e3. Both conflicting mock-field groups and both test registrations are retained. Fresh build of the pool, graphics and draw-cache fixture targets; agc_buffer_pool, agc_driver_graphics, agc_driver_draw_cache_reuse and _off pass4/4 in5.50s on Windows11/RTX5070Ti. Conventions pass, worktree clean. No game staging or FPS/memory-gain claim from this combination. Branch: https://github.com/Scramblrrrr/AnyPS5/tree/codex/buffer-fallback-vram-budget-cross-test . This confirms the shared test-file overlap can be resolved without dropping either mechanism's checks. AI-assisted: Codex.

@oneandonlydean oneandonlydean self-assigned this Oct 12, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

BufferPool::AllocateMemory allocates again, with a memory type on another heap, after vkAllocateMemory has failed (core/libs/prx/libSceAgcDriver/Graphics/src/BufferPool.cpp:60-73). Retrying after an error isn't allowed here: choose the memory type before allocating, from the heap's VK_EXT_memory_budget budget and usage as #4108 now does for images, and keep the throw.

Which title fails this allocation on main? The 2359296000-byte failure in the description is from an older integration stack.

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Agreed: the current post-failure retry does not follow the requested allocation policy. I will revise it to select an eligible coherent heap from its budget/usage before allocating, retaining the allocation error rather than retrying it, following #4108's approach.

The quoted2359296000-byte failure is from the older007 integration described in this PR; I do not have a demonstrated title failure on current main. I will keep those claims separate and have moved this PR back to draft while preparing the revision and focused selection tests. The earlier native retry tests do not validate the requested budget-selection policy. AI-assisted: Codex.

@Scramblrrrr
Scramblrrrr marked this pull request as draft October 13, 2026 01:00
@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Source-only requested budget-selection revision is prepared on current main510a2188cf4ea505dd58b3d5fc80ae3af2680c38: https://github.com/Scramblrrrr/AnyPS5/tree/codex/buffer-budget-selection-current-main head1590dbe5c95438cbed9ceea5abad72240502ee3f. Unbuilt/untested yet; waiting for the separate007 comparison to end, so no build/tests compete with it. Conventions/diff checks pass.

Context::BufferMemoryType keeps the existing cached coherent preference when it fits or budget information is unavailable. When the preferred heap's reported headroom cannot fit the allocation, it selects another allowed heap/type satisfying all required flags and with enough reported headroom. Default cached preference can yield to an eligible uncached coherent type; explicitly required cached/device-local flags remain mandatory. Slab placement uses the full slab block size, and its key is created for the selected type. BufferPool::AllocateMemory is unchanged from main; none of this revision contains the additional post-failure alternate-heap retry. The existing allocation error handling remains.

Twelve selection cases added to the existing device-memory-placement fixture cover exact fit, budget overflow, allowed masks, same-heap types, noncoherent alternatives, explicit device-local/cached constraints, unreported/insufficient budgets and unavailable queries/devices. Native and production-negative validation still pending. No current-main title failure demonstrated; original older-stack evidence stays historical. Keeping #4446 draft and its old branch unchanged until this revision is validated. AI-assisted: Codex.

@Scramblrrrr
Scramblrrrr force-pushed the codex/buffer-heap-fallback-main branch from 55e6664 to 4c291ba Compare October 13, 2026 01:40
@Scramblrrrr Scramblrrrr changed the title fix(agc): retry coherent host buffers on another heap fix(agc): select coherent buffer heaps using available budget Oct 13, 2026
@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Revised this draft to pre-allocation budget selection as requested. New head 4c291ba, base main 510a218. The earlier post-error alternate-heap retry is gone; required flags/memoryTypeBits remain enforced, slab selection budgets for the full slab, and unavailable budget data preserves the original preference. Existing upstream BufferPool behavior is unchanged.

Selection, buffer-pool control, and full mocked graphics tests pass (3/3, 5.69 s). Removing only production selection makes the helper and allocation-level regressions fail; restoring it passes. The real Buffer cases verify a single request to the selected type for both a 2 MiB slab backing a 512-byte buffer and a direct 3 MiB allocation, including allocation failures. Conventions pass.

Still no current-main title failure established. I rewrote the title/body around the final change and kept it draft rather than presenting the older integration failure as current-main evidence.

AI-assisted (Codex).

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Conflicts with main in core/libs/prx/libSceAgcDriver/tests/Graphics.cpp after today's merges there (#2121, #2132, #3471, #3633, #4206); please rebase before marking it ready.

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Acknowledged, thanks. I have resolved the current-main conflicts locally on base 88107b7: the sparse-buffer-holes CLI mode and our independent buffer-budget-placement mode are both retained. Local rebased head is 9528749; production budget-selection behavior is unchanged. Scoped conventions/diff checks pass. I am holding the PR branch update until the rebased native checks finish; they will run after the current isolated 007 test, to avoid concurrent build/test load. It remains draft, and the lack of a demonstrated current-main title allocation failure still applies. AI-assisted (Codex).

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Rebase now validated and pushed: 9528749 on main 88107b7. Preserved both CLI modes in the Graphics.cpp conflict. Windows/GCC15.2/Release: selection, buffer pool, full mocked graphics 3/3 PASS 5.67s; focused real-allocation placement and sparse-buffer-holes control PASS. Scoped conventions/diff checks pass, source clean. Earlier negative controls remain tied to their original base in the updated description. Production selection behavior is unchanged; still draft, with no established current-main title failure or 007 FPS/OOM claim. No current-main build library staged into the game. AI-assisted (Codex).

@Scramblrrrr
Scramblrrrr force-pushed the codex/buffer-heap-fallback-main branch from 4c291ba to 9528749 Compare October 13, 2026 06:02
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@coltken95

Copy link
Copy Markdown

Tested on Linux x86-64 (Release build, lavapipe / llvmpipe LLVM 20.1.2), with 9528749 cherry-picked onto main 5d50886:

  • With the PR: agc_device_memory_fallback, agc_driver_graphics, agc_sparse_buffer_holes, agc_descriptor_pool_reset_failure and agc_compute_descriptor_layout_flags all pass.
  • With main's Context.hpp / Resources.cpp, the new test doesn't compile:
    'struct AgcDriver::Graphics::Context' has no member named 'BufferMemoryType'

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Thanks for the Linux cross-test of9528749cd and the five passing controls. The compile failure with main production restored confirms the new fixture depends on the added API, but is not a runtime behavioral negative; retaining that distinction in the validation evidence. Current head remains mergeable. AI-assisted: yes (Codex).

@EagleBearerYS

Copy link
Copy Markdown
Contributor

A title that hits this: Resident Evil Village (PPSA01556, 01.210.000), on Windows 11 with an RTX 3070 Laptop GPU (8 GB) and 16 GB of RAM (WinLibs GCC 15.2.0 r7, Release). With 16 GB, heap 1 (host-visible system memory) is 7,888 MiB, with a VK_EXT_memory_budget budget of 7,237 MiB, and about 6 GiB of it are host imports at main's default import budget.

On plain main, Village stops at its first frame (#4949). These runs are on main 18c438b with the open pull requests it needs to get further (listed in #5169, among them #4517 and #5249) and a few local changes. They used the same save and inputs and a warm shader cache, single runs, up to the title screen after the first scene load:

without this PR with it
allocations that failed with -2 1: an 8 MiB pool buffer in type 4. It was allocated only after the pool released 1,023 MB of retained buffers and tried again none
heap 1 at the title screen (budget 7,237 MiB) 7,485 MiB 7,236 MiB
heap 0 2,581 MiB 3,111 MiB
type 5 (DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT, resizable BAR) nothing 231 MiB in 40 buffers, 46 MiB in 21 slabs
title screen, after the VR Mode question 138 s 131 s

On an older build, before #4515, #4517 and #5249 cut the per-build copies of descriptor views, the reclaim ran dry and the load stopped:

[bufferpool] vkAllocateMemory buffer recovered: allocation=10485760 reclaimed=892783216 initialResult=-2 attempts=2
[videoout] presentation failed: compute shader 0x207e430a00: AGC graphics: vkAllocateMemory buffer: allocation=10485760 memoryType=4 reclaimed=0 initialResult=-2 attempts=1: Vulkan result -2

Heap 1 was then at 7,483 MiB: 6,143 MiB of imports, 1,076 MiB of dedicated buffers in type 4 (most of them per-build copies of descriptor views) and 84 MiB of slabs. I haven't run that build with this PR. With it, an allocation made while heap 1 is past its budget goes to heap 0, as in the runs above.

@Scramblrrrr

Copy link
Copy Markdown
Contributor Author

Thanks for the Windows title cross-test and exact heap/type measurements. This provides relevant title evidence that my original 007 motivation did not: same-stack Resident Evil Village runs select compatible alternate heap types without weakening required properties. I will add the result to the description as your single-run integration evidence, keeping the current-main stop and lack of a clean-main A/B explicit. No allocation retry or OOM recovery is added by this PR.

Also thanks @coltken95 for the Linux native controls. The parent compile failure is an API mismatch, rather than a production-negative result; the meaningful budget-selection/allocation negatives in the description remain the Windows controls with unchanged fixtures and only production selection disabled. AI-assisted: yes (Codex).

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Our points are met here: the heap is chosen from the budget before allocating, and the allocation failure is gone in the title run. Please mark it ready for review when you are happy with it, and it gets a full build.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 14, 2026
@oneandonlydean
oneandonlydean force-pushed the codex/buffer-heap-fallback-main branch from 9528749 to 53acbf0 Compare October 14, 2026 02:51
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Rebased onto main to resolve the conflict in tests/Graphics.cpp after #290; no other change. Mark it ready when you want it reviewed.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 14, 2026
@oneandonlydean
oneandonlydean marked this pull request as ready for review October 14, 2026 02:57
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Marked this ready, since both points are met and it's rebased onto main; it goes through the full build now.

@oneandonlydean
oneandonlydean merged commit bf8e69d into boykopovar:main Oct 14, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants