BsGLOcclusionQuery.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLOcclusionQuery.h"
  4. #include "BsGLCommandBuffer.h"
  5. #include "Math/BsMath.h"
  6. #include "Profiling/BsRenderStats.h"
  7. namespace bs { namespace ct
  8. {
  9. GLOcclusionQuery::GLOcclusionQuery(bool binary, UINT32 deviceIdx)
  10. :OcclusionQuery(binary), mQueryObj(0), mFinalized(false), mEndIssued(false), mNumSamples(0)
  11. {
  12. assert(deviceIdx == 0 && "Multiple GPUs not supported natively on OpenGL.");
  13. glGenQueries(1, &mQueryObj);
  14. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Query);
  15. }
  16. GLOcclusionQuery::~GLOcclusionQuery()
  17. {
  18. glDeleteQueries(1, &mQueryObj);
  19. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Query);
  20. }
  21. void GLOcclusionQuery::begin(const SPtr<CommandBuffer>& cb)
  22. {
  23. auto execute = [&]()
  24. {
  25. glBeginQuery(mBinary ? GL_ANY_SAMPLES_PASSED : GL_SAMPLES_PASSED, mQueryObj);
  26. mNumSamples = 0;
  27. mEndIssued = false;
  28. setActive(true);
  29. };
  30. if (cb == nullptr)
  31. execute();
  32. else
  33. {
  34. SPtr<GLCommandBuffer> glCB = std::static_pointer_cast<GLCommandBuffer>(cb);
  35. glCB->queueCommand(execute);
  36. }
  37. }
  38. void GLOcclusionQuery::end(const SPtr<CommandBuffer>& cb)
  39. {
  40. auto execute = [&]()
  41. {
  42. glEndQuery(mBinary ? GL_ANY_SAMPLES_PASSED : GL_SAMPLES_PASSED);
  43. mEndIssued = true;
  44. mFinalized = false;
  45. };
  46. if (cb == nullptr)
  47. execute();
  48. else
  49. {
  50. SPtr<GLCommandBuffer> glCB = std::static_pointer_cast<GLCommandBuffer>(cb);
  51. glCB->queueCommand(execute);
  52. }
  53. }
  54. bool GLOcclusionQuery::isReady() const
  55. {
  56. if (!mEndIssued)
  57. return false;
  58. GLint done = 0;
  59. glGetQueryObjectiv(mQueryObj, GL_QUERY_RESULT_AVAILABLE, &done);
  60. return done == GL_TRUE;
  61. }
  62. UINT32 GLOcclusionQuery::getNumSamples()
  63. {
  64. if (!mFinalized && isReady())
  65. {
  66. finalize();
  67. }
  68. return mNumSamples;
  69. }
  70. void GLOcclusionQuery::finalize()
  71. {
  72. mFinalized = true;
  73. if (mBinary)
  74. {
  75. GLboolean anyPassed = GL_FALSE;
  76. glGetQueryObjectuiv(mQueryObj, GL_QUERY_RESULT_ARB, (GLuint*)&anyPassed);
  77. mNumSamples = anyPassed == GL_TRUE ? 1 : 0;
  78. }
  79. else
  80. {
  81. GLuint numSamples = 0;
  82. glGetQueryObjectuiv(mQueryObj, GL_QUERY_RESULT_ARB, (GLuint*)&numSamples);
  83. mNumSamples = (UINT32)numSamples;
  84. }
  85. }
  86. }}