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 void InitFloatCoordsCommon(image_descriptor *imageInfo,
46 image_sampler_data *imageSampler,
47 float *xOffsets, float *yOffsets,
48 float *zOffsets, float xfract, float yfract,
49 float zfract, int normalized_coords, MTdata d,
50 int lod);
51
52 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)53 int determine_validation_error_offset(
54 void *imagePtr, image_descriptor *imageInfo,
55 image_sampler_data *imageSampler, T *resultPtr, T *expected, float error,
56 float x, float y, float z, float xAddressOffset, float yAddressOffset,
57 float zAddressOffset, size_t j, int &numTries, int &numClamped,
58 bool printAsFloat, int lod)
59 {
60 int actualX, actualY, actualZ;
61 int found = debug_find_pixel_in_image(imagePtr, imageInfo, resultPtr,
62 &actualX, &actualY, &actualZ, lod);
63 bool clampingErr = false, clamped = false, otherClampingBug = false;
64 int clampedX, clampedY, clampedZ;
65
66 size_t imageWidth = imageInfo->width, imageHeight = imageInfo->height,
67 imageDepth = imageInfo->depth;
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( clamped && ( (int)x + (int)xOffsetValues[ j ] < 0 ||
151 (int)y + (int)yOffsetValues[ j ] < 0 ) )
152 {
153 log_error( "NEGATIVE COORDINATE ERROR\n" );
154 return -1;
155 }
156 */
157 if (true) // gExtraValidateInfo )
158 {
159 if (printAsFloat)
160 {
161 log_error("Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did not "
162 "validate!\n\tExpected (%g,%g,%g,%g),\n\t got "
163 "(%g,%g,%g,%g), error of %g\n",
164 j, x, x, y, y, z, z, (float)expected[0],
165 (float)expected[1], (float)expected[2],
166 (float)expected[3], (float)resultPtr[0],
167 (float)resultPtr[1], (float)resultPtr[2],
168 (float)resultPtr[3], error);
169 }
170 else
171 {
172 log_error("Sample %ld: coord {%f(%a),%f(%a),%f(%a)} did not "
173 "validate!\n\tExpected (%x,%x,%x,%x),\n\t got "
174 "(%x,%x,%x,%x)\n",
175 j, x, x, y, y, z, z, (int)expected[0],
176 (int)expected[1], (int)expected[2], (int)expected[3],
177 (int)resultPtr[0], (int)resultPtr[1],
178 (int)resultPtr[2], (int)resultPtr[3]);
179 }
180 log_error(
181 "Integer coords resolve to %d,%d,%d with img size %d,%d,%d\n",
182 clampedX, clampedY, clampedZ, (int)imageWidth, (int)imageHeight,
183 (int)imageDepth);
184
185 if (printAsFloat && gExtraValidateInfo)
186 {
187 log_error("\nNearby values:\n");
188 for (int zOff = -1; zOff <= 1; zOff++)
189 {
190 for (int yOff = -1; yOff <= 1; yOff++)
191 {
192 float top[4], real[4], bot[4];
193 read_image_pixel_float(imagePtr, imageInfo,
194 clampedX - 1, clampedY + yOff,
195 clampedZ + zOff, top);
196 read_image_pixel_float(imagePtr, imageInfo, clampedX,
197 clampedY + yOff, clampedZ + zOff,
198 real);
199 read_image_pixel_float(imagePtr, imageInfo,
200 clampedX + 1, clampedY + yOff,
201 clampedZ + zOff, bot);
202 log_error("\t(%g,%g,%g,%g)", top[0], top[1], top[2],
203 top[3]);
204 log_error(" (%g,%g,%g,%g)", real[0], real[1], real[2],
205 real[3]);
206 log_error(" (%g,%g,%g,%g)\n", bot[0], bot[1], bot[2],
207 bot[3]);
208 }
209 }
210 }
211 // }
212 // else
213 // log_error( "\n" );
214 if (imageSampler->filter_mode != CL_FILTER_LINEAR)
215 {
216 if (found)
217 log_error(
218 "\tValue really found in image at %d,%d,%d (%s)\n",
219 actualX, actualY, actualZ,
220 (found > 1) ? "NOT unique!!" : "unique");
221 else
222 log_error("\tValue not actually found in image\n");
223 }
224 log_error("\n");
225 }
226
227 numClamped = -1; // We force the clamped counter to never work
228 if ((--numTries) == 0) return -1;
229 }
230 return 0;
231 }
232