Skip to content

d3d12: real SO_STATISTICS query pools (D3D-302) - #1630

Merged
aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
BubsMcgee:stream-output
Nov 25, 2026
Merged

aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
BubsMcgee:stream-output

Conversation

@BubsMcgee

@BubsMcgee BubsMcgee commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-302 (claim: #1608 (comment))
Depends on: #1891

Lands only the real SO_STATISTICS query-pool part, on its own (the option aslan2142 offered). StreamOutput PSOs are still refused and SOSetTargets is still not implemented; the capture path is not part of this PR.

The device detect/enable and the vk_pipeline_check parity fix now live in #1891 (which adopts #1236). This PR is stacked on it and contains only the query pools:

When #1891 merges I will rebase this onto main; the two foundation commits drop and only the query changes remain.

The remaining pipeline_cache_seed failure on AMD is neither this PR nor #1891: adrianmjim rechecked on an RX 7900 XT and it fails identically on the base main without either, from #1723's robustBufferAccess change. That is #1833, where I flagged a checker-vs-runtime bindless input mismatch (the checker's OR of update-after-bind flags vs the runtime's six-feature AND) to correct before relying on it.

Tested on main 71a769d7d (Windows 11 Pro 26300, MSVC RelWithDebInfo, NVIDIA RTX 3080 Ti, driver 616.64, Vulkan SDK 1.4.363.0): d3d12_vulkan_device, d3d12_mesh and pipeline_cache_seed pass, plain and with LEONIDA_VK_VALIDATION=1. AI-assisted.

@adrianmjim

Copy link
Copy Markdown

Reviewed cd65952f4 applied onto main 42537e7b6, and ran it on an AMD Radeon RX 7900 XT. The AMD driver exposes the whole extension (transformFeedback, geometryStreams, transformFeedbackQueries, transformFeedbackRasterizationStreamSelect, 4 streams), so the new paths are live there.

A regression on AMD: pipeline_cache_seed fails

With this PR, ctest -R ^pipeline_cache_seed$ fails 3 of 3 on the 7900 XT. It passes 3 of 3 on main with the same build setup:

FAIL: vk_pipeline_check cache does not hit on the runtime's device (AMD Radeon RX 7900 XT), which does hit a cache it wrote itself: vk_pipeline_check's device differs from the runtime's in something the driver makes part of a pipeline's cache key

The runtime's device now enables VK_EXT_transform_feedback, but runtime/test/vk_pipeline_check.cpp does not, and AMD's driver makes that part of a pipeline's cache key. So the cache vk_pipeline_check seeds offline no longer hits in gta6rt, and every pipeline is compiled again at run time. That is the same mismatch the tool already guards against for mesh shaders and maintenance8. I checked it with a two-line local change: linking a VkPhysicalDeviceTransformFeedbackFeaturesEXT with the extension in vk_pipeline_check's feature chain, next to the mesh-shader one. With that, the test passes 3 of 3 again. NVIDIA evidently leaves it out of the key, which is why it didn't show up on the 3080 Ti. The header comment of vk_pipeline_check.cpp and docs/pipeline_check.md ("The cache and the runtime's device") list the always-enabled extensions and would need the new one too.

Problems in the capture path

None of these shows up in the tests, because no test binds a StreamOutput PSO.

1. The rasterization-stream struct has no sType. VkPipelineRasterizationStateStreamCreateInfoEXT rasterization_stream{}; leaves sType at 0, which is VK_STRUCTURE_TYPE_APPLICATION_INFO. That is the case both in the PSO constructor and in the recipe member's default, and build() copies the recipe's value. For a StreamOutput PSO with RasterizedStream != 0 (only accepted when geometryStreams is enabled), build() chains this struct into raster.pNext, so the driver ignores or misreads it and the validation layers report it. It needs {VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_STREAM_CREATE_INFO_EXT}.

2. Nothing makes the translated shaders capture anything. The README, the docs and the new comment in the PSO constructor say capture is driven by XfbBuffer/XfbStride/XfbOffset decorations in the translated SPIR-V. But nothing emits them: tools/rdna2spirv.py never writes an Xfb* decoration or the Xfb execution mode, and the runtime's SPIR-V patching only touches Location. That is true on main and with this PR. So whenever a StreamOutput pipeline draws with at least one SO target bound, vkCmdBeginTransformFeedbackEXT runs with a last pre-rasterization stage that does not declare Xfb, which violates VUID-vkCmdBeginTransformFeedbackEXT-None-04128. In practice nothing is captured, yet the PSO is accepted as if it could be. The D3D12_SO_DECLARATION_ENTRY list is validated (and hashed into the PSO cache key) but never reaches the shaders or the capture setup.

3. Capture buffers lack the transform-feedback usage bit. No buffer is created with VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT. SO targets are resolved from their VAs like vertex buffers, so they land in the layer's ordinary buffers, and none of their usage lists has that bit (line numbers are in runtime/shims/d3d12_vulkan_device.cpp on main):

  • the GPU-VA arena (create_arena, 10151)
  • committed and placed buffers (4710)
  • host-imported buffers (4844)
  • title-placed shadow buffers (4986)
  • reserved buffers (2318)
  • imported console heaps (2659)

Binding them with vkCmdBindTransformFeedbackBuffersEXT violates VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360.

4. Unbound lower slots are bound as VK_NULL_HANDLE. begin_stream_output sets count to the highest bound slot + 1 and passes d.stream_output.data() for slots 0..count-1 in one vkCmdBindTransformFeedbackBuffersEXT call. With slot 0 unbound and slot 1 bound (SOSetTargets with StartSlot = 1, which D3D12 allows), slot 0 is still the VK_NULL_HANDLE that reset() left there. Transform-feedback binds have no null-buffer allowance: every one of the bindingCount entries must be a valid buffer. So this needs one bind per contiguous run of bound slots. It also contradicts the comment on PreparedDraw::stream_output, which says only slots with a buffer are used.

5. Each draw restarts the capture at byte 0. vkCmdBeginTransformFeedbackEXT(..., nullptr, nullptr), and the matching End, use no counter buffers. D3D12 appends at BufferFilledSize and writes it back after the draw. Here consecutive draws overwrite each other. BufferFilledSizeLocation is copied into filled_size_va but never used afterwards, so the count at that address is never read or written. The PR calls the write-back a follow-up, but the comment in prepare_draw ("written after the draw") doesn't match the code yet. Using the filled-size VA as the counter buffer would be the direct mapping, and it also needs VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT.

6. Smaller points:

  • RasterizedStream > 3 returns E_INVALIDARG. That also refuses D3D12_SO_NO_RASTERIZED_STREAM (0xFFFFFFFF), which is valid and common for stream-out-only passes; it maps to rasterizer discard.
  • An entry with a null SemanticName is refused. In D3D12 that is a gap entry, and its ComponentCount may be more than 4.
  • prepare_draw resolves the SO targets for every draw whenever the device has transform feedback, not only for StreamOutput pipelines. A stale SO target left bound makes every later ordinary draw in the list unresolvable (unresolved_at): by default each of those draws is dropped, and with LEONIDA_STRICT_REPLAY=1 the whole list fails. The new test binds 0x1000 and draws nothing after it, so it does not hit this.
  • SOSetTargets in a bundle is deferred and replayed. D3D12 lists it among the calls not allowed in a bundle ("Recording command lists and bundles": the runtime drops the call). The neighbouring RSSetViewports already does check(false) there.
  • prepare() neither refuses nor splits overlapping SO_STATISTICS queries; it only rejects the same heap slot twice. On main that was harmless, because no Vulkan query ran for them. Now they are real TRANSFORM_FEEDBACK_STREAM_EXT queries, so two overlapping queries on the same stream (different slots or heaps) become two active indexed queries of the same type and index. Vulkan forbids that: VUID-vkCmdBeginQueryIndexedEXT-queryPool-04753.

Points 2 to 4 together mean a real StreamOutput draw is undefined behaviour on any driver, and can lose the device. Until the translator emits Xfb and the buffers carry the usage bit, it may be safer to keep refusing StreamOutput PSOs, and land the query part (SO_STATISTICS as TRANSFORM_FEEDBACK_STREAM_EXT pools) on its own.

RX 7900 XT

  • Targeted tests (d3d12_vulkan_device, d3d12_com, d3d12_query_rules, command_list, d3d12_state_map, replay_query_edges, replay_query_predication, vk_queue_query_sync): 8 of 8 pass, plain and with LEONIDA_VK_VALIDATION=1 (validation on, no messages). The SO block of d3d12_vulkan_device now runs real TRANSFORM_FEEDBACK_STREAM_EXT queries on streams 0 and 3 here, and the resolved counts are 0.
  • The detection works: there is no "the GPU has no VK_EXT_transform_feedback" line.
  • Whole ctest -j 6: 397 of 402 with this PR, against 399 of 402 on main. The difference is pipeline_cache_seed (above) and d3d12_indirect_rt, which failed once under -j 6 and passes 3 of 3 alone, on both trees. The other three failures are the same on main: d3d12_texture_tiles (test: texture tiles NOTE only for the measured CopyTiles offset rounding on AMD and Intel (D3D-TILES-AMD-BYTE-OFFSET) #964), d3d12_clear, and d3d12_geometry_views (an AMD-without-Resizable-BAR failure on main, unrelated).

AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, VS 2022, RelWithDebInfo. No game files.

@aslan2142

Copy link
Copy Markdown
Collaborator

Thanks for this. It can't be merged yet, for these reasons:

AI-assisted (Claude Code, Claude Opus 5.5).

@potatoe0100

Copy link
Copy Markdown
Contributor

NVIDIA data point for the pipeline_cache_seed question: tested ac4a992ed merged onto main 57ba40bd6 (clean), native Windows 11 Pro 26H2 (build 26300), NVIDIA GeForce RTX 4060, driver 621.14 (VK_EXT_transform_feedback with 4 streams), VS 2022 RelWithDebInfo + Ninja.

  • Full ctest -j 6: 408 of 408 passed on both main and this PR (same skips).
  • pipeline_cache_seed: passes 3 of 3 with this PR, so the vk_pipeline_check change in this head keeps the cache hitting on NVIDIA.
  • d3d12_vulkan_device with LEONIDA_VK_VALIDATION=1: exit 0, 0 VUIDs.

Nothing here draws through a StreamOutput PSO, so this doesn't cover the review points above.

AI-assisted testing (non-developer tester).

@BubsMcgee BubsMcgee changed the title d3d12: stream output (SOSetTargets, transform feedback, SO_STATISTICS) (D3D-302) d3d12: stream output (D3D-302) ALREADY MERGED-DROP Nov 25, 2026
@BubsMcgee BubsMcgee closed this Nov 25, 2026
@BubsMcgee BubsMcgee reopened this Nov 25, 2026
@BubsMcgee BubsMcgee changed the title d3d12: stream output (D3D-302) ALREADY MERGED-DROP d3d12: real SO_STATISTICS query pools (D3D-302) Nov 25, 2026
@whoalin1

Copy link
Copy Markdown
Contributor

Independent verification (native Windows; RTX 4070 SUPER + AMD gfx1036 iGPU, LEONIDA_VK_VALIDATION=1): builds clean; CPU/mock tests 5/5; GPU tests (d3d12_com, d3d12_vulkan_device incl. the new SOSetTargets/SO_STATISTICS block, vk_queue_query_sync, pipeline_cache_seed) pass on both GPUs; full ctest NV 400/401 (sole failure d3d12_pipeline_library_host 0xc0000409 is pre-existing/environmental — identical crash signature on other branches).

Confirming the outstanding reviews independently — every cited defect verified in the diff, plus additional findings the reviews may not have covered:

  • VkPipelineRasterizationStateStreamCreateInfoEXT is value-initialized {} with no sType at both sites (~7590, ~8169) — sType stays APPLICATION_INFO when chained into raster.pNext.
  • Nothing emits Xfb decorations — rdna2spirv.py never writes XfbBuffer/XfbStride/XfbOffset or the Xfb execution mode, so any real StreamOutput draw hits VUID-vkCmdBeginTransformFeedbackEXT-None-04128.
  • No buffer has VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT at any of the 6 creation sites -> VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360.
  • begin_stream_output binds VK_NULL_HANDLE for unbound lower slots (TF binds have no null allowance) — needs per-contiguous-run binds.
  • BufferFilledSizeLocation/filled_size_va is stored but never read or written — consecutive draws overwrite; no counter buffers.
  • Structural: first commit message describes indirect signature arg types (already on main) while its diff is the stream-output bulk; device-detection hunks in cd65952 duplicate open d3d12_vulkan_device: detect and enable VK_EXT_transform_feedback (D3D-TRANSFORM-FEEDBACK-DEVICE) #1236 verbatim; merge-base is old (c7e82e2).
  • pipeline_cache_seed AMD regression mechanism confirmed: vk_pipeline_check.cpp does not enable VK_EXT_transform_feedback while the runtime now does.
  • No test exercises a real StreamOutput draw — the new test records SOSetTargets with a dead VA and draws nothing.

Recommend the same conclusion as the existing reviews: not mergeable as-is; needs the Xfb decoration emission + TF usage bits + counter/fill semantics before it can pass validation on a real SO draw. AI-assisted verification.

@adrianmjim

Copy link
Copy Markdown

Rechecked b4e4787d8 on the RX 7900 XT (driver 32.0.32029.2010, Windows 11 26300, VS 2022 RelWithDebInfo). Thanks for splitting it. The query-only scope and the vk_pipeline_check change both look right.

  • The query and replay tests (d3d12_vulkan_device, d3d12_com, d3d12_query_rules, replay_query_edges, replay_query_predication, vk_queue_query_sync, command_list) pass with LEONIDA_VK_VALIDATION=1, with no VUIDs.
  • pipeline_cache_seed still fails, but not because of this PR. It fails the same way, 3 of 3, on this PR's base main ed94fbef9 without it. Integrate post-story-load memory, queue and Vulkan fixes (RT-POST-STORY-RUNTIME) #1723 (ba626820e) made the runtime's device enable robustBufferAccess, with VK_EXT_robustness2's robustBufferAccess2 and nullDescriptor, for bounded indexed indirect draws. vk_pipeline_check still forces robustBufferAccess off, and AMD keys pipelines on it.
  • I checked it with a local experiment that makes the checker mirror those choices: the seed then passes on top of this PR. Mirroring nullDescriptor alone is not enough; robustBufferAccess is the one that matters.

So your transform-feedback line in the checker is needed and correct; there is now a second mismatch from main. I'll open a separate PR for that one, so it doesn't hold this up.

Whole ctest -j 6 on this head: 435 of 438 pass or skip. The failures are d3d12_texture_tiles, d3d12_clear and pipeline_cache_seed, all three on main too.

@adrianmjim

Copy link
Copy Markdown

The other pipeline_cache_seed mismatch, from #1723's robustness choices, is #1833. With both, the seed passes on the RX 7900 XT.

@BubsMcgee

Copy link
Copy Markdown
Contributor Author

Addressed the review points on this update:

AI-assisted.

@BubsMcgee

Copy link
Copy Markdown
Contributor Author

Restructured on top of #1891, which adopts #1236 and carries the vk_pipeline_check parity fix. The device detect/enable and the vk_pipeline_check change are no longer duplicated here; this PR is now only the SO_STATISTICS query pools, stacked on #1891. Once #1891 merges I rebase onto main and the two foundation commits drop.

Also: the remaining AMD pipeline_cache_seed mismatch is #1833 (robustBufferAccess), where I flagged that the checker passes an OR of update-after-bind flags while the runtime passes the six-feature info.bindless AND, so it can still choose a different robustness setting on some devices.

AI-assisted.

BubsMcgee added a commit to BubsMcgee/leonida that referenced this pull request Nov 25, 2026
…g uses it yet

Addresses Snooozs's review on ProjectLeonida#1236: the comments and the log line no longer claim StreamOutput PSO acceptance, SOSetTargets or capture that this foundation does not have. ProjectLeonida#1630 adds the SO_STATISTICS query pools that use it.
With VK_EXT_transform_feedback enabled on the device, an SO_STATISTICS heap is a real VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT pool, begun and ended indexed on the stream (vkCmdBeginQueryIndexedEXT / vkCmdEndQueryIndexedEXT). ResolveQueryData copies the two 64-bit results (NumPrimitivesWritten, PrimitivesStorageNeeded) in the same order. The layer still refuses StreamOutput PSOs and still does not implement SOSetTargets, so no primitive reaches a stream yet and the counts stay 0; without the feature the heap is made without a VkQueryPool and resolves zeros, as before.
@BubsMcgee

Copy link
Copy Markdown
Contributor Author

Merge order for the TF cluster is now on #1891 (the #1608 thread is rejecting writes at 640 comments): #1891 (comment) . This PR is step 2: rebase onto main the moment #1891 lands, dropping the three foundation commits. Ready otherwise.

@BubsMcgee BubsMcgee mentioned this pull request Nov 25, 2026
4 tasks
@aslan2142

Copy link
Copy Markdown
Collaborator

Thanks for narrowing it, @BubsMcgee. The claim is accepted. Still open:

AI-assisted (Claude Code, Claude Opus 5.5).

@BubsMcgee

Copy link
Copy Markdown
Contributor Author

Acknowledged the three remaining items: (1) refuse or split two overlapping SO_STATISTICS queries on the same stream (VUID-vkCmdBeginQueryIndexedEXT-queryPool-04753) with a test, (2) gate the pool on ransformFeedbackQueries and on maxTransformFeedbackStreams for streams above 0, (3) a test that fails without the change. I will take them in that order, rerun d3d12_vulkan_device/d3d12_state_map plain and with LEONIDA_VK_VALIDATION=1, then rebase onto main after #1891 lands.

The three review points on ProjectLeonida#1630:

- prepare() refuses two overlapping SO_STATISTICS queries on one stream: two
  active indexed queries in one pool break
  VUID-vkCmdBeginQueryIndexedEXT-queryPool-04753. Queries on different streams
  are fine.
- The transform-feedback pool now also needs the transformFeedbackQueries
  property, and a stream at or above maxTransformFeedbackStreams runs nothing
  and resolves zero instead of issuing an invalid indexed begin.
- replay_query_predication case 18 fails without both: the overlapping-stream
  list is invalid_query and a stream past the pool's stream count fills zeros.

Tested: d3d12_vulkan_device, d3d12_state_map and replay_query_predication pass
plain and with LEONIDA_VK_VALIDATION=1 (RTX 3080 Ti, driver 616.64). AI-assisted.
@BubsMcgee

Copy link
Copy Markdown
Contributor Author

Took the three review points in 369bb1c75:

  • Overlapping SO_STATISTICS on one stream — prepare() now keeps the active SO queries by heap and stream and returns ReplayResult::invalid_query for a second Begin on a stream already active in that pool (VUID-vkCmdBeginQueryIndexedEXT-queryPool-04753). Different streams still run together. A test that fails without this is case 18 in replay_query_predication_test.
  • Pool gating — an SO_STATISTICS heap only creates VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT when VK_EXT_transform_feedback is on and its transformFeedbackQueries property is set and maxTransformFeedbackStreams is nonzero. A stream at or above maxTransformFeedbackStreams runs nothing and resolves zero (the indexed begin is not issued, so no invalid index), same path as a heap with no pool.
  • Docs/comments — docs/d3d12_vulkan.md §4.6 and the GPU test's comment now say what is gated.

Tested d3d12_vulkan_device, d3d12_state_map and replay_query_predication on an RTX 3080 Ti (driver 616.64), plain and with LEONIDA_VK_VALIDATION=1; all pass. Still stacked on #1891, to be rebased onto main when it lands.

AI-assisted (Claude, opencode).

aslan2142 and others added 3 commits November 25, 2026 17:04
VUID-vkCmdBeginQueryIndexedEXT-queryPool-04753 forbids two active
transform-feedback queries with the same index anywhere in the command
buffer, whatever their pools. prepare() tracked the active ones by heap
and stream, so two heaps beginning stream 0 together still issued two
active indexed queries. It now keeps one bit per stream for the list.

replay_query_predication case 18 adds a second heap: overlapping queries
on one stream in two heaps are refused, and queries that take turns on
that stream are fine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings ProjectLeonida#1630 up to date with main; no conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in main after ProjectLeonida#1891, so main's tip is the single merge base;
no conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142
aslan2142 merged commit 5fab939 into ProjectLeonida:main Nov 25, 2026
2 of 5 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged, thanks! Your three points are in, plus one maintainer commit on top (6af9c80a9):

  • Overlap across heaps: VUID-04753 holds for the whole command buffer, not one pool, so two heaps beginning stream 0 together were still invalid. The check now keeps one bit per stream, and case 18 covers two heaps on one stream: refused when they overlap, fine when they take turns.
  • Updated: main merged into the branch (dd6d3fd8a), with no conflicts.
  • Tested: the full Windows suite under Wine on lavapipe and RADV (gfx1036 iGPU), on exactly this tree: no test went from pass to fail. With the old per-heap key, the new case fails on both devices.
  • Checked on main 1bf5eeafd + 844519ab5: it merges cleanly; the semantic suite passes (493 files, 6,318 tests, 0 files failed); the 15 docs and writer tests pass; check_no_game_data.py is clean on the 7 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

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