Skip to content

Commit 245b0d6

Browse files
committed
fix destruction of texture in imgui
1 parent b48a14a commit 245b0d6

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

examples/15-imgui.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int main()
6464
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
6565
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
6666
io.IniFilename = nullptr;
67-
io.Fonts->AddFontFromFileTTF(font_file.string().c_str());
67+
io.Fonts->AddFontFromFileTTF(font_file.string().c_str(), 32.0f);
6868

6969
ImguiScene scene(&scene_manager);
7070
return scene_manager.run(&scene);

include/gf2/imgui/ImguiEntity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace gf {
3434
void update_texture(ImTextureData* texture_data, RenderManager* render_manager);
3535

3636
std::vector<std::unique_ptr<Texture>> m_textures;
37+
std::vector<std::unique_ptr<Texture>> m_unused_textures;
3738

3839
DynamicBuffer m_vertices;
3940
DynamicBuffer m_indices;

library/imgui/ImguiEntity.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#include <imgui.h>
1414

15-
#include <gf2/core/Log.h>
16-
1715
#include <gf2/graphics/RenderManager.h>
1816
#include <gf2/graphics/RenderRecorder.h>
1917
#include <gf2/graphics/Texture.h>
@@ -41,6 +39,8 @@ namespace gf {
4139

4240
// update textures
4341

42+
m_unused_textures.clear(); // these textures are not used anymore
43+
4444
if (data->Textures != nullptr) {
4545
for (ImTextureData* texture_data : *data->Textures) {
4646
if (texture_data->Status != ImTextureStatus_OK) {
@@ -157,7 +157,7 @@ namespace gf {
157157
switch (data->Status) {
158158
case ImTextureStatus_WantCreate:
159159
{
160-
Log::debug("Creating a texture for imgui.");
160+
// Log::debug("Creating a texture for imgui.");
161161

162162
const Vec2I size = { data->Width, data->Height };
163163
auto texture = std::make_unique<Texture>(size, TextureUsage::TransferDestination | TextureUsage::Sampled, Format::Color8S, render_manager);
@@ -173,7 +173,7 @@ namespace gf {
173173

174174
case ImTextureStatus_WantUpdates:
175175
{
176-
Log::debug("Updating a texture for imgui.");
176+
// Log::debug("Updating a texture for imgui.");
177177

178178
auto* texture = static_cast<Texture*>(data->BackendUserData);
179179
texture->update(static_cast<std::size_t>(data->GetSizeInBytes()), static_cast<const uint8_t*>(data->GetPixels()), render_manager);
@@ -184,12 +184,13 @@ namespace gf {
184184

185185
case ImTextureStatus_WantDestroy:
186186
{
187-
Log::debug("Destroying a texture for imgui.");
187+
// Log::debug("Destroying a texture for imgui.");
188188

189189
auto* texture = static_cast<Texture*>(data->BackendUserData);
190190

191191
auto iterator = std::find_if(m_textures.begin(), m_textures.end(), [texture](const std::unique_ptr<Texture>& other) { return other.get() == texture; });
192192
assert(iterator != m_textures.end());
193+
m_unused_textures.push_back(std::move(*iterator)); // do not remove now, texture may still be in use for the current frame
193194
m_textures.erase(iterator);
194195

195196
data->SetTexID(ImTextureID_Invalid);

0 commit comments

Comments
 (0)