gallivm: don't branch on KILLs near end of shader
[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_scan.h"
49 #include "lp_bld_type.h"
50 #include "lp_bld_const.h"
51 #include "lp_bld_arit.h"
52 #include "lp_bld_bitarit.h"
53 #include "lp_bld_gather.h"
54 #include "lp_bld_logic.h"
55 #include "lp_bld_swizzle.h"
56 #include "lp_bld_flow.h"
57 #include "lp_bld_quad.h"
58 #include "lp_bld_tgsi.h"
59 #include "lp_bld_limits.h"
60 #include "lp_bld_debug.h"
61
62
63 #define FOR_EACH_CHANNEL( CHAN )\
64 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
65
66 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
67 ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
68
69 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
70 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
71
72 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
73 FOR_EACH_CHANNEL( CHAN )\
74 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
75
76 #define CHAN_X 0
77 #define CHAN_Y 1
78 #define CHAN_Z 2
79 #define CHAN_W 3
80 #define NUM_CHANNELS 4
81
82 #define LP_MAX_INSTRUCTIONS 256
83
84
85 struct lp_exec_mask {
86 struct lp_build_context *bld;
87
88 boolean has_mask;
89
90 LLVMTypeRef int_vec_type;
91
92 LLVMValueRef cond_stack[LP_MAX_TGSI_NESTING];
93 int cond_stack_size;
94 LLVMValueRef cond_mask;
95
96 LLVMBasicBlockRef loop_block;
97 LLVMValueRef cont_mask;
98 LLVMValueRef break_mask;
99 LLVMValueRef break_var;
100 struct {
101 LLVMBasicBlockRef loop_block;
102 LLVMValueRef cont_mask;
103 LLVMValueRef break_mask;
104 LLVMValueRef break_var;
105 } loop_stack[LP_MAX_TGSI_NESTING];
106 int loop_stack_size;
107
108 LLVMValueRef ret_mask;
109 struct {
110 int pc;
111 LLVMValueRef ret_mask;
112 } call_stack[LP_MAX_TGSI_NESTING];
113 int call_stack_size;
114
115 LLVMValueRef exec_mask;
116 };
117
118 struct lp_build_tgsi_soa_context
119 {
120 struct lp_build_context base;
121
122 /* Builder for integer masks and indices */
123 struct lp_build_context uint_bld;
124
125 LLVMValueRef consts_ptr;
126 const LLVMValueRef *pos;
127 const LLVMValueRef (*inputs)[NUM_CHANNELS];
128 LLVMValueRef (*outputs)[NUM_CHANNELS];
129
130 const struct lp_build_sampler_soa *sampler;
131
132 LLVMValueRef immediates[LP_MAX_TGSI_IMMEDIATES][NUM_CHANNELS];
133 LLVMValueRef temps[LP_MAX_TGSI_TEMPS][NUM_CHANNELS];
134 LLVMValueRef addr[LP_MAX_TGSI_ADDRS][NUM_CHANNELS];
135 LLVMValueRef preds[LP_MAX_TGSI_PREDS][NUM_CHANNELS];
136
137 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
138 * set in the indirect_files field.
139 * The temps[] array above is unused then.
140 */
141 LLVMValueRef temps_array;
142
143 const struct tgsi_shader_info *info;
144 /** bitmask indicating which register files are accessed indirectly */
145 unsigned indirect_files;
146
147 struct lp_build_mask_context *mask;
148 struct lp_exec_mask exec_mask;
149
150 struct tgsi_full_instruction *instructions;
151 uint max_instructions;
152 };
153
154 static void lp_exec_mask_init(struct lp_exec_mask *mask, struct lp_build_context *bld)
155 {
156 mask->bld = bld;
157 mask->has_mask = FALSE;
158 mask->cond_stack_size = 0;
159 mask->loop_stack_size = 0;
160 mask->call_stack_size = 0;
161
162 mask->int_vec_type = lp_build_int_vec_type(mask->bld->type);
163 mask->exec_mask = mask->ret_mask = mask->break_mask = mask->cont_mask = mask->cond_mask =
164 LLVMConstAllOnes(mask->int_vec_type);
165 }
166
167 static void lp_exec_mask_update(struct lp_exec_mask *mask)
168 {
169 if (mask->loop_stack_size) {
170 /*for loops we need to update the entire mask at runtime */
171 LLVMValueRef tmp;
172 assert(mask->break_mask);
173 tmp = LLVMBuildAnd(mask->bld->builder,
174 mask->cont_mask,
175 mask->break_mask,
176 "maskcb");
177 mask->exec_mask = LLVMBuildAnd(mask->bld->builder,
178 mask->cond_mask,
179 tmp,
180 "maskfull");
181 } else
182 mask->exec_mask = mask->cond_mask;
183
184 if (mask->call_stack_size) {
185 mask->exec_mask = LLVMBuildAnd(mask->bld->builder,
186 mask->exec_mask,
187 mask->ret_mask,
188 "callmask");
189 }
190
191 mask->has_mask = (mask->cond_stack_size > 0 ||
192 mask->loop_stack_size > 0 ||
193 mask->call_stack_size > 0);
194 }
195
196 static void lp_exec_mask_cond_push(struct lp_exec_mask *mask,
197 LLVMValueRef val)
198 {
199 assert(mask->cond_stack_size < LP_MAX_TGSI_NESTING);
200 if (mask->cond_stack_size == 0) {
201 assert(mask->cond_mask == LLVMConstAllOnes(mask->int_vec_type));
202 }
203 mask->cond_stack[mask->cond_stack_size++] = mask->cond_mask;
204 assert(LLVMTypeOf(val) == mask->int_vec_type);
205 mask->cond_mask = LLVMBuildAnd(mask->bld->builder,
206 mask->cond_mask,
207 val,
208 "");
209 lp_exec_mask_update(mask);
210 }
211
212 static void lp_exec_mask_cond_invert(struct lp_exec_mask *mask)
213 {
214 LLVMValueRef prev_mask;
215 LLVMValueRef inv_mask;
216
217 assert(mask->cond_stack_size);
218 prev_mask = mask->cond_stack[mask->cond_stack_size - 1];
219 if (mask->cond_stack_size == 1) {
220 assert(prev_mask == LLVMConstAllOnes(mask->int_vec_type));
221 }
222
223 inv_mask = LLVMBuildNot(mask->bld->builder, mask->cond_mask, "");
224
225 mask->cond_mask = LLVMBuildAnd(mask->bld->builder,
226 inv_mask,
227 prev_mask, "");
228 lp_exec_mask_update(mask);
229 }
230
231 static void lp_exec_mask_cond_pop(struct lp_exec_mask *mask)
232 {
233 assert(mask->cond_stack_size);
234 mask->cond_mask = mask->cond_stack[--mask->cond_stack_size];
235 lp_exec_mask_update(mask);
236 }
237
238 static void lp_exec_bgnloop(struct lp_exec_mask *mask)
239 {
240 if (mask->loop_stack_size == 0) {
241 assert(mask->loop_block == NULL);
242 assert(mask->cont_mask == LLVMConstAllOnes(mask->int_vec_type));
243 assert(mask->break_mask == LLVMConstAllOnes(mask->int_vec_type));
244 assert(mask->break_var == NULL);
245 }
246
247 assert(mask->loop_stack_size < LP_MAX_TGSI_NESTING);
248
249 mask->loop_stack[mask->loop_stack_size].loop_block = mask->loop_block;
250 mask->loop_stack[mask->loop_stack_size].cont_mask = mask->cont_mask;
251 mask->loop_stack[mask->loop_stack_size].break_mask = mask->break_mask;
252 mask->loop_stack[mask->loop_stack_size].break_var = mask->break_var;
253 ++mask->loop_stack_size;
254
255 mask->break_var = lp_build_alloca(mask->bld->builder, mask->int_vec_type, "");
256 LLVMBuildStore(mask->bld->builder, mask->break_mask, mask->break_var);
257
258 mask->loop_block = lp_build_insert_new_block(mask->bld->builder, "bgnloop");
259 LLVMBuildBr(mask->bld->builder, mask->loop_block);
260 LLVMPositionBuilderAtEnd(mask->bld->builder, mask->loop_block);
261
262 mask->break_mask = LLVMBuildLoad(mask->bld->builder, mask->break_var, "");
263
264 lp_exec_mask_update(mask);
265 }
266
267 static void lp_exec_break(struct lp_exec_mask *mask)
268 {
269 LLVMValueRef exec_mask = LLVMBuildNot(mask->bld->builder,
270 mask->exec_mask,
271 "break");
272
273 mask->break_mask = LLVMBuildAnd(mask->bld->builder,
274 mask->break_mask,
275 exec_mask, "break_full");
276
277 lp_exec_mask_update(mask);
278 }
279
280 static void lp_exec_continue(struct lp_exec_mask *mask)
281 {
282 LLVMValueRef exec_mask = LLVMBuildNot(mask->bld->builder,
283 mask->exec_mask,
284 "");
285
286 mask->cont_mask = LLVMBuildAnd(mask->bld->builder,
287 mask->cont_mask,
288 exec_mask, "");
289
290 lp_exec_mask_update(mask);
291 }
292
293
294 static void lp_exec_endloop(struct lp_exec_mask *mask)
295 {
296 LLVMBasicBlockRef endloop;
297 LLVMTypeRef reg_type = LLVMIntType(mask->bld->type.width*
298 mask->bld->type.length);
299 LLVMValueRef i1cond;
300
301 assert(mask->break_mask);
302
303 /*
304 * Restore the cont_mask, but don't pop
305 */
306 assert(mask->loop_stack_size);
307 mask->cont_mask = mask->loop_stack[mask->loop_stack_size - 1].cont_mask;
308 lp_exec_mask_update(mask);
309
310 /*
311 * Unlike the continue mask, the break_mask must be preserved across loop
312 * iterations
313 */
314 LLVMBuildStore(mask->bld->builder, mask->break_mask, mask->break_var);
315
316 /* i1cond = (mask == 0) */
317 i1cond = LLVMBuildICmp(
318 mask->bld->builder,
319 LLVMIntNE,
320 LLVMBuildBitCast(mask->bld->builder, mask->exec_mask, reg_type, ""),
321 LLVMConstNull(reg_type), "");
322
323 endloop = lp_build_insert_new_block(mask->bld->builder, "endloop");
324
325 LLVMBuildCondBr(mask->bld->builder,
326 i1cond, mask->loop_block, endloop);
327
328 LLVMPositionBuilderAtEnd(mask->bld->builder, endloop);
329
330 assert(mask->loop_stack_size);
331 --mask->loop_stack_size;
332 mask->loop_block = mask->loop_stack[mask->loop_stack_size].loop_block;
333 mask->cont_mask = mask->loop_stack[mask->loop_stack_size].cont_mask;
334 mask->break_mask = mask->loop_stack[mask->loop_stack_size].break_mask;
335 mask->break_var = mask->loop_stack[mask->loop_stack_size].break_var;
336
337 lp_exec_mask_update(mask);
338 }
339
340 /* stores val into an address pointed to by dst.
341 * mask->exec_mask is used to figure out which bits of val
342 * should be stored into the address
343 * (0 means don't store this bit, 1 means do store).
344 */
345 static void lp_exec_mask_store(struct lp_exec_mask *mask,
346 LLVMValueRef pred,
347 LLVMValueRef val,
348 LLVMValueRef dst)
349 {
350 /* Mix the predicate and execution mask */
351 if (mask->has_mask) {
352 if (pred) {
353 pred = LLVMBuildAnd(mask->bld->builder, pred, mask->exec_mask, "");
354 } else {
355 pred = mask->exec_mask;
356 }
357 }
358
359 if (pred) {
360 LLVMValueRef real_val, dst_val;
361
362 dst_val = LLVMBuildLoad(mask->bld->builder, dst, "");
363 real_val = lp_build_select(mask->bld,
364 pred,
365 val, dst_val);
366
367 LLVMBuildStore(mask->bld->builder, real_val, dst);
368 } else
369 LLVMBuildStore(mask->bld->builder, val, dst);
370 }
371
372 static void lp_exec_mask_call(struct lp_exec_mask *mask,
373 int func,
374 int *pc)
375 {
376 assert(mask->call_stack_size < LP_MAX_TGSI_NESTING);
377 mask->call_stack[mask->call_stack_size].pc = *pc;
378 mask->call_stack[mask->call_stack_size].ret_mask = mask->ret_mask;
379 mask->call_stack_size++;
380 *pc = func;
381 }
382
383 static void lp_exec_mask_ret(struct lp_exec_mask *mask, int *pc)
384 {
385 LLVMValueRef exec_mask;
386
387 if (mask->call_stack_size == 0) {
388 /* returning from main() */
389 *pc = -1;
390 return;
391 }
392 exec_mask = LLVMBuildNot(mask->bld->builder,
393 mask->exec_mask,
394 "ret");
395
396 mask->ret_mask = LLVMBuildAnd(mask->bld->builder,
397 mask->ret_mask,
398 exec_mask, "ret_full");
399
400 lp_exec_mask_update(mask);
401 }
402
403 static void lp_exec_mask_bgnsub(struct lp_exec_mask *mask)
404 {
405 }
406
407 static void lp_exec_mask_endsub(struct lp_exec_mask *mask, int *pc)
408 {
409 assert(mask->call_stack_size);
410 mask->call_stack_size--;
411 *pc = mask->call_stack[mask->call_stack_size].pc;
412 mask->ret_mask = mask->call_stack[mask->call_stack_size].ret_mask;
413 lp_exec_mask_update(mask);
414 }
415
416
417 /**
418 * Return pointer to a temporary register channel (src or dest).
419 * Note that indirect addressing cannot be handled here.
420 * \param index which temporary register
421 * \param chan which channel of the temp register.
422 */
423 static LLVMValueRef
424 get_temp_ptr(struct lp_build_tgsi_soa_context *bld,
425 unsigned index,
426 unsigned chan)
427 {
428 assert(chan < 4);
429 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
430 LLVMValueRef lindex = lp_build_const_int32(index * 4 + chan);
431 return LLVMBuildGEP(bld->base.builder, bld->temps_array, &lindex, 1, "");
432 }
433 else {
434 return bld->temps[index][chan];
435 }
436 }
437
438
439 /**
440 * Gather vector.
441 * XXX the lp_build_gather() function should be capable of doing this
442 * with a little work.
443 */
444 static LLVMValueRef
445 build_gather(struct lp_build_tgsi_soa_context *bld,
446 LLVMValueRef base_ptr,
447 LLVMValueRef indexes)
448 {
449 LLVMValueRef res = bld->base.undef;
450 unsigned i;
451
452 /*
453 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
454 */
455 for (i = 0; i < bld->base.type.length; i++) {
456 LLVMValueRef ii = LLVMConstInt(LLVMInt32Type(), i, 0);
457 LLVMValueRef index = LLVMBuildExtractElement(bld->base.builder,
458 indexes, ii, "");
459 LLVMValueRef scalar_ptr = LLVMBuildGEP(bld->base.builder, base_ptr,
460 &index, 1, "");
461 LLVMValueRef scalar = LLVMBuildLoad(bld->base.builder, scalar_ptr, "");
462
463 res = LLVMBuildInsertElement(bld->base.builder, res, scalar, ii, "");
464 }
465
466 return res;
467 }
468
469
470 /**
471 * Read the current value of the ADDR register, convert the floats to
472 * ints, multiply by four and return the vector of offsets.
473 * The offsets will be used to index into the constant buffer or
474 * temporary register file.
475 */
476 static LLVMValueRef
477 get_indirect_index(struct lp_build_tgsi_soa_context *bld,
478 unsigned reg_file, unsigned reg_index,
479 const struct tgsi_src_register *indirect_reg)
480 {
481 struct lp_build_context *uint_bld = &bld->uint_bld;
482 /* always use X component of address register */
483 unsigned swizzle = indirect_reg->SwizzleX;
484 LLVMValueRef base;
485 LLVMValueRef rel;
486 LLVMValueRef max_index;
487 LLVMValueRef index;
488
489 assert(bld->indirect_files & (1 << reg_file));
490
491 base = lp_build_const_int_vec(uint_bld->type, reg_index);
492
493 assert(swizzle < 4);
494 rel = LLVMBuildLoad(bld->base.builder,
495 bld->addr[indirect_reg->Index][swizzle],
496 "load addr reg");
497
498 /* for indexing we want integers */
499 rel = LLVMBuildFPToSI(bld->base.builder,
500 rel,
501 uint_bld->vec_type, "");
502
503 index = lp_build_add(uint_bld, base, rel);
504
505 max_index = lp_build_const_int_vec(uint_bld->type,
506 bld->info->file_max[reg_file]);
507
508 assert(!uint_bld->type.sign);
509 index = lp_build_min(uint_bld, index, max_index);
510
511 return index;
512 }
513
514
515 /**
516 * Register fetch.
517 */
518 static LLVMValueRef
519 emit_fetch(
520 struct lp_build_tgsi_soa_context *bld,
521 const struct tgsi_full_instruction *inst,
522 unsigned src_op,
523 const unsigned chan_index )
524 {
525 struct lp_build_context *uint_bld = &bld->uint_bld;
526 const struct tgsi_full_src_register *reg = &inst->Src[src_op];
527 const unsigned swizzle =
528 tgsi_util_get_full_src_register_swizzle(reg, chan_index);
529 LLVMValueRef res;
530 LLVMValueRef indirect_index = NULL;
531
532 if (swizzle > 3) {
533 assert(0 && "invalid swizzle in emit_fetch()");
534 return bld->base.undef;
535 }
536
537 if (reg->Register.Indirect) {
538 indirect_index = get_indirect_index(bld,
539 reg->Register.File,
540 reg->Register.Index,
541 &reg->Indirect);
542 } else {
543 assert(reg->Register.Index <= bld->info->file_max[reg->Register.File]);
544 }
545
546 switch (reg->Register.File) {
547 case TGSI_FILE_CONSTANT:
548 if (reg->Register.Indirect) {
549 LLVMValueRef swizzle_vec =
550 lp_build_const_int_vec(uint_bld->type, swizzle);
551 LLVMValueRef index_vec; /* index into the const buffer */
552
553 /* index_vec = indirect_index * 4 + swizzle */
554 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
555 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
556
557 /* Gather values from the constant buffer */
558 res = build_gather(bld, bld->consts_ptr, index_vec);
559 }
560 else {
561 LLVMValueRef index; /* index into the const buffer */
562 LLVMValueRef scalar, scalar_ptr;
563
564 index = lp_build_const_int32(reg->Register.Index*4 + swizzle);
565
566 scalar_ptr = LLVMBuildGEP(bld->base.builder, bld->consts_ptr,
567 &index, 1, "");
568 scalar = LLVMBuildLoad(bld->base.builder, scalar_ptr, "");
569
570 res = lp_build_broadcast_scalar(&bld->base, scalar);
571 }
572 break;
573
574 case TGSI_FILE_IMMEDIATE:
575 res = bld->immediates[reg->Register.Index][swizzle];
576 assert(res);
577 break;
578
579 case TGSI_FILE_INPUT:
580 res = bld->inputs[reg->Register.Index][swizzle];
581 assert(res);
582 break;
583
584 case TGSI_FILE_TEMPORARY:
585 if (reg->Register.Indirect) {
586 LLVMValueRef swizzle_vec =
587 lp_build_const_int_vec(uint_bld->type, swizzle);
588 LLVMValueRef length_vec =
589 lp_build_const_int_vec(uint_bld->type, bld->base.type.length);
590 LLVMValueRef index_vec; /* index into the const buffer */
591 LLVMValueRef temps_array;
592 LLVMTypeRef float4_ptr_type;
593
594 /* index_vec = (indirect_index * 4 + swizzle) * length */
595 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
596 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
597 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
598
599 /* cast temps_array pointer to float* */
600 float4_ptr_type = LLVMPointerType(LLVMFloatType(), 0);
601 temps_array = LLVMBuildBitCast(uint_bld->builder, bld->temps_array,
602 float4_ptr_type, "");
603
604 /* Gather values from the temporary register array */
605 res = build_gather(bld, temps_array, index_vec);
606 }
607 else {
608 LLVMValueRef temp_ptr;
609 temp_ptr = get_temp_ptr(bld, reg->Register.Index, swizzle);
610 res = LLVMBuildLoad(bld->base.builder, temp_ptr, "");
611 if (!res)
612 return bld->base.undef;
613 }
614 break;
615
616 default:
617 assert(0 && "invalid src register in emit_fetch()");
618 return bld->base.undef;
619 }
620
621 switch( tgsi_util_get_full_src_register_sign_mode( reg, chan_index ) ) {
622 case TGSI_UTIL_SIGN_CLEAR:
623 res = lp_build_abs( &bld->base, res );
624 break;
625
626 case TGSI_UTIL_SIGN_SET:
627 res = lp_build_abs( &bld->base, res );
628 /* fall through */
629 case TGSI_UTIL_SIGN_TOGGLE:
630 res = lp_build_negate( &bld->base, res );
631 break;
632
633 case TGSI_UTIL_SIGN_KEEP:
634 break;
635 }
636
637 return res;
638 }
639
640
641 /**
642 * Register fetch with derivatives.
643 */
644 static void
645 emit_fetch_deriv(
646 struct lp_build_tgsi_soa_context *bld,
647 const struct tgsi_full_instruction *inst,
648 unsigned index,
649 const unsigned chan_index,
650 LLVMValueRef *res,
651 LLVMValueRef *ddx,
652 LLVMValueRef *ddy)
653 {
654 LLVMValueRef src;
655
656 src = emit_fetch(bld, inst, index, chan_index);
657
658 if(res)
659 *res = src;
660
661 /* TODO: use interpolation coeffs for inputs */
662
663 if(ddx)
664 *ddx = lp_build_ddx(&bld->base, src);
665
666 if(ddy)
667 *ddy = lp_build_ddy(&bld->base, src);
668 }
669
670
671 /**
672 * Predicate.
673 */
674 static void
675 emit_fetch_predicate(
676 struct lp_build_tgsi_soa_context *bld,
677 const struct tgsi_full_instruction *inst,
678 LLVMValueRef *pred)
679 {
680 unsigned index;
681 unsigned char swizzles[4];
682 LLVMValueRef unswizzled[4] = {NULL, NULL, NULL, NULL};
683 LLVMValueRef value;
684 unsigned chan;
685
686 if (!inst->Instruction.Predicate) {
687 FOR_EACH_CHANNEL( chan ) {
688 pred[chan] = NULL;
689 }
690 return;
691 }
692
693 swizzles[0] = inst->Predicate.SwizzleX;
694 swizzles[1] = inst->Predicate.SwizzleY;
695 swizzles[2] = inst->Predicate.SwizzleZ;
696 swizzles[3] = inst->Predicate.SwizzleW;
697
698 index = inst->Predicate.Index;
699 assert(index < LP_MAX_TGSI_PREDS);
700
701 FOR_EACH_CHANNEL( chan ) {
702 unsigned swizzle = swizzles[chan];
703
704 /*
705 * Only fetch the predicate register channels that are actually listed
706 * in the swizzles
707 */
708 if (!unswizzled[swizzle]) {
709 value = LLVMBuildLoad(bld->base.builder,
710 bld->preds[index][swizzle], "");
711
712 /*
713 * Convert the value to an integer mask.
714 *
715 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
716 * is needlessly causing two comparisons due to storing the intermediate
717 * result as float vector instead of an integer mask vector.
718 */
719 value = lp_build_compare(bld->base.builder,
720 bld->base.type,
721 PIPE_FUNC_NOTEQUAL,
722 value,
723 bld->base.zero);
724 if (inst->Predicate.Negate) {
725 value = LLVMBuildNot(bld->base.builder, value, "");
726 }
727
728 unswizzled[swizzle] = value;
729 } else {
730 value = unswizzled[swizzle];
731 }
732
733 pred[chan] = value;
734 }
735 }
736
737
738 /**
739 * Register store.
740 */
741 static void
742 emit_store(
743 struct lp_build_tgsi_soa_context *bld,
744 const struct tgsi_full_instruction *inst,
745 unsigned index,
746 unsigned chan_index,
747 LLVMValueRef pred,
748 LLVMValueRef value)
749 {
750 const struct tgsi_full_dst_register *reg = &inst->Dst[index];
751 LLVMValueRef indirect_index = NULL;
752
753 switch( inst->Instruction.Saturate ) {
754 case TGSI_SAT_NONE:
755 break;
756
757 case TGSI_SAT_ZERO_ONE:
758 value = lp_build_max(&bld->base, value, bld->base.zero);
759 value = lp_build_min(&bld->base, value, bld->base.one);
760 break;
761
762 case TGSI_SAT_MINUS_PLUS_ONE:
763 value = lp_build_max(&bld->base, value, lp_build_const_vec(bld->base.type, -1.0));
764 value = lp_build_min(&bld->base, value, bld->base.one);
765 break;
766
767 default:
768 assert(0);
769 }
770
771 if (reg->Register.Indirect) {
772 indirect_index = get_indirect_index(bld,
773 reg->Register.File,
774 reg->Register.Index,
775 &reg->Indirect);
776 } else {
777 assert(reg->Register.Index <= bld->info->file_max[reg->Register.File]);
778 }
779
780 switch( reg->Register.File ) {
781 case TGSI_FILE_OUTPUT:
782 lp_exec_mask_store(&bld->exec_mask, pred, value,
783 bld->outputs[reg->Register.Index][chan_index]);
784 break;
785
786 case TGSI_FILE_TEMPORARY:
787 if (reg->Register.Indirect) {
788 /* XXX not done yet */
789 debug_printf("WARNING: LLVM scatter store of temp regs"
790 " not implemented\n");
791 }
792 else {
793 LLVMValueRef temp_ptr = get_temp_ptr(bld, reg->Register.Index,
794 chan_index);
795 lp_exec_mask_store(&bld->exec_mask, pred, value, temp_ptr);
796 }
797 break;
798
799 case TGSI_FILE_ADDRESS:
800 lp_exec_mask_store(&bld->exec_mask, pred, value,
801 bld->addr[reg->Indirect.Index][chan_index]);
802 break;
803
804 case TGSI_FILE_PREDICATE:
805 lp_exec_mask_store(&bld->exec_mask, pred, value,
806 bld->preds[reg->Register.Index][chan_index]);
807 break;
808
809 default:
810 assert( 0 );
811 }
812 }
813
814
815 /**
816 * High-level instruction translators.
817 */
818
819 static void
820 emit_tex( struct lp_build_tgsi_soa_context *bld,
821 const struct tgsi_full_instruction *inst,
822 enum lp_build_tex_modifier modifier,
823 LLVMValueRef *texel)
824 {
825 unsigned unit;
826 LLVMValueRef lod_bias, explicit_lod;
827 LLVMValueRef oow = NULL;
828 LLVMValueRef coords[3];
829 LLVMValueRef ddx[3];
830 LLVMValueRef ddy[3];
831 unsigned num_coords;
832 unsigned i;
833
834 if (!bld->sampler) {
835 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
836 for (i = 0; i < 4; i++) {
837 texel[i] = bld->base.undef;
838 }
839 return;
840 }
841
842 switch (inst->Texture.Texture) {
843 case TGSI_TEXTURE_1D:
844 num_coords = 1;
845 break;
846 case TGSI_TEXTURE_2D:
847 case TGSI_TEXTURE_RECT:
848 num_coords = 2;
849 break;
850 case TGSI_TEXTURE_SHADOW1D:
851 case TGSI_TEXTURE_SHADOW2D:
852 case TGSI_TEXTURE_SHADOWRECT:
853 case TGSI_TEXTURE_3D:
854 case TGSI_TEXTURE_CUBE:
855 num_coords = 3;
856 break;
857 default:
858 assert(0);
859 return;
860 }
861
862 if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS) {
863 lod_bias = emit_fetch( bld, inst, 0, 3 );
864 explicit_lod = NULL;
865 }
866 else if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
867 lod_bias = NULL;
868 explicit_lod = emit_fetch( bld, inst, 0, 3 );
869 }
870 else {
871 lod_bias = NULL;
872 explicit_lod = NULL;
873 }
874
875 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED) {
876 oow = emit_fetch( bld, inst, 0, 3 );
877 oow = lp_build_rcp(&bld->base, oow);
878 }
879
880 for (i = 0; i < num_coords; i++) {
881 coords[i] = emit_fetch( bld, inst, 0, i );
882 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED)
883 coords[i] = lp_build_mul(&bld->base, coords[i], oow);
884 }
885 for (i = num_coords; i < 3; i++) {
886 coords[i] = bld->base.undef;
887 }
888
889 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
890 LLVMTypeRef i32t = LLVMInt32Type();
891 LLVMValueRef index0 = LLVMConstInt(i32t, 0, 0);
892 for (i = 0; i < num_coords; i++) {
893 LLVMValueRef src1 = emit_fetch( bld, inst, 1, i );
894 LLVMValueRef src2 = emit_fetch( bld, inst, 2, i );
895 ddx[i] = LLVMBuildExtractElement(bld->base.builder, src1, index0, "");
896 ddy[i] = LLVMBuildExtractElement(bld->base.builder, src2, index0, "");
897 }
898 unit = inst->Src[3].Register.Index;
899 } else {
900 for (i = 0; i < num_coords; i++) {
901 ddx[i] = lp_build_scalar_ddx( &bld->base, coords[i] );
902 ddy[i] = lp_build_scalar_ddy( &bld->base, coords[i] );
903 }
904 unit = inst->Src[1].Register.Index;
905 }
906 for (i = num_coords; i < 3; i++) {
907 ddx[i] = LLVMGetUndef(bld->base.elem_type);
908 ddy[i] = LLVMGetUndef(bld->base.elem_type);
909 }
910
911 bld->sampler->emit_fetch_texel(bld->sampler,
912 bld->base.builder,
913 bld->base.type,
914 unit, num_coords, coords,
915 ddx, ddy,
916 lod_bias, explicit_lod,
917 texel);
918 }
919
920 static boolean
921 near_end_of_shader(struct lp_build_tgsi_soa_context *bld,
922 int pc)
923 {
924 int i;
925
926 for (i = 0; i < 5; i++) {
927 unsigned opcode;
928
929 if (pc + i >= bld->info->num_instructions)
930 return TRUE;
931
932 opcode = bld->instructions[pc + i].Instruction.Opcode;
933
934 if (opcode == TGSI_OPCODE_END)
935 return TRUE;
936
937 if (opcode == TGSI_OPCODE_TEX ||
938 opcode == TGSI_OPCODE_TXP ||
939 opcode == TGSI_OPCODE_TXD ||
940 opcode == TGSI_OPCODE_TXB ||
941 opcode == TGSI_OPCODE_TXL ||
942 opcode == TGSI_OPCODE_TXF ||
943 opcode == TGSI_OPCODE_TXQ ||
944 opcode == TGSI_OPCODE_CAL ||
945 opcode == TGSI_OPCODE_CALLNZ ||
946 opcode == TGSI_OPCODE_IF ||
947 opcode == TGSI_OPCODE_IFC ||
948 opcode == TGSI_OPCODE_BGNLOOP ||
949 opcode == TGSI_OPCODE_SWITCH)
950 return FALSE;
951 }
952
953 return TRUE;
954 }
955
956
957
958 /**
959 * Kill fragment if any of the src register values are negative.
960 */
961 static void
962 emit_kil(
963 struct lp_build_tgsi_soa_context *bld,
964 const struct tgsi_full_instruction *inst,
965 int pc)
966 {
967 const struct tgsi_full_src_register *reg = &inst->Src[0];
968 LLVMValueRef terms[NUM_CHANNELS];
969 LLVMValueRef mask;
970 unsigned chan_index;
971
972 memset(&terms, 0, sizeof terms);
973
974 FOR_EACH_CHANNEL( chan_index ) {
975 unsigned swizzle;
976
977 /* Unswizzle channel */
978 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
979
980 /* Check if the component has not been already tested. */
981 assert(swizzle < NUM_CHANNELS);
982 if( !terms[swizzle] )
983 /* TODO: change the comparison operator instead of setting the sign */
984 terms[swizzle] = emit_fetch(bld, inst, 0, chan_index );
985 }
986
987 mask = NULL;
988 FOR_EACH_CHANNEL( chan_index ) {
989 if(terms[chan_index]) {
990 LLVMValueRef chan_mask;
991
992 /*
993 * If term < 0 then mask = 0 else mask = ~0.
994 */
995 chan_mask = lp_build_cmp(&bld->base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->base.zero);
996
997 if(mask)
998 mask = LLVMBuildAnd(bld->base.builder, mask, chan_mask, "");
999 else
1000 mask = chan_mask;
1001 }
1002 }
1003
1004 if(mask) {
1005 lp_build_mask_update(bld->mask, mask);
1006
1007 if (!near_end_of_shader(bld, pc))
1008 lp_build_mask_check(bld->mask);
1009 }
1010 }
1011
1012
1013 /**
1014 * Predicated fragment kill.
1015 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1016 * The only predication is the execution mask which will apply if
1017 * we're inside a loop or conditional.
1018 */
1019 static void
1020 emit_kilp(struct lp_build_tgsi_soa_context *bld,
1021 const struct tgsi_full_instruction *inst,
1022 int pc)
1023 {
1024 LLVMValueRef mask;
1025
1026 /* For those channels which are "alive", disable fragment shader
1027 * execution.
1028 */
1029 if (bld->exec_mask.has_mask) {
1030 mask = LLVMBuildNot(bld->base.builder, bld->exec_mask.exec_mask, "kilp");
1031 }
1032 else {
1033 mask = bld->base.zero;
1034 }
1035
1036 lp_build_mask_update(bld->mask, mask);
1037
1038 if (!near_end_of_shader(bld, pc))
1039 lp_build_mask_check(bld->mask);
1040 }
1041
1042 static void
1043 emit_declaration(
1044 struct lp_build_tgsi_soa_context *bld,
1045 const struct tgsi_full_declaration *decl)
1046 {
1047 LLVMTypeRef vec_type = bld->base.vec_type;
1048
1049 unsigned first = decl->Range.First;
1050 unsigned last = decl->Range.Last;
1051 unsigned idx, i;
1052
1053 for (idx = first; idx <= last; ++idx) {
1054 assert(last <= bld->info->file_max[decl->Declaration.File]);
1055 switch (decl->Declaration.File) {
1056 case TGSI_FILE_TEMPORARY:
1057 assert(idx < LP_MAX_TGSI_TEMPS);
1058 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
1059 LLVMValueRef array_size = LLVMConstInt(LLVMInt32Type(),
1060 last*4 + 4, 0);
1061 bld->temps_array = lp_build_array_alloca(bld->base.builder,
1062 vec_type, array_size, "");
1063 } else {
1064 for (i = 0; i < NUM_CHANNELS; i++)
1065 bld->temps[idx][i] = lp_build_alloca(bld->base.builder,
1066 vec_type, "");
1067 }
1068 break;
1069
1070 case TGSI_FILE_OUTPUT:
1071 for (i = 0; i < NUM_CHANNELS; i++)
1072 bld->outputs[idx][i] = lp_build_alloca(bld->base.builder,
1073 vec_type, "");
1074 break;
1075
1076 case TGSI_FILE_ADDRESS:
1077 assert(idx < LP_MAX_TGSI_ADDRS);
1078 for (i = 0; i < NUM_CHANNELS; i++)
1079 bld->addr[idx][i] = lp_build_alloca(bld->base.builder,
1080 vec_type, "");
1081 break;
1082
1083 case TGSI_FILE_PREDICATE:
1084 assert(idx < LP_MAX_TGSI_PREDS);
1085 for (i = 0; i < NUM_CHANNELS; i++)
1086 bld->preds[idx][i] = lp_build_alloca(bld->base.builder,
1087 vec_type, "");
1088 break;
1089
1090 default:
1091 /* don't need to declare other vars */
1092 break;
1093 }
1094 }
1095 }
1096
1097
1098 /**
1099 * Emit LLVM for one TGSI instruction.
1100 * \param return TRUE for success, FALSE otherwise
1101 */
1102 static boolean
1103 emit_instruction(
1104 struct lp_build_tgsi_soa_context *bld,
1105 const struct tgsi_full_instruction *inst,
1106 const struct tgsi_opcode_info *info,
1107 int *pc)
1108 {
1109 unsigned chan_index;
1110 LLVMValueRef src0, src1, src2;
1111 LLVMValueRef tmp0, tmp1, tmp2;
1112 LLVMValueRef tmp3 = NULL;
1113 LLVMValueRef tmp4 = NULL;
1114 LLVMValueRef tmp5 = NULL;
1115 LLVMValueRef tmp6 = NULL;
1116 LLVMValueRef tmp7 = NULL;
1117 LLVMValueRef res;
1118 LLVMValueRef dst0[NUM_CHANNELS];
1119
1120 /*
1121 * Stores and write masks are handled in a general fashion after the long
1122 * instruction opcode switch statement.
1123 *
1124 * Although not stricitly necessary, we avoid generating instructions for
1125 * channels which won't be stored, in cases where's that easy. For some
1126 * complex instructions, like texture sampling, it is more convenient to
1127 * assume a full writemask and then let LLVM optimization passes eliminate
1128 * redundant code.
1129 */
1130
1131 (*pc)++;
1132
1133 assert(info->num_dst <= 1);
1134 if (info->num_dst) {
1135 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1136 dst0[chan_index] = bld->base.undef;
1137 }
1138 }
1139
1140 switch (inst->Instruction.Opcode) {
1141 case TGSI_OPCODE_ARL:
1142 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1143 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1144 tmp0 = lp_build_floor(&bld->base, tmp0);
1145 dst0[chan_index] = tmp0;
1146 }
1147 break;
1148
1149 case TGSI_OPCODE_MOV:
1150 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1151 dst0[chan_index] = emit_fetch( bld, inst, 0, chan_index );
1152 }
1153 break;
1154
1155 case TGSI_OPCODE_LIT:
1156 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ) {
1157 dst0[CHAN_X] = bld->base.one;
1158 }
1159 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
1160 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1161 dst0[CHAN_Y] = lp_build_max( &bld->base, src0, bld->base.zero);
1162 }
1163 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1164 /* XMM[1] = SrcReg[0].yyyy */
1165 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1166 /* XMM[1] = max(XMM[1], 0) */
1167 tmp1 = lp_build_max( &bld->base, tmp1, bld->base.zero);
1168 /* XMM[2] = SrcReg[0].wwww */
1169 tmp2 = emit_fetch( bld, inst, 0, CHAN_W );
1170 tmp1 = lp_build_pow( &bld->base, tmp1, tmp2);
1171 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1172 tmp2 = lp_build_cmp(&bld->base, PIPE_FUNC_GREATER, tmp0, bld->base.zero);
1173 dst0[CHAN_Z] = lp_build_select(&bld->base, tmp2, tmp1, bld->base.zero);
1174 }
1175 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) ) {
1176 dst0[CHAN_W] = bld->base.one;
1177 }
1178 break;
1179
1180 case TGSI_OPCODE_RCP:
1181 /* TGSI_OPCODE_RECIP */
1182 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1183 res = lp_build_rcp(&bld->base, src0);
1184 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1185 dst0[chan_index] = res;
1186 }
1187 break;
1188
1189 case TGSI_OPCODE_RSQ:
1190 /* TGSI_OPCODE_RECIPSQRT */
1191 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1192 src0 = lp_build_abs(&bld->base, src0);
1193 res = lp_build_rsqrt(&bld->base, src0);
1194 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1195 dst0[chan_index] = res;
1196 }
1197 break;
1198
1199 case TGSI_OPCODE_EXP:
1200 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1201 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
1202 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
1203 LLVMValueRef *p_exp2_int_part = NULL;
1204 LLVMValueRef *p_frac_part = NULL;
1205 LLVMValueRef *p_exp2 = NULL;
1206
1207 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1208
1209 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1210 p_exp2_int_part = &tmp0;
1211 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
1212 p_frac_part = &tmp1;
1213 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1214 p_exp2 = &tmp2;
1215
1216 lp_build_exp2_approx(&bld->base, src0, p_exp2_int_part, p_frac_part, p_exp2);
1217
1218 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1219 dst0[CHAN_X] = tmp0;
1220 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
1221 dst0[CHAN_Y] = tmp1;
1222 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1223 dst0[CHAN_Z] = tmp2;
1224 }
1225 /* dst.w = 1.0 */
1226 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
1227 dst0[CHAN_W] = bld->base.one;
1228 }
1229 break;
1230
1231 case TGSI_OPCODE_LOG:
1232 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1233 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
1234 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
1235 LLVMValueRef *p_floor_log2 = NULL;
1236 LLVMValueRef *p_exp = NULL;
1237 LLVMValueRef *p_log2 = NULL;
1238
1239 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1240 src0 = lp_build_abs( &bld->base, src0 );
1241
1242 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1243 p_floor_log2 = &tmp0;
1244 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
1245 p_exp = &tmp1;
1246 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1247 p_log2 = &tmp2;
1248
1249 lp_build_log2_approx(&bld->base, src0, p_exp, p_floor_log2, p_log2);
1250
1251 /* dst.x = floor(lg2(abs(src.x))) */
1252 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1253 dst0[CHAN_X] = tmp0;
1254 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1255 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y )) {
1256 dst0[CHAN_Y] = lp_build_div( &bld->base, src0, tmp1);
1257 }
1258 /* dst.z = lg2(abs(src.x)) */
1259 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1260 dst0[CHAN_Z] = tmp2;
1261 }
1262 /* dst.w = 1.0 */
1263 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
1264 dst0[CHAN_W] = bld->base.one;
1265 }
1266 break;
1267
1268 case TGSI_OPCODE_MUL:
1269 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1270 src0 = emit_fetch( bld, inst, 0, chan_index );
1271 src1 = emit_fetch( bld, inst, 1, chan_index );
1272 dst0[chan_index] = lp_build_mul(&bld->base, src0, src1);
1273 }
1274 break;
1275
1276 case TGSI_OPCODE_ADD:
1277 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1278 src0 = emit_fetch( bld, inst, 0, chan_index );
1279 src1 = emit_fetch( bld, inst, 1, chan_index );
1280 dst0[chan_index] = lp_build_add(&bld->base, src0, src1);
1281 }
1282 break;
1283
1284 case TGSI_OPCODE_DP3:
1285 /* TGSI_OPCODE_DOT3 */
1286 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1287 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
1288 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1289 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1290 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
1291 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1292 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1293 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
1294 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
1295 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1296 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1297 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1298 dst0[chan_index] = tmp0;
1299 }
1300 break;
1301
1302 case TGSI_OPCODE_DP4:
1303 /* TGSI_OPCODE_DOT4 */
1304 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1305 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
1306 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1307 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1308 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
1309 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1310 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1311 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
1312 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
1313 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1314 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1315 tmp1 = emit_fetch( bld, inst, 0, CHAN_W );
1316 tmp2 = emit_fetch( bld, inst, 1, CHAN_W );
1317 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1318 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1319 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1320 dst0[chan_index] = tmp0;
1321 }
1322 break;
1323
1324 case TGSI_OPCODE_DST:
1325 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1326 dst0[CHAN_X] = bld->base.one;
1327 }
1328 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1329 tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
1330 tmp1 = emit_fetch( bld, inst, 1, CHAN_Y );
1331 dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp0, tmp1);
1332 }
1333 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1334 dst0[CHAN_Z] = emit_fetch( bld, inst, 0, CHAN_Z );
1335 }
1336 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1337 dst0[CHAN_W] = emit_fetch( bld, inst, 1, CHAN_W );
1338 }
1339 break;
1340
1341 case TGSI_OPCODE_MIN:
1342 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1343 src0 = emit_fetch( bld, inst, 0, chan_index );
1344 src1 = emit_fetch( bld, inst, 1, chan_index );
1345 dst0[chan_index] = lp_build_min( &bld->base, src0, src1 );
1346 }
1347 break;
1348
1349 case TGSI_OPCODE_MAX:
1350 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1351 src0 = emit_fetch( bld, inst, 0, chan_index );
1352 src1 = emit_fetch( bld, inst, 1, chan_index );
1353 dst0[chan_index] = lp_build_max( &bld->base, src0, src1 );
1354 }
1355 break;
1356
1357 case TGSI_OPCODE_SLT:
1358 /* TGSI_OPCODE_SETLT */
1359 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1360 src0 = emit_fetch( bld, inst, 0, chan_index );
1361 src1 = emit_fetch( bld, inst, 1, chan_index );
1362 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, src1 );
1363 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1364 }
1365 break;
1366
1367 case TGSI_OPCODE_SGE:
1368 /* TGSI_OPCODE_SETGE */
1369 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1370 src0 = emit_fetch( bld, inst, 0, chan_index );
1371 src1 = emit_fetch( bld, inst, 1, chan_index );
1372 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GEQUAL, src0, src1 );
1373 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1374 }
1375 break;
1376
1377 case TGSI_OPCODE_MAD:
1378 /* TGSI_OPCODE_MADD */
1379 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1380 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1381 tmp1 = emit_fetch( bld, inst, 1, chan_index );
1382 tmp2 = emit_fetch( bld, inst, 2, chan_index );
1383 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1384 tmp0 = lp_build_add( &bld->base, tmp0, tmp2);
1385 dst0[chan_index] = tmp0;
1386 }
1387 break;
1388
1389 case TGSI_OPCODE_SUB:
1390 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1391 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1392 tmp1 = emit_fetch( bld, inst, 1, chan_index );
1393 dst0[chan_index] = lp_build_sub( &bld->base, tmp0, tmp1);
1394 }
1395 break;
1396
1397 case TGSI_OPCODE_LRP:
1398 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1399 src0 = emit_fetch( bld, inst, 0, chan_index );
1400 src1 = emit_fetch( bld, inst, 1, chan_index );
1401 src2 = emit_fetch( bld, inst, 2, chan_index );
1402 tmp0 = lp_build_sub( &bld->base, src1, src2 );
1403 tmp0 = lp_build_mul( &bld->base, src0, tmp0 );
1404 dst0[chan_index] = lp_build_add( &bld->base, tmp0, src2 );
1405 }
1406 break;
1407
1408 case TGSI_OPCODE_CND:
1409 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1410 src0 = emit_fetch( bld, inst, 0, chan_index );
1411 src1 = emit_fetch( bld, inst, 1, chan_index );
1412 src2 = emit_fetch( bld, inst, 2, chan_index );
1413 tmp1 = lp_build_const_vec(bld->base.type, 0.5);
1414 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src2, tmp1);
1415 dst0[chan_index] = lp_build_select( &bld->base, tmp0, src0, src1 );
1416 }
1417 break;
1418
1419 case TGSI_OPCODE_DP2A:
1420 tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
1421 tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
1422 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
1423 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
1424 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
1425 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
1426 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1427 tmp1 = emit_fetch( bld, inst, 2, CHAN_X ); /* xmm1 = src[2].x */
1428 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1429 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1430 dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
1431 }
1432 break;
1433
1434 case TGSI_OPCODE_FRC:
1435 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1436 src0 = emit_fetch( bld, inst, 0, chan_index );
1437 tmp0 = lp_build_floor(&bld->base, src0);
1438 tmp0 = lp_build_sub(&bld->base, src0, tmp0);
1439 dst0[chan_index] = tmp0;
1440 }
1441 break;
1442
1443 case TGSI_OPCODE_CLAMP:
1444 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1445 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1446 src1 = emit_fetch( bld, inst, 1, chan_index );
1447 src2 = emit_fetch( bld, inst, 2, chan_index );
1448 tmp0 = lp_build_max(&bld->base, tmp0, src1);
1449 tmp0 = lp_build_min(&bld->base, tmp0, src2);
1450 dst0[chan_index] = tmp0;
1451 }
1452 break;
1453
1454 case TGSI_OPCODE_FLR:
1455 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1456 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1457 dst0[chan_index] = lp_build_floor(&bld->base, tmp0);
1458 }
1459 break;
1460
1461 case TGSI_OPCODE_ROUND:
1462 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1463 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1464 dst0[chan_index] = lp_build_round(&bld->base, tmp0);
1465 }
1466 break;
1467
1468 case TGSI_OPCODE_EX2: {
1469 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1470 tmp0 = lp_build_exp2( &bld->base, tmp0);
1471 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1472 dst0[chan_index] = tmp0;
1473 }
1474 break;
1475 }
1476
1477 case TGSI_OPCODE_LG2:
1478 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1479 tmp0 = lp_build_log2( &bld->base, tmp0);
1480 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1481 dst0[chan_index] = tmp0;
1482 }
1483 break;
1484
1485 case TGSI_OPCODE_POW:
1486 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1487 src1 = emit_fetch( bld, inst, 1, CHAN_X );
1488 res = lp_build_pow( &bld->base, src0, src1 );
1489 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1490 dst0[chan_index] = res;
1491 }
1492 break;
1493
1494 case TGSI_OPCODE_XPD:
1495 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1496 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
1497 tmp1 = emit_fetch( bld, inst, 1, CHAN_Z );
1498 tmp3 = emit_fetch( bld, inst, 0, CHAN_Z );
1499 }
1500 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1501 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1502 tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
1503 tmp4 = emit_fetch( bld, inst, 1, CHAN_Y );
1504 }
1505 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1506 tmp2 = tmp0;
1507 tmp2 = lp_build_mul( &bld->base, tmp2, tmp1);
1508 tmp5 = tmp3;
1509 tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
1510 tmp2 = lp_build_sub( &bld->base, tmp2, tmp5);
1511 dst0[CHAN_X] = tmp2;
1512 }
1513 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
1514 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1515 tmp2 = emit_fetch( bld, inst, 1, CHAN_X );
1516 tmp5 = emit_fetch( bld, inst, 0, CHAN_X );
1517 }
1518 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1519 tmp3 = lp_build_mul( &bld->base, tmp3, tmp2);
1520 tmp1 = lp_build_mul( &bld->base, tmp1, tmp5);
1521 tmp3 = lp_build_sub( &bld->base, tmp3, tmp1);
1522 dst0[CHAN_Y] = tmp3;
1523 }
1524 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1525 tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
1526 tmp0 = lp_build_mul( &bld->base, tmp0, tmp2);
1527 tmp5 = lp_build_sub( &bld->base, tmp5, tmp0);
1528 dst0[CHAN_Z] = tmp5;
1529 }
1530 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1531 dst0[CHAN_W] = bld->base.one;
1532 }
1533 break;
1534
1535 case TGSI_OPCODE_ABS:
1536 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1537 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1538 dst0[chan_index] = lp_build_abs( &bld->base, tmp0 );
1539 }
1540 break;
1541
1542 case TGSI_OPCODE_RCC:
1543 /* deprecated? */
1544 assert(0);
1545 return FALSE;
1546
1547 case TGSI_OPCODE_DPH:
1548 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1549 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
1550 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1551 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1552 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
1553 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1554 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1555 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
1556 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
1557 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1558 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1559 tmp1 = emit_fetch( bld, inst, 1, CHAN_W );
1560 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1561 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1562 dst0[chan_index] = tmp0;
1563 }
1564 break;
1565
1566 case TGSI_OPCODE_COS:
1567 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1568 tmp0 = lp_build_cos( &bld->base, tmp0 );
1569 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1570 dst0[chan_index] = tmp0;
1571 }
1572 break;
1573
1574 case TGSI_OPCODE_DDX:
1575 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1576 emit_fetch_deriv( bld, inst, 0, chan_index, NULL, &dst0[chan_index], NULL);
1577 }
1578 break;
1579
1580 case TGSI_OPCODE_DDY:
1581 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1582 emit_fetch_deriv( bld, inst, 0, chan_index, NULL, NULL, &dst0[chan_index]);
1583 }
1584 break;
1585
1586 case TGSI_OPCODE_KILP:
1587 /* predicated kill */
1588 emit_kilp( bld, inst, (*pc)-1 );
1589 break;
1590
1591 case TGSI_OPCODE_KIL:
1592 /* conditional kill */
1593 emit_kil( bld, inst, (*pc)-1 );
1594 break;
1595
1596 case TGSI_OPCODE_PK2H:
1597 return FALSE;
1598 break;
1599
1600 case TGSI_OPCODE_PK2US:
1601 return FALSE;
1602 break;
1603
1604 case TGSI_OPCODE_PK4B:
1605 return FALSE;
1606 break;
1607
1608 case TGSI_OPCODE_PK4UB:
1609 return FALSE;
1610 break;
1611
1612 case TGSI_OPCODE_RFL:
1613 return FALSE;
1614 break;
1615
1616 case TGSI_OPCODE_SEQ:
1617 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1618 src0 = emit_fetch( bld, inst, 0, chan_index );
1619 src1 = emit_fetch( bld, inst, 1, chan_index );
1620 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_EQUAL, src0, src1 );
1621 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1622 }
1623 break;
1624
1625 case TGSI_OPCODE_SFL:
1626 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1627 dst0[chan_index] = bld->base.zero;
1628 }
1629 break;
1630
1631 case TGSI_OPCODE_SGT:
1632 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1633 src0 = emit_fetch( bld, inst, 0, chan_index );
1634 src1 = emit_fetch( bld, inst, 1, chan_index );
1635 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src0, src1 );
1636 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1637 }
1638 break;
1639
1640 case TGSI_OPCODE_SIN:
1641 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1642 tmp0 = lp_build_sin( &bld->base, tmp0 );
1643 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1644 dst0[chan_index] = tmp0;
1645 }
1646 break;
1647
1648 case TGSI_OPCODE_SLE:
1649 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1650 src0 = emit_fetch( bld, inst, 0, chan_index );
1651 src1 = emit_fetch( bld, inst, 1, chan_index );
1652 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LEQUAL, src0, src1 );
1653 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1654 }
1655 break;
1656
1657 case TGSI_OPCODE_SNE:
1658 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1659 src0 = emit_fetch( bld, inst, 0, chan_index );
1660 src1 = emit_fetch( bld, inst, 1, chan_index );
1661 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_NOTEQUAL, src0, src1 );
1662 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1663 }
1664 break;
1665
1666 case TGSI_OPCODE_STR:
1667 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1668 dst0[chan_index] = bld->base.one;
1669 }
1670 break;
1671
1672 case TGSI_OPCODE_TEX:
1673 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_NONE, dst0 );
1674 break;
1675
1676 case TGSI_OPCODE_TXD:
1677 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV, dst0 );
1678 break;
1679
1680 case TGSI_OPCODE_UP2H:
1681 /* deprecated */
1682 assert (0);
1683 return FALSE;
1684 break;
1685
1686 case TGSI_OPCODE_UP2US:
1687 /* deprecated */
1688 assert(0);
1689 return FALSE;
1690 break;
1691
1692 case TGSI_OPCODE_UP4B:
1693 /* deprecated */
1694 assert(0);
1695 return FALSE;
1696 break;
1697
1698 case TGSI_OPCODE_UP4UB:
1699 /* deprecated */
1700 assert(0);
1701 return FALSE;
1702 break;
1703
1704 case TGSI_OPCODE_X2D:
1705 /* deprecated? */
1706 assert(0);
1707 return FALSE;
1708 break;
1709
1710 case TGSI_OPCODE_ARA:
1711 /* deprecated */
1712 assert(0);
1713 return FALSE;
1714 break;
1715
1716 case TGSI_OPCODE_ARR:
1717 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1718 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1719 tmp0 = lp_build_round(&bld->base, tmp0);
1720 dst0[chan_index] = tmp0;
1721 }
1722 break;
1723
1724 case TGSI_OPCODE_BRA:
1725 /* deprecated */
1726 assert(0);
1727 return FALSE;
1728 break;
1729
1730 case TGSI_OPCODE_CAL:
1731 lp_exec_mask_call(&bld->exec_mask,
1732 inst->Label.Label,
1733 pc);
1734
1735 break;
1736
1737 case TGSI_OPCODE_RET:
1738 lp_exec_mask_ret(&bld->exec_mask, pc);
1739 break;
1740
1741 case TGSI_OPCODE_END:
1742 *pc = -1;
1743 break;
1744
1745 case TGSI_OPCODE_SSG:
1746 /* TGSI_OPCODE_SGN */
1747 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1748 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1749 dst0[chan_index] = lp_build_sgn( &bld->base, tmp0 );
1750 }
1751 break;
1752
1753 case TGSI_OPCODE_CMP:
1754 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1755 src0 = emit_fetch( bld, inst, 0, chan_index );
1756 src1 = emit_fetch( bld, inst, 1, chan_index );
1757 src2 = emit_fetch( bld, inst, 2, chan_index );
1758 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, bld->base.zero );
1759 dst0[chan_index] = lp_build_select( &bld->base, tmp0, src1, src2);
1760 }
1761 break;
1762
1763 case TGSI_OPCODE_SCS:
1764 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1765 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1766 dst0[CHAN_X] = lp_build_cos( &bld->base, tmp0 );
1767 }
1768 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1769 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1770 dst0[CHAN_Y] = lp_build_sin( &bld->base, tmp0 );
1771 }
1772 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1773 dst0[CHAN_Z] = bld->base.zero;
1774 }
1775 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1776 dst0[CHAN_W] = bld->base.one;
1777 }
1778 break;
1779
1780 case TGSI_OPCODE_TXB:
1781 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_LOD_BIAS, dst0 );
1782 break;
1783
1784 case TGSI_OPCODE_NRM:
1785 /* fall-through */
1786 case TGSI_OPCODE_NRM4:
1787 /* 3 or 4-component normalization */
1788 {
1789 uint dims = (inst->Instruction.Opcode == TGSI_OPCODE_NRM) ? 3 : 4;
1790
1791 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) ||
1792 IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y) ||
1793 IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z) ||
1794 (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 4)) {
1795
1796 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1797
1798 /* xmm4 = src.x */
1799 /* xmm0 = src.x * src.x */
1800 tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
1801 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
1802 tmp4 = tmp0;
1803 }
1804 tmp0 = lp_build_mul( &bld->base, tmp0, tmp0);
1805
1806 /* xmm5 = src.y */
1807 /* xmm0 = xmm0 + src.y * src.y */
1808 tmp1 = emit_fetch(bld, inst, 0, CHAN_Y);
1809 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
1810 tmp5 = tmp1;
1811 }
1812 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1813 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1814
1815 /* xmm6 = src.z */
1816 /* xmm0 = xmm0 + src.z * src.z */
1817 tmp1 = emit_fetch(bld, inst, 0, CHAN_Z);
1818 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
1819 tmp6 = tmp1;
1820 }
1821 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1822 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1823
1824 if (dims == 4) {
1825 /* xmm7 = src.w */
1826 /* xmm0 = xmm0 + src.w * src.w */
1827 tmp1 = emit_fetch(bld, inst, 0, CHAN_W);
1828 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W)) {
1829 tmp7 = tmp1;
1830 }
1831 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1832 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1833 }
1834
1835 /* xmm1 = 1 / sqrt(xmm0) */
1836 tmp1 = lp_build_rsqrt( &bld->base, tmp0);
1837
1838 /* dst.x = xmm1 * src.x */
1839 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
1840 dst0[CHAN_X] = lp_build_mul( &bld->base, tmp4, tmp1);
1841 }
1842
1843 /* dst.y = xmm1 * src.y */
1844 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
1845 dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp5, tmp1);
1846 }
1847
1848 /* dst.z = xmm1 * src.z */
1849 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
1850 dst0[CHAN_Z] = lp_build_mul( &bld->base, tmp6, tmp1);
1851 }
1852
1853 /* dst.w = xmm1 * src.w */
1854 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) && dims == 4) {
1855 dst0[CHAN_W] = lp_build_mul( &bld->base, tmp7, tmp1);
1856 }
1857 }
1858
1859 /* dst.w = 1.0 */
1860 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 3) {
1861 dst0[CHAN_W] = bld->base.one;
1862 }
1863 }
1864 break;
1865
1866 case TGSI_OPCODE_DIV:
1867 /* deprecated */
1868 assert( 0 );
1869 return FALSE;
1870 break;
1871
1872 case TGSI_OPCODE_DP2:
1873 tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
1874 tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
1875 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
1876 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
1877 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
1878 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
1879 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1880 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1881 dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
1882 }
1883 break;
1884
1885 case TGSI_OPCODE_TXL:
1886 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD, dst0 );
1887 break;
1888
1889 case TGSI_OPCODE_TXP:
1890 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_PROJECTED, dst0 );
1891 break;
1892
1893 case TGSI_OPCODE_BRK:
1894 lp_exec_break(&bld->exec_mask);
1895 break;
1896
1897 case TGSI_OPCODE_IF:
1898 tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
1899 tmp0 = lp_build_cmp(&bld->base, PIPE_FUNC_NOTEQUAL,
1900 tmp0, bld->base.zero);
1901 lp_exec_mask_cond_push(&bld->exec_mask, tmp0);
1902 break;
1903
1904 case TGSI_OPCODE_BGNLOOP:
1905 lp_exec_bgnloop(&bld->exec_mask);
1906 break;
1907
1908 case TGSI_OPCODE_BGNSUB:
1909 lp_exec_mask_bgnsub(&bld->exec_mask);
1910 break;
1911
1912 case TGSI_OPCODE_ELSE:
1913 lp_exec_mask_cond_invert(&bld->exec_mask);
1914 break;
1915
1916 case TGSI_OPCODE_ENDIF:
1917 lp_exec_mask_cond_pop(&bld->exec_mask);
1918 break;
1919
1920 case TGSI_OPCODE_ENDLOOP:
1921 lp_exec_endloop(&bld->exec_mask);
1922 break;
1923
1924 case TGSI_OPCODE_ENDSUB:
1925 lp_exec_mask_endsub(&bld->exec_mask, pc);
1926 break;
1927
1928 case TGSI_OPCODE_PUSHA:
1929 /* deprecated? */
1930 assert(0);
1931 return FALSE;
1932 break;
1933
1934 case TGSI_OPCODE_POPA:
1935 /* deprecated? */
1936 assert(0);
1937 return FALSE;
1938 break;
1939
1940 case TGSI_OPCODE_CEIL:
1941 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1942 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1943 dst0[chan_index] = lp_build_ceil(&bld->base, tmp0);
1944 }
1945 break;
1946
1947 case TGSI_OPCODE_I2F:
1948 /* deprecated? */
1949 assert(0);
1950 return FALSE;
1951 break;
1952
1953 case TGSI_OPCODE_NOT:
1954 /* deprecated? */
1955 assert(0);
1956 return FALSE;
1957 break;
1958
1959 case TGSI_OPCODE_TRUNC:
1960 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1961 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1962 dst0[chan_index] = lp_build_trunc(&bld->base, tmp0);
1963 }
1964 break;
1965
1966 case TGSI_OPCODE_SHL:
1967 /* deprecated? */
1968 assert(0);
1969 return FALSE;
1970 break;
1971
1972 case TGSI_OPCODE_ISHR:
1973 /* deprecated? */
1974 assert(0);
1975 return FALSE;
1976 break;
1977
1978 case TGSI_OPCODE_AND:
1979 /* deprecated? */
1980 assert(0);
1981 return FALSE;
1982 break;
1983
1984 case TGSI_OPCODE_OR:
1985 /* deprecated? */
1986 assert(0);
1987 return FALSE;
1988 break;
1989
1990 case TGSI_OPCODE_MOD:
1991 /* deprecated? */
1992 assert(0);
1993 return FALSE;
1994 break;
1995
1996 case TGSI_OPCODE_XOR:
1997 /* deprecated? */
1998 assert(0);
1999 return FALSE;
2000 break;
2001
2002 case TGSI_OPCODE_SAD:
2003 /* deprecated? */
2004 assert(0);
2005 return FALSE;
2006 break;
2007
2008 case TGSI_OPCODE_TXF:
2009 /* deprecated? */
2010 assert(0);
2011 return FALSE;
2012 break;
2013
2014 case TGSI_OPCODE_TXQ:
2015 /* deprecated? */
2016 assert(0);
2017 return FALSE;
2018 break;
2019
2020 case TGSI_OPCODE_CONT:
2021 lp_exec_continue(&bld->exec_mask);
2022 break;
2023
2024 case TGSI_OPCODE_EMIT:
2025 return FALSE;
2026 break;
2027
2028 case TGSI_OPCODE_ENDPRIM:
2029 return FALSE;
2030 break;
2031
2032 case TGSI_OPCODE_NOP:
2033 break;
2034
2035 default:
2036 return FALSE;
2037 }
2038
2039 if(info->num_dst) {
2040 LLVMValueRef pred[NUM_CHANNELS];
2041
2042 emit_fetch_predicate( bld, inst, pred );
2043
2044 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
2045 emit_store( bld, inst, 0, chan_index, pred[chan_index], dst0[chan_index]);
2046 }
2047 }
2048
2049 return TRUE;
2050 }
2051
2052
2053 void
2054 lp_build_tgsi_soa(LLVMBuilderRef builder,
2055 const struct tgsi_token *tokens,
2056 struct lp_type type,
2057 struct lp_build_mask_context *mask,
2058 LLVMValueRef consts_ptr,
2059 const LLVMValueRef *pos,
2060 const LLVMValueRef (*inputs)[NUM_CHANNELS],
2061 LLVMValueRef (*outputs)[NUM_CHANNELS],
2062 struct lp_build_sampler_soa *sampler,
2063 const struct tgsi_shader_info *info)
2064 {
2065 struct lp_build_tgsi_soa_context bld;
2066 struct tgsi_parse_context parse;
2067 uint num_immediates = 0;
2068 uint num_instructions = 0;
2069 unsigned i;
2070 int pc = 0;
2071
2072 struct lp_type res_type;
2073
2074 assert(type.length <= LP_MAX_VECTOR_LENGTH);
2075 memset(&res_type, 0, sizeof res_type);
2076 res_type.width = type.width;
2077 res_type.length = type.length;
2078 res_type.sign = 1;
2079
2080 /* Setup build context */
2081 memset(&bld, 0, sizeof bld);
2082 lp_build_context_init(&bld.base, builder, type);
2083 lp_build_context_init(&bld.uint_bld, builder, lp_uint_type(type));
2084 bld.mask = mask;
2085 bld.pos = pos;
2086 bld.inputs = inputs;
2087 bld.outputs = outputs;
2088 bld.consts_ptr = consts_ptr;
2089 bld.sampler = sampler;
2090 bld.info = info;
2091 bld.indirect_files = info->indirect_files;
2092 bld.instructions = (struct tgsi_full_instruction *)
2093 MALLOC( LP_MAX_INSTRUCTIONS * sizeof(struct tgsi_full_instruction) );
2094 bld.max_instructions = LP_MAX_INSTRUCTIONS;
2095
2096 if (!bld.instructions) {
2097 return;
2098 }
2099
2100 lp_exec_mask_init(&bld.exec_mask, &bld.base);
2101
2102 tgsi_parse_init( &parse, tokens );
2103
2104 while( !tgsi_parse_end_of_tokens( &parse ) ) {
2105 tgsi_parse_token( &parse );
2106
2107 switch( parse.FullToken.Token.Type ) {
2108 case TGSI_TOKEN_TYPE_DECLARATION:
2109 /* Inputs already interpolated */
2110 emit_declaration( &bld, &parse.FullToken.FullDeclaration );
2111 break;
2112
2113 case TGSI_TOKEN_TYPE_INSTRUCTION:
2114 {
2115 /* save expanded instruction */
2116 if (num_instructions == bld.max_instructions) {
2117 struct tgsi_full_instruction *instructions;
2118 instructions = REALLOC(bld.instructions,
2119 bld.max_instructions
2120 * sizeof(struct tgsi_full_instruction),
2121 (bld.max_instructions + LP_MAX_INSTRUCTIONS)
2122 * sizeof(struct tgsi_full_instruction));
2123 if (!instructions) {
2124 break;
2125 }
2126 bld.instructions = instructions;
2127 bld.max_instructions += LP_MAX_INSTRUCTIONS;
2128 }
2129
2130 memcpy(bld.instructions + num_instructions,
2131 &parse.FullToken.FullInstruction,
2132 sizeof(bld.instructions[0]));
2133
2134 num_instructions++;
2135 }
2136
2137 break;
2138
2139 case TGSI_TOKEN_TYPE_IMMEDIATE:
2140 /* simply copy the immediate values into the next immediates[] slot */
2141 {
2142 const uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
2143 assert(size <= 4);
2144 assert(num_immediates < LP_MAX_TGSI_IMMEDIATES);
2145 for( i = 0; i < size; ++i )
2146 bld.immediates[num_immediates][i] =
2147 lp_build_const_vec(type, parse.FullToken.FullImmediate.u[i].Float);
2148 for( i = size; i < 4; ++i )
2149 bld.immediates[num_immediates][i] = bld.base.undef;
2150 num_immediates++;
2151 }
2152 break;
2153
2154 case TGSI_TOKEN_TYPE_PROPERTY:
2155 break;
2156
2157 default:
2158 assert( 0 );
2159 }
2160 }
2161
2162 while (pc != -1) {
2163 struct tgsi_full_instruction *instr = bld.instructions + pc;
2164 const struct tgsi_opcode_info *opcode_info =
2165 tgsi_get_opcode_info(instr->Instruction.Opcode);
2166 if (!emit_instruction( &bld, instr, opcode_info, &pc ))
2167 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2168 opcode_info->mnemonic);
2169 }
2170
2171 if (0) {
2172 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
2173 LLVMValueRef function = LLVMGetBasicBlockParent(block);
2174 debug_printf("11111111111111111111111111111 \n");
2175 tgsi_dump(tokens, 0);
2176 lp_debug_dump_value(function);
2177 debug_printf("2222222222222222222222222222 \n");
2178 }
2179 tgsi_parse_free( &parse );
2180
2181 if (0) {
2182 LLVMModuleRef module = LLVMGetGlobalParent(
2183 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld.base.builder)));
2184 LLVMDumpModule(module);
2185
2186 }
2187
2188 FREE( bld.instructions );
2189 }
2190