Merge branch 'gallium-new-formats'
[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
32 #include "util/u_format.h"
33 #include "util/u_format_tests.h"
34
35
36 static boolean
37 test_format_unpack_float(const struct util_format_description *format_desc,
38 const struct util_format_test_case *test)
39 {
40 float unpacked[4];
41 unsigned i;
42 boolean success;
43
44 format_desc->unpack_float(unpacked, test->packed, 1);
45
46 success = TRUE;
47 for (i = 0; i < 4; ++i)
48 if (test->unpacked[i] != unpacked[i])
49 success = FALSE;
50
51 if (!success) {
52 printf("FAILED: (%f %f %f %f) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
53 printf(" (%f %f %f %f) expected\n", test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
54 }
55
56 return success;
57 }
58
59
60 static boolean
61 test_format_pack_float(const struct util_format_description *format_desc,
62 const struct util_format_test_case *test)
63 {
64 float unpacked[4];
65 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
66 unsigned i;
67 boolean success;
68
69 memset(packed, 0, sizeof packed);
70
71 for (i = 0; i < 4; ++i)
72 unpacked[i] = (float) test->unpacked[i];
73
74 format_desc->pack_float(packed, unpacked, 1);
75
76 success = TRUE;
77 for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
78 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
79 success = FALSE;
80
81 if (!success) {
82 /* TODO: print more than 4 bytes */
83 printf("FAILED: (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) obtained\n",
84 packed[0], packed[1], packed[2], packed[3],
85 packed[4], packed[5], packed[6], packed[7],
86 packed[8], packed[9], packed[10], packed[11],
87 packed[12], packed[13], packed[14], packed[15]);
88 printf(" (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) expected\n",
89 test->packed[0], test->packed[1], test->packed[2], test->packed[3],
90 test->packed[4], test->packed[5], test->packed[6], test->packed[7],
91 test->packed[8], test->packed[9], test->packed[10], test->packed[11],
92 test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
93 }
94
95 return success;
96 }
97
98
99 static boolean
100 convert_float_to_8unorm(uint8_t *dst, const double *src)
101 {
102 unsigned i;
103 boolean accurate = TRUE;
104
105 for (i = 0; i < 4; ++i) {
106 if (src[i] < 0.0) {
107 accurate = FALSE;
108 dst[i] = 0;
109 }
110 else if (src[i] > 1.0) {
111 accurate = FALSE;
112 dst[i] = 255;
113 }
114 else {
115 dst[i] = src[i] * 255.0;
116 }
117 }
118
119 return accurate;
120 }
121
122
123 static boolean
124 test_format_unpack_8unorm(const struct util_format_description *format_desc,
125 const struct util_format_test_case *test)
126 {
127 uint8_t unpacked[4];
128 uint8_t expected[4];
129 unsigned i;
130 boolean success;
131
132 format_desc->unpack_8unorm(unpacked, test->packed, 1);
133
134 convert_float_to_8unorm(expected, test->unpacked);
135
136 success = TRUE;
137 for (i = 0; i < 4; ++i)
138 if (expected[i] != unpacked[i])
139 success = FALSE;
140
141 if (!success) {
142 printf("FAILED: (0x%02x 0x%02x 0x%02x 0x%02x) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
143 printf(" (0x%02x 0x%02x 0x%02x 0x%02x) expected\n", expected[0], expected[1], expected[2], expected[3]);
144 }
145
146 return success;
147 }
148
149
150 static boolean
151 test_format_pack_8unorm(const struct util_format_description *format_desc,
152 const struct util_format_test_case *test)
153 {
154 uint8_t unpacked[4];
155 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
156 unsigned i;
157 boolean success;
158
159 if (!convert_float_to_8unorm(unpacked, test->unpacked)) {
160 /*
161 * Skip test cases which cannot be represented by four unorm bytes.
162 */
163 return TRUE;
164 }
165
166 memset(packed, 0, sizeof packed);
167
168 format_desc->pack_8unorm(packed, unpacked, 1);
169
170 success = TRUE;
171 for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
172 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
173 success = FALSE;
174
175 if (!success) {
176 /* TODO: print more than 4 bytes */
177 printf("FAILED: (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) obtained\n",
178 packed[0], packed[1], packed[2], packed[3],
179 packed[4], packed[5], packed[6], packed[7],
180 packed[8], packed[9], packed[10], packed[11],
181 packed[12], packed[13], packed[14], packed[15]);
182 printf(" (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) expected\n",
183 test->packed[0], test->packed[1], test->packed[2], test->packed[3],
184 test->packed[4], test->packed[5], test->packed[6], test->packed[7],
185 test->packed[8], test->packed[9], test->packed[10], test->packed[11],
186 test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
187 }
188
189 return success;
190 }
191
192
193 typedef boolean
194 (*test_func_t)(const struct util_format_description *format_desc,
195 const struct util_format_test_case *test);
196
197
198 static boolean
199 test_one(test_func_t func, const char *suffix)
200 {
201 enum pipe_format last_format = PIPE_FORMAT_NONE;
202 unsigned i;
203 bool success = TRUE;
204
205 for (i = 0; i < util_format_nr_test_cases; ++i) {
206 const struct util_format_test_case *test = &util_format_test_cases[i];
207 const struct util_format_description *format_desc;
208 format_desc = util_format_description(test->format);
209
210 if (test->format != last_format) {
211 printf("Testing util_format_%s_%s ...\n", format_desc->short_name, suffix);
212 last_format = test->format;
213 }
214
215 if (!func(format_desc, &util_format_test_cases[i]))
216 success = FALSE;
217 }
218
219 return success;
220 }
221
222
223 static boolean
224 test_all(void)
225 {
226 bool success = TRUE;
227
228 if (!test_one(&test_format_pack_float, "pack_float"))
229 success = FALSE;
230
231 if (!test_one(&test_format_unpack_float, "unpack_float"))
232 success = FALSE;
233
234 if (!test_one(&test_format_pack_8unorm, "pack_8unorm"))
235 success = FALSE;
236
237 if (!test_one(&test_format_unpack_8unorm, "unpack_8unorm"))
238 success = FALSE;
239
240 return success;
241 }
242
243
244 int main(int argc, char **argv)
245 {
246 boolean success;
247
248 success = test_all();
249
250 return success ? 0 : 1;
251 }