OGLplus (0.52.0) a C++ wrapper for OpenGL

query.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_QUERY_1107121519_HPP
14 #define OGLPLUS_QUERY_1107121519_HPP
15 
16 #include <oglplus/glfunc.hpp>
19 #include <oglplus/error/object.hpp>
20 #include <oglplus/enumerations.hpp>
21 #include <cassert>
22 
23 namespace oglplus {
24 
26 
29 OGLPLUS_ENUM_CLASS_BEGIN(QueryTarget, GLenum)
30 #include <oglplus/enums/query_target.ipp>
31 OGLPLUS_ENUM_CLASS_END(QueryTarget)
32 
33 #if !OGLPLUS_NO_ENUM_VALUE_NAMES
34 #include <oglplus/enums/query_target_names.ipp>
35 #endif
36 
37 #if !OGLPLUS_ENUM_VALUE_RANGES
38 #include <oglplus/enums/query_target_range.ipp>
39 #endif
40 
42 
45 OGLPLUS_ENUM_CLASS_BEGIN(ConditionalRenderMode, GLenum)
46 #include <oglplus/enums/conditional_render_mode.ipp>
47 OGLPLUS_ENUM_CLASS_END(ConditionalRenderMode)
48 
49 #if !OGLPLUS_NO_ENUM_VALUE_NAMES
50 #include <oglplus/enums/conditional_render_mode_names.ipp>
51 #endif
52 
53 #if !OGLPLUS_ENUM_VALUE_RANGES
54 #include <oglplus/enums/conditional_render_mode_range.ipp>
55 #endif
56 
58 
65 template <>
66 class ObjGenDelOps<tag::Query>
67 {
68 protected:
69  static void Gen(tag::Generate, GLsizei count, GLuint* names)
70  {
71  assert(names != nullptr);
72  OGLPLUS_GLFUNC(GenQueries)(count, names);
73  OGLPLUS_CHECK_SIMPLE(GenQueries);
74  }
75 #if GL_VERSION_4_5 || GL_ARB_direct_state_access
76  static void Gen(
77  tag::Create,
78  GLenum target,
79  GLsizei count,
80  GLuint* names
81  )
82  {
83  assert(names != nullptr);
84  OGLPLUS_GLFUNC(CreateQueries)(target, count, names);
85  OGLPLUS_CHECK_SIMPLE(CreateQueries);
86  }
87 
88  GLenum _type;
89 
90  void Gen(tag::Create create, GLsizei count, GLuint* names)
91  {
92  Gen(create, _type, count, names);
93  }
94 #endif
95 
96  static void Delete(GLsizei count, GLuint* names)
97  {
98  assert(names != nullptr);
99  OGLPLUS_GLFUNC(DeleteQueries)(count, names);
100  OGLPLUS_VERIFY_SIMPLE(DeleteQueries);
101  }
102 
103  static GLboolean IsA(GLuint name)
104  {
105  assert(name != 0);
106  GLboolean result = OGLPLUS_GLFUNC(IsQuery)(name);
107  OGLPLUS_VERIFY_SIMPLE(IsQuery);
108  return result;
109  }
110 };
111 
112 template <>
113 struct ObjectSubtype<tag::Query>
114 {
115  typedef QueryTarget Type;
116 };
117 
118 class QueryActivator;
119 
120 template <typename ResultType>
122 
124 
126 template <>
127 class ObjectOps<tag::DirectState, tag::Query>
128  : public ObjZeroOps<tag::DirectState, tag::Query>
129 {
130 protected:
131  ObjectOps(void){ }
132 public:
135 
137 
141  void Begin(Target target)
142  {
143  assert(_name != 0);
144  OGLPLUS_GLFUNC(BeginQuery)(GLenum(target), _name);
145  OGLPLUS_VERIFY(
146  BeginQuery,
147  ObjectError,
148  Object(*this).
149  EnumParam(target)
150  );
151  }
152 
154 
158  void End(Target target)
159  {
160  assert(_name != 0);
161  OGLPLUS_GLFUNC(EndQuery)(GLenum(target));
162  OGLPLUS_VERIFY(
163  EndQuery,
164  ObjectError,
165  Object(*this).
166  EnumParam(target)
167  );
168  }
169 
170 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_0
171 
177  {
178  assert(_name != 0);
179  OGLPLUS_GLFUNC(BeginConditionalRender)(_name, GLenum(mode));
180  OGLPLUS_VERIFY(
181  BeginConditionalRender,
182  ObjectError,
183  Object(*this).
184  EnumParam(mode)
185  );
186  }
187 
189 
193  static void EndConditionalRender(void)
194  {
195  OGLPLUS_GLFUNC(EndConditionalRender)();
196  OGLPLUS_VERIFY_SIMPLE(EndConditionalRender);
197  }
198 #endif
199 
200 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_3 || GL_ARB_timer_query
201 
206  void Counter(Target target)
207  {
208  assert(_name != 0);
209  OGLPLUS_GLFUNC(QueryCounter)(_name, GLenum(target));
210  OGLPLUS_VERIFY(
211  QueryCounter,
212  ObjectError,
213  Object(*this).
214  EnumParam(target)
215  );
216  }
217 
219 
224  void Timestamp(void)
225  {
226  Counter(Target::Timestamp);
227  }
228 #endif
229 
231 
236  bool ResultAvailable(void) const
237  {
238  assert(_name != 0);
239  GLuint result = GL_FALSE;
240  OGLPLUS_GLFUNC(GetQueryObjectuiv)(
241  _name,
242  GL_QUERY_RESULT_AVAILABLE,
243  &result
244  );
245  OGLPLUS_VERIFY(
246  GetQueryObjectuiv,
247  ObjectError,
248  Object(*this)
249  );
250  return result == GL_TRUE;
251  }
252 
253 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_0
254 
260  void Result(GLint& result) const
261  {
262  assert(_name != 0);
263  OGLPLUS_GLFUNC(GetQueryObjectiv)(
264  _name,
265  GL_QUERY_RESULT,
266  &result
267  );
268  OGLPLUS_VERIFY(
269  GetQueryObjectiv,
270  ObjectError,
271  Object(*this)
272  );
273  }
274 #endif // GL_VERSION_3_0
275 
277 
282  void Result(GLuint& result) const
283  {
284  assert(_name != 0);
285  OGLPLUS_GLFUNC(GetQueryObjectuiv)(
286  _name,
287  GL_QUERY_RESULT,
288  &result
289  );
290  OGLPLUS_VERIFY(
291  GetQueryObjectuiv,
292  ObjectError,
293  Object(*this)
294  );
295  }
296 
297 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_3 || GL_ARB_timer_query
298 
305  void Result(GLint64& result) const
306  {
307  assert(_name != 0);
308  OGLPLUS_GLFUNC(GetQueryObjecti64v)(
309  _name,
310  GL_QUERY_RESULT,
311  &result
312  );
313  OGLPLUS_VERIFY(
314  GetQueryObjecti64v,
315  ObjectError,
316  Object(*this)
317  );
318  }
319 
321 
327  void Result(GLuint64& result) const
328  {
329  assert(_name != 0);
330  OGLPLUS_GLFUNC(GetQueryObjectui64v)(
331  _name,
332  GL_QUERY_RESULT,
333  &result
334  );
335  OGLPLUS_VERIFY(
336  GetQueryObjectui64v,
337  ObjectError,
338  Object(*this)
339  );
340  }
341 #endif // timer query
342 
344  template <typename ResultType>
345  void WaitForResult(ResultType& result) const
346  {
347  while(!ResultAvailable())
348  OGLPLUS_GLFUNC(Finish)();
349  Result(result);
350  }
351 
354 
355  Activator Activate(Target target);
356 
358 
362  template <typename ResultType>
364  Execute(Target target, ResultType& result);
365 };
366 
370 
372 
376 {
377 protected:
378  QueryName _query;
379  QueryTarget _target;
380 private:
381  bool _alive;
383 public:
386  QueryName query,
387  QueryTarget target
388  ): _query(query)
389  , _target(target)
390  , _alive(false)
391  {
392  Reference<QueryOps>(_query).Begin(_target);
393  _alive = true;
394  }
395 
398  : _query(std::move(temp._query))
399  , _target(temp._target)
400  , _alive(temp._alive)
401  {
402  temp._alive = false;
403  }
404 
407  {
408  try { Finish(); }
409  catch(...) { }
410  }
411 
413  bool Finish(void)
414  {
415  if(_alive)
416  {
417  Reference<QueryOps>(_query).End(_target);
418  _alive = false;
419  return true;
420  }
421  else return false;
422  }
423 };
424 
425 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_3_0
426 
431 {
432 private:
433  bool _alive;
435 public:
438  QueryName query,
440  ): _alive(false)
441  {
442  Reference<QueryOps>(query).BeginConditionalRender(mode);
443  _alive = true;
444  }
445 
448  : _alive(temp._alive)
449  {
450  temp._alive = false;
451  }
452 
455  {
456  try { Finish(); }
457  catch(...) { }
458  }
459 
461  bool Finish(void)
462  {
463  if(_alive)
464  {
466  _alive = false;
467  return true;
468  }
469  else return false;
470  }
471 };
472 #endif
473 
475 
482 template <typename ResultType>
483 class QueryExecution : public QueryActivator
484 {
485 private:
486  ResultType& _result;
487 public:
488  QueryExecution(
489  QueryName query,
490  QueryTarget target,
491  ResultType& result
492  ): QueryActivator(query, target)
493  , _result(result)
494  { }
495 
496  QueryExecution(QueryExecution&& temp)
497  : QueryActivator(static_cast<QueryActivator&&>(temp))
498  , _result(temp._result)
499  { }
500 
501  ~QueryExecution(void)
502  {
503  try { WaitForResult(); }
504  catch(...) { }
505  }
506 
507  void WaitForResult(void)
508  {
509  if(this->Finish())
510  {
511  Reference<QueryOps>(this->_query).WaitForResult(_result);
512  }
513  }
514 };
515 
516 inline
517 QueryActivator
518 ObjectOps<tag::DirectState, tag::Query>::
519 Activate(Target target)
520 {
521  return QueryActivator(*this, target);
522 }
523 
524 template <typename ResultType>
525 inline
526 QueryExecution<ResultType>
528 Execute(QueryTarget target, ResultType& result)
529 {
530  return QueryExecution<ResultType>(*this, target, result);
531 }
532 
534 
537 typedef Object<QueryOps> Query;
538 
539 } // namespace oglplus
540 
541 #endif // include guard
void WaitForResult(ResultType &result) const
Waits for and queries the result.
Definition: query.hpp:345
QueryExecution< ResultType > Execute(Target target, ResultType &result)
Executes this query on the specified target and gets the result.
Definition: query.hpp:528
RAII conditional render activator/deactivator.
Definition: query.hpp:430
TIMESTAMP: Current timestamp query.
ConditionalRender(ConditionalRender &&temp)
ConditionalRenders are moveable.
Definition: query.hpp:447
void Counter(Target target)
Do a counter query on the specified target.
Definition: query.hpp:206
Reference to an existing Object.
QueryActivator Activator
The activator class.
Definition: query.hpp:353
ConditionalRenderMode
Conditional render modes.
Definition: query.hpp:45
QueryActivator(QueryName query, QueryTarget target)
Begins a query on the specified target.
Definition: query.hpp:385
Class wrapping query functions with (direct state access)
Definition: query.hpp:127
void Timestamp(void)
Do a timestamp query.
Definition: query.hpp:224
Declaration of OGLplus object-related error.
Generic OpenGL object wrapper.
Enumeration-related declarations.
ConditionalRender(QueryName query, ConditionalRenderMode mode)
Begins conditional render on query in the specified mode.
Definition: query.hpp:437
~ConditionalRender(void)
Ends the conditional render.
Definition: query.hpp:454
Implements operations applicable to named (non-zero) objects.
Definition: wrap_tpl.hpp:45
void Result(GLint64 &result) const
Get the query result.
Definition: query.hpp:305
QueryActivator(QueryActivator &&temp)
Activators are moveable.
Definition: query.hpp:397
void End(Target target)
End the query on the specified target.
Definition: query.hpp:158
void Begin(Target target)
Begin the query on the specified target.
Definition: query.hpp:141
RAII Query activator/deactivator.
Definition: query.hpp:375
Helper macro for optional checking of availability of GL function.
Implements operations applicable to any object including object 0 (zero)
Definition: fwd.hpp:157
Object< QueryOps > Query
An oglplus_object encapsulating the asynchronous query functionality.
Definition: query.hpp:537
void BeginConditionalRender(ConditionalRenderMode mode)
Begin conditional render on the query in the specified mode.
Definition: query.hpp:176
static void EndConditionalRender(void)
Ends currently active conditional render.
Definition: query.hpp:193
void Result(GLint &result) const
Get the query result.
Definition: query.hpp:260
A helper class automatically executing a query.
Definition: query.hpp:121
Exception class for GL object-related errors.
Definition: object.hpp:24
QueryTarget
Query bind target.
Definition: query.hpp:29
ObjectOps< tag::DirectState, tag::Query > QueryOps
Query operations with direct state access.
Definition: query.hpp:369
void Result(GLuint &result) const
Get the query result.
Definition: query.hpp:282
~QueryActivator(void)
Ends the query.
Definition: query.hpp:406
bool Finish(void)
Explicitly ends the query.
Definition: query.hpp:413
bool ResultAvailable(void) const
Returns true if the query result is available.
Definition: query.hpp:236
Allows to make managed copies of instances of Object.
Definition: fwd.hpp:172
bool Finish(void)
Explicitly ends the conditional render.
Definition: query.hpp:461
void Result(GLuint64 &result) const
Get the query result.
Definition: query.hpp:327
QueryTarget Target
Query execution target.
Definition: query.hpp:134

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).