From 83955d6769da8d1a50e2fe1fc03d5ab9d93420e2 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Thu, 16 Jul 2020 13:25:57 +0200 Subject: Particles: support Map Range node Only linear interpolation mode is supported for now. --- .../blenkernel/BKE_node_tree_multi_function.hh | 5 + source/blender/nodes/CMakeLists.txt | 2 +- .../nodes/shader/nodes/node_shader_map_range.c | 91 ------------ .../nodes/shader/nodes/node_shader_map_range.cc | 165 +++++++++++++++++++++ 4 files changed, 171 insertions(+), 92 deletions(-) delete mode 100644 source/blender/nodes/shader/nodes/node_shader_map_range.c create mode 100644 source/blender/nodes/shader/nodes/node_shader_map_range.cc diff --git a/source/blender/blenkernel/BKE_node_tree_multi_function.hh b/source/blender/blenkernel/BKE_node_tree_multi_function.hh index 95a6c61da7f..7219eb1726a 100644 --- a/source/blender/blenkernel/BKE_node_tree_multi_function.hh +++ b/source/blender/blenkernel/BKE_node_tree_multi_function.hh @@ -348,6 +348,11 @@ class NodeMFNetworkBuilder : public MFNetworkBuilderBase { const fn::MultiFunction &get_default_fn(StringRef name); + const void set_not_implemented() + { + this->set_matching_fn(this->get_not_implemented_fn()); + } + /** * Tells the builder that the given function corresponds to the node that is being built. It will * try to match up sockets. For that it skips unavailable and non-data sockets. diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index 2381e499eee..a4b10e955a3 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -176,7 +176,7 @@ set(SRC shader/nodes/node_shader_layer_weight.c shader/nodes/node_shader_light_falloff.c shader/nodes/node_shader_light_path.c - shader/nodes/node_shader_map_range.c + shader/nodes/node_shader_map_range.cc shader/nodes/node_shader_mapping.c shader/nodes/node_shader_math.cc shader/nodes/node_shader_mixRgb.c diff --git a/source/blender/nodes/shader/nodes/node_shader_map_range.c b/source/blender/nodes/shader/nodes/node_shader_map_range.c deleted file mode 100644 index 5db7983e752..00000000000 --- a/source/blender/nodes/shader/nodes/node_shader_map_range.c +++ /dev/null @@ -1,91 +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. - * - * The Original Code is Copyright (C) 2005 Blender Foundation. - * All rights reserved. - */ - -/** \file - * \ingroup shdnodes - */ - -#include "node_shader_util.h" - -/* **************** Map Range ******************** */ -static bNodeSocketTemplate sh_node_map_range_in[] = { - {SOCK_FLOAT, N_("Value"), 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, PROP_NONE}, - {SOCK_FLOAT, N_("From Min"), 0.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, - {SOCK_FLOAT, N_("From Max"), 1.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, - {SOCK_FLOAT, N_("To Min"), 0.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, - {SOCK_FLOAT, N_("To Max"), 1.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, - {SOCK_FLOAT, N_("Steps"), 4.0f, 1.0f, 1.0f, 1.0f, 0.0f, 10000.0f, PROP_NONE}, - {-1, ""}, -}; -static bNodeSocketTemplate sh_node_map_range_out[] = { - {SOCK_FLOAT, N_("Result")}, - {-1, ""}, -}; - -static void node_shader_update_map_range(bNodeTree *UNUSED(ntree), bNode *node) -{ - bNodeSocket *sockSteps = nodeFindSocket(node, SOCK_IN, "Steps"); - nodeSetSocketAvailability(sockSteps, node->custom2 == NODE_MAP_RANGE_STEPPED); -} - -static void node_shader_init_map_range(bNodeTree *UNUSED(ntree), bNode *node) -{ - node->custom1 = true; /* use_clamp */ - node->custom2 = NODE_MAP_RANGE_LINEAR; /* interpolation */ -} - -static int gpu_shader_map_range(GPUMaterial *mat, - bNode *node, - bNodeExecData *UNUSED(execdata), - GPUNodeStack *in, - GPUNodeStack *out) -{ - static const char *names[] = { - [NODE_MAP_RANGE_LINEAR] = "map_range_linear", - [NODE_MAP_RANGE_STEPPED] = "map_range_stepped", - [NODE_MAP_RANGE_SMOOTHSTEP] = "map_range_smoothstep", - [NODE_MAP_RANGE_SMOOTHERSTEP] = "map_range_smootherstep", - }; - - int ret = 0; - if (node->custom2 < ARRAY_SIZE(names) && names[node->custom2]) { - ret = GPU_stack_link(mat, node, names[node->custom2], in, out); - } - else { - ret = GPU_stack_link(mat, node, "map_range_linear", in, out); - } - if (ret && node->custom1 && - !ELEM(node->custom2, NODE_MAP_RANGE_SMOOTHSTEP, NODE_MAP_RANGE_SMOOTHERSTEP)) { - GPU_link(mat, "clamp_range", out[0].link, in[3].link, in[4].link, &out[0].link); - } - return ret; -} - -void register_node_type_sh_map_range(void) -{ - static bNodeType ntype; - - sh_fn_node_type_base(&ntype, SH_NODE_MAP_RANGE, "Map Range", NODE_CLASS_CONVERTOR, 0); - node_type_socket_templates(&ntype, sh_node_map_range_in, sh_node_map_range_out); - node_type_init(&ntype, node_shader_init_map_range); - node_type_update(&ntype, node_shader_update_map_range); - node_type_gpu(&ntype, gpu_shader_map_range); - - nodeRegisterType(&ntype); -} diff --git a/source/blender/nodes/shader/nodes/node_shader_map_range.cc b/source/blender/nodes/shader/nodes/node_shader_map_range.cc new file mode 100644 index 00000000000..3f8a516c175 --- /dev/null +++ b/source/blender/nodes/shader/nodes/node_shader_map_range.cc @@ -0,0 +1,165 @@ +/* + * 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. + * + * The Original Code is Copyright (C) 2005 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup shdnodes + */ + +#include "node_shader_util.h" + +/* **************** Map Range ******************** */ +static bNodeSocketTemplate sh_node_map_range_in[] = { + {SOCK_FLOAT, N_("Value"), 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, PROP_NONE}, + {SOCK_FLOAT, N_("From Min"), 0.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, + {SOCK_FLOAT, N_("From Max"), 1.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, + {SOCK_FLOAT, N_("To Min"), 0.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, + {SOCK_FLOAT, N_("To Max"), 1.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, + {SOCK_FLOAT, N_("Steps"), 4.0f, 1.0f, 1.0f, 1.0f, 0.0f, 10000.0f, PROP_NONE}, + {-1, ""}, +}; +static bNodeSocketTemplate sh_node_map_range_out[] = { + {SOCK_FLOAT, N_("Result")}, + {-1, ""}, +}; + +static void node_shader_update_map_range(bNodeTree *UNUSED(ntree), bNode *node) +{ + bNodeSocket *sockSteps = nodeFindSocket(node, SOCK_IN, "Steps"); + nodeSetSocketAvailability(sockSteps, node->custom2 == NODE_MAP_RANGE_STEPPED); +} + +static void node_shader_init_map_range(bNodeTree *UNUSED(ntree), bNode *node) +{ + node->custom1 = true; /* use_clamp */ + node->custom2 = NODE_MAP_RANGE_LINEAR; /* interpolation */ +} + +static const char *gpu_shader_get_name(int mode) +{ + switch (mode) { + case NODE_MAP_RANGE_LINEAR: + return "map_range_linear"; + case NODE_MAP_RANGE_STEPPED: + return "map_range_stepped"; + case NODE_MAP_RANGE_SMOOTHSTEP: + return "map_range_smoothstep"; + case NODE_MAP_RANGE_SMOOTHERSTEP: + return "map_range_smootherstep"; + } + + return nullptr; +} + +static int gpu_shader_map_range(GPUMaterial *mat, + bNode *node, + bNodeExecData *UNUSED(execdata), + GPUNodeStack *in, + GPUNodeStack *out) +{ + const char *name = gpu_shader_get_name(node->custom2); + + int ret = 0; + if (name != nullptr) { + ret = GPU_stack_link(mat, node, name, in, out); + } + else { + ret = GPU_stack_link(mat, node, "map_range_linear", in, out); + } + if (ret && node->custom1 && + !ELEM(node->custom2, NODE_MAP_RANGE_SMOOTHSTEP, NODE_MAP_RANGE_SMOOTHERSTEP)) { + GPU_link(mat, "clamp_range", out[0].link, in[3].link, in[4].link, &out[0].link); + } + return ret; +} + +class MapRangeFunction : public blender::fn::MultiFunction { + private: + bool clamp_; + + public: + MapRangeFunction(bool clamp) : clamp_(clamp) + { + blender::fn::MFSignatureBuilder signature = this->get_builder("Map Range"); + signature.single_input("Value"); + signature.single_input("From Min"); + signature.single_input("From Max"); + signature.single_input("To Min"); + signature.single_input("To Max"); + signature.single_output("Result"); + } + + void call(blender::IndexMask mask, + 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"); + blender::MutableSpan results = params.uninitialized_single_output(5, "Result"); + + for (uint i : mask) { + float factor = safe_divide(values[i] - from_min[i], from_max[i] - from_min[i]); + results[i] = to_min[i] + factor * (to_max[i] - to_min[i]); + } + + if (clamp_) { + for (uint i : mask) { + CLAMP(results[i], 0.0f, 1.0f); + } + } + } +}; + +static void sh_node_map_range_expand_in_mf_network(blender::bke::NodeMFNetworkBuilder &builder) +{ + bNode &bnode = builder.bnode(); + bool clamp = bnode.custom1 != 0; + int interpolation_type = bnode.custom2; + + if (interpolation_type == NODE_MAP_RANGE_LINEAR) { + static MapRangeFunction fn_with_clamp{true}; + static MapRangeFunction fn_without_clamp{false}; + + if (clamp) { + builder.set_matching_fn(fn_with_clamp); + } + else { + builder.set_matching_fn(fn_without_clamp); + } + } + else { + builder.set_not_implemented(); + } +} + +void register_node_type_sh_map_range(void) +{ + static bNodeType ntype; + + sh_fn_node_type_base(&ntype, SH_NODE_MAP_RANGE, "Map Range", NODE_CLASS_CONVERTOR, 0); + node_type_socket_templates(&ntype, sh_node_map_range_in, sh_node_map_range_out); + node_type_init(&ntype, node_shader_init_map_range); + node_type_update(&ntype, node_shader_update_map_range); + node_type_gpu(&ntype, gpu_shader_map_range); + ntype.expand_in_mf_network = sh_node_map_range_expand_in_mf_network; + + nodeRegisterType(&ntype); +} -- cgit v1.2.3