Pioneer
GLDebug.h
Go to the documentation of this file.
1 // Copyright © 2008-2012 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _GLDEBUG_H
5 #define _GLDEBUG_H
6 /*
7  * OpenGL debug helper using the GL_KHR_debug extension
8  * which does not require a debug context.
9  *
10  * Something similar can be found at:
11  * https://github.com/OpenGLInsights/ (Chapter 33)
12  * which also includes stack printout
13  */
14 #include "OpenGLLibs.h"
15 #include "utils.h"
16 
17 #ifdef _WIN32
18 #define STDCALL __stdcall
19 #else
20 #define STDCALL
21 #endif
22 
23 // some people build with an old version of GLEW that doesn't include KHR_debug
24 #if 1 //(GL_ARB_debug_output && GL_KHR_debug)
25 
26 namespace Graphics {
27 
28  class GLDebug {
29  private:
30  static const char *type_to_string(GLenum type)
31  {
32  switch (type) {
33  case GL_DEBUG_TYPE_ERROR:
34  return ("Error");
35  case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
36  return ("Deprecated Behaviour");
37  case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
38  return ("Undefined Behaviour");
39  case GL_DEBUG_TYPE_PORTABILITY:
40  return ("Portability");
41  case GL_DEBUG_TYPE_PERFORMANCE:
42  return ("Performance");
43  case GL_DEBUG_TYPE_OTHER:
44  return ("Other");
45  default:
46  return ("");
47  }
48  }
49 
50  static const char *source_to_string(GLenum source)
51  {
52  switch (source) {
53  case GL_DEBUG_SOURCE_API:
54  return ("API");
55  case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
56  return ("Window System");
57  case GL_DEBUG_SOURCE_SHADER_COMPILER:
58  return ("Shader Compiler");
59  case GL_DEBUG_SOURCE_THIRD_PARTY:
60  return ("Third Party");
61  case GL_DEBUG_SOURCE_APPLICATION:
62  return ("Application");
63  case GL_DEBUG_SOURCE_OTHER:
64  return ("Other");
65  default:
66  return ("");
67  }
68  }
69 
70  static const char *severity_to_string(GLenum severity)
71  {
72  switch (severity) {
73  case GL_DEBUG_SEVERITY_HIGH:
74  return ("High");
75  case GL_DEBUG_SEVERITY_MEDIUM:
76  return ("Medium");
77  case GL_DEBUG_SEVERITY_LOW:
78  return ("Low");
79  default:
80  return ("");
81  }
82  }
83 
84  // put a breakpoint in this function
85  static void STDCALL PrintMessage(GLenum source, GLenum type,
86  GLuint id, GLenum severity, GLsizei length,
87  const GLchar *message, const void *userParam)
88  {
89  // filter out Type=Other informational messages
90  if (type > GL_DEBUG_TYPE_PERFORMANCE)
91  return;
92  Output("Type: %s, Source: %s, ID: %u, Severity: %s, Message: %s\n",
93  type_to_string(type), source_to_string(source), id,
94  severity_to_string(severity), message);
95  }
96 
97  public:
98  //register the callback function, if the extension is available
99  static void Enable()
100  {
101  if (!glewIsSupported("GL_KHR_debug")) {
102  Output("GL_KHR_debug is not supported; GLDebug will not work\n");
103  return;
104  }
105 
106  glEnable(GL_DEBUG_OUTPUT);
107  glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
108  glDebugMessageCallback(PrintMessage, 0);
109 
110  //Using the default message type and severity parameters.
111  //If you want to be drowned in performance warnings, use:
112  //glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, 0, true);
113  }
114 
115  static void Disable()
116  {
117  if (glewIsSupported("GL_KHR_debug")) {
118  glDisable(GL_DEBUG_OUTPUT);
119  }
120  }
121  };
122 
123 } // namespace Graphics
124 
125 #else
126 
127 namespace Graphics {
128 
129  class GLDebug {
130  public:
131  static void Enable()
132  {
133  Output("GL Debug support was excluded from this build because the glLoadGen headers did not include support for it.\n");
134  }
135 
136  static void Disable() {}
137  };
138 
139 } // namespace Graphics
140 
141 #endif
142 
143 #endif
#define STDCALL
Definition: GLDebug.h:20
Definition: GLDebug.h:28
static void Enable()
Definition: GLDebug.h:99
static void Disable()
Definition: GLDebug.h:115
Definition: Background.h:14
void Output(const char *message, Args... args)
Definition: utils.h:41