BsGLOcclusionQuery.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_CHECK_GL_ERROR();
  15. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Query);
  16. }
  17. GLOcclusionQuery::~GLOcclusionQuery()
  18. {
  19. glDeleteQueries(1, &mQueryObj);
  20. BS_CHECK_GL_ERROR();
  21. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Query);
  22. }
  23. void GLOcclusionQuery::begin(const SPtr<CommandBuffer>& cb)
  24. {
  25. auto execute = [&]()
  26. {
  27. glBeginQuery(mBinary ? GL_ANY_SAMPLES_PASSED : GL_SAMPLES_PASSED, mQueryObj);
  28. BS_CHECK_GL_ERROR();
  29. mNumSamples = 0;
  30. mEndIssued = false;
  31. setActive(true);
  32. };
  33. if (cb == nullptr)
  34. execute();
  35. else
  36. {
  37. SPtr<GLCommandBuffer> glCB = std::static_pointer_cast<GLCommandBuffer>(cb);
  38. glCB->queueCommand(execute);
  39. }
  40. }
  41. void GLOcclusionQuery::end(const SPtr<CommandBuffer>& cb)
  42. {
  43. auto execute = [&]()
  44. {
  45. glEndQuery(mBinary ? GL_ANY_SAMPLES_PASSED : GL_SAMPLES_PASSED);
  46. BS_CHECK_GL_ERROR();
  47. mEndIssued = true;
  48. mFinalized = false;
  49. };
  50. if (cb == nullptr)
  51. execute();
  52. else
  53. {
  54. SPtr<GLCommandBuffer> glCB = std::static_pointer_cast<GLCommandBuffer>(cb);
  55. glCB->queueCommand(execute);
  56. }
  57. }
  58. bool GLOcclusionQuery::isReady() const
  59. {
  60. if (!mEndIssued)
  61. return false;
  62. GLint done = 0;
  63. glGetQueryObjectiv(mQueryObj, GL_QUERY_RESULT_AVAILABLE, &done);
  64. BS_CHECK_GL_ERROR();
  65. return done == GL_TRUE;
  66. }
  67. UINT32 GLOcclusionQuery::getNumSamples()
  68. {
  69. if (!mFinalized && isReady())
  70. {
  71. finalize();
  72. }
  73. return mNumSamples;
  74. }
  75. void GLOcclusionQuery::finalize()
  76. {
  77. mFinalized = true;
  78. if (mBinary)
  79. {
  80. GLboolean anyPassed = GL_FALSE;
  81. glGetQueryObjectuiv(mQueryObj, GL_QUERY_RESULT, (GLuint*)&anyPassed);
  82. BS_CHECK_GL_ERROR();
  83. mNumSamples = anyPassed == GL_TRUE ? 1 : 0;
  84. }
  85. else
  86. {
  87. GLuint numSamples = 0;
  88. glGetQueryObjectuiv(mQueryObj, GL_QUERY_RESULT, (GLuint*)&numSamples);
  89. BS_CHECK_GL_ERROR();
  90. mNumSamples = (UINT32)numSamples;
  91. }
  92. }
  93. }}