Path: blob/master/servers/rendering/renderer_scene_occlusion_cull.cpp
11352 views
/**************************************************************************/1/* renderer_scene_occlusion_cull.cpp */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#include "renderer_scene_occlusion_cull.h"3132RendererSceneOcclusionCull *RendererSceneOcclusionCull::singleton = nullptr;3334bool RendererSceneOcclusionCull::HZBuffer::occlusion_jitter_enabled = false;3536void RendererSceneOcclusionCull::HZBuffer::clear() {37if (sizes.is_empty()) {38return; // Already cleared39}4041data.clear();42sizes.clear();43mips.clear();4445debug_data.clear();46if (debug_image.is_valid()) {47debug_image.unref();48}4950ERR_FAIL_NULL(RenderingServer::get_singleton());51RS::get_singleton()->free_rid(debug_texture);52}5354void RendererSceneOcclusionCull::HZBuffer::resize(const Size2i &p_size) {55occlusion_buffer_size = p_size;5657if (p_size == Size2i()) {58clear();59return;60}6162if (!sizes.is_empty() && p_size == sizes[0]) {63return; // Size didn't change64}6566int mip_count = 0;67int data_size = 0;68int w = p_size.x;69int h = p_size.y;7071while (true) {72data_size += h * w;7374w = MAX(1, w >> 1);75h = MAX(1, h >> 1);7677mip_count++;7879if (w == 1U && h == 1U) {80data_size += 1U;81mip_count++;82break;83}84}8586data.resize(data_size);87mips.resize(mip_count);88sizes.resize(mip_count);8990w = p_size.x;91h = p_size.y;92float *ptr = data.ptr();9394for (int i = 0; i < mip_count; i++) {95sizes[i] = Size2i(w, h);96mips[i] = ptr;9798ptr = &ptr[w * h];99w = MAX(1, w >> 1);100h = MAX(1, h >> 1);101}102103for (int i = 0; i < data_size; i++) {104data[i] = FLT_MAX;105}106107debug_data.resize(sizes[0].x * sizes[0].y);108if (debug_texture.is_valid()) {109RS::get_singleton()->free_rid(debug_texture);110debug_texture = RID();111}112}113114void RendererSceneOcclusionCull::HZBuffer::update_mips() {115// Keep this up to date as a local to be used for occlusion timers.116occlusion_frame = Engine::get_singleton()->get_frames_drawn();117118if (sizes.is_empty()) {119return;120}121122for (uint32_t mip = 1; mip < mips.size(); mip++) {123for (int y = 0; y < sizes[mip].y; y++) {124for (int x = 0; x < sizes[mip].x; x++) {125int prev_x = x * 2;126int prev_y = y * 2;127128int prev_w = sizes[mip - 1].width;129int prev_h = sizes[mip - 1].height;130131bool odd_w = (prev_w % 2) != 0;132bool odd_h = (prev_h % 2) != 0;133134#define CHECK_OFFSET(xx, yy) max_depth = MAX(max_depth, mips[mip - 1][MIN(prev_h - 1, prev_y + (yy)) * prev_w + MIN(prev_w - 1, prev_x + (xx))])135136float max_depth = mips[mip - 1][prev_y * sizes[mip - 1].x + prev_x];137CHECK_OFFSET(0, 1);138CHECK_OFFSET(1, 0);139CHECK_OFFSET(1, 1);140141if (odd_w) {142CHECK_OFFSET(2, 0);143CHECK_OFFSET(2, 1);144}145146if (odd_h) {147CHECK_OFFSET(0, 2);148CHECK_OFFSET(1, 2);149}150151if (odd_w && odd_h) {152CHECK_OFFSET(2, 2);153}154155mips[mip][y * sizes[mip].x + x] = max_depth;156#undef CHECK_OFFSET157}158}159}160}161162RID RendererSceneOcclusionCull::HZBuffer::get_debug_texture() {163if (sizes.is_empty() || sizes[0] == Size2i()) {164return RID();165}166167if (debug_image.is_null()) {168debug_image.instantiate();169}170171unsigned char *ptrw = debug_data.ptrw();172for (int i = 0; i < debug_data.size(); i++) {173ptrw[i] = MIN(Math::log(1.0 + mips[0][i]) / Math::log(1.0 + debug_tex_range), 1.0) * 255;174}175176debug_image->set_data(sizes[0].x, sizes[0].y, false, Image::FORMAT_L8, debug_data);177178if (debug_texture.is_null()) {179debug_texture = RS::get_singleton()->texture_2d_create(debug_image);180} else {181RenderingServer::get_singleton()->texture_2d_update(debug_texture, debug_image);182}183184return debug_texture;185}186187188