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