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