2021-04-20 18:40:24 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* raycast_occlusion_cull.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#include "raycast_occlusion_cull.h"
|
2023-06-13 16:56:21 +02:00
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
#include "core/config/project_settings.h"
|
2022-07-23 19:12:41 +02:00
|
|
|
#include "core/object/worker_thread_pool.h"
|
2021-04-20 18:40:24 +02:00
|
|
|
#include "core/templates/local_vector.h"
|
|
|
|
|
|
|
|
#ifdef __SSE2__
|
|
|
|
#include <pmmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RaycastOcclusionCull *RaycastOcclusionCull::raycast_singleton = nullptr;
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::RaycastHZBuffer::clear() {
|
|
|
|
HZBuffer::clear();
|
|
|
|
|
2021-09-30 01:53:19 +02:00
|
|
|
if (camera_rays_unaligned_buffer) {
|
|
|
|
memfree(camera_rays_unaligned_buffer);
|
|
|
|
camera_rays_unaligned_buffer = nullptr;
|
|
|
|
camera_rays = nullptr;
|
|
|
|
}
|
2021-04-20 18:40:24 +02:00
|
|
|
camera_ray_masks.clear();
|
2021-09-30 01:53:19 +02:00
|
|
|
camera_rays_tile_count = 0;
|
|
|
|
tile_grid_size = Size2i();
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::RaycastHZBuffer::resize(const Size2i &p_size) {
|
|
|
|
if (p_size == Size2i()) {
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sizes.is_empty() && p_size == sizes[0]) {
|
|
|
|
return; // Size didn't change
|
|
|
|
}
|
|
|
|
|
|
|
|
HZBuffer::resize(p_size);
|
|
|
|
|
2021-09-30 01:53:19 +02:00
|
|
|
tile_grid_size = Size2i(Math::ceil(p_size.x / (float)TILE_SIZE), Math::ceil(p_size.y / (float)TILE_SIZE));
|
|
|
|
camera_rays_tile_count = tile_grid_size.x * tile_grid_size.y;
|
|
|
|
|
|
|
|
if (camera_rays_unaligned_buffer) {
|
|
|
|
memfree(camera_rays_unaligned_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int alignment = 64; // Embree requires ray packets to be 64-aligned
|
|
|
|
camera_rays_unaligned_buffer = (uint8_t *)memalloc(camera_rays_tile_count * sizeof(CameraRayTile) + alignment);
|
|
|
|
camera_rays = (CameraRayTile *)(camera_rays_unaligned_buffer + alignment - (((uint64_t)camera_rays_unaligned_buffer) % alignment));
|
|
|
|
|
|
|
|
camera_ray_masks.resize(camera_rays_tile_count * TILE_RAYS);
|
|
|
|
memset(camera_ray_masks.ptr(), ~0, camera_rays_tile_count * TILE_RAYS * sizeof(uint32_t));
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
void RaycastOcclusionCull::RaycastHZBuffer::update_camera_rays(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {
|
2021-04-20 18:40:24 +02:00
|
|
|
CameraRayThreadData td;
|
2022-07-23 19:12:41 +02:00
|
|
|
td.thread_count = WorkerThreadPool::get_singleton()->get_thread_count();
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2021-09-10 18:15:10 +02:00
|
|
|
td.z_near = p_cam_projection.get_z_near();
|
|
|
|
td.z_far = p_cam_projection.get_z_far() * 1.05f;
|
|
|
|
td.camera_pos = p_cam_transform.origin;
|
2022-05-03 14:50:35 +02:00
|
|
|
td.camera_dir = -p_cam_transform.basis.get_column(2);
|
2021-09-10 18:15:10 +02:00
|
|
|
td.camera_orthogonal = p_cam_orthogonal;
|
|
|
|
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-20 01:11:13 +02:00
|
|
|
Projection inv_camera_matrix = p_cam_projection.inverse();
|
2021-09-10 18:15:10 +02:00
|
|
|
Vector3 camera_corner_proj = Vector3(-1.0f, -1.0f, -1.0f);
|
|
|
|
Vector3 camera_corner_view = inv_camera_matrix.xform(camera_corner_proj);
|
|
|
|
td.pixel_corner = p_cam_transform.xform(camera_corner_view);
|
|
|
|
|
|
|
|
Vector3 top_corner_proj = Vector3(-1.0f, 1.0f, -1.0f);
|
|
|
|
Vector3 top_corner_view = inv_camera_matrix.xform(top_corner_proj);
|
|
|
|
Vector3 top_corner_world = p_cam_transform.xform(top_corner_view);
|
|
|
|
|
|
|
|
Vector3 left_corner_proj = Vector3(1.0f, -1.0f, -1.0f);
|
|
|
|
Vector3 left_corner_view = inv_camera_matrix.xform(left_corner_proj);
|
|
|
|
Vector3 left_corner_world = p_cam_transform.xform(left_corner_view);
|
|
|
|
|
|
|
|
td.pixel_u_interp = left_corner_world - td.pixel_corner;
|
|
|
|
td.pixel_v_interp = top_corner_world - td.pixel_corner;
|
|
|
|
|
|
|
|
debug_tex_range = td.z_far;
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &RaycastHZBuffer::_camera_rays_threaded, &td, td.thread_count, -1, true, SNAME("RaycastOcclusionCullUpdateCamera"));
|
|
|
|
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-10 18:15:10 +02:00
|
|
|
void RaycastOcclusionCull::RaycastHZBuffer::_camera_rays_threaded(uint32_t p_thread, const CameraRayThreadData *p_data) {
|
2021-09-30 01:53:19 +02:00
|
|
|
uint32_t total_tiles = camera_rays_tile_count;
|
2021-04-20 18:40:24 +02:00
|
|
|
uint32_t total_threads = p_data->thread_count;
|
2021-09-30 01:53:19 +02:00
|
|
|
uint32_t from = p_thread * total_tiles / total_threads;
|
|
|
|
uint32_t to = (p_thread + 1 == total_threads) ? total_tiles : ((p_thread + 1) * total_tiles / total_threads);
|
2021-09-10 18:15:10 +02:00
|
|
|
_generate_camera_rays(p_data, from, to);
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-10 18:15:10 +02:00
|
|
|
void RaycastOcclusionCull::RaycastHZBuffer::_generate_camera_rays(const CameraRayThreadData *p_data, int p_from, int p_to) {
|
|
|
|
const Size2i &buffer_size = sizes[0];
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
for (int i = p_from; i < p_to; i++) {
|
2021-09-30 01:53:19 +02:00
|
|
|
CameraRayTile &tile = camera_rays[i];
|
|
|
|
int tile_x = (i % tile_grid_size.x) * TILE_SIZE;
|
|
|
|
int tile_y = (i / tile_grid_size.x) * TILE_SIZE;
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < TILE_RAYS; j++) {
|
2021-09-10 18:15:10 +02:00
|
|
|
int x = tile_x + j % TILE_SIZE;
|
|
|
|
int y = tile_y + j / TILE_SIZE;
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2021-09-10 18:15:10 +02:00
|
|
|
float u = (float(x) + 0.5f) / buffer_size.x;
|
|
|
|
float v = (float(y) + 0.5f) / buffer_size.y;
|
|
|
|
Vector3 pixel_pos = p_data->pixel_corner + u * p_data->pixel_u_interp + v * p_data->pixel_v_interp;
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2021-09-30 01:53:19 +02:00
|
|
|
tile.ray.tnear[j] = p_data->z_near;
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2021-09-10 18:15:10 +02:00
|
|
|
Vector3 dir;
|
|
|
|
if (p_data->camera_orthogonal) {
|
|
|
|
dir = -p_data->camera_dir;
|
2021-09-30 01:53:19 +02:00
|
|
|
tile.ray.org_x[j] = pixel_pos.x - dir.x * p_data->z_near;
|
|
|
|
tile.ray.org_y[j] = pixel_pos.y - dir.y * p_data->z_near;
|
|
|
|
tile.ray.org_z[j] = pixel_pos.z - dir.z * p_data->z_near;
|
2021-09-10 18:15:10 +02:00
|
|
|
} else {
|
|
|
|
dir = (pixel_pos - p_data->camera_pos).normalized();
|
2021-09-30 01:53:19 +02:00
|
|
|
tile.ray.org_x[j] = p_data->camera_pos.x;
|
|
|
|
tile.ray.org_y[j] = p_data->camera_pos.y;
|
|
|
|
tile.ray.org_z[j] = p_data->camera_pos.z;
|
|
|
|
tile.ray.tnear[j] /= dir.dot(p_data->camera_dir);
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:53:19 +02:00
|
|
|
tile.ray.dir_x[j] = dir.x;
|
|
|
|
tile.ray.dir_y[j] = dir.y;
|
|
|
|
tile.ray.dir_z[j] = dir.z;
|
2021-09-10 18:15:10 +02:00
|
|
|
|
2021-09-30 01:53:19 +02:00
|
|
|
tile.ray.tfar[j] = p_data->z_far;
|
|
|
|
tile.ray.time[j] = 0.0f;
|
2021-09-10 18:15:10 +02:00
|
|
|
|
2021-09-30 01:53:19 +02:00
|
|
|
tile.ray.flags[j] = 0;
|
|
|
|
tile.ray.mask[j] = ~0U;
|
|
|
|
tile.hit.geomID[j] = RTC_INVALID_GEOMETRY_ID;
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 18:15:10 +02:00
|
|
|
void RaycastOcclusionCull::RaycastHZBuffer::sort_rays(const Vector3 &p_camera_dir, bool p_orthogonal) {
|
|
|
|
ERR_FAIL_COND(is_empty());
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
Size2i buffer_size = sizes[0];
|
2021-09-30 01:53:19 +02:00
|
|
|
for (int i = 0; i < tile_grid_size.y; i++) {
|
|
|
|
for (int j = 0; j < tile_grid_size.x; j++) {
|
2021-04-20 18:40:24 +02:00
|
|
|
for (int tile_i = 0; tile_i < TILE_SIZE; tile_i++) {
|
|
|
|
for (int tile_j = 0; tile_j < TILE_SIZE; tile_j++) {
|
|
|
|
int x = j * TILE_SIZE + tile_j;
|
|
|
|
int y = i * TILE_SIZE + tile_i;
|
|
|
|
if (x >= buffer_size.x || y >= buffer_size.y) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int k = tile_i * TILE_SIZE + tile_j;
|
2021-09-30 01:53:19 +02:00
|
|
|
int tile_index = i * tile_grid_size.x + j;
|
|
|
|
float d = camera_rays[tile_index].ray.tfar[k];
|
2021-09-10 18:15:10 +02:00
|
|
|
|
|
|
|
if (!p_orthogonal) {
|
2021-09-30 01:53:19 +02:00
|
|
|
const float &dir_x = camera_rays[tile_index].ray.dir_x[k];
|
|
|
|
const float &dir_y = camera_rays[tile_index].ray.dir_y[k];
|
|
|
|
const float &dir_z = camera_rays[tile_index].ray.dir_z[k];
|
2021-09-10 18:15:10 +02:00
|
|
|
float cos_theta = p_camera_dir.x * dir_x + p_camera_dir.y * dir_y + p_camera_dir.z * dir_z;
|
|
|
|
d *= cos_theta;
|
|
|
|
}
|
|
|
|
|
|
|
|
mips[0][y * buffer_size.x + x] = d;
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:53:19 +02:00
|
|
|
RaycastOcclusionCull::RaycastHZBuffer::~RaycastHZBuffer() {
|
|
|
|
if (camera_rays_unaligned_buffer) {
|
|
|
|
memfree(camera_rays_unaligned_buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool RaycastOcclusionCull::is_occluder(RID p_rid) {
|
|
|
|
return occluder_owner.owns(p_rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
RID RaycastOcclusionCull::occluder_allocate() {
|
|
|
|
return occluder_owner.allocate_rid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::occluder_initialize(RID p_occluder) {
|
|
|
|
Occluder *occluder = memnew(Occluder);
|
|
|
|
occluder_owner.initialize_rid(p_occluder, occluder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) {
|
2021-09-29 19:08:41 +02:00
|
|
|
Occluder *occluder = occluder_owner.get_or_null(p_occluder);
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL(occluder);
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
occluder->vertices = p_vertices;
|
|
|
|
occluder->indices = p_indices;
|
|
|
|
|
2022-05-19 01:43:40 +02:00
|
|
|
for (const InstanceID &E : occluder->users) {
|
|
|
|
RID scenario_rid = E.scenario;
|
|
|
|
RID instance_rid = E.instance;
|
2021-04-20 18:40:24 +02:00
|
|
|
ERR_CONTINUE(!scenarios.has(scenario_rid));
|
|
|
|
Scenario &scenario = scenarios[scenario_rid];
|
|
|
|
ERR_CONTINUE(!scenario.instances.has(instance_rid));
|
|
|
|
|
|
|
|
if (!scenario.dirty_instances.has(instance_rid)) {
|
|
|
|
scenario.dirty_instances.insert(instance_rid);
|
|
|
|
scenario.dirty_instances_array.push_back(instance_rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::free_occluder(RID p_occluder) {
|
2021-09-29 19:08:41 +02:00
|
|
|
Occluder *occluder = occluder_owner.get_or_null(p_occluder);
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL(occluder);
|
2021-04-20 18:40:24 +02:00
|
|
|
memdelete(occluder);
|
|
|
|
occluder_owner.free(p_occluder);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::add_scenario(RID p_scenario) {
|
2023-09-26 20:53:17 +02:00
|
|
|
ERR_FAIL_COND(scenarios.has(p_scenario));
|
|
|
|
scenarios[p_scenario] = Scenario();
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::remove_scenario(RID p_scenario) {
|
2023-09-26 20:53:17 +02:00
|
|
|
Scenario *scenario = scenarios.getptr(p_scenario);
|
|
|
|
ERR_FAIL_NULL(scenario);
|
|
|
|
scenario->free();
|
|
|
|
scenarios.erase(p_scenario);
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
void RaycastOcclusionCull::scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) {
|
2021-04-20 18:40:24 +02:00
|
|
|
ERR_FAIL_COND(!scenarios.has(p_scenario));
|
|
|
|
Scenario &scenario = scenarios[p_scenario];
|
|
|
|
|
|
|
|
if (!scenario.instances.has(p_instance)) {
|
|
|
|
scenario.instances[p_instance] = OccluderInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
OccluderInstance &instance = scenario.instances[p_instance];
|
|
|
|
|
2022-02-04 16:28:18 +01:00
|
|
|
bool changed = false;
|
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
if (instance.removed) {
|
|
|
|
instance.removed = false;
|
|
|
|
scenario.removed_instances.erase(p_instance);
|
2022-02-04 16:28:18 +01:00
|
|
|
changed = true; // It was removed and re-added, we might have missed some changes
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.occluder != p_occluder) {
|
2021-09-29 19:08:41 +02:00
|
|
|
Occluder *old_occluder = occluder_owner.get_or_null(instance.occluder);
|
2021-04-20 18:40:24 +02:00
|
|
|
if (old_occluder) {
|
|
|
|
old_occluder->users.erase(InstanceID(p_scenario, p_instance));
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.occluder = p_occluder;
|
|
|
|
|
|
|
|
if (p_occluder.is_valid()) {
|
2021-09-29 19:08:41 +02:00
|
|
|
Occluder *occluder = occluder_owner.get_or_null(p_occluder);
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL(occluder);
|
2021-04-20 18:40:24 +02:00
|
|
|
occluder->users.insert(InstanceID(p_scenario, p_instance));
|
|
|
|
}
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.xform != p_xform) {
|
|
|
|
scenario.instances[p_instance].xform = p_xform;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.enabled != p_enabled) {
|
|
|
|
instance.enabled = p_enabled;
|
|
|
|
scenario.dirty = true; // The scenario needs a scene re-build, but the instance doesn't need update
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed && !scenario.dirty_instances.has(p_instance)) {
|
|
|
|
scenario.dirty_instances.insert(p_instance);
|
|
|
|
scenario.dirty_instances_array.push_back(p_instance);
|
|
|
|
scenario.dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::scenario_remove_instance(RID p_scenario, RID p_instance) {
|
|
|
|
ERR_FAIL_COND(!scenarios.has(p_scenario));
|
|
|
|
Scenario &scenario = scenarios[p_scenario];
|
|
|
|
|
|
|
|
if (scenario.instances.has(p_instance)) {
|
|
|
|
OccluderInstance &instance = scenario.instances[p_instance];
|
|
|
|
|
|
|
|
if (!instance.removed) {
|
2021-09-29 19:08:41 +02:00
|
|
|
Occluder *occluder = occluder_owner.get_or_null(instance.occluder);
|
2021-04-20 18:40:24 +02:00
|
|
|
if (occluder) {
|
|
|
|
occluder->users.erase(InstanceID(p_scenario, p_instance));
|
|
|
|
}
|
|
|
|
|
|
|
|
scenario.removed_instances.push_back(p_instance);
|
|
|
|
instance.removed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::Scenario::_update_dirty_instance_thread(int p_idx, RID *p_instances) {
|
2022-07-23 19:12:41 +02:00
|
|
|
_update_dirty_instance(p_idx, p_instances);
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
void RaycastOcclusionCull::Scenario::_update_dirty_instance(int p_idx, RID *p_instances) {
|
2021-04-20 18:40:24 +02:00
|
|
|
OccluderInstance *occ_inst = instances.getptr(p_instances[p_idx]);
|
|
|
|
|
|
|
|
if (!occ_inst) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-29 19:08:41 +02:00
|
|
|
Occluder *occ = raycast_singleton->occluder_owner.get_or_null(occ_inst->occluder);
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
if (!occ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int vertices_size = occ->vertices.size();
|
|
|
|
|
|
|
|
// Embree requires the last element to be readable by a 16-byte SSE load instruction, so we add padding to be safe.
|
|
|
|
occ_inst->xformed_vertices.resize(vertices_size + 1);
|
|
|
|
|
2023-07-27 13:41:27 +02:00
|
|
|
const Vector3 *read_ptr = occ->vertices.ptr();
|
|
|
|
Vector3 *write_ptr = occ_inst->xformed_vertices.ptr();
|
|
|
|
|
|
|
|
if (vertices_size > 1024) {
|
|
|
|
TransformThreadData td;
|
|
|
|
td.xform = occ_inst->xform;
|
|
|
|
td.read = read_ptr;
|
|
|
|
td.write = write_ptr;
|
|
|
|
td.vertex_count = vertices_size;
|
|
|
|
td.thread_count = WorkerThreadPool::get_singleton()->get_thread_count();
|
|
|
|
WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &Scenario::_transform_vertices_thread, &td, td.thread_count, -1, true, SNAME("RaycastOcclusionCull"));
|
|
|
|
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
_transform_vertices_range(read_ptr, write_ptr, occ_inst->xform, 0, vertices_size);
|
|
|
|
}
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
occ_inst->indices.resize(occ->indices.size());
|
2021-04-28 11:09:20 +02:00
|
|
|
memcpy(occ_inst->indices.ptr(), occ->indices.ptr(), occ->indices.size() * sizeof(int32_t));
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-27 13:41:27 +02:00
|
|
|
void RaycastOcclusionCull::Scenario::_transform_vertices_thread(uint32_t p_thread, TransformThreadData *p_data) {
|
|
|
|
uint32_t vertex_total = p_data->vertex_count;
|
|
|
|
uint32_t total_threads = p_data->thread_count;
|
|
|
|
uint32_t from = p_thread * vertex_total / total_threads;
|
|
|
|
uint32_t to = (p_thread + 1 == total_threads) ? vertex_total : ((p_thread + 1) * vertex_total / total_threads);
|
|
|
|
_transform_vertices_range(p_data->read, p_data->write, p_data->xform, from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::Scenario::_transform_vertices_range(const Vector3 *p_read, Vector3 *p_write, const Transform3D &p_xform, int p_from, int p_to) {
|
|
|
|
for (int i = p_from; i < p_to; i++) {
|
|
|
|
p_write[i] = p_xform.xform(p_read[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 20:53:17 +02:00
|
|
|
void RaycastOcclusionCull::Scenario::free() {
|
|
|
|
if (commit_thread) {
|
|
|
|
if (commit_thread->is_started()) {
|
|
|
|
commit_thread->wait_to_finish();
|
|
|
|
}
|
|
|
|
memdelete(commit_thread);
|
|
|
|
commit_thread = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (ebr_scene[i]) {
|
|
|
|
rtcReleaseScene(ebr_scene[i]);
|
|
|
|
ebr_scene[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
void RaycastOcclusionCull::Scenario::_commit_scene(void *p_ud) {
|
|
|
|
Scenario *scenario = (Scenario *)p_ud;
|
|
|
|
int commit_idx = 1 - (scenario->current_scene_idx);
|
|
|
|
rtcCommitScene(scenario->ebr_scene[commit_idx]);
|
|
|
|
scenario->commit_done = true;
|
|
|
|
}
|
|
|
|
|
2023-09-26 20:53:17 +02:00
|
|
|
void RaycastOcclusionCull::Scenario::update() {
|
|
|
|
ERR_FAIL_NULL(singleton);
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
if (commit_thread == nullptr) {
|
|
|
|
commit_thread = memnew(Thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commit_thread->is_started()) {
|
|
|
|
if (commit_done) {
|
|
|
|
commit_thread->wait_to_finish();
|
|
|
|
current_scene_idx = 1 - current_scene_idx;
|
|
|
|
} else {
|
2023-09-26 20:53:17 +02:00
|
|
|
return;
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dirty && removed_instances.is_empty() && dirty_instances_array.is_empty()) {
|
2023-09-26 20:53:17 +02:00
|
|
|
return;
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 01:24:45 +01:00
|
|
|
for (const RID &scenario : removed_instances) {
|
|
|
|
instances.erase(scenario);
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
if (dirty_instances_array.size() / WorkerThreadPool::get_singleton()->get_thread_count() > 128) {
|
2021-04-20 18:40:24 +02:00
|
|
|
// Lots of instances, use per-instance threading
|
2022-07-23 19:12:41 +02:00
|
|
|
WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &Scenario::_update_dirty_instance_thread, dirty_instances_array.ptr(), dirty_instances_array.size(), -1, true, SNAME("RaycastOcclusionCullUpdate"));
|
|
|
|
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
|
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
} else {
|
|
|
|
// Few instances, use threading on the vertex transforms
|
|
|
|
for (unsigned int i = 0; i < dirty_instances_array.size(); i++) {
|
2022-07-23 19:12:41 +02:00
|
|
|
_update_dirty_instance(i, dirty_instances_array.ptr());
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty_instances.clear();
|
|
|
|
dirty_instances_array.clear();
|
|
|
|
removed_instances.clear();
|
|
|
|
|
|
|
|
if (raycast_singleton->ebr_device == nullptr) {
|
|
|
|
raycast_singleton->_init_embree();
|
|
|
|
}
|
|
|
|
|
|
|
|
int next_scene_idx = 1 - current_scene_idx;
|
|
|
|
RTCScene &next_scene = ebr_scene[next_scene_idx];
|
|
|
|
|
|
|
|
if (next_scene) {
|
|
|
|
rtcReleaseScene(next_scene);
|
|
|
|
}
|
|
|
|
|
|
|
|
next_scene = rtcNewScene(raycast_singleton->ebr_device);
|
|
|
|
rtcSetSceneBuildQuality(next_scene, RTCBuildQuality(raycast_singleton->build_quality));
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
for (const KeyValue<RID, OccluderInstance> &E : instances) {
|
|
|
|
const OccluderInstance *occ_inst = &E.value;
|
|
|
|
const Occluder *occ = raycast_singleton->occluder_owner.get_or_null(occ_inst->occluder);
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
if (!occ || !occ_inst->enabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RTCGeometry geom = rtcNewGeometry(raycast_singleton->ebr_device, RTC_GEOMETRY_TYPE_TRIANGLE);
|
|
|
|
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, occ_inst->xformed_vertices.ptr(), 0, sizeof(Vector3), occ_inst->xformed_vertices.size());
|
|
|
|
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, occ_inst->indices.ptr(), 0, sizeof(uint32_t) * 3, occ_inst->indices.size() / 3);
|
|
|
|
rtcCommitGeometry(geom);
|
|
|
|
rtcAttachGeometry(next_scene, geom);
|
|
|
|
rtcReleaseGeometry(geom);
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty = false;
|
|
|
|
commit_done = false;
|
|
|
|
commit_thread->start(&Scenario::_commit_scene, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::Scenario::_raycast(uint32_t p_idx, const RaycastThreadData *p_raycast_data) const {
|
|
|
|
RTCIntersectContext ctx;
|
|
|
|
rtcInitIntersectContext(&ctx);
|
|
|
|
ctx.flags = RTC_INTERSECT_CONTEXT_FLAG_COHERENT;
|
|
|
|
|
|
|
|
rtcIntersect16((const int *)&p_raycast_data->masks[p_idx * TILE_RAYS], ebr_scene[current_scene_idx], &ctx, &p_raycast_data->rays[p_idx]);
|
|
|
|
}
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
void RaycastOcclusionCull::Scenario::raycast(CameraRayTile *r_rays, const uint32_t *p_valid_masks, uint32_t p_tile_count) const {
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL(singleton);
|
2021-04-20 18:40:24 +02:00
|
|
|
if (raycast_singleton->ebr_device == nullptr) {
|
|
|
|
return; // Embree is initialized on demand when there is some scenario with occluders in it.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ebr_scene[current_scene_idx] == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RaycastThreadData td;
|
2021-09-30 01:53:19 +02:00
|
|
|
td.rays = r_rays;
|
|
|
|
td.masks = p_valid_masks;
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &Scenario::_raycast, &td, p_tile_count, -1, true, SNAME("RaycastOcclusionCullRaycast"));
|
|
|
|
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::add_buffer(RID p_buffer) {
|
|
|
|
ERR_FAIL_COND(buffers.has(p_buffer));
|
|
|
|
buffers[p_buffer] = RaycastHZBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::remove_buffer(RID p_buffer) {
|
|
|
|
ERR_FAIL_COND(!buffers.has(p_buffer));
|
|
|
|
buffers.erase(p_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::buffer_set_scenario(RID p_buffer, RID p_scenario) {
|
|
|
|
ERR_FAIL_COND(!buffers.has(p_buffer));
|
|
|
|
ERR_FAIL_COND(p_scenario.is_valid() && !scenarios.has(p_scenario));
|
|
|
|
buffers[p_buffer].scenario_rid = p_scenario;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::buffer_set_size(RID p_buffer, const Vector2i &p_size) {
|
|
|
|
ERR_FAIL_COND(!buffers.has(p_buffer));
|
|
|
|
buffers[p_buffer].resize(p_size);
|
|
|
|
}
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
void RaycastOcclusionCull::buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {
|
2021-04-20 18:40:24 +02:00
|
|
|
if (!buffers.has(p_buffer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RaycastHZBuffer &buffer = buffers[p_buffer];
|
|
|
|
|
|
|
|
if (buffer.is_empty() || !scenarios.has(buffer.scenario_rid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scenario &scenario = scenarios[buffer.scenario_rid];
|
2023-09-26 20:53:17 +02:00
|
|
|
scenario.update();
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
buffer.update_camera_rays(p_cam_transform, p_cam_projection, p_cam_orthogonal);
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
scenario.raycast(buffer.camera_rays, buffer.camera_ray_masks.ptr(), buffer.camera_rays_tile_count);
|
2022-05-03 14:50:35 +02:00
|
|
|
buffer.sort_rays(-p_cam_transform.basis.get_column(2), p_cam_orthogonal);
|
2021-04-20 18:40:24 +02:00
|
|
|
buffer.update_mips();
|
|
|
|
}
|
|
|
|
|
|
|
|
RaycastOcclusionCull::HZBuffer *RaycastOcclusionCull::buffer_get_ptr(RID p_buffer) {
|
|
|
|
if (!buffers.has(p_buffer)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &buffers[p_buffer];
|
|
|
|
}
|
|
|
|
|
|
|
|
RID RaycastOcclusionCull::buffer_get_debug_texture(RID p_buffer) {
|
|
|
|
ERR_FAIL_COND_V(!buffers.has(p_buffer), RID());
|
|
|
|
return buffers[p_buffer].get_debug_texture();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {
|
|
|
|
if (build_quality == p_quality) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
build_quality = p_quality;
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
for (KeyValue<RID, Scenario> &K : scenarios) {
|
|
|
|
K.value.dirty = true;
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RaycastOcclusionCull::_init_embree() {
|
|
|
|
#ifdef __SSE2__
|
|
|
|
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
|
|
|
|
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
String settings = vformat("threads=%d", MAX(1, OS::get_singleton()->get_processor_count() - 2));
|
|
|
|
ebr_device = rtcNewDevice(settings.utf8().ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
RaycastOcclusionCull::RaycastOcclusionCull() {
|
|
|
|
raycast_singleton = this;
|
|
|
|
int default_quality = GLOBAL_GET("rendering/occlusion_culling/bvh_build_quality");
|
|
|
|
build_quality = RS::ViewportOcclusionCullingBuildQuality(default_quality);
|
|
|
|
}
|
|
|
|
|
|
|
|
RaycastOcclusionCull::~RaycastOcclusionCull() {
|
2022-05-08 10:09:19 +02:00
|
|
|
for (KeyValue<RID, Scenario> &K : scenarios) {
|
2023-09-26 20:53:17 +02:00
|
|
|
K.value.free();
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ebr_device != nullptr) {
|
|
|
|
rtcReleaseDevice(ebr_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
raycast_singleton = nullptr;
|
|
|
|
}
|