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_info.h"
45 #include "tgsi/tgsi_parse.h"
46 #include "tgsi/tgsi_util.h"
47 #include "tgsi/tgsi_exec.h"
48 #include "lp_bld_type.h"
49 #include "lp_bld_const.h"
50 #include "lp_bld_arit.h"
51 #include "lp_bld_logic.h"
52 #include "lp_bld_swizzle.h"
53 #include "lp_bld_flow.h"
54 #include "lp_bld_tgsi.h"
55 #include "lp_bld_debug.h"
58 #define LP_MAX_TEMPS 256
59 #define LP_MAX_IMMEDIATES 256
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 )
80 #define QUAD_TOP_LEFT 0
81 #define QUAD_TOP_RIGHT 1
82 #define QUAD_BOTTOM_LEFT 2
83 #define QUAD_BOTTOM_RIGHT 3
85 #define LP_TGSI_MAX_NESTING 16
88 struct lp_build_context
*bld
;
92 LLVMTypeRef int_vec_type
;
94 LLVMValueRef cond_stack
[LP_TGSI_MAX_NESTING
];
96 LLVMValueRef cond_mask
;
98 LLVMValueRef exec_mask
;
101 struct lp_build_tgsi_soa_context
103 struct lp_build_context base
;
105 LLVMValueRef consts_ptr
;
106 const LLVMValueRef
*pos
;
107 const LLVMValueRef (*inputs
)[NUM_CHANNELS
];
108 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
110 struct lp_build_sampler_soa
*sampler
;
112 LLVMValueRef immediates
[LP_MAX_IMMEDIATES
][NUM_CHANNELS
];
113 LLVMValueRef temps
[LP_MAX_TEMPS
][NUM_CHANNELS
];
115 struct lp_build_mask_context
*mask
;
116 struct lp_exec_mask exec_mask
;
119 static const unsigned char
121 QUAD_TOP_LEFT
, QUAD_TOP_LEFT
,
122 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_LEFT
125 static const unsigned char
127 QUAD_TOP_RIGHT
, QUAD_TOP_RIGHT
,
128 QUAD_BOTTOM_RIGHT
, QUAD_BOTTOM_RIGHT
131 static const unsigned char
133 QUAD_TOP_LEFT
, QUAD_TOP_RIGHT
,
134 QUAD_TOP_LEFT
, QUAD_TOP_RIGHT
137 static const unsigned char
138 swizzle_bottom
[4] = {
139 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_RIGHT
,
140 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_RIGHT
143 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
146 mask
->has_mask
= FALSE
;
147 mask
->cond_stack_size
= 0;
149 mask
->int_vec_type
= lp_build_int_vec_type(mask
->bld
->type
);
152 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
154 mask
->exec_mask
= mask
->cond_mask
;
155 if (mask
->cond_stack_size
> 0)
156 mask
->has_mask
= TRUE
;
159 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
162 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
163 mask
->cond_mask
= LLVMBuildBitCast(mask
->bld
->builder
, val
,
164 mask
->int_vec_type
, "");
166 lp_exec_mask_update(mask
);
169 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
171 LLVMValueRef prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
172 LLVMValueRef inv_mask
= LLVMBuildNot(mask
->bld
->builder
,
173 mask
->cond_mask
, "");
175 /* means that we didn't have any mask before and that
176 * we were fully enabled */
177 if (mask
->cond_stack_size
<= 1) {
178 prev_mask
= LLVMConstAllOnes(mask
->int_vec_type
);
181 mask
->cond_mask
= LLVMBuildAnd(mask
->bld
->builder
,
184 lp_exec_mask_update(mask
);
187 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
189 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
190 lp_exec_mask_update(mask
);
193 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
197 if (mask
->has_mask
) {
198 LLVMValueRef real_val
, dst_val
;
200 dst_val
= LLVMBuildLoad(mask
->bld
->builder
, dst
, "");
201 real_val
= lp_build_select(mask
->bld
,
205 LLVMBuildStore(mask
->bld
->builder
, real_val
, dst
);
207 LLVMBuildStore(mask
->bld
->builder
, val
, dst
);
212 emit_ddx(struct lp_build_tgsi_soa_context
*bld
,
215 LLVMValueRef src_left
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_left
);
216 LLVMValueRef src_right
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_right
);
217 return lp_build_sub(&bld
->base
, src_right
, src_left
);
222 emit_ddy(struct lp_build_tgsi_soa_context
*bld
,
225 LLVMValueRef src_top
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_top
);
226 LLVMValueRef src_bottom
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_bottom
);
227 return lp_build_sub(&bld
->base
, src_top
, src_bottom
);
236 struct lp_build_tgsi_soa_context
*bld
,
237 const struct tgsi_full_instruction
*inst
,
239 const unsigned chan_index
)
241 const struct tgsi_full_src_register
*reg
= &inst
->Src
[index
];
242 unsigned swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
251 switch (reg
->Register
.File
) {
252 case TGSI_FILE_CONSTANT
: {
253 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), reg
->Register
.Index
*4 + swizzle
, 0);
254 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
, &index
, 1, "");
255 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
256 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
260 case TGSI_FILE_IMMEDIATE
:
261 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
265 case TGSI_FILE_INPUT
:
266 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
270 case TGSI_FILE_TEMPORARY
:
271 res
= LLVMBuildLoad(bld
->base
.builder
, bld
->temps
[reg
->Register
.Index
][swizzle
], "");
273 return bld
->base
.undef
;
278 return bld
->base
.undef
;
284 return bld
->base
.undef
;
287 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
288 case TGSI_UTIL_SIGN_CLEAR
:
289 res
= lp_build_abs( &bld
->base
, res
);
292 case TGSI_UTIL_SIGN_SET
:
293 /* TODO: Use bitwese OR for floating point */
294 res
= lp_build_abs( &bld
->base
, res
);
295 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
298 case TGSI_UTIL_SIGN_TOGGLE
:
299 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
302 case TGSI_UTIL_SIGN_KEEP
:
311 * Register fetch with derivatives.
315 struct lp_build_tgsi_soa_context
*bld
,
316 const struct tgsi_full_instruction
*inst
,
318 const unsigned chan_index
,
325 src
= emit_fetch(bld
, inst
, index
, chan_index
);
330 /* TODO: use interpolation coeffs for inputs */
333 *ddx
= emit_ddx(bld
, src
);
336 *ddy
= emit_ddy(bld
, src
);
345 struct lp_build_tgsi_soa_context
*bld
,
346 const struct tgsi_full_instruction
*inst
,
351 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
353 switch( inst
->Instruction
.Saturate
) {
357 case TGSI_SAT_ZERO_ONE
:
358 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
359 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
362 case TGSI_SAT_MINUS_PLUS_ONE
:
363 value
= lp_build_max(&bld
->base
, value
, lp_build_const_scalar(bld
->base
.type
, -1.0));
364 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
371 switch( reg
->Register
.File
) {
372 case TGSI_FILE_OUTPUT
:
373 lp_exec_mask_store(&bld
->exec_mask
, value
,
374 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
377 case TGSI_FILE_TEMPORARY
:
378 lp_exec_mask_store(&bld
->exec_mask
, value
,
379 bld
->temps
[reg
->Register
.Index
][chan_index
]);
382 case TGSI_FILE_ADDRESS
:
394 * High-level instruction translators.
399 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
400 const struct tgsi_full_instruction
*inst
,
401 boolean apply_lodbias
,
405 const uint unit
= inst
->Src
[1].Register
.Index
;
406 LLVMValueRef lodbias
;
407 LLVMValueRef oow
= NULL
;
408 LLVMValueRef coords
[3];
412 switch (inst
->Texture
.Texture
) {
413 case TGSI_TEXTURE_1D
:
416 case TGSI_TEXTURE_2D
:
417 case TGSI_TEXTURE_RECT
:
420 case TGSI_TEXTURE_SHADOW1D
:
421 case TGSI_TEXTURE_SHADOW2D
:
422 case TGSI_TEXTURE_SHADOWRECT
:
423 case TGSI_TEXTURE_3D
:
424 case TGSI_TEXTURE_CUBE
:
433 lodbias
= emit_fetch( bld
, inst
, 0, 3 );
435 lodbias
= bld
->base
.zero
;
438 oow
= emit_fetch( bld
, inst
, 0, 3 );
439 oow
= lp_build_rcp(&bld
->base
, oow
);
442 for (i
= 0; i
< num_coords
; i
++) {
443 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
445 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
447 for (i
= num_coords
; i
< 3; i
++) {
448 coords
[i
] = bld
->base
.undef
;
451 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
454 unit
, num_coords
, coords
, lodbias
,
461 struct lp_build_tgsi_soa_context
*bld
,
462 const struct tgsi_full_instruction
*inst
)
464 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
465 LLVMValueRef terms
[NUM_CHANNELS
];
469 memset(&terms
, 0, sizeof terms
);
471 FOR_EACH_CHANNEL( chan_index
) {
474 /* Unswizzle channel */
475 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
477 /* Check if the component has not been already tested. */
478 assert(swizzle
< NUM_CHANNELS
);
479 if( !terms
[swizzle
] )
480 /* TODO: change the comparison operator instead of setting the sign */
481 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
485 FOR_EACH_CHANNEL( chan_index
) {
486 if(terms
[chan_index
]) {
487 LLVMValueRef chan_mask
;
489 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
492 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
499 lp_build_mask_update(bld
->mask
, mask
);
504 * Check if inst src/dest regs use indirect addressing into temporary
508 indirect_temp_reference(const struct tgsi_full_instruction
*inst
)
511 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
512 const struct tgsi_full_src_register
*reg
= &inst
->Src
[i
];
513 if (reg
->Register
.File
== TGSI_FILE_TEMPORARY
&&
514 reg
->Register
.Indirect
)
517 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
518 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[i
];
519 if (reg
->Register
.File
== TGSI_FILE_TEMPORARY
&&
520 reg
->Register
.Indirect
)
528 struct lp_build_tgsi_soa_context
*bld
,
529 const struct tgsi_full_declaration
*decl
)
531 unsigned first
= decl
->Range
.First
;
532 unsigned last
= decl
->Range
.Last
;
535 for (idx
= first
; idx
<= last
; ++idx
) {
538 switch (decl
->Declaration
.File
) {
539 case TGSI_FILE_TEMPORARY
:
540 for (i
= 0; i
< NUM_CHANNELS
; i
++)
541 bld
->temps
[idx
][i
] = lp_build_alloca(&bld
->base
);
545 case TGSI_FILE_OUTPUT
:
546 for (i
= 0; i
< NUM_CHANNELS
; i
++)
547 bld
->outputs
[idx
][i
] = lp_build_alloca(&bld
->base
);
552 /* don't need to declare other vars */
565 struct lp_build_tgsi_soa_context
*bld
,
566 const struct tgsi_full_instruction
*inst
,
567 const struct tgsi_opcode_info
*info
)
570 LLVMValueRef src0
, src1
, src2
;
571 LLVMValueRef tmp0
, tmp1
, tmp2
;
572 LLVMValueRef tmp3
= NULL
;
573 LLVMValueRef tmp4
= NULL
;
574 LLVMValueRef tmp5
= NULL
;
575 LLVMValueRef tmp6
= NULL
;
576 LLVMValueRef tmp7
= NULL
;
578 LLVMValueRef dst0
[NUM_CHANNELS
];
580 /* we can't handle indirect addressing into temp register file yet */
581 if (indirect_temp_reference(inst
))
584 assert(info
->num_dst
<= 1);
586 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
587 dst0
[chan_index
] = bld
->base
.undef
;
591 switch (inst
->Instruction
.Opcode
) {
593 case TGSI_OPCODE_ARL
:
595 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
596 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
599 dst0
[chan_index
] = tmp0
;
604 case TGSI_OPCODE_MOV
:
605 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
606 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
610 case TGSI_OPCODE_LIT
:
611 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
612 dst0
[CHAN_X
] = bld
->base
.one
;
614 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
615 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
616 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
618 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
619 /* XMM[1] = SrcReg[0].yyyy */
620 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
621 /* XMM[1] = max(XMM[1], 0) */
622 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
623 /* XMM[2] = SrcReg[0].wwww */
624 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
625 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
626 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
627 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
628 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
630 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
631 dst0
[CHAN_W
] = bld
->base
.one
;
635 case TGSI_OPCODE_RCP
:
636 /* TGSI_OPCODE_RECIP */
637 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
638 res
= lp_build_rcp(&bld
->base
, src0
);
639 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
640 dst0
[chan_index
] = res
;
644 case TGSI_OPCODE_RSQ
:
645 /* TGSI_OPCODE_RECIPSQRT */
646 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
647 src0
= lp_build_abs(&bld
->base
, src0
);
648 res
= lp_build_rsqrt(&bld
->base
, src0
);
649 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
650 dst0
[chan_index
] = res
;
654 case TGSI_OPCODE_EXP
:
655 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
656 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
657 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
658 LLVMValueRef
*p_exp2_int_part
= NULL
;
659 LLVMValueRef
*p_frac_part
= NULL
;
660 LLVMValueRef
*p_exp2
= NULL
;
662 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
664 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
665 p_exp2_int_part
= &tmp0
;
666 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
668 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
671 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
673 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
675 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
677 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
681 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
682 dst0
[CHAN_W
] = bld
->base
.one
;
686 case TGSI_OPCODE_LOG
:
687 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
688 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
689 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
690 LLVMValueRef
*p_floor_log2
= NULL
;
691 LLVMValueRef
*p_exp
= NULL
;
692 LLVMValueRef
*p_log2
= NULL
;
694 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
695 src0
= lp_build_abs( &bld
->base
, src0
);
697 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
698 p_floor_log2
= &tmp0
;
699 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
701 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
704 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
706 /* dst.x = floor(lg2(abs(src.x))) */
707 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
709 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
710 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
711 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
713 /* dst.z = lg2(abs(src.x)) */
714 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
718 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
719 dst0
[CHAN_W
] = bld
->base
.one
;
723 case TGSI_OPCODE_MUL
:
724 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
725 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
726 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
727 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
731 case TGSI_OPCODE_ADD
:
732 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
733 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
734 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
735 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
739 case TGSI_OPCODE_DP3
:
740 /* TGSI_OPCODE_DOT3 */
741 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
742 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
743 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
744 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
745 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
746 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
747 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
748 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
749 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
750 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
751 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
752 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
753 dst0
[chan_index
] = tmp0
;
757 case TGSI_OPCODE_DP4
:
758 /* TGSI_OPCODE_DOT4 */
759 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
760 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
761 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
762 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
763 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
764 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
765 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
766 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
767 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
768 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
769 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
770 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
771 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
772 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
773 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
774 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
775 dst0
[chan_index
] = tmp0
;
779 case TGSI_OPCODE_DST
:
780 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
781 dst0
[CHAN_X
] = bld
->base
.one
;
783 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
784 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
785 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
786 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
788 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
789 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
791 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
792 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
796 case TGSI_OPCODE_MIN
:
797 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
798 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
799 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
800 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
804 case TGSI_OPCODE_MAX
:
805 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
806 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
807 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
808 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
812 case TGSI_OPCODE_SLT
:
813 /* TGSI_OPCODE_SETLT */
814 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
815 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
816 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
817 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
818 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
822 case TGSI_OPCODE_SGE
:
823 /* TGSI_OPCODE_SETGE */
824 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
825 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
826 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
827 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
828 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
832 case TGSI_OPCODE_MAD
:
833 /* TGSI_OPCODE_MADD */
834 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
835 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
836 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
837 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
838 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
839 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
840 dst0
[chan_index
] = tmp0
;
844 case TGSI_OPCODE_SUB
:
845 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
846 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
847 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
848 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
852 case TGSI_OPCODE_LRP
:
853 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
854 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
855 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
856 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
857 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
858 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
859 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
863 case TGSI_OPCODE_CND
:
864 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
865 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
866 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
867 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
868 tmp1
= lp_build_const_scalar(bld
->base
.type
, 0.5);
869 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
870 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
874 case TGSI_OPCODE_DP2A
:
875 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
876 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
877 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
878 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
879 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
880 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
881 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
882 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
883 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
884 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
885 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
889 case TGSI_OPCODE_FRC
:
890 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
891 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
892 tmp0
= lp_build_floor(&bld
->base
, src0
);
893 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
894 dst0
[chan_index
] = tmp0
;
898 case TGSI_OPCODE_CLAMP
:
899 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
900 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
901 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
902 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
903 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
904 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
905 dst0
[chan_index
] = tmp0
;
909 case TGSI_OPCODE_FLR
:
910 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
911 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
912 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
916 case TGSI_OPCODE_ROUND
:
917 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
918 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
919 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
923 case TGSI_OPCODE_EX2
: {
924 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
925 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
926 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
927 dst0
[chan_index
] = tmp0
;
932 case TGSI_OPCODE_LG2
:
933 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
934 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
935 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
936 dst0
[chan_index
] = tmp0
;
940 case TGSI_OPCODE_POW
:
941 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
942 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
943 res
= lp_build_pow( &bld
->base
, src0
, src1
);
944 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
945 dst0
[chan_index
] = res
;
949 case TGSI_OPCODE_XPD
:
950 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
951 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
952 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
953 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
955 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
956 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
957 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
958 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
960 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
962 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
964 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
965 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
968 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
969 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
970 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
971 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
973 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
974 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
975 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
976 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
979 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
980 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
981 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
982 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
985 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
986 dst0
[CHAN_W
] = bld
->base
.one
;
990 case TGSI_OPCODE_ABS
:
991 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
992 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
993 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
997 case TGSI_OPCODE_RCC
:
1002 case TGSI_OPCODE_DPH
:
1003 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1004 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1005 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1006 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1007 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1008 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1009 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1010 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1011 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1012 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1013 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1014 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1015 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1016 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1017 dst0
[chan_index
] = tmp0
;
1021 case TGSI_OPCODE_COS
:
1022 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1023 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1024 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1025 dst0
[chan_index
] = tmp0
;
1029 case TGSI_OPCODE_DDX
:
1030 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1031 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1035 case TGSI_OPCODE_DDY
:
1036 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1037 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1041 case TGSI_OPCODE_KILP
:
1042 /* predicated kill */
1047 case TGSI_OPCODE_KIL
:
1048 /* conditional kill */
1049 emit_kil( bld
, inst
);
1052 case TGSI_OPCODE_PK2H
:
1056 case TGSI_OPCODE_PK2US
:
1060 case TGSI_OPCODE_PK4B
:
1064 case TGSI_OPCODE_PK4UB
:
1068 case TGSI_OPCODE_RFL
:
1072 case TGSI_OPCODE_SEQ
:
1073 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1074 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1075 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1076 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1077 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1081 case TGSI_OPCODE_SFL
:
1082 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1083 dst0
[chan_index
] = bld
->base
.zero
;
1087 case TGSI_OPCODE_SGT
:
1088 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1089 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1090 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1091 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1092 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1096 case TGSI_OPCODE_SIN
:
1097 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1098 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1099 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1100 dst0
[chan_index
] = tmp0
;
1104 case TGSI_OPCODE_SLE
:
1105 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1106 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1107 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1108 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1109 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1113 case TGSI_OPCODE_SNE
:
1114 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1115 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1116 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1117 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1118 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1122 case TGSI_OPCODE_STR
:
1123 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1124 dst0
[chan_index
] = bld
->base
.one
;
1128 case TGSI_OPCODE_TEX
:
1129 /* XXX what about dst0 writemask? */
1130 emit_tex( bld
, inst
, FALSE
, FALSE
, dst0
);
1133 case TGSI_OPCODE_TXD
:
1138 case TGSI_OPCODE_UP2H
:
1144 case TGSI_OPCODE_UP2US
:
1150 case TGSI_OPCODE_UP4B
:
1156 case TGSI_OPCODE_UP4UB
:
1162 case TGSI_OPCODE_X2D
:
1168 case TGSI_OPCODE_ARA
:
1175 case TGSI_OPCODE_ARR
:
1177 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1178 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1179 emit_rnd( bld
, 0, 0 );
1180 emit_f2it( bld
, 0 );
1181 dst0
[chan_index
] = tmp0
;
1186 case TGSI_OPCODE_BRA
:
1192 case TGSI_OPCODE_CAL
:
1197 case TGSI_OPCODE_RET
:
1202 case TGSI_OPCODE_END
:
1205 case TGSI_OPCODE_SSG
:
1206 /* TGSI_OPCODE_SGN */
1207 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1208 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1209 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1213 case TGSI_OPCODE_CMP
:
1214 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1215 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1216 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1217 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1218 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1219 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1223 case TGSI_OPCODE_SCS
:
1224 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1225 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1226 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1228 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1229 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1230 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1232 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1233 dst0
[CHAN_Z
] = bld
->base
.zero
;
1235 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1236 dst0
[CHAN_W
] = bld
->base
.one
;
1240 case TGSI_OPCODE_TXB
:
1241 emit_tex( bld
, inst
, TRUE
, FALSE
, dst0
);
1244 case TGSI_OPCODE_NRM
:
1246 case TGSI_OPCODE_NRM4
:
1247 /* 3 or 4-component normalization */
1249 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1251 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1252 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1253 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1254 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1256 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1259 /* xmm0 = src.x * src.x */
1260 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1261 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1264 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1267 /* xmm0 = xmm0 + src.y * src.y */
1268 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1269 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1272 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1273 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1276 /* xmm0 = xmm0 + src.z * src.z */
1277 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1278 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1281 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1282 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1286 /* xmm0 = xmm0 + src.w * src.w */
1287 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1288 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1291 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1292 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1295 /* xmm1 = 1 / sqrt(xmm0) */
1296 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1298 /* dst.x = xmm1 * src.x */
1299 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1300 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1303 /* dst.y = xmm1 * src.y */
1304 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1305 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1308 /* dst.z = xmm1 * src.z */
1309 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1310 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1313 /* dst.w = xmm1 * src.w */
1314 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1315 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1320 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1321 dst0
[CHAN_W
] = bld
->base
.one
;
1326 case TGSI_OPCODE_DIV
:
1332 case TGSI_OPCODE_DP2
:
1333 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1334 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1335 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1336 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1337 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1338 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1339 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1340 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1341 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1345 case TGSI_OPCODE_TXL
:
1346 emit_tex( bld
, inst
, TRUE
, FALSE
, dst0
);
1349 case TGSI_OPCODE_TXP
:
1350 emit_tex( bld
, inst
, FALSE
, TRUE
, dst0
);
1353 case TGSI_OPCODE_BRK
:
1358 case TGSI_OPCODE_IF
:
1359 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1360 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1363 case TGSI_OPCODE_BGNFOR
:
1369 case TGSI_OPCODE_REP
:
1375 case TGSI_OPCODE_ELSE
:
1376 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1379 case TGSI_OPCODE_ENDIF
:
1380 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1383 case TGSI_OPCODE_ENDFOR
:
1389 case TGSI_OPCODE_ENDREP
:
1395 case TGSI_OPCODE_PUSHA
:
1401 case TGSI_OPCODE_POPA
:
1407 case TGSI_OPCODE_CEIL
:
1408 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1409 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1410 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1414 case TGSI_OPCODE_I2F
:
1420 case TGSI_OPCODE_NOT
:
1426 case TGSI_OPCODE_TRUNC
:
1427 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1428 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1429 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1433 case TGSI_OPCODE_SHL
:
1439 case TGSI_OPCODE_ISHR
:
1445 case TGSI_OPCODE_AND
:
1451 case TGSI_OPCODE_OR
:
1457 case TGSI_OPCODE_MOD
:
1463 case TGSI_OPCODE_XOR
:
1469 case TGSI_OPCODE_SAD
:
1475 case TGSI_OPCODE_TXF
:
1481 case TGSI_OPCODE_TXQ
:
1487 case TGSI_OPCODE_CONT
:
1492 case TGSI_OPCODE_EMIT
:
1496 case TGSI_OPCODE_ENDPRIM
:
1500 case TGSI_OPCODE_NOP
:
1508 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1509 emit_store( bld
, inst
, 0, chan_index
, dst0
[chan_index
]);
1518 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1519 const struct tgsi_token
*tokens
,
1520 struct lp_type type
,
1521 struct lp_build_mask_context
*mask
,
1522 LLVMValueRef consts_ptr
,
1523 const LLVMValueRef
*pos
,
1524 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
1525 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1526 struct lp_build_sampler_soa
*sampler
)
1528 struct lp_build_tgsi_soa_context bld
;
1529 struct tgsi_parse_context parse
;
1530 uint num_immediates
= 0;
1533 /* Setup build context */
1534 memset(&bld
, 0, sizeof bld
);
1535 lp_build_context_init(&bld
.base
, builder
, type
);
1538 bld
.inputs
= inputs
;
1539 bld
.outputs
= outputs
;
1540 bld
.consts_ptr
= consts_ptr
;
1541 bld
.sampler
= sampler
;
1543 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
1545 tgsi_parse_init( &parse
, tokens
);
1547 while( !tgsi_parse_end_of_tokens( &parse
) ) {
1548 tgsi_parse_token( &parse
);
1550 switch( parse
.FullToken
.Token
.Type
) {
1551 case TGSI_TOKEN_TYPE_DECLARATION
:
1552 /* Inputs already interpolated */
1554 if (!emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
))
1555 _debug_printf("warning: failed to define LLVM variable\n");
1559 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1561 unsigned opcode
= parse
.FullToken
.FullInstruction
.Instruction
.Opcode
;
1562 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info(opcode
);
1563 if (!emit_instruction( &bld
, &parse
.FullToken
.FullInstruction
, info
))
1564 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1565 info
? info
->mnemonic
: "<invalid>");
1570 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1571 /* simply copy the immediate values into the next immediates[] slot */
1573 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1575 assert(num_immediates
< LP_MAX_IMMEDIATES
);
1576 for( i
= 0; i
< size
; ++i
)
1577 bld
.immediates
[num_immediates
][i
] =
1578 lp_build_const_scalar(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
1579 for( i
= size
; i
< 4; ++i
)
1580 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
1585 case TGSI_TOKEN_TYPE_PROPERTY
:
1593 tgsi_parse_free( &parse
);