Path: blob/master/servers/rendering/renderer_scene_occlusion_cull.h
11352 views
/**************************************************************************/1/* renderer_scene_occlusion_cull.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/math/projection.h"33#include "core/templates/local_vector.h"34#include "servers/rendering/rendering_server.h"3536class RendererSceneOcclusionCull {37protected:38static RendererSceneOcclusionCull *singleton;3940public:41class HZBuffer {42protected:43LocalVector<float> data;44LocalVector<Size2i> sizes;45LocalVector<float *> mips;4647RID debug_texture;48Ref<Image> debug_image;49PackedByteArray debug_data;50float debug_tex_range = 0.0f;5152uint64_t occlusion_frame = 0;53Size2i occlusion_buffer_size;5455_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 {56if (is_empty()) {57return false;58}5960Vector3 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]));6162if (closest_point == p_cam_position) {63return false;64}6566Vector3 closest_point_view = p_cam_inv_transform.xform(closest_point);67if (closest_point_view.z > -p_near) {68return false;69}7071// Force distance calculation to use double precision to avoid floating-point overflow for distant objects.72closest_point = closest_point - p_cam_position;73float 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);7475Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX);76Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN);7778for (int j = 0; j < 8; j++) {79// Bitmask to cycle through the corners of the AABB.80Vector3 corner = Vector3(81j & 4 ? p_bounds[0] : p_bounds[3],82j & 2 ? p_bounds[1] : p_bounds[4],83j & 1 ? p_bounds[2] : p_bounds[5]);84Vector3 view = p_cam_inv_transform.xform(corner);8586// When using an orthogonal camera, the closest point of an AABB to the camera is guaranteed to be a corner.87if (p_is_orthogonal) {88min_depth = MIN(min_depth, -view.z);89}9091Vector3 projected = p_cam_projection.xform(view);9293if (-view.z < 0.0) {94rect_min = Vector2(0.0f, 0.0f);95rect_max = Vector2(1.0f, 1.0f);96break;97}9899Vector2 normalized = Vector2(projected.x * 0.5f + 0.5f, projected.y * 0.5f + 0.5f);100rect_min = rect_min.min(normalized);101rect_max = rect_max.max(normalized);102}103104rect_max = rect_max.minf(1);105rect_min = rect_min.maxf(0);106107int mip_count = mips.size();108109Vector2 screen_diagonal = (rect_max - rect_min) * sizes[0];110float size = MAX(screen_diagonal.x, screen_diagonal.y);111float l = Math::ceil(Math::log2(size));112int lod = CLAMP(l, 0, mip_count - 1);113114const int max_samples = 512;115int sample_count = 0;116bool visible = true;117118for (; lod >= 0; lod--) {119int w = sizes[lod].x;120int h = sizes[lod].y;121122int minx = CLAMP(rect_min.x * w - 1, 0, w - 1);123int maxx = CLAMP(rect_max.x * w + 1, 0, w - 1);124125int miny = CLAMP(rect_min.y * h - 1, 0, h - 1);126int maxy = CLAMP(rect_max.y * h + 1, 0, h - 1);127128sample_count += (maxx - minx + 1) * (maxy - miny + 1);129130if (sample_count > max_samples) {131return false;132}133134visible = false;135for (int y = miny; y <= maxy; y++) {136for (int x = minx; x <= maxx; x++) {137float depth = mips[lod][y * w + x];138if (depth > min_depth) {139visible = true;140break;141}142}143if (visible) {144break;145}146}147148if (!visible) {149return true;150}151}152153return !visible;154}155156public:157static bool occlusion_jitter_enabled;158159_FORCE_INLINE_ bool is_empty() const {160return sizes.is_empty();161}162163virtual void clear();164virtual void resize(const Size2i &p_size);165166void update_mips();167168// Thin wrapper around _is_occluded(),169// allowing occlusion timers to delay the disappearance170// of objects to prevent flickering when using jittering.171_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 {172bool occluded = _is_occluded(p_bounds, p_cam_position, p_cam_inv_transform, p_cam_projection, p_near, p_is_orthogonal);173174// Special case, temporal jitter disabled,175// so we don't use occlusion timers.176if (!occlusion_jitter_enabled) {177return occluded;178}179180if (!occluded) {181//#define DEBUG_RASTER_OCCLUSION_JITTER182#ifdef DEBUG_RASTER_OCCLUSION_JITTER183r_occlusion_timeout = occlusion_frame + 1;184#else185r_occlusion_timeout = occlusion_frame + 9;186#endif187} else if (r_occlusion_timeout) {188// Regular timeout, allow occlusion culling189// to proceed as normal after the delay.190if (occlusion_frame >= r_occlusion_timeout) {191r_occlusion_timeout = 0;192}193}194195return occluded && !r_occlusion_timeout;196}197198RID get_debug_texture();199const Size2i &get_occlusion_buffer_size() const { return occlusion_buffer_size; }200201virtual ~HZBuffer() {}202};203204static RendererSceneOcclusionCull *get_singleton() { return singleton; }205206void _print_warning() {207WARN_PRINT_ONCE("Occlusion culling is disabled at build-time.");208}209210virtual bool is_occluder(RID p_rid) { return false; }211virtual RID occluder_allocate() { return RID(); }212virtual void occluder_initialize(RID p_occluder) {}213virtual void free_occluder(RID p_occluder) { _print_warning(); }214virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) { _print_warning(); }215216virtual void add_scenario(RID p_scenario) {}217virtual void remove_scenario(RID p_scenario) {}218virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) { _print_warning(); }219virtual void scenario_remove_instance(RID p_scenario, RID p_instance) { _print_warning(); }220221virtual void add_buffer(RID p_buffer) { _print_warning(); }222virtual void remove_buffer(RID p_buffer) { _print_warning(); }223virtual HZBuffer *buffer_get_ptr(RID p_buffer) {224return nullptr;225}226virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) { _print_warning(); }227virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) { _print_warning(); }228virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {}229230virtual RID buffer_get_debug_texture(RID p_buffer) {231_print_warning();232return RID();233}234235virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {}236237RendererSceneOcclusionCull() {238singleton = this;239}240241virtual ~RendererSceneOcclusionCull() {242singleton = nullptr;243}244};245246247