CmGLTimerQuery.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "CmGLTimerQuery.h"
  2. #include "CmMath.h"
  3. namespace CamelotFramework
  4. {
  5. GLTimerQuery::GLTimerQuery()
  6. :mQueryStartObj(0), mQueryEndObj(0),
  7. mTimeDelta(0.0f), mFinalized(false)
  8. {
  9. GLuint queries[2];
  10. glGenQueries(2, queries);
  11. mQueryStartObj = queries[0];
  12. mQueryEndObj = queries[1];
  13. }
  14. GLTimerQuery::~GLTimerQuery()
  15. {
  16. GLuint queries[2];
  17. queries[0] = mQueryStartObj;
  18. queries[1] = mQueryEndObj;
  19. glDeleteQueries(2, queries);
  20. }
  21. void GLTimerQuery::begin()
  22. {
  23. glQueryCounter(mQueryStartObj, GL_TIMESTAMP);
  24. setActive(true);
  25. }
  26. void GLTimerQuery::end()
  27. {
  28. glQueryCounter(mQueryEndObj, GL_TIMESTAMP);
  29. }
  30. bool GLTimerQuery::isReady() const
  31. {
  32. GLint done = 0;
  33. glGetQueryObjectiv(mQueryEndObj, GL_QUERY_RESULT_AVAILABLE, &done);
  34. return done == GL_TRUE;
  35. }
  36. float GLTimerQuery::getTimeMs()
  37. {
  38. if(!mFinalized && isReady())
  39. {
  40. finalize();
  41. }
  42. return mTimeDelta;
  43. }
  44. void GLTimerQuery::finalize()
  45. {
  46. mFinalized = true;
  47. GLuint64 timeStart;
  48. GLuint64 timeEnd;
  49. glGetQueryObjectui64v(mQueryStartObj, GL_QUERY_RESULT, &timeStart);
  50. glGetQueryObjectui64v(mQueryEndObj, GL_QUERY_RESULT, &timeEnd);
  51. mTimeDelta = (timeEnd - timeStart) / 1000000.0f;
  52. }
  53. }