BsGLTimerQuery.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(UINT32 deviceIdx)
  9. :mQueryStartObj(0), mQueryEndObj(0), mFinalized(false), mEndIssued(false), mTimeDelta(0.0f)
  10. {
  11. assert(deviceIdx == 0 && "Multiple GPUs not supported natively on OpenGL.");
  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. }