xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/basic/test_arrayimagecopy.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 #include "harness/imageHelpers.h"
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <vector>
25 
26 #include "procs.h"
27 
test_arrayimagecopy_single_format(cl_device_id device,cl_context context,cl_command_queue queue,cl_mem_flags flags,cl_mem_object_type image_type,const cl_image_format * format)28 int test_arrayimagecopy_single_format(cl_device_id device, cl_context context,
29                                       cl_command_queue queue,
30                                       cl_mem_flags flags,
31                                       cl_mem_object_type image_type,
32                                       const cl_image_format *format)
33 {
34     cl_uchar *bufptr, *imgptr;
35     clMemWrapper buffer, image;
36     int img_width = 512;
37     int img_height = 512;
38     int img_depth = (image_type == CL_MEM_OBJECT_IMAGE3D) ? 32 : 1;
39     size_t elem_size;
40     size_t buffer_size;
41     cl_int err;
42     cl_event copyevent;
43 
44     log_info("Testing %s %s\n",
45              GetChannelOrderName(format->image_channel_order),
46              GetChannelTypeName(format->image_channel_data_type));
47 
48     if (CL_MEM_OBJECT_IMAGE2D == image_type)
49     {
50         image = create_image_2d(context, flags, format, img_width, img_height,
51                                 0, nullptr, &err);
52     }
53     else
54     {
55         image = create_image_3d(context, flags, format, img_width, img_height,
56                                 img_depth, 0, 0, nullptr, &err);
57     }
58     test_error(err, "create_image_xd failed");
59 
60     err = clGetImageInfo(image, CL_IMAGE_ELEMENT_SIZE, sizeof(size_t),
61                          &elem_size, NULL);
62     test_error(err, "clGetImageInfo failed");
63 
64     buffer_size =
65         sizeof(cl_uchar) * elem_size * img_width * img_height * img_depth;
66 
67     buffer =
68         clCreateBuffer(context, CL_MEM_READ_WRITE, buffer_size, NULL, &err);
69     test_error(err, "clCreateBuffer failed");
70 
71     RandomSeed seed(gRandomSeed);
72     bufptr =
73         static_cast<cl_uchar *>(create_random_data(kUChar, seed, buffer_size));
74 
75     size_t origin[3] = { 0, 0, 0 },
76            region[3] = { img_width, img_height, img_depth };
77     err = clEnqueueWriteBuffer(queue, buffer, CL_TRUE, 0, buffer_size, bufptr,
78                                0, NULL, NULL);
79     test_error(err, "clEnqueueWriteBuffer failed");
80 
81     err = clEnqueueCopyBufferToImage(queue, buffer, image, 0, origin, region, 0,
82                                      NULL, &copyevent);
83     test_error(err, "clEnqueueCopyImageToBuffer failed");
84 
85     imgptr = static_cast<cl_uchar *>(malloc(buffer_size));
86 
87     err = clEnqueueReadImage(queue, image, CL_TRUE, origin, region, 0, 0,
88                              imgptr, 1, &copyevent, NULL);
89     test_error(err, "clEnqueueReadImage failed");
90 
91     err = clReleaseEvent(copyevent);
92     test_error(err, "clReleaseEvent failed");
93 
94     if (memcmp(bufptr, imgptr, buffer_size) != 0)
95     {
96         log_error("ERROR: Results did not validate!\n");
97         auto inchar = static_cast<unsigned char *>(bufptr);
98         auto outchar = static_cast<unsigned char *>(imgptr);
99         int failuresPrinted = 0;
100         for (int i = 0; i < (int)buffer_size; i += (int)elem_size)
101         {
102             if (memcmp(&inchar[i], &outchar[i], elem_size) != 0)
103             {
104                 log_error("%d(0x%x) -> actual [", i, i);
105                 for (int j = 0; j < (int)elem_size; j++)
106                     log_error("0x%02x ", inchar[i + j]);
107                 log_error("] != expected [");
108                 for (int j = 0; j < (int)elem_size; j++)
109                     log_error("0x%02x ", outchar[i + j]);
110                 log_error("]\n");
111                 failuresPrinted++;
112             }
113             if (failuresPrinted > 5)
114             {
115                 log_error("Not printing further failures...\n");
116                 break;
117             }
118         }
119         err = -1;
120     }
121 
122     free(bufptr);
123     free(imgptr);
124 
125     if (err)
126         log_error(
127             "ARRAY to IMAGE copy test failed for image_channel_order=0x%lx and "
128             "image_channel_data_type=0x%lx\n",
129             (unsigned long)format->image_channel_order,
130             (unsigned long)format->image_channel_data_type);
131 
132     return err;
133 }
134 
135 
test_arrayimagecommon(cl_device_id device,cl_context context,cl_command_queue queue,cl_mem_flags flags,cl_mem_object_type image_type)136 int test_arrayimagecommon(cl_device_id device, cl_context context,
137                           cl_command_queue queue, cl_mem_flags flags,
138                           cl_mem_object_type image_type)
139 {
140     cl_int err;
141     cl_uint num_formats;
142 
143     err = clGetSupportedImageFormats(context, flags, image_type, 0, NULL,
144                                      &num_formats);
145     test_error(err, "clGetSupportedImageFormats failed");
146 
147     std::vector<cl_image_format> formats(num_formats);
148 
149     err = clGetSupportedImageFormats(context, flags, image_type, num_formats,
150                                      formats.data(), NULL);
151     test_error(err, "clGetSupportedImageFormats failed");
152 
153     for (const auto &format : formats)
154     {
155         err |= test_arrayimagecopy_single_format(device, context, queue, flags,
156                                                  image_type, &format);
157     }
158 
159     if (err)
160         log_error("ARRAY to IMAGE%s copy test failed\n",
161                   convert_image_type_to_string(image_type));
162     else
163         log_info("ARRAY to IMAGE%s copy test passed\n",
164                  convert_image_type_to_string(image_type));
165 
166     return err;
167 }
168 
test_arrayimagecopy(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)169 int test_arrayimagecopy(cl_device_id device, cl_context context,
170                         cl_command_queue queue, int num_elements)
171 {
172     PASSIVE_REQUIRE_IMAGE_SUPPORT(device)
173 
174     return test_arrayimagecommon(device, context, queue, CL_MEM_READ_WRITE,
175                                  CL_MEM_OBJECT_IMAGE2D);
176 }
177 
178 
test_arrayimagecopy3d(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)179 int test_arrayimagecopy3d(cl_device_id device, cl_context context,
180                           cl_command_queue queue, int num_elements)
181 {
182     PASSIVE_REQUIRE_3D_IMAGE_SUPPORT(device)
183 
184     return test_arrayimagecommon(device, context, queue, CL_MEM_READ_ONLY,
185                                  CL_MEM_OBJECT_IMAGE3D);
186 }
187