diff options
| author | kett1m <benho0@proton.me> | 2026-11-29 20:30:08 +0100 |
|---|---|---|
| committer | kett1m <benho0@proton.me> | 2026-11-29 20:30:08 +0100 |
| commit | 91ef64f43a5a201c2b3123309d379ee707e79c53 (patch) | |
| tree | cf46fb129e6974bd4c171a4809ae664bc02bd104 /runtime/src/vulkan_aftermath.cpp | |
| parent | 691493ac133f0a453eff53e93dcaaf068dc9f9dc (diff) | |
| parent | dd87aa405a0d48cbce4269c9d20e50c3ec8b5dbc (diff) | |
Merge remote-tracking branch 'origin/main' into x64-recomp-insertq-imm8
# Conflicts:
# tests/test_x64_recomp.py
Diffstat (limited to 'runtime/src/vulkan_aftermath.cpp')
| -rw-r--r-- | runtime/src/vulkan_aftermath.cpp | 822 |
1 files changed, 822 insertions, 0 deletions
diff --git a/runtime/src/vulkan_aftermath.cpp b/runtime/src/vulkan_aftermath.cpp new file mode 100644 index 000000000..82c166136 --- /dev/null +++ b/runtime/src/vulkan_aftermath.cpp | |||
| @@ -0,0 +1,822 @@ | |||
| 1 | // NVIDIA Nsight Aftermath GPU crash dumps for the Vulkan device (vulkan_aftermath.h). Without LEONIDA_HAVE_AFTERMATH | ||
| 2 | // (no -DLEONIDA_AFTERMATH_SDK at configure time) this file only has the do-nothing versions. | ||
| 3 | #include "vulkan_aftermath.h" | ||
| 4 | |||
| 5 | #include "vulkan_device_fault.h" | ||
| 6 | |||
| 7 | #ifdef LEONIDA_HAVE_AFTERMATH | ||
| 8 | |||
| 9 | #include "shader_library.h" // leonida::Json, leonida::JsonReader | ||
| 10 | |||
| 11 | #include <windows.h> | ||
| 12 | |||
| 13 | #include <GFSDK_Aftermath.h> | ||
| 14 | #include <GFSDK_Aftermath_GpuCrashDump.h> | ||
| 15 | #include <GFSDK_Aftermath_GpuCrashDumpDecoding.h> | ||
| 16 | |||
| 17 | #include <atomic> | ||
| 18 | #include <chrono> | ||
| 19 | #include <cinttypes> | ||
| 20 | #include <condition_variable> | ||
| 21 | #include <cstdio> | ||
| 22 | #include <cstring> | ||
| 23 | #include <filesystem> | ||
| 24 | #include <fstream> | ||
| 25 | #include <map> | ||
| 26 | #include <mutex> | ||
| 27 | #include <string> | ||
| 28 | #include <thread> | ||
| 29 | #include <unordered_map> | ||
| 30 | #include <vector> | ||
| 31 | |||
| 32 | #ifndef LEONIDA_AFTERMATH_DLL_DEFAULT | ||
| 33 | #define LEONIDA_AFTERMATH_DLL_DEFAULT "" | ||
| 34 | #endif | ||
| 35 | #ifndef LEONIDA_AFTERMATH_APP_VERSION | ||
| 36 | #define LEONIDA_AFTERMATH_APP_VERSION "unknown" | ||
| 37 | #endif | ||
| 38 | |||
| 39 | namespace leonida::aftermath { | ||
| 40 | namespace { | ||
| 41 | |||
| 42 | using Clock = std::chrono::steady_clock; | ||
| 43 | constexpr auto wait_limit = std::chrono::seconds(10); | ||
| 44 | |||
| 45 | std::wstring env_w(const wchar_t* name) { | ||
| 46 | wchar_t buffer[32768]; | ||
| 47 | const DWORD n = GetEnvironmentVariableW(name, buffer, DWORD(std::size(buffer))); | ||
| 48 | return n && n < std::size(buffer) ? std::wstring(buffer, n) : std::wstring(); | ||
| 49 | } | ||
| 50 | |||
| 51 | bool env_is(const wchar_t* name, wchar_t value) { | ||
| 52 | const std::wstring v = env_w(name); | ||
| 53 | return v.size() == 1 && v[0] == value; | ||
| 54 | } | ||
| 55 | |||
| 56 | std::filesystem::path from_utf8(std::string_view text) { | ||
| 57 | return std::filesystem::path(std::u8string(reinterpret_cast<const char8_t*>(text.data()), text.size())); | ||
| 58 | } | ||
| 59 | |||
| 60 | std::string narrow(const std::filesystem::path& path) { | ||
| 61 | const std::u8string s = path.u8string(); | ||
| 62 | return std::string(s.begin(), s.end()); | ||
| 63 | } | ||
| 64 | |||
| 65 | struct Library { | ||
| 66 | HMODULE module = nullptr; | ||
| 67 | PFN_GFSDK_Aftermath_EnableGpuCrashDumps EnableGpuCrashDumps = nullptr; | ||
| 68 | PFN_GFSDK_Aftermath_GetCrashDumpStatus GetCrashDumpStatus = nullptr; | ||
| 69 | PFN_GFSDK_Aftermath_GetShaderHashSpirv GetShaderHashSpirv = nullptr; | ||
| 70 | PFN_GFSDK_Aftermath_GetShaderDebugInfoIdentifier GetShaderDebugInfoIdentifier = nullptr; | ||
| 71 | PFN_GFSDK_Aftermath_GpuCrashDump_CreateDecoder CreateDecoder = nullptr; | ||
| 72 | PFN_GFSDK_Aftermath_GpuCrashDump_DestroyDecoder DestroyDecoder = nullptr; | ||
| 73 | PFN_GFSDK_Aftermath_GpuCrashDump_GetBaseInfo GetBaseInfo = nullptr; | ||
| 74 | PFN_GFSDK_Aftermath_GpuCrashDump_GetDeviceInfo GetDeviceInfo = nullptr; | ||
| 75 | PFN_GFSDK_Aftermath_GpuCrashDump_GetPageFaultInfo GetPageFaultInfo = nullptr; | ||
| 76 | PFN_GFSDK_Aftermath_GpuCrashDump_GetPageFaultResourceInfo GetPageFaultResourceInfo = nullptr; | ||
| 77 | PFN_GFSDK_Aftermath_GpuCrashDump_GetActiveShadersInfoCount GetActiveShadersInfoCount = nullptr; | ||
| 78 | PFN_GFSDK_Aftermath_GpuCrashDump_GetActiveShadersInfo GetActiveShadersInfo = nullptr; | ||
| 79 | PFN_GFSDK_Aftermath_GetShaderHashForShaderInfo GetShaderHashForShaderInfo = nullptr; | ||
| 80 | PFN_GFSDK_Aftermath_GpuCrashDump_GenerateJSON GenerateJSON = nullptr; | ||
| 81 | PFN_GFSDK_Aftermath_GpuCrashDump_GetJSON GetJSON = nullptr; | ||
| 82 | |||
| 83 | bool load(const std::wstring& path) { | ||
| 84 | module = path.find_first_of(L"\\/") != std::wstring::npos | ||
| 85 | ? LoadLibraryExW(path.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH) | ||
| 86 | : LoadLibraryW(path.c_str()); | ||
| 87 | if (!module) return false; | ||
| 88 | bool ok = true; | ||
| 89 | const auto get = [&](auto& fn, const char* name) { | ||
| 90 | fn = reinterpret_cast<std::remove_reference_t<decltype(fn)>>(GetProcAddress(module, name)); | ||
| 91 | if (!fn) { | ||
| 92 | std::fprintf(stderr, "[rt] aftermath: %s is missing from the library\n", name); | ||
| 93 | ok = false; | ||
| 94 | } | ||
| 95 | }; | ||
| 96 | get(EnableGpuCrashDumps, "GFSDK_Aftermath_EnableGpuCrashDumps"); | ||
| 97 | get(GetCrashDumpStatus, "GFSDK_Aftermath_GetCrashDumpStatus"); | ||
| 98 | get(GetShaderHashSpirv, "GFSDK_Aftermath_GetShaderHashSpirv"); | ||
| 99 | get(GetShaderDebugInfoIdentifier, "GFSDK_Aftermath_GetShaderDebugInfoIdentifier"); | ||
| 100 | get(CreateDecoder, "GFSDK_Aftermath_GpuCrashDump_CreateDecoder"); | ||
| 101 | get(DestroyDecoder, "GFSDK_Aftermath_GpuCrashDump_DestroyDecoder"); | ||
| 102 | get(GetBaseInfo, "GFSDK_Aftermath_GpuCrashDump_GetBaseInfo"); | ||
| 103 | get(GetDeviceInfo, "GFSDK_Aftermath_GpuCrashDump_GetDeviceInfo"); | ||
| 104 | get(GetPageFaultInfo, "GFSDK_Aftermath_GpuCrashDump_GetPageFaultInfo"); | ||
| 105 | get(GetPageFaultResourceInfo, "GFSDK_Aftermath_GpuCrashDump_GetPageFaultResourceInfo"); | ||
| 106 | get(GetActiveShadersInfoCount, "GFSDK_Aftermath_GpuCrashDump_GetActiveShadersInfoCount"); | ||
| 107 | get(GetActiveShadersInfo, "GFSDK_Aftermath_GpuCrashDump_GetActiveShadersInfo"); | ||
| 108 | get(GetShaderHashForShaderInfo, "GFSDK_Aftermath_GetShaderHashForShaderInfo"); | ||
| 109 | get(GenerateJSON, "GFSDK_Aftermath_GpuCrashDump_GenerateJSON"); | ||
| 110 | get(GetJSON, "GFSDK_Aftermath_GpuCrashDump_GetJSON"); | ||
| 111 | return ok; | ||
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | struct Shader { | ||
| 116 | std::string name; | ||
| 117 | std::vector<std::uint32_t> words; // only with LEONIDA_AFTERMATH_KEEP_SPIRV=1 | ||
| 118 | }; | ||
| 119 | |||
| 120 | struct State { | ||
| 121 | Library lib; | ||
| 122 | std::filesystem::path dir; | ||
| 123 | std::filesystem::path shader_dir; // LEONIDA_SHADERS: where a module's SPIR-V is read back for the decoder | ||
| 124 | bool keep_spirv = false; | ||
| 125 | VkDeviceDiagnosticsConfigFlagsNV extra_flags = 0; | ||
| 126 | std::atomic<bool> enabled{false}; | ||
| 127 | std::atomic<PFN_vkCreateShaderModule> real_create{nullptr}; | ||
| 128 | std::atomic<PFN_vkGetDeviceProcAddr> real_get{nullptr}; | ||
| 129 | |||
| 130 | std::mutex shaders_mutex; // leaf | ||
| 131 | std::unordered_map<std::uint64_t, Shader> shaders; | ||
| 132 | std::atomic<std::uint64_t> registered{0}, hash_failures{0}; | ||
| 133 | |||
| 134 | std::mutex debug_mutex; // leaf | ||
| 135 | std::map<std::pair<std::uint64_t, std::uint64_t>, std::vector<std::uint8_t>> debug_info; | ||
| 136 | |||
| 137 | std::mutex dump_mutex; // leaf, guards everything below | ||
| 138 | std::condition_variable dump_cv; | ||
| 139 | std::vector<std::uint8_t> dump; | ||
| 140 | std::filesystem::path dump_path; | ||
| 141 | bool have_dump = false, decoded = false; | ||
| 142 | std::string summary; | ||
| 143 | |||
| 144 | std::mutex wait_mutex; // held by the first device-loss waiter; later ones queue behind it | ||
| 145 | bool waited = false; | ||
| 146 | }; | ||
| 147 | |||
| 148 | State& state() { | ||
| 149 | static State* s = new State; // never destroyed: Aftermath's threads may call back during exit | ||
| 150 | return *s; | ||
| 151 | } | ||
| 152 | |||
| 153 | std::string hex64(std::uint64_t v) { | ||
| 154 | char buffer[24]; | ||
| 155 | std::snprintf(buffer, sizeof buffer, "%016" PRIx64, v); | ||
| 156 | return buffer; | ||
| 157 | } | ||
| 158 | |||
| 159 | std::string stamp() { | ||
| 160 | SYSTEMTIME t{}; | ||
| 161 | GetLocalTime(&t); | ||
| 162 | char buffer[64]; | ||
| 163 | std::snprintf(buffer, sizeof buffer, "%04u%02u%02u-%02u%02u%02u-%03u-pid%lu", t.wYear, t.wMonth, t.wDay, t.wHour, | ||
| 164 | t.wMinute, t.wSecond, t.wMilliseconds, GetCurrentProcessId()); | ||
| 165 | return buffer; | ||
| 166 | } | ||
| 167 | |||
| 168 | bool write_file(const std::filesystem::path& path, const void* data, std::size_t size) { | ||
| 169 | std::error_code ec; | ||
| 170 | std::filesystem::create_directories(path.parent_path(), ec); | ||
| 171 | std::ofstream out(path, std::ios::binary | std::ios::trunc); | ||
| 172 | out.write(static_cast<const char*>(data), std::streamsize(size)); | ||
| 173 | return bool(out); | ||
| 174 | } | ||
| 175 | |||
| 176 | std::uint64_t spirv_hash(const void* code, std::size_t bytes) { | ||
| 177 | State& s = state(); | ||
| 178 | GFSDK_Aftermath_SpirvCode spirv{}; | ||
| 179 | spirv.pData = code; | ||
| 180 | spirv.size = std::uint32_t(bytes); | ||
| 181 | GFSDK_Aftermath_ShaderBinaryHash hash{}; | ||
| 182 | if (!GFSDK_Aftermath_SUCCEED(s.lib.GetShaderHashSpirv(GFSDK_Aftermath_Version_API, &spirv, &hash))) return 0; | ||
| 183 | return hash.hash; | ||
| 184 | } | ||
| 185 | |||
| 186 | const char* status_name(GFSDK_Aftermath_Device_Status status) { | ||
| 187 | switch (status) { | ||
| 188 | case GFSDK_Aftermath_Device_Status_Active: return "Active"; | ||
| 189 | case GFSDK_Aftermath_Device_Status_Timeout: return "Timeout"; | ||
| 190 | case GFSDK_Aftermath_Device_Status_OutOfMemory: return "OutOfMemory"; | ||
| 191 | case GFSDK_Aftermath_Device_Status_PageFault: return "PageFault"; | ||
| 192 | case GFSDK_Aftermath_Device_Status_Stopped: return "Stopped"; | ||
| 193 | case GFSDK_Aftermath_Device_Status_Reset: return "Reset"; | ||
| 194 | case GFSDK_Aftermath_Device_Status_DmaFault: return "DmaFault"; | ||
| 195 | case GFSDK_Aftermath_Device_Status_DeviceRemovedNoGpuFault: return "DeviceRemovedNoGpuFault"; | ||
| 196 | default: return "Unknown"; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | const char* fault_name(GFSDK_Aftermath_FaultType type) { | ||
| 201 | switch (type) { | ||
| 202 | case GFSDK_Aftermath_FaultType_AddressTranslationError: return "AddressTranslationError"; | ||
| 203 | case GFSDK_Aftermath_FaultType_IllegalAccessError: return "IllegalAccessError"; | ||
| 204 | default: return "Unknown"; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | const char* access_name(GFSDK_Aftermath_AccessType type) { | ||
| 209 | switch (type) { | ||
| 210 | case GFSDK_Aftermath_AccessType_Read: return "Read"; | ||
| 211 | case GFSDK_Aftermath_AccessType_Write: return "Write"; | ||
| 212 | case GFSDK_Aftermath_AccessType_Atomic: return "Atomic"; | ||
| 213 | default: return "Unknown"; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | const char* engine_name(GFSDK_Aftermath_Engine engine) { | ||
| 218 | switch (engine) { | ||
| 219 | case GFSDK_Aftermath_Engine_Graphics: return "Graphics"; | ||
| 220 | case GFSDK_Aftermath_Engine_GraphicsCompute: return "GraphicsCompute"; | ||
| 221 | case GFSDK_Aftermath_Engine_Display: return "Display"; | ||
| 222 | case GFSDK_Aftermath_Engine_CopyEngine: return "CopyEngine"; | ||
| 223 | case GFSDK_Aftermath_Engine_VideoDecoder: return "VideoDecoder"; | ||
| 224 | case GFSDK_Aftermath_Engine_VideoEncoder: return "VideoEncoder"; | ||
| 225 | case GFSDK_Aftermath_Engine_Other: return "Other"; | ||
| 226 | default: return "Unknown"; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | const char* client_name(GFSDK_Aftermath_Client client) { | ||
| 231 | switch (client) { | ||
| 232 | case GFSDK_Aftermath_Client_HostInterface: return "HostInterface"; | ||
| 233 | case GFSDK_Aftermath_Client_FrontEnd: return "FrontEnd"; | ||
| 234 | case GFSDK_Aftermath_Client_PrimitiveDistributor: return "PrimitiveDistributor"; | ||
| 235 | case GFSDK_Aftermath_Client_GraphicsProcessingCluster: return "GraphicsProcessingCluster"; | ||
| 236 | case GFSDK_Aftermath_Client_PolymorphEngine: return "PolymorphEngine"; | ||
| 237 | case GFSDK_Aftermath_Client_RasterEngine: return "RasterEngine"; | ||
| 238 | case GFSDK_Aftermath_Client_Rasterizer2D: return "Rasterizer2D"; | ||
| 239 | case GFSDK_Aftermath_Client_RenderOutputUnit: return "RenderOutputUnit"; | ||
| 240 | case GFSDK_Aftermath_Client_TextureProcessingCluster: return "TextureProcessingCluster"; | ||
| 241 | case GFSDK_Aftermath_Client_CopyEngine: return "CopyEngine"; | ||
| 242 | case GFSDK_Aftermath_Client_VideoDecoder: return "VideoDecoder"; | ||
| 243 | case GFSDK_Aftermath_Client_VideoEncoder: return "VideoEncoder"; | ||
| 244 | case GFSDK_Aftermath_Client_Other: return "Other"; | ||
| 245 | default: return "Unknown"; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | const char* shader_type_name(GFSDK_Aftermath_ShaderType type) { | ||
| 250 | switch (type) { | ||
| 251 | case GFSDK_Aftermath_ShaderType_Vertex: return "vertex"; | ||
| 252 | case GFSDK_Aftermath_ShaderType_Tessellation_Control: return "hull"; | ||
| 253 | case GFSDK_Aftermath_ShaderType_Tessellation_Evaluation: return "domain"; | ||
| 254 | case GFSDK_Aftermath_ShaderType_Geometry: return "geometry"; | ||
| 255 | case GFSDK_Aftermath_ShaderType_Fragment: return "pixel"; | ||
| 256 | case GFSDK_Aftermath_ShaderType_Compute: return "compute"; | ||
| 257 | case GFSDK_Aftermath_ShaderType_Mesh: return "mesh"; | ||
| 258 | case GFSDK_Aftermath_ShaderType_Task: return "task"; | ||
| 259 | default: return type >= GFSDK_Aftermath_ShaderType_RayTracing_RayGeneration && | ||
| 260 | type <= GFSDK_Aftermath_ShaderType_RayTracing_Internal | ||
| 261 | ? "ray tracing" | ||
| 262 | : "unknown"; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | const char* residency_name(GFSDK_Aftermath_ResourceResidency residency) { | ||
| 267 | switch (residency) { | ||
| 268 | case GFSDK_Aftermath_ResourceResidency_FullyResident: return "resident"; | ||
| 269 | case GFSDK_Aftermath_ResourceResidency_Evicted: return "evicted"; | ||
| 270 | case GFSDK_Aftermath_ResourceResidency_MemoryFreed: return "memory freed"; | ||
| 271 | case GFSDK_Aftermath_ResourceResidency_MemoryUnbound: return "memory unbound"; | ||
| 272 | default: return "unknown"; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | std::string shader_name(std::uint64_t hash) { | ||
| 277 | State& s = state(); | ||
| 278 | std::lock_guard lock(s.shaders_mutex); | ||
| 279 | const auto it = s.shaders.find(hash); | ||
| 280 | return it == s.shaders.end() ? "unregistered" : it->second.name; | ||
| 281 | } | ||
| 282 | |||
| 283 | // ---- decoding ---- | ||
| 284 | |||
| 285 | struct DecodeContext { | ||
| 286 | std::map<std::uint64_t, std::vector<std::uint32_t>> binaries; // alive until the decoder is destroyed | ||
| 287 | std::vector<std::string> written; | ||
| 288 | }; | ||
| 289 | |||
| 290 | // The module's SPIR-V for the decoder: the kept copy, else <LEONIDA_SHADERS>/<name>.spv when its hash matches (a | ||
| 291 | // patched module, such as a depth24 pixel shader, does not). | ||
| 292 | std::vector<std::uint32_t> find_binary(std::uint64_t hash) { | ||
| 293 | State& s = state(); | ||
| 294 | std::string name; | ||
| 295 | { | ||
| 296 | std::lock_guard lock(s.shaders_mutex); | ||
| 297 | const auto it = s.shaders.find(hash); | ||
| 298 | if (it == s.shaders.end()) return {}; | ||
| 299 | if (!it->second.words.empty()) return it->second.words; | ||
| 300 | name = it->second.name; | ||
| 301 | } | ||
| 302 | if (s.shader_dir.empty() || name.empty() || name.front() == '(') return {}; | ||
| 303 | const std::filesystem::path path = s.shader_dir / from_utf8(name + ".spv"); | ||
| 304 | std::ifstream file(path, std::ios::binary | std::ios::ate); | ||
| 305 | if (!file) return {}; | ||
| 306 | const auto size = std::size_t(file.tellg()); | ||
| 307 | if (!size || size % 4 || size > (256u << 20)) return {}; | ||
| 308 | std::vector<std::uint32_t> words(size / 4); | ||
| 309 | file.seekg(0); | ||
| 310 | file.read(reinterpret_cast<char*>(words.data()), std::streamsize(size)); | ||
| 311 | if (!file || spirv_hash(words.data(), size) != hash) return {}; | ||
| 312 | return words; | ||
| 313 | } | ||
| 314 | |||
| 315 | void GFSDK_AFTERMATH_CALL lookup_debug_info(const GFSDK_Aftermath_ShaderDebugInfoIdentifier* id, | ||
| 316 | PFN_GFSDK_Aftermath_SetData set, void*) { | ||
| 317 | State& s = state(); | ||
| 318 | std::lock_guard lock(s.debug_mutex); | ||
| 319 | const auto it = s.debug_info.find({id->id[0], id->id[1]}); | ||
| 320 | if (it != s.debug_info.end()) set(it->second.data(), std::uint32_t(it->second.size())); | ||
| 321 | } | ||
| 322 | |||
| 323 | void GFSDK_AFTERMATH_CALL lookup_shader(const GFSDK_Aftermath_ShaderBinaryHash* hash, PFN_GFSDK_Aftermath_SetData set, | ||
| 324 | void* user) { | ||
| 325 | auto& context = *static_cast<DecodeContext*>(user); | ||
| 326 | auto it = context.binaries.find(hash->hash); | ||
| 327 | if (it == context.binaries.end()) { | ||
| 328 | it = context.binaries.emplace(hash->hash, find_binary(hash->hash)).first; | ||
| 329 | if (!it->second.empty()) { | ||
| 330 | // Next to the dump, for nv-aftermath-format -b <dir> and Nsight Graphics. | ||
| 331 | const auto path = state().dir / (hex64(hash->hash) + ".spv"); | ||
| 332 | if (write_file(path, it->second.data(), it->second.size() * 4)) context.written.push_back(narrow(path)); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | if (!it->second.empty()) set(it->second.data(), std::uint32_t(it->second.size() * 4)); | ||
| 336 | } | ||
| 337 | |||
| 338 | std::string lower(std::string_view text) { | ||
| 339 | std::string out(text); | ||
| 340 | for (char& c : out) c = char(std::tolower(static_cast<unsigned char>(c))); | ||
| 341 | return out; | ||
| 342 | } | ||
| 343 | |||
| 344 | std::string scalar(const Json& value) { | ||
| 345 | switch (value.kind) { | ||
| 346 | case Json::Kind::string: return value.string; | ||
| 347 | case Json::Kind::boolean: return value.boolean ? "true" : "false"; | ||
| 348 | case Json::Kind::number: { | ||
| 349 | char buffer[32]; | ||
| 350 | if (value.number >= 0 && value.number < 18446744073709551616.0 && value.number == double(std::uint64_t(value.number))) | ||
| 351 | std::snprintf(buffer, sizeof buffer, "%" PRIu64, std::uint64_t(value.number)); | ||
| 352 | else | ||
| 353 | std::snprintf(buffer, sizeof buffer, "%g", value.number); | ||
| 354 | return buffer; | ||
| 355 | } | ||
| 356 | default: return {}; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | // One line per object: its scalar fields, and the scalars of objects nested one level down. | ||
| 361 | std::string flatten(const Json& object) { | ||
| 362 | std::string out; | ||
| 363 | const auto add = [&out](std::string_view key, std::string value) { | ||
| 364 | for (char& c : value) | ||
| 365 | if (c == '\n' || c == '\r') c = ' '; | ||
| 366 | if (!value.empty()) out += (out.empty() ? "" : " ") + std::string(key) + "=" + value; | ||
| 367 | }; | ||
| 368 | for (const auto& [key, value] : object.object) { | ||
| 369 | if (value.kind == Json::Kind::object) | ||
| 370 | for (const auto& [inner_key, inner] : value.object) add(key + "." + inner_key, scalar(inner)); | ||
| 371 | else if (value.kind != Json::Kind::array) | ||
| 372 | add(key, scalar(value)); | ||
| 373 | } | ||
| 374 | return out; | ||
| 375 | } | ||
| 376 | |||
| 377 | // The decoder writes 64-bit values (shader hashes, pointers) as JSON integers, which a double cannot hold. Every | ||
| 378 | // unsigned integer literal of 16 or more digits is quoted, so it reaches the reader exactly, as a string. | ||
| 379 | std::string quote_big_integers(std::string_view json) { | ||
| 380 | std::string out; | ||
| 381 | out.reserve(json.size() + 256); | ||
| 382 | bool in_string = false, escaped = false; | ||
| 383 | for (std::size_t i = 0; i < json.size();) { | ||
| 384 | const char c = json[i]; | ||
| 385 | if (in_string) { | ||
| 386 | out += c; | ||
| 387 | ++i; | ||
| 388 | if (escaped) escaped = false; | ||
| 389 | else if (c == '\\') escaped = true; | ||
| 390 | else if (c == '"') in_string = false; | ||
| 391 | continue; | ||
| 392 | } | ||
| 393 | if (c == '"') { | ||
| 394 | in_string = true; | ||
| 395 | out += c; | ||
| 396 | ++i; | ||
| 397 | continue; | ||
| 398 | } | ||
| 399 | if (c >= '0' && c <= '9') { | ||
| 400 | std::size_t j = i; | ||
| 401 | while (j < json.size() && json[j] >= '0' && json[j] <= '9') ++j; | ||
| 402 | const bool integer = j == json.size() || (json[j] != '.' && json[j] != 'e' && json[j] != 'E'); | ||
| 403 | const bool quote = integer && j - i >= 16 && (i == 0 || json[i - 1] != '-'); | ||
| 404 | if (quote) out += '"'; | ||
| 405 | out.append(json.substr(i, j - i)); | ||
| 406 | if (quote) out += '"'; | ||
| 407 | i = j; | ||
| 408 | continue; | ||
| 409 | } | ||
| 410 | out += c; | ||
| 411 | ++i; | ||
| 412 | } | ||
| 413 | return out; | ||
| 414 | } | ||
| 415 | |||
| 416 | const Json* field(const Json& object, std::string_view needle) { | ||
| 417 | for (const auto& [key, value] : object.object) | ||
| 418 | if (lower(key).find(needle) != std::string::npos) return &value; | ||
| 419 | return nullptr; | ||
| 420 | } | ||
| 421 | |||
| 422 | // Records under a key naming warps ("Faulted Warps", "Active Warps"), and the shader table entries ("Shader infos": | ||
| 423 | // objects with a "Shader name" and a "Shader hash"). The SDK does not specify the JSON layout; this is the one the | ||
| 424 | // 2026.3 decoder writes, read loosely. | ||
| 425 | void walk(const Json& node, std::string_view key, std::vector<std::pair<std::string, const Json*>>& warps, | ||
| 426 | std::map<std::string, std::uint64_t>& shader_hashes, int depth = 0) { | ||
| 427 | if (depth > 16) return; | ||
| 428 | if (node.kind == Json::Kind::array) { | ||
| 429 | if (lower(key).find("warp") != std::string::npos) | ||
| 430 | for (const Json& item : node.array) | ||
| 431 | if (item.kind == Json::Kind::object) warps.emplace_back(std::string(key), &item); | ||
| 432 | for (const Json& item : node.array) walk(item, key, warps, shader_hashes, depth + 1); | ||
| 433 | } else if (node.kind == Json::Kind::object) { | ||
| 434 | const Json* name = node.find("Shader name"); | ||
| 435 | const Json* hash = node.find("Shader hash"); | ||
| 436 | if (name && hash) shader_hashes[scalar(*name)] = std::strtoull(scalar(*hash).c_str(), nullptr, 0); | ||
| 437 | for (const auto& [k, v] : node.object) walk(v, k, warps, shader_hashes, depth + 1); | ||
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | void decode_and_report() noexcept { | ||
| 442 | State& s = state(); | ||
| 443 | std::vector<std::uint8_t> dump; | ||
| 444 | std::filesystem::path dump_path; | ||
| 445 | { | ||
| 446 | std::lock_guard lock(s.dump_mutex); | ||
| 447 | if (s.decoded || !s.have_dump) return; | ||
| 448 | dump = s.dump; | ||
| 449 | dump_path = s.dump_path; | ||
| 450 | } | ||
| 451 | std::string summary; | ||
| 452 | const auto line = [&summary](const char* format, auto... args) { | ||
| 453 | char buffer[1024]; | ||
| 454 | std::snprintf(buffer, sizeof buffer, format, args...); | ||
| 455 | summary += "[rt] aftermath: "; | ||
| 456 | summary += buffer; | ||
| 457 | summary += '\n'; | ||
| 458 | }; | ||
| 459 | try { | ||
| 460 | GFSDK_Aftermath_GpuCrashDump_Decoder decoder = nullptr; | ||
| 461 | const auto created = | ||
| 462 | s.lib.CreateDecoder(GFSDK_Aftermath_Version_API, dump.data(), std::uint32_t(dump.size()), &decoder); | ||
| 463 | if (!GFSDK_Aftermath_SUCCEED(created)) { | ||
| 464 | line("GPU crash dump %s could not be decoded (0x%x)", narrow(dump_path).c_str(), unsigned(created)); | ||
| 465 | } else { | ||
| 466 | GFSDK_Aftermath_GpuCrashDump_DeviceInfo device{}; | ||
| 467 | const bool have_device = GFSDK_Aftermath_SUCCEED(s.lib.GetDeviceInfo(decoder, &device)); | ||
| 468 | line("GPU crash dump %s (%zu bytes): device status %s, adapter reset %s, engine reset %s", | ||
| 469 | narrow(dump_path).c_str(), dump.size(), have_device ? status_name(device.status) : "unknown", | ||
| 470 | have_device && device.adapterReset ? "yes" : "no", have_device && device.engineReset ? "yes" : "no"); | ||
| 471 | |||
| 472 | // Active shaders, by our module names. | ||
| 473 | std::uint32_t count = 0; | ||
| 474 | std::vector<GFSDK_Aftermath_GpuCrashDump_ShaderInfo> shaders; | ||
| 475 | if (GFSDK_Aftermath_SUCCEED(s.lib.GetActiveShadersInfoCount(decoder, &count)) && count) { | ||
| 476 | shaders.resize(count); | ||
| 477 | if (!GFSDK_Aftermath_SUCCEED(s.lib.GetActiveShadersInfo(decoder, count, shaders.data()))) shaders.clear(); | ||
| 478 | } | ||
| 479 | struct Named { | ||
| 480 | std::string name; // our module name | ||
| 481 | std::string binary; // Aftermath shader binary hash, hex | ||
| 482 | }; | ||
| 483 | std::unordered_map<std::uint64_t, Named> by_dump_hash; // ShaderInfo::shaderHash -> module | ||
| 484 | std::vector<std::string> active; | ||
| 485 | for (const auto& info : shaders) { | ||
| 486 | GFSDK_Aftermath_ShaderBinaryHash hash{}; | ||
| 487 | const bool mapped = GFSDK_Aftermath_SUCCEED(s.lib.GetShaderHashForShaderInfo(decoder, &info, &hash)); | ||
| 488 | const std::string name = info.isInternal ? "(driver internal)" | ||
| 489 | : mapped ? shader_name(hash.hash) | ||
| 490 | : "unmapped"; | ||
| 491 | by_dump_hash[info.shaderHash] = {name, mapped ? hex64(hash.hash) : "-"}; | ||
| 492 | char buffer[512]; | ||
| 493 | std::snprintf(buffer, sizeof buffer, "%s %s hash=%s", shader_type_name(info.shaderType), name.c_str(), | ||
| 494 | mapped ? hex64(hash.hash).c_str() : "-"); | ||
| 495 | active.emplace_back(buffer); | ||
| 496 | } | ||
| 497 | |||
| 498 | // Warps, from the JSON (the C API has no per-warp pc). Shader mapping info asks lookup_shader for binaries. | ||
| 499 | DecodeContext context; | ||
| 500 | std::uint32_t json_size = 0; | ||
| 501 | std::string json; | ||
| 502 | const auto generated = s.lib.GenerateJSON( | ||
| 503 | decoder, GFSDK_Aftermath_GpuCrashDumpDecoderFlags_ALL_INFO, | ||
| 504 | GFSDK_Aftermath_GpuCrashDumpFormatterFlags_UTF8_OUTPUT, lookup_debug_info, lookup_shader, nullptr, | ||
| 505 | &context, &json_size); | ||
| 506 | if (GFSDK_Aftermath_SUCCEED(generated) && json_size) { | ||
| 507 | json.resize(json_size); | ||
| 508 | if (!GFSDK_Aftermath_SUCCEED(s.lib.GetJSON(decoder, json_size, json.data()))) json.clear(); | ||
| 509 | while (!json.empty() && json.back() == '\0') json.pop_back(); | ||
| 510 | } | ||
| 511 | std::filesystem::path json_path = dump_path; | ||
| 512 | json_path.replace_extension(".json"); | ||
| 513 | if (!json.empty()) write_file(json_path, json.data(), json.size()); | ||
| 514 | |||
| 515 | Json root; | ||
| 516 | std::vector<std::pair<std::string, const Json*>> warps; | ||
| 517 | std::map<std::string, std::uint64_t> shader_hashes; // the dump's shader name ("compute_02") -> dump hash | ||
| 518 | if (!json.empty() && JsonReader(quote_big_integers(json)).parse(root)) | ||
| 519 | walk(root, {}, warps, shader_hashes); | ||
| 520 | std::stable_partition(warps.begin(), warps.end(), | ||
| 521 | [](const auto& warp) { return lower(warp.first).find("fault") != std::string::npos; }); | ||
| 522 | if (warps.empty()) line("no warp records in the dump (%s)", json.empty() ? "no JSON" : "see the JSON"); | ||
| 523 | std::size_t shown = 0; | ||
| 524 | for (const auto& [key, warp] : warps) { | ||
| 525 | if (shown++ == 16) { | ||
| 526 | line("... %zu more warp records in %s", warps.size() - 16, narrow(json_path).c_str()); | ||
| 527 | break; | ||
| 528 | } | ||
| 529 | // "GPU PC Address": "<dump shader name> @ 0x<pc>", the name keyed in the shader table. | ||
| 530 | const Json* pc_field = field(*warp, "pc address"); | ||
| 531 | const std::string address = pc_field ? scalar(*pc_field) : std::string(); | ||
| 532 | const auto at = address.find(" @ "); | ||
| 533 | const std::string dump_name = at == std::string::npos ? address : address.substr(0, at); | ||
| 534 | const std::string pc = at == std::string::npos ? "?" : address.substr(at + 3); | ||
| 535 | Named module{"?", "-"}; | ||
| 536 | if (const auto h = shader_hashes.find(dump_name); h != shader_hashes.end()) | ||
| 537 | if (const auto m = by_dump_hash.find(h->second); m != by_dump_hash.end()) module = m->second; | ||
| 538 | const Json* mapping = field(*warp, "mapping"); | ||
| 539 | const Json* warp_count = field(*warp, "count"); | ||
| 540 | const Json* fault_name = field(*warp, "fault name"); | ||
| 541 | const bool faulted = lower(key).find("fault") != std::string::npos; | ||
| 542 | if (!pc_field) { | ||
| 543 | line("%s: %s", key.c_str(), flatten(*warp).c_str()); | ||
| 544 | } else if (faulted) { | ||
| 545 | line("faulting shader %s (hash %s, %s) pc %s spirv %s: %s", module.name.c_str(), | ||
| 546 | module.binary.c_str(), dump_name.c_str(), pc.c_str(), | ||
| 547 | mapping ? scalar(*mapping).c_str() : "-", fault_name ? scalar(*fault_name).c_str() : key.c_str()); | ||
| 548 | } else { | ||
| 549 | line("%s: shader %s (hash %s, %s) pc %s spirv %s warps %s", key.c_str(), module.name.c_str(), | ||
| 550 | module.binary.c_str(), dump_name.c_str(), pc.c_str(), | ||
| 551 | mapping ? scalar(*mapping).c_str() : "-", warp_count ? scalar(*warp_count).c_str() : "?"); | ||
| 552 | } | ||
| 553 | } | ||
| 554 | |||
| 555 | GFSDK_Aftermath_GpuCrashDump_PageFaultInfo fault{}; | ||
| 556 | if (GFSDK_Aftermath_SUCCEED(s.lib.GetPageFaultInfo(decoder, &fault))) { | ||
| 557 | line("page fault va=0x%016" PRIx64 " fault=%s access=%s engine=%s client=%s resources=%u", | ||
| 558 | fault.faultingGpuVA, fault_name(fault.faultType), access_name(fault.accessType), | ||
| 559 | engine_name(fault.engine), client_name(fault.client), fault.resourceInfoCount); | ||
| 560 | std::vector<GFSDK_Aftermath_GpuCrashDump_ResourceInfo> resources(fault.resourceInfoCount); | ||
| 561 | if (!resources.empty() && | ||
| 562 | GFSDK_Aftermath_SUCCEED(s.lib.GetPageFaultResourceInfo(decoder, fault.resourceInfoCount, | ||
| 563 | resources.data()))) | ||
| 564 | for (std::size_t i = 0; i < resources.size(); ++i) { | ||
| 565 | const auto& r = resources[i]; | ||
| 566 | line("page fault resource[%zu] va=0x%016" PRIx64 " size=0x%" PRIx64 | ||
| 567 | " %ux%ux%u mips=%u format=%u handle=0x%016" PRIx64 | ||
| 568 | " name=\"%s\" %s%s%s%s", | ||
| 569 | i, r.gpuVa, r.size, r.width, r.height, r.depth, r.mipLevels, r.format, r.apiResource, | ||
| 570 | r.debugName, residency_name(r.residency), r.bWasDestroyed ? ", destroyed" : "", | ||
| 571 | r.bPlacedResource ? ", placed" : "", r.bIsBufferHeap ? ", buffer heap" : ""); | ||
| 572 | } | ||
| 573 | } else { | ||
| 574 | line("no page fault information in the dump"); | ||
| 575 | } | ||
| 576 | for (std::size_t i = 0; i < active.size(); ++i) line("active shader[%zu] %s", i, active[i].c_str()); | ||
| 577 | { | ||
| 578 | // The hash -> name table next to the dump, for matching it offline (nv-aftermath-format, Nsight). | ||
| 579 | std::string table; | ||
| 580 | std::lock_guard lock(s.shaders_mutex); | ||
| 581 | for (const auto& [hash, shader] : s.shaders) table += hex64(hash) + '\t' + shader.name + '\n'; | ||
| 582 | std::filesystem::path table_path = dump_path; | ||
| 583 | table_path.replace_extension(".shaders.tsv"); | ||
| 584 | const bool table_written = write_file(table_path, table.data(), table.size()); | ||
| 585 | line("%llu SPIR-V modules registered (%zu distinct, table %s), %llu not hashed", | ||
| 586 | static_cast<unsigned long long>(s.registered.load()), s.shaders.size(), | ||
| 587 | table_written ? narrow(table_path).c_str() : "not written", | ||
| 588 | static_cast<unsigned long long>(s.hash_failures.load())); | ||
| 589 | } | ||
| 590 | for (const auto& path : context.written) line("wrote %s", path.c_str()); | ||
| 591 | if (!json.empty()) line("decoded JSON %s", narrow(json_path).c_str()); | ||
| 592 | s.lib.DestroyDecoder(decoder); | ||
| 593 | } | ||
| 594 | } catch (...) { | ||
| 595 | line("decoding failed"); | ||
| 596 | } | ||
| 597 | std::fputs(summary.c_str(), stderr); | ||
| 598 | std::fflush(stderr); | ||
| 599 | std::lock_guard lock(s.dump_mutex); | ||
| 600 | s.summary = std::move(summary); | ||
| 601 | s.decoded = true; | ||
| 602 | s.dump_cv.notify_all(); | ||
| 603 | } | ||
| 604 | |||
| 605 | // ---- callbacks (free-threaded, on Aftermath's threads) ---- | ||
| 606 | |||
| 607 | void GFSDK_AFTERMATH_CALL on_crash_dump(const void* data, const std::uint32_t size, void*) { | ||
| 608 | State& s = state(); | ||
| 609 | const auto path = s.dir / (stamp() + ".nv-gpudmp"); | ||
| 610 | const bool written = write_file(path, data, size); | ||
| 611 | std::fprintf(stderr, "[rt] aftermath: GPU crash dump, %u bytes%s %s\n", size, written ? " written to" : ", cannot write", | ||
| 612 | narrow(path).c_str()); | ||
| 613 | { | ||
| 614 | std::lock_guard lock(s.dump_mutex); | ||
| 615 | if (s.have_dump) return; // one decode per process: the first crash is the one that matters | ||
| 616 | const auto bytes = static_cast<const std::uint8_t*>(data); | ||
| 617 | s.dump.assign(bytes, bytes + size); | ||
| 618 | s.dump_path = path; | ||
| 619 | s.have_dump = true; | ||
| 620 | } | ||
| 621 | // Decode once Aftermath is done (the deferred shader debug info callbacks may still follow this one). | ||
| 622 | std::thread([] { | ||
| 623 | State& st = state(); | ||
| 624 | const auto start = Clock::now(); | ||
| 625 | for (;;) { | ||
| 626 | GFSDK_Aftermath_CrashDump_Status status = GFSDK_Aftermath_CrashDump_Status_Unknown; | ||
| 627 | st.lib.GetCrashDumpStatus(&status); | ||
| 628 | if (status == GFSDK_Aftermath_CrashDump_Status_Finished || | ||
| 629 | status == GFSDK_Aftermath_CrashDump_Status_CollectingDataFailed || | ||
| 630 | status == GFSDK_Aftermath_CrashDump_Status_NotStarted || Clock::now() - start > std::chrono::seconds(5)) | ||
| 631 | break; | ||
| 632 | std::this_thread::sleep_for(std::chrono::milliseconds(20)); | ||
| 633 | } | ||
| 634 | decode_and_report(); | ||
| 635 | }).detach(); | ||
| 636 | } | ||
| 637 | |||
| 638 | void GFSDK_AFTERMATH_CALL on_shader_debug_info(const void* data, const std::uint32_t size, void*) { | ||
| 639 | State& s = state(); | ||
| 640 | GFSDK_Aftermath_ShaderDebugInfoIdentifier id{}; | ||
| 641 | if (!GFSDK_Aftermath_SUCCEED(s.lib.GetShaderDebugInfoIdentifier(GFSDK_Aftermath_Version_API, data, size, &id))) | ||
| 642 | return; | ||
| 643 | write_file(s.dir / ("shader-" + hex64(id.id[0]) + "-" + hex64(id.id[1]) + ".nvdbg"), data, size); | ||
| 644 | const auto bytes = static_cast<const std::uint8_t*>(data); | ||
| 645 | std::lock_guard lock(s.debug_mutex); | ||
| 646 | s.debug_info[{id.id[0], id.id[1]}].assign(bytes, bytes + size); | ||
| 647 | } | ||
| 648 | |||
| 649 | void GFSDK_AFTERMATH_CALL on_description(PFN_GFSDK_Aftermath_AddGpuCrashDumpDescription add, void*) { | ||
| 650 | add(GFSDK_Aftermath_GpuCrashDumpDescriptionKey_ApplicationName, "Leonida gta6rt"); | ||
| 651 | add(GFSDK_Aftermath_GpuCrashDumpDescriptionKey_ApplicationVersion, LEONIDA_AFTERMATH_APP_VERSION); | ||
| 652 | } | ||
| 653 | |||
| 654 | VKAPI_ATTR VkResult VKAPI_CALL create_shader_module(VkDevice device, const VkShaderModuleCreateInfo* info, | ||
| 655 | const VkAllocationCallbacks* allocator, VkShaderModule* module) { | ||
| 656 | const VkResult result = state().real_create.load(std::memory_order_relaxed)(device, info, allocator, module); | ||
| 657 | if (result == VK_SUCCESS && info) register_spirv(info->pCode, info->codeSize, detail::shader_name); | ||
| 658 | return result; | ||
| 659 | } | ||
| 660 | |||
| 661 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device_proc_addr(VkDevice device, const char* name) { | ||
| 662 | const PFN_vkVoidFunction function = state().real_get.load(std::memory_order_relaxed)(device, name); | ||
| 663 | if (!function || !name || std::strcmp(name, "vkCreateShaderModule") != 0) return function; | ||
| 664 | return reinterpret_cast<PFN_vkVoidFunction>( | ||
| 665 | wrap_create_shader_module(reinterpret_cast<PFN_vkCreateShaderModule>(function))); | ||
| 666 | } | ||
| 667 | |||
| 668 | void device_lost() noexcept { wait_for_dump(); } | ||
| 669 | |||
| 670 | } // namespace | ||
| 671 | |||
| 672 | bool available() noexcept { return true; } | ||
| 673 | |||
| 674 | bool enable() noexcept { | ||
| 675 | static const bool on = []() noexcept { | ||
| 676 | if (!env_is(L"LEONIDA_AFTERMATH", L'1')) return false; | ||
| 677 | try { | ||
| 678 | State& s = state(); | ||
| 679 | std::wstring dll = env_w(L"LEONIDA_AFTERMATH_DLL"); | ||
| 680 | if (dll.empty()) { | ||
| 681 | const std::filesystem::path built = from_utf8(LEONIDA_AFTERMATH_DLL_DEFAULT); | ||
| 682 | std::error_code ec; | ||
| 683 | dll = !built.empty() && std::filesystem::exists(built, ec) ? built.wstring() | ||
| 684 | : L"GFSDK_Aftermath_Lib.x64.dll"; | ||
| 685 | } | ||
| 686 | if (!s.lib.load(dll)) { | ||
| 687 | std::fprintf(stderr, "[rt] aftermath: cannot load %s (error %lu); crash dumps are off\n", | ||
| 688 | narrow(dll).c_str(), GetLastError()); | ||
| 689 | return false; | ||
| 690 | } | ||
| 691 | const std::wstring dir = env_w(L"LEONIDA_AFTERMATH_DIR"); | ||
| 692 | s.dir = dir.empty() ? std::filesystem::temp_directory_path() / L"leonida-aftermath" : std::filesystem::path(dir); | ||
| 693 | std::error_code ec; | ||
| 694 | std::filesystem::create_directories(s.dir, ec); | ||
| 695 | s.shader_dir = env_w(L"LEONIDA_SHADERS"); | ||
| 696 | s.keep_spirv = env_is(L"LEONIDA_AFTERMATH_KEEP_SPIRV", L'1'); | ||
| 697 | if (env_is(L"LEONIDA_AFTERMATH_SHADER_ERRORS", L'1')) | ||
| 698 | s.extra_flags |= VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_ERROR_REPORTING_BIT_NV; | ||
| 699 | const auto result = s.lib.EnableGpuCrashDumps( | ||
| 700 | GFSDK_Aftermath_Version_API, GFSDK_Aftermath_GpuCrashDumpWatchedApiFlags_Vulkan, | ||
| 701 | GFSDK_Aftermath_GpuCrashDumpFeatureFlags_DeferDebugInfoCallbacks, on_crash_dump, on_shader_debug_info, | ||
| 702 | on_description, nullptr, nullptr); | ||
| 703 | if (!GFSDK_Aftermath_SUCCEED(result)) { | ||
| 704 | std::fprintf(stderr, "[rt] aftermath: GFSDK_Aftermath_EnableGpuCrashDumps failed (0x%x)\n", | ||
| 705 | unsigned(result)); | ||
| 706 | return false; | ||
| 707 | } | ||
| 708 | s.enabled.store(true, std::memory_order_release); | ||
| 709 | device_lost_hook.store(device_lost, std::memory_order_release); | ||
| 710 | std::fprintf(stderr, "[rt] aftermath: GPU crash dumps on (%s), written to %s%s\n", narrow(dll).c_str(), | ||
| 711 | narrow(s.dir).c_str(), s.keep_spirv ? ", SPIR-V kept" : ""); | ||
| 712 | return true; | ||
| 713 | } catch (...) { | ||
| 714 | std::fprintf(stderr, "[rt] aftermath: setup failed\n"); | ||
| 715 | return false; | ||
| 716 | } | ||
| 717 | }(); | ||
| 718 | return on; | ||
| 719 | } | ||
| 720 | |||
| 721 | bool enabled() noexcept { return state().enabled.load(std::memory_order_acquire); } | ||
| 722 | |||
| 723 | VkDeviceDiagnosticsConfigFlagsNV extra_config_flags() noexcept { return enabled() ? state().extra_flags : 0; } | ||
| 724 | |||
| 725 | PFN_vkCreateShaderModule wrap_create_shader_module(PFN_vkCreateShaderModule real) noexcept { | ||
| 726 | if (!enabled() || !real || real == create_shader_module) return real; | ||
| 727 | PFN_vkCreateShaderModule expected = nullptr; | ||
| 728 | State& s = state(); | ||
| 729 | if (!s.real_create.compare_exchange_strong(expected, real) && expected != real) return real; | ||
| 730 | return create_shader_module; | ||
| 731 | } | ||
| 732 | |||
| 733 | PFN_vkGetDeviceProcAddr wrap_get_device_proc_addr(PFN_vkGetDeviceProcAddr real) noexcept { | ||
| 734 | if (!enabled() || !real || real == get_device_proc_addr) return real; | ||
| 735 | PFN_vkGetDeviceProcAddr expected = nullptr; | ||
| 736 | State& s = state(); | ||
| 737 | if (!s.real_get.compare_exchange_strong(expected, real) && expected != real) return real; | ||
| 738 | return get_device_proc_addr; | ||
| 739 | } | ||
| 740 | |||
| 741 | void register_spirv(const void* code, std::size_t bytes, const char* name) noexcept { | ||
| 742 | if (!enabled() || !code || !bytes) return; | ||
| 743 | try { | ||
| 744 | State& s = state(); | ||
| 745 | const std::uint64_t hash = spirv_hash(code, bytes); | ||
| 746 | if (!hash) { | ||
| 747 | s.hash_failures.fetch_add(1, std::memory_order_relaxed); | ||
| 748 | return; | ||
| 749 | } | ||
| 750 | s.registered.fetch_add(1, std::memory_order_relaxed); | ||
| 751 | std::vector<std::uint32_t> words; | ||
| 752 | // The runtime's own modules are few and small, and not in LEONIDA_SHADERS: always kept for the decoder. | ||
| 753 | if (s.keep_spirv || (name && std::strncmp(name, "(runtime)", 9) == 0)) { | ||
| 754 | const auto first = static_cast<const std::uint32_t*>(code); | ||
| 755 | words.assign(first, first + bytes / 4); | ||
| 756 | } | ||
| 757 | std::lock_guard lock(s.shaders_mutex); | ||
| 758 | auto& shader = s.shaders[hash]; | ||
| 759 | if (shader.name.empty() || name) shader.name = name ? name : "(runtime)"; | ||
| 760 | if (!words.empty()) shader.words = std::move(words); | ||
| 761 | } catch (...) { | ||
| 762 | } | ||
| 763 | } | ||
| 764 | |||
| 765 | void wait_for_dump() noexcept { | ||
| 766 | if (!enabled()) return; | ||
| 767 | State& s = state(); | ||
| 768 | std::lock_guard wait(s.wait_mutex); | ||
| 769 | if (s.waited) return; | ||
| 770 | s.waited = true; | ||
| 771 | std::fprintf(stderr, "[rt] aftermath: device lost, waiting for the GPU crash dump (at most %lld s)\n", | ||
| 772 | static_cast<long long>(wait_limit.count())); | ||
| 773 | const auto deadline = Clock::now() + wait_limit; | ||
| 774 | GFSDK_Aftermath_CrashDump_Status status = GFSDK_Aftermath_CrashDump_Status_Unknown; | ||
| 775 | for (;;) { | ||
| 776 | { | ||
| 777 | std::unique_lock lock(s.dump_mutex); | ||
| 778 | if (s.dump_cv.wait_for(lock, std::chrono::milliseconds(50), [&s] { return s.decoded; })) break; | ||
| 779 | if (Clock::now() >= deadline) break; | ||
| 780 | if (status == GFSDK_Aftermath_CrashDump_Status_Finished && !s.have_dump) break; | ||
| 781 | } | ||
| 782 | s.lib.GetCrashDumpStatus(&status); | ||
| 783 | if (status == GFSDK_Aftermath_CrashDump_Status_CollectingDataFailed) break; | ||
| 784 | } | ||
| 785 | std::lock_guard lock(s.dump_mutex); | ||
| 786 | if (!s.decoded) | ||
| 787 | std::fprintf(stderr, "[rt] aftermath: no GPU crash dump after the wait (status %u%s)\n", unsigned(status), | ||
| 788 | s.have_dump ? ", dump written but not decoded yet" : ""); | ||
| 789 | } | ||
| 790 | |||
| 791 | std::string last_dump_path() { | ||
| 792 | State& s = state(); | ||
| 793 | std::lock_guard lock(s.dump_mutex); | ||
| 794 | return s.have_dump ? narrow(s.dump_path) : std::string(); | ||
| 795 | } | ||
| 796 | |||
| 797 | std::string last_summary() { | ||
| 798 | State& s = state(); | ||
| 799 | std::lock_guard lock(s.dump_mutex); | ||
| 800 | return s.summary; | ||
| 801 | } | ||
| 802 | |||
| 803 | } // namespace leonida::aftermath | ||
| 804 | |||
| 805 | #else // !LEONIDA_HAVE_AFTERMATH | ||
| 806 | |||
| 807 | namespace leonida::aftermath { | ||
| 808 | |||
| 809 | bool available() noexcept { return false; } | ||
| 810 | bool enable() noexcept { return false; } | ||
| 811 | bool enabled() noexcept { return false; } | ||
| 812 | VkDeviceDiagnosticsConfigFlagsNV extra_config_flags() noexcept { return 0; } | ||
| 813 | PFN_vkCreateShaderModule wrap_create_shader_module(PFN_vkCreateShaderModule real) noexcept { return real; } | ||
| 814 | PFN_vkGetDeviceProcAddr wrap_get_device_proc_addr(PFN_vkGetDeviceProcAddr real) noexcept { return real; } | ||
| 815 | void register_spirv(const void*, std::size_t, const char*) noexcept {} | ||
| 816 | void wait_for_dump() noexcept {} | ||
| 817 | std::string last_dump_path() { return {}; } | ||
| 818 | std::string last_summary() { return {}; } | ||
| 819 | |||
| 820 | } // namespace leonida::aftermath | ||
| 821 | |||
| 822 | #endif | ||