Blender  V3.3
denoising.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __DENOISING_H__
5 #define __DENOISING_H__
6 
7 /* TODO(sergey): Make it explicit and clear when something is a denoiser, its pipeline or
8  * parameters. Currently it is an annoying mixture of terms used interchangeably. */
9 
10 #include "device/device.h"
11 #include "integrator/denoiser.h"
12 
13 #include "util/string.h"
14 #include "util/unique_ptr.h"
15 #include "util/vector.h"
16 
17 #include <OpenImageIO/imageio.h>
18 
19 OIIO_NAMESPACE_USING
20 
22 
23 /* Denoiser pipeline */
24 
26  public:
27  DenoiserPipeline(DeviceInfo &device_info, const DenoiseParams &params);
29 
30  bool run();
31 
32  /* Error message after running, in case of failure. */
33  string error;
34 
35  /* Sequential list of frame filepaths to denoise. */
37  /* Sequential list of frame filepaths to write result to. Empty entries
38  * are skipped, so only a subset of the sequence can be denoised while
39  * taking into account all input frames. */
41 
42  protected:
43  friend class DenoiseTask;
44 
48  std::unique_ptr<Denoiser> denoiser;
49 };
50 
51 /* Denoise Image Layer */
52 
54  string name;
55  /* All channels belonging to this DenoiseImageLayer. */
57  /* Layer to image channel mapping. */
59 
60  /* Sample amount that was used for rendering this layer. */
61  int samples;
62 
63  /* Device input channel will be copied from image channel input_to_image_channel[i]. */
65 
66  /* Write i-th channel of the processing output to output_to_image_channel[i]-th channel of the
67  * file. */
69 
70  /* output_to_image_channel of the previous frame, if used. */
72 
73  /* Detect whether this layer contains a full set of channels and set up the offsets accordingly.
74  */
76 
77  /* Map the channels of a secondary frame to the channels that are required for processing,
78  * fill neighbor_input_to_image_channel if all are present or return false if a channel are
79  * missing. */
80  bool match_channels(const std::vector<string> &channelnames,
81  const std::vector<string> &neighbor_channelnames);
82 };
83 
84 /* Denoise Image Data */
85 
86 class DenoiseImage {
87  public:
88  DenoiseImage();
89  ~DenoiseImage();
90 
91  /* Dimensions */
93 
94  /* Samples */
95  int samples;
96 
97  /* Pixel buffer with interleaved channels. */
99 
100  /* Image file handles */
101  ImageSpec in_spec;
102  unique_ptr<ImageInput> in_previous;
103 
104  /* Render layers */
106 
107  void free();
108 
109  /* Open the input image, parse its channels, open the output image and allocate the output
110  * buffer. */
111  bool load(const string &in_filepath, string &error);
112 
113  /* Load neighboring frames. */
114  bool load_previous(const string &in_filepath, string &error);
115 
116  /* Load subset of pixels from file buffer into input buffer, as needed for denoising
117  * on the device. Channels are reshuffled following the provided mapping. */
118  void read_pixels(const DenoiseImageLayer &layer,
119  const BufferParams &params,
120  float *input_pixels);
121  bool read_previous_pixels(const DenoiseImageLayer &layer,
122  const BufferParams &params,
123  float *input_pixels);
124 
125  bool save_output(const string &out_filepath, string &error);
126 
127  protected:
128  /* Parse input file channels, separate them into DenoiseImageLayers,
129  * detect DenoiseImageLayers with full channel sets,
130  * fill layers and set up the output channels and passthrough map. */
131  bool parse_channels(const ImageSpec &in_spec, string &error);
132 
133  void close_input();
134 };
135 
136 /* Denoise Task */
137 
138 class DenoiseTask {
139  public:
141  ~DenoiseTask();
142 
143  /* Task stages */
144  bool load();
145  bool exec();
146  bool save();
147  void free();
148 
149  string error;
150 
151  protected:
152  /* Denoiser parameters and device */
155 
156  /* Frame number to be denoised */
157  int frame;
158 
159  /* Image file data */
162 
164 
165  /* Task handling */
166  bool load_input_pixels(int layer);
167 };
168 
170 
171 #endif /* __DENOISING_H__ */
void read_pixels(const DenoiseImageLayer &layer, const BufferParams &params, float *input_pixels)
Definition: denoising.cpp:400
bool parse_channels(const ImageSpec &in_spec, string &error)
Definition: denoising.cpp:339
void close_input()
Definition: denoising.cpp:328
array< float > pixels
Definition: denoising.h:98
vector< DenoiseImageLayer > layers
Definition: denoising.h:105
bool read_previous_pixels(const DenoiseImageLayer &layer, const BufferParams &params, float *input_pixels)
Definition: denoising.cpp:436
int num_channels
Definition: denoising.h:92
bool load(const string &in_filepath, string &error)
Definition: denoising.cpp:462
unique_ptr< ImageInput > in_previous
Definition: denoising.h:102
bool load_previous(const string &in_filepath, string &error)
Definition: denoising.cpp:502
bool save_output(const string &out_filepath, string &error)
Definition: denoising.cpp:533
ImageSpec in_spec
Definition: denoising.h:101
DenoiserPipeline * denoiser
Definition: denoising.h:153
RenderBuffers buffers
Definition: denoising.h:163
bool load()
Definition: denoising.cpp:206
string error
Definition: denoising.h:149
DenoiseTask(Device *device, DenoiserPipeline *denoiser, int frame)
Definition: denoising.cpp:163
Device * device
Definition: denoising.h:154
bool exec()
Definition: denoising.cpp:262
bool save()
Definition: denoising.cpp:300
bool load_input_pixels(int layer)
Definition: denoising.cpp:175
int current_layer
Definition: denoising.h:161
void free()
Definition: denoising.cpp:307
DenoiseImage image
Definition: denoising.h:160
std::unique_ptr< Denoiser > denoiser
Definition: denoising.h:48
DenoiserPipeline(DeviceInfo &device_info, const DenoiseParams &params)
Definition: denoising.cpp:597
Device * device
Definition: denoising.h:47
vector< string > input
Definition: denoising.h:36
Profiler profiler
Definition: denoising.h:46
vector< string > output
Definition: denoising.h:40
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void error(const char *str)
Definition: meshlaplacian.c:51
vector< int > previous_output_to_image_channel
Definition: denoising.h:71
vector< int > input_to_image_channel
Definition: denoising.h:64
bool match_channels(const std::vector< string > &channelnames, const std::vector< string > &neighbor_channelnames)
Definition: denoising.cpp:138
bool detect_denoising_channels()
Definition: denoising.cpp:95
vector< int > output_to_image_channel
Definition: denoising.h:68
vector< string > channels
Definition: denoising.h:56
vector< int > layer_to_image_channel
Definition: denoising.h:58