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
;
100 LLVMValueRef inv_mask
;
103 struct lp_build_tgsi_soa_context
105 struct lp_build_context base
;
107 LLVMValueRef consts_ptr
;
108 const LLVMValueRef
*pos
;
109 const LLVMValueRef (*inputs
)[NUM_CHANNELS
];
110 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
112 struct lp_build_sampler_soa
*sampler
;
114 LLVMValueRef immediates
[LP_MAX_IMMEDIATES
][NUM_CHANNELS
];
115 LLVMValueRef temps
[LP_MAX_TEMPS
][NUM_CHANNELS
];
117 struct lp_build_mask_context
*mask
;
118 struct lp_exec_mask exec_mask
;
121 static const unsigned char
123 QUAD_TOP_LEFT
, QUAD_TOP_LEFT
,
124 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_LEFT
127 static const unsigned char
129 QUAD_TOP_RIGHT
, QUAD_TOP_RIGHT
,
130 QUAD_BOTTOM_RIGHT
, QUAD_BOTTOM_RIGHT
133 static const unsigned char
135 QUAD_TOP_LEFT
, QUAD_TOP_RIGHT
,
136 QUAD_TOP_LEFT
, QUAD_TOP_RIGHT
139 static const unsigned char
140 swizzle_bottom
[4] = {
141 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_RIGHT
,
142 QUAD_BOTTOM_LEFT
, QUAD_BOTTOM_RIGHT
145 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
148 mask
->has_mask
= FALSE
;
149 mask
->cond_stack_size
= 0;
151 mask
->int_vec_type
= lp_build_int_vec_type(mask
->bld
->type
);
153 LLVMConstSub(LLVMConstNull(mask
->int_vec_type
),
154 LLVMConstAllOnes(mask
->int_vec_type
));
157 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
159 mask
->exec_mask
= mask
->cond_mask
;
160 if (mask
->cond_stack_size
> 0)
161 mask
->has_mask
= TRUE
;
164 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
167 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
168 mask
->cond_mask
= LLVMBuildBitCast(mask
->bld
->builder
, val
,
169 mask
->int_vec_type
, "");
171 lp_exec_mask_update(mask
);
174 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
176 LLVMValueRef prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
177 LLVMValueRef inv_mask
= LLVMBuildXor(mask
->bld
->builder
,
180 mask
->cond_mask
= LLVMBuildAnd(mask
->bld
->builder
,
183 lp_exec_mask_update(mask
);
186 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
188 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
189 lp_exec_mask_update(mask
);
192 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
196 if (mask
->has_mask
) {
197 LLVMValueRef real_val
, dst_val
;
199 dst_val
= LLVMBuildLoad(mask
->bld
->builder
, dst
, "");
200 real_val
= lp_build_select(mask
->bld
,
204 LLVMBuildStore(mask
->bld
->builder
, real_val
, dst
);
206 LLVMBuildStore(mask
->bld
->builder
, val
, dst
);
211 emit_ddx(struct lp_build_tgsi_soa_context
*bld
,
214 LLVMValueRef src_left
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_left
);
215 LLVMValueRef src_right
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_right
);
216 return lp_build_sub(&bld
->base
, src_right
, src_left
);
221 emit_ddy(struct lp_build_tgsi_soa_context
*bld
,
224 LLVMValueRef src_top
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_top
);
225 LLVMValueRef src_bottom
= lp_build_swizzle1_aos(&bld
->base
, src
, swizzle_bottom
);
226 return lp_build_sub(&bld
->base
, src_top
, src_bottom
);
235 struct lp_build_tgsi_soa_context
*bld
,
236 const struct tgsi_full_instruction
*inst
,
238 const unsigned chan_index
)
240 const struct tgsi_full_src_register
*reg
= &inst
->Src
[index
];
241 unsigned swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
250 switch (reg
->Register
.File
) {
251 case TGSI_FILE_CONSTANT
: {
252 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), reg
->Register
.Index
*4 + swizzle
, 0);
253 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
, &index
, 1, "");
254 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
255 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
259 case TGSI_FILE_IMMEDIATE
:
260 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
264 case TGSI_FILE_INPUT
:
265 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
269 case TGSI_FILE_TEMPORARY
:
270 res
= LLVMBuildLoad(bld
->base
.builder
, bld
->temps
[reg
->Register
.Index
][swizzle
], "");
272 return bld
->base
.undef
;
277 return bld
->base
.undef
;
283 return bld
->base
.undef
;
286 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
287 case TGSI_UTIL_SIGN_CLEAR
:
288 res
= lp_build_abs( &bld
->base
, res
);
291 case TGSI_UTIL_SIGN_SET
:
292 /* TODO: Use bitwese OR for floating point */
293 res
= lp_build_abs( &bld
->base
, res
);
294 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
297 case TGSI_UTIL_SIGN_TOGGLE
:
298 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
301 case TGSI_UTIL_SIGN_KEEP
:
310 * Register fetch with derivatives.
314 struct lp_build_tgsi_soa_context
*bld
,
315 const struct tgsi_full_instruction
*inst
,
317 const unsigned chan_index
,
324 src
= emit_fetch(bld
, inst
, index
, chan_index
);
329 /* TODO: use interpolation coeffs for inputs */
332 *ddx
= emit_ddx(bld
, src
);
335 *ddy
= emit_ddy(bld
, src
);
344 struct lp_build_tgsi_soa_context
*bld
,
345 const struct tgsi_full_instruction
*inst
,
350 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
352 switch( inst
->Instruction
.Saturate
) {
356 case TGSI_SAT_ZERO_ONE
:
357 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
358 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
361 case TGSI_SAT_MINUS_PLUS_ONE
:
362 value
= lp_build_max(&bld
->base
, value
, lp_build_const_scalar(bld
->base
.type
, -1.0));
363 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
370 switch( reg
->Register
.File
) {
371 case TGSI_FILE_OUTPUT
:
372 lp_exec_mask_store(&bld
->exec_mask
, value
,
373 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
376 case TGSI_FILE_TEMPORARY
:
377 lp_exec_mask_store(&bld
->exec_mask
, value
,
378 bld
->temps
[reg
->Register
.Index
][chan_index
]);
381 case TGSI_FILE_ADDRESS
:
393 * High-level instruction translators.
398 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
399 const struct tgsi_full_instruction
*inst
,
400 boolean apply_lodbias
,
404 const uint unit
= inst
->Src
[1].Register
.Index
;
405 LLVMValueRef lodbias
;
406 LLVMValueRef oow
= NULL
;
407 LLVMValueRef coords
[3];
411 switch (inst
->Texture
.Texture
) {
412 case TGSI_TEXTURE_1D
:
415 case TGSI_TEXTURE_2D
:
416 case TGSI_TEXTURE_RECT
:
419 case TGSI_TEXTURE_SHADOW1D
:
420 case TGSI_TEXTURE_SHADOW2D
:
421 case TGSI_TEXTURE_SHADOWRECT
:
422 case TGSI_TEXTURE_3D
:
423 case TGSI_TEXTURE_CUBE
:
432 lodbias
= emit_fetch( bld
, inst
, 0, 3 );
434 lodbias
= bld
->base
.zero
;
437 oow
= emit_fetch( bld
, inst
, 0, 3 );
438 oow
= lp_build_rcp(&bld
->base
, oow
);
441 for (i
= 0; i
< num_coords
; i
++) {
442 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
444 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
446 for (i
= num_coords
; i
< 3; i
++) {
447 coords
[i
] = bld
->base
.undef
;
450 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
453 unit
, num_coords
, coords
, lodbias
,
460 struct lp_build_tgsi_soa_context
*bld
,
461 const struct tgsi_full_instruction
*inst
)
463 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
464 LLVMValueRef terms
[NUM_CHANNELS
];
468 memset(&terms
, 0, sizeof terms
);
470 FOR_EACH_CHANNEL( chan_index
) {
473 /* Unswizzle channel */
474 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
476 /* Check if the component has not been already tested. */
477 assert(swizzle
< NUM_CHANNELS
);
478 if( !terms
[swizzle
] )
479 /* TODO: change the comparison operator instead of setting the sign */
480 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
484 FOR_EACH_CHANNEL( chan_index
) {
485 if(terms
[chan_index
]) {
486 LLVMValueRef chan_mask
;
488 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
491 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
498 lp_build_mask_update(bld
->mask
, mask
);
503 * Check if inst src/dest regs use indirect addressing into temporary
507 indirect_temp_reference(const struct tgsi_full_instruction
*inst
)
510 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
511 const struct tgsi_full_src_register
*reg
= &inst
->Src
[i
];
512 if (reg
->Register
.File
== TGSI_FILE_TEMPORARY
&&
513 reg
->Register
.Indirect
)
516 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
517 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[i
];
518 if (reg
->Register
.File
== TGSI_FILE_TEMPORARY
&&
519 reg
->Register
.Indirect
)
527 struct lp_build_tgsi_soa_context
*bld
,
528 const struct tgsi_full_declaration
*decl
)
530 unsigned first
= decl
->Range
.First
;
531 unsigned last
= decl
->Range
.Last
;
534 for (idx
= first
; idx
<= last
; ++idx
) {
537 switch (decl
->Declaration
.File
) {
538 case TGSI_FILE_TEMPORARY
:
539 for (i
= 0; i
< NUM_CHANNELS
; i
++)
540 bld
->temps
[idx
][i
] = lp_build_alloca(&bld
->base
);
544 case TGSI_FILE_OUTPUT
:
545 for (i
= 0; i
< NUM_CHANNELS
; i
++)
546 bld
->outputs
[idx
][i
] = lp_build_alloca(&bld
->base
);
551 /* don't need to declare other vars */
564 struct lp_build_tgsi_soa_context
*bld
,
565 const struct tgsi_full_instruction
*inst
,
566 const struct tgsi_opcode_info
*info
)
569 LLVMValueRef src0
, src1
, src2
;
570 LLVMValueRef tmp0
, tmp1
, tmp2
;
571 LLVMValueRef tmp3
= NULL
;
572 LLVMValueRef tmp4
= NULL
;
573 LLVMValueRef tmp5
= NULL
;
574 LLVMValueRef tmp6
= NULL
;
575 LLVMValueRef tmp7
= NULL
;
577 LLVMValueRef dst0
[NUM_CHANNELS
];
579 /* we can't handle indirect addressing into temp register file yet */
580 if (indirect_temp_reference(inst
))
583 assert(info
->num_dst
<= 1);
585 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
586 dst0
[chan_index
] = bld
->base
.undef
;
590 switch (inst
->Instruction
.Opcode
) {
592 case TGSI_OPCODE_ARL
:
594 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
595 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
598 dst0
[chan_index
] = tmp0
;
603 case TGSI_OPCODE_MOV
:
604 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
605 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
609 case TGSI_OPCODE_LIT
:
610 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
611 dst0
[CHAN_X
] = bld
->base
.one
;
613 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
614 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
615 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
617 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
618 /* XMM[1] = SrcReg[0].yyyy */
619 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
620 /* XMM[1] = max(XMM[1], 0) */
621 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
622 /* XMM[2] = SrcReg[0].wwww */
623 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
624 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
625 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
626 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
627 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
629 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
630 dst0
[CHAN_W
] = bld
->base
.one
;
634 case TGSI_OPCODE_RCP
:
635 /* TGSI_OPCODE_RECIP */
636 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
637 res
= lp_build_rcp(&bld
->base
, src0
);
638 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
639 dst0
[chan_index
] = res
;
643 case TGSI_OPCODE_RSQ
:
644 /* TGSI_OPCODE_RECIPSQRT */
645 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
646 src0
= lp_build_abs(&bld
->base
, src0
);
647 res
= lp_build_rsqrt(&bld
->base
, src0
);
648 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
649 dst0
[chan_index
] = res
;
653 case TGSI_OPCODE_EXP
:
654 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
655 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
656 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
657 LLVMValueRef
*p_exp2_int_part
= NULL
;
658 LLVMValueRef
*p_frac_part
= NULL
;
659 LLVMValueRef
*p_exp2
= NULL
;
661 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
663 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
664 p_exp2_int_part
= &tmp0
;
665 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
667 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
670 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
672 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
674 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
676 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
680 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
681 dst0
[CHAN_W
] = bld
->base
.one
;
685 case TGSI_OPCODE_LOG
:
686 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
687 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
688 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
689 LLVMValueRef
*p_floor_log2
= NULL
;
690 LLVMValueRef
*p_exp
= NULL
;
691 LLVMValueRef
*p_log2
= NULL
;
693 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
694 src0
= lp_build_abs( &bld
->base
, src0
);
696 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
697 p_floor_log2
= &tmp0
;
698 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
700 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
703 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
705 /* dst.x = floor(lg2(abs(src.x))) */
706 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
708 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
709 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
710 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
712 /* dst.z = lg2(abs(src.x)) */
713 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
717 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
718 dst0
[CHAN_W
] = bld
->base
.one
;
722 case TGSI_OPCODE_MUL
:
723 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
724 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
725 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
726 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
730 case TGSI_OPCODE_ADD
:
731 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
732 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
733 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
734 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
738 case TGSI_OPCODE_DP3
:
739 /* TGSI_OPCODE_DOT3 */
740 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
741 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
742 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
743 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
744 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
745 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
746 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
747 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
748 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
749 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
750 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
751 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
752 dst0
[chan_index
] = tmp0
;
756 case TGSI_OPCODE_DP4
:
757 /* TGSI_OPCODE_DOT4 */
758 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
759 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
760 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
761 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
762 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
763 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
764 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
765 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
766 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
767 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
768 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
769 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
770 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
771 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
772 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
773 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
774 dst0
[chan_index
] = tmp0
;
778 case TGSI_OPCODE_DST
:
779 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
780 dst0
[CHAN_X
] = bld
->base
.one
;
782 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
783 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
784 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
785 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
787 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
788 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
790 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
791 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
795 case TGSI_OPCODE_MIN
:
796 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
797 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
798 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
799 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
803 case TGSI_OPCODE_MAX
:
804 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
805 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
806 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
807 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
811 case TGSI_OPCODE_SLT
:
812 /* TGSI_OPCODE_SETLT */
813 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
814 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
815 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
816 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
817 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
821 case TGSI_OPCODE_SGE
:
822 /* TGSI_OPCODE_SETGE */
823 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
824 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
825 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
826 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
827 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
831 case TGSI_OPCODE_MAD
:
832 /* TGSI_OPCODE_MADD */
833 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
834 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
835 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
836 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
837 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
838 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
839 dst0
[chan_index
] = tmp0
;
843 case TGSI_OPCODE_SUB
:
844 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
845 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
846 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
847 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
851 case TGSI_OPCODE_LRP
:
852 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
853 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
854 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
855 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
856 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
857 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
858 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
862 case TGSI_OPCODE_CND
:
863 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
864 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
865 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
866 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
867 tmp1
= lp_build_const_scalar(bld
->base
.type
, 0.5);
868 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
869 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
873 case TGSI_OPCODE_DP2A
:
874 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
875 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
876 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
877 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
878 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
879 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
880 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
881 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
882 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
883 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
884 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
888 case TGSI_OPCODE_FRC
:
889 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
890 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
891 tmp0
= lp_build_floor(&bld
->base
, src0
);
892 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
893 dst0
[chan_index
] = tmp0
;
897 case TGSI_OPCODE_CLAMP
:
898 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
899 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
900 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
901 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
902 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
903 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
904 dst0
[chan_index
] = tmp0
;
908 case TGSI_OPCODE_FLR
:
909 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
910 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
911 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
915 case TGSI_OPCODE_ROUND
:
916 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
917 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
918 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
922 case TGSI_OPCODE_EX2
: {
923 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
924 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
925 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
926 dst0
[chan_index
] = tmp0
;
931 case TGSI_OPCODE_LG2
:
932 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
933 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
934 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
935 dst0
[chan_index
] = tmp0
;
939 case TGSI_OPCODE_POW
:
940 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
941 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
942 res
= lp_build_pow( &bld
->base
, src0
, src1
);
943 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
944 dst0
[chan_index
] = res
;
948 case TGSI_OPCODE_XPD
:
949 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
950 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
951 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
952 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
954 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
955 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
956 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
957 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
959 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
961 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
963 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
964 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
967 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
968 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
969 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
970 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
972 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
973 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
974 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
975 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
978 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
979 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
980 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
981 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
984 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
985 dst0
[CHAN_W
] = bld
->base
.one
;
989 case TGSI_OPCODE_ABS
:
990 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
991 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
992 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
996 case TGSI_OPCODE_RCC
:
1001 case TGSI_OPCODE_DPH
:
1002 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1003 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1004 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1005 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1006 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1007 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1008 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1009 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1010 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1011 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1012 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1013 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1014 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1015 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1016 dst0
[chan_index
] = tmp0
;
1020 case TGSI_OPCODE_COS
:
1021 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1022 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1023 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1024 dst0
[chan_index
] = tmp0
;
1028 case TGSI_OPCODE_DDX
:
1029 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1030 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1034 case TGSI_OPCODE_DDY
:
1035 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1036 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1040 case TGSI_OPCODE_KILP
:
1041 /* predicated kill */
1046 case TGSI_OPCODE_KIL
:
1047 /* conditional kill */
1048 emit_kil( bld
, inst
);
1051 case TGSI_OPCODE_PK2H
:
1055 case TGSI_OPCODE_PK2US
:
1059 case TGSI_OPCODE_PK4B
:
1063 case TGSI_OPCODE_PK4UB
:
1067 case TGSI_OPCODE_RFL
:
1071 case TGSI_OPCODE_SEQ
:
1072 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1073 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1074 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1075 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1076 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1080 case TGSI_OPCODE_SFL
:
1081 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1082 dst0
[chan_index
] = bld
->base
.zero
;
1086 case TGSI_OPCODE_SGT
:
1087 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1088 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1089 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1090 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1091 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1095 case TGSI_OPCODE_SIN
:
1096 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1097 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1098 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1099 dst0
[chan_index
] = tmp0
;
1103 case TGSI_OPCODE_SLE
:
1104 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1105 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1106 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1107 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1108 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1112 case TGSI_OPCODE_SNE
:
1113 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1114 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1115 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1116 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1117 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1121 case TGSI_OPCODE_STR
:
1122 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1123 dst0
[chan_index
] = bld
->base
.one
;
1127 case TGSI_OPCODE_TEX
:
1128 emit_tex( bld
, inst
, FALSE
, FALSE
, dst0
);
1131 case TGSI_OPCODE_TXD
:
1136 case TGSI_OPCODE_UP2H
:
1142 case TGSI_OPCODE_UP2US
:
1148 case TGSI_OPCODE_UP4B
:
1154 case TGSI_OPCODE_UP4UB
:
1160 case TGSI_OPCODE_X2D
:
1166 case TGSI_OPCODE_ARA
:
1173 case TGSI_OPCODE_ARR
:
1175 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1176 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1177 emit_rnd( bld
, 0, 0 );
1178 emit_f2it( bld
, 0 );
1179 dst0
[chan_index
] = tmp0
;
1184 case TGSI_OPCODE_BRA
:
1190 case TGSI_OPCODE_CAL
:
1195 case TGSI_OPCODE_RET
:
1200 case TGSI_OPCODE_END
:
1203 case TGSI_OPCODE_SSG
:
1204 /* TGSI_OPCODE_SGN */
1205 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1206 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1207 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1211 case TGSI_OPCODE_CMP
:
1212 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1213 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1214 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1215 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1216 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1217 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1221 case TGSI_OPCODE_SCS
:
1222 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1223 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1224 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1226 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1227 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1228 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1230 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1231 dst0
[CHAN_Z
] = bld
->base
.zero
;
1233 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1234 dst0
[CHAN_W
] = bld
->base
.one
;
1238 case TGSI_OPCODE_TXB
:
1239 emit_tex( bld
, inst
, TRUE
, FALSE
, dst0
);
1242 case TGSI_OPCODE_NRM
:
1244 case TGSI_OPCODE_NRM4
:
1245 /* 3 or 4-component normalization */
1247 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1249 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1250 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1251 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1252 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1254 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1257 /* xmm0 = src.x * src.x */
1258 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1259 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1262 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1265 /* xmm0 = xmm0 + src.y * src.y */
1266 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1267 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1270 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1271 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1274 /* xmm0 = xmm0 + src.z * src.z */
1275 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1276 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1279 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1280 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1284 /* xmm0 = xmm0 + src.w * src.w */
1285 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1286 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1289 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1290 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1293 /* xmm1 = 1 / sqrt(xmm0) */
1294 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1296 /* dst.x = xmm1 * src.x */
1297 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1298 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1301 /* dst.y = xmm1 * src.y */
1302 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1303 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1306 /* dst.z = xmm1 * src.z */
1307 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1308 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1311 /* dst.w = xmm1 * src.w */
1312 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1313 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1318 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1319 dst0
[CHAN_W
] = bld
->base
.one
;
1324 case TGSI_OPCODE_DIV
:
1330 case TGSI_OPCODE_DP2
:
1331 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1332 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1333 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1334 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1335 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1336 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1337 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1338 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1339 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1343 case TGSI_OPCODE_TXL
:
1344 emit_tex( bld
, inst
, TRUE
, FALSE
, dst0
);
1347 case TGSI_OPCODE_TXP
:
1348 emit_tex( bld
, inst
, FALSE
, TRUE
, dst0
);
1351 case TGSI_OPCODE_BRK
:
1356 case TGSI_OPCODE_IF
:
1357 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1358 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1361 case TGSI_OPCODE_BGNFOR
:
1367 case TGSI_OPCODE_REP
:
1373 case TGSI_OPCODE_ELSE
:
1374 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1377 case TGSI_OPCODE_ENDIF
:
1378 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1381 case TGSI_OPCODE_ENDFOR
:
1387 case TGSI_OPCODE_ENDREP
:
1393 case TGSI_OPCODE_PUSHA
:
1399 case TGSI_OPCODE_POPA
:
1405 case TGSI_OPCODE_CEIL
:
1406 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1407 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1408 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1412 case TGSI_OPCODE_I2F
:
1418 case TGSI_OPCODE_NOT
:
1424 case TGSI_OPCODE_TRUNC
:
1425 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1426 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1427 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1431 case TGSI_OPCODE_SHL
:
1437 case TGSI_OPCODE_ISHR
:
1443 case TGSI_OPCODE_AND
:
1449 case TGSI_OPCODE_OR
:
1455 case TGSI_OPCODE_MOD
:
1461 case TGSI_OPCODE_XOR
:
1467 case TGSI_OPCODE_SAD
:
1473 case TGSI_OPCODE_TXF
:
1479 case TGSI_OPCODE_TXQ
:
1485 case TGSI_OPCODE_CONT
:
1490 case TGSI_OPCODE_EMIT
:
1494 case TGSI_OPCODE_ENDPRIM
:
1498 case TGSI_OPCODE_NOP
:
1506 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1507 emit_store( bld
, inst
, 0, chan_index
, dst0
[chan_index
]);
1516 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1517 const struct tgsi_token
*tokens
,
1518 struct lp_type type
,
1519 struct lp_build_mask_context
*mask
,
1520 LLVMValueRef consts_ptr
,
1521 const LLVMValueRef
*pos
,
1522 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
1523 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1524 struct lp_build_sampler_soa
*sampler
)
1526 struct lp_build_tgsi_soa_context bld
;
1527 struct tgsi_parse_context parse
;
1528 uint num_immediates
= 0;
1531 /* Setup build context */
1532 memset(&bld
, 0, sizeof bld
);
1533 lp_build_context_init(&bld
.base
, builder
, type
);
1536 bld
.inputs
= inputs
;
1537 bld
.outputs
= outputs
;
1538 bld
.consts_ptr
= consts_ptr
;
1539 bld
.sampler
= sampler
;
1541 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
1543 tgsi_parse_init( &parse
, tokens
);
1545 while( !tgsi_parse_end_of_tokens( &parse
) ) {
1546 tgsi_parse_token( &parse
);
1548 switch( parse
.FullToken
.Token
.Type
) {
1549 case TGSI_TOKEN_TYPE_DECLARATION
:
1550 /* Inputs already interpolated */
1552 if (!emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
))
1553 _debug_printf("warning: failed to define LLVM variable\n");
1557 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1559 unsigned opcode
= parse
.FullToken
.FullInstruction
.Instruction
.Opcode
;
1560 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info(opcode
);
1561 if (!emit_instruction( &bld
, &parse
.FullToken
.FullInstruction
, info
))
1562 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1563 info
? info
->mnemonic
: "<invalid>");
1568 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1569 /* simply copy the immediate values into the next immediates[] slot */
1571 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1573 assert(num_immediates
< LP_MAX_IMMEDIATES
);
1574 for( i
= 0; i
< size
; ++i
)
1575 bld
.immediates
[num_immediates
][i
] =
1576 lp_build_const_scalar(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
1577 for( i
= size
; i
< 4; ++i
)
1578 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
1583 case TGSI_TOKEN_TYPE_PROPERTY
:
1591 tgsi_parse_free( &parse
);