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