From 8513a3b6133b2ff85ec1c04cce950536a6e22107 Mon Sep 17 00:00:00 2001 From: Charles Waldner Date: Sun, 22 Feb 2026 02:40:30 -0700 Subject: [PATCH] Add cf_gpu_sync --- include/cute_graphics.h | 8 ++++++++ src/cute_graphics.cpp | 1 + src/cute_graphics_gles.cpp | 5 +++++ src/cute_graphics_sdlgpu.cpp | 11 +++++++++++ src/internal/cute_graphics_internal.h | 2 ++ 5 files changed, 27 insertions(+) diff --git a/include/cute_graphics.h b/include/cute_graphics.h index d3b02242..1f0e595f 100644 --- a/include/cute_graphics.h +++ b/include/cute_graphics.h @@ -683,6 +683,14 @@ CF_API void CF_CALL cf_texture_update_mip(CF_Texture texture, void* data, int si */ CF_API void CF_CALL cf_generate_mipmaps(CF_Texture texture); +/** + * @function cf_gpu_sync + * @category graphics + * @brief Submits the command buffer, waits for GPU completion via fence, then reacquires. + * @remarks Forces GPU/CPU serialization. + */ +CF_API void CF_CALL cf_gpu_sync(void); + /** * @function cf_texture_handle * @category graphics diff --git a/src/cute_graphics.cpp b/src/cute_graphics.cpp index e90db538..9ff02704 100644 --- a/src/cute_graphics.cpp +++ b/src/cute_graphics.cpp @@ -747,3 +747,4 @@ CF_DISPATCH_SHIM_VOID(dispatch_compute, (CF_ComputeShader shader, CF_Material ma CF_DISPATCH_SHIM(void*, create_draw_sampler, (CF_Filter filter), filter) CF_DISPATCH_SHIM_VOID(destroy_draw_sampler, (void* sampler), sampler) CF_DISPATCH_SHIM_VOID(set_sampler_override, (void* sampler), sampler) +CF_DISPATCH_SHIM_VOID(gpu_sync, (), ) diff --git a/src/cute_graphics_gles.cpp b/src/cute_graphics_gles.cpp index b02631d0..721e1878 100644 --- a/src/cute_graphics_gles.cpp +++ b/src/cute_graphics_gles.cpp @@ -698,6 +698,11 @@ void cf_gles_flush() glFlush(); } +void cf_gles_gpu_sync() +{ + glFinish(); +} + void cf_gles_set_vsync(bool true_turn_on_vsync) { SDL_GL_SetSwapInterval(true_turn_on_vsync ? 1 : 0); diff --git a/src/cute_graphics_sdlgpu.cpp b/src/cute_graphics_sdlgpu.cpp index 31f79f82..73c6fce1 100644 --- a/src/cute_graphics_sdlgpu.cpp +++ b/src/cute_graphics_sdlgpu.cpp @@ -812,6 +812,17 @@ void cf_sdlgpu_flush() } } +void cf_sdlgpu_gpu_sync() +{ + s_end_active_pass(); + if (g_ctx.cmd) { + SDL_GPUFence *fence = SDL_SubmitGPUCommandBufferAndAcquireFence(g_ctx.cmd); + SDL_WaitForGPUFences(g_ctx.device, true, &fence, 1); + SDL_ReleaseGPUFence(g_ctx.device, fence); + g_ctx.cmd = SDL_AcquireGPUCommandBuffer(g_ctx.device); + } +} + void cf_sdlgpu_set_vsync(bool vsync) { SDL_SetGPUSwapchainParameters(g_ctx.device, g_ctx.window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, vsync ? SDL_GPU_PRESENTMODE_VSYNC : SDL_GPU_PRESENTMODE_IMMEDIATE); diff --git a/src/internal/cute_graphics_internal.h b/src/internal/cute_graphics_internal.h index 447ff8ed..fb3471fe 100644 --- a/src/internal/cute_graphics_internal.h +++ b/src/internal/cute_graphics_internal.h @@ -241,6 +241,7 @@ SDL_GPUCommandBuffer* cf_sdlgpu_get_command_buffer(); void cf_sdlgpu_attach(SDL_Window* window); bool cf_sdlgpu_supports_msaa(int sample_count); void cf_sdlgpu_flush(); +void cf_sdlgpu_gpu_sync(); void cf_sdlgpu_set_vsync(bool true_turn_on_vsync); void cf_sdlgpu_set_vsync_mailbox(bool true_turn_on_vsync); void cf_sdlgpu_begin_frame(); @@ -255,6 +256,7 @@ SDL_GLContext cf_gles_get_gl_context(); void cf_gles_attach(SDL_Window* window); bool cf_gles_supports_msaa(int sample_count); void cf_gles_flush(); +void cf_gles_gpu_sync(); void cf_gles_set_vsync(bool true_turn_on_vsync); void cf_gles_begin_frame(); void cf_gles_blit_canvas(CF_Canvas canvas);