Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions docs/fspx.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,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).
Expand Down
68 changes: 66 additions & 2 deletions runtime/shims/d3d12_xs_native_pso.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;
Expand Down Expand Up @@ -184,7 +230,25 @@ 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 (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&) {
Expand Down
25 changes: 25 additions & 0 deletions runtime/shims/d3d12_xs_native_pso_fallback.h
Original file line number Diff line number Diff line change
@@ -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
Loading