HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oidn.hpp
Go to the documentation of this file.
1 // ======================================================================== //
2 // Copyright 2009-2019 Intel Corporation //
3 // //
4 // Licensed under the Apache License, Version 2.0 (the "License"); //
5 // you may not use this file except in compliance with the License. //
6 // You may obtain a copy of the License at //
7 // //
8 // http://www.apache.org/licenses/LICENSE-2.0 //
9 // //
10 // Unless required by applicable law or agreed to in writing, software //
11 // distributed under the License is distributed on an "AS IS" BASIS, //
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13 // See the License for the specific language governing permissions and //
14 // limitations under the License. //
15 // ======================================================================== //
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include "oidn.h"
21 
22 namespace oidn {
23 
24  // --------------------------------------------------------------------------
25  // Buffer
26  // --------------------------------------------------------------------------
27 
28  // Formats for images and other data stored in buffers
29  enum class Format
30  {
32 
33  // 32-bit single-precision floating point scalar and vector formats
38  };
39 
40  // Access modes for mapping buffers
41  enum class Access
42  {
43  Read = OIDN_ACCESS_READ, // read-only access
44  Write = OIDN_ACCESS_WRITE, // write-only access
45  ReadWrite = OIDN_ACCESS_READ_WRITE, // read and write access
46  WriteDiscard = OIDN_ACCESS_WRITE_DISCARD, // write-only access, previous contents discarded
47  };
48 
49  // Buffer object with automatic reference counting
50  class BufferRef
51  {
52  private:
53  OIDNBuffer handle;
54 
55  public:
56  BufferRef() : handle(nullptr) {}
57  BufferRef(OIDNBuffer handle) : handle(handle) {}
58 
59  BufferRef(const BufferRef& other) : handle(other.handle)
60  {
61  if (handle)
62  oidnRetainBuffer(handle);
63  }
64 
65  BufferRef(BufferRef&& other) : handle(other.handle)
66  {
67  other.handle = nullptr;
68  }
69 
71  {
72  if (&other != this)
73  {
74  if (other.handle)
75  oidnRetainBuffer(other.handle);
76  if (handle)
77  oidnReleaseBuffer(handle);
78  handle = other.handle;
79  }
80  return *this;
81  }
82 
84  {
85  std::swap(handle, other.handle);
86  return *this;
87  }
88 
90  {
91  if (other)
92  oidnRetainBuffer(other);
93  if (handle)
94  oidnReleaseBuffer(handle);
95  handle = other;
96  return *this;
97  }
98 
100  {
101  if (handle)
102  oidnReleaseBuffer(handle);
103  }
104 
106  {
107  return handle;
108  }
109 
110  operator bool() const
111  {
112  return handle != nullptr;
113  }
114 
115  // Maps a region of the buffer to host memory.
116  // If byteSize is 0, the maximum available amount of memory will be mapped.
117  void* map(Access access = Access::ReadWrite, size_t byteOffset = 0, size_t byteSize = 0)
118  {
119  return oidnMapBuffer(handle, (OIDNAccess)access, byteOffset, byteSize);
120  }
121 
122  // Unmaps a region of the buffer.
123  // mappedPtr must be a pointer returned by a previous call to map.
124  void unmap(void* mappedPtr)
125  {
126  oidnUnmapBuffer(handle, mappedPtr);
127  }
128  };
129 
130  // --------------------------------------------------------------------------
131  // Filter
132  // --------------------------------------------------------------------------
133 
134  // Progress monitor callback function
135  typedef bool (*ProgressMonitorFunction)(void* userPtr, double n);
136 
137  // Filter object with automatic reference counting
138  class FilterRef
139  {
140  private:
141  OIDNFilter handle;
142 
143  public:
144  FilterRef() : handle(nullptr) {}
145  FilterRef(OIDNFilter handle) : handle(handle) {}
146 
147  FilterRef(const FilterRef& other) : handle(other.handle)
148  {
149  if (handle)
150  oidnRetainFilter(handle);
151  }
152 
153  FilterRef(FilterRef&& other) : handle(other.handle)
154  {
155  other.handle = nullptr;
156  }
157 
159  {
160  if (&other != this)
161  {
162  if (other.handle)
163  oidnRetainFilter(other.handle);
164  if (handle)
165  oidnReleaseFilter(handle);
166  handle = other.handle;
167  }
168  return *this;
169  }
170 
172  {
173  std::swap(handle, other.handle);
174  return *this;
175  }
176 
178  {
179  if (other)
180  oidnRetainFilter(other);
181  if (handle)
182  oidnReleaseFilter(handle);
183  handle = other;
184  return *this;
185  }
186 
188  {
189  if (handle)
190  oidnReleaseFilter(handle);
191  }
192 
194  {
195  return handle;
196  }
197 
198  operator bool() const
199  {
200  return handle != nullptr;
201  }
202 
203  // Sets an image parameter of the filter (stored in a buffer).
204  void setImage(const char* name,
205  const BufferRef& buffer, Format format,
206  size_t width, size_t height,
207  size_t byteOffset = 0,
208  size_t bytePixelStride = 0, size_t byteRowStride = 0)
209  {
210  oidnSetFilterImage(handle, name,
211  buffer.getHandle(), (OIDNFormat)format,
212  width, height,
213  byteOffset,
214  bytePixelStride, byteRowStride);
215  }
216 
217  // Sets an image parameter of the filter (owned by the user).
218  void setImage(const char* name,
219  void* ptr, Format format,
220  size_t width, size_t height,
221  size_t byteOffset = 0,
222  size_t bytePixelStride = 0, size_t byteRowStride = 0)
223  {
224  oidnSetSharedFilterImage(handle, name,
225  ptr, (OIDNFormat)format,
226  width, height,
227  byteOffset,
228  bytePixelStride, byteRowStride);
229  }
230 
231  // Sets a boolean parameter of the filter.
232  void set(const char* name, bool value)
233  {
234  oidnSetFilter1b(handle, name, value);
235  }
236 
237  // Sets an integer parameter of the filter.
238  void set(const char* name, int value)
239  {
240  oidnSetFilter1i(handle, name, value);
241  }
242 
243  // Sets a float parameter of the filter.
244  void set(const char* name, float value)
245  {
246  oidnSetFilter1f(handle, name, value);
247  }
248 
249  // Gets a parameter of the filter.
250  template<typename T>
251  T get(const char* name);
252 
253  // Sets the progress monitor callback function of the filter.
255  {
257  }
258 
259  // Commits all previous changes to the filter.
260  void commit()
261  {
262  oidnCommitFilter(handle);
263  }
264 
265  // Executes the filter.
266  void execute()
267  {
268  oidnExecuteFilter(handle);
269  }
270  };
271 
272  // Gets a boolean parameter of the filter.
273  template<>
274  inline bool FilterRef::get(const char* name)
275  {
276  return oidnGetFilter1b(handle, name);
277  }
278 
279  // Gets an integer parameter of the filter.
280  template<>
281  inline int FilterRef::get(const char* name)
282  {
283  return oidnGetFilter1i(handle, name);
284  }
285 
286  // Gets a float parameter of the filter.
287  template<>
288  inline float FilterRef::get(const char* name)
289  {
290  return oidnGetFilter1f(handle, name);
291  }
292 
293  // --------------------------------------------------------------------------
294  // Device
295  // --------------------------------------------------------------------------
296 
297  // Device types
298  enum class DeviceType
299  {
300  Default = OIDN_DEVICE_TYPE_DEFAULT, // select device automatically
301 
302  CPU = OIDN_DEVICE_TYPE_CPU, // CPU device
303  };
304 
305  // Error codes
306  enum class Error
307  {
308  None = OIDN_ERROR_NONE, // no error occurred
309  Unknown = OIDN_ERROR_UNKNOWN, // an unknown error occurred
310  InvalidArgument = OIDN_ERROR_INVALID_ARGUMENT, // an invalid argument was specified
311  InvalidOperation = OIDN_ERROR_INVALID_OPERATION, // the operation is not allowed
312  OutOfMemory = OIDN_ERROR_OUT_OF_MEMORY, // not enough memory to execute the operation
313  UnsupportedHardware = OIDN_ERROR_UNSUPPORTED_HARDWARE, // the hardware (e.g. CPU) is not supported
314  Cancelled = OIDN_ERROR_CANCELLED, // the operation was cancelled by the user
315  };
316 
317  // Error callback function
318  typedef void (*ErrorFunction)(void* userPtr, Error code, const char* message);
319 
320  // Device object with automatic reference counting
321  class DeviceRef
322  {
323  private:
324  OIDNDevice handle;
325 
326  public:
327  DeviceRef() : handle(nullptr) {}
328  DeviceRef(OIDNDevice handle) : handle(handle) {}
329 
330  DeviceRef(const DeviceRef& other) : handle(other.handle)
331  {
332  if (handle)
333  oidnRetainDevice(handle);
334  }
335 
336  DeviceRef(DeviceRef&& other) : handle(other.handle)
337  {
338  other.handle = nullptr;
339  }
340 
342  {
343  if (&other != this)
344  {
345  if (other.handle)
346  oidnRetainDevice(other.handle);
347  if (handle)
348  oidnReleaseDevice(handle);
349  handle = other.handle;
350  }
351  return *this;
352  }
353 
355  {
356  std::swap(handle, other.handle);
357  return *this;
358  }
359 
361  {
362  if (other)
363  oidnRetainDevice(other);
364  if (handle)
365  oidnReleaseDevice(handle);
366  handle = other;
367  return *this;
368  }
369 
371  {
372  if (handle)
373  oidnReleaseDevice(handle);
374  }
375 
377  {
378  return handle;
379  }
380 
381  operator bool() const
382  {
383  return handle != nullptr;
384  }
385 
386  // Sets a boolean parameter of the device.
387  void set(const char* name, bool value)
388  {
389  oidnSetDevice1b(handle, name, value);
390  }
391 
392  // Sets an integer parameter of the device.
393  void set(const char* name, int value)
394  {
395  oidnSetDevice1i(handle, name, value);
396  }
397 
398  // Gets a parameter of the device.
399  template<typename T>
400  T get(const char* name);
401 
402  // Sets the error callback function of the device.
403  void setErrorFunction(ErrorFunction func, void* userPtr = nullptr)
404  {
405  oidnSetDeviceErrorFunction(handle, (OIDNErrorFunction)func, userPtr);
406  }
407 
408  // Returns the first unqueried error code and clears the stored error.
409  // Can be called for a null device as well to check why a device creation failed.
411  {
412  return (Error)oidnGetDeviceError(handle, nullptr);
413  }
414 
415  // Returns the first unqueried error code and string message, and clears the stored error.
416  // Can be called for a null device as well to check why a device creation failed.
417  Error getError(const char*& outMessage)
418  {
419  return (Error)oidnGetDeviceError(handle, &outMessage);
420  }
421 
422  // Commits all previous changes to the device.
423  // Must be called before first using the device (e.g. creating filters).
424  void commit()
425  {
426  oidnCommitDevice(handle);
427  }
428 
429  // Creates a new buffer (data allocated and owned by the device).
430  BufferRef newBuffer(size_t byteSize)
431  {
432  return oidnNewBuffer(handle, byteSize);
433  }
434 
435  // Creates a new shared buffer (data allocated and owned by the user).
436  BufferRef newBuffer(void* ptr, size_t byteSize)
437  {
438  return oidnNewSharedBuffer(handle, ptr, byteSize);
439  }
440 
441  // Creates a new filter of the specified type (e.g. "RT").
442  FilterRef newFilter(const char* type)
443  {
444  return oidnNewFilter(handle, type);
445  }
446  };
447 
448  // Gets a boolean parameter of the device.
449  template<>
450  inline bool DeviceRef::get(const char* name)
451  {
452  return oidnGetDevice1b(handle, name);
453  }
454 
455  // Gets an integer parameter of the device (e.g. "version").
456  template<>
457  inline int DeviceRef::get(const char* name)
458  {
459  return oidnGetDevice1i(handle, name);
460  }
461 
462  // Creates a new device.
464  {
466  }
467 
468 } // namespace oidn
OIDNDevice getHandle() const
Definition: oidn.hpp:376
void commit()
Definition: oidn.hpp:424
BufferRef(OIDNBuffer handle)
Definition: oidn.hpp:57
OIDN_API int oidnGetDevice1i(OIDNDevice device, const char *name)
OIDN_API bool oidnGetDevice1b(OIDNDevice device, const char *name)
GLuint const GLchar * name
Definition: glew.h:1814
OIDN_API void oidnUnmapBuffer(OIDNBuffer buffer, void *mappedPtr)
void commit()
Definition: oidn.hpp:260
OIDN_API void oidnExecuteFilter(OIDNFilter filter)
FilterRef & operator=(const FilterRef &other)
Definition: oidn.hpp:158
OIDN_API int oidnGetFilter1i(OIDNFilter filter, const char *name)
OIDN_API void oidnReleaseDevice(OIDNDevice device)
OIDNBuffer getHandle() const
Definition: oidn.hpp:105
OIDN_API void oidnReleaseFilter(OIDNFilter filter)
void set(const char *name, bool value)
Definition: oidn.hpp:232
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1252
void set(const char *name, int value)
Definition: oidn.hpp:238
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:1629
T get(const char *name)
void * map(Access access=Access::ReadWrite, size_t byteOffset=0, size_t byteSize=0)
Definition: oidn.hpp:117
bool(* ProgressMonitorFunction)(void *userPtr, double n)
Definition: oidn.hpp:135
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:218
OIDNFormat
Definition: oidn.h:106
DeviceRef(const DeviceRef &other)
Definition: oidn.hpp:330
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1252
OIDNFilter getHandle() const
Definition: oidn.hpp:193
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
DeviceType
Definition: oidn.hpp:298
Format
Definition: oidn.hpp:29
void setProgressMonitorFunction(ProgressMonitorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:254
void unmap(void *mappedPtr)
Definition: oidn.hpp:124
struct OIDNFilterImpl * OIDNFilter
Definition: oidn.h:157
OIDN_API void oidnSetDevice1b(OIDNDevice device, const char *name, bool value)
DeviceRef(OIDNDevice handle)
Definition: oidn.hpp:328
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:204
FilterRef(const FilterRef &other)
Definition: oidn.hpp:147
void set(const char *name, int value)
Definition: oidn.hpp:393
OIDN_API void oidnRetainFilter(OIDNFilter filter)
OIDNDeviceType
Definition: oidn.h:42
OIDN_API void oidnRetainBuffer(OIDNBuffer buffer)
struct OIDNBufferImpl * OIDNBuffer
Definition: oidn.h:127
OIDN_API void oidnSetFilter1f(OIDNFilter filter, const char *name, float value)
void(* OIDNErrorFunction)(void *userPtr, OIDNError code, const char *message)
Definition: oidn.h:62
OIDN_API OIDNBuffer oidnNewSharedBuffer(OIDNDevice device, void *ptr, size_t byteSize)
BufferRef newBuffer(void *ptr, size_t byteSize)
Definition: oidn.hpp:436
void setErrorFunction(ErrorFunction func, void *userPtr=nullptr)
Definition: oidn.hpp:403
BufferRef(const BufferRef &other)
Definition: oidn.hpp:59
OIDN_API void * oidnMapBuffer(OIDNBuffer buffer, OIDNAccess access, size_t byteOffset, size_t byteSize)
OIDN_API void oidnReleaseBuffer(OIDNBuffer buffer)
GLuint buffer
Definition: glew.h:1680
OIDN_API void oidnRetainDevice(OIDNDevice device)
BufferRef(BufferRef &&other)
Definition: oidn.hpp:65
OIDN_API bool oidnGetFilter1b(OIDNFilter filter, const char *name)
Error getError(const char *&outMessage)
Definition: oidn.hpp:417
OIDN_API void oidnCommitFilter(OIDNFilter filter)
OIDN_API void oidnSetFilterProgressMonitorFunction(OIDNFilter filter, OIDNProgressMonitorFunction func, void *userPtr)
DeviceRef & operator=(const DeviceRef &other)
Definition: oidn.hpp:341
OIDN_API void oidnSetFilter1b(OIDNFilter filter, const char *name, bool value)
GLsizei GLenum GLuint GLuint GLsizei GLchar * message
Definition: glew.h:2581
void
Definition: png.h:1083
GLsizei n
Definition: glew.h:4040
OIDN_API void oidnSetDevice1i(OIDNDevice device, const char *name, int value)
OIDN_API void oidnCommitDevice(OIDNDevice device)
OIDN_API OIDNBuffer oidnNewBuffer(OIDNDevice device, size_t byteSize)
OIDN_API OIDNDevice oidnNewDevice(OIDNDeviceType type)
OIDN_API void oidnSetDeviceErrorFunction(OIDNDevice device, OIDNErrorFunction func, void *userPtr)
Access
Definition: oidn.hpp:41
OIDN_API float oidnGetFilter1f(OIDNFilter filter, const char *name)
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
BufferRef newBuffer(size_t byteSize)
Definition: oidn.hpp:430
FilterRef(OIDNFilter handle)
Definition: oidn.hpp:145
Error
Definition: oidn.hpp:306
FilterRef newFilter(const char *type)
Definition: oidn.hpp:442
GLenum func
Definition: glcorearb.h:782
bool(* OIDNProgressMonitorFunction)(void *userPtr, double n)
Definition: oidn.h:154
void execute()
Definition: oidn.hpp:266
void(* ErrorFunction)(void *userPtr, Error code, const char *message)
Definition: oidn.hpp:318
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)
FilterRef(FilterRef &&other)
Definition: oidn.hpp:153
const void * ptr(const T *p)
Definition: format.h:3292
struct OIDNDeviceImpl * OIDNDevice
Definition: oidn.h:65
OIDN_API void oidnSetFilter1i(OIDNFilter filter, const char *name, int value)
DeviceRef newDevice(DeviceType type=DeviceType::Default)
Definition: oidn.hpp:463
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)
GLenum access
Definition: glew.h:3608
BufferRef & operator=(const BufferRef &other)
Definition: oidn.hpp:70
void set(const char *name, bool value)
Definition: oidn.hpp:387
DeviceRef(DeviceRef &&other)
Definition: oidn.hpp:336
OIDN_API OIDNFilter oidnNewFilter(OIDNDevice device, const char *type)
OIDNAccess
Definition: oidn.h:118
GLsizei const GLfloat * value
Definition: glew.h:1849
void set(const char *name, float value)
Definition: oidn.hpp:244
Error getError()
Definition: oidn.hpp:410
OIDN_API OIDNError oidnGetDeviceError(OIDNDevice device, const char **outMessage)
T get(const char *name)