HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oidn.hpp
Go to the documentation of this file.
1 // Copyright 2018 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include "oidn.h"
7 #include <cstdint>
8 #include <cassert>
9 #include <cstring>
10 #include <algorithm>
11 #include <type_traits>
12 #include <vector>
13 #include <array>
14 #include <string>
15 
17 
18  // -----------------------------------------------------------------------------------------------
19  // Flags helper type
20  // -----------------------------------------------------------------------------------------------
21 
22  template<typename FlagT>
23  struct IsFlag
24  {
25  static constexpr bool value = false;
26  };
27 
28  template<typename FlagT>
29  class Flags
30  {
31  public:
32  static_assert(IsFlag<FlagT>::value, "not a flag type");
33 
35 
36  constexpr Flags() noexcept : mask(0) {}
37  constexpr Flags(FlagT flag) noexcept : mask(static_cast<MaskType>(flag)) {}
38  constexpr Flags(const Flags& b) noexcept = default;
39  constexpr explicit Flags(MaskType mask) noexcept : mask(mask) {}
40 
41  constexpr bool operator !() const noexcept { return !mask; }
42 
43  constexpr Flags operator &(const Flags& b) const noexcept { return Flags(mask & b.mask); }
44  constexpr Flags operator |(const Flags& b) const noexcept { return Flags(mask | b.mask); }
45  constexpr Flags operator ^(const Flags& b) const noexcept { return Flags(mask ^ b.mask); }
46 
47  Flags& operator =(const Flags& b) noexcept = default;
48 
49  Flags& operator &=(const Flags& b) noexcept
50  {
51  mask &= b.mask;
52  return *this;
53  }
54 
55  Flags& operator |=(const Flags& b) noexcept
56  {
57  mask |= b.mask;
58  return *this;
59  }
60 
61  Flags& operator ^=(const Flags& b) noexcept
62  {
63  mask ^= b.mask;
64  return *this;
65  }
66 
67  constexpr bool operator ==(const Flags& b) const noexcept { return mask == b.mask; }
68  constexpr bool operator !=(const Flags& b) const noexcept { return mask != b.mask; }
69 
70  constexpr explicit operator bool() const noexcept { return mask; }
71  constexpr explicit operator MaskType() const noexcept { return mask; }
72 
73  private:
74  MaskType mask;
75  };
76 
77  template<typename FlagT>
78  inline constexpr Flags<FlagT> operator &(FlagT a, const Flags<FlagT>& b) noexcept
79  {
80  return Flags<FlagT>(a) & b;
81  }
82 
83  template<typename FlagT>
84  inline constexpr Flags<FlagT> operator |(FlagT a, const Flags<FlagT>& b) noexcept
85  {
86  return Flags<FlagT>(a) | b;
87  }
88 
89  template<typename FlagT>
90  inline constexpr Flags<FlagT> operator ^(FlagT a, const Flags<FlagT>& b) noexcept
91  {
92  return Flags<FlagT>(a) ^ b;
93  }
94 
96  inline constexpr Flags<FlagT> operator &(FlagT a, FlagT b) noexcept
97  {
98  return Flags<FlagT>(a) & b;
99  }
100 
102  inline constexpr Flags<FlagT> operator |(FlagT a, FlagT b) noexcept
103  {
104  return Flags<FlagT>(a) | b;
105  }
106 
108  inline constexpr Flags<FlagT> operator ^(FlagT a, FlagT b) noexcept
109  {
110  return Flags<FlagT>(a) ^ b;
111  }
112 
113  // -----------------------------------------------------------------------------------------------
114  // Buffer
115  // -----------------------------------------------------------------------------------------------
116 
117  // Formats for images and other data stored in buffers
118  enum class Format
119  {
121 
122  // 32-bit single-precision floating-point scalar and vector formats
127 
128  // 16-bit half-precision floating-point scalar and vector formats
133  };
134 
135  // Storage modes for buffers
136  enum class Storage
137  {
139 
140  // stored on the host, accessible by both host and device
142 
143  // stored on the device, *not* accessible by the host
145 
146  // automatically migrated between host and device, accessible by both
147  // *not* supported by all devices, "managedMemorySupported" device parameter should be checked
149  };
150 
151  // External memory type flags
153  {
155 
156  // opaque POSIX file descriptor handle
158 
159  // file descriptor handle for a Linux dma_buf
161 
162  // NT handle
164 
165  // global share (KMT) handle
167 
168  // NT handle returned by IDXGIResource1::CreateSharedHandle referring to a Direct3D 11
169  // texture resource
171 
172  // global share (KMT) handle returned by IDXGIResource::GetSharedHandle referring to a
173  // Direct3D 11 texture resource
175 
176  // NT handle returned by IDXGIResource1::CreateSharedHandle referring to a Direct3D 11
177  // resource
179 
180  // global share (KMT) handle returned by IDXGIResource::GetSharedHandle referring to a
181  // Direct3D 11 resource
183 
184  // NT handle returned by ID3D12Device::CreateSharedHandle referring to a Direct3D 12
185  // heap resource
187 
188  // NT handle returned by ID3D12Device::CreateSharedHandle referring to a Direct3D 12
189  // committed resource
191  };
192 
193  template<> struct IsFlag<ExternalMemoryTypeFlag> { static constexpr bool value = true; };
195 
196  // Buffer object with automatic reference counting
197  class BufferRef
198  {
199  public:
200  BufferRef() : handle(nullptr) {}
201  BufferRef(OIDNBuffer handle) : handle(handle) {}
202 
203  BufferRef(const BufferRef& other) : handle(other.handle)
204  {
205  if (handle)
206  oidnRetainBuffer(handle);
207  }
208 
209  BufferRef(BufferRef&& other) noexcept : handle(other.handle)
210  {
211  other.handle = nullptr;
212  }
213 
215  {
216  if (&other != this)
217  {
218  if (other.handle)
219  oidnRetainBuffer(other.handle);
220  if (handle)
221  oidnReleaseBuffer(handle);
222  handle = other.handle;
223  }
224  return *this;
225  }
226 
227  BufferRef& operator =(BufferRef&& other) noexcept
228  {
229  std::swap(handle, other.handle);
230  return *this;
231  }
232 
234  {
235  if (other)
236  oidnRetainBuffer(other);
237  if (handle)
238  oidnReleaseBuffer(handle);
239  handle = other;
240  return *this;
241  }
242 
244  {
245  if (handle)
246  oidnReleaseBuffer(handle);
247  }
248 
250  {
251  return handle;
252  }
253 
254  operator bool() const
255  {
256  return handle != nullptr;
257  }
258 
259  // Releases the buffer (decrements the reference count).
260  void release()
261  {
262  if (handle)
263  {
264  oidnReleaseBuffer(handle);
265  handle = nullptr;
266  }
267  }
268 
269  // Gets the size of the buffer in bytes.
270  size_t getSize() const
271  {
272  return oidnGetBufferSize(handle);
273  }
274 
275  // Gets the storage mode of the buffer.
277  {
278  return static_cast<Storage>(oidnGetBufferStorage(handle));
279  }
280 
281  // Gets a pointer to the buffer data, which is accessible to the device but not necessarily to
282  // the host as well, depending on the storage mode. Null pointer may be returned if the buffer
283  // is empty or getting a pointer to data with device storage is not supported by the device.
284  void* getData() const
285  {
286  return oidnGetBufferData(handle);
287  }
288 
289  // Copies data from a region of the buffer to host memory.
290  void read(size_t byteOffset, size_t byteSize, void* dstHostPtr) const
291  {
292  oidnReadBuffer(handle, byteOffset, byteSize, dstHostPtr);
293  }
294 
295  // Copies data from a region of the buffer to host memory asynchronously.
296  void readAsync(size_t byteOffset, size_t byteSize, void* dstHostPtr) const
297  {
298  oidnReadBufferAsync(handle, byteOffset, byteSize, dstHostPtr);
299  }
300 
301  // Copies data to a region of the buffer from host memory.
302  void write(size_t byteOffset, size_t byteSize, const void* srcHostPtr)
303  {
304  oidnWriteBuffer(handle, byteOffset, byteSize, srcHostPtr);
305  }
306 
307  // Copies data to a region of the buffer from host memory asynchronously.
308  void writeAsync(size_t byteOffset, size_t byteSize, const void* srcHostPtr)
309  {
310  oidnWriteBufferAsync(handle, byteOffset, byteSize, srcHostPtr);
311  }
312 
313  private:
314  OIDNBuffer handle;
315  };
316 
317  // -----------------------------------------------------------------------------------------------
318  // Filter
319  // -----------------------------------------------------------------------------------------------
320 
321  // Filter quality/performance modes
322  enum class Quality
323  {
324  Default = OIDN_QUALITY_DEFAULT, // default quality
325 
326  Fast = OIDN_QUALITY_FAST, // high performance (for interactive/real-time preview rendering)
327  Balanced = OIDN_QUALITY_BALANCED, // balanced quality/performance (for interactive/real-time rendering)
328  High = OIDN_QUALITY_HIGH, // high quality (for final-frame rendering)
329  };
330 
331  // Progress monitor callback function
333 
334  // Filter object with automatic reference counting
335  class FilterRef
336  {
337  public:
338  FilterRef() : handle(nullptr) {}
339  FilterRef(OIDNFilter handle) : handle(handle) {}
340 
341  FilterRef(const FilterRef& other) : handle(other.handle)
342  {
343  if (handle)
344  oidnRetainFilter(handle);
345  }
346 
347  FilterRef(FilterRef&& other) noexcept : handle(other.handle)
348  {
349  other.handle = nullptr;
350  }
351 
353  {
354  if (&other != this)
355  {
356  if (other.handle)
357  oidnRetainFilter(other.handle);
358  if (handle)
359  oidnReleaseFilter(handle);
360  handle = other.handle;
361  }
362  return *this;
363  }
364 
365  FilterRef& operator =(FilterRef&& other) noexcept
366  {
367  std::swap(handle, other.handle);
368  return *this;
369  }
370 
372  {
373  if (other)
374  oidnRetainFilter(other);
375  if (handle)
376  oidnReleaseFilter(handle);
377  handle = other;
378  return *this;
379  }
380 
382  {
383  if (handle)
384  oidnReleaseFilter(handle);
385  }
386 
388  {
389  return handle;
390  }
391 
392  operator bool() const
393  {
394  return handle != nullptr;
395  }
396 
397  // Releases the filter (decrements the reference count).
398  void release()
399  {
400  if (handle)
401  {
402  oidnReleaseFilter(handle);
403  handle = nullptr;
404  }
405  }
406 
407  // Sets an image parameter of the filter with data stored in a buffer.
408  void setImage(const char* name,
409  const BufferRef& buffer, Format format,
410  size_t width, size_t height,
411  size_t byteOffset = 0,
412  size_t pixelByteStride = 0, size_t rowByteStride = 0)
413  {
414  oidnSetFilterImage(handle, name,
415  buffer.getHandle(), static_cast<OIDNFormat>(format),
416  width, height,
417  byteOffset,
418  pixelByteStride, rowByteStride);
419  }
420 
421  // Sets an image parameter of the filter with data owned by the user and accessible to the device.
422  void setImage(const char* name,
423  void* devPtr, Format format,
424  size_t width, size_t height,
425  size_t byteOffset = 0,
426  size_t pixelByteStride = 0, size_t rowByteStride = 0)
427  {
428  oidnSetSharedFilterImage(handle, name,
429  devPtr, static_cast<OIDNFormat>(format),
430  width, height,
431  byteOffset,
432  pixelByteStride, rowByteStride);
433  }
434 
435  // Unsets an image parameter of the filter that was previously set.
436  void unsetImage(const char* name)
437  {
438  oidnUnsetFilterImage(handle, name);
439  }
440 
441  OIDN_DEPRECATED("removeImage is deprecated. Use unsetImage instead.")
442  void removeImage(const char* name)
443  {
444  oidnUnsetFilterImage(handle, name);
445  }
446 
447  // Sets an opaque data parameter of the filter owned by the user and accessible to the host.
448  void setData(const char* name, void* hostPtr, size_t byteSize)
449  {
450  oidnSetSharedFilterData(handle, name, hostPtr, byteSize);
451  }
452 
453  // Notifies the filter that the contents of an opaque data parameter has been changed.
454  void updateData(const char* name)
455  {
456  oidnUpdateFilterData(handle, name);
457  }
458 
459  // Unsets an opaque data parameter of the filter that was previously set.
460  void unsetData(const char* name)
461  {
462  oidnUnsetFilterData(handle, name);
463  }
464 
465  OIDN_DEPRECATED("removeData is deprecated. Use unsetData instead.")
466  void removeData(const char* name)
467  {
468  oidnUnsetFilterData(handle, name);
469  }
470 
471  // Sets a boolean parameter of the filter.
472  void set(const char* name, bool value)
473  {
474  oidnSetFilterBool(handle, name, value);
475  }
476 
477  // Sets an integer parameter of the filter.
478  void set(const char* name, int value)
479  {
480  oidnSetFilterInt(handle, name, value);
481  }
482 
483  void set(const char* name, Quality value)
484  {
485  oidnSetFilterInt(handle, name, static_cast<int>(value));
486  }
487 
488  // Sets a float parameter of the filter.
489  void set(const char* name, float value)
490  {
491  oidnSetFilterFloat(handle, name, value);
492  }
493 
494  // Gets a parameter of the filter.
495  template<typename T>
496  T get(const char* name) const;
497 
498  // Sets the progress monitor callback function of the filter.
500  {
501  oidnSetFilterProgressMonitorFunction(handle, func, userPtr);
502  }
503 
504  // Commits all previous changes to the filter.
505  void commit()
506  {
507  oidnCommitFilter(handle);
508  }
509 
510  // Executes the filter.
511  void execute()
512  {
513  oidnExecuteFilter(handle);
514  }
515 
516  // Executes the filter asynchronously.
518  {
519  oidnExecuteFilterAsync(handle);
520  }
521 
522  #if defined(OIDN_SYCL_HPP)
523  // Executes the filter of a SYCL device using the specified dependent events asynchronously, and
524  // optionally returns an event for completion.
525  sycl::event executeAsync(const std::vector<sycl::event>& depEvents)
526  {
527  sycl::event doneEvent;
528  oidnExecuteSYCLFilterAsync(handle, depEvents.data(), static_cast<int>(depEvents.size()), &doneEvent);
529  return doneEvent;
530  }
531  #endif
532 
533  private:
534  OIDNFilter handle;
535  };
536 
537  template<>
538  inline bool FilterRef::get(const char* name) const
539  {
540  return oidnGetFilterBool(handle, name);
541  }
542 
543  template<>
544  inline int FilterRef::get(const char* name) const
545  {
546  return oidnGetFilterInt(handle, name);
547  }
548 
549  template<>
550  inline Quality FilterRef::get(const char* name) const
551  {
552  return static_cast<Quality>(oidnGetFilterInt(handle, name));
553  }
554 
555  template<>
556  inline float FilterRef::get(const char* name) const
557  {
558  return oidnGetFilterFloat(handle, name);
559  }
560 
561  // -----------------------------------------------------------------------------------------------
562  // Device
563  // -----------------------------------------------------------------------------------------------
564 
565  // Device types
566  enum class DeviceType
567  {
568  Default = OIDN_DEVICE_TYPE_DEFAULT, // select device automatically
569 
570  CPU = OIDN_DEVICE_TYPE_CPU, // CPU device
571  SYCL = OIDN_DEVICE_TYPE_SYCL, // SYCL device
572  CUDA = OIDN_DEVICE_TYPE_CUDA, // CUDA device
573  HIP = OIDN_DEVICE_TYPE_HIP, // HIP device
574  Metal = OIDN_DEVICE_TYPE_METAL, // Metal device
575  };
576 
577  // Error codes
578  enum class Error
579  {
580  None = OIDN_ERROR_NONE, // no error occurred
581  Unknown = OIDN_ERROR_UNKNOWN, // an unknown error occurred
582  InvalidArgument = OIDN_ERROR_INVALID_ARGUMENT, // an invalid argument was specified
583  InvalidOperation = OIDN_ERROR_INVALID_OPERATION, // the operation is not allowed
584  OutOfMemory = OIDN_ERROR_OUT_OF_MEMORY, // not enough memory to execute the operation
585  UnsupportedHardware = OIDN_ERROR_UNSUPPORTED_HARDWARE, // the hardware (e.g. CPU) is not supported
586  Cancelled = OIDN_ERROR_CANCELLED, // the operation was cancelled by the user
587  };
588 
589  // Error callback function
590  typedef void (*ErrorFunction)(void* userPtr, Error code, const char* message);
591 
592  // Opaque universally unique identifier (UUID) of a physical device
593  struct UUID
594  {
596  };
597 
598  // Opaque locally unique identifier (LUID) of a physical device
599  struct LUID
600  {
601  union
602  {
603  struct
604  {
605  uint32_t low;
606  int32_t high;
607  };
609  };
610  };
611 
612  // Device object with automatic reference counting
613  class DeviceRef
614  {
615  public:
616  DeviceRef() : handle(nullptr) {}
617  DeviceRef(OIDNDevice handle) : handle(handle) {}
618 
619  DeviceRef(const DeviceRef& other) : handle(other.handle)
620  {
621  if (handle)
622  oidnRetainDevice(handle);
623  }
624 
625  DeviceRef(DeviceRef&& other) noexcept : handle(other.handle)
626  {
627  other.handle = nullptr;
628  }
629 
631  {
632  if (&other != this)
633  {
634  if (other.handle)
635  oidnRetainDevice(other.handle);
636  if (handle)
637  oidnReleaseDevice(handle);
638  handle = other.handle;
639  }
640  return *this;
641  }
642 
643  DeviceRef& operator =(DeviceRef&& other) noexcept
644  {
645  std::swap(handle, other.handle);
646  return *this;
647  }
648 
650  {
651  if (other)
652  oidnRetainDevice(other);
653  if (handle)
654  oidnReleaseDevice(handle);
655  handle = other;
656  return *this;
657  }
658 
660  {
661  if (handle)
662  oidnReleaseDevice(handle);
663  }
664 
666  {
667  return handle;
668  }
669 
670  operator bool() const
671  {
672  return handle != nullptr;
673  }
674 
675  // Releases the device (decrements the reference count).
676  void release()
677  {
678  if (handle)
679  {
680  oidnReleaseDevice(handle);
681  handle = nullptr;
682  }
683  }
684 
685  // Sets a boolean parameter of the device.
686  void set(const char* name, bool value)
687  {
688  oidnSetDeviceBool(handle, name, value);
689  }
690 
691  // Sets an integer parameter of the device.
692  void set(const char* name, int value)
693  {
694  oidnSetDeviceInt(handle, name, value);
695  }
696 
697  // Sets an unsigned integer parameter of the device.
698  void set(const char* name, unsigned int value)
699  {
700  oidnSetDeviceUInt(handle, name, value);
701  }
702 
703  // Gets a parameter of the device.
704  template<typename T>
705  T get(const char* name) const;
706 
707  // Sets the error callback function of the device.
708  void setErrorFunction(ErrorFunction func, void* userPtr = nullptr)
709  {
710  oidnSetDeviceErrorFunction(handle, reinterpret_cast<OIDNErrorFunction>(func), userPtr);
711  }
712 
713  // Returns the first unqueried error code and clears the stored error.
714  // Can be called for a null device as well to check for global errors (e.g. why a device
715  // creation or physical device query has failed.
717  {
718  return static_cast<Error>(oidnGetDeviceError(handle, nullptr));
719  }
720 
721  // Returns the first unqueried error code and string message, and clears the stored error.
722  // Can be called for a null device as well to check why a device creation failed.
723  Error getError(const char*& outMessage)
724  {
725  return static_cast<Error>(oidnGetDeviceError(handle, &outMessage));
726  }
727 
728  // Commits all previous changes to the device.
729  // Must be called before first using the device (e.g. creating filters).
730  void commit()
731  {
732  oidnCommitDevice(handle);
733  }
734 
735  // Waits for all asynchronous operations running on the device to complete.
736  void sync()
737  {
738  oidnSyncDevice(handle);
739  }
740 
741  // Creates a buffer accessible to both the host and device.
742  BufferRef newBuffer(size_t byteSize) const
743  {
744  return oidnNewBuffer(handle, byteSize);
745  }
746 
747  // Creates a buffer with the specified storage mode.
748  BufferRef newBuffer(size_t byteSize, Storage storage) const
749  {
750  return oidnNewBufferWithStorage(handle, byteSize, static_cast<OIDNStorage>(storage));
751  }
752 
753  // Creates a shared buffer from memory allocated and owned by the user and accessible to the
754  // device.
755  BufferRef newBuffer(void* ptr, size_t byteSize) const
756  {
757  return oidnNewSharedBuffer(handle, ptr, byteSize);
758  }
759 
760  // Creates a shared buffer by importing external memory from a POSIX file descriptor.
761  BufferRef newBuffer(ExternalMemoryTypeFlag fdType, int fd, size_t byteSize) const
762  {
764  handle, static_cast<OIDNExternalMemoryTypeFlag>(fdType), fd, byteSize);
765  }
766 
767  // Creates a shared buffer by importing external memory from a Win32 handle.
768  BufferRef newBuffer(ExternalMemoryTypeFlag handleType, void* handle, const void* name, size_t byteSize) const
769  {
771  this->handle, static_cast<OIDNExternalMemoryTypeFlag>(handleType), handle, name, byteSize);
772  }
773 
774  // Creates a shared buffer from a Metal buffer.
775  // Only buffers with shared or private storage and hazard tracking are supported.
776  #if defined(__OBJC__)
778  {
779  return oidnNewSharedBufferFromMetal(handle, buffer);
780  }
781  #endif
782 
783  // Creates a filter of the specified type (e.g. "RT").
784  FilterRef newFilter(const char* type) const
785  {
786  return oidnNewFilter(handle, type);
787  }
788 
789  private:
790  OIDNDevice handle;
791  };
792 
793  template<>
794  inline bool DeviceRef::get(const char* name) const
795  {
796  return oidnGetDeviceBool(handle, name);
797  }
798 
799  template<>
800  inline int DeviceRef::get(const char* name) const
801  {
802  return oidnGetDeviceInt(handle, name);
803  }
804 
805  template<>
806  inline unsigned int DeviceRef::get(const char* name) const
807  {
808  return oidnGetDeviceUInt(handle, name);
809  }
810 
811  template<>
812  inline DeviceType DeviceRef::get(const char* name) const
813  {
814  return static_cast<DeviceType>(oidnGetDeviceInt(handle, name));
815  }
816 
817  template<>
818  inline ExternalMemoryTypeFlags DeviceRef::get(const char* name) const
819  {
820  return ExternalMemoryTypeFlags(oidnGetDeviceInt(handle, name));
821  }
822 
823  // Returns the first unqueried per-thread global error code and clears the stored error.
824  inline Error getError()
825  {
826  return static_cast<Error>(oidnGetDeviceError(nullptr, nullptr));
827  }
828 
829  // Returns the first unqueried per-thread global error code and string message, and clears the
830  // stored error.
831  inline Error getError(const char*& outMessage)
832  {
833  return static_cast<Error>(oidnGetDeviceError(nullptr, &outMessage));
834  }
835 
836  // Returns whether the CPU device is supported.
837  inline bool isCPUDeviceSupported()
838  {
839  return oidnIsCPUDeviceSupported();
840  }
841 
842  // Returns whether the specified SYCL device is supported.
843 #if defined(OIDN_SYCL_HPP)
844  inline bool isSYCLDeviceSupported(const sycl::device& device)
845  {
846  return oidnIsSYCLDeviceSupported(&device);
847  }
848 #endif
849 
850  // Returns whether the specified CUDA device is supported.
851  inline bool isCUDADeviceSupported(int deviceID)
852  {
853  return oidnIsCUDADeviceSupported(deviceID);
854  }
855 
856  // Returns whether the specified HIP device is supported.
857  inline bool isHIPDeviceSupported(int deviceID)
858  {
859  return oidnIsHIPDeviceSupported(deviceID);
860  }
861 
862  // Returns whether the specified Metal device is supported.
864  {
865  return oidnIsMetalDeviceSupported(device);
866  }
867 
868  // Creates a device of the specified type.
870  {
871  return DeviceRef(oidnNewDevice(static_cast<OIDNDeviceType>(type)));
872  }
873 
874  // Creates a device from a physical device specified by its ID (0 to getNumPhysicalDevices()-1).
875  inline DeviceRef newDevice(int physicalDeviceID)
876  {
877  return DeviceRef(oidnNewDeviceByID(physicalDeviceID));
878  }
879 
880  // Creates a device from a physical device specified by its UUID.
881  inline DeviceRef newDevice(const UUID& uuid)
882  {
883  return DeviceRef(oidnNewDeviceByUUID(uuid.bytes));
884  }
885 
886  // Creates a device from a physical device specified by its LUID.
887  inline DeviceRef newDevice(const LUID& luid)
888  {
889  return DeviceRef(oidnNewDeviceByLUID(luid.bytes));
890  }
891 
892  // Creates a device from a physical device specified by its PCI address.
893  inline DeviceRef newDevice(int pciDomain, int pciBus, int pciDevice, int pciFunction)
894  {
895  return DeviceRef(oidnNewDeviceByPCIAddress(pciDomain, pciBus, pciDevice, pciFunction));
896  }
897 
898 #if defined(OIDN_SYCL_HPP)
899  // Creates a device from the specified SYCL queue.
900  inline DeviceRef newSYCLDevice(const sycl::queue& queue)
901  {
902  return DeviceRef(oidnNewSYCLDevice(&queue, 1));
903  }
904 
905  // Creates a device from the specified list of SYCL queues.
906  // The queues should belong to different SYCL sub-devices (Xe Stack/Tile) of the same SYCL
907  // root-device (GPU).
908  inline DeviceRef newSYCLDevice(const std::vector<sycl::queue>& queues)
909  {
910  return DeviceRef(oidnNewSYCLDevice(queues.data(), static_cast<int>(queues.size())));
911  }
912 #endif
913 
914  // Creates a device from the specified CUDA device ID and stream (null stream corresponds to the
915  // default stream).
917  {
918  return DeviceRef(oidnNewCUDADevice(&deviceID, &stream, 1));
919  }
920 
921  // Creates a device from the specified pairs of CUDA device IDs and streams (null stream
922  // corresponds to the default stream). Currently only one device ID/stream is supported.
923  inline DeviceRef newCUDADevice(const std::vector<int>& deviceIDs,
924  const std::vector<cudaStream_t>& streams)
925  {
926  assert(deviceIDs.size() == streams.size());
927  return DeviceRef(oidnNewCUDADevice(deviceIDs.data(), streams.data(),
928  static_cast<int>(streams.size())));
929  }
930 
931  // Creates a device from the specified HIP device ID and stream (null stream corresponds to the
932  // default stream).
933  inline DeviceRef newHIPDevice(int deviceID, hipStream_t stream)
934  {
935  return DeviceRef(oidnNewHIPDevice(&deviceID, &stream, 1));
936  }
937 
938  // Creates a device from the specified pairs of HIP device IDs and streams (null stream
939  // corresponds to the default stream). Currently only one device ID/stream is supported.
940  inline DeviceRef newHIPDevice(const std::vector<int>& deviceIDs,
941  const std::vector<hipStream_t>& streams)
942  {
943  assert(deviceIDs.size() == streams.size());
944  return DeviceRef(oidnNewHIPDevice(deviceIDs.data(), streams.data(),
945  static_cast<int>(streams.size())));
946  }
947 
948  // Creates a device from the specified Metal command queue.
950  {
951  return DeviceRef(oidnNewMetalDevice(&commandQueue, 1));
952  }
953 
954  // Creates a device from the specified list of Metal command queues.
955  // Currently only one queue is supported.
956  inline DeviceRef newMetalDevice(const std::vector<MTLCommandQueue_id>& commandQueues)
957  {
958  return DeviceRef(oidnNewMetalDevice(commandQueues.data(), static_cast<int>(commandQueues.size())));
959  }
960 
961  // -----------------------------------------------------------------------------------------------
962  // Physical Device
963  // -----------------------------------------------------------------------------------------------
964 
966  {
967  public:
969  PhysicalDeviceRef(int id) : id(id) {}
970 
972  {
973  id = other;
974  return *this;
975  }
976 
977  int getID() const
978  {
979  return id;
980  }
981 
982  operator bool() const
983  {
984  return id >= 0;
985  }
986 
987  // Gets a paramter of the physical device.
988  template<typename T>
989  T get(const char* name) const;
990 
991  // Gets an opaque data parameter of the physical device.
992  std::pair<const void*, size_t> getData(const char* name) const
993  {
994  size_t byteSize = 0;
995  const void* ptr = oidnGetPhysicalDeviceData(id, name, &byteSize);
996  return {ptr, byteSize};
997  }
998 
999  // Creates a device from the physical device.
1001  {
1002  return DeviceRef(oidnNewDeviceByID(id));
1003  }
1004 
1005  private:
1006  int id;
1007  };
1008 
1009  // Returns the number of supported physical devices.
1011  {
1012  return oidnGetNumPhysicalDevices();
1013  }
1014 
1015  template<>
1016  inline bool PhysicalDeviceRef::get(const char* name) const
1017  {
1018  return oidnGetPhysicalDeviceBool(id, name);
1019  }
1020 
1021  template<>
1022  inline int PhysicalDeviceRef::get(const char* name) const
1023  {
1024  return oidnGetPhysicalDeviceInt(id, name);
1025  }
1026 
1027  template<>
1028  inline unsigned int PhysicalDeviceRef::get(const char* name) const
1029  {
1030  return oidnGetPhysicalDeviceUInt(id, name);
1031  }
1032 
1033  template<>
1034  inline DeviceType PhysicalDeviceRef::get(const char* name) const
1035  {
1036  return static_cast<DeviceType>(oidnGetPhysicalDeviceInt(id, name));
1037  }
1038 
1039  template<>
1040  inline const char* PhysicalDeviceRef::get(const char* name) const
1041  {
1042  return oidnGetPhysicalDeviceString(id, name);
1043  }
1044 
1045  template<>
1046  inline std::string PhysicalDeviceRef::get(const char* name) const
1047  {
1048  const char* str = oidnGetPhysicalDeviceString(id, name);
1049  return str ? str : "";
1050  }
1051 
1052  template<>
1053  inline UUID PhysicalDeviceRef::get(const char* name) const
1054  {
1055  UUID uuid{};
1056  auto data = getData(name);
1057  if (data.first != nullptr)
1058  {
1059  if (data.second == sizeof(uuid.bytes))
1060  std::memcpy(uuid.bytes, data.first, sizeof(uuid.bytes));
1061  else
1062  getData(""); // invoke an error
1063  }
1064  return uuid;
1065  }
1066 
1067  template<>
1068  inline LUID PhysicalDeviceRef::get(const char* name) const
1069  {
1070  LUID luid{};
1071  auto data = getData(name);
1072  if (data.first != nullptr)
1073  {
1074  if (data.second == sizeof(luid.bytes))
1075  std::memcpy(luid.bytes, data.first, sizeof(luid.bytes));
1076  else
1077  getData(""); // invoke an error
1078  }
1079  return luid;
1080  }
1081 
void set(const char *name, int value)
Definition: oidn.hpp:692
GLuint GLuint stream
Definition: glcorearb.h:1832
DeviceRef newDevice()
Definition: oidn.hpp:1000
~FilterRef()
Definition: oidn.hpp:381
Error getError()
Definition: oidn.hpp:716
type
Definition: core.h:556
OIDNDevice getHandle() const
Definition: oidn.hpp:665
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
constexpr Flags(MaskType mask) noexcept
Definition: oidn.hpp:39
BufferRef newBuffer(ExternalMemoryTypeFlag fdType, int fd, size_t byteSize) const
Definition: oidn.hpp:761
constexpr bool operator==(const Flags &b) const noexcept
Definition: oidn.hpp:67
void unsetData(const char *name)
Definition: oidn.hpp:460
OIDN_API void oidnSetDeviceInt(OIDNDevice device, const char *name, int value)
Definition: oidn.hpp:599
OIDN_API OIDNDevice oidnNewCUDADevice(const int *deviceIDs, const cudaStream_t *streams, int numPairs)
OIDNFilter getHandle() const
Definition: oidn.hpp:387
OIDN_API void oidnExecuteFilterAsync(OIDNFilter filter)
BufferRef newBuffer(size_t byteSize) const
Definition: oidn.hpp:742
OIDN_API OIDNBuffer oidnNewSharedBufferFromWin32Handle(OIDNDevice device, OIDNExternalMemoryTypeFlag handleType, void *handle, const void *name, size_t byteSize)
OIDN_API void oidnExecuteFilter(OIDNFilter filter)
Flags & operator|=(const Flags &b) noexcept
Definition: oidn.hpp:55
OIDN_API void oidnReadBufferAsync(OIDNBuffer buffer, size_t byteOffset, size_t byteSize, void *dstHostPtr)
Flags & operator^=(const Flags &b) noexcept
Definition: oidn.hpp:61
Storage
Definition: oidn.hpp:136
constexpr Flags< FlagT > operator^(FlagT a, const Flags< FlagT > &b) noexcept
Definition: oidn.hpp:90
OIDN_API void oidnReleaseDevice(OIDNDevice device)
OIDN_API void oidnSetFilterImage(OIDNFilter filter, const char *name, OIDNBuffer buffer, OIDNFormat format, size_t width, size_t height, size_t byteOffset, size_t pixelByteStride, size_t rowByteStride)
OIDN_API void oidnReleaseFilter(OIDNFilter filter)
void
Definition: png.h:1083
uint8_t bytes[OIDN_LUID_SIZE]
Definition: oidn.hpp:608
void commit()
Definition: oidn.hpp:505
void setImage(const char *name, const BufferRef &buffer, Format format, size_t width, size_t height, size_t byteOffset=0, size_t pixelByteStride=0, size_t rowByteStride=0)
Definition: oidn.hpp:408
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Definition: UT_ArraySet.h:1699
DeviceRef()
Definition: oidn.hpp:616
uint32_t low
Definition: oidn.hpp:605
PhysicalDeviceRef & operator=(int other)
Definition: oidn.hpp:971
OIDN_API void oidnUnsetFilterImage(OIDNFilter filter, const char *name)
GLsizei const GLfloat * value
Definition: glcorearb.h:824
void readAsync(size_t byteOffset, size_t byteSize, void *dstHostPtr) const
Definition: oidn.hpp:296
OIDNFormat
Definition: oidn.h:239
void executeAsync()
Definition: oidn.hpp:517
OIDN_API bool oidnGetDeviceBool(OIDNDevice device, const char *name)
GLuint64 GLenum handleType
Definition: RE_OGL.h:262
OIDN_API void oidnSetSharedFilterImage(OIDNFilter filter, const char *name, void *devPtr, OIDNFormat format, size_t width, size_t height, size_t byteOffset, size_t pixelByteStride, size_t rowByteStride)
OIDN_API void oidnSetFilterInt(OIDNFilter filter, const char *name, int value)
OIDN_API void oidnUnsetFilterData(OIDNFilter filter, const char *name)
Flags & operator=(const Flags &b) noexcept=default
OIDN_API OIDNDevice oidnNewHIPDevice(const int *deviceIDs, const hipStream_t *streams, int numPairs)
OIDN_API int oidnGetFilterInt(OIDNFilter filter, const char *name)
T get(const char *name) const
OIDNProgressMonitorFunction ProgressMonitorFunction
Definition: oidn.hpp:332
void commit()
Definition: oidn.hpp:730
void setErrorFunction(ErrorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:708
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
constexpr Flags() noexcept
Definition: oidn.hpp:36
void writeAsync(size_t byteOffset, size_t byteSize, const void *srcHostPtr)
Definition: oidn.hpp:308
OIDN_API int oidnGetNumPhysicalDevices()
OIDN_API int oidnGetPhysicalDeviceInt(int physicalDeviceID, const char *name)
constexpr Flags(FlagT flag) noexcept
Definition: oidn.hpp:37
Storage getStorage() const
Definition: oidn.hpp:276
void release()
Definition: oidn.hpp:676
~BufferRef()
Definition: oidn.hpp:243
GLuint buffer
Definition: glcorearb.h:660
void * MTLDevice_id
Definition: oidn.h:40
BufferRef()
Definition: oidn.hpp:200
OIDN_API float oidnGetFilterFloat(OIDNFilter filter, const char *name)
struct ihipStream_t * hipStream_t
Definition: oidn.h:29
DeviceRef(const DeviceRef &other)
Definition: oidn.hpp:619
~DeviceRef()
Definition: oidn.hpp:659
Definition: oidn.hpp:593
struct OIDNFilterImpl * OIDNFilter
Definition: oidn.h:389
OutGridT const XformOp bool bool
void setProgressMonitorFunction(ProgressMonitorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:499
void write(size_t byteOffset, size_t byteSize, const void *srcHostPtr)
Definition: oidn.hpp:302
constexpr Flags operator|(const Flags &b) const noexcept
Definition: oidn.hpp:44
OIDN_API void oidnWriteBufferAsync(OIDNBuffer buffer, size_t byteOffset, size_t byteSize, const void *srcHostPtr)
Flags< ExternalMemoryTypeFlag > ExternalMemoryTypeFlags
Definition: oidn.hpp:194
FilterRef(OIDNFilter handle)
Definition: oidn.hpp:339
OIDN_API void oidnRetainFilter(OIDNFilter filter)
struct _cl_event * event
Definition: glcorearb.h:2961
BufferRef(OIDNBuffer handle)
Definition: oidn.hpp:201
OIDN_API void oidnRetainBuffer(OIDNBuffer buffer)
DeviceRef(OIDNDevice handle)
Definition: oidn.hpp:617
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
Format
Definition: oidn.hpp:118
struct OIDNBufferImpl * OIDNBuffer
Definition: oidn.h:314
Quality
Definition: oidn.hpp:322
constexpr Flags operator^(const Flags &b) const noexcept
Definition: oidn.hpp:45
void setImage(const char *name, void *devPtr, Format format, size_t width, size_t height, size_t byteOffset=0, size_t pixelByteStride=0, size_t rowByteStride=0)
Definition: oidn.hpp:422
OIDN_API bool oidnIsMetalDeviceSupported(MTLDevice_id device)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
DeviceRef(DeviceRef &&other) noexcept
Definition: oidn.hpp:625
BufferRef(BufferRef &&other) noexcept
Definition: oidn.hpp:209
void * getData() const
Definition: oidn.hpp:284
void updateData(const char *name)
Definition: oidn.hpp:454
OIDN_API bool oidnIsCPUDeviceSupported()
DeviceRef newHIPDevice(int deviceID, hipStream_t stream)
Definition: oidn.hpp:933
BufferRef & operator=(const BufferRef &other)
Definition: oidn.hpp:214
void setData(const char *name, void *hostPtr, size_t byteSize)
Definition: oidn.hpp:448
OIDN_API void oidnUpdateFilterData(OIDNFilter filter, const char *name)
DeviceRef newMetalDevice(MTLCommandQueue_id commandQueue)
Definition: oidn.hpp:949
constexpr Flags< FlagT > operator|(FlagT a, const Flags< FlagT > &b) noexcept
Definition: oidn.hpp:84
Definition: oidn.hpp:23
BufferRef newBuffer(void *ptr, size_t byteSize) const
Definition: oidn.hpp:755
OIDN_API void oidnWriteBuffer(OIDNBuffer buffer, size_t byteOffset, size_t byteSize, const void *srcHostPtr)
FilterRef & operator=(const FilterRef &other)
Definition: oidn.hpp:352
OIDN_API void oidnReleaseBuffer(OIDNBuffer buffer)
OIDNBuffer getHandle() const
Definition: oidn.hpp:249
void * MTLCommandQueue_id
Definition: oidn.h:41
OIDN_API void oidnRetainDevice(OIDNDevice device)
OIDN_API const char * oidnGetPhysicalDeviceString(int physicalDeviceID, const char *name)
void set(const char *name, float value)
Definition: oidn.hpp:489
#define OIDN_LUID_SIZE
Definition: oidn.h:52
void removeData(const char *name)
Definition: oidn.hpp:466
OIDN_API void oidnCommitFilter(OIDNFilter filter)
bool isCPUDeviceSupported()
Definition: oidn.hpp:837
OIDN_API void oidnSetFilterProgressMonitorFunction(OIDNFilter filter, OIDNProgressMonitorFunction func, void *userPtr)
OIDN_API void oidnSetFilterBool(OIDNFilter filter, const char *name, bool value)
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
FilterRef()
Definition: oidn.hpp:338
constexpr Flags< FlagT > operator&(FlagT a, const Flags< FlagT > &b) noexcept
Definition: oidn.hpp:78
FilterRef(FilterRef &&other) noexcept
Definition: oidn.hpp:347
GLint GLuint mask
Definition: glcorearb.h:124
OIDN_API OIDNBuffer oidnNewSharedBufferFromFD(OIDNDevice device, OIDNExternalMemoryTypeFlag fdType, int fd, size_t byteSize)
OIDN_API bool oidnGetPhysicalDeviceBool(int physicalDeviceID, const char *name)
void release()
Definition: oidn.hpp:398
OIDN_API void oidnCommitDevice(OIDNDevice device)
OIDN_API void * oidnGetBufferData(OIDNBuffer buffer)
#define OIDN_DEPRECATED(msg)
Definition: config.h:61
void set(const char *name, bool value)
Definition: oidn.hpp:686
OIDN_API OIDNBuffer oidnNewSharedBufferFromMetal(OIDNDevice device, MTLBuffer_id buffer)
Flags & operator&=(const Flags &b) noexcept
Definition: oidn.hpp:49
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the queue
Definition: thread.h:632
OIDN_API void oidnSyncDevice(OIDNDevice device)
OIDN_API OIDNBuffer oidnNewBuffer(OIDNDevice device, size_t byteSize)
OIDN_API bool oidnIsCUDADeviceSupported(int deviceID)
OIDN_API OIDNDevice oidnNewDevice(OIDNDeviceType type)
GLuint id
Definition: glcorearb.h:655
FilterRef(const FilterRef &other)
Definition: oidn.hpp:341
OIDN_API void oidnSetDeviceErrorFunction(OIDNDevice device, OIDNErrorFunction func, void *userPtr)
T get(const char *name) const
#define OIDN_NAMESPACE_BEGIN
Definition: config.h:35
void read(size_t byteOffset, size_t byteSize, void *dstHostPtr) const
Definition: oidn.hpp:290
OIDN_API OIDNStorage oidnGetBufferStorage(OIDNBuffer buffer)
GLuint const GLchar * name
Definition: glcorearb.h:786
std::pair< const void *, size_t > getData(const char *name) const
Definition: oidn.hpp:992
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
#define OIDN_UUID_SIZE
Definition: oidn.h:51
OIDN_API OIDNBuffer oidnNewSharedBuffer(OIDNDevice device, void *devPtr, size_t byteSize)
struct CUstream_st * cudaStream_t
Definition: oidn.h:28
OIDN_API OIDNDevice oidnNewMetalDevice(const MTLCommandQueue_id *commandQueues, int numQueues)
OIDN_API size_t oidnGetBufferSize(OIDNBuffer buffer)
PhysicalDeviceRef(int id)
Definition: oidn.hpp:969
constexpr Flags operator&(const Flags &b) const noexcept
Definition: oidn.hpp:43
void unsetImage(const char *name)
Definition: oidn.hpp:436
OIDN_API bool oidnIsHIPDeviceSupported(int deviceID)
Definition: oidn.hpp:29
BufferRef newBuffer(ExternalMemoryTypeFlag handleType, void *handle, const void *name, size_t byteSize) const
Definition: oidn.hpp:768
Error getError(const char *&outMessage)
Definition: oidn.hpp:723
int getID() const
Definition: oidn.hpp:977
bool isMetalDeviceSupported(MTLDevice_id device)
Definition: oidn.hpp:863
OIDN_API void oidnSetFilterFloat(OIDNFilter filter, const char *name, float value)
bool isCUDADeviceSupported(int deviceID)
Definition: oidn.hpp:851
Error getError()
Definition: oidn.hpp:824
OIDN_API void oidnSetSharedFilterData(OIDNFilter filter, const char *name, void *hostPtr, size_t byteSize)
OIDN_API OIDNDevice oidnNewDeviceByLUID(const void *luid)
DeviceRef newDevice(DeviceType type=DeviceType::Default)
Definition: oidn.hpp:869
void set(const char *name, int value)
Definition: oidn.hpp:478
FilterRef newFilter(const char *type) const
Definition: oidn.hpp:784
int32_t high
Definition: oidn.hpp:606
GLenum func
Definition: glcorearb.h:783
unsigned int oidnGetDeviceUInt(OIDNDevice device, const char *name)
Definition: oidn.h:207
Error
Definition: oidn.hpp:578
bool(* OIDNProgressMonitorFunction)(void *userPtr, double n)
Definition: oidn.h:386
void removeImage(const char *name)
Definition: oidn.hpp:442
DeviceType
Definition: oidn.hpp:566
void execute()
Definition: oidn.hpp:511
DeviceRef newCUDADevice(int deviceID, cudaStream_t stream)
Definition: oidn.hpp:916
struct OIDNDeviceImpl * OIDNDevice
Definition: oidn.h:108
OIDN_API const void * oidnGetPhysicalDeviceData(int physicalDeviceID, const char *name, size_t *byteSize)
OIDN_API bool oidnGetFilterBool(OIDNFilter filter, const char *name)
unsigned int oidnGetPhysicalDeviceUInt(int physicalDeviceID, const char *name)
Definition: oidn.h:64
OIDN_API void oidnReadBuffer(OIDNBuffer buffer, size_t byteOffset, size_t byteSize, void *dstHostPtr)
constexpr bool operator!() const noexcept
Definition: oidn.hpp:41
void release()
Definition: oidn.hpp:260
void set(const char *name, bool value)
Definition: oidn.hpp:472
auto ptr(T p) -> const void *
Definition: format.h:4331
void(* ErrorFunction)(void *userPtr, Error code, const char *message)
Definition: oidn.hpp:590
int getNumPhysicalDevices()
Definition: oidn.hpp:1010
OIDN_API OIDNDevice oidnNewDeviceByPCIAddress(int pciDomain, int pciBus, int pciDevice, int pciFunction)
GLint GLsizei width
Definition: glcorearb.h:103
BufferRef newBuffer(size_t byteSize, Storage storage) const
Definition: oidn.hpp:748
OIDN_API OIDNBuffer oidnNewBufferWithStorage(OIDNDevice device, size_t byteSize, OIDNStorage storage)
void set(const char *name, unsigned int value)
Definition: oidn.hpp:698
BufferRef(const BufferRef &other)
Definition: oidn.hpp:203
OIDN_API OIDNDevice oidnNewDeviceByID(int physicalDeviceID)
OIDN_API OIDNDevice oidnNewDeviceByUUID(const void *uuid)
OIDN_API OIDNFilter oidnNewFilter(OIDNDevice device, const char *type)
OIDN_API void oidnSetDeviceBool(OIDNDevice device, const char *name, bool value)
void sync()
Definition: oidn.hpp:736
void oidnSetDeviceUInt(OIDNDevice device, const char *name, unsigned int value)
Definition: oidn.h:189
void set(const char *name, Quality value)
Definition: oidn.hpp:483
T get(const char *name) const
bool isHIPDeviceSupported(int deviceID)
Definition: oidn.hpp:857
ExternalMemoryTypeFlag
Definition: oidn.hpp:152
uint8_t bytes[OIDN_UUID_SIZE]
Definition: oidn.hpp:595
size_t getSize() const
Definition: oidn.hpp:270
OIDN_API OIDNError oidnGetDeviceError(OIDNDevice device, const char **outMessage)
Definition: Types.h:101
GLuint64 GLenum GLint fd
Definition: RE_OGL.h:262
Definition: format.h:1821
Definition: format.h:4365
typename std::underlying_type< FlagT >::type MaskType
Definition: oidn.hpp:34
#define OIDN_NAMESPACE_END
Definition: config.h:36
constexpr bool operator!=(const Flags &b) const noexcept
Definition: oidn.hpp:68
DeviceRef & operator=(const DeviceRef &other)
Definition: oidn.hpp:630
OIDN_API int oidnGetDeviceInt(OIDNDevice device, const char *name)