OpenGL error checking is rather nasty and works like this in C:
glUseProgram(...); // Make a call
if (glGetError() != GL_NO_ERROR) {
// error ocurred
}
Thus you have to call the glGetError
function after every OpenGL call.
Now I'm using Rust and the gl-rs crate, and I have a handy glchk!()
macro that does this error checking in debug builds and prints nice error messages.
Unfortunately, I'm fairly new to Rust, and I could use a code review and some advice.
E.g. this works well on statements:
glchk!(gl::UseProgram(...));
But any functions that return a value look a little weird. e.g.:
glchk!(uniform_mtx =
gl::GetUniformLocation(program, CString::new("mtx").unwrap().as_ptr()));
If you had to implement this, how would you do so? The full macro is as follows:
macro_rules! glchk {
($s:stmt) => {
$s;
if cfg!(debug_assertions) {
let err = gl::GetError();
match err {
gl::NO_ERROR => {
},
_ => {
let err_str = match err {
gl::INVALID_ENUM => "GL_INVALID_ENUM",
gl::INVALID_VALUE => "GL_INVALID_VALUE",
gl::INVALID_OPERATION => "GL_INVALID_OPERATION",
gl::INVALID_FRAMEBUFFER_OPERATION => "GL_INVALID_FRAMEBUFFER_OPERATION",
gl::OUT_OF_MEMORY => "GL_OUT_OF_MEMORY",
gl::STACK_UNDERFLOW => "GL_STACK_UNDERFLOW",
gl::STACK_OVERFLOW => "GL_STACK_OVERFLOW",
_ => "unknown error"
};
println!("{}:{} - {} caused {}",
file!(),
line!(),
stringify!($s),
err_str);
}
}
}
}
}