Blender V5.3
node_composite_filter.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
6#include "BLI_math_vector.hh"
8
9#include "BKE_node_runtime.hh"
10
11#include "RNA_types.hh"
12
13#include "COM_node_operation.hh"
14#include "COM_utilities.hh"
15
17
19
20static const EnumPropertyItem type_items[] = {
21 {CMP_NODE_FILTER_SOFT, "SOFTEN", 0, N_("Soften"), ""},
23 "SHARPEN",
24 0,
25 N_("Box Sharpen"),
26 N_("An aggressive sharpening filter")},
28 "SHARPEN_DIAMOND",
29 0,
30 N_("Diamond Sharpen"),
31 N_("A moderate sharpening filter")},
32 {CMP_NODE_FILTER_LAPLACE, "LAPLACE", 0, N_("Laplace"), ""},
33 {CMP_NODE_FILTER_SOBEL, "SOBEL", 0, N_("Sobel"), ""},
34 {CMP_NODE_FILTER_PREWITT, "PREWITT", 0, N_("Prewitt"), ""},
35 {CMP_NODE_FILTER_KIRSCH, "KIRSCH", 0, N_("Kirsch"), ""},
36 {CMP_NODE_FILTER_SHADOW, "SHADOW", 0, N_("Shadow"), ""},
37 {0, nullptr, 0, nullptr, nullptr},
38};
39
41{
42 b.use_custom_socket_order();
43 b.allow_any_socket_order();
44 b.add_input<decl::Color>("Image"_ustr)
45 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
46 .hide_value()
47 .structure_type(StructureType::Dynamic);
48 b.add_output<decl::Color>("Image"_ustr)
49 .structure_type(StructureType::Dynamic)
50 .align_with_previous();
51
52 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
53 .default_value(1.0f)
54 .min(0.0f)
55 .max(1.0f)
57 .structure_type(StructureType::Dynamic);
58 b.add_input<decl::Menu>("Type"_ustr)
59 .default_value(CMP_NODE_FILTER_SOFT)
60 .static_items(type_items)
62}
63
65 public:
68 {
69 bNode &node = params.add_node("CompositorNodeFilter"_ustr);
70 bNodeSocket &type_socket = *bke::node_find_socket(node, SOCK_IN, "Type"_ustr);
71 type_socket.default_value_typed<bNodeSocketValueMenu>()->value = this->filter_type;
72 params.update_and_connect_available_socket(node, "Image"_ustr);
73 }
74};
75
77{
78 const eNodeSocketDatatype from_socket_type = params.other_socket().type;
79 if (!params.node_tree().typeinfo->validate_link(from_socket_type, SOCK_RGBA)) {
80 return;
81 }
82
91}
92
93using namespace blender::compositor;
94
96 public:
98
99 void execute() override
100 {
101 const Result &input_image = this->get_input("Image");
102 if (input_image.is_single_value()) {
103 Result &output_image = this->get_result("Image");
104 output_image.share_data(input_image);
105 return;
106 }
107
108 if (this->context().use_gpu()) {
109 this->execute_gpu();
110 }
111 else {
112 this->execute_cpu();
113 }
114 }
115
117 {
120
122
123 const Result &input_image = get_input("Image");
124 input_image.bind_as_texture(shader, "input_tx");
125
126 const Result &factor = get_input("Fac");
127 gpu::Texture *factor_texture = factor.bind_as_texture_or_single_value(shader, "factor_tx");
128
129 const Domain domain = compute_domain();
130
131 Result &output_image = get_result("Image");
132 output_image.allocate_texture(domain);
133 output_image.bind_as_image(shader, "output_img");
134
136
137 input_image.unbind_as_texture();
138 factor.unbind_as_texture_or_single_value(factor_texture);
139 output_image.unbind_as_image();
141 }
142
143 const char *get_shader_name()
144 {
145 if (this->is_edge_filter()) {
146 return "compositor_edge_filter";
147 }
148 return "compositor_filter";
149 }
150
152 {
153 const float3x3 kernel = this->get_filter_kernel();
154
155 const Result &input = get_input("Image");
156 const Result &factor = get_input("Fac");
157
158 const Domain domain = compute_domain();
159 Result &output = get_result("Image");
160 output.allocate_texture(domain);
161
162 if (this->is_edge_filter()) {
163 parallel_for(domain.data_size, [&](const int2 texel) {
164 /* Compute the dot product between the 3x3 window around the pixel and the edge detection
165 * kernel in the X direction and Y direction. The Y direction kernel is computed by
166 * transposing the given X direction kernel. */
167 float3 color_x = float3(0.0f);
168 float3 color_y = float3(0.0f);
169 for (int j = 0; j < 3; j++) {
170 for (int i = 0; i < 3; i++) {
171 float3 color =
172 float4(input.load_pixel_extended<Color>(texel + int2(i - 1, j - 1))).xyz();
173 color_x += color * kernel[j][i];
174 color_y += color * kernel[i][j];
175 }
176 }
177
178 /* Compute the channel-wise magnitude of the 2D vector composed from the X and Y edge
179 * detection filter results. */
180 float3 magnitude = math::sqrt(color_x * color_x + color_y * color_y);
181
182 /* Mix the channel-wise magnitude with the original color at the center of the kernel using
183 * the input factor. */
184 float4 color = float4(input.load_pixel<Color>(texel));
185 magnitude = math::interpolate(
186 color.xyz(), magnitude, factor.load_pixel<float, true>(texel));
187
188 /* Store the channel-wise magnitude with the original alpha of the input. */
189 output.store_pixel(texel, Color(float4(magnitude, color.w)));
190 });
191 }
192 else {
193 parallel_for(domain.data_size, [&](const int2 texel) {
194 /* Compute the dot product between the 3x3 window around the pixel and the kernel. */
195 float4 color = float4(0.0f);
196 for (int j = 0; j < 3; j++) {
197 for (int i = 0; i < 3; i++) {
198 color += float4(input.load_pixel_extended<Color>(texel + int2(i - 1, j - 1))) *
199 kernel[j][i];
200 }
201 }
202
203 /* Mix with the original color at the center of the kernel using the input factor. */
205 float4(input.load_pixel<Color>(texel)), color, factor.load_pixel<float, true>(texel));
206
207 /* Store the color making sure it is not negative. */
208 output.store_pixel(texel, Color(math::max(color, float4(0.0f))));
209 });
210 }
211 }
212
214 {
215 switch (this->get_type()) {
220 return true;
225 return false;
226 }
227 return false;
228 }
229
231 {
232 /* Initialize the kernels as arrays of rows with the top row first. Edge detection kernels
233 * return the kernel in the X direction, while the kernel in the Y direction will be computed
234 * inside the shader by transposing the kernel in the X direction. */
235 switch (this->get_type()) {
237 const float kernel[3][3] = {{1.0f / 16.0f, 2.0f / 16.0f, 1.0f / 16.0f},
238 {2.0f / 16.0f, 4.0f / 16.0f, 2.0f / 16.0f},
239 {1.0f / 16.0f, 2.0f / 16.0f, 1.0f / 16.0f}};
240 return float3x3(kernel);
241 }
243 const float kernel[3][3] = {
244 {-1.0f, -1.0f, -1.0f}, {-1.0f, 9.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}};
245 return float3x3(kernel);
246 }
248 const float kernel[3][3] = {{-1.0f / 8.0f, -1.0f / 8.0f, -1.0f / 8.0f},
249 {-1.0f / 8.0f, 1.0f, -1.0f / 8.0f},
250 {-1.0f / 8.0f, -1.0f / 8.0f, -1.0f / 8.0f}};
251 return float3x3(kernel);
252 }
254 const float kernel[3][3] = {{1.0f, 0.0f, -1.0f}, {2.0f, 0.0f, -2.0f}, {1.0f, 0.0f, -1.0f}};
255 return float3x3(kernel);
256 }
258 const float kernel[3][3] = {{1.0f, 0.0f, -1.0f}, {1.0f, 0.0f, -1.0f}, {1.0f, 0.0f, -1.0f}};
259 return float3x3(kernel);
260 }
262 const float kernel[3][3] = {
263 {5.0f, -3.0f, -2.0f}, {5.0f, -3.0f, -2.0f}, {5.0f, -3.0f, -2.0f}};
264 return float3x3(kernel);
265 }
267 const float kernel[3][3] = {{1.0f, 2.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, {-1.0f, -2.0f, -1.0f}};
268 return float3x3(kernel);
269 }
271 const float kernel[3][3] = {
272 {0.0f, -1.0f, 0.0f}, {-1.0f, 5.0f, -1.0f}, {0.0f, -1.0f, 0.0f}};
273 return float3x3(kernel);
274 }
275 }
276
277 const float kernel[3][3] = {{0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
278 return float3x3(kernel);
279 }
280
282 {
283 return CMPNodeFilterMethod(
284 this->get_input("Type").get_single_value_default<MenuValue>().value);
285 }
286};
287
289{
290 return new FilterOperation(context, node);
291}
292
293static void node_register()
294{
295 static bke::bNodeType ntype;
296
297 cmp_node_type_base(&ntype, "CompositorNodeFilter"_ustr, CMP_NODE_FILTER);
298 ntype.ui_name = "Filter";
299 ntype.ui_description = "Apply common image enhancement filters";
300 ntype.enum_name_legacy = "FILTER";
302 ntype.declare = node_declare;
303 ntype.flag |= NODE_PREVIEW;
306
308}
309NOD_REGISTER_NODE(node_register)
310
311} // namespace blender::nodes::node_composite_filter_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_FILTER
#define IFACE_(msgid)
#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 unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
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
T load_pixel(const int2 &texel) const
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
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:861
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void parallel_for(const int2 range, const Function &function)
T sqrt(const T &a)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
static void gather_link_searches(GatherLinkSearchOpParams &params)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
static const EnumPropertyItem type_items[]
@ CMP_NODE_FILTER_SOBEL
@ CMP_NODE_FILTER_SOFT
@ CMP_NODE_FILTER_SHARP_DIAMOND
@ CMP_NODE_FILTER_SHARP_BOX
@ CMP_NODE_FILTER_KIRSCH
@ CMP_NODE_FILTER_SHADOW
@ CMP_NODE_FILTER_PREWITT
@ CMP_NODE_FILTER_LAPLACE
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
@ PROP_FACTOR
Definition RNA_types.hh:308
void GPU_shader_uniform_mat3_as_mat4(gpu::Shader *sh, const char *name, const float data[3][3])
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)