5

我尝试在网上搜索,但我必须注意,要找到有关 X 编程这方面的资料并不容易。

我使用 X 和 GLX 来创建 OpenGL 上下文。我已经知道我当前的显卡驱动程序仅支持最高 3.3 版的 OpenGL API,但我希望我的应用程序能够尝试使用任何类型的版本创建上下文(因为它可以在其他计算机上运行)。我的代码是这样的:

  • 版本<- 请求的 OpenGL 版本(例如:3.3)
  • 创建上下文:
    • 如果版本是 3.x 或 4.x,请使用glXCreateContextAttribsARB
    • 否则使用glXCreateContext
  • 如果创建上下文失败,请降低一个版本(3.3 变为 3.2,或 3.0 变为 2.1,...)
  • 如果创建了上下文或者甚至无法使用最低 OpenGL 版本,则停止。

我的代码还可以,但我希望在检索 X/GLX 启动的错误的方式上更加干净,目前,如果我使用glXCreateContextAttribARB创建 4.4 版本(请记住,我的显卡最多只支持3.3),我显然得到:

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  153 (GLX)
  Minor opcode of failed request:  34 ()
  Serial number of failed request:  33
  Current serial number in output stream:  34

我想在我的代码中插入 X 的错误处理来处理它。X 是 C,而不是 C++,在这个阶段异常是不可用的。这是我创建上下文的地方(我故意删除了不直接创建上下文的内容):

// Notes :
// mGLXContext : The GLX context we want to create
// vDepthSize, vAntialiasingLevel, vStencilSize are here to customize mGLXContext
// vTryVersion : a pointer to the API version {major, minor} we want to create
// vSharedContext : a pointer to an other (existing) context for data sharing.
// mXDisplay : the X Display


// Get the proc
const GLubyte* vProcName = reinterpret_cast<const GLubyte*>("glXCreateContextAttribsARB");
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = 
    reinterpret_cast<PFNGLXCREATECONTEXTATTRIBSARBPROC>(glXGetProcAddress(vProcName));

if(glXCreateContextAttribsARB) {

    // Create the FB attributes
    int vFBAttributes[] = {
        GLX_DEPTH_SIZE, (int)(vDepthSize),
        GLX_STENCIL_SIZE, (int)(vStencilSize),
        GLX_SAMPLE_BUFFERS, vAntialiasingLevel > 0,
        GLX_SAMPLES, (int)(vAntialiasingLevel),
        GLX_RED_SIZE, 8,
        GLX_GREEN_SIZE, 8,
        GLX_BLUE_SIZE, 8,
        GLX_ALPHA_SIZE, pDepth == 32 ? 8 : 0,
        GLX_DOUBLEBUFFER, True,
        GLX_X_RENDERABLE, True,
        GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
        GLX_RENDER_TYPE, GLX_RGBA_BIT,
        GLX_CONFIG_CAVEAT, GLX_NONE,
        None
    };


    // Get the FB configs
    int vNbXConfigs = 0;
    ::GLXFBConfig* vGLXFBConfig = glXChooseFBConfig(mXDisplay, DefaultScreen(mXDisplay), vFBAttributes, &vNbXConfigs);

    if(vGLXFBConfig && vNbXConfigs) {

        // Create the context attributes
        int vAttributes[] = {
            GLX_CONTEXT_MAJOR_VERSION_ARB, static_cast<int>(vTryVersion->major),
            GLX_CONTEXT_MINOR_VERSION_ARB, static_cast<int>(vTryVersion->minor),
            0, 0
        };

        // Create the context : Error is generated by GLX here
        mGLXContext = glXCreateContextAttribsARB(mXDisplay, vGLXFBConfig[0], vSharedContext, true, vAttributes);

    }
}

所以我的问题是如何捕获 X 错误并查询它们?

感谢您阅读:)

4

1 回答 1

5

您需要使用XSetErrorHandler来指定错误处理程序,例如

XSetErrorHandler(handler);

错误处理程序是

int handler(Display * d, XErrorEvent * e)
{
    std::cerr << "Error code: " << e->error_code << std::endl;
    return 0;
}
于 2013-11-21T13:17:01.573 回答