gallium: Remove util_format_s3tc_init()
[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, ARRAY_SIZE(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 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);
154
155 fetch = add_fetch_rgba_test(gallivm, verbose, desc, lp_float32_vec4_type());
156
157 gallivm_compile_module(gallivm);
158
159 fetch_ptr = (fetch_ptr_t) gallivm_jit_function(gallivm, fetch);
160
161 gallivm_free_ir(gallivm);
162
163 for (l = 0; l < util_format_nr_test_cases; ++l) {
164 const struct util_format_test_case *test = &util_format_test_cases[l];
165
166 if (test->format == desc->format) {
167
168 if (first) {
169 printf("Testing %s (float) ...\n",
170 desc->name);
171 fflush(stdout);
172 first = FALSE;
173 }
174
175 /* To ensure it's 16-byte aligned */
176 memcpy(packed, test->packed, sizeof packed);
177
178 for (i = 0; i < desc->block.height; ++i) {
179 for (j = 0; j < desc->block.width; ++j) {
180 boolean match = TRUE;
181
182 memset(unpacked, 0, sizeof unpacked);
183
184 fetch_ptr(unpacked, packed, j, i, cache_ptr);
185
186 for(k = 0; k < 4; ++k) {
187 if (util_double_inf_sign(test->unpacked[i][j][k]) != util_inf_sign(unpacked[k])) {
188 match = FALSE;
189 }
190
191 if (util_is_double_nan(test->unpacked[i][j][k]) != util_is_nan(unpacked[k])) {
192 match = FALSE;
193 }
194
195 if (!util_is_double_inf_or_nan(test->unpacked[i][j][k]) &&
196 fabs((float)test->unpacked[i][j][k] - unpacked[k]) > FLT_EPSILON) {
197 match = FALSE;
198 }
199 }
200
201 /* Ignore errors in S3TC for now */
202 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
203 match = TRUE;
204 }
205
206 if (!match) {
207 printf("FAILED\n");
208 printf(" Packed: %02x %02x %02x %02x\n",
209 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
210 printf(" Unpacked (%u,%u): %.9g %.9g %.9g %.9g obtained\n",
211 j, i,
212 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
213 printf(" %.9g %.9g %.9g %.9g expected\n",
214 test->unpacked[i][j][0],
215 test->unpacked[i][j][1],
216 test->unpacked[i][j][2],
217 test->unpacked[i][j][3]);
218 fflush(stdout);
219 success = FALSE;
220 }
221 }
222 }
223 }
224 }
225
226 gallivm_destroy(gallivm);
227 LLVMContextDispose(context);
228
229 if(fp)
230 write_tsv_row(fp, desc, success);
231
232 return success;
233 }
234
235
236 PIPE_ALIGN_STACK
237 static boolean
238 test_format_unorm8(unsigned verbose, FILE *fp,
239 const struct util_format_description *desc)
240 {
241 LLVMContextRef context;
242 struct gallivm_state *gallivm;
243 LLVMValueRef fetch = NULL;
244 fetch_ptr_t fetch_ptr;
245 PIPE_ALIGN_VAR(16) uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
246 uint8_t unpacked[4];
247 boolean first = TRUE;
248 boolean success = TRUE;
249 unsigned i, j, k, l;
250
251 context = LLVMContextCreate();
252 gallivm = gallivm_create("test_module_unorm8", context);
253
254 fetch = add_fetch_rgba_test(gallivm, verbose, desc, lp_unorm8_vec4_type());
255
256 gallivm_compile_module(gallivm);
257
258 fetch_ptr = (fetch_ptr_t) gallivm_jit_function(gallivm, fetch);
259
260 gallivm_free_ir(gallivm);
261
262 for (l = 0; l < util_format_nr_test_cases; ++l) {
263 const struct util_format_test_case *test = &util_format_test_cases[l];
264
265 if (test->format == desc->format) {
266
267 if (first) {
268 printf("Testing %s (unorm8) ...\n",
269 desc->name);
270 first = FALSE;
271 }
272
273 /* To ensure it's 16-byte aligned */
274 /* Could skip this and use unaligned lp_build_fetch_rgba_aos */
275 memcpy(packed, test->packed, sizeof packed);
276
277 for (i = 0; i < desc->block.height; ++i) {
278 for (j = 0; j < desc->block.width; ++j) {
279 boolean match;
280
281 memset(unpacked, 0, sizeof unpacked);
282
283 fetch_ptr(unpacked, packed, j, i, cache_ptr);
284
285 match = TRUE;
286 for(k = 0; k < 4; ++k) {
287 int error = float_to_ubyte(test->unpacked[i][j][k]) - unpacked[k];
288
289 if (util_is_double_nan(test->unpacked[i][j][k]))
290 continue;
291
292 if (error < 0)
293 error = -error;
294
295 if (error > 1)
296 match = FALSE;
297 }
298
299 /* Ignore errors in S3TC as we only implement a poor man approach */
300 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
301 match = TRUE;
302 }
303
304 if (!match) {
305 printf("FAILED\n");
306 printf(" Packed: %02x %02x %02x %02x\n",
307 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
308 printf(" Unpacked (%u,%u): %02x %02x %02x %02x obtained\n",
309 j, i,
310 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
311 printf(" %02x %02x %02x %02x expected\n",
312 float_to_ubyte(test->unpacked[i][j][0]),
313 float_to_ubyte(test->unpacked[i][j][1]),
314 float_to_ubyte(test->unpacked[i][j][2]),
315 float_to_ubyte(test->unpacked[i][j][3]));
316
317 success = FALSE;
318 }
319 }
320 }
321 }
322 }
323
324 gallivm_destroy(gallivm);
325 LLVMContextDispose(context);
326
327 if(fp)
328 write_tsv_row(fp, desc, success);
329
330 return success;
331 }
332
333
334
335
336 static boolean
337 test_one(unsigned verbose, FILE *fp,
338 const struct util_format_description *format_desc)
339 {
340 boolean success = TRUE;
341
342 if (!test_format_float(verbose, fp, format_desc)) {
343 success = FALSE;
344 }
345
346 if (!test_format_unorm8(verbose, fp, format_desc)) {
347 success = FALSE;
348 }
349
350 return success;
351 }
352
353
354 boolean
355 test_all(unsigned verbose, FILE *fp)
356 {
357 enum pipe_format format;
358 boolean success = TRUE;
359
360 #if USE_TEXTURE_CACHE
361 cache_ptr = align_malloc(sizeof(struct lp_build_format_cache), 16);
362 #endif
363
364 for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
365 const struct util_format_description *format_desc;
366
367 format_desc = util_format_description(format);
368 if (!format_desc) {
369 continue;
370 }
371
372
373 /*
374 * TODO: test more
375 */
376
377 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
378 continue;
379 }
380
381 if (util_format_is_pure_integer(format))
382 continue;
383
384 /* only have util fetch func for etc1 */
385 if (format_desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
386 format != PIPE_FORMAT_ETC1_RGB8) {
387 continue;
388 }
389
390 /* missing fetch funcs */
391 if (format_desc->layout == UTIL_FORMAT_LAYOUT_BPTC ||
392 format_desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
393 continue;
394 }
395
396 if (!test_one(verbose, fp, format_desc)) {
397 success = FALSE;
398 }
399 }
400 #if USE_TEXTURE_CACHE
401 align_free(cache_ptr);
402 #endif
403
404 return success;
405 }
406
407
408 boolean
409 test_some(unsigned verbose, FILE *fp,
410 unsigned long n)
411 {
412 return test_all(verbose, fp);
413 }
414
415
416 boolean
417 test_single(unsigned verbose, FILE *fp)
418 {
419 printf("no test_single()");
420 return TRUE;
421 }