21
21
// 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.
22
22
23
23
// CHANGELOG
24
- // 2025-01-09: SDL_Gpu: Added the SDL_GPU3 backend.
24
+ // 2025-01-16: Renamed ImGui_ImplSDLGPU3_InitInfo::GpuDevice to Device.
25
+ // 2025-01-09: SDL_GPU: Added the SDL_GPU3 backend.
25
26
26
27
#include " imgui.h"
27
28
#ifndef IMGUI_DISABLE
28
29
#include " imgui_impl_sdlgpu3.h"
29
30
#include " imgui_impl_sdlgpu3_shaders.h"
30
31
32
+ // SDL_GPU Data
33
+
31
34
// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU3_RenderDrawData()
32
35
struct ImGui_ImplSDLGPU3_FrameData
33
36
{
@@ -37,10 +40,9 @@ struct ImGui_ImplSDLGPU3_FrameData
37
40
uint32_t IndexBufferSize = 0 ;
38
41
};
39
42
40
- // SDL_GPU Data
41
43
struct ImGui_ImplSDLGPU3_Data
42
44
{
43
- ImGui_ImplSDLGPU3_InitInfo GPUInitInfo ;
45
+ ImGui_ImplSDLGPU3_InitInfo InitInfo ;
44
46
45
47
// Graphics pipeline & shaders
46
48
SDL_GPUShader* VertexShader = nullptr ;
@@ -57,8 +59,6 @@ struct ImGui_ImplSDLGPU3_Data
57
59
};
58
60
59
61
// Forward Declarations
60
- static bool ImGui_ImplSDLGPU3_CreateDeviceObjects ();
61
- static void ImGui_ImplSDLGPU3_DestroyDeviceObjects ();
62
62
static void ImGui_ImplSDLGPU3_DestroyFrameData ();
63
63
64
64
// -----------------------------------------------------------------------------
@@ -116,16 +116,16 @@ static void ImGui_ImplSDLGPU3_SetupRenderState(ImDrawData* draw_data, SDL_GPUGra
116
116
static void CreateOrResizeBuffer (SDL_GPUBuffer** buffer, uint32_t * old_size, uint32_t new_size, SDL_GPUBufferUsageFlags usage)
117
117
{
118
118
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
119
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
119
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
120
120
121
- SDL_WaitForGPUIdle (v->GpuDevice );
122
- SDL_ReleaseGPUBuffer (v->GpuDevice , *buffer);
121
+ SDL_WaitForGPUIdle (v->Device );
122
+ SDL_ReleaseGPUBuffer (v->Device , *buffer);
123
123
124
124
SDL_GPUBufferCreateInfo buffer_info = {};
125
125
buffer_info.usage = usage;
126
126
buffer_info.size = new_size;
127
127
buffer_info.props = 0 ;
128
- *buffer = SDL_CreateGPUBuffer (v->GpuDevice , &buffer_info);
128
+ *buffer = SDL_CreateGPUBuffer (v->Device , &buffer_info);
129
129
*old_size = new_size;
130
130
IM_ASSERT (*buffer != nullptr && " Failed to create GPU Buffer, call SDL_GetError() for more information" );
131
131
}
@@ -142,7 +142,7 @@ void Imgui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
142
142
return ;
143
143
144
144
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
145
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
145
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
146
146
ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData ;
147
147
148
148
uint32_t vertex_size = draw_data->TotalVtxCount * sizeof (ImDrawVert);
@@ -160,13 +160,13 @@ void Imgui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
160
160
index_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
161
161
index_transferbuffer_info.size = index_size;
162
162
163
- SDL_GPUTransferBuffer* vertex_transferbuffer = SDL_CreateGPUTransferBuffer (v->GpuDevice , &vertex_transferbuffer_info);
163
+ SDL_GPUTransferBuffer* vertex_transferbuffer = SDL_CreateGPUTransferBuffer (v->Device , &vertex_transferbuffer_info);
164
164
IM_ASSERT (vertex_transferbuffer != nullptr && " Failed to create the vertex transfer buffer, call SDL_GetError() for more information" );
165
- SDL_GPUTransferBuffer* index_transferbuffer = SDL_CreateGPUTransferBuffer (v->GpuDevice , &index_transferbuffer_info);
165
+ SDL_GPUTransferBuffer* index_transferbuffer = SDL_CreateGPUTransferBuffer (v->Device , &index_transferbuffer_info);
166
166
IM_ASSERT (index_transferbuffer != nullptr && " Failed to create the index transfer buffer, call SDL_GetError() for more information" );
167
167
168
- ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer (v->GpuDevice , vertex_transferbuffer, true );
169
- ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer (v->GpuDevice , index_transferbuffer, true );
168
+ ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer (v->Device , vertex_transferbuffer, true );
169
+ ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer (v->Device , index_transferbuffer, true );
170
170
for (int n = 0 ; n < draw_data->CmdListsCount ; n++)
171
171
{
172
172
const ImDrawList* draw_list = draw_data->CmdLists [n];
@@ -175,8 +175,8 @@ void Imgui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
175
175
vtx_dst += draw_list->VtxBuffer .Size ;
176
176
idx_dst += draw_list->IdxBuffer .Size ;
177
177
}
178
- SDL_UnmapGPUTransferBuffer (v->GpuDevice , vertex_transferbuffer);
179
- SDL_UnmapGPUTransferBuffer (v->GpuDevice , index_transferbuffer);
178
+ SDL_UnmapGPUTransferBuffer (v->Device , vertex_transferbuffer);
179
+ SDL_UnmapGPUTransferBuffer (v->Device , index_transferbuffer);
180
180
181
181
SDL_GPUTransferBufferLocation vertex_buffer_location = {};
182
182
vertex_buffer_location.offset = 0 ;
@@ -199,8 +199,8 @@ void Imgui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
199
199
SDL_UploadToGPUBuffer (copy_pass, &vertex_buffer_location, &vertex_buffer_region,true );
200
200
SDL_UploadToGPUBuffer (copy_pass, &index_buffer_location, &index_buffer_region,true );
201
201
SDL_EndGPUCopyPass (copy_pass);
202
- SDL_ReleaseGPUTransferBuffer (v->GpuDevice , index_transferbuffer);
203
- SDL_ReleaseGPUTransferBuffer (v->GpuDevice , vertex_transferbuffer);
202
+ SDL_ReleaseGPUTransferBuffer (v->Device , index_transferbuffer);
203
+ SDL_ReleaseGPUTransferBuffer (v->Device , vertex_transferbuffer);
204
204
}
205
205
206
206
void ImGui_ImplSDLGPU3_RenderDrawData (ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline)
@@ -278,16 +278,16 @@ void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffe
278
278
SDL_SetGPUScissor (render_pass, &scissor_rect);
279
279
}
280
280
281
- bool ImGui_ImplSDLGPU3_CreateFontsTexture ()
281
+ void ImGui_ImplSDLGPU3_CreateFontsTexture ()
282
282
{
283
283
ImGuiIO& io = ImGui::GetIO ();
284
284
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
285
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
285
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
286
286
287
287
// Destroy existing texture (if any)
288
288
if (bd->FontTexture )
289
289
{
290
- SDL_WaitForGPUIdle (v->GpuDevice );
290
+ SDL_WaitForGPUIdle (v->Device );
291
291
ImGui_ImplSDLGPU3_DestroyFontsTexture ();
292
292
}
293
293
@@ -308,7 +308,7 @@ bool ImGui_ImplSDLGPU3_CreateFontsTexture()
308
308
texture_info.num_levels = 1 ;
309
309
texture_info.sample_count = SDL_GPU_SAMPLECOUNT_1;
310
310
311
- bd->FontTexture = SDL_CreateGPUTexture (v->GpuDevice , &texture_info);
311
+ bd->FontTexture = SDL_CreateGPUTexture (v->Device , &texture_info);
312
312
IM_ASSERT (bd->FontTexture && " Failed to create font texture, call SDL_GetError() for more info" );
313
313
}
314
314
@@ -317,50 +317,48 @@ bool ImGui_ImplSDLGPU3_CreateFontsTexture()
317
317
318
318
// Create all the upload structures and upload:
319
319
{
320
- SDL_GPUTransferBufferCreateInfo font_transferbuffer_info = {};
321
- font_transferbuffer_info .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
322
- font_transferbuffer_info .size = upload_size;
320
+ SDL_GPUTransferBufferCreateInfo transferbuffer_info = {};
321
+ transferbuffer_info .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
322
+ transferbuffer_info .size = upload_size;
323
323
324
- SDL_GPUTransferBuffer* font_transferbuffer = SDL_CreateGPUTransferBuffer (v->GpuDevice , &font_transferbuffer_info );
325
- IM_ASSERT (font_transferbuffer != nullptr && " Failed to create font transfer buffer, call SDL_GetError() for more information" );
324
+ SDL_GPUTransferBuffer* transferbuffer = SDL_CreateGPUTransferBuffer (v->Device , &transferbuffer_info );
325
+ IM_ASSERT (transferbuffer != nullptr && " Failed to create font transfer buffer, call SDL_GetError() for more information" );
326
326
327
- void * texture_ptr = SDL_MapGPUTransferBuffer (v->GpuDevice , font_transferbuffer , false );
327
+ void * texture_ptr = SDL_MapGPUTransferBuffer (v->Device , transferbuffer , false );
328
328
memcpy (texture_ptr, pixels, upload_size);
329
- SDL_UnmapGPUTransferBuffer (v->GpuDevice , font_transferbuffer );
329
+ SDL_UnmapGPUTransferBuffer (v->Device , transferbuffer );
330
330
331
- SDL_GPUTextureTransferInfo font_transfer_info = {};
332
- font_transfer_info .offset = 0 ;
333
- font_transfer_info .transfer_buffer = font_transferbuffer ;
331
+ SDL_GPUTextureTransferInfo transfer_info = {};
332
+ transfer_info .offset = 0 ;
333
+ transfer_info .transfer_buffer = transferbuffer ;
334
334
335
- SDL_GPUTextureRegion font_texture_region = {};
336
- font_texture_region .texture = bd->FontTexture ;
337
- font_texture_region .w = width;
338
- font_texture_region .h = height;
339
- font_texture_region .d = 1 ;
335
+ SDL_GPUTextureRegion texture_region = {};
336
+ texture_region .texture = bd->FontTexture ;
337
+ texture_region .w = width;
338
+ texture_region .h = height;
339
+ texture_region .d = 1 ;
340
340
341
- SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer (v->GpuDevice );
341
+ SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer (v->Device );
342
342
SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass (cmd);
343
- SDL_UploadToGPUTexture (copy_pass, &font_transfer_info , &font_texture_region , false );
343
+ SDL_UploadToGPUTexture (copy_pass, &transfer_info , &texture_region , false );
344
344
SDL_EndGPUCopyPass (copy_pass);
345
345
SDL_SubmitGPUCommandBuffer (cmd);
346
- SDL_ReleaseGPUTransferBuffer (v->GpuDevice , font_transferbuffer );
346
+ SDL_ReleaseGPUTransferBuffer (v->Device , transferbuffer );
347
347
}
348
348
349
349
// Store our identifier
350
350
io.Fonts ->SetTexID ((ImTextureID)&bd->FontBinding );
351
-
352
- return true ;
353
351
}
354
352
355
353
// You probably never need to call this, as it is called by ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_Shutdown().
356
354
void ImGui_ImplSDLGPU3_DestroyFontsTexture ()
357
355
{
358
356
ImGuiIO& io = ImGui::GetIO ();
359
357
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
360
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
358
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
361
359
if (bd->FontTexture )
362
360
{
363
- SDL_ReleaseGPUTexture (v->GpuDevice , bd->FontTexture );
361
+ SDL_ReleaseGPUTexture (v->Device , bd->FontTexture );
364
362
bd->FontBinding .texture = nullptr ;
365
363
bd->FontTexture = nullptr ;
366
364
}
@@ -371,9 +369,9 @@ static void Imgui_ImplSDLGPU3_CreateShaders()
371
369
{
372
370
// Create the shader modules
373
371
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
374
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
372
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
375
373
376
- const char * driver = SDL_GetGPUDeviceDriver (v->GpuDevice );
374
+ const char * driver = SDL_GetGPUDeviceDriver (v->Device );
377
375
378
376
SDL_GPUShaderCreateInfo vertex_shader_info = {};
379
377
vertex_shader_info.entrypoint = " main" ;
@@ -422,16 +420,16 @@ static void Imgui_ImplSDLGPU3_CreateShaders()
422
420
fragment_shader_info.code_size = sizeof (metallib_fragment);
423
421
}
424
422
#endif
425
- bd->VertexShader = SDL_CreateGPUShader (v->GpuDevice , &vertex_shader_info);
426
- bd->FragmentShader = SDL_CreateGPUShader (v->GpuDevice , &fragment_shader_info);
423
+ bd->VertexShader = SDL_CreateGPUShader (v->Device , &vertex_shader_info);
424
+ bd->FragmentShader = SDL_CreateGPUShader (v->Device , &fragment_shader_info);
427
425
IM_ASSERT (bd->VertexShader != nullptr && " Failed to create vertex shader, call SDL_GetError() for more information" );
428
426
IM_ASSERT (bd->FragmentShader != nullptr && " Failed to create fragment shader, call SDL_GetError() for more information" );
429
427
}
430
428
431
429
static void ImGui_ImplSDLGPU3_CreateGraphicsPipeline ()
432
430
{
433
431
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
434
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
432
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
435
433
Imgui_ImplSDLGPU3_CreateShaders ();
436
434
437
435
SDL_GPUVertexBufferDescription vertex_buffer_desc[1 ];
@@ -507,14 +505,14 @@ static void ImGui_ImplSDLGPU3_CreateGraphicsPipeline()
507
505
pipeline_info.depth_stencil_state = depth_stencil_state;
508
506
pipeline_info.target_info = target_info;
509
507
510
- bd->Pipeline = SDL_CreateGPUGraphicsPipeline (v->GpuDevice , &pipeline_info);
508
+ bd->Pipeline = SDL_CreateGPUGraphicsPipeline (v->Device , &pipeline_info);
511
509
IM_ASSERT (bd->Pipeline != nullptr && " Failed to create graphics pipeline, call SDL_GetError() for more information" );
512
510
}
513
511
514
- bool ImGui_ImplSDLGPU3_CreateDeviceObjects ()
512
+ void ImGui_ImplSDLGPU3_CreateDeviceObjects ()
515
513
{
516
514
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
517
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
515
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
518
516
519
517
if (!bd->FontSampler )
520
518
{
@@ -533,23 +531,22 @@ bool ImGui_ImplSDLGPU3_CreateDeviceObjects()
533
531
sampler_info.max_anisotropy = 1 .0f ;
534
532
sampler_info.enable_compare = false ;
535
533
536
- bd->FontSampler = SDL_CreateGPUSampler (v->GpuDevice , &sampler_info);
534
+ bd->FontSampler = SDL_CreateGPUSampler (v->Device , &sampler_info);
537
535
bd->FontBinding .sampler = bd->FontSampler ;
538
536
IM_ASSERT (bd->FontSampler != nullptr && " Failed to create font sampler, call SDL_GetError() for more information" );
539
537
}
540
538
541
539
ImGui_ImplSDLGPU3_CreateGraphicsPipeline ();
542
-
543
- return true ;
540
+ ImGui_ImplSDLGPU3_CreateFontsTexture ();
544
541
}
545
542
546
543
void ImGui_ImplSDLGPU3_DestroyFrameData ()
547
544
{
548
545
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
549
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
546
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
550
547
551
- SDL_ReleaseGPUBuffer (v->GpuDevice , bd->MainWindowFrameData .VertexBuffer );
552
- SDL_ReleaseGPUBuffer (v->GpuDevice , bd->MainWindowFrameData .IndexBuffer );
548
+ SDL_ReleaseGPUBuffer (v->Device , bd->MainWindowFrameData .VertexBuffer );
549
+ SDL_ReleaseGPUBuffer (v->Device , bd->MainWindowFrameData .IndexBuffer );
553
550
bd->MainWindowFrameData .VertexBuffer = nullptr ;
554
551
bd->MainWindowFrameData .IndexBuffer = nullptr ;
555
552
bd->MainWindowFrameData .VertexBufferSize = 0 ;
@@ -559,15 +556,15 @@ void ImGui_ImplSDLGPU3_DestroyFrameData()
559
556
void ImGui_ImplSDLGPU3_DestroyDeviceObjects ()
560
557
{
561
558
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData ();
562
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo ;
559
+ ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo ;
563
560
564
561
ImGui_ImplSDLGPU3_DestroyFrameData ();
565
562
ImGui_ImplSDLGPU3_DestroyFontsTexture ();
566
563
567
- if (bd->VertexShader ) { SDL_ReleaseGPUShader (v->GpuDevice , bd->VertexShader ); bd->VertexShader = nullptr ;}
568
- if (bd->FragmentShader ) { SDL_ReleaseGPUShader (v->GpuDevice , bd->FragmentShader ); bd->FragmentShader = nullptr ;}
569
- if (bd->FontSampler ) { SDL_ReleaseGPUSampler (v->GpuDevice , bd->FontSampler ); bd->FontSampler = nullptr ;}
570
- if (bd->Pipeline ) { SDL_ReleaseGPUGraphicsPipeline (v->GpuDevice , bd->Pipeline ); bd->Pipeline = nullptr ;}
564
+ if (bd->VertexShader ) { SDL_ReleaseGPUShader (v->Device , bd->VertexShader ); bd->VertexShader = nullptr ;}
565
+ if (bd->FragmentShader ) { SDL_ReleaseGPUShader (v->Device , bd->FragmentShader ); bd->FragmentShader = nullptr ;}
566
+ if (bd->FontSampler ) { SDL_ReleaseGPUSampler (v->Device , bd->FontSampler ); bd->FontSampler = nullptr ;}
567
+ if (bd->Pipeline ) { SDL_ReleaseGPUGraphicsPipeline (v->Device , bd->Pipeline ); bd->Pipeline = nullptr ;}
571
568
}
572
569
573
570
bool ImGui_ImplSDLGPU3_Init (ImGui_ImplSDLGPU3_InitInfo* info)
@@ -582,10 +579,10 @@ bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info)
582
579
io.BackendRendererName = " imgui_impl_sdlgpu3" ;
583
580
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
584
581
585
- IM_ASSERT (info->GpuDevice != nullptr );
582
+ IM_ASSERT (info->Device != nullptr );
586
583
IM_ASSERT (info->ColorTargetFormat != SDL_GPU_TEXTUREFORMAT_INVALID);
587
584
588
- bd->GPUInitInfo = *info;
585
+ bd->InitInfo = *info;
589
586
590
587
ImGui_ImplSDLGPU3_CreateDeviceObjects ();
591
588
0 commit comments