xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/basic/test_multireadimagemultifmt.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
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_READ_WRITE, &img_format,
140                                  img_width, img_height, 0, NULL, NULL);
141     if (!streams[0])
142     {
143         log_error("create_image_2d failed\n");
144         return -1;
145     }
146     img_format.image_channel_order = CL_RGBA;
147     img_format.image_channel_data_type = CL_UNORM_INT16;
148     streams[1] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
149                                  img_width, img_height, 0, NULL, NULL);
150     if (!streams[1])
151     {
152         log_error("create_image_2d failed\n");
153         return -1;
154     }
155     img_format.image_channel_order = CL_RGBA;
156     img_format.image_channel_data_type = CL_FLOAT;
157     streams[2] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
158                                  img_width, img_height, 0, NULL, NULL);
159     if (!streams[2])
160     {
161         log_error("create_image_2d failed\n");
162         return -1;
163     }
164 
165     streams[3] =
166         clCreateBuffer(context, CL_MEM_READ_WRITE,
167                        sizeof(float) * 4 * img_width * img_height, NULL, NULL);
168     if (!streams[3])
169     {
170         log_error("clCreateBuffer failed\n");
171         return -1;
172     }
173 
174     for (i=0; i<3; i++)
175     {
176       size_t origin[3] = {0,0,0}, region[3]={img_width, img_height,1};
177       err = clEnqueueWriteImage(queue, streams[i], CL_TRUE, origin, region, 0, 0, input_ptr[i], 0, NULL, NULL);
178         if (err != CL_SUCCESS)
179         {
180             log_error("clWriteImage failed\n");
181             return -1;
182         }
183     }
184 
185     err = create_single_kernel_helper( context, &program, &kernel, 1, &multireadimage_kernel_code, "test_multireadimage");
186     if (err)
187         return -1;
188 
189     cl_sampler sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err);
190     test_error(err, "clCreateSampler failed");
191 
192     for (i=0; i<4; i++)
193       err |= clSetKernelArg(kernel, i,sizeof streams[i], &streams[i]);
194     err |= clSetKernelArg(kernel, 4, sizeof sampler, &sampler);
195 
196     if (err != CL_SUCCESS)
197     {
198         log_error("clSetKernelArgs failed\n");
199         return -1;
200     }
201 
202     threads[0] = (size_t)img_width;
203     threads[1] = (size_t)img_height;
204 
205     err = clEnqueueNDRangeKernel( queue, kernel, 2, NULL, threads, NULL, 0, NULL, NULL );
206     if (err != CL_SUCCESS)
207     {
208         log_error("clEnqueueNDRangeKernel failed\n");
209         return -1;
210     }
211     err = clEnqueueReadBuffer( queue, streams[3], CL_TRUE, 0, sizeof(float)*4*img_width*img_height, (void *)output_ptr, 0, NULL, NULL );
212     if (err != CL_SUCCESS)
213     {
214         log_error("clEnqueueReadBuffer failed\n");
215         return -1;
216     }
217 
218     err = verify_multireadimage(input_ptr, (float*)output_ptr, img_width, img_height);
219 
220     // cleanup
221     clReleaseSampler(sampler);
222     for (i=0; i<4; i++)
223         clReleaseMemObject(streams[i]);
224     clReleaseKernel(kernel);
225     clReleaseProgram(program);
226     for (i=0; i<3; i++)
227         free(input_ptr[i]);
228     free(output_ptr);
229 
230     return err;
231 }
232 
233 
234 
235 
236 
237