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

Open
github-bot wants to merge 2 commits from github-bot/gh-1884 into main AGit
Owner

@⁠Miici0 on GitHub · 2026-11-25 14:15 UTC · original
Miici0:feat/d3d-stream-output-runtime → main · +477 −7 · 11 files · 2 commits · open

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

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 (#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

  • Based on current main (stacked on #1236) and merges cleanly.
  • One topic; follow-up work (RasterizedStream != 0, SO_STATISTICS, 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.
**@⁠Miici0** on GitHub · 2026-11-25 14:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1884) `Miici0:feat/d3d-stream-output-runtime` → `main` · +477 −7 · 11 files · 2 commits · open Claimed in the coordination issue as: D3D-STREAMOUTPUT-RUNTIME (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) 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 (#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 - [x] Based on current `main` (stacked on #1236) and merges cleanly. - [x] One topic; follow-up work (RasterizedStream != 0, SO_STATISTICS, real-shader SPIR-V patching) goes in new pull requests. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [ ] N/A: no new translator rule.
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 <[email protected]>
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 (#504, fixed by the separate PR #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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:54 +00:00
Author
Owner

@⁠BubsMcgee on GitHub · 2026-11-25 15:18 UTC · original

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** on GitHub · 2026-11-25 15:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1884#issuecomment-7166638089) 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`: ```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.
Author
Owner

@⁠BubsMcgee on GitHub · 2026-11-25 15:26 UTC · original

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):

  • 2fc3f1f0e — the capture commit rebased onto #1891's foundation (adopted #1236 + vk_pipeline_check + Snooozs's comment fix), with the six usage-bit sites and the 4-byte counter.
  • e79908b9e — the test now asserts validation_errors() == 0.

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** on GitHub · 2026-11-25 15:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1884#issuecomment-7166844196) 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`): - `2fc3f1f0e` — the capture commit rebased onto #1891's foundation (adopted #1236 + `vk_pipeline_check` + Snooozs's comment fix), with the six usage-bit sites and the 4-byte counter. - `e79908b9e` — the test now asserts `validation_errors() == 0`. The usage-bit hunk is the same at every site: ```cpp 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.
Author
Owner

@⁠BubsMcgee on GitHub · 2026-11-25 17:11 UTC · original · edited 2026-11-25 17:26 UTC

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** on GitHub · 2026-11-25 17:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1884#issuecomment-7169147448) · edited 2026-11-25 17:26 UTC 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.)
Author
Owner

@⁠BubsMcgee on GitHub · 2026-11-25 17:28 UTC · original

Merge order for the TF cluster is on #1891: #1891 . 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.

**@⁠BubsMcgee** on GitHub · 2026-11-25 17:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1884#issuecomment-7169548155) Merge order for the TF cluster is on #1891: https://git.gtahub.st/github/ubiquitarianism--leonida/pulls/1891 . 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.
This pull request has changes conflicting with the target branch.
  • 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
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1884/head:github-bot/gh-1884'
git switch 'github-bot/gh-1884'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1884'
git switch 'github-bot/gh-1884'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1884'
git switch 'github-bot/gh-1884'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1884'
git switch 'main'
git merge --squash 'github-bot/gh-1884'
git switch 'main'
git merge --ff-only 'github-bot/gh-1884'
git switch 'main'
git merge 'github-bot/gh-1884'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.