Skip to content

Commit 3c3197d

Browse files
committed
gl: Fix crashes when creating new context
1 parent cffc136 commit 3c3197d

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

rpcs3/rpcs3qt/gl_gs_frame.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,43 @@ void gl_gs_frame::reset()
3535

3636
draw_context_t gl_gs_frame::make_context()
3737
{
38+
// This whole function should run in the main GUI thread.
39+
// This really matters on Windows where a lot of wgl internals are stashed in the TEB.
40+
3841
auto context = new GLContext();
3942
context->handle = new QOpenGLContext();
43+
bool success = true;
4044

41-
if (m_primary_context)
45+
Emu.BlockingCallFromMainThread([&]()
4246
{
43-
QOffscreenSurface* surface = nullptr;
44-
45-
// Workaround for the Qt warning: "Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures."
46-
Emu.BlockingCallFromMainThread([&]()
47+
if (m_primary_context)
4748
{
48-
surface = new QOffscreenSurface();
49+
QOffscreenSurface* surface = new QOffscreenSurface();
4950
surface->setFormat(m_format);
5051
surface->create();
51-
});
5252

53-
// Share resources with the first created context
54-
context->handle->setShareContext(m_primary_context->handle);
55-
context->surface = surface;
56-
context->owner = true;
57-
}
58-
else
59-
{
60-
// This is the first created context, all others will share resources with this one
61-
m_primary_context = context;
62-
context->surface = this;
63-
context->owner = false;
64-
}
53+
// Share resources with the first created context
54+
context->handle->setShareContext(m_primary_context->handle);
55+
context->surface = surface;
56+
context->owner = true;
57+
}
58+
else
59+
{
60+
// This is the first created context, all others will share resources with this one
61+
m_primary_context = context;
62+
context->surface = this;
63+
context->owner = false;
64+
}
65+
66+
context->handle->setFormat(m_format);
6567

66-
context->handle->setFormat(m_format);
68+
if (!context->handle->create())
69+
{
70+
success = false;
71+
}
72+
});
6773

68-
if (!context->handle->create())
74+
if (!success)
6975
{
7076
fmt::throw_exception("Failed to create OpenGL context");
7177
}
@@ -110,8 +116,8 @@ void gl_gs_frame::delete_context(draw_context_t ctx)
110116
gl_ctx->handle->doneCurrent();
111117

112118
#ifdef _MSC_VER
113-
//AMD driver crashes when executing wglDeleteContext
114-
//Catch with SEH
119+
// AMD driver crashes when executing wglDeleteContext, probably because the current thread does not own the context.
120+
// Catch with SEH
115121
__try
116122
{
117123
delete gl_ctx->handle;

0 commit comments

Comments
 (0)