Blender V4.3
node_shader_color_ramp.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 "DNA_texture_types.h"
10
11#include "BKE_colorband.hh"
12
13#include "BLI_color.hh"
14
15#include "NOD_multi_function.hh"
16
17#include "node_shader_util.hh"
18#include "node_util.hh"
19
21
23{
24 b.is_function_node();
25 b.add_input<decl::Float>("Fac")
26 .default_value(0.5f)
27 .min(0.0f)
28 .max(1.0f)
30 .description(
31 "The value used to map onto the color gradient. 0.0 results in the leftmost color, "
32 "while 1.0 results in the rightmost");
33 b.add_output<decl::Color>("Color");
34 b.add_output<decl::Float>("Alpha");
35}
36
37static void node_shader_init_valtorgb(bNodeTree * /*ntree*/, bNode *node)
38{
39 node->storage = BKE_colorband_add(true);
40}
41
43 bNode *node,
44 bNodeExecData * /*execdata*/,
45 GPUNodeStack *in,
46 GPUNodeStack *out)
47{
48 ColorBand *coba = (ColorBand *)node->storage;
49 float *array, layer;
50 int size;
51
52 /* Common / easy case optimization. */
53 if ((coba->tot <= 2) && (coba->color_mode == COLBAND_BLEND_RGB)) {
54 float mul_bias[2];
55 switch (coba->ipotype) {
57 mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
58 mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
59 return GPU_stack_link(mat,
60 node,
61 "valtorgb_opti_linear",
62 in,
63 out,
64 GPU_uniform(mul_bias),
65 GPU_uniform(&coba->data[0].r),
66 GPU_uniform(&coba->data[1].r));
68 mul_bias[1] = max_ff(coba->data[0].pos, coba->data[1].pos);
69 return GPU_stack_link(mat,
70 node,
71 "valtorgb_opti_constant",
72 in,
73 out,
74 GPU_uniform(&mul_bias[1]),
75 GPU_uniform(&coba->data[0].r),
76 GPU_uniform(&coba->data[1].r));
78 mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
79 mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
80 return GPU_stack_link(mat,
81 node,
82 "valtorgb_opti_ease",
83 in,
84 out,
85 GPU_uniform(mul_bias),
86 GPU_uniform(&coba->data[0].r),
87 GPU_uniform(&coba->data[1].r));
88 default:
89 break;
90 }
91 }
92
94 GPUNodeLink *tex = GPU_color_band(mat, size, array, &layer);
95
96 if (coba->ipotype == COLBAND_INTERP_CONSTANT) {
97 return GPU_stack_link(mat, node, "valtorgb_nearest", in, out, tex, GPU_constant(&layer));
98 }
99
100 return GPU_stack_link(mat, node, "valtorgb", in, out, tex, GPU_constant(&layer));
101}
102
104 private:
105 const ColorBand &color_band_;
106
107 public:
108 ColorBandFunction(const ColorBand &color_band) : color_band_(color_band)
109 {
110 static const mf::Signature signature = []() {
112 mf::SignatureBuilder builder{"Color Band", signature};
113 builder.single_input<float>("Value");
114 builder.single_output<ColorGeometry4f>("Color");
115 builder.single_output<float>("Alpha");
116 return signature;
117 }();
118 this->set_signature(&signature);
119 }
120
121 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
122 {
123 const VArray<float> &values = params.readonly_single_input<float>(0, "Value");
124 MutableSpan<ColorGeometry4f> colors = params.uninitialized_single_output<ColorGeometry4f>(
125 1, "Color");
126 MutableSpan<float> alphas = params.uninitialized_single_output<float>(2, "Alpha");
127
128 mask.foreach_index([&](const int64_t i) {
129 ColorGeometry4f color;
130 BKE_colorband_evaluate(&color_band_, values[i], color);
131 colors[i] = color;
132 alphas[i] = color.a;
133 });
134 }
135};
136
138{
139 const bNode &bnode = builder.node();
140 const ColorBand *color_band = (const ColorBand *)bnode.storage;
142}
143
145#ifdef WITH_MATERIALX
146{
147 /* TODO: Implement */
148 NodeItem res = empty();
149 return res;
150}
151#endif
153
154} // namespace blender::nodes::node_shader_color_ramp_cc
155
157{
159
160 static blender::bke::bNodeType ntype;
161
163 ntype.declare = file_ns::sh_node_valtorgb_declare;
164 ntype.initfunc = file_ns::node_shader_init_valtorgb;
168 ntype.gpu_fn = file_ns::gpu_shader_valtorgb;
169 ntype.build_multi_function = file_ns::sh_node_valtorgb_build_multi_function;
170 ntype.materialx_fn = file_ns::node_shader_materialx;
171
173}
void BKE_colorband_evaluate_table_rgba(const ColorBand *coba, float **array, int *size)
Definition colorband.cc:559
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:396
ColorBand * BKE_colorband_add(bool rangetype)
Definition colorband.cc:298
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
#define SH_NODE_VALTORGB
Definition BKE_node.hh:894
MINLINE float max_ff(float a, float b)
@ COLBAND_BLEND_RGB
@ COLBAND_INTERP_LINEAR
@ COLBAND_INTERP_CONSTANT
@ COLBAND_INTERP_EASE
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUNodeLink * GPU_uniform(const float *num)
@ PROP_FACTOR
Definition RNA_types.hh:154
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
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_type_size_preset(bNodeType *ntype, eNodeSizePreset size)
Definition node.cc:4614
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void node_shader_init_valtorgb(bNodeTree *, bNode *node)
static void sh_node_valtorgb_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static void sh_node_valtorgb_declare(NodeDeclarationBuilder &b)
static int gpu_shader_valtorgb(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_valtorgb()
void sh_fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
__int64 int64_t
Definition stdint.h:89
CBData data[32]
void * storage
Defines a node type.
Definition BKE_node.hh:218
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:320
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:318
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
NodeDeclareFunction declare
Definition BKE_node.hh:347