raycast_occlusion_cull.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* raycast_occlusion_cull.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 OCCLUSION_CULL_RAYCASTER_H
  31. #define OCCLUSION_CULL_RAYCASTER_H
  32. #include "core/io/image.h"
  33. #include "core/math/camera_matrix.h"
  34. #include "core/object/object.h"
  35. #include "core/object/reference.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 <embree3/rtcore.h>
  41. class RaycastOcclusionCull : public RendererSceneOcclusionCull {
  42. typedef RTCRayHit16 RayPacket;
  43. public:
  44. class RaycastHZBuffer : public HZBuffer {
  45. private:
  46. Size2i packs_size;
  47. struct CameraRayThreadData {
  48. CameraMatrix camera_matrix;
  49. Transform camera_transform;
  50. bool camera_orthogonal;
  51. int thread_count;
  52. Size2i buffer_size;
  53. };
  54. void _camera_rays_threaded(uint32_t p_thread, CameraRayThreadData *p_data);
  55. void _generate_camera_rays(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, int p_from, int p_to);
  56. public:
  57. LocalVector<RayPacket> camera_rays;
  58. LocalVector<uint32_t> camera_ray_masks;
  59. RID scenario_rid;
  60. virtual void clear() override;
  61. virtual void resize(const Size2i &p_size) override;
  62. void sort_rays();
  63. void update_camera_rays(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_work_pool);
  64. };
  65. private:
  66. struct InstanceID {
  67. RID scenario;
  68. RID instance;
  69. bool operator<(const InstanceID &rhs) const {
  70. if (instance == rhs.instance) {
  71. return rhs.scenario < scenario;
  72. }
  73. return instance < rhs.instance;
  74. }
  75. InstanceID() {}
  76. InstanceID(RID s, RID i) :
  77. scenario(s), instance(i) {}
  78. };
  79. struct Occluder {
  80. PackedVector3Array vertices;
  81. PackedInt32Array indices;
  82. Set<InstanceID> users;
  83. };
  84. struct OccluderInstance {
  85. RID occluder;
  86. LocalVector<uint32_t> indices;
  87. LocalVector<Vector3> xformed_vertices;
  88. Transform xform;
  89. bool enabled = true;
  90. bool removed = false;
  91. };
  92. struct Scenario {
  93. struct RaycastThreadData {
  94. RayPacket *rays;
  95. const uint32_t *masks;
  96. };
  97. struct TransformThreadData {
  98. uint32_t thread_count;
  99. uint32_t vertex_count;
  100. Transform xform;
  101. const Vector3 *read;
  102. Vector3 *write;
  103. };
  104. Thread *commit_thread = nullptr;
  105. bool commit_done = true;
  106. bool dirty = false;
  107. bool removed = false;
  108. RTCScene ebr_scene[2] = { nullptr, nullptr };
  109. int current_scene_idx = 0;
  110. HashMap<RID, OccluderInstance> instances;
  111. Set<RID> dirty_instances; // To avoid duplicates
  112. LocalVector<RID> dirty_instances_array; // To iterate and split into threads
  113. LocalVector<RID> removed_instances;
  114. void _update_dirty_instance_thread(int p_idx, RID *p_instances);
  115. void _update_dirty_instance(int p_idx, RID *p_instances, ThreadWorkPool *p_thread_pool);
  116. void _transform_vertices_thread(uint32_t p_thread, TransformThreadData *p_data);
  117. void _transform_vertices_range(const Vector3 *p_read, Vector3 *p_write, const Transform &p_xform, int p_from, int p_to);
  118. static void _commit_scene(void *p_ud);
  119. bool update(ThreadWorkPool &p_thread_pool);
  120. void _raycast(uint32_t p_thread, const RaycastThreadData *p_raycast_data) const;
  121. void raycast(LocalVector<RayPacket> &r_rays, const LocalVector<uint32_t> p_valid_masks, ThreadWorkPool &p_thread_pool) const;
  122. };
  123. static RaycastOcclusionCull *raycast_singleton;
  124. static const int TILE_SIZE = 4;
  125. static const int TILE_RAYS = TILE_SIZE * TILE_SIZE;
  126. RTCDevice ebr_device = nullptr;
  127. RID_PtrOwner<Occluder> occluder_owner;
  128. HashMap<RID, Scenario> scenarios;
  129. HashMap<RID, RaycastHZBuffer> buffers;
  130. RS::ViewportOcclusionCullingBuildQuality build_quality;
  131. void _init_embree();
  132. public:
  133. virtual bool is_occluder(RID p_rid) override;
  134. virtual RID occluder_allocate() override;
  135. virtual void occluder_initialize(RID p_occluder) override;
  136. virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) override;
  137. virtual void free_occluder(RID p_occluder) override;
  138. virtual void add_scenario(RID p_scenario) override;
  139. virtual void remove_scenario(RID p_scenario) override;
  140. virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform &p_xform, bool p_enabled) override;
  141. virtual void scenario_remove_instance(RID p_scenario, RID p_instance) override;
  142. virtual void add_buffer(RID p_buffer) override;
  143. virtual void remove_buffer(RID p_buffer) override;
  144. virtual HZBuffer *buffer_get_ptr(RID p_buffer) override;
  145. virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) override;
  146. virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) override;
  147. virtual void buffer_update(RID p_buffer, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_pool) override;
  148. virtual RID buffer_get_debug_texture(RID p_buffer) override;
  149. virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) override;
  150. RaycastOcclusionCull();
  151. ~RaycastOcclusionCull();
  152. };
  153. #endif // OCCLUSION_CULL_RAYCASTER_H