File: gl.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (67 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once

#include "igl.h"
#include "string/convert.h"

namespace debug
{

/**
 * \brief Check and print GL errors
 *
 * Reads GL errors and dumps them to the internal console, returning the most
 * recent error to calling code.
 *
 * \param context
 * Optional caller-defined context string which will be printed along with any
 * errors. This can help identify what code was doing at the point where GL
 * errors were detected.
 *
 * \return The last GL error, or 0 if there is no error
 */
inline GLenum checkGLErrors(const std::string& context = {})
{
    // Return if no error
    GLenum error = glGetError();
    if (error == GL_NO_ERROR)
    {
        return GL_NO_ERROR;
    }

    // Build list of all GL errors
    std::string allErrString;
    int maxErrors = 10;
    GLenum lastError = error;
    for (; error != GL_NO_ERROR; error = glGetError())
    {
        const char* strErr = reinterpret_cast<const char*>(
            gluErrorString(error)
        );
        allErrString += string::to_string(error);
        allErrString += " (" + std::string(strErr) + ") ";

        if (--maxErrors <= 0)
        {
            allErrString += "---> Maximum number of GL errors reached, maybe there is a problem with the GL context?";
            break;
        }

        lastError = error;
    }

    // Show the error message return last error
    rError() << "OpenGL Error(s)"
             << (context.empty() ? "" : " [" + context + "]") << ":\n"
             << allErrString << "\n";
    return lastError;
}

/// Check GL errors only in debug mode
inline void assertNoGlErrors(const std::string& context = {})
{
#if !defined(NDEBUG)
    checkGLErrors(context);
#endif
}

}