From 70d351788924726089efac6344da8a49f8e45d39 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:35:47 +0100 Subject: [PATCH 01/10] d3d12_xs: blend state for slot 57 native pipelines from state blob A (RT-SLOT57-BLEND-STATE) Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 2 + runtime/shims/d3d12_xs_native_pso.h | 42 +++++++- runtime/src/native_pso.h | 102 +++++++++++++++++++ runtime/test/native_pso_blend_test.cpp | 130 +++++++++++++++++++++++++ 4 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 runtime/test/native_pso_blend_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 2763e9d1b0..a725881f0d 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1358,6 +1358,8 @@ if(BUILD_TESTING) add_test(NAME descriptor_table COMMAND descriptor_table_test) add_executable(shader_library_test test/shader_library_test.cpp) add_test(NAME shader_library COMMAND shader_library_test) + add_executable(native_pso_blend_test test/native_pso_blend_test.cpp) + add_test(NAME native_pso_blend COMMAND native_pso_blend_test) add_executable(spirv_f16_halves_test test/spirv_f16_halves_test.cpp) add_test(NAME spirv_f16_halves COMMAND spirv_f16_halves_test) # Optional: skips unless LEONIDA_SHADERS names a translated work folder (/shaders/spirv). diff --git a/runtime/shims/d3d12_xs_native_pso.h b/runtime/shims/d3d12_xs_native_pso.h index ba8537c5fc..3ad62ffb72 100644 --- a/runtime/shims/d3d12_xs_native_pso.h +++ b/runtime/shims/d3d12_xs_native_pso.h @@ -17,7 +17,9 @@ #include "../src/shader_library.h" #include +#include #include +#include #include #include #include @@ -30,8 +32,46 @@ namespace rt::d3d12_xs { namespace native_pipeline_detail { namespace np = leonida::native_pso; +// The blend state of blob A's colour-target registers (RT-SLOT57-BLEND-STATE). Without it, or for a target this path +// cannot express (np::d3d12_blend), every target keeps blending off and writes every component, as before. +inline void fill_blend_state(const np::FixedState& s, D3D12_BLEND_DESC& out) { + for (auto& rt : out.RenderTarget) { + rt = {}; + rt.SrcBlend = rt.SrcBlendAlpha = D3D12_BLEND_ONE; + rt.DestBlend = rt.DestBlendAlpha = D3D12_BLEND_ZERO; + rt.BlendOp = rt.BlendOpAlpha = D3D12_BLEND_OP_ADD; + rt.LogicOp = D3D12_LOGIC_OP_NOOP; + rt.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + } + out.AlphaToCoverageEnable = FALSE; + out.IndependentBlendEnable = FALSE; + const auto targets = s.blend ? np::d3d12_blend(*s.blend, s.rt_count) : std::nullopt; + if (!targets) { + static std::atomic logged{0}; + if (logged.fetch_add(1, std::memory_order_relaxed) < 4) + std::fprintf(stderr, "[rt] d3d12_xs: slot 57: %s, so this pipeline keeps blending off (CB_BLEND0 %08x)\n", + s.blend ? "a target's blend state is not one this path builds (dual-source or unknown factor)" + : "blob A is absent or not the measured shape", + s.blend ? static_cast(s.blend->blend[0]) : 0u); + return; + } + for (unsigned i = 0; i < s.rt_count; ++i) { + const np::TargetBlend& t = (*targets)[i]; + D3D12_RENDER_TARGET_BLEND_DESC& rt = out.RenderTarget[i]; + rt.BlendEnable = t.enable; + rt.SrcBlend = static_cast(t.src); + rt.DestBlend = static_cast(t.dst); + rt.BlendOp = static_cast(t.op); + rt.SrcBlendAlpha = static_cast(t.src_alpha); + rt.DestBlendAlpha = static_cast(t.dst_alpha); + rt.BlendOpAlpha = static_cast(t.op_alpha); + rt.RenderTargetWriteMask = t.write_mask; + if (i && std::memcmp(&rt, &out.RenderTarget[0], sizeof(rt)) != 0) out.IndependentBlendEnable = TRUE; + } +} + inline void fill_fixed_state(const np::FixedState& s, D3D12_GRAPHICS_PIPELINE_STATE_DESC& pc) { - for (auto& rt : pc.BlendState.RenderTarget) rt.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + fill_blend_state(s, pc.BlendState); pc.SampleMask = s.sample_mask ? s.sample_mask : UINT_MAX; pc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; pc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index 8982c701ac..b1ad26df39 100644 --- a/runtime/src/native_pso.h +++ b/runtime/src/native_pso.h @@ -93,9 +93,17 @@ struct InputElement { std::uint32_t step_rate = 0; }; +// The colour-target registers of state blob A (RT-SLOT57-BLEND-STATE, docs/fspx.md "State blob A"): CB_TARGET_MASK +// and CB_BLEND0..7_CONTROL as the console's driver serialized them, in the RDNA2 register layout. +struct BlendRegisters { + std::uint32_t target_mask = 0; // CB_TARGET_MASK: four component bits (R, G, B, A) per target + std::array blend{}; // CB_BLENDn_CONTROL +}; + // State blob B. The u32 field widths after each name are a hypothesis that fits the // measured sizes: 0x50 bytes with no element, and 188 bytes with POSITION, COLOR and TEXCOORD. struct FixedState { + std::optional blend; // from blob A; empty when blob A is absent or not the measured shape std::array rtv_formats{}; // +0x10, DXGI_FORMAT values std::uint32_t sample_mask = 0; // +0x1c std::uint8_t dsv_format = 0; // +0x24 @@ -153,6 +161,99 @@ inline std::optional decode_state_b(std::span b, return s; } +// State blob A, measured on 1.0.2.1 over all 5,680 distinct blobs (25,471 graphics records, docs/fspx.md "State blob +// A"): 380 + 8k bytes (k = 0..5) for vertex-shader pipelines and 464 + 8k (k = 0..7) for mesh pipelines, whose stage +// block is 84 bytes longer, so every offset after +0x28 moves by 84 there. Word 0 is 0x4005d800 with bits 3 and 4 +// free. CB_TARGET_MASK is at +0x78 and CB_BLEND0..7_CONTROL at +0xd0..+0xec (vertex-pipeline offsets). Any other +// shape is not decoded: the caller keeps its defaults. +inline constexpr std::uint32_t kStateATag = 0x4005d800; +inline constexpr std::size_t kStateATargetMask = 0x78, kStateABlend = 0xd0, kStateAMeshShift = 84; + +inline std::optional decode_state_a_blend(std::span a, const char** why = nullptr) { + const auto fail = [&](const char* reason) { + if (why) *why = reason; + return std::nullopt; + }; + const std::size_t n = a.size(); + std::size_t shift = 0; + if (n >= 380 && n <= 420 && (n - 380) % 8 == 0) shift = 0; + else if (n >= 464 && n <= 520 && (n - 464) % 8 == 0) shift = kStateAMeshShift; + else return fail("blob A size not one of the measured shapes"); + if ((read_u32(a, 0) & ~0x18u) != kStateATag) return fail("blob A tag not 0x4005d800"); + BlendRegisters r; + r.target_mask = read_u32(a, kStateATargetMask + shift); + for (std::size_t i = 0; i < r.blend.size(); ++i) r.blend[i] = read_u32(a, kStateABlend + shift + 4 * i); + return r; +} + +// One target's blend state in D3D12 terms, with D3D12_BLEND and D3D12_BLEND_OP values (d3d12.h) so this header +// stays standard-library only. +struct TargetBlend { + bool enable = false; + std::uint8_t src = 2, dst = 1, op = 1; // D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD + std::uint8_t src_alpha = 2, dst_alpha = 1, op_alpha = 1; + std::uint8_t write_mask = 0xf; // D3D12_COLOR_WRITE_ENABLE_*: R 1, G 2, B 4, A 8 +}; + +// An RDNA2 blend factor (V_028780_BLEND_*, the gfx10 register header) as a D3D12_BLEND, or 0 for one this path does +// not take: BOTH_(INV_)SRC_ALPHA (11, 12; not on gfx10) and the dual-source SRC1 factors (15 to 18), whose second +// pixel-shader output the translator does not declare. For an alpha factor, a colour factor names its alpha. +inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { + static constexpr std::uint8_t colour[21] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 14, 15, 0, 0, 0, 0, 20, 21}; + if (f >= std::size(colour)) return 0; + if (alpha) { + switch (f) { + case 2: return 5; // SRC_COLOR -> SRC_ALPHA + case 3: return 6; // ONE_MINUS_SRC_COLOR -> INV_SRC_ALPHA + case 8: return 7; // DST_COLOR -> DEST_ALPHA + case 9: return 8; // ONE_MINUS_DST_COLOR -> INV_DEST_ALPHA + default: break; + } + } + return colour[f]; +} + +// An RDNA2 COMB_FCN as a D3D12_BLEND_OP, or 0: DST_PLUS_SRC ADD, SRC_MINUS_DST SUBTRACT, MIN, MAX, DST_MINUS_SRC +// REV_SUBTRACT. +inline std::uint8_t d3d12_blend_op(std::uint32_t c) { + static constexpr std::uint8_t op[5] = {1, 2, 4, 5, 3}; + return c < std::size(op) ? op[c] : 0; +} + +// CB_BLENDn_CONTROL: COLOR_SRCBLEND [4:0], COLOR_COMB_FCN [7:5], COLOR_DESTBLEND [12:8], ALPHA_SRCBLEND [20:16], +// ALPHA_COMB_FCN [23:21], ALPHA_DESTBLEND [28:24], SEPARATE_ALPHA_BLEND [29], ENABLE [30], DISABLE_ROP3 [31]. Without +// SEPARATE_ALPHA_BLEND the alpha channel takes the colour fields. Empty for an enabled target this path cannot express. +inline std::optional d3d12_target_blend(std::uint32_t cb_blend, std::uint32_t write_mask) { + TargetBlend t; + t.write_mask = static_cast(write_mask & 0xf); + if (!(cb_blend >> 30 & 1)) return t; + const bool separate = cb_blend >> 29 & 1; + const std::uint32_t cs = cb_blend & 31, cf = cb_blend >> 5 & 7, cd = cb_blend >> 8 & 31; + const std::uint32_t as = separate ? cb_blend >> 16 & 31 : cs, af = separate ? cb_blend >> 21 & 7 : cf, + ad = separate ? cb_blend >> 24 & 31 : cd; + t.enable = true; + t.src = d3d12_blend_factor(cs, false); + t.dst = d3d12_blend_factor(cd, false); + t.op = d3d12_blend_op(cf); + t.src_alpha = d3d12_blend_factor(as, true); + t.dst_alpha = d3d12_blend_factor(ad, true); + t.op_alpha = d3d12_blend_op(af); + if (!t.src || !t.dst || !t.op || !t.src_alpha || !t.dst_alpha || !t.op_alpha) return std::nullopt; + return t; +} + +// The targets 0..count-1 of `r` in D3D12 terms, or empty when one of them cannot be expressed: the pipeline then keeps +// the defaults (blending off, every component written) for all of its targets, as before blob A was read. +inline std::optional> d3d12_blend(const BlendRegisters& r, unsigned count) { + std::array out{}; + for (unsigned i = 0; i < count && i < out.size(); ++i) { + const auto t = d3d12_target_blend(r.blend[i], r.target_mask >> (4 * i)); + if (!t) return std::nullopt; + out[i] = *t; + } + return out; +} + using Digest = std::array; // Every 16 bytes that follow a 4-aligned 0x43 word, without duplicates. A register number can also read 0x43, so @@ -293,6 +394,7 @@ inline Plan plan(const GraphicsDesc& desc, const HashMap& map) { auto state = decode_state_b(bytes(desc.state_b), &p.reason); if (!state) return p; p.state = std::move(*state); + if (pair_state(desc.state_a) == PairState::present) p.state.blend = decode_state_a_blend(bytes(desc.state_a)); for (std::size_t k = 0; k < kStageBlocks; ++k) { const StageBlock& block = desc.stages[k]; const PairState code = pair_state(block.code), records = pair_state(block.records); diff --git a/runtime/test/native_pso_blend_test.cpp b/runtime/test/native_pso_blend_test.cpp new file mode 100644 index 0000000000..5106eec581 --- /dev/null +++ b/runtime/test/native_pso_blend_test.cpp @@ -0,0 +1,130 @@ +// The blend state of the slot 57 record's state blob A (RT-SLOT57-BLEND-STATE): the shape check, the CB_TARGET_MASK +// and CB_BLENDn_CONTROL reads at their vertex and mesh offsets, and the RDNA2 -> D3D12 mapping. Synthetic blobs only: +// no game data, no D3D12. The register words are the forms docs/fspx.md "State blob A" counts in 1.0.2.1. +#include "../src/native_pso.h" + +#include +#include +#include + +namespace np = leonida::native_pso; + +namespace { + +int g_failures = 0; + +void check(bool ok, const char* what) { + std::printf("%s %s\n", ok ? "PASS" : "FAIL", what); + if (!ok) ++g_failures; +} + +void put(std::vector& b, std::size_t at, std::uint32_t v) { std::memcpy(b.data() + at, &v, 4); } + +// A blob of `size` bytes with the tag, CB_TARGET_MASK and CB_BLEND0..7 at the measured offsets for its family. +std::vector blob_a(std::size_t size, std::uint32_t tag, std::uint32_t mask, std::uint32_t blend) { + std::vector b(size, 0); + const std::size_t shift = size > 420 ? np::kStateAMeshShift : 0; + put(b, 0, tag); + put(b, np::kStateATargetMask + shift, mask); + for (std::size_t i = 0; i < 8; ++i) put(b, np::kStateABlend + shift + 4 * i, blend + static_cast(i)); + return b; +} + +// D3D12_BLEND / D3D12_BLEND_OP values (d3d12.h). +enum : std::uint8_t { ZERO = 1, ONE = 2, SRC_ALPHA = 5, INV_SRC_ALPHA = 6, ADD = 1, SUBTRACT = 2, REV_SUBTRACT = 3, + MIN = 4, MAX = 5 }; + +bool same(const np::TargetBlend& t, bool enable, std::uint8_t s, std::uint8_t d, std::uint8_t op, std::uint8_t sa, + std::uint8_t da, std::uint8_t opa, std::uint8_t mask) { + return t.enable == enable && t.src == s && t.dst == d && t.op == op && t.src_alpha == sa && t.dst_alpha == da && + t.op_alpha == opa && t.write_mask == mask; +} + +void decode() { + // Both families and every measured size; the low bits 3 and 4 of the tag vary (0x4005d800/08/10). + for (std::size_t size : {380u, 388u, 420u, 464u, 488u, 520u}) { + const auto b = blob_a(size, 0x4005d808, 0x00ff, 0xe5010504); + const auto r = np::decode_state_a_blend(b); + check(r && r->target_mask == 0x00ff && r->blend[0] == 0xe5010504 && r->blend[7] == 0xe501050b, + size > 420 ? "a mesh-family blob reads its registers 84 bytes further on" + : "a vertex-family blob reads CB_TARGET_MASK at +0x78 and CB_BLEND0..7 at +0xd0"); + } + const char* why = nullptr; + check(!np::decode_state_a_blend(blob_a(400, 0x4005d800, 0xf, 0x80000000), &why) && why && + std::strstr(why, "size"), + "a size between the two families is not decoded"); + check(!np::decode_state_a_blend(blob_a(372, 0x4005d800, 0xf, 0x80000000)), "a shorter blob is not decoded"); + check(!np::decode_state_a_blend(blob_a(388, 0x4005d900, 0xf, 0x80000000), &why) && std::strstr(why, "tag"), + "another tag is not decoded"); +} + +void mapping() { + // The forms the library uses most (docs/fspx.md): off, alpha over, its straight-alpha variant, MAX, MIN. + check(same(*np::d3d12_target_blend(0x80000000, 0xf), false, ONE, ZERO, ADD, ONE, ZERO, ADD, 0xf), + "0x80000000 (ROP3 off only): blending off, every component written"); + check(same(*np::d3d12_target_blend(0xe5010504, 0xf), true, SRC_ALPHA, INV_SRC_ALPHA, ADD, ONE, INV_SRC_ALPHA, ADD, + 0xf), + "0xe5010504: colour SRC_ALPHA/INV_SRC_ALPHA, alpha ONE/INV_SRC_ALPHA"); + check(same(*np::d3d12_target_blend(0xe5040504, 0xf), true, SRC_ALPHA, INV_SRC_ALPHA, ADD, SRC_ALPHA, INV_SRC_ALPHA, + ADD, 0xf), + "0xe5040504: alpha SRC_ALPHA/INV_SRC_ALPHA too"); + check(same(*np::d3d12_target_blend(0xe0010161, 0xf), true, ONE, ONE, MAX, ONE, ZERO, ADD, 0xf), + "0xe0010161: colour MAX, alpha ONE/ZERO ADD"); + check(same(*np::d3d12_target_blend(0xe1410141, 0x3), true, ONE, ONE, MIN, ONE, ONE, MIN, 0x3), + "0xe1410141: MIN on both, and the target's mask nibble is its write mask"); + // COMB_FCN 1 is source minus destination, 4 destination minus source. + check(np::d3d12_target_blend(0x60000121, 0xf)->op == SUBTRACT, "COMB_FCN 1 is D3D12_BLEND_OP_SUBTRACT"); + check(np::d3d12_target_blend(0x60000181, 0xf)->op == REV_SUBTRACT, "COMB_FCN 4 is D3D12_BLEND_OP_REV_SUBTRACT"); + // Without SEPARATE_ALPHA_BLEND (bit 29) alpha takes the colour fields, a colour factor naming its alpha. + check(same(*np::d3d12_target_blend(0x40000504, 0xf), true, SRC_ALPHA, INV_SRC_ALPHA, ADD, SRC_ALPHA, INV_SRC_ALPHA, + ADD, 0xf), + "no SEPARATE_ALPHA_BLEND: alpha uses the colour factors"); + const auto colour = np::d3d12_target_blend(0x40000302, 0xf); + check(colour && colour->src == 3 && colour->dst == 4 && colour->src_alpha == SRC_ALPHA && + colour->dst_alpha == INV_SRC_ALPHA, + "SRC_COLOR/INV_SRC_COLOR stay colour factors, and alpha reads them as SRC_ALPHA/INV_SRC_ALPHA"); + // Not built here: dual-source factors (the translator declares no second pixel-shader output), BOTH_SRC_ALPHA + // (not on gfx10) and a COMB_FCN past 4. + check(!np::d3d12_target_blend(0xf1010f01, 0xf), "a dual-source (SRC1) target is not built"); + check(!np::d3d12_target_blend(0x6000010b, 0xf), "BOTH_SRC_ALPHA is not built"); + check(!np::d3d12_target_blend(0x600001a1, 0xf), "COMB_FCN 5 is not built"); + // A disabled target with a dual-source pattern left in it is still a plain disabled target. + check(np::d3d12_target_blend(0xb1010f01, 0xf) && !np::d3d12_target_blend(0xb1010f01, 0xf)->enable, + "fields of a disabled target are not read"); +} + +void pipeline() { + np::BlendRegisters r; + r.target_mask = 0x01f; // target 0: R G B A, target 1: R, target 2: none + r.blend = {0xe5010504, 0x80000000, 0x80000000, 0xf1010f01, 0xf1010f01, 0xf1010f01, 0xf1010f01, 0xf1010f01}; + const auto t = np::d3d12_blend(r, 3); + check(t && (*t)[0].enable && (*t)[0].write_mask == 0xf && !(*t)[1].enable && (*t)[1].write_mask == 0x1 && + (*t)[2].write_mask == 0, + "each target gets its own CB_BLENDn and its CB_TARGET_MASK nibble"); + check(t && !(*t)[3].enable && (*t)[3].write_mask == 0xf, "targets past the pipeline's count keep the defaults"); + check(!np::d3d12_blend(r, 4), "one target this path cannot build keeps the whole pipeline on the defaults"); + + // plan() reads blob A beside blob B. A description without stages is refused as unsupported, after both blobs. + std::vector b(np::kStateBHeader, 0); + b[0x25] = 1; + const auto a = blob_a(388, 0x4005d800, 0xf, 0xe5010504); + np::GraphicsDesc desc{}; + desc.state_a = {a.data(), a.size()}; + desc.state_b = {b.data(), b.size()}; + np::HashMap map; + auto p = np::plan(desc, map); + check(p.state.blend && p.state.blend->blend[0] == 0xe5010504, "plan() decodes blob A into FixedState::blend"); + desc.state_a = {}; + p = np::plan(desc, map); + check(!p.state.blend, "without blob A, FixedState::blend stays empty"); +} + +} // namespace + +int main() { + decode(); + mapping(); + pipeline(); + std::printf("%s\n", g_failures ? "FAILED" : "all passed"); + return g_failures ? 1 : 0; +} From 2f44380590e9471cb44b15818ad2c2c422a2af21 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:41:48 +0100 Subject: [PATCH 02/10] docs: state blob A's colour-target registers, measured over the library (RT-SLOT57-BLEND-STATE) Co-Authored-By: Claude Opus 5.5 --- docs/fspx.md | 37 +++++++++++++++++++++++++++++++++++-- runtime/src/native_pso.h | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/fspx.md b/docs/fspx.md index 6fa4a4c8db..6b4e3d8ae9 100644 --- a/docs/fspx.md +++ b/docs/fspx.md @@ -276,12 +276,45 @@ title calls device slot 57 (`+0x1c8`) with `IID_ID3D12PipelineState` (`{765a30f3 - `state blob`: `+0x000` version tag (`0x4005d800` in 15,632 records, `0x4005d808` in 9,159, `0x4005d810` in 6), `+0x004` family (`4` in 12,735 or `6` in 12,062), `+0x02c = 2`, `+0x07c` (`0xcc0010` / `0` / `0xcc0011`), `+0x0f4 = 5`, `+0x0f8 = 1`, `+0x0fc = 0x33333333` (`0.1f`), `+0x100..+0x11c` (`0x00060006` x8), `+0x150 = 0x40`, - `+0x158 = 0xffffffff`, `+0x17c = 0x10000a00`. Unidentified offsets are logged once per offset while safe default - raster/depth/blend state is used. + `+0x158 = 0xffffffff`, `+0x17c = 0x10000a00`. The colour-target registers are decoded (next paragraph); raster + and depth state are not, and keep the factory's defaults. - `stage blob`: `+0x00 = 2` (graphics stage blob), `+0x04` payload size (`196` in 208 B blobs), `+0x08 = 0x66`, `+0x0c = 0x40 | flags`, followed by records `0x41` (32 B input/output signature), `0x42` (32 B output signature) and `0x43` (20 B: tag + 16 B opaque digest). +**State blob A: the colour-target registers (RT-SLOT57-BLEND-STATE).** Blob A is the console driver's serialized +pipeline state, and its shared block holds context register values in the RDNA2 layout (Mesa's gfx10 register +headers). Measured on 1.0.2.1 over all 5,680 distinct blobs, which the 25,471 graphics records reference: + +- **Shape:** 380 + 8k bytes (k = 0..5) for vertex-shader pipelines, and 464 + 8k (k = 0..7) for mesh pipelines. The + mesh pipelines' stage block is 84 bytes longer, so every offset after `+0x28` moves by 84 there; the offsets below + are the vertex ones. Word 0 is `0x4005d800` with bits 3 and 4 free in all 25,471 records. +- **`+0x78` `CB_TARGET_MASK`:** four component bits (R, G, B, A, as in `D3D12_COLOR_WRITE_ENABLE`) per target: + `0xf` in 16,650 records, `0xff` in 2,805, `0xffffff` in 2,561. In 17,626 records the targets it enables are exactly + `0..rt_count-1` of blob B. In 1,937 it also enables target 1 of a one-target pipeline: those are the dual-source + pipelines below. The others leave a target with a format unwritten. +- **`+0x7c` `CB_COLOR_CONTROL`:** ROP3 `0xcc` (copy) in every record, so no logic op. +- **`+0xd0..+0xec` `CB_BLEND0..7_CONTROL`:** one word per target. There are 34 distinct values, every one an + in-range RDNA2 blend: factors 0 to 20 with neither `BOTH_*` value, `COMB_FCN` 0 to 4, `SEPARATE_ALPHA_BLEND` set + wherever `ENABLE` is, and `DISABLE_ROP3` set in all. By target 0: `0x80000000` (blending off) in 15,373 records, + `0xe5010504` (colour `SRC_ALPHA`/`INV_SRC_ALPHA`, alpha `ONE`/`INV_SRC_ALPHA`) in 3,456, `0xf1010f01` (colour + `ONE`/`SRC1_COLOR`, alpha `ONE`/`SRC1_ALPHA`) in 1,931, and `0xe5040504` in 1,387. 10,135 records blend on at least + one of their targets. +- **`+0x100..+0x11c`:** eight more per-target words that follow `CB_BLENDn` (`0x06000600` where blending is off). + They fit `SX_MRTn_BLEND_OPT`, whose `COMB_FCN` 6 is "blend disabled". They aren't read. + +`runtime/src/native_pso.h` (`decode_state_a_blend`, `d3d12_blend`) maps each target's `CB_BLENDn_CONTROL` and +`CB_TARGET_MASK` nibble to a `D3D12_RENDER_TARGET_BLEND_DESC`, and `fill_blend_state` in +`runtime/shims/d3d12_xs_native_pso.h` uses it for every native PSO: + +- **Before:** every target had blending off and wrote every component, so the first-run pages after #939 drew glyph + quads solid, artwork hard-edged and composited backgrounds black. +- **With the decoded state:** the pages, the legal screens and the loading slideshow draw as on console (RTX 4070 + Laptop). +- **Kept on the defaults:** the 1,937 records that blend with the dual-source `SRC1` factors, because the translator + declares no second pixel-shader output. A blob of another shape, or a target with a factor this path does not + take, keeps the whole pipeline on the defaults and is logged. + ## Translation status In wave64, VALU compares and carry outputs write both predicate words. Conditional selects and carry inputs diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index b1ad26df39..37460a21e4 100644 --- a/runtime/src/native_pso.h +++ b/runtime/src/native_pso.h @@ -15,7 +15,7 @@ // // Layout measured by LooperSalty on 1.0.2.1 and implemented by enx in 5c175db90: 0x108 bytes, the root // signature at +0x00, pairs at +0x08 and +0x18, then seven blocks {code, size, records, size} at +0x28 + k * 0x20. -// Measured by KkelebekK: pair 0 is state blob A (380-388 bytes: blend, raster, +// Measured by KkelebekK: pair 0 is state blob A (380-520 bytes: blend, decode_state_a_blend; raster and // depth, not decoded), pair 1 is state blob B (80-188 bytes: formats and input layout, decode_state_b), and each // stage's records blob carries the source container's HASH digest after its 0x43 word (docs/fspx.md, kind 7). // The block index is not trusted as the stage: the stage comes from the translator's index entry of the container. From 4e7b764dbc04fbd5343fb6c99224a8b0bec9ae9c Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:02:38 +0100 Subject: [PATCH 03/10] d3d12_xs: integer render targets keep blending off in slot 57 native 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 --- docs/fspx.md | 3 +++ runtime/shims/d3d12_xs_native_pso.h | 2 +- runtime/src/native_pso.h | 22 +++++++++++++++++++--- runtime/test/native_pso_blend_test.cpp | 13 +++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/fspx.md b/docs/fspx.md index 6b4e3d8ae9..f62c116a5e 100644 --- a/docs/fspx.md +++ b/docs/fspx.md @@ -311,6 +311,9 @@ headers). Measured on 1.0.2.1 over all 5,680 distinct blobs, which the 25,471 gr quads solid, artwork hard-edged and composited backgrounds black. - **With the decoded state:** the pages, the legal screens and the loading slideshow draw as on console (RTX 4070 Laptop). +- **Integer targets:** a target with a `UINT` or `SINT` format never blends, as the colour block bypasses blending + there and D3D12 refuses a PSO that asks for it. One library pipeline enables `MAX` on an `R8_UINT` target beside + two others. - **Kept on the defaults:** the 1,937 records that blend with the dual-source `SRC1` factors, because the translator declares no second pixel-shader output. A blob of another shape, or a target with a factor this path does not take, keeps the whole pipeline on the defaults and is logged. diff --git a/runtime/shims/d3d12_xs_native_pso.h b/runtime/shims/d3d12_xs_native_pso.h index 3ad62ffb72..6b22156885 100644 --- a/runtime/shims/d3d12_xs_native_pso.h +++ b/runtime/shims/d3d12_xs_native_pso.h @@ -45,7 +45,7 @@ inline void fill_blend_state(const np::FixedState& s, D3D12_BLEND_DESC& out) { } out.AlphaToCoverageEnable = FALSE; out.IndependentBlendEnable = FALSE; - const auto targets = s.blend ? np::d3d12_blend(*s.blend, s.rt_count) : std::nullopt; + const auto targets = s.blend ? np::d3d12_blend(*s.blend, s.rt_count, s.rtv_formats) : std::nullopt; if (!targets) { static std::atomic logged{0}; if (logged.fetch_add(1, std::memory_order_relaxed) < 4) diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index 37460a21e4..abe2c52e1e 100644 --- a/runtime/src/native_pso.h +++ b/runtime/src/native_pso.h @@ -242,12 +242,28 @@ inline std::optional d3d12_target_blend(std::uint32_t cb_blend, std return t; } +// A DXGI_FORMAT with UINT or SINT channels. The colour block does not blend those (RDNA2 bypasses blending for integer +// targets), and D3D12 refuses a PSO that enables blending on one. +inline bool dxgi_integer_format(std::uint32_t f) { + switch (f) { + case 3: case 4: case 7: case 8: case 12: case 14: case 17: case 18: case 25: case 30: case 32: case 36: + case 38: case 42: case 43: case 50: case 52: case 57: case 59: case 62: case 64: + return true; + default: + return false; + } +} + // The targets 0..count-1 of `r` in D3D12 terms, or empty when one of them cannot be expressed: the pipeline then keeps -// the defaults (blending off, every component written) for all of its targets, as before blob A was read. -inline std::optional> d3d12_blend(const BlendRegisters& r, unsigned count) { +// the defaults (blending off, every component written) for all of its targets, as before blob A was read. A target +// with an integer format (`formats`, DXGI values) keeps its write mask and has blending off, as on the console. +inline std::optional> d3d12_blend(const BlendRegisters& r, unsigned count, + const std::array& formats = {}) { std::array out{}; for (unsigned i = 0; i < count && i < out.size(); ++i) { - const auto t = d3d12_target_blend(r.blend[i], r.target_mask >> (4 * i)); + const std::uint32_t mask = r.target_mask >> (4 * i); + const auto t = dxgi_integer_format(formats[i]) ? d3d12_target_blend(r.blend[i] & ~(1u << 30), mask) + : d3d12_target_blend(r.blend[i], mask); if (!t) return std::nullopt; out[i] = *t; } diff --git a/runtime/test/native_pso_blend_test.cpp b/runtime/test/native_pso_blend_test.cpp index 5106eec581..bfe028e16e 100644 --- a/runtime/test/native_pso_blend_test.cpp +++ b/runtime/test/native_pso_blend_test.cpp @@ -104,6 +104,19 @@ void pipeline() { check(t && !(*t)[3].enable && (*t)[3].write_mask == 0xf, "targets past the pipeline's count keep the defaults"); check(!np::d3d12_blend(r, 4), "one target this path cannot build keeps the whole pipeline on the defaults"); + // A library pipeline writes R32_FLOAT, B4G4R4A4_UNORM and R8_UINT with MAX on every target (CB_BLEND 0xe1610161): + // the integer target keeps its write mask and does not blend, which D3D12 would refuse. + np::BlendRegisters mixed; + mixed.target_mask = 0xff1; + mixed.blend.fill(0xe1610161); + const auto m = np::d3d12_blend(mixed, 3, {41, 115, 62, 0, 0, 0, 0, 0}); + check(m && (*m)[0].enable && (*m)[1].enable && !(*m)[2].enable && (*m)[2].write_mask == 0xf && + (*m)[0].write_mask == 0x1, + "an integer target (R8_UINT) does not blend; the float and unorm ones do"); + check(np::dxgi_integer_format(62) && np::dxgi_integer_format(43) && !np::dxgi_integer_format(41) && + !np::dxgi_integer_format(28) && !np::dxgi_integer_format(115), + "R8_UINT and R32_SINT are integer formats; R32_FLOAT, R8G8B8A8_UNORM and B4G4R4A4_UNORM are not"); + // plan() reads blob A beside blob B. A description without stages is refused as unsupported, after both blobs. std::vector b(np::kStateBHeader, 0); b[0x25] = 1; From f5576377d1b2052277b406d2299c3a2692137173 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:13:09 +0100 Subject: [PATCH 04/10] d3d12_xs: a native PSO whose blend state the device refuses is built 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 --- runtime/shims/d3d12_xs_native_pso.h | 38 +++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/runtime/shims/d3d12_xs_native_pso.h b/runtime/shims/d3d12_xs_native_pso.h index 6b22156885..c32b24fbe8 100644 --- a/runtime/shims/d3d12_xs_native_pso.h +++ b/runtime/shims/d3d12_xs_native_pso.h @@ -34,7 +34,8 @@ namespace np = leonida::native_pso; // The blend state of blob A's colour-target registers (RT-SLOT57-BLEND-STATE). Without it, or for a target this path // cannot express (np::d3d12_blend), every target keeps blending off and writes every component, as before. -inline void fill_blend_state(const np::FixedState& s, D3D12_BLEND_DESC& out) { +// Blending off and every component written on every target: the factory's state before blob A was read. +inline void default_blend_state(D3D12_BLEND_DESC& out) { for (auto& rt : out.RenderTarget) { rt = {}; rt.SrcBlend = rt.SrcBlendAlpha = D3D12_BLEND_ONE; @@ -45,6 +46,10 @@ inline void fill_blend_state(const np::FixedState& s, D3D12_BLEND_DESC& out) { } out.AlphaToCoverageEnable = FALSE; out.IndependentBlendEnable = FALSE; +} + +inline void fill_blend_state(const np::FixedState& s, D3D12_BLEND_DESC& out) { + default_blend_state(out); const auto targets = s.blend ? np::d3d12_blend(*s.blend, s.rt_count, s.rtv_formats) : std::nullopt; if (!targets) { static std::atomic logged{0}; @@ -224,7 +229,36 @@ class NativeGraphicsPipelines { else if (stage.shader->stage == "gs") pc.GS = view; else mesh = view; // plan() admits only VS/PS/GS or MS/PS. } - return create_pipeline(pc, mesh, code); + HRESULT hr = create_pipeline(pc, mesh, code); + // The decoded blend state can ask for something this device's layer does not do: blending a B4G4R4A4 target + // that is an RGBA8 stand-in, which the layer refuses (E_NOTIMPL) because the stand-in quantizes the shader + // output before blending. Turn blending off on those targets first, then on all of them (the state before + // blob A was read), rather than leave the pipeline to the PC description path. + if (hr == E_NOTIMPL && plan.state.blend) { + bool changed = false; + for (unsigned i = 0; i < pc.NumRenderTargets; ++i) + if (pc.RTVFormats[i] == DXGI_FORMAT_B4G4R4A4_UNORM && pc.BlendState.RenderTarget[i].BlendEnable) { + pc.BlendState.RenderTarget[i].BlendEnable = FALSE; + pc.BlendState.IndependentBlendEnable = TRUE; + changed = true; + } + if (changed) hr = create_pipeline(pc, mesh, code); + const char* how = "on its B4G4R4A4 targets"; + if (hr == E_NOTIMPL) { + D3D12_BLEND_DESC decoded = pc.BlendState; + default_blend_state(pc.BlendState); + if (std::memcmp(&decoded, &pc.BlendState, sizeof(decoded)) != 0) { + hr = create_pipeline(pc, mesh, code); + how = "on every target"; + changed = true; + } + } + static std::atomic logged{0}; + if (changed && logged.fetch_add(1, std::memory_order_relaxed) < 4) + std::fprintf(stderr, "[rt] d3d12_xs: slot 57: the device refused this pipeline's blend state, so it " + "was built with blending off %s (0x%08x)\n", how, static_cast(hr)); + } + return hr; } catch (const std::bad_alloc&) { return E_OUTOFMEMORY; } catch (const std::exception&) { From 81f33de1cd307ac7cd7365724c55d27999888629 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:19:00 +0100 Subject: [PATCH 05/10] docs: the stand-in-target fallback of RT-SLOT57-BLEND-STATE Co-Authored-By: Claude Opus 5.5 --- docs/fspx.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/fspx.md b/docs/fspx.md index f62c116a5e..948f846f56 100644 --- a/docs/fspx.md +++ b/docs/fspx.md @@ -314,6 +314,10 @@ headers). Measured on 1.0.2.1 over all 5,680 distinct blobs, which the 25,471 gr - **Integer targets:** a target with a `UINT` or `SINT` format never blends, as the colour block bypasses blending there and D3D12 refuses a PSO that asks for it. One library pipeline enables `MAX` on an `R8_UINT` target beside two others. +- **Stand-in targets:** where `B4G4R4A4_UNORM` is not renderable, the Vulkan layer draws it into an RGBA8 stand-in + and refuses blending there (`E_NOTIMPL`, the output is quantized before it). The factory then turns blending off on + those targets only, and on every target if that is still refused, instead of leaving the pipeline to the PC + description path. On an RTX 4070 Laptop that is the same library pipeline (the first one the title creates). - **Kept on the defaults:** the 1,937 records that blend with the dual-source `SRC1` factors, because the translator declares no second pixel-shader output. A blob of another shape, or a target with a factor this path does not take, keeps the whole pipeline on the defaults and is logged. From d763845349ec7f36669f4b2bfa67e40576b88c8a Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:37:42 +0100 Subject: [PATCH 06/10] d3d12: dual-source blending reads SV_Target1 at Location 0, Index 1 (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 --- docs/fspx.md | 8 ++- runtime/CMakeLists.txt | 2 + runtime/shims/d3d12_vulkan_device.cpp | 17 +++++ runtime/shims/d3d12_xs_native_pso.h | 2 +- runtime/src/dual_source_fragment.h | 59 ++++++++++++++++ runtime/src/native_pso.h | 9 ++- runtime/test/dual_source_fragment_test.cpp | 82 ++++++++++++++++++++++ runtime/test/native_pso_blend_test.cpp | 13 ++-- 8 files changed, 181 insertions(+), 11 deletions(-) create mode 100644 runtime/src/dual_source_fragment.h create mode 100644 runtime/test/dual_source_fragment_test.cpp diff --git a/docs/fspx.md b/docs/fspx.md index 948f846f56..82f5cd8c0b 100644 --- a/docs/fspx.md +++ b/docs/fspx.md @@ -318,9 +318,11 @@ headers). Measured on 1.0.2.1 over all 5,680 distinct blobs, which the 25,471 gr and refuses blending there (`E_NOTIMPL`, the output is quantized before it). The factory then turns blending off on those targets only, and on every target if that is still refused, instead of leaving the pipeline to the PC description path. On an RTX 4070 Laptop that is the same library pipeline (the first one the title creates). -- **Kept on the defaults:** the 1,937 records that blend with the dual-source `SRC1` factors, because the translator - declares no second pixel-shader output. A blob of another shape, or a target with a factor this path does not - take, keeps the whole pipeline on the defaults and is logged. +- **Dual-source:** the 1,937 records that blend with `SRC1` factors get them as `D3D12_BLEND_SRC1_*`. The Vulkan + layer then moves the pixel shader's `SV_Target1` from `Location 1` to `Location 0, Index 1` + (`runtime/src/dual_source_fragment.h`, RT-DUAL-SOURCE-BLEND). +- **Kept on the defaults:** a blob of another shape, or a target with a factor or function this path does not take, + keeps the whole pipeline on the defaults and is logged. ## Translation status diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index a725881f0d..ed89f9eaf7 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1360,6 +1360,8 @@ if(BUILD_TESTING) add_test(NAME shader_library COMMAND shader_library_test) add_executable(native_pso_blend_test test/native_pso_blend_test.cpp) add_test(NAME native_pso_blend COMMAND native_pso_blend_test) + add_executable(dual_source_fragment_test test/dual_source_fragment_test.cpp) + add_test(NAME dual_source_fragment COMMAND dual_source_fragment_test) add_executable(spirv_f16_halves_test test/spirv_f16_halves_test.cpp) add_test(NAME spirv_f16_halves COMMAND spirv_f16_halves_test) # Optional: skips unless LEONIDA_SHADERS names a translated work folder (/shaders/spirv). diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 12494d201d..890bcca529 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -30,6 +30,7 @@ #include "../src/gfx10_descriptors.h" #include "../src/depth24_fragment.h" #include "../src/rgba4_fragment.h" +#include "../src/dual_source_fragment.h" #include "../src/rect_list_geometry.h" #include "../src/uav_clear.h" #include "../src/vulkan_bindless.h" @@ -7240,6 +7241,19 @@ class PipelineState final packed_color_mask|=1u< stage_infos; // Chained from stage_infos, which the recipe keeps for variants built at draw time: so they live in the recipe. @@ -7338,6 +7352,9 @@ class PipelineState final } stage.words = library->spirv(*stage.entry); if(stage.bit==VK_SHADER_STAGE_FRAGMENT_BIT && packed_color_mask && !leonida::rgba4::quantize(stage.words,packed_color_mask)) return E_INVALIDARG; + if (stage.bit == VK_SHADER_STAGE_FRAGMENT_BIT && dual_source && !leonida::dual_source::remap(stage.words)) + return stream_unsupported("a SRC1 blend factor needs the pixel shader's second colour output; this module's " + "outputs do not fit Location 0 Index 1; stage", 0); if (stage.words.empty()) return E_FAIL; const auto inner = leonida::conservative_raster::inner_coverage_requirements(stage.words); if (!inner.valid) return E_INVALIDARG; diff --git a/runtime/shims/d3d12_xs_native_pso.h b/runtime/shims/d3d12_xs_native_pso.h index c32b24fbe8..f816e40f5c 100644 --- a/runtime/shims/d3d12_xs_native_pso.h +++ b/runtime/shims/d3d12_xs_native_pso.h @@ -55,7 +55,7 @@ inline void fill_blend_state(const np::FixedState& s, D3D12_BLEND_DESC& out) { static std::atomic logged{0}; if (logged.fetch_add(1, std::memory_order_relaxed) < 4) std::fprintf(stderr, "[rt] d3d12_xs: slot 57: %s, so this pipeline keeps blending off (CB_BLEND0 %08x)\n", - s.blend ? "a target's blend state is not one this path builds (dual-source or unknown factor)" + s.blend ? "a target's blend state is not one this path builds (an unknown factor or function)" : "blob A is absent or not the measured shape", s.blend ? static_cast(s.blend->blend[0]) : 0u); return; diff --git a/runtime/src/dual_source_fragment.h b/runtime/src/dual_source_fragment.h new file mode 100644 index 0000000000..86bc662363 --- /dev/null +++ b/runtime/src/dual_source_fragment.h @@ -0,0 +1,59 @@ +#pragma once + +// Dual-source blending (RT-DUAL-SOURCE-BLEND). D3D12 takes the second blend source from SV_Target1, which the +// translated pixel shader declares as the output at Location 1. Vulkan takes it from Location 0, Index 1 +// (VK_BLEND_FACTOR_SRC1_*, dualSrcBlend). For a pipeline whose blend state uses a SRC1 factor, the fragment module's +// Location 1 output is moved to Location 0 and given Index 1. Standard library only. + +#include +#include +#include +#include + +namespace leonida::dual_source { + +// Rewrites `words` in place and returns true, or returns false and leaves them unchanged when the module is not one +// this rewrite understands: not SPIR-V, no Output variable at Location 1, an output at Location 2 or above (Vulkan has +// one dual-source attachment), or an Index decoration already present. +inline bool remap(std::vector& words) { + constexpr std::uint32_t kOpDecorate = 71, kOpVariable = 59, kOpTypePointer = 32; + constexpr std::uint32_t kLocation = 30, kIndex = 32, kBuiltIn = 11, kStorageOutput = 3; + if (words.size() < 5 || words[0] != 0x07230203) return false; + std::map location_at; // variable id -> word index of its Location literal + std::map location_of; + std::set output_pointers, outputs, builtins; + for (std::size_t at = 5; at < words.size();) { + const std::uint32_t n = words[at] >> 16, op = words[at] & 0xffffu; + if (!n || n > words.size() - at) return false; + if (op == kOpDecorate && n == 4 && words[at + 2] == kLocation) { + location_at[words[at + 1]] = at + 3; + location_of[words[at + 1]] = words[at + 3]; + } else if (op == kOpDecorate && n >= 3 && words[at + 2] == kIndex) { + return false; + } else if (op == kOpDecorate && n >= 3 && words[at + 2] == kBuiltIn) { + builtins.insert(words[at + 1]); + } else if (op == kOpTypePointer && n == 4 && words[at + 2] == kStorageOutput) { + output_pointers.insert(words[at + 1]); + } else if (op == kOpVariable && n >= 4 && words[at + 3] == kStorageOutput) { + outputs.insert(words[at + 2]); + } + at += n; + } + std::uint32_t second = 0; + for (const std::uint32_t id : outputs) { + if (builtins.count(id)) continue; + const auto loc = location_of.find(id); + if (loc == location_of.end()) continue; + if (loc->second >= 2) return false; + if (loc->second == 1) second = id; + } + if (!second) return false; + const std::size_t literal = location_at[second]; + words[literal] = 0; + // OpDecorate %second Index 1, right after its Location decoration (annotations stay together). + const std::vector index{(4u << 16) | kOpDecorate, second, kIndex, 1}; + words.insert(words.begin() + static_cast(literal + 1), index.begin(), index.end()); + return true; +} + +} // namespace leonida::dual_source diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index abe2c52e1e..5d117dba3c 100644 --- a/runtime/src/native_pso.h +++ b/runtime/src/native_pso.h @@ -196,10 +196,11 @@ struct TargetBlend { }; // An RDNA2 blend factor (V_028780_BLEND_*, the gfx10 register header) as a D3D12_BLEND, or 0 for one this path does -// not take: BOTH_(INV_)SRC_ALPHA (11, 12; not on gfx10) and the dual-source SRC1 factors (15 to 18), whose second -// pixel-shader output the translator does not declare. For an alpha factor, a colour factor names its alpha. +// not take: BOTH_(INV_)SRC_ALPHA (11, 12; not on gfx10). The dual-source SRC1 factors (15 to 18) read the pixel +// shader's SV_Target1, which the Vulkan layer moves to Location 0, Index 1 (RT-DUAL-SOURCE-BLEND). For an alpha +// factor, a colour factor names its alpha. inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { - static constexpr std::uint8_t colour[21] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 14, 15, 0, 0, 0, 0, 20, 21}; + static constexpr std::uint8_t colour[21] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 14, 15, 16, 17, 18, 19, 20, 21}; if (f >= std::size(colour)) return 0; if (alpha) { switch (f) { @@ -207,6 +208,8 @@ inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { case 3: return 6; // ONE_MINUS_SRC_COLOR -> INV_SRC_ALPHA case 8: return 7; // DST_COLOR -> DEST_ALPHA case 9: return 8; // ONE_MINUS_DST_COLOR -> INV_DEST_ALPHA + case 15: return 18; // SRC1_COLOR -> SRC1_ALPHA + case 16: return 19; // INV_SRC1_COLOR -> INV_SRC1_ALPHA default: break; } } diff --git a/runtime/test/dual_source_fragment_test.cpp b/runtime/test/dual_source_fragment_test.cpp new file mode 100644 index 0000000000..d754c7e6a2 --- /dev/null +++ b/runtime/test/dual_source_fragment_test.cpp @@ -0,0 +1,82 @@ +// Dual-source blending (RT-DUAL-SOURCE-BLEND): the fragment module rewrite that moves SV_Target1 from Location 1 to +// Location 0, Index 1. Hand-built SPIR-V word streams only (the declarations the rewrite reads); no GPU, no game data. +#include "../src/dual_source_fragment.h" + +#include +#include +#include + +namespace { + +int g_failures = 0; + +void check(bool ok, const char* what) { + std::printf("%s %s\n", ok ? "PASS" : "FAIL", what); + if (!ok) ++g_failures; +} + +using Words = std::vector; + +void op(Words& w, std::uint32_t code, std::initializer_list args) { + w.push_back(std::uint32_t(args.size() + 1) << 16 | code); + w.insert(w.end(), args); +} + +constexpr std::uint32_t kDecorate = 71, kTypeFloat = 22, kTypeVector = 23, kTypePointer = 32, kVariable = 59; +constexpr std::uint32_t kLocation = 30, kIndex = 32, kBuiltIn = 11, kFragDepth = 22, kInput = 1, kOutput = 3; + +// Output vec4 %10 at Location 0 and %11 at `second` (none if 0xff), FragDepth %12, an input %13 at Location 1, and +// extra decorations from `extra`. +Words module(std::uint32_t second, std::initializer_list> extra = {}) { + Words w{0x07230203, 0x00010000, 0, 20, 0}; + op(w, kDecorate, {10, kLocation, 0}); + if (second != 0xff) op(w, kDecorate, {11, kLocation, second}); + op(w, kDecorate, {12, kBuiltIn, kFragDepth}); + op(w, kDecorate, {13, kLocation, 1}); + for (const auto& e : extra) op(w, kDecorate, e); + op(w, kTypeFloat, {2, 32}); + op(w, kTypeVector, {3, 2, 4}); + op(w, kTypePointer, {4, kOutput, 3}); + op(w, kTypePointer, {5, kOutput, 2}); + op(w, kTypePointer, {6, kInput, 3}); + op(w, kVariable, {4, 10, kOutput}); + if (second != 0xff) op(w, kVariable, {4, 11, kOutput}); + op(w, kVariable, {5, 12, kOutput}); + op(w, kVariable, {6, 13, kInput}); + return w; +} + +// The literal of `id`'s first decoration of `kind`, or -1. +long decoration(const Words& w, std::uint32_t id, std::uint32_t kind) { + for (std::size_t at = 5; at < w.size(); at += w[at] >> 16) + if ((w[at] & 0xffff) == kDecorate && w[at + 1] == id && w[at + 2] == kind) return long(w[at + 3]); + return -1; +} + +} // namespace + +int main() { + Words w = module(1); + const std::size_t before = w.size(); + check(leonida::dual_source::remap(w), "SV_Target0 at Location 0 and SV_Target1 at Location 1: rewritten"); + check(decoration(w, 11, kLocation) == 0 && decoration(w, 11, kIndex) == 1, + "SV_Target1 is at Location 0, Index 1"); + check(decoration(w, 10, kLocation) == 0 && decoration(w, 10, kIndex) == -1, "SV_Target0 keeps Location 0, no Index"); + check(decoration(w, 13, kLocation) == 1, "an input at Location 1 is not touched"); + check(w.size() == before + 4, "one OpDecorate added, nothing else"); + + for (const auto& [m, what] : {std::pair{module(0xff), "no second colour output"}, + std::pair{module(2), "an output at Location 2"}, + std::pair{module(1, {{11, kIndex, 0}}), "an Index decoration already present"}}) { + Words copy = m; + check(!leonida::dual_source::remap(copy) && copy == m, what); + } + Words not_spirv{1, 2, 3, 4, 5, 6}; + check(!leonida::dual_source::remap(not_spirv), "not SPIR-V"); + Words truncated = module(1); + truncated[5] = (40u << 16) | kDecorate; + check(!leonida::dual_source::remap(truncated), "an instruction past the end"); + + std::printf("%s\n", g_failures ? "FAILED" : "all passed"); + return g_failures ? 1 : 0; +} diff --git a/runtime/test/native_pso_blend_test.cpp b/runtime/test/native_pso_blend_test.cpp index bfe028e16e..c7aa8e616d 100644 --- a/runtime/test/native_pso_blend_test.cpp +++ b/runtime/test/native_pso_blend_test.cpp @@ -83,9 +83,14 @@ void mapping() { check(colour && colour->src == 3 && colour->dst == 4 && colour->src_alpha == SRC_ALPHA && colour->dst_alpha == INV_SRC_ALPHA, "SRC_COLOR/INV_SRC_COLOR stay colour factors, and alpha reads them as SRC_ALPHA/INV_SRC_ALPHA"); - // Not built here: dual-source factors (the translator declares no second pixel-shader output), BOTH_SRC_ALPHA - // (not on gfx10) and a COMB_FCN past 4. - check(!np::d3d12_target_blend(0xf1010f01, 0xf), "a dual-source (SRC1) target is not built"); + // Dual-source (RT-DUAL-SOURCE-BLEND): 0xf1010f01 is colour ONE/SRC1_COLOR, alpha ONE/SRC1_ALPHA. + check(same(*np::d3d12_target_blend(0xf1010f01, 0xf), true, ONE, 16, ADD, ONE, 18, ADD, 0xf), + "0xf1010f01: D3D12_BLEND_SRC1_COLOR and D3D12_BLEND_SRC1_ALPHA"); + check(np::d3d12_target_blend(0xf2011001, 0xf)->dst == 17 && np::d3d12_target_blend(0xf2011001, 0xf)->dst_alpha == 19, + "0xf2011001: INV_SRC1_COLOR and INV_SRC1_ALPHA"); + check(np::d3d12_target_blend(0x40000f0f, 0xf)->src_alpha == 18, + "without SEPARATE_ALPHA_BLEND, alpha reads SRC1_COLOR as SRC1_ALPHA"); + // Not built here: BOTH_SRC_ALPHA (not on gfx10) and a COMB_FCN past 4. check(!np::d3d12_target_blend(0x6000010b, 0xf), "BOTH_SRC_ALPHA is not built"); check(!np::d3d12_target_blend(0x600001a1, 0xf), "COMB_FCN 5 is not built"); // A disabled target with a dual-source pattern left in it is still a plain disabled target. @@ -96,7 +101,7 @@ void mapping() { void pipeline() { np::BlendRegisters r; r.target_mask = 0x01f; // target 0: R G B A, target 1: R, target 2: none - r.blend = {0xe5010504, 0x80000000, 0x80000000, 0xf1010f01, 0xf1010f01, 0xf1010f01, 0xf1010f01, 0xf1010f01}; + r.blend = {0xe5010504, 0x80000000, 0x80000000, 0x6000010b, 0x6000010b, 0x6000010b, 0x6000010b, 0x6000010b}; const auto t = np::d3d12_blend(r, 3); check(t && (*t)[0].enable && (*t)[0].write_mask == 0xf && !(*t)[1].enable && (*t)[1].write_mask == 0x1 && (*t)[2].write_mask == 0, From 0000242d8d1c44accedbe1a0ffc0ed1bb1c65687 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:44:26 +0100 Subject: [PATCH 07/10] runtime: register dual_source_fragment_test away from the slot 57 tests (RT-DUAL-SOURCE-BLEND) Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index ed89f9eaf7..86c2901321 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1360,10 +1360,10 @@ if(BUILD_TESTING) add_test(NAME shader_library COMMAND shader_library_test) add_executable(native_pso_blend_test test/native_pso_blend_test.cpp) add_test(NAME native_pso_blend COMMAND native_pso_blend_test) - add_executable(dual_source_fragment_test test/dual_source_fragment_test.cpp) - add_test(NAME dual_source_fragment COMMAND dual_source_fragment_test) add_executable(spirv_f16_halves_test test/spirv_f16_halves_test.cpp) add_test(NAME spirv_f16_halves COMMAND spirv_f16_halves_test) + add_executable(dual_source_fragment_test test/dual_source_fragment_test.cpp) + add_test(NAME dual_source_fragment COMMAND dual_source_fragment_test) # Optional: skips unless LEONIDA_SHADERS names a translated work folder (/shaders/spirv). add_executable(shader_library_corpus_test test/shader_library_corpus_test.cpp) add_test(NAME shader_library_corpus COMMAND shader_library_corpus_test) From 1943a92f7be008f8f6873439425bc4ebcca4b578 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:07:54 +0100 Subject: [PATCH 08/10] d3d12: lazy pipelines keep the dual-source remap, and a dual-source draw 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 #1703. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 12 +++++-- runtime/shims/d3d12_vulkan_device.cpp | 36 +++++++++++++++---- runtime/test/d3d12_translated_draw_test.cpp | 39 ++++++++++++++++++--- runtime/test/shaders/ps_m15_dual.asm | 31 ++++++++++++++++ runtime/test/vk_test_device.h | 1 + 5 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 runtime/test/shaders/ps_m15_dual.asm diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 86c2901321..13ef28d57c 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3012,17 +3012,18 @@ if(TARGET Vulkan::Headers) get_filename_component(LEONIDA_SPIRV_TOOLS_DIR ${LEONIDA_SPIRV_AS} DIRECTORY) file(TO_NATIVE_PATH "${LEONIDA_SPIRV_TOOLS_DIR}" LEONIDA_SPIRV_TOOLS_NATIVE) set(LEONIDA_M15_FILES ${LEONIDA_M15_DIR}/spirv/index.json ${LEONIDA_M15_DIR}/fssm/vs_m15.fssm - ${LEONIDA_M15_DIR}/fssm/ps_m15.fssm) + ${LEONIDA_M15_DIR}/fssm/ps_m15.fssm ${LEONIDA_M15_DIR}/fssm/ps_m15_dual.fssm) add_custom_command( OUTPUT ${LEONIDA_M15_FILES} COMMAND ${CMAKE_COMMAND} -E remove_directory ${LEONIDA_M15_DIR} COMMAND ${CMAKE_COMMAND} -E env "LLVM_MC=${LEONIDA_LLVM_MC}" "LEONIDA_SPIRV_TOOLS=${LEONIDA_SPIRV_TOOLS_NATIVE}" $ -I ${CMAKE_CURRENT_SOURCE_DIR}/test/shaders/build.py - ${LEONIDA_M15_DIR} vs_m15 ps_m15 + ${LEONIDA_M15_DIR} vs_m15 ps_m15 ps_m15_dual DEPENDS ${LEONIDA_TRANSLATOR_DEPS} ${CMAKE_CURRENT_SOURCE_DIR}/test/shaders/vs_m15.asm ${CMAKE_CURRENT_SOURCE_DIR}/test/shaders/ps_m15.asm - COMMENT "Translating vs_m15 and ps_m15 for the translated draw test" + ${CMAKE_CURRENT_SOURCE_DIR}/test/shaders/ps_m15_dual.asm + COMMENT "Translating vs_m15, ps_m15 and ps_m15_dual for the translated draw test" VERBATIM) add_executable(d3d12_translated_draw_test test/d3d12_translated_draw_test.cpp shims/d3d12_xs_device.cpp shims/d3d12_xs_pso_stream.cpp ${LEONIDA_M15_FILES}) @@ -3036,6 +3037,11 @@ if(TARGET Vulkan::Headers) add_test(NAME xbox_derived_mask_restore COMMAND d3d12_translated_draw_test --derived-restore) set_tests_properties(d3d12_translated_draw PROPERTIES SKIP_RETURN_CODE 77) + # Dual-source blending (RT-DUAL-SOURCE-BLEND): with lazy pipelines (the default) and built at creation. + add_test(NAME d3d12_dual_source_draw COMMAND d3d12_translated_draw_test --dual-source) + add_test(NAME d3d12_dual_source_draw_eager COMMAND d3d12_translated_draw_test --dual-source) + set_tests_properties(d3d12_dual_source_draw d3d12_dual_source_draw_eager PROPERTIES SKIP_RETURN_CODE 77) + set_tests_properties(d3d12_dual_source_draw_eager PROPERTIES ENVIRONMENT "LEONIDA_LAZY_PIPELINES=0") # The console's rectangle and quad lists (src/rect_list_geometry.h), through the same translated vs_m15 / ps_m15. # The files are d3d12_translated_draw_test's: a second target that lists them runs the translation a second diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 890bcca529..bee46e63e0 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -7254,6 +7254,9 @@ class PipelineState final (src1(b.SrcBlend) || src1(b.DestBlend) || src1(b.SrcBlendAlpha) || src1(b.DestBlendAlpha))) dual_source = true; } + // Kept for a lazy pipeline too: build() makes the fragment words again through rewrite_fragment(). + rgba4_mask_ = packed_color_mask; + dual_source_ = dual_source; bool uses_bindless = false; // a stage declares set-0 bindings: the layout holds the device's set 0 std::vector stage_infos; // Chained from stage_infos, which the recipe keeps for variants built at draw time: so they live in the recipe. @@ -7351,10 +7354,15 @@ class PipelineState final code_stage = &stage; } stage.words = library->spirv(*stage.entry); - if(stage.bit==VK_SHADER_STAGE_FRAGMENT_BIT && packed_color_mask && !leonida::rgba4::quantize(stage.words,packed_color_mask)) return E_INVALIDARG; - if (stage.bit == VK_SHADER_STAGE_FRAGMENT_BIT && dual_source && !leonida::dual_source::remap(stage.words)) - return stream_unsupported("a SRC1 blend factor needs the pixel shader's second colour output; this module's " - "outputs do not fit Location 0 Index 1; stage", 0); + if (stage.bit == VK_SHADER_STAGE_FRAGMENT_BIT) { + std::string why; + if (!rewrite_fragment(stage.words, why)) { + if (why.rfind("has no second colour output", 0) == 0) + return stream_unsupported("a SRC1 blend factor needs the pixel shader's second colour output; " + "this module's outputs do not fit Location 0 Index 1; stage", 0); + return E_INVALIDARG; + } + } if (stage.words.empty()) return E_FAIL; const auto inner = leonida::conservative_raster::inner_coverage_requirements(stage.words); if (!inner.valid) return E_INVALIDARG; @@ -8355,6 +8363,21 @@ class PipelineState final // stages (VS or MS, GS, PS) read again, the f16 halves linked, the D24 fragment patch, then the generated stages // (the rectangle-list geometry stage from the vertex stage, the depth-only fragment stage). False with `why` when // a file went missing since. + // The rewrites a pixel shader's words get for this pipeline, the same at creation and in a lazy build(): + // quantising B4G4R4A4 targets stored as RGBA8, then moving SV_Target1 to Location 0, Index 1 for a SRC1 blend + // factor (RT-DUAL-SOURCE-BLEND). + bool rewrite_fragment(std::vector& words, std::string& why) const { + if (rgba4_mask_ && !leonida::rgba4::quantize(words, rgba4_mask_)) { + why = "could not be quantised for a B4G4R4A4 target stored as RGBA8"; + return false; + } + if (dual_source_ && !leonida::dual_source::remap(words)) { + why = "has no second colour output at Location 1 for a SRC1 blend factor"; + return false; + } + return true; + } + bool stage_words(std::vector>& words, std::string& why) const { const leonida::ShaderLibrary* library = device_.library(); if (!library) { @@ -8371,8 +8394,8 @@ class PipelineState final why = source.entry->name + ".spv is missing or not SPIR-V"; return false; } - if (source.slot == 2 && rgba4_mask_ && !leonida::rgba4::quantize(words[i], rgba4_mask_)) { - why = source.entry->name + " could not be quantised for a B4G4R4A4 target stored as RGBA8"; + if (source.slot == 2 && !rewrite_fragment(words[i], why)) { + why = source.entry->name + ": " + why; return false; } if (source.slot == 0) vs = i; @@ -8437,6 +8460,7 @@ class PipelineState final bool f16_linked_ = false; // graphics: the VS and PS words had their f16 halves linked leonida::Depth24Bias depth24_bias_{}; unsigned rgba4_mask_ = 0; // graphics: the B4G4R4A4 targets stored as RGBA8 that the pixel shader is quantised for + bool dual_source_ = false; // graphics: a SRC1 blend factor; the pixel shader's SV_Target1 is Location 0, Index 1 std::vector sources_; struct Request { VkPipeline handle{}; unsigned long long skips = 0; }; using Requests = std::map; diff --git a/runtime/test/d3d12_translated_draw_test.cpp b/runtime/test/d3d12_translated_draw_test.cpp index aa3f5ec280..5601102409 100644 --- a/runtime/test/d3d12_translated_draw_test.cpp +++ b/runtime/test/d3d12_translated_draw_test.cpp @@ -118,23 +118,31 @@ int main(int argc, char** argv) { } // The FSSM programs the index entries are keyed by; the shim's device finds the index through LEONIDA_SHADERS. const std::filesystem::path root_dir = argv[1]; + constexpr bool dual = false; // the guest build draws ps_m15 only #else int main(int argc, char** argv) { const bool derived_zero = argc == 2 && std::strcmp(argv[1], "--derived-zero") == 0; const bool derived_restore = argc == 2 && std::strcmp(argv[1], "--derived-restore") == 0; const bool native = derived_zero || derived_restore || (argc == 2 && std::strcmp(argv[1], "--native-pso") == 0); - EXPECT(argc == 1 || native); + // RT-DUAL-SOURCE-BLEND: ps_m15_dual.asm's second export (SV_Target1) as the second blend source, ONE + SRC1. + const bool dual = argc == 2 && std::strcmp(argv[1], "--dual-source") == 0; + EXPECT(argc == 1 || native || dual); vktest::Device d; const auto device_status = d.initialize(); if (device_status == vktest::DeviceStatus::unavailable) return 77; // no Vulkan GPU: ctest skips EXPECT(device_status == vktest::DeviceStatus::ready); + if (dual && !d.enabled.dualSrcBlend) return 77; // a device without dual-source blending: ctest skips // The translator-style library the build wrote, and the FSSM programs its index entries are keyed by. const std::filesystem::path root_dir = LEONIDA_TEST_M15_DIR; #endif const auto vs_program = read_file(root_dir / "fssm" / "vs_m15.fssm"); +#ifndef LEONIDA_M15_GUEST + const auto ps_program = read_file(root_dir / "fssm" / (dual ? "ps_m15_dual.fssm" : "ps_m15.fssm")); +#else const auto ps_program = read_file(root_dir / "fssm" / "ps_m15.fssm"); - EXPECT(vs_program.size() >= 4 && ps_program.size() == 0x110); +#endif + EXPECT(vs_program.size() >= 4 && ps_program.size() == (dual ? 0x114u : 0x110u)); // ps_m15_dual: one more word const float scale[4] = {0.5f, 2.0f, 1.0f, 1.0f}; // ps_m15.asm's table at offset 0x100 EXPECT(std::memcmp(ps_program.data() + 0x100, scale, sizeof(scale)) == 0); #ifndef LEONIDA_M15_GUEST @@ -224,6 +232,16 @@ int main(int argc, char** argv) { pso_desc.VS = {vs_container.data(), vs_container.size()}; pso_desc.PS = {ps_container.data(), ps_container.size()}; pso_desc.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; +#ifndef LEONIDA_M15_GUEST + if (dual) { + auto& b = pso_desc.BlendState.RenderTarget[0]; + b.BlendEnable = TRUE; + b.SrcBlend = b.SrcBlendAlpha = D3D12_BLEND_ONE; + b.DestBlend = D3D12_BLEND_SRC1_COLOR; + b.DestBlendAlpha = D3D12_BLEND_SRC1_ALPHA; + b.BlendOp = b.BlendOpAlpha = D3D12_BLEND_OP_ADD; + } +#endif pso_desc.SampleMask = 0xffffffff; pso_desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; pso_desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; @@ -424,7 +442,7 @@ int main(int argc, char** argv) { #endif EXPECT(render_frame(1, 0.0f, 0.0f) == 0); #ifndef LEONIDA_M15_GUEST - if (d.enabled.geometryShader) { + if (d.enabled.geometryShader && !dual) { // The last draw covers half the lower-left quadrant as a triangle. The Xbox rectangle primitive // fills the quadrant through the same PSO. The other three quadrants keep their original values. const auto triangles = pixels; @@ -464,8 +482,14 @@ int main(int argc, char** argv) { } const Pixel clear_color = {0.5f, 0.125f, 0.25f, 0.5f}; - const Pixel top_left = {0.125f, 1.0f, 0.75f, 1.0f}, bottom_right = {0.25f, 1.125f, 0.875f, 1.0f}; - const Pixel bottom_left = {0.75f, 0.25f, 0.75f, 1.0f}; + Pixel top_left = {0.125f, 1.0f, 0.75f, 1.0f}, bottom_right = {0.25f, 1.125f, 0.875f, 1.0f}; + Pixel bottom_left = {0.75f, 0.25f, 0.75f, 1.0f}; +#ifndef LEONIDA_M15_GUEST + if (dual) { // + SV_Target1 (0.5 everywhere) times the clear colour, the destination + for (Pixel* p : {&top_left, &bottom_right, &bottom_left}) + for (int c = 0; c < 4; ++c) (*p)[c] += 0.5f * clear_color[c]; + } +#endif #ifndef LEONIDA_M15_GUEST if (derived_zero) { const Pixel background{0.5f,0.125f,0.25f,0.5f}; @@ -514,6 +538,11 @@ int main(int argc, char** argv) { std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); return 1; } + if (dual) { + std::printf("dual-source: SV_Target1 blended as the second source (ONE + SRC1_COLOR / SRC1_ALPHA), %u of %u " + "pixels exact\n", target_extent * target_extent - edge, target_extent * target_extent); + return 0; + } std::printf("M1.5: translated VS+PS found by FSSM hash drew three draws; %u of %u pixels exact, %u edge " "pixels on the triangle's diagonal (%u its colour, %u the clear colour). User data from a descriptor " "table and root constants, V# vertex fetch from the arena, PS constants " diff --git a/runtime/test/shaders/ps_m15_dual.asm b/runtime/test/shaders/ps_m15_dual.asm new file mode 100644 index 0000000000..7d8142c2bb --- /dev/null +++ b/runtime/test/shaders/ps_m15_dual.asm @@ -0,0 +1,31 @@ +; Dual-source blending (RT-DUAL-SOURCE-BLEND): ps_m15.asm with a second colour export. mrt0 is ps_m15's colour; +; mrt1 is the constant 0.5 in every channel, the second blend source (SV_Target1). d3d12_translated_draw_test +; --dual-source blends with ONE and SRC1_COLOR / SRC1_ALPHA over the clear colour (0.5, 0.125, 0.25, 0.5), so each +; covered pixel is ps_m15's colour + (0.25, 0.0625, 0.125, 0.25), exact in a float target. The translator declares +; mrt1 at Location 1; the layer moves it to Location 0, Index 1 for the SRC1 factors. +; fssm-at: 0x100 +; The fifth word is unused by the code; it gives this program an FSSM of its own (the layer finds a PSO's shader by its +; FSSM hash, and ps_m15's would be the same otherwise). +; fssm-data: 0x3f000000 0x40000000 0x3f800000 0x3f800000 0x00000002 +s_getpc_b64 s[6:7] +s_add_u32 s6, s6, 0xfc +s_addc_u32 s7, s7, 0 +s_mov_b32 s4, s11 +s_load_dwordx4 s[8:11], s[6:7], 0x0 +v_interp_p1_f32_e32 v2, v0, attr0.x +v_interp_p2_f32_e32 v2, v1, attr0.x +v_interp_p1_f32_e32 v3, v0, attr0.y +v_interp_p2_f32_e32 v3, v1, attr0.y +v_interp_p1_f32_e32 v4, v0, attr0.z +v_interp_p2_f32_e32 v4, v1, attr0.z +v_interp_p1_f32_e32 v5, v0, attr0.w +v_interp_p2_f32_e32 v5, v1, attr0.w +s_waitcnt lgkmcnt(0) +v_fma_f32 v2, v2, s8, s4 +v_fma_f32 v3, v3, s9, s4 +v_fma_f32 v4, v4, s10, s4 +v_mul_f32_e32 v5, s11, v5 +v_mov_b32_e32 v6, 0.5 +exp mrt1 v6, v6, v6, v6 vm +exp mrt0 v2, v3, v4, v5 done vm +s_endpgm diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 079707aa90..9e009b3c05 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -478,6 +478,7 @@ class Device { enable.logicOp = features.features.logicOp; // d3d12_draw_test's logic-op draw, where available enable.multiViewport = features.features.multiViewport; // d3d12_multi_viewport_test, where available enable.geometryShader = features.features.geometryShader; // d3d12_geometry_test, where available + enable.dualSrcBlend = features.features.dualSrcBlend; // d3d12_translated_draw_test --dual-source const char* conditional_extension = VK_EXT_CONDITIONAL_RENDERING_EXTENSION_NAME; // VK_EXT_host_image_copy where the device has it, for the D3D12 layer's CPU texture copies. From 188207a9d4df1d73a954bdf4d9471a6eed829d89 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:08:24 +0100 Subject: [PATCH 09/10] run_gpu_tests: ps_m15_dual is run by d3d12_translated_draw_test, like 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 --- scripts/run_gpu_tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/run_gpu_tests.py b/scripts/run_gpu_tests.py index 8ba62104d8..a9d18b6eaa 100644 --- a/scripts/run_gpu_tests.py +++ b/scripts/run_gpu_tests.py @@ -678,7 +678,8 @@ def check_clip_half(raw_path: str, want_b: float, want_a: float) -> tuple[bool, # Fixtures run by another harness, which needs what these tools do not have (a D3D12 device, the layer's root # arguments and code base). They are still assembled and translated here, so a translator regression shows, but no # checker runs them: the test program that owns them judges them. -OTHER_HARNESS = {"ps_m15": "d3d12_translated_draw_test", "vs_m15": "d3d12_translated_draw_test", +OTHER_HARNESS = {"ps_m15": "d3d12_translated_draw_test", "ps_m15_dual": "d3d12_translated_draw_test", + "vs_m15": "d3d12_translated_draw_test", "ps_m2": "d3d12_m2_textured_test", "vs_m2": "d3d12_m2_textured_test", "ps_m2_uoff": "d3d12_m2_textured_test", "ps_m2_literal": "d3d12_m2_textured_test", "ps_m2_array": "d3d12_m2_formats_test", "ps_m3": "d3d12_m3_frame_test", "vs_m3": "d3d12_m3_frame_test", From 668d65b4b9cb74586176edb97a9379231153e4ff Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:00:57 +0100 Subject: [PATCH 10/10] d3d12_xs: slot 57 retries only a pipeline that blends a B4G4R4A4 stand-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 #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 --- docs/fspx.md | 9 ++- runtime/CMakeLists.txt | 5 ++ runtime/shims/d3d12_xs_native_pso.h | 38 ++++------ runtime/shims/d3d12_xs_native_pso_fallback.h | 25 +++++++ runtime/test/native_pso_fallback_test.cpp | 73 ++++++++++++++++++++ 5 files changed, 123 insertions(+), 27 deletions(-) create mode 100644 runtime/shims/d3d12_xs_native_pso_fallback.h create mode 100644 runtime/test/native_pso_fallback_test.cpp diff --git a/docs/fspx.md b/docs/fspx.md index 948f846f56..53521ad41a 100644 --- a/docs/fspx.md +++ b/docs/fspx.md @@ -315,9 +315,12 @@ headers). Measured on 1.0.2.1 over all 5,680 distinct blobs, which the 25,471 gr there and D3D12 refuses a PSO that asks for it. One library pipeline enables `MAX` on an `R8_UINT` target beside two others. - **Stand-in targets:** where `B4G4R4A4_UNORM` is not renderable, the Vulkan layer draws it into an RGBA8 stand-in - and refuses blending there (`E_NOTIMPL`, the output is quantized before it). The factory then turns blending off on - those targets only, and on every target if that is still refused, instead of leaving the pipeline to the PC - description path. On an RTX 4070 Laptop that is the same library pipeline (the first one the title creates). + and refuses blending there (`E_NOTIMPL`, the output is quantized before it). The factory then retries the pipeline + with blending off on those targets only (`shims/d3d12_xs_native_pso_fallback.h`), instead of leaving it to the PC + description path. On an RTX 4070 Laptop that is the same library pipeline (the first one the title creates). Only a + pipeline that blends a `B4G4R4A4` target is retried: `E_NOTIMPL` also stands for refusals unrelated to blending (a + rect list without its geometry stage, mesh limits, `FullyCovered`, a dual-source output that does not fit + `Location 0, Index 1`), and those keep the blend the title asked for. - **Kept on the defaults:** the 1,937 records that blend with the dual-source `SRC1` factors, because the translator declares no second pixel-shader output. A blob of another shape, or a target with a factor this path does not take, keeps the whole pipeline on the defaults and is logged. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index a725881f0d..aa83493380 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1360,6 +1360,11 @@ if(BUILD_TESTING) add_test(NAME shader_library COMMAND shader_library_test) add_executable(native_pso_blend_test test/native_pso_blend_test.cpp) add_test(NAME native_pso_blend COMMAND native_pso_blend_test) + if(WIN32) + # The slot 57 factory retries only a pipeline that blends a B4G4R4A4 stand-in target (d3d12.h types). + add_executable(native_pso_fallback_test test/native_pso_fallback_test.cpp) + add_test(NAME native_pso_fallback COMMAND native_pso_fallback_test) + endif() add_executable(spirv_f16_halves_test test/spirv_f16_halves_test.cpp) add_test(NAME spirv_f16_halves COMMAND spirv_f16_halves_test) # Optional: skips unless LEONIDA_SHADERS names a translated work folder (/shaders/spirv). diff --git a/runtime/shims/d3d12_xs_native_pso.h b/runtime/shims/d3d12_xs_native_pso.h index c32b24fbe8..bb03959ce8 100644 --- a/runtime/shims/d3d12_xs_native_pso.h +++ b/runtime/shims/d3d12_xs_native_pso.h @@ -13,6 +13,7 @@ // The state and stage resolution are in native_pso.h. All stage bytecode handed to D3D12 // comes from the user's carved DXBC containers and is translated by the normal Vulkan PSO path. #include "../src/native_pso.h" +#include "d3d12_xs_native_pso_fallback.h" #include "d3d12_xs_pso_stream.h" #include "../src/shader_library.h" @@ -231,32 +232,21 @@ class NativeGraphicsPipelines { } HRESULT hr = create_pipeline(pc, mesh, code); // The decoded blend state can ask for something this device's layer does not do: blending a B4G4R4A4 target - // that is an RGBA8 stand-in, which the layer refuses (E_NOTIMPL) because the stand-in quantizes the shader - // output before blending. Turn blending off on those targets first, then on all of them (the state before - // blob A was read), rather than leave the pipeline to the PC description path. + // that is an RGBA8 stand-in (without_stand_in_blend). Only a pipeline that blends such a target is retried. + // E_NOTIMPL also stands for refusals that have nothing to do with blending (stream_unsupported: a rect list + // without its geometry stage, mesh limits, FullyCovered; a dual-source output that does not fit Location 0 + // Index 1): those are returned as they are, never with a blend the title did not ask for. if (hr == E_NOTIMPL && plan.state.blend) { - bool changed = false; - for (unsigned i = 0; i < pc.NumRenderTargets; ++i) - if (pc.RTVFormats[i] == DXGI_FORMAT_B4G4R4A4_UNORM && pc.BlendState.RenderTarget[i].BlendEnable) { - pc.BlendState.RenderTarget[i].BlendEnable = FALSE; - pc.BlendState.IndependentBlendEnable = TRUE; - changed = true; - } - if (changed) hr = create_pipeline(pc, mesh, code); - const char* how = "on its B4G4R4A4 targets"; - if (hr == E_NOTIMPL) { - D3D12_BLEND_DESC decoded = pc.BlendState; - default_blend_state(pc.BlendState); - if (std::memcmp(&decoded, &pc.BlendState, sizeof(decoded)) != 0) { - hr = create_pipeline(pc, mesh, code); - how = "on every target"; - changed = true; - } + D3D12_GRAPHICS_PIPELINE_STATE_DESC retry = pc; + const HRESULT again = without_stand_in_blend(retry) ? create_pipeline(retry, mesh, code) : hr; + if (SUCCEEDED(again)) { + hr = again; + static std::atomic logged{0}; + if (logged.fetch_add(1, std::memory_order_relaxed) < 4) + std::fprintf(stderr, "[rt] d3d12_xs: slot 57: the device refused blending on this pipeline's " + "B4G4R4A4 stand-in targets, so it was built with blending off on those " + "targets\n"); } - static std::atomic logged{0}; - if (changed && logged.fetch_add(1, std::memory_order_relaxed) < 4) - std::fprintf(stderr, "[rt] d3d12_xs: slot 57: the device refused this pipeline's blend state, so it " - "was built with blending off %s (0x%08x)\n", how, static_cast(hr)); } return hr; } catch (const std::bad_alloc&) { diff --git a/runtime/shims/d3d12_xs_native_pso_fallback.h b/runtime/shims/d3d12_xs_native_pso_fallback.h new file mode 100644 index 0000000000..276c9a32fb --- /dev/null +++ b/runtime/shims/d3d12_xs_native_pso_fallback.h @@ -0,0 +1,25 @@ +#pragma once + +// The one blend fallback of the slot 57 native graphics factory (RT-SLOT57-BLEND-STATE). Where B4G4R4A4_UNORM is not +// renderable, the Vulkan layer draws it into an RGBA8 stand-in and refuses blending there (E_NOTIMPL): the stand-in +// quantizes the shader output before the blend. A pipeline that blends such a target is retried with blending off on +// those targets alone. E_NOTIMPL also stands for refusals that have nothing to do with blending, so a pipeline with no +// blended B4G4R4A4 target is never retried. +#include + +namespace rt::d3d12_xs::native_pipeline_detail { + +// Turns blending off on the B4G4R4A4 targets that blend, and only there. False, with `desc` unchanged, when there is +// no such target: the refusal was not the stand-in's. +inline bool without_stand_in_blend(D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc) { + bool changed = false; + for (unsigned i = 0; i < desc.NumRenderTargets && i < 8; ++i) + if (desc.RTVFormats[i] == DXGI_FORMAT_B4G4R4A4_UNORM && desc.BlendState.RenderTarget[i].BlendEnable) { + desc.BlendState.RenderTarget[i].BlendEnable = FALSE; + changed = true; + } + if (changed) desc.BlendState.IndependentBlendEnable = TRUE; + return changed; +} + +} // namespace rt::d3d12_xs::native_pipeline_detail diff --git a/runtime/test/native_pso_fallback_test.cpp b/runtime/test/native_pso_fallback_test.cpp new file mode 100644 index 0000000000..6f3a770ebf --- /dev/null +++ b/runtime/test/native_pso_fallback_test.cpp @@ -0,0 +1,73 @@ +// The slot 57 factory's blend fallback (shims/d3d12_xs_native_pso_fallback.h): only a pipeline that blends a +// B4G4R4A4 target is retried, with blending off on those targets alone. Any other E_NOTIMPL refusal keeps the blend. +#include "../shims/d3d12_xs_native_pso_fallback.h" + +#include +#include + +namespace { + +int failures = 0; + +void expect(bool ok, const char* what) { + if (!ok) { + std::fprintf(stderr, "FAIL: %s\n", what); + ++failures; + } +} + +D3D12_GRAPHICS_PIPELINE_STATE_DESC two_targets(DXGI_FORMAT first, DXGI_FORMAT second) { + D3D12_GRAPHICS_PIPELINE_STATE_DESC desc{}; + desc.NumRenderTargets = 2; + desc.RTVFormats[0] = first; + desc.RTVFormats[1] = second; + for (auto& rt : desc.BlendState.RenderTarget) { + rt.BlendEnable = TRUE; + rt.SrcBlend = D3D12_BLEND_SRC_ALPHA; + rt.DestBlend = D3D12_BLEND_INV_SRC_ALPHA; + rt.BlendOp = D3D12_BLEND_OP_ADD; + rt.SrcBlendAlpha = D3D12_BLEND_ONE; + rt.DestBlendAlpha = D3D12_BLEND_ZERO; + rt.BlendOpAlpha = D3D12_BLEND_OP_ADD; + rt.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + } + desc.BlendState.IndependentBlendEnable = FALSE; + return desc; +} + +} // namespace + +int main() { + using rt::d3d12_xs::native_pipeline_detail::without_stand_in_blend; + + { + auto desc = two_targets(DXGI_FORMAT_B4G4R4A4_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM); + expect(without_stand_in_blend(desc), "a blended B4G4R4A4 target is the stand-in's refusal"); + expect(!desc.BlendState.RenderTarget[0].BlendEnable, "blending goes off on the B4G4R4A4 target"); + expect(desc.BlendState.RenderTarget[1].BlendEnable, "the other target keeps its blend"); + expect(desc.BlendState.IndependentBlendEnable, "the targets now differ, so blending is per target"); + } + { + // No B4G4R4A4 target: an E_NOTIMPL here is some other refusal (a rect list, mesh limits, a dual-source output + // that does not fit Location 0 Index 1), and the pipeline must not be rebuilt without its blend. + const auto before = two_targets(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R16G16B16A16_FLOAT); + auto desc = before; + expect(!without_stand_in_blend(desc), "no B4G4R4A4 target, no retry"); + expect(std::memcmp(&desc, &before, sizeof(desc)) == 0, "the description is left as it was"); + } + { + auto before = two_targets(DXGI_FORMAT_B4G4R4A4_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM); + before.BlendState.RenderTarget[0].BlendEnable = FALSE; + auto desc = before; + expect(!without_stand_in_blend(desc), "a B4G4R4A4 target that does not blend, no retry"); + expect(std::memcmp(&desc, &before, sizeof(desc)) == 0, "the description is left as it was"); + } + { + auto desc = two_targets(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B4G4R4A4_UNORM); + desc.NumRenderTargets = 1; // the B4G4R4A4 format past the target count is not a target + expect(!without_stand_in_blend(desc), "a format past NumRenderTargets is not looked at"); + } + if (failures) return 1; + std::printf("native_pso_fallback: ok\n"); + return 0; +}