mirror of
https://github.com/Redot-Engine/redot-engine.git
synced 2025-12-06 07:17:42 -05:00
Merge commit godotengine/godot@80a3d205f1
This commit is contained in:
@@ -87,14 +87,6 @@ private:
|
||||
|
||||
// internal helpers
|
||||
|
||||
static _FORCE_INLINE_ SafeNumeric<USize> *_get_refcount_ptr(uint8_t *p_ptr) {
|
||||
return (SafeNumeric<USize> *)(p_ptr + REF_COUNT_OFFSET);
|
||||
}
|
||||
|
||||
static _FORCE_INLINE_ USize *_get_size_ptr(uint8_t *p_ptr) {
|
||||
return (USize *)(p_ptr + SIZE_OFFSET);
|
||||
}
|
||||
|
||||
static _FORCE_INLINE_ T *_get_data_ptr(uint8_t *p_ptr) {
|
||||
return (T *)(p_ptr + DATA_OFFSET);
|
||||
}
|
||||
@@ -148,8 +140,23 @@ private:
|
||||
void _unref();
|
||||
void _ref(const CowData *p_from);
|
||||
void _ref(const CowData &p_from);
|
||||
USize _copy_on_write();
|
||||
Error _realloc(Size p_alloc_size);
|
||||
|
||||
// Ensures that the backing buffer is at least p_size wide, and that this CowData instance is
|
||||
// the only reference to it. The buffer is populated with as many element copies from the old
|
||||
// array as possible.
|
||||
// It is the responsibility of the caller to populate newly allocated space up to p_size.
|
||||
Error _fork_allocate(USize p_size);
|
||||
Error _copy_on_write() { return _fork_allocate(size()); }
|
||||
|
||||
// Allocates a backing array of the given capacity. The reference count is initialized to 1.
|
||||
// It is the responsibility of the caller to populate the array and the new size property.
|
||||
Error _alloc(USize p_alloc_size);
|
||||
|
||||
// Re-allocates the backing array to the given capacity. The reference count is initialized to 1.
|
||||
// It is the responsibility of the caller to populate the array and the new size property.
|
||||
// The caller must also make sure there are no other references to the data, as pointers may
|
||||
// be invalidated.
|
||||
Error _realloc(USize p_alloc_size);
|
||||
|
||||
public:
|
||||
void operator=(const CowData<T> &p_from) { _ref(p_from); }
|
||||
@@ -181,7 +188,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ void clear() { resize(0); }
|
||||
_FORCE_INLINE_ void clear() { _unref(); }
|
||||
_FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; }
|
||||
|
||||
_FORCE_INLINE_ void set(Size p_index, const T &p_elem) {
|
||||
@@ -255,7 +262,8 @@ void CowData<T>::_unref() {
|
||||
_ptr = nullptr;
|
||||
return;
|
||||
}
|
||||
// Clean up.
|
||||
// We had the only reference; destroy the data.
|
||||
|
||||
// First, invalidate our own reference.
|
||||
// NOTE: It is required to do so immediately because it must not be observable outside of this
|
||||
// function after refcount has already been reduced to 0.
|
||||
@@ -273,48 +281,89 @@ void CowData<T>::_unref() {
|
||||
}
|
||||
}
|
||||
|
||||
// free mem
|
||||
// Free memory.
|
||||
Memory::free_static((uint8_t *)prev_ptr - DATA_OFFSET, false);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename CowData<T>::USize CowData<T>::_copy_on_write() {
|
||||
if (!_ptr) {
|
||||
return 0;
|
||||
Error CowData<T>::_fork_allocate(USize p_size) {
|
||||
if (p_size == 0) {
|
||||
// Wants to clean up.
|
||||
_unref();
|
||||
return OK;
|
||||
}
|
||||
|
||||
SafeNumeric<USize> *refc = _get_refcount();
|
||||
USize alloc_size;
|
||||
ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
|
||||
|
||||
USize rc = refc->get();
|
||||
if (unlikely(rc > 1)) {
|
||||
/* in use by more than me */
|
||||
USize current_size = *_get_size();
|
||||
const USize prev_size = size();
|
||||
|
||||
uint8_t *mem_new = (uint8_t *)Memory::alloc_static(_get_alloc_size(current_size) + DATA_OFFSET, false);
|
||||
ERR_FAIL_NULL_V(mem_new, 0);
|
||||
if (!_ptr) {
|
||||
// We had no data before; just allocate a new array.
|
||||
const Error error = _alloc(alloc_size);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
} else if (_get_refcount()->get() == 1) {
|
||||
// Resize in-place.
|
||||
// NOTE: This case is not just an optimization, but required, as some callers depend on
|
||||
// `_copy_on_write()` calls not changing the pointer after the first fork
|
||||
// (e.g. mutable iterators).
|
||||
if (p_size == prev_size) {
|
||||
// We can shortcut here; we don't need to do anything.
|
||||
return OK;
|
||||
}
|
||||
|
||||
SafeNumeric<USize> *_refc_ptr = _get_refcount_ptr(mem_new);
|
||||
USize *_size_ptr = _get_size_ptr(mem_new);
|
||||
T *_data_ptr = _get_data_ptr(mem_new);
|
||||
|
||||
new (_refc_ptr) SafeNumeric<USize>(1); //refcount
|
||||
*(_size_ptr) = current_size; //size
|
||||
|
||||
// initialize new elements
|
||||
if constexpr (std::is_trivially_copyable_v<T>) {
|
||||
memcpy((uint8_t *)_data_ptr, _ptr, current_size * sizeof(T));
|
||||
} else {
|
||||
for (USize i = 0; i < current_size; i++) {
|
||||
memnew_placement(&_data_ptr[i], T(_ptr[i]));
|
||||
// Destroy extraneous elements.
|
||||
if constexpr (!std::is_trivially_destructible_v<T>) {
|
||||
for (USize i = prev_size; i > p_size; i--) {
|
||||
_ptr[i - 1].~T();
|
||||
}
|
||||
}
|
||||
|
||||
_unref();
|
||||
_ptr = _data_ptr;
|
||||
if (alloc_size != _get_alloc_size(prev_size)) {
|
||||
const Error error = _realloc(alloc_size);
|
||||
if (error) {
|
||||
// Out of memory; the current array is still valid though.
|
||||
return error;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Resize by forking.
|
||||
|
||||
rc = 1;
|
||||
// Create a temporary CowData to hold ownership over our _ptr.
|
||||
// It will be used to copy elements from the old buffer over to our new buffer.
|
||||
// At the end of the block, it will be automatically destructed by going out of scope.
|
||||
const CowData prev_data;
|
||||
prev_data._ptr = _ptr;
|
||||
_ptr = nullptr;
|
||||
|
||||
const Error error = _alloc(alloc_size);
|
||||
if (error) {
|
||||
// On failure to allocate, just give up the old data and return.
|
||||
// We could recover our old pointer from prev_data, but by just dropping our data, we
|
||||
// consciously invite early failure for the case that the caller does not handle this
|
||||
// case gracefully.
|
||||
return error;
|
||||
}
|
||||
|
||||
// Copy over elements.
|
||||
const USize copied_element_count = MIN(prev_size, p_size);
|
||||
if (copied_element_count > 0) {
|
||||
if constexpr (std::is_trivially_copyable_v<T>) {
|
||||
memcpy((uint8_t *)_ptr, (uint8_t *)prev_data._ptr, copied_element_count * sizeof(T));
|
||||
} else {
|
||||
for (USize i = 0; i < copied_element_count; i++) {
|
||||
memnew_placement(&_ptr[i], T(prev_data._ptr[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
|
||||
// Set our new size.
|
||||
*_get_size() = p_size;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -322,87 +371,46 @@ template <bool p_ensure_zero>
|
||||
Error CowData<T>::resize(Size p_size) {
|
||||
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
|
||||
|
||||
Size current_size = size();
|
||||
|
||||
if (p_size == current_size) {
|
||||
const Size prev_size = size();
|
||||
if (p_size == prev_size) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
if (p_size == 0) {
|
||||
// Wants to clean up.
|
||||
_unref(); // Resets _ptr to nullptr.
|
||||
return OK;
|
||||
const Error error = _fork_allocate(p_size);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
// possibly changing size, copy on write
|
||||
_copy_on_write();
|
||||
|
||||
USize current_alloc_size = _get_alloc_size(current_size);
|
||||
USize alloc_size;
|
||||
ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
|
||||
|
||||
if (p_size > current_size) {
|
||||
if (alloc_size != current_alloc_size) {
|
||||
if (current_size == 0) {
|
||||
// alloc from scratch
|
||||
uint8_t *mem_new = (uint8_t *)Memory::alloc_static(alloc_size + DATA_OFFSET, false);
|
||||
ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY);
|
||||
|
||||
SafeNumeric<USize> *_refc_ptr = _get_refcount_ptr(mem_new);
|
||||
USize *_size_ptr = _get_size_ptr(mem_new);
|
||||
T *_data_ptr = _get_data_ptr(mem_new);
|
||||
|
||||
new (_refc_ptr) SafeNumeric<USize>(1); //refcount
|
||||
*(_size_ptr) = 0; //size, currently none
|
||||
|
||||
_ptr = _data_ptr;
|
||||
|
||||
} else {
|
||||
const Error error = _realloc(alloc_size);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// construct the newly created elements
|
||||
memnew_arr_placement<p_ensure_zero>(_ptr + current_size, p_size - current_size);
|
||||
|
||||
*_get_size() = p_size;
|
||||
|
||||
} else if (p_size < current_size) {
|
||||
if constexpr (!std::is_trivially_destructible_v<T>) {
|
||||
// deinitialize no longer needed elements
|
||||
for (USize i = p_size; i < *_get_size(); i++) {
|
||||
T *t = &_ptr[i];
|
||||
t->~T();
|
||||
}
|
||||
}
|
||||
|
||||
if (alloc_size != current_alloc_size) {
|
||||
const Error error = _realloc(alloc_size);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
*_get_size() = p_size;
|
||||
if (p_size > prev_size) {
|
||||
memnew_arr_placement<p_ensure_zero>(_ptr + prev_size, p_size - prev_size);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Error CowData<T>::_realloc(Size p_alloc_size) {
|
||||
Error CowData<T>::_alloc(USize p_alloc_size) {
|
||||
uint8_t *mem_new = (uint8_t *)Memory::alloc_static(p_alloc_size + DATA_OFFSET, false);
|
||||
ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY);
|
||||
|
||||
_ptr = _get_data_ptr(mem_new);
|
||||
|
||||
// If we alloc, we're guaranteed to be the only reference.
|
||||
new (_get_refcount()) SafeNumeric<USize>(1);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Error CowData<T>::_realloc(USize p_alloc_size) {
|
||||
uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, p_alloc_size + DATA_OFFSET, false);
|
||||
ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY);
|
||||
|
||||
SafeNumeric<USize> *_refc_ptr = _get_refcount_ptr(mem_new);
|
||||
T *_data_ptr = _get_data_ptr(mem_new);
|
||||
_ptr = _get_data_ptr(mem_new);
|
||||
|
||||
// If we realloc, we're guaranteed to be the only reference.
|
||||
new (_refc_ptr) SafeNumeric<USize>(1);
|
||||
_ptr = _data_ptr;
|
||||
// So the reference was 1 and was copied to be 1 again.
|
||||
DEV_ASSERT(_get_refcount()->get() == 1);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
165
core/templates/fixed_vector.h
Normal file
165
core/templates/fixed_vector.h
Normal file
@@ -0,0 +1,165 @@
|
||||
/**************************************************************************/
|
||||
/* fixed_vector.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* REDOT ENGINE */
|
||||
/* https://redotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2024-present Redot Engine contributors */
|
||||
/* (see REDOT_AUTHORS.md) */
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* A high performance Vector of fixed capacity.
|
||||
* Especially useful if you need to create an array on the stack, to
|
||||
* prevent dynamic allocations (especially in bottleneck code).
|
||||
*
|
||||
* Choose CAPACITY such that it is enough for all elements that could be added through all branches.
|
||||
*
|
||||
*/
|
||||
template <class T, uint32_t CAPACITY>
|
||||
class FixedVector {
|
||||
// This declaration allows us to access other FixedVector's private members.
|
||||
template <class T_, uint32_t CAPACITY_>
|
||||
friend class FixedVector;
|
||||
|
||||
uint32_t _size = 0;
|
||||
alignas(T) uint8_t _data[CAPACITY * sizeof(T)];
|
||||
|
||||
constexpr static uint32_t DATA_PADDING = MAX(alignof(T), alignof(uint32_t)) - alignof(uint32_t);
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ constexpr FixedVector() = default;
|
||||
constexpr FixedVector(std::initializer_list<T> p_init) {
|
||||
ERR_FAIL_COND(p_init.size() > CAPACITY);
|
||||
for (const T &element : p_init) {
|
||||
memnew_placement(ptr() + _size++, T(element));
|
||||
}
|
||||
}
|
||||
|
||||
template <uint32_t p_capacity>
|
||||
constexpr FixedVector(const FixedVector<T, p_capacity> &p_from) {
|
||||
ERR_FAIL_COND(p_from.size() > CAPACITY);
|
||||
if constexpr (std::is_trivially_copyable_v<T>) {
|
||||
// Copy size and all provided elements at once.
|
||||
memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
|
||||
} else {
|
||||
for (const T &element : p_from) {
|
||||
memnew_placement(ptr() + _size++, T(element));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <uint32_t p_capacity>
|
||||
constexpr FixedVector(FixedVector<T, p_capacity> &&p_from) {
|
||||
ERR_FAIL_COND(p_from.size() > CAPACITY);
|
||||
// Copy size and all provided elements at once.
|
||||
// Note: Assumes trivial relocatability.
|
||||
memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
|
||||
p_from._size = 0;
|
||||
}
|
||||
|
||||
~FixedVector() {
|
||||
if constexpr (!std::is_trivially_destructible_v<T>) {
|
||||
for (uint32_t i = 0; i < _size; i++) {
|
||||
ptr()[i].~T();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ constexpr T *ptr() { return (T *)(_data); }
|
||||
_FORCE_INLINE_ constexpr const T *ptr() const { return (const T *)(_data); }
|
||||
|
||||
_FORCE_INLINE_ constexpr operator Span<T>() const { return Span<T>(ptr(), size()); }
|
||||
_FORCE_INLINE_ constexpr Span<T> span() const { return operator Span<T>(); }
|
||||
|
||||
_FORCE_INLINE_ constexpr uint32_t size() const { return _size; }
|
||||
_FORCE_INLINE_ constexpr bool is_empty() const { return !_size; }
|
||||
_FORCE_INLINE_ constexpr bool is_full() const { return _size == CAPACITY; }
|
||||
_FORCE_INLINE_ constexpr uint32_t capacity() const { return CAPACITY; }
|
||||
|
||||
_FORCE_INLINE_ constexpr void clear() { resize_initialized(0); }
|
||||
|
||||
/// Changes the size of the vector.
|
||||
/// If p_size > size(), constructs new elements.
|
||||
/// If p_size < size(), destructs new elements.
|
||||
constexpr Error resize_initialized(uint32_t p_size) {
|
||||
if (p_size > _size) {
|
||||
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
|
||||
memnew_arr_placement<true>(ptr() + _size, p_size - _size);
|
||||
} else if (p_size < _size) {
|
||||
if constexpr (!std::is_trivially_destructible_v<T>) {
|
||||
for (uint32_t i = p_size; i < _size; i++) {
|
||||
ptr()[i].~T();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_size = p_size;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/// Changes the size of the vector.
|
||||
/// The initializer of new elements is skipped, making this function faster than resize_initialized.
|
||||
/// The caller is required to initialize the new values.
|
||||
constexpr Error resize_uninitialized(uint32_t p_size) {
|
||||
static_assert(std::is_trivially_destructible_v<T>, "resize_uninitialized is unsafe to call if T is not trivially destructible.");
|
||||
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
|
||||
_size = p_size;
|
||||
return OK;
|
||||
}
|
||||
|
||||
constexpr void push_back(const T &p_val) {
|
||||
ERR_FAIL_COND(_size >= CAPACITY);
|
||||
memnew_placement(ptr() + _size, T(p_val));
|
||||
_size++;
|
||||
}
|
||||
|
||||
constexpr void pop_back() {
|
||||
ERR_FAIL_COND(_size == 0);
|
||||
_size--;
|
||||
ptr()[_size].~T();
|
||||
}
|
||||
|
||||
// NOTE: Subscripts sanity check the bounds to avoid undefined behavior.
|
||||
// This is slower than direct buffer access and can prevent autovectorization.
|
||||
// If the bounds are known, use ptr() subscript instead.
|
||||
constexpr const T &operator[](uint32_t p_index) const {
|
||||
CRASH_COND(p_index >= _size);
|
||||
return ptr()[p_index];
|
||||
}
|
||||
|
||||
constexpr T &operator[](uint32_t p_index) {
|
||||
CRASH_COND(p_index >= _size);
|
||||
return ptr()[p_index];
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ constexpr T *begin() { return ptr(); }
|
||||
_FORCE_INLINE_ constexpr T *end() { return ptr() + _size; }
|
||||
|
||||
_FORCE_INLINE_ constexpr const T *begin() const { return ptr(); }
|
||||
_FORCE_INLINE_ constexpr const T *end() const { return ptr() + _size; }
|
||||
};
|
||||
51
core/templates/interpolated_property.cpp
Normal file
51
core/templates/interpolated_property.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/**************************************************************************/
|
||||
/* interpolated_property.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* REDOT ENGINE */
|
||||
/* https://redotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2024-present Redot Engine contributors */
|
||||
/* (see REDOT_AUTHORS.md) */
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "interpolated_property.h"
|
||||
|
||||
#include "core/math/vector2.h"
|
||||
|
||||
namespace InterpolatedPropertyFuncs {
|
||||
|
||||
float lerp(float p_a, float p_b, float p_fraction) {
|
||||
return Math::lerp(p_a, p_b, p_fraction);
|
||||
}
|
||||
|
||||
double lerp(double p_a, double p_b, float p_fraction) {
|
||||
return Math::lerp(p_a, p_b, (double)p_fraction);
|
||||
}
|
||||
|
||||
Vector2 lerp(const Vector2 &p_a, const Vector2 &p_b, float p_fraction) {
|
||||
return p_a.lerp(p_b, p_fraction);
|
||||
}
|
||||
|
||||
} //namespace InterpolatedPropertyFuncs
|
||||
109
core/templates/interpolated_property.h
Normal file
109
core/templates/interpolated_property.h
Normal file
@@ -0,0 +1,109 @@
|
||||
/**************************************************************************/
|
||||
/* interpolated_property.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* REDOT ENGINE */
|
||||
/* https://redotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2024-present Redot Engine contributors */
|
||||
/* (see REDOT_AUTHORS.md) */
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
struct Vector2;
|
||||
|
||||
namespace InterpolatedPropertyFuncs {
|
||||
float lerp(float p_a, float p_b, float p_fraction);
|
||||
double lerp(double p_a, double p_b, float p_fraction);
|
||||
Vector2 lerp(const Vector2 &p_a, const Vector2 &p_b, float p_fraction);
|
||||
} //namespace InterpolatedPropertyFuncs
|
||||
|
||||
// This class is intended to reduce the boiler plate involved to
|
||||
// support custom properties to be physics interpolated.
|
||||
|
||||
template <class T>
|
||||
class InterpolatedProperty {
|
||||
// Only needs interpolating / updating the servers when
|
||||
// curr and prev are different.
|
||||
bool _needs_interpolating = false;
|
||||
T _interpolated;
|
||||
T curr;
|
||||
T prev;
|
||||
|
||||
public:
|
||||
// FTI depends on the constant flow between current values
|
||||
// (on the current tick) and stored previous values (on the previous tick).
|
||||
// These should be updated both on each tick, and also on resets.
|
||||
void pump() {
|
||||
prev = curr;
|
||||
_needs_interpolating = false;
|
||||
}
|
||||
void reset() { pump(); }
|
||||
|
||||
void set_interpolated_value(const T &p_val) {
|
||||
_interpolated = p_val;
|
||||
}
|
||||
const T &interpolated() const { return _interpolated; }
|
||||
bool needs_interpolating() const { return _needs_interpolating; }
|
||||
|
||||
bool interpolate(float p_interpolation_fraction) {
|
||||
if (_needs_interpolating) {
|
||||
_interpolated = InterpolatedPropertyFuncs::lerp(prev, curr, p_interpolation_fraction);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
operator T() const {
|
||||
return curr;
|
||||
}
|
||||
|
||||
bool operator==(const T &p_o) const {
|
||||
return p_o == curr;
|
||||
}
|
||||
|
||||
bool operator!=(const T &p_o) const {
|
||||
return p_o != curr;
|
||||
}
|
||||
|
||||
InterpolatedProperty &operator=(T p_val) {
|
||||
curr = p_val;
|
||||
_interpolated = p_val;
|
||||
_needs_interpolating = true;
|
||||
return *this;
|
||||
}
|
||||
InterpolatedProperty(T p_val) {
|
||||
curr = p_val;
|
||||
_interpolated = p_val;
|
||||
pump();
|
||||
}
|
||||
InterpolatedProperty() {
|
||||
// Ensure either the constructor is run,
|
||||
// or the memory is zeroed if using a fundamental type.
|
||||
_interpolated = T{};
|
||||
curr = T{};
|
||||
prev = T{};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user