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