Skip to content

fix(agc): place device memory in a host heap only when that heap has room - #57

Draft
Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/6553
Draft

Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/6553

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#6553, originally opened by fdfawef324.

Source commit: 4e8edc80e2646ad7619ffafb3bbb24c3f08949c6. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

When a device-local allocation would exceed its reported heap budget, DeviceMemoryType currently selects the first mask-allowed host type without checking that host heap's budget. This change selects a host type only when its heap has room for the allocation. It shares the existing coherent-buffer fit rule through Context::HeapFits, searches later allowed host heaps when an earlier one is full, and keeps the device-local type when no host heap fits.

Placement diagnostics include the selected host heap's usage and budget. A separate rate-limited line records keeping the device-local type when allowed host types exist but none fit. A mask allowing no host type remains silent.

Rebased onto fc623324a after boykopovar#5987 merged. This PR now contains one guard commit, preserving main's AllocateImageMemory routing, buffer-pool trim, texture-cache reclaim and eventual host fallback after an actual device allocation refusal. It does not restore the superseded AllocateDeviceMemory helper. Thanks to blawesom for the offered 321ab70c9 rebase; the author and earlier Claude credit are retained, with credit for that recovery work.

This is a placement decision, not a hard memory cap. Main's existing refusal path can still allocate in host memory after the device driver refuses a device-local allocation, even when the host budget reports no room. Reports are snapshots and can lag or be shared by concurrent allocations. On Windows, WDDM can also move device-local allocations into system memory. A device buffer kept device-local may remain in the existing bounded pool after release; boykopovar#4472 separately changes retention under pressure.

Demon's Souls evidence remains a hypothesis. A historical Windows run on a5e2a80c6 with boykopovar#5870, boykopovar#6280 and boykopovar#6412 died about ten seconds after a Windows event 2004 reported 95.65 GiB committed against a 95.9 GiB limit. The app had 32.5 GiB private, and about 45.3 GiB of commit belonged to no process. There was no failing-allocation stack showing this placement path. This PR neither identifies all that memory nor establishes a title memory or FPS improvement.

parl0v's RTX 4060/Windows report identifies a host-type allocation refusal after the host heap filled. Their later on/off A/B peaked below the host budget and never logged the guard; both arms survived 3/3. It supports the failure mode, not proof that this guard prevented the failure. No claim is made to fix boykopovar#5961's retained storage-image cause.

Nearby work: boykopovar#4472 moves placement counters and changes pool retention; boykopovar#7119 adds failure diagnostics. Their current heads already conflict with fc623324a, including shared buffer-pool tests, and need manual composition. Preserve the host-room guard with boykopovar#4472's counters and boykopovar#7119's diagnostics. Proposed order is this guard first; coordination is pending on #4472 and #7119. No combined-build result or merge-order agreement is claimed here.

Tested

Exact candidate: 4e8edc80e2646ad7619ffafb3bbb24c3f08949c6, based on fc623324a2ab3443cb69a6d0199141b8b2fee604, Windows Server 2025, MinGW GCC 15.2 Release/O3. Independent source review accepted the three-file change; the current fallback and multisample allocation paths remain unchanged.

New CPU/mock coverage checks host room, exact fit, nearly full/over-budget/zero budgets, a second host heap and its type mask. Integration fixtures call the actual DeviceBuffer and AllocateImageMemory paths: when neither heap fits, success stays device-local; a mocked refusal attempts device-local before host memory.

An isolated control copies the exact header and DeviceMemoryFallback.cpp. With only the host-room lookup changed back to the old host lookup, all six budget/mask assertions fail; the unchanged source passes. Fresh O3 control executables plus both test runs took 2.81 seconds of bounded child-job time. This isolated fixture uses native Windows exception handling and no driver library or GPU; it is separate from the normal project targets and does not measure game speed.

The normal Release targets built successfully. The cold build reached its 900-second bound without a compiler error; the remaining 25 incremental steps completed in 86.13 seconds. All five selected CPU/mock CTests passed in 11.58 seconds with no skipped cases: agc_device_memory_fallback, agc_buffer_pool, its two small-device variants and agc_driver_graphics. Conventions passed against the pinned base. The source remained clean throughout; generated TraceRevision is 4e8edc80e264, dirty 0.

Fresh current-head managed-slot Vulkan batch: agc_driver_pooled_buffer_range, agc_driver_sampled_cache_budget, agc_driver_sampled_cache_budget_memory and agc_driver_storage_cache_budget, 4/4 passed in 9.36 seconds, no skipped cases, with ANYPS5_REQUIRE_VULKAN=1. These are real Vulkan adjacency tests, not a real both-heaps-full reproduction. Actual executables and runtime libraries were hash-checked before and after. A clean merge into main 64b57d1b0 was checked separately; all tests ran on the exact candidate.

Historical evidence: blawesom reported 1338/1339 tests on RX 9070 XT/RADV Linux for their offered 321ab70c9, with the stated main tessellation failure. That is their external result on the offered head, not a local run or a test of this current head. The original 56d44ed2c branch had seven selected cases pass on our NVIDIA machine; those are historical head-specific results, not fresh verification.

Not tested: a title on this rebased head; a real device with both heaps unable to fit; Linux locally; full suite. No title memory decrease or FPS gain is claimed.

Checklist

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Co-Authored-By: blawesom blaplane@gmail.com
Co-Authored-By: Codex noreply@openai.com

Once the device-local heap is over its VK_EXT_memory_budget budget,
DeviceMemoryType placed textures, storage images and device buffers in
the first allowed host memory type without reading that type's heap, so
placements kept growing system memory after the host heap was past its
own budget. A host type is now chosen only when its heap fits the
allocation, by the test BufferMemoryType already applies to coherent
buffers, and a full host heap moves the search on to the next allowed
host type. When no allowed host heap has room the device-local type is
kept; if the driver refuses that allocation, AllocateImageMemory and
the buffer pool still place it in a host type after their existing
reclaim steps. The placement line now reports the host heap's usage
and budget, and keeping the device-local type is logged at the same
rate when an allowed host type exists.

Replay the guard onto the merged allocation recovery path and retain
the offered rebase's authorship. Exercise both device buffers and
images through their current fallback helpers when neither heap fits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: blawesom <blaplane@gmail.com>
Co-Authored-By: Codex <noreply@openai.com>
@Thimo188 Thimo188 closed this Dec 2, 2026
@Thimo188 Thimo188 reopened this Dec 2, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed fix error Closes an issue (Fixes #N) labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix error Closes an issue (Fixes #N) needs rebase Conflicts with main, rebase needed

Projects

None yet

2 participants