Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GPU] Pipeline caches #11629

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions include/SDL3/SDL_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ typedef struct SDL_GPUComputePipeline SDL_GPUComputePipeline;
*/
typedef struct SDL_GPUGraphicsPipeline SDL_GPUGraphicsPipeline;

/**
* An opaque handle representing a pipeline cache.
*
* Used during pipeline creation.
*
* \since This struct is available since SDL 3.1.3
*
* \sa SDL_CreateGPUPipelineCache
* \sa SDL_ReleaseGPUPipelineCache
* \sa SDL_FetchGPUPipelineCacheData
*/
typedef struct SDL_GPUPipelineCache SDL_GPUPipelineCache;

/**
* An opaque handle representing a command buffer.
*
Expand Down Expand Up @@ -1694,6 +1707,22 @@ typedef struct SDL_GPUComputePipelineCreateInfo
SDL_PropertiesID props; /**< A properties ID for extensions. Should be 0 if no extensions are needed. */
} SDL_GPUComputePipelineCreateInfo;

/**
* A structure specifying the parameters of a pipeline cache.
*
* \since This struct is available since SDL 3.1.3
*
* \sa SDL_CreateGPUPipelineCache
* \sa SDL_FetchGPUPipelineCacheData
*/
typedef struct SDL_GPUPipelineCacheCreateInfo
{
size_t checksum_size; /**< The size in bytes of the cache checksum. */
void* checksum_data; /**< A pointer to the checksum data. */
size_t cache_size; /**< The size in bytes of the pipeline cache. */
void* cache_data; /**< A pointer to the cache data. */
} SDL_GPUPipelineCacheCreateInfo;

/**
* A structure specifying the parameters of a color target used by a render
* pass.
Expand Down Expand Up @@ -2003,6 +2032,8 @@ extern SDL_DECLSPEC SDL_GPUDevice *SDLCALL SDL_CreateGPUDeviceWithProperties(
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN "SDL.gpu.device.create.shaders.msl"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN "SDL.gpu.device.create.shaders.metallib"
#define SDL_PROP_GPU_DEVICE_CREATE_D3D12_SEMANTIC_NAME_STRING "SDL.gpu.device.create.d3d12.semantic"
#define SDL_PROP_GPU_PIPELINE_STORE_CACHE "SDL.gpu.pipeline.store.cache"
#define SDL_PROP_GPU_PIPELINE_USE_CACHE "SDL.gpu.pipeline.use.cache"

/**
* Destroys a GPU context previously returned by SDL_CreateGPUDevice.
Expand Down Expand Up @@ -2130,6 +2161,43 @@ extern SDL_DECLSPEC SDL_GPUGraphicsPipeline *SDLCALL SDL_CreateGPUGraphicsPipeli
SDL_GPUDevice *device,
const SDL_GPUGraphicsPipelineCreateInfo *createinfo);

/**
* Creates a pipeline cache object to be used during pipeline builds.
*
* \param device a GPU Context.
* \param createinfo a struct containing the pipeline cache data. Set everything to NULL/0 in order to force a cache rebuild
* \returns a pipeline cache object on success, or NULL on failure; call
* SDL_GetError() for more information.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_CreatePipelineCache
* \sa SDL_ReleasePipelineCache
* \sa SDL_RetrievePipelineCacheData
*/
extern SDL_DECLSPEC SDL_GPUPipelineCache* SDLCALL SDL_CreateGPUPipelineCache(
SDL_GPUDevice* device,
const SDL_GPUPipelineCacheCreateInfo* createinfo);

/**
* Fetches a pipeline cache object data. This can then be stored to disk and loaded on the next application run
*
* \param device a GPU Context.
* \param pipeline_cache pipeline cache object to fetch data from
* \param createinfo a struct that will contain the cache's serializable data
* \returns true on success, or false on failure; call
* SDL_GetError() for more information.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_CreatePipelineCache
* \sa SDL_ReleasePipelineCache
*/
extern SDL_DECLSPEC bool SDLCALL SDL_FetchGPUPipelineCacheData(
SDL_GPUDevice* device,
SDL_GPUPipelineCache* pipeline_cache,
SDL_GPUPipelineCacheCreateInfo* createinfo);

