diff options
author | Clment Foucault <fclem> | 2022-01-12 14:46:52 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:47:43 +0300 |
commit | 46e049d0ce2bce2f53ddc41a0dbbea2969d00a5d (patch) | |
tree | 2b814f1b737ebe17364de1a5938deb9a7066d2a9 /source/blender/blenlib/BLI_string.h | |
parent | e5766752d04794c2693dedad75baeb8c7d68f4cf (diff) |
BLI: Refactor vector types & functions to use templates
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
Diffstat (limited to 'source/blender/blenlib/BLI_string.h')
-rw-r--r-- | source/blender/blenlib/BLI_string.h | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 8177545911c..a82e97914db 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -42,7 +42,8 @@ extern "C" { * \param len: The number of bytes to duplicate * \retval Returns the duplicated string */ -char *BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +char *BLI_strdupn(const char *str, const size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT + ATTR_NONNULL(); /** * Duplicates the cstring \a str into a newly mallocN'd @@ -73,7 +74,8 @@ char *BLI_strdupcat(const char *__restrict str1, * the size of dst) * \retval Returns dst */ -char *BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL(); +char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) + ATTR_NONNULL(); /** * Like BLI_strncpy but ensures dst is always padded by given char, @@ -105,7 +107,7 @@ char *BLI_strncpy_ensure_pad(char *__restrict dst, */ size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, - size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); + const size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); @@ -184,7 +186,7 @@ void BLI_str_replace_char(char *str, char src, char dst) ATTR_NONNULL(); * \note Larger tables should use a hash table. */ bool BLI_str_replace_table_exact(char *string, - size_t string_len, + const size_t string_len, const char *replace_table[][2], int replace_table_len); @@ -233,7 +235,7 @@ char *BLI_sprintfN(const char *__restrict format, ...) ATTR_WARN_UNUSED_RESULT * * \note This is used for creating animation paths in blend files. */ -size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) +size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const size_t dst_maxncpy) ATTR_NONNULL(); /** * This roughly matches C and Python's string escaping with double quotes - `"`. @@ -249,9 +251,9 @@ size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t d */ size_t BLI_str_unescape_ex(char *__restrict dst, const char *__restrict src, - size_t src_maxncpy, + const size_t src_maxncpy, /* Additional arguments. */ - size_t dst_maxncpy, + const size_t dst_maxncpy, bool *r_is_complete) ATTR_NONNULL(); /** * See #BLI_str_unescape_ex doc-string. @@ -263,7 +265,7 @@ size_t BLI_str_unescape_ex(char *__restrict dst, * * \note This is used for parsing animation paths in blend files (runs often). */ -size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, size_t src_maxncpy) +size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, const size_t src_maxncpy) ATTR_NONNULL(); /** @@ -357,10 +359,10 @@ int BLI_strcmp_ignore_pad(const char *str1, const char *str2, char pad) ATTR_WAR /** * Determine the length of a fixed-size string. */ -size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +size_t BLI_strnlen(const char *str, const size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -void BLI_str_tolower_ascii(char *str, size_t len) ATTR_NONNULL(); -void BLI_str_toupper_ascii(char *str, size_t len) ATTR_NONNULL(); +void BLI_str_tolower_ascii(char *str, const size_t len) ATTR_NONNULL(); +void BLI_str_toupper_ascii(char *str, const size_t len) ATTR_NONNULL(); /** * Strip white-space from end of the string. */ @@ -477,7 +479,7 @@ bool BLI_string_all_words_matched(const char *name, * \return The number of words found in \a str */ int BLI_string_find_split_words(const char *str, - size_t len, + const size_t len, char delim, int r_words[][2], int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); |