diff options
Diffstat (limited to 'runtime/src/vulkan_aftermath.h')
| -rw-r--r-- | runtime/src/vulkan_aftermath.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/runtime/src/vulkan_aftermath.h b/runtime/src/vulkan_aftermath.h new file mode 100644 index 000000000..ebf95ed4d --- /dev/null +++ b/runtime/src/vulkan_aftermath.h | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Optional NVIDIA Nsight Aftermath GPU crash dumps (LEONIDA_AFTERMATH=1). The SDK is never part of the build unless | ||
| 4 | // CMake is given -DLEONIDA_AFTERMATH_SDK=<dir>; then vulkan_aftermath.cpp is compiled with LEONIDA_HAVE_AFTERMATH and | ||
| 5 | // loads GFSDK_Aftermath_Lib.x64.dll at run time (LEONIDA_AFTERMATH_DLL, else the SDK's lib/x64, else the DLL search | ||
| 6 | // path). Without the SDK, or without the DLL or an NVIDIA driver, every function here does nothing. | ||
| 7 | // | ||
| 8 | // Environment: | ||
| 9 | // LEONIDA_AFTERMATH=1 enable GPU crash dumps for the Vulkan device (before vkCreateDevice) | ||
| 10 | // LEONIDA_AFTERMATH_DIR=<dir> where dumps go (default leonida-aftermath under the system temp directory): <stamp>.nv-gpudmp, the decoded | ||
| 11 | // <stamp>.json, shader-<id>.nvdbg debug info, and <hash>.spv for the active shaders | ||
| 12 | // LEONIDA_AFTERMATH_DLL=<file> the Aftermath library to load | ||
| 13 | // LEONIDA_AFTERMATH_SHADER_ERRORS=1 also set VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_ERROR_REPORTING_BIT_NV | ||
| 14 | // (reports more shader errors, so it can fault where a run does not) | ||
| 15 | // LEONIDA_AFTERMATH_KEEP_SPIRV=1 keep a copy of every SPIR-V module created, for the decoder (it needs the binary | ||
| 16 | // to map a pc to a SPIR-V instruction). Off by default, as the game creates GBs of | ||
| 17 | // them: the decoder then reads <LEONIDA_SHADERS>/<name>.spv back when its hash | ||
| 18 | // matches the module's (true for unpatched library shaders) | ||
| 19 | |||
| 20 | #include <vulkan/vulkan.h> | ||
| 21 | |||
| 22 | #include <cstddef> | ||
| 23 | #include <cstdint> | ||
| 24 | #include <string> | ||
| 25 | |||
| 26 | namespace leonida::aftermath { | ||
| 27 | |||
| 28 | // True when this build has the Aftermath integration (LEONIDA_HAVE_AFTERMATH). | ||
| 29 | bool available() noexcept; | ||
| 30 | |||
| 31 | // LEONIDA_AFTERMATH=1: loads the library and calls GFSDK_Aftermath_EnableGpuCrashDumps for Vulkan. Must run before | ||
| 32 | // vkCreateDevice. The first call decides; returns whether crash dumps are enabled. Also installs the device-lost hook | ||
| 33 | // of vulkan_device_fault.h. | ||
| 34 | bool enable() noexcept; | ||
| 35 | bool enabled() noexcept; | ||
| 36 | |||
| 37 | // VK_NV_device_diagnostics_config flags to add to the device-fault defaults (shader debug info, resource tracking, | ||
| 38 | // automatic checkpoints), 0 when Aftermath is off. | ||
| 39 | VkDeviceDiagnosticsConfigFlagsNV extra_config_flags() noexcept; | ||
| 40 | |||
| 41 | // Returns a vkCreateShaderModule that also records the module's Aftermath shader hash under the name in | ||
| 42 | // ShaderName, or `real` itself when Aftermath is off. Process-wide: the first `real` wins. | ||
| 43 | PFN_vkCreateShaderModule wrap_create_shader_module(PFN_vkCreateShaderModule real) noexcept; | ||
| 44 | |||
| 45 | // Returns a vkGetDeviceProcAddr that hands out the wrapper above for "vkCreateShaderModule", so every function table | ||
| 46 | // loaded through it (the runtime's helpers load their own) records its modules; `real` itself when Aftermath is off. | ||
| 47 | PFN_vkGetDeviceProcAddr wrap_get_device_proc_addr(PFN_vkGetDeviceProcAddr real) noexcept; | ||
| 48 | |||
| 49 | // Records a SPIR-V module created outside the wrapper. | ||
| 50 | void register_spirv(const void* code, std::size_t bytes, const char* name) noexcept; | ||
| 51 | |||
| 52 | // Blocks until Aftermath has written and decoded the GPU crash dump, at most ~10 s. Any thread, any number of times: | ||
| 53 | // the first device loss waits, later calls return at once (or wait for the first one to finish). | ||
| 54 | void wait_for_dump() noexcept; | ||
| 55 | |||
| 56 | // For tests: the last dump written and the summary printed for it (empty before a dump). | ||
| 57 | std::string last_dump_path(); | ||
| 58 | std::string last_summary(); | ||
| 59 | |||
| 60 | namespace detail { | ||
| 61 | inline thread_local const char* shader_name = nullptr; | ||
| 62 | } | ||
| 63 | |||
| 64 | // Names the shader modules this thread creates while it is in scope (the wrapper above reads it). | ||
| 65 | class ShaderName { | ||
| 66 | public: | ||
| 67 | explicit ShaderName(const char* name) noexcept : previous_(detail::shader_name) { detail::shader_name = name; } | ||
| 68 | explicit ShaderName(const std::string& name) noexcept : ShaderName(name.c_str()) {} | ||
| 69 | ~ShaderName() { detail::shader_name = previous_; } | ||
| 70 | ShaderName(const ShaderName&) = delete; | ||
| 71 | ShaderName& operator=(const ShaderName&) = delete; | ||
| 72 | |||
| 73 | private: | ||
| 74 | const char* previous_; | ||
| 75 | }; | ||
| 76 | |||
| 77 | } // namespace leonida::aftermath | ||