Skip to content

Commit d4ac646

Browse files
Matteo Bruniflightlessmango
authored andcommitted
gl: track the GL contexts and the resources that depend on them together
While at it, only create and initialize the overlay from {glX|egl}SwapBuffers(). Also default gl_bind_framebuffer parameter to 0: we basically always want to draw to the system framebuffer.
1 parent 53b5edc commit d4ac646

File tree

10 files changed

+350
-211
lines changed

10 files changed

+350
-211
lines changed

src/gl/gl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ void glXSwapIntervalEXT(void*, void*, int);
1313
int glXSwapIntervalSGI(int);
1414
int glXSwapIntervalMESA(unsigned int);
1515
int glXGetSwapIntervalMESA(void);
16+
int glXMakeContextCurrent(void*, void*, void*, void*);
1617
int glXMakeCurrent(void*, void*, void*);
17-
void* glXGetCurrentContext();
18+
void *glXGetCurrentContext();
19+
void *glXGetCurrentDrawable();
20+
void *glXGetCurrentReadDrawable();
1821
void *glXCreateContextAttribsARB(void *dpy, void *config,void *share_context, int direct, const int *attrib_list);
1922

2023
void* glXGetProcAddress(const unsigned char*);

src/gl/gl_hud.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,16 @@ void imgui_init()
104104
init_cpu_stats(params);
105105
}
106106

107-
//static
108-
void imgui_create(void *ctx, const gl_wsi plat)
107+
void imgui_create(gl_context *ctx, const gl_wsi plat)
109108
{
110-
if (inited)
109+
//SPDLOG_DEBUG("ctx {}", (void *)ctx);
110+
if (!ctx)
111111
return;
112112

113-
if (!ctx)
113+
if (inited)
114114
return;
115115

116-
imgui_shutdown();
117116
imgui_init();
118-
inited = true;
119117

120118
if (!gladLoadGL())
121119
spdlog::error("Failed to initialize OpenGL context, crash incoming");
@@ -160,6 +158,7 @@ void imgui_create(void *ctx, const gl_wsi plat)
160158
IMGUI_CHECKVERSION();
161159
ImGuiContext *saved_ctx = ImGui::GetCurrentContext();
162160
state.imgui_ctx = ImGui::CreateContext();
161+
ImGui::SetCurrentContext(state.imgui_ctx);
163162
#ifdef __linux__
164163
ImPlot::CreateContext();
165164
#endif
@@ -178,35 +177,36 @@ void imgui_create(void *ctx, const gl_wsi plat)
178177
ImGui::GetIO().IniFilename = NULL;
179178
ImGui::GetIO().DisplaySize = ImVec2(last_vp[2], last_vp[3]);
180179

181-
ImGui_ImplOpenGL3_Init();
180+
ImGui_ImplOpenGL3_Init(ctx);
182181

183182
create_fonts(nullptr, params, sw_stats.font_small, sw_stats.font_text, sw_stats.font_secondary);
184183
sw_stats.font_params_hash = params.font_params_hash;
184+
inited = true;
185185

186186
// Restore global context or ours might clash with apps that use Dear ImGui
187187
ImGui::SetCurrentContext(saved_ctx);
188188
}
189189

190-
void imgui_shutdown()
190+
void imgui_shutdown(gl_context *ctx, bool last)
191191
{
192+
//SPDLOG_DEBUG("destroying ctx {}, imgui_ctx {}, last {}", (void *)ctx, (void *)state.imgui_ctx, last);
192193
if (state.imgui_ctx) {
194+
ImGuiContext *saved_ctx = ImGui::GetCurrentContext();
193195
ImGui::SetCurrentContext(state.imgui_ctx);
194-
ImGui_ImplOpenGL3_Shutdown();
195-
ImGui::DestroyContext(state.imgui_ctx);
196-
state.imgui_ctx = nullptr;
196+
ImGui_ImplOpenGL3_Shutdown(ctx);
197+
if (last)
198+
{
199+
ImGui::DestroyContext(state.imgui_ctx);
200+
state.imgui_ctx = nullptr;
201+
inited = false;
202+
}
203+
ImGui::SetCurrentContext(saved_ctx);
197204
}
198-
inited = false;
199-
}
200-
201-
void imgui_set_context(void *ctx, const gl_wsi plat)
202-
{
203-
if (!ctx)
204-
return;
205-
imgui_create(ctx, plat);
206205
}
207206

208-
void imgui_render(unsigned int width, unsigned int height)
207+
void imgui_render(gl_context *ctx, unsigned int width, unsigned int height)
209208
{
209+
//SPDLOG_DEBUG("imgui_ctx {}", (void *)state.imgui_ctx);
210210
if (!state.imgui_ctx)
211211
return;
212212

@@ -229,10 +229,10 @@ void imgui_render(unsigned int width, unsigned int height)
229229
{
230230
sw_stats.font_params_hash = params.font_params_hash;
231231
create_fonts(nullptr, params, sw_stats.font_small, sw_stats.font_text, sw_stats.font_secondary);
232-
ImGui_ImplOpenGL3_CreateFontsTexture();
232+
ImGui_ImplOpenGL3_CreateFontsTexture(ctx);
233233
}
234234

235-
ImGui_ImplOpenGL3_NewFrame();
235+
ImGui_ImplOpenGL3_NewFrame(ctx);
236236
ImGui::NewFrame();
237237
{
238238
std::lock_guard<std::mutex> lk(notifier.mutex);

src/gl/gl_hud.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ enum gl_wsi
1515
};
1616

1717
void imgui_init();
18-
void imgui_create(void *ctx, const gl_wsi plat);
19-
void imgui_shutdown();
20-
void imgui_set_context(void *ctx, const gl_wsi plat);
21-
void imgui_render(unsigned int width, unsigned int height);
18+
void imgui_create(gl_context *ctx, const gl_wsi plat);
19+
void imgui_shutdown(gl_context *ctx, bool last);
20+
void imgui_render(gl_context *ctx, unsigned int width, unsigned int height);
2221

2322
}} // namespace
2423

0 commit comments

Comments
 (0)