1 /**************************************************************************
3 * Copyright 2009-2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
33 #include "util/u_format.h"
34 #include "util/u_format_tests.h"
35 #include "util/u_format_s3tc.h"
39 compare_float(float x
, float y
)
46 if (error
> FLT_EPSILON
) {
55 print_packed(const struct util_format_description
*format_desc
,
57 const uint8_t *packed
,
64 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
) {
65 printf("%s%02x", sep
, packed
[i
]);
73 print_unpacked_doubl(const struct util_format_description
*format_desc
,
75 const double unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
82 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
83 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
84 printf("%s{%f, %f, %f, %f}", sep
, unpacked
[i
][j
][0], unpacked
[i
][j
][1], unpacked
[i
][j
][2], unpacked
[i
][j
][3]);
94 print_unpacked_float(const struct util_format_description
*format_desc
,
96 const float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
100 const char *sep
= "";
102 printf("%s", prefix
);
103 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
104 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
105 printf("%s{%f, %f, %f, %f}", sep
, unpacked
[i
][j
][0], unpacked
[i
][j
][1], unpacked
[i
][j
][2], unpacked
[i
][j
][3]);
110 printf("%s", suffix
);
115 print_unpacked_8unorm(const struct util_format_description
*format_desc
,
117 const uint8_t unpacked
[][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
121 const char *sep
= "";
123 printf("%s", prefix
);
124 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
125 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
126 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]);
130 printf("%s", suffix
);
135 test_format_fetch_float(const struct util_format_description
*format_desc
,
136 const struct util_format_test_case
*test
)
138 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
143 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
144 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
145 format_desc
->fetch_float(unpacked
[i
][j
], test
->packed
, j
, i
);
146 for (k
= 0; k
< 4; ++k
) {
147 if (!compare_float(test
->unpacked
[i
][j
][k
], unpacked
[i
][j
][k
])) {
155 print_unpacked_float(format_desc
, "FAILED: ", unpacked
, " obtained\n");
156 print_unpacked_doubl(format_desc
, " ", test
->unpacked
, " expected\n");
164 test_format_unpack_float(const struct util_format_description
*format_desc
,
165 const struct util_format_test_case
*test
)
167 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
171 format_desc
->unpack_float(&unpacked
[0][0][0], sizeof unpacked
[0], test
->packed
, 0, format_desc
->block
.width
, format_desc
->block
.height
);
174 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
175 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
176 for (k
= 0; k
< 4; ++k
) {
177 if (!compare_float(test
->unpacked
[i
][j
][k
], unpacked
[i
][j
][k
])) {
185 print_unpacked_float(format_desc
, "FAILED: ", unpacked
, " obtained\n");
186 print_unpacked_doubl(format_desc
, " ", test
->unpacked
, " expected\n");
195 test_format_pack_float(const struct util_format_description
*format_desc
,
196 const struct util_format_test_case
*test
)
198 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
199 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
203 if (test
->format
== PIPE_FORMAT_DXT1_RGBA
) {
205 * Skip S3TC as packed representation is not canonical.
207 * TODO: Do a round trip conversion.
212 memset(packed
, 0, sizeof packed
);
213 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
214 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
215 for (k
= 0; k
< 4; ++k
) {
216 unpacked
[i
][j
][k
] = (float) test
->unpacked
[i
][j
][k
];
221 format_desc
->pack_float(packed
, 0, &unpacked
[0][0][0], sizeof unpacked
[0], format_desc
->block
.width
, format_desc
->block
.height
);
224 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
)
225 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
229 print_packed(format_desc
, "FAILED: ", packed
, " obtained\n");
230 print_packed(format_desc
, " ", test
->packed
, " expected\n");
238 convert_float_to_8unorm(uint8_t *dst
, const double *src
)
241 boolean accurate
= TRUE
;
243 for (i
= 0; i
< UTIL_FORMAT_MAX_UNPACKED_HEIGHT
*UTIL_FORMAT_MAX_UNPACKED_WIDTH
*4; ++i
) {
248 else if (src
[i
] > 1.0) {
253 dst
[i
] = src
[i
] * 255.0;
262 test_format_unpack_8unorm(const struct util_format_description
*format_desc
,
263 const struct util_format_test_case
*test
)
265 uint8_t unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
266 uint8_t expected
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
270 format_desc
->unpack_8unorm(&unpacked
[0][0][0], sizeof unpacked
[0], test
->packed
, 0, 1, 1);
272 convert_float_to_8unorm(&expected
[0][0][0], &test
->unpacked
[0][0][0]);
275 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
276 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
277 for (k
= 0; k
< 4; ++k
) {
278 if (expected
[i
][j
][k
] != unpacked
[i
][j
][k
]) {
286 print_unpacked_8unorm(format_desc
, "FAILED: ", unpacked
, " obtained\n");
287 print_unpacked_8unorm(format_desc
, " ", expected
, " expected\n");
295 test_format_pack_8unorm(const struct util_format_description
*format_desc
,
296 const struct util_format_test_case
*test
)
298 uint8_t unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
299 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
303 if (test
->format
== PIPE_FORMAT_DXT1_RGBA
) {
305 * Skip S3TC as packed representation is not canonical.
307 * TODO: Do a round trip conversion.
312 if (!convert_float_to_8unorm(&unpacked
[0][0][0], &test
->unpacked
[0][0][0])) {
314 * Skip test cases which cannot be represented by four unorm bytes.
319 memset(packed
, 0, sizeof packed
);
321 format_desc
->pack_8unorm(packed
, 0, &unpacked
[0][0][0], sizeof unpacked
[0], 1, 1);
324 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
)
325 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
329 print_packed(format_desc
, "FAILED: ", packed
, " obtained\n");
330 print_packed(format_desc
, " ", test
->packed
, " expected\n");
338 (*test_func_t
)(const struct util_format_description
*format_desc
,
339 const struct util_format_test_case
*test
);
343 test_one(test_func_t func
, const char *suffix
)
345 enum pipe_format last_format
= PIPE_FORMAT_NONE
;
349 for (i
= 0; i
< util_format_nr_test_cases
; ++i
) {
350 const struct util_format_test_case
*test
= &util_format_test_cases
[i
];
351 const struct util_format_description
*format_desc
;
354 format_desc
= util_format_description(test
->format
);
356 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
&&
357 !util_format_s3tc_enabled
) {
361 if (test
->format
!= last_format
) {
362 printf("%s util_format_%s_%s ...\n",
363 skip
? "Skipping" : "Testing", format_desc
->short_name
, suffix
);
364 last_format
= test
->format
;
368 if (!func(format_desc
, &util_format_test_cases
[i
])) {
383 if (!test_one(&test_format_fetch_float
, "fetch_float"))
386 if (!test_one(&test_format_pack_float
, "pack_float"))
389 if (!test_one(&test_format_unpack_float
, "unpack_float"))
392 if (!test_one(&test_format_pack_8unorm
, "pack_8unorm"))
395 if (!test_one(&test_format_unpack_8unorm
, "unpack_8unorm"))
402 int main(int argc
, char **argv
)
406 util_format_s3tc_init();
408 success
= test_all();
410 return success
? 0 : 1;