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 * Note that indirect addressing cannot be handled here.
412 * \param index which temporary register
413 * \param chan which channel of the temp register.
416 get_temp_ptr(struct lp_build_tgsi_soa_context
*bld
,
421 if (bld
->has_indirect_addressing
) {
422 LLVMValueRef lindex
= lp_build_const_int32(index
* 4 + chan
);
423 return LLVMBuildGEP(bld
->base
.builder
, bld
->temps_array
, &lindex
, 1, "");
426 return bld
->temps
[index
][chan
];
433 * XXX the lp_build_gather() function should be capable of doing this
434 * with a little work.
437 build_gather(struct lp_build_tgsi_soa_context
*bld
,
438 LLVMValueRef base_ptr
,
439 LLVMValueRef indexes
)
441 LLVMValueRef res
= bld
->base
.undef
;
445 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
447 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
448 LLVMValueRef ii
= LLVMConstInt(LLVMInt32Type(), i
, 0);
449 LLVMValueRef index
= LLVMBuildExtractElement(bld
->base
.builder
,
451 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, base_ptr
,
453 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
455 res
= LLVMBuildInsertElement(bld
->base
.builder
, res
, scalar
, ii
, "");
467 struct lp_build_tgsi_soa_context
*bld
,
468 const struct tgsi_full_instruction
*inst
,
470 const unsigned chan_index
)
472 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
473 const unsigned swizzle
=
474 tgsi_util_get_full_src_register_swizzle(reg
, chan_index
);
476 LLVMValueRef addr_vec
= NULL
;
479 assert(0 && "invalid swizzle in emit_fetch()");
480 return bld
->base
.undef
;
483 if (reg
->Register
.Indirect
) {
484 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
485 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
487 LLVMValueRef vec4
= lp_build_const_int_vec(bld
->int_bld
.type
, 4);
489 assert(bld
->has_indirect_addressing
);
491 addr_vec
= LLVMBuildLoad(bld
->base
.builder
,
492 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
495 /* for indexing we want integers */
496 addr_vec
= LLVMBuildFPToSI(bld
->base
.builder
, addr_vec
,
499 /* addr_vec = addr_vec * 4 */
500 addr_vec
= lp_build_mul(&bld
->base
, addr_vec
, vec4
);
503 switch (reg
->Register
.File
) {
504 case TGSI_FILE_CONSTANT
:
505 if (reg
->Register
.Indirect
) {
506 LLVMValueRef index_vec
; /* index into the const buffer */
508 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
509 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
510 reg
->Register
.Index
* 4 + swizzle
);
512 /* index_vec = index_vec + addr_vec */
513 index_vec
= lp_build_add(&bld
->base
, index_vec
, addr_vec
);
515 /* Gather values from the constant buffer */
516 res
= build_gather(bld
, bld
->consts_ptr
, index_vec
);
519 LLVMValueRef index
; /* index into the const buffer */
520 LLVMValueRef scalar
, scalar_ptr
;
522 index
= lp_build_const_int32(reg
->Register
.Index
*4 + swizzle
);
524 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
526 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
528 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
532 case TGSI_FILE_IMMEDIATE
:
533 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
537 case TGSI_FILE_INPUT
:
538 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
542 case TGSI_FILE_TEMPORARY
:
543 if (reg
->Register
.Indirect
) {
544 LLVMValueRef vec_len
=
545 lp_build_const_int_vec(bld
->int_bld
.type
, bld
->base
.type
.length
);
546 LLVMValueRef index_vec
; /* index into the const buffer */
547 LLVMValueRef temps_array
;
548 LLVMTypeRef float4_ptr_type
;
550 assert(bld
->has_indirect_addressing
);
552 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
553 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
554 reg
->Register
.Index
* 4 + swizzle
);
556 /* index_vec += addr_vec */
557 index_vec
= lp_build_add(&bld
->int_bld
, index_vec
, addr_vec
);
559 /* index_vec *= vector_length */
560 index_vec
= lp_build_mul(&bld
->int_bld
, index_vec
, vec_len
);
562 /* cast temps_array pointer to float* */
563 float4_ptr_type
= LLVMPointerType(LLVMFloatType(), 0);
564 temps_array
= LLVMBuildBitCast(bld
->int_bld
.builder
, bld
->temps_array
,
565 float4_ptr_type
, "");
567 /* Gather values from the temporary register array */
568 res
= build_gather(bld
, temps_array
, index_vec
);
571 LLVMValueRef temp_ptr
;
572 temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
, swizzle
);
573 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
575 return bld
->base
.undef
;
580 assert(0 && "invalid src register in emit_fetch()");
581 return bld
->base
.undef
;
584 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
585 case TGSI_UTIL_SIGN_CLEAR
:
586 res
= lp_build_abs( &bld
->base
, res
);
589 case TGSI_UTIL_SIGN_SET
:
590 /* TODO: Use bitwese OR for floating point */
591 res
= lp_build_abs( &bld
->base
, res
);
592 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
595 case TGSI_UTIL_SIGN_TOGGLE
:
596 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
599 case TGSI_UTIL_SIGN_KEEP
:
608 * Register fetch with derivatives.
612 struct lp_build_tgsi_soa_context
*bld
,
613 const struct tgsi_full_instruction
*inst
,
615 const unsigned chan_index
,
622 src
= emit_fetch(bld
, inst
, index
, chan_index
);
627 /* TODO: use interpolation coeffs for inputs */
630 *ddx
= lp_build_ddx(&bld
->base
, src
);
633 *ddy
= lp_build_ddy(&bld
->base
, src
);
641 emit_fetch_predicate(
642 struct lp_build_tgsi_soa_context
*bld
,
643 const struct tgsi_full_instruction
*inst
,
647 unsigned char swizzles
[4];
648 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
652 if (!inst
->Instruction
.Predicate
) {
653 FOR_EACH_CHANNEL( chan
) {
659 swizzles
[0] = inst
->Predicate
.SwizzleX
;
660 swizzles
[1] = inst
->Predicate
.SwizzleY
;
661 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
662 swizzles
[3] = inst
->Predicate
.SwizzleW
;
664 index
= inst
->Predicate
.Index
;
665 assert(index
< LP_MAX_TGSI_PREDS
);
667 FOR_EACH_CHANNEL( chan
) {
668 unsigned swizzle
= swizzles
[chan
];
671 * Only fetch the predicate register channels that are actually listed
674 if (!unswizzled
[swizzle
]) {
675 value
= LLVMBuildLoad(bld
->base
.builder
,
676 bld
->preds
[index
][swizzle
], "");
679 * Convert the value to an integer mask.
681 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
682 * is needlessly causing two comparisons due to storing the intermediate
683 * result as float vector instead of an integer mask vector.
685 value
= lp_build_compare(bld
->base
.builder
,
690 if (inst
->Predicate
.Negate
) {
691 value
= LLVMBuildNot(bld
->base
.builder
, value
, "");
694 unswizzled
[swizzle
] = value
;
696 value
= unswizzled
[swizzle
];
709 struct lp_build_tgsi_soa_context
*bld
,
710 const struct tgsi_full_instruction
*inst
,
716 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
717 LLVMValueRef addr
= NULL
;
719 switch( inst
->Instruction
.Saturate
) {
723 case TGSI_SAT_ZERO_ONE
:
724 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
725 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
728 case TGSI_SAT_MINUS_PLUS_ONE
:
729 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
730 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
737 if (reg
->Register
.Indirect
) {
738 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
739 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
740 addr
= LLVMBuildLoad(bld
->base
.builder
,
741 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
743 /* for indexing we want integers */
744 addr
= LLVMBuildFPToSI(bld
->base
.builder
, addr
,
746 addr
= LLVMBuildExtractElement(bld
->base
.builder
,
747 addr
, LLVMConstInt(LLVMInt32Type(), 0, 0),
749 addr
= lp_build_mul(&bld
->base
, addr
, LLVMConstInt(LLVMInt32Type(), 4, 0));
752 switch( reg
->Register
.File
) {
753 case TGSI_FILE_OUTPUT
:
754 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
755 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
758 case TGSI_FILE_TEMPORARY
:
759 if (reg
->Register
.Indirect
) {
760 /* XXX not done yet */
761 debug_printf("WARNING: LLVM scatter store of temp regs"
762 " not implemented\n");
765 LLVMValueRef temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
767 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, temp_ptr
);
771 case TGSI_FILE_ADDRESS
:
772 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
773 bld
->addr
[reg
->Indirect
.Index
][chan_index
]);
776 case TGSI_FILE_PREDICATE
:
777 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
778 bld
->preds
[index
][chan_index
]);
788 * High-level instruction translators.
792 TEX_MODIFIER_NONE
= 0,
793 TEX_MODIFIER_PROJECTED
,
794 TEX_MODIFIER_LOD_BIAS
,
795 TEX_MODIFIER_EXPLICIT_LOD
,
796 TEX_MODIFIER_EXPLICIT_DERIV
800 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
801 const struct tgsi_full_instruction
*inst
,
802 enum tex_modifier modifier
,
806 LLVMValueRef lod_bias
, explicit_lod
;
807 LLVMValueRef oow
= NULL
;
808 LLVMValueRef coords
[3];
815 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
816 for (i
= 0; i
< 4; i
++) {
817 texel
[i
] = bld
->base
.undef
;
822 switch (inst
->Texture
.Texture
) {
823 case TGSI_TEXTURE_1D
:
826 case TGSI_TEXTURE_2D
:
827 case TGSI_TEXTURE_RECT
:
830 case TGSI_TEXTURE_SHADOW1D
:
831 case TGSI_TEXTURE_SHADOW2D
:
832 case TGSI_TEXTURE_SHADOWRECT
:
833 case TGSI_TEXTURE_3D
:
834 case TGSI_TEXTURE_CUBE
:
842 if (modifier
== TEX_MODIFIER_LOD_BIAS
) {
843 lod_bias
= emit_fetch( bld
, inst
, 0, 3 );
846 else if (modifier
== TEX_MODIFIER_EXPLICIT_LOD
) {
848 explicit_lod
= emit_fetch( bld
, inst
, 0, 3 );
855 if (modifier
== TEX_MODIFIER_PROJECTED
) {
856 oow
= emit_fetch( bld
, inst
, 0, 3 );
857 oow
= lp_build_rcp(&bld
->base
, oow
);
860 for (i
= 0; i
< num_coords
; i
++) {
861 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
862 if (modifier
== TEX_MODIFIER_PROJECTED
)
863 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
865 for (i
= num_coords
; i
< 3; i
++) {
866 coords
[i
] = bld
->base
.undef
;
869 if (modifier
== TEX_MODIFIER_EXPLICIT_DERIV
) {
870 for (i
= 0; i
< num_coords
; i
++) {
871 ddx
[i
] = emit_fetch( bld
, inst
, 1, i
);
872 ddy
[i
] = emit_fetch( bld
, inst
, 2, i
);
874 unit
= inst
->Src
[3].Register
.Index
;
876 for (i
= 0; i
< num_coords
; i
++) {
877 ddx
[i
] = lp_build_ddx( &bld
->base
, coords
[i
] );
878 ddy
[i
] = lp_build_ddy( &bld
->base
, coords
[i
] );
880 unit
= inst
->Src
[1].Register
.Index
;
882 for (i
= num_coords
; i
< 3; i
++) {
883 ddx
[i
] = bld
->base
.undef
;
884 ddy
[i
] = bld
->base
.undef
;
887 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
890 unit
, num_coords
, coords
,
892 lod_bias
, explicit_lod
,
898 * Kill fragment if any of the src register values are negative.
902 struct lp_build_tgsi_soa_context
*bld
,
903 const struct tgsi_full_instruction
*inst
)
905 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
906 LLVMValueRef terms
[NUM_CHANNELS
];
910 memset(&terms
, 0, sizeof terms
);
912 FOR_EACH_CHANNEL( chan_index
) {
915 /* Unswizzle channel */
916 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
918 /* Check if the component has not been already tested. */
919 assert(swizzle
< NUM_CHANNELS
);
920 if( !terms
[swizzle
] )
921 /* TODO: change the comparison operator instead of setting the sign */
922 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
926 FOR_EACH_CHANNEL( chan_index
) {
927 if(terms
[chan_index
]) {
928 LLVMValueRef chan_mask
;
931 * If term < 0 then mask = 0 else mask = ~0.
933 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
936 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
943 lp_build_mask_update(bld
->mask
, mask
);
948 * Predicated fragment kill.
949 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
950 * The only predication is the execution mask which will apply if
951 * we're inside a loop or conditional.
954 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
955 const struct tgsi_full_instruction
*inst
)
959 /* For those channels which are "alive", disable fragment shader
962 if (bld
->exec_mask
.has_mask
) {
963 mask
= LLVMBuildNot(bld
->base
.builder
, bld
->exec_mask
.exec_mask
, "kilp");
966 mask
= bld
->base
.zero
;
969 lp_build_mask_update(bld
->mask
, mask
);
974 struct lp_build_tgsi_soa_context
*bld
,
975 const struct tgsi_full_declaration
*decl
)
977 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
979 unsigned first
= decl
->Range
.First
;
980 unsigned last
= decl
->Range
.Last
;
983 for (idx
= first
; idx
<= last
; ++idx
) {
984 switch (decl
->Declaration
.File
) {
985 case TGSI_FILE_TEMPORARY
:
986 assert(idx
< LP_MAX_TGSI_TEMPS
);
987 if (bld
->has_indirect_addressing
) {
988 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
990 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
991 vec_type
, array_size
, "");
993 for (i
= 0; i
< NUM_CHANNELS
; i
++)
994 bld
->temps
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
999 case TGSI_FILE_OUTPUT
:
1000 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1001 bld
->outputs
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1005 case TGSI_FILE_ADDRESS
:
1006 assert(idx
< LP_MAX_TGSI_ADDRS
);
1007 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1008 bld
->addr
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1012 case TGSI_FILE_PREDICATE
:
1013 assert(idx
< LP_MAX_TGSI_PREDS
);
1014 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1015 bld
->preds
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1020 /* don't need to declare other vars */
1028 * Emit LLVM for one TGSI instruction.
1029 * \param return TRUE for success, FALSE otherwise
1033 struct lp_build_tgsi_soa_context
*bld
,
1034 const struct tgsi_full_instruction
*inst
,
1035 const struct tgsi_opcode_info
*info
,
1038 unsigned chan_index
;
1039 LLVMValueRef src0
, src1
, src2
;
1040 LLVMValueRef tmp0
, tmp1
, tmp2
;
1041 LLVMValueRef tmp3
= NULL
;
1042 LLVMValueRef tmp4
= NULL
;
1043 LLVMValueRef tmp5
= NULL
;
1044 LLVMValueRef tmp6
= NULL
;
1045 LLVMValueRef tmp7
= NULL
;
1047 LLVMValueRef dst0
[NUM_CHANNELS
];
1050 * Stores and write masks are handled in a general fashion after the long
1051 * instruction opcode switch statement.
1053 * Although not stricitly necessary, we avoid generating instructions for
1054 * channels which won't be stored, in cases where's that easy. For some
1055 * complex instructions, like texture sampling, it is more convenient to
1056 * assume a full writemask and then let LLVM optimization passes eliminate
1062 assert(info
->num_dst
<= 1);
1063 if (info
->num_dst
) {
1064 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1065 dst0
[chan_index
] = bld
->base
.undef
;
1069 switch (inst
->Instruction
.Opcode
) {
1070 case TGSI_OPCODE_ARL
:
1071 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1072 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1073 tmp0
= lp_build_floor(&bld
->base
, tmp0
);
1074 dst0
[chan_index
] = tmp0
;
1078 case TGSI_OPCODE_MOV
:
1079 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1080 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
1084 case TGSI_OPCODE_LIT
:
1085 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
1086 dst0
[CHAN_X
] = bld
->base
.one
;
1088 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1089 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1090 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
1092 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1093 /* XMM[1] = SrcReg[0].yyyy */
1094 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1095 /* XMM[1] = max(XMM[1], 0) */
1096 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
1097 /* XMM[2] = SrcReg[0].wwww */
1098 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1099 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
1100 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1101 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
1102 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
1104 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
1105 dst0
[CHAN_W
] = bld
->base
.one
;
1109 case TGSI_OPCODE_RCP
:
1110 /* TGSI_OPCODE_RECIP */
1111 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1112 res
= lp_build_rcp(&bld
->base
, src0
);
1113 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1114 dst0
[chan_index
] = res
;
1118 case TGSI_OPCODE_RSQ
:
1119 /* TGSI_OPCODE_RECIPSQRT */
1120 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1121 src0
= lp_build_abs(&bld
->base
, src0
);
1122 res
= lp_build_rsqrt(&bld
->base
, src0
);
1123 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1124 dst0
[chan_index
] = res
;
1128 case TGSI_OPCODE_EXP
:
1129 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1130 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1131 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1132 LLVMValueRef
*p_exp2_int_part
= NULL
;
1133 LLVMValueRef
*p_frac_part
= NULL
;
1134 LLVMValueRef
*p_exp2
= NULL
;
1136 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1138 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1139 p_exp2_int_part
= &tmp0
;
1140 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1141 p_frac_part
= &tmp1
;
1142 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1145 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
1147 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1148 dst0
[CHAN_X
] = tmp0
;
1149 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1150 dst0
[CHAN_Y
] = tmp1
;
1151 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1152 dst0
[CHAN_Z
] = tmp2
;
1155 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1156 dst0
[CHAN_W
] = bld
->base
.one
;
1160 case TGSI_OPCODE_LOG
:
1161 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1162 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1163 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1164 LLVMValueRef
*p_floor_log2
= NULL
;
1165 LLVMValueRef
*p_exp
= NULL
;
1166 LLVMValueRef
*p_log2
= NULL
;
1168 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1169 src0
= lp_build_abs( &bld
->base
, src0
);
1171 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1172 p_floor_log2
= &tmp0
;
1173 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1175 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1178 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
1180 /* dst.x = floor(lg2(abs(src.x))) */
1181 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1182 dst0
[CHAN_X
] = tmp0
;
1183 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1184 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
1185 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
1187 /* dst.z = lg2(abs(src.x)) */
1188 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1189 dst0
[CHAN_Z
] = tmp2
;
1192 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1193 dst0
[CHAN_W
] = bld
->base
.one
;
1197 case TGSI_OPCODE_MUL
:
1198 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1199 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1200 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1201 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
1205 case TGSI_OPCODE_ADD
:
1206 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1207 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1208 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1209 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
1213 case TGSI_OPCODE_DP3
:
1214 /* TGSI_OPCODE_DOT3 */
1215 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1216 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1217 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1218 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1219 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1220 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1221 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1222 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1223 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1224 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1225 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1226 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1227 dst0
[chan_index
] = tmp0
;
1231 case TGSI_OPCODE_DP4
:
1232 /* TGSI_OPCODE_DOT4 */
1233 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1234 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1235 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1236 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1237 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1238 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1239 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1240 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1241 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1242 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1243 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1244 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1245 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1246 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1247 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1248 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1249 dst0
[chan_index
] = tmp0
;
1253 case TGSI_OPCODE_DST
:
1254 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1255 dst0
[CHAN_X
] = bld
->base
.one
;
1257 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1258 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1259 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1260 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1262 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1263 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
1265 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1266 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
1270 case TGSI_OPCODE_MIN
:
1271 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1272 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1273 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1274 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
1278 case TGSI_OPCODE_MAX
:
1279 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1280 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1281 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1282 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
1286 case TGSI_OPCODE_SLT
:
1287 /* TGSI_OPCODE_SETLT */
1288 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1289 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1290 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1291 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
1292 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1296 case TGSI_OPCODE_SGE
:
1297 /* TGSI_OPCODE_SETGE */
1298 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1299 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1300 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1301 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1302 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1306 case TGSI_OPCODE_MAD
:
1307 /* TGSI_OPCODE_MADD */
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 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1312 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1313 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1314 dst0
[chan_index
] = tmp0
;
1318 case TGSI_OPCODE_SUB
:
1319 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1320 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1321 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1322 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1326 case TGSI_OPCODE_LRP
:
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 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1332 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1333 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1337 case TGSI_OPCODE_CND
:
1338 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1339 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1340 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1341 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1342 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
1343 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1344 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1348 case TGSI_OPCODE_DP2A
:
1349 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1350 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1351 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1352 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1353 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1354 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1355 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1356 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1357 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1358 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1359 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1363 case TGSI_OPCODE_FRC
:
1364 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1365 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1366 tmp0
= lp_build_floor(&bld
->base
, src0
);
1367 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1368 dst0
[chan_index
] = tmp0
;
1372 case TGSI_OPCODE_CLAMP
:
1373 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1374 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1375 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1376 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1377 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1378 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1379 dst0
[chan_index
] = tmp0
;
1383 case TGSI_OPCODE_FLR
:
1384 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1385 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1386 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1390 case TGSI_OPCODE_ROUND
:
1391 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1392 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1393 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1397 case TGSI_OPCODE_EX2
: {
1398 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1399 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1400 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1401 dst0
[chan_index
] = tmp0
;
1406 case TGSI_OPCODE_LG2
:
1407 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1408 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1409 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1410 dst0
[chan_index
] = tmp0
;
1414 case TGSI_OPCODE_POW
:
1415 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1416 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1417 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1418 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1419 dst0
[chan_index
] = res
;
1423 case TGSI_OPCODE_XPD
:
1424 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1425 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1426 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1427 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1429 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1430 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1431 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1432 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1434 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1436 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1438 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1439 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1440 dst0
[CHAN_X
] = tmp2
;
1442 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1443 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1444 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1445 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1447 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1448 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1449 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1450 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1451 dst0
[CHAN_Y
] = tmp3
;
1453 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1454 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1455 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1456 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1457 dst0
[CHAN_Z
] = tmp5
;
1459 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1460 dst0
[CHAN_W
] = bld
->base
.one
;
1464 case TGSI_OPCODE_ABS
:
1465 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1466 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1467 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1471 case TGSI_OPCODE_RCC
:
1476 case TGSI_OPCODE_DPH
:
1477 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1478 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1479 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1480 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1481 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1482 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1483 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1484 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1485 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1486 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1487 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1488 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1489 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1490 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1491 dst0
[chan_index
] = tmp0
;
1495 case TGSI_OPCODE_COS
:
1496 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1497 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1498 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1499 dst0
[chan_index
] = tmp0
;
1503 case TGSI_OPCODE_DDX
:
1504 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1505 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1509 case TGSI_OPCODE_DDY
:
1510 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1511 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1515 case TGSI_OPCODE_KILP
:
1516 /* predicated kill */
1517 emit_kilp( bld
, inst
);
1520 case TGSI_OPCODE_KIL
:
1521 /* conditional kill */
1522 emit_kil( bld
, inst
);
1525 case TGSI_OPCODE_PK2H
:
1529 case TGSI_OPCODE_PK2US
:
1533 case TGSI_OPCODE_PK4B
:
1537 case TGSI_OPCODE_PK4UB
:
1541 case TGSI_OPCODE_RFL
:
1545 case TGSI_OPCODE_SEQ
:
1546 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1547 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1548 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1549 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1550 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1554 case TGSI_OPCODE_SFL
:
1555 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1556 dst0
[chan_index
] = bld
->base
.zero
;
1560 case TGSI_OPCODE_SGT
:
1561 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1562 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1563 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1564 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1565 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1569 case TGSI_OPCODE_SIN
:
1570 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1571 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1572 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1573 dst0
[chan_index
] = tmp0
;
1577 case TGSI_OPCODE_SLE
:
1578 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1579 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1580 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1581 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1582 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1586 case TGSI_OPCODE_SNE
:
1587 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1588 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1589 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1590 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1591 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1595 case TGSI_OPCODE_STR
:
1596 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1597 dst0
[chan_index
] = bld
->base
.one
;
1601 case TGSI_OPCODE_TEX
:
1602 emit_tex( bld
, inst
, TEX_MODIFIER_NONE
, dst0
);
1605 case TGSI_OPCODE_TXD
:
1606 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_DERIV
, dst0
);
1609 case TGSI_OPCODE_UP2H
:
1615 case TGSI_OPCODE_UP2US
:
1621 case TGSI_OPCODE_UP4B
:
1627 case TGSI_OPCODE_UP4UB
:
1633 case TGSI_OPCODE_X2D
:
1639 case TGSI_OPCODE_ARA
:
1645 case TGSI_OPCODE_ARR
:
1646 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1647 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1648 tmp0
= lp_build_round(&bld
->base
, tmp0
);
1649 dst0
[chan_index
] = tmp0
;
1653 case TGSI_OPCODE_BRA
:
1659 case TGSI_OPCODE_CAL
:
1660 lp_exec_mask_call(&bld
->exec_mask
,
1666 case TGSI_OPCODE_RET
:
1667 lp_exec_mask_ret(&bld
->exec_mask
, pc
);
1670 case TGSI_OPCODE_END
:
1674 case TGSI_OPCODE_SSG
:
1675 /* TGSI_OPCODE_SGN */
1676 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1677 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1678 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1682 case TGSI_OPCODE_CMP
:
1683 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1684 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1685 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1686 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1687 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1688 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1692 case TGSI_OPCODE_SCS
:
1693 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1694 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1695 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1697 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1698 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1699 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1701 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1702 dst0
[CHAN_Z
] = bld
->base
.zero
;
1704 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1705 dst0
[CHAN_W
] = bld
->base
.one
;
1709 case TGSI_OPCODE_TXB
:
1710 emit_tex( bld
, inst
, TEX_MODIFIER_LOD_BIAS
, dst0
);
1713 case TGSI_OPCODE_NRM
:
1715 case TGSI_OPCODE_NRM4
:
1716 /* 3 or 4-component normalization */
1718 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1720 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1721 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1722 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1723 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1725 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1728 /* xmm0 = src.x * src.x */
1729 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1730 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1733 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1736 /* xmm0 = xmm0 + src.y * src.y */
1737 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1738 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1741 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1742 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1745 /* xmm0 = xmm0 + src.z * src.z */
1746 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1747 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1750 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1751 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1755 /* xmm0 = xmm0 + src.w * src.w */
1756 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1757 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1760 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1761 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1764 /* xmm1 = 1 / sqrt(xmm0) */
1765 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1767 /* dst.x = xmm1 * src.x */
1768 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1769 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1772 /* dst.y = xmm1 * src.y */
1773 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1774 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1777 /* dst.z = xmm1 * src.z */
1778 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1779 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1782 /* dst.w = xmm1 * src.w */
1783 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1784 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1789 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1790 dst0
[CHAN_W
] = bld
->base
.one
;
1795 case TGSI_OPCODE_DIV
:
1801 case TGSI_OPCODE_DP2
:
1802 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1803 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1804 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1805 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1806 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1807 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1808 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1809 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1810 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1814 case TGSI_OPCODE_TXL
:
1815 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_LOD
, dst0
);
1818 case TGSI_OPCODE_TXP
:
1819 emit_tex( bld
, inst
, TEX_MODIFIER_PROJECTED
, dst0
);
1822 case TGSI_OPCODE_BRK
:
1823 lp_exec_break(&bld
->exec_mask
);
1826 case TGSI_OPCODE_IF
:
1827 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1828 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
1829 tmp0
, bld
->base
.zero
);
1830 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1833 case TGSI_OPCODE_BGNLOOP
:
1834 lp_exec_bgnloop(&bld
->exec_mask
);
1837 case TGSI_OPCODE_BGNSUB
:
1838 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1841 case TGSI_OPCODE_ELSE
:
1842 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1845 case TGSI_OPCODE_ENDIF
:
1846 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1849 case TGSI_OPCODE_ENDLOOP
:
1850 lp_exec_endloop(&bld
->exec_mask
);
1853 case TGSI_OPCODE_ENDSUB
:
1854 lp_exec_mask_endsub(&bld
->exec_mask
, pc
);
1857 case TGSI_OPCODE_PUSHA
:
1863 case TGSI_OPCODE_POPA
:
1869 case TGSI_OPCODE_CEIL
:
1870 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1871 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1872 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1876 case TGSI_OPCODE_I2F
:
1882 case TGSI_OPCODE_NOT
:
1888 case TGSI_OPCODE_TRUNC
:
1889 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1890 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1891 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1895 case TGSI_OPCODE_SHL
:
1901 case TGSI_OPCODE_ISHR
:
1907 case TGSI_OPCODE_AND
:
1913 case TGSI_OPCODE_OR
:
1919 case TGSI_OPCODE_MOD
:
1925 case TGSI_OPCODE_XOR
:
1931 case TGSI_OPCODE_SAD
:
1937 case TGSI_OPCODE_TXF
:
1943 case TGSI_OPCODE_TXQ
:
1949 case TGSI_OPCODE_CONT
:
1950 lp_exec_continue(&bld
->exec_mask
);
1953 case TGSI_OPCODE_EMIT
:
1957 case TGSI_OPCODE_ENDPRIM
:
1961 case TGSI_OPCODE_NOP
:
1969 LLVMValueRef pred
[NUM_CHANNELS
];
1971 emit_fetch_predicate( bld
, inst
, pred
);
1973 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1974 emit_store( bld
, inst
, 0, chan_index
, pred
[chan_index
], dst0
[chan_index
]);
1983 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1984 const struct tgsi_token
*tokens
,
1985 struct lp_type type
,
1986 struct lp_build_mask_context
*mask
,
1987 LLVMValueRef consts_ptr
,
1988 const LLVMValueRef
*pos
,
1989 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
1990 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1991 struct lp_build_sampler_soa
*sampler
,
1992 const struct tgsi_shader_info
*info
)
1994 struct lp_build_tgsi_soa_context bld
;
1995 struct tgsi_parse_context parse
;
1996 uint num_immediates
= 0;
1997 uint num_instructions
= 0;
2001 /* Setup build context */
2002 memset(&bld
, 0, sizeof bld
);
2003 lp_build_context_init(&bld
.base
, builder
, type
);
2004 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
2007 bld
.inputs
= inputs
;
2008 bld
.outputs
= outputs
;
2009 bld
.consts_ptr
= consts_ptr
;
2010 bld
.sampler
= sampler
;
2011 bld
.has_indirect_addressing
= info
->opcode_count
[TGSI_OPCODE_ARR
] > 0 ||
2012 info
->opcode_count
[TGSI_OPCODE_ARL
] > 0;
2013 bld
.instructions
= (struct tgsi_full_instruction
*)
2014 MALLOC( LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
) );
2015 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
2017 if (!bld
.instructions
) {
2021 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
2023 tgsi_parse_init( &parse
, tokens
);
2025 while( !tgsi_parse_end_of_tokens( &parse
) ) {
2026 tgsi_parse_token( &parse
);
2028 switch( parse
.FullToken
.Token
.Type
) {
2029 case TGSI_TOKEN_TYPE_DECLARATION
:
2030 /* Inputs already interpolated */
2031 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
2034 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2036 /* save expanded instruction */
2037 if (num_instructions
== bld
.max_instructions
) {
2038 bld
.instructions
= REALLOC(bld
.instructions
,
2039 bld
.max_instructions
2040 * sizeof(struct tgsi_full_instruction
),
2041 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
2042 * sizeof(struct tgsi_full_instruction
));
2043 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
2046 memcpy(bld
.instructions
+ num_instructions
,
2047 &parse
.FullToken
.FullInstruction
,
2048 sizeof(bld
.instructions
[0]));
2055 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2056 /* simply copy the immediate values into the next immediates[] slot */
2058 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
2060 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
2061 for( i
= 0; i
< size
; ++i
)
2062 bld
.immediates
[num_immediates
][i
] =
2063 lp_build_const_vec(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
2064 for( i
= size
; i
< 4; ++i
)
2065 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
2070 case TGSI_TOKEN_TYPE_PROPERTY
:
2079 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
2080 const struct tgsi_opcode_info
*opcode_info
=
2081 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
2082 if (!emit_instruction( &bld
, instr
, opcode_info
, &pc
))
2083 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2084 opcode_info
->mnemonic
);
2088 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
2089 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2090 debug_printf("11111111111111111111111111111 \n");
2091 tgsi_dump(tokens
, 0);
2092 lp_debug_dump_value(function
);
2093 debug_printf("2222222222222222222222222222 \n");
2095 tgsi_parse_free( &parse
);
2098 LLVMModuleRef module
= LLVMGetGlobalParent(
2099 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
2100 LLVMDumpModule(module
);
2104 FREE( bld
.instructions
);