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
) {
47 printf("error = %g\n", error
);
56 print_packed(const struct util_format_description
*format_desc
,
58 const uint8_t *packed
,
65 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
) {
66 printf("%s%02x", sep
, packed
[i
]);
74 print_unpacked_doubl(const struct util_format_description
*format_desc
,
76 const double unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
83 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
84 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
85 printf("%s{%f, %f, %f, %f}", sep
, unpacked
[i
][j
][0], unpacked
[i
][j
][1], unpacked
[i
][j
][2], unpacked
[i
][j
][3]);
95 print_unpacked_float(const struct util_format_description
*format_desc
,
97 const float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
101 const char *sep
= "";
103 printf("%s", prefix
);
104 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
105 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
106 printf("%s{%f, %f, %f, %f}", sep
, unpacked
[i
][j
][0], unpacked
[i
][j
][1], unpacked
[i
][j
][2], unpacked
[i
][j
][3]);
111 printf("%s", suffix
);
116 print_unpacked_8unorm(const struct util_format_description
*format_desc
,
118 const uint8_t unpacked
[][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4],
122 const char *sep
= "";
124 printf("%s", prefix
);
125 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
126 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
127 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 printf("%s", suffix
);
136 test_format_fetch_float(const struct util_format_description
*format_desc
,
137 const struct util_format_test_case
*test
)
139 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
144 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
145 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
146 format_desc
->fetch_float(unpacked
[i
][j
], test
->packed
, j
, i
);
147 for (k
= 0; k
< 4; ++k
) {
148 if (!compare_float(test
->unpacked
[i
][j
][k
], unpacked
[i
][j
][k
])) {
156 print_unpacked_float(format_desc
, "FAILED: ", unpacked
, " obtained\n");
157 print_unpacked_doubl(format_desc
, " ", test
->unpacked
, " expected\n");
165 test_format_unpack_float(const struct util_format_description
*format_desc
,
166 const struct util_format_test_case
*test
)
168 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
172 format_desc
->unpack_float(&unpacked
[0][0][0], sizeof unpacked
[0], test
->packed
, 0, format_desc
->block
.width
, format_desc
->block
.height
);
175 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
176 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
177 for (k
= 0; k
< 4; ++k
) {
178 if (!compare_float(test
->unpacked
[i
][j
][k
], unpacked
[i
][j
][k
])) {
186 print_unpacked_float(format_desc
, "FAILED: ", unpacked
, " obtained\n");
187 print_unpacked_doubl(format_desc
, " ", test
->unpacked
, " expected\n");
196 test_format_pack_float(const struct util_format_description
*format_desc
,
197 const struct util_format_test_case
*test
)
199 float unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
200 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
204 memset(packed
, 0, sizeof packed
);
205 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
206 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
207 for (k
= 0; k
< 4; ++k
) {
208 unpacked
[i
][j
][k
] = (float) test
->unpacked
[i
][j
][k
];
213 format_desc
->pack_float(packed
, 0, &unpacked
[0][0][0], sizeof unpacked
[0], format_desc
->block
.width
, format_desc
->block
.height
);
216 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
)
217 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
221 print_packed(format_desc
, "FAILED: ", packed
, " obtained\n");
222 print_packed(format_desc
, " ", test
->packed
, " expected\n");
230 convert_float_to_8unorm(uint8_t *dst
, const double *src
)
233 boolean accurate
= TRUE
;
235 for (i
= 0; i
< UTIL_FORMAT_MAX_UNPACKED_HEIGHT
*UTIL_FORMAT_MAX_UNPACKED_WIDTH
*4; ++i
) {
240 else if (src
[i
] > 1.0) {
245 dst
[i
] = src
[i
] * 255.0;
254 test_format_unpack_8unorm(const struct util_format_description
*format_desc
,
255 const struct util_format_test_case
*test
)
257 uint8_t unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
258 uint8_t expected
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
262 format_desc
->unpack_8unorm(&unpacked
[0][0][0], 0, test
->packed
, 0, 1, 1);
264 convert_float_to_8unorm(&expected
[0][0][0], &test
->unpacked
[0][0][0]);
267 for (i
= 0; i
< format_desc
->block
.height
; ++i
) {
268 for (j
= 0; j
< format_desc
->block
.width
; ++j
) {
269 for (k
= 0; k
< 4; ++k
) {
270 if (expected
[i
][j
][k
] != unpacked
[i
][j
][k
]) {
278 print_unpacked_8unorm(format_desc
, "FAILED: ", unpacked
, " obtained\n");
279 print_unpacked_8unorm(format_desc
, " ", expected
, " expected\n");
287 test_format_pack_8unorm(const struct util_format_description
*format_desc
,
288 const struct util_format_test_case
*test
)
290 uint8_t unpacked
[UTIL_FORMAT_MAX_UNPACKED_HEIGHT
][UTIL_FORMAT_MAX_UNPACKED_WIDTH
][4];
291 uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
295 if (!convert_float_to_8unorm(&unpacked
[0][0][0], &test
->unpacked
[0][0][0])) {
297 * Skip test cases which cannot be represented by four unorm bytes.
302 memset(packed
, 0, sizeof packed
);
304 format_desc
->pack_8unorm(packed
, 0, &unpacked
[0][0][0], sizeof unpacked
[0], 1, 1);
307 for (i
= 0; i
< format_desc
->block
.bits
/8; ++i
)
308 if ((test
->packed
[i
] & test
->mask
[i
]) != (packed
[i
] & test
->mask
[i
]))
312 print_packed(format_desc
, "FAILED: ", packed
, " obtained\n");
313 print_packed(format_desc
, " ", test
->packed
, " expected\n");
321 (*test_func_t
)(const struct util_format_description
*format_desc
,
322 const struct util_format_test_case
*test
);
326 test_one(test_func_t func
, const char *suffix
)
328 enum pipe_format last_format
= PIPE_FORMAT_NONE
;
332 for (i
= 0; i
< util_format_nr_test_cases
; ++i
) {
333 const struct util_format_test_case
*test
= &util_format_test_cases
[i
];
334 const struct util_format_description
*format_desc
;
337 format_desc
= util_format_description(test
->format
);
339 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
&&
340 !util_format_s3tc_enabled
) {
344 if (test
->format
!= last_format
) {
345 printf("%s util_format_%s_%s ...\n",
346 skip
? "Skipping" : "Testing", format_desc
->short_name
, suffix
);
347 last_format
= test
->format
;
351 if (!func(format_desc
, &util_format_test_cases
[i
])) {
366 if (!test_one(&test_format_fetch_float
, "fetch_float"))
369 if (!test_one(&test_format_pack_float
, "pack_float"))
372 if (!test_one(&test_format_unpack_float
, "unpack_float"))
375 if (!test_one(&test_format_pack_8unorm
, "pack_8unorm"))
378 if (!test_one(&test_format_unpack_8unorm
, "unpack_8unorm"))
385 int main(int argc
, char **argv
)
389 util_format_s3tc_init();
391 success
= test_all();
393 return success
? 0 : 1;