# OpenGL without X using EGL

**URL:** <https://forums.developer.nvidia.com/t/opengl-without-x-using-egl/49523>\
**Category:** OpenGL\
**Created:** [April 25, 2017, 6:26am UTC](https://forums.developer.nvidia.com/t/opengl-without-x-using-egl/49523 "2017-04-25T06:26:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![monkle](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@monkle](https://forums.developer.nvidia.com/u/monkle)\
**Post date:** [April 25, 2017, 6:26am UTC](https://forums.developer.nvidia.com/t/opengl-without-x-using-egl/49523/1 "2017-04-25T06:26:58Z")

</div>

Hi

Using EGL I implemented an OpenGL off-screen renderer on Linux without X server running.  
I was able to find some OpenGL ES and EGL examples and made it work,  
but for desktop OpenGL I had no luck…

I checked this blog post

> **[EGL Eye: OpenGL Visualization without an X Server | NVIDIA Technical Blog](https://developer.nvidia.com/blog/egl-eye-opengl-visualization-without-x-server/)**
>
> If you’re like me, you have a GPU-accelerated in-situ visualization toolkit that you need to run on the latest-generation supercomputer. Or maybe you have a fantastic OpenGL application that you want…

It showed how to initialize EGL and create an OpenGL context using EGL,  
but it doesn’t show how to load OpenGL functions where I was stuck  
(All EGL initialization and OpenGL context creation looked OK in my renderer) …

I tried GLEW, but it’s strongly tied to GLX/X11. I was not able to build it with EGL.  
Tried other ways to load OpenGL :

> **[GitHub - Dav1dde/glad: Multi-Language Vulkan/GL/GLES/EGL/GLX/WGL...](https://github.com/Dav1dde/glad)**
>
> Multi-Language Vulkan/GL/GLES/EGL/GLX/WGL Loader-Generator based on the official specs.

> **[Loading OpenGL without GLEW](https://apoorvaj.io/loading-opengl-without-glew)**

This time it built OK, and looked like it loaded OpenGL function symbols correctly  
(the function pointers have valid addresses) …  
However, the OpenGL functions failed to run in my renderer.  
Two specific functions I ran were glCreateShader and glCreateProgram.  
They returned 0 (failure) all the time with glError code 0.

Does anyone have a working example of EGL + (desktop) OpenGL using shaders?  
Or any other suggestion for loading OpenGL functions running with EGL?  
My Nvidia Linux driver version is 361.42.  
Would it be helpful to upgrade the driver to a recent version?

---

<div class="post-metadata">

**Author:** ![wenyang.chu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wenyang.chu](https://forums.developer.nvidia.com/u/wenyang.chu)\
**Post date:** [September 1, 2017, 9:19am UTC](https://forums.developer.nvidia.com/t/opengl-without-x-using-egl/49523/2 "2017-09-01T09:19:25Z")

</div>

Hi  
I run into the same situation. Have you found a solution?  
Thanks

---

<div class="post-metadata">

**Author:** ![hgrano](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@hgrano](https://forums.developer.nvidia.com/u/hgrano)\
**Post date:** [September 11, 2017, 6:26am UTC](https://forums.developer.nvidia.com/t/opengl-without-x-using-egl/49523/3 "2017-09-11T06:26:43Z")

</div>

I’m also experiencing similar problems. Did you guys check for errors occurring during the EGL initialisation (this is not included in the example code from [https://devblogs.nvidia.com/parallelforall/egl-eye-opengl-visualization-without-x-server/](https://devblogs.nvidia.com/parallelforall/egl-eye-opengl-visualization-without-x-server/)  
). For me, during execution of the following code my program fails as eglGetDisplay(EGL\_DEFAULT\_DISPLAY) returns EGL\_NO\_DISPLAY. Anyone also have this problem?

```auto
void checkEglErrors()
{
    EGLint err = eglGetError();
    if (EGL_SUCCESS != err)
    {
        std::cerr << "EGL error: " << std::hex << err << std::endl;
        throw std::runtime_error("EGL Error");
    }
}

int main(int argc, char *argv[])
{
    // 1. Initialize EGL
    EGLDisplay eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (EGL_NO_DISPLAY == eglDpy)
    {
        std::cerr << "eglGetDisplay returned EGL_NO_DISPLAY" << std::endl;
        return 1;
    }

    EGLint major, minor;

    if (eglInitialize(eglDpy, &major, &minor) == EGL_FALSE)
    {
        std::cerr << "Unable to initialize EGL using eglInitialize" << std::endl;
        return 1;
    }

    // 2. Select an appropriate configuration
    EGLint numConfigs;
    EGLConfig eglCfg;

    if (EGL_FALSE == eglChooseConfig(eglDpy, configAttribs, &eglCfg, 1, &numConfigs))
    {
        std::cerr << "eglChooseConfig failed" << std::endl;
        return 1;
    }

     // 3. Create a surface
    EGLSurface eglSurf = eglCreatePbufferSurface(eglDpy, eglCfg, pbufferAttribs);
    if (EGL_NO_SURFACE == eglSurf)
    {
        std::cerr << "Unable to initialize EGLSurface" << std::endl;
        return 1;
    }
    // 4. Bind the API
    if (EGL_FALSE == eglBindAPI(EGL_OPENGL_API)) 
    {
        std::cerr << "Unable to bind EGL API to OpenGL" << std::endl;
        return 1;
    }

    // 5. Create a context and make it current
    EGLContext eglCtx = eglCreateContext(eglDpy, eglCfg, EGL_NO_CONTEXT, NULL);
    if (EGL_NO_CONTEXT == eglCtx)
    {
        std:cerr << "Unable to initialize EGLContext" << std::endl;
        return 1;
    }

    if (EGL_FALSE == eglMakeCurrent(eglDpy, eglSurf, eglSurf, eglCtx))
    {
        std::cerr << "Unable to make EGL context current" << std::endl;
        return 1;
    }

    checkEglErrors();
    // from now on use your OpenGL context

    // 6. Terminate EGL when finished
    eglTerminate(eglDpy);
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![demotomohiro](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/demotomohiro/32/10550_2.png) [@demotomohiro](https://forums.developer.nvidia.com/u/demotomohiro)\
**Post date:** [September 30, 2017, 12:10am UTC](https://forums.developer.nvidia.com/t/opengl-without-x-using-egl/49523/4 "2017-09-30T00:10:27Z")

</div>

When you use EGL and (desktop)OpenGL with GLEW, you need GLEW 2.0.0 or newer.  
And GLEW must be built with

```auto
make SYSTEM=linux-egl

```

Here is my working EGL + OpenGL + GLEW initialize/uninitialize code for off-line rendering(Only render to frame buffer object).  
[https://github.com/demotomohiro/Reflection-Refraction-less-Ronpa-Raytracing-Renderer/blob/master/src/glcontext\_egl.cpp](https://github.com/demotomohiro/Reflection-Refraction-less-Ronpa-Raytracing-Renderer/blob/master/src/glcontext_egl.cpp)
