TimeQuery.cpp 1.2 KB

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