Blender V5.0
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.h"
15
16#include "DNA_material_types.h"
17
18#include "NOD_multi_function.hh"
19
21
23{
24 b.is_function_node();
25 b.add_input<decl::Float>("Factor", "Fac")
26 .default_value(0.5f)
27 .min(0.0f)
28 .max(1.0f)
30 b.add_input<decl::Color>("Color1").default_value({0.5f, 0.5f, 0.5f, 1.0f});
31 b.add_input<decl::Color>("Color2").default_value({0.5f, 0.5f, 0.5f, 1.0f});
32 b.add_output<decl::Color>("Color");
33}
34
35static const char *gpu_shader_get_name(int mode)
36{
37 switch (mode) {
38 case MA_RAMP_BLEND:
39 return "mix_blend";
40 case MA_RAMP_ADD:
41 return "mix_add";
42 case MA_RAMP_MULT:
43 return "mix_mult";
44 case MA_RAMP_SUB:
45 return "mix_sub";
46 case MA_RAMP_SCREEN:
47 return "mix_screen";
48 case MA_RAMP_DIV:
49 return "mix_div_fallback";
50 case MA_RAMP_DIFF:
51 return "mix_diff";
53 return "mix_exclusion";
54 case MA_RAMP_DARK:
55 return "mix_dark";
56 case MA_RAMP_LIGHT:
57 return "mix_light";
58 case MA_RAMP_OVERLAY:
59 return "mix_overlay";
60 case MA_RAMP_DODGE:
61 return "mix_dodge";
62 case MA_RAMP_BURN:
63 return "mix_burn";
64 case MA_RAMP_HUE:
65 return "mix_hue";
66 case MA_RAMP_SAT:
67 return "mix_sat";
68 case MA_RAMP_VAL:
69 return "mix_val";
70 case MA_RAMP_COLOR:
71 return "mix_color";
72 case MA_RAMP_SOFT:
73 return "mix_soft";
74 case MA_RAMP_LINEAR:
75 return "mix_linear";
76 }
77
78 return nullptr;
79}
80
82 bNode *node,
83 bNodeExecData * /*execdata*/,
86{
87 const char *name = gpu_shader_get_name(node->custom1);
88
89 if (name == nullptr) {
90 return 0;
91 }
92
93 const float min = 0.0f;
94 const float max = 1.0f;
95 const GPUNodeLink *factor_link = in[0].link ? in[0].link : GPU_uniform(in[0].vec);
96 GPU_link(mat, "clamp_value", factor_link, GPU_constant(&min), GPU_constant(&max), &in[0].link);
97
98 int ret = GPU_stack_link(mat, node, name, in, out);
99
100 if (ret && node->custom2 & SHD_MIXRGB_CLAMP) {
101 const float min[3] = {0.0f, 0.0f, 0.0f};
102 const float max[3] = {1.0f, 1.0f, 1.0f};
103 GPU_link(mat, "clamp_color", out[0].link, GPU_constant(min), GPU_constant(max), &out[0].link);
104 }
105 return ret;
106}
107
108class MixRGBFunction : public mf::MultiFunction {
109 private:
110 bool clamp_;
111 int type_;
112
113 public:
114 MixRGBFunction(bool clamp, int type) : clamp_(clamp), type_(type)
115 {
116 static const mf::Signature signature = []() {
117 mf::Signature signature;
118 mf::SignatureBuilder builder{"MixRGB", signature};
119 builder.single_input<float>("Fac");
120 builder.single_input<ColorGeometry4f>("Color1");
121 builder.single_input<ColorGeometry4f>("Color2");
122 builder.single_output<ColorGeometry4f>("Color");
123 return signature;
124 }();
125 this->set_signature(&signature);
126 }
127
128 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
129 {
130 const VArray<float> &fac = params.readonly_single_input<float>(0, "Fac");
131 const VArray<ColorGeometry4f> &col1 = params.readonly_single_input<ColorGeometry4f>(1,
132 "Color1");
133 const VArray<ColorGeometry4f> &col2 = params.readonly_single_input<ColorGeometry4f>(2,
134 "Color2");
135 MutableSpan<ColorGeometry4f> results = params.uninitialized_single_output<ColorGeometry4f>(
136 3, "Color");
137
138 mask.foreach_index([&](const int64_t i) {
139 results[i] = col1[i];
140 ramp_blend(type_, results[i], clamp_f(fac[i], 0.0f, 1.0f), col2[i]);
141 });
142
143 if (clamp_) {
144 mask.foreach_index([&](const int64_t i) { clamp_v3(results[i], 0.0f, 1.0f); });
145 }
146 }
147};
148
150{
151 const bNode &node = builder.node();
152 bool clamp = node.custom2 & SHD_MIXRGB_CLAMP;
153 int mix_type = node.custom1;
155}
156
157} // namespace blender::nodes::node_shader_mix_rgb_cc
158
160{
161 namespace file_ns = blender::nodes::node_shader_mix_rgb_cc;
162
163 static blender::bke::bNodeType ntype;
164
165 common_node_type_base(&ntype, "ShaderNodeMixRGB", SH_NODE_MIX_RGB_LEGACY);
166 ntype.ui_name = "Mix (Legacy)";
167 ntype.ui_description = "Mix two input colors";
168 ntype.enum_name_legacy = "MIX_RGB";
170 ntype.declare = file_ns::sh_node_mix_rgb_declare;
172 ntype.gpu_fn = file_ns::gpu_shader_mix_rgb;
173 ntype.build_multi_function = file_ns::sh_node_mix_rgb_build_multi_function;
174 ntype.gather_link_search_ops = nullptr;
176}
General operations, lookup, etc. for materials.
void ramp_blend(int type, float r_col[3], float fac, const float col[3])
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:449
#define SH_NODE_MIX_RGB_LEGACY
MINLINE float clamp_f(float value, float min, float max)
MINLINE void clamp_v3(float vec[3], float min, float max)
@ MA_RAMP_LIGHT
@ MA_RAMP_COLOR
@ MA_RAMP_SAT
@ MA_RAMP_HUE
@ MA_RAMP_LINEAR
@ MA_RAMP_DIV
@ MA_RAMP_EXCLUSION
@ MA_RAMP_ADD
@ MA_RAMP_DODGE
@ MA_RAMP_SUB
@ MA_RAMP_SCREEN
@ MA_RAMP_SOFT
@ MA_RAMP_DARK
@ MA_RAMP_BURN
@ MA_RAMP_BLEND
@ MA_RAMP_VAL
@ MA_RAMP_OVERLAY
@ MA_RAMP_MULT
@ MA_RAMP_DIFF
@ SHD_MIXRGB_CLAMP
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeLink * GPU_uniform(const float *num)
@ PROP_FACTOR
Definition RNA_types.hh:251
long long int int64_t
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
#define in
#define out
constexpr T clamp(T, U, U) RET
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void register_node_type_sh_mix_rgb()
void common_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_blend_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:176
const char * name
return ret
#define min(a, b)
Definition sort.cc:36
int16_t custom1
int16_t custom2
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:270
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251