vtk9 rc1: OSMESA support doesn't compile

Hi,
I try to compile vtk 9 rc1 with osmesa support. It seems that although I set the include and library path, those variables are not used by CMake in the build process. I configure as follows:

OSMESA_INCLUDE_DIR               /home/angainor/mesa/install/include
OSMESA_LIBRARY                   /home/angainor/mesa/install/lib/x86_64-linux-gnu/libOSMesa.so
VTK_OPENGL_HAS_OSMESA = ON

When I compile, very early on I get the following error:

vtk/vtk-release/ThirdParty/glew/vtkglew/src/glew.c:41:12: fatal error: GL/osmesa.h: No such file or directory
   41 | #  include <GL/osmesa.h>
      |            ^~~~~~~~~~~~~

When I run make VERBOSE=1 I do not see the configured OSMESA include dir in the command line. I tried to explicitly add -I... to CXX and C flags. That works, but then I experience linker errors and unresolved symbols.

The same works with vtk 8.2. Is this a problem with v9?

Thanks!

Can you see what the VTK::opengl target looks like either by tracing the execution trhough Utilities/OpenGL/CMakeLists.txt or checking on what the VTK::opengl and VTK::glew target properties look like in the lib/cmake/vtk-9.0/VTK-targets.cmake file in the build tree?

Thanks, @ben.boeckel. In Utilities/OpenGL/CMakeLists.txt I see this:

if (VTK_OPENGL_HAS_OSMESA AND NOT VTK_CAN_DO_ONSCREEN)
  vtk_module_third_party_external(
    PACKAGE OSMesa
    TARGETS OSMesa::OSMesa)
else ()
...

I don’t know what VTK_CAN_DO_ONSCREEN exactly means, but I guess it could be that osmesa is not used if onscreen rendering is available? There is no other mention of OSMesa in this file.

In VTK-targets.cmake I see this:

# Create imported target VTK::opengl
add_library(VTK::opengl INTERFACE IMPORTED)
...
set_target_properties(VTK::opengl PROPERTIES
  INTERFACE_LINK_LIBRARIES "OpenGL::OpenGL;OpenGL::GLX"
)
...
set_target_properties(VTK::glew PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include/vtk-9.0;${_IMPORT_PREFIX}/include/vtk-9.0/"
  INTERFACE_LINK_LIBRARIES "VTK::opengl"
  INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "include/vtk-9.0/"
)

No sign of OSMesa.

OK, the logic in CMake/vtkOpenGLOptions.cmake is going bad somewhere. If you trace it through, does anything not make sense in your setup? (FWIW, ParaView has OSMesa binaries that are working, but it probably is doing less than what your build is trying.)

Could you have a brief look at my custom config values? I essentially only add Java bindings, the rest is autodetected:

set(CMAKE_INSTALL_PREFIX "/home/angainor/vtk/VTK-9.0.0.rc1/install" CACHE PATH "")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "")
set(BUILD_TESTING "OFF" CACHE BOOL "")
set(VTK_RENDERING_BACKEND "OpenGL2" CACHE STRING "")
set(VTK_SMP_IMPLEMENTATION_TYPE "OpenMP" CACHE STRING "")
set(VTK_LEGACY_REMOVE "ON" CACHE BOOL "")
set(VTK_USE_LARGE_DATA "OFF" CACHE BOOL "")
set(VTK_USE_64BIT_IDS "OFF" CACHE BOOL "")
set(VTK_WRAP_JAVA "ON" CACHE BOOL "")
set(VTK_JAVA_INSTALL "ON" CACHE BOOL "")
set(JAVA_AWT_INCLUDE_PATH "/home/angainor/jdk1.8.0_77/include/linux"  CACHE PATH "")
set(JAVA_AWT_LIBRARY "/home/angainor/jdk1.8.0_77/jre/lib/amd64/libjawt.so" CACHE FILEPATH "")
set(JAVA_INCLUDE_PATH "/home/angainor/jdk1.8.0_77/include" CACHE PATH "")
set(JAVA_INCLUDE_PATH2 "/home/angainor/jdk1.8.0_77/include/linux" CACHE PATH "")
set(JAVA_JVM_LIBRARY "/home/angainor/jdk1.8.0_77/jre/lib/amd64/server/libjvm.so" CACHE FILEPATH "")
set(Java_JAR_EXECUTABLE "/home/angainor/jdk1.8.0_77/bin/jar" CACHE FILEPATH "")
set(Java_JAVAC_EXECUTABLE "/home/angainor/jdk1.8.0_77/bin/javac" CACHE FILEPATH "")
set(Java_JAVADOC_EXECUTABLE "/home/angainor/jdk1.8.0_77/bin/javadoc" CACHE FILEPATH "")
set(Java_JAVAH_EXECUTABLE "/home/angainor/jdk1.8.0_77/bin/javah" CACHE FILEPATH "")
set(Java_JAVA_EXECUTABLE "/home/angainor/jdk1.8.0_77/bin/java" CACHE FILEPATH "")
set(OSMESA_INCLUDE_DIR "/home/angainor/mesa/install/include" CACHE PATH "")
set(OSMESA_LIBRARY "/home/angainor/mesa/install/lib/x86_64-linux-gnu/libOSMesa.so" CACHE FILEPATH "")
set(VTK_OPENGL_HAS_OSMESA "ON" CACHE BOOL "")

Otherwise, build/CMakeCache.txt says

VTK_USE_X:BOOL=ON
VTK_OPENGL_HAS_EGL:BOOL=OFF
VTK_OPENGL_HAS_OSMESA:BOOL=ON
VTK_DEFAULT_RENDER_WINDOW_OFFSCREEN:BOOL=OFF
VTK_DEFAULT_RENDER_WINDOW_HEADLESS:BOOL=OFF

So I guess the logic is that I should have the following results:

VTK_CAN_DO_OFFSCREEN TRUE
VTK_CAN_DO_ONSCREEN TRUE
VTK_CAN_DO_HEADLESS TRUE

I don’t see anything unreasonable with those values. Looks all OK.

In the meantime I tried a vanilla configure without any of my options. It seems that I can only set VTK_OPENGL_HAS_OSMESA, but the other two options I used from 8.2 are not available in ccmake advanced mode (OSMESA_INCLUDE_DIR and OSMESA_LIBRARY). I only find OPENGL_xmesa_INCLUDE_DIR OPENGL_xmesa_INCLUDE_DIR-NOTFOUND.

Am I using some deprecated variables here? Maybe I should provide the mesa installation location differently?

The VTK_CAN_DO_ONSCREEN option masks even asking for OSMesa. Something is making that variable not be what is needed. VTK_USE_X looks to be it.

@ken-martin We probably need to look at the vtkOpenGLOptions.cmake logic again. I probably mixed something up.

Ah, set VTK_USE_X=OFF. You can’t use X and OSMesa at the same time (since OSMesa apparently turns off X support) and asking for X takes precedence.

I’ll try that, thanks! but will I then be able to use the same compilation for both headless and on-screen visualization? or do I have to compile a separate installation for off-screen rendering?

Indeed, turning off VTK_USE_X gave me the option to set OSMESA paths. so I guess this is it.

Currently, on or off screen rendering (with OSMesa at least) is a compile-time option; each VTK only supports one or the other.

OK, thanks a lot, all clear!

Could you please share your complete cmake setup that allowed you to get this working?