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