GlQuery.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "anki/gl/Query.h"
  2. #include "anki/util/Exception.h"
  3. #include "anki/util/Assert.h"
  4. #include <GL/glew.h>
  5. namespace anki {
  6. //==============================================================================
  7. // QueryImpl =
  8. //==============================================================================
  9. /// Query implementation
  10. struct QueryImpl
  11. {
  12. GLuint glId;
  13. GLenum question;
  14. };
  15. //==============================================================================
  16. // Query =
  17. //==============================================================================
  18. //==============================================================================
  19. Query::Query(QueryQuestion q)
  20. {
  21. impl.reset(new QueryImpl);
  22. // question
  23. switch(q)
  24. {
  25. case QQ_SAMPLES_PASSED:
  26. impl->question = GL_SAMPLES_PASSED;
  27. break;
  28. case QQ_ANY_SAMPLES_PASSED:
  29. impl->question = GL_ANY_SAMPLES_PASSED;
  30. break;
  31. case QQ_TIME_ELAPSED:
  32. impl->question = GL_TIME_ELAPSED;
  33. break;
  34. default:
  35. ANKI_ASSERT(0);
  36. };
  37. // glId
  38. glGenQueries(1, &impl->glId);
  39. if(impl->glId == 0)
  40. {
  41. throw ANKI_EXCEPTION("Query generation failed");
  42. }
  43. }
  44. //==============================================================================
  45. Query::~Query()
  46. {
  47. glDeleteQueries(1, &impl->glId);
  48. }
  49. //==============================================================================
  50. void Query::beginQuery()
  51. {
  52. glBeginQuery(impl->question, impl->glId);
  53. }
  54. //==============================================================================
  55. void Query::endQuery()
  56. {
  57. glEndQuery(impl->question);
  58. }
  59. //==============================================================================
  60. uint64_t Query::getResult()
  61. {
  62. GLuint64 result;
  63. glGetQueryObjectui64v(impl->glId, GL_QUERY_RESULT, &result);
  64. return result;
  65. }
  66. //==============================================================================
  67. uint64_t Query::getResultNoWait(bool& finished)
  68. {
  69. GLuint resi;
  70. glGetQueryObjectuiv(impl->glId, GL_QUERY_RESULT_AVAILABLE, &resi);
  71. GLuint64 result;
  72. if(resi)
  73. {
  74. glGetQueryObjectui64v(impl->glId, GL_QUERY_RESULT, &result);
  75. finished = true;
  76. }
  77. else
  78. {
  79. finished = false;
  80. result = 0;
  81. }
  82. return result;
  83. }
  84. } // end namespace anki