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"
69 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
71 LLVMTypeRef int_type
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
72 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
75 mask
->has_mask
= FALSE
;
76 mask
->ret_in_main
= FALSE
;
77 mask
->cond_stack_size
= 0;
78 mask
->loop_stack_size
= 0;
79 mask
->call_stack_size
= 0;
81 mask
->int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, mask
->bld
->type
);
82 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
83 LLVMConstAllOnes(mask
->int_vec_type
);
85 mask
->loop_limiter
= lp_build_alloca(bld
->gallivm
, int_type
, "looplimiter");
89 LLVMConstInt(int_type
, LP_MAX_TGSI_LOOP_ITERATIONS
, false),
93 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
95 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
97 if (mask
->loop_stack_size
) {
98 /*for loops we need to update the entire mask at runtime */
100 assert(mask
->break_mask
);
101 tmp
= LLVMBuildAnd(builder
,
105 mask
->exec_mask
= LLVMBuildAnd(builder
,
110 mask
->exec_mask
= mask
->cond_mask
;
112 if (mask
->call_stack_size
|| mask
->ret_in_main
) {
113 mask
->exec_mask
= LLVMBuildAnd(builder
,
119 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
120 mask
->loop_stack_size
> 0 ||
121 mask
->call_stack_size
> 0 ||
125 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
128 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
130 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
131 if (mask
->cond_stack_size
== 0) {
132 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
134 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
135 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
136 mask
->cond_mask
= LLVMBuildAnd(builder
,
140 lp_exec_mask_update(mask
);
143 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
145 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
146 LLVMValueRef prev_mask
;
147 LLVMValueRef inv_mask
;
149 assert(mask
->cond_stack_size
);
150 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
151 if (mask
->cond_stack_size
== 1) {
152 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
155 inv_mask
= LLVMBuildNot(builder
, mask
->cond_mask
, "");
157 mask
->cond_mask
= LLVMBuildAnd(builder
,
160 lp_exec_mask_update(mask
);
163 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
165 assert(mask
->cond_stack_size
);
166 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
167 lp_exec_mask_update(mask
);
170 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
172 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
174 if (mask
->loop_stack_size
== 0) {
175 assert(mask
->loop_block
== NULL
);
176 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
177 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
178 assert(mask
->break_var
== NULL
);
181 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
183 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
184 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
185 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
186 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
187 ++mask
->loop_stack_size
;
189 mask
->break_var
= lp_build_alloca(mask
->bld
->gallivm
, mask
->int_vec_type
, "");
190 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
192 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->gallivm
, "bgnloop");
194 LLVMBuildBr(builder
, mask
->loop_block
);
195 LLVMPositionBuilderAtEnd(builder
, mask
->loop_block
);
197 mask
->break_mask
= LLVMBuildLoad(builder
, mask
->break_var
, "");
199 lp_exec_mask_update(mask
);
202 static void lp_exec_break(struct lp_exec_mask
*mask
)
204 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
205 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
209 mask
->break_mask
= LLVMBuildAnd(builder
,
211 exec_mask
, "break_full");
213 lp_exec_mask_update(mask
);
216 static void lp_exec_break_condition(struct lp_exec_mask
*mask
,
219 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
220 LLVMValueRef cond_mask
= LLVMBuildAnd(builder
,
223 cond_mask
= LLVMBuildNot(builder
, cond
, "break_cond");
225 mask
->break_mask
= LLVMBuildAnd(builder
,
227 cond_mask
, "breakc_full");
229 lp_exec_mask_update(mask
);
232 static void lp_exec_continue(struct lp_exec_mask
*mask
)
234 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
235 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
239 mask
->cont_mask
= LLVMBuildAnd(builder
,
243 lp_exec_mask_update(mask
);
247 static void lp_exec_endloop(struct gallivm_state
*gallivm
,
248 struct lp_exec_mask
*mask
)
250 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
251 LLVMBasicBlockRef endloop
;
252 LLVMTypeRef int_type
= LLVMInt32TypeInContext(mask
->bld
->gallivm
->context
);
253 LLVMTypeRef reg_type
= LLVMIntTypeInContext(gallivm
->context
,
254 mask
->bld
->type
.width
*
255 mask
->bld
->type
.length
);
256 LLVMValueRef i1cond
, i2cond
, icond
, limiter
;
258 assert(mask
->break_mask
);
261 * Restore the cont_mask, but don't pop
263 assert(mask
->loop_stack_size
);
264 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
265 lp_exec_mask_update(mask
);
268 * Unlike the continue mask, the break_mask must be preserved across loop
271 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
273 /* Decrement the loop limiter */
274 limiter
= LLVMBuildLoad(builder
, mask
->loop_limiter
, "");
276 limiter
= LLVMBuildSub(
279 LLVMConstInt(int_type
, 1, false),
282 LLVMBuildStore(builder
, limiter
, mask
->loop_limiter
);
284 /* i1cond = (mask != 0) */
285 i1cond
= LLVMBuildICmp(
288 LLVMBuildBitCast(builder
, mask
->exec_mask
, reg_type
, ""),
289 LLVMConstNull(reg_type
), "i1cond");
291 /* i2cond = (looplimiter > 0) */
292 i2cond
= LLVMBuildICmp(
296 LLVMConstNull(int_type
), "i2cond");
298 /* if( i1cond && i2cond ) */
299 icond
= LLVMBuildAnd(builder
, i1cond
, i2cond
, "");
301 endloop
= lp_build_insert_new_block(mask
->bld
->gallivm
, "endloop");
303 LLVMBuildCondBr(builder
,
304 icond
, mask
->loop_block
, endloop
);
306 LLVMPositionBuilderAtEnd(builder
, endloop
);
308 assert(mask
->loop_stack_size
);
309 --mask
->loop_stack_size
;
310 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
311 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
312 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
313 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
315 lp_exec_mask_update(mask
);
318 /* stores val into an address pointed to by dst.
319 * mask->exec_mask is used to figure out which bits of val
320 * should be stored into the address
321 * (0 means don't store this bit, 1 means do store).
323 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
324 struct lp_build_context
*bld_store
,
329 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
331 /* Mix the predicate and execution mask */
332 if (mask
->has_mask
) {
334 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
336 pred
= mask
->exec_mask
;
341 LLVMValueRef real_val
, dst_val
;
343 dst_val
= LLVMBuildLoad(builder
, dst
, "");
344 real_val
= lp_build_select(bld_store
,
348 LLVMBuildStore(builder
, real_val
, dst
);
350 LLVMBuildStore(builder
, val
, dst
);
353 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
357 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
358 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
359 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
360 mask
->call_stack_size
++;
364 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
366 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
367 LLVMValueRef exec_mask
;
369 if (mask
->cond_stack_size
== 0 &&
370 mask
->loop_stack_size
== 0 &&
371 mask
->call_stack_size
== 0) {
372 /* returning from main() */
377 if (mask
->call_stack_size
== 0) {
379 * This requires special handling since we need to ensure
380 * we don't drop the mask even if we have no call stack
381 * (e.g. after a ret in a if clause after the endif)
383 mask
->ret_in_main
= TRUE
;
386 exec_mask
= LLVMBuildNot(builder
,
390 mask
->ret_mask
= LLVMBuildAnd(builder
,
392 exec_mask
, "ret_full");
394 lp_exec_mask_update(mask
);
397 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
401 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
403 assert(mask
->call_stack_size
);
404 mask
->call_stack_size
--;
405 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
406 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
407 lp_exec_mask_update(mask
);
412 * Return pointer to a temporary register channel (src or dest).
413 * Note that indirect addressing cannot be handled here.
414 * \param index which temporary register
415 * \param chan which channel of the temp register.
418 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context
*bld
,
422 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
424 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
425 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, index
* 4 + chan
);
426 return LLVMBuildGEP(builder
, bld
->temps_array
, &lindex
, 1, "");
429 return bld
->temps
[index
][chan
];
434 * Return pointer to a output register channel (src or dest).
435 * Note that indirect addressing cannot be handled here.
436 * \param index which output register
437 * \param chan which channel of the output register.
440 lp_get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
444 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
446 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
447 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
,
449 return LLVMBuildGEP(builder
, bld
->outputs_array
, &lindex
, 1, "");
452 return bld
->outputs
[index
][chan
];
457 * If we have indirect addressing in outputs copy our alloca array
458 * to the outputs slots specified by the caller to make sure
459 * our outputs are delivered consistently via the same interface.
462 gather_outputs(struct lp_build_tgsi_soa_context
* bld
)
464 if ((bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
465 unsigned index
, chan
;
466 assert(bld
->bld_base
.info
->num_outputs
<=
467 bld
->bld_base
.info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
468 for (index
= 0; index
< bld
->bld_base
.info
->num_outputs
; ++index
) {
469 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
470 bld
->outputs
[index
][chan
] = lp_get_output_ptr(bld
, index
, chan
);
478 * XXX the lp_build_gather() function should be capable of doing this
479 * with a little work.
482 build_gather(struct lp_build_context
*bld
,
483 LLVMValueRef base_ptr
,
484 LLVMValueRef indexes
)
486 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
487 LLVMValueRef res
= bld
->undef
;
491 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
493 for (i
= 0; i
< bld
->type
.length
; i
++) {
494 LLVMValueRef ii
= lp_build_const_int32(bld
->gallivm
, i
);
495 LLVMValueRef index
= LLVMBuildExtractElement(builder
,
497 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
498 &index
, 1, "gather_ptr");
499 LLVMValueRef scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
501 res
= LLVMBuildInsertElement(builder
, res
, scalar
, ii
, "");
509 * Scatter/store vector.
512 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
513 LLVMValueRef base_ptr
,
514 LLVMValueRef indexes
,
516 struct lp_exec_mask
*mask
,
519 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
520 LLVMBuilderRef builder
= gallivm
->builder
;
523 /* Mix the predicate and execution mask */
524 if (mask
->has_mask
) {
526 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
529 pred
= mask
->exec_mask
;
534 * Loop over elements of index_vec, store scalar value.
536 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
537 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
538 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
539 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
540 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
541 LLVMValueRef scalar_pred
= pred
?
542 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
545 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
546 ii
, val
, index
, scalar_ptr
);
549 LLVMValueRef real_val
, dst_val
;
550 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
551 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
552 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
555 LLVMBuildStore(builder
, val
, scalar_ptr
);
562 * Read the current value of the ADDR register, convert the floats to
563 * ints, add the base index and return the vector of offsets.
564 * The offsets will be used to index into the constant buffer or
565 * temporary register file.
568 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
569 unsigned reg_file
, unsigned reg_index
,
570 const struct tgsi_ind_register
*indirect_reg
)
572 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
573 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
574 /* always use X component of address register */
575 unsigned swizzle
= indirect_reg
->Swizzle
;
578 LLVMValueRef max_index
;
581 assert(bld
->indirect_files
& (1 << reg_file
));
583 base
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, reg_index
);
586 switch (indirect_reg
->File
) {
587 case TGSI_FILE_ADDRESS
:
588 rel
= LLVMBuildLoad(builder
,
589 bld
->addr
[indirect_reg
->Index
][swizzle
],
591 /* ADDR LLVM values already have LLVM integer type. */
593 case TGSI_FILE_TEMPORARY
:
594 rel
= lp_get_temp_ptr_soa(bld
, indirect_reg
->Index
, swizzle
);
595 rel
= LLVMBuildLoad(builder
, rel
, "load temp reg");
596 /* TEMP LLVM values always have LLVM float type, but for indirection, the
597 * value actually stored is expected to be an integer */
598 rel
= LLVMBuildBitCast(builder
, rel
, uint_bld
->vec_type
, "");
602 rel
= uint_bld
->zero
;
605 index
= lp_build_add(uint_bld
, base
, rel
);
607 max_index
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
,
609 bld
->bld_base
.info
->file_max
[reg_file
]);
611 assert(!uint_bld
->type
.sign
);
612 index
= lp_build_min(uint_bld
, index
, max_index
);
617 static struct lp_build_context
*
618 stype_to_fetch(struct lp_build_tgsi_context
* bld_base
,
619 enum tgsi_opcode_type stype
)
621 struct lp_build_context
*bld_fetch
;
624 case TGSI_TYPE_FLOAT
:
625 case TGSI_TYPE_UNTYPED
:
626 bld_fetch
= &bld_base
->base
;
628 case TGSI_TYPE_UNSIGNED
:
629 bld_fetch
= &bld_base
->uint_bld
;
631 case TGSI_TYPE_SIGNED
:
632 bld_fetch
= &bld_base
->int_bld
;
635 case TGSI_TYPE_DOUBLE
:
646 struct lp_build_tgsi_context
* bld_base
,
647 const struct tgsi_full_src_register
* reg
,
648 enum tgsi_opcode_type stype
,
651 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
652 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
653 LLVMBuilderRef builder
= gallivm
->builder
;
654 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
655 LLVMValueRef indirect_index
= NULL
;
656 unsigned dimension
= 0;
657 LLVMValueRef dimension_index
;
658 LLVMValueRef consts_ptr
;
661 /* XXX: Handle fetching xyzw components as a vector */
662 assert(swizzle
!= ~0);
664 if (reg
->Register
.Dimension
) {
665 assert(!reg
->Dimension
.Indirect
);
666 dimension
= reg
->Dimension
.Index
;
667 assert(dimension
< LP_MAX_TGSI_CONST_BUFFERS
);
670 dimension_index
= lp_build_const_int32(gallivm
, dimension
);
671 consts_ptr
= lp_build_array_get(gallivm
, bld
->consts_ptr
, dimension_index
);
673 if (reg
->Register
.Indirect
) {
674 indirect_index
= get_indirect_index(bld
,
680 if (reg
->Register
.Indirect
) {
681 LLVMValueRef swizzle_vec
=
682 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
683 LLVMValueRef index_vec
; /* index into the const buffer */
685 /* index_vec = indirect_index * 4 + swizzle */
686 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
687 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
689 /* Gather values from the constant buffer */
690 res
= build_gather(&bld_base
->base
, consts_ptr
, index_vec
);
693 LLVMValueRef index
; /* index into the const buffer */
694 LLVMValueRef scalar
, scalar_ptr
;
696 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
*4 + swizzle
);
698 scalar_ptr
= LLVMBuildGEP(builder
, consts_ptr
,
700 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
701 res
= lp_build_broadcast_scalar(&bld_base
->base
, scalar
);
704 if (stype
== TGSI_TYPE_SIGNED
|| stype
== TGSI_TYPE_UNSIGNED
) {
705 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
706 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
712 emit_fetch_immediate(
713 struct lp_build_tgsi_context
* bld_base
,
714 const struct tgsi_full_src_register
* reg
,
715 enum tgsi_opcode_type stype
,
718 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
719 LLVMValueRef res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
722 if (stype
== TGSI_TYPE_UNSIGNED
) {
723 res
= LLVMConstBitCast(res
, bld_base
->uint_bld
.vec_type
);
724 } else if (stype
== TGSI_TYPE_SIGNED
) {
725 res
= LLVMConstBitCast(res
, bld_base
->int_bld
.vec_type
);
732 struct lp_build_tgsi_context
* bld_base
,
733 const struct tgsi_full_src_register
* reg
,
734 enum tgsi_opcode_type stype
,
737 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
738 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
739 LLVMBuilderRef builder
= gallivm
->builder
;
740 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
741 LLVMValueRef indirect_index
= NULL
;
744 if (reg
->Register
.Indirect
) {
745 indirect_index
= get_indirect_index(bld
,
751 if (reg
->Register
.Indirect
) {
752 LLVMValueRef swizzle_vec
=
753 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
754 LLVMValueRef length_vec
=
755 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
756 LLVMValueRef index_vec
; /* index into the const buffer */
757 LLVMValueRef inputs_array
;
758 LLVMTypeRef float4_ptr_type
;
760 /* index_vec = (indirect_index * 4 + swizzle) * length */
761 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
762 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
763 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
765 /* cast inputs_array pointer to float* */
766 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
767 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
,
768 float4_ptr_type
, "");
770 /* Gather values from the temporary register array */
771 res
= build_gather(&bld_base
->base
, inputs_array
, index_vec
);
773 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
774 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
775 reg
->Register
.Index
* 4 + swizzle
);
776 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
777 bld
->inputs_array
, &lindex
, 1, "");
778 res
= LLVMBuildLoad(builder
, input_ptr
, "");
781 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
787 if (stype
== TGSI_TYPE_UNSIGNED
) {
788 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
789 } else if (stype
== TGSI_TYPE_SIGNED
) {
790 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
799 struct lp_build_tgsi_context
* bld_base
,
800 const struct tgsi_full_src_register
* reg
,
801 enum tgsi_opcode_type stype
,
804 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
805 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
806 LLVMBuilderRef builder
= gallivm
->builder
;
807 LLVMValueRef attrib_index
= NULL
;
808 LLVMValueRef vertex_index
= NULL
;
809 LLVMValueRef swizzle_index
= lp_build_const_int32(gallivm
, swizzle
);
812 if (reg
->Register
.Indirect
) {
813 attrib_index
= get_indirect_index(bld
,
818 attrib_index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
);
821 if (reg
->Dimension
.Indirect
) {
822 vertex_index
= get_indirect_index(bld
,
824 reg
->Dimension
.Index
,
827 vertex_index
= lp_build_const_int32(gallivm
, reg
->Dimension
.Index
);
831 res
= bld
->gs_iface
->fetch_input(bld
->gs_iface
, bld_base
,
832 vertex_index
, attrib_index
,
837 if (stype
== TGSI_TYPE_UNSIGNED
) {
838 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
839 } else if (stype
== TGSI_TYPE_SIGNED
) {
840 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
847 emit_fetch_temporary(
848 struct lp_build_tgsi_context
* bld_base
,
849 const struct tgsi_full_src_register
* reg
,
850 enum tgsi_opcode_type stype
,
853 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
854 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
855 LLVMBuilderRef builder
= gallivm
->builder
;
856 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
857 LLVMValueRef indirect_index
= NULL
;
860 if (reg
->Register
.Indirect
) {
861 indirect_index
= get_indirect_index(bld
,
867 if (reg
->Register
.Indirect
) {
868 LLVMValueRef swizzle_vec
=
869 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
870 LLVMValueRef length_vec
=
871 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
,
872 bld
->bld_base
.base
.type
.length
);
873 LLVMValueRef index_vec
; /* index into the const buffer */
874 LLVMValueRef temps_array
;
875 LLVMTypeRef float4_ptr_type
;
877 /* index_vec = (indirect_index * 4 + swizzle) * length */
878 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
879 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
880 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
882 /* cast temps_array pointer to float* */
883 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(bld
->bld_base
.base
.gallivm
->context
), 0);
884 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
885 float4_ptr_type
, "");
887 /* Gather values from the temporary register array */
888 res
= build_gather(&bld_base
->base
, temps_array
, index_vec
);
891 LLVMValueRef temp_ptr
;
892 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
893 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
896 if (stype
== TGSI_TYPE_SIGNED
|| stype
== TGSI_TYPE_UNSIGNED
) {
897 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
898 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
905 emit_fetch_system_value(
906 struct lp_build_tgsi_context
* bld_base
,
907 const struct tgsi_full_src_register
* reg
,
908 enum tgsi_opcode_type stype
,
911 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
912 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
913 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
914 LLVMBuilderRef builder
= gallivm
->builder
;
916 enum tgsi_opcode_type atype
; // Actual type of the value
918 assert(!reg
->Register
.Indirect
);
920 switch (info
->system_value_semantic_name
[reg
->Register
.Index
]) {
921 case TGSI_SEMANTIC_INSTANCEID
:
922 res
= lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.instance_id
);
923 atype
= TGSI_TYPE_UNSIGNED
;
926 case TGSI_SEMANTIC_VERTEXID
:
927 res
= bld
->system_values
.vertex_id
;
928 atype
= TGSI_TYPE_UNSIGNED
;
931 case TGSI_SEMANTIC_PRIMID
:
932 res
= bld
->system_values
.prim_id
;
933 atype
= TGSI_TYPE_UNSIGNED
;
937 assert(!"unexpected semantic in emit_fetch_system_value");
938 res
= bld_base
->base
.zero
;
939 atype
= TGSI_TYPE_FLOAT
;
943 if (atype
!= stype
) {
944 if (stype
== TGSI_TYPE_FLOAT
) {
945 res
= LLVMBuildBitCast(builder
, res
, bld_base
->base
.vec_type
, "");
946 } else if (stype
== TGSI_TYPE_UNSIGNED
) {
947 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
948 } else if (stype
== TGSI_TYPE_SIGNED
) {
949 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
957 * Register fetch with derivatives.
961 struct lp_build_tgsi_soa_context
*bld
,
970 /* TODO: use interpolation coeffs for inputs */
973 *ddx
= lp_build_ddx(&bld
->bld_base
.base
, src
);
976 *ddy
= lp_build_ddy(&bld
->bld_base
.base
, src
);
984 emit_fetch_predicate(
985 struct lp_build_tgsi_soa_context
*bld
,
986 const struct tgsi_full_instruction
*inst
,
989 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
991 unsigned char swizzles
[4];
992 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
996 if (!inst
->Instruction
.Predicate
) {
997 TGSI_FOR_EACH_CHANNEL( chan
) {
1003 swizzles
[0] = inst
->Predicate
.SwizzleX
;
1004 swizzles
[1] = inst
->Predicate
.SwizzleY
;
1005 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
1006 swizzles
[3] = inst
->Predicate
.SwizzleW
;
1008 index
= inst
->Predicate
.Index
;
1009 assert(index
< LP_MAX_TGSI_PREDS
);
1011 TGSI_FOR_EACH_CHANNEL( chan
) {
1012 unsigned swizzle
= swizzles
[chan
];
1015 * Only fetch the predicate register channels that are actually listed
1018 if (!unswizzled
[swizzle
]) {
1019 value
= LLVMBuildLoad(builder
,
1020 bld
->preds
[index
][swizzle
], "");
1023 * Convert the value to an integer mask.
1025 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
1026 * is needlessly causing two comparisons due to storing the intermediate
1027 * result as float vector instead of an integer mask vector.
1029 value
= lp_build_compare(bld
->bld_base
.base
.gallivm
,
1030 bld
->bld_base
.base
.type
,
1033 bld
->bld_base
.base
.zero
);
1034 if (inst
->Predicate
.Negate
) {
1035 value
= LLVMBuildNot(builder
, value
, "");
1038 unswizzled
[swizzle
] = value
;
1040 value
= unswizzled
[swizzle
];
1052 struct lp_build_tgsi_context
*bld_base
,
1053 const struct tgsi_full_instruction
*inst
,
1055 unsigned chan_index
,
1059 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1060 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1061 LLVMBuilderRef builder
= gallivm
->builder
;
1062 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
1063 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1064 LLVMValueRef indirect_index
= NULL
;
1065 struct lp_build_context
*bld_store
;
1066 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
1070 case TGSI_TYPE_FLOAT
:
1071 case TGSI_TYPE_UNTYPED
:
1072 bld_store
= &bld_base
->base
;
1074 case TGSI_TYPE_UNSIGNED
:
1075 bld_store
= &bld_base
->uint_bld
;
1077 case TGSI_TYPE_SIGNED
:
1078 bld_store
= &bld_base
->int_bld
;
1080 case TGSI_TYPE_DOUBLE
:
1081 case TGSI_TYPE_VOID
:
1087 /* If the destination is untyped then the source can be anything,
1088 * but LLVM won't like if the types don't match so lets cast
1089 * to the correct destination type as expected by LLVM. */
1090 if (dtype
== TGSI_TYPE_UNTYPED
&&
1091 !lp_check_vec_type(bld_store
->type
, LLVMTypeOf(value
))) {
1092 value
= LLVMBuildBitCast(builder
, value
, bld_store
->vec_type
,
1096 switch( inst
->Instruction
.Saturate
) {
1100 case TGSI_SAT_ZERO_ONE
:
1101 value
= lp_build_max(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.zero
);
1102 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
1105 case TGSI_SAT_MINUS_PLUS_ONE
:
1106 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));
1107 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
1114 if (reg
->Register
.Indirect
) {
1115 indirect_index
= get_indirect_index(bld
,
1117 reg
->Register
.Index
,
1120 assert(reg
->Register
.Index
<=
1121 bld
->bld_base
.info
->file_max
[reg
->Register
.File
]);
1124 switch( reg
->Register
.File
) {
1125 case TGSI_FILE_OUTPUT
:
1126 if (reg
->Register
.Indirect
) {
1127 LLVMValueRef chan_vec
=
1128 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
1129 LLVMValueRef length_vec
=
1130 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
1131 LLVMValueRef index_vec
; /* indexes into the temp registers */
1132 LLVMValueRef outputs_array
;
1133 LLVMValueRef pixel_offsets
;
1134 LLVMTypeRef float_ptr_type
;
1137 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1138 pixel_offsets
= uint_bld
->undef
;
1139 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
1140 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1141 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
1145 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1146 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1147 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1148 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1149 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1152 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1153 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
,
1154 float_ptr_type
, "");
1156 /* Scatter store values into temp registers */
1157 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
1158 &bld
->exec_mask
, pred
);
1161 LLVMValueRef out_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
1163 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, out_ptr
);
1167 case TGSI_FILE_TEMPORARY
:
1168 if (reg
->Register
.Indirect
) {
1169 LLVMValueRef chan_vec
=
1170 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
1171 LLVMValueRef length_vec
=
1172 lp_build_const_int_vec(gallivm
, uint_bld
->type
,
1173 bld
->bld_base
.base
.type
.length
);
1174 LLVMValueRef index_vec
; /* indexes into the temp registers */
1175 LLVMValueRef temps_array
;
1176 LLVMValueRef pixel_offsets
;
1177 LLVMTypeRef float_ptr_type
;
1180 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1181 pixel_offsets
= uint_bld
->undef
;
1182 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
1183 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1184 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
1188 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1189 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1190 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1191 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1192 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1195 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1196 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
1197 float_ptr_type
, "");
1199 /* Scatter store values into temp registers */
1200 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
1201 &bld
->exec_mask
, pred
);
1204 LLVMValueRef temp_ptr
;
1207 case TGSI_TYPE_UNSIGNED
:
1208 case TGSI_TYPE_SIGNED
: {
1209 LLVMTypeRef itype
= bld_base
->int_bld
.vec_type
;
1210 LLVMTypeRef ivtype
= LLVMPointerType(itype
, 0);
1211 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1213 LLVMValueRef temp_value_ptr
;
1215 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, ivtype
, "");
1216 temp_value_ptr
= LLVMBuildBitCast(builder
, value
, itype
, "");
1217 value
= temp_value_ptr
;
1221 case TGSI_TYPE_FLOAT
:
1222 case TGSI_TYPE_UNTYPED
:
1223 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1228 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, temp_ptr
);
1232 case TGSI_FILE_ADDRESS
:
1233 assert(dtype
== TGSI_TYPE_SIGNED
);
1234 assert(LLVMTypeOf(value
) == bld_base
->base
.int_vec_type
);
1235 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1236 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1239 case TGSI_FILE_PREDICATE
:
1240 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1241 bld
->preds
[reg
->Register
.Index
][chan_index
]);
1251 struct lp_build_tgsi_context
* bld_base
,
1252 const struct tgsi_full_instruction
* inst
,
1253 const struct tgsi_opcode_info
* info
,
1254 LLVMValueRef dst
[4])
1257 unsigned chan_index
;
1258 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1261 LLVMValueRef pred
[TGSI_NUM_CHANNELS
];
1263 emit_fetch_predicate( bld
, inst
, pred
);
1265 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1266 emit_store_chan(bld_base
, inst
, 0, chan_index
, pred
[chan_index
], dst
[chan_index
]);
1272 * High-level instruction translators.
1276 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
1277 const struct tgsi_full_instruction
*inst
,
1278 enum lp_build_tex_modifier modifier
,
1279 LLVMValueRef
*texel
)
1282 LLVMValueRef lod_bias
, explicit_lod
;
1283 LLVMValueRef oow
= NULL
;
1284 LLVMValueRef coords
[4];
1285 LLVMValueRef offsets
[3] = { NULL
};
1286 struct lp_derivatives derivs
;
1287 struct lp_derivatives
*deriv_ptr
= NULL
;
1288 unsigned num_coords
, num_derivs
, num_offsets
;
1291 if (!bld
->sampler
) {
1292 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1293 for (i
= 0; i
< 4; i
++) {
1294 texel
[i
] = bld
->bld_base
.base
.undef
;
1299 switch (inst
->Texture
.Texture
) {
1300 case TGSI_TEXTURE_1D
:
1305 case TGSI_TEXTURE_1D_ARRAY
:
1310 case TGSI_TEXTURE_2D
:
1311 case TGSI_TEXTURE_RECT
:
1316 case TGSI_TEXTURE_SHADOW1D
:
1317 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1322 case TGSI_TEXTURE_SHADOW2D
:
1323 case TGSI_TEXTURE_SHADOWRECT
:
1324 case TGSI_TEXTURE_2D_ARRAY
:
1329 case TGSI_TEXTURE_CUBE
:
1334 case TGSI_TEXTURE_3D
:
1339 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1344 case TGSI_TEXTURE_SHADOWCUBE
:
1354 /* Note lod and especially projected are illegal in a LOT of cases */
1355 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
1356 assert(num_coords
< 4);
1357 lod_bias
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1358 explicit_lod
= NULL
;
1360 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
1361 assert(num_coords
< 4);
1363 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1367 explicit_lod
= NULL
;
1370 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
1371 assert(num_coords
< 4);
1372 oow
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1373 oow
= lp_build_rcp(&bld
->bld_base
.base
, oow
);
1376 for (i
= 0; i
< num_coords
; i
++) {
1377 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1378 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
1379 coords
[i
] = lp_build_mul(&bld
->bld_base
.base
, coords
[i
], oow
);
1381 for (i
= num_coords
; i
< 4; i
++) {
1382 coords
[i
] = bld
->bld_base
.base
.undef
;
1385 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
1387 for (dim
= 0; dim
< num_derivs
; ++dim
) {
1388 derivs
.ddx
[dim
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 1, dim
);
1389 derivs
.ddy
[dim
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 2, dim
);
1391 deriv_ptr
= &derivs
;
1392 unit
= inst
->Src
[3].Register
.Index
;
1394 unit
= inst
->Src
[1].Register
.Index
;
1397 /* some advanced gather instructions (txgo) would require 4 offsets */
1398 if (inst
->Texture
.NumOffsets
== 1) {
1400 for (dim
= 0; dim
< num_offsets
; dim
++) {
1401 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
1405 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1406 bld
->bld_base
.base
.gallivm
,
1407 bld
->bld_base
.base
.type
,
1413 lod_bias
, explicit_lod
,
1418 emit_sample(struct lp_build_tgsi_soa_context
*bld
,
1419 const struct tgsi_full_instruction
*inst
,
1420 enum lp_build_tex_modifier modifier
,
1422 LLVMValueRef
*texel
)
1424 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1425 unsigned texture_unit
, sampler_unit
;
1426 LLVMValueRef lod_bias
, explicit_lod
;
1427 LLVMValueRef coords
[4];
1428 LLVMValueRef offsets
[3] = { NULL
};
1429 struct lp_derivatives derivs
;
1430 struct lp_derivatives
*deriv_ptr
= NULL
;
1431 unsigned num_coords
, num_offsets
, num_derivs
;
1434 if (!bld
->sampler
) {
1435 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1436 for (i
= 0; i
< 4; i
++) {
1437 texel
[i
] = bld
->bld_base
.base
.undef
;
1443 * unlike old-style tex opcodes the texture/sampler indices
1444 * always come from src1 and src2 respectively.
1446 texture_unit
= inst
->Src
[1].Register
.Index
;
1447 sampler_unit
= inst
->Src
[2].Register
.Index
;
1450 * Note inst->Texture.Texture will contain the number of offsets,
1451 * however the target information is NOT there and comes from the
1452 * declared sampler views instead.
1454 switch (bld
->sv
[texture_unit
].Resource
) {
1455 case TGSI_TEXTURE_1D
:
1460 case TGSI_TEXTURE_1D_ARRAY
:
1465 case TGSI_TEXTURE_2D
:
1466 case TGSI_TEXTURE_RECT
:
1471 case TGSI_TEXTURE_2D_ARRAY
:
1476 case TGSI_TEXTURE_CUBE
:
1481 case TGSI_TEXTURE_3D
:
1486 case TGSI_TEXTURE_CUBE_ARRAY
:
1497 * unlike old-style tex opcodes the texture/sampler indices
1498 * always come from src1 and src2 respectively.
1500 texture_unit
= inst
->Src
[1].Register
.Index
;
1501 sampler_unit
= inst
->Src
[2].Register
.Index
;
1503 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
1504 lod_bias
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 3, 0 );
1505 explicit_lod
= NULL
;
1507 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
1509 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 3, 0 );
1511 else if (modifier
== LP_BLD_TEX_MODIFIER_LOD_ZERO
) {
1513 /* XXX might be better to explicitly pass the level zero information */
1514 explicit_lod
= lp_build_const_vec(gallivm
, bld
->bld_base
.base
.type
, 0.0F
);
1518 explicit_lod
= NULL
;
1521 for (i
= 0; i
< num_coords
; i
++) {
1522 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1524 for (i
= num_coords
; i
< 4; i
++) {
1525 coords
[i
] = bld
->bld_base
.base
.undef
;
1528 * XXX: whack shadow comparison value into place.
1529 * Should probably fix the interface for separate value
1530 * (it will not work for cube arrays if it is part of coords).
1533 unsigned c_coord
= num_coords
> 2 ? 3 : 2;
1534 assert(num_coords
< 4);
1535 coords
[c_coord
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 3, 0 );
1538 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
1540 for (dim
= 0; dim
< num_derivs
; ++dim
) {
1541 derivs
.ddx
[dim
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 3, dim
);
1542 derivs
.ddy
[dim
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 4, dim
);
1544 deriv_ptr
= &derivs
;
1547 /* some advanced gather instructions (txgo) would require 4 offsets */
1548 if (inst
->Texture
.NumOffsets
== 1) {
1550 for (dim
= 0; dim
< num_offsets
; dim
++) {
1551 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
1555 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1556 bld
->bld_base
.base
.gallivm
,
1557 bld
->bld_base
.base
.type
,
1559 texture_unit
, sampler_unit
,
1563 lod_bias
, explicit_lod
,
1568 emit_fetch_texels( struct lp_build_tgsi_soa_context
*bld
,
1569 const struct tgsi_full_instruction
*inst
,
1570 LLVMValueRef
*texel
,
1573 unsigned unit
, target
;
1574 LLVMValueRef coord_undef
= LLVMGetUndef(bld
->bld_base
.base
.int_vec_type
);
1575 LLVMValueRef explicit_lod
= NULL
;
1576 LLVMValueRef coords
[3];
1577 LLVMValueRef offsets
[3] = { NULL
};
1578 unsigned num_coords
;
1582 if (!bld
->sampler
) {
1583 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1584 for (i
= 0; i
< 4; i
++) {
1585 texel
[i
] = coord_undef
;
1590 unit
= inst
->Src
[1].Register
.Index
;
1593 target
= bld
->sv
[unit
].Resource
;
1596 target
= inst
->Texture
.Texture
;
1600 case TGSI_TEXTURE_1D
:
1601 case TGSI_TEXTURE_BUFFER
:
1605 case TGSI_TEXTURE_1D_ARRAY
:
1609 case TGSI_TEXTURE_2D
:
1610 case TGSI_TEXTURE_RECT
:
1614 case TGSI_TEXTURE_2D_ARRAY
:
1618 case TGSI_TEXTURE_3D
:
1627 /* always have lod except for buffers ? */
1628 if (target
!= TGSI_TEXTURE_BUFFER
) {
1629 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1632 for (i
= 0; i
< num_coords
; i
++) {
1633 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1635 for (i
= num_coords
; i
< 3; i
++) {
1636 coords
[i
] = coord_undef
;
1639 if (inst
->Texture
.NumOffsets
== 1) {
1641 for (dim
= 0; dim
< dims
; dim
++) {
1642 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
1646 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1647 bld
->bld_base
.base
.gallivm
,
1648 bld
->bld_base
.base
.type
,
1659 emit_size_query( struct lp_build_tgsi_soa_context
*bld
,
1660 const struct tgsi_full_instruction
*inst
,
1661 LLVMValueRef
*sizes_out
,
1662 boolean is_sviewinfo
)
1664 LLVMValueRef explicit_lod
;
1667 unsigned unit
= inst
->Src
[1].Register
.Index
;
1671 target
= bld
->sv
[unit
].Resource
;
1674 target
= inst
->Texture
.Texture
;
1677 case TGSI_TEXTURE_BUFFER
:
1678 case TGSI_TEXTURE_RECT
:
1679 case TGSI_TEXTURE_SHADOWRECT
:
1687 if (!bld
->sampler
) {
1688 _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
1689 for (i
= 0; i
< 4; i
++)
1690 sizes_out
[i
] = bld
->bld_base
.int_bld
.undef
;
1695 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 0 );
1697 explicit_lod
= NULL
;
1699 bld
->sampler
->emit_size_query(bld
->sampler
,
1700 bld
->bld_base
.base
.gallivm
,
1701 bld
->bld_base
.int_bld
.type
,
1709 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
1714 for (i
= 0; i
< 5; i
++) {
1717 if (pc
+ i
>= bld
->bld_base
.info
->num_instructions
)
1720 opcode
= bld
->bld_base
.instructions
[pc
+ i
].Instruction
.Opcode
;
1722 if (opcode
== TGSI_OPCODE_END
)
1725 if (opcode
== TGSI_OPCODE_TEX
||
1726 opcode
== TGSI_OPCODE_TXP
||
1727 opcode
== TGSI_OPCODE_TXD
||
1728 opcode
== TGSI_OPCODE_TXB
||
1729 opcode
== TGSI_OPCODE_TXL
||
1730 opcode
== TGSI_OPCODE_TXF
||
1731 opcode
== TGSI_OPCODE_TXQ
||
1732 opcode
== TGSI_OPCODE_CAL
||
1733 opcode
== TGSI_OPCODE_CALLNZ
||
1734 opcode
== TGSI_OPCODE_IF
||
1735 opcode
== TGSI_OPCODE_BGNLOOP
||
1736 opcode
== TGSI_OPCODE_SWITCH
)
1746 * Kill fragment if any of the src register values are negative.
1750 struct lp_build_tgsi_soa_context
*bld
,
1751 const struct tgsi_full_instruction
*inst
,
1754 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1755 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1756 LLVMValueRef terms
[TGSI_NUM_CHANNELS
];
1758 unsigned chan_index
;
1760 memset(&terms
, 0, sizeof terms
);
1762 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1765 /* Unswizzle channel */
1766 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
1768 /* Check if the component has not been already tested. */
1769 assert(swizzle
< TGSI_NUM_CHANNELS
);
1770 if( !terms
[swizzle
] )
1771 /* TODO: change the comparison operator instead of setting the sign */
1772 terms
[swizzle
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, chan_index
);
1776 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1777 if(terms
[chan_index
]) {
1778 LLVMValueRef chan_mask
;
1781 * If term < 0 then mask = 0 else mask = ~0.
1783 chan_mask
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->bld_base
.base
.zero
);
1786 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
1793 lp_build_mask_update(bld
->mask
, mask
);
1795 if (!near_end_of_shader(bld
, pc
))
1796 lp_build_mask_check(bld
->mask
);
1802 * Predicated fragment kill.
1803 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1804 * The only predication is the execution mask which will apply if
1805 * we're inside a loop or conditional.
1808 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
1811 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1814 /* For those channels which are "alive", disable fragment shader
1817 if (bld
->exec_mask
.has_mask
) {
1818 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1821 LLVMValueRef zero
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
1825 lp_build_mask_update(bld
->mask
, mask
);
1827 if (!near_end_of_shader(bld
, pc
))
1828 lp_build_mask_check(bld
->mask
);
1833 * Emit code which will dump the value of all the temporary registers
1837 emit_dump_temps(struct lp_build_tgsi_soa_context
*bld
)
1839 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1840 LLVMBuilderRef builder
= gallivm
->builder
;
1841 LLVMValueRef temp_ptr
;
1842 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
1843 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
1844 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
1845 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
1847 int n
= bld
->bld_base
.info
->file_max
[TGSI_FILE_TEMPORARY
];
1849 for (index
= 0; index
< n
; index
++) {
1850 LLVMValueRef idx
= lp_build_const_int32(gallivm
, index
);
1851 LLVMValueRef v
[4][4], res
;
1854 lp_build_printf(gallivm
, "TEMP[%d]:\n", idx
);
1856 for (chan
= 0; chan
< 4; chan
++) {
1857 temp_ptr
= lp_get_temp_ptr_soa(bld
, index
, chan
);
1858 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1859 v
[chan
][0] = LLVMBuildExtractElement(builder
, res
, i0
, "");
1860 v
[chan
][1] = LLVMBuildExtractElement(builder
, res
, i1
, "");
1861 v
[chan
][2] = LLVMBuildExtractElement(builder
, res
, i2
, "");
1862 v
[chan
][3] = LLVMBuildExtractElement(builder
, res
, i3
, "");
1865 lp_build_printf(gallivm
, " X: %f %f %f %f\n",
1866 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
1867 lp_build_printf(gallivm
, " Y: %f %f %f %f\n",
1868 v
[1][0], v
[1][1], v
[1][2], v
[1][3]);
1869 lp_build_printf(gallivm
, " Z: %f %f %f %f\n",
1870 v
[2][0], v
[2][1], v
[2][2], v
[2][3]);
1871 lp_build_printf(gallivm
, " W: %f %f %f %f\n",
1872 v
[3][0], v
[3][1], v
[3][2], v
[3][3]);
1879 lp_emit_declaration_soa(
1880 struct lp_build_tgsi_context
*bld_base
,
1881 const struct tgsi_full_declaration
*decl
)
1883 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1884 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1885 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
1886 const unsigned first
= decl
->Range
.First
;
1887 const unsigned last
= decl
->Range
.Last
;
1890 for (idx
= first
; idx
<= last
; ++idx
) {
1891 assert(last
<= bld
->bld_base
.info
->file_max
[decl
->Declaration
.File
]);
1892 switch (decl
->Declaration
.File
) {
1893 case TGSI_FILE_TEMPORARY
:
1894 assert(idx
< LP_MAX_TGSI_TEMPS
);
1895 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
1896 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1897 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
1901 case TGSI_FILE_OUTPUT
:
1902 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
1903 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1904 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
1905 vec_type
, "output");
1909 case TGSI_FILE_ADDRESS
:
1910 /* ADDR registers are only allocated with an integer LLVM IR type,
1911 * as they are guaranteed to always have integers.
1912 * XXX: Not sure if this exception is worthwhile (or the whole idea of
1913 * an ADDR register for that matter).
1915 assert(idx
< LP_MAX_TGSI_ADDRS
);
1916 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1917 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, bld_base
->base
.int_vec_type
, "addr");
1920 case TGSI_FILE_PREDICATE
:
1921 assert(idx
< LP_MAX_TGSI_PREDS
);
1922 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1923 bld
->preds
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
,
1927 case TGSI_FILE_SAMPLER_VIEW
:
1929 * The target stored here MUST match whatever there actually
1930 * is in the set sampler views (what about return type?).
1932 assert(idx
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
1933 bld
->sv
[idx
] = decl
->SamplerView
;
1937 /* don't need to declare other vars */
1944 void lp_emit_immediate_soa(
1945 struct lp_build_tgsi_context
*bld_base
,
1946 const struct tgsi_full_immediate
*imm
)
1948 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1949 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1951 /* simply copy the immediate values into the next immediates[] slot */
1953 const uint size
= imm
->Immediate
.NrTokens
- 1;
1955 assert(bld
->num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1956 switch (imm
->Immediate
.DataType
) {
1957 case TGSI_IMM_FLOAT32
:
1958 for( i
= 0; i
< size
; ++i
)
1959 bld
->immediates
[bld
->num_immediates
][i
] =
1960 lp_build_const_vec(gallivm
, bld_base
->base
.type
, imm
->u
[i
].Float
);
1963 case TGSI_IMM_UINT32
:
1964 for( i
= 0; i
< size
; ++i
) {
1965 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->uint_bld
.type
, imm
->u
[i
].Uint
);
1966 bld
->immediates
[bld
->num_immediates
][i
] =
1967 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1971 case TGSI_IMM_INT32
:
1972 for( i
= 0; i
< size
; ++i
) {
1973 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->int_bld
.type
, imm
->u
[i
].Int
);
1974 bld
->immediates
[bld
->num_immediates
][i
] =
1975 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1980 for( i
= size
; i
< 4; ++i
)
1981 bld
->immediates
[bld
->num_immediates
][i
] = bld_base
->base
.undef
;
1983 bld
->num_immediates
++;
1988 const struct lp_build_tgsi_action
* action
,
1989 struct lp_build_tgsi_context
* bld_base
,
1990 struct lp_build_emit_data
* emit_data
)
1992 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1994 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
,
1995 &emit_data
->output
[emit_data
->chan
], NULL
);
2000 const struct lp_build_tgsi_action
* action
,
2001 struct lp_build_tgsi_context
* bld_base
,
2002 struct lp_build_emit_data
* emit_data
)
2004 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2006 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
, NULL
,
2007 &emit_data
->output
[emit_data
->chan
]);
2012 const struct lp_build_tgsi_action
* action
,
2013 struct lp_build_tgsi_context
* bld_base
,
2014 struct lp_build_emit_data
* emit_data
)
2016 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2018 emit_kilp(bld
, bld_base
->pc
- 1);
2023 const struct lp_build_tgsi_action
* action
,
2024 struct lp_build_tgsi_context
* bld_base
,
2025 struct lp_build_emit_data
* emit_data
)
2027 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2029 emit_kil(bld
, emit_data
->inst
, bld_base
->pc
- 1);
2034 const struct lp_build_tgsi_action
* action
,
2035 struct lp_build_tgsi_context
* bld_base
,
2036 struct lp_build_emit_data
* emit_data
)
2038 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2040 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
, emit_data
->output
);
2045 const struct lp_build_tgsi_action
* action
,
2046 struct lp_build_tgsi_context
* bld_base
,
2047 struct lp_build_emit_data
* emit_data
)
2049 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2051 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
2057 const struct lp_build_tgsi_action
* action
,
2058 struct lp_build_tgsi_context
* bld_base
,
2059 struct lp_build_emit_data
* emit_data
)
2061 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2063 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
2069 const struct lp_build_tgsi_action
* action
,
2070 struct lp_build_tgsi_context
* bld_base
,
2071 struct lp_build_emit_data
* emit_data
)
2073 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2075 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
2081 const struct lp_build_tgsi_action
* action
,
2082 struct lp_build_tgsi_context
* bld_base
,
2083 struct lp_build_emit_data
* emit_data
)
2085 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2087 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_PROJECTED
,
2093 const struct lp_build_tgsi_action
* action
,
2094 struct lp_build_tgsi_context
* bld_base
,
2095 struct lp_build_emit_data
* emit_data
)
2097 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2099 emit_size_query(bld
, emit_data
->inst
, emit_data
->output
, FALSE
);
2104 const struct lp_build_tgsi_action
* action
,
2105 struct lp_build_tgsi_context
* bld_base
,
2106 struct lp_build_emit_data
* emit_data
)
2108 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2110 emit_fetch_texels(bld
, emit_data
->inst
, emit_data
->output
, FALSE
);
2115 const struct lp_build_tgsi_action
* action
,
2116 struct lp_build_tgsi_context
* bld_base
,
2117 struct lp_build_emit_data
* emit_data
)
2119 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2121 emit_fetch_texels(bld
, emit_data
->inst
, emit_data
->output
, TRUE
);
2126 const struct lp_build_tgsi_action
* action
,
2127 struct lp_build_tgsi_context
* bld_base
,
2128 struct lp_build_emit_data
* emit_data
)
2130 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2132 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
2133 FALSE
, emit_data
->output
);
2138 const struct lp_build_tgsi_action
* action
,
2139 struct lp_build_tgsi_context
* bld_base
,
2140 struct lp_build_emit_data
* emit_data
)
2142 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2144 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
2145 FALSE
, emit_data
->output
);
2150 const struct lp_build_tgsi_action
* action
,
2151 struct lp_build_tgsi_context
* bld_base
,
2152 struct lp_build_emit_data
* emit_data
)
2154 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2156 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
2157 TRUE
, emit_data
->output
);
2162 const struct lp_build_tgsi_action
* action
,
2163 struct lp_build_tgsi_context
* bld_base
,
2164 struct lp_build_emit_data
* emit_data
)
2166 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2168 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_ZERO
,
2169 TRUE
, emit_data
->output
);
2174 const struct lp_build_tgsi_action
* action
,
2175 struct lp_build_tgsi_context
* bld_base
,
2176 struct lp_build_emit_data
* emit_data
)
2178 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2180 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
2181 FALSE
, emit_data
->output
);
2186 const struct lp_build_tgsi_action
* action
,
2187 struct lp_build_tgsi_context
* bld_base
,
2188 struct lp_build_emit_data
* emit_data
)
2190 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2192 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
2193 FALSE
, emit_data
->output
);
2198 const struct lp_build_tgsi_action
* action
,
2199 struct lp_build_tgsi_context
* bld_base
,
2200 struct lp_build_emit_data
* emit_data
)
2202 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2204 emit_size_query(bld
, emit_data
->inst
, emit_data
->output
, TRUE
);
2208 mask_to_one_vec(struct lp_build_tgsi_context
*bld_base
)
2210 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2211 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
2212 LLVMValueRef one_vec
= bld_base
->int_bld
.one
;
2213 struct lp_exec_mask
*exec_mask
= &bld
->exec_mask
;
2215 if (exec_mask
->has_mask
) {
2216 one_vec
= LLVMBuildAnd(builder
, one_vec
, exec_mask
->exec_mask
, "");
2218 one_vec
= LLVMBuildAnd(builder
, one_vec
,
2219 lp_build_mask_value(bld
->mask
), "");
2224 increment_vec_ptr_by_mask(struct lp_build_tgsi_context
* bld_base
,
2228 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2230 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
2232 current_vec
= LLVMBuildAdd(builder
, current_vec
, mask
, "");
2234 LLVMBuildStore(builder
, current_vec
, ptr
);
2238 clear_uint_vec_ptr_from_mask(struct lp_build_tgsi_context
* bld_base
,
2242 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2244 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
2245 LLVMValueRef full_mask
= lp_build_cmp(&bld_base
->uint_bld
,
2248 bld_base
->uint_bld
.zero
);
2250 current_vec
= lp_build_select(&bld_base
->uint_bld
,
2252 bld_base
->uint_bld
.zero
,
2255 LLVMBuildStore(builder
, current_vec
, ptr
);
2260 const struct lp_build_tgsi_action
* action
,
2261 struct lp_build_tgsi_context
* bld_base
,
2262 struct lp_build_emit_data
* emit_data
)
2264 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2265 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
2267 if (bld
->gs_iface
->emit_vertex
) {
2268 LLVMValueRef masked_ones
= mask_to_one_vec(bld_base
);
2269 LLVMValueRef total_emitted_vertices_vec
=
2270 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
2271 gather_outputs(bld
);
2272 bld
->gs_iface
->emit_vertex(bld
->gs_iface
, &bld
->bld_base
,
2274 total_emitted_vertices_vec
);
2275 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
2277 increment_vec_ptr_by_mask(bld_base
, bld
->total_emitted_vertices_vec_ptr
,
2280 lp_build_print_value(bld
->bld_base
.base
.gallivm
, " +++ emit vertex masked ones = ",
2282 lp_build_print_value(bld
->bld_base
.base
.gallivm
, " +++ emit vertex emitted = ",
2283 total_emitted_vertices_vec
);
2285 bld
->pending_end_primitive
= TRUE
;
2292 const struct lp_build_tgsi_action
* action
,
2293 struct lp_build_tgsi_context
* bld_base
,
2294 struct lp_build_emit_data
* emit_data
)
2296 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2297 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
2299 if (bld
->gs_iface
->end_primitive
) {
2300 LLVMValueRef masked_ones
= mask_to_one_vec(bld_base
);
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
, " +++ end prim masked ones = ",
2313 lp_build_print_value(bld
->bld_base
.base
.gallivm
, " +++ end prim emitted verts1 = ",
2314 emitted_vertices_vec
);
2315 lp_build_print_value(bld
->bld_base
.base
.gallivm
, " +++ end prim emitted prims1 = ",
2316 LLVMBuildLoad(builder
, bld
->emitted_prims_vec_ptr
, ""));
2318 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_prims_vec_ptr
,
2320 clear_uint_vec_ptr_from_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
2323 lp_build_print_value(bld
->bld_base
.base
.gallivm
, " +++ end prim emitted verts2 = ",
2324 LLVMBuildLoad(builder
, bld
->emitted_vertices_vec_ptr
, ""));
2327 bld
->pending_end_primitive
= FALSE
;
2333 const struct lp_build_tgsi_action
* action
,
2334 struct lp_build_tgsi_context
* bld_base
,
2335 struct lp_build_emit_data
* emit_data
)
2337 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2339 lp_exec_mask_call(&bld
->exec_mask
, emit_data
->inst
->Label
.Label
,
2345 const struct lp_build_tgsi_action
* action
,
2346 struct lp_build_tgsi_context
* bld_base
,
2347 struct lp_build_emit_data
* emit_data
)
2349 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2351 lp_exec_mask_ret(&bld
->exec_mask
, &bld_base
->pc
);
2356 const struct lp_build_tgsi_action
* action
,
2357 struct lp_build_tgsi_context
* bld_base
,
2358 struct lp_build_emit_data
* emit_data
)
2360 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2362 lp_exec_break(&bld
->exec_mask
);
2367 const struct lp_build_tgsi_action
* action
,
2368 struct lp_build_tgsi_context
* bld_base
,
2369 struct lp_build_emit_data
* emit_data
)
2371 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2372 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2373 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
2374 LLVMValueRef unsigned_cond
=
2375 LLVMBuildBitCast(builder
, emit_data
->args
[0], uint_bld
->vec_type
, "");
2376 LLVMValueRef cond
= lp_build_cmp(uint_bld
, PIPE_FUNC_NOTEQUAL
,
2380 lp_exec_break_condition(&bld
->exec_mask
, cond
);
2385 const struct lp_build_tgsi_action
* action
,
2386 struct lp_build_tgsi_context
* bld_base
,
2387 struct lp_build_emit_data
* emit_data
)
2390 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2392 tmp
= lp_build_cmp(&bld_base
->base
, PIPE_FUNC_NOTEQUAL
,
2393 emit_data
->args
[0], bld
->bld_base
.base
.zero
);
2394 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
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
);
2405 lp_exec_bgnloop(&bld
->exec_mask
);
2410 const struct lp_build_tgsi_action
* action
,
2411 struct lp_build_tgsi_context
* bld_base
,
2412 struct lp_build_emit_data
* emit_data
)
2414 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2416 lp_exec_mask_bgnsub(&bld
->exec_mask
);
2421 const struct lp_build_tgsi_action
* action
,
2422 struct lp_build_tgsi_context
* bld_base
,
2423 struct lp_build_emit_data
* emit_data
)
2425 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2427 lp_exec_mask_cond_invert(&bld
->exec_mask
);
2432 const struct lp_build_tgsi_action
* action
,
2433 struct lp_build_tgsi_context
* bld_base
,
2434 struct lp_build_emit_data
* emit_data
)
2436 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2438 lp_exec_mask_cond_pop(&bld
->exec_mask
);
2443 const struct lp_build_tgsi_action
* action
,
2444 struct lp_build_tgsi_context
* bld_base
,
2445 struct lp_build_emit_data
* emit_data
)
2447 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2449 lp_exec_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
2454 const struct lp_build_tgsi_action
* action
,
2455 struct lp_build_tgsi_context
* bld_base
,
2456 struct lp_build_emit_data
* emit_data
)
2458 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2460 lp_exec_mask_endsub(&bld
->exec_mask
, &bld_base
->pc
);
2465 const struct lp_build_tgsi_action
* action
,
2466 struct lp_build_tgsi_context
* bld_base
,
2467 struct lp_build_emit_data
* emit_data
)
2469 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2471 lp_exec_continue(&bld
->exec_mask
);
2474 /* XXX: Refactor and move it to lp_bld_tgsi_action.c
2476 * XXX: What do the comments about xmm registers mean? Maybe they are left over
2477 * from old code, but there is no garauntee that LLVM will use those registers
2480 * XXX: There should be no calls to lp_build_emit_fetch in this function. This
2481 * should be handled by the emit_data->fetch_args function. */
2484 const struct lp_build_tgsi_action
* action
,
2485 struct lp_build_tgsi_context
* bld_base
,
2486 struct lp_build_emit_data
* emit_data
)
2488 LLVMValueRef tmp0
, tmp1
;
2489 LLVMValueRef tmp4
= NULL
;
2490 LLVMValueRef tmp5
= NULL
;
2491 LLVMValueRef tmp6
= NULL
;
2492 LLVMValueRef tmp7
= NULL
;
2493 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2495 uint dims
= (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
2497 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) ||
2498 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
) ||
2499 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
) ||
2500 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 4)) {
2502 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
2505 /* xmm0 = src.x * src.x */
2506 tmp0
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
2507 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
2510 tmp0
= lp_build_mul( &bld
->bld_base
.base
, tmp0
, tmp0
);
2513 /* xmm0 = xmm0 + src.y * src.y */
2514 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Y
);
2515 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
2518 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
2519 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
2522 /* xmm0 = xmm0 + src.z * src.z */
2523 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Z
);
2524 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
2527 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
2528 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
2532 /* xmm0 = xmm0 + src.w * src.w */
2533 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
2534 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
)) {
2537 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
2538 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
2540 /* xmm1 = 1 / sqrt(xmm0) */
2541 tmp1
= lp_build_rsqrt( &bld
->bld_base
.base
, tmp0
);
2542 /* dst.x = xmm1 * src.x */
2543 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
2544 emit_data
->output
[TGSI_CHAN_X
] = lp_build_mul( &bld
->bld_base
.base
, tmp4
, tmp1
);
2546 /* dst.y = xmm1 * src.y */
2547 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
2548 emit_data
->output
[TGSI_CHAN_Y
] = lp_build_mul( &bld
->bld_base
.base
, tmp5
, tmp1
);
2551 /* dst.z = xmm1 * src.z */
2552 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
2553 emit_data
->output
[TGSI_CHAN_Z
] = lp_build_mul( &bld
->bld_base
.base
, tmp6
, tmp1
);
2555 /* dst.w = xmm1 * src.w */
2556 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) && dims
== 4) {
2557 emit_data
->output
[TGSI_CHAN_W
] = lp_build_mul( &bld
->bld_base
.base
, tmp7
, tmp1
);
2562 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 3) {
2563 emit_data
->output
[TGSI_CHAN_W
] = bld
->bld_base
.base
.one
;
2567 static void emit_prologue(struct lp_build_tgsi_context
* bld_base
)
2569 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2570 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2572 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
2573 LLVMValueRef array_size
=
2574 lp_build_const_int32(gallivm
,
2575 bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
2576 bld
->temps_array
= lp_build_array_alloca(gallivm
,
2577 bld_base
->base
.vec_type
, array_size
,
2581 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
2582 LLVMValueRef array_size
=
2583 lp_build_const_int32(gallivm
,
2584 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
2585 bld
->outputs_array
= lp_build_array_alloca(gallivm
,
2586 bld_base
->base
.vec_type
, array_size
,
2590 /* If we have indirect addressing in inputs we need to copy them into
2591 * our alloca array to be able to iterate over them */
2592 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
) && !bld
->gs_iface
) {
2593 unsigned index
, chan
;
2594 LLVMTypeRef vec_type
= bld_base
->base
.vec_type
;
2595 LLVMValueRef array_size
= lp_build_const_int32(gallivm
,
2596 bld_base
->info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
2597 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
2598 vec_type
, array_size
,
2601 assert(bld_base
->info
->num_inputs
2602 <= bld_base
->info
->file_max
[TGSI_FILE_INPUT
] + 1);
2604 for (index
= 0; index
< bld_base
->info
->num_inputs
; ++index
) {
2605 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
2606 LLVMValueRef lindex
=
2607 lp_build_const_int32(gallivm
, index
* 4 + chan
);
2608 LLVMValueRef input_ptr
=
2609 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
2611 LLVMValueRef value
= bld
->inputs
[index
][chan
];
2613 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
2618 if (bld
->gs_iface
) {
2619 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
2620 bld
->emitted_prims_vec_ptr
=
2621 lp_build_alloca(gallivm
,
2623 "emitted_prims_ptr");
2624 bld
->emitted_vertices_vec_ptr
=
2625 lp_build_alloca(gallivm
,
2627 "emitted_vertices_ptr");
2628 bld
->total_emitted_vertices_vec_ptr
=
2629 lp_build_alloca(gallivm
,
2631 "total_emitted_vertices_ptr");
2633 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
2634 bld
->emitted_prims_vec_ptr
);
2635 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
2636 bld
->emitted_vertices_vec_ptr
);
2637 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
2638 bld
->total_emitted_vertices_vec_ptr
);
2642 static void emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
2644 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2645 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2649 emit_dump_temps(bld
);
2652 /* If we have indirect addressing in outputs we need to copy our alloca array
2653 * to the outputs slots specified by the caller */
2654 if (bld
->gs_iface
) {
2655 LLVMValueRef total_emitted_vertices_vec
;
2656 LLVMValueRef emitted_prims_vec
;
2657 /* flush the accumulated vertices as a primitive */
2658 if (bld
->pending_end_primitive
) {
2659 end_primitive(NULL
, bld_base
, NULL
);
2660 bld
->pending_end_primitive
= FALSE
;
2662 total_emitted_vertices_vec
=
2663 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
2665 LLVMBuildLoad(builder
, bld
->emitted_prims_vec_ptr
, "");
2667 bld
->gs_iface
->gs_epilogue(bld
->gs_iface
,
2669 total_emitted_vertices_vec
,
2672 gather_outputs(bld
);
2677 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
2678 const struct tgsi_token
*tokens
,
2679 struct lp_type type
,
2680 struct lp_build_mask_context
*mask
,
2681 LLVMValueRef consts_ptr
,
2682 const struct lp_bld_tgsi_system_values
*system_values
,
2683 const LLVMValueRef
*pos
,
2684 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
2685 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
2686 struct lp_build_sampler_soa
*sampler
,
2687 const struct tgsi_shader_info
*info
,
2688 const struct lp_build_tgsi_gs_iface
*gs_iface
)
2690 struct lp_build_tgsi_soa_context bld
;
2692 struct lp_type res_type
;
2694 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
2695 memset(&res_type
, 0, sizeof res_type
);
2696 res_type
.width
= type
.width
;
2697 res_type
.length
= type
.length
;
2700 /* Setup build context */
2701 memset(&bld
, 0, sizeof bld
);
2702 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
2703 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
2704 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
2705 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
2708 bld
.inputs
= inputs
;
2709 bld
.outputs
= outputs
;
2710 bld
.consts_ptr
= consts_ptr
;
2711 bld
.sampler
= sampler
;
2712 bld
.bld_base
.info
= info
;
2713 bld
.indirect_files
= info
->indirect_files
;
2715 bld
.bld_base
.soa
= TRUE
;
2716 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
2717 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
2718 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
2719 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
2720 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = emit_fetch_system_value
;
2721 bld
.bld_base
.emit_store
= emit_store
;
2723 bld
.bld_base
.emit_declaration
= lp_emit_declaration_soa
;
2724 bld
.bld_base
.emit_immediate
= lp_emit_immediate_soa
;
2726 bld
.bld_base
.emit_prologue
= emit_prologue
;
2727 bld
.bld_base
.emit_epilogue
= emit_epilogue
;
2729 /* Set opcode actions */
2730 lp_set_default_actions_cpu(&bld
.bld_base
);
2732 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
2733 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNSUB
].emit
= bgnsub_emit
;
2734 bld
.bld_base
.op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
2735 bld
.bld_base
.op_actions
[TGSI_OPCODE_BREAKC
].emit
= breakc_emit
;
2736 bld
.bld_base
.op_actions
[TGSI_OPCODE_CAL
].emit
= cal_emit
;
2737 bld
.bld_base
.op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
2738 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDX
].emit
= ddx_emit
;
2739 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDY
].emit
= ddy_emit
;
2740 bld
.bld_base
.op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
2741 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
2742 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
2743 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSUB
].emit
= endsub_emit
;
2744 bld
.bld_base
.op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
2745 bld
.bld_base
.op_actions
[TGSI_OPCODE_KIL
].emit
= kil_emit
;
2746 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILP
].emit
= kilp_emit
;
2747 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM
].emit
= nrm_emit
;
2748 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM4
].emit
= nrm_emit
;
2749 bld
.bld_base
.op_actions
[TGSI_OPCODE_RET
].emit
= ret_emit
;
2750 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX
].emit
= tex_emit
;
2751 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB
].emit
= txb_emit
;
2752 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXD
].emit
= txd_emit
;
2753 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL
].emit
= txl_emit
;
2754 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXP
].emit
= txp_emit
;
2755 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
2756 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXF
].emit
= txf_emit
;
2757 /* DX10 sampling ops */
2758 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE
].emit
= sample_emit
;
2759 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_B
].emit
= sample_b_emit
;
2760 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_C
].emit
= sample_c_emit
;
2761 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_C_LZ
].emit
= sample_c_lz_emit
;
2762 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_D
].emit
= sample_d_emit
;
2763 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_I
].emit
= sample_i_emit
;
2764 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_L
].emit
= sample_l_emit
;
2765 bld
.bld_base
.op_actions
[TGSI_OPCODE_SVIEWINFO
].emit
= sviewinfo_emit
;
2768 /* inputs are always indirect with gs */
2769 bld
.indirect_files
|= (1 << TGSI_FILE_INPUT
);
2770 bld
.gs_iface
= gs_iface
;
2771 bld
.pending_end_primitive
= FALSE
;
2772 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_gs_input
;
2773 bld
.bld_base
.op_actions
[TGSI_OPCODE_EMIT
].emit
= emit_vertex
;
2774 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= end_primitive
;
2777 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.base
);
2779 bld
.system_values
= *system_values
;
2781 lp_build_tgsi_llvm(&bld
.bld_base
, tokens
);
2784 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
2785 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2786 debug_printf("11111111111111111111111111111 \n");
2787 tgsi_dump(tokens
, 0);
2788 lp_debug_dump_value(function
);
2789 debug_printf("2222222222222222222222222222 \n");
2793 LLVMModuleRef module
= LLVMGetGlobalParent(
2794 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
2795 LLVMDumpModule(module
);