Skip to content

Commit b678659

Browse files
committed
Backends: SDLGPU3: Made ImGui_ImplSDLGPU3_PrepareDrawData() reuse GPU Transfer Buffers which were unusually slow to recreate every frame. (#8534)
1 parent a26fcf5 commit b678659

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

backends/imgui_impl_sdlgpu3.cpp

+33-36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// Calling the function is MANDATORY, otherwise the ImGui will not upload neither the vertex nor the index buffer for the GPU. See imgui_impl_sdlgpu3.cpp for more info.
2222

2323
// CHANGELOG
24+
// 2025-03-30: Made ImGui_ImplSDLGPU3_PrepareDrawData() reuse GPU Transfer Buffers which were unusually slow to recreate every frame. Much faster now.
2425
// 2025-03-21: Fixed typo in function name Imgui_ImplSDLGPU3_PrepareDrawData() -> ImGui_ImplSDLGPU3_PrepareDrawData().
2526
// 2025-01-16: Renamed ImGui_ImplSDLGPU3_InitInfo::GpuDevice to Device.
2627
// 2025-01-09: SDL_GPU: Added the SDL_GPU3 backend.
@@ -35,10 +36,12 @@
3536
// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU3_RenderDrawData()
3637
struct ImGui_ImplSDLGPU3_FrameData
3738
{
38-
SDL_GPUBuffer* VertexBuffer = nullptr;
39-
SDL_GPUBuffer* IndexBuffer = nullptr;
40-
uint32_t VertexBufferSize = 0;
41-
uint32_t IndexBufferSize = 0;
39+
SDL_GPUBuffer* VertexBuffer = nullptr;
40+
SDL_GPUTransferBuffer* VertexTransferBuffer = nullptr;
41+
uint32_t VertexBufferSize = 0;
42+
SDL_GPUBuffer* IndexBuffer = nullptr;
43+
SDL_GPUTransferBuffer* IndexTransferBuffer = nullptr;
44+
uint32_t IndexBufferSize = 0;
4245
};
4346

4447
struct ImGui_ImplSDLGPU3_Data
@@ -114,14 +117,15 @@ static void ImGui_ImplSDLGPU3_SetupRenderState(ImDrawData* draw_data, SDL_GPUGra
114117
SDL_PushGPUVertexUniformData(command_buffer, 0, &ubo, sizeof(UBO));
115118
}
116119

117-
static void CreateOrResizeBuffer(SDL_GPUBuffer** buffer, uint32_t* old_size, uint32_t new_size, SDL_GPUBufferUsageFlags usage)
120+
static void CreateOrResizeBuffers(SDL_GPUBuffer** buffer, SDL_GPUTransferBuffer** transferbuffer, uint32_t* old_size, uint32_t new_size, SDL_GPUBufferUsageFlags usage)
118121
{
119122
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
120123
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
121124

122-
// Even though this is fairly rarely called.
125+
// FIXME-OPT: Not optimal, but this is fairly rarely called.
123126
SDL_WaitForGPUIdle(v->Device);
124127
SDL_ReleaseGPUBuffer(v->Device, *buffer);
128+
SDL_ReleaseGPUTransferBuffer(v->Device, *transferbuffer);
125129

126130
SDL_GPUBufferCreateInfo buffer_info = {};
127131
buffer_info.usage = usage;
@@ -130,6 +134,12 @@ static void CreateOrResizeBuffer(SDL_GPUBuffer** buffer, uint32_t* old_size, uin
130134
*buffer = SDL_CreateGPUBuffer(v->Device, &buffer_info);
131135
*old_size = new_size;
132136
IM_ASSERT(*buffer != nullptr && "Failed to create GPU Buffer, call SDL_GetError() for more information");
137+
138+
SDL_GPUTransferBufferCreateInfo transferbuffer_info = {};
139+
transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
140+
transferbuffer_info.size = new_size;
141+
*transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &transferbuffer_info);
142+
IM_ASSERT(*transferbuffer != nullptr && "Failed to create GPU Transfer Buffer, call SDL_GetError() for more information");
133143
}
134144

135145
// SDL_GPU doesn't allow copy passes to occur while a render or compute pass is bound!
@@ -150,25 +160,12 @@ void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
150160
uint32_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
151161
uint32_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
152162
if (fd->VertexBuffer == nullptr || fd->VertexBufferSize < vertex_size)
153-
CreateOrResizeBuffer(&fd->VertexBuffer, &fd->VertexBufferSize, vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX);
163+
CreateOrResizeBuffers(&fd->VertexBuffer, &fd->VertexTransferBuffer, &fd->VertexBufferSize, vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX);
154164
if (fd->IndexBuffer == nullptr || fd->IndexBufferSize < index_size)
155-
CreateOrResizeBuffer(&fd->IndexBuffer, &fd->IndexBufferSize, index_size, SDL_GPU_BUFFERUSAGE_INDEX);
156-
157-
// FIXME: It feels like more code could be shared there.
158-
SDL_GPUTransferBufferCreateInfo vertex_transferbuffer_info = {};
159-
vertex_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
160-
vertex_transferbuffer_info.size = vertex_size;
161-
SDL_GPUTransferBufferCreateInfo index_transferbuffer_info = {};
162-
index_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
163-
index_transferbuffer_info.size = index_size;
164-
165-
SDL_GPUTransferBuffer* vertex_transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &vertex_transferbuffer_info);
166-
IM_ASSERT(vertex_transferbuffer != nullptr && "Failed to create the vertex transfer buffer, call SDL_GetError() for more information");
167-
SDL_GPUTransferBuffer* index_transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &index_transferbuffer_info);
168-
IM_ASSERT(index_transferbuffer != nullptr && "Failed to create the index transfer buffer, call SDL_GetError() for more information");
169-
170-
ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->Device, vertex_transferbuffer, true);
171-
ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->Device, index_transferbuffer, true);
165+
CreateOrResizeBuffers(&fd->IndexBuffer, &fd->IndexTransferBuffer, &fd->IndexBufferSize, index_size, SDL_GPU_BUFFERUSAGE_INDEX);
166+
167+
ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->Device, fd->VertexTransferBuffer, true);
168+
ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->Device, fd->IndexTransferBuffer, true);
172169
for (int n = 0; n < draw_data->CmdListsCount; n++)
173170
{
174171
const ImDrawList* draw_list = draw_data->CmdLists[n];
@@ -177,15 +174,15 @@ void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
177174
vtx_dst += draw_list->VtxBuffer.Size;
178175
idx_dst += draw_list->IdxBuffer.Size;
179176
}
180-
SDL_UnmapGPUTransferBuffer(v->Device, vertex_transferbuffer);
181-
SDL_UnmapGPUTransferBuffer(v->Device, index_transferbuffer);
177+
SDL_UnmapGPUTransferBuffer(v->Device, fd->VertexTransferBuffer);
178+
SDL_UnmapGPUTransferBuffer(v->Device, fd->IndexTransferBuffer);
182179

