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