renderer_scene_occlusion_cull.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. public:
  50. bool is_empty() const;
  51. virtual void clear();
  52. virtual void resize(const Size2i &p_size);
  53. void update_mips();
  54. _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 {
  55. if (is_empty()) {
  56. return false;
  57. }
  58. Vector3 closest_point = Vector3(CLAMP(p_cam_position.x, p_bounds[0], p_bounds[3]), CLAMP(p_cam_position.y, p_bounds[1], p_bounds[4]), CLAMP(p_cam_position.z, p_bounds[2], p_bounds[5]));
  59. if (closest_point == p_cam_position) {
  60. return false;
  61. }
  62. Vector3 closest_point_view = p_cam_inv_transform.xform(closest_point);
  63. if (closest_point_view.z > -p_near) {
  64. return false;
  65. }
  66. float min_depth = -closest_point_view.z * 0.95f;
  67. Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX);
  68. Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN);
  69. for (int j = 0; j < 8; j++) {
  70. const Vector3 &c = RendererSceneOcclusionCull::HZBuffer::corners[j];
  71. Vector3 nc = Vector3(1, 1, 1) - c;
  72. 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);
  73. Vector3 view = p_cam_inv_transform.xform(corner);
  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.min(Vector2(1, 1));
  87. rect_min = rect_min.max(Vector2(0, 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. RID get_debug_texture();
  127. virtual ~HZBuffer(){};
  128. };
  129. static RendererSceneOcclusionCull *get_singleton() { return singleton; }
  130. void _print_warning() {
  131. WARN_PRINT_ONCE("Occlusion culling is disabled at build-time.");
  132. }
  133. virtual bool is_occluder(RID p_rid) { return false; }
  134. virtual RID occluder_allocate() { return RID(); }
  135. virtual void occluder_initialize(RID p_occluder) {}
  136. virtual void free_occluder(RID p_occluder) { _print_warning(); }
  137. virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) { _print_warning(); }
  138. virtual void add_scenario(RID p_scenario) {}
  139. virtual void remove_scenario(RID p_scenario) {}
  140. virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) { _print_warning(); }
  141. virtual void scenario_remove_instance(RID p_scenario, RID p_instance) { _print_warning(); }
  142. virtual void add_buffer(RID p_buffer) { _print_warning(); }
  143. virtual void remove_buffer(RID p_buffer) { _print_warning(); }
  144. virtual HZBuffer *buffer_get_ptr(RID p_buffer) {
  145. return nullptr;
  146. }
  147. virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) { _print_warning(); }
  148. virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) { _print_warning(); }
  149. virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {}
  150. virtual RID buffer_get_debug_texture(RID p_buffer) {
  151. _print_warning();
  152. return RID();
  153. }
  154. virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {}
  155. RendererSceneOcclusionCull() {
  156. singleton = this;
  157. };
  158. virtual ~RendererSceneOcclusionCull() {
  159. singleton = nullptr;
  160. };
  161. };
  162. #endif // RENDERER_SCENE_OCCLUSION_CULL_H