Blender V5.3
denoiser.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 "device/device.h"
8
12#ifdef WITH_OPENIMAGEDENOISE
14#endif
15#ifdef WITH_OPTIX
17#endif
18#include "session/buffers.h"
19
20#include "util/log.h"
22#include "util/progress.h"
23
25
26/* Check whether given device is single (not a MultiDevice). */
27static bool is_single_device(const Device *device)
28{
29 if (device->info.type == DEVICE_MULTI) {
30 /* Assume multi-device is never created with a single sub-device.
31 * If one requests such configuration it should be checked on the session level. */
32 return false;
33 }
34
35 if (!device->info.multi_devices.empty()) {
36 /* Some configurations will use multi_devices, but keep the type of an individual device.
37 * This does simplify checks for homogeneous setups, but here we really need a single device.
38 */
39 return false;
40 }
41
42 return true;
43}
44
45/* Find best suitable device to perform denoiser on. Will iterate over possible sub-devices of
46 * multi-device. */
48 const DenoiserType type,
49 const GraphicsInteropDevice &interop_device)
50{
51 Device *best_device = nullptr;
52
53 device->foreach_device([&](Device *sub_device) {
54 if ((sub_device->info.denoisers & type) == 0) {
55 return;
56 }
57
58 if (!best_device) {
59 best_device = sub_device;
60 }
61 else {
62 /* Prefer non-CPU devices over CPU for performance reasons. */
63 if (sub_device->info.type != DEVICE_CPU && best_device->info.type == DEVICE_CPU) {
64 best_device = sub_device;
65 }
66
67 /* Prefer a device that can use graphics interop for faster display update. */
68 if (sub_device->should_use_graphics_interop(interop_device) &&
69 !best_device->should_use_graphics_interop(interop_device))
70 {
71 best_device = sub_device;
72 }
73
74 /* TODO(sergey): Choose fastest device from available ones. Taking into account performance
75 * of the device and data transfer cost. */
76 }
77 });
78
79 return best_device;
80}
81
82bool use_optix_denoiser(Device *denoiser_device, const DenoiseParams &params)
83{
84#ifdef WITH_OPTIX
85 return (params.type == DENOISER_OPTIX &&
86 OptiXDenoiser::is_device_supported(denoiser_device->info));
87#else
88 (void)denoiser_device;
89 (void)params;
90 return false;
91#endif
92}
93
94bool use_gpu_oidn_denoiser(Device *denoiser_device, const DenoiseParams &params)
95{
96#ifdef WITH_OPENIMAGEDENOISE
97 return (params.type == DENOISER_OPENIMAGEDENOISE && params.use_gpu &&
98 OIDNDenoiserGPU::is_device_supported(denoiser_device->info));
99#else
100 (void)denoiser_device;
101 (void)params;
102 return false;
103#endif
104}
105
106bool use_dlss_denoiser(Device *denoiser_device, const DenoiseParams &params)
107{
108#ifdef WITH_DLSS
109 return (params.type == DENOISER_DLSS &&
110 DLSSDenoiser::is_device_supported(denoiser_device->info));
111#else
112 (void)denoiser_device;
113 (void)params;
114 return false;
115#endif
116}
117
119 Device *cpu_fallback_device,
120 const DenoiseParams &params,
121 const GraphicsInteropDevice &interop_device,
122 Device *&single_denoiser_device)
123{
124 DCHECK(params.use);
125
126 DenoiseParams effective_denoise_params = params;
127
128 single_denoiser_device = nullptr;
129 if (is_single_device(denoiser_device)) {
130 /* Simple case: denoising happens on a single device. */
131 single_denoiser_device = denoiser_device;
132 }
133 else {
134 /* Find best device from the ones which are proposed for denoising.
135 * The choice is expected to be between a few GPUs, or between a GPU and a CPU
136 * or between a few GPUs and a CPU. */
137 single_denoiser_device = find_best_device(denoiser_device, params.type, interop_device);
138 }
139 /* Ensure that we have a device to be used later in the code below. */
140 if (single_denoiser_device == nullptr) {
141 single_denoiser_device = cpu_fallback_device;
142 }
143
144 const bool is_cpu_denoiser_device = single_denoiser_device->info.type == DEVICE_CPU;
145 if (is_cpu_denoiser_device == false) {
146 if (use_optix_denoiser(single_denoiser_device, effective_denoise_params) ||
147 use_gpu_oidn_denoiser(single_denoiser_device, effective_denoise_params) ||
148 use_dlss_denoiser(single_denoiser_device, effective_denoise_params))
149 {
150 /* Denoising parameters are correct and there is no need to fall back to CPU OIDN. */
151 return effective_denoise_params;
152 }
153 }
154
155 /* Always fallback to OIDN on CPU. */
156 effective_denoise_params.type = DENOISER_OPENIMAGEDENOISE;
157 effective_denoise_params.use_gpu = false;
158 effective_denoise_params.upscale_factor = 1.0f;
159
160 return effective_denoise_params;
161}
162
164 Device *cpu_fallback_device,
165 const DenoiseParams &params,
166 const GraphicsInteropDevice &interop_device)
167{
168
169 Device *single_denoiser_device;
170 const DenoiseParams effective_denoiser_params = get_effective_denoise_params(
171 denoiser_device, cpu_fallback_device, params, interop_device, single_denoiser_device);
172
173 const bool is_cpu_denoiser_device = single_denoiser_device->info.type == DEVICE_CPU;
174 if (is_cpu_denoiser_device == false) {
175#ifdef WITH_OPTIX
176 if (use_optix_denoiser(single_denoiser_device, effective_denoiser_params)) {
177 return make_unique<OptiXDenoiser>(single_denoiser_device, effective_denoiser_params);
178 }
179#endif
180
181#ifdef WITH_OPENIMAGEDENOISE
182 /* If available and allowed, then we will use OpenImageDenoise on GPU, otherwise on CPU. */
183 if (use_gpu_oidn_denoiser(single_denoiser_device, effective_denoiser_params)) {
184 return make_unique<OIDNDenoiserGPU>(single_denoiser_device, effective_denoiser_params);
185 }
186#endif
187
188#ifdef WITH_DLSS
189 if (use_dlss_denoiser(single_denoiser_device, effective_denoiser_params)) {
190 return make_unique<DLSSDenoiser>(single_denoiser_device, effective_denoiser_params);
191 }
192#endif
193 }
194
195 if (!openimagedenoise_supported()) {
196 return nullptr;
197 }
198
199 /* Used preference CPU when possible, and fallback on CPU fallback device otherwise. */
200 return make_unique<OIDNDenoiser>(is_cpu_denoiser_device ? single_denoiser_device :
201 cpu_fallback_device,
202 effective_denoiser_params);
203}
204
205bool Denoiser::is_device_supported(DenoiserType type, const DeviceInfo &denoise_device_info)
206{
207 switch (type) {
208#ifdef WITH_OPTIX
209 case DENOISER_OPTIX:
210 return OptiXDenoiser::is_device_supported(denoise_device_info);
211#endif
212#ifdef WITH_OPENIMAGEDENOISE
214 return OIDNDenoiserGPU::is_device_supported(denoise_device_info);
215#endif
216#ifdef WITH_DLSS
217 case DENOISER_DLSS:
218 return DLSSDenoiser::is_device_supported(denoise_device_info);
219#endif
220 default:
221 return false;
222 }
223}
224
226{
227#ifdef WITH_OPENIMAGEDENOISE
228 if (denoise_device_info.type != DEVICE_CPU &&
229 OIDNDenoiserGPU::is_device_supported(denoise_device_info))
230 {
232 }
233#else
234 (void)denoise_device_info;
235#endif
236
237#ifdef WITH_OPTIX
238 if (OptiXDenoiser::is_device_supported(denoise_device_info)) {
239 return DENOISER_OPTIX;
240 }
241#endif
242
243#ifdef WITH_OPENIMAGEDENOISE
244 if (openimagedenoise_supported()) {
246 }
247#endif
248
249 return DENOISER_NONE;
250}
251
252Denoiser::Denoiser(Device *denoiser_device, const DenoiseParams &params)
253 : denoiser_device_(denoiser_device), denoise_kernels_are_loaded_(false), params_(params)
254{
256 DCHECK(params.use);
257}
258
260{
261 DCHECK_EQ(params.type, params_.type);
262
263 if (params.type == params_.type) {
264 params_ = params;
265 }
266 else {
267 LOG_ERROR << "Attempt to change denoiser type.";
268 }
269}
270
272{
273 return params_;
274}
275
277{
278 /* If we have successfully loaded kernels once, then there is no need to repeat this again. */
281 }
282
283 if (progress) {
284 progress->set_status("Loading denoising kernels (may take a few minutes the first time)");
285 }
286
287 if (!denoiser_device_) {
288 set_error("No device available to denoise on");
289 return false;
290 }
291
292 /* Only need denoising feature, everything else is unused. */
294 string message = denoiser_device_->error_message();
295 if (message.empty()) {
296 message = "Failed loading denoising kernel, see console for errors";
297 }
298 set_error(message);
299 return false;
300 }
301
302 LOG_DEBUG << "Will denoise on " << denoiser_device_->info.description << " ("
303 << denoiser_device_->info.id << ")";
304
306 return true;
307}
308
313
float upscale_factor
Definition denoise.h:90
DenoiserType type
Definition denoise.h:73
bool use_gpu
Definition denoise.h:86
NODE_DECLARE bool use
Definition denoise.h:70
static unique_ptr< Denoiser > create(Device *denoiser_device, Device *cpu_fallback_device, const DenoiseParams &params, const GraphicsInteropDevice &interop_device)
Definition denoiser.cpp:163
void set_error(const string &error)
Definition denoiser.h:125
void set_params(const DenoiseParams &params)
Definition denoiser.cpp:259
static DenoiserType automatic_viewport_denoiser_type(const DeviceInfo &denoise_device_info)
Definition denoiser.cpp:225
DenoiseParams params_
Definition denoiser.h:135
Denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:252
virtual bool load_kernels(Progress *progress)
Definition denoiser.cpp:276
bool denoise_kernels_are_loaded_
Definition denoiser.h:134
Device * get_denoiser_device() const
Definition denoiser.cpp:309
Device * denoiser_device_
Definition denoiser.h:133
const DenoiseParams & get_params() const
Definition denoiser.cpp:271
static bool is_device_supported(DenoiserType type, const DeviceInfo &denoise_device_info)
Definition denoiser.cpp:205
vector< DeviceInfo > multi_devices
DenoiserTypeMask denoisers
DeviceType type
string description
virtual const string & error_message()
virtual bool should_use_graphics_interop(const GraphicsInteropDevice &, const bool=false)
DeviceInfo info
virtual void foreach_device(const std::function< void(Device *)> &callback)
virtual bool load_kernels(const uint64_t)
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
DenoiserType
Definition denoise.h:11
@ DENOISER_NONE
Definition denoise.h:17
@ DENOISER_OPTIX
Definition denoise.h:12
@ DENOISER_OPENIMAGEDENOISE
Definition denoise.h:13
@ DENOISER_DLSS
Definition denoise.h:14
bool use_gpu_oidn_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:94
bool use_dlss_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:106
static Device * find_best_device(Device *device, const DenoiserType type, const GraphicsInteropDevice &interop_device)
Definition denoiser.cpp:47
bool use_optix_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:82
static CCL_NAMESPACE_BEGIN bool is_single_device(const Device *device)
Definition denoiser.cpp:27
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:118
bool use_gpu_oidn_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:94
bool use_dlss_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:106
bool use_optix_denoiser(Device *denoiser_device, const DenoiseParams &params)
Definition denoiser.cpp:82
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:118
#define KERNEL_FEATURE_DENOISING
#define CCL_NAMESPACE_END
@ DEVICE_MULTI
@ DEVICE_CPU
#define DCHECK(expression)
Definition log.h:135
#define DCHECK_EQ(a, b)
Definition log.h:144
#define LOG_DEBUG
Definition log.h:107
#define LOG_ERROR
Definition log.h:101