LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Michael Tetzlaff on January 11, 2024, 23:31:50

Title: LWJGL + JavaFX + MacOS
Post by: Michael Tetzlaff on January 11, 2024, 23:31:50
Hi, I'm trying to get a project running on MacOS that uses LWJGL and JavaFX.  (This project has run successfully on MacOS a number of years ago, but I haven't tested it in several years, and significant development on the project has happened since then.)

I am currently trying this with LWJGL 3.0.0 and JavaFX 17 (version 17.0.2 from org.openjfx in Maven).  I was previously using LWJGL 3.0.0b and JavaFX 11 (11.0.2).

I am using the XstartOnFirstThread VM argument.  I am running LWJGL / GLFW on the main thread, and launching JavaFX in a separate thread.  I only need LWJGL for offscreen rendering; I'm not trying to create a visible window or poll events.

The basic problem is that it seems that between GLFW and JavaFX, whichever one I initialize second hangs forever on initialization. 

For instance, if I try the following, JavaFX launches fine, but glfwInit() hangs and never returns:

        log.info("Starting JavaFX UI");
        new Thread(() -> kintsugi3d.builder.javafx.MainApplication.launchWrapper("")).start();

        try
        {
            Thread.sleep(5000L);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }

        if ( glfwInit() != true )
        {
            throw new GLFWException("Unable to initialize GLFW.");
        }

        // finish initializing GLFW, never gets here


(The sleep for 5 seconds is excessive, but just wanted a quick hack to be relatively certain it wasn't a race condition.)

On the other hand, if I use the following code, glfwInit() returns successfully, but my JavaFX application is never created (presumably hanging somewhere in internal JavaFX code):

        if ( glfwInit() != true )
        {
            throw new GLFWException("Unable to initialize GLFW.");
        }


        log.info("Starting JavaFX UI");
        new Thread(() -> kintsugi3d.builder.javafx.MainApplication.launchWrapper("")).start();

        try
        {
            Thread.sleep(5000L);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }

        // finish initializing GLFW


Any help getting this to work, or even which way is most likely expected to work, would be appreciated.  As I said before, I'm pretty sure I got this to work in the past, but that was before a lot of additional development happened, and probably on different versions of Java/JavaFX/LWJGL/MacOS.
Title: Re: LWJGL + JavaFX + MacOS
Post by: Michael Tetzlaff on January 12, 2024, 18:12:33
I've also now tried updating to LWJGL 3.3.3.  With the latest version of LWJGL, it now seems that if -XstartOnFirstThread is used, JavaFX ALWAYS hangs / fails to launch the window, regardless of initialization order or thread (main vs. non-main).  However, if -XstartOnFirstThread is not used, glfwInit() crashes the program (even when called on the main thread).
Title: Re: LWJGL + JavaFX + MacOS
Post by: spasi on January 12, 2024, 18:40:36
Hey Michael,

Without -XstartOnFirstThread, you must use the async GLFW implementation for macOS. You can do that with Configuration.GLFW_LIBRARY_NAME.set("glfw_async"). This will let you use GLFW from a secondary thread.

I'm not sure how it's going to work with JavaFX though. I've given up on trying to support any kind of integration between JavaFX and GLFW and haven't tested such a configuration. The recommended solution, which doesn't require any interaction with GLFW, is DriftFX: https://github.com/eclipse-efx/efxclipse-drift
Title: Re: LWJGL + JavaFX + MacOS
Post by: Michael Tetzlaff on January 12, 2024, 20:28:55
That works, thanks.

Good to know about DriftFX.  Currently I'm just using framebuffer reads to put the graphics output into the JavaFX window and it works fine, but if I have time to rewrite the graphics backend of my project, it would be nice to have something more integrated.

For anyone else finding this, here's a bit more about what discovered with regards to the relationship between LWJGL, JavaFX, and MacOS: