Path: blob/master/servers/rendering/renderer_scene_occlusion_cull.h
10277 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_server.h"3536class RendererSceneOcclusionCull {37protected:38static RendererSceneOcclusionCull *singleton;3940public:41class HZBuffer {42protected:43static const Vector3 corners[8];4445LocalVector<float> data;46LocalVector<Size2i> sizes;47LocalVector<float *> mips;4849RID debug_texture;50Ref<Image> debug_image;51PackedByteArray debug_data;52float debug_tex_range = 0.0f;5354uint64_t occlusion_frame = 0;55Size2i occlusion_buffer_size;5657_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) const {58if (is_empty()) {59return false;60}6162Vector3 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]));6364if (closest_point == p_cam_position) {65return false;66}6768Vector3 closest_point_view = p_cam_inv_transform.xform(closest_point);69if (closest_point_view.z > -p_near) {70return false;71}7273// Force distance calculation to use double precision to avoid floating-point overflow for distant objects.74closest_point = closest_point - p_cam_position;75float 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);7677Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX);78Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN);7980for (int j = 0; j < 8; j++) {81const Vector3 &c = RendererSceneOcclusionCull::HZBuffer::corners[j];82Vector3 nc = Vector3(1, 1, 1) - c;83Vector3 corner = Vector3(p_bounds[0] * c.x + p_bounds[3] * nc.x, p_bounds[1] * c.y + p_bounds[4] * nc.y, p_bounds[2] * c.z + p_bounds[5] * nc.z);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_cam_projection.is_orthogonal()) {88min_depth = MIN(min_depth, -view.z);89}9091Plane vp = Plane(view, 1.0);92Plane projected = p_cam_projection.xform4(vp);9394float w = projected.d;95if (w < 1.0) {96rect_min = Vector2(0.0f, 0.0f);97rect_max = Vector2(1.0f, 1.0f);98break;99}100101Vector2 normalized = Vector2(projected.normal.x / w * 0.5f + 0.5f, projected.normal.y / w * 0.5f + 0.5f);102rect_min = rect_min.min(normalized);103rect_max = rect_max.max(normalized);104}105106rect_max = rect_max.minf(1);107rect_min = rect_min.maxf(0);108109int mip_count = mips.size();110111Vector2 screen_diagonal = (rect_max - rect_min) * sizes[0];112float size = MAX(screen_diagonal.x, screen_diagonal.y);113float l = Math::ceil(Math::log2(size));114int lod = CLAMP(l, 0, mip_count - 1);115116const int max_samples = 512;117int sample_count = 0;118bool visible = true;119120for (; lod >= 0; lod--) {121int w = sizes[lod].x;122int h = sizes[lod].y;123124int minx = CLAMP(rect_min.x * w - 1, 0, w - 1);125int maxx = CLAMP(rect_max.x * w + 1, 0, w - 1);126127int miny = CLAMP(rect_min.y * h - 1, 0, h - 1);128int maxy = CLAMP(rect_max.y * h + 1, 0, h - 1);129130sample_count += (maxx - minx + 1) * (maxy - miny + 1);131132if (sample_count > max_samples) {133return false;134}135136visible = false;137for (int y = miny; y <= maxy; y++) {138for (int x = minx; x <= maxx; x++) {139float depth = mips[lod][y * w + x];140if (depth > min_depth) {141visible = true;142break;143}144}145if (visible) {146break;147}148}149150if (!visible) {151return true;152}153}154155return !visible;156}157158public:159static bool occlusion_jitter_enabled;160161bool is_empty() const;162virtual void clear();163virtual void resize(const Size2i &p_size);164165void update_mips();166167// Thin wrapper around _is_occluded(),168// allowing occlusion timers to delay the disappearance169// of objects to prevent flickering when using jittering.170_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, uint64_t &r_occlusion_timeout) const {171bool occluded = _is_occluded(p_bounds, p_cam_position, p_cam_inv_transform, p_cam_projection, p_near);172173// Special case, temporal jitter disabled,174// so we don't use occlusion timers.175if (!occlusion_jitter_enabled) {176return occluded;177}178179if (!occluded) {180//#define DEBUG_RASTER_OCCLUSION_JITTER181#ifdef DEBUG_RASTER_OCCLUSION_JITTER182r_occlusion_timeout = occlusion_frame + 1;183#else184r_occlusion_timeout = occlusion_frame + 9;185#endif186} else if (r_occlusion_timeout) {187// Regular timeout, allow occlusion culling188// to proceed as normal after the delay.189if (occlusion_frame >= r_occlusion_timeout) {190r_occlusion_timeout = 0;191}192}193194return occluded && !r_occlusion_timeout;195}196197RID get_debug_texture();198const Size2i &get_occlusion_buffer_size() const { return occlusion_buffer_size; }199200virtual ~HZBuffer() {}201};202203static RendererSceneOcclusionCull *get_singleton() { return singleton; }204205void _print_warning() {206WARN_PRINT_ONCE("Occlusion culling is disabled at build-time.");207}208209virtual bool is_occluder(RID p_rid) { return false; }210virtual RID occluder_allocate() { return RID(); }211virtual void occluder_initialize(RID p_occluder) {}212virtual void free_occluder(RID p_occluder) { _print_warning(); }213virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) { _print_warning(); }214215virtual void add_scenario(RID p_scenario) {}216virtual void remove_scenario(RID p_scenario) {}217virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) { _print_warning(); }218virtual void scenario_remove_instance(RID p_scenario, RID p_instance) { _print_warning(); }219220virtual void add_buffer(RID p_buffer) { _print_warning(); }221virtual void remove_buffer(RID p_buffer) { _print_warning(); }222virtual HZBuffer *buffer_get_ptr(RID p_buffer) {223return nullptr;224}225virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) { _print_warning(); }226virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) { _print_warning(); }227virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {}228229virtual RID buffer_get_debug_texture(RID p_buffer) {230_print_warning();231return RID();232}233234virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) {}235236RendererSceneOcclusionCull() {237singleton = this;238}239240virtual ~RendererSceneOcclusionCull() {241singleton = nullptr;242}243};244245246