Blender V5.3
denoiser_oidn_gpu.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#if defined(WITH_OPENIMAGEDENOISE)
6
8
9# include "device/device.h"
11# include "device/queue.h"
12
13# include "session/buffers.h"
14
15# include "util/log.h"
16
17# if OIDN_VERSION_MAJOR < 2
18# define oidnSetFilterBool oidnSetFilter1b
19# define oidnSetFilterInt oidnSetFilter1i
20# define oidnExecuteFilterAsync oidnExecuteFilter
21# endif
22
24
25# if OIDN_VERSION < 20300
26static const char *oidn_device_type_to_string(const OIDNDeviceType type)
27{
28 switch (type) {
29 case OIDN_DEVICE_TYPE_DEFAULT:
30 return "DEFAULT";
31 case OIDN_DEVICE_TYPE_CPU:
32 return "CPU";
33
34 /* The initial GPU support was added in OIDN 2.0. */
35# if OIDN_VERSION_MAJOR >= 2
36 case OIDN_DEVICE_TYPE_SYCL:
37 return "SYCL";
38 case OIDN_DEVICE_TYPE_CUDA:
39 return "CUDA";
40 case OIDN_DEVICE_TYPE_HIP:
41 return "HIP";
42# endif
43
44 /* The Metal support was added in OIDN 2.2. */
45# if (OIDN_VERSION_MAJOR > 2) || ((OIDN_VERSION_MAJOR == 2) && (OIDN_VERSION_MINOR >= 2))
46 case OIDN_DEVICE_TYPE_METAL:
47 return "METAL";
48# endif
49 }
50 return "UNKNOWN";
51}
52# endif
53
54bool OIDNDenoiserGPU::is_device_supported(const DeviceInfo &device)
55{
56# if OIDN_VERSION >= 20300
57 if (device.type == DEVICE_MULTI) {
58 for (const DeviceInfo &multi_device : device.multi_devices) {
59 if (multi_device.type != DEVICE_CPU && multi_device.denoisers & DENOISER_OPENIMAGEDENOISE) {
60 return true;
61 }
62 }
63
64 return false;
65 }
66
68# else
69 if (device.type == DEVICE_MULTI) {
70 for (const DeviceInfo &multi_device : device.multi_devices) {
71 if (multi_device.type != DEVICE_CPU && is_device_supported(multi_device)) {
72 return true;
73 }
74 }
75
76 return false;
77 }
78
79 LOG_TRACE << "Checking device " << device.description << " (" << device.id
80 << ") for OIDN GPU support";
81
82 int device_type = OIDN_DEVICE_TYPE_DEFAULT;
83 switch (device.type) {
84# ifdef OIDN_DEVICE_SYCL
85 case DEVICE_ONEAPI:
86 device_type = OIDN_DEVICE_TYPE_SYCL;
87 break;
88# endif
89# ifdef OIDN_DEVICE_HIP
90 case DEVICE_HIP:
91 device_type = OIDN_DEVICE_TYPE_HIP;
92 break;
93# endif
94# ifdef OIDN_DEVICE_CUDA
95 case DEVICE_CUDA:
96 case DEVICE_OPTIX:
97 device_type = OIDN_DEVICE_TYPE_CUDA;
98 break;
99# endif
100# ifdef OIDN_DEVICE_METAL
101 case DEVICE_METAL: {
102 const int num_devices = oidnGetNumPhysicalDevices();
103 LOG_TRACE << "Found " << num_devices << " OIDN device(s)";
104 for (int i = 0; i < num_devices; i++) {
105 const int type = oidnGetPhysicalDeviceInt(i, "type");
106 const char *name = oidnGetPhysicalDeviceString(i, "name");
107 LOG_TRACE << "OIDN device " << i << ": name=\"" << name
108 << "\", type=" << oidn_device_type_to_string(OIDNDeviceType(type));
109 if (type == OIDN_DEVICE_TYPE_METAL) {
110 if (device.id.find(name) != std::string::npos) {
111 LOG_TRACE << "OIDN device name matches the Cycles device name";
112 return true;
113 }
114 }
115 }
116 LOG_TRACE << "No matched OIDN device found";
117 return false;
118 }
119# endif
120 case DEVICE_CPU:
121 /* This is the GPU denoiser - CPU devices shouldn't end up here. */
122 assert(0);
123 default:
124 return false;
125 }
126
127 /* Match GPUs by their PCI ID. */
128 const int num_devices = oidnGetNumPhysicalDevices();
129 LOG_TRACE << "Found " << num_devices << " OIDN device(s)";
130 for (int i = 0; i < num_devices; i++) {
131 const int type = oidnGetPhysicalDeviceInt(i, "type");
132 const char *name = oidnGetPhysicalDeviceString(i, "name");
133 LOG_TRACE << "OIDN device " << i << ": name=\"" << name
134 << "\" type=" << oidn_device_type_to_string(OIDNDeviceType(type));
135 if (type == device_type) {
136 if (oidnGetPhysicalDeviceBool(i, "pciAddressSupported")) {
137 unsigned int pci_domain = oidnGetPhysicalDeviceInt(i, "pciDomain");
138 unsigned int pci_bus = oidnGetPhysicalDeviceInt(i, "pciBus");
139 unsigned int pci_device = oidnGetPhysicalDeviceInt(i, "pciDevice");
140 string pci_id = string_printf("%04x:%02x:%02x", pci_domain, pci_bus, pci_device);
141 LOG_INFO << "OIDN device PCI-e identifier: " << pci_id;
142 if (device.id.find(pci_id) != string::npos) {
143 LOG_TRACE << "OIDN device PCI-e identifier matches the Cycles device ID";
144 return true;
145 }
146 }
147 else {
148 LOG_TRACE << "Device does not support pciAddressSupported";
149 }
150 }
151 }
152 LOG_TRACE << "No matched OIDN device found";
153 return false;
154# endif
155}
156
157OIDNDenoiserGPU::OIDNDenoiserGPU(Device *denoiser_device, const DenoiseParams &params)
158 : DenoiserGPU(denoiser_device, params), base_(this)
159{
161}
162
163bool OIDNDenoiserGPU::commit_and_execute_filter(OIDNFilter filter, ExecMode mode)
164{
165 const char *error_message = nullptr;
166 OIDNError err = OIDN_ERROR_NONE;
167
168 for (;;) {
169 oidnCommitFilter(filter);
170 if (mode == ExecMode::ASYNC) {
171 oidnExecuteFilterAsync(filter);
172 }
173 else {
174 oidnExecuteFilter(filter);
175 }
176
177 /* If OIDN runs out of memory, reduce mem limit and retry */
178 err = oidnGetDeviceError(base_.oidn_device_, &error_message);
179 if (err != OIDN_ERROR_OUT_OF_MEMORY || max_mem_ < 200) {
180 break;
181 }
182 max_mem_ = max_mem_ / 2;
183 oidnSetFilterInt(filter, "maxMemoryMB", max_mem_);
184 }
185
186 if (err != OIDN_ERROR_NONE) {
187 if (error_message == nullptr) {
188 error_message = "Unspecified OIDN error";
189 }
190 LOG_ERROR << "OIDN error: " << error_message;
191 set_error(error_message);
192 return false;
193 }
194 return true;
195}
196
197bool OIDNDenoiserGPU::denoise_create_if_needed(DenoiseContext &context)
198{
199 const bool use_pass_albedo = (context.denoise_params.passes & DENOISER_PASS_ALBEDO) != 0;
200 const bool use_pass_normal = (context.denoise_params.passes & DENOISER_PASS_NORMAL) != 0;
201
202 const bool recreate_denoiser = (base_.oidn_device_ == nullptr) ||
203 (base_.oidn_filter_ == nullptr) ||
204 (base_.use_pass_albedo_ != use_pass_albedo) ||
205 (base_.use_pass_normal_ != use_pass_normal) ||
206 (base_.quality_ != params_.quality);
207 if (!recreate_denoiser) {
208 return true;
209 }
210
211 /* Destroy existing handles before creating new ones. */
212 base_.release_all_resources();
213
214 switch (denoiser_device_->info.type) {
215# if defined(OIDN_DEVICE_SYCL) && defined(WITH_ONEAPI)
216 case DEVICE_ONEAPI:
217 base_.oidn_device_ = oidnNewSYCLDevice(
218 (const sycl::queue *)reinterpret_cast<OneapiDevice *>(denoiser_device_)->sycl_queue(),
219 1);
220 break;
221# endif
222# if defined(OIDN_DEVICE_METAL) && defined(WITH_METAL)
223 case DEVICE_METAL: {
224 denoiser_queue_->init_execution();
225 const MTLCommandQueue_id queue = (const MTLCommandQueue_id)denoiser_queue_->native_queue();
226 base_.oidn_device_ = oidnNewMetalDevice(&queue, 1);
227 } break;
228# endif
229# if defined(OIDN_DEVICE_CUDA) && defined(WITH_CUDA)
230 case DEVICE_CUDA:
231 case DEVICE_OPTIX: {
232 /* Directly using the stream from the DeviceQueue returns "invalid resource handle". */
233 cudaStream_t stream = nullptr;
234 base_.oidn_device_ = oidnNewCUDADevice(&denoiser_device_->info.num, &stream, 1);
235 break;
236 }
237# endif
238# if defined(OIDN_DEVICE_HIP) && defined(WITH_HIP)
239 case DEVICE_HIP: {
240 hipStream_t stream = nullptr;
241 base_.oidn_device_ = oidnNewHIPDevice(&denoiser_device_->info.num, &stream, 1);
242 break;
243 }
244# endif
245 default:
246 break;
247 }
248
249 if (!base_.oidn_device_) {
250 set_error("Failed to create OIDN device");
251 return false;
252 }
253
254 if (denoiser_queue_) {
255 denoiser_queue_->init_execution();
256 }
257
258 oidnCommitDevice(base_.oidn_device_);
259 base_.load_custom_weights();
260 return base_.create_filters(params_.quality, use_pass_albedo, use_pass_normal);
261}
262
263bool OIDNDenoiserGPU::denoise_configure_if_needed(DenoiseContext &context)
264{
265 /* Limit maximum tile size denoiser can be invoked with. */
266 return base_.denoise_configure_if_needed(context.buffer_params.width,
267 context.buffer_params.height);
268}
269
270bool OIDNDenoiserGPU::denoise_run(const DenoiseContext &context, const DenoisePass &pass)
271{
272 /* Color pass. */
273 const int64_t pass_stride_in_bytes = context.buffer_params.pass_stride * sizeof(float);
274
275 set_filter_pass(base_.oidn_filter_,
276 "color",
277 context.render_buffers->buffer.device_pointer,
278 OIDN_FORMAT_FLOAT3,
279 context.buffer_params.width,
280 context.buffer_params.height,
281 pass.denoised_offset * sizeof(float),
282 pass_stride_in_bytes,
283 pass_stride_in_bytes * context.buffer_params.stride);
284
285 set_filter_pass(base_.oidn_filter_,
286 "output",
287 context.render_buffers->buffer.device_pointer,
288 OIDN_FORMAT_FLOAT3,
289 context.buffer_params.width,
290 context.buffer_params.height,
291 pass.denoised_offset * sizeof(float),
292 pass_stride_in_bytes,
293 pass_stride_in_bytes * context.buffer_params.stride);
294
295 /* Optional albedo and color passes. */
296 const device_ptr d_guiding_buffer = context.guiding_params.device_pointer;
297 const int64_t pixel_stride_in_bytes = context.guiding_params.pass_stride * sizeof(float);
298 const int64_t row_stride_in_bytes = context.guiding_params.stride * pixel_stride_in_bytes;
299
300 if (base_.use_pass_albedo_) {
301 set_filter_pass(base_.oidn_filter_,
302 "albedo",
303 d_guiding_buffer,
304 OIDN_FORMAT_FLOAT3,
305 context.buffer_params.width,
306 context.buffer_params.height,
307 context.guiding_params.pass_albedo * sizeof(float),
308 pixel_stride_in_bytes,
309 row_stride_in_bytes);
310
311 if (params_.prefilter == DENOISER_PREFILTER_ACCURATE) {
312 set_filter_pass(base_.albedo_filter_,
313 "albedo",
314 d_guiding_buffer,
315 OIDN_FORMAT_FLOAT3,
316 context.buffer_params.width,
317 context.buffer_params.height,
318 context.guiding_params.pass_albedo * sizeof(float),
319 pixel_stride_in_bytes,
320 row_stride_in_bytes);
321
322 set_filter_pass(base_.albedo_filter_,
323 "output",
324 d_guiding_buffer,
325 OIDN_FORMAT_FLOAT3,
326 context.buffer_params.width,
327 context.buffer_params.height,
328 context.guiding_params.pass_albedo * sizeof(float),
329 pixel_stride_in_bytes,
330 row_stride_in_bytes);
331
332 if (!commit_and_execute_filter(base_.albedo_filter_, ExecMode::ASYNC)) {
333 return false;
334 }
335 }
336 }
337
338 if (base_.use_pass_normal_) {
339 set_filter_pass(base_.oidn_filter_,
340 "normal",
341 d_guiding_buffer,
342 OIDN_FORMAT_FLOAT3,
343 context.buffer_params.width,
344 context.buffer_params.height,
345 context.guiding_params.pass_normal * sizeof(float),
346 pixel_stride_in_bytes,
347 row_stride_in_bytes);
348
349 if (params_.prefilter == DENOISER_PREFILTER_ACCURATE) {
350 set_filter_pass(base_.normal_filter_,
351 "normal",
352 d_guiding_buffer,
353 OIDN_FORMAT_FLOAT3,
354 context.buffer_params.width,
355 context.buffer_params.height,
356 context.guiding_params.pass_normal * sizeof(float),
357 pixel_stride_in_bytes,
358 row_stride_in_bytes);
359
360 set_filter_pass(base_.normal_filter_,
361 "output",
362 d_guiding_buffer,
363 OIDN_FORMAT_FLOAT3,
364 context.buffer_params.width,
365 context.buffer_params.height,
366 context.guiding_params.pass_normal * sizeof(float),
367 pixel_stride_in_bytes,
368 row_stride_in_bytes);
369
370 if (!commit_and_execute_filter(base_.normal_filter_, ExecMode::ASYNC)) {
371 return false;
372 }
373 }
374 }
375
376 oidnSetFilterInt(base_.oidn_filter_, "cleanAux", params_.prefilter != DENOISER_PREFILTER_FAST);
377 return commit_and_execute_filter(base_.oidn_filter_);
378}
379
380void OIDNDenoiserGPU::set_filter_pass(OIDNFilter filter,
381 const char *name,
382 device_ptr ptr,
383 const int format,
384 const int width,
385 const int height,
386 const size_t offset_in_bytes,
387 const size_t pixel_stride_in_bytes,
388 const size_t row_stride_in_bytes)
389{
390# if defined(OIDN_DEVICE_METAL) && defined(WITH_METAL)
391 if (denoiser_device_->info.type == DEVICE_METAL) {
392 void *mtl_buffer = denoiser_device_->get_native_buffer(ptr);
393 OIDNBuffer oidn_buffer = oidnNewSharedBufferFromMetal(base_.oidn_device_, mtl_buffer);
394
395 oidnSetFilterImage(filter,
396 name,
397 oidn_buffer,
398 (OIDNFormat)format,
399 width,
400 height,
401 offset_in_bytes,
402 pixel_stride_in_bytes,
403 row_stride_in_bytes);
404
405 oidnReleaseBuffer(oidn_buffer);
406 }
407 else
408# endif
409 {
410 oidnSetSharedFilterImage(filter,
411 name,
412 (void *)ptr,
413 (OIDNFormat)format,
414 width,
415 height,
416 offset_in_bytes,
417 pixel_stride_in_bytes,
418 row_stride_in_bytes);
419 }
420}
421
423
424#endif
long long int int64_t
DenoiserType type
Definition denoise.h:73
vector< DeviceInfo > multi_devices
DenoiserTypeMask denoisers
DeviceType type
string description
@ DENOISER_PREFILTER_FAST
Definition denoise.h:46
@ DENOISER_PREFILTER_ACCURATE
Definition denoise.h:50
@ DENOISER_PASS_NORMAL
Definition denoise.h:30
@ DENOISER_PASS_ALBEDO
Definition denoise.h:28
@ DENOISER_OPENIMAGEDENOISE
Definition denoise.h:13
#define CCL_NAMESPACE_END
@ DEVICE_METAL
@ DEVICE_MULTI
@ DEVICE_CUDA
@ DEVICE_CPU
@ DEVICE_OPTIX
@ DEVICE_HIP
@ DEVICE_ONEAPI
#define assert(assertion)
#define DCHECK_EQ(a, b)
Definition log.h:144
#define LOG_ERROR
Definition log.h:101
#define LOG_INFO
Definition log.h:106
#define LOG_TRACE
Definition log.h:108
int context(const bContext *C, const char *member, bContextDataResult *result)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
uint64_t device_ptr
Definition types_base.h:44