BsGLTimerQuery.cpp 1.7 KB

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