Last active
February 12, 2017 13:05
-
-
Save iondune/59e5d9a6d0b9edfa85fa35c957961371 to your computer and use it in GitHub Desktop.
OpenGL Debugging Tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "OpenGLDebug.h" | |
| char const * GetOpenGLErrorString(GLenum const Error) | |
| { | |
| switch (Error) | |
| { | |
| case GL_NO_ERROR: | |
| return "GL_NO_ERROR"; | |
| case GL_INVALID_ENUM: | |
| return "GL_INVALID_ENUM"; | |
| case GL_INVALID_VALUE: | |
| return "GL_INVALID_VALUE"; | |
| case GL_INVALID_OPERATION: | |
| return "GL_INVALID_OPERATION"; | |
| case GL_INVALID_FRAMEBUFFER_OPERATION: | |
| return "GL_INVALID_FRAMEBUFFER_OPERATION"; | |
| case GL_OUT_OF_MEMORY: | |
| return "GL_OUT_OF_MEMORY"; | |
| case GL_STACK_UNDERFLOW: | |
| return "GL_STACK_UNDERFLOW"; | |
| case GL_STACK_OVERFLOW: | |
| return "GL_STACK_OVERFLOW"; | |
| default: | |
| return "UNKNOWN"; | |
| } | |
| } | |
| void PrintOpenGLErrors(char const * const Function, char const * const File, int const Line) | |
| { | |
| GLenum Error = glGetError(); | |
| if (Error != GL_NO_ERROR) | |
| { | |
| char const * const ErrorString = GetOpenGLErrorString(Error); | |
| printf("OpenGL error in file '%s' at line %d calling function '%s': '%s' '%d 0x%X'\n", File, Line, Function, ErrorString, Error, Error); | |
| } | |
| } | |
| void __stdcall DebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * message, void const * userParam) | |
| // You might have to use this alternative declaration on Unix-based OS | |
| //void DebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * message, void const * userParam) | |
| { | |
| char const * Source = ""; | |
| char const * Type = ""; | |
| char const * Severity = ""; | |
| switch (source) | |
| { | |
| default: | |
| Source = "???"; | |
| break; | |
| case GL_DEBUG_SOURCE_API: | |
| Source = "API"; | |
| break; | |
| case GL_DEBUG_SOURCE_WINDOW_SYSTEM: | |
| Source = "Window System"; | |
| break; | |
| case GL_DEBUG_SOURCE_SHADER_COMPILER: | |
| Source = "Shader Compiler"; | |
| break; | |
| case GL_DEBUG_SOURCE_THIRD_PARTY: | |
| Source = "Third Party"; | |
| break; | |
| case GL_DEBUG_SOURCE_APPLICATION: | |
| Source = "Application"; | |
| break; | |
| case GL_DEBUG_SOURCE_OTHER: | |
| Source = "Other"; | |
| break; | |
| } | |
| switch (type) | |
| { | |
| default: | |
| Type = "???"; | |
| break; | |
| case GL_DEBUG_TYPE_ERROR: | |
| Type = "Error"; | |
| break; | |
| case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: | |
| Type = "Deprecated Behavior"; | |
| break; | |
| case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: | |
| Type = "Undefined Behavior"; | |
| break; | |
| case GL_DEBUG_TYPE_PORTABILITY: | |
| Type = "Portability"; | |
| break; | |
| case GL_DEBUG_TYPE_PERFORMANCE: | |
| Type = "Performance"; | |
| break; | |
| case GL_DEBUG_TYPE_OTHER: | |
| Type = "Other"; | |
| break; | |
| case GL_DEBUG_TYPE_MARKER: | |
| Type = "Marker"; | |
| break; | |
| case GL_DEBUG_TYPE_PUSH_GROUP: | |
| Type = "Push Group"; | |
| break; | |
| case GL_DEBUG_TYPE_POP_GROUP: | |
| Type = "Pop Group"; | |
| break; | |
| } | |
| switch (severity) | |
| { | |
| default: | |
| Severity = "???"; | |
| break; | |
| case GL_DEBUG_SEVERITY_HIGH: | |
| Severity = "High"; | |
| break; | |
| case GL_DEBUG_SEVERITY_MEDIUM: | |
| Severity = "Medium"; | |
| break; | |
| case GL_DEBUG_SEVERITY_LOW: | |
| Severity = "Low"; | |
| break; | |
| case GL_DEBUG_SEVERITY_NOTIFICATION: | |
| Severity = "Notification"; | |
| break; | |
| } | |
| if (severity != GL_DEBUG_SEVERITY_NOTIFICATION) | |
| { | |
| printf("OpenGL Debug Message [%s/%s/%s]: %s\n", Source, Type, Severity, message); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string> | |
| #include <GL/glew.h> | |
| char const * GetOpenGLErrorString(GLenum const Error); | |
| void PrintOpenGLErrors(char const * const Function, char const * const File, int const Line); | |
| void __stdcall DebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * message, void const * userParam); | |
| // You might have to use this alternative declaration on Unix-based OS | |
| //void DebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * message, void const * userParam) | |
| #ifdef _DEBUG | |
| #define CheckedGLCall(x) do { PrintOpenGLErrors(">>BEFORE<< "#x, __FILE__, __LINE__); (x); PrintOpenGLErrors(#x, __FILE__, __LINE__); } while (0) | |
| #else | |
| #define CheckedGLCall(x) (x) | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "OpenGLDebug.h" | |
| int main() | |
| { | |
| glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); | |
| // ... | |
| glewInit(); | |
| glDebugMessageCallback(DebugMessageCallback, nullptr); | |
| // ... | |
| CheckedGLCall(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0)); | |
| // ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment