oidn.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #pragma once
  17. #include <algorithm>
  18. #include "oidn.h"
  19. namespace oidn {
  20. // --------------------------------------------------------------------------
  21. // Buffer
  22. // --------------------------------------------------------------------------
  23. // Formats for images and other data stored in buffers
  24. enum class Format
  25. {
  26. Undefined = OIDN_FORMAT_UNDEFINED,
  27. // 32-bit single-precision floating point scalar and vector formats
  28. Float = OIDN_FORMAT_FLOAT,
  29. Float2 = OIDN_FORMAT_FLOAT2,
  30. Float3 = OIDN_FORMAT_FLOAT3,
  31. Float4 = OIDN_FORMAT_FLOAT4,
  32. };
  33. // Access modes for mapping buffers
  34. enum class Access
  35. {
  36. Read = OIDN_ACCESS_READ, // read-only access
  37. Write = OIDN_ACCESS_WRITE, // write-only access
  38. ReadWrite = OIDN_ACCESS_READ_WRITE, // read and write access
  39. WriteDiscard = OIDN_ACCESS_WRITE_DISCARD, // write-only access, previous contents discarded
  40. };
  41. // Buffer object with automatic reference counting
  42. class BufferRef
  43. {
  44. private:
  45. OIDNBuffer handle;
  46. public:
  47. BufferRef() : handle(nullptr) {}
  48. BufferRef(OIDNBuffer handle) : handle(handle) {}
  49. BufferRef(const BufferRef& other) : handle(other.handle)
  50. {
  51. if (handle)
  52. oidnRetainBuffer(handle);
  53. }
  54. BufferRef(BufferRef&& other) : handle(other.handle)
  55. {
  56. other.handle = nullptr;
  57. }
  58. BufferRef& operator =(const BufferRef& other)
  59. {
  60. if (&other != this)
  61. {
  62. if (other.handle)
  63. oidnRetainBuffer(other.handle);
  64. if (handle)
  65. oidnReleaseBuffer(handle);
  66. handle = other.handle;
  67. }
  68. return *this;
  69. }
  70. BufferRef& operator =(BufferRef&& other)
  71. {
  72. std::swap(handle, other.handle);
  73. return *this;
  74. }
  75. BufferRef& operator =(OIDNBuffer other)
  76. {
  77. if (other)
  78. oidnRetainBuffer(other);
  79. if (handle)
  80. oidnReleaseBuffer(handle);
  81. handle = other;
  82. return *this;
  83. }
  84. ~BufferRef()
  85. {
  86. if (handle)
  87. oidnReleaseBuffer(handle);
  88. }
  89. OIDNBuffer getHandle() const
  90. {
  91. return handle;
  92. }
  93. operator bool() const
  94. {
  95. return handle != nullptr;
  96. }
  97. // Maps a region of the buffer to host memory.
  98. // If byteSize is 0, the maximum available amount of memory will be mapped.
  99. void* map(Access access = Access::ReadWrite, size_t byteOffset = 0, size_t byteSize = 0)
  100. {
  101. return oidnMapBuffer(handle, (OIDNAccess)access, byteOffset, byteSize);
  102. }
  103. // Unmaps a region of the buffer.
  104. // mappedPtr must be a pointer returned by a previous call to map.
  105. void unmap(void* mappedPtr)
  106. {
  107. oidnUnmapBuffer(handle, mappedPtr);
  108. }
  109. };
  110. // --------------------------------------------------------------------------
  111. // Filter
  112. // --------------------------------------------------------------------------
  113. // Progress monitor callback function
  114. typedef bool (*ProgressMonitorFunction)(void* userPtr, double n);
  115. // Filter object with automatic reference counting
  116. class FilterRef
  117. {
  118. private:
  119. OIDNFilter handle;
  120. public:
  121. FilterRef() : handle(nullptr) {}
  122. FilterRef(OIDNFilter handle) : handle(handle) {}
  123. FilterRef(const FilterRef& other) : handle(other.handle)
  124. {
  125. if (handle)
  126. oidnRetainFilter(handle);
  127. }
  128. FilterRef(FilterRef&& other) : handle(other.handle)
  129. {
  130. other.handle = nullptr;
  131. }
  132. FilterRef& operator =(const FilterRef& other)
  133. {
  134. if (&other != this)
  135. {
  136. if (other.handle)
  137. oidnRetainFilter(other.handle);
  138. if (handle)
  139. oidnReleaseFilter(handle);
  140. handle = other.handle;
  141. }
  142. return *this;
  143. }
  144. FilterRef& operator =(FilterRef&& other)
  145. {
  146. std::swap(handle, other.handle);
  147. return *this;
  148. }
  149. FilterRef& operator =(OIDNFilter other)
  150. {
  151. if (other)
  152. oidnRetainFilter(other);
  153. if (handle)
  154. oidnReleaseFilter(handle);
  155. handle = other;
  156. return *this;
  157. }
  158. ~FilterRef()
  159. {
  160. if (handle)
  161. oidnReleaseFilter(handle);
  162. }
  163. OIDNFilter getHandle() const
  164. {
  165. return handle;
  166. }
  167. operator bool() const
  168. {
  169. return handle != nullptr;
  170. }
  171. // Sets an image parameter of the filter (stored in a buffer).
  172. void setImage(const char* name,
  173. const BufferRef& buffer, Format format,
  174. size_t width, size_t height,
  175. size_t byteOffset = 0,
  176. size_t bytePixelStride = 0, size_t byteRowStride = 0)
  177. {
  178. oidnSetFilterImage(handle, name,
  179. buffer.getHandle(), (OIDNFormat)format,
  180. width, height,
  181. byteOffset,
  182. bytePixelStride, byteRowStride);
  183. }
  184. // Sets an image parameter of the filter (owned by the user).
  185. void setImage(const char* name,
  186. void* ptr, Format format,
  187. size_t width, size_t height,
  188. size_t byteOffset = 0,
  189. size_t bytePixelStride = 0, size_t byteRowStride = 0)
  190. {
  191. oidnSetSharedFilterImage(handle, name,
  192. ptr, (OIDNFormat)format,
  193. width, height,
  194. byteOffset,
  195. bytePixelStride, byteRowStride);
  196. }
  197. // Sets a boolean parameter of the filter.
  198. void set(const char* name, bool value)
  199. {
  200. oidnSetFilter1b(handle, name, value);
  201. }
  202. // Sets an integer parameter of the filter.
  203. void set(const char* name, int value)
  204. {
  205. oidnSetFilter1i(handle, name, value);
  206. }
  207. // Sets a float parameter of the filter.
  208. void set(const char* name, float value)
  209. {
  210. oidnSetFilter1f(handle, name, value);
  211. }
  212. // Gets a parameter of the filter.
  213. template<typename T>
  214. T get(const char* name);
  215. // Sets the progress monitor callback function of the filter.
  216. void setProgressMonitorFunction(ProgressMonitorFunction func, void* userPtr = nullptr)
  217. {
  218. oidnSetFilterProgressMonitorFunction(handle, (OIDNProgressMonitorFunction)func, userPtr);
  219. }
  220. // Commits all previous changes to the filter.
  221. void commit()
  222. {
  223. oidnCommitFilter(handle);
  224. }
  225. // Executes the filter.
  226. void execute()
  227. {
  228. oidnExecuteFilter(handle);
  229. }
  230. };
  231. // Gets a boolean parameter of the filter.
  232. template<>
  233. inline bool FilterRef::get(const char* name)
  234. {
  235. return oidnGetFilter1b(handle, name);
  236. }
  237. // Gets an integer parameter of the filter.
  238. template<>
  239. inline int FilterRef::get(const char* name)
  240. {
  241. return oidnGetFilter1i(handle, name);
  242. }
  243. // Gets a float parameter of the filter.
  244. template<>
  245. inline float FilterRef::get(const char* name)
  246. {
  247. return oidnGetFilter1f(handle, name);
  248. }
  249. // --------------------------------------------------------------------------
  250. // Device
  251. // --------------------------------------------------------------------------
  252. // Device types
  253. enum class DeviceType
  254. {
  255. Default = OIDN_DEVICE_TYPE_DEFAULT, // select device automatically
  256. CPU = OIDN_DEVICE_TYPE_CPU, // CPU device
  257. };
  258. // Error codes
  259. enum class Error
  260. {
  261. None = OIDN_ERROR_NONE, // no error occurred
  262. Unknown = OIDN_ERROR_UNKNOWN, // an unknown error occurred
  263. InvalidArgument = OIDN_ERROR_INVALID_ARGUMENT, // an invalid argument was specified
  264. InvalidOperation = OIDN_ERROR_INVALID_OPERATION, // the operation is not allowed
  265. OutOfMemory = OIDN_ERROR_OUT_OF_MEMORY, // not enough memory to execute the operation
  266. UnsupportedHardware = OIDN_ERROR_UNSUPPORTED_HARDWARE, // the hardware (e.g. CPU) is not supported
  267. Cancelled = OIDN_ERROR_CANCELLED, // the operation was cancelled by the user
  268. };
  269. // Error callback function
  270. typedef void (*ErrorFunction)(void* userPtr, Error code, const char* message);
  271. // Device object with automatic reference counting
  272. class DeviceRef
  273. {
  274. private:
  275. OIDNDevice handle;
  276. public:
  277. DeviceRef() : handle(nullptr) {}
  278. DeviceRef(OIDNDevice handle) : handle(handle) {}
  279. DeviceRef(const DeviceRef& other) : handle(other.handle)
  280. {
  281. if (handle)
  282. oidnRetainDevice(handle);
  283. }
  284. DeviceRef(DeviceRef&& other) : handle(other.handle)
  285. {
  286. other.handle = nullptr;
  287. }
  288. DeviceRef& operator =(const DeviceRef& other)
  289. {
  290. if (&other != this)
  291. {
  292. if (other.handle)
  293. oidnRetainDevice(other.handle);
  294. if (handle)
  295. oidnReleaseDevice(handle);
  296. handle = other.handle;
  297. }
  298. return *this;
  299. }
  300. DeviceRef& operator =(DeviceRef&& other)
  301. {
  302. std::swap(handle, other.handle);
  303. return *this;
  304. }
  305. DeviceRef& operator =(OIDNDevice other)
  306. {
  307. if (other)
  308. oidnRetainDevice(other);
  309. if (handle)
  310. oidnReleaseDevice(handle);
  311. handle = other;
  312. return *this;
  313. }
  314. ~DeviceRef()
  315. {
  316. if (handle)
  317. oidnReleaseDevice(handle);
  318. }
  319. OIDNDevice getHandle() const
  320. {
  321. return handle;
  322. }
  323. operator bool() const
  324. {
  325. return handle != nullptr;
  326. }
  327. // Sets a boolean parameter of the device.
  328. void set(const char* name, bool value)
  329. {
  330. oidnSetDevice1b(handle, name, value);
  331. }
  332. // Sets an integer parameter of the device.
  333. void set(const char* name, int value)
  334. {
  335. oidnSetDevice1i(handle, name, value);
  336. }
  337. // Gets a parameter of the device.
  338. template<typename T>
  339. T get(const char* name);
  340. // Sets the error callback function of the device.
  341. void setErrorFunction(ErrorFunction func, void* userPtr = nullptr)
  342. {
  343. oidnSetDeviceErrorFunction(handle, (OIDNErrorFunction)func, userPtr);
  344. }
  345. // Returns the first unqueried error code and clears the stored error.
  346. // Can be called for a null device as well to check why a device creation failed.
  347. Error getError()
  348. {
  349. return (Error)oidnGetDeviceError(handle, nullptr);
  350. }
  351. // Returns the first unqueried error code and string message, and clears the stored error.
  352. // Can be called for a null device as well to check why a device creation failed.
  353. Error getError(const char*& outMessage)
  354. {
  355. return (Error)oidnGetDeviceError(handle, &outMessage);
  356. }
  357. // Commits all previous changes to the device.
  358. // Must be called before first using the device (e.g. creating filters).
  359. void commit()
  360. {
  361. oidnCommitDevice(handle);
  362. }
  363. // Creates a new buffer (data allocated and owned by the device).
  364. BufferRef newBuffer(size_t byteSize)
  365. {
  366. return oidnNewBuffer(handle, byteSize);
  367. }
  368. // Creates a new shared buffer (data allocated and owned by the user).
  369. BufferRef newBuffer(void* ptr, size_t byteSize)
  370. {
  371. return oidnNewSharedBuffer(handle, ptr, byteSize);
  372. }
  373. // Creates a new filter of the specified type (e.g. "RT").
  374. FilterRef newFilter(const char* type)
  375. {
  376. return oidnNewFilter(handle, type);
  377. }
  378. };
  379. // Gets a boolean parameter of the device.
  380. template<>
  381. inline bool DeviceRef::get(const char* name)
  382. {
  383. return oidnGetDevice1b(handle, name);
  384. }
  385. // Gets an integer parameter of the device (e.g. "version").
  386. template<>
  387. inline int DeviceRef::get(const char* name)
  388. {
  389. return oidnGetDevice1i(handle, name);
  390. }
  391. // Creates a new device.
  392. inline DeviceRef newDevice(DeviceType type = DeviceType::Default)
  393. {
  394. return DeviceRef(oidnNewDevice((OIDNDeviceType)type));
  395. }
  396. } // namespace oidn