Blender V4.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
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::realtime_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 Result &input_image = get_input("Value");
46 Result &output_image = get_result("Value");
47 if (input_image.is_single_value()) {
48 input_image.pass_through(output_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 GPUShader *shader = context().get_shader("compositor_normalize");
57 GPU_shader_bind(shader);
58
59 GPU_shader_uniform_1f(shader, "minimum", minimum);
60 GPU_shader_uniform_1f(shader, "scale", scale);
61
62 input_image.bind_as_texture(shader, "input_tx");
63
64 const Domain domain = compute_domain();
65 output_image.allocate_texture(domain);
66 output_image.bind_as_image(shader, "output_img");
67
68 compute_dispatch_threads_at_least(shader, domain.size);
69
71 output_image.unbind_as_image();
72 input_image.unbind_as_texture();
73 }
74};
75
77{
78 return new NormalizeOperation(context, node);
79}
80
81} // namespace blender::nodes::node_composite_normalize_cc
82
84{
86
87 static blender::bke::bNodeType ntype;
88
89 cmp_node_type_base(&ntype, CMP_NODE_NORMALIZE, "Normalize", NODE_CLASS_OP_VECTOR);
90 ntype.declare = file_ns::cmp_node_normalize_declare;
91 ntype.get_compositor_operation = file_ns::get_compositor_operation;
92
94}
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:407
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value)
void GPU_shader_bind(GPUShader *shader)
void GPU_shader_unbind()
struct GPUShader GPUShader
GPUShader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, DNode node)
Result & get_input(StringRef identifier) const
Definition operation.cc:144
Result & get_result(StringRef identifier)
Definition operation.cc:46
void bind_as_image(GPUShader *shader, const char *image_name, bool read=false) const
Definition result.cc:264
void pass_through(Result &target)
Definition result.cc:289
void allocate_texture(Domain domain, bool from_pool=true)
Definition result.cc:204
void bind_as_texture(GPUShader *shader, const char *texture_name) const
Definition result.cc:253
local_group_size(16, 16) .push_constant(Type b
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void cmp_node_normalize_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
float maximum_float_in_range(Context &context, GPUTexture *texture, float lower_bound, float upper_bound)
float minimum_float_in_range(Context &context, GPUTexture *texture, float lower_bound, float upper_bound)
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:131
void register_node_type_cmp_normalize()
void cmp_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
Defines a node type.
Definition BKE_node.hh:218
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:324
NodeDeclareFunction declare
Definition BKE_node.hh:347