Blender V4.5
node_composite_normalize.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
10#include "COM_node_operation.hh"
11#include "COM_utilities.hh"
12
14
15/* **************** NORMALIZE single channel, useful for Z buffer ******************** */
16
18
20{
21 b.add_input<decl::Float>("Value")
22 .default_value(1.0f)
23 .min(0.0f)
24 .max(1.0f)
26 b.add_output<decl::Float>("Value");
27}
28
29using namespace blender::compositor;
30
32 private:
33 /* The normalize operation is specifically designed to normalize Z Depth information. But since Z
34 * Depth can contain near infinite values, normalization is limited to [-range_, range], meaning
35 * that values outside of that range will be ignored when computing the maximum and minimum for
36 * normalization and will eventually be 0 or 1 if they are less than or larger than the range
37 * respectively. */
38 constexpr static float range_ = 10000.0f;
39
40 public:
42
43 void execute() override
44 {
45 const Result &input_image = this->get_input("Value");
46 if (input_image.is_single_value()) {
47 Result &output_image = this->get_result("Value");
48 output_image.share_data(input_image);
49 return;
50 }
51
52 const float maximum = maximum_float_in_range(context(), input_image, -range_, range_);
53 const float minimum = minimum_float_in_range(context(), input_image, -range_, range_);
54 const float scale = (maximum != minimum) ? (1.0f / (maximum - minimum)) : 0.0f;
55
56 if (context().use_gpu()) {
57 this->execute_gpu(minimum, scale);
58 }
59 else {
60 this->execute_cpu(minimum, scale);
61 }
62 }
63
64 void execute_gpu(const float minimum, const float scale)
65 {
66 GPUShader *shader = this->context().get_shader("compositor_normalize");
67 GPU_shader_bind(shader);
68
69 GPU_shader_uniform_1f(shader, "minimum", minimum);
70 GPU_shader_uniform_1f(shader, "scale", scale);
71
72 Result &input_image = this->get_input("Value");
73 input_image.bind_as_texture(shader, "input_tx");
74
75 const Domain domain = this->compute_domain();
76 Result &output_image = this->get_result("Value");
77 output_image.allocate_texture(domain);
78 output_image.bind_as_image(shader, "output_img");
79
81
83 output_image.unbind_as_image();
84 input_image.unbind_as_texture();
85 }
86
87 void execute_cpu(const float minimum, const float scale)
88 {
89 Result &image = this->get_input("Value");
90
91 const Domain domain = this->compute_domain();
92 Result &output = this->get_result("Value");
93 output.allocate_texture(domain);
94
95 parallel_for(domain.size, [&](const int2 texel) {
96 const float value = image.load_pixel<float>(texel);
97 const float normalized_value = (value - minimum) * scale;
98 const float clamped_value = math::clamp(normalized_value, 0.0f, 1.0f);
99 output.store_pixel(texel, clamped_value);
100 });
101 }
102};
103
105{
106 return new NormalizeOperation(context, node);
107}
108
109} // namespace blender::nodes::node_composite_normalize_cc
110
112{
114
115 static blender::bke::bNodeType ntype;
116
117 cmp_node_type_base(&ntype, "CompositorNodeNormalize", CMP_NODE_NORMALIZE);
118 ntype.ui_name = "Normalize";
119 ntype.ui_description =
120 "Map values to 0 to 1 range, based on the minimum and maximum pixel values";
121 ntype.enum_name_legacy = "NORMALIZE";
123 ntype.declare = file_ns::cmp_node_normalize_declare;
124 ntype.get_compositor_operation = file_ns::get_compositor_operation;
125
127}
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:436
#define CMP_NODE_NORMALIZE
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value)
void GPU_shader_bind(GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
GPUShader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, DNode node)
Result & get_result(StringRef identifier)
Definition operation.cc:39
Result & get_input(StringRef identifier) const
Definition operation.cc:138
virtual Domain compute_domain()
Definition operation.cc:56
void share_data(const Result &source)
Definition result.cc:401
void allocate_texture(Domain domain, bool from_pool=true)
Definition result.cc:309
void unbind_as_texture() const
Definition result.cc:389
void bind_as_texture(GPUShader *shader, const char *texture_name) const
Definition result.cc:365
void bind_as_image(GPUShader *shader, const char *image_name, bool read=false) const
Definition result.cc:376
void unbind_as_image() const
Definition result.cc:395
bool is_single_value() const
Definition result.cc:625
#define output
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
float minimum_float_in_range(Context &context, const Result &result, const float lower_bound, const float upper_bound)
float maximum_float_in_range(Context &context, const Result &result, const float lower_bound, const float upper_bound)
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:170
void parallel_for(const int2 range, const Function &function)
static void cmp_node_normalize_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
VecBase< int32_t, 2 > int2
static void register_node_type_cmp_normalize()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:336
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355