Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_blend.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 /**
30 * @file
31 * Unit tests for blend LLVM IR generation
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 *
35 * Blend computation code derived from code written by
36 * @author Brian Paul <brian@vmware.com>
37 */
38
39
40 #include "gallivm/lp_bld_init.h"
41 #include "gallivm/lp_bld_type.h"
42 #include "gallivm/lp_bld_debug.h"
43 #include "lp_bld_blend.h"
44 #include "lp_test.h"
45
46
47 enum vector_mode
48 {
49 AoS = 0,
50 SoA = 1
51 };
52
53
54 typedef void (*blend_test_ptr_t)(const void *src, const void *dst, const void *con, void *res);
55
56 /** cast wrapper */
57 static blend_test_ptr_t
58 voidptr_to_blend_test_ptr_t(void *p)
59 {
60 union {
61 void *v;
62 blend_test_ptr_t f;
63 } u;
64 u.v = p;
65 return u.f;
66 }
67
68
69
70 void
71 write_tsv_header(FILE *fp)
72 {
73 fprintf(fp,
74 "result\t"
75 "cycles_per_channel\t"
76 "mode\t"
77 "type\t"
78 "sep_func\t"
79 "sep_src_factor\t"
80 "sep_dst_factor\t"
81 "rgb_func\t"
82 "rgb_src_factor\t"
83 "rgb_dst_factor\t"
84 "alpha_func\t"
85 "alpha_src_factor\t"
86 "alpha_dst_factor\n");
87
88 fflush(fp);
89 }
90
91
92 static void
93 write_tsv_row(FILE *fp,
94 const struct pipe_blend_state *blend,
95 enum vector_mode mode,
96 struct lp_type type,
97 double cycles,
98 boolean success)
99 {
100 fprintf(fp, "%s\t", success ? "pass" : "fail");
101
102 if (mode == AoS) {
103 fprintf(fp, "%.1f\t", cycles / type.length);
104 fprintf(fp, "aos\t");
105 }
106
107 if (mode == SoA) {
108 fprintf(fp, "%.1f\t", cycles / (4 * type.length));
109 fprintf(fp, "soa\t");
110 }
111
112 fprintf(fp, "%s%u%sx%u\t",
113 type.floating ? "f" : (type.fixed ? "h" : (type.sign ? "s" : "u")),
114 type.width,
115 type.norm ? "n" : "",
116 type.length);
117
118 fprintf(fp,
119 "%s\t%s\t%s\t",
120 blend->rt[0].rgb_func != blend->rt[0].alpha_func ? "true" : "false",
121 blend->rt[0].rgb_src_factor != blend->rt[0].alpha_src_factor ? "true" : "false",
122 blend->rt[0].rgb_dst_factor != blend->rt[0].alpha_dst_factor ? "true" : "false");
123
124 fprintf(fp,
125 "%s\t%s\t%s\t%s\t%s\t%s\n",
126 util_dump_blend_func(blend->rt[0].rgb_func, TRUE),
127 util_dump_blend_factor(blend->rt[0].rgb_src_factor, TRUE),
128 util_dump_blend_factor(blend->rt[0].rgb_dst_factor, TRUE),
129 util_dump_blend_func(blend->rt[0].alpha_func, TRUE),
130 util_dump_blend_factor(blend->rt[0].alpha_src_factor, TRUE),
131 util_dump_blend_factor(blend->rt[0].alpha_dst_factor, TRUE));
132
133 fflush(fp);
134 }
135
136
137 static void
138 dump_blend_type(FILE *fp,
139 const struct pipe_blend_state *blend,
140 enum vector_mode mode,
141 struct lp_type type)
142 {
143 fprintf(fp, "%s", mode ? "soa" : "aos");
144
145 fprintf(fp, " type=%s%u%sx%u",
146 type.floating ? "f" : (type.fixed ? "h" : (type.sign ? "s" : "u")),
147 type.width,
148 type.norm ? "n" : "",
149 type.length);
150
151 fprintf(fp,
152 " %s=%s %s=%s %s=%s %s=%s %s=%s %s=%s",
153 "rgb_func", util_dump_blend_func(blend->rt[0].rgb_func, TRUE),
154 "rgb_src_factor", util_dump_blend_factor(blend->rt[0].rgb_src_factor, TRUE),
155 "rgb_dst_factor", util_dump_blend_factor(blend->rt[0].rgb_dst_factor, TRUE),
156 "alpha_func", util_dump_blend_func(blend->rt[0].alpha_func, TRUE),
157 "alpha_src_factor", util_dump_blend_factor(blend->rt[0].alpha_src_factor, TRUE),
158 "alpha_dst_factor", util_dump_blend_factor(blend->rt[0].alpha_dst_factor, TRUE));
159
160 fprintf(fp, " ...\n");
161 fflush(fp);
162 }
163
164
165 static LLVMValueRef
166 add_blend_test(LLVMModuleRef module,
167 const struct pipe_blend_state *blend,
168 enum vector_mode mode,
169 struct lp_type type)
170 {
171 LLVMTypeRef vec_type;
172 LLVMTypeRef args[4];
173 LLVMValueRef func;
174 LLVMValueRef src_ptr;
175 LLVMValueRef dst_ptr;
176 LLVMValueRef const_ptr;
177 LLVMValueRef res_ptr;
178 LLVMBasicBlockRef block;
179 LLVMBuilderRef builder;
180 const unsigned rt = 0;
181
182 vec_type = lp_build_vec_type(type);
183
184 args[3] = args[2] = args[1] = args[0] = LLVMPointerType(vec_type, 0);
185 func = LLVMAddFunction(module, "test", LLVMFunctionType(LLVMVoidType(), args, 4, 0));
186 LLVMSetFunctionCallConv(func, LLVMCCallConv);
187 src_ptr = LLVMGetParam(func, 0);
188 dst_ptr = LLVMGetParam(func, 1);
189 const_ptr = LLVMGetParam(func, 2);
190 res_ptr = LLVMGetParam(func, 3);
191
192 block = LLVMAppendBasicBlock(func, "entry");
193 builder = LLVMCreateBuilder();
194 LLVMPositionBuilderAtEnd(builder, block);
195
196 if (mode == AoS) {
197 LLVMValueRef src;
198 LLVMValueRef dst;
199 LLVMValueRef con;
200 LLVMValueRef res;
201
202 src = LLVMBuildLoad(builder, src_ptr, "src");
203 dst = LLVMBuildLoad(builder, dst_ptr, "dst");
204 con = LLVMBuildLoad(builder, const_ptr, "const");
205
206 res = lp_build_blend_aos(builder, blend, type, rt, src, dst, con, 3);
207
208 lp_build_name(res, "res");
209
210 LLVMBuildStore(builder, res, res_ptr);
211 }
212
213 if (mode == SoA) {
214 LLVMValueRef src[4];
215 LLVMValueRef dst[4];
216 LLVMValueRef con[4];
217 LLVMValueRef res[4];
218 unsigned i;
219
220 for(i = 0; i < 4; ++i) {
221 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
222 src[i] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, src_ptr, &index, 1, ""), "");
223 dst[i] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dst_ptr, &index, 1, ""), "");
224 con[i] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, const_ptr, &index, 1, ""), "");
225 lp_build_name(src[i], "src.%c", "rgba"[i]);
226 lp_build_name(con[i], "con.%c", "rgba"[i]);
227 lp_build_name(dst[i], "dst.%c", "rgba"[i]);
228 }
229
230 lp_build_blend_soa(builder, blend, type, rt, src, dst, con, res);
231
232 for(i = 0; i < 4; ++i) {
233 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
234 lp_build_name(res[i], "res.%c", "rgba"[i]);
235 LLVMBuildStore(builder, res[i], LLVMBuildGEP(builder, res_ptr, &index, 1, ""));
236 }
237 }
238
239 LLVMBuildRetVoid(builder);;
240
241 LLVMDisposeBuilder(builder);
242 return func;
243 }
244
245
246 static void
247 compute_blend_ref_term(unsigned rgb_factor,
248 unsigned alpha_factor,
249 const double *factor,
250 const double *src,
251 const double *dst,
252 const double *con,
253 double *term)
254 {
255 double temp;
256
257 switch (rgb_factor) {
258 case PIPE_BLENDFACTOR_ONE:
259 term[0] = factor[0]; /* R */
260 term[1] = factor[1]; /* G */
261 term[2] = factor[2]; /* B */
262 break;
263 case PIPE_BLENDFACTOR_SRC_COLOR:
264 term[0] = factor[0] * src[0]; /* R */
265 term[1] = factor[1] * src[1]; /* G */
266 term[2] = factor[2] * src[2]; /* B */
267 break;
268 case PIPE_BLENDFACTOR_SRC_ALPHA:
269 term[0] = factor[0] * src[3]; /* R */
270 term[1] = factor[1] * src[3]; /* G */
271 term[2] = factor[2] * src[3]; /* B */
272 break;
273 case PIPE_BLENDFACTOR_DST_COLOR:
274 term[0] = factor[0] * dst[0]; /* R */
275 term[1] = factor[1] * dst[1]; /* G */
276 term[2] = factor[2] * dst[2]; /* B */
277 break;
278 case PIPE_BLENDFACTOR_DST_ALPHA:
279 term[0] = factor[0] * dst[3]; /* R */
280 term[1] = factor[1] * dst[3]; /* G */
281 term[2] = factor[2] * dst[3]; /* B */
282 break;
283 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
284 temp = MIN2(src[3], 1.0f - dst[3]);
285 term[0] = factor[0] * temp; /* R */
286 term[1] = factor[1] * temp; /* G */
287 term[2] = factor[2] * temp; /* B */
288 break;
289 case PIPE_BLENDFACTOR_CONST_COLOR:
290 term[0] = factor[0] * con[0]; /* R */
291 term[1] = factor[1] * con[1]; /* G */
292 term[2] = factor[2] * con[2]; /* B */
293 break;
294 case PIPE_BLENDFACTOR_CONST_ALPHA:
295 term[0] = factor[0] * con[3]; /* R */
296 term[1] = factor[1] * con[3]; /* G */
297 term[2] = factor[2] * con[3]; /* B */
298 break;
299 case PIPE_BLENDFACTOR_SRC1_COLOR:
300 assert(0); /* to do */
301 break;
302 case PIPE_BLENDFACTOR_SRC1_ALPHA:
303 assert(0); /* to do */
304 break;
305 case PIPE_BLENDFACTOR_ZERO:
306 term[0] = 0.0f; /* R */
307 term[1] = 0.0f; /* G */
308 term[2] = 0.0f; /* B */
309 break;
310 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
311 term[0] = factor[0] * (1.0f - src[0]); /* R */
312 term[1] = factor[1] * (1.0f - src[1]); /* G */
313 term[2] = factor[2] * (1.0f - src[2]); /* B */
314 break;
315 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
316 term[0] = factor[0] * (1.0f - src[3]); /* R */
317 term[1] = factor[1] * (1.0f - src[3]); /* G */
318 term[2] = factor[2] * (1.0f - src[3]); /* B */
319 break;
320 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
321 term[0] = factor[0] * (1.0f - dst[3]); /* R */
322 term[1] = factor[1] * (1.0f - dst[3]); /* G */
323 term[2] = factor[2] * (1.0f - dst[3]); /* B */
324 break;
325 case PIPE_BLENDFACTOR_INV_DST_COLOR:
326 term[0] = factor[0] * (1.0f - dst[0]); /* R */
327 term[1] = factor[1] * (1.0f - dst[1]); /* G */
328 term[2] = factor[2] * (1.0f - dst[2]); /* B */
329 break;
330 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
331 term[0] = factor[0] * (1.0f - con[0]); /* R */
332 term[1] = factor[1] * (1.0f - con[1]); /* G */
333 term[2] = factor[2] * (1.0f - con[2]); /* B */
334 break;
335 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
336 term[0] = factor[0] * (1.0f - con[3]); /* R */
337 term[1] = factor[1] * (1.0f - con[3]); /* G */
338 term[2] = factor[2] * (1.0f - con[3]); /* B */
339 break;
340 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
341 assert(0); /* to do */
342 break;
343 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
344 assert(0); /* to do */
345 break;
346 default:
347 assert(0);
348 }
349
350 /*
351 * Compute src/first term A
352 */
353 switch (alpha_factor) {
354 case PIPE_BLENDFACTOR_ONE:
355 term[3] = factor[3]; /* A */
356 break;
357 case PIPE_BLENDFACTOR_SRC_COLOR:
358 case PIPE_BLENDFACTOR_SRC_ALPHA:
359 term[3] = factor[3] * src[3]; /* A */
360 break;
361 case PIPE_BLENDFACTOR_DST_COLOR:
362 case PIPE_BLENDFACTOR_DST_ALPHA:
363 term[3] = factor[3] * dst[3]; /* A */
364 break;
365 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
366 term[3] = src[3]; /* A */
367 break;
368 case PIPE_BLENDFACTOR_CONST_COLOR:
369 case PIPE_BLENDFACTOR_CONST_ALPHA:
370 term[3] = factor[3] * con[3]; /* A */
371 break;
372 case PIPE_BLENDFACTOR_ZERO:
373 term[3] = 0.0f; /* A */
374 break;
375 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
376 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
377 term[3] = factor[3] * (1.0f - src[3]); /* A */
378 break;
379 case PIPE_BLENDFACTOR_INV_DST_COLOR:
380 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
381 term[3] = factor[3] * (1.0f - dst[3]); /* A */
382 break;
383 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
384 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
385 term[3] = factor[3] * (1.0f - con[3]);
386 break;
387 default:
388 assert(0);
389 }
390 }
391
392
393 static void
394 compute_blend_ref(const struct pipe_blend_state *blend,
395 const double *src,
396 const double *dst,
397 const double *con,
398 double *res)
399 {
400 double src_term[4];
401 double dst_term[4];
402
403 compute_blend_ref_term(blend->rt[0].rgb_src_factor, blend->rt[0].alpha_src_factor,
404 src, src, dst, con, src_term);
405 compute_blend_ref_term(blend->rt[0].rgb_dst_factor, blend->rt[0].alpha_dst_factor,
406 dst, src, dst, con, dst_term);
407
408 /*
409 * Combine RGB terms
410 */
411 switch (blend->rt[0].rgb_func) {
412 case PIPE_BLEND_ADD:
413 res[0] = src_term[0] + dst_term[0]; /* R */
414 res[1] = src_term[1] + dst_term[1]; /* G */
415 res[2] = src_term[2] + dst_term[2]; /* B */
416 break;
417 case PIPE_BLEND_SUBTRACT:
418 res[0] = src_term[0] - dst_term[0]; /* R */
419 res[1] = src_term[1] - dst_term[1]; /* G */
420 res[2] = src_term[2] - dst_term[2]; /* B */
421 break;
422 case PIPE_BLEND_REVERSE_SUBTRACT:
423 res[0] = dst_term[0] - src_term[0]; /* R */
424 res[1] = dst_term[1] - src_term[1]; /* G */
425 res[2] = dst_term[2] - src_term[2]; /* B */
426 break;
427 case PIPE_BLEND_MIN:
428 res[0] = MIN2(src_term[0], dst_term[0]); /* R */
429 res[1] = MIN2(src_term[1], dst_term[1]); /* G */
430 res[2] = MIN2(src_term[2], dst_term[2]); /* B */
431 break;
432 case PIPE_BLEND_MAX:
433 res[0] = MAX2(src_term[0], dst_term[0]); /* R */
434 res[1] = MAX2(src_term[1], dst_term[1]); /* G */
435 res[2] = MAX2(src_term[2], dst_term[2]); /* B */
436 break;
437 default:
438 assert(0);
439 }
440
441 /*
442 * Combine A terms
443 */
444 switch (blend->rt[0].alpha_func) {
445 case PIPE_BLEND_ADD:
446 res[3] = src_term[3] + dst_term[3]; /* A */
447 break;
448 case PIPE_BLEND_SUBTRACT:
449 res[3] = src_term[3] - dst_term[3]; /* A */
450 break;
451 case PIPE_BLEND_REVERSE_SUBTRACT:
452 res[3] = dst_term[3] - src_term[3]; /* A */
453 break;
454 case PIPE_BLEND_MIN:
455 res[3] = MIN2(src_term[3], dst_term[3]); /* A */
456 break;
457 case PIPE_BLEND_MAX:
458 res[3] = MAX2(src_term[3], dst_term[3]); /* A */
459 break;
460 default:
461 assert(0);
462 }
463 }
464
465
466 PIPE_ALIGN_STACK
467 static boolean
468 test_one(unsigned verbose,
469 FILE *fp,
470 const struct pipe_blend_state *blend,
471 enum vector_mode mode,
472 struct lp_type type)
473 {
474 LLVMModuleRef module = NULL;
475 LLVMValueRef func = NULL;
476 LLVMExecutionEngineRef engine = lp_build_engine;
477 LLVMPassManagerRef pass = NULL;
478 char *error = NULL;
479 blend_test_ptr_t blend_test_ptr;
480 boolean success;
481 const unsigned n = LP_TEST_NUM_SAMPLES;
482 int64_t cycles[LP_TEST_NUM_SAMPLES];
483 double cycles_avg = 0.0;
484 unsigned i, j;
485 void *code;
486
487 if(verbose >= 1)
488 dump_blend_type(stdout, blend, mode, type);
489
490 module = LLVMModuleCreateWithName("test");
491
492 func = add_blend_test(module, blend, mode, type);
493
494 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
495 LLVMDumpModule(module);
496 abort();
497 }
498 LLVMDisposeMessage(error);
499
500 #if 0
501 pass = LLVMCreatePassManager();
502 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
503 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
504 * but there are more on SVN. */
505 LLVMAddConstantPropagationPass(pass);
506 LLVMAddInstructionCombiningPass(pass);
507 LLVMAddPromoteMemoryToRegisterPass(pass);
508 LLVMAddGVNPass(pass);
509 LLVMAddCFGSimplificationPass(pass);
510 LLVMRunPassManager(pass, module);
511 #else
512 (void)pass;
513 #endif
514
515 if(verbose >= 2)
516 LLVMDumpModule(module);
517
518 code = LLVMGetPointerToGlobal(engine, func);
519 blend_test_ptr = voidptr_to_blend_test_ptr_t(code);
520
521 if(verbose >= 2)
522 lp_disassemble(code);
523
524 success = TRUE;
525 for(i = 0; i < n && success; ++i) {
526 if(mode == AoS) {
527 PIPE_ALIGN_VAR(16) uint8_t src[LP_NATIVE_VECTOR_WIDTH/8];
528 PIPE_ALIGN_VAR(16) uint8_t dst[LP_NATIVE_VECTOR_WIDTH/8];
529 PIPE_ALIGN_VAR(16) uint8_t con[LP_NATIVE_VECTOR_WIDTH/8];
530 PIPE_ALIGN_VAR(16) uint8_t res[LP_NATIVE_VECTOR_WIDTH/8];
531 PIPE_ALIGN_VAR(16) uint8_t ref[LP_NATIVE_VECTOR_WIDTH/8];
532 int64_t start_counter = 0;
533 int64_t end_counter = 0;
534
535 random_vec(type, src);
536 random_vec(type, dst);
537 random_vec(type, con);
538
539 {
540 double fsrc[LP_MAX_VECTOR_LENGTH];
541 double fdst[LP_MAX_VECTOR_LENGTH];
542 double fcon[LP_MAX_VECTOR_LENGTH];
543 double fref[LP_MAX_VECTOR_LENGTH];
544
545 read_vec(type, src, fsrc);
546 read_vec(type, dst, fdst);
547 read_vec(type, con, fcon);
548
549 for(j = 0; j < type.length; j += 4)
550 compute_blend_ref(blend, fsrc + j, fdst + j, fcon + j, fref + j);
551
552 write_vec(type, ref, fref);
553 }
554
555 start_counter = rdtsc();
556 blend_test_ptr(src, dst, con, res);
557 end_counter = rdtsc();
558
559 cycles[i] = end_counter - start_counter;
560
561 if(!compare_vec(type, res, ref)) {
562 success = FALSE;
563
564 if(verbose < 1)
565 dump_blend_type(stderr, blend, mode, type);
566 fprintf(stderr, "MISMATCH\n");
567
568 fprintf(stderr, " Src: ");
569 dump_vec(stderr, type, src);
570 fprintf(stderr, "\n");
571
572 fprintf(stderr, " Dst: ");
573 dump_vec(stderr, type, dst);
574 fprintf(stderr, "\n");
575
576 fprintf(stderr, " Con: ");
577 dump_vec(stderr, type, con);
578 fprintf(stderr, "\n");
579
580 fprintf(stderr, " Res: ");
581 dump_vec(stderr, type, res);
582 fprintf(stderr, "\n");
583
584 fprintf(stderr, " Ref: ");
585 dump_vec(stderr, type, ref);
586 fprintf(stderr, "\n");
587 }
588 }
589
590 if(mode == SoA) {
591 const unsigned stride = type.length*type.width/8;
592 PIPE_ALIGN_VAR(16) uint8_t src[4*LP_NATIVE_VECTOR_WIDTH/8];
593 PIPE_ALIGN_VAR(16) uint8_t dst[4*LP_NATIVE_VECTOR_WIDTH/8];
594 PIPE_ALIGN_VAR(16) uint8_t con[4*LP_NATIVE_VECTOR_WIDTH/8];
595 PIPE_ALIGN_VAR(16) uint8_t res[4*LP_NATIVE_VECTOR_WIDTH/8];
596 PIPE_ALIGN_VAR(16) uint8_t ref[4*LP_NATIVE_VECTOR_WIDTH/8];
597 int64_t start_counter = 0;
598 int64_t end_counter = 0;
599 boolean mismatch;
600
601 for(j = 0; j < 4; ++j) {
602 random_vec(type, src + j*stride);
603 random_vec(type, dst + j*stride);
604 random_vec(type, con + j*stride);
605 }
606
607 {
608 double fsrc[4];
609 double fdst[4];
610 double fcon[4];
611 double fref[4];
612 unsigned k;
613
614 for(k = 0; k < type.length; ++k) {
615 for(j = 0; j < 4; ++j) {
616 fsrc[j] = read_elem(type, src + j*stride, k);
617 fdst[j] = read_elem(type, dst + j*stride, k);
618 fcon[j] = read_elem(type, con + j*stride, k);
619 }
620
621 compute_blend_ref(blend, fsrc, fdst, fcon, fref);
622
623 for(j = 0; j < 4; ++j)
624 write_elem(type, ref + j*stride, k, fref[j]);
625 }
626 }
627
628 start_counter = rdtsc();
629 blend_test_ptr(src, dst, con, res);
630 end_counter = rdtsc();
631
632 cycles[i] = end_counter - start_counter;
633
634 mismatch = FALSE;
635 for (j = 0; j < 4; ++j)
636 if(!compare_vec(type, res + j*stride, ref + j*stride))
637 mismatch = TRUE;
638
639 if (mismatch) {
640 success = FALSE;
641
642 if(verbose < 1)
643 dump_blend_type(stderr, blend, mode, type);
644 fprintf(stderr, "MISMATCH\n");
645 for(j = 0; j < 4; ++j) {
646 char channel = "RGBA"[j];
647 fprintf(stderr, " Src%c: ", channel);
648 dump_vec(stderr, type, src + j*stride);
649 fprintf(stderr, "\n");
650
651 fprintf(stderr, " Dst%c: ", channel);
652 dump_vec(stderr, type, dst + j*stride);
653 fprintf(stderr, "\n");
654
655 fprintf(stderr, " Con%c: ", channel);
656 dump_vec(stderr, type, con + j*stride);
657 fprintf(stderr, "\n");
658
659 fprintf(stderr, " Res%c: ", channel);
660 dump_vec(stderr, type, res + j*stride);
661 fprintf(stderr, "\n");
662
663 fprintf(stderr, " Ref%c: ", channel);
664 dump_vec(stderr, type, ref + j*stride);
665 fprintf(stderr, "\n");
666
667 fprintf(stderr, "\n");
668 }
669 }
670 }
671 }
672
673 /*
674 * Unfortunately the output of cycle counter is not very reliable as it comes
675 * -- sometimes we get outliers (due IRQs perhaps?) which are
676 * better removed to avoid random or biased data.
677 */
678 {
679 double sum = 0.0, sum2 = 0.0;
680 double avg, std;
681 unsigned m;
682
683 for(i = 0; i < n; ++i) {
684 sum += cycles[i];
685 sum2 += cycles[i]*cycles[i];
686 }
687
688 avg = sum/n;
689 std = sqrtf((sum2 - n*avg*avg)/n);
690
691 m = 0;
692 sum = 0.0;
693 for(i = 0; i < n; ++i) {
694 if(fabs(cycles[i] - avg) <= 4.0*std) {
695 sum += cycles[i];
696 ++m;
697 }
698 }
699
700 cycles_avg = sum/m;
701
702 }
703
704 if(fp)
705 write_tsv_row(fp, blend, mode, type, cycles_avg, success);
706
707 if (!success) {
708 if(verbose < 2)
709 LLVMDumpModule(module);
710 LLVMWriteBitcodeToFile(module, "blend.bc");
711 fprintf(stderr, "blend.bc written\n");
712 fprintf(stderr, "Invoke as \"llc -o - blend.bc\"\n");
713 abort();
714 }
715
716 LLVMFreeMachineCodeForFunction(engine, func);
717
718 if(pass)
719 LLVMDisposePassManager(pass);
720
721 return success;
722 }
723
724
725 const unsigned
726 blend_factors[] = {
727 PIPE_BLENDFACTOR_ZERO,
728 PIPE_BLENDFACTOR_ONE,
729 PIPE_BLENDFACTOR_SRC_COLOR,
730 PIPE_BLENDFACTOR_SRC_ALPHA,
731 PIPE_BLENDFACTOR_DST_COLOR,
732 PIPE_BLENDFACTOR_DST_ALPHA,
733 PIPE_BLENDFACTOR_CONST_COLOR,
734 PIPE_BLENDFACTOR_CONST_ALPHA,
735 #if 0
736 PIPE_BLENDFACTOR_SRC1_COLOR,
737 PIPE_BLENDFACTOR_SRC1_ALPHA,
738 #endif
739 PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE,
740 PIPE_BLENDFACTOR_INV_SRC_COLOR,
741 PIPE_BLENDFACTOR_INV_SRC_ALPHA,
742 PIPE_BLENDFACTOR_INV_DST_COLOR,
743 PIPE_BLENDFACTOR_INV_DST_ALPHA,
744 PIPE_BLENDFACTOR_INV_CONST_COLOR,
745 PIPE_BLENDFACTOR_INV_CONST_ALPHA,
746 #if 0
747 PIPE_BLENDFACTOR_INV_SRC1_COLOR,
748 PIPE_BLENDFACTOR_INV_SRC1_ALPHA,
749 #endif
750 };
751
752
753 const unsigned
754 blend_funcs[] = {
755 PIPE_BLEND_ADD,
756 PIPE_BLEND_SUBTRACT,
757 PIPE_BLEND_REVERSE_SUBTRACT,
758 PIPE_BLEND_MIN,
759 PIPE_BLEND_MAX
760 };
761
762
763 const struct lp_type blend_types[] = {
764 /* float, fixed, sign, norm, width, len */
765 { TRUE, FALSE, TRUE, FALSE, 32, 4 }, /* f32 x 4 */
766 { FALSE, FALSE, FALSE, TRUE, 8, 16 }, /* u8n x 16 */
767 };
768
769
770 const unsigned num_funcs = sizeof(blend_funcs)/sizeof(blend_funcs[0]);
771 const unsigned num_factors = sizeof(blend_factors)/sizeof(blend_factors[0]);
772 const unsigned num_types = sizeof(blend_types)/sizeof(blend_types[0]);
773
774
775 boolean
776 test_all(unsigned verbose, FILE *fp)
777 {
778 const unsigned *rgb_func;
779 const unsigned *rgb_src_factor;
780 const unsigned *rgb_dst_factor;
781 const unsigned *alpha_func;
782 const unsigned *alpha_src_factor;
783 const unsigned *alpha_dst_factor;
784 struct pipe_blend_state blend;
785 enum vector_mode mode;
786 const struct lp_type *type;
787 boolean success = TRUE;
788
789 for(rgb_func = blend_funcs; rgb_func < &blend_funcs[num_funcs]; ++rgb_func) {
790 for(alpha_func = blend_funcs; alpha_func < &blend_funcs[num_funcs]; ++alpha_func) {
791 for(rgb_src_factor = blend_factors; rgb_src_factor < &blend_factors[num_factors]; ++rgb_src_factor) {
792 for(rgb_dst_factor = blend_factors; rgb_dst_factor <= rgb_src_factor; ++rgb_dst_factor) {
793 for(alpha_src_factor = blend_factors; alpha_src_factor < &blend_factors[num_factors]; ++alpha_src_factor) {
794 for(alpha_dst_factor = blend_factors; alpha_dst_factor <= alpha_src_factor; ++alpha_dst_factor) {
795 for(mode = 0; mode < 2; ++mode) {
796 for(type = blend_types; type < &blend_types[num_types]; ++type) {
797
798 if(*rgb_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
799 *alpha_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
800 continue;
801
802 memset(&blend, 0, sizeof blend);
803 blend.rt[0].blend_enable = 1;
804 blend.rt[0].rgb_func = *rgb_func;
805 blend.rt[0].rgb_src_factor = *rgb_src_factor;
806 blend.rt[0].rgb_dst_factor = *rgb_dst_factor;
807 blend.rt[0].alpha_func = *alpha_func;
808 blend.rt[0].alpha_src_factor = *alpha_src_factor;
809 blend.rt[0].alpha_dst_factor = *alpha_dst_factor;
810 blend.rt[0].colormask = PIPE_MASK_RGBA;
811
812 if(!test_one(verbose, fp, &blend, mode, *type))
813 success = FALSE;
814
815 }
816 }
817 }
818 }
819 }
820 }
821 }
822 }
823
824 return success;
825 }
826
827
828 boolean
829 test_some(unsigned verbose, FILE *fp, unsigned long n)
830 {
831 const unsigned *rgb_func;
832 const unsigned *rgb_src_factor;
833 const unsigned *rgb_dst_factor;
834 const unsigned *alpha_func;
835 const unsigned *alpha_src_factor;
836 const unsigned *alpha_dst_factor;
837 struct pipe_blend_state blend;
838 enum vector_mode mode;
839 const struct lp_type *type;
840 unsigned long i;
841 boolean success = TRUE;
842
843 for(i = 0; i < n; ++i) {
844 rgb_func = &blend_funcs[rand() % num_funcs];
845 alpha_func = &blend_funcs[rand() % num_funcs];
846 rgb_src_factor = &blend_factors[rand() % num_factors];
847 alpha_src_factor = &blend_factors[rand() % num_factors];
848
849 do {
850 rgb_dst_factor = &blend_factors[rand() % num_factors];
851 } while(*rgb_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE);
852
853 do {
854 alpha_dst_factor = &blend_factors[rand() % num_factors];
855 } while(*alpha_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE);
856
857 mode = rand() & 1;
858
859 type = &blend_types[rand() % num_types];
860
861 memset(&blend, 0, sizeof blend);
862 blend.rt[0].blend_enable = 1;
863 blend.rt[0].rgb_func = *rgb_func;
864 blend.rt[0].rgb_src_factor = *rgb_src_factor;
865 blend.rt[0].rgb_dst_factor = *rgb_dst_factor;
866 blend.rt[0].alpha_func = *alpha_func;
867 blend.rt[0].alpha_src_factor = *alpha_src_factor;
868 blend.rt[0].alpha_dst_factor = *alpha_dst_factor;
869 blend.rt[0].colormask = PIPE_MASK_RGBA;
870
871 if(!test_one(verbose, fp, &blend, mode, *type))
872 success = FALSE;
873 }
874
875 return success;
876 }
877
878
879 boolean
880 test_single(unsigned verbose, FILE *fp)
881 {
882 printf("no test_single()");
883 return TRUE;
884 }