Skip to content

Commit

Permalink
Add point_size fixup for MSL + sdlgpu (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSpydog authored Nov 5, 2024
1 parent 32ae391 commit 01cc1f3
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions mojoshader_sdlgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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

Expand Down

0 comments on commit 01cc1f3

Please sign in to comment.