MATLAB starts with warning after Psychtoolbox installation

I am setting up a new machine (DELL XPS 9320) running Ubuntu 22.04.1 LTS. I first installed MATLAB with no problems and it works nicely, then installed Psychtoolbox following the instructions on the install page (Psychtoolbox-3 - Download, Installation, and Update). I made sure to do the following as suggested:

“3. On a Debian or Ubuntu based system, quit Matlab, and install the matlab-support package by typing sudo apt install matlab-support into a terminal window. When it asks you for the location of your Matlab installation, tell it. When it asks you if it should rename some Matlab libraries to fix Matlab compatibility issues, answer YES, otherwise hardware accelerated graphics will likely not work later on!”

Once this was done, I now get the following whenever I start MATLAB:

com.jogamp.opengl.GLException: X11GLXDrawableFactory - Could not initialize shared resources for X11GraphicsDevice[type .x11, connection :0, unitID 0, handle 0x0, owner false, ResourceToolkitLock[obj 0x52c01ea9, isOwner false, <534c3471, 6f944a82>[count 0, qsz 0, owner <NULL>]]]
	at jogamp.opengl.x11.glx.X11GLXDrawableFactory$SharedResourceImplementation.createSharedResource(X11GLXDrawableFactory.java:326)
	at jogamp.opengl.SharedResourceRunner.run(SharedResourceRunner.java:297)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
	at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:688)
	at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:580)
	at jogamp.opengl.x11.glx.X11GLXDrawableFactory$SharedResourceImplementation.createSharedResource(X11GLXDrawableFactory.java:297)
	... 2 more

Is this a natural consequence of the renaming that has no bearing on performance or does it suggest an issue that can cause problems later on?

Background:

This is a bug in the Matlab OpenGL hardware accelerated graphics and plotting system of all Matlab versions on Linux. I reported the problem to our Mathworks contact in the past, but the problem wasn’t resolved so far for more than a year. The bug triggers specifically on Intel graphics chips (and the just released Intel “Arc” discrete graphics cards) with the modern Intel iris graphics driver, e.g., as used on Ubuntu 20.04-LTS and later and similar modern distributions.

The bug does not affect Psychtoolbox operation itself, so visual stimulation works just fine. But it completely breaks any Matlab plotting a la figure() plot() etc.

The reason Matlab may have appeared to work before installing PTB and the matlab-support package is due to another serious deficiency of Matlab, unfixed since literally forever, affecting PTB on different operating systems differently on and off, but on Linux the typical effect is to disable all hardware accelerated rendering, so Matlab then uses its own backup software renderer → Plotting works (much more slowly for complex plots), PTB and other OpenGL software is broken. The matlab-support package for Linux fixes that Matlab bug, and thereby PTB stimulus presentation, but now exposes this other Intel graphics related Matlab bug, hurray!

Past solution, which won’t work much longer anymore:

So far, as of R2022a, the “solution” was to either:

  • Ignore the error message if you don’t need Matlab plotting, just a working Psychtoolbox.
  • Or if you also need plotting, you could select the old Matlab ‘painters’ rendering backend, which doesn’t use OpenGL, via the following command, which still works as of R2022b:
    set(groot, 'defaultFigureRenderer', 'painters');
    Downside is relatively slow plotting and other limitations.

However, release notes of R2022a state that this helpful workaround will no longer work on R2022b and later versions, ie. the “Renderer” figure property will have no effect anymore. Great work of the Mathworks graphics team, leaving bugs unfixed, but also removing the ability to work around the problem in more recent releases! That said, testing with the new R2022b just showed that the workaround still works as of R2022b!

Another solution would have been to force use of the old Intel i965 OpenGL graphics driver, something that works with Intel gpu’s up to Gen 9.x “Skylake, Kabylake”.

However, this is no longer an option for users of modern Laptops or PC with Intel Gen 11 graphics or later, e.g., Gen 11 “Icelake”, Gen 12 “Tigerlake”, or Gen 13 “Alderlake” and Intel “Arc” discrete graphics cards. Your Dell machine happens to have an Alderlake processor and graphics, so with Matlab R2022b and later you’d have been out of luck wrt. plotting.

It’s your lucky Mathworks sponsored day:

