TimeQuery.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /// XXX BAD BAD BAD BAD!!!!!
  33. glGetQueryObjectiv(glIds[1], GL_QUERY_RESULT_AVAILABLE, &done);
  34. }
  35. // Get elapsed time
  36. GLuint64 timerStart, timerEnd;
  37. glGetQueryObjectui64v(glIds[0], GL_QUERY_RESULT, &timerStart);
  38. glGetQueryObjectui64v(glIds[1], GL_QUERY_RESULT, &timerEnd);
  39. state = S_ENDED;
  40. return (timerEnd - timerStart) / 1000000000.0;
  41. }
  42. } // end namespace