GPU: shaders, pipelines & buffersSDL 3.2

SDL_CreateGPUShader

Loads one compiled shader (vertex or fragment) into the device.

Signature

SDL_GPUShader * SDL_CreateGPUShader(SDL_GPUDevice *device, const SDL_GPUShaderCreateInfo *createinfo);

How it works

SDL_GPU takes shaders already compiled, in the device's format (SPIR-V, DXIL or MSL). The create info gives the bytes (code, code_size), the entrypoint name ("main"), the format, the stage (SDL_GPU_SHADERSTAGE_VERTEX or _FRAGMENT), and how many resources the shader uses: num_samplers, num_storage_textures, num_storage_buffers, num_uniform_buffers.

Those counts must match the shader exactly, and resources must sit in the binding slots SDL expects (for SPIR-V: set 0/1 for the vertex stage, 2/3 for the fragment stage). A shader is only an ingredient: it does nothing until it goes into a pipeline, after which you can release it.

Parameters

deviceSDL_GPUDevice *

The device.

createinfoconst SDL_GPUShaderCreateInfo *

Code, entry point, format, stage and resource counts.

Return value

The shader, or NULL on failure.

Example

example.cpp
1size_t size;
2void* code = SDL_LoadFile("shaders/triangle.vert.spv", &size);
3SDL_GPUShaderCreateInfo info{};
4info.code       = static_cast<const Uint8*>(code);
5info.code_size  = size;
6info.entrypoint = "main";
7info.format     = SDL_GPU_SHADERFORMAT_SPIRV;
8info.stage      = SDL_GPU_SHADERSTAGE_VERTEX;
9SDL_GPUShader* vs = SDL_CreateGPUShader(device, &info);
10SDL_free(code);
Official SDL wiki