renderer_scene_occlusion_cull.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 - p_cam_position).length();
  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. if (p_cam_projection.is_orthogonal()) {
  72. min_depth = MIN(min_depth, -view.z);
  73. }
  74. Plane vp = Plane(view, 1.0);
  75. Plane projected = p_cam_projection.xform4(vp);
  76. float w = projected.d;
  77. if (w < 1.0) {
  78. rect_min = Vector2(0.0f, 0.0f);
  79. rect_max = Vector2(1.0f, 1.0f);
  80. break;
  81. }
  82. Vector2 normalized = Vector2(projected.normal.x / w * 0.5f + 0.5f, projected.normal.y / w * 0.5f + 0.5f);
  83. rect_min = rect_min.min(normalized);
  84. rect_max = rect_max.max(normalized);
  85. }
  86. rect_max = rect_max.minf(1);
  87. rect_min = rect_min.maxf(0);
  88. int mip_count = mips.size();
  89. Vector2 screen_diagonal = (rect_max - rect_min) * sizes[0];
  90. float size = MAX(screen_diagonal.x, screen_diagonal.y);
  91. float l = Math::ceil(Math::log2(size));
  92. int lod = CLAMP(l, 0, mip_count - 1);
  93. const int max_samples = 512;
  94. int sample_count = 0;
  95. bool visible = true;
  96. for (; lod >= 0; lod--) {
  97. int w = sizes[lod].x;
  98. int h = sizes[lod].y;
  99. int minx = CLAMP(rect_min.x * w - 1, 0, w - 1);
  100. int maxx = CLAMP(rect_max.x * w + 1, 0, w - 1);
  101. int miny = CLAMP(rect_min.y * h - 1, 0, h - 1);
  102. int maxy = CLAMP(rect_max.y * h + 1, 0, h - 1);
  103. sample_count += (maxx - minx + 1) * (maxy - miny + 1);
  104. if (sample_count > max_samples) {
  105. return false;
  106. }
  107. visible = false;
  108. for (int y = miny; y <= maxy; y++) {
  109. for (int x = minx; x <= maxx; x++) {
  110. float depth = mips[lod][y * w + x];
  111. if (depth > min_depth) {
  112. visible = true;
  113. break;
  114. }
  115. }
  116. if (visible) {
  117. break;
  118. }
  119. }
  120. if (!visible) {
  121. return true;
  122. }
  123. }
  124. return !visible;
  125. }
  126. public:
  127. static bool occlusion_jitter_enabled;
  128. bool is_empty() const;
  129. virtual void clear();
  130. virtual void resize(const Size2i &p_size);
  131. void update_mips();
  132. // Thin wrapper around _is_occluded(),
  133. // allowing occlusion timers to delay the disappearance
  134. // of objects to prevent flickering when using jittering.
  135. _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 {
  136. bool occluded = _is_occluded(p_bounds, p_cam_position, p_cam_inv_transform, p_cam_projection, p_near);
  137. // Special case, temporal jitter disabled,
  138. // so we don't use occlusion timers.
  139. if (!occlusion_jitter_enabled) {
  140. return occluded;
  141. }
  142. if (!occluded) {
  143. //#define DEBUG_RASTER_OCCLUSION_JITTER
  144. #ifdef DEBUG_RASTER_OCCLUSION_JITTER
  145. r_occlusion_timeout = occlusion_frame + 1;
  146. #else
  147. r_occlusion_timeout = occlusion_frame + 9;
  148. #endif
  149. } else if (r_occlusion_timeout) {
  150. // Regular timeout, allow occlusion culling
  151. // to proceed as normal after the delay.
  152. if (occlusion_frame >= r_occlusion_timeout) {
  153. r_occlusion_timeout = 0;
  154. }
  155. }
  156. return occluded && !r_occlusion_timeout;
  157. }
  158. RID get_debug_texture();
  159. const Size2i &get_occlusion_buffer_size() const { return occlusion_buffer_size; }
  160. virtual ~HZBuffer() {}
  161. };
  162. static RendererSceneOcclusionCull *get_singleton() { return singleton; }
  163. void _print_warning() {
  164. WARN_PRINT_ONCE("Occlusion culling is disabled at build-time.");
  165. }
  166. virtual bool is_occluder(RID p_rid) { return false; }
  167. virtual RID occluder_allocate() { return RID(); }
  168. virtual void occluder_initialize(RID p_occluder) {}
  169. virtual void free_occluder(RID p_occluder) { _print_warning(); }
  170. virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) { _print_warning(); }
  171. virtual void add_scenario(RID p_scenario) {}
  172. virtual void remove_scenario(RID p_scenario) {}
  173. virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) { _print_warning(); }
  174. virtual void scenario_remove_instance(RID p_scenario, RID p_instance) { _print_warning(); }
  175. virtual void add_buffer(RID p_buffer) { _print_warning(); }
  176. virtual void remove_buffer(RID p_buffer) { _print_warning(); }
  177. virtual HZBuffer *buffer_get_ptr(RID p_buffer) {
  178. return nullptr;
  179. }
  180. virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) { _print_warning(); }
  181. virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) { _print_warning(); }
  182. virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {}
  183. virtual RID buffer_get_debug_texture(RID p_buffer) {
  184. _print_warning();
  185. return RID();
  186. }
  187. virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {}
  188. RendererSceneOcclusionCull() {
  189. singleton = this;
  190. }
  191. virtual ~RendererSceneOcclusionCull() {
  192. singleton = nullptr;
  193. }
  194. };
  195. #endif // RENDERER_SCENE_OCCLUSION_CULL_H