This patch implements the vector types (i.e:`float2`) by making heavy usage of templating. All vector functions are now outside of the vector classes (inside the `blender::math` namespace) and are not vector size dependent for the most part. In the ongoing effort to make shaders less GL centric, we are aiming to share more code between GLSL and C++ to avoid code duplication. ####Motivations: - We are aiming to share UBO and SSBO structures between GLSL and C++. This means we will use many of the existing vector types and others we currently don't have (uintX, intX). All these variations were asking for many more code duplication. - Deduplicate existing code which is duplicated for each vector size. - We also want to share small functions. Which means that vector functions should be static and not in the class namespace. - Reduce friction to use these types in new projects due to their incompleteness. - The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a bit of a let down. Most clases are incomplete, out of sync with each others with different codestyles, and some functions that should be static are not (i.e: `float3::reflect()`). ####Upsides: - Still support `.x, .y, .z, .w` for readability. - Compact, readable and easilly extendable. - All of the vector functions are available for all the vectors types and can be restricted to certain types. Also template specialization let us define exception for special class (like mpq). - With optimization ON, the compiler unroll the loops and performance is the same. ####Downsides: - Might impact debugability. Though I would arge that the bugs are rarelly caused by the vector class itself (since the operations are quite trivial) but by the type conversions. - Might impact compile time. I did not saw a significant impact since the usage is not really widespread. - Functions needs to be rewritten to support arbitrary vector length. For instance, one can't call `len_squared_v3v3` in `math::length_squared()` and call it a day. - Type cast does not work with the template version of the `math::` vector functions. Meaning you need to manually cast `float *` and `(float *)[3]` to `float3` for the function calls. i.e: `math::distance_squared(float3(nearest.co), positions[i]);` - Some parts might loose in readability: `float3::dot(v1.normalized(), v2.normalized())` becoming `math::dot(math::normalize(v1), math::normalize(v2))` But I propose, when appropriate, to use `using namespace blender::math;` on function local or file scope to increase readability. `dot(normalize(v1), normalize(v2))` ####Consideration: - Include back `.length()` method. It is quite handy and is more C++ oriented. - I considered the GLM library as a candidate for replacement. It felt like too much for what we need and would be difficult to extend / modify to our needs. - I used Macros to reduce code in operators declaration and potential copy paste bugs. This could reduce debugability and could be reverted. - This touches `delaunay_2d.cc` and the intersection code. I would like to know @howardt opinion on the matter. - The `noexcept` on the copy constructor of `mpq(2|3)` is being removed. But according to @JacquesLucke it is not a real problem for now. I would like to give a huge thanks to @JacquesLucke who helped during this and pushed me to reduce the duplication further. Reviewed By: brecht, sergey, JacquesLucke Differential Revision: https://developer.blender.org/D13791
561 lines
13 KiB
C++
561 lines
13 KiB
C++
/*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
/** \file
|
|
* \ingroup bli
|
|
* Some of the functions below have very similar alternatives in the standard library. However, it
|
|
* is rather annoying to use those when debugging. Therefore, some more specialized and easier to
|
|
* debug functions are provided here.
|
|
*/
|
|
|
|
#include <memory>
|
|
#include <new>
|
|
#include <type_traits>
|
|
|
|
#include "BLI_utildefines.h"
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
namespace blender {
|
|
|
|
/**
|
|
* Call the destructor on n consecutive values. For trivially destructible types, this does
|
|
* nothing.
|
|
*
|
|
* Exception Safety: Destructors shouldn't throw exceptions.
|
|
*
|
|
* Before:
|
|
* ptr: initialized
|
|
* After:
|
|
* ptr: uninitialized
|
|
*/
|
|
template<typename T> void destruct_n(T *ptr, int64_t n)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
static_assert(std::is_nothrow_destructible_v<T>,
|
|
"This should be true for all types. Destructors are noexcept by default.");
|
|
|
|
/* This is not strictly necessary, because the loop below will be optimized away anyway. It is
|
|
* nice to make behavior this explicitly, though. */
|
|
if (std::is_trivially_destructible_v<T>) {
|
|
return;
|
|
}
|
|
|
|
for (int64_t i = 0; i < n; i++) {
|
|
ptr[i].~T();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Call the default constructor on n consecutive elements. For trivially constructible types, this
|
|
* does nothing.
|
|
*
|
|
* Exception Safety: Strong.
|
|
*
|
|
* Before:
|
|
* ptr: uninitialized
|
|
* After:
|
|
* ptr: initialized
|
|
*/
|
|
template<typename T> void default_construct_n(T *ptr, int64_t n)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
/* This is not strictly necessary, because the loop below will be optimized away anyway. It is
|
|
* nice to make behavior this explicitly, though. */
|
|
if (std::is_trivially_constructible_v<T>) {
|
|
return;
|
|
}
|
|
|
|
int64_t current = 0;
|
|
try {
|
|
for (; current < n; current++) {
|
|
new (static_cast<void *>(ptr + current)) T;
|
|
}
|
|
}
|
|
catch (...) {
|
|
destruct_n(ptr, current);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Copy n values from src to dst.
|
|
*
|
|
* Exception Safety: Basic.
|
|
*
|
|
* Before:
|
|
* src: initialized
|
|
* dst: initialized
|
|
* After:
|
|
* src: initialized
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void initialized_copy_n(const T *src, int64_t n, T *dst)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
for (int64_t i = 0; i < n; i++) {
|
|
dst[i] = src[i];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Copy n values from src to dst.
|
|
*
|
|
* Exception Safety: Strong.
|
|
*
|
|
* Before:
|
|
* src: initialized
|
|
* dst: uninitialized
|
|
* After:
|
|
* src: initialized
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void uninitialized_copy_n(const T *src, int64_t n, T *dst)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
int64_t current = 0;
|
|
try {
|
|
for (; current < n; current++) {
|
|
new (static_cast<void *>(dst + current)) T(src[current]);
|
|
}
|
|
}
|
|
catch (...) {
|
|
destruct_n(dst, current);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Convert n values from type `From` to type `To`.
|
|
*
|
|
* Exception Safety: Strong.
|
|
*
|
|
* Before:
|
|
* src: initialized
|
|
* dst: uninitialized
|
|
* After:
|
|
* src: initialized
|
|
* dst: initialized
|
|
*/
|
|
template<typename From, typename To>
|
|
void uninitialized_convert_n(const From *src, int64_t n, To *dst)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
int64_t current = 0;
|
|
try {
|
|
for (; current < n; current++) {
|
|
new (static_cast<void *>(dst + current)) To(static_cast<To>(src[current]));
|
|
}
|
|
}
|
|
catch (...) {
|
|
destruct_n(dst, current);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Move n values from src to dst.
|
|
*
|
|
* Exception Safety: Basic.
|
|
*
|
|
* Before:
|
|
* src: initialized
|
|
* dst: initialized
|
|
* After:
|
|
* src: initialized, moved-from
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void initialized_move_n(T *src, int64_t n, T *dst)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
for (int64_t i = 0; i < n; i++) {
|
|
dst[i] = std::move(src[i]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Move n values from src to dst.
|
|
*
|
|
* Exception Safety: Basic.
|
|
*
|
|
* Before:
|
|
* src: initialized
|
|
* dst: uninitialized
|
|
* After:
|
|
* src: initialized, moved-from
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void uninitialized_move_n(T *src, int64_t n, T *dst)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
int64_t current = 0;
|
|
try {
|
|
for (; current < n; current++) {
|
|
new (static_cast<void *>(dst + current)) T(std::move(src[current]));
|
|
}
|
|
}
|
|
catch (...) {
|
|
destruct_n(dst, current);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Relocate n values from src to dst. Relocation is a move followed by destruction of the src
|
|
* value.
|
|
*
|
|
* Exception Safety: Basic.
|
|
*
|
|
* Before:
|
|
* src: initialized
|
|
* dst: initialized
|
|
* After:
|
|
* src: uninitialized
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void initialized_relocate_n(T *src, int64_t n, T *dst)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
initialized_move_n(src, n, dst);
|
|
destruct_n(src, n);
|
|
}
|
|
|
|
/**
|
|
* Relocate n values from src to dst. Relocation is a move followed by destruction of the src
|
|
* value.
|
|
*
|
|
* Exception Safety: Basic.
|
|
*
|
|
* Before:
|
|
* src: initialized
|
|
* dst: uninitialized
|
|
* After:
|
|
* src: uninitialized
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void uninitialized_relocate_n(T *src, int64_t n, T *dst)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
uninitialized_move_n(src, n, dst);
|
|
destruct_n(src, n);
|
|
}
|
|
|
|
/**
|
|
* Copy the value to n consecutive elements.
|
|
*
|
|
* Exception Safety: Basic.
|
|
*
|
|
* Before:
|
|
* dst: initialized
|
|
* After:
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void initialized_fill_n(T *dst, int64_t n, const T &value)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
for (int64_t i = 0; i < n; i++) {
|
|
dst[i] = value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Copy the value to n consecutive elements.
|
|
*
|
|
* Exception Safety: Strong.
|
|
*
|
|
* Before:
|
|
* dst: uninitialized
|
|
* After:
|
|
* dst: initialized
|
|
*/
|
|
template<typename T> void uninitialized_fill_n(T *dst, int64_t n, const T &value)
|
|
{
|
|
BLI_assert(n >= 0);
|
|
|
|
int64_t current = 0;
|
|
try {
|
|
for (; current < n; current++) {
|
|
new (static_cast<void *>(dst + current)) T(value);
|
|
}
|
|
}
|
|
catch (...) {
|
|
destruct_n(dst, current);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
template<typename T> struct DestructValueAtAddress {
|
|
DestructValueAtAddress() = default;
|
|
|
|
template<typename U> DestructValueAtAddress(const U &)
|
|
{
|
|
}
|
|
|
|
void operator()(T *ptr)
|
|
{
|
|
ptr->~T();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* A destruct_ptr is like unique_ptr, but it will only call the destructor and will not free the
|
|
* memory. This is useful when using custom allocators.
|
|
*/
|
|
template<typename T> using destruct_ptr = std::unique_ptr<T, DestructValueAtAddress<T>>;
|
|
|
|
/**
|
|
* An `AlignedBuffer` is a byte array with at least the given size and alignment. The buffer will
|
|
* not be initialized by the default constructor.
|
|
*/
|
|
template<size_t Size, size_t Alignment> class alignas(Alignment) AlignedBuffer {
|
|
private:
|
|
/* Don't create an empty array. This causes problems with some compilers. */
|
|
char buffer_[(Size > 0) ? Size : 1];
|
|
|
|
public:
|
|
operator void *()
|
|
{
|
|
return buffer_;
|
|
}
|
|
|
|
operator const void *() const
|
|
{
|
|
return buffer_;
|
|
}
|
|
|
|
void *ptr()
|
|
{
|
|
return buffer_;
|
|
}
|
|
|
|
const void *ptr() const
|
|
{
|
|
return buffer_;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* This can be used to reserve memory for C++ objects whose lifetime is different from the
|
|
* lifetime of the object they are embedded in. It's used by containers with small buffer
|
|
* optimization and hash table implementations.
|
|
*/
|
|
template<typename T, int64_t Size = 1> class TypedBuffer {
|
|
private:
|
|
AlignedBuffer<sizeof(T) * (size_t)Size, alignof(T)> buffer_;
|
|
|
|
public:
|
|
operator T *()
|
|
{
|
|
return static_cast<T *>(buffer_.ptr());
|
|
}
|
|
|
|
operator const T *() const
|
|
{
|
|
return static_cast<const T *>(buffer_.ptr());
|
|
}
|
|
|
|
T &operator*()
|
|
{
|
|
return *static_cast<T *>(buffer_.ptr());
|
|
}
|
|
|
|
const T &operator*() const
|
|
{
|
|
return *static_cast<const T *>(buffer_.ptr());
|
|
}
|
|
|
|
T *ptr()
|
|
{
|
|
return static_cast<T *>(buffer_.ptr());
|
|
}
|
|
|
|
const T *ptr() const
|
|
{
|
|
return static_cast<const T *>(buffer_.ptr());
|
|
}
|
|
|
|
T &ref()
|
|
{
|
|
return *static_cast<T *>(buffer_.ptr());
|
|
}
|
|
|
|
const T &ref() const
|
|
{
|
|
return *static_cast<const T *>(buffer_.ptr());
|
|
}
|
|
};
|
|
|
|
/* A dynamic stack buffer can be used instead of #alloca when wants to allocate a dynamic amount of
|
|
* memory on the stack. Using this class has some advantages:
|
|
* - It falls back to heap allocation, when the size is too large.
|
|
* - It can be used in loops safely.
|
|
* - If the buffer is heap allocated, it is free automatically in the destructor.
|
|
*/
|
|
template<size_t ReservedSize = 64, size_t ReservedAlignment = 64>
|
|
class alignas(ReservedAlignment) DynamicStackBuffer {
|
|
private:
|
|
/* Don't create an empty array. This causes problems with some compilers. */
|
|
char reserved_buffer_[(ReservedSize > 0) ? ReservedSize : 1];
|
|
void *buffer_;
|
|
|
|
public:
|
|
DynamicStackBuffer(const int64_t size, const int64_t alignment)
|
|
{
|
|
BLI_assert(size >= 0);
|
|
BLI_assert(alignment >= 0);
|
|
if (size <= ReservedSize && alignment <= ReservedAlignment) {
|
|
buffer_ = reserved_buffer_;
|
|
}
|
|
else {
|
|
buffer_ = MEM_mallocN_aligned(size, alignment, __func__);
|
|
}
|
|
}
|
|
~DynamicStackBuffer()
|
|
{
|
|
if (buffer_ != reserved_buffer_) {
|
|
MEM_freeN(buffer_);
|
|
}
|
|
}
|
|
|
|
/* Don't allow any copying or moving of this type. */
|
|
DynamicStackBuffer(const DynamicStackBuffer &other) = delete;
|
|
DynamicStackBuffer(DynamicStackBuffer &&other) = delete;
|
|
DynamicStackBuffer &operator=(const DynamicStackBuffer &other) = delete;
|
|
DynamicStackBuffer &operator=(DynamicStackBuffer &&other) = delete;
|
|
|
|
void *buffer() const
|
|
{
|
|
return buffer_;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* This can be used by container constructors. A parameter of this type should be used to indicate
|
|
* that the constructor does not construct the elements.
|
|
*/
|
|
class NoInitialization {
|
|
};
|
|
|
|
/**
|
|
* This can be used to mark a constructor of an object that does not throw exceptions. Other
|
|
* constructors can delegate to this constructor to make sure that the object lifetime starts.
|
|
* With this, the destructor of the object will be called, even when the remaining constructor
|
|
* throws.
|
|
*/
|
|
class NoExceptConstructor {
|
|
};
|
|
|
|
/**
|
|
* Helper variable that checks if a pointer type can be converted into another pointer type without
|
|
* issues. Possible issues are casting away const and casting a pointer to a child class.
|
|
* Adding const or casting to a parent class is fine.
|
|
*/
|
|
template<typename From, typename To>
|
|
inline constexpr bool is_convertible_pointer_v =
|
|
std::is_convertible_v<From, To> &&std::is_pointer_v<From> &&std::is_pointer_v<To>;
|
|
|
|
/**
|
|
* Helper variable that checks if a Span<From> can be converted to Span<To> safely, whereby From
|
|
* and To are pointers. Adding const and casting to a void pointer is allowed.
|
|
* Casting up and down a class hierarchy generally is not allowed, because this might change the
|
|
* pointer under some circumstances.
|
|
*/
|
|
template<typename From, typename To>
|
|
inline constexpr bool is_span_convertible_pointer_v =
|
|
/* Make sure we are working with pointers. */
|
|
std::is_pointer_v<From> &&std::is_pointer_v<To> &&
|
|
(/* No casting is necessary when both types are the same. */
|
|
std::is_same_v<From, To> ||
|
|
/* Allow adding const to the underlying type. */
|
|
std::is_same_v<const std::remove_pointer_t<From>, std::remove_pointer_t<To>> ||
|
|
/* Allow casting non-const pointers to void pointers. */
|
|
(!std::is_const_v<std::remove_pointer_t<From>> && std::is_same_v<To, void *>) ||
|
|
/* Allow casting any pointer to const void pointers. */
|
|
std::is_same_v<To, const void *>);
|
|
|
|
/**
|
|
* Same as #std::is_same_v but allows for checking multiple types at the same time.
|
|
*/
|
|
template<typename T, typename... Args>
|
|
inline constexpr bool is_same_any_v = (std::is_same_v<T, Args> || ...);
|
|
|
|
/**
|
|
* Inline buffers for small-object-optimization should be disable by default. Otherwise we might
|
|
* get large unexpected allocations on the stack.
|
|
*/
|
|
inline constexpr int64_t default_inline_buffer_capacity(size_t element_size)
|
|
{
|
|
return (static_cast<int64_t>(element_size) < 100) ? 4 : 0;
|
|
}
|
|
|
|
/**
|
|
* This can be used by containers to implement an exception-safe copy-assignment-operator.
|
|
* It assumes that the container has an exception safe copy constructor and an exception-safe
|
|
* move-assignment-operator.
|
|
*/
|
|
template<typename Container> Container ©_assign_container(Container &dst, const Container &src)
|
|
{
|
|
if (&src == &dst) {
|
|
return dst;
|
|
}
|
|
|
|
Container container_copy{src};
|
|
dst = std::move(container_copy);
|
|
return dst;
|
|
}
|
|
|
|
/**
|
|
* This can be used by containers to implement an exception-safe move-assignment-operator.
|
|
* It assumes that the container has an exception-safe move-constructor and a noexcept constructor
|
|
* tagged with the NoExceptConstructor tag.
|
|
*/
|
|
template<typename Container>
|
|
Container &move_assign_container(Container &dst, Container &&src) noexcept(
|
|
std::is_nothrow_move_constructible_v<Container>)
|
|
{
|
|
if (&dst == &src) {
|
|
return dst;
|
|
}
|
|
|
|
dst.~Container();
|
|
if constexpr (std::is_nothrow_move_constructible_v<Container>) {
|
|
new (&dst) Container(std::move(src));
|
|
}
|
|
else {
|
|
try {
|
|
new (&dst) Container(std::move(src));
|
|
}
|
|
catch (...) {
|
|
new (&dst) Container(NoExceptConstructor());
|
|
throw;
|
|
}
|
|
}
|
|
return dst;
|
|
}
|
|
|
|
} // namespace blender
|