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 3 commits into
ProjectLeonida:mainfrom
Miici0:feat/d3d-stream-output-runtime
Open

Miici0 wants to merge 3 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: none (its foundation, #1236, landed through #1891)

What and why

The capture path #1630 (D3D-302) left out: StreamOutput PSOs were refused and SOSetTargets was not implemented. This PR adds that capture path.

A graphics PSO that declares StreamOutput is now accepted instead of refused. The supported case is RasterizedStream 0 with NumStrides within the device's maxTransformFeedbackBuffers. SOSetTargets stores the bound buffers on the command list, the way IASetVertexBuffers already does. A draw whose pipeline has StreamOutput, with every declared buffer slot bound, is wrapped in vkCmdBindTransformFeedbackBuffersEXT + vkCmdBeginTransformFeedbackEXT/vkCmdEndTransformFeedbackEXT, so the captured data reaches the bound buffers.

Rebased onto main f44904d21. Three commits:

  1. The capture path. emit_commands now keeps a rendering scope open across draws, so Begin/End wrap only the draw call itself. Transform feedback is never active when the scope ends or when the next draw binds its pipeline (VUID-vkCmdBindPipeline-None-02323).
  2. BubsMcgee's review: buffers are now created with VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT and VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT at the six creation sites that gate CONDITIONAL_RENDERING the same way (VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360). The BufferFilledSizeLocation counter is resolved as 4 bytes (a D3D12 UINT), not 8.
  3. The test now also draws through a real, zeroed 4-byte BufferFilledSizeLocation counter, and it fails on any validation error when run with LEONIDA_VK_VALIDATION=1.

The counter test settles the open question from the review. After 3 vertices at a 16-byte stride, the counter reads 48. Vulkan's counter records a byte position, the same quantity as D3D12's filled size. So binding BufferFilledSizeLocation directly as the Vulkan counter buffer needs no conversion.

#1630's SO_STATISTICS pools are on main now, and this PR only touches that case's comment: the pool now counts what these draws capture.

Left as follow-up:

Tested

  • d3d12_stream_output passes with LEONIDA_VK_VALIDATION=1 (Khronos validation, 0 errors) and without it. Both draws capture 3 vertices with 0 mismatches, and the counter reads 48 bytes.
  • Full ctest (runtime/build, RelWithDebInfo): 568 of 570 pass. The 2 failures are on main too. I built main f44904d21 without this PR and ran them there, and they fail the same way:

GPU: NVIDIA GeForce RTX 4070 SUPER, driver 621.24, Windows 11 26300.9974, Vulkan SDK 1.4.363.1, MSVC 19.42.34435 (VS 2022 Build Tools).

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work (RasterizedStream != 0, real-shader SPIR-V patching) goes in new pull requests.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • N/A: no new translator rule.

🤖 Generated with Claude Code

@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
@MoonEyes

Copy link
Copy Markdown
Collaborator

Hi @Miici0, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/shims/d3d12_com.h, runtime/shims/d3d12_vulkan_device.cpp, runtime/src/vulkan_backend.h, runtime/src/vulkan_replay.h, runtime/test/vk_test_device.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

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

Miici0 and others added 3 commits November 30, 2026 15:12
…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

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>
…ge bits

BubsMcgee's review on ProjectLeonida#1884: vkCmdBindTransformFeedbackBuffersEXT requires
every bound buffer (and the counter buffers Begin/End take) to have been
created with VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT and
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT
(VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360). NVIDIA does not
enforce this, so the test passed on the 4070 SUPER without it; stricter
drivers (and the Khronos validation layer) catch it. Added at the six buffer
creation sites that gate VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT the
same way, behind the matching transform_feedback flag.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
… on validation errors

BubsMcgee's review on ProjectLeonida#1884: the test passed BufferFilledSizeLocation = 0, so
the counter path was never exercised. A second draw now captures through a
zeroed 4-byte counter (a D3D12 UINT); both buffers must hold the 3 vertices and
the counter must read the bytes written. It reads 48 (3 x 16) on an RTX 4070
SUPER, so the position Vulkan's counter buffer records is the same quantity as
D3D12's filled size, in bytes, and binding BufferFilledSizeLocation as the
Vulkan counter needs no conversion. The test also fails on any validation error
when run with LEONIDA_VK_VALIDATION=1, as in BubsMcgee's fixed branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Miici0
Miici0 force-pushed the feat/d3d-stream-output-runtime branch from 9fc0789 to 6de8bf4 Compare November 30, 2026 15:44
@Miici0

Miici0 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@BubsMcgee thanks for the review and the fixed branch. Both points are in, rebased onto main f44904d21. #1236's copy is dropped since #1891 landed.

  • Usage bits: 79ad1c0e9 adds the transform-feedback buffer and counter usage bits at the six creation sites you listed, behind the same transform_feedback flag that gates CONDITIONAL_RENDERING.
  • Counter width: the counter is resolved as 4 bytes.
  • Test: 6de8bf43b adds a second draw through a real, zeroed 4-byte BufferFilledSizeLocation, and the test now fails on any validation error.

On the open question about counter semantics: after 3 vertices at a 16-byte stride the counter reads 48. Vulkan's counter is a byte position, the same quantity as D3D12's filled size, so binding BufferFilledSizeLocation directly as the counter buffer needs no conversion.

d3d12_stream_output passes with LEONIDA_VK_VALIDATION=1 (0 validation errors) on an RTX 4070 SUPER (driver 621.24). The full suite is 568 of 570. The 2 failures are on main too; see the PR description.

AI-assisted (Claude Code).

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

3 participants