/**
* Creates a sampler object to be used when binding textures in a graphics
* workflow.
Expand Down Expand Up @@ -2490,6 +2558,20 @@ extern SDL_DECLSPEC void SDLCALL SDL_ReleaseGPUGraphicsPipeline(
SDL_GPUDevice *device,
SDL_GPUGraphicsPipeline *graphics_pipeline);

/**
* Frees the given pipeline cache as soon as it is safe to do so.
*
* You must not reference the pipeline cache after calling this function.
*
* \param device a GPU context.
* \param pipeline_cache a pipeline cache to be destroyed.
*
* \since This function is available since SDL 3.1.3.
*/
extern SDL_DECLSPEC void SDLCALL SDL_ReleaseGPUPipelineCache(
SDL_GPUDevice* device,
SDL_GPUPipelineCache* pipeline_cache);

/**
* Acquire a command buffer.
*
Expand Down
3 changes: 3 additions & 0 deletions src/dynapi/SDL_dynapi.sym
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,9 @@ SDL3_0.0.0 {
SDL_SetGPUAllowedFramesInFlight;
SDL_RenderTextureAffine;
SDL_WaitAndAcquireGPUSwapchainTexture;
SDL_CreateGPUPipelineCache;
SDL_FetchGPUPipelineCacheData;
SDL_ReleaseGPUPipelineCache;
# extra symbols go here (don't modify this line)
local: *;
};
3 changes: 3 additions & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,6 @@
#define SDL_SetGPUAllowedFramesInFlight SDL_SetGPUAllowedFramesInFlight_REAL
#define SDL_RenderTextureAffine SDL_RenderTextureAffine_REAL
#define SDL_WaitAndAcquireGPUSwapchainTexture SDL_WaitAndAcquireGPUSwapchainTexture_REAL
#define SDL_CreateGPUPipelineCache SDL_CreateGPUPipelineCache_REAL
#define SDL_FetchGPUPipelineCacheData SDL_FetchGPUPipelineCacheData_REAL
#define SDL_ReleaseGPUPipelineCache SDL_ReleaseGPUPipelineCache_REAL
3 changes: 3 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,6 @@ SDL_DYNAPI_PROC(bool,SDL_RunOnMainThread,(SDL_MainThreadCallback a,void *b,bool
SDL_DYNAPI_PROC(bool,SDL_SetGPUAllowedFramesInFlight,(SDL_GPUDevice *a,Uint32 b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_RenderTextureAffine,(SDL_Renderer *a,SDL_Texture *b,const SDL_FRect *c,const SDL_FPoint *d,const SDL_FPoint *e,const SDL_FPoint *f),(a,b,c,d,e,f),return)
SDL_DYNAPI_PROC(bool,SDL_WaitAndAcquireGPUSwapchainTexture,(SDL_GPUCommandBuffer *a,SDL_Window *b,SDL_GPUTexture **c,Uint32 *d,Uint32 *e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(SDL_GPUPipelineCache*,SDL_CreateGPUPipelineCache,(SDL_GPUDevice *a,const SDL_GPUPipelineCacheCreateInfo *b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_FetchGPUPipelineCacheData,(SDL_GPUDevice *a,SDL_GPUPipelineCache *b,SDL_GPUPipelineCacheCreateInfo *c),(a,b,c),return)
SDL_DYNAPI_PROC(void,SDL_ReleaseGPUPipelineCache,(SDL_GPUDevice *a,SDL_GPUPipelineCache *b),(a,b),)
45 changes: 45 additions & 0 deletions src/gpu/SDL_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,37 @@ SDL_GPUGraphicsPipeline *SDL_CreateGPUGraphicsPipeline(
graphicsPipelineCreateInfo);
}

SDL_GPUPipelineCache* SDL_CreateGPUPipelineCache(
SDL_GPUDevice* device,
const SDL_GPUPipelineCacheCreateInfo* createinfo)
{
CHECK_DEVICE_MAGIC(device, NULL);
if (createinfo == NULL) {
SDL_InvalidParamError("createinfo");
return NULL;
}

return device->CreatePipelineCache(device->driverData, createinfo);
}

bool SDL_FetchGPUPipelineCacheData(
SDL_GPUDevice* device,
SDL_GPUPipelineCache* pipeline_cache,
SDL_GPUPipelineCacheCreateInfo* createinfo)
{
CHECK_DEVICE_MAGIC(device, false);
if (pipeline_cache == NULL) {
SDL_InvalidParamError("pipeline_cache");
return false;
}
if (createinfo == NULL) {
SDL_InvalidParamError("createinfo");
return false;
}

return device->FetchPipelineCacheData(device->driverData, pipeline_cache, createinfo);
}

SDL_GPUSampler *SDL_CreateGPUSampler(
SDL_GPUDevice *device,
const SDL_GPUSamplerCreateInfo *createinfo)
Expand Down Expand Up @@ -1279,6 +1310,20 @@ void SDL_ReleaseGPUGraphicsPipeline(
graphics_pipeline);
}

void SDL_ReleaseGPUPipelineCache(
SDL_GPUDevice* device,
SDL_GPUPipelineCache* pipeline_cache)
{
CHECK_DEVICE_MAGIC(device, );
if (pipeline_cache == NULL) {
return;
}

device->ReleasePipelineCache(
device->driverData,
pipeline_cache);
}

// Command Buffer

SDL_GPUCommandBuffer *SDL_AcquireGPUCommandBuffer(
Expand Down
16 changes: 16 additions & 0 deletions src/gpu/SDL_sysgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,15 @@ struct SDL_GPUDevice
SDL_GPURenderer *driverData,
const SDL_GPUGraphicsPipelineCreateInfo *createinfo);

SDL_GPUPipelineCache* (*CreatePipelineCache)(
SDL_GPURenderer* driverData,
const SDL_GPUPipelineCacheCreateInfo* createinfo);

bool (*FetchPipelineCacheData)(
SDL_GPURenderer* driverData,
SDL_GPUPipelineCache* pipelineCache,
SDL_GPUPipelineCacheCreateInfo* createinfo);

SDL_GPUSampler *(*CreateSampler)(
SDL_GPURenderer *driverData,
const SDL_GPUSamplerCreateInfo *createinfo);
Expand Down Expand Up @@ -534,6 +543,10 @@ struct SDL_GPUDevice
SDL_GPURenderer *driverData,
SDL_GPUGraphicsPipeline *graphicsPipeline);

void (*ReleasePipelineCache)(
SDL_GPURenderer* driverData,
SDL_GPUPipelineCache* pipelineCache);

// Render Pass

void (*BeginRenderPass)(
Expand Down Expand Up @@ -880,6 +893,8 @@ struct SDL_GPUDevice
ASSIGN_DRIVER_FUNC(DestroyDevice, name) \
ASSIGN_DRIVER_FUNC(CreateComputePipeline, name) \
ASSIGN_DRIVER_FUNC(CreateGraphicsPipeline, name) \
ASSIGN_DRIVER_FUNC(CreatePipelineCache, name) \
ASSIGN_DRIVER_FUNC(FetchPipelineCacheData, name) \
ASSIGN_DRIVER_FUNC(CreateSampler, name) \
ASSIGN_DRIVER_FUNC(CreateShader, name) \
ASSIGN_DRIVER_FUNC(CreateTexture, name) \
Expand All @@ -897,6 +912,7 @@ struct SDL_GPUDevice
ASSIGN_DRIVER_FUNC(ReleaseShader, name) \
ASSIGN_DRIVER_FUNC(ReleaseComputePipeline, name) \
ASSIGN_DRIVER_FUNC(ReleaseGraphicsPipeline, name) \
ASSIGN_DRIVER_FUNC(ReleasePipelineCache, name) \
ASSIGN_DRIVER_FUNC(BeginRenderPass, name) \
ASSIGN_DRIVER_FUNC(BindGraphicsPipeline, name) \
ASSIGN_DRIVER_FUNC(SetViewport, name) \
Expand Down
Loading
Loading