Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
DebugFunctions.cpp
Go to the documentation of this file.
1 #include <Windows.h> //wglGetProcAddress
2 #include "DebugFunctions.h"
3 
4 void APIENTRY DebugFunctions::DebugCallback(GLenum source,
5  GLenum type,
6  GLuint id,
7  GLenum severity,
8  GLsizei length,
9  const GLchar* message,
10  GLvoid* userParam)
11 {
12  // Messages to ignore, as they appear to be a NVIDIA driver peculiarity ->
13  // FBO bound textures have to have nearest neighbor filtering and the texture border definition explicity set
14  if (id == 131218)
15  {
16  // Source: OpenGL
17  // Msg.Type: Performance
18  // Msg. ID: 131218
19  // Severity: Medium
20  // Message: Program/shader state performance warning:
21  // Fragment Shader is going to be recompiled because the shader key based on GL state mismatches.
22  //
23  // "NVIDIA like to optimise their shaders to run as fast as possible with the current OpenGL state
24  // (eg. eliminating code because a uniform is set to 0 or 1).
25  // Therefore it has to be recompiled if the state changes in a way that will cause the optimised shader to give incorrect results."
26  return;
27  }
28  std::string error = FormatDebugOutput(source, type, id, severity, message);
29  std::cout << error << std::endl;
30 }
31 
32 std::string DebugFunctions::FormatDebugOutput( GLenum source,
33  GLenum type,
34  GLuint id,
35  GLenum severity,
36  const char* msg)
37 {
38  std::stringstream stringStream;
39  std::string sourceString;
40  std::string typeString;
41  std::string severityString;
42 
43  switch (source) {
44  case GL_DEBUG_SOURCE_API: {
45  sourceString = "API";
46  break;
47  }
48  case GL_DEBUG_SOURCE_APPLICATION: {
49  sourceString = "Application";
50  break;
51  }
52  case GL_DEBUG_SOURCE_WINDOW_SYSTEM: {
53  sourceString = "Window System";
54  break;
55  }
56  case GL_DEBUG_SOURCE_SHADER_COMPILER: {
57  sourceString = "Shader Compiler";
58  break;
59  }
60  case GL_DEBUG_SOURCE_THIRD_PARTY: {
61  sourceString = "Third Party";
62  break;
63  }
64  case GL_DEBUG_SOURCE_OTHER: {
65  sourceString = "Other";
66  break;
67  }
68  default: {
69  sourceString = "Unknown";
70  break;
71  }
72  }
73 
74  switch (type) {
75  case GL_DEBUG_TYPE_ERROR: {
76  typeString = "Error";
77  break;
78  }
79  case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: {
80  typeString = "Deprecated Behavior";
81  break;
82  }
83  case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: {
84  typeString = "Undefined Behavior";
85  break;
86  }
87  case GL_DEBUG_TYPE_PORTABILITY_ARB: {
88  typeString = "Portability";
89  break;
90  }
91  case GL_DEBUG_TYPE_PERFORMANCE: {
92  typeString = "Performance";
93  break;
94  }
95  case GL_DEBUG_TYPE_OTHER: {
96  typeString = "Other";
97  break;
98  }
99  default: {
100  typeString = "Unknown";
101  break;
102  }
103  }
104 
105  switch (severity) {
106  case GL_DEBUG_SEVERITY_HIGH: {
107  severityString = "High";
108  break;
109  }
110  case GL_DEBUG_SEVERITY_MEDIUM: {
111  severityString = "Medium";
112  break;
113  }
114  case GL_DEBUG_SEVERITY_LOW: {
115  severityString = "Low";
116  break;
117  }
118  default: {
119  severityString = "Unknown";
120  break;
121  }
122  }
123 
124  stringStream << "OpenGL Error: " << msg;
125  stringStream << " [Source = " << sourceString;
126  stringStream << ", Type = " << typeString;
127  stringStream << ", Severity = " << severityString;
128  stringStream << ", ID = " << id << "]";
129 
130  return stringStream.str();
131 }
132 
133 
135 {
136  // Query the OpenGL function to register your callback function.
137  PFNGLDEBUGMESSAGECALLBACKPROC _glDebugMessageCallback = (PFNGLDEBUGMESSAGECALLBACKPROC) wglGetProcAddress("glDebugMessageCallback");
138  PFNGLDEBUGMESSAGECALLBACKARBPROC _glDebugMessageCallbackARB = (PFNGLDEBUGMESSAGECALLBACKARBPROC) wglGetProcAddress("glDebugMessageCallbackARB");
139 
140  // Register your callback function.
141  if (_glDebugMessageCallback != NULL) {
142  _glDebugMessageCallback(DebugCallback, NULL);
143  }
144  else if (_glDebugMessageCallbackARB != NULL) {
145  _glDebugMessageCallbackARB(DebugCallback, NULL);
146  }
147 
148  // Enable synchronous callback. This ensures that your callback function is called
149  // right after an error has occurred. This capability is not defined in the AMD
150  // version.
151  if ((_glDebugMessageCallback != NULL) || (_glDebugMessageCallbackARB != NULL)) {
152  glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
153  }
154 }