Added support for using the GPU renderer as an offscreen renderer

SDL_CreateGPURenderer() now allows passing in an existing GPU device and passing in a NULL window to create an offscreen renderer.

Also renamed SDL_SetRenderGPUState() to SDL_SetGPURenderState().
This commit is contained in:
Sam Lantinga
2025-10-01 14:36:56 -07:00
parent 9461db1ec8
commit a864dcac25
36 changed files with 2390 additions and 3981 deletions

View File

@@ -74,6 +74,13 @@ extern "C" {
*/
#define SDL_SOFTWARE_RENDERER "software"
/**
* The name of the GPU renderer.
*
* \since This macro is available since SDL 3.4.0.
*/
#define SDL_GPU_RENDERER "gpu"
/**
* Vertex structure.
*
@@ -285,6 +292,7 @@ extern SDL_DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window *window
*
* With the SDL GPU renderer (since SDL 3.4.0):
*
* - `SDL_PROP_RENDERER_CREATE_GPU_DEVICE_POINTER`: the device to use with the renderer, optional.
* - `SDL_PROP_RENDERER_CREATE_GPU_SHADERS_SPIRV_BOOLEAN`: the app is able to
* provide SPIR-V shaders to SDL_GPURenderState, optional.
* - `SDL_PROP_RENDERER_CREATE_GPU_SHADERS_DXIL_BOOLEAN`: the app is able to
@@ -328,6 +336,7 @@ extern SDL_DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRendererWithProperties(SDL_
#define SDL_PROP_RENDERER_CREATE_SURFACE_POINTER "SDL.renderer.create.surface"
#define SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER "SDL.renderer.create.output_colorspace"
#define SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER "SDL.renderer.create.present_vsync"
#define SDL_PROP_RENDERER_CREATE_GPU_DEVICE_POINTER "SDL.renderer.create.gpu.device"
#define SDL_PROP_RENDERER_CREATE_GPU_SHADERS_SPIRV_BOOLEAN "SDL.renderer.create.gpu.shaders_spirv"
#define SDL_PROP_RENDERER_CREATE_GPU_SHADERS_DXIL_BOOLEAN "SDL.renderer.create.gpu.shaders_dxil"
#define SDL_PROP_RENDERER_CREATE_GPU_SHADERS_MSL_BOOLEAN "SDL.renderer.create.gpu.shaders_msl"
@@ -339,35 +348,40 @@ extern SDL_DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRendererWithProperties(SDL_
#define SDL_PROP_RENDERER_CREATE_VULKAN_PRESENT_QUEUE_FAMILY_INDEX_NUMBER "SDL.renderer.create.vulkan.present_queue_family_index"
/**
* Create a 2D GPU rendering context for a window, with support for the
* specified shader format.
* Create a 2D GPU rendering context.
*
* This is a convenience function to create a SDL GPU backed renderer,
* intended to be used with SDL_GPURenderState. The resulting renderer will
* support shaders in one of the specified shader formats.
* The GPU device to use is passed in as a parameter. If this is NULL, then a device will be created normally and can be retrieved using SDL_GetGPURendererDevice().
*
* If no available GPU driver supports any of the specified shader formats,
* this function will fail.
* The window to use is passed in as a parameter. If this is NULL, then this will become an offscreen renderer. In that case, you should call SDL_SetRenderTarget() to setup rendering to a texture, and then call SDL_RenderPresent() normally to complete drawing a frame.
*
* \param window the window where rendering is displayed.
* \param format_flags a bitflag indicating which shader formats the app is
* able to provide.
* \param device a pointer filled with the associated GPU device, or NULL on
* error.
* \param device the GPU device to use with the renderer, or NULL to create a device.
* \param window the window where rendering is displayed, or NULL to create an offscreen renderer.
* \returns a valid rendering context or NULL if there was an error; call
* SDL_GetError() for more information.
*
* \threadsafety This function should only be called on the main thread.
* \threadsafety If this function is called with a valid GPU device, it should be called on the thread that created the device. If this function is called with a valid window, it should be called on the thread that created the window.
*
* \since This function is available since SDL 3.4.0.
*
* \sa SDL_CreateRendererWithProperties
* \sa SDL_GetGPUShaderFormats
* \sa SDL_GetGPURendererDevice
* \sa SDL_CreateGPUShader
* \sa SDL_CreateGPURenderState
* \sa SDL_SetRenderGPUState
* \sa SDL_SetGPURenderState
*/
extern SDL_DECLSPEC SDL_Renderer * SDLCALL SDL_CreateGPURenderer(SDL_Window *window, SDL_GPUShaderFormat format_flags, SDL_GPUDevice **device);
extern SDL_DECLSPEC SDL_Renderer * SDLCALL SDL_CreateGPURenderer(SDL_GPUDevice *device, SDL_Window *window);
/**
* Return the GPU device used by a renderer.
*
* \param renderer the rendering context.
* \returns the GPU device used by the renderer, or NULL if the renderer is not a GPU renderer; call SDL_GetError() for more information.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.4.0.
*/
extern SDL_DECLSPEC SDL_GPUDevice * SDLCALL SDL_GetGPURendererDevice(SDL_Renderer *renderer);
/**
* Create a 2D software rendering context for a surface.
@@ -382,7 +396,7 @@ extern SDL_DECLSPEC SDL_Renderer * SDLCALL SDL_CreateGPURenderer(SDL_Window *win
* \returns a valid rendering context or NULL if there was an error; call
* SDL_GetError() for more information.
*
* \threadsafety This function should only be called on the main thread.
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.2.0.
*
@@ -2888,7 +2902,7 @@ typedef struct SDL_GPURenderStateCreateInfo
*
* \sa SDL_CreateGPURenderState
* \sa SDL_SetGPURenderStateFragmentUniforms
* \sa SDL_SetRenderGPUState
* \sa SDL_SetGPURenderState
* \sa SDL_DestroyGPURenderState
*/
typedef struct SDL_GPURenderState SDL_GPURenderState;
@@ -2907,7 +2921,7 @@ typedef struct SDL_GPURenderState SDL_GPURenderState;
* \since This function is available since SDL 3.4.0.
*
* \sa SDL_SetGPURenderStateFragmentUniforms
* \sa SDL_SetRenderGPUState
* \sa SDL_SetGPURenderState
* \sa SDL_DestroyGPURenderState
*/
extern SDL_DECLSPEC SDL_GPURenderState * SDLCALL SDL_CreateGPURenderState(SDL_Renderer *renderer, SDL_GPURenderStateCreateInfo *createinfo);
@@ -2948,7 +2962,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetGPURenderStateFragmentUniforms(SDL_GPURe
*
* \since This function is available since SDL 3.4.0.
*/
extern SDL_DECLSPEC bool SDLCALL SDL_SetRenderGPUState(SDL_Renderer *renderer, SDL_GPURenderState *state);
extern SDL_DECLSPEC bool SDLCALL SDL_SetGPURenderState(SDL_Renderer *renderer, SDL_GPURenderState *state);
/**
* Destroy custom GPU render state.