Blender V5.0
denoiser.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7/* TODO(sergey): The integrator folder might not be the best. Is easy to move files around if the
8 * better place is figured out. */
9
10#include <functional>
11
12#include "device/denoise.h"
13#include "device/device.h"
14#include "util/unique_ptr.h"
15
17
18class BufferParams;
19class Device;
21class RenderBuffers;
22class Progress;
23
24bool use_optix_denoiser(Device *denoiser_device, const DenoiseParams &params);
25
26bool use_gpu_oidn_denoiser(Device *denoiser_device, const DenoiseParams &params);
27
29 Device *cpu_fallback_device,
30 const DenoiseParams &params,
31 const GraphicsInteropDevice &interop_device,
32 Device *&single_denoiser_device);
33
34/* Implementation of a specific denoising algorithm.
35 *
36 * This class takes care of breaking down denoising algorithm into a series of device calls or to
37 * calls of an external API to denoise given input.
38 *
39 * TODO(sergey): Are we better with device or a queue here? */
40class Denoiser {
41 public:
42 /* Create denoiser for the given path trace device.
43 *
44 * Notes:
45 * - The denoiser must be configured. This means that `params.use` must be true.
46 * This is checked in debug builds.
47 * - The device might be MultiDevice.
48 * - If Denoiser from params is not supported by provided denoise device, then Blender will
49 * fallback on the OIDN CPU denoising and use provided cpu_fallback_device.
50 * - Specifying the graphics interop device helps pick a more efficient denoising device.*/
51 static unique_ptr<Denoiser> create(Device *denoiser_device,
52 Device *cpu_fallback_device,
53 const DenoiseParams &params,
54 const GraphicsInteropDevice &interop_device);
55
56 virtual ~Denoiser() = default;
57
58 void set_params(const DenoiseParams &params);
59 const DenoiseParams &get_params() const;
60
61 /* Recommended type for viewport denoising. */
62 static DenoiserType automatic_viewport_denoiser_type(const DeviceInfo &denoise_device_info);
63
64 /* Create devices and load kernels needed for denoising.
65 * The progress is used to communicate state when kernels actually needs to be loaded.
66 *
67 * NOTE: The `progress` is an optional argument, can be nullptr. */
68 virtual bool load_kernels(Progress *progress);
69
70 /* Denoise the entire buffer.
71 *
72 * Buffer parameters denotes an effective parameters used during rendering. It could be
73 * a lower resolution render into a bigger allocated buffer, which is used in viewport during
74 * navigation and non-unit pixel size. Use that instead of render_buffers->params.
75 *
76 * The buffer might be coming from a "foreign" device from what this denoise is created for.
77 * This means that in general case the denoiser will make sure the input data is available on
78 * the denoiser device, perform denoising, and put data back to the device where the buffer
79 * came from.
80 *
81 * The `num_samples` corresponds to the number of samples in the render buffers. It is used
82 * to scale buffers down to the "final" value in algorithms which don't do automatic exposure,
83 * or which needs "final" value for data passes.
84 *
85 * The `allow_inplace_modification` means that the denoiser is allowed to do in-place
86 * modification of the input passes (scaling them down i.e.). This will lower the memory
87 * footprint of the denoiser but will make input passes "invalid" (from path tracer) point of
88 * view.
89 *
90 * Returns true when all passes are denoised. Will return false if there is a denoiser error (for
91 * example, caused by misconfigured denoiser) or when user requested to cancel rendering. */
92 virtual bool denoise_buffer(const BufferParams &buffer_params,
93 RenderBuffers *render_buffers,
94 const int num_samples,
95 bool allow_inplace_modification) = 0;
96
97 /* Get a device which is used to perform actual denoising.
98 *
99 * Notes:
100 *
101 * - The device can be different from the path tracing device. This happens, for example, when
102 * using OptiX denoiser and rendering on CPU.
103 *
104 * - No threading safety is ensured in this call. This means, that it is up to caller to ensure
105 * that there is no threading-conflict between denoising task lazily initializing the device
106 * and access to this device happen. */
108
109 std::function<bool(void)> is_cancelled_cb;
110
111 bool is_cancelled() const
112 {
113 if (!is_cancelled_cb) {
114 return false;
115 }
116 return is_cancelled_cb();
117 }
118
119 void set_error(const string &error)
120 {
121 denoiser_device_->set_error(error);
122 }
123
124 protected:
125 Denoiser(Device *denoiser_device, const DenoiseParams &params);
126
127 /* Get device type mask which is used to filter available devices when new device needs to be
128 * created. */
129 virtual uint get_device_type_mask() const = 0;
130
134};
135
unsigned int uint
static unique_ptr< Denoiser > create(Device *denoiser_device, Device *cpu_fallback_device, const DenoiseParams &params, const GraphicsInteropDevice &interop_device)
Definition denoiser.cpp:148
void set_error(const string &error)
Definition denoiser.h:119
virtual bool denoise_buffer(const BufferParams &buffer_params, RenderBuffers *render_buffers, const int num_samples, bool allow_inplace_modification)=0
void set_params(const DenoiseParams &params)
Definition denoiser.cpp:218
static DenoiserType automatic_viewport_denoiser_type(const DeviceInfo &denoise_device_info)
Definition denoiser.cpp:184
DenoiseParams params_
Definition denoiser.h:133
Denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:211
virtual bool load_kernels(Progress *progress)
Definition denoiser.cpp:235
bool denoise_kernels_are_loaded_
Definition denoiser.h:132
bool is_cancelled() const
Definition denoiser.h:111
std::function< bool(void)> is_cancelled_cb
Definition denoiser.h:109
virtual uint get_device_type_mask() const =0
Device * get_denoiser_device() const
Definition denoiser.cpp:268
Device * denoiser_device_
Definition denoiser.h:131
const DenoiseParams & get_params() const
Definition denoiser.cpp:230
virtual ~Denoiser()=default
DenoiserType
Definition denoise.h:11
bool use_gpu_oidn_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:93
bool use_optix_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:81
DenoiseParams get_effective_denoise_params(Device *denoiser_device, Device *cpu_fallback_device, const DenoiseParams &params, const GraphicsInteropDevice &interop_device, Device *&single_denoiser_device)
Definition denoiser.cpp:105
#define CCL_NAMESPACE_END
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void error(const char *str)