Merge remote branch 'origin/master' into nvc0-new
[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(struct gallivm_state *gallivm,
167 const struct pipe_blend_state *blend,
168 enum vector_mode mode,
169 struct lp_type type)
170 {
171 LLVMModuleRef module = gallivm->module;
172 LLVMContextRef context = gallivm->context;
173 LLVMTypeRef vec_type;
174 LLVMTypeRef args[4];
175 LLVMValueRef func;
176 LLVMValueRef src_ptr;
177 LLVMValueRef dst_ptr;
178 LLVMValueRef const_ptr;
179 LLVMValueRef res_ptr;
180 LLVMBasicBlockRef block;
181 LLVMBuilderRef builder;
182 const unsigned rt = 0;
183
184 vec_type = lp_build_vec_type(gallivm, type);
185
186 args[3] = args[2] = args[1] = args[0] = LLVMPointerType(vec_type, 0);
187 func = LLVMAddFunction(module, "test", LLVMFunctionType(LLVMVoidTypeInContext(context), args, 4, 0));
188 LLVMSetFunctionCallConv(func, LLVMCCallConv);
189 src_ptr = LLVMGetParam(func, 0);
190 dst_ptr = LLVMGetParam(func, 1);
191 const_ptr = LLVMGetParam(func, 2);
192 res_ptr = LLVMGetParam(func, 3);
193
194 block = LLVMAppendBasicBlockInContext(context, func, "entry");
195 builder = gallivm->builder;
196 LLVMPositionBuilderAtEnd(builder, block);
197
198 if (mode == AoS) {
199 LLVMValueRef src;
200 LLVMValueRef dst;
201 LLVMValueRef con;
202 LLVMValueRef res;
203
204 src = LLVMBuildLoad(builder, src_ptr, "src");
205 dst = LLVMBuildLoad(builder, dst_ptr, "dst");
206 con = LLVMBuildLoad(builder, const_ptr, "const");
207
208 res = lp_build_blend_aos(gallivm, blend, type, rt, src, dst, con, 3);
209
210 lp_build_name(res, "res");
211
212 LLVMBuildStore(builder, res, res_ptr);
213 }
214
215 if (mode == SoA) {
216 LLVMValueRef src[4];
217 LLVMValueRef dst[4];
218 LLVMValueRef con[4];
219 LLVMValueRef res[4];
220 unsigned i;
221
222 for(i = 0; i < 4; ++i) {
223 LLVMValueRef index = LLVMConstInt(LLVMInt32TypeInContext(context), i, 0);
224 src[i] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, src_ptr, &index, 1, ""), "");
225 dst[i] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dst_ptr, &index, 1, ""), "");
226 con[i] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, const_ptr, &index, 1, ""), "");
227 lp_build_name(src[i], "src.%c", "rgba"[i]);
228 lp_build_name(con[i], "con.%c", "rgba"[i]);
229 lp_build_name(dst[i], "dst.%c", "rgba"[i]);
230 }
231
232 lp_build_blend_soa(gallivm, blend, type, rt, src, dst, con, res);
233
234 for(i = 0; i < 4; ++i) {
235 LLVMValueRef index = LLVMConstInt(LLVMInt32TypeInContext(context), i, 0);
236 lp_build_name(res[i], "res.%c", "rgba"[i]);
237 LLVMBuildStore(builder, res[i], LLVMBuildGEP(builder, res_ptr, &index, 1, ""));
238 }
239 }
240
241 LLVMBuildRetVoid(builder);;
242
243 return func;
244 }
245
246
247 static void
248 compute_blend_ref_term(unsigned rgb_factor,
249 unsigned alpha_factor,
250 const double *factor,
251 const double *src,
252 const double *dst,
253 const double *con,
254 double *term)
255 {
256 double temp;
257
258 switch (rgb_factor) {
259 case PIPE_BLENDFACTOR_ONE:
260 term[0] = factor[0]; /* R */
261 term[1] = factor[1]; /* G */
262 term[2] = factor[2]; /* B */
263 break;
264 case PIPE_BLENDFACTOR_SRC_COLOR:
265 term[0] = factor[0] * src[0]; /* R */
266 term[1] = factor[1] * src[1]; /* G */
267 term[2] = factor[2] * src[2]; /* B */
268 break;
269 case PIPE_BLENDFACTOR_SRC_ALPHA:
270 term[0] = factor[0] * src[3]; /* R */
271 term[1] = factor[1] * src[3]; /* G */
272 term[2] = factor[2] * src[3]; /* B */
273 break;
274 case PIPE_BLENDFACTOR_DST_COLOR:
275 term[0] = factor[0] * dst[0]; /* R */
276 term[1] = factor[1] * dst[1]; /* G */
277 term[2] = factor[2] * dst[2]; /* B */
278 break;
279 case PIPE_BLENDFACTOR_DST_ALPHA:
280 term[0] = factor[0] * dst[3]; /* R */
281 term[1] = factor[1] * dst[3]; /* G */
282 term[2] = factor[2] * dst[3]; /* B */
283 break;
284 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
285 temp = MIN2(src[3], 1.0f - dst[3]);
286 term[0] = factor[0] * temp; /* R */
287 term[1] = factor[1] * temp; /* G */
288 term[2] = factor[2] * temp; /* B */
289 break;
290 case PIPE_BLENDFACTOR_CONST_COLOR:
291 term[0] = factor[0] * con[0]; /* R */
292 term[1] = factor[1] * con[1]; /* G */
293 term[2] = factor[2] * con[2]; /* B */
294 break;
295 case PIPE_BLENDFACTOR_CONST_ALPHA:
296 term[0] = factor[0] * con[3]; /* R */
297 term[1] = factor[1] * con[3]; /* G */
298 term[2] = factor[2] * con[3]; /* B */
299 break;
300 case PIPE_BLENDFACTOR_SRC1_COLOR:
301 assert(0); /* to do */
302 break;
303 case PIPE_BLENDFACTOR_SRC1_ALPHA:
304 assert(0); /* to do */
305 break;
306 case PIPE_BLENDFACTOR_ZERO:
307 term[0] = 0.0f; /* R */
308 term[1] = 0.0f; /* G */
309 term[2] = 0.0f; /* B */
310 break;
311 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
312 term[0] = factor[0] * (1.0f - src[0]); /* R */
313 term[1] = factor[1] * (1.0f - src[1]); /* G */
314 term[2] = factor[2] * (1.0f - src[2]); /* B */
315 break;
316 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
317 term[0] = factor[0] * (1.0f - src[3]); /* R */
318 term[1] = factor[1] * (1.0f - src[3]); /* G */
319 term[2] = factor[2] * (1.0f - src[3]); /* B */
320 break;
321 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
322 term[0] = factor[0] * (1.0f - dst[3]); /* R */
323 term[1] = factor[1] * (1.0f - dst[3]); /* G */
324 term[2] = factor[2] * (1.0f - dst[3]); /* B */
325 break;
326 case PIPE_BLENDFACTOR_INV_DST_COLOR:
327 term[0] = factor[0] * (1.0f - dst[0]); /* R */
328 term[1] = factor[1] * (1.0f - dst[1]); /* G */
329 term[2] = factor[2] * (1.0f - dst[2]); /* B */
330 break;
331 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
332 term[0] = factor[0] * (1.0f - con[0]); /* R */
333 term[1] = factor[1] * (1.0f - con[1]); /* G */
334 term[2] = factor[2] * (1.0f - con[2]); /* B */
335 break;
336 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
337 term[0] = factor[0] * (1.0f - con[3]); /* R */
338 term[1] = factor[1] * (1.0f - con[3]); /* G */
339 term[2] = factor[2] * (1.0f - con[3]); /* B */
340 break;
341 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
342 assert(0); /* to do */
343 break;
344 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
345 assert(0); /* to do */
346 break;
347 default:
348 assert(0);
349 }
350
351 /*
352 * Compute src/first term A
353 */
354 switch (alpha_factor) {
355 case PIPE_BLENDFACTOR_ONE:
356 term[3] = factor[3]; /* A */
357 break;
358 case PIPE_BLENDFACTOR_SRC_COLOR:
359 case PIPE_BLENDFACTOR_SRC_ALPHA:
360 term[3] = factor[3] * src[3]; /* A */
361 break;
362 case PIPE_BLENDFACTOR_DST_COLOR:
363 case PIPE_BLENDFACTOR_DST_ALPHA:
364 term[3] = factor[3] * dst[3]; /* A */
365 break;
366 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
367 term[3] = src[3]; /* A */
368 break;
369 case PIPE_BLENDFACTOR_CONST_COLOR:
370 case PIPE_BLENDFACTOR_CONST_ALPHA:
371 term[3] = factor[3] * con[3]; /* A */
372 break;
373 case PIPE_BLENDFACTOR_ZERO:
374 term[3] = 0.0f; /* A */
375 break;
376 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
377 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
378 term[3] = factor[3] * (1.0f - src[3]); /* A */
379 break;
380 case PIPE_BLENDFACTOR_INV_DST_COLOR:
381 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
382 term[3] = factor[3] * (1.0f - dst[3]); /* A */
383 break;
384 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
385 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
386 term[3] = factor[3] * (1.0f - con[3]);
387 break;
388 default:
389 assert(0);
390 }
391 }
392
393
394 static void
395 compute_blend_ref(const struct pipe_blend_state *blend,
396 const double *src,
397 const double *dst,
398 const double *con,
399 double *res)
400 {
401 double src_term[4];
402 double dst_term[4];
403
404 compute_blend_ref_term(blend->rt[0].rgb_src_factor, blend->rt[0].alpha_src_factor,
405 src, src, dst, con, src_term);
406 compute_blend_ref_term(blend->rt[0].rgb_dst_factor, blend->rt[0].alpha_dst_factor,
407 dst, src, dst, con, dst_term);
408
409 /*
410 * Combine RGB terms
411 */
412 switch (blend->rt[0].rgb_func) {
413 case PIPE_BLEND_ADD:
414 res[0] = src_term[0] + dst_term[0]; /* R */
415 res[1] = src_term[1] + dst_term[1]; /* G */
416 res[2] = src_term[2] + dst_term[2]; /* B */
417 break;
418 case PIPE_BLEND_SUBTRACT:
419 res[0] = src_term[0] - dst_term[0]; /* R */
420 res[1] = src_term[1] - dst_term[1]; /* G */
421 res[2] = src_term[2] - dst_term[2]; /* B */
422 break;
423 case PIPE_BLEND_REVERSE_SUBTRACT:
424 res[0] = dst_term[0] - src_term[0]; /* R */
425 res[1] = dst_term[1] - src_term[1]; /* G */
426 res[2] = dst_term[2] - src_term[2]; /* B */
427 break;
428 case PIPE_BLEND_MIN:
429 res[0] = MIN2(src_term[0], dst_term[0]); /* R */
430 res[1] = MIN2(src_term[1], dst_term[1]); /* G */
431 res[2] = MIN2(src_term[2], dst_term[2]); /* B */
432 break;
433 case PIPE_BLEND_MAX:
434 res[0] = MAX2(src_term[0], dst_term[0]); /* R */
435 res[1] = MAX2(src_term[1], dst_term[1]); /* G */
436 res[2] = MAX2(src_term[2], dst_term[2]); /* B */
437 break;
438 default:
439 assert(0);
440 }
441
442 /*
443 * Combine A terms
444 */
445 switch (blend->rt[0].alpha_func) {
446 case PIPE_BLEND_ADD:
447 res[3] = src_term[3] + dst_term[3]; /* A */
448 break;
449 case PIPE_BLEND_SUBTRACT:
450 res[3] = src_term[3] - dst_term[3]; /* A */
451 break;
452 case PIPE_BLEND_REVERSE_SUBTRACT:
453 res[3] = dst_term[3] - src_term[3]; /* A */
454 break;
455 case PIPE_BLEND_MIN:
456 res[3] = MIN2(src_term[3], dst_term[3]); /* A */
457 break;
458 case PIPE_BLEND_MAX:
459 res[3] = MAX2(src_term[3], dst_term[3]); /* A */
460 break;
461 default:
462 assert(0);
463 }
464 }
465
466
467 PIPE_ALIGN_STACK
468 static boolean
469 test_one(struct gallivm_state *gallivm,
470 unsigned verbose,
471 FILE *fp,
472 const struct pipe_blend_state *blend,
473 enum vector_mode mode,
474 struct lp_type type)
475 {
476 LLVMModuleRef module = gallivm->module;
477 LLVMValueRef func = NULL;
478 LLVMExecutionEngineRef engine = gallivm->engine;
479 char *error = NULL;
480 blend_test_ptr_t blend_test_ptr;
481 boolean success;
482 const unsigned n = LP_TEST_NUM_SAMPLES;
483 int64_t cycles[LP_TEST_NUM_SAMPLES];
484 double cycles_avg = 0.0;
485 unsigned i, j;
486 void *code;
487
488 if(verbose >= 1)
489 dump_blend_type(stdout, blend, mode, type);
490
491 func = add_blend_test(gallivm, blend, mode, type);
492
493 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
494 LLVMDumpModule(module);
495 abort();
496 }
497 LLVMDisposeMessage(error);
498
499 code = LLVMGetPointerToGlobal(engine, func);
500 blend_test_ptr = voidptr_to_blend_test_ptr_t(code);
501
502 if(verbose >= 2)
503 lp_disassemble(code);
504
505 success = TRUE;
506 for(i = 0; i < n && success; ++i) {
507 if(mode == AoS) {
508 PIPE_ALIGN_VAR(16) uint8_t src[LP_NATIVE_VECTOR_WIDTH/8];
509 PIPE_ALIGN_VAR(16) uint8_t dst[LP_NATIVE_VECTOR_WIDTH/8];
510 PIPE_ALIGN_VAR(16) uint8_t con[LP_NATIVE_VECTOR_WIDTH/8];
511 PIPE_ALIGN_VAR(16) uint8_t res[LP_NATIVE_VECTOR_WIDTH/8];
512 PIPE_ALIGN_VAR(16) uint8_t ref[LP_NATIVE_VECTOR_WIDTH/8];
513 int64_t start_counter = 0;
514 int64_t end_counter = 0;
515
516 random_vec(type, src);
517 random_vec(type, dst);
518 random_vec(type, con);
519
520 {
521 double fsrc[LP_MAX_VECTOR_LENGTH];
522 double fdst[LP_MAX_VECTOR_LENGTH];
523 double fcon[LP_MAX_VECTOR_LENGTH];
524 double fref[LP_MAX_VECTOR_LENGTH];
525
526 read_vec(type, src, fsrc);
527 read_vec(type, dst, fdst);
528 read_vec(type, con, fcon);
529
530 for(j = 0; j < type.length; j += 4)
531 compute_blend_ref(blend, fsrc + j, fdst + j, fcon + j, fref + j);
532
533 write_vec(type, ref, fref);
534 }
535
536 start_counter = rdtsc();
537 blend_test_ptr(src, dst, con, res);
538 end_counter = rdtsc();
539
540 cycles[i] = end_counter - start_counter;
541
542 if(!compare_vec(type, res, ref)) {
543 success = FALSE;
544
545 if(verbose < 1)
546 dump_blend_type(stderr, blend, mode, type);
547 fprintf(stderr, "MISMATCH\n");
548
549 fprintf(stderr, " Src: ");
550 dump_vec(stderr, type, src);
551 fprintf(stderr, "\n");
552
553 fprintf(stderr, " Dst: ");
554 dump_vec(stderr, type, dst);
555 fprintf(stderr, "\n");
556
557 fprintf(stderr, " Con: ");
558 dump_vec(stderr, type, con);
559 fprintf(stderr, "\n");
560
561 fprintf(stderr, " Res: ");
562 dump_vec(stderr, type, res);
563 fprintf(stderr, "\n");
564
565 fprintf(stderr, " Ref: ");
566 dump_vec(stderr, type, ref);
567 fprintf(stderr, "\n");
568 }
569 }
570
571 if(mode == SoA) {
572 const unsigned stride = type.length*type.width/8;
573 PIPE_ALIGN_VAR(16) uint8_t src[4*LP_NATIVE_VECTOR_WIDTH/8];
574 PIPE_ALIGN_VAR(16) uint8_t dst[4*LP_NATIVE_VECTOR_WIDTH/8];
575 PIPE_ALIGN_VAR(16) uint8_t con[4*LP_NATIVE_VECTOR_WIDTH/8];
576 PIPE_ALIGN_VAR(16) uint8_t res[4*LP_NATIVE_VECTOR_WIDTH/8];
577 PIPE_ALIGN_VAR(16) uint8_t ref[4*LP_NATIVE_VECTOR_WIDTH/8];
578 int64_t start_counter = 0;
579 int64_t end_counter = 0;
580 boolean mismatch;
581
582 for(j = 0; j < 4; ++j) {
583 random_vec(type, src + j*stride);
584 random_vec(type, dst + j*stride);
585 random_vec(type, con + j*stride);
586 }
587
588 {
589 double fsrc[4];
590 double fdst[4];
591 double fcon[4];
592 double fref[4];
593 unsigned k;
594
595 for(k = 0; k < type.length; ++k) {
596 for(j = 0; j < 4; ++j) {
597 fsrc[j] = read_elem(type, src + j*stride, k);
598 fdst[j] = read_elem(type, dst + j*stride, k);
599 fcon[j] = read_elem(type, con + j*stride, k);
600 }
601
602 compute_blend_ref(blend, fsrc, fdst, fcon, fref);
603
604 for(j = 0; j < 4; ++j)
605 write_elem(type, ref + j*stride, k, fref[j]);
606 }
607 }
608
609 start_counter = rdtsc();
610 blend_test_ptr(src, dst, con, res);
611 end_counter = rdtsc();
612
613 cycles[i] = end_counter - start_counter;
614
615 mismatch = FALSE;
616 for (j = 0; j < 4; ++j)
617 if(!compare_vec(type, res + j*stride, ref + j*stride))
618 mismatch = TRUE;
619
620 if (mismatch) {
621 success = FALSE;
622
623 if(verbose < 1)
624 dump_blend_type(stderr, blend, mode, type);
625 fprintf(stderr, "MISMATCH\n");
626 for(j = 0; j < 4; ++j) {
627 char channel = "RGBA"[j];
628 fprintf(stderr, " Src%c: ", channel);
629 dump_vec(stderr, type, src + j*stride);
630 fprintf(stderr, "\n");
631
632 fprintf(stderr, " Dst%c: ", channel);
633 dump_vec(stderr, type, dst + j*stride);
634 fprintf(stderr, "\n");
635
636 fprintf(stderr, " Con%c: ", channel);
637 dump_vec(stderr, type, con + j*stride);
638 fprintf(stderr, "\n");
639
640 fprintf(stderr, " Res%c: ", channel);
641 dump_vec(stderr, type, res + j*stride);
642 fprintf(stderr, "\n");
643
644 fprintf(stderr, " Ref%c: ", channel);
645 dump_vec(stderr, type, ref + j*stride);
646 fprintf(stderr, "\n");
647
648 fprintf(stderr, "\n");
649 }
650 }
651 }
652 }
653
654 /*
655 * Unfortunately the output of cycle counter is not very reliable as it comes
656 * -- sometimes we get outliers (due IRQs perhaps?) which are
657 * better removed to avoid random or biased data.
658 */
659 {
660 double sum = 0.0, sum2 = 0.0;
661 double avg, std;
662 unsigned m;
663
664 for(i = 0; i < n; ++i) {
665 sum += cycles[i];
666 sum2 += cycles[i]*cycles[i];
667 }
668
669 avg = sum/n;
670 std = sqrtf((sum2 - n*avg*avg)/n);
671
672 m = 0;
673 sum = 0.0;
674 for(i = 0; i < n; ++i) {
675 if(fabs(cycles[i] - avg) <= 4.0*std) {
676 sum += cycles[i];
677 ++m;
678 }
679 }
680
681 cycles_avg = sum/m;
682
683 }
684
685 if(fp)
686 write_tsv_row(fp, blend, mode, type, cycles_avg, success);
687
688 if (!success) {
689 if(verbose < 2)
690 LLVMDumpModule(module);
691 LLVMWriteBitcodeToFile(module, "blend.bc");
692 fprintf(stderr, "blend.bc written\n");
693 fprintf(stderr, "Invoke as \"llc -o - blend.bc\"\n");
694 abort();
695 }
696
697 LLVMFreeMachineCodeForFunction(engine, func);
698
699 return success;
700 }
701
702
703 const unsigned
704 blend_factors[] = {
705 PIPE_BLENDFACTOR_ZERO,
706 PIPE_BLENDFACTOR_ONE,
707 PIPE_BLENDFACTOR_SRC_COLOR,
708 PIPE_BLENDFACTOR_SRC_ALPHA,
709 PIPE_BLENDFACTOR_DST_COLOR,
710 PIPE_BLENDFACTOR_DST_ALPHA,
711 PIPE_BLENDFACTOR_CONST_COLOR,
712 PIPE_BLENDFACTOR_CONST_ALPHA,
713 #if 0
714 PIPE_BLENDFACTOR_SRC1_COLOR,
715 PIPE_BLENDFACTOR_SRC1_ALPHA,
716 #endif
717 PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE,
718 PIPE_BLENDFACTOR_INV_SRC_COLOR,
719 PIPE_BLENDFACTOR_INV_SRC_ALPHA,
720 PIPE_BLENDFACTOR_INV_DST_COLOR,
721 PIPE_BLENDFACTOR_INV_DST_ALPHA,
722 PIPE_BLENDFACTOR_INV_CONST_COLOR,
723 PIPE_BLENDFACTOR_INV_CONST_ALPHA,
724 #if 0
725 PIPE_BLENDFACTOR_INV_SRC1_COLOR,
726 PIPE_BLENDFACTOR_INV_SRC1_ALPHA,
727 #endif
728 };
729
730
731 const unsigned
732 blend_funcs[] = {
733 PIPE_BLEND_ADD,
734 PIPE_BLEND_SUBTRACT,
735 PIPE_BLEND_REVERSE_SUBTRACT,
736 PIPE_BLEND_MIN,
737 PIPE_BLEND_MAX
738 };
739
740
741 const struct lp_type blend_types[] = {
742 /* float, fixed, sign, norm, width, len */
743 { TRUE, FALSE, TRUE, FALSE, 32, 4 }, /* f32 x 4 */
744 { FALSE, FALSE, FALSE, TRUE, 8, 16 }, /* u8n x 16 */
745 };
746
747
748 const unsigned num_funcs = sizeof(blend_funcs)/sizeof(blend_funcs[0]);
749 const unsigned num_factors = sizeof(blend_factors)/sizeof(blend_factors[0]);
750 const unsigned num_types = sizeof(blend_types)/sizeof(blend_types[0]);
751
752
753 boolean
754 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
755 {
756 const unsigned *rgb_func;
757 const unsigned *rgb_src_factor;
758 const unsigned *rgb_dst_factor;
759 const unsigned *alpha_func;
760 const unsigned *alpha_src_factor;
761 const unsigned *alpha_dst_factor;
762 struct pipe_blend_state blend;
763 enum vector_mode mode;
764 const struct lp_type *type;
765 boolean success = TRUE;
766
767 for(rgb_func = blend_funcs; rgb_func < &blend_funcs[num_funcs]; ++rgb_func) {
768 for(alpha_func = blend_funcs; alpha_func < &blend_funcs[num_funcs]; ++alpha_func) {
769 for(rgb_src_factor = blend_factors; rgb_src_factor < &blend_factors[num_factors]; ++rgb_src_factor) {
770 for(rgb_dst_factor = blend_factors; rgb_dst_factor <= rgb_src_factor; ++rgb_dst_factor) {
771 for(alpha_src_factor = blend_factors; alpha_src_factor < &blend_factors[num_factors]; ++alpha_src_factor) {
772 for(alpha_dst_factor = blend_factors; alpha_dst_factor <= alpha_src_factor; ++alpha_dst_factor) {
773 for(mode = 0; mode < 2; ++mode) {
774 for(type = blend_types; type < &blend_types[num_types]; ++type) {
775
776 if(*rgb_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
777 *alpha_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
778 continue;
779
780 memset(&blend, 0, sizeof blend);
781 blend.rt[0].blend_enable = 1;
782 blend.rt[0].rgb_func = *rgb_func;
783 blend.rt[0].rgb_src_factor = *rgb_src_factor;
784 blend.rt[0].rgb_dst_factor = *rgb_dst_factor;
785 blend.rt[0].alpha_func = *alpha_func;
786 blend.rt[0].alpha_src_factor = *alpha_src_factor;
787 blend.rt[0].alpha_dst_factor = *alpha_dst_factor;
788 blend.rt[0].colormask = PIPE_MASK_RGBA;
789
790 if(!test_one(gallivm, verbose, fp, &blend, mode, *type))
791 success = FALSE;
792
793 }
794 }
795 }
796 }
797 }
798 }
799 }
800 }
801
802 return success;
803 }
804
805
806 boolean
807 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
808 unsigned long n)
809 {
810 const unsigned *rgb_func;
811 const unsigned *rgb_src_factor;
812 const unsigned *rgb_dst_factor;
813 const unsigned *alpha_func;
814 const unsigned *alpha_src_factor;
815 const unsigned *alpha_dst_factor;
816 struct pipe_blend_state blend;
817 enum vector_mode mode;
818 const struct lp_type *type;
819 unsigned long i;
820 boolean success = TRUE;
821
822 for(i = 0; i < n; ++i) {
823 rgb_func = &blend_funcs[rand() % num_funcs];
824 alpha_func = &blend_funcs[rand() % num_funcs];
825 rgb_src_factor = &blend_factors[rand() % num_factors];
826 alpha_src_factor = &blend_factors[rand() % num_factors];
827
828 do {
829 rgb_dst_factor = &blend_factors[rand() % num_factors];
830 } while(*rgb_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE);
831
832 do {
833 alpha_dst_factor = &blend_factors[rand() % num_factors];
834 } while(*alpha_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE);
835
836 mode = rand() & 1;
837
838 type = &blend_types[rand() % num_types];
839
840 memset(&blend, 0, sizeof blend);
841 blend.rt[0].blend_enable = 1;
842 blend.rt[0].rgb_func = *rgb_func;
843 blend.rt[0].rgb_src_factor = *rgb_src_factor;
844 blend.rt[0].rgb_dst_factor = *rgb_dst_factor;
845 blend.rt[0].alpha_func = *alpha_func;
846 blend.rt[0].alpha_src_factor = *alpha_src_factor;
847 blend.rt[0].alpha_dst_factor = *alpha_dst_factor;
848 blend.rt[0].colormask = PIPE_MASK_RGBA;
849
850 if(!test_one(gallivm, verbose, fp, &blend, mode, *type))
851 success = FALSE;
852 }
853
854 return success;
855 }
856
857
858 boolean
859 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
860 {
861 printf("no test_single()");
862 return TRUE;
863 }