util: Add dedicated depth-stencil packing/unpacking functions.
[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 #include <float.h>
32
33 #include "util/u_half.h"
34 #include "util/u_format.h"
35 #include "util/u_format_tests.h"
36 #include "util/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 }
71
72
73 static void
74 print_unpacked_doubl(const struct util_format_description *format_desc,
75 const char *prefix,
76 const double unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
77 const char *suffix)
78 {
79 unsigned i, j;
80 const char *sep = "";
81
82 printf("%s", prefix);
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]);
86 sep = ", ";
87 }
88 sep = ",\n";
89 }
90 printf("%s", suffix);
91 }
92
93
94 static void
95 print_unpacked_float(const struct util_format_description *format_desc,
96 const char *prefix,
97 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
98 const char *suffix)
99 {
100 unsigned i, j;
101 const char *sep = "";
102
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]);
107 sep = ", ";
108 }
109 sep = ",\n";
110 }
111 printf("%s", suffix);
112 }
113
114
115 static void
116 print_unpacked_8unorm(const struct util_format_description *format_desc,
117 const char *prefix,
118 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
119 const char *suffix)
120 {
121 unsigned i, j;
122 const char *sep = "";
123
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]);
128 sep = ", ";
129 }
130 }
131 printf("%s", suffix);
132 }
133
134
135 static boolean
136 test_format_fetch_rgba_float(const struct util_format_description *format_desc,
137 const struct util_format_test_case *test)
138 {
139 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
140 unsigned i, j, k;
141 boolean success;
142
143 success = TRUE;
144 for (i = 0; i < format_desc->block.height; ++i) {
145 for (j = 0; j < format_desc->block.width; ++j) {
146 format_desc->fetch_rgba_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])) {
149 success = FALSE;
150 }
151 }
152 }
153 }
154
155 if (!success) {
156 print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
157 print_unpacked_doubl(format_desc, " ", test->unpacked, " expected\n");
158 }
159
160 return success;
161 }
162
163
164 static boolean
165 test_format_unpack_rgba_float(const struct util_format_description *format_desc,
166 const struct util_format_test_case *test)
167 {
168 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
169 unsigned i, j, k;
170 boolean success;
171
172 format_desc->unpack_rgba_float(&unpacked[0][0][0], sizeof unpacked[0],
173 test->packed, 0,
174 format_desc->block.width, format_desc->block.height);
175
176 success = TRUE;
177 for (i = 0; i < format_desc->block.height; ++i) {
178 for (j = 0; j < format_desc->block.width; ++j) {
179 for (k = 0; k < 4; ++k) {
180 if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) {
181 success = FALSE;
182 }
183 }
184 }
185 }
186
187 if (!success) {
188 print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
189 print_unpacked_doubl(format_desc, " ", test->unpacked, " expected\n");
190 }
191
192 return success;
193 }
194
195
196 static boolean
197
198 test_format_pack_rgba_float(const struct util_format_description *format_desc,
199 const struct util_format_test_case *test)
200 {
201 float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
202 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
203 unsigned i, j, k;
204 boolean success;
205
206 if (test->format == PIPE_FORMAT_DXT1_RGBA) {
207 /*
208 * Skip S3TC as packed representation is not canonical.
209 *
210 * TODO: Do a round trip conversion.
211 */
212 return TRUE;
213 }
214
215 memset(packed, 0, sizeof packed);
216 for (i = 0; i < format_desc->block.height; ++i) {
217 for (j = 0; j < format_desc->block.width; ++j) {
218 for (k = 0; k < 4; ++k) {
219 unpacked[i][j][k] = (float) test->unpacked[i][j][k];
220 }
221 }
222 }
223
224 format_desc->pack_rgba_float(packed, 0,
225 &unpacked[0][0][0], sizeof unpacked[0],
226 format_desc->block.width, format_desc->block.height);
227
228 success = TRUE;
229 for (i = 0; i < format_desc->block.bits/8; ++i)
230 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
231 success = FALSE;
232
233 if (!success) {
234 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
235 print_packed(format_desc, " ", test->packed, " expected\n");
236 }
237
238 return success;
239 }
240
241
242 static boolean
243 convert_float_to_8unorm(uint8_t *dst, const double *src)
244 {
245 unsigned i;
246 boolean accurate = TRUE;
247
248 for (i = 0; i < UTIL_FORMAT_MAX_UNPACKED_HEIGHT*UTIL_FORMAT_MAX_UNPACKED_WIDTH*4; ++i) {
249 if (src[i] < 0.0) {
250 accurate = FALSE;
251 dst[i] = 0;
252 }
253 else if (src[i] > 1.0) {
254 accurate = FALSE;
255 dst[i] = 255;
256 }
257 else {
258 dst[i] = src[i] * 255.0;
259 }
260 }
261
262 return accurate;
263 }
264
265
266 static boolean
267 test_format_unpack_rgba_8unorm(const struct util_format_description *format_desc,
268 const struct util_format_test_case *test)
269 {
270 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
271 uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } };
272 unsigned i, j, k;
273 boolean success;
274
275 format_desc->unpack_rgba_8unorm(&unpacked[0][0][0], sizeof unpacked[0],
276 test->packed, 0,
277 format_desc->block.width, format_desc->block.height);
278
279 convert_float_to_8unorm(&expected[0][0][0], &test->unpacked[0][0][0]);
280
281 success = TRUE;
282 for (i = 0; i < format_desc->block.height; ++i) {
283 for (j = 0; j < format_desc->block.width; ++j) {
284 for (k = 0; k < 4; ++k) {
285 if (expected[i][j][k] != unpacked[i][j][k]) {
286 success = FALSE;
287 }
288 }
289 }
290 }
291
292 if (!success) {
293 print_unpacked_8unorm(format_desc, "FAILED: ", unpacked, " obtained\n");
294 print_unpacked_8unorm(format_desc, " ", expected, " expected\n");
295 }
296
297 return success;
298 }
299
300
301 static boolean
302 test_format_pack_rgba_8unorm(const struct util_format_description *format_desc,
303 const struct util_format_test_case *test)
304 {
305 uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
306 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
307 unsigned i;
308 boolean success;
309
310 if (test->format == PIPE_FORMAT_DXT1_RGBA) {
311 /*
312 * Skip S3TC as packed representation is not canonical.
313 *
314 * TODO: Do a round trip conversion.
315 */
316 return TRUE;
317 }
318
319 if (!convert_float_to_8unorm(&unpacked[0][0][0], &test->unpacked[0][0][0])) {
320 /*
321 * Skip test cases which cannot be represented by four unorm bytes.
322 */
323 return TRUE;
324 }
325
326 memset(packed, 0, sizeof packed);
327
328 format_desc->pack_rgba_8unorm(packed, 0,
329 &unpacked[0][0][0], sizeof unpacked[0],
330 format_desc->block.width, format_desc->block.height);
331
332 success = TRUE;
333 for (i = 0; i < format_desc->block.bits/8; ++i)
334 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
335 success = FALSE;
336
337 if (!success) {
338 print_packed(format_desc, "FAILED: ", packed, " obtained\n");
339 print_packed(format_desc, " ", test->packed, " expected\n");
340 }
341
342 return success;
343 }
344
345
346 typedef boolean
347 (*test_func_t)(const struct util_format_description *format_desc,
348 const struct util_format_test_case *test);
349
350
351 static boolean
352 test_one(test_func_t func, const char *suffix)
353 {
354 enum pipe_format last_format = PIPE_FORMAT_NONE;
355 unsigned i;
356 bool success = TRUE;
357
358 for (i = 0; i < util_format_nr_test_cases; ++i) {
359 const struct util_format_test_case *test = &util_format_test_cases[i];
360 const struct util_format_description *format_desc;
361 bool skip = FALSE;
362
363 format_desc = util_format_description(test->format);
364
365 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC &&
366 !util_format_s3tc_enabled) {
367 skip = TRUE;
368 }
369
370 if (test->format != last_format) {
371 printf("%s util_format_%s_%s ...\n",
372 skip ? "Skipping" : "Testing", format_desc->short_name, suffix);
373 last_format = test->format;
374 }
375
376 if (!skip) {
377 if (!func(format_desc, &util_format_test_cases[i])) {
378 success = FALSE;
379 }
380 }
381 }
382
383 return success;
384 }
385
386
387 static boolean
388 test_all(void)
389 {
390 bool success = TRUE;
391
392 if (!test_one(&test_format_fetch_rgba_float, "fetch_rgba_float"))
393 success = FALSE;
394
395 if (!test_one(&test_format_pack_rgba_float, "pack_rgba_float"))
396 success = FALSE;
397
398 if (!test_one(&test_format_unpack_rgba_float, "unpack_rgba_float"))
399 success = FALSE;
400
401 if (!test_one(&test_format_pack_rgba_8unorm, "pack_rgba_8unorm"))
402 success = FALSE;
403
404 if (!test_one(&test_format_unpack_rgba_8unorm, "unpack_rgba_8unorm"))
405 success = FALSE;
406
407 return success;
408 }
409
410
411 int main(int argc, char **argv)
412 {
413 boolean success;
414
415 util_format_s3tc_init();
416
417 success = test_all();
418
419 return success ? 0 : 1;
420 }