aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/basic/test_readimage.cpp
blob: 0aa70525a7c68dc514eb7cb4351c604602bc9c4e (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "harness/compat.h"
#include "harness/imageHelpers.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <algorithm>
#include <string>
#include <vector>

#include "procs.h"

#define TEST_IMAGE_WIDTH_2D (512)
#define TEST_IMAGE_HEIGHT_2D (512)

#define TEST_IMAGE_WIDTH_3D (64)
#define TEST_IMAGE_HEIGHT_3D (64)
#define TEST_IMAGE_DEPTH_3D (64)

#define TEST_IMAGE_WIDTH(TYPE)                                                 \
    ((CL_MEM_OBJECT_IMAGE2D == TYPE) ? TEST_IMAGE_WIDTH_2D                     \
                                     : TEST_IMAGE_WIDTH_3D)
#define TEST_IMAGE_HEIGHT(TYPE)                                                \
    ((CL_MEM_OBJECT_IMAGE2D == TYPE) ? TEST_IMAGE_HEIGHT_2D                    \
                                     : TEST_IMAGE_HEIGHT_3D)
#define TEST_IMAGE_DEPTH(TYPE)                                                 \
    ((CL_MEM_OBJECT_IMAGE2D == TYPE) ? 1 : TEST_IMAGE_DEPTH_3D)

namespace {
const char *kernel_source_2d = R"(
__kernel void test_CL_BGRACL_UNORM_INT8(read_only image2d_t srcimg, __global uchar4 *dst, sampler_t sampler)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    indx = tid_y * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y)) * 255.0f;
    dst[indx] = convert_uchar4_rte(color.zyxw);
}

__kernel void test_CL_RGBACL_UNORM_INT8(read_only image2d_t srcimg, __global uchar4 *dst, sampler_t sampler)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    indx = tid_y * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y)) * 255.0f;
    dst[indx] = convert_uchar4_rte(color);
}

__kernel void test_CL_RGBACL_UNORM_INT16(read_only image2d_t srcimg, __global ushort4 *dst, sampler_t smp)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    indx = tid_y * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, smp, (int2)(tid_x, tid_y));
    ushort4 dst_write;
    dst_write.x = convert_ushort_rte(color.x * 65535.0f);
    dst_write.y = convert_ushort_rte(color.y * 65535.0f);
    dst_write.z = convert_ushort_rte(color.z * 65535.0f);
    dst_write.w = convert_ushort_rte(color.w * 65535.0f);
    dst[indx] = dst_write;
}

__kernel void test_CL_RGBACL_FLOAT(read_only image2d_t srcimg, __global float4 *dst, sampler_t smp)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    indx = tid_y * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, smp, (int2)(tid_x, tid_y));
    
    dst[indx].x = color.x;
    dst[indx].y = color.y;
    dst[indx].z = color.z;
    dst[indx].w = color.w;

}
)";

static const char *kernel_source_3d = R"(
__kernel void test_CL_BGRACL_UNORM_INT8(read_only image3d_t srcimg, __global uchar4 *dst, sampler_t sampler)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    tid_z = get_global_id(2);
    int    indx = (tid_z * get_image_height(srcimg) + tid_y) * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, sampler, (int4)(tid_x, tid_y, tid_z, 0))* 255.0f;
    dst[indx].x = color.z;
    dst[indx].y = color.y;
    dst[indx].z = color.x;
    dst[indx].w = color.w;

}

__kernel void test_CL_RGBACL_UNORM_INT8(read_only image3d_t srcimg, __global uchar4 *dst, sampler_t sampler)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    tid_z = get_global_id(2);
    int    indx = (tid_z * get_image_height(srcimg) + tid_y) * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, sampler, (int4)(tid_x, tid_y, tid_z, 0))* 255.0f;

    dst[indx].x = color.x;
    dst[indx].y = color.y;
    dst[indx].z = color.z;
    dst[indx].w = color.w;

}

__kernel void test_CL_RGBACL_UNORM_INT16(read_only image3d_t srcimg, __global ushort4 *dst, sampler_t sampler)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    tid_z = get_global_id(2);
    int    indx = (tid_z * get_image_height(srcimg) + tid_y) * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, sampler, (int4)(tid_x, tid_y, tid_z, 0));
    ushort4 dst_write;
    dst_write.x = convert_ushort_rte(color.x * 65535.0f);
    dst_write.y = convert_ushort_rte(color.y * 65535.0f);
    dst_write.z = convert_ushort_rte(color.z * 65535.0f);
    dst_write.w = convert_ushort_rte(color.w * 65535.0f);
    dst[indx] = dst_write;

}

