WIP: fix(vk): a revisited area's ASTC textures take the finalized copies' images, not a decode #1222
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1222
Loading…
Reference in a new issue
No description provided.
Delete branch "TrashPanda/Longshot:fix/astc-decode-reuse"
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?
Type: fix · Risk: medium · Tested on: Linux, RTX 4070 Ti · Needs: AMD (RADV), Windows, any GPU
Why
Going back to an area you just left is no smoother than the first visit, and the GPU memory it holds keeps growing.
The game puts an area's textures at new addresses each time it loads them, and the renderer's texture cache is keyed
by address, so every revisit decodes the same ASTC textures again (0.8 to 1.2 GB a load) and keeps the old decoded
copies until the budget evicts them (#1069). On cards that don't sample ASTC (every desktop NVIDIA and AMD card) that
is a decode and often a BC3 re-encode per load, and on 8 GB cards the duplicates push the budget into evicting
textures that are still in use.
How to reproduce
Seen on: Linux, RTX 4070 Ti 12 GB, NVIDIA 595.99.02 (and in #1069)
Settings and switches: defaults;
RX_VK_VRAM_MB=8192to reach the budget as an 8 GB card doesSteps: from the repository save
kokiri-start, teleport Kakariko, the Market, Kakariko, the Market (scripts/memtour.py --repo-save kokiri-start --dests 2,3,2,3,2,3,2,3)Expected: a revisit uses the textures the first visit decoded
Actual: every visit decodes them again:
vk: textures: decoded ASTCclimbs to 3.2 to 3.7 GB and the budget retires about 23,000 textures in a 6-minute tourWhat changed
A new ASTC texture's blocks are hashed on its first upload. When the game finalizes a decoded ASTC texture, it is
listed by that hash; a later texture with the same blocks and the same shape takes its image by a GPU copy instead
of a decode, and the old one is retired at the next frame.
RX_VK_ASTC_REUSE=0turns it off.gpu/vk/astc_reuse.h(new): XXH64 over the blocks (Hasher), the shape that has to match (Shape: format,size, levels, layers, layout, BC or not), and the list of finalized textures by hash (
Freed).gpu/vk/vk_texture.cpp:reuse_upload(), fromupload_inner()'s ASTC path, hashes a texture's first whole upload, and copies a listedtexture's image into it when one matches.
reuse_released(), fromtexture_released(), lists a finalized texture.reuse_retire_frame(), fromtexture_frame()afterdrawn_writeback_frame(), destroys the textures whose imagewas taken.
reuse_drop()unlists a texture used again at its own address, or going.gpu/vk/vk_texture.h:Texture::blocks_hash,freed_listed,reuse_retire.tests/test_astc_reuse.{py,cpp}: XXH64 against its published vectors, streaming against one call, and the shape match.tests/test_drawn_page_map.py: its fixture stubsreuse_released(), which thetexture_released()it compiles now calls.docs/development.md: the switch.docs/tested/: the two runs below.Measurements
Verdict: helps. Over three interleaved tours a side, the peak of decoded ASTC in VRAM is 29% lower, VRAM in use at
its peak is 506 MB lower, textures retired for the budget are 31% fewer, and the worst frame of a tour is 23% shorter.
Host memory is unchanged. One cost: hashing takes about 0.5 s of the replay thread over a tour (6 GB of blocks), which
fills the 40 ms upload budget sooner during a load, so more new ASTC textures wait a frame or more (median 537 against 85).
Hardware: Ubuntu 26.04.1 LTS (kernel 7.0.0-43), RTX 4070 Ti 12 GB, NVIDIA 595.99.02, Ryzen 7 9800X3D, 32 GB
Scene: Kakariko and the Market (child), four visits each, walking 25 s in each; 2560x1440, headless, defaults
Method:
scripts/memtour.py(memory sampled every 2 s), the numbers from each run'slongshot_log.txt: the peak andmedian of
vk: textures: decoded ASTC, the peak ofvk: VRAM: N MB used, the sum oftextures retired ... budget N,the largest
vk: status: 600 frames, the slowest N ms, the lastnew ASTC textures left for a later frame ... N so far.Three runs a side, interleaved off, on, off, on..., each through
scripts/job_queue.pytiming mode (alone on the PC).The median of the three.
How to reproduce:
RX_VK_VRAM_MB=8192 python -I scripts/memtour.py <game> --repo-save kokiri-start --dests 2,3,2,3,2,3,2,3 --dwell 25 --settle 20, with and without--env RX_VK_ASTC_REUSE=0Both columns are the same build: the renderer built alone (
RX_VK_ONLY=1) from 64b1ad0c, this change on main71b1edd8with #1220 and #1221.
Baseline:
RX_VK_ASTC_REUSE=0instead of main, so that both sides are the same build; switched off, the change hashesand lists nothing and every upload takes main's path.
Rebased on main
0cab3548since. One more pair on the rebasedcode (da36d129: this PR's code but for where one block sits in the file, with #1220 and #1221): decoded ASTC at its
peak 3,189 → 2,897 MB, textures retired for the budget 21,266 → 15,314, the slowest frame 684.0 → 490.5 ms; VRAM used
at its peak went the other way in that pair, 8,676 → 8,976 MB. A tour on the PR's head (51595f27) reused 4,819
textures and reached all 8 areas; its frame times aren't comparable (another program held 5 GB more during it, 1.9 GB
of RAM free).
RX_VK_ASTC_REUSE=0The second Kakariko visit, the same spot, reuse off and on, are in a comment: the textures are the same (only
the windmill and Navi, which move, differ).
Flags and settings
One new switch, on by default.
RX_VK_ASTC_REUSE=0offTesting
Tested:
python -I -B tests/run_all.pyon Linux at the head: 25 checks fail, the ones that fail onmain@0cab3548onthis PC (its toolchain lacks what they need; listed in a comment), but for
tests/test_control.py, which timed outin the suite (196 s) and passes alone, twice.
tests/test_astc_reuse.pyis new and passes. In the game: Ubuntu26.04.1, RTX 4070 Ti, NVIDIA 595.99.02, nine tours and a screenshot run with reuse on, no device lost; the textures
checked by eye only, in the screenshots: the second visit's frame matches the decoded one.
Not tested: AMD and Intel cards (ASTC decoded and re-encoded to BC3 on the GPU there too, the same path); Windows;
the Steam Deck (its GPU samples no ASTC either, and it has the least VRAM); an adult save's areas; a long play session
instead of a 6-minute tour. The Windows build (llvm-mingw) was not made here:
astc_reuse.his standard C++ only.Must be tested:
RX_VK_ASTC_REUSE=0, expectingvk: decoded ASTC used again: N textureswith N in the thousands,decoded ASTClower, no texture wrong after a revisit (Test Farm: @Beanstalk linux radv, a memtour of Kakariko and the Market with RX_VK_ASTC_REUSE=0 and without)textures retired ... budget(Test Farm: @Beanstalk steamdeck, the same tour)Risks
What could break: a texture shown with another texture's image. That needs two textures of the same shape whose
blocks hash the same but differ (a 64-bit collision), or a texture whose blocks change between its hash and its
first draw (the hash is of the blocks as they are uploaded, the same bytes the decode reads). Textures the GPU has
written are never listed or reused. Also: textures waiting a frame more in a load (above).
Turning it off:
RX_VK_ASTC_REUSE=0.Compatibility: Nothing changes: no cache, options or ABI change.
Issues
Fixes #1069
Checklist
Everyone
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.docs/tested/for each new measured run (format indocs/tested-configurations.md).mainand passing here.tests/that reads changed source updated in this PR, not weakened.Tested in game: Ubuntu 26.04.1, RTX 4070 Ti, NVIDIA 595.99.02, RX_VK_VRAM_MB=8192: three interleaved tours a side of Kakariko and the Market (memtour.py), reuse off and on; with it on ~5,900 textures a tour copied instead of decoded, decoded ASTC peak 3.6 -> 2.6 GB, budget evictions -31%, worst frame 637 -> 487 ms; the revisit's textures look the same (screenshots in a comment); no device lost.
The second Kakariko visit (Kakariko, the Market, Kakariko; the screenshot 300 frames after arriving, no walking), the same build (51595f27's renderer over game v3.0.0-rc.5-26-g71b1edd8), RTX 4070 Ti, 2560x1440. First:
RX_VK_ASTC_REUSE=0(decoded). Second: reuse on, 343 textures (642 MB) copied, not decoded. The textures match; the windmill and Navi move.Checks that fail on
main@0cab3548on this PC (Ubuntu 26.04.1; its toolchain lacks what they need),python -I -B tests/run_all.py:tests/test_audio_output_devices.pytests/test_bug_report.pytests/test_bug_report_defaults.pytests/test_crash_record.pytests/test_crt_options.pytests/test_dlss_feature_size.pytests/test_failed_compile_keeps_objects.pytests/test_fsr_uniform_ownership.pytests/test_guest_imported.pytests/test_hardware_report.pytests/test_import_reach.pytests/test_lift_lookups.pytests/test_linux_preflight.pytests/test_linux_update.pytests/test_make_game_cache.pytests/test_missing_nup.pytests/test_mods_size_table.pytests/test_nonuniform_image_arrays.pytests/test_parallel_init_order.pytests/test_pending_guest_import.pytests/test_replay_write_order.pytests/test_run_all_changed.pytests/test_vk_occlusion_queries.pytests/test_walk.pytests/test_walk_key.pyThis PR's run fails the same ones and nothing else (
tests/test_control.pytimed out once in the full run on #1222's branch and passes alone).@Beanstalk test the decoded-ASTC reuse on AMD (Linux, RADV), on a Steam Deck, and on Windows with any GPU: from the repository save kokiri-start, teleport Kakariko, the Market, Kakariko, the Market (four visits each, walking about 25 s in each), once with RX_VK_ASTC_REUSE=0 and once without it. Compare with main: the log's "vk: decoded ASTC used again: N textures" (N in the thousands with it on), the peak of "vk: textures: decoded ASTC N MB", the sum of "textures retired ... budget N", and the slowest frames after each teleport. Take a screenshot on the second Kakariko visit each way: the textures must look the same. Any wrong or swapped texture after a revisit is a failure.
@TrashPanda only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.