• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #define BOOST_TEST_MODULE TestImage2D
12 #include <boost/test/unit_test.hpp>
13 
14 #include <iostream>
15 
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/image/image2d.hpp>
18 #include <boost/compute/utility/dim.hpp>
19 
20 #include "quirks.hpp"
21 #include "context_setup.hpp"
22 
23 namespace compute = boost::compute;
24 
BOOST_AUTO_TEST_CASE(image2d_get_supported_formats)25 BOOST_AUTO_TEST_CASE(image2d_get_supported_formats)
26 {
27     const std::vector<compute::image_format> formats =
28         compute::image2d::get_supported_formats(context);
29 }
30 
BOOST_AUTO_TEST_CASE(create_image_doctest)31 BOOST_AUTO_TEST_CASE(create_image_doctest)
32 {
33     try {
34 //! [create_image]
35 // create 8-bit RGBA image format
36 boost::compute::image_format rgba8(CL_RGBA, CL_UNSIGNED_INT8);
37 
38 // create 640x480 image object
39 boost::compute::image2d img(context, 640, 480, rgba8);
40 //! [create_image]
41 
42         // verify image has been created and format is correct
43         BOOST_CHECK(img.get() != cl_mem());
44         BOOST_CHECK(img.format() == rgba8);
45         BOOST_CHECK_EQUAL(img.width(), size_t(640));
46         BOOST_CHECK_EQUAL(img.height(), size_t(480));
47     }
48     catch(compute::opencl_error &e){
49         if(e.error_code() == CL_IMAGE_FORMAT_NOT_SUPPORTED){
50             // image format not supported by device
51             return;
52         }
53 
54         // some other error, rethrow
55         throw;
56     }
57 }
58 
BOOST_AUTO_TEST_CASE(get_info)59 BOOST_AUTO_TEST_CASE(get_info)
60 {
61     compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);
62 
63     if(!compute::image2d::is_supported_format(format, context)){
64         std::cerr << "skipping get_info test, image format not supported" << std::endl;
65         return;
66     }
67 
68     compute::image2d image(
69         context, 48, 64, format, compute::image2d::read_only
70     );
71 
72     BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_WIDTH), size_t(48));
73     BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_HEIGHT), size_t(64));
74     BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_DEPTH), size_t(0));
75     BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_ROW_PITCH), size_t(48*4));
76     BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_SLICE_PITCH), size_t(0));
77     BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_ELEMENT_SIZE), size_t(4));
78 
79     BOOST_CHECK(image.format() == format);
80     BOOST_CHECK_EQUAL(image.width(), size_t(48));
81     BOOST_CHECK_EQUAL(image.height(), size_t(64));
82 }
83 
BOOST_AUTO_TEST_CASE(clone_image)84 BOOST_AUTO_TEST_CASE(clone_image)
85 {
86     compute::image_format format(CL_RGBA, CL_UNORM_INT8);
87 
88     if(!compute::image2d::is_supported_format(format, context)){
89         std::cerr << "skipping clone_image test, image format not supported" << std::endl;
90         return;
91     }
92 
93     // image data
94     unsigned int data[] = { 0x0000ffff, 0xff00ffff,
95                             0x00ff00ff, 0xffffffff };
96 
97     // create image on the device
98     compute::image2d image(context, 2, 2, format);
99 
100     // ensure we have a valid image object
101     BOOST_REQUIRE(image.get() != cl_mem());
102 
103     // copy image data to the device
104     queue.enqueue_write_image(image, image.origin(), image.size(), data);
105 
106     // clone image
107     compute::image2d copy = image.clone(queue);
108 
109     // ensure image format is the same
110     BOOST_CHECK(copy.format() == image.format());
111 
112     // read cloned image data back to the host
113     unsigned int cloned_data[4];
114     queue.enqueue_read_image(copy, image.origin(), image.size(), cloned_data);
115 
116     // ensure original data and cloned data are the same
117     BOOST_CHECK_EQUAL(cloned_data[0], data[0]);
118     BOOST_CHECK_EQUAL(cloned_data[1], data[1]);
119     BOOST_CHECK_EQUAL(cloned_data[2], data[2]);
120     BOOST_CHECK_EQUAL(cloned_data[3], data[3]);
121 }
122 
123 #ifdef BOOST_COMPUTE_CL_VERSION_1_2
BOOST_AUTO_TEST_CASE(fill_image)124 BOOST_AUTO_TEST_CASE(fill_image)
125 {
126     REQUIRES_OPENCL_VERSION(1, 2); // device OpenCL version check
127 
128     compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);
129 
130     if(!compute::image2d::is_supported_format(format, context)){
131         std::cerr << "skipping fill_image test, image format not supported" << std::endl;
132         return;
133     }
134 
135     compute::image2d img(context, 640, 480, format);
136 
137     // fill image with black
138     compute::uint4_ black(0, 0, 0, 255);
139     queue.enqueue_fill_image(img, &black, img.origin(), img.size());
140 
141     // read value of first pixel
142     compute::uchar4_ first_pixel;
143     queue.enqueue_read_image(img, compute::dim(0), compute::dim(1), &first_pixel);
144     BOOST_CHECK_EQUAL(first_pixel, compute::uchar4_(0, 0, 0, 255));
145 
146     // fill image with white
147     compute::uint4_ white(255, 255, 255, 255);
148     queue.enqueue_fill_image(img, &white, img.origin(), img.size());
149 
150     // read value of first pixel
151     queue.enqueue_read_image(img, compute::dim(0), compute::dim(1), &first_pixel);
152     BOOST_CHECK_EQUAL(first_pixel, compute::uchar4_(255, 255, 255, 255));
153 }
154 #endif
155 
156 // check type_name() for image2d
BOOST_AUTO_TEST_CASE(image2d_type_name)157 BOOST_AUTO_TEST_CASE(image2d_type_name)
158 {
159     BOOST_CHECK(
160         std::strcmp(
161             boost::compute::type_name<boost::compute::image2d>(), "image2d_t"
162         ) == 0
163     );
164 }
165 
BOOST_AUTO_TEST_CASE(map_image)166 BOOST_AUTO_TEST_CASE(map_image)
167 {
168     compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);
169 
170     if(!compute::image2d::is_supported_format(format, context)){
171         std::cerr << "skipping clone_image test, image format not supported" << std::endl;
172         return;
173     }
174 
175     // create image on the device
176     compute::image2d image(context, 2, 2, format);
177 
178     // ensure we have a valid image object
179     BOOST_REQUIRE(image.get() != cl_mem());
180 
181     size_t row_pitch = 0;
182     size_t slice_pitch = 0;
183 
184     // write map image
185     compute::uint_* ptr = static_cast<compute::uint_*>(
186         queue.enqueue_map_image(image, CL_MAP_WRITE, image.origin(),
187                                 image.size(), row_pitch, slice_pitch)
188     );
189 
190     BOOST_CHECK_EQUAL(row_pitch, size_t(2*4));
191 
192     // image data
193     compute::uint_ data[] = { 0x0000ffff, 0xff00ffff,
194                               0x00ff00ff, 0xffffffff };
195 
196     // copy data to image
197     for(size_t i = 0; i < 4; i++){
198         *(ptr+i) = data[i];
199     }
200 
201     // unmap
202     queue.enqueue_unmap_image(image, static_cast<void*>(ptr));
203 
204     // read map image
205     compute::event map_event;
206     ptr = static_cast<compute::uint_*>(
207         queue.enqueue_map_image_async(image, CL_MAP_READ, image.origin(),
208                                       image.size(), row_pitch, slice_pitch,
209                                       map_event)
210     );
211 
212     map_event.wait();
213 
214     BOOST_CHECK(map_event.get_status() == CL_COMPLETE);
215     BOOST_CHECK_EQUAL(row_pitch, size_t(2*4));
216 
217     // checking
218     for(size_t i = 0; i < 4; i++){
219         BOOST_CHECK_EQUAL(*(ptr+i), data[i]);
220     }
221 
222     // unmap
223     queue.enqueue_unmap_image(image, static_cast<void*>(ptr));
224 }
225 
226 BOOST_AUTO_TEST_SUITE_END()
227