• 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 unsigned char *
generate_rgba8_image(int w,int h,MTdata d)28 generate_rgba8_image(int w, int h, MTdata d)
29 {
30     unsigned char   *ptr = (unsigned char*)malloc(w * h * 4);
31     int             i;
32 
33     for (i=0; i<w*h*4; i++)
34         ptr[i] = (unsigned char)genrand_int32(d);
35 
36     return ptr;
37 }
38 
39 static int
verify_rgba8_image(unsigned char * image,unsigned char * outptr,int x,int y,int w,int h,int img_width)40 verify_rgba8_image(unsigned char *image, unsigned char *outptr, int x, int y, int w, int h, int img_width)
41 {
42     int     i, j, indx;
43 
44     for (j=y; j<(y+h); j++)
45     {
46         indx = j*img_width*4;
47         for (i=x*4; i<(x+w)*4; i++)
48         {
49             if (outptr[indx+i] != image[indx+i])
50                 return -1;
51         }
52     }
53     return 0;
54 }
55 
56 
57 static unsigned short *
generate_rgba16_image(int w,int h,MTdata d)58 generate_rgba16_image(int w, int h, MTdata d)
59 {
60     unsigned short    *ptr = (unsigned short*)malloc(w * h * 4 * sizeof(unsigned short));
61     int             i;
62 
63     for (i=0; i<w*h*4; i++)
64         ptr[i] = (unsigned short)genrand_int32(d);
65 
66     return ptr;
67 }
68 
69 static int
verify_rgba16_image(unsigned short * image,unsigned short * outptr,int x,int y,int w,int h,int img_width)70 verify_rgba16_image(unsigned short *image, unsigned short *outptr, int x, int y, int w, int h, int img_width)
71 {
72     int     i, j, indx;
73 
74     for (j=y; j<(y+h); j++)
75     {
76         indx = j*img_width*4;
77         for (i=x*4; i<(x+w)*4; i++)
78         {
79             if (outptr[indx+i] != image[indx+i])
80                 return -1;
81         }
82     }
83     return 0;
84 }
85 
86 
87 static float *
generate_rgbafp_image(int w,int h,MTdata d)88 generate_rgbafp_image(int w, int h, MTdata d)
89 {
90     float   *ptr = (float*)malloc(w * h * 4 * sizeof(float));
91     int     i;
92 
93     for (i=0; i<w*h*4; i++)
94         ptr[i] = get_random_float(-0x40000000, 0x40000000, d);
95 
96     return ptr;
97 }
98 
99 static int
verify_rgbafp_image(float * image,float * outptr,int x,int y,int w,int h,int img_width)100 verify_rgbafp_image(float *image, float *outptr, int x, int y, int w, int h, int img_width)
101 {
102     int     i, j, indx;
103 
104     for (j=y; j<(y+h); j++)
105     {
106         indx = j*img_width*4;
107         for (i=x*4; i<(x+w)*4; i++)
108         {
109             if (outptr[indx+i] != image[indx+i])
110                 return -1;
111         }
112     }
113     return 0;
114 }
115 
116 
117 #define NUM_COPIES    10
118 static const char *test_str_names[] = { "CL_RGBA CL_UNORM_INT8", "CL_RGBA CL_UNORM_INT16", "CL_RGBA CL_FLOAT" };
119 
120 int
test_imagerandomcopy(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)121 test_imagerandomcopy(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
122 {
123     cl_image_format    img_format;
124     unsigned char    *rgba8_inptr, *rgba8_outptr;
125     unsigned short    *rgba16_inptr, *rgba16_outptr;
126     float            *rgbafp_inptr, *rgbafp_outptr;
127     clMemWrapper            streams[6];
128     int                img_width = 512;
129     int                img_height = 512;
130     int                i, j;
131     cl_int          err;
132     MTdata          d;
133 
134     PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
135 
136     log_info("Testing with image %d x %d.\n", img_width, img_height);
137 
138     d = init_genrand( gRandomSeed );
139     rgba8_inptr = (unsigned char *)generate_rgba8_image(img_width, img_height, d);
140     rgba16_inptr = (unsigned short *)generate_rgba16_image(img_width, img_height, d);
141     rgbafp_inptr = (float *)generate_rgbafp_image(img_width, img_height, d);
142 
143     rgba8_outptr = (unsigned char*)malloc(sizeof(unsigned char) * 4 * img_width * img_height);
144     rgba16_outptr = (unsigned short*)malloc(sizeof(unsigned short) * 4 * img_width * img_height);
145     rgbafp_outptr = (float*)malloc(sizeof(float) * 4 * img_width * img_height);
146 
147     img_format.image_channel_order = CL_RGBA;
148     img_format.image_channel_data_type = CL_UNORM_INT8;
149     streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
150                                  img_width, img_height, 0, NULL, &err);
151     test_error(err, "create_image_2d failed");
152     streams[1] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
153                                  img_width, img_height, 0, NULL, &err);
154     test_error(err, "create_image_2d failed");
155 
156     img_format.image_channel_order = CL_RGBA;
157     img_format.image_channel_data_type = CL_UNORM_INT16;
158     streams[2] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
159                                  img_width, img_height, 0, NULL, &err);
160     test_error(err, "create_image_2d failed");
161     streams[3] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
162                                  img_width, img_height, 0, NULL, &err);
163     test_error(err, "create_image_2d failed");
164 
165     img_format.image_channel_order = CL_RGBA;
166     img_format.image_channel_data_type = CL_FLOAT;
167     streams[4] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
168                                  img_width, img_height, 0, NULL, &err);
169     test_error(err, "create_image_2d failed");
170     streams[5] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
171                                  img_width, img_height, 0, NULL, &err);
172     test_error(err, "create_image_2d failed");
173 
174     for (i=0; i<3; i++)
175     {
176         void            *p, *outp;
177         unsigned int    x[2], y[2], delta_w, delta_h ;
178 
179         switch (i)
180         {
181             case 0:
182                 p = (void *)rgba8_inptr;
183                 outp = (void *)rgba8_outptr;
184                 break;
185             case 1:
186                 p = (void *)rgba16_inptr;
187                 outp = (void *)rgba16_outptr;
188                 break;
189             case 2:
190                 p = (void *)rgbafp_inptr;
191                 outp = (void *)rgbafp_outptr;
192                 break;
193         }
194 
195         size_t origin[3]={0,0,0}, region[3]={img_width, img_height,1};
196         err = clEnqueueWriteImage(queue, streams[i*2], CL_TRUE, origin, region, 0, 0, p, 0, NULL, NULL);
197 //        err = clWriteImage(context, streams[i*2], false, 0, 0, 0, img_width, img_height, 0, NULL, 0, 0, p, NULL);
198         test_error(err, "clEnqueueWriteImage failed");
199 
200         for (j=0; j<NUM_COPIES; j++)
201         {
202             x[0] = (int)get_random_float(0, img_width, d);
203             do
204             {
205                 x[1] = (int)get_random_float(0, img_width, d);
206             } while (x[1] <= x[0]);
207 
208             y[0] = (int)get_random_float(0, img_height, d);
209             do
210             {
211                 y[1] = (int)get_random_float(0, img_height, d);
212             } while (y[1] <= y[0]);
213 
214             delta_w = x[1] - x[0];
215             delta_h = y[1] - y[0];
216             log_info("Testing clCopyImage for %s: x = %d, y = %d, w = %d, h = %d\n", test_str_names[i], x[0], y[0], delta_w, delta_h);
217             origin[0] = x[0];
218             origin[1] = y[0];
219             origin[2] = 0;
220             region[0] = delta_w;
221             region[1] = delta_h;
222             region[2] = 1;
223             err = clEnqueueCopyImage(queue, streams[i*2], streams[i*2+1], origin, origin, region, 0, NULL, NULL);
224 //          err = clCopyImage(context, streams[i*2], streams[i*2+1],
225 //                              x[0], y[0], 0, x[0], y[0], 0, delta_w, delta_h, 0, NULL);
226             test_error(err, "clEnqueueCopyImage failed");
227 
228             origin[0] = 0;
229             origin[1] = 0;
230             origin[2] = 0;
231             region[0] = img_width;
232             region[1] = img_height;
233             region[2] = 1;
234             err = clEnqueueReadImage(queue, streams[i*2+1], CL_TRUE, origin, region, 0, 0, outp, 0, NULL, NULL);
235 //            err = clReadImage(context, streams[i*2+1], false, 0, 0, 0, img_width, img_height, 0, 0, 0, outp, NULL);
236             test_error(err, "clEnqueueReadImage failed");
237 
238             switch (i)
239             {
240                 case 0:
241                     err = verify_rgba8_image(rgba8_inptr, rgba8_outptr, x[0], y[0], delta_w, delta_h, img_width);
242                     break;
243                 case 1:
244                     err = verify_rgba16_image(rgba16_inptr, rgba16_outptr, x[0], y[0], delta_w, delta_h, img_width);
245                     break;
246                 case 2:
247                     err = verify_rgbafp_image(rgbafp_inptr, rgbafp_outptr, x[0], y[0], delta_w, delta_h, img_width);
248                     break;
249             }
250 
251             if (err)
252                 break;
253         }
254 
255         if (err)
256             break;
257     }
258 
259     free_mtdata(d); d = NULL;
260     free(rgba8_inptr);
261     free(rgba16_inptr);
262     free(rgbafp_inptr);
263     free(rgba8_outptr);
264     free(rgba16_outptr);
265     free(rgbafp_outptr);
266 
267     if (err)
268         log_error("IMAGE random copy test failed\n");
269     else
270         log_info("IMAGE random copy test passed\n");
271 
272     return err;
273 }
274 
275 
276 
277