• 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_write_kernel_code =
28 "\n"
29 "__kernel void test_bgra8888_write(__global unsigned char *src, write_only image2d_t dstimg)\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(dstimg) + tid_x;\n"
34 "    float4         color;\n"
35 "\n"
36 "    indx *= 4;\n"
37 "    color = (float4)((float)src[indx+2], (float)src[indx+1], (float)src[indx+0], (float)src[indx+3]);\n"
38 "    color /= (float4)(255.0f, 255.0f, 255.0f, 255.0f);\n"
39 "    write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
40 "\n"
41 "}\n";
42 
43 
44 static const char *rgba8888_write_kernel_code =
45 "\n"
46 "__kernel void test_rgba8888_write(__global unsigned char *src, write_only image2d_t dstimg)\n"
47 "{\n"
48 "    int            tid_x = get_global_id(0);\n"
49 "    int            tid_y = get_global_id(1);\n"
50 "    int            indx = tid_y * get_image_width(dstimg) + tid_x;\n"
51 "    float4         color;\n"
52 "\n"
53 "    indx *= 4;\n"
54 "    color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);\n"
55 "    color /= (float4)(255.0f, 255.0f, 255.0f, 255.0f);\n"
56 "    write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
57 "\n"
58 "}\n";
59 
60 
61 static unsigned char *
generate_8888_image(int w,int h,MTdata d)62 generate_8888_image(int w, int h, MTdata d)
63 {
64     cl_uchar   *ptr = (cl_uchar *)malloc(w * h * 4);
65     int             i;
66 
67     for (i=0; i<w*h*4; i++)
68         ptr[i] = (cl_uchar)genrand_int32(d);
69 
70     return ptr;
71 }
72 
73 static int
verify_bgra8888_image(unsigned char * image,unsigned char * outptr,int w,int h)74 verify_bgra8888_image(unsigned char *image, unsigned char *outptr, int w, int h)
75 {
76     int     i;
77 
78     for (i=0; i<w*h*4; i++)
79     {
80         if (outptr[i] != image[i])
81         {
82             log_error("WRITE_IMAGE_BGRA_UNORM_INT8 test failed\n");
83             return -1;
84         }
85     }
86 
87     log_info("WRITE_IMAGE_BGRA_UNORM_INT8 test passed\n");
88     return 0;
89 }
90 
91 static int
verify_rgba8888_image(unsigned char * image,unsigned char * outptr,int w,int h)92 verify_rgba8888_image(unsigned char *image, unsigned char *outptr, int w, int h)
93 {
94     int     i;
95 
96     for (i=0; i<w*h*4; i++)
97     {
98         if (outptr[i] != image[i])
99         {
100             log_error("WRITE_IMAGE_RGBA_UNORM_INT8 test failed\n");
101             return -1;
102         }
103     }
104 
105     log_info("WRITE_IMAGE_RGBA_UNORM_INT8 test passed\n");
106     return 0;
107 }
108 
109 
test_writeimage(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)110 int test_writeimage(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
111 {
112     cl_mem streams[6];
113     cl_program program[2];
114     cl_kernel kernel[4];
115 
116     unsigned char    *input_ptr[2], *output_ptr;
117     cl_image_format    img_format;
118     cl_image_format *supported_formats;
119     size_t threads[2];
120     int img_width = 512;
121     int img_height = 512;
122     int i, err, any_err = 0;
123     size_t origin[3] = {0, 0, 0};
124     size_t region[3] = {img_width, img_height, 1};
125     size_t length = img_width * img_height * 4 * sizeof(unsigned char);
126     int supportsBGRA = 0;
127     cl_uint numFormats = 0;
128 
129     PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
130 
131     MTdata d = init_genrand( gRandomSeed );
132     input_ptr[0] = generate_8888_image(img_width, img_height, d);
133     input_ptr[1] = generate_8888_image(img_width, img_height, d);
134     free_mtdata(d); d = NULL;
135     output_ptr = (unsigned char*)malloc(length);
136 
137     if(gIsEmbedded)
138     {
139         /* Get the supported image formats to see if BGRA is supported */
140         clGetSupportedImageFormats (context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, NULL, &numFormats);
141         supported_formats = (cl_image_format *) malloc(sizeof(cl_image_format) * numFormats);
142         clGetSupportedImageFormats (context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, numFormats, supported_formats, NULL);
143 
144         for(i = 0; i < numFormats; i++)
145         {
146             if(supported_formats[i].image_channel_order == CL_BGRA)
147             {
148                     supportsBGRA = 1;
149                     break;
150             }
151         }
152     }
153     else
154     {
155         supportsBGRA = 1;
156     }
157 
158     if(supportsBGRA)
159     {
160         img_format.image_channel_order = CL_BGRA;
161         img_format.image_channel_data_type = CL_UNORM_INT8;
162         streams[0] = clCreateImage2D(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
163         if (!streams[0])
164         {
165             log_error("clCreateImage2D failed\n");
166             return -1;
167         }
168     }
169 
170     img_format.image_channel_order = CL_RGBA;
171     img_format.image_channel_data_type = CL_UNORM_INT8;
172     streams[1] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
173     if (!streams[1])
174     {
175         log_error("create_image_2d failed\n");
176         return -1;
177     }
178 
179     if(supportsBGRA)
180     {
181         img_format.image_channel_order = CL_BGRA;
182         img_format.image_channel_data_type = CL_UNORM_INT8;
183         streams[2] = clCreateImage2D(context, CL_MEM_WRITE_ONLY, &img_format, img_width, img_height, 0, NULL, NULL);
184         if (!streams[2])
185         {
186             log_error("clCreateImage2D failed\n");
187             return -1;
188         }
189     }
190 
191     img_format.image_channel_order = CL_RGBA;
192     img_format.image_channel_data_type = CL_UNORM_INT8;
193     streams[3] = create_image_2d(context, CL_MEM_WRITE_ONLY, &img_format, img_width, img_height, 0, NULL, NULL);
194     if (!streams[3])
195     {
196         log_error("create_image_2d failed\n");
197         return -1;
198     }
199 
200     streams[4] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
201     if (!streams[4])
202     {
203         log_error("clCreateBuffer failed\n");
204         return -1;
205     }
206     streams[5] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
207     if (!streams[5])
208     {
209         log_error("clCreateBuffer failed\n");
210         return -1;
211     }
212 
213     err = clEnqueueWriteBuffer(queue, streams[4], CL_TRUE, 0, length, input_ptr[0], 0, NULL, NULL);
214     if (err != CL_SUCCESS)
215     {
216         log_error("clEnqueueWriteBuffer failed\n");
217         return -1;
218     }
219     err = clEnqueueWriteBuffer(queue, streams[5], CL_TRUE, 0, length, input_ptr[1], 0, NULL, NULL);
220     if (err != CL_SUCCESS)
221     {
222         log_error("clEnqueueWriteBuffer failed\n");
223         return -1;
224     }
225 
226     if(supportsBGRA)
227     {
228         err = create_single_kernel_helper(context, &program[0], &kernel[0], 1, &bgra8888_write_kernel_code, "test_bgra8888_write" );
229         if (err)
230                 return -1;
231 
232         kernel[2] = clCreateKernel(program[0], "test_bgra8888_write", NULL);
233         if (!kernel[2])
234         {
235                 log_error("clCreateKernel failed\n");
236                 return -1;
237         }
238     }
239 
240     err = create_single_kernel_helper(context, &program[1], &kernel[1], 1, &rgba8888_write_kernel_code, "test_rgba8888_write" );
241     if (err)
242     return -1;
243     kernel[3] = clCreateKernel(program[1], "test_rgba8888_write", NULL);
244     if (!kernel[3])
245     {
246         log_error("clCreateKernel failed\n");
247         return -1;
248     }
249 
250     if(supportsBGRA)
251     {
252         err  = clSetKernelArg(kernel[0], 0, sizeof streams[4], &streams[4]);
253         err |= clSetKernelArg(kernel[0], 1, sizeof streams[0], &streams[0]);
254         if (err != CL_SUCCESS)
255         {
256             log_error("clSetKernelArgs failed\n");
257             return -1;
258         }
259     }
260 
261     err  = clSetKernelArg(kernel[1], 0, sizeof streams[5], &streams[5]);
262     err |= clSetKernelArg(kernel[1], 1, sizeof streams[1], &streams[1]);
263     if (err != CL_SUCCESS)
264     {
265         log_error("clSetKernelArgs failed\n");
266         return -1;
267     }
268 
269     if(supportsBGRA)
270     {
271         err  = clSetKernelArg(kernel[2], 0, sizeof streams[4], &streams[4]);
272         err |= clSetKernelArg(kernel[2], 1, sizeof streams[2], &streams[2]);
273         if (err != CL_SUCCESS)
274         {
275             log_error("clSetKernelArgs failed\n");
276             return -1;
277         }
278     }
279 
280     err  = clSetKernelArg(kernel[3], 0, sizeof streams[5], &streams[5]);
281     err |= clSetKernelArg(kernel[3], 1, sizeof streams[3], &streams[3]);
282     if (err != CL_SUCCESS)
283     {
284         log_error("clSetKernelArgs failed\n");
285         return -1;
286     }
287 
288     threads[0] = (unsigned int)img_width;
289     threads[1] = (unsigned int)img_height;
290 
291     for (i=0; i<4; i++)
292     {
293          if(!supportsBGRA && (i == 0 || i == 2))
294             continue;
295 
296         err = clEnqueueNDRangeKernel(queue, kernel[i], 2, NULL, threads, NULL, 0, NULL, NULL);
297         if (err != CL_SUCCESS)
298         {
299             log_error("clEnqueueNDRangeKernel failed\n");
300             return -1;
301         }
302 
303         err = clEnqueueReadImage(queue, streams[i], CL_TRUE, origin, region, 0, 0, output_ptr, 0, NULL, NULL);
304         if (err != CL_SUCCESS)
305         {
306             log_error("clReadImage failed\n");
307             return -1;
308         }
309 
310         switch (i)
311         {
312             case 0:
313             case 2:
314                 err = verify_bgra8888_image(input_ptr[i&0x01], output_ptr, img_width, img_height);
315                 break;
316             case 1:
317             case 3:
318                 err = verify_rgba8888_image(input_ptr[i&0x01], output_ptr, img_width, img_height);
319                 break;
320         }
321 
322         //if (err)
323         //break;
324 
325         any_err |= err;
326     }
327 
328     // cleanup
329     if(supportsBGRA)
330         clReleaseMemObject(streams[0]);
331 
332     clReleaseMemObject(streams[1]);
333 
334     if(supportsBGRA)
335         clReleaseMemObject(streams[2]);
336 
337     clReleaseMemObject(streams[3]);
338     clReleaseMemObject(streams[4]);
339     clReleaseMemObject(streams[5]);
340     for (i=0; i<2; i++)
341     {
342         if(i == 0 && !supportsBGRA)
343             continue;
344 
345         clReleaseKernel(kernel[i]);
346         clReleaseKernel(kernel[i+2]);
347         clReleaseProgram(program[i]);
348     }
349     free(input_ptr[0]);
350     free(input_ptr[1]);
351     free(output_ptr);
352 
353     return any_err;
354 }
355