diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-25 14:28:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-25 14:28:49 +0100 |
| commit | 5e95fa2f46d5df1a8397d5ee871add369031d8b6 (patch) | |
| tree | f1bf9b346a528eb3501ce324b6a388d2f396246d /runtime/src | |
| parent | 384abe5b06d08e349f4b4e4394c68f036da47dc7 (diff) | |
| parent | 668d65b4b9cb74586176edb97a9379231153e4ff (diff) | |
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)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/native_pso.h | 120 |
1 files changed, 119 insertions, 1 deletions
diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index 8982c701a..abe2c52e1 100644 --- a/runtime/src/native_pso.h +++ b/runtime/src/native_pso.h | |||
| @@ -15,7 +15,7 @@ | |||
| 15 | // | 15 | // |
| 16 | // Layout measured by LooperSalty on 1.0.2.1 and implemented by enx in 5c175db90: 0x108 bytes, the root | 16 | // Layout measured by LooperSalty on 1.0.2.1 and implemented by enx in 5c175db90: 0x108 bytes, the root |
| 17 | // signature at +0x00, pairs at +0x08 and +0x18, then seven blocks {code, size, records, size} at +0x28 + k * 0x20. | 17 | // signature at +0x00, pairs at +0x08 and +0x18, then seven blocks {code, size, records, size} at +0x28 + k * 0x20. |
| 18 | // Measured by KkelebekK: pair 0 is state blob A (380-388 bytes: blend, raster, | 18 | // Measured by KkelebekK: pair 0 is state blob A (380-520 bytes: blend, decode_state_a_blend; raster and |
| 19 | // depth, not decoded), pair 1 is state blob B (80-188 bytes: formats and input layout, decode_state_b), and each | 19 | // depth, not decoded), pair 1 is state blob B (80-188 bytes: formats and input layout, decode_state_b), and each |
| 20 | // stage's records blob carries the source container's HASH digest after its 0x43 word (docs/fspx.md, kind 7). | 20 | // stage's records blob carries the source container's HASH digest after its 0x43 word (docs/fspx.md, kind 7). |
| 21 | // The block index is not trusted as the stage: the stage comes from the translator's index entry of the container. | 21 | // 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 { | |||
| 93 | std::uint32_t step_rate = 0; | 93 | std::uint32_t step_rate = 0; |
| 94 | }; | 94 | }; |
| 95 | 95 | ||
| 96 | // The colour-target registers of state blob A (RT-SLOT57-BLEND-STATE, docs/fspx.md "State blob A"): CB_TARGET_MASK | ||
| 97 | // and CB_BLEND0..7_CONTROL as the console's driver serialized them, in the RDNA2 register layout. | ||
| 98 | struct BlendRegisters { | ||
| 99 | std::uint32_t target_mask = 0; // CB_TARGET_MASK: four component bits (R, G, B, A) per target | ||
| 100 | std::array<std::uint32_t, 8> blend{}; // CB_BLENDn_CONTROL | ||
| 101 | }; | ||
| 102 | |||
| 96 | // State blob B. The u32 field widths after each name are a hypothesis that fits the | 103 | // State blob B. The u32 field widths after each name are a hypothesis that fits the |
| 97 | // measured sizes: 0x50 bytes with no element, and 188 bytes with POSITION, COLOR and TEXCOORD. | 104 | // measured sizes: 0x50 bytes with no element, and 188 bytes with POSITION, COLOR and TEXCOORD. |
| 98 | struct FixedState { | 105 | struct FixedState { |
| 106 | std::optional<BlendRegisters> blend; // from blob A; empty when blob A is absent or not the measured shape | ||
| 99 | std::array<std::uint8_t, 8> rtv_formats{}; // +0x10, DXGI_FORMAT values | 107 | std::array<std::uint8_t, 8> rtv_formats{}; // +0x10, DXGI_FORMAT values |
| 100 | std::uint32_t sample_mask = 0; // +0x1c | 108 | std::uint32_t sample_mask = 0; // +0x1c |
| 101 | std::uint8_t dsv_format = 0; // +0x24 | 109 | 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, | |||
| 153 | return s; | 161 | return s; |
| 154 | } | 162 | } |
| 155 | 163 | ||
| 164 | // State blob A, measured on 1.0.2.1 over all 5,680 distinct blobs (25,471 graphics records, docs/fspx.md "State blob | ||
| 165 | // A"): 380 + 8k bytes (k = 0..5) for vertex-shader pipelines and 464 + 8k (k = 0..7) for mesh pipelines, whose stage | ||
| 166 | // block is 84 bytes longer, so every offset after +0x28 moves by 84 there. Word 0 is 0x4005d800 with bits 3 and 4 | ||
| 167 | // free. CB_TARGET_MASK is at +0x78 and CB_BLEND0..7_CONTROL at +0xd0..+0xec (vertex-pipeline offsets). Any other | ||
| 168 | // shape is not decoded: the caller keeps its defaults. | ||
| 169 | inline constexpr std::uint32_t kStateATag = 0x4005d800; | ||
| 170 | inline constexpr std::size_t kStateATargetMask = 0x78, kStateABlend = 0xd0, kStateAMeshShift = 84; | ||
| 171 | |||
| 172 | inline std::optional<BlendRegisters> decode_state_a_blend(std::span<const std::uint8_t> a, const char** why = nullptr) { | ||
| 173 | const auto fail = [&](const char* reason) { | ||
| 174 | if (why) *why = reason; | ||
| 175 | return std::nullopt; | ||
| 176 | }; | ||
| 177 | const std::size_t n = a.size(); | ||
| 178 | std::size_t shift = 0; | ||
| 179 | if (n >= 380 && n <= 420 && (n - 380) % 8 == 0) shift = 0; | ||
| 180 | else if (n >= 464 && n <= 520 && (n - 464) % 8 == 0) shift = kStateAMeshShift; | ||
| 181 | else return fail("blob A size not one of the measured shapes"); | ||
| 182 | if ((read_u32(a, 0) & ~0x18u) != kStateATag) return fail("blob A tag not 0x4005d800"); | ||
| 183 | BlendRegisters r; | ||
| 184 | r.target_mask = read_u32(a, kStateATargetMask + shift); | ||
| 185 | for (std::size_t i = 0; i < r.blend.size(); ++i) r.blend[i] = read_u32(a, kStateABlend + shift + 4 * i); | ||
| 186 | return r; | ||
| 187 | } | ||
| 188 | |||
| 189 | // One target's blend state in D3D12 terms, with D3D12_BLEND and D3D12_BLEND_OP values (d3d12.h) so this header | ||
| 190 | // stays standard-library only. | ||
| 191 | struct TargetBlend { | ||
| 192 | bool enable = false; | ||
| 193 | std::uint8_t src = 2, dst = 1, op = 1; // D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD | ||
| 194 | std::uint8_t src_alpha = 2, dst_alpha = 1, op_alpha = 1; | ||
| 195 | std::uint8_t write_mask = 0xf; // D3D12_COLOR_WRITE_ENABLE_*: R 1, G 2, B 4, A 8 | ||
| 196 | }; | ||
| 197 | |||
| 198 | // An RDNA2 blend factor (V_028780_BLEND_*, the gfx10 register header) as a D3D12_BLEND, or 0 for one this path does | ||
| 199 | // not take: BOTH_(INV_)SRC_ALPHA (11, 12; not on gfx10) and the dual-source SRC1 factors (15 to 18), whose second | ||
| 200 | // pixel-shader output the translator does not declare. For an alpha factor, a colour factor names its alpha. | ||
| 201 | inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { | ||
| 202 | 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}; | ||
| 203 | if (f >= std::size(colour)) return 0; | ||
| 204 | if (alpha) { | ||
| 205 | switch (f) { | ||
| 206 | case 2: return 5; // SRC_COLOR -> SRC_ALPHA | ||
| 207 | case 3: return 6; // ONE_MINUS_SRC_COLOR -> INV_SRC_ALPHA | ||
| 208 | case 8: return 7; // DST_COLOR -> DEST_ALPHA | ||
| 209 | case 9: return 8; // ONE_MINUS_DST_COLOR -> INV_DEST_ALPHA | ||
| 210 | default: break; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | return colour[f]; | ||
| 214 | } | ||
| 215 | |||
| 216 | // An RDNA2 COMB_FCN as a D3D12_BLEND_OP, or 0: DST_PLUS_SRC ADD, SRC_MINUS_DST SUBTRACT, MIN, MAX, DST_MINUS_SRC | ||
| 217 | // REV_SUBTRACT. | ||
| 218 | inline std::uint8_t d3d12_blend_op(std::uint32_t c) { | ||
| 219 | static constexpr std::uint8_t op[5] = {1, 2, 4, 5, 3}; | ||
| 220 | return c < std::size(op) ? op[c] : 0; | ||
| 221 | } | ||
| 222 | |||
| 223 | // CB_BLENDn_CONTROL: COLOR_SRCBLEND [4:0], COLOR_COMB_FCN [7:5], COLOR_DESTBLEND [12:8], ALPHA_SRCBLEND [20:16], | ||
| 224 | // ALPHA_COMB_FCN [23:21], ALPHA_DESTBLEND [28:24], SEPARATE_ALPHA_BLEND [29], ENABLE [30], DISABLE_ROP3 [31]. Without | ||
| 225 | // SEPARATE_ALPHA_BLEND the alpha channel takes the colour fields. Empty for an enabled target this path cannot express. | ||
| 226 | inline std::optional<TargetBlend> d3d12_target_blend(std::uint32_t cb_blend, std::uint32_t write_mask) { | ||
| 227 | TargetBlend t; | ||
| 228 | t.write_mask = static_cast<std::uint8_t>(write_mask & 0xf); | ||
| 229 | if (!(cb_blend >> 30 & 1)) return t; | ||
| 230 | const bool separate = cb_blend >> 29 & 1; | ||
| 231 | const std::uint32_t cs = cb_blend & 31, cf = cb_blend >> 5 & 7, cd = cb_blend >> 8 & 31; | ||
| 232 | const std::uint32_t as = separate ? cb_blend >> 16 & 31 : cs, af = separate ? cb_blend >> 21 & 7 : cf, | ||
| 233 | ad = separate ? cb_blend >> 24 & 31 : cd; | ||
| 234 | t.enable = true; | ||
| 235 | t.src = d3d12_blend_factor(cs, false); | ||
| 236 | t.dst = d3d12_blend_factor(cd, false); | ||
| 237 | t.op = d3d12_blend_op(cf); | ||
| 238 | t.src_alpha = d3d12_blend_factor(as, true); | ||
| 239 | t.dst_alpha = d3d12_blend_factor(ad, true); | ||
| 240 | t.op_alpha = d3d12_blend_op(af); | ||
| 241 | if (!t.src || !t.dst || !t.op || !t.src_alpha || !t.dst_alpha || !t.op_alpha) return std::nullopt; | ||
| 242 | return t; | ||
| 243 | } | ||
| 244 | |||
| 245 | // A DXGI_FORMAT with UINT or SINT channels. The colour block does not blend those (RDNA2 bypasses blending for integer | ||
| 246 | // targets), and D3D12 refuses a PSO that enables blending on one. | ||
| 247 | inline bool dxgi_integer_format(std::uint32_t f) { | ||
| 248 | switch (f) { | ||
| 249 | case 3: case 4: case 7: case 8: case 12: case 14: case 17: case 18: case 25: case 30: case 32: case 36: | ||
| 250 | case 38: case 42: case 43: case 50: case 52: case 57: case 59: case 62: case 64: | ||
| 251 | return true; | ||
| 252 | default: | ||
| 253 | return false; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | // The targets 0..count-1 of `r` in D3D12 terms, or empty when one of them cannot be expressed: the pipeline then keeps | ||
| 258 | // the defaults (blending off, every component written) for all of its targets, as before blob A was read. A target | ||
| 259 | // with an integer format (`formats`, DXGI values) keeps its write mask and has blending off, as on the console. | ||
| 260 | inline std::optional<std::array<TargetBlend, 8>> d3d12_blend(const BlendRegisters& r, unsigned count, | ||
| 261 | const std::array<std::uint8_t, 8>& formats = {}) { | ||
| 262 | std::array<TargetBlend, 8> out{}; | ||
| 263 | for (unsigned i = 0; i < count && i < out.size(); ++i) { | ||
| 264 | const std::uint32_t mask = r.target_mask >> (4 * i); | ||
| 265 | const auto t = dxgi_integer_format(formats[i]) ? d3d12_target_blend(r.blend[i] & ~(1u << 30), mask) | ||
| 266 | : d3d12_target_blend(r.blend[i], mask); | ||
| 267 | if (!t) return std::nullopt; | ||
| 268 | out[i] = *t; | ||
| 269 | } | ||
| 270 | return out; | ||
| 271 | } | ||
| 272 | |||
| 156 | using Digest = std::array<std::uint8_t, 16>; | 273 | using Digest = std::array<std::uint8_t, 16>; |
| 157 | 274 | ||
| 158 | // Every 16 bytes that follow a 4-aligned 0x43 word, without duplicates. A register number can also read 0x43, so | 275 | // 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) { | |||
| 293 | auto state = decode_state_b(bytes(desc.state_b), &p.reason); | 410 | auto state = decode_state_b(bytes(desc.state_b), &p.reason); |
| 294 | if (!state) return p; | 411 | if (!state) return p; |
| 295 | p.state = std::move(*state); | 412 | p.state = std::move(*state); |
| 413 | if (pair_state(desc.state_a) == PairState::present) p.state.blend = decode_state_a_blend(bytes(desc.state_a)); | ||
| 296 | for (std::size_t k = 0; k < kStageBlocks; ++k) { | 414 | for (std::size_t k = 0; k < kStageBlocks; ++k) { |
| 297 | const StageBlock& block = desc.stages[k]; | 415 | const StageBlock& block = desc.stages[k]; |
| 298 | const PairState code = pair_state(block.code), records = pair_state(block.records); | 416 | const PairState code = pair_state(block.code), records = pair_state(block.records); |