diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:19:00 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:19:39 +0300 |
commit | a2c1c368af48644fa8995ecbe7138cc0d7900c30 (patch) | |
tree | 39cd435d2033f2952d2c39ce302816b037263c63 /source/blender/editors/interface | |
parent | d320f3677e2a98b12d386a3e6f3da2d7b7018463 (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 @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
Diffstat (limited to 'source/blender/editors/interface')
5 files changed, 17 insertions, 23 deletions
diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h index 923f741e3ae..027f03d05c7 100644 --- a/source/blender/editors/interface/interface_intern.h +++ b/source/blender/editors/interface/interface_intern.h @@ -688,11 +688,11 @@ extern void ui_hsvcube_pos_from_vals( */ extern void ui_but_string_get_ex(uiBut *but, char *str, - size_t maxlen, + const size_t maxlen, int float_precision, bool use_exp_float, bool *r_use_exp_float) ATTR_NONNULL(1, 2); -extern void ui_but_string_get(uiBut *but, char *str, size_t maxlen) ATTR_NONNULL(); +extern void ui_but_string_get(uiBut *but, char *str, const size_t maxlen) ATTR_NONNULL(); /** * A version of #ui_but_string_get_ex for dynamic buffer sizes * (where #ui_but_string_get_max_length returns 0). diff --git a/source/blender/editors/interface/interface_panel.c b/source/blender/editors/interface/interface_panel.c index 135cef5fe53..bc1d3387ad7 100644 --- a/source/blender/editors/interface/interface_panel.c +++ b/source/blender/editors/interface/interface_panel.c @@ -1146,7 +1146,6 @@ static void panel_draw_aligned_widgets(const uiStyle *style, UI_fontstyle_draw(fontstyle, &title_rect, panel->drawname, - sizeof(panel->drawname), title_color, &(struct uiFontStyleDraw_Params){ .align = UI_STYLE_TEXT_LEFT, diff --git a/source/blender/editors/interface/interface_region_tooltip.c b/source/blender/editors/interface/interface_region_tooltip.c index fe58a6a05ae..e146443faaa 100644 --- a/source/blender/editors/interface/interface_region_tooltip.c +++ b/source/blender/editors/interface/interface_region_tooltip.c @@ -74,8 +74,6 @@ #define UI_TIP_PADDING (int)(UI_TIP_PAD_FAC * UI_UNIT_Y) #define UI_TIP_MAXWIDTH 600 -#define UI_TIP_STR_MAX 1024 - typedef struct uiTooltipFormat { enum { UI_TIP_STYLE_NORMAL = 0, @@ -216,7 +214,7 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *region /* draw header and active data (is done here to be able to change color) */ rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_MAIN]); UI_fontstyle_set(&data->fstyle); - UI_fontstyle_draw(&data->fstyle, &bbox, field->text, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw(&data->fstyle, &bbox, field->text, drawcol, &fs_params); /* offset to the end of the last line */ if (field->text_suffix) { @@ -226,8 +224,7 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *region bbox.ymax -= yofs; rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_ACTIVE]); - UI_fontstyle_draw( - &data->fstyle, &bbox, field->text_suffix, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw(&data->fstyle, &bbox, field->text_suffix, drawcol, &fs_params); /* undo offset */ bbox.xmin -= xofs; @@ -246,7 +243,7 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *region /* XXX, needed because we don't have mono in 'U.uifonts' */ BLF_size(fstyle_mono.uifont_id, fstyle_mono.points * U.pixelsize, U.dpi); rgb_float_to_uchar(drawcol, tip_colors[field->format.color_id]); - UI_fontstyle_draw(&fstyle_mono, &bbox, field->text, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw(&fstyle_mono, &bbox, field->text, drawcol, &fs_params); } else { BLI_assert(field->format.style == UI_TIP_STYLE_NORMAL); @@ -258,7 +255,7 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *region /* draw remaining data */ rgb_float_to_uchar(drawcol, tip_colors[field->format.color_id]); UI_fontstyle_set(&data->fstyle); - UI_fontstyle_draw(&data->fstyle, &bbox, field->text, UI_TIP_STR_MAX, drawcol, &fs_params); + UI_fontstyle_draw(&data->fstyle, &bbox, field->text, drawcol, &fs_params); } bbox.ymax -= data->lineh * field->geom.lines; @@ -1218,12 +1215,12 @@ static ARegion *ui_tooltip_create_with_data(bContext *C, BLI_assert(ELEM(field->format.style, UI_TIP_STYLE_NORMAL, UI_TIP_STYLE_HEADER)); font_id = data->fstyle.uifont_id; } - w = BLF_width_ex(font_id, field->text, UI_TIP_STR_MAX, &info); + w = BLF_width_ex(font_id, field->text, BLF_DRAW_STR_DUMMY_MAX, &info); /* check for suffix (enum label) */ if (field->text_suffix && field->text_suffix[0]) { x_pos = info.width; - w = max_ii(w, x_pos + BLF_width(font_id, field->text_suffix, UI_TIP_STR_MAX)); + w = max_ii(w, x_pos + BLF_width(font_id, field->text_suffix, BLF_DRAW_STR_DUMMY_MAX)); } fontw = max_ii(fontw, w); diff --git a/source/blender/editors/interface/interface_style.c b/source/blender/editors/interface/interface_style.c index 44942d508ca..c28769a4951 100644 --- a/source/blender/editors/interface/interface_style.c +++ b/source/blender/editors/interface/interface_style.c @@ -140,9 +140,9 @@ static uiFont *uifont_to_blfont(int id) void UI_fontstyle_draw_ex(const uiFontStyle *fs, const rcti *rect, const char *str, - const size_t str_len, const uchar col[4], const struct uiFontStyleDraw_Params *fs_params, + size_t len, int *r_xofs, int *r_yofs, struct ResultBLF *r_info) @@ -183,10 +183,10 @@ void UI_fontstyle_draw_ex(const uiFontStyle *fs, } if (fs_params->align == UI_STYLE_TEXT_CENTER) { - xofs = floor(0.5f * (BLI_rcti_size_x(rect) - BLF_width(fs->uifont_id, str, str_len))); + xofs = floor(0.5f * (BLI_rcti_size_x(rect) - BLF_width(fs->uifont_id, str, len))); } else if (fs_params->align == UI_STYLE_TEXT_RIGHT) { - xofs = BLI_rcti_size_x(rect) - BLF_width(fs->uifont_id, str, str_len); + xofs = BLI_rcti_size_x(rect) - BLF_width(fs->uifont_id, str, len); } yofs = MAX2(0, yofs); @@ -196,7 +196,7 @@ void UI_fontstyle_draw_ex(const uiFontStyle *fs, BLF_position(fs->uifont_id, rect->xmin + xofs, rect->ymin + yofs, 0.0f); BLF_color4ubv(fs->uifont_id, col); - BLF_draw_ex(fs->uifont_id, str, str_len, r_info); + BLF_draw_ex(fs->uifont_id, str, len, r_info); BLF_disable(fs->uifont_id, font_flag); @@ -211,11 +211,12 @@ void UI_fontstyle_draw_ex(const uiFontStyle *fs, void UI_fontstyle_draw(const uiFontStyle *fs, const rcti *rect, const char *str, - const size_t str_len, const uchar col[4], const struct uiFontStyleDraw_Params *fs_params) { - UI_fontstyle_draw_ex(fs, rect, str, str_len, col, fs_params, NULL, NULL, NULL); + int xofs, yofs; + + UI_fontstyle_draw_ex(fs, rect, str, col, fs_params, BLF_DRAW_STR_DUMMY_MAX, &xofs, &yofs, NULL); } void UI_fontstyle_draw_rotated(const uiFontStyle *fs, diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c index b44496731f7..ad8c0842657 100644 --- a/source/blender/editors/interface/interface_widgets.c +++ b/source/blender/editors/interface/interface_widgets.c @@ -2130,11 +2130,11 @@ static void widget_draw_text(const uiFontStyle *fstyle, UI_fontstyle_draw_ex(fstyle, rect, drawstr + but->ofs, - drawlen, wcol->text, &(struct uiFontStyleDraw_Params){ .align = align, }, + drawlen, &font_xofs, &font_yofs, NULL); @@ -2194,7 +2194,6 @@ static void widget_draw_text(const uiFontStyle *fstyle, UI_fontstyle_draw(fstyle, rect, drawstr_right, - UI_MAX_DRAW_STR, col, &(struct uiFontStyleDraw_Params){ .align = UI_STYLE_TEXT_RIGHT, @@ -5418,11 +5417,11 @@ void ui_draw_menu_item(const uiFontStyle *fstyle, UI_fontstyle_draw_ex(fstyle, rect, drawstr, - sizeof(drawstr), wt->wcol.text, &(struct uiFontStyleDraw_Params){ .align = UI_STYLE_TEXT_LEFT, }, + BLF_DRAW_STR_DUMMY_MAX, &xofs, &yofs, &info); @@ -5469,7 +5468,6 @@ void ui_draw_menu_item(const uiFontStyle *fstyle, UI_fontstyle_draw(fstyle, rect, hint_drawstr, - sizeof(hint_drawstr), wt->wcol.text, &(struct uiFontStyleDraw_Params){ .align = UI_STYLE_TEXT_RIGHT, @@ -5525,7 +5523,6 @@ void ui_draw_preview_item_stateless(const uiFontStyle *fstyle, UI_fontstyle_draw(fstyle, &trect, drawstr, - sizeof(drawstr), text_col, &(struct uiFontStyleDraw_Params){ .align = text_align, |