ocl.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the OpenCV Foundation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef OPENCV_OPENCL_HPP
  42. #define OPENCV_OPENCL_HPP
  43. #include "opencv2/core.hpp"
  44. namespace cv { namespace ocl {
  45. //! @addtogroup core_opencl
  46. //! @{
  47. CV_EXPORTS_W bool haveOpenCL();
  48. CV_EXPORTS_W bool useOpenCL();
  49. CV_EXPORTS_W bool haveAmdBlas();
  50. CV_EXPORTS_W bool haveAmdFft();
  51. CV_EXPORTS_W void setUseOpenCL(bool flag);
  52. CV_EXPORTS_W void finish();
  53. CV_EXPORTS bool haveSVM();
  54. class CV_EXPORTS Context;
  55. class CV_EXPORTS_W_SIMPLE Device;
  56. class CV_EXPORTS Kernel;
  57. class CV_EXPORTS Program;
  58. class CV_EXPORTS ProgramSource;
  59. class CV_EXPORTS Queue;
  60. class CV_EXPORTS PlatformInfo;
  61. class CV_EXPORTS Image2D;
  62. class CV_EXPORTS_W_SIMPLE Device
  63. {
  64. public:
  65. CV_WRAP Device();
  66. explicit Device(void* d);
  67. Device(const Device& d);
  68. Device& operator = (const Device& d);
  69. CV_WRAP ~Device();
  70. void set(void* d);
  71. enum
  72. {
  73. TYPE_DEFAULT = (1 << 0),
  74. TYPE_CPU = (1 << 1),
  75. TYPE_GPU = (1 << 2),
  76. TYPE_ACCELERATOR = (1 << 3),
  77. TYPE_DGPU = TYPE_GPU + (1 << 16),
  78. TYPE_IGPU = TYPE_GPU + (1 << 17),
  79. TYPE_ALL = 0xFFFFFFFF
  80. };
  81. CV_WRAP String name() const;
  82. CV_WRAP String extensions() const;
  83. CV_WRAP bool isExtensionSupported(const String& extensionName) const;
  84. CV_WRAP String version() const;
  85. CV_WRAP String vendorName() const;
  86. CV_WRAP String OpenCL_C_Version() const;
  87. CV_WRAP String OpenCLVersion() const;
  88. CV_WRAP int deviceVersionMajor() const;
  89. CV_WRAP int deviceVersionMinor() const;
  90. CV_WRAP String driverVersion() const;
  91. void* ptr() const;
  92. CV_WRAP int type() const;
  93. CV_WRAP int addressBits() const;
  94. CV_WRAP bool available() const;
  95. CV_WRAP bool compilerAvailable() const;
  96. CV_WRAP bool linkerAvailable() const;
  97. enum
  98. {
  99. FP_DENORM=(1 << 0),
  100. FP_INF_NAN=(1 << 1),
  101. FP_ROUND_TO_NEAREST=(1 << 2),
  102. FP_ROUND_TO_ZERO=(1 << 3),
  103. FP_ROUND_TO_INF=(1 << 4),
  104. FP_FMA=(1 << 5),
  105. FP_SOFT_FLOAT=(1 << 6),
  106. FP_CORRECTLY_ROUNDED_DIVIDE_SQRT=(1 << 7)
  107. };
  108. CV_WRAP int doubleFPConfig() const;
  109. CV_WRAP int singleFPConfig() const;
  110. CV_WRAP int halfFPConfig() const;
  111. CV_WRAP bool endianLittle() const;
  112. CV_WRAP bool errorCorrectionSupport() const;
  113. enum
  114. {
  115. EXEC_KERNEL=(1 << 0),
  116. EXEC_NATIVE_KERNEL=(1 << 1)
  117. };
  118. CV_WRAP int executionCapabilities() const;
  119. CV_WRAP size_t globalMemCacheSize() const;
  120. enum
  121. {
  122. NO_CACHE=0,
  123. READ_ONLY_CACHE=1,
  124. READ_WRITE_CACHE=2
  125. };
  126. CV_WRAP int globalMemCacheType() const;
  127. CV_WRAP int globalMemCacheLineSize() const;
  128. CV_WRAP size_t globalMemSize() const;
  129. CV_WRAP size_t localMemSize() const;
  130. enum
  131. {
  132. NO_LOCAL_MEM=0,
  133. LOCAL_IS_LOCAL=1,
  134. LOCAL_IS_GLOBAL=2
  135. };
  136. CV_WRAP int localMemType() const;
  137. CV_WRAP bool hostUnifiedMemory() const;
  138. CV_WRAP bool imageSupport() const;
  139. CV_WRAP bool imageFromBufferSupport() const;
  140. uint imagePitchAlignment() const;
  141. uint imageBaseAddressAlignment() const;
  142. /// deprecated, use isExtensionSupported() method (probably with "cl_khr_subgroups" value)
  143. CV_WRAP bool intelSubgroupsSupport() const;
  144. CV_WRAP size_t image2DMaxWidth() const;
  145. CV_WRAP size_t image2DMaxHeight() const;
  146. CV_WRAP size_t image3DMaxWidth() const;
  147. CV_WRAP size_t image3DMaxHeight() const;
  148. CV_WRAP size_t image3DMaxDepth() const;
  149. CV_WRAP size_t imageMaxBufferSize() const;
  150. CV_WRAP size_t imageMaxArraySize() const;
  151. enum
  152. {
  153. UNKNOWN_VENDOR=0,
  154. VENDOR_AMD=1,
  155. VENDOR_INTEL=2,
  156. VENDOR_NVIDIA=3
  157. };
  158. CV_WRAP int vendorID() const;
  159. // FIXIT
  160. // dev.isAMD() doesn't work for OpenCL CPU devices from AMD OpenCL platform.
  161. // This method should use platform name instead of vendor name.
  162. // After fix restore code in arithm.cpp: ocl_compare()
  163. CV_WRAP inline bool isAMD() const { return vendorID() == VENDOR_AMD; }
  164. CV_WRAP inline bool isIntel() const { return vendorID() == VENDOR_INTEL; }
  165. CV_WRAP inline bool isNVidia() const { return vendorID() == VENDOR_NVIDIA; }
  166. CV_WRAP int maxClockFrequency() const;
  167. CV_WRAP int maxComputeUnits() const;
  168. CV_WRAP int maxConstantArgs() const;
  169. CV_WRAP size_t maxConstantBufferSize() const;
  170. CV_WRAP size_t maxMemAllocSize() const;
  171. CV_WRAP size_t maxParameterSize() const;
  172. CV_WRAP int maxReadImageArgs() const;
  173. CV_WRAP int maxWriteImageArgs() const;
  174. CV_WRAP int maxSamplers() const;
  175. CV_WRAP size_t maxWorkGroupSize() const;
  176. CV_WRAP int maxWorkItemDims() const;
  177. void maxWorkItemSizes(size_t*) const;
  178. CV_WRAP int memBaseAddrAlign() const;
  179. CV_WRAP int nativeVectorWidthChar() const;
  180. CV_WRAP int nativeVectorWidthShort() const;
  181. CV_WRAP int nativeVectorWidthInt() const;
  182. CV_WRAP int nativeVectorWidthLong() const;
  183. CV_WRAP int nativeVectorWidthFloat() const;
  184. CV_WRAP int nativeVectorWidthDouble() const;
  185. CV_WRAP int nativeVectorWidthHalf() const;
  186. CV_WRAP int preferredVectorWidthChar() const;
  187. CV_WRAP int preferredVectorWidthShort() const;
  188. CV_WRAP int preferredVectorWidthInt() const;
  189. CV_WRAP int preferredVectorWidthLong() const;
  190. CV_WRAP int preferredVectorWidthFloat() const;
  191. CV_WRAP int preferredVectorWidthDouble() const;
  192. CV_WRAP int preferredVectorWidthHalf() const;
  193. CV_WRAP size_t printfBufferSize() const;
  194. CV_WRAP size_t profilingTimerResolution() const;
  195. CV_WRAP static const Device& getDefault();
  196. protected:
  197. struct Impl;
  198. Impl* p;
  199. };
  200. class CV_EXPORTS Context
  201. {
  202. public:
  203. Context();
  204. explicit Context(int dtype);
  205. ~Context();
  206. Context(const Context& c);
  207. Context& operator = (const Context& c);
  208. bool create();
  209. bool create(int dtype);
  210. size_t ndevices() const;
  211. const Device& device(size_t idx) const;
  212. Program getProg(const ProgramSource& prog,
  213. const String& buildopt, String& errmsg);
  214. void unloadProg(Program& prog);
  215. static Context& getDefault(bool initialize = true);
  216. void* ptr() const;
  217. friend void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  218. bool useSVM() const;
  219. void setUseSVM(bool enabled);
  220. struct Impl;
  221. inline Impl* getImpl() const { return (Impl*)p; }
  222. //protected:
  223. Impl* p;
  224. };
  225. class CV_EXPORTS Platform
  226. {
  227. public:
  228. Platform();
  229. ~Platform();
  230. Platform(const Platform& p);
  231. Platform& operator = (const Platform& p);
  232. void* ptr() const;
  233. static Platform& getDefault();
  234. friend void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  235. protected:
  236. struct Impl;
  237. Impl* p;
  238. };
  239. /** @brief Attaches OpenCL context to OpenCV
  240. @note
  241. OpenCV will check if available OpenCL platform has platformName name, then assign context to
  242. OpenCV and call `clRetainContext` function. The deviceID device will be used as target device and
  243. new command queue will be created.
  244. @param platformName name of OpenCL platform to attach, this string is used to check if platform is available to OpenCV at runtime
  245. @param platformID ID of platform attached context was created for
  246. @param context OpenCL context to be attached to OpenCV
  247. @param deviceID ID of device, must be created from attached context
  248. */
  249. CV_EXPORTS void attachContext(const String& platformName, void* platformID, void* context, void* deviceID);
  250. /** @brief Convert OpenCL buffer to UMat
  251. @note
  252. OpenCL buffer (cl_mem_buffer) should contain 2D image data, compatible with OpenCV. Memory
  253. content is not copied from `clBuffer` to UMat. Instead, buffer handle assigned to UMat and
  254. `clRetainMemObject` is called.
  255. @param cl_mem_buffer source clBuffer handle
  256. @param step num of bytes in single row
  257. @param rows number of rows
  258. @param cols number of cols
  259. @param type OpenCV type of image
  260. @param dst destination UMat
  261. */
  262. CV_EXPORTS void convertFromBuffer(void* cl_mem_buffer, size_t step, int rows, int cols, int type, UMat& dst);
  263. /** @brief Convert OpenCL image2d_t to UMat
  264. @note
  265. OpenCL `image2d_t` (cl_mem_image), should be compatible with OpenCV UMat formats. Memory content
  266. is copied from image to UMat with `clEnqueueCopyImageToBuffer` function.
  267. @param cl_mem_image source image2d_t handle
  268. @param dst destination UMat
  269. */
  270. CV_EXPORTS void convertFromImage(void* cl_mem_image, UMat& dst);
  271. // TODO Move to internal header
  272. void initializeContextFromHandle(Context& ctx, void* platform, void* context, void* device);
  273. class CV_EXPORTS Queue
  274. {
  275. public:
  276. Queue();
  277. explicit Queue(const Context& c, const Device& d=Device());
  278. ~Queue();
  279. Queue(const Queue& q);
  280. Queue& operator = (const Queue& q);
  281. bool create(const Context& c=Context(), const Device& d=Device());
  282. void finish();
  283. void* ptr() const;
  284. static Queue& getDefault();
  285. /// @brief Returns OpenCL command queue with enable profiling mode support
  286. const Queue& getProfilingQueue() const;
  287. struct Impl; friend struct Impl;
  288. inline Impl* getImpl() const { return p; }
  289. protected:
  290. Impl* p;
  291. };
  292. class CV_EXPORTS KernelArg
  293. {
  294. public:
  295. enum { LOCAL=1, READ_ONLY=2, WRITE_ONLY=4, READ_WRITE=6, CONSTANT=8, PTR_ONLY = 16, NO_SIZE=256 };
  296. KernelArg(int _flags, UMat* _m, int wscale=1, int iwscale=1, const void* _obj=0, size_t _sz=0);
  297. KernelArg();
  298. static KernelArg Local(size_t localMemSize)
  299. { return KernelArg(LOCAL, 0, 1, 1, 0, localMemSize); }
  300. static KernelArg PtrWriteOnly(const UMat& m)
  301. { return KernelArg(PTR_ONLY+WRITE_ONLY, (UMat*)&m); }
  302. static KernelArg PtrReadOnly(const UMat& m)
  303. { return KernelArg(PTR_ONLY+READ_ONLY, (UMat*)&m); }
  304. static KernelArg PtrReadWrite(const UMat& m)
  305. { return KernelArg(PTR_ONLY+READ_WRITE, (UMat*)&m); }
  306. static KernelArg ReadWrite(const UMat& m, int wscale=1, int iwscale=1)
  307. { return KernelArg(READ_WRITE, (UMat*)&m, wscale, iwscale); }
  308. static KernelArg ReadWriteNoSize(const UMat& m, int wscale=1, int iwscale=1)
  309. { return KernelArg(READ_WRITE+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  310. static KernelArg ReadOnly(const UMat& m, int wscale=1, int iwscale=1)
  311. { return KernelArg(READ_ONLY, (UMat*)&m, wscale, iwscale); }
  312. static KernelArg WriteOnly(const UMat& m, int wscale=1, int iwscale=1)
  313. { return KernelArg(WRITE_ONLY, (UMat*)&m, wscale, iwscale); }
  314. static KernelArg ReadOnlyNoSize(const UMat& m, int wscale=1, int iwscale=1)
  315. { return KernelArg(READ_ONLY+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  316. static KernelArg WriteOnlyNoSize(const UMat& m, int wscale=1, int iwscale=1)
  317. { return KernelArg(WRITE_ONLY+NO_SIZE, (UMat*)&m, wscale, iwscale); }
  318. static KernelArg Constant(const Mat& m);
  319. template<typename _Tp> static KernelArg Constant(const _Tp* arr, size_t n)
  320. { return KernelArg(CONSTANT, 0, 1, 1, (void*)arr, n); }
  321. int flags;
  322. UMat* m;
  323. const void* obj;
  324. size_t sz;
  325. int wscale, iwscale;
  326. };
  327. class CV_EXPORTS Kernel
  328. {
  329. public:
  330. Kernel();
  331. Kernel(const char* kname, const Program& prog);
  332. Kernel(const char* kname, const ProgramSource& prog,
  333. const String& buildopts = String(), String* errmsg=0);
  334. ~Kernel();
  335. Kernel(const Kernel& k);
  336. Kernel& operator = (const Kernel& k);
  337. bool empty() const;
  338. bool create(const char* kname, const Program& prog);
  339. bool create(const char* kname, const ProgramSource& prog,
  340. const String& buildopts, String* errmsg=0);
  341. int set(int i, const void* value, size_t sz);
  342. int set(int i, const Image2D& image2D);
  343. int set(int i, const UMat& m);
  344. int set(int i, const KernelArg& arg);
  345. template<typename _Tp> int set(int i, const _Tp& value)
  346. { return set(i, &value, sizeof(value)); }
  347. protected:
  348. template<typename _Tp0> inline
  349. int set_args_(int i, const _Tp0& a0) { return set(i, a0); }
  350. template<typename _Tp0, typename... _Tps> inline
  351. int set_args_(int i, const _Tp0& a0, const _Tps&... rest_args) { i = set(i, a0); return set_args_(i, rest_args...); }
  352. public:
  353. /** @brief Setup OpenCL Kernel arguments.
  354. Avoid direct using of set(i, ...) methods.
  355. @code
  356. bool ok = kernel
  357. .args(
  358. srcUMat, dstUMat,
  359. (float)some_float_param
  360. ).run(ndims, globalSize, localSize);
  361. if (!ok) return false;
  362. @endcode
  363. */
  364. template<typename... _Tps> inline
  365. Kernel& args(const _Tps&... kernel_args) { set_args_(0, kernel_args...); return *this; }
  366. /** @brief Run the OpenCL kernel.
  367. @param dims the work problem dimensions. It is the length of globalsize and localsize. It can be either 1, 2 or 3.
  368. @param globalsize work items for each dimension. It is not the final globalsize passed to
  369. OpenCL. Each dimension will be adjusted to the nearest integer divisible by the corresponding
  370. value in localsize. If localsize is NULL, it will still be adjusted depending on dims. The
  371. adjusted values are greater than or equal to the original values.
  372. @param localsize work-group size for each dimension.
  373. @param sync specify whether to wait for OpenCL computation to finish before return.
  374. @param q command queue
  375. */
  376. bool run(int dims, size_t globalsize[],
  377. size_t localsize[], bool sync, const Queue& q=Queue());
  378. bool runTask(bool sync, const Queue& q=Queue());
  379. /** @brief Similar to synchronized run() call with returning of kernel execution time
  380. * Separate OpenCL command queue may be used (with CL_QUEUE_PROFILING_ENABLE)
  381. * @return Execution time in nanoseconds or negative number on error
  382. */
  383. int64 runProfiling(int dims, size_t globalsize[], size_t localsize[], const Queue& q=Queue());
  384. size_t workGroupSize() const;
  385. size_t preferedWorkGroupSizeMultiple() const;
  386. bool compileWorkGroupSize(size_t wsz[]) const;
  387. size_t localMemSize() const;
  388. void* ptr() const;
  389. struct Impl;
  390. protected:
  391. Impl* p;
  392. };
  393. class CV_EXPORTS Program
  394. {
  395. public:
  396. Program();
  397. Program(const ProgramSource& src,
  398. const String& buildflags, String& errmsg);
  399. Program(const Program& prog);
  400. Program& operator = (const Program& prog);
  401. ~Program();
  402. bool create(const ProgramSource& src,
  403. const String& buildflags, String& errmsg);
  404. void* ptr() const;
  405. /**
  406. * @brief Query device-specific program binary.
  407. *
  408. * Returns RAW OpenCL executable binary without additional attachments.
  409. *
  410. * @sa ProgramSource::fromBinary
  411. *
  412. * @param[out] binary output buffer
  413. */
  414. void getBinary(std::vector<char>& binary) const;
  415. struct Impl; friend struct Impl;
  416. inline Impl* getImpl() const { return (Impl*)p; }
  417. protected:
  418. Impl* p;
  419. public:
  420. #ifndef OPENCV_REMOVE_DEPRECATED_API
  421. // TODO Remove this
  422. CV_DEPRECATED bool read(const String& buf, const String& buildflags); // removed, use ProgramSource instead
  423. CV_DEPRECATED bool write(String& buf) const; // removed, use getBinary() method instead (RAW OpenCL binary)
  424. CV_DEPRECATED const ProgramSource& source() const; // implementation removed
  425. CV_DEPRECATED String getPrefix() const; // deprecated, implementation replaced
  426. CV_DEPRECATED static String getPrefix(const String& buildflags); // deprecated, implementation replaced
  427. #endif
  428. };
  429. class CV_EXPORTS ProgramSource
  430. {
  431. public:
  432. typedef uint64 hash_t; // deprecated
  433. ProgramSource();
  434. explicit ProgramSource(const String& module, const String& name, const String& codeStr, const String& codeHash);
  435. explicit ProgramSource(const String& prog); // deprecated
  436. explicit ProgramSource(const char* prog); // deprecated
  437. ~ProgramSource();
  438. ProgramSource(const ProgramSource& prog);
  439. ProgramSource& operator = (const ProgramSource& prog);
  440. const String& source() const; // deprecated
  441. hash_t hash() const; // deprecated
  442. /** @brief Describe OpenCL program binary.
  443. * Do not call clCreateProgramWithBinary() and/or clBuildProgram().
  444. *
  445. * Caller should guarantee binary buffer lifetime greater than ProgramSource object (and any of its copies).
  446. *
  447. * This kind of binary is not portable between platforms in general - it is specific to OpenCL vendor / device / driver version.
  448. *
  449. * @param module name of program owner module
  450. * @param name unique name of program (module+name is used as key for OpenCL program caching)
  451. * @param binary buffer address. See buffer lifetime requirement in description.
  452. * @param size buffer size
  453. * @param buildOptions additional program-related build options passed to clBuildProgram()
  454. * @return created ProgramSource object
  455. */
  456. static ProgramSource fromBinary(const String& module, const String& name,
  457. const unsigned char* binary, const size_t size,
  458. const cv::String& buildOptions = cv::String());
  459. /** @brief Describe OpenCL program in SPIR format.
  460. * Do not call clCreateProgramWithBinary() and/or clBuildProgram().
  461. *
  462. * Supports SPIR 1.2 by default (pass '-spir-std=X.Y' in buildOptions to override this behavior)
  463. *
  464. * Caller should guarantee binary buffer lifetime greater than ProgramSource object (and any of its copies).
  465. *
  466. * Programs in this format are portable between OpenCL implementations with 'khr_spir' extension:
  467. * https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/cl_khr_spir.html
  468. * (but they are not portable between different platforms: 32-bit / 64-bit)
  469. *
  470. * Note: these programs can't support vendor specific extensions, like 'cl_intel_subgroups'.
  471. *
  472. * @param module name of program owner module
  473. * @param name unique name of program (module+name is used as key for OpenCL program caching)
  474. * @param binary buffer address. See buffer lifetime requirement in description.
  475. * @param size buffer size
  476. * @param buildOptions additional program-related build options passed to clBuildProgram()
  477. * (these options are added automatically: '-x spir' and '-spir-std=1.2')
  478. * @return created ProgramSource object.
  479. */
  480. static ProgramSource fromSPIR(const String& module, const String& name,
  481. const unsigned char* binary, const size_t size,
  482. const cv::String& buildOptions = cv::String());
  483. //OpenCL 2.1+ only
  484. //static Program fromSPIRV(const String& module, const String& name,
  485. // const unsigned char* binary, const size_t size,
  486. // const cv::String& buildOptions = cv::String());
  487. struct Impl; friend struct Impl;
  488. inline Impl* getImpl() const { return (Impl*)p; }
  489. protected:
  490. Impl* p;
  491. };
  492. class CV_EXPORTS PlatformInfo
  493. {
  494. public:
  495. PlatformInfo();
  496. explicit PlatformInfo(void* id);
  497. ~PlatformInfo();
  498. PlatformInfo(const PlatformInfo& i);
  499. PlatformInfo& operator =(const PlatformInfo& i);
  500. String name() const;
  501. String vendor() const;
  502. String version() const;
  503. int deviceNumber() const;
  504. void getDevice(Device& device, int d) const;
  505. protected:
  506. struct Impl;
  507. Impl* p;
  508. };
  509. CV_EXPORTS const char* convertTypeStr(int sdepth, int ddepth, int cn, char* buf);
  510. CV_EXPORTS const char* typeToStr(int t);
  511. CV_EXPORTS const char* memopTypeToStr(int t);
  512. CV_EXPORTS const char* vecopTypeToStr(int t);
  513. CV_EXPORTS const char* getOpenCLErrorString(int errorCode);
  514. CV_EXPORTS String kernelToStr(InputArray _kernel, int ddepth = -1, const char * name = NULL);
  515. CV_EXPORTS void getPlatfomsInfo(std::vector<PlatformInfo>& platform_info);
  516. enum OclVectorStrategy
  517. {
  518. // all matrices have its own vector width
  519. OCL_VECTOR_OWN = 0,
  520. // all matrices have maximal vector width among all matrices
  521. // (useful for cases when matrices have different data types)
  522. OCL_VECTOR_MAX = 1,
  523. // default strategy
  524. OCL_VECTOR_DEFAULT = OCL_VECTOR_OWN
  525. };
  526. CV_EXPORTS int predictOptimalVectorWidth(InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  527. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  528. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray(),
  529. OclVectorStrategy strat = OCL_VECTOR_DEFAULT);
  530. CV_EXPORTS int checkOptimalVectorWidth(const int *vectorWidths,
  531. InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  532. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  533. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray(),
  534. OclVectorStrategy strat = OCL_VECTOR_DEFAULT);
  535. // with OCL_VECTOR_MAX strategy
  536. CV_EXPORTS int predictOptimalVectorWidthMax(InputArray src1, InputArray src2 = noArray(), InputArray src3 = noArray(),
  537. InputArray src4 = noArray(), InputArray src5 = noArray(), InputArray src6 = noArray(),
  538. InputArray src7 = noArray(), InputArray src8 = noArray(), InputArray src9 = noArray());
  539. CV_EXPORTS void buildOptionsAddMatrixDescription(String& buildOptions, const String& name, InputArray _m);
  540. class CV_EXPORTS Image2D
  541. {
  542. public:
  543. Image2D();
  544. /**
  545. @param src UMat object from which to get image properties and data
  546. @param norm flag to enable the use of normalized channel data types
  547. @param alias flag indicating that the image should alias the src UMat. If true, changes to the
  548. image or src will be reflected in both objects.
  549. */
  550. explicit Image2D(const UMat &src, bool norm = false, bool alias = false);
  551. Image2D(const Image2D & i);
  552. ~Image2D();
  553. Image2D & operator = (const Image2D & i);
  554. /** Indicates if creating an aliased image should succeed.
  555. Depends on the underlying platform and the dimensions of the UMat.
  556. */
  557. static bool canCreateAlias(const UMat &u);
  558. /** Indicates if the image format is supported.
  559. */
  560. static bool isFormatSupported(int depth, int cn, bool norm);
  561. void* ptr() const;
  562. protected:
  563. struct Impl;
  564. Impl* p;
  565. };
  566. class CV_EXPORTS Timer
  567. {
  568. public:
  569. Timer(const Queue& q);
  570. ~Timer();
  571. void start();
  572. void stop();
  573. uint64 durationNS() const; //< duration in nanoseconds
  574. protected:
  575. struct Impl;
  576. Impl* const p;
  577. private:
  578. Timer(const Timer&); // disabled
  579. Timer& operator=(const Timer&); // disabled
  580. };
  581. CV_EXPORTS MatAllocator* getOpenCLAllocator();
  582. #ifdef __OPENCV_BUILD
  583. namespace internal {
  584. CV_EXPORTS bool isOpenCLForced();
  585. #define OCL_FORCE_CHECK(condition) (cv::ocl::internal::isOpenCLForced() || (condition))
  586. CV_EXPORTS bool isPerformanceCheckBypassed();
  587. #define OCL_PERFORMANCE_CHECK(condition) (cv::ocl::internal::isPerformanceCheckBypassed() || (condition))
  588. CV_EXPORTS bool isCLBuffer(UMat& u);
  589. } // namespace internal
  590. #endif
  591. //! @}
  592. }}
  593. #endif