util: Allow to have block format test cases
[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 void
37 print_packed(const struct util_format_description *format_desc,
38 const char *prefix,
39 const uint8_t *packed,
40 const char *suffix)
41 {
42 unsigned i;
43 const char *sep = "";
44
45 printf("%s", prefix);
46 for (i = 0; i < format_desc->block.bits/8; ++i) {
47 printf("%s%02x", sep, packed[i]);
48 sep = " ";
49 }
50 printf("%s", suffix);
51 }
52
53
54 static void
55 print_unpacked_doubl(const struct util_format_description *format_desc,
56 const char *prefix,
57 const double unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
58 const char *suffix)
59 {
60 unsigned i, j;
61 const char *sep = "";
62
63 printf("%s", prefix);
64 for (i = 0; i < format_desc->block.height; ++i) {
65 for (j = 0; j < format_desc->block.width; ++j) {
66 printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
67 sep = ", ";
68 }
69 sep = ",\n";
70 }
71 printf("%s", suffix);
72 }
73
74
75 static void
76 print_unpacked_float(const struct util_format_description *format_desc,
77 const char *prefix,
78 const float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
79 const char *suffix)
80 {
81 unsigned i, j;
82 const char *sep = "";
83
84 printf("%s", prefix);
85 for (i = 0; i < format_desc->block.height; ++i) {
86 for (j = 0; j < format_desc->block.width; ++j) {
87 printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
88 sep = ", ";
89 }
90 sep = ",\n";
91 }
92 printf("%s", suffix);
93 }
94
95
96 static void
97 print_unpacked_8unorm(const struct util_format_description *format_desc,
98 const char *prefix,
99 const uint8_t unpacked[][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{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]);
109 sep = ", ";
110 }
111 }
112 printf("%s", suffix);
113 }
114
115
116 static boolean
117 test_format_fetch_float(const struct util_format_description *format_desc,
118 const struct util_format_test_case *test)
119 {
120 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
121 unsigned i, j, k;
122 boolean success;
123
124 success = TRUE;
125 for (i = 0; i < format_desc->block.height; ++i) {
126 for (j = 0; j < format_desc->block.width; ++j) {
127 format_desc->fetch_float(unpacked[i][j], test->packed, j, i);
128 for (k = 0; k < 4; ++k) {
129 if (test->unpacked[i][j][k] != unpacked[i][j][k]) {
130 success = FALSE;
131 }
132 }
133 }
134 }
135
136 if (!success) {
137 print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
138 print_unpacked_doubl(format_desc, " ", test->unpacked, " expected\n");
139 }
140
141 return success;
142 }
143
144
145 static boolean
146 test_format_unpack_float(const struct util_format_description *format_desc,
147 const struct util_format_test_case *test)
148 {
149 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
150 unsigned i, j, k;
151 boolean success;
152
153 format_desc->unpack_float(&unpacked[0][0][0], sizeof unpacked[0], test->packed, 0, format_desc->block.width, format_desc->block.height);
154
155 success = TRUE;
156 for (i = 0; i < format_desc->block.height; ++i) {
157 for (j = 0; j < format_desc->block.width; ++j) {
158 for (k = 0; k < 4; ++k) {
159 if (test->unpacked[i][j][k] != unpacked[i][j][k]) {
160 success = FALSE;
161 }
162 }
163 }
164 }
165
166 if (!success) {
167 print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
168 print_unpacked_doubl(format_desc, " ", test->unpacked, " expected\n");
169 }
170
171 return success;
172 }
173
174
175 static boolean
176
177 test_format_pack_float(const struct util_format_description *format_desc,
178 const struct util_format_test_case *test)
179 {
180 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
181 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
182 unsigned i, j, k;
183 boolean success;
184
185 memset(packed, 0, sizeof packed);
186 for (i = 0; i < format_desc->block.height; ++i) {
187 for (j = 0; j < format_desc->block.width; ++j) {
188 for (k = 0; k < 4; ++k) {
189 unpacked[i][j][k] = (float) test->unpacked[i][j][k];
190 }
191 }
192 }
193
194 format_desc->pack_float(packed, 0, &unpacked[0][0][0], sizeof unpacked[0], format_desc->block.width, format_desc->block.height);
195
196 success = TRUE;
197 for (i = 0; i < format_desc->block.bits/8; ++i)
198 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
199 success = FALSE;
200
201 if (!success) {
202 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
203 print_packed(format_desc, " ", test->packed, " expected\n");
204 }
205
206 return success;
207 }
208
209
210 static boolean
211 convert_float_to_8unorm(uint8_t *dst, const double *src)
212 {
213 unsigned i;
214 boolean accurate = TRUE;
215
216 for (i = 0; i < UTIL_FORMAT_MAX_UNPACKED_HEIGHT*UTIL_FORMAT_MAX_UNPACKED_WIDTH*4; ++i) {
217 if (src[i] < 0.0) {
218 accurate = FALSE;
219 dst[i] = 0;
220 }
221 else if (src[i] > 1.0) {
222 accurate = FALSE;
223 dst[i] = 255;
224 }
225 else {
226 dst[i] = src[i] * 255.0;
227 }
228 }
229
230 return accurate;
231 }
232
233
234 static boolean
235 test_format_unpack_8unorm(const struct util_format_description *format_desc,
236 const struct util_format_test_case *test)
237 {
238 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
239 uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
240 unsigned i, j, k;
241 boolean success;
242
243 format_desc->unpack_8unorm(&unpacked[0][0][0], 0, test->packed, 0, 1, 1);
244
245 convert_float_to_8unorm(&expected[0][0][0], &test->unpacked[0][0][0]);
246
247 success = TRUE;
248 for (i = 0; i < format_desc->block.height; ++i) {
249 for (j = 0; j < format_desc->block.width; ++j) {
250 for (k = 0; k < 4; ++k) {
251 if (expected[i][j][k] != unpacked[i][j][k]) {
252 success = FALSE;
253 }
254 }
255 }
256 }
257
258 if (!success) {
259 print_unpacked_8unorm(format_desc, "FAILED: ", unpacked, " obtained\n");
260 print_unpacked_8unorm(format_desc, " ", expected, " expected\n");
261 }
262
263 return success;
264 }
265
266
267 static boolean
268 test_format_pack_8unorm(const struct util_format_description *format_desc,
269 const struct util_format_test_case *test)
270 {
271 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
272 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
273 unsigned i;
274 boolean success;
275
276 if (!convert_float_to_8unorm(&unpacked[0][0][0], &test->unpacked[0][0][0])) {
277 /*
278 * Skip test cases which cannot be represented by four unorm bytes.
279 */
280 return TRUE;
281 }
282
283 memset(packed, 0, sizeof packed);
284
285 format_desc->pack_8unorm(packed, 0, &unpacked[0][0][0], sizeof unpacked[0], 1, 1);
286
287 success = TRUE;
288 for (i = 0; i < format_desc->block.bits/8; ++i)
289 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
290 success = FALSE;
291
292 if (!success) {
293 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
294 print_packed(format_desc, " ", test->packed, " expected\n");
295 }
296
297 return success;
298 }
299
300
301 typedef boolean
302 (*test_func_t)(const struct util_format_description *format_desc,
303 const struct util_format_test_case *test);
304
305
306 static boolean
307 test_one(test_func_t func, const char *suffix)
308 {
309 enum pipe_format last_format = PIPE_FORMAT_NONE;
310 unsigned i;
311 bool success = TRUE;
312
313 for (i = 0; i < util_format_nr_test_cases; ++i) {
314 const struct util_format_test_case *test = &util_format_test_cases[i];
315 const struct util_format_description *format_desc;
316 format_desc = util_format_description(test->format);
317
318 if (test->format != last_format) {
319 printf("Testing util_format_%s_%s ...\n", format_desc->short_name, suffix);
320 last_format = test->format;
321 }
322
323 if (!func(format_desc, &util_format_test_cases[i]))
324 success = FALSE;
325 }
326
327 return success;
328 }
329
330
331 static boolean
332 test_all(void)
333 {
334 bool success = TRUE;
335
336 if (!test_one(&test_format_fetch_float, "fetch_float"))
337 success = FALSE;
338
339 if (!test_one(&test_format_pack_float, "pack_float"))
340 success = FALSE;
341
342 if (!test_one(&test_format_unpack_float, "unpack_float"))
343 success = FALSE;
344
345 if (!test_one(&test_format_pack_8unorm, "pack_8unorm"))
346 success = FALSE;
347
348 if (!test_one(&test_format_unpack_8unorm, "unpack_8unorm"))
349 success = FALSE;
350
351 return success;
352 }
353
354
355 int main(int argc, char **argv)
356 {
357 boolean success;
358
359 success = test_all();
360
361 return success ? 0 : 1;
362 }