Added SDL3+SDL_GPU3 example.

omar 2025-08-20 18:48:30 +02:00
parent 3605277ebe
commit 92e768ae83

@ -12,6 +12,7 @@ _(had to turn off wiki editing, see [Home](Home) for details. you may post meani
- [Example: If you are using GLFW + Metal (on Apple devices)](#example-if-you-are-using-glfw--metal-on-apple-devices)
- [Example: If you are using SDL2/SDL3 + OpenGL/WebGL](#example-if-you-are-using-sdl2sdl3--openglwebgl)
- [Example: If you are using SDL2/SDL3 + Vulkan](#example-if-you-are-using-sdl2sdl3--vulkan)
- [Example: If you are using SDL3 + SDL_GPU](#example-if-you-are-using-sdl3--sdl_gpu)
- [Using another combination of backends?](#using-another-combination-of-backends)
- [Additional code to enable Docking](#additional-code-to-enable-docking)
- [Additional code to enable Multi-viewports](#additional-code-to-enable-multi-viewports)
@ -457,6 +458,84 @@ ImGui::DestroyContext();
```
That should be all!
## Example: If you are using SDL3 + SDL_GPU
Full standalone example [example_sdl3_sdlgpu3/main.cpp](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_sdlgpu3/main.cpp)
Add to Includes:
```cpp
#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlgpu3.h"
```
Add to Initialization:
```cpp
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // IF using Docking Branch
// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForSDLGPU(window);
ImGui_ImplSDLGPU3_InitInfo init_info = {};
init_info.Device = gpu_device;
init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_device, window);
init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; // Only used in multi-viewports mode.
init_info.SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR; // Only used in multi-viewports mode.
init_info.PresentMode = SDL_GPU_PRESENTMODE_VSYNC;
ImGui_ImplSDLGPU3_Init(&init_info);
```
Add to start of main loop:
```cpp
// (Where your code calls SDL_PollEvent())
ImGui_ImplSDL3_ProcessEvent(&event); // Forward your event to backend
```
```cpp
// (After event loop)
// Start the Dear ImGui frame
ImGui_ImplSDLGPU3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow(); // Show demo window! :)
```
Add to end of main loop:
```cpp
// Rendering
// (Your code clears your framebuffer, renders your other stuff etc.)
ImGui::Render();
SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpu_device); // Acquire a GPU command buffer
SDL_WaitAndAcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr); // Acquire a swapchain texture
ImGui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer);
// Setup and start a render pass
SDL_GPUColorTargetInfo target_info = {};
target_info.texture = swapchain_texture;
target_info.clear_color = SDL_FColor { clear_color.x, clear_color.y, clear_color.z, clear_color.w };
target_info.load_op = SDL_GPU_LOADOP_CLEAR;
target_info.store_op = SDL_GPU_STOREOP_STORE;
target_info.mip_level = 0;
target_info.layer_or_depth_plane = 0;
target_info.cycle = false;
SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(command_buffer, &target_info, 1, nullptr);
// Render ImGui
ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, render_pass);
SDL_EndGPURenderPass(render_pass);
```
Add to Shutdown:
```cpp
ImGui_ImplSDL3_Shutdown();
ImGui_ImplSDLGPU3_Shutdown();
ImGui::DestroyContext();
```
That should be all!
## Using another combination of backends?
The various examples above should reflect integration with a majority of backends, so you can follow the same logic.