Skip to content

Instantly share code, notes, and snippets.

@larsoner
Last active February 10, 2022 18:31
Show Gist options
  • Save larsoner/05f13182d26b70ceb6c2df739cbb3edd to your computer and use it in GitHub Desktop.
Save larsoner/05f13182d26b70ceb6c2df739cbb3edd to your computer and use it in GitHub Desktop.
Linux OpenGL test
from ctypes import CDLL, c_int, byref
import faulthandler
import vtk as _vtk
faulthandler.enable()
renderWindow = _vtk.vtkRenderWindow()
logger = _vtk.vtkLogger
logger.Init()
logger.SetStderrVerbosity(_vtk.vtkLogger.VERBOSITY_MAX)
renderWindow.DebugOn()
renderWindow.SetOffScreenRendering(True)
exited = False
def _obs(obj, event):
print(obj, event)
global exited
exited = True
renderWindow.AddObserver(_vtk.vtkCommand.ExitEvent, _obs)
assert renderWindow.HasObserver(_vtk.vtkCommand.ExitEvent)
# Eventually we should add more special cases here, but let's at least catch
# the linux-with-no-display and linux-with-old-opengl cases
# adapted from https://github.com/Kitware/VTK/blob/master/Rendering/OpenGL2/vtkXOpenGLRenderWindow.cxx
if isinstance(renderWindow, _vtk.vtkXOpenGLRenderWindow):
# "bad X server connection"
xlib = CDLL('libX11.so.6')
assert xlib is not None
display = xlib.XOpenDisplay()
if not display:
raise RuntimeError('Could not initialize display')
# "Could not find a decent config"
glx = CDLL('libGL.so')
assert glx is not None
attributes = [0] * 50
attributes[:15] = [
0x8010, # GLX_DRAWABLE_TYPE
0x00000001, # GLX_WINDOW_BIT
0x8011, # GLX_RENDER_TYPE
0x00000001, # GLX_RGBA_BIT
8, # GLX_RED_SIZE
1,
9, # GLX_GREEN_SIZE
1,
10, # GLX_BLUE_SIZE
1,
12, # GLX_DEPTH_SIZE
1,
11, # GLX_ALPHA_SIZE
1,
0,
]
attributes = (c_int * len(attributes))(*attributes)
tmp = c_int(0)
fb = glx.glXChooseFBConfig(
display, xlib.XDefaultScreen(display), attributes, byref(tmp))
try:
if not fb or tmp.value <= 0:
raise RuntimeError('no decent framebuffer')
finally:
pass
if fb:
xlib.XFree(fb)
xlib.XCloseDisplay(display)
# Cannot create GLX context
renderWindow.Initialize()
if exited:
raise RuntimeError('Could not create GLX context')
print('Usable')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment