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