How to run moderngl in Colab?

2.8k Views Asked by At

I'm trying to run moderngl in Colab. I installed it and ran a virtual display:

!sudo apt-get update --fix-missing && apt-get -qqq install x11-utils > /dev/null
!sudo apt-get update --fix-missing && apt-get -qqq install xvfb > /dev/null
!python3 -m pip install -U -qqq moderngl
!python3 -m pip install -U -qqq moderngl-window
!python3 -m pip install -U -qqq pyvirtualdisplay

from pyvirtualdisplay import Display
display = Display(visible=0, size=(960, 540)).start()

import moderngl
ctx = moderngl.create_standalone_context()
buf = ctx.buffer(b'Hello World!')  # allocated on the GPU
buf.read()

b'Hello World!'

It printed as expected, but when I run an example I see the error:

!python3 /content/moderngl/examples/basic_alpha_blending.py --window pyglet

2020-03-28 10:25:48,312 - moderngl_window - INFO - Attempting to load window class: moderngl_window.context.pyglet.Window
Traceback (most recent call last):
  File "/content/moderngl/examples/basic_alpha_blending.py", line 74, in <module>
    AlphaBlending.run()
  File "/content/moderngl/examples/ported/_example.py", line 21, in run
    mglw.run_window_config(cls)
  File "/usr/local/lib/python3.6/dist-packages/moderngl_window/__init__.py", line 185, in run_window_config
    cursor=show_cursor if show_cursor is not None else True,
  File "/usr/local/lib/python3.6/dist-packages/moderngl_window/context/pyglet/window.py", line 54, in __init__
    config=config,
  File "/usr/local/lib/python3.6/dist-packages/pyglet/window/xlib/__init__.py", line 165, in __init__
    super(XlibWindow, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pyglet/window/__init__.py", line 588, in __init__
    config = screen.get_best_config(config)
  File "/usr/local/lib/python3.6/dist-packages/pyglet/canvas/base.py", line 194, in get_best_config
    raise window.NoSuchConfigException()
pyglet.window.NoSuchConfigException

I also tried with another virtual display, but the result is the same:

!python3 -m pip install -U -qqq  xvfbwrapper
from xvfbwrapper import Xvfb
display = Xvfb(width=960, height=540).start()

pyglet.window.NoSuchConfigException
1

There are 1 best solutions below

5
On BEST ANSWER

In Google Colab you can use the EGL backend with moderngl 5.6.

ctx = moderngl.create_context(standalone=True, backend='egl')
print(ctx.info)

Output (partial):

{
    'GL_VENDOR': 'NVIDIA Corporation',
    'GL_RENDERER': 'Tesla P100-PCIE-16GB/PCIe/SSE2',
    'GL_VERSION': '3.3.0 NVIDIA 418.67',
    ....
}

moderngl creates an OpenGL 3.3 core context by default. If you need a higher context version you can pass in require=430 for OpenGL 4.3 for example. I don't know what these Tesla cards support.

There is a standard example in moderngl for this. It would be able to create the standard RGB triangle: https://github.com/moderngl/moderngl/blob/master/examples/headless_egl.py

The underlying library creating the contexts is glcontext (https://github.com/moderngl/glcontext).

if you are using the moderngl-window package you have to use the headless.Window because pyglet currently is not able to work in headless mode (It might in the future: https://github.com/pyglet/pyglet/issues/51)

If you run into issue make an issue in the moderngl project: https://github.com/moderngl/moderngl or invade their discord server.