How to programmatically determine if mesa dlls are needed to display a scene?

Hi,

I can display a VTK scene both on a Windows Remote machine through RDP (with no GPU), and on a local Windows machine. Through RDP, I managed to do this by loading GitHub - pal1000/mesa-dist-win: Pre-built Mesa3D drivers for Windows dlls. If I do not load these dlls, I get the message:

2025-07-24 22:50:48.412 ( 76.582s) [84168C5CCF6F0470]vtkWin32OpenGLRenderWin:661 ERR| vtkWin32OpenGLRenderWindow (000001ACAFEECD30): failed to get wglChoosePixelFormatARB
ERROR:root:failed to get wglChoosePixelFormatARB
2025-07-24 22:50:48.429 ( 76.598s) [84168C5CCF6F0470]vtkWin32OpenGLRenderWin:736 ERR| vtkWin32OpenGLRenderWindow (000001ACAFEECD30): failed to get valid pixel format.
ERROR:root:failed to get valid pixel format.
2025-07-24 22:50:48.443 ( 76.613s) [84168C5CCF6F0470]vtkOpenGLRenderWindow.c:804 WARN| vtkWin32OpenGLRenderWindow (000001ACAFEECD30): Failed to initialize OpenGL functions!
2025-07-24 22:50:48.450 ( 76.619s) [84168C5CCF6F0470]vtkOpenGLRenderWindow.c:819 WARN| vtkWin32OpenGLRenderWindow (000001ACAFEECD30): Unable to find a valid OpenGL 3.2 or later implementation. Please update your video card driver to the latest version. If you are using Mesa please make sure you have version 11.2 or later and make sure your driver in Mesa supports OpenGL 3.2 such as llvmpipe or openswr. If you are on windows and using Microsoft remote desktop note that it only supports OpenGL 3.2 with nvidia quadro cards. You can use other remoting software such as nomachine to avoid this issue.
2025-07-24 22:50:48.474 ( 76.643s) [84168C5CCF6F0470]vtkOSOpenGLRenderWindow:137 WARN| osmesa.dll not found. It appears that OSMesa is not installed in your system. Please install the OSMesa library. You can obtain pre-built binaries for Windows from GitHub - pal1000/mesa-dist-win: Pre-built Mesa3D drivers for Windows. Ensure that osmesa.dll is available in PATH.

Now, when run on a local Windows machine, or a remote machine which has a GPU, I don’t want to always load these dlls because they greatly decrease the performance.

I thought I could programmatically determine whether the dll are needed or not (I’m in Python) using:

from vtkmodules.vtkRenderingCore import vtkRenderWindow
vtkRenderWindow().SupportsOpenGL()

But this always returns 0, locally and remotely through RDP until something happens regarding OpenGL, like for example after:

import vtk

In this case, the behavior will then differ locally and through RDP. Locally vtkRenderWindow().SupportsOpenGL() returns 1. Through RDP, and when mesa dll are not loaded, the error message above is displayed.

So the question is, how to programmatically determine if mesa dlls are needed to display a scene?

Thanks for your help.

Due to VTK’s object factory mechanism, it’s necessary to import vtkRenderingOpenGL2 before creating the window. Otherwise, the window can’t do anything useful.

from vtkmodules.vtkRenderingCore import vtkRenderWindow
# load OpenGL implementations of core rendering classes 
import vtkmodules.vtkRenderingOpenGL2

win = vtkRenderWindow()
hasOpenGL = win.SupportsOpenGL()
print(win.GetOpenGLSupportMessage())

In other words, using SupportsOpenGL() before loading the vtkRenderingOpenGL2 DLL will not provide the information that you need.

I’m fairly certain that I’ve heard of other people conditionally loading the mesa DLLs, but I can’t remember what mechanism they used and my searches aren’t turning up anything useful.

Just a brief follow-up. It seems that, in order to avoid getting an error when you instantiate the window, it is necessary to instantiate the vtkWin32OpenGLRenderWindow directly:

from vtkmodules.vtkRenderingOpenGL2 import vtkWin32OpenGLRenderWindow

win = vtkWin32OpenGLRenderWindow()
hasOpenGL = win.SupportsOpenGL()

Otherwise, the object factory mechanism that I mentioned earlier will raise errors while trying to decide what kind of window to instantiate.

Thanks David. Indeed I observe that with:

import vtkmodules.vtkRenderingOpenGL2
win = vtkRenderWindow()
hasOpenGL = win.SupportsOpenGL()

I get an error, and Python exits.

With:

from vtkmodules.vtkRenderingOpenGL2 import vtkWin32OpenGLRenderWindow
win = vtkWin32OpenGLRenderWindow()
hasOpenGL = win.SupportsOpenGL()

I don’t have the error anymore. Through RDP with no GPU, hasOpenGL is equal to 0.

BUT, if then I try to load my dll and call again:

hasOpenGL = win.SupportsOpenGL()

hasOpenGL is still equal to 0 and I can’t open my scene.

It looks like either importing or instantiating vtkWin32OpenGLRenderWindow prevents from further loading mesa dll.

Does it tell you something?

If the program can spawn a separate process for the OpenGL check, e.g. with multiprocessing.Process, then the original process should still be able to load the mesa DLL before importing vtkRenderingOpenGL. Don’t ask me for an example, though, because I’ve never attempted to do this.