183180
SDL_GPUTransferBufferLocation vertex_buffer_location = {};
184181
vertex_buffer_location.offset = 0;
185-
vertex_buffer_location.transfer_buffer = vertex_transferbuffer;
182+
vertex_buffer_location.transfer_buffer = fd->VertexTransferBuffer;
186183
SDL_GPUTransferBufferLocation index_buffer_location = {};
187184
index_buffer_location.offset = 0;
188-
index_buffer_location.transfer_buffer = index_transferbuffer;
185+
index_buffer_location.transfer_buffer = fd->IndexTransferBuffer;
189186

190187
SDL_GPUBufferRegion vertex_buffer_region = {};
191188
vertex_buffer_region.buffer = fd->VertexBuffer;
@@ -201,8 +198,6 @@ void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
201198
SDL_UploadToGPUBuffer(copy_pass, &vertex_buffer_location, &vertex_buffer_region, true);
202199
SDL_UploadToGPUBuffer(copy_pass, &index_buffer_location, &index_buffer_region, true);
203200
SDL_EndGPUCopyPass(copy_pass);
204-
SDL_ReleaseGPUTransferBuffer(v->Device, index_transferbuffer);
205-
SDL_ReleaseGPUTransferBuffer(v->Device, vertex_transferbuffer);
206201
}
207202

208203
void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline)
@@ -547,12 +542,14 @@ void ImGui_ImplSDLGPU3_DestroyFrameData()
547542
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
548543
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
549544

550-
SDL_ReleaseGPUBuffer(v->Device, bd->MainWindowFrameData.VertexBuffer);
551-
SDL_ReleaseGPUBuffer(v->Device, bd->MainWindowFrameData.IndexBuffer);
552-
bd->MainWindowFrameData.VertexBuffer = nullptr;
553-
bd->MainWindowFrameData.IndexBuffer = nullptr;
554-
bd->MainWindowFrameData.VertexBufferSize = 0;
555-
bd->MainWindowFrameData.IndexBufferSize = 0;
545+
ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
546+
SDL_ReleaseGPUBuffer(v->Device, fd->VertexBuffer);
547+
SDL_ReleaseGPUBuffer(v->Device, fd->IndexBuffer);
548+
SDL_ReleaseGPUTransferBuffer(v->Device, fd->VertexTransferBuffer);
549+
SDL_ReleaseGPUTransferBuffer(v->Device, fd->IndexTransferBuffer);
550+
fd->VertexBuffer = fd->IndexBuffer = nullptr;
551+
fd->VertexTransferBuffer = fd->IndexTransferBuffer = nullptr;
552+
fd->VertexBufferSize = fd->IndexBufferSize = 0;
556553
}
557554

558555
void ImGui_ImplSDLGPU3_DestroyDeviceObjects()

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Other changes:
6363
regardless of ImGuiConfigFlags_NavEnableGamepad being set. (#8508)
6464
- Backends: SDL3: Update for SDL3 api changes: revert SDL_GetClipboardText()
6565
memory ownership change. (#8530, #7801) [@Green-Sky]
66+
- Backends: SDLGPU3: Made ImGui_ImplSDLGPU3_PrepareDrawData() reuse GPU Transfer Buffers
67+
which were unusually slow to recreate every frame. Much faster now. (#8534)
6668

6769

6870
-----------------------------------------------------------------------

0 commit comments

Comments
 (0)