From 01cc1f3cbab5454284bf2d544f642c46da8da26e Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Mon, 4 Nov 2024 21:11:25 -0500 Subject: [PATCH] Add point_size fixup for MSL + sdlgpu (#75) --- mojoshader_sdlgpu.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/mojoshader_sdlgpu.c b/mojoshader_sdlgpu.c index d871604..b936dd0 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,39 @@ 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 = (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 + 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++; + *ptr = '2'; + } // 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 +699,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; @@ -687,6 +719,9 @@ static MOJOSHADER_sdlProgram *compile_program( return NULL; } // if + if (pshaderSource != pshader->parseData->output) + ctx->free_fn(pshaderSource, ctx->malloc_data); + return program; } // compile_program