Skip to content

Commit 2e48a75

Browse files
committed
Update ImGui to v1.92.1.
1 parent 32ff2c0 commit 2e48a75

File tree

12 files changed

+217
-159
lines changed

12 files changed

+217
-159
lines changed

extlibs/module-ports/imgui/imgui.cppm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export using ::ImGuiTreeNodeFlags_SpanAllColumns;
4646
export using ::ImGuiTreeNodeFlags_Selected;
4747
export using ::ImGuiWindowFlags_AlwaysAutoResize;
4848
export using ::ImTextureID;
49+
export using ::ImTextureRef;
4950
export using ::ImU32;
5051
export using ::ImVec2;
5152
export using ::ImVec4;

extlibs/module-ports/imgui/imgui.internal.cppm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export using ::ImGuiInputTextFlags_CallbackResize;
1010
export using ::ImGuiItemFlags_MixedValue;
1111
export using ::ImGuiSettingsHandler;
1212
export using ::ImGuiTextBuffer;
13-
export using ::ImGuiTreeNodeFlags_DrawTreeLines;
1413
export using ::ImGuiWindow;
1514
export using ::ImHashStr;
1615
export using ::ImRect;

impl/MainApp.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,32 @@ void vk_gltf_viewer::MainApp::run() {
162162

163163
std::array<bool, FRAMES_IN_FLIGHT> regenerateDrawCommands{};
164164

165+
// Current application running loop flow is like:
166+
//
167+
// while (application is running) {
168+
// waitForPreviousFrameGpuExecution();
169+
//
170+
// // Collect frame tasks.
171+
// pollEvents(); // <- Collect task from window events (mouse, keyboard, drag and drop, ...).
172+
// imGuiFuncs(); // <- Collect task from GUI input (buton click, ...).
173+
//
174+
// processFrameTasks();
175+
//
176+
// recordCommandBuffers(); // Will call ImGui_ImplVulkan_RenderDrawData().
177+
// submitCommandBuffers();
178+
// presentFrame();
179+
// }
180+
//
181+
// The problem is: changing glTF asset may be requested via ImGui menu click, and the previous asset will be
182+
// destroyed in processFrameTasks() function. But, imGuiFuncs() was already executed, and it will store the
183+
// ImTextureID (=VkDescriptorSet) in its internal state. Then recordCommandBuffers() will try to use the already
184+
// destroyed texture.
185+
//
186+
// To work-around, before destroying the previously loaded asset, its ownership is shared to
187+
// retainedAssetExtended[frameIndex % FRAMES_IN_FLIGHT], to prevent the asset is completely destroyed. The asset
188+
// will be retained during its frame execution, and should be destroyed after the frame execution is completed.
189+
std::array<std::shared_ptr<const gltf::AssetExtended>, FRAMES_IN_FLIGHT> retainedAssetExtended;
190+
165191
// TODO: we need more general mechanism to upload the GPU buffer data in shared data. This is just a stopgap solution
166192
// for current KHR_materials_variants implementation.
167193
const vk::raii::CommandPool graphicsCommandPool { gpu.device, vk::CommandPoolCreateInfo { {}, gpu.queueFamilies.graphicsPresent } };
@@ -265,6 +291,12 @@ void vk_gltf_viewer::MainApp::run() {
265291
}
266292
}
267293

294+
if (auto &retained = retainedAssetExtended[frameIndex % FRAMES_IN_FLIGHT]) {
295+
// The previous execution of the frame requested destroying the asset, and now the request can be done
296+
// (as ImGui will not refer its texture).
297+
retained.reset();
298+
}
299+
268300
graphicsCommandPool.reset();
269301
sharedDataUpdateCommandBuffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
270302

