Blender V5.3
denoiser_oidn.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7#include <array>
8
9#include "device/device.h"
10#include "device/queue.h"
12#include "session/buffers.h"
13#include "util/array.h"
14#include "util/log.h"
16#include "util/path.h"
17
19
21
22OIDNDenoiser::OIDNDenoiser(Device *denoiser_device, const DenoiseParams &params)
23 : Denoiser(denoiser_device, params)
24#ifdef WITH_OPENIMAGEDENOISE
25 ,
26 base_(this)
27#endif
28{
30
31#ifndef WITH_OPENIMAGEDENOISE
32 set_error("Failed to denoise, build has no OpenImageDenoise support");
33#else
34 if (!openimagedenoise_supported()) {
35 set_error("OpenImageDenoiser is not supported on this CPU: missing SSE 4.1 support");
36 }
37#endif
38}
39
40#ifdef WITH_OPENIMAGEDENOISE
41static bool oidn_progress_monitor_function(void *user_ptr, double /*n*/)
42{
43 OIDNDenoiser *oidn_denoiser = reinterpret_cast<OIDNDenoiser *>(user_ptr);
44 return !oidn_denoiser->is_cancelled();
45}
46
47class OIDNPass {
48 public:
49 OIDNPass() = default;
50
51 OIDNPass(const BufferParams &buffer_params,
52 const char *name,
53 PassType type,
55 : name(name), type(type), mode(mode)
56 {
57 offset = buffer_params.get_pass_offset(type, mode);
58 need_scale = (type == PASS_DENOISING_ALBEDO || type == PASS_DENOISING_NORMAL);
59
60 const PassInfo pass_info = Pass::get_info(type, mode);
61 num_components = pass_info.num_components;
62 use_compositing = pass_info.use_compositing;
63 use_denoising_albedo = pass_info.use_denoising_albedo;
64 }
65
66 operator bool() const
67 {
68 return name[0] != '\0';
69 }
70
71 /* Name of an image which will be passed to the OIDN library.
72 * Should be one of the following: color, albedo, normal, output.
73 * The albedo and normal images are optional. */
74 const char *name = "";
75
76 PassType type = PASS_NONE;
78 int num_components = -1;
79 bool use_compositing = false;
80 bool use_denoising_albedo = true;
81
82 /* Offset of beginning of this pass in the render buffers. */
83 int offset = -1;
84
85 /* Denotes whether the data is to be scaled down with the number of passes.
86 * Is required for albedo and normal passes. The color pass OIDN will perform auto-exposure, so
87 * scaling is not needed for the color pass unless adaptive sampling is used.
88 *
89 * NOTE: Do not scale the output pass, as that requires to be a pointer in the original buffer.
90 * All the scaling on the output needed for integration with adaptive sampling will happen
91 * outside of generic pass handling. */
92 bool need_scale = false;
93
94 /* The content of the pass has been pre-filtered. */
95 bool is_filtered = false;
96
97 /* For the scaled passes, the data which holds values of scaled pixels. */
98 array<float> scaled_buffer;
99};
100
101class OIDNDenoiseContext {
102 friend class OIDNDenoiser;
103
104 public:
105 OIDNDenoiseContext(OIDNDenoiser *denoiser,
106 const DenoiseParams &denoise_params,
107 const BufferParams &buffer_params,
108 RenderBuffers *render_buffers,
109 const int num_samples,
110 const bool allow_inplace_modification)
111 : denoiser_(denoiser),
112 denoise_params_(denoise_params),
113 buffer_params_(buffer_params),
114 render_buffers_(render_buffers),
115 num_samples_(num_samples),
116 allow_inplace_modification_(allow_inplace_modification),
117 pass_sample_count_(buffer_params_.get_pass_offset(PASS_SAMPLE_COUNT))
118 {
119 if (denoise_params_.passes & DENOISER_PASS_ALBEDO) {
120 oidn_albedo_pass_ = OIDNPass(buffer_params_, "albedo", PASS_DENOISING_ALBEDO);
121 }
122
123 if (denoise_params_.passes & DENOISER_PASS_NORMAL) {
124 oidn_normal_pass_ = OIDNPass(buffer_params_, "normal", PASS_DENOISING_NORMAL);
125 }
126 }
127
128 bool need_denoising() const
129 {
130 if (buffer_params_.width == 0 && buffer_params_.height == 0) {
131 return false;
132 }
133
134 return true;
135 }
136
137 /* Make the guiding passes available by a sequential denoising of various passes. */
138 void read_guiding_passes()
139 {
140 read_guiding_pass(oidn_albedo_pass_);
141 read_guiding_pass(oidn_normal_pass_);
142 }
143
144 bool denoise_pass(const PassType pass_type)
145 {
146 OIDNPass oidn_color_pass(buffer_params_, "color", pass_type);
147 if (oidn_color_pass.offset == PASS_UNUSED) {
148 return true;
149 }
150
151 if (oidn_color_pass.use_denoising_albedo) {
152 if (albedo_replaced_with_fake_) {
153 LOG_ERROR << "Pass which requires albedo is denoised after fake albedo has been set.";
154 return false;
155 }
156 }
157
158 OIDNPass oidn_output_pass(buffer_params_, "output", pass_type, PassMode::DENOISED);
159 if (oidn_output_pass.offset == PASS_UNUSED) {
160 LOG_DFATAL << "Missing denoised pass " << pass_type_as_string(pass_type);
161 return false;
162 }
163
164 OIDNPass oidn_color_access_pass = read_input_pass(oidn_color_pass, oidn_output_pass);
165
166 if (!denoiser_->base_.oidn_filter_) {
167 denoiser_->set_error("OpenImageDenoise filter is not initialized");
168 return false;
169 }
170
171 if (!filter_guiding_pass_if_needed(oidn_albedo_pass_)) {
172 return false;
173 }
174 if (!filter_guiding_pass_if_needed(oidn_normal_pass_)) {
175 return false;
176 }
177
178 set_input_pass(denoiser_->base_.oidn_filter_, oidn_color_access_pass);
179 set_guiding_passes(denoiser_->base_.oidn_filter_, oidn_color_pass);
180 set_output_pass(denoiser_->base_.oidn_filter_, oidn_output_pass);
181
182 const bool clean_aux = denoise_params_.prefilter != DENOISER_PREFILTER_FAST;
183 oidnSetFilterInt(denoiser_->base_.oidn_filter_, "cleanAux", clean_aux);
184
185 if (!denoiser_->commit_and_execute_filter(denoiser_->base_.oidn_filter_)) {
186 return false;
187 }
188
189 postprocess_output(oidn_color_pass, oidn_output_pass);
190 return true;
191 }
192
193 protected:
194 bool filter_guiding_pass_if_needed(OIDNPass &oidn_pass)
195 {
196 if (denoise_params_.prefilter != DENOISER_PREFILTER_ACCURATE || !oidn_pass ||
197 oidn_pass.is_filtered)
198 {
199 return true;
200 }
201
202 OIDNFilter filter = (oidn_pass.type == PASS_DENOISING_ALBEDO) ?
203 denoiser_->base_.albedo_filter_ :
204 denoiser_->base_.normal_filter_;
205
206 if (!filter) {
207 denoiser_->set_error("OpenImageDenoise guiding filter is not initialized");
208 return false;
209 }
210
211 set_pass(filter, oidn_pass);
212 set_output_pass(filter, oidn_pass);
213
214 if (!denoiser_->commit_and_execute_filter(filter)) {
215 return false;
216 }
217
218 oidn_pass.is_filtered = true;
219 return true;
220 }
221
222 /* Make pixels of a guiding pass available by the denoiser. */
223 void read_guiding_pass(OIDNPass &oidn_pass)
224 {
225 if (!oidn_pass) {
226 return;
227 }
228
229 DCHECK(!oidn_pass.use_compositing);
230
231 if (denoise_params_.prefilter != DENOISER_PREFILTER_ACCURATE &&
232 !is_pass_scale_needed(oidn_pass))
233 {
234 /* Pass data is available as-is from the render buffers. */
235 return;
236 }
237
238 if (allow_inplace_modification_) {
239 scale_pass_in_render_buffers(oidn_pass);
240 return;
241 }
242
243 read_pass_pixels_into_buffer(oidn_pass);
244 }
245
246 /* Special reader of the input pass.
247 * To save memory it will read pixels into the output, and let the denoiser to perform an
248 * in-place operation. */
249 OIDNPass read_input_pass(OIDNPass &oidn_input_pass, const OIDNPass &oidn_output_pass)
250 {
251 const bool use_compositing = oidn_input_pass.use_compositing;
252
253 /* Simple case: no compositing is involved, no scaling is needed.
254 * The pass pixels will be referenced as-is, without extra processing. */
255 if (!use_compositing && !is_pass_scale_needed(oidn_input_pass)) {
256 return oidn_input_pass;
257 }
258
259 float *buffer_data = render_buffers_->buffer.data();
260 float *pass_data = buffer_data + oidn_output_pass.offset;
261
262 PassAccessor::Destination destination(pass_data, 3);
263 destination.pixel_stride = buffer_params_.pass_stride;
264
265 read_pass_pixels(oidn_input_pass, destination);
266
267 OIDNPass oidn_input_pass_at_output = oidn_input_pass;
268 oidn_input_pass_at_output.offset = oidn_output_pass.offset;
269
270 return oidn_input_pass_at_output;
271 }
272
273 /* Read pass pixels using PassAccessor into the given destination. */
274 void read_pass_pixels(const OIDNPass &oidn_pass, const PassAccessor::Destination &destination)
275 {
276 PassAccessor::PassAccessInfo pass_access_info;
277 pass_access_info.type = oidn_pass.type;
278 pass_access_info.mode = oidn_pass.mode;
279 pass_access_info.offset = oidn_pass.offset;
280
281 /* Denoiser operates on passes which are used to calculate the approximation, and is never used
282 * on the approximation. The latter is not even possible because OIDN does not support
283 * denoising of semi-transparent pixels. */
284 pass_access_info.use_approximate_shadow_catcher = false;
285 pass_access_info.use_approximate_shadow_catcher_background = false;
286 pass_access_info.show_active_pixels = false;
287
288 /* OIDN will perform an auto-exposure, so it is not required to know exact exposure configured
289 * by users. What is important is to use same exposure for read and write access of the pass
290 * pixels. */
291 const PassAccessorCPU pass_accessor(pass_access_info, 1.0f, num_samples_);
292
293 BufferParams buffer_params = buffer_params_;
294 buffer_params.window_x = 0;
295 buffer_params.window_y = 0;
296 buffer_params.window_width = buffer_params.width;
297 buffer_params.window_height = buffer_params.height;
298
299 pass_accessor.get_render_tile_pixels(render_buffers_, buffer_params, destination);
300 }
301
302 /* Read pass pixels using PassAccessor into a temporary buffer which is owned by the pass.. */
303 void read_pass_pixels_into_buffer(OIDNPass &oidn_pass)
304 {
305 LOG_DEBUG << "Allocating temporary buffer for pass " << oidn_pass.name << " ("
306 << pass_type_as_string(oidn_pass.type) << ")";
307
308 const int64_t width = buffer_params_.width;
309 const int64_t height = buffer_params_.height;
310
311 array<float> &scaled_buffer = oidn_pass.scaled_buffer;
312 scaled_buffer.resize(width * height * 3);
313
314 const PassAccessor::Destination destination(scaled_buffer.data(), 3);
315
316 read_pass_pixels(oidn_pass, destination);
317 }
318
319 /* Set OIDN image to reference pixels from the given render buffer pass.
320 * No transform to the pixels is done, no additional memory is used. */
321 void set_pass_referenced(OIDNFilter oidn_filter, const char *name, const OIDNPass &oidn_pass)
322 {
323 const int64_t x = buffer_params_.full_x;
324 const int64_t y = buffer_params_.full_y;
325 const int64_t width = buffer_params_.width;
326 const int64_t height = buffer_params_.height;
327 const int64_t offset = buffer_params_.offset;
328 const int64_t stride = buffer_params_.stride;
329 const int64_t pass_stride = buffer_params_.pass_stride;
330
331 const int64_t pixel_index = offset + x + y * stride;
332 const int64_t buffer_offset = pixel_index * pass_stride;
333
334 float *buffer_data = render_buffers_->buffer.data();
335
336 oidnSetSharedFilterImage(oidn_filter,
337 name,
338 buffer_data + buffer_offset + oidn_pass.offset,
339 OIDN_FORMAT_FLOAT3,
340 width,
341 height,
342 0,
343 pass_stride * sizeof(float),
344 stride * pass_stride * sizeof(float));
345 }
346
347 void set_pass_from_buffer(OIDNFilter oidn_filter, const char *name, OIDNPass &oidn_pass)
348 {
349 const int64_t width = buffer_params_.width;
350 const int64_t height = buffer_params_.height;
351
352 oidnSetSharedFilterImage(oidn_filter,
353 name,
354 oidn_pass.scaled_buffer.data(),
355 OIDN_FORMAT_FLOAT3,
356 width,
357 height,
358 0,
359 0,
360 0);
361 }
362
363 void set_pass(OIDNFilter oidn_filter, OIDNPass &oidn_pass)
364 {
365 set_pass(oidn_filter, oidn_pass.name, oidn_pass);
366 }
367 void set_pass(OIDNFilter oidn_filter, const char *name, OIDNPass &oidn_pass)
368 {
369 if (oidn_pass.scaled_buffer.empty()) {
370 set_pass_referenced(oidn_filter, name, oidn_pass);
371 }
372 else {
373 set_pass_from_buffer(oidn_filter, name, oidn_pass);
374 }
375 }
376
377 void set_input_pass(OIDNFilter oidn_filter, OIDNPass &oidn_pass)
378 {
379 set_pass_referenced(oidn_filter, oidn_pass.name, oidn_pass);
380 }
381
382 void set_guiding_passes(OIDNFilter oidn_filter, OIDNPass &oidn_pass)
383 {
384 if (oidn_albedo_pass_) {
385 if (oidn_pass.use_denoising_albedo) {
386 set_pass(oidn_filter, oidn_albedo_pass_);
387 }
388 else {
389 /* NOTE: OpenImageDenoise library implicitly expects albedo pass when normal pass has been
390 * provided. */
391 set_fake_albedo_pass(oidn_filter);
392 }
393 }
394
395 if (oidn_normal_pass_) {
396 set_pass(oidn_filter, oidn_normal_pass_);
397 }
398 }
399
400 void set_fake_albedo_pass(OIDNFilter oidn_filter)
401 {
402 const int64_t width = buffer_params_.width;
403 const int64_t height = buffer_params_.height;
404
405 if (!albedo_replaced_with_fake_) {
406 const int64_t num_pixel_components = width * height * 3;
407 oidn_albedo_pass_.scaled_buffer.resize(num_pixel_components);
408
409 for (int i = 0; i < num_pixel_components; ++i) {
410 oidn_albedo_pass_.scaled_buffer[i] = 0.5f;
411 }
412
413 albedo_replaced_with_fake_ = true;
414 }
415
416 set_pass(oidn_filter, oidn_albedo_pass_);
417 }
418
419 void set_output_pass(OIDNFilter oidn_filter, OIDNPass &oidn_pass)
420 {
421 set_pass(oidn_filter, "output", oidn_pass);
422 }
423
424 /* Scale output pass to match adaptive sampling per-pixel scale, as well as bring alpha channel
425 * back. */
426 void postprocess_output(const OIDNPass &oidn_input_pass, const OIDNPass &oidn_output_pass)
427 {
428 kernel_assert(oidn_input_pass.num_components == oidn_output_pass.num_components);
429
430 const int64_t x = buffer_params_.full_x;
431 const int64_t y = buffer_params_.full_y;
432 const int64_t width = buffer_params_.width;
433 const int64_t height = buffer_params_.height;
434 const int64_t offset = buffer_params_.offset;
435 const int64_t stride = buffer_params_.stride;
436 const int64_t pass_stride = buffer_params_.pass_stride;
437 const int64_t row_stride = stride * pass_stride;
438
439 const int64_t pixel_offset = offset + x + y * stride;
440 const int64_t buffer_offset = (pixel_offset * pass_stride);
441
442 float *buffer_data = render_buffers_->buffer.data();
443
444 const bool has_pass_sample_count = (pass_sample_count_ != PASS_UNUSED);
445 const bool need_scale = has_pass_sample_count || oidn_input_pass.use_compositing;
446
447 for (int y = 0; y < height; ++y) {
448 float *buffer_row = buffer_data + buffer_offset + y * row_stride;
449 for (int x = 0; x < width; ++x) {
450 float *buffer_pixel = buffer_row + x * pass_stride;
451 float *denoised_pixel = buffer_pixel + oidn_output_pass.offset;
452
453 if (need_scale) {
454 const float pixel_scale = has_pass_sample_count ?
455 __float_as_uint(buffer_pixel[pass_sample_count_]) :
456 num_samples_;
457
458 denoised_pixel[0] = denoised_pixel[0] * pixel_scale;
459 denoised_pixel[1] = denoised_pixel[1] * pixel_scale;
460 denoised_pixel[2] = denoised_pixel[2] * pixel_scale;
461 }
462
463 if (oidn_output_pass.num_components == 3) {
464 /* Pass without alpha channel. */
465 }
466 else if (!oidn_input_pass.use_compositing) {
467 /* Currently compositing passes are either 3-component (derived by dividing light passes)
468 * or do not have transparency (shadow catcher). Implicitly rely on this logic, as it
469 * simplifies logic and avoids extra memory allocation. */
470 const float *noisy_pixel = buffer_pixel + oidn_input_pass.offset;
471 denoised_pixel[3] = noisy_pixel[3];
472 }
473 else {
474 /* Assigning to zero since this is a default alpha value for 3-component passes, and it
475 * is an opaque pixel for 4 component passes. */
476 denoised_pixel[3] = 0;
477 }
478 }
479 }
480 }
481
482 bool is_pass_scale_needed(OIDNPass &oidn_pass) const
483 {
484 if (pass_sample_count_ != PASS_UNUSED) {
485 /* With adaptive sampling pixels will have different number of samples in them, so need to
486 * always scale the pass to make pixels uniformly sampled. */
487 return true;
488 }
489
490 if (!oidn_pass.need_scale) {
491 return false;
492 }
493
494 if (num_samples_ == 1) {
495 /* If the avoid scaling if there is only one sample, to save up time (so we don't divide
496 * buffer by 1). */
497 return false;
498 }
499
500 return true;
501 }
502
503 void scale_pass_in_render_buffers(OIDNPass &oidn_pass)
504 {
505 const int64_t x = buffer_params_.full_x;
506 const int64_t y = buffer_params_.full_y;
507 const int64_t width = buffer_params_.width;
508 const int64_t height = buffer_params_.height;
509 const int64_t offset = buffer_params_.offset;
510 const int64_t stride = buffer_params_.stride;
511 const int64_t pass_stride = buffer_params_.pass_stride;
512 const int64_t row_stride = stride * pass_stride;
513
514 const int64_t pixel_offset = offset + x + y * stride;
515 const int64_t buffer_offset = (pixel_offset * pass_stride);
516
517 float *buffer_data = render_buffers_->buffer.data();
518
519 const bool has_pass_sample_count = (pass_sample_count_ != PASS_UNUSED);
520
521 for (int y = 0; y < height; ++y) {
522 float *buffer_row = buffer_data + buffer_offset + y * row_stride;
523 for (int x = 0; x < width; ++x) {
524 float *buffer_pixel = buffer_row + x * pass_stride;
525 float *pass_pixel = buffer_pixel + oidn_pass.offset;
526
527 const float pixel_scale = 1.0f / (has_pass_sample_count ?
528 __float_as_uint(buffer_pixel[pass_sample_count_]) :
529 num_samples_);
530
531 pass_pixel[0] = pass_pixel[0] * pixel_scale;
532 pass_pixel[1] = pass_pixel[1] * pixel_scale;
533 pass_pixel[2] = pass_pixel[2] * pixel_scale;
534 }
535 }
536 }
537
538 OIDNDenoiser *denoiser_ = nullptr;
539
540 const DenoiseParams &denoise_params_;
541 const BufferParams &buffer_params_;
542 RenderBuffers *render_buffers_ = nullptr;
543 int num_samples_ = 0;
544 bool allow_inplace_modification_ = false;
545 int pass_sample_count_ = PASS_UNUSED;
546
547 /* Optional albedo and normal passes, reused by denoising of different pass types. */
548 OIDNPass oidn_albedo_pass_;
549 OIDNPass oidn_normal_pass_;
550
551 /* For passes which don't need albedo channel for denoising we replace the actual albedo with
552 * the (0.5, 0.5, 0.5). This flag indicates that the real albedo pass has been replaced with
553 * the fake values and denoising of passes which do need albedo can no longer happen. */
554 bool albedo_replaced_with_fake_ = false;
555};
556
557bool OIDNDenoiser::commit_and_execute_filter(OIDNFilter filter)
558{
559 const char *error_message = nullptr;
560
561 oidnCommitFilter(filter);
562 oidnExecuteFilter(filter);
563
564 const OIDNError err = oidnGetDeviceError(base_.oidn_device_, &error_message);
565 if (err == OIDN_ERROR_NONE || err == OIDN_ERROR_CANCELLED) {
566 return true;
567 }
568
569 if (error_message == nullptr) {
570 error_message = "Unspecified OIDN error";
571 }
572
573 LOG_ERROR << "OIDN error: " << error_message;
574 set_error(error_message);
575 return false;
576}
577
578bool OIDNDenoiser::denoise_create_if_needed(const OIDNDenoiseContext &context)
579{
580 /* Create device on first call if it doesn't exist yet. */
581 if (!base_.oidn_device_) {
582 base_.oidn_device_ = oidnNewDevice(OIDN_DEVICE_TYPE_CPU);
583 if (!base_.oidn_device_) {
584 set_error("Failed to create OIDN CPU device");
585 return false;
586 }
587 oidnSetDeviceBool(base_.oidn_device_, "setAffinity", false);
588 oidnCommitDevice(base_.oidn_device_);
589 base_.load_custom_weights();
590 }
591
592 const bool use_pass_albedo = params_.prefilter == DENOISER_PREFILTER_ACCURATE &&
593 (context.denoise_params_.passes & DENOISER_PASS_ALBEDO) != 0;
594 const bool use_pass_normal = params_.prefilter == DENOISER_PREFILTER_ACCURATE &&
595 (context.denoise_params_.passes & DENOISER_PASS_NORMAL) != 0;
596
597 const bool recreate_filter = (base_.oidn_filter_ == nullptr) ||
598 (base_.use_pass_albedo_ != use_pass_albedo) ||
599 (base_.use_pass_normal_ != use_pass_normal) ||
600 (base_.quality_ != params_.quality);
601
602 if (!recreate_filter) {
603 return true;
604 }
605
606 if (base_.albedo_filter_) {
607 oidnReleaseFilter(base_.albedo_filter_);
608 base_.albedo_filter_ = nullptr;
609 }
610
611 if (base_.normal_filter_) {
612 oidnReleaseFilter(base_.normal_filter_);
613 base_.normal_filter_ = nullptr;
614 }
615
616 if (base_.oidn_filter_) {
617 oidnReleaseFilter(base_.oidn_filter_);
618 base_.oidn_filter_ = nullptr;
619 }
620
621 if (!base_.create_filters(params_.quality, use_pass_albedo, use_pass_normal)) {
622 return false;
623 }
624
625 oidnSetFilterProgressMonitorFunction(base_.oidn_filter_, oidn_progress_monitor_function, this);
626 if (base_.albedo_filter_) {
627 oidnSetFilterProgressMonitorFunction(
628 base_.albedo_filter_, oidn_progress_monitor_function, this);
629 }
630 if (base_.normal_filter_) {
631 oidnSetFilterProgressMonitorFunction(
632 base_.normal_filter_, oidn_progress_monitor_function, this);
633 }
634 return true;
635}
636
637bool OIDNDenoiser::denoise_run(OIDNDenoiseContext &context, const PassType pass_type)
638{
639 return context.denoise_pass(pass_type);
640}
641
642static unique_ptr<DeviceQueue> create_device_queue(const RenderBuffers *render_buffers)
643{
644 Device *device = render_buffers->buffer.device;
645 if (device->info.has_gpu_queue) {
646 return device->gpu_queue_create();
647 }
648 return nullptr;
649}
650
651static void copy_render_buffers_from_device(unique_ptr<DeviceQueue> &queue,
652 RenderBuffers *render_buffers)
653{
654 if (queue) {
655 queue->copy_from_device(render_buffers->buffer);
656 queue->synchronize();
657 }
658 else {
659 render_buffers->copy_from_device();
660 }
661}
662
663static void copy_render_buffers_to_device(unique_ptr<DeviceQueue> &queue,
664 RenderBuffers *render_buffers)
665{
666 if (queue) {
667 queue->copy_to_device(render_buffers->buffer);
668 queue->synchronize();
669 }
670 else {
671 render_buffers->copy_to_device();
672 }
673}
674
675#endif
676
678 const BufferParams & /*denoised_buffer_params*/,
679 RenderBuffers *render_buffers,
680 const int num_samples,
681 const bool allow_inplace_modification,
682 const float2 /*pixel_jitter*/)
683{
684 DCHECK(openimagedenoise_supported())
685 << "OpenImageDenoise is not supported on this platform or build.";
686
687#ifdef WITH_OPENIMAGEDENOISE
689
690 /* Make sure the host-side data is available for denoising. */
691 unique_ptr<DeviceQueue> queue = create_device_queue(render_buffers);
692 copy_render_buffers_from_device(queue, render_buffers);
693
694 OIDNDenoiseContext context(
695 this, params_, buffer_params, render_buffers, num_samples, allow_inplace_modification);
696
697 if (context.need_denoising()) {
698 context.read_guiding_passes();
699
700 if (!denoise_create_if_needed(context)) {
701 return false;
702 }
703
704 if (!base_.denoise_configure_if_needed(context.buffer_params_.width,
705 context.buffer_params_.height))
706 {
707 return false;
708 }
709
710 const std::array<PassType, 3> passes = {
711 {/* Passes which will use real albedo when it is available. */
714
715 /* Passes which do not need albedo and hence if real is present it needs to become fake.
716 */
718
719 for (const PassType pass_type : passes) {
720 if (!denoise_run(context, pass_type)) {
721 return false;
722 }
723 if (is_cancelled()) {
724 return false;
725 }
726 }
727
728 /* TODO: It may be possible to avoid this copy, but we have to ensure that when other code
729 * copies data from the device it doesn't overwrite the denoiser buffers. */
730 copy_render_buffers_to_device(queue, render_buffers);
731 }
732#else
733 (void)buffer_params;
734 (void)render_buffers;
735 (void)num_samples;
736 (void)allow_inplace_modification;
737#endif
738
739 /* This code is not supposed to run when compiled without OIDN support, so can assume if we made
740 * it up here all passes are properly denoised. */
741 return true;
742}
743
volatile int lock
long long int int64_t
int get_pass_offset(PassType type, PassMode mode=PassMode::NOISY) const
Definition buffers.cpp:164
int window_y
Definition buffers.h:78
int window_height
Definition buffers.h:80
int window_width
Definition buffers.h:79
NODE_DECLARE int width
Definition buffers.h:70
int window_x
Definition buffers.h:77
DenoiserType type
Definition denoise.h:73
DenoiserQuality quality
Definition denoise.h:89
DenoiserPrefilter prefilter
Definition denoise.h:88
void set_error(const string &error)
Definition denoiser.h:125
DenoiseParams params_
Definition denoiser.h:135
Denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:252
bool is_cancelled() const
Definition denoiser.h:117
bool has_gpu_queue
virtual unique_ptr< DeviceQueue > gpu_queue_create()
DeviceInfo info
bool denoise_buffer(const BufferParams &buffer_params, const BufferParams &denoised_buffer_params, RenderBuffers *render_buffers, int num_samples, bool allow_inplace_modification, float2 pixel_jitter) override
OIDNDenoiser(Device *denoiser_device, const DenoiseParams &params)
static thread_mutex mutex_
PassInfo get_info() const
Definition pass.cpp:150
device_vector< float > buffer
Definition buffers.h:158
bool copy_from_device()
Definition buffers.cpp:286
void copy_to_device()
Definition buffers.cpp:299
T * resize(const size_t newsize)
@ DENOISER_PREFILTER_FAST
Definition denoise.h:46
@ DENOISER_PREFILTER_ACCURATE
Definition denoise.h:50
@ DENOISER_PASS_NORMAL
Definition denoise.h:30
@ DENOISER_PASS_ALBEDO
Definition denoise.h:28
@ DENOISER_OPENIMAGEDENOISE
Definition denoise.h:13
#define kernel_assert(cond)
#define PASS_UNUSED
#define CCL_NAMESPACE_END
#define __float_as_uint(x)
if(state< num_states)
PassType
@ PASS_SHADOW_CATCHER_MATTE
@ PASS_SHADOW_CATCHER
@ PASS_DENOISING_NORMAL
@ PASS_COMBINED
@ PASS_NONE
@ PASS_SAMPLE_COUNT
@ PASS_DENOISING_ALBEDO
#define DCHECK(expression)
Definition log.h:135
#define DCHECK_EQ(a, b)
Definition log.h:144
#define LOG_DFATAL
Definition log.h:100
#define LOG_DEBUG
Definition log.h:107
#define LOG_ERROR
Definition log.h:101
int context(const bContext *C, const char *member, bContextDataResult *result)
DO_INLINE void filter(lfVector *V, fmatrix3x3 *S)
CCL_NAMESPACE_BEGIN const char * pass_type_as_string(const PassType type)
Definition pass.cpp:12
PassMode
Definition pass.h:20
@ DENOISED
Definition pass.h:22
@ NOISY
Definition pass.h:21
bool use_compositing
Definition pass.h:39
bool use_denoising_albedo
Definition pass.h:44
int num_components
Definition pass.h:28
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28