BsGLTimerQuery.cpp 1.8 KB

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