| 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 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/projection.h" |
| 34 |
#include "core/templates/local_vector.h" |
| 35 |
#include "servers/rendering/rendering_server.h" |
| 36 |
|
| 37 |
class RendererSceneOcclusionCull { |
| 38 |
protected: |
| 39 |
static RendererSceneOcclusionCull *singleton; |
| 40 |
|
| 41 |
public: |
| 42 |
class HZBuffer { |
| 43 |
protected: |
| 44 |
LocalVector<float> data; |
| 45 |
LocalVector<Size2i> sizes; |
| 46 |
LocalVector<float *> mips; |
| 47 |
|
| 48 |
RID debug_texture; |
| 49 |
Ref<Image> debug_image; |
| 50 |
PackedByteArray debug_data; |
| 51 |
float debug_tex_range = 0.0f; |
| 52 |
|
| 53 |
uint64_t occlusion_frame = 0; |
| 54 |
Size2i occlusion_buffer_size; |
| 55 |
|
| 56 |
_FORCE_INLINE_ bool _is_occluded(const real_t p_bounds[6], const Vector3 &p_cam_position, const Transform3D &p_cam_inv_transform, const Projection &p_cam_projection, real_t p_near, bool p_is_orthogonal) const { |
| 57 |
if (is_empty()) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
Vector3 closest_point = p_cam_position.clamp(Vector3(p_bounds[0], p_bounds[1], p_bounds[2]), Vector3(p_bounds[3], p_bounds[4], p_bounds[5])); |
| 62 |
|
| 63 |
if (closest_point == p_cam_position) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
Vector3 closest_point_view = p_cam_inv_transform.xform(closest_point); |
| 68 |
if (closest_point_view.z > -p_near) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
closest_point = closest_point - p_cam_position; |
| 74 |
float min_depth = Math::sqrt((double)closest_point.x * (double)closest_point.x + (double)closest_point.y * (double)closest_point.y + (double)closest_point.z * (double)closest_point.z); |
| 75 |
|
| 76 |
Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX); |
| 77 |
Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN); |
| 78 |
|
| 79 |
for (int j = 0; j < 8; j++) { |
| 80 |
|
| 81 |
Vector3 corner = Vector3( |
| 82 |
j & 4 ? p_bounds[0] : p_bounds[3], |
| 83 |
j & 2 ? p_bounds[1] : p_bounds[4], |
| 84 |
j & 1 ? p_bounds[2] : p_bounds[5]); |
| 85 |
Vector3 view = p_cam_inv_transform.xform(corner); |
| 86 |
|
| 87 |
|
| 88 |
if (p_is_orthogonal) { |
| 89 |
min_depth = MIN(min_depth, -view.z); |
| 90 |
} |
| 91 |
|
| 92 |
Vector3 projected = p_cam_projection.xform(view); |
| 93 |
|
| 94 |
if (-view.z < 0.0) { |
| 95 |
rect_min = Vector2(0.0f, 0.0f); |
| 96 |
rect_max = Vector2(1.0f, 1.0f); |
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
Vector2 normalized = Vector2(projected.x * 0.5f + 0.5f, projected.y * 0.5f + 0.5f); |
| 101 |
rect_min = rect_min.min(normalized); |
| 102 |
rect_max = rect_max.max(normalized); |
| 103 |
} |
| 104 |
|
| 105 |
rect_max = rect_max.minf(1); |
| 106 |
rect_min = rect_min.maxf(0); |
| 107 |
|
| 108 |
int mip_count = mips.size(); |
| 109 |
|
| 110 |
Vector2 screen_diagonal = (rect_max - rect_min) * sizes[0]; |
| 111 |
float size = MAX(screen_diagonal.x, screen_diagonal.y); |
| 112 |
float l = Math::ceil(Math::log2(size)); |
| 113 |
int lod = CLAMP(l, 0, mip_count - 1); |
| 114 |
|
| 115 |
const int max_samples = 512; |
| 116 |
int sample_count = 0; |
| 117 |
bool visible = true; |
| 118 |
|
| 119 |
for (; lod >= 0; lod--) { |
| 120 |
int w = sizes[lod].x; |
| 121 |
int h = sizes[lod].y; |
| 122 |
|
| 123 |
int minx = CLAMP(rect_min.x * w - 1, 0, w - 1); |
| 124 |
int maxx = CLAMP(rect_max.x * w + 1, 0, w - 1); |
| 125 |
|
| 126 |
int miny = CLAMP(rect_min.y * h - 1, 0, h - 1); |
| 127 |
int maxy = CLAMP(rect_max.y * h + 1, 0, h - 1); |
| 128 |
|
| 129 |
sample_count += (maxx - minx + 1) * (maxy - miny + 1); |
| 130 |
|
| 131 |
if (sample_count > max_samples) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
visible = false; |
| 136 |
for (int y = miny; y <= maxy; y++) { |
| 137 |
for (int x = minx; x <= maxx; x++) { |
| 138 |
float depth = mips[lod][y * w + x]; |
| 139 |
if (depth > min_depth) { |
| 140 |
visible = true; |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
if (visible) { |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if (!visible) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return !visible; |
| 155 |
} |
| 156 |
|
| 157 |
public: |
| 158 |
static bool occlusion_jitter_enabled; |
| 159 |
|
| 160 |
_FORCE_INLINE_ bool is_empty() const { |
| 161 |
return sizes.is_empty(); |
| 162 |
} |
| 163 |
|
| 164 |
virtual void clear(); |
| 165 |
virtual void resize(const Size2i &p_size); |
| 166 |
|
| 167 |
void update_mips(); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
_FORCE_INLINE_ bool is_occluded(const real_t p_bounds[6], const Vector3 &p_cam_position, const Transform3D &p_cam_inv_transform, const Projection &p_cam_projection, real_t p_near, bool p_is_orthogonal, uint64_t &r_occlusion_timeout) const { |
| 173 |
bool occluded = _is_occluded(p_bounds, p_cam_position, p_cam_inv_transform, p_cam_projection, p_near, p_is_orthogonal); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (!occlusion_jitter_enabled) { |
| 178 |
return occluded; |
| 179 |
} |
| 180 |
|
| 181 |
if (!occluded) { |
| 182 |
|
| 183 |
#ifdef DEBUG_RASTER_OCCLUSION_JITTER |
| 184 |
r_occlusion_timeout = occlusion_frame + 1; |
| 185 |
#else |
| 186 |
r_occlusion_timeout = occlusion_frame + 9; |
| 187 |
#endif |
| 188 |
} else if (r_occlusion_timeout) { |
| 189 |
|
| 190 |
|
| 191 |
if (occlusion_frame >= r_occlusion_timeout) { |
| 192 |
r_occlusion_timeout = 0; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return occluded && !r_occlusion_timeout; |
| 197 |
} |
| 198 |
|
| 199 |
RID get_debug_texture(); |
| 200 |
const Size2i &get_occlusion_buffer_size() const { return occlusion_buffer_size; } |
| 201 |
|
| 202 |
virtual ~HZBuffer() {} |
| 203 |
}; |
| 204 |
|
| 205 |
static RendererSceneOcclusionCull *get_singleton() { return singleton; } |
| 206 |
|
| 207 |
void _print_warning() { |
| 208 |
WARN_PRINT_ONCE("Occlusion culling is disabled at build-time."); |
| 209 |
} |
| 210 |
|
| 211 |
virtual bool is_occluder(RID p_rid) { return false; } |
| 212 |
virtual RID occluder_allocate() { return RID(); } |
| 213 |
virtual void occluder_initialize(RID p_occluder) {} |
| 214 |
virtual void free_occluder(RID p_occluder) { _print_warning(); } |
| 215 |
virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) { _print_warning(); } |
| 216 |
|
| 217 |
virtual void add_scenario(RID p_scenario) {} |
| 218 |
virtual void remove_scenario(RID p_scenario) {} |
| 219 |
virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) { _print_warning(); } |
| 220 |
virtual void scenario_remove_instance(RID p_scenario, RID p_instance) { _print_warning(); } |
| 221 |
|
| 222 |
virtual void add_buffer(RID p_buffer) { _print_warning(); } |
| 223 |
virtual void remove_buffer(RID p_buffer) { _print_warning(); } |
| 224 |
virtual HZBuffer *buffer_get_ptr(RID p_buffer) { |
| 225 |
return nullptr; |
| 226 |
} |
| 227 |
virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) { _print_warning(); } |
| 228 |
virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) { _print_warning(); } |
| 229 |
virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {} |
| 230 |
|
| 231 |
virtual RID buffer_get_debug_texture(RID p_buffer) { |
| 232 |
_print_warning(); |
| 233 |
return RID(); |
| 234 |
} |
| 235 |
|
| 236 |
virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {} |
| 237 |
|
| 238 |
RendererSceneOcclusionCull() { |
| 239 |
singleton = this; |
| 240 |
} |
| 241 |
|
| 242 |
virtual ~RendererSceneOcclusionCull() { |
| 243 |
singleton = nullptr; |
| 244 |
} |
| 245 |
}; |