【发布时间】:2013-11-20 21:43:25
【问题描述】:
我尝试在网上搜索,但我必须注意,要找到有关 X 编程这方面的资料并不容易。
我使用 X 和 GLX 来创建 OpenGL 上下文。我已经知道我当前的显卡驱动程序仅支持最高 3.3 版的 OpenGL API,但我希望我的应用程序能够尝试使用任何类型的版本创建上下文(因为它可以在其他计算机上运行)。我的代码是这样的:
- 版本
- 创建上下文:
- 如果 版本 是 3.x 或 4.x,请使用
glXCreateContextAttribsARB - 否则使用
glXCreateContext
- 如果 版本 是 3.x 或 4.x,请使用
- 如果创建上下文失败,请降低一个版本(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 错误并查询它们?
感谢您的阅读:)
【问题讨论】:
-
您的意思是捕获代码中的错误,对吧?不读取错误日志文件。
-
是的,这就是我的意思:)。我相信它比 GLX 更多,因为当我在某些 X/GLX 函数上进行搜索时,它还描述了 X 服务器可能生成的错误。