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 @Howard Trickey (howardt) opinion on the matter. - The noexcept on the copy constructor of mpq(2|3) is being removed. But according to @Jacques Lucke (JacquesLucke) it is not a real problem for now. I would like to give a huge thanks to @Jacques Lucke (JacquesLucke) who helped during this and pushed me to reduce the duplication further. Reviewed By: brecht, sergey, JacquesLucke Differential Revision: http://developer.blender.org/D13791
130 lines
3.7 KiB
C++
130 lines
3.7 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
|
|
*/
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef struct BLI_Buffer {
|
|
void *data;
|
|
const size_t elem_size;
|
|
size_t count, alloc_count;
|
|
int flag;
|
|
} BLI_Buffer;
|
|
|
|
enum {
|
|
BLI_BUFFER_NOP = 0,
|
|
BLI_BUFFER_USE_STATIC = (1 << 0),
|
|
};
|
|
|
|
#define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
|
|
char name_##user; /* warn for free only */ \
|
|
type_ name_##_static_[static_count_]; \
|
|
BLI_Buffer name_ = { \
|
|
(name_##_static_), sizeof(type_), 0, static_count_, BLI_BUFFER_USE_STATIC | (flag_)}
|
|
|
|
/* Never use static. */
|
|
#define BLI_buffer_declare(type_, name_, flag_) \
|
|
bool name_##user; /* warn for free only */ \
|
|
BLI_Buffer name_ = {NULL, sizeof(type_), 0, 0, (flag_)}
|
|
|
|
#define BLI_buffer_at(buffer_, type_, index_) \
|
|
((((type_ *)(buffer_) \
|
|
->data)[(BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
|
|
(BLI_assert((int)(index_) >= 0 && (size_t)(index_) < (buffer_)->count)), \
|
|
index_]))
|
|
|
|
#define BLI_buffer_array(buffer_, type_) (&(BLI_buffer_at(buffer_, type_, 0)))
|
|
|
|
#define BLI_buffer_resize_data(buffer_, type_, new_count_) \
|
|
((BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
|
|
|
|
#define BLI_buffer_reinit_data(buffer_, type_, new_count_) \
|
|
((BLI_buffer_reinit(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
|
|
|
|
#define BLI_buffer_append(buffer_, type_, val_) \
|
|
(BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
|
|
(BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_))
|
|
|
|
#define BLI_buffer_clear(buffer_) \
|
|
{ \
|
|
(buffer_)->count = 0; \
|
|
} \
|
|
(void)0
|
|
|
|
/**
|
|
* \note Never decreases the amount of memory allocated.
|
|
*/
|
|
void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count);
|
|
|
|
/**
|
|
* Ensure size, throwing away old data, respecting #BLI_BUFFER_USE_CALLOC.
|
|
*
|
|
* Similar to #BLI_buffer_resize, but use when the existing data can be:
|
|
* - Ignored (malloc'd).
|
|
* - Cleared (when #BLI_BUFFER_USE_CALLOC is set).
|
|
*/
|
|
void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count);
|
|
|
|
/**
|
|
* Append an array of elements.
|
|
*
|
|
* Callers use #BLI_buffer_append_array.
|
|
*/
|
|
void _bli_buffer_append_array(BLI_Buffer *buffer, void *data, size_t count);
|
|
#define BLI_buffer_append_array(buffer_, type_, data_, count_) \
|
|
{ \
|
|
type_ *__tmp = (data_); \
|
|
BLI_assert(sizeof(type_) == (buffer_)->elem_size); \
|
|
_bli_buffer_append_array(buffer_, __tmp, count_); \
|
|
} \
|
|
(void)0
|
|
|
|
/**
|
|
* Does not free the buffer structure itself.
|
|
*
|
|
* Callers use #BLI_buffer_free.
|
|
*/
|
|
void _bli_buffer_free(BLI_Buffer *buffer);
|
|
#define BLI_buffer_free(name_) \
|
|
{ \
|
|
_bli_buffer_free(name_); \
|
|
(void)name_##user; /* ensure we free */ \
|
|
} \
|
|
(void)0
|
|
|
|
/**
|
|
* A buffer embedded in a struct. Using #memcpy is allowed until first resize.
|
|
*/
|
|
#define BLI_buffer_field_init(name_, type_) \
|
|
{ \
|
|
memset(name_, 0, sizeof(*name_)); \
|
|
*(size_t *)&((name_)->elem_size) = sizeof(type_); \
|
|
} \
|
|
(void)0
|
|
|
|
#define BLI_buffer_field_free(name_) _bli_buffer_free(name_)
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|