99
1010import moderngl
1111from OpenGL import GL
12+ from OpenGL .error import GLError
1213
1314try :
1415 import moderngl_window
@@ -122,12 +123,19 @@ def _create_orthogonal_projection(
122123 )
123124
124125
126+ def _get_gl_error () -> int :
127+ try :
128+ return GL .glGetError ()
129+ except GLError as e :
130+ return e .err
131+
132+
125133def _extract_gl_errors () -> Iterator [int ]:
126134 # glGetError does _not_ return the most error;
127135 # it turns out OpenGL maintains a queue of errors,
128136 # and glGetError pops the most recent one.
129137 err : int
130- while (err := GL . glGetError ()) != GL .GL_NO_ERROR :
138+ while (err := _get_gl_error ()) != GL .GL_NO_ERROR :
131139 yield err
132140
133141
@@ -398,6 +406,7 @@ def reinit(self) -> None:
398406 if not self ._system_av_info :
399407 raise RuntimeError ("System AV info not set" )
400408
409+ print ("Reinitializing ModernGL video driver..." )
401410 context_type = (
402411 HardwareContext (self ._callback .context_type )
403412 if self ._callback
@@ -409,8 +418,10 @@ def reinit(self) -> None:
409418
410419 # TODO: Honor cache_context; try to avoid reinitializing the context
411420 if self ._context :
412- _clear_gl_errors ()
421+ print ("Tearing down existing context..." )
422+ self ._context .clear_errors ()
413423 if self ._callback and self ._callback .context_destroy :
424+ print ("Cleaning up core resources..." )
414425 # If the core wants to clean up before the context is destroyed...
415426 with self ._context .debug_scope (
416427 "libretro.ModernGlVideoDriver.reinit.context_destroy"
@@ -419,6 +430,7 @@ def reinit(self) -> None:
419430 _warn_unhandled_gl_errors ()
420431
421432 if self ._window :
433+ print ("Destroying window..." )
422434 self ._window .destroy ()
423435 del self ._window
424436
@@ -437,6 +449,7 @@ def reinit(self) -> None:
437449
438450 geometry = self ._system_av_info .geometry
439451
452+ print ("Creating new context..." )
440453 match context_type , self ._window_class :
441454 case HardwareContext .NONE , type () as window_class :
442455 self ._window = window_class (
@@ -480,7 +493,8 @@ def reinit(self) -> None:
480493 ver = self ._callback .version_major * 100 + self ._callback .version_minor * 10
481494 self ._context = create_context (require = ver , standalone = True , share = self ._shared )
482495
483- _clear_gl_errors ()
496+ print ("Created new context" )
497+ self ._context .clear_errors ()
484498 if self ._context .version_code >= 430 :
485499 self ._context .enable_direct (GL .GL_DEBUG_OUTPUT )
486500 self ._context .enable_direct (GL .GL_DEBUG_OUTPUT_SYNCHRONOUS )
@@ -489,7 +503,7 @@ def reinit(self) -> None:
489503 self ._context .enable_direct (GL .GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR )
490504 # TODO: Contribute this stuff to moderngl
491505
492- _clear_gl_errors ()
506+ self . _context . clear_errors ()
493507 with self ._context .debug_scope ("libretro.ModernGlVideoDriver.reinit" ):
494508 self ._context .gc_mode = "auto"
495509 self .__init_fbo ()
@@ -625,7 +639,7 @@ def screenshot(self, prerotate: bool = True) -> Screenshot | None:
625639 if self ._system_av_info is None :
626640 return None
627641
628- _clear_gl_errors ()
642+ self . _context . clear_errors ()
629643 with self ._context .debug_scope ("libretro.ModernGlVideoDriver.screenshot" ):
630644 size = (self ._last_width , self ._last_height )
631645 if self ._window :
@@ -636,7 +650,7 @@ def screenshot(self, prerotate: bool = True) -> Screenshot | None:
636650 if frame is None :
637651 return None
638652
639- _clear_gl_errors ()
653+ self . _context . clear_errors ()
640654 if not self ._callback or not self ._callback .bottom_left_origin :
641655 # If we're using software rendering or the origin is at the bottom-left...
642656 bytes_per_row = self ._last_width * self ._pixel_format .bytes_per_pixel
@@ -735,7 +749,7 @@ def __init_fbo(self):
735749 self ._fbo .scissor = (0 , 0 , geometry .base_width , geometry .base_height )
736750 self ._fbo .clear ()
737751
738- _clear_gl_errors ()
752+ self . _context . clear_errors ()
739753
740754 def __init_hw_render (self ):
741755 with self ._context .debug_scope ("libretro.ModernGlVideoDriver.__init_hw_render" ):
0 commit comments