Skip to content

Textures: staging buffers come from a pool instead of being made per texture - #781

Open
marquezinii wants to merge 1 commit into
fxceless:mainfrom
marquezinii:perf/staging-pool
Open

marquezinii wants to merge 1 commit into
fxceless:mainfrom
marquezinii:perf/staging-pool

Conversation

@marquezinii

Copy link
Copy Markdown
Contributor

The pass over the heap's textures (draw.rs heap_textures) runs on the thread that records the frame, with a budget of 250 ms and 192 MB a pass, three times that while 1,024 or more slots are due. On a Ryzen 5 5600X + RTX 5070 the queue held about 5,000 due slots for minutes, so frames sat at that budget (0.7-1.4 s) whenever it ran full.

I split the pass with local counters. In the 111 heaviest lines of a run, staging was 257 ms of the median 392 ms of the texture part. Most of it was staging() making an upload buffer for each texture (Host::upload_buffer, a committed resource and a map) and releasing it when the texture was let go a frame later. A small benchmark of 1,000 buffers / 350 MB on the 5070:

ms per MB
create + map 1.9
release 1.17
create + map with D3D12_HEAP_FLAG_CREATE_NOT_ZEROED 1.9 (no change)
write a buffer that already exists 0.36

This change keeps those buffers in a pool:

  • A Staging that is dropped gives its buffer back. The lists that recorded a copy from it hold the Arc until they are reset, so the card is done with it by then.
  • Buffers are kept by size class: whole 64 KiB up to 1 MiB, then eighths of a power of two (at most an eighth unused). A texture takes the buffer of its class given back last.
  • With the pool full, the oldest buffer of the class holding the most bytes is released. Most staging buffers live a frame, so the newest are the ones asked for again. A first version that refused new buffers when full reused only 5 %; this one reused 77 % in the same scenes.
  • A buffer taken from the pool holds another texture's texels. detile writes every subresource (including the zeros for levels on demand), and if the first refresh does not detile at all (memory all zero, or not there yet) the buffer is zeroed, as a new one would be.
  • VICTORY_STAGING_POOL_MB (512) caps it; 0 makes and releases every buffer as before. The diagnostics memory line counts buffers made, taken, given back and released.

Measured on 310c6eb plus #95 and #665 (the remap and fetch waits hid this otherwise), diag build, Series X, 480 s runs with the same scripted input, one exe with the switch on and off:

VICTORY_STAGING_POOL_MB=0 pool on
staging time per slot looked at 0.112 ms 0.036 ms
lines with the texture part over 150 ms 46 23
buffers made / taken from the pool 56,021 / 0 14,781 / 50,093

The queue drained a few minutes into the world in the runs with the pool. After that the pass costs 15-40 ms a frame. Frames in the steady door scene of the prologue are bound by the card either way (about 2 fps there with these two runs), so this shows up while textures stream in, not in the steady frame. I compared the captured frames (VICTORY_CAPTURE) of both runs at the same offsets and saw no texture with stale texels.

Tests: two new unit tests (size classes; a buffer given back is taken again for its class and not for another). cargo test --release -p victory-gpu passes, except inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset. That test also fails on a26859f without this change when the whole suite runs (3 of 3 runs here) and passes alone. Clippy shows no warnings in the changed files, in both CI configurations. Rebased on a26859f; levels on demand were not enabled in the measured runs.

…texture

A title texture's staging buffer was an upload buffer made for it and
released with it. On an RTX 5070 making and mapping one costs about 1.9 ms
a megabyte and releasing it 1.2 more, whatever the heap flags, against
0.36 to write one already there. The pass over the heap's textures stages
up to 192 MB a frame (576 MB while it catches up) on the thread that
records the frame, so most of its 250-750 ms went to that.

A staging buffer let go now goes back to a pool by size class (whole
64 KiB up to 1 MiB, then eighths of a power of two), and the next texture
of that class takes the one given back last. With the pool full, the
oldest buffer of the class holding the most bytes is released to make
room: most staging buffers live a frame, so the newest are the likeliest
to be asked for again. A buffer taken from the pool that the first
refresh does not detile into (memory all zero, or not there yet) is
zeroed, as a new one would be.

