From 1384bad2d20bac563a9b8abcdfd9f94c6a0d3400 Mon Sep 17 00:00:00 2001 From: Christoph Neuhauser Date: Wed, 13 Aug 2025 13:41:41 +0200 Subject: [PATCH] GPU: Add flag for shader debug info generation This PR proposes to add a new flag `--shader-debug-info` that enables the generation of shader debug information. I created this PR as WIP due to the following reasons: - Currently it only works for the Vulkan backend. I do not know if it makes sense for other backends. For example, OpenGL directly receives the GLSL code, so there no need for this might exist. - So far `--debug-gpu-renderdoc` already turns on the following changes for GLSL shader compilation with shaderc: ``` options.SetOptimizationLevel(shaderc_optimization_level_zero); options.SetGenerateDebugInfo(); ``` - While combining optimization level zero with debug info is a sensible choice for frame debuggers like RenderDoc, my use case for creating this PR is shader profiling. In this case, one does not want compiler optimizations to be turned off. At the current point in time, the only information my profiler uses (which is unfortunately not public at this point in time) is the name of the shader. When turning on debug information, shaderc/glslang store this information in the generated SPIR-V data. Otherwise, it would be impossible for the profiler to tell the user what the name of the shader it is that is profiled. - An alternative solution would be to rename the entry point `main` of a shader to the name of the shader. But this might be an even uglier hack, as it requires editing the source code (and the name of the shader then needs to be a valid GLSL function name). - We should first clarify if there is interest in the Blender side in upstreaming an option like this. While I could just keep this in my local fork of Blender, there is merit in having the possibility to profile arbitrary Blender builds. Pull Request: https://projects.blender.org/blender/blender/pulls/142986 --- source/blender/blenkernel/BKE_global.hh | 9 +++++---- .../blender/gpu/vulkan/vk_shader_compiler.cc | 2 ++ source/creator/creator_args.cc | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/source/blender/blenkernel/BKE_global.hh b/source/blender/blenkernel/BKE_global.hh index b27d80d911d..b9c5c54653a 100644 --- a/source/blender/blenkernel/BKE_global.hh +++ b/source/blender/blenkernel/BKE_global.hh @@ -258,11 +258,12 @@ enum { G_DEBUG_GPU_FORCE_VULKAN_LOCAL_READ = (1 << 19), /* Force GPU dynamic rendering local read. */ G_DEBUG_GPU_COMPILE_SHADERS = (1 << 20), /* Compile all statically defined shaders. . */ G_DEBUG_GPU_RENDERDOC = (1 << 21), /* Enable RenderDoc integration. */ - G_DEBUG_XR = (1 << 22), /* XR/OpenXR messages */ - G_DEBUG_XR_TIME = (1 << 23), /* XR/OpenXR timing messages */ + G_DEBUG_GPU_SHADER_DEBUG_INFO = (1 << 22), /* Enable the generation of shader debug info. */ + G_DEBUG_XR = (1 << 23), /* XR/OpenXR messages */ + G_DEBUG_XR_TIME = (1 << 24), /* XR/OpenXR timing messages */ - G_DEBUG_GHOST = (1 << 24), /* Debug GHOST module. */ - G_DEBUG_WINTAB = (1 << 25), /* Debug Wintab. */ + G_DEBUG_GHOST = (1 << 25), /* Debug GHOST module. */ + G_DEBUG_WINTAB = (1 << 26), /* Debug Wintab. */ }; #define G_DEBUG_ALL \ diff --git a/source/blender/gpu/vulkan/vk_shader_compiler.cc b/source/blender/gpu/vulkan/vk_shader_compiler.cc index 7b80a0b70b8..5b02c2a612c 100644 --- a/source/blender/gpu/vulkan/vk_shader_compiler.cc +++ b/source/blender/gpu/vulkan/vk_shader_compiler.cc @@ -176,6 +176,8 @@ static bool compile_ex(shaderc::Compiler &compiler, options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_2); if (G.debug & G_DEBUG_GPU_RENDERDOC) { options.SetOptimizationLevel(shaderc_optimization_level_zero); + } + if (G.debug & G_DEBUG_GPU_SHADER_DEBUG_INFO) { options.SetGenerateDebugInfo(); } diff --git a/source/creator/creator_args.cc b/source/creator/creator_args.cc index c4818a9d7bc..c9a3306558d 100644 --- a/source/creator/creator_args.cc +++ b/source/creator/creator_args.cc @@ -1529,13 +1529,24 @@ static int arg_handle_debug_gpu_renderdoc_set(int /*argc*/, void * /*data*/) { # ifdef WITH_RENDERDOC - G.debug |= G_DEBUG_GPU_RENDERDOC | G_DEBUG_GPU; + G.debug |= G_DEBUG_GPU_RENDERDOC | G_DEBUG_GPU | G_DEBUG_GPU_SHADER_DEBUG_INFO; # else BLI_assert_unreachable(); # endif return 0; } +static const char arg_handle_debug_gpu_shader_debug_info_set_doc[] = + "\n" + "\tEnable shader debug info generation (Vulkan only)."; +static int arg_handle_debug_gpu_shader_debug_info_set(int /*argc*/, + const char ** /*argv*/, + void * /*data*/) +{ + G.debug |= G_DEBUG_GPU_SHADER_DEBUG_INFO; + return 0; +} + static const char arg_handle_gpu_backend_set_doc_all[] = "\n" "\tForce to use a specific GPU backend. Valid options: " @@ -2886,6 +2897,11 @@ void main_args_setup(bContext *C, bArgs *ba, bool all) BLI_args_add( ba, nullptr, "--debug-gpu-renderdoc", CB(arg_handle_debug_gpu_renderdoc_set), nullptr); } + BLI_args_add(ba, + nullptr, + "--debug-gpu-shader-debug-info", + CB(arg_handle_debug_gpu_shader_debug_info_set), + nullptr); BLI_args_add(ba, nullptr,