Blender V5.3
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
7#include "COM_utilities.hh"
8
10
12
14{
15 b.use_custom_socket_order();
16 b.allow_any_socket_order();
17 b.add_input<decl::Float>("Value"_ustr)
18 .default_value(1.0f)
19 .min(0.0f)
20 .max(1.0f)
21 .structure_type(StructureType::Dynamic);
22 b.add_output<decl::Float>("Value"_ustr)
23 .structure_type(StructureType::Dynamic)
24 .align_with_previous();
25}
26
27using namespace blender::compositor;
28
30 private:
31 /* The normalize operation is specifically designed to normalize Z Depth information. But since Z
32 * Depth can contain near infinite values, normalization is limited to [-range_, range], meaning
33 * that values outside of that range will be ignored when computing the maximum and minimum for
34 * normalization and will eventually be 0 or 1 if they are less than or larger than the range
35 * respectively. */
36 constexpr static float range_ = 10000.0f;
37
38 public:
40
41 void execute() override
42 {
43 const Result &input_image = this->get_input("Value");
44 if (input_image.is_single_value()) {
45 Result &output_image = this->get_result("Value");
46 output_image.share_data(input_image);
47 return;
48 }
49
50 const float maximum = maximum_float_in_range(context(), input_image, -range_, range_);
51 const float minimum = minimum_float_in_range(context(), input_image, -range_, range_);
52 const float scale = (maximum != minimum) ? (1.0f / (maximum - minimum)) : 0.0f;
53
54 if (context().use_gpu()) {
55 this->execute_gpu(minimum, scale);
56 }
57 else {
58 this->execute_cpu(minimum, scale);
59 }
60 }
61
62 void execute_gpu(const float minimum, const float scale)
63 {
64 gpu::Shader *shader = this->context().get_shader("compositor_normalize");
66
67 GPU_shader_uniform_1f(shader, "minimum", minimum);
69
70 Result &input_image = this->get_input("Value");
71 input_image.bind_as_texture(shader, "input_tx");
72
73 const Domain domain = this->compute_domain();
74 Result &output_image = this->get_result("Value");
75 output_image.allocate_texture(domain);
76 output_image.bind_as_image(shader, "output_img");
77
79
81 output_image.unbind_as_image();
82 input_image.unbind_as_texture();
83 }
84
85 void execute_cpu(const float minimum, const float scale)
86 {
87 Result &image = this->get_input("Value");
88
89 const Domain domain = this->compute_domain();
90 Result &output = this->get_result("Value");
91 output.allocate_texture(domain);
92
93 parallel_for(domain.data_size, [&](const int2 texel) {
94 const float value = image.load_pixel<float>(texel);
95 const float normalized_value = (value - minimum) * scale;
96 const float clamped_value = math::clamp(normalized_value, 0.0f, 1.0f);
97 output.store_pixel(texel, clamped_value);
98 });
99 }
100};
101
103{
104 return new NormalizeOperation(context, node);
105}
106
107static void node_register()
108{
109 static bke::bNodeType ntype;
110
111 cmp_node_type_base(&ntype, "CompositorNodeNormalize"_ustr, CMP_NODE_NORMALIZE);
112 ntype.ui_name = "Normalize";
113 ntype.ui_description =
114 "Map values to 0 to 1 range, based on the minimum and maximum pixel values";
115 ntype.enum_name_legacy = "NORMALIZE";
117 ntype.declare = node_declare;
119
121}
123
124} // namespace blender::nodes::node_composite_normalize_cc
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define CMP_NODE_NORMALIZE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
void share_data(const Result &source)
Definition result.cc:996
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
float minimum_float_in_range(Context &context, const Result &input, const float lower_bound, const float upper_bound)
float maximum_float_in_range(Context &context, const Result &input, const float lower_bound, const float upper_bound)
void parallel_for(const int2 range, const Function &function)
static void node_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393