Merge pull request #1671 from OreoFR/rt-slot57-blend-state

d3d12_xs: blend state for slot 57 native pipelines from state blob A (RT-SLOT57-BLEND-STATE)
This commit is contained in:
aslan2142
2026-11-25 14:28:49 +01:00
committed by GitHub
7 changed files with 478 additions and 5 deletions

View File

@ -276,12 +276,55 @@ 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).
- **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 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.
## Translation status
In wave64, VALU compares and carry outputs write both predicate words. Conditional selects and carry inputs

View File

@ -1390,6 +1390,13 @@ 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)
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 (<work>/shaders/spirv).

View File

@ -13,11 +13,14 @@
// 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"
#include <d3d12.h>
#include <atomic>
#include <climits>
#include <cstring>
#include <cstdio>
#include <exception>
#include <filesystem>
@ -30,8 +33,51 @@ 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.
// 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;
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;
}
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<int> 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<unsigned>(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<D3D12_BLEND>(t.src);
rt.DestBlend = static_cast<D3D12_BLEND>(t.dst);
rt.BlendOp = static_cast<D3D12_BLEND_OP>(t.op);
rt.SrcBlendAlpha = static_cast<D3D12_BLEND>(t.src_alpha);
rt.DestBlendAlpha = static_cast<D3D12_BLEND>(t.dst_alpha);
rt.BlendOpAlpha = static_cast<D3D12_BLEND_OP>(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;
@ -184,7 +230,25 @@ public:
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 (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) {
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<int> 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");
}
}
return hr;
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
} catch (const std::exception&) {

View File

@ -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 <d3d12.h>
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

View File

@ -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.
@ -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<std::uint32_t, 8> 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<BlendRegisters> blend; // from blob A; empty when blob A is absent or not the measured shape
std::array<std::uint8_t, 8> rtv_formats{}; // +0x10, DXGI_FORMAT values
std::uint32_t sample_mask = 0; // +0x1c
std::uint8_t dsv_format = 0; // +0x24
@ -153,6 +161,115 @@ inline std::optional<FixedState> decode_state_b(std::span<const std::uint8_t> 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<BlendRegisters> decode_state_a_blend(std::span<const std::uint8_t> 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<TargetBlend> d3d12_target_blend(std::uint32_t cb_blend, std::uint32_t write_mask) {
TargetBlend t;
t.write_mask = static_cast<std::uint8_t>(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;
}
// 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. A target
// with an integer format (`formats`, DXGI values) keeps its write mask and has blending off, as on the console.
inline std::optional<std::array<TargetBlend, 8>> d3d12_blend(const BlendRegisters& r, unsigned count,
const std::array<std::uint8_t, 8>& formats = {}) {
std::array<TargetBlend, 8> out{};
for (unsigned i = 0; i < count && i < out.size(); ++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;
}
return out;
}
using Digest = std::array<std::uint8_t, 16>;
// Every 16 bytes that follow a 4-aligned 0x43 word, without duplicates. A register number can also read 0x43, so
@ -293,6 +410,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);

View File

@ -0,0 +1,143 @@
// 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 <cstdio>
#include <cstring>
#include <vector>
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<std::uint8_t>& 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<std::uint8_t> blob_a(std::size_t size, std::uint32_t tag, std::uint32_t mask, std::uint32_t blend) {
std::vector<std::uint8_t> 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<std::uint32_t>(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");
// 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<std::uint8_t> 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;
}

View File

@ -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 <cstdio>
#include <cstring>
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;
}