File: test_image2d.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (226 lines) | stat: -rw-r--r-- 7,013 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestImage2D
#include <boost/test/unit_test.hpp>

#include <iostream>

#include <boost/compute/system.hpp>
#include <boost/compute/image/image2d.hpp>
#include <boost/compute/utility/dim.hpp>

#include "quirks.hpp"
#include "context_setup.hpp"

namespace compute = boost::compute;

BOOST_AUTO_TEST_CASE(image2d_get_supported_formats)
{
    const std::vector<compute::image_format> formats =
        compute::image2d::get_supported_formats(context);
}

BOOST_AUTO_TEST_CASE(create_image_doctest)
{
    try {
//! [create_image]
// create 8-bit RGBA image format
boost::compute::image_format rgba8(CL_RGBA, CL_UNSIGNED_INT8);

// create 640x480 image object
boost::compute::image2d img(context, 640, 480, rgba8);
//! [create_image]

        // verify image has been created and format is correct
        BOOST_CHECK(img.get() != cl_mem());
        BOOST_CHECK(img.format() == rgba8);
        BOOST_CHECK_EQUAL(img.width(), size_t(640));
        BOOST_CHECK_EQUAL(img.height(), size_t(480));
    }
    catch(compute::opencl_error &e){
        if(e.error_code() == CL_IMAGE_FORMAT_NOT_SUPPORTED){
            // image format not supported by device
            return;
        }

        // some other error, rethrow
        throw;
    }
}

BOOST_AUTO_TEST_CASE(get_info)
{
    compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);

    if(!compute::image2d::is_supported_format(format, context)){
        std::cerr << "skipping get_info test, image format not supported" << std::endl;
        return;
    }

    compute::image2d image(
        context, 48, 64, format, compute::image2d::read_only
    );

    BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_WIDTH), size_t(48));
    BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_HEIGHT), size_t(64));
    BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_DEPTH), size_t(0));
    BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_ROW_PITCH), size_t(48*4));
    BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_SLICE_PITCH), size_t(0));
    BOOST_CHECK_EQUAL(image.get_info<size_t>(CL_IMAGE_ELEMENT_SIZE), size_t(4));

    BOOST_CHECK(image.format() == format);
    BOOST_CHECK_EQUAL(image.width(), size_t(48));
    BOOST_CHECK_EQUAL(image.height(), size_t(64));
}

BOOST_AUTO_TEST_CASE(clone_image)
{
    compute::image_format format(CL_RGBA, CL_UNORM_INT8);

    if(!compute::image2d::is_supported_format(format, context)){
        std::cerr << "skipping clone_image test, image format not supported" << std::endl;
        return;
    }

    // image data
    unsigned int data[] = { 0x0000ffff, 0xff00ffff,
                            0x00ff00ff, 0xffffffff };

    // create image on the device
    compute::image2d image(context, 2, 2, format);

    // ensure we have a valid image object
    BOOST_REQUIRE(image.get() != cl_mem());

    // copy image data to the device
    queue.enqueue_write_image(image, image.origin(), image.size(), data);

    // clone image
    compute::image2d copy = image.clone(queue);

    // ensure image format is the same
    BOOST_CHECK(copy.format() == image.format());

    // read cloned image data back to the host
    unsigned int cloned_data[4];
    queue.enqueue_read_image(copy, image.origin(), image.size(), cloned_data);

    // ensure original data and cloned data are the same
    BOOST_CHECK_EQUAL(cloned_data[0], data[0]);
    BOOST_CHECK_EQUAL(cloned_data[1], data[1]);
    BOOST_CHECK_EQUAL(cloned_data[2], data[2]);
    BOOST_CHECK_EQUAL(cloned_data[3], data[3]);
}

#ifdef BOOST_COMPUTE_CL_VERSION_1_2
BOOST_AUTO_TEST_CASE(fill_image)
{
    REQUIRES_OPENCL_VERSION(1, 2); // device OpenCL version check

    compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);

    if(!compute::image2d::is_supported_format(format, context)){
        std::cerr << "skipping fill_image test, image format not supported" << std::endl;
        return;
    }

    compute::image2d img(context, 640, 480, format);

    // fill image with black
    compute::uint4_ black(0, 0, 0, 255);
    queue.enqueue_fill_image(img, &black, img.origin(), img.size());

    // read value of first pixel
    compute::uchar4_ first_pixel;
    queue.enqueue_read_image(img, compute::dim(0), compute::dim(1), &first_pixel);
    BOOST_CHECK_EQUAL(first_pixel, compute::uchar4_(0, 0, 0, 255));

    // fill image with white
    compute::uint4_ white(255, 255, 255, 255);
    queue.enqueue_fill_image(img, &white, img.origin(), img.size());

    // read value of first pixel
    queue.enqueue_read_image(img, compute::dim(0), compute::dim(1), &first_pixel);
    BOOST_CHECK_EQUAL(first_pixel, compute::uchar4_(255, 255, 255, 255));
}
#endif

// check type_name() for image2d
BOOST_AUTO_TEST_CASE(image2d_type_name)
{
    BOOST_CHECK(
        std::strcmp(
            boost::compute::type_name<boost::compute::image2d>(), "image2d_t"
        ) == 0
    );
}

BOOST_AUTO_TEST_CASE(map_image)
{
    compute::image_format format(CL_RGBA, CL_UNSIGNED_INT8);

    if(!compute::image2d::is_supported_format(format, context)){
        std::cerr << "skipping clone_image test, image format not supported" << std::endl;
        return;
    }

    // create image on the device
    compute::image2d image(context, 2, 2, format);

    // ensure we have a valid image object
    BOOST_REQUIRE(image.get() != cl_mem());

    size_t row_pitch = 0;
    size_t slice_pitch = 0;

    // write map image
    compute::uint_* ptr = static_cast<compute::uint_*>(
        queue.enqueue_map_image(image, CL_MAP_WRITE, image.origin(),
                                image.size(), row_pitch, slice_pitch)
    );

    BOOST_CHECK_EQUAL(row_pitch, size_t(2*4));

    // image data
    compute::uint_ data[] = { 0x0000ffff, 0xff00ffff,
                              0x00ff00ff, 0xffffffff };

    // copy data to image
    for(size_t i = 0; i < 4; i++){
        *(ptr+i) = data[i];
    }

    // unmap
    queue.enqueue_unmap_image(image, static_cast<void*>(ptr));

    // read map image
    compute::event map_event;
    ptr = static_cast<compute::uint_*>(
        queue.enqueue_map_image_async(image, CL_MAP_READ, image.origin(),
                                      image.size(), row_pitch, slice_pitch,
                                      map_event)
    );

    map_event.wait();

    BOOST_CHECK(map_event.get_status() == CL_COMPLETE);
    BOOST_CHECK_EQUAL(row_pitch, size_t(2*4));

    // checking
    for(size_t i = 0; i < 4; i++){
        BOOST_CHECK_EQUAL(*(ptr+i), data[i]);
    }

    // unmap
    queue.enqueue_unmap_image(image, static_cast<void*>(ptr));
}

BOOST_AUTO_TEST_SUITE_END()