Cross-platform shader compiler and runtime library
HLSL shader compilation to multiple backends with reflection extraction.
Offline compilation of HLSL source to all supported GPU backends. Produces nshader_t objects containing compiled blobs and reflection metadata. Uses SDL_shadercross internally.
Shaders must follow SDL3 GPU binding conventions for cross-platform compatibility.
| Backend | Binding Order |
|---|---|
| SPIR-V | Set 0: samplers, RO textures, RO buffers; Set 1: RW textures, RW buffers; Set 2: uniforms |
| DXBC/DXIL | t[n] space0: samplers+RO; u[n] space1: RW; b[n] space2: uniforms |
| MSL | [[buffer]]: uniforms, RO buffers, RW buffers; [[texture]]: samplers, RO textures, RW textures |
| Stage | SPIR-V Sets | DXBC/DXIL Spaces |
|---|---|---|
| Vertex | 0: resources, 1: uniforms | t/s space0, b space1 |
| Fragment | 2: resources, 3: uniforms | t/s space2, b space3 |
Preprocessor define:
name - define namevalue - optional value (NULL for simple defines)Per-stage compilation input:
stage_type - vertex/fragment/computeentry_point - function name in HLSLsource_code - HLSL source stringdefines, num_defines - stage-specific definesFull compilation configuration:
stages, num_stages - stages to compileinclude_dir - single include path (SDL_shadercross limitation)disable_dxil/dxbc/msl/spv - skip specific backendsenable_debug - include debug infodebug_name - identifier for debuggingpreserve_unused_bindings - keep unreferenced resourcesdefines, num_defines - global defines (all stages)nshader_t* nshader_compiler_compile_hlsl(
const nshader_compiler_config_t* config,
nshader_error_list_t* out_errors); // optional
void nshader_error_list_free(nshader_error_list_t* list);
Returns NULL on failure; check out_errors for messages.
nshader_compiler_stage_setup_t stages[] = {
{ NSHADER_STAGE_TYPE_VERTEX, "vs_main", vs_hlsl, NULL, 0 },
{ NSHADER_STAGE_TYPE_FRAGMENT, "fs_main", fs_hlsl, NULL, 0 }
};
nshader_compiler_config_t config = {
.stages = stages,
.num_stages = 2,
.disable_dxbc = true, // skip DX11 backend
.enable_debug = true
};
nshader_error_list_t errors = {0};
nshader_t* shader = nshader_compiler_compile_hlsl(&config, &errors);
if (!shader) {
for (size_t i = 0; i < errors.num_errors; i++)
fprintf(stderr, "Error: %s\n", errors.errors[i]);
}
nshader_error_list_free(&errors);