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"
67 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
69 LLVMTypeRef int_type
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
70 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
73 mask
->has_mask
= FALSE
;
74 mask
->cond_stack_size
= 0;
75 mask
->loop_stack_size
= 0;
76 mask
->call_stack_size
= 0;
78 mask
->int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, mask
->bld
->type
);
79 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
80 LLVMConstAllOnes(mask
->int_vec_type
);
82 mask
->loop_limiter
= lp_build_alloca(bld
->gallivm
, int_type
, "looplimiter");
86 LLVMConstInt(int_type
, LP_MAX_TGSI_LOOP_ITERATIONS
, false),
90 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
92 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
94 if (mask
->loop_stack_size
) {
95 /*for loops we need to update the entire mask at runtime */
97 assert(mask
->break_mask
);
98 tmp
= LLVMBuildAnd(builder
,
102 mask
->exec_mask
= LLVMBuildAnd(builder
,
107 mask
->exec_mask
= mask
->cond_mask
;
109 if (mask
->call_stack_size
) {
110 mask
->exec_mask
= LLVMBuildAnd(builder
,
116 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
117 mask
->loop_stack_size
> 0 ||
118 mask
->call_stack_size
> 0);
121 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
124 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
126 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
127 if (mask
->cond_stack_size
== 0) {
128 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
130 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
131 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
132 mask
->cond_mask
= LLVMBuildAnd(builder
,
136 lp_exec_mask_update(mask
);
139 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
141 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
142 LLVMValueRef prev_mask
;
143 LLVMValueRef inv_mask
;
145 assert(mask
->cond_stack_size
);
146 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
147 if (mask
->cond_stack_size
== 1) {
148 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
151 inv_mask
= LLVMBuildNot(builder
, mask
->cond_mask
, "");
153 mask
->cond_mask
= LLVMBuildAnd(builder
,
156 lp_exec_mask_update(mask
);
159 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
161 assert(mask
->cond_stack_size
);
162 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
163 lp_exec_mask_update(mask
);
166 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
168 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
170 if (mask
->loop_stack_size
== 0) {
171 assert(mask
->loop_block
== NULL
);
172 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
173 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
174 assert(mask
->break_var
== NULL
);
177 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
179 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
180 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
181 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
182 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
183 ++mask
->loop_stack_size
;
185 mask
->break_var
= lp_build_alloca(mask
->bld
->gallivm
, mask
->int_vec_type
, "");
186 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
188 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->gallivm
, "bgnloop");
190 LLVMBuildBr(builder
, mask
->loop_block
);
191 LLVMPositionBuilderAtEnd(builder
, mask
->loop_block
);
193 mask
->break_mask
= LLVMBuildLoad(builder
, mask
->break_var
, "");
195 lp_exec_mask_update(mask
);
198 static void lp_exec_break(struct lp_exec_mask
*mask
)
200 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
201 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
205 mask
->break_mask
= LLVMBuildAnd(builder
,
207 exec_mask
, "break_full");
209 lp_exec_mask_update(mask
);
212 static void lp_exec_continue(struct lp_exec_mask
*mask
)
214 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
215 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
219 mask
->cont_mask
= LLVMBuildAnd(builder
,
223 lp_exec_mask_update(mask
);
227 static void lp_exec_endloop(struct gallivm_state
*gallivm
,
228 struct lp_exec_mask
*mask
)
230 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
231 LLVMBasicBlockRef endloop
;
232 LLVMTypeRef int_type
= LLVMInt32TypeInContext(mask
->bld
->gallivm
->context
);
233 LLVMTypeRef reg_type
= LLVMIntTypeInContext(gallivm
->context
,
234 mask
->bld
->type
.width
*
235 mask
->bld
->type
.length
);
236 LLVMValueRef i1cond
, i2cond
, icond
, limiter
;
238 assert(mask
->break_mask
);
241 * Restore the cont_mask, but don't pop
243 assert(mask
->loop_stack_size
);
244 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
245 lp_exec_mask_update(mask
);
248 * Unlike the continue mask, the break_mask must be preserved across loop
251 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
253 /* Decrement the loop limiter */
254 limiter
= LLVMBuildLoad(builder
, mask
->loop_limiter
, "");
256 limiter
= LLVMBuildSub(
259 LLVMConstInt(int_type
, 1, false),
262 LLVMBuildStore(builder
, limiter
, mask
->loop_limiter
);
264 /* i1cond = (mask != 0) */
265 i1cond
= LLVMBuildICmp(
268 LLVMBuildBitCast(builder
, mask
->exec_mask
, reg_type
, ""),
269 LLVMConstNull(reg_type
), "");
271 /* i2cond = (looplimiter > 0) */
272 i2cond
= LLVMBuildICmp(
276 LLVMConstNull(int_type
), "");
278 /* if( i1cond && i2cond ) */
279 icond
= LLVMBuildAnd(builder
, i1cond
, i2cond
, "");
281 endloop
= lp_build_insert_new_block(mask
->bld
->gallivm
, "endloop");
283 LLVMBuildCondBr(builder
,
284 icond
, mask
->loop_block
, endloop
);
286 LLVMPositionBuilderAtEnd(builder
, endloop
);
288 assert(mask
->loop_stack_size
);
289 --mask
->loop_stack_size
;
290 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
291 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
292 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
293 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
295 lp_exec_mask_update(mask
);
298 /* stores val into an address pointed to by dst.
299 * mask->exec_mask is used to figure out which bits of val
300 * should be stored into the address
301 * (0 means don't store this bit, 1 means do store).
303 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
304 struct lp_build_context
*bld_store
,
309 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
311 /* Mix the predicate and execution mask */
312 if (mask
->has_mask
) {
314 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
316 pred
= mask
->exec_mask
;
321 LLVMValueRef real_val
, dst_val
;
323 dst_val
= LLVMBuildLoad(builder
, dst
, "");
324 real_val
= lp_build_select(bld_store
,
328 LLVMBuildStore(builder
, real_val
, dst
);
330 LLVMBuildStore(builder
, val
, dst
);
333 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
337 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
338 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
339 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
340 mask
->call_stack_size
++;
344 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
346 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
347 LLVMValueRef exec_mask
;
349 if (mask
->call_stack_size
== 0) {
350 /* returning from main() */
354 exec_mask
= LLVMBuildNot(builder
,
358 mask
->ret_mask
= LLVMBuildAnd(builder
,
360 exec_mask
, "ret_full");
362 lp_exec_mask_update(mask
);
365 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
369 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
371 assert(mask
->call_stack_size
);
372 mask
->call_stack_size
--;
373 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
374 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
375 lp_exec_mask_update(mask
);
380 * Return pointer to a temporary register channel (src or dest).
381 * Note that indirect addressing cannot be handled here.
382 * \param index which temporary register
383 * \param chan which channel of the temp register.
386 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context
*bld
,
390 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
392 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
393 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, index
* 4 + chan
);
394 return LLVMBuildGEP(builder
, bld
->temps_array
, &lindex
, 1, "");
397 return bld
->temps
[index
][chan
];
402 * Return pointer to a output register channel (src or dest).
403 * Note that indirect addressing cannot be handled here.
404 * \param index which output register
405 * \param chan which channel of the output register.
408 lp_get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
412 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
414 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
415 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
,
417 return LLVMBuildGEP(builder
, bld
->outputs_array
, &lindex
, 1, "");
420 return bld
->outputs
[index
][chan
];
426 * XXX the lp_build_gather() function should be capable of doing this
427 * with a little work.
430 build_gather(struct lp_build_context
*bld
,
431 LLVMValueRef base_ptr
,
432 LLVMValueRef indexes
)
434 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
435 LLVMValueRef res
= bld
->undef
;
439 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
441 for (i
= 0; i
< bld
->type
.length
; i
++) {
442 LLVMValueRef ii
= lp_build_const_int32(bld
->gallivm
, i
);
443 LLVMValueRef index
= LLVMBuildExtractElement(builder
,
445 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
446 &index
, 1, "gather_ptr");
447 LLVMValueRef scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
449 res
= LLVMBuildInsertElement(builder
, res
, scalar
, ii
, "");
457 * Scatter/store vector.
460 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
461 LLVMValueRef base_ptr
,
462 LLVMValueRef indexes
,
464 struct lp_exec_mask
*mask
,
467 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
468 LLVMBuilderRef builder
= gallivm
->builder
;
471 /* Mix the predicate and execution mask */
472 if (mask
->has_mask
) {
474 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
477 pred
= mask
->exec_mask
;
482 * Loop over elements of index_vec, store scalar value.
484 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
485 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
486 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
487 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
488 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
489 LLVMValueRef scalar_pred
= pred
?
490 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
493 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
494 ii
, val
, index
, scalar_ptr
);
497 LLVMValueRef real_val
, dst_val
;
498 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
499 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
500 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
503 LLVMBuildStore(builder
, val
, scalar_ptr
);
510 * Read the current value of the ADDR register, convert the floats to
511 * ints, add the base index and return the vector of offsets.
512 * The offsets will be used to index into the constant buffer or
513 * temporary register file.
516 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
517 unsigned reg_file
, unsigned reg_index
,
518 const struct tgsi_src_register
*indirect_reg
)
520 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
521 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
522 /* always use X component of address register */
523 unsigned swizzle
= indirect_reg
->SwizzleX
;
526 LLVMValueRef max_index
;
529 assert(bld
->indirect_files
& (1 << reg_file
));
531 base
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, reg_index
);
534 rel
= LLVMBuildLoad(builder
,
535 bld
->addr
[indirect_reg
->Index
][swizzle
],
538 index
= lp_build_add(uint_bld
, base
, rel
);
540 max_index
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
,
542 bld
->bld_base
.info
->file_max
[reg_file
]);
544 assert(!uint_bld
->type
.sign
);
545 index
= lp_build_min(uint_bld
, index
, max_index
);
550 static struct lp_build_context
*
551 stype_to_fetch(struct lp_build_tgsi_context
* bld_base
,
552 enum tgsi_opcode_type stype
)
554 struct lp_build_context
*bld_fetch
;
557 case TGSI_TYPE_FLOAT
:
558 case TGSI_TYPE_UNTYPED
:
559 bld_fetch
= &bld_base
->base
;
561 case TGSI_TYPE_UNSIGNED
:
562 bld_fetch
= &bld_base
->uint_bld
;
564 case TGSI_TYPE_SIGNED
:
565 bld_fetch
= &bld_base
->int_bld
;
568 case TGSI_TYPE_DOUBLE
:
579 struct lp_build_tgsi_context
* bld_base
,
580 const struct tgsi_full_src_register
* reg
,
581 enum tgsi_opcode_type stype
,
584 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
585 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
586 LLVMBuilderRef builder
= gallivm
->builder
;
587 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
588 LLVMValueRef indirect_index
= NULL
;
589 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
591 /* XXX: Handle fetching xyzw components as a vector */
592 assert(swizzle
!= ~0);
594 if (reg
->Register
.Indirect
) {
595 indirect_index
= get_indirect_index(bld
,
601 if (reg
->Register
.Indirect
) {
602 LLVMValueRef swizzle_vec
=
603 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
604 LLVMValueRef index_vec
; /* index into the const buffer */
606 /* index_vec = indirect_index * 4 + swizzle */
607 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
608 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
610 /* Gather values from the constant buffer */
611 return build_gather(bld_fetch
, bld
->consts_ptr
, index_vec
);
614 LLVMValueRef index
; /* index into the const buffer */
615 LLVMValueRef scalar
, scalar_ptr
;
617 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
*4 + swizzle
);
619 scalar_ptr
= LLVMBuildGEP(builder
, bld
->consts_ptr
,
622 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
623 LLVMTypeRef ivtype
= LLVMPointerType(LLVMInt32TypeInContext(gallivm
->context
), 0);
624 LLVMValueRef temp_ptr
;
625 temp_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, ivtype
, "");
626 scalar
= LLVMBuildLoad(builder
, temp_ptr
, "");
628 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
630 return lp_build_broadcast_scalar(bld_fetch
, scalar
);
635 emit_fetch_immediate(
636 struct lp_build_tgsi_context
* bld_base
,
637 const struct tgsi_full_src_register
* reg
,
638 enum tgsi_opcode_type stype
,
641 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
642 LLVMValueRef res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
645 if (stype
== TGSI_TYPE_UNSIGNED
) {
646 res
= LLVMConstBitCast(res
, bld_base
->uint_bld
.vec_type
);
647 } else if (stype
== TGSI_TYPE_SIGNED
) {
648 res
= LLVMConstBitCast(res
, bld_base
->int_bld
.vec_type
);
655 struct lp_build_tgsi_context
* bld_base
,
656 const struct tgsi_full_src_register
* reg
,
657 enum tgsi_opcode_type stype
,
660 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
661 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
662 LLVMBuilderRef builder
= gallivm
->builder
;
663 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
664 LLVMValueRef indirect_index
= NULL
;
667 if (reg
->Register
.Indirect
) {
668 indirect_index
= get_indirect_index(bld
,
674 if (reg
->Register
.Indirect
) {
675 LLVMValueRef swizzle_vec
=
676 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
677 LLVMValueRef length_vec
=
678 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
679 LLVMValueRef index_vec
; /* index into the const buffer */
680 LLVMValueRef inputs_array
;
681 LLVMTypeRef float4_ptr_type
;
683 /* index_vec = (indirect_index * 4 + swizzle) * length */
684 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
685 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
686 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
688 /* cast inputs_array pointer to float* */
689 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
690 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
,
691 float4_ptr_type
, "");
693 /* Gather values from the temporary register array */
694 res
= build_gather(&bld_base
->base
, inputs_array
, index_vec
);
696 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
697 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
698 reg
->Register
.Index
* 4 + swizzle
);
699 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
700 bld
->inputs_array
, &lindex
, 1, "");
701 res
= LLVMBuildLoad(builder
, input_ptr
, "");
704 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
710 if (stype
== TGSI_TYPE_UNSIGNED
) {
711 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
712 } else if (stype
== TGSI_TYPE_SIGNED
) {
713 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
720 emit_fetch_temporary(
721 struct lp_build_tgsi_context
* bld_base
,
722 const struct tgsi_full_src_register
* reg
,
723 enum tgsi_opcode_type stype
,
726 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
727 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
728 LLVMBuilderRef builder
= gallivm
->builder
;
729 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
730 LLVMValueRef indirect_index
= NULL
;
733 if (reg
->Register
.Indirect
) {
734 indirect_index
= get_indirect_index(bld
,
740 if (reg
->Register
.Indirect
) {
741 LLVMValueRef swizzle_vec
=
742 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
743 LLVMValueRef length_vec
=
744 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
,
745 bld
->bld_base
.base
.type
.length
);
746 LLVMValueRef index_vec
; /* index into the const buffer */
747 LLVMValueRef temps_array
;
748 LLVMTypeRef float4_ptr_type
;
750 /* index_vec = (indirect_index * 4 + swizzle) * length */
751 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
752 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
753 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
755 /* cast temps_array pointer to float* */
756 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(bld
->bld_base
.base
.gallivm
->context
), 0);
757 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
758 float4_ptr_type
, "");
760 /* Gather values from the temporary register array */
761 res
= build_gather(&bld_base
->base
, temps_array
, index_vec
);
764 LLVMValueRef temp_ptr
;
765 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
766 LLVMTypeRef itype
= LLVMPointerType(LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4), 0);
767 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
769 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, itype
, "");
771 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
772 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
774 return bld
->bld_base
.base
.undef
;
781 emit_fetch_system_value(
782 struct lp_build_tgsi_context
* bld_base
,
783 const struct tgsi_full_src_register
* reg
,
784 enum tgsi_opcode_type stype
,
787 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
788 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
789 LLVMBuilderRef builder
= gallivm
->builder
;
790 LLVMValueRef index
; /* index into the system value array */
791 LLVMValueRef scalar
, scalar_ptr
;
793 assert(!reg
->Register
.Indirect
);
795 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
* 4 + swizzle
);
797 scalar_ptr
= LLVMBuildGEP(builder
, bld
->system_values_array
, &index
, 1, "");
798 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
800 return lp_build_broadcast_scalar(&bld
->bld_base
.base
, scalar
);
804 * Register fetch with derivatives.
808 struct lp_build_tgsi_soa_context
*bld
,
817 /* TODO: use interpolation coeffs for inputs */
820 *ddx
= lp_build_ddx(&bld
->bld_base
.base
, src
);
823 *ddy
= lp_build_ddy(&bld
->bld_base
.base
, src
);
831 emit_fetch_predicate(
832 struct lp_build_tgsi_soa_context
*bld
,
833 const struct tgsi_full_instruction
*inst
,
836 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
838 unsigned char swizzles
[4];
839 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
843 if (!inst
->Instruction
.Predicate
) {
844 TGSI_FOR_EACH_CHANNEL( chan
) {
850 swizzles
[0] = inst
->Predicate
.SwizzleX
;
851 swizzles
[1] = inst
->Predicate
.SwizzleY
;
852 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
853 swizzles
[3] = inst
->Predicate
.SwizzleW
;
855 index
= inst
->Predicate
.Index
;
856 assert(index
< LP_MAX_TGSI_PREDS
);
858 TGSI_FOR_EACH_CHANNEL( chan
) {
859 unsigned swizzle
= swizzles
[chan
];
862 * Only fetch the predicate register channels that are actually listed
865 if (!unswizzled
[swizzle
]) {
866 value
= LLVMBuildLoad(builder
,
867 bld
->preds
[index
][swizzle
], "");
870 * Convert the value to an integer mask.
872 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
873 * is needlessly causing two comparisons due to storing the intermediate
874 * result as float vector instead of an integer mask vector.
876 value
= lp_build_compare(bld
->bld_base
.base
.gallivm
,
877 bld
->bld_base
.base
.type
,
880 bld
->bld_base
.base
.zero
);
881 if (inst
->Predicate
.Negate
) {
882 value
= LLVMBuildNot(builder
, value
, "");
885 unswizzled
[swizzle
] = value
;
887 value
= unswizzled
[swizzle
];
899 struct lp_build_tgsi_context
*bld_base
,
900 const struct tgsi_full_instruction
*inst
,
906 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
907 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
908 LLVMBuilderRef builder
= gallivm
->builder
;
909 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
910 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
911 LLVMValueRef indirect_index
= NULL
;
912 struct lp_build_context
*bld_store
;
913 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
917 case TGSI_TYPE_FLOAT
:
918 case TGSI_TYPE_UNTYPED
:
919 bld_store
= &bld_base
->base
;
921 case TGSI_TYPE_UNSIGNED
:
922 bld_store
= &bld_base
->uint_bld
;
924 case TGSI_TYPE_SIGNED
:
925 bld_store
= &bld_base
->int_bld
;
927 case TGSI_TYPE_DOUBLE
:
934 switch( inst
->Instruction
.Saturate
) {
938 case TGSI_SAT_ZERO_ONE
:
939 value
= lp_build_max(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.zero
);
940 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
943 case TGSI_SAT_MINUS_PLUS_ONE
:
944 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));
945 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
952 if (reg
->Register
.Indirect
) {
953 indirect_index
= get_indirect_index(bld
,
958 assert(reg
->Register
.Index
<=
959 bld
->bld_base
.info
->file_max
[reg
->Register
.File
]);
962 switch( reg
->Register
.File
) {
963 case TGSI_FILE_OUTPUT
:
964 if (reg
->Register
.Indirect
) {
965 LLVMValueRef chan_vec
=
966 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
967 LLVMValueRef length_vec
=
968 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
969 LLVMValueRef index_vec
; /* indexes into the temp registers */
970 LLVMValueRef outputs_array
;
971 LLVMValueRef pixel_offsets
;
972 LLVMTypeRef float_ptr_type
;
975 /* build pixel offset vector: {0, 1, 2, 3, ...} */
976 pixel_offsets
= uint_bld
->undef
;
977 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
978 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
979 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
983 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
984 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
985 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
986 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
987 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
990 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
991 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
,
994 /* Scatter store values into temp registers */
995 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
996 &bld
->exec_mask
, pred
);
999 LLVMValueRef out_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
1001 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, out_ptr
);
1005 case TGSI_FILE_TEMPORARY
:
1006 if (reg
->Register
.Indirect
) {
1007 LLVMValueRef chan_vec
=
1008 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
1009 LLVMValueRef length_vec
=
1010 lp_build_const_int_vec(gallivm
, uint_bld
->type
,
1011 bld
->bld_base
.base
.type
.length
);
1012 LLVMValueRef index_vec
; /* indexes into the temp registers */
1013 LLVMValueRef temps_array
;
1014 LLVMValueRef pixel_offsets
;
1015 LLVMTypeRef float_ptr_type
;
1018 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1019 pixel_offsets
= uint_bld
->undef
;
1020 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
1021 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1022 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
1026 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1027 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1028 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1029 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1030 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1033 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1034 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
1035 float_ptr_type
, "");
1037 /* Scatter store values into temp registers */
1038 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
1039 &bld
->exec_mask
, pred
);
1042 LLVMValueRef temp_ptr
;
1045 case TGSI_TYPE_UNSIGNED
:
1046 case TGSI_TYPE_SIGNED
: {
1047 LLVMTypeRef itype
= LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4);
1048 LLVMTypeRef ivtype
= LLVMPointerType(itype
, 0);
1049 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1051 LLVMValueRef temp_value_ptr
;
1053 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, ivtype
, "");
1054 temp_value_ptr
= LLVMBuildBitCast(builder
, value
, itype
, "");
1055 value
= temp_value_ptr
;
1059 case TGSI_TYPE_FLOAT
:
1060 case TGSI_TYPE_UNTYPED
:
1061 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1066 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, temp_ptr
);
1070 case TGSI_FILE_ADDRESS
:
1071 assert(dtype
== TGSI_TYPE_SIGNED
);
1072 assert(LLVMTypeOf(value
) == bld_base
->base
.int_vec_type
);
1073 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1074 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1077 case TGSI_FILE_PREDICATE
:
1078 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1079 bld
->preds
[reg
->Register
.Index
][chan_index
]);
1089 struct lp_build_tgsi_context
* bld_base
,
1090 const struct tgsi_full_instruction
* inst
,
1091 const struct tgsi_opcode_info
* info
,
1092 LLVMValueRef dst
[4])
1095 unsigned chan_index
;
1096 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1099 LLVMValueRef pred
[TGSI_NUM_CHANNELS
];
1101 emit_fetch_predicate( bld
, inst
, pred
);
1103 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1104 emit_store_chan(bld_base
, inst
, 0, chan_index
, pred
[chan_index
], dst
[chan_index
]);
1110 * High-level instruction translators.
1114 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
1115 const struct tgsi_full_instruction
*inst
,
1116 enum lp_build_tex_modifier modifier
,
1117 LLVMValueRef
*texel
)
1119 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1121 LLVMValueRef lod_bias
, explicit_lod
;
1122 LLVMValueRef oow
= NULL
;
1123 LLVMValueRef coords
[3];
1124 LLVMValueRef ddx
[3];
1125 LLVMValueRef ddy
[3];
1126 unsigned num_coords
;
1129 if (!bld
->sampler
) {
1130 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1131 for (i
= 0; i
< 4; i
++) {
1132 texel
[i
] = bld
->bld_base
.base
.undef
;
1137 switch (inst
->Texture
.Texture
) {
1138 case TGSI_TEXTURE_1D
:
1141 case TGSI_TEXTURE_1D_ARRAY
:
1142 case TGSI_TEXTURE_2D
:
1143 case TGSI_TEXTURE_RECT
:
1146 case TGSI_TEXTURE_SHADOW1D
:
1147 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1148 case TGSI_TEXTURE_SHADOW2D
:
1149 case TGSI_TEXTURE_SHADOWRECT
:
1150 case TGSI_TEXTURE_2D_ARRAY
:
1151 case TGSI_TEXTURE_3D
:
1152 case TGSI_TEXTURE_CUBE
:
1155 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1163 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
1164 lod_bias
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1165 explicit_lod
= NULL
;
1167 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
1169 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1173 explicit_lod
= NULL
;
1176 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
1177 oow
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1178 oow
= lp_build_rcp(&bld
->bld_base
.base
, oow
);
1181 for (i
= 0; i
< num_coords
; i
++) {
1182 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1183 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
1184 coords
[i
] = lp_build_mul(&bld
->bld_base
.base
, coords
[i
], oow
);
1186 for (i
= num_coords
; i
< 3; i
++) {
1187 coords
[i
] = bld
->bld_base
.base
.undef
;
1190 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
1191 LLVMValueRef index0
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, 0);
1192 for (i
= 0; i
< num_coords
; i
++) {
1193 LLVMValueRef src1
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 1, i
);
1194 LLVMValueRef src2
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 2, i
);
1195 ddx
[i
] = LLVMBuildExtractElement(builder
, src1
, index0
, "");
1196 ddy
[i
] = LLVMBuildExtractElement(builder
, src2
, index0
, "");
1198 unit
= inst
->Src
[3].Register
.Index
;
1200 for (i
= 0; i
< num_coords
; i
++) {
1201 ddx
[i
] = lp_build_scalar_ddx( &bld
->bld_base
.base
, coords
[i
] );
1202 ddy
[i
] = lp_build_scalar_ddy( &bld
->bld_base
.base
, coords
[i
] );
1204 unit
= inst
->Src
[1].Register
.Index
;
1206 for (i
= num_coords
; i
< 3; i
++) {
1207 ddx
[i
] = LLVMGetUndef(bld
->bld_base
.base
.elem_type
);
1208 ddy
[i
] = LLVMGetUndef(bld
->bld_base
.base
.elem_type
);
1211 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1212 bld
->bld_base
.base
.gallivm
,
1213 bld
->bld_base
.base
.type
,
1214 unit
, num_coords
, coords
,
1216 lod_bias
, explicit_lod
,
1221 emit_txq( struct lp_build_tgsi_soa_context
*bld
,
1222 const struct tgsi_full_instruction
*inst
,
1223 LLVMValueRef
*sizes_out
)
1225 LLVMValueRef explicit_lod
;
1226 unsigned num_coords
, has_lod
;
1229 switch (inst
->Texture
.Texture
) {
1230 case TGSI_TEXTURE_1D
:
1231 case TGSI_TEXTURE_SHADOW1D
:
1232 case TGSI_TEXTURE_SHADOW2D
:
1233 case TGSI_TEXTURE_SHADOWCUBE
:
1237 case TGSI_TEXTURE_2D
:
1238 case TGSI_TEXTURE_CUBE
:
1239 case TGSI_TEXTURE_1D_ARRAY
:
1240 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1244 case TGSI_TEXTURE_3D
:
1245 // case TGSI_TEXTURE_CUBE_ARRAY:
1246 // case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1247 case TGSI_TEXTURE_2D_ARRAY
:
1248 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1253 case TGSI_TEXTURE_BUFFER
:
1258 case TGSI_TEXTURE_RECT
:
1259 case TGSI_TEXTURE_SHADOWRECT
:
1260 // case TGSI_TEXTURE_2D_MS:
1265 // case TGSI_TEXTURE_2D_MS_ARRAY:
1275 if (!bld
->sampler
) {
1276 _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
1277 for (i
= 0; i
< num_coords
; i
++)
1278 sizes_out
[i
] = bld
->bld_base
.base
.undef
;
1283 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 2 );
1285 explicit_lod
= NULL
;
1287 bld
->sampler
->emit_size_query(bld
->sampler
,
1288 bld
->bld_base
.base
.gallivm
,
1289 inst
->Src
[1].Register
.Index
,
1295 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
1300 for (i
= 0; i
< 5; i
++) {
1303 if (pc
+ i
>= bld
->bld_base
.info
->num_instructions
)
1306 opcode
= bld
->bld_base
.instructions
[pc
+ i
].Instruction
.Opcode
;
1308 if (opcode
== TGSI_OPCODE_END
)
1311 if (opcode
== TGSI_OPCODE_TEX
||
1312 opcode
== TGSI_OPCODE_TXP
||
1313 opcode
== TGSI_OPCODE_TXD
||
1314 opcode
== TGSI_OPCODE_TXB
||
1315 opcode
== TGSI_OPCODE_TXL
||
1316 opcode
== TGSI_OPCODE_TXF
||
1317 opcode
== TGSI_OPCODE_TXQ
||
1318 opcode
== TGSI_OPCODE_CAL
||
1319 opcode
== TGSI_OPCODE_CALLNZ
||
1320 opcode
== TGSI_OPCODE_IF
||
1321 opcode
== TGSI_OPCODE_IFC
||
1322 opcode
== TGSI_OPCODE_BGNLOOP
||
1323 opcode
== TGSI_OPCODE_SWITCH
)
1333 * Kill fragment if any of the src register values are negative.
1337 struct lp_build_tgsi_soa_context
*bld
,
1338 const struct tgsi_full_instruction
*inst
,
1341 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1342 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1343 LLVMValueRef terms
[TGSI_NUM_CHANNELS
];
1345 unsigned chan_index
;
1347 memset(&terms
, 0, sizeof terms
);
1349 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1352 /* Unswizzle channel */
1353 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
1355 /* Check if the component has not been already tested. */
1356 assert(swizzle
< TGSI_NUM_CHANNELS
);
1357 if( !terms
[swizzle
] )
1358 /* TODO: change the comparison operator instead of setting the sign */
1359 terms
[swizzle
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, chan_index
);
1363 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1364 if(terms
[chan_index
]) {
1365 LLVMValueRef chan_mask
;
1368 * If term < 0 then mask = 0 else mask = ~0.
1370 chan_mask
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->bld_base
.base
.zero
);
1373 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
1380 lp_build_mask_update(bld
->mask
, mask
);
1382 if (!near_end_of_shader(bld
, pc
))
1383 lp_build_mask_check(bld
->mask
);
1389 * Predicated fragment kill.
1390 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1391 * The only predication is the execution mask which will apply if
1392 * we're inside a loop or conditional.
1395 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
1398 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1401 /* For those channels which are "alive", disable fragment shader
1404 if (bld
->exec_mask
.has_mask
) {
1405 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1408 LLVMValueRef zero
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
1412 lp_build_mask_update(bld
->mask
, mask
);
1414 if (!near_end_of_shader(bld
, pc
))
1415 lp_build_mask_check(bld
->mask
);
1420 * Emit code which will dump the value of all the temporary registers
1424 emit_dump_temps(struct lp_build_tgsi_soa_context
*bld
)
1426 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1427 LLVMBuilderRef builder
= gallivm
->builder
;
1428 LLVMValueRef temp_ptr
;
1429 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
1430 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
1431 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
1432 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
1434 int n
= bld
->bld_base
.info
->file_max
[TGSI_FILE_TEMPORARY
];
1436 for (index
= 0; index
< n
; index
++) {
1437 LLVMValueRef idx
= lp_build_const_int32(gallivm
, index
);
1438 LLVMValueRef v
[4][4], res
;
1441 lp_build_printf(gallivm
, "TEMP[%d]:\n", idx
);
1443 for (chan
= 0; chan
< 4; chan
++) {
1444 temp_ptr
= lp_get_temp_ptr_soa(bld
, index
, chan
);
1445 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1446 v
[chan
][0] = LLVMBuildExtractElement(builder
, res
, i0
, "");
1447 v
[chan
][1] = LLVMBuildExtractElement(builder
, res
, i1
, "");
1448 v
[chan
][2] = LLVMBuildExtractElement(builder
, res
, i2
, "");
1449 v
[chan
][3] = LLVMBuildExtractElement(builder
, res
, i3
, "");
1452 lp_build_printf(gallivm
, " X: %f %f %f %f\n",
1453 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
1454 lp_build_printf(gallivm
, " Y: %f %f %f %f\n",
1455 v
[1][0], v
[1][1], v
[1][2], v
[1][3]);
1456 lp_build_printf(gallivm
, " Z: %f %f %f %f\n",
1457 v
[2][0], v
[2][1], v
[2][2], v
[2][3]);
1458 lp_build_printf(gallivm
, " W: %f %f %f %f\n",
1459 v
[3][0], v
[3][1], v
[3][2], v
[3][3]);
1466 lp_emit_declaration_soa(
1467 struct lp_build_tgsi_context
*bld_base
,
1468 const struct tgsi_full_declaration
*decl
)
1470 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1471 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1472 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
1473 const unsigned first
= decl
->Range
.First
;
1474 const unsigned last
= decl
->Range
.Last
;
1477 for (idx
= first
; idx
<= last
; ++idx
) {
1478 assert(last
<= bld
->bld_base
.info
->file_max
[decl
->Declaration
.File
]);
1479 switch (decl
->Declaration
.File
) {
1480 case TGSI_FILE_TEMPORARY
:
1481 assert(idx
< LP_MAX_TGSI_TEMPS
);
1482 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
1483 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1484 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
1488 case TGSI_FILE_OUTPUT
:
1489 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
1490 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1491 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
1492 vec_type
, "output");
1496 case TGSI_FILE_ADDRESS
:
1497 /* ADDR registers are the only allocated with an integer LLVM IR type,
1498 * as they are guaranteed to always have integers.
1499 * XXX: Not sure if this exception is worthwhile (or the whole idea of
1500 * an ADDR register for that matter).
1502 assert(idx
< LP_MAX_TGSI_ADDRS
);
1503 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1504 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, bld_base
->base
.int_vec_type
, "addr");
1507 case TGSI_FILE_PREDICATE
:
1508 assert(idx
< LP_MAX_TGSI_PREDS
);
1509 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1510 bld
->preds
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
,
1515 /* don't need to declare other vars */
1522 void lp_emit_immediate_soa(
1523 struct lp_build_tgsi_context
*bld_base
,
1524 const struct tgsi_full_immediate
*imm
)
1526 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1527 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1529 /* simply copy the immediate values into the next immediates[] slot */
1531 const uint size
= imm
->Immediate
.NrTokens
- 1;
1533 assert(bld
->num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1534 switch (imm
->Immediate
.DataType
) {
1535 case TGSI_IMM_FLOAT32
:
1536 for( i
= 0; i
< size
; ++i
)
1537 bld
->immediates
[bld
->num_immediates
][i
] =
1538 lp_build_const_vec(gallivm
, bld_base
->base
.type
, imm
->u
[i
].Float
);
1541 case TGSI_IMM_UINT32
:
1542 for( i
= 0; i
< size
; ++i
) {
1543 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->uint_bld
.type
, imm
->u
[i
].Uint
);
1544 bld
->immediates
[bld
->num_immediates
][i
] =
1545 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1549 case TGSI_IMM_INT32
:
1550 for( i
= 0; i
< size
; ++i
) {
1551 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->int_bld
.type
, imm
->u
[i
].Int
);
1552 bld
->immediates
[bld
->num_immediates
][i
] =
1553 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1558 for( i
= size
; i
< 4; ++i
)
1559 bld
->immediates
[bld
->num_immediates
][i
] = bld_base
->base
.undef
;
1561 bld
->num_immediates
++;
1566 const struct lp_build_tgsi_action
* action
,
1567 struct lp_build_tgsi_context
* bld_base
,
1568 struct lp_build_emit_data
* emit_data
)
1570 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1572 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
,
1573 &emit_data
->output
[emit_data
->chan
], NULL
);
1578 const struct lp_build_tgsi_action
* action
,
1579 struct lp_build_tgsi_context
* bld_base
,
1580 struct lp_build_emit_data
* emit_data
)
1582 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1584 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
, NULL
,
1585 &emit_data
->output
[emit_data
->chan
]);
1590 const struct lp_build_tgsi_action
* action
,
1591 struct lp_build_tgsi_context
* bld_base
,
1592 struct lp_build_emit_data
* emit_data
)
1594 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1596 emit_kilp(bld
, bld_base
->pc
- 1);
1601 const struct lp_build_tgsi_action
* action
,
1602 struct lp_build_tgsi_context
* bld_base
,
1603 struct lp_build_emit_data
* emit_data
)
1605 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1607 emit_kil(bld
, emit_data
->inst
, bld_base
->pc
- 1);
1612 const struct lp_build_tgsi_action
* action
,
1613 struct lp_build_tgsi_context
* bld_base
,
1614 struct lp_build_emit_data
* emit_data
)
1616 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1618 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
, emit_data
->output
);
1623 const struct lp_build_tgsi_action
* action
,
1624 struct lp_build_tgsi_context
* bld_base
,
1625 struct lp_build_emit_data
* emit_data
)
1627 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1629 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
1635 const struct lp_build_tgsi_action
* action
,
1636 struct lp_build_tgsi_context
* bld_base
,
1637 struct lp_build_emit_data
* emit_data
)
1639 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1641 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
1647 const struct lp_build_tgsi_action
* action
,
1648 struct lp_build_tgsi_context
* bld_base
,
1649 struct lp_build_emit_data
* emit_data
)
1651 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1653 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
1659 const struct lp_build_tgsi_action
* action
,
1660 struct lp_build_tgsi_context
* bld_base
,
1661 struct lp_build_emit_data
* emit_data
)
1663 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1665 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_PROJECTED
,
1671 const struct lp_build_tgsi_action
* action
,
1672 struct lp_build_tgsi_context
* bld_base
,
1673 struct lp_build_emit_data
* emit_data
)
1675 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1677 emit_txq(bld
, emit_data
->inst
, emit_data
->output
);
1682 const struct lp_build_tgsi_action
* action
,
1683 struct lp_build_tgsi_context
* bld_base
,
1684 struct lp_build_emit_data
* emit_data
)
1686 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1688 lp_exec_mask_call(&bld
->exec_mask
, emit_data
->inst
->Label
.Label
,
1694 const struct lp_build_tgsi_action
* action
,
1695 struct lp_build_tgsi_context
* bld_base
,
1696 struct lp_build_emit_data
* emit_data
)
1698 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1700 lp_exec_mask_ret(&bld
->exec_mask
, &bld_base
->pc
);
1705 const struct lp_build_tgsi_action
* action
,
1706 struct lp_build_tgsi_context
* bld_base
,
1707 struct lp_build_emit_data
* emit_data
)
1709 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1711 lp_exec_break(&bld
->exec_mask
);
1716 const struct lp_build_tgsi_action
* action
,
1717 struct lp_build_tgsi_context
* bld_base
,
1718 struct lp_build_emit_data
* emit_data
)
1721 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1723 tmp
= lp_build_cmp(&bld_base
->base
, PIPE_FUNC_NOTEQUAL
,
1724 emit_data
->args
[0], bld
->bld_base
.base
.zero
);
1725 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
1730 const struct lp_build_tgsi_action
* action
,
1731 struct lp_build_tgsi_context
* bld_base
,
1732 struct lp_build_emit_data
* emit_data
)
1734 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1736 lp_exec_bgnloop(&bld
->exec_mask
);
1741 const struct lp_build_tgsi_action
* action
,
1742 struct lp_build_tgsi_context
* bld_base
,
1743 struct lp_build_emit_data
* emit_data
)
1745 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1747 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1752 const struct lp_build_tgsi_action
* action
,
1753 struct lp_build_tgsi_context
* bld_base
,
1754 struct lp_build_emit_data
* emit_data
)
1756 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1758 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1763 const struct lp_build_tgsi_action
* action
,
1764 struct lp_build_tgsi_context
* bld_base
,
1765 struct lp_build_emit_data
* emit_data
)
1767 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1769 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1774 const struct lp_build_tgsi_action
* action
,
1775 struct lp_build_tgsi_context
* bld_base
,
1776 struct lp_build_emit_data
* emit_data
)
1778 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1780 lp_exec_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
1785 const struct lp_build_tgsi_action
* action
,
1786 struct lp_build_tgsi_context
* bld_base
,
1787 struct lp_build_emit_data
* emit_data
)
1789 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1791 lp_exec_mask_endsub(&bld
->exec_mask
, &bld_base
->pc
);
1796 const struct lp_build_tgsi_action
* action
,
1797 struct lp_build_tgsi_context
* bld_base
,
1798 struct lp_build_emit_data
* emit_data
)
1800 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1802 lp_exec_continue(&bld
->exec_mask
);
1805 /* XXX: Refactor and move it to lp_bld_tgsi_action.c
1807 * XXX: What do the comments about xmm registers mean? Maybe they are left over
1808 * from old code, but there is no garauntee that LLVM will use those registers
1811 * XXX: There should be no calls to lp_build_emit_fetch in this function. This
1812 * should be handled by the emit_data->fetch_args function. */
1815 const struct lp_build_tgsi_action
* action
,
1816 struct lp_build_tgsi_context
* bld_base
,
1817 struct lp_build_emit_data
* emit_data
)
1819 LLVMValueRef tmp0
, tmp1
;
1820 LLVMValueRef tmp4
= NULL
;
1821 LLVMValueRef tmp5
= NULL
;
1822 LLVMValueRef tmp6
= NULL
;
1823 LLVMValueRef tmp7
= NULL
;
1824 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1826 uint dims
= (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1828 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) ||
1829 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
) ||
1830 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
) ||
1831 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 4)) {
1833 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1836 /* xmm0 = src.x * src.x */
1837 tmp0
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
1838 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
1841 tmp0
= lp_build_mul( &bld
->bld_base
.base
, tmp0
, tmp0
);
1844 /* xmm0 = xmm0 + src.y * src.y */
1845 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Y
);
1846 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
1849 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1850 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1853 /* xmm0 = xmm0 + src.z * src.z */
1854 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Z
);
1855 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
1858 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1859 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1863 /* xmm0 = xmm0 + src.w * src.w */
1864 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1865 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
)) {
1868 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1869 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1871 /* xmm1 = 1 / sqrt(xmm0) */
1872 tmp1
= lp_build_rsqrt( &bld
->bld_base
.base
, tmp0
);
1873 /* dst.x = xmm1 * src.x */
1874 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
1875 emit_data
->output
[TGSI_CHAN_X
] = lp_build_mul( &bld
->bld_base
.base
, tmp4
, tmp1
);
1877 /* dst.y = xmm1 * src.y */
1878 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
1879 emit_data
->output
[TGSI_CHAN_Y
] = lp_build_mul( &bld
->bld_base
.base
, tmp5
, tmp1
);
1882 /* dst.z = xmm1 * src.z */
1883 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
1884 emit_data
->output
[TGSI_CHAN_Z
] = lp_build_mul( &bld
->bld_base
.base
, tmp6
, tmp1
);
1886 /* dst.w = xmm1 * src.w */
1887 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) && dims
== 4) {
1888 emit_data
->output
[TGSI_CHAN_W
] = lp_build_mul( &bld
->bld_base
.base
, tmp7
, tmp1
);
1893 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 3) {
1894 emit_data
->output
[TGSI_CHAN_W
] = bld
->bld_base
.base
.one
;
1898 static void emit_prologue(struct lp_build_tgsi_context
* bld_base
)
1900 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1901 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1903 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
1904 LLVMValueRef array_size
=
1905 lp_build_const_int32(gallivm
,
1906 bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
1907 bld
->temps_array
= lp_build_array_alloca(gallivm
,
1908 bld_base
->base
.vec_type
, array_size
,
1912 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
1913 LLVMValueRef array_size
=
1914 lp_build_const_int32(gallivm
,
1915 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
1916 bld
->outputs_array
= lp_build_array_alloca(gallivm
,
1917 bld_base
->base
.vec_type
, array_size
,
1921 /* If we have indirect addressing in inputs we need to copy them into
1922 * our alloca array to be able to iterate over them */
1923 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
1924 unsigned index
, chan
;
1925 LLVMTypeRef vec_type
= bld_base
->base
.vec_type
;
1926 LLVMValueRef array_size
= lp_build_const_int32(gallivm
,
1927 bld_base
->info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
1928 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
1929 vec_type
, array_size
,
1932 assert(bld_base
->info
->num_inputs
1933 <= bld_base
->info
->file_max
[TGSI_FILE_INPUT
] + 1);
1935 for (index
= 0; index
< bld_base
->info
->num_inputs
; ++index
) {
1936 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1937 LLVMValueRef lindex
=
1938 lp_build_const_int32(gallivm
, index
* 4 + chan
);
1939 LLVMValueRef input_ptr
=
1940 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
1942 LLVMValueRef value
= bld
->inputs
[index
][chan
];
1944 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
1950 static void emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
1952 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1956 emit_dump_temps(bld
);
1959 /* If we have indirect addressing in outputs we need to copy our alloca array
1960 * to the outputs slots specified by the called */
1961 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
1962 unsigned index
, chan
;
1963 assert(bld_base
->info
->num_outputs
<=
1964 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
1965 for (index
= 0; index
< bld_base
->info
->num_outputs
; ++index
) {
1966 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1967 bld
->outputs
[index
][chan
] = lp_get_output_ptr(bld
, index
, chan
);
1974 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
1975 const struct tgsi_token
*tokens
,
1976 struct lp_type type
,
1977 struct lp_build_mask_context
*mask
,
1978 LLVMValueRef consts_ptr
,
1979 LLVMValueRef system_values_array
,
1980 const LLVMValueRef
*pos
,
1981 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
1982 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1983 struct lp_build_sampler_soa
*sampler
,
1984 const struct tgsi_shader_info
*info
)
1986 struct lp_build_tgsi_soa_context bld
;
1988 struct lp_type res_type
;
1990 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
1991 memset(&res_type
, 0, sizeof res_type
);
1992 res_type
.width
= type
.width
;
1993 res_type
.length
= type
.length
;
1996 /* Setup build context */
1997 memset(&bld
, 0, sizeof bld
);
1998 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
1999 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
2000 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
2001 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
2004 bld
.inputs
= inputs
;
2005 bld
.outputs
= outputs
;
2006 bld
.consts_ptr
= consts_ptr
;
2007 bld
.sampler
= sampler
;
2008 bld
.bld_base
.info
= info
;
2009 bld
.indirect_files
= info
->indirect_files
;
2011 bld
.bld_base
.soa
= TRUE
;
2012 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
2013 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
2014 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
2015 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
2016 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = emit_fetch_system_value
;
2017 bld
.bld_base
.emit_store
= emit_store
;
2019 bld
.bld_base
.emit_declaration
= lp_emit_declaration_soa
;
2020 bld
.bld_base
.emit_immediate
= lp_emit_immediate_soa
;
2022 bld
.bld_base
.emit_prologue
= emit_prologue
;
2023 bld
.bld_base
.emit_epilogue
= emit_epilogue
;
2025 /* Set opcode actions */
2026 lp_set_default_actions_cpu(&bld
.bld_base
);
2028 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
2029 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNSUB
].emit
= bgnsub_emit
;
2030 bld
.bld_base
.op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
2031 bld
.bld_base
.op_actions
[TGSI_OPCODE_CAL
].emit
= cal_emit
;
2032 bld
.bld_base
.op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
2033 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDX
].emit
= ddx_emit
;
2034 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDY
].emit
= ddy_emit
;
2035 bld
.bld_base
.op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
2036 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
2037 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
2038 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSUB
].emit
= endsub_emit
;
2039 bld
.bld_base
.op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
2040 bld
.bld_base
.op_actions
[TGSI_OPCODE_KIL
].emit
= kil_emit
;
2041 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILP
].emit
= kilp_emit
;
2042 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM
].emit
= nrm_emit
;
2043 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM4
].emit
= nrm_emit
;
2044 bld
.bld_base
.op_actions
[TGSI_OPCODE_RET
].emit
= ret_emit
;
2045 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX
].emit
= tex_emit
;
2046 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB
].emit
= txb_emit
;
2047 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXD
].emit
= txd_emit
;
2048 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL
].emit
= txl_emit
;
2049 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXP
].emit
= txp_emit
;
2050 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
2052 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.base
);
2055 bld
.system_values_array
= system_values_array
;
2057 lp_build_tgsi_llvm(&bld
.bld_base
, tokens
);
2060 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
2061 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2062 debug_printf("11111111111111111111111111111 \n");
2063 tgsi_dump(tokens
, 0);
2064 lp_debug_dump_value(function
);
2065 debug_printf("2222222222222222222222222222 \n");
2069 LLVMModuleRef module
= LLVMGetGlobalParent(
2070 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
2071 LLVMDumpModule(module
);
2078 * Build up the system values array out of individual values such as
2079 * the instance ID, front-face, primitive ID, etc. The shader info is
2080 * used to determine which system values are needed and where to put
2081 * them in the system values array.
2083 * XXX only instance ID is implemented at this time.
2085 * The system values register file is similar to the constants buffer.
2086 * Example declaration:
2087 * DCL SV[0], INSTANCEID
2088 * Example instruction:
2089 * MOVE foo, SV[0].xxxx;
2091 * \return LLVM float array (interpreted as float [][4])
2094 lp_build_system_values_array(struct gallivm_state
*gallivm
,
2095 const struct tgsi_shader_info
*info
,
2096 LLVMValueRef instance_id
,
2097 LLVMValueRef facing
)
2099 LLVMValueRef size
= lp_build_const_int32(gallivm
, 4 * info
->num_system_values
);
2100 LLVMTypeRef float_t
= LLVMFloatTypeInContext(gallivm
->context
);
2101 LLVMValueRef array
= lp_build_array_alloca(gallivm
, float_t
,
2102 size
, "sysvals_array");
2105 for (i
= 0; i
< info
->num_system_values
; i
++) {
2106 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
* 4);
2107 LLVMValueRef ptr
, value
= 0;
2109 switch (info
->system_value_semantic_name
[i
]) {
2110 case TGSI_SEMANTIC_INSTANCEID
:
2111 /* convert instance ID from int to float */
2112 value
= LLVMBuildSIToFP(gallivm
->builder
, instance_id
, float_t
,
2113 "sysval_instanceid");
2115 case TGSI_SEMANTIC_FACE
:
2118 assert(0 && "unexpected semantic in build_system_values_array()");
2121 ptr
= LLVMBuildGEP(gallivm
->builder
, array
, &index
, 1, "");
2122 LLVMBuildStore(gallivm
->builder
, value
, ptr
);