2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* raycast_occlusion_cull.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2021-04-20 18:40:24 +02:00
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#ifndef RAYCAST_OCCLUSION_CULL_H
|
|
|
|
#define RAYCAST_OCCLUSION_CULL_H
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
#include "core/io/image.h"
|
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
|
|
|
#include "core/math/projection.h"
|
2021-04-20 18:40:24 +02:00
|
|
|
#include "core/object/object.h"
|
2021-06-04 18:03:15 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2021-04-20 18:40:24 +02:00
|
|
|
#include "core/templates/local_vector.h"
|
|
|
|
#include "core/templates/rid_owner.h"
|
|
|
|
#include "scene/resources/mesh.h"
|
|
|
|
#include "servers/rendering/renderer_scene_occlusion_cull.h"
|
|
|
|
|
|
|
|
#include <embree3/rtcore.h>
|
|
|
|
|
|
|
|
class RaycastOcclusionCull : public RendererSceneOcclusionCull {
|
2021-09-30 01:53:19 +02:00
|
|
|
typedef RTCRayHit16 CameraRayTile;
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
class RaycastHZBuffer : public HZBuffer {
|
|
|
|
private:
|
2021-09-30 01:53:19 +02:00
|
|
|
Size2i tile_grid_size;
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
struct CameraRayThreadData {
|
|
|
|
int thread_count;
|
2021-09-10 18:15:10 +02:00
|
|
|
float z_near;
|
|
|
|
float z_far;
|
|
|
|
Vector3 camera_dir;
|
|
|
|
Vector3 camera_pos;
|
|
|
|
Vector3 pixel_corner;
|
|
|
|
Vector3 pixel_u_interp;
|
|
|
|
Vector3 pixel_v_interp;
|
|
|
|
bool camera_orthogonal;
|
2021-04-20 18:40:24 +02:00
|
|
|
Size2i buffer_size;
|
|
|
|
};
|
|
|
|
|
2021-09-10 18:15:10 +02:00
|
|
|
void _camera_rays_threaded(uint32_t p_thread, const CameraRayThreadData *p_data);
|
|
|
|
void _generate_camera_rays(const CameraRayThreadData *p_data, int p_from, int p_to);
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
public:
|
2021-09-30 01:53:19 +02:00
|
|
|
unsigned int camera_rays_tile_count = 0;
|
|
|
|
uint8_t *camera_rays_unaligned_buffer = nullptr;
|
|
|
|
CameraRayTile *camera_rays = nullptr;
|
2021-04-20 18:40:24 +02:00
|
|
|
LocalVector<uint32_t> camera_ray_masks;
|
|
|
|
RID scenario_rid;
|
|
|
|
|
|
|
|
virtual void clear() override;
|
|
|
|
virtual void resize(const Size2i &p_size) override;
|
2021-09-10 18:15:10 +02:00
|
|
|
void sort_rays(const Vector3 &p_camera_dir, bool p_orthogonal);
|
2022-07-23 19:12:41 +02:00
|
|
|
void update_camera_rays(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal);
|
2021-09-30 01:53:19 +02:00
|
|
|
|
|
|
|
~RaycastHZBuffer();
|
2021-04-20 18:40:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct InstanceID {
|
|
|
|
RID scenario;
|
|
|
|
RID instance;
|
|
|
|
|
2022-05-19 17:00:06 +02:00
|
|
|
static uint32_t hash(const InstanceID &p_ins) {
|
2022-06-18 16:20:55 +02:00
|
|
|
uint32_t h = hash_murmur3_one_64(p_ins.scenario.get_id());
|
|
|
|
return hash_fmix32(hash_murmur3_one_64(p_ins.instance.get_id(), h));
|
2022-05-19 17:00:06 +02:00
|
|
|
}
|
|
|
|
bool operator==(const InstanceID &rhs) const {
|
|
|
|
return instance == rhs.instance && rhs.scenario == scenario;
|
|
|
|
;
|
2021-04-20 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InstanceID() {}
|
|
|
|
InstanceID(RID s, RID i) :
|
|
|
|
scenario(s), instance(i) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Occluder {
|
|
|
|
PackedVector3Array vertices;
|
|
|
|
PackedInt32Array indices;
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<InstanceID, InstanceID> users;
|
2021-04-20 18:40:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct OccluderInstance {
|
|
|
|
RID occluder;
|
|
|
|
LocalVector<uint32_t> indices;
|
|
|
|
LocalVector<Vector3> xformed_vertices;
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D xform;
|
2021-04-20 18:40:24 +02:00
|
|
|
bool enabled = true;
|
|
|
|
bool removed = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Scenario {
|
|
|
|
struct RaycastThreadData {
|
2022-04-04 15:06:57 +02:00
|
|
|
CameraRayTile *rays = nullptr;
|
2021-04-20 18:40:24 +02:00
|
|
|
const uint32_t *masks;
|
|
|
|
};
|
|
|
|
|
2023-07-27 13:41:27 +02:00
|
|
|
struct TransformThreadData {
|
|
|
|
uint32_t thread_count;
|
|
|
|
uint32_t vertex_count;
|
|
|
|
Transform3D xform;
|
|
|
|
const Vector3 *read;
|
|
|
|
Vector3 *write = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
Thread *commit_thread = nullptr;
|
|
|
|
bool commit_done = true;
|
|
|
|
bool dirty = false;
|
|
|
|
|
|
|
|
RTCScene ebr_scene[2] = { nullptr, nullptr };
|
|
|
|
int current_scene_idx = 0;
|
|
|
|
|
|
|
|
HashMap<RID, OccluderInstance> instances;
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<RID> dirty_instances; // To avoid duplicates
|
2021-04-20 18:40:24 +02:00
|
|
|
LocalVector<RID> dirty_instances_array; // To iterate and split into threads
|
|
|
|
LocalVector<RID> removed_instances;
|
|
|
|
|
|
|
|
void _update_dirty_instance_thread(int p_idx, RID *p_instances);
|
2022-07-23 19:12:41 +02:00
|
|
|
void _update_dirty_instance(int p_idx, RID *p_instances);
|
2023-07-27 13:41:27 +02:00
|
|
|
void _transform_vertices_thread(uint32_t p_thread, TransformThreadData *p_data);
|
|
|
|
void _transform_vertices_range(const Vector3 *p_read, Vector3 *p_write, const Transform3D &p_xform, int p_from, int p_to);
|
2021-04-20 18:40:24 +02:00
|
|
|
static void _commit_scene(void *p_ud);
|
2023-09-26 20:53:17 +02:00
|
|
|
void free();
|
|
|
|
void update();
|
2021-04-20 18:40:24 +02:00
|
|
|
|
|
|
|
void _raycast(uint32_t p_thread, const RaycastThreadData *p_raycast_data) const;
|
2022-07-23 19:12:41 +02:00
|
|
|
void raycast(CameraRayTile *r_rays, const uint32_t *p_valid_masks, uint32_t p_tile_count) const;
|
2021-04-20 18:40:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static RaycastOcclusionCull *raycast_singleton;
|
|
|
|
|
|
|
|
static const int TILE_SIZE = 4;
|
|
|
|
static const int TILE_RAYS = TILE_SIZE * TILE_SIZE;
|
|
|
|
|
|
|
|
RTCDevice ebr_device = nullptr;
|
|
|
|
RID_PtrOwner<Occluder> occluder_owner;
|
|
|
|
HashMap<RID, Scenario> scenarios;
|
|
|
|
HashMap<RID, RaycastHZBuffer> buffers;
|
|
|
|
RS::ViewportOcclusionCullingBuildQuality build_quality;
|
|
|
|
|
|
|
|
void _init_embree();
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual bool is_occluder(RID p_rid) override;
|
|
|
|
virtual RID occluder_allocate() override;
|
|
|
|
virtual void occluder_initialize(RID p_occluder) override;
|
|
|
|
virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) override;
|
|
|
|
virtual void free_occluder(RID p_occluder) override;
|
|
|
|
|
|
|
|
virtual void add_scenario(RID p_scenario) override;
|
|
|
|
virtual void remove_scenario(RID p_scenario) override;
|
2020-10-17 07:08:21 +02:00
|
|
|
virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) override;
|
2021-04-20 18:40:24 +02:00
|
|
|
virtual void scenario_remove_instance(RID p_scenario, RID p_instance) override;
|
|
|
|
|
|
|
|
virtual void add_buffer(RID p_buffer) override;
|
|
|
|
virtual void remove_buffer(RID p_buffer) override;
|
|
|
|
virtual HZBuffer *buffer_get_ptr(RID p_buffer) override;
|
|
|
|
virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) override;
|
|
|
|
virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) override;
|
2022-07-23 19:12:41 +02:00
|
|
|
virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) override;
|
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
virtual RID buffer_get_debug_texture(RID p_buffer) override;
|
|
|
|
|
|
|
|
virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) override;
|
|
|
|
|
|
|
|
RaycastOcclusionCull();
|
|
|
|
~RaycastOcclusionCull();
|
|
|
|
};
|
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#endif // RAYCAST_OCCLUSION_CULL_H
|