summaryrefslogtreecommitdiff
path: root/modules/raycast/raycast_occlusion_cull.h
blob: 056b8086407998beef5f587cb9b634d8ce0171ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*************************************************************************/
/*  raycast_occlusion_cull.h                                             */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* 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.                */
/*************************************************************************/

#ifndef RAYCAST_OCCLUSION_CULL_H
#define RAYCAST_OCCLUSION_CULL_H

#include "core/io/image.h"
#include "core/math/projection.h"
#include "core/object/object.h"
#include "core/object/ref_counted.h"
#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 {
	typedef RTCRayHit16 CameraRayTile;

public:
	class RaycastHZBuffer : public HZBuffer {
	private:
		Size2i tile_grid_size;

		struct CameraRayThreadData {
			int thread_count;
			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;
			Size2i buffer_size;
		};

		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);

	public:
		unsigned int camera_rays_tile_count = 0;
		uint8_t *camera_rays_unaligned_buffer = nullptr;
		CameraRayTile *camera_rays = nullptr;
		LocalVector<uint32_t> camera_ray_masks;
		RID scenario_rid;

		virtual void clear() override;
		virtual void resize(const Size2i &p_size) override;
		void sort_rays(const Vector3 &p_camera_dir, bool p_orthogonal);
		void update_camera_rays(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal);

		~RaycastHZBuffer();
	};

private:
	struct InstanceID {
		RID scenario;
		RID instance;

		static uint32_t hash(const InstanceID &p_ins) {
			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));
		}
		bool operator==(const InstanceID &rhs) const {
			return instance == rhs.instance && rhs.scenario == scenario;
			;
		}

		InstanceID() {}
		InstanceID(RID s, RID i) :
				scenario(s), instance(i) {}
	};

	struct Occluder {
		PackedVector3Array vertices;
		PackedInt32Array indices;
		HashSet<InstanceID, InstanceID> users;
	};

	struct OccluderInstance {
		RID occluder;
		LocalVector<uint32_t> indices;
		LocalVector<Vector3> xformed_vertices;
		Transform3D xform;
		bool enabled = true;
		bool removed = false;
	};

	struct Scenario {
		struct RaycastThreadData {
			CameraRayTile *rays = nullptr;
			const uint32_t *masks;
		};

		struct TransformThreadData {
			uint32_t thread_count;
			uint32_t vertex_count;
			Transform3D xform;
			const Vector3 *read;
			Vector3 *write = nullptr;
		};

		Thread *commit_thread = nullptr;
		bool commit_done = true;
		bool dirty = false;
		bool removed = false;

		RTCScene ebr_scene[2] = { nullptr, nullptr };
		int current_scene_idx = 0;

		HashMap<RID, OccluderInstance> instances;
		HashSet<RID> dirty_instances; // To avoid duplicates
		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);
		void _update_dirty_instance(int p_idx, RID *p_instances);
		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);
		static void _commit_scene(void *p_ud);
		bool update();

		void _raycast(uint32_t p_thread, const RaycastThreadData *p_raycast_data) const;
		void raycast(CameraRayTile *r_rays, const uint32_t *p_valid_masks, uint32_t p_tile_count) const;
	};

	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;
	virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) override;
	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;
	virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) override;

	virtual RID buffer_get_debug_texture(RID p_buffer) override;

	virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) override;

	RaycastOcclusionCull();
	~RaycastOcclusionCull();
};

#endif // RAYCAST_OCCLUSION_CULL_H