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
82 changes: 80 additions & 2 deletions docs/fspx.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,90 @@ 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, depth, stencil and raster registers are decoded
(next paragraphs); depth bias is not.
- `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.

**State blob A: the depth, stencil and raster registers (RT-SLOT57-DEPTH-RASTER).** The same shared block, at the
same vertex-pipeline offsets (+84 in mesh pipelines). The register layouts and their checks against blob B are below,
measured over all 25,471 graphics records:

- **`+0x9c` `PA_CL_CLIP_CNTL`:** `DX_CLIP_SPACE_DEF` set in every record. Both z clips are off in 960 records and on
in the rest, so the near and far planes always agree.
- **`+0xa0` `PA_SU_SC_MODE_CNTL`:** no culling in 13,534 records, back faces in 11,873 and front faces in 62.
`FACE` (clockwise is front) is clear in all but 2, `POLY_MODE` is 0 in all, and the polygon-offset enables are set
in all.
- **`+0xb8` `DB_DEPTH_CONTROL`:**
- with no DSV in blob B, `Z_ENABLE` is clear in 7,040 of 7,079 records;
- `STENCIL_ENABLE` is set only with formats that have stencil;
- `DEPTH_BOUNDS_ENABLE` and bits 30-31 are clear in all;
- `BACKFACE_ENABLE` is set in all;
- the test is off (`0x007007f0`) in 9,062 records, otherwise mostly `GEQUAL` (a reversed depth range), with `LESS`,
`LEQUAL`, `EQUAL` and `NOTEQUAL` in some.
- **`+0xbc` `DB_STENCIL_CONTROL`:** nonzero in 5,512 of the 6,213 records with stencil on, and in 2 without. The ops
used are `KEEP`, `REPLACE_TEST`, `ADD_WRAP`, `ZERO` and `INVERT`.
- **`+0xc0`/`+0xc4` `DB_STENCILREFMASK(_BF)`:** the two faces have the same read and write masks in every stencil
record, and `STENCILOPVAL` is 1 in all. `STENCILTESTVAL` (bits 7:0, the reference) is 0 in all 6,213 stencil records, front and
back, so the reference always comes from the title (`OMSetStencilRef`) and the PSO does not need it. 245 records use a back-face function of their own.

`runtime/src/native_pso.h` (`decode_state_a_depth_raster`, `d3d12_depth_raster`) maps them to
`D3D12_DEPTH_STENCIL_DESC` and the cull, front-face and depth-clip fields of `D3D12_RASTERIZER_DESC`, and
`fill_depth_raster_state` in `runtime/shims/d3d12_xs_native_pso.h` uses them for every native PSO:

- **Before:** depth `ALWAYS` with writes wherever there was a DSV, no stencil, no culling and depth clip on.
- **With the decoded state:** every one of the 25,471 records maps. A pipeline without a DSV keeps depth and stencil
off.
- **Not decoded:** depth bias (`PA_SU_POLY_OFFSET_*`). Its D3D12 units depend on the depth format, so it stays 0.
The polygon-offset enables in `PA_SU_SC_MODE_CNTL` are set in all 25,471 records. The values look like the floats at
`+0xac`/`+0xb0` (scale and offset), which are both 0 in 23,987 records and nonzero in about 1,480.
- **Front face:** `FACE` is the only field whose effect can't be seen before a world frame. It maps to
`FrontCounterClockwise = !FACE`, as RADV and PAL program it.

## Translation status

In wave64, VALU compares and carry outputs write both predicate words. Conditional selects and carry inputs
Expand Down
9 changes: 9 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,15 @@ 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(native_pso_depth_test test/native_pso_depth_test.cpp)
add_test(NAME native_pso_depth COMMAND native_pso_depth_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
114 changes: 106 additions & 8 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,16 +33,93 @@ 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;
}
}

// The depth, stencil and raster state of blob A's registers (RT-SLOT57-DEPTH-RASTER). Without them, or for a state
// this path cannot express (np::d3d12_depth_raster), the defaults below stay, as before: depth ALWAYS with writes
// where there is a DSV, no stencil, no culling, depth clip on. Depth bias is not decoded and stays 0.
inline void fill_depth_raster_state(const np::FixedState& s, D3D12_GRAPHICS_PIPELINE_STATE_DESC& pc) {
const auto dr = s.depth_raster ? np::d3d12_depth_raster(*s.depth_raster) : std::nullopt;
if (!dr) {
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 the default depth and raster state "
"(DB_DEPTH_CONTROL %08x, PA_SU_SC_MODE_CNTL %08x)\n",
s.depth_raster ? "blob A's depth, stencil or raster state is not one this path builds"
: "blob A is absent or not the measured shape",
s.depth_raster ? static_cast<unsigned>(s.depth_raster->depth_control) : 0u,
s.depth_raster ? static_cast<unsigned>(s.depth_raster->mode_cntl) : 0u);
}
const np::DepthRaster state = dr ? *dr : np::DepthRaster{};
auto& r = pc.RasterizerState;
r.FillMode = D3D12_FILL_MODE_SOLID;
r.CullMode = static_cast<D3D12_CULL_MODE>(state.cull);
r.FrontCounterClockwise = state.front_ccw;
r.DepthClipEnable = state.depth_clip;
auto& ds = pc.DepthStencilState;
ds.DepthEnable = state.depth_enable;
ds.DepthWriteMask = state.depth_write ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO;
ds.DepthFunc = static_cast<D3D12_COMPARISON_FUNC>(state.depth_func);
ds.StencilEnable = state.stencil_enable;
ds.StencilReadMask = state.read_mask;
ds.StencilWriteMask = state.write_mask;
const auto face = [](const np::StencilFace& f) {
return D3D12_DEPTH_STENCILOP_DESC{static_cast<D3D12_STENCIL_OP>(f.fail), static_cast<D3D12_STENCIL_OP>(f.depth_fail),
static_cast<D3D12_STENCIL_OP>(f.pass), static_cast<D3D12_COMPARISON_FUNC>(f.func)};
};
ds.FrontFace = face(state.front);
ds.BackFace = face(state.back);
// Without a DSV the console has nothing to test or write (27 records enable Z with none).
if (pc.DSVFormat == DXGI_FORMAT_UNKNOWN) ds.DepthEnable = ds.StencilEnable = FALSE;
}

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;
pc.RasterizerState.DepthClipEnable = TRUE;
pc.DSVFormat = static_cast<DXGI_FORMAT>(s.dsv_format);
pc.DepthStencilState.DepthEnable = pc.DSVFormat != DXGI_FORMAT_UNKNOWN;
pc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
pc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
fill_depth_raster_state(s, pc);
pc.PrimitiveTopologyType = s.topology_type ? static_cast<D3D12_PRIMITIVE_TOPOLOGY_TYPE>(s.topology_type)
: D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
pc.NumRenderTargets = s.rt_count;
Expand Down Expand Up @@ -184,7 +264,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