Skip to content

Commit

Permalink
sdlgpu: Update to latest SDL3 ABI, pre-cache uniform buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Sep 10, 2024
1 parent 9485200 commit 7b06174
Show file tree
Hide file tree
Showing 2 changed files with 335 additions and 183 deletions.
38 changes: 20 additions & 18 deletions mojoshader_sdlgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct MOJOSHADER_sdlShaderData
uint16_t tag;
uint32_t refcount;
uint32_t samplerSlots;
int32_t uniformBufferSize;
};

struct MOJOSHADER_sdlProgram
Expand Down Expand Up @@ -261,7 +262,8 @@ static void update_uniform_buffer(

unsigned int MOJOSHADER_sdlGetShaderFormats(void)
{
return SDL_ShaderCross_GetShaderFormats();
SDL_ShaderCross_Init();
return SDL_ShaderCross_GetSPIRVShaderFormats();
} // MOJOSHADER_sdlGetShaderFormats

MOJOSHADER_sdlContext *MOJOSHADER_sdlCreateContext(
Expand Down Expand Up @@ -311,6 +313,8 @@ void MOJOSHADER_sdlDestroyContext(
hash_destroy(ctx->linker_cache, ctx);

ctx->free_fn(ctx, ctx->malloc_data);

SDL_ShaderCross_Quit();
} // MOJOSHADER_sdlDestroyContext

static uint16_t shaderTagCounter = 1;
Expand Down Expand Up @@ -367,6 +371,13 @@ MOJOSHADER_sdlShaderData *MOJOSHADER_sdlCompileShader(

shader->samplerSlots = (uint32_t) maxSamplerIndex + 1;

shader->uniformBufferSize = 0;
for (i = 0; i < pd->uniform_count; i++)
{
shader->uniformBufferSize += SDL_max(pd->uniforms[i].array_count, 1);
} // for
shader->uniformBufferSize *= 16; // Yes, even the bool registers are this size

return shader;

parse_shader_fail:
Expand Down Expand Up @@ -471,12 +482,12 @@ MOJOSHADER_sdlProgram *MOJOSHADER_sdlLinkProgram(

SDL_zero(createInfo);
createInfo.code = (const Uint8*) vshader->parseData->output;
createInfo.codeSize = vshader->parseData->output_len - sizeof(SpirvPatchTable);
createInfo.entryPointName = vshader->parseData->mainfn;
createInfo.code_size = vshader->parseData->output_len - sizeof(SpirvPatchTable);
createInfo.entrypoint = vshader->parseData->mainfn;
createInfo.format = SDL_GPU_SHADERFORMAT_SPIRV;
createInfo.stage = SDL_GPU_SHADERSTAGE_VERTEX;
createInfo.samplerCount = vshader->samplerSlots;
createInfo.uniformBufferCount = 1;
createInfo.num_samplers = vshader->samplerSlots;
createInfo.num_uniform_buffers = 1;

program->vertexShader = SDL_ShaderCross_CompileFromSPIRV(
ctx->device,
Expand All @@ -492,11 +503,11 @@ MOJOSHADER_sdlProgram *MOJOSHADER_sdlLinkProgram(
} // if

createInfo.code = (const Uint8*) pshader->parseData->output;
createInfo.codeSize = pshader->parseData->output_len - sizeof(SpirvPatchTable);
createInfo.entryPointName = pshader->parseData->mainfn;
createInfo.code_size = pshader->parseData->output_len - sizeof(SpirvPatchTable);
createInfo.entrypoint = pshader->parseData->mainfn;
createInfo.format = SDL_GPU_SHADERFORMAT_SPIRV;
createInfo.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
createInfo.samplerCount = pshader->samplerSlots;
createInfo.num_samplers = pshader->samplerSlots;

program->pixelShader = SDL_ShaderCross_CompileFromSPIRV(
ctx->device,
Expand Down Expand Up @@ -648,16 +659,7 @@ void MOJOSHADER_sdlUnmapUniformBufferMemory(MOJOSHADER_sdlContext *ctx)

int MOJOSHADER_sdlGetUniformBufferSize(MOJOSHADER_sdlShaderData *shader)
{
int32_t i;
int32_t buflen = 0;
const int32_t uniformSize = 16; // Yes, even the bool registers
for (i = 0; i < shader->parseData->uniform_count; i++)
{
const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
buflen += (arrayCount ? arrayCount : 1) * uniformSize;
} // for

return buflen;
return shader->uniformBufferSize;
} // MOJOSHADER_sdlGetUniformBufferSize

void MOJOSHADER_sdlUpdateUniformBuffers(MOJOSHADER_sdlContext *ctx,
Expand Down
Loading

0 comments on commit 7b06174

Please sign in to comment.