-2

Most of the OpenGL API calls do not return a value. With C++11 perfect forwarding, it would be easy to write an error-checking wrapper function template and variadic macro such as this:

template <typename F, typename ...Args>
void call(
    const char *text,
    int line,
    const char *file,
    F && f, Args &&... args
    )
{
    std::forward<F>(f)(std::forward<Args>(args)...);
    auto err = glGetError();
    if (err != 0) _throw_error(text, err, line, file);
}

#define GL(fn, ...) call(#fn, __LINE__, __FILE__, gl##fn, __VA_ARGS__)

This works and allows calls to be written like this:

GL(Viewport, 0, 0, 1920, 1080);

However, some OpenGL functions do return a value, making it necessary for the wrapper function to store the return value in a temporary variable before checking for errors. The wrapper function template for calls with return values could be written like this:

template <typename F, typename ...Args>
auto call(
    const char *text,
    int line,
    const char *file,
    F && f, Args &&... args
    )
{
    auto && res = std::forward<F>(f)(std::forward<Args>(args)...);
    auto err = glGetError();
    if (err != 0) _throw_error(text, err, line, file);
    return res;
}

(The same macro can be used to call this form.)

The problem is that it is not possible to define both of the above function templates, because for all API calls with void return type, both templates will match, leading to an "ambiguous call to overloaded function" error (Visual C++).

It would be possible, of course, to define separate macros for the two forms, but that would not be elegant. GL is the natural, obvious and correct choice for the name of the wrapper macro, and any additional macro (such as GLR) would appear ugly and be difficult to remember.

5
  • This is going to be a self-answered question Then answer it while you ask it. Don't ask it, then spend minutes compiling the answer. That's why there's an "answer" checkbox at the bottom of the Ask a Question page. Dec 5, 2015 at 19:12
  • 4
    Also, you're using glGetError wrong. It can return multiple errors. And glIntercept is much better at this anyway. Dec 5, 2015 at 19:15
  • I missed that checkbox. But that's not worth a downvote or a vote to close. I honestly didn't know about multiple error flags though, and I admit I'm not sure how to handle this. In my experience though, calling glGetError() after each call has always done the job, except for glEnd() which is deprecated anyway. Dec 5, 2015 at 19:23
  • 3
    You know that there are debug contexts in modern OpenGL?
    – BDL
    Dec 5, 2015 at 19:35
  • @BDL: I actually didn't. I just looked it up, and it sounds like I'm gonna need that sometime. However the purpose here is plain error-checking. Dec 5, 2015 at 19:49

2 Answers 2

4

Especially in a release build, you do not want to call glGetError after every single call. It will not be good for performance, and glGetError doesn't return descriptive errors anyway (GL_INVALID_OPERATION is used for a lot of things).

Instead, for debugging, create a debug context and register a callback using glDebugMessageCallback​ that prints out the error messages. This is a relatively new OpenGL feature, but provides much better messages in my experience.

void debug_callback(GLenum source​, GLenum type​, GLuint id​,
    GLenum severity​, GLsizei length​, const GLchar* message​, const void* userParam) {
    printf("%s", message);
}
glDebugMessageCallback​(&debug_callback​, nullptr​);
3
  • Thanks @Colonel! I'm going to add this to my toolbox. Regarding performance, I haven't shown this in my posts, but the release build defines the macro GL so that it just calls the function directly, without any checking at all. Dec 5, 2015 at 19:55
  • is the debug callback called before the OpenGL call returns? Can this be relied on? Dec 6, 2015 at 0:10
  • @JPNotADragon The callback function may be called asynchronously, unless you call glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS) Dec 6, 2015 at 16:24
1

The solution to the above problem is not very difficult, however I was surprised to find that Visual C++ would not accept (meaning that it choked with an internal compiler error) the solution that I had worked out and verified using Coliru, which uses gcc.

What is needed of course is SFINAE, and C++11 supports that with the very handy std::enable_if<>. The solution that worked with gcc, but didn't with Visual Studio 2015, was this:

template <typename F, typename ...Args,
    typename R = std::enable_if_t<!std::is_void<std::result_of_t<F(Args...)>>::value, std::result_of_t<F(Args...)>>>
>
R call(const char *text, int line, const char *file, F && f, Args &&...args)
{...}

(and similar for void functions, with std:is_void<...>::value instead).

After some searching, I came up with the following that does compile:

template <typename F, typename ...Args>
auto call(
    std::enable_if_t<!std::is_void<std::result_of_t<F(Args...)>>::value, const char> *text,
    int line,
    const char *file,
    F && f, Args &&... args
    )
{
    auto && res = std::forward<F>(f)(std::forward<Args>(args)...);
    auto err = glGetError();
    if (err != 0) _throw_error(text, err, line, file);
    return res;
}

and

template <typename F, typename ...Args>
void call(
    std::enable_if_t<std::is_void<std::result_of_t<F(Args...)>>::value, const char> *text,
    int line,
    const char *file,
    F && f, Args &&... args
    )
{
    std::forward<F>(f)(std::forward<Args>(args)...);
    auto err = glGetError();
    if (err != 0) _throw_error(text, err, line, file);
}

The only remarkable thing about this solution is the way it makes use of SFINAE. std::enable_if_t<> not only serves to eliminate the unwanted specialization, but at the same time declares the type of the text parameter passed in by the macro. So, instead of doing SFINAE via an extra, unused parameter with a default value (which I tried but for some reason also lead to an "internal compiler error" under Visual Studio), I just used it on one of the three extra parameters that I needed anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.