renderer_scene_occlusion_cull.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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::is_empty() const {
  43. return sizes.is_empty();
  44. }
  45. void RendererSceneOcclusionCull::HZBuffer::clear() {
  46. if (sizes.is_empty()) {
  47. return; // Already cleared
  48. }
  49. data.clear();
  50. sizes.clear();
  51. mips.clear();
  52. debug_data.clear();
  53. if (debug_image.is_valid()) {
  54. debug_image.unref();
  55. }
  56. RS::get_singleton()->free(debug_texture);
  57. }
  58. void RendererSceneOcclusionCull::HZBuffer::resize(const Size2i &p_size) {
  59. if (p_size == Size2i()) {
  60. clear();
  61. return;
  62. }
  63. if (!sizes.is_empty() && p_size == sizes[0]) {
  64. return; // Size didn't change
  65. }
  66. int mip_count = 0;
  67. int data_size = 0;
  68. int w = p_size.x;
  69. int h = p_size.y;
  70. while (true) {
  71. data_size += h * w;
  72. w = MAX(1, w >> 1);
  73. h = MAX(1, h >> 1);
  74. mip_count++;
  75. if (w == 1U && h == 1U) {
  76. data_size += 1U;
  77. mip_count++;
  78. break;
  79. }
  80. }
  81. data.resize(data_size);
  82. mips.resize(mip_count);
  83. sizes.resize(mip_count);
  84. w = p_size.x;
  85. h = p_size.y;
  86. float *ptr = data.ptr();
  87. for (int i = 0; i < mip_count; i++) {
  88. sizes[i] = Size2i(w, h);
  89. mips[i] = ptr;
  90. ptr = &ptr[w * h];
  91. w = MAX(1, w >> 1);
  92. h = MAX(1, h >> 1);
  93. }
  94. for (int i = 0; i < data_size; i++) {
  95. data[i] = FLT_MAX;
  96. }
  97. debug_data.resize(sizes[0].x * sizes[0].y);
  98. if (debug_texture.is_valid()) {
  99. RS::get_singleton()->free(debug_texture);
  100. debug_texture = RID();
  101. }
  102. }
  103. void RendererSceneOcclusionCull::HZBuffer::update_mips() {
  104. if (sizes.is_empty()) {
  105. return;
  106. }
  107. for (uint32_t mip = 1; mip < mips.size(); mip++) {
  108. for (int y = 0; y < sizes[mip].y; y++) {
  109. for (int x = 0; x < sizes[mip].x; x++) {
  110. int prev_x = x * 2;
  111. int prev_y = y * 2;
  112. int prev_w = sizes[mip - 1].width;
  113. int prev_h = sizes[mip - 1].height;
  114. bool odd_w = (prev_w % 2) != 0;
  115. bool odd_h = (prev_h % 2) != 0;
  116. #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))])
  117. float max_depth = mips[mip - 1][prev_y * sizes[mip - 1].x + prev_x];
  118. CHECK_OFFSET(0, 1);
  119. CHECK_OFFSET(1, 0);
  120. CHECK_OFFSET(1, 1);
  121. if (odd_w) {
  122. CHECK_OFFSET(2, 0);
  123. CHECK_OFFSET(2, 1);
  124. }
  125. if (odd_h) {
  126. CHECK_OFFSET(0, 2);
  127. CHECK_OFFSET(1, 2);
  128. }
  129. if (odd_w && odd_h) {
  130. CHECK_OFFSET(2, 2);
  131. }
  132. mips[mip][y * sizes[mip].x + x] = max_depth;
  133. #undef CHECK_OFFSET
  134. }
  135. }
  136. }
  137. }
  138. RID RendererSceneOcclusionCull::HZBuffer::get_debug_texture() {
  139. if (sizes.is_empty() || sizes[0] == Size2i()) {
  140. return RID();
  141. }
  142. if (debug_image.is_null()) {
  143. debug_image.instantiate();
  144. }
  145. unsigned char *ptrw = debug_data.ptrw();
  146. for (int i = 0; i < debug_data.size(); i++) {
  147. ptrw[i] = MIN(mips[0][i] / debug_tex_range, 1.0) * 255;
  148. }
  149. debug_image->set_data(sizes[0].x, sizes[0].y, false, Image::FORMAT_L8, debug_data);
  150. if (debug_texture.is_null()) {
  151. debug_texture = RS::get_singleton()->texture_2d_create(debug_image);
  152. } else {
  153. RenderingServer::get_singleton()->texture_2d_update(debug_texture, debug_image);
  154. }
  155. return debug_texture;
  156. }