llvmpipe: try to be sensible about whether to branch after mask updates
[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 for (i = 0; i < num_coords; i++) {
891 ddx[i] = emit_fetch( bld, inst, 1, i );
892 ddy[i] = emit_fetch( bld, inst, 2, i );
893 }
894 unit = inst->Src[3].Register.Index;
895 } else {
896 for (i = 0; i < num_coords; i++) {
897 ddx[i] = lp_build_ddx( &bld->base, coords[i] );
898 ddy[i] = lp_build_ddy( &bld->base, coords[i] );
899 }
900 unit = inst->Src[1].Register.Index;
901 }
902 for (i = num_coords; i < 3; i++) {
903 ddx[i] = bld->base.undef;
904 ddy[i] = bld->base.undef;
905 }
906
907 bld->sampler->emit_fetch_texel(bld->sampler,
908 bld->base.builder,
909 bld->base.type,
910 unit, num_coords, coords,
911 ddx, ddy,
912 lod_bias, explicit_lod,
913 texel);
914 }
915
916
917 /**
918 * Kill fragment if any of the src register values are negative.
919 */
920 static void
921 emit_kil(
922 struct lp_build_tgsi_soa_context *bld,
923 const struct tgsi_full_instruction *inst )
924 {
925 const struct tgsi_full_src_register *reg = &inst->Src[0];
926 LLVMValueRef terms[NUM_CHANNELS];
927 LLVMValueRef mask;
928 unsigned chan_index;
929
930 memset(&terms, 0, sizeof terms);
931
932 FOR_EACH_CHANNEL( chan_index ) {
933 unsigned swizzle;
934
935 /* Unswizzle channel */
936 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
937
938 /* Check if the component has not been already tested. */
939 assert(swizzle < NUM_CHANNELS);
940 if( !terms[swizzle] )
941 /* TODO: change the comparison operator instead of setting the sign */
942 terms[swizzle] = emit_fetch(bld, inst, 0, chan_index );
943 }
944
945 mask = NULL;
946 FOR_EACH_CHANNEL( chan_index ) {
947 if(terms[chan_index]) {
948 LLVMValueRef chan_mask;
949
950 /*
951 * If term < 0 then mask = 0 else mask = ~0.
952 */
953 chan_mask = lp_build_cmp(&bld->base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->base.zero);
954
955 if(mask)
956 mask = LLVMBuildAnd(bld->base.builder, mask, chan_mask, "");
957 else
958 mask = chan_mask;
959 }
960 }
961
962 if(mask) {
963 lp_build_mask_update(bld->mask, mask);
964
965 /* XXX: figure out if we are at the end of the shader and skip this:
966 */
967 lp_build_mask_check(bld->mask);
968 }
969 }
970
971
972 /**
973 * Predicated fragment kill.
974 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
975 * The only predication is the execution mask which will apply if
976 * we're inside a loop or conditional.
977 */
978 static void
979 emit_kilp(struct lp_build_tgsi_soa_context *bld,
980 const struct tgsi_full_instruction *inst)
981 {
982 LLVMValueRef mask;
983
984 /* For those channels which are "alive", disable fragment shader
985 * execution.
986 */
987 if (bld->exec_mask.has_mask) {
988 mask = LLVMBuildNot(bld->base.builder, bld->exec_mask.exec_mask, "kilp");
989 }
990 else {
991 mask = bld->base.zero;
992 }
993
994 lp_build_mask_update(bld->mask, mask);
995
996 /* XXX: figure out if we are at the end of the shader and skip this:
997 */
998 lp_build_mask_check(bld->mask);
999 }
1000
1001 static void
1002 emit_declaration(
1003 struct lp_build_tgsi_soa_context *bld,
1004 const struct tgsi_full_declaration *decl)
1005 {
1006 LLVMTypeRef vec_type = bld->base.vec_type;
1007
1008 unsigned first = decl->Range.First;
1009 unsigned last = decl->Range.Last;
1010 unsigned idx, i;
1011
1012 for (idx = first; idx <= last; ++idx) {
1013 assert(last <= bld->info->file_max[decl->Declaration.File]);
1014 switch (decl->Declaration.File) {
1015 case TGSI_FILE_TEMPORARY:
1016 assert(idx < LP_MAX_TGSI_TEMPS);
1017 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
1018 LLVMValueRef array_size = LLVMConstInt(LLVMInt32Type(),
1019 last*4 + 4, 0);
1020 bld->temps_array = lp_build_array_alloca(bld->base.builder,
1021 vec_type, array_size, "");
1022 } else {
1023 for (i = 0; i < NUM_CHANNELS; i++)
1024 bld->temps[idx][i] = lp_build_alloca(bld->base.builder,
1025 vec_type, "");
1026 }
1027 break;
1028
1029 case TGSI_FILE_OUTPUT:
1030 for (i = 0; i < NUM_CHANNELS; i++)
1031 bld->outputs[idx][i] = lp_build_alloca(bld->base.builder,
1032 vec_type, "");
1033 break;
1034
1035 case TGSI_FILE_ADDRESS:
1036 assert(idx < LP_MAX_TGSI_ADDRS);
1037 for (i = 0; i < NUM_CHANNELS; i++)
1038 bld->addr[idx][i] = lp_build_alloca(bld->base.builder,
1039 vec_type, "");
1040 break;
1041
1042 case TGSI_FILE_PREDICATE:
1043 assert(idx < LP_MAX_TGSI_PREDS);
1044 for (i = 0; i < NUM_CHANNELS; i++)
1045 bld->preds[idx][i] = lp_build_alloca(bld->base.builder,
1046 vec_type, "");
1047 break;
1048
1049 default:
1050 /* don't need to declare other vars */
1051 break;
1052 }
1053 }
1054 }
1055
1056
1057 /**
1058 * Emit LLVM for one TGSI instruction.
1059 * \param return TRUE for success, FALSE otherwise
1060 */
1061 static boolean
1062 emit_instruction(
1063 struct lp_build_tgsi_soa_context *bld,
1064 const struct tgsi_full_instruction *inst,
1065 const struct tgsi_opcode_info *info,
1066 int *pc)
1067 {
1068 unsigned chan_index;
1069 LLVMValueRef src0, src1, src2;
1070 LLVMValueRef tmp0, tmp1, tmp2;
1071 LLVMValueRef tmp3 = NULL;
1072 LLVMValueRef tmp4 = NULL;
1073 LLVMValueRef tmp5 = NULL;
1074 LLVMValueRef tmp6 = NULL;
1075 LLVMValueRef tmp7 = NULL;
1076 LLVMValueRef res;
1077 LLVMValueRef dst0[NUM_CHANNELS];
1078
1079 /*
1080 * Stores and write masks are handled in a general fashion after the long
1081 * instruction opcode switch statement.
1082 *
1083 * Although not stricitly necessary, we avoid generating instructions for
1084 * channels which won't be stored, in cases where's that easy. For some
1085 * complex instructions, like texture sampling, it is more convenient to
1086 * assume a full writemask and then let LLVM optimization passes eliminate
1087 * redundant code.
1088 */
1089
1090 (*pc)++;
1091
1092 assert(info->num_dst <= 1);
1093 if (info->num_dst) {
1094 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1095 dst0[chan_index] = bld->base.undef;
1096 }
1097 }
1098
1099 switch (inst->Instruction.Opcode) {
1100 case TGSI_OPCODE_ARL:
1101 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1102 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1103 tmp0 = lp_build_floor(&bld->base, tmp0);
1104 dst0[chan_index] = tmp0;
1105 }
1106 break;
1107
1108 case TGSI_OPCODE_MOV:
1109 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1110 dst0[chan_index] = emit_fetch( bld, inst, 0, chan_index );
1111 }
1112 break;
1113
1114 case TGSI_OPCODE_LIT:
1115 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ) {
1116 dst0[CHAN_X] = bld->base.one;
1117 }
1118 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
1119 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1120 dst0[CHAN_Y] = lp_build_max( &bld->base, src0, bld->base.zero);
1121 }
1122 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1123 /* XMM[1] = SrcReg[0].yyyy */
1124 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1125 /* XMM[1] = max(XMM[1], 0) */
1126 tmp1 = lp_build_max( &bld->base, tmp1, bld->base.zero);
1127 /* XMM[2] = SrcReg[0].wwww */
1128 tmp2 = emit_fetch( bld, inst, 0, CHAN_W );
1129 tmp1 = lp_build_pow( &bld->base, tmp1, tmp2);
1130 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1131 tmp2 = lp_build_cmp(&bld->base, PIPE_FUNC_GREATER, tmp0, bld->base.zero);
1132 dst0[CHAN_Z] = lp_build_select(&bld->base, tmp2, tmp1, bld->base.zero);
1133 }
1134 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) ) {
1135 dst0[CHAN_W] = bld->base.one;
1136 }
1137 break;
1138
1139 case TGSI_OPCODE_RCP:
1140 /* TGSI_OPCODE_RECIP */
1141 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1142 res = lp_build_rcp(&bld->base, src0);
1143 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1144 dst0[chan_index] = res;
1145 }
1146 break;
1147
1148 case TGSI_OPCODE_RSQ:
1149 /* TGSI_OPCODE_RECIPSQRT */
1150 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1151 src0 = lp_build_abs(&bld->base, src0);
1152 res = lp_build_rsqrt(&bld->base, src0);
1153 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1154 dst0[chan_index] = res;
1155 }
1156 break;
1157
1158 case TGSI_OPCODE_EXP:
1159 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1160 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
1161 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
1162 LLVMValueRef *p_exp2_int_part = NULL;
1163 LLVMValueRef *p_frac_part = NULL;
1164 LLVMValueRef *p_exp2 = NULL;
1165
1166 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1167
1168 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1169 p_exp2_int_part = &tmp0;
1170 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
1171 p_frac_part = &tmp1;
1172 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1173 p_exp2 = &tmp2;
1174
1175 lp_build_exp2_approx(&bld->base, src0, p_exp2_int_part, p_frac_part, p_exp2);
1176
1177 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1178 dst0[CHAN_X] = tmp0;
1179 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
1180 dst0[CHAN_Y] = tmp1;
1181 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1182 dst0[CHAN_Z] = tmp2;
1183 }
1184 /* dst.w = 1.0 */
1185 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
1186 dst0[CHAN_W] = bld->base.one;
1187 }
1188 break;
1189
1190 case TGSI_OPCODE_LOG:
1191 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1192 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
1193 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
1194 LLVMValueRef *p_floor_log2 = NULL;
1195 LLVMValueRef *p_exp = NULL;
1196 LLVMValueRef *p_log2 = NULL;
1197
1198 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1199 src0 = lp_build_abs( &bld->base, src0 );
1200
1201 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1202 p_floor_log2 = &tmp0;
1203 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
1204 p_exp = &tmp1;
1205 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1206 p_log2 = &tmp2;
1207
1208 lp_build_log2_approx(&bld->base, src0, p_exp, p_floor_log2, p_log2);
1209
1210 /* dst.x = floor(lg2(abs(src.x))) */
1211 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
1212 dst0[CHAN_X] = tmp0;
1213 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1214 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y )) {
1215 dst0[CHAN_Y] = lp_build_div( &bld->base, src0, tmp1);
1216 }
1217 /* dst.z = lg2(abs(src.x)) */
1218 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
1219 dst0[CHAN_Z] = tmp2;
1220 }
1221 /* dst.w = 1.0 */
1222 if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
1223 dst0[CHAN_W] = bld->base.one;
1224 }
1225 break;
1226
1227 case TGSI_OPCODE_MUL:
1228 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1229 src0 = emit_fetch( bld, inst, 0, chan_index );
1230 src1 = emit_fetch( bld, inst, 1, chan_index );
1231 dst0[chan_index] = lp_build_mul(&bld->base, src0, src1);
1232 }
1233 break;
1234
1235 case TGSI_OPCODE_ADD:
1236 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1237 src0 = emit_fetch( bld, inst, 0, chan_index );
1238 src1 = emit_fetch( bld, inst, 1, chan_index );
1239 dst0[chan_index] = lp_build_add(&bld->base, src0, src1);
1240 }
1241 break;
1242
1243 case TGSI_OPCODE_DP3:
1244 /* TGSI_OPCODE_DOT3 */
1245 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1246 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
1247 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1248 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1249 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
1250 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1251 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1252 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
1253 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
1254 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1255 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1256 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1257 dst0[chan_index] = tmp0;
1258 }
1259 break;
1260
1261 case TGSI_OPCODE_DP4:
1262 /* TGSI_OPCODE_DOT4 */
1263 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1264 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
1265 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1266 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1267 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
1268 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1269 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1270 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
1271 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
1272 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1273 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1274 tmp1 = emit_fetch( bld, inst, 0, CHAN_W );
1275 tmp2 = emit_fetch( bld, inst, 1, CHAN_W );
1276 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1277 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1278 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1279 dst0[chan_index] = tmp0;
1280 }
1281 break;
1282
1283 case TGSI_OPCODE_DST:
1284 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1285 dst0[CHAN_X] = bld->base.one;
1286 }
1287 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1288 tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
1289 tmp1 = emit_fetch( bld, inst, 1, CHAN_Y );
1290 dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp0, tmp1);
1291 }
1292 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1293 dst0[CHAN_Z] = emit_fetch( bld, inst, 0, CHAN_Z );
1294 }
1295 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1296 dst0[CHAN_W] = emit_fetch( bld, inst, 1, CHAN_W );
1297 }
1298 break;
1299
1300 case TGSI_OPCODE_MIN:
1301 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1302 src0 = emit_fetch( bld, inst, 0, chan_index );
1303 src1 = emit_fetch( bld, inst, 1, chan_index );
1304 dst0[chan_index] = lp_build_min( &bld->base, src0, src1 );
1305 }
1306 break;
1307
1308 case TGSI_OPCODE_MAX:
1309 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1310 src0 = emit_fetch( bld, inst, 0, chan_index );
1311 src1 = emit_fetch( bld, inst, 1, chan_index );
1312 dst0[chan_index] = lp_build_max( &bld->base, src0, src1 );
1313 }
1314 break;
1315
1316 case TGSI_OPCODE_SLT:
1317 /* TGSI_OPCODE_SETLT */
1318 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1319 src0 = emit_fetch( bld, inst, 0, chan_index );
1320 src1 = emit_fetch( bld, inst, 1, chan_index );
1321 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, src1 );
1322 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1323 }
1324 break;
1325
1326 case TGSI_OPCODE_SGE:
1327 /* TGSI_OPCODE_SETGE */
1328 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1329 src0 = emit_fetch( bld, inst, 0, chan_index );
1330 src1 = emit_fetch( bld, inst, 1, chan_index );
1331 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GEQUAL, src0, src1 );
1332 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1333 }
1334 break;
1335
1336 case TGSI_OPCODE_MAD:
1337 /* TGSI_OPCODE_MADD */
1338 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1339 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1340 tmp1 = emit_fetch( bld, inst, 1, chan_index );
1341 tmp2 = emit_fetch( bld, inst, 2, chan_index );
1342 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1343 tmp0 = lp_build_add( &bld->base, tmp0, tmp2);
1344 dst0[chan_index] = tmp0;
1345 }
1346 break;
1347
1348 case TGSI_OPCODE_SUB:
1349 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1350 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1351 tmp1 = emit_fetch( bld, inst, 1, chan_index );
1352 dst0[chan_index] = lp_build_sub( &bld->base, tmp0, tmp1);
1353 }
1354 break;
1355
1356 case TGSI_OPCODE_LRP:
1357 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1358 src0 = emit_fetch( bld, inst, 0, chan_index );
1359 src1 = emit_fetch( bld, inst, 1, chan_index );
1360 src2 = emit_fetch( bld, inst, 2, chan_index );
1361 tmp0 = lp_build_sub( &bld->base, src1, src2 );
1362 tmp0 = lp_build_mul( &bld->base, src0, tmp0 );
1363 dst0[chan_index] = lp_build_add( &bld->base, tmp0, src2 );
1364 }
1365 break;
1366
1367 case TGSI_OPCODE_CND:
1368 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1369 src0 = emit_fetch( bld, inst, 0, chan_index );
1370 src1 = emit_fetch( bld, inst, 1, chan_index );
1371 src2 = emit_fetch( bld, inst, 2, chan_index );
1372 tmp1 = lp_build_const_vec(bld->base.type, 0.5);
1373 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src2, tmp1);
1374 dst0[chan_index] = lp_build_select( &bld->base, tmp0, src0, src1 );
1375 }
1376 break;
1377
1378 case TGSI_OPCODE_DP2A:
1379 tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
1380 tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
1381 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
1382 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
1383 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
1384 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
1385 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1386 tmp1 = emit_fetch( bld, inst, 2, CHAN_X ); /* xmm1 = src[2].x */
1387 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1388 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1389 dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
1390 }
1391 break;
1392
1393 case TGSI_OPCODE_FRC:
1394 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1395 src0 = emit_fetch( bld, inst, 0, chan_index );
1396 tmp0 = lp_build_floor(&bld->base, src0);
1397 tmp0 = lp_build_sub(&bld->base, src0, tmp0);
1398 dst0[chan_index] = tmp0;
1399 }
1400 break;
1401
1402 case TGSI_OPCODE_CLAMP:
1403 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1404 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1405 src1 = emit_fetch( bld, inst, 1, chan_index );
1406 src2 = emit_fetch( bld, inst, 2, chan_index );
1407 tmp0 = lp_build_max(&bld->base, tmp0, src1);
1408 tmp0 = lp_build_min(&bld->base, tmp0, src2);
1409 dst0[chan_index] = tmp0;
1410 }
1411 break;
1412
1413 case TGSI_OPCODE_FLR:
1414 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1415 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1416 dst0[chan_index] = lp_build_floor(&bld->base, tmp0);
1417 }
1418 break;
1419
1420 case TGSI_OPCODE_ROUND:
1421 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1422 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1423 dst0[chan_index] = lp_build_round(&bld->base, tmp0);
1424 }
1425 break;
1426
1427 case TGSI_OPCODE_EX2: {
1428 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1429 tmp0 = lp_build_exp2( &bld->base, tmp0);
1430 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1431 dst0[chan_index] = tmp0;
1432 }
1433 break;
1434 }
1435
1436 case TGSI_OPCODE_LG2:
1437 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1438 tmp0 = lp_build_log2( &bld->base, tmp0);
1439 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1440 dst0[chan_index] = tmp0;
1441 }
1442 break;
1443
1444 case TGSI_OPCODE_POW:
1445 src0 = emit_fetch( bld, inst, 0, CHAN_X );
1446 src1 = emit_fetch( bld, inst, 1, CHAN_X );
1447 res = lp_build_pow( &bld->base, src0, src1 );
1448 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1449 dst0[chan_index] = res;
1450 }
1451 break;
1452
1453 case TGSI_OPCODE_XPD:
1454 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1455 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
1456 tmp1 = emit_fetch( bld, inst, 1, CHAN_Z );
1457 tmp3 = emit_fetch( bld, inst, 0, CHAN_Z );
1458 }
1459 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
1460 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1461 tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
1462 tmp4 = emit_fetch( bld, inst, 1, CHAN_Y );
1463 }
1464 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1465 tmp2 = tmp0;
1466 tmp2 = lp_build_mul( &bld->base, tmp2, tmp1);
1467 tmp5 = tmp3;
1468 tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
1469 tmp2 = lp_build_sub( &bld->base, tmp2, tmp5);
1470 dst0[CHAN_X] = tmp2;
1471 }
1472 if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
1473 IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
1474 tmp2 = emit_fetch( bld, inst, 1, CHAN_X );
1475 tmp5 = emit_fetch( bld, inst, 0, CHAN_X );
1476 }
1477 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1478 tmp3 = lp_build_mul( &bld->base, tmp3, tmp2);
1479 tmp1 = lp_build_mul( &bld->base, tmp1, tmp5);
1480 tmp3 = lp_build_sub( &bld->base, tmp3, tmp1);
1481 dst0[CHAN_Y] = tmp3;
1482 }
1483 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1484 tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
1485 tmp0 = lp_build_mul( &bld->base, tmp0, tmp2);
1486 tmp5 = lp_build_sub( &bld->base, tmp5, tmp0);
1487 dst0[CHAN_Z] = tmp5;
1488 }
1489 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1490 dst0[CHAN_W] = bld->base.one;
1491 }
1492 break;
1493
1494 case TGSI_OPCODE_ABS:
1495 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1496 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1497 dst0[chan_index] = lp_build_abs( &bld->base, tmp0 );
1498 }
1499 break;
1500
1501 case TGSI_OPCODE_RCC:
1502 /* deprecated? */
1503 assert(0);
1504 return FALSE;
1505
1506 case TGSI_OPCODE_DPH:
1507 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1508 tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
1509 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
1510 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
1511 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
1512 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1513 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1514 tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
1515 tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
1516 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
1517 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1518 tmp1 = emit_fetch( bld, inst, 1, CHAN_W );
1519 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1520 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1521 dst0[chan_index] = tmp0;
1522 }
1523 break;
1524
1525 case TGSI_OPCODE_COS:
1526 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1527 tmp0 = lp_build_cos( &bld->base, tmp0 );
1528 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1529 dst0[chan_index] = tmp0;
1530 }
1531 break;
1532
1533 case TGSI_OPCODE_DDX:
1534 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1535 emit_fetch_deriv( bld, inst, 0, chan_index, NULL, &dst0[chan_index], NULL);
1536 }
1537 break;
1538
1539 case TGSI_OPCODE_DDY:
1540 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1541 emit_fetch_deriv( bld, inst, 0, chan_index, NULL, NULL, &dst0[chan_index]);
1542 }
1543 break;
1544
1545 case TGSI_OPCODE_KILP:
1546 /* predicated kill */
1547 emit_kilp( bld, inst );
1548 break;
1549
1550 case TGSI_OPCODE_KIL:
1551 /* conditional kill */
1552 emit_kil( bld, inst );
1553 break;
1554
1555 case TGSI_OPCODE_PK2H:
1556 return FALSE;
1557 break;
1558
1559 case TGSI_OPCODE_PK2US:
1560 return FALSE;
1561 break;
1562
1563 case TGSI_OPCODE_PK4B:
1564 return FALSE;
1565 break;
1566
1567 case TGSI_OPCODE_PK4UB:
1568 return FALSE;
1569 break;
1570
1571 case TGSI_OPCODE_RFL:
1572 return FALSE;
1573 break;
1574
1575 case TGSI_OPCODE_SEQ:
1576 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1577 src0 = emit_fetch( bld, inst, 0, chan_index );
1578 src1 = emit_fetch( bld, inst, 1, chan_index );
1579 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_EQUAL, src0, src1 );
1580 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1581 }
1582 break;
1583
1584 case TGSI_OPCODE_SFL:
1585 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1586 dst0[chan_index] = bld->base.zero;
1587 }
1588 break;
1589
1590 case TGSI_OPCODE_SGT:
1591 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1592 src0 = emit_fetch( bld, inst, 0, chan_index );
1593 src1 = emit_fetch( bld, inst, 1, chan_index );
1594 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src0, src1 );
1595 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1596 }
1597 break;
1598
1599 case TGSI_OPCODE_SIN:
1600 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1601 tmp0 = lp_build_sin( &bld->base, tmp0 );
1602 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1603 dst0[chan_index] = tmp0;
1604 }
1605 break;
1606
1607 case TGSI_OPCODE_SLE:
1608 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1609 src0 = emit_fetch( bld, inst, 0, chan_index );
1610 src1 = emit_fetch( bld, inst, 1, chan_index );
1611 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LEQUAL, src0, src1 );
1612 dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
1613 }
1614 break;
1615
1616 case TGSI_OPCODE_SNE:
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_NOTEQUAL, 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_STR:
1626 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1627 dst0[chan_index] = bld->base.one;
1628 }
1629 break;
1630
1631 case TGSI_OPCODE_TEX:
1632 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_NONE, dst0 );
1633 break;
1634
1635 case TGSI_OPCODE_TXD:
1636 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV, dst0 );
1637 break;
1638
1639 case TGSI_OPCODE_UP2H:
1640 /* deprecated */
1641 assert (0);
1642 return FALSE;
1643 break;
1644
1645 case TGSI_OPCODE_UP2US:
1646 /* deprecated */
1647 assert(0);
1648 return FALSE;
1649 break;
1650
1651 case TGSI_OPCODE_UP4B:
1652 /* deprecated */
1653 assert(0);
1654 return FALSE;
1655 break;
1656
1657 case TGSI_OPCODE_UP4UB:
1658 /* deprecated */
1659 assert(0);
1660 return FALSE;
1661 break;
1662
1663 case TGSI_OPCODE_X2D:
1664 /* deprecated? */
1665 assert(0);
1666 return FALSE;
1667 break;
1668
1669 case TGSI_OPCODE_ARA:
1670 /* deprecated */
1671 assert(0);
1672 return FALSE;
1673 break;
1674
1675 case TGSI_OPCODE_ARR:
1676 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1677 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1678 tmp0 = lp_build_round(&bld->base, tmp0);
1679 dst0[chan_index] = tmp0;
1680 }
1681 break;
1682
1683 case TGSI_OPCODE_BRA:
1684 /* deprecated */
1685 assert(0);
1686 return FALSE;
1687 break;
1688
1689 case TGSI_OPCODE_CAL:
1690 lp_exec_mask_call(&bld->exec_mask,
1691 inst->Label.Label,
1692 pc);
1693
1694 break;
1695
1696 case TGSI_OPCODE_RET:
1697 lp_exec_mask_ret(&bld->exec_mask, pc);
1698 break;
1699
1700 case TGSI_OPCODE_END:
1701 *pc = -1;
1702 break;
1703
1704 case TGSI_OPCODE_SSG:
1705 /* TGSI_OPCODE_SGN */
1706 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1707 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1708 dst0[chan_index] = lp_build_sgn( &bld->base, tmp0 );
1709 }
1710 break;
1711
1712 case TGSI_OPCODE_CMP:
1713 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1714 src0 = emit_fetch( bld, inst, 0, chan_index );
1715 src1 = emit_fetch( bld, inst, 1, chan_index );
1716 src2 = emit_fetch( bld, inst, 2, chan_index );
1717 tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, bld->base.zero );
1718 dst0[chan_index] = lp_build_select( &bld->base, tmp0, src1, src2);
1719 }
1720 break;
1721
1722 case TGSI_OPCODE_SCS:
1723 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
1724 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1725 dst0[CHAN_X] = lp_build_cos( &bld->base, tmp0 );
1726 }
1727 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
1728 tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
1729 dst0[CHAN_Y] = lp_build_sin( &bld->base, tmp0 );
1730 }
1731 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
1732 dst0[CHAN_Z] = bld->base.zero;
1733 }
1734 IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
1735 dst0[CHAN_W] = bld->base.one;
1736 }
1737 break;
1738
1739 case TGSI_OPCODE_TXB:
1740 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_LOD_BIAS, dst0 );
1741 break;
1742
1743 case TGSI_OPCODE_NRM:
1744 /* fall-through */
1745 case TGSI_OPCODE_NRM4:
1746 /* 3 or 4-component normalization */
1747 {
1748 uint dims = (inst->Instruction.Opcode == TGSI_OPCODE_NRM) ? 3 : 4;
1749
1750 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) ||
1751 IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y) ||
1752 IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z) ||
1753 (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 4)) {
1754
1755 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1756
1757 /* xmm4 = src.x */
1758 /* xmm0 = src.x * src.x */
1759 tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
1760 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
1761 tmp4 = tmp0;
1762 }
1763 tmp0 = lp_build_mul( &bld->base, tmp0, tmp0);
1764
1765 /* xmm5 = src.y */
1766 /* xmm0 = xmm0 + src.y * src.y */
1767 tmp1 = emit_fetch(bld, inst, 0, CHAN_Y);
1768 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
1769 tmp5 = tmp1;
1770 }
1771 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1772 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1773
1774 /* xmm6 = src.z */
1775 /* xmm0 = xmm0 + src.z * src.z */
1776 tmp1 = emit_fetch(bld, inst, 0, CHAN_Z);
1777 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
1778 tmp6 = tmp1;
1779 }
1780 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1781 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1782
1783 if (dims == 4) {
1784 /* xmm7 = src.w */
1785 /* xmm0 = xmm0 + src.w * src.w */
1786 tmp1 = emit_fetch(bld, inst, 0, CHAN_W);
1787 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W)) {
1788 tmp7 = tmp1;
1789 }
1790 tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
1791 tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
1792 }
1793
1794 /* xmm1 = 1 / sqrt(xmm0) */
1795 tmp1 = lp_build_rsqrt( &bld->base, tmp0);
1796
1797 /* dst.x = xmm1 * src.x */
1798 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
1799 dst0[CHAN_X] = lp_build_mul( &bld->base, tmp4, tmp1);
1800 }
1801
1802 /* dst.y = xmm1 * src.y */
1803 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
1804 dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp5, tmp1);
1805 }
1806
1807 /* dst.z = xmm1 * src.z */
1808 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
1809 dst0[CHAN_Z] = lp_build_mul( &bld->base, tmp6, tmp1);
1810 }
1811
1812 /* dst.w = xmm1 * src.w */
1813 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) && dims == 4) {
1814 dst0[CHAN_W] = lp_build_mul( &bld->base, tmp7, tmp1);
1815 }
1816 }
1817
1818 /* dst.w = 1.0 */
1819 if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 3) {
1820 dst0[CHAN_W] = bld->base.one;
1821 }
1822 }
1823 break;
1824
1825 case TGSI_OPCODE_DIV:
1826 /* deprecated */
1827 assert( 0 );
1828 return FALSE;
1829 break;
1830
1831 case TGSI_OPCODE_DP2:
1832 tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
1833 tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
1834 tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
1835 tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
1836 tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
1837 tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
1838 tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
1839 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1840 dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
1841 }
1842 break;
1843
1844 case TGSI_OPCODE_TXL:
1845 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD, dst0 );
1846 break;
1847
1848 case TGSI_OPCODE_TXP:
1849 emit_tex( bld, inst, LP_BLD_TEX_MODIFIER_PROJECTED, dst0 );
1850 break;
1851
1852 case TGSI_OPCODE_BRK:
1853 lp_exec_break(&bld->exec_mask);
1854 break;
1855
1856 case TGSI_OPCODE_IF:
1857 tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
1858 tmp0 = lp_build_cmp(&bld->base, PIPE_FUNC_NOTEQUAL,
1859 tmp0, bld->base.zero);
1860 lp_exec_mask_cond_push(&bld->exec_mask, tmp0);
1861 break;
1862
1863 case TGSI_OPCODE_BGNLOOP:
1864 lp_exec_bgnloop(&bld->exec_mask);
1865 break;
1866
1867 case TGSI_OPCODE_BGNSUB:
1868 lp_exec_mask_bgnsub(&bld->exec_mask);
1869 break;
1870
1871 case TGSI_OPCODE_ELSE:
1872 lp_exec_mask_cond_invert(&bld->exec_mask);
1873 break;
1874
1875 case TGSI_OPCODE_ENDIF:
1876 lp_exec_mask_cond_pop(&bld->exec_mask);
1877 break;
1878
1879 case TGSI_OPCODE_ENDLOOP:
1880 lp_exec_endloop(&bld->exec_mask);
1881 break;
1882
1883 case TGSI_OPCODE_ENDSUB:
1884 lp_exec_mask_endsub(&bld->exec_mask, pc);
1885 break;
1886
1887 case TGSI_OPCODE_PUSHA:
1888 /* deprecated? */
1889 assert(0);
1890 return FALSE;
1891 break;
1892
1893 case TGSI_OPCODE_POPA:
1894 /* deprecated? */
1895 assert(0);
1896 return FALSE;
1897 break;
1898
1899 case TGSI_OPCODE_CEIL:
1900 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1901 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1902 dst0[chan_index] = lp_build_ceil(&bld->base, tmp0);
1903 }
1904 break;
1905
1906 case TGSI_OPCODE_I2F:
1907 /* deprecated? */
1908 assert(0);
1909 return FALSE;
1910 break;
1911
1912 case TGSI_OPCODE_NOT:
1913 /* deprecated? */
1914 assert(0);
1915 return FALSE;
1916 break;
1917
1918 case TGSI_OPCODE_TRUNC:
1919 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1920 tmp0 = emit_fetch( bld, inst, 0, chan_index );
1921 dst0[chan_index] = lp_build_trunc(&bld->base, tmp0);
1922 }
1923 break;
1924
1925 case TGSI_OPCODE_SHL:
1926 /* deprecated? */
1927 assert(0);
1928 return FALSE;
1929 break;
1930
1931 case TGSI_OPCODE_ISHR:
1932 /* deprecated? */
1933 assert(0);
1934 return FALSE;
1935 break;
1936
1937 case TGSI_OPCODE_AND:
1938 /* deprecated? */
1939 assert(0);
1940 return FALSE;
1941 break;
1942
1943 case TGSI_OPCODE_OR:
1944 /* deprecated? */
1945 assert(0);
1946 return FALSE;
1947 break;
1948
1949 case TGSI_OPCODE_MOD:
1950 /* deprecated? */
1951 assert(0);
1952 return FALSE;
1953 break;
1954
1955 case TGSI_OPCODE_XOR:
1956 /* deprecated? */
1957 assert(0);
1958 return FALSE;
1959 break;
1960
1961 case TGSI_OPCODE_SAD:
1962 /* deprecated? */
1963 assert(0);
1964 return FALSE;
1965 break;
1966
1967 case TGSI_OPCODE_TXF:
1968 /* deprecated? */
1969 assert(0);
1970 return FALSE;
1971 break;
1972
1973 case TGSI_OPCODE_TXQ:
1974 /* deprecated? */
1975 assert(0);
1976 return FALSE;
1977 break;
1978
1979 case TGSI_OPCODE_CONT:
1980 lp_exec_continue(&bld->exec_mask);
1981 break;
1982
1983 case TGSI_OPCODE_EMIT:
1984 return FALSE;
1985 break;
1986
1987 case TGSI_OPCODE_ENDPRIM:
1988 return FALSE;
1989 break;
1990
1991 case TGSI_OPCODE_NOP:
1992 break;
1993
1994 default:
1995 return FALSE;
1996 }
1997
1998 if(info->num_dst) {
1999 LLVMValueRef pred[NUM_CHANNELS];
2000
2001 emit_fetch_predicate( bld, inst, pred );
2002
2003 FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
2004 emit_store( bld, inst, 0, chan_index, pred[chan_index], dst0[chan_index]);
2005 }
2006 }
2007
2008 return TRUE;
2009 }
2010
2011
2012 void
2013 lp_build_tgsi_soa(LLVMBuilderRef builder,
2014 const struct tgsi_token *tokens,
2015 struct lp_type type,
2016 struct lp_build_mask_context *mask,
2017 LLVMValueRef consts_ptr,
2018 const LLVMValueRef *pos,
2019 const LLVMValueRef (*inputs)[NUM_CHANNELS],
2020 LLVMValueRef (*outputs)[NUM_CHANNELS],
2021 struct lp_build_sampler_soa *sampler,
2022 const struct tgsi_shader_info *info)
2023 {
2024 struct lp_build_tgsi_soa_context bld;
2025 struct tgsi_parse_context parse;
2026 uint num_immediates = 0;
2027 uint num_instructions = 0;
2028 unsigned i;
2029 int pc = 0;
2030
2031 struct lp_type res_type;
2032
2033 assert(type.length <= LP_MAX_VECTOR_LENGTH);
2034 memset(&res_type, 0, sizeof res_type);
2035 res_type.width = type.width;
2036 res_type.length = type.length;
2037 res_type.sign = 1;
2038
2039 /* Setup build context */
2040 memset(&bld, 0, sizeof bld);
2041 lp_build_context_init(&bld.base, builder, type);
2042 lp_build_context_init(&bld.uint_bld, builder, lp_uint_type(type));
2043 bld.mask = mask;
2044 bld.pos = pos;
2045 bld.inputs = inputs;
2046 bld.outputs = outputs;
2047 bld.consts_ptr = consts_ptr;
2048 bld.sampler = sampler;
2049 bld.info = info;
2050 bld.indirect_files = info->indirect_files;
2051 bld.instructions = (struct tgsi_full_instruction *)
2052 MALLOC( LP_MAX_INSTRUCTIONS * sizeof(struct tgsi_full_instruction) );
2053 bld.max_instructions = LP_MAX_INSTRUCTIONS;
2054
2055 if (!bld.instructions) {
2056 return;
2057 }
2058
2059 lp_exec_mask_init(&bld.exec_mask, &bld.base);
2060
2061 tgsi_parse_init( &parse, tokens );
2062
2063 while( !tgsi_parse_end_of_tokens( &parse ) ) {
2064 tgsi_parse_token( &parse );
2065
2066 switch( parse.FullToken.Token.Type ) {
2067 case TGSI_TOKEN_TYPE_DECLARATION:
2068 /* Inputs already interpolated */
2069 emit_declaration( &bld, &parse.FullToken.FullDeclaration );
2070 break;
2071
2072 case TGSI_TOKEN_TYPE_INSTRUCTION:
2073 {
2074 /* save expanded instruction */
2075 if (num_instructions == bld.max_instructions) {
2076 struct tgsi_full_instruction *instructions;
2077 instructions = REALLOC(bld.instructions,
2078 bld.max_instructions
2079 * sizeof(struct tgsi_full_instruction),
2080 (bld.max_instructions + LP_MAX_INSTRUCTIONS)
2081 * sizeof(struct tgsi_full_instruction));
2082 if (!instructions) {
2083 break;
2084 }
2085 bld.instructions = instructions;
2086 bld.max_instructions += LP_MAX_INSTRUCTIONS;
2087 }
2088
2089 memcpy(bld.instructions + num_instructions,
2090 &parse.FullToken.FullInstruction,
2091 sizeof(bld.instructions[0]));
2092
2093 num_instructions++;
2094 }
2095
2096 break;
2097
2098 case TGSI_TOKEN_TYPE_IMMEDIATE:
2099 /* simply copy the immediate values into the next immediates[] slot */
2100 {
2101 const uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
2102 assert(size <= 4);
2103 assert(num_immediates < LP_MAX_TGSI_IMMEDIATES);
2104 for( i = 0; i < size; ++i )
2105 bld.immediates[num_immediates][i] =
2106 lp_build_const_vec(type, parse.FullToken.FullImmediate.u[i].Float);
2107 for( i = size; i < 4; ++i )
2108 bld.immediates[num_immediates][i] = bld.base.undef;
2109 num_immediates++;
2110 }
2111 break;
2112
2113 case TGSI_TOKEN_TYPE_PROPERTY:
2114 break;
2115
2116 default:
2117 assert( 0 );
2118 }
2119 }
2120
2121 while (pc != -1) {
2122 struct tgsi_full_instruction *instr = bld.instructions + pc;
2123 const struct tgsi_opcode_info *opcode_info =
2124 tgsi_get_opcode_info(instr->Instruction.Opcode);
2125 if (!emit_instruction( &bld, instr, opcode_info, &pc ))
2126 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2127 opcode_info->mnemonic);
2128 }
2129
2130 if (0) {
2131 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
2132 LLVMValueRef function = LLVMGetBasicBlockParent(block);
2133 debug_printf("11111111111111111111111111111 \n");
2134 tgsi_dump(tokens, 0);
2135 lp_debug_dump_value(function);
2136 debug_printf("2222222222222222222222222222 \n");
2137 }
2138 tgsi_parse_free( &parse );
2139
2140 if (0) {
2141 LLVMModuleRef module = LLVMGetGlobalParent(
2142 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld.base.builder)));
2143 LLVMDumpModule(module);
2144
2145 }
2146
2147 FREE( bld.instructions );
2148 }
2149