Blender  V2.93
node_shader_valToRgb.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "IMB_colormanagement.h"
25 
26 #include "DNA_texture_types.h"
27 
28 #include "BLI_color.hh"
29 
30 #include "node_shader_util.h"
31 
32 /* **************** VALTORGB ******************** */
34  {SOCK_FLOAT, N_("Fac"), 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
35  {-1, ""},
36 };
38  {SOCK_RGBA, N_("Color")},
39  {SOCK_FLOAT, N_("Alpha")},
40  {-1, ""},
41 };
42 
44  int UNUSED(thread),
45  bNode *node,
46  bNodeExecData *UNUSED(execdata),
47  bNodeStack **in,
48  bNodeStack **out)
49 {
50  /* stack order in: fac */
51  /* stack order out: col, alpha */
52 
53  if (node->storage) {
54  float fac;
55  nodestack_get_vec(&fac, SOCK_FLOAT, in[0]);
56 
57  BKE_colorband_evaluate((ColorBand *)node->storage, fac, out[0]->vec);
58  out[1]->vec[0] = out[0]->vec[3];
59  }
60 }
61 
63 {
64  node->storage = BKE_colorband_add(true);
65 }
66 
68  bNode *node,
69  bNodeExecData *UNUSED(execdata),
70  GPUNodeStack *in,
71  GPUNodeStack *out)
72 {
73  struct ColorBand *coba = (ColorBand *)node->storage;
74  float *array, layer;
75  int size;
76 
77  /* Common / easy case optimization. */
78  if ((coba->tot <= 2) && (coba->color_mode == COLBAND_BLEND_RGB)) {
79  float mul_bias[2];
80  switch (coba->ipotype) {
82  mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
83  mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
84  return GPU_stack_link(mat,
85  node,
86  "valtorgb_opti_linear",
87  in,
88  out,
89  GPU_uniform(mul_bias),
90  GPU_uniform(&coba->data[0].r),
91  GPU_uniform(&coba->data[1].r));
93  mul_bias[1] = max_ff(coba->data[0].pos, coba->data[1].pos);
94  return GPU_stack_link(mat,
95  node,
96  "valtorgb_opti_constant",
97  in,
98  out,
99  GPU_uniform(&mul_bias[1]),
100  GPU_uniform(&coba->data[0].r),
101  GPU_uniform(&coba->data[1].r));
102  case COLBAND_INTERP_EASE:
103  mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
104  mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
105  return GPU_stack_link(mat,
106  node,
107  "valtorgb_opti_ease",
108  in,
109  out,
110  GPU_uniform(mul_bias),
111  GPU_uniform(&coba->data[0].r),
112  GPU_uniform(&coba->data[1].r));
113  default:
114  break;
115  }
116  }
117 
119  GPUNodeLink *tex = GPU_color_band(mat, size, array, &layer);
120 
121  if (coba->ipotype == COLBAND_INTERP_CONSTANT) {
122  return GPU_stack_link(mat, node, "valtorgb_nearest", in, out, tex, GPU_constant(&layer));
123  }
124 
125  return GPU_stack_link(mat, node, "valtorgb", in, out, tex, GPU_constant(&layer));
126 }
127 
129  private:
130  const ColorBand &color_band_;
131 
132  public:
133  ColorBandFunction(const ColorBand &color_band) : color_band_(color_band)
134  {
136  this->set_signature(&signature);
137  }
138 
140  {
142  signature.single_input<float>("Value");
143  signature.single_output<blender::Color4f>("Color");
144  signature.single_output<float>("Alpha");
145  return signature.build();
146  }
147 
150  blender::fn::MFContext UNUSED(context)) const override
151  {
152  const blender::VArray<float> &values = params.readonly_single_input<float>(0, "Value");
154  params.uninitialized_single_output<blender::Color4f>(1, "Color");
155  blender::MutableSpan<float> alphas = params.uninitialized_single_output<float>(2, "Alpha");
156 
157  for (int64_t i : mask) {
158  blender::Color4f color;
159  BKE_colorband_evaluate(&color_band_, values[i], color);
160  colors[i] = color;
161  alphas[i] = color.a;
162  }
163  }
164 };
165 
167 {
168  bNode &bnode = builder.bnode();
169  const ColorBand *color_band = (const ColorBand *)bnode.storage;
170  builder.construct_and_set_matching_fn<ColorBandFunction>(*color_band);
171 }
172 
174 {
175  static bNodeType ntype;
176 
182  node_type_exec(&ntype, nullptr, nullptr, node_shader_exec_valtorgb);
185 
186  nodeRegisterType(&ntype);
187 }
188 
189 /* **************** RGBTOBW ******************** */
191  {SOCK_RGBA, N_("Color"), 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f}, {-1, ""}};
193  {SOCK_FLOAT, N_("Val"), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, {-1, ""}};
194 
196  int UNUSED(thread),
197  bNode *UNUSED(node),
198  bNodeExecData *UNUSED(execdata),
199  bNodeStack **in,
200  bNodeStack **out)
201 {
202  /* stack order out: bw */
203  /* stack order in: col */
204  float col[3];
206 
208 }
209 
211  bNode *node,
212  bNodeExecData *UNUSED(execdata),
213  GPUNodeStack *in,
214  GPUNodeStack *out)
215 {
216  return GPU_stack_link(mat, node, "rgbtobw", in, out);
217 }
218 
220 {
221  static bNodeType ntype;
222 
223  sh_node_type_base(&ntype, SH_NODE_RGBTOBW, "RGB to BW", NODE_CLASS_CONVERTOR, 0);
225  node_type_exec(&ntype, nullptr, nullptr, node_shader_exec_rgbtobw);
227 
228  nodeRegisterType(&ntype);
229 }
struct ColorBand * BKE_colorband_add(bool rangetype)
Definition: colorband.c:312
bool BKE_colorband_evaluate(const struct ColorBand *coba, float in, float out[4])
void BKE_colorband_evaluate_table_rgba(const struct ColorBand *coba, float **array, int *size)
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4645
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
#define NODE_CLASS_CONVERTOR
Definition: BKE_node.h:341
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define SH_NODE_VALTORGB
Definition: BKE_node.h:975
void node_type_exec(struct bNodeType *ntype, NodeInitExecFunction init_exec_fn, NodeFreeExecFunction free_exec_fn, NodeExecFunction exec_fn)
Definition: node.cc:4635
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_LARGE
Definition: BKE_node.h:373
MINLINE float max_ff(float a, float b)
#define UNUSED(x)
#define N_(msgid)
@ SOCK_VECTOR
@ SOCK_FLOAT
@ SOCK_RGBA
@ COLBAND_INTERP_LINEAR
@ COLBAND_INTERP_CONSTANT
@ COLBAND_INTERP_EASE
@ COLBAND_BLEND_RGB
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
Group SH_NODE_RGBTOBW
@ PROP_FACTOR
Definition: RNA_types.h:131
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void call(blender::IndexMask mask, blender::fn::MFParams params, blender::fn::MFContext UNUSED(context)) const override
static blender::fn::MFSignature create_signature()
ColorBandFunction(const ColorBand &color_band)
void set_signature(const MFSignature *signature)
const MFSignature & signature() const
OperationNode * node
bNodeTree * ntree
uint col
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
void sh_fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
static bNodeSocketTemplate sh_node_rgbtobw_out[]
static int gpu_shader_rgbtobw(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static int gpu_shader_valtorgb(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static bNodeSocketTemplate sh_node_valtorgb_out[]
static void sh_node_valtorgb_expand_in_mf_network(blender::nodes::NodeMFNetworkBuilder &builder)
static void node_shader_init_valtorgb(bNodeTree *UNUSED(ntree), bNode *node)
static void node_shader_exec_rgbtobw(void *UNUSED(data), int UNUSED(thread), bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), bNodeStack **in, bNodeStack **out)
static void node_shader_exec_valtorgb(void *UNUSED(data), int UNUSED(thread), bNode *node, bNodeExecData *UNUSED(execdata), bNodeStack **in, bNodeStack **out)
void register_node_type_sh_rgbtobw(void)
void register_node_type_sh_valtorgb(void)
static bNodeSocketTemplate sh_node_rgbtobw_in[]
static bNodeSocketTemplate sh_node_valtorgb_in[]
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
struct SELECTID_Context context
Definition: select_engine.c:47
__int64 int64_t
Definition: stdint.h:92
CBData data[32]
Compact definition of a node socket.
Definition: BKE_node.h:95
float vec[4]
Defines a node type.
Definition: BKE_node.h:221
NodeExpandInMFNetworkFunction expand_in_mf_network
Definition: BKE_node.h:324
void * storage
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)