summaryrefslogtreecommitdiff
path: root/progs/gallium/unit/u_format_test.c
blob: 54cb6b879e55a8dee5a9fa6f0089de1e720636a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**************************************************************************
 *
 * Copyright 2009-2010 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/


#include <stdlib.h>
#include <stdio.h>

#include "util/u_format.h"
#include "util/u_format_tests.h"


static boolean
test_format_fetch_float(const struct util_format_description *format_desc,
                        const struct util_format_test_case *test)
{
   float unpacked[4];
   unsigned i;
   boolean success;

   /*
    * TODO: test block formats too.
    */
   if (format_desc->block.width != 1 && format_desc->block.height != 1) {
     return TRUE;
   }

   format_desc->fetch_float(unpacked, test->packed, 0, 0);

   success = TRUE;
   for (i = 0; i < 4; ++i)
      if (test->unpacked[i] != unpacked[i])
         success = FALSE;

   if (!success) {
      printf("FAILED: (%f %f %f %f) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
      printf("        (%f %f %f %f) expected\n", test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
   }

   return success;
}


static boolean
test_format_unpack_float(const struct util_format_description *format_desc,
                         const struct util_format_test_case *test)
{
   float unpacked[4];
   unsigned i;
   boolean success;

   format_desc->unpack_float(unpacked, 0, test->packed, 0, 1, 1);

   success = TRUE;
   for (i = 0; i < 4; ++i)
      if (test->unpacked[i] != unpacked[i])
         success = FALSE;

   if (!success) {
      printf("FAILED: (%f %f %f %f) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
      printf("        (%f %f %f %f) expected\n", test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
   }

   return success;
}


static boolean
test_format_pack_float(const struct util_format_description *format_desc,
                       const struct util_format_test_case *test)
{
   float unpacked[4];
   uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
   unsigned i;
   boolean success;

   memset(packed, 0, sizeof packed);

   for (i = 0; i < 4; ++i)
      unpacked[i] = (float) test->unpacked[i];

   format_desc->pack_float(packed, 0, unpacked, 0, 1, 1);

   success = TRUE;
   for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
      if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
         success = FALSE;

   if (!success) {
      /* TODO: print more than 4 bytes */
      printf("FAILED: (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) obtained\n",
             packed[0], packed[1], packed[2], packed[3],
             packed[4], packed[5], packed[6], packed[7],
             packed[8], packed[9], packed[10], packed[11],
             packed[12], packed[13], packed[14], packed[15]);
      printf("        (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) expected\n",
             test->packed[0], test->packed[1], test->packed[2], test->packed[3],
             test->packed[4], test->packed[5], test->packed[6], test->packed[7],
             test->packed[8], test->packed[9], test->packed[10], test->packed[11],
             test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
   }

   return success;
}


static boolean
convert_float_to_8unorm(uint8_t *dst, const double *src)
{
   unsigned i;
   boolean accurate = TRUE;

   for (i = 0; i < 4; ++i) {
      if (src[i] < 0.0) {
         accurate = FALSE;
         dst[i] = 0;
      }
      else if (src[i] > 1.0) {
         accurate = FALSE;
         dst[i] = 255;
      }
      else {
         dst[i] = src[i] * 255.0;
      }
   }

   return accurate;
}


static boolean
test_format_unpack_8unorm(const struct util_format_description *format_desc,
                          const struct util_format_test_case *test)
{
   uint8_t unpacked[4];
   uint8_t expected[4];
   unsigned i;
   boolean success;

   format_desc->unpack_8unorm(unpacked, 0, test->packed, 0, 1, 1);

   convert_float_to_8unorm(expected, test->unpacked);

   success = TRUE;
   for (i = 0; i < 4; ++i)
      if (expected[i] != unpacked[i])
         success = FALSE;

   if (!success) {
      printf("FAILED: (0x%02x 0x%02x 0x%02x 0x%02x) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
      printf("        (0x%02x 0x%02x 0x%02x 0x%02x) expected\n", expected[0], expected[1], expected[2], expected[3]);
   }

   return success;
}


static boolean
test_format_pack_8unorm(const struct util_format_description *format_desc,
                        const struct util_format_test_case *test)
{
   uint8_t unpacked[4];
   uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
   unsigned i;
   boolean success;

   if (!convert_float_to_8unorm(unpacked, test->unpacked)) {
      /*
       * Skip test cases which cannot be represented by four unorm bytes.
       */
      return TRUE;
   }

   memset(packed, 0, sizeof packed);

   format_desc->pack_8unorm(packed, 0, unpacked, 0, 1, 1);

   success = TRUE;
   for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
      if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
         success = FALSE;

   if (!success) {
      /* TODO: print more than 4 bytes */
      printf("FAILED: (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) obtained\n",
             packed[0], packed[1], packed[2], packed[3],
             packed[4], packed[5], packed[6], packed[7],
             packed[8], packed[9], packed[10], packed[11],
             packed[12], packed[13], packed[14], packed[15]);
      printf("        (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) expected\n",
             test->packed[0], test->packed[1], test->packed[2], test->packed[3],
             test->packed[4], test->packed[5], test->packed[6], test->packed[7],
             test->packed[8], test->packed[9], test->packed[10], test->packed[11],
             test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
   }

   return success;
}


typedef boolean
(*test_func_t)(const struct util_format_description *format_desc,
               const struct util_format_test_case *test);


static boolean
test_one(test_func_t func, const char *suffix)
{
   enum pipe_format last_format = PIPE_FORMAT_NONE;
   unsigned i;
   bool success = TRUE;

   for (i = 0; i < util_format_nr_test_cases; ++i) {
      const struct util_format_test_case *test = &util_format_test_cases[i];
      const struct util_format_description *format_desc;
      format_desc = util_format_description(test->format);

      if (test->format != last_format) {
         printf("Testing util_format_%s_%s ...\n", format_desc->short_name, suffix);
         last_format = test->format;
      }

      if (!func(format_desc, &util_format_test_cases[i]))
        success = FALSE;
   }

   return success;
}


static boolean
test_all(void)
{
   bool success = TRUE;

   if (!test_one(&test_format_fetch_float, "fetch_float"))
     success = FALSE;

   if (!test_one(&test_format_pack_float, "pack_float"))
     success = FALSE;

   if (!test_one(&test_format_unpack_float, "unpack_float"))
     success = FALSE;

   if (!test_one(&test_format_pack_8unorm, "pack_8unorm"))
     success = FALSE;

   if (!test_one(&test_format_unpack_8unorm, "unpack_8unorm"))
     success = FALSE;

   return success;
}


int main(int argc, char **argv)
{
   boolean success;

   success = test_all();

   return success ? 0 : 1;
}