Blender V5.3
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
28bool use_dlss_denoiser(Device *denoiser_device, const DenoiseParams &params);
29
31 Device *cpu_fallback_device,
32 const DenoiseParams &params,
33 const GraphicsInteropDevice &interop_device,
34 Device *&single_denoiser_device);
35
36/* Implementation of a specific denoising algorithm.
37 *
38 * This class takes care of breaking down denoising algorithm into a series of device calls or to
39 * calls of an external API to denoise given input.
40 *
41 * TODO(sergey): Are we better with device or a queue here? */
42class Denoiser {
43 public:
44 /* Create denoiser for the given path trace device.
45 *
46 * Notes:
47 * - The denoiser must be configured. This means that `params.use` must be true.
48 * This is checked in debug builds.
49 * - The device might be MultiDevice.
50 * - If Denoiser from params is not supported by provided denoise device, then Blender will
51 * fallback on the OIDN CPU denoising and use provided cpu_fallback_device.
52 * - Specifying the graphics interop device helps pick a more efficient denoising device.*/
53 static unique_ptr<Denoiser> create(Device *denoiser_device,
54 Device *cpu_fallback_device,
55 const DenoiseParams &params,
56 const GraphicsInteropDevice &interop_device);
57
58 virtual ~Denoiser() = default;
59
60 void set_params(const DenoiseParams &params);
61 const DenoiseParams &get_params() const;
62
63 static bool is_device_supported(DenoiserType type, const DeviceInfo &denoise_device_info);
64
65 /* Recommended type for viewport denoising. */
66 static DenoiserType automatic_viewport_denoiser_type(const DeviceInfo &denoise_device_info);
67
68 /* Create devices and load kernels needed for denoising.
69 * The progress is used to communicate state when kernels actually needs to be loaded.
70 *
71 * NOTE: The `progress` is an optional argument, can be nullptr. */
72 virtual bool load_kernels(Progress *progress);
73
74 /* Denoise the entire buffer.
75 *
76 * Buffer parameters denotes an effective parameters used during rendering. It could be
77 * a lower resolution render into a bigger allocated buffer, which is used in viewport during
78 * navigation and non-unit pixel size. Use that instead of render_buffers->params.
79 *
80 * The buffer might be coming from a "foreign" device from what this denoise is created for.
81 * This means that in general case the denoiser will make sure the input data is available on
82 * the denoiser device, perform denoising, and put data back to the device where the buffer
83 * came from.
84 *
85 * The `num_samples` corresponds to the number of samples in the render buffers. It is used
86 * to scale buffers down to the "final" value in algorithms which don't do automatic exposure,
87 * or which needs "final" value for data passes.
88 *
89 * The `allow_inplace_modification` means that the denoiser is allowed to do in-place
90 * modification of the input passes (scaling them down i.e.). This will lower the memory
91 * footprint of the denoiser but will make input passes "invalid" (from path tracer) point of
92 * view.
93 *
94 * Returns true when all passes are denoised. Will return false if there is a denoiser error (for
95 * example, caused by misconfigured denoiser) or when user requested to cancel rendering. */
96 virtual bool denoise_buffer(const BufferParams &buffer_params,
97 const BufferParams &denoised_buffer_params,
98 RenderBuffers *render_buffers,
99 int num_samples,
100 bool allow_inplace_modification,
101 float2 pixel_jitter = {}) = 0;
102
103 /* Get a device which is used to perform actual denoising.
104 *
105 * Notes:
106 *
107 * - The device can be different from the path tracing device. This happens, for example, when
108 * using OptiX denoiser and rendering on CPU.
109 *
110 * - No threading safety is ensured in this call. This means, that it is up to caller to ensure
111 * that there is no threading-conflict between denoising task lazily initializing the device
112 * and access to this device happen. */
114
115 std::function<bool(void)> is_cancelled_cb;
116
117 bool is_cancelled() const
118 {
119 if (!is_cancelled_cb) {
120 return false;
121 }
122 return is_cancelled_cb();
123 }
124
125 void set_error(const string &error)
126 {
128 }
129
130 protected:
131 Denoiser(Device *denoiser_device, const DenoiseParams &params);
132
136};
137
static unique_ptr< Denoiser > create(Device *denoiser_device, Device *cpu_fallback_device, const DenoiseParams &params, const GraphicsInteropDevice &interop_device)
Definition denoiser.cpp:163
virtual 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={})=0
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
bool is_cancelled() const
Definition denoiser.h:117
std::function< bool(void)> is_cancelled_cb
Definition denoiser.h:115
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
virtual ~Denoiser()=default
virtual void set_error(const string &error)
DenoiserType
Definition denoise.h:11
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 CCL_NAMESPACE_END