Skip to content

Runtime: put the descriptor table in device-local memory (D3D-76) - #696

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
SuarezPM:device-local-descriptor-table
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
SuarezPM:device-local-descriptor-table

Conversation

@SuarezPM

@SuarezPM SuarezPM commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

D3D-76 (see #313). Depends on: #639, whose benchmark this measures with.

Every T#/S# lookup in a translated shader probes the table at set 0 binding 2. BindlessSet allocated it HOST_VISIBLE | HOST_COHERENT and flush() memcpy'd into the mapping, so divergent lookups read it over PCIe. Measured with vk_descriptor_bench on an RTX 2060 SUPER (Linux, 620.73.04, Vulkan 1.4.357; default sizes, full run, net wave µs/1M lookups):

entries capacity / load divergent miss DEVICE_LOCAL divergent miss HOST_VISIBLE ratio
1,000 4,096 / 24.4% 1,288 1,310 1.0×
10,000 65,536 / 15.3% 3,169 4,309 1.4×
100,000 524,288 / 19.1% 4,585 61,984 13.5×

T#+S# pairs at 100k: 757 vs 3,145 (4.2×). The penalty scales with the table's footprint: on the pre-relocation 2M-slot / 80 MiB table the same benchmark measured 46.2× and 19.5×. main's relocation strategy (make_probe_slot) has already brought the 100k table to 524,288 slots / 20.00 MiB / 19.1% load, which is the size this run uses; @rafaelpssz measured 121.5× / 89× at that same 20 MiB size on Windows/Ampere, so the placement still matters and stays in SHADER-100.

What changed

  • The table is allocated DEVICE_LOCAL | TRANSFER_DST (with HOST_VISIBLE | HOST_COHERENT kept as a fallback for a device without device-local memory), plus a mapped staging buffer.
  • flush(VkCommandBuffer) uploads only the slots whose entries changed — the whole table when it grew or was never uploaded — records vkCmdCopyBuffer and a TRANSFER_WRITE → SHADER_READ barrier in the command buffer that set_bindless_prepare already hands it. No API or shader-contract change.
  • DescriptorTable now records the slots that insert, replace, erase, clear and grow touch (including both slots of a relocation), so a flush no longer rebuilds an 80 MiB table for one new descriptor.
  • Slot changes go through vkCmdUpdateBuffer, which copies into the command buffer while recording; the mapped staging buffer is written only for buffers that were just created (first use or growth), so a copy still in flight never reads bytes the CPU rewrites.
  • A rejected vkQueueSubmit keeps the upload pending: VulkanQueue::execute reports the submit result back (set_bindless_commit), and the set forces a full table upload on the retry (table_upload_result(false)), which also reports flush_needs_idle(). @Vonsek's recovered-submit case on the first revision (aa21d11) is the regression test.
  • The device now honours flush_needs_idle() (it ignored it before): replaced/erased entries and growth wait for the queue before flushing, and retired buffers are freed once the device is idle instead of at teardown.

Verified on NVIDIA RTX 2060 SUPER, Linux 620.73.04, Vulkan 1.4.357:

  • ctest: 10/10, including a new descriptor_table case for the change list (insert/replace/erase/grow, consume-once, read_slot, out-of-range refused).
  • vk_bindless_test with LEONIDA_VK_VALIDATION=1: 14 draws (probe depth, erase, growth 64 → 1024 slots with the binding rewritten, rebind, a failed-submit retry, other view, removal) read back exact, and both descriptor-indexing configurations report the table in device-local memory. Mutation check for the retry: with it disabled, the failed-submit draw reports 8192 bad pixels.
  • GPU suite on the rebased head (762893f): 64/91 pass with the same 27 skips as main (all 64-wide subgroup), 0 failures; 91/91 with LEONIDA_W64_EMULATE=1.

The anchor the shader contract advertises is untouched: desc_probe in tools/rdna2spirv.py and the 10-dword entry layout are the same. AI-assisted. No game data; synthetic fixtures only.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

A recoverable queue-submit failure loses a newly registered SRV on aa21d11. The next ExecuteCommandLists succeeds, but the shader reads the fallback for all 64 texels. The same retry on parent c432cdc returns the intended texture.

I extended the synthetic d3d12_descriptor_test locally. After its existing 8×8 texture and set 0 had been uploaded and sampled successfully, I created a second SRV on that texture with R and B swapped. I then executed an empty closed D3D12 list twice, without changing the descriptor in between. For the error case, a proc-address wrapper returned VK_ERROR_OUT_OF_HOST_MEMORY on the first vkQueueSubmit without forwarding it, then forwarded every subsequent call. The runtime reported unsupported for that first execute, success for the retry, and the subsequent queue fence completed. The existing compute shader sampled using the new SRV bytes and the original wrap sampler on the runtime's set 0.

Revision Both submits succeed One submit rejected, then retry succeeds
c432cdc (parent) 64/64 expected 64/64 expected
aa21d11 (PR) 64/64 expected 0/64 expected, 64/64 matched the separately sampled unknown-key fallback

At (0,0) the expected swapped RGBA8 was (128,0,0,255); the fallback was (0,0,0,0). RTX 5080, Windows, driver 616.92, Khronos validation enabled. The stock test passed on both revisions. The upload, retry, sampling and readback ran on the real device. The single submit error was injected.

upload_table() consumes take_changes() and clears table_dirty_ during command recording, before VulkanQueue::execute knows whether the transfer was submitted. Retaining the upload obligation until successful submission, or forcing a full table upload after a rejected submit, would let this retry recover. The test primes the fallback and table first, so the finding is specific to a later incremental insertion.

@SuarezPM
SuarezPM force-pushed the device-local-descriptor-table branch from aa21d11 to f6f9dea Compare November 22, 2026 02:09
@SuarezPM

Copy link
Copy Markdown
Contributor Author

The one failing check is check → Build rpf9dump: ArchiveEntry has no ResourceDrawableOffset (CS1061 at tools/rpf9dump/Program.cs:124 and :147). It reproduces on main's own CI run 44974310144 on 762893f, so it comes from the rpf9-explorer re-pin, not this branch.

Everything that touches this change is green: linux-gpu-tools (llvmpipe: the copy path and barrier run there), windows (MSVC build of the runtime), semantic-tests both OS, translator-tests, viewer-tests, change-scope, no-game-data.

@troythedecoyx

Copy link
Copy Markdown
Contributor

Tested f6f9dea merged onto main 762893f on NVIDIA GeForce RTX 4060 Ti (Ada), driver 32.0.16.2124 (NVIDIA 621.24), Windows 11 Home 26300, MSVC 2022 / Ninja, Vulkan SDK 1.4.363: pass.

  • descriptor_table_test (changed here): exit 0, all tests passed (0 failures).
  • vk_bindless_test (changed here) through run_gpu_tests.py --filter ps_tex: PASS, 14 draws read back, 0 bad pixels, largest error 0 (same as main).
  • ctest: 28/28.

AI-assisted.

@SuarezPM
SuarezPM force-pushed the device-local-descriptor-table branch from f6f9dea to db3dc92 Compare November 22, 2026 02:21
@SuarezPM

Copy link
Copy Markdown
Contributor Author

Reproduced the shape of it and fixed it in db3dc92 — thank you, this was a real hole and your cause analysis is exact: upload_table() consumed take_changes() while the command buffer was still being recorded, so a submit that never reached the queue left the table in VRAM as it was, and the retry had nothing left to record.

Fix: VulkanQueue::execute now reports the submit result through a commit callback (VulkanContext::set_bindless_commit). On a rejected submit the set keeps the obligation and forces a full table upload on the next flush (BindlessSet::table_upload_result(false)); on success it clears it. That covers both a real vkQueueSubmit error and the prepare-failed path, where the command buffer is submitted but the call still reports an error.

vk_bindless_test now runs your sequence without needing fault injection: after the gradient is restored it inserts the decoy mapping, records the upload in a command buffer that is never submitted, calls table_upload_result(false), and draws. It expects the decoy colour. Mutation check: with the retry disabled, that draw reports 8192 bad pixels; with the fix, 0. (It does not inject an OOM at vkQueueSubmit; if your extended d3d12_descriptor_test can be re-run on db3dc92, that is the one input I cannot reproduce locally.)

New head: db3dc92, rebased onto 762893f. On an RTX 2060 SUPER / 620.73.04: vk_bindless_test under validation 0 bad pixels, ctest 10/10, GPU suite 64/91 + 27 64-wide skips (0 failures) and 91/91 emulated. Thank you for the 5080 table — it is what made the window obvious.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Re-ran the original submit-injection probe on db3dc92, RTX 5080 / NVIDIA 616.92, Windows 11, Khronos validation enabled. The wrapper returned VK_ERROR_OUT_OF_HOST_MEMORY once without forwarding the first vkQueueSubmit. The first execute reported unsupported, then the unchanged closed list succeeded and its fence completed. The shader read the expected swapped SRV for 64/64 texels; zero matched the unknown-key fallback. Pixel (0,0) was (128,0,0,255). Before the fix, including on f6f9dea, the same retry returned (0,0,0,0) for all 64.

The no-injection control also matched 64/64, the stock descriptor test exited 0, and the validation layer reported no errors. This verifies the commit callback on the normal queue submission path for the incremental insertion case I reported.

@SuarezPM
SuarezPM force-pushed the device-local-descriptor-table branch from db3dc92 to c0ec562 Compare November 22, 2026 02:29
AI-assisted. Set 0 binding 2 is the table every T#/S# lookup probes.
BindlessSet allocated it HOST_VISIBLE | HOST_COHERENT and flush() memcpy'd into
the mapping, so a divergent lookup read it over PCIe: 19.5x slower for T#+S#
pairs and 46.2x for a divergent miss at 100k entries on an RTX 2060 SUPER
(vk_descriptor_bench, ItsNotRexed#639; 58-796x on the 5080).

The table now lives in DEVICE_LOCAL (HOST_VISIBLE kept as a fallback for a device
without it). flush() takes the command buffer that will use the set and uploads
only what changed: the whole table from a mapped staging buffer when the buffers
were just created (first use or growth), and the changed slots with
vkCmdUpdateBuffer otherwise, which copies the data into the command buffer while
recording so no staging bytes are read at execution. A TRANSFER_WRITE ->
SHADER_READ barrier follows, before the draws. DescriptorTable tracks the slots
that insert, replace, erase, clear and grow touched, including both slots of a
relocation, so a flush no longer rebuilds an 80 MiB table for one descriptor.

A command buffer whose vkQueueSubmit is rejected never runs the upload, so the
set keeps the obligation: VulkanQueue::execute reports the submit result through
a commit callback, and the set rebuilds the table on the next flush, which also
reports flush_needs_idle() and makes the device wait before it. The same wait now
honours the pre-existing contract for replaced/erased entries and for growth
(the device ignored flush_needs_idle() before), and frees the retired buffers
once the queue is idle instead of at teardown.

Verified on an NVIDIA RTX 2060 SUPER, Linux 620.73.04, Vulkan 1.4.357:
- ctest 10/10, including descriptor_table cases for the change list, relocation
  marking and consume-once reads.
- vk_bindless_test with LEONIDA_VK_VALIDATION=1: 14 draws (probe depth, erase,
  relocation, growth 64 -> 512 slots, rebind, failed-submit retry, other view,
  removal) read back exact, table in device-local memory in both
  descriptor-indexing configurations. With the retry disabled the test is red.
- GPU suite 64/91 with the same 27 64-wide skips as main, 0 failures; 91/91 with
  LEONIDA_W64_EMULATE=1.

No game data; synthetic fixtures only.
@SuarezPM
SuarezPM force-pushed the device-local-descriptor-table branch from c0ec562 to e6eaecd Compare November 22, 2026 02:30
@SuarezPM

Copy link
Copy Markdown
Contributor Author

Thank you — that is the confirmation the fix needed, and it pins the mechanism: the retry path is the commit callback, not the old memcpy.

One honest caveat: you re-ran db3dc92; the head is now e6eaecd. The retry logic is identical, but the transfer under it changed after a cold review of the branch:

  • incremental uploads are recorded with vkCmdUpdateBuffer (copied into the command buffer while recording) instead of a staged vkCmdCopyBuffer, so a later flush can never rewrite staging bytes a pending copy still reads;
  • the forced full upload after your rejected submit now goes through the same chunked vkCmdUpdateBuffer path;
  • the device now honours flush_needs_idle() (it ignored it before): replaced/erased entries and growth wait for the queue before the flush, and retired buffers are freed once the device is idle.

So the case you exercised is the same scenario with a different copy underneath it. If the extended d3d12_descriptor_test is still in front of you, a re-run on e6eaecd would close that gap. Locally vk_bindless_test covers the same sequence without fault injection (record the upload, never submit it, table_upload_result(false), then draw) and is red when the retry is disabled; on an RTX 2060 SUPER / 620.73.04 it reads 0 bad pixels under validation.

Current head e6eaecd; no validation errors here either.

@rafaelpssz

Copy link
Copy Markdown
Contributor

Tested e6eaecd on Windows 11, NVIDIA GeForce RTX 3060 (Ampere), driver 616.64, Vulkan SDK 1.4.363.1, clang-cl 23 + lld-link against the MSVC 14.44 CRT and Windows SDK 10.0.26100: pass.

  • ctest with LEONIDA_VK_VALIDATION=1: 28/29. The one failure is loader's antivirus timing check (written PE images scanned), unrelated.
  • Note for anyone testing under validation: this machine has a stale Epic Games overlay entry in HKLM\...\Vulkan\ImplicitLayers whose JSON is gone, so on this head d3d12_vulkan_device_test first failed with 1 validation errors, the only message being the loader's Failed to open JSON file ...EOSOverlayVkLayer-Win64.json. With GPU tests: count only validation-type errors (TEST-98, @Ryx69's 3732227) #764's filter applied locally (not part of this PR) everything except loader passes and no validation-type message is printed.

vk_descriptor_bench on the same machine, default sizes and reps, net µs per 1M lookups, Windows / Ampere next to your Linux / Turing numbers:

entries capacity / load divergent miss DEVICE_LOCAL divergent miss HOST_VISIBLE ratio T#+S# pairs DEVICE_LOCAL pairs HOST_VISIBLE
1,000 4,096 / 24.4% 437 434 1.0× 108 104
10,000 65,536 / 15.3% 1,518 13,008 8.6× 205 247
100,000 524,288 / 19.1% 3,527 428,594 121.5× 441 39,311 (89×)

So on Windows the HOST_VISIBLE penalty at 100k is larger than on the RTX 2060 SUPER/Linux run (46.2×). The 214 MiB DEVICE_LOCAL|HOST_VISIBLE (ReBAR-less BAR) heap measured within a few percent of plain DEVICE_LOCAL at 1k (430 vs 437). Uniform lookups are the same in every placement (~17-18 net µs/1M for pairs).

@SuarezPM

Copy link
Copy Markdown
Contributor Author

Thank you — third GPU, first on Windows with clang-cl, and the first on the final head. The 28/29 with validation is useful: the one failure is the loader antivirus timing check, and the Epic overlay JSON is a machine entry (its JSON is gone, so the loader prints it as a validation message); #764's filter is the right place for that, not this PR.

Your 121.5x is the strongest number in the thread, and it also caught something worth fixing in my own write-up: with main's relocation strategy (make_probe_slot) the 100k table is now 524,288 slots / 20.00 MiB / 19.1% load, not the 2M / 80 MiB the #639 baseline measured. I re-ran the benchmark on e6eaecd here (RTX 2060 SUPER, Linux 620.73.04) at that same size, for apples-to-apples with your table:

entries divergent miss DEVICE_LOCAL divergent miss HOST_VISIBLE ratio pairs DEVICE / HOST
1,000 1,288 1,310 1.0x 284 / 314
10,000 3,169 4,309 1.4x 513 / 611
100,000 4,585 61,984 13.5x 757 / 3,145 (4.2x)

On the older 80 MiB table the same benchmark measured 46.2x / 19.5x, so the penalty tracks the footprint — and Windows/WDDM roughly doubles it at the same 20 MiB size (your 121.5x). Both directions point the same way: the table belongs in DEVICE_LOCAL, and the remaining placement work (SHADER-100) is still worth doing.

I have updated the PR body with your numbers and the re-run. Thanks again for the injection-free data point on the loader path.

@ItsNotRexed
ItsNotRexed merged commit 6cb0fac into ItsNotRexed:main Nov 22, 2026
8 of 9 checks passed
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.

5 participants