2
0

raycast_occlusion_cull.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/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 <embree3/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 CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_work_pool);
  71. ~RaycastHZBuffer();
  72. };
  73. private:
  74. struct InstanceID {
  75. RID scenario;
  76. RID instance;
  77. bool operator<(const InstanceID &rhs) const {
  78. if (instance == rhs.instance) {
  79. return rhs.scenario < scenario;
  80. }
  81. return instance < rhs.instance;
  82. }
  83. InstanceID() {}
  84. InstanceID(RID s, RID i) :
  85. scenario(s), instance(i) {}
  86. };
  87. struct Occluder {
  88. PackedVector3Array vertices;
  89. PackedInt32Array indices;
  90. Set<InstanceID> users;
  91. };
  92. struct OccluderInstance {
  93. RID occluder;
  94. LocalVector<uint32_t> indices;
  95. LocalVector<Vector3> xformed_vertices;
  96. Transform3D xform;
  97. bool enabled = true;
  98. bool removed = false;
  99. };
  100. struct Scenario {
  101. struct RaycastThreadData {
  102. CameraRayTile *rays;
  103. const uint32_t *masks;
  104. };
  105. struct TransformThreadData {
  106. uint32_t thread_count;
  107. uint32_t vertex_count;
  108. Transform3D xform;
  109. const Vector3 *read;
  110. Vector3 *write;
  111. };
  112. Thread *commit_thread = nullptr;
  113. bool commit_done = true;
  114. bool dirty = false;
  115. bool removed = false;
  116. RTCScene ebr_scene[2] = { nullptr, nullptr };
  117. int current_scene_idx = 0;
  118. HashMap<RID, OccluderInstance> instances;
  119. Set<RID> dirty_instances; // To avoid duplicates
  120. LocalVector<RID> dirty_instances_array; // To iterate and split into threads
  121. LocalVector<RID> removed_instances;
  122. void _update_dirty_instance_thread(int p_idx, RID *p_instances);
  123. void _update_dirty_instance(int p_idx, RID *p_instances, ThreadWorkPool *p_thread_pool);
  124. void _transform_vertices_thread(uint32_t p_thread, TransformThreadData *p_data);
  125. void _transform_vertices_range(const Vector3 *p_read, Vector3 *p_write, const Transform3D &p_xform, int p_from, int p_to);
  126. static void _commit_scene(void *p_ud);
  127. bool update(ThreadWorkPool &p_thread_pool);
  128. void _raycast(uint32_t p_thread, const RaycastThreadData *p_raycast_data) const;
  129. void raycast(CameraRayTile *r_rays, const uint32_t *p_valid_masks, uint32_t p_tile_count, ThreadWorkPool &p_thread_pool) const;
  130. };
  131. static RaycastOcclusionCull *raycast_singleton;
  132. static const int TILE_SIZE = 4;
  133. static const int TILE_RAYS = TILE_SIZE * TILE_SIZE;
  134. RTCDevice ebr_device = nullptr;
  135. RID_PtrOwner<Occluder> occluder_owner;
  136. HashMap<RID, Scenario> scenarios;
  137. HashMap<RID, RaycastHZBuffer> buffers;
  138. RS::ViewportOcclusionCullingBuildQuality build_quality;
  139. void _init_embree();
  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 CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_pool) 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 // OCCLUSION_CULL_RAYCASTER_H