Skip to content

d3d12: accept StreamOutput PSOs, implement SOSetTargets, capture draws via VK_EXT_transform_feedback (D3D-STREAMOUTPUT-RUNTIME) - #1884

Open
Miici0 wants to merge 2 commits into
ProjectLeonida:mainfrom
Miici0:feat/d3d-stream-output-runtime
Open

Miici0 wants to merge 2 commits into
ProjectLeonida:mainfrom
Miici0:feat/d3d-stream-output-runtime

Conversation

@Miici0

@Miici0 Miici0 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-STREAMOUTPUT-RUNTIME (#1608 (comment))

Depends on: #1236

What and why

The capture path #1630 (D3D-302) explicitly left unclaimed: "StreamOutput PSOs are still refused and SOSetTargets is still not implemented; the capture path is not part of this PR." This PR is that capture path.

A graphics PSO that declares StreamOutput is now accepted instead of refused (the common case: RasterizedStream 0, NumStrides within the device's maxTransformFeedbackBuffers). SOSetTargets stores the bound buffers on the command list the same way IASetVertexBuffers already does. A draw whose pipeline has StreamOutput and every declared buffer slot currently bound wraps itself in vkCmdBindTransformFeedbackBuffersEXT + vkCmdBeginTransformFeedbackEXT/vkCmdEndTransformFeedbackEXT, writing real captured data to the bound buffers.

Stacked on #1236, same as #1630: the first commit is #1236's device detect/enable, unchanged. When #1236 merges I will rebase and drop that copy.

Touches the same few files as #1630 (d3d12_com.h, d3d12_vulkan_device.cpp, vulkan_backend.h, vulkan_replay.h) but a different part of each - the query-heap code I touched is only a comment update (the SO_STATISTICS pools themselves stay #1630's). A rebase against each other may be needed depending on merge order.

Not done, left as follow-up (noted in the code and in docs/d3d12_vulkan.md):

  • RasterizedStream other than 0 (needs VkPipelineRasterizationStateStreamCreateInfoEXT / rasterizerDiscardEnable at pipeline creation)
  • SO_STATISTICS queries (d3d12: real SO_STATISTICS query pools (D3D-302) #1630's follow-up)
  • Matching a real translated game shader's output semantics to StreamOutput buffer slots, which needs patching Xfb decorations into its SPIR-V at PSO-creation time (the translator runs before any PSO exists, so it cannot know this ahead of time)

Tested

New test d3d12_stream_output_test.cpp: a StreamOutput PSO with a hand-written vertex shader (runtime/test/glsl/vs_stream_output.vert, Xfb-decorated, writes the fixed vec4 (1,2,3,4)) draws 3 vertices; the buffer SOSetTargets bound is read back and every vertex's 16 bytes are checked against that constant. 0 mismatches on real hardware.

Full ctest (runtime/build, RelWithDebInfo): 307/308 pass (one more test than main, the new one); the only failure is the pre-existing d3d12_execute_indirect_validation (#504, fixed by the separate PR #1066, unrelated to this change). No regressions.

GPUs tested: NVIDIA GeForce RTX 4070 SUPER, driver 621.24, Windows 11 26300.9974, Vulkan SDK 1.4.363.1.

AI-assisted: yes (Claude Code)

Checklist

Miici0 and others added 2 commits November 24, 2026 21:40
Foundation for D3D-302 (stream output): read the physical device's
transformFeedback/geometryStreams features where the extension is
present, record them on VulkanDeviceInfo, and enable them on the
logical device when supported, exactly the same pattern the existing
VK_EXT_mesh_shader detection/enable code already follows.

Device-capability plumbing only: no behaviour changes yet. A graphics
PSO declaring StreamOutput is still refused and SOSetTargets is still
not implemented, same as before this commit. Confirmed
VK_EXT_transform_feedback is available (4 streams, 4 buffers) on an
NVIDIA GeForce RTX 4070 SUPER, driver 621.24.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…s via VK_EXT_transform_feedback

Builds on D3D-TRANSFORM-FEEDBACK-DEVICE (the device-capability plumbing): a
graphics PSO that declares StreamOutput is now accepted instead of refused
(the common case only: RasterizedStream 0, NumStrides within the device's
maxTransformFeedbackBuffers), SOSetTargets stores the bound buffers on the
command list the same way IASetVertexBuffers does, and a draw with a pipeline
that has StreamOutput and every declared buffer slot bound wraps itself in
vkCmdBindTransformFeedbackBuffersEXT + vkCmdBeginTransformFeedbackEXT /
vkCmdEndTransformFeedbackEXT, writing real captured data.

Not done, left as follow-up:
- RasterizedStream other than 0 (needs VkPipelineRasterizationStateStreamCreateInfoEXT
  / rasterizerDiscardEnable at pipeline creation)
- SO_STATISTICS queries (still stubbed to 0; the real wiring is
  VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT with the indexed
  vkCmdBeginQueryIndexedEXT/EndQueryIndexedEXT commands)
- Matching a real translated game shader's output semantics to StreamOutput
  buffer slots, which needs patching Xfb decorations into its SPIR-V at PSO
  creation time (the translator runs before any PSO exists, so it cannot know
  this ahead of time) - this PR's own test uses a hand-written GLSL shader
  with the decorations already in place, not a translated one

### Tested

New test d3d12_stream_output_test.cpp: a StreamOutput PSO with a hand-written
vertex shader (runtime/test/glsl/vs_stream_output.vert, Xfb-decorated,
writes the fixed vec4 (1,2,3,4)) draws 3 vertices; the buffer SOSetTargets
bound is read back and every vertex's 16 bytes are checked against that
constant. 0 mismatches on real hardware.

Full ctest run (runtime/build, RelWithDebInfo): 307/308 pass (one more test
than main, the new one); the only failure is the pre-existing
d3d12_execute_indirect_validation (ProjectLeonida#504, fixed by the separate PR ProjectLeonida#1066,
unrelated to this change). No regressions.

GPU: NVIDIA GeForce RTX 4070 SUPER, driver 621.24, Windows 11 26300.9974.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@BubsMcgee

Copy link
Copy Markdown
Contributor

Nice work — this is the capture path we were also poking at. Two concrete gaps from our own capture work (BubsMcgee/leonida:stream-output-capture-wip, commit 4f05ff666), both static (I couldn't run this branch: it conflicts with current main).

1. The TF buffer usage bits are missing. vkCmdBindTransformFeedbackBuffersEXT requires every bound buffer to have been created with VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT, and the counter buffers passed to begin/end with VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT. Current main has no TRANSFORM_FEEDBACK_*_BIT_EXT usage anywhere (git grep over runtime/), and this PR binds the SOSetTargets buffers and the filled-size counters without adding them. The new test creates its SO buffer directly in D3D12_RESOURCE_STATE_STREAM_OUT, but that is only a barrier layout (s::stream_out in command_list.h / vulkan_replay.h), not a creation usage, so it does not get the bit either. The test passes on the 4070 SUPER because NVIDIA does not enforce it; with LEONIDA_VK_VALIDATION=1 (and on stricter drivers / the real title's buffers, which are created in other states) the bind is invalid. Fix: add both bits to every buffer when info.transform_feedback is on. Our WIP commit has the six creation sites in runtime/shims/d3d12_vulkan_device.cpp:

if (info.transform_feedback)
    result.usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
                    VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;

2. Counter width. D3D12_STREAM_OUTPUT_BUFFER_VIEW::BufferFilledSizeLocation points to a 32-bit UINT, so counter_size should be sizeof(std::uint32_t), not sizeof(std::uint64_t); with 8, buffer_at can reject a 4-byte counter buffer. (Our WIP used 4.)

Also, the test passes BufferFilledSizeLocation = 0, so the counter path is not exercised at all — worth a second case with a real 4-byte filled-size buffer, under validation.

One semantic question: Vulkan writes a primitive/vertex count to the counter buffer, while D3D12's filled size is bytes. Binding the BufferFilledSizeLocation as the Vulkan counter writes a different quantity, so if the title reads the filled size it needs its own mapping. (Our WIP made the same binding, so this is an open question we share, not a regression you introduced.)

Coordination: #1891 adopts #1236 (with the Snooozs comment/log fix) and has an NVIDIA LGTM; once it lands, this can rebase onto main and drop the #1236 copy. #1630 (SO_STATISTICS) is also stacked and touches the same files, so one of us rebases after the other. Happy to hand over the WIP commit or test this once it rebases.

AI-assisted.

@BubsMcgee

Copy link
Copy Markdown
Contributor

Verified the usage-bit finding end to end on Windows 11 / NVIDIA RTX 3080 Ti (driver 616.64, Vulkan SDK 1.4.363.0, Khronos validation layer), by rebasing the capture commit onto our #1891 foundation and running the new test.

  • Without the TF usage bits (the branch as it stands): d3d12_stream_output fails under LEONIDA_VK_VALIDATION=1 with
    VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360 — "pBuffers[0] was created with ..." and no VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT.
  • With the fix (the six creation sites in d3d12_vulkan_device.cpp, counter_size = sizeof(std::uint32_t), and EXPECT(vk.validation_errors() == 0) in the test): passes plain and with validation, no errors.

Branch to cherry-pick: BubsMcgee/leonida:cap-1884-fixed (head e79908b9e):

The usage-bit hunk is the same at every site:

if (<info>.transform_feedback)
    <usage> |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
               VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;

(result.usage in DeviceState, create.usage for the heap, info.usage at the three resource sites, state_.info for the default-heap buffers.)

The counter semantics note from before still stands (Vulkan's counter is a primitive/vertex count; D3D12's filled size is bytes). Happy to open a PR against your branch if that is easier, or you can cherry-pick the two commits.

AI-assisted.

@BubsMcgee

BubsMcgee commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Heads-up: this is CONFLICTING with main (our duplicate copy, #1925, is closed, so there is no competing implementation). The vk_pipeline_check parity hunk this feature needs is already in #1891; everything else here — StreamOutput PSO acceptance, SOSetTargets, the capture path and the tests — is unique to this branch. A rebase onto current main should be all that is left.

(Correction to my first version: #1148 is another older conflicting PR, so this is not the only one; both need a rebase.)

@BubsMcgee

Copy link
Copy Markdown
Contributor

Merge order for the TF cluster is on #1891: #1891 (comment) . This PR is step 3: rebase onto main after #1891 lands (your first commit is #1236, which #1891 adopts, so that copy drops). The TF buffer-usage + 4-byte counter fixes, with the test passing under validation, are in BubsMcgee/leonida:cap-1884-fixed.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026
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.

2 participants