Given that this problem will obviously affect more people with modern Laptops in the future, I decided to select this issue for “free support on resolving a non-trivial issue”, sponsored by Mathworks Neuroscience - MATLAB and Simulink Solutions - MATLAB & Simulink - You get my free advice here, based on over 21 hours of diagnostic work throughout the past two days from myself for free, which would normally have cost your lab over 6000 Euros. Free for you, because Mathworks pays the bill for this one. Mathworks provided us with 3 such “Jokers” for free support during the contract period 2022/2023, and you get to use the third out of three.

Diagnosis of the root cause and technical background:

So the outcome of those 21 hours of debugging is the following:

Matlab uses Java + OpenGL for its plotting, and the bug is in the JOGL toolkit for interfacing Java with OpenGL, which is contained in version 2.3.1 in current Matlab R2022b.

Specifically, on any driver who reports the word “Mesa” in the OpenGL renderer name string, e.g., “Mesa Intel(R) UHD graphics 620…” and exposes an OpenGL compatibility context of OpenGL version of 3.1 or later, JOGL triggers a workaround for past potential bugs in Mesa which don’t exist anymore since over 4 years, but then that workaround itself triggers a cascade of more JOGL bugs with the result you observed.

The bug has been apparently fixed in the JOGL development branch, for a future v2.4.0 release with unknown ETA, but at the moment the only clean solution would be for Mathworks to fund a new JOGL release - because JOGL is open-source software developed by volunteers - and integrate it into an upcoming Matlab bugfix release. Or roll their own private bug-fix release. For reference, these are relevant bugtracker entries and commits which supposedly fix the problem (I can’t verify the fixes, because recent Matlab releases create fun new obstacles for testing - something that was easier a few years ago):

https://jogamp.org/bugzilla/show_bug.cgi?id=1383
https://jogamp.org/bugzilla/show_bug.cgi?id=1385

Commit SHA’s 70f360d1a58405502a5a2a0f95a2f2abda94d488 and preceeding commits, already present since August 2019:

https://jogamp.org/cgit/jogl.git/commit/?id=70f360d1a58405502a5a2a0f95a2f2abda94d488

I guess lacking financial sponsoring, the JOGL developers are not too much in a hurry to release a new version of JOGL, but they can be contracted for such work.

A workaround that currently works:

So the best I could come up with after 21 hours of work is the following workaround:

Launch Matlab from a terminal window, with the environment variable MESA_GL_VERSION_OVERRIDE set to 3.0, e.g. like this:

MESA_GL_VERSION_OVERRIDE=3.0 matlab

Or generally to the Matlab startup shell script in, e.g., “/usr/local/MATLAB/R2022a/bin/matlab.sh” for a typical R2022a location.

Or for R2022b, to the file “/usr/local/MATLAB/R2022b/bin/matlab” for a typical R2022b location. Note the file no longer has a .sh extension.

What works is adding this line:
export MESA_GL_VERSION_OVERRIDE=3.0
at the start or after the end of the section “# Export the variables”.

What this does is override the OpenGL version reported by the Mesa graphics driver to always report OpenGL 3.0. This way the trigger condition for the bug of an OpenGL version >= 3.1 is no longer satisfied and Matlab’s plotting is happy. Psychtoolbox doesn’t care much what the reported OpenGL version is as it will detect and use more advanced OpenGL functionality from OpenGL 3.1 and later in a different way.

Therefore this little workaround should make things work again with current Matlab releases, with no known downsides, definitely none for Psychtoolbox itself.

Update:

What also worked for me on Ubuntu 20.04.6-LTS, Intel graphics, Mesa 21.2.6, with Matlab R2023b is the following older approach at fixing it:

Hope it helps,
-mario

1 Like

Thanks Mario! It helped with the OpenGL issue.
I still have a problem with XOrgConf. Although I can create an config file and select it without any error message on matlab, it doesn’t apply it after logout/login - even after restart. It is a similar issue with this: AMD graphics card - #2 by mariokleiner
I ran sudo dpkg-reconfigure matlab-support on terminal and it changed only GCC library names. uname -r reports 6.5.0-14-generic. There is no PTB output about XOrgConf. Do you have further suggestions?