Skip to content

d3d12: dual-source blending reads SV_Target1 at Location 0, Index 1 (RT-DUAL-SOURCE-BLEND) - #1703

Merged
aslan2142 merged 11 commits into
ProjectLeonida:mainfrom
OreoFR:rt-dual-source-blend
Nov 25, 2026
Merged

aslan2142 merged 11 commits into
ProjectLeonida:mainfrom
OreoFR:rt-dual-source-blend

Conversation

@OreoFR

@OreoFR OreoFR commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-DUAL-SOURCE-BLEND (#1608 (comment))

Depends on: #1671, #1592

This builds on #1671: its commits are the first five here and drop out once it merges. Like #1671, it has a one-line #include conflict with #1592. It merges cleanly with #1691, which is also stacked on #1671. The real-boot test also used #1558 and #1547 to get past the landing screen on NVIDIA; this change doesn't need them.

What and why

D3D12 dual-source blending takes the second source from the pixel shader's SV_Target1. The translator declares SV_Target1 as the colour output at Location 1, which is right for two render targets. But Vulkan takes the second blend source from Location 0, Index 1 (VK_BLEND_FACTOR_SRC1_*, dualSrcBlend). So no PSO that blends with a SRC1 factor could be built correctly. #1671 therefore keeps the 1,937 slot 57 records that blend this way (CB_BLEND0 0xf1010f01: colour ONE/SRC1_COLOR, alpha ONE/SRC1_ALPHA, plus a few INV_SRC1 forms) on blending off.

  • runtime/src/dual_source_fragment.h (standard library only), dual_source::remap:
    • moves the fragment module's non-builtin Output at Location 1 to Location 0 and adds OpDecorate ... Index 1 next to it;
    • refuses, leaving the words unchanged, a module that has no such output, has an output at Location 2 or above (Vulkan has one dual-source attachment), or already carries an Index.
  • runtime/shims/d3d12_vulkan_device.cpp: when an enabled target's blend uses D3D12_BLEND_(INV_)SRC1_COLOR/ALPHA, the fragment module is remapped at pipeline creation, beside the existing B4G4R4A4 output rewrite. A refused module refuses the PSO through stream_unsupported, as today. This applies to every graphics PSO, not only slot 57 ones.
  • runtime/src/native_pso.h: d3d12_xs: blend state for slot 57 native pipelines from state blob A (RT-SLOT57-BLEND-STATE) #1671's blend mapping now takes the SRC1 factors (SRC1_COLOR / INV_SRC1_COLOR read as their alpha forms in the alpha factors). Only the BOTH_* factors and an unknown COMB_FCN stay refused.
  • runtime/test/dual_source_fragment_test.cpp (portable): hand-built word streams test the rewrite and each refusal. The input at Location 1 and the FragDepth builtin are left alone. native_pso_blend_test covers the SRC1 mappings. The layout note in docs/fspx.md is updated.

Tested

GPUs tested: NVIDIA GeForce RTX 4070 Laptop GPU, driver 591.59 (32.0.15.9159), Windows 11

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase). It's stacked on d3d12_xs: blend state for slot 57 native pipelines from state blob A (RT-SLOT57-BLEND-STATE) #1671, as stated above.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not a translator change: the rewrite happens at pipeline creation; the measurement is in docs/fspx.md.)

🤖 Generated with Claude Code

OreoFR and others added 7 commits November 25, 2026 08:35
…(RT-SLOT57-BLEND-STATE)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ry (RT-SLOT57-BLEND-STATE)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…PSOs (RT-SLOT57-BLEND-STATE)

A library pipeline enables MAX on an R8_UINT target; the colour block bypasses
blending there, and D3D12 refuses the PSO.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…with blending off on its stand-in targets (RT-SLOT57-BLEND-STATE)

On devices without renderable B4G4R4A4, the Vulkan layer refuses blending on the
RGBA8 stand-in (E_NOTIMPL). Turn blending off on those targets, then on every
target, before leaving the pipeline to the PC description path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…RT-DUAL-SOURCE-BLEND)

