简体   繁体   English

EGL在第一次opengl函数调用时崩溃

[英]EGL crashes at first opengl function call

I am using nvidia 1060 gtx card on arch linux with newest drivers. 我在带有最新驱动程序的nvidia 1060 gtx linux上使用nvidia 1060 gtx卡。 But ELG does not work at all, actually it crashes at first call to an opengl function eg glGenBuffers() . 但是ELG根本不起作用,实际上它在第一次调用opengl函数(例如glGenBuffers()时崩溃。 Everything works well when using GLFW for creating a context. 使用GLFW创建上下文时,一切工作正常。 But there is a need here to not depend on x-server presence. 但是这里有必要不依赖x-server存在。

The following code was compiled using : gcc testegl.cpp -lGLEW -lGL -lEGL_nvidia -lEGL -lstdc++ -o testegl 使用以下代码编译了以下代码: gcc testegl.cpp -lGLEW -lGL -lEGL_nvidia -lEGL -lstdc++ -o testegl

Output is: 输出为:

  • Starting... 开始...
  • Segmentation fault (core dumped) 分段故障(核心已转储)

testegl.cpp: testegl.cpp:

#include <EGL/egl.h>
#include <GL/glew.h>
#include <exception>
#include <string>
#include <iostream>

class GLEGLContext {
private:
    EGLDisplay mDisplay;
    EGLSurface mSurface;
    EGLContext mContext;

public:
    GLEGLContext ();
    ~GLEGLContext ();
    GLEGLContext (const GLEGLContext& ) = delete;
    GLEGLContext& operator = (const GLEGLContext&) = delete;

public:
    virtual void makeCurrent ();
    virtual void releaseCurrent ();
};

GLEGLContext::GLEGLContext () {
    const EGLint configAttribs[] = {
        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_DEPTH_SIZE, 0,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
        EGL_NONE
    };    

    const EGLint pbufferAttribs[] = {
        EGL_WIDTH, 1920,
        EGL_HEIGHT, 1080,
        EGL_NONE,
    };

    mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (mDisplay == EGL_NO_DISPLAY)
        throw StdException(__PRETTY_FUNCTION__, "eglGetDisplay() failed");

    EGLint major, minor;
    if (!eglInitialize(mDisplay, &major, &minor))
        throw StdException(__PRETTY_FUNCTION__, "eglInitialize() failed");

    EGLint numConfigs;
    EGLConfig eglCfg;
    if (!eglChooseConfig(mDisplay, configAttribs, &eglCfg, 1, &numConfigs))
        throw StdException(__PRETTY_FUNCTION__, "eglChooseConfig() failed");

    mSurface = eglCreatePbufferSurface(mDisplay, eglCfg, pbufferAttribs);
    if (mSurface == EGL_NO_SURFACE)
        throw StdException(__PRETTY_FUNCTION__, "eglCreatePbufferSurface() failed");

    if (!eglBindAPI(EGL_OPENGL_API))
        throw StdException(__PRETTY_FUNCTION__, "eglBindAPI() failed");

    mContext = eglCreateContext(mDisplay, eglCfg, EGL_NO_CONTEXT, NULL);
    if (mContext == EGL_NO_CONTEXT)
        throw StdException(__PRETTY_FUNCTION__, "eglCreateContext() failed");

    makeCurrent();
}

GLEGLContext::~GLEGLContext () {
    eglTerminate(mDisplay);
}

void GLEGLContext::makeCurrent () {
    if (!eglMakeCurrent(mDisplay, mSurface, mSurface, mContext))
        throw StdException(__PRETTY_FUNCTION__, "eglMakeCurrent() create failed");
}

void GLEGLContext::releaseCurrent () {
    if (!eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
        throw StdException(__PRETTY_FUNCTION__, "eglMakeCurrent() release failed");
}

int main(int argc, char** argv) {
    GLEGLContext ctx;
    ctx.makeCurrent();
    std::cout << "Starting..." << std::endl;
    GLuint vertexBuffer = 0;
    glGenBuffers(1, &vertexBuffer);
    std::cout << "done" << std::endl;
    ctx.releaseCurrent();

    return 0;
}

You have to initialize GLEW . 您必须初始化GLEW Call glewInit right after you created the OpenGL context: 创建OpenGL上下文后立即调用glewInit

if ( glewInit() != GLEW_OK )
    return;

Note, that glewInit returns GLEW_OK if succeeded. 请注意,如果成功, glewInit将返回GLEW_OK glewInit initializes the function pointers for the OpenGL functions. glewInit初始化OpenGL函数的函数指针。 If you try to call function via an uninitialized function pointer, a segmentation fault occurs. 如果尝试通过未初始化的函数指针调用函数,则会发生分段错误。

see also the answer to Undefined reference when using glew and mingw? 在使用glew和mingw时,还可以看到未定义参考的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM