renderer_scene_occlusion_cull.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**************************************************************************/
  2. /* renderer_scene_occlusion_cull.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "renderer_scene_occlusion_cull.h"
  31. RendererSceneOcclusionCull *RendererSceneOcclusionCull::singleton = nullptr;
  32. const Vector3 RendererSceneOcclusionCull::HZBuffer::corners[8] = {
  33. Vector3(0, 0, 0),
  34. Vector3(0, 0, 1),
  35. Vector3(0, 1, 0),
  36. Vector3(0, 1, 1),
  37. Vector3(1, 0, 0),
  38. Vector3(1, 0, 1),
  39. Vector3(1, 1, 0),
  40. Vector3(1, 1, 1)
  41. };
  42. bool RendererSceneOcclusionCull::HZBuffer::occlusion_jitter_enabled = false;
  43. bool RendererSceneOcclusionCull::HZBuffer::is_empty() const {
  44. return sizes.is_empty();
  45. }
  46. void RendererSceneOcclusionCull::HZBuffer::clear() {
  47. if (sizes.is_empty()) {
  48. return; // Already cleared
  49. }
  50. data.clear();
  51. sizes.clear();
  52. mips.clear();
  53. debug_data.clear();
  54. if (debug_image.is_valid()) {
  55. debug_image.unref();
  56. }
  57. ERR_FAIL_NULL(RenderingServer::get_singleton());
  58. RS::get_singleton()->free(debug_texture);
  59. }
  60. void RendererSceneOcclusionCull::HZBuffer::resize(const Size2i &p_size) {
  61. occlusion_buffer_size = p_size;
  62. if (p_size == Size2i()) {
  63. clear();
  64. return;
  65. }
  66. if (!sizes.is_empty() && p_size == sizes[0]) {
  67. return; // Size didn't change
  68. }
  69. int mip_count = 0;
  70. int data_size = 0;
  71. int w = p_size.x;
  72. int h = p_size.y;
  73. while (true) {
  74. data_size += h * w;
  75. w = MAX(1, w >> 1);
  76. h = MAX(1, h >> 1);
  77. mip_count++;
  78. if (w == 1U && h == 1U) {
  79. data_size += 1U;
  80. mip_count++;
  81. break;
  82. }
  83. }
  84. data.resize(data_size);
  85. mips.resize(mip_count);
  86. sizes.resize(mip_count);
  87. w = p_size.x;
  88. h = p_size.y;
  89. float *ptr = data.ptr();
  90. for (int i = 0; i < mip_count; i++) {
  91. sizes[i] = Size2i(w, h);
  92. mips[i] = ptr;
  93. ptr = &ptr[w * h];
  94. w = MAX(1, w >> 1);
  95. h = MAX(1, h >> 1);
  96. }
  97. for (int i = 0; i < data_size; i++) {
  98. data[i] = FLT_MAX;
  99. }
  100. debug_data.resize(sizes[0].x * sizes[0].y);
  101. if (debug_texture.is_valid()) {
  102. RS::get_singleton()->free(debug_texture);
  103. debug_texture = RID();
  104. }
  105. }
  106. void RendererSceneOcclusionCull::HZBuffer::update_mips() {
  107. // Keep this up to date as a local to be used for occlusion timers.
  108. occlusion_frame = Engine::get_singleton()->get_frames_drawn();
  109. if (sizes.is_empty()) {
  110. return;
  111. }
  112. for (uint32_t mip = 1; mip < mips.size(); mip++) {
  113. for (int y = 0; y < sizes[mip].y; y++) {
  114. for (int x = 0; x < sizes[mip].x; x++) {
  115. int prev_x = x * 2;
  116. int prev_y = y * 2;
  117. int prev_w = sizes[mip - 1].width;
  118. int prev_h = sizes[mip - 1].height;
  119. bool odd_w = (prev_w % 2) != 0;
  120. bool odd_h = (prev_h % 2) != 0;
  121. #define CHECK_OFFSET(xx, yy) max_depth = MAX(max_depth, mips[mip - 1][MIN(prev_h - 1, prev_y + (yy)) * prev_w + MIN(prev_w - 1, prev_x + (xx))])
  122. float max_depth = mips[mip - 1][prev_y * sizes[mip - 1].x + prev_x];
  123. CHECK_OFFSET(0, 1);
  124. CHECK_OFFSET(1, 0);
  125. CHECK_OFFSET(1, 1);
  126. if (odd_w) {
  127. CHECK_OFFSET(2, 0);
  128. CHECK_OFFSET(2, 1);
  129. }
  130. if (odd_h) {
  131. CHECK_OFFSET(0, 2);
  132. CHECK_OFFSET(1, 2);
  133. }
  134. if (odd_w && odd_h) {
  135. CHECK_OFFSET(2, 2);
  136. }
  137. mips[mip][y * sizes[mip].x + x] = max_depth;
  138. #undef CHECK_OFFSET
  139. }
  140. }
  141. }
  142. }
  143. RID RendererSceneOcclusionCull::HZBuffer::get_debug_texture() {
  144. if (sizes.is_empty() || sizes[0] == Size2i()) {
  145. return RID();
  146. }
  147. if (debug_image.is_null()) {
  148. debug_image.instantiate();
  149. }
  150. unsigned char *ptrw = debug_data.ptrw();
  151. for (int i = 0; i < debug_data.size(); i++) {
  152. ptrw[i] = MIN(mips[0][i] / debug_tex_range, 1.0) * 255;
  153. }
  154. debug_image->set_data(sizes[0].x, sizes[0].y, false, Image::FORMAT_L8, debug_data);
  155. if (debug_texture.is_null()) {
  156. debug_texture = RS::get_singleton()->texture_2d_create(debug_image);
  157. } else {
  158. RenderingServer::get_singleton()->texture_2d_update(debug_texture, debug_image);
  159. }
  160. return debug_texture;
  161. }