A PSO whose blend state uses a SRC1 factor gets its fragment module's Location 1
colour output moved to Location 0 with Index 1, as Vulkan takes the second
source. Slot 57 native PSOs now map the SRC1 factors of state blob A.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ts (RT-DUAL-SOURCE-BLEND)

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

@kawaii-shakal kawaii-shakal 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.

Review (read-only, not run): one likely bug in the lazy path.

Lazy pipelines lose the dual-source remap. PipelineState remaps the fragment module (dual_source::remap(stage.words)) at creation. But graphics pipelines are lazy by default (lazy_graphics_pipelines(), off only with LEONIDA_LAZY_PIPELINES=0). When a lazy pipeline is first drawn, build() gets its SPIR-V again through stage_words(), and stage_words() replays rgba4::quantize (rgba4_mask_), link_f16_halves and depth24_patch_fragment, but not the dual-source remap. With the default settings a dual-source pipeline is therefore built with SV_Target1 still at Location 1 and SRC1 blend factors, which is invalid in Vulkan (the second source has to come from Location 0, Index 1). The blend reads an undefined second source, or a driver refuses the pipeline.

Suggested fix: keep a dual_source_ flag next to rgba4_mask_ and apply leonida::dual_source::remap(words[i]) to the fragment stage (source.slot == 2) in stage_words(), failing the same way. A ctest that creates a dual-source PSO with lazy pipelines on and draws it once would catch it.

If the slot 57 factory's pipelines never take the lazy path, please say so in the PR; the PC CreateGraphicsPipelineState path does.

Checked and fine: spirv() returns a copy, so the in-place remap does not leak into other pipelines that share the pixel shader. dualSrcBlend is already required and enabled on main (d3d12_vulkan_device.cpp ~7705 and ~16450).

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

…raw test (RT-DUAL-SOURCE-BLEND review)

A lazy graphics pipeline (the default) makes its stage words again in build()
through stage_words(), which replayed the B4G4R4A4 quantisation but not the
Location 0 / Index 1 remap. Both now go through rewrite_fragment(), the same at
creation and in a lazy build.

d3d12_translated_draw_test --dual-source draws ps_m15_dual (ps_m15 plus a
constant SV_Target1) with ONE + SRC1_COLOR / SRC1_ALPHA, lazy and at creation:
every covered pixel is exact. Without the lazy fix, NVIDIA still draws the
right colours but LEONIDA_VK_VALIDATION=1 reports 3 errors.

Found by kawaii-shakal's review of ProjectLeonida#1703.

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

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@kawaii-shakal thanks, that was a real bug. Fixed in 1943a92f7:

  • One rewrite for both paths. The fragment rewrites are now one member, rewrite_fragment(): the B4G4R4A4 quantisation, then the Location 0, Index 1 remap under a new dual_source_ flag. Both the creation path and stage_words() call it, so a lazy build() makes the same words as creation, and a refusal fails the same way in both.
  • A draw test. d3d12_translated_draw_test --dual-source draws ps_m15_dual.asm, which is ps_m15 plus a constant SV_Target1 (0.5), with ONE + SRC1_COLOR / SRC1_ALPHA over the clear colour. Every covered pixel must be exact. It runs twice, lazily (d3d12_dual_source_draw) and at creation (d3d12_dual_source_draw_eager, LEONIDA_LAZY_PIPELINES=0). The test device now enables dualSrcBlend where the GPU has it, and the test exits 77 where it doesn't. ps_m15_dual carries one more FSSM data word, because the layer finds a PSO's shader by FSSM hash and it would otherwise collide with ps_m15's.
  • What the test shows. Without the fix, the RTX 4070 Laptop still drew the right colours: NVIDIA appears to take Location 1 as the second source. So the colours alone don't catch it, but LEONIDA_VK_VALIDATION=1 does, with 3 errors on the secondary-color-input / max Location rule. With the fix, it's clean and all 4,064 covered pixels are exact.

The slot 57 factory goes through the same PC CreatePipelineState path, so it was affected too.

AI-assisted.

@potatoe0100

Copy link
Copy Markdown
Contributor

