1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * TGSI to LLVM IR translation -- SoA.
33 * @author Jose Fonseca <jfonseca@vmware.com>
35 * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
36 * Brian Paul, and others.
39 #include "pipe/p_config.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "util/u_debug.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "tgsi/tgsi_dump.h"
45 #include "tgsi/tgsi_exec.h"
46 #include "tgsi/tgsi_info.h"
47 #include "tgsi/tgsi_parse.h"
48 #include "tgsi/tgsi_util.h"
49 #include "tgsi/tgsi_scan.h"
50 #include "lp_bld_tgsi_action.h"
51 #include "lp_bld_type.h"
52 #include "lp_bld_const.h"
53 #include "lp_bld_arit.h"
54 #include "lp_bld_bitarit.h"
55 #include "lp_bld_gather.h"
56 #include "lp_bld_init.h"
57 #include "lp_bld_logic.h"
58 #include "lp_bld_swizzle.h"
59 #include "lp_bld_flow.h"
60 #include "lp_bld_quad.h"
61 #include "lp_bld_tgsi.h"
62 #include "lp_bld_limits.h"
63 #include "lp_bld_debug.h"
64 #include "lp_bld_printf.h"
65 #include "lp_bld_sample.h"
66 #include "lp_bld_struct.h"
69 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
71 LLVMTypeRef int_type
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
72 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
75 mask
->has_mask
= FALSE
;
76 mask
->cond_stack_size
= 0;
77 mask
->loop_stack_size
= 0;
78 mask
->call_stack_size
= 0;
80 mask
->int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, mask
->bld
->type
);
81 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
82 LLVMConstAllOnes(mask
->int_vec_type
);
84 mask
->loop_limiter
= lp_build_alloca(bld
->gallivm
, int_type
, "looplimiter");
88 LLVMConstInt(int_type
, LP_MAX_TGSI_LOOP_ITERATIONS
, false),
92 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
94 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
96 if (mask
->loop_stack_size
) {
97 /*for loops we need to update the entire mask at runtime */
99 assert(mask
->break_mask
);
100 tmp
= LLVMBuildAnd(builder
,
104 mask
->exec_mask
= LLVMBuildAnd(builder
,
109 mask
->exec_mask
= mask
->cond_mask
;
111 if (mask
->call_stack_size
) {
112 mask
->exec_mask
= LLVMBuildAnd(builder
,
118 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
119 mask
->loop_stack_size
> 0 ||
120 mask
->call_stack_size
> 0);
123 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
126 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
128 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
129 if (mask
->cond_stack_size
== 0) {
130 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
132 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
133 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
134 mask
->cond_mask
= LLVMBuildAnd(builder
,
138 lp_exec_mask_update(mask
);
141 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
143 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
144 LLVMValueRef prev_mask
;
145 LLVMValueRef inv_mask
;
147 assert(mask
->cond_stack_size
);
148 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
149 if (mask
->cond_stack_size
== 1) {
150 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
153 inv_mask
= LLVMBuildNot(builder
, mask
->cond_mask
, "");
155 mask
->cond_mask
= LLVMBuildAnd(builder
,
158 lp_exec_mask_update(mask
);
161 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
163 assert(mask
->cond_stack_size
);
164 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
165 lp_exec_mask_update(mask
);
168 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
170 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
172 if (mask
->loop_stack_size
== 0) {
173 assert(mask
->loop_block
== NULL
);
174 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
175 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
176 assert(mask
->break_var
== NULL
);
179 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
181 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
182 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
183 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
184 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
185 ++mask
->loop_stack_size
;
187 mask
->break_var
= lp_build_alloca(mask
->bld
->gallivm
, mask
->int_vec_type
, "");
188 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
190 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->gallivm
, "bgnloop");
192 LLVMBuildBr(builder
, mask
->loop_block
);
193 LLVMPositionBuilderAtEnd(builder
, mask
->loop_block
);
195 mask
->break_mask
= LLVMBuildLoad(builder
, mask
->break_var
, "");
197 lp_exec_mask_update(mask
);
200 static void lp_exec_break(struct lp_exec_mask
*mask
)
202 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
203 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
207 mask
->break_mask
= LLVMBuildAnd(builder
,
209 exec_mask
, "break_full");
211 lp_exec_mask_update(mask
);
214 static void lp_exec_continue(struct lp_exec_mask
*mask
)
216 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
217 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
221 mask
->cont_mask
= LLVMBuildAnd(builder
,
225 lp_exec_mask_update(mask
);
229 static void lp_exec_endloop(struct gallivm_state
*gallivm
,
230 struct lp_exec_mask
*mask
)
232 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
233 LLVMBasicBlockRef endloop
;
234 LLVMTypeRef int_type
= LLVMInt32TypeInContext(mask
->bld
->gallivm
->context
);
235 LLVMTypeRef reg_type
= LLVMIntTypeInContext(gallivm
->context
,
236 mask
->bld
->type
.width
*
237 mask
->bld
->type
.length
);
238 LLVMValueRef i1cond
, i2cond
, icond
, limiter
;
240 assert(mask
->break_mask
);
243 * Restore the cont_mask, but don't pop
245 assert(mask
->loop_stack_size
);
246 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
247 lp_exec_mask_update(mask
);
250 * Unlike the continue mask, the break_mask must be preserved across loop
253 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
255 /* Decrement the loop limiter */
256 limiter
= LLVMBuildLoad(builder
, mask
->loop_limiter
, "");
258 limiter
= LLVMBuildSub(
261 LLVMConstInt(int_type
, 1, false),
264 LLVMBuildStore(builder
, limiter
, mask
->loop_limiter
);
266 /* i1cond = (mask != 0) */
267 i1cond
= LLVMBuildICmp(
270 LLVMBuildBitCast(builder
, mask
->exec_mask
, reg_type
, ""),
271 LLVMConstNull(reg_type
), "");
273 /* i2cond = (looplimiter > 0) */
274 i2cond
= LLVMBuildICmp(
278 LLVMConstNull(int_type
), "");
280 /* if( i1cond && i2cond ) */
281 icond
= LLVMBuildAnd(builder
, i1cond
, i2cond
, "");
283 endloop
= lp_build_insert_new_block(mask
->bld
->gallivm
, "endloop");
285 LLVMBuildCondBr(builder
,
286 icond
, mask
->loop_block
, endloop
);
288 LLVMPositionBuilderAtEnd(builder
, endloop
);
290 assert(mask
->loop_stack_size
);
291 --mask
->loop_stack_size
;
292 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
293 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
294 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
295 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
297 lp_exec_mask_update(mask
);
300 /* stores val into an address pointed to by dst.
301 * mask->exec_mask is used to figure out which bits of val
302 * should be stored into the address
303 * (0 means don't store this bit, 1 means do store).
305 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
306 struct lp_build_context
*bld_store
,
311 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
313 /* Mix the predicate and execution mask */
314 if (mask
->has_mask
) {
316 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
318 pred
= mask
->exec_mask
;
323 LLVMValueRef real_val
, dst_val
;
325 dst_val
= LLVMBuildLoad(builder
, dst
, "");
326 real_val
= lp_build_select(bld_store
,
330 LLVMBuildStore(builder
, real_val
, dst
);
332 LLVMBuildStore(builder
, val
, dst
);
335 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
339 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
340 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
341 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
342 mask
->call_stack_size
++;
346 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
348 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
349 LLVMValueRef exec_mask
;
351 if (mask
->call_stack_size
== 0) {
352 /* returning from main() */
356 exec_mask
= LLVMBuildNot(builder
,
360 mask
->ret_mask
= LLVMBuildAnd(builder
,
362 exec_mask
, "ret_full");
364 lp_exec_mask_update(mask
);
367 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
371 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
373 assert(mask
->call_stack_size
);
374 mask
->call_stack_size
--;
375 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
376 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
377 lp_exec_mask_update(mask
);
382 * Return pointer to a temporary register channel (src or dest).
383 * Note that indirect addressing cannot be handled here.
384 * \param index which temporary register
385 * \param chan which channel of the temp register.
388 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context
*bld
,
392 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
394 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
395 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, index
* 4 + chan
);
396 return LLVMBuildGEP(builder
, bld
->temps_array
, &lindex
, 1, "");
399 return bld
->temps
[index
][chan
];
404 * Return pointer to a output register channel (src or dest).
405 * Note that indirect addressing cannot be handled here.
406 * \param index which output register
407 * \param chan which channel of the output register.
410 lp_get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
414 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
416 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
417 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
,
419 return LLVMBuildGEP(builder
, bld
->outputs_array
, &lindex
, 1, "");
422 return bld
->outputs
[index
][chan
];
428 * XXX the lp_build_gather() function should be capable of doing this
429 * with a little work.
432 build_gather(struct lp_build_context
*bld
,
433 LLVMValueRef base_ptr
,
434 LLVMValueRef indexes
)
436 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
437 LLVMValueRef res
= bld
->undef
;
441 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
443 for (i
= 0; i
< bld
->type
.length
; i
++) {
444 LLVMValueRef ii
= lp_build_const_int32(bld
->gallivm
, i
);
445 LLVMValueRef index
= LLVMBuildExtractElement(builder
,
447 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
448 &index
, 1, "gather_ptr");
449 LLVMValueRef scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
451 res
= LLVMBuildInsertElement(builder
, res
, scalar
, ii
, "");
459 * Scatter/store vector.
462 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
463 LLVMValueRef base_ptr
,
464 LLVMValueRef indexes
,
466 struct lp_exec_mask
*mask
,
469 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
470 LLVMBuilderRef builder
= gallivm
->builder
;
473 /* Mix the predicate and execution mask */
474 if (mask
->has_mask
) {
476 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
479 pred
= mask
->exec_mask
;
484 * Loop over elements of index_vec, store scalar value.
486 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
487 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
488 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
489 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
490 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
491 LLVMValueRef scalar_pred
= pred
?
492 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
495 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
496 ii
, val
, index
, scalar_ptr
);
499 LLVMValueRef real_val
, dst_val
;
500 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
501 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
502 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
505 LLVMBuildStore(builder
, val
, scalar_ptr
);
512 * Read the current value of the ADDR register, convert the floats to
513 * ints, add the base index and return the vector of offsets.
514 * The offsets will be used to index into the constant buffer or
515 * temporary register file.
518 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
519 unsigned reg_file
, unsigned reg_index
,
520 const struct tgsi_src_register
*indirect_reg
)
522 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
523 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
524 /* always use X component of address register */
525 unsigned swizzle
= indirect_reg
->SwizzleX
;
528 LLVMValueRef max_index
;
531 assert(bld
->indirect_files
& (1 << reg_file
));
533 base
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, reg_index
);
536 rel
= LLVMBuildLoad(builder
,
537 bld
->addr
[indirect_reg
->Index
][swizzle
],
540 index
= lp_build_add(uint_bld
, base
, rel
);
542 max_index
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
,
544 bld
->bld_base
.info
->file_max
[reg_file
]);
546 assert(!uint_bld
->type
.sign
);
547 index
= lp_build_min(uint_bld
, index
, max_index
);
552 static struct lp_build_context
*
553 stype_to_fetch(struct lp_build_tgsi_context
* bld_base
,
554 enum tgsi_opcode_type stype
)
556 struct lp_build_context
*bld_fetch
;
559 case TGSI_TYPE_FLOAT
:
560 case TGSI_TYPE_UNTYPED
:
561 bld_fetch
= &bld_base
->base
;
563 case TGSI_TYPE_UNSIGNED
:
564 bld_fetch
= &bld_base
->uint_bld
;
566 case TGSI_TYPE_SIGNED
:
567 bld_fetch
= &bld_base
->int_bld
;
570 case TGSI_TYPE_DOUBLE
:
581 struct lp_build_tgsi_context
* bld_base
,
582 const struct tgsi_full_src_register
* reg
,
583 enum tgsi_opcode_type stype
,
586 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
587 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
588 LLVMBuilderRef builder
= gallivm
->builder
;
589 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
590 LLVMValueRef indirect_index
= NULL
;
591 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
592 unsigned dimension
= 0;
593 LLVMValueRef dimension_index
;
594 LLVMValueRef consts_ptr
;
596 /* XXX: Handle fetching xyzw components as a vector */
597 assert(swizzle
!= ~0);
599 if (reg
->Register
.Dimension
) {
600 assert(!reg
->Dimension
.Indirect
);
601 dimension
= reg
->Dimension
.Index
;
602 assert(dimension
< LP_MAX_TGSI_CONST_BUFFERS
);
605 dimension_index
= lp_build_const_int32(gallivm
, dimension
);
606 consts_ptr
= lp_build_array_get(gallivm
, bld
->consts_ptr
, dimension_index
);
608 if (reg
->Register
.Indirect
) {
609 indirect_index
= get_indirect_index(bld
,
615 if (reg
->Register
.Indirect
) {
616 LLVMValueRef swizzle_vec
=
617 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
618 LLVMValueRef index_vec
; /* index into the const buffer */
620 /* index_vec = indirect_index * 4 + swizzle */
621 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
622 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
624 /* Gather values from the constant buffer */
625 return build_gather(bld_fetch
, consts_ptr
, index_vec
);
628 LLVMValueRef index
; /* index into the const buffer */
629 LLVMValueRef scalar
, scalar_ptr
;
631 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
*4 + swizzle
);
633 scalar_ptr
= LLVMBuildGEP(builder
, consts_ptr
,
636 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
637 LLVMTypeRef ivtype
= LLVMPointerType(LLVMInt32TypeInContext(gallivm
->context
), 0);
638 LLVMValueRef temp_ptr
;
639 temp_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, ivtype
, "");
640 scalar
= LLVMBuildLoad(builder
, temp_ptr
, "");
642 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
644 return lp_build_broadcast_scalar(bld_fetch
, scalar
);
649 emit_fetch_immediate(
650 struct lp_build_tgsi_context
* bld_base
,
651 const struct tgsi_full_src_register
* reg
,
652 enum tgsi_opcode_type stype
,
655 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
656 LLVMValueRef res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
659 if (stype
== TGSI_TYPE_UNSIGNED
) {
660 res
= LLVMConstBitCast(res
, bld_base
->uint_bld
.vec_type
);
661 } else if (stype
== TGSI_TYPE_SIGNED
) {
662 res
= LLVMConstBitCast(res
, bld_base
->int_bld
.vec_type
);
669 struct lp_build_tgsi_context
* bld_base
,
670 const struct tgsi_full_src_register
* reg
,
671 enum tgsi_opcode_type stype
,
674 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
675 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
676 LLVMBuilderRef builder
= gallivm
->builder
;
677 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
678 LLVMValueRef indirect_index
= NULL
;
681 if (reg
->Register
.Indirect
) {
682 indirect_index
= get_indirect_index(bld
,
688 if (reg
->Register
.Indirect
) {
689 LLVMValueRef swizzle_vec
=
690 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
691 LLVMValueRef length_vec
=
692 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
693 LLVMValueRef index_vec
; /* index into the const buffer */
694 LLVMValueRef inputs_array
;
695 LLVMTypeRef float4_ptr_type
;
697 /* index_vec = (indirect_index * 4 + swizzle) * length */
698 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
699 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
700 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
702 /* cast inputs_array pointer to float* */
703 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
704 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
,
705 float4_ptr_type
, "");
707 /* Gather values from the temporary register array */
708 res
= build_gather(&bld_base
->base
, inputs_array
, index_vec
);
710 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
711 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
712 reg
->Register
.Index
* 4 + swizzle
);
713 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
714 bld
->inputs_array
, &lindex
, 1, "");
715 res
= LLVMBuildLoad(builder
, input_ptr
, "");
718 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
724 if (stype
== TGSI_TYPE_UNSIGNED
) {
725 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
726 } else if (stype
== TGSI_TYPE_SIGNED
) {
727 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
734 emit_fetch_temporary(
735 struct lp_build_tgsi_context
* bld_base
,
736 const struct tgsi_full_src_register
* reg
,
737 enum tgsi_opcode_type stype
,
740 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
741 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
742 LLVMBuilderRef builder
= gallivm
->builder
;
743 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
744 LLVMValueRef indirect_index
= NULL
;
747 if (reg
->Register
.Indirect
) {
748 indirect_index
= get_indirect_index(bld
,
754 if (reg
->Register
.Indirect
) {
755 LLVMValueRef swizzle_vec
=
756 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, swizzle
);
757 LLVMValueRef length_vec
=
758 lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
,
759 bld
->bld_base
.base
.type
.length
);
760 LLVMValueRef index_vec
; /* index into the const buffer */
761 LLVMValueRef temps_array
;
762 LLVMTypeRef float4_ptr_type
;
764 /* index_vec = (indirect_index * 4 + swizzle) * length */
765 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
766 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
767 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
769 /* cast temps_array pointer to float* */
770 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(bld
->bld_base
.base
.gallivm
->context
), 0);
771 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
772 float4_ptr_type
, "");
774 /* Gather values from the temporary register array */
775 res
= build_gather(&bld_base
->base
, temps_array
, index_vec
);
778 LLVMValueRef temp_ptr
;
779 if (stype
!= TGSI_TYPE_FLOAT
&& stype
!= TGSI_TYPE_UNTYPED
) {
780 LLVMTypeRef itype
= LLVMPointerType(bld
->bld_base
.int_bld
.vec_type
, 0);
781 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
783 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, itype
, "");
785 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
786 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
788 return bld
->bld_base
.base
.undef
;
795 emit_fetch_system_value(
796 struct lp_build_tgsi_context
* bld_base
,
797 const struct tgsi_full_src_register
* reg
,
798 enum tgsi_opcode_type stype
,
801 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
802 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
803 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
804 LLVMBuilderRef builder
= gallivm
->builder
;
806 enum tgsi_opcode_type atype
; // Actual type of the value
808 assert(!reg
->Register
.Indirect
);
810 switch (info
->system_value_semantic_name
[reg
->Register
.Index
]) {
811 case TGSI_SEMANTIC_INSTANCEID
:
812 res
= lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.instance_id
);
813 atype
= TGSI_TYPE_UNSIGNED
;
816 case TGSI_SEMANTIC_VERTEXID
:
817 res
= bld
->system_values
.vertex_id
;
818 atype
= TGSI_TYPE_UNSIGNED
;
822 assert(!"unexpected semantic in emit_fetch_system_value");
823 res
= bld_base
->base
.zero
;
824 atype
= TGSI_TYPE_FLOAT
;
828 if (atype
!= stype
) {
829 if (stype
== TGSI_TYPE_FLOAT
) {
830 res
= LLVMBuildBitCast(builder
, res
, bld_base
->base
.vec_type
, "");
831 } else if (stype
== TGSI_TYPE_UNSIGNED
) {
832 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
833 } else if (stype
== TGSI_TYPE_SIGNED
) {
834 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
842 * Register fetch with derivatives.
846 struct lp_build_tgsi_soa_context
*bld
,
855 /* TODO: use interpolation coeffs for inputs */
858 *ddx
= lp_build_ddx(&bld
->bld_base
.base
, src
);
861 *ddy
= lp_build_ddy(&bld
->bld_base
.base
, src
);
869 emit_fetch_predicate(
870 struct lp_build_tgsi_soa_context
*bld
,
871 const struct tgsi_full_instruction
*inst
,
874 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
876 unsigned char swizzles
[4];
877 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
881 if (!inst
->Instruction
.Predicate
) {
882 TGSI_FOR_EACH_CHANNEL( chan
) {
888 swizzles
[0] = inst
->Predicate
.SwizzleX
;
889 swizzles
[1] = inst
->Predicate
.SwizzleY
;
890 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
891 swizzles
[3] = inst
->Predicate
.SwizzleW
;
893 index
= inst
->Predicate
.Index
;
894 assert(index
< LP_MAX_TGSI_PREDS
);
896 TGSI_FOR_EACH_CHANNEL( chan
) {
897 unsigned swizzle
= swizzles
[chan
];
900 * Only fetch the predicate register channels that are actually listed
903 if (!unswizzled
[swizzle
]) {
904 value
= LLVMBuildLoad(builder
,
905 bld
->preds
[index
][swizzle
], "");
908 * Convert the value to an integer mask.
910 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
911 * is needlessly causing two comparisons due to storing the intermediate
912 * result as float vector instead of an integer mask vector.
914 value
= lp_build_compare(bld
->bld_base
.base
.gallivm
,
915 bld
->bld_base
.base
.type
,
918 bld
->bld_base
.base
.zero
);
919 if (inst
->Predicate
.Negate
) {
920 value
= LLVMBuildNot(builder
, value
, "");
923 unswizzled
[swizzle
] = value
;
925 value
= unswizzled
[swizzle
];
937 struct lp_build_tgsi_context
*bld_base
,
938 const struct tgsi_full_instruction
*inst
,
944 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
945 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
946 LLVMBuilderRef builder
= gallivm
->builder
;
947 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
948 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
949 LLVMValueRef indirect_index
= NULL
;
950 struct lp_build_context
*bld_store
;
951 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
955 case TGSI_TYPE_FLOAT
:
956 case TGSI_TYPE_UNTYPED
:
957 bld_store
= &bld_base
->base
;
959 case TGSI_TYPE_UNSIGNED
:
960 bld_store
= &bld_base
->uint_bld
;
962 case TGSI_TYPE_SIGNED
:
963 bld_store
= &bld_base
->int_bld
;
965 case TGSI_TYPE_DOUBLE
:
972 switch( inst
->Instruction
.Saturate
) {
976 case TGSI_SAT_ZERO_ONE
:
977 value
= lp_build_max(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.zero
);
978 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
981 case TGSI_SAT_MINUS_PLUS_ONE
:
982 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));
983 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
990 if (reg
->Register
.Indirect
) {
991 indirect_index
= get_indirect_index(bld
,
996 assert(reg
->Register
.Index
<=
997 bld
->bld_base
.info
->file_max
[reg
->Register
.File
]);
1000 switch( reg
->Register
.File
) {
1001 case TGSI_FILE_OUTPUT
:
1002 if (reg
->Register
.Indirect
) {
1003 LLVMValueRef chan_vec
=
1004 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
1005 LLVMValueRef length_vec
=
1006 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->bld_base
.base
.type
.length
);
1007 LLVMValueRef index_vec
; /* indexes into the temp registers */
1008 LLVMValueRef outputs_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 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
,
1030 float_ptr_type
, "");
1032 /* Scatter store values into temp registers */
1033 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
1034 &bld
->exec_mask
, pred
);
1037 LLVMValueRef out_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
1039 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, out_ptr
);
1043 case TGSI_FILE_TEMPORARY
:
1044 if (reg
->Register
.Indirect
) {
1045 LLVMValueRef chan_vec
=
1046 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
1047 LLVMValueRef length_vec
=
1048 lp_build_const_int_vec(gallivm
, uint_bld
->type
,
1049 bld
->bld_base
.base
.type
.length
);
1050 LLVMValueRef index_vec
; /* indexes into the temp registers */
1051 LLVMValueRef temps_array
;
1052 LLVMValueRef pixel_offsets
;
1053 LLVMTypeRef float_ptr_type
;
1056 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1057 pixel_offsets
= uint_bld
->undef
;
1058 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
1059 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1060 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
1064 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1065 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1066 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1067 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1068 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1071 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1072 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
1073 float_ptr_type
, "");
1075 /* Scatter store values into temp registers */
1076 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
1077 &bld
->exec_mask
, pred
);
1080 LLVMValueRef temp_ptr
;
1083 case TGSI_TYPE_UNSIGNED
:
1084 case TGSI_TYPE_SIGNED
: {
1085 LLVMTypeRef itype
= bld_base
->int_bld
.vec_type
;
1086 LLVMTypeRef ivtype
= LLVMPointerType(itype
, 0);
1087 LLVMValueRef tint_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1089 LLVMValueRef temp_value_ptr
;
1091 temp_ptr
= LLVMBuildBitCast(builder
, tint_ptr
, ivtype
, "");
1092 temp_value_ptr
= LLVMBuildBitCast(builder
, value
, itype
, "");
1093 value
= temp_value_ptr
;
1097 case TGSI_TYPE_FLOAT
:
1098 case TGSI_TYPE_UNTYPED
:
1099 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
,
1104 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
, temp_ptr
);
1108 case TGSI_FILE_ADDRESS
:
1109 assert(dtype
== TGSI_TYPE_SIGNED
);
1110 assert(LLVMTypeOf(value
) == bld_base
->base
.int_vec_type
);
1111 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1112 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1115 case TGSI_FILE_PREDICATE
:
1116 lp_exec_mask_store(&bld
->exec_mask
, bld_store
, pred
, value
,
1117 bld
->preds
[reg
->Register
.Index
][chan_index
]);
1127 struct lp_build_tgsi_context
* bld_base
,
1128 const struct tgsi_full_instruction
* inst
,
1129 const struct tgsi_opcode_info
* info
,
1130 LLVMValueRef dst
[4])
1133 unsigned chan_index
;
1134 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1137 LLVMValueRef pred
[TGSI_NUM_CHANNELS
];
1139 emit_fetch_predicate( bld
, inst
, pred
);
1141 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1142 emit_store_chan(bld_base
, inst
, 0, chan_index
, pred
[chan_index
], dst
[chan_index
]);
1148 * High-level instruction translators.
1152 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
1153 const struct tgsi_full_instruction
*inst
,
1154 enum lp_build_tex_modifier modifier
,
1155 LLVMValueRef
*texel
)
1157 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1158 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1160 LLVMValueRef lod_bias
, explicit_lod
;
1161 LLVMValueRef oow
= NULL
;
1162 LLVMValueRef coords
[4];
1163 LLVMValueRef offsets
[3] = { NULL
};
1164 struct lp_derivatives derivs
;
1165 unsigned num_coords
;
1169 if (!bld
->sampler
) {
1170 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1171 for (i
= 0; i
< 4; i
++) {
1172 texel
[i
] = bld
->bld_base
.base
.undef
;
1177 derivs
.ddx_ddy
[0] = bld
->bld_base
.base
.undef
;
1178 derivs
.ddx_ddy
[1] = bld
->bld_base
.base
.undef
;
1180 switch (inst
->Texture
.Texture
) {
1181 case TGSI_TEXTURE_1D
:
1185 case TGSI_TEXTURE_1D_ARRAY
:
1189 case TGSI_TEXTURE_2D
:
1190 case TGSI_TEXTURE_RECT
:
1194 case TGSI_TEXTURE_SHADOW1D
:
1195 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1199 case TGSI_TEXTURE_SHADOW2D
:
1200 case TGSI_TEXTURE_SHADOWRECT
:
1201 case TGSI_TEXTURE_2D_ARRAY
:
1202 case TGSI_TEXTURE_CUBE
:
1206 case TGSI_TEXTURE_3D
:
1210 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1211 case TGSI_TEXTURE_SHADOWCUBE
:
1220 /* Note lod and especially projected are illegal in a LOT of cases */
1221 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
1222 assert(num_coords
< 4);
1223 lod_bias
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1224 explicit_lod
= NULL
;
1226 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
1227 assert(num_coords
< 4);
1229 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1233 explicit_lod
= NULL
;
1236 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
1237 assert(num_coords
< 4);
1238 oow
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1239 oow
= lp_build_rcp(&bld
->bld_base
.base
, oow
);
1242 for (i
= 0; i
< num_coords
; i
++) {
1243 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1244 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
1245 coords
[i
] = lp_build_mul(&bld
->bld_base
.base
, coords
[i
], oow
);
1247 for (i
= num_coords
; i
< 4; i
++) {
1248 coords
[i
] = bld
->bld_base
.base
.undef
;
1251 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
1252 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1253 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
1254 LLVMValueRef ddxdyonec
[3];
1255 unsigned length
= bld
->bld_base
.base
.type
.length
;
1256 unsigned num_quads
= length
/ 4;
1260 for (dim
= 0; dim
< dims
; ++dim
) {
1261 LLVMValueRef srcx
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 1, dim
);
1262 LLVMValueRef srcy
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 2, dim
);
1263 for (quad
= 0; quad
< num_quads
; ++quad
) {
1264 unsigned s1
= 4*quad
;
1265 unsigned s2
= 4*quad
+ length
;
1266 shuffles
[4*quad
+ 0] = lp_build_const_int32(gallivm
, s1
);
1267 shuffles
[4*quad
+ 1] = lp_build_const_int32(gallivm
, s2
);
1268 shuffles
[4*quad
+ 2] = i32undef
;
1269 shuffles
[4*quad
+ 3] = i32undef
;
1271 ddxdyonec
[dim
] = LLVMBuildShuffleVector(builder
, srcx
, srcy
,
1272 LLVMConstVector(shuffles
, length
), "");
1275 derivs
.ddx_ddy
[0] = ddxdyonec
[0];
1277 else if (dims
>= 2) {
1278 for (quad
= 0; quad
< num_quads
; ++quad
) {
1279 unsigned s1
= 4*quad
;
1280 unsigned s2
= 4*quad
+ length
;
1281 shuffles
[4*quad
+ 0] = lp_build_const_int32(gallivm
, s1
);
1282 shuffles
[4*quad
+ 1] = lp_build_const_int32(gallivm
, s1
+ 1);
1283 shuffles
[4*quad
+ 2] = lp_build_const_int32(gallivm
, s2
);
1284 shuffles
[4*quad
+ 3] = lp_build_const_int32(gallivm
, s2
+ 1);
1286 derivs
.ddx_ddy
[0] = LLVMBuildShuffleVector(builder
, ddxdyonec
[0], ddxdyonec
[1],
1287 LLVMConstVector(shuffles
, length
), "");
1289 derivs
.ddx_ddy
[1] = ddxdyonec
[2];
1292 unit
= inst
->Src
[3].Register
.Index
;
1295 derivs
.ddx_ddy
[0] = lp_build_packed_ddx_ddy_onecoord(&bld
->bld_base
.base
, coords
[0]);
1297 else if (dims
>= 2) {
1298 derivs
.ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(&bld
->bld_base
.base
,
1299 coords
[0], coords
[1]);
1301 derivs
.ddx_ddy
[1] = lp_build_packed_ddx_ddy_onecoord(&bld
->bld_base
.base
, coords
[2]);
1304 unit
= inst
->Src
[1].Register
.Index
;
1307 /* some advanced gather instructions (txgo) would require 4 offsets */
1308 if (inst
->Texture
.NumOffsets
== 1) {
1310 for (dim
= 0; dim
< dims
; dim
++) {
1311 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
1315 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1316 bld
->bld_base
.base
.gallivm
,
1317 bld
->bld_base
.base
.type
,
1322 lod_bias
, explicit_lod
,
1327 emit_txf( struct lp_build_tgsi_soa_context
*bld
,
1328 const struct tgsi_full_instruction
*inst
,
1329 LLVMValueRef
*texel
)
1332 LLVMValueRef coord_undef
= LLVMGetUndef(bld
->bld_base
.base
.int_vec_type
);
1333 LLVMValueRef explicit_lod
= NULL
;
1334 LLVMValueRef coords
[3];
1335 LLVMValueRef offsets
[3] = { NULL
};
1336 struct lp_derivatives derivs
;
1337 unsigned num_coords
;
1341 if (!bld
->sampler
) {
1342 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1343 for (i
= 0; i
< 4; i
++) {
1344 texel
[i
] = coord_undef
;
1349 derivs
.ddx_ddy
[0] = coord_undef
;
1350 derivs
.ddx_ddy
[1] = coord_undef
;
1352 switch (inst
->Texture
.Texture
) {
1353 case TGSI_TEXTURE_1D
:
1354 case TGSI_TEXTURE_BUFFER
:
1358 case TGSI_TEXTURE_1D_ARRAY
:
1362 case TGSI_TEXTURE_2D
:
1363 case TGSI_TEXTURE_RECT
:
1367 case TGSI_TEXTURE_2D_ARRAY
:
1371 case TGSI_TEXTURE_3D
:
1380 /* always have lod except for buffers ? */
1381 if (inst
->Texture
.Texture
!= TGSI_TEXTURE_BUFFER
) {
1382 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 3 );
1385 for (i
= 0; i
< num_coords
; i
++) {
1386 coords
[i
] = lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, i
);
1388 for (i
= num_coords
; i
< 3; i
++) {
1389 coords
[i
] = coord_undef
;
1392 unit
= inst
->Src
[1].Register
.Index
;
1394 if (inst
->Texture
.NumOffsets
== 1) {
1396 for (dim
= 0; dim
< dims
; dim
++) {
1397 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
1401 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1402 bld
->bld_base
.base
.gallivm
,
1403 bld
->bld_base
.base
.type
,
1413 emit_txq( struct lp_build_tgsi_soa_context
*bld
,
1414 const struct tgsi_full_instruction
*inst
,
1415 LLVMValueRef
*sizes_out
)
1417 LLVMValueRef explicit_lod
;
1418 unsigned num_coords
, has_lod
;
1421 switch (inst
->Texture
.Texture
) {
1422 case TGSI_TEXTURE_1D
:
1423 case TGSI_TEXTURE_SHADOW1D
:
1427 case TGSI_TEXTURE_2D
:
1428 case TGSI_TEXTURE_SHADOW2D
:
1429 case TGSI_TEXTURE_CUBE
:
1430 case TGSI_TEXTURE_SHADOWCUBE
:
1431 case TGSI_TEXTURE_1D_ARRAY
:
1432 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1436 case TGSI_TEXTURE_3D
:
1437 // case TGSI_TEXTURE_CUBE_ARRAY:
1438 // case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1439 case TGSI_TEXTURE_2D_ARRAY
:
1440 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1445 case TGSI_TEXTURE_BUFFER
:
1450 case TGSI_TEXTURE_RECT
:
1451 case TGSI_TEXTURE_SHADOWRECT
:
1452 // case TGSI_TEXTURE_2D_MS:
1457 // case TGSI_TEXTURE_2D_MS_ARRAY:
1467 if (!bld
->sampler
) {
1468 _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
1469 for (i
= 0; i
< num_coords
; i
++)
1470 sizes_out
[i
] = bld
->bld_base
.base
.undef
;
1475 explicit_lod
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0, 2 );
1477 explicit_lod
= NULL
;
1479 bld
->sampler
->emit_size_query(bld
->sampler
,
1480 bld
->bld_base
.base
.gallivm
,
1481 bld
->bld_base
.int_bld
.type
,
1482 inst
->Src
[1].Register
.Index
,
1488 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
1493 for (i
= 0; i
< 5; i
++) {
1496 if (pc
+ i
>= bld
->bld_base
.info
->num_instructions
)
1499 opcode
= bld
->bld_base
.instructions
[pc
+ i
].Instruction
.Opcode
;
1501 if (opcode
== TGSI_OPCODE_END
)
1504 if (opcode
== TGSI_OPCODE_TEX
||
1505 opcode
== TGSI_OPCODE_TXP
||
1506 opcode
== TGSI_OPCODE_TXD
||
1507 opcode
== TGSI_OPCODE_TXB
||
1508 opcode
== TGSI_OPCODE_TXL
||
1509 opcode
== TGSI_OPCODE_TXF
||
1510 opcode
== TGSI_OPCODE_TXQ
||
1511 opcode
== TGSI_OPCODE_CAL
||
1512 opcode
== TGSI_OPCODE_CALLNZ
||
1513 opcode
== TGSI_OPCODE_IF
||
1514 opcode
== TGSI_OPCODE_IFC
||
1515 opcode
== TGSI_OPCODE_BGNLOOP
||
1516 opcode
== TGSI_OPCODE_SWITCH
)
1526 * Kill fragment if any of the src register values are negative.
1530 struct lp_build_tgsi_soa_context
*bld
,
1531 const struct tgsi_full_instruction
*inst
,
1534 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1535 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1536 LLVMValueRef terms
[TGSI_NUM_CHANNELS
];
1538 unsigned chan_index
;
1540 memset(&terms
, 0, sizeof terms
);
1542 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1545 /* Unswizzle channel */
1546 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
1548 /* Check if the component has not been already tested. */
1549 assert(swizzle
< TGSI_NUM_CHANNELS
);
1550 if( !terms
[swizzle
] )
1551 /* TODO: change the comparison operator instead of setting the sign */
1552 terms
[swizzle
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, chan_index
);
1556 TGSI_FOR_EACH_CHANNEL( chan_index
) {
1557 if(terms
[chan_index
]) {
1558 LLVMValueRef chan_mask
;
1561 * If term < 0 then mask = 0 else mask = ~0.
1563 chan_mask
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->bld_base
.base
.zero
);
1566 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
1573 lp_build_mask_update(bld
->mask
, mask
);
1575 if (!near_end_of_shader(bld
, pc
))
1576 lp_build_mask_check(bld
->mask
);
1582 * Predicated fragment kill.
1583 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1584 * The only predication is the execution mask which will apply if
1585 * we're inside a loop or conditional.
1588 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
1591 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1594 /* For those channels which are "alive", disable fragment shader
1597 if (bld
->exec_mask
.has_mask
) {
1598 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1601 LLVMValueRef zero
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
1605 lp_build_mask_update(bld
->mask
, mask
);
1607 if (!near_end_of_shader(bld
, pc
))
1608 lp_build_mask_check(bld
->mask
);
1613 * Emit code which will dump the value of all the temporary registers
1617 emit_dump_temps(struct lp_build_tgsi_soa_context
*bld
)
1619 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1620 LLVMBuilderRef builder
= gallivm
->builder
;
1621 LLVMValueRef temp_ptr
;
1622 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
1623 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
1624 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
1625 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
1627 int n
= bld
->bld_base
.info
->file_max
[TGSI_FILE_TEMPORARY
];
1629 for (index
= 0; index
< n
; index
++) {
1630 LLVMValueRef idx
= lp_build_const_int32(gallivm
, index
);
1631 LLVMValueRef v
[4][4], res
;
1634 lp_build_printf(gallivm
, "TEMP[%d]:\n", idx
);
1636 for (chan
= 0; chan
< 4; chan
++) {
1637 temp_ptr
= lp_get_temp_ptr_soa(bld
, index
, chan
);
1638 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1639 v
[chan
][0] = LLVMBuildExtractElement(builder
, res
, i0
, "");
1640 v
[chan
][1] = LLVMBuildExtractElement(builder
, res
, i1
, "");
1641 v
[chan
][2] = LLVMBuildExtractElement(builder
, res
, i2
, "");
1642 v
[chan
][3] = LLVMBuildExtractElement(builder
, res
, i3
, "");
1645 lp_build_printf(gallivm
, " X: %f %f %f %f\n",
1646 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
1647 lp_build_printf(gallivm
, " Y: %f %f %f %f\n",
1648 v
[1][0], v
[1][1], v
[1][2], v
[1][3]);
1649 lp_build_printf(gallivm
, " Z: %f %f %f %f\n",
1650 v
[2][0], v
[2][1], v
[2][2], v
[2][3]);
1651 lp_build_printf(gallivm
, " W: %f %f %f %f\n",
1652 v
[3][0], v
[3][1], v
[3][2], v
[3][3]);
1659 lp_emit_declaration_soa(
1660 struct lp_build_tgsi_context
*bld_base
,
1661 const struct tgsi_full_declaration
*decl
)
1663 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1664 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1665 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
1666 const unsigned first
= decl
->Range
.First
;
1667 const unsigned last
= decl
->Range
.Last
;
1670 for (idx
= first
; idx
<= last
; ++idx
) {
1671 assert(last
<= bld
->bld_base
.info
->file_max
[decl
->Declaration
.File
]);
1672 switch (decl
->Declaration
.File
) {
1673 case TGSI_FILE_TEMPORARY
:
1674 assert(idx
< LP_MAX_TGSI_TEMPS
);
1675 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
1676 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1677 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
1681 case TGSI_FILE_OUTPUT
:
1682 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
1683 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1684 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
1685 vec_type
, "output");
1689 case TGSI_FILE_ADDRESS
:
1690 /* ADDR registers are the only allocated with an integer LLVM IR type,
1691 * as they are guaranteed to always have integers.
1692 * XXX: Not sure if this exception is worthwhile (or the whole idea of
1693 * an ADDR register for that matter).
1695 assert(idx
< LP_MAX_TGSI_ADDRS
);
1696 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1697 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, bld_base
->base
.int_vec_type
, "addr");
1700 case TGSI_FILE_PREDICATE
:
1701 assert(idx
< LP_MAX_TGSI_PREDS
);
1702 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
1703 bld
->preds
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
,
1708 /* don't need to declare other vars */
1715 void lp_emit_immediate_soa(
1716 struct lp_build_tgsi_context
*bld_base
,
1717 const struct tgsi_full_immediate
*imm
)
1719 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1720 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1722 /* simply copy the immediate values into the next immediates[] slot */
1724 const uint size
= imm
->Immediate
.NrTokens
- 1;
1726 assert(bld
->num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1727 switch (imm
->Immediate
.DataType
) {
1728 case TGSI_IMM_FLOAT32
:
1729 for( i
= 0; i
< size
; ++i
)
1730 bld
->immediates
[bld
->num_immediates
][i
] =
1731 lp_build_const_vec(gallivm
, bld_base
->base
.type
, imm
->u
[i
].Float
);
1734 case TGSI_IMM_UINT32
:
1735 for( i
= 0; i
< size
; ++i
) {
1736 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->uint_bld
.type
, imm
->u
[i
].Uint
);
1737 bld
->immediates
[bld
->num_immediates
][i
] =
1738 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1742 case TGSI_IMM_INT32
:
1743 for( i
= 0; i
< size
; ++i
) {
1744 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->int_bld
.type
, imm
->u
[i
].Int
);
1745 bld
->immediates
[bld
->num_immediates
][i
] =
1746 LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
1751 for( i
= size
; i
< 4; ++i
)
1752 bld
->immediates
[bld
->num_immediates
][i
] = bld_base
->base
.undef
;
1754 bld
->num_immediates
++;
1759 const struct lp_build_tgsi_action
* action
,
1760 struct lp_build_tgsi_context
* bld_base
,
1761 struct lp_build_emit_data
* emit_data
)
1763 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1765 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
,
1766 &emit_data
->output
[emit_data
->chan
], NULL
);
1771 const struct lp_build_tgsi_action
* action
,
1772 struct lp_build_tgsi_context
* bld_base
,
1773 struct lp_build_emit_data
* emit_data
)
1775 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1777 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
, NULL
,
1778 &emit_data
->output
[emit_data
->chan
]);
1783 const struct lp_build_tgsi_action
* action
,
1784 struct lp_build_tgsi_context
* bld_base
,
1785 struct lp_build_emit_data
* emit_data
)
1787 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1789 emit_kilp(bld
, bld_base
->pc
- 1);
1794 const struct lp_build_tgsi_action
* action
,
1795 struct lp_build_tgsi_context
* bld_base
,
1796 struct lp_build_emit_data
* emit_data
)
1798 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1800 emit_kil(bld
, emit_data
->inst
, bld_base
->pc
- 1);
1805 const struct lp_build_tgsi_action
* action
,
1806 struct lp_build_tgsi_context
* bld_base
,
1807 struct lp_build_emit_data
* emit_data
)
1809 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1811 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
, emit_data
->output
);
1816 const struct lp_build_tgsi_action
* action
,
1817 struct lp_build_tgsi_context
* bld_base
,
1818 struct lp_build_emit_data
* emit_data
)
1820 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1822 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
1828 const struct lp_build_tgsi_action
* action
,
1829 struct lp_build_tgsi_context
* bld_base
,
1830 struct lp_build_emit_data
* emit_data
)
1832 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1834 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
1840 const struct lp_build_tgsi_action
* action
,
1841 struct lp_build_tgsi_context
* bld_base
,
1842 struct lp_build_emit_data
* emit_data
)
1844 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1846 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
1852 const struct lp_build_tgsi_action
* action
,
1853 struct lp_build_tgsi_context
* bld_base
,
1854 struct lp_build_emit_data
* emit_data
)
1856 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1858 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_PROJECTED
,
1864 const struct lp_build_tgsi_action
* action
,
1865 struct lp_build_tgsi_context
* bld_base
,
1866 struct lp_build_emit_data
* emit_data
)
1868 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1870 emit_txq(bld
, emit_data
->inst
, emit_data
->output
);
1875 const struct lp_build_tgsi_action
* action
,
1876 struct lp_build_tgsi_context
* bld_base
,
1877 struct lp_build_emit_data
* emit_data
)
1879 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1881 emit_txf(bld
, emit_data
->inst
, emit_data
->output
);
1886 const struct lp_build_tgsi_action
* action
,
1887 struct lp_build_tgsi_context
* bld_base
,
1888 struct lp_build_emit_data
* emit_data
)
1890 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1892 lp_exec_mask_call(&bld
->exec_mask
, emit_data
->inst
->Label
.Label
,
1898 const struct lp_build_tgsi_action
* action
,
1899 struct lp_build_tgsi_context
* bld_base
,
1900 struct lp_build_emit_data
* emit_data
)
1902 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1904 lp_exec_mask_ret(&bld
->exec_mask
, &bld_base
->pc
);
1909 const struct lp_build_tgsi_action
* action
,
1910 struct lp_build_tgsi_context
* bld_base
,
1911 struct lp_build_emit_data
* emit_data
)
1913 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1915 lp_exec_break(&bld
->exec_mask
);
1920 const struct lp_build_tgsi_action
* action
,
1921 struct lp_build_tgsi_context
* bld_base
,
1922 struct lp_build_emit_data
* emit_data
)
1925 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1927 tmp
= lp_build_cmp(&bld_base
->base
, PIPE_FUNC_NOTEQUAL
,
1928 emit_data
->args
[0], bld
->bld_base
.base
.zero
);
1929 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
1934 const struct lp_build_tgsi_action
* action
,
1935 struct lp_build_tgsi_context
* bld_base
,
1936 struct lp_build_emit_data
* emit_data
)
1938 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1940 lp_exec_bgnloop(&bld
->exec_mask
);
1945 const struct lp_build_tgsi_action
* action
,
1946 struct lp_build_tgsi_context
* bld_base
,
1947 struct lp_build_emit_data
* emit_data
)
1949 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1951 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1956 const struct lp_build_tgsi_action
* action
,
1957 struct lp_build_tgsi_context
* bld_base
,
1958 struct lp_build_emit_data
* emit_data
)
1960 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1962 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1967 const struct lp_build_tgsi_action
* action
,
1968 struct lp_build_tgsi_context
* bld_base
,
1969 struct lp_build_emit_data
* emit_data
)
1971 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1973 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1978 const struct lp_build_tgsi_action
* action
,
1979 struct lp_build_tgsi_context
* bld_base
,
1980 struct lp_build_emit_data
* emit_data
)
1982 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1984 lp_exec_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
1989 const struct lp_build_tgsi_action
* action
,
1990 struct lp_build_tgsi_context
* bld_base
,
1991 struct lp_build_emit_data
* emit_data
)
1993 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1995 lp_exec_mask_endsub(&bld
->exec_mask
, &bld_base
->pc
);
2000 const struct lp_build_tgsi_action
* action
,
2001 struct lp_build_tgsi_context
* bld_base
,
2002 struct lp_build_emit_data
* emit_data
)
2004 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2006 lp_exec_continue(&bld
->exec_mask
);
2009 /* XXX: Refactor and move it to lp_bld_tgsi_action.c
2011 * XXX: What do the comments about xmm registers mean? Maybe they are left over
2012 * from old code, but there is no garauntee that LLVM will use those registers
2015 * XXX: There should be no calls to lp_build_emit_fetch in this function. This
2016 * should be handled by the emit_data->fetch_args function. */
2019 const struct lp_build_tgsi_action
* action
,
2020 struct lp_build_tgsi_context
* bld_base
,
2021 struct lp_build_emit_data
* emit_data
)
2023 LLVMValueRef tmp0
, tmp1
;
2024 LLVMValueRef tmp4
= NULL
;
2025 LLVMValueRef tmp5
= NULL
;
2026 LLVMValueRef tmp6
= NULL
;
2027 LLVMValueRef tmp7
= NULL
;
2028 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2030 uint dims
= (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
2032 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) ||
2033 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
) ||
2034 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
) ||
2035 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 4)) {
2037 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
2040 /* xmm0 = src.x * src.x */
2041 tmp0
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
2042 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
2045 tmp0
= lp_build_mul( &bld
->bld_base
.base
, tmp0
, tmp0
);
2048 /* xmm0 = xmm0 + src.y * src.y */
2049 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Y
);
2050 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
2053 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
2054 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
2057 /* xmm0 = xmm0 + src.z * src.z */
2058 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_Z
);
2059 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
2062 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
2063 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
2067 /* xmm0 = xmm0 + src.w * src.w */
2068 tmp1
= lp_build_emit_fetch(&bld
->bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
2069 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
)) {
2072 tmp1
= lp_build_mul( &bld
->bld_base
.base
, tmp1
, tmp1
);
2073 tmp0
= lp_build_add( &bld
->bld_base
.base
, tmp0
, tmp1
);
2075 /* xmm1 = 1 / sqrt(xmm0) */
2076 tmp1
= lp_build_rsqrt( &bld
->bld_base
.base
, tmp0
);
2077 /* dst.x = xmm1 * src.x */
2078 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
)) {
2079 emit_data
->output
[TGSI_CHAN_X
] = lp_build_mul( &bld
->bld_base
.base
, tmp4
, tmp1
);
2081 /* dst.y = xmm1 * src.y */
2082 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Y
)) {
2083 emit_data
->output
[TGSI_CHAN_Y
] = lp_build_mul( &bld
->bld_base
.base
, tmp5
, tmp1
);
2086 /* dst.z = xmm1 * src.z */
2087 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_Z
)) {
2088 emit_data
->output
[TGSI_CHAN_Z
] = lp_build_mul( &bld
->bld_base
.base
, tmp6
, tmp1
);
2090 /* dst.w = xmm1 * src.w */
2091 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_X
) && dims
== 4) {
2092 emit_data
->output
[TGSI_CHAN_W
] = lp_build_mul( &bld
->bld_base
.base
, tmp7
, tmp1
);
2097 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data
->inst
, TGSI_CHAN_W
) && dims
== 3) {
2098 emit_data
->output
[TGSI_CHAN_W
] = bld
->bld_base
.base
.one
;
2102 static void emit_prologue(struct lp_build_tgsi_context
* bld_base
)
2104 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2105 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2107 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
2108 LLVMValueRef array_size
=
2109 lp_build_const_int32(gallivm
,
2110 bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
2111 bld
->temps_array
= lp_build_array_alloca(gallivm
,
2112 bld_base
->base
.vec_type
, array_size
,
2116 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
2117 LLVMValueRef array_size
=
2118 lp_build_const_int32(gallivm
,
2119 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
2120 bld
->outputs_array
= lp_build_array_alloca(gallivm
,
2121 bld_base
->base
.vec_type
, array_size
,
2125 /* If we have indirect addressing in inputs we need to copy them into
2126 * our alloca array to be able to iterate over them */
2127 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
2128 unsigned index
, chan
;
2129 LLVMTypeRef vec_type
= bld_base
->base
.vec_type
;
2130 LLVMValueRef array_size
= lp_build_const_int32(gallivm
,
2131 bld_base
->info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
2132 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
2133 vec_type
, array_size
,
2136 assert(bld_base
->info
->num_inputs
2137 <= bld_base
->info
->file_max
[TGSI_FILE_INPUT
] + 1);
2139 for (index
= 0; index
< bld_base
->info
->num_inputs
; ++index
) {
2140 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
2141 LLVMValueRef lindex
=
2142 lp_build_const_int32(gallivm
, index
* 4 + chan
);
2143 LLVMValueRef input_ptr
=
2144 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
2146 LLVMValueRef value
= bld
->inputs
[index
][chan
];
2148 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
2154 static void emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
2156 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2160 emit_dump_temps(bld
);
2163 /* If we have indirect addressing in outputs we need to copy our alloca array
2164 * to the outputs slots specified by the called */
2165 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
2166 unsigned index
, chan
;
2167 assert(bld_base
->info
->num_outputs
<=
2168 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
2169 for (index
= 0; index
< bld_base
->info
->num_outputs
; ++index
) {
2170 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
2171 bld
->outputs
[index
][chan
] = lp_get_output_ptr(bld
, index
, chan
);
2178 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
2179 const struct tgsi_token
*tokens
,
2180 struct lp_type type
,
2181 struct lp_build_mask_context
*mask
,
2182 LLVMValueRef consts_ptr
,
2183 const struct lp_bld_tgsi_system_values
*system_values
,
2184 const LLVMValueRef
*pos
,
2185 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
2186 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
2187 struct lp_build_sampler_soa
*sampler
,
2188 const struct tgsi_shader_info
*info
)
2190 struct lp_build_tgsi_soa_context bld
;
2192 struct lp_type res_type
;
2194 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
2195 memset(&res_type
, 0, sizeof res_type
);
2196 res_type
.width
= type
.width
;
2197 res_type
.length
= type
.length
;
2200 /* Setup build context */
2201 memset(&bld
, 0, sizeof bld
);
2202 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
2203 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
2204 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
2205 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
2208 bld
.inputs
= inputs
;
2209 bld
.outputs
= outputs
;
2210 bld
.consts_ptr
= consts_ptr
;
2211 bld
.sampler
= sampler
;
2212 bld
.bld_base
.info
= info
;
2213 bld
.indirect_files
= info
->indirect_files
;
2215 bld
.bld_base
.soa
= TRUE
;
2216 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
2217 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
2218 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
2219 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
2220 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = emit_fetch_system_value
;
2221 bld
.bld_base
.emit_store
= emit_store
;
2223 bld
.bld_base
.emit_declaration
= lp_emit_declaration_soa
;
2224 bld
.bld_base
.emit_immediate
= lp_emit_immediate_soa
;
2226 bld
.bld_base
.emit_prologue
= emit_prologue
;
2227 bld
.bld_base
.emit_epilogue
= emit_epilogue
;
2229 /* Set opcode actions */
2230 lp_set_default_actions_cpu(&bld
.bld_base
);
2232 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
2233 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNSUB
].emit
= bgnsub_emit
;
2234 bld
.bld_base
.op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
2235 bld
.bld_base
.op_actions
[TGSI_OPCODE_CAL
].emit
= cal_emit
;
2236 bld
.bld_base
.op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
2237 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDX
].emit
= ddx_emit
;
2238 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDY
].emit
= ddy_emit
;
2239 bld
.bld_base
.op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
2240 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
2241 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
2242 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSUB
].emit
= endsub_emit
;
2243 bld
.bld_base
.op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
2244 bld
.bld_base
.op_actions
[TGSI_OPCODE_KIL
].emit
= kil_emit
;
2245 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILP
].emit
= kilp_emit
;
2246 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM
].emit
= nrm_emit
;
2247 bld
.bld_base
.op_actions
[TGSI_OPCODE_NRM4
].emit
= nrm_emit
;
2248 bld
.bld_base
.op_actions
[TGSI_OPCODE_RET
].emit
= ret_emit
;
2249 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX
].emit
= tex_emit
;
2250 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB
].emit
= txb_emit
;
2251 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXD
].emit
= txd_emit
;
2252 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL
].emit
= txl_emit
;
2253 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXP
].emit
= txp_emit
;
2254 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
2255 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXF
].emit
= txf_emit
;
2257 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.base
);
2259 bld
.system_values
= *system_values
;
2261 lp_build_tgsi_llvm(&bld
.bld_base
, tokens
);
2264 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
2265 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2266 debug_printf("11111111111111111111111111111 \n");
2267 tgsi_dump(tokens
, 0);
2268 lp_debug_dump_value(function
);
2269 debug_printf("2222222222222222222222222222 \n");
2273 LLVMModuleRef module
= LLVMGetGlobalParent(
2274 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
2275 LLVMDumpModule(module
);