diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-25 14:55:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-25 14:55:52 +0100 |
| commit | 71a769d7d85208f86c6e2e50263aca7df1becce9 (patch) | |
| tree | 2cc29d405f040972cdb3c738880fca3850aa6e60 /runtime/src | |
| parent | 77baf906275d7b695664adbf0b33f3c2b306a1dc (diff) | |
| parent | 7a769396d99d93474e980e2d1426854a6c2b3e6c (diff) | |
Merge pull request #1691 from OreoFR/rt-slot57-depth-raster
d3d12_xs: depth, stencil and raster state for slot 57 native pipelines from state blob A (RT-SLOT57-DEPTH-RASTER)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/native_pso.h | 126 |
1 files changed, 122 insertions, 4 deletions
diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index 5d117dba3..22427d1f5 100644 --- a/runtime/src/native_pso.h +++ b/runtime/src/native_pso.h | |||
| @@ -100,10 +100,21 @@ struct BlendRegisters { | |||
| 100 | std::array<std::uint32_t, 8> blend{}; // CB_BLENDn_CONTROL | 100 | std::array<std::uint32_t, 8> blend{}; // CB_BLENDn_CONTROL |
| 101 | }; | 101 | }; |
| 102 | 102 | ||
| 103 | // The depth, stencil and raster registers of state blob A (RT-SLOT57-DEPTH-RASTER, docs/fspx.md "State blob A"). | ||
| 104 | struct DepthRasterRegisters { | ||
| 105 | std::uint32_t clip_cntl = 0; // PA_CL_CLIP_CNTL | ||
| 106 | std::uint32_t mode_cntl = 0; // PA_SU_SC_MODE_CNTL | ||
| 107 | std::uint32_t depth_control = 0; // DB_DEPTH_CONTROL | ||
| 108 | std::uint32_t stencil_control = 0; // DB_STENCIL_CONTROL | ||
| 109 | std::uint32_t stencil_ref_mask = 0; // DB_STENCILREFMASK | ||
| 110 | std::uint32_t stencil_ref_mask_bf = 0; // DB_STENCILREFMASK_BF | ||
| 111 | }; | ||
| 112 | |||
| 103 | // State blob B. The u32 field widths after each name are a hypothesis that fits the | 113 | // State blob B. The u32 field widths after each name are a hypothesis that fits the |
| 104 | // measured sizes: 0x50 bytes with no element, and 188 bytes with POSITION, COLOR and TEXCOORD. | 114 | // measured sizes: 0x50 bytes with no element, and 188 bytes with POSITION, COLOR and TEXCOORD. |
| 105 | struct FixedState { | 115 | struct FixedState { |
| 106 | std::optional<BlendRegisters> blend; // from blob A; empty when blob A is absent or not the measured shape | 116 | std::optional<BlendRegisters> blend; // from blob A; empty when blob A is absent or not the measured shape |
| 117 | std::optional<DepthRasterRegisters> depth_raster; // from blob A, likewise | ||
| 107 | std::array<std::uint8_t, 8> rtv_formats{}; // +0x10, DXGI_FORMAT values | 118 | std::array<std::uint8_t, 8> rtv_formats{}; // +0x10, DXGI_FORMAT values |
| 108 | std::uint32_t sample_mask = 0; // +0x1c | 119 | std::uint32_t sample_mask = 0; // +0x1c |
| 109 | std::uint8_t dsv_format = 0; // +0x24 | 120 | std::uint8_t dsv_format = 0; // +0x24 |
| @@ -168,8 +179,11 @@ inline std::optional<FixedState> decode_state_b(std::span<const std::uint8_t> b, | |||
| 168 | // shape is not decoded: the caller keeps its defaults. | 179 | // shape is not decoded: the caller keeps its defaults. |
| 169 | inline constexpr std::uint32_t kStateATag = 0x4005d800; | 180 | inline constexpr std::uint32_t kStateATag = 0x4005d800; |
| 170 | inline constexpr std::size_t kStateATargetMask = 0x78, kStateABlend = 0xd0, kStateAMeshShift = 84; | 181 | inline constexpr std::size_t kStateATargetMask = 0x78, kStateABlend = 0xd0, kStateAMeshShift = 84; |
| 182 | inline constexpr std::size_t kStateAClip = 0x9c, kStateAMode = 0xa0, kStateADepth = 0xb8, kStateAStencil = 0xbc, | ||
| 183 | kStateARefMask = 0xc0, kStateARefMaskBf = 0xc4; | ||
| 171 | 184 | ||
| 172 | inline std::optional<BlendRegisters> decode_state_a_blend(std::span<const std::uint8_t> a, const char** why = nullptr) { | 185 | // The offset shift of a measured blob A shape (0 for vertex pipelines, 84 for mesh ones), or empty. |
| 186 | inline std::optional<std::size_t> state_a_shift(std::span<const std::uint8_t> a, const char** why) { | ||
| 173 | const auto fail = [&](const char* reason) { | 187 | const auto fail = [&](const char* reason) { |
| 174 | if (why) *why = reason; | 188 | if (why) *why = reason; |
| 175 | return std::nullopt; | 189 | return std::nullopt; |
| @@ -180,9 +194,31 @@ inline std::optional<BlendRegisters> decode_state_a_blend(std::span<const std::u | |||
| 180 | else if (n >= 464 && n <= 520 && (n - 464) % 8 == 0) shift = kStateAMeshShift; | 194 | 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"); | 195 | 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"); | 196 | if ((read_u32(a, 0) & ~0x18u) != kStateATag) return fail("blob A tag not 0x4005d800"); |
| 197 | return shift; | ||
| 198 | } | ||
| 199 | |||
| 200 | inline std::optional<BlendRegisters> decode_state_a_blend(std::span<const std::uint8_t> a, const char** why = nullptr) { | ||
| 201 | const auto shift = state_a_shift(a, why); | ||
| 202 | if (!shift) return std::nullopt; | ||
| 183 | BlendRegisters r; | 203 | BlendRegisters r; |
| 184 | r.target_mask = read_u32(a, kStateATargetMask + shift); | 204 | 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); | 205 | for (std::size_t i = 0; i < r.blend.size(); ++i) r.blend[i] = read_u32(a, kStateABlend + *shift + 4 * i); |
| 206 | return r; | ||
| 207 | } | ||
| 208 | |||
| 209 | // PA_CL_CLIP_CNTL at +0x9c, PA_SU_SC_MODE_CNTL at +0xa0, DB_DEPTH_CONTROL at +0xb8, DB_STENCIL_CONTROL at +0xbc and | ||
| 210 | // DB_STENCILREFMASK(_BF) at +0xc0/+0xc4 (vertex-pipeline offsets), measured as for the blend registers. | ||
| 211 | inline std::optional<DepthRasterRegisters> decode_state_a_depth_raster(std::span<const std::uint8_t> a, | ||
| 212 | const char** why = nullptr) { | ||
| 213 | const auto shift = state_a_shift(a, why); | ||
| 214 | if (!shift) return std::nullopt; | ||
| 215 | DepthRasterRegisters r; | ||
| 216 | r.clip_cntl = read_u32(a, kStateAClip + *shift); | ||
| 217 | r.mode_cntl = read_u32(a, kStateAMode + *shift); | ||
| 218 | r.depth_control = read_u32(a, kStateADepth + *shift); | ||
| 219 | r.stencil_control = read_u32(a, kStateAStencil + *shift); | ||
| 220 | r.stencil_ref_mask = read_u32(a, kStateARefMask + *shift); | ||
| 221 | r.stencil_ref_mask_bf = read_u32(a, kStateARefMaskBf + *shift); | ||
| 186 | return r; | 222 | return r; |
| 187 | } | 223 | } |
| 188 | 224 | ||
| @@ -273,6 +309,85 @@ inline std::optional<std::array<TargetBlend, 8>> d3d12_blend(const BlendRegister | |||
| 273 | return out; | 309 | return out; |
| 274 | } | 310 | } |
| 275 | 311 | ||
| 312 | // The depth, stencil and raster state in D3D12 terms, with D3D12_COMPARISON_FUNC, D3D12_STENCIL_OP and D3D12_CULL_MODE | ||
| 313 | // values (d3d12.h). The defaults are the factory's state before blob A was read. | ||
| 314 | struct StencilFace { | ||
| 315 | std::uint8_t fail = 1, depth_fail = 1, pass = 1; // D3D12_STENCIL_OP_KEEP | ||
| 316 | std::uint8_t func = 8; // D3D12_COMPARISON_FUNC_ALWAYS | ||
| 317 | }; | ||
| 318 | struct DepthRaster { | ||
| 319 | bool depth_enable = true; // the factory also turns it off without a DSV | ||
| 320 | bool depth_write = true; // D3D12_DEPTH_WRITE_MASK_ALL | ||
| 321 | std::uint8_t depth_func = 8; // D3D12_COMPARISON_FUNC_ALWAYS | ||
| 322 | bool stencil_enable = false; | ||
| 323 | std::uint8_t read_mask = 0xff, write_mask = 0xff; | ||
| 324 | StencilFace front, back; | ||
| 325 | std::uint8_t cull = 1; // D3D12_CULL_MODE_NONE | ||
| 326 | bool front_ccw = false; | ||
| 327 | bool depth_clip = true; | ||
| 328 | }; | ||
| 329 | |||
| 330 | // An RDNA2 stencil op (V_02842C_STENCIL_*) as a D3D12_STENCIL_OP, or 0. The adding and subtracting ops take | ||
| 331 | // STENCILOPVAL as their operand, so they are INCR/DECR only with an operand of 1 (all 6,213 stencil records measured); | ||
| 332 | // REPLACE_TEST writes the reference value as D3D12's REPLACE does. ONES, REPLACE_OP and the logic ops have no D3D12 op. | ||
| 333 | inline std::uint8_t d3d12_stencil_op(std::uint32_t op, std::uint32_t operand) { | ||
| 334 | switch (op) { | ||
| 335 | case 0: return 1; // KEEP | ||
| 336 | case 1: return 2; // ZERO | ||
| 337 | case 3: return 3; // REPLACE_TEST -> REPLACE | ||
| 338 | case 5: return operand == 1 ? 4 : 0; // ADD_CLAMP -> INCR_SAT | ||
| 339 | case 6: return operand == 1 ? 5 : 0; // SUB_CLAMP -> DECR_SAT | ||
| 340 | case 7: return 6; // INVERT | ||
| 341 | case 8: return operand == 1 ? 7 : 0; // ADD_WRAP -> INCR | ||
| 342 | case 9: return operand == 1 ? 8 : 0; // SUB_WRAP -> DECR | ||
| 343 | default: return 0; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | // Empty for a state D3D12 cannot express here: clip space other than D3D's, only one of the near/far z clips off, | ||
| 348 | // a polygon mode, culling both faces, depth bounds, the colour-write-on-depth flags, a stencil op without a D3D12 op, | ||
| 349 | // or front and back stencil masks that differ (D3D12_DEPTH_STENCIL_DESC has one pair). | ||
| 350 | // PA_CL_CLIP_CNTL: DX_CLIP_SPACE_DEF [19], ZCLIP_NEAR_DISABLE [26], ZCLIP_FAR_DISABLE [27]. | ||
| 351 | // PA_SU_SC_MODE_CNTL: CULL_FRONT [0], CULL_BACK [1], FACE [2] (1: clockwise is front), POLY_MODE [4:3]. | ||
| 352 | // DB_DEPTH_CONTROL: STENCIL_ENABLE [0], Z_ENABLE [1], Z_WRITE_ENABLE [2], DEPTH_BOUNDS_ENABLE [3], ZFUNC [6:4], | ||
| 353 | // BACKFACE_ENABLE [7], STENCILFUNC [10:8], STENCILFUNC_BF [22:20], bits 30 and 31 the colour-write flags. | ||
| 354 | // DB_STENCIL_CONTROL: STENCILFAIL [3:0], STENCILZPASS [7:4], STENCILZFAIL [11:8], and the _BF ones at +12. | ||
| 355 | // DB_STENCILREFMASK: STENCILMASK [15:8], STENCILWRITEMASK [23:16], STENCILOPVAL [31:24]. | ||
| 356 | // RDNA2 compare functions run NEVER..ALWAYS as 0..7, D3D12's as 1..8. | ||
| 357 | inline std::optional<DepthRaster> d3d12_depth_raster(const DepthRasterRegisters& r) { | ||
| 358 | DepthRaster s; | ||
| 359 | const std::uint32_t clip = r.clip_cntl, mode = r.mode_cntl, d = r.depth_control; | ||
| 360 | if (!(clip >> 19 & 1) || (clip >> 26 & 1) != (clip >> 27 & 1)) return std::nullopt; | ||
| 361 | s.depth_clip = !(clip >> 26 & 1); | ||
| 362 | if (mode >> 3 & 3 || (mode & 3) == 3) return std::nullopt; | ||
| 363 | s.cull = (mode & 1) ? 2 : (mode & 2) ? 3 : 1; // FRONT, BACK, NONE | ||
| 364 | s.front_ccw = !(mode >> 2 & 1); | ||
| 365 | if (d >> 3 & 1 || d >> 30) return std::nullopt; | ||
| 366 | s.depth_enable = d >> 1 & 1; | ||
| 367 | s.depth_write = d >> 2 & 1; | ||
| 368 | s.depth_func = static_cast<std::uint8_t>((d >> 4 & 7) + 1); | ||
| 369 | s.stencil_enable = d & 1; | ||
| 370 | if (!s.stencil_enable) return s; | ||
| 371 | if ((r.stencil_ref_mask & 0x00ffff00) != (r.stencil_ref_mask_bf & 0x00ffff00)) return std::nullopt; | ||
| 372 | s.read_mask = static_cast<std::uint8_t>(r.stencil_ref_mask >> 8); | ||
| 373 | s.write_mask = static_cast<std::uint8_t>(r.stencil_ref_mask >> 16); | ||
| 374 | const auto face = [&](std::uint32_t ops, std::uint32_t func, std::uint32_t operand) -> std::optional<StencilFace> { | ||
| 375 | StencilFace f; | ||
| 376 | f.fail = d3d12_stencil_op(ops & 15, operand); | ||
| 377 | f.pass = d3d12_stencil_op(ops >> 4 & 15, operand); | ||
| 378 | f.depth_fail = d3d12_stencil_op(ops >> 8 & 15, operand); | ||
| 379 | f.func = static_cast<std::uint8_t>(func + 1); | ||
| 380 | if (!f.fail || !f.pass || !f.depth_fail) return std::nullopt; | ||
| 381 | return f; | ||
| 382 | }; | ||
| 383 | const auto front = face(r.stencil_control, d >> 8 & 7, r.stencil_ref_mask >> 24); | ||
| 384 | const auto back = (d >> 7 & 1) ? face(r.stencil_control >> 12, d >> 20 & 7, r.stencil_ref_mask_bf >> 24) : front; | ||
| 385 | if (!front || !back) return std::nullopt; | ||
| 386 | s.front = *front; | ||
| 387 | s.back = *back; | ||
| 388 | return s; | ||
| 389 | } | ||
| 390 | |||
| 276 | using Digest = std::array<std::uint8_t, 16>; | 391 | using Digest = std::array<std::uint8_t, 16>; |
| 277 | 392 | ||
| 278 | // Every 16 bytes that follow a 4-aligned 0x43 word, without duplicates. A register number can also read 0x43, so | 393 | // Every 16 bytes that follow a 4-aligned 0x43 word, without duplicates. A register number can also read 0x43, so |
| @@ -413,7 +528,10 @@ inline Plan plan(const GraphicsDesc& desc, const HashMap& map) { | |||
| 413 | auto state = decode_state_b(bytes(desc.state_b), &p.reason); | 528 | auto state = decode_state_b(bytes(desc.state_b), &p.reason); |
| 414 | if (!state) return p; | 529 | if (!state) return p; |
| 415 | p.state = std::move(*state); | 530 | p.state = std::move(*state); |
| 416 | if (pair_state(desc.state_a) == PairState::present) p.state.blend = decode_state_a_blend(bytes(desc.state_a)); | 531 | if (pair_state(desc.state_a) == PairState::present) { |
| 532 | p.state.blend = decode_state_a_blend(bytes(desc.state_a)); | ||
| 533 | p.state.depth_raster = decode_state_a_depth_raster(bytes(desc.state_a)); | ||
| 534 | } | ||
| 417 | for (std::size_t k = 0; k < kStageBlocks; ++k) { | 535 | for (std::size_t k = 0; k < kStageBlocks; ++k) { |
| 418 | const StageBlock& block = desc.stages[k]; | 536 | const StageBlock& block = desc.stages[k]; |
| 419 | const PairState code = pair_state(block.code), records = pair_state(block.records); | 537 | const PairState code = pair_state(block.code), records = pair_state(block.records); |