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_exec.h"
49 #include "lp_bld_type.h"
50 #include "lp_bld_const.h"
51 #include "lp_bld_arit.h"
52 #include "lp_bld_logic.h"
53 #include "lp_bld_swizzle.h"
54 #include "lp_bld_flow.h"
55 #include "lp_bld_tgsi.h"
56 #include "lp_bld_debug.h"
59 #define LP_MAX_TEMPS 256
60 #define LP_MAX_IMMEDIATES 256
63 #define FOR_EACH_CHANNEL( CHAN )\
64 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
66 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
67 ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
69 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
70 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
72 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
73 FOR_EACH_CHANNEL( CHAN )\
74 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
81 #define QUAD_TOP_LEFT 0
82 #define QUAD_TOP_RIGHT 1
83 #define QUAD_BOTTOM_LEFT 2
84 #define QUAD_BOTTOM_RIGHT 3
86 #define LP_TGSI_MAX_NESTING 16
89 struct lp_build_context
*bld
;
93 LLVMTypeRef int_vec_type
;
95 LLVMValueRef cond_stack
[LP_TGSI_MAX_NESTING
];
97 LLVMValueRef cond_mask
;
99 LLVMValueRef break_stack
[LP_TGSI_MAX_NESTING
];
100 int break_stack_size
;
101 LLVMValueRef break_mask
;
103 LLVMValueRef cont_stack
[LP_TGSI_MAX_NESTING
];
105 LLVMValueRef cont_mask
;
107 LLVMBasicBlockRef loop_stack
[LP_TGSI_MAX_NESTING
];
109 LLVMBasicBlockRef loop_block
;
112 LLVMValueRef exec_mask
;
115 struct lp_build_tgsi_soa_context
117 struct lp_build_context base
;
119 LLVMValueRef consts_ptr
;
120 const LLVMValueRef
*pos
;
121 const LLVMValueRef (*inputs
)[NUM_CHANNELS
];
122 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
124 struct lp_build_sampler_soa
*sampler
;
126 LLVMValueRef immediates
[LP_MAX_IMMEDIATES
][NUM_CHANNELS
];
127 LLVMValueRef temps
[LP_MAX_TEMPS
][NUM_CHANNELS
];
129 struct lp_build_mask_context
*mask
;
130 struct lp_exec_mask exec_mask
;
133 static const unsigned char
135 QUAD_TOP_LEFT
, QUAD_TOP_LEFT
,
136 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_LEFT
139 static const unsigned char
141 QUAD_TOP_RIGHT
, QUAD_TOP_RIGHT
,
142 QUAD_BOTTOM_RIGHT
, QUAD_BOTTOM_RIGHT
145 static const unsigned char
147 QUAD_TOP_LEFT
, QUAD_TOP_RIGHT
,
148 QUAD_TOP_LEFT
, QUAD_TOP_RIGHT
151 static const unsigned char
152 swizzle_bottom
[4] = {
153 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_RIGHT
,
154 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_RIGHT
157 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
160 mask
->has_mask
= FALSE
;
161 mask
->cond_stack_size
= 0;
162 mask
->loop_stack_size
= 0;
163 mask
->break_stack_size
= 0;
164 mask
->cont_stack_size
= 0;
166 mask
->int_vec_type
= lp_build_int_vec_type(mask
->bld
->type
);
169 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
171 if (mask
->loop_stack_size
) {
172 /*for loops we need to update the entire mask at
175 assert(mask
->break_mask
);
176 tmp
= LLVMBuildAnd(mask
->bld
->builder
,
180 mask
->exec_mask
= LLVMBuildAnd(mask
->bld
->builder
,
185 mask
->exec_mask
= mask
->cond_mask
;
188 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
189 mask
->loop_stack_size
> 0);
192 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
195 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
196 mask
->cond_mask
= LLVMBuildBitCast(mask
->bld
->builder
, val
,
197 mask
->int_vec_type
, "");
199 lp_exec_mask_update(mask
);
202 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
204 LLVMValueRef prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
205 LLVMValueRef inv_mask
= LLVMBuildNot(mask
->bld
->builder
,
206 mask
->cond_mask
, "");
208 /* means that we didn't have any mask before and that
209 * we were fully enabled */
210 if (mask
->cond_stack_size
<= 1) {
211 prev_mask
= LLVMConstAllOnes(mask
->int_vec_type
);
214 mask
->cond_mask
= LLVMBuildAnd(mask
->bld
->builder
,
217 lp_exec_mask_update(mask
);
220 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
222 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
223 lp_exec_mask_update(mask
);
226 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
229 if (mask
->cont_stack_size
== 0)
230 mask
->cont_mask
= LLVMConstAllOnes(mask
->int_vec_type
);
231 if (mask
->break_stack_size
== 0)
232 mask
->break_mask
= LLVMConstAllOnes(mask
->int_vec_type
);
233 if (mask
->cond_stack_size
== 0)
234 mask
->cond_mask
= LLVMConstAllOnes(mask
->int_vec_type
);
235 mask
->loop_stack
[mask
->loop_stack_size
++] = mask
->loop_block
;
236 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->builder
, "bgnloop");
237 LLVMBuildBr(mask
->bld
->builder
, mask
->loop_block
);
238 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, mask
->loop_block
);
240 lp_exec_mask_update(mask
);
243 static void lp_exec_break(struct lp_exec_mask
*mask
)
245 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
249 mask
->break_stack
[mask
->break_stack_size
++] = mask
->break_mask
;
250 if (mask
->break_stack_size
> 1) {
251 mask
->break_mask
= LLVMBuildAnd(mask
->bld
->builder
,
253 exec_mask
, "break_full");
255 mask
->break_mask
= exec_mask
;
257 lp_exec_mask_update(mask
);
260 static void lp_exec_continue(struct lp_exec_mask
*mask
)
262 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
266 mask
->cont_stack
[mask
->cont_stack_size
++] = mask
->cont_mask
;
267 if (mask
->cont_stack_size
> 1) {
268 mask
->cont_mask
= LLVMBuildAnd(mask
->bld
->builder
,
272 mask
->cont_mask
= exec_mask
;
274 lp_exec_mask_update(mask
);
278 static void lp_exec_endloop(struct lp_exec_mask
*mask
)
280 LLVMBasicBlockRef endloop
;
281 LLVMTypeRef reg_type
= LLVMIntType(mask
->bld
->type
.width
*
282 mask
->bld
->type
.length
);
285 assert(mask
->break_mask
);
287 /* i1cond = (mask == 0) */
288 i1cond
= LLVMBuildICmp(
291 LLVMBuildBitCast(mask
->bld
->builder
, mask
->break_mask
, reg_type
, ""),
292 LLVMConstNull(reg_type
), "");
294 endloop
= lp_build_insert_new_block(mask
->bld
->builder
, "endloop");
296 LLVMBuildCondBr(mask
->bld
->builder
,
297 i1cond
, mask
->loop_block
, endloop
);
299 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, endloop
);
301 mask
->loop_block
= mask
->loop_stack
[--mask
->loop_stack_size
];
302 /* pop the break mask */
303 if (mask
->cont_stack_size
) {
304 mask
->cont_mask
= mask
->cont_stack
[--mask
->cont_stack_size
];
306 if (mask
->break_stack_size
) {
307 mask
->break_mask
= mask
->cont_stack
[--mask
->break_stack_size
];
310 lp_exec_mask_update(mask
);
313 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
317 if (mask
->has_mask
) {
318 LLVMValueRef real_val
, dst_val
;
320 dst_val
= LLVMBuildLoad(mask
->bld
->builder
, dst
, "");
321 real_val
= lp_build_select(mask
->bld
,
325 LLVMBuildStore(mask
->bld
->builder
, real_val
, dst
);
327 LLVMBuildStore(mask
->bld
->builder
, val
, dst
);
332 emit_ddx(struct lp_build_tgsi_soa_context
*bld
,
335 LLVMValueRef src_left
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_left
);
336 LLVMValueRef src_right
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_right
);
337 return lp_build_sub(&bld
->base
, src_right
, src_left
);
342 emit_ddy(struct lp_build_tgsi_soa_context
*bld
,
345 LLVMValueRef src_top
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_top
);
346 LLVMValueRef src_bottom
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_bottom
);
347 return lp_build_sub(&bld
->base
, src_top
, src_bottom
);
356 struct lp_build_tgsi_soa_context
*bld
,
357 const struct tgsi_full_instruction
*inst
,
359 const unsigned chan_index
)
361 const struct tgsi_full_src_register
*reg
= &inst
->Src
[index
];
362 unsigned swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
371 switch (reg
->Register
.File
) {
372 case TGSI_FILE_CONSTANT
: {
373 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), reg
->Register
.Index
*4 + swizzle
, 0);
374 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
, &index
, 1, "");
375 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
376 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
380 case TGSI_FILE_IMMEDIATE
:
381 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
385 case TGSI_FILE_INPUT
:
386 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
390 case TGSI_FILE_TEMPORARY
:
391 res
= LLVMBuildLoad(bld
->base
.builder
, bld
->temps
[reg
->Register
.Index
][swizzle
], "");
393 return bld
->base
.undef
;
398 return bld
->base
.undef
;
404 return bld
->base
.undef
;
407 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
408 case TGSI_UTIL_SIGN_CLEAR
:
409 res
= lp_build_abs( &bld
->base
, res
);
412 case TGSI_UTIL_SIGN_SET
:
413 /* TODO: Use bitwese OR for floating point */
414 res
= lp_build_abs( &bld
->base
, res
);
415 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
418 case TGSI_UTIL_SIGN_TOGGLE
:
419 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
422 case TGSI_UTIL_SIGN_KEEP
:
431 * Register fetch with derivatives.
435 struct lp_build_tgsi_soa_context
*bld
,
436 const struct tgsi_full_instruction
*inst
,
438 const unsigned chan_index
,
445 src
= emit_fetch(bld
, inst
, index
, chan_index
);
450 /* TODO: use interpolation coeffs for inputs */
453 *ddx
= emit_ddx(bld
, src
);
456 *ddy
= emit_ddy(bld
, src
);
465 struct lp_build_tgsi_soa_context
*bld
,
466 const struct tgsi_full_instruction
*inst
,
471 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
473 switch( inst
->Instruction
.Saturate
) {
477 case TGSI_SAT_ZERO_ONE
:
478 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
479 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
482 case TGSI_SAT_MINUS_PLUS_ONE
:
483 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
484 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
491 switch( reg
->Register
.File
) {
492 case TGSI_FILE_OUTPUT
:
493 lp_exec_mask_store(&bld
->exec_mask
, value
,
494 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
497 case TGSI_FILE_TEMPORARY
:
498 lp_exec_mask_store(&bld
->exec_mask
, value
,
499 bld
->temps
[reg
->Register
.Index
][chan_index
]);
502 case TGSI_FILE_ADDRESS
:
507 case TGSI_FILE_PREDICATE
:
519 * High-level instruction translators.
524 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
525 const struct tgsi_full_instruction
*inst
,
526 boolean apply_lodbias
,
530 const uint unit
= inst
->Src
[1].Register
.Index
;
531 LLVMValueRef lodbias
;
532 LLVMValueRef oow
= NULL
;
533 LLVMValueRef coords
[3];
537 switch (inst
->Texture
.Texture
) {
538 case TGSI_TEXTURE_1D
:
541 case TGSI_TEXTURE_2D
:
542 case TGSI_TEXTURE_RECT
:
545 case TGSI_TEXTURE_SHADOW1D
:
546 case TGSI_TEXTURE_SHADOW2D
:
547 case TGSI_TEXTURE_SHADOWRECT
:
548 case TGSI_TEXTURE_3D
:
549 case TGSI_TEXTURE_CUBE
:
558 lodbias
= emit_fetch( bld
, inst
, 0, 3 );
560 lodbias
= bld
->base
.zero
;
563 oow
= emit_fetch( bld
, inst
, 0, 3 );
564 oow
= lp_build_rcp(&bld
->base
, oow
);
567 for (i
= 0; i
< num_coords
; i
++) {
568 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
570 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
572 for (i
= num_coords
; i
< 3; i
++) {
573 coords
[i
] = bld
->base
.undef
;
576 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
579 unit
, num_coords
, coords
, lodbias
,
585 * Kill fragment if any of the src register values are negative.
589 struct lp_build_tgsi_soa_context
*bld
,
590 const struct tgsi_full_instruction
*inst
)
592 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
593 LLVMValueRef terms
[NUM_CHANNELS
];
597 memset(&terms
, 0, sizeof terms
);
599 FOR_EACH_CHANNEL( chan_index
) {
602 /* Unswizzle channel */
603 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
605 /* Check if the component has not been already tested. */
606 assert(swizzle
< NUM_CHANNELS
);
607 if( !terms
[swizzle
] )
608 /* TODO: change the comparison operator instead of setting the sign */
609 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
613 FOR_EACH_CHANNEL( chan_index
) {
614 if(terms
[chan_index
]) {
615 LLVMValueRef chan_mask
;
618 * If term < 0 then mask = 0 else mask = ~0.
620 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
623 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
630 lp_build_mask_update(bld
->mask
, mask
);
635 * Predicated fragment kill.
636 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
637 * The only predication is the execution mask which will apply if
638 * we're inside a loop or conditional.
641 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
642 const struct tgsi_full_instruction
*inst
)
646 /* For those channels which are "alive", disable fragment shader
649 if (bld
->exec_mask
.has_mask
) {
650 mask
= LLVMBuildNot(bld
->base
.builder
, bld
->exec_mask
.exec_mask
, "kilp");
653 mask
= bld
->base
.zero
;
656 lp_build_mask_update(bld
->mask
, mask
);
661 * Check if inst src/dest regs use indirect addressing into temporary
665 indirect_temp_reference(const struct tgsi_full_instruction
*inst
)
668 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
669 const struct tgsi_full_src_register
*reg
= &inst
->Src
[i
];
670 if (reg
->Register
.File
== TGSI_FILE_TEMPORARY
&&
671 reg
->Register
.Indirect
)
674 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
675 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[i
];
676 if (reg
->Register
.File
== TGSI_FILE_TEMPORARY
&&
677 reg
->Register
.Indirect
)
685 struct lp_build_tgsi_soa_context
*bld
,
686 const struct tgsi_full_declaration
*decl
)
688 unsigned first
= decl
->Range
.First
;
689 unsigned last
= decl
->Range
.Last
;
691 LLVMBasicBlockRef current_block
=
692 LLVMGetInsertBlock(bld
->base
.builder
);
693 LLVMBasicBlockRef first_block
=
694 LLVMGetEntryBasicBlock(
695 LLVMGetBasicBlockParent(current_block
));
696 LLVMValueRef first_inst
=
697 LLVMGetFirstInstruction(first_block
);
699 /* we want alloca's to be the first instruction
700 * in the function so we need to rewind the builder
701 * to the very beginning */
702 LLVMPositionBuilderBefore(bld
->base
.builder
,
705 for (idx
= first
; idx
<= last
; ++idx
) {
706 switch (decl
->Declaration
.File
) {
707 case TGSI_FILE_TEMPORARY
:
708 for (i
= 0; i
< NUM_CHANNELS
; i
++)
709 bld
->temps
[idx
][i
] = lp_build_alloca(&bld
->base
);
712 case TGSI_FILE_OUTPUT
:
713 for (i
= 0; i
< NUM_CHANNELS
; i
++)
714 bld
->outputs
[idx
][i
] = lp_build_alloca(&bld
->base
);
718 /* don't need to declare other vars */
723 LLVMPositionBuilderAtEnd(bld
->base
.builder
,
730 * Emit LLVM for one TGSI instruction.
731 * \param return TRUE for success, FALSE otherwise
735 struct lp_build_tgsi_soa_context
*bld
,
736 const struct tgsi_full_instruction
*inst
,
737 const struct tgsi_opcode_info
*info
)
740 LLVMValueRef src0
, src1
, src2
;
741 LLVMValueRef tmp0
, tmp1
, tmp2
;
742 LLVMValueRef tmp3
= NULL
;
743 LLVMValueRef tmp4
= NULL
;
744 LLVMValueRef tmp5
= NULL
;
745 LLVMValueRef tmp6
= NULL
;
746 LLVMValueRef tmp7
= NULL
;
748 LLVMValueRef dst0
[NUM_CHANNELS
];
750 /* we can't handle indirect addressing into temp register file yet */
751 if (indirect_temp_reference(inst
))
755 * Stores and write masks are handled in a general fashion after the long
756 * instruction opcode switch statement.
758 * Although not stricitly necessary, we avoid generating instructions for
759 * channels which won't be stored, in cases where's that easy. For some
760 * complex instructions, like texture sampling, it is more convenient to
761 * assume a full writemask and then let LLVM optimization passes eliminate
765 assert(info
->num_dst
<= 1);
767 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
768 dst0
[chan_index
] = bld
->base
.undef
;
772 switch (inst
->Instruction
.Opcode
) {
774 case TGSI_OPCODE_ARL
:
776 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
777 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
780 dst0
[chan_index
] = tmp0
;
785 case TGSI_OPCODE_MOV
:
786 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
787 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
791 case TGSI_OPCODE_LIT
:
792 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
793 dst0
[CHAN_X
] = bld
->base
.one
;
795 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
796 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
797 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
799 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
800 /* XMM[1] = SrcReg[0].yyyy */
801 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
802 /* XMM[1] = max(XMM[1], 0) */
803 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
804 /* XMM[2] = SrcReg[0].wwww */
805 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
806 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
807 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
808 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
809 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
811 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
812 dst0
[CHAN_W
] = bld
->base
.one
;
816 case TGSI_OPCODE_RCP
:
817 /* TGSI_OPCODE_RECIP */
818 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
819 res
= lp_build_rcp(&bld
->base
, src0
);
820 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
821 dst0
[chan_index
] = res
;
825 case TGSI_OPCODE_RSQ
:
826 /* TGSI_OPCODE_RECIPSQRT */
827 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
828 src0
= lp_build_abs(&bld
->base
, src0
);
829 res
= lp_build_rsqrt(&bld
->base
, src0
);
830 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
831 dst0
[chan_index
] = res
;
835 case TGSI_OPCODE_EXP
:
836 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
837 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
838 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
839 LLVMValueRef
*p_exp2_int_part
= NULL
;
840 LLVMValueRef
*p_frac_part
= NULL
;
841 LLVMValueRef
*p_exp2
= NULL
;
843 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
845 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
846 p_exp2_int_part
= &tmp0
;
847 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
849 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
852 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
854 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
856 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
858 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
862 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
863 dst0
[CHAN_W
] = bld
->base
.one
;
867 case TGSI_OPCODE_LOG
:
868 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
869 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
870 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
871 LLVMValueRef
*p_floor_log2
= NULL
;
872 LLVMValueRef
*p_exp
= NULL
;
873 LLVMValueRef
*p_log2
= NULL
;
875 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
876 src0
= lp_build_abs( &bld
->base
, src0
);
878 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
879 p_floor_log2
= &tmp0
;
880 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
882 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
885 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
887 /* dst.x = floor(lg2(abs(src.x))) */
888 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
890 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
891 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
892 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
894 /* dst.z = lg2(abs(src.x)) */
895 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
899 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
900 dst0
[CHAN_W
] = bld
->base
.one
;
904 case TGSI_OPCODE_MUL
:
905 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
906 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
907 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
908 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
912 case TGSI_OPCODE_ADD
:
913 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
914 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
915 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
916 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
920 case TGSI_OPCODE_DP3
:
921 /* TGSI_OPCODE_DOT3 */
922 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
923 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
924 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
925 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
926 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
927 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
928 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
929 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
930 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
931 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
932 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
933 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
934 dst0
[chan_index
] = tmp0
;
938 case TGSI_OPCODE_DP4
:
939 /* TGSI_OPCODE_DOT4 */
940 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
941 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
942 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
943 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
944 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
945 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
946 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
947 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
948 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
949 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
950 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
951 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
952 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
953 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
954 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
955 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
956 dst0
[chan_index
] = tmp0
;
960 case TGSI_OPCODE_DST
:
961 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
962 dst0
[CHAN_X
] = bld
->base
.one
;
964 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
965 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
966 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
967 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
969 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
970 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
972 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
973 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
977 case TGSI_OPCODE_MIN
:
978 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
979 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
980 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
981 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
985 case TGSI_OPCODE_MAX
:
986 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
987 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
988 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
989 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
993 case TGSI_OPCODE_SLT
:
994 /* TGSI_OPCODE_SETLT */
995 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
996 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
997 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
998 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
999 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1003 case TGSI_OPCODE_SGE
:
1004 /* TGSI_OPCODE_SETGE */
1005 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1006 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1007 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1008 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1009 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1013 case TGSI_OPCODE_MAD
:
1014 /* TGSI_OPCODE_MADD */
1015 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1016 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1017 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1018 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1019 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1020 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1021 dst0
[chan_index
] = tmp0
;
1025 case TGSI_OPCODE_SUB
:
1026 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1027 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1028 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1029 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1033 case TGSI_OPCODE_LRP
:
1034 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1035 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1036 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1037 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1038 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1039 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1040 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1044 case TGSI_OPCODE_CND
:
1045 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1046 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1047 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1048 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1049 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
1050 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1051 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1055 case TGSI_OPCODE_DP2A
:
1056 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1057 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1058 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1059 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1060 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1061 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1062 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1063 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1064 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1065 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1066 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1070 case TGSI_OPCODE_FRC
:
1071 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1072 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1073 tmp0
= lp_build_floor(&bld
->base
, src0
);
1074 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1075 dst0
[chan_index
] = tmp0
;
1079 case TGSI_OPCODE_CLAMP
:
1080 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1081 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1082 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1083 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1084 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1085 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1086 dst0
[chan_index
] = tmp0
;
1090 case TGSI_OPCODE_FLR
:
1091 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1092 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1093 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1097 case TGSI_OPCODE_ROUND
:
1098 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1099 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1100 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1104 case TGSI_OPCODE_EX2
: {
1105 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1106 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1107 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1108 dst0
[chan_index
] = tmp0
;
1113 case TGSI_OPCODE_LG2
:
1114 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1115 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1116 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1117 dst0
[chan_index
] = tmp0
;
1121 case TGSI_OPCODE_POW
:
1122 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1123 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1124 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1125 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1126 dst0
[chan_index
] = res
;
1130 case TGSI_OPCODE_XPD
:
1131 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1132 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1133 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1134 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1136 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1137 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1138 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1139 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1141 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1143 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1145 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1146 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1147 dst0
[CHAN_X
] = tmp2
;
1149 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1150 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1151 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1152 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1154 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1155 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1156 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1157 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1158 dst0
[CHAN_Y
] = tmp3
;
1160 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1161 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1162 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1163 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1164 dst0
[CHAN_Z
] = tmp5
;
1166 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1167 dst0
[CHAN_W
] = bld
->base
.one
;
1171 case TGSI_OPCODE_ABS
:
1172 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1173 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1174 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1178 case TGSI_OPCODE_RCC
:
1183 case TGSI_OPCODE_DPH
:
1184 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1185 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1186 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1187 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1188 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1189 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1190 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1191 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1192 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1193 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1194 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1195 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1196 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1197 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1198 dst0
[chan_index
] = tmp0
;
1202 case TGSI_OPCODE_COS
:
1203 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1204 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1205 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1206 dst0
[chan_index
] = tmp0
;
1210 case TGSI_OPCODE_DDX
:
1211 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1212 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1216 case TGSI_OPCODE_DDY
:
1217 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1218 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1222 case TGSI_OPCODE_KILP
:
1223 /* predicated kill */
1224 emit_kilp( bld
, inst
);
1227 case TGSI_OPCODE_KIL
:
1228 /* conditional kill */
1229 emit_kil( bld
, inst
);
1232 case TGSI_OPCODE_PK2H
:
1236 case TGSI_OPCODE_PK2US
:
1240 case TGSI_OPCODE_PK4B
:
1244 case TGSI_OPCODE_PK4UB
:
1248 case TGSI_OPCODE_RFL
:
1252 case TGSI_OPCODE_SEQ
:
1253 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1254 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1255 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1256 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1257 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1261 case TGSI_OPCODE_SFL
:
1262 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1263 dst0
[chan_index
] = bld
->base
.zero
;
1267 case TGSI_OPCODE_SGT
:
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 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1272 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1276 case TGSI_OPCODE_SIN
:
1277 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1278 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1279 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1280 dst0
[chan_index
] = tmp0
;
1284 case TGSI_OPCODE_SLE
:
1285 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1286 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1287 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1288 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1289 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1293 case TGSI_OPCODE_SNE
:
1294 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1295 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1296 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1297 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1298 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1302 case TGSI_OPCODE_STR
:
1303 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1304 dst0
[chan_index
] = bld
->base
.one
;
1308 case TGSI_OPCODE_TEX
:
1309 emit_tex( bld
, inst
, FALSE
, FALSE
, dst0
);
1312 case TGSI_OPCODE_TXD
:
1317 case TGSI_OPCODE_UP2H
:
1323 case TGSI_OPCODE_UP2US
:
1329 case TGSI_OPCODE_UP4B
:
1335 case TGSI_OPCODE_UP4UB
:
1341 case TGSI_OPCODE_X2D
:
1347 case TGSI_OPCODE_ARA
:
1354 case TGSI_OPCODE_ARR
:
1356 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1357 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1358 emit_rnd( bld
, 0, 0 );
1359 emit_f2it( bld
, 0 );
1360 dst0
[chan_index
] = tmp0
;
1365 case TGSI_OPCODE_BRA
:
1371 case TGSI_OPCODE_CAL
:
1376 case TGSI_OPCODE_RET
:
1381 case TGSI_OPCODE_END
:
1384 case TGSI_OPCODE_SSG
:
1385 /* TGSI_OPCODE_SGN */
1386 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1387 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1388 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1392 case TGSI_OPCODE_CMP
:
1393 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1394 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1395 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1396 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1397 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1398 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1402 case TGSI_OPCODE_SCS
:
1403 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1404 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1405 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1407 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1408 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1409 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1411 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1412 dst0
[CHAN_Z
] = bld
->base
.zero
;
1414 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1415 dst0
[CHAN_W
] = bld
->base
.one
;
1419 case TGSI_OPCODE_TXB
:
1420 emit_tex( bld
, inst
, TRUE
, FALSE
, dst0
);
1423 case TGSI_OPCODE_NRM
:
1425 case TGSI_OPCODE_NRM4
:
1426 /* 3 or 4-component normalization */
1428 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1430 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1431 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1432 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1433 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1435 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1438 /* xmm0 = src.x * src.x */
1439 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1440 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1443 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1446 /* xmm0 = xmm0 + src.y * src.y */
1447 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1448 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1451 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1452 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1455 /* xmm0 = xmm0 + src.z * src.z */
1456 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1457 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1460 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1461 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1465 /* xmm0 = xmm0 + src.w * src.w */
1466 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1467 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1470 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1471 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1474 /* xmm1 = 1 / sqrt(xmm0) */
1475 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1477 /* dst.x = xmm1 * src.x */
1478 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1479 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1482 /* dst.y = xmm1 * src.y */
1483 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1484 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1487 /* dst.z = xmm1 * src.z */
1488 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1489 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1492 /* dst.w = xmm1 * src.w */
1493 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1494 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1499 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1500 dst0
[CHAN_W
] = bld
->base
.one
;
1505 case TGSI_OPCODE_DIV
:
1511 case TGSI_OPCODE_DP2
:
1512 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1513 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1514 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1515 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1516 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1517 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1518 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1519 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1520 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1524 case TGSI_OPCODE_TXL
:
1525 emit_tex( bld
, inst
, TRUE
, FALSE
, dst0
);
1528 case TGSI_OPCODE_TXP
:
1529 emit_tex( bld
, inst
, FALSE
, TRUE
, dst0
);
1532 case TGSI_OPCODE_BRK
:
1533 lp_exec_break(&bld
->exec_mask
);
1536 case TGSI_OPCODE_IF
:
1537 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1538 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
1539 tmp0
, bld
->base
.zero
);
1540 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1543 case TGSI_OPCODE_BGNFOR
:
1549 case TGSI_OPCODE_BGNLOOP
:
1550 lp_exec_bgnloop(&bld
->exec_mask
);
1553 case TGSI_OPCODE_REP
:
1559 case TGSI_OPCODE_ELSE
:
1560 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1563 case TGSI_OPCODE_ENDIF
:
1564 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1567 case TGSI_OPCODE_ENDFOR
:
1573 case TGSI_OPCODE_ENDLOOP
:
1574 lp_exec_endloop(&bld
->exec_mask
);
1577 case TGSI_OPCODE_ENDREP
:
1583 case TGSI_OPCODE_PUSHA
:
1589 case TGSI_OPCODE_POPA
:
1595 case TGSI_OPCODE_CEIL
:
1596 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1597 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1598 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1602 case TGSI_OPCODE_I2F
:
1608 case TGSI_OPCODE_NOT
:
1614 case TGSI_OPCODE_TRUNC
:
1615 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1616 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1617 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1621 case TGSI_OPCODE_SHL
:
1627 case TGSI_OPCODE_ISHR
:
1633 case TGSI_OPCODE_AND
:
1639 case TGSI_OPCODE_OR
:
1645 case TGSI_OPCODE_MOD
:
1651 case TGSI_OPCODE_XOR
:
1657 case TGSI_OPCODE_SAD
:
1663 case TGSI_OPCODE_TXF
:
1669 case TGSI_OPCODE_TXQ
:
1675 case TGSI_OPCODE_CONT
:
1676 lp_exec_continue(&bld
->exec_mask
);
1679 case TGSI_OPCODE_EMIT
:
1683 case TGSI_OPCODE_ENDPRIM
:
1687 case TGSI_OPCODE_NOP
:
1695 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1696 emit_store( bld
, inst
, 0, chan_index
, dst0
[chan_index
]);
1705 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1706 const struct tgsi_token
*tokens
,
1707 struct lp_type type
,
1708 struct lp_build_mask_context
*mask
,
1709 LLVMValueRef consts_ptr
,
1710 const LLVMValueRef
*pos
,
1711 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
1712 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1713 struct lp_build_sampler_soa
*sampler
)
1715 struct lp_build_tgsi_soa_context bld
;
1716 struct tgsi_parse_context parse
;
1717 uint num_immediates
= 0;
1720 /* Setup build context */
1721 memset(&bld
, 0, sizeof bld
);
1722 lp_build_context_init(&bld
.base
, builder
, type
);
1725 bld
.inputs
= inputs
;
1726 bld
.outputs
= outputs
;
1727 bld
.consts_ptr
= consts_ptr
;
1728 bld
.sampler
= sampler
;
1730 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
1732 tgsi_parse_init( &parse
, tokens
);
1734 while( !tgsi_parse_end_of_tokens( &parse
) ) {
1735 tgsi_parse_token( &parse
);
1737 switch( parse
.FullToken
.Token
.Type
) {
1738 case TGSI_TOKEN_TYPE_DECLARATION
:
1739 /* Inputs already interpolated */
1741 if (!emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
))
1742 _debug_printf("warning: failed to define LLVM variable\n");
1746 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1748 unsigned opcode
= parse
.FullToken
.FullInstruction
.Instruction
.Opcode
;
1749 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info(opcode
);
1750 if (!emit_instruction( &bld
, &parse
.FullToken
.FullInstruction
, info
))
1751 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1752 info
? info
->mnemonic
: "<invalid>");
1757 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1758 /* simply copy the immediate values into the next immediates[] slot */
1760 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1762 assert(num_immediates
< LP_MAX_IMMEDIATES
);
1763 for( i
= 0; i
< size
; ++i
)
1764 bld
.immediates
[num_immediates
][i
] =
1765 lp_build_const_vec(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
1766 for( i
= size
; i
< 4; ++i
)
1767 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
1772 case TGSI_TOKEN_TYPE_PROPERTY
:
1780 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
1781 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
1782 debug_printf("11111111111111111111111111111 \n");
1783 tgsi_dump(tokens
, 0);
1784 LLVMDumpValue(function
);
1785 debug_printf("2222222222222222222222222222 \n");
1787 tgsi_parse_free( &parse
);