Skip to content

Runtime: PIPELINE_STATISTICS1 query heaps with mesh counts (D3D-132) - #2870

Draft
itsRevela wants to merge 4 commits into
ItsNotRexed:mainfrom
itsRevela:d3d12/pipeline-statistics1
Draft

itsRevela wants to merge 4 commits into
ItsNotRexed:mainfrom
itsRevela:d3d12/pipeline-statistics1

Conversation

@itsRevela

@itsRevela itsRevela commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Item: D3D-132 (claim).
Depends on: #2359 (D3D-291), which depends on #1712 (D3D-29); this is the coordinator's order. This branch is one commit, 25373c44, on #2359's head 46f73149, which sits on #1712's 56e57fbe.

Draft until there is an AMD run, as below.

What

CreateQueryHeap(D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS1) works when the device has VK_EXT_mesh_shader's meshShaderQueries. The feature is enabled with meshShader where the GPU has it and pipelineStatisticsQuery, and OPTIONS9 MeshShaderPipelineStatsSupported reports it. Without the feature the heap is refused with E_NOTIMPL.

It's built on #2359's split of a statistics query around mesh draws, which VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 requires:

  • Heap pool: the 11 classic counters plus TASK (0x800) and MESH (0x1000) invocations. VUID-07819 forbids those bits only around vkCmdDrawClusterHUAWEI, so they're allowed around ordinary vertex draws.
  • Unsplit query: it holds no mesh draw (Runtime: pipeline-statistics queries around mesh draws (D3D-291) #2359 always splits one that does), so its 13 statistics are copied from the heap pool and MSPrimitives is 0.
  • Split query:
    • Runtime: pipeline-statistics queries around mesh draws (D3D-291) #2359's full segments keep the 11-counter pool, so submissions with only PIPELINE_STATISTICS are unchanged.
    • When a split PIPELINE_STATISTICS1 query is present, the mesh-segment pool grows from 4 to 6 counters (adding task and mesh invocations).
    • Each mesh segment runs a VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT query in a per-submission pool. It runs only beside mesh segments, because NVIDIA's Vulkan driver counts a VS draw's triangle in that query and D3D12 doesn't.
    • The result always goes through the sum, so all 14 fields are written. statistics_sum.comp takes the result width (11 or 14) as a push constant and maps a mesh segment's AS, MS and primitives into fields 11–13. statistics_sum_spirv.h is regenerated with glslang 16.6.0 and passes spirv-val.
  • Refusal: planning refuses a list whose mesh segments lack the task/mesh counters or the primitives pool.

VUIDs checked in the validusage.json shipped with the pinned Vulkan-Headers (1.4.363): 07076, 07819, 07068/07069 (both need meshShaderQueries), 07070 and 07071.

Results

NVIDIA GeForce RTX 3070 Ti, driver 610.88, Windows 11, RelWithDebInfo.

d3d12_mesh_statistics compares all 14 fields with host D3D12 and fails on any difference. They're identical:

Query Layer and host
VS draw IAV 3, IAP 1, VS 3, C 1/1, PS 4096, the rest 0; PIPELINE_STATISTICS gives the same first 11
DispatchMesh(2,2,1) C 8/8, PS 1024, MS 128, MSPrimitives 8, the rest 0
DispatchMesh(1,1,2) C 4/4, PS 512, MS 64, MSPrimitives 4
split (3 + 1) same as 2×2
never-ended all 0
inner resolve same as 2×2; nothing is written past the resolved range

AS is 0 because #1712 refuses amplification shaders.

No regressions against the base: ctest on #2359's head alone gives 124/124 (8 skipped), and with this commit 125/125 (the same 8 skipped). The extra test is d3d12_mesh_statistics.

Not verified:

  • Vulkan validation layers aren't installed here, so nothing ran under them.
  • Only NVIDIA was tested. An AMD run (RADV and Windows, RDNA1/2) of ctest -R "mesh|statistics|query|predication" against Runtime: pipeline-statistics queries around mesh draws (D3D-291) #2359's head is needed before this leaves draft.
  • The host refusal HRESULT without meshShaderQueries is unmeasured, because every host adapter tried supports the heap.

No game files. AI-assisted (Claude Code).

LloydNicholson and others added 3 commits November 22, 2026 20:26
…h (D3D-29)

The device enables VK_EXT_mesh_shader (meshShader) where the GPU has it
and reports MeshShaderTier 1 only then.

ID3D12Device2::CreatePipelineState parses pipeline state streams in the
d3dx12.h layout into the descriptions the existing PSO creators take, with
the runtime's defaults for missing subobjects. Streams the PC runtime
refuses are E_INVALIDARG; AS, depth bounds, split stencil masks and view
instancing are E_NOTIMPL with a log.

A mesh PSO is MS + PS with no vertex input or input assembly, and no
dynamic topology or strides. Its output sizes and workgroup are checked
against VkPhysicalDeviceMeshShaderPropertiesEXT.

DispatchMesh records a Draw with mesh set and replays as
vkCmdDrawMeshTasksEXT; a draw of the wrong kind for its pipeline fails
with ReplayResult::invalid_draw.

d3d12_mesh_test covers the refusals and checks a 2x2x1 and a 1x1x2 mesh
grid plus a VS PSO from a stream, pixel by pixel.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ates; mesh test skips without Vulkan

The stream parser keyed its duplicate check on the raw subobject type, so DEPTH_STENCIL with DEPTH_STENCIL1
(either order) or RASTERIZER with RASTERIZER2 was accepted; D3D12_PIPELINE_STATE_STREAM_DESC counts versions of
one subobject as the same one. The check now uses the base type, while the payload still follows the original
type. d3d12_mesh_test covers both mixed orders, RASTERIZER + RASTERIZER2, and each description alone.
Reported by Vonsek in review.

d3d12_mesh_test failed on a machine without a Vulkan device (the Windows CI runner) instead of exiting 77; it
now skips there like d3d12_draw_test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
D3D12 allows DispatchMesh inside a PIPELINE_STATISTICS query and reads its input-assembly, vertex, geometry and
tessellation fields as 0; Vulkan forbids vkCmdDrawMeshTasksEXT while a query with those counters is active
(VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076), and the heap's pool has all eleven.

A statistics query that holds a mesh draw is now split like one that holds a resolve (D3D-162). Each segment's
first draw picks the submission's full pool or a second one with only the clipping, fragment and compute counters,
and a draw of the other kind starts a new segment. The statistics sum copies a mesh segment's four counters into
fields 5..8 of its record and statistics_sum.comp adds them to their D3D12 fields. A list that would need the split
without the compute helper is refused instead of running the query unsplit.

replay_query_predication_test checks the split points, segment kinds and resolve plan on the CPU; d3d12_mesh_test
resolves queries around mixed, mesh-only and vertex-only draws on the GPU.

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

Copy link
Copy Markdown

Tested 617455ad on Windows 11 with an RTX 4070 (driver 621.01). d3d12_mesh_statistics passed with validation off, including the native D3D12 comparison. With LEONIDA_VK_VALIDATION=1 and Khronos validation 1.4.357, it failed twice with the same six errors:

  • 1 × VUID-vkCmdDraw-stage-07073: the vertex draw ran while a VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT query was active.
  • 4 × VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076: the mesh draws ran with an active pipeline statistics query that included counters Vulkan disallows for mesh draws. This occurred in both the regular and split queries.
  • 1 × WRITE_AFTER_WRITE: synchronization validation flagged two vkCmdCopyQueryPoolResults writes to the same destination buffer. The reported region was offset 112, size 232 bytes.

d3d12_query_rules and replay_query_predication both passed in the same run.

AI-assisted

@LloydNicholson

Copy link
Copy Markdown
Contributor

Heads-up on an overlap with #2359 (D3D-291, mine), which is also stacked on #1712.

Both PRs change runtime/src/statistics_sum.comp, statistics_sum_spirv.h and OcclusionResolve::sum_statistics:

  • Here, the counter count becomes a push constant (13 for PIPELINE_STATISTICS1).
  • Runtime: pipeline-statistics queries around mesh draws (D3D-291) #2359 splits a classic statistics query wherever its draws switch between mesh and vertex pipelines, because Vulkan forbids the IA/VS/GS/tessellation counters around vkCmdDrawMeshTasksEXT (VUID 07076).
    • Mesh segments run in a second pool with only clipping, fragment and compute counters.
    • A mesh push constant tells the sum to put that segment's four counters at fields 5, 6, 7 and 10.

The merged shader needs both. Whichever lands second rebases. If this one lands first, I'll rebase #2359 onto it. That split is also most of what your D3D-339 finding asks for; more in the coordination issue: #2891 (comment)

AI-assisted (Claude Code).

@CLHelper

Copy link
Copy Markdown
Contributor

Tested 617455ad as is (its own base) on Windows 11, NVIDIA GeForce RTX 4060 Laptop GPU (Ada), driver 576.80, MSVC 14.44, Vulkan SDK 1.4.363: pass. Synthetic tests only, no game files.

  • d3d12_mesh_test: mesh tier 1, DispatchMesh and the stream VS draw match all 4096 pixels.
  • d3d12_mesh_statistics_test: pass. AS, MS and MSPrimitives match host D3D12 on all four queries, split included.
  • replay_query_predication_test, d3d12_query_rules_test: pass.

D3D-339 reproduces on this card and driver too, with the same numbers as on the 3070 Ti: DispatchMesh(2,2,1) gives IAVertices/IAPrimitives/VSInvocations 4/4/128 on the layer and 0/0/0 on the host, DispatchMesh(1,1,2) 2/2/64, and the VS draw's MSPrimitives is 1 against the host's 0.

@itsRevela, D3D-339 stays yours. In case it helps, I tried the one-kind case on top of this PR: CLHelper#3 (a draft in my fork, not a PR here). prepare() records which kinds of draw each statistics query saw (D3D12 keeps Begin and End in one list), and a query that saw only mesh or only vertex draws gets the other kind's counters zeroed at resolve. With it, all 14 counters of the test's four queries match the host on the 4060. A query that mixes both kinds still needs the split you described. Use it, change it or ignore it.

AI-assisted.

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

The implementation is sound but the branch is not mergeable as-is:

  1. Rebase required. The head sits on 47e8b421, a dropped #1712 head, and conflicts with current main in runtime/CMakeLists.txt (main's d3d12_bundle_test vs #1712's mesh-test block) and runtime/shims/d3d12_vulkan_device.cpp (main's Device9 shader-cache methods vs #1712's Device2 CreatePipelineState). Current #1712 is 56e57fbe, which merges cleanly with main; rebase this PR onto that head (or onto main once #1712 lands) and drop the stale stack.
  2. Build/test evidence. No configured build was available and the PR adds 3 test targets, so none were built or run here. Please provide a configured build plus the d3d12_mesh_statistics_test, replay_query_predication_test and d3d12_query_rules_test results, with validation enabled.
  3. Contract delta. Unsupported mesh statistics currently return E_NOTIMPL, while the D3D12 spec says the three new PIPELINE_STATISTICS1 fields read 0. Either return the zeroed structure or state the deviation explicitly in the body/docs.

The SDK constants check out (D3D12_QUERY_DATA_PIPELINE_STATISTICS1 = 14 x UINT64, AS/MS/MSPrimitives at 11/12/13, heap type 7, query type 10), the 13 Vulkan counters match, and the embedded SPIR-V equals a fresh glslang compile (361 words, 0 diff) with spirv-val clean. Rebase and CI should be enough after that.

@itsRevela

Copy link
Copy Markdown
Contributor Author

Thanks for the review. This is now a draft:

  • It needs the rebase you describe.
  • Runtime: pipeline-statistics queries around mesh draws (D3D-291) #2359 (D3D-291) shows that drawing mesh tasks inside a statistics query with the vertex-stage counters is invalid Vulkan (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076). The IA/VS counts this PR reported as a "known driver difference" are that violation, not a driver quirk.

I'll rebuild D3D-132 on top of #2359's per-segment pools (#1712 → #2359 → #2870, as the coordinator lists), so PIPELINE_STATISTICS1 adds only the task/mesh counters and the primitives pool to segments that #2359 already makes valid. That comes with a full configured build and ctest output.

AI-assisted (Claude Code).

ID3D12Device::CreateQueryHeap refused D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS1. It now creates one on a device
with VK_EXT_mesh_shader's meshShaderQueries (enabled with meshShader where the GPU has it, and reported through
OPTIONS9.MeshShaderPipelineStatsSupported); without it the heap is still refused with E_NOTIMPL. Built on D3D-291
(ItsNotRexed#2359), which splits a statistics query around mesh draws into full and mesh-pool segments.

- The heap's pool counts the 11 classic statistics plus TASK_SHADER_INVOCATIONS (0x800) and
  MESH_SHADER_INVOCATIONS (0x1000); Vulkan writes them in bit order after CSInvocations, the order of
  ASInvocations and MSInvocations. Neither bit is forbidden around vertex draws (only around
  vkCmdDrawClusterHUAWEI, VUID-07819).
- A query that holds a mesh draw is always split (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076, D3D-291), so
  an unsplit query ran no mesh draw: the resolve copies its 13 statistics 112 bytes apart and writes MSPrimitives
  as 0.
- With a split PIPELINE_STATISTICS1 query in the submission, the mesh pool also counts task and mesh invocations
  (6 counters), and each mesh segment runs a VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT query of its index beside
  it; full segments run none. No VUID ties those queries to the draw kind (07068, 07070, 07071), but NVIDIA's
  Vulkan driver counts a VS draw's triangles in them and D3D12 does not, so MSPrimitives is counted only around
  mesh draws.
- A split PIPELINE_STATISTICS1 query always resolves through the sum. statistics_sum.comp takes the result width
  (11 or 14) as a push constant and maps a mesh segment's task and mesh invocations and primitives to fields 11-13.
  statistics_sum_spirv.h is regenerated with glslang 16.6.0 (-V --target-env vulkan1.0 -Os -g0; spirv-val clean).

Tested on Windows 11, NVIDIA GeForce RTX 3070 Ti, driver 610.88, without Vulkan validation layers (not installed):
ctest 125/125 passed, 8 skipped (game data, the validation layer, an RGB9E5 RTV); ItsNotRexed#2359's head alone: 124/124
passed, the same 8 skipped. New: d3d12_mesh_statistics runs one list on the layer and on host D3D12 (DXIL from
HLSL equivalents compiled at run time by the Windows SDK's dxcompiler.dll): a VS draw in PIPELINE_STATISTICS1 and
PIPELINE_STATISTICS queries, DispatchMesh(2,2,1) and (1,1,2), a query never ended, and a query split by a resolve
inside it, resolved at an offset. Every one of the 14 fields matches the host. replay_query_predication cases
10-12 cover the unsplit resolve, the primitives queries beside mesh segments only, the refusals and the 14-counter
sum; d3d12_query_rules compares PIPELINE_STATISTICS1 heap creation by count and node mask with WARP and the GPU.
The host's refusal HRESULT is unmeasured: every host adapter here (WARP, RTX 3070 Ti) supports these heaps.

Depends on: ItsNotRexed#1712, ItsNotRexed#2359
AI-assisted (Claude Code).
@itsRevela
itsRevela force-pushed the d3d12/pipeline-statistics1 branch from 617455a to 25373c4 Compare November 22, 2026 20:26
@itsRevela

Copy link
Copy Markdown
Contributor Author

Rebuilt on #2359 (25373c44 on 46f73149), as asked: the configured build and ctest totals are in the updated description. All 14 fields now match host D3D12 exactly; the earlier IA/VS difference was VUID-07076, which #2359 fixes. It stays a draft until an AMD run shows no regression against #2359's head. AI-assisted (Claude Code).

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.

5 participants