Skip to content

d3d12: destroy raw-texture image views at device teardown - #2184

Open
ammonium231 wants to merge 2 commits into
ProjectLeonida:mainfrom
ammonium231:fix-raw-2d-imageview-leak
Open

ammonium231 wants to merge 2 commits into
ProjectLeonida:mainfrom
ammonium231:fix-raw-2d-imageview-leak

Conversation

@ammonium231

@ammonium231 ammonium231 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

set_raw_image_address never registered the title VA, so ~Resource did not forget the views and xbox_raw_2d_stripes and xbox_raw_2d_surface leaked 2 VkImageViews each (VUID-vkDestroyDevice-device-05137).

Claimed in the coordination issue as: RT-RAW-2D-IMAGEVIEW-LEAK

Depends on: none

Fixes: VUID-vkDestroyDevice-device-05137 in xbox_raw_2d_stripes and xbox_raw_2d_surface (no standalone issue).

What and why

set_raw_image_address overwrote image_address_ without note_image_address, so ~Resource skipped forget_image. It now registers the title VA the same way as place_at_title_address.

Tested

LEONIDA_VK_VALIDATION=1 after rebase onto 0b11d86: xbox_raw_2d_stripes and xbox_raw_2d_surface still 2 leaked VkImageViews to 0. Full ctest 472/475; xbox_present and d3d12_set_descriptor_heaps also fail on unpatched main; xmem_no_write_watch failed once under -j 4 and passed twice isolated.

GPUs tested: NVIDIA RTX 3090, driver 621.40, Windows 11

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

set_raw_image_address never registered the title VA, so ~Resource did not forget the views and xbox_raw_2d_stripes and xbox_raw_2d_surface leaked 2 VkImageViews each (VUID-vkDestroyDevice-device-05137).
@Tyrannys

Copy link
Copy Markdown
Contributor

Independent verification of head ee0aa61a8 merged into current main (ec91d695c), no conflicts.

Machine: profile M (RTX 4070 SUPER 12 GB desktop, NVIDIA 621.01, Windows 11 Pro 26300), MSVC 14.52 (VS 2026, MSBuild generator), RelWithDebInfo. No game files.

Leak, before and after (LEONIDA_VK_VALIDATION=1 ctest -R xbox_raw_2d -V):

main main + this PR
xbox_raw_2d_stripes passes, 2 leaked VkImageView (VUID-vkDestroyDevice-device-05137) passes, 0
xbox_raw_2d_surface passes, 2 leaked VkImageView (same VUID) passes, 0
other validation errors 0 0

So the fix does what it says on NVIDIA too.

Full suite (ctest -j 6, then failures rerun alone):

  • main alone: 488/501 under -j 6; after rerun still failing: xbox_present, d3d12_set_descriptor_heaps, xs_proxy_mock, d3d12_queue_handover_two_queues, big_compile_gate (timeout), api_ref_state (timeout).
  • With this PR: 497/501 under -j 6; after rerun only xbox_present and d3d12_set_descriptor_heaps fail, both of which fail on main too (known). No new failure.

Diff read: the new body matches place_at_title_address (same alignment and image_address_ guards), and ~Resource only forgets an address it still owns (image_owner(...) == this), so two cached raw surfaces at the same address with different formats/sizes do not unregister each other on teardown. One behaviour change worth knowing: an address with low bits set (not 256-aligned) now keeps the resource's synthetic address instead of taking the raw one. A T# cannot name such an address anyway, so I see no practical effect.

No problems found.

AI-assisted (Claude Code).

@ammonium231

Copy link
Copy Markdown
Contributor Author

Thanks @Tyrannys for verifying on the 4070 SUPER. Marking ready for review.

@ammonium231
ammonium231 marked this pull request as ready for review November 27, 2026 20:54
@RenoThaGutta

Copy link
Copy Markdown
Contributor

Independent RTX 4080 validation of the runtime change in ee0aa61a8249293c0e633deea2ac06cd73e5a911 on baseline 02f71ccfe78160997a02950607be583af87e1180. Windows 11 Pro 25H2, NVIDIA 621.01, Khronos Vulkan validation enabled. Synthetic fixtures only. No GTA VI files were used.

MEASURED at device teardown (VUID-vkDestroyDevice-device-05137):

CTest case Baseline leaked image views With #2184
xbox_raw_2d_stripes 2 0
xbox_raw_2d_surface 2 0

The ordinary harness reported passes despite the baseline diagnostics. I ran an identical local test-only check on both builds that reads the validation error count after the device is destroyed. With that check, baseline passed 8 of 10 textured-draw cases and failed exactly the two above. With #2184, all 10 passed. The PR runtime diff was applied unchanged.

Independent evidence review completed. AI-assisted: OpenAI Codex.

@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

