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 *multireadimage_kernel_code =
28 "__kernel void test_multireadimage(read_only image2d_t img0, read_only image2d_t img1, \n"
29 " read_only image2d_t img2, __global float4 *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 " int2 tid = (int2)(tid_x, tid_y);\n"
34 " int indx = tid_y * get_image_width(img1) + tid_x;\n"
35 " float4 sum;\n"
36 "\n"
37 " sum = read_imagef(img0, sampler, tid);\n"
38 " sum += read_imagef(img1, sampler, tid);\n"
39 " sum += read_imagef(img2, sampler, tid);\n"
40 "\n"
41 " dst[indx] = sum;\n"
42 "}\n";
43
44 #define MAX_ERR 1e-7f
45
46 static unsigned char *
generate_8888_image(int w,int h,MTdata d)47 generate_8888_image(int w, int h, MTdata d)
48 {
49 unsigned char *ptr = (unsigned char*)malloc(w * h * 4);
50 int i;
51
52 for (i=0; i<w*h*4; i++)
53 ptr[i] = (unsigned char)genrand_int32(d);
54
55 return ptr;
56 }
57
58 static unsigned short *
generate_16bit_image(int w,int h,MTdata d)59 generate_16bit_image(int w, int h, MTdata d)
60 {
61 unsigned short *ptr = (unsigned short*)malloc(w * h * 4 * sizeof(unsigned short));
62 int i;
63
64 for (i=0; i<w*h*4; i++)
65 ptr[i] = (unsigned short)genrand_int32(d);
66
67 return ptr;
68 }
69
70 static float *
generate_float_image(int w,int h,MTdata d)71 generate_float_image(int w, int h, MTdata d)
72 {
73 float *ptr = (float*)malloc(w * h * 4 * (int)sizeof(float));
74 int i;
75
76 for (i=0; i<w*h*4; i++)
77 ptr[i] = get_random_float(-0x40000000, 0x40000000, d);
78
79 return ptr;
80 }
81
82
83 static int
verify_multireadimage(void * image[],float * outptr,int w,int h)84 verify_multireadimage(void *image[], float *outptr, int w, int h)
85 {
86 int i;
87 float sum;
88 float ulp, max_ulp = 0.0f;
89
90 // ULP error of 1.5 for each read_imagef plus 0.5 for each addition.
91 float max_ulp_allowed = (float)(3*1.5+2*0.5);
92
93 for (i=0; i<w*h*4; i++)
94 {
95 sum = (float)((unsigned char *)image[0])[i] / 255.0f;
96 sum += (float)((unsigned short *)image[1])[i] / 65535.0f;
97 sum += (float)((float *)image[2])[i];
98 ulp = Ulp_Error(outptr[i], sum);
99 if (ulp > max_ulp)
100 max_ulp = ulp;
101 }
102
103 if (max_ulp > max_ulp_allowed) {
104 log_error("READ_MULTIREADIMAGE_MULTIFORMAT test failed. Max ulp error = %g\n", max_ulp);
105 return -1;
106 }
107
108 log_info("READ_MULTIREADIMAGE_MULTIFORMAT test passed. Max ulp error = %g\n", max_ulp);
109 return 0;
110 }
111
112
113 int
test_mri_multiple(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)114 test_mri_multiple(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
115 {
116 cl_mem streams[4];
117 cl_image_format img_format;
118 void *input_ptr[3], *output_ptr;
119 cl_program program;
120 cl_kernel kernel;
121 size_t threads[2];
122 int img_width = 512;
123 int img_height = 512;
124 int i, err;
125 MTdata d;
126
127 PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
128
129 d = init_genrand( gRandomSeed );
130 input_ptr[0] = (void *)generate_8888_image(img_width, img_height, d);
131 input_ptr[1] = (void *)generate_16bit_image(img_width, img_height, d);
132 input_ptr[2] = (void *)generate_float_image(img_width, img_height, d);
133 free_mtdata(d); d = NULL;
134
135 output_ptr = (void *)malloc(sizeof(float) * 4 * img_width * img_height);
136
137 img_format.image_channel_order = CL_RGBA;
138 img_format.image_channel_data_type = CL_UNORM_INT8;
139 streams[0] = create_image_2d(context, (cl_mem_flags)(CL_MEM_READ_WRITE), &img_format, img_width, img_height, 0, NULL, NULL);
140 if (!streams[0])
141 {
142 log_error("create_image_2d failed\n");
143 return -1;
144 }
145 img_format.image_channel_order = CL_RGBA;
146 img_format.image_channel_data_type = CL_UNORM_INT16;
147 streams[1] = create_image_2d(context, (cl_mem_flags)(CL_MEM_READ_WRITE), &img_format, img_width, img_height, 0, NULL, NULL);
148 if (!streams[1])
149 {
150 log_error("create_image_2d failed\n");
151 return -1;
152 }
153 img_format.image_channel_order = CL_RGBA;
154 img_format.image_channel_data_type = CL_FLOAT;
155 streams[2] = create_image_2d(context, (cl_mem_flags)(CL_MEM_READ_WRITE), &img_format, img_width, img_height, 0, NULL, NULL);
156 if (!streams[2])
157 {
158 log_error("create_image_2d failed\n");
159 return -1;
160 }
161
162 streams[3] = clCreateBuffer(context, (cl_mem_flags)(CL_MEM_READ_WRITE), sizeof(float)*4 * img_width*img_height, NULL, NULL);
163 if (!streams[3])
164 {
165 log_error("clCreateBuffer failed\n");
166 return -1;
167 }
168
169 for (i=0; i<3; i++)
170 {
171 size_t origin[3] = {0,0,0}, region[3]={img_width, img_height,1};
172 err = clEnqueueWriteImage(queue, streams[i], CL_TRUE, origin, region, 0, 0, input_ptr[i], 0, NULL, NULL);
173 if (err != CL_SUCCESS)
174 {
175 log_error("clWriteImage failed\n");
176 return -1;
177 }
178 }
179
180 err = create_single_kernel_helper( context, &program, &kernel, 1, &multireadimage_kernel_code, "test_multireadimage");
181 if (err)
182 return -1;
183
184 cl_sampler sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err);
185 test_error(err, "clCreateSampler failed");
186
187 for (i=0; i<4; i++)
188 err |= clSetKernelArg(kernel, i,sizeof streams[i], &streams[i]);
189 err |= clSetKernelArg(kernel, 4, sizeof sampler, &sampler);
190
191 if (err != CL_SUCCESS)
192 {
193 log_error("clSetKernelArgs failed\n");
194 return -1;
195 }
196
197 threads[0] = (size_t)img_width;
198 threads[1] = (size_t)img_height;
199
200 err = clEnqueueNDRangeKernel( queue, kernel, 2, NULL, threads, NULL, 0, NULL, NULL );
201 if (err != CL_SUCCESS)
202 {
203 log_error("clEnqueueNDRangeKernel failed\n");
204 return -1;
205 }
206 err = clEnqueueReadBuffer( queue, streams[3], CL_TRUE, 0, sizeof(float)*4*img_width*img_height, (void *)output_ptr, 0, NULL, NULL );
207 if (err != CL_SUCCESS)
208 {
209 log_error("clEnqueueReadBuffer failed\n");
210 return -1;
211 }
212
213 err = verify_multireadimage(input_ptr, (float*)output_ptr, img_width, img_height);
214
215 // cleanup
216 clReleaseSampler(sampler);
217 for (i=0; i<4; i++)
218 clReleaseMemObject(streams[i]);
219 clReleaseKernel(kernel);
220 clReleaseProgram(program);
221 for (i=0; i<3; i++)
222 free(input_ptr[i]);
223 free(output_ptr);
224
225 return err;
226 }
227
228
229
230
231
232