Tested 1943a92f7 merged onto main bcaa13dad (clean), native Windows 11 Pro 26H2 (build 26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 RelWithDebInfo + Ninja, llvm-mc 21.1.8.

  • Full ctest -j 6: all pass, including d3d12_dual_source_draw, d3d12_dual_source_draw_eager, d3d12_translated_draw, dual_source_fragment and native_pso_blend.
  • run_gpu_tests.py: one new failure vs main. The new ps_m15_dual.asm is picked up by the generic pixel-shader harness, and on this GPU it ends in a lost device, every time (3 of 3):
    FAIL  ps_m15_dual           device: NVIDIA GeForce RTX 4060 (subgroup 32..32)
    FAILED vkQueueWaitIdle(q) -> -4 (line 1181)
    
    ps_m15 is listed in OTHER_HARNESS in scripts/run_gpu_tests.py (run by d3d12_translated_draw_test) but ps_m15_dual isn't. With "ps_m15_dual": "d3d12_translated_draw_test" added there locally, the suite is back to main's result: 209/264 passed, 54 skipped, SKIP ps_m15_dual (run by d3d12_translated_draw_test).

AI-assisted testing (non-developer tester).

… ps_m15 (RT-DUAL-SOURCE-BLEND review)

The generic pixel-shader harness picked up the new fixture and lost the device on an RTX 4060: it has no second
colour target and no SRC1 blend. d3d12_translated_draw_test --dual-source owns that draw.

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

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@potatoe0100 thanks, good catch. ps_m15_dual is now in OTHER_HARNESS next to ps_m15 (188207a9d): run_gpu_tests.py still assembles and translates it, and d3d12_translated_draw_test --dual-source judges the draw. The generic pixel-shader harness has no second colour target or SRC1 blend for it, which is presumably why the 4060 lost the device. tests/test_run_gpu_tests.py passes (68 tests).

AI-assisted.

@adrianmjim

Copy link
Copy Markdown

Tested 1943a92f7 applied onto main 42537e7b6; 188207a9d since then only adds the OTHER_HARNESS entry. The test machine is AMD: Radeon RX 7900 XT (RDNA3), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, VS 2022 RelWithDebInfo. No game files.

The new draw tests pass on AMD. d3d12_dual_source_draw (lazy) and d3d12_dual_source_draw_eager both print SV_Target1 blended as the second source (ONE + SRC1_COLOR / SRC1_ALPHA), 4064 of 4096 pixels exact. With dual_source_fragment, native_pso_blend, d3d12_translated_draw, d3d12_pso_validation, d3d12_state_sweep and xbox_native_base_draw that makes 8 of 8 pass, plain and with LEONIDA_VK_VALIDATION=1 (validation on, no messages). So the rewrite_fragment() fix works on this driver in both modes.

Whole ctest -j 6: 401 of 406 pass or skip. Three failures are the same as on main: d3d12_texture_tiles, d3d12_clear and d3d12_geometry_views. xmem and xmem_write_watch failed once under -j 6 and pass 3 of 3 alone. They are load-sensitive and have failed like that on other trees here too.

I did not run ps_m15_dual through run_gpu_tests.py. The lost device on the RTX 4060 above comes from the generic pixel harness picking it up, and 188207a9d's OTHER_HARNESS entry fixes that.

One thing that is still there: the slot 57 blend fallback fires on any E_NOTIMPL. if (hr == E_NOTIMPL && plan.state.blend) retries with blending off on B4G4R4A4 targets, and then on every target. E_NOTIMPL is also what stream_unsupported() and a few direct returns give for refusals that have nothing to do with B4G4R4A4: rect lists without the geometryShader feature or whose geometry stage can't be generated, mesh limits, two stages reading their code base, FullyCovered. This PR's own "outputs do not fit Location 0 Index 1" refusal is caught too, and the retry then turns the SRC1 blend off.

For any of those, a pipeline whose decoded blend differs from the default is retried with blending off. If the refusal had nothing to do with blending, that retry fails the same way, but the log still says "the device refused this pipeline's blend state". If it does succeed, the pipeline draws without the blending the title asked for. Checking that the refusal was the B4G4R4A4 one, for example with a distinct HRESULT or a flag set where that refusal is made, would keep the fallback to the case it is meant for.

@MoonEyes MoonEyes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and tested on Windows 11 with MSVC 19.44 (scripts\build_runtime.ps1, incremental on the same build tree, after #1691), NVIDIA GeForce RTX 4060, driver 610.88. Head 188207a (it carries #1671's commits; branch point main 3f81d46, not merged with current main 237deb6).

ctest -R "native_pso|dual_source|translated_draw|xs_proxy_mock|xbox_native_base_draw": 15/15 passed, including dual_source_fragment, native_pso_blend, and the GPU draws d3d12_translated_draw, xbox_native_base_draw, d3d12_dual_source_draw and d3d12_dual_source_draw_eager.

Not checked: the dual-source result in a real world frame, which needs a world frame my setup does not reach yet.

AI-assisted: yes (Claude Code ran the build and the tests; I read the results).

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Tested as part of the #1671 + #1691 + #1703 stack (188207a) on main bcaa13d, in a real boot on an RTX 5070 Ti (NVIDIA 617.42, Windows 11). Menu text renders as glyphs instead of solid blocks, and no pipeline is refused. Details: #1671. AI-assisted (Claude Code, Claude Opus 5.5).

OreoFR and others added 2 commits November 25, 2026 13:00
…d-in (RT-SLOT57-BLEND-STATE review)

adrianmjim's review: the blend fallback ran on any E_NOTIMPL, which also stands for
refusals unrelated to blending (stream_unsupported, mesh limits, FullyCovered, and
ProjectLeonida#1703's dual-source refusal). Such a pipeline was retried with blending off, logged as
a blend refusal, and, if the retry succeeded, drew without the blend the title asked for.

Now only a pipeline with a blended B4G4R4A4 target is retried, with blending off on
those targets alone (without_stand_in_blend, its own header), and the line is printed
only when that retry succeeds. The "every target" step is gone: no real boot used it
(RTX 4070 Laptop and kawaii-shakal's RTX 5070 Ti only ever logged the B4G4R4A4 step).
native_pso_fallback_test covers the decision.

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

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@adrianmjim thanks for the AMD run, and you're right about the fallback. Fixed in #1671 (668d65b4b), merged into this branch (b19b00e88) and #1691 (7a769396d):

  • Only a pipeline that blends a B4G4R4A4 target is retried, with blending off on those targets alone (without_stand_in_blend, in its own header d3d12_xs_native_pso_fallback.h). Any other E_NOTIMPL is returned as it is, with the blend the title asked for. That includes this PR's "does not fit Location 0 Index 1" refusal.
  • The "every target" step is gone. No real boot used it: my RTX 4070 Laptop runs and kawaii-shakal's RTX 5070 Ti boot only ever logged the B4G4R4A4 step.
  • The line is printed only when that retry succeeds, and it now names the stand-in rather than "this pipeline's blend state".
  • New native_pso_fallback test covers the decision: a blended B4G4R4A4 target is retried, while no B4G4R4A4 target, a B4G4R4A4 target that doesn't blend, or one past NumRenderTargets leaves the description untouched.

On main 0d80bc5df + the three PRs (RTX 4070 Laptop, NVIDIA 621.40), ctest -R "native_pso|dual_source|translated_draw|xs_proxy_mock|xbox_native_base_draw" passes 17/17, and gta6rt builds.

AI-assisted.

@aslan2142
aslan2142 merged commit 77baf90 into ProjectLeonida:main Nov 25, 2026
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged. Thank you, @OreoFR, and thanks to @kawaii-shakal, @potatoe0100 and @adrianmjim for the findings and to @MoonEyes for the independent pass.

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

Snooozs added a commit to Snooozs/leonida that referenced this pull request Nov 27, 2026
Conflicts: the blend fallback of ProjectLeonida#1671/ProjectLeonida#1703 for B4G4R4A4 stand-ins moves into the build() that ProjectLeonida#1592 split out, so
it runs for both stage sources; the translated draw test keeps --native-fspx and --dual-source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

6 participants