Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_tgsi_soa.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * TGSI to LLVM IR translation -- SoA.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 *
35 * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
36 * Brian Paul, and others.
37 */
38
39 #include "pipe/p_config.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "util/u_debug.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "tgsi/tgsi_dump.h"
45 #include "tgsi/tgsi_info.h"
46 #include "tgsi/tgsi_parse.h"
47 #include "tgsi/tgsi_util.h"
48 #include "tgsi/tgsi_exec.h"
49 #include "lp_bld_type.h"
50 #include "lp_bld_const.h"
51 #include "lp_bld_arit.h"
52 #include "lp_bld_logic.h"
53 #include "lp_bld_swizzle.h"
54 #include "lp_bld_flow.h"
55 #include "lp_bld_tgsi.h"
56 #include "lp_bld_debug.h"
57
58
59 #define LP_MAX_TEMPS 256
60 #define LP_MAX_IMMEDIATES 256
61
62
63 #define FOR_EACH_CHANNEL( CHAN )\
64 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
65
66 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
67 ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
68
69 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
70 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
71
72 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
73 FOR_EACH_CHANNEL( CHAN )\
74 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
75
76 #define CHAN_X 0
77 #define CHAN_Y 1
78 #define CHAN_Z 2
79 #define CHAN_W 3
80
81 #define QUAD_TOP_LEFT 0
82 #define QUAD_TOP_RIGHT 1
83 #define QUAD_BOTTOM_LEFT 2
84 #define QUAD_BOTTOM_RIGHT 3
85
86 #define LP_TGSI_MAX_NESTING 16
87
88 struct lp_exec_mask {
89 struct lp_build_context *bld;
90
91 boolean has_mask;
92
93 LLVMTypeRef int_vec_type;
94
95 LLVMValueRef cond_stack[LP_TGSI_MAX_NESTING];
96 int cond_stack_size;
97 LLVMValueRef cond_mask;
98
99 LLVMValueRef break_stack[LP_TGSI_MAX_NESTING];
100 int break_stack_size;
101 LLVMValueRef break_mask;
102
103 LLVMValueRef cont_stack[LP_TGSI_MAX_NESTING];
104 int cont_stack_size;
105 LLVMValueRef cont_mask;
106
107 LLVMBasicBlockRef loop_stack[LP_TGSI_MAX_NESTING];
108 int loop_stack_size;
109 LLVMBasicBlockRef loop_block;
110
111
112 LLVMValueRef exec_mask;
113 };
114
115 struct lp_build_tgsi_soa_context
116 {
117 struct lp_build_context base;
118
119 LLVMValueRef consts_ptr;
120 const LLVMValueRef *pos;
121 const LLVMValueRef (*inputs)[NUM_CHANNELS];
122 LLVMValueRef (*outputs)[NUM_CHANNELS];
123
124 struct lp_build_sampler_soa *sampler;
125
126 LLVMValueRef immediates[LP_MAX_IMMEDIATES][NUM_CHANNELS];
127 LLVMValueRef temps[LP_MAX_TEMPS][NUM_CHANNELS];
128
129 struct lp_build_mask_context *mask;
130 struct lp_exec_mask exec_mask;
131 };
132
133 static const unsigned char
134 swizzle_left[4] = {
135 QUAD_TOP_LEFT, QUAD_TOP_LEFT,
136 QUAD_BOTTOM_LEFT, QUAD_BOTTOM_LEFT
137 };
138
139 static const unsigned char
140 swizzle_right[4] = {
141 QUAD_TOP_RIGHT, QUAD_TOP_RIGHT,
142 QUAD_BOTTOM_RIGHT, QUAD_BOTTOM_RIGHT
143 };
144
145 static const unsigned char
146 swizzle_top[4] = {
147 QUAD_TOP_LEFT, QUAD_TOP_RIGHT,
148 QUAD_TOP_LEFT, QUAD_TOP_RIGHT
149 };
150
151 static const unsigned char
152 swizzle_bottom[4] = {
153 QUAD_BOTTOM_LEFT, QUAD_BOTTOM_RIGHT,
154 QUAD_BOTTOM_LEFT, QUAD_BOTTOM_RIGHT
155 };
156
157 static void lp_exec_mask_init(struct lp_exec_mask *mask, struct lp_build_context *bld)
158 {
159 mask->bld = bld;
160 mask->has_mask = FALSE;
161 mask->cond_stack_size = 0;
162 mask->loop_stack_size = 0;
163 mask->break_stack_size = 0;
164 mask->cont_stack_size = 0;
165
166 mask->int_vec_type = lp_build_int_vec_type(mask->bld->type);
167 }
168
169 static void lp_exec_mask_update(struct lp_exec_mask *mask)
170 {
171 if (mask->loop_stack_size) {
172 /*for loops we need to update the entire mask at
173 * runtime */
174 LLVMValueRef tmp;
175 tmp = LLVMBuildAnd(mask->bld->builder,
176 mask->cont_mask,
177 mask->break_mask,
178 "maskcb");
179 mask->exec_mask = LLVMBuildAnd(mask->bld->builder,
180 mask->cond_mask,
181 tmp,
182 "maskfull");
183 } else
184 mask->exec_mask = mask->cond_mask;
185
186
187 mask->has_mask = (mask->cond_stack_size > 0 ||
188 mask->loop_stack_size > 0);
189 }
190
191 static void lp_exec_mask_cond_push(struct lp_exec_mask *mask,
192 LLVMValueRef val)
193 {
194 mask->cond_stack[mask->cond_stack_size++] = mask->cond_mask;
195 mask->cond_mask = LLVMBuildBitCast(mask->bld->builder, val,
196 mask->int_vec_type, "");
197
198 lp_exec_mask_update(mask);
199 }
200
201 static void lp_exec_mask_cond_invert(struct lp_exec_mask *mask)
202 {
203 LLVMValueRef prev_mask = mask->cond_stack[mask->cond_stack_size - 1];
204 LLVMValueRef inv_mask = LLVMBuildNot(mask->bld->builder,
205 mask->cond_mask, "");
206
207 /* means that we didn't have any mask before and that
208 * we were fully enabled */
209 if (mask->cond_stack_size <= 1) {
210 prev_mask = LLVMConstAllOnes(mask->int_vec_type);
211 }
212
213 mask->cond_mask = LLVMBuildAnd(mask->bld->builder,
214 inv_mask,
215 prev_mask, "");
216 lp_exec_mask_update(mask);
217 }
218
219 static void lp_exec_mask_cond_pop(struct lp_exec_mask *mask)
220 {
221 mask->cond_mask = mask->cond_stack[--mask->cond_stack_size];
222 lp_exec_mask_update(mask);
223 }
224
225 static void lp_exec_bgnloop(struct lp_exec_mask *mask)
226 {
227
228 if (mask->cont_stack_size == 0)
229 mask->cont_mask = LLVMConstAllOnes(mask->int_vec_type);
230 if (mask->cont_stack_size == 0)
231 mask->break_mask = LLVMConstAllOnes(mask->int_vec_type);
232 if (mask->cond_stack_size == 0)
233 mask->cond_mask = LLVMConstAllOnes(mask->int_vec_type);
234 mask->loop_stack[mask->loop_stack_size++] = mask->loop_block;
235 mask->loop_block = lp_build_insert_new_block(mask->bld->builder, "bgnloop");
236 LLVMBuildBr(mask->bld->builder, mask->loop_block);
237 LLVMPositionBuilderAtEnd(mask->bld->builder, mask->loop_block);
238
239 lp_exec_mask_update(mask);
240 }
241
242 static void lp_exec_break(struct lp_exec_mask *mask)
243 {
244 LLVMValueRef exec_mask = LLVMBuildNot(mask->bld->builder,
245 mask->exec_mask,
246 "break");
247
248 mask->break_stack[mask->break_stack_size++] = mask->break_mask;
249 if (mask->break_stack_size > 1) {
250 mask->break_mask = LLVMBuildAnd(mask->bld->builder,
251 mask->break_mask,
252 exec_mask, "break_full");
253 } else
254 mask->break_mask = exec_mask;
255
256 lp_exec_mask_update(mask);
257 }
258
259 static void lp_exec_continue(struct lp_exec_mask *mask)
260 {
261 LLVMValueRef exec_mask = LLVMBuildNot(mask->bld->builder,
262 mask->exec_mask,
263 "");
264
265 mask->cont_stack[mask->cont_stack_size++] = mask->cont_mask;
266 if (mask->cont_stack_size > 1) {
267 mask->cont_mask = LLVMBuildAnd(mask->bld->builder,
268 mask->cont_mask,
269 exec_mask, "");
270 } else
271 mask->cont_mask = exec_mask;
272
273 lp_exec_mask_update(mask);
274 }
275
276
277 static void lp_exec_endloop(struct lp_exec_mask *mask)
278 {
279 LLVMBasicBlockRef endloop;
280 LLVMTypeRef reg_type = LLVMIntType(mask->bld->type.width*
281 mask->bld->type.length);
282 /* i1cond = (mask == 0) */
283 LLVMValueRef i1cond = LLVMBuildICmp(
284 mask->bld->builder,
285 LLVMIntNE,
286 LLVMBuildBitCast(mask->bld->builder, mask->break_mask, reg_type, ""),
287 LLVMConstNull(reg_type), "");
288
289 endloop = lp_build_insert_new_block(mask->bld->builder, "endloop");
290
291 LLVMBuildCondBr(mask->bld->builder,
292 i1cond, mask->loop_block, endloop);
293
294 LLVMPositionBuilderAtEnd(mask->bld->builder, endloop);
295
296 mask->loop_block = mask->loop_stack[--mask->loop_stack_size];
297 /* pop the break mask */
298 if (mask->cont_stack_size) {
299 mask->cont_mask = mask->cont_stack[--mask->cont_stack_size];
300 }
301 if (mask->break_stack_size) {
302 mask->break_mask = mask->cont_stack[--mask->break_stack_size];
303 }
304
305 lp_exec_mask_update(mask);
306 }
307
308 static void lp_exec_mask_store(struct lp_exec_mask *mask,
309 LLVMValueRef val,
310 LLVMValueRef dst)
311 {
312 if (mask->has_mask) {
313 LLVMValueRef real_val, dst_val;
314
315 dst_val = LLVMBuildLoad(mask->bld->builder, dst, "");
316 real_val = lp_build_select(mask->bld,
317 mask->exec_mask,
318 val, dst_val);
319
320 LLVMBuildStore(mask->bld->builder, real_val, dst);
321 } else
322 LLVMBuildStore(mask->bld->builder, val, dst);
323 }
324
325
326 static LLVMValueRef
327 emit_ddx(struct lp_build_tgsi_soa_context *bld,
328 LLVMValueRef src)
329 {
330 LLVMValueRef src_left = lp_build_swizzle1_aos(&bld->base, src, swizzle_left);
331 LLVMValueRef src_right = lp_build_swizzle1_aos(&bld->base, src, swizzle_right);
332 return lp_build_sub(&bld->base, src_right, src_left);
333 }
334
335
336 static LLVMValueRef
337 emit_ddy(struct lp_build_tgsi_soa_context *bld,
338 LLVMValueRef src)
339 {
340 LLVMValueRef src_top = lp_build_swizzle1_aos(&bld->base, src, swizzle_top);
341 LLVMValueRef src_bottom = lp_build_swizzle1_aos(&bld->base, src, swizzle_bottom);
342 return lp_build_sub(&bld->base, src_top, src_bottom);
343 }
344
345
346 /**
347 * Register fetch.
348 */
349 static LLVMValueRef
350 emit_fetch(
351 struct lp_build_tgsi_soa_context *bld,
352 const struct tgsi_full_instruction *inst,
353 unsigned index,
354 const unsigned chan_index )
355 {
356 const struct tgsi_full_src_register *reg = &inst->Src[index];
357 unsigned swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
358 LLVMValueRef res;
359
360 switch (swizzle) {
361 case TGSI_SWIZZLE_X:
362 case TGSI_SWIZZLE_Y:
363 case TGSI_SWIZZLE_Z:
364 case TGSI_SWIZZLE_W:
365
366 switch (reg->Register.File) {
367 case TGSI_FILE_CONSTANT: {
368 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), reg->Register.Index*4 + swizzle, 0);
369 LLVMValueRef scalar_ptr = LLVMBuildGEP(bld->base.builder, bld->consts_ptr, &index, 1, "");
370 LLVMValueRef scalar = LLVMBuildLoad(bld->base.builder, scalar_ptr, "");
371 res = lp_build_broadcast_scalar(&bld->base, scalar);
372 break;
373 }
374
375 case TGSI_FILE_IMMEDIATE:
376 res = bld->immediates[reg->Register.Index][swizzle];
377 assert(res);
378 break;
379
380 case TGSI_FILE_INPUT:
381 res = bld->inputs[reg->Register.Index][swizzle];
382 assert(res);
383 break;
384
385 case TGSI_FILE_TEMPORARY:
386 res = LLVMBuildLoad(bld->base.builder, bld->temps[reg->Register.Index][swizzle], "");
387 if(!res)
388 return bld->base.undef;
389 break;
390
391 default:
392 assert( 0 );
393 return bld->base.undef;
394 }
395 break;
396
397 default:
398 assert( 0 );
399 return bld->base.undef;
400 }
401
402 switch( tgsi_util_get_full_src_register_sign_mode( reg, chan_index ) ) {
403 case TGSI_UTIL_SIGN_CLEAR:
404 res = lp_build_abs( &bld->base, res );
405 break;
406
407 case TGSI_UTIL_SIGN_SET:
408 /* TODO: Use bitwese OR for floating point */
409 res = lp_build_abs( &bld->base, res );
410 res = LLVMBuildNeg( bld->base.builder, res, "" );
411 break;
412
413 case TGSI_UTIL_SIGN_TOGGLE:
414 res = LLVMBuildNeg( bld->base.builder, res, "" );
415 break;
416
417 case TGSI_UTIL_SIGN_KEEP:
418 break;
419 }
420
421 return res;
422 }
423
424
425 /**
426 * Register fetch with derivatives.
427 */
428 static void
429 emit_fetch_deriv(
430 struct lp_build_tgsi_soa_context *bld,
431 const struct tgsi_full_instruction *inst,
432 unsigned index,
433 const unsigned chan_index,
434 LLVMValueRef *res,
435 LLVMValueRef *ddx,
436 LLVMValueRef *ddy)
437 {
438 LLVMValueRef src;
439
440 src = emit_fetch(bld, inst, index, chan_index);
441
442 if(res)
443 *res = src;
444
445 /* TODO: use interpolation coeffs for inputs */
446
447 if(ddx)
448 *ddx = emit_ddx(bld, src);
449
450 if(ddy)
451 *ddy = emit_ddy(bld, src);
452 }
453
454
455 /**
456 * Register store.
457 */
458 static void
459 emit_store(
460 struct lp_build_tgsi_soa_context *bld,
461 const struct tgsi_full_instruction *inst,
462 unsigned index,
463 unsigned chan_index,
464 LLVMValueRef value)
465 {
466 const struct tgsi_full_dst_register *reg = &inst->Dst[index];
467
468 switch( inst->Instruction.Saturate ) {
469 case TGSI_SAT_NONE:
470 break;
471
472 case TGSI_SAT_ZERO_ONE:
473 value = lp_build_max(&bld->base, value, bld->base.zero);
474 value = lp_build_min(&bld->base, value, bld->base.one);
475 break;
476
477 case TGSI_SAT_MINUS_PLUS_ONE:
478 value = lp_build_max(&bld->base, value, lp_build_const_vec(bld->base.type, -1.0));
479 value = lp_build_min(&bld->base, value, bld->base.one);
480 break;
481
482 default:
483 assert(0);
484 }
485
486 switch( reg->Register.File ) {
487 case TGSI_FILE_OUTPUT:
488 lp_exec_mask_store(&bld->exec_mask, value,
489 bld->outputs[reg->Register.Index][chan_index]);
490 break;
491
492 case TGSI_FILE_TEMPORARY:
493 lp_exec_mask_store(&bld->exec_mask, value,
494 bld->temps[reg->Register.Index][chan_index]);
495 break;
496
497 case TGSI_FILE_ADDRESS:
498 /* FIXME */
499 assert(0);
500 break;
501
502 case TGSI_FILE_PREDICATE:
503 /* FIXME */
504 assert(0);
505 break;
506
507 default:
508 assert( 0 );
509 }
510 }
511
512
513 /**
514 * High-level instruction translators.
515 */
516
517
518 static void
519 emit_tex( struct lp_build_tgsi_soa_context *bld,
520 const struct tgsi_full_instruction *inst,
521 boolean apply_lodbias,
522 boolean projected,
523 LLVMValueRef *texel)
524 {
525 const uint unit = inst->Src[1].Register.Index;
526 LLVMValueRef lodbias;
527 LLVMValueRef oow = NULL;
528 LLVMValueRef coords[3];
529 unsigned num_coords;
530 unsigned i;
531
532 switch (inst->Texture.Texture) {
533 case TGSI_TEXTURE_1D:
534 num_coords = 1;
535 break;
536 case TGSI_TEXTURE_2D:
537 case TGSI_TEXTURE_RECT:
538 num_coords = 2;
539 break;
540 case TGSI_TEXTURE_SHADOW1D:
541 case TGSI_TEXTURE_SHADOW2D:
542 case TGSI_TEXTURE_SHADOWRECT:
543 case TGSI_TEXTURE_3D:
544 case TGSI_TEXTURE_CUBE:
545 num_coords = 3;
546 break;
547 default:
548 assert(0);
549 return;
550 }
551
552 if(apply_lodbias)
553 lodbias = emit_fetch( bld, inst, 0, 3 );
554 else
555 lodbias = bld->base.zero;
556
557 if (projected) {
558 oow = emit_fetch( bld, inst, 0, 3 );
559 oow = lp_build_rcp(&bld->base, oow);
560 }
561
562 for (i = 0; i < num_coords; i++) {
563 coords[i] = emit_fetch( bld, inst, 0, i );
564 if (projected)
565 coords[i] = lp_build_mul(&bld->base, coords[i], oow);
566 }
567 for (i = num_coords; i < 3; i++) {
568 coords[i] = bld->base.undef;
569 }
570
571 bld->sampler->emit_fetch_texel(bld->sampler,
572 bld->base.builder,
573 bld->base.type,
574 unit, num_coords, coords, lodbias,
575 texel);
576 }
577
578
579 static void
580 emit_kil(
581 struct lp_build_tgsi_soa_context *bld,
582 const struct tgsi_full_instruction *inst )
583 {
584 const struct tgsi_full_src_register *reg = &inst->Src[0];
585 LLVMValueRef terms[NUM_CHANNELS];
586 LLVMValueRef mask;
587 unsigned chan_index;
588
589 memset(&terms, 0, sizeof terms);
590
591 FOR_EACH_CHANNEL( chan_index ) {
592 unsigned swizzle;
593
594 /* Unswizzle channel */
595 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
596
597 /* Check if the component has not been already tested. */
598 assert(swizzle < NUM_CHANNELS);
599 if( !terms[swizzle] )
600 /* TODO: change the comparison operator instead of setting the sign */
601 terms[swizzle] = emit_fetch(bld, inst, 0, chan_index );
602 }
603
604 mask = NULL;
605 FOR_EACH_CHANNEL( chan_index ) {
606 if(terms[chan_index]) {
607 LLVMValueRef chan_mask;
608
609 chan_mask = lp_build_cmp(&bld->base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->base.zero);
610
611 if(mask)
612 mask = LLVMBuildAnd(bld->base.builder, mask, chan_mask, "");
613 else
614 mask = chan_mask;
615 }
616 }
617
618 if(mask)
619 lp_build_mask_update(bld->mask, mask);
620 }
621
622
623 /**
624 * Check if inst src/dest regs use indirect addressing into temporary
625 * register file.
626 */
627 static boolean
628 indirect_temp_reference(const struct tgsi_full_instruction *inst)
629 {
630 uint i;
631 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
632 const struct tgsi_full_src_register *reg = &inst->Src[i];
633 if (reg->Register.File == TGSI_FILE_TEMPORARY &&
634 reg->Register.Indirect)
635 return TRUE;
636 }
637 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
638 const struct tgsi_full_dst_register *reg = &inst->Dst[i];
639 if (reg->Register.File == TGSI_FILE_TEMPORARY &&
640 reg->Register.Indirect)
641 return TRUE;
642 }
643 return FALSE;
644 }
645
646 static int
647 emit_declaration(
648 struct lp_build_tgsi_soa_context *bld,
649 const struct tgsi_full_declaration *decl)
650 {
651 unsigned first = decl->Range.First;
652 unsigned last = decl->Range.Last;
653 unsigned idx, i;
654 LLVMBasicBlockRef current_block =
655 LLVMGetInsertBlock(bld->base.builder);
656 LLVMBasicBlockRef first_block =
657 LLVMGetEntryBasicBlock(
658 LLVMGetBasicBlockParent(current_block));
659 LLVMValueRef first_inst =
660 LLVMGetFirstInstruction(first_block);
661
662 /* we want alloca's to be the first instruction
663 * in the function so we need to rewind the builder
664 * to the very beginning */
665 LLVMPositionBuilderBefore(bld->base.builder,
666 first_inst);
667
668 for (idx = first; idx <= last; ++idx) {
669 switch (decl->Declaration.File) {
670 case TGSI_FILE_TEMPORARY:
671 for (i = 0; i < NUM_CHANNELS; i++)
672 bld->temps[idx][i] = lp_build_alloca(&bld->base);
673 break;
674
675 case TGSI_FILE_OUTPUT:
676 for (i = 0; i < NUM_CHANNELS; i++)
677 bld->outputs[idx][i] = lp_build_alloca(&bld->base);
678 break;
679
680 default:
681 /* don't need to declare other vars */
682 break;
683 }
684 }
685
686 LLVMPositionBuilderAtEnd(bld->base.builder,
687 current_block);
688 return TRUE;
689 }
690
691 static int
692 emit_instruction(
693 struct lp_build_tgsi_soa_context *bld,
694 const struct tgsi_full_instruction *inst,
695 const struct tgsi_opcode_info *info)
696 {
697 unsigned chan_index;
698 LLVMValueRef src0, src1, src2;
699 LLVMValueRef tmp0, tmp1, tmp2;
700 LLVMValueRef tmp3 = NULL;
701 LLVMValueRef tmp4 = NULL;
702 LLVMValueRef tmp5 = NULL;
703 LLVMValueRef tmp6 = NULL;
704 LLVMValueRef tmp7 = NULL;
705 LLVMValueRef res;
706 LLVMValueRef dst0[NUM_CHANNELS];
707
708 /* we can't handle indirect addressing into temp register file yet */
709 if (indirect_temp_reference(inst))
710 return FALSE;
711
712 /*
713 * Stores and write masks are handled in a general fashion after the long
714 * instruction opcode switch statement.
715 *
716 * Although not stricitly necessary, we avoid generating instructions for
717 * channels which won't be stored, in cases where's that easy. For some
718 * complex instructions, like texture sampling, it is more convenient to
719 * assume a full writemask and then let LLVM optimization passes eliminate
720 * redundant code.
721 */
722
723 assert(info->num_dst <= 1);
724 if(info->num_dst) {
725 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
726 dst0[chan_index] = bld->base.undef;
727 }
728 }
729
730 switch (inst->Instruction.Opcode) {
731 #if 0
732 case TGSI_OPCODE_ARL:
733 /* FIXME */
734 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
735 tmp0 = emit_fetch( bld, inst, 0, chan_index );
736 emit_flr(bld, 0, 0);
737 emit_f2it( bld, 0 );
738 dst0[chan_index] = tmp0;
739 }
740 break;
741 #endif
742
743 case TGSI_OPCODE_MOV:
744 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
745 dst0[chan_index] = emit_fetch( bld, inst, 0, chan_index );
746 }
747 break;
748
749 case TGSI_OPCODE_LIT:
750 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ) {
751 dst0[CHAN_X] = bld->base.one;
752 }
753 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
754 src0 = emit_fetch( bld, inst, 0, CHAN_X );
755 dst0[CHAN_Y] = lp_build_max( &bld->base, src0, bld->base.zero);
756 }
757 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
758 /* XMM[1] = SrcReg[0].yyyy */
759 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
760 /* XMM[1] = max(XMM[1], 0) */
761 tmp1 = lp_build_max( &bld->base, tmp1, bld->base.zero);
762 /* XMM[2] = SrcReg[0].wwww */
763 tmp2 = emit_fetch( bld, inst, 0, CHAN_W );
764 tmp1 = lp_build_pow( &bld->base, tmp1, tmp2);
765 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
766 tmp2 = lp_build_cmp(&bld->base, PIPE_FUNC_GREATER, tmp0, bld->base.zero);
767 dst0[CHAN_Z] = lp_build_select(&bld->base, tmp2, tmp1, bld->base.zero);
768 }
769 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) ) {
770 dst0[CHAN_W] = bld->base.one;
771 }
772 break;
773
774 case TGSI_OPCODE_RCP:
775 /* TGSI_OPCODE_RECIP */
776 src0 = emit_fetch( bld, inst, 0, CHAN_X );
777 res = lp_build_rcp(&bld->base, src0);
778 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
779 dst0[chan_index] = res;
780 }
781 break;
782
783 case TGSI_OPCODE_RSQ:
784 /* TGSI_OPCODE_RECIPSQRT */
785 src0 = emit_fetch( bld, inst, 0, CHAN_X );
786 src0 = lp_build_abs(&bld->base, src0);
787 res = lp_build_rsqrt(&bld->base, src0);
788 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
789 dst0[chan_index] = res;
790 }
791 break;
792
793 case TGSI_OPCODE_EXP:
794 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
795 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
796 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
797 LLVMValueRef *p_exp2_int_part = NULL;
798 LLVMValueRef *p_frac_part = NULL;
799 LLVMValueRef *p_exp2 = NULL;
800
801 src0 = emit_fetch( bld, inst, 0, CHAN_X );
802
803 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
804 p_exp2_int_part = &tmp0;
805 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
806 p_frac_part = &tmp1;
807 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
808 p_exp2 = &tmp2;
809
810 lp_build_exp2_approx(&bld->base, src0, p_exp2_int_part, p_frac_part, p_exp2);
811
812 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
813 dst0[CHAN_X] = tmp0;
814 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
815 dst0[CHAN_Y] = tmp1;
816 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
817 dst0[CHAN_Z] = tmp2;
818 }
819 /* dst.w = 1.0 */
820 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
821 dst0[CHAN_W] = bld->base.one;
822 }
823 break;
824
825 case TGSI_OPCODE_LOG:
826 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
827 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
828 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
829 LLVMValueRef *p_floor_log2 = NULL;
830 LLVMValueRef *p_exp = NULL;
831 LLVMValueRef *p_log2 = NULL;
832
833 src0 = emit_fetch( bld, inst, 0, CHAN_X );
834 src0 = lp_build_abs( &bld->base, src0 );
835
836 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
837 p_floor_log2 = &tmp0;
838 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
839 p_exp = &tmp1;
840 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
841 p_log2 = &tmp2;
842
843 lp_build_log2_approx(&bld->base, src0, p_exp, p_floor_log2, p_log2);
844
845 /* dst.x = floor(lg2(abs(src.x))) */
846 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
847 dst0[CHAN_X] = tmp0;
848 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
849 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y )) {
850 dst0[CHAN_Y] = lp_build_div( &bld->base, src0, tmp1);
851 }
852 /* dst.z = lg2(abs(src.x)) */
853 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
854 dst0[CHAN_Z] = tmp2;
855 }
856 /* dst.w = 1.0 */
857 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
858 dst0[CHAN_W] = bld->base.one;
859 }
860 break;
861
862 case TGSI_OPCODE_MUL:
863 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
864 src0 = emit_fetch( bld, inst, 0, chan_index );
865 src1 = emit_fetch( bld, inst, 1, chan_index );
866 dst0[chan_index] = lp_build_mul(&bld->base, src0, src1);
867 }
868 break;
869
870 case TGSI_OPCODE_ADD:
871 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
872 src0 = emit_fetch( bld, inst, 0, chan_index );
873 src1 = emit_fetch( bld, inst, 1, chan_index );
874 dst0[chan_index] = lp_build_add(&bld->base, src0, src1);
875 }
876 break;
877
878 case TGSI_OPCODE_DP3:
879 /* TGSI_OPCODE_DOT3 */
880 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
881 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
882 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
883 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
884 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
885 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
886 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
887 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
888 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
889 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
890 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
891 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
892 dst0[chan_index] = tmp0;
893 }
894 break;
895
896 case TGSI_OPCODE_DP4:
897 /* TGSI_OPCODE_DOT4 */
898 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
899 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
900 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
901 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
902 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
903 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
904 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
905 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
906 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
907 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
908 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
909 tmp1 = emit_fetch( bld, inst, 0, CHAN_W );
910 tmp2 = emit_fetch( bld, inst, 1, CHAN_W );
911 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
912 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
913 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
914 dst0[chan_index] = tmp0;
915 }
916 break;
917
918 case TGSI_OPCODE_DST:
919 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
920 dst0[CHAN_X] = bld->base.one;
921 }
922 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
923 tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
924 tmp1 = emit_fetch( bld, inst, 1, CHAN_Y );
925 dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp0, tmp1);
926 }
927 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
928 dst0[CHAN_Z] = emit_fetch( bld, inst, 0, CHAN_Z );
929 }
930 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
931 dst0[CHAN_W] = emit_fetch( bld, inst, 1, CHAN_W );
932 }
933 break;
934
935 case TGSI_OPCODE_MIN:
936 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
937 src0 = emit_fetch( bld, inst, 0, chan_index );
938 src1 = emit_fetch( bld, inst, 1, chan_index );
939 dst0[chan_index] = lp_build_min( &bld->base, src0, src1 );
940 }
941 break;
942
943 case TGSI_OPCODE_MAX:
944 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
945 src0 = emit_fetch( bld, inst, 0, chan_index );
946 src1 = emit_fetch( bld, inst, 1, chan_index );
947 dst0[chan_index] = lp_build_max( &bld->base, src0, src1 );
948 }
949 break;
950
951 case TGSI_OPCODE_SLT:
952 /* TGSI_OPCODE_SETLT */
953 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
954 src0 = emit_fetch( bld, inst, 0, chan_index );
955 src1 = emit_fetch( bld, inst, 1, chan_index );
956 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, src1 );
957 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
958 }
959 break;
960
961 case TGSI_OPCODE_SGE:
962 /* TGSI_OPCODE_SETGE */
963 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
964 src0 = emit_fetch( bld, inst, 0, chan_index );
965 src1 = emit_fetch( bld, inst, 1, chan_index );
966 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GEQUAL, src0, src1 );
967 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
968 }
969 break;
970
971 case TGSI_OPCODE_MAD:
972 /* TGSI_OPCODE_MADD */
973 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
974 tmp0 = emit_fetch( bld, inst, 0, chan_index );
975 tmp1 = emit_fetch( bld, inst, 1, chan_index );
976 tmp2 = emit_fetch( bld, inst, 2, chan_index );
977 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
978 tmp0 = lp_build_add( &bld->base, tmp0, tmp2);
979 dst0[chan_index] = tmp0;
980 }
981 break;
982
983 case TGSI_OPCODE_SUB:
984 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
985 tmp0 = emit_fetch( bld, inst, 0, chan_index );
986 tmp1 = emit_fetch( bld, inst, 1, chan_index );
987 dst0[chan_index] = lp_build_sub( &bld->base, tmp0, tmp1);
988 }
989 break;
990
991 case TGSI_OPCODE_LRP:
992 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
993 src0 = emit_fetch( bld, inst, 0, chan_index );
994 src1 = emit_fetch( bld, inst, 1, chan_index );
995 src2 = emit_fetch( bld, inst, 2, chan_index );
996 tmp0 = lp_build_sub( &bld->base, src1, src2 );
997 tmp0 = lp_build_mul( &bld->base, src0, tmp0 );
998 dst0[chan_index] = lp_build_add( &bld->base, tmp0, src2 );
999 }
1000 break;
1001
1002 case TGSI_OPCODE_CND:
1003 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1004 src0 = emit_fetch( bld, inst, 0, chan_index );
1005 src1 = emit_fetch( bld, inst, 1, chan_index );
1006 src2 = emit_fetch( bld, inst, 2, chan_index );
1007 tmp1 = lp_build_const_vec(bld->base.type, 0.5);
1008 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src2, tmp1);
1009 dst0[chan_index] = lp_build_select( &bld->base, tmp0, src0, src1 );
1010 }
1011 break;
1012
1013 case TGSI_OPCODE_DP2A:
1014 tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
1015 tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
1016 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
1017 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
1018 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
1019 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
1020 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1021 tmp1 = emit_fetch( bld, inst, 2, CHAN_X ); /* xmm1 = src[2].x */
1022 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1023 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1024 dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
1025 }
1026 break;
1027
1028 case TGSI_OPCODE_FRC:
1029 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1030 src0 = emit_fetch( bld, inst, 0, chan_index );
1031 tmp0 = lp_build_floor(&bld->base, src0);
1032 tmp0 = lp_build_sub(&bld->base, src0, tmp0);
1033 dst0[chan_index] = tmp0;
1034 }
1035 break;
1036
1037 case TGSI_OPCODE_CLAMP:
1038 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1039 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1040 src1 = emit_fetch( bld, inst, 1, chan_index );
1041 src2 = emit_fetch( bld, inst, 2, chan_index );
1042 tmp0 = lp_build_max(&bld->base, tmp0, src1);
1043 tmp0 = lp_build_min(&bld->base, tmp0, src2);
1044 dst0[chan_index] = tmp0;
1045 }
1046 break;
1047
1048 case TGSI_OPCODE_FLR:
1049 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1050 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1051 dst0[chan_index] = lp_build_floor(&bld->base, tmp0);
1052 }
1053 break;
1054
1055 case TGSI_OPCODE_ROUND:
1056 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1057 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1058 dst0[chan_index] = lp_build_round(&bld->base, tmp0);
1059 }
1060 break;
1061
1062 case TGSI_OPCODE_EX2: {
1063 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1064 tmp0 = lp_build_exp2( &bld->base, tmp0);
1065 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1066 dst0[chan_index] = tmp0;
1067 }
1068 break;
1069 }
1070
1071 case TGSI_OPCODE_LG2:
1072 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1073 tmp0 = lp_build_log2( &bld->base, tmp0);
1074 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1075 dst0[chan_index] = tmp0;
1076 }
1077 break;
1078
1079 case TGSI_OPCODE_POW:
1080 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1081 src1 = emit_fetch( bld, inst, 1, CHAN_X );
1082 res = lp_build_pow( &bld->base, src0, src1 );
1083 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1084 dst0[chan_index] = res;
1085 }
1086 break;
1087
1088 case TGSI_OPCODE_XPD:
1089 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1090 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
1091 tmp1 = emit_fetch( bld, inst, 1, CHAN_Z );
1092 tmp3 = emit_fetch( bld, inst, 0, CHAN_Z );
1093 }
1094 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1095 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1096 tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
1097 tmp4 = emit_fetch( bld, inst, 1, CHAN_Y );
1098 }
1099 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1100 tmp2 = tmp0;
1101 tmp2 = lp_build_mul( &bld->base, tmp2, tmp1);
1102 tmp5 = tmp3;
1103 tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
1104 tmp2 = lp_build_sub( &bld->base, tmp2, tmp5);
1105 dst0[CHAN_X] = tmp2;
1106 }
1107 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
1108 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1109 tmp2 = emit_fetch( bld, inst, 1, CHAN_X );
1110 tmp5 = emit_fetch( bld, inst, 0, CHAN_X );
1111 }
1112 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1113 tmp3 = lp_build_mul( &bld->base, tmp3, tmp2);
1114 tmp1 = lp_build_mul( &bld->base, tmp1, tmp5);
1115 tmp3 = lp_build_sub( &bld->base, tmp3, tmp1);
1116 dst0[CHAN_Y] = tmp3;
1117 }
1118 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1119 tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
1120 tmp0 = lp_build_mul( &bld->base, tmp0, tmp2);
1121 tmp5 = lp_build_sub( &bld->base, tmp5, tmp0);
1122 dst0[CHAN_Z] = tmp5;
1123 }
1124 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1125 dst0[CHAN_W] = bld->base.one;
1126 }
1127 break;
1128
1129 case TGSI_OPCODE_ABS:
1130 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1131 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1132 dst0[chan_index] = lp_build_abs( &bld->base, tmp0 );
1133 }
1134 break;
1135
1136 case TGSI_OPCODE_RCC:
1137 /* deprecated? */
1138 assert(0);
1139 return 0;
1140
1141 case TGSI_OPCODE_DPH:
1142 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1143 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
1144 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1145 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1146 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
1147 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1148 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1149 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
1150 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
1151 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1152 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1153 tmp1 = emit_fetch( bld, inst, 1, CHAN_W );
1154 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1155 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1156 dst0[chan_index] = tmp0;
1157 }
1158 break;
1159
1160 case TGSI_OPCODE_COS:
1161 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1162 tmp0 = lp_build_cos( &bld->base, tmp0 );
1163 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1164 dst0[chan_index] = tmp0;
1165 }
1166 break;
1167
1168 case TGSI_OPCODE_DDX:
1169 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1170 emit_fetch_deriv( bld, inst, 0, chan_index, NULL, &dst0[chan_index], NULL);
1171 }
1172 break;
1173
1174 case TGSI_OPCODE_DDY:
1175 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1176 emit_fetch_deriv( bld, inst, 0, chan_index, NULL, NULL, &dst0[chan_index]);
1177 }
1178 break;
1179
1180 case TGSI_OPCODE_KILP:
1181 /* predicated kill */
1182 /* FIXME */
1183 return 0;
1184 break;
1185
1186 case TGSI_OPCODE_KIL:
1187 /* conditional kill */
1188 emit_kil( bld, inst );
1189 break;
1190
1191 case TGSI_OPCODE_PK2H:
1192 return 0;
1193 break;
1194
1195 case TGSI_OPCODE_PK2US:
1196 return 0;
1197 break;
1198
1199 case TGSI_OPCODE_PK4B:
1200 return 0;
1201 break;
1202
1203 case TGSI_OPCODE_PK4UB:
1204 return 0;
1205 break;
1206
1207 case TGSI_OPCODE_RFL:
1208 return 0;
1209 break;
1210
1211 case TGSI_OPCODE_SEQ:
1212 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1213 src0 = emit_fetch( bld, inst, 0, chan_index );
1214 src1 = emit_fetch( bld, inst, 1, chan_index );
1215 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_EQUAL, src0, src1 );
1216 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1217 }
1218 break;
1219
1220 case TGSI_OPCODE_SFL:
1221 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1222 dst0[chan_index] = bld->base.zero;
1223 }
1224 break;
1225
1226 case TGSI_OPCODE_SGT:
1227 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1228 src0 = emit_fetch( bld, inst, 0, chan_index );
1229 src1 = emit_fetch( bld, inst, 1, chan_index );
1230 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src0, src1 );
1231 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1232 }
1233 break;
1234
1235 case TGSI_OPCODE_SIN:
1236 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1237 tmp0 = lp_build_sin( &bld->base, tmp0 );
1238 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1239 dst0[chan_index] = tmp0;
1240 }
1241 break;
1242
1243 case TGSI_OPCODE_SLE:
1244 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1245 src0 = emit_fetch( bld, inst, 0, chan_index );
1246 src1 = emit_fetch( bld, inst, 1, chan_index );
1247 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LEQUAL, src0, src1 );
1248 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1249 }
1250 break;
1251
1252 case TGSI_OPCODE_SNE:
1253 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1254 src0 = emit_fetch( bld, inst, 0, chan_index );
1255 src1 = emit_fetch( bld, inst, 1, chan_index );
1256 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_NOTEQUAL, src0, src1 );
1257 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1258 }
1259 break;
1260
1261 case TGSI_OPCODE_STR:
1262 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1263 dst0[chan_index] = bld->base.one;
1264 }
1265 break;
1266
1267 case TGSI_OPCODE_TEX:
1268 emit_tex( bld, inst, FALSE, FALSE, dst0 );
1269 break;
1270
1271 case TGSI_OPCODE_TXD:
1272 /* FIXME */
1273 return 0;
1274 break;
1275
1276 case TGSI_OPCODE_UP2H:
1277 /* deprecated */
1278 assert (0);
1279 return 0;
1280 break;
1281
1282 case TGSI_OPCODE_UP2US:
1283 /* deprecated */
1284 assert(0);
1285 return 0;
1286 break;
1287
1288 case TGSI_OPCODE_UP4B:
1289 /* deprecated */
1290 assert(0);
1291 return 0;
1292 break;
1293
1294 case TGSI_OPCODE_UP4UB:
1295 /* deprecated */
1296 assert(0);
1297 return 0;
1298 break;
1299
1300 case TGSI_OPCODE_X2D:
1301 /* deprecated? */
1302 assert(0);
1303 return 0;
1304 break;
1305
1306 case TGSI_OPCODE_ARA:
1307 /* deprecated */
1308 assert(0);
1309 return 0;
1310 break;
1311
1312 #if 0
1313 case TGSI_OPCODE_ARR:
1314 /* FIXME */
1315 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1316 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1317 emit_rnd( bld, 0, 0 );
1318 emit_f2it( bld, 0 );
1319 dst0[chan_index] = tmp0;
1320 }
1321 break;
1322 #endif
1323
1324 case TGSI_OPCODE_BRA:
1325 /* deprecated */
1326 assert(0);
1327 return 0;
1328 break;
1329
1330 case TGSI_OPCODE_CAL:
1331 /* FIXME */
1332 return 0;
1333 break;
1334
1335 case TGSI_OPCODE_RET:
1336 /* FIXME */
1337 return 0;
1338 break;
1339
1340 case TGSI_OPCODE_END:
1341 break;
1342
1343 case TGSI_OPCODE_SSG:
1344 /* TGSI_OPCODE_SGN */
1345 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1346 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1347 dst0[chan_index] = lp_build_sgn( &bld->base, tmp0 );
1348 }
1349 break;
1350
1351 case TGSI_OPCODE_CMP:
1352 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1353 src0 = emit_fetch( bld, inst, 0, chan_index );
1354 src1 = emit_fetch( bld, inst, 1, chan_index );
1355 src2 = emit_fetch( bld, inst, 2, chan_index );
1356 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, bld->base.zero );
1357 dst0[chan_index] = lp_build_select( &bld->base, tmp0, src1, src2);
1358 }
1359 break;
1360
1361 case TGSI_OPCODE_SCS:
1362 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1363 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1364 dst0[CHAN_X] = lp_build_cos( &bld->base, tmp0 );
1365 }
1366 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1367 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1368 dst0[CHAN_Y] = lp_build_sin( &bld->base, tmp0 );
1369 }
1370 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1371 dst0[CHAN_Z] = bld->base.zero;
1372 }
1373 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1374 dst0[CHAN_W] = bld->base.one;
1375 }
1376 break;
1377
1378 case TGSI_OPCODE_TXB:
1379 emit_tex( bld, inst, TRUE, FALSE, dst0 );
1380 break;
1381
1382 case TGSI_OPCODE_NRM:
1383 /* fall-through */
1384 case TGSI_OPCODE_NRM4:
1385 /* 3 or 4-component normalization */
1386 {
1387 uint dims = (inst->Instruction.Opcode == TGSI_OPCODE_NRM) ? 3 : 4;
1388
1389 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) ||
1390 IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y) ||
1391 IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z) ||
1392 (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 4)) {
1393
1394 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1395
1396 /* xmm4 = src.x */
1397 /* xmm0 = src.x * src.x */
1398 tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
1399 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
1400 tmp4 = tmp0;
1401 }
1402 tmp0 = lp_build_mul( &bld->base, tmp0, tmp0);
1403
1404 /* xmm5 = src.y */
1405 /* xmm0 = xmm0 + src.y * src.y */
1406 tmp1 = emit_fetch(bld, inst, 0, CHAN_Y);
1407 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
1408 tmp5 = tmp1;
1409 }
1410 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1411 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1412
1413 /* xmm6 = src.z */
1414 /* xmm0 = xmm0 + src.z * src.z */
1415 tmp1 = emit_fetch(bld, inst, 0, CHAN_Z);
1416 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
1417 tmp6 = tmp1;
1418 }
1419 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1420 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1421
1422 if (dims == 4) {
1423 /* xmm7 = src.w */
1424 /* xmm0 = xmm0 + src.w * src.w */
1425 tmp1 = emit_fetch(bld, inst, 0, CHAN_W);
1426 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W)) {
1427 tmp7 = tmp1;
1428 }
1429 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1430 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1431 }
1432
1433 /* xmm1 = 1 / sqrt(xmm0) */
1434 tmp1 = lp_build_rsqrt( &bld->base, tmp0);
1435
1436 /* dst.x = xmm1 * src.x */
1437 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
1438 dst0[CHAN_X] = lp_build_mul( &bld->base, tmp4, tmp1);
1439 }
1440
1441 /* dst.y = xmm1 * src.y */
1442 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
1443 dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp5, tmp1);
1444 }
1445
1446 /* dst.z = xmm1 * src.z */
1447 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
1448 dst0[CHAN_Z] = lp_build_mul( &bld->base, tmp6, tmp1);
1449 }
1450
1451 /* dst.w = xmm1 * src.w */
1452 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) && dims == 4) {
1453 dst0[CHAN_W] = lp_build_mul( &bld->base, tmp7, tmp1);
1454 }
1455 }
1456
1457 /* dst.w = 1.0 */
1458 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 3) {
1459 dst0[CHAN_W] = bld->base.one;
1460 }
1461 }
1462 break;
1463
1464 case TGSI_OPCODE_DIV:
1465 /* deprecated */
1466 assert( 0 );
1467 return 0;
1468 break;
1469
1470 case TGSI_OPCODE_DP2:
1471 tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
1472 tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
1473 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
1474 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
1475 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
1476 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
1477 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1478 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1479 dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
1480 }
1481 break;
1482
1483 case TGSI_OPCODE_TXL:
1484 emit_tex( bld, inst, TRUE, FALSE, dst0 );
1485 break;
1486
1487 case TGSI_OPCODE_TXP:
1488 emit_tex( bld, inst, FALSE, TRUE, dst0 );
1489 break;
1490
1491 case TGSI_OPCODE_BRK:
1492 lp_exec_break(&bld->exec_mask);
1493 break;
1494
1495 case TGSI_OPCODE_IF:
1496 tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
1497 tmp0 = lp_build_cmp(&bld->base, PIPE_FUNC_NOTEQUAL,
1498 tmp0, bld->base.zero);
1499 lp_exec_mask_cond_push(&bld->exec_mask, tmp0);
1500 break;
1501
1502 case TGSI_OPCODE_BGNFOR:
1503 /* deprecated */
1504 assert(0);
1505 return 0;
1506 break;
1507
1508 case TGSI_OPCODE_BGNLOOP:
1509 lp_exec_bgnloop(&bld->exec_mask);
1510 break;
1511
1512 case TGSI_OPCODE_REP:
1513 /* deprecated */
1514 assert(0);
1515 return 0;
1516 break;
1517
1518 case TGSI_OPCODE_ELSE:
1519 lp_exec_mask_cond_invert(&bld->exec_mask);
1520 break;
1521
1522 case TGSI_OPCODE_ENDIF:
1523 lp_exec_mask_cond_pop(&bld->exec_mask);
1524 break;
1525
1526 case TGSI_OPCODE_ENDFOR:
1527 /* deprecated */
1528 assert(0);
1529 return 0;
1530 break;
1531
1532 case TGSI_OPCODE_ENDLOOP:
1533 lp_exec_endloop(&bld->exec_mask);
1534 break;
1535
1536 case TGSI_OPCODE_ENDREP:
1537 /* deprecated */
1538 assert(0);
1539 return 0;
1540 break;
1541
1542 case TGSI_OPCODE_PUSHA:
1543 /* deprecated? */
1544 assert(0);
1545 return 0;
1546 break;
1547
1548 case TGSI_OPCODE_POPA:
1549 /* deprecated? */
1550 assert(0);
1551 return 0;
1552 break;
1553
1554 case TGSI_OPCODE_CEIL:
1555 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1556 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1557 dst0[chan_index] = lp_build_ceil(&bld->base, tmp0);
1558 }
1559 break;
1560
1561 case TGSI_OPCODE_I2F:
1562 /* deprecated? */
1563 assert(0);
1564 return 0;
1565 break;
1566
1567 case TGSI_OPCODE_NOT:
1568 /* deprecated? */
1569 assert(0);
1570 return 0;
1571 break;
1572
1573 case TGSI_OPCODE_TRUNC:
1574 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1575 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1576 dst0[chan_index] = lp_build_trunc(&bld->base, tmp0);
1577 }
1578 break;
1579
1580 case TGSI_OPCODE_SHL:
1581 /* deprecated? */
1582 assert(0);
1583 return 0;
1584 break;
1585
1586 case TGSI_OPCODE_ISHR:
1587 /* deprecated? */
1588 assert(0);
1589 return 0;
1590 break;
1591
1592 case TGSI_OPCODE_AND:
1593 /* deprecated? */
1594 assert(0);
1595 return 0;
1596 break;
1597
1598 case TGSI_OPCODE_OR:
1599 /* deprecated? */
1600 assert(0);
1601 return 0;
1602 break;
1603
1604 case TGSI_OPCODE_MOD:
1605 /* deprecated? */
1606 assert(0);
1607 return 0;
1608 break;
1609
1610 case TGSI_OPCODE_XOR:
1611 /* deprecated? */
1612 assert(0);
1613 return 0;
1614 break;
1615
1616 case TGSI_OPCODE_SAD:
1617 /* deprecated? */
1618 assert(0);
1619 return 0;
1620 break;
1621
1622 case TGSI_OPCODE_TXF:
1623 /* deprecated? */
1624 assert(0);
1625 return 0;
1626 break;
1627
1628 case TGSI_OPCODE_TXQ:
1629 /* deprecated? */
1630 assert(0);
1631 return 0;
1632 break;
1633
1634 case TGSI_OPCODE_CONT:
1635 lp_exec_continue(&bld->exec_mask);
1636 break;
1637
1638 case TGSI_OPCODE_EMIT:
1639 return 0;
1640 break;
1641
1642 case TGSI_OPCODE_ENDPRIM:
1643 return 0;
1644 break;
1645
1646 case TGSI_OPCODE_NOP:
1647 break;
1648
1649 default:
1650 return 0;
1651 }
1652
1653 if(info->num_dst) {
1654 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1655 emit_store( bld, inst, 0, chan_index, dst0[chan_index]);
1656 }
1657 }
1658
1659 return 1;
1660 }
1661
1662
1663 void
1664 lp_build_tgsi_soa(LLVMBuilderRef builder,
1665 const struct tgsi_token *tokens,
1666 struct lp_type type,
1667 struct lp_build_mask_context *mask,
1668 LLVMValueRef consts_ptr,
1669 const LLVMValueRef *pos,
1670 const LLVMValueRef (*inputs)[NUM_CHANNELS],
1671 LLVMValueRef (*outputs)[NUM_CHANNELS],
1672 struct lp_build_sampler_soa *sampler)
1673 {
1674 struct lp_build_tgsi_soa_context bld;
1675 struct tgsi_parse_context parse;
1676 uint num_immediates = 0;
1677 unsigned i;
1678
1679 /* Setup build context */
1680 memset(&bld, 0, sizeof bld);
1681 lp_build_context_init(&bld.base, builder, type);
1682 bld.mask = mask;
1683 bld.pos = pos;
1684 bld.inputs = inputs;
1685 bld.outputs = outputs;
1686 bld.consts_ptr = consts_ptr;
1687 bld.sampler = sampler;
1688
1689 lp_exec_mask_init(&bld.exec_mask, &bld.base);
1690
1691 tgsi_parse_init( &parse, tokens );
1692
1693 while( !tgsi_parse_end_of_tokens( &parse ) ) {
1694 tgsi_parse_token( &parse );
1695
1696 switch( parse.FullToken.Token.Type ) {
1697 case TGSI_TOKEN_TYPE_DECLARATION:
1698 /* Inputs already interpolated */
1699 {
1700 if (!emit_declaration( &bld, &parse.FullToken.FullDeclaration ))
1701 _debug_printf("warning: failed to define LLVM variable\n");
1702 }
1703 break;
1704
1705 case TGSI_TOKEN_TYPE_INSTRUCTION:
1706 {
1707 unsigned opcode = parse.FullToken.FullInstruction.Instruction.Opcode;
1708 const struct tgsi_opcode_info *info = tgsi_get_opcode_info(opcode);
1709 if (!emit_instruction( &bld, &parse.FullToken.FullInstruction, info ))
1710 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1711 info ? info->mnemonic : "<invalid>");
1712 }
1713
1714 break;
1715
1716 case TGSI_TOKEN_TYPE_IMMEDIATE:
1717 /* simply copy the immediate values into the next immediates[] slot */
1718 {
1719 const uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
1720 assert(size <= 4);
1721 assert(num_immediates < LP_MAX_IMMEDIATES);
1722 for( i = 0; i < size; ++i )
1723 bld.immediates[num_immediates][i] =
1724 lp_build_const_vec(type, parse.FullToken.FullImmediate.u[i].Float);
1725 for( i = size; i < 4; ++i )
1726 bld.immediates[num_immediates][i] = bld.base.undef;
1727 num_immediates++;
1728 }
1729 break;
1730
1731 case TGSI_TOKEN_TYPE_PROPERTY:
1732 break;
1733
1734 default:
1735 assert( 0 );
1736 }
1737 }
1738 if (0) {
1739 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
1740 LLVMValueRef function = LLVMGetBasicBlockParent(block);
1741 debug_printf("11111111111111111111111111111 \n");
1742 tgsi_dump(tokens, 0);
1743 LLVMDumpValue(function);
1744 debug_printf("2222222222222222222222222222 \n");
1745 }
1746 tgsi_parse_free( &parse );
1747 }
1748