llvmpipe: Share testing infrastructure with 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
32 #include <llvm-c/Core.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
41 #include "lp_bld_format.h"
42 #include "lp_test.h"
43
44
45 struct pixel_test_case
46 {
47 enum pipe_format format;
48 uint32_t packed;
49 double unpacked[4];
50 };
51
52
53 struct pixel_test_case test_cases[] =
54 {
55 {PIPE_FORMAT_R5G6B5_UNORM, 0x0000, {0.0, 0.0, 0.0, 1.0}},
56 {PIPE_FORMAT_R5G6B5_UNORM, 0x001f, {0.0, 0.0, 1.0, 1.0}},
57 {PIPE_FORMAT_R5G6B5_UNORM, 0x07e0, {0.0, 1.0, 0.0, 1.0}},
58 {PIPE_FORMAT_R5G6B5_UNORM, 0xf800, {1.0, 0.0, 0.0, 1.0}},
59 {PIPE_FORMAT_R5G6B5_UNORM, 0xffff, {1.0, 1.0, 1.0, 1.0}},
60
61 {PIPE_FORMAT_A1R5G5B5_UNORM, 0x0000, {0.0, 0.0, 0.0, 0.0}},
62 {PIPE_FORMAT_A1R5G5B5_UNORM, 0x001f, {0.0, 0.0, 1.0, 0.0}},
63 {PIPE_FORMAT_A1R5G5B5_UNORM, 0x03e0, {0.0, 1.0, 0.0, 0.0}},
64 {PIPE_FORMAT_A1R5G5B5_UNORM, 0x7c00, {1.0, 0.0, 0.0, 0.0}},
65 {PIPE_FORMAT_A1R5G5B5_UNORM, 0x8000, {0.0, 0.0, 0.0, 1.0}},
66 {PIPE_FORMAT_A1R5G5B5_UNORM, 0xffff, {1.0, 1.0, 1.0, 1.0}},
67
68 {PIPE_FORMAT_A8R8G8B8_UNORM, 0x00000000, {0.0, 0.0, 0.0, 0.0}},
69 {PIPE_FORMAT_A8R8G8B8_UNORM, 0x000000ff, {0.0, 0.0, 1.0, 0.0}},
70 {PIPE_FORMAT_A8R8G8B8_UNORM, 0x0000ff00, {0.0, 1.0, 0.0, 0.0}},
71 {PIPE_FORMAT_A8R8G8B8_UNORM, 0x00ff0000, {1.0, 0.0, 0.0, 0.0}},
72 {PIPE_FORMAT_A8R8G8B8_UNORM, 0xff000000, {0.0, 0.0, 0.0, 1.0}},
73 {PIPE_FORMAT_A8R8G8B8_UNORM, 0xffffffff, {1.0, 1.0, 1.0, 1.0}},
74
75 #if 0
76 {PIPE_FORMAT_R8G8B8A8_UNORM, 0x00000000, {0.0, 0.0, 0.0, 0.0}},
77 {PIPE_FORMAT_R8G8B8A8_UNORM, 0x000000ff, {0.0, 0.0, 0.0, 1.0}},
78 {PIPE_FORMAT_R8G8B8A8_UNORM, 0x0000ff00, {0.0, 0.0, 1.0, 0.0}},
79 {PIPE_FORMAT_R8G8B8A8_UNORM, 0x00ff0000, {0.0, 1.0, 0.0, 0.0}},
80 {PIPE_FORMAT_R8G8B8A8_UNORM, 0xff000000, {1.0, 0.0, 0.0, 0.0}},
81 {PIPE_FORMAT_R8G8B8A8_UNORM, 0xffffffff, {1.0, 1.0, 1.0, 1.0}},
82 #endif
83
84 {PIPE_FORMAT_B8G8R8A8_UNORM, 0x00000000, {0.0, 0.0, 0.0, 0.0}},
85 {PIPE_FORMAT_B8G8R8A8_UNORM, 0x000000ff, {0.0, 0.0, 0.0, 1.0}},
86 {PIPE_FORMAT_B8G8R8A8_UNORM, 0x0000ff00, {1.0, 0.0, 0.0, 0.0}},
87 {PIPE_FORMAT_B8G8R8A8_UNORM, 0x00ff0000, {0.0, 1.0, 0.0, 0.0}},
88 {PIPE_FORMAT_B8G8R8A8_UNORM, 0xff000000, {0.0, 0.0, 1.0, 0.0}},
89 {PIPE_FORMAT_B8G8R8A8_UNORM, 0xffffffff, {1.0, 1.0, 1.0, 1.0}},
90 };
91
92
93 void
94 write_tsv_header(FILE *fp)
95 {
96 fprintf(fp,
97 "result\t"
98 "format\n");
99
100 fflush(fp);
101 }
102
103
104 static void
105 write_tsv_row(FILE *fp,
106 const struct util_format_description *desc,
107 boolean success)
108 {
109 fprintf(fp, "%s\t", success ? "pass" : "fail");
110
111 fprintf(fp, "%s\n", desc->name);
112
113 fflush(fp);
114 }
115
116
117 typedef void (*load_ptr_t)(const uint32_t packed, float *);
118
119
120 static LLVMValueRef
121 add_load_rgba_test(LLVMModuleRef module,
122 const struct util_format_description *desc)
123 {
124 LLVMTypeRef args[2];
125 LLVMValueRef func;
126 LLVMValueRef packed;
127 LLVMValueRef rgba_ptr;
128 LLVMBasicBlockRef block;
129 LLVMBuilderRef builder;
130 LLVMValueRef rgba;
131
132 args[0] = LLVMInt32Type();
133 args[1] = LLVMPointerType(LLVMVectorType(LLVMFloatType(), 4), 0);
134
135 func = LLVMAddFunction(module, "load", LLVMFunctionType(LLVMVoidType(), args, 2, 0));
136 LLVMSetFunctionCallConv(func, LLVMCCallConv);
137 packed = LLVMGetParam(func, 0);
138 rgba_ptr = LLVMGetParam(func, 1);
139
140 block = LLVMAppendBasicBlock(func, "entry");
141 builder = LLVMCreateBuilder();
142 LLVMPositionBuilderAtEnd(builder, block);
143
144 if(desc->block.bits < 32)
145 packed = LLVMBuildTrunc(builder, packed, LLVMIntType(desc->block.bits), "");
146
147 rgba = lp_build_unpack_rgba_aos(builder, desc, packed);
148
149 LLVMBuildStore(builder, rgba, rgba_ptr);
150
151 LLVMBuildRetVoid(builder);
152
153 LLVMDisposeBuilder(builder);
154 return func;
155 }
156
157
158 typedef void (*store_ptr_t)(uint32_t *, const float *);
159
160
161 static LLVMValueRef
162 add_store_rgba_test(LLVMModuleRef module,
163 const struct util_format_description *desc)
164 {
165 LLVMTypeRef args[2];
166 LLVMValueRef func;
167 LLVMValueRef packed_ptr;
168 LLVMValueRef rgba_ptr;
169 LLVMBasicBlockRef block;
170 LLVMBuilderRef builder;
171 LLVMValueRef rgba;
172 LLVMValueRef packed;
173
174 args[0] = LLVMPointerType(LLVMInt32Type(), 0);
175 args[1] = LLVMPointerType(LLVMVectorType(LLVMFloatType(), 4), 0);
176
177 func = LLVMAddFunction(module, "store", LLVMFunctionType(LLVMVoidType(), args, 2, 0));
178 LLVMSetFunctionCallConv(func, LLVMCCallConv);
179 packed_ptr = LLVMGetParam(func, 0);
180 rgba_ptr = LLVMGetParam(func, 1);
181
182 block = LLVMAppendBasicBlock(func, "entry");
183 builder = LLVMCreateBuilder();
184 LLVMPositionBuilderAtEnd(builder, block);
185
186 rgba = LLVMBuildLoad(builder, rgba_ptr, "");
187
188 packed = lp_build_pack_rgba_aos(builder, desc, rgba);
189
190 if(desc->block.bits < 32)
191 packed = LLVMBuildZExt(builder, packed, LLVMInt32Type(), "");
192
193 LLVMBuildStore(builder, packed, packed_ptr);
194
195 LLVMBuildRetVoid(builder);
196
197 LLVMDisposeBuilder(builder);
198 return func;
199 }
200
201
202 static boolean
203 test_format(unsigned verbose, FILE *fp, const struct pixel_test_case *test)
204 {
205 LLVMModuleRef module = NULL;
206 LLVMValueRef load = NULL;
207 LLVMValueRef store = NULL;
208 LLVMExecutionEngineRef engine = NULL;
209 LLVMModuleProviderRef provider = NULL;
210 LLVMPassManagerRef pass = NULL;
211 char *error = NULL;
212 const struct util_format_description *desc;
213 load_ptr_t load_ptr;
214 store_ptr_t store_ptr;
215 float unpacked[4];
216 unsigned packed;
217 boolean success;
218 unsigned i;
219
220 desc = util_format_description(test->format);
221 fprintf(stderr, "%s\n", desc->name);
222
223 module = LLVMModuleCreateWithName("test");
224
225 load = add_load_rgba_test(module, desc);
226 store = add_store_rgba_test(module, desc);
227
228 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
229 LLVMDumpModule(module);
230 abort();
231 }
232 LLVMDisposeMessage(error);
233
234 provider = LLVMCreateModuleProviderForExistingModule(module);
235 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
236 fprintf(stderr, "%s\n", error);
237 LLVMDisposeMessage(error);
238 abort();
239 }
240
241 #if 0
242 pass = LLVMCreatePassManager();
243 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
244 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
245 * but there are more on SVN. */
246 LLVMAddConstantPropagationPass(pass);
247 LLVMAddInstructionCombiningPass(pass);
248 LLVMAddPromoteMemoryToRegisterPass(pass);
249 LLVMAddGVNPass(pass);
250 LLVMAddCFGSimplificationPass(pass);
251 LLVMRunPassManager(pass, module);
252 #else
253 (void)pass;
254 #endif
255
256 load_ptr = (load_ptr_t) LLVMGetPointerToGlobal(engine, load);
257 store_ptr = (store_ptr_t)LLVMGetPointerToGlobal(engine, store);
258
259 memset(unpacked, 0, sizeof unpacked);
260 packed = 0;
261
262 load_ptr(test->packed, unpacked);
263 store_ptr(&packed, unpacked);
264
265 success = TRUE;
266 if(test->packed != packed)
267 success = FALSE;
268 for(i = 0; i < 4; ++i)
269 if(test->unpacked[i] != unpacked[i])
270 success = FALSE;
271
272 if (!success) {
273 printf("FAILED\n");
274 printf(" Packed: %08x\n", test->packed);
275 printf(" %08x\n", packed);
276 printf(" Unpacked: %f %f %f %f\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
277 printf(" %f %f %f %f\n", test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
278 LLVMDumpModule(module);
279 }
280
281 LLVMFreeMachineCodeForFunction(engine, store);
282 LLVMFreeMachineCodeForFunction(engine, load);
283
284 LLVMDisposeExecutionEngine(engine);
285 if(pass)
286 LLVMDisposePassManager(pass);
287
288 if(fp)
289 write_tsv_row(fp, desc, success);
290
291 return success;
292 }
293
294
295 boolean
296 test_all(unsigned verbose, FILE *fp)
297 {
298 unsigned i;
299 bool success = TRUE;
300
301 for (i = 0; i < sizeof(test_cases)/sizeof(test_cases[0]); ++i)
302 if(!test_format(verbose, fp, &test_cases[i]))
303 success = FALSE;
304
305 return success;
306 }
307
308
309 boolean
310 test_some(unsigned verbose, FILE *fp, unsigned long n)
311 {
312 return test_all(verbose, fp);
313 }