Path: blob/master/servers/rendering/renderer_scene_occlusion_cull.cpp
10277 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;3334const Vector3 RendererSceneOcclusionCull::HZBuffer::corners[8] = {35Vector3(0, 0, 0),36Vector3(0, 0, 1),37Vector3(0, 1, 0),38Vector3(0, 1, 1),39Vector3(1, 0, 0),40Vector3(1, 0, 1),41Vector3(1, 1, 0),42Vector3(1, 1, 1)43};4445bool RendererSceneOcclusionCull::HZBuffer::occlusion_jitter_enabled = false;4647bool RendererSceneOcclusionCull::HZBuffer::is_empty() const {48return sizes.is_empty();49}5051void RendererSceneOcclusionCull::HZBuffer::clear() {52if (sizes.is_empty()) {53return; // Already cleared54}5556data.clear();57sizes.clear();58mips.clear();5960debug_data.clear();61if (debug_image.is_valid()) {62debug_image.unref();63}6465ERR_FAIL_NULL(RenderingServer::get_singleton());66RS::get_singleton()->free(debug_texture);67}6869void RendererSceneOcclusionCull::HZBuffer::resize(const Size2i &p_size) {70occlusion_buffer_size = p_size;7172if (p_size == Size2i()) {73clear();74return;75}7677if (!sizes.is_empty() && p_size == sizes[0]) {78return; // Size didn't change79}8081int mip_count = 0;82int data_size = 0;83int w = p_size.x;84int h = p_size.y;8586while (true) {87data_size += h * w;8889w = MAX(1, w >> 1);90h = MAX(1, h >> 1);9192mip_count++;9394if (w == 1U && h == 1U) {95data_size += 1U;96mip_count++;97break;98}99}100101data.resize(data_size);102mips.resize(mip_count);103sizes.resize(mip_count);104105w = p_size.x;106h = p_size.y;107float *ptr = data.ptr();108109for (int i = 0; i < mip_count; i++) {110sizes[i] = Size2i(w, h);111mips[i] = ptr;112113ptr = &ptr[w * h];114w = MAX(1, w >> 1);115h = MAX(1, h >> 1);116}117118for (int i = 0; i < data_size; i++) {119data[i] = FLT_MAX;120}121122debug_data.resize(sizes[0].x * sizes[0].y);123if (debug_texture.is_valid()) {124RS::get_singleton()->free(debug_texture);125debug_texture = RID();126}127}128129void RendererSceneOcclusionCull::HZBuffer::update_mips() {130// Keep this up to date as a local to be used for occlusion timers.131occlusion_frame = Engine::get_singleton()->get_frames_drawn();132133if (sizes.is_empty()) {134return;135}136137for (uint32_t mip = 1; mip < mips.size(); mip++) {138for (int y = 0; y < sizes[mip].y; y++) {139for (int x = 0; x < sizes[mip].x; x++) {140int prev_x = x * 2;141int prev_y = y * 2;142143int prev_w = sizes[mip - 1].width;144int prev_h = sizes[mip - 1].height;145146bool odd_w = (prev_w % 2) != 0;147bool odd_h = (prev_h % 2) != 0;148149#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))])150151float max_depth = mips[mip - 1][prev_y * sizes[mip - 1].x + prev_x];152CHECK_OFFSET(0, 1);153CHECK_OFFSET(1, 0);154CHECK_OFFSET(1, 1);155156if (odd_w) {157CHECK_OFFSET(2, 0);158CHECK_OFFSET(2, 1);159}160161if (odd_h) {162CHECK_OFFSET(0, 2);163CHECK_OFFSET(1, 2);164}165166if (odd_w && odd_h) {167CHECK_OFFSET(2, 2);168}169170mips[mip][y * sizes[mip].x + x] = max_depth;171#undef CHECK_OFFSET172}173}174}175}176177RID RendererSceneOcclusionCull::HZBuffer::get_debug_texture() {178if (sizes.is_empty() || sizes[0] == Size2i()) {179return RID();180}181182if (debug_image.is_null()) {183debug_image.instantiate();184}185186unsigned char *ptrw = debug_data.ptrw();187for (int i = 0; i < debug_data.size(); i++) {188ptrw[i] = MIN(Math::log(1.0 + mips[0][i]) / Math::log(1.0 + debug_tex_range), 1.0) * 255;189}190191debug_image->set_data(sizes[0].x, sizes[0].y, false, Image::FORMAT_L8, debug_data);192193if (debug_texture.is_null()) {194debug_texture = RS::get_singleton()->texture_2d_create(debug_image);195} else {196RenderingServer::get_singleton()->texture_2d_update(debug_texture, debug_image);197}198199return debug_texture;200}201202203