-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy path__init__.py
More file actions
109 lines (87 loc) · 3.34 KB
/
__init__.py
File metadata and controls
109 lines (87 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright 2017-2018 The dm_control Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""OpenGL context management for rendering MuJoCo scenes.
By default, the `Renderer` class will try to load one of the following rendering
APIs, in descending order of priority: GLFW > EGL > OSMesa.
It is also possible to select a specific backend by setting the `MUJOCO_GL=`
environment variable to 'glfw', 'egl', or 'osmesa'.
"""
import collections
import os
from absl import logging
from dm_control._render import constants
BACKEND = os.environ.get(constants.MUJOCO_GL)
# pylint: disable=g-import-not-at-top
def _import_egl():
from dm_control._render.pyopengl.egl_renderer import EGLContext
return EGLContext
def _import_glfw():
from dm_control._render.glfw_renderer import GLFWContext
return GLFWContext
def _import_osmesa():
from dm_control._render.pyopengl.osmesa_renderer import OSMesaContext
return OSMesaContext
# Import removed.
# pylint: enable=g-import-not-at-top
def _no_renderer():
def no_renderer(*args, **kwargs):
del args, kwargs
raise RuntimeError('No OpenGL rendering backend is available.')
return no_renderer
_ALL_RENDERERS = (
(constants.GLFW, _import_glfw),
(constants.EGL, _import_egl),
(constants.OSMESA, _import_osmesa),
# Option removed.
)
_NO_RENDERER = (
(constants.NO_RENDERER, _no_renderer),
)
if BACKEND is not None:
# If a backend was specified, try importing it and error if unsuccessful.
import_func = None
for names, importer in _ALL_RENDERERS + _NO_RENDERER:
if BACKEND in names:
import_func = importer
BACKEND = names[0] # canonicalize the renderer name
break
if import_func is None:
all_names = set()
for names, _ in _ALL_RENDERERS + _NO_RENDERER:
all_names.update(names)
raise RuntimeError(
'Environment variable {} must be one of {!r}: got {!r}.'
.format(constants.MUJOCO_GL, sorted(all_names), BACKEND))
logging.info('MUJOCO_GL=%s, attempting to import specified OpenGL backend.',
BACKEND)
Renderer = import_func()
else:
logging.info('MUJOCO_GL is not set, so an OpenGL backend will be chosen '
'automatically.')
# Otherwise try importing them in descending order of priority until
# successful.
for names, import_func in _ALL_RENDERERS:
try:
Renderer = import_func()
BACKEND = names[0]
logging.info('Successfully imported OpenGL backend: %s', names[0])
break
except ImportError:
logging.info('Failed to import OpenGL backend: %s', names[0])
if BACKEND is None:
logging.info('No OpenGL backend could be imported. Attempting to create a '
'rendering context will result in a RuntimeError.')
Renderer = _no_renderer()
USING_GPU = BACKEND in constants.EGL + constants.GLFW