| 1 |
// dear imgui: Renderer Backend for SDL_Renderer for SDL3 |
| 2 |
// (Requires: SDL 3.0.0+) |
| 3 |
|
| 4 |
// (**IMPORTANT: SDL 3.0.0 is NOT YET RELEASED AND CURRENTLY HAS A FAST CHANGING API. THIS CODE BREAKS OFTEN AS SDL3 CHANGES.**) |
| 5 |
|
| 6 |
// Note how SDL_Renderer is an _optional_ component of SDL3. |
| 7 |
// For a multi-platform app consider using e.g. SDL+DirectX on Windows and SDL+OpenGL on Linux/OSX. |
| 8 |
// If your application will want to render any non trivial amount of graphics other than UI, |
| 9 |
// please be aware that SDL_Renderer currently offers a limited graphic API to the end-user and |
| 10 |
// it might be difficult to step out of those boundaries. |
| 11 |
|
| 12 |
// Implemented features: |
| 13 |
// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID! |
| 14 |
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
| 15 |
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'. |
| 16 |
|
| 17 |
// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 18 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 19 |
// Learn about Dear ImGui: |
| 20 |
// - FAQ https://dearimgui.com/faq |
| 21 |
// - Getting Started https://dearimgui.com/getting-started |
| 22 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 23 |
// - Introduction, links and more at the top of imgui.cpp |
| 24 |
|
| 25 |
// CHANGELOG |
| 26 |
// 2024-10-09: Expose selected render state in ImGui_ImplSDLRenderer3_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks. |
| 27 |
// 2024-07-01: Update for SDL3 api changes: SDL_RenderGeometryRaw() uint32 version was removed (SDL#9009). |
| 28 |
// 2024-05-14: *BREAKING CHANGE* ImGui_ImplSDLRenderer3_RenderDrawData() requires SDL_Renderer* passed as parameter. |
| 29 |
// 2024-02-12: Amend to query SDL_RenderViewportSet() and restore viewport accordingly. |
| 30 |
// 2023-05-30: Initial version. |
| 31 |
|
| 32 |
#include "imgui.h" |
| 33 |
#ifndef IMGUI_DISABLE |
| 34 |
#include "imgui_impl_sdlrenderer3.h" |
| 35 |
#include <stdint.h> // intptr_t |
| 36 |
|
| 37 |
// Clang warnings with -Weverything |
| 38 |
#if defined(__clang__) |
| 39 |
#pragma clang diagnostic push |
| 40 |
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness |
| 41 |
#endif |
| 42 |
|
| 43 |
// SDL |
| 44 |
#include <SDL3/SDL.h> |
| 45 |
#if !SDL_VERSION_ATLEAST(3,0,0) |
| 46 |
#error This backend requires SDL 3.0.0+ |
| 47 |
#endif |
| 48 |
|
| 49 |
// SDL_Renderer data |
| 50 |
struct ImGui_ImplSDLRenderer3_Data |
| 51 |
{ |
| 52 |
SDL_Renderer* Renderer; // Main viewport's renderer |
| 53 |
SDL_Texture* FontTexture; |
| 54 |
ImVector<SDL_FColor> ColorBuffer; |
| 55 |
|
| 56 |
ImGui_ImplSDLRenderer3_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 57 |
}; |
| 58 |
|
| 59 |
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts |
| 60 |
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
| 61 |
static ImGui_ImplSDLRenderer3_Data* ImGui_ImplSDLRenderer3_GetBackendData() |
| 62 |
{ |
| 63 |
return ImGui::GetCurrentContext() ? (ImGui_ImplSDLRenderer3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
// Functions |
| 67 |
bool ImGui_ImplSDLRenderer3_Init(SDL_Renderer* renderer) |
| 68 |
{ |
| 69 |
ImGuiIO& io = ImGui::GetIO(); |
| 70 |
IMGUI_CHECKVERSION(); |
| 71 |
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); |
| 72 |
IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!"); |
| 73 |
|
| 74 |
// Setup backend capabilities flags |
| 75 |
ImGui_ImplSDLRenderer3_Data* bd = IM_NEW(ImGui_ImplSDLRenderer3_Data)(); |
| 76 |
io.BackendRendererUserData = (void*)bd; |
| 77 |
io.BackendRendererName = "imgui_impl_sdlrenderer3"; |
| 78 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. |
| 79 |
|
| 80 |
bd->Renderer = renderer; |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
void ImGui_ImplSDLRenderer3_Shutdown() |
| 86 |
{ |
| 87 |
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); |
| 88 |
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); |
| 89 |
ImGuiIO& io = ImGui::GetIO(); |
| 90 |
|
| 91 |
ImGui_ImplSDLRenderer3_DestroyDeviceObjects(); |
| 92 |
|
| 93 |
io.BackendRendererName = nullptr; |
| 94 |
io.BackendRendererUserData = nullptr; |
| 95 |
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; |
| 96 |
IM_DELETE(bd); |
| 97 |
} |
| 98 |
|
| 99 |
static void ImGui_ImplSDLRenderer3_SetupRenderState(SDL_Renderer* renderer) |
| 100 |
{ |
| 101 |
// Clear out any viewports and cliprect set by the user |
| 102 |
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process. |
| 103 |
SDL_SetRenderViewport(renderer, nullptr); |
| 104 |
SDL_SetRenderClipRect(renderer, nullptr); |
| 105 |
} |
| 106 |
|
| 107 |
void ImGui_ImplSDLRenderer3_NewFrame() |
| 108 |
{ |
| 109 |
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); |
| 110 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer3_Init()?"); |
| 111 |
|
| 112 |
if (!bd->FontTexture) |
| 113 |
ImGui_ImplSDLRenderer3_CreateDeviceObjects(); |
| 114 |
} |
| 115 |
|
| 116 |
// https://github.com/libsdl-org/SDL/issues/9009 |
| 117 |
static int SDL_RenderGeometryRaw8BitColor(SDL_Renderer* renderer, ImVector<SDL_FColor>& colors_out, SDL_Texture* texture, const float* xy, int xy_stride, const SDL_Color* color, int color_stride, const float* uv, int uv_stride, int num_vertices, const void* indices, int num_indices, int size_indices) |
| 118 |
{ |
| 119 |
const Uint8* color2 = (const Uint8*)color; |
| 120 |
colors_out.resize(num_vertices); |
| 121 |
SDL_FColor* color3 = colors_out.Data; |
| 122 |
for (int i = 0; i < num_vertices; i++) |
| 123 |
{ |
| 124 |
color3[i].r = color->r / 255.0f; |
| 125 |
color3[i].g = color->g / 255.0f; |
| 126 |
color3[i].b = color->b / 255.0f; |
| 127 |
color3[i].a = color->a / 255.0f; |
| 128 |
color2 += color_stride; |
| 129 |
color = (const SDL_Color*)color2; |
| 130 |
} |
| 131 |
return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color3, sizeof(*color3), uv, uv_stride, num_vertices, indices, num_indices, size_indices); |
| 132 |
} |
| 133 |
|
| 134 |
void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer) |
| 135 |
{ |
| 136 |
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); |
| 137 |
|
| 138 |
// If there's a scale factor set by the user, use that instead |
| 139 |
// If the user has specified a scale factor to SDL_Renderer already via SDL_RenderSetScale(), SDL will scale whatever we pass |
| 140 |
// to SDL_RenderGeometryRaw() by that scale factor. In that case we don't want to be also scaling it ourselves here. |
| 141 |
float rsx = 1.0f; |
| 142 |
float rsy = 1.0f; |
| 143 |
SDL_GetRenderScale(renderer, &rsx, &rsy); |
| 144 |
ImVec2 render_scale; |
| 145 |
render_scale.x = (rsx == 1.0f) ? draw_data->FramebufferScale.x : 1.0f; |
| 146 |
render_scale.y = (rsy == 1.0f) ? draw_data->FramebufferScale.y : 1.0f; |
| 147 |
|
| 148 |
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) |
| 149 |
int fb_width = (int)(draw_data->DisplaySize.x * render_scale.x); |
| 150 |
int fb_height = (int)(draw_data->DisplaySize.y * render_scale.y); |
| 151 |
if (fb_width == 0 || fb_height == 0) |
| 152 |
return; |
| 153 |
|
| 154 |
// Backup SDL_Renderer state that will be modified to restore it afterwards |
| 155 |
struct BackupSDLRendererState |
| 156 |
{ |
| 157 |
SDL_Rect Viewport; |
| 158 |
bool ViewportEnabled; |
| 159 |
bool ClipEnabled; |
| 160 |
SDL_Rect ClipRect; |
| 161 |
}; |
| 162 |
BackupSDLRendererState old = {}; |
| 163 |
old.ViewportEnabled = SDL_RenderViewportSet(renderer); |
| 164 |
old.ClipEnabled = SDL_RenderClipEnabled(renderer); |
| 165 |
SDL_GetRenderViewport(renderer, &old.Viewport); |
| 166 |
SDL_GetRenderClipRect(renderer, &old.ClipRect); |
| 167 |
|
| 168 |
// Setup desired state |
| 169 |
ImGui_ImplSDLRenderer3_SetupRenderState(renderer); |
| 170 |
|
| 171 |
// Setup render state structure (for callbacks and custom texture bindings) |
| 172 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 173 |
ImGui_ImplSDLRenderer3_RenderState render_state; |
| 174 |
render_state.Renderer = renderer; |
| 175 |
platform_io.Renderer_RenderState = &render_state; |
| 176 |
|
| 177 |
// Will project scissor/clipping rectangles into framebuffer space |
| 178 |
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports |
| 179 |
ImVec2 clip_scale = render_scale; |
| 180 |
|
| 181 |
// Render command lists |
| 182 |
for (int n = 0; n < draw_data->CmdListsCount; n++) |
| 183 |
{ |
| 184 |
const ImDrawList* draw_list = draw_data->CmdLists[n]; |
| 185 |
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data; |
| 186 |
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data; |
| 187 |
|
| 188 |
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| 189 |
{ |
| 190 |
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| 191 |
if (pcmd->UserCallback) |
| 192 |
{ |
| 193 |
// User callback, registered via ImDrawList::AddCallback() |
| 194 |
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) |
| 195 |
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) |
| 196 |
ImGui_ImplSDLRenderer3_SetupRenderState(renderer); |
| 197 |
else |
| 198 |
pcmd->UserCallback(draw_list, pcmd); |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
// Project scissor/clipping rectangles into framebuffer space |
| 203 |
ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); |
| 204 |
ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); |
| 205 |
if (clip_min.x < 0.0f) { clip_min.x = 0.0f; } |
| 206 |
if (clip_min.y < 0.0f) { clip_min.y = 0.0f; } |
| 207 |
if (clip_max.x > (float)fb_width) { clip_max.x = (float)fb_width; } |
| 208 |
if (clip_max.y > (float)fb_height) { clip_max.y = (float)fb_height; } |
| 209 |
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| 210 |
continue; |
| 211 |
|
| 212 |
SDL_Rect r = { (int)(clip_min.x), (int)(clip_min.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y) }; |
| 213 |
SDL_SetRenderClipRect(renderer, &r); |
| 214 |
|
| 215 |
const float* xy = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, pos)); |
| 216 |
const float* uv = (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, uv)); |
| 217 |
const SDL_Color* color = (const SDL_Color*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + offsetof(ImDrawVert, col)); // SDL 2.0.19+ |
| 218 |
|
| 219 |
// Bind texture, Draw |
| 220 |
SDL_Texture* tex = (SDL_Texture*)pcmd->GetTexID(); |
| 221 |
SDL_RenderGeometryRaw8BitColor(renderer, bd->ColorBuffer, tex, |
| 222 |
xy, (int)sizeof(ImDrawVert), |
| 223 |
color, (int)sizeof(ImDrawVert), |
| 224 |
uv, (int)sizeof(ImDrawVert), |
| 225 |
draw_list->VtxBuffer.Size - pcmd->VtxOffset, |
| 226 |
idx_buffer + pcmd->IdxOffset, pcmd->ElemCount, sizeof(ImDrawIdx)); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
platform_io.Renderer_RenderState = nullptr; |
| 231 |
|
| 232 |
// Restore modified SDL_Renderer state |
| 233 |
SDL_SetRenderViewport(renderer, old.ViewportEnabled ? &old.Viewport : nullptr); |
| 234 |
SDL_SetRenderClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr); |
| 235 |
} |
| 236 |
|
| 237 |
// Called by Init/NewFrame/Shutdown |
| 238 |
bool ImGui_ImplSDLRenderer3_CreateFontsTexture() |
| 239 |
{ |
| 240 |
ImGuiIO& io = ImGui::GetIO(); |
| 241 |
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); |
| 242 |
|
| 243 |
// Build texture atlas |
| 244 |
unsigned char* pixels; |
| 245 |
int width, height; |
| 246 |
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. |
| 247 |
|
| 248 |
// Upload texture to graphics system |
| 249 |
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling) |
| 250 |
bd->FontTexture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, width, height); |
| 251 |
if (bd->FontTexture == nullptr) |
| 252 |
{ |
| 253 |
SDL_Log("error creating texture"); |
| 254 |
return false; |
| 255 |
} |
| 256 |
SDL_UpdateTexture(bd->FontTexture, nullptr, pixels, 4 * width); |
| 257 |
SDL_SetTextureBlendMode(bd->FontTexture, SDL_BLENDMODE_BLEND); |
| 258 |
SDL_SetTextureScaleMode(bd->FontTexture, SDL_SCALEMODE_LINEAR); |
| 259 |
|
| 260 |
// Store our identifier |
| 261 |
io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture); |
| 262 |
|
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
void ImGui_ImplSDLRenderer3_DestroyFontsTexture() |
| 267 |
{ |
| 268 |
ImGuiIO& io = ImGui::GetIO(); |
| 269 |
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); |
| 270 |
if (bd->FontTexture) |
| 271 |
{ |
| 272 |
io.Fonts->SetTexID(0); |
| 273 |
SDL_DestroyTexture(bd->FontTexture); |
| 274 |
bd->FontTexture = nullptr; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
bool ImGui_ImplSDLRenderer3_CreateDeviceObjects() |
| 279 |
{ |
| 280 |
return ImGui_ImplSDLRenderer3_CreateFontsTexture(); |
| 281 |
} |
| 282 |
|
| 283 |
void ImGui_ImplSDLRenderer3_DestroyDeviceObjects() |
| 284 |
{ |
| 285 |
ImGui_ImplSDLRenderer3_DestroyFontsTexture(); |
| 286 |
} |
| 287 |
|
| 288 |
//----------------------------------------------------------------------------- |
| 289 |
|
| 290 |
#if defined(__clang__) |
| 291 |
#pragma clang diagnostic pop |
| 292 |
#endif |
| 293 |
|
| 294 |
#endif // #ifndef IMGUI_DISABLE |