test_opengl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #if defined(HAVE_CUDA) && defined(HAVE_OPENGL)
  44. #include "opencv2/core/cuda.hpp"
  45. #include "opencv2/core/opengl.hpp"
  46. #include "opencv2/ts/cuda_test.hpp"
  47. namespace opencv_test { namespace {
  48. /////////////////////////////////////////////
  49. // Buffer
  50. PARAM_TEST_CASE(Buffer, cv::Size, MatType)
  51. {
  52. static void SetUpTestCase()
  53. {
  54. cv::namedWindow("test", cv::WINDOW_OPENGL);
  55. }
  56. static void TearDownTestCase()
  57. {
  58. cv::destroyAllWindows();
  59. }
  60. cv::Size size;
  61. int type;
  62. virtual void SetUp()
  63. {
  64. size = GET_PARAM(0);
  65. type = GET_PARAM(1);
  66. }
  67. };
  68. CUDA_TEST_P(Buffer, Constructor1)
  69. {
  70. cv::ogl::Buffer buf(size.height, size.width, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
  71. EXPECT_EQ(size.height, buf.rows());
  72. EXPECT_EQ(size.width, buf.cols());
  73. EXPECT_EQ(type, buf.type());
  74. }
  75. CUDA_TEST_P(Buffer, Constructor2)
  76. {
  77. cv::ogl::Buffer buf(size, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
  78. EXPECT_EQ(size.height, buf.rows());
  79. EXPECT_EQ(size.width, buf.cols());
  80. EXPECT_EQ(type, buf.type());
  81. }
  82. CUDA_TEST_P(Buffer, ConstructorFromMat)
  83. {
  84. cv::Mat gold = randomMat(size, type);
  85. cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  86. cv::Mat bufData;
  87. buf.copyTo(bufData);
  88. EXPECT_MAT_NEAR(gold, bufData, 0);
  89. }
  90. CUDA_TEST_P(Buffer, ConstructorFromGpuMat)
  91. {
  92. cv::Mat gold = randomMat(size, type);
  93. cv::cuda::GpuMat d_gold(gold);
  94. cv::ogl::Buffer buf(d_gold, cv::ogl::Buffer::ARRAY_BUFFER);
  95. cv::Mat bufData;
  96. buf.copyTo(bufData);
  97. EXPECT_MAT_NEAR(gold, bufData, 0);
  98. }
  99. CUDA_TEST_P(Buffer, ConstructorFromBuffer)
  100. {
  101. cv::ogl::Buffer buf_gold(size, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
  102. cv::ogl::Buffer buf(buf_gold);
  103. EXPECT_EQ(buf_gold.bufId(), buf.bufId());
  104. EXPECT_EQ(buf_gold.rows(), buf.rows());
  105. EXPECT_EQ(buf_gold.cols(), buf.cols());
  106. EXPECT_EQ(buf_gold.type(), buf.type());
  107. }
  108. CUDA_TEST_P(Buffer, Create)
  109. {
  110. cv::ogl::Buffer buf;
  111. buf.create(size.height, size.width, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
  112. EXPECT_EQ(size.height, buf.rows());
  113. EXPECT_EQ(size.width, buf.cols());
  114. EXPECT_EQ(type, buf.type());
  115. }
  116. CUDA_TEST_P(Buffer, CopyFromMat)
  117. {
  118. cv::Mat gold = randomMat(size, type);
  119. cv::ogl::Buffer buf;
  120. buf.copyFrom(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  121. cv::Mat bufData;
  122. buf.copyTo(bufData);
  123. EXPECT_MAT_NEAR(gold, bufData, 0);
  124. }
  125. CUDA_TEST_P(Buffer, CopyFromGpuMat)
  126. {
  127. cv::Mat gold = randomMat(size, type);
  128. cv::cuda::GpuMat d_gold(gold);
  129. cv::ogl::Buffer buf;
  130. buf.copyFrom(d_gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  131. cv::Mat bufData;
  132. buf.copyTo(bufData);
  133. EXPECT_MAT_NEAR(gold, bufData, 0);
  134. }
  135. CUDA_TEST_P(Buffer, CopyFromBuffer)
  136. {
  137. cv::Mat gold = randomMat(size, type);
  138. cv::ogl::Buffer buf_gold(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  139. cv::ogl::Buffer buf;
  140. buf.copyFrom(buf_gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  141. EXPECT_NE(buf_gold.bufId(), buf.bufId());
  142. cv::Mat bufData;
  143. buf.copyTo(bufData);
  144. EXPECT_MAT_NEAR(gold, bufData, 0);
  145. }
  146. CUDA_TEST_P(Buffer, CopyToGpuMat)
  147. {
  148. cv::Mat gold = randomMat(size, type);
  149. cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  150. cv::cuda::GpuMat dst;
  151. buf.copyTo(dst);
  152. EXPECT_MAT_NEAR(gold, dst, 0);
  153. }
  154. CUDA_TEST_P(Buffer, CopyToBuffer)
  155. {
  156. cv::Mat gold = randomMat(size, type);
  157. cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  158. cv::ogl::Buffer dst;
  159. buf.copyTo(dst);
  160. dst.setAutoRelease(true);
  161. EXPECT_NE(buf.bufId(), dst.bufId());
  162. cv::Mat bufData;
  163. dst.copyTo(bufData);
  164. EXPECT_MAT_NEAR(gold, bufData, 0);
  165. }
  166. CUDA_TEST_P(Buffer, Clone)
  167. {
  168. cv::Mat gold = randomMat(size, type);
  169. cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  170. cv::ogl::Buffer dst = buf.clone(cv::ogl::Buffer::ARRAY_BUFFER, true);
  171. EXPECT_NE(buf.bufId(), dst.bufId());
  172. cv::Mat bufData;
  173. dst.copyTo(bufData);
  174. EXPECT_MAT_NEAR(gold, bufData, 0);
  175. }
  176. CUDA_TEST_P(Buffer, MapHostRead)
  177. {
  178. cv::Mat gold = randomMat(size, type);
  179. cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  180. cv::Mat dst = buf.mapHost(cv::ogl::Buffer::READ_ONLY);
  181. EXPECT_MAT_NEAR(gold, dst, 0);
  182. buf.unmapHost();
  183. }
  184. CUDA_TEST_P(Buffer, MapHostWrite)
  185. {
  186. cv::Mat gold = randomMat(size, type);
  187. cv::ogl::Buffer buf(size, type, cv::ogl::Buffer::ARRAY_BUFFER, true);
  188. cv::Mat dst = buf.mapHost(cv::ogl::Buffer::WRITE_ONLY);
  189. gold.copyTo(dst);
  190. buf.unmapHost();
  191. dst.release();
  192. cv::Mat bufData;
  193. buf.copyTo(bufData);
  194. EXPECT_MAT_NEAR(gold, bufData, 0);
  195. }
  196. CUDA_TEST_P(Buffer, MapDevice)
  197. {
  198. cv::Mat gold = randomMat(size, type);
  199. cv::ogl::Buffer buf(gold, cv::ogl::Buffer::ARRAY_BUFFER, true);
  200. cv::cuda::GpuMat dst = buf.mapDevice();
  201. EXPECT_MAT_NEAR(gold, dst, 0);
  202. buf.unmapDevice();
  203. }
  204. INSTANTIATE_TEST_CASE_P(OpenGL, Buffer, testing::Combine(DIFFERENT_SIZES, ALL_TYPES));
  205. /////////////////////////////////////////////
  206. // Texture2D
  207. PARAM_TEST_CASE(Texture2D, cv::Size, MatType)
  208. {
  209. static void SetUpTestCase()
  210. {
  211. cv::namedWindow("test", cv::WINDOW_OPENGL);
  212. }
  213. static void TearDownTestCase()
  214. {
  215. cv::destroyAllWindows();
  216. }
  217. cv::Size size;
  218. int type;
  219. int depth;
  220. int cn;
  221. cv::ogl::Texture2D::Format format;
  222. virtual void SetUp()
  223. {
  224. size = GET_PARAM(0);
  225. type = GET_PARAM(1);
  226. depth = CV_MAT_DEPTH(type);
  227. cn = CV_MAT_CN(type);
  228. format = cn == 1 ? cv::ogl::Texture2D::DEPTH_COMPONENT : cn == 3 ? cv::ogl::Texture2D::RGB : cn == 4 ? cv::ogl::Texture2D::RGBA : cv::ogl::Texture2D::NONE;
  229. }
  230. };
  231. CUDA_TEST_P(Texture2D, Constructor1)
  232. {
  233. cv::ogl::Texture2D tex(size.height, size.width, format, true);
  234. EXPECT_EQ(size.height, tex.rows());
  235. EXPECT_EQ(size.width, tex.cols());
  236. EXPECT_EQ(format, tex.format());
  237. }
  238. CUDA_TEST_P(Texture2D, Constructor2)
  239. {
  240. cv::ogl::Texture2D tex(size, format, true);
  241. EXPECT_EQ(size.height, tex.rows());
  242. EXPECT_EQ(size.width, tex.cols());
  243. EXPECT_EQ(format, tex.format());
  244. }
  245. CUDA_TEST_P(Texture2D, ConstructorFromMat)
  246. {
  247. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  248. cv::ogl::Texture2D tex(gold, true);
  249. cv::Mat texData;
  250. tex.copyTo(texData, depth);
  251. EXPECT_MAT_NEAR(gold, texData, 1e-2);
  252. }
  253. CUDA_TEST_P(Texture2D, ConstructorFromGpuMat)
  254. {
  255. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  256. cv::cuda::GpuMat d_gold(gold);
  257. cv::ogl::Texture2D tex(d_gold, true);
  258. cv::Mat texData;
  259. tex.copyTo(texData, depth);
  260. EXPECT_MAT_NEAR(gold, texData, 1e-2);
  261. }
  262. CUDA_TEST_P(Texture2D, ConstructorFromBuffer)
  263. {
  264. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  265. cv::ogl::Buffer buf_gold(gold, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
  266. cv::ogl::Texture2D tex(buf_gold, true);
  267. cv::Mat texData;
  268. tex.copyTo(texData, depth);
  269. EXPECT_MAT_NEAR(gold, texData, 1e-2);
  270. }
  271. CUDA_TEST_P(Texture2D, ConstructorFromTexture2D)
  272. {
  273. cv::ogl::Texture2D tex_gold(size, format, true);
  274. cv::ogl::Texture2D tex(tex_gold);
  275. EXPECT_EQ(tex_gold.texId(), tex.texId());
  276. EXPECT_EQ(tex_gold.rows(), tex.rows());
  277. EXPECT_EQ(tex_gold.cols(), tex.cols());
  278. EXPECT_EQ(tex_gold.format(), tex.format());
  279. }
  280. CUDA_TEST_P(Texture2D, Create)
  281. {
  282. cv::ogl::Texture2D tex;
  283. tex.create(size.height, size.width, format, true);
  284. EXPECT_EQ(size.height, tex.rows());
  285. EXPECT_EQ(size.width, tex.cols());
  286. EXPECT_EQ(format, tex.format());
  287. }
  288. CUDA_TEST_P(Texture2D, CopyFromMat)
  289. {
  290. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  291. cv::ogl::Texture2D tex;
  292. tex.copyFrom(gold, true);
  293. cv::Mat texData;
  294. tex.copyTo(texData, depth);
  295. EXPECT_MAT_NEAR(gold, texData, 1e-2);
  296. }
  297. CUDA_TEST_P(Texture2D, CopyFromGpuMat)
  298. {
  299. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  300. cv::cuda::GpuMat d_gold(gold);
  301. cv::ogl::Texture2D tex;
  302. tex.copyFrom(d_gold, true);
  303. cv::Mat texData;
  304. tex.copyTo(texData, depth);
  305. EXPECT_MAT_NEAR(gold, texData, 1e-2);
  306. }
  307. CUDA_TEST_P(Texture2D, CopyFromBuffer)
  308. {
  309. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  310. cv::ogl::Buffer buf_gold(gold, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
  311. cv::ogl::Texture2D tex;
  312. tex.copyFrom(buf_gold, true);
  313. cv::Mat texData;
  314. tex.copyTo(texData, depth);
  315. EXPECT_MAT_NEAR(gold, texData, 1e-2);
  316. }
  317. CUDA_TEST_P(Texture2D, CopyToGpuMat)
  318. {
  319. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  320. cv::ogl::Texture2D tex(gold, true);
  321. cv::cuda::GpuMat dst;
  322. tex.copyTo(dst, depth);
  323. EXPECT_MAT_NEAR(gold, dst, 1e-2);
  324. }
  325. CUDA_TEST_P(Texture2D, CopyToBuffer)
  326. {
  327. cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
  328. cv::ogl::Texture2D tex(gold, true);
  329. cv::ogl::Buffer dst;
  330. tex.copyTo(dst, depth, true);
  331. cv::Mat bufData;
  332. dst.copyTo(bufData);
  333. EXPECT_MAT_NEAR(gold, bufData, 1e-2);
  334. }
  335. INSTANTIATE_TEST_CASE_P(OpenGL, Texture2D, testing::Combine(DIFFERENT_SIZES, testing::Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4)));
  336. }} // namespace
  337. #endif