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
= LLVMBuildAlloca(
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
) {
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);
124 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
127 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
129 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
130 if (mask
->cond_stack_size
== 0) {
131 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
133 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
134 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
135 mask
->cond_mask
= LLVMBuildAnd(builder
,
139 lp_exec_mask_update(mask
);
142 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
144 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
145 LLVMValueRef prev_mask
;
146 LLVMValueRef inv_mask
;
148 assert(mask
->cond_stack_size
);
149 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
150 if (mask
->cond_stack_size
== 1) {
151 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
154 inv_mask
= LLVMBuildNot(builder
, mask
->cond_mask
, "");
156 mask
->cond_mask
= LLVMBuildAnd(builder
,
159 lp_exec_mask_update(mask
);
162 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
164 assert(mask
->cond_stack_size
);
165 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
166 lp_exec_mask_update(mask
);
169 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
171 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
173 if (mask
->loop_stack_size
== 0) {
174 assert(mask
->loop_block
== NULL
);
175 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
176 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
177 assert(mask
->break_var
== NULL
);
180 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
182 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
183 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
184 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
185 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
186 ++mask
->loop_stack_size
;
188 mask
->break_var
= lp_build_alloca(mask
->bld
->gallivm
, mask
->int_vec_type
, "");
189 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
191 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->gallivm
, "bgnloop");
193 LLVMBuildBr(builder
, mask
->loop_block
);
194 LLVMPositionBuilderAtEnd(builder
, mask
->loop_block
);
196 mask
->break_mask
= LLVMBuildLoad(builder
, mask
->break_var
, "");
198 lp_exec_mask_update(mask
);
201 static void lp_exec_break(struct lp_exec_mask
*mask
)
203 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
204 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
208 mask
->break_mask
= LLVMBuildAnd(builder
,
210 exec_mask
, "break_full");
212 lp_exec_mask_update(mask
);
215 static void lp_exec_continue(struct lp_exec_mask
*mask
)
217 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
218 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
222 mask
->cont_mask
= LLVMBuildAnd(builder
,
226 lp_exec_mask_update(mask
);
230 static void lp_exec_endloop(struct gallivm_state
*gallivm
,
231 struct lp_exec_mask
*mask
)
233 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
234 LLVMBasicBlockRef endloop
;
235 LLVMTypeRef int_type
= LLVMInt32TypeInContext(mask
->bld
->gallivm
->context
);
236 LLVMTypeRef reg_type
= LLVMIntTypeInContext(gallivm
->context
,
237 mask
->bld
->type
.width
*
238 mask
->bld
->type
.length
);
239 LLVMValueRef i1cond
, i2cond
, icond
, limiter
;
241 assert(mask
->break_mask
);
244 * Restore the cont_mask, but don't pop
246 assert(mask
->loop_stack_size
);
247 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
248 lp_exec_mask_update(mask
);
251 * Unlike the continue mask, the break_mask must be preserved across loop
254 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
256 /* Decrement the loop limiter */
257 limiter
= LLVMBuildLoad(builder
, mask
->loop_limiter
, "");
259 limiter
= LLVMBuildSub(
262 LLVMConstInt(int_type
, 1, false),
265 LLVMBuildStore(builder
, limiter
, mask
->loop_limiter
);
267 /* i1cond = (mask != 0) */
268 i1cond
= LLVMBuildICmp(
271 LLVMBuildBitCast(builder
, mask
->exec_mask
, reg_type
, ""),
272 LLVMConstNull(reg_type
), "");
274 /* i2cond = (looplimiter > 0) */
275 i2cond
= LLVMBuildICmp(
279 LLVMConstNull(int_type
), "");
281 /* if( i1cond && i2cond ) */
282 icond
= LLVMBuildAnd(builder
, i1cond
, i2cond
, "");
284 endloop
= lp_build_insert_new_block(mask
->bld
->gallivm
, "endloop");
286 LLVMBuildCondBr(builder
,
287 icond
, mask
->loop_block
, endloop
);
289 LLVMPositionBuilderAtEnd(builder
, endloop
);
291 assert(mask
->loop_stack_size
);
292 --mask
->loop_stack_size
;
293 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
294 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
295 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
296 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
298 lp_exec_mask_update(mask
);
301 /* stores val into an address pointed to by dst.
302 * mask->exec_mask is used to figure out which bits of val
303 * should be stored into the address
304 * (0 means don't store this bit, 1 means do store).
306 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
307 struct lp_build_context
*bld_store
,
312 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
314 /* Mix the predicate and execution mask */
315 if (mask
->has_mask
) {
317 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
319 pred
= mask
->exec_mask
;
324 LLVMValueRef real_val
, dst_val
;
326 dst_val
= LLVMBuildLoad(builder
, dst
, "");
327 real_val
= lp_build_select(bld_store
,
331 LLVMBuildStore(builder
, real_val
, dst
);
333 LLVMBuildStore(builder
, val
, dst
);
336 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
340 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
341 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
342 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
343 mask
->call_stack_size
++;
347 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
349 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
350 LLVMValueRef exec_mask
;
352 if (mask
->call_stack_size
== 0) {
353 /* returning from main() */
357 exec_mask
= LLVMBuildNot(builder
,
361 mask
->ret_mask
= LLVMBuildAnd(builder
,
363 exec_mask
, "ret_full");
365 lp_exec_mask_update(mask
);
368 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
372 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
374 assert(mask
->call_stack_size
);
375 mask
->call_stack_size
--;
376 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
377 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
378 lp_exec_mask_update(mask
);
383 * Return pointer to a temporary register channel (src or dest).
384 * Note that indirect addressing cannot be handled here.
385 * \param index which temporary register
386 * \param chan which channel of the temp register.
389 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context
*bld
,
393 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
395 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
396 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, index
* 4 + chan
);
397 return LLVMBuildGEP(builder
, bld
->temps_array
, &lindex
, 1, "");
400 return bld
->temps
[index
][chan
];
405 * Return pointer to a output register channel (src or dest).
406 * Note that indirect addressing cannot be handled here.
407 * \param index which output register
408 * \param chan which channel of the output register.
411 lp_get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
415 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
417 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
418 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
,
420 return LLVMBuildGEP(builder
, bld
->outputs_array
, &lindex
, 1, "");
423 return bld
->outputs
[index
][chan
];
429 * XXX the lp_build_gather() function should be capable of doing this
430 * with a little work.
433 build_gather(struct lp_build_context
*bld
,
434 LLVMValueRef base_ptr
,
435 LLVMValueRef indexes
)
437 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
438 LLVMValueRef res
= bld
->undef
;
442 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
444 for (i
= 0; i
< bld
->type
.length
; i
++) {
445 LLVMValueRef ii
= lp_build_const_int32(bld
->gallivm
, i
);
446 LLVMValueRef index
= LLVMBuildExtractElement(builder
,
448 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
449 &index
, 1, "gather_ptr");
450 LLVMValueRef scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
452 res
= LLVMBuildInsertElement(builder
, res
, scalar
, ii
, "");
460 * Scatter/store vector.
463 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
464 LLVMValueRef base_ptr
,
465 LLVMValueRef indexes
,
467 struct lp_exec_mask
*mask
,
470 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
471 LLVMBuilderRef builder
= gallivm
->builder
;
474 /* Mix the predicate and execution mask */
475 if (mask
->has_mask
) {
477 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
480 pred
= mask
->exec_mask
;
485 * Loop over elements of index_vec, store scalar value.
487 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
488 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
489 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
490 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
491 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
492 LLVMValueRef scalar_pred
= pred
?
493 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
496 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
497 ii
, val
, index
, scalar_ptr
);
500 LLVMValueRef real_val
, dst_val
;
501 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
502 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
503 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
506 LLVMBuildStore(builder
, val
, scalar_ptr
);
513 * Read the current value of the ADDR register, convert the floats to
514 * ints, add the base index and return the vector of offsets.
515 * The offsets will be used to index into the constant buffer or
516 * temporary register file.
519 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
520 unsigned reg_file
, unsigned reg_index
,
521 const struct tgsi_src_register
*indirect_reg
)
523 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
524 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
525 /* always use X component of address register */
526 unsigned swizzle
= indirect_reg
->SwizzleX
;
529 LLVMValueRef max_index
;
532 assert(bld
->indirect_files
& (1 << reg_file
));
534 base
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, reg_index
);
537 rel
= LLVMBuildLoad(builder
,
538 bld
->addr
[indirect_reg
->Index
][swizzle
],
541 index
= lp_build_add(uint_bld
, base
, rel
);
543 max_index
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
,
545 bld
->bld_base
.info
->file_max
[reg_file
]);
547 assert(!uint_bld
->type
.sign
);
548 index
= lp_build_min(uint_bld
, index
, max_index
);
553 static struct lp_build_context
*
554 stype_to_fetch(struct lp_build_tgsi_context
* bld_base
,
555 enum tgsi_opcode_type stype
)
557 struct lp_build_context
*bld_fetch
;
560 case TGSI_TYPE_FLOAT
:
561 case TGSI_TYPE_UNTYPED
:
562 bld_fetch
= &bld_base
->base
;
564 case TGSI_TYPE_UNSIGNED
:
565 bld_fetch
= &bld_base
->uint_bld
;
567 case TGSI_TYPE_SIGNED
:
568 bld_fetch
= &bld_base
->int_bld
;
571 case TGSI_TYPE_DOUBLE
:
582 struct lp_build_tgsi_context
* bld_base
,
583 const struct tgsi_full_src_register
* reg
,
584 enum tgsi_opcode_type stype
,
587 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
588 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
589 LLVMBuilderRef builder
= gallivm
->builder
;
590 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
591 LLVMValueRef indirect_index
= NULL
;
592 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
594 /* XXX: Handle fetching xyzw components as a vector */
595 assert(swizzle
!= ~0);
597 if (reg
->Register
.Indirect
) {
598 indirect_index
= get_indirect_index(bld
,
604 if (reg
->Register
.Indirect
) {
605 LLVMValueRef swizzle_vec
=
606 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
607 LLVMValueRef index_vec
; /* index into the const buffer */
609 /* index_vec = indirect_index * 4 + swizzle */
610 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
611 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
613 /* Gather values from the constant buffer */
614 return build_gather(bld_fetch
, bld
->consts_ptr
, index_vec
);
617 LLVMValueRef index
; /* index into the const buffer */
618 LLVMValueRef scalar
, scalar_ptr
;
620 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
*4 + swizzle
);
622 scalar_ptr
= LLVMBuildGEP(builder
, bld
->consts_ptr
,
625 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
626 LLVMTypeRef ivtype
= LLVMPointerType(LLVMInt32TypeInContext(gallivm
->context
), 0);
627 LLVMValueRef temp_ptr
;
628 temp_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, ivtype
, "");
629 scalar
= LLVMBuildLoad(builder
, temp_ptr
, "");
631 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
633 return lp_build_broadcast_scalar(bld_fetch
, scalar
);
638 emit_fetch_immediate(
639 struct lp_build_tgsi_context
* bld_base
,
640 const struct tgsi_full_src_register
* reg
,
641 enum tgsi_opcode_type stype
,
644 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
645 LLVMValueRef res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
648 if (stype
== TGSI_TYPE_UNSIGNED
) {
649 res
= LLVMConstBitCast(res
, bld_base
->uint_bld
.vec_type
);
650 } else if (stype
== TGSI_TYPE_SIGNED
) {
651 res
= LLVMConstBitCast(res
, bld_base
->int_bld
.vec_type
);
658 struct lp_build_tgsi_context
* bld_base
,
659 const struct tgsi_full_src_register
* reg
,
660 enum tgsi_opcode_type stype
,
663 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
664 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
665 LLVMBuilderRef builder
= gallivm
->builder
;
666 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
667 LLVMValueRef indirect_index
= NULL
;
670 if (reg
->Register
.Indirect
) {
671 indirect_index
= get_indirect_index(bld
,
677 if (reg
->Register
.Indirect
) {
678 LLVMValueRef swizzle_vec
=
679 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
680 LLVMValueRef length_vec
=
681 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
682 LLVMValueRef index_vec
; /* index into the const buffer */
683 LLVMValueRef inputs_array
;
684 LLVMTypeRef float4_ptr_type
;
686 /* index_vec = (indirect_index * 4 + swizzle) * length */
687 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
688 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
689 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
691 /* cast inputs_array pointer to float* */
692 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
693 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
,
694 float4_ptr_type
, "");
696 /* Gather values from the temporary register array */
697 res
= build_gather(&bld_base
->base
, inputs_array
, index_vec
);
699 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
700 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
701 reg
->Register
.Index
* 4 + swizzle
);
702 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
703 bld
->inputs_array
, &lindex
, 1, "");
704 res
= LLVMBuildLoad(builder
, input_ptr
, "");
707 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
715 emit_fetch_temporary(
716 struct lp_build_tgsi_context
* bld_base
,
717 const struct tgsi_full_src_register
* reg
,
718 enum tgsi_opcode_type stype
,
721 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
722 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
723 LLVMBuilderRef builder
= gallivm
->builder
;
724 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
725 LLVMValueRef indirect_index
= NULL
;
728 if (reg
->Register
.Indirect
) {
729 indirect_index
= get_indirect_index(bld
,
735 if (reg
->Register
.Indirect
) {
736 LLVMValueRef swizzle_vec
=
737 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
738 LLVMValueRef length_vec
=
739 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
,
740 bld
->bld_base
.base
.type
.length
);
741 LLVMValueRef index_vec
; /* index into the const buffer */
742 LLVMValueRef temps_array
;
743 LLVMTypeRef float4_ptr_type
;
745 /* index_vec = (indirect_index * 4 + swizzle) * length */
746 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
747 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
748 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
750 /* cast temps_array pointer to float* */
751 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(bld
->bld_base
.base
.gallivm
->context
), 0);
752 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
753 float4_ptr_type
, "");
755 /* Gather values from the temporary register array */
756 res
= build_gather(&bld_base
->base
, temps_array
, index_vec
);
759 LLVMValueRef temp_ptr
;
760 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
761 LLVMTypeRef itype
= LLVMPointerType(LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4), 0);
762 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
764 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, itype
, "");
766 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
767 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
769 return bld
->bld_base
.base
.undef
;
776 emit_fetch_system_value(
777 struct lp_build_tgsi_context
* bld_base
,
778 const struct tgsi_full_src_register
* reg
,
779 enum tgsi_opcode_type stype
,
782 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
783 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
784 LLVMBuilderRef builder
= gallivm
->builder
;
785 LLVMValueRef index
; /* index into the system value array */
786 LLVMValueRef scalar
, scalar_ptr
;
788 assert(!reg
->Register
.Indirect
);
790 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
* 4 + swizzle
);
792 scalar_ptr
= LLVMBuildGEP(builder
, bld
->system_values_array
, &index
, 1, "");
793 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
795 return lp_build_broadcast_scalar(&bld
->bld_base
.base
, scalar
);
799 * Register fetch with derivatives.
803 struct lp_build_tgsi_soa_context
*bld
,
812 /* TODO: use interpolation coeffs for inputs */
815 *ddx
= lp_build_ddx(&bld
->bld_base
.base
, src
);
818 *ddy
= lp_build_ddy(&bld
->bld_base
.base
, src
);
826 emit_fetch_predicate(
827 struct lp_build_tgsi_soa_context
*bld
,
828 const struct tgsi_full_instruction
*inst
,
831 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
833 unsigned char swizzles
[4];
834 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
838 if (!inst
->Instruction
.Predicate
) {
839 TGSI_FOR_EACH_CHANNEL( chan
) {
845 swizzles
[0] = inst
->Predicate
.SwizzleX
;
846 swizzles
[1] = inst
->Predicate
.SwizzleY
;
847 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
848 swizzles
[3] = inst
->Predicate
.SwizzleW
;
850 index
= inst
->Predicate
.Index
;
851 assert(index
< LP_MAX_TGSI_PREDS
);
853 TGSI_FOR_EACH_CHANNEL( chan
) {
854 unsigned swizzle
= swizzles
[chan
];
857 * Only fetch the predicate register channels that are actually listed
860 if (!unswizzled
[swizzle
]) {
861 value
= LLVMBuildLoad(builder
,
862 bld
->preds
[index
][swizzle
], "");
865 * Convert the value to an integer mask.
867 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
868 * is needlessly causing two comparisons due to storing the intermediate
869 * result as float vector instead of an integer mask vector.
871 value
= lp_build_compare(bld
->bld_base
.base
.gallivm
,
872 bld
->bld_base
.base
.type
,
875 bld
->bld_base
.base
.zero
);
876 if (inst
->Predicate
.Negate
) {
877 value
= LLVMBuildNot(builder
, value
, "");
880 unswizzled
[swizzle
] = value
;
882 value
= unswizzled
[swizzle
];
894 struct lp_build_tgsi_context
*bld_base
,
895 const struct tgsi_full_instruction
*inst
,
901 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
902 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
903 LLVMBuilderRef builder
= gallivm
->builder
;
904 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
905 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
906 LLVMValueRef indirect_index
= NULL
;
907 struct lp_build_context
*bld_store
;
908 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
912 case TGSI_TYPE_FLOAT
:
913 case TGSI_TYPE_UNTYPED
:
914 bld_store
= &bld_base
->base
;
916 case TGSI_TYPE_UNSIGNED
:
917 bld_store
= &bld_base
->uint_bld
;
919 case TGSI_TYPE_SIGNED
:
920 bld_store
= &bld_base
->int_bld
;
922 case TGSI_TYPE_DOUBLE
:
929 switch( inst
->Instruction
.Saturate
) {
933 case TGSI_SAT_ZERO_ONE
:
934 value
= lp_build_max(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.zero
);
935 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
938 case TGSI_SAT_MINUS_PLUS_ONE
:
939 value
= lp_build_max(&bld
->bld_base
.base
, value
, lp_build_const_vec(bld
->bld_base
.base
.gallivm
, bld
->bld_base
.base
.type
, -1.0));
940 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
947 if (reg
->Register
.Indirect
) {
948 indirect_index
= get_indirect_index(bld
,
953 assert(reg
->Register
.Index
<=
954 bld
->bld_base
.info
->file_max
[reg
->Register
.File
]);
957 switch( reg
->Register
.File
) {
958 case TGSI_FILE_OUTPUT
:
959 if (reg
->Register
.Indirect
) {
960 LLVMValueRef chan_vec
=
961 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
962 LLVMValueRef length_vec
=
963 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
964 LLVMValueRef index_vec
; /* indexes into the temp registers */
965 LLVMValueRef outputs_array
;
966 LLVMValueRef pixel_offsets
;
967 LLVMTypeRef float_ptr_type
;
970 /* build pixel offset vector: {0, 1, 2, 3, ...} */
971 pixel_offsets
= uint_bld
->undef
;
972 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
973 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
974 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
978 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
979 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
980 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
981 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
982 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
985 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
986 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
,
989 /* Scatter store values into temp registers */
990 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
991 &bld
->exec_mask
, pred
);
994 LLVMValueRef out_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
996 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, out_ptr
);
1000 case TGSI_FILE_TEMPORARY
:
1001 if (reg
->Register
.Indirect
) {
1002 LLVMValueRef chan_vec
=
1003 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
1004 LLVMValueRef length_vec
=
1005 lp_build_const_int_vec(gallivm
, uint_bld
->type
,
1006 bld
->bld_base
.base
.type
.length
);
1007 LLVMValueRef index_vec
; /* indexes into the temp registers */
1008 LLVMValueRef temps_array
;
1009 LLVMValueRef pixel_offsets
;
1010 LLVMTypeRef float_ptr_type
;
1013 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1014 pixel_offsets
= uint_bld
->undef
;
1015 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
1016 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1017 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
1021 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1022 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1023 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1024 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1025 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1028 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1029 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
1030 float_ptr_type
, "");
1032 /* Scatter store values into temp registers */
1033 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
1034 &bld
->exec_mask
, pred
);
1037 LLVMValueRef temp_ptr
;
1040 case TGSI_TYPE_UNSIGNED
:
1041 case TGSI_TYPE_SIGNED
: {
1042 LLVMTypeRef itype
= LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4);
1043 LLVMTypeRef ivtype
= LLVMPointerType(itype
, 0);
1044 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1046 LLVMValueRef temp_value_ptr
;
1048 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, ivtype
, "");
1049 temp_value_ptr
= LLVMBuildBitCast(builder
, value
, itype
, "");
1050 value
= temp_value_ptr
;
1054 case TGSI_TYPE_FLOAT
:
1055 case TGSI_TYPE_UNTYPED
:
1056 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1061 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, temp_ptr
);
1065 case TGSI_FILE_ADDRESS
:
1066 assert(dtype
== TGSI_TYPE_SIGNED
);
1067 assert(LLVMTypeOf(value
) == bld_base
->base
.int_vec_type
);
1068 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1069 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1072 case TGSI_FILE_PREDICATE
:
1073 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1074 bld
->preds
[reg
->Register
.Index
][chan_index
]);
1084 struct lp_build_tgsi_context
* bld_base
,
1085 const struct tgsi_full_instruction
* inst
,
1086 const struct tgsi_opcode_info
* info
,
1087 LLVMValueRef dst
[4])
1090 unsigned chan_index
;
1091 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1094 LLVMValueRef pred
[TGSI_NUM_CHANNELS
];
1096 emit_fetch_predicate( bld
, inst
, pred
);
1098 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1099 emit_store_chan(bld_base
, inst
, 0, chan_index
, pred
[chan_index
], dst
[chan_index
]);
1105 * High-level instruction translators.
1109 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
1110 const struct tgsi_full_instruction
*inst
,
1111 enum lp_build_tex_modifier modifier
,
1112 LLVMValueRef
*texel
)
1114 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1116 LLVMValueRef lod_bias
, explicit_lod
;
1117 LLVMValueRef oow
= NULL
;
1118 LLVMValueRef coords
[3];
1119 LLVMValueRef ddx
[3];
1120 LLVMValueRef ddy
[3];
1121 unsigned num_coords
;
1124 if (!bld
->sampler
) {
1125 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1126 for (i
= 0; i
< 4; i
++) {
1127 texel
[i
] = bld
->bld_base
.base
.undef
;
1132 switch (inst
->Texture
.Texture
) {
1133 case TGSI_TEXTURE_1D
:
1136 case TGSI_TEXTURE_1D_ARRAY
:
1137 case TGSI_TEXTURE_2D
:
1138 case TGSI_TEXTURE_RECT
:
1141 case TGSI_TEXTURE_SHADOW1D
:
1142 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1143 case TGSI_TEXTURE_SHADOW2D
:
1144 case TGSI_TEXTURE_SHADOWRECT
:
1145 case TGSI_TEXTURE_2D_ARRAY
:
1146 case TGSI_TEXTURE_3D
:
1147 case TGSI_TEXTURE_CUBE
:
1150 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1158 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
1159 lod_bias
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1160 explicit_lod
= NULL
;
1162 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
1164 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1168 explicit_lod
= NULL
;
1171 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
1172 oow
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1173 oow
= lp_build_rcp(&bld
->bld_base
.base
, oow
);
1176 for (i
= 0; i
< num_coords
; i
++) {
1177 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1178 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
1179 coords
[i
] = lp_build_mul(&bld
->bld_base
.base
, coords
[i
], oow
);
1181 for (i
= num_coords
; i
< 3; i
++) {
1182 coords
[i
] = bld
->bld_base
.base
.undef
;
1185 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
1186 LLVMValueRef index0
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, 0);
1187 for (i
= 0; i
< num_coords
; i
++) {
1188 LLVMValueRef src1
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 1, i
);
1189 LLVMValueRef src2
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 2, i
);
1190 ddx
[i
] = LLVMBuildExtractElement(builder
, src1
, index0
, "");
1191 ddy
[i
] = LLVMBuildExtractElement(builder
, src2
, index0
, "");
1193 unit
= inst
->Src
[3].Register
.Index
;
1195 for (i
= 0; i
< num_coords
; i
++) {
1196 ddx
[i
] = lp_build_scalar_ddx( &bld
->bld_base
.base
, coords
[i
] );
1197 ddy
[i
] = lp_build_scalar_ddy( &bld
->bld_base
.base
, coords
[i
] );
1199 unit
= inst
->Src
[1].Register
.Index
;
1201 for (i
= num_coords
; i
< 3; i
++) {
1202 ddx
[i
] = LLVMGetUndef(bld
->bld_base
.base
.elem_type
);
1203 ddy
[i
] = LLVMGetUndef(bld
->bld_base
.base
.elem_type
);
1206 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1207 bld
->bld_base
.base
.gallivm
,
1208 bld
->bld_base
.base
.type
,
1209 unit
, num_coords
, coords
,
1211 lod_bias
, explicit_lod
,
1216 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
1221 for (i
= 0; i
< 5; i
++) {
1224 if (pc
+ i
>= bld
->bld_base
.info
->num_instructions
)
1227 opcode
= bld
->bld_base
.instructions
[pc
+ i
].Instruction
.Opcode
;
1229 if (opcode
== TGSI_OPCODE_END
)
1232 if (opcode
== TGSI_OPCODE_TEX
||
1233 opcode
== TGSI_OPCODE_TXP
||
1234 opcode
== TGSI_OPCODE_TXD
||
1235 opcode
== TGSI_OPCODE_TXB
||
1236 opcode
== TGSI_OPCODE_TXL
||
1237 opcode
== TGSI_OPCODE_TXF
||
1238 opcode
== TGSI_OPCODE_TXQ
||
1239 opcode
== TGSI_OPCODE_CAL
||
1240 opcode
== TGSI_OPCODE_CALLNZ
||
1241 opcode
== TGSI_OPCODE_IF
||
1242 opcode
== TGSI_OPCODE_IFC
||
1243 opcode
== TGSI_OPCODE_BGNLOOP
||
1244 opcode
== TGSI_OPCODE_SWITCH
)
1254 * Kill fragment if any of the src register values are negative.
1258 struct lp_build_tgsi_soa_context
*bld
,
1259 const struct tgsi_full_instruction
*inst
,
1262 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1263 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1264 LLVMValueRef terms
[TGSI_NUM_CHANNELS
];
1266 unsigned chan_index
;
1268 memset(&terms
, 0, sizeof terms
);
1270 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1273 /* Unswizzle channel */
1274 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
1276 /* Check if the component has not been already tested. */
1277 assert(swizzle
< TGSI_NUM_CHANNELS
);
1278 if( !terms
[swizzle
] )
1279 /* TODO: change the comparison operator instead of setting the sign */
1280 terms
[swizzle
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, chan_index
);
1284 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1285 if(terms
[chan_index
]) {
1286 LLVMValueRef chan_mask
;
1289 * If term < 0 then mask = 0 else mask = ~0.
1291 chan_mask
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->bld_base
.base
.zero
);
1294 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
1301 lp_build_mask_update(bld
->mask
, mask
);
1303 if (!near_end_of_shader(bld
, pc
))
1304 lp_build_mask_check(bld
->mask
);
1310 * Predicated fragment kill.
1311 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1312 * The only predication is the execution mask which will apply if
1313 * we're inside a loop or conditional.
1316 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
1319 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1322 /* For those channels which are "alive", disable fragment shader
1325 if (bld
->exec_mask
.has_mask
) {
1326 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1329 LLVMValueRef zero
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
1333 lp_build_mask_update(bld
->mask
, mask
);
1335 if (!near_end_of_shader(bld
, pc
))
1336 lp_build_mask_check(bld
->mask
);
1341 * Emit code which will dump the value of all the temporary registers
1345 emit_dump_temps(struct lp_build_tgsi_soa_context
*bld
)
1347 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1348 LLVMBuilderRef builder
= gallivm
->builder
;
1349 LLVMValueRef temp_ptr
;
1350 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
1351 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
1352 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
1353 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
1355 int n
= bld
->bld_base
.info
->file_max
[TGSI_FILE_TEMPORARY
];
1357 for (index
= 0; index
< n
; index
++) {
1358 LLVMValueRef idx
= lp_build_const_int32(gallivm
, index
);
1359 LLVMValueRef v
[4][4], res
;
1362 lp_build_printf(gallivm
, "TEMP[%d]:\n", idx
);
1364 for (chan
= 0; chan
< 4; chan
++) {
1365 temp_ptr
= lp_get_temp_ptr_soa(bld
, index
, chan
);
1366 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1367 v
[chan
][0] = LLVMBuildExtractElement(builder
, res
, i0
, "");
1368 v
[chan
][1] = LLVMBuildExtractElement(builder
, res
, i1
, "");
1369 v
[chan
][2] = LLVMBuildExtractElement(builder
, res
, i2
, "");
1370 v
[chan
][3] = LLVMBuildExtractElement(builder
, res
, i3
, "");
1373 lp_build_printf(gallivm
, " X: %f %f %f %f\n",
1374 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
1375 lp_build_printf(gallivm
, " Y: %f %f %f %f\n",
1376 v
[1][0], v
[1][1], v
[1][2], v
[1][3]);
1377 lp_build_printf(gallivm
, " Z: %f %f %f %f\n",
1378 v
[2][0], v
[2][1], v
[2][2], v
[2][3]);
1379 lp_build_printf(gallivm
, " W: %f %f %f %f\n",
1380 v
[3][0], v
[3][1], v
[3][2], v
[3][3]);
1387 lp_emit_declaration_soa(
1388 struct lp_build_tgsi_context
*bld_base
,
1389 const struct tgsi_full_declaration
*decl
)
1391 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1392 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1393 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
1394 const unsigned first
= decl
->Range
.First
;
1395 const unsigned last
= decl
->Range
.Last
;
1398 for (idx
= first
; idx
<= last
; ++idx
) {
1399 assert(last
<= bld
->bld_base
.info
->file_max
[decl
->Declaration
.File
]);
1400 switch (decl
->Declaration
.File
) {
1401 case TGSI_FILE_TEMPORARY
:
1402 assert(idx
< LP_MAX_TGSI_TEMPS
);
1403 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
1404 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1405 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
1409 case TGSI_FILE_OUTPUT
:
1410 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
1411 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1412 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
1413 vec_type
, "output");
1417 case TGSI_FILE_ADDRESS
:
1418 /* ADDR registers are the only allocated with an integer LLVM IR type,
1419 * as they are guaranteed to always have integers.
1420 * XXX: Not sure if this exception is worthwhile (or the whole idea of
1421 * an ADDR register for that matter).
1423 assert(idx
< LP_MAX_TGSI_ADDRS
);
1424 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1425 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, bld_base
->base
.int_vec_type
, "addr");
1428 case TGSI_FILE_PREDICATE
:
1429 assert(idx
< LP_MAX_TGSI_PREDS
);
1430 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1431 bld
->preds
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
,
1436 /* don't need to declare other vars */
1443 void lp_emit_immediate_soa(
1444 struct lp_build_tgsi_context
*bld_base
,
1445 const struct tgsi_full_immediate
*imm
)
1447 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1448 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1450 /* simply copy the immediate values into the next immediates[] slot */
1452 const uint size
= imm
->Immediate
.NrTokens
- 1;
1454 assert(bld
->num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1455 switch (imm
->Immediate
.DataType
) {
1456 case TGSI_IMM_FLOAT32
:
1457 for( i
= 0; i
< size
; ++i
)
1458 bld
->immediates
[bld
->num_immediates
][i
] =
1459 lp_build_const_vec(gallivm
, bld_base
->base
.type
, imm
->u
[i
].Float
);
1462 case TGSI_IMM_UINT32
:
1463 for( i
= 0; i
< size
; ++i
) {
1464 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->uint_bld
.type
, imm
->u
[i
].Uint
);
1465 bld
->immediates
[bld
->num_immediates
][i
] =
1466 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1470 case TGSI_IMM_INT32
:
1471 for( i
= 0; i
< size
; ++i
) {
1472 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->int_bld
.type
, imm
->u
[i
].Int
);
1473 bld
->immediates
[bld
->num_immediates
][i
] =
1474 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1479 for( i
= size
; i
< 4; ++i
)
1480 bld
->immediates
[bld
->num_immediates
][i
] = bld_base
->base
.undef
;
1482 bld
->num_immediates
++;
1487 const struct lp_build_tgsi_action
* action
,
1488 struct lp_build_tgsi_context
* bld_base
,
1489 struct lp_build_emit_data
* emit_data
)
1491 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1493 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
,
1494 &emit_data
->output
[emit_data
->chan
], NULL
);
1499 const struct lp_build_tgsi_action
* action
,
1500 struct lp_build_tgsi_context
* bld_base
,
1501 struct lp_build_emit_data
* emit_data
)
1503 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1505 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
, NULL
,
1506 &emit_data
->output
[emit_data
->chan
]);
1511 const struct lp_build_tgsi_action
* action
,
1512 struct lp_build_tgsi_context
* bld_base
,
1513 struct lp_build_emit_data
* emit_data
)
1515 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1517 emit_kilp(bld
, bld_base
->pc
- 1);
1522 const struct lp_build_tgsi_action
* action
,
1523 struct lp_build_tgsi_context
* bld_base
,
1524 struct lp_build_emit_data
* emit_data
)
1526 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1528 emit_kil(bld
, emit_data
->inst
, bld_base
->pc
- 1);
1533 const struct lp_build_tgsi_action
* action
,
1534 struct lp_build_tgsi_context
* bld_base
,
1535 struct lp_build_emit_data
* emit_data
)
1537 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1539 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
, emit_data
->output
);
1544 const struct lp_build_tgsi_action
* action
,
1545 struct lp_build_tgsi_context
* bld_base
,
1546 struct lp_build_emit_data
* emit_data
)
1548 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1550 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
1556 const struct lp_build_tgsi_action
* action
,
1557 struct lp_build_tgsi_context
* bld_base
,
1558 struct lp_build_emit_data
* emit_data
)
1560 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1562 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
1568 const struct lp_build_tgsi_action
* action
,
1569 struct lp_build_tgsi_context
* bld_base
,
1570 struct lp_build_emit_data
* emit_data
)
1572 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1574 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
1580 const struct lp_build_tgsi_action
* action
,
1581 struct lp_build_tgsi_context
* bld_base
,
1582 struct lp_build_emit_data
* emit_data
)
1584 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1586 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_PROJECTED
,
1592 const struct lp_build_tgsi_action
* action
,
1593 struct lp_build_tgsi_context
* bld_base
,
1594 struct lp_build_emit_data
* emit_data
)
1596 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1598 lp_exec_mask_call(&bld
->exec_mask
, emit_data
->inst
->Label
.Label
,
1604 const struct lp_build_tgsi_action
* action
,
1605 struct lp_build_tgsi_context
* bld_base
,
1606 struct lp_build_emit_data
* emit_data
)
1608 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1610 lp_exec_mask_ret(&bld
->exec_mask
, &bld_base
->pc
);
1615 const struct lp_build_tgsi_action
* action
,
1616 struct lp_build_tgsi_context
* bld_base
,
1617 struct lp_build_emit_data
* emit_data
)
1619 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1621 lp_exec_break(&bld
->exec_mask
);
1626 const struct lp_build_tgsi_action
* action
,
1627 struct lp_build_tgsi_context
* bld_base
,
1628 struct lp_build_emit_data
* emit_data
)
1631 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1633 tmp
= lp_build_cmp(&bld_base
->base
, PIPE_FUNC_NOTEQUAL
,
1634 emit_data
->args
[0], bld
->bld_base
.base
.zero
);
1635 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
1640 const struct lp_build_tgsi_action
* action
,
1641 struct lp_build_tgsi_context
* bld_base
,
1642 struct lp_build_emit_data
* emit_data
)
1644 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1646 lp_exec_bgnloop(&bld
->exec_mask
);
1651 const struct lp_build_tgsi_action
* action
,
1652 struct lp_build_tgsi_context
* bld_base
,
1653 struct lp_build_emit_data
* emit_data
)
1655 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1657 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1662 const struct lp_build_tgsi_action
* action
,
1663 struct lp_build_tgsi_context
* bld_base
,
1664 struct lp_build_emit_data
* emit_data
)
1666 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1668 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1673 const struct lp_build_tgsi_action
* action
,
1674 struct lp_build_tgsi_context
* bld_base
,
1675 struct lp_build_emit_data
* emit_data
)
1677 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1679 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1684 const struct lp_build_tgsi_action
* action
,
1685 struct lp_build_tgsi_context
* bld_base
,
1686 struct lp_build_emit_data
* emit_data
)
1688 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1690 lp_exec_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
1695 const struct lp_build_tgsi_action
* action
,
1696 struct lp_build_tgsi_context
* bld_base
,
1697 struct lp_build_emit_data
* emit_data
)
1699 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1701 lp_exec_mask_endsub(&bld
->exec_mask
, &bld_base
->pc
);
1706 const struct lp_build_tgsi_action
* action
,
1707 struct lp_build_tgsi_context
* bld_base
,
1708 struct lp_build_emit_data
* emit_data
)
1710 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1712 lp_exec_continue(&bld
->exec_mask
);
1715 /* XXX: Refactor and move it to lp_bld_tgsi_action.c
1717 * XXX: What do the comments about xmm registers mean? Maybe they are left over
1718 * from old code, but there is no garauntee that LLVM will use those registers
1721 * XXX: There should be no calls to lp_build_emit_fetch in this function. This
1722 * should be handled by the emit_data->fetch_args function. */
1725 const struct lp_build_tgsi_action
* action
,
1726 struct lp_build_tgsi_context
* bld_base
,
1727 struct lp_build_emit_data
* emit_data
)
1729 LLVMValueRef tmp0
, tmp1
;
1730 LLVMValueRef tmp4
= NULL
;
1731 LLVMValueRef tmp5
= NULL
;
1732 LLVMValueRef tmp6
= NULL
;
1733 LLVMValueRef tmp7
= NULL
;
1734 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1736 uint dims
= (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1738 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) ||
1739 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
) ||
1740 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
) ||
1741 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 4)) {
1743 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1746 /* xmm0 = src.x * src.x */
1747 tmp0
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
1748 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
1751 tmp0
= lp_build_mul( &bld
->bld_base
.base
, tmp0
, tmp0
);
1754 /* xmm0 = xmm0 + src.y * src.y */
1755 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Y
);
1756 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
1759 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1760 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1763 /* xmm0 = xmm0 + src.z * src.z */
1764 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Z
);
1765 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
1768 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1769 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1773 /* xmm0 = xmm0 + src.w * src.w */
1774 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1775 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
)) {
1778 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1779 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1781 /* xmm1 = 1 / sqrt(xmm0) */
1782 tmp1
= lp_build_rsqrt( &bld
->bld_base
.base
, tmp0
);
1783 /* dst.x = xmm1 * src.x */
1784 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
1785 emit_data
->output
[TGSI_CHAN_X
] = lp_build_mul( &bld
->bld_base
.base
, tmp4
, tmp1
);
1787 /* dst.y = xmm1 * src.y */
1788 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
1789 emit_data
->output
[TGSI_CHAN_Y
] = lp_build_mul( &bld
->bld_base
.base
, tmp5
, tmp1
);
1792 /* dst.z = xmm1 * src.z */
1793 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
1794 emit_data
->output
[TGSI_CHAN_Z
] = lp_build_mul( &bld
->bld_base
.base
, tmp6
, tmp1
);
1796 /* dst.w = xmm1 * src.w */
1797 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) && dims
== 4) {
1798 emit_data
->output
[TGSI_CHAN_W
] = lp_build_mul( &bld
->bld_base
.base
, tmp7
, tmp1
);
1803 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 3) {
1804 emit_data
->output
[TGSI_CHAN_W
] = bld
->bld_base
.base
.one
;
1808 static void emit_prologue(struct lp_build_tgsi_context
* bld_base
)
1810 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1811 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1813 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
1814 LLVMValueRef array_size
=
1815 lp_build_const_int32(gallivm
,
1816 bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
1817 bld
->temps_array
= lp_build_array_alloca(gallivm
,
1818 bld_base
->base
.vec_type
, array_size
,
1822 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
1823 LLVMValueRef array_size
=
1824 lp_build_const_int32(gallivm
,
1825 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
1826 bld
->outputs_array
= lp_build_array_alloca(gallivm
,
1827 bld_base
->base
.vec_type
, array_size
,
1831 /* If we have indirect addressing in inputs we need to copy them into
1832 * our alloca array to be able to iterate over them */
1833 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
1834 unsigned index
, chan
;
1835 LLVMTypeRef vec_type
= bld_base
->base
.vec_type
;
1836 LLVMValueRef array_size
= lp_build_const_int32(gallivm
,
1837 bld_base
->info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
1838 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
1839 vec_type
, array_size
,
1842 assert(bld_base
->info
->num_inputs
1843 <= bld_base
->info
->file_max
[TGSI_FILE_INPUT
] + 1);
1845 for (index
= 0; index
< bld_base
->info
->num_inputs
; ++index
) {
1846 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1847 LLVMValueRef lindex
=
1848 lp_build_const_int32(gallivm
, index
* 4 + chan
);
1849 LLVMValueRef input_ptr
=
1850 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
1852 LLVMValueRef value
= bld
->inputs
[index
][chan
];
1854 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
1860 static void emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
1862 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1866 emit_dump_temps(bld
);
1869 /* If we have indirect addressing in outputs we need to copy our alloca array
1870 * to the outputs slots specified by the called */
1871 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
1872 unsigned index
, chan
;
1873 assert(bld_base
->info
->num_outputs
<=
1874 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
1875 for (index
= 0; index
< bld_base
->info
->num_outputs
; ++index
) {
1876 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1877 bld
->outputs
[index
][chan
] = lp_get_output_ptr(bld
, index
, chan
);
1884 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
1885 const struct tgsi_token
*tokens
,
1886 struct lp_type type
,
1887 struct lp_build_mask_context
*mask
,
1888 LLVMValueRef consts_ptr
,
1889 LLVMValueRef system_values_array
,
1890 const LLVMValueRef
*pos
,
1891 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
1892 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1893 struct lp_build_sampler_soa
*sampler
,
1894 const struct tgsi_shader_info
*info
)
1896 struct lp_build_tgsi_soa_context bld
;
1898 struct lp_type res_type
;
1900 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
1901 memset(&res_type
, 0, sizeof res_type
);
1902 res_type
.width
= type
.width
;
1903 res_type
.length
= type
.length
;
1906 /* Setup build context */
1907 memset(&bld
, 0, sizeof bld
);
1908 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
1909 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
1910 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
1911 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
1914 bld
.inputs
= inputs
;
1915 bld
.outputs
= outputs
;
1916 bld
.consts_ptr
= consts_ptr
;
1917 bld
.sampler
= sampler
;
1918 bld
.bld_base
.info
= info
;
1919 bld
.indirect_files
= info
->indirect_files
;
1921 bld
.bld_base
.soa
= TRUE
;
1922 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
1923 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
1924 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
1925 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
1926 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = emit_fetch_system_value
;
1927 bld
.bld_base
.emit_store
= emit_store
;
1929 bld
.bld_base
.emit_declaration
= lp_emit_declaration_soa
;
1930 bld
.bld_base
.emit_immediate
= lp_emit_immediate_soa
;
1932 bld
.bld_base
.emit_prologue
= emit_prologue
;
1933 bld
.bld_base
.emit_epilogue
= emit_epilogue
;
1935 /* Set opcode actions */
1936 lp_set_default_actions_cpu(&bld
.bld_base
);
1938 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1939 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNSUB
].emit
= bgnsub_emit
;
1940 bld
.bld_base
.op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1941 bld
.bld_base
.op_actions
[TGSI_OPCODE_CAL
].emit
= cal_emit
;
1942 bld
.bld_base
.op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1943 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDX
].emit
= ddx_emit
;
1944 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDY
].emit
= ddy_emit
;
1945 bld
.bld_base
.op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1946 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1947 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1948 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSUB
].emit
= endsub_emit
;
1949 bld
.bld_base
.op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1950 bld
.bld_base
.op_actions
[TGSI_OPCODE_KIL
].emit
= kil_emit
;
1951 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILP
].emit
= kilp_emit
;
1952 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM
].emit
= nrm_emit
;
1953 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM4
].emit
= nrm_emit
;
1954 bld
.bld_base
.op_actions
[TGSI_OPCODE_RET
].emit
= ret_emit
;
1955 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX
].emit
= tex_emit
;
1956 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB
].emit
= txb_emit
;
1957 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXD
].emit
= txd_emit
;
1958 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL
].emit
= txl_emit
;
1959 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXP
].emit
= txp_emit
;
1961 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.base
);
1964 bld
.system_values_array
= system_values_array
;
1966 lp_build_tgsi_llvm(&bld
.bld_base
, tokens
);
1969 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
1970 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
1971 debug_printf("11111111111111111111111111111 \n");
1972 tgsi_dump(tokens
, 0);
1973 lp_debug_dump_value(function
);
1974 debug_printf("2222222222222222222222222222 \n");
1978 LLVMModuleRef module
= LLVMGetGlobalParent(
1979 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
1980 LLVMDumpModule(module
);
1987 * Build up the system values array out of individual values such as
1988 * the instance ID, front-face, primitive ID, etc. The shader info is
1989 * used to determine which system values are needed and where to put
1990 * them in the system values array.
1992 * XXX only instance ID is implemented at this time.
1994 * The system values register file is similar to the constants buffer.
1995 * Example declaration:
1996 * DCL SV[0], INSTANCEID
1997 * Example instruction:
1998 * MOVE foo, SV[0].xxxx;
2000 * \return LLVM float array (interpreted as float [][4])
2003 lp_build_system_values_array(struct gallivm_state
*gallivm
,
2004 const struct tgsi_shader_info
*info
,
2005 LLVMValueRef instance_id
,
2006 LLVMValueRef facing
)
2008 LLVMValueRef size
= lp_build_const_int32(gallivm
, 4 * info
->num_system_values
);
2009 LLVMTypeRef float_t
= LLVMFloatTypeInContext(gallivm
->context
);
2010 LLVMValueRef array
= lp_build_array_alloca(gallivm
, float_t
,
2011 size
, "sysvals_array");
2014 for (i
= 0; i
< info
->num_system_values
; i
++) {
2015 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
* 4);
2016 LLVMValueRef ptr
, value
= 0;
2018 switch (info
->system_value_semantic_name
[i
]) {
2019 case TGSI_SEMANTIC_INSTANCEID
:
2020 /* convert instance ID from int to float */
2021 value
= LLVMBuildSIToFP(gallivm
->builder
, instance_id
, float_t
,
2022 "sysval_instanceid");
2024 case TGSI_SEMANTIC_FACE
:
2027 assert(0 && "unexpected semantic in build_system_values_array()");
2030 ptr
= LLVMBuildGEP(gallivm
->builder
, array
, &index
, 1, "");
2031 LLVMBuildStore(gallivm
->builder
, value
, ptr
);