Blender V5.0
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.use_custom_socket_order();
22 b.allow_any_socket_order();
23 b.add_input<decl::Float>("Value").default_value(1.0f).min(0.0f).max(1.0f).structure_type(
24 StructureType::Dynamic);
25 b.add_output<decl::Float>("Value").structure_type(StructureType::Dynamic).align_with_previous();
26}
27
28using namespace blender::compositor;
29
31 private:
32 /* The normalize operation is specifically designed to normalize Z Depth information. But since Z
33 * Depth can contain near infinite values, normalization is limited to [-range_, range], meaning
34 * that values outside of that range will be ignored when computing the maximum and minimum for
35 * normalization and will eventually be 0 or 1 if they are less than or larger than the range
36 * respectively. */
37 constexpr static float range_ = 10000.0f;
38
39 public:
41
42 void execute() override
43 {
44 const Result &input_image = this->get_input("Value");
45 if (input_image.is_single_value()) {
46 Result &output_image = this->get_result("Value");
47 output_image.share_data(input_image);
48 return;
49 }
50
51 const float maximum = maximum_float_in_range(context(), input_image, -range_, range_);
52 const float minimum = minimum_float_in_range(context(), input_image, -range_, range_);
53 const float scale = (maximum != minimum) ? (1.0f / (maximum - minimum)) : 0.0f;
54
55 if (context().use_gpu()) {
56 this->execute_gpu(minimum, scale);
57 }
58 else {
59 this->execute_cpu(minimum, scale);
60 }
61 }
62
63 void execute_gpu(const float minimum, const float scale)
64 {
65 gpu::Shader *shader = this->context().get_shader("compositor_normalize");
66 GPU_shader_bind(shader);
67
68 GPU_shader_uniform_1f(shader, "minimum", minimum);
69 GPU_shader_uniform_1f(shader, "scale", scale);
70
71 Result &input_image = this->get_input("Value");
72 input_image.bind_as_texture(shader, "input_tx");
73
74 const Domain domain = this->compute_domain();
75 Result &output_image = this->get_result("Value");
76 output_image.allocate_texture(domain);
77 output_image.bind_as_image(shader, "output_img");
78
80
82 output_image.unbind_as_image();
83 input_image.unbind_as_texture();
84 }
85
86 void execute_cpu(const float minimum, const float scale)
87 {
88 Result &image = this->get_input("Value");
89
90 const Domain domain = this->compute_domain();
91 Result &output = this->get_result("Value");
92 output.allocate_texture(domain);
93
94 parallel_for(domain.size, [&](const int2 texel) {
95 const float value = image.load_pixel<float>(texel);
96 const float normalized_value = (value - minimum) * scale;
97 const float clamped_value = math::clamp(normalized_value, 0.0f, 1.0f);
98 output.store_pixel(texel, clamped_value);
99 });
100 }
101};
102
104{
105 return new NormalizeOperation(context, node);
106}
107
108} // namespace blender::nodes::node_composite_normalize_cc
109
111{
113
114 static blender::bke::bNodeType ntype;
115
116 cmp_node_type_base(&ntype, "CompositorNodeNormalize", CMP_NODE_NORMALIZE);
117 ntype.ui_name = "Normalize";
118 ntype.ui_description =
119 "Map values to 0 to 1 range, based on the minimum and maximum pixel values";
120 ntype.enum_name_legacy = "NORMALIZE";
122 ntype.declare = file_ns::cmp_node_normalize_declare;
123 ntype.get_compositor_operation = file_ns::get_compositor_operation;
124
126}
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:450
#define CMP_NODE_NORMALIZE
void GPU_shader_uniform_1f(blender::gpu::Shader *sh, const char *name, float value)
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
gpu::Shader * 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:523
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:389
void unbind_as_texture() const
Definition result.cc:511
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:487
void unbind_as_image() const
Definition result.cc:517
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:498
bool is_single_value() const
Definition result.cc:758
#define output
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:196
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 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:238
std::string ui_description
Definition BKE_node.hh:244
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:348
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362