Blender V5.3
oidn.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#ifdef WITH_OPENIMAGEDENOISE
6
7# include <cstdint>
8
9# include "BLI_array.hh"
10# include "BLI_assert.hh"
11# include "BLI_span.hh"
12
13# include "GPU_platform.hh"
14
15# include "COM_context.hh"
16# include "COM_result.hh"
17# include "COM_utilities_oidn.hh"
18
19# include <OpenImageDenoise/oidn.hpp>
20
21namespace blender::compositor {
22
23static oidn::DeviceRef create_oidn_gpu_device(const Context &context)
24{
25
26 /* The compositor uses CPU execution and does not have an active GPU context or device, so let
27 * OIDN select the best device, which is typically the fastest. */
28 if (!context.use_gpu()) {
29 return oidn::newDevice(oidn::DeviceType::Default);
30 }
31
32 /* Try to select the device that is used by the currently active GPU context. First, try to
33 * select the device based on the device LUID. */
34 const Span<uint8_t> platform_luid = GPU_platform_luid();
35 const uint32_t platform_luid_node_mask = GPU_platform_luid_node_mask();
36 const int devices_count = oidn::getNumPhysicalDevices();
37 for (int i = 0; i < devices_count; i++) {
38 oidn::PhysicalDeviceRef physical_device(i);
39 if (!physical_device.get<bool>("luidSupported")) {
40 continue;
41 }
42
43 oidn::LUID luid = physical_device.get<oidn::LUID>("luid");
44 uint32_t luid_node_mask = physical_device.get<uint32_t>("nodeMask");
45 if (platform_luid == Span<uint8_t>(luid.bytes, sizeof(luid.bytes)) &&
46 platform_luid_node_mask == luid_node_mask)
47 {
48 return physical_device.newDevice();
49 }
50 }
51
52 /* If LUID matching was unsuccessful, try to match based on UUID. We rely on multiple selection
53 * methods because not all platforms support both UUID and LUID, but all platforms support either
54 * one of them. UUID supports all except MacOS Metal, while LUID only supports Windows and MacOS
55 * Metal. Note that we prefer LUID as a first match because UUID is unreliable in practice as
56 * some implementations report the same UUID for different devices in the same machine. */
57 const Span<uint8_t> platform_uuid = GPU_platform_uuid();
58 for (int i = 0; i < devices_count; i++) {
59 oidn::PhysicalDeviceRef physical_device(i);
60 if (!physical_device.get<bool>("uuidSupported")) {
61 continue;
62 }
63
64 oidn::UUID uuid = physical_device.get<oidn::UUID>("uuid");
65 if (platform_uuid == Span<uint8_t>(uuid.bytes, sizeof(uuid.bytes))) {
66 return physical_device.newDevice();
67 }
68 }
69
70 return oidn::newDevice(oidn::DeviceType::Default);
71}
72
73oidn::DeviceRef create_oidn_device(const Context &context)
74{
75 const eCompositorDenoiseDevice preferred_denoise_device = static_cast<eCompositorDenoiseDevice>(
76 context.get_render_data().compositor_denoise_device);
77
78 switch (preferred_denoise_device) {
80 return oidn::newDevice(oidn::DeviceType::CPU);
82 return create_oidn_gpu_device(context);
84 if (!context.use_gpu()) {
85 return oidn::newDevice(oidn::DeviceType::CPU);
86 }
87 else {
88 return create_oidn_gpu_device(context);
89 }
90 }
91
93 return oidn::newDevice(oidn::DeviceType::Default);
94}
95
96oidn::BufferRef create_oidn_buffer(const oidn::DeviceRef &device, Result &image)
97{
98 /* The device can access host-side data, so create a shared buffer that wraps the data. */
99 const bool can_access_host_memory = device.get<bool>("systemMemorySupported");
100 if (can_access_host_memory) {
101 /* OIDN does not have const pointer variant in the shared buffer API, so use a const_cast. */
102 return device.newBuffer(const_cast<void *>(image.cpu_data().data()), image.size_in_bytes());
103 }
104
105 /* Otherwise, create a device-only buffer and copy the data to it. */
106 oidn::BufferRef buffer = device.newBuffer(image.size_in_bytes(), oidn::Storage::Device);
107 buffer.write(0, image.size_in_bytes(), image.cpu_data().data());
108 return buffer;
109}
110
111} // namespace blender::compositor
112
113#endif
#define BLI_assert_unreachable()
#define image(slot, qualifiers, format)
int context(const bContext *C, const char *member, bContextDataResult *result)
uint32_t GPU_platform_luid_node_mask()
@ SCE_COMPOSITOR_DENOISE_DEVICE_CPU
@ SCE_COMPOSITOR_DENOISE_DEVICE_GPU
@ SCE_COMPOSITOR_DENOISE_DEVICE_AUTO
unsigned int uint32_t
Definition BKE_text.h:81
Span< uint8_t > GPU_platform_luid()
Span< uint8_t > GPU_platform_uuid()