__kernel void test_CL_RGBACL_FLOAT(read_only image3d_t srcimg, __global float *dst, sampler_t sampler)
{
    int    tid_x = get_global_id(0);
    int    tid_y = get_global_id(1);
    int    tid_z = get_global_id(2);
    int    indx = (tid_z * get_image_height(srcimg) + tid_y) * get_image_width(srcimg) + tid_x;
    float4 color;

    color = read_imagef(srcimg, sampler, (int4)(tid_x, tid_y, tid_z, 0));
    indx *= 4;
    dst[indx+0] = color.x;
    dst[indx+1] = color.y;
    dst[indx+2] = color.z;
    dst[indx+3] = color.w;

}
)";

template <typename T> void generate_random_inputs(std::vector<T> &v)
{
    RandomSeed seed(gRandomSeed);

    auto random_generator = [&seed]() {
        return static_cast<T>(genrand_int32(seed));
    };

    std::generate(v.begin(), v.end(), random_generator);
}

template <> void generate_random_inputs<float>(std::vector<float> &v)
{
    RandomSeed seed(gRandomSeed);

    auto random_generator = [&seed]() {
        return get_random_float(-0x40000000, 0x40000000, seed);
    };

    std::generate(v.begin(), v.end(), random_generator);
}

cl_mem create_image_xd(cl_context context, cl_mem_flags flags,
                       cl_mem_object_type type, const cl_image_format *fmt,
                       size_t x, size_t y, size_t z, cl_int *err)
{

    return (CL_MEM_OBJECT_IMAGE2D == type)
        ? create_image_2d(context, flags, fmt, x, y, 0, nullptr, err)
        : create_image_3d(context, flags, fmt, x, y, z, 0, 0, nullptr, err);
}

template <cl_mem_object_type IMG_TYPE, typename T>
int test_readimage(cl_device_id device, cl_context context,
                   cl_command_queue queue, const cl_image_format *img_format)
{
    clMemWrapper streams[2];
    clProgramWrapper program;
    clKernelWrapper kernel;
    clSamplerWrapper sampler;

    std::string kernel_name("test_");

    size_t img_width = TEST_IMAGE_WIDTH(IMG_TYPE);
    size_t img_height = TEST_IMAGE_HEIGHT(IMG_TYPE);
    size_t img_depth = TEST_IMAGE_DEPTH(IMG_TYPE);

    int err;

    const size_t origin[3] = { 0, 0, 0 };
    const size_t region[3] = { img_width, img_height, img_depth };

    const size_t num_elements = img_width * img_height * img_depth * 4;
    const size_t length = num_elements * sizeof(T);

    PASSIVE_REQUIRE_IMAGE_SUPPORT(device)

    std::vector<T> input(num_elements);
    std::vector<T> output(num_elements);

    generate_random_inputs(input);

    streams[0] =
        create_image_xd(context, CL_MEM_READ_ONLY, IMG_TYPE, img_format,
                        img_width, img_height, img_depth, &err);
    test_error(err, "create_image failed.");

    streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, &err);
    test_error(err, "clCreateBuffer failed.");

    sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE,
                              CL_FILTER_NEAREST, &err);
    test_error(err, "clCreateSampler failed");

    err = clEnqueueWriteImage(queue, streams[0], CL_TRUE, origin, region, 0, 0,
                              input.data(), 0, NULL, NULL);
    test_error(err, "clEnqueueWriteImage failed.");

    kernel_name += GetChannelOrderName(img_format->image_channel_order);
    kernel_name += GetChannelTypeName(img_format->image_channel_data_type);

    const char **kernel_source = (CL_MEM_OBJECT_IMAGE2D == IMG_TYPE)
        ? &kernel_source_2d
        : &kernel_source_3d;

    err = create_single_kernel_helper(context, &program, &kernel, 1,
                                      kernel_source, kernel_name.c_str());
    test_error(err, "create_single_kernel_helper failed.");

    err = clSetKernelArg(kernel, 0, sizeof(streams[0]), &streams[0]);
    err |= clSetKernelArg(kernel, 1, sizeof(streams[1]), &streams[1]);
    err |= clSetKernelArg(kernel, 2, sizeof(sampler), &sampler);
    test_error(err, "clSetKernelArgs failed\n");

    err = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, region, NULL, 0, NULL,
                                 NULL);
    test_error(err, "clEnqueueNDRangeKernel failed\n");

    err = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, length,
                              output.data(), 0, NULL, NULL);
    test_error(err, "clEnqueueReadBuffer failed\n");

    if (0 != memcmp(input.data(), output.data(), length))
    {
        log_error("READ_IMAGE_%s_%s test failed\n",
                  GetChannelOrderName(img_format->image_channel_order),
                  GetChannelTypeName(img_format->image_channel_data_type));
        err = -1;
    }
    else
    {
        log_info("READ_IMAGE_%s_%s test passed\n",
                 GetChannelOrderName(img_format->image_channel_order),
                 GetChannelTypeName(img_format->image_channel_data_type));
    }

    return err;
}

