raycast_occlusion_cull.h 7.3 KB

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