Blender V5.0
node_composite_id_mask.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
9#include <cmath>
10
11#include "BLI_math_base.hh"
13
14#include "UI_resources.hh"
15
16#include "GPU_shader.hh"
17
18#include "COM_algorithm_smaa.hh"
19#include "COM_node_operation.hh"
20#include "COM_utilities.hh"
21
23
24/* **************** ID Mask ******************** */
25
27
29{
30 b.add_input<decl::Float>("ID value")
31 .default_value(1.0f)
32 .min(0.0f)
33 .max(1.0f)
34 .structure_type(StructureType::Dynamic);
35 b.add_input<decl::Int>("Index").default_value(0).min(0);
36 b.add_input<decl::Bool>("Anti-Alias").default_value(false);
37
38 b.add_output<decl::Float>("Alpha").structure_type(StructureType::Dynamic);
39}
40
41using namespace blender::compositor;
42
44 public:
46
47 void execute() override
48 {
49 const Result &input_mask = get_input("ID value");
50 if (input_mask.is_single_value()) {
52 return;
53 }
54
55 /* If anti-aliasing is disabled, write to the output directly, otherwise, write to a temporary
56 * result to later perform anti-aliasing. */
57 Result non_anti_aliased_mask = context().create_result(ResultType::Float);
58 Result &output_mask = use_anti_aliasing() ? non_anti_aliased_mask : get_result("Alpha");
59
60 if (this->context().use_gpu()) {
61 this->execute_gpu(output_mask);
62 }
63 else {
64 this->execute_cpu(output_mask);
65 }
66
67 if (this->use_anti_aliasing()) {
68 smaa(context(), non_anti_aliased_mask, get_result("Alpha"));
69 non_anti_aliased_mask.release();
70 }
71 }
72
73 void execute_gpu(Result &output_mask)
74 {
75 gpu::Shader *shader = context().get_shader("compositor_id_mask");
76 GPU_shader_bind(shader);
77
78 GPU_shader_uniform_1i(shader, "index", get_index());
79
80 const Result &input_mask = get_input("ID value");
81 input_mask.bind_as_texture(shader, "input_mask_tx");
82
83 const Domain domain = compute_domain();
84 output_mask.allocate_texture(domain);
85 output_mask.bind_as_image(shader, "output_mask_img");
86
88
89 input_mask.unbind_as_texture();
90 output_mask.unbind_as_image();
92 }
93
94 void execute_cpu(Result &output_mask)
95 {
96 const int index = this->get_index();
97
98 const Result &input_mask = get_input("ID value");
99
100 const Domain domain = compute_domain();
101 output_mask.allocate_texture(domain);
102
103 parallel_for(domain.size, [&](const int2 texel) {
104 float input_mask_value = input_mask.load_pixel<float>(texel);
105 float mask = int(math::round(input_mask_value)) == index ? 1.0f : 0.0f;
106 output_mask.store_pixel(texel, mask);
107 });
108 }
109
111 {
112 const float input_mask_value = get_input("ID value").get_single_value<float>();
113 const float mask = int(round(input_mask_value)) == get_index() ? 1.0f : 0.0f;
116 }
117
119 {
120 return math::max(0, this->get_input("Index").get_single_value_default(0));
121 }
122
124 {
125 return this->get_input("Anti-Alias").get_single_value_default(false);
126 }
127};
128
130{
131 return new IDMaskOperation(context, node);
132}
133
134} // namespace blender::nodes::node_composite_id_mask_cc
135
137{
139
140 static blender::bke::bNodeType ntype;
141
142 cmp_node_type_base(&ntype, "CompositorNodeIDMask", CMP_NODE_ID_MASK);
143 ntype.ui_name = "ID Mask";
144 ntype.ui_description = "Create a matte from an object or material index pass";
145 ntype.enum_name_legacy = "ID_MASK";
147 ntype.declare = file_ns::cmp_node_idmask_declare;
148 ntype.get_compositor_operation = file_ns::get_compositor_operation;
149
151}
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define CMP_NODE_ID_MASK
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(blender::gpu::Shader *sh, const char *name, int value)
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
Result create_result(ResultType type, ResultPrecision precision)
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
T get_single_value_default(const T &default_value) const
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 set_single_value(const T &value)
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
const T & get_single_value() const
#define round
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
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
void smaa(Context &context, const Result &input, Result &output, const float threshold=0.1f, const float local_contrast_adaptation_factor=2.0f, const int corner_rounding=25)
Definition smaa.cc:1646
void parallel_for(const int2 range, const Function &function)
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
static void cmp_node_idmask_declare(NodeDeclarationBuilder &b)
VecBase< int32_t, 2 > int2
static void register_node_type_cmp_idmask()
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