+-
如何在Colab中运行moderngl?

我正在尝试在Colab中运行moderngl。我安装了它并运行了虚拟显示器:

!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!'

它按预期打印,但是当我运行示例时,看到错误:

!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

我也尝试过使用另一个虚拟显示器,但是结果是相同的:

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

pyglet.window.NoSuchConfigException
0
投票

在Google Colab中,您可以将EGL后端与moderngl 5.6一起使用。

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

输出(部分):

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

moderngl默认情况下会创建OpenGL 3.3核心上下文。如果需要更高的上下文版本,则可以传入OpenGL 4.3的require=430。我不知道这些特斯拉卡支持什么。

为此,在moderngl中有一个标准示例。它将能够创建标准的RGB三角形:https://github.com/moderngl/moderngl/blob/master/examples/headless_egl.py

创建上下文的基础库是glcontext(https://github.com/moderngl/glcontext)。

如果使用moderngl-window软件包,则必须使用headless.Window,因为pyglet当前无法在无头模式下工作(将来可能:https://github.com/pyglet/pyglet/issues/51)