#2184 | head ee0aa61 (merged cleanly with main 48bae6c — no conflicts on current main) | d3d12_m2_textured_test (LEONIDA_TEST_LARGE_RAW_TEXTURE=1, LEONIDA_TEST_RAW_TEXTURE=1, default) + ctest --test-dir runtime/build -C RelWithDebInfo -R "^d3d12_descriptor$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: both xbox_raw_2d_stripes (LEONIDA_TEST_LARGE_RAW_TEXTURE=1) and xbox_raw_2d_surface (LEONIDA_TEST_RAW_TEXTURE=1) emit vkDestroyDevice(): ... has 2 leaked objects that have not been destroyed (VkImageView) / VUID-vkDestroyDevice-device-05137 at teardown | green on PR+main: xbox_raw_2d_stripes, xbox_raw_2d_surface, d3d12_m2_textured (4096 pixels, maximum RGBA8 channel error 0), and d3d12_descriptor all pass on RTX 3090 with 0 leaked VkImageViews and 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static audit of ee0aa61a8. No builds or GPU runs. The reported Windows/MSVC validation covers the two raw fixtures, which allocate distinct VAs in runtime/test/d3d12_m2_textured_test.cpp:364. The changed source is in the Windows runtime target (runtime/CMakeLists.txt:2377), which is excluded from the Linux build.

  • [MAJOR] runtime/shims/d3d12_vulkan_device.cpp:8757 — Create two raw SRVs with distinct widths at the same aligned VA. The second materialization erases the first SRV's bindless key while its descriptor is live, and console heap resolution can rebuild it against the second backing image. A leak also remains if a sampled slot-133 placement takes over the raw VA and is released before the cached raw SRV is written again. That cache hit creates a view without an address owner, so the raw destructor skips view cleanup. Give each cached backing its own view lifetime, using a synthetic key or a separate ownership index.
  • [MINOR] runtime/test/d3d12_m2_textured_test.cpp:723 — The validation count is read before the fixture calls vkDestroyDevice in runtime/test/vk_test_device.h:170, so the existing cases miss leak messages emitted by that call. Check the count after fixture teardown.

For a raw surface with sole ownership of its VA, the descriptor mutex protects entry removal and each non-null view is queued once, ahead of the backing image. The teardown drain runs after vkDeviceWaitIdle and before the owned device is destroyed (runtime/shims/d3d12_vulkan_device.cpp:14361, runtime/src/vulkan_backend.h:511). The raw cache's early erase handles failed image creation before any SRV is built.

AI-assisted.

A second raw SRV at the same VA, or a slot-133 texture placed there, no longer drops a live raw view, and the raw surface frees its views itself. Tests read validation after device teardown.
@ammonium231

Copy link
Copy Markdown
Contributor Author

@timbot103 Reproduced both on #2184 + main (RTX 3090, validation on): a second raw SRV of another width at the same VA dropped the first's hash entry (index 4096 → 0), and the slot-133 takeover left 1 VkImageView at vkDestroyDevice.
Fixed in e86a217: raw surfaces keep their views under their own registered key instead of owning the title VA. Added xbox_raw_2d_same_va and xbox_raw_2d_takeover, and the test now reads validation after device teardown. 13/13 raw/descriptor/m2 cases pass with 0 validation errors.

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent Wine check of the updated raw-view lifetime tests. I merged e86a217d9 onto 8fb974878 and kept its exact synthetic fixtures for two controls, restoring only runtime/shims/d3d12_vulkan_device.cpp to main or to the earlier PR version (ee0aa61a8 merged on the same base). No game files. CachyOS, Wine 11.22, clang-cl 23.1.5, Radeon 610M (RADV Mesa 26.3.1) and RTX 4070 SUPER (NVIDIA 620.73.04). Both GPUs gave the same results.

Check Main implementation Earlier PR Updated PR
xbox_raw_2d_stripes leaked VkImageViews 2 0 0
xbox_raw_2d_surface leaked VkImageViews 2 0 0
xbox_raw_2d_same_va first entry after adding the wider SRV intact missing (exit 1) intact
xbox_raw_2d_takeover leaked VkImageViews 2 1 0

The updated head passed all six selected cases per GPU, including d3d12_descriptor and d3d12_m2_textured. I verified that the Linux Vulkan loader inserted Khronos validation 1.4.363.1 (VK_LOADER_LAYERS_ENABLE=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer). Wine does not enumerate the host layer to the Windows fixture, so I checked VUID-vkDestroyDevice-device-05137 in the output separately from the exit status. The loader-enabled layer also reported the same VK_EXT_external_memory_dma_buf extension-dependency VUID in all three builds. The leak counts above are for device teardown only. AI-assisted with OpenAI Codex (GPT-6).

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

TAKEN by OreoFR (Windows, RTX 4070 8 GB)

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR — NO RED (red only with validation on)

  • Base: main c22a0a7 + this PR (head e86a217) merged with --no-ff
  • Red (main + only this PR's test and its CMake registration): the new ctests xbox_raw_2d_same_va and xbox_raw_2d_takeover pass on main as registered (raw same VA: slot 0 T# index 4096 before, 4096 after). Run by hand with LEONIDA_VK_VALIDATION=1, both fail on main with 1 validation error each, VUID-vkDestroyDevice-device-05137 (objects still alive at vkDestroyDevice). So the tests show the bug only with validation on, which their ctest entries do not set.
  • Green (merge): xbox_raw_2d_same_va, xbox_raw_2d_takeover and the other xbox_raw_2d_* and d3d12_m2_* tests pass as registered (the merge was not re-run with validation on before this PC moved to in-game runs only); ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p2 = main + d3d12: destroy raw-texture image views at device teardown #2184 + Preserve sRGB midtones on the SDR UNORM swapchain fallback #2001 (no shared files): 507 tests, new failures vs main: none. dxgi_waitable_latency failed in the parallel run and passes when re-run alone (main alone: 505 tests, 6 failures: big_compile_gate and api_ref_state and d3d12_queue_wait_timeout timeouts, xs_proxy_mock, vk_compile_gate_calibration, xbox_present)
  • gta6rt.exe links without the XMA decoder (LEONIDA_CARGO=OFF, LEONIDA_XMA2_DECODER_LIB empty) in package p2; imports unchanged vs main (25 DLLs, 755 functions, no WS2_32)
  • Diff read: no bug. A raw surface keeps its views under its own key (view_key()) while its T# carries the title VA, and teardown forgets that key. Nit: the four new add_test/set_tests_properties lines in runtime/CMakeLists.txt are not indented like the lines around them.
  • Other runs: ammonium231 reproduced the bug on an RTX 3090 with validation on; consistent with Tyrannys's, RenoThaGutta's, zmasi's and merzleckeier's runs.
  • Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants