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_info.h"
46 #include "tgsi/tgsi_parse.h"
47 #include "tgsi/tgsi_util.h"
48 #include "tgsi/tgsi_scan.h"
49 #include "lp_bld_type.h"
50 #include "lp_bld_const.h"
51 #include "lp_bld_arit.h"
52 #include "lp_bld_bitarit.h"
53 #include "lp_bld_gather.h"
54 #include "lp_bld_init.h"
55 #include "lp_bld_logic.h"
56 #include "lp_bld_swizzle.h"
57 #include "lp_bld_flow.h"
58 #include "lp_bld_quad.h"
59 #include "lp_bld_tgsi.h"
60 #include "lp_bld_limits.h"
61 #include "lp_bld_debug.h"
62 #include "lp_bld_printf.h"
65 #define FOR_EACH_CHANNEL( CHAN )\
66 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
68 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
69 ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
71 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
72 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
74 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
75 FOR_EACH_CHANNEL( CHAN )\
76 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
82 #define NUM_CHANNELS 4
84 #define LP_MAX_INSTRUCTIONS 256
88 struct lp_build_context
*bld
;
92 LLVMTypeRef int_vec_type
;
94 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
96 LLVMValueRef cond_mask
;
98 LLVMBasicBlockRef loop_block
;
99 LLVMValueRef cont_mask
;
100 LLVMValueRef break_mask
;
101 LLVMValueRef break_var
;
103 LLVMBasicBlockRef loop_block
;
104 LLVMValueRef cont_mask
;
105 LLVMValueRef break_mask
;
106 LLVMValueRef break_var
;
107 } loop_stack
[LP_MAX_TGSI_NESTING
];
110 LLVMValueRef ret_mask
;
113 LLVMValueRef ret_mask
;
114 } call_stack
[LP_MAX_TGSI_NESTING
];
117 LLVMValueRef exec_mask
;
120 struct lp_build_tgsi_soa_context
122 struct lp_build_context base
;
124 /* Builder for vector integer masks and indices */
125 struct lp_build_context uint_bld
;
127 /* Builder for scalar elements of shader's data type (float) */
128 struct lp_build_context elem_bld
;
130 LLVMValueRef consts_ptr
;
131 const LLVMValueRef
*pos
;
132 const LLVMValueRef (*inputs
)[NUM_CHANNELS
];
133 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
135 const struct lp_build_sampler_soa
*sampler
;
137 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][NUM_CHANNELS
];
138 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][NUM_CHANNELS
];
139 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][NUM_CHANNELS
];
140 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][NUM_CHANNELS
];
142 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
143 * set in the indirect_files field.
144 * The temps[] array above is unused then.
146 LLVMValueRef temps_array
;
148 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
149 * set in the indirect_files field.
150 * The outputs[] array above is unused then.
152 LLVMValueRef outputs_array
;
154 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
155 * set in the indirect_files field.
156 * The inputs[] array above is unused then.
158 LLVMValueRef inputs_array
;
160 LLVMValueRef system_values_array
;
162 const struct tgsi_shader_info
*info
;
163 /** bitmask indicating which register files are accessed indirectly */
164 unsigned indirect_files
;
166 struct lp_build_mask_context
*mask
;
167 struct lp_exec_mask exec_mask
;
169 struct tgsi_full_instruction
*instructions
;
170 uint max_instructions
;
173 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
176 mask
->has_mask
= FALSE
;
177 mask
->cond_stack_size
= 0;
178 mask
->loop_stack_size
= 0;
179 mask
->call_stack_size
= 0;
181 mask
->int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, mask
->bld
->type
);
182 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
183 LLVMConstAllOnes(mask
->int_vec_type
);
186 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
188 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
190 if (mask
->loop_stack_size
) {
191 /*for loops we need to update the entire mask at runtime */
193 assert(mask
->break_mask
);
194 tmp
= LLVMBuildAnd(builder
,
198 mask
->exec_mask
= LLVMBuildAnd(builder
,
203 mask
->exec_mask
= mask
->cond_mask
;
205 if (mask
->call_stack_size
) {
206 mask
->exec_mask
= LLVMBuildAnd(builder
,
212 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
213 mask
->loop_stack_size
> 0 ||
214 mask
->call_stack_size
> 0);
217 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
220 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
222 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
223 if (mask
->cond_stack_size
== 0) {
224 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
226 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
227 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
228 mask
->cond_mask
= LLVMBuildAnd(builder
,
232 lp_exec_mask_update(mask
);
235 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
237 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
238 LLVMValueRef prev_mask
;
239 LLVMValueRef inv_mask
;
241 assert(mask
->cond_stack_size
);
242 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
243 if (mask
->cond_stack_size
== 1) {
244 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
247 inv_mask
= LLVMBuildNot(builder
, mask
->cond_mask
, "");
249 mask
->cond_mask
= LLVMBuildAnd(builder
,
252 lp_exec_mask_update(mask
);
255 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
257 assert(mask
->cond_stack_size
);
258 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
259 lp_exec_mask_update(mask
);
262 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
264 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
266 if (mask
->loop_stack_size
== 0) {
267 assert(mask
->loop_block
== NULL
);
268 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
269 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
270 assert(mask
->break_var
== NULL
);
273 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
275 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
276 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
277 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
278 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
279 ++mask
->loop_stack_size
;
281 mask
->break_var
= lp_build_alloca(mask
->bld
->gallivm
, mask
->int_vec_type
, "");
282 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
284 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->gallivm
, "bgnloop");
285 LLVMBuildBr(builder
, mask
->loop_block
);
286 LLVMPositionBuilderAtEnd(builder
, mask
->loop_block
);
288 mask
->break_mask
= LLVMBuildLoad(builder
, mask
->break_var
, "");
290 lp_exec_mask_update(mask
);
293 static void lp_exec_break(struct lp_exec_mask
*mask
)
295 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
296 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
300 mask
->break_mask
= LLVMBuildAnd(builder
,
302 exec_mask
, "break_full");
304 lp_exec_mask_update(mask
);
307 static void lp_exec_continue(struct lp_exec_mask
*mask
)
309 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
310 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
314 mask
->cont_mask
= LLVMBuildAnd(builder
,
318 lp_exec_mask_update(mask
);
322 static void lp_exec_endloop(struct gallivm_state
*gallivm
,
323 struct lp_exec_mask
*mask
)
325 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
326 LLVMBasicBlockRef endloop
;
327 LLVMTypeRef reg_type
= LLVMIntTypeInContext(gallivm
->context
,
328 mask
->bld
->type
.width
*
329 mask
->bld
->type
.length
);
332 assert(mask
->break_mask
);
335 * Restore the cont_mask, but don't pop
337 assert(mask
->loop_stack_size
);
338 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
339 lp_exec_mask_update(mask
);
342 * Unlike the continue mask, the break_mask must be preserved across loop
345 LLVMBuildStore(builder
, mask
->break_mask
, mask
->break_var
);
347 /* i1cond = (mask == 0) */
348 i1cond
= LLVMBuildICmp(
351 LLVMBuildBitCast(builder
, mask
->exec_mask
, reg_type
, ""),
352 LLVMConstNull(reg_type
), "");
354 endloop
= lp_build_insert_new_block(mask
->bld
->gallivm
, "endloop");
356 LLVMBuildCondBr(builder
,
357 i1cond
, mask
->loop_block
, endloop
);
359 LLVMPositionBuilderAtEnd(builder
, endloop
);
361 assert(mask
->loop_stack_size
);
362 --mask
->loop_stack_size
;
363 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
364 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
365 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
366 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
368 lp_exec_mask_update(mask
);
371 /* stores val into an address pointed to by dst.
372 * mask->exec_mask is used to figure out which bits of val
373 * should be stored into the address
374 * (0 means don't store this bit, 1 means do store).
376 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
381 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
383 /* Mix the predicate and execution mask */
384 if (mask
->has_mask
) {
386 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
388 pred
= mask
->exec_mask
;
393 LLVMValueRef real_val
, dst_val
;
395 dst_val
= LLVMBuildLoad(builder
, dst
, "");
396 real_val
= lp_build_select(mask
->bld
,
400 LLVMBuildStore(builder
, real_val
, dst
);
402 LLVMBuildStore(builder
, val
, dst
);
405 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
409 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
410 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
411 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
412 mask
->call_stack_size
++;
416 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
418 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
419 LLVMValueRef exec_mask
;
421 if (mask
->call_stack_size
== 0) {
422 /* returning from main() */
426 exec_mask
= LLVMBuildNot(builder
,
430 mask
->ret_mask
= LLVMBuildAnd(builder
,
432 exec_mask
, "ret_full");
434 lp_exec_mask_update(mask
);
437 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
441 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
443 assert(mask
->call_stack_size
);
444 mask
->call_stack_size
--;
445 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
446 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
447 lp_exec_mask_update(mask
);
452 * Return pointer to a temporary register channel (src or dest).
453 * Note that indirect addressing cannot be handled here.
454 * \param index which temporary register
455 * \param chan which channel of the temp register.
458 get_temp_ptr(struct lp_build_tgsi_soa_context
*bld
,
462 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
464 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
465 LLVMValueRef lindex
= lp_build_const_int32(bld
->base
.gallivm
, index
* 4 + chan
);
466 return LLVMBuildGEP(builder
, bld
->temps_array
, &lindex
, 1, "");
469 return bld
->temps
[index
][chan
];
474 * Return pointer to a output register channel (src or dest).
475 * Note that indirect addressing cannot be handled here.
476 * \param index which output register
477 * \param chan which channel of the output register.
480 get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
484 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
486 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
487 LLVMValueRef lindex
= lp_build_const_int32(bld
->base
.gallivm
,
489 return LLVMBuildGEP(builder
, bld
->outputs_array
, &lindex
, 1, "");
492 return bld
->outputs
[index
][chan
];
498 * XXX the lp_build_gather() function should be capable of doing this
499 * with a little work.
502 build_gather(struct lp_build_tgsi_soa_context
*bld
,
503 LLVMValueRef base_ptr
,
504 LLVMValueRef indexes
)
506 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
507 LLVMValueRef res
= bld
->base
.undef
;
511 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
513 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
514 LLVMValueRef ii
= lp_build_const_int32(bld
->base
.gallivm
, i
);
515 LLVMValueRef index
= LLVMBuildExtractElement(builder
,
517 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
518 &index
, 1, "gather_ptr");
519 LLVMValueRef scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
521 res
= LLVMBuildInsertElement(builder
, res
, scalar
, ii
, "");
529 * Scatter/store vector.
532 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
533 LLVMValueRef base_ptr
,
534 LLVMValueRef indexes
,
536 struct lp_exec_mask
*mask
,
539 struct gallivm_state
*gallivm
= bld
->base
.gallivm
;
540 LLVMBuilderRef builder
= gallivm
->builder
;
543 /* Mix the predicate and execution mask */
544 if (mask
->has_mask
) {
546 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
549 pred
= mask
->exec_mask
;
554 * Loop over elements of index_vec, store scalar value.
556 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
557 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
558 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
559 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
560 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
561 LLVMValueRef scalar_pred
= pred
?
562 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
565 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
566 ii
, val
, index
, scalar_ptr
);
569 LLVMValueRef real_val
, dst_val
;
570 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
571 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
572 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
575 LLVMBuildStore(builder
, val
, scalar_ptr
);
582 * Read the current value of the ADDR register, convert the floats to
583 * ints, add the base index and return the vector of offsets.
584 * The offsets will be used to index into the constant buffer or
585 * temporary register file.
588 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
589 unsigned reg_file
, unsigned reg_index
,
590 const struct tgsi_src_register
*indirect_reg
)
592 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
593 struct lp_build_context
*uint_bld
= &bld
->uint_bld
;
594 /* always use X component of address register */
595 unsigned swizzle
= indirect_reg
->SwizzleX
;
598 LLVMValueRef max_index
;
601 assert(bld
->indirect_files
& (1 << reg_file
));
603 base
= lp_build_const_int_vec(bld
->base
.gallivm
, uint_bld
->type
, reg_index
);
606 rel
= LLVMBuildLoad(builder
,
607 bld
->addr
[indirect_reg
->Index
][swizzle
],
610 /* for indexing we want integers */
611 rel
= LLVMBuildFPToSI(builder
,
613 uint_bld
->vec_type
, "");
615 index
= lp_build_add(uint_bld
, base
, rel
);
617 max_index
= lp_build_const_int_vec(bld
->base
.gallivm
,
619 bld
->info
->file_max
[reg_file
]);
621 assert(!uint_bld
->type
.sign
);
622 index
= lp_build_min(uint_bld
, index
, max_index
);
633 struct lp_build_tgsi_soa_context
*bld
,
634 const struct tgsi_full_instruction
*inst
,
636 const unsigned chan_index
)
638 struct gallivm_state
*gallivm
= bld
->base
.gallivm
;
639 LLVMBuilderRef builder
= gallivm
->builder
;
640 struct lp_build_context
*uint_bld
= &bld
->uint_bld
;
641 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
642 const unsigned swizzle
=
643 tgsi_util_get_full_src_register_swizzle(reg
, chan_index
);
645 LLVMValueRef indirect_index
= NULL
;
648 assert(0 && "invalid swizzle in emit_fetch()");
649 return bld
->base
.undef
;
652 if (reg
->Register
.Indirect
) {
653 indirect_index
= get_indirect_index(bld
,
658 assert(reg
->Register
.Index
<= bld
->info
->file_max
[reg
->Register
.File
]);
661 switch (reg
->Register
.File
) {
662 case TGSI_FILE_CONSTANT
:
663 if (reg
->Register
.Indirect
) {
664 LLVMValueRef swizzle_vec
=
665 lp_build_const_int_vec(bld
->base
.gallivm
, uint_bld
->type
, swizzle
);
666 LLVMValueRef index_vec
; /* index into the const buffer */
668 /* index_vec = indirect_index * 4 + swizzle */
669 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
670 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
672 /* Gather values from the constant buffer */
673 res
= build_gather(bld
, bld
->consts_ptr
, index_vec
);
676 LLVMValueRef index
; /* index into the const buffer */
677 LLVMValueRef scalar
, scalar_ptr
;
679 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
*4 + swizzle
);
681 scalar_ptr
= LLVMBuildGEP(builder
, bld
->consts_ptr
,
683 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
685 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
689 case TGSI_FILE_IMMEDIATE
:
690 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
694 case TGSI_FILE_INPUT
:
695 if (reg
->Register
.Indirect
) {
696 LLVMValueRef swizzle_vec
=
697 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
698 LLVMValueRef length_vec
=
699 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->base
.type
.length
);
700 LLVMValueRef index_vec
; /* index into the const buffer */
701 LLVMValueRef inputs_array
;
702 LLVMTypeRef float4_ptr_type
;
704 /* index_vec = (indirect_index * 4 + swizzle) * length */
705 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
706 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
707 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
709 /* cast inputs_array pointer to float* */
710 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
711 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
,
712 float4_ptr_type
, "");
714 /* Gather values from the temporary register array */
715 res
= build_gather(bld
, inputs_array
, index_vec
);
717 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
718 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
719 reg
->Register
.Index
* 4 + swizzle
);
720 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
721 bld
->inputs_array
, &lindex
, 1, "");
722 res
= LLVMBuildLoad(builder
, input_ptr
, "");
725 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
731 case TGSI_FILE_TEMPORARY
:
732 if (reg
->Register
.Indirect
) {
733 LLVMValueRef swizzle_vec
=
734 lp_build_const_int_vec(bld
->base
.gallivm
, uint_bld
->type
, swizzle
);
735 LLVMValueRef length_vec
=
736 lp_build_const_int_vec(bld
->base
.gallivm
, uint_bld
->type
,
737 bld
->base
.type
.length
);
738 LLVMValueRef index_vec
; /* index into the const buffer */
739 LLVMValueRef temps_array
;
740 LLVMTypeRef float4_ptr_type
;
742 /* index_vec = (indirect_index * 4 + swizzle) * length */
743 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
744 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
745 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
747 /* cast temps_array pointer to float* */
748 float4_ptr_type
= LLVMPointerType(LLVMFloatTypeInContext(bld
->base
.gallivm
->context
), 0);
749 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
750 float4_ptr_type
, "");
752 /* Gather values from the temporary register array */
753 res
= build_gather(bld
, temps_array
, index_vec
);
756 LLVMValueRef temp_ptr
;
757 temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
, swizzle
);
758 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
760 return bld
->base
.undef
;
764 case TGSI_FILE_SYSTEM_VALUE
:
765 assert(!reg
->Register
.Indirect
);
767 LLVMValueRef index
; /* index into the system value array */
768 LLVMValueRef scalar
, scalar_ptr
;
770 index
= lp_build_const_int32(gallivm
,
771 reg
->Register
.Index
* 4 + swizzle
);
773 scalar_ptr
= LLVMBuildGEP(builder
, bld
->system_values_array
,
775 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
777 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
782 assert(0 && "invalid src register in emit_fetch()");
783 return bld
->base
.undef
;
786 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
787 case TGSI_UTIL_SIGN_CLEAR
:
788 res
= lp_build_abs( &bld
->base
, res
);
791 case TGSI_UTIL_SIGN_SET
:
792 res
= lp_build_abs( &bld
->base
, res
);
794 case TGSI_UTIL_SIGN_TOGGLE
:
795 res
= lp_build_negate( &bld
->base
, res
);
798 case TGSI_UTIL_SIGN_KEEP
:
807 * Register fetch with derivatives.
811 struct lp_build_tgsi_soa_context
*bld
,
812 const struct tgsi_full_instruction
*inst
,
814 const unsigned chan_index
,
821 src
= emit_fetch(bld
, inst
, index
, chan_index
);
826 /* TODO: use interpolation coeffs for inputs */
829 *ddx
= lp_build_ddx(&bld
->base
, src
);
832 *ddy
= lp_build_ddy(&bld
->base
, src
);
840 emit_fetch_predicate(
841 struct lp_build_tgsi_soa_context
*bld
,
842 const struct tgsi_full_instruction
*inst
,
845 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
847 unsigned char swizzles
[4];
848 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
852 if (!inst
->Instruction
.Predicate
) {
853 FOR_EACH_CHANNEL( chan
) {
859 swizzles
[0] = inst
->Predicate
.SwizzleX
;
860 swizzles
[1] = inst
->Predicate
.SwizzleY
;
861 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
862 swizzles
[3] = inst
->Predicate
.SwizzleW
;
864 index
= inst
->Predicate
.Index
;
865 assert(index
< LP_MAX_TGSI_PREDS
);
867 FOR_EACH_CHANNEL( chan
) {
868 unsigned swizzle
= swizzles
[chan
];
871 * Only fetch the predicate register channels that are actually listed
874 if (!unswizzled
[swizzle
]) {
875 value
= LLVMBuildLoad(builder
,
876 bld
->preds
[index
][swizzle
], "");
879 * Convert the value to an integer mask.
881 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
882 * is needlessly causing two comparisons due to storing the intermediate
883 * result as float vector instead of an integer mask vector.
885 value
= lp_build_compare(bld
->base
.gallivm
,
890 if (inst
->Predicate
.Negate
) {
891 value
= LLVMBuildNot(builder
, value
, "");
894 unswizzled
[swizzle
] = value
;
896 value
= unswizzled
[swizzle
];
909 struct lp_build_tgsi_soa_context
*bld
,
910 const struct tgsi_full_instruction
*inst
,
916 struct gallivm_state
*gallivm
= bld
->base
.gallivm
;
917 LLVMBuilderRef builder
= gallivm
->builder
;
918 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
919 struct lp_build_context
*uint_bld
= &bld
->uint_bld
;
920 LLVMValueRef indirect_index
= NULL
;
922 switch( inst
->Instruction
.Saturate
) {
926 case TGSI_SAT_ZERO_ONE
:
927 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
928 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
931 case TGSI_SAT_MINUS_PLUS_ONE
:
932 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.gallivm
, bld
->base
.type
, -1.0));
933 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
940 if (reg
->Register
.Indirect
) {
941 indirect_index
= get_indirect_index(bld
,
946 assert(reg
->Register
.Index
<= bld
->info
->file_max
[reg
->Register
.File
]);
949 switch( reg
->Register
.File
) {
950 case TGSI_FILE_OUTPUT
:
951 if (reg
->Register
.Indirect
) {
952 LLVMValueRef chan_vec
=
953 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
954 LLVMValueRef length_vec
=
955 lp_build_const_int_vec(gallivm
, uint_bld
->type
, bld
->base
.type
.length
);
956 LLVMValueRef index_vec
; /* indexes into the temp registers */
957 LLVMValueRef outputs_array
;
958 LLVMValueRef pixel_offsets
;
959 LLVMTypeRef float_ptr_type
;
962 /* build pixel offset vector: {0, 1, 2, 3, ...} */
963 pixel_offsets
= uint_bld
->undef
;
964 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
965 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
966 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
970 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
971 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
972 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
973 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
974 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
977 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
978 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
,
981 /* Scatter store values into temp registers */
982 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
983 &bld
->exec_mask
, pred
);
986 LLVMValueRef out_ptr
= get_output_ptr(bld
, reg
->Register
.Index
,
988 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, out_ptr
);
992 case TGSI_FILE_TEMPORARY
:
993 if (reg
->Register
.Indirect
) {
994 LLVMValueRef chan_vec
=
995 lp_build_const_int_vec(gallivm
, uint_bld
->type
, chan_index
);
996 LLVMValueRef length_vec
=
997 lp_build_const_int_vec(gallivm
, uint_bld
->type
,
998 bld
->base
.type
.length
);
999 LLVMValueRef index_vec
; /* indexes into the temp registers */
1000 LLVMValueRef temps_array
;
1001 LLVMValueRef pixel_offsets
;
1002 LLVMTypeRef float_ptr_type
;
1005 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1006 pixel_offsets
= uint_bld
->undef
;
1007 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
1008 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1009 pixel_offsets
= LLVMBuildInsertElement(builder
, pixel_offsets
,
1013 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1014 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1015 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1016 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1017 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1020 LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1021 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
,
1022 float_ptr_type
, "");
1024 /* Scatter store values into temp registers */
1025 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
1026 &bld
->exec_mask
, pred
);
1029 LLVMValueRef temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
1031 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, temp_ptr
);
1035 case TGSI_FILE_ADDRESS
:
1036 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
1037 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1040 case TGSI_FILE_PREDICATE
:
1041 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
1042 bld
->preds
[reg
->Register
.Index
][chan_index
]);
1052 * High-level instruction translators.
1056 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
1057 const struct tgsi_full_instruction
*inst
,
1058 enum lp_build_tex_modifier modifier
,
1059 LLVMValueRef
*texel
)
1061 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
1063 LLVMValueRef lod_bias
, explicit_lod
;
1064 LLVMValueRef oow
= NULL
;
1065 LLVMValueRef coords
[3];
1066 LLVMValueRef ddx
[3];
1067 LLVMValueRef ddy
[3];
1068 unsigned num_coords
;
1071 if (!bld
->sampler
) {
1072 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1073 for (i
= 0; i
< 4; i
++) {
1074 texel
[i
] = bld
->base
.undef
;
1079 switch (inst
->Texture
.Texture
) {
1080 case TGSI_TEXTURE_1D
:
1083 case TGSI_TEXTURE_1D_ARRAY
:
1084 case TGSI_TEXTURE_2D
:
1085 case TGSI_TEXTURE_RECT
:
1088 case TGSI_TEXTURE_SHADOW1D
:
1089 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1090 case TGSI_TEXTURE_SHADOW2D
:
1091 case TGSI_TEXTURE_SHADOWRECT
:
1092 case TGSI_TEXTURE_2D_ARRAY
:
1093 case TGSI_TEXTURE_3D
:
1094 case TGSI_TEXTURE_CUBE
:
1097 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1105 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
1106 lod_bias
= emit_fetch( bld
, inst
, 0, 3 );
1107 explicit_lod
= NULL
;
1109 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
1111 explicit_lod
= emit_fetch( bld
, inst
, 0, 3 );
1115 explicit_lod
= NULL
;
1118 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
1119 oow
= emit_fetch( bld
, inst
, 0, 3 );
1120 oow
= lp_build_rcp(&bld
->base
, oow
);
1123 for (i
= 0; i
< num_coords
; i
++) {
1124 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
1125 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
1126 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
1128 for (i
= num_coords
; i
< 3; i
++) {
1129 coords
[i
] = bld
->base
.undef
;
1132 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
1133 LLVMValueRef index0
= lp_build_const_int32(bld
->base
.gallivm
, 0);
1134 for (i
= 0; i
< num_coords
; i
++) {
1135 LLVMValueRef src1
= emit_fetch( bld
, inst
, 1, i
);
1136 LLVMValueRef src2
= emit_fetch( bld
, inst
, 2, i
);
1137 ddx
[i
] = LLVMBuildExtractElement(builder
, src1
, index0
, "");
1138 ddy
[i
] = LLVMBuildExtractElement(builder
, src2
, index0
, "");
1140 unit
= inst
->Src
[3].Register
.Index
;
1142 for (i
= 0; i
< num_coords
; i
++) {
1143 ddx
[i
] = lp_build_scalar_ddx( &bld
->base
, coords
[i
] );
1144 ddy
[i
] = lp_build_scalar_ddy( &bld
->base
, coords
[i
] );
1146 unit
= inst
->Src
[1].Register
.Index
;
1148 for (i
= num_coords
; i
< 3; i
++) {
1149 ddx
[i
] = LLVMGetUndef(bld
->base
.elem_type
);
1150 ddy
[i
] = LLVMGetUndef(bld
->base
.elem_type
);
1153 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
1156 unit
, num_coords
, coords
,
1158 lod_bias
, explicit_lod
,
1163 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
1168 for (i
= 0; i
< 5; i
++) {
1171 if (pc
+ i
>= bld
->info
->num_instructions
)
1174 opcode
= bld
->instructions
[pc
+ i
].Instruction
.Opcode
;
1176 if (opcode
== TGSI_OPCODE_END
)
1179 if (opcode
== TGSI_OPCODE_TEX
||
1180 opcode
== TGSI_OPCODE_TXP
||
1181 opcode
== TGSI_OPCODE_TXD
||
1182 opcode
== TGSI_OPCODE_TXB
||
1183 opcode
== TGSI_OPCODE_TXL
||
1184 opcode
== TGSI_OPCODE_TXF
||
1185 opcode
== TGSI_OPCODE_TXQ
||
1186 opcode
== TGSI_OPCODE_CAL
||
1187 opcode
== TGSI_OPCODE_CALLNZ
||
1188 opcode
== TGSI_OPCODE_IF
||
1189 opcode
== TGSI_OPCODE_IFC
||
1190 opcode
== TGSI_OPCODE_BGNLOOP
||
1191 opcode
== TGSI_OPCODE_SWITCH
)
1201 * Kill fragment if any of the src register values are negative.
1205 struct lp_build_tgsi_soa_context
*bld
,
1206 const struct tgsi_full_instruction
*inst
,
1209 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
1210 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1211 LLVMValueRef terms
[NUM_CHANNELS
];
1213 unsigned chan_index
;
1215 memset(&terms
, 0, sizeof terms
);
1217 FOR_EACH_CHANNEL( chan_index
) {
1220 /* Unswizzle channel */
1221 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
1223 /* Check if the component has not been already tested. */
1224 assert(swizzle
< NUM_CHANNELS
);
1225 if( !terms
[swizzle
] )
1226 /* TODO: change the comparison operator instead of setting the sign */
1227 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
1231 FOR_EACH_CHANNEL( chan_index
) {
1232 if(terms
[chan_index
]) {
1233 LLVMValueRef chan_mask
;
1236 * If term < 0 then mask = 0 else mask = ~0.
1238 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
1241 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
1248 lp_build_mask_update(bld
->mask
, mask
);
1250 if (!near_end_of_shader(bld
, pc
))
1251 lp_build_mask_check(bld
->mask
);
1257 * Predicated fragment kill.
1258 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1259 * The only predication is the execution mask which will apply if
1260 * we're inside a loop or conditional.
1263 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
1264 const struct tgsi_full_instruction
*inst
,
1267 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
1270 /* For those channels which are "alive", disable fragment shader
1273 if (bld
->exec_mask
.has_mask
) {
1274 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
1277 LLVMValueRef zero
= LLVMConstNull(bld
->base
.int_vec_type
);
1281 lp_build_mask_update(bld
->mask
, mask
);
1283 if (!near_end_of_shader(bld
, pc
))
1284 lp_build_mask_check(bld
->mask
);
1289 * Emit code which will dump the value of all the temporary registers
1293 emit_dump_temps(struct lp_build_tgsi_soa_context
*bld
)
1295 struct gallivm_state
*gallivm
= bld
->base
.gallivm
;
1296 LLVMBuilderRef builder
= gallivm
->builder
;
1297 LLVMValueRef temp_ptr
;
1298 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
1299 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
1300 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
1301 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
1303 int n
= bld
->info
->file_max
[TGSI_FILE_TEMPORARY
];
1305 for (index
= 0; index
< n
; index
++) {
1306 LLVMValueRef idx
= lp_build_const_int32(gallivm
, index
);
1307 LLVMValueRef v
[4][4], res
;
1310 lp_build_printf(gallivm
, "TEMP[%d]:\n", idx
);
1312 for (chan
= 0; chan
< 4; chan
++) {
1313 temp_ptr
= get_temp_ptr(bld
, index
, chan
);
1314 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1315 v
[chan
][0] = LLVMBuildExtractElement(builder
, res
, i0
, "");
1316 v
[chan
][1] = LLVMBuildExtractElement(builder
, res
, i1
, "");
1317 v
[chan
][2] = LLVMBuildExtractElement(builder
, res
, i2
, "");
1318 v
[chan
][3] = LLVMBuildExtractElement(builder
, res
, i3
, "");
1321 lp_build_printf(gallivm
, " X: %f %f %f %f\n",
1322 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
1323 lp_build_printf(gallivm
, " Y: %f %f %f %f\n",
1324 v
[1][0], v
[1][1], v
[1][2], v
[1][3]);
1325 lp_build_printf(gallivm
, " Z: %f %f %f %f\n",
1326 v
[2][0], v
[2][1], v
[2][2], v
[2][3]);
1327 lp_build_printf(gallivm
, " W: %f %f %f %f\n",
1328 v
[3][0], v
[3][1], v
[3][2], v
[3][3]);
1336 struct lp_build_tgsi_soa_context
*bld
,
1337 const struct tgsi_full_declaration
*decl
)
1339 struct gallivm_state
*gallivm
= bld
->base
.gallivm
;
1340 LLVMTypeRef vec_type
= bld
->base
.vec_type
;
1341 const unsigned first
= decl
->Range
.First
;
1342 const unsigned last
= decl
->Range
.Last
;
1345 for (idx
= first
; idx
<= last
; ++idx
) {
1346 assert(last
<= bld
->info
->file_max
[decl
->Declaration
.File
]);
1347 switch (decl
->Declaration
.File
) {
1348 case TGSI_FILE_TEMPORARY
:
1349 assert(idx
< LP_MAX_TGSI_TEMPS
);
1350 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
1351 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1352 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
1356 case TGSI_FILE_OUTPUT
:
1357 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
1358 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1359 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
1360 vec_type
, "output");
1364 case TGSI_FILE_ADDRESS
:
1365 assert(idx
< LP_MAX_TGSI_ADDRS
);
1366 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1367 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "addr");
1370 case TGSI_FILE_PREDICATE
:
1371 assert(idx
< LP_MAX_TGSI_PREDS
);
1372 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1373 bld
->preds
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
,
1378 /* don't need to declare other vars */
1386 * Emit LLVM for one TGSI instruction.
1387 * \param return TRUE for success, FALSE otherwise
1391 struct lp_build_tgsi_soa_context
*bld
,
1392 const struct tgsi_full_instruction
*inst
,
1393 const struct tgsi_opcode_info
*info
,
1396 unsigned chan_index
;
1397 LLVMValueRef src0
, src1
, src2
;
1398 LLVMValueRef tmp0
, tmp1
, tmp2
;
1399 LLVMValueRef tmp3
= NULL
;
1400 LLVMValueRef tmp4
= NULL
;
1401 LLVMValueRef tmp5
= NULL
;
1402 LLVMValueRef tmp6
= NULL
;
1403 LLVMValueRef tmp7
= NULL
;
1405 LLVMValueRef dst0
[NUM_CHANNELS
];
1408 * Stores and write masks are handled in a general fashion after the long
1409 * instruction opcode switch statement.
1411 * Although not stricitly necessary, we avoid generating instructions for
1412 * channels which won't be stored, in cases where's that easy. For some
1413 * complex instructions, like texture sampling, it is more convenient to
1414 * assume a full writemask and then let LLVM optimization passes eliminate
1420 assert(info
->num_dst
<= 1);
1421 if (info
->num_dst
) {
1422 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1423 dst0
[chan_index
] = bld
->base
.undef
;
1427 switch (inst
->Instruction
.Opcode
) {
1428 case TGSI_OPCODE_ARL
:
1429 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1430 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1431 tmp0
= lp_build_floor(&bld
->base
, tmp0
);
1432 dst0
[chan_index
] = tmp0
;
1436 case TGSI_OPCODE_MOV
:
1437 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1438 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
1442 case TGSI_OPCODE_LIT
:
1443 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
1444 dst0
[CHAN_X
] = bld
->base
.one
;
1446 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1447 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1448 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
1450 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1451 /* XMM[1] = SrcReg[0].yyyy */
1452 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1453 /* XMM[1] = max(XMM[1], 0) */
1454 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
1455 /* XMM[2] = SrcReg[0].wwww */
1456 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1457 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
1458 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1459 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
1460 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
1462 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
1463 dst0
[CHAN_W
] = bld
->base
.one
;
1467 case TGSI_OPCODE_RCP
:
1468 /* TGSI_OPCODE_RECIP */
1469 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1470 res
= lp_build_rcp(&bld
->base
, src0
);
1471 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1472 dst0
[chan_index
] = res
;
1476 case TGSI_OPCODE_RSQ
:
1477 /* TGSI_OPCODE_RECIPSQRT */
1478 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1479 src0
= lp_build_abs(&bld
->base
, src0
);
1480 res
= lp_build_rsqrt(&bld
->base
, src0
);
1481 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1482 dst0
[chan_index
] = res
;
1486 case TGSI_OPCODE_EXP
:
1487 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1488 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1489 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1490 LLVMValueRef
*p_exp2_int_part
= NULL
;
1491 LLVMValueRef
*p_frac_part
= NULL
;
1492 LLVMValueRef
*p_exp2
= NULL
;
1494 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1496 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1497 p_exp2_int_part
= &tmp0
;
1498 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1499 p_frac_part
= &tmp1
;
1500 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1503 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
1505 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1506 dst0
[CHAN_X
] = tmp0
;
1507 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1508 dst0
[CHAN_Y
] = tmp1
;
1509 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1510 dst0
[CHAN_Z
] = tmp2
;
1513 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1514 dst0
[CHAN_W
] = bld
->base
.one
;
1518 case TGSI_OPCODE_LOG
:
1519 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1520 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1521 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1522 LLVMValueRef
*p_floor_log2
= NULL
;
1523 LLVMValueRef
*p_exp
= NULL
;
1524 LLVMValueRef
*p_log2
= NULL
;
1526 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1527 src0
= lp_build_abs( &bld
->base
, src0
);
1529 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1530 p_floor_log2
= &tmp0
;
1531 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1533 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1536 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
1538 /* dst.x = floor(lg2(abs(src.x))) */
1539 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1540 dst0
[CHAN_X
] = tmp0
;
1541 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1542 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
1543 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
1545 /* dst.z = lg2(abs(src.x)) */
1546 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1547 dst0
[CHAN_Z
] = tmp2
;
1550 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1551 dst0
[CHAN_W
] = bld
->base
.one
;
1555 case TGSI_OPCODE_MUL
:
1556 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1557 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1558 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1559 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
1563 case TGSI_OPCODE_ADD
:
1564 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1565 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1566 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1567 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
1571 case TGSI_OPCODE_DP3
:
1572 /* TGSI_OPCODE_DOT3 */
1573 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1574 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1575 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1576 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1577 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1578 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1579 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1580 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1581 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1582 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1583 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1584 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1585 dst0
[chan_index
] = tmp0
;
1589 case TGSI_OPCODE_DP4
:
1590 /* TGSI_OPCODE_DOT4 */
1591 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1592 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1593 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1594 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1595 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1596 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1597 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1598 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1599 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1600 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1601 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1602 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1603 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1604 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1605 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1606 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1607 dst0
[chan_index
] = tmp0
;
1611 case TGSI_OPCODE_DST
:
1612 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1613 dst0
[CHAN_X
] = bld
->base
.one
;
1615 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1616 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1617 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1618 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1620 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1621 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
1623 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1624 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
1628 case TGSI_OPCODE_MIN
:
1629 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1630 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1631 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1632 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
1636 case TGSI_OPCODE_MAX
:
1637 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1638 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1639 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1640 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
1644 case TGSI_OPCODE_SLT
:
1645 /* TGSI_OPCODE_SETLT */
1646 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1647 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1648 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1649 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
1650 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1654 case TGSI_OPCODE_SGE
:
1655 /* TGSI_OPCODE_SETGE */
1656 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1657 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1658 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1659 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1660 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1664 case TGSI_OPCODE_MAD
:
1665 /* TGSI_OPCODE_MADD */
1666 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1667 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1668 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1669 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1670 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1671 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1672 dst0
[chan_index
] = tmp0
;
1676 case TGSI_OPCODE_SUB
:
1677 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1678 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1679 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1680 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1684 case TGSI_OPCODE_LRP
:
1685 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1686 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1687 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1688 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1689 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1690 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1691 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1695 case TGSI_OPCODE_CND
:
1696 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1697 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1698 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1699 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1700 tmp1
= lp_build_const_vec(bld
->base
.gallivm
, bld
->base
.type
, 0.5);
1701 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1702 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1706 case TGSI_OPCODE_DP2A
:
1707 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1708 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1709 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1710 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1711 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1712 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1713 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1714 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1715 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1716 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1717 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1721 case TGSI_OPCODE_FRC
:
1722 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1723 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1724 tmp0
= lp_build_floor(&bld
->base
, src0
);
1725 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1726 dst0
[chan_index
] = tmp0
;
1730 case TGSI_OPCODE_CLAMP
:
1731 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1732 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1733 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1734 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1735 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1736 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1737 dst0
[chan_index
] = tmp0
;
1741 case TGSI_OPCODE_FLR
:
1742 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1743 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1744 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1748 case TGSI_OPCODE_ROUND
:
1749 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1750 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1751 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1755 case TGSI_OPCODE_EX2
: {
1756 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1757 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1758 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1759 dst0
[chan_index
] = tmp0
;
1764 case TGSI_OPCODE_LG2
:
1765 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1766 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1767 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1768 dst0
[chan_index
] = tmp0
;
1772 case TGSI_OPCODE_POW
:
1773 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1774 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1775 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1776 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1777 dst0
[chan_index
] = res
;
1781 case TGSI_OPCODE_XPD
:
1782 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1783 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1784 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1785 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1787 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1788 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1789 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1790 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1792 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1794 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1796 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1797 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1798 dst0
[CHAN_X
] = tmp2
;
1800 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1801 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1802 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1803 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1805 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1806 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1807 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1808 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1809 dst0
[CHAN_Y
] = tmp3
;
1811 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1812 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1813 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1814 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1815 dst0
[CHAN_Z
] = tmp5
;
1817 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1818 dst0
[CHAN_W
] = bld
->base
.one
;
1822 case TGSI_OPCODE_ABS
:
1823 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1824 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1825 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1829 case TGSI_OPCODE_RCC
:
1834 case TGSI_OPCODE_DPH
:
1835 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1836 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1837 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1838 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1839 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1840 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1841 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1842 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1843 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1844 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1845 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1846 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1847 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1848 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1849 dst0
[chan_index
] = tmp0
;
1853 case TGSI_OPCODE_COS
:
1854 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1855 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1856 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1857 dst0
[chan_index
] = tmp0
;
1861 case TGSI_OPCODE_DDX
:
1862 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1863 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1867 case TGSI_OPCODE_DDY
:
1868 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1869 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1873 case TGSI_OPCODE_KILP
:
1874 /* predicated kill */
1875 emit_kilp( bld
, inst
, (*pc
)-1 );
1878 case TGSI_OPCODE_KIL
:
1879 /* conditional kill */
1880 emit_kil( bld
, inst
, (*pc
)-1 );
1883 case TGSI_OPCODE_PK2H
:
1887 case TGSI_OPCODE_PK2US
:
1891 case TGSI_OPCODE_PK4B
:
1895 case TGSI_OPCODE_PK4UB
:
1899 case TGSI_OPCODE_RFL
:
1903 case TGSI_OPCODE_SEQ
:
1904 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1905 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1906 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1907 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1908 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1912 case TGSI_OPCODE_SFL
:
1913 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1914 dst0
[chan_index
] = bld
->base
.zero
;
1918 case TGSI_OPCODE_SGT
:
1919 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1920 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1921 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1922 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1923 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1927 case TGSI_OPCODE_SIN
:
1928 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1929 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1930 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1931 dst0
[chan_index
] = tmp0
;
1935 case TGSI_OPCODE_SLE
:
1936 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1937 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1938 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1939 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1940 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1944 case TGSI_OPCODE_SNE
:
1945 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1946 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1947 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1948 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1949 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1953 case TGSI_OPCODE_STR
:
1954 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1955 dst0
[chan_index
] = bld
->base
.one
;
1959 case TGSI_OPCODE_TEX
:
1960 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_NONE
, dst0
);
1963 case TGSI_OPCODE_TXD
:
1964 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
, dst0
);
1967 case TGSI_OPCODE_UP2H
:
1973 case TGSI_OPCODE_UP2US
:
1979 case TGSI_OPCODE_UP4B
:
1985 case TGSI_OPCODE_UP4UB
:
1991 case TGSI_OPCODE_X2D
:
1997 case TGSI_OPCODE_ARA
:
2003 case TGSI_OPCODE_ARR
:
2004 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2005 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
2006 tmp0
= lp_build_round(&bld
->base
, tmp0
);
2007 dst0
[chan_index
] = tmp0
;
2011 case TGSI_OPCODE_BRA
:
2017 case TGSI_OPCODE_CAL
:
2018 lp_exec_mask_call(&bld
->exec_mask
,
2024 case TGSI_OPCODE_RET
:
2025 lp_exec_mask_ret(&bld
->exec_mask
, pc
);
2028 case TGSI_OPCODE_END
:
2031 emit_dump_temps(bld
);
2036 case TGSI_OPCODE_SSG
:
2037 /* TGSI_OPCODE_SGN */
2038 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2039 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
2040 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
2044 case TGSI_OPCODE_CMP
:
2045 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2046 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
2047 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
2048 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
2049 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
2050 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
2054 case TGSI_OPCODE_SCS
:
2055 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
2056 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
2057 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
2059 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
2060 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
2061 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
2063 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
2064 dst0
[CHAN_Z
] = bld
->base
.zero
;
2066 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
2067 dst0
[CHAN_W
] = bld
->base
.one
;
2071 case TGSI_OPCODE_TXB
:
2072 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
, dst0
);
2075 case TGSI_OPCODE_NRM
:
2077 case TGSI_OPCODE_NRM4
:
2078 /* 3 or 4-component normalization */
2080 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
2082 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
2083 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
2084 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
2085 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
2087 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
2090 /* xmm0 = src.x * src.x */
2091 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
2092 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
2095 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
2098 /* xmm0 = xmm0 + src.y * src.y */
2099 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
2100 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
2103 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
2104 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
2107 /* xmm0 = xmm0 + src.z * src.z */
2108 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
2109 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
2112 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
2113 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
2117 /* xmm0 = xmm0 + src.w * src.w */
2118 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
2119 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
2122 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
2123 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
2126 /* xmm1 = 1 / sqrt(xmm0) */
2127 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
2129 /* dst.x = xmm1 * src.x */
2130 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
2131 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
2134 /* dst.y = xmm1 * src.y */
2135 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
2136 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
2139 /* dst.z = xmm1 * src.z */
2140 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
2141 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
2144 /* dst.w = xmm1 * src.w */
2145 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
2146 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
2151 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
2152 dst0
[CHAN_W
] = bld
->base
.one
;
2157 case TGSI_OPCODE_DIV
:
2163 case TGSI_OPCODE_DP2
:
2164 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
2165 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
2166 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
2167 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
2168 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
2169 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
2170 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
2171 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2172 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
2176 case TGSI_OPCODE_TXL
:
2177 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
, dst0
);
2180 case TGSI_OPCODE_TXP
:
2181 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_PROJECTED
, dst0
);
2184 case TGSI_OPCODE_BRK
:
2185 lp_exec_break(&bld
->exec_mask
);
2188 case TGSI_OPCODE_IF
:
2189 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
2190 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
2191 tmp0
, bld
->base
.zero
);
2192 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
2195 case TGSI_OPCODE_BGNLOOP
:
2196 lp_exec_bgnloop(&bld
->exec_mask
);
2199 case TGSI_OPCODE_BGNSUB
:
2200 lp_exec_mask_bgnsub(&bld
->exec_mask
);
2203 case TGSI_OPCODE_ELSE
:
2204 lp_exec_mask_cond_invert(&bld
->exec_mask
);
2207 case TGSI_OPCODE_ENDIF
:
2208 lp_exec_mask_cond_pop(&bld
->exec_mask
);
2211 case TGSI_OPCODE_ENDLOOP
:
2212 lp_exec_endloop(bld
->base
.gallivm
, &bld
->exec_mask
);
2215 case TGSI_OPCODE_ENDSUB
:
2216 lp_exec_mask_endsub(&bld
->exec_mask
, pc
);
2219 case TGSI_OPCODE_PUSHA
:
2225 case TGSI_OPCODE_POPA
:
2231 case TGSI_OPCODE_CEIL
:
2232 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2233 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
2234 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
2238 case TGSI_OPCODE_I2F
:
2244 case TGSI_OPCODE_NOT
:
2250 case TGSI_OPCODE_TRUNC
:
2251 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2252 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
2253 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
2257 case TGSI_OPCODE_SHL
:
2263 case TGSI_OPCODE_ISHR
:
2269 case TGSI_OPCODE_AND
:
2275 case TGSI_OPCODE_OR
:
2281 case TGSI_OPCODE_MOD
:
2287 case TGSI_OPCODE_XOR
:
2293 case TGSI_OPCODE_SAD
:
2299 case TGSI_OPCODE_TXF
:
2305 case TGSI_OPCODE_TXQ
:
2311 case TGSI_OPCODE_CONT
:
2312 lp_exec_continue(&bld
->exec_mask
);
2315 case TGSI_OPCODE_EMIT
:
2319 case TGSI_OPCODE_ENDPRIM
:
2323 case TGSI_OPCODE_NOP
:
2331 LLVMValueRef pred
[NUM_CHANNELS
];
2333 emit_fetch_predicate( bld
, inst
, pred
);
2335 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2336 emit_store( bld
, inst
, 0, chan_index
, pred
[chan_index
], dst0
[chan_index
]);
2345 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
2346 const struct tgsi_token
*tokens
,
2347 struct lp_type type
,
2348 struct lp_build_mask_context
*mask
,
2349 LLVMValueRef consts_ptr
,
2350 LLVMValueRef system_values_array
,
2351 const LLVMValueRef
*pos
,
2352 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
2353 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
2354 struct lp_build_sampler_soa
*sampler
,
2355 const struct tgsi_shader_info
*info
)
2357 struct lp_build_tgsi_soa_context bld
;
2358 struct tgsi_parse_context parse
;
2359 uint num_immediates
= 0;
2360 uint num_instructions
= 0;
2364 struct lp_type res_type
;
2366 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
2367 memset(&res_type
, 0, sizeof res_type
);
2368 res_type
.width
= type
.width
;
2369 res_type
.length
= type
.length
;
2372 /* Setup build context */
2373 memset(&bld
, 0, sizeof bld
);
2374 lp_build_context_init(&bld
.base
, gallivm
, type
);
2375 lp_build_context_init(&bld
.uint_bld
, gallivm
, lp_uint_type(type
));
2376 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
2379 bld
.inputs
= inputs
;
2380 bld
.outputs
= outputs
;
2381 bld
.consts_ptr
= consts_ptr
;
2382 bld
.sampler
= sampler
;
2384 bld
.indirect_files
= info
->indirect_files
;
2385 bld
.instructions
= (struct tgsi_full_instruction
*)
2386 MALLOC( LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
) );
2387 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
2389 if (!bld
.instructions
) {
2393 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
2395 if (bld
.indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
2396 LLVMValueRef array_size
=
2397 lp_build_const_int32(gallivm
,
2398 info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
2399 bld
.temps_array
= lp_build_array_alloca(gallivm
,
2400 bld
.base
.vec_type
, array_size
,
2404 if (bld
.indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
2405 LLVMValueRef array_size
=
2406 lp_build_const_int32(gallivm
,
2407 info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
2408 bld
.outputs_array
= lp_build_array_alloca(gallivm
,
2409 bld
.base
.vec_type
, array_size
,
2413 /* If we have indirect addressing in inputs we need to copy them into
2414 * our alloca array to be able to iterate over them */
2415 if (bld
.indirect_files
& (1 << TGSI_FILE_INPUT
)) {
2416 unsigned index
, chan
;
2417 LLVMTypeRef vec_type
= bld
.base
.vec_type
;
2418 LLVMValueRef array_size
=
2419 lp_build_const_int32(gallivm
, info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
2420 bld
.inputs_array
= lp_build_array_alloca(gallivm
,
2421 vec_type
, array_size
,
2424 assert(info
->num_inputs
<= info
->file_max
[TGSI_FILE_INPUT
] + 1);
2426 for (index
= 0; index
< info
->num_inputs
; ++index
) {
2427 for (chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
2428 LLVMValueRef lindex
=
2429 lp_build_const_int32(gallivm
, index
* 4 + chan
);
2430 LLVMValueRef input_ptr
=
2431 LLVMBuildGEP(gallivm
->builder
, bld
.inputs_array
,
2433 LLVMValueRef value
= bld
.inputs
[index
][chan
];
2435 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
2440 bld
.system_values_array
= system_values_array
;
2442 tgsi_parse_init( &parse
, tokens
);
2444 while( !tgsi_parse_end_of_tokens( &parse
) ) {
2445 tgsi_parse_token( &parse
);
2447 switch( parse
.FullToken
.Token
.Type
) {
2448 case TGSI_TOKEN_TYPE_DECLARATION
:
2449 /* Inputs already interpolated */
2450 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
2453 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2455 /* save expanded instruction */
2456 if (num_instructions
== bld
.max_instructions
) {
2457 struct tgsi_full_instruction
*instructions
;
2458 instructions
= REALLOC(bld
.instructions
,
2459 bld
.max_instructions
2460 * sizeof(struct tgsi_full_instruction
),
2461 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
2462 * sizeof(struct tgsi_full_instruction
));
2463 if (!instructions
) {
2466 bld
.instructions
= instructions
;
2467 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
2470 memcpy(bld
.instructions
+ num_instructions
,
2471 &parse
.FullToken
.FullInstruction
,
2472 sizeof(bld
.instructions
[0]));
2479 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2480 /* simply copy the immediate values into the next immediates[] slot */
2482 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
2484 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
2485 for( i
= 0; i
< size
; ++i
)
2486 bld
.immediates
[num_immediates
][i
] =
2487 lp_build_const_vec(gallivm
, type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
2488 for( i
= size
; i
< 4; ++i
)
2489 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
2494 case TGSI_TOKEN_TYPE_PROPERTY
:
2503 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
2504 const struct tgsi_opcode_info
*opcode_info
=
2505 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
2506 if (!emit_instruction( &bld
, instr
, opcode_info
, &pc
))
2507 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2508 opcode_info
->mnemonic
);
2511 /* If we have indirect addressing in outputs we need to copy our alloca array
2512 * to the outputs slots specified by the called */
2513 if (bld
.indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
2514 unsigned index
, chan
;
2515 assert(info
->num_outputs
<= info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
2516 for (index
= 0; index
< info
->num_outputs
; ++index
) {
2517 for (chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
2518 bld
.outputs
[index
][chan
] = get_output_ptr(&bld
, index
, chan
);
2524 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
2525 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2526 debug_printf("11111111111111111111111111111 \n");
2527 tgsi_dump(tokens
, 0);
2528 lp_debug_dump_value(function
);
2529 debug_printf("2222222222222222222222222222 \n");
2531 tgsi_parse_free( &parse
);
2534 LLVMModuleRef module
= LLVMGetGlobalParent(
2535 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
2536 LLVMDumpModule(module
);
2540 FREE( bld
.instructions
);
2545 * Build up the system values array out of individual values such as
2546 * the instance ID, front-face, primitive ID, etc. The shader info is
2547 * used to determine which system values are needed and where to put
2548 * them in the system values array.
2550 * XXX only instance ID is implemented at this time.
2552 * The system values register file is similar to the constants buffer.
2553 * Example declaration:
2554 * DCL SV[0], INSTANCEID
2555 * Example instruction:
2556 * MOVE foo, SV[0].xxxx;
2558 * \return LLVM float array (interpreted as float [][4])
2561 lp_build_system_values_array(struct gallivm_state
*gallivm
,
2562 const struct tgsi_shader_info
*info
,
2563 LLVMValueRef instance_id
,
2564 LLVMValueRef facing
)
2566 LLVMValueRef size
= lp_build_const_int32(gallivm
, 4 * info
->num_system_values
);
2567 LLVMTypeRef float_t
= LLVMFloatTypeInContext(gallivm
->context
);
2568 LLVMValueRef array
= lp_build_array_alloca(gallivm
, float_t
,
2569 size
, "sysvals_array");
2572 for (i
= 0; i
< info
->num_system_values
; i
++) {
2573 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
* 4);
2574 LLVMValueRef ptr
, value
= 0;
2576 switch (info
->system_value_semantic_name
[i
]) {
2577 case TGSI_SEMANTIC_INSTANCEID
:
2578 /* convert instance ID from int to float */
2579 value
= LLVMBuildSIToFP(gallivm
->builder
, instance_id
, float_t
,
2580 "sysval_instanceid");
2582 case TGSI_SEMANTIC_FACE
:
2585 assert(0 && "unexpected semantic in build_system_values_array()");
2588 ptr
= LLVMBuildGEP(gallivm
->builder
, array
, &index
, 1, "");
2589 LLVMBuildStore(gallivm
->builder
, value
, ptr
);