raycast_occlusion_cull.h 7.5 KB

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