Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --debuginfo argument to create debugging information #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions include/SDL3_shadercross/SDL_shadercross.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a SDL_ShaderCross_CompileFromProperties to avoid breaking the api?


/**
* Compile DXIL bytecode from SPIRV code.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
}
Expand Down
103 changes: 72 additions & 31 deletions src/SDL_shadercross.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -558,7 +570,8 @@ void *SDL_ShaderCross_CompileDXILFromHLSL(
defines,
numDefines,
shaderStage,
&spirvSize);
&spirvSize,
debugInfoEnabled);

if (spirv == NULL) {
return NULL;
Expand All @@ -583,7 +596,8 @@ void *SDL_ShaderCross_CompileDXILFromHLSL(
numDefines,
shaderStage,
false,
size);
size,
debugInfoEnabled);
}

void *SDL_ShaderCross_CompileSPIRVFromHLSL(
Expand All @@ -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,
Expand All @@ -603,7 +618,8 @@ void *SDL_ShaderCross_CompileSPIRVFromHLSL(
numDefines,
shaderStage,
true,
size);
size,
debugInfoEnabled);
}

/* DXBC via FXC */
Expand Down Expand Up @@ -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;

Expand All @@ -742,7 +759,8 @@ void *SDL_ShaderCross_INTERNAL_CompileDXBCFromHLSL(
defines,
numDefines,
shaderStage,
&spirv_size);
&spirv_size,
debugInfoEnabled);

if (spirv == NULL) {
return NULL;
Expand Down Expand Up @@ -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,
Expand All @@ -809,7 +828,8 @@ void *SDL_ShaderCross_CompileDXBCFromHLSL(
numDefines,
shaderStage,
true,
size);
size,
debugInfoEnabled);
}

static void *SDL_ShaderCross_INTERNAL_CreateShaderFromHLSL(
Expand All @@ -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;

Expand All @@ -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!");
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -898,7 +923,8 @@ SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromHLSL(
defines,
numDefines,
SDL_SHADERCROSS_SHADERSTAGE_COMPUTE,
(void *)info);
(void *)info,
debugInfoEnabled);
}

#include <spirv_cross_c.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -2151,7 +2183,8 @@ void *SDL_ShaderCross_CompileDXBCFromSPIRV(
0,
shaderStage,
false,
size);
size,
debugInfoEnabled);

SDL_ShaderCross_INTERNAL_DestroyTranspileContext(context);
return result;
Expand All @@ -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!");
Expand All @@ -2188,7 +2222,8 @@ void *SDL_ShaderCross_CompileDXILFromSPIRV(
NULL,
0,
shaderStage,
size);
size,
debugInfoEnabled);

SDL_ShaderCross_INTERNAL_DestroyTranspileContext(context);
return result;
Expand All @@ -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;

Expand Down Expand Up @@ -2272,7 +2308,8 @@ static void *SDL_ShaderCross_INTERNAL_CreateShaderFromSPIRV(
entrypoint,
shaderStage,
format,
info);
info,
debugInfoEnabled);
}

SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromSPIRV(
Expand All @@ -2281,31 +2318,35 @@ 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,
bytecode,
bytecodeSize,
entrypoint,
(SDL_ShaderCross_ShaderStage)shaderStage,
info);
info,
debugInfoEnabled);
}

SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromSPIRV(
SDL_GPUDevice *device,
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,
bytecode,
bytecodeSize,
entrypoint,
SDL_SHADERCROSS_SHADERSTAGE_COMPUTE,
info);
info,
debugInfoEnabled);
}

bool SDL_ShaderCross_Init(void)
Expand Down
Loading
Loading