Skip to content

Commit b69de33

Browse files
saran-tcopybara-github
authored andcommitted
Accept the same values of MUJOCO_GL as the mujoco package.
The `mujoco` package also makes use of the `MUJOCO_GL` environment variable to switch between rendering backends, which are implemented separately from the ones currently used in `dm_control`. However, `mujoco` accepts a wider range of `MUJOCO_GL` values, in particular it allows `off` to be specified to disable rendering entirely. Before this change, specifying `MUJOCO_GL=off` will result in `dm_control` complaining that it is an invalid value. This change eliminates the conflicting `MUJOCO_GL` values expected by the two libraries. Fixes #324. PiperOrigin-RevId: 461577872 Change-Id: Ie962040395b766affc8342ab9d9fd09a0afe15cb
1 parent 63b79ed commit b69de33

File tree

5 files changed

+48
-27
lines changed

5 files changed

+48
-27
lines changed

dm_control/_render/__init__.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,43 +47,59 @@ def _import_osmesa():
4747
return OSMesaContext
4848
# pylint: enable=g-import-not-at-top
4949

50-
_ALL_RENDERERS = collections.OrderedDict([
50+
51+
def _no_renderer():
52+
def no_renderer(*args, **kwargs):
53+
del args, kwargs
54+
raise RuntimeError('No OpenGL rendering backend is available.')
55+
return no_renderer
56+
57+
58+
_ALL_RENDERERS = (
5159
(constants.GLFW, _import_glfw),
5260
(constants.EGL, _import_egl),
5361
(constants.OSMESA, _import_osmesa),
54-
])
62+
)
63+
64+
_NO_RENDERER = (
65+
(constants.NO_RENDERER, _no_renderer),
66+
)
5567

5668

5769
if BACKEND is not None:
5870
# If a backend was specified, try importing it and error if unsuccessful.
59-
try:
60-
import_func = _ALL_RENDERERS[BACKEND]
61-
except KeyError:
71+
import_func = None
72+
for names, importer in _ALL_RENDERERS + _NO_RENDERER:
73+
if BACKEND in names:
74+
import_func = importer
75+
BACKEND = names[0] # canonicalize the renderer name
76+
break
77+
if import_func is None:
78+
all_names = set()
79+
for names, _ in _ALL_RENDERERS + _NO_RENDERER:
80+
all_names.update(names)
6281
raise RuntimeError(
6382
'Environment variable {} must be one of {!r}: got {!r}.'
64-
.format(constants.MUJOCO_GL, _ALL_RENDERERS.keys(), BACKEND))
83+
.format(constants.MUJOCO_GL, sorted(all_names), BACKEND))
6584
logging.info('MUJOCO_GL=%s, attempting to import specified OpenGL backend.',
6685
BACKEND)
67-
Renderer = import_func() # pylint: disable=invalid-name
86+
Renderer = import_func()
6887
else:
6988
logging.info('MUJOCO_GL is not set, so an OpenGL backend will be chosen '
7089
'automatically.')
7190
# Otherwise try importing them in descending order of priority until
7291
# successful.
73-
for name, import_func in _ALL_RENDERERS.items():
92+
for names, import_func in _ALL_RENDERERS:
7493
try:
7594
Renderer = import_func()
76-
BACKEND = name
77-
logging.info('Successfully imported OpenGL backend: %s', name)
95+
BACKEND = names[0]
96+
logging.info('Successfully imported OpenGL backend: %s', names[0])
7897
break
7998
except ImportError:
80-
logging.info('Failed to import OpenGL backend: %s', name)
99+
logging.info('Failed to import OpenGL backend: %s', names[0])
81100
if BACKEND is None:
82101
logging.info('No OpenGL backend could be imported. Attempting to create a '
83102
'rendering context will result in a RuntimeError.')
103+
Renderer = _no_renderer()
84104

85-
def Renderer(*args, **kwargs): # pylint: disable=function-redefined,invalid-name
86-
del args, kwargs
87-
raise RuntimeError('No OpenGL rendering backend is available.')
88-
89-
USING_GPU = BACKEND in (constants.EGL, constants.GLFW)
105+
USING_GPU = BACKEND in constants.EGL + constants.GLFW

dm_control/_render/constants.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
PYOPENGL_PLATFORM = 'PYOPENGL_PLATFORM'
2323

2424
# Renderer platform specifiers.
25-
OSMESA = 'osmesa'
26-
GLFW = 'glfw'
27-
EGL = 'egl'
25+
# All values in each tuple are synonyms for the MUJOCO_GL environment variable.
26+
# The first entry in each tuple is considered "canonical", and is the one
27+
# assigned to the _render.BACKEND variable.
28+
OSMESA = ('osmesa',)
29+
GLFW = ('glfw', 'on', 'enable', 'enabled', 'true', '1', '')
30+
EGL = ('egl',)
31+
NO_RENDERER = ('off', 'disable', 'disabled', 'false', '0')
32+

dm_control/_render/glfw_renderer_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
@unittest.skipUnless(
33-
_render.BACKEND == _render.constants.GLFW,
33+
_render.BACKEND == _render.constants.GLFW[0],
3434
reason='GLFW beckend not selected.')
3535
class GLFWContextTest(absltest.TestCase):
3636

dm_control/_render/pyopengl/egl_renderer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
PYOPENGL_PLATFORM = os.environ.get(constants.PYOPENGL_PLATFORM)
2727

2828
if not PYOPENGL_PLATFORM:
29-
os.environ[constants.PYOPENGL_PLATFORM] = constants.EGL
30-
elif PYOPENGL_PLATFORM != constants.EGL:
29+
os.environ[constants.PYOPENGL_PLATFORM] = constants.EGL[0]
30+
elif PYOPENGL_PLATFORM != constants.EGL[0]:
3131
raise ImportError(
3232
'Cannot use EGL rendering platform. '
3333
'The PYOPENGL_PLATFORM environment variable is set to {!r} '
3434
'(should be either unset or {!r}).'
35-
.format(PYOPENGL_PLATFORM, constants.EGL))
35+
.format(PYOPENGL_PLATFORM, constants.EGL[0]))
3636

3737

3838
# pylint: disable=g-import-not-at-top
@@ -100,7 +100,7 @@ def __init__(self, max_width, max_height):
100100

101101
def _platform_init(self, unused_max_width, unused_max_height):
102102
"""Initialization this EGL context."""
103-
num_configs = ctypes.c_long()
103+
num_configs = ctypes.c_long(0)
104104
config_size = 1
105105
config = EGL.EGLConfig()
106106
EGL.eglReleaseThread()

dm_control/_render/pyopengl/osmesa_renderer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
PYOPENGL_PLATFORM = os.environ.get(constants.PYOPENGL_PLATFORM)
2424

2525
if not PYOPENGL_PLATFORM:
26-
os.environ[constants.PYOPENGL_PLATFORM] = constants.OSMESA
27-
elif PYOPENGL_PLATFORM != constants.OSMESA:
26+
os.environ[constants.PYOPENGL_PLATFORM] = constants.OSMESA[0]
27+
elif PYOPENGL_PLATFORM != constants.OSMESA[0]:
2828
raise ImportError(
2929
'Cannot use OSMesa rendering platform. '
3030
'The PYOPENGL_PLATFORM environment variable is set to {!r} '
3131
'(should be either unset or {!r}).'
32-
.format(PYOPENGL_PLATFORM, constants.OSMESA))
32+
.format(PYOPENGL_PLATFORM, constants.OSMESA[0]))
3333

3434
# pylint: disable=g-import-not-at-top
3535
from OpenGL import GL

0 commit comments

Comments
 (0)