gallivm: Cleanups and bugfixes to aos format translation.
[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
32 #include "gallivm/lp_bld.h"
33 #include <llvm-c/Analysis.h>
34 #include <llvm-c/ExecutionEngine.h>
35 #include <llvm-c/Target.h>
36 #include <llvm-c/Transforms/Scalar.h>
37
38 #include "util/u_cpu_detect.h"
39 #include "util/u_format.h"
40 #include "util/u_format_tests.h"
41 #include "util/u_format_s3tc.h"
42
43 #include "gallivm/lp_bld_format.h"
44 #include "lp_test.h"
45
46
47 void
48 write_tsv_header(FILE *fp)
49 {
50 fprintf(fp,
51 "result\t"
52 "format\n");
53
54 fflush(fp);
55 }
56
57
58 static void
59 write_tsv_row(FILE *fp,
60 const struct util_format_description *desc,
61 boolean success)
62 {
63 fprintf(fp, "%s\t", success ? "pass" : "fail");
64
65 fprintf(fp, "%s\n", desc->name);
66
67 fflush(fp);
68 }
69
70
71 typedef void (*fetch_ptr_t)(const void *packed, float *);
72
73
74 static LLVMValueRef
75 add_fetch_rgba_test(LLVMModuleRef module,
76 const struct util_format_description *desc)
77 {
78 LLVMTypeRef args[2];
79 LLVMValueRef func;
80 LLVMValueRef packed_ptr;
81 LLVMValueRef rgba_ptr;
82 LLVMBasicBlockRef block;
83 LLVMBuilderRef builder;
84 LLVMValueRef rgba;
85
86 args[0] = LLVMPointerType(LLVMInt8Type(), 0);
87 args[1] = LLVMPointerType(LLVMVectorType(LLVMFloatType(), 4), 0);
88
89 func = LLVMAddFunction(module, "fetch", LLVMFunctionType(LLVMVoidType(), args, 2, 0));
90 LLVMSetFunctionCallConv(func, LLVMCCallConv);
91 packed_ptr = LLVMGetParam(func, 0);
92 rgba_ptr = LLVMGetParam(func, 1);
93
94 block = LLVMAppendBasicBlock(func, "entry");
95 builder = LLVMCreateBuilder();
96 LLVMPositionBuilderAtEnd(builder, block);
97
98 rgba = lp_build_fetch_rgba_aos(builder, desc, packed_ptr);
99
100 LLVMBuildStore(builder, rgba, rgba_ptr);
101
102 LLVMBuildRetVoid(builder);
103
104 LLVMDisposeBuilder(builder);
105 return func;
106 }
107
108
109 PIPE_ALIGN_STACK
110 static boolean
111 test_format(unsigned verbose, FILE *fp,
112 const struct util_format_description *desc,
113 const struct util_format_test_case *test)
114 {
115 LLVMModuleRef module = NULL;
116 LLVMValueRef fetch = NULL;
117 LLVMExecutionEngineRef engine = NULL;
118 LLVMModuleProviderRef provider = NULL;
119 LLVMPassManagerRef pass = NULL;
120 char *error = NULL;
121 fetch_ptr_t fetch_ptr;
122 float unpacked[4];
123 boolean success;
124 unsigned i;
125
126 module = LLVMModuleCreateWithName("test");
127
128 fetch = add_fetch_rgba_test(module, desc);
129
130 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
131 LLVMDumpModule(module);
132 abort();
133 }
134 LLVMDisposeMessage(error);
135
136 provider = LLVMCreateModuleProviderForExistingModule(module);
137 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
138 fprintf(stderr, "%s\n", error);
139 LLVMDisposeMessage(error);
140 abort();
141 }
142
143 #if 0
144 pass = LLVMCreatePassManager();
145 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
146 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
147 * but there are more on SVN. */
148 LLVMAddConstantPropagationPass(pass);
149 LLVMAddInstructionCombiningPass(pass);
150 LLVMAddPromoteMemoryToRegisterPass(pass);
151 LLVMAddGVNPass(pass);
152 LLVMAddCFGSimplificationPass(pass);
153 LLVMRunPassManager(pass, module);
154 #else
155 (void)pass;
156 #endif
157
158 fetch_ptr = (fetch_ptr_t) LLVMGetPointerToGlobal(engine, fetch);
159
160 memset(unpacked, 0, sizeof unpacked);
161
162 fetch_ptr(test->packed, unpacked);
163
164 success = TRUE;
165 for(i = 0; i < 4; ++i)
166 if(test->unpacked[0][0][i] != unpacked[i])
167 success = FALSE;
168
169 if (!success) {
170 printf("FAILED\n");
171 printf(" Packed: %02x %02x %02x %02x\n",
172 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
173 printf(" Unpacked: %f %f %f %f obtained\n",
174 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
175 printf(" %f %f %f %f expected\n",
176 test->unpacked[0][0][0],
177 test->unpacked[0][0][1],
178 test->unpacked[0][0][2],
179 test->unpacked[0][0][3]);
180 LLVMDumpModule(module);
181 }
182
183 LLVMFreeMachineCodeForFunction(engine, fetch);
184
185 LLVMDisposeExecutionEngine(engine);
186 if(pass)
187 LLVMDisposePassManager(pass);
188
189 if(fp)
190 write_tsv_row(fp, desc, success);
191
192 return success;
193 }
194
195
196
197 static boolean
198 test_one(unsigned verbose, FILE *fp,
199 const struct util_format_description *format_desc)
200 {
201 unsigned i;
202 bool success = TRUE;
203
204 printf("Testing %s ...\n",
205 format_desc->name);
206
207 for (i = 0; i < util_format_nr_test_cases; ++i) {
208 const struct util_format_test_case *test = &util_format_test_cases[i];
209
210 if (test->format == format_desc->format) {
211
212 if (!test_format(verbose, fp, format_desc, test)) {
213 success = FALSE;
214 }
215
216 }
217 }
218
219 return success;
220 }
221
222
223 boolean
224 test_all(unsigned verbose, FILE *fp)
225 {
226 enum pipe_format format;
227 bool success = TRUE;
228
229 for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
230 const struct util_format_description *format_desc;
231
232 format_desc = util_format_description(format);
233 if (!format_desc) {
234 continue;
235 }
236
237 /*
238 * XXX: copied from lp_build_fetch_rgba_aos()
239 * TODO: test more
240 */
241
242 if (!(format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
243 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
244 format_desc->block.width == 1 &&
245 format_desc->block.height == 1 &&
246 util_is_pot(format_desc->block.bits) &&
247 format_desc->block.bits <= 32 &&
248 format_desc->is_bitmask &&
249 !format_desc->is_mixed &&
250 (format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED ||
251 format_desc->channel[1].type == UTIL_FORMAT_TYPE_UNSIGNED))) {
252 continue;
253 }
254
255 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC &&
256 !util_format_s3tc_enabled) {
257 continue;
258 }
259
260 if (!test_one(verbose, fp, format_desc)) {
261 success = FALSE;
262 }
263 }
264
265 return success;
266 }
267
268
269 boolean
270 test_some(unsigned verbose, FILE *fp, unsigned long n)
271 {
272 return test_all(verbose, fp);
273 }