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)
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)
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)
239  handle = other;
240  return *this;
241  }
242 
244  {
245  if (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  {
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:
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  Balanced = OIDN_QUALITY_BALANCED, // balanced quality/performance (for interactive/real-time rendering)
327  High = OIDN_QUALITY_HIGH, // high quality (for final-frame rendering)
328  };
329 
330  // Progress monitor callback function
332 
333  // Filter object with automatic reference counting
334  class FilterRef
335  {
336  public:
337  FilterRef() : handle(nullptr) {}
338  FilterRef(OIDNFilter handle) : handle(handle) {}
339 
340  FilterRef(const FilterRef& other) : handle(other.handle)
341  {
342  if (handle)
344  }
345 
346  FilterRef(FilterRef&& other) noexcept : handle(other.handle)
347  {
348  other.handle = nullptr;
349  }
350 
352  {
353  if (&other != this)
354  {
355  if (other.handle)
356  oidnRetainFilter(other.handle);
357  if (handle)
359  handle = other.handle;
360  }
361  return *this;
362  }
363 
364  FilterRef& operator =(FilterRef&& other) noexcept
365  {
366  std::swap(handle, other.handle);
367  return *this;
368  }
369 
371  {
372  if (other)
373  oidnRetainFilter(other);
374  if (handle)
376  handle = other;
377  return *this;
378  }
379 
381  {
382  if (handle)
384  }
385 
387  {
388  return handle;
389  }
390 
391  operator bool() const
392  {
393  return handle != nullptr;
394  }
395 
396  // Releases the filter (decrements the reference count).
397  void release()
398  {
399  if (handle)
400  {
402  handle = nullptr;
403  }
404  }
405 
406  // Sets an image parameter of the filter with data stored in a buffer.
407  void setImage(const char* name,
408  const BufferRef& buffer, Format format,
409  size_t width, size_t height,
410  size_t byteOffset = 0,
411  size_t pixelByteStride = 0, size_t rowByteStride = 0)
412  {
414  buffer.getHandle(), static_cast<OIDNFormat>(format),
415  width, height,
416  byteOffset,
417  pixelByteStride, rowByteStride);
418  }
419 
420  // Sets an image parameter of the filter with data owned by the user and accessible to the device.
421  void setImage(const char* name,
422  void* devPtr, Format format,
423  size_t width, size_t height,
424  size_t byteOffset = 0,
425  size_t pixelByteStride = 0, size_t rowByteStride = 0)
426  {
428  devPtr, static_cast<OIDNFormat>(format),
429  width, height,
430  byteOffset,
431  pixelByteStride, rowByteStride);
432  }
433 
434  // Unsets an image parameter of the filter that was previously set.
435  void unsetImage(const char* name)
436  {
438  }
439 
440  OIDN_DEPRECATED("removeImage is deprecated. Use unsetImage instead.")
441  void removeImage(const char* name)
442  {
444  }
445 
446  // Sets an opaque data parameter of the filter owned by the user and accessible to the host.
447  void setData(const char* name, void* hostPtr, size_t byteSize)
448  {
449  oidnSetSharedFilterData(handle, name, hostPtr, byteSize);
450  }
451 
452  // Notifies the filter that the contents of an opaque data parameter has been changed.
453  void updateData(const char* name)
454  {
456  }
457 
458  // Unsets an opaque data parameter of the filter that was previously set.
459  void unsetData(const char* name)
460  {
462  }
463 
464  OIDN_DEPRECATED("removeData is deprecated. Use unsetData instead.")
465  void removeData(const char* name)
466  {
468  }
469 
470  // Sets a boolean parameter of the filter.
471  void set(const char* name, bool value)
472  {
473  oidnSetFilterBool(handle, name, value);
474  }
475 
476  // Sets an integer parameter of the filter.
477  void set(const char* name, int value)
478  {
479  oidnSetFilterInt(handle, name, value);
480  }
481 
482  void set(const char* name, Quality value)
483  {
484  oidnSetFilterInt(handle, name, static_cast<int>(value));
485  }
486 
487  // Sets a float parameter of the filter.
488  void set(const char* name, float value)
489  {
490  oidnSetFilterFloat(handle, name, value);
491  }
492 
493  // Gets a parameter of the filter.
494  template<typename T>
495  T get(const char* name) const;
496 
497  // Sets the progress monitor callback function of the filter.
499  {
501  }
502 
503  // Commits all previous changes to the filter.
504  void commit()
505  {
507  }
508 
509  // Executes the filter.
510  void execute()
511  {
513  }
514 
515  // Executes the filter asynchronously.
517  {
519  }
520 
521  #if defined(SYCL_LANGUAGE_VERSION)
522  // Executes the filter of a SYCL device using the specified dependent events asynchronously, and
523  // optionally returns an event for completion.
524  sycl::event executeAsync(const std::vector<sycl::event>& depEvents)
525  {
526  sycl::event doneEvent;
527  oidnExecuteSYCLFilterAsync(handle, depEvents.data(), static_cast<int>(depEvents.size()), &doneEvent);
528  return doneEvent;
529  }
530  #endif
531 
532  private:
534  };
535 
536  template<>
537  inline bool FilterRef::get(const char* name) const
538  {
539  return oidnGetFilterBool(handle, name);
540  }
541 
542  template<>
543  inline int FilterRef::get(const char* name) const
544  {
545  return oidnGetFilterInt(handle, name);
546  }
547 
548  template<>
549  inline Quality FilterRef::get(const char* name) const
550  {
551  return static_cast<Quality>(oidnGetFilterInt(handle, name));
552  }
553 
554  template<>
555  inline float FilterRef::get(const char* name) const
556  {
557  return oidnGetFilterFloat(handle, name);
558  }
559 
560  // -----------------------------------------------------------------------------------------------
561  // Device
562  // -----------------------------------------------------------------------------------------------
563 
564  // Device types
565  enum class DeviceType
566  {
567  Default = OIDN_DEVICE_TYPE_DEFAULT, // select device automatically
568 
569  CPU = OIDN_DEVICE_TYPE_CPU, // CPU device
570  SYCL = OIDN_DEVICE_TYPE_SYCL, // SYCL device
571  CUDA = OIDN_DEVICE_TYPE_CUDA, // CUDA device
572  HIP = OIDN_DEVICE_TYPE_HIP, // HIP device
573  Metal = OIDN_DEVICE_TYPE_METAL, // Metal device
574  };
575 
576  // Error codes
577  enum class Error
578  {
579  None = OIDN_ERROR_NONE, // no error occurred
580  Unknown = OIDN_ERROR_UNKNOWN, // an unknown error occurred
581  InvalidArgument = OIDN_ERROR_INVALID_ARGUMENT, // an invalid argument was specified
582  InvalidOperation = OIDN_ERROR_INVALID_OPERATION, // the operation is not allowed
583  OutOfMemory = OIDN_ERROR_OUT_OF_MEMORY, // not enough memory to execute the operation
584  UnsupportedHardware = OIDN_ERROR_UNSUPPORTED_HARDWARE, // the hardware (e.g. CPU) is not supported
585  Cancelled = OIDN_ERROR_CANCELLED, // the operation was cancelled by the user
586  };
587 
588  // Error callback function
589  typedef void (*ErrorFunction)(void* userPtr, Error code, const char* message);
590 
591  // Opaque universally unique identifier (UUID) of a physical device
592  struct UUID
593  {
595  };
596 
597  // Opaque locally unique identifier (LUID) of a physical device
598  struct LUID
599  {
600  union
601  {
602  struct
603  {
604  uint32_t low;
605  int32_t high;
606  };
608  };
609  };
610 
611  // Device object with automatic reference counting
612  class DeviceRef
613  {
614  public:
615  DeviceRef() : handle(nullptr) {}
616  DeviceRef(OIDNDevice handle) : handle(handle) {}
617 
618  DeviceRef(const DeviceRef& other) : handle(other.handle)
619  {
620  if (handle)
622  }
623 
624  DeviceRef(DeviceRef&& other) noexcept : handle(other.handle)
625  {
626  other.handle = nullptr;
627  }
628 
630  {
631  if (&other != this)
632  {
633  if (other.handle)
634  oidnRetainDevice(other.handle);
635  if (handle)
637  handle = other.handle;
638  }
639  return *this;
640  }
641 
642  DeviceRef& operator =(DeviceRef&& other) noexcept
643  {
644  std::swap(handle, other.handle);
645  return *this;
646  }
647 
649  {
650  if (other)
651  oidnRetainDevice(other);
652  if (handle)
654  handle = other;
655  return *this;
656  }
657 
659  {
660  if (handle)
662  }
663 
665  {
666  return handle;
667  }
668 
669  operator bool() const
670  {
671  return handle != nullptr;
672  }
673 
674  // Releases the device (decrements the reference count).
675  void release()
676  {
677  if (handle)
678  {
680  handle = nullptr;
681  }
682  }
683 
684  // Sets a boolean parameter of the device.
685  void set(const char* name, bool value)
686  {
687  oidnSetDeviceBool(handle, name, value);
688  }
689 
690  // Sets an integer parameter of the device.
691  void set(const char* name, int value)
692  {
693  oidnSetDeviceInt(handle, name, value);
694  }
695 
696  // Sets an unsigned integer parameter of the device.
697  void set(const char* name, unsigned int value)
698  {
699  oidnSetDeviceUInt(handle, name, value);
700  }
701 
702  // Gets a parameter of the device.
703  template<typename T>
704  T get(const char* name) const;
705 
706  // Sets the error callback function of the device.
707  void setErrorFunction(ErrorFunction func, void* userPtr = nullptr)
708  {
709  oidnSetDeviceErrorFunction(handle, reinterpret_cast<OIDNErrorFunction>(func), userPtr);
710  }
711 
712  // Returns the first unqueried error code and clears the stored error.
713  // Can be called for a null device as well to check for global errors (e.g. why a device
714  // creation or physical device query has failed.
716  {
717  return static_cast<Error>(oidnGetDeviceError(handle, nullptr));
718  }
719 
720  // Returns the first unqueried error code and string message, and clears the stored error.
721  // Can be called for a null device as well to check why a device creation failed.
722  Error getError(const char*& outMessage)
723  {
724  return static_cast<Error>(oidnGetDeviceError(handle, &outMessage));
725  }
726 
727  // Commits all previous changes to the device.
728  // Must be called before first using the device (e.g. creating filters).
729  void commit()
730  {
732  }
733 
734  // Waits for all asynchronous operations running on the device to complete.
735  void sync()
736  {
738  }
739 
740  // Creates a buffer accessible to both the host and device.
741  BufferRef newBuffer(size_t byteSize) const
742  {
743  return oidnNewBuffer(handle, byteSize);
744  }
745 
746  // Creates a buffer with the specified storage mode.
747  BufferRef newBuffer(size_t byteSize, Storage storage) const
748  {
749  return oidnNewBufferWithStorage(handle, byteSize, static_cast<OIDNStorage>(storage));
750  }
751 
752  // Creates a shared buffer from memory allocated and owned by the user and accessible to the
753  // device.
754  BufferRef newBuffer(void* ptr, size_t byteSize) const
755  {
756  return oidnNewSharedBuffer(handle, ptr, byteSize);
757  }
758 
759  // Creates a shared buffer by importing external memory from a POSIX file descriptor.
760  BufferRef newBuffer(ExternalMemoryTypeFlag fdType, int fd, size_t byteSize) const
761  {
763  handle, static_cast<OIDNExternalMemoryTypeFlag>(fdType), fd, byteSize);
764  }
765 
766  // Creates a shared buffer by importing external memory from a Win32 handle.
767  BufferRef newBuffer(ExternalMemoryTypeFlag handleType, void* handle, const void* name, size_t byteSize) const
768  {
770  this->handle, static_cast<OIDNExternalMemoryTypeFlag>(handleType), handle, name, byteSize);
771  }
772 
773  // Creates a shared buffer from a Metal buffer.
774  // Only buffers with shared or private storage and hazard tracking are supported.
775  #if defined(__OBJC__)
777  {
778  return oidnNewSharedBufferFromMetal(handle, buffer);
779  }
780  #endif
781 
782  // Creates a filter of the specified type (e.g. "RT").
783  FilterRef newFilter(const char* type) const
784  {
785  return oidnNewFilter(handle, type);
786  }
787 
788  private:
790  };
791 
792  template<>
793  inline bool DeviceRef::get(const char* name) const
794  {
795  return oidnGetDeviceBool(handle, name);
796  }
797 
798  template<>
799  inline int DeviceRef::get(const char* name) const
800  {
801  return oidnGetDeviceInt(handle, name);
802  }
803 
804  template<>
805  inline unsigned int DeviceRef::get(const char* name) const
806  {
807  return oidnGetDeviceUInt(handle, name);
808  }
809 
810  template<>
811  inline DeviceType DeviceRef::get(const char* name) const
812  {
813  return static_cast<DeviceType>(oidnGetDeviceInt(handle, name));
814  }
815 
816  template<>
817  inline ExternalMemoryTypeFlags DeviceRef::get(const char* name) const
818  {
820  }
821 
822  // Returns the first unqueried per-thread global error code and clears the stored error.
823  inline Error getError()
824  {
825  return static_cast<Error>(oidnGetDeviceError(nullptr, nullptr));
826  }
827 
828  // Returns the first unqueried per-thread global error code and string message, and clears the
829  // stored error.
830  inline Error getError(const char*& outMessage)
831  {
832  return static_cast<Error>(oidnGetDeviceError(nullptr, &outMessage));
833  }
834 
835  // Creates a device of the specified type.
837  {
838  return DeviceRef(oidnNewDevice(static_cast<OIDNDeviceType>(type)));
839  }
840 
841  // Creates a device from a physical device specified by its ID (0 to getNumPhysicalDevices()-1).
842  inline DeviceRef newDevice(int physicalDeviceID)
843  {
844  return DeviceRef(oidnNewDeviceByID(physicalDeviceID));
845  }
846 
847  // Creates a device from a physical device specified by its UUID.
848  inline DeviceRef newDevice(const UUID& uuid)
849  {
850  return DeviceRef(oidnNewDeviceByUUID(uuid.bytes));
851  }
852 
853  // Creates a device from a physical device specified by its LUID.
854  inline DeviceRef newDevice(const LUID& luid)
855  {
856  return DeviceRef(oidnNewDeviceByLUID(luid.bytes));
857  }
858 
859  // Creates a device from a physical device specified by its PCI address.
860  inline DeviceRef newDevice(int pciDomain, int pciBus, int pciDevice, int pciFunction)
861  {
862  return DeviceRef(oidnNewDeviceByPCIAddress(pciDomain, pciBus, pciDevice, pciFunction));
863  }
864 
865 #if defined(SYCL_LANGUAGE_VERSION)
866  // Creates a device from the specified SYCL queue.
867  inline DeviceRef newSYCLDevice(const sycl::queue& queue)
868  {
869  return DeviceRef(oidnNewSYCLDevice(&queue, 1));
870  }
871 
872  // Creates a device from the specified list of SYCL queues.
873  // The queues should belong to different SYCL sub-devices (Xe Stack/Tile) of the same SYCL
874  // root-device (GPU).
875  inline DeviceRef newSYCLDevice(const std::vector<sycl::queue>& queues)
876  {
877  return DeviceRef(oidnNewSYCLDevice(queues.data(), static_cast<int>(queues.size())));
878  }
879 #endif
880 
881  // Creates a device from the specified CUDA device ID (negative value maps to the current device)
882  // and stream.
884  {
885  return DeviceRef(oidnNewCUDADevice(&deviceID, &stream, 1));
886  }
887 
888  // Creates a device from the specified pairs of CUDA device IDs (negative ID corresponds to the
889  // current device) and streams (null stream corresponds to the default stream).
890  // Currently only one device ID/stream is supported.
891  inline DeviceRef newCUDADevice(const std::vector<int>& deviceIDs,
892  const std::vector<cudaStream_t>& streams)
893  {
894  assert(deviceIDs.size() == streams.size());
895  return DeviceRef(oidnNewCUDADevice(deviceIDs.data(), streams.data(),
896  static_cast<int>(streams.size())));
897  }
898 
899  // Creates a device from the specified HIP device ID (negative ID corresponds to the current
900  // device) and stream (null stream corresponds to the default stream).
901  inline DeviceRef newHIPDevice(int deviceID, hipStream_t stream)
902  {
903  return DeviceRef(oidnNewHIPDevice(&deviceID, &stream, 1));
904  }
905 
906  // Creates a device from the specified pairs of HIP device IDs (negative ID corresponds to the
907  // current device) and streams (null stream corresponds to the default stream).
908  // Currently only one device ID/stream is supported.
909  inline DeviceRef newHIPDevice(const std::vector<int>& deviceIDs,
910  const std::vector<hipStream_t>& streams)
911  {
912  assert(deviceIDs.size() == streams.size());
913  return DeviceRef(oidnNewHIPDevice(deviceIDs.data(), streams.data(),
914  static_cast<int>(streams.size())));
915  }
916 
917  // Creates a device from the specified Metal command queue.
919  {
920  return DeviceRef(oidnNewMetalDevice(&commandQueue, 1));
921  }
922 
923  // Creates a device from the specified list of Metal command queues.
924  // Currently only one queue is supported.
925  inline DeviceRef newMetalDevice(const std::vector<MTLCommandQueue_id>& commandQueues)
926  {
927  return DeviceRef(oidnNewMetalDevice(commandQueues.data(), static_cast<int>(commandQueues.size())));
928  }
929 
930  // -----------------------------------------------------------------------------------------------
931  // Physical Device
932  // -----------------------------------------------------------------------------------------------
933 
935  {
936  public:
938  PhysicalDeviceRef(int id) : id(id) {}
939 
941  {
942  id = other;
943  return *this;
944  }
945 
946  int getID() const
947  {
948  return id;
949  }
950 
951  operator bool() const
952  {
953  return id >= 0;
954  }
955 
956  // Gets a paramter of the physical device.
957  template<typename T>
958  T get(const char* name) const;
959 
960  // Gets an opaque data parameter of the physical device.
961  std::pair<const void*, size_t> getData(const char* name) const
962  {
963  size_t byteSize = 0;
964  const void* ptr = oidnGetPhysicalDeviceData(id, name, &byteSize);
965  return {ptr, byteSize};
966  }
967 
968  // Creates a device from the physical device.
970  {
971  return DeviceRef(oidnNewDeviceByID(id));
972  }
973 
974  private:
975  int id;
976  };
977 
978  // Returns the number of supported physical devices.
980  {
981  return oidnGetNumPhysicalDevices();
982  }
983 
984  template<>
985  inline bool PhysicalDeviceRef::get(const char* name) const
986  {
987  return oidnGetPhysicalDeviceBool(id, name);
988  }
989 
990  template<>
991  inline int PhysicalDeviceRef::get(const char* name) const
992  {
993  return oidnGetPhysicalDeviceInt(id, name);
994  }
995 
996  template<>
997  inline unsigned int PhysicalDeviceRef::get(const char* name) const
998  {
999  return oidnGetPhysicalDeviceUInt(id, name);
1000  }
1001 
1002  template<>
1003  inline DeviceType PhysicalDeviceRef::get(const char* name) const
1004  {
1005  return static_cast<DeviceType>(oidnGetPhysicalDeviceInt(id, name));
1006  }
1007 
1008  template<>
1009  inline const char* PhysicalDeviceRef::get(const char* name) const
1010  {
1011  return oidnGetPhysicalDeviceString(id, name);
1012  }
1013 
1014  template<>
1015  inline std::string PhysicalDeviceRef::get(const char* name) const
1016  {
1017  const char* str = oidnGetPhysicalDeviceString(id, name);
1018  return str ? str : "";
1019  }
1020 
1021  template<>
1022  inline UUID PhysicalDeviceRef::get(const char* name) const
1023  {
1024  UUID uuid{};
1025  auto data = getData(name);
1026  if (data.first != nullptr)
1027  {
1028  if (data.second == sizeof(uuid.bytes))
1029  std::memcpy(uuid.bytes, data.first, sizeof(uuid.bytes));
1030  else
1031  getData(""); // invoke an error
1032  }
1033  return uuid;
1034  }
1035 
1036  template<>
1037  inline LUID PhysicalDeviceRef::get(const char* name) const
1038  {
1039  LUID luid{};
1040  auto data = getData(name);
1041  if (data.first != nullptr)
1042  {
1043  if (data.second == sizeof(luid.bytes))
1044  std::memcpy(luid.bytes, data.first, sizeof(luid.bytes));
1045  else
1046  getData(""); // invoke an error
1047  }
1048  return luid;
1049  }
1050 
void set(const char *name, int value)
Definition: oidn.hpp:691
GLuint GLuint stream
Definition: glcorearb.h:1832
DeviceRef newDevice()
Definition: oidn.hpp:969
~FilterRef()
Definition: oidn.hpp:380
Error getError()
Definition: oidn.hpp:715
OIDNDevice getHandle() const
Definition: oidn.hpp:664
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:760
constexpr bool operator==(const Flags &b) const noexcept
Definition: oidn.hpp:67
void unsetData(const char *name)
Definition: oidn.hpp:459
OIDN_API void oidnSetDeviceInt(OIDNDevice device, const char *name, int value)
Definition: oidn.hpp:598
OIDN_API OIDNDevice oidnNewCUDADevice(const int *deviceIDs, const cudaStream_t *streams, int numPairs)
OIDNFilter getHandle() const
Definition: oidn.hpp:386
OIDN_API void oidnExecuteFilterAsync(OIDNFilter filter)
BufferRef newBuffer(size_t byteSize) const
Definition: oidn.hpp:741
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:607
void commit()
Definition: oidn.hpp:504
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:407
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:1639
DeviceRef()
Definition: oidn.hpp:615
uint32_t low
Definition: oidn.hpp:604
PhysicalDeviceRef & operator=(int other)
Definition: oidn.hpp:940
OIDN_API void oidnUnsetFilterImage(OIDNFilter filter, const char *name)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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:217
void executeAsync()
Definition: oidn.hpp:516
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:331
void commit()
Definition: oidn.hpp:729
void setErrorFunction(ErrorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:707
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:675
~BufferRef()
Definition: oidn.hpp:243
BufferRef()
Definition: oidn.hpp:200
OIDN_API float oidnGetFilterFloat(OIDNFilter filter, const char *name)
struct ihipStream_t * hipStream_t
Definition: oidn.h:25
DeviceRef(const DeviceRef &other)
Definition: oidn.hpp:618
~DeviceRef()
Definition: oidn.hpp:658
Definition: oidn.hpp:592
struct OIDNFilterImpl * OIDNFilter
Definition: oidn.h:367
void setProgressMonitorFunction(ProgressMonitorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:498
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:338
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:616
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
Format
Definition: oidn.hpp:118
struct OIDNBufferImpl * OIDNBuffer
Definition: oidn.h:292
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:421
DeviceRef(DeviceRef &&other) noexcept
Definition: oidn.hpp:624
BufferRef(BufferRef &&other) noexcept
Definition: oidn.hpp:209
void * getData() const
Definition: oidn.hpp:284
Definition: core.h:760
void updateData(const char *name)
Definition: oidn.hpp:453
DeviceRef newHIPDevice(int deviceID, hipStream_t stream)
Definition: oidn.hpp:901
BufferRef & operator=(const BufferRef &other)
Definition: oidn.hpp:214
void setData(const char *name, void *hostPtr, size_t byteSize)
Definition: oidn.hpp:447
OIDN_API void oidnUpdateFilterData(OIDNFilter filter, const char *name)
DeviceRef newMetalDevice(MTLCommandQueue_id commandQueue)
Definition: oidn.hpp:918
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:754
OIDN_API void oidnWriteBuffer(OIDNBuffer buffer, size_t byteOffset, size_t byteSize, const void *srcHostPtr)
FilterRef & operator=(const FilterRef &other)
Definition: oidn.hpp:351
OIDN_API void oidnReleaseBuffer(OIDNBuffer buffer)
OIDNBuffer getHandle() const
Definition: oidn.hpp:249
void * MTLCommandQueue_id
Definition: oidn.h:34
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:488
#define OIDN_LUID_SIZE
Definition: oidn.h:45
void removeData(const char *name)
Definition: oidn.hpp:465
OIDN_API void oidnCommitFilter(OIDNFilter filter)
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:337
constexpr Flags< FlagT > operator&(FlagT a, const Flags< FlagT > &b) noexcept
Definition: oidn.hpp:78
FilterRef(FilterRef &&other) noexcept
Definition: oidn.hpp:346
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:397
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:685
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:623
OIDN_API void oidnSyncDevice(OIDNDevice device)
OIDN_API OIDNBuffer oidnNewBuffer(OIDNDevice device, size_t byteSize)
OIDN_API OIDNDevice oidnNewDevice(OIDNDeviceType type)
GLuint id
Definition: glcorearb.h:655
FilterRef(const FilterRef &other)
Definition: oidn.hpp:340
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:961
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
#define OIDN_UUID_SIZE
Definition: oidn.h:44
OIDN_API OIDNBuffer oidnNewSharedBuffer(OIDNDevice device, void *devPtr, size_t byteSize)
struct CUstream_st * cudaStream_t
Definition: oidn.h:24
OIDN_API OIDNDevice oidnNewMetalDevice(const MTLCommandQueue_id *commandQueues, int numQueues)
OIDN_API size_t oidnGetBufferSize(OIDNBuffer buffer)
PhysicalDeviceRef(int id)
Definition: oidn.hpp:938
constexpr Flags operator&(const Flags &b) const noexcept
Definition: oidn.hpp:43
void unsetImage(const char *name)
Definition: oidn.hpp:435
Definition: oidn.hpp:29
BufferRef newBuffer(ExternalMemoryTypeFlag handleType, void *handle, const void *name, size_t byteSize) const
Definition: oidn.hpp:767
Error getError(const char *&outMessage)
Definition: oidn.hpp:722
int getID() const
Definition: oidn.hpp:946
OIDN_API void oidnSetFilterFloat(OIDNFilter filter, const char *name, float value)
Error getError()
Definition: oidn.hpp:823
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:836
void set(const char *name, int value)
Definition: oidn.hpp:477
FilterRef newFilter(const char *type) const
Definition: oidn.hpp:783
int32_t high
Definition: oidn.hpp:605
GLenum func
Definition: glcorearb.h:783
unsigned int oidnGetDeviceUInt(OIDNDevice device, const char *name)
Definition: oidn.h:185
Error
Definition: oidn.hpp:577
bool(* OIDNProgressMonitorFunction)(void *userPtr, double n)
Definition: oidn.h:364
void removeImage(const char *name)
Definition: oidn.hpp:441
DeviceType
Definition: oidn.hpp:565
void execute()
Definition: oidn.hpp:510
DeviceRef newCUDADevice(int deviceID, cudaStream_t stream)
Definition: oidn.hpp:883
struct OIDNDeviceImpl * OIDNDevice
Definition: oidn.h:101
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:57
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:471
auto ptr(T p) -> const void *
Definition: format.h:2448
void(* ErrorFunction)(void *userPtr, Error code, const char *message)
Definition: oidn.hpp:589
int getNumPhysicalDevices()
Definition: oidn.hpp:979
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:747
OIDN_API OIDNBuffer oidnNewBufferWithStorage(OIDNDevice device, size_t byteSize, OIDNStorage storage)
void set(const char *name, unsigned int value)
Definition: oidn.hpp:697
BufferRef(const BufferRef &other)
Definition: oidn.hpp:203
Definition: core.h:1131
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:735
void oidnSetDeviceUInt(OIDNDevice device, const char *name, unsigned int value)
Definition: oidn.h:167
void set(const char *name, Quality value)
Definition: oidn.hpp:482
T get(const char *name) const
type
Definition: core.h:1059
ExternalMemoryTypeFlag
Definition: oidn.hpp:152
uint8_t bytes[OIDN_UUID_SIZE]
Definition: oidn.hpp:594
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:895
Definition: format.h:2459
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:629
OIDN_API int oidnGetDeviceInt(OIDNDevice device, const char *name)