Skip to content

Runtime: zero-fill default-heap buffers outside the arena (D3D-71) - #655

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
ismailchaka1:d3d-71-zero-fill-buffers
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
ismailchaka1:d3d-71-zero-fill-buffers

Conversation

@ismailchaka1

@ismailchaka1 ismailchaka1 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Item: D3D-71 (claimed in #374)

Rebuilt on main now that #291 is merged: one commit on top of main, with no dependency left. main's backend has changed since the first version (split locks, several queues), so the change is re-implemented rather than rebased.

D3D12 committed resources start zeroed unless the heap has D3D12_HEAP_FLAG_CREATE_NOT_ZEROED. Arena buffers are zeroed through its mapping, but device-local buffers outside it were not; the layer only logged "default-heap buffers are not zero-filled yet".

Change

  • VulkanContext::zero_on_next_submit(id, buffer) queues the buffer, guarded by storage_mutex_ like note_storage_image. The next ExecuteCommandLists on any queue records vkCmdFillBuffer for each queued buffer and a transfer → all-commands barrier, ahead of its lists.
  • That submission publishes the same initialisation semaphore as the storage-image setup. So when a list on another queue uses the buffer first, it waits for the fill.
  • A buffer destroyed before any submission leaves the queue. A failed submission puts its fills back.
  • CREATE_NOT_ZEROED skips the fill. Placed buffers (create_placed_buffer) are never filled, since D3D12 doesn't zero them and they may alias live data. The log line is removed.

A latent bug this exposed (fixed here). ~VulkanQueue never cleared the shared initialisation semaphore when it was the one that published it. So a later submission on another queue could wait on a destroyed semaphore. Storage-image setup rarely triggers this; buffer fills do. Without the fix, d3d12_vulkan_device_test failed with "vkQueueSubmit(): pWaitSemaphores[0] Invalid VkSemaphore", then crashed. The destructor now clears the semaphore after its idle wait, when the work is known to be finished.

Test (d3d12_vulkan_device_test, whose device has no arena): a default buffer is filled with 0xab and released, then a new one is read back as all zeros. A CREATE_NOT_ZEROED buffer written by a copy earlier in the same list reads back the copy. As before, the zero check also passes without the fill on NVIDIA, whose driver hands out zeroed allocations; it guards the behaviour rather than reproducing the bug there.

Tested on NVIDIA GeForce RTX 4070 SUPER, driver 32.0.16.1088, Windows 11, MSVC 2022:

  • ctest passes 129/129.
  • With LEONIDA_VK_VALIDATION=1, d3d12_vulkan_device_test, d3d12_compute_test, d3d12_draw_test, d3d12_descriptor_test and d3d12_present_test pass with 0 validation errors.

No game files. AI-assisted.

@quackottu

Copy link
Copy Markdown
Contributor

Tested b9cf389 on RTX 5070, driver 621.24, Windows 11: pass. Clean build links, ctest 15/15 including bink_decode and bink_audio. The branch is far behind main (about 2,300 commits), so a rebase is probably needed before merge.

D3D12 committed resources start zeroed unless the heap has
CREATE_NOT_ZEROED. Arena buffers are zeroed through its mapping, but
device-local buffers outside it were not ("default-heap buffers are not
zero-filled yet"). They are now queued on the context and filled with
vkCmdFillBuffer by the next submission on any queue, ahead of its lists.

That submission publishes the same initialisation semaphore as the
storage-image setup, so a list on another queue that uses the buffer first
waits for the fill. A buffer destroyed before any submission leaves the
queue, and a failed submission puts its fills back.

A queue being destroyed now clears that shared semaphore if it published
it: the queue is idle, so the work is done, and a later submission on
another queue would otherwise wait on a destroyed semaphore. With buffer
fills publishing it often, d3d12_vulkan_device_test hit exactly that
(vkQueueSubmit: invalid pWaitSemaphores, then a crash).

d3d12_vulkan_device_test (a device without the arena) reads a fresh
default buffer back as zeros after dirtying recycled memory, and checks
that a CREATE_NOT_ZEROED buffer written in the same list keeps the copy.
@ismailchaka1
ismailchaka1 force-pushed the d3d-71-zero-fill-buffers branch from b9cf389 to 63ab7be Compare November 22, 2026 20:11
@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

Independent native D3D12 WARP vs Leonida RTX 4060Ti placed-buffer alias validation. On current main f847748, Resource::create_placed_buffer() unconditionally memset(device_.arena.mapped + va_, 0, desc_.Width) in runtime/shims/d3d12_vulkan_device.cpp. Synthetic DEFAULT heap 2x64KiB and placed buffer A offset 0: GPU upload distinct pattern, fenced readback A exact; CreatePlacedResource B at SAME offset 0 but NEVER use or transition B; fenced readback A. Native WARP A stays exact 0/65536 wrong; Leonida RTX A becomes 65280/65536 wrong, byte0 expected 93 got 0, no VUID. Disjoint B at offset65536 passes on both. Prior GPU work fenced; B never used. Repro source/results in new local LeonidaRuntimeGapAuditRound3_20261122 only, no proprietary data. Your PR #655 body explicitly says placed buffers are never filled, yet current #655 patch changes only committed default buffer path and retains create_placed_buffer memset from main. Could you incorporate removing the initialization and a native-vs-Vulkan overlapped unused B regression into your existing PR, or advise scope? To avoid competing with your initialization claim I am not submitting a separate PR for this bug. AI-assisted synthetic investigation.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 63ab7be on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R d3d12_vulkan_device (the test this PR changes): pass on both GPUs (1/1 each). No game files.

AI-assisted.

@sh1shu1

sh1shu1 commented Nov 23, 2026

Copy link
Copy Markdown

Tested 63ab7be on NVIDIA RTX 4070 SUPER (driver 616.92), Windows 11 26200, MSVC 14.44, Vulkan SDK 1.4.363, RelWithDebInfo: pass, no regression against main 1628210.

  • ctest: 129/129 passed, same 8 skips as main 1628210
  • d3d12_vulkan_device: 1/1
  • run_gpu_tests (LEONIDA_VK_VALIDATION=1): 143/189 native, 174/189 with LEONIDA_W64_EMULATE=1, 0 failed; identical to main 1628210
    AI-assisted (Claude Code, Claude Fable 5.1).

@Flyander Flyander 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.

Review of the default-heap zero-fill path.

zero_buffers.swap(context_.zero_pending_); // any queue can fill a buffer
}
for (const auto& zero : zero_buffers)
context_.vk.vkCmdFillBuffer(command_buffer, zero.buffer, 0, VK_WHOLE_SIZE, 0);

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.

[P2] Cover the trailing bytes of non-four-byte buffers

VK_WHOLE_SIZE does not fill a trailing 1–3 bytes when the remaining buffer length is not a multiple of four; Vulkan specifies that it fills only the nearest smaller multiple. This code accepts any nonzero Width, and the buffer is allocated at that exact size, so a committed buffer of 5 bytes gets only a 4-byte zero-fill and its final byte remains stale. Please ensure initialization covers the complete D3D12 buffer range. Vulkan vkCmdFillBuffer specification.

@ItsNotRexed
ItsNotRexed merged commit d3a352f into ItsNotRexed:main Nov 23, 2026
3 of 10 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.

7 participants