Skip to content

Runtime: DiscardResource, WriteBufferImmediate and atomic copies on Vulkan (D3D-26) - #417

Merged
ItsNotRexed merged 6 commits into
ItsNotRexed:mainfrom
MrCheese5:d3d-26-cmdlist-extras
Nov 22, 2026
Merged

ItsNotRexed merged 6 commits into
ItsNotRexed:mainfrom
MrCheese5:d3d-26-cmdlist-extras

Conversation

@MrCheese5

@MrCheese5 MrCheese5 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Item: D3D-26 (#313, claimed in #156)

Three command-list methods, recorded into the command-list IR and replayed on Vulkan by vulkan_replay.h:

Method Vulkan Notes
DiscardResource a barrier from oldLayout = UNDEFINED to the layout the tracker already has for each subresource No-op (allowed, since the contents become undefined) for buffers, discards with rectangles, parts never used yet (they are still zero-filled on first use), replays without a tracker, and one plane of a combined depth-stencil image without separateDepthStencilLayouts
WriteBufferImmediate (GCL2) a 4-byte vkCmdUpdateBuffer The address is resolved at execution through ResourceBinding::va / buffer_offset and ReplayFunctions::buffer_at. MARKER_OUT: ALL_COMMANDS/MEMORY_WRITE → TRANSFER first. MARKER_IN: an execution-only ALL_COMMANDS → TRANSFER dependency, because Vulkan cannot express "started" (TOP_OF_PIPE in a first scope is no stage), so it is stricter than D3D12 requires, never weaker. DEFAULT: ordered by the application's barriers, like any copy
AtomicCopyBufferUINT / UINT64 (GCL1) one 4- or 8-byte vkCmdCopyBuffer region after an ALL_COMMANDS/MEMORY_WRITE → TRANSFER barrier The barrier covers every earlier write, those to the dependent resources among them. The dependent ranges are checked against their buffers

Recording refuses what the D3D12 debug layer flags (WRITEBUFFERIMMEDIATE_* and ATOMICCOPYBUFFER_* in d3d12sdklayers.h): a null or unaligned address, an unknown mode, offsets not aligned to the copy size, and zero dependencies. A refused call makes Close return E_INVALIDARG, as the other recorded calls do.

How it fits #291 / #157: main has no GetGPUVirtualAddress yet. This PR adds the same ResourceBinding::va / buffer_offset fields and the same buffer_at lookup as #291, with identical lines, so the two should merge without a real conflict. Nothing in GetGPUVirtualAddress is touched. Until a buffer gets a VA on main, a list with an immediate write is refused at execution as unresolved_resource and nothing is submitted. Once #291 or #157 fills binding.va, it works with no further change.

Assumption (documented in docs/d3d12_vulkan.md §4.2): Vulkan promises no atomicity for a transfer. The layer relies on a naturally aligned 4- or 8-byte copy being a single write on the GPU, which matters only to a reader that races the copy from another queue or the host.

Tests

  • command_list_test (CPU, in ctest): the recording rules for all three calls.
  • vk_command_replay_test part E (GPU readback; main's part D is the sync/access test):
    • WriteBufferImmediate in all three modes at console VAs, one of them inside a binding that is a range of another buffer, as arena resources are.
    • UINT and UINT64 atomic copies, ordered between the writes so that a missing barrier would copy a stale or a later value.
    • A buffer discard (kept), a texture cleared, discarded and cleared again, and a rectangle discard (kept).
    • The lists that must be refused: an address past a binding or in none, no vkCmdUpdateBuffer, a copy or dependent range past its buffer, an unknown dependent resource, and a subresource the resource doesn't have.
  • d3d12_vulkan_device_test: both atomic copies and the discards through COM, with readback; refused misaligned and empty calls; WriteBufferImmediate refused at execution while no buffer has a VA.

Results:

  • ctest --test-dir runtime/build --output-on-failure: 18/18 passed (after merging main)
  • vk_command_replay_test (parts A–E), d3d12_vulkan_device_test, d3d12_com_test and d3d12_descriptor_test pass with LEONIDA_VK_VALIDATION=1, with no validation errors
  • Full runtime build (RelWithDebInfo, Ninja) has no warnings at /W4

Tested on an NVIDIA RTX 4090, driver 621.14, Windows 11 build 26200, MSVC 19.44, Vulkan SDK 1.4.363.0. No game files used.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…ulkan (D3D-26)

Three command-list IR commands, recorded through COM and replayed by
vulkan_replay.h:

- DiscardResource: an oldLayout = UNDEFINED barrier to the layout the
  tracker already has for each subresource. Buffers, rectangle discards,
  parts never used yet, untracked replays and one plane of a combined
  depth-stencil without separate layouts are no-ops.
- WriteBufferImmediate: a 4-byte vkCmdUpdateBuffer at the address,
  resolved at execution through ResourceBinding::va / buffer_offset and
  ReplayFunctions::buffer_at (the same fields and lookup as ItsNotRexed#291).
  MARKER_OUT waits for and sees every earlier write; MARKER_IN gets an
  execution-only ALL_COMMANDS dependency (Vulkan cannot say "started");
  DEFAULT is ordered by the application's barriers.
- AtomicCopyBufferUINT/UINT64: one vkCmdCopyBuffer region after a
  barrier that covers every earlier write, the dependent resources'
  included. Recording follows the debug layer's ATOMICCOPYBUFFER_* and
  WRITEBUFFERIMMEDIATE_* rules.

Tests: command_list_test (recording rules), vk_command_replay_test part D
(GPU readback of writes in all three modes, ordered atomic copies,
discards, and the lists that must be refused), and the COM path in
d3d12_vulkan_device_test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ion section

main's vk_command_replay_test now has a part D (sync/access pairs), so the
D3D-26 test becomes part E. The device test's D3D-26 section signals fence
value 7, after main's resolve-region section took 5 and 6. The docs keep
both sides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…llow the bundle rules

main added ExecuteBundle (command_list.h, d3d12_com.cpp) and samplers
(vulkan_backend.h); both sides are kept. DiscardResource and the atomic
copies are now refused in a bundle, as D3D12 documents, and
WriteBufferImmediate is allowed there (d3d12_com_test covers all four).

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

Copy link
Copy Markdown
Contributor Author

Merged current main (4abd545) into the branch, now at 1eaca53: ctest 19/19, vk_command_replay_test A–E plus d3d12_vulkan_device/com/descriptor tests pass with LEONIDA_VK_VALIDATION=1 and no validation errors (RTX 4090, driver 621.14). Also: DiscardResource and the atomic copies are now refused in bundles, WriteBufferImmediate is allowed, per the D3D12 bundle rules.

@NRXM

NRXM commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 1eaca53 on an NVIDIA RTX 4090 (621.24) and an AMD Radeon iGPU (2.0.353), Windows 11 (build 26300), MSVC 14.44, Vulkan SDK 1.4.363.0, clean Ninja build of runtime/: pass on both.

  • ctest 19/19 on each GPU
  • d3d12_com_test, d3d12_descriptor_test, d3d12_uint_clear_test, d3d12_vulkan_device_test and vk_command_replay_test exit 0 on each GPU
  • everything ran with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: no validation-layer errors on either GPU (only loader notices); the AMD run used VK_LOADER_DRIVERS_SELECT=*amd*

(AI-assisted test run.)

main added SetPredication (D3D-18) and reserved resources; both sides are
kept. The predication replay test took part E, so the D3D-26 test is now
part F. An atomic copy recorded under a predicate is counted with the
other copies that Vulkan's conditional rendering cannot skip, and
SetPredication, DiscardResource and the atomic copies are all refused in
a bundle.

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

Copy link
Copy Markdown
Contributor Author

Merged current main (c432cdc, with D3D-18 predication and reserved resources) into the branch, now at e40bd65: ctest 27/27; vk_command_replay_test A–F (D3D-26 is now part F) and d3d12_vulkan_device/com/descriptor/uint_clear tests pass with LEONIDA_VK_VALIDATION=1 and no validation errors (RTX 4090, driver 621.14). Thanks @NRXM for the AMD run on 1eaca53.

@matheuskshn

Copy link
Copy Markdown
Contributor

Code review at e40bd65. For DiscardResource, count == all_subresources represents the null-region case, but the COM method also copies the caller's non-null NumSubresources into the same field. An explicit count of UINT_MAX therefore takes the entire-resource path without validating FirstSubresource. Can the IR distinguish the null region from an explicit range and test the latter at this boundary? AI-assisted code review.

MrCheese5 and others added 2 commits November 22, 2026 02:55
A region with NumSubresources = UINT_MAX was recorded like a null region and
skipped the FirstSubresource check. DiscardResource now has a whole flag;
explicit ranges are checked in 64-bit at recording and replay. Tests at the
UINT_MAX boundary in command_list_test, part F and the COM device test.
Found by matheuskshn in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keeps both sides; the discard emit now passes the binding to image_barrier
as main's version takes it.

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

MrCheese5 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @matheuskshn, you're right: a non-null region with NumSubresources = UINT_MAX took the null-region path and skipped the FirstSubresource check. Fixed in c7e450f: DiscardResource in the IR now has a whole flag set only for a null region, and an explicit range is checked in 64-bit at recording (first + count must not pass UINT_MAX) and against the resource at replay. New tests at that boundary: command_list_test (first 1 / count UINT_MAX and first UINT_MAX / count 1 refused), replay part F (an explicit UINT_MAX count on a texture and a buffer → invalid_subresource), and the COM device test (Close → E_INVALIDARG for first 1, invalid_subresource at execution for first 0). Also merged main (UAV clears) again. At 96404ac: ctest 29/29; vk_command_replay_test A–F, d3d12_vulkan_device_test, d3d12_com_test, d3d12_descriptor_test, d3d12_uint_clear_test, d3d12_uav_clear_test exit 0 with no validation errors (RTX 4090, 621.14). AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit d549d2d 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.

4 participants