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 2009-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include "oidn.h"
8 
10 
11  // ---------------------------------------------------------------------------
12  // Buffer
13  // ---------------------------------------------------------------------------
14 
15  // Formats for images and other data stored in buffers
16  enum class Format
17  {
19 
20  // 32-bit single-precision floating point scalar and vector formats
25  };
26 
27  // Access modes for mapping buffers
28  enum class Access
29  {
30  Read = OIDN_ACCESS_READ, // read-only access
31  Write = OIDN_ACCESS_WRITE, // write-only access
32  ReadWrite = OIDN_ACCESS_READ_WRITE, // read and write access
33  WriteDiscard = OIDN_ACCESS_WRITE_DISCARD, // write-only access, previous contents discarded
34  };
35 
36  // Buffer object with automatic reference counting
37  class BufferRef
38  {
39  private:
41 
42  public:
43  BufferRef() : handle(nullptr) {}
44  BufferRef(OIDNBuffer handle) : handle(handle) {}
45 
46  BufferRef(const BufferRef& other) : handle(other.handle)
47  {
48  if (handle)
50  }
51 
52  BufferRef(BufferRef&& other) : handle(other.handle)
53  {
54  other.handle = nullptr;
55  }
56 
58  {
59  if (&other != this)
60  {
61  if (other.handle)
62  oidnRetainBuffer(other.handle);
63  if (handle)
65  handle = other.handle;
66  }
67  return *this;
68  }
69 
71  {
72  std::swap(handle, other.handle);
73  return *this;
74  }
75 
77  {
78  if (other)
79  oidnRetainBuffer(other);
80  if (handle)
82  handle = other;
83  return *this;
84  }
85 
87  {
88  if (handle)
90  }
91 
93  {
94  return handle;
95  }
96 
97  operator bool() const
98  {
99  return handle != nullptr;
100  }
101 
102  // Maps a region of the buffer to host memory.
103  // If byteSize is 0, the maximum available amount of memory will be mapped.
104  void* map(Access access = Access::ReadWrite, size_t byteOffset = 0, size_t byteSize = 0)
105  {
106  return oidnMapBuffer(handle, (OIDNAccess)access, byteOffset, byteSize);
107  }
108 
109  // Unmaps a region of the buffer.
110  // mappedPtr must be a pointer returned by a previous call to map.
111  void unmap(void* mappedPtr)
112  {
113  oidnUnmapBuffer(handle, mappedPtr);
114  }
115  };
116 
117  // ---------------------------------------------------------------------------
118  // Filter
119  // ---------------------------------------------------------------------------
120 
121  // Progress monitor callback function
122  typedef bool (*ProgressMonitorFunction)(void* userPtr, double n);
123 
124  // Filter object with automatic reference counting
125  class FilterRef
126  {
127  private:
129 
130  public:
131  FilterRef() : handle(nullptr) {}
132  FilterRef(OIDNFilter handle) : handle(handle) {}
133 
134  FilterRef(const FilterRef& other) : handle(other.handle)
135  {
136  if (handle)
138  }
139 
140  FilterRef(FilterRef&& other) : handle(other.handle)
141  {
142  other.handle = nullptr;
143  }
144 
146  {
147  if (&other != this)
148  {
149  if (other.handle)
150  oidnRetainFilter(other.handle);
151  if (handle)
153  handle = other.handle;
154  }
155  return *this;
156  }
157 
159  {
160  std::swap(handle, other.handle);
161  return *this;
162  }
163 
165  {
166  if (other)
167  oidnRetainFilter(other);
168  if (handle)
170  handle = other;
171  return *this;
172  }
173 
175  {
176  if (handle)
178  }
179 
181  {
182  return handle;
183  }
184 
185  operator bool() const
186  {
187  return handle != nullptr;
188  }
189 
190  // Sets an image parameter of the filter (stored in a buffer).
191  void setImage(const char* name,
192  const BufferRef& buffer, Format format,
193  size_t width, size_t height,
194  size_t byteOffset = 0,
195  size_t bytePixelStride = 0, size_t byteRowStride = 0)
196  {
198  buffer.getHandle(), (OIDNFormat)format,
199  width, height,
200  byteOffset,
201  bytePixelStride, byteRowStride);
202  }
203 
204  // Sets an image parameter of the filter (owned by the user).
205  void setImage(const char* name,
206  void* ptr, Format format,
207  size_t width, size_t height,
208  size_t byteOffset = 0,
209  size_t bytePixelStride = 0, size_t byteRowStride = 0)
210  {
212  ptr, (OIDNFormat)format,
213  width, height,
214  byteOffset,
215  bytePixelStride, byteRowStride);
216  }
217 
218  // Removes an image parameter of the filter that was previously set.
219  void removeImage(const char* name)
220  {
222  }
223 
224  // Sets an opaque data parameter of the filter (owned by the user).
225  void setData(const char* name,
226  void* ptr, size_t byteSize)
227  {
229  ptr, byteSize);
230  }
231 
232  // Notifies the filter that the contents of an opaque data parameter has been changed.
233  void updateData(const char* name)
234  {
236  }
237 
238  // Removes an opaque data parameter of the filter that was previously set.
239  void removeData(const char* name)
240  {
242  }
243 
244  // Sets a boolean parameter of the filter.
245  void set(const char* name, bool value)
246  {
247  oidnSetFilter1b(handle, name, value);
248  }
249 
250  // Sets an integer parameter of the filter.
251  void set(const char* name, int value)
252  {
253  oidnSetFilter1i(handle, name, value);
254  }
255 
256  // Sets a float parameter of the filter.
257  void set(const char* name, float value)
258  {
259  oidnSetFilter1f(handle, name, value);
260  }
261 
262  // Gets a parameter of the filter.
263  template<typename T>
264  T get(const char* name);
265 
266  // Sets the progress monitor callback function of the filter.
268  {
270  }
271 
272  // Commits all previous changes to the filter.
273  void commit()
274  {
276  }
277 
278  // Executes the filter.
279  void execute()
280  {
282  }
283  };
284 
285  // Gets a boolean parameter of the filter.
286  template<>
287  inline bool FilterRef::get(const char* name)
288  {
289  return oidnGetFilter1b(handle, name);
290  }
291 
292  // Gets an integer parameter of the filter.
293  template<>
294  inline int FilterRef::get(const char* name)
295  {
296  return oidnGetFilter1i(handle, name);
297  }
298 
299  // Gets a float parameter of the filter.
300  template<>
301  inline float FilterRef::get(const char* name)
302  {
303  return oidnGetFilter1f(handle, name);
304  }
305 
306  // ---------------------------------------------------------------------------
307  // Device
308  // ---------------------------------------------------------------------------
309 
310  // Device types
311  enum class DeviceType
312  {
313  Default = OIDN_DEVICE_TYPE_DEFAULT, // select device automatically
314 
315  CPU = OIDN_DEVICE_TYPE_CPU, // CPU device
316  };
317 
318  // Error codes
319  enum class Error
320  {
321  None = OIDN_ERROR_NONE, // no error occurred
322  Unknown = OIDN_ERROR_UNKNOWN, // an unknown error occurred
323  InvalidArgument = OIDN_ERROR_INVALID_ARGUMENT, // an invalid argument was specified
324  InvalidOperation = OIDN_ERROR_INVALID_OPERATION, // the operation is not allowed
325  OutOfMemory = OIDN_ERROR_OUT_OF_MEMORY, // not enough memory to execute the operation
326  UnsupportedHardware = OIDN_ERROR_UNSUPPORTED_HARDWARE, // the hardware (e.g. CPU) is not supported
327  Cancelled = OIDN_ERROR_CANCELLED, // the operation was cancelled by the user
328  };
329 
330  // Error callback function
331  typedef void (*ErrorFunction)(void* userPtr, Error code, const char* message);
332 
333  // Device object with automatic reference counting
334  class DeviceRef
335  {
336  private:
338 
339  public:
340  DeviceRef() : handle(nullptr) {}
341  DeviceRef(OIDNDevice handle) : handle(handle) {}
342 
343  DeviceRef(const DeviceRef& other) : handle(other.handle)
344  {
345  if (handle)
347  }
348 
349  DeviceRef(DeviceRef&& other) : handle(other.handle)
350  {
351  other.handle = nullptr;
352  }
353 
355  {
356  if (&other != this)
357  {
358  if (other.handle)
359  oidnRetainDevice(other.handle);
360  if (handle)
362  handle = other.handle;
363  }
364  return *this;
365  }
366 
368  {
369  std::swap(handle, other.handle);
370  return *this;
371  }
372 
374  {
375  if (other)
376  oidnRetainDevice(other);
377  if (handle)
379  handle = other;
380  return *this;
381  }
382 
384  {
385  if (handle)
387  }
388 
390  {
391  return handle;
392  }
393 
394  operator bool() const
395  {
396  return handle != nullptr;
397  }
398 
399  // Sets a boolean parameter of the device.
400  void set(const char* name, bool value)
401  {
402  oidnSetDevice1b(handle, name, value);
403  }
404 
405  // Sets an integer parameter of the device.
406  void set(const char* name, int value)
407  {
408  oidnSetDevice1i(handle, name, value);
409  }
410 
411  // Gets a parameter of the device.
412  template<typename T>
413  T get(const char* name);
414 
415  // Sets the error callback function of the device.
416  void setErrorFunction(ErrorFunction func, void* userPtr = nullptr)
417  {
419  }
420 
421  // Returns the first unqueried error code and clears the stored error.
422  // Can be called for a null device as well to check why a device creation failed.
424  {
425  return (Error)oidnGetDeviceError(handle, nullptr);
426  }
427 
428  // Returns the first unqueried error code and string message, and clears the stored error.
429  // Can be called for a null device as well to check why a device creation failed.
430  Error getError(const char*& outMessage)
431  {
432  return (Error)oidnGetDeviceError(handle, &outMessage);
433  }
434 
435  // Commits all previous changes to the device.
436  // Must be called before first using the device (e.g. creating filters).
437  void commit()
438  {
440  }
441 
442  // Creates a new buffer (data allocated and owned by the device).
443  BufferRef newBuffer(size_t byteSize)
444  {
445  return oidnNewBuffer(handle, byteSize);
446  }
447 
448  // Creates a new shared buffer (data allocated and owned by the user).
449  BufferRef newBuffer(void* ptr, size_t byteSize)
450  {
451  return oidnNewSharedBuffer(handle, ptr, byteSize);
452  }
453 
454  // Creates a new filter of the specified type (e.g. "RT").
455  FilterRef newFilter(const char* type)
456  {
457  return oidnNewFilter(handle, type);
458  }
459  };
460 
461  // Gets a boolean parameter of the device.
462  template<>
463  inline bool DeviceRef::get(const char* name)
464  {
465  return oidnGetDevice1b(handle, name);
466  }
467 
468  // Gets an integer parameter of the device (e.g. "version").
469  template<>
470  inline int DeviceRef::get(const char* name)
471  {
472  return oidnGetDevice1i(handle, name);
473  }
474 
475  // Creates a new device.
477  {
479  }
480 
void set(const char *name, int value)
Definition: oidn.hpp:406
~FilterRef()
Definition: oidn.hpp:174
Error getError()
Definition: oidn.hpp:423
OIDNDevice getHandle() const
Definition: oidn.hpp:389
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
OIDN_API void oidnRemoveFilterData(OIDNFilter filter, const char *name)
OIDNFilter getHandle() const
Definition: oidn.hpp:180
OIDN_API int oidnGetDevice1i(OIDNDevice device, const char *name)
OIDN_API bool oidnGetDevice1b(OIDNDevice device, const char *name)
OIDN_API void oidnUnmapBuffer(OIDNBuffer buffer, void *mappedPtr)
OIDN_API void oidnExecuteFilter(OIDNFilter filter)
OIDN_API int oidnGetFilter1i(OIDNFilter filter, const char *name)
OIDN_API void oidnReleaseDevice(OIDNDevice device)
OIDN_API void oidnReleaseFilter(OIDNFilter filter)
void
Definition: png.h:1083
void commit()
Definition: oidn.hpp:273
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:1631
DeviceRef()
Definition: oidn.hpp:340
BufferRef newBuffer(size_t byteSize)
Definition: oidn.hpp:443
OIDNFormat
Definition: oidn.h:83
BufferRef(BufferRef &&other)
Definition: oidn.hpp:52
void commit()
Definition: oidn.hpp:437
void setErrorFunction(ErrorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:416
BufferRef newBuffer(void *ptr, size_t byteSize)
Definition: oidn.hpp:449
void setImage(const char *name, void *ptr, Format format, size_t width, size_t height, size_t byteOffset=0, size_t bytePixelStride=0, size_t byteRowStride=0)
Definition: oidn.hpp:205
~BufferRef()
Definition: oidn.hpp:86
BufferRef()
Definition: oidn.hpp:43
DeviceRef(const DeviceRef &other)
Definition: oidn.hpp:343
~DeviceRef()
Definition: oidn.hpp:383
struct OIDNFilterImpl * OIDNFilter
Definition: oidn.h:134
void setProgressMonitorFunction(ProgressMonitorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:267
OIDN_API void oidnSetDevice1b(OIDNDevice device, const char *name, bool value)
FilterRef(OIDNFilter handle)
Definition: oidn.hpp:132
OIDN_API void oidnRemoveFilterImage(OIDNFilter filter, const char *name)
DeviceRef(DeviceRef &&other)
Definition: oidn.hpp:349
OIDN_API void oidnRetainFilter(OIDNFilter filter)
BufferRef(OIDNBuffer handle)
Definition: oidn.hpp:44
OIDNDeviceType
Definition: oidn.h:19
OIDN_API void oidnRetainBuffer(OIDNBuffer buffer)
DeviceRef(OIDNDevice handle)
Definition: oidn.hpp:341
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
Format
Definition: oidn.hpp:16
struct OIDNBufferImpl * OIDNBuffer
Definition: oidn.h:104
OIDN_API void oidnSetFilter1f(OIDNFilter filter, const char *name, float value)
void setImage(const char *name, const BufferRef &buffer, Format format, size_t width, size_t height, size_t byteOffset=0, size_t bytePixelStride=0, size_t byteRowStride=0)
Definition: oidn.hpp:191
void(* OIDNErrorFunction)(void *userPtr, OIDNError code, const char *message)
Definition: oidn.h:39
OIDN_API OIDNBuffer oidnNewSharedBuffer(OIDNDevice device, void *ptr, size_t byteSize)
GLdouble n
Definition: glcorearb.h:2008
bool(* ProgressMonitorFunction)(void *userPtr, double n)
Definition: oidn.hpp:122
Definition: core.h:760
void updateData(const char *name)
Definition: oidn.hpp:233
BufferRef & operator=(const BufferRef &other)
Definition: oidn.hpp:57
OIDN_API void oidnUpdateFilterData(OIDNFilter filter, const char *name)
FilterRef & operator=(const FilterRef &other)
Definition: oidn.hpp:145
OIDN_API void * oidnMapBuffer(OIDNBuffer buffer, OIDNAccess access, size_t byteOffset, size_t byteSize)
OIDN_API void oidnReleaseBuffer(OIDNBuffer buffer)
OIDNBuffer getHandle() const
Definition: oidn.hpp:92
OIDN_API void oidnRetainDevice(OIDNDevice device)
GLuint GLint GLboolean GLint GLenum access
Definition: glcorearb.h:2222
void set(const char *name, float value)
Definition: oidn.hpp:257
void removeData(const char *name)
Definition: oidn.hpp:239
OIDN_API bool oidnGetFilter1b(OIDNFilter filter, const char *name)
OIDN_API void oidnCommitFilter(OIDNFilter filter)
void setData(const char *name, void *ptr, size_t byteSize)
Definition: oidn.hpp:225
OIDN_API void oidnSetFilterProgressMonitorFunction(OIDNFilter filter, OIDNProgressMonitorFunction func, void *userPtr)
OIDN_API void oidnSetFilter1b(OIDNFilter filter, const char *name, bool value)
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
FilterRef()
Definition: oidn.hpp:131
OIDN_API void oidnSetDevice1i(OIDNDevice device, const char *name, int value)
OIDN_API void oidnCommitDevice(OIDNDevice device)
void set(const char *name, bool value)
Definition: oidn.hpp:400
OIDN_API OIDNBuffer oidnNewBuffer(OIDNDevice device, size_t byteSize)
OIDN_API OIDNDevice oidnNewDevice(OIDNDeviceType type)
FilterRef(const FilterRef &other)
Definition: oidn.hpp:134
OIDN_API void oidnSetDeviceErrorFunction(OIDNDevice device, OIDNErrorFunction func, void *userPtr)
#define OIDN_NAMESPACE_BEGIN
Definition: config.h:33
OIDN_API float oidnGetFilter1f(OIDNFilter filter, const char *name)
GLuint const GLchar * name
Definition: glcorearb.h:786
void * map(Access access=Access::ReadWrite, size_t byteOffset=0, size_t byteSize=0)
Definition: oidn.hpp:104
OIDN_API void oidnSetSharedFilterData(OIDNFilter filter, const char *name, void *ptr, size_t byteSize)
Error getError(const char *&outMessage)
Definition: oidn.hpp:430
T get(const char *name)
DeviceRef newDevice(DeviceType type=DeviceType::Default)
Definition: oidn.hpp:476
void set(const char *name, int value)
Definition: oidn.hpp:251
GLenum func
Definition: glcorearb.h:783
Error
Definition: oidn.hpp:319
bool(* OIDNProgressMonitorFunction)(void *userPtr, double n)
Definition: oidn.h:131
void removeImage(const char *name)
Definition: oidn.hpp:219
OIDN_API void oidnSetSharedFilterImage(OIDNFilter filter, const char *name, void *ptr, OIDNFormat format, size_t width, size_t height, size_t byteOffset, size_t bytePixelStride, size_t byteRowStride)
DeviceType
Definition: oidn.hpp:311
void execute()
Definition: oidn.hpp:279
struct OIDNDeviceImpl * OIDNDevice
Definition: oidn.h:42
void set(const char *name, bool value)
Definition: oidn.hpp:245
auto ptr(T p) -> const void *
Definition: format.h:2448
OIDN_API void oidnSetFilter1i(OIDNFilter filter, const char *name, int value)
OIDN_API void oidnSetFilterImage(OIDNFilter filter, const char *name, OIDNBuffer buffer, OIDNFormat format, size_t width, size_t height, size_t byteOffset, size_t bytePixelStride, size_t byteRowStride)
void(* ErrorFunction)(void *userPtr, Error code, const char *message)
Definition: oidn.hpp:331
GLint GLsizei width
Definition: glcorearb.h:103
Access
Definition: oidn.hpp:28
BufferRef(const BufferRef &other)
Definition: oidn.hpp:46
Definition: core.h:1131
FilterRef(FilterRef &&other)
Definition: oidn.hpp:140
T get(const char *name)
OIDN_API OIDNFilter oidnNewFilter(OIDNDevice device, const char *type)
OIDNAccess
Definition: oidn.h:95
void unmap(void *mappedPtr)
Definition: oidn.hpp:111
type
Definition: core.h:1059
FilterRef newFilter(const char *type)
Definition: oidn.hpp:455
OIDN_API OIDNError oidnGetDeviceError(OIDNDevice device, const char **outMessage)
#define OIDN_NAMESPACE_END
Definition: config.h:34
DeviceRef & operator=(const DeviceRef &other)
Definition: oidn.hpp:354