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