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
49 changes: 47 additions & 2 deletions docs/fspx.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,57 @@ 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.
- **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

In wave64, VALU compares and carry outputs write both predicate words. Conditional selects and carry inputs
Expand Down
21 changes: 18 additions & 3 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,17 @@ 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)
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 (<work>/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)
Expand Down Expand Up @@ -3008,17 +3017,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}"
$<TARGET_FILE:Python3::Interpreter> -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})
Expand All @@ -3032,6 +3042,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
Expand Down
47 changes: 44 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -7240,6 +7241,22 @@ class PipelineState final
packed_color_mask|=1u<<i;
}
}
// Dual-source blending (RT-DUAL-SOURCE-BLEND): a SRC1 factor reads the pixel shader's SV_Target1, which the
// translator declares at Location 1 and Vulkan takes from Location 0, Index 1 (dual_source_fragment.h).
bool dual_source = false;
for (UINT i = 0; i < desc.NumRenderTargets; ++i) {
const auto& b = desc.BlendState.RenderTarget[desc.BlendState.IndependentBlendEnable ? i : 0];
const auto src1 = [](D3D12_BLEND f) {
return f == D3D12_BLEND_SRC1_COLOR || f == D3D12_BLEND_INV_SRC1_COLOR || f == D3D12_BLEND_SRC1_ALPHA ||
f == D3D12_BLEND_INV_SRC1_ALPHA;
};
if (b.BlendEnable &&
(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<VkPipelineShaderStageCreateInfo> stage_infos;
// Chained from stage_infos, which the recipe keeps for variants built at draw time: so they live in the recipe.
Expand Down Expand Up @@ -7337,7 +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) {
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;
Expand Down Expand Up @@ -8338,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<std::uint32_t>& 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<std::vector<std::uint32_t>>& words, std::string& why) const {
const leonida::ShaderLibrary* library = device_.library();
if (!library) {
Expand All @@ -8354,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;
Expand Down Expand Up @@ -8420,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<StageSource> sources_;
struct Request { VkPipeline handle{}; unsigned long long skips = 0; };
using Requests = std::map<std::uint32_t, Request>;
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 (an unknown factor or function)"
: "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
Loading