(Define a macro to) facilitate OpenGL command debugging?

后端 未结 2 570
情深已故
情深已故 2021-02-09 07:56

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

相关标签:
2条回答
  • 2021-02-09 08:42

    BuGLe sounds like it will do what you want:

    • Dump a textual log of all GL calls made.
    • Take a screenshot or capture a video.
    • Call glGetError after each call to check for errors, and wrap glGetError so that this checking is transparent to your program.
    • Capture and display statistics (such as frame rate)
    • Force a wireframe mode
    • Recover a backtrace from segmentation faults inside the driver, even if the driver is compiled without symbols.
    0 讨论(0)
  • 2021-02-09 08:48

    Try this:

    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
    

    Use it like this:

    GL_CHECK( glBindTexture2D(GL_TEXTURE_2D, id) );
    

    If OpenGL function returns variable, then remember to declare it outside of GL_CHECK:

    const char* vendor;
    GL_CHECK( vendor = glGetString(GL_VENDOR) );
    

    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.

    0 讨论(0)
提交回复
热议问题