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