From afc2d736340b5d0f27393a756341cb8fc3ab767f Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Sun, 3 Nov 2024 20:31:16 -0500 Subject: [PATCH 1/3] Add point_size fixup for MSL+sdlgpu --- mojoshader_sdlgpu.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/mojoshader_sdlgpu.c b/mojoshader_sdlgpu.c index d871604..ade678f 100644 --- a/mojoshader_sdlgpu.c +++ b/mojoshader_sdlgpu.c @@ -597,6 +597,8 @@ static MOJOSHADER_sdlProgram *compile_program( return NULL; } // if + const char *vshaderSource = vshader->parseData->output; + const char *pshaderSource = pshader->parseData->output; size_t vshaderCodeSize = vshader->parseData->output_len; size_t pshaderCodeSize = pshader->parseData->output_len; @@ -645,9 +647,33 @@ static MOJOSHADER_sdlProgram *compile_program( vshaderCodeSize -= sizeof(SpirvPatchTable); pshaderCodeSize -= sizeof(SpirvPatchTable); } + else if (shader_format == SDL_GPU_SHADERFORMAT_MSL) + { + // Handle texcoord0 -> point_coord conversion + if (strstr(vshaderSource, "[[point_size]]")) + { + pshaderSource = strdup(pshader->parseData->output); + + // !!! FIXME: This assumes all texcoord0 attributes in the effect are + // !!! FIXME: actually point coords! It ain't necessarily so! -caleb + const char *repl = "[[ point_coord ]]"; + char *ptr; + while ((ptr = strstr(pshaderSource, "[[user(texcoord0)]]"))) + { + memcpy(ptr, repl, strlen(repl)); + + // "float4" -> "float2" + int spaces = 0; + while (spaces < 2) + if (*(ptr--) == ' ') + spaces++; + memcpy(ptr, "2", sizeof(char)); + } // while + } // if + } SDL_zero(createInfo); - createInfo.code = (const Uint8*) vshader->parseData->output; + createInfo.code = (const Uint8*) vshaderSource; createInfo.code_size = vshaderCodeSize; createInfo.entrypoint = vshader->parseData->mainfn; createInfo.format = shader_format; @@ -667,7 +693,7 @@ static MOJOSHADER_sdlProgram *compile_program( return NULL; } // if - createInfo.code = (const Uint8*) pshader->parseData->output; + createInfo.code = (const Uint8*) pshaderSource; createInfo.code_size = pshaderCodeSize; createInfo.entrypoint = pshader->parseData->mainfn; createInfo.format = shader_format; From ce544d84207a0bdf3ca2a4c9bca9e580c34c02f7 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Mon, 4 Nov 2024 18:03:25 -0500 Subject: [PATCH 2/3] Use manual strdup, free the duplicated pshader string, simplify char assignment --- mojoshader_sdlgpu.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mojoshader_sdlgpu.c b/mojoshader_sdlgpu.c index ade678f..ac9f3b1 100644 --- a/mojoshader_sdlgpu.c +++ b/mojoshader_sdlgpu.c @@ -652,7 +652,13 @@ static MOJOSHADER_sdlProgram *compile_program( // Handle texcoord0 -> point_coord conversion if (strstr(vshaderSource, "[[point_size]]")) { - pshaderSource = strdup(pshader->parseData->output); + pshaderSource = (char *) ctx->malloc_fn(strlen(pshader->parseData->output) + 1, ctx->malloc_data); + if (!pshaderSource) + { + out_of_memory(); + return NULL; + } + strcpy(pshaderSource, pshader->parseData->output); // !!! FIXME: This assumes all texcoord0 attributes in the effect are // !!! FIXME: actually point coords! It ain't necessarily so! -caleb @@ -667,7 +673,7 @@ static MOJOSHADER_sdlProgram *compile_program( while (spaces < 2) if (*(ptr--) == ' ') spaces++; - memcpy(ptr, "2", sizeof(char)); + *ptr = '2'; } // while } // if } @@ -713,6 +719,9 @@ static MOJOSHADER_sdlProgram *compile_program( return NULL; } // if + if (pshaderSource != pshader->parseData->output) + ctx->free_fn(ctx, pshaderSource); + return program; } // compile_program From cd43cb375c98d10b75c0c614cf05843a98da5c7d Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Mon, 4 Nov 2024 20:52:23 -0500 Subject: [PATCH 3/3] free_fn fix --- mojoshader_sdlgpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mojoshader_sdlgpu.c b/mojoshader_sdlgpu.c index ac9f3b1..b936dd0 100644 --- a/mojoshader_sdlgpu.c +++ b/mojoshader_sdlgpu.c @@ -720,7 +720,7 @@ static MOJOSHADER_sdlProgram *compile_program( } // if if (pshaderSource != pshader->parseData->output) - ctx->free_fn(ctx, pshaderSource); + ctx->free_fn(pshaderSource, ctx->malloc_data); return program; } // compile_program