@@ -1109,6 +1109,7 @@ WrappedOpenGL::ContextData &WrappedOpenGL::GetCtxData()
11091109void WrappedOpenGL::DeleteContext (void *contextHandle)
11101110{
11111111 ContextData &ctxdata = m_ContextData[contextHandle];
1112+ GLWindowingData existing = m_ActiveContexts[Threading::GetCurrentID ()];
11121113
11131114 RDCLOG (" Deleting context %p" , contextHandle);
11141115
@@ -1131,28 +1132,76 @@ void WrappedOpenGL::DeleteContext(void *contextHandle)
11311132 }
11321133 }
11331134
1134- // if this is the last context in the share group, delete the group.
1135- if (lastInGroup)
1135+ bool canDelete = (existing.ctx == ctxdata.ctx );
1136+
1137+ GLWindowingData savedContext;
1138+
1139+ if (existing.ctx == NULL )
11361140 {
1137- RDCLOG (" Deleting shader group %p" , ctxdata.shareGroup );
1138- delete ctxdata.shareGroup ;
1141+ if (m_Platform.PushChildContext (existing, ctxdata.shareGroup ->m_BackDoor , &savedContext))
1142+ {
1143+ canDelete = true ;
1144+ }
1145+ }
1146+
1147+ GLenum err = glGetError ();
1148+ if (canDelete)
1149+ {
1150+ if (ctxdata.built && ctxdata.ready )
1151+ {
1152+ ctxdata.ArrayMS .Destroy ();
1153+ if (ctxdata.Program )
1154+ GL .glDeleteProgram (ctxdata.Program );
1155+ if (ctxdata.ArrayBuffer )
1156+ GL .glDeleteBuffers (1 , &ctxdata.ArrayBuffer );
1157+ if (ctxdata.GlyphTexture )
1158+ GL .glDeleteTextures (1 , &ctxdata.GlyphTexture );
1159+ }
1160+
1161+ if (ctxdata.m_ClientMemoryVBOs [0 ])
1162+ glDeleteBuffers (ARRAY_COUNT (ctxdata.m_ClientMemoryVBOs ), ctxdata.m_ClientMemoryVBOs );
1163+ err = glGetError ();
1164+ if (ctxdata.m_ClientMemoryIBO )
1165+ glDeleteBuffers (1 , &ctxdata.m_ClientMemoryIBO );
1166+ err = glGetError ();
1167+ }
1168+ else
1169+ {
1170+ if (!lastInGroup)
1171+ RDCWARN (" leaking objects in context %p, assuming driver cleanup eventually" , ctxdata.ctx );
1172+
1173+ // release our tracking to ensure we don't false-alias
1174+ GLResource res = BufferRes ({ctxdata.ctx , ctxdata.shareGroup }, ctxdata.m_ClientMemoryIBO );
1175+ if (GetResourceManager ()->HasResource (res))
1176+ {
1177+ if (GetResourceManager ()->HasResourceRecord (res))
1178+ GetResourceManager ()->GetResourceRecord (res)->Delete (GetResourceManager ());
1179+ GetResourceManager ()->UnregisterResource (res);
1180+ }
1181+ for (GLuint buf : ctxdata.m_ClientMemoryVBOs )
1182+ {
1183+ res.name = buf;
1184+ if (GetResourceManager ()->HasResource (res))
1185+ {
1186+ if (GetResourceManager ()->HasResourceRecord (res))
1187+ GetResourceManager ()->GetResourceRecord (res)->Delete (GetResourceManager ());
1188+ GetResourceManager ()->UnregisterResource (res);
1189+ }
1190+ }
11391191 }
11401192
1141- if (ctxdata. built && ctxdata. ready )
1193+ if (savedContext. ctx != NULL )
11421194 {
1143- ctxdata.ArrayMS .Destroy ();
1144- if (ctxdata.Program )
1145- GL .glDeleteProgram (ctxdata.Program );
1146- if (ctxdata.ArrayBuffer )
1147- GL .glDeleteBuffers (1 , &ctxdata.ArrayBuffer );
1148- if (ctxdata.GlyphTexture )
1149- GL .glDeleteTextures (1 , &ctxdata.GlyphTexture );
1195+ // restore the context
1196+ m_Platform.PopChildContext (existing, ctxdata.shareGroup ->m_BackDoor , savedContext);
11501197 }
11511198
1152- if (ctxdata.m_ClientMemoryVBOs [0 ])
1153- glDeleteBuffers (ARRAY_COUNT (ctxdata.m_ClientMemoryVBOs ), ctxdata.m_ClientMemoryVBOs );
1154- if (ctxdata.m_ClientMemoryIBO )
1155- glDeleteBuffers (1 , &ctxdata.m_ClientMemoryIBO );
1199+ // if this is the last context in the share group, delete the group.
1200+ if (lastInGroup)
1201+ {
1202+ RDCLOG (" Deleting shader group %p" , ctxdata.shareGroup );
1203+ delete ctxdata.shareGroup ;
1204+ }
11561205
11571206 if (ctxdata.m_ContextDataRecord )
11581207 {
0 commit comments