better logging and OpenGL ARB_debug_output
[laserbrain_demo] / src / opengl.c
1 #include "opengl.h"
2 #include "logger.h"
3
4 static void gldebug_logger(unsigned int src, unsigned int type, unsigned int id,
5                 unsigned int severity, int len, const char *msg, const void *cls);
6 static const char *gldebug_srcstr(unsigned int src);
7 static const char *gldebug_typestr(unsigned int type);
8 static const char *gldebug_sevstr(unsigned int sev);
9
10 struct GLCaps glcaps;
11
12 int init_opengl(void)
13 {
14         glewInit();
15
16         glcaps.debug = GLEW_ARB_debug_output;
17
18 #ifndef NDEBUG
19         if(glcaps.debug) {
20                 info_log("Installing OpenGL debug callback\n");
21                 glDebugMessageCallback(gldebug_logger, 0);
22         }
23 #endif
24
25         return 0;
26 }
27
28
29 static void gldebug_logger(unsigned int src, unsigned int type, unsigned int id,
30                 unsigned int severity, int len, const char *msg, const void *cls)
31 {
32         static const char *fmt = "[GLDEBUG] (%s) %s: %s\n";
33         switch(type) {
34         case GL_DEBUG_TYPE_ERROR:
35                 error_log(fmt, gldebug_srcstr(src), gldebug_typestr(type), msg);
36                 break;
37
38         case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
39         case GL_DEBUG_TYPE_PORTABILITY:
40         case GL_DEBUG_TYPE_PERFORMANCE:
41                 warning_log(fmt, gldebug_srcstr(src), gldebug_typestr(type), msg);
42                 break;
43
44         default:
45                 debug_log(fmt, gldebug_srcstr(src), gldebug_typestr(type), msg);
46         }
47 }
48
49 static const char *gldebug_srcstr(unsigned int src)
50 {
51         switch(src) {
52         case GL_DEBUG_SOURCE_API:
53                 return "api";
54         case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
55                 return "wsys";
56         case GL_DEBUG_SOURCE_SHADER_COMPILER:
57                 return "sdrc";
58         case GL_DEBUG_SOURCE_THIRD_PARTY:
59                 return "3rdparty";
60         case GL_DEBUG_SOURCE_APPLICATION:
61                 return "app";
62         case GL_DEBUG_SOURCE_OTHER:
63                 return "other";
64         default:
65                 break;
66         }
67         return "unknown";
68 }
69
70 static const char *gldebug_typestr(unsigned int type)
71 {
72         switch(type) {
73         case GL_DEBUG_TYPE_ERROR:
74                 return "error";
75         case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
76                 return "deprecated";
77         case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
78                 return "undefined behavior";
79         case GL_DEBUG_TYPE_PORTABILITY:
80                 return "portability warning";
81         case GL_DEBUG_TYPE_PERFORMANCE:
82                 return "performance warning";
83         case GL_DEBUG_TYPE_OTHER:
84                 return "other";
85         default:
86                 break;
87         }
88         return "unknown";
89 }
90
91 static const char *gldebug_sevstr(unsigned int sev)
92 {
93         switch(sev) {
94         case GL_DEBUG_SEVERITY_HIGH:
95                 return "high";
96         case GL_DEBUG_SEVERITY_MEDIUM:
97                 return "medium";
98         case GL_DEBUG_SEVERITY_LOW:
99                 return "low";
100         default:
101                 break;
102         }
103         return "unknown";
104 }