Skip to content

d3d12: defer DispatchMesh in bundles (D3D-BUNDLE-DISPATCH-MESH) - #841

Open
Maticcm wants to merge 2 commits into
ProjectLeonida:mainfrom
Maticcm:feat/d3d12-bundle-dispatch-mesh
Open

Maticcm wants to merge 2 commits into
ProjectLeonida:mainfrom
Maticcm:feat/d3d12-bundle-dispatch-mesh

Conversation

@Maticcm

@Maticcm Maticcm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-BUNDLE-DISPATCH-MESH

What and why

ID3D12GraphicsCommandList6::DispatchMesh in runtime/shims/d3d12_com.cpp previously evaluated graphics_pipeline_, graphics_.layout, and viewport_ immediately when recorded, so calling DispatchMesh inside a D3D12_COMMAND_LIST_TYPE_BUNDLE command list did not defer through defer() and failed to inherit the parent direct list's render targets, viewport, scissor, or root arguments at ExecuteBundle. It also skipped note_direct_heap_indexing(graphics_.layout).

This change:

  • Keeps d3d12_trace::record and the D3D12 grid-dimension checks (<= 65535 per axis and product <= 2^22) at record time in GraphicsCommandList::DispatchMesh, then routes the recording through defer([=](GraphicsCommandList& l) { l.record_dispatch_mesh(x, y, z); }, true).
  • Adds record_dispatch_mesh(UINT x, UINT y, UINT z) (matching record_draw and record_dispatch) with note_direct_heap_indexing(graphics_.layout), empty-viewport skip, and leonida::Draw (draw.mesh = true).
  • Updates docs/d3d12_vulkan.md section 3.2 to note that DispatchMesh is deferred alongside draws and dispatches.
  • Adds GPU + negative bundle tests in runtime/test/d3d12_mesh_test.cpp:
    • A bundle created with mesh_pso executing DispatchMesh(2, 2, 1) twice from a parent created with vs_pso using different parent root constants/CBVs (verifying bundle initial PSO override and inherited render target, viewport, scissor, and root arguments).
    • A state-setting bundle handing back mesh_pso and root arguments to the parent for a subsequent DispatchMesh(2, 2, 1), verifying all 4,096 pixels (64x64) read back exact.
    • Negative checks: oversized grid in a bundle (E_INVALIDARG on bundle Close), executing a DispatchMesh bundle on a parent with no graphics root signature (E_INVALIDARG on parent Close), and calling DispatchMesh on an already-closed bundle (E_INVALIDARG on parent Close).

Tested

  • scripts\build_runtime.ps1 (MSVC 19.50.35729.0, Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.1, RelWithDebInfo): [263/263] targets built cleanly.
  • ctest --test-dir runtime/build -R "^(d3d12_mesh|d3d12_bundle|d3d12_bundle_forbidden|d3d12_close_parity)$" --output-on-failure: 4/4 passed (100%).
  • Same 4 tests with LEONIDA_VK_VALIDATION=1 (VK_ADD_LAYER_PATH): 4/4 passed (100%, 0 Vulkan validation errors).
  • python -I scripts/check_no_game_data.py runtime/shims/d3d12_com.cpp runtime/test/d3d12_mesh_test.cpp docs/d3d12_vulkan.md: ok (3 file(s)).
  • python -m compileall -q make_game.py tools tests: exit 0.

GPUs tested: NVIDIA GeForce RTX 3080, driver 630.20, Windows 11

AI-assisted: yes

Known limitations: ExecuteIndirect inside a bundle remains un-deferred, and bundle Dispatch still has no dedicated GPU test (both noted in docs/d3d12_vulkan.md).

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. (n/a — no translator changes)

Copilot AI balanced review requested due to automatic review settings November 24, 2026 14:26

@abnalstlll-byte abnalstlll-byte 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.

Audited and verified. Implements deferred DispatchMesh recording for D3D12 bundle command lists, validating dispatch grid dimensions (<= 65535) at record time and replaying calls onto the parent command list during ExecuteBundle. Includes comprehensive test additions in
untime/tests/d3d12_mesh_shader_test.cpp covering state inheritance, constant buffer offset updates across bundle executions, and negative error conditions. Approved.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
What changed in this PR

Defers ID3D12GraphicsCommandList6::DispatchMesh through the bundle replay path so bundles correctly inherit parent state at ExecuteBundle, and adds regression tests + documentation for the behavior.

Changes:

  • Route DispatchMesh recording through defer() and add record_dispatch_mesh() to replay with inherited parent state.
  • Add GPU + negative bundle coverage for deferred DispatchMesh behavior.
  • Document that DispatchMesh is deferred like draws/dispatches for bundle replay.
File Description
runtime/​shims/​d3d12_com.cpp Defers DispatchMesh via defer() and adds record_dispatch_mesh() for replay-time state snapshotting.
runtime/​test/​d3d12_mesh_test.cpp Adds bundle execution + negative tests validating deferred DispatchMesh semantics and error cases.
docs/​d3d12_vulkan.md Updates bundle behavior documentation to include deferred DispatchMesh.

Review effort: Lite

Comment on lines +560 to +568
const UINT64 bundle_fence = d.pipeline_statistics_query ? 4 : 3;
EXPECT(SUCCEEDED(queue->Signal(fence.p, bundle_fence)) &&
SUCCEEDED(fence->SetEventOnCompletion(bundle_fence, nullptr)));

