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 static struct lp_build_format_cache *cache_ptr;
48
49 void
write_tsv_header(FILE * fp)50 write_tsv_header(FILE *fp)
51 {
52 fprintf(fp,
53 "result\t"
54 "format\n");
55
56 fflush(fp);
57 }
58
59
60 static void
write_tsv_row(FILE * fp,const struct util_format_description * desc,boolean success)61 write_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
73 typedef void
74 (*fetch_ptr_t)(void *unpacked, const void *packed,
75 unsigned i, unsigned j, struct lp_build_format_cache *cache);
76
77
78 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)79 add_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
136 PIPE_ALIGN_STACK
137 static boolean
test_format_float(unsigned verbose,FILE * fp,const struct util_format_description * desc,unsigned use_cache)138 test_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 alignas(16) uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
147 alignas(16) float unpacked[4];
148 boolean first = TRUE;
149 boolean success = TRUE;
150 unsigned i, j, k, l;
151
152 context = LLVMContextCreate();
153 #if LLVM_VERSION_MAJOR >= 15
154 LLVMContextSetOpaquePointers(context, false);
155 #endif
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);
160
161 gallivm_compile_module(gallivm);
162
163 fetch_ptr = (fetch_ptr_t) gallivm_jit_function(gallivm, fetch);
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 boolean 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 LLVMContextDispose(context);
232
233 if(fp)
234 write_tsv_row(fp, desc, success);
235
236 return success;
237 }
238
239
240 PIPE_ALIGN_STACK
241 static boolean
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 LLVMContextRef context;
247 struct gallivm_state *gallivm;
248 LLVMValueRef fetch = NULL;
249 fetch_ptr_t fetch_ptr;
250 alignas(16) uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
251 uint8_t unpacked[4];
252 boolean first = TRUE;
253 boolean success = TRUE;
254 unsigned i, j, k, l;
255
256 context = LLVMContextCreate();
257 #if LLVM_VERSION_MAJOR >= 15
258 LLVMContextSetOpaquePointers(context, false);
259 #endif
260 gallivm = gallivm_create("test_module_unorm8", context, NULL);
261
262 fetch = add_fetch_rgba_test(gallivm, verbose, desc,
263 lp_unorm8_vec4_type(), use_cache);
264
265 gallivm_compile_module(gallivm);
266
267 fetch_ptr = (fetch_ptr_t) gallivm_jit_function(gallivm, fetch);
268
269 gallivm_free_ir(gallivm);
270
271 for (l = 0; l < util_format_nr_test_cases; ++l) {
272 const struct util_format_test_case *test = &util_format_test_cases[l];
273
274 if (test->format == desc->format) {
275
276 if (first) {
277 printf("Testing %s (unorm8) ...\n",
278 desc->name);
279 first = FALSE;
280 }
281
282 /* To ensure it's 16-byte aligned */
283 /* Could skip this and use unaligned lp_build_fetch_rgba_aos */
284 memcpy(packed, test->packed, sizeof packed);
285
286 for (i = 0; i < desc->block.height; ++i) {
287 for (j = 0; j < desc->block.width; ++j) {
288 boolean match;
289
290 memset(unpacked, 0, sizeof unpacked);
291
292 fetch_ptr(unpacked, packed, j, i, use_cache ? cache_ptr : NULL);
293
294 match = TRUE;
295 for(k = 0; k < 4; ++k) {
296 int error = float_to_ubyte(test->unpacked[i][j][k]) - unpacked[k];
297
298 if (util_is_double_nan(test->unpacked[i][j][k]))
299 continue;
300
301 if (error < 0)
302 error = -error;
303
304 if (error > 1)
305 match = FALSE;
306 }
307
308 /* Ignore errors in S3TC as we only implement a poor man approach */
309 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
310 match = TRUE;
311 }
312
313 if (!match) {
314 printf("FAILED\n");
315 printf(" Packed: %02x %02x %02x %02x\n",
316 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
317 printf(" Unpacked (%u,%u): %02x %02x %02x %02x obtained\n",
318 j, i,
319 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
320 printf(" %02x %02x %02x %02x expected\n",
321 float_to_ubyte(test->unpacked[i][j][0]),
322 float_to_ubyte(test->unpacked[i][j][1]),
323 float_to_ubyte(test->unpacked[i][j][2]),
324 float_to_ubyte(test->unpacked[i][j][3]));
325
326 success = FALSE;
327 }
328 }
329 }
330 }
331 }
332
333 gallivm_destroy(gallivm);
334 LLVMContextDispose(context);
335
336 if(fp)
337 write_tsv_row(fp, desc, success);
338
339 return success;
340 }
341
342
343
344
345 static boolean
test_one(unsigned verbose,FILE * fp,const struct util_format_description * format_desc,unsigned use_cache)346 test_one(unsigned verbose, FILE *fp,
347 const struct util_format_description *format_desc,
348 unsigned use_cache)
349 {
350 boolean success = TRUE;
351
352 if (!test_format_float(verbose, fp, format_desc, use_cache)) {
353 success = FALSE;
354 }
355
356 if (!test_format_unorm8(verbose, fp, format_desc, use_cache)) {
357 success = FALSE;
358 }
359
360 return success;
361 }
362
363
364 boolean
test_all(unsigned verbose,FILE * fp)365 test_all(unsigned verbose, FILE *fp)
366 {
367 enum pipe_format format;
368 boolean success = TRUE;
369 unsigned use_cache;
370
371 cache_ptr = align_malloc(sizeof(struct lp_build_format_cache), 16);
372
373 for (use_cache = 0; use_cache < 2; use_cache++) {
374 for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
375 const struct util_format_description *format_desc;
376
377 format_desc = util_format_description(format);
378
379 /*
380 * TODO: test more
381 */
382
383 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
384 continue;
385 }
386
387 if (util_format_is_pure_integer(format))
388 continue;
389
390 /* The codegen sometimes falls back to calling the precompiled fetch
391 * func, so if we don't have one of those (some compressed formats,
392 * some ), we can't reliably test it. We'll surely have a
393 * precompiled fetch func for any format before we write LLVM code to
394 * fetch from it.
395 */
396 if (!util_format_fetch_rgba_func(format))
397 continue;
398
399 /* only test twice with formats which can use cache */
400 if (format_desc->layout != UTIL_FORMAT_LAYOUT_S3TC && use_cache) {
401 continue;
402 }
403
404 if (!test_one(verbose, fp, format_desc, use_cache)) {
405 success = FALSE;
406 }
407 }
408 }
409 align_free(cache_ptr);
410
411 return success;
412 }
413
414
415 boolean
test_some(unsigned verbose,FILE * fp,unsigned long n)416 test_some(unsigned verbose, FILE *fp,
417 unsigned long n)
418 {
419 return test_all(verbose, fp);
420 }
421
422
423 boolean
test_single(unsigned verbose,FILE * fp)424 test_single(unsigned verbose, FILE *fp)
425 {
426 printf("no test_single()");
427 return TRUE;
428 }
429