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