The pool keeps at most VICTORY_STAGING_POOL_MB (512); 0 makes and
releases every buffer as before. The memory line counts buffers made,
taken from the pool, given back and released.
@marquezinii

Copy link
Copy Markdown
Contributor Author

The red check is the Clippy step failing on three lints that main has as well (its run for a26859f fails on the same ones): placed_filler_twins never used (resource.rs:2553), sort_unstable_by_key (draw.rs:2387), depth_written never read (indirect.rs:1335). This PR only changes texture.rs and copystat.rs.

ExoCalibra added a commit to ExoCalibra/victory-exo that referenced this pull request Nov 25, 2026
clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…1887) & staging pool (#781)

Wave 11D fixes four d3d12_vulkan_device.cpp hot-path regressions (GPL-3.0, zero non-trivial thread_local):

1. Page-dirty-only console descriptor heap diff + AVX2/SSE2 compare:
   - Extend leonida::title_shadow::Shadow::refresh() (runtime/src/title_shadow_sync.h) with optional std::vector<DirtyRange>* output recording exact changed 64 KiB page slices (page index, byte offset/length, zeroed flag).
   - Update DeviceState::refresh_console_copy() to scan ONLY changed 64 KiB pages (instead of all 32 MiB / 1M slots when 1 page changes), skip zeroed pages on initial refresh, fast-clear aligned zeroed pages, and compare 32-byte descriptors via AVX2/SSE2 (equal_descriptor_32 / is_zero_descriptor_32) instead of scalar memcmp.

2. Eliminate uncached WC arena.mapped reads in scan_title_table & host-RAM V# bitset:
   - Add leonida::ConsoleHeapVsharpSlots (runtime/src/console_heap_vsharp_slots.h, adapted from ubiquitarianism/leonida#1887, author IlyassRS3) tracking live console-heap buffer V# slots in L1-resident host RAM.
   - Skip console heaps in scan_title_table() (already refreshed & sanitized from cached host RAM by refresh_console_heaps() in before_execute_()).
   - Add a 128-entry per-submission epoch span cache for layer shader-visible heaps (invalidated on before_execute_(), heap create/destroy, and arena descriptor API writes) and stream-copy cache misses from WC arena.mapped via SSE4.1 MOVNTDQA (_mm_stream_load_si128) in 64-slot chunks, writing back only modified slots.

3. Bounded LIFO size-classed texture upload staging buffer pool:
   - Adapted from fxceless/victory-fork#781 (GPL-3.0, author marquezinii): 256 MiB cap, 64 KiB steps up to 1 MiB then 8 steps/power-of-two, separate upload vs cached-readback buckets, LIFO reuse, fullest-class oldest eviction outside the pool lock, and clean teardown in ~Device().

Note: ubiquitarianism/leonida#1926 (TitleImagePool / vulkan_title_image_pool.h) does not apply because leonida-ultimate uses VulkanSuballocator (vulkan_suballoc.h, outside W11D file boundary).

Verified (RTX 3080, VK_VALIDATION=1, 0 validation errors):
- 6/6 GPU CTests passed: d3d12_vulkan_device, d3d12_xmem_shadow, d3d12_unmapped_vsharp, d3d12_subresource_copy, d3d12_console_descriptor_heap, d3d12_console_table_draw
- Dirty-page scan: 1-slot write in 16-page heap scanned 1 page (2,048 / 32,768 slots); idle submit scanned 0 pages
- Staging pool: 1 created / 3 reused in d3d12_unmapped_vsharp; 2 created / 38 reused in d3d12_subresource_copy
- title_shadow_sync_test: 16/16 new dirty-range & ConsoleHeapVsharpSlots checks passed (84/85 total; 1 pre-existing line-139 coalesced-run split check from 8cd13ce)
- check_no_game_data.py: 0 violations; test_docs_env_vars.py: 4/4 OK
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.

1 participant