This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gl_check.py
301 lines (270 loc) · 12.3 KB
/
gl_check.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python
# This file is part of Xpra.
# Copyright (C) 2012 Serviware (Arthur Huillet, <ahuillet@serviware.com>)
# Copyright (C) 2012, 2013 Antoine Martin <antoine@devloop.org.uk>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.
import sys, os
import logging
from xpra.log import Logger, debug_if_env
log = Logger()
debug = debug_if_env(log, "XPRA_OPENGL_DEBUG")
required_extensions = ["GL_ARB_texture_rectangle", "GL_ARB_vertex_program"]
#warnings seem unavoidable on win32, so silence them
#(other platforms should fix their packages instead)
SILENCE_FORMAT_HANDLER_LOGGER = sys.platform.startswith("win") or sys.platform.startswith("darwin")
BLACKLIST = {"vendor" : ["nouveau", "Humper"]}
DEFAULT_HAS_ALPHA = not sys.platform.startswith("win") and not sys.platform.startswith("darwin")
HAS_ALPHA = os.environ.get("XPRA_ALPHA", DEFAULT_HAS_ALPHA) in (True, "1")
DEFAULT_DOUBLE_BUFFERED = 0
if sys.platform.startswith("win"):
#needed on win32?
DEFAULT_DOUBLE_BUFFERED = 1
DOUBLE_BUFFERED = os.environ.get("XPRA_OPENGL_DOUBLE_BUFFERED", str(DEFAULT_DOUBLE_BUFFERED))=="1"
def get_DISPLAY_MODE():
import gtk.gdkgl
#gtk.gdkgl.MODE_DEPTH
mode = 0
if HAS_ALPHA:
mode = mode | gtk.gdkgl.MODE_RGBA | gtk.gdkgl.MODE_ALPHA
else:
mode = mode | gtk.gdkgl.MODE_RGB
if DOUBLE_BUFFERED:
mode = mode | gtk.gdkgl.MODE_DOUBLE
else:
mode = mode | gtk.gdkgl.MODE_SINGLE
return mode
def get_MODE_names(mode):
import gtk.gdkgl
friendly_mode_names = {gtk.gdkgl.MODE_RGB : "RGB",
gtk.gdkgl.MODE_RGB : "RGBA",
gtk.gdkgl.MODE_ALPHA : "ALPHA",
gtk.gdkgl.MODE_DEPTH : "DEPTH",
gtk.gdkgl.MODE_DOUBLE : "DOUBLE",
gtk.gdkgl.MODE_SINGLE : "SINGLE"}
friendly_modes = [v for k,v in friendly_mode_names.items() if k>0 and (k&mode)==k]
#special case for single (value is zero!)
if not (mode>k.gdkgl.MODE_DOUBLE==gtk.gdkgl.MODE_DOUBLE):
friendly_modes.append("SINGLE")
return friendly_modes
#by default, we raise an ImportError as soon as we find something missing:
def raise_error(msg):
raise ImportError(msg)
gl_check_error = raise_error
if sys.version > '3':
unicode = str #@ReservedAssignment
def check_functions(*functions):
missing = []
available = []
for x in functions:
try:
name = x.__name__
except:
name = str(x)
if not bool(x):
missing.append(name)
else:
available.append(name)
if len(missing)>0:
gl_check_error("some required OpenGL functions are not available: %s" % (", ".join(missing)))
else:
debug("All the required OpenGL functions are available: %s " % (", ".join(available)))
#sanity checks: OpenGL version and fragment program support:
def check_GL_support(gldrawable, glcontext, min_texture_size=0, force_enable=False):
if not gldrawable.gl_begin(glcontext):
raise ImportError("gl_begin failed on %s" % gldrawable)
props = {}
try:
if SILENCE_FORMAT_HANDLER_LOGGER:
debug("silencing formathandler warnings")
logging.getLogger('OpenGL.formathandler').setLevel(logging.WARN)
import OpenGL
props["pyopengl"] = OpenGL.__version__
from OpenGL.GL import GL_VERSION, GL_EXTENSIONS
from OpenGL.GL import glGetString, glGetInteger
gl_version_str = glGetString(GL_VERSION)
if gl_version_str is None:
gl_check_error("OpenGL version is missing - cannot continue")
return {}
gl_major = int(gl_version_str[0])
gl_minor = int(gl_version_str[2])
props["opengl"] = gl_major, gl_minor
MIN_VERSION = (1,1)
if (gl_major, gl_minor) < MIN_VERSION:
gl_check_error("OpenGL output requires version %s or greater, not %s.%s" %
(".".join([str(x) for x in MIN_VERSION]), gl_major, gl_minor))
else:
debug("found valid OpenGL version: %s.%s", gl_major, gl_minor)
try:
extensions = glGetString(GL_EXTENSIONS).split(" ")
except:
gl_check_error("OpenGL could not find the list of GL extensions - does the graphics driver support OpenGL?")
debug("OpenGL extensions found: %s", ", ".join(extensions))
props["extensions"] = extensions
from OpenGL.GL import GL_RENDERER, GL_VENDOR, GL_SHADING_LANGUAGE_VERSION
for d,s,fatal in (("vendor", GL_VENDOR, True),
("renderer", GL_RENDERER, True),
("shading language version", GL_SHADING_LANGUAGE_VERSION, False)):
try:
v = glGetString(s)
debug("%s: %s", d, v)
except:
if fatal:
gl_check_error("OpenGL property '%s' is missing" % d)
else:
log.warn("OpenGL property '%s' is missing" % d)
v = ""
props[d] = v
from OpenGL.GLU import gluGetString, GLU_VERSION, GLU_EXTENSIONS
for d,s in {"GLU version": GLU_VERSION, "GLU extensions":GLU_EXTENSIONS}.items():
v = gluGetString(s)
debug("%s: %s", d, v)
props[d] = v
for k,vlist in BLACKLIST.items():
v = props.get(k)
if v in vlist:
if force_enable:
log.warn("Warning: %s '%s' is blacklisted!", k, v)
else:
gl_check_error("%s '%s' is blacklisted!" % (k, v))
#check for specific functions we need:
from OpenGL.GL import glActiveTexture, glTexSubImage2D, glTexCoord2i, \
glViewport, glMatrixMode, glLoadIdentity, glOrtho, \
glEnableClientState, glGenTextures, glDisable, \
glBindTexture, glPixelStorei, glEnable, glBegin, glFlush, \
glTexParameteri, \
glTexImage2D, \
glMultiTexCoord2i, \
glVertex2i, glEnd
check_functions(glActiveTexture, glTexSubImage2D, glTexCoord2i, \
glViewport, glMatrixMode, glLoadIdentity, glOrtho, \
glEnableClientState, glGenTextures, glDisable, \
glBindTexture, glPixelStorei, glEnable, glBegin, glFlush, \
glTexParameteri, \
glTexImage2D, \
glMultiTexCoord2i, \
glVertex2i, glEnd)
#check for framebuffer functions we need:
from OpenGL.GL.ARB.framebuffer_object import GL_FRAMEBUFFER, \
GL_COLOR_ATTACHMENT0, glGenFramebuffers, glBindFramebuffer, glFramebufferTexture2D
check_functions(GL_FRAMEBUFFER, \
GL_COLOR_ATTACHMENT0, glGenFramebuffers, glBindFramebuffer, glFramebufferTexture2D)
for ext in required_extensions:
if ext not in extensions:
gl_check_error("OpenGL driver lacks support for extension: %s" % ext)
else:
debug("Extension %s is present", ext)
#this allows us to do CSC via OpenGL:
#see http://www.opengl.org/registry/specs/ARB/fragment_program.txt
from OpenGL.GL.ARB.fragment_program import glInitFragmentProgramARB
if not glInitFragmentProgramARB():
gl_check_error("OpenGL output requires glInitFragmentProgramARB")
else:
debug("glInitFragmentProgramARB works")
from OpenGL.GL.ARB.texture_rectangle import glInitTextureRectangleARB
if not glInitTextureRectangleARB():
gl_check_error("OpenGL output requires glInitTextureRectangleARB")
else:
debug("glInitTextureRectangleARB works")
from OpenGL.GL.ARB.vertex_program import glGenProgramsARB, glDeleteProgramsARB, \
glBindProgramARB, glProgramStringARB
check_functions(glGenProgramsARB, glDeleteProgramsARB, glBindProgramARB, glProgramStringARB)
from OpenGL.GL import GL_MAX_RECTANGLE_TEXTURE_SIZE, GL_MAX_TEXTURE_SIZE
texture_size = glGetInteger(GL_MAX_TEXTURE_SIZE)
rect_texture_size = glGetInteger(GL_MAX_RECTANGLE_TEXTURE_SIZE)
if min_texture_size>texture_size or min_texture_size>rect_texture_size:
gl_check_error("The texture size is too small: %s" % texture_size)
else:
debug("Texture size GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB=%s, GL_MAX_TEXTURE_SIZE=%s", rect_texture_size, texture_size)
return props
finally:
if SILENCE_FORMAT_HANDLER_LOGGER:
try:
logging.getLogger('OpenGL.formathandler').setLevel(logging.INFO)
except:
pass
gldrawable.gl_end()
def check_support(min_texture_size=0, force_enable=False):
#tricks to get py2exe to include what we need / load it from its unusual path:
opengl_icon = os.path.join(os.getcwd(), "icons", "opengl.png")
if sys.platform.startswith("win"):
debug("is frozen: %s", hasattr(sys, "frozen"))
if hasattr(sys, "frozen"):
debug("found frozen path: %s", sys.frozen)
if sys.frozen in ("windows_exe", "console_exe"):
main_dir = os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding()))
debug("main_dir=%s", main_dir)
sys.path.insert(0, main_dir)
os.chdir(main_dir)
opengl_icon = os.path.join(main_dir, "icons", "opengl.png")
else:
sys.path.insert(0, ".")
#This is supposed to help py2exe (after we setup the path):
from OpenGL.platform import win32 #@UnusedImport
props = {}
from gtk import gdk
import gtk.gdkgl, gtk.gtkgl
assert gtk.gdkgl is not None and gtk.gtkgl is not None
debug("pygdkglext version=%s", gtk.gdkgl.pygdkglext_version)
props["pygdkglext_version"] = gtk.gdkgl.pygdkglext_version
debug("pygdkglext OpenGL version=%s", gtk.gdkgl.query_version())
props["gdkgl_version"] = gtk.gdkgl.query_version()
display_mode = get_DISPLAY_MODE()
try:
glconfig = gtk.gdkgl.Config(mode=display_mode)
except gtk.gdkgl.NoMatches, e:
debug("no match: %s, toggling double-buffering", e)
display_mode &= ~gtk.gdkgl.MODE_DOUBLE
glconfig = gtk.gdkgl.Config(mode=display_mode)
props["display_mode"] = get_MODE_names(display_mode)
props["glconfig"] = glconfig
props["has_alpha"] = glconfig.has_alpha()
props["rgba"] = glconfig.is_rgba()
debug("GL props=%s", props)
assert gtk.gdkgl.query_extension()
glcontext, gldrawable, glext, w = None, None, None, None
try:
if sys.platform.startswith("win"):
#FIXME: ugly win32 hack for getting a drawable and context, we must use a window...
#maybe using a gl.drawable would work too?
w = gtk.Window()
w.set_decorated(False)
vbox = gtk.VBox()
if opengl_icon and os.path.exists(opengl_icon):
pixbuf = gtk.gdk.pixbuf_new_from_file(opengl_icon)
image = gtk.image_new_from_pixbuf(pixbuf)
vbox.add(image)
w.set_default_size(pixbuf.get_width(), pixbuf.get_height())
w.set_resizable(False)
glarea = gtk.gtkgl.DrawingArea(glconfig)
vbox.add(glarea)
w.add(vbox)
w.show_all()
gtk.gdk.window_process_all_updates()
gldrawable = glarea.get_gl_drawable()
glcontext = glarea.get_gl_context()
else:
glext = gtk.gdkgl.ext(gdk.Pixmap(gdk.get_default_root_window(), 1, 1))
gldrawable = glext.set_gl_capability(glconfig)
glcontext = gtk.gdkgl.Context(gldrawable, direct=True)
gl_props = check_GL_support(gldrawable, glcontext, min_texture_size, force_enable)
finally:
if w:
w.destroy()
del glcontext, gldrawable, glext, glconfig
props.update(gl_props)
return props
def main():
logging.basicConfig(format="%(asctime)s %(message)s")
logging.root.setLevel(logging.DEBUG)
#replace ImportError with a log message:
global gl_check_error
def log_error(msg):
log.error("ERROR: %s", msg)
gl_check_error = log_error
check_support(True)
if sys.platform.startswith("win"):
print("\nPress Enter to close")
sys.stdin.readline()
if __name__ == "__main__":
main()