raycast_occlusion_cull.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*************************************************************************/
  2. /* raycast_occlusion_cull.cpp */
  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. #include "raycast_occlusion_cull.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/templates/local_vector.h"
  33. #ifdef __SSE2__
  34. #include <pmmintrin.h>
  35. #endif
  36. RaycastOcclusionCull *RaycastOcclusionCull::raycast_singleton = nullptr;
  37. void RaycastOcclusionCull::RaycastHZBuffer::clear() {
  38. HZBuffer::clear();
  39. if (camera_rays_unaligned_buffer) {
  40. memfree(camera_rays_unaligned_buffer);
  41. camera_rays_unaligned_buffer = nullptr;
  42. camera_rays = nullptr;
  43. }
  44. camera_ray_masks.clear();
  45. camera_rays_tile_count = 0;
  46. tile_grid_size = Size2i();
  47. }
  48. void RaycastOcclusionCull::RaycastHZBuffer::resize(const Size2i &p_size) {
  49. if (p_size == Size2i()) {
  50. clear();
  51. return;
  52. }
  53. if (!sizes.is_empty() && p_size == sizes[0]) {
  54. return; // Size didn't change
  55. }
  56. HZBuffer::resize(p_size);
  57. tile_grid_size = Size2i(Math::ceil(p_size.x / (float)TILE_SIZE), Math::ceil(p_size.y / (float)TILE_SIZE));
  58. camera_rays_tile_count = tile_grid_size.x * tile_grid_size.y;
  59. if (camera_rays_unaligned_buffer) {
  60. memfree(camera_rays_unaligned_buffer);
  61. }
  62. const int alignment = 64; // Embree requires ray packets to be 64-aligned
  63. camera_rays_unaligned_buffer = (uint8_t *)memalloc(camera_rays_tile_count * sizeof(CameraRayTile) + alignment);
  64. camera_rays = (CameraRayTile *)(camera_rays_unaligned_buffer + alignment - (((uint64_t)camera_rays_unaligned_buffer) % alignment));
  65. camera_ray_masks.resize(camera_rays_tile_count * TILE_RAYS);
  66. memset(camera_ray_masks.ptr(), ~0, camera_rays_tile_count * TILE_RAYS * sizeof(uint32_t));
  67. }
  68. void RaycastOcclusionCull::RaycastHZBuffer::update_camera_rays(const Transform3D &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_work_pool) {
  69. CameraRayThreadData td;
  70. td.thread_count = p_thread_work_pool.get_thread_count();
  71. td.z_near = p_cam_projection.get_z_near();
  72. td.z_far = p_cam_projection.get_z_far() * 1.05f;
  73. td.camera_pos = p_cam_transform.origin;
  74. td.camera_dir = -p_cam_transform.basis.get_axis(2);
  75. td.camera_orthogonal = p_cam_orthogonal;
  76. CameraMatrix inv_camera_matrix = p_cam_projection.inverse();
  77. Vector3 camera_corner_proj = Vector3(-1.0f, -1.0f, -1.0f);
  78. Vector3 camera_corner_view = inv_camera_matrix.xform(camera_corner_proj);
  79. td.pixel_corner = p_cam_transform.xform(camera_corner_view);
  80. Vector3 top_corner_proj = Vector3(-1.0f, 1.0f, -1.0f);
  81. Vector3 top_corner_view = inv_camera_matrix.xform(top_corner_proj);
  82. Vector3 top_corner_world = p_cam_transform.xform(top_corner_view);
  83. Vector3 left_corner_proj = Vector3(1.0f, -1.0f, -1.0f);
  84. Vector3 left_corner_view = inv_camera_matrix.xform(left_corner_proj);
  85. Vector3 left_corner_world = p_cam_transform.xform(left_corner_view);
  86. td.pixel_u_interp = left_corner_world - td.pixel_corner;
  87. td.pixel_v_interp = top_corner_world - td.pixel_corner;
  88. debug_tex_range = td.z_far;
  89. p_thread_work_pool.do_work(td.thread_count, this, &RaycastHZBuffer::_camera_rays_threaded, &td);
  90. }
  91. void RaycastOcclusionCull::RaycastHZBuffer::_camera_rays_threaded(uint32_t p_thread, const CameraRayThreadData *p_data) {
  92. uint32_t total_tiles = camera_rays_tile_count;
  93. uint32_t total_threads = p_data->thread_count;
  94. uint32_t from = p_thread * total_tiles / total_threads;
  95. uint32_t to = (p_thread + 1 == total_threads) ? total_tiles : ((p_thread + 1) * total_tiles / total_threads);
  96. _generate_camera_rays(p_data, from, to);
  97. }
  98. void RaycastOcclusionCull::RaycastHZBuffer::_generate_camera_rays(const CameraRayThreadData *p_data, int p_from, int p_to) {
  99. const Size2i &buffer_size = sizes[0];
  100. for (int i = p_from; i < p_to; i++) {
  101. CameraRayTile &tile = camera_rays[i];
  102. int tile_x = (i % tile_grid_size.x) * TILE_SIZE;
  103. int tile_y = (i / tile_grid_size.x) * TILE_SIZE;
  104. for (int j = 0; j < TILE_RAYS; j++) {
  105. int x = tile_x + j % TILE_SIZE;
  106. int y = tile_y + j / TILE_SIZE;
  107. float u = (float(x) + 0.5f) / buffer_size.x;
  108. float v = (float(y) + 0.5f) / buffer_size.y;
  109. Vector3 pixel_pos = p_data->pixel_corner + u * p_data->pixel_u_interp + v * p_data->pixel_v_interp;
  110. tile.ray.tnear[j] = p_data->z_near;
  111. Vector3 dir;
  112. if (p_data->camera_orthogonal) {
  113. dir = -p_data->camera_dir;
  114. tile.ray.org_x[j] = pixel_pos.x - dir.x * p_data->z_near;
  115. tile.ray.org_y[j] = pixel_pos.y - dir.y * p_data->z_near;
  116. tile.ray.org_z[j] = pixel_pos.z - dir.z * p_data->z_near;
  117. } else {
  118. dir = (pixel_pos - p_data->camera_pos).normalized();
  119. tile.ray.org_x[j] = p_data->camera_pos.x;
  120. tile.ray.org_y[j] = p_data->camera_pos.y;
  121. tile.ray.org_z[j] = p_data->camera_pos.z;
  122. tile.ray.tnear[j] /= dir.dot(p_data->camera_dir);
  123. }
  124. tile.ray.dir_x[j] = dir.x;
  125. tile.ray.dir_y[j] = dir.y;
  126. tile.ray.dir_z[j] = dir.z;
  127. tile.ray.tfar[j] = p_data->z_far;
  128. tile.ray.time[j] = 0.0f;
  129. tile.ray.flags[j] = 0;
  130. tile.ray.mask[j] = ~0U;
  131. tile.hit.geomID[j] = RTC_INVALID_GEOMETRY_ID;
  132. }
  133. }
  134. }
  135. void RaycastOcclusionCull::RaycastHZBuffer::sort_rays(const Vector3 &p_camera_dir, bool p_orthogonal) {
  136. ERR_FAIL_COND(is_empty());
  137. Size2i buffer_size = sizes[0];
  138. for (int i = 0; i < tile_grid_size.y; i++) {
  139. for (int j = 0; j < tile_grid_size.x; j++) {
  140. for (int tile_i = 0; tile_i < TILE_SIZE; tile_i++) {
  141. for (int tile_j = 0; tile_j < TILE_SIZE; tile_j++) {
  142. int x = j * TILE_SIZE + tile_j;
  143. int y = i * TILE_SIZE + tile_i;
  144. if (x >= buffer_size.x || y >= buffer_size.y) {
  145. continue;
  146. }
  147. int k = tile_i * TILE_SIZE + tile_j;
  148. int tile_index = i * tile_grid_size.x + j;
  149. float d = camera_rays[tile_index].ray.tfar[k];
  150. if (!p_orthogonal) {
  151. const float &dir_x = camera_rays[tile_index].ray.dir_x[k];
  152. const float &dir_y = camera_rays[tile_index].ray.dir_y[k];
  153. const float &dir_z = camera_rays[tile_index].ray.dir_z[k];
  154. float cos_theta = p_camera_dir.x * dir_x + p_camera_dir.y * dir_y + p_camera_dir.z * dir_z;
  155. d *= cos_theta;
  156. }
  157. mips[0][y * buffer_size.x + x] = d;
  158. }
  159. }
  160. }
  161. }
  162. }
  163. RaycastOcclusionCull::RaycastHZBuffer::~RaycastHZBuffer() {
  164. if (camera_rays_unaligned_buffer) {
  165. memfree(camera_rays_unaligned_buffer);
  166. }
  167. }
  168. ////////////////////////////////////////////////////////
  169. bool RaycastOcclusionCull::is_occluder(RID p_rid) {
  170. return occluder_owner.owns(p_rid);
  171. }
  172. RID RaycastOcclusionCull::occluder_allocate() {
  173. return occluder_owner.allocate_rid();
  174. }
  175. void RaycastOcclusionCull::occluder_initialize(RID p_occluder) {
  176. Occluder *occluder = memnew(Occluder);
  177. occluder_owner.initialize_rid(p_occluder, occluder);
  178. }
  179. void RaycastOcclusionCull::occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) {
  180. Occluder *occluder = occluder_owner.getornull(p_occluder);
  181. ERR_FAIL_COND(!occluder);
  182. occluder->vertices = p_vertices;
  183. occluder->indices = p_indices;
  184. for (Set<InstanceID>::Element *E = occluder->users.front(); E; E = E->next()) {
  185. RID scenario_rid = E->get().scenario;
  186. RID instance_rid = E->get().instance;
  187. ERR_CONTINUE(!scenarios.has(scenario_rid));
  188. Scenario &scenario = scenarios[scenario_rid];
  189. ERR_CONTINUE(!scenario.instances.has(instance_rid));
  190. if (!scenario.dirty_instances.has(instance_rid)) {
  191. scenario.dirty_instances.insert(instance_rid);
  192. scenario.dirty_instances_array.push_back(instance_rid);
  193. }
  194. }
  195. }
  196. void RaycastOcclusionCull::free_occluder(RID p_occluder) {
  197. Occluder *occluder = occluder_owner.getornull(p_occluder);
  198. ERR_FAIL_COND(!occluder);
  199. memdelete(occluder);
  200. occluder_owner.free(p_occluder);
  201. }
  202. ////////////////////////////////////////////////////////
  203. void RaycastOcclusionCull::add_scenario(RID p_scenario) {
  204. if (scenarios.has(p_scenario)) {
  205. scenarios[p_scenario].removed = false;
  206. } else {
  207. scenarios[p_scenario] = Scenario();
  208. }
  209. }
  210. void RaycastOcclusionCull::remove_scenario(RID p_scenario) {
  211. ERR_FAIL_COND(!scenarios.has(p_scenario));
  212. Scenario &scenario = scenarios[p_scenario];
  213. scenario.removed = true;
  214. }
  215. void RaycastOcclusionCull::scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) {
  216. ERR_FAIL_COND(!scenarios.has(p_scenario));
  217. Scenario &scenario = scenarios[p_scenario];
  218. if (!scenario.instances.has(p_instance)) {
  219. scenario.instances[p_instance] = OccluderInstance();
  220. }
  221. OccluderInstance &instance = scenario.instances[p_instance];
  222. if (instance.removed) {
  223. instance.removed = false;
  224. scenario.removed_instances.erase(p_instance);
  225. }
  226. bool changed = false;
  227. if (instance.occluder != p_occluder) {
  228. Occluder *old_occluder = occluder_owner.getornull(instance.occluder);
  229. if (old_occluder) {
  230. old_occluder->users.erase(InstanceID(p_scenario, p_instance));
  231. }
  232. instance.occluder = p_occluder;
  233. if (p_occluder.is_valid()) {
  234. Occluder *occluder = occluder_owner.getornull(p_occluder);
  235. ERR_FAIL_COND(!occluder);
  236. occluder->users.insert(InstanceID(p_scenario, p_instance));
  237. }
  238. changed = true;
  239. }
  240. if (instance.xform != p_xform) {
  241. scenario.instances[p_instance].xform = p_xform;
  242. changed = true;
  243. }
  244. if (instance.enabled != p_enabled) {
  245. instance.enabled = p_enabled;
  246. scenario.dirty = true; // The scenario needs a scene re-build, but the instance doesn't need update
  247. }
  248. if (changed && !scenario.dirty_instances.has(p_instance)) {
  249. scenario.dirty_instances.insert(p_instance);
  250. scenario.dirty_instances_array.push_back(p_instance);
  251. scenario.dirty = true;
  252. }
  253. }
  254. void RaycastOcclusionCull::scenario_remove_instance(RID p_scenario, RID p_instance) {
  255. ERR_FAIL_COND(!scenarios.has(p_scenario));
  256. Scenario &scenario = scenarios[p_scenario];
  257. if (scenario.instances.has(p_instance)) {
  258. OccluderInstance &instance = scenario.instances[p_instance];
  259. if (!instance.removed) {
  260. Occluder *occluder = occluder_owner.getornull(instance.occluder);
  261. if (occluder) {
  262. occluder->users.erase(InstanceID(p_scenario, p_instance));
  263. }
  264. scenario.removed_instances.push_back(p_instance);
  265. instance.removed = true;
  266. }
  267. }
  268. }
  269. void RaycastOcclusionCull::Scenario::_update_dirty_instance_thread(int p_idx, RID *p_instances) {
  270. _update_dirty_instance(p_idx, p_instances, nullptr);
  271. }
  272. void RaycastOcclusionCull::Scenario::_update_dirty_instance(int p_idx, RID *p_instances, ThreadWorkPool *p_thread_pool) {
  273. OccluderInstance *occ_inst = instances.getptr(p_instances[p_idx]);
  274. if (!occ_inst) {
  275. return;
  276. }
  277. Occluder *occ = raycast_singleton->occluder_owner.getornull(occ_inst->occluder);
  278. if (!occ) {
  279. return;
  280. }
  281. int vertices_size = occ->vertices.size();
  282. // Embree requires the last element to be readable by a 16-byte SSE load instruction, so we add padding to be safe.
  283. occ_inst->xformed_vertices.resize(vertices_size + 1);
  284. const Vector3 *read_ptr = occ->vertices.ptr();
  285. Vector3 *write_ptr = occ_inst->xformed_vertices.ptr();
  286. if (p_thread_pool && vertices_size > 1024) {
  287. TransformThreadData td;
  288. td.xform = occ_inst->xform;
  289. td.read = read_ptr;
  290. td.write = write_ptr;
  291. td.vertex_count = vertices_size;
  292. td.thread_count = p_thread_pool->get_thread_count();
  293. p_thread_pool->do_work(td.thread_count, this, &Scenario::_transform_vertices_thread, &td);
  294. } else {
  295. _transform_vertices_range(read_ptr, write_ptr, occ_inst->xform, 0, vertices_size);
  296. }
  297. occ_inst->indices.resize(occ->indices.size());
  298. memcpy(occ_inst->indices.ptr(), occ->indices.ptr(), occ->indices.size() * sizeof(int32_t));
  299. }
  300. void RaycastOcclusionCull::Scenario::_transform_vertices_thread(uint32_t p_thread, TransformThreadData *p_data) {
  301. uint32_t vertex_total = p_data->vertex_count;
  302. uint32_t total_threads = p_data->thread_count;
  303. uint32_t from = p_thread * vertex_total / total_threads;
  304. uint32_t to = (p_thread + 1 == total_threads) ? vertex_total : ((p_thread + 1) * vertex_total / total_threads);
  305. _transform_vertices_range(p_data->read, p_data->write, p_data->xform, from, to);
  306. }
  307. void RaycastOcclusionCull::Scenario::_transform_vertices_range(const Vector3 *p_read, Vector3 *p_write, const Transform3D &p_xform, int p_from, int p_to) {
  308. for (int i = p_from; i < p_to; i++) {
  309. p_write[i] = p_xform.xform(p_read[i]);
  310. }
  311. }
  312. void RaycastOcclusionCull::Scenario::_commit_scene(void *p_ud) {
  313. Scenario *scenario = (Scenario *)p_ud;
  314. int commit_idx = 1 - (scenario->current_scene_idx);
  315. rtcCommitScene(scenario->ebr_scene[commit_idx]);
  316. scenario->commit_done = true;
  317. }
  318. bool RaycastOcclusionCull::Scenario::update(ThreadWorkPool &p_thread_pool) {
  319. ERR_FAIL_COND_V(singleton == nullptr, false);
  320. if (commit_thread == nullptr) {
  321. commit_thread = memnew(Thread);
  322. }
  323. if (commit_thread->is_started()) {
  324. if (commit_done) {
  325. commit_thread->wait_to_finish();
  326. current_scene_idx = 1 - current_scene_idx;
  327. } else {
  328. return false;
  329. }
  330. }
  331. if (removed) {
  332. if (ebr_scene[0]) {
  333. rtcReleaseScene(ebr_scene[0]);
  334. }
  335. if (ebr_scene[1]) {
  336. rtcReleaseScene(ebr_scene[1]);
  337. }
  338. return true;
  339. }
  340. if (!dirty && removed_instances.is_empty() && dirty_instances_array.is_empty()) {
  341. return false;
  342. }
  343. for (unsigned int i = 0; i < removed_instances.size(); i++) {
  344. instances.erase(removed_instances[i]);
  345. }
  346. if (dirty_instances_array.size() / p_thread_pool.get_thread_count() > 128) {
  347. // Lots of instances, use per-instance threading
  348. p_thread_pool.do_work(dirty_instances_array.size(), this, &Scenario::_update_dirty_instance_thread, dirty_instances_array.ptr());
  349. } else {
  350. // Few instances, use threading on the vertex transforms
  351. for (unsigned int i = 0; i < dirty_instances_array.size(); i++) {
  352. _update_dirty_instance(i, dirty_instances_array.ptr(), &p_thread_pool);
  353. }
  354. }
  355. dirty_instances.clear();
  356. dirty_instances_array.clear();
  357. removed_instances.clear();
  358. if (raycast_singleton->ebr_device == nullptr) {
  359. raycast_singleton->_init_embree();
  360. }
  361. int next_scene_idx = 1 - current_scene_idx;
  362. RTCScene &next_scene = ebr_scene[next_scene_idx];
  363. if (next_scene) {
  364. rtcReleaseScene(next_scene);
  365. }
  366. next_scene = rtcNewScene(raycast_singleton->ebr_device);
  367. rtcSetSceneBuildQuality(next_scene, RTCBuildQuality(raycast_singleton->build_quality));
  368. const RID *inst_rid = nullptr;
  369. while ((inst_rid = instances.next(inst_rid))) {
  370. OccluderInstance *occ_inst = instances.getptr(*inst_rid);
  371. Occluder *occ = raycast_singleton->occluder_owner.getornull(occ_inst->occluder);
  372. if (!occ || !occ_inst->enabled) {
  373. continue;
  374. }
  375. RTCGeometry geom = rtcNewGeometry(raycast_singleton->ebr_device, RTC_GEOMETRY_TYPE_TRIANGLE);
  376. rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, occ_inst->xformed_vertices.ptr(), 0, sizeof(Vector3), occ_inst->xformed_vertices.size());
  377. rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, occ_inst->indices.ptr(), 0, sizeof(uint32_t) * 3, occ_inst->indices.size() / 3);
  378. rtcCommitGeometry(geom);
  379. rtcAttachGeometry(next_scene, geom);
  380. rtcReleaseGeometry(geom);
  381. }
  382. dirty = false;
  383. commit_done = false;
  384. commit_thread->start(&Scenario::_commit_scene, this);
  385. return false;
  386. }
  387. void RaycastOcclusionCull::Scenario::_raycast(uint32_t p_idx, const RaycastThreadData *p_raycast_data) const {
  388. RTCIntersectContext ctx;
  389. rtcInitIntersectContext(&ctx);
  390. ctx.flags = RTC_INTERSECT_CONTEXT_FLAG_COHERENT;
  391. rtcIntersect16((const int *)&p_raycast_data->masks[p_idx * TILE_RAYS], ebr_scene[current_scene_idx], &ctx, &p_raycast_data->rays[p_idx]);
  392. }
  393. void RaycastOcclusionCull::Scenario::raycast(CameraRayTile *r_rays, const uint32_t *p_valid_masks, uint32_t p_tile_count, ThreadWorkPool &p_thread_pool) const {
  394. ERR_FAIL_COND(singleton == nullptr);
  395. if (raycast_singleton->ebr_device == nullptr) {
  396. return; // Embree is initialized on demand when there is some scenario with occluders in it.
  397. }
  398. if (ebr_scene[current_scene_idx] == nullptr) {
  399. return;
  400. }
  401. RaycastThreadData td;
  402. td.rays = r_rays;
  403. td.masks = p_valid_masks;
  404. p_thread_pool.do_work(p_tile_count, this, &Scenario::_raycast, &td);
  405. }
  406. ////////////////////////////////////////////////////////
  407. void RaycastOcclusionCull::add_buffer(RID p_buffer) {
  408. ERR_FAIL_COND(buffers.has(p_buffer));
  409. buffers[p_buffer] = RaycastHZBuffer();
  410. }
  411. void RaycastOcclusionCull::remove_buffer(RID p_buffer) {
  412. ERR_FAIL_COND(!buffers.has(p_buffer));
  413. buffers.erase(p_buffer);
  414. }
  415. void RaycastOcclusionCull::buffer_set_scenario(RID p_buffer, RID p_scenario) {
  416. ERR_FAIL_COND(!buffers.has(p_buffer));
  417. ERR_FAIL_COND(p_scenario.is_valid() && !scenarios.has(p_scenario));
  418. buffers[p_buffer].scenario_rid = p_scenario;
  419. }
  420. void RaycastOcclusionCull::buffer_set_size(RID p_buffer, const Vector2i &p_size) {
  421. ERR_FAIL_COND(!buffers.has(p_buffer));
  422. buffers[p_buffer].resize(p_size);
  423. }
  424. void RaycastOcclusionCull::buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_pool) {
  425. if (!buffers.has(p_buffer)) {
  426. return;
  427. }
  428. RaycastHZBuffer &buffer = buffers[p_buffer];
  429. if (buffer.is_empty() || !scenarios.has(buffer.scenario_rid)) {
  430. return;
  431. }
  432. Scenario &scenario = scenarios[buffer.scenario_rid];
  433. bool removed = scenario.update(p_thread_pool);
  434. if (removed) {
  435. scenarios.erase(buffer.scenario_rid);
  436. return;
  437. }
  438. buffer.update_camera_rays(p_cam_transform, p_cam_projection, p_cam_orthogonal, p_thread_pool);
  439. scenario.raycast(buffer.camera_rays, buffer.camera_ray_masks.ptr(), buffer.camera_rays_tile_count, p_thread_pool);
  440. buffer.sort_rays(-p_cam_transform.basis.get_axis(2), p_cam_orthogonal);
  441. buffer.update_mips();
  442. }
  443. RaycastOcclusionCull::HZBuffer *RaycastOcclusionCull::buffer_get_ptr(RID p_buffer) {
  444. if (!buffers.has(p_buffer)) {
  445. return nullptr;
  446. }
  447. return &buffers[p_buffer];
  448. }
  449. RID RaycastOcclusionCull::buffer_get_debug_texture(RID p_buffer) {
  450. ERR_FAIL_COND_V(!buffers.has(p_buffer), RID());
  451. return buffers[p_buffer].get_debug_texture();
  452. }
  453. ////////////////////////////////////////////////////////
  454. void RaycastOcclusionCull::set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {
  455. if (build_quality == p_quality) {
  456. return;
  457. }
  458. build_quality = p_quality;
  459. const RID *scenario_rid = nullptr;
  460. while ((scenario_rid = scenarios.next(scenario_rid))) {
  461. scenarios[*scenario_rid].dirty = true;
  462. }
  463. }
  464. void RaycastOcclusionCull::_init_embree() {
  465. #ifdef __SSE2__
  466. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  467. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  468. #endif
  469. String settings = vformat("threads=%d", MAX(1, OS::get_singleton()->get_processor_count() - 2));
  470. ebr_device = rtcNewDevice(settings.utf8().ptr());
  471. }
  472. RaycastOcclusionCull::RaycastOcclusionCull() {
  473. raycast_singleton = this;
  474. int default_quality = GLOBAL_GET("rendering/occlusion_culling/bvh_build_quality");
  475. build_quality = RS::ViewportOcclusionCullingBuildQuality(default_quality);
  476. }
  477. RaycastOcclusionCull::~RaycastOcclusionCull() {
  478. const RID *scenario_rid = nullptr;
  479. while ((scenario_rid = scenarios.next(scenario_rid))) {
  480. Scenario &scenario = scenarios[*scenario_rid];
  481. if (scenario.commit_thread) {
  482. scenario.commit_thread->wait_to_finish();
  483. memdelete(scenario.commit_thread);
  484. }
  485. for (int i = 0; i < 2; i++) {
  486. if (scenario.ebr_scene[i]) {
  487. rtcReleaseScene(scenario.ebr_scene[i]);
  488. }
  489. }
  490. }
  491. if (ebr_device != nullptr) {
  492. rtcReleaseDevice(ebr_device);
  493. }
  494. raycast_singleton = nullptr;
  495. }