From a5f3596e6cbbda1a275ac4eb66b00a8a60ecd408 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 30 Nov 2022 14:43:00 +0100 Subject: [PATCH] Backends: Allegro5: use al_draw_indexed_prim() or al_draw_prim() depending on version. (#5937) + OSX comments (#5938) Amend 185b4dde + 50aeeff --- backends/imgui_impl_allegro5.cpp | 66 +++++++++++++++++++++----------- backends/imgui_impl_osx.h | 3 +- backends/imgui_impl_osx.mm | 3 +- docs/CHANGELOG.txt | 1 + 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/backends/imgui_impl_allegro5.cpp b/backends/imgui_impl_allegro5.cpp index 4a48a2bb3..bf7f141e6 100644 --- a/backends/imgui_impl_allegro5.cpp +++ b/backends/imgui_impl_allegro5.cpp @@ -17,6 +17,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2022-11-30: Renderer: Restoring using al_draw_indexed_prim() when Allegro version is >= 5.2.5. // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. @@ -37,6 +38,7 @@ // 2018-11-30: Misc: Setting up io.BackendPlatformName/io.BackendRendererName so they can be displayed in the About Window. // 2018-06-13: Platform: Added clipboard support (from Allegro 5.1.12). // 2018-06-13: Renderer: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle. +// 2018-06-13: Renderer: Stopped using al_draw_indexed_prim() as it is buggy in Allegro's DX9 backend. // 2018-06-13: Renderer: Backup/restore transform and clipping rectangle. // 2018-06-11: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag. // 2018-04-18: Misc: Renamed file from imgui_impl_a5.cpp to imgui_impl_allegro5.cpp. @@ -56,13 +58,27 @@ #ifdef _WIN32 #include #endif -#define ALLEGRO_HAS_CLIPBOARD (ALLEGRO_VERSION_INT >= ((5 << 24) | (1 << 16) | (12 << 8))) // Clipboard only supported from Allegro 5.1.12 +#define ALLEGRO_HAS_CLIPBOARD (ALLEGRO_VERSION_INT >= ((5 << 24) | (1 << 16) | (12 << 8))) // Clipboard only supported from Allegro 5.1.12 +#define ALLEGRO_HAS_DRAW_INDEXED_PRIM (ALLEGRO_VERSION_INT >= ((5 << 24) | (2 << 16) | ( 5 << 8))) // DX9 implementation of al_draw_indexed_prim() got fixed in Allegro 5.2.5 // Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4127) // condition expression is constant #endif +struct ImDrawVertAllegro +{ + ImVec2 pos; + ImVec2 uv; + ALLEGRO_COLOR col; +}; + +// FIXME-OPT: Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 float as well.. +// FIXME-OPT: Consider inlining al_map_rgba()? +// see https://github.com/liballeg/allegro5/blob/master/src/pixels.c#L554 +// and https://github.com/liballeg/allegro5/blob/master/include/allegro5/internal/aintern_pixels.h +#define DRAW_VERT_IMGUI_TO_ALLEGRO(DST, SRC) { (DST)->pos = (SRC)->pos; (DST)->uv = (SRC)->uv; unsigned char* c = (unsigned char*)&(SRC)->col; (DST)->col = al_map_rgba(c[0], c[1], c[2], c[3]); } + // Allegro Data struct ImGui_ImplAllegro5_Data { @@ -73,6 +89,9 @@ struct ImGui_ImplAllegro5_Data ALLEGRO_VERTEX_DECL* VertexDecl; char* ClipboardTextData; + ImVector BufVertices; + ImVector BufIndices; + ImGui_ImplAllegro5_Data() { memset((void*)this, 0, sizeof(*this)); } }; @@ -81,13 +100,6 @@ struct ImGui_ImplAllegro5_Data // FIXME: multi-context support is not well tested and probably dysfunctional in this backend. static ImGui_ImplAllegro5_Data* ImGui_ImplAllegro5_GetBackendData() { return ImGui::GetCurrentContext() ? (ImGui_ImplAllegro5_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; } -struct ImDrawVertAllegro -{ - ImVec2 pos; - ImVec2 uv; - ALLEGRO_COLOR col; -}; - static void ImGui_ImplAllegro5_SetupRenderState(ImDrawData* draw_data) { // Setup blending @@ -132,34 +144,40 @@ void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data) { const ImDrawList* cmd_list = draw_data->CmdLists[n]; - // FIXME-OPT: Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 float as well.. - static ImVector vertices; + ImVector& vertices = bd->BufVertices; +#if ALLEGRO_HAS_DRAW_INDEXED_PRIM vertices.resize(cmd_list->VtxBuffer.Size); for (int i = 0; i < cmd_list->VtxBuffer.Size; i++) { const ImDrawVert* src_v = &cmd_list->VtxBuffer[i]; ImDrawVertAllegro* dst_v = &vertices[i]; - dst_v->pos = src_v->pos; - dst_v->uv = src_v->uv; - unsigned char* c = (unsigned char*)&src_v->col; - dst_v->col = al_map_rgba(c[0], c[1], c[2], c[3]); + DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v); } - const int* indices = nullptr; if (sizeof(ImDrawIdx) == 2) { - // FIXME-OPT: Unfortunately Allegro doesn't support 16-bit indices.. You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices. + // FIXME-OPT: Allegro doesn't support 16-bit indices. + // You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices. // Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful. - static ImVector indices_converted; - indices_converted.resize(cmd_list->IdxBuffer.Size); + bd->BufIndices.resize(cmd_list->IdxBuffer.Size); for (int i = 0; i < cmd_list->IdxBuffer.Size; ++i) - indices_converted[i] = (int)cmd_list->IdxBuffer.Data[i]; - indices = indices_converted.Data; + bd->BufIndices[i] = (int)cmd_list->IdxBuffer.Data[i]; + indices = bd->BufIndices.Data; } else if (sizeof(ImDrawIdx) == 4) { indices = (const int*)cmd_list->IdxBuffer.Data; } +#else + // Allegro's implementation of al_draw_indexed_prim() for DX9 was broken until 5.2.5. Unindex buffers ourselves while converting vertex format. + vertices.resize(cmd_list->IdxBuffer.Size); + for (int i = 0; i < cmd_list->IdxBuffer.Size; i++) + { + const ImDrawVert* src_v = &cmd_list->VtxBuffer[cmd_list->IdxBuffer[i]]; + ImDrawVertAllegro* dst_v = &vertices[i]; + DRAW_VERT_IMGUI_TO_ALLEGRO(dst_v, src_v); + } +#endif // Render command lists ImVec2 clip_off = draw_data->DisplayPos; @@ -186,9 +204,11 @@ void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data) // Apply scissor/clipping rectangle, Draw ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->GetTexID(); al_set_clipping_rectangle(clip_min.x, clip_min.y, clip_max.x - clip_min.x, clip_max.y - clip_min.y); - if(pcmd->ElemCount > 0) { - al_draw_indexed_prim(&vertices[0], bd->VertexDecl, texture, &indices[pcmd->IdxOffset], pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST); - } +#if ALLEGRO_HAS_DRAW_INDEXED_PRIM + al_draw_indexed_prim(&vertices[0], bd->VertexDecl, texture, &indices[pcmd->IdxOffset], pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST); +#else + al_draw_prim(&vertices[0], bd->VertexDecl, texture, pcmd->IdxOffset, pcmd->IdxOffset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST); +#endif } } } diff --git a/backends/imgui_impl_osx.h b/backends/imgui_impl_osx.h index 70619e39c..fd8755b66 100644 --- a/backends/imgui_impl_osx.h +++ b/backends/imgui_impl_osx.h @@ -1,6 +1,7 @@ // dear imgui: Platform Backend for OSX / Cocoa // This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan, Metal..) -// [ALPHA] Early backend, not well tested. If you want a portable application, prefer using the GLFW or SDL platform Backends on Mac. +// - Not well tested. If you want a portable application, prefer using the GLFW or SDL platform Backends on Mac. +// - Requires linking with the GameController framework ("-framework GameController"). // Implemented features: // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. diff --git a/backends/imgui_impl_osx.mm b/backends/imgui_impl_osx.mm index 2505bbd3b..aa65d7131 100644 --- a/backends/imgui_impl_osx.mm +++ b/backends/imgui_impl_osx.mm @@ -1,6 +1,7 @@ // dear imgui: Platform Backend for OSX / Cocoa // This needs to be used along with a Renderer (e.g. OpenGL2, OpenGL3, Vulkan, Metal..) -// [ALPHA] Early backend, not well tested. If you want a portable application, prefer using the GLFW or SDL platform Backends on Mac. +// - Not well tested. If you want a portable application, prefer using the GLFW or SDL platform Backends on Mac. +// - Requires linking with the GameController framework ("-framework GameController"). // Implemented features: // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 93f788c2b..c4720e789 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -43,6 +43,7 @@ Other changes: clipping region. Regression added in 1.89. (#5720, #5919) - Misc: fixed parameters to IMGUI_DEBUG_LOG() not being dead-stripped when building with IMGUI_DISABLE_DEBUG_TOOLS is used. (#5901) [@Teselka] +- Backends: Allegro5: restoring using al_draw_indexed_prim() when Allegro version is >= 5.2.5. (#5937) [@Espyo] -----------------------------------------------------------------------