moderngl: Error running moderngl in Colab.

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 39 (20 by maintainers)

Most upvoted comments

You still need to run xvfb

Was there anything else do discuss here? I would suggest closing this and instead make new issues if needed to make things a bit more tidy. I now know a whole deal more about the manim project as well.

It’s very good news that the egl example runs!

This is just simply triangle geometry with 2d positions and a color

vertices = np.array([
    # x, y, r, g, b (3 vertices creating a triangle)
    -1.0,  -1.0,   1.0, 0.0, 0.0,
     1.0,  -1.0,   0.0, 1.0, 0.0,
     0.0,   1.0,   0.0, 0.0, 1.0],
    dtype='f4',  # Ensure 32 bit float
)

A GLSL shader rendering the triangle

prog = ctx.program(vertex_shader="""
#version 330
in vec2 in_vert;
in vec3 in_color;
out vec3 color;
void main() {
    gl_Position = vec4(in_vert, 0.0, 1.0);
    color = in_color;
}
""",
fragment_shader="""
#version 330
out vec4 fragColor;
in vec3 color;
void main() {
    fragColor = vec4(color, 1.0);
}
""",
)

Create a vertex array object that describes to opengl what the buffer contains. It will figure out how to pass this data looking at the in attributes in the shader.

vao = ctx.simple_vertex_array(prog, ctx.buffer(vertices), 'in_vert', 'in_color')

We render the triangle to the screen. The shader will iterate over the 3 vertices and pass them to the fragment shader, then the fragment shader will fill the triangle. The in value will interpolate between the values emitted from the vertex shader making a smooth color transition between the corners.

vao.render(mode=moderngl.TRIANGLES)

EDIT: The Vertex Array being tied to a glsl program is special for moderngl. It’s simply because the VAO and shader/program have complex interactions and we do a lot of inner magic to make this painless. It’s a lot of work doing this manually for a user.

I am curious what error message you eventually get running Manim. I might have a couple of tricks to resolve the remaining issues if given more info.

Awesome. I actually learned a lot from this so far regarding EGL and displays. Kind of hard to understand what’s going on when I don’t know much about the server configuration.

If the simple egl example i linked actually works I’m sure the remaining issues can be solved without too much pain. (https://github.com/moderngl/moderngl/blob/master/examples/headless_egl.py)

Not sure if I read the last update as “it’s working” or not.

It starts rendering as usual. tqdm is growing very fast as it should on GPU. It doesn’t ask to run xvfb or any other virtual display. Yes, it eventually returns an error. But it’s a great step forward.

There is a headless issue in pyglet : https://github.com/pyglet/pyglet/issues/51

Other than that I don’t really have an answer. I’m guessing improvements could be done in glcontex as well : https://github.com/moderngl/glcontext. I haven’t had much time to play with it. You can even write your own backends with C++ or ctypes if you want to experiment or tweak the existing ones.