SDL_CreateGPUDevice
Opens the GPU through SDL's modern 3D API, which runs on Vulkan, Direct3D 12 or Metal.
Signature
SDL_GPUDevice * SDL_CreateGPUDevice(SDL_GPUShaderFormat format_flags, bool debug_mode, const char *name);
How it works
SDL_GPU is one API on top of the three modern graphics APIs, with the same ideas (command buffers, passes, pipelines) and far less code than raw Vulkan. The device is the object everything else is created from.
You declare which shader formats you can supply. Shaders are not written in one shared language: Vulkan wants SPIR-V, Direct3D 12 DXIL, Metal MSL. SDL only picks a backend whose format you offered, so ship each shader in several formats (tools such as SDL_shadercross convert one source to all of them).
Parameters
The shader formats you can provide, combined with |.
Common values
- SDL_GPU_SHADERFORMAT_SPIRV
- SPIR-V, for Vulkan.
- SDL_GPU_SHADERFORMAT_DXIL
- DXIL (Shader Model 6.0), for Direct3D 12.
- SDL_GPU_SHADERFORMAT_MSL / METALLIB
- Metal source or precompiled library, for Apple platforms.
true turns on validation (Vulkan validation layers, D3D12 debug layer): slower, but explains mistakes in the log.
NULL to let SDL choose, or "vulkan", "direct3d12", "metal".
Return value
The device, or NULL if no backend accepts your formats.
Example
1SDL_GPUDevice* device = SDL_CreateGPUDevice(
2 SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_MSL,
3 true, // debug while developing
4 nullptr);
5if (!device) { SDL_Log("CreateGPUDevice: %s", SDL_GetError()); return 1; }
6SDL_Log("GPU backend: %s", SDL_GetGPUDeviceDriver(device));