Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
NsightAftermathGpuCrashTracker.h
1//*********************************************************
2//
3// Copyright (c) 2019-2025, NVIDIA CORPORATION. All rights reserved.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a
6// copy of this software and associated documentation files (the "Software"),
7// to deal in the Software without restriction, including without limitation
8// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9// and/or sell copies of the Software, and to permit persons to whom the
10// Software is furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21// DEALINGS IN THE SOFTWARE.
22//
23//*********************************************************
24
25#pragma once
26
27#include <map>
28#include <mutex>
29#include <string>
30
31#include "NsightAftermathHelpers.h"
32#include "NsightAftermathShaderDatabase.h"
33
34//*********************************************************
35// Implements GPU crash dump tracking using the Nsight
36// Aftermath API.
37//
38class GpuCrashTracker
39{
40public:
41 // keep four frames worth of marker history
42 const static unsigned int c_markerFrameHistory = 4;
43 typedef std::array<std::map<uint64_t, std::string>, c_markerFrameHistory> MarkerMap;
44
45 GpuCrashTracker(const MarkerMap& markerMap);
46 ~GpuCrashTracker();
47
48 // Initialize the GPU crash dump tracker.
49 void Initialize(bool applicationUsesStrippedShaders);
50
51 // Set the process command line for inclusion in crash dump descriptions.
52 // May be called before Initialize(); value is cached and used in OnDescription.
53 void SetCommandLine(const std::string& commandLine) { m_commandLine = commandLine; }
54
55private:
56
57 //*********************************************************
58 // Callback handlers for GPU crash dumps and related data.
59 //
60
61 // Handler for GPU crash dump callbacks.
62 void OnCrashDump(const void* pGpuCrashDump, const uint32_t gpuCrashDumpSize);
63
64 // Handler for shader debug information callbacks.
65 void OnShaderDebugInfo(const void* pShaderDebugInfo, const uint32_t shaderDebugInfoSize);
66
67 // Handler for GPU crash dump description callbacks.
68 void OnDescription(PFN_GFSDK_Aftermath_AddGpuCrashDumpDescription addDescription);
69
70 // Handler for app-managed marker resolve callback
71 void OnResolveMarker(const void* pMarkerData, const uint32_t markerDataSize, PFN_GFSDK_Aftermath_ResolveMarker resolveMarker);
72
73 //*********************************************************
74 // Helpers for writing a GPU crash dump and debug information
75 // data to files.
76 //
77
78 // Helper for writing a GPU crash dump to a file.
79 void WriteGpuCrashDumpToFile(const void* pGpuCrashDump, const uint32_t gpuCrashDumpSize);
80
81 // Helper for writing shader debug information to a file
82 void WriteShaderDebugInformationToFile(
83 GFSDK_Aftermath_ShaderDebugInfoIdentifier identifier,
84 const void* pShaderDebugInfo,
85 const uint32_t shaderDebugInfoSize);
86
87 //*********************************************************
88 // Helpers for decoding GPU crash dump to JSON.
89 //
90
91 // Handler for shader debug info lookup callbacks.
92 void OnShaderDebugInfoLookup(
93 const GFSDK_Aftermath_ShaderDebugInfoIdentifier& identifier,
94 PFN_GFSDK_Aftermath_SetData setShaderDebugInfo) const;
95
96 // Handler for shader lookup callbacks.
97 void OnShaderLookup(
98 const GFSDK_Aftermath_ShaderBinaryHash& shaderHash,
99 PFN_GFSDK_Aftermath_SetData setShaderBinary) const;
100
101 // Handler for shader source debug info lookup callbacks.
102 void OnShaderSourceDebugInfoLookup(
103 const GFSDK_Aftermath_ShaderDebugName& shaderDebugName,
104 PFN_GFSDK_Aftermath_SetData setShaderBinary) const;
105
106 //*********************************************************
107 // Static callback wrappers.
108 //
109
110 // GPU crash dump callback.
111 static void GpuCrashDumpCallback(
112 const void* pGpuCrashDump,
113 const uint32_t gpuCrashDumpSize,
114 void* pUserData);
115
116 // Shader debug information callback.
117 static void ShaderDebugInfoCallback(
118 const void* pShaderDebugInfo,
119 const uint32_t shaderDebugInfoSize,
120 void* pUserData);
121
122 // GPU crash dump description callback.
123 static void CrashDumpDescriptionCallback(
124 PFN_GFSDK_Aftermath_AddGpuCrashDumpDescription addDescription,
125 void* pUserData);
126
127 // App-managed marker resolve callback
128 static void ResolveMarkerCallback(
129 const void* pMarkerData,
130 const uint32_t markerDataSize,
131 void* pUserData,
132 PFN_GFSDK_Aftermath_ResolveMarker resolveMarker);
133
134 // Shader debug information lookup callback.
135 static void ShaderDebugInfoLookupCallback(
136 const GFSDK_Aftermath_ShaderDebugInfoIdentifier* pIdentifier,
137 PFN_GFSDK_Aftermath_SetData setShaderDebugInfo,
138 void* pUserData);
139
140 // Shader lookup callback.
141 static void ShaderLookupCallback(
142 const GFSDK_Aftermath_ShaderBinaryHash* pShaderHash,
143 PFN_GFSDK_Aftermath_SetData setShaderBinary,
144 void* pUserData);
145
146 // Shader source debug info lookup callback.
147 static void ShaderSourceDebugInfoLookupCallback(
148 const GFSDK_Aftermath_ShaderDebugName* pShaderDebugName,
149 PFN_GFSDK_Aftermath_SetData setShaderBinary,
150 void* pUserData);
151
152 //*********************************************************
153 // GPU crash tracker state.
154 //
155
156 // Is the GPU crash dump tracker initialized?
157 bool m_initialized;
158
159 // For thread-safe access of GPU crash tracker state.
160 mutable std::mutex m_mutex;
161
162 // List of Shader Debug Information by ShaderDebugInfoIdentifier.
163 std::map<GFSDK_Aftermath_ShaderDebugInfoIdentifier, std::vector<uint8_t>> m_shaderDebugInfo;
164
165 // The mock shader database.
166 ShaderDatabase m_shaderDatabase;
167
168 // App-managed marker tracking
169 const MarkerMap& m_markerMap;
170
171 // Optional: command line string for crash dump description
172 std::string m_commandLine;
173};
Definition NsightAftermathShaderDatabase.h:41