renderer_scene_occlusion_cull.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**************************************************************************/
  2. /* renderer_scene_occlusion_cull.h */
  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. #ifndef RENDERER_SCENE_OCCLUSION_CULL_H
  31. #define RENDERER_SCENE_OCCLUSION_CULL_H
  32. #include "core/math/projection.h"
  33. #include "core/templates/local_vector.h"
  34. #include "servers/rendering_server.h"
  35. class RendererSceneOcclusionCull {
  36. protected:
  37. static RendererSceneOcclusionCull *singleton;
  38. public:
  39. class HZBuffer {
  40. protected:
  41. static const Vector3 corners[8];
  42. LocalVector<float> data;
  43. LocalVector<Size2i> sizes;
  44. LocalVector<float *> mips;
  45. RID debug_texture;
  46. Ref<Image> debug_image;
  47. PackedByteArray debug_data;
  48. float debug_tex_range = 0.0f;
  49. uint64_t occlusion_frame = 0;
  50. Size2i occlusion_buffer_size;
  51. _FORCE_INLINE_ bool _is_occluded(const real_t p_bounds[6], const Vector3 &p_cam_position, const Transform3D &p_cam_inv_transform, const Projection &p_cam_projection, real_t p_near) const {
  52. if (is_empty()) {
  53. return false;
  54. }
  55. Vector3 closest_point = p_cam_position.clamp(Vector3(p_bounds[0], p_bounds[1], p_bounds[2]), Vector3(p_bounds[3], p_bounds[4], p_bounds[5]));
  56. if (closest_point == p_cam_position) {
  57. return false;
  58. }
  59. Vector3 closest_point_view = p_cam_inv_transform.xform(closest_point);
  60. if (closest_point_view.z > -p_near) {
  61. return false;
  62. }
  63. float min_depth = -closest_point_view.z * 0.95f;
  64. Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX);
  65. Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN);
  66. for (int j = 0; j < 8; j++) {
  67. const Vector3 &c = RendererSceneOcclusionCull::HZBuffer::corners[j];
  68. Vector3 nc = Vector3(1, 1, 1) - c;
  69. Vector3 corner = Vector3(p_bounds[0] * c.x + p_bounds[3] * nc.x, p_bounds[1] * c.y + p_bounds[4] * nc.y, p_bounds[2] * c.z + p_bounds[5] * nc.z);
  70. Vector3 view = p_cam_inv_transform.xform(corner);
  71. Plane vp = Plane(view, 1.0);
  72. Plane projected = p_cam_projection.xform4(vp);
  73. float w = projected.d;
  74. if (w < 1.0) {
  75. rect_min = Vector2(0.0f, 0.0f);
  76. rect_max = Vector2(1.0f, 1.0f);
  77. break;
  78. }
  79. Vector2 normalized = Vector2(projected.normal.x / w * 0.5f + 0.5f, projected.normal.y / w * 0.5f + 0.5f);
  80. rect_min = rect_min.min(normalized);
  81. rect_max = rect_max.max(normalized);
  82. }
  83. rect_max = rect_max.minf(1);
  84. rect_min = rect_min.maxf(0);
  85. int mip_count = mips.size();
  86. Vector2 screen_diagonal = (rect_max - rect_min) * sizes[0];
  87. float size = MAX(screen_diagonal.x, screen_diagonal.y);
  88. float l = Math::ceil(Math::log2(size));
  89. int lod = CLAMP(l, 0, mip_count - 1);
  90. const int max_samples = 512;
  91. int sample_count = 0;
  92. bool visible = true;
  93. for (; lod >= 0; lod--) {
  94. int w = sizes[lod].x;
  95. int h = sizes[lod].y;
  96. int minx = CLAMP(rect_min.x * w - 1, 0, w - 1);
  97. int maxx = CLAMP(rect_max.x * w + 1, 0, w - 1);
  98. int miny = CLAMP(rect_min.y * h - 1, 0, h - 1);
  99. int maxy = CLAMP(rect_max.y * h + 1, 0, h - 1);
  100. sample_count += (maxx - minx + 1) * (maxy - miny + 1);
  101. if (sample_count > max_samples) {
  102. return false;
  103. }
  104. visible = false;
  105. for (int y = miny; y <= maxy; y++) {
  106. for (int x = minx; x <= maxx; x++) {
  107. float depth = mips[lod][y * w + x];
  108. if (depth > min_depth) {
  109. visible = true;
  110. break;
  111. }
  112. }
  113. if (visible) {
  114. break;
  115. }
  116. }
  117. if (!visible) {
  118. return true;
  119. }
  120. }
  121. return !visible;
  122. }
  123. public:
  124. static bool occlusion_jitter_enabled;
  125. bool is_empty() const;
  126. virtual void clear();
  127. virtual void resize(const Size2i &p_size);
  128. void update_mips();
  129. // Thin wrapper around _is_occluded(),
  130. // allowing occlusion timers to delay the disappearance
  131. // of objects to prevent flickering when using jittering.
  132. _FORCE_INLINE_ bool is_occluded(const real_t p_bounds[6], const Vector3 &p_cam_position, const Transform3D &p_cam_inv_transform, const Projection &p_cam_projection, real_t p_near, uint64_t &r_occlusion_timeout) const {
  133. bool occluded = _is_occluded(p_bounds, p_cam_position, p_cam_inv_transform, p_cam_projection, p_near);
  134. // Special case, temporal jitter disabled,
  135. // so we don't use occlusion timers.
  136. if (!occlusion_jitter_enabled) {
  137. return occluded;
  138. }
  139. if (!occluded) {
  140. //#define DEBUG_RASTER_OCCLUSION_JITTER
  141. #ifdef DEBUG_RASTER_OCCLUSION_JITTER
  142. r_occlusion_timeout = occlusion_frame + 1;
  143. #else
  144. r_occlusion_timeout = occlusion_frame + 9;
  145. #endif
  146. } else if (r_occlusion_timeout) {
  147. // Regular timeout, allow occlusion culling
  148. // to proceed as normal after the delay.
  149. if (occlusion_frame >= r_occlusion_timeout) {
  150. r_occlusion_timeout = 0;
  151. }
  152. }
  153. return occluded && !r_occlusion_timeout;
  154. }
  155. RID get_debug_texture();
  156. const Size2i &get_occlusion_buffer_size() const { return occlusion_buffer_size; }
  157. virtual ~HZBuffer(){};
  158. };
  159. static RendererSceneOcclusionCull *get_singleton() { return singleton; }
  160. void _print_warning() {
  161. WARN_PRINT_ONCE("Occlusion culling is disabled at build-time.");
  162. }
  163. virtual bool is_occluder(RID p_rid) { return false; }
  164. virtual RID occluder_allocate() { return RID(); }
  165. virtual void occluder_initialize(RID p_occluder) {}
  166. virtual void free_occluder(RID p_occluder) { _print_warning(); }
  167. virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) { _print_warning(); }
  168. virtual void add_scenario(RID p_scenario) {}
  169. virtual void remove_scenario(RID p_scenario) {}
  170. virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) { _print_warning(); }
  171. virtual void scenario_remove_instance(RID p_scenario, RID p_instance) { _print_warning(); }
  172. virtual void add_buffer(RID p_buffer) { _print_warning(); }
  173. virtual void remove_buffer(RID p_buffer) { _print_warning(); }
  174. virtual HZBuffer *buffer_get_ptr(RID p_buffer) {
  175. return nullptr;
  176. }
  177. virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) { _print_warning(); }
  178. virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) { _print_warning(); }
  179. virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {}
  180. virtual RID buffer_get_debug_texture(RID p_buffer) {
  181. _print_warning();
  182. return RID();
  183. }
  184. virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {}
  185. RendererSceneOcclusionCull() {
  186. singleton = this;
  187. };
  188. virtual ~RendererSceneOcclusionCull() {
  189. singleton = nullptr;
  190. };
  191. };
  192. #endif // RENDERER_SCENE_OCCLUSION_CULL_H