Skip to content

d3d12: the title-texture upload ring gives back large idle buffers, and counts as staging (RT-TITLE-UPLOAD-TRIM) - #2463

Open
kawaii-shakal wants to merge 4 commits into
ProjectLeonida:mainfrom
kawaii-shakal:title-upload-staging-trim
Open

kawaii-shakal wants to merge 4 commits into
ProjectLeonida:mainfrom
kawaii-shakal:title-upload-staging-trim

Conversation

@kawaii-shakal

@kawaii-shakal kawaii-shakal commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-TITLE-UPLOAD-TRIM

Overlaps #2021 (the same header, a different change): the two conflict only in runtime/CMakeLists.txt, where
each registers a test; whichever lands second keeps both lines.

What and why

TitleImageUpload (runtime/shims/d3d12_title_image_upload.h) is a ring of four slots. Each slot grew to the
largest whole-image upload that went through it and kept that buffer until the device was destroyed. The buffers
are host-visible, so on a discrete GPU they are system RAM: after a burst of large textures (a cutscene's, say)
up to four of the largest stayed allocated for the rest of the run. The ring's memory was also not named in the
mem probe, so it showed up as other.

  • trim(), called from the periodic title-texture pass (refresh_title_textures) under a try_lock, frees a
    slot whose buffer is over LEONIDA_TITLE_UPLOAD_KEEP_MB (4) and that has not been used for
    LEONIDA_TITLE_UPLOAD_IDLE_MS (2000), once its last transfer has completed. Completion is read with
    vkGetSemaphoreCounterValue; the pass never waits for the GPU, and a slot whose value can't be read stays.
    LEONIDA_TITLE_UPLOAD_IDLE_MS=0 keeps the old behaviour, for A/B runs.
  • The ring allocates under a memory_breakdown staging owner, so [rt] mem-probe breakdown shows it as staging.
    The registered image backing line now also says how many buffers were released.
  • The rule is runtime/src/title_upload_trim.h, tested by title_upload_trim_test: idle limit and size floor,
    a transfer still running, an empty slot, a clock read before the last use, 0 = off, malformed and huge values.
  • runtime/README.md and docs/d3d12_vulkan.md describe the two variables.

Tested

  • Linux (Ubuntu 24.04 container, g++ 13, Mesa lavapipe): ctest 117 of 117, with the new test.
  • The changed header compiled on Linux in a test translation unit that calls trim() and capacity() on a fake
    device (-Wall -Wextra, no warnings from these files).
  • Windows build cross-compiled on Linux (clang-cl 21.1.8, lld-link, xwin MSVC CRT and SDK 10.0.26100): all
    1,091 targets, including d3d12_vulkan_device.cpp. Its tests under Wine 9.0 with Xvfb on Mesa lavapipe: 464 of
    490 pass; the 26 that fail fail the same way on main (checked by building main's two files in the same tree).
  • Not run with the game, so the RAM saved is not measured. The way to see it: LEONIDA_MEM_PROBE=1 through a
    cutscene and after it, with LEONIDA_TITLE_UPLOAD_IDLE_MS=0 and with the default, comparing the staging part
    of the breakdown line and the buffer size on the registered image backing line.

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption.
  • A new translator rule is in the contract table of docs/design.md. (n/a: no translator change)

🤖 Generated with Claude Code

…nd counts as staging

TitleImageUpload's four slots each grew to the largest whole-image upload that went through them and kept that
host-visible buffer (system RAM on a discrete GPU) until the device was destroyed, so after a burst of large
textures up to four of the largest stayed allocated. The periodic title-texture pass now calls trim(), which,
under a try_lock and without waiting for the GPU, releases a slot over LEONIDA_TITLE_UPLOAD_KEEP_MB (default 4)
that has been idle for LEONIDA_TITLE_UPLOAD_IDLE_MS (default 2000; 0 keeps the old behaviour) once its last
transfer's timeline value is reached. The rule is src/title_upload_trim.h, tested by title_upload_trim_test.

The ring's allocations now run under a memory_breakdown staging owner, so the mem probe's breakdown line shows
them as staging instead of other, and its texture line says how many buffers were released.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
@ecox982745

Copy link
Copy Markdown

Native Windows check on d14a81479. The Windows-only d3d12_vulkan_device.cpp compiled with MSVC 19.44 and gta6rt.exe linked in Release. The stock title_upload_trim CPU test and d3d12_mapped_plane_async RTX 4090 test both passed.

I also measured the ring through the production refresh path on Windows 11, RTX 4090, NVIDIA 616.64. I adapted the test's mapped-plane fixture locally to upload four 4096×2048 R8 textures, each filled with a different byte. I disabled host image copy on the test device to exercise staging. Each copy completed and its full 8 MiB readback matched before the next upload. After 10.5 s idle, an empty queue submission triggered the next refresh and let the regular memory probe print the counters.

Settings Completed staged fills Ring capacity after idle Buffers released idle
Defaults (2,000 ms, 4 MiB) 4 0 MiB 4
LEONIDA_TITLE_UPLOAD_IDLE_MS=0 4 32 MiB 0

A fifth 8 MiB upload with a new fill byte passed full GPU readback in both processes. Khronos validation was enabled and reported zero errors. This checks retention, release and reuse of the staging ring with synthetic textures. The 32 MiB is the ring-capacity difference for this fixture.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and tested at d14a81479 (merged cleanly onto main ec91d695c).

The trim policy header (runtime/src/title_upload_trim.h), non-blocking timeline check in transfer_done(), atomic capacity() / trimmed() counters, and Consumer::staging accounting are clean and pass CPU and GPU tests. Two notes inline:

  1. Small uploads on the round-robin ring can keep oversized slots alive indefinitely: write() rotates slots_ round-robin and stamps slot.last_used_ns = now_ns() on every upload regardless of size, so steady small uploads (<= 4 MiB) every ~0.5 s keep resetting last_used_ns on all 4 oversized slots and prevent trim() from reclaiming them.
  2. Minor portability note: sibling std::getenv(...) calls in one argument list in trim().

Verified locally on Windows 11 (MSVC 19.44.35230.0, Ninja Release, NVIDIA GeForce RTX 5070, driver 610.88, Vulkan 1.4.341; note VK_LAYER_KHRONOS_validation is not installed locally so Vulkan validation layers did not run):

  • title_upload_trim (CPU): passed
  • d3d12_existing_heap and d3d12_host_prefix (GPU): 2/2 passed
  • scripts/check_no_game_data.py and git diff --check: clean

AI-assisted.

@djrolfes

Copy link
Copy Markdown
Contributor

Independent Linux/Wine + NVIDIA check at head d14a81479 (merged cleanly onto origin/main ec91d695c in a local integration stack alongside #2467, #2470, #2469, #2461, #2126, #2091, and two local V# patches).

  • Environment: Linux 7.2.15, wine-11.22, clang-cl + xwin cross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Ryzen 7 5800X, 32 GB RAM. No game files.
  • Build & CTest: gta6rt and title_upload_trim_test cross-compiled cleanly with clang-cl + xwin; title_upload_trim passed (4.39 s) under Wine.
  • Code review: relaxed atomic capacity()/trimmed() counters and non-blocking vkGetSemaphoreCounterValue check in transfer_done() before releasing idle slots look clean.

AI-assisted.

…he trim settings with owned copies

Review of ProjectLeonida#2463 by lixonik:
- write() stamped a slot's last use on every upload, so steady small uploads going round the ring kept every
  slot's large buffer from a burst alive for ever. Only an upload over LEONIDA_TITLE_UPLOAD_KEEP_MB stamps it
  now (title_upload_trim::counts_as_use): a small one fits the buffer trim() leaves behind.
- The two variables were read by sibling std::getenv calls, whose results may share a buffer, and the Windows CRT
  deprecates getenv. title_upload_trim::settings_from_env copies each value (_dupenv_s on Windows).

title_upload_trim_test covers both: small uploads every 0.5 s after a large one no longer keep the slot, and
both variables are read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

@lixonik thanks, both were right. Changed by Claude Code (AI-assisted) in 07326df:

  1. write() no longer stamps a slot on every upload. Only an upload over LEONIDA_TITLE_UPLOAD_KEEP_MB counts
    (title_upload_trim::counts_as_use), so small uploads going round the ring no longer keep a large buffer from
    a burst alive; a small one fits the buffer trim() leaves behind. title_upload_trim_test has the case you
    described: 1 MiB uploads every 0.5 s after a 64 MiB one, and the slot is released 2 s after the large one.
  2. The settings are read by title_upload_trim::settings_from_env, which copies each value before the next read
    (_dupenv_s on Windows, so the CRT's getenv deprecation warning is gone too).

Tested: Linux ctest 117/117; Windows build cross-compiled with clang-cl 21 and xwin (gta6rt,
title_upload_trim_test), the test passing under Wine 9.0.

@ecox982745 @djrolfes the change is small but it touches what you measured: a re-run of your adapted fixture on
this head would confirm the four buffers still go.


Generated by Claude Code

@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

Note on #2464, which came out after this PR: it lists runtime/shims/d3d12_title_image_upload.h among the files
the performance rewrite owns. @aslan2142 if the rewrite covers the upload ring, feel free to close this or take
the change into it; the rule and its test (src/title_upload_trim.h, title_upload_trim_test) are separate files
and can move as they are. The measured effect is in ecox982745's comment above.


Generated by Claude Code

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 07326dfb8 and re-tested:

  • Both earlier comments are cleanly addressed:
    1. write() now only updates slot.last_used_ns when title_upload_trim::counts_as_use(bytes, trim_settings()) is true (bytes > keep_bytes), so steady small uploads rotating through the ring no longer keep oversized buffers alive indefinitely while in-flight transfers remain protected by the timeline semaphore check in trim().
    2. title_upload_trim::settings_from_env() reads both environment variables via env_copy() (_dupenv_s on Windows, owned std::string copy on POSIX), avoiding sibling std::getenv pointer aliasing and MSVC C4996 warnings.
  • Tested on NVIDIA GeForce RTX 5070 (Windows 11, driver 610.88, Vulkan 1.4.341, MSVC 19.44; VK_LAYER_KHRONOS_validation not installed locally):
    • title_upload_trim: passed (0.02 s).
    • d3d12_mapped_plane_async: passed (0.21 s).

Looks good to merge (maintainers can weigh overlap with #2464 perf work on d3d12_title_image_upload.h).

AI-assisted (Antigravity, Gemini).

@timbot103

Copy link
Copy Markdown
Contributor

Reviewed at head 07326dfb8 on Windows 11 (MSVC 2022 19.44).

Verification

  • Built title_upload_trim_test and gta6rt.
  • title_upload_trim_test: ok.

Notes (non-blocking)

  • Lock ordering (texture mutex -> try upload mutex), retirement gating on slot timeline values (retaining on counter query failure), once-per-mapping capacity accounting, staging classification in the memory probe, and the updates for large-upload-only timestamping + copied env values (d3d12_title_image_upload.h:93-98, 160-299; title_upload_trim.h:53-77; d3d12_vulkan_device.cpp:15661-15668) all look solid.
  • Minor perf note (d3d12_title_image_upload.h:37-47, 160-172; title_upload_trim.h:80-85): The periodic trim evaluates transfer_done(d, slot) (polling vkGetSemaphoreCounterValue) for every non-empty slot even when size/age already disqualify it. Checking size and age first — and querying the shared timeline at most once if any candidate qualifies — avoids redundant Vulkan calls under both locks during steady small uploads.

Approve.

AI-assisted review + local MSVC verification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants