diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 37 | ||||
| -rw-r--r-- | runtime/src/descriptor_table.h | 252 | ||||
| -rw-r--r-- | runtime/src/root_signature.cpp | 456 | ||||
| -rw-r--r-- | runtime/src/root_signature.h | 150 | ||||
| -rw-r--r-- | runtime/test/descriptor_table_test.cpp | 234 | ||||
| -rw-r--r-- | runtime/test/root_signature_oracle_test.cpp | 120 | ||||
| -rw-r--r-- | runtime/test/root_signature_test.cpp | 221 | ||||
| -rw-r--r-- | runtime/test/shaders/build.py | 24 | ||||
| -rw-r--r-- | runtime/test/shaders/check.py | 6 | ||||
| -rw-r--r-- | runtime/test/shaders/cs_smem_soffset_imm.asm | 61 | ||||
| -rw-r--r-- | runtime/test/vk_texture_test.cpp | 27 | ||||
| -rw-r--r-- | runtime/test/vulkan_loader.h | 11 |
12 files changed, 1566 insertions, 33 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3051d8012..d2df25019 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -17,6 +17,24 @@ if(WIN32) | |||
| 17 | target_compile_definitions(gta6rt_d3d12_xs_rootsig PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | 17 | target_compile_definitions(gta6rt_d3d12_xs_rootsig PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) |
| 18 | target_compile_options(gta6rt_d3d12_xs_rootsig PRIVATE /W4 /permissive- /utf-8) | 18 | target_compile_options(gta6rt_d3d12_xs_rootsig PRIVATE /W4 /permissive- /utf-8) |
| 19 | 19 | ||
| 20 | # Portable root-signature model + packer to user SGPRs (no d3d12.h, no windows.h). | ||
| 21 | add_library(gta6rt_root_signature STATIC | ||
| 22 | src/root_signature.cpp | ||
| 23 | ) | ||
| 24 | target_include_directories(gta6rt_root_signature PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) | ||
| 25 | target_compile_definitions(gta6rt_root_signature PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | ||
| 26 | target_compile_options(gta6rt_root_signature PRIVATE /W4 /permissive- /utf-8) | ||
| 27 | |||
| 28 | # Portable contract test: model, packer, canonical codec and RTS0 hypothesis. | ||
| 29 | # Needs neither d3d12.lib nor the game; runs on Windows CI like the rest here. | ||
| 30 | add_executable(root_signature_test | ||
| 31 | test/root_signature_test.cpp | ||
| 32 | ) | ||
| 33 | target_link_libraries(root_signature_test PRIVATE gta6rt_root_signature) | ||
| 34 | target_compile_definitions(root_signature_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | ||
| 35 | target_compile_options(root_signature_test PRIVATE /W4 /permissive- /utf-8) | ||
| 36 | target_link_options(root_signature_test PRIVATE /OPT:NOREF) | ||
| 37 | |||
| 20 | add_executable(gta6rt | 38 | add_executable(gta6rt |
| 21 | src/main.cpp | 39 | src/main.cpp |
| 22 | src/pe_loader.cpp | 40 | src/pe_loader.cpp |
| @@ -57,6 +75,17 @@ if(WIN32) | |||
| 57 | target_compile_options(root_sig_test PRIVATE /W4 /permissive- /utf-8) | 75 | target_compile_options(root_sig_test PRIVATE /W4 /permissive- /utf-8) |
| 58 | target_link_options(root_sig_test PRIVATE /OPT:NOREF) | 76 | target_link_options(root_sig_test PRIVATE /OPT:NOREF) |
| 59 | 77 | ||
| 78 | # RTS0 parser oracle: synthetic descriptors only, serialized by the real PC | ||
| 79 | # d3d12.dll and parsed back by rt::rootsig::parse_rts0. A layout mismatch | ||
| 80 | # fails here with the blob bytes, never silently in a draw. | ||
| 81 | add_executable(root_signature_oracle_test | ||
| 82 | test/root_signature_oracle_test.cpp | ||
| 83 | ) | ||
| 84 | target_link_libraries(root_signature_oracle_test PRIVATE gta6rt_root_signature ${D3D12_LIBRARY}) | ||
| 85 | target_compile_definitions(root_signature_oracle_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | ||
| 86 | target_compile_options(root_signature_oracle_test PRIVATE /W4 /permissive- /utf-8) | ||
| 87 | target_link_options(root_signature_oracle_test PRIVATE /OPT:NOREF) | ||
| 88 | |||
| 60 | # Xbox-ABI device proxy test: WARP PC device, no game executable. Needs DXGI for the WARP adapter. | 89 | # Xbox-ABI device proxy test: WARP PC device, no game executable. Needs DXGI for the WARP adapter. |
| 61 | if(DXGI_LIBRARY) | 90 | if(DXGI_LIBRARY) |
| 62 | add_executable(device_proxy_test | 91 | add_executable(device_proxy_test |
| @@ -159,6 +188,8 @@ if(BUILD_TESTING) | |||
| 159 | endif() | 188 | endif() |
| 160 | add_executable(command_list_test test/command_list_test.cpp) | 189 | add_executable(command_list_test test/command_list_test.cpp) |
| 161 | add_test(NAME command_list COMMAND command_list_test) | 190 | add_test(NAME command_list COMMAND command_list_test) |
| 191 | add_executable(descriptor_table_test test/descriptor_table_test.cpp) | ||
| 192 | add_test(NAME descriptor_table COMMAND descriptor_table_test) | ||
| 162 | if(WIN32) | 193 | if(WIN32) |
| 163 | # Optional: skips unless LEONIDA_GAME_DIR and LEONIDA_MOVIE are set. | 194 | # Optional: skips unless LEONIDA_GAME_DIR and LEONIDA_MOVIE are set. |
| 164 | add_executable(bink_decode_test | 195 | add_executable(bink_decode_test |
| @@ -191,6 +222,12 @@ if(BUILD_TESTING) | |||
| 191 | if(WIN32 AND TARGET xmem_test) | 222 | if(WIN32 AND TARGET xmem_test) |
| 192 | add_test(NAME xmem COMMAND xmem_test) | 223 | add_test(NAME xmem COMMAND xmem_test) |
| 193 | endif() | 224 | endif() |
| 225 | if(WIN32 AND TARGET root_signature_test) | ||
| 226 | add_test(NAME root_signature COMMAND root_signature_test) | ||
| 227 | endif() | ||
| 228 | if(WIN32 AND TARGET root_signature_oracle_test) | ||
| 229 | add_test(NAME root_signature_oracle COMMAND root_signature_oracle_test) | ||
| 230 | endif() | ||
| 194 | endif() | 231 | endif() |
| 195 | 232 | ||
| 196 | if(WIN32) | 233 | if(WIN32) |
diff --git a/runtime/src/descriptor_table.h b/runtime/src/descriptor_table.h new file mode 100644 index 000000000..2fc9e99cb --- /dev/null +++ b/runtime/src/descriptor_table.h | |||
| @@ -0,0 +1,252 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // The descriptor hash table for set 0, binding 2 (docs/d3d12_vulkan.md section 5.1 and docs/design.md): | ||
| 4 | // Shaders look up bindless resource/sampler indices by FNV-1a hashing the 8-dword descriptor key (T# or padded S#) | ||
| 5 | // and probing up to 8 slots in this table. Miss or fallback yields index 0. | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <array> | ||
| 9 | #include <cstdint> | ||
| 10 | #include <cstring> | ||
| 11 | #include <mutex> | ||
| 12 | #include <span> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | namespace leonida { | ||
| 16 | |||
| 17 | // FNV-1a constants for 32-bit dwords | ||
| 18 | inline constexpr std::uint32_t kFnvOffsetBasis = 2166136261u; | ||
| 19 | inline constexpr std::uint32_t kFnvPrime = 16777619u; | ||
| 20 | inline constexpr std::uint32_t kDescriptorProbes = 8; | ||
| 21 | inline constexpr std::uint32_t kSlotOccupiedMarker = 0xA11CA7EDu; | ||
| 22 | |||
| 23 | // Key types: | ||
| 24 | // T# key is 8 dwords. | ||
| 25 | // S# key is 4 dwords followed by 4x 0xFFFFFFFF. | ||
| 26 | using DescriptorKey = std::array<std::uint32_t, 8>; | ||
| 27 | |||
| 28 | inline std::uint32_t hash_descriptor_key(const std::uint32_t key[8]) { | ||
| 29 | std::uint32_t h = kFnvOffsetBasis; | ||
| 30 | for (int i = 0; i < 8; ++i) { | ||
| 31 | h = (h ^ key[i]) * kFnvPrime; | ||
| 32 | } | ||
| 33 | return h; | ||
| 34 | } | ||
| 35 | |||
| 36 | inline std::uint32_t hash_descriptor_key(const DescriptorKey& key) { | ||
| 37 | return hash_descriptor_key(key.data()); | ||
| 38 | } | ||
| 39 | |||
| 40 | inline DescriptorKey make_tsharp_key(const std::uint32_t tsharp[8]) { | ||
| 41 | DescriptorKey k{}; | ||
| 42 | std::memcpy(k.data(), tsharp, sizeof(DescriptorKey)); | ||
| 43 | return k; | ||
| 44 | } | ||
| 45 | |||
| 46 | inline DescriptorKey make_ssharp_key(const std::uint32_t ssharp[4]) { | ||
| 47 | DescriptorKey k{}; | ||
| 48 | k[0] = ssharp[0]; | ||
| 49 | k[1] = ssharp[1]; | ||
| 50 | k[2] = ssharp[2]; | ||
| 51 | k[3] = ssharp[3]; | ||
| 52 | k[4] = 0xFFFFFFFFu; | ||
| 53 | k[5] = 0xFFFFFFFFu; | ||
| 54 | k[6] = 0xFFFFFFFFu; | ||
| 55 | k[7] = 0xFFFFFFFFu; | ||
| 56 | return k; | ||
| 57 | } | ||
| 58 | |||
| 59 | // Memory layout of each entry in the table: 10 uint32s = 40 bytes. | ||
| 60 | struct DescriptorEntry { | ||
| 61 | std::uint32_t key[8] = {}; | ||
| 62 | std::uint32_t index = 0; | ||
| 63 | std::uint32_t pad = 0; // 0 when empty, kSlotOccupiedMarker when occupied | ||
| 64 | }; | ||
| 65 | static_assert(sizeof(DescriptorEntry) == 40); | ||
| 66 | |||
| 67 | class DescriptorTable { | ||
| 68 | public: | ||
| 69 | explicit DescriptorTable(std::uint32_t initial_capacity = 16) { | ||
| 70 | std::uint32_t cap = 16; | ||
| 71 | while (cap < initial_capacity) cap <<= 1; | ||
| 72 | mask_ = cap - 1; | ||
| 73 | entries_.resize(cap); | ||
| 74 | } | ||
| 75 | |||
| 76 | // Inserts or updates a key -> index mapping. | ||
| 77 | // If the key already exists in one of the 8 probe slots, its index is updated. | ||
| 78 | // If insertion would exceed 8 probes or 50% load, the table grows and re-hashes. | ||
| 79 | bool insert(const std::uint32_t key[8], std::uint32_t index) { | ||
| 80 | std::lock_guard lock(mutex_); | ||
| 81 | return insert_locked(key, index); | ||
| 82 | } | ||
| 83 | |||
| 84 | bool insert(const DescriptorKey& key, std::uint32_t index) { | ||
| 85 | return insert(key.data(), index); | ||
| 86 | } | ||
| 87 | |||
| 88 | // Queries the index associated with key. Returns 0 if not found. | ||
| 89 | std::uint32_t lookup(const std::uint32_t key[8]) const { | ||
| 90 | std::lock_guard lock(mutex_); | ||
| 91 | const std::uint32_t h = hash_descriptor_key(key); | ||
| 92 | for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { | ||
| 93 | const std::uint32_t slot = (h + k) & mask_; | ||
| 94 | const auto& entry = entries_[slot]; | ||
| 95 | if (entry.pad == kSlotOccupiedMarker && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { | ||
| 96 | return entry.index; | ||
| 97 | } | ||
| 98 | if (entry.pad != kSlotOccupiedMarker) { | ||
| 99 | return 0; // Empty slot encountered: key is definitely absent | ||
| 100 | } | ||
| 101 | } | ||
| 102 | return 0; // Exceeded 8 probes without a match | ||
| 103 | } | ||
| 104 | |||
| 105 | std::uint32_t lookup(const DescriptorKey& key) const { | ||
| 106 | return lookup(key.data()); | ||
| 107 | } | ||
| 108 | |||
| 109 | bool contains(const std::uint32_t key[8]) const { | ||
| 110 | std::lock_guard lock(mutex_); | ||
| 111 | const std::uint32_t h = hash_descriptor_key(key); | ||
| 112 | for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { | ||
| 113 | const std::uint32_t slot = (h + k) & mask_; | ||
| 114 | const auto& entry = entries_[slot]; | ||
| 115 | if (entry.pad == kSlotOccupiedMarker && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { | ||
| 116 | return true; | ||
| 117 | } | ||
| 118 | if (entry.pad != kSlotOccupiedMarker) return false; | ||
| 119 | } | ||
| 120 | return false; | ||
| 121 | } | ||
| 122 | |||
| 123 | bool contains(const DescriptorKey& key) const { | ||
| 124 | return contains(key.data()); | ||
| 125 | } | ||
| 126 | |||
| 127 | void clear() { | ||
| 128 | std::lock_guard lock(mutex_); | ||
| 129 | std::fill(entries_.begin(), entries_.end(), DescriptorEntry{}); | ||
| 130 | occupied_count_ = 0; | ||
| 131 | } | ||
| 132 | |||
| 133 | std::size_t size() const { | ||
| 134 | std::lock_guard lock(mutex_); | ||
| 135 | return occupied_count_; | ||
| 136 | } | ||
| 137 | |||
| 138 | std::size_t capacity() const { | ||
| 139 | std::lock_guard lock(mutex_); | ||
| 140 | return entries_.size(); | ||
| 141 | } | ||
| 142 | |||
| 143 | std::uint32_t mask() const { | ||
| 144 | std::lock_guard lock(mutex_); | ||
| 145 | return mask_; | ||
| 146 | } | ||
| 147 | |||
| 148 | std::size_t size_dwords() const { | ||
| 149 | std::lock_guard lock(mutex_); | ||
| 150 | return 1 + entries_.size() * 10; | ||
| 151 | } | ||
| 152 | |||
| 153 | std::size_t size_bytes() const { | ||
| 154 | return size_dwords() * sizeof(std::uint32_t); | ||
| 155 | } | ||
| 156 | |||
| 157 | // Exports the table buffer format consumed by SPIR-V set 0, binding 2. | ||
| 158 | // Word 0 is mask; followed by entries[mask + 1] of 10 dwords: key[8], index, pad. | ||
| 159 | std::vector<std::uint32_t> build_gpu_buffer(bool clear_pad = false) const { | ||
| 160 | std::lock_guard lock(mutex_); | ||
| 161 | std::vector<std::uint32_t> out(1 + entries_.size() * 10, 0); | ||
| 162 | out[0] = mask_; | ||
| 163 | for (std::size_t s = 0; s < entries_.size(); ++s) { | ||
| 164 | const std::size_t base = 1 + s * 10; | ||
| 165 | std::memcpy(&out[base], entries_[s].key, sizeof(entries_[s].key)); | ||
| 166 | out[base + 8] = entries_[s].index; | ||
| 167 | out[base + 9] = clear_pad ? 0 : entries_[s].pad; | ||
| 168 | } | ||
| 169 | return out; | ||
| 170 | } | ||
| 171 | |||
| 172 | void write_to_buffer(void* dest, bool clear_pad = false) const { | ||
| 173 | if (!dest) return; | ||
| 174 | const auto buf = build_gpu_buffer(clear_pad); | ||
| 175 | std::memcpy(dest, buf.data(), buf.size() * sizeof(std::uint32_t)); | ||
| 176 | } | ||
| 177 | |||
| 178 | private: | ||
| 179 | bool insert_locked(const std::uint32_t key[8], std::uint32_t index) { | ||
| 180 | while (true) { | ||
| 181 | const std::uint32_t h = hash_descriptor_key(key); | ||
| 182 | int empty_slot = -1; | ||
| 183 | |||
| 184 | for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { | ||
| 185 | const std::uint32_t slot = (h + k) & mask_; | ||
| 186 | auto& entry = entries_[slot]; | ||
| 187 | if (entry.pad == kSlotOccupiedMarker) { | ||
| 188 | if (std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { | ||
| 189 | entry.index = index; | ||
| 190 | return true; | ||
| 191 | } | ||
| 192 | } else if (empty_slot < 0) { | ||
| 193 | empty_slot = static_cast<int>(slot); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | // Check if we can place in an empty slot without exceeding 50% load | ||
| 198 | if (empty_slot >= 0 && (occupied_count_ + 1) * 2 <= entries_.size()) { | ||
| 199 | auto& entry = entries_[empty_slot]; | ||
| 200 | std::memcpy(entry.key, key, sizeof(entry.key)); | ||
| 201 | entry.index = index; | ||
| 202 | entry.pad = kSlotOccupiedMarker; | ||
| 203 | ++occupied_count_; | ||
| 204 | return true; | ||
| 205 | } | ||
| 206 | |||
| 207 | // Table is full (>50% load) or no slot found in 8 probes: grow and retry | ||
| 208 | grow_locked(); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | void grow_locked() { | ||
| 213 | std::uint32_t new_cap = static_cast<std::uint32_t>(entries_.size() * 2); | ||
| 214 | while (true) { | ||
| 215 | std::vector<DescriptorEntry> new_entries(new_cap); | ||
| 216 | const std::uint32_t new_mask = new_cap - 1; | ||
| 217 | bool rehash_ok = true; | ||
| 218 | |||
| 219 | for (const auto& old : entries_) { | ||
| 220 | if (old.pad != kSlotOccupiedMarker) continue; | ||
| 221 | const std::uint32_t h = hash_descriptor_key(old.key); | ||
| 222 | bool placed = false; | ||
| 223 | for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { | ||
| 224 | const std::uint32_t s = (h + k) & new_mask; | ||
| 225 | if (new_entries[s].pad != kSlotOccupiedMarker) { | ||
| 226 | new_entries[s] = old; | ||
| 227 | placed = true; | ||
| 228 | break; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | if (!placed) { | ||
| 232 | rehash_ok = false; | ||
| 233 | break; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | if (rehash_ok) { | ||
| 238 | entries_ = std::move(new_entries); | ||
| 239 | mask_ = new_mask; | ||
| 240 | break; | ||
| 241 | } | ||
| 242 | new_cap *= 2; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | mutable std::mutex mutex_; | ||
| 247 | std::uint32_t mask_ = 15; | ||
| 248 | std::vector<DescriptorEntry> entries_; | ||
| 249 | std::size_t occupied_count_ = 0; | ||
| 250 | }; | ||
| 251 | |||
| 252 | } // namespace leonida | ||
diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp new file mode 100644 index 000000000..79a633b61 --- /dev/null +++ b/runtime/src/root_signature.cpp | |||
| @@ -0,0 +1,456 @@ | |||
| 1 | // Portable root-signature packer, see root_signature.h. | ||
| 2 | #include "root_signature.h" | ||
| 3 | |||
| 4 | #include <cstring> | ||
| 5 | |||
| 6 | namespace rt::rootsig { | ||
| 7 | |||
| 8 | PackResult pack(const RootSignature& sig) { | ||
| 9 | PackResult out; | ||
| 10 | if (sig.params.size() > 64) { | ||
| 11 | out.error = "root signature has more than 64 root parameters"; | ||
| 12 | return out; | ||
| 13 | } | ||
| 14 | uint32_t cost = 0; | ||
| 15 | for (const RootParameter& p : sig.params) { | ||
| 16 | if (p.type == ParamType::Constants && p.num_values == 0) { | ||
| 17 | out.error = "root constant parameter needs at least 1 dword"; | ||
| 18 | return out; | ||
| 19 | } | ||
| 20 | const uint32_t dwords = p.type == ParamType::Table ? 2 | ||
| 21 | : p.type == ParamType::Descriptor ? 2 | ||
| 22 | : p.num_values; | ||
| 23 | cost += param_cost(p.type, p.num_values); | ||
| 24 | if (cost > kMaxD3d12Cost) { | ||
| 25 | out.error = "root signature exceeds the 64-dword D3D12 cost limit"; | ||
| 26 | return out; | ||
| 27 | } | ||
| 28 | if (out.dword_count + dwords > kMaxUserSgprs) { | ||
| 29 | // Spill layout is unknown (see __XB_GetRootSignatureSpillPointer): | ||
| 30 | // refuse loudly instead of silently truncating every draw. | ||
| 31 | out.error = "root signature needs more than 32 user SGPRs (spill unsupported)"; | ||
| 32 | return out; | ||
| 33 | } | ||
| 34 | const uint32_t param_index = | ||
| 35 | static_cast<uint32_t>(&p - sig.params.data()); | ||
| 36 | for (uint32_t i = 0; i < dwords; ++i) { | ||
| 37 | out.dwords[out.dword_count] = 0; // filled at bind time | ||
| 38 | out.provenance.push_back({param_index, i}); | ||
| 39 | ++out.dword_count; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | out.d3d12_cost = cost; | ||
| 43 | out.ok = true; | ||
| 44 | return out; | ||
| 45 | } | ||
| 46 | |||
| 47 | namespace { | ||
| 48 | |||
| 49 | void put_u32(std::vector<uint8_t>& out, uint32_t v) { | ||
| 50 | out.push_back(static_cast<uint8_t>(v)); | ||
| 51 | out.push_back(static_cast<uint8_t>(v >> 8)); | ||
| 52 | out.push_back(static_cast<uint8_t>(v >> 16)); | ||
| 53 | out.push_back(static_cast<uint8_t>(v >> 24)); | ||
| 54 | } | ||
| 55 | |||
| 56 | void put_f32(std::vector<uint8_t>& out, float v) { | ||
| 57 | uint32_t bits = 0; | ||
| 58 | static_assert(sizeof(bits) == sizeof(v)); | ||
| 59 | std::memcpy(&bits, &v, sizeof(bits)); | ||
| 60 | put_u32(out, bits); | ||
| 61 | } | ||
| 62 | |||
| 63 | class Reader { | ||
| 64 | public: | ||
| 65 | Reader(const uint8_t* data, size_t size) : data_(data), size_(size) {} | ||
| 66 | |||
| 67 | [[nodiscard]] bool u32(uint32_t& v) { | ||
| 68 | if (pos_ + 4 > size_) return false; | ||
| 69 | v = static_cast<uint32_t>(data_[pos_]) | | ||
| 70 | (static_cast<uint32_t>(data_[pos_ + 1]) << 8) | | ||
| 71 | (static_cast<uint32_t>(data_[pos_ + 2]) << 16) | | ||
| 72 | (static_cast<uint32_t>(data_[pos_ + 3]) << 24); | ||
| 73 | pos_ += 4; | ||
| 74 | return true; | ||
| 75 | } | ||
| 76 | |||
| 77 | [[nodiscard]] bool f32(float& v) { | ||
| 78 | uint32_t bits = 0; | ||
| 79 | if (!u32(bits)) return false; | ||
| 80 | std::memcpy(&v, &bits, sizeof(v)); | ||
| 81 | return true; | ||
| 82 | } | ||
| 83 | |||
| 84 | [[nodiscard]] bool at_end() const { return pos_ == size_; } | ||
| 85 | |||
| 86 | private: | ||
| 87 | const uint8_t* data_ = nullptr; | ||
| 88 | size_t size_ = 0; | ||
| 89 | size_t pos_ = 0; | ||
| 90 | }; | ||
| 91 | |||
| 92 | constexpr uint32_t kCanonicalMagic = 0x3153524Cu; // "LRS1" little-endian | ||
| 93 | constexpr uint32_t kCanonicalVersion = 1; | ||
| 94 | |||
| 95 | } // namespace | ||
| 96 | |||
| 97 | std::vector<uint8_t> encode_canonical(const RootSignature& sig) { | ||
| 98 | std::vector<uint8_t> out; | ||
| 99 | put_u32(out, kCanonicalMagic); | ||
| 100 | put_u32(out, kCanonicalVersion); | ||
| 101 | put_u32(out, static_cast<uint32_t>(sig.version)); | ||
| 102 | put_u32(out, static_cast<uint32_t>(sig.params.size())); | ||
| 103 | put_u32(out, static_cast<uint32_t>(sig.static_samplers.size())); | ||
| 104 | put_u32(out, sig.flags); | ||
| 105 | for (const RootParameter& p : sig.params) { | ||
| 106 | put_u32(out, static_cast<uint32_t>(p.type)); | ||
| 107 | put_u32(out, static_cast<uint32_t>(p.visibility)); | ||
| 108 | put_u32(out, p.num_values); | ||
| 109 | put_u32(out, p.shader_register); | ||
| 110 | put_u32(out, p.register_space); | ||
| 111 | put_u32(out, p.descriptor_register); | ||
| 112 | put_u32(out, p.descriptor_space); | ||
| 113 | put_u32(out, static_cast<uint32_t>(p.ranges.size())); | ||
| 114 | for (const DescriptorRange& r : p.ranges) { | ||
| 115 | put_u32(out, r.range_type); | ||
| 116 | put_u32(out, r.num_descriptors); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | for (const StaticSampler& s : sig.static_samplers) { | ||
| 120 | put_u32(out, s.filter); | ||
| 121 | put_u32(out, s.address_u); | ||
| 122 | put_u32(out, s.address_v); | ||
| 123 | put_u32(out, s.address_w); | ||
| 124 | put_f32(out, s.mip_lod_bias); | ||
| 125 | put_u32(out, s.max_anisotropy); | ||
| 126 | put_u32(out, s.comparison_func); | ||
| 127 | put_u32(out, s.border_color); | ||
| 128 | put_f32(out, s.min_lod); | ||
| 129 | put_f32(out, s.max_lod); | ||
| 130 | put_u32(out, s.shader_register); | ||
| 131 | put_u32(out, s.register_space); | ||
| 132 | put_u32(out, static_cast<uint32_t>(s.visibility)); | ||
| 133 | } | ||
| 134 | return out; | ||
| 135 | } | ||
| 136 | |||
| 137 | DecodeResult decode_canonical(const uint8_t* data, size_t size) { | ||
| 138 | DecodeResult out; | ||
| 139 | if (data == nullptr && size != 0) { | ||
| 140 | out.error = "null data with nonzero size"; | ||
| 141 | return out; | ||
| 142 | } | ||
| 143 | Reader r(data == nullptr ? reinterpret_cast<const uint8_t*>("") : data, size); | ||
| 144 | uint32_t magic = 0, version = 0, nparams = 0, nsamplers = 0; | ||
| 145 | if (!r.u32(magic) || magic != kCanonicalMagic) { | ||
| 146 | out.error = "bad canonical magic"; | ||
| 147 | return out; | ||
| 148 | } | ||
| 149 | if (!r.u32(version) || version != kCanonicalVersion) { | ||
| 150 | out.error = "unsupported canonical version"; | ||
| 151 | return out; | ||
| 152 | } | ||
| 153 | uint32_t ver = 0; | ||
| 154 | if (!r.u32(ver) || !r.u32(nparams) || !r.u32(nsamplers) || !r.u32(out.sig.flags)) { | ||
| 155 | out.error = "truncated canonical header"; | ||
| 156 | return out; | ||
| 157 | } | ||
| 158 | if (ver != 1 && ver != 2) { | ||
| 159 | out.error = "unknown root-signature version"; | ||
| 160 | return out; | ||
| 161 | } | ||
| 162 | if (nparams > 64) { | ||
| 163 | out.error = "more than 64 root parameters"; | ||
| 164 | return out; | ||
| 165 | } | ||
| 166 | if (nsamplers > 4096) { | ||
| 167 | out.error = "absurd sampler count"; | ||
| 168 | return out; | ||
| 169 | } | ||
| 170 | out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; | ||
| 171 | out.sig.params.reserve(nparams); | ||
| 172 | for (uint32_t i = 0; i < nparams; ++i) { | ||
| 173 | RootParameter p; | ||
| 174 | uint32_t type = 0, vis = 0, nranges = 0; | ||
| 175 | if (!r.u32(type) || !r.u32(vis) || !r.u32(p.num_values) || !r.u32(p.shader_register) || | ||
| 176 | !r.u32(p.register_space) || !r.u32(p.descriptor_register) || | ||
| 177 | !r.u32(p.descriptor_space) || !r.u32(nranges)) { | ||
| 178 | out.error = "truncated canonical parameter"; | ||
| 179 | return out; | ||
| 180 | } | ||
| 181 | if (type > 2) { | ||
| 182 | out.error = "unknown parameter type"; | ||
| 183 | return out; | ||
| 184 | } | ||
| 185 | p.type = static_cast<ParamType>(type); | ||
| 186 | p.visibility = static_cast<Visibility>(vis); | ||
| 187 | if (nranges > 1024) { | ||
| 188 | out.error = "absurd range count"; | ||
| 189 | return out; | ||
| 190 | } | ||
| 191 | p.ranges.reserve(nranges); | ||
| 192 | for (uint32_t j = 0; j < nranges; ++j) { | ||
| 193 | DescriptorRange range; | ||
| 194 | if (!r.u32(range.range_type) || !r.u32(range.num_descriptors)) { | ||
| 195 | out.error = "truncated canonical range"; | ||
| 196 | return out; | ||
| 197 | } | ||
| 198 | p.ranges.push_back(range); | ||
| 199 | } | ||
| 200 | out.sig.params.push_back(std::move(p)); | ||
| 201 | } | ||
| 202 | out.sig.static_samplers.reserve(nsamplers); | ||
| 203 | for (uint32_t i = 0; i < nsamplers; ++i) { | ||
| 204 | StaticSampler s; | ||
| 205 | uint32_t vis = 0; | ||
| 206 | if (!r.u32(s.filter) || !r.u32(s.address_u) || !r.u32(s.address_v) || !r.u32(s.address_w) || | ||
| 207 | !r.f32(s.mip_lod_bias) || !r.u32(s.max_anisotropy) || !r.u32(s.comparison_func) || | ||
| 208 | !r.u32(s.border_color) || !r.f32(s.min_lod) || !r.f32(s.max_lod) || | ||
| 209 | !r.u32(s.shader_register) || !r.u32(s.register_space) || !r.u32(vis)) { | ||
| 210 | out.error = "truncated canonical sampler"; | ||
| 211 | return out; | ||
| 212 | } | ||
| 213 | s.visibility = static_cast<Visibility>(vis); | ||
| 214 | out.sig.static_samplers.push_back(s); | ||
| 215 | } | ||
| 216 | if (!r.at_end()) { | ||
| 217 | out.error = "trailing bytes after canonical root signature"; | ||
| 218 | return out; | ||
| 219 | } | ||
| 220 | out.ok = true; | ||
| 221 | return out; | ||
| 222 | } | ||
| 223 | |||
| 224 | // RTS0 payload hypothesis: u32 version (1 = 1.0, 2 = 1.1), u32 num_params, | ||
| 225 | // u32 num_static_samplers, u32 flags, then per-parameter fields in d3d12.h | ||
| 226 | // order. Static samplers follow the same field order as D3D12_STATIC_SAMPLER_DESC. | ||
| 227 | // Anything else (v1.2 blobs, extra dwords, truncation) is a hard error. | ||
| 228 | // Payload decoder: the RTS0 chunk contents (see header for the hypothesis). | ||
| 229 | static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { | ||
| 230 | DecodeResult out; | ||
| 231 | if (data == nullptr && size != 0) { | ||
| 232 | out.error = "null data with nonzero size"; | ||
| 233 | return out; | ||
| 234 | } | ||
| 235 | if (data == nullptr) { | ||
| 236 | out.error = "null RTS0 payload"; | ||
| 237 | return out; | ||
| 238 | } | ||
| 239 | // Random access into the payload: every (offset, byte-count) pair is | ||
| 240 | // validated before any byte is touched. Offsets count from payload start. | ||
| 241 | auto at = [&](size_t off, size_t bytes) -> const uint8_t* { | ||
| 242 | if (bytes > size || off > size - bytes) return nullptr; | ||
| 243 | return data + off; | ||
| 244 | }; | ||
| 245 | auto u32_at = [&](size_t off, uint32_t& v) { | ||
| 246 | const uint8_t* p = at(off, 4); | ||
| 247 | if (p == nullptr) return false; | ||
| 248 | v = static_cast<uint32_t>(p[0]) | (static_cast<uint32_t>(p[1]) << 8) | | ||
| 249 | (static_cast<uint32_t>(p[2]) << 16) | (static_cast<uint32_t>(p[3]) << 24); | ||
| 250 | return true; | ||
| 251 | }; | ||
| 252 | auto f32_at = [&](size_t off, float& v) { | ||
| 253 | uint32_t bits = 0; | ||
| 254 | if (!u32_at(off, bits)) return false; | ||
| 255 | static_assert(sizeof(bits) == sizeof(v)); | ||
| 256 | std::memcpy(&v, &bits, sizeof(v)); | ||
| 257 | return true; | ||
| 258 | }; | ||
| 259 | // Header: version, num_params, params_offset, num_samplers, | ||
| 260 | // samplers_offset, flags. | ||
| 261 | uint32_t ver = 0, nparams = 0, params_off = 0, nsamplers = 0, samplers_off = 0, flags = 0; | ||
| 262 | if (!u32_at(0, ver) || !u32_at(4, nparams) || !u32_at(8, params_off) || | ||
| 263 | !u32_at(12, nsamplers) || !u32_at(16, samplers_off) || !u32_at(20, flags)) { | ||
| 264 | out.error = "truncated RTS0 header"; | ||
| 265 | return out; | ||
| 266 | } | ||
| 267 | if (ver != 1 && ver != 2) { | ||
| 268 | out.error = "unknown RTS0 version (not 1.0/1.1)"; | ||
| 269 | return out; | ||
| 270 | } | ||
| 271 | if (nparams > 64) { | ||
| 272 | out.error = "RTS0 with more than 64 root parameters"; | ||
| 273 | return out; | ||
| 274 | } | ||
| 275 | if (nsamplers > 4096) { | ||
| 276 | out.error = "RTS0 with absurd sampler count"; | ||
| 277 | return out; | ||
| 278 | } | ||
| 279 | out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; | ||
| 280 | out.sig.flags = flags; | ||
| 281 | // Parameter table at params_off: num_params x {u32 d3d_type, u32 | ||
| 282 | // visibility, u32 offset}. d3d_type follows D3D12_ROOT_PARAMETER_TYPE | ||
| 283 | // (0 table, 1 constants, 2/3/4 CBV/SRV/UAV root descriptors). | ||
| 284 | constexpr size_t kEntryBytes = 12; | ||
| 285 | if (nparams > 0 && | ||
| 286 | (params_off > size || nparams > (size - params_off) / kEntryBytes)) { | ||
| 287 | out.error = "RTS0 parameter table overruns its payload"; | ||
| 288 | return out; | ||
| 289 | } | ||
| 290 | out.sig.params.reserve(nparams); | ||
| 291 | for (uint32_t i = 0; i < nparams; ++i) { | ||
| 292 | const size_t eoff = static_cast<size_t>(params_off) + i * kEntryBytes; | ||
| 293 | uint32_t dtype = 0, vis = 0, poff = 0; | ||
| 294 | if (!u32_at(eoff, dtype) || !u32_at(eoff + 4, vis) || !u32_at(eoff + 8, poff)) { | ||
| 295 | out.error = "truncated RTS0 parameter entry"; | ||
| 296 | return out; | ||
| 297 | } | ||
| 298 | RootParameter p; | ||
| 299 | p.visibility = static_cast<Visibility>(vis); | ||
| 300 | if (dtype == 1) { | ||
| 301 | p.type = ParamType::Constants; | ||
| 302 | if (!u32_at(poff, p.shader_register) || !u32_at(poff + 4, p.register_space) || | ||
| 303 | !u32_at(poff + 8, p.num_values)) { | ||
| 304 | out.error = "truncated RTS0 constants payload"; | ||
| 305 | return out; | ||
| 306 | } | ||
| 307 | } else if (dtype == 2 || dtype == 3 || dtype == 4) { | ||
| 308 | p.type = ParamType::Descriptor; | ||
| 309 | uint32_t dflags = 0; | ||
| 310 | const size_t need = ver == 2 ? 12 : 8; | ||
| 311 | if (poff > size || need > size - static_cast<size_t>(poff)) { | ||
| 312 | out.error = "truncated RTS0 descriptor payload"; | ||
| 313 | return out; | ||
| 314 | } | ||
| 315 | if (!u32_at(poff, p.descriptor_register) || !u32_at(poff + 4, p.descriptor_space)) { | ||
| 316 | out.error = "truncated RTS0 descriptor payload"; | ||
| 317 | return out; | ||
| 318 | } | ||
| 319 | if (ver == 2 && !u32_at(poff + 8, dflags)) { | ||
| 320 | out.error = "truncated RTS0 descriptor flags"; | ||
| 321 | return out; | ||
| 322 | } | ||
| 323 | (void)dflags; // descriptor flags do not affect the user-SGPR map | ||
| 324 | } else if (dtype == 0) { | ||
| 325 | p.type = ParamType::Table; | ||
| 326 | uint32_t nranges = 0; | ||
| 327 | if (!u32_at(poff, nranges)) { | ||
| 328 | out.error = "truncated RTS0 table payload"; | ||
| 329 | return out; | ||
| 330 | } | ||
| 331 | if (nranges > 1024) { | ||
| 332 | out.error = "absurd RTS0 range count"; | ||
| 333 | return out; | ||
| 334 | } | ||
| 335 | // Range record: {type, num, base_reg, space, offset} (+ flags on 1.1). | ||
| 336 | const size_t stride = ver == 2 ? 24 : 20; | ||
| 337 | if (nranges > 0 && | ||
| 338 | (poff + 4 > size || nranges > (size - (poff + 4)) / stride)) { | ||
| 339 | out.error = "RTS0 ranges overrun their payload"; | ||
| 340 | return out; | ||
| 341 | } | ||
| 342 | p.ranges.reserve(nranges); | ||
| 343 | for (uint32_t j = 0; j < nranges; ++j) { | ||
| 344 | const size_t roff = static_cast<size_t>(poff) + 4 + j * stride; | ||
| 345 | DescriptorRange range; | ||
| 346 | uint32_t base_reg = 0, space = 0, disp_off = 0, rflags = 0; | ||
| 347 | if (!u32_at(roff, range.range_type) || !u32_at(roff + 4, range.num_descriptors) || | ||
| 348 | !u32_at(roff + 8, base_reg) || !u32_at(roff + 12, space) || | ||
| 349 | !u32_at(roff + 16, disp_off) || | ||
| 350 | (ver == 2 && !u32_at(roff + 20, rflags))) { | ||
| 351 | out.error = "truncated RTS0 range"; | ||
| 352 | return out; | ||
| 353 | } | ||
| 354 | (void)base_reg; | ||
| 355 | (void)space; | ||
| 356 | (void)disp_off; | ||
| 357 | (void)rflags; | ||
| 358 | p.ranges.push_back(range); | ||
| 359 | } | ||
| 360 | } else { | ||
| 361 | out.error = "unknown RTS0 parameter type"; | ||
| 362 | return out; | ||
| 363 | } | ||
| 364 | out.sig.params.push_back(std::move(p)); | ||
| 365 | } | ||
| 366 | // Sampler records mirror D3D12_STATIC_SAMPLER_DESC field order. | ||
| 367 | constexpr size_t kSamplerBytes = 13 * 4; | ||
| 368 | if (nsamplers > 0 && | ||
| 369 | (samplers_off > size || nsamplers > (size - samplers_off) / kSamplerBytes)) { | ||
| 370 | out.error = "RTS0 samplers overrun their payload"; | ||
| 371 | return out; | ||
| 372 | } | ||
| 373 | out.sig.static_samplers.reserve(nsamplers); | ||
| 374 | for (uint32_t i = 0; i < nsamplers; ++i) { | ||
| 375 | const size_t soff = static_cast<size_t>(samplers_off) + i * kSamplerBytes; | ||
| 376 | StaticSampler s; | ||
| 377 | uint32_t vis = 0; | ||
| 378 | if (!u32_at(soff, s.filter) || !u32_at(soff + 4, s.address_u) || | ||
| 379 | !u32_at(soff + 8, s.address_v) || !u32_at(soff + 12, s.address_w) || | ||
| 380 | !f32_at(soff + 16, s.mip_lod_bias) || !u32_at(soff + 20, s.max_anisotropy) || | ||
| 381 | !u32_at(soff + 24, s.comparison_func) || !u32_at(soff + 28, s.border_color) || | ||
| 382 | !f32_at(soff + 32, s.min_lod) || !f32_at(soff + 36, s.max_lod) || | ||
| 383 | !u32_at(soff + 40, s.shader_register) || !u32_at(soff + 44, s.register_space) || | ||
| 384 | !u32_at(soff + 48, vis)) { | ||
| 385 | out.error = "truncated RTS0 sampler"; | ||
| 386 | return out; | ||
| 387 | } | ||
| 388 | s.visibility = static_cast<Visibility>(vis); | ||
| 389 | out.sig.static_samplers.push_back(s); | ||
| 390 | } | ||
| 391 | out.ok = true; | ||
| 392 | return out; | ||
| 393 | } | ||
| 394 | |||
| 395 | DecodeResult parse_rts0(const uint8_t* data, size_t size) { | ||
| 396 | DecodeResult out; | ||
| 397 | if ((data == nullptr && size != 0) || (data != nullptr && size < 4)) { | ||
| 398 | out.error = "blob too small for a DXBC container"; | ||
| 399 | return out; | ||
| 400 | } | ||
| 401 | if (data == nullptr) { | ||
| 402 | out.error = "null blob"; | ||
| 403 | return out; | ||
| 404 | } | ||
| 405 | // A bare payload (unit-test path) has no DXBC magic: decode directly. | ||
| 406 | if (!(data[0] == 'D' && data[1] == 'X' && data[2] == 'B' && data[3] == 'C')) { | ||
| 407 | return parse_rts0_payload(data, size); | ||
| 408 | } | ||
| 409 | // DXBC container walk (mirrors input_signature in tools/rdna2spirv.py): | ||
| 410 | // +28: chunk count, +32: chunk offsets, each chunk is FourCC + u32 size. | ||
| 411 | auto at = [&](size_t off, uint32_t& v) { | ||
| 412 | if (off + 4 > size) return false; | ||
| 413 | v = static_cast<uint32_t>(data[off]) | (static_cast<uint32_t>(data[off + 1]) << 8) | | ||
| 414 | (static_cast<uint32_t>(data[off + 2]) << 16) | (static_cast<uint32_t>(data[off + 3]) << 24); | ||
| 415 | return true; | ||
| 416 | }; | ||
| 417 | uint32_t nchunks = 0; | ||
| 418 | if (!at(28, nchunks)) { | ||
| 419 | out.error = "truncated DXBC header"; | ||
| 420 | return out; | ||
| 421 | } | ||
| 422 | if (nchunks > 1024) { | ||
| 423 | out.error = "absurd DXBC chunk count"; | ||
| 424 | return out; | ||
| 425 | } | ||
| 426 | for (uint32_t k = 0; k < nchunks; ++k) { | ||
| 427 | uint32_t off = 0; | ||
| 428 | // NOTE: off and payload_size are u32; promote to size_t BEFORE adding | ||
| 429 | // or a hostile container wraps the check and turns it into an OOB read. | ||
| 430 | if (!at(32 + 4 * static_cast<size_t>(k), off) || | ||
| 431 | static_cast<size_t>(off) + 8 > size) { | ||
| 432 | out.error = "truncated DXBC chunk table"; | ||
| 433 | return out; | ||
| 434 | } | ||
| 435 | if (data[off] == 'R' && data[off + 1] == 'T' && data[off + 2] == 'S' && data[off + 3] == '0') { | ||
| 436 | uint32_t payload_size = 0; | ||
| 437 | if (static_cast<size_t>(off) + 8 > size) { | ||
| 438 | out.error = "truncated RTS0 chunk header"; | ||
| 439 | return out; | ||
| 440 | } | ||
| 441 | payload_size = static_cast<uint32_t>(data[off + 4]) | | ||
| 442 | (static_cast<uint32_t>(data[off + 5]) << 8) | | ||
| 443 | (static_cast<uint32_t>(data[off + 6]) << 16) | | ||
| 444 | (static_cast<uint32_t>(data[off + 7]) << 24); | ||
| 445 | if (static_cast<size_t>(off) + 8 + payload_size > size) { | ||
| 446 | out.error = "RTS0 chunk overruns its container"; | ||
| 447 | return out; | ||
| 448 | } | ||
| 449 | return parse_rts0_payload(data + off + 8, payload_size); | ||
| 450 | } | ||
| 451 | } | ||
| 452 | out.error = "no RTS0 chunk in DXBC container"; | ||
| 453 | return out; | ||
| 454 | } | ||
| 455 | |||
| 456 | } // namespace rt::rootsig | ||
diff --git a/runtime/src/root_signature.h b/runtime/src/root_signature.h new file mode 100644 index 000000000..4c19cf72d --- /dev/null +++ b/runtime/src/root_signature.h | |||
| @@ -0,0 +1,150 @@ | |||
| 1 | // Portable D3D12 root-signature model + packer to user SGPRs (s0..s31). | ||
| 2 | // | ||
| 3 | // No d3d12.h, no windows.h: this compiles on Linux CI and in the Windows | ||
| 4 | // runtime alike. It models exactly what docs/d3d12_vulkan.md section 3.5 | ||
| 5 | // needs: root parameters in order, static samplers (no dwords), the 64-dword | ||
| 6 | // D3D12 cost limit, and the 32-dword console user-SGPR budget. Anything that | ||
| 7 | // does not fit is refused loudly, never guessed (see pack()). | ||
| 8 | // | ||
| 9 | // Numbering follows D3D12 docs: descriptor tables cost 1 dword of the 64-dword | ||
| 10 | // limit, root views (CBV/SRV/UAV) cost 2, 32-bit constants cost their count. | ||
| 11 | #pragma once | ||
| 12 | |||
| 13 | #include <array> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <string> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | namespace rt::rootsig { | ||
| 19 | |||
| 20 | // D3D12_ROOT_SIGNATURE_VERSION subset the layer handles. | ||
| 21 | enum class Version : uint32_t { V1_0 = 1, V1_1 = 2 }; | ||
| 22 | |||
| 23 | // D3D12_ROOT_PARAMETER_TYPE subset (order matches D3D12 docs). | ||
| 24 | enum class ParamType : uint32_t { | ||
| 25 | Constants = 0, // 32BIT_CONSTANTS: NumValues dwords | ||
| 26 | Descriptor = 1, // CBV/SRV/UAV root descriptor: 2 dwords (64-bit VA) | ||
| 27 | Table = 2, // DESCRIPTOR_TABLE: 2 dwords (table GPU VA) | ||
| 28 | }; | ||
| 29 | |||
| 30 | // D3D12_SHADER_VISIBILITY (ALL 0, VERTEX 1, HULL 2, DOMAIN 3, | ||
| 31 | // GEOMETRY 4, PIXEL 5, AMPLIFICATION 6, MESH 7). | ||
| 32 | enum class Visibility : uint32_t { | ||
| 33 | All = 0, | ||
| 34 | Vertex = 1, | ||
| 35 | Geometry = 4, | ||
| 36 | Pixel = 5, | ||
| 37 | Amplification = 6, | ||
| 38 | Mesh = 7, | ||
| 39 | }; | ||
| 40 | |||
| 41 | // One descriptor range inside a table (SRV/CBV/UAV/sampler + count). | ||
| 42 | struct DescriptorRange { | ||
| 43 | uint32_t range_type = 0; // 0 SRV, 1 UAV, 2 CBV, 3 sampler (D3D12 order) | ||
| 44 | uint32_t num_descriptors = 0; | ||
| 45 | }; | ||
| 46 | |||
| 47 | // Static sampler: no user-SGPR dwords; registered in the hash table instead. | ||
| 48 | struct StaticSampler { | ||
| 49 | uint32_t filter = 0; | ||
| 50 | uint32_t address_u = 0; | ||
| 51 | uint32_t address_v = 0; | ||
| 52 | uint32_t address_w = 0; | ||
| 53 | float mip_lod_bias = 0.0f; | ||
| 54 | uint32_t max_anisotropy = 0; | ||
| 55 | uint32_t comparison_func = 0; | ||
| 56 | uint32_t border_color = 0; // 0 transparent-black .. 4 opaque-white | ||
| 57 | float min_lod = 0.0f; | ||
| 58 | float max_lod = 0.0f; | ||
| 59 | uint32_t shader_register = 0; | ||
| 60 | uint32_t register_space = 0; | ||
| 61 | Visibility visibility = Visibility::All; | ||
| 62 | }; | ||
| 63 | |||
| 64 | struct RootParameter { | ||
| 65 | ParamType type = ParamType::Constants; | ||
| 66 | Visibility visibility = Visibility::All; | ||
| 67 | // Constants: | ||
| 68 | uint32_t num_values = 0; // shader register + space describe the slot | ||
| 69 | uint32_t shader_register = 0; | ||
| 70 | uint32_t register_space = 0; | ||
| 71 | // Root descriptor: | ||
| 72 | uint32_t descriptor_register = 0; | ||
| 73 | uint32_t descriptor_space = 0; | ||
| 74 | // Descriptor table: | ||
| 75 | std::vector<DescriptorRange> ranges; | ||
| 76 | }; | ||
| 77 | |||
| 78 | struct RootSignature { | ||
| 79 | Version version = Version::V1_1; | ||
| 80 | std::vector<RootParameter> params; // at most 64 root parameters (D3D12) | ||
| 81 | std::vector<StaticSampler> static_samplers; | ||
| 82 | uint32_t flags = 0; // low 16 bits: PC meaning; high byte: Xbox type bits | ||
| 83 | }; | ||
| 84 | |||
| 85 | // Where one packed dword came from (provenance for diagnostics). | ||
| 86 | struct DwordProvenance { | ||
| 87 | uint32_t param_index = 0; // index into RootSignature::params | ||
| 88 | uint32_t sub_index = 0; // constant index, or 0 = VA lo, 1 = VA hi | ||
| 89 | }; | ||
| 90 | |||
| 91 | struct PackResult { | ||
| 92 | bool ok = false; | ||
| 93 | std::string error; // set when !ok; names the refusing parameter | ||
| 94 | std::array<uint32_t, 32> dwords{}; // valid dwords [0, dword_count) | ||
| 95 | uint32_t dword_count = 0; | ||
| 96 | std::vector<DwordProvenance> provenance; // same length as dword_count | ||
| 97 | uint32_t d3d12_cost = 0; // cost against the 64-dword D3D12 limit | ||
| 98 | }; | ||
| 99 | |||
| 100 | constexpr uint32_t kMaxUserSgprs = 32; | ||
| 101 | constexpr uint32_t kMaxD3d12Cost = 64; | ||
| 102 | |||
| 103 | // Packs root parameters in order. Static samplers consume no dwords. | ||
| 104 | // Fails (ok == false) when the signature needs more than 32 user SGPRs | ||
| 105 | // (spill layout unknown: refuse, do not guess) or exceeds the 64-dword | ||
| 106 | // D3D12 cost limit, or holds more than 64 root parameters. | ||
| 107 | [[nodiscard]] PackResult pack(const RootSignature& sig); | ||
| 108 | |||
| 109 | // D3D12 cost of one parameter against the 64-dword limit. | ||
| 110 | [[nodiscard]] constexpr uint32_t param_cost(ParamType type, uint32_t num_values) noexcept { | ||
| 111 | switch (type) { | ||
| 112 | case ParamType::Constants: | ||
| 113 | return num_values; | ||
| 114 | case ParamType::Descriptor: | ||
| 115 | return 2; | ||
| 116 | case ParamType::Table: | ||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | // Canonical binary codec for caching root signatures (magic "LRS1", | ||
| 123 | // little-endian). This is Leonida's own layout, NOT the Microsoft RTS0 | ||
| 124 | // format: it round-trips pack() inputs byte-exactly and is fully tested on | ||
| 125 | // every CI leg. Never write game blobs in this format and claim otherwise. | ||
| 126 | [[nodiscard]] std::vector<uint8_t> encode_canonical(const RootSignature& sig); | ||
| 127 | |||
| 128 | struct DecodeResult { | ||
| 129 | bool ok = false; | ||
| 130 | std::string error; | ||
| 131 | RootSignature sig; | ||
| 132 | }; | ||
| 133 | |||
| 134 | [[nodiscard]] DecodeResult decode_canonical(const uint8_t* data, size_t size); | ||
| 135 | |||
| 136 | // RTS0 chunk parser (payload layout per the maintainer: offset table). | ||
| 137 | // | ||
| 138 | // The input is a whole DXBC container (what D3D12SerializeVersionedRootSignature | ||
| 139 | // returns: "DXBC" magic, hash, size, chunk table). The parser walks the chunk | ||
| 140 | // table exactly like tools/rdna2spirv.py:input_signature does, finds the | ||
| 141 | // "RTS0" chunk and decodes its payload: version, counts and table offsets, | ||
| 142 | // then a parameter table of {d3d_type, visibility, offset} entries with | ||
| 143 | // payloads at their offsets (d3d types follow D3D12_ROOT_PARAMETER_TYPE | ||
| 144 | // numbering). A bare payload without the DXBC header is also accepted | ||
| 145 | // (unit-test path). Every deviation the oracle test finds is a hard failure | ||
| 146 | // to investigate, not a quirk to absorb. | ||
| 147 | // Returns ok == false with a reason for unknown versions or truncation. | ||
| 148 | [[nodiscard]] DecodeResult parse_rts0(const uint8_t* data, size_t size); | ||
| 149 | |||
| 150 | } // namespace rt::rootsig | ||
diff --git a/runtime/test/descriptor_table_test.cpp b/runtime/test/descriptor_table_test.cpp new file mode 100644 index 000000000..f9c2ba186 --- /dev/null +++ b/runtime/test/descriptor_table_test.cpp | |||
| @@ -0,0 +1,234 @@ | |||
| 1 | // Unit tests for the descriptor hash table (set 0, binding 2 contract). | ||
| 2 | // Tests FNV-1a hashing, T#/S# key generation, probe resolution, dynamic resizing, | ||
| 3 | // GPU buffer serialization, and multithreaded insertion. | ||
| 4 | #include "../src/descriptor_table.h" | ||
| 5 | |||
| 6 | #include <cassert> | ||
| 7 | #include <cstdio> | ||
| 8 | #include <cstring> | ||
| 9 | #include <random> | ||
| 10 | #include <thread> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #define EXPECT(condition) do { \ | ||
| 14 | if (!(condition)) { \ | ||
| 15 | std::fprintf(stderr, "assertion failed: %s at %s:%d\n", #condition, __FILE__, __LINE__); \ | ||
| 16 | return 1; \ | ||
| 17 | } \ | ||
| 18 | } while (0) | ||
| 19 | |||
| 20 | using namespace leonida; | ||
| 21 | |||
| 22 | int test_fnv1a_hash_matches_contract() { | ||
| 23 | // Verified against vk_texture_test.cpp key_hash and rdna2spirv.py desc_probe | ||
| 24 | const std::uint32_t zeros[8] = {0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 25 | std::uint32_t expected_zero_hash = 2166136261u; | ||
| 26 | for (int i = 0; i < 8; ++i) expected_zero_hash = (expected_zero_hash ^ 0) * 16777619u; | ||
| 27 | EXPECT(hash_descriptor_key(zeros) == expected_zero_hash); | ||
| 28 | |||
| 29 | const std::uint32_t sample_key[8] = { | ||
| 30 | 0x12345678, 0x9abcdef0, 0x11223344, 0x55667788, | ||
| 31 | 0x99aabbcc, 0xddeeff00, 0x01020304, 0x05060708 | ||
| 32 | }; | ||
| 33 | std::uint32_t expected_hash = 2166136261u; | ||
| 34 | for (int i = 0; i < 8; ++i) expected_hash = (expected_hash ^ sample_key[i]) * 16777619u; | ||
| 35 | EXPECT(hash_descriptor_key(sample_key) == expected_hash); | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | int test_ssharp_key_padding() { | ||
| 40 | const std::uint32_t ssharp[4] = {0x92, 0xc00000, 0x14500000, 0x0}; | ||
| 41 | const auto key = make_ssharp_key(ssharp); | ||
| 42 | EXPECT(key[0] == 0x92); | ||
| 43 | EXPECT(key[1] == 0xc00000); | ||
| 44 | EXPECT(key[2] == 0x14500000); | ||
| 45 | EXPECT(key[3] == 0x0); | ||
| 46 | EXPECT(key[4] == 0xFFFFFFFFu); | ||
| 47 | EXPECT(key[5] == 0xFFFFFFFFu); | ||
| 48 | EXPECT(key[6] == 0xFFFFFFFFu); | ||
| 49 | EXPECT(key[7] == 0xFFFFFFFFu); | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | int test_basic_insertion_and_lookup() { | ||
| 54 | DescriptorTable table(16); | ||
| 55 | EXPECT(table.capacity() == 16); | ||
| 56 | EXPECT(table.mask() == 15); | ||
| 57 | EXPECT(table.size() == 0); | ||
| 58 | |||
| 59 | const std::uint32_t tsharp[8] = { | ||
| 60 | 0x1000, 0x2000, 0x3000, 0x4000, | ||
| 61 | 0x5000, 0x6000, 0x7000, 0x8000 | ||
| 62 | }; | ||
| 63 | EXPECT(table.lookup(tsharp) == 0); | ||
| 64 | EXPECT(!table.contains(tsharp)); | ||
| 65 | |||
| 66 | EXPECT(table.insert(tsharp, 1)); | ||
| 67 | EXPECT(table.size() == 1); | ||
| 68 | EXPECT(table.contains(tsharp)); | ||
| 69 | EXPECT(table.lookup(tsharp) == 1); | ||
| 70 | |||
| 71 | // Duplicate key updates in place | ||
| 72 | EXPECT(table.insert(tsharp, 42)); | ||
| 73 | EXPECT(table.size() == 1); | ||
| 74 | EXPECT(table.lookup(tsharp) == 42); | ||
| 75 | |||
| 76 | // S# key | ||
| 77 | const std::uint32_t ssharp[4] = {0xa, 0xb, 0xc, 0xd}; | ||
| 78 | const auto skey = make_ssharp_key(ssharp); | ||
| 79 | EXPECT(table.insert(skey, 7)); | ||
| 80 | EXPECT(table.size() == 2); | ||
| 81 | EXPECT(table.lookup(skey) == 7); | ||
| 82 | EXPECT(table.lookup(tsharp) == 42); | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | |||
| 86 | int test_load_factor_and_growth() { | ||
| 87 | // Start with 16 slots. Load factor threshold 50% means max 8 entries before growth. | ||
| 88 | DescriptorTable table(16); | ||
| 89 | EXPECT(table.capacity() == 16); | ||
| 90 | |||
| 91 | std::vector<DescriptorKey> keys; | ||
| 92 | for (std::uint32_t i = 1; i <= 20; ++i) { | ||
| 93 | std::uint32_t k[8] = {i, i * 2, i * 3, i * 4, i * 5, i * 6, i * 7, i * 8}; | ||
| 94 | keys.push_back(make_tsharp_key(k)); | ||
| 95 | EXPECT(table.insert(keys.back(), i + 100)); | ||
| 96 | } | ||
| 97 | |||
| 98 | EXPECT(table.size() == 20); | ||
| 99 | EXPECT(table.capacity() >= 64); // Doubled from 16 -> 32 -> 64 | ||
| 100 | EXPECT(table.mask() == table.capacity() - 1); | ||
| 101 | |||
| 102 | // Verify all 20 keys resolve to their exact indices after growths | ||
| 103 | for (std::uint32_t i = 1; i <= 20; ++i) { | ||
| 104 | EXPECT(table.lookup(keys[i - 1]) == i + 100); | ||
| 105 | } | ||
| 106 | return 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | int test_probe_collision_resolution() { | ||
| 110 | DescriptorTable table(64); | ||
| 111 | |||
| 112 | // Create 4 distinct keys that share the exact same hash | ||
| 113 | // We can forge keys that share hash or test linear collision handling | ||
| 114 | std::uint32_t k1[8] = {1, 0, 0, 0, 0, 0, 0, 0}; | ||
| 115 | std::uint32_t k2[8] = {2, 0, 0, 0, 0, 0, 0, 0}; | ||
| 116 | std::uint32_t k3[8] = {3, 0, 0, 0, 0, 0, 0, 0}; | ||
| 117 | |||
| 118 | EXPECT(table.insert(k1, 10)); | ||
| 119 | EXPECT(table.insert(k2, 20)); | ||
| 120 | EXPECT(table.insert(k3, 30)); | ||
| 121 | |||
| 122 | EXPECT(table.lookup(k1) == 10); | ||
| 123 | EXPECT(table.lookup(k2) == 20); | ||
| 124 | EXPECT(table.lookup(k3) == 30); | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | int test_gpu_buffer_serialization() { | ||
| 129 | DescriptorTable table(16); | ||
| 130 | const std::uint32_t t1[8] = {10, 20, 30, 40, 50, 60, 70, 80}; | ||
| 131 | const std::uint32_t s1[4] = {1, 2, 3, 4}; | ||
| 132 | const auto skey = make_ssharp_key(s1); | ||
| 133 | |||
| 134 | EXPECT(table.insert(t1, 5)); | ||
| 135 | EXPECT(table.insert(skey, 9)); | ||
| 136 | |||
| 137 | const auto gpu_buf = table.build_gpu_buffer(false); | ||
| 138 | EXPECT(gpu_buf.size() == 1 + 16 * 10); | ||
| 139 | EXPECT(gpu_buf[0] == 15); // mask | ||
| 140 | |||
| 141 | // Verify lookup by traversing the generated buffer using the exact shader logic | ||
| 142 | const auto shader_lookup = [&](const std::uint32_t key[8]) -> std::uint32_t { | ||
| 143 | const std::uint32_t mask = gpu_buf[0]; | ||
| 144 | const std::uint32_t h = hash_descriptor_key(key); | ||
| 145 | for (std::uint32_t k = 0; k < 8; ++k) { | ||
| 146 | const std::uint32_t slot = (h + k) & mask; | ||
| 147 | const std::size_t base = 1 + slot * 10; | ||
| 148 | bool match = true; | ||
| 149 | for (std::size_t j = 0; j < 8; ++j) { | ||
| 150 | if (gpu_buf[base + j] != key[j]) { | ||
| 151 | match = false; | ||
| 152 | break; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | if (match) { | ||
| 156 | return gpu_buf[base + 8]; // index | ||
| 157 | } | ||
| 158 | } | ||
| 159 | return 0; | ||
| 160 | }; | ||
| 161 | |||
| 162 | EXPECT(shader_lookup(t1) == 5); | ||
| 163 | EXPECT(shader_lookup(skey.data()) == 9); | ||
| 164 | |||
| 165 | const std::uint32_t missing[8] = {99, 99, 99, 99, 99, 99, 99, 99}; | ||
| 166 | EXPECT(shader_lookup(missing) == 0); | ||
| 167 | |||
| 168 | // Test write_to_buffer | ||
| 169 | std::vector<std::uint32_t> raw_copy(gpu_buf.size()); | ||
| 170 | table.write_to_buffer(raw_copy.data(), true); | ||
| 171 | EXPECT(raw_copy[0] == 15); | ||
| 172 | for (std::size_t s = 0; s < 16; ++s) { | ||
| 173 | EXPECT(raw_copy[1 + s * 10 + 9] == 0); // clear_pad = true | ||
| 174 | } | ||
| 175 | return 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | int test_multithreaded_insert_and_lookup() { | ||
| 179 | DescriptorTable table(32); | ||
| 180 | constexpr int kThreads = 4; | ||
| 181 | constexpr int kPerThread = 50; | ||
| 182 | |||
| 183 | std::vector<std::thread> workers; | ||
| 184 | for (int t = 0; t < kThreads; ++t) { | ||
| 185 | workers.emplace_back([&table, t] { | ||
| 186 | for (int i = 0; i < kPerThread; ++i) { | ||
| 187 | const std::uint32_t id = static_cast<std::uint32_t>(t * kPerThread + i + 1); | ||
| 188 | std::uint32_t key[8] = {id, id * 3, id * 7, id * 11, id * 13, id * 17, id * 19, id * 23}; | ||
| 189 | table.insert(key, id); | ||
| 190 | } | ||
| 191 | }); | ||
| 192 | } | ||
| 193 | |||
| 194 | for (auto& w : workers) { | ||
| 195 | w.join(); | ||
| 196 | } | ||
| 197 | |||
| 198 | EXPECT(table.size() == kThreads * kPerThread); | ||
| 199 | for (int t = 0; t < kThreads; ++t) { | ||
| 200 | for (int i = 0; i < kPerThread; ++i) { | ||
| 201 | const std::uint32_t id = static_cast<std::uint32_t>(t * kPerThread + i + 1); | ||
| 202 | std::uint32_t key[8] = {id, id * 3, id * 7, id * 11, id * 13, id * 17, id * 19, id * 23}; | ||
| 203 | EXPECT(table.lookup(key) == id); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | return 0; | ||
| 207 | } | ||
| 208 | |||
| 209 | int test_clear_resets_table() { | ||
| 210 | DescriptorTable table(16); | ||
| 211 | std::uint32_t k[8] = {1, 2, 3, 4, 5, 6, 7, 8}; | ||
| 212 | table.insert(k, 100); | ||
| 213 | EXPECT(table.size() == 1); | ||
| 214 | EXPECT(table.lookup(k) == 100); | ||
| 215 | |||
| 216 | table.clear(); | ||
| 217 | EXPECT(table.size() == 0); | ||
| 218 | EXPECT(table.lookup(k) == 0); | ||
| 219 | return 0; | ||
| 220 | } | ||
| 221 | |||
| 222 | int main() { | ||
| 223 | EXPECT(test_fnv1a_hash_matches_contract() == 0); | ||
| 224 | EXPECT(test_ssharp_key_padding() == 0); | ||
| 225 | EXPECT(test_basic_insertion_and_lookup() == 0); | ||
| 226 | EXPECT(test_load_factor_and_growth() == 0); | ||
| 227 | EXPECT(test_probe_collision_resolution() == 0); | ||
| 228 | EXPECT(test_gpu_buffer_serialization() == 0); | ||
| 229 | EXPECT(test_multithreaded_insert_and_lookup() == 0); | ||
| 230 | EXPECT(test_clear_resets_table() == 0); | ||
| 231 | |||
| 232 | std::printf("descriptor_table_test: all tests passed (0 failures)\n"); | ||
| 233 | return 0; | ||
| 234 | } | ||
diff --git a/runtime/test/root_signature_oracle_test.cpp b/runtime/test/root_signature_oracle_test.cpp new file mode 100644 index 000000000..d60dfae3a --- /dev/null +++ b/runtime/test/root_signature_oracle_test.cpp | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | // Windows oracle for the RTS0 parser hypothesis (needs a real PC d3d12.lib). | ||
| 2 | // | ||
| 3 | // Builds synthetic versioned root-signature descriptors by hand (no game | ||
| 4 | // data), serializes each with Microsoft's D3D12SerializeVersionedRootSignature, | ||
| 5 | // and requires rt::rootsig::parse_rts0 to recover the same model. If the | ||
| 6 | // hypothesis layout in root_signature.cpp ever disagrees with the real | ||
| 7 | // serializer, this fails loudly here instead of corrupting draws silently. | ||
| 8 | // NOT portable: runs on Windows CI only, like root_sig_test.cpp. | ||
| 9 | #include <windows.h> | ||
| 10 | #include <d3d12.h> | ||
| 11 | |||
| 12 | #include <cstdint> | ||
| 13 | #include <cstdio> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | #include "root_signature.h" | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | int g_failures = 0; | ||
| 21 | |||
| 22 | void check(bool condition, const char* what) { | ||
| 23 | std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); | ||
| 24 | if (!condition) ++g_failures; | ||
| 25 | } | ||
| 26 | |||
| 27 | struct Blobs { | ||
| 28 | ID3DBlob* blob = nullptr; | ||
| 29 | ID3DBlob* error = nullptr; | ||
| 30 | ~Blobs() { | ||
| 31 | if (blob) blob->Release(); | ||
| 32 | if (error) error->Release(); | ||
| 33 | } | ||
| 34 | }; | ||
| 35 | |||
| 36 | D3D12_VERSIONED_ROOT_SIGNATURE_DESC make_desc( | ||
| 37 | const D3D12_ROOT_PARAMETER1* params, uint32_t nparams) { | ||
| 38 | D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc{}; | ||
| 39 | desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1; | ||
| 40 | desc.Desc_1_1.NumParameters = nparams; | ||
| 41 | desc.Desc_1_1.pParameters = params; | ||
| 42 | desc.Desc_1_1.NumStaticSamplers = 0; | ||
| 43 | desc.Desc_1_1.pStaticSamplers = nullptr; | ||
| 44 | desc.Desc_1_1.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE; | ||
| 45 | return desc; | ||
| 46 | } | ||
| 47 | |||
| 48 | bool parse_blob(ID3DBlob* blob, rt::rootsig::RootSignature& model) { | ||
| 49 | const auto* bytes = static_cast<const uint8_t*>(blob->GetBufferPointer()); | ||
| 50 | const size_t size = blob->GetBufferSize(); | ||
| 51 | // Diagnostics first: if the layout hypothesis is wrong, the CI log shows | ||
| 52 | // the real bytes instead of just a crash or a bare failure. | ||
| 53 | std::printf("oracle blob: %zu bytes: ", size); | ||
| 54 | for (size_t i = 0; i < 32 && i < size; ++i) { | ||
| 55 | std::printf("%02x", bytes[i]); | ||
| 56 | } | ||
| 57 | std::printf("\n"); | ||
| 58 | std::fflush(stdout); | ||
| 59 | const rt::rootsig::DecodeResult d = rt::rootsig::parse_rts0(bytes, size); | ||
| 60 | if (!d.ok) { | ||
| 61 | std::printf("oracle parse failed: %s\n", d.error.c_str()); | ||
| 62 | return false; | ||
| 63 | } | ||
| 64 | model = d.sig; | ||
| 65 | return true; | ||
| 66 | } | ||
| 67 | |||
| 68 | } // namespace | ||
| 69 | |||
| 70 | int main() { | ||
| 71 | using namespace rt::rootsig; | ||
| 72 | |||
| 73 | // 4 constants (VS-visible) + 1 CBV: parses back to the same model and | ||
| 74 | // packs to 6 dwords, exactly like the portable packer says for 1.1. | ||
| 75 | D3D12_ROOT_PARAMETER1 params[2]{}; | ||
| 76 | params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; | ||
| 77 | params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX; | ||
| 78 | params[0].Constants.ShaderRegister = 0; | ||
| 79 | params[0].Constants.RegisterSpace = 0; | ||
| 80 | params[0].Constants.Num32BitValues = 4; | ||
| 81 | params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; | ||
| 82 | params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; | ||
| 83 | params[1].Descriptor.ShaderRegister = 2; | ||
| 84 | params[1].Descriptor.RegisterSpace = 0; | ||
| 85 | params[1].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE; | ||
| 86 | |||
| 87 | const D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc = make_desc(params, 2); | ||
| 88 | Blobs blobs; | ||
| 89 | const HRESULT hr = | ||
| 90 | D3D12SerializeVersionedRootSignature(&desc, &blobs.blob, &blobs.error); | ||
| 91 | check(hr == S_OK, "Microsoft serializer accepts the synthetic desc"); | ||
| 92 | if (hr != S_OK) { | ||
| 93 | if (blobs.error != nullptr) { | ||
| 94 | std::printf("serializer error: %s\n", | ||
| 95 | static_cast<const char*>(blobs.error->GetBufferPointer())); | ||
| 96 | } | ||
| 97 | std::printf("%d FAILURES\n", g_failures + 1); | ||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | RootSignature model; | ||
| 102 | check(parse_blob(blobs.blob, model), "oracle blob parses"); | ||
| 103 | if (g_failures != 0) { | ||
| 104 | // Never index into a model that failed to parse (that segfaulted CI). | ||
| 105 | std::printf("%d FAILURES\n", g_failures); | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | check(model.params.size() == 2, "oracle model has 2 params"); | ||
| 109 | check(model.params[0].type == ParamType::Constants && | ||
| 110 | model.params[0].num_values == 4 && | ||
| 111 | model.params[0].visibility == Visibility::Vertex, | ||
| 112 | "oracle constants match"); | ||
| 113 | check(model.params[1].type == ParamType::Descriptor, | ||
| 114 | "oracle CBV matches"); | ||
| 115 | const PackResult packed = pack(model); | ||
| 116 | check(packed.ok && packed.dword_count == 6, "oracle model packs to 6 dwords"); | ||
| 117 | |||
| 118 | if (g_failures != 0) std::printf("%d FAILURES\n", g_failures); | ||
| 119 | return g_failures == 0 ? 0 : 1; | ||
| 120 | } | ||
diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp new file mode 100644 index 000000000..92ef8347f --- /dev/null +++ b/runtime/test/root_signature_test.cpp | |||
| @@ -0,0 +1,221 @@ | |||
| 1 | // Portable tests for runtime/src/root_signature.{h,cpp} (no windows.h). | ||
| 2 | // | ||
| 3 | // Own main + PASS/FAIL lines like the other runtime tests; returns nonzero | ||
| 4 | // on any failure so CTest gates on it. Everything here is hand-built: no | ||
| 5 | // game data, no DXBC, no serializer needed. | ||
| 6 | #include <cstdint> | ||
| 7 | #include <cstdio> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "root_signature.h" | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | int g_failures = 0; | ||
| 15 | |||
| 16 | void check(bool condition, const char* what) { | ||
| 17 | std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); | ||
| 18 | if (!condition) ++g_failures; | ||
| 19 | } | ||
| 20 | |||
| 21 | rt::rootsig::RootParameter consts(uint32_t n) { | ||
| 22 | rt::rootsig::RootParameter p; | ||
| 23 | p.type = rt::rootsig::ParamType::Constants; | ||
| 24 | p.num_values = n; | ||
| 25 | return p; | ||
| 26 | } | ||
| 27 | |||
| 28 | rt::rootsig::RootParameter view() { | ||
| 29 | rt::rootsig::RootParameter p; | ||
| 30 | p.type = rt::rootsig::ParamType::Descriptor; | ||
| 31 | return p; | ||
| 32 | } | ||
| 33 | |||
| 34 | rt::rootsig::RootParameter table() { | ||
| 35 | rt::rootsig::RootParameter p; | ||
| 36 | p.type = rt::rootsig::ParamType::Table; | ||
| 37 | p.ranges.push_back({0, 8}); | ||
| 38 | return p; | ||
| 39 | } | ||
| 40 | |||
| 41 | void put_u32(std::vector<uint8_t>& out, uint32_t v) { | ||
| 42 | out.push_back(static_cast<uint8_t>(v)); | ||
| 43 | out.push_back(static_cast<uint8_t>(v >> 8)); | ||
| 44 | out.push_back(static_cast<uint8_t>(v >> 16)); | ||
| 45 | out.push_back(static_cast<uint8_t>(v >> 24)); | ||
| 46 | } | ||
| 47 | |||
| 48 | // Hand-built RTS0 payload in the offset-table layout the maintainer | ||
| 49 | // described: header, parameter table, then payloads at their offsets. | ||
| 50 | std::vector<uint8_t> build_payload() { | ||
| 51 | std::vector<uint8_t> p; | ||
| 52 | put_u32(p, 2); // version 1.1 | ||
| 53 | put_u32(p, 2); // 2 params | ||
| 54 | put_u32(p, 24); // params table offset | ||
| 55 | put_u32(p, 0); // 0 samplers | ||
| 56 | put_u32(p, 0); // samplers offset | ||
| 57 | put_u32(p, 0x03000000u); // Xbox flags | ||
| 58 | // entry 0: constants at 48; entry 1: CBV descriptor at 60. | ||
| 59 | put_u32(p, 1); | ||
| 60 | put_u32(p, 0); | ||
| 61 | put_u32(p, 48); | ||
| 62 | put_u32(p, 2); | ||
| 63 | put_u32(p, 4); | ||
| 64 | put_u32(p, 60); | ||
| 65 | put_u32(p, 7); // register (ignored by packer) | ||
| 66 | put_u32(p, 0); // space | ||
| 67 | put_u32(p, 3); // 3 values | ||
| 68 | put_u32(p, 0); // register | ||
| 69 | put_u32(p, 0); // space | ||
| 70 | put_u32(p, 0); // 1.1 descriptor flags | ||
| 71 | return p; | ||
| 72 | } | ||
| 73 | |||
| 74 | } // namespace | ||
| 75 | |||
| 76 | int main() { | ||
| 77 | using namespace rt::rootsig; | ||
| 78 | |||
| 79 | // Mixed signature: 4 constants + CBV + table + 1 static sampler. | ||
| 80 | { | ||
| 81 | RootSignature sig; | ||
| 82 | sig.params = {consts(4), view(), table()}; | ||
| 83 | StaticSampler s; | ||
| 84 | s.shader_register = 3; | ||
| 85 | sig.static_samplers = {s}; | ||
| 86 | const PackResult r = pack(sig); | ||
| 87 | check(r.ok, "mixed signature packs"); | ||
| 88 | check(r.dword_count == 8, "mixed signature uses 8 dwords"); | ||
| 89 | check(r.d3d12_cost == 4 + 2 + 1, "mixed signature costs 7"); | ||
| 90 | check(r.provenance.size() == 8 && r.provenance[0].param_index == 0 && | ||
| 91 | r.provenance[0].sub_index == 0 && r.provenance[4].param_index == 1 && | ||
| 92 | r.provenance[4].sub_index == 0 && r.provenance[5].sub_index == 1 && | ||
| 93 | r.provenance[6].param_index == 2, | ||
| 94 | "provenance tracks param and sub-dword"); | ||
| 95 | } | ||
| 96 | |||
| 97 | // Exactly 32 dwords still fits. | ||
| 98 | { | ||
| 99 | RootSignature sig; | ||
| 100 | for (int i = 0; i < 16; ++i) sig.params.push_back(view()); | ||
| 101 | const PackResult r = pack(sig); | ||
| 102 | check(r.ok && r.dword_count == 32, "16 views fill 32 dwords"); | ||
| 103 | } | ||
| 104 | |||
| 105 | // 34 dwords: spill is refused loudly, never truncated. | ||
| 106 | { | ||
| 107 | RootSignature sig; | ||
| 108 | for (int i = 0; i < 17; ++i) sig.params.push_back(view()); | ||
| 109 | const PackResult r = pack(sig); | ||
| 110 | check(!r.ok && r.error.find("spill") != std::string::npos, | ||
| 111 | "17 views refused as spill"); | ||
| 112 | } | ||
| 113 | |||
| 114 | // 65 constants exceed the 64-dword D3D12 cost limit. | ||
| 115 | { | ||
| 116 | RootSignature sig; | ||
| 117 | sig.params = {consts(65)}; | ||
| 118 | const PackResult r = pack(sig); | ||
| 119 | check(!r.ok && r.error.find("64-dword") != std::string::npos, | ||
| 120 | "65 constants refused by cost limit"); | ||
| 121 | } | ||
| 122 | |||
| 123 | // Canonical codec round-trips byte-exactly, sampler included. | ||
| 124 | { | ||
| 125 | RootSignature sig; | ||
| 126 | sig.version = Version::V1_0; | ||
| 127 | sig.params = {consts(2), table()}; | ||
| 128 | sig.flags = 0x03000000u; // Xbox high byte survives the model | ||
| 129 | StaticSampler s; | ||
| 130 | s.filter = 21; | ||
| 131 | s.mip_lod_bias = 0.5f; | ||
| 132 | sig.static_samplers = {s}; | ||
| 133 | const std::vector<uint8_t> bytes = encode_canonical(sig); | ||
| 134 | const DecodeResult d = decode_canonical(bytes.data(), bytes.size()); | ||
| 135 | check(d.ok, "canonical round-trip decodes"); | ||
| 136 | check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 2 && | ||
| 137 | d.sig.params[1].ranges.size() == 1 && | ||
| 138 | d.sig.static_samplers.size() == 1 && d.sig.flags == 0x03000000u && | ||
| 139 | d.sig.static_samplers[0].filter == 21, | ||
| 140 | "canonical round-trip preserves model"); | ||
| 141 | } | ||
| 142 | |||
| 143 | // Canonical decoder controls: bad magic, truncation, trailing bytes. | ||
| 144 | { | ||
| 145 | const std::vector<uint8_t> bad = {0, 1, 2, 3}; | ||
| 146 | check(!decode_canonical(bad.data(), bad.size()).ok, "bad magic rejected"); | ||
| 147 | RootSignature sig; | ||
| 148 | sig.params = {consts(1)}; | ||
| 149 | const std::vector<uint8_t> bytes = encode_canonical(sig); | ||
| 150 | check(!decode_canonical(bytes.data(), bytes.size() - 1).ok, | ||
| 151 | "truncated canonical rejected"); | ||
| 152 | std::vector<uint8_t> trailed = bytes; | ||
| 153 | trailed.push_back(0); | ||
| 154 | check(!decode_canonical(trailed.data(), trailed.size()).ok, | ||
| 155 | "trailing bytes rejected"); | ||
| 156 | } | ||
| 157 | |||
| 158 | // RTS0 hypothesis: a hand-built blob per the documented layout parses. | ||
| 159 | // Hand-built RTS0 payload in the offset-table layout the maintainer | ||
| 160 | // described: header, parameter table, then payloads at their offsets. | ||
| 161 | { | ||
| 162 | const std::vector<uint8_t> payload = build_payload(); | ||
| 163 | const DecodeResult d = parse_rts0(payload.data(), payload.size()); | ||
| 164 | check(d.ok, "hand-built RTS0 parses"); | ||
| 165 | check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3 && | ||
| 166 | d.sig.flags == 0x03000000u, | ||
| 167 | "RTS0 model matches hand-built blob"); | ||
| 168 | const PackResult r = pack(d.sig); | ||
| 169 | check(r.ok && r.dword_count == 3 + 2, "parsed RTS0 packs to 5 dwords"); | ||
| 170 | } | ||
| 171 | |||
| 172 | // RTS0 controls: unknown version, wild offsets and truncation fail loudly. | ||
| 173 | { | ||
| 174 | std::vector<uint8_t> blob; | ||
| 175 | put_u32(blob, 9); // unknown version | ||
| 176 | put_u32(blob, 0); | ||
| 177 | put_u32(blob, 0); | ||
| 178 | put_u32(blob, 0); | ||
| 179 | put_u32(blob, 0); | ||
| 180 | put_u32(blob, 0); | ||
| 181 | const DecodeResult d = parse_rts0(blob.data(), blob.size()); | ||
| 182 | check(!d.ok, "unknown RTS0 version rejected"); | ||
| 183 | std::vector<uint8_t> cut = {1, 0}; | ||
| 184 | check(!parse_rts0(cut.data(), cut.size()).ok, "truncated RTS0 rejected"); | ||
| 185 | std::vector<uint8_t> wild = build_payload(); | ||
| 186 | // Corrupt the params offset to point past the end. | ||
| 187 | wild[8] = 0xFF; | ||
| 188 | wild[9] = 0xFF; | ||
| 189 | wild[10] = 0xFF; | ||
| 190 | wild[11] = 0xFF; | ||
| 191 | check(!parse_rts0(wild.data(), wild.size()).ok, "wild offset rejected"); | ||
| 192 | } | ||
| 193 | |||
| 194 | // DXBC container walk: a hand-built container with one RTS0 chunk parses | ||
| 195 | // through the chunk table (same path the MS serializer blob takes). | ||
| 196 | { | ||
| 197 | const std::vector<uint8_t> payload = build_payload(); | ||
| 198 | std::vector<uint8_t> blob; | ||
| 199 | blob.insert(blob.end(), {'D', 'X', 'B', 'C'}); | ||
| 200 | blob.insert(blob.end(), 16, 0); // hash | ||
| 201 | put_u32(blob, 1); // version | ||
| 202 | put_u32(blob, 0); // file size (patched below) | ||
| 203 | put_u32(blob, 1); // 1 chunk | ||
| 204 | put_u32(blob, 36); // chunk offset: 4 magic + 16 hash + 4 ver + 4 size + 4 count + 4 offset | ||
| 205 | blob.insert(blob.end(), {'R', 'T', 'S', '0'}); | ||
| 206 | put_u32(blob, static_cast<uint32_t>(payload.size())); | ||
| 207 | blob.insert(blob.end(), payload.begin(), payload.end()); | ||
| 208 | const uint32_t total = static_cast<uint32_t>(blob.size()); | ||
| 209 | blob[24] = static_cast<uint8_t>(total); | ||
| 210 | blob[25] = static_cast<uint8_t>(total >> 8); | ||
| 211 | blob[26] = static_cast<uint8_t>(total >> 16); | ||
| 212 | blob[27] = static_cast<uint8_t>(total >> 24); | ||
| 213 | const DecodeResult d = parse_rts0(blob.data(), blob.size()); | ||
| 214 | check(d.ok, "DXBC container walk finds RTS0"); | ||
| 215 | check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3, | ||
| 216 | "walked RTS0 model matches payload"); | ||
| 217 | } | ||
| 218 | |||
| 219 | if (g_failures != 0) std::printf("%d FAILURES\n", g_failures); | ||
| 220 | return g_failures == 0 ? 0 : 1; | ||
| 221 | } | ||
diff --git a/runtime/test/shaders/build.py b/runtime/test/shaders/build.py index 180d48ec5..09730a2b0 100644 --- a/runtime/test/shaders/build.py +++ b/runtime/test/shaders/build.py | |||
| @@ -3,8 +3,9 @@ | |||
| 3 | python3 -I runtime/test/shaders/build.py <out dir> [name ...] | 3 | python3 -I runtime/test/shaders/build.py <out dir> [name ...] |
| 4 | 4 | ||
| 5 | Each .asm is RDNA2 assembly. It is assembled with llvm-mc (LLVM 21+ with the AMDGPU target; set LLVM_MC to its | 5 | Each .asm is RDNA2 assembly. It is assembled with llvm-mc (LLVM 21+ with the AMDGPU target; set LLVM_MC to its |
| 6 | path, as for the shaders stage). Listings, synthetic FSSM headers and DXBC containers are staged in a temporary | 6 | path or to a whole command, as for the shaders stage). Listings, synthetic FSSM headers and DXBC containers are |
| 7 | directory. After validation, the build writes <out>/spirv/<name>.spv and the translator metadata. | 7 | staged in a temporary directory. After validation, the build writes <out>/spirv/<name>.spv and the translator |
| 8 | metadata. | ||
| 8 | 9 | ||
| 9 | Compute shaders can override the default header with leading "; group: X,Y,Z" and "; rsrc2: 0x..." comments. | 10 | Compute shaders can override the default header with leading "; group: X,Y,Z" and "; rsrc2: 0x..." comments. |
| 10 | The stage comes from the name's prefix. Mesh and geometry shaders (ms_, gs_) also need a DXBC container with | 11 | The stage comes from the name's prefix. Mesh and geometry shaders (ms_, gs_) also need a DXBC container with |
| @@ -20,10 +21,15 @@ import tempfile | |||
| 20 | from pathlib import Path | 21 | from pathlib import Path |
| 21 | 22 | ||
| 22 | HERE = os.path.dirname(os.path.abspath(__file__)) | 23 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 23 | TRANSLATOR = os.path.join(HERE, "..", "..", "..", "tools", "rdna2spirv.py") | 24 | TOOLS = os.path.join(HERE, "..", "..", "..", "tools") |
| 24 | LLVM_MC = os.environ.get("LLVM_MC", "llvm-mc") | 25 | TRANSLATOR = os.path.join(TOOLS, "rdna2spirv.py") |
| 26 | sys.path.insert(0, TOOLS) | ||
| 27 | import mc_command # noqa: E402 | ||
| 28 | |||
| 29 | MC = mc_command.mc_command() | ||
| 30 | RSRC2, GROUP = 0x8088, (64, 1, 1) | ||
| 25 | sys.path.insert(0, HERE) # this script is also run with python -I | 31 | sys.path.insert(0, HERE) # this script is also run with python -I |
| 26 | from compute_fixture import compute_header | 32 | from compute_fixture import compute_header # noqa: E402 |
| 27 | 33 | ||
| 28 | 34 | ||
| 29 | def asm_mattr(path): | 35 | def asm_mattr(path): |
| @@ -45,12 +51,14 @@ def translate_tests(work, names): | |||
| 45 | fssm.mkdir() | 51 | fssm.mkdir() |
| 46 | for n in names: | 52 | for n in names: |
| 47 | asm_path = os.path.join(HERE, n + ".asm") | 53 | asm_path = os.path.join(HERE, n + ".asm") |
| 54 | raw = Path(asm_path).read_text(encoding="utf-8") | ||
| 48 | mattr = asm_mattr(asm_path) | 55 | mattr = asm_mattr(asm_path) |
| 49 | cmd = [LLVM_MC, "-triple=amdgcn--amdpal", "-mcpu=gfx1013"] | 56 | cmd = MC + ["-triple=amdgcn--amdpal", "-mcpu=gfx1013"] |
| 50 | if mattr: | 57 | if mattr: |
| 51 | cmd.append(f"-mattr={mattr}") | 58 | cmd.append(f"-mattr={mattr}") |
| 52 | cmd += ["-show-encoding", asm_path] | 59 | cmd.append("-show-encoding") |
| 53 | r = subprocess.run(cmd, capture_output=True, text=True) | 60 | # Feed the text on stdin: an LLVM_MC command that runs in WSL cannot open a Windows path argument. |
| 61 | r = subprocess.run(cmd, input=raw, capture_output=True, text=True) | ||
| 54 | if r.returncode or "error:" in r.stderr: | 62 | if r.returncode or "error:" in r.stderr: |
| 55 | sys.exit(f"{n}.asm did not assemble:\n{r.stderr}") | 63 | sys.exit(f"{n}.asm did not assemble:\n{r.stderr}") |
| 56 | # rdna2spirv.py rejects assembler directives such as the "\t.text" llvm-mc prints first. | 64 | # rdna2spirv.py rejects assembler directives such as the "\t.text" llvm-mc prints first. |
diff --git a/runtime/test/shaders/check.py b/runtime/test/shaders/check.py index 15c2a0c60..75f9a2d50 100644 --- a/runtime/test/shaders/check.py +++ b/runtime/test/shaders/check.py | |||
| @@ -29,7 +29,9 @@ UNSUPPORTED = 77 # a test tool exits with | |||
| 29 | COMPUTE = [("cs_lds", "4", "mirror64"), ("cs_exec64", "1", "exec64"), ("cs_bvh", "1", "bvh"), | 29 | COMPUTE = [("cs_lds", "4", "mirror64"), ("cs_exec64", "1", "exec64"), ("cs_bvh", "1", "bvh"), |
| 30 | ("cs_workgroup_yz", "2x3x4", "workgroup_yz")] | 30 | ("cs_workgroup_yz", "2x3x4", "workgroup_yz")] |
| 31 | CS_STORE = [0x400080ff, 0xe00003ff, 0x3c004000, 0xeeeeeeee, 0x40003c00, 0xbc003800] | 31 | CS_STORE = [0x400080ff, 0xe00003ff, 0x3c004000, 0xeeeeeeee, 0x40003c00, 0xbc003800] |
| 32 | CS_DUMPS = [("cs_store", CS_STORE), ("cs_wave64_ballot", [32, 16, 24, 40, 8, 48]), | 32 | CS_SMEM = [0x5eed0024, 0x5eed0028, 0x5eed002c, 0x5eed0030, 0x5eed0010, 0x5eed0014, 0x5eed0018, 0x5eed001c] |
| 33 | WAVE32_DUMPS = ("cs_store", "cs_smem_soffset_imm") | ||
| 34 | CS_DUMPS = [("cs_store", CS_STORE), ("cs_smem_soffset_imm", CS_SMEM), ("cs_wave64_ballot", [32, 16, 24, 40, 8, 48]), | ||
| 33 | ("cs_wm_half_ops", [32, 16, 16, 0, 16]), ("cs_wm_half_cmp", [1, 0, 1, 1]), | 35 | ("cs_wm_half_ops", [32, 16, 16, 0, 16]), ("cs_wm_half_cmp", [1, 0, 1, 1]), |
| 34 | ("cs_wm_bfm64", [16, 8, 8, 40]), ("cs_wm_scc_rfl", [55, 48])] | 36 | ("cs_wm_bfm64", [16, 8, 8, 40]), ("cs_wm_scc_rfl", [55, 48])] |
| 35 | # Cross-half wave64 tests (x = cross-half): exact results need 64 lanes, or an emulation of them on narrower subgroups. | 37 | # Cross-half wave64 tests (x = cross-half): exact results need 64 lanes, or an emulation of them on narrower subgroups. |
| @@ -229,7 +231,7 @@ def main(): | |||
| 229 | require_wave64=not W64_EMULATED))) | 231 | require_wave64=not W64_EMULATED))) |
| 230 | for name, want in CS_DUMPS: # all but cs_store need a 64-wide subgroup unless wave64 is emulated | 232 | for name, want in CS_DUMPS: # all but cs_store need a 64-wide subgroup unless wave64 is emulated |
| 231 | results.append((name, compute_test(tools, spirv, name, "1", "dump", | 233 | results.append((name, compute_test(tools, spirv, name, "1", "dump", |
| 232 | require_wave64=name != "cs_store" and not W64_EMULATED, | 234 | require_wave64=name not in WAVE32_DUMPS and not W64_EMULATED, |
| 233 | expected_words=want))) | 235 | expected_words=want))) |
| 234 | for name, want in W64X_DUMPS: # without --require-wave64 when LEONIDA_W64_EMULATE=1 | 236 | for name, want in W64X_DUMPS: # without --require-wave64 when LEONIDA_W64_EMULATE=1 |
| 235 | results.append((name, compute_test(tools, spirv, name, "1", "dump", require_wave64=not W64_EMULATED, | 237 | results.append((name, compute_test(tools, spirv, name, "1", "dump", require_wave64=not W64_EMULATED, |
diff --git a/runtime/test/shaders/cs_smem_soffset_imm.asm b/runtime/test/shaders/cs_smem_soffset_imm.asm new file mode 100644 index 000000000..399de1860 --- /dev/null +++ b/runtime/test/shaders/cs_smem_soffset_imm.asm | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | ; SMEM loads that carry a register offset AND an immediate (llvm-mc: "sOFF offset:IMM"): address = base + soffset + imm. | ||
| 2 | ; Measured on an AMD Radeon RX 6800 (gfx1030) by running s_buffer_load_dword/x2/x4/x8 as a raw compute kernel: | ||
| 3 | ; soffset 8 + imm 4 reads the dword at +12. s_load_dword shares the SMEM encoding; applying the rule to it is an inference. | ||
| 4 | ; Thread 0 seeds heap bytes 0x10..0x30 with 0x5eed0000 + byte offset, then loads with s8 = 0x10: | ||
| 5 | ; s_buffer_load_dword s9, s[0:3], s8 offset:0x14 -> byte 0x24 (imm dropped: 0x10, s8 dropped: 0x14) | ||
| 6 | ; s_load_dword s10, s[4:5], s8 offset:0x18 -> byte 0x28 (imm dropped: 0x10, s8 dropped: 0x18) | ||
| 7 | ; s_buffer_load_dwordx2 s[12:13], s[0:3], s8 offset:0x1c -> bytes 0x2c, 0x30 (imm dropped: 0x10, 0x14; s8 dropped: 0x1c, 0x20) | ||
| 8 | ; and writes s9, s10, s12, s13 to out[0..3]; out[4..7] keep the seeds of bytes 0x10..0x1c. | ||
| 9 | ; All offsets are multiples of 4 and inside the buffer, so alignment and num_records play no part. | ||
| 10 | ; Run: vk_compute_test cs_smem_soffset_imm.spv 1 dump -> out[0..7] = 0x5eed0024 0x5eed0028 0x5eed002c 0x5eed0030 | ||
| 11 | ; 0x5eed0010 0x5eed0014 0x5eed0018 0x5eed001c (1592590372 1592590376 1592590380 1592590384 1592590352 1592590356 | ||
| 12 | ; 1592590360 1592590364). Same FSSM header as cs_lds. | ||
| 13 | v_cmpx_eq_u32_e32 0, v0 | ||
| 14 | v_mov_b32 v1, 0x5eed0010 | ||
| 15 | v_mov_b32 v2, 0x10 | ||
| 16 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 17 | v_mov_b32 v1, 0x5eed0014 | ||
| 18 | v_mov_b32 v2, 0x14 | ||
| 19 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 20 | v_mov_b32 v1, 0x5eed0018 | ||
| 21 | v_mov_b32 v2, 0x18 | ||
| 22 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 23 | v_mov_b32 v1, 0x5eed001c | ||
| 24 | v_mov_b32 v2, 0x1c | ||
| 25 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 26 | v_mov_b32 v1, 0x5eed0020 | ||
| 27 | v_mov_b32 v2, 0x20 | ||
| 28 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 29 | v_mov_b32 v1, 0x5eed0024 | ||
| 30 | v_mov_b32 v2, 0x24 | ||
| 31 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 32 | v_mov_b32 v1, 0x5eed0028 | ||
| 33 | v_mov_b32 v2, 0x28 | ||
| 34 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 35 | v_mov_b32 v1, 0x5eed002c | ||
| 36 | v_mov_b32 v2, 0x2c | ||
| 37 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 38 | v_mov_b32 v1, 0x5eed0030 | ||
| 39 | v_mov_b32 v2, 0x30 | ||
| 40 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 41 | s_waitcnt vmcnt(0) | ||
| 42 | s_mov_b32 s4, 0 | ||
| 43 | s_mov_b32 s5, 0 | ||
| 44 | s_mov_b32 s8, 0x10 | ||
| 45 | s_buffer_load_dword s9, s[0:3], s8 offset:0x14 | ||
| 46 | s_load_dword s10, s[4:5], s8 offset:0x18 | ||
| 47 | s_buffer_load_dwordx2 s[12:13], s[0:3], s8 offset:0x1c | ||
| 48 | s_waitcnt lgkmcnt(0) | ||
| 49 | v_mov_b32 v1, s9 | ||
| 50 | v_mov_b32 v2, 0 | ||
| 51 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 52 | v_mov_b32 v1, s10 | ||
| 53 | v_mov_b32 v2, 4 | ||
| 54 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 55 | v_mov_b32 v1, s12 | ||
| 56 | v_mov_b32 v2, 8 | ||
| 57 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 58 | v_mov_b32 v1, s13 | ||
| 59 | v_mov_b32 v2, 12 | ||
| 60 | buffer_store_dword v1, v2, s[0:3], 0 offen | ||
| 61 | s_endpgm | ||
diff --git a/runtime/test/vk_texture_test.cpp b/runtime/test/vk_texture_test.cpp index a46338bf6..fafc8ef50 100644 --- a/runtime/test/vk_texture_test.cpp +++ b/runtime/test/vk_texture_test.cpp | |||
| @@ -34,23 +34,7 @@ static std::vector<uint32_t> read_spv(const char* p) { | |||
| 34 | return w; | 34 | return w; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | // Same hash as the translator's desc_lookup: FNV-1a over the 8 key dwords (whole dwords, not bytes). | 37 | #include "../src/descriptor_table.h" |
| 38 | static uint32_t key_hash(const uint32_t k[8]) { | ||
| 39 | uint32_t h = 2166136261u; | ||
| 40 | for (int i = 0; i < 8; i++) h = (h ^ k[i]) * 16777619u; | ||
| 41 | return h; | ||
| 42 | } | ||
| 43 | static void table_insert(std::vector<uint32_t>& t, const uint32_t key[8], uint32_t index) { | ||
| 44 | uint32_t mask = t[0], h = key_hash(key); | ||
| 45 | for (uint32_t k = 0; k < 8; k++) { | ||
| 46 | uint32_t slot = (h + k) & mask, base = slot * 10; | ||
| 47 | if (t[base + 10] == 0xA11CA7ED) continue; // pad dword marks a used slot (host-side only) | ||
| 48 | for (int j = 0; j < 8; j++) t[base + 1 + j] = key[j]; | ||
| 49 | t[base + 9] = index; | ||
| 50 | t[base + 10] = 0xA11CA7ED; | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | 38 | ||
| 55 | int main(int argc, char** argv) { | 39 | int main(int argc, char** argv) { |
| 56 | if (argc < 9) { std::fprintf(stderr, "usage: vk_texture_test vs fs out ptr_sgpr s0 s1 s2 s3\n"); return 2; } | 40 | if (argc < 9) { std::fprintf(stderr, "usage: vk_texture_test vs fs out ptr_sgpr s0 s1 s2 s3\n"); return 2; } |
| @@ -133,11 +117,10 @@ int main(int argc, char** argv) { | |||
| 133 | VkBufferDeviceAddressInfo bdai{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; bdai.buffer = heap; | 117 | VkBufferDeviceAddressInfo bdai{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; bdai.buffer = heap; |
| 134 | VkDeviceAddress heap_base = vkGetBufferDeviceAddress(dev, &bdai); | 118 | VkDeviceAddress heap_base = vkGetBufferDeviceAddress(dev, &bdai); |
| 135 | // Descriptor hash table: 16 slots. | 119 | // Descriptor hash table: 16 slots. |
| 136 | std::vector<uint32_t> table(1 + 16 * 10 + 10, 0); | 120 | leonida::DescriptorTable desc_table(16); |
| 137 | table[0] = 15; | 121 | desc_table.insert(tsharp, 1); |
| 138 | table_insert(table, tsharp, 1); | 122 | desc_table.insert(ssharp, 1); |
| 139 | table_insert(table, ssharp, 1); | 123 | const auto table = desc_table.build_gpu_buffer(true); |
| 140 | for (uint32_t s = 0; s <= 16; s++) if (s * 10 + 10 < table.size() && table[s * 10 + 10] == 0xA11CA7ED) table[s * 10 + 10] = 0; | ||
| 141 | VkBuffer tb; VkDeviceMemory tbm; void* tbp; | 124 | VkBuffer tb; VkDeviceMemory tbm; void* tbp; |
| 142 | CHECK(make_buffer(table.size() * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &tb, &tbm, &tbp)); | 125 | CHECK(make_buffer(table.size() * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &tb, &tbm, &tbp)); |
| 143 | std::memcpy(tbp, table.data(), table.size() * 4); | 126 | std::memcpy(tbp, table.data(), table.size() * 4); |
diff --git a/runtime/test/vulkan_loader.h b/runtime/test/vulkan_loader.h index 43a54d242..a1a461ef0 100644 --- a/runtime/test/vulkan_loader.h +++ b/runtime/test/vulkan_loader.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | // Loads the Vulkan loader at run time: vulkan-1.dll on Windows, libvulkan.so.1 elsewhere. No import library. | 1 | // Loads the Vulkan loader at run time: vulkan-1.dll on Windows, MoltenVK on macOS, libvulkan.so.1 elsewhere. No import library. |
| 2 | #pragma once | 2 | #pragma once |
| 3 | 3 | ||
| 4 | #ifndef VK_NO_PROTOTYPES | 4 | #ifndef VK_NO_PROTOTYPES |
| @@ -20,6 +20,15 @@ inline PFN_vkGetInstanceProcAddr load_vulkan() { | |||
| 20 | constexpr auto library_name = "vulkan-1.dll"; | 20 | constexpr auto library_name = "vulkan-1.dll"; |
| 21 | static const auto library = LoadLibraryA(library_name); | 21 | static const auto library = LoadLibraryA(library_name); |
| 22 | const auto entry = library ? GetProcAddress(library, "vkGetInstanceProcAddr") : nullptr; | 22 | const auto entry = library ? GetProcAddress(library, "vkGetInstanceProcAddr") : nullptr; |
| 23 | #elif defined(__APPLE__) | ||
| 24 | // MoltenVK directly: through the loader it is a portability driver, which every vkCreateInstance here would | ||
| 25 | // have to opt into. The Vulkan SDK installs it in /usr/local/lib (a default search path), Homebrew does not. | ||
| 26 | constexpr auto library_name = "libMoltenVK.dylib"; | ||
| 27 | static const auto library = [] { | ||
| 28 | auto handle = dlopen(library_name, RTLD_NOW | RTLD_LOCAL); | ||
| 29 | return handle ? handle : dlopen("/opt/homebrew/lib/libMoltenVK.dylib", RTLD_NOW | RTLD_LOCAL); | ||
| 30 | }(); | ||
| 31 | const auto entry = library ? dlsym(library, "vkGetInstanceProcAddr") : nullptr; | ||
| 23 | #else | 32 | #else |
| 24 | constexpr auto library_name = "libvulkan.so.1"; | 33 | constexpr auto library_name = "libvulkan.so.1"; |
| 25 | static const auto library = dlopen(library_name, RTLD_NOW | RTLD_LOCAL); | 34 | static const auto library = dlopen(library_name, RTLD_NOW | RTLD_LOCAL); |