Blender V5.3
node_shader_mix_rgb.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "BKE_material.hh"
13
14#include "BLI_math_vector_c.hh"
15
16#include "DNA_material_types.h"
17
18#include "NOD_multi_function.hh"
19
20namespace blender {
21
23
25{
26 b.is_function_node();
27 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
28 .default_value(0.5f)
29 .min(0.0f)
30 .max(1.0f)
32 b.add_input<decl::Color>("Color1"_ustr).default_value({0.5f, 0.5f, 0.5f, 1.0f});
33 b.add_input<decl::Color>("Color2"_ustr).default_value({0.5f, 0.5f, 0.5f, 1.0f});
34 b.add_output<decl::Color>("Color"_ustr);
35}
36
37static const char *gpu_shader_get_name(int mode)
38{
39 switch (mode) {
40 case MA_RAMP_BLEND:
41 return "mix_blend";
42 case MA_RAMP_ADD:
43 return "mix_add";
44 case MA_RAMP_MULT:
45 return "mix_mult";
46 case MA_RAMP_SUB:
47 return "mix_sub";
48 case MA_RAMP_SCREEN:
49 return "mix_screen";
50 case MA_RAMP_DIV:
51 return "mix_div_fallback";
52 case MA_RAMP_DIFF:
53 return "mix_diff";
55 return "mix_exclusion";
56 case MA_RAMP_DARK:
57 return "mix_dark";
58 case MA_RAMP_LIGHT:
59 return "mix_light";
60 case MA_RAMP_OVERLAY:
61 return "mix_overlay";
62 case MA_RAMP_DODGE:
63 return "mix_dodge";
64 case MA_RAMP_BURN:
65 return "mix_burn";
66 case MA_RAMP_HUE:
67 return "mix_hue";
68 case MA_RAMP_SAT:
69 return "mix_sat";
70 case MA_RAMP_VAL:
71 return "mix_val";
72 case MA_RAMP_COLOR:
73 return "mix_color";
74 case MA_RAMP_SOFT:
75 return "mix_soft";
76 case MA_RAMP_LINEAR:
77 return "mix_linear";
78 }
79
80 return nullptr;
81}
82
84 bNode *node,
85 bNodeExecData * /*execdata*/,
88{
89 const char *name = gpu_shader_get_name(node->custom1);
90
91 if (name == nullptr) {
92 return 0;
93 }
94
95 const float min = 0.0f;
96 const float max = 1.0f;
97 const GPUNodeLink *factor_link = in[0].link ? in[0].link : GPU_uniform(in[0]);
98 GPU_link(mat, "clamp_value", factor_link, GPU_constant(&min), GPU_constant(&max), &in[0].link);
99
100 int ret = GPU_stack_link(mat, node, name, in, out);
101
102 if (ret && node->custom2 & SHD_MIXRGB_CLAMP) {
103 const float min[3] = {0.0f, 0.0f, 0.0f};
104 const float max[3] = {1.0f, 1.0f, 1.0f};
105 GPU_link(mat, "clamp_color", out[0].link, GPU_constant(min), GPU_constant(max), &out[0].link);
106 }
107 return ret;
108}
109
110class MixRGBFunction : public mf::MultiFunction {
111 private:
112 bool clamp_;
113 int type_;
114
115 public:
116 MixRGBFunction(bool clamp, int type) : clamp_(clamp), type_(type)
117 {
118 static const mf::Signature signature = []() {
119 mf::Signature signature;
120 mf::SignatureBuilder builder{"MixRGB", signature};
121 builder.single_input<float>("Fac");
122 builder.single_input<ColorGeometry4f>("Color1");
123 builder.single_input<ColorGeometry4f>("Color2");
124 builder.single_output<ColorGeometry4f>("Color");
125 return signature;
126 }();
127 this->set_signature(&signature);
128 }
129
130 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
131 {
132 const VArray<float> &fac = params.readonly_single_input<float>(0, "Fac");
133 const VArray<ColorGeometry4f> &col1 = params.readonly_single_input<ColorGeometry4f>(1,
134 "Color1");
135 const VArray<ColorGeometry4f> &col2 = params.readonly_single_input<ColorGeometry4f>(2,
136 "Color2");
137 MutableSpan<ColorGeometry4f> results = params.uninitialized_single_output<ColorGeometry4f>(
138 3, "Color");
139
140 mask.foreach_index([&](const int64_t i) {
141 results[i] = col1[i];
142 ramp_blend(type_, results[i], clamp_f(fac[i], 0.0f, 1.0f), col2[i]);
143 });
144
145 if (clamp_) {
146 mask.foreach_index_optimized<int64_t>(
147 [&](const int64_t i) { clamp_v4(results[i], 0.0f, 1.0f); });
148 }
149 }
150};
151
153{
154 const bNode &node = builder.node();
155 bool clamp = node.custom2 & SHD_MIXRGB_CLAMP;
156 int mix_type = node.custom1;
158}
159
160} // namespace nodes::node_shader_mix_rgb_cc
161
163{
164 namespace file_ns = nodes::node_shader_mix_rgb_cc;
165
166 static bke::bNodeType ntype;
167
168 common_node_type_base(&ntype, "ShaderNodeMixRGB"_ustr, SH_NODE_MIX_RGB_LEGACY);
169 ntype.ui_name = "Mix (Legacy)";
170 ntype.ui_description = "Mix two input colors";
171 ntype.enum_name_legacy = "MIX_RGB";
173 ntype.declare = file_ns::sh_node_mix_rgb_declare;
175 ntype.gpu_fn = file_ns::gpu_shader_mix_rgb;
176 ntype.build_multi_function = file_ns::sh_node_mix_rgb_build_multi_function;
177 ntype.gather_link_search_ops = nullptr;
179}
180
181} // namespace blender
General operations, lookup, etc. for materials.
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define SH_NODE_MIX_RGB_LEGACY
btScalar max
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
constexpr T clamp(T, U, U) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static int gpu_shader_mix_rgb(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_mix_rgb_build_multi_function(NodeMultiFunctionBuilder &builder)
static void sh_node_mix_rgb_declare(NodeDeclarationBuilder &b)
static const char * gpu_shader_get_name(int mode)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
void ramp_blend(int type, float r_col[4], float fac, const float col[4])
GPUNodeLink * GPU_uniform(const float *num)
void node_blend_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:133
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
MINLINE void clamp_v4(float vec[4], float min, float max)
void register_node_type_sh_mix_rgb()
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
@ PROP_FACTOR
Definition RNA_types.hh:308
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
MINLINE float clamp_f(float value, float min, float max)
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393