test_image2d.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestImage2D
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/image/image2d.hpp>
  15. #include <boost/compute/utility/dim.hpp>
  16. #include "quirks.hpp"
  17. #include "context_setup.hpp"
  18. namespace compute = boost::compute;
  19. BOOST_AUTO_TEST_CASE(image2d_get_supported_formats)
  20. {
  21. const std::vector<compute::image_format> formats =
  22. compute::image2d::get_supported_formats(context);
  23. }
  24. BOOST_AUTO_TEST_CASE(create_image_doctest)
  25. {
  26. try {
  27. //! [create_image]
  28. // create 8-bit RGBA image format
  29. boost::compute::image_format rgba8(CL_RGBA, CL_UNSIGNED_INT8);
  30. // create 640x480 image object
  31. boost::compute::image2d img(context, 640, 480, rgba8);
  32. //! [create_image]
  33. // verify image has been created and format is correct
  34. BOOST_CHECK(img.get() != cl_mem());
  35. BOOST_CHECK(img.format() == rgba8);
  36. BOOST_CHECK_EQUAL(img.width(), size_t(640));
  37. BOOST_CHECK_EQUAL(img.height(), size_t(480));
  38. }
  39. catch(compute::opencl_error &e){
  40. if(e.error_code() == CL_IMAGE_FORMAT_NOT_SUPPORTED){
  41. // image format not supported by device
  42. return;
  43. }
  44. // some other error, rethrow
  45. throw;
  46. }
  47. }
  48. BOOST_AUTO_TEST_CASE(get_info)
  49. {
  50. compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);
  51. if(!compute::image2d::is_supported_format(format, context)){
  52. std::cerr << "skipping get_info test, image format not supported" << std::endl;
  53. return;
  54. }
  55. compute::image2d image(
  56. context, 48, 64, format, compute::image2d::read_only
  57. );
  58. BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_WIDTH), size_t(48));
  59. BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_HEIGHT), size_t(64));
  60. BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_DEPTH), size_t(0));
  61. BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_ROW_PITCH), size_t(48*4));
  62. BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_SLICE_PITCH), size_t(0));
  63. BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_ELEMENT_SIZE), size_t(4));
  64. BOOST_CHECK(image.format() == format);
  65. BOOST_CHECK_EQUAL(image.width(), size_t(48));
  66. BOOST_CHECK_EQUAL(image.height(), size_t(64));
  67. }
  68. BOOST_AUTO_TEST_CASE(clone_image)
  69. {
  70. compute::image_format format(CL_RGBA, CL_UNORM_INT8);
  71. if(!compute::image2d::is_supported_format(format, context)){
  72. std::cerr << "skipping clone_image test, image format not supported" << std::endl;
  73. return;
  74. }
  75. // image data
  76. unsigned int data[] = { 0x0000ffff, 0xff00ffff,
  77. 0x00ff00ff, 0xffffffff };
  78. // create image on the device
  79. compute::image2d image(context, 2, 2, format);
  80. // ensure we have a valid image object
  81. BOOST_REQUIRE(image.get() != cl_mem());
  82. // copy image data to the device
  83. queue.enqueue_write_image(image, image.origin(), image.size(), data);
  84. // clone image
  85. compute::image2d copy = image.clone(queue);
  86. // ensure image format is the same
  87. BOOST_CHECK(copy.format() == image.format());
  88. // read cloned image data back to the host
  89. unsigned int cloned_data[4];
  90. queue.enqueue_read_image(copy, image.origin(), image.size(), cloned_data);
  91. // ensure original data and cloned data are the same
  92. BOOST_CHECK_EQUAL(cloned_data[0], data[0]);
  93. BOOST_CHECK_EQUAL(cloned_data[1], data[1]);
  94. BOOST_CHECK_EQUAL(cloned_data[2], data[2]);
  95. BOOST_CHECK_EQUAL(cloned_data[3], data[3]);
  96. }
  97. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  98. BOOST_AUTO_TEST_CASE(fill_image)
  99. {
  100. REQUIRES_OPENCL_VERSION(1, 2); // device OpenCL version check
  101. compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);
  102. if(!compute::image2d::is_supported_format(format, context)){
  103. std::cerr << "skipping fill_image test, image format not supported" << std::endl;
  104. return;
  105. }
  106. compute::image2d img(context, 640, 480, format);
  107. // fill image with black
  108. compute::uint4_ black(0, 0, 0, 255);
  109. queue.enqueue_fill_image(img, &black, img.origin(), img.size());
  110. // read value of first pixel
  111. compute::uchar4_ first_pixel;
  112. queue.enqueue_read_image(img, compute::dim(0), compute::dim(1), &first_pixel);
  113. BOOST_CHECK_EQUAL(first_pixel, compute::uchar4_(0, 0, 0, 255));
  114. // fill image with white
  115. compute::uint4_ white(255, 255, 255, 255);
  116. queue.enqueue_fill_image(img, &white, img.origin(), img.size());
  117. // read value of first pixel
  118. queue.enqueue_read_image(img, compute::dim(0), compute::dim(1), &first_pixel);
  119. BOOST_CHECK_EQUAL(first_pixel, compute::uchar4_(255, 255, 255, 255));
  120. }
  121. #endif
  122. // check type_name() for image2d
  123. BOOST_AUTO_TEST_CASE(image2d_type_name)
  124. {
  125. BOOST_CHECK(
  126. std::strcmp(
  127. boost::compute::type_name<boost::compute::image2d>(), "image2d_t"
  128. ) == 0
  129. );
  130. }
  131. BOOST_AUTO_TEST_CASE(map_image)
  132. {
  133. compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);
  134. if(!compute::image2d::is_supported_format(format, context)){
  135. std::cerr << "skipping clone_image test, image format not supported" << std::endl;
  136. return;
  137. }
  138. // create image on the device
  139. compute::image2d image(context, 2, 2, format);
  140. // ensure we have a valid image object
  141. BOOST_REQUIRE(image.get() != cl_mem());
  142. size_t row_pitch = 0;
  143. size_t slice_pitch = 0;
  144. // write map image
  145. compute::uint_* ptr = static_cast<compute::uint_*>(
  146. queue.enqueue_map_image(image, CL_MAP_WRITE, image.origin(),
  147. image.size(), row_pitch, slice_pitch)
  148. );
  149. BOOST_CHECK_EQUAL(row_pitch, size_t(2*4));
  150. // image data
  151. compute::uint_ data[] = { 0x0000ffff, 0xff00ffff,
  152. 0x00ff00ff, 0xffffffff };
  153. // copy data to image
  154. for(size_t i = 0; i < 4; i++){
  155. *(ptr+i) = data[i];
  156. }
  157. // unmap
  158. queue.enqueue_unmap_image(image, static_cast<void*>(ptr));
  159. // read map image
  160. compute::event map_event;
  161. ptr = static_cast<compute::uint_*>(
  162. queue.enqueue_map_image_async(image, CL_MAP_READ, image.origin(),
  163. image.size(), row_pitch, slice_pitch,
  164. map_event)
  165. );
  166. map_event.wait();
  167. BOOST_CHECK(map_event.get_status() == CL_COMPLETE);
  168. BOOST_CHECK_EQUAL(row_pitch, size_t(2*4));
  169. // checking
  170. for(size_t i = 0; i < 4; i++){
  171. BOOST_CHECK_EQUAL(*(ptr+i), data[i]);
  172. }
  173. // unmap
  174. queue.enqueue_unmap_image(image, static_cast<void*>(ptr));
  175. }
  176. BOOST_AUTO_TEST_SUITE_END()