From 4fe8d0419c2f080a248f52b3924ce2a4e897e5cb Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sun, 21 Mar 2021 19:31:24 +0100 Subject: Functions: refactor virtual array data structures When a function is executed for many elements (e.g. per point) it is often the case that some parameters are different for every element and other parameters are the same (there are some more less common cases). To simplify writing such functions one can use a "virtual array". This is a data structure that has a value for every index, but might not be stored as an actual array internally. Instead, it might be just a single value or is computed on the fly. There are various tradeoffs involved when using this data structure which are mentioned in `BLI_virtual_array.hh`. It is called "virtual", because it uses inheritance and virtual methods. Furthermore, there is a new virtual vector array data structure, which is an array of vectors. Both these types have corresponding generic variants, which can be used when the data type is not known at compile time. This is typically the case when building a somewhat generic execution system. The function system used these virtual data structures before, but now they are more versatile. I've done this refactor in preparation for the attribute processor and other features of geometry nodes. I moved the typed virtual arrays to blenlib, so that they can be used independent of the function system. One open question for me is whether all the generic data structures (and `CPPType`) should be moved to blenlib as well. They are well isolated and don't really contain any business logic. That can be done later if necessary. --- .../blender/nodes/function/nodes/node_fn_object_transforms.cc | 2 +- source/blender/nodes/function/nodes/node_fn_random_float.cc | 6 +++--- source/blender/nodes/shader/nodes/node_shader_map_range.cc | 10 +++++----- source/blender/nodes/shader/nodes/node_shader_sepcombRGB.cc | 4 ++-- source/blender/nodes/shader/nodes/node_shader_sepcombXYZ.cc | 4 ++-- source/blender/nodes/shader/nodes/node_shader_valToRgb.cc | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) (limited to 'source/blender/nodes') diff --git a/source/blender/nodes/function/nodes/node_fn_object_transforms.cc b/source/blender/nodes/function/nodes/node_fn_object_transforms.cc index 26b25406590..55f592d7879 100644 --- a/source/blender/nodes/function/nodes/node_fn_object_transforms.cc +++ b/source/blender/nodes/function/nodes/node_fn_object_transforms.cc @@ -42,7 +42,7 @@ class ObjectTransformsFunction : public blender::fn::MultiFunction { blender::fn::MFParams params, blender::fn::MFContext context) const override { - blender::fn::VSpan handles = + const blender::VArray &handles = params.readonly_single_input(0, "Object"); blender::MutableSpan locations = params.uninitialized_single_output( 1, "Location"); diff --git a/source/blender/nodes/function/nodes/node_fn_random_float.cc b/source/blender/nodes/function/nodes/node_fn_random_float.cc index d0ecb5592da..84ded73c601 100644 --- a/source/blender/nodes/function/nodes/node_fn_random_float.cc +++ b/source/blender/nodes/function/nodes/node_fn_random_float.cc @@ -48,9 +48,9 @@ class RandomFloatFunction : public blender::fn::MultiFunction { blender::fn::MFParams params, blender::fn::MFContext UNUSED(context)) const override { - blender::fn::VSpan min_values = params.readonly_single_input(0, "Min"); - blender::fn::VSpan max_values = params.readonly_single_input(1, "Max"); - blender::fn::VSpan seeds = params.readonly_single_input(2, "Seed"); + const blender::VArray &min_values = params.readonly_single_input(0, "Min"); + const blender::VArray &max_values = params.readonly_single_input(1, "Max"); + const blender::VArray &seeds = params.readonly_single_input(2, "Seed"); blender::MutableSpan values = params.uninitialized_single_output(3, "Value"); for (int64_t i : mask) { diff --git a/source/blender/nodes/shader/nodes/node_shader_map_range.cc b/source/blender/nodes/shader/nodes/node_shader_map_range.cc index c01e390c513..3aa533599cf 100644 --- a/source/blender/nodes/shader/nodes/node_shader_map_range.cc +++ b/source/blender/nodes/shader/nodes/node_shader_map_range.cc @@ -108,11 +108,11 @@ class MapRangeFunction : public blender::fn::MultiFunction { blender::fn::MFParams params, blender::fn::MFContext UNUSED(context)) const override { - blender::fn::VSpan values = params.readonly_single_input(0, "Value"); - blender::fn::VSpan from_min = params.readonly_single_input(1, "From Min"); - blender::fn::VSpan from_max = params.readonly_single_input(2, "From Max"); - blender::fn::VSpan to_min = params.readonly_single_input(3, "To Min"); - blender::fn::VSpan to_max = params.readonly_single_input(4, "To Max"); + const blender::VArray &values = params.readonly_single_input(0, "Value"); + const blender::VArray &from_min = params.readonly_single_input(1, "From Min"); + const blender::VArray &from_max = params.readonly_single_input(2, "From Max"); + const blender::VArray &to_min = params.readonly_single_input(3, "To Min"); + const blender::VArray &to_max = params.readonly_single_input(4, "To Max"); blender::MutableSpan results = params.uninitialized_single_output(5, "Result"); for (int64_t i : mask) { diff --git a/source/blender/nodes/shader/nodes/node_shader_sepcombRGB.cc b/source/blender/nodes/shader/nodes/node_shader_sepcombRGB.cc index 59b89c46fc7..a9057428981 100644 --- a/source/blender/nodes/shader/nodes/node_shader_sepcombRGB.cc +++ b/source/blender/nodes/shader/nodes/node_shader_sepcombRGB.cc @@ -74,8 +74,8 @@ class SeparateRGBFunction : public blender::fn::MultiFunction { blender::fn::MFParams params, blender::fn::MFContext UNUSED(context)) const override { - blender::fn::VSpan colors = params.readonly_single_input( - 0, "Color"); + const blender::VArray &colors = + params.readonly_single_input(0, "Color"); blender::MutableSpan rs = params.uninitialized_single_output(1, "R"); blender::MutableSpan gs = params.uninitialized_single_output(2, "G"); blender::MutableSpan bs = params.uninitialized_single_output(3, "B"); diff --git a/source/blender/nodes/shader/nodes/node_shader_sepcombXYZ.cc b/source/blender/nodes/shader/nodes/node_shader_sepcombXYZ.cc index 8b23327460f..c0dc66b3342 100644 --- a/source/blender/nodes/shader/nodes/node_shader_sepcombXYZ.cc +++ b/source/blender/nodes/shader/nodes/node_shader_sepcombXYZ.cc @@ -59,8 +59,8 @@ class MF_SeparateXYZ : public blender::fn::MultiFunction { blender::fn::MFParams params, blender::fn::MFContext UNUSED(context)) const override { - blender::fn::VSpan vectors = params.readonly_single_input( - 0, "XYZ"); + const blender::VArray &vectors = + params.readonly_single_input(0, "XYZ"); blender::MutableSpan xs = params.uninitialized_single_output(1, "X"); blender::MutableSpan ys = params.uninitialized_single_output(2, "Y"); blender::MutableSpan zs = params.uninitialized_single_output(3, "Z"); diff --git a/source/blender/nodes/shader/nodes/node_shader_valToRgb.cc b/source/blender/nodes/shader/nodes/node_shader_valToRgb.cc index a86a8bb8935..0d50582e23a 100644 --- a/source/blender/nodes/shader/nodes/node_shader_valToRgb.cc +++ b/source/blender/nodes/shader/nodes/node_shader_valToRgb.cc @@ -142,7 +142,7 @@ class ColorBandFunction : public blender::fn::MultiFunction { blender::fn::MFParams params, blender::fn::MFContext UNUSED(context)) const override { - blender::fn::VSpan values = params.readonly_single_input(0, "Value"); + const blender::VArray &values = params.readonly_single_input(0, "Value"); blender::MutableSpan colors = params.uninitialized_single_output(1, "Color"); blender::MutableSpan alphas = params.uninitialized_single_output(2, "Alpha"); -- cgit v1.2.3