Blender V4.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
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "BKE_material.h"
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>("Fac").default_value(0.5f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
26 b.add_input<decl::Color>("Color1").default_value({0.5f, 0.5f, 0.5f, 1.0f});
27 b.add_input<decl::Color>("Color2").default_value({0.5f, 0.5f, 0.5f, 1.0f});
28 b.add_output<decl::Color>("Color");
29}
30
31static const char *gpu_shader_get_name(int mode)
32{
33 switch (mode) {
34 case MA_RAMP_BLEND:
35 return "mix_blend";
36 case MA_RAMP_ADD:
37 return "mix_add";
38 case MA_RAMP_MULT:
39 return "mix_mult";
40 case MA_RAMP_SUB:
41 return "mix_sub";
42 case MA_RAMP_SCREEN:
43 return "mix_screen";
44 case MA_RAMP_DIV:
45 return "mix_div_fallback";
46 case MA_RAMP_DIFF:
47 return "mix_diff";
49 return "mix_exclusion";
50 case MA_RAMP_DARK:
51 return "mix_dark";
52 case MA_RAMP_LIGHT:
53 return "mix_light";
54 case MA_RAMP_OVERLAY:
55 return "mix_overlay";
56 case MA_RAMP_DODGE:
57 return "mix_dodge";
58 case MA_RAMP_BURN:
59 return "mix_burn";
60 case MA_RAMP_HUE:
61 return "mix_hue";
62 case MA_RAMP_SAT:
63 return "mix_sat";
64 case MA_RAMP_VAL:
65 return "mix_val";
66 case MA_RAMP_COLOR:
67 return "mix_color";
68 case MA_RAMP_SOFT:
69 return "mix_soft";
70 case MA_RAMP_LINEAR:
71 return "mix_linear";
72 }
73
74 return nullptr;
75}
76
78 bNode *node,
79 bNodeExecData * /*execdata*/,
80 GPUNodeStack *in,
81 GPUNodeStack *out)
82{
83 const char *name = gpu_shader_get_name(node->custom1);
84
85 if (name == nullptr) {
86 return 0;
87 }
88
89 const float min = 0.0f;
90 const float max = 1.0f;
91 const GPUNodeLink *factor_link = in[0].link ? in[0].link : GPU_uniform(in[0].vec);
92 GPU_link(mat, "clamp_value", factor_link, GPU_constant(&min), GPU_constant(&max), &in[0].link);
93
94 int ret = GPU_stack_link(mat, node, name, in, out);
95
96 if (ret && node->custom2 & SHD_MIXRGB_CLAMP) {
97 const float min[3] = {0.0f, 0.0f, 0.0f};
98 const float max[3] = {1.0f, 1.0f, 1.0f};
99 GPU_link(mat, "clamp_color", out[0].link, GPU_constant(min), GPU_constant(max), &out[0].link);
100 }
101 return ret;
102}
103
105 private:
106 bool clamp_;
107 int type_;
108
109 public:
110 MixRGBFunction(bool clamp, int type) : clamp_(clamp), type_(type)
111 {
112 static const mf::Signature signature = []() {
114 mf::SignatureBuilder builder{"MixRGB", signature};
115 builder.single_input<float>("Fac");
116 builder.single_input<ColorGeometry4f>("Color1");
117 builder.single_input<ColorGeometry4f>("Color2");
118 builder.single_output<ColorGeometry4f>("Color");
119 return signature;
120 }();
121 this->set_signature(&signature);
122 }
123
124 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
125 {
126 const VArray<float> &fac = params.readonly_single_input<float>(0, "Fac");
127 const VArray<ColorGeometry4f> &col1 = params.readonly_single_input<ColorGeometry4f>(1,
128 "Color1");
129 const VArray<ColorGeometry4f> &col2 = params.readonly_single_input<ColorGeometry4f>(2,
130 "Color2");
131 MutableSpan<ColorGeometry4f> results = params.uninitialized_single_output<ColorGeometry4f>(
132 3, "Color");
133
134 mask.foreach_index([&](const int64_t i) {
135 results[i] = col1[i];
136 ramp_blend(type_, results[i], clamp_f(fac[i], 0.0f, 1.0f), col2[i]);
137 });
138
139 if (clamp_) {
140 mask.foreach_index([&](const int64_t i) { clamp_v3(results[i], 0.0f, 1.0f); });
141 }
142 }
143};
144
146{
147 const bNode &node = builder.node();
148 bool clamp = node.custom2 & SHD_MIXRGB_CLAMP;
149 int mix_type = node.custom1;
151}
152
153} // namespace blender::nodes::node_shader_mix_rgb_cc
154
156{
157 namespace file_ns = blender::nodes::node_shader_mix_rgb_cc;
158
159 static blender::bke::bNodeType ntype;
160
162 ntype.declare = file_ns::sh_node_mix_rgb_declare;
164 ntype.gpu_fn = file_ns::gpu_shader_mix_rgb;
165 ntype.build_multi_function = file_ns::sh_node_mix_rgb_build_multi_function;
166 ntype.gather_link_search_ops = nullptr;
168}
General operations, lookup, etc. for materials.
void ramp_blend(int type, float r_col[3], float fac, const float col[3])
#define SH_NODE_MIX_RGB_LEGACY
Definition BKE_node.hh:893
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:406
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:154
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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)
void register_node_type_sh_mix_rgb()
void sh_fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_blend_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:180
return ret
#define min(a, b)
Definition sort.c:32
__int64 int64_t
Definition stdint.h:89
int16_t custom2
Defines a node type.
Definition BKE_node.hh:218
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:249
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:318
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:363
NodeDeclareFunction declare
Definition BKE_node.hh:347
ccl_device_inline int clamp(int a, int mn, int mx)
Definition util/math.h:379