From a8e0fe6a542318a31dff94e9fcfdc420a4808af2 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 7 Dec 2021 15:21:59 +0100 Subject: Geometry Nodes: move type conversions to blenkernel The type conversions do not depend on other files in the nodes module. Furthermore we want to use the conversions in the geometry module without creating a dependency to the nodes module there. --- source/blender/blenkernel/BKE_type_conversions.hh | 82 +++++ source/blender/blenkernel/CMakeLists.txt | 2 + .../blender/blenkernel/intern/attribute_access.cc | 17 +- .../blender/blenkernel/intern/type_conversions.cc | 347 +++++++++++++++++++++ .../modifiers/intern/MOD_nodes_evaluator.cc | 7 +- source/blender/nodes/CMakeLists.txt | 2 - source/blender/nodes/NOD_type_conversions.hh | 82 ----- .../nodes/geometry/nodes/node_geo_join_geometry.cc | 3 +- source/blender/nodes/intern/node_geometry_exec.cc | 5 +- source/blender/nodes/intern/type_conversions.cc | 347 --------------------- 10 files changed, 447 insertions(+), 447 deletions(-) create mode 100644 source/blender/blenkernel/BKE_type_conversions.hh create mode 100644 source/blender/blenkernel/intern/type_conversions.cc delete mode 100644 source/blender/nodes/NOD_type_conversions.hh delete mode 100644 source/blender/nodes/intern/type_conversions.cc (limited to 'source/blender') diff --git a/source/blender/blenkernel/BKE_type_conversions.hh b/source/blender/blenkernel/BKE_type_conversions.hh new file mode 100644 index 00000000000..27419b32de6 --- /dev/null +++ b/source/blender/blenkernel/BKE_type_conversions.hh @@ -0,0 +1,82 @@ +/* + * 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 + +#include "FN_multi_function.hh" + +namespace blender::bke { + +using fn::CPPType; + +struct ConversionFunctions { + const fn::MultiFunction *multi_function; + void (*convert_single_to_initialized)(const void *src, void *dst); + void (*convert_single_to_uninitialized)(const void *src, void *dst); +}; + +class DataTypeConversions { + private: + Map, ConversionFunctions> conversions_; + + public: + void add(fn::MFDataType from_type, + fn::MFDataType to_type, + const fn::MultiFunction &fn, + void (*convert_single_to_initialized)(const void *src, void *dst), + void (*convert_single_to_uninitialized)(const void *src, void *dst)) + { + conversions_.add_new({from_type, to_type}, + {&fn, convert_single_to_initialized, convert_single_to_uninitialized}); + } + + const ConversionFunctions *get_conversion_functions(fn::MFDataType from, fn::MFDataType to) const + { + return conversions_.lookup_ptr({from, to}); + } + + const ConversionFunctions *get_conversion_functions(const CPPType &from, const CPPType &to) const + { + return this->get_conversion_functions(fn::MFDataType::ForSingle(from), + fn::MFDataType::ForSingle(to)); + } + + const fn::MultiFunction *get_conversion_multi_function(fn::MFDataType from, + fn::MFDataType to) const + { + const ConversionFunctions *functions = this->get_conversion_functions(from, to); + return functions ? functions->multi_function : nullptr; + } + + bool is_convertible(const CPPType &from_type, const CPPType &to_type) const + { + return conversions_.contains( + {fn::MFDataType::ForSingle(from_type), fn::MFDataType::ForSingle(to_type)}); + } + + void convert_to_uninitialized(const CPPType &from_type, + const CPPType &to_type, + const void *from_value, + void *to_value) const; + + fn::GVArray try_convert(fn::GVArray varray, const CPPType &to_type) const; + + fn::GVMutableArray try_convert(fn::GVMutableArray varray, const CPPType &to_type) const; +}; + +const DataTypeConversions &get_implicit_type_conversions(); + +} // namespace blender::bke diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt index a2179a42520..5a9b5735838 100644 --- a/source/blender/blenkernel/CMakeLists.txt +++ b/source/blender/blenkernel/CMakeLists.txt @@ -226,6 +226,7 @@ set(SRC intern/multires_versioning.c intern/nla.c intern/node.cc + intern/type_conversions.cc intern/object.cc intern/object_deform.c intern/object_dupli.cc @@ -456,6 +457,7 @@ set(SRC BKE_text_suggestions.h BKE_texture.h BKE_tracking.h + BKE_type_conversions.hh BKE_undo_system.h BKE_unit.h BKE_vfont.h diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc index d510a405b46..c1a83e65362 100644 --- a/source/blender/blenkernel/intern/attribute_access.cc +++ b/source/blender/blenkernel/intern/attribute_access.cc @@ -23,6 +23,7 @@ #include "BKE_geometry_set.hh" #include "BKE_mesh.h" #include "BKE_pointcloud.h" +#include "BKE_type_conversions.hh" #include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" @@ -36,8 +37,6 @@ #include "CLG_log.h" -#include "NOD_type_conversions.hh" - #include "attribute_access_intern.hh" static CLG_LogRef LOG = {"bke.attribute_access"}; @@ -766,8 +765,8 @@ GVArray CustomDataAttributes::get_for_read(const AttributeIDRef &attribute_id, if (attribute->type() == *type) { return GVArray::ForSpan(*attribute); } - const blender::nodes::DataTypeConversions &conversions = - blender::nodes::get_implicit_type_conversions(); + const blender::bke::DataTypeConversions &conversions = + blender::bke::get_implicit_type_conversions(); return conversions.try_convert(GVArray::ForSpan(*attribute), *type); } @@ -1112,8 +1111,8 @@ std::optional GeometryComponent::attribute_get_meta_data( static blender::fn::GVArray try_adapt_data_type(blender::fn::GVArray varray, const blender::fn::CPPType &to_type) { - const blender::nodes::DataTypeConversions &conversions = - blender::nodes::get_implicit_type_conversions(); + const blender::bke::DataTypeConversions &conversions = + blender::bke::get_implicit_type_conversions(); return conversions.try_convert(std::move(varray), to_type); } @@ -1178,8 +1177,8 @@ blender::bke::ReadAttributeLookup GeometryComponent::attribute_try_get_for_read( if (attribute.varray.type() == *type) { return attribute; } - const blender::nodes::DataTypeConversions &conversions = - blender::nodes::get_implicit_type_conversions(); + const blender::bke::DataTypeConversions &conversions = + blender::bke::get_implicit_type_conversions(); return {conversions.try_convert(std::move(attribute.varray), *type), attribute.domain}; } @@ -1299,7 +1298,7 @@ static OutputAttribute create_output_attribute(GeometryComponent &component, const CPPType *cpp_type = custom_data_type_to_cpp_type(data_type); BLI_assert(cpp_type != nullptr); - const nodes::DataTypeConversions &conversions = nodes::get_implicit_type_conversions(); + const DataTypeConversions &conversions = get_implicit_type_conversions(); if (component.attribute_is_builtin(attribute_id)) { const StringRef attribute_name = attribute_id.name(); diff --git a/source/blender/blenkernel/intern/type_conversions.cc b/source/blender/blenkernel/intern/type_conversions.cc new file mode 100644 index 00000000000..ab42d508538 --- /dev/null +++ b/source/blender/blenkernel/intern/type_conversions.cc @@ -0,0 +1,347 @@ +/* + * 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. + */ + +#include "BKE_type_conversions.hh" + +#include "FN_multi_function_builder.hh" + +#include "BLI_color.hh" +#include "BLI_float2.hh" +#include "BLI_float3.hh" + +namespace blender::bke { + +using fn::MFDataType; + +template +static void add_implicit_conversion(DataTypeConversions &conversions) +{ + static const CPPType &from_type = CPPType::get(); + static const CPPType &to_type = CPPType::get(); + static const std::string conversion_name = from_type.name() + " to " + to_type.name(); + + static fn::CustomMF_SI_SO multi_function{conversion_name.c_str(), ConversionF}; + static auto convert_single_to_initialized = [](const void *src, void *dst) { + *(To *)dst = ConversionF(*(const From *)src); + }; + static auto convert_single_to_uninitialized = [](const void *src, void *dst) { + new (dst) To(ConversionF(*(const From *)src)); + }; + conversions.add(fn::MFDataType::ForSingle(), + fn::MFDataType::ForSingle(), + multi_function, + convert_single_to_initialized, + convert_single_to_uninitialized); +} + +static float2 float_to_float2(const float &a) +{ + return float2(a); +} +static float3 float_to_float3(const float &a) +{ + return float3(a); +} +static int32_t float_to_int(const float &a) +{ + return (int32_t)a; +} +static bool float_to_bool(const float &a) +{ + return a > 0.0f; +} +static ColorGeometry4f float_to_color(const float &a) +{ + return ColorGeometry4f(a, a, a, 1.0f); +} + +static float3 float2_to_float3(const float2 &a) +{ + return float3(a.x, a.y, 0.0f); +} +static float float2_to_float(const float2 &a) +{ + return (a.x + a.y) / 2.0f; +} +static int float2_to_int(const float2 &a) +{ + return (int32_t)((a.x + a.y) / 2.0f); +} +static bool float2_to_bool(const float2 &a) +{ + return !is_zero_v2(a); +} +static ColorGeometry4f float2_to_color(const float2 &a) +{ + return ColorGeometry4f(a.x, a.y, 0.0f, 1.0f); +} + +static bool float3_to_bool(const float3 &a) +{ + return !is_zero_v3(a); +} +static float float3_to_float(const float3 &a) +{ + return (a.x + a.y + a.z) / 3.0f; +} +static int float3_to_int(const float3 &a) +{ + return (int)((a.x + a.y + a.z) / 3.0f); +} +static float2 float3_to_float2(const float3 &a) +{ + return float2(a); +} +static ColorGeometry4f float3_to_color(const float3 &a) +{ + return ColorGeometry4f(a.x, a.y, a.z, 1.0f); +} + +static bool int_to_bool(const int32_t &a) +{ + return a > 0; +} +static float int_to_float(const int32_t &a) +{ + return (float)a; +} +static float2 int_to_float2(const int32_t &a) +{ + return float2((float)a); +} +static float3 int_to_float3(const int32_t &a) +{ + return float3((float)a); +} +static ColorGeometry4f int_to_color(const int32_t &a) +{ + return ColorGeometry4f((float)a, (float)a, (float)a, 1.0f); +} + +static float bool_to_float(const bool &a) +{ + return (bool)a; +} +static int32_t bool_to_int(const bool &a) +{ + return (int32_t)a; +} +static float2 bool_to_float2(const bool &a) +{ + return (a) ? float2(1.0f) : float2(0.0f); +} +static float3 bool_to_float3(const bool &a) +{ + return (a) ? float3(1.0f) : float3(0.0f); +} +static ColorGeometry4f bool_to_color(const bool &a) +{ + return (a) ? ColorGeometry4f(1.0f, 1.0f, 1.0f, 1.0f) : ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f); +} + +static bool color_to_bool(const ColorGeometry4f &a) +{ + return rgb_to_grayscale(a) > 0.0f; +} +static float color_to_float(const ColorGeometry4f &a) +{ + return rgb_to_grayscale(a); +} +static int32_t color_to_int(const ColorGeometry4f &a) +{ + return (int)rgb_to_grayscale(a); +} +static float2 color_to_float2(const ColorGeometry4f &a) +{ + return float2(a.r, a.g); +} +static float3 color_to_float3(const ColorGeometry4f &a) +{ + return float3(a.r, a.g, a.b); +} + +static DataTypeConversions create_implicit_conversions() +{ + DataTypeConversions conversions; + + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + add_implicit_conversion(conversions); + + return conversions; +} + +const DataTypeConversions &get_implicit_type_conversions() +{ + static const DataTypeConversions conversions = create_implicit_conversions(); + return conversions; +} + +void DataTypeConversions::convert_to_uninitialized(const CPPType &from_type, + const CPPType &to_type, + const void *from_value, + void *to_value) const +{ + if (from_type == to_type) { + from_type.copy_construct(from_value, to_value); + return; + } + + const ConversionFunctions *functions = this->get_conversion_functions( + MFDataType::ForSingle(from_type), MFDataType::ForSingle(to_type)); + BLI_assert(functions != nullptr); + + functions->convert_single_to_uninitialized(from_value, to_value); +} + +class GVArray_For_ConvertedGVArray : public fn::GVArrayImpl { + private: + fn::GVArray varray_; + const CPPType &from_type_; + ConversionFunctions old_to_new_conversions_; + + public: + GVArray_For_ConvertedGVArray(fn::GVArray varray, + const CPPType &to_type, + const DataTypeConversions &conversions) + : fn::GVArrayImpl(to_type, varray.size()), + varray_(std::move(varray)), + from_type_(varray_.type()) + { + old_to_new_conversions_ = *conversions.get_conversion_functions(from_type_, to_type); + } + + private: + void get(const int64_t index, void *r_value) const override + { + BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); + varray_.get(index, buffer); + old_to_new_conversions_.convert_single_to_initialized(buffer, r_value); + from_type_.destruct(buffer); + } + + void get_to_uninitialized(const int64_t index, void *r_value) const override + { + BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); + varray_.get(index, buffer); + old_to_new_conversions_.convert_single_to_uninitialized(buffer, r_value); + from_type_.destruct(buffer); + } +}; + +class GVMutableArray_For_ConvertedGVMutableArray : public fn::GVMutableArrayImpl { + private: + fn::GVMutableArray varray_; + const CPPType &from_type_; + ConversionFunctions old_to_new_conversions_; + ConversionFunctions new_to_old_conversions_; + + public: + GVMutableArray_For_ConvertedGVMutableArray(fn::GVMutableArray varray, + const CPPType &to_type, + const DataTypeConversions &conversions) + : fn::GVMutableArrayImpl(to_type, varray.size()), + varray_(std::move(varray)), + from_type_(varray_.type()) + { + old_to_new_conversions_ = *conversions.get_conversion_functions(from_type_, to_type); + new_to_old_conversions_ = *conversions.get_conversion_functions(to_type, from_type_); + } + + private: + void get(const int64_t index, void *r_value) const override + { + BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); + varray_.get(index, buffer); + old_to_new_conversions_.convert_single_to_initialized(buffer, r_value); + from_type_.destruct(buffer); + } + + void get_to_uninitialized(const int64_t index, void *r_value) const override + { + BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); + varray_.get(index, buffer); + old_to_new_conversions_.convert_single_to_uninitialized(buffer, r_value); + from_type_.destruct(buffer); + } + + void set_by_move(const int64_t index, void *value) override + { + BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); + new_to_old_conversions_.convert_single_to_uninitialized(value, buffer); + varray_.set_by_relocate(index, buffer); + } +}; + +fn::GVArray DataTypeConversions::try_convert(fn::GVArray varray, const CPPType &to_type) const +{ + const CPPType &from_type = varray.type(); + if (from_type == to_type) { + return varray; + } + if (!this->is_convertible(from_type, to_type)) { + return {}; + } + return fn::GVArray::For(std::move(varray), to_type, *this); +} + +fn::GVMutableArray DataTypeConversions::try_convert(fn::GVMutableArray varray, + const CPPType &to_type) const +{ + const CPPType &from_type = varray.type(); + if (from_type == to_type) { + return varray; + } + if (!this->is_convertible(from_type, to_type)) { + return {}; + } + return fn::GVMutableArray::For( + std::move(varray), to_type, *this); +} + +} // namespace blender::bke diff --git a/source/blender/modifiers/intern/MOD_nodes_evaluator.cc b/source/blender/modifiers/intern/MOD_nodes_evaluator.cc index dee9329e21b..4e808120f4a 100644 --- a/source/blender/modifiers/intern/MOD_nodes_evaluator.cc +++ b/source/blender/modifiers/intern/MOD_nodes_evaluator.cc @@ -16,9 +16,10 @@ #include "MOD_nodes_evaluator.hh" +#include "BKE_type_conversions.hh" + #include "NOD_geometry_exec.hh" #include "NOD_socket_declarations.hh" -#include "NOD_type_conversions.hh" #include "DEG_depsgraph_query.h" @@ -449,7 +450,7 @@ class GeometryNodesEvaluator { TaskPool *task_pool_ = nullptr; GeometryNodesEvaluationParams ¶ms_; - const blender::nodes::DataTypeConversions &conversions_; + const blender::bke::DataTypeConversions &conversions_; friend NodeParamsProvider; @@ -457,7 +458,7 @@ class GeometryNodesEvaluator { GeometryNodesEvaluator(GeometryNodesEvaluationParams ¶ms) : outer_allocator_(params.allocator), params_(params), - conversions_(blender::nodes::get_implicit_type_conversions()) + conversions_(blender::bke::get_implicit_type_conversions()) { } diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index c8a14f25f21..da7b7712267 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -288,7 +288,6 @@ set(SRC intern/node_socket_declarations.cc intern/node_tree_ref.cc intern/node_util.c - intern/type_conversions.cc composite/node_composite_util.hh function/node_function_util.hh @@ -312,7 +311,6 @@ set(SRC NOD_socket_declarations_geometry.hh NOD_static_types.h NOD_texture.h - NOD_type_conversions.hh intern/node_common.h intern/node_exec.h intern/node_util.h diff --git a/source/blender/nodes/NOD_type_conversions.hh b/source/blender/nodes/NOD_type_conversions.hh deleted file mode 100644 index c8b24fd1260..00000000000 --- a/source/blender/nodes/NOD_type_conversions.hh +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 - -#include "FN_multi_function.hh" - -namespace blender::nodes { - -using fn::CPPType; - -struct ConversionFunctions { - const fn::MultiFunction *multi_function; - void (*convert_single_to_initialized)(const void *src, void *dst); - void (*convert_single_to_uninitialized)(const void *src, void *dst); -}; - -class DataTypeConversions { - private: - Map, ConversionFunctions> conversions_; - - public: - void add(fn::MFDataType from_type, - fn::MFDataType to_type, - const fn::MultiFunction &fn, - void (*convert_single_to_initialized)(const void *src, void *dst), - void (*convert_single_to_uninitialized)(const void *src, void *dst)) - { - conversions_.add_new({from_type, to_type}, - {&fn, convert_single_to_initialized, convert_single_to_uninitialized}); - } - - const ConversionFunctions *get_conversion_functions(fn::MFDataType from, fn::MFDataType to) const - { - return conversions_.lookup_ptr({from, to}); - } - - const ConversionFunctions *get_conversion_functions(const CPPType &from, const CPPType &to) const - { - return this->get_conversion_functions(fn::MFDataType::ForSingle(from), - fn::MFDataType::ForSingle(to)); - } - - const fn::MultiFunction *get_conversion_multi_function(fn::MFDataType from, - fn::MFDataType to) const - { - const ConversionFunctions *functions = this->get_conversion_functions(from, to); - return functions ? functions->multi_function : nullptr; - } - - bool is_convertible(const CPPType &from_type, const CPPType &to_type) const - { - return conversions_.contains( - {fn::MFDataType::ForSingle(from_type), fn::MFDataType::ForSingle(to_type)}); - } - - void convert_to_uninitialized(const CPPType &from_type, - const CPPType &to_type, - const void *from_value, - void *to_value) const; - - fn::GVArray try_convert(fn::GVArray varray, const CPPType &to_type) const; - - fn::GVMutableArray try_convert(fn::GVMutableArray varray, const CPPType &to_type) const; -}; - -const DataTypeConversions &get_implicit_type_conversions(); - -} // namespace blender::nodes diff --git a/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc b/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc index 82dd6e294e9..48bec4cf9da 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc @@ -19,12 +19,11 @@ #include "BKE_mesh_runtime.h" #include "BKE_pointcloud.h" #include "BKE_spline.hh" +#include "BKE_type_conversions.hh" #include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" -#include "NOD_type_conversions.hh" - #include "node_geometry_util.hh" namespace blender::nodes::node_geo_join_geometry_cc { diff --git a/source/blender/nodes/intern/node_geometry_exec.cc b/source/blender/nodes/intern/node_geometry_exec.cc index ce87b40fec7..ca89e4f67c3 100644 --- a/source/blender/nodes/intern/node_geometry_exec.cc +++ b/source/blender/nodes/intern/node_geometry_exec.cc @@ -18,8 +18,9 @@ #include "DEG_depsgraph_query.h" +#include "BKE_type_conversions.hh" + #include "NOD_geometry_exec.hh" -#include "NOD_type_conversions.hh" #include "node_geometry_util.hh" @@ -149,7 +150,7 @@ GVArray GeoNodeExecParams::get_input_attribute(const StringRef name, } return GVArray::ForSingle(*cpp_type, domain_size, default_value); } - const DataTypeConversions &conversions = get_implicit_type_conversions(); + const bke::DataTypeConversions &conversions = bke::get_implicit_type_conversions(); if (found_socket->type == SOCK_FLOAT) { const float value = this->get_input(found_socket->identifier); BUFFER_FOR_CPP_TYPE_VALUE(*cpp_type, buffer); diff --git a/source/blender/nodes/intern/type_conversions.cc b/source/blender/nodes/intern/type_conversions.cc deleted file mode 100644 index d4cd7dc6bfe..00000000000 --- a/source/blender/nodes/intern/type_conversions.cc +++ /dev/null @@ -1,347 +0,0 @@ -/* - * 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. - */ - -#include "NOD_type_conversions.hh" - -#include "FN_multi_function_builder.hh" - -#include "BLI_color.hh" -#include "BLI_float2.hh" -#include "BLI_float3.hh" - -namespace blender::nodes { - -using fn::MFDataType; - -template -static void add_implicit_conversion(DataTypeConversions &conversions) -{ - static const CPPType &from_type = CPPType::get(); - static const CPPType &to_type = CPPType::get(); - static const std::string conversion_name = from_type.name() + " to " + to_type.name(); - - static fn::CustomMF_SI_SO multi_function{conversion_name.c_str(), ConversionF}; - static auto convert_single_to_initialized = [](const void *src, void *dst) { - *(To *)dst = ConversionF(*(const From *)src); - }; - static auto convert_single_to_uninitialized = [](const void *src, void *dst) { - new (dst) To(ConversionF(*(const From *)src)); - }; - conversions.add(fn::MFDataType::ForSingle(), - fn::MFDataType::ForSingle(), - multi_function, - convert_single_to_initialized, - convert_single_to_uninitialized); -} - -static float2 float_to_float2(const float &a) -{ - return float2(a); -} -static float3 float_to_float3(const float &a) -{ - return float3(a); -} -static int32_t float_to_int(const float &a) -{ - return (int32_t)a; -} -static bool float_to_bool(const float &a) -{ - return a > 0.0f; -} -static ColorGeometry4f float_to_color(const float &a) -{ - return ColorGeometry4f(a, a, a, 1.0f); -} - -static float3 float2_to_float3(const float2 &a) -{ - return float3(a.x, a.y, 0.0f); -} -static float float2_to_float(const float2 &a) -{ - return (a.x + a.y) / 2.0f; -} -static int float2_to_int(const float2 &a) -{ - return (int32_t)((a.x + a.y) / 2.0f); -} -static bool float2_to_bool(const float2 &a) -{ - return !is_zero_v2(a); -} -static ColorGeometry4f float2_to_color(const float2 &a) -{ - return ColorGeometry4f(a.x, a.y, 0.0f, 1.0f); -} - -static bool float3_to_bool(const float3 &a) -{ - return !is_zero_v3(a); -} -static float float3_to_float(const float3 &a) -{ - return (a.x + a.y + a.z) / 3.0f; -} -static int float3_to_int(const float3 &a) -{ - return (int)((a.x + a.y + a.z) / 3.0f); -} -static float2 float3_to_float2(const float3 &a) -{ - return float2(a); -} -static ColorGeometry4f float3_to_color(const float3 &a) -{ - return ColorGeometry4f(a.x, a.y, a.z, 1.0f); -} - -static bool int_to_bool(const int32_t &a) -{ - return a > 0; -} -static float int_to_float(const int32_t &a) -{ - return (float)a; -} -static float2 int_to_float2(const int32_t &a) -{ - return float2((float)a); -} -static float3 int_to_float3(const int32_t &a) -{ - return float3((float)a); -} -static ColorGeometry4f int_to_color(const int32_t &a) -{ - return ColorGeometry4f((float)a, (float)a, (float)a, 1.0f); -} - -static float bool_to_float(const bool &a) -{ - return (bool)a; -} -static int32_t bool_to_int(const bool &a) -{ - return (int32_t)a; -} -static float2 bool_to_float2(const bool &a) -{ - return (a) ? float2(1.0f) : float2(0.0f); -} -static float3 bool_to_float3(const bool &a) -{ - return (a) ? float3(1.0f) : float3(0.0f); -} -static ColorGeometry4f bool_to_color(const bool &a) -{ - return (a) ? ColorGeometry4f(1.0f, 1.0f, 1.0f, 1.0f) : ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f); -} - -static bool color_to_bool(const ColorGeometry4f &a) -{ - return rgb_to_grayscale(a) > 0.0f; -} -static float color_to_float(const ColorGeometry4f &a) -{ - return rgb_to_grayscale(a); -} -static int32_t color_to_int(const ColorGeometry4f &a) -{ - return (int)rgb_to_grayscale(a); -} -static float2 color_to_float2(const ColorGeometry4f &a) -{ - return float2(a.r, a.g); -} -static float3 color_to_float3(const ColorGeometry4f &a) -{ - return float3(a.r, a.g, a.b); -} - -static DataTypeConversions create_implicit_conversions() -{ - DataTypeConversions conversions; - - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - add_implicit_conversion(conversions); - - return conversions; -} - -const DataTypeConversions &get_implicit_type_conversions() -{ - static const DataTypeConversions conversions = create_implicit_conversions(); - return conversions; -} - -void DataTypeConversions::convert_to_uninitialized(const CPPType &from_type, - const CPPType &to_type, - const void *from_value, - void *to_value) const -{ - if (from_type == to_type) { - from_type.copy_construct(from_value, to_value); - return; - } - - const ConversionFunctions *functions = this->get_conversion_functions( - MFDataType::ForSingle(from_type), MFDataType::ForSingle(to_type)); - BLI_assert(functions != nullptr); - - functions->convert_single_to_uninitialized(from_value, to_value); -} - -class GVArray_For_ConvertedGVArray : public fn::GVArrayImpl { - private: - fn::GVArray varray_; - const CPPType &from_type_; - ConversionFunctions old_to_new_conversions_; - - public: - GVArray_For_ConvertedGVArray(fn::GVArray varray, - const CPPType &to_type, - const DataTypeConversions &conversions) - : fn::GVArrayImpl(to_type, varray.size()), - varray_(std::move(varray)), - from_type_(varray_.type()) - { - old_to_new_conversions_ = *conversions.get_conversion_functions(from_type_, to_type); - } - - private: - void get(const int64_t index, void *r_value) const override - { - BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); - varray_.get(index, buffer); - old_to_new_conversions_.convert_single_to_initialized(buffer, r_value); - from_type_.destruct(buffer); - } - - void get_to_uninitialized(const int64_t index, void *r_value) const override - { - BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); - varray_.get(index, buffer); - old_to_new_conversions_.convert_single_to_uninitialized(buffer, r_value); - from_type_.destruct(buffer); - } -}; - -class GVMutableArray_For_ConvertedGVMutableArray : public fn::GVMutableArrayImpl { - private: - fn::GVMutableArray varray_; - const CPPType &from_type_; - ConversionFunctions old_to_new_conversions_; - ConversionFunctions new_to_old_conversions_; - - public: - GVMutableArray_For_ConvertedGVMutableArray(fn::GVMutableArray varray, - const CPPType &to_type, - const DataTypeConversions &conversions) - : fn::GVMutableArrayImpl(to_type, varray.size()), - varray_(std::move(varray)), - from_type_(varray_.type()) - { - old_to_new_conversions_ = *conversions.get_conversion_functions(from_type_, to_type); - new_to_old_conversions_ = *conversions.get_conversion_functions(to_type, from_type_); - } - - private: - void get(const int64_t index, void *r_value) const override - { - BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); - varray_.get(index, buffer); - old_to_new_conversions_.convert_single_to_initialized(buffer, r_value); - from_type_.destruct(buffer); - } - - void get_to_uninitialized(const int64_t index, void *r_value) const override - { - BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); - varray_.get(index, buffer); - old_to_new_conversions_.convert_single_to_uninitialized(buffer, r_value); - from_type_.destruct(buffer); - } - - void set_by_move(const int64_t index, void *value) override - { - BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer); - new_to_old_conversions_.convert_single_to_uninitialized(value, buffer); - varray_.set_by_relocate(index, buffer); - } -}; - -fn::GVArray DataTypeConversions::try_convert(fn::GVArray varray, const CPPType &to_type) const -{ - const CPPType &from_type = varray.type(); - if (from_type == to_type) { - return varray; - } - if (!this->is_convertible(from_type, to_type)) { - return {}; - } - return fn::GVArray::For(std::move(varray), to_type, *this); -} - -fn::GVMutableArray DataTypeConversions::try_convert(fn::GVMutableArray varray, - const CPPType &to_type) const -{ - const CPPType &from_type = varray.type(); - if (from_type == to_type) { - return varray; - } - if (!this->is_convertible(from_type, to_type)) { - return {}; - } - return fn::GVMutableArray::For( - std::move(varray), to_type, *this); -} - -} // namespace blender::nodes -- cgit v1.2.3