renderer_scene_occlusion_cull.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. bool RendererSceneOcclusionCull::HZBuffer::occlusion_jitter_enabled = false;
  33. void RendererSceneOcclusionCull::HZBuffer::clear() {
  34. if (sizes.is_empty()) {
  35. return; // Already cleared
  36. }
  37. data.clear();
  38. sizes.clear();
  39. mips.clear();
  40. debug_data.clear();
  41. if (debug_image.is_valid()) {
  42. debug_image.unref();
  43. }
  44. ERR_FAIL_NULL(RenderingServer::get_singleton());
  45. RS::get_singleton()->free_rid(debug_texture);
  46. }
  47. void RendererSceneOcclusionCull::HZBuffer::resize(const Size2i &p_size) {
  48. occlusion_buffer_size = p_size;
  49. if (p_size == Size2i()) {
  50. clear();
  51. return;
  52. }
  53. if (!sizes.is_empty() && p_size == sizes[0]) {
  54. return; // Size didn't change
  55. }
  56. int mip_count = 0;
  57. int data_size = 0;
  58. int w = p_size.x;
  59. int h = p_size.y;
  60. while (true) {
  61. data_size += h * w;
  62. w = MAX(1, w >> 1);
  63. h = MAX(1, h >> 1);
  64. mip_count++;
  65. if (w == 1U && h == 1U) {
  66. data_size += 1U;
  67. mip_count++;
  68. break;
  69. }
  70. }
  71. data.resize(data_size);
  72. mips.resize(mip_count);
  73. sizes.resize(mip_count);
  74. w = p_size.x;
  75. h = p_size.y;
  76. float *ptr = data.ptr();
  77. for (int i = 0; i < mip_count; i++) {
  78. sizes[i] = Size2i(w, h);
  79. mips[i] = ptr;
  80. ptr = &ptr[w * h];
  81. w = MAX(1, w >> 1);
  82. h = MAX(1, h >> 1);
  83. }
  84. for (int i = 0; i < data_size; i++) {
  85. data[i] = FLT_MAX;
  86. }
  87. debug_data.resize(sizes[0].x * sizes[0].y);
  88. if (debug_texture.is_valid()) {
  89. RS::get_singleton()->free_rid(debug_texture);
  90. debug_texture = RID();
  91. }
  92. }
  93. void RendererSceneOcclusionCull::HZBuffer::update_mips() {
  94. // Keep this up to date as a local to be used for occlusion timers.
  95. occlusion_frame = Engine::get_singleton()->get_frames_drawn();
  96. if (sizes.is_empty()) {
  97. return;
  98. }
  99. for (uint32_t mip = 1; mip < mips.size(); mip++) {
  100. for (int y = 0; y < sizes[mip].y; y++) {
  101. for (int x = 0; x < sizes[mip].x; x++) {
  102. int prev_x = x * 2;
  103. int prev_y = y * 2;
  104. int prev_w = sizes[mip - 1].width;
  105. int prev_h = sizes[mip - 1].height;
  106. bool odd_w = (prev_w % 2) != 0;
  107. bool odd_h = (prev_h % 2) != 0;
  108. #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))])
  109. float max_depth = mips[mip - 1][prev_y * sizes[mip - 1].x + prev_x];
  110. CHECK_OFFSET(0, 1);
  111. CHECK_OFFSET(1, 0);
  112. CHECK_OFFSET(1, 1);
  113. if (odd_w) {
  114. CHECK_OFFSET(2, 0);
  115. CHECK_OFFSET(2, 1);
  116. }
  117. if (odd_h) {
  118. CHECK_OFFSET(0, 2);
  119. CHECK_OFFSET(1, 2);
  120. }
  121. if (odd_w && odd_h) {
  122. CHECK_OFFSET(2, 2);
  123. }
  124. mips[mip][y * sizes[mip].x + x] = max_depth;
  125. #undef CHECK_OFFSET
  126. }
  127. }
  128. }
  129. }
  130. RID RendererSceneOcclusionCull::HZBuffer::get_debug_texture() {
  131. if (sizes.is_empty() || sizes[0] == Size2i()) {
  132. return RID();
  133. }
  134. if (debug_image.is_null()) {
  135. debug_image.instantiate();
  136. }
  137. unsigned char *ptrw = debug_data.ptrw();
  138. for (int i = 0; i < debug_data.size(); i++) {
  139. ptrw[i] = MIN(Math::log(1.0 + mips[0][i]) / Math::log(1.0 + debug_tex_range), 1.0) * 255;
  140. }
  141. debug_image->set_data(sizes[0].x, sizes[0].y, false, Image::FORMAT_L8, debug_data);
  142. if (debug_texture.is_null()) {
  143. debug_texture = RS::get_singleton()->texture_2d_create(debug_image);
  144. } else {
  145. RenderingServer::get_singleton()->texture_2d_update(debug_texture, debug_image);
  146. }
  147. return debug_texture;
  148. }