gallium/util: revert util_format_init addition
[mesa.git] / progs / gallium / unit / u_format_test.c
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <float.h>
32
33 #include "util/u_format.h"
34 #include "util/u_format_tests.h"
35
36
37 static boolean
38 compare_float(float x, float y)
39 {
40 float error = y - x;
41
42 if (error < 0.0f)
43 error = -error;
44
45 if (error > FLT_EPSILON) {
46 return FALSE;
47 }
48
49 return TRUE;
50 }
51
52
53 static void
54 print_packed(const struct util_format_description *format_desc,
55 const char *prefix,
56 const uint8_t *packed,
57 const char *suffix)
58 {
59 unsigned i;
60 const char *sep = "";
61
62 printf("%s", prefix);
63 for (i = 0; i < format_desc->block.bits/8; ++i) {
64 printf("%s%02x", sep, packed[i]);
65 sep = " ";
66 }
67 printf("%s", suffix);
68 }
69
70
71 static void
72 print_unpacked_doubl(const struct util_format_description *format_desc,
73 const char *prefix,
74 const double unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
75 const char *suffix)
76 {
77 unsigned i, j;
78 const char *sep = "";
79
80 printf("%s", prefix);
81 for (i = 0; i < format_desc->block.height; ++i) {
82 for (j = 0; j < format_desc->block.width; ++j) {
83 printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
84 sep = ", ";
85 }
86 sep = ",\n";
87 }
88 printf("%s", suffix);
89 }
90
91
92 static void
93 print_unpacked_float(const struct util_format_description *format_desc,
94 const char *prefix,
95 const float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
96 const char *suffix)
97 {
98 unsigned i, j;
99 const char *sep = "";
100
101 printf("%s", prefix);
102 for (i = 0; i < format_desc->block.height; ++i) {
103 for (j = 0; j < format_desc->block.width; ++j) {
104 printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
105 sep = ", ";
106 }
107 sep = ",\n";
108 }
109 printf("%s", suffix);
110 }
111
112
113 static void
114 print_unpacked_8unorm(const struct util_format_description *format_desc,
115 const char *prefix,
116 const uint8_t unpacked[][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
117 const char *suffix)
118 {
119 unsigned i, j;
120 const char *sep = "";
121
122 printf("%s", prefix);
123 for (i = 0; i < format_desc->block.height; ++i) {
124 for (j = 0; j < format_desc->block.width; ++j) {
125 printf("%s{0x%02x, 0x%02x, 0x%02x, 0x%02x}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
126 sep = ", ";
127 }
128 }
129 printf("%s", suffix);
130 }
131
132
133 static boolean
134 test_format_fetch_float(const struct util_format_description *format_desc,
135 const struct util_format_test_case *test)
136 {
137 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
138 unsigned i, j, k;
139 boolean success;
140
141 success = TRUE;
142 for (i = 0; i < format_desc->block.height; ++i) {
143 for (j = 0; j < format_desc->block.width; ++j) {
144 format_desc->fetch_float(unpacked[i][j], test->packed, j, i);
145 for (k = 0; k < 4; ++k) {
146 if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) {
147 success = FALSE;
148 }
149 }
150 }
151 }
152
153 if (!success) {
154 print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
155 print_unpacked_doubl(format_desc, " ", test->unpacked, " expected\n");
156 }
157
158 return success;
159 }
160
161
162 static boolean
163 test_format_unpack_float(const struct util_format_description *format_desc,
164 const struct util_format_test_case *test)
165 {
166 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
167 unsigned i, j, k;
168 boolean success;
169
170 format_desc->unpack_float(&unpacked[0][0][0], sizeof unpacked[0], test->packed, 0, format_desc->block.width, format_desc->block.height);
171
172 success = TRUE;
173 for (i = 0; i < format_desc->block.height; ++i) {
174 for (j = 0; j < format_desc->block.width; ++j) {
175 for (k = 0; k < 4; ++k) {
176 if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) {
177 success = FALSE;
178 }
179 }
180 }
181 }
182
183 if (!success) {
184 print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
185 print_unpacked_doubl(format_desc, " ", test->unpacked, " expected\n");
186 }
187
188 return success;
189 }
190
191
192 static boolean
193
194 test_format_pack_float(const struct util_format_description *format_desc,
195 const struct util_format_test_case *test)
196 {
197 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
198 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
199 unsigned i, j, k;
200 boolean success;
201
202 if (test->format == PIPE_FORMAT_DXT1_RGBA) {
203 /*
204 * Skip S3TC as packed representation is not canonical.
205 *
206 * TODO: Do a round trip conversion.
207 */
208 return TRUE;
209 }
210
211 memset(packed, 0, sizeof packed);
212 for (i = 0; i < format_desc->block.height; ++i) {
213 for (j = 0; j < format_desc->block.width; ++j) {
214 for (k = 0; k < 4; ++k) {
215 unpacked[i][j][k] = (float) test->unpacked[i][j][k];
216 }
217 }
218 }
219
220 format_desc->pack_float(packed, 0, &unpacked[0][0][0], sizeof unpacked[0], format_desc->block.width, format_desc->block.height);
221
222 success = TRUE;
223 for (i = 0; i < format_desc->block.bits/8; ++i)
224 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
225 success = FALSE;
226
227 if (!success) {
228 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
229 print_packed(format_desc, " ", test->packed, " expected\n");
230 }
231
232 return success;
233 }
234
235
236 static boolean
237 convert_float_to_8unorm(uint8_t *dst, const double *src)
238 {
239 unsigned i;
240 boolean accurate = TRUE;
241
242 for (i = 0; i < UTIL_FORMAT_MAX_UNPACKED_HEIGHT*UTIL_FORMAT_MAX_UNPACKED_WIDTH*4; ++i) {
243 if (src[i] < 0.0) {
244 accurate = FALSE;
245 dst[i] = 0;
246 }
247 else if (src[i] > 1.0) {
248 accurate = FALSE;
249 dst[i] = 255;
250 }
251 else {
252 dst[i] = src[i] * 255.0;
253 }
254 }
255
256 return accurate;
257 }
258
259
260 static boolean
261 test_format_unpack_8unorm(const struct util_format_description *format_desc,
262 const struct util_format_test_case *test)
263 {
264 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
265 uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
266 unsigned i, j, k;
267 boolean success;
268
269 format_desc->unpack_8unorm(&unpacked[0][0][0], sizeof unpacked[0], test->packed, 0, 1, 1);
270
271 convert_float_to_8unorm(&expected[0][0][0], &test->unpacked[0][0][0]);
272
273 success = TRUE;
274 for (i = 0; i < format_desc->block.height; ++i) {
275 for (j = 0; j < format_desc->block.width; ++j) {
276 for (k = 0; k < 4; ++k) {
277 if (expected[i][j][k] != unpacked[i][j][k]) {
278 success = FALSE;
279 }
280 }
281 }
282 }
283
284 if (!success) {
285 print_unpacked_8unorm(format_desc, "FAILED: ", unpacked, " obtained\n");
286 print_unpacked_8unorm(format_desc, " ", expected, " expected\n");
287 }
288
289 return success;
290 }
291
292
293 static boolean
294 test_format_pack_8unorm(const struct util_format_description *format_desc,
295 const struct util_format_test_case *test)
296 {
297 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
298 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
299 unsigned i;
300 boolean success;
301
302 if (test->format == PIPE_FORMAT_DXT1_RGBA) {
303 /*
304 * Skip S3TC as packed representation is not canonical.
305 *
306 * TODO: Do a round trip conversion.
307 */
308 return TRUE;
309 }
310
311 if (!convert_float_to_8unorm(&unpacked[0][0][0], &test->unpacked[0][0][0])) {
312 /*
313 * Skip test cases which cannot be represented by four unorm bytes.
314 */
315 return TRUE;
316 }
317
318 memset(packed, 0, sizeof packed);
319
320 format_desc->pack_8unorm(packed, 0, &unpacked[0][0][0], sizeof unpacked[0], 1, 1);
321
322 success = TRUE;
323 for (i = 0; i < format_desc->block.bits/8; ++i)
324 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
325 success = FALSE;
326
327 if (!success) {
328 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
329 print_packed(format_desc, " ", test->packed, " expected\n");
330 }
331
332 return success;
333 }
334
335
336 typedef boolean
337 (*test_func_t)(const struct util_format_description *format_desc,
338 const struct util_format_test_case *test);
339
340
341 static boolean
342 test_one(test_func_t func, const char *suffix)
343 {
344 enum pipe_format last_format = PIPE_FORMAT_NONE;
345 unsigned i;
346 bool success = TRUE;
347
348 for (i = 0; i < util_format_nr_test_cases; ++i) {
349 const struct util_format_test_case *test = &util_format_test_cases[i];
350 const struct util_format_description *format_desc;
351 bool skip = FALSE;
352
353 format_desc = util_format_description(test->format);
354
355 if (!util_format_is_supported(test->format))
356 skip = TRUE;
357
358 if (test->format != last_format) {
359 printf("%s util_format_%s_%s ...\n",
360 skip ? "Skipping" : "Testing", format_desc->short_name, suffix);
361 last_format = test->format;
362 }
363
364 if (!skip) {
365 if (!func(format_desc, &util_format_test_cases[i])) {
366 success = FALSE;
367 }
368 }
369 }
370
371 return success;
372 }
373
374
375 static boolean
376 test_all(void)
377 {
378 bool success = TRUE;
379
380 if (!test_one(&test_format_fetch_float, "fetch_float"))
381 success = FALSE;
382
383 if (!test_one(&test_format_pack_float, "pack_float"))
384 success = FALSE;
385
386 if (!test_one(&test_format_unpack_float, "unpack_float"))
387 success = FALSE;
388
389 if (!test_one(&test_format_pack_8unorm, "pack_8unorm"))
390 success = FALSE;
391
392 if (!test_one(&test_format_unpack_8unorm, "unpack_8unorm"))
393 success = FALSE;
394
395 return success;
396 }
397
398
399 int main(int argc, char **argv)
400 {
401 boolean success;
402
403 success = test_all();
404
405 return success ? 0 : 1;
406 }