(Define a macro to) facilitate OpenGL command debugging?

后端 未结 2 571
情深已故
情深已故 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: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.

提交回复
热议问题