Optimize octree and fix octree leak
Prevents adding new octants until a limiting number of elements have been added to the current octant. This enables balancing the benefits of brute force against the benefits of spatial partitioning. The limit can be set per octree. Project settings are added for rendering octree to set the best balance per project depending on number of tests per frame / tick, and the amount of editing of the octree. Fixes octants being leaked when removing elements. Optimize octree with cached linear lists Storing elements in octants using linked lists is efficient for housekeeping but very slow for testing. This optimization stores additional local_vectors with Element pointers and AABBs which are cached and only updated when a dirty flag is set on the octant. This is selectable with 2 versions of Octree : Octree and Octree_CL, Octree being the old behaviour. At present the cached list version is only used for the visual server octree (rendering) as it has only been demonstrated to be faster there so far. This uses slightly more memory (probably a few kb in most cases) but can be significantly faster during testing (culling etc). Co-authored-by: Sergey Minakov <naithar@icloud.com>
This commit is contained in:
parent
212744e7a5
commit
667c970b77
8 changed files with 2072 additions and 1352 deletions
|
@ -172,6 +172,19 @@ void _err_print_index_error(const char *p_function, const char *p_file, int p_li
|
|||
} \
|
||||
} while (0); // (*)
|
||||
|
||||
/**
|
||||
* If `m_index` is greater than or equal to `m_size`,
|
||||
* prints a generic error message and returns the value specified in `m_retval`.
|
||||
* This macro should be preferred to `ERR_FAIL_COND_V` for unsigned bounds checking.
|
||||
*/
|
||||
#define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
|
||||
do { \
|
||||
if (unlikely((m_index) >= (m_size))) { \
|
||||
_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
|
||||
return; \
|
||||
} \
|
||||
} while (0); // (*)
|
||||
|
||||
/**
|
||||
* If `m_index` is greater than or equal to `m_size`,
|
||||
* prints a generic error message and returns the value specified in `m_retval`.
|
||||
|
@ -226,6 +239,20 @@ void _err_print_index_error(const char *p_function, const char *p_file, int p_li
|
|||
} \
|
||||
} while (0); // (*)
|
||||
|
||||
/**
|
||||
* If `m_index` is greater than or equal to `m_size`,
|
||||
* crashes the engine immediately with a generic error message.
|
||||
* Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
|
||||
* This macro should be preferred to `CRASH_COND` for bounds checking.
|
||||
*/
|
||||
#define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
|
||||
do { \
|
||||
if (unlikely((m_index) >= (m_size))) { \
|
||||
_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
|
||||
GENERATE_TRAP \
|
||||
} \
|
||||
} while (0); // (*)
|
||||
|
||||
/**
|
||||
* If `m_param` is `null`, prints a generic error message and returns from the function.
|
||||
*/
|
||||
|
|
246
core/local_vector.h
Normal file
246
core/local_vector.h
Normal file
|
@ -0,0 +1,246 @@
|
|||
/*************************************************************************/
|
||||
/* local_vector.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef LOCAL_VECTOR_H
|
||||
#define LOCAL_VECTOR_H
|
||||
|
||||
#include "core/error_macros.h"
|
||||
#include "core/os/copymem.h"
|
||||
#include "core/os/memory.h"
|
||||
#include "core/sort_array.h"
|
||||
#include "core/vector.h"
|
||||
|
||||
template <class T, class U = uint32_t, bool force_trivial = false>
|
||||
class LocalVector {
|
||||
private:
|
||||
U count = 0;
|
||||
U capacity = 0;
|
||||
T *data = nullptr;
|
||||
|
||||
public:
|
||||
T *ptr() {
|
||||
return data;
|
||||
}
|
||||
|
||||
const T *ptr() const {
|
||||
return data;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ void push_back(T p_elem) {
|
||||
if (unlikely(count == capacity)) {
|
||||
if (capacity == 0) {
|
||||
capacity = 1;
|
||||
} else {
|
||||
capacity <<= 1;
|
||||
}
|
||||
data = (T *)memrealloc(data, capacity * sizeof(T));
|
||||
CRASH_COND_MSG(!data, "Out of memory");
|
||||
}
|
||||
|
||||
if (!__has_trivial_constructor(T) && !force_trivial) {
|
||||
memnew_placement(&data[count++], T(p_elem));
|
||||
} else {
|
||||
data[count++] = p_elem;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(U p_index) {
|
||||
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
|
||||
count--;
|
||||
for (U i = p_index; i < count; i++) {
|
||||
data[i] = data[i + 1];
|
||||
}
|
||||
if (!__has_trivial_destructor(T) && !force_trivial) {
|
||||
data[count].~T();
|
||||
}
|
||||
}
|
||||
|
||||
void erase(const T &p_val) {
|
||||
int64_t idx = find(p_val);
|
||||
if (idx >= 0) {
|
||||
remove(idx);
|
||||
}
|
||||
}
|
||||
|
||||
void invert() {
|
||||
for (U i = 0; i < count / 2; i++) {
|
||||
SWAP(data[i], data[count - i - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ void clear() { resize(0); }
|
||||
_FORCE_INLINE_ void reset() {
|
||||
clear();
|
||||
if (data) {
|
||||
memfree(data);
|
||||
data = nullptr;
|
||||
capacity = 0;
|
||||
}
|
||||
}
|
||||
_FORCE_INLINE_ bool empty() const { return count == 0; }
|
||||
_FORCE_INLINE_ void reserve(U p_size) {
|
||||
p_size = nearest_power_of_2_templated(p_size);
|
||||
if (p_size > capacity) {
|
||||
capacity = p_size;
|
||||
data = (T *)memrealloc(data, capacity * sizeof(T));
|
||||
CRASH_COND_MSG(!data, "Out of memory");
|
||||
}
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ U size() const { return count; }
|
||||
void resize(U p_size) {
|
||||
if (p_size < count) {
|
||||
if (!__has_trivial_destructor(T) && !force_trivial) {
|
||||
for (U i = p_size; i < count; i++) {
|
||||
data[i].~T();
|
||||
}
|
||||
}
|
||||
count = p_size;
|
||||
} else if (p_size > count) {
|
||||
if (unlikely(p_size > capacity)) {
|
||||
if (capacity == 0) {
|
||||
capacity = 1;
|
||||
}
|
||||
while (capacity < p_size) {
|
||||
capacity <<= 1;
|
||||
}
|
||||
data = (T *)memrealloc(data, capacity * sizeof(T));
|
||||
CRASH_COND_MSG(!data, "Out of memory");
|
||||
}
|
||||
if (!__has_trivial_constructor(T) && !force_trivial) {
|
||||
for (U i = count; i < p_size; i++) {
|
||||
memnew_placement(&data[i], T);
|
||||
}
|
||||
}
|
||||
count = p_size;
|
||||
}
|
||||
}
|
||||
_FORCE_INLINE_ const T &operator[](U p_index) const {
|
||||
CRASH_BAD_UNSIGNED_INDEX(p_index, count);
|
||||
return data[p_index];
|
||||
}
|
||||
_FORCE_INLINE_ T &operator[](U p_index) {
|
||||
CRASH_BAD_UNSIGNED_INDEX(p_index, count);
|
||||
return data[p_index];
|
||||
}
|
||||
|
||||
void insert(U p_pos, T p_val) {
|
||||
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
|
||||
if (p_pos == count) {
|
||||
push_back(p_val);
|
||||
} else {
|
||||
resize(count + 1);
|
||||
for (U i = count; i > p_pos; i--) {
|
||||
data[i] = data[i - 1];
|
||||
}
|
||||
data[p_pos] = p_val;
|
||||
}
|
||||
}
|
||||
|
||||
int64_t find(const T &p_val, U p_from = 0) const {
|
||||
for (U i = 0; i < count; i++) {
|
||||
if (data[i] == p_val) {
|
||||
return int64_t(i);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class C>
|
||||
void sort_custom() {
|
||||
U len = count;
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
SortArray<T, C> sorter;
|
||||
sorter.sort(data, len);
|
||||
}
|
||||
|
||||
void sort() {
|
||||
sort_custom<_DefaultComparator<T> >();
|
||||
}
|
||||
|
||||
void ordered_insert(T p_val) {
|
||||
U i;
|
||||
for (i = 0; i < count; i++) {
|
||||
if (p_val < data[i]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
insert(i, p_val);
|
||||
}
|
||||
|
||||
operator Vector<T>() const {
|
||||
Vector<T> ret;
|
||||
ret.resize(size());
|
||||
T *w = ret.ptrw();
|
||||
copymem(w, data, sizeof(T) * count);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
|
||||
Vector<uint8_t> ret;
|
||||
ret.resize(count * sizeof(T));
|
||||
uint8_t *w = ret.ptrw();
|
||||
copymem(w, data, sizeof(T) * count);
|
||||
return ret;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ LocalVector() {}
|
||||
_FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
|
||||
resize(p_from.size());
|
||||
for (U i = 0; i < p_from.count; i++) {
|
||||
data[i] = p_from.data[i];
|
||||
}
|
||||
}
|
||||
inline LocalVector &operator=(const LocalVector &p_from) {
|
||||
resize(p_from.size());
|
||||
for (U i = 0; i < p_from.count; i++) {
|
||||
data[i] = p_from.data[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
inline LocalVector &operator=(const Vector<T> &p_from) {
|
||||
resize(p_from.size());
|
||||
for (U i = 0; i < count; i++) {
|
||||
data[i] = p_from[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ ~LocalVector() {
|
||||
if (data) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // LOCAL_VECTOR_H
|
1377
core/math/octree.h
1377
core/math/octree.h
File diff suppressed because it is too large
Load diff
1763
core/math/octree_definition.inc
Normal file
1763
core/math/octree_definition.inc
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1168,6 +1168,10 @@
|
|||
<member name="rendering/quality/shadows/filter_mode.mobile" type="int" setter="" getter="" default="0">
|
||||
Lower-end override for [member rendering/quality/shadows/filter_mode] on mobile devices, due to performance concerns or driver support.
|
||||
</member>
|
||||
<member name="rendering/quality/spatial_partitioning/render_tree_balance" type="float" setter="" getter="" default="0.17">
|
||||
The rendering octree balance can be changed to favor smaller ([code]0[/code]), or larger ([code]1[/code]) branches.
|
||||
Larger branches can increase performance significantly in some projects.
|
||||
</member>
|
||||
<member name="rendering/quality/subsurface_scattering/follow_surface" type="bool" setter="" getter="" default="false">
|
||||
Improves quality of subsurface scattering, but cost significantly increases.
|
||||
</member>
|
||||
|
|
|
@ -260,6 +260,7 @@ RID VisualServerScene::scenario_create() {
|
|||
RID scenario_rid = scenario_owner.make_rid(scenario);
|
||||
scenario->self = scenario_rid;
|
||||
|
||||
scenario->octree.set_balance(GLOBAL_GET("rendering/quality/spatial_partitioning/render_tree_balance"));
|
||||
scenario->octree.set_pair_callback(_instance_pair, this);
|
||||
scenario->octree.set_unpair_callback(_instance_unpair, this);
|
||||
scenario->reflection_probe_shadow_atlas = VSG::scene_render->shadow_atlas_create();
|
||||
|
|
|
@ -108,7 +108,7 @@ public:
|
|||
VS::ScenarioDebugMode debug;
|
||||
RID self;
|
||||
|
||||
Octree<Instance, true> octree;
|
||||
Octree_CL<Instance, true> octree;
|
||||
|
||||
List<Instance *> directional_lights;
|
||||
RID environment;
|
||||
|
|
|
@ -2432,6 +2432,10 @@ VisualServer::VisualServer() {
|
|||
|
||||
GLOBAL_DEF("rendering/quality/filters/use_nearest_mipmap_filter", false);
|
||||
|
||||
const char *sz_balance_render_tree = "rendering/quality/spatial_partitioning/render_tree_balance";
|
||||
GLOBAL_DEF(sz_balance_render_tree, 0.17f);
|
||||
ProjectSettings::get_singleton()->set_custom_property_info(sz_balance_render_tree, PropertyInfo(Variant::REAL, sz_balance_render_tree, PROPERTY_HINT_RANGE, "0.0,1.0,0.01"));
|
||||
|
||||
GLOBAL_DEF("rendering/batching/options/use_batching", true);
|
||||
GLOBAL_DEF_RST("rendering/batching/options/use_batching_in_editor", true);
|
||||
GLOBAL_DEF("rendering/batching/options/single_rect_fallback", false);
|
||||
|
|
Loading…
Reference in a new issue