9
9
10
10
import moderngl
11
11
from OpenGL import GL
12
+ from OpenGL .error import GLError
12
13
13
14
try :
14
15
import moderngl_window
@@ -122,12 +123,19 @@ def _create_orthogonal_projection(
122
123
)
123
124
124
125
126
+ def _get_gl_error () -> int :
127
+ try :
128
+ return GL .glGetError ()
129
+ except GLError as e :
130
+ return e .err
131
+
132
+
125
133
def _extract_gl_errors () -> Iterator [int ]:
126
134
# glGetError does _not_ return the most error;
127
135
# it turns out OpenGL maintains a queue of errors,
128
136
# and glGetError pops the most recent one.
129
137
err : int
130
- while (err := GL . glGetError ()) != GL .GL_NO_ERROR :
138
+ while (err := _get_gl_error ()) != GL .GL_NO_ERROR :
131
139
yield err
132
140
133
141
@@ -398,6 +406,7 @@ def reinit(self) -> None:
398
406
if not self ._system_av_info :
399
407
raise RuntimeError ("System AV info not set" )
400
408
409
+ print ("Reinitializing ModernGL video driver..." )
401
410
context_type = (
402
411
HardwareContext (self ._callback .context_type )
403
412
if self ._callback
@@ -409,8 +418,10 @@ def reinit(self) -> None:
409
418
410
419
# TODO: Honor cache_context; try to avoid reinitializing the context
411
420
if self ._context :
412
- _clear_gl_errors ()
421
+ print ("Tearing down existing context..." )
422
+ self ._context .clear_errors ()
413
423
if self ._callback and self ._callback .context_destroy :
424
+ print ("Cleaning up core resources..." )
414
425
# If the core wants to clean up before the context is destroyed...
415
426
with self ._context .debug_scope (
416
427
"libretro.ModernGlVideoDriver.reinit.context_destroy"
@@ -419,6 +430,7 @@ def reinit(self) -> None:
419
430
_warn_unhandled_gl_errors ()
420
431
421
432
if self ._window :
433
+ print ("Destroying window..." )
422
434
self ._window .destroy ()
423
435
del self ._window
424
436
@@ -437,6 +449,7 @@ def reinit(self) -> None:
437
449
438
450
geometry = self ._system_av_info .geometry
439
451
452
+ print ("Creating new context..." )
440
453
match context_type , self ._window_class :
441
454
case HardwareContext .NONE , type () as window_class :
442
455
self ._window = window_class (
@@ -480,7 +493,8 @@ def reinit(self) -> None:
480
493
ver = self ._callback .version_major * 100 + self ._callback .version_minor * 10
481
494
self ._context = create_context (require = ver , standalone = True , share = self ._shared )
482
495
483
- _clear_gl_errors ()
496
+ print ("Created new context" )
497
+ self ._context .clear_errors ()
484
498
if self ._context .version_code >= 430 :
485
499
self ._context .enable_direct (GL .GL_DEBUG_OUTPUT )
486
500
self ._context .enable_direct (GL .GL_DEBUG_OUTPUT_SYNCHRONOUS )
@@ -489,7 +503,7 @@ def reinit(self) -> None:
489
503
self ._context .enable_direct (GL .GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR )
490
504
# TODO: Contribute this stuff to moderngl
491
505
492
- _clear_gl_errors ()
506
+ self . _context . clear_errors ()
493
507
with self ._context .debug_scope ("libretro.ModernGlVideoDriver.reinit" ):
494
508
self ._context .gc_mode = "auto"
495
509
self .__init_fbo ()
@@ -625,7 +639,7 @@ def screenshot(self, prerotate: bool = True) -> Screenshot | None:
625
639
if self ._system_av_info is None :
626
640
return None
627
641
628
- _clear_gl_errors ()
642
+ self . _context . clear_errors ()
629
643
with self ._context .debug_scope ("libretro.ModernGlVideoDriver.screenshot" ):
630
644
size = (self ._last_width , self ._last_height )
631
645
if self ._window :
@@ -636,7 +650,7 @@ def screenshot(self, prerotate: bool = True) -> Screenshot | None:
636
650
if frame is None :
637
651
return None
638
652
639
- _clear_gl_errors ()
653
+ self . _context . clear_errors ()
640
654
if not self ._callback or not self ._callback .bottom_left_origin :
641
655
# If we're using software rendering or the origin is at the bottom-left...
642
656
bytes_per_row = self ._last_width * self ._pixel_format .bytes_per_pixel
@@ -735,7 +749,7 @@ def __init_fbo(self):
735
749
self ._fbo .scissor = (0 , 0 , geometry .base_width , geometry .base_height )
736
750
self ._fbo .clear ()
737
751
738
- _clear_gl_errors ()
752
+ self . _context . clear_errors ()
739
753
740
754
def __init_hw_render (self ):
741
755
with self ._context .debug_scope ("libretro.ModernGlVideoDriver.__init_hw_render" ):
0 commit comments