• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *bgra8888_kernel_code =
28 "\n"
29 "__kernel void test_bgra8888(read_only image2d_t srcimg, __global uchar4 *dst, sampler_t sampler)\n"
30 "{\n"
31 "    int    tid_x = get_global_id(0);\n"
32 "    int    tid_y = get_global_id(1);\n"
33 "    int    indx = tid_y * get_image_width(srcimg) + tid_x;\n"
34 "    float4 color;\n"
35 "\n"
36 "    color = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y)) * 255.0f;\n"
37 "    dst[indx] = convert_uchar4_rte(color.zyxw);\n"
38 "\n"
39 "}\n";
40 
41 
42 static const char *rgba8888_kernel_code =
43 "\n"
44 "__kernel void test_rgba8888(read_only image2d_t srcimg, __global uchar4 *dst, sampler_t sampler)\n"
45 "{\n"
46 "    int    tid_x = get_global_id(0);\n"
47 "    int    tid_y = get_global_id(1);\n"
48 "    int    indx = tid_y * get_image_width(srcimg) + tid_x;\n"
49 "    float4 color;\n"
50 "\n"
51 "    color = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y)) * 255.0f;\n"
52 "    dst[indx] = convert_uchar4_rte(color);\n"
53 "\n"
54 "}\n";
55 
56 
57 static unsigned char *
generate_8888_image(int w,int h,MTdata d)58 generate_8888_image(int w, int h, MTdata d)
59 {
60     unsigned char   *ptr = (unsigned char*)malloc(w * h * 4);
61     int             i;
62 
63     for (i=0; i<w*h*4; i++)
64         ptr[i] = (unsigned char)genrand_int32( d);
65 
66     return ptr;
67 }
68 
69 static int
verify_bgra8888_image(unsigned char * image,unsigned char * outptr,int w,int h)70 verify_bgra8888_image(unsigned char *image, unsigned char *outptr, int w, int h)
71 {
72     int     i;
73 
74     for (i=0; i<w*h; i++)
75     {
76         if (outptr[i] != image[i])
77         {
78             log_error("READ_IMAGE_BGRA_UNORM_INT8 test failed\n");
79             return -1;
80         }
81     }
82 
83     log_info("READ_IMAGE_BGRA_UNORM_INT8 test passed\n");
84     return 0;
85 }
86 
87 static int
verify_rgba8888_image(unsigned char * image,unsigned char * outptr,int w,int h)88 verify_rgba8888_image(unsigned char *image, unsigned char *outptr, int w, int h)
89 {
90     int     i;
91 
92     for (i=0; i<w*h*4; i++)
93     {
94         if (outptr[i] != image[i])
95         {
96             log_error("READ_IMAGE_RGBA_UNORM_INT8 test failed\n");
97             return -1;
98         }
99     }
100 
101     log_info("READ_IMAGE_RGBA_UNORM_INT8 test passed\n");
102     return 0;
103 }
104 
105 
test_readimage(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)106 int test_readimage(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
107 {
108     cl_mem streams[3];
109     cl_program program[2];
110     cl_kernel kernel[2];
111     cl_image_format    img_format;
112     cl_image_format *supported_formats;
113     unsigned char    *input_ptr[2], *output_ptr;
114     size_t threads[2];
115     int img_width = 512;
116     int img_height = 512;
117     int i, err;
118     size_t origin[3] = {0, 0, 0};
119     size_t region[3] = {img_width, img_height, 1};
120     size_t length = img_width * img_height * 4 * sizeof(unsigned char);
121     MTdata d = init_genrand( gRandomSeed );
122     int supportsBGRA = 0;
123     cl_uint numFormats = 0;
124 
125     PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
126 
127     PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
128 
129     d = init_genrand( gRandomSeed );
130     input_ptr[0] = generate_8888_image(img_width, img_height, d);
131     input_ptr[1] = generate_8888_image(img_width, img_height, d);
132     free_mtdata(d); d = NULL;
133 
134     output_ptr = (unsigned char*)malloc(length);
135 
136     if(gIsEmbedded)
137     {
138         /* Get the supported image formats to see if BGRA is supported */
139         clGetSupportedImageFormats (context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, NULL, &numFormats);
140         supported_formats = (cl_image_format *) malloc(sizeof(cl_image_format) * numFormats);
141         clGetSupportedImageFormats (context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, numFormats, supported_formats, NULL);
142 
143         for(i = 0; i < numFormats; i++)
144         {
145             if(supported_formats[i].image_channel_order == CL_BGRA)
146             {
147                 supportsBGRA = 1;
148                 break;
149             }
150         }
151     }
152     else
153     {
154         supportsBGRA = 1;
155     }
156 
157     if(supportsBGRA)
158     {
159         img_format.image_channel_order = CL_BGRA;
160         img_format.image_channel_data_type = CL_UNORM_INT8;
161         streams[0] = clCreateImage2D(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
162         if (!streams[0])
163         {
164             log_error("clCreateImage2D failed\n");
165             return -1;
166         }
167     }
168 
169     img_format.image_channel_order = CL_RGBA;
170     img_format.image_channel_data_type = CL_UNORM_INT8;
171     streams[1] = clCreateImage2D(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
172     if (!streams[1])
173     {
174         log_error("clCreateImage2D failed\n");
175         return -1;
176     }
177     streams[2] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
178     if (!streams[2])
179     {
180         log_error("clCreateBuffer failed\n");
181         return -1;
182     }
183 
184     if(supportsBGRA)
185     {
186         err = clEnqueueWriteImage(queue, streams[0], CL_TRUE, origin, region, 0, 0, input_ptr[0], 0, NULL, NULL);
187         if (err != CL_SUCCESS)
188         {
189             log_error("clEnqueueWriteImage failed\n");
190             return -1;
191         }
192     }
193 
194     err = clEnqueueWriteImage(queue, streams[1], CL_TRUE, origin, region, 0, 0, input_ptr[1], 0, NULL, NULL);
195     if (err != CL_SUCCESS)
196     {
197         log_error("clEnqueueWriteImage failed\n");
198         return -1;
199     }
200 
201     if(supportsBGRA)
202     {
203         err = create_single_kernel_helper(context, &program[0], &kernel[0], 1, &bgra8888_kernel_code, "test_bgra8888" );
204         if (err)
205             return -1;
206     }
207 
208     err = create_single_kernel_helper(context, &program[1], &kernel[1], 1, &rgba8888_kernel_code, "test_rgba8888" );
209     if (err)
210         return -1;
211 
212     cl_sampler sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err);
213     test_error(err, "clCreateSampler failed");
214 
215     if(supportsBGRA)
216     {
217         err  = clSetKernelArg(kernel[0], 0, sizeof streams[0], &streams[0]);
218         err |= clSetKernelArg(kernel[0], 1, sizeof streams[2], &streams[2]);
219         err |= clSetKernelArg(kernel[0], 2, sizeof sampler, &sampler);
220         if (err != CL_SUCCESS)
221         {
222             log_error("clSetKernelArg failed\n");
223             return -1;
224         }
225     }
226 
227     err  = clSetKernelArg(kernel[1], 0, sizeof streams[1], &streams[1]);
228     err |= clSetKernelArg(kernel[1], 1, sizeof streams[2], &streams[2]);
229     err |= clSetKernelArg(kernel[1], 2, sizeof sampler, &sampler);
230     if (err != CL_SUCCESS)
231     {
232         log_error("clSetKernelArg failed\n");
233         return -1;
234     }
235 
236     threads[0] = (unsigned int)img_width;
237     threads[1] = (unsigned int)img_height;
238 
239     for (i=0; i<2; i++)
240     {
241         if(i == 0 && !supportsBGRA)
242             continue;
243 
244         err = clEnqueueNDRangeKernel(queue, kernel[i], 2, NULL, threads, NULL, 0, NULL, NULL);
245         if (err != CL_SUCCESS)
246         {
247             log_error("%s clEnqueueNDRangeKernel failed\n", __FUNCTION__);
248             return -1;
249         }
250         err = clEnqueueReadBuffer(queue, streams[2], CL_TRUE, 0, length, output_ptr, 0, NULL, NULL);
251         if (err != CL_SUCCESS)
252         {
253             log_error("clEnqueueReadBuffer failed\n");
254             return -1;
255         }
256 
257         switch (i)
258         {
259             case 0:
260                 err = verify_bgra8888_image(input_ptr[i], output_ptr, img_width, img_height);
261                 break;
262             case 1:
263                 err = verify_rgba8888_image(input_ptr[i], output_ptr, img_width, img_height);
264                 break;
265         }
266 
267         if (err)
268             break;
269     }
270 
271     // cleanup
272     clReleaseSampler(sampler);
273 
274     if(supportsBGRA)
275             clReleaseMemObject(streams[0]);
276 
277     clReleaseMemObject(streams[1]);
278     clReleaseMemObject(streams[2]);
279     for (i=0; i<2; i++)
280     {
281         if(i == 0 && !supportsBGRA)
282             continue;
283 
284         clReleaseKernel(kernel[i]);
285         clReleaseProgram(program[i]);
286     }
287     free(input_ptr[0]);
288     free(input_ptr[1]);
289     free(output_ptr);
290 
291     return err;
292 }
293