Skip to content

Commit b44867b

Browse files
committed
Merge pull request #77975 from sakrel/shader_debug_info
Add support for GLSL source-level debugging with RenderDoc
2 parents c495eb5 + 80a36ff commit b44867b

5 files changed

Lines changed: 25 additions & 18 deletions

File tree

core/config/engine.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ bool Engine::is_validation_layers_enabled() const {
239239
return use_validation_layers;
240240
}
241241

242+
bool Engine::is_generate_spirv_debug_info_enabled() const {
243+
return generate_spirv_debug_info;
244+
}
245+
242246
void Engine::set_print_error_messages(bool p_enabled) {
243247
CoreGlobals::print_error_enabled = p_enabled;
244248
}

core/config/engine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Engine {
6767
double _physics_interpolation_fraction = 0.0f;
6868
bool abort_on_gpu_errors = false;
6969
bool use_validation_layers = false;
70+
bool generate_spirv_debug_info = false;
7071
int32_t gpu_idx = -1;
7172

7273
uint64_t _process_frames = 0;
@@ -156,6 +157,7 @@ class Engine {
156157

157158
bool is_abort_on_gpu_errors_enabled() const;
158159
bool is_validation_layers_enabled() const;
160+
bool is_generate_spirv_debug_info_enabled() const;
159161
int32_t get_gpu_index() const;
160162

161163
Engine();

drivers/vulkan/vulkan_context.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@ Error VulkanContext::_initialize_device_extensions() {
504504
register_requested_device_extension(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, false);
505505
register_requested_device_extension(VK_KHR_MAINTENANCE_2_EXTENSION_NAME, false);
506506

507+
if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
508+
register_requested_device_extension(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME, true);
509+
}
510+
507511
// TODO consider the following extensions:
508512
// - VK_KHR_spirv_1_4
509513
// - VK_KHR_swapchain_mutable_format

main/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ void Main::print_help(const char *p_binary) {
458458
#if DEBUG_ENABLED
459459
OS::get_singleton()->print(" --gpu-abort Abort on graphics API usage errors (usually validation layer errors). May help see the problem if your system freezes.\n");
460460
#endif
461+
OS::get_singleton()->print(" --generate-spirv-debug-info Generate SPIR-V debug information. This allows source-level shader debugging with RenderDoc.\n");
461462
OS::get_singleton()->print(" --remote-debug <uri> Remote debug (<protocol>://<host/IP>[:<port>], e.g. tcp://127.0.0.1:6007).\n");
462463
OS::get_singleton()->print(" --single-threaded-scene Scene tree runs in single-threaded mode. Sub-thread groups are disabled and run on the main thread.\n");
463464
#if defined(DEBUG_ENABLED)
@@ -1019,6 +1020,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
10191020
} else if (I->get() == "--gpu-abort") {
10201021
Engine::singleton->abort_on_gpu_errors = true;
10211022
#endif
1023+
} else if (I->get() == "--generate-spirv-debug-info") {
1024+
Engine::singleton->generate_spirv_debug_info = true;
10221025
} else if (I->get() == "--tablet-driver") {
10231026
if (I->next()) {
10241027
tablet_driver = I->next()->get();

modules/glslang/register_types.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "glslang_resource_limits.h"
3434

35+
#include "core/config/engine.h"
3536
#include "servers/rendering/rendering_device.h"
3637

3738
#include <glslang/Include/Types.h>
@@ -56,7 +57,6 @@ static Vector<uint8_t> _compile_shader_glsl(RenderingDevice::ShaderStage p_stage
5657

5758
glslang::EShTargetClientVersion ClientVersion = glslang::EShTargetVulkan_1_2;
5859
glslang::EShTargetLanguageVersion TargetVersion = glslang::EShTargetSpv_1_5;
59-
glslang::TShader::ForbidIncluder includer;
6060

6161
if (capabilities->device_family == RenderingDevice::DeviceFamily::DEVICE_VULKAN) {
6262
if (capabilities->version_major == 1 && capabilities->version_minor == 0) {
@@ -127,23 +127,10 @@ static Vector<uint8_t> _compile_shader_glsl(RenderingDevice::ShaderStage p_stage
127127
}
128128

129129
EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
130-
const int DefaultVersion = 100;
131-
std::string pre_processed_code;
132-
133-
//preprocess
134-
if (!shader.preprocess(&DefaultTBuiltInResource, DefaultVersion, ENoProfile, false, false, messages, &pre_processed_code, includer)) {
135-
if (r_error) {
136-
(*r_error) = "Failed pre-process:\n";
137-
(*r_error) += shader.getInfoLog();
138-
(*r_error) += "\n";
139-
(*r_error) += shader.getInfoDebugLog();
140-
}
141-
142-
return ret;
130+
if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
131+
messages = (EShMessages)(messages | EShMsgDebugInfo);
143132
}
144-
//set back..
145-
cs_strings = pre_processed_code.c_str();
146-
shader.setStrings(&cs_strings, 1);
133+
const int DefaultVersion = 100;
147134

148135
//parse
149136
if (!shader.parse(&DefaultTBuiltInResource, DefaultVersion, false, messages)) {
@@ -174,6 +161,13 @@ static Vector<uint8_t> _compile_shader_glsl(RenderingDevice::ShaderStage p_stage
174161
std::vector<uint32_t> SpirV;
175162
spv::SpvBuildLogger logger;
176163
glslang::SpvOptions spvOptions;
164+
165+
if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
166+
spvOptions.generateDebugInfo = true;
167+
spvOptions.emitNonSemanticShaderDebugInfo = true;
168+
spvOptions.emitNonSemanticShaderDebugSource = true;
169+
}
170+
177171
glslang::GlslangToSpv(*program.getIntermediate(stages[p_stage]), SpirV, &logger, &spvOptions);
178172

179173
ret.resize(SpirV.size() * sizeof(uint32_t));
@@ -188,7 +182,7 @@ static Vector<uint8_t> _compile_shader_glsl(RenderingDevice::ShaderStage p_stage
188182
static String _get_cache_key_function_glsl(const RenderingDevice *p_render_device) {
189183
const RD::Capabilities *capabilities = p_render_device->get_device_capabilities();
190184
String version;
191-
version = "SpirVGen=" + itos(glslang::GetSpirvGeneratorVersion()) + ", major=" + itos(capabilities->version_major) + ", minor=" + itos(capabilities->version_minor) + " , subgroup_size=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_SIZE)) + " , subgroup_ops=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_OPERATIONS)) + " , subgroup_in_shaders=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_IN_SHADERS));
185+
version = "SpirVGen=" + itos(glslang::GetSpirvGeneratorVersion()) + ", major=" + itos(capabilities->version_major) + ", minor=" + itos(capabilities->version_minor) + " , subgroup_size=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_SIZE)) + " , subgroup_ops=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_OPERATIONS)) + " , subgroup_in_shaders=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_IN_SHADERS)) + " , debug=" + itos(Engine::get_singleton()->is_generate_spirv_debug_info_enabled());
192186
return version;
193187
}
194188

0 commit comments

Comments
 (0)