renderer_scene_occlusion_cull.cpp 5.5 KB

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