Blender V5.3
node_fn_float_to_int.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cmath>
6
7#include "BLI_string_utf8.hh"
8
9#include "RNA_enum_types.hh"
10
12#include "UI_resources.hh"
13
14#include "node_function_util.hh"
15#include "node_shader_util.hh"
16
18
20{
21 b.is_function_node();
22 b.add_input<decl::Float>("Float"_ustr);
23 b.add_output<decl::Int>("Integer"_ustr);
24}
25
26static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
27{
28 layout.prop(ptr, "rounding_mode", UI_ITEM_NONE, "", ICON_NONE);
29}
30
31static void node_label(const bNodeTree * /*tree*/,
32 const bNode *node,
33 char *label,
34 int label_maxncpy)
35{
36 const char *name;
37 bool enum_label = RNA_enum_name(rna_enum_node_float_to_int_items, node->custom1, &name);
38 if (!enum_label) {
39 name = CTX_N_(BLT_I18NCONTEXT_ID_NODETREE, "Unknown");
40 }
41 BLI_strncpy_utf8(label, CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, name), label_maxncpy);
42}
43
44static const mf::MultiFunction *get_multi_function(const bNode &bnode)
45{
46 static auto exec_preset = mf::build::exec_presets::AllSpanOrSingle();
47 static auto round_fn = mf::build::SI1_SO<float, int>(
48 "Round", [](float a) { return int(round(a)); }, exec_preset);
49 static auto floor_fn = mf::build::SI1_SO<float, int>(
50 "Floor", [](float a) { return int(floor(a)); }, exec_preset);
51 static auto ceil_fn = mf::build::SI1_SO<float, int>(
52 "Ceiling", [](float a) { return int(ceil(a)); }, exec_preset);
53 static auto trunc_fn = mf::build::SI1_SO<float, int>(
54 "Truncate", [](float a) { return int(trunc(a)); }, exec_preset);
55
56 switch (static_cast<FloatToIntRoundingMode>(bnode.custom1)) {
58 return &round_fn;
60 return &floor_fn;
62 return &ceil_fn;
64 return &trunc_fn;
65 }
66
68 return nullptr;
69}
70
72{
73 const mf::MultiFunction *fn = get_multi_function(builder.node());
74 builder.set_matching_fn(fn);
75}
76
77static const char *gpu_shader_get_name(const FloatToIntRoundingMode rounding_mode)
78{
79 switch (rounding_mode) {
81 return "float_to_int_round";
83 return "float_to_int_floor";
85 return "float_to_int_ceil";
87 return "float_to_int_truncate";
88 }
89
91 return nullptr;
92}
93
95 bNode *node,
96 bNodeExecData * /*execdata*/,
99{
100 const char *name = gpu_shader_get_name(FloatToIntRoundingMode(node->custom1));
101
102 if (name == nullptr) {
103 return 0;
104 }
105
106 return GPU_stack_link(mat, node, name, in, out);
107}
108
109static void node_register()
110{
111 static bke::bNodeType ntype;
112
113 fn_cmp_node_type_base(&ntype, "FunctionNodeFloatToInt"_ustr, FN_NODE_FLOAT_TO_INT);
114 ntype.ui_name = "Float to Integer";
115 ntype.ui_description =
116 "Convert the given floating-point number to an integer, with a choice of methods";
117 ntype.enum_name_legacy = "FLOAT_TO_INT";
119 ntype.declare = node_declare;
120 ntype.labelfunc = node_label;
125}
127
128} // namespace blender::nodes::node_fn_float_to_int_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_FLOAT_TO_INT
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
void set_matching_fn(const mf::MultiFunction *fn)
#define round
#define trunc
#define floor
#define ceil
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static const char * gpu_shader_get_name(const FloatToIntRoundingMode rounding_mode)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_node_float_to_int_items[]
@ FN_NODE_FLOAT_TO_INT_FLOOR
@ FN_NODE_FLOAT_TO_INT_CEIL
@ FN_NODE_FLOAT_TO_INT_TRUNCATE
@ FN_NODE_FLOAT_TO_INT_ROUND
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)