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
)
70 mask
->has_mask
= FALSE
;
71 mask
->cond_stack_size
= 0;
72 mask
->loop_stack_size
= 0;
73 mask
->call_stack_size
= 0;
75 mask
->int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, mask
->bld
->type
);
76 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
77 LLVMConstAllOnes(mask
->int_vec_type
);
80 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
82 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
84 if (mask
->loop_stack_size
) {
85 /*for loops we need to update the entire mask at runtime */
87 assert(mask
->break_mask
);
88 tmp
= LLVMBuildAnd(builder
,
92 mask
->exec_mask
= LLVMBuildAnd(builder
,
97 mask
->exec_mask
= mask
->cond_mask
;
99 if (mask
->call_stack_size
) {
100 mask
->exec_mask
= LLVMBuildAnd(builder
,
106 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
107 mask
->loop_stack_size
> 0 ||
108 mask
->call_stack_size
> 0);
111 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
114 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
116 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
117 if (mask
->cond_stack_size
== 0) {
118 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
120 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
121 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
122 mask
->cond_mask
= LLVMBuildAnd(builder
,
126 lp_exec_mask_update(mask
);
129 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
131 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
132 LLVMValueRef prev_mask
;
133 LLVMValueRef inv_mask
;
135 assert(mask
->cond_stack_size
);
136 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
137 if (mask
->cond_stack_size
== 1) {
138 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
141 inv_mask
= LLVMBuildNot(builder
, mask
->cond_mask
, "");
143 mask
->cond_mask
= LLVMBuildAnd(builder
,
146 lp_exec_mask_update(mask
);
149 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
151 assert(mask
->cond_stack_size
);
152 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
153 lp_exec_mask_update(mask
);
156 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
158 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
160 if (mask
->loop_stack_size
== 0) {
161 assert(mask
->loop_block
== NULL
);
162 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
163 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
164 assert(mask
->break_var
== NULL
);
167 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
169 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
170 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
171 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
172 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
173 ++mask
->loop_stack_size
;
175 mask
->break_var
= lp_build_alloca(mask
->bld
->gallivm
, mask
->int_vec_type
, "");
176 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
178 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->gallivm
, "bgnloop");
179 LLVMBuildBr(builder
, mask
->loop_block
);
180 LLVMPositionBuilderAtEnd(builder
, mask
->loop_block
);
182 mask
->break_mask
= LLVMBuildLoad(builder
, mask
->break_var
, "");
184 lp_exec_mask_update(mask
);
187 static void lp_exec_break(struct lp_exec_mask
*mask
)
189 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
190 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
194 mask
->break_mask
= LLVMBuildAnd(builder
,
196 exec_mask
, "break_full");
198 lp_exec_mask_update(mask
);
201 static void lp_exec_continue(struct lp_exec_mask
*mask
)
203 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
204 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
208 mask
->cont_mask
= LLVMBuildAnd(builder
,
212 lp_exec_mask_update(mask
);
216 static void lp_exec_endloop(struct gallivm_state
*gallivm
,
217 struct lp_exec_mask
*mask
)
219 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
220 LLVMBasicBlockRef endloop
;
221 LLVMTypeRef reg_type
= LLVMIntTypeInContext(gallivm
->context
,
222 mask
->bld
->type
.width
*
223 mask
->bld
->type
.length
);
226 assert(mask
->break_mask
);
229 * Restore the cont_mask, but don't pop
231 assert(mask
->loop_stack_size
);
232 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
233 lp_exec_mask_update(mask
);
236 * Unlike the continue mask, the break_mask must be preserved across loop
239 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
241 /* i1cond = (mask == 0) */
242 i1cond
= LLVMBuildICmp(
245 LLVMBuildBitCast(builder
, mask
->exec_mask
, reg_type
, ""),
246 LLVMConstNull(reg_type
), "");
248 endloop
= lp_build_insert_new_block(mask
->bld
->gallivm
, "endloop");
250 LLVMBuildCondBr(builder
,
251 i1cond
, mask
->loop_block
, endloop
);
253 LLVMPositionBuilderAtEnd(builder
, endloop
);
255 assert(mask
->loop_stack_size
);
256 --mask
->loop_stack_size
;
257 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
258 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
259 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
260 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
262 lp_exec_mask_update(mask
);
265 /* stores val into an address pointed to by dst.
266 * mask->exec_mask is used to figure out which bits of val
267 * should be stored into the address
268 * (0 means don't store this bit, 1 means do store).
270 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
271 struct lp_build_context
*bld_store
,
276 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
278 /* Mix the predicate and execution mask */
279 if (mask
->has_mask
) {
281 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
283 pred
= mask
->exec_mask
;
288 LLVMValueRef real_val
, dst_val
;
290 dst_val
= LLVMBuildLoad(builder
, dst
, "");
291 real_val
= lp_build_select(bld_store
,
295 LLVMBuildStore(builder
, real_val
, dst
);
297 LLVMBuildStore(builder
, val
, dst
);
300 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
304 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
305 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
306 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
307 mask
->call_stack_size
++;
311 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
313 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
314 LLVMValueRef exec_mask
;
316 if (mask
->call_stack_size
== 0) {
317 /* returning from main() */
321 exec_mask
= LLVMBuildNot(builder
,
325 mask
->ret_mask
= LLVMBuildAnd(builder
,
327 exec_mask
, "ret_full");
329 lp_exec_mask_update(mask
);
332 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
336 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
338 assert(mask
->call_stack_size
);
339 mask
->call_stack_size
--;
340 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
341 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
342 lp_exec_mask_update(mask
);
347 * Return pointer to a temporary register channel (src or dest).
348 * Note that indirect addressing cannot be handled here.
349 * \param index which temporary register
350 * \param chan which channel of the temp register.
353 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context
*bld
,
357 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
359 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
360 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, index
* 4 + chan
);
361 return LLVMBuildGEP(builder
, bld
->temps_array
, &lindex
, 1, "");
364 return bld
->temps
[index
][chan
];
369 * Return pointer to a output register channel (src or dest).
370 * Note that indirect addressing cannot be handled here.
371 * \param index which output register
372 * \param chan which channel of the output register.
375 lp_get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
379 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
381 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
382 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
,
384 return LLVMBuildGEP(builder
, bld
->outputs_array
, &lindex
, 1, "");
387 return bld
->outputs
[index
][chan
];
393 * XXX the lp_build_gather() function should be capable of doing this
394 * with a little work.
397 build_gather(struct lp_build_context
*bld
,
398 LLVMValueRef base_ptr
,
399 LLVMValueRef indexes
)
401 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
402 LLVMValueRef res
= bld
->undef
;
406 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
408 for (i
= 0; i
< bld
->type
.length
; i
++) {
409 LLVMValueRef ii
= lp_build_const_int32(bld
->gallivm
, i
);
410 LLVMValueRef index
= LLVMBuildExtractElement(builder
,
412 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
413 &index
, 1, "gather_ptr");
414 LLVMValueRef scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
416 res
= LLVMBuildInsertElement(builder
, res
, scalar
, ii
, "");
424 * Scatter/store vector.
427 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
428 LLVMValueRef base_ptr
,
429 LLVMValueRef indexes
,
431 struct lp_exec_mask
*mask
,
434 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
435 LLVMBuilderRef builder
= gallivm
->builder
;
438 /* Mix the predicate and execution mask */
439 if (mask
->has_mask
) {
441 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
444 pred
= mask
->exec_mask
;
449 * Loop over elements of index_vec, store scalar value.
451 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
452 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
453 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
454 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
455 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
456 LLVMValueRef scalar_pred
= pred
?
457 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
460 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
461 ii
, val
, index
, scalar_ptr
);
464 LLVMValueRef real_val
, dst_val
;
465 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
466 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
467 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
470 LLVMBuildStore(builder
, val
, scalar_ptr
);
477 * Read the current value of the ADDR register, convert the floats to
478 * ints, add the base index and return the vector of offsets.
479 * The offsets will be used to index into the constant buffer or
480 * temporary register file.
483 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
484 unsigned reg_file
, unsigned reg_index
,
485 const struct tgsi_src_register
*indirect_reg
)
487 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
488 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
489 /* always use X component of address register */
490 unsigned swizzle
= indirect_reg
->SwizzleX
;
493 LLVMValueRef max_index
;
496 assert(bld
->indirect_files
& (1 << reg_file
));
498 base
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, reg_index
);
501 rel
= LLVMBuildLoad(builder
,
502 bld
->addr
[indirect_reg
->Index
][swizzle
],
505 index
= lp_build_add(uint_bld
, base
, rel
);
507 max_index
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
,
509 bld
->bld_base
.info
->file_max
[reg_file
]);
511 assert(!uint_bld
->type
.sign
);
512 index
= lp_build_min(uint_bld
, index
, max_index
);
517 static struct lp_build_context
*
518 stype_to_fetch(struct lp_build_tgsi_context
* bld_base
,
519 enum tgsi_opcode_type stype
)
521 struct lp_build_context
*bld_fetch
;
524 case TGSI_TYPE_FLOAT
:
525 case TGSI_TYPE_UNTYPED
:
526 bld_fetch
= &bld_base
->base
;
528 case TGSI_TYPE_UNSIGNED
:
529 bld_fetch
= &bld_base
->uint_bld
;
531 case TGSI_TYPE_SIGNED
:
532 bld_fetch
= &bld_base
->int_bld
;
535 case TGSI_TYPE_DOUBLE
:
546 struct lp_build_tgsi_context
* bld_base
,
547 const struct tgsi_full_src_register
* reg
,
548 enum tgsi_opcode_type stype
,
551 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
552 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
553 LLVMBuilderRef builder
= gallivm
->builder
;
554 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
555 LLVMValueRef indirect_index
= NULL
;
556 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
558 /* XXX: Handle fetching xyzw components as a vector */
559 assert(swizzle
!= ~0);
561 if (reg
->Register
.Indirect
) {
562 indirect_index
= get_indirect_index(bld
,
568 if (reg
->Register
.Indirect
) {
569 LLVMValueRef swizzle_vec
=
570 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
571 LLVMValueRef index_vec
; /* index into the const buffer */
573 /* index_vec = indirect_index * 4 + swizzle */
574 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
575 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
577 /* Gather values from the constant buffer */
578 return build_gather(bld_fetch
, bld
->consts_ptr
, index_vec
);
581 LLVMValueRef index
; /* index into the const buffer */
582 LLVMValueRef scalar
, scalar_ptr
;
584 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
*4 + swizzle
);
586 scalar_ptr
= LLVMBuildGEP(builder
, bld
->consts_ptr
,
589 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
590 LLVMTypeRef ivtype
= LLVMPointerType(LLVMInt32TypeInContext(gallivm
->context
), 0);
591 LLVMValueRef temp_ptr
;
592 temp_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, ivtype
, "");
593 scalar
= LLVMBuildLoad(builder
, temp_ptr
, "");
595 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
597 return lp_build_broadcast_scalar(bld_fetch
, scalar
);
602 emit_fetch_immediate(
603 struct lp_build_tgsi_context
* bld_base
,
604 const struct tgsi_full_src_register
* reg
,
605 enum tgsi_opcode_type stype
,
608 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
609 LLVMValueRef res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
612 if (stype
== TGSI_TYPE_UNSIGNED
) {
613 res
= LLVMConstBitCast(res
, bld_base
->uint_bld
.vec_type
);
614 } else if (stype
== TGSI_TYPE_SIGNED
) {
615 res
= LLVMConstBitCast(res
, bld_base
->int_bld
.vec_type
);
622 struct lp_build_tgsi_context
* bld_base
,
623 const struct tgsi_full_src_register
* reg
,
624 enum tgsi_opcode_type stype
,
627 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
628 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
629 LLVMBuilderRef builder
= gallivm
->builder
;
630 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
631 LLVMValueRef indirect_index
= NULL
;
634 if (reg
->Register
.Indirect
) {
635 indirect_index
= get_indirect_index(bld
,
641 if (reg
->Register
.Indirect
) {
642 LLVMValueRef swizzle_vec
=
643 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
644 LLVMValueRef length_vec
=
645 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
646 LLVMValueRef index_vec
; /* index into the const buffer */
647 LLVMValueRef inputs_array
;
648 LLVMTypeRef float4_ptr_type
;
650 /* index_vec = (indirect_index * 4 + swizzle) * length */
651 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
652 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
653 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
655 /* cast inputs_array pointer to float* */
656 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
657 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
,
658 float4_ptr_type
, "");
660 /* Gather values from the temporary register array */
661 res
= build_gather(&bld_base
->base
, inputs_array
, index_vec
);
663 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
664 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
665 reg
->Register
.Index
* 4 + swizzle
);
666 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
667 bld
->inputs_array
, &lindex
, 1, "");
668 res
= LLVMBuildLoad(builder
, input_ptr
, "");
671 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
679 emit_fetch_temporary(
680 struct lp_build_tgsi_context
* bld_base
,
681 const struct tgsi_full_src_register
* reg
,
682 enum tgsi_opcode_type stype
,
685 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
686 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
687 LLVMBuilderRef builder
= gallivm
->builder
;
688 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
689 LLVMValueRef indirect_index
= NULL
;
692 if (reg
->Register
.Indirect
) {
693 indirect_index
= get_indirect_index(bld
,
699 if (reg
->Register
.Indirect
) {
700 LLVMValueRef swizzle_vec
=
701 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
702 LLVMValueRef length_vec
=
703 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
,
704 bld
->bld_base
.base
.type
.length
);
705 LLVMValueRef index_vec
; /* index into the const buffer */
706 LLVMValueRef temps_array
;
707 LLVMTypeRef float4_ptr_type
;
709 /* index_vec = (indirect_index * 4 + swizzle) * length */
710 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
711 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
712 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
714 /* cast temps_array pointer to float* */
715 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(bld
->bld_base
.base
.gallivm
->context
), 0);
716 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
717 float4_ptr_type
, "");
719 /* Gather values from the temporary register array */
720 res
= build_gather(&bld_base
->base
, temps_array
, index_vec
);
723 LLVMValueRef temp_ptr
;
724 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
725 LLVMTypeRef itype
= LLVMPointerType(LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4), 0);
726 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
728 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, itype
, "");
730 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
731 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
733 return bld
->bld_base
.base
.undef
;
740 emit_fetch_system_value(
741 struct lp_build_tgsi_context
* bld_base
,
742 const struct tgsi_full_src_register
* reg
,
743 enum tgsi_opcode_type stype
,
746 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
747 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
748 LLVMBuilderRef builder
= gallivm
->builder
;
749 LLVMValueRef index
; /* index into the system value array */
750 LLVMValueRef scalar
, scalar_ptr
;
752 assert(!reg
->Register
.Indirect
);
754 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
* 4 + swizzle
);
756 scalar_ptr
= LLVMBuildGEP(builder
, bld
->system_values_array
, &index
, 1, "");
757 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
759 return lp_build_broadcast_scalar(&bld
->bld_base
.base
, scalar
);
763 * Register fetch with derivatives.
767 struct lp_build_tgsi_soa_context
*bld
,
776 /* TODO: use interpolation coeffs for inputs */
779 *ddx
= lp_build_ddx(&bld
->bld_base
.base
, src
);
782 *ddy
= lp_build_ddy(&bld
->bld_base
.base
, src
);
790 emit_fetch_predicate(
791 struct lp_build_tgsi_soa_context
*bld
,
792 const struct tgsi_full_instruction
*inst
,
795 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
797 unsigned char swizzles
[4];
798 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
802 if (!inst
->Instruction
.Predicate
) {
803 TGSI_FOR_EACH_CHANNEL( chan
) {
809 swizzles
[0] = inst
->Predicate
.SwizzleX
;
810 swizzles
[1] = inst
->Predicate
.SwizzleY
;
811 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
812 swizzles
[3] = inst
->Predicate
.SwizzleW
;
814 index
= inst
->Predicate
.Index
;
815 assert(index
< LP_MAX_TGSI_PREDS
);
817 TGSI_FOR_EACH_CHANNEL( chan
) {
818 unsigned swizzle
= swizzles
[chan
];
821 * Only fetch the predicate register channels that are actually listed
824 if (!unswizzled
[swizzle
]) {
825 value
= LLVMBuildLoad(builder
,
826 bld
->preds
[index
][swizzle
], "");
829 * Convert the value to an integer mask.
831 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
832 * is needlessly causing two comparisons due to storing the intermediate
833 * result as float vector instead of an integer mask vector.
835 value
= lp_build_compare(bld
->bld_base
.base
.gallivm
,
836 bld
->bld_base
.base
.type
,
839 bld
->bld_base
.base
.zero
);
840 if (inst
->Predicate
.Negate
) {
841 value
= LLVMBuildNot(builder
, value
, "");
844 unswizzled
[swizzle
] = value
;
846 value
= unswizzled
[swizzle
];
858 struct lp_build_tgsi_context
*bld_base
,
859 const struct tgsi_full_instruction
*inst
,
865 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
866 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
867 LLVMBuilderRef builder
= gallivm
->builder
;
868 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
869 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
870 LLVMValueRef indirect_index
= NULL
;
871 struct lp_build_context
*bld_store
;
872 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
876 case TGSI_TYPE_FLOAT
:
877 case TGSI_TYPE_UNTYPED
:
878 bld_store
= &bld_base
->base
;
880 case TGSI_TYPE_UNSIGNED
:
881 bld_store
= &bld_base
->uint_bld
;
883 case TGSI_TYPE_SIGNED
:
884 bld_store
= &bld_base
->int_bld
;
886 case TGSI_TYPE_DOUBLE
:
893 switch( inst
->Instruction
.Saturate
) {
897 case TGSI_SAT_ZERO_ONE
:
898 value
= lp_build_max(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.zero
);
899 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
902 case TGSI_SAT_MINUS_PLUS_ONE
:
903 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));
904 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
911 if (reg
->Register
.Indirect
) {
912 indirect_index
= get_indirect_index(bld
,
917 assert(reg
->Register
.Index
<=
918 bld
->bld_base
.info
->file_max
[reg
->Register
.File
]);
921 switch( reg
->Register
.File
) {
922 case TGSI_FILE_OUTPUT
:
923 if (reg
->Register
.Indirect
) {
924 LLVMValueRef chan_vec
=
925 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
926 LLVMValueRef length_vec
=
927 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
928 LLVMValueRef index_vec
; /* indexes into the temp registers */
929 LLVMValueRef outputs_array
;
930 LLVMValueRef pixel_offsets
;
931 LLVMTypeRef float_ptr_type
;
934 /* build pixel offset vector: {0, 1, 2, 3, ...} */
935 pixel_offsets
= uint_bld
->undef
;
936 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
937 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
938 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
942 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
943 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
944 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
945 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
946 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
949 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
950 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
,
953 /* Scatter store values into temp registers */
954 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
955 &bld
->exec_mask
, pred
);
958 LLVMValueRef out_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
960 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, out_ptr
);
964 case TGSI_FILE_TEMPORARY
:
965 if (reg
->Register
.Indirect
) {
966 LLVMValueRef chan_vec
=
967 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
968 LLVMValueRef length_vec
=
969 lp_build_const_int_vec(gallivm
, uint_bld
->type
,
970 bld
->bld_base
.base
.type
.length
);
971 LLVMValueRef index_vec
; /* indexes into the temp registers */
972 LLVMValueRef temps_array
;
973 LLVMValueRef pixel_offsets
;
974 LLVMTypeRef float_ptr_type
;
977 /* build pixel offset vector: {0, 1, 2, 3, ...} */
978 pixel_offsets
= uint_bld
->undef
;
979 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
980 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
981 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
985 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
986 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
987 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
988 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
989 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
992 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
993 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
996 /* Scatter store values into temp registers */
997 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
998 &bld
->exec_mask
, pred
);
1001 LLVMValueRef temp_ptr
;
1004 case TGSI_TYPE_UNSIGNED
:
1005 case TGSI_TYPE_SIGNED
: {
1006 LLVMTypeRef itype
= LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4);
1007 LLVMTypeRef ivtype
= LLVMPointerType(itype
, 0);
1008 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1010 LLVMValueRef temp_value_ptr
;
1012 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, ivtype
, "");
1013 temp_value_ptr
= LLVMBuildBitCast(builder
, value
, itype
, "");
1014 value
= temp_value_ptr
;
1018 case TGSI_TYPE_FLOAT
:
1019 case TGSI_TYPE_UNTYPED
:
1020 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1025 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, temp_ptr
);
1029 case TGSI_FILE_ADDRESS
:
1030 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1031 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1034 case TGSI_FILE_PREDICATE
:
1035 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1036 bld
->preds
[reg
->Register
.Index
][chan_index
]);
1046 struct lp_build_tgsi_context
* bld_base
,
1047 const struct tgsi_full_instruction
* inst
,
1048 const struct tgsi_opcode_info
* info
,
1049 LLVMValueRef dst
[4])
1052 unsigned chan_index
;
1053 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1056 LLVMValueRef pred
[TGSI_NUM_CHANNELS
];
1058 emit_fetch_predicate( bld
, inst
, pred
);
1060 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1061 emit_store_chan(bld_base
, inst
, 0, chan_index
, pred
[chan_index
], dst
[chan_index
]);
1067 * High-level instruction translators.
1071 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
1072 const struct tgsi_full_instruction
*inst
,
1073 enum lp_build_tex_modifier modifier
,
1074 LLVMValueRef
*texel
)
1076 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1078 LLVMValueRef lod_bias
, explicit_lod
;
1079 LLVMValueRef oow
= NULL
;
1080 LLVMValueRef coords
[3];
1081 LLVMValueRef ddx
[3];
1082 LLVMValueRef ddy
[3];
1083 unsigned num_coords
;
1086 if (!bld
->sampler
) {
1087 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1088 for (i
= 0; i
< 4; i
++) {
1089 texel
[i
] = bld
->bld_base
.base
.undef
;
1094 switch (inst
->Texture
.Texture
) {
1095 case TGSI_TEXTURE_1D
:
1098 case TGSI_TEXTURE_1D_ARRAY
:
1099 case TGSI_TEXTURE_2D
:
1100 case TGSI_TEXTURE_RECT
:
1103 case TGSI_TEXTURE_SHADOW1D
:
1104 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1105 case TGSI_TEXTURE_SHADOW2D
:
1106 case TGSI_TEXTURE_SHADOWRECT
:
1107 case TGSI_TEXTURE_2D_ARRAY
:
1108 case TGSI_TEXTURE_3D
:
1109 case TGSI_TEXTURE_CUBE
:
1112 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1120 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
1121 lod_bias
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1122 explicit_lod
= NULL
;
1124 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
1126 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1130 explicit_lod
= NULL
;
1133 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
1134 oow
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1135 oow
= lp_build_rcp(&bld
->bld_base
.base
, oow
);
1138 for (i
= 0; i
< num_coords
; i
++) {
1139 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1140 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
1141 coords
[i
] = lp_build_mul(&bld
->bld_base
.base
, coords
[i
], oow
);
1143 for (i
= num_coords
; i
< 3; i
++) {
1144 coords
[i
] = bld
->bld_base
.base
.undef
;
1147 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
1148 LLVMValueRef index0
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, 0);
1149 for (i
= 0; i
< num_coords
; i
++) {
1150 LLVMValueRef src1
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 1, i
);
1151 LLVMValueRef src2
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 2, i
);
1152 ddx
[i
] = LLVMBuildExtractElement(builder
, src1
, index0
, "");
1153 ddy
[i
] = LLVMBuildExtractElement(builder
, src2
, index0
, "");
1155 unit
= inst
->Src
[3].Register
.Index
;
1157 for (i
= 0; i
< num_coords
; i
++) {
1158 ddx
[i
] = lp_build_scalar_ddx( &bld
->bld_base
.base
, coords
[i
] );
1159 ddy
[i
] = lp_build_scalar_ddy( &bld
->bld_base
.base
, coords
[i
] );
1161 unit
= inst
->Src
[1].Register
.Index
;
1163 for (i
= num_coords
; i
< 3; i
++) {
1164 ddx
[i
] = LLVMGetUndef(bld
->bld_base
.base
.elem_type
);
1165 ddy
[i
] = LLVMGetUndef(bld
->bld_base
.base
.elem_type
);
1168 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1169 bld
->bld_base
.base
.gallivm
,
1170 bld
->bld_base
.base
.type
,
1171 unit
, num_coords
, coords
,
1173 lod_bias
, explicit_lod
,
1178 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
1183 for (i
= 0; i
< 5; i
++) {
1186 if (pc
+ i
>= bld
->bld_base
.info
->num_instructions
)
1189 opcode
= bld
->bld_base
.instructions
[pc
+ i
].Instruction
.Opcode
;
1191 if (opcode
== TGSI_OPCODE_END
)
1194 if (opcode
== TGSI_OPCODE_TEX
||
1195 opcode
== TGSI_OPCODE_TXP
||
1196 opcode
== TGSI_OPCODE_TXD
||
1197 opcode
== TGSI_OPCODE_TXB
||
1198 opcode
== TGSI_OPCODE_TXL
||
1199 opcode
== TGSI_OPCODE_TXF
||
1200 opcode
== TGSI_OPCODE_TXQ
||
1201 opcode
== TGSI_OPCODE_CAL
||
1202 opcode
== TGSI_OPCODE_CALLNZ
||
1203 opcode
== TGSI_OPCODE_IF
||
1204 opcode
== TGSI_OPCODE_IFC
||
1205 opcode
== TGSI_OPCODE_BGNLOOP
||
1206 opcode
== TGSI_OPCODE_SWITCH
)
1216 * Kill fragment if any of the src register values are negative.
1220 struct lp_build_tgsi_soa_context
*bld
,
1221 const struct tgsi_full_instruction
*inst
,
1224 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1225 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1226 LLVMValueRef terms
[TGSI_NUM_CHANNELS
];
1228 unsigned chan_index
;
1230 memset(&terms
, 0, sizeof terms
);
1232 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1235 /* Unswizzle channel */
1236 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
1238 /* Check if the component has not been already tested. */
1239 assert(swizzle
< TGSI_NUM_CHANNELS
);
1240 if( !terms
[swizzle
] )
1241 /* TODO: change the comparison operator instead of setting the sign */
1242 terms
[swizzle
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, chan_index
);
1246 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1247 if(terms
[chan_index
]) {
1248 LLVMValueRef chan_mask
;
1251 * If term < 0 then mask = 0 else mask = ~0.
1253 chan_mask
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->bld_base
.base
.zero
);
1256 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
1263 lp_build_mask_update(bld
->mask
, mask
);
1265 if (!near_end_of_shader(bld
, pc
))
1266 lp_build_mask_check(bld
->mask
);
1272 * Predicated fragment kill.
1273 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1274 * The only predication is the execution mask which will apply if
1275 * we're inside a loop or conditional.
1278 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
1281 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1284 /* For those channels which are "alive", disable fragment shader
1287 if (bld
->exec_mask
.has_mask
) {
1288 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1291 LLVMValueRef zero
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
1295 lp_build_mask_update(bld
->mask
, mask
);
1297 if (!near_end_of_shader(bld
, pc
))
1298 lp_build_mask_check(bld
->mask
);
1303 * Emit code which will dump the value of all the temporary registers
1307 emit_dump_temps(struct lp_build_tgsi_soa_context
*bld
)
1309 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1310 LLVMBuilderRef builder
= gallivm
->builder
;
1311 LLVMValueRef temp_ptr
;
1312 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
1313 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
1314 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
1315 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
1317 int n
= bld
->bld_base
.info
->file_max
[TGSI_FILE_TEMPORARY
];
1319 for (index
= 0; index
< n
; index
++) {
1320 LLVMValueRef idx
= lp_build_const_int32(gallivm
, index
);
1321 LLVMValueRef v
[4][4], res
;
1324 lp_build_printf(gallivm
, "TEMP[%d]:\n", idx
);
1326 for (chan
= 0; chan
< 4; chan
++) {
1327 temp_ptr
= lp_get_temp_ptr_soa(bld
, index
, chan
);
1328 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1329 v
[chan
][0] = LLVMBuildExtractElement(builder
, res
, i0
, "");
1330 v
[chan
][1] = LLVMBuildExtractElement(builder
, res
, i1
, "");
1331 v
[chan
][2] = LLVMBuildExtractElement(builder
, res
, i2
, "");
1332 v
[chan
][3] = LLVMBuildExtractElement(builder
, res
, i3
, "");
1335 lp_build_printf(gallivm
, " X: %f %f %f %f\n",
1336 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
1337 lp_build_printf(gallivm
, " Y: %f %f %f %f\n",
1338 v
[1][0], v
[1][1], v
[1][2], v
[1][3]);
1339 lp_build_printf(gallivm
, " Z: %f %f %f %f\n",
1340 v
[2][0], v
[2][1], v
[2][2], v
[2][3]);
1341 lp_build_printf(gallivm
, " W: %f %f %f %f\n",
1342 v
[3][0], v
[3][1], v
[3][2], v
[3][3]);
1349 lp_emit_declaration_soa(
1350 struct lp_build_tgsi_context
*bld_base
,
1351 const struct tgsi_full_declaration
*decl
)
1353 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1354 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1355 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
1356 const unsigned first
= decl
->Range
.First
;
1357 const unsigned last
= decl
->Range
.Last
;
1360 for (idx
= first
; idx
<= last
; ++idx
) {
1361 assert(last
<= bld
->bld_base
.info
->file_max
[decl
->Declaration
.File
]);
1362 switch (decl
->Declaration
.File
) {
1363 case TGSI_FILE_TEMPORARY
:
1364 assert(idx
< LP_MAX_TGSI_TEMPS
);
1365 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
1366 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1367 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
1371 case TGSI_FILE_OUTPUT
:
1372 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
1373 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1374 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
1375 vec_type
, "output");
1379 case TGSI_FILE_ADDRESS
:
1380 assert(idx
< LP_MAX_TGSI_ADDRS
);
1381 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1382 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, bld_base
->base
.int_vec_type
, "addr");
1385 case TGSI_FILE_PREDICATE
:
1386 assert(idx
< LP_MAX_TGSI_PREDS
);
1387 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1388 bld
->preds
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
,
1393 /* don't need to declare other vars */
1400 void lp_emit_immediate_soa(
1401 struct lp_build_tgsi_context
*bld_base
,
1402 const struct tgsi_full_immediate
*imm
)
1404 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1405 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1407 /* simply copy the immediate values into the next immediates[] slot */
1409 const uint size
= imm
->Immediate
.NrTokens
- 1;
1411 assert(bld
->num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1413 for( i
= 0; i
< size
; ++i
)
1414 bld
->immediates
[bld
->num_immediates
][i
] =
1415 lp_build_const_vec(gallivm
, bld_base
->base
.type
, imm
->u
[i
].Float
);
1417 for( i
= size
; i
< 4; ++i
)
1418 bld
->immediates
[bld
->num_immediates
][i
] = bld_base
->base
.undef
;
1420 bld
->num_immediates
++;
1425 const struct lp_build_tgsi_action
* action
,
1426 struct lp_build_tgsi_context
* bld_base
,
1427 struct lp_build_emit_data
* emit_data
)
1429 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1431 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
,
1432 &emit_data
->output
[emit_data
->chan
], NULL
);
1437 const struct lp_build_tgsi_action
* action
,
1438 struct lp_build_tgsi_context
* bld_base
,
1439 struct lp_build_emit_data
* emit_data
)
1441 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1443 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
, NULL
,
1444 &emit_data
->output
[emit_data
->chan
]);
1449 const struct lp_build_tgsi_action
* action
,
1450 struct lp_build_tgsi_context
* bld_base
,
1451 struct lp_build_emit_data
* emit_data
)
1453 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1455 emit_kilp(bld
, bld_base
->pc
- 1);
1460 const struct lp_build_tgsi_action
* action
,
1461 struct lp_build_tgsi_context
* bld_base
,
1462 struct lp_build_emit_data
* emit_data
)
1464 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1466 emit_kil(bld
, emit_data
->inst
, bld_base
->pc
- 1);
1471 const struct lp_build_tgsi_action
* action
,
1472 struct lp_build_tgsi_context
* bld_base
,
1473 struct lp_build_emit_data
* emit_data
)
1475 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1477 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
, emit_data
->output
);
1482 const struct lp_build_tgsi_action
* action
,
1483 struct lp_build_tgsi_context
* bld_base
,
1484 struct lp_build_emit_data
* emit_data
)
1486 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1488 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
1494 const struct lp_build_tgsi_action
* action
,
1495 struct lp_build_tgsi_context
* bld_base
,
1496 struct lp_build_emit_data
* emit_data
)
1498 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1500 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
1506 const struct lp_build_tgsi_action
* action
,
1507 struct lp_build_tgsi_context
* bld_base
,
1508 struct lp_build_emit_data
* emit_data
)
1510 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1512 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
1518 const struct lp_build_tgsi_action
* action
,
1519 struct lp_build_tgsi_context
* bld_base
,
1520 struct lp_build_emit_data
* emit_data
)
1522 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1524 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_PROJECTED
,
1530 const struct lp_build_tgsi_action
* action
,
1531 struct lp_build_tgsi_context
* bld_base
,
1532 struct lp_build_emit_data
* emit_data
)
1534 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1536 lp_exec_mask_call(&bld
->exec_mask
, emit_data
->inst
->Label
.Label
,
1542 const struct lp_build_tgsi_action
* action
,
1543 struct lp_build_tgsi_context
* bld_base
,
1544 struct lp_build_emit_data
* emit_data
)
1546 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1548 lp_exec_mask_ret(&bld
->exec_mask
, &bld_base
->pc
);
1553 const struct lp_build_tgsi_action
* action
,
1554 struct lp_build_tgsi_context
* bld_base
,
1555 struct lp_build_emit_data
* emit_data
)
1557 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1559 lp_exec_break(&bld
->exec_mask
);
1564 const struct lp_build_tgsi_action
* action
,
1565 struct lp_build_tgsi_context
* bld_base
,
1566 struct lp_build_emit_data
* emit_data
)
1569 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1571 tmp
= lp_build_cmp(&bld_base
->base
, PIPE_FUNC_NOTEQUAL
,
1572 emit_data
->args
[0], bld
->bld_base
.base
.zero
);
1573 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
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 lp_exec_bgnloop(&bld
->exec_mask
);
1589 const struct lp_build_tgsi_action
* action
,
1590 struct lp_build_tgsi_context
* bld_base
,
1591 struct lp_build_emit_data
* emit_data
)
1593 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1595 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1600 const struct lp_build_tgsi_action
* action
,
1601 struct lp_build_tgsi_context
* bld_base
,
1602 struct lp_build_emit_data
* emit_data
)
1604 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1606 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1611 const struct lp_build_tgsi_action
* action
,
1612 struct lp_build_tgsi_context
* bld_base
,
1613 struct lp_build_emit_data
* emit_data
)
1615 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1617 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1622 const struct lp_build_tgsi_action
* action
,
1623 struct lp_build_tgsi_context
* bld_base
,
1624 struct lp_build_emit_data
* emit_data
)
1626 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1628 lp_exec_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
1633 const struct lp_build_tgsi_action
* action
,
1634 struct lp_build_tgsi_context
* bld_base
,
1635 struct lp_build_emit_data
* emit_data
)
1637 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1639 lp_exec_mask_endsub(&bld
->exec_mask
, &bld_base
->pc
);
1644 const struct lp_build_tgsi_action
* action
,
1645 struct lp_build_tgsi_context
* bld_base
,
1646 struct lp_build_emit_data
* emit_data
)
1648 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1650 lp_exec_continue(&bld
->exec_mask
);
1653 /* XXX: Refactor and move it to lp_bld_tgsi_action.c
1655 * XXX: What do the comments about xmm registers mean? Maybe they are left over
1656 * from old code, but there is no garauntee that LLVM will use those registers
1659 * XXX: There should be no calls to lp_build_emit_fetch in this function. This
1660 * should be handled by the emit_data->fetch_args function. */
1663 const struct lp_build_tgsi_action
* action
,
1664 struct lp_build_tgsi_context
* bld_base
,
1665 struct lp_build_emit_data
* emit_data
)
1667 LLVMValueRef tmp0
, tmp1
;
1668 LLVMValueRef tmp4
= NULL
;
1669 LLVMValueRef tmp5
= NULL
;
1670 LLVMValueRef tmp6
= NULL
;
1671 LLVMValueRef tmp7
= NULL
;
1672 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1674 uint dims
= (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1676 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) ||
1677 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
) ||
1678 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
) ||
1679 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 4)) {
1681 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1684 /* xmm0 = src.x * src.x */
1685 tmp0
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
1686 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
1689 tmp0
= lp_build_mul( &bld
->bld_base
.base
, tmp0
, tmp0
);
1692 /* xmm0 = xmm0 + src.y * src.y */
1693 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Y
);
1694 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
1697 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1698 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1701 /* xmm0 = xmm0 + src.z * src.z */
1702 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Z
);
1703 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
1706 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1707 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1711 /* xmm0 = xmm0 + src.w * src.w */
1712 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1713 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
)) {
1716 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
1717 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
1719 /* xmm1 = 1 / sqrt(xmm0) */
1720 tmp1
= lp_build_rsqrt( &bld
->bld_base
.base
, tmp0
);
1721 /* dst.x = xmm1 * src.x */
1722 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
1723 emit_data
->output
[TGSI_CHAN_X
] = lp_build_mul( &bld
->bld_base
.base
, tmp4
, tmp1
);
1725 /* dst.y = xmm1 * src.y */
1726 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
1727 emit_data
->output
[TGSI_CHAN_Y
] = lp_build_mul( &bld
->bld_base
.base
, tmp5
, tmp1
);
1730 /* dst.z = xmm1 * src.z */
1731 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
1732 emit_data
->output
[TGSI_CHAN_Z
] = lp_build_mul( &bld
->bld_base
.base
, tmp6
, tmp1
);
1734 /* dst.w = xmm1 * src.w */
1735 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) && dims
== 4) {
1736 emit_data
->output
[TGSI_CHAN_W
] = lp_build_mul( &bld
->bld_base
.base
, tmp7
, tmp1
);
1741 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 3) {
1742 emit_data
->output
[TGSI_CHAN_W
] = bld
->bld_base
.base
.one
;
1746 static void emit_prologue(struct lp_build_tgsi_context
* bld_base
)
1748 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1749 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1751 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
1752 LLVMValueRef array_size
=
1753 lp_build_const_int32(gallivm
,
1754 bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
1755 bld
->temps_array
= lp_build_array_alloca(gallivm
,
1756 bld_base
->base
.vec_type
, array_size
,
1760 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
1761 LLVMValueRef array_size
=
1762 lp_build_const_int32(gallivm
,
1763 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
1764 bld
->outputs_array
= lp_build_array_alloca(gallivm
,
1765 bld_base
->base
.vec_type
, array_size
,
1769 /* If we have indirect addressing in inputs we need to copy them into
1770 * our alloca array to be able to iterate over them */
1771 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
1772 unsigned index
, chan
;
1773 LLVMTypeRef vec_type
= bld_base
->base
.vec_type
;
1774 LLVMValueRef array_size
= lp_build_const_int32(gallivm
,
1775 bld_base
->info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
1776 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
1777 vec_type
, array_size
,
1780 assert(bld_base
->info
->num_inputs
1781 <= bld_base
->info
->file_max
[TGSI_FILE_INPUT
] + 1);
1783 for (index
= 0; index
< bld_base
->info
->num_inputs
; ++index
) {
1784 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1785 LLVMValueRef lindex
=
1786 lp_build_const_int32(gallivm
, index
* 4 + chan
);
1787 LLVMValueRef input_ptr
=
1788 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
1790 LLVMValueRef value
= bld
->inputs
[index
][chan
];
1792 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
1798 static void emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
1800 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1804 emit_dump_temps(bld
);
1807 /* If we have indirect addressing in outputs we need to copy our alloca array
1808 * to the outputs slots specified by the called */
1809 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
1810 unsigned index
, chan
;
1811 assert(bld_base
->info
->num_outputs
<=
1812 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
1813 for (index
= 0; index
< bld_base
->info
->num_outputs
; ++index
) {
1814 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
1815 bld
->outputs
[index
][chan
] = lp_get_output_ptr(bld
, index
, chan
);
1822 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
1823 const struct tgsi_token
*tokens
,
1824 struct lp_type type
,
1825 struct lp_build_mask_context
*mask
,
1826 LLVMValueRef consts_ptr
,
1827 LLVMValueRef system_values_array
,
1828 const LLVMValueRef
*pos
,
1829 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
1830 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1831 struct lp_build_sampler_soa
*sampler
,
1832 const struct tgsi_shader_info
*info
)
1834 struct lp_build_tgsi_soa_context bld
;
1836 struct lp_type res_type
;
1838 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
1839 memset(&res_type
, 0, sizeof res_type
);
1840 res_type
.width
= type
.width
;
1841 res_type
.length
= type
.length
;
1844 /* Setup build context */
1845 memset(&bld
, 0, sizeof bld
);
1846 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
1847 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
1848 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
1849 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
1852 bld
.inputs
= inputs
;
1853 bld
.outputs
= outputs
;
1854 bld
.consts_ptr
= consts_ptr
;
1855 bld
.sampler
= sampler
;
1856 bld
.bld_base
.info
= info
;
1857 bld
.indirect_files
= info
->indirect_files
;
1859 bld
.bld_base
.soa
= TRUE
;
1860 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
1861 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
1862 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
1863 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
1864 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = emit_fetch_system_value
;
1865 bld
.bld_base
.emit_store
= emit_store
;
1867 bld
.bld_base
.emit_declaration
= lp_emit_declaration_soa
;
1868 bld
.bld_base
.emit_immediate
= lp_emit_immediate_soa
;
1870 bld
.bld_base
.emit_prologue
= emit_prologue
;
1871 bld
.bld_base
.emit_epilogue
= emit_epilogue
;
1873 /* Set opcode actions */
1874 lp_set_default_actions_cpu(&bld
.bld_base
);
1876 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1877 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNSUB
].emit
= bgnsub_emit
;
1878 bld
.bld_base
.op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1879 bld
.bld_base
.op_actions
[TGSI_OPCODE_CAL
].emit
= cal_emit
;
1880 bld
.bld_base
.op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1881 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDX
].emit
= ddx_emit
;
1882 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDY
].emit
= ddy_emit
;
1883 bld
.bld_base
.op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1884 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1885 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1886 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSUB
].emit
= endsub_emit
;
1887 bld
.bld_base
.op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1888 bld
.bld_base
.op_actions
[TGSI_OPCODE_KIL
].emit
= kil_emit
;
1889 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILP
].emit
= kilp_emit
;
1890 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM
].emit
= nrm_emit
;
1891 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM4
].emit
= nrm_emit
;
1892 bld
.bld_base
.op_actions
[TGSI_OPCODE_RET
].emit
= ret_emit
;
1893 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX
].emit
= tex_emit
;
1894 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB
].emit
= txb_emit
;
1895 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXD
].emit
= txd_emit
;
1896 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL
].emit
= txl_emit
;
1897 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXP
].emit
= txp_emit
;
1899 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.base
);
1902 bld
.system_values_array
= system_values_array
;
1904 lp_build_tgsi_llvm(&bld
.bld_base
, tokens
);
1907 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
1908 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
1909 debug_printf("11111111111111111111111111111 \n");
1910 tgsi_dump(tokens
, 0);
1911 lp_debug_dump_value(function
);
1912 debug_printf("2222222222222222222222222222 \n");
1916 LLVMModuleRef module
= LLVMGetGlobalParent(
1917 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
1918 LLVMDumpModule(module
);
1925 * Build up the system values array out of individual values such as
1926 * the instance ID, front-face, primitive ID, etc. The shader info is
1927 * used to determine which system values are needed and where to put
1928 * them in the system values array.
1930 * XXX only instance ID is implemented at this time.
1932 * The system values register file is similar to the constants buffer.
1933 * Example declaration:
1934 * DCL SV[0], INSTANCEID
1935 * Example instruction:
1936 * MOVE foo, SV[0].xxxx;
1938 * \return LLVM float array (interpreted as float [][4])
1941 lp_build_system_values_array(struct gallivm_state
*gallivm
,
1942 const struct tgsi_shader_info
*info
,
1943 LLVMValueRef instance_id
,
1944 LLVMValueRef facing
)
1946 LLVMValueRef size
= lp_build_const_int32(gallivm
, 4 * info
->num_system_values
);
1947 LLVMTypeRef float_t
= LLVMFloatTypeInContext(gallivm
->context
);
1948 LLVMValueRef array
= lp_build_array_alloca(gallivm
, float_t
,
1949 size
, "sysvals_array");
1952 for (i
= 0; i
< info
->num_system_values
; i
++) {
1953 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
* 4);
1954 LLVMValueRef ptr
, value
= 0;
1956 switch (info
->system_value_semantic_name
[i
]) {
1957 case TGSI_SEMANTIC_INSTANCEID
:
1958 /* convert instance ID from int to float */
1959 value
= LLVMBuildSIToFP(gallivm
->builder
, instance_id
, float_t
,
1960 "sysval_instanceid");
1962 case TGSI_SEMANTIC_FACE
:
1965 assert(0 && "unexpected semantic in build_system_values_array()");
1968 ptr
= LLVMBuildGEP(gallivm
->builder
, array
, &index
, 1, "");
1969 LLVMBuildStore(gallivm
->builder
, value
, ptr
);