std::array<std::uint8_t, size * 256> bundle_pixels{};
void* bundle_mapped = nullptr;
EXPECT(SUCCEEDED(readback->Map(0, nullptr, &bundle_mapped)));
std::memcpy(bundle_pixels.data(), bundle_mapped, bundle_pixels.size());
readback->Unmap(0, nullptr);
Comment on lines +1420 to +1424
void record_dispatch_mesh(UINT x, UINT y, UINT z) {
if (!graphics_pipeline_ || !graphics_.layout) return check(false);
note_direct_heap_indexing(graphics_.layout);
if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized
leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_,
Comment thread runtime/shims/d3d12_com.cpp Outdated
scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0};
draw.mesh = true;
check(list_.draw(std::move(draw)));
if (defer([=](GraphicsCommandList& l) { l.record_dispatch_mesh(x, y, z); }, true)) return;
@Maticcm

Maticcm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Addressed @copilot review comments (rebased onto current main):
1.
untime/shims/d3d12_com.cpp (DispatchMesh): replaced implicit [=] lambda capture with explicit [x, y, z].
2.
untime/shims/d3d12_com.cpp (
ecord_dispatch_mesh): confirmed
ecord_draw (L1408-1418) uses the exact same ordering (
ote_direct_heap_indexing before the empty-viewport early-return), so both paths behave identically.
3.
untime/test/d3d12_mesh_test.cpp: replaced the
ullptr event in the bundle fence wait with an explicit CreateEventW / WaitForSingleObject / CloseHandle wait before mapping
eadback.

Rebuilt d3d12_mesh_test and verified on NVIDIA GeForce RTX 3080 (LEONIDA_VK_VALIDATION=1, 0 validation errors). AI-assisted.

@Sangonapou

Copy link
Copy Markdown
Contributor

Tested a74ab14 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0); llvm-mc 23.1.6, SPIRV-Tools 2026.4. No game files.

ctest -R ^d3d12_mesh$ -V: passed, D3D-29: mesh tier 1; ... DispatchMesh over X, Y and Z, deferred bundle DispatchMesh and a stream VS draw matched all 4096 pixels on the native mesh stage.

One observation, not a failure: the statistics line here reads statistics, mixed: IA 6/2 VS 6 C 12/2 PS 3328 CS 0; mesh only: C 8/0 PS 1024; vertex only: C 2 PS 2048, while on this card the same test printed C 12/12 and mesh only: C 8/8 on the heads of #424 and #815 (older bases). I have not checked whether the second clipper count changed on main or in this branch; mentioning it in case it is the bundle path.

@Timon360SB

Copy link
Copy Markdown
Contributor

Independent testing results

Verified deferred DispatchMesh execution in bundles on NVIDIA Blackwell hardware under native Windows, with Vulkan validation enabled.

Tested revisions

  • BASE: a28927849adb4e40dfdec8c32fb6758dcd205a37
  • HEAD: a74ab14076c803ecf7e9c34f6d2e1158e6d2d194

Test environment

Component Configuration
CPU AMD Ryzen 7 7800X3D
GPU NVIDIA GeForce RTX 5070 (Blackwell)
VRAM 12227 MiB
RAM 31.11 GiB
GPU driver 621.14
OS Windows 11 Pro, build 26200
Vulkan SDK 1.4.363.0
MSVC 19.44.35230
Windows SDK 10.0.26100.0

Test configuration

  • Build configuration: RelWithDebInfo.
  • Build generator: Ninja Multi-Config.
  • Build scope: selected targets.
  • Vulkan core, GPU-assisted and synchronization validation enabled.
  • Three repetitions per revision.
  • Synthetic fixtures only.

Both revisions compiled successfully.

CTest results

Test BASE HEAD
d3d12_mesh 3/3 3/3
d3d12_bundle 3/3 3/3
d3d12_bundle_forbidden 3/3 3/3
d3d12_close_parity 3/3 3/3
vk_sync_validation 3/3 3/3
Total 15/15 15/15

Combined: 30/30 PASS, with no failed or skipped CTest executions.

Deferred DispatchMesh

The mesh test reported mesh shader tier 1 support on the RTX 5070.

All three HEAD executions explicitly confirmed successful deferred bundle DispatchMesh behavior.

The relevant HEAD diagnostic was:

DispatchMesh over X, Y and Z, deferred bundle DispatchMesh and a stream VS draw matched all 4096 pixels
Verification HEAD result
DispatchMesh over X, Y and Z PASS
Deferred bundle DispatchMesh PASS
Pipeline stream VS draw PASS
Pixel comparison 4096/4096 matched
Oversized dispatch grids Rejected at Close
Invalid bundle replays Rejected at Close
Wrong-kind draws Rejected at execution
Mesh statistics queries Resolved successfully

The pipeline state stream checks also consistently reported nine invalid cases and two not-implemented cases being rejected.

Bundle regression coverage

The forbidden bundle calls test reported the same results on BASE and HEAD:

  • 10 calls checked on the layer.
  • 10 compared with the host.
  • 0 host disagreements.
  • 0 failures.

The close-parity test also remained unchanged:

Property BASE HEAD
Cases 113 113
Match WARP 82 82
Known layer-stricter cases 16 16
Known layer-looser cases 15 15
Not run 0 0
Failures 0 0

Vulkan validation

The synchronization self-test produced the expected intentional WRITE_AFTER_WRITE hazard without a barrier and zero validation errors with a barrier.

Limitations

  • Tested on one NVIDIA Blackwell GPU.
  • No second hardware configuration was available for this run.
  • Testing was limited to the five selected CTest tests and synthetic fixtures.
  • No real-title execution was performed.

Conclusion

Result: PASS — no regressions detected within the tested scope.

The new deferred bundle DispatchMesh scenario successfully matched all 4096 pixels in every HEAD repetition. Invalid bundle replays were rejected as expected, and the existing bundle and close-parity regression tests remained successful.

AI-assisted log analysis and report preparation.

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.

5 participants