raycast_occlusion_cull.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /* raycast_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 RAYCAST_OCCLUSION_CULL_H
  31. #define RAYCAST_OCCLUSION_CULL_H
  32. #include "core/math/projection.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/rid_owner.h"
  35. #include "servers/rendering/renderer_scene_occlusion_cull.h"
  36. #include <embree4/rtcore.h>
  37. class RaycastOcclusionCull : public RendererSceneOcclusionCull {
  38. typedef RTCRayHit16 CameraRayTile;
  39. public:
  40. class RaycastHZBuffer : public HZBuffer {
  41. private:
  42. Size2i tile_grid_size;
  43. struct CameraRayThreadData {
  44. int thread_count;
  45. float z_near;
  46. float z_far;
  47. Vector3 camera_dir;
  48. Vector3 camera_pos;
  49. Vector3 pixel_corner;
  50. Vector3 pixel_u_interp;
  51. Vector3 pixel_v_interp;
  52. bool camera_orthogonal;
  53. Size2i buffer_size;
  54. };
  55. void _camera_rays_threaded(uint32_t p_thread, const CameraRayThreadData *p_data);
  56. void _generate_camera_rays(const CameraRayThreadData *p_data, int p_from, int p_to);
  57. public:
  58. unsigned int camera_rays_tile_count = 0;
  59. uint8_t *camera_rays_unaligned_buffer = nullptr;
  60. CameraRayTile *camera_rays = nullptr;
  61. LocalVector<uint32_t> camera_ray_masks;
  62. RID scenario_rid;
  63. virtual void clear() override;
  64. virtual void resize(const Size2i &p_size) override;
  65. void sort_rays(const Vector3 &p_camera_dir, bool p_orthogonal);
  66. void update_camera_rays(const Transform3D &p_cam_transform, const Vector3 &p_near_bottom_left, const Vector2 &p_near_extents, real_t p_z_far, bool p_cam_orthogonal);
  67. ~RaycastHZBuffer();
  68. };
  69. private:
  70. struct InstanceID {
  71. RID scenario;
  72. RID instance;
  73. static uint32_t hash(const InstanceID &p_ins) {
  74. uint32_t h = hash_murmur3_one_64(p_ins.scenario.get_id());
  75. return hash_fmix32(hash_murmur3_one_64(p_ins.instance.get_id(), h));
  76. }
  77. bool operator==(const InstanceID &rhs) const {
  78. return instance == rhs.instance && rhs.scenario == scenario;
  79. ;
  80. }
  81. InstanceID() {}
  82. InstanceID(RID s, RID i) :
  83. scenario(s), instance(i) {}
  84. };
  85. struct Occluder {
  86. PackedVector3Array vertices;
  87. PackedInt32Array indices;
  88. HashSet<InstanceID, InstanceID> users;
  89. };
  90. struct OccluderInstance {
  91. RID occluder;
  92. LocalVector<uint32_t> indices;
  93. LocalVector<float> xformed_vertices;
  94. Transform3D xform;
  95. bool enabled = true;
  96. bool removed = false;
  97. };
  98. struct Scenario {
  99. struct RaycastThreadData {
  100. CameraRayTile *rays = nullptr;
  101. const uint32_t *masks;
  102. };
  103. struct TransformThreadData {
  104. uint32_t thread_count;
  105. uint32_t vertex_count;
  106. Transform3D xform;
  107. const Vector3 *read;
  108. float *write = nullptr;
  109. };
  110. Thread *commit_thread = nullptr;
  111. bool commit_done = true;
  112. bool dirty = false;
  113. RTCScene ebr_scene[2] = { nullptr, nullptr };
  114. int current_scene_idx = 0;
  115. HashMap<RID, OccluderInstance> instances;
  116. HashSet<RID> dirty_instances; // To avoid duplicates
  117. LocalVector<RID> dirty_instances_array; // To iterate and split into threads
  118. LocalVector<RID> removed_instances;
  119. void _update_dirty_instance_thread(int p_idx, RID *p_instances);
  120. void _update_dirty_instance(int p_idx, RID *p_instances);
  121. void _transform_vertices_thread(uint32_t p_thread, TransformThreadData *p_data);
  122. void _transform_vertices_range(const Vector3 *p_read, float *p_write, const Transform3D &p_xform, int p_from, int p_to);
  123. static void _commit_scene(void *p_ud);
  124. void free();
  125. void update();
  126. void _raycast(uint32_t p_thread, const RaycastThreadData *p_raycast_data) const;
  127. void raycast(CameraRayTile *r_rays, const uint32_t *p_valid_masks, uint32_t p_tile_count) const;
  128. };
  129. static RaycastOcclusionCull *raycast_singleton;
  130. static const int TILE_SIZE = 4;
  131. static const int TILE_RAYS = TILE_SIZE * TILE_SIZE;
  132. RTCDevice ebr_device = nullptr;
  133. RID_PtrOwner<Occluder> occluder_owner;
  134. HashMap<RID, Scenario> scenarios;
  135. HashMap<RID, RaycastHZBuffer> buffers;
  136. RS::ViewportOcclusionCullingBuildQuality build_quality;
  137. bool _jitter_enabled = false;
  138. void _init_embree();
  139. Vector2 _jitter_half_extents(const Vector2 &p_half_extents, const Size2i &p_viewport_size);
  140. public:
  141. virtual bool is_occluder(RID p_rid) override;
  142. virtual RID occluder_allocate() override;
  143. virtual void occluder_initialize(RID p_occluder) override;
  144. virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) override;
  145. virtual void free_occluder(RID p_occluder) override;
  146. virtual void add_scenario(RID p_scenario) override;
  147. virtual void remove_scenario(RID p_scenario) override;
  148. virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) override;
  149. virtual void scenario_remove_instance(RID p_scenario, RID p_instance) override;
  150. virtual void add_buffer(RID p_buffer) override;
  151. virtual void remove_buffer(RID p_buffer) override;
  152. virtual HZBuffer *buffer_get_ptr(RID p_buffer) override;
  153. virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) override;
  154. virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) override;
  155. virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) override;
  156. virtual RID buffer_get_debug_texture(RID p_buffer) override;
  157. virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) override;
  158. RaycastOcclusionCull();
  159. ~RaycastOcclusionCull();
  160. };
  161. #endif // RAYCAST_OCCLUSION_CULL_H