2
0

BsGLTimerQuery.cpp 1.4 KB

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