1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "harness/compat.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24
25 #include "procs.h"
26
27 static const char *rgba16_kernel_code =
28 "__kernel void test_rgba16(read_only image2d_t srcimg, __global ushort4 *dst, sampler_t smp)\n"
29 "{\n"
30 " int tid_x = get_global_id(0);\n"
31 " int tid_y = get_global_id(1);\n"
32 " int indx = tid_y * get_image_width(srcimg) + tid_x;\n"
33 " float4 color;\n"
34 "\n"
35 " color = read_imagef(srcimg, smp, (int2)(tid_x, tid_y));\n"
36 " ushort4 dst_write;\n"
37 " dst_write.x = convert_ushort_rte(color.x * 65535.0f);\n"
38 " dst_write.y = convert_ushort_rte(color.y * 65535.0f);\n"
39 " dst_write.z = convert_ushort_rte(color.z * 65535.0f);\n"
40 " dst_write.w = convert_ushort_rte(color.w * 65535.0f);\n"
41 " dst[indx] = dst_write;\n"
42 "\n"
43 "}\n";
44
45
46 static unsigned short *
generate_16bit_image(int w,int h,MTdata d)47 generate_16bit_image(int w, int h, MTdata d)
48 {
49 cl_ushort *ptr = (cl_ushort*)malloc(w * h * 4 * sizeof(cl_ushort));
50 int i;
51
52 for (i=0; i<w*h*4; i++)
53 ptr[i] = (cl_ushort)genrand_int32(d);
54
55 return ptr;
56 }
57
58 static int
verify_16bit_image(cl_ushort * image,cl_ushort * outptr,int w,int h)59 verify_16bit_image(cl_ushort *image, cl_ushort *outptr, int w, int h)
60 {
61 int i;
62 for (i=0; i<w*h*4; i++)
63 {
64 if (outptr[i] != image[i])
65 {
66 log_error("READ_IMAGE_RGBA_UNORM_INT16 test failed\n");
67 return -1;
68 }
69 }
70
71 log_info("READ_IMAGE_RGBA_UNORM_INT16 test passed\n");
72 return 0;
73 }
74
test_readimage_int16(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)75 int test_readimage_int16(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
76 {
77 cl_mem streams[2];
78 cl_program program;
79 cl_kernel kernel;
80 cl_image_format img_format;
81 cl_ushort *input_ptr, *output_ptr;
82 size_t threads[2];
83 int img_width = 512;
84 int img_height = 512;
85 int err;
86 size_t origin[3] = {0, 0, 0};
87 size_t region[3] = {img_width, img_height, 1};
88 size_t length = img_width * img_height * 4 * sizeof(cl_ushort);
89 MTdata d;
90
91 PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
92
93 d = init_genrand( gRandomSeed );
94 input_ptr = generate_16bit_image(img_width, img_height, d);
95 free_mtdata(d); d = NULL;
96
97 output_ptr = (cl_ushort*)malloc(length);
98
99 img_format.image_channel_order = CL_RGBA;
100 img_format.image_channel_data_type = CL_UNORM_INT16;
101 streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
102 if (!streams[0])
103 {
104 log_error("create_image_2d failed\n");
105 return -1;
106 }
107 streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
108 if (!streams[1])
109 {
110 log_error("clCreateArray failed\n");
111 return -1;
112 }
113
114 err = clEnqueueWriteImage(queue, streams[0], CL_TRUE, origin, region, 0, 0, input_ptr, 0, NULL, NULL);
115 if (err != CL_SUCCESS)
116 {
117 log_error("clWriteImage failed\n");
118 return -1;
119 }
120
121 err = create_single_kernel_helper(context, &program, &kernel, 1, &rgba16_kernel_code, "test_rgba16" );
122 if (err)
123 return -1;
124
125 cl_sampler sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err);
126 test_error(err, "clCreateSampler failed");
127
128 err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
129 err |= clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
130 err |= clSetKernelArg(kernel, 2, sizeof sampler, &sampler);
131 if (err != CL_SUCCESS)
132 {
133 log_error("clSetKernelArgs failed\n");
134 return -1;
135 }
136
137 threads[0] = (unsigned int)img_width;
138 threads[1] = (unsigned int)img_height;
139 err = clEnqueueNDRangeKernel(queue, kernel, 2, NULL, threads, NULL, 0, NULL, NULL);
140 if (err != CL_SUCCESS)
141 {
142 log_error("%s clEnqueueNDRangeKernel failed\n", __FUNCTION__);
143 return -1;
144 }
145
146 err = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, length, output_ptr, 0, NULL, NULL);
147 if (err != CL_SUCCESS)
148 {
149 log_error("clEnqueueReadBuffer failed\n");
150 return -1;
151 }
152
153 err = verify_16bit_image(input_ptr, output_ptr, img_width, img_height);
154
155 // cleanup
156 clReleaseSampler(sampler);
157 clReleaseMemObject(streams[0]);
158 clReleaseMemObject(streams[1]);
159 clReleaseKernel(kernel);
160 clReleaseProgram(program);
161 free(input_ptr);
162 free(output_ptr);
163
164 return err;
165 }
166
167
168