@@ -420,6 +452,8 @@ void vk_gltf_viewer::MainApp::run() {
420452
frameDeferredTask.setPassthruExtent(extent);
421453
},
422454
[&](const control::task::LoadGltf &task) {
455+
retainedAssetExtended[frameIndex % FRAMES_IN_FLIGHT] = assetExtended;
456+
423457
loadGltf(task.path);
424458

425459
// All planned updates related to the previous glTF asset have to be canceled.
@@ -430,6 +464,8 @@ void vk_gltf_viewer::MainApp::run() {
430464
regenerateDrawCommands.fill(true);
431465
},
432466
[&](control::task::CloseGltf) {
467+
retainedAssetExtended[frameIndex % FRAMES_IN_FLIGHT] = assetExtended;
468+
433469
closeGltf();
434470

435471
// All planned updates related to the previous glTF asset have to be canceled.
@@ -770,15 +806,10 @@ vk_gltf_viewer::MainApp::ImGuiContext::ImGuiContext(const control::AppWindow &wi
770806
ImGui::CreateContext();
771807

772808
ImGuiIO &io = ImGui::GetIO();
773-
const glm::vec2 contentScale = window.getContentScale();
774-
io.DisplayFramebufferScale = { contentScale.x, contentScale.y };
775-
#if __APPLE__
776-
io.FontGlobalScale = 1.f / io.DisplayFramebufferScale.x;
777-
#endif
778809
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
779810

780811
ImFontConfig fontConfig;
781-
fontConfig.SizePixels = 16.f * io.DisplayFramebufferScale.x;
812+
fontConfig.SizePixels = 16.f;
782813

783814
const char *defaultFontPath =
784815
#ifdef _WIN32
@@ -791,7 +822,7 @@ vk_gltf_viewer::MainApp::ImGuiContext::ImGuiContext(const control::AppWindow &wi
791822
#error "Type your own font file in here!"
792823
#endif
793824
if (std::filesystem::exists(defaultFontPath)) {
794-
io.Fonts->AddFontFromFileTTF(defaultFontPath, 16.f * io.DisplayFramebufferScale.x);
825+
io.Fonts->AddFontFromFileTTF(defaultFontPath, 16.f);
795826
}
796827
else {
797828
std::println(std::cerr, "Your system doesn't have expected system font at {}. Low-resolution font will be used instead.", defaultFontPath);
@@ -804,8 +835,6 @@ vk_gltf_viewer::MainApp::ImGuiContext::ImGuiContext(const control::AppWindow &wi
804835
asset::font::fontawesome_webfont_ttf_compressed_data_base85,
805836
fontConfig.SizePixels, &fontConfig, fontAwesomeIconRanges);
806837

807-
io.Fonts->Build();
808-
809838
ImGui_ImplGlfw_InitForVulkan(window, true);
810839
const vk::Format colorAttachmentFormat = gpu.supportSwapchainMutableFormat ? vk::Format::eB8G8R8A8Unorm : vk::Format::eB8G8R8A8Srgb;
811840
ImGui_ImplVulkan_InitInfo initInfo {

impl/control/ImGuiTaskCollector.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ void makeWindowVisible(const char* window_name) {
123123
}
124124
}
125125

126-
void hoverableImage(ImTextureID texture, const ImVec2 &size) {
126+
void hoverableImage(ImTextureRef tex_ref, const ImVec2 &size) {
127127
const ImVec2 texturePosition = ImGui::GetCursorScreenPos();
128-
ImGui::Image(texture, size);
128+
ImGui::Image(tex_ref, size);
129129

130130
if (ImGui::BeginItemTooltip()) {
131131
const ImGuiIO &io = ImGui::GetIO();
@@ -136,16 +136,16 @@ void hoverableImage(ImTextureID texture, const ImVec2 &size) {
136136
region.y = std::clamp(region.y, 0.f, size.y - zoomedPortionSize.y);
137137

138138
constexpr float zoomScale = 4.0f;
139-
ImGui::Image(texture, zoomedPortionSize * zoomScale, region / size, (region + zoomedPortionSize) / size);
139+
ImGui::Image(tex_ref, zoomedPortionSize * zoomScale, region / size, (region + zoomedPortionSize) / size);
140140
ImGui::TextUnformatted(tempStringBuffer.write("Showing: [{:.0f}, {:.0f}]x[{:.0f}, {:.0f}]", region.x, region.y, region.x + zoomedPortionSize.y, region.y + zoomedPortionSize.y));
141141

142142
ImGui::EndTooltip();
143143
}
144144
}
145145

146-
void hoverableImageCheckerboardBackground(ImTextureID texture, const ImVec2 &size) {
146+
void hoverableImageCheckerboardBackground(ImTextureRef texture_ref, const ImVec2 &size) {
147147
const ImVec2 texturePosition = ImGui::GetCursorScreenPos();
148-
ImGui::ImageCheckerboardBackground(texture, size);
148+
ImGui::ImageCheckerboardBackground(texture_ref, size);
149149

150150
if (ImGui::BeginItemTooltip()) {
151151
const ImGuiIO &io = ImGui::GetIO();
@@ -156,7 +156,7 @@ void hoverableImageCheckerboardBackground(ImTextureID texture, const ImVec2 &siz
156156
region.y = std::clamp(region.y, 0.f, size.y - zoomedPortionSize.y);
157157

158158
constexpr float zoomScale = 4.0f;
159-
ImGui::ImageCheckerboardBackground(texture, zoomedPortionSize * zoomScale, region / size, (region + zoomedPortionSize) / size);
159+
ImGui::ImageCheckerboardBackground(texture_ref, zoomedPortionSize * zoomScale, region / size, (region + zoomedPortionSize) / size);
160160
ImGui::TextUnformatted(tempStringBuffer.write("Showing: [{:.0f}, {:.0f}]x[{:.0f}, {:.0f}]", region.x, region.y, region.x + zoomedPortionSize.y, region.y + zoomedPortionSize.y));
161161

162162
ImGui::EndTooltip();
@@ -1069,7 +1069,7 @@ void vk_gltf_viewer::control::ImGuiTaskCollector::sceneHierarchy(gltf::AssetExte
10691069

10701070
bool isNodeSelected = std::ranges::all_of(ancestorNodeIndices, LIFT(assetExtended.selectedNodes.contains)) && assetExtended.selectedNodes.contains(nodeIndex);
10711071
const bool isTreeNodeOpen = ImGui::WithStyleColor(ImGuiCol_Header, ImGui::GetStyleColorVec4(ImGuiCol_HeaderActive), [&]() {
1072-
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_AllowOverlap;
1072+
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_AllowOverlap | ImGuiTreeNodeFlags_DrawLinesToNodes;
10731073
if (nodeIndex == assetExtended.hoveringNode) flags |= ImGuiTreeNodeFlags_Framed;
10741074
if (isNodeSelected) flags |= ImGuiTreeNodeFlags_Selected;
10751075
if (node.children.empty()) flags |= ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_Leaf;
@@ -1540,13 +1540,13 @@ void vk_gltf_viewer::control::ImGuiTaskCollector::nodeInspector(gltf::AssetExten
15401540

15411541
void vk_gltf_viewer::control::ImGuiTaskCollector::imageBasedLighting(
15421542
const AppState::ImageBasedLighting &info,
1543-
ImTextureID eqmapTextureImGuiDescriptorSet
1543+
ImTextureRef eqmapTextureRef
15441544
) {
15451545
if (ImGui::Begin("IBL")) {
15461546
if (ImGui::CollapsingHeader("Equirectangular map")) {
15471547
const float eqmapAspectRatio = static_cast<float>(info.eqmap.dimension.y) / info.eqmap.dimension.x;
15481548
const ImVec2 eqmapTextureSize = ImVec2 { 1.f, eqmapAspectRatio } * ImGui::GetContentRegionAvail().x;
1549-
hoverableImage(eqmapTextureImGuiDescriptorSet, eqmapTextureSize);
1549+
hoverableImage(eqmapTextureRef, eqmapTextureSize);
15501550

15511551
ImGui::WithLabel("File"sv, [&]() {
15521552
ImGui::TextLinkOpenURL(PATH_C_STR(info.eqmap.path.filename()), PATH_C_STR(info.eqmap.path));

interface/control/ImGuiTaskCollector.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace vk_gltf_viewer::control {
2727
void materialVariants(gltf::AssetExtended &assetExtended);
2828
void sceneHierarchy(gltf::AssetExtended &assetExtended);
2929
void nodeInspector(gltf::AssetExtended &assetExtended);
30-
void imageBasedLighting(const AppState::ImageBasedLighting &info, ImTextureID eqmapTextureImGuiDescriptorSet);
30+
void imageBasedLighting(const AppState::ImageBasedLighting &info, ImTextureRef eqmapTextureImGuiDescriptorSet);
3131
void rendererSetting(Renderer &renderer);
3232
void imguizmo(Renderer &renderer);
3333
void imguizmo(Renderer &renderer, gltf::AssetExtended &assetExtended);

interface/helpers/imgui/mod.cppm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace ImGui {
7373
}
7474
}
7575

76-
export bool ImageButtonWithText(std::string_view str_id, ImTextureID user_texture_id, std::string_view text, const ImVec2 &image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1), ImGuiButtonFlags flags = 0) {
76+
export bool ImageButtonWithText(std::string_view str_id, ImTextureRef tex_ref, std::string_view text, const ImVec2 &image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1), ImGuiButtonFlags flags = 0) {
7777
ImGuiStyle &style = GetStyle();
7878
ImGuiWindow* window = GetCurrentWindow();
7979
if (window->SkipItems)
@@ -100,20 +100,20 @@ namespace ImGui {
100100
if (bg_col.w > 0.0f)
101101
window->DrawList->AddRectFilled(p_min, p_max, GetColorU32(bg_col));
102102
window->DrawList->AddImage(static_cast<const vk_gltf_viewer::imgui::UserData*>(GetIO().UserData)->platformResource->checkerboardTextureID, p_min, p_max, image_size * uv0 / 16.f, image_size * uv1 / 16.f, GetColorU32(tint_col));
103-
window->DrawList->AddImage(user_texture_id, p_min, p_max, uv0, uv1, GetColorU32(tint_col));
103+
window->DrawList->AddImage(tex_ref, p_min, p_max, uv0, uv1, GetColorU32(tint_col));
104104
window->DrawList->PushClipRect({ p_min.x, p_max.y }, { p_max.x, p_max.y + GetTextLineHeight() }, true);
105105
window->DrawList->AddText({ p_min.x, p_max.y }, GetColorU32(ImGuiCol_Text), text.data(), text.data() + text.size());
106106
window->DrawList->PopClipRect();
107107

108108
return pressed;
109109
}
110110

111-
export void ImageCheckerboardBackground(ImTextureID textureId, const ImVec2 &size, const ImVec2 &uv0 = {}, const ImVec2 &uv1 = { 1, 1 }) {
111+
export void ImageCheckerboardBackground(ImTextureRef tex_ref, const ImVec2 &size, const ImVec2 &uv0 = {}, const ImVec2 &uv1 = { 1, 1 }) {
112112
const ImVec2 texturePosition = GetCursorScreenPos();
113113
SetNextItemAllowOverlap();
114114
Image(static_cast<const vk_gltf_viewer::imgui::UserData*>(GetIO().UserData)->platformResource->checkerboardTextureID, size, size * uv0 / 16.f, size * uv1 / 16.f);
115115
SetCursorScreenPos(texturePosition);
116-
Image(textureId, size, uv0, uv1);
116+
Image(tex_ref, size, uv0, uv1);
117117
}
118118

119119
export template <std::invocable F>

overlays/imgui/draw-tree-lines.patch

Lines changed: 0 additions & 55 deletions
This file was deleted.

overlays/imgui/msvc-module-fix.patch

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)