TimeQuery.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "anki/gl/TimeQuery.h"
  2. #include "anki/util/Assert.h"
  3. #include "anki/gl/GlException.h"
  4. namespace anki
  5. {
  6. //==============================================================================
  7. TimeQuery::TimeQuery()
  8. {
  9. glGenQueries(2, &glIds[0]);
  10. state = S_CREATED;
  11. }
  12. //==============================================================================
  13. TimeQuery::~TimeQuery()
  14. {
  15. glDeleteQueries(2, &glIds[0]);
  16. }
  17. //==============================================================================
  18. void TimeQuery::begin()
  19. {
  20. ANKI_ASSERT(state == S_CREATED || state == S_ENDED);
  21. glQueryCounter(glIds[0], GL_TIMESTAMP);
  22. state = S_STARTED;
  23. }
  24. //==============================================================================
  25. double TimeQuery::end()
  26. {
  27. ANKI_ASSERT(state == S_STARTED);
  28. glQueryCounter(glIds[1], GL_TIMESTAMP);
  29. // Wait
  30. GLint done = 0;
  31. while(!done)
  32. {
  33. /// XXX BAD BAD BAD BAD!!!!!
  34. glGetQueryObjectiv(glIds[1], GL_QUERY_RESULT_AVAILABLE, &done);
  35. }
  36. // Get elapsed time
  37. GLuint64 timerStart, timerEnd;
  38. glGetQueryObjectui64v(glIds[0], GL_QUERY_RESULT, &timerStart);
  39. glGetQueryObjectui64v(glIds[1], GL_QUERY_RESULT, &timerEnd);
  40. state = S_ENDED;
  41. return (timerEnd - timerStart) / 1000000000.0;
  42. }
  43. } // end namespace