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