diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 09:40:21 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 09:40:21 -0500 |
| commit | 769634dd61e4ff352ebf0ce0b2c69fd4596bce9b (patch) | |
| tree | 983850dcb303c7ec5d74afae3ae5673813e2e3a7 /runtime/src | |
| parent | 002c720f299abe4f1e4c8a4d7db2b710b774f88f (diff) | |
| parent | 446574e7610921947112069f3b3d36f40a20230b (diff) | |
Merge main into fix/host-import-prefix
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/arena_blocks.h | 35 | ||||
| -rw-r--r-- | runtime/src/arena_size.h | 31 | ||||
| -rw-r--r-- | runtime/src/dual_source_fragment.h | 59 | ||||
| -rw-r--r-- | runtime/src/native_pso.h | 241 | ||||
| -rw-r--r-- | runtime/src/title_window_plan.h | 17 |
5 files changed, 366 insertions, 17 deletions
diff --git a/runtime/src/arena_blocks.h b/runtime/src/arena_blocks.h index f6b379e2f..c7ac093d8 100644 --- a/runtime/src/arena_blocks.h +++ b/runtime/src/arena_blocks.h | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #include <algorithm> | 14 | #include <algorithm> |
| 15 | #include <cstddef> | 15 | #include <cstddef> |
| 16 | #include <cstdint> | 16 | #include <cstdint> |
| 17 | #include <limits> | ||
| 17 | #include <map> | 18 | #include <map> |
| 18 | #include <utility> | 19 | #include <utility> |
| 19 | #include <vector> | 20 | #include <vector> |
| @@ -66,11 +67,18 @@ public: | |||
| 66 | const std::uint64_t block = next->first; | 67 | const std::uint64_t block = next->first; |
| 67 | const std::uint64_t block_size = next->second; | 68 | const std::uint64_t block_size = next->second; |
| 68 | const std::uint64_t end = block + block_size; | 69 | const std::uint64_t end = block + block_size; |
| 69 | std::uint64_t start = align_up(block, alignment); | 70 | std::uint64_t start = 0; |
| 70 | while (start < end && bytes <= end - start) { | 71 | if (align_up(block, alignment, start)) { |
| 71 | const std::uint64_t keep_out_end = keep_out(start, start + bytes); | 72 | while (start < end && bytes <= end - start) { |
| 72 | if (!keep_out_end) break; | 73 | const std::uint64_t keep_out_end = keep_out(start, start + bytes); |
| 73 | start = align_up(keep_out_end, alignment); | 74 | if (!keep_out_end) break; |
| 75 | if (keep_out_end <= start || !align_up(keep_out_end, alignment, start)) { | ||
| 76 | start = end; | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } else { | ||
| 81 | start = end; | ||
| 74 | } | 82 | } |
| 75 | if (cuts_ != cuts) break; // the map moved under the walk: search the new one | 83 | if (cuts_ != cuts) break; // the map moved under the walk: search the new one |
| 76 | if (start < end && bytes <= end - start) { | 84 | if (start < end && bytes <= end - start) { |
| @@ -189,7 +197,10 @@ public: | |||
| 189 | 197 | ||
| 190 | // Whether the map's free bytes and the device's live bytes add up to the arena: a byte released twice, or a | 198 | // Whether the map's free bytes and the device's live bytes add up to the arena: a byte released twice, or a |
| 191 | // released range that was never allocated, breaks this. O(1), so the device checks it on every call. | 199 | // released range that was never allocated, breaks this. O(1), so the device checks it on every call. |
| 192 | bool consistent(std::uint64_t live_bytes) const { return free_bytes_ + live_bytes == size_ - reserved_; } | 200 | bool consistent(std::uint64_t live_bytes) const { |
| 201 | const std::uint64_t total = size_ > reserved_ ? size_ - reserved_ : 0; | ||
| 202 | return live_bytes <= total && free_bytes_ == total - live_bytes; | ||
| 203 | } | ||
| 193 | 204 | ||
| 194 | // Faults reported so far, and the last one this call reported. | 205 | // Faults reported so far, and the last one this call reported. |
| 195 | std::uint64_t faults() const { return faults_; } | 206 | std::uint64_t faults() const { return faults_; } |
| @@ -202,8 +213,16 @@ public: | |||
| 202 | static constexpr int kAttempts = 8; | 213 | static constexpr int kAttempts = 8; |
| 203 | 214 | ||
| 204 | private: | 215 | private: |
| 205 | static std::uint64_t align_up(std::uint64_t value, std::uint64_t alignment) { | 216 | static bool align_up(std::uint64_t value, std::uint64_t alignment, std::uint64_t& out) { |
| 206 | return (value + alignment - 1) / alignment * alignment; | 217 | const std::uint64_t rem = value % alignment; |
| 218 | if (!rem) { | ||
| 219 | out = value; | ||
| 220 | return true; | ||
| 221 | } | ||
| 222 | const std::uint64_t add = alignment - rem; | ||
| 223 | if (value > std::numeric_limits<std::uint64_t>::max() - add) return false; | ||
| 224 | out = value + add; | ||
| 225 | return true; | ||
| 207 | } | 226 | } |
| 208 | 227 | ||
| 209 | Fault note_fault(Fault fault) { | 228 | Fault note_fault(Fault fault) { |
diff --git a/runtime/src/arena_size.h b/runtime/src/arena_size.h index 196b03e3e..66eed0dc2 100644 --- a/runtime/src/arena_size.h +++ b/runtime/src/arena_size.h | |||
| @@ -9,7 +9,7 @@ | |||
| 9 | // The decision is a pure function of the variable's text and the two limits, so it is tested without a GPU | 9 | // The decision is a pure function of the variable's text and the two limits, so it is tested without a GPU |
| 10 | // (arena_size_test). It depends only on the Vulkan headers. | 10 | // (arena_size_test). It depends only on the Vulkan headers. |
| 11 | 11 | ||
| 12 | #include <vulkan/vulkan_core.h> | 12 | #include "vulkan_memory.h" |
| 13 | 13 | ||
| 14 | #include <algorithm> | 14 | #include <algorithm> |
| 15 | #include <cstdint> | 15 | #include <cstdint> |
| @@ -21,6 +21,9 @@ constexpr VkDeviceSize kArenaMiB = VkDeviceSize(1) << 20; | |||
| 21 | constexpr VkDeviceSize kArenaDefaultMiB = 256; | 21 | constexpr VkDeviceSize kArenaDefaultMiB = 256; |
| 22 | constexpr VkDeviceSize kArenaMinMiB = 16; | 22 | constexpr VkDeviceSize kArenaMinMiB = 16; |
| 23 | constexpr VkDeviceSize kArenaMaxMiB = 4095; | 23 | constexpr VkDeviceSize kArenaMaxMiB = 4095; |
| 24 | constexpr VkMemoryPropertyFlags kArenaRequiredMemory = | ||
| 25 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 26 | constexpr VkMemoryPropertyFlags kArenaPreferredMemory = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; | ||
| 24 | 27 | ||
| 25 | // The device's limits; 0 is "not known", which caps nothing. | 28 | // The device's limits; 0 is "not known", which caps nothing. |
| 26 | struct ArenaLimits { | 29 | struct ArenaLimits { |
| @@ -43,6 +46,7 @@ enum ArenaCap : unsigned { | |||
| 43 | arena_cap_none = 0, | 46 | arena_cap_none = 0, |
| 44 | arena_cap_buffer = 1, // maxBufferSize | 47 | arena_cap_buffer = 1, // maxBufferSize |
| 45 | arena_cap_allocation = 2, // maxMemoryAllocationSize | 48 | arena_cap_allocation = 2, // maxMemoryAllocationSize |
| 49 | arena_cap_bar = 4, // the largest size whose buffer may use DEVICE_LOCAL | HOST_VISIBLE memory | ||
| 46 | }; | 50 | }; |
| 47 | 51 | ||
| 48 | struct ArenaSize { | 52 | struct ArenaSize { |
| @@ -82,6 +86,7 @@ inline ArenaSize decide_arena_size(const char* env, const ArenaLimits& limits, V | |||
| 82 | 86 | ||
| 83 | // The Vulkan names of the limits in `capped`, for the log line. | 87 | // The Vulkan names of the limits in `capped`, for the log line. |
| 84 | inline const char* arena_cap_name(unsigned capped) { | 88 | inline const char* arena_cap_name(unsigned capped) { |
| 89 | if (capped & arena_cap_bar) return "Resizable BAR memory types for a buffer that large"; | ||
| 85 | switch (capped) { | 90 | switch (capped) { |
| 86 | case arena_cap_buffer: return "maxBufferSize"; | 91 | case arena_cap_buffer: return "maxBufferSize"; |
| 87 | case arena_cap_allocation: return "maxMemoryAllocationSize"; | 92 | case arena_cap_allocation: return "maxMemoryAllocationSize"; |
| @@ -90,4 +95,28 @@ inline const char* arena_cap_name(unsigned capped) { | |||
| 90 | } | 95 | } |
| 91 | } | 96 | } |
| 92 | 97 | ||
| 98 | constexpr VkDeviceSize kArenaBarStep = 16 * kArenaMiB; | ||
| 99 | |||
| 100 | // Apply the same memory-type eligibility checks as the arena allocation, requiring its preferred device-local type. | ||
| 101 | inline bool device_local_arena_fits(const VkPhysicalDeviceMemoryProperties& memory, | ||
| 102 | const VkMemoryRequirements& requirements) { | ||
| 103 | return !memory_type_order(memory, requirements.memoryTypeBits, kArenaRequiredMemory | kArenaPreferredMemory, | ||
| 104 | 0, requirements.size).empty(); | ||
| 105 | } | ||
| 106 | |||
| 107 | // With Resizable BAR the arena belongs in VRAM the CPU can write. A driver can leave those memory types out of a | ||
| 108 | // large buffer's memoryTypeBits: on an RTX 5070 Ti (NVIDIA 617.42, Windows 11) a 4,090 MiB buffer may use the | ||
| 109 | // DEVICE_LOCAL | HOST_VISIBLE type and a 4,092 MiB one may not, so LEONIDA_ARENA_MB=4096 (4,095 MiB) put the whole | ||
| 110 | // arena in system memory. `fits(bytes)` says whether a buffer of that size may use such a type. Returns the size to | ||
| 111 | // create: `bytes` when it fits (or when nothing in reach does), else the largest size below it, in 16 MiB steps | ||
| 112 | // and losing at most an eighth, that fits. | ||
| 113 | template <class Fits> | ||
| 114 | VkDeviceSize device_local_arena_bytes(VkDeviceSize bytes, Fits fits) { | ||
| 115 | if (fits(bytes)) return bytes; | ||
| 116 | const VkDeviceSize floor = std::max(bytes - bytes / 8, kArenaMinMiB * kArenaMiB); | ||
| 117 | for (VkDeviceSize size = bytes / kArenaBarStep * kArenaBarStep; size >= floor && size > 0; size -= kArenaBarStep) | ||
| 118 | if (size < bytes && fits(size)) return size; | ||
| 119 | return bytes; | ||
| 120 | } | ||
| 121 | |||
| 93 | } // namespace leonida | 122 | } // namespace leonida |
diff --git a/runtime/src/dual_source_fragment.h b/runtime/src/dual_source_fragment.h new file mode 100644 index 000000000..86bc66236 --- /dev/null +++ b/runtime/src/dual_source_fragment.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Dual-source blending (RT-DUAL-SOURCE-BLEND). D3D12 takes the second blend source from SV_Target1, which the | ||
| 4 | // translated pixel shader declares as the output at Location 1. Vulkan takes it from Location 0, Index 1 | ||
| 5 | // (VK_BLEND_FACTOR_SRC1_*, dualSrcBlend). For a pipeline whose blend state uses a SRC1 factor, the fragment module's | ||
| 6 | // Location 1 output is moved to Location 0 and given Index 1. Standard library only. | ||
| 7 | |||
| 8 | #include <cstdint> | ||
| 9 | #include <map> | ||
| 10 | #include <set> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace leonida::dual_source { | ||
| 14 | |||
| 15 | // Rewrites `words` in place and returns true, or returns false and leaves them unchanged when the module is not one | ||
| 16 | // this rewrite understands: not SPIR-V, no Output variable at Location 1, an output at Location 2 or above (Vulkan has | ||
| 17 | // one dual-source attachment), or an Index decoration already present. | ||
| 18 | inline bool remap(std::vector<std::uint32_t>& words) { | ||
| 19 | constexpr std::uint32_t kOpDecorate = 71, kOpVariable = 59, kOpTypePointer = 32; | ||
| 20 | constexpr std::uint32_t kLocation = 30, kIndex = 32, kBuiltIn = 11, kStorageOutput = 3; | ||
| 21 | if (words.size() < 5 || words[0] != 0x07230203) return false; | ||
| 22 | std::map<std::uint32_t, std::size_t> location_at; // variable id -> word index of its Location literal | ||
| 23 | std::map<std::uint32_t, std::uint32_t> location_of; | ||
| 24 | std::set<std::uint32_t> output_pointers, outputs, builtins; | ||
| 25 | for (std::size_t at = 5; at < words.size();) { | ||
| 26 | const std::uint32_t n = words[at] >> 16, op = words[at] & 0xffffu; | ||
| 27 | if (!n || n > words.size() - at) return false; | ||
| 28 | if (op == kOpDecorate && n == 4 && words[at + 2] == kLocation) { | ||
| 29 | location_at[words[at + 1]] = at + 3; | ||
| 30 | location_of[words[at + 1]] = words[at + 3]; | ||
| 31 | } else if (op == kOpDecorate && n >= 3 && words[at + 2] == kIndex) { | ||
| 32 | return false; | ||
| 33 | } else if (op == kOpDecorate && n >= 3 && words[at + 2] == kBuiltIn) { | ||
| 34 | builtins.insert(words[at + 1]); | ||
| 35 | } else if (op == kOpTypePointer && n == 4 && words[at + 2] == kStorageOutput) { | ||
| 36 | output_pointers.insert(words[at + 1]); | ||
| 37 | } else if (op == kOpVariable && n >= 4 && words[at + 3] == kStorageOutput) { | ||
| 38 | outputs.insert(words[at + 2]); | ||
| 39 | } | ||
| 40 | at += n; | ||
| 41 | } | ||
| 42 | std::uint32_t second = 0; | ||
| 43 | for (const std::uint32_t id : outputs) { | ||
| 44 | if (builtins.count(id)) continue; | ||
| 45 | const auto loc = location_of.find(id); | ||
| 46 | if (loc == location_of.end()) continue; | ||
| 47 | if (loc->second >= 2) return false; | ||
| 48 | if (loc->second == 1) second = id; | ||
| 49 | } | ||
| 50 | if (!second) return false; | ||
| 51 | const std::size_t literal = location_at[second]; | ||
| 52 | words[literal] = 0; | ||
| 53 | // OpDecorate %second Index 1, right after its Location decoration (annotations stay together). | ||
| 54 | const std::vector<std::uint32_t> index{(4u << 16) | kOpDecorate, second, kIndex, 1}; | ||
| 55 | words.insert(words.begin() + static_cast<std::ptrdiff_t>(literal + 1), index.begin(), index.end()); | ||
| 56 | return true; | ||
| 57 | } | ||
| 58 | |||
| 59 | } // namespace leonida::dual_source | ||
diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index 8982c701a..22427d1f5 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,28 @@ 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 | |||
| 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 | |||
| 96 | // 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 |
| 97 | // 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. |
| 98 | struct FixedState { | 115 | struct FixedState { |
| 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 | ||
| 99 | 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 |
| 100 | std::uint32_t sample_mask = 0; // +0x1c | 119 | std::uint32_t sample_mask = 0; // +0x1c |
| 101 | std::uint8_t dsv_format = 0; // +0x24 | 120 | std::uint8_t dsv_format = 0; // +0x24 |
| @@ -153,6 +172,222 @@ inline std::optional<FixedState> decode_state_b(std::span<const std::uint8_t> b, | |||
| 153 | return s; | 172 | return s; |
| 154 | } | 173 | } |
| 155 | 174 | ||
| 175 | // State blob A, measured on 1.0.2.1 over all 5,680 distinct blobs (25,471 graphics records, docs/fspx.md "State blob | ||
| 176 | // A"): 380 + 8k bytes (k = 0..5) for vertex-shader pipelines and 464 + 8k (k = 0..7) for mesh pipelines, whose stage | ||
| 177 | // block is 84 bytes longer, so every offset after +0x28 moves by 84 there. Word 0 is 0x4005d800 with bits 3 and 4 | ||
| 178 | // free. CB_TARGET_MASK is at +0x78 and CB_BLEND0..7_CONTROL at +0xd0..+0xec (vertex-pipeline offsets). Any other | ||
| 179 | // shape is not decoded: the caller keeps its defaults. | ||
| 180 | inline constexpr std::uint32_t kStateATag = 0x4005d800; | ||
| 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; | ||
| 184 | |||
| 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) { | ||
| 187 | const auto fail = [&](const char* reason) { | ||
| 188 | if (why) *why = reason; | ||
| 189 | return std::nullopt; | ||
| 190 | }; | ||
| 191 | const std::size_t n = a.size(); | ||
| 192 | std::size_t shift = 0; | ||
| 193 | if (n >= 380 && n <= 420 && (n - 380) % 8 == 0) shift = 0; | ||
| 194 | else if (n >= 464 && n <= 520 && (n - 464) % 8 == 0) shift = kStateAMeshShift; | ||
| 195 | else return fail("blob A size not one of the measured shapes"); | ||
| 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; | ||
| 203 | BlendRegisters r; | ||
| 204 | r.target_mask = read_u32(a, kStateATargetMask + *shift); | ||
| 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); | ||
| 222 | return r; | ||
| 223 | } | ||
| 224 | |||
| 225 | // One target's blend state in D3D12 terms, with D3D12_BLEND and D3D12_BLEND_OP values (d3d12.h) so this header | ||
| 226 | // stays standard-library only. | ||
| 227 | struct TargetBlend { | ||
| 228 | bool enable = false; | ||
| 229 | std::uint8_t src = 2, dst = 1, op = 1; // D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD | ||
| 230 | std::uint8_t src_alpha = 2, dst_alpha = 1, op_alpha = 1; | ||
| 231 | std::uint8_t write_mask = 0xf; // D3D12_COLOR_WRITE_ENABLE_*: R 1, G 2, B 4, A 8 | ||
| 232 | }; | ||
| 233 | |||
| 234 | // An RDNA2 blend factor (V_028780_BLEND_*, the gfx10 register header) as a D3D12_BLEND, or 0 for one this path does | ||
| 235 | // not take: BOTH_(INV_)SRC_ALPHA (11, 12; not on gfx10). The dual-source SRC1 factors (15 to 18) read the pixel | ||
| 236 | // shader's SV_Target1, which the Vulkan layer moves to Location 0, Index 1 (RT-DUAL-SOURCE-BLEND). For an alpha | ||
| 237 | // factor, a colour factor names its alpha. | ||
| 238 | inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { | ||
| 239 | static constexpr std::uint8_t colour[21] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 14, 15, 16, 17, 18, 19, 20, 21}; | ||
| 240 | if (f >= std::size(colour)) return 0; | ||
| 241 | if (alpha) { | ||
| 242 | switch (f) { | ||
| 243 | case 2: return 5; // SRC_COLOR -> SRC_ALPHA | ||
| 244 | case 3: return 6; // ONE_MINUS_SRC_COLOR -> INV_SRC_ALPHA | ||
| 245 | case 8: return 7; // DST_COLOR -> DEST_ALPHA | ||
| 246 | case 9: return 8; // ONE_MINUS_DST_COLOR -> INV_DEST_ALPHA | ||
| 247 | case 15: return 18; // SRC1_COLOR -> SRC1_ALPHA | ||
| 248 | case 16: return 19; // INV_SRC1_COLOR -> INV_SRC1_ALPHA | ||
| 249 | default: break; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | return colour[f]; | ||
| 253 | } | ||
| 254 | |||
| 255 | // An RDNA2 COMB_FCN as a D3D12_BLEND_OP, or 0: DST_PLUS_SRC ADD, SRC_MINUS_DST SUBTRACT, MIN, MAX, DST_MINUS_SRC | ||
| 256 | // REV_SUBTRACT. | ||
| 257 | inline std::uint8_t d3d12_blend_op(std::uint32_t c) { | ||
| 258 | static constexpr std::uint8_t op[5] = {1, 2, 4, 5, 3}; | ||
| 259 | return c < std::size(op) ? op[c] : 0; | ||
| 260 | } | ||
| 261 | |||
| 262 | // CB_BLENDn_CONTROL: COLOR_SRCBLEND [4:0], COLOR_COMB_FCN [7:5], COLOR_DESTBLEND [12:8], ALPHA_SRCBLEND [20:16], | ||
| 263 | // ALPHA_COMB_FCN [23:21], ALPHA_DESTBLEND [28:24], SEPARATE_ALPHA_BLEND [29], ENABLE [30], DISABLE_ROP3 [31]. Without | ||
| 264 | // SEPARATE_ALPHA_BLEND the alpha channel takes the colour fields. Empty for an enabled target this path cannot express. | ||
| 265 | inline std::optional<TargetBlend> d3d12_target_blend(std::uint32_t cb_blend, std::uint32_t write_mask) { | ||
| 266 | TargetBlend t; | ||
| 267 | t.write_mask = static_cast<std::uint8_t>(write_mask & 0xf); | ||
| 268 | if (!(cb_blend >> 30 & 1)) return t; | ||
| 269 | const bool separate = cb_blend >> 29 & 1; | ||
| 270 | const std::uint32_t cs = cb_blend & 31, cf = cb_blend >> 5 & 7, cd = cb_blend >> 8 & 31; | ||
| 271 | const std::uint32_t as = separate ? cb_blend >> 16 & 31 : cs, af = separate ? cb_blend >> 21 & 7 : cf, | ||
| 272 | ad = separate ? cb_blend >> 24 & 31 : cd; | ||
| 273 | t.enable = true; | ||
| 274 | t.src = d3d12_blend_factor(cs, false); | ||
| 275 | t.dst = d3d12_blend_factor(cd, false); | ||
| 276 | t.op = d3d12_blend_op(cf); | ||
| 277 | t.src_alpha = d3d12_blend_factor(as, true); | ||
| 278 | t.dst_alpha = d3d12_blend_factor(ad, true); | ||
| 279 | t.op_alpha = d3d12_blend_op(af); | ||
| 280 | if (!t.src || !t.dst || !t.op || !t.src_alpha || !t.dst_alpha || !t.op_alpha) return std::nullopt; | ||
| 281 | return t; | ||
| 282 | } | ||
| 283 | |||
| 284 | // A DXGI_FORMAT with UINT or SINT channels. The colour block does not blend those (RDNA2 bypasses blending for integer | ||
| 285 | // targets), and D3D12 refuses a PSO that enables blending on one. | ||
| 286 | inline bool dxgi_integer_format(std::uint32_t f) { | ||
| 287 | switch (f) { | ||
| 288 | case 3: case 4: case 7: case 8: case 12: case 14: case 17: case 18: case 25: case 30: case 32: case 36: | ||
| 289 | case 38: case 42: case 43: case 50: case 52: case 57: case 59: case 62: case 64: | ||
| 290 | return true; | ||
| 291 | default: | ||
| 292 | return false; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | // The targets 0..count-1 of `r` in D3D12 terms, or empty when one of them cannot be expressed: the pipeline then keeps | ||
| 297 | // the defaults (blending off, every component written) for all of its targets, as before blob A was read. A target | ||
| 298 | // with an integer format (`formats`, DXGI values) keeps its write mask and has blending off, as on the console. | ||
| 299 | inline std::optional<std::array<TargetBlend, 8>> d3d12_blend(const BlendRegisters& r, unsigned count, | ||
| 300 | const std::array<std::uint8_t, 8>& formats = {}) { | ||
| 301 | std::array<TargetBlend, 8> out{}; | ||
| 302 | for (unsigned i = 0; i < count && i < out.size(); ++i) { | ||
| 303 | const std::uint32_t mask = r.target_mask >> (4 * i); | ||
| 304 | const auto t = dxgi_integer_format(formats[i]) ? d3d12_target_blend(r.blend[i] & ~(1u << 30), mask) | ||
| 305 | : d3d12_target_blend(r.blend[i], mask); | ||
| 306 | if (!t) return std::nullopt; | ||
| 307 | out[i] = *t; | ||
| 308 | } | ||
| 309 | return out; | ||
| 310 | } | ||
| 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 | |||
| 156 | using Digest = std::array<std::uint8_t, 16>; | 391 | using Digest = std::array<std::uint8_t, 16>; |
| 157 | 392 | ||
| 158 | // 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 |
| @@ -293,6 +528,10 @@ inline Plan plan(const GraphicsDesc& desc, const HashMap& map) { | |||
| 293 | auto state = decode_state_b(bytes(desc.state_b), &p.reason); | 528 | auto state = decode_state_b(bytes(desc.state_b), &p.reason); |
| 294 | if (!state) return p; | 529 | if (!state) return p; |
| 295 | p.state = std::move(*state); | 530 | p.state = std::move(*state); |
| 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 | } | ||
| 296 | for (std::size_t k = 0; k < kStageBlocks; ++k) { | 535 | for (std::size_t k = 0; k < kStageBlocks; ++k) { |
| 297 | const StageBlock& block = desc.stages[k]; | 536 | const StageBlock& block = desc.stages[k]; |
| 298 | 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); |
diff --git a/runtime/src/title_window_plan.h b/runtime/src/title_window_plan.h index 807641df1..cd17bc596 100644 --- a/runtime/src/title_window_plan.h +++ b/runtime/src/title_window_plan.h | |||
| @@ -31,12 +31,13 @@ inline bool title_window_fits(std::uint64_t cap, std::uint64_t used, std::uint64 | |||
| 31 | inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint64_t lo, std::uint64_t hi, | 31 | inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint64_t lo, std::uint64_t hi, |
| 32 | std::uint64_t align, std::uint64_t most, std::uint64_t cap, std::uint64_t used, | 32 | std::uint64_t align, std::uint64_t most, std::uint64_t cap, std::uint64_t used, |
| 33 | TitleWindowRange& out) { | 33 | TitleWindowRange& out) { |
| 34 | if (!align || hi <= va) return false; | 34 | if (!align || (align & (align - 1)) != 0 || !most || va < lo || hi <= va) return false; |
| 35 | const std::uint64_t want = std::clamp<std::uint64_t>(bytes ? bytes : 1, 1, most); | 35 | const std::uint64_t want = (std::min)(bytes ? bytes : std::uint64_t(1), most); |
| 36 | const std::uint64_t start = (std::max)(va & ~(align - 1), lo); | 36 | const std::uint64_t start = (std::max)(va & ~(align - 1), lo); |
| 37 | std::uint64_t end = va > UINT64_MAX - want - align ? UINT64_MAX : (va + want + align - 1) & ~(align - 1); | 37 | const std::uint64_t raw_end = want > UINT64_MAX - va ? UINT64_MAX : va + want; |
| 38 | std::uint64_t end = raw_end > UINT64_MAX - (align - 1) ? UINT64_MAX : (raw_end + align - 1) & ~(align - 1); | ||
| 38 | end = (std::min)(end, hi); | 39 | end = (std::min)(end, hi); |
| 39 | if (end <= start) return false; | 40 | if (end <= va || end <= start) return false; |
| 40 | const TitleWindowRange range{start, end - start}; | 41 | const TitleWindowRange range{start, end - start}; |
| 41 | if (!title_window_fits(cap, used, range.size)) return false; | 42 | if (!title_window_fits(cap, used, range.size)) return false; |
| 42 | out = range; | 43 | out = range; |
| @@ -49,13 +50,15 @@ inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint6 | |||
| 49 | inline bool title_pool_extent(std::uint64_t pool_lo, std::uint64_t pool_hi, std::uint64_t va, | 50 | inline bool title_pool_extent(std::uint64_t pool_lo, std::uint64_t pool_hi, std::uint64_t va, |
| 50 | std::uint64_t readable_end, std::uint64_t align, std::uint64_t most, | 51 | std::uint64_t readable_end, std::uint64_t align, std::uint64_t most, |
| 51 | TitleWindowRange& out) { | 52 | TitleWindowRange& out) { |
| 52 | if (!align || pool_hi <= pool_lo || va < pool_lo || va >= pool_hi) return false; | 53 | if (!align || (align & (align - 1)) != 0 || pool_hi <= pool_lo || va < pool_lo || va >= pool_hi || |
| 54 | readable_end <= va) | ||
| 55 | return false; | ||
| 53 | const std::uint64_t start = (std::max)(va & ~(align - 1), pool_lo); | 56 | const std::uint64_t start = (std::max)(va & ~(align - 1), pool_lo); |
| 54 | std::uint64_t end = (std::min)(readable_end, pool_hi); | 57 | std::uint64_t end = (std::min)(readable_end, pool_hi); |
| 55 | if (most) end = (std::min)(end, start > UINT64_MAX - most ? UINT64_MAX : start + most); | ||
| 56 | end = end > UINT64_MAX - (align - 1) ? UINT64_MAX : (end + align - 1) & ~(align - 1); | 58 | end = end > UINT64_MAX - (align - 1) ? UINT64_MAX : (end + align - 1) & ~(align - 1); |
| 59 | if (most) end = (std::min)(end, start > UINT64_MAX - most ? UINT64_MAX : start + most); | ||
| 57 | end = (std::min)(end, pool_hi); | 60 | end = (std::min)(end, pool_hi); |
| 58 | if (end <= va) return false; | 61 | if (end <= va || end <= start) return false; |
| 59 | out = {start, end - start}; | 62 | out = {start, end - start}; |
| 60 | return true; | 63 | return true; |
| 61 | } | 64 | } |