| 1 | /** |
| 2 | * MojoShader; generate shader programs from bytecode of compiled |
| 3 | * Direct3D shaders. |
| 4 | * |
| 5 | * Please see the file LICENSE.txt in the source's root directory. |
| 6 | * |
| 7 | * This file written by Ryan C. Gordon. |
| 8 | */ |
| 9 | |
| 10 | #define __MOJOSHADER_INTERNAL__ 1 |
| 11 | #include "mojoshader_internal.h" |
| 12 | |
| 13 | #ifdef USE_SDL3 /* Private define, for now */ |
| 14 | |
| 15 | #include <SDL3/SDL.h> |
| 16 | |
| 17 | /* SDL_shadercross API */ |
| 18 | |
| 19 | typedef enum SDL_ShaderCross_ShaderStage |
| 20 | { |
| 21 | SDL_SHADERCROSS_SHADERSTAGE_VERTEX, |
| 22 | SDL_SHADERCROSS_SHADERSTAGE_FRAGMENT, |
| 23 | SDL_SHADERCROSS_SHADERSTAGE_COMPUTE |
| 24 | } SDL_ShaderCross_ShaderStage; |
| 25 | |
| 26 | typedef struct SDL_ShaderCross_SPIRV_Info |
| 27 | { |
| 28 | const Uint8 *bytecode; /**< The SPIRV bytecode. */ |
| 29 | size_t bytecode_size; /**< The length of the SPIRV bytecode. */ |
| 30 | const char *entrypoint; /**< The entry point function name for the shader in UTF-8. */ |
| 31 | SDL_ShaderCross_ShaderStage shader_stage; /**< The shader stage to transpile the shader with. */ |
| 32 | bool enable_debug; /**< Allows debug info to be emitted when relevant. Can be useful for graphics debuggers like RenderDoc. */ |
| 33 | const char *name; /**< A UTF-8 name to associate with the shader. Optional, can be NULL. */ |
| 34 | |
| 35 | SDL_PropertiesID props; /**< A properties ID for extensions. Should be 0 if no extensions are needed. */ |
| 36 | } SDL_ShaderCross_SPIRV_Info; |
| 37 | |
| 38 | typedef struct SDL_ShaderCross_GraphicsShaderMetadata SDL_ShaderCross_GraphicsShaderMetadata; |
| 39 | |
| 40 | typedef SDL_GPUShaderFormat (SDLCALL *PFN_SDL_ShaderCross_GetSPIRVShaderFormats)(void); |
| 41 | typedef SDL_ShaderCross_GraphicsShaderMetadata *(SDLCALL *PFN_SDL_ShaderCross_ReflectGraphicsSPIRV)( |
| 42 | const Uint8 *bytecode, |
| 43 | size_t bytecode_size, |
| 44 | SDL_PropertiesID props); |
| 45 | typedef SDL_GPUShader *(SDLCALL *PFN_SDL_ShaderCross_CompileGraphicsShaderFromSPIRV)( |
| 46 | SDL_GPUDevice *device, |
| 47 | const SDL_ShaderCross_SPIRV_Info *info, |
| 48 | const SDL_ShaderCross_GraphicsShaderMetadata *metadata, |
| 49 | SDL_PropertiesID props); |
| 50 | |
| 51 | static SDL_SharedObject *SDL_shadercross_lib = NULL; |
| 52 | static PFN_SDL_ShaderCross_GetSPIRVShaderFormats SDL_ShaderCross_GetSPIRVShaderFormats = NULL; |
| 53 | static PFN_SDL_ShaderCross_ReflectGraphicsSPIRV SDL_ShaderCross_ReflectGraphicsSPIRV = NULL; |
| 54 | static PFN_SDL_ShaderCross_CompileGraphicsShaderFromSPIRV SDL_ShaderCross_CompileGraphicsShaderFromSPIRV = NULL; |
| 55 | |
| 56 | #ifdef _WIN32 |
| 57 | #define SDL_SHADERCROSS_LIB_NAME "SDL3_shadercross.dll" |
| 58 | #elif defined(__APPLE__) |
| 59 | #define SDL_SHADERCROSS_LIB_NAME "libSDL3_shadercross.0.dylib" |
| 60 | #else |
| 61 | #define SDL_SHADERCROSS_LIB_NAME "libSDL3_shadercross.so.0" |
| 62 | #endif |
| 63 | |
| 64 | /* Max entries for each register file type */ |
| 65 | #define MAX_REG_FILE_F 8192 |
| 66 | #define MAX_REG_FILE_I 2047 |
| 67 | #define MAX_REG_FILE_B 2047 |
| 68 | |
| 69 | /* The destination shader format to use */ |
| 70 | static SDL_GPUShaderFormat shader_format = |
| 71 | #ifdef __APPLE__ |
| 72 | SDL_GPU_SHADERFORMAT_MSL; |
| 73 | #else |
| 74 | SDL_GPU_SHADERFORMAT_SPIRV; |
| 75 | #endif |
| 76 | |
| 77 | typedef struct ShaderEntry |
| 78 | { |
| 79 | uint64_t hash; |
| 80 | uint32_t offset; |
| 81 | uint32_t size; |
| 82 | } ShaderEntry; |
| 83 | |
| 84 | typedef struct ShaderBlob |
| 85 | { |
| 86 | uint64_t hash; |
| 87 | void* binary; |
| 88 | } ShaderBlob; |
| 89 | |
| 90 | struct MOJOSHADER_sdlContext |
| 91 | { |
| 92 | SDL_GPUDevice *device; |
| 93 | const char *profile; |
| 94 | |
| 95 | MOJOSHADER_malloc malloc_fn; |
| 96 | MOJOSHADER_free free_fn; |
| 97 | void *malloc_data; |
| 98 | |
| 99 | /* The constant register files... |
| 100 | * !!! FIXME: Man, it kills me how much memory this takes... |
| 101 | * !!! FIXME: ... make this dynamically allocated on demand. |
| 102 | */ |
| 103 | float vs_reg_file_f[MAX_REG_FILE_F * 4]; |
| 104 | int32_t vs_reg_file_i[MAX_REG_FILE_I * 4]; |
| 105 | uint8_t vs_reg_file_b[MAX_REG_FILE_B * 4]; |
| 106 | float ps_reg_file_f[MAX_REG_FILE_F * 4]; |
| 107 | int32_t ps_reg_file_i[MAX_REG_FILE_I * 4]; |
| 108 | uint8_t ps_reg_file_b[MAX_REG_FILE_B * 4]; |
| 109 | |
| 110 | uint8_t *uniform_staging; |
| 111 | uint32_t uniform_staging_length; |
| 112 | |
| 113 | MOJOSHADER_sdlShaderData *bound_vshader_data; |
| 114 | MOJOSHADER_sdlShaderData *bound_pshader_data; |
| 115 | MOJOSHADER_sdlProgram *bound_program; |
| 116 | HashTable *linker_cache; |
| 117 | }; |
| 118 | |
| 119 | struct MOJOSHADER_sdlShaderData |
| 120 | { |
| 121 | const MOJOSHADER_parseData *parseData; |
| 122 | uint16_t tag; |
| 123 | uint32_t refcount; |
| 124 | uint32_t samplerSlots; |
| 125 | int32_t uniformBufferSize; |
| 126 | }; |
| 127 | |
| 128 | struct MOJOSHADER_sdlProgram |
| 129 | { |
| 130 | SDL_GPUShader *vertexShader; |
| 131 | SDL_GPUShader *pixelShader; |
| 132 | MOJOSHADER_sdlShaderData *vertexShaderData; |
| 133 | MOJOSHADER_sdlShaderData *pixelShaderData; |
| 134 | }; |
| 135 | |
| 136 | /* Error state... */ |
| 137 | |
| 138 | static char error_buffer[1024] = { '\0' }; |
| 139 | |
| 140 | static void set_error(const char *str) |
| 141 | { |
| 142 | snprintf(error_buffer, sizeof (error_buffer), "%s", str); |
| 143 | } // set_error |
| 144 | |
| 145 | static inline void out_of_memory(void) |
| 146 | { |
| 147 | set_error("out of memory"); |
| 148 | } // out_of_memory |
| 149 | |
| 150 | /* Internals */ |
| 151 | |
| 152 | typedef struct LinkedShaderData |
| 153 | { |
| 154 | MOJOSHADER_sdlShaderData *vertex; |
| 155 | MOJOSHADER_sdlShaderData *fragment; |
| 156 | MOJOSHADER_vertexAttribute vertexAttributes[16]; |
| 157 | uint32_t vertexAttributeCount; |
| 158 | } LinkedShaderData; |
| 159 | |
| 160 | static uint32_t hash_shaders(const void *sym, void *data) |
| 161 | { |
| 162 | (void) data; |
| 163 | const LinkedShaderData *s = (const LinkedShaderData *) sym; |
| 164 | const uint32_t HASH_FACTOR = 31; |
| 165 | uint32_t hash = s->vertexAttributeCount; |
| 166 | for (uint32_t i = 0; i < s->vertexAttributeCount; i += 1) |
| 167 | { |
| 168 | hash = hash * HASH_FACTOR + s->vertexAttributes[i].usage; |
| 169 | hash = hash * HASH_FACTOR + s->vertexAttributes[i].usageIndex; |
| 170 | hash = hash * HASH_FACTOR + s->vertexAttributes[i].vertexElementFormat; |
| 171 | } |
| 172 | hash = hash * HASH_FACTOR + s->vertex->tag; |
| 173 | hash = hash * HASH_FACTOR + s->fragment->tag; |
| 174 | return hash; |
| 175 | } // hash_shaders |
| 176 | |
| 177 | static int match_shaders(const void *_a, const void *_b, void *data) |
| 178 | { |
| 179 | (void) data; |
| 180 | const LinkedShaderData *a = (const LinkedShaderData *) _a; |
| 181 | const LinkedShaderData *b = (const LinkedShaderData *) _b; |
| 182 | |
| 183 | const uint16_t av = (a->vertex) ? a->vertex->tag : 0; |
| 184 | const uint16_t bv = (b->vertex) ? b->vertex->tag : 0; |
| 185 | if (av != bv) |
| 186 | return 0; |
| 187 | |
| 188 | const uint16_t af = (a->fragment) ? a->fragment->tag : 0; |
| 189 | const uint16_t bf = (b->fragment) ? b->fragment->tag : 0; |
| 190 | if (af != bf) |
| 191 | return 0; |
| 192 | |
| 193 | if (a->vertexAttributeCount != b->vertexAttributeCount) |
| 194 | return 0; |
| 195 | |
| 196 | for (uint32_t i = 0; i < a->vertexAttributeCount; i += 1) |
| 197 | { |
| 198 | if (a->vertexAttributes[i].usage != b->vertexAttributes[i].usage) |
| 199 | { |
| 200 | return 0; |
| 201 | } |
| 202 | if (a->vertexAttributes[i].usageIndex != b->vertexAttributes[i].usageIndex) |
| 203 | { |
| 204 | return 0; |
| 205 | } |
| 206 | if (a->vertexAttributes[i].vertexElementFormat != b->vertexAttributes[i].vertexElementFormat) |
| 207 | { |
| 208 | return 0; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return 1; |
| 213 | } // match_shaders |
| 214 | |
| 215 | static void nuke_shaders( |
| 216 | const void *_ctx, |
| 217 | const void *key, |
| 218 | const void *value, |
| 219 | void *data |
| 220 | ) { |
| 221 | MOJOSHADER_sdlContext *ctx = (MOJOSHADER_sdlContext *) _ctx; |
| 222 | (void) data; |
| 223 | ctx->free_fn((void *) key, ctx->malloc_data); // this was a LinkedShaderData struct. |
| 224 | MOJOSHADER_sdlDeleteProgram(ctx, (MOJOSHADER_sdlProgram *) value); |
| 225 | } // nuke_shaders |
| 226 | |
| 227 | static uint8_t update_uniform_buffer( |
| 228 | MOJOSHADER_sdlContext *ctx, |
| 229 | SDL_GPUCommandBuffer *cb, |
| 230 | MOJOSHADER_sdlShaderData *shader, |
| 231 | float *regF, |
| 232 | int *regI, |
| 233 | uint8_t *regB |
| 234 | ) { |
| 235 | int32_t i, j; |
| 236 | int32_t offset; |
| 237 | uint32_t *contentsI; |
| 238 | |
| 239 | if (shader->uniformBufferSize > ctx->uniform_staging_length) |
| 240 | { |
| 241 | ctx->free_fn(ctx->uniform_staging, ctx->malloc_data); |
| 242 | ctx->uniform_staging = (uint8_t*) ctx->malloc_fn(shader->uniformBufferSize, ctx->malloc_data); |
| 243 | ctx->uniform_staging_length = shader->uniformBufferSize; |
| 244 | } // if |
| 245 | |
| 246 | offset = 0; |
| 247 | for (i = 0; i < shader->parseData->uniform_count; i++) |
| 248 | { |
| 249 | const int32_t index = shader->parseData->uniforms[i].index; |
| 250 | const int32_t arrayCount = shader->parseData->uniforms[i].array_count; |
| 251 | const int32_t size = arrayCount ? arrayCount : 1; |
| 252 | |
| 253 | switch (shader->parseData->uniforms[i].type) |
| 254 | { |
| 255 | case MOJOSHADER_UNIFORM_FLOAT: |
| 256 | memcpy( |
| 257 | ctx->uniform_staging + offset, |
| 258 | ®F[4 * index], |
| 259 | size * 16 |
| 260 | ); |
| 261 | break; |
| 262 | |
| 263 | case MOJOSHADER_UNIFORM_INT: |
| 264 | memcpy( |
| 265 | ctx->uniform_staging + offset, |
| 266 | ®I[4 * index], |
| 267 | size * 16 |
| 268 | ); |
| 269 | break; |
| 270 | |
| 271 | case MOJOSHADER_UNIFORM_BOOL: |
| 272 | contentsI = (uint32_t *) (ctx->uniform_staging + offset); |
| 273 | for (j = 0; j < size; j++) |
| 274 | contentsI[j * 4] = regB[index + j]; |
| 275 | break; |
| 276 | |
| 277 | default: |
| 278 | set_error( |
| 279 | "SOMETHING VERY WRONG HAPPENED WHEN UPDATING UNIFORMS" |
| 280 | ); |
| 281 | assert(0); |
| 282 | break; |
| 283 | } // switch |
| 284 | |
| 285 | offset += size * 16; |
| 286 | } // for |
| 287 | |
| 288 | return 1; // FIXME: Return 0 when uniform data is unchanged |
| 289 | } // update_uniform_buffer |
| 290 | |
| 291 | /* Public API */ |
| 292 | |
| 293 | unsigned int MOJOSHADER_sdlGetShaderFormats(void) |
| 294 | { |
| 295 | int ret = shader_format; |
| 296 | if (SDL_ShaderCross_GetSPIRVShaderFormats != NULL) |
| 297 | ret |= SDL_ShaderCross_GetSPIRVShaderFormats(); |
| 298 | else |
| 299 | { |
| 300 | // We only care about this on Windows, hardcode the DLL name :shrug: |
| 301 | SDL_shadercross_lib = SDL_LoadObject(SDL_SHADERCROSS_LIB_NAME); |
| 302 | if (SDL_shadercross_lib != NULL) |
| 303 | { |
| 304 | SDL_ShaderCross_GetSPIRVShaderFormats = (PFN_SDL_ShaderCross_GetSPIRVShaderFormats) SDL_LoadFunction( |
| 305 | SDL_shadercross_lib, |
| 306 | "SDL_ShaderCross_GetSPIRVShaderFormats" |
| 307 | ); |
| 308 | ret |= SDL_ShaderCross_GetSPIRVShaderFormats(); |
| 309 | SDL_ShaderCross_GetSPIRVShaderFormats = NULL; |
| 310 | SDL_UnloadObject(SDL_shadercross_lib); |
| 311 | SDL_shadercross_lib = NULL; |
| 312 | } // if |
| 313 | } // else |
| 314 | return ret; |
| 315 | } // MOJOSHADER_sdlGetShaderFormats |
| 316 | |
| 317 | MOJOSHADER_sdlContext *MOJOSHADER_sdlCreateContext( |
| 318 | SDL_GPUDevice *device, |
| 319 | MOJOSHADER_malloc m, |
| 320 | MOJOSHADER_free f, |
| 321 | void *malloc_d |
| 322 | ) { |
| 323 | MOJOSHADER_sdlContext* resultCtx; |
| 324 | |
| 325 | if (m == NULL) m = MOJOSHADER_internal_malloc; |
| 326 | if (f == NULL) f = MOJOSHADER_internal_free; |
| 327 | |
| 328 | resultCtx = (MOJOSHADER_sdlContext*) m(sizeof(MOJOSHADER_sdlContext), malloc_d); |
| 329 | if (resultCtx == NULL) |
| 330 | { |
| 331 | out_of_memory(); |
| 332 | goto init_fail; |
| 333 | } // if |
| 334 | |
| 335 | SDL_memset(resultCtx, '\0', sizeof(MOJOSHADER_sdlContext)); |
| 336 | resultCtx->device = device; |
| 337 | resultCtx->malloc_fn = m; |
| 338 | resultCtx->free_fn = f; |
| 339 | resultCtx->malloc_data = malloc_d; |
| 340 | |
| 341 | resultCtx->profile = (shader_format == SDL_GPU_SHADERFORMAT_SPIRV) ? "spirv" : "metal"; |
| 342 | |
| 343 | // We only care about ShaderCross if the device doesn't natively support the profile |
| 344 | if (!(SDL_GetGPUShaderFormats(device) & shader_format)) |
| 345 | { |
| 346 | SDL_shadercross_lib = SDL_LoadObject(SDL_SHADERCROSS_LIB_NAME); |
| 347 | if (SDL_shadercross_lib != NULL) |
| 348 | { |
| 349 | SDL_ShaderCross_GetSPIRVShaderFormats = (PFN_SDL_ShaderCross_GetSPIRVShaderFormats) SDL_LoadFunction( |
| 350 | SDL_shadercross_lib, |
| 351 | "SDL_ShaderCross_GetSPIRVShaderFormats" |
| 352 | ); |
| 353 | SDL_ShaderCross_ReflectGraphicsSPIRV = (PFN_SDL_ShaderCross_ReflectGraphicsSPIRV) SDL_LoadFunction( |
| 354 | SDL_shadercross_lib, |
| 355 | "SDL_ShaderCross_ReflectGraphicsSPIRV" |
| 356 | ); |
| 357 | SDL_ShaderCross_CompileGraphicsShaderFromSPIRV = (PFN_SDL_ShaderCross_CompileGraphicsShaderFromSPIRV) SDL_LoadFunction( |
| 358 | SDL_shadercross_lib, |
| 359 | "SDL_ShaderCross_CompileGraphicsShaderFromSPIRV" |
| 360 | ); |
| 361 | } // if |
| 362 | } // if |
| 363 | |
| 364 | return resultCtx; |
| 365 | |
| 366 | init_fail: |
| 367 | if (resultCtx != NULL) |
| 368 | f(resultCtx, malloc_d); |
| 369 | return NULL; |
| 370 | } // MOJOSHADER_sdlCreateContext |
| 371 | |
| 372 | const char *MOJOSHADER_sdlGetError( |
| 373 | MOJOSHADER_sdlContext *ctx |
| 374 | ) { |
| 375 | return error_buffer; |
| 376 | } // MOJOSHADER_sdlGetError |
| 377 | |
| 378 | void MOJOSHADER_sdlDestroyContext( |
| 379 | MOJOSHADER_sdlContext *ctx |
| 380 | ) { |
| 381 | uint32_t i; |
| 382 | |
| 383 | if (SDL_shadercross_lib != NULL) |
| 384 | { |
| 385 | SDL_ShaderCross_GetSPIRVShaderFormats = NULL; |
| 386 | SDL_ShaderCross_ReflectGraphicsSPIRV = NULL; |
| 387 | SDL_ShaderCross_CompileGraphicsShaderFromSPIRV = NULL; |
| 388 | SDL_UnloadObject(SDL_shadercross_lib); |
| 389 | SDL_shadercross_lib = NULL; |
| 390 | } // if |
| 391 | |
| 392 | if (ctx->linker_cache) |
| 393 | hash_destroy(ctx->linker_cache, ctx); |
| 394 | |
| 395 | ctx->free_fn(ctx->uniform_staging, ctx->malloc_data); |
| 396 | |
| 397 | ctx->free_fn(ctx, ctx->malloc_data); |
| 398 | } // MOJOSHADER_sdlDestroyContext |
| 399 | |
| 400 | static uint16_t shaderTagCounter = 1; |
| 401 | |
| 402 | MOJOSHADER_sdlShaderData *MOJOSHADER_sdlCompileShader( |
| 403 | MOJOSHADER_sdlContext *ctx, |
| 404 | const char *mainfn, |
| 405 | const unsigned char *tokenbuf, |
| 406 | const unsigned int bufsize, |
| 407 | const MOJOSHADER_swizzle *swiz, |
| 408 | const unsigned int swizcount, |
| 409 | const MOJOSHADER_samplerMap *smap, |
| 410 | const unsigned int smapcount |
| 411 | ) { |
| 412 | MOJOSHADER_sdlShaderData *shader = NULL;; |
| 413 | int maxSamplerIndex = 0; |
| 414 | int i; |
| 415 | |
| 416 | const MOJOSHADER_parseData *pd = MOJOSHADER_parse( |
| 417 | ctx->profile, mainfn, |
| 418 | tokenbuf, bufsize, |
| 419 | swiz, swizcount, |
| 420 | smap, smapcount, |
| 421 | ctx->malloc_fn, |
| 422 | ctx->free_fn, |
| 423 | ctx->malloc_data |
| 424 | ); |
| 425 | |
| 426 | if (pd->error_count > 0) |
| 427 | { |
| 428 | set_error(pd->errors[0].error); |
| 429 | goto parse_shader_fail; |
| 430 | } // if |
| 431 | |
| 432 | shader = (MOJOSHADER_sdlShaderData*) ctx->malloc_fn(sizeof(MOJOSHADER_sdlShaderData), ctx->malloc_data); |
| 433 | if (shader == NULL) |
| 434 | { |
| 435 | out_of_memory(); |
| 436 | goto parse_shader_fail; |
| 437 | } // if |
| 438 | |
| 439 | shader->parseData = pd; |
| 440 | shader->refcount = 1; |
| 441 | shader->tag = shaderTagCounter++; |
| 442 | |
| 443 | /* XNA allows empty shader slots in the middle, so we have to find the actual max binding index */ |
| 444 | for (i = 0; i < pd->sampler_count; i += 1) |
| 445 | { |
| 446 | if (pd->samplers[i].index > maxSamplerIndex) |
| 447 | { |
| 448 | maxSamplerIndex = pd->samplers[i].index; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | shader->samplerSlots = (uint32_t) maxSamplerIndex + 1; |
| 453 | |
| 454 | shader->uniformBufferSize = 0; |
| 455 | for (i = 0; i < pd->uniform_count; i++) |
| 456 | { |
| 457 | shader->uniformBufferSize += SDL_max(pd->uniforms[i].array_count, 1); |
| 458 | } // for |
| 459 | shader->uniformBufferSize *= 16; // Yes, even the bool registers are this size |
| 460 | |
| 461 | return shader; |
| 462 | |
| 463 | parse_shader_fail: |
| 464 | MOJOSHADER_freeParseData(pd); |
| 465 | if (shader != NULL) |
| 466 | ctx->free_fn(shader, ctx->malloc_data); |
| 467 | return NULL; |
| 468 | } // MOJOSHADER_sdlCompileShader |
| 469 | |
| 470 | static MOJOSHADER_sdlProgram *compile_program( |
| 471 | MOJOSHADER_sdlContext *ctx, |
| 472 | MOJOSHADER_sdlShaderData *vshader, |
| 473 | MOJOSHADER_sdlShaderData *pshader) |
| 474 | { |
| 475 | SDL_GPUShaderCreateInfo createInfo; |
| 476 | SDL_ShaderCross_SPIRV_Info crossCreateInfo; |
| 477 | SDL_ShaderCross_GraphicsShaderMetadata *whoCares; |
| 478 | MOJOSHADER_sdlProgram *program = (MOJOSHADER_sdlProgram*) ctx->malloc_fn(sizeof(MOJOSHADER_sdlProgram), |
| 479 | ctx->malloc_data); |
| 480 | if (program == NULL) |
| 481 | { |
| 482 | out_of_memory(); |
| 483 | return NULL; |
| 484 | } // if |
| 485 | |
| 486 | char *vshaderSource = (char*) vshader->parseData->output; |
| 487 | char *pshaderSource = (char*) pshader->parseData->output; |
| 488 | size_t vshaderCodeSize = vshader->parseData->output_len; |
| 489 | size_t pshaderCodeSize = pshader->parseData->output_len; |
| 490 | |
| 491 | // Last-minute fixups before we actually invoke the compiler |
| 492 | if (shader_format == SDL_GPU_SHADERFORMAT_SPIRV) |
| 493 | { |
| 494 | vshaderCodeSize -= sizeof(SpirvPatchTable); |
| 495 | pshaderCodeSize -= sizeof(SpirvPatchTable); |
| 496 | } // if |
| 497 | else if (shader_format == SDL_GPU_SHADERFORMAT_MSL) |
| 498 | { |
| 499 | // Handle texcoord0 -> point_coord conversion |
| 500 | if (strstr((char*) vshader->parseData->output, "[[point_size]]")) |
| 501 | { |
| 502 | pshaderSource = (char *) ctx->malloc_fn(strlen(pshader->parseData->output) + 1, ctx->malloc_data); |
| 503 | if (!pshaderSource) |
| 504 | { |
| 505 | out_of_memory(); |
| 506 | return NULL; |
| 507 | } |
| 508 | strcpy(pshaderSource, pshader->parseData->output); |
| 509 | |
| 510 | // !!! FIXME: This assumes all texcoord0 attributes in the effect are |
| 511 | // !!! FIXME: actually point coords! It ain't necessarily so! -caleb |
| 512 | const char *repl = "[[ point_coord ]]"; |
| 513 | char *ptr; |
| 514 | while ((ptr = strstr(pshaderSource, "[[user(texcoord0)]]"))) |
| 515 | { |
| 516 | memcpy(ptr, repl, strlen(repl)); |
| 517 | |
| 518 | // "float4" -> "float2" |
| 519 | int spaces = 0; |
| 520 | while (spaces < 2) |
| 521 | if (*(ptr--) == ' ') |
| 522 | spaces++; |
| 523 | *ptr = '2'; |
| 524 | } // while |
| 525 | } // if |
| 526 | } // else if |
| 527 | |
| 528 | if (SDL_ShaderCross_CompileGraphicsShaderFromSPIRV != NULL) |
| 529 | { |
| 530 | crossCreateInfo.bytecode = (const Uint8*) vshaderSource; |
| 531 | crossCreateInfo.bytecode_size = vshaderCodeSize; |
| 532 | crossCreateInfo.entrypoint = vshader->parseData->mainfn; |
| 533 | crossCreateInfo.shader_stage = SDL_SHADERCROSS_SHADERSTAGE_VERTEX; |
| 534 | crossCreateInfo.enable_debug = 0; |
| 535 | crossCreateInfo.name = NULL; |
| 536 | crossCreateInfo.props = 0; |
| 537 | |
| 538 | whoCares = SDL_ShaderCross_ReflectGraphicsSPIRV( |
| 539 | crossCreateInfo.bytecode, |
| 540 | crossCreateInfo.bytecode_size, |
| 541 | 0 |
| 542 | ); |
| 543 | |
| 544 | program->vertexShader = SDL_ShaderCross_CompileGraphicsShaderFromSPIRV( |
| 545 | ctx->device, |
| 546 | &crossCreateInfo, |
| 547 | whoCares, |
| 548 | 0 |
| 549 | ); |
| 550 | |
| 551 | SDL_free(whoCares); |
| 552 | } // if |
| 553 | else |
| 554 | { |
| 555 | SDL_zero(createInfo); |
| 556 | createInfo.code = (const Uint8*) vshaderSource; |
| 557 | createInfo.code_size = vshaderCodeSize; |
| 558 | createInfo.entrypoint = vshader->parseData->mainfn; |
| 559 | createInfo.format = shader_format; |
| 560 | createInfo.stage = SDL_GPU_SHADERSTAGE_VERTEX; |
| 561 | createInfo.num_samplers = vshader->samplerSlots; |
| 562 | createInfo.num_uniform_buffers = 1; |
| 563 | |
| 564 | program->vertexShader = SDL_CreateGPUShader( |
| 565 | ctx->device, |
| 566 | &createInfo |
| 567 | ); |
| 568 | } // else |
| 569 | |
| 570 | if (program->vertexShader == NULL) |
| 571 | { |
| 572 | set_error(SDL_GetError()); |
| 573 | ctx->free_fn(program, ctx->malloc_data); |
| 574 | return NULL; |
| 575 | } // if |
| 576 | |
| 577 | if (SDL_ShaderCross_CompileGraphicsShaderFromSPIRV != NULL) |
| 578 | { |
| 579 | crossCreateInfo.bytecode = (const Uint8*) pshaderSource; |
| 580 | crossCreateInfo.bytecode_size = pshaderCodeSize; |
| 581 | crossCreateInfo.entrypoint = pshader->parseData->mainfn; |
| 582 | crossCreateInfo.shader_stage = SDL_SHADERCROSS_SHADERSTAGE_FRAGMENT; |
| 583 | crossCreateInfo.enable_debug = 0; |
| 584 | crossCreateInfo.name = NULL; |
| 585 | crossCreateInfo.props = 0; |
| 586 | |
| 587 | whoCares = SDL_ShaderCross_ReflectGraphicsSPIRV( |
| 588 | crossCreateInfo.bytecode, |
| 589 | crossCreateInfo.bytecode_size, |
| 590 | 0 |
| 591 | ); |
| 592 | |
| 593 | program->pixelShader = SDL_ShaderCross_CompileGraphicsShaderFromSPIRV( |
| 594 | ctx->device, |
| 595 | &crossCreateInfo, |
| 596 | whoCares, |
| 597 | 0 |
| 598 | ); |
| 599 | |
| 600 | SDL_free(whoCares); |
| 601 | } // if |
| 602 | else |
| 603 | { |
| 604 | createInfo.code = (const Uint8*) pshaderSource; |
| 605 | createInfo.code_size = pshaderCodeSize; |
| 606 | createInfo.entrypoint = pshader->parseData->mainfn; |
| 607 | createInfo.format = shader_format; |
| 608 | createInfo.stage = SDL_GPU_SHADERSTAGE_FRAGMENT; |
| 609 | createInfo.num_samplers = pshader->samplerSlots; |
| 610 | |
| 611 | program->pixelShader = SDL_CreateGPUShader( |
| 612 | ctx->device, |
| 613 | &createInfo |
| 614 | ); |
| 615 | } // else |
| 616 | |
| 617 | if (program->pixelShader == NULL) |
| 618 | { |
| 619 | set_error(SDL_GetError()); |
| 620 | SDL_ReleaseGPUShader(ctx->device, program->vertexShader); |
| 621 | ctx->free_fn(program, ctx->malloc_data); |
| 622 | return NULL; |
| 623 | } // if |
| 624 | |
| 625 | if (pshaderSource != pshader->parseData->output) |
| 626 | ctx->free_fn(pshaderSource, ctx->malloc_data); |
| 627 | |
| 628 | return program; |
| 629 | } // compile_program |
| 630 | |
| 631 | MOJOSHADER_sdlProgram *MOJOSHADER_sdlLinkProgram( |
| 632 | MOJOSHADER_sdlContext *ctx, |
| 633 | MOJOSHADER_vertexAttribute *vertexAttributes, |
| 634 | int vertexAttributeCount |
| 635 | ) { |
| 636 | MOJOSHADER_sdlProgram *program = NULL; |
| 637 | |
| 638 | MOJOSHADER_sdlShaderData *vshader = ctx->bound_vshader_data; |
| 639 | MOJOSHADER_sdlShaderData *pshader = ctx->bound_pshader_data; |
| 640 | |
| 641 | if ((vshader == NULL) || (pshader == NULL)) /* Both shaders MUST exist! */ |
| 642 | return NULL; |
| 643 | |
| 644 | if (ctx->linker_cache == NULL) |
| 645 | { |
| 646 | ctx->linker_cache = hash_create(NULL, hash_shaders, match_shaders, |
| 647 | nuke_shaders, 0, ctx->malloc_fn, |
| 648 | ctx->free_fn, ctx->malloc_data); |
| 649 | |
| 650 | if (ctx->linker_cache == NULL) |
| 651 | { |
| 652 | out_of_memory(); |
| 653 | return NULL; |
| 654 | } // if |
| 655 | } // if |
| 656 | |
| 657 | LinkedShaderData shaders; |
| 658 | shaders.vertex = vshader; |
| 659 | shaders.fragment = pshader; |
| 660 | memset(shaders.vertexAttributes, 0, sizeof(MOJOSHADER_vertexAttribute) * 16); |
| 661 | shaders.vertexAttributeCount = vertexAttributeCount; |
| 662 | for (int i = 0; i < vertexAttributeCount; i += 1) |
| 663 | { |
| 664 | shaders.vertexAttributes[i] = vertexAttributes[i]; |
| 665 | } |
| 666 | |
| 667 | const void *val = NULL; |
| 668 | |
| 669 | if (hash_find(ctx->linker_cache, &shaders, &val)) |
| 670 | { |
| 671 | ctx->bound_program = (MOJOSHADER_sdlProgram *) val; |
| 672 | return ctx->bound_program; |
| 673 | } |
| 674 | |
| 675 | if (shader_format == SDL_GPU_SHADERFORMAT_SPIRV) |
| 676 | MOJOSHADER_linkSPIRVShaders(vshader->parseData, pshader->parseData, |
| 677 | vertexAttributes, vertexAttributeCount); |
| 678 | program = compile_program(ctx, vshader, pshader); |
| 679 | |
| 680 | if (program == NULL) |
| 681 | return NULL; |
| 682 | |
| 683 | program->vertexShaderData = vshader; |
| 684 | program->pixelShaderData = pshader; |
| 685 | |
| 686 | LinkedShaderData *item = (LinkedShaderData *) ctx->malloc_fn(sizeof (LinkedShaderData), |
| 687 | ctx->malloc_data); |
| 688 | |
| 689 | if (item == NULL) |
| 690 | { |
| 691 | MOJOSHADER_sdlDeleteProgram(ctx, program); |
| 692 | } |
| 693 | |
| 694 | memcpy(item, &shaders, sizeof(LinkedShaderData)); |
| 695 | if (hash_insert(ctx->linker_cache, item, program) != 1) |
| 696 | { |
| 697 | ctx->free_fn(item, ctx->malloc_data); |
| 698 | MOJOSHADER_sdlDeleteProgram(ctx, program); |
| 699 | out_of_memory(); |
| 700 | return NULL; |
| 701 | } |
| 702 | |
| 703 | ctx->bound_program = program; |
| 704 | return program; |
| 705 | } // MOJOSHADER_sdlLinkProgram |
| 706 | |
| 707 | void MOJOSHADER_sdlShaderAddRef(MOJOSHADER_sdlShaderData *shader) |
| 708 | { |
| 709 | if (shader != NULL) |
| 710 | shader->refcount++; |
| 711 | } // MOJOSHADER_sdlShaderAddRef |
| 712 | |
| 713 | void MOJOSHADER_sdlDeleteShader( |
| 714 | MOJOSHADER_sdlContext *ctx, |
| 715 | MOJOSHADER_sdlShaderData *shader |
| 716 | ) { |
| 717 | if (shader != NULL) |
| 718 | { |
| 719 | if (shader->refcount > 1) |
| 720 | shader->refcount--; |
| 721 | else |
| 722 | { |
| 723 | // See if this was bound as an unlinked program anywhere... |
| 724 | if (ctx->linker_cache) |
| 725 | { |
| 726 | const void *key = NULL; |
| 727 | void *iter = NULL; |
| 728 | int morekeys = hash_iter_keys(ctx->linker_cache, &key, &iter); |
| 729 | while (morekeys) |
| 730 | { |
| 731 | const LinkedShaderData *shaders = (const LinkedShaderData *) key; |
| 732 | // Do this here so we don't confuse the iteration by removing... |
| 733 | morekeys = hash_iter_keys(ctx->linker_cache, &key, &iter); |
| 734 | if ((shaders->vertex == shader) || (shaders->fragment == shader)) |
| 735 | { |
| 736 | // Deletes the linked program |
| 737 | hash_remove(ctx->linker_cache, shaders, ctx); |
| 738 | } // if |
| 739 | } // while |
| 740 | } // if |
| 741 | |
| 742 | MOJOSHADER_freeParseData(shader->parseData); |
| 743 | ctx->free_fn(shader, ctx->malloc_data); |
| 744 | } // else |
| 745 | } // if |
| 746 | } // MOJOSHADER_sdlDeleteShader |
| 747 | |
| 748 | const MOJOSHADER_parseData *MOJOSHADER_sdlGetShaderParseData( |
| 749 | MOJOSHADER_sdlShaderData *shader |
| 750 | ) { |
| 751 | return (shader != NULL) ? shader->parseData : NULL; |
| 752 | } // MOJOSHADER_sdlGetShaderParseData |
| 753 | |
| 754 | void MOJOSHADER_sdlDeleteProgram( |
| 755 | MOJOSHADER_sdlContext *ctx, |
| 756 | MOJOSHADER_sdlProgram *p |
| 757 | ) { |
| 758 | if (ctx->bound_program == p) |
| 759 | ctx->bound_program = NULL; |
| 760 | if (p->vertexShader != NULL) |
| 761 | SDL_ReleaseGPUShader(ctx->device, p->vertexShader); |
| 762 | if (p->pixelShader != NULL) |
| 763 | SDL_ReleaseGPUShader(ctx->device, p->pixelShader); |
| 764 | ctx->free_fn(p, ctx->malloc_data); |
| 765 | } // MOJOSHADER_sdlDeleteProgram |
| 766 | |
| 767 | void MOJOSHADER_sdlBindProgram( |
| 768 | MOJOSHADER_sdlContext *ctx, |
| 769 | MOJOSHADER_sdlProgram *p |
| 770 | ) { |
| 771 | ctx->bound_program = p; |
| 772 | } // MOJOSHADER_sdlBindProgram |
| 773 | |
| 774 | void MOJOSHADER_sdlBindShaders( |
| 775 | MOJOSHADER_sdlContext *ctx, |
| 776 | MOJOSHADER_sdlShaderData *vshader, |
| 777 | MOJOSHADER_sdlShaderData *pshader |
| 778 | ) { |
| 779 | MOJOSHADER_sdlProgram *program = NULL; |
| 780 | ctx->bound_vshader_data = vshader; |
| 781 | ctx->bound_pshader_data = pshader; |
| 782 | } // MOJOSHADER_sdlBindShaders |
| 783 | |
| 784 | void MOJOSHADER_sdlGetBoundShaderData( |
| 785 | MOJOSHADER_sdlContext *ctx, |
| 786 | MOJOSHADER_sdlShaderData **vshaderdata, |
| 787 | MOJOSHADER_sdlShaderData **pshaderdata |
| 788 | ) { |
| 789 | if (vshaderdata != NULL) |
| 790 | { |
| 791 | *vshaderdata = ctx->bound_vshader_data; |
| 792 | } // if |
| 793 | if (pshaderdata != NULL) |
| 794 | { |
| 795 | *pshaderdata = ctx->bound_pshader_data; |
| 796 | } // if |
| 797 | } // MOJOSHADER_sdlGetBoundShaderData |
| 798 | |
| 799 | void MOJOSHADER_sdlMapUniformBufferMemory( |
| 800 | MOJOSHADER_sdlContext *ctx, |
| 801 | float **vsf, int **vsi, unsigned char **vsb, |
| 802 | float **psf, int **psi, unsigned char **psb |
| 803 | ) { |
| 804 | *vsf = ctx->vs_reg_file_f; |
| 805 | *vsi = ctx->vs_reg_file_i; |
| 806 | *vsb = ctx->vs_reg_file_b; |
| 807 | *psf = ctx->ps_reg_file_f; |
| 808 | *psi = ctx->ps_reg_file_i; |
| 809 | *psb = ctx->ps_reg_file_b; |
| 810 | } // MOJOSHADER_sdlMapUniformBufferMemory |
| 811 | |
| 812 | void MOJOSHADER_sdlUnmapUniformBufferMemory(MOJOSHADER_sdlContext *ctx) |
| 813 | { |
| 814 | /* no-op! real work done in sdlUpdateUniformBuffers */ |
| 815 | } // MOJOSHADER_sdlUnmapUniformBufferMemory |
| 816 | |
| 817 | int MOJOSHADER_sdlGetUniformBufferSize(MOJOSHADER_sdlShaderData *shader) |
| 818 | { |
| 819 | if (shader == NULL) |
| 820 | return 0; |
| 821 | return shader->uniformBufferSize; |
| 822 | } // MOJOSHADER_sdlGetUniformBufferSize |
| 823 | |
| 824 | void MOJOSHADER_sdlUpdateUniformBuffers(MOJOSHADER_sdlContext *ctx, |
| 825 | SDL_GPUCommandBuffer *cb) |
| 826 | { |
| 827 | if (MOJOSHADER_sdlGetUniformBufferSize(ctx->bound_program->vertexShaderData) > 0) |
| 828 | { |
| 829 | if (update_uniform_buffer(ctx, cb, ctx->bound_program->vertexShaderData, |
| 830 | ctx->vs_reg_file_f, |
| 831 | ctx->vs_reg_file_i, |
| 832 | ctx->vs_reg_file_b)) |
| 833 | { |
| 834 | SDL_PushGPUVertexUniformData( |
| 835 | cb, |
| 836 | 0, |
| 837 | ctx->uniform_staging, |
| 838 | ctx->bound_program->vertexShaderData->uniformBufferSize |
| 839 | ); |
| 840 | } // if |
| 841 | } // if |
| 842 | if (MOJOSHADER_sdlGetUniformBufferSize(ctx->bound_program->pixelShaderData) > 0) |
| 843 | { |
| 844 | if (update_uniform_buffer(ctx, cb, ctx->bound_program->pixelShaderData, |
| 845 | ctx->ps_reg_file_f, |
| 846 | ctx->ps_reg_file_i, |
| 847 | ctx->ps_reg_file_b)) |
| 848 | { |
| 849 | SDL_PushGPUFragmentUniformData( |
| 850 | cb, |
| 851 | 0, |
| 852 | ctx->uniform_staging, |
| 853 | ctx->bound_program->pixelShaderData->uniformBufferSize |
| 854 | ); |
| 855 | } // if |
| 856 | } // if |
| 857 | } // MOJOSHADER_sdlUpdateUniformBuffers |
| 858 | |
| 859 | int MOJOSHADER_sdlGetVertexAttribLocation( |
| 860 | MOJOSHADER_sdlShaderData *vert, |
| 861 | MOJOSHADER_usage usage, int index |
| 862 | ) { |
| 863 | int32_t i; |
| 864 | if (vert == NULL) |
| 865 | return -1; |
| 866 | |
| 867 | for (i = 0; i < vert->parseData->attribute_count; i++) |
| 868 | { |
| 869 | if (vert->parseData->attributes[i].usage == usage && |
| 870 | vert->parseData->attributes[i].index == index) |
| 871 | { |
| 872 | return i; |
| 873 | } // if |
| 874 | } // for |
| 875 | |
| 876 | // failure |
| 877 | return -1; |
| 878 | } // MOJOSHADER_sdlGetVertexAttribLocation |
| 879 | |
| 880 | void MOJOSHADER_sdlGetShaders( |
| 881 | MOJOSHADER_sdlContext *ctx, |
| 882 | SDL_GPUShader **vshader, |
| 883 | SDL_GPUShader **pshader |
| 884 | ) { |
| 885 | assert(ctx->bound_program != NULL); |
| 886 | if (vshader != NULL) |
| 887 | *vshader = ctx->bound_program->vertexShader; |
| 888 | if (pshader != NULL) |
| 889 | *pshader = ctx->bound_program->pixelShader; |
| 890 | } // MOJOSHADER_sdlGetShaders |
| 891 | |
| 892 | unsigned int MOJOSHADER_sdlGetSamplerSlots(MOJOSHADER_sdlShaderData *shader) |
| 893 | { |
| 894 | assert(shader != NULL); |
| 895 | return shader->samplerSlots; |
| 896 | } // MOJOSHADER_sdlGetSamplerSlots |
| 897 | |
| 898 | #endif // USE_SDL3 |