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