问题描述
有时,需要很长时间插入条件打印,并检查 glGetError()
使用二叉搜索的形式缩小范围,其中第一个函数调用是错误首先由OpenGL报告。
Sometimes it takes a long time of inserting conditional prints and checks to glGetError()
to narrow down using a form of binary search where the first function call is that an error is first reported by OpenGL.
我认为如果有一种方法可以构建一个宏,我可以包围所有GL调用可能会失败,这将是很酷的有条件调用 glGetError
之后。当编译一个特殊目标时,我可以用非常高的粒度检查 glGetError
,而编译典型的发布或调试这不会启用(我会检查它只有一帧)。
I think it would be cool if there is a way to build a macro which I can wrap around all GL calls which may fail which will conditionally call glGetError
immediately after. When compiling for a special target I can have it check glGetError
with a very high granularity, while compiling for typical release or debug this wouldn't get enabled (I'd check it only once a frame).
这样做有意义吗?搜索这一点我发现几个人建议在每次非绘制gl-call之后调用 glGetError
,这基本上是我描述的相同的事情。
Does this make sense to do? Searching for this a bit I find a few people recommending calling glGetError
after every non-draw gl-call which is basically the same thing I'm describing.
所以在这种情况下,有什么聪明的我可以做的(上下文:我使用GLEW)来简化我的gl调用这种方式的过程?在这一点上,转换我的代码以围绕每个OpenGL函数调用包装宏,这将是一个大量的工作。什么是伟大的是,如果我可以做一些聪明,并获得所有这一切,而无需手动确定哪些是代码片段(虽然这也有潜在的优势,但不是真的。真的
So in this case is there anything clever that I can do (context: I am using GLEW) to simplify the process of instrumenting my gl calls this way? It would be a significant amount of work at this point to convert my code to wrap a macro around each OpenGL function call. What would be great is if I can do something clever and get all of this going without manually determining which are the sections of code to instrument (though that also has potential advantages... but not really. I really don't care about performance by the time I'm debugging the source of an error).
推荐答案
尝试这样:
void CheckOpenGLError(const char* stmt, const char* fname, int line)
{
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
printf("OpenGL error %08x, at %s:%i - for %s\n", err, fname, line, stmt);
abort();
}
}
#ifdef _DEBUG
#define GL_CHECK(stmt) do { \
stmt; \
CheckOpenGLError(#stmt, __FILE__, __LINE__); \
} while (0)
#else
#define GL_CHECK(stmt) stmt
#endif
使用它像这样:
GL_CHECK( glBindTexture2D(GL_TEXTURE_2D, id) );
如果OpenGL函数返回变量,请记住在GL_CHECK之外声明:
If OpenGL function returns variable, then remember to declare it outside of GL_CHECK:
const char* vendor;
GL_CHECK( vendor = glGetString(GL_VENDOR) );
$ b
This way you'll have debug checking if _DEBUG preprocessor symbol is defined, but in "Release" build you'll have just the raw OpenGL calls.
这篇关于(定义一个宏)方便OpenGL命令调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!