xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/images/kernel_read_write/test_common.h (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 //
2 // Copyright (c) 2021 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 
17 #include "../testBase.h"
18 
19 #define ABS_ERROR(result, expected) (fabs(expected - result))
20 #define CLAMP(_val, _min, _max)                                                \
21     ((_val) < (_min) ? (_min) : (_val) > (_max) ? (_max) : (_val))
22 
23 #define MAX_ERR 0.005f
24 #define MAX_TRIES 1
25 #define MAX_CLAMPED 1
26 
27 extern cl_sampler create_sampler(cl_context context, image_sampler_data *sdata, bool test_mipmaps, cl_int *error);
28 extern void read_image_pixel_float(void *imageData, image_descriptor *imageInfo,
29                                    int x, int y, int z, float *outData);
30 
31 extern bool gExtraValidateInfo;
32 extern bool gDisableOffsets;
33 extern bool gUseKernelSamplers;
34 extern cl_mem_flags gMemFlagsToUse;
35 extern int gtestTypesToRun;
36 extern uint64_t gRoundingStartValue;
37 extern bool gPrintOptions;
38 
39 extern int test_read_image(cl_context context, cl_command_queue queue,
40                            cl_kernel kernel, image_descriptor *imageInfo,
41                            image_sampler_data *imageSampler,
42                            bool useFloatCoords, ExplicitType outputType,
43                            MTdata d);
44 
45 extern bool get_image_dimensions(image_descriptor *imageInfo, size_t &width,
46                                  size_t &height, size_t &depth);
47 
48 template <class T>
determine_validation_error_offset(void * imagePtr,image_descriptor * imageInfo,image_sampler_data * imageSampler,T * resultPtr,T * expected,float error,float x,float y,float z,float xAddressOffset,float yAddressOffset,float zAddressOffset,size_t j,int & numTries,int & numClamped,bool printAsFloat,int lod)49 int determine_validation_error_offset(
50     void *imagePtr, image_descriptor *imageInfo,
51     image_sampler_data *imageSampler, T *resultPtr, T *expected, float error,
52     float x, float y, float z, float xAddressOffset, float yAddressOffset,
53     float zAddressOffset, size_t j, int &numTries, int &numClamped,
54     bool printAsFloat, int lod)
55 {
56     int actualX, actualY, actualZ;
57     int found = debug_find_pixel_in_image(imagePtr, imageInfo, resultPtr,
58                                           &actualX, &actualY, &actualZ, lod);
59     bool clampingErr = false, clamped = false, otherClampingBug = false;
60     int clampedX, clampedY, clampedZ;
61 
62     size_t imageWidth, imageHeight, imageDepth;
63     if (get_image_dimensions(imageInfo, imageWidth, imageHeight, imageDepth))
64     {
65         log_error("ERROR: invalid image dimensions");
66         return TEST_FAIL;
67     }
68 
69     clamped = get_integer_coords_offset(x, y, z, xAddressOffset, yAddressOffset,
70                                         zAddressOffset, imageWidth, imageHeight,
71                                         imageDepth, imageSampler, imageInfo,
72                                         clampedX, clampedY, clampedZ);
73 
74     if (found)
75     {
76         // Is it a clamping bug?
77         if (clamped && clampedX == actualX && clampedY == actualY
78             && clampedZ == actualZ)
79         {
80             if ((--numClamped) == 0)
81             {
82                 if (printAsFloat)
83                 {
84                     log_error("Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did "
85                               "not validate! Expected (%g,%g,%g,%g), got "
86                               "(%g,%g,%g,%g), error of %g\n",
87                               j, x, x, y, y, z, z, (float)expected[0],
88                               (float)expected[1], (float)expected[2],
89                               (float)expected[3], (float)resultPtr[0],
90                               (float)resultPtr[1], (float)resultPtr[2],
91                               (float)resultPtr[3], error);
92                 }
93                 else
94                 {
95                     log_error(
96                         "Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did not "
97                         "validate! Expected (%x,%x,%x,%x), got (%x,%x,%x,%x)\n",
98                         j, x, x, y, y, z, z, (int)expected[0], (int)expected[1],
99                         (int)expected[2], (int)expected[3], (int)resultPtr[0],
100                         (int)resultPtr[1], (int)resultPtr[2],
101                         (int)resultPtr[3]);
102                 }
103                 log_error("ERROR: TEST FAILED: Read is erroneously clamping "
104                           "coordinates!\n");
105                 return -1;
106             }
107             clampingErr = true;
108             otherClampingBug = true;
109         }
110     }
111     if (clamped && !otherClampingBug)
112     {
113         // If we are in clamp-to-edge mode and we're getting zeroes, it's
114         // possible we're getting border erroneously
115         if (resultPtr[0] == 0 && resultPtr[1] == 0 && resultPtr[2] == 0
116             && resultPtr[3] == 0)
117         {
118             if ((--numClamped) == 0)
119             {
120                 if (printAsFloat)
121                 {
122                     log_error("Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did "
123                               "not validate! Expected (%g,%g,%g,%g), got "
124                               "(%g,%g,%g,%g), error of %g\n",
125                               j, x, x, y, y, z, z, (float)expected[0],
126                               (float)expected[1], (float)expected[2],
127                               (float)expected[3], (float)resultPtr[0],
128                               (float)resultPtr[1], (float)resultPtr[2],
129                               (float)resultPtr[3], error);
130                 }
131                 else
132                 {
133                     log_error(
134                         "Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did not "
135                         "validate! Expected (%x,%x,%x,%x), got (%x,%x,%x,%x)\n",
136                         j, x, x, y, y, z, z, (int)expected[0], (int)expected[1],
137                         (int)expected[2], (int)expected[3], (int)resultPtr[0],
138                         (int)resultPtr[1], (int)resultPtr[2],
139                         (int)resultPtr[3]);
140                 }
141                 log_error("ERROR: TEST FAILED: Clamping is erroneously "
142                           "returning border color!\n");
143                 return -1;
144             }
145             clampingErr = true;
146         }
147     }
148     if (!clampingErr)
149     {
150         if (printAsFloat)
151         {
152             log_error("Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did not "
153                       "validate!\n\tExpected (%g,%g,%g,%g),\n\t     got "
154                       "(%g,%g,%g,%g), error of %g\n",
155                       j, x, x, y, y, z, z, (float)expected[0],
156                       (float)expected[1], (float)expected[2],
157                       (float)expected[3], (float)resultPtr[0],
158                       (float)resultPtr[1], (float)resultPtr[2],
159                       (float)resultPtr[3], error);
160         }
161         else
162         {
163             log_error("Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did not "
164                       "validate!\n\tExpected (%x,%x,%x,%x),\n\t     got "
165                       "(%x,%x,%x,%x)\n",
166                       j, x, x, y, y, z, z, (int)expected[0], (int)expected[1],
167                       (int)expected[2], (int)expected[3], (int)resultPtr[0],
168                       (int)resultPtr[1], (int)resultPtr[2], (int)resultPtr[3]);
169         }
170         log_error(
171             "Integer coords resolve to %d,%d,%d   with img size %d,%d,%d\n",
172             clampedX, clampedY, clampedZ, (int)imageWidth, (int)imageHeight,
173             (int)imageDepth);
174 
175         if (printAsFloat && gExtraValidateInfo)
176         {
177             log_error("\nNearby values:\n");
178             for (int zOff = -1; zOff <= 1; zOff++)
179             {
180                 for (int yOff = -1; yOff <= 1; yOff++)
181                 {
182                     float top[4], real[4], bot[4];
183                     read_image_pixel_float(imagePtr, imageInfo, clampedX - 1,
184                                            clampedY + yOff, clampedZ + zOff,
185                                            top);
186                     read_image_pixel_float(imagePtr, imageInfo, clampedX,
187                                            clampedY + yOff, clampedZ + zOff,
188                                            real);
189                     read_image_pixel_float(imagePtr, imageInfo, clampedX + 1,
190                                            clampedY + yOff, clampedZ + zOff,
191                                            bot);
192                     log_error("\t(%g,%g,%g,%g)", top[0], top[1], top[2],
193                               top[3]);
194                     log_error(" (%g,%g,%g,%g)", real[0], real[1], real[2],
195                               real[3]);
196                     log_error(" (%g,%g,%g,%g)\n", bot[0], bot[1], bot[2],
197                               bot[3]);
198                 }
199             }
200         }
201         if (imageSampler->filter_mode != CL_FILTER_LINEAR)
202         {
203             if (found)
204                 log_error("\tValue really found in image at %d,%d,%d (%s)\n",
205                           actualX, actualY, actualZ,
206                           (found > 1) ? "NOT unique!!" : "unique");
207             else
208                 log_error("\tValue not actually found in image\n");
209         }
210         log_error("\n");
211 
212         numClamped = -1; // We force the clamped counter to never work
213         if ((--numTries) == 0) return -1;
214     }
215     return 0;
216 }
217 
218 
219 extern int filter_rounding_errors(int forceCorrectlyRoundedWrites,
220                                   image_descriptor *imageInfo, float *errors);
221 extern void filter_undefined_bits(image_descriptor *imageInfo, char *resultPtr);
222