Blender V5.3
denoiser_oidn_base.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_OPENIMAGEDENOISE
6
8
9# include "util/log.h"
10# include "util/path.h"
11
12# if OIDN_VERSION_MAJOR < 2
13# define oidnSetFilterInt oidnSetFilter1i
14# endif
15
17
18OIDNDenoiserBase::OIDNDenoiserBase(Denoiser *denoiser) : denoiser_(denoiser) {}
19
20OIDNFilter OIDNDenoiserBase::new_filter()
21{
22 OIDNFilter filter = oidnNewFilter(oidn_device_, "RT");
23 if (filter == nullptr) {
24 const char *error_message = nullptr;
25 const OIDNError err = oidnGetDeviceError(oidn_device_, &error_message);
26 if (OIDN_ERROR_NONE != err) {
27 LOG_ERROR << "OIDN error: " << error_message;
28 denoiser_->set_error(error_message);
29 }
30 }
31 else {
32# if OIDN_VERSION_MAJOR >= 2
33 switch (quality_) {
35# if OIDN_VERSION >= 20300
36 oidnSetFilterInt(filter, "quality", OIDN_QUALITY_FAST);
37 break;
38# endif
40 oidnSetFilterInt(filter, "quality", OIDN_QUALITY_BALANCED);
41 break;
43 default:
44 oidnSetFilterInt(filter, "quality", OIDN_QUALITY_HIGH);
45 }
46# endif
47
48 oidnSetFilterBool(filter, "srgb", false);
49
50 /* Set custom weights if available. */
51 if (!custom_weights_.empty()) {
52 oidnSetSharedFilterData(filter, "weights", custom_weights_.data(), custom_weights_.size());
53 }
54 }
55
56 return filter;
57}
58
59bool OIDNDenoiserBase::create_filters(DenoiserQuality quality, bool use_albedo, bool use_normal)
60{
61 quality_ = quality;
62
63 oidn_filter_ = new_filter();
64 if (oidn_filter_ == nullptr) {
65 return false;
66 }
67
68 oidnSetFilterBool(oidn_filter_, "hdr", true);
69
70 if (use_albedo) {
71 albedo_filter_ = new_filter();
72 if (albedo_filter_ == nullptr) {
73 return false;
74 }
75 }
76
77 if (use_normal) {
78 normal_filter_ = new_filter();
79 if (normal_filter_ == nullptr) {
80 return false;
81 }
82 }
83
84 /* OIDN denoiser handle was created with the requested number of input passes. */
85 use_pass_albedo_ = use_albedo;
86 use_pass_normal_ = use_normal;
87
88 /* OIDN denoiser has been created, but it needs configuration. */
89 is_configured_ = false;
90
91 return true;
92}
93
94void OIDNDenoiserBase::load_custom_weights()
95{
96 const char *custom_weight_path = getenv("CYCLES_OIDN_CUSTOM_WEIGHTS");
97 if (!custom_weight_path) {
98 return;
99 }
100
101 if (!path_read_binary(custom_weight_path, custom_weights_)) {
102 LOG_ERROR << "Failed to load custom OpenImageDenoise weights";
103 }
104}
105
106void OIDNDenoiserBase::release_all_resources()
107{
108 if (albedo_filter_) {
109 oidnReleaseFilter(albedo_filter_);
110 albedo_filter_ = nullptr;
111 }
112 if (normal_filter_) {
113 oidnReleaseFilter(normal_filter_);
114 normal_filter_ = nullptr;
115 }
116 if (oidn_filter_) {
117 oidnReleaseFilter(oidn_filter_);
118 oidn_filter_ = nullptr;
119 }
120 if (oidn_device_) {
121 oidnReleaseDevice(oidn_device_);
122 oidn_device_ = nullptr;
123 }
124
125 is_configured_ = false;
126 use_pass_albedo_ = false;
127 use_pass_normal_ = false;
128}
129
130bool OIDNDenoiserBase::denoise_configure_if_needed(int width, int height)
131{
132 const int2 size = make_int2(width, height);
133
134 if (is_configured_ && configured_size_.x == size.x && configured_size_.y == size.y) {
135 return true;
136 }
137
138 configured_size_ = size;
139 is_configured_ = true;
140 return true;
141}
142
144
145#endif /* WITH_OPENIMAGEDENOISE */
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
DenoiserQuality
Definition denoise.h:55
@ DENOISER_QUALITY_FAST
Definition denoise.h:58
@ DENOISER_QUALITY_BALANCED
Definition denoise.h:57
@ DENOISER_QUALITY_HIGH
Definition denoise.h:56
#define CCL_NAMESPACE_END
ccl_device_forceinline int2 make_int2(const int x, const int y)
#define LOG_ERROR
Definition log.h:101
bool use_normal(const VPaint &vp)
DO_INLINE void filter(lfVector *V, fmatrix3x3 *S)
bool path_read_binary(const string &path, vector< uint8_t > &binary)
Definition path.cpp:730