bool check_format(cl_device_id device, cl_context context,
                  cl_mem_object_type image_type,
                  const cl_image_format img_format)
{
    return is_image_format_required(img_format, CL_MEM_READ_ONLY, image_type,
                                    device)
        || is_image_format_supported(context, CL_MEM_READ_ONLY, image_type,
                                     &img_format);
}

}
int test_readimage(cl_device_id device, cl_context context,
                   cl_command_queue queue, int num_elements)
{
    const cl_image_format format[] = { { CL_RGBA, CL_UNORM_INT8 },
                                       { CL_BGRA, CL_UNORM_INT8 } };

    int err = test_readimage<CL_MEM_OBJECT_IMAGE2D, cl_uchar>(
        device, context, queue, &format[0]);

    if (check_format(device, context, CL_MEM_OBJECT_IMAGE2D, format[1]))
    {
        err |= test_readimage<CL_MEM_OBJECT_IMAGE2D, cl_uchar>(
            device, context, queue, &format[1]);
    }

    return err;
}

int test_readimage_int16(cl_device_id device, cl_context context,
                         cl_command_queue queue, int num_elements)
{
    const cl_image_format format = { CL_RGBA, CL_UNORM_INT16 };
    return test_readimage<CL_MEM_OBJECT_IMAGE2D, cl_ushort>(device, context,
                                                            queue, &format);
}

int test_readimage_fp32(cl_device_id device, cl_context context,
                        cl_command_queue queue, int num_elements)
{
    const cl_image_format format = { CL_RGBA, CL_FLOAT };
    return test_readimage<CL_MEM_OBJECT_IMAGE2D, cl_float>(device, context,
                                                           queue, &format);
}

int test_readimage3d(cl_device_id device, cl_context context,
                     cl_command_queue queue, int num_elements)
{
    const cl_image_format format[] = { { CL_RGBA, CL_UNORM_INT8 },
                                       { CL_BGRA, CL_UNORM_INT8 } };

    PASSIVE_REQUIRE_3D_IMAGE_SUPPORT(device)

    int err = test_readimage<CL_MEM_OBJECT_IMAGE3D, cl_uchar>(
        device, context, queue, &format[0]);

    if (check_format(device, context, CL_MEM_OBJECT_IMAGE3D, format[1]))
    {
        err |= test_readimage<CL_MEM_OBJECT_IMAGE3D, cl_uchar>(
            device, context, queue, &format[1]);
    }

    return err;
}

int test_readimage3d_int16(cl_device_id device, cl_context context,
                           cl_command_queue queue, int num_elements)
{
    const cl_image_format format = { CL_RGBA, CL_UNORM_INT16 };

    PASSIVE_REQUIRE_3D_IMAGE_SUPPORT(device)

    return test_readimage<CL_MEM_OBJECT_IMAGE3D, cl_ushort>(device, context,
                                                            queue, &format);
}
int test_readimage3d_fp32(cl_device_id device, cl_context context,
                          cl_command_queue queue, int num_elements)
{
    const cl_image_format format = { CL_RGBA, CL_FLOAT };

    PASSIVE_REQUIRE_3D_IMAGE_SUPPORT(device)

    return test_readimage<CL_MEM_OBJECT_IMAGE3D, cl_float>(device, context,
                                                           queue, &format);
}