PyOpenGL headless rendering
Asked Answered
B

3

6

I'm using PyOpenGL+glfw for rendering.

When trying to do the same on a headless machine (e.g a server) glfw.init() fails:

glfw.GLFWError: (65544) b'X11: The DISPLAY environment variable is missing'
Fatal Python error: Couldn't create autoTLSkey mapping
Aborted (core dumped)

I found some information about headless rendering, but only when using OpenGL directly and not through python

EDIT: I understand that maybe glfw isn't able to support it. A solution without glfw, but with something else might also work...

Bucovina answered 1/2, 2019 at 16:59 Comment(0)
B
5

The solution is to use xvfb for a virtual framebuffer.

The problem is that the glfw that is installed in Ubuntu using apt-get install libglfw3 libglfw3-dev is old and unfit, so we need to compile it from source.

Here is a full working docker example:

docker run --name headless_test -ti ubuntu /bin/bash

# Inside the ubuntu shell:
apt update && apt install -y python3 python3-pip git python-opengl xvfb xorg-dev cmake
pip3 install pyopengl glfw
mkdir /projects
git clone https://github.com/glfw/glfw.git /projects/glfw
cd /projects/glfw
cmake -DBUILD_SHARED_LIBS=ON .
make
export PYGLFW_LIBRARY=/projects/glfw/src/libglfw.so
xvfb-run python3 some_script_using_pyopengl_and_glfw.py

And here is the base of PyOpenGL code to use it:

from OpenGL.GL import *
from OpenGL.GLU import *
import glfw

glfw.init()
# Set window hint NOT visible
glfw.window_hint(glfw.VISIBLE, False)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(DISPLAY_WIDTH, DISPLAY_HEIGHT, "hidden window", None, None)
# Make the window's context current
glfw.make_context_current(window)
Bucovina answered 30/3, 2019 at 7:37 Comment(0)
F
3

GLFW does not support headless OpenGL at all.

https://www.glfw.org/docs/latest/context.html#context_offscreen

GLFW doesn't support creating contexts without an associated window.

This isn’t an unusual limitation, the problem is that the normal way to create an OpenGL context is by using the X server. There are now alternatives using EGL, which is relatively new. You will need to use an EGL wrapper for Python.

See: OpenGL without X.org in linux

Fireworm answered 1/2, 2019 at 17:16 Comment(0)
E
0

If you want to use OpenGL without a display environment on linux(eg. x server) the best approach is to use EGL. what EGL does is it separate OpenGL context management from windowing system, so it let you create context without a displaying window.

If you are using Nvidia graphics card, you have to install proprietary driver in order to use it. along with the driver there is a library called GLVND this is a library which include EGL your app need to linked against.

Please refer to following links to learn how to use EGL:

Pro Tip: Linking OpenGL for Server-Side Rendering

EGL Eye: OpenGL Visualization without an X Serve

PS. If your EGL api cannot find any devices, you probably linked wrong EGL library, the EGL library must match the driver.

Extemporize answered 19/4, 2019 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.