gfxGLOcclusionQuery.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/gl/gfxGLOcclusionQuery.h"
  24. #include "gfx/gl/tGL/tGL.h"
  25. GFXGLOcclusionQuery::GFXGLOcclusionQuery(GFXDevice* device) :
  26. GFXOcclusionQuery(device),
  27. mQuery(-1),
  28. mTesting(false)
  29. {
  30. }
  31. GFXGLOcclusionQuery::~GFXGLOcclusionQuery()
  32. {
  33. glDeleteQueries(1, &mQuery);
  34. }
  35. bool GFXGLOcclusionQuery::begin()
  36. {
  37. if (GFXDevice::getDisableOcclusionQuery())
  38. return true;
  39. if (!glIsQuery(mQuery))
  40. glGenQueries(1, &mQuery);
  41. if (!mTesting)
  42. {
  43. glBeginQuery(GL_SAMPLES_PASSED, mQuery);
  44. mTesting = true;
  45. }
  46. return true;
  47. }
  48. void GFXGLOcclusionQuery::end()
  49. {
  50. if (GFXDevice::getDisableOcclusionQuery())
  51. return;
  52. if (!glIsQuery(mQuery))
  53. return;
  54. glEndQuery(GL_SAMPLES_PASSED);
  55. mTesting = false;
  56. }
  57. GFXOcclusionQuery::OcclusionQueryStatus GFXGLOcclusionQuery::getStatus(bool block, U32* data)
  58. {
  59. // If this ever shows up near the top of a profile
  60. // then your system is GPU bound.
  61. PROFILE_SCOPE(GFXGLOcclusionQuery_getStatus);
  62. if (GFXDevice::getDisableOcclusionQuery())
  63. return NotOccluded;
  64. if (!glIsQuery(mQuery))
  65. return NotOccluded;
  66. GLint numPixels = 0;
  67. GLint queryDone = false;
  68. if (block)
  69. {
  70. while (!queryDone)
  71. {
  72. //If we're stalled out, proceed with worst-case scenario -BJR
  73. if (GFX->mFrameTime->getElapsedMs()>4)
  74. {
  75. this->begin();
  76. this->end();
  77. return NotOccluded;
  78. }
  79. glGetQueryObjectiv(mQuery, GL_QUERY_RESULT_AVAILABLE, &queryDone);
  80. }
  81. }
  82. else
  83. {
  84. glGetQueryObjectiv(mQuery, GL_QUERY_RESULT_AVAILABLE, &queryDone);
  85. }
  86. if (queryDone)
  87. glGetQueryObjectiv(mQuery, GL_QUERY_RESULT, &numPixels);
  88. else
  89. return Waiting;
  90. if (data)
  91. *data = numPixels;
  92. return numPixels > 0 ? NotOccluded : Occluded;
  93. }
  94. void GFXGLOcclusionQuery::zombify()
  95. {
  96. glDeleteQueries(1, &mQuery);
  97. mQuery = 0;
  98. }
  99. void GFXGLOcclusionQuery::resurrect()
  100. {
  101. glGenQueries(1, &mQuery);
  102. }
  103. const String GFXGLOcclusionQuery::describeSelf() const
  104. {
  105. // We've got nothing
  106. return String();
  107. }