How to use osmesa backend?

I want to run an application glfw. The case is that the machine does not have display, so I try to use osmesa backend and I am struggling with it.

Here is my simple original code:

int main(int argc, char *argv[])
{
    GLFWwindow* window;
    int width, height;

    printf("%s\n", glfwGetVersionString());
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        exit( EXIT_FAILURE );
    }

    glfwWindowHint(GLFW_DEPTH_BITS, 16);
    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);

    window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL );
    if (!window)
    {
        fprintf( stderr, "Failed to open GLFW window\n" );
        glfwTerminate();
        exit( EXIT_FAILURE );
    }

    // do something
}

It works well, with X11.

Now I am trying to replace it with osmesa, so I add these lines:

    glfwWindowHint(GLFW_DEPTH_BITS, 16);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  
    glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_OSMESA_CONTEXT_API);

When running the program, it crashes with “Failed to initialize GLFW”. And with error_callback there is error: [65543]: OSMesa: Failed to create context.
What is the correct approach to switch the normal mode to osmesa?
Thanks!

I would first check you have OSMesa installed, and also remove all window hints except for glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_OSMESA_CONTEXT_API); as it’s possible the selection of hints you have requested are not compatible with OSMesa.

I have run sudo yum install mesa-libOSMesa-devel and there is /usr/lib/libOSMesa.so. It looks OK after removing all window hints except for glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_OSMESA_CONTEXT_API)! Thanks!
Further step. It currently works with X11 forwarding, etc. with environment variable DISPLAY=xxx.
Finally, I want the program to run on the server without a screen device and X serer, I think there will be no such DISPLAY variable. So I run unset DISPLAY, and run the program. It crushes with message [65550]: Failed to detect any supported platform and Failed to initialize GLFW.
According to my understanding, OSMesa is for the purpose of running off-screen. How do I fix this? In compilation or runtime?
Thanks a lot for helping with my stupid question :slight_smile:

If you are using GLFW on a headless system you will need to use the GLFW_PLATFORM_NULL, see the Runtime platform selection documentation.

I see you’ve also opened an issue on GLFW, so I’ll link it here for reference:

It works! Now my whole program runs successfully and correctly.
I have struggled with it for many days, to find a way to run glfw headless. I have tried things like xvfb, osmesa, egl. I encountered various errors and always wondered if I was using OSMesa correctly. It seems that OSMesa headless is a new feature in recent versions, some usage details like ‘remove hints’ and ‘set GLFW_PLATFORM_NULL’ are missing. Adding some example code or test cases would be user-friendly.:slight_smile:
Thank you very much!