Blender V5.3
node_composite_denoise.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#ifndef __APPLE__
6# include "BLI_system.hh"
7#endif
8
9#include "MEM_guardedalloc.h"
10
12#include "UI_resources.hh"
13
14#include "DNA_node_types.h"
15
18#include "COM_node_operation.hh"
19#include "COM_utilities.hh"
20#include "COM_utilities_oidn.hh"
21
23
24#ifdef WITH_OPENIMAGEDENOISE
25# include <OpenImageDenoise/oidn.hpp>
26#endif
27
29
32 "NONE",
33 0,
34 N_("None"),
35 N_("No prefiltering, use when guiding passes are noise-free")},
37 "FAST",
38 0,
39 N_("Fast"),
40 N_("Denoise image and guiding passes together. Improves quality when guiding passes are "
41 "noisy using least amount of extra processing time.")},
43 "ACCURATE",
44 0,
45 N_("Accurate"),
46 N_("Prefilter noisy guiding passes before denoising image. Improves quality when guiding "
47 "passes are noisy using extra processing time.")},
48 {0, nullptr, 0, nullptr, nullptr}};
49
52 "FOLLOW_SCENE",
53 0,
54 N_("Follow Scene"),
55 N_("Use the scene's denoising quality setting")},
56 {CMP_NODE_DENOISE_QUALITY_HIGH, "HIGH", 0, "High", "High quality"},
58 "BALANCED",
59 0,
60 N_("Balanced"),
61 N_("Balanced between performance and quality")},
62 {CMP_NODE_DENOISE_QUALITY_FAST, "FAST", 0, "Fast", "High performance"},
63 {0, nullptr, 0, nullptr, nullptr}};
64
66{
67 b.use_custom_socket_order();
68 b.allow_any_socket_order();
69 b.add_input<decl::Color>("Image"_ustr)
70 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
71 .hide_value()
72 .structure_type(StructureType::Dynamic);
73 b.add_output<decl::Color>("Image"_ustr)
74 .structure_type(StructureType::Dynamic)
75 .align_with_previous();
76
77 b.add_input<decl::Color>("Albedo"_ustr)
78 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
79 .hide_value()
80 .structure_type(StructureType::Dynamic);
81 b.add_input<decl::Vector>("Normal"_ustr)
82 .default_value({0.0f, 0.0f, 0.0f})
83 .min(-1.0f)
84 .max(1.0f)
85 .hide_value()
86 .structure_type(StructureType::Dynamic);
87 b.add_input<decl::Bool>("HDR"_ustr).default_value(true);
88 b.add_input<decl::Menu>("Prefilter"_ustr)
90 .static_items(prefilter_items)
92 b.add_input<decl::Menu>("Quality"_ustr)
93 .default_value(CMP_NODE_DENOISE_QUALITY_SCENE)
94 .static_items(quality_items)
96}
97
98static void node_init(bNodeTree * /*ntree*/, bNode *node)
99{
100 /* Unused, kept for forward compatibility. */
101 NodeDenoise *ndg = MEM_new<NodeDenoise>(__func__);
102 node->storage = ndg;
103}
104
105static bool is_oidn_supported()
106{
107#ifdef WITH_OPENIMAGEDENOISE
108# if defined(__APPLE__)
109 /* Always supported through Accelerate framework BNNS. */
110 return true;
111# elif defined(__aarch64__) || defined(_M_ARM64)
112 /* OIDN 2.2 and up supports ARM64 on Windows and Linux. */
113 return true;
114# else
115 return BLI_cpu_support_sse42();
116# endif
117#else
118 return false;
119#endif
120}
121
122static void node_draw_buttons(ui::Layout &layout, bContext * /*C*/, PointerRNA * /*ptr*/)
123{
124#ifndef WITH_OPENIMAGEDENOISE
125 layout.label(RPT_("Disabled. Built without OpenImageDenoise"), ICON_STATUS_ERROR);
126#else
127 if (!is_oidn_supported()) {
128 layout.label(RPT_("Disabled. Platform not supported"), ICON_STATUS_ERROR);
129 }
130#endif
131}
132
133using namespace blender::compositor;
134
135/* A callback to cancel the filter operations by evaluating the context's is_canceled method. The
136 * API specifies that true indicates the filter should continue, while false indicates it should
137 * stop, so invert the condition. This callback can also be used to track progress using the given
138 * n argument, but we currently don't make use of it. See OIDNProgressMonitorFunction in the API
139 * for more information. */
140[[maybe_unused]] static bool oidn_progress_monitor_function(void *user_ptr, double /*n*/)
141{
142 const Context *context = static_cast<const Context *>(user_ptr);
143 return !context->is_canceled();
144}
145
147 public:
149
150 void execute() override
151 {
152 const Result &input_image = this->get_input("Image");
153 Result &output_image = this->get_result("Image");
154
155 if (!is_oidn_supported() || input_image.is_single_value()) {
156 output_image.share_data(input_image);
157 return;
158 }
159
160#ifdef WITH_OPENIMAGEDENOISE
161 oidn::DeviceRef device = create_oidn_device(this->context());
162 device.set("setAffinity", false);
163 device.commit();
164
165 /* For GPU, we download the input into a temporary result and mark it to be freed later, and
166 * since it is temporary, we also perform denoising in-place. */
167 Result denoise_input = this->context().create_result(input_image.type());
168 Result *denoise_output = nullptr;
169 if (this->context().use_gpu()) {
170 Result input_image_cpu = input_image.download_to_cpu();
171 denoise_input.share_data(input_image_cpu);
172 input_image_cpu.release();
173
174 denoise_output = &denoise_input;
175 }
176 else {
177 denoise_input.share_data(input_image);
178
179 output_image.allocate_texture(input_image.domain());
180 denoise_output = &output_image;
181 }
182
183 oidn::BufferRef input_buffer = create_oidn_buffer(device, denoise_input);
184 oidn::BufferRef output_buffer = create_oidn_buffer(device, *denoise_output);
185
186 oidn::FilterRef filter = device.newFilter("RT");
187 const int2 size = input_image.domain().data_size;
188 const int pixel_stride = sizeof(float) * denoise_input.channels_count();
189 filter.setImage("color", input_buffer, oidn::Format::Float3, size.x, size.y, 0, pixel_stride);
190 filter.setImage(
191 "output", output_buffer, oidn::Format::Float3, size.x, size.y, 0, pixel_stride);
192 filter.set("hdr", use_hdr());
193 filter.set("cleanAux", auxiliary_passes_are_clean());
194 this->set_filter_quality(filter);
195 filter.setProgressMonitorFunction(oidn_progress_monitor_function, &context());
196
197 /* If the albedo input is not a single value input, set it to the albedo input of the filter,
198 * denoising it if needed. */
199 Result &input_albedo = this->get_input("Albedo");
200 Result denoise_albedo = this->context().create_result(input_albedo.type());
201 if (!input_albedo.is_single_value()) {
203 denoise_albedo.share_data(input_albedo.derived_resources()
205 .get(this->context(),
206 input_albedo,
207 DenoisedAuxiliaryPassType::Albedo,
208 this->get_quality())
209 .result);
210 }
211 else {
212 if (this->context().use_gpu()) {
213 Result input_albedo_cpu = input_albedo.download_to_cpu();
214 denoise_albedo.share_data(input_albedo_cpu);
215 input_albedo_cpu.release();
216 }
217 else {
218 denoise_albedo.share_data(input_albedo);
219 }
220 }
221
222 oidn::BufferRef albedo_buffer = create_oidn_buffer(device, denoise_albedo);
223 const int pixel_stride = sizeof(float) * denoise_albedo.channels_count();
224 filter.setImage(
225 "albedo", albedo_buffer, oidn::Format::Float3, size.x, size.y, 0, pixel_stride);
226 }
227
228 /* If the albedo and normal inputs are not single value inputs, set the normal input to the
229 * albedo input of the filter, denoising it if needed. Notice that we also consider the albedo
230 * input because OIDN doesn't support denoising with only the normal auxiliary pass. */
231 Result &input_normal = this->get_input("Normal");
232 Result denoise_normal = this->context().create_result(input_normal.type());
233 if (!input_albedo.is_single_value() && !input_normal.is_single_value()) {
235 denoise_normal.share_data(input_normal.derived_resources()
237 .get(this->context(),
238 input_normal,
239 DenoisedAuxiliaryPassType::Normal,
240 this->get_quality())
241 .result);
242 }
243 else {
244 if (this->context().use_gpu()) {
245 Result input_normal_cpu = input_normal.download_to_cpu();
246 denoise_normal.share_data(input_normal_cpu);
247 input_normal_cpu.release();
248 }
249 else {
250 denoise_normal.share_data(input_normal);
251 }
252 }
253
254 oidn::BufferRef normal_buffer = create_oidn_buffer(device, denoise_normal);
255 const int pixel_stride = sizeof(float) * denoise_normal.channels_count();
256 filter.setImage(
257 "normal", normal_buffer, oidn::Format::Float3, size.x, size.y, 0, pixel_stride);
258 }
259
260 filter.commit();
261 filter.execute();
262
263 if (output_buffer.getStorage() != oidn::Storage::Host) {
264 output_buffer.read(
265 0, denoise_output->size_in_bytes(), denoise_output->cpu_data_for_write().data());
266 }
267
268 if (this->context().use_gpu()) {
269 Result output_gpu = denoise_output->upload_to_gpu(true);
270 output_image.share_data(output_gpu);
271 output_gpu.release();
272 }
273 else {
274 /* OIDN already wrote to the output directly, however, OIDN skips the alpha channel, so we
275 * need to restore it. */
276 parallel_for(size, [&](const int2 texel) {
277 const float alpha = input_image.load_pixel<Color>(texel).a;
278 output_image.store_pixel(
279 texel, Color(float4(float4(output_image.load_pixel<Color>(texel)).xyz(), alpha)));
280 });
281 }
282
283 denoise_input.release();
284 denoise_albedo.release();
285 denoise_normal.release();
286#endif
287 }
288
289 /* If the pre-filter mode is set to CMP_NODE_DENOISE_PREFILTER_NONE, that it means the supplied
290 * auxiliary passes are already noise-free, if it is set to CMP_NODE_DENOISE_PREFILTER_ACCURATE,
291 * the auxiliary passes will be denoised before denoising the main image, so in both cases, the
292 * auxiliary passes are considered clean. If it is set to CMP_NODE_DENOISE_PREFILTER_FAST on the
293 * other hand, the auxiliary passes are assumed to be noisy and are thus not clean, and will be
294 * denoised while denoising the main image. */
299
300 /* Returns whether the auxiliary passes should be denoised, see the auxiliary_passes_are_clean
301 * method for more information. */
306
307#ifdef WITH_OPENIMAGEDENOISE
308# if OIDN_VERSION_MAJOR >= 2
309 oidn::Quality get_quality()
310 {
311 const CMPNodeDenoiseQuality node_quality = this->get_quality_mode();
312
313 if (node_quality == CMP_NODE_DENOISE_QUALITY_SCENE) {
314 const eCompositorDenoiseQaulity scene_quality = context().get_denoise_quality();
315 switch (scene_quality) {
316# if OIDN_VERSION >= 20300
318 return oidn::Quality::Fast;
319# endif
321 return oidn::Quality::Balanced;
323 return oidn::Quality::High;
324 }
325
326 return oidn::Quality::High;
327 }
328
329 switch (node_quality) {
330# if OIDN_VERSION >= 20300
332 return oidn::Quality::Fast;
333# endif
335 return oidn::Quality::Balanced;
338 return oidn::Quality::High;
339 }
340
341 return oidn::Quality::High;
342 }
343# endif /* OIDN_VERSION_MAJOR >= 2 */
344
345 void set_filter_quality([[maybe_unused]] oidn::FilterRef &filter)
346 {
347# if OIDN_VERSION_MAJOR >= 2
348 oidn::Quality quality = this->get_quality();
349 filter.set("quality", quality);
350# endif
351 }
352#endif /* WITH_OPENIMAGEDENOISE */
353
354 bool use_hdr()
355 {
356 return this->get_input("HDR").get_single_value_default<bool>();
357 }
358
360 {
362 this->get_input("Prefilter").get_single_value_default<MenuValue>().value);
363 }
364
366 {
368 this->get_input("Quality").get_single_value_default<MenuValue>().value);
369 }
370};
371
373{
374 return new DenoiseOperation(context, node);
375}
376
377static void node_register()
378{
379 static bke::bNodeType ntype;
380
381 cmp_node_type_base(&ntype, "CompositorNodeDenoise"_ustr, CMP_NODE_DENOISE);
382 ntype.ui_name = "Denoise";
383 ntype.ui_description = "Denoise renders from Cycles and other ray tracing renderers";
384 ntype.enum_name_legacy = "DENOISE";
386 ntype.declare = node_declare;
388 ntype.initfunc = node_init;
392
394}
396
397} // namespace blender::nodes::node_composite_denoise_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_DENOISE
#define RPT_(msgid)
Read Blender memory allocation and freeing.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
Result create_result(ResultType type, ResultPrecision precision)
eCompositorDenoiseQaulity get_denoise_quality() const
DenoisedAuxiliaryPassContainer denoised_auxiliary_passes
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
void share_data(const Result &source)
Definition result.cc:996
Result upload_to_gpu(const bool from_pool) const
Definition result.cc:671
void store_pixel(const int2 &texel, const T &pixel_value)
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
int64_t size_in_bytes() const
Definition result.cc:1224
int64_t channels_count() const
Definition result.cc:1180
Result download_to_cpu() const
Definition result.cc:747
GMutableSpan cpu_data_for_write()
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
T load_pixel(const int2 &texel) const
DerivedResources & derived_resources()
Definition result.cc:1129
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void parallel_for(const int2 range, const Function &function)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_draw_buttons(ui::Layout &layout, bContext *, PointerRNA *)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static bool oidn_progress_monitor_function(void *user_ptr, double)
static const EnumPropertyItem quality_items[]
static const EnumPropertyItem prefilter_items[]
@ SCE_COMPOSITOR_DENOISE_FAST
@ SCE_COMPOSITOR_DENOISE_BALANCED
@ SCE_COMPOSITOR_DENOISE_HIGH
int BLI_cpu_support_sse42()
Definition system.cc:177
@ CMP_NODE_DENOISE_QUALITY_HIGH
@ CMP_NODE_DENOISE_QUALITY_BALANCED
@ CMP_NODE_DENOISE_QUALITY_FAST
@ CMP_NODE_DENOISE_QUALITY_SCENE
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ CMP_NODE_DENOISE_PREFILTER_NONE
@ CMP_NODE_DENOISE_PREFILTER_FAST
@ CMP_NODE_DENOISE_PREFILTER_ACCURATE
DO_INLINE void filter(lfVector *V, fmatrix3x3 *S)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void label(StringRef name, int icon)
#define N_(msgid)