From 6e2fcaf2b485ab426976931f6cc23d4a64f88be7 Mon Sep 17 00:00:00 2001 From: Naman Dixit Date: Sun, 1 Dec 2024 11:06:07 +0530 Subject: [PATCH] Added `--debuginfo` argument to create debugging information For use with RenderDoc --- include/SDL3_shadercross/SDL_shadercross.h | 21 +++-- src/SDL_shadercross.c | 103 ++++++++++++++------- src/cli.c | 29 ++++-- 3 files changed, 107 insertions(+), 46 deletions(-) diff --git a/include/SDL3_shadercross/SDL_shadercross.h b/include/SDL3_shadercross/SDL_shadercross.h index 08cc9e0..e39b4c2 100644 --- a/include/SDL3_shadercross/SDL_shadercross.h +++ b/include/SDL3_shadercross/SDL_shadercross.h @@ -137,7 +137,8 @@ extern SDL_DECLSPEC void * SDLCALL SDL_ShaderCross_CompileDXBCFromSPIRV( size_t bytecodeSize, const char *entrypoint, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size); + size_t *size, + bool debugInfoEnabled); /** * Compile DXIL bytecode from SPIRV code. @@ -155,7 +156,8 @@ extern SDL_DECLSPEC void * SDLCALL SDL_ShaderCross_CompileDXILFromSPIRV( size_t bytecodeSize, const char *entrypoint, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size); + size_t *size, + bool debugInfoEnabled); /** * Compile an SDL GPU shader from SPIRV code. @@ -255,7 +257,8 @@ extern SDL_DECLSPEC void * SDLCALL SDL_ShaderCross_CompileDXBCFromHLSL( char **defines, Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size); + size_t *size, + bool debugInfoEnabled); /** * Compile to DXIL bytecode from HLSL code via a SPIRV-Cross round trip. @@ -280,7 +283,8 @@ extern SDL_DECLSPEC void * SDLCALL SDL_ShaderCross_CompileDXILFromHLSL( char **defines, Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size); + size_t *size, + bool debugInfoEnabled); /** * Compile to SPIRV bytecode from HLSL code. @@ -305,7 +309,8 @@ extern SDL_DECLSPEC void * SDLCALL SDL_ShaderCross_CompileSPIRVFromHLSL( char **defines, Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size); + size_t *size, + bool debugInfoEnabled); /** * Compile an SDL GPU shader from HLSL code. @@ -330,7 +335,8 @@ extern SDL_DECLSPEC SDL_GPUShader * SDLCALL SDL_ShaderCross_CompileGraphicsShade char **defines, Uint32 numDefines, SDL_GPUShaderStage graphicsShaderStage, - SDL_ShaderCross_GraphicsShaderInfo *info); + SDL_ShaderCross_GraphicsShaderInfo *info, + bool debugInfoEnabled); /** * Compile an SDL GPU compute pipeline from code. @@ -353,7 +359,8 @@ extern SDL_DECLSPEC SDL_GPUComputePipeline * SDLCALL SDL_ShaderCross_CompileComp const char *includeDir, char **defines, Uint32 numDefines, - SDL_ShaderCross_ComputePipelineInfo *info); + SDL_ShaderCross_ComputePipelineInfo *info, + bool debugInfoEnabled); #ifdef __cplusplus } diff --git a/src/SDL_shadercross.c b/src/SDL_shadercross.c index fd9af40..e72a81f 100644 --- a/src/SDL_shadercross.c +++ b/src/SDL_shadercross.c @@ -336,7 +336,8 @@ static void *SDL_ShaderCross_INTERNAL_CompileUsingDXC( Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, bool spirv, - size_t *size) // filled in with number of bytes of returned buffer + size_t *size, // filled in with number of bytes of returned buffer + bool debugInfoEnabled) { #ifdef SDL_SHADERCROSS_DXC DxcBuffer source; @@ -459,6 +460,16 @@ static void *SDL_ShaderCross_INTERNAL_CompileUsingDXC( args[argCount++] = (LPCWSTR)L"-spirv"; } + if (debugInfoEnabled) { + if (spirv) { + // https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#debugging + args[argCount++] = (LPCWSTR)L"-fspv-debug=vulkan-with-source"; + } else { // DXIL + // https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SourceLevelDebuggingHLSL.rst#command-line-options + args[argCount++] = (LPCWSTR)L"-Zi"; + } + } + #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES) args[argCount++] = L"-D__XBOX_DISABLE_PRECOMPILE=1"; #endif @@ -547,7 +558,8 @@ void *SDL_ShaderCross_CompileDXILFromHLSL( char **defines, Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size) + size_t *size, + bool debugInfoEnabled) { // Roundtrip to SPIR-V to support things like Structured Buffers. size_t spirvSize; @@ -558,7 +570,8 @@ void *SDL_ShaderCross_CompileDXILFromHLSL( defines, numDefines, shaderStage, - &spirvSize); + &spirvSize, + debugInfoEnabled); if (spirv == NULL) { return NULL; @@ -583,7 +596,8 @@ void *SDL_ShaderCross_CompileDXILFromHLSL( numDefines, shaderStage, false, - size); + size, + debugInfoEnabled); } void *SDL_ShaderCross_CompileSPIRVFromHLSL( @@ -593,7 +607,8 @@ void *SDL_ShaderCross_CompileSPIRVFromHLSL( char **defines, Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size) + size_t *size, + bool debugInfoEnabled) { return SDL_ShaderCross_INTERNAL_CompileUsingDXC( hlslSource, @@ -603,7 +618,8 @@ void *SDL_ShaderCross_CompileSPIRVFromHLSL( numDefines, shaderStage, true, - size); + size, + debugInfoEnabled); } /* DXBC via FXC */ @@ -728,7 +744,8 @@ void *SDL_ShaderCross_INTERNAL_CompileDXBCFromHLSL( Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, bool enableRoundtrip, - size_t *size) // filled in with number of bytes of returned buffer + size_t *size, // filled in with number of bytes of returned buffer + bool debugInfoEnabled) { char *transpiledSource = NULL; @@ -742,7 +759,8 @@ void *SDL_ShaderCross_INTERNAL_CompileDXBCFromHLSL( defines, numDefines, shaderStage, - &spirv_size); + &spirv_size, + debugInfoEnabled); if (spirv == NULL) { return NULL; @@ -799,7 +817,8 @@ void *SDL_ShaderCross_CompileDXBCFromHLSL( char **defines, Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size) // filled in with number of bytes of returned buffer + size_t *size, // filled in with number of bytes of returned buffer + bool debugInfoEnabled) { return SDL_ShaderCross_INTERNAL_CompileDXBCFromHLSL( hlslSource, @@ -809,7 +828,8 @@ void *SDL_ShaderCross_CompileDXBCFromHLSL( numDefines, shaderStage, true, - size); + size, + debugInfoEnabled); } static void *SDL_ShaderCross_INTERNAL_CreateShaderFromHLSL( @@ -820,7 +840,8 @@ static void *SDL_ShaderCross_INTERNAL_CreateShaderFromHLSL( char **defines, Uint32 numDefines, SDL_ShaderCross_ShaderStage shaderStage, - SDL_ShaderCross_GraphicsShaderInfo *info) + SDL_ShaderCross_GraphicsShaderInfo *info, + bool debugInfoEnabled) { size_t bytecodeSize; @@ -832,7 +853,8 @@ static void *SDL_ShaderCross_INTERNAL_CreateShaderFromHLSL( defines, numDefines, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfoEnabled); if (spirv == NULL) { SDL_SetError("%s", "Failed to compile SPIR-V!"); @@ -868,7 +890,8 @@ SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromHLSL( char **defines, Uint32 numDefines, SDL_GPUShaderStage graphicsShaderStage, - SDL_ShaderCross_GraphicsShaderInfo *info) + SDL_ShaderCross_GraphicsShaderInfo *info, + bool debugInfoEnabled) { return (SDL_GPUShader *)SDL_ShaderCross_INTERNAL_CreateShaderFromHLSL( device, @@ -878,7 +901,8 @@ SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromHLSL( defines, numDefines, (SDL_ShaderCross_ShaderStage)graphicsShaderStage, - (void *)info); + (void *)info, + debugInfoEnabled); } SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromHLSL( @@ -888,7 +912,8 @@ SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromHLSL( const char *includeDir, char **defines, Uint32 numDefines, - SDL_ShaderCross_ComputePipelineInfo *info) + SDL_ShaderCross_ComputePipelineInfo *info, + bool debugInfoEnabled) { return (SDL_GPUComputePipeline *)SDL_ShaderCross_INTERNAL_CreateShaderFromHLSL( device, @@ -898,7 +923,8 @@ SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromHLSL( defines, numDefines, SDL_SHADERCROSS_SHADERSTAGE_COMPUTE, - (void *)info); + (void *)info, + debugInfoEnabled); } #include @@ -1946,7 +1972,8 @@ static void *SDL_ShaderCross_INTERNAL_CompileFromSPIRV( const char *entrypoint, SDL_ShaderCross_ShaderStage shaderStage, SDL_GPUShaderFormat targetFormat, - void *info + void *info, + bool debugInfoEnabled ) { spvc_backend backend; unsigned shadermodel = 0; @@ -2007,7 +2034,8 @@ static void *SDL_ShaderCross_INTERNAL_CompileFromSPIRV( 0, shaderStage, false, - &createInfo.code_size); + &createInfo.code_size, + debugInfoEnabled); } else if (targetFormat == SDL_GPU_SHADERFORMAT_DXIL) { createInfo.code = SDL_ShaderCross_CompileDXILFromHLSL( transpileContext->translated_source, @@ -2016,7 +2044,8 @@ static void *SDL_ShaderCross_INTERNAL_CompileFromSPIRV( NULL, 0, shaderStage, - &createInfo.code_size); + &createInfo.code_size, + debugInfoEnabled); } else { // MSL createInfo.code = (const Uint8 *)transpileContext->translated_source; createInfo.code_size = SDL_strlen(transpileContext->translated_source) + 1; @@ -2048,7 +2077,8 @@ static void *SDL_ShaderCross_INTERNAL_CompileFromSPIRV( 0, shaderStage, false, - &createInfo.code_size); + &createInfo.code_size, + debugInfoEnabled); } else if (targetFormat == SDL_GPU_SHADERFORMAT_DXIL) { createInfo.code = SDL_ShaderCross_CompileDXILFromHLSL( transpileContext->translated_source, @@ -2057,7 +2087,8 @@ static void *SDL_ShaderCross_INTERNAL_CompileFromSPIRV( NULL, 0, shaderStage, - &createInfo.code_size); + &createInfo.code_size, + debugInfoEnabled); } else { // MSL createInfo.code = (const Uint8 *)transpileContext->translated_source; createInfo.code_size = SDL_strlen(transpileContext->translated_source) + 1; @@ -2129,7 +2160,8 @@ void *SDL_ShaderCross_CompileDXBCFromSPIRV( size_t bytecodeSize, const char *entrypoint, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size) + size_t *size, + bool debugInfoEnabled) { SPIRVTranspileContext *context = SDL_ShaderCross_INTERNAL_TranspileFromSPIRV( SPVC_BACKEND_HLSL, @@ -2151,7 +2183,8 @@ void *SDL_ShaderCross_CompileDXBCFromSPIRV( 0, shaderStage, false, - size); + size, + debugInfoEnabled); SDL_ShaderCross_INTERNAL_DestroyTranspileContext(context); return result; @@ -2162,7 +2195,8 @@ void *SDL_ShaderCross_CompileDXILFromSPIRV( size_t bytecodeSize, const char *entrypoint, SDL_ShaderCross_ShaderStage shaderStage, - size_t *size) + size_t *size, + bool debugInfoEnabled) { #ifndef SDL_SHADERCROSS_DXC SDL_SetError("%s", "Shadercross was not compiled with DXC support, cannot compile to SPIR-V!"); @@ -2188,7 +2222,8 @@ void *SDL_ShaderCross_CompileDXILFromSPIRV( NULL, 0, shaderStage, - size); + size, + debugInfoEnabled); SDL_ShaderCross_INTERNAL_DestroyTranspileContext(context); return result; @@ -2200,7 +2235,8 @@ static void *SDL_ShaderCross_INTERNAL_CreateShaderFromSPIRV( size_t bytecodeSize, const char *entrypoint, SDL_ShaderCross_ShaderStage shaderStage, - void *info) + void *info, + bool debugInfoEnabled) { SDL_GPUShaderFormat format; @@ -2272,7 +2308,8 @@ static void *SDL_ShaderCross_INTERNAL_CreateShaderFromSPIRV( entrypoint, shaderStage, format, - info); + info, + debugInfoEnabled); } SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromSPIRV( @@ -2281,7 +2318,8 @@ SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromSPIRV( size_t bytecodeSize, const char *entrypoint, SDL_GPUShaderStage shaderStage, - SDL_ShaderCross_GraphicsShaderInfo *info) + SDL_ShaderCross_GraphicsShaderInfo *info, + bool debugInfoEnabled) { return (SDL_GPUShader *)SDL_ShaderCross_INTERNAL_CreateShaderFromSPIRV( device, @@ -2289,7 +2327,8 @@ SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromSPIRV( bytecodeSize, entrypoint, (SDL_ShaderCross_ShaderStage)shaderStage, - info); + info, + debugInfoEnabled); } SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromSPIRV( @@ -2297,7 +2336,8 @@ SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromSPIRV( const Uint8 *bytecode, size_t bytecodeSize, const char *entrypoint, - SDL_ShaderCross_ComputePipelineInfo *info) + SDL_ShaderCross_ComputePipelineInfo *info, + bool debugInfoEnabled) { return (SDL_GPUComputePipeline *)SDL_ShaderCross_INTERNAL_CreateShaderFromSPIRV( device, @@ -2305,7 +2345,8 @@ SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromSPIRV( bytecodeSize, entrypoint, SDL_SHADERCROSS_SHADERSTAGE_COMPUTE, - info); + info, + debugInfoEnabled); } bool SDL_ShaderCross_Init(void) diff --git a/src/cli.c b/src/cli.c index 177066a..073d373 100644 --- a/src/cli.c +++ b/src/cli.c @@ -47,6 +47,7 @@ void print_help(void) SDL_Log("Optional options:\n"); SDL_Log(" %-*s %s", column_width, "-I | --include ", "HLSL include directory. Only used with HLSL source."); SDL_Log(" %-*s %s", column_width, "-D", "HLSL define. Only used with HLSL source. Can be repeated."); + SDL_Log(" %-*s %s", column_width, "--debuginfo", "Generate HLSL debugging information. Only used with HLSL source."); } void write_graphics_reflect_json(SDL_IOStream *outputIO, SDL_ShaderCross_GraphicsShaderInfo *info) @@ -99,6 +100,8 @@ int main(int argc, char *argv[]) Uint32 numDefines = 0; char **defines = NULL; + bool debugInfo = false; + for (int i = 1; i < argc; i += 1) { char *arg = argv[i]; @@ -208,6 +211,8 @@ int main(int argc, char *argv[]) numDefines += 1; defines = SDL_realloc(defines, sizeof(char *) * numDefines); defines[numDefines - 1] = argv[i]; + } else if (strncmp(argv[i], "--debuginfo", strlen("--debuginfo")) == 0) { + debugInfo = true; } else if (SDL_strcmp(arg, "--") == 0) { accept_optionals = false; } else { @@ -309,7 +314,8 @@ int main(int argc, char *argv[]) fileSize, entrypointName, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (buffer == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile DXBC from SPIR-V: %s", SDL_GetError()); result = 1; @@ -326,7 +332,8 @@ int main(int argc, char *argv[]) fileSize, entrypointName, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (buffer == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile DXIL from SPIR-V: %s", SDL_GetError()); result = 1; @@ -418,7 +425,8 @@ int main(int argc, char *argv[]) defines, numDefines, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (buffer == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile DXBC from HLSL: %s", SDL_GetError()); result = 1; @@ -437,7 +445,8 @@ int main(int argc, char *argv[]) defines, numDefines, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (buffer == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile DXIL from HLSL: %s", SDL_GetError()); result = 1; @@ -457,7 +466,8 @@ int main(int argc, char *argv[]) defines, numDefines, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (spirv == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to transpile MSL from HLSL: %s", SDL_GetError()); result = 1; @@ -487,7 +497,8 @@ int main(int argc, char *argv[]) defines, numDefines, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (buffer == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile SPIR-V From HLSL: %s", SDL_GetError()); result = 1; @@ -506,7 +517,8 @@ int main(int argc, char *argv[]) defines, numDefines, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (spirv == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile HLSL to SPIRV: %s", SDL_GetError()); @@ -540,7 +552,8 @@ int main(int argc, char *argv[]) defines, numDefines, shaderStage, - &bytecodeSize); + &bytecodeSize, + debugInfo); if (spirv == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to compile HLSL to SPIRV: %s", SDL_GetError());