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_gather.h"
53 #include "lp_bld_logic.h"
54 #include "lp_bld_swizzle.h"
55 #include "lp_bld_flow.h"
56 #include "lp_bld_quad.h"
57 #include "lp_bld_tgsi.h"
58 #include "lp_bld_limits.h"
59 #include "lp_bld_debug.h"
62 #define FOR_EACH_CHANNEL( CHAN )\
63 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
65 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
66 ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
68 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
69 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
71 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
72 FOR_EACH_CHANNEL( CHAN )\
73 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
79 #define NUM_CHANNELS 4
81 #define LP_MAX_INSTRUCTIONS 256
85 struct lp_build_context
*bld
;
89 LLVMTypeRef int_vec_type
;
91 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
93 LLVMValueRef cond_mask
;
95 LLVMBasicBlockRef loop_block
;
96 LLVMValueRef cont_mask
;
97 LLVMValueRef break_mask
;
98 LLVMValueRef break_var
;
100 LLVMBasicBlockRef loop_block
;
101 LLVMValueRef cont_mask
;
102 LLVMValueRef break_mask
;
103 LLVMValueRef break_var
;
104 } loop_stack
[LP_MAX_TGSI_NESTING
];
107 LLVMValueRef ret_mask
;
110 LLVMValueRef ret_mask
;
111 } call_stack
[LP_MAX_TGSI_NESTING
];
114 LLVMValueRef exec_mask
;
117 struct lp_build_tgsi_soa_context
119 struct lp_build_context base
;
121 /* Builder for integer masks and indices */
122 struct lp_build_context int_bld
;
124 LLVMValueRef consts_ptr
;
125 const LLVMValueRef
*pos
;
126 const LLVMValueRef (*inputs
)[NUM_CHANNELS
];
127 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
129 const struct lp_build_sampler_soa
*sampler
;
131 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][NUM_CHANNELS
];
132 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][NUM_CHANNELS
];
133 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][NUM_CHANNELS
];
134 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][NUM_CHANNELS
];
136 /* we allocate an array of temps if we have indirect
137 * addressing and then the temps above is unused */
138 LLVMValueRef temps_array
;
139 boolean has_indirect_addressing
;
141 struct lp_build_mask_context
*mask
;
142 struct lp_exec_mask exec_mask
;
144 struct tgsi_full_instruction
*instructions
;
145 uint max_instructions
;
148 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
151 mask
->has_mask
= FALSE
;
152 mask
->cond_stack_size
= 0;
153 mask
->loop_stack_size
= 0;
154 mask
->call_stack_size
= 0;
156 mask
->int_vec_type
= lp_build_int_vec_type(mask
->bld
->type
);
157 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
158 LLVMConstAllOnes(mask
->int_vec_type
);
161 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
163 if (mask
->loop_stack_size
) {
164 /*for loops we need to update the entire mask at runtime */
166 assert(mask
->break_mask
);
167 tmp
= LLVMBuildAnd(mask
->bld
->builder
,
171 mask
->exec_mask
= LLVMBuildAnd(mask
->bld
->builder
,
176 mask
->exec_mask
= mask
->cond_mask
;
178 if (mask
->call_stack_size
) {
179 mask
->exec_mask
= LLVMBuildAnd(mask
->bld
->builder
,
185 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
186 mask
->loop_stack_size
> 0 ||
187 mask
->call_stack_size
> 0);
190 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
193 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
194 if (mask
->cond_stack_size
== 0) {
195 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
197 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
198 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
199 mask
->cond_mask
= val
;
201 lp_exec_mask_update(mask
);
204 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
206 LLVMValueRef prev_mask
;
207 LLVMValueRef inv_mask
;
209 assert(mask
->cond_stack_size
);
210 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
211 if (mask
->cond_stack_size
== 1) {
212 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
215 inv_mask
= LLVMBuildNot(mask
->bld
->builder
, mask
->cond_mask
, "");
217 mask
->cond_mask
= LLVMBuildAnd(mask
->bld
->builder
,
220 lp_exec_mask_update(mask
);
223 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
225 assert(mask
->cond_stack_size
);
226 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
227 lp_exec_mask_update(mask
);
230 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
232 if (mask
->loop_stack_size
== 0) {
233 assert(mask
->loop_block
== NULL
);
234 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
235 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
236 assert(mask
->break_var
== NULL
);
239 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
241 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
242 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
243 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
244 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
245 ++mask
->loop_stack_size
;
247 mask
->break_var
= lp_build_alloca(mask
->bld
->builder
, mask
->int_vec_type
, "");
248 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
250 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->builder
, "bgnloop");
251 LLVMBuildBr(mask
->bld
->builder
, mask
->loop_block
);
252 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, mask
->loop_block
);
254 mask
->break_mask
= LLVMBuildLoad(mask
->bld
->builder
, mask
->break_var
, "");
256 lp_exec_mask_update(mask
);
259 static void lp_exec_break(struct lp_exec_mask
*mask
)
261 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
265 mask
->break_mask
= LLVMBuildAnd(mask
->bld
->builder
,
267 exec_mask
, "break_full");
269 lp_exec_mask_update(mask
);
272 static void lp_exec_continue(struct lp_exec_mask
*mask
)
274 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
278 mask
->cont_mask
= LLVMBuildAnd(mask
->bld
->builder
,
282 lp_exec_mask_update(mask
);
286 static void lp_exec_endloop(struct lp_exec_mask
*mask
)
288 LLVMBasicBlockRef endloop
;
289 LLVMTypeRef reg_type
= LLVMIntType(mask
->bld
->type
.width
*
290 mask
->bld
->type
.length
);
293 assert(mask
->break_mask
);
296 * Restore the cont_mask, but don't pop
298 assert(mask
->loop_stack_size
);
299 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
300 lp_exec_mask_update(mask
);
303 * Unlike the continue mask, the break_mask must be preserved across loop
306 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
308 /* i1cond = (mask == 0) */
309 i1cond
= LLVMBuildICmp(
312 LLVMBuildBitCast(mask
->bld
->builder
, mask
->exec_mask
, reg_type
, ""),
313 LLVMConstNull(reg_type
), "");
315 endloop
= lp_build_insert_new_block(mask
->bld
->builder
, "endloop");
317 LLVMBuildCondBr(mask
->bld
->builder
,
318 i1cond
, mask
->loop_block
, endloop
);
320 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, endloop
);
322 assert(mask
->loop_stack_size
);
323 --mask
->loop_stack_size
;
324 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
325 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
326 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
327 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
329 lp_exec_mask_update(mask
);
332 /* stores val into an address pointed to by dst.
333 * mask->exec_mask is used to figure out which bits of val
334 * should be stored into the address
335 * (0 means don't store this bit, 1 means do store).
337 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
342 /* Mix the predicate and execution mask */
343 if (mask
->has_mask
) {
345 pred
= LLVMBuildAnd(mask
->bld
->builder
, pred
, mask
->exec_mask
, "");
347 pred
= mask
->exec_mask
;
352 LLVMValueRef real_val
, dst_val
;
354 dst_val
= LLVMBuildLoad(mask
->bld
->builder
, dst
, "");
355 real_val
= lp_build_select(mask
->bld
,
359 LLVMBuildStore(mask
->bld
->builder
, real_val
, dst
);
361 LLVMBuildStore(mask
->bld
->builder
, val
, dst
);
364 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
368 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
369 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
370 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
371 mask
->call_stack_size
++;
375 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
377 LLVMValueRef exec_mask
;
379 if (mask
->call_stack_size
== 0) {
380 /* returning from main() */
384 exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
388 mask
->ret_mask
= LLVMBuildAnd(mask
->bld
->builder
,
390 exec_mask
, "ret_full");
392 lp_exec_mask_update(mask
);
395 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
399 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
401 assert(mask
->call_stack_size
);
402 mask
->call_stack_size
--;
403 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
404 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
405 lp_exec_mask_update(mask
);
410 * Return pointer to a temporary register channel (src or dest).
411 * \param index which temporary register
412 * \param chan which channel of the temp register.
413 * \param is_indirect if true, add 'addr' to the index
414 * \param addr indirect addressing offset (should already have been
415 * multiplied by four).
418 get_temp_ptr(struct lp_build_tgsi_soa_context
*bld
,
425 if (bld
->has_indirect_addressing
) {
426 LLVMValueRef lindex
=
427 LLVMConstInt(LLVMInt32Type(), index
* 4 + chan
, 0);
429 lindex
= lp_build_add(&bld
->base
, lindex
, addr
);
430 return LLVMBuildGEP(bld
->base
.builder
, bld
->temps_array
, &lindex
, 1, "");
433 return bld
->temps
[index
][chan
];
440 * XXX the lp_build_gather() function should be capable of doing this
441 * with a little work.
444 build_gather(struct lp_build_tgsi_soa_context
*bld
,
445 LLVMValueRef base_ptr
,
446 LLVMValueRef indexes
)
448 LLVMValueRef res
= bld
->base
.undef
;
452 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
454 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
455 LLVMValueRef ii
= LLVMConstInt(LLVMInt32Type(), i
, 0);
456 LLVMValueRef index
= LLVMBuildExtractElement(bld
->base
.builder
,
458 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, base_ptr
,
460 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
462 res
= LLVMBuildInsertElement(bld
->base
.builder
, res
, scalar
, ii
, "");
474 struct lp_build_tgsi_soa_context
*bld
,
475 const struct tgsi_full_instruction
*inst
,
477 const unsigned chan_index
)
479 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
480 const unsigned swizzle
=
481 tgsi_util_get_full_src_register_swizzle(reg
, chan_index
);
483 LLVMValueRef addr_vec
= NULL
;
486 assert(0 && "invalid swizzle in emit_fetch()");
487 return bld
->base
.undef
;
490 if (reg
->Register
.Indirect
) {
491 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
492 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
494 LLVMValueRef vec4
= lp_build_const_int_vec(bld
->int_bld
.type
, 4);
496 assert(bld
->has_indirect_addressing
);
498 addr_vec
= LLVMBuildLoad(bld
->base
.builder
,
499 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
502 /* for indexing we want integers */
503 addr_vec
= LLVMBuildFPToSI(bld
->base
.builder
, addr_vec
,
506 /* addr_vec = addr_vec * 4 */
507 addr_vec
= lp_build_mul(&bld
->base
, addr_vec
, vec4
);
510 switch (reg
->Register
.File
) {
511 case TGSI_FILE_CONSTANT
:
513 if (reg
->Register
.Indirect
) {
514 LLVMValueRef index_vec
; /* index into the const buffer */
516 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
517 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
518 reg
->Register
.Index
* 4 + swizzle
);
520 /* index_vec = index_vec + addr_vec */
521 index_vec
= lp_build_add(&bld
->base
, index_vec
, addr_vec
);
523 /* Gather values from the constant buffer */
524 res
= build_gather(bld
, bld
->consts_ptr
, index_vec
);
527 LLVMValueRef index
; /* index into the const buffer */
528 LLVMValueRef scalar
, scalar_ptr
;
530 index
= lp_build_const_int32(reg
->Register
.Index
*4 + swizzle
);
532 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
534 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
536 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
541 case TGSI_FILE_IMMEDIATE
:
542 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
546 case TGSI_FILE_INPUT
:
547 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
551 case TGSI_FILE_TEMPORARY
:
553 LLVMValueRef addr
= NULL
;
554 LLVMValueRef temp_ptr
;
556 if (reg
->Register
.Indirect
) {
557 LLVMValueRef zero
= lp_build_const_int32(0);
558 addr
= LLVMBuildExtractElement(bld
->base
.builder
,
562 temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
564 reg
->Register
.Indirect
,
566 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
568 return bld
->base
.undef
;
573 assert(0 && "invalid src register in emit_fetch()");
574 return bld
->base
.undef
;
577 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
578 case TGSI_UTIL_SIGN_CLEAR
:
579 res
= lp_build_abs( &bld
->base
, res
);
582 case TGSI_UTIL_SIGN_SET
:
583 /* TODO: Use bitwese OR for floating point */
584 res
= lp_build_abs( &bld
->base
, res
);
585 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
588 case TGSI_UTIL_SIGN_TOGGLE
:
589 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
592 case TGSI_UTIL_SIGN_KEEP
:
601 * Register fetch with derivatives.
605 struct lp_build_tgsi_soa_context
*bld
,
606 const struct tgsi_full_instruction
*inst
,
608 const unsigned chan_index
,
615 src
= emit_fetch(bld
, inst
, index
, chan_index
);
620 /* TODO: use interpolation coeffs for inputs */
623 *ddx
= lp_build_ddx(&bld
->base
, src
);
626 *ddy
= lp_build_ddy(&bld
->base
, src
);
634 emit_fetch_predicate(
635 struct lp_build_tgsi_soa_context
*bld
,
636 const struct tgsi_full_instruction
*inst
,
640 unsigned char swizzles
[4];
641 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
645 if (!inst
->Instruction
.Predicate
) {
646 FOR_EACH_CHANNEL( chan
) {
652 swizzles
[0] = inst
->Predicate
.SwizzleX
;
653 swizzles
[1] = inst
->Predicate
.SwizzleY
;
654 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
655 swizzles
[3] = inst
->Predicate
.SwizzleW
;
657 index
= inst
->Predicate
.Index
;
658 assert(index
< LP_MAX_TGSI_PREDS
);
660 FOR_EACH_CHANNEL( chan
) {
661 unsigned swizzle
= swizzles
[chan
];
664 * Only fetch the predicate register channels that are actually listed
667 if (!unswizzled
[swizzle
]) {
668 value
= LLVMBuildLoad(bld
->base
.builder
,
669 bld
->preds
[index
][swizzle
], "");
672 * Convert the value to an integer mask.
674 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
675 * is needlessly causing two comparisons due to storing the intermediate
676 * result as float vector instead of an integer mask vector.
678 value
= lp_build_compare(bld
->base
.builder
,
683 if (inst
->Predicate
.Negate
) {
684 value
= LLVMBuildNot(bld
->base
.builder
, value
, "");
687 unswizzled
[swizzle
] = value
;
689 value
= unswizzled
[swizzle
];
702 struct lp_build_tgsi_soa_context
*bld
,
703 const struct tgsi_full_instruction
*inst
,
709 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
710 LLVMValueRef addr
= NULL
;
712 switch( inst
->Instruction
.Saturate
) {
716 case TGSI_SAT_ZERO_ONE
:
717 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
718 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
721 case TGSI_SAT_MINUS_PLUS_ONE
:
722 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
723 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
730 if (reg
->Register
.Indirect
) {
731 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
732 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
733 addr
= LLVMBuildLoad(bld
->base
.builder
,
734 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
736 /* for indexing we want integers */
737 addr
= LLVMBuildFPToSI(bld
->base
.builder
, addr
,
739 addr
= LLVMBuildExtractElement(bld
->base
.builder
,
740 addr
, LLVMConstInt(LLVMInt32Type(), 0, 0),
742 addr
= lp_build_mul(&bld
->base
, addr
, LLVMConstInt(LLVMInt32Type(), 4, 0));
745 switch( reg
->Register
.File
) {
746 case TGSI_FILE_OUTPUT
:
747 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
748 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
751 case TGSI_FILE_TEMPORARY
: {
752 LLVMValueRef temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
754 reg
->Register
.Indirect
,
756 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, temp_ptr
);
760 case TGSI_FILE_ADDRESS
:
761 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
762 bld
->addr
[reg
->Indirect
.Index
][chan_index
]);
765 case TGSI_FILE_PREDICATE
:
766 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
767 bld
->preds
[index
][chan_index
]);
777 * High-level instruction translators.
781 TEX_MODIFIER_NONE
= 0,
782 TEX_MODIFIER_PROJECTED
,
783 TEX_MODIFIER_LOD_BIAS
,
784 TEX_MODIFIER_EXPLICIT_LOD
,
785 TEX_MODIFIER_EXPLICIT_DERIV
789 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
790 const struct tgsi_full_instruction
*inst
,
791 enum tex_modifier modifier
,
795 LLVMValueRef lod_bias
, explicit_lod
;
796 LLVMValueRef oow
= NULL
;
797 LLVMValueRef coords
[3];
804 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
805 for (i
= 0; i
< 4; i
++) {
806 texel
[i
] = bld
->base
.undef
;
811 switch (inst
->Texture
.Texture
) {
812 case TGSI_TEXTURE_1D
:
815 case TGSI_TEXTURE_2D
:
816 case TGSI_TEXTURE_RECT
:
819 case TGSI_TEXTURE_SHADOW1D
:
820 case TGSI_TEXTURE_SHADOW2D
:
821 case TGSI_TEXTURE_SHADOWRECT
:
822 case TGSI_TEXTURE_3D
:
823 case TGSI_TEXTURE_CUBE
:
831 if (modifier
== TEX_MODIFIER_LOD_BIAS
) {
832 lod_bias
= emit_fetch( bld
, inst
, 0, 3 );
835 else if (modifier
== TEX_MODIFIER_EXPLICIT_LOD
) {
837 explicit_lod
= emit_fetch( bld
, inst
, 0, 3 );
844 if (modifier
== TEX_MODIFIER_PROJECTED
) {
845 oow
= emit_fetch( bld
, inst
, 0, 3 );
846 oow
= lp_build_rcp(&bld
->base
, oow
);
849 for (i
= 0; i
< num_coords
; i
++) {
850 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
851 if (modifier
== TEX_MODIFIER_PROJECTED
)
852 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
854 for (i
= num_coords
; i
< 3; i
++) {
855 coords
[i
] = bld
->base
.undef
;
858 if (modifier
== TEX_MODIFIER_EXPLICIT_DERIV
) {
859 for (i
= 0; i
< num_coords
; i
++) {
860 ddx
[i
] = emit_fetch( bld
, inst
, 1, i
);
861 ddy
[i
] = emit_fetch( bld
, inst
, 2, i
);
863 unit
= inst
->Src
[3].Register
.Index
;
865 for (i
= 0; i
< num_coords
; i
++) {
866 ddx
[i
] = lp_build_ddx( &bld
->base
, coords
[i
] );
867 ddy
[i
] = lp_build_ddy( &bld
->base
, coords
[i
] );
869 unit
= inst
->Src
[1].Register
.Index
;
871 for (i
= num_coords
; i
< 3; i
++) {
872 ddx
[i
] = bld
->base
.undef
;
873 ddy
[i
] = bld
->base
.undef
;
876 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
879 unit
, num_coords
, coords
,
881 lod_bias
, explicit_lod
,
887 * Kill fragment if any of the src register values are negative.
891 struct lp_build_tgsi_soa_context
*bld
,
892 const struct tgsi_full_instruction
*inst
)
894 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
895 LLVMValueRef terms
[NUM_CHANNELS
];
899 memset(&terms
, 0, sizeof terms
);
901 FOR_EACH_CHANNEL( chan_index
) {
904 /* Unswizzle channel */
905 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
907 /* Check if the component has not been already tested. */
908 assert(swizzle
< NUM_CHANNELS
);
909 if( !terms
[swizzle
] )
910 /* TODO: change the comparison operator instead of setting the sign */
911 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
915 FOR_EACH_CHANNEL( chan_index
) {
916 if(terms
[chan_index
]) {
917 LLVMValueRef chan_mask
;
920 * If term < 0 then mask = 0 else mask = ~0.
922 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
925 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
932 lp_build_mask_update(bld
->mask
, mask
);
937 * Predicated fragment kill.
938 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
939 * The only predication is the execution mask which will apply if
940 * we're inside a loop or conditional.
943 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
944 const struct tgsi_full_instruction
*inst
)
948 /* For those channels which are "alive", disable fragment shader
951 if (bld
->exec_mask
.has_mask
) {
952 mask
= LLVMBuildNot(bld
->base
.builder
, bld
->exec_mask
.exec_mask
, "kilp");
955 mask
= bld
->base
.zero
;
958 lp_build_mask_update(bld
->mask
, mask
);
963 struct lp_build_tgsi_soa_context
*bld
,
964 const struct tgsi_full_declaration
*decl
)
966 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
968 unsigned first
= decl
->Range
.First
;
969 unsigned last
= decl
->Range
.Last
;
972 for (idx
= first
; idx
<= last
; ++idx
) {
973 switch (decl
->Declaration
.File
) {
974 case TGSI_FILE_TEMPORARY
:
975 assert(idx
< LP_MAX_TGSI_TEMPS
);
976 if (bld
->has_indirect_addressing
) {
977 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
979 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
980 vec_type
, array_size
, "");
982 for (i
= 0; i
< NUM_CHANNELS
; i
++)
983 bld
->temps
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
988 case TGSI_FILE_OUTPUT
:
989 for (i
= 0; i
< NUM_CHANNELS
; i
++)
990 bld
->outputs
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
994 case TGSI_FILE_ADDRESS
:
995 assert(idx
< LP_MAX_TGSI_ADDRS
);
996 for (i
= 0; i
< NUM_CHANNELS
; i
++)
997 bld
->addr
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1001 case TGSI_FILE_PREDICATE
:
1002 assert(idx
< LP_MAX_TGSI_PREDS
);
1003 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1004 bld
->preds
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1009 /* don't need to declare other vars */
1017 * Emit LLVM for one TGSI instruction.
1018 * \param return TRUE for success, FALSE otherwise
1022 struct lp_build_tgsi_soa_context
*bld
,
1023 const struct tgsi_full_instruction
*inst
,
1024 const struct tgsi_opcode_info
*info
,
1027 unsigned chan_index
;
1028 LLVMValueRef src0
, src1
, src2
;
1029 LLVMValueRef tmp0
, tmp1
, tmp2
;
1030 LLVMValueRef tmp3
= NULL
;
1031 LLVMValueRef tmp4
= NULL
;
1032 LLVMValueRef tmp5
= NULL
;
1033 LLVMValueRef tmp6
= NULL
;
1034 LLVMValueRef tmp7
= NULL
;
1036 LLVMValueRef dst0
[NUM_CHANNELS
];
1039 * Stores and write masks are handled in a general fashion after the long
1040 * instruction opcode switch statement.
1042 * Although not stricitly necessary, we avoid generating instructions for
1043 * channels which won't be stored, in cases where's that easy. For some
1044 * complex instructions, like texture sampling, it is more convenient to
1045 * assume a full writemask and then let LLVM optimization passes eliminate
1051 assert(info
->num_dst
<= 1);
1052 if (info
->num_dst
) {
1053 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1054 dst0
[chan_index
] = bld
->base
.undef
;
1058 switch (inst
->Instruction
.Opcode
) {
1059 case TGSI_OPCODE_ARL
:
1060 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1061 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1062 tmp0
= lp_build_floor(&bld
->base
, tmp0
);
1063 dst0
[chan_index
] = tmp0
;
1067 case TGSI_OPCODE_MOV
:
1068 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1069 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
1073 case TGSI_OPCODE_LIT
:
1074 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
1075 dst0
[CHAN_X
] = bld
->base
.one
;
1077 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1078 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1079 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
1081 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1082 /* XMM[1] = SrcReg[0].yyyy */
1083 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1084 /* XMM[1] = max(XMM[1], 0) */
1085 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
1086 /* XMM[2] = SrcReg[0].wwww */
1087 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1088 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
1089 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1090 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
1091 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
1093 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
1094 dst0
[CHAN_W
] = bld
->base
.one
;
1098 case TGSI_OPCODE_RCP
:
1099 /* TGSI_OPCODE_RECIP */
1100 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1101 res
= lp_build_rcp(&bld
->base
, src0
);
1102 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1103 dst0
[chan_index
] = res
;
1107 case TGSI_OPCODE_RSQ
:
1108 /* TGSI_OPCODE_RECIPSQRT */
1109 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1110 src0
= lp_build_abs(&bld
->base
, src0
);
1111 res
= lp_build_rsqrt(&bld
->base
, src0
);
1112 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1113 dst0
[chan_index
] = res
;
1117 case TGSI_OPCODE_EXP
:
1118 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1119 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1120 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1121 LLVMValueRef
*p_exp2_int_part
= NULL
;
1122 LLVMValueRef
*p_frac_part
= NULL
;
1123 LLVMValueRef
*p_exp2
= NULL
;
1125 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1127 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1128 p_exp2_int_part
= &tmp0
;
1129 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1130 p_frac_part
= &tmp1
;
1131 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1134 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
1136 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1137 dst0
[CHAN_X
] = tmp0
;
1138 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1139 dst0
[CHAN_Y
] = tmp1
;
1140 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1141 dst0
[CHAN_Z
] = tmp2
;
1144 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1145 dst0
[CHAN_W
] = bld
->base
.one
;
1149 case TGSI_OPCODE_LOG
:
1150 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1151 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1152 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1153 LLVMValueRef
*p_floor_log2
= NULL
;
1154 LLVMValueRef
*p_exp
= NULL
;
1155 LLVMValueRef
*p_log2
= NULL
;
1157 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1158 src0
= lp_build_abs( &bld
->base
, src0
);
1160 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1161 p_floor_log2
= &tmp0
;
1162 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1164 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1167 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
1169 /* dst.x = floor(lg2(abs(src.x))) */
1170 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1171 dst0
[CHAN_X
] = tmp0
;
1172 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1173 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
1174 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
1176 /* dst.z = lg2(abs(src.x)) */
1177 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1178 dst0
[CHAN_Z
] = tmp2
;
1181 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1182 dst0
[CHAN_W
] = bld
->base
.one
;
1186 case TGSI_OPCODE_MUL
:
1187 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1188 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1189 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1190 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
1194 case TGSI_OPCODE_ADD
:
1195 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1196 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1197 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1198 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
1202 case TGSI_OPCODE_DP3
:
1203 /* TGSI_OPCODE_DOT3 */
1204 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1205 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1206 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1207 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1208 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1209 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1210 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1211 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1212 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1213 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1214 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1215 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1216 dst0
[chan_index
] = tmp0
;
1220 case TGSI_OPCODE_DP4
:
1221 /* TGSI_OPCODE_DOT4 */
1222 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1223 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1224 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1225 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1226 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1227 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1228 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1229 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1230 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1231 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1232 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1233 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1234 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1235 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1236 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1237 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1238 dst0
[chan_index
] = tmp0
;
1242 case TGSI_OPCODE_DST
:
1243 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1244 dst0
[CHAN_X
] = bld
->base
.one
;
1246 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1247 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1248 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1249 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1251 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1252 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
1254 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1255 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
1259 case TGSI_OPCODE_MIN
:
1260 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1261 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1262 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1263 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
1267 case TGSI_OPCODE_MAX
:
1268 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1269 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1270 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1271 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
1275 case TGSI_OPCODE_SLT
:
1276 /* TGSI_OPCODE_SETLT */
1277 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1278 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1279 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1280 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
1281 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1285 case TGSI_OPCODE_SGE
:
1286 /* TGSI_OPCODE_SETGE */
1287 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1288 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1289 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1290 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1291 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1295 case TGSI_OPCODE_MAD
:
1296 /* TGSI_OPCODE_MADD */
1297 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1298 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1299 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1300 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1301 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1302 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1303 dst0
[chan_index
] = tmp0
;
1307 case TGSI_OPCODE_SUB
:
1308 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1309 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1310 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1311 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1315 case TGSI_OPCODE_LRP
:
1316 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1317 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1318 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1319 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1320 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1321 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1322 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1326 case TGSI_OPCODE_CND
:
1327 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1328 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1329 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1330 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1331 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
1332 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1333 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1337 case TGSI_OPCODE_DP2A
:
1338 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1339 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1340 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1341 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1342 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1343 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1344 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1345 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1346 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1347 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1348 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1352 case TGSI_OPCODE_FRC
:
1353 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1354 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1355 tmp0
= lp_build_floor(&bld
->base
, src0
);
1356 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1357 dst0
[chan_index
] = tmp0
;
1361 case TGSI_OPCODE_CLAMP
:
1362 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1363 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1364 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1365 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1366 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1367 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1368 dst0
[chan_index
] = tmp0
;
1372 case TGSI_OPCODE_FLR
:
1373 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1374 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1375 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1379 case TGSI_OPCODE_ROUND
:
1380 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1381 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1382 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1386 case TGSI_OPCODE_EX2
: {
1387 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1388 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1389 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1390 dst0
[chan_index
] = tmp0
;
1395 case TGSI_OPCODE_LG2
:
1396 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1397 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1398 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1399 dst0
[chan_index
] = tmp0
;
1403 case TGSI_OPCODE_POW
:
1404 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1405 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1406 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1407 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1408 dst0
[chan_index
] = res
;
1412 case TGSI_OPCODE_XPD
:
1413 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1414 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1415 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1416 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1418 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1419 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1420 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1421 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1423 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1425 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1427 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1428 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1429 dst0
[CHAN_X
] = tmp2
;
1431 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1432 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1433 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1434 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1436 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1437 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1438 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1439 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1440 dst0
[CHAN_Y
] = tmp3
;
1442 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1443 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1444 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1445 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1446 dst0
[CHAN_Z
] = tmp5
;
1448 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1449 dst0
[CHAN_W
] = bld
->base
.one
;
1453 case TGSI_OPCODE_ABS
:
1454 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1455 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1456 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1460 case TGSI_OPCODE_RCC
:
1465 case TGSI_OPCODE_DPH
:
1466 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1467 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1468 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1469 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1470 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1471 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1472 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1473 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1474 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1475 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1476 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1477 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1478 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1479 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1480 dst0
[chan_index
] = tmp0
;
1484 case TGSI_OPCODE_COS
:
1485 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1486 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1487 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1488 dst0
[chan_index
] = tmp0
;
1492 case TGSI_OPCODE_DDX
:
1493 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1494 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1498 case TGSI_OPCODE_DDY
:
1499 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1500 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1504 case TGSI_OPCODE_KILP
:
1505 /* predicated kill */
1506 emit_kilp( bld
, inst
);
1509 case TGSI_OPCODE_KIL
:
1510 /* conditional kill */
1511 emit_kil( bld
, inst
);
1514 case TGSI_OPCODE_PK2H
:
1518 case TGSI_OPCODE_PK2US
:
1522 case TGSI_OPCODE_PK4B
:
1526 case TGSI_OPCODE_PK4UB
:
1530 case TGSI_OPCODE_RFL
:
1534 case TGSI_OPCODE_SEQ
:
1535 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1536 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1537 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1538 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1539 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1543 case TGSI_OPCODE_SFL
:
1544 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1545 dst0
[chan_index
] = bld
->base
.zero
;
1549 case TGSI_OPCODE_SGT
:
1550 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1551 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1552 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1553 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1554 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1558 case TGSI_OPCODE_SIN
:
1559 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1560 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1561 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1562 dst0
[chan_index
] = tmp0
;
1566 case TGSI_OPCODE_SLE
:
1567 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1568 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1569 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1570 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1571 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1575 case TGSI_OPCODE_SNE
:
1576 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1577 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1578 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1579 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1580 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1584 case TGSI_OPCODE_STR
:
1585 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1586 dst0
[chan_index
] = bld
->base
.one
;
1590 case TGSI_OPCODE_TEX
:
1591 emit_tex( bld
, inst
, TEX_MODIFIER_NONE
, dst0
);
1594 case TGSI_OPCODE_TXD
:
1595 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_DERIV
, dst0
);
1598 case TGSI_OPCODE_UP2H
:
1604 case TGSI_OPCODE_UP2US
:
1610 case TGSI_OPCODE_UP4B
:
1616 case TGSI_OPCODE_UP4UB
:
1622 case TGSI_OPCODE_X2D
:
1628 case TGSI_OPCODE_ARA
:
1634 case TGSI_OPCODE_ARR
:
1635 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1636 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1637 tmp0
= lp_build_round(&bld
->base
, tmp0
);
1638 dst0
[chan_index
] = tmp0
;
1642 case TGSI_OPCODE_BRA
:
1648 case TGSI_OPCODE_CAL
:
1649 lp_exec_mask_call(&bld
->exec_mask
,
1655 case TGSI_OPCODE_RET
:
1656 lp_exec_mask_ret(&bld
->exec_mask
, pc
);
1659 case TGSI_OPCODE_END
:
1663 case TGSI_OPCODE_SSG
:
1664 /* TGSI_OPCODE_SGN */
1665 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1666 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1667 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1671 case TGSI_OPCODE_CMP
:
1672 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1673 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1674 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1675 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1676 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1677 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1681 case TGSI_OPCODE_SCS
:
1682 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1683 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1684 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1686 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1687 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1688 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1690 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1691 dst0
[CHAN_Z
] = bld
->base
.zero
;
1693 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1694 dst0
[CHAN_W
] = bld
->base
.one
;
1698 case TGSI_OPCODE_TXB
:
1699 emit_tex( bld
, inst
, TEX_MODIFIER_LOD_BIAS
, dst0
);
1702 case TGSI_OPCODE_NRM
:
1704 case TGSI_OPCODE_NRM4
:
1705 /* 3 or 4-component normalization */
1707 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1709 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1710 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1711 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1712 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1714 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1717 /* xmm0 = src.x * src.x */
1718 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1719 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1722 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1725 /* xmm0 = xmm0 + src.y * src.y */
1726 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1727 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1730 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1731 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1734 /* xmm0 = xmm0 + src.z * src.z */
1735 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1736 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1739 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1740 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1744 /* xmm0 = xmm0 + src.w * src.w */
1745 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1746 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1749 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1750 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1753 /* xmm1 = 1 / sqrt(xmm0) */
1754 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1756 /* dst.x = xmm1 * src.x */
1757 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1758 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1761 /* dst.y = xmm1 * src.y */
1762 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1763 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1766 /* dst.z = xmm1 * src.z */
1767 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1768 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1771 /* dst.w = xmm1 * src.w */
1772 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1773 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1778 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1779 dst0
[CHAN_W
] = bld
->base
.one
;
1784 case TGSI_OPCODE_DIV
:
1790 case TGSI_OPCODE_DP2
:
1791 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1792 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1793 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1794 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1795 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1796 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1797 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1798 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1799 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1803 case TGSI_OPCODE_TXL
:
1804 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_LOD
, dst0
);
1807 case TGSI_OPCODE_TXP
:
1808 emit_tex( bld
, inst
, TEX_MODIFIER_PROJECTED
, dst0
);
1811 case TGSI_OPCODE_BRK
:
1812 lp_exec_break(&bld
->exec_mask
);
1815 case TGSI_OPCODE_IF
:
1816 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1817 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
1818 tmp0
, bld
->base
.zero
);
1819 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1822 case TGSI_OPCODE_BGNLOOP
:
1823 lp_exec_bgnloop(&bld
->exec_mask
);
1826 case TGSI_OPCODE_BGNSUB
:
1827 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1830 case TGSI_OPCODE_ELSE
:
1831 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1834 case TGSI_OPCODE_ENDIF
:
1835 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1838 case TGSI_OPCODE_ENDLOOP
:
1839 lp_exec_endloop(&bld
->exec_mask
);
1842 case TGSI_OPCODE_ENDSUB
:
1843 lp_exec_mask_endsub(&bld
->exec_mask
, pc
);
1846 case TGSI_OPCODE_PUSHA
:
1852 case TGSI_OPCODE_POPA
:
1858 case TGSI_OPCODE_CEIL
:
1859 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1860 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1861 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1865 case TGSI_OPCODE_I2F
:
1871 case TGSI_OPCODE_NOT
:
1877 case TGSI_OPCODE_TRUNC
:
1878 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1879 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1880 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1884 case TGSI_OPCODE_SHL
:
1890 case TGSI_OPCODE_ISHR
:
1896 case TGSI_OPCODE_AND
:
1902 case TGSI_OPCODE_OR
:
1908 case TGSI_OPCODE_MOD
:
1914 case TGSI_OPCODE_XOR
:
1920 case TGSI_OPCODE_SAD
:
1926 case TGSI_OPCODE_TXF
:
1932 case TGSI_OPCODE_TXQ
:
1938 case TGSI_OPCODE_CONT
:
1939 lp_exec_continue(&bld
->exec_mask
);
1942 case TGSI_OPCODE_EMIT
:
1946 case TGSI_OPCODE_ENDPRIM
:
1950 case TGSI_OPCODE_NOP
:
1958 LLVMValueRef pred
[NUM_CHANNELS
];
1960 emit_fetch_predicate( bld
, inst
, pred
);
1962 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1963 emit_store( bld
, inst
, 0, chan_index
, pred
[chan_index
], dst0
[chan_index
]);
1972 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1973 const struct tgsi_token
*tokens
,
1974 struct lp_type type
,
1975 struct lp_build_mask_context
*mask
,
1976 LLVMValueRef consts_ptr
,
1977 const LLVMValueRef
*pos
,
1978 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
1979 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1980 struct lp_build_sampler_soa
*sampler
,
1981 const struct tgsi_shader_info
*info
)
1983 struct lp_build_tgsi_soa_context bld
;
1984 struct tgsi_parse_context parse
;
1985 uint num_immediates
= 0;
1986 uint num_instructions
= 0;
1990 /* Setup build context */
1991 memset(&bld
, 0, sizeof bld
);
1992 lp_build_context_init(&bld
.base
, builder
, type
);
1993 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
1996 bld
.inputs
= inputs
;
1997 bld
.outputs
= outputs
;
1998 bld
.consts_ptr
= consts_ptr
;
1999 bld
.sampler
= sampler
;
2000 bld
.has_indirect_addressing
= info
->opcode_count
[TGSI_OPCODE_ARR
] > 0 ||
2001 info
->opcode_count
[TGSI_OPCODE_ARL
] > 0;
2002 bld
.instructions
= (struct tgsi_full_instruction
*)
2003 MALLOC( LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
) );
2004 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
2006 if (!bld
.instructions
) {
2010 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
2012 tgsi_parse_init( &parse
, tokens
);
2014 while( !tgsi_parse_end_of_tokens( &parse
) ) {
2015 tgsi_parse_token( &parse
);
2017 switch( parse
.FullToken
.Token
.Type
) {
2018 case TGSI_TOKEN_TYPE_DECLARATION
:
2019 /* Inputs already interpolated */
2020 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
2023 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2025 /* save expanded instruction */
2026 if (num_instructions
== bld
.max_instructions
) {
2027 bld
.instructions
= REALLOC(bld
.instructions
,
2028 bld
.max_instructions
2029 * sizeof(struct tgsi_full_instruction
),
2030 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
2031 * sizeof(struct tgsi_full_instruction
));
2032 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
2035 memcpy(bld
.instructions
+ num_instructions
,
2036 &parse
.FullToken
.FullInstruction
,
2037 sizeof(bld
.instructions
[0]));
2044 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2045 /* simply copy the immediate values into the next immediates[] slot */
2047 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
2049 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
2050 for( i
= 0; i
< size
; ++i
)
2051 bld
.immediates
[num_immediates
][i
] =
2052 lp_build_const_vec(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
2053 for( i
= size
; i
< 4; ++i
)
2054 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
2059 case TGSI_TOKEN_TYPE_PROPERTY
:
2068 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
2069 const struct tgsi_opcode_info
*opcode_info
=
2070 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
2071 if (!emit_instruction( &bld
, instr
, opcode_info
, &pc
))
2072 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2073 opcode_info
->mnemonic
);
2077 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
2078 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2079 debug_printf("11111111111111111111111111111 \n");
2080 tgsi_dump(tokens
, 0);
2081 lp_debug_dump_value(function
);
2082 debug_printf("2222222222222222222222222222 \n");
2084 tgsi_parse_free( &parse
);
2087 LLVMModuleRef module
= LLVMGetGlobalParent(
2088 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
2089 LLVMDumpModule(module
);
2093 FREE( bld
.instructions
);