1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #define BOOST_TEST_MODULE TestImage2D
12 #include <boost/test/unit_test.hpp>
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/image/image2d.hpp>
18 #include <boost/compute/utility/dim.hpp>
21 #include "context_setup.hpp"
23 namespace compute
= boost::compute
;
25 BOOST_AUTO_TEST_CASE(image2d_get_supported_formats
)
27 const std::vector
<compute::image_format
> formats
=
28 compute::image2d::get_supported_formats(context
);
31 BOOST_AUTO_TEST_CASE(create_image_doctest
)
35 // create 8-bit RGBA image format
36 boost::compute::image_format
rgba8(CL_RGBA
, CL_UNSIGNED_INT8
);
38 // create 640x480 image object
39 boost::compute::image2d
img(context
, 640, 480, rgba8
);
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));
48 catch(compute::opencl_error
&e
){
49 if(e
.error_code() == CL_IMAGE_FORMAT_NOT_SUPPORTED
){
50 // image format not supported by device
54 // some other error, rethrow
59 BOOST_AUTO_TEST_CASE(get_info
)
61 compute::image_format
format(CL_RGBA
, CL_UNSIGNED_INT8
);
63 if(!compute::image2d::is_supported_format(format
, context
)){
64 std::cerr
<< "skipping get_info test, image format not supported" << std::endl
;
68 compute::image2d
image(
69 context
, 48, 64, format
, compute::image2d::read_only
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));
79 BOOST_CHECK(image
.format() == format
);
80 BOOST_CHECK_EQUAL(image
.width(), size_t(48));
81 BOOST_CHECK_EQUAL(image
.height(), size_t(64));
84 BOOST_AUTO_TEST_CASE(clone_image
)
86 compute::image_format
format(CL_RGBA
, CL_UNORM_INT8
);
88 if(!compute::image2d::is_supported_format(format
, context
)){
89 std::cerr
<< "skipping clone_image test, image format not supported" << std::endl
;
94 unsigned int data
[] = { 0x0000ffff, 0xff00ffff,
95 0x00ff00ff, 0xffffffff };
97 // create image on the device
98 compute::image2d
image(context
, 2, 2, format
);
100 // ensure we have a valid image object
101 BOOST_REQUIRE(image
.get() != cl_mem());
103 // copy image data to the device
104 queue
.enqueue_write_image(image
, image
.origin(), image
.size(), data
);
107 compute::image2d copy
= image
.clone(queue
);
109 // ensure image format is the same
110 BOOST_CHECK(copy
.format() == image
.format());
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
);
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]);
123 #ifdef BOOST_COMPUTE_CL_VERSION_1_2
124 BOOST_AUTO_TEST_CASE(fill_image
)
126 REQUIRES_OPENCL_VERSION(1, 2); // device OpenCL version check
128 compute::image_format
format(CL_RGBA
, CL_UNSIGNED_INT8
);
130 if(!compute::image2d::is_supported_format(format
, context
)){
131 std::cerr
<< "skipping fill_image test, image format not supported" << std::endl
;
135 compute::image2d
img(context
, 640, 480, format
);
137 // fill image with black
138 compute::uint4_
black(0, 0, 0, 255);
139 queue
.enqueue_fill_image(img
, &black
, img
.origin(), img
.size());
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));
146 // fill image with white
147 compute::uint4_
white(255, 255, 255, 255);
148 queue
.enqueue_fill_image(img
, &white
, img
.origin(), img
.size());
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));
156 // check type_name() for image2d
157 BOOST_AUTO_TEST_CASE(image2d_type_name
)
161 boost::compute::type_name
<boost::compute::image2d
>(), "image2d_t"
166 BOOST_AUTO_TEST_CASE(map_image
)
168 compute::image_format
format(CL_RGBA
, CL_UNSIGNED_INT8
);
170 if(!compute::image2d::is_supported_format(format
, context
)){
171 std::cerr
<< "skipping clone_image test, image format not supported" << std::endl
;
175 // create image on the device
176 compute::image2d
image(context
, 2, 2, format
);
178 // ensure we have a valid image object
179 BOOST_REQUIRE(image
.get() != cl_mem());
181 size_t row_pitch
= 0;
182 size_t slice_pitch
= 0;
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
)
190 BOOST_CHECK_EQUAL(row_pitch
, size_t(2*4));
193 compute::uint_ data
[] = { 0x0000ffff, 0xff00ffff,
194 0x00ff00ff, 0xffffffff };
196 // copy data to image
197 for(size_t i
= 0; i
< 4; i
++){
202 queue
.enqueue_unmap_image(image
, static_cast<void*>(ptr
));
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
,
214 BOOST_CHECK(map_event
.get_status() == CL_COMPLETE
);
215 BOOST_CHECK_EQUAL(row_pitch
, size_t(2*4));
218 for(size_t i
= 0; i
< 4; i
++){
219 BOOST_CHECK_EQUAL(*(ptr
+i
), data
[i
]);
223 queue
.enqueue_unmap_image(image
, static_cast<void*>(ptr
));
226 BOOST_AUTO_TEST_SUITE_END()