WIP: perf(vk): block-linear textures' write-back swizzle and RGB widening in x86-64 assembly #2136
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
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2136
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/asm-texture"
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?
Summary
Write-backs of block-linear textures are swizzled by suyu's C (
SwizzleTextureinflush_finish), and 3-, 6- and12-byte texels are widened to four channels with an alpha in a C loop (
widen_rows), both ingpu/vk/vk_texture.cpp.This adds
gpu/vk/asm/texture.Son the asm kit:lsasm_swizzle(suyu's atstride_alignment0, with the Switch 2GOB, a line copied 16-byte sector by sector, a sector being whole in both layouts) and
lsasm_widen_rows(alpha 0xFF,half 1.0 or float 1.0), AVX2 and SSE2 bodies.
flush_finish's write-back swizzle callslsasm_swizzle, andwiden_rowsgives the three alphas the kernel writes tolsasm_widen_rows; every other widening (SNORM, UNORM16, theinteger ones) stays in its loop. The write-back swizzle was called with
stride_alignment1 and the kernel is 0's: thesame layout for every bytes per block a write-back has (1, 2, 4, 8, 16;
flushable()keeps the widened formats out), asthe test shows. The result expected: the same texels, those write-backs and widenings faster.
The upload's unswizzle stays suyu's C. This pull request had a kernel for it too (
lsasm_unswizzle, up tod6313013);in play it was slower than the C on the game's shapes (Measurements), so it is dropped and
unswizzle_uploadis as onmain.
No switch: the kernels stand for suyu's C and the widening loop at those two calls, the same bytes.
From 971f8074 on
exp/x86-asm-sync(its fix of the upload's ownstride_alignmentis main's already: #1603). Dependson #1947 (the asm kit, which now carries the GPU-visible ranges' sort,
lsasm_sort_ranges): its commits show in thisdiff until #1947 merges.
Not the same change as #925 (jcabarca's, open): it sends sparse textures from the GPU unswizzle to the CPU path; this one
changes the CPU path's write-back swizzle and widening, and the two merge cleanly.
Read first:
gpu/vk/asm/texture.S, thenflush_finishandwiden_rowsingpu/vk/vk_texture.cpp.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (
CC/CXX,RX_CPMset); heada360ac98on #1947's head
186fdb85(the asm kit, on main9dd41326). Through the PC's job queue (build jobfb-2136n), each run ina hidden console,
tests/run_all.py --strict --exact: the change's own checks and the kit's (log2136n-head-own):test_asm_texture.py,test_texture_tile_upload.py,test_widen_alpha.py,test_asm_abi.py,test_asm_build.py,test_asm_kit.pypass (6 of 6).test_texture_tile_upload.pyat the test commit3f5be891, before the fix, fails as itshould:
Assertion failed: Tegra::Texture::fallback_calls == before + ...(log2136n-tile-test).--changed 186fdb85(log
2136n-head-changed): 74 checks, 64 pass, 2 skip, 8 fail, the same 8 that fail on #1947's head (atcffa2f46, logst-AsmCut5-base-kit-changedandt-AsmCut5-base-kit-more;186fdb85changes onlytests/test_asm_build.py):test_linux_pressure.py,test_low_vram_texture_mips.py,test_pass_barrier.py,test_readback_timeout_source.py,test_staging_burst.py,test_target_reuse_memory.py,test_texel_buffer_ownership.py,test_ub_update_batch.py.It has run in the game on this PC as the two arms of the like-for-like run in Measurements (held job
fb-g2136-sw,Hyrule Field, 16 runs and two warm-ups: this head's unswizzle is that run's C arm, its swizzle and widening its kernel
arm), and
d6313013infb-g2136(19 runs), with no FATAL, device lost or Vulkan error in any run.Still to test:
only ones
lsasm_widen_rowstakes). Hyrule Field has none; such a scene is not yet found (by hand)#1947's head and on this PR, warmed and interleaved, expecting the same picture and frame times within the runs'
spread or better on this PR (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri
Forest and Hyrule Field three minutes each, #1947's head against this PR, warmed and interleaved, frame times and
screenshots)
python3 -I -B tests/test_asm_texture.py --cxx g++and--cxx clang++, expecting PASS (by hand)Measurements
Verdict: the write-back swizzle kernel about 2 times faster in play, in the loads: 0.230 against 0.112 ms a MB
(p < 0.0001), about 1.4 ms over a run (Hyrule Field, one runtime, like for like, at an 800x600 desktop with the output
pinned at 2560x1440, below). The widening kernel was not reached in play so far: the textures that would call it are RGB8
UNORM, RGB16 float and RGB32 float ones widened with an alpha, and Hyrule Field has none. Nothing is shown in frame time.
No new game run is needed for this head: its unswizzle is the C arm and its swizzle the kernel arm of
fb-g2136-sw(below). The micro-benchmark (authored input) has the write-back swizzle 1.21-3.13× faster and the RGB widening 4.8-9.5×
in the SSE2 build and 6.5-28.6× in the AVX2 build.
The unswizzle kernel this pull request had up to
d6313013is dropped: in the same run it was slower than suyu's C wherethe game uses it, by 14.5 % a MB in the loads (p = 0.037, matched work) and 64 % in steady play (p < 0.0001). A
micro-benchmark of it on play's sizes (the same script, its cases replaced: partial tiles of about 17 KB, 4 and 16 bytes a
block, a 16 KB 3D level, and 160-192 KB partial tiles and 3D levels; source bytes warm in the caches, run in the queue
beside builds on 2026-11-23) still had it 1.10-3.44× faster than suyu's C, so the loss in play is not the surfaces'
shapes alone; what it is (cold source bytes from guest memory, the upload's destination) is not known. Its rows stay in
the table below as measured.
Baseline: the C this PR replaces, as #1947's head has it (
cffa2f46for every run below;186fdb85now, the kit's codethe same: the asm kit on main
9dd41326, which this PR depends on), not main itself.The micro-benchmark: suyu's
UnswizzleTexture(..., 0)andSwizzleTexture(..., 1)(asunswizzle_uploadandflush_finishcall them atcffa2f46; suyu'sdecoders.cpp, the same at both commits, built at the arm's level) andwiden_rows' loop cut fromgpu/vk/vk_texture.cppatcffa2f46, againstlsasm_unswizzleandlsasm_swizzlecalled asd6313013called them and itswiden_rows(lsasm_widen_rows); 16 MB of authored texel bytes; the swizzle's destination filled with what is there first, untimed. A script on this PC, not in this PR (asm_texture_bench.py, PrTrials'trials/tools/bench), run alone in the PC's job queue on 2026-11-22: 3 rounds of 201 repetitions per case and arm, i7-9700, llvm-mingw clang 23.1.5-O2 -ffp-contract=off, arms SSE2 (-march=x86-64) and AVX2 (-march=x86-64-v3, what the release builds the back end for). The arms take turns at going first; their outputs were compared at every repetition and were equal. Each cell: the median of the three rounds' medians, [lowest–highest round median], in ns per block or texel. Change: before ÷ after (above 1: this PR faster).cffa2f46d6313013(its head then)In play (held job
fb-g2136, 07:15 to 08:05 UTC on 2026-11-23): Hyrule Field, chosen because it has the most CPUunswizzling of the scenes on disk. 60 s a run. The two builds are the head then,
d6313013(rt-atx-d6313013) and its parent,the asm kit
cffa2f46(rt-kit-cffa2f46): a warm-up each, then kit and head interleaved (kit head head kit, four times),every run loading at create. The output was pinned at 2560x1440, on a desktop at 800x600 at 30 Hz (the PC's only screen
then); the runs are alike among themselves. One kit run (base12) is held: in it the game's first uses waited 2,188 ms in
all for shader loads (the longest wait 585 ms), so it wrote the load marker into the kit's runtime. The marker was moved
out before the next run, and every later run logged loading at create. That leaves 7 kit runs against 8 head runs.
Reach, from a diagnostic build of
d6313013that is local and never pushed (one uncounted run in this scene, countersbeside the 120-frame unswizzle line): over about 12,840 frames,
lsasm_unswizzlewas called 19,895 times (528.6 MB),lsasm_swizzle570 times (12.4 MB) andlsasm_widen_rowsnever.every one of the 570 swizzles.
What the line counts: main's
vk: a frame: X MB unswizzled on the CPU in Y mstimes all of a frame's CPU unswizzling,the unchanged tile fast path (
texture_tiles.h) together with the fallback d6313013's kernel replaced. The kernel carriesabout a third of those bytes in steady play and about 40 % in the loads. So the figures below are the whole stage, not
the kernel's own; they cannot be read as the kernel's speed.
cffa2f46, 7 runsd6313013(its head then), 8 runsa frame's replay time (ms): submitOf the two causes this table left open (the kernel, or the build), the like-for-like run below settles it: it was the
kernel. With one build and only a switch between the arms, the unchanged fast path is level, and the fallback is slower
with the kernel.
Like for like (held job
fb-g2136-sw, 09:3x to 10:33 UTC on 2026-11-23): one runtime,rt-atx-sw. It isd6313013plus two commits of a diagnostic build, local and never pushed: one counts the three kernels' calls, the other adds a
switch,
LONGSHOT_DIAG_TEX_C=1, that sends the three call sites back to the C they replaced (suyu'sUnswizzleTextureand
SwizzleTexture, the widening loop) and times the fallback unswizzle and the swizzle on their own. Hyrule Field, 60 sa run, a warm-up a side, then the C and the kernels interleaved, 8 runs a side, every run at create, the output pinned at
2560x1440 on a desktop at 800x600 at 30 Hz. Two short shader-load waits wrote strike files: the kernels' warm-up 578 ms and
its run on3 1,340 ms. Both were moved out, neither reached 2 s, and on3 is kept (both p values are given where it
matters). The figures are from the diagnostic line: the loads are everything before its last 30 lines, steady play the
last 30 (3,600 frames).
LONGSHOT_DIAG_TEX_C=1), 8 runsIn steady play the kernel runs streamed more and in bigger calls, which a vector kernel should handle better, so the
work's difference does not explain the loss. The calm game time's +2.9 % is not explained and is not the unswizzle: the
kernel's extra unswizzle time in steady play is about 0.03 ms a frame, against 0.4 ms. Each run's calm game time does not
follow how much it streamed (r = 0.01 against the MB unswizzled in steady play, which ran from 229 to 917 MB a run). It
is one figure of 14 at p 0.033, and 0.066 without on3.
Risks
What could break: a texel put in the wrong place would show as a scrambled or shifted texture, in a write-back the
game reads back, or in a widened RGB texture; the test checks the swizzle against suyu's reference
built from its source for every bytes per block, sizes across and off a GOB, block heights 0-5 and 3D depths, ending
at a guard page. A revert is enough. Renderer
files: the runtime is rebuilt; nothing changes for players' saves, options or caches.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
tests/that reads changed source is updated, not weakened.mainand here, for a change that shows on screen.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Possibly related, for context:
@Beanstalk This does the block-linear textures' CPU unswizzle fallback, their swizzle and RGB widening with hand-written x86-64 kernels (gpu/vk), when textures load. It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. No switch: a revert gives back main's code. Please start from the kokiri-start save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Kokiri Forest for two minutes and go into Hyrule Field (one scene load) and play it for two minutes; at least two starts a side, interleaved, the PR against main on the same PC, on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line. No garbled or black textures.@siantm only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was ford631301, and the PR has changed since (a360ac9), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.Measurements: the micro-benchmark (trials script, run alone on this PC 2026-11-22, 3 rounds, SSE2 and AVX2): figures and Verdict
@Beanstalk One correction to my request 69771 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.
@siantm only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was ford631301, and the PR has changed since (a360ac9), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.@Beanstalk This replaces my request 69771 on this head and the corrections under it, in a form the farm can run: it cannot time a picture from the first frame that is not a uniform colour (its answer on #2131), so each picture here follows a fixed wait after the map event. This does the block-linear textures' CPU unswizzle fallback, their swizzle and RGB widening with hand-written x86-64 kernels (gpu/vk), when textures load. It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. No switch: a revert gives back main's code. Please start from the kokiri-start save and take the picture 10 s (600 frames at 60 fps) after the map event that ends the load, a fixed wait with no input in between (the farm cannot time it from the first frame that is not a uniform colour), with a main-against-itself picture on the same PC from the same point; then, for frame times only, play Kokiri Forest for two minutes and go into Hyrule Field (one scene load) and play it for two minutes; at least two starts a side, interleaved, the PR against main on the same PC, on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line. No garbled or black textures.@siantm only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was ford631301, and the PR has changed since (a360ac9), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.d63130135da360ac981cWIP: perf(vk): block-linear textures' unswizzle, swizzle and RGB widening in x86-64 assemblyto WIP: perf(vk): block-linear textures' write-back swizzle and RGB widening in x86-64 assembly@Beanstalk This replaces my request 74258, made for the earlier head
d6313013; this head is narrower (the upload unswizzle is main's C again). It does the block-linear textures' write-back swizzle (when the renderer writes a texture back to guest memory) and the RGB8, RGB16F and RGB32F widening on upload with hand-written x86-64 kernels (gpu/vk); the swizzle runs only during loads, and the widening was not reached in Hyrule Field on the author's PC. It sits on #1947 (186fdb85, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. No switch: a revert gives back main's code. Please start from the kokiri-start save, which already puts Link in Kokiri Forest (no teleport to Kokiri Forest is needed), and take the picture 10 s after the map event that ends the load, a fixed wait with no input in between, with a main-against-itself picture on the same PC from the same point; then, for frame times only, play Kokiri Forest for two minutes and go into Hyrule Field (one scene load: a teleport asked after about 80 frames of play, its arrival checked) and play it for two minutes; at least two starts a side, interleaved, the PR against main on the same PC, on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line. No garbled or black textures.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.