Backends: Metal4: vertex/index buffers of a viewport are overwritten by the next viewport rendered in the same frame #9625

Closed
opened 2026-11-11 17:14:17 +00:00 by lailoken · 0 comments
lailoken commented 2026-11-11 17:14:17 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

Version 1.93.1 WIP, Branch: docking (cf53467ab)

Back-ends:

imgui_impl_sdl3.cpp + imgui_impl_metal4.mm

Compiler, OS:

macOS 27.0.1 (Apple Silicon) + Apple Clang 21

Full config/build information:

No response

Details:

My Issue/Question:

With multi-viewports, a viewport can be rendered with the geometry of a viewport rendered after it in the same frame. In our application (many viewports) contents are scrambled and menus flicker in and out.

ImGui_ImplMetal4_RenderDrawData() puts its vertex and index buffers back into bufferCaches[slot] right after encoding, so the next ImGui_ImplMetal4_RenderDrawData() of the same frame dequeues the same buffers and overwrites them before the GPU has executed the previous command buffer. The Metal backend returns them in the command buffer's completed handler.

Reproduces in example_sdl3_metal4 with the code below and #define FRAMES_IN_FLIGHT 1 (with the default 3 it freezes first, see #9621). I compared every presented frame of the main window with a second render of the same ImDrawData made once the GPU is idle: they differ in 123 to 230 of 230 frames depending on the run, and in 0 of 230 with the fix.

Fix proposed in #9622.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// examples/example_sdl3_metal4/main.mm (docking) with '#define FRAMES_IN_FLIGHT 1', anywhere between ImGui::NewFrame() and ImGui::Render().
// Creates a window outside the main viewport; same as dragging any window out of the main window.
const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(main_viewport->Pos.x + main_viewport->Size.x + 20.0f, main_viewport->Pos.y), ImGuiCond_Appearing);
ImGui::Begin("Outside the main viewport");
ImGui::Text("Hello from a secondary viewport!");
ImGui::End();
### Version/Branch of Dear ImGui: Version 1.93.1 WIP, Branch: docking (cf53467ab) ### Back-ends: imgui_impl_sdl3.cpp + imgui_impl_metal4.mm ### Compiler, OS: macOS 27.0.1 (Apple Silicon) + Apple Clang 21 ### Full config/build information: _No response_ ### Details: **My Issue/Question:** With multi-viewports, a viewport can be rendered with the geometry of a viewport rendered after it in the same frame. In our application (many viewports) contents are scrambled and menus flicker in and out. `ImGui_ImplMetal4_RenderDrawData()` puts its vertex and index buffers back into `bufferCaches[slot]` right after encoding, so the next `ImGui_ImplMetal4_RenderDrawData()` of the same frame dequeues the same buffers and overwrites them before the GPU has executed the previous command buffer. The Metal backend returns them in the command buffer's completed handler. Reproduces in example_sdl3_metal4 with the code below and `#define FRAMES_IN_FLIGHT 1` (with the default 3 it freezes first, see #9621). I compared every presented frame of the main window with a second render of the same `ImDrawData` made once the GPU is idle: they differ in 123 to 230 of 230 frames depending on the run, and in 0 of 230 with the fix. Fix proposed in #9622. ### Screenshots/Video: _No response_ ### Minimal, Complete and Verifiable Example code: ```cpp // examples/example_sdl3_metal4/main.mm (docking) with '#define FRAMES_IN_FLIGHT 1', anywhere between ImGui::NewFrame() and ImGui::Render(). // Creates a window outside the main viewport; same as dragging any window out of the main window. const ImGuiViewport* main_viewport = ImGui::GetMainViewport(); ImGui::SetNextWindowPos(ImVec2(main_viewport->Pos.x + main_viewport->Size.x + 20.0f, main_viewport->Pos.y), ImGuiCond_Appearing); ImGui::Begin("Outside the main viewport"); ImGui::Text("Hello from a secondary viewport!"); ImGui::End(); ```
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#9625
No description provided.