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