Skip to content

d3d12/vulkan: a mesh segment's clipping invocations come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER) - #575

Closed
Mby159 wants to merge 2 commits into
ProjectLeonida:mainfrom
Mby159:d3d-mesh-clipping-counter
Closed

Mby159 wants to merge 2 commits into
ProjectLeonida:mainfrom
Mby159:d3d-mesh-clipping-counter

Conversation

@Mby159

@Mby159 Mby159 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-MESH-CLIPPING-COUNTER (#14)

Depends on: #540

What and why

D3D-291 leaves the clipping fields of a mesh segment at 0, because
VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 forbids the clipping counters in a
PIPELINE_STATISTICS pool that is active during a mesh draw. Where the device exposes
VK_EXT_mesh_shader with the meshShaderQueries feature, the number of primitives the mesh
shader emitted can come from a VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT query run around
the mesh segment instead — the addition @milk4sale describes in
#14.

That query needs a pool of its own type, so a third pool of the submission is created next to
the two the statistics split needs, begun and ended with each mesh segment. Its count is copied
to field 5 of the segment's record and statistics_sum.comp adds it as CInvocations.

CPrimitives stays 0. Vulkan has no clipped-primitive counter, and D3D12's CPrimitives is
the clipper's output — 0 when a triangle is dropped, 2 when it is split — so there is nothing to
read it from. Where the device has no meshShaderQueries, the fields stay 0, as in #540.

The one divergence, documented rather than hidden. The count is not D3D12's number: Vulkan
counts a primitive the mesh shader culled with gl_CullPrimitiveEXT, which D3D12 does not
count, so a mesh segment's CInvocations can be higher than D3D12's. Both measurements show
it — @milk4sale's table on an RTX 4070 Ti and @sodove's cross-API comparison against native
D3D12 on the RTX 5070 Ti, where the generated count stayed at 1 for a triangle D3D12's
clipping counters reported as 0/0.

The files:

  • runtime/src/vulkan_backend.h: the pool of VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT,
    created only when the device has the feature, and the copy of its count into field 5 of the
    segment's record. The mesh_queries flag on the context says whether the feature is there.
  • runtime/src/vulkan_replay.h: the pool travels with the list, the segment and the resolve.
  • runtime/src/statistics_sum.comp: a mesh segment now also contributes counter 5.
  • runtime/src/statistics_sum_spirv.h: regenerated from statistics_sum.comp with glslang
    16.6.0 (the version the CI pins) and the command its header names; the module's ID numbering
    differs slightly from the build it replaces, the code is the same apart from that one
    condition.

Tested

  • ctest --test-dir runtime/build -C Release: 72 of 72 tests passed (2 skipped:
    shader_library_corpus needs the game's shaders, vk_sync_validation needs a device with
    the feature). Built with clang 23.1.6 (llvm-mingw 20261117) against the pinned Vulkan headers
    (6802bb47, vulkan-sdk-1.4.363.0).
  • replay_query_predication_test: the mesh-primitives pool is the list's, and the mesh-only
    query's resolve sums its one mesh segment with it.
  • replay_query_edges_test (new case): a mesh segment's record carries the mesh-primitives
    count as CInvocations, the fragment and compute invocations as PSInvocations and
    CSInvocations, and nothing else.
  • python -I scripts/check_no_game_data.py <the eight changed files>: passes.
  • The regenerated SPIR-V passes spirv-val and matches statistics_sum.comp byte for byte
    when recompiled with the same glslang.

The GPU test that covers this path is d3d12_mesh_test, which needs a GPU with
VK_EXT_mesh_shader and the Khronos validation layer; this machine has neither (the Radeon
780M is RDNA3, which has no mesh shader support, and there is no validation layer installed).
The test checks the counts against each other rather than against fixed numbers, because the
generated count is the driver's and depends on the mesh shader's output.

GPUs tested: n/a on this host. @sodove has offered to run this on the RTX 5070 Ti under the
validation layer, including a test device with meshShaderQueries disabled; @milk4sale measured
the query on an RTX 4070 Ti. Their numbers are quoted above, not mine.

No game files, no keys. AI-assisted: yes.

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption.
  • A new translator rule is in the contract table of docs/design.md (not applicable: this
    is the D3D12 to Vulkan runtime layer, whose contract is docs/d3d12_vulkan.md).

…primitives query (D3D-MESH-CLIPPING-COUNTER)

D3D-291 leaves the clipping fields of a mesh segment at 0, because
VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 forbids the clipping counters in a
PIPELINE_STATISTICS pool that is active during a mesh draw. Where the device exposes
VK_EXT_mesh_shader with the meshShaderQueries feature, the number of primitives the mesh
shader emitted can come from a VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT query run around
the mesh segment instead.

The query needs a pool of its own type, so a third pool of the submission is created next to
the two the statistics split needs, begun and ended with each mesh segment. Its count is
copied to field 5 of the segment's record and statistics_sum.comp adds it as CInvocations.
CPrimitives stays 0: Vulkan has no clipped-primitive counter, and D3D12's CPrimitives is the
clipper's output (0 when a triangle is dropped, 2 when it is split). Without the feature the
field reads 0, as in D3D-291.

The count is not D3D12's number: Vulkan counts a primitive the mesh shader culled with
gl_CullPrimitiveEXT, which D3D12 does not count, so a mesh segment's CInvocations can be
higher than D3D12's. Both measurements are in the coordination issue.

Depends on: ProjectLeonida#540
@Mby159

Mby159 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #577: the two are the same work (the same claim and the same design), published twice.

This branch is missing the device half. It never asks for the meshShaderQueries feature, so context_.mesh_queries stays false and the third pool is never made; and runtime/test/d3d12_mesh_test.cpp here reads d.mesh_shader_queries, which this branch does not add, so the runtime does not build.

#577 has the three missing files — the feature is asked for when the device is created, the layer is told, and the test device enables it — and was built and tested on the tree it publishes: ctest --test-dir runtime/build -C Release 71 of 72 (the one failure, vk_queue_query_sync, fails the same way on the unmodified upstream d42773d0), the semantic suite at 465 files and 6105 tests with 0 files failed, check_no_game_data.py on all eleven changed files, and spirv-val on the regenerated module.

Keeping one pull request for the item.

AI-assisted: yes.

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.

1 participant