﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	blockedby	blocking	notify_on_close	platform	project
4130	Enable offscreen rendering on macOS	Tom Goddard	Tom Goddard	"Provide OSMesa in macOS ChimeraX builds for offscreen rendering with Python scripts.

Currently we provide the OSMesa (Offscreen Mesa) library in the Linux ChimeraX builds to allow rendering images without a window or on a headless machine without a windowing system.   We compile mesa and llvm which is used by the gallium osmesa implementation I think because OSMesa is not available already compiled.

On macOS the only compiled OSMesa I could find online comes with XQuartz 2.7.11, this is the latest XQuartz from 2016.  I tried that osmesa in ChimeraX and it could only provide OpenGL 2.1 and only compatibility contexts, so it is not usable since ChimeraX needs 3.3 and a core context.

I tried compiling mesa, the same version we use on Linux, version 19.0.8 from June 2019 and it failed on a missing system header declaration of timespec_get, supposedly a problem caused by using C99 instead of C11 std.

I then tried the current mesa version 20.3.2.  This version uses a different build system called meson.  That worked but gave the same error, but was easy to fix with a build option.

cd /mesa/src/dir
mkdir build
meson build/ -Dc_std=c11 -Dosmesa=gallium
ninja -C build/
sudo ninja -C build/ install

The resulting libOSMesa.8.dylib rendered molecule images in ChimeraX.  That required editing PyOpenGL

OpenGL/platform/ctypesloader.py

to recognized the Darwin platform and hardcoding the path in loadLibrary()

    elif sys.platform == 'darwin' and name == 'OSMesa':
        return dllType('/usr/local/lib/libOSMesa.8.dylib', mode)

I used llvm from homebrew to compile (brew install llvm), which is here /usr/local/opt/llvm/bin/llvm-config.

The compiled libOSMesa.8.dylib unfortunately depends on other non-system libraries

otool -L /usr/local/lib/libOSMesa.8.dylib 
/usr/local/lib/libOSMesa.8.dylib:
	/usr/local/lib/libOSMesa.8.dylib (compatibility version 8.0.0, current version 8.0.0)
	/usr/local/lib/libglapi.0.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/local/opt/llvm/lib/libLLVM-C.dylib (compatibility version 1.0.0, current version 11.0.0)
	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
	/usr/local/opt/zstd/lib/libzstd.1.dylib (compatibility version 1.0.0, current version 1.4.5)
	/usr/local/opt/llvm/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)

I believe on Linux our compiled version was statically linked to llvm and has no non-system dependencies like zstd here.  Probably need some more build options to achieve that.  The libglapi.0.dylib dependency was built by meson.  Would prefer that is also statically linked.  The mesa 19.0.8 build using configure / make lists the needed options I think (--disable-llvm-shared-libraries, --disable-shared-glapi)"	enhancement	closed	moderate		Graphics		fixed						all	ChimeraX
