Blender V4.5
node_composite_val_to_rgb.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_assert.h"
10#include "BLI_math_vector.hh"
12
14
15#include "NOD_multi_function.hh"
16
18
19#include "BKE_colorband.hh"
20
21#include "GPU_material.hh"
22
24
25/* **************** VALTORGB ******************** */
26
28
30{
31 b.add_input<decl::Float>("Fac")
32 .default_value(0.5f)
33 .min(0.0f)
34 .max(1.0f)
36 .compositor_domain_priority(1);
37 b.add_output<decl::Color>("Image").compositor_domain_priority(0);
38 b.add_output<decl::Float>("Alpha");
39}
40
41static void node_composit_init_valtorgb(bNodeTree * /*ntree*/, bNode *node)
42{
43 node->storage = BKE_colorband_add(true);
44}
45
46using namespace blender::compositor;
47
48static ColorBand *get_color_band(const bNode &node)
49{
50 return static_cast<ColorBand *>(node.storage);
51}
52
53static int node_gpu_material(GPUMaterial *material,
54 bNode *node,
55 bNodeExecData * /*execdata*/,
58{
59 ColorBand *color_band = get_color_band(*node);
60
61 /* Common / easy case optimization. */
62 if ((color_band->tot <= 2) && (color_band->color_mode == COLBAND_BLEND_RGB)) {
63 float mul_bias[2];
64 switch (color_band->ipotype) {
66 mul_bias[0] = 1.0f / (color_band->data[1].pos - color_band->data[0].pos);
67 mul_bias[1] = -mul_bias[0] * color_band->data[0].pos;
68 return GPU_stack_link(material,
69 node,
70 "valtorgb_opti_linear",
71 inputs,
72 outputs,
73 GPU_uniform(mul_bias),
74 GPU_uniform(&color_band->data[0].r),
75 GPU_uniform(&color_band->data[1].r));
77 mul_bias[1] = max_ff(color_band->data[0].pos, color_band->data[1].pos);
78 return GPU_stack_link(material,
79 node,
80 "valtorgb_opti_constant",
81 inputs,
82 outputs,
83 GPU_uniform(&mul_bias[1]),
84 GPU_uniform(&color_band->data[0].r),
85 GPU_uniform(&color_band->data[1].r));
87 mul_bias[0] = 1.0f / (color_band->data[1].pos - color_band->data[0].pos);
88 mul_bias[1] = -mul_bias[0] * color_band->data[0].pos;
89 return GPU_stack_link(material,
90 node,
91 "valtorgb_opti_ease",
92 inputs,
93 outputs,
94 GPU_uniform(mul_bias),
95 GPU_uniform(&color_band->data[0].r),
96 GPU_uniform(&color_band->data[1].r));
99 /* Not optimized yet. Fall back to gradient texture. */
100 break;
101 }
102 }
103
104 float *array, layer;
105 int size;
107 GPUNodeLink *tex = GPU_color_band(material, size, array, &layer);
108
109 if (color_band->ipotype == COLBAND_INTERP_CONSTANT) {
110 return GPU_stack_link(
111 material, node, "valtorgb_nearest", inputs, outputs, tex, GPU_constant(&layer));
112 }
113
114 return GPU_stack_link(material, node, "valtorgb", inputs, outputs, tex, GPU_constant(&layer));
115}
116
118{
119 ColorBand *color_band = get_color_band(builder.node());
121 return mf::build::SI1_SO2<float, float4, float>(
122 "Color Ramp",
123 [=](const float factor, float4 &color, float &alpha) -> void {
124 BKE_colorband_evaluate(color_band, factor, color);
125 alpha = color.w;
126 },
127 mf::build::exec_presets::AllSpanOrSingle());
128 });
129}
130
131} // namespace blender::nodes::node_composite_color_ramp_cc
132
134{
136
137 static blender::bke::bNodeType ntype;
138
139 cmp_node_type_base(&ntype, "CompositorNodeValToRGB", CMP_NODE_VALTORGB);
140 ntype.ui_name = "Color Ramp";
141 ntype.ui_description = "Map values to colors with the use of a gradient";
142 ntype.enum_name_legacy = "VALTORGB";
144 ntype.declare = file_ns::cmp_node_valtorgb_declare;
145 blender::bke::node_type_size(ntype, 240, 200, 320);
146 ntype.initfunc = file_ns::node_composit_init_valtorgb;
149 ntype.gpu_fn = file_ns::node_gpu_material;
150 ntype.build_multi_function = file_ns::node_build_multi_function;
151
153}
155
156/* **************** RGBTOBW ******************** */
157
159
161{
162 b.add_input<decl::Color>("Image")
163 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
164 .compositor_domain_priority(0);
165 b.add_output<decl::Float>("Val");
166}
167
168using namespace blender::compositor;
169
170static int node_gpu_material(GPUMaterial *material,
171 bNode *node,
172 bNodeExecData * /*execdata*/,
175{
176 float luminance_coefficients[3];
178
179 return GPU_stack_link(
180 material, node, "color_to_luminance", inputs, outputs, GPU_constant(luminance_coefficients));
181}
182
184{
185 float3 luminance_coefficients;
187
189 return mf::build::SI1_SO<float4, float>(
190 "RGB to BW",
191 [=](const float4 &color) -> float {
192 return math::dot(color.xyz(), luminance_coefficients);
193 },
194 mf::build::exec_presets::AllSpanOrSingle());
195 });
196}
197
198} // namespace blender::nodes::node_composite_rgb_to_bw_cc
199
201{
203
204 static blender::bke::bNodeType ntype;
205
206 cmp_node_type_base(&ntype, "CompositorNodeRGBToBW", CMP_NODE_RGBTOBW);
207 ntype.ui_name = "RGB to BW";
208 ntype.ui_description = "Convert RGB input into grayscale using luminance";
209 ntype.enum_name_legacy = "RGBTOBW";
211 ntype.declare = file_ns::cmp_node_rgbtobw_declare;
213 ntype.gpu_fn = file_ns::node_gpu_material;
214 ntype.build_multi_function = file_ns::node_build_multi_function;
215
217}
void BKE_colorband_evaluate_table_rgba(const ColorBand *coba, float **array, int *size)
Definition colorband.cc:558
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:395
ColorBand * BKE_colorband_add(bool rangetype)
Definition colorband.cc:297
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:439
#define CMP_NODE_VALTORGB
#define CMP_NODE_RGBTOBW
MINLINE float max_ff(float a, float b)
@ COLBAND_BLEND_RGB
@ COLBAND_INTERP_LINEAR
@ COLBAND_INTERP_CONSTANT
@ COLBAND_INTERP_B_SPLINE
@ COLBAND_INTERP_EASE
@ COLBAND_INTERP_CARDINAL
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)
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:239
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5573
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:5603
void node_type_size_preset(bNodeType &ntype, eNodeSizePreset size)
Definition node.cc:5585
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static void cmp_node_valtorgb_declare(NodeDeclarationBuilder &b)
static ColorBand * get_color_band(const bNode &node)
static void node_composit_init_valtorgb(bNodeTree *, bNode *node)
static void cmp_node_rgbtobw_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
VecBase< float, 4 > float4
VecBase< float, 3 > float3
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
static void register_node_type_cmp_valtorgb()
static void register_node_type_cmp_rgbtobw()
static blender::bke::bNodeSocketTemplate outputs[]
static blender::bke::bNodeSocketTemplate inputs[]
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
CBData data[32]
void * storage
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:330
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355