Skip to content

gfx10_descriptors: index the format table instead of scanning it (~20x faster SRV format lookups) - #8021

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/runtime-vulkan-hotpath
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/runtime-vulkan-hotpath

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claim: self-proposed perf item, claimed in #7804 (#7804 (comment); no item ID in the tables). Depends on: none.

What

format_row, vulkan_format and vulkan_view_format in runtime/src/gfx10_descriptors.h walked the 94-row format_table on every call. One SRV/UAV view makes five of these lookups (format_from_vulkan, vulkan_view_format, format_swizzle, vulkan_swizzle, vulkan_format; see d3d12_vulkan_device.cpp around 4528–4579), and the T# → set 0 binding (bind_image, ~1136) does two of them while holding descriptor_mutex.

The table is now indexed at compile time:

  • by Vulkan format for the core range (< 256). The VK_EXT_4444_formats rows above that range (A4R4G4B4/A4B4G4R4, 1000340000+) and unknown values still use the scan.
  • by 9-bit GFX10 code: the first row and the row count. vulkan_view_format now visits only the rows of its code.

Behaviour is unchanged. Each lookup still returns the first row in table order, as the scan did. A static_assert keeps all rows of one code next to each other, so [first, first + count) holds exactly the rows the scan would have matched. Out-of-range codes (>= 512) give VK_FORMAT_UNDEFINED, as before. The table itself is not edited, and no open PR touches these lines (I checked #3128, #5170 and #5666, which edit other parts of the file).

Numbers

runtime/test/gfx10_format_bench.cpp (new) times the call sequence of one SRV view over a mix of 18 common frame formats (BC1–7, RGBA8/sRGB, BGRA8, RGBA16F, R11G11B10, RGB10A2, R32F, D32→R32F, a cast). It runs the new lookups against the old linear scans, which the bench keeps as the reference. Release build, llvm-mingw clang 20261117, median of 15 runs × 2,000,000, three invocations:

run 1 run 2 run 3
table scan (before) 111.13 ns 113.83 ns 110.53 ns
indexed (after) 4.88 ns 5.48 ns 6.22 ns
speedup 22.8x 20.8x 17.8x

So each SRV view's format work drops by about 95%. This is CPU time on the descriptor-creation path, and part of the time bind_image holds the descriptor lock. It is not per-draw time. Creating the Vulkan image view, on a bind_image miss, still dominates that path.

Tests

  • gfx10_descriptors_test adds checks against a first-match scan for every table row, and checks that every unlisted code < 1024 and unlisted format < 1024 resolves to UNDEFINED/null. It also pins edge cases: vulkan_format(5) == R8_UINT (not S8), the 4444 extension formats, 0x7FFFFFFF, 0xFFFFFFFF, and second rows of a code (B8G8R8A8_UNORM → B8G8R8A8_SRGB under 130). Passes.
  • gfx10_format_bench --self-test (new ctest gfx10_format_bench_selftest) compares old and new on 502 formats × 1,103 codes × cast/no-cast, 1,107,412 vulkan_view_format queries in total. Also compared: format_row, format_from_vulkan, format_swizzle, vulkan_format. All agree.
  • Full CTest (-DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, llvm-mingw, Ninja, Release): 67 of 68 pass. vk_queue_query_sync failed once under ctest -j8 and again in a --repeat until-fail:5 run, but passed on its own. That test does not include gfx10_descriptors.h (checked with ninja -t deps), so it is not affected by this change.
  • scripts/run_gpu_tests.py (full run, ~95 s): 223 of 241 passed, 17 skipped, 1 failure (ms_wave_info_s3). The same failure shows on unmodified origin/main with the same build (--filter ms_wave_info, run twice), so it was there before this change.

Tested on AMD Radeon RX 6800 (gfx1030), AMD Software 26.11.2, Windows 11 Pro.

AI-assisted (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

format_row, vulkan_format and vulkan_view_format walked the 94-row
format_table on every call; an SRV makes five of these lookups
(format_from_vulkan, vulkan_view_format, format_swizzle, vulkan_swizzle,
vulkan_format), and the T# -> set 0 binding does two of them under the
descriptor lock. The table is now indexed at compile time: by Vulkan
format for the core range (< 256, the VK_EXT_4444_formats rows above it
keep the scan) and by 9-bit GFX10 code (first row and row count). Each
lookup returns the first row in table order, as the scan did; a
static_assert keeps the rows of one code adjacent so the code's range is
exactly the rows the scan visited.

gfx10_format_bench times the lookups against the old scans (kept in the
bench as the reference) and its --self-test, run by ctest, checks both
agree on 502 formats x 1103 codes x cast/no cast. On a Ryzen host the
SRV lookup sequence goes from ~111 ns to ~5 ns (about 20x).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TieruYT

TieruYT commented Nov 23, 2026

Copy link
Copy Markdown

Windows, synthetic inputs. Tested c77db34.

I compiled gfx10_format_bench with MSVC x64 (/O2 /std:c++20) and ran the CPU self-test. The reference and indexed paths agreed across 502 formats, 1103 codes and 1,107,412 view-format queries. In the bundled per-SRV benchmark the median of seven runs of one million iterations was 73.33 ns for the scan and 3.42 ns for the index (21.5x). This measures just the lookup sequence.

AI-assisted verification · GPT-6.2 Astra Medium

@justrealyme

Copy link
Copy Markdown

Tested c77db34 on NVIDIA GeForce RTX 5070 Ti, driver 617.42, Windows 11 build 26300, VS 2022 Build Tools (MSVC 19.44), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, RelWithDebInfo: pass.

Built gfx10_descriptors_test and ran ctest -R ^gfx10_descriptors: Passed.

Synthetic inputs only, no game files. AI-assisted (Claude Code).

@rideinstead

Copy link
Copy Markdown

TEST validation — Approve

Validated on CPU (no GPU required for the test itself).

Tests added: runtime/test/gfx10_format_bench.cpp + self-test ctest gfx10_format_bench_selftest; gfx10_descriptors_test extended (edge cases: unlisted codes < 1024 → UNDEFINED/null, 4444 extension formats, 0x7FFFFFFF, 0xFFFFFFFF, second rows of a code).

Verification:

  • TieruYT (MSVC x64 /O2): CPU self-test passed; reference and indexed paths agreed across 502 formats × 1,103 codes = 1,107,412 vulkan_view_format queries. Benchmark: 73.33 ns scan vs 3.42 ns index (21.5x).
  • justrealyme (MSVC 19.44, RelWithDebInfo): ctest -R ^gfx10_descriptors passed, synthetic inputs only, no game files.
  • Author's full CTest: 67/68 pass (1 pre-existing failure vk_queue_query_sync, confirmed unrelated). run_gpu_tests.py: 223/241 pass, 17 skipped, 1 pre-existing failure.

Performance: SRV lookup sequence ~111 ns → ~5 ns (~20x speedup).

Note: Author tested on AMD Radeon RX 6800 (gfx1030), but the change and tests are CPU-only — GPU was context only.

Recommendation: Approve / merge. CPU-validatable, thoroughly tested (1.1M cross-check queries), performance confirmed by independent reviewers.

@Kerwinss

Copy link
Copy Markdown

Tested c77db34 on Windows 11 Pro 10.0.26300, MSVC 14.51 (VS Build Tools 2026), Windows SDK 10.0.26100, NVIDIA GeForce RTX 4060 / 621.24, Khronos Vulkan headers via -DLEONIDA_VULKAN_HEADERS_FETCH=ON (no glslangValidator): pass, no regression.

Merged onto main dde82f2 (clean), full runtime build, ctest -C RelWithDebInfo -j6: gfx10_descriptors, gfx10_format_bench_selftest and gfx10_tiling pass. In the parallel run, loader failed on its own Defender check ("PE images written to %TEMP% ... 42 ms on average"), which is antivirus timing under load, not this change: run alone it passed 3/3 with this PR. The two failures are the ones main has on this GPU: d3d12_depth_resolve (#7985) and d3d12_vulkan_device:6574 (claimed by iTzAlvar).

AI-assisted (Claude Code). No game files.

@Ekabusto

Copy link
Copy Markdown

Tested c77db34a6 on Windows 11 10.0.26300, AMD Ryzen 9 5900X, MSVC 19.44 RelWithDebInfo: pass, and the speedup
holds
.

  • ctest -R gfx10: 3/3 passed (gfx10_tiling, gfx10_descriptors, gfx10_format_bench_selftest).
  • gfx10_format_bench: "self-test: 502 formats, 1103 codes, 1107412 view-format queries agree". Per SRV view, median
    of 15 runs of 2,000,000: table scan 115.86 ns, indexed 5.49 ns (21.1x).

AI-assisted: yes (Claude ran the build and tests; results checked by me).

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