BsGLTimerQuery.cpp 1.7 KB

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