a6f1d608c848a69331bb71c8fb5cd8cb0d851874
[mesa.git] / src / util / tests / format / 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_half.h"
34 #include "util/format/u_format.h"
35 #include "util/format/u_format_tests.h"
36 #include "util/format/u_format_s3tc.h"
37
38
39 static boolean
40 compare_float(float x, float y)
41 {
42 float error = y - x;
43
44 if (error < 0.0f)
45 error = -error;
46
47 if (error > FLT_EPSILON) {
48 return FALSE;
49 }
50
51 return TRUE;
52 }
53
54
55 static void
56 print_packed(const struct util_format_description *format_desc,
57 const char *prefix,
58 const uint8_t *packed,
59 const char *suffix)
60 {
61 unsigned i;
62 const char *sep = "";
63
64 printf("%s", prefix);
65 for (i = 0; i < format_desc->block.bits/8; ++i) {
66 printf("%s%02x", sep, packed[i]);
67 sep = " ";
68 }
69 printf("%s", suffix);
70 fflush(stdout);
71 }
72
73
74 static void
75 print_unpacked_rgba_doubl(const struct util_format_description *format_desc,
76 const char *prefix,
77 const double unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
78 const char *suffix)
79 {
80 unsigned i, j;
81 const char *sep = "";
82
83 printf("%s", prefix);
84 for (i = 0; i < format_desc->block.height; ++i) {
85 for (j = 0; j < format_desc->block.width; ++j) {
86 printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
87 sep = ", ";
88 }
89 sep = ",\n";
90 }
91 printf("%s", suffix);
92 fflush(stdout);
93 }
94
95
96 static void
97 print_unpacked_rgba_float(const struct util_format_description *format_desc,
98 const char *prefix,
99 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
100 const char *suffix)
101 {
102 unsigned i, j;
103 const char *sep = "";
104
105 printf("%s", prefix);
106 for (i = 0; i < format_desc->block.height; ++i) {
107 for (j = 0; j < format_desc->block.width; ++j) {
108 printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
109 sep = ", ";
110 }
111 sep = ",\n";
112 }
113 printf("%s", suffix);
114 fflush(stdout);
115 }
116
117
118 static void
119 print_unpacked_rgba_8unorm(const struct util_format_description *format_desc,
120 const char *prefix,
121 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
122 const char *suffix)
123 {
124 unsigned i, j;
125 const char *sep = "";
126
127 printf("%s", prefix);
128 for (i = 0; i < format_desc->block.height; ++i) {
129 for (j = 0; j < format_desc->block.width; ++j) {
130 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]);
131 sep = ", ";
132 }
133 }
134 printf("%s", suffix);
135 fflush(stdout);
136 }
137
138
139 static void
140 print_unpacked_z_float(const struct util_format_description *format_desc,
141 const char *prefix,
142 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH],
143 const char *suffix)
144 {
145 unsigned i, j;
146 const char *sep = "";
147
148 printf("%s", prefix);
149 for (i = 0; i < format_desc->block.height; ++i) {
150 for (j = 0; j < format_desc->block.width; ++j) {
151 printf("%s%f", sep, unpacked[i][j]);
152 sep = ", ";
153 }
154 sep = ",\n";
155 }
156 printf("%s", suffix);
157 fflush(stdout);
158 }
159
160
161 static void
162 print_unpacked_z_32unorm(const struct util_format_description *format_desc,
163 const char *prefix,
164 uint32_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH],
165 const char *suffix)
166 {
167 unsigned i, j;
168 const char *sep = "";
169
170 printf("%s", prefix);
171 for (i = 0; i < format_desc->block.height; ++i) {
172 for (j = 0; j < format_desc->block.width; ++j) {
173 printf("%s0x%08x", sep, unpacked[i][j]);
174 sep = ", ";
175 }
176 }
177 printf("%s", suffix);
178 fflush(stdout);
179 }
180
181
182 static void
183 print_unpacked_s_8uint(const struct util_format_description *format_desc,
184 const char *prefix,
185 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH],
186 const char *suffix)
187 {
188 unsigned i, j;
189 const char *sep = "";
190
191 printf("%s", prefix);
192 for (i = 0; i < format_desc->block.height; ++i) {
193 for (j = 0; j < format_desc->block.width; ++j) {
194 printf("%s0x%02x", sep, unpacked[i][j]);
195 sep = ", ";
196 }
197 }
198 printf("%s", suffix);
199 fflush(stdout);
200 }
201
202
203 static boolean
204 test_format_fetch_rgba_float(const struct util_format_description *format_desc,
205 const struct util_format_test_case *test)
206 {
207 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
208 unsigned i, j, k;
209 boolean success;
210
211 success = TRUE;
212 for (i = 0; i < format_desc->block.height; ++i) {
213 for (j = 0; j < format_desc->block.width; ++j) {
214 format_desc->fetch_rgba_float(unpacked[i][j], test->packed, j, i);
215 for (k = 0; k < 4; ++k) {
216 if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) {
217 success = FALSE;
218 }
219 }
220 }
221 }
222
223 /* Ignore S3TC errors */
224 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
225 success = TRUE;
226 }
227
228 if (!success) {
229 print_unpacked_rgba_float(format_desc, "FAILED: ", unpacked, " obtained\n");
230 print_unpacked_rgba_doubl(format_desc, " ", test->unpacked, " expected\n");
231 }
232
233 return success;
234 }
235
236
237 static boolean
238 test_format_unpack_rgba(const struct util_format_description *format_desc,
239 const struct util_format_test_case *test)
240 {
241 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
242 unsigned i, j, k;
243 boolean success;
244
245 format_desc->unpack_rgba(&unpacked[0][0][0], sizeof unpacked[0],
246 test->packed, 0,
247 format_desc->block.width, format_desc->block.height);
248
249 success = TRUE;
250 for (i = 0; i < format_desc->block.height; ++i) {
251 for (j = 0; j < format_desc->block.width; ++j) {
252 for (k = 0; k < 4; ++k) {
253 if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) {
254 success = FALSE;
255 }
256 }
257 }
258 }
259
260 /* Ignore S3TC errors */
261 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
262 success = TRUE;
263 }
264
265 if (!success) {
266 print_unpacked_rgba_float(format_desc, "FAILED: ", unpacked, " obtained\n");
267 print_unpacked_rgba_doubl(format_desc, " ", test->unpacked, " expected\n");
268 }
269
270 return success;
271 }
272
273
274 static boolean
275 test_format_pack_rgba_float(const struct util_format_description *format_desc,
276 const struct util_format_test_case *test)
277 {
278 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
279 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
280 unsigned i, j, k;
281 boolean success;
282
283 if (test->format == PIPE_FORMAT_DXT1_RGBA) {
284 /*
285 * Skip S3TC as packed representation is not canonical.
286 *
287 * TODO: Do a round trip conversion.
288 */
289 return TRUE;
290 }
291
292 memset(packed, 0, sizeof packed);
293 for (i = 0; i < format_desc->block.height; ++i) {
294 for (j = 0; j < format_desc->block.width; ++j) {
295 for (k = 0; k < 4; ++k) {
296 unpacked[i][j][k] = (float) test->unpacked[i][j][k];
297 }
298 }
299 }
300
301 format_desc->pack_rgba_float(packed, 0,
302 &unpacked[0][0][0], sizeof unpacked[0],
303 format_desc->block.width, format_desc->block.height);
304
305 success = TRUE;
306 for (i = 0; i < format_desc->block.bits/8; ++i) {
307 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
308 success = FALSE;
309 }
310
311 /* Ignore NaN */
312 if (util_is_double_nan(test->unpacked[0][0][0]))
313 success = TRUE;
314
315 /* Ignore S3TC errors */
316 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
317 success = TRUE;
318 }
319
320 if (!success) {
321 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
322 print_packed(format_desc, " ", test->packed, " expected\n");
323 }
324
325 return success;
326 }
327
328
329 static boolean
330 convert_float_to_8unorm(uint8_t *dst, const double *src)
331 {
332 unsigned i;
333 boolean accurate = TRUE;
334
335 for (i = 0; i < UTIL_FORMAT_MAX_UNPACKED_HEIGHT*UTIL_FORMAT_MAX_UNPACKED_WIDTH*4; ++i) {
336 if (src[i] < 0.0) {
337 accurate = FALSE;
338 dst[i] = 0;
339 }
340 else if (src[i] > 1.0) {
341 accurate = FALSE;
342 dst[i] = 255;
343 }
344 else {
345 dst[i] = src[i] * 255.0;
346 }
347 }
348
349 return accurate;
350 }
351
352
353 static boolean
354 test_format_unpack_rgba_8unorm(const struct util_format_description *format_desc,
355 const struct util_format_test_case *test)
356 {
357 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
358 uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
359 unsigned i, j, k;
360 boolean success;
361
362 if (util_format_is_pure_integer(format_desc->format))
363 return FALSE;
364
365 format_desc->unpack_rgba_8unorm(&unpacked[0][0][0], sizeof unpacked[0],
366 test->packed, 0,
367 format_desc->block.width, format_desc->block.height);
368
369 convert_float_to_8unorm(&expected[0][0][0], &test->unpacked[0][0][0]);
370
371 success = TRUE;
372 for (i = 0; i < format_desc->block.height; ++i) {
373 for (j = 0; j < format_desc->block.width; ++j) {
374 for (k = 0; k < 4; ++k) {
375 if (expected[i][j][k] != unpacked[i][j][k]) {
376 success = FALSE;
377 }
378 }
379 }
380 }
381
382 /* Ignore NaN */
383 if (util_is_double_nan(test->unpacked[0][0][0]))
384 success = TRUE;
385
386 if (!success) {
387 print_unpacked_rgba_8unorm(format_desc, "FAILED: ", unpacked, " obtained\n");
388 print_unpacked_rgba_8unorm(format_desc, " ", expected, " expected\n");
389 }
390
391 return success;
392 }
393
394
395 static boolean
396 test_format_pack_rgba_8unorm(const struct util_format_description *format_desc,
397 const struct util_format_test_case *test)
398 {
399 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
400 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
401 unsigned i;
402 boolean success;
403
404 if (test->format == PIPE_FORMAT_DXT1_RGBA) {
405 /*
406 * Skip S3TC as packed representation is not canonical.
407 *
408 * TODO: Do a round trip conversion.
409 */
410 return TRUE;
411 }
412
413 if (!convert_float_to_8unorm(&unpacked[0][0][0], &test->unpacked[0][0][0])) {
414 /*
415 * Skip test cases which cannot be represented by four unorm bytes.
416 */
417 return TRUE;
418 }
419
420 memset(packed, 0, sizeof packed);
421
422 format_desc->pack_rgba_8unorm(packed, 0,
423 &unpacked[0][0][0], sizeof unpacked[0],
424 format_desc->block.width, format_desc->block.height);
425
426 success = TRUE;
427 for (i = 0; i < format_desc->block.bits/8; ++i)
428 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
429 success = FALSE;
430
431 /* Ignore NaN */
432 if (util_is_double_nan(test->unpacked[0][0][0]))
433 success = TRUE;
434
435 /* Ignore failure cases due to unorm8 format */
436 if (test->unpacked[0][0][0] > 1.0f || test->unpacked[0][0][0] < 0.0f)
437 success = TRUE;
438
439 /* Multiple of 255 */
440 if ((test->unpacked[0][0][0] * 255.0) != (int)(test->unpacked[0][0][0] * 255.0))
441 success = TRUE;
442
443 /* Ignore S3TC errors */
444 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
445 success = TRUE;
446 }
447
448 if (!success) {
449 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
450 print_packed(format_desc, " ", test->packed, " expected\n");
451 }
452
453 return success;
454 }
455
456
457 static boolean
458 test_format_unpack_z_float(const struct util_format_description *format_desc,
459 const struct util_format_test_case *test)
460 {
461 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } };
462 unsigned i, j;
463 boolean success;
464
465 format_desc->unpack_z_float(&unpacked[0][0], sizeof unpacked[0],
466 test->packed, 0,
467 format_desc->block.width, format_desc->block.height);
468
469 success = TRUE;
470 for (i = 0; i < format_desc->block.height; ++i) {
471 for (j = 0; j < format_desc->block.width; ++j) {
472 if (!compare_float(test->unpacked[i][j][0], unpacked[i][j])) {
473 success = FALSE;
474 }
475 }
476 }
477
478 if (!success) {
479 print_unpacked_z_float(format_desc, "FAILED: ", unpacked, " obtained\n");
480 print_unpacked_rgba_doubl(format_desc, " ", test->unpacked, " expected\n");
481 }
482
483 return success;
484 }
485
486
487 static boolean
488 test_format_pack_z_float(const struct util_format_description *format_desc,
489 const struct util_format_test_case *test)
490 {
491 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH];
492 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
493 unsigned i, j;
494 boolean success;
495
496 memset(packed, 0, sizeof packed);
497 for (i = 0; i < format_desc->block.height; ++i) {
498 for (j = 0; j < format_desc->block.width; ++j) {
499 unpacked[i][j] = (float) test->unpacked[i][j][0];
500 if (test->unpacked[i][j][1]) {
501 return TRUE;
502 }
503 }
504 }
505
506 format_desc->pack_z_float(packed, 0,
507 &unpacked[0][0], sizeof unpacked[0],
508 format_desc->block.width, format_desc->block.height);
509
510 success = TRUE;
511 for (i = 0; i < format_desc->block.bits/8; ++i)
512 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
513 success = FALSE;
514
515 if (!success) {
516 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
517 print_packed(format_desc, " ", test->packed, " expected\n");
518 }
519
520 return success;
521 }
522
523
524 static boolean
525 test_format_unpack_z_32unorm(const struct util_format_description *format_desc,
526 const struct util_format_test_case *test)
527 {
528 uint32_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } };
529 uint32_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } };
530 unsigned i, j;
531 boolean success;
532
533 format_desc->unpack_z_32unorm(&unpacked[0][0], sizeof unpacked[0],
534 test->packed, 0,
535 format_desc->block.width, format_desc->block.height);
536
537 for (i = 0; i < format_desc->block.height; ++i) {
538 for (j = 0; j < format_desc->block.width; ++j) {
539 expected[i][j] = test->unpacked[i][j][0] * 0xffffffff;
540 }
541 }
542
543 success = TRUE;
544 for (i = 0; i < format_desc->block.height; ++i) {
545 for (j = 0; j < format_desc->block.width; ++j) {
546 if (expected[i][j] != unpacked[i][j]) {
547 success = FALSE;
548 }
549 }
550 }
551
552 if (!success) {
553 print_unpacked_z_32unorm(format_desc, "FAILED: ", unpacked, " obtained\n");
554 print_unpacked_z_32unorm(format_desc, " ", expected, " expected\n");
555 }
556
557 return success;
558 }
559
560
561 static boolean
562 test_format_pack_z_32unorm(const struct util_format_description *format_desc,
563 const struct util_format_test_case *test)
564 {
565 uint32_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH];
566 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
567 unsigned i, j;
568 boolean success;
569
570 for (i = 0; i < format_desc->block.height; ++i) {
571 for (j = 0; j < format_desc->block.width; ++j) {
572 unpacked[i][j] = test->unpacked[i][j][0] * 0xffffffff;
573 if (test->unpacked[i][j][1]) {
574 return TRUE;
575 }
576 }
577 }
578
579 memset(packed, 0, sizeof packed);
580
581 format_desc->pack_z_32unorm(packed, 0,
582 &unpacked[0][0], sizeof unpacked[0],
583 format_desc->block.width, format_desc->block.height);
584
585 success = TRUE;
586 for (i = 0; i < format_desc->block.bits/8; ++i)
587 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
588 success = FALSE;
589
590 if (!success) {
591 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
592 print_packed(format_desc, " ", test->packed, " expected\n");
593 }
594
595 return success;
596 }
597
598
599 static boolean
600 test_format_unpack_s_8uint(const struct util_format_description *format_desc,
601 const struct util_format_test_case *test)
602 {
603 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } };
604 uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } };
605 unsigned i, j;
606 boolean success;
607
608 format_desc->unpack_s_8uint(&unpacked[0][0], sizeof unpacked[0],
609 test->packed, 0,
610 format_desc->block.width, format_desc->block.height);
611
612 for (i = 0; i < format_desc->block.height; ++i) {
613 for (j = 0; j < format_desc->block.width; ++j) {
614 expected[i][j] = test->unpacked[i][j][1];
615 }
616 }
617
618 success = TRUE;
619 for (i = 0; i < format_desc->block.height; ++i) {
620 for (j = 0; j < format_desc->block.width; ++j) {
621 if (expected[i][j] != unpacked[i][j]) {
622 success = FALSE;
623 }
624 }
625 }
626
627 if (!success) {
628 print_unpacked_s_8uint(format_desc, "FAILED: ", unpacked, " obtained\n");
629 print_unpacked_s_8uint(format_desc, " ", expected, " expected\n");
630 }
631
632 return success;
633 }
634
635
636 static boolean
637 test_format_pack_s_8uint(const struct util_format_description *format_desc,
638 const struct util_format_test_case *test)
639 {
640 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH];
641 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
642 unsigned i, j;
643 boolean success;
644
645 for (i = 0; i < format_desc->block.height; ++i) {
646 for (j = 0; j < format_desc->block.width; ++j) {
647 unpacked[i][j] = test->unpacked[i][j][1];
648 if (test->unpacked[i][j][0]) {
649 return TRUE;
650 }
651 }
652 }
653
654 memset(packed, 0, sizeof packed);
655
656 format_desc->pack_s_8uint(packed, 0,
657 &unpacked[0][0], sizeof unpacked[0],
658 format_desc->block.width, format_desc->block.height);
659
660 success = TRUE;
661 for (i = 0; i < format_desc->block.bits/8; ++i)
662 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
663 success = FALSE;
664
665 if (!success) {
666 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
667 print_packed(format_desc, " ", test->packed, " expected\n");
668 }
669
670 return success;
671 }
672
673
674 /* Touch-test that the unorm/snorm flags are set up right by codegen. */
675 static boolean
676 test_format_norm_flags(const struct util_format_description *format_desc)
677 {
678 boolean success = TRUE;
679
680 #define FORMAT_CASE(format, unorm, snorm) \
681 case format: \
682 success = (format_desc->is_unorm == unorm && \
683 format_desc->is_snorm == snorm); \
684 break
685
686 switch (format_desc->format) {
687 FORMAT_CASE(PIPE_FORMAT_R8G8B8A8_UNORM, TRUE, FALSE);
688 FORMAT_CASE(PIPE_FORMAT_R8G8B8A8_SRGB, TRUE, FALSE);
689 FORMAT_CASE(PIPE_FORMAT_R8G8B8A8_SNORM, FALSE, TRUE);
690 FORMAT_CASE(PIPE_FORMAT_R32_FLOAT, FALSE, FALSE);
691 FORMAT_CASE(PIPE_FORMAT_X8Z24_UNORM, TRUE, FALSE);
692 FORMAT_CASE(PIPE_FORMAT_S8X24_UINT, FALSE, FALSE);
693 FORMAT_CASE(PIPE_FORMAT_DXT1_RGB, TRUE, FALSE);
694 FORMAT_CASE(PIPE_FORMAT_ETC2_RGB8, TRUE, FALSE);
695 FORMAT_CASE(PIPE_FORMAT_ETC2_R11_SNORM, FALSE, TRUE);
696 FORMAT_CASE(PIPE_FORMAT_ASTC_4x4, TRUE, FALSE);
697 FORMAT_CASE(PIPE_FORMAT_BPTC_RGBA_UNORM, TRUE, FALSE);
698 FORMAT_CASE(PIPE_FORMAT_BPTC_RGB_FLOAT, FALSE, FALSE);
699 default:
700 success = !(format_desc->is_unorm && format_desc->is_snorm);
701 break;
702 }
703 #undef FORMAT_CASE
704
705 if (!success) {
706 printf("FAILED: %s (unorm %s, snorm %s)\n",
707 format_desc->short_name,
708 format_desc->is_unorm ? "yes" : "no",
709 format_desc->is_snorm ? "yes" : "no");
710 }
711
712 return success;
713 }
714
715 typedef boolean
716 (*test_func_t)(const struct util_format_description *format_desc,
717 const struct util_format_test_case *test);
718
719
720 static boolean
721 test_one_func(const struct util_format_description *format_desc,
722 test_func_t func,
723 const char *suffix)
724 {
725 unsigned i;
726 boolean success = TRUE;
727
728 printf("Testing util_format_%s_%s ...\n",
729 format_desc->short_name, suffix);
730 fflush(stdout);
731
732 for (i = 0; i < util_format_nr_test_cases; ++i) {
733 const struct util_format_test_case *test = &util_format_test_cases[i];
734
735 if (test->format == format_desc->format) {
736 if (!func(format_desc, &util_format_test_cases[i])) {
737 success = FALSE;
738 }
739 }
740 }
741
742 return success;
743 }
744
745 static boolean
746 test_format_metadata(const struct util_format_description *format_desc,
747 boolean (*func)(const struct util_format_description *format_desc),
748 const char *suffix)
749 {
750 boolean success = TRUE;
751
752 printf("Testing util_format_%s_%s ...\n", format_desc->short_name, suffix);
753 fflush(stdout);
754
755 if (!func(format_desc)) {
756 success = FALSE;
757 }
758
759 return success;
760 }
761
762 static boolean
763 test_all(void)
764 {
765 enum pipe_format format;
766 boolean success = TRUE;
767
768 for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
769 const struct util_format_description *format_desc;
770
771 format_desc = util_format_description(format);
772 if (!format_desc) {
773 continue;
774 }
775
776 assert(format_desc->block.bits <= UTIL_FORMAT_MAX_PACKED_BYTES * 8);
777 assert(format_desc->block.height <= UTIL_FORMAT_MAX_UNPACKED_HEIGHT);
778 assert(format_desc->block.width <= UTIL_FORMAT_MAX_UNPACKED_WIDTH);
779
780 # define TEST_ONE_FUNC(name) \
781 if (format_desc->name) { \
782 if (!test_one_func(format_desc, &test_format_##name, #name)) { \
783 success = FALSE; \
784 } \
785 }
786
787 # define TEST_FORMAT_METADATA(name) \
788 if (!test_format_metadata(format_desc, &test_format_##name, #name)) { \
789 success = FALSE; \
790 } \
791
792 TEST_ONE_FUNC(fetch_rgba_float);
793 TEST_ONE_FUNC(pack_rgba_float);
794 TEST_ONE_FUNC(unpack_rgba);
795 TEST_ONE_FUNC(pack_rgba_8unorm);
796 TEST_ONE_FUNC(unpack_rgba_8unorm);
797
798 TEST_ONE_FUNC(unpack_z_32unorm);
799 TEST_ONE_FUNC(pack_z_32unorm);
800 TEST_ONE_FUNC(unpack_z_float);
801 TEST_ONE_FUNC(pack_z_float);
802 TEST_ONE_FUNC(unpack_s_8uint);
803 TEST_ONE_FUNC(pack_s_8uint);
804
805 TEST_FORMAT_METADATA(norm_flags);
806
807 # undef TEST_ONE_FUNC
808 # undef TEST_ONE_FORMAT
809 }
810
811 return success;
812 }
813
814
815 int main(int argc, char **argv)
816 {
817 boolean success;
818
819 success = test_all();
820
821 return success ? 0 : 1;
822 }