SpiecsEngine
 
Loading...
Searching...
No Matches
NsightAftermathShaderDatabase.cpp
Go to the documentation of this file.
1/**
2* @file NsightAftermathShaderDataBase.cpp
3* @brief The ShaderDataBase Class Implementation.
4* @author NVIDIA
5* @see https://github.com/NVIDIA/nsight-aftermath-samples
6*/
7
8#include "Pchheader.h"
9#include <fstream>
10#include <iomanip>
11#include <winuser.h>
12
14
15namespace Spices {
16
20 {}
21
22 bool ShaderDataBase::ReadFile(const char* filename, std::vector<uint8_t>& data)
23 {
25
26 std::ifstream fs(filename, std::ios::in | std::ios::binary);
27 if (!fs)
28 {
29 return false;
30 }
31
32 fs.seekg(0, std::ios::end);
33 data.resize(fs.tellg());
34 fs.seekg(0, std::ios::beg);
35 fs.read(reinterpret_cast<char*>(data.data()), data.size());
36 fs.close();
37
38 return true;
39 }
40
41 void ShaderDataBase::AddShaderBinary(const char* shaderFilePath)
42 {
44
45 /**
46 * @brief Read the shader binary code from the file.
47 */
48 std::vector<uint8_t> data;
49 if (!ReadFile(shaderFilePath, data))
50 {
51 return;
52 }
53
54 /**
55 * @brief Create shader hash for the shader.
56 */
57 const GFSDK_Aftermath_SpirvCode shader{ data.data(), uint32_t(data.size()) };
58 GFSDK_Aftermath_ShaderBinaryHash shaderHash;
59 AFTERMATH_CHECK_ERROR(GFSDK_Aftermath_GetShaderHashSpirv(
60 GFSDK_Aftermath_Version_API ,
61 &shader ,
62 &shaderHash
63 ));
64
65 /**
66 * @brief Store the data for shader mapping when decoding GPU crash dumps.
67 * cf. FindShaderBinary()
68 */
69 m_ShaderBinaries[shaderHash].swap(data);
70 }
71
72 void ShaderDataBase::AddShaderSource(std::vector<uint8_t> spirv)
73 {
75
76 /**
77 * @brief Create shader hash for the shader.
78 */
79 const GFSDK_Aftermath_SpirvCode shader{ spirv.data(), static_cast<uint32_t>(spirv.size()) };
80 GFSDK_Aftermath_ShaderBinaryHash shaderHash;
81 AFTERMATH_CHECK_ERROR(GFSDK_Aftermath_GetShaderHashSpirv(
82 GFSDK_Aftermath_Version_API ,
83 &shader ,
84 &shaderHash
85 ));
86
87 /**
88 * @brief Store the data for shader mapping when decoding GPU crash dumps.
89 * cf. FindShaderBinary()
90 */
91 m_ShaderBinaries[shaderHash].swap(spirv);
92 }
93
94 void ShaderDataBase::AddShaderBinaryWithDebugInfo(const char* strippedShaderFilePath, const char* shaderFilePath)
95 {
97
98 /**
99 * @brief Read the shader debug data from the file.
100 */
101 std::vector<uint8_t> data;
102 if (!ReadFile(shaderFilePath, data))
103 {
104 return;
105 }
106 std::vector<uint8_t> strippedData;
107 if (!ReadFile(strippedShaderFilePath, strippedData))
108 {
109 return;
110 }
111
112 /**
113 * @brief Generate shader debug name.
114 */
115 GFSDK_Aftermath_ShaderDebugName debugName;
116 const GFSDK_Aftermath_SpirvCode shader{ data.data(), static_cast<uint32_t>(data.size()) };
117 const GFSDK_Aftermath_SpirvCode strippedShader{ strippedData.data(), static_cast<uint32_t>(strippedData.size()) };
118 AFTERMATH_CHECK_ERROR(GFSDK_Aftermath_GetShaderDebugNameSpirv(
119 GFSDK_Aftermath_Version_API ,
120 &shader ,
121 &strippedShader ,
122 &debugName
123 ));
124
125 /**
126 * @brief Store the data for shader instruction address mapping when decoding GPU crash dumps.
127 * cf. FindShaderBinaryWithDebugData()
128 */
129 m_ShaderBinariesWithDebugInfo[debugName].swap(data);
130 }
131
132 void ShaderDataBase::AddShaderSourceWithDebugInfo(std::vector<uint8_t> strippedSpirv, std::vector<uint8_t> spirv)
133 {
135
136 /**
137 * @brief Generate shader debug name.
138 */
139 GFSDK_Aftermath_ShaderDebugName debugName;
140 const GFSDK_Aftermath_SpirvCode shader{ spirv.data(), static_cast<uint32_t>(spirv.size()) };
141 const GFSDK_Aftermath_SpirvCode strippedShader{ strippedSpirv.data(), static_cast<uint32_t>(strippedSpirv.size()) };
142 AFTERMATH_CHECK_ERROR(GFSDK_Aftermath_GetShaderDebugNameSpirv(
143 GFSDK_Aftermath_Version_API ,
144 &shader ,
145 &strippedShader ,
146 &debugName
147 ));
148
149 /**
150 * @brief Store the data for shader instruction address mapping when decoding GPU crash dumps.
151 * cf. FindShaderBinaryWithDebugData()
152 */
153 m_ShaderBinariesWithDebugInfo[debugName].swap(spirv);
154 }
155
156 bool ShaderDataBase::FindShaderBinary(const GFSDK_Aftermath_ShaderBinaryHash& shaderHash, std::vector<uint8_t>& shader) const
157 {
159
160 /**
161 * @brief Find shader binary data for the shader hash.
162 */
163 const auto i_shader = m_ShaderBinaries.find(shaderHash);
164 if (i_shader == m_ShaderBinaries.end())
165 {
166 /**
167 * @brief Nothing found.
168 */
169 return false;
170 }
171
172 shader = i_shader->second;
173 return true;
174 }
175
176 bool ShaderDataBase::FindShaderBinaryWithDebugData(const GFSDK_Aftermath_ShaderDebugName& shaderDebugName, std::vector<uint8_t>& shader) const
177 {
179
180 /**
181 * @brief Find shader binary for the shader debug name.
182 */
183 const auto i_shader = m_ShaderBinariesWithDebugInfo.find(shaderDebugName);
184 if (i_shader == m_ShaderBinariesWithDebugInfo.end())
185 {
186 /**
187 * @brief Nothing found.
188 */
189 return false;
190 }
191
192 shader = i_shader->second;
193 return true;
194 }
195
196}
#define AFTERMATH_CHECK_ERROR(FC)
Helper macro for checking Nsight Aftermath results and throwing exception in case of a failure.
#define SPICES_PROFILE_ZONE
void AddShaderSource(std::vector< uint8_t > spirv)
Add a shader source to m_ShaderBinaries.
bool FindShaderBinaryWithDebugData(const GFSDK_Aftermath_ShaderDebugName &shaderDebugName, std::vector< uint8_t > &shader) const
Find a source shader debug info by shader debug name generated by the DXC compiler.
void AddShaderBinary(const char *shaderFilePath)
Add a shader binary to m_ShaderBinaries.
void AddShaderBinaryWithDebugInfo(const char *strippedShaderFilePath, const char *shaderFilePath)
Add a shader binary with debug info to m_ShaderBinariesWithDebugInfo.
bool FindShaderBinary(const GFSDK_Aftermath_ShaderBinaryHash &shaderHash, std::vector< uint8_t > &shader) const
Find a shader bytecode binary by shader hash.
void AddShaderSourceWithDebugInfo(std::vector< uint8_t > strippedSpirv, std::vector< uint8_t > spirv)
Add a shader Source with debug info to m_ShaderBinariesWithDebugInfo.
Implements a very simple shader database to help demonstrate how to use the Nsight Aftermath GPU cras...