1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
31 * TGSI to LLVM IR translation -- SoA.
33 * @author Jose Fonseca <jfonseca@vmware.com>
35 * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
36 * Brian Paul, and others.
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 #include "lp_bld_sample.h"
66 #include "lp_bld_struct.h"
68 #define DUMP_GS_EMITS 0
70 static void lp_exec_mask_init(struct lp_exec_mask *mask, struct lp_build_context *bld)
72 LLVMTypeRef int_type = LLVMInt32TypeInContext(bld->gallivm->context);
73 LLVMBuilderRef builder = bld->gallivm->builder;
76 mask->has_mask = FALSE;
77 mask->ret_in_main = FALSE;
78 mask->cond_stack_size = 0;
79 mask->loop_stack_size = 0;
80 mask->call_stack_size = 0;
82 mask->int_vec_type = lp_build_int_vec_type(bld->gallivm, mask->bld->type);
83 mask->exec_mask = mask->ret_mask = mask->break_mask = mask->cont_mask = mask->cond_mask =
84 LLVMConstAllOnes(mask->int_vec_type);
86 mask->loop_limiter = lp_build_alloca(bld->gallivm, int_type, "looplimiter");
90 LLVMConstInt(int_type, LP_MAX_TGSI_LOOP_ITERATIONS, false),
94 static void lp_exec_mask_update(struct lp_exec_mask *mask)
96 LLVMBuilderRef builder = mask->bld->gallivm->builder;
98 if (mask->loop_stack_size) {
99 /*for loops we need to update the entire mask at runtime */
101 assert(mask->break_mask);
102 tmp = LLVMBuildAnd(builder,
106 mask->exec_mask = LLVMBuildAnd(builder,
111 mask->exec_mask = mask->cond_mask;
113 if (mask->call_stack_size || mask->ret_in_main) {
114 mask->exec_mask = LLVMBuildAnd(builder,
120 mask->has_mask = (mask->cond_stack_size > 0 ||
121 mask->loop_stack_size > 0 ||
122 mask->call_stack_size > 0 ||
126 static void lp_exec_mask_cond_push(struct lp_exec_mask *mask,
129 LLVMBuilderRef builder = mask->bld->gallivm->builder;
131 assert(mask->cond_stack_size < LP_MAX_TGSI_NESTING);
132 if (mask->cond_stack_size == 0) {
133 assert(mask->cond_mask == LLVMConstAllOnes(mask->int_vec_type));
135 mask->cond_stack[mask->cond_stack_size++] = mask->cond_mask;
136 assert(LLVMTypeOf(val) == mask->int_vec_type);
137 mask->cond_mask = LLVMBuildAnd(builder,
141 lp_exec_mask_update(mask);
144 static void lp_exec_mask_cond_invert(struct lp_exec_mask *mask)
146 LLVMBuilderRef builder = mask->bld->gallivm->builder;
147 LLVMValueRef prev_mask;
148 LLVMValueRef inv_mask;
150 assert(mask->cond_stack_size);
151 prev_mask = mask->cond_stack[mask->cond_stack_size - 1];
152 if (mask->cond_stack_size == 1) {
153 assert(prev_mask == LLVMConstAllOnes(mask->int_vec_type));
156 inv_mask = LLVMBuildNot(builder, mask->cond_mask, "");
158 mask->cond_mask = LLVMBuildAnd(builder,
161 lp_exec_mask_update(mask);
164 static void lp_exec_mask_cond_pop(struct lp_exec_mask *mask)
166 assert(mask->cond_stack_size);
167 mask->cond_mask = mask->cond_stack[--mask->cond_stack_size];
168 lp_exec_mask_update(mask);
171 static void lp_exec_bgnloop(struct lp_exec_mask *mask)
173 LLVMBuilderRef builder = mask->bld->gallivm->builder;
175 if (mask->loop_stack_size == 0) {
176 assert(mask->loop_block == NULL);
177 assert(mask->cont_mask == LLVMConstAllOnes(mask->int_vec_type));
178 assert(mask->break_mask == LLVMConstAllOnes(mask->int_vec_type));
179 assert(mask->break_var == NULL);
182 assert(mask->loop_stack_size < LP_MAX_TGSI_NESTING);
184 mask->loop_stack[mask->loop_stack_size].loop_block = mask->loop_block;
185 mask->loop_stack[mask->loop_stack_size].cont_mask = mask->cont_mask;
186 mask->loop_stack[mask->loop_stack_size].break_mask = mask->break_mask;
187 mask->loop_stack[mask->loop_stack_size].break_var = mask->break_var;
188 ++mask->loop_stack_size;
190 mask->break_var = lp_build_alloca(mask->bld->gallivm, mask->int_vec_type, "");
191 LLVMBuildStore(builder, mask->break_mask, mask->break_var);
193 mask->loop_block = lp_build_insert_new_block(mask->bld->gallivm, "bgnloop");
195 LLVMBuildBr(builder, mask->loop_block);
196 LLVMPositionBuilderAtEnd(builder, mask->loop_block);
198 mask->break_mask = LLVMBuildLoad(builder, mask->break_var, "");
200 lp_exec_mask_update(mask);
203 static void lp_exec_break(struct lp_exec_mask *mask)
205 LLVMBuilderRef builder = mask->bld->gallivm->builder;
206 LLVMValueRef exec_mask = LLVMBuildNot(builder,
210 mask->break_mask = LLVMBuildAnd(builder,
212 exec_mask, "break_full");
214 lp_exec_mask_update(mask);
217 static void lp_exec_break_condition(struct lp_exec_mask *mask,
220 LLVMBuilderRef builder = mask->bld->gallivm->builder;
221 LLVMValueRef cond_mask = LLVMBuildAnd(builder,
224 cond_mask = LLVMBuildNot(builder, cond, "break_cond");
226 mask->break_mask = LLVMBuildAnd(builder,
228 cond_mask, "breakc_full");
230 lp_exec_mask_update(mask);
233 static void lp_exec_continue(struct lp_exec_mask *mask)
235 LLVMBuilderRef builder = mask->bld->gallivm->builder;
236 LLVMValueRef exec_mask = LLVMBuildNot(builder,
240 mask->cont_mask = LLVMBuildAnd(builder,
244 lp_exec_mask_update(mask);
248 static void lp_exec_endloop(struct gallivm_state *gallivm,
249 struct lp_exec_mask *mask)
251 LLVMBuilderRef builder = mask->bld->gallivm->builder;
252 LLVMBasicBlockRef endloop;
253 LLVMTypeRef int_type = LLVMInt32TypeInContext(mask->bld->gallivm->context);
254 LLVMTypeRef reg_type = LLVMIntTypeInContext(gallivm->context,
255 mask->bld->type.width *
256 mask->bld->type.length);
257 LLVMValueRef i1cond, i2cond, icond, limiter;
259 assert(mask->break_mask);
262 * Restore the cont_mask, but don't pop
264 assert(mask->loop_stack_size);
265 mask->cont_mask = mask->loop_stack[mask->loop_stack_size - 1].cont_mask;
266 lp_exec_mask_update(mask);
269 * Unlike the continue mask, the break_mask must be preserved across loop
272 LLVMBuildStore(builder, mask->break_mask, mask->break_var);
274 /* Decrement the loop limiter */
275 limiter = LLVMBuildLoad(builder, mask->loop_limiter, "");
277 limiter = LLVMBuildSub(
280 LLVMConstInt(int_type, 1, false),
283 LLVMBuildStore(builder, limiter, mask->loop_limiter);
285 /* i1cond = (mask != 0) */
286 i1cond = LLVMBuildICmp(
289 LLVMBuildBitCast(builder, mask->exec_mask, reg_type, ""),
290 LLVMConstNull(reg_type), "i1cond");
292 /* i2cond = (looplimiter > 0) */
293 i2cond = LLVMBuildICmp(
297 LLVMConstNull(int_type), "i2cond");
299 /* if( i1cond && i2cond ) */
300 icond = LLVMBuildAnd(builder, i1cond, i2cond, "");
302 endloop = lp_build_insert_new_block(mask->bld->gallivm, "endloop");
304 LLVMBuildCondBr(builder,
305 icond, mask->loop_block, endloop);
307 LLVMPositionBuilderAtEnd(builder, endloop);
309 assert(mask->loop_stack_size);
310 --mask->loop_stack_size;
311 mask->loop_block = mask->loop_stack[mask->loop_stack_size].loop_block;
312 mask->cont_mask = mask->loop_stack[mask->loop_stack_size].cont_mask;
313 mask->break_mask = mask->loop_stack[mask->loop_stack_size].break_mask;
314 mask->break_var = mask->loop_stack[mask->loop_stack_size].break_var;
316 lp_exec_mask_update(mask);
319 /* stores val into an address pointed to by dst.
320 * mask->exec_mask is used to figure out which bits of val
321 * should be stored into the address
322 * (0 means don't store this bit, 1 means do store).
324 static void lp_exec_mask_store(struct lp_exec_mask *mask,
325 struct lp_build_context *bld_store,
330 LLVMBuilderRef builder = mask->bld->gallivm->builder;
332 /* Mix the predicate and execution mask */
333 if (mask->has_mask) {
335 pred = LLVMBuildAnd(builder, pred, mask->exec_mask, "");
337 pred = mask->exec_mask;
342 LLVMValueRef real_val, dst_val;
344 dst_val = LLVMBuildLoad(builder, dst, "");
345 real_val = lp_build_select(bld_store,
349 LLVMBuildStore(builder, real_val, dst);
351 LLVMBuildStore(builder, val, dst);
354 static void lp_exec_mask_call(struct lp_exec_mask *mask,
358 assert(mask->call_stack_size < LP_MAX_TGSI_NESTING);
359 mask->call_stack[mask->call_stack_size].pc = *pc;
360 mask->call_stack[mask->call_stack_size].ret_mask = mask->ret_mask;
361 mask->call_stack_size++;
365 static void lp_exec_mask_ret(struct lp_exec_mask *mask, int *pc)
367 LLVMBuilderRef builder = mask->bld->gallivm->builder;
368 LLVMValueRef exec_mask;
370 if (mask->cond_stack_size == 0 &&
371 mask->loop_stack_size == 0 &&
372 mask->call_stack_size == 0) {
373 /* returning from main() */
378 if (mask->call_stack_size == 0) {
380 * This requires special handling since we need to ensure
381 * we don't drop the mask even if we have no call stack
382 * (e.g. after a ret in a if clause after the endif)
384 mask->ret_in_main = TRUE;
387 exec_mask = LLVMBuildNot(builder,
391 mask->ret_mask = LLVMBuildAnd(builder,
393 exec_mask, "ret_full");
395 lp_exec_mask_update(mask);
398 static void lp_exec_mask_bgnsub(struct lp_exec_mask *mask)
402 static void lp_exec_mask_endsub(struct lp_exec_mask *mask, int *pc)
404 assert(mask->call_stack_size);
405 mask->call_stack_size--;
406 *pc = mask->call_stack[mask->call_stack_size].pc;
407 mask->ret_mask = mask->call_stack[mask->call_stack_size].ret_mask;
408 lp_exec_mask_update(mask);
413 * Return pointer to a temporary register channel (src or dest).
414 * Note that indirect addressing cannot be handled here.
415 * \param index which temporary register
416 * \param chan which channel of the temp register.
419 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context *bld,
423 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
425 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
426 LLVMValueRef lindex = lp_build_const_int32(bld->bld_base.base.gallivm, index * 4 + chan);
427 return LLVMBuildGEP(builder, bld->temps_array, &lindex, 1, "");
430 return bld->temps[index][chan];
435 * Return pointer to a output register channel (src or dest).
436 * Note that indirect addressing cannot be handled here.
437 * \param index which output register
438 * \param chan which channel of the output register.
441 lp_get_output_ptr(struct lp_build_tgsi_soa_context *bld,
445 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
447 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) {
448 LLVMValueRef lindex = lp_build_const_int32(bld->bld_base.base.gallivm,
450 return LLVMBuildGEP(builder, bld->outputs_array, &lindex, 1, "");
453 return bld->outputs[index][chan];
458 * If we have indirect addressing in outputs copy our alloca array
459 * to the outputs slots specified by the caller to make sure
460 * our outputs are delivered consistently via the same interface.
463 gather_outputs(struct lp_build_tgsi_soa_context * bld)
465 if ((bld->indirect_files & (1 << TGSI_FILE_OUTPUT))) {
466 unsigned index, chan;
467 assert(bld->bld_base.info->num_outputs <=
468 bld->bld_base.info->file_max[TGSI_FILE_OUTPUT] + 1);
469 for (index = 0; index < bld->bld_base.info->num_outputs; ++index) {
470 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
471 bld->outputs[index][chan] = lp_get_output_ptr(bld, index, chan);
479 * XXX the lp_build_gather() function should be capable of doing this
480 * with a little work.
483 build_gather(struct lp_build_context *bld,
484 LLVMValueRef base_ptr,
485 LLVMValueRef indexes)
487 LLVMBuilderRef builder = bld->gallivm->builder;
488 LLVMValueRef res = bld->undef;
492 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
494 for (i = 0; i < bld->type.length; i++) {
495 LLVMValueRef ii = lp_build_const_int32(bld->gallivm, i);
496 LLVMValueRef index = LLVMBuildExtractElement(builder,
498 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr,
499 &index, 1, "gather_ptr");
500 LLVMValueRef scalar = LLVMBuildLoad(builder, scalar_ptr, "");
502 res = LLVMBuildInsertElement(builder, res, scalar, ii, "");
510 * Scatter/store vector.
513 emit_mask_scatter(struct lp_build_tgsi_soa_context *bld,
514 LLVMValueRef base_ptr,
515 LLVMValueRef indexes,
517 struct lp_exec_mask *mask,
520 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
521 LLVMBuilderRef builder = gallivm->builder;
524 /* Mix the predicate and execution mask */
525 if (mask->has_mask) {
527 pred = LLVMBuildAnd(builder, pred, mask->exec_mask, "");
530 pred = mask->exec_mask;
535 * Loop over elements of index_vec, store scalar value.
537 for (i = 0; i < bld->bld_base.base.type.length; i++) {
538 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
539 LLVMValueRef index = LLVMBuildExtractElement(builder, indexes, ii, "");
540 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr, &index, 1, "scatter_ptr");
541 LLVMValueRef val = LLVMBuildExtractElement(builder, values, ii, "scatter_val");
542 LLVMValueRef scalar_pred = pred ?
543 LLVMBuildExtractElement(builder, pred, ii, "scatter_pred") : NULL;
546 lp_build_printf(gallivm, "scatter %d: val %f at %d %p\n",
547 ii, val, index, scalar_ptr);
550 LLVMValueRef real_val, dst_val;
551 dst_val = LLVMBuildLoad(builder, scalar_ptr, "");
552 real_val = lp_build_select(&bld->elem_bld, scalar_pred, val, dst_val);
553 LLVMBuildStore(builder, real_val, scalar_ptr);
556 LLVMBuildStore(builder, val, scalar_ptr);
563 * Read the current value of the ADDR register, convert the floats to
564 * ints, add the base index and return the vector of offsets.
565 * The offsets will be used to index into the constant buffer or
566 * temporary register file.
569 get_indirect_index(struct lp_build_tgsi_soa_context *bld,
570 unsigned reg_file, unsigned reg_index,
571 const struct tgsi_ind_register *indirect_reg)
573 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
574 struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;
575 /* always use X component of address register */
576 unsigned swizzle = indirect_reg->Swizzle;
579 LLVMValueRef max_index;
582 assert(bld->indirect_files & (1 << reg_file));
584 base = lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, reg_index);
587 switch (indirect_reg->File) {
588 case TGSI_FILE_ADDRESS:
589 rel = LLVMBuildLoad(builder,
590 bld->addr[indirect_reg->Index][swizzle],
592 /* ADDR LLVM values already have LLVM integer type. */
594 case TGSI_FILE_TEMPORARY:
595 rel = lp_get_temp_ptr_soa(bld, indirect_reg->Index, swizzle);
596 rel = LLVMBuildLoad(builder, rel, "load temp reg");
597 /* TEMP LLVM values always have LLVM float type, but for indirection, the
598 * value actually stored is expected to be an integer */
599 rel = LLVMBuildBitCast(builder, rel, uint_bld->vec_type, "");
603 rel = uint_bld->zero;
606 index = lp_build_add(uint_bld, base, rel);
608 max_index = lp_build_const_int_vec(bld->bld_base.base.gallivm,
610 bld->bld_base.info->file_max[reg_file]);
612 assert(!uint_bld->type.sign);
613 index = lp_build_min(uint_bld, index, max_index);
618 static struct lp_build_context *
619 stype_to_fetch(struct lp_build_tgsi_context * bld_base,
620 enum tgsi_opcode_type stype)
622 struct lp_build_context *bld_fetch;
625 case TGSI_TYPE_FLOAT:
626 case TGSI_TYPE_UNTYPED:
627 bld_fetch = &bld_base->base;
629 case TGSI_TYPE_UNSIGNED:
630 bld_fetch = &bld_base->uint_bld;
632 case TGSI_TYPE_SIGNED:
633 bld_fetch = &bld_base->int_bld;
636 case TGSI_TYPE_DOUBLE:
647 struct lp_build_tgsi_context * bld_base,
648 const struct tgsi_full_src_register * reg,
649 enum tgsi_opcode_type stype,
652 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
653 struct gallivm_state *gallivm = bld_base->base.gallivm;
654 LLVMBuilderRef builder = gallivm->builder;
655 struct lp_build_context *uint_bld = &bld_base->uint_bld;
656 LLVMValueRef indirect_index = NULL;
657 unsigned dimension = 0;
658 LLVMValueRef dimension_index;
659 LLVMValueRef consts_ptr;
662 /* XXX: Handle fetching xyzw components as a vector */
663 assert(swizzle != ~0);
665 if (reg->Register.Dimension) {
666 assert(!reg->Dimension.Indirect);
667 dimension = reg->Dimension.Index;
668 assert(dimension < LP_MAX_TGSI_CONST_BUFFERS);
671 dimension_index = lp_build_const_int32(gallivm, dimension);
672 consts_ptr = lp_build_array_get(gallivm, bld->consts_ptr, dimension_index);
674 if (reg->Register.Indirect) {
675 indirect_index = get_indirect_index(bld,
681 if (reg->Register.Indirect) {
682 LLVMValueRef swizzle_vec =
683 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, swizzle);
684 LLVMValueRef index_vec; /* index into the const buffer */
686 /* index_vec = indirect_index * 4 + swizzle */
687 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
688 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
690 /* Gather values from the constant buffer */
691 res = build_gather(&bld_base->base, consts_ptr, index_vec);
694 LLVMValueRef index; /* index into the const buffer */
695 LLVMValueRef scalar, scalar_ptr;
697 index = lp_build_const_int32(gallivm, reg->Register.Index*4 + swizzle);
699 scalar_ptr = LLVMBuildGEP(builder, consts_ptr,
701 scalar = LLVMBuildLoad(builder, scalar_ptr, "");
702 res = lp_build_broadcast_scalar(&bld_base->base, scalar);
705 if (stype == TGSI_TYPE_SIGNED || stype == TGSI_TYPE_UNSIGNED) {
706 struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
707 res = LLVMBuildBitCast(builder, res, bld_fetch->vec_type, "");
713 emit_fetch_immediate(
714 struct lp_build_tgsi_context * bld_base,
715 const struct tgsi_full_src_register * reg,
716 enum tgsi_opcode_type stype,
719 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
720 LLVMValueRef res = bld->immediates[reg->Register.Index][swizzle];
723 if (stype == TGSI_TYPE_UNSIGNED) {
724 res = LLVMConstBitCast(res, bld_base->uint_bld.vec_type);
725 } else if (stype == TGSI_TYPE_SIGNED) {
726 res = LLVMConstBitCast(res, bld_base->int_bld.vec_type);
733 struct lp_build_tgsi_context * bld_base,
734 const struct tgsi_full_src_register * reg,
735 enum tgsi_opcode_type stype,
738 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
739 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
740 LLVMBuilderRef builder = gallivm->builder;
741 struct lp_build_context *uint_bld = &bld_base->uint_bld;
742 LLVMValueRef indirect_index = NULL;
745 if (reg->Register.Indirect) {
746 indirect_index = get_indirect_index(bld,
752 if (reg->Register.Indirect) {
753 LLVMValueRef swizzle_vec =
754 lp_build_const_int_vec(gallivm, uint_bld->type, swizzle);
755 LLVMValueRef length_vec =
756 lp_build_const_int_vec(gallivm, uint_bld->type, bld->bld_base.base.type.length);
757 LLVMValueRef index_vec; /* index into the const buffer */
758 LLVMValueRef inputs_array;
759 LLVMTypeRef float4_ptr_type;
761 /* index_vec = (indirect_index * 4 + swizzle) * length */
762 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
763 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
764 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
766 /* cast inputs_array pointer to float* */
767 float4_ptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
768 inputs_array = LLVMBuildBitCast(builder, bld->inputs_array,
769 float4_ptr_type, "");
771 /* Gather values from the temporary register array */
772 res = build_gather(&bld_base->base, inputs_array, index_vec);
774 if (bld->indirect_files & (1 << TGSI_FILE_INPUT)) {
775 LLVMValueRef lindex = lp_build_const_int32(gallivm,
776 reg->Register.Index * 4 + swizzle);
777 LLVMValueRef input_ptr = LLVMBuildGEP(builder,
778 bld->inputs_array, &lindex, 1, "");
779 res = LLVMBuildLoad(builder, input_ptr, "");
782 res = bld->inputs[reg->Register.Index][swizzle];
788 if (stype == TGSI_TYPE_UNSIGNED) {
789 res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
790 } else if (stype == TGSI_TYPE_SIGNED) {
791 res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
800 struct lp_build_tgsi_context * bld_base,
801 const struct tgsi_full_src_register * reg,
802 enum tgsi_opcode_type stype,
805 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
806 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
807 LLVMBuilderRef builder = gallivm->builder;
808 LLVMValueRef attrib_index = NULL;
809 LLVMValueRef vertex_index = NULL;
810 LLVMValueRef swizzle_index = lp_build_const_int32(gallivm, swizzle);
813 if (reg->Register.Indirect) {
814 attrib_index = get_indirect_index(bld,
819 attrib_index = lp_build_const_int32(gallivm, reg->Register.Index);
822 if (reg->Dimension.Indirect) {
823 vertex_index = get_indirect_index(bld,
825 reg->Dimension.Index,
828 vertex_index = lp_build_const_int32(gallivm, reg->Dimension.Index);
832 res = bld->gs_iface->fetch_input(bld->gs_iface, bld_base,
833 vertex_index, attrib_index,
838 if (stype == TGSI_TYPE_UNSIGNED) {
839 res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
840 } else if (stype == TGSI_TYPE_SIGNED) {
841 res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
848 emit_fetch_temporary(
849 struct lp_build_tgsi_context * bld_base,
850 const struct tgsi_full_src_register * reg,
851 enum tgsi_opcode_type stype,
854 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
855 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
856 LLVMBuilderRef builder = gallivm->builder;
857 struct lp_build_context *uint_bld = &bld_base->uint_bld;
858 LLVMValueRef indirect_index = NULL;
861 if (reg->Register.Indirect) {
862 indirect_index = get_indirect_index(bld,
868 if (reg->Register.Indirect) {
869 LLVMValueRef swizzle_vec =
870 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, swizzle);
871 LLVMValueRef length_vec =
872 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type,
873 bld->bld_base.base.type.length);
874 LLVMValueRef index_vec; /* index into the const buffer */
875 LLVMValueRef temps_array;
876 LLVMTypeRef float4_ptr_type;
878 /* index_vec = (indirect_index * 4 + swizzle) * length */
879 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
880 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
881 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
883 /* cast temps_array pointer to float* */
884 float4_ptr_type = LLVMPointerType(LLVMFloatTypeInContext(bld->bld_base.base.gallivm->context), 0);
885 temps_array = LLVMBuildBitCast(builder, bld->temps_array,
886 float4_ptr_type, "");
888 /* Gather values from the temporary register array */
889 res = build_gather(&bld_base->base, temps_array, index_vec);
892 LLVMValueRef temp_ptr;
893 temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, swizzle);
894 res = LLVMBuildLoad(builder, temp_ptr, "");
897 if (stype == TGSI_TYPE_SIGNED || stype == TGSI_TYPE_UNSIGNED) {
898 struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
899 res = LLVMBuildBitCast(builder, res, bld_fetch->vec_type, "");
906 emit_fetch_system_value(
907 struct lp_build_tgsi_context * bld_base,
908 const struct tgsi_full_src_register * reg,
909 enum tgsi_opcode_type stype,
912 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
913 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
914 const struct tgsi_shader_info *info = bld->bld_base.info;
915 LLVMBuilderRef builder = gallivm->builder;
917 enum tgsi_opcode_type atype; // Actual type of the value
919 assert(!reg->Register.Indirect);
921 switch (info->system_value_semantic_name[reg->Register.Index]) {
922 case TGSI_SEMANTIC_INSTANCEID:
923 res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.instance_id);
924 atype = TGSI_TYPE_UNSIGNED;
927 case TGSI_SEMANTIC_VERTEXID:
928 res = bld->system_values.vertex_id;
929 atype = TGSI_TYPE_UNSIGNED;
932 case TGSI_SEMANTIC_PRIMID:
933 res = bld->system_values.prim_id;
934 atype = TGSI_TYPE_UNSIGNED;
938 assert(!"unexpected semantic in emit_fetch_system_value");
939 res = bld_base->base.zero;
940 atype = TGSI_TYPE_FLOAT;
944 if (atype != stype) {
945 if (stype == TGSI_TYPE_FLOAT) {
946 res = LLVMBuildBitCast(builder, res, bld_base->base.vec_type, "");
947 } else if (stype == TGSI_TYPE_UNSIGNED) {
948 res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
949 } else if (stype == TGSI_TYPE_SIGNED) {
950 res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
958 * Register fetch with derivatives.
962 struct lp_build_tgsi_soa_context *bld,
971 /* TODO: use interpolation coeffs for inputs */
974 *ddx = lp_build_ddx(&bld->bld_base.base, src);
977 *ddy = lp_build_ddy(&bld->bld_base.base, src);
985 emit_fetch_predicate(
986 struct lp_build_tgsi_soa_context *bld,
987 const struct tgsi_full_instruction *inst,
990 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
992 unsigned char swizzles[4];
993 LLVMValueRef unswizzled[4] = {NULL, NULL, NULL, NULL};
997 if (!inst->Instruction.Predicate) {
998 TGSI_FOR_EACH_CHANNEL( chan ) {
1004 swizzles[0] = inst->Predicate.SwizzleX;
1005 swizzles[1] = inst->Predicate.SwizzleY;
1006 swizzles[2] = inst->Predicate.SwizzleZ;
1007 swizzles[3] = inst->Predicate.SwizzleW;
1009 index = inst->Predicate.Index;
1010 assert(index < LP_MAX_TGSI_PREDS);
1012 TGSI_FOR_EACH_CHANNEL( chan ) {
1013 unsigned swizzle = swizzles[chan];
1016 * Only fetch the predicate register channels that are actually listed
1019 if (!unswizzled[swizzle]) {
1020 value = LLVMBuildLoad(builder,
1021 bld->preds[index][swizzle], "");
1024 * Convert the value to an integer mask.
1026 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
1027 * is needlessly causing two comparisons due to storing the intermediate
1028 * result as float vector instead of an integer mask vector.
1030 value = lp_build_compare(bld->bld_base.base.gallivm,
1031 bld->bld_base.base.type,
1034 bld->bld_base.base.zero);
1035 if (inst->Predicate.Negate) {
1036 value = LLVMBuildNot(builder, value, "");
1039 unswizzled[swizzle] = value;
1041 value = unswizzled[swizzle];
1053 struct lp_build_tgsi_context *bld_base,
1054 const struct tgsi_full_instruction *inst,
1056 unsigned chan_index,
1060 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1061 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1062 LLVMBuilderRef builder = gallivm->builder;
1063 const struct tgsi_full_dst_register *reg = &inst->Dst[index];
1064 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1065 LLVMValueRef indirect_index = NULL;
1066 struct lp_build_context *bld_store;
1067 enum tgsi_opcode_type dtype = tgsi_opcode_infer_dst_type(inst->Instruction.Opcode);
1071 case TGSI_TYPE_FLOAT:
1072 case TGSI_TYPE_UNTYPED:
1073 bld_store = &bld_base->base;
1075 case TGSI_TYPE_UNSIGNED:
1076 bld_store = &bld_base->uint_bld;
1078 case TGSI_TYPE_SIGNED:
1079 bld_store = &bld_base->int_bld;
1081 case TGSI_TYPE_DOUBLE:
1082 case TGSI_TYPE_VOID:
1088 /* If the destination is untyped then the source can be anything,
1089 * but LLVM won't like if the types don't match so lets cast
1090 * to the correct destination type as expected by LLVM. */
1091 if (dtype == TGSI_TYPE_UNTYPED &&
1092 !lp_check_vec_type(bld_store->type, LLVMTypeOf(value))) {
1093 value = LLVMBuildBitCast(builder, value, bld_store->vec_type,
1097 switch( inst->Instruction.Saturate ) {
1101 case TGSI_SAT_ZERO_ONE:
1102 value = lp_build_max(&bld->bld_base.base, value, bld->bld_base.base.zero);
1103 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one);
1106 case TGSI_SAT_MINUS_PLUS_ONE:
1107 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));
1108 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one);
1115 if (reg->Register.Indirect) {
1116 indirect_index = get_indirect_index(bld,
1118 reg->Register.Index,
1121 assert(reg->Register.Index <=
1122 bld->bld_base.info->file_max[reg->Register.File]);
1125 switch( reg->Register.File ) {
1126 case TGSI_FILE_OUTPUT:
1127 if (reg->Register.Indirect) {
1128 LLVMValueRef chan_vec =
1129 lp_build_const_int_vec(gallivm, uint_bld->type, chan_index);
1130 LLVMValueRef length_vec =
1131 lp_build_const_int_vec(gallivm, uint_bld->type, bld->bld_base.base.type.length);
1132 LLVMValueRef index_vec; /* indexes into the temp registers */
1133 LLVMValueRef outputs_array;
1134 LLVMValueRef pixel_offsets;
1135 LLVMTypeRef float_ptr_type;
1138 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1139 pixel_offsets = uint_bld->undef;
1140 for (i = 0; i < bld->bld_base.base.type.length; i++) {
1141 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
1142 pixel_offsets = LLVMBuildInsertElement(builder, pixel_offsets,
1146 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1147 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
1148 index_vec = lp_build_add(uint_bld, index_vec, chan_vec);
1149 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
1150 index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets);
1153 LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1154 outputs_array = LLVMBuildBitCast(builder, bld->outputs_array,
1155 float_ptr_type, "");
1157 /* Scatter store values into temp registers */
1158 emit_mask_scatter(bld, outputs_array, index_vec, value,
1159 &bld->exec_mask, pred);
1162 LLVMValueRef out_ptr = lp_get_output_ptr(bld, reg->Register.Index,
1164 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, out_ptr);
1168 case TGSI_FILE_TEMPORARY:
1169 if (reg->Register.Indirect) {
1170 LLVMValueRef chan_vec =
1171 lp_build_const_int_vec(gallivm, uint_bld->type, chan_index);
1172 LLVMValueRef length_vec =
1173 lp_build_const_int_vec(gallivm, uint_bld->type,
1174 bld->bld_base.base.type.length);
1175 LLVMValueRef index_vec; /* indexes into the temp registers */
1176 LLVMValueRef temps_array;
1177 LLVMValueRef pixel_offsets;
1178 LLVMTypeRef float_ptr_type;
1181 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1182 pixel_offsets = uint_bld->undef;
1183 for (i = 0; i < bld->bld_base.base.type.length; i++) {
1184 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
1185 pixel_offsets = LLVMBuildInsertElement(builder, pixel_offsets,
1189 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1190 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
1191 index_vec = lp_build_add(uint_bld, index_vec, chan_vec);
1192 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
1193 index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets);
1196 LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1197 temps_array = LLVMBuildBitCast(builder, bld->temps_array,
1198 float_ptr_type, "");
1200 /* Scatter store values into temp registers */
1201 emit_mask_scatter(bld, temps_array, index_vec, value,
1202 &bld->exec_mask, pred);
1205 LLVMValueRef temp_ptr;
1208 case TGSI_TYPE_UNSIGNED:
1209 case TGSI_TYPE_SIGNED: {
1210 LLVMTypeRef itype = bld_base->int_bld.vec_type;
1211 LLVMTypeRef ivtype = LLVMPointerType(itype, 0);
1212 LLVMValueRef tint_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index,
1214 LLVMValueRef temp_value_ptr;
1216 temp_ptr = LLVMBuildBitCast(builder, tint_ptr, ivtype, "");
1217 temp_value_ptr = LLVMBuildBitCast(builder, value, itype, "");
1218 value = temp_value_ptr;
1222 case TGSI_TYPE_FLOAT:
1223 case TGSI_TYPE_UNTYPED:
1224 temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index,
1229 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, temp_ptr);
1233 case TGSI_FILE_ADDRESS:
1234 assert(dtype == TGSI_TYPE_SIGNED);
1235 assert(LLVMTypeOf(value) == bld_base->base.int_vec_type);
1236 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value,
1237 bld->addr[reg->Register.Index][chan_index]);
1240 case TGSI_FILE_PREDICATE:
1241 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value,
1242 bld->preds[reg->Register.Index][chan_index]);
1252 struct lp_build_tgsi_context * bld_base,
1253 const struct tgsi_full_instruction * inst,
1254 const struct tgsi_opcode_info * info,
1255 LLVMValueRef dst[4])
1258 unsigned chan_index;
1259 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1262 LLVMValueRef pred[TGSI_NUM_CHANNELS];
1264 emit_fetch_predicate( bld, inst, pred );
1266 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1267 emit_store_chan(bld_base, inst, 0, chan_index, pred[chan_index], dst[chan_index]);
1273 * High-level instruction translators.
1277 emit_tex( struct lp_build_tgsi_soa_context *bld,
1278 const struct tgsi_full_instruction *inst,
1279 enum lp_build_tex_modifier modifier,
1280 LLVMValueRef *texel)
1283 LLVMValueRef lod_bias, explicit_lod;
1284 LLVMValueRef oow = NULL;
1285 LLVMValueRef coords[4];
1286 LLVMValueRef offsets[3] = { NULL };
1287 struct lp_derivatives derivs;
1288 struct lp_derivatives *deriv_ptr = NULL;
1289 unsigned num_coords, num_derivs, num_offsets;
1292 if (!bld->sampler) {
1293 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1294 for (i = 0; i < 4; i++) {
1295 texel[i] = bld->bld_base.base.undef;
1300 switch (inst->Texture.Texture) {
1301 case TGSI_TEXTURE_1D:
1306 case TGSI_TEXTURE_1D_ARRAY:
1311 case TGSI_TEXTURE_2D:
1312 case TGSI_TEXTURE_RECT:
1317 case TGSI_TEXTURE_SHADOW1D:
1318 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1323 case TGSI_TEXTURE_SHADOW2D:
1324 case TGSI_TEXTURE_SHADOWRECT:
1325 case TGSI_TEXTURE_2D_ARRAY:
1330 case TGSI_TEXTURE_CUBE:
1335 case TGSI_TEXTURE_3D:
1340 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1345 case TGSI_TEXTURE_SHADOWCUBE:
1355 /* Note lod and especially projected are illegal in a LOT of cases */
1356 if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS) {
1357 assert(num_coords < 4);
1358 lod_bias = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1359 explicit_lod = NULL;
1361 else if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
1362 assert(num_coords < 4);
1364 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1368 explicit_lod = NULL;
1371 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED) {
1372 assert(num_coords < 4);
1373 oow = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1374 oow = lp_build_rcp(&bld->bld_base.base, oow);
1377 for (i = 0; i < num_coords; i++) {
1378 coords[i] = lp_build_emit_fetch( &bld->bld_base, inst, 0, i );
1379 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED)
1380 coords[i] = lp_build_mul(&bld->bld_base.base, coords[i], oow);
1382 for (i = num_coords; i < 4; i++) {
1383 coords[i] = bld->bld_base.base.undef;
1386 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
1388 for (dim = 0; dim < num_derivs; ++dim) {
1389 derivs.ddx[dim] = lp_build_emit_fetch( &bld->bld_base, inst, 1, dim );
1390 derivs.ddy[dim] = lp_build_emit_fetch( &bld->bld_base, inst, 2, dim );
1392 deriv_ptr = &derivs;
1393 unit = inst->Src[3].Register.Index;
1395 unit = inst->Src[1].Register.Index;
1398 /* some advanced gather instructions (txgo) would require 4 offsets */
1399 if (inst->Texture.NumOffsets == 1) {
1401 for (dim = 0; dim < num_offsets; dim++) {
1402 offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim );
1406 bld->sampler->emit_fetch_texel(bld->sampler,
1407 bld->bld_base.base.gallivm,
1408 bld->bld_base.base.type,
1414 lod_bias, explicit_lod,
1419 emit_sample(struct lp_build_tgsi_soa_context *bld,
1420 const struct tgsi_full_instruction *inst,
1421 enum lp_build_tex_modifier modifier,
1423 LLVMValueRef *texel)
1425 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1426 unsigned texture_unit, sampler_unit;
1427 LLVMValueRef lod_bias, explicit_lod;
1428 LLVMValueRef coords[4];
1429 LLVMValueRef offsets[3] = { NULL };
1430 struct lp_derivatives derivs;
1431 struct lp_derivatives *deriv_ptr = NULL;
1432 unsigned num_coords, num_offsets, num_derivs;
1435 if (!bld->sampler) {
1436 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1437 for (i = 0; i < 4; i++) {
1438 texel[i] = bld->bld_base.base.undef;
1444 * unlike old-style tex opcodes the texture/sampler indices
1445 * always come from src1 and src2 respectively.
1447 texture_unit = inst->Src[1].Register.Index;
1448 sampler_unit = inst->Src[2].Register.Index;
1451 * Note inst->Texture.Texture will contain the number of offsets,
1452 * however the target information is NOT there and comes from the
1453 * declared sampler views instead.
1455 switch (bld->sv[texture_unit].Resource) {
1456 case TGSI_TEXTURE_1D:
1461 case TGSI_TEXTURE_1D_ARRAY:
1466 case TGSI_TEXTURE_2D:
1467 case TGSI_TEXTURE_RECT:
1472 case TGSI_TEXTURE_2D_ARRAY:
1477 case TGSI_TEXTURE_CUBE:
1482 case TGSI_TEXTURE_3D:
1487 case TGSI_TEXTURE_CUBE_ARRAY:
1498 * unlike old-style tex opcodes the texture/sampler indices
1499 * always come from src1 and src2 respectively.
1501 texture_unit = inst->Src[1].Register.Index;
1502 sampler_unit = inst->Src[2].Register.Index;
1504 if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS) {
1505 lod_bias = lp_build_emit_fetch( &bld->bld_base, inst, 3, 0 );
1506 explicit_lod = NULL;
1508 else if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
1510 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 3, 0 );
1512 else if (modifier == LP_BLD_TEX_MODIFIER_LOD_ZERO) {
1514 /* XXX might be better to explicitly pass the level zero information */
1515 explicit_lod = lp_build_const_vec(gallivm, bld->bld_base.base.type, 0.0F);
1519 explicit_lod = NULL;
1522 for (i = 0; i < num_coords; i++) {
1523 coords[i] = lp_build_emit_fetch( &bld->bld_base, inst, 0, i );
1525 for (i = num_coords; i < 4; i++) {
1526 coords[i] = bld->bld_base.base.undef;
1529 * XXX: whack shadow comparison value into place.
1530 * Should probably fix the interface for separate value
1531 * (it will not work for cube arrays if it is part of coords).
1534 unsigned c_coord = num_coords > 2 ? 3 : 2;
1535 assert(num_coords < 4);
1536 coords[c_coord] = lp_build_emit_fetch( &bld->bld_base, inst, 3, 0 );
1539 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
1541 for (dim = 0; dim < num_derivs; ++dim) {
1542 derivs.ddx[dim] = lp_build_emit_fetch( &bld->bld_base, inst, 3, dim );
1543 derivs.ddy[dim] = lp_build_emit_fetch( &bld->bld_base, inst, 4, dim );
1545 deriv_ptr = &derivs;
1548 /* some advanced gather instructions (txgo) would require 4 offsets */
1549 if (inst->Texture.NumOffsets == 1) {
1551 for (dim = 0; dim < num_offsets; dim++) {
1552 offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim );
1556 bld->sampler->emit_fetch_texel(bld->sampler,
1557 bld->bld_base.base.gallivm,
1558 bld->bld_base.base.type,
1560 texture_unit, sampler_unit,
1564 lod_bias, explicit_lod,
1569 emit_fetch_texels( struct lp_build_tgsi_soa_context *bld,
1570 const struct tgsi_full_instruction *inst,
1571 LLVMValueRef *texel,
1574 unsigned unit, target;
1575 LLVMValueRef coord_undef = LLVMGetUndef(bld->bld_base.base.int_vec_type);
1576 LLVMValueRef explicit_lod = NULL;
1577 LLVMValueRef coords[3];
1578 LLVMValueRef offsets[3] = { NULL };
1579 unsigned num_coords;
1583 if (!bld->sampler) {
1584 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1585 for (i = 0; i < 4; i++) {
1586 texel[i] = coord_undef;
1591 unit = inst->Src[1].Register.Index;
1594 target = bld->sv[unit].Resource;
1597 target = inst->Texture.Texture;
1601 case TGSI_TEXTURE_1D:
1602 case TGSI_TEXTURE_BUFFER:
1606 case TGSI_TEXTURE_1D_ARRAY:
1610 case TGSI_TEXTURE_2D:
1611 case TGSI_TEXTURE_RECT:
1615 case TGSI_TEXTURE_2D_ARRAY:
1619 case TGSI_TEXTURE_3D:
1628 /* always have lod except for buffers ? */
1629 if (target != TGSI_TEXTURE_BUFFER) {
1630 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1633 for (i = 0; i < num_coords; i++) {
1634 coords[i] = lp_build_emit_fetch( &bld->bld_base, inst, 0, i );
1636 for (i = num_coords; i < 3; i++) {
1637 coords[i] = coord_undef;
1640 if (inst->Texture.NumOffsets == 1) {
1642 for (dim = 0; dim < dims; dim++) {
1643 offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim );
1647 bld->sampler->emit_fetch_texel(bld->sampler,
1648 bld->bld_base.base.gallivm,
1649 bld->bld_base.base.type,
1660 emit_size_query( struct lp_build_tgsi_soa_context *bld,
1661 const struct tgsi_full_instruction *inst,
1662 LLVMValueRef *sizes_out,
1663 boolean is_sviewinfo)
1665 LLVMValueRef explicit_lod;
1668 unsigned unit = inst->Src[1].Register.Index;
1672 target = bld->sv[unit].Resource;
1675 target = inst->Texture.Texture;
1678 case TGSI_TEXTURE_BUFFER:
1679 case TGSI_TEXTURE_RECT:
1680 case TGSI_TEXTURE_SHADOWRECT:
1688 if (!bld->sampler) {
1689 _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
1690 for (i = 0; i < 4; i++)
1691 sizes_out[i] = bld->bld_base.int_bld.undef;
1696 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 0, 0 );
1698 explicit_lod = NULL;
1700 bld->sampler->emit_size_query(bld->sampler,
1701 bld->bld_base.base.gallivm,
1702 bld->bld_base.int_bld.type,
1710 near_end_of_shader(struct lp_build_tgsi_soa_context *bld,
1715 for (i = 0; i < 5; i++) {
1718 if (pc + i >= bld->bld_base.info->num_instructions)
1721 opcode = bld->bld_base.instructions[pc + i].Instruction.Opcode;
1723 if (opcode == TGSI_OPCODE_END)
1726 if (opcode == TGSI_OPCODE_TEX ||
1727 opcode == TGSI_OPCODE_TXP ||
1728 opcode == TGSI_OPCODE_TXD ||
1729 opcode == TGSI_OPCODE_TXB ||
1730 opcode == TGSI_OPCODE_TXL ||
1731 opcode == TGSI_OPCODE_TXF ||
1732 opcode == TGSI_OPCODE_TXQ ||
1733 opcode == TGSI_OPCODE_CAL ||
1734 opcode == TGSI_OPCODE_CALLNZ ||
1735 opcode == TGSI_OPCODE_IF ||
1736 opcode == TGSI_OPCODE_UIF ||
1737 opcode == TGSI_OPCODE_BGNLOOP ||
1738 opcode == TGSI_OPCODE_SWITCH)
1748 * Kill fragment if any of the src register values are negative.
1752 struct lp_build_tgsi_soa_context *bld,
1753 const struct tgsi_full_instruction *inst,
1756 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1757 const struct tgsi_full_src_register *reg = &inst->Src[0];
1758 LLVMValueRef terms[TGSI_NUM_CHANNELS];
1760 unsigned chan_index;
1762 memset(&terms, 0, sizeof terms);
1764 TGSI_FOR_EACH_CHANNEL( chan_index ) {
1767 /* Unswizzle channel */
1768 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
1770 /* Check if the component has not been already tested. */
1771 assert(swizzle < TGSI_NUM_CHANNELS);
1772 if( !terms[swizzle] )
1773 /* TODO: change the comparison operator instead of setting the sign */
1774 terms[swizzle] = lp_build_emit_fetch(&bld->bld_base, inst, 0, chan_index );
1778 TGSI_FOR_EACH_CHANNEL( chan_index ) {
1779 if(terms[chan_index]) {
1780 LLVMValueRef chan_mask;
1783 * If term < 0 then mask = 0 else mask = ~0.
1785 chan_mask = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->bld_base.base.zero);
1788 mask = LLVMBuildAnd(builder, mask, chan_mask, "");
1795 lp_build_mask_update(bld->mask, mask);
1797 if (!near_end_of_shader(bld, pc))
1798 lp_build_mask_check(bld->mask);
1804 * Predicated fragment kill.
1805 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1806 * The only predication is the execution mask which will apply if
1807 * we're inside a loop or conditional.
1810 emit_kilp(struct lp_build_tgsi_soa_context *bld,
1813 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1816 /* For those channels which are "alive", disable fragment shader
1819 if (bld->exec_mask.has_mask) {
1820 mask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
1823 LLVMValueRef zero = LLVMConstNull(bld->bld_base.base.int_vec_type);
1827 lp_build_mask_update(bld->mask, mask);
1829 if (!near_end_of_shader(bld, pc))
1830 lp_build_mask_check(bld->mask);
1835 * Emit code which will dump the value of all the temporary registers
1839 emit_dump_temps(struct lp_build_tgsi_soa_context *bld)
1841 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1842 LLVMBuilderRef builder = gallivm->builder;
1843 LLVMValueRef temp_ptr;
1844 LLVMValueRef i0 = lp_build_const_int32(gallivm, 0);
1845 LLVMValueRef i1 = lp_build_const_int32(gallivm, 1);
1846 LLVMValueRef i2 = lp_build_const_int32(gallivm, 2);
1847 LLVMValueRef i3 = lp_build_const_int32(gallivm, 3);
1849 int n = bld->bld_base.info->file_max[TGSI_FILE_TEMPORARY];
1851 for (index = 0; index < n; index++) {
1852 LLVMValueRef idx = lp_build_const_int32(gallivm, index);
1853 LLVMValueRef v[4][4], res;
1856 lp_build_printf(gallivm, "TEMP[%d]:\n", idx);
1858 for (chan = 0; chan < 4; chan++) {
1859 temp_ptr = lp_get_temp_ptr_soa(bld, index, chan);
1860 res = LLVMBuildLoad(builder, temp_ptr, "");
1861 v[chan][0] = LLVMBuildExtractElement(builder, res, i0, "");
1862 v[chan][1] = LLVMBuildExtractElement(builder, res, i1, "");
1863 v[chan][2] = LLVMBuildExtractElement(builder, res, i2, "");
1864 v[chan][3] = LLVMBuildExtractElement(builder, res, i3, "");
1867 lp_build_printf(gallivm, " X: %f %f %f %f\n",
1868 v[0][0], v[0][1], v[0][2], v[0][3]);
1869 lp_build_printf(gallivm, " Y: %f %f %f %f\n",
1870 v[1][0], v[1][1], v[1][2], v[1][3]);
1871 lp_build_printf(gallivm, " Z: %f %f %f %f\n",
1872 v[2][0], v[2][1], v[2][2], v[2][3]);
1873 lp_build_printf(gallivm, " W: %f %f %f %f\n",
1874 v[3][0], v[3][1], v[3][2], v[3][3]);
1881 lp_emit_declaration_soa(
1882 struct lp_build_tgsi_context *bld_base,
1883 const struct tgsi_full_declaration *decl)
1885 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1886 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1887 LLVMTypeRef vec_type = bld->bld_base.base.vec_type;
1888 const unsigned first = decl->Range.First;
1889 const unsigned last = decl->Range.Last;
1892 for (idx = first; idx <= last; ++idx) {
1893 assert(last <= bld->bld_base.info->file_max[decl->Declaration.File]);
1894 switch (decl->Declaration.File) {
1895 case TGSI_FILE_TEMPORARY:
1896 assert(idx < LP_MAX_TGSI_TEMPS);
1897 if (!(bld->indirect_files & (1 << TGSI_FILE_TEMPORARY))) {
1898 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1899 bld->temps[idx][i] = lp_build_alloca(gallivm, vec_type, "temp");
1903 case TGSI_FILE_OUTPUT:
1904 if (!(bld->indirect_files & (1 << TGSI_FILE_OUTPUT))) {
1905 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1906 bld->outputs[idx][i] = lp_build_alloca(gallivm,
1907 vec_type, "output");
1911 case TGSI_FILE_ADDRESS:
1912 /* ADDR registers are only allocated with an integer LLVM IR type,
1913 * as they are guaranteed to always have integers.
1914 * XXX: Not sure if this exception is worthwhile (or the whole idea of
1915 * an ADDR register for that matter).
1917 assert(idx < LP_MAX_TGSI_ADDRS);
1918 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1919 bld->addr[idx][i] = lp_build_alloca(gallivm, bld_base->base.int_vec_type, "addr");
1922 case TGSI_FILE_PREDICATE:
1923 assert(idx < LP_MAX_TGSI_PREDS);
1924 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1925 bld->preds[idx][i] = lp_build_alloca(gallivm, vec_type,
1929 case TGSI_FILE_SAMPLER_VIEW:
1931 * The target stored here MUST match whatever there actually
1932 * is in the set sampler views (what about return type?).
1934 assert(idx < PIPE_MAX_SHADER_SAMPLER_VIEWS);
1935 bld->sv[idx] = decl->SamplerView;
1939 /* don't need to declare other vars */
1946 void lp_emit_immediate_soa(
1947 struct lp_build_tgsi_context *bld_base,
1948 const struct tgsi_full_immediate *imm)
1950 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1951 struct gallivm_state * gallivm = bld_base->base.gallivm;
1953 /* simply copy the immediate values into the next immediates[] slot */
1955 const uint size = imm->Immediate.NrTokens - 1;
1957 assert(bld->num_immediates < LP_MAX_TGSI_IMMEDIATES);
1958 switch (imm->Immediate.DataType) {
1959 case TGSI_IMM_FLOAT32:
1960 for( i = 0; i < size; ++i )
1961 bld->immediates[bld->num_immediates][i] =
1962 lp_build_const_vec(gallivm, bld_base->base.type, imm->u[i].Float);
1965 case TGSI_IMM_UINT32:
1966 for( i = 0; i < size; ++i ) {
1967 LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->uint_bld.type, imm->u[i].Uint);
1968 bld->immediates[bld->num_immediates][i] =
1969 LLVMConstBitCast(tmp, bld_base->base.vec_type);
1973 case TGSI_IMM_INT32:
1974 for( i = 0; i < size; ++i ) {
1975 LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->int_bld.type, imm->u[i].Int);
1976 bld->immediates[bld->num_immediates][i] =
1977 LLVMConstBitCast(tmp, bld_base->base.vec_type);
1982 for( i = size; i < 4; ++i )
1983 bld->immediates[bld->num_immediates][i] = bld_base->base.undef;
1985 bld->num_immediates++;
1990 const struct lp_build_tgsi_action * action,
1991 struct lp_build_tgsi_context * bld_base,
1992 struct lp_build_emit_data * emit_data)
1994 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1996 emit_fetch_deriv(bld, emit_data->args[0], NULL,
1997 &emit_data->output[emit_data->chan], NULL);
2002 const struct lp_build_tgsi_action * action,
2003 struct lp_build_tgsi_context * bld_base,
2004 struct lp_build_emit_data * emit_data)
2006 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2008 emit_fetch_deriv(bld, emit_data->args[0], NULL, NULL,
2009 &emit_data->output[emit_data->chan]);
2014 const struct lp_build_tgsi_action * action,
2015 struct lp_build_tgsi_context * bld_base,
2016 struct lp_build_emit_data * emit_data)
2018 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2020 emit_kilp(bld, bld_base->pc - 1);
2025 const struct lp_build_tgsi_action * action,
2026 struct lp_build_tgsi_context * bld_base,
2027 struct lp_build_emit_data * emit_data)
2029 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2031 emit_kil(bld, emit_data->inst, bld_base->pc - 1);
2036 const struct lp_build_tgsi_action * action,
2037 struct lp_build_tgsi_context * bld_base,
2038 struct lp_build_emit_data * emit_data)
2040 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2042 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE, emit_data->output);
2047 const struct lp_build_tgsi_action * action,
2048 struct lp_build_tgsi_context * bld_base,
2049 struct lp_build_emit_data * emit_data)
2051 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2053 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_BIAS,
2059 const struct lp_build_tgsi_action * action,
2060 struct lp_build_tgsi_context * bld_base,
2061 struct lp_build_emit_data * emit_data)
2063 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2065 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
2071 const struct lp_build_tgsi_action * action,
2072 struct lp_build_tgsi_context * bld_base,
2073 struct lp_build_emit_data * emit_data)
2075 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2077 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
2083 const struct lp_build_tgsi_action * action,
2084 struct lp_build_tgsi_context * bld_base,
2085 struct lp_build_emit_data * emit_data)
2087 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2089 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_PROJECTED,
2095 const struct lp_build_tgsi_action * action,
2096 struct lp_build_tgsi_context * bld_base,
2097 struct lp_build_emit_data * emit_data)
2099 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2101 emit_size_query(bld, emit_data->inst, emit_data->output, FALSE);
2106 const struct lp_build_tgsi_action * action,
2107 struct lp_build_tgsi_context * bld_base,
2108 struct lp_build_emit_data * emit_data)
2110 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2112 emit_fetch_texels(bld, emit_data->inst, emit_data->output, FALSE);
2117 const struct lp_build_tgsi_action * action,
2118 struct lp_build_tgsi_context * bld_base,
2119 struct lp_build_emit_data * emit_data)
2121 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2123 emit_fetch_texels(bld, emit_data->inst, emit_data->output, TRUE);
2128 const struct lp_build_tgsi_action * action,
2129 struct lp_build_tgsi_context * bld_base,
2130 struct lp_build_emit_data * emit_data)
2132 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2134 emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
2135 FALSE, emit_data->output);
2140 const struct lp_build_tgsi_action * action,
2141 struct lp_build_tgsi_context * bld_base,
2142 struct lp_build_emit_data * emit_data)
2144 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2146 emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_BIAS,
2147 FALSE, emit_data->output);
2152 const struct lp_build_tgsi_action * action,
2153 struct lp_build_tgsi_context * bld_base,
2154 struct lp_build_emit_data * emit_data)
2156 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2158 emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
2159 TRUE, emit_data->output);
2164 const struct lp_build_tgsi_action * action,
2165 struct lp_build_tgsi_context * bld_base,
2166 struct lp_build_emit_data * emit_data)
2168 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2170 emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_ZERO,
2171 TRUE, emit_data->output);
2176 const struct lp_build_tgsi_action * action,
2177 struct lp_build_tgsi_context * bld_base,
2178 struct lp_build_emit_data * emit_data)
2180 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2182 emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
2183 FALSE, emit_data->output);
2188 const struct lp_build_tgsi_action * action,
2189 struct lp_build_tgsi_context * bld_base,
2190 struct lp_build_emit_data * emit_data)
2192 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2194 emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
2195 FALSE, emit_data->output);
2200 const struct lp_build_tgsi_action * action,
2201 struct lp_build_tgsi_context * bld_base,
2202 struct lp_build_emit_data * emit_data)
2204 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2206 emit_size_query(bld, emit_data->inst, emit_data->output, TRUE);
2210 mask_to_one_vec(struct lp_build_tgsi_context *bld_base)
2212 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2213 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
2214 LLVMValueRef one_vec = bld_base->int_bld.one;
2215 struct lp_exec_mask *exec_mask = &bld->exec_mask;
2217 if (exec_mask->has_mask) {
2218 one_vec = LLVMBuildAnd(builder, one_vec, exec_mask->exec_mask, "");
2220 one_vec = LLVMBuildAnd(builder, one_vec,
2221 lp_build_mask_value(bld->mask), "");
2226 increment_vec_ptr_by_mask(struct lp_build_tgsi_context * bld_base,
2230 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2232 LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
2234 current_vec = LLVMBuildAdd(builder, current_vec, mask, "");
2236 LLVMBuildStore(builder, current_vec, ptr);
2240 clear_uint_vec_ptr_from_mask(struct lp_build_tgsi_context * bld_base,
2244 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2246 LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
2247 LLVMValueRef full_mask = lp_build_cmp(&bld_base->uint_bld,
2250 bld_base->uint_bld.zero);
2252 current_vec = lp_build_select(&bld_base->uint_bld,
2254 bld_base->uint_bld.zero,
2257 LLVMBuildStore(builder, current_vec, ptr);
2262 const struct lp_build_tgsi_action * action,
2263 struct lp_build_tgsi_context * bld_base,
2264 struct lp_build_emit_data * emit_data)
2266 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2267 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
2269 if (bld->gs_iface->emit_vertex) {
2270 LLVMValueRef masked_ones = mask_to_one_vec(bld_base);
2271 LLVMValueRef total_emitted_vertices_vec =
2272 LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr, "");
2273 gather_outputs(bld);
2274 bld->gs_iface->emit_vertex(bld->gs_iface, &bld->bld_base,
2276 total_emitted_vertices_vec);
2277 increment_vec_ptr_by_mask(bld_base, bld->emitted_vertices_vec_ptr,
2279 increment_vec_ptr_by_mask(bld_base, bld->total_emitted_vertices_vec_ptr,
2282 lp_build_print_value(bld->bld_base.base.gallivm,
2283 " +++ emit vertex masked ones = ",
2285 lp_build_print_value(bld->bld_base.base.gallivm,
2286 " +++ emit vertex emitted = ",
2287 total_emitted_vertices_vec);
2294 end_primitive_masked(struct lp_build_tgsi_context * bld_base,
2295 LLVMValueRef masked_ones)
2297 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2298 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
2300 if (bld->gs_iface->end_primitive) {
2301 LLVMValueRef emitted_vertices_vec =
2302 LLVMBuildLoad(builder, bld->emitted_vertices_vec_ptr, "");
2303 LLVMValueRef emitted_prims_vec =
2304 LLVMBuildLoad(builder, bld->emitted_prims_vec_ptr, "");
2306 bld->gs_iface->end_primitive(bld->gs_iface, &bld->bld_base,
2307 emitted_vertices_vec,
2311 lp_build_print_value(bld->bld_base.base.gallivm,
2312 " +++ end prim masked ones = ",
2314 lp_build_print_value(bld->bld_base.base.gallivm,
2315 " +++ end prim emitted verts1 = ",
2316 emitted_vertices_vec);
2317 lp_build_print_value(bld->bld_base.base.gallivm,
2318 " +++ end prim emitted prims1 = ",
2319 LLVMBuildLoad(builder,
2320 bld->emitted_prims_vec_ptr, ""));
2322 increment_vec_ptr_by_mask(bld_base, bld->emitted_prims_vec_ptr,
2324 clear_uint_vec_ptr_from_mask(bld_base, bld->emitted_vertices_vec_ptr,
2327 lp_build_print_value(bld->bld_base.base.gallivm,
2328 " +++ end prim emitted verts2 = ",
2329 LLVMBuildLoad(builder,
2330 bld->emitted_vertices_vec_ptr, ""));
2338 const struct lp_build_tgsi_action * action,
2339 struct lp_build_tgsi_context * bld_base,
2340 struct lp_build_emit_data * emit_data)
2342 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2344 if (bld->gs_iface->end_primitive) {
2345 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2346 LLVMValueRef masked_ones = mask_to_one_vec(bld_base);
2347 struct lp_build_context *uint_bld = &bld_base->uint_bld;
2348 LLVMValueRef emitted_verts = LLVMBuildLoad(
2349 builder, bld->emitted_vertices_vec_ptr, "");
2350 LLVMValueRef emitted_mask = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL,
2353 /* We need to combine the current execution mask with the mask
2354 telling us which, if any, execution slots actually have
2355 unemitted primitives, this way we make sure that end_primitives
2356 executes only on the paths that have unflushed vertices */
2357 masked_ones = LLVMBuildAnd(builder, masked_ones, emitted_mask, "");
2359 end_primitive_masked(bld_base, masked_ones);
2365 const struct lp_build_tgsi_action * action,
2366 struct lp_build_tgsi_context * bld_base,
2367 struct lp_build_emit_data * emit_data)
2369 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2371 lp_exec_mask_call(&bld->exec_mask, emit_data->inst->Label.Label,
2377 const struct lp_build_tgsi_action * action,
2378 struct lp_build_tgsi_context * bld_base,
2379 struct lp_build_emit_data * emit_data)
2381 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2383 lp_exec_mask_ret(&bld->exec_mask, &bld_base->pc);
2388 const struct lp_build_tgsi_action * action,
2389 struct lp_build_tgsi_context * bld_base,
2390 struct lp_build_emit_data * emit_data)
2392 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2394 lp_exec_break(&bld->exec_mask);
2399 const struct lp_build_tgsi_action * action,
2400 struct lp_build_tgsi_context * bld_base,
2401 struct lp_build_emit_data * emit_data)
2403 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2404 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2405 struct lp_build_context *uint_bld = &bld_base->uint_bld;
2406 LLVMValueRef unsigned_cond =
2407 LLVMBuildBitCast(builder, emit_data->args[0], uint_bld->vec_type, "");
2408 LLVMValueRef cond = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL,
2412 lp_exec_break_condition(&bld->exec_mask, cond);
2417 const struct lp_build_tgsi_action * action,
2418 struct lp_build_tgsi_context * bld_base,
2419 struct lp_build_emit_data * emit_data)
2422 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2424 tmp = lp_build_cmp(&bld_base->base, PIPE_FUNC_NOTEQUAL,
2425 emit_data->args[0], bld->bld_base.base.zero);
2426 lp_exec_mask_cond_push(&bld->exec_mask, tmp);
2431 const struct lp_build_tgsi_action * action,
2432 struct lp_build_tgsi_context * bld_base,
2433 struct lp_build_emit_data * emit_data)
2436 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2437 struct lp_build_context *uint_bld = &bld_base->uint_bld;
2439 tmp = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL,
2440 emit_data->args[0], uint_bld->zero);
2441 lp_exec_mask_cond_push(&bld->exec_mask, tmp);
2446 const struct lp_build_tgsi_action * action,
2447 struct lp_build_tgsi_context * bld_base,
2448 struct lp_build_emit_data * emit_data)
2450 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2452 lp_exec_bgnloop(&bld->exec_mask);
2457 const struct lp_build_tgsi_action * action,
2458 struct lp_build_tgsi_context * bld_base,
2459 struct lp_build_emit_data * emit_data)
2461 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2463 lp_exec_mask_bgnsub(&bld->exec_mask);
2468 const struct lp_build_tgsi_action * action,
2469 struct lp_build_tgsi_context * bld_base,
2470 struct lp_build_emit_data * emit_data)
2472 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2474 lp_exec_mask_cond_invert(&bld->exec_mask);
2479 const struct lp_build_tgsi_action * action,
2480 struct lp_build_tgsi_context * bld_base,
2481 struct lp_build_emit_data * emit_data)
2483 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2485 lp_exec_mask_cond_pop(&bld->exec_mask);
2490 const struct lp_build_tgsi_action * action,
2491 struct lp_build_tgsi_context * bld_base,
2492 struct lp_build_emit_data * emit_data)
2494 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2496 lp_exec_endloop(bld_base->base.gallivm, &bld->exec_mask);
2501 const struct lp_build_tgsi_action * action,
2502 struct lp_build_tgsi_context * bld_base,
2503 struct lp_build_emit_data * emit_data)
2505 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2507 lp_exec_mask_endsub(&bld->exec_mask, &bld_base->pc);
2512 const struct lp_build_tgsi_action * action,
2513 struct lp_build_tgsi_context * bld_base,
2514 struct lp_build_emit_data * emit_data)
2516 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2518 lp_exec_continue(&bld->exec_mask);
2521 /* XXX: Refactor and move it to lp_bld_tgsi_action.c
2523 * XXX: What do the comments about xmm registers mean? Maybe they are left over
2524 * from old code, but there is no garauntee that LLVM will use those registers
2527 * XXX: There should be no calls to lp_build_emit_fetch in this function. This
2528 * should be handled by the emit_data->fetch_args function. */
2531 const struct lp_build_tgsi_action * action,
2532 struct lp_build_tgsi_context * bld_base,
2533 struct lp_build_emit_data * emit_data)
2535 LLVMValueRef tmp0, tmp1;
2536 LLVMValueRef tmp4 = NULL;
2537 LLVMValueRef tmp5 = NULL;
2538 LLVMValueRef tmp6 = NULL;
2539 LLVMValueRef tmp7 = NULL;
2540 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2542 uint dims = (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_NRM) ? 3 : 4;
2544 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X) ||
2545 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y) ||
2546 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z) ||
2547 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W) && dims == 4)) {
2549 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
2552 /* xmm0 = src.x * src.x */
2553 tmp0 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_X);
2554 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X)) {
2557 tmp0 = lp_build_mul( &bld->bld_base.base, tmp0, tmp0);
2560 /* xmm0 = xmm0 + src.y * src.y */
2561 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_Y);
2562 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y)) {
2565 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1);
2566 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1);
2569 /* xmm0 = xmm0 + src.z * src.z */
2570 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_Z);
2571 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z)) {
2574 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1);
2575 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1);
2579 /* xmm0 = xmm0 + src.w * src.w */
2580 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_W);
2581 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W)) {
2584 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1);
2585 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1);
2587 /* xmm1 = 1 / sqrt(xmm0) */
2588 tmp1 = lp_build_rsqrt( &bld->bld_base.base, tmp0);
2589 /* dst.x = xmm1 * src.x */
2590 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X)) {
2591 emit_data->output[TGSI_CHAN_X] = lp_build_mul( &bld->bld_base.base, tmp4, tmp1);
2593 /* dst.y = xmm1 * src.y */
2594 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y)) {
2595 emit_data->output[TGSI_CHAN_Y] = lp_build_mul( &bld->bld_base.base, tmp5, tmp1);
2598 /* dst.z = xmm1 * src.z */
2599 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z)) {
2600 emit_data->output[TGSI_CHAN_Z] = lp_build_mul( &bld->bld_base.base, tmp6, tmp1);
2602 /* dst.w = xmm1 * src.w */
2603 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X) && dims == 4) {
2604 emit_data->output[TGSI_CHAN_W] = lp_build_mul( &bld->bld_base.base, tmp7, tmp1);
2609 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W) && dims == 3) {
2610 emit_data->output[TGSI_CHAN_W] = bld->bld_base.base.one;
2614 static void emit_prologue(struct lp_build_tgsi_context * bld_base)
2616 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2617 struct gallivm_state * gallivm = bld_base->base.gallivm;
2619 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
2620 LLVMValueRef array_size =
2621 lp_build_const_int32(gallivm,
2622 bld_base->info->file_max[TGSI_FILE_TEMPORARY] * 4 + 4);
2623 bld->temps_array = lp_build_array_alloca(gallivm,
2624 bld_base->base.vec_type, array_size,
2628 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) {
2629 LLVMValueRef array_size =
2630 lp_build_const_int32(gallivm,
2631 bld_base->info->file_max[TGSI_FILE_OUTPUT] * 4 + 4);
2632 bld->outputs_array = lp_build_array_alloca(gallivm,
2633 bld_base->base.vec_type, array_size,
2637 /* If we have indirect addressing in inputs we need to copy them into
2638 * our alloca array to be able to iterate over them */
2639 if (bld->indirect_files & (1 << TGSI_FILE_INPUT) && !bld->gs_iface) {
2640 unsigned index, chan;
2641 LLVMTypeRef vec_type = bld_base->base.vec_type;
2642 LLVMValueRef array_size = lp_build_const_int32(gallivm,
2643 bld_base->info->file_max[TGSI_FILE_INPUT]*4 + 4);
2644 bld->inputs_array = lp_build_array_alloca(gallivm,
2645 vec_type, array_size,
2648 assert(bld_base->info->num_inputs
2649 <= bld_base->info->file_max[TGSI_FILE_INPUT] + 1);
2651 for (index = 0; index < bld_base->info->num_inputs; ++index) {
2652 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
2653 LLVMValueRef lindex =
2654 lp_build_const_int32(gallivm, index * 4 + chan);
2655 LLVMValueRef input_ptr =
2656 LLVMBuildGEP(gallivm->builder, bld->inputs_array,
2658 LLVMValueRef value = bld->inputs[index][chan];
2660 LLVMBuildStore(gallivm->builder, value, input_ptr);
2665 if (bld->gs_iface) {
2666 struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;
2667 bld->emitted_prims_vec_ptr =
2668 lp_build_alloca(gallivm,
2670 "emitted_prims_ptr");
2671 bld->emitted_vertices_vec_ptr =
2672 lp_build_alloca(gallivm,
2674 "emitted_vertices_ptr");
2675 bld->total_emitted_vertices_vec_ptr =
2676 lp_build_alloca(gallivm,
2678 "total_emitted_vertices_ptr");
2680 LLVMBuildStore(gallivm->builder, uint_bld->zero,
2681 bld->emitted_prims_vec_ptr);
2682 LLVMBuildStore(gallivm->builder, uint_bld->zero,
2683 bld->emitted_vertices_vec_ptr);
2684 LLVMBuildStore(gallivm->builder, uint_bld->zero,
2685 bld->total_emitted_vertices_vec_ptr);
2689 static void emit_epilogue(struct lp_build_tgsi_context * bld_base)
2691 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2692 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2696 emit_dump_temps(bld);
2699 /* If we have indirect addressing in outputs we need to copy our alloca array
2700 * to the outputs slots specified by the caller */
2701 if (bld->gs_iface) {
2702 LLVMValueRef total_emitted_vertices_vec;
2703 LLVMValueRef emitted_prims_vec;
2704 /* implicit end_primitives, needed in case there are any unflushed
2705 vertices in the cache */
2706 end_primitive(NULL, bld_base, NULL);
2708 total_emitted_vertices_vec =
2709 LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr, "");
2711 LLVMBuildLoad(builder, bld->emitted_prims_vec_ptr, "");
2713 bld->gs_iface->gs_epilogue(bld->gs_iface,
2715 total_emitted_vertices_vec,
2718 gather_outputs(bld);
2723 lp_build_tgsi_soa(struct gallivm_state *gallivm,
2724 const struct tgsi_token *tokens,
2725 struct lp_type type,
2726 struct lp_build_mask_context *mask,
2727 LLVMValueRef consts_ptr,
2728 const struct lp_bld_tgsi_system_values *system_values,
2729 const LLVMValueRef *pos,
2730 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS],
2731 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
2732 struct lp_build_sampler_soa *sampler,
2733 const struct tgsi_shader_info *info,
2734 const struct lp_build_tgsi_gs_iface *gs_iface)
2736 struct lp_build_tgsi_soa_context bld;
2738 struct lp_type res_type;
2740 assert(type.length <= LP_MAX_VECTOR_LENGTH);
2741 memset(&res_type, 0, sizeof res_type);
2742 res_type.width = type.width;
2743 res_type.length = type.length;
2746 /* Setup build context */
2747 memset(&bld, 0, sizeof bld);
2748 lp_build_context_init(&bld.bld_base.base, gallivm, type);
2749 lp_build_context_init(&bld.bld_base.uint_bld, gallivm, lp_uint_type(type));
2750 lp_build_context_init(&bld.bld_base.int_bld, gallivm, lp_int_type(type));
2751 lp_build_context_init(&bld.elem_bld, gallivm, lp_elem_type(type));
2754 bld.inputs = inputs;
2755 bld.outputs = outputs;
2756 bld.consts_ptr = consts_ptr;
2757 bld.sampler = sampler;
2758 bld.bld_base.info = info;
2759 bld.indirect_files = info->indirect_files;
2761 bld.bld_base.soa = TRUE;
2762 bld.bld_base.emit_fetch_funcs[TGSI_FILE_CONSTANT] = emit_fetch_constant;
2763 bld.bld_base.emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = emit_fetch_immediate;
2764 bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_input;
2765 bld.bld_base.emit_fetch_funcs[TGSI_FILE_TEMPORARY] = emit_fetch_temporary;
2766 bld.bld_base.emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = emit_fetch_system_value;
2767 bld.bld_base.emit_store = emit_store;
2769 bld.bld_base.emit_declaration = lp_emit_declaration_soa;
2770 bld.bld_base.emit_immediate = lp_emit_immediate_soa;
2772 bld.bld_base.emit_prologue = emit_prologue;
2773 bld.bld_base.emit_epilogue = emit_epilogue;
2775 /* Set opcode actions */
2776 lp_set_default_actions_cpu(&bld.bld_base);
2778 bld.bld_base.op_actions[TGSI_OPCODE_BGNLOOP].emit = bgnloop_emit;
2779 bld.bld_base.op_actions[TGSI_OPCODE_BGNSUB].emit = bgnsub_emit;
2780 bld.bld_base.op_actions[TGSI_OPCODE_BRK].emit = brk_emit;
2781 bld.bld_base.op_actions[TGSI_OPCODE_BREAKC].emit = breakc_emit;
2782 bld.bld_base.op_actions[TGSI_OPCODE_CAL].emit = cal_emit;
2783 bld.bld_base.op_actions[TGSI_OPCODE_CONT].emit = cont_emit;
2784 bld.bld_base.op_actions[TGSI_OPCODE_DDX].emit = ddx_emit;
2785 bld.bld_base.op_actions[TGSI_OPCODE_DDY].emit = ddy_emit;
2786 bld.bld_base.op_actions[TGSI_OPCODE_ELSE].emit = else_emit;
2787 bld.bld_base.op_actions[TGSI_OPCODE_ENDIF].emit = endif_emit;
2788 bld.bld_base.op_actions[TGSI_OPCODE_ENDLOOP].emit = endloop_emit;
2789 bld.bld_base.op_actions[TGSI_OPCODE_ENDSUB].emit = endsub_emit;
2790 bld.bld_base.op_actions[TGSI_OPCODE_IF].emit = if_emit;
2791 bld.bld_base.op_actions[TGSI_OPCODE_UIF].emit = uif_emit;
2792 bld.bld_base.op_actions[TGSI_OPCODE_KIL].emit = kil_emit;
2793 bld.bld_base.op_actions[TGSI_OPCODE_KILP].emit = kilp_emit;
2794 bld.bld_base.op_actions[TGSI_OPCODE_NRM].emit = nrm_emit;
2795 bld.bld_base.op_actions[TGSI_OPCODE_NRM4].emit = nrm_emit;
2796 bld.bld_base.op_actions[TGSI_OPCODE_RET].emit = ret_emit;
2797 bld.bld_base.op_actions[TGSI_OPCODE_TEX].emit = tex_emit;
2798 bld.bld_base.op_actions[TGSI_OPCODE_TXB].emit = txb_emit;
2799 bld.bld_base.op_actions[TGSI_OPCODE_TXD].emit = txd_emit;
2800 bld.bld_base.op_actions[TGSI_OPCODE_TXL].emit = txl_emit;
2801 bld.bld_base.op_actions[TGSI_OPCODE_TXP].emit = txp_emit;
2802 bld.bld_base.op_actions[TGSI_OPCODE_TXQ].emit = txq_emit;
2803 bld.bld_base.op_actions[TGSI_OPCODE_TXF].emit = txf_emit;
2804 /* DX10 sampling ops */
2805 bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE].emit = sample_emit;
2806 bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_B].emit = sample_b_emit;
2807 bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_C].emit = sample_c_emit;
2808 bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_C_LZ].emit = sample_c_lz_emit;
2809 bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_D].emit = sample_d_emit;
2810 bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_I].emit = sample_i_emit;
2811 bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_L].emit = sample_l_emit;
2812 bld.bld_base.op_actions[TGSI_OPCODE_SVIEWINFO].emit = sviewinfo_emit;
2815 /* inputs are always indirect with gs */
2816 bld.indirect_files |= (1 << TGSI_FILE_INPUT);
2817 bld.gs_iface = gs_iface;
2818 bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_gs_input;
2819 bld.bld_base.op_actions[TGSI_OPCODE_EMIT].emit = emit_vertex;
2820 bld.bld_base.op_actions[TGSI_OPCODE_ENDPRIM].emit = end_primitive;
2823 lp_exec_mask_init(&bld.exec_mask, &bld.bld_base.base);
2825 bld.system_values = *system_values;
2827 lp_build_tgsi_llvm(&bld.bld_base, tokens);
2830 LLVMBasicBlockRef block = LLVMGetInsertBlock(gallivm->builder);
2831 LLVMValueRef function = LLVMGetBasicBlockParent(block);
2832 debug_printf("11111111111111111111111111111 \n");
2833 tgsi_dump(tokens, 0);
2834 lp_debug_dump_value(function);
2835 debug_printf("2222222222222222222222222222 \n");
2839 LLVMModuleRef module = LLVMGetGlobalParent(
2840 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm->builder)));
2841 LLVMDumpModule(module);