1/**************************************************************************
2 *
3 * Copyright 2009 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_memory.h"
34#include "util/u_pointer.h"
35#include "util/u_string.h"
36#include "util/format/u_format.h"
37#include "util/format/u_format_tests.h"
38#include "util/format/u_format_s3tc.h"
39
40#include "gallivm/lp_bld.h"
41#include "gallivm/lp_bld_debug.h"
42#include "gallivm/lp_bld_format.h"
43#include "gallivm/lp_bld_init.h"
44
45#include "lp_test.h"
46
47static struct lp_build_format_cache *cache_ptr;
48
49void
50write_tsv_header(FILE *fp)
51{
52   fprintf(fp,
53           "result\t"
54           "format\n");
55
56   fflush(fp);
57}
58
59
60static void
61write_tsv_row(FILE *fp,
62              const struct util_format_description *desc,
63              boolean success)
64{
65   fprintf(fp, "%s\t", success ? "pass" : "fail");
66
67   fprintf(fp, "%s\n", desc->name);
68
69   fflush(fp);
70}
71
72
73typedef void
74(*fetch_ptr_t)(void *unpacked, const void *packed,
75               unsigned i, unsigned j, struct lp_build_format_cache *cache);
76
77
78static LLVMValueRef
79add_fetch_rgba_test(struct gallivm_state *gallivm, unsigned verbose,
80                    const struct util_format_description *desc,
81                    struct lp_type type,
82                    unsigned use_cache)
83{
84   char name[256];
85   LLVMContextRef context = gallivm->context;
86   LLVMModuleRef module = gallivm->module;
87   LLVMBuilderRef builder = gallivm->builder;
88   LLVMTypeRef args[5];
89   LLVMValueRef func;
90   LLVMValueRef packed_ptr;
91   LLVMValueRef offset = LLVMConstNull(LLVMInt32TypeInContext(context));
92   LLVMValueRef rgba_ptr;
93   LLVMValueRef i;
94   LLVMValueRef j;
95   LLVMBasicBlockRef block;
96   LLVMValueRef rgba;
97   LLVMValueRef cache = NULL;
98
99   snprintf(name, sizeof name, "fetch_%s_%s", desc->short_name,
100            type.floating ? "float" : "unorm8");
101
102   args[0] = LLVMPointerType(lp_build_vec_type(gallivm, type), 0);
103   args[1] = LLVMPointerType(LLVMInt8TypeInContext(context), 0);
104   args[3] = args[2] = LLVMInt32TypeInContext(context);
105   args[4] = LLVMPointerType(lp_build_format_cache_type(gallivm), 0);
106
107   func = LLVMAddFunction(module, name,
108                          LLVMFunctionType(LLVMVoidTypeInContext(context),
109                                           args, ARRAY_SIZE(args), 0));
110   LLVMSetFunctionCallConv(func, LLVMCCallConv);
111   rgba_ptr = LLVMGetParam(func, 0);
112   packed_ptr = LLVMGetParam(func, 1);
113   i = LLVMGetParam(func, 2);
114   j = LLVMGetParam(func, 3);
115
116   if (use_cache) {
117      cache = LLVMGetParam(func, 4);
118   }
119
120   block = LLVMAppendBasicBlockInContext(context, func, "entry");
121   LLVMPositionBuilderAtEnd(builder, block);
122
123   rgba = lp_build_fetch_rgba_aos(gallivm, desc, type, TRUE,
124                                  packed_ptr, offset, i, j, cache);
125
126   LLVMBuildStore(builder, rgba, rgba_ptr);
127
128   LLVMBuildRetVoid(builder);
129
130   gallivm_verify_function(gallivm, func);
131
132   return func;
133}
134
135
136PIPE_ALIGN_STACK
137static boolean
138test_format_float(unsigned verbose, FILE *fp,
139                  const struct util_format_description *desc,
140                  unsigned use_cache)
141{
142   LLVMContextRef context;
143   struct gallivm_state *gallivm;
144   LLVMValueRef fetch = NULL;
145   fetch_ptr_t fetch_ptr;
146   PIPE_ALIGN_VAR(16) uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
147   PIPE_ALIGN_VAR(16) float unpacked[4];
148   boolean first = TRUE;
149   boolean success = TRUE;
150   unsigned i, j, k, l;
151
152   context = LLVMContextCreate();
153   gallivm = gallivm_create("test_module_float", context, NULL);
154
155   fetch = add_fetch_rgba_test(gallivm, verbose, desc,
156                               lp_float32_vec4_type(), use_cache);
157
158   gallivm_compile_module(gallivm);
159
160   fetch_ptr = (fetch_ptr_t) gallivm_jit_function(gallivm, fetch);
161
162   gallivm_free_ir(gallivm);
163
164   for (l = 0; l < util_format_nr_test_cases; ++l) {
165      const struct util_format_test_case *test = &util_format_test_cases[l];
166
167      if (test->format == desc->format) {
168
169         if (first) {
170            printf("Testing %s (float) ...\n",
171                   desc->name);
172            fflush(stdout);
173            first = FALSE;
174         }
175
176         /* To ensure it's 16-byte aligned */
177         memcpy(packed, test->packed, sizeof packed);
178
179         for (i = 0; i < desc->block.height; ++i) {
180            for (j = 0; j < desc->block.width; ++j) {
181               boolean match = TRUE;
182
183               memset(unpacked, 0, sizeof unpacked);
184
185               fetch_ptr(unpacked, packed, j, i, use_cache ? cache_ptr : NULL);
186
187               for(k = 0; k < 4; ++k) {
188                  if (util_double_inf_sign(test->unpacked[i][j][k]) != util_inf_sign(unpacked[k])) {
189                     match = FALSE;
190                  }
191
192                  if (util_is_double_nan(test->unpacked[i][j][k]) != util_is_nan(unpacked[k])) {
193                     match = FALSE;
194                  }
195
196                  if (!util_is_double_inf_or_nan(test->unpacked[i][j][k]) &&
197                      fabs((float)test->unpacked[i][j][k] - unpacked[k]) > FLT_EPSILON) {
198                     match = FALSE;
199                  }
200               }
201
202               /* Ignore errors in S3TC for now */
203               if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
204                  match = TRUE;
205               }
206
207               if (!match) {
208                  printf("FAILED\n");
209                  printf("  Packed: %02x %02x %02x %02x\n",
210                         test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
211                  printf("  Unpacked (%u,%u): %.9g %.9g %.9g %.9g obtained\n",
212                         j, i,
213                         unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
214                  printf("                  %.9g %.9g %.9g %.9g expected\n",
215                         test->unpacked[i][j][0],
216                         test->unpacked[i][j][1],
217                         test->unpacked[i][j][2],
218                         test->unpacked[i][j][3]);
219                  fflush(stdout);
220                  success = FALSE;
221               }
222            }
223         }
224      }
225   }
226
227   gallivm_destroy(gallivm);
228   LLVMContextDispose(context);
229
230   if(fp)
231      write_tsv_row(fp, desc, success);
232
233   return success;
234}
235
236
237PIPE_ALIGN_STACK
238static boolean
239test_format_unorm8(unsigned verbose, FILE *fp,
240                   const struct util_format_description *desc,
241                   unsigned use_cache)
242{
243   LLVMContextRef context;
244   struct gallivm_state *gallivm;
245   LLVMValueRef fetch = NULL;
246   fetch_ptr_t fetch_ptr;
247   PIPE_ALIGN_VAR(16) uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
248   uint8_t unpacked[4];
249   boolean first = TRUE;
250   boolean success = TRUE;
251   unsigned i, j, k, l;
252
253   context = LLVMContextCreate();
254   gallivm = gallivm_create("test_module_unorm8", context, NULL);
255
256   fetch = add_fetch_rgba_test(gallivm, verbose, desc,
257                               lp_unorm8_vec4_type(), use_cache);
258
259   gallivm_compile_module(gallivm);
260
261   fetch_ptr = (fetch_ptr_t) gallivm_jit_function(gallivm, fetch);
262
263   gallivm_free_ir(gallivm);
264
265   for (l = 0; l < util_format_nr_test_cases; ++l) {
266      const struct util_format_test_case *test = &util_format_test_cases[l];
267
268      if (test->format == desc->format) {
269
270         if (first) {
271            printf("Testing %s (unorm8) ...\n",
272                   desc->name);
273            first = FALSE;
274         }
275
276         /* To ensure it's 16-byte aligned */
277         /* Could skip this and use unaligned lp_build_fetch_rgba_aos */
278         memcpy(packed, test->packed, sizeof packed);
279
280         for (i = 0; i < desc->block.height; ++i) {
281            for (j = 0; j < desc->block.width; ++j) {
282               boolean match;
283
284               memset(unpacked, 0, sizeof unpacked);
285
286               fetch_ptr(unpacked, packed, j, i, use_cache ? cache_ptr : NULL);
287
288               match = TRUE;
289               for(k = 0; k < 4; ++k) {
290                  int error = float_to_ubyte(test->unpacked[i][j][k]) - unpacked[k];
291
292                  if (util_is_double_nan(test->unpacked[i][j][k]))
293                     continue;
294
295                  if (error < 0)
296                     error = -error;
297
298                  if (error > 1)
299                     match = FALSE;
300               }
301
302               /* Ignore errors in S3TC as we only implement a poor man approach */
303               if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
304                  match = TRUE;
305               }
306
307               if (!match) {
308                  printf("FAILED\n");
309                  printf("  Packed: %02x %02x %02x %02x\n",
310                         test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
311                  printf("  Unpacked (%u,%u): %02x %02x %02x %02x obtained\n",
312                         j, i,
313                         unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
314                  printf("                  %02x %02x %02x %02x expected\n",
315                         float_to_ubyte(test->unpacked[i][j][0]),
316                         float_to_ubyte(test->unpacked[i][j][1]),
317                         float_to_ubyte(test->unpacked[i][j][2]),
318                         float_to_ubyte(test->unpacked[i][j][3]));
319
320                  success = FALSE;
321               }
322            }
323         }
324      }
325   }
326
327   gallivm_destroy(gallivm);
328   LLVMContextDispose(context);
329
330   if(fp)
331      write_tsv_row(fp, desc, success);
332
333   return success;
334}
335
336
337
338
339static boolean
340test_one(unsigned verbose, FILE *fp,
341         const struct util_format_description *format_desc,
342         unsigned use_cache)
343{
344   boolean success = TRUE;
345
346   if (!test_format_float(verbose, fp, format_desc, use_cache)) {
347     success = FALSE;
348   }
349
350   if (!test_format_unorm8(verbose, fp, format_desc, use_cache)) {
351     success = FALSE;
352   }
353
354   return success;
355}
356
357
358boolean
359test_all(unsigned verbose, FILE *fp)
360{
361   enum pipe_format format;
362   boolean success = TRUE;
363   unsigned use_cache;
364
365   cache_ptr = align_malloc(sizeof(struct lp_build_format_cache), 16);
366
367   for (use_cache = 0; use_cache < 2; use_cache++) {
368      for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
369         const struct util_format_description *format_desc;
370
371         format_desc = util_format_description(format);
372         if (!format_desc) {
373            continue;
374         }
375
376         /*
377          * TODO: test more
378          */
379
380         if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
381            continue;
382         }
383
384         if (util_format_is_pure_integer(format))
385            continue;
386
387         /* The codegen sometimes falls back to calling the precompiled fetch
388          * func, so if we don't have one of those (some compressed formats,
389          * some ), we can't reliably test it.  We'll surely have a
390          * precompiled fetch func for any format before we write LLVM code to
391          * fetch from it.
392          */
393         if (!util_format_fetch_rgba_func(format))
394            continue;
395
396         /* only test twice with formats which can use cache */
397         if (format_desc->layout != UTIL_FORMAT_LAYOUT_S3TC && use_cache) {
398            continue;
399         }
400
401         if (!test_one(verbose, fp, format_desc, use_cache)) {
402            success = FALSE;
403         }
404      }
405   }
406   align_free(cache_ptr);
407
408   return success;
409}
410
411
412boolean
413test_some(unsigned verbose, FILE *fp,
414          unsigned long n)
415{
416   return test_all(verbose, fp);
417}
418
419
420boolean
421test_single(unsigned verbose, FILE *fp)
422{
423   printf("no test_single()");
424   return TRUE;
425}
426