Skip to content

Commit 7ded904

Browse files
committed
Use moderngl 5.12's debug APIs instead of hacking them together with PyOpenGL
1 parent 8861ebf commit 7ded904

File tree

2 files changed

+28
-84
lines changed

2 files changed

+28
-84
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ docs = [
6060
]
6161
doc = ["libretro.py[docs]"] # Alias for the docs extra
6262
opengl = [
63-
'moderngl[headless] == 5.10.*',
63+
'moderngl[headless] >= 5.12.*',
6464
'PyOpenGL == 3.1.*',
6565
]
6666
opengl-window = [

src/libretro/drivers/video/opengl/moderngl.py

Lines changed: 27 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import struct
22
import warnings
33
from array import array
4-
from collections.abc import Callable, Iterator, Sequence, Set
5-
from contextlib import contextmanager
4+
from collections.abc import Iterator, Sequence, Set
65
from copy import deepcopy
76
from importlib import resources
87
from sys import modules
@@ -267,11 +266,6 @@ def __init__(
267266

268267
self._window_class = moderngl_window.get_local_window_cls(window_mode)
269268

270-
self._debug_group_id = 0
271-
self.__gl_push_debug_group: Callable[[int, int, int, bytes], None] | None = None
272-
self.__gl_pop_debug_group: Callable[[], None] | None = None
273-
self.__gl_object_label: Callable[[int, int, int, bytes], None] | None = None
274-
275269
def __del__(self):
276270
if self._cpu_color:
277271
del self._cpu_color
@@ -353,7 +347,7 @@ def refresh(
353347
) -> None:
354348
_clear_gl_errors()
355349

356-
with self.__debug_group(b"libretro.ModernGlVideoDriver.refresh"):
350+
with self._context.debug_scope("libretro.ModernGlVideoDriver.refresh"):
357351
match data:
358352
case FrameBufferSpecial.DUPE:
359353
# Do nothing, we're re-rendering the previous frame
@@ -377,7 +371,9 @@ def refresh(
377371
self._vao.render(moderngl.TRIANGLE_STRIP)
378372

379373
if self._window:
380-
with self.__debug_group(b"libretro.ModernGlVideoDriver.refresh.swap_buffers"):
374+
with self._context.debug_scope(
375+
"libretro.ModernGlVideoDriver.refresh.swap_buffers"
376+
):
381377
self._window.fbo.use()
382378
self._context.copy_framebuffer(self._window.fbo, self._fbo)
383379
self._window.swap_buffers()
@@ -416,7 +412,9 @@ def reinit(self) -> None:
416412
_clear_gl_errors()
417413
if self._callback and self._callback.context_destroy:
418414
# If the core wants to clean up before the context is destroyed...
419-
with self.__debug_group(b"libretro.ModernGlVideoDriver.reinit.context_destroy"):
415+
with self._context.debug_scope(
416+
"libretro.ModernGlVideoDriver.reinit.context_destroy"
417+
):
420418
self._callback.context_destroy()
421419

422420
_warn_unhandled_gl_errors()
@@ -436,9 +434,6 @@ def reinit(self) -> None:
436434
del self._shader_program
437435
del self._vbo
438436
del self._cpu_color
439-
del self.__gl_push_debug_group
440-
del self.__gl_pop_debug_group
441-
del self.__gl_object_label
442437
# Destroy the OpenGL context and create a new one
443438

444439
geometry = self._system_av_info.geometry
@@ -488,25 +483,15 @@ def reinit(self) -> None:
488483

489484
_clear_gl_errors()
490485
if self._context.version_code >= 430:
491-
self.__gl_push_debug_group = GL.glPushDebugGroup
492-
self.__gl_pop_debug_group = GL.glPopDebugGroup
493-
self.__gl_object_label = GL.glObjectLabel
494486
self._context.enable_direct(GL.GL_DEBUG_OUTPUT)
495487
self._context.enable_direct(GL.GL_DEBUG_OUTPUT_SYNCHRONOUS)
496488
elif "GL_KHR_debug" in self._context.extensions and GL.glPushDebugGroupKHR:
497-
self.__gl_push_debug_group = GL.glPushDebugGroupKHR
498-
self.__gl_pop_debug_group = GL.glPopDebugGroupKHR
499-
self.__gl_object_label = GL.glObjectLabelKHR
500489
self._context.enable_direct(GL.GL_DEBUG_OUTPUT_KHR)
501490
self._context.enable_direct(GL.GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR)
502491
# TODO: Contribute this stuff to moderngl
503-
else:
504-
self.__gl_push_debug_group = None
505-
self.__gl_pop_debug_group = None
506-
self.__gl_object_label = None
507492

508493
_clear_gl_errors()
509-
with self.__debug_group(b"libretro.ModernGlVideoDriver.reinit"):
494+
with self._context.debug_scope("libretro.ModernGlVideoDriver.reinit"):
510495
self._context.gc_mode = "auto"
511496
self.__init_fbo()
512497

@@ -529,9 +514,9 @@ def reinit(self) -> None:
529514
)
530515
# TODO: Make the particular names configurable
531516

532-
self.__object_label(self._shader_program, b"libretro.py Shader Program")
533-
self.__object_label(self._vbo, b"libretro.py Screen VBO")
534-
self.__object_label(self._vao, b"libretro.py Screen VAO")
517+
self._shader_program.label = "libretro.py Shader Program"
518+
self._vbo.label = "libretro.py Screen VBO"
519+
self._vao.label = "libretro.py Screen VAO"
535520

536521
# TODO: Honor debug_context; enable debugging features if requested
537522
if self._callback is not None and context_type != HardwareContext.NONE:
@@ -540,8 +525,10 @@ def reinit(self) -> None:
540525

541526
if self._callback.context_reset:
542527
# If the core wants to set up resources after the context is created...
543-
_clear_gl_errors()
544-
with self.__debug_group(b"libretro.ModernGlVideoDriver.reinit.context_reset"):
528+
self._context.clear_errors()
529+
with self._context.debug_scope(
530+
"libretro.ModernGlVideoDriver.reinit.context_reset"
531+
):
545532
self._callback.context_reset()
546533

547534
_warn_unhandled_gl_errors()
@@ -641,7 +628,7 @@ def screenshot(self, prerotate: bool = True) -> Screenshot | None:
641628
return None
642629

643630
_clear_gl_errors()
644-
with self.__debug_group(b"libretro.ModernGlVideoDriver.screenshot"):
631+
with self._context.debug_scope("libretro.ModernGlVideoDriver.screenshot"):
645632
size = (self._last_width, self._last_height)
646633
if self._window:
647634
frame = self._window.fbo.read(size, 4)
@@ -724,7 +711,7 @@ def __get_framebuffer_size(self) -> tuple[int, int]:
724711
return width, height
725712

726713
def __init_fbo(self):
727-
with self.__debug_group(b"libretro.ModernGlVideoDriver.__init_fbo"):
714+
with self._context.debug_scope("libretro.ModernGlVideoDriver.__init_fbo"):
728715
assert self._context is not None
729716
assert self._system_av_info is not None
730717

@@ -742,9 +729,9 @@ def __init_fbo(self):
742729
# Similar to glGenFramebuffers, glBindFramebuffer, and glFramebufferTexture2D
743730
self._fbo = self._context.framebuffer(self._color, self._depth)
744731

745-
self.__object_label(self._color, b"libretro.py Main FBO Color Attachment")
746-
self.__object_label(self._depth, b"libretro.py Main FBO Depth Attachment")
747-
self.__object_label(self._fbo, b"libretro.py Main FBO")
732+
self._color.label = "libretro.py Main FBO Color Attachment"
733+
self._depth.label = "libretro.py Main FBO Depth Attachment"
734+
self._fbo.label = "libretro.py Main FBO"
748735

749736
self._fbo.viewport = (0, 0, geometry.base_width, geometry.base_height)
750737
self._fbo.scissor = (0, 0, geometry.base_width, geometry.base_height)
@@ -753,7 +740,7 @@ def __init_fbo(self):
753740
_clear_gl_errors()
754741

755742
def __init_hw_render(self):
756-
with self.__debug_group(b"libretro.ModernGlVideoDriver.__init_hw_render"):
743+
with self._context.debug_scope("libretro.ModernGlVideoDriver.__init_hw_render"):
757744
assert self._context is not None
758745
assert self._callback is not None
759746
assert self._system_av_info is not None
@@ -783,17 +770,13 @@ def __init_hw_render(self):
783770
)
784771
self._hw_render_fbo.clear()
785772

786-
self.__object_label(self._hw_render_fbo, b"libretro.py Hardware Rendering FBO")
787-
self.__object_label(
788-
self._hw_render_color, b"libretro.py Hardware Rendering FBO Color Attachment"
789-
)
773+
self._hw_render_fbo.label = "libretro.py Hardware Rendering FBO"
774+
self._hw_render_color.label = "libretro.py Hardware Rendering FBO Color Attachment"
790775
if self._hw_render_depth:
791-
self.__object_label(
792-
self._hw_render_depth, b"libretro.py Hardware Rendering FBO Depth Attachment"
793-
)
776+
self._hw_render_depth.label = "libretro.py Hardware Rendering FBO Depth Attachment"
794777

795778
def __update_cpu_texture(self, data: memoryview, width: int, height: int, pitch: int):
796-
with self.__debug_group(b"libretro.ModernGlVideoDriver.__update_cpu_texture"):
779+
with self._context.debug_scope("libretro.ModernGlVideoDriver.__update_cpu_texture"):
797780
if self._cpu_color and self._cpu_color.size == (width, height):
798781
# If we have a texture for CPU-rendered output, and it's the right size...
799782
self._cpu_color.write(data)
@@ -820,46 +803,7 @@ def __update_cpu_texture(self, data: memoryview, width: int, height: int, pitch:
820803
)
821804
# moderngl can't natively express GL_RGB5
822805

823-
self.__object_label(self._cpu_color, b"libretro.py CPU-Rendered Frame")
824-
825-
def __object_label(self, obj: ModernGlResource, label: bytes):
826-
if self.__gl_object_label:
827-
# TODO: Honor self._callback.debug_context
828-
gltype: int
829-
match obj:
830-
case Buffer():
831-
gltype = GL.GL_BUFFER
832-
case Program():
833-
gltype = GL.GL_PROGRAM
834-
case VertexArray():
835-
gltype = GL.GL_VERTEX_ARRAY
836-
case Query():
837-
gltype = GL.GL_QUERY
838-
case Sampler():
839-
gltype = GL.GL_SAMPLER
840-
case Texture():
841-
gltype = GL.GL_TEXTURE
842-
case Renderbuffer():
843-
gltype = GL.GL_RENDERBUFFER
844-
case Framebuffer():
845-
gltype = GL.GL_FRAMEBUFFER
846-
case _:
847-
raise TypeError(f"Unsupported object type: {type(obj).__name__}")
848-
849-
self.__gl_object_label(gltype, obj.glo, -1, label)
850-
851-
@contextmanager
852-
def __debug_group(self, label: bytes):
853-
if self.__gl_push_debug_group:
854-
self._debug_group_id = self._debug_group_id + 1
855-
self.__gl_push_debug_group(
856-
GL.GL_DEBUG_SOURCE_APPLICATION, self._debug_group_id, -1, label
857-
)
858-
yield None
859-
self.__gl_pop_debug_group()
860-
self._debug_group_id = self._debug_group_id - 1
861-
else:
862-
yield None
806+
self._cpu_color.label = "libretro.py CPU-Rendered Frame"
863807

864808

865809
__all__ = ["ModernGlVideoDriver"]

0 commit comments

Comments
 (0)