OGLplus (0.52.0) a C++ wrapper for OpenGL

KHR_debug.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_EXT_KHR_DEBUG_1308270710_HPP
14 #define OGLPLUS_EXT_KHR_DEBUG_1308270710_HPP
15 
16 #include <oglplus/extension.hpp>
17 #include <oglplus/config/compiler.hpp>
18 #include <oglplus/string/def.hpp>
19 #include <oglplus/string/ref.hpp>
20 #include <oglplus/glfunc.hpp>
24 
25 #include <cassert>
26 #include <cstddef>
27 #include <stack>
28 #include <functional>
29 #include <memory>
30 #include <unordered_set>
31 #include <iostream>
32 
33 namespace oglplus {
34 
35 #if OGLPLUS_DOCUMENTATION_ONLY || GL_KHR_debug
36 
38 
44 class KHR_debug
45 {
46 public:
47  OGLPLUS_EXTENSION_CLASS(KHR, debug)
48 
49 
50  static void Control(
51  DebugSource source,
52  DebugType type,
53  DebugSeverity severity,
54  GLsizei count,
55  const GLuint* ids,
56  bool enable
57  )
58  {
59  OGLPLUS_GLFUNC(DebugMessageControl)(
60  GLenum(source),
61  GLenum(type),
62  GLenum(severity),
63  count, ids,
64  enable ? GL_TRUE : GL_FALSE
65  );
66  OGLPLUS_VERIFY_SIMPLE(DebugMessageControl);
67  }
68 
70  static void Control(
71  DebugSource source,
72  DebugType type,
73  DebugSeverity severity,
74  bool enable
75  )
76  {
77  Control(source, type, severity, 0, nullptr, enable);
78  }
79 
81  template <std::size_t N>
82  static void Control(
83  DebugSource source,
84  DebugType type,
85  DebugSeverity severity,
86  const GLuint (&ids)[N],
87  bool enable
88  )
89  {
90  Control(source, type, severity, N, ids, enable);
91  }
92 
94  struct CallbackData
95  {
98 
101 
103  GLuint id;
104 
107 
109  GLsizei length;
110 
112  const GLchar* message;
113  };
114 
116 
122  typedef std::function<void (const CallbackData&)> Callback;
123 
125 
130  class LogSink
131  {
132  private:
133  static void GLAPIENTRY _gl_debug_proc(
134  GLenum source,
135  GLenum type,
136  GLuint id,
137  GLenum severity,
138  GLsizei length,
139  const GLchar* message,
140  GLvoid* user_param
141  )
142  {
143  LogSink* self = static_cast<LogSink*>(user_param);
144  assert(self);
145  if(self->_callback)
146  {
147  CallbackData data;
148  data.source = DebugSource(source);
149  data.type = DebugType(type);
150  data.id = id;
151  data.severity = DebugSeverity(severity);
152  data.length = length;
153  data.message = message;
154  self->_callback(data);
155  }
156  }
157 
158  Callback _callback;
159  GLDEBUGPROC _prev_callback;
160  void* _prev_context;
161  public:
163  LogSink(Callback callback)
164  : _callback(callback)
165  , _prev_callback(nullptr)
166  , _prev_context(nullptr)
167  {
168  // get the previous callback
169  GLDEBUGPROC _tmp_callback = nullptr;
170  void** _tmp_ptr=reinterpret_cast<void**>(&_tmp_callback);
171  OGLPLUS_GLFUNC(GetPointerv)(
172  GL_DEBUG_CALLBACK_FUNCTION,
173  _tmp_ptr
174  );
175  OGLPLUS_IGNORE(GetPointerv);
176  _prev_callback = _tmp_callback;
177 
178  //get the previous context
179  OGLPLUS_GLFUNC(GetPointerv)(
180  GL_DEBUG_CALLBACK_USER_PARAM,
181  &_prev_context
182  );
183  OGLPLUS_IGNORE(GetPointerv);
184 
185  OGLPLUS_GLFUNC(DebugMessageCallback)(
186  GLDEBUGPROC(&LogSink::_gl_debug_proc),
187  static_cast<void*>(this)
188  );
189  OGLPLUS_VERIFY_SIMPLE(DebugMessageCallback);
190  }
191 
192 #if !OGLPLUS_NO_DELETED_FUNCTIONS
193  LogSink(const LogSink&) = delete;
195 #else
196  private:
197  LogSink(const LogSink&);
198  public:
199 #endif
200 
202  ~LogSink(void)
203  {
204  if(_prev_callback)
205  {
206  OGLPLUS_GLFUNC(DebugMessageCallback)(
207  _prev_callback,
208  _prev_context
209  );
210  }
211  }
212  };
213 
215  static void InsertMessage(
216  DebugSource source,
217  DebugType type,
218  GLuint id,
219  DebugSeverity severity,
220  GLint length,
221  const GLchar* buffer
222  )
223  {
224  OGLPLUS_GLFUNC(DebugMessageInsert)(
225  GLenum(source),
226  GLenum(type),
227  id,
228  GLenum(severity),
229  length,
230  buffer
231  );
232  OGLPLUS_VERIFY_SIMPLE(DebugMessageInsert);
233  }
234 
236  static void InsertMessage(
237  DebugSource source,
238  DebugType type,
239  GLuint id,
240  DebugSeverity severity,
241  StrCRef message
242  )
243  {
245  source,
246  type,
247  id,
248  severity,
249  message.size(),
250  message.c_str()
251  );
252  }
253 
255  template <std::size_t N>
256  static void InsertMessage(
257  DebugSource source,
258  DebugType type,
259  GLuint id,
260  DebugSeverity severity,
261  const GLchar (&message)[N]
262  )
263  {
265  source,
266  type,
267  id,
268  severity,
269  N,
270  message
271  );
272  }
273 
275  static void PushGroup(
276  DebugSource source,
277  GLuint id,
278  GLsizei length,
279  const GLchar* message
280  )
281  {
282  OGLPLUS_GLFUNC(PushDebugGroup)(
283  GLenum(source),
284  id,
285  length,
286  message
287  );
288  OGLPLUS_VERIFY_SIMPLE(PushDebugGroup);
289  }
290 
292  static void PushGroup(
293  DebugSource source,
294  GLuint id,
295  StrCRef message
296  )
297  {
298  PushGroup(source, id, message.size(), message.c_str());
299  }
300 
302  template <std::size_t N>
303  static void PushGroup(
304  DebugSource source,
305  GLuint id,
306  const GLchar (&message)[N]
307  )
308  {
309  PushGroup(source, id, N, message);
310  }
311 
313  static void PopGroup(void)
314  {
315  OGLPLUS_GLFUNC(PopDebugGroup)();
316  OGLPLUS_VERIFY_SIMPLE(PopDebugGroup);
317  }
318 
320  static void Synchronous(bool enable = true)
321  {
322  if(enable)
323  {
324  OGLPLUS_GLFUNC(Enable)(GL_DEBUG_OUTPUT_SYNCHRONOUS);
325  OGLPLUS_VERIFY_SIMPLE(Enable);
326  }
327  else
328  {
329  OGLPLUS_GLFUNC(Disable)(GL_DEBUG_OUTPUT_SYNCHRONOUS);
330  OGLPLUS_VERIFY_SIMPLE(Disable);
331  }
332  }
333 
335  static void Asynchronous(bool enable = true)
336  {
337  Synchronous(!enable);
338  }
339 
340 };
341 
342 template <typename Essence>
343 class KHR_debug_CallbackWithEssence
344 {
345 private:
346  std::shared_ptr<Essence> essence;
347 public:
348  typedef typename KHR_debug::Callback Callback;
349 
350  KHR_debug_CallbackWithEssence(
351  typename Essence::CtrParam param
352  ): essence(std::make_shared<Essence>(param))
353  { }
354 
355  void operator()(const KHR_debug::CallbackData& data)
356  {
357  essence->Call(data);
358  }
359 
360  operator Callback (void) const
361  {
362  return Callback(*this);
363  }
364 };
365 
366 class KHR_debug_UniqueEssence
367 {
368 private:
369  typedef KHR_debug::Callback Callback;
370  Callback _callback;
371 
372  String buffer;
373  std::unordered_set<String> already_done;
374 
375  KHR_debug_UniqueEssence(const KHR_debug_UniqueEssence&);
376 public:
377  typedef const Callback& CtrParam;
378 
379  KHR_debug_UniqueEssence(const Callback& callback)
380  : _callback(callback)
381  { }
382 
383  void Call(const KHR_debug::CallbackData& data);
384 };
385 
386 #if OGLPLUS_DOCUMENTATION_ONLY
387 
396 {
397 public:
400 
402  operator KHR_debug::Callback (void) const;
403 };
404 #else
405 typedef KHR_debug_CallbackWithEssence<KHR_debug_UniqueEssence>
407 #endif
408 
409 
410 class KHR_debug_TreeEssence
411 {
412 private:
413  std::ostream& dbgout;
414 
415  KHR_debug_TreeEssence(const KHR_debug_TreeEssence&);
416 public:
417  typedef std::ostream& CtrParam;
418 
419  KHR_debug_TreeEssence(std::ostream& out);
420  ~KHR_debug_TreeEssence(void);
421 
422  void Call(const KHR_debug::CallbackData& data);
423 };
424 
425 #if OGLPLUS_DOCUMENTATION_ONLY
426 
434 {
435 public:
437  KHR_debug_Tree(std::ostream&);
438 
440  operator KHR_debug::Callback (void) const;
441 };
442 #else
443 typedef KHR_debug_CallbackWithEssence<KHR_debug_TreeEssence>
445 #endif
446 
447 
448 class KHR_debug_ToXMLEssence
449 {
450 private:
451  std::ostream& dbgout;
452 
453  KHR_debug_ToXMLEssence(const KHR_debug_ToXMLEssence&);
454 public:
455  typedef std::ostream& CtrParam;
456 
457  KHR_debug_ToXMLEssence(std::ostream& out);
458  ~KHR_debug_ToXMLEssence(void);
459 
460  void Call(const KHR_debug::CallbackData& data);
461 };
462 
463 #if OGLPLUS_DOCUMENTATION_ONLY
464 
471 {
472 public:
474  KHR_debug_ToXML(std::ostream&);
475 
477  operator KHR_debug::Callback (void) const;
478 };
479 #else
480 typedef KHR_debug_CallbackWithEssence<KHR_debug_ToXMLEssence>
482 #endif
483 
484 #endif // KHR_debug
485 
486 } // namespace oglplus
487 
488 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
489 #include <oglplus/ext/KHR_debug.ipp>
490 #endif // OGLPLUS_LINK_LIBRARY
491 
492 #endif // include guard
const Char * c_str(void) const
Returns the null-terminated c-string.
Definition: ref_tpl.hpp:200
static void PushGroup(DebugSource source, GLuint id, const GLchar(&message)[N])
Pushes a debug group.
Definition: KHR_debug.hpp:303
DebugType type
The type of the debug message.
Definition: KHR_debug.hpp:100
Funcions and classes for handling and wrapping OpenGL extensions.
The DebugSeverity enumeration.
Structure containing data passed to Callback functor.
Definition: KHR_debug.hpp:94
DebugType
Debug output type enumeration.
Definition: type.hpp:27
static void Control(DebugSource source, DebugType type, DebugSeverity severity, const GLuint(&ids)[N], bool enable)
Enables/disables messages with specific parameters.
Definition: KHR_debug.hpp:82
static void Synchronous(bool enable=true)
Enables or disables synchronous debug output.
Definition: KHR_debug.hpp:320
std::function< void(const CallbackData &)> Callback
Type of a callback functor processing debug output.
Definition: KHR_debug.hpp:122
static void PopGroup(void)
Pops a debug group.
Definition: KHR_debug.hpp:313
String type definition and related functions.
DebugSeverity severity
The severity of the debug message.
Definition: KHR_debug.hpp:106
static void Control(DebugSource source, DebugType type, DebugSeverity severity, GLsizei count, const GLuint *ids, bool enable)
Enables/disables messages with specific parameters.
Definition: KHR_debug.hpp:50
KHR_debug_Tree(std::ostream &)
Constructor takes a reference to standard output stream.
::std::basic_string< ALchar > String
Wrapper for the KHR_debug extension.
Definition: KHR_debug.hpp:44
String reference.
DebugSeverity
Debug output severity enumeration.
Definition: severity.hpp:27
static void PushGroup(DebugSource source, GLuint id, GLsizei length, const GLchar *message)
Pushes a debug group.
Definition: KHR_debug.hpp:275
LogSink(Callback callback)
Installs the callback and remembers the previous.
Definition: KHR_debug.hpp:163
std::size_t size(void) const
Return the size (length) string.
Definition: ref_tpl.hpp:149
GLsizei length
The length of th debug message.
Definition: KHR_debug.hpp:109
String const reference wrapper template.
Definition: ref_tpl.hpp:72
static void InsertMessage(DebugSource source, DebugType type, GLuint id, DebugSeverity severity, const GLchar(&message)[N])
Inserts a new message into the debug output.
Definition: KHR_debug.hpp:256
Helper macro for optional checking of availability of GL function.
KHR_debug_ToXML(std::ostream &)
Constructor takes a reference to standard output stream.
Filter for KHR_debug printing a simple tree to a standard output.
Definition: KHR_debug.hpp:433
static void PushGroup(DebugSource source, GLuint id, StrCRef message)
Pushes a debug group.
Definition: KHR_debug.hpp:292
static void Asynchronous(bool enable=true)
Enables or disables asynchronous debug output.
Definition: KHR_debug.hpp:335
KHR_debug_Unique(KHR_debug::Callback)
Construction takes another callback implementation.
The DebugSource enumeration.
The DebugType enumeration.
static void InsertMessage(DebugSource source, DebugType type, GLuint id, DebugSeverity severity, GLint length, const GLchar *buffer)
Inserts a new message into the debug output.
Definition: KHR_debug.hpp:215
static void InsertMessage(DebugSource source, DebugType type, GLuint id, DebugSeverity severity, StrCRef message)
Inserts a new message into the debug output.
Definition: KHR_debug.hpp:236
const GLchar * message
The debug message.
Definition: KHR_debug.hpp:112
~LogSink(void)
Restores the previous callback and its context.
Definition: KHR_debug.hpp:202
GLuint id
The id of the debug message.
Definition: KHR_debug.hpp:103
static void Control(DebugSource source, DebugType type, DebugSeverity severity, bool enable)
Enables/disables messages with specific parameters.
Definition: KHR_debug.hpp:70
Filter for KHR_debug removing duplicate messages.
Definition: KHR_debug.hpp:395
DebugSource
Debug output source enumeration.
Definition: source.hpp:27
DebugSource source
The source of the debug message.
Definition: KHR_debug.hpp:97
Filter for KHR_debug formatting the debug output into XML.
Definition: KHR_debug.hpp:470
Installs a custom callback processing the debug output.
Definition: KHR_debug.hpp:130

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).