|
1 | 1 | """ |
2 | | -Load the EGL library, create a persistent GLContext, and register it with the C++ backend. |
| 2 | +Bootstrap the rendering backend used by the C++ simulation camera path. |
3 | 3 |
|
4 | | -Globals prevent the library and context from being garbage-collected. |
5 | | -Call `bootstrap()` to complete initialization. |
| 4 | +Linux uses MuJoCo's EGL backend. macOS uses a persistent MuJoCo GLContext |
| 5 | +that is made current on the Python thread before C++ creates rendering |
| 6 | +contexts. |
6 | 7 | """ |
7 | 8 |
|
8 | 9 | import ctypes |
9 | 10 | import ctypes.util |
10 | 11 | import os |
| 12 | +import sys |
11 | 13 |
|
12 | 14 | _egl_available = False |
13 | 15 | _addr_make_current = None |
14 | 16 | _egl_display = None |
15 | 17 | _egl_context = None |
| 18 | +_backend = None |
| 19 | +_gl_context = None |
16 | 20 |
|
17 | | -name = ctypes.util.find_library("EGL") |
18 | | -if name is not None: |
| 21 | +if sys.platform == "darwin": |
19 | 22 | try: |
20 | | - import mujoco.egl |
21 | | - from mujoco.egl import GLContext |
22 | | - |
23 | | - _egl = ctypes.CDLL(name, mode=os.RTLD_LOCAL | os.RTLD_NOW) |
24 | | - _addr_make_current = ctypes.cast(_egl.eglMakeCurrent, ctypes.c_void_p).value |
25 | | - _ctx = GLContext(max_width=3840, max_height=2160) |
26 | | - _egl_display = int(mujoco.egl.EGL_DISPLAY.address) |
27 | | - _egl_context = int(_ctx._context.address) |
28 | | - _egl_available = True |
| 23 | + import mujoco |
| 24 | + |
| 25 | + _gl_context = mujoco.GLContext(3840, 2160) |
| 26 | + _backend = "gl_context" |
29 | 27 | except Exception: |
30 | 28 | pass |
| 29 | +else: |
| 30 | + name = ctypes.util.find_library("EGL") |
| 31 | + if name is not None: |
| 32 | + try: |
| 33 | + import mujoco.egl |
| 34 | + from mujoco.egl import GLContext |
| 35 | + |
| 36 | + _egl = ctypes.CDLL(name, mode=os.RTLD_LOCAL | os.RTLD_NOW) |
| 37 | + _addr_make_current = ctypes.cast(_egl.eglMakeCurrent, ctypes.c_void_p).value |
| 38 | + _ctx = GLContext(max_width=3840, max_height=2160) |
| 39 | + _egl_display = int(mujoco.egl.EGL_DISPLAY.address) |
| 40 | + _egl_context = int(_ctx._context.address) |
| 41 | + _egl_available = True |
| 42 | + _backend = "egl" |
| 43 | + except Exception: |
| 44 | + pass |
31 | 45 |
|
32 | 46 |
|
33 | 47 | def bootstrap(): |
34 | | - if not _egl_available: |
35 | | - return |
36 | 48 | import rcs._core as _cxx |
37 | 49 |
|
38 | | - assert _addr_make_current is not None |
39 | | - assert _egl_display is not None |
40 | | - assert _egl_context is not None |
41 | | - _cxx.common._bootstrap_egl(_addr_make_current, _egl_display, _egl_context) |
| 50 | + if _backend == "gl_context": |
| 51 | + assert _gl_context is not None |
| 52 | + _gl_context.make_current() |
| 53 | + _cxx.common._bootstrap_gl_context() |
| 54 | + return |
| 55 | + |
| 56 | + if _backend == "egl": |
| 57 | + assert _addr_make_current is not None |
| 58 | + assert _egl_display is not None |
| 59 | + assert _egl_context is not None |
| 60 | + _cxx.common._bootstrap_egl(_addr_make_current, _egl_display, _egl_context) |
0 commit comments