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_intr.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)->FullDstRegisters[0].DstRegister.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 )
82 struct lp_build_tgsi_soa_context
84 struct lp_build_context base
;
88 LLVMValueRef dadx_ptr
;
89 LLVMValueRef dady_ptr
;
91 LLVMValueRef consts_ptr
;
92 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
93 LLVMValueRef samplers_ptr
;
97 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
];
99 LLVMValueRef immediates
[LP_MAX_IMMEDIATES
][NUM_CHANNELS
];
100 LLVMValueRef temps
[LP_MAX_TEMPS
][NUM_CHANNELS
];
102 struct lp_build_mask_context
*mask
;
104 /** Coords/texels store */
105 LLVMValueRef store_ptr
;
114 struct lp_build_tgsi_soa_context
*bld
,
115 const struct tgsi_full_instruction
*inst
,
117 const unsigned chan_index
)
119 const struct tgsi_full_src_register
*reg
= &inst
->FullSrcRegisters
[index
];
120 unsigned swizzle
= tgsi_util_get_full_src_register_extswizzle( reg
, chan_index
);
124 case TGSI_EXTSWIZZLE_X
:
125 case TGSI_EXTSWIZZLE_Y
:
126 case TGSI_EXTSWIZZLE_Z
:
127 case TGSI_EXTSWIZZLE_W
:
129 switch (reg
->SrcRegister
.File
) {
130 case TGSI_FILE_CONSTANT
: {
131 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), reg
->SrcRegister
.Index
*4 + swizzle
, 0);
132 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
, &index
, 1, "");
133 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
134 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
138 case TGSI_FILE_IMMEDIATE
:
139 res
= bld
->immediates
[reg
->SrcRegister
.Index
][swizzle
];
143 case TGSI_FILE_INPUT
:
144 res
= bld
->inputs
[reg
->SrcRegister
.Index
][swizzle
];
148 case TGSI_FILE_TEMPORARY
:
149 res
= bld
->temps
[reg
->SrcRegister
.Index
][swizzle
];
151 return bld
->base
.undef
;
156 return bld
->base
.undef
;
160 case TGSI_EXTSWIZZLE_ZERO
:
161 res
= bld
->base
.zero
;
164 case TGSI_EXTSWIZZLE_ONE
:
170 return bld
->base
.undef
;
173 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
174 case TGSI_UTIL_SIGN_CLEAR
:
175 res
= lp_build_abs( &bld
->base
, res
);
178 case TGSI_UTIL_SIGN_SET
:
179 res
= lp_build_abs( &bld
->base
, res
);
180 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
183 case TGSI_UTIL_SIGN_TOGGLE
:
184 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
187 case TGSI_UTIL_SIGN_KEEP
:
200 struct lp_build_tgsi_soa_context
*bld
,
201 const struct tgsi_full_instruction
*inst
,
206 const struct tgsi_full_dst_register
*reg
= &inst
->FullDstRegisters
[index
];
208 switch( inst
->Instruction
.Saturate
) {
212 case TGSI_SAT_ZERO_ONE
:
213 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
214 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
217 case TGSI_SAT_MINUS_PLUS_ONE
:
218 value
= lp_build_max(&bld
->base
, value
, lp_build_const_scalar(bld
->base
.type
, -1.0));
219 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
226 switch( reg
->DstRegister
.File
) {
227 case TGSI_FILE_OUTPUT
:
228 bld
->outputs
[reg
->DstRegister
.Index
][chan_index
] = value
;
231 case TGSI_FILE_TEMPORARY
:
232 bld
->temps
[reg
->DstRegister
.Index
][chan_index
] = value
;
235 case TGSI_FILE_ADDRESS
:
247 lp_build_tgsi_fetch_texel_soa( struct tgsi_sampler
**samplers
,
251 struct tgsi_sampler
*sampler
= samplers
[unit
];
256 debug_printf("%s sampler: %p (%p) store: %p\n",
261 debug_printf("lodbias %f\n", store
[12]);
263 for (j
= 0; j
< 4; j
++)
264 debug_printf("sample %d texcoord %f %f\n",
271 float rgba
[NUM_CHANNELS
][QUAD_SIZE
];
272 sampler
->get_samples(sampler
,
276 0.0f
, /*store[12], lodbias */
278 memcpy(store
, rgba
, sizeof rgba
);
282 for (j
= 0; j
< 4; j
++)
283 debug_printf("sample %d result %f %f %f %f\n",
293 * High-level instruction translators.
297 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
298 const struct tgsi_full_instruction
*inst
,
299 boolean apply_lodbias
,
302 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
303 const uint unit
= inst
->FullSrcRegisters
[1].SrcRegister
.Index
;
304 LLVMValueRef lodbias
;
306 LLVMValueRef args
[3];
310 switch (inst
->InstructionExtTexture
.Texture
) {
311 case TGSI_TEXTURE_1D
:
312 case TGSI_TEXTURE_SHADOW1D
:
315 case TGSI_TEXTURE_2D
:
316 case TGSI_TEXTURE_RECT
:
317 case TGSI_TEXTURE_SHADOW2D
:
318 case TGSI_TEXTURE_SHADOWRECT
:
321 case TGSI_TEXTURE_3D
:
322 case TGSI_TEXTURE_CUBE
:
331 lodbias
= emit_fetch( bld
, inst
, 0, 3 );
333 lodbias
= bld
->base
.zero
;
336 bld
->store_ptr
= LLVMBuildArrayAlloca(bld
->base
.builder
,
338 LLVMConstInt(LLVMInt32Type(), 4, 0),
342 oow
= emit_fetch( bld
, inst
, 0, 3 );
343 oow
= lp_build_rcp(&bld
->base
, oow
);
346 for (i
= 0; i
< count
; i
++) {
347 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
348 LLVMValueRef coord_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->store_ptr
, &index
, 1, "");
351 coord
= emit_fetch( bld
, inst
, 0, i
);
354 coord
= lp_build_mul(&bld
->base
, coord
, oow
);
356 LLVMBuildStore(bld
->base
.builder
, coord
, coord_ptr
);
359 args
[0] = bld
->samplers_ptr
;
360 args
[1] = LLVMConstInt(LLVMInt32Type(), unit
, 0);
361 args
[2] = bld
->store_ptr
;
363 lp_build_intrinsic(bld
->base
.builder
, "fetch_texel", LLVMVoidType(), args
, 3);
365 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, i
) {
366 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
367 LLVMValueRef res_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->store_ptr
, &index
, 1, "");
368 LLVMValueRef res
= LLVMBuildLoad(bld
->base
.builder
, res_ptr
, "");
369 emit_store( bld
, inst
, 0, i
, res
);
376 struct lp_build_tgsi_soa_context
*bld
,
377 const struct tgsi_full_instruction
*inst
)
379 const struct tgsi_full_src_register
*reg
= &inst
->FullSrcRegisters
[0];
380 LLVMValueRef terms
[NUM_CHANNELS
];
384 memset(&terms
, 0, sizeof terms
);
386 FOR_EACH_CHANNEL( chan_index
) {
389 /* Unswizzle channel */
390 swizzle
= tgsi_util_get_full_src_register_extswizzle( reg
, chan_index
);
392 /* Note that we test if the value is less than zero, so 1.0 and 0.0 need
393 * not to be tested. */
394 if(swizzle
== TGSI_EXTSWIZZLE_ZERO
|| swizzle
== TGSI_EXTSWIZZLE_ONE
)
397 /* Check if the component has not been already tested. */
398 assert(swizzle
< NUM_CHANNELS
);
399 if( !terms
[swizzle
] )
400 /* TODO: change the comparison operator instead of setting the sign */
401 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
405 FOR_EACH_CHANNEL( chan_index
) {
406 if(terms
[chan_index
]) {
407 LLVMValueRef chan_mask
;
409 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
412 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
419 lp_build_mask_update(bld
->mask
, mask
);
425 struct lp_build_tgsi_soa_context
*bld
)
427 /* XXX todo / fix me */
432 * Check if inst src/dest regs use indirect addressing into temporary
436 indirect_temp_reference(const struct tgsi_full_instruction
*inst
)
439 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
440 const struct tgsi_full_src_register
*reg
= &inst
->FullSrcRegisters
[i
];
441 if (reg
->SrcRegister
.File
== TGSI_FILE_TEMPORARY
&&
442 reg
->SrcRegister
.Indirect
)
445 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
446 const struct tgsi_full_dst_register
*reg
= &inst
->FullDstRegisters
[i
];
447 if (reg
->DstRegister
.File
== TGSI_FILE_TEMPORARY
&&
448 reg
->DstRegister
.Indirect
)
457 struct lp_build_tgsi_soa_context
*bld
,
458 struct tgsi_full_instruction
*inst
)
461 LLVMValueRef src0
, src1
, src2
;
462 LLVMValueRef tmp0
, tmp1
, tmp2
, tmp3
, tmp4
, tmp5
, tmp6
, tmp7
;
465 /* we can't handle indirect addressing into temp register file yet */
466 if (indirect_temp_reference(inst
))
469 switch (inst
->Instruction
.Opcode
) {
471 case TGSI_OPCODE_ARL
:
472 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
473 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
476 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
481 case TGSI_OPCODE_MOV
:
482 case TGSI_OPCODE_SWZ
:
483 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
484 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
485 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
489 case TGSI_OPCODE_LIT
:
490 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
491 emit_store( bld
, inst
, 0, CHAN_X
, bld
->base
.one
);
493 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
494 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
495 dst0
= lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
496 emit_store( bld
, inst
, 0, CHAN_Y
, dst0
);
498 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
499 /* XMM[1] = SrcReg[0].yyyy */
500 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
501 /* XMM[1] = max(XMM[1], 0) */
502 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
503 /* XMM[2] = SrcReg[0].wwww */
504 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
505 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
506 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
507 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
508 dst0
= lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
509 emit_store( bld
, inst
, 0, CHAN_Z
, dst0
);
511 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
512 emit_store( bld
, inst
, 0, CHAN_W
, bld
->base
.one
);
516 case TGSI_OPCODE_RCP
:
517 /* TGSI_OPCODE_RECIP */
518 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
519 dst0
= lp_build_rcp(&bld
->base
, src0
);
520 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
521 emit_store( bld
, inst
, 0, chan_index
, dst0
);
525 case TGSI_OPCODE_RSQ
:
526 /* TGSI_OPCODE_RECIPSQRT */
527 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
528 src0
= lp_build_abs(&bld
->base
, src0
);
529 dst0
= lp_build_rsqrt(&bld
->base
, src0
);
530 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
531 emit_store( bld
, inst
, 0, chan_index
, dst0
);
535 case TGSI_OPCODE_EXP
:
536 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
537 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
538 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
539 LLVMValueRef
*p_exp2_int_part
= NULL
;
540 LLVMValueRef
*p_frac_part
= NULL
;
541 LLVMValueRef
*p_exp2
= NULL
;
543 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
545 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
546 p_exp2_int_part
= &tmp0
;
547 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
549 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
552 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
554 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
555 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
556 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
557 emit_store( bld
, inst
, 0, CHAN_Y
, tmp1
);
558 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
559 emit_store( bld
, inst
, 0, CHAN_Z
, tmp2
);
562 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
563 tmp0
= bld
->base
.one
;
564 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
568 case TGSI_OPCODE_LOG
:
569 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
570 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
571 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
572 LLVMValueRef
*p_floor_log2
;
574 LLVMValueRef
*p_log2
;
576 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
577 src0
= lp_build_abs( &bld
->base
, src0
);
579 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
580 p_floor_log2
= &tmp0
;
581 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
583 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
586 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
588 /* dst.x = floor(lg2(abs(src.x))) */
589 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
590 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
591 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
592 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
593 tmp1
= lp_build_div( &bld
->base
, src0
, tmp1
);
594 emit_store( bld
, inst
, 0, CHAN_Y
, tmp1
);
596 /* dst.z = lg2(abs(src.x)) */
597 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
598 emit_store( bld
, inst
, 0, CHAN_Z
, tmp2
);
601 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
602 tmp0
= bld
->base
.one
;
603 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
607 case TGSI_OPCODE_MUL
:
608 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
609 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
610 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
611 dst0
= lp_build_mul(&bld
->base
, src0
, src1
);
612 emit_store( bld
, inst
, 0, chan_index
, dst0
);
616 case TGSI_OPCODE_ADD
:
617 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
618 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
619 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
620 dst0
= lp_build_add(&bld
->base
, src0
, src1
);
621 emit_store( bld
, inst
, 0, chan_index
, dst0
);
625 case TGSI_OPCODE_DP3
:
626 /* TGSI_OPCODE_DOT3 */
627 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
628 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
629 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
630 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
631 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
632 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
633 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
634 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
635 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
636 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
637 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
638 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
639 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
643 case TGSI_OPCODE_DP4
:
644 /* TGSI_OPCODE_DOT4 */
645 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
646 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
647 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
648 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
649 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
650 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
651 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
652 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
653 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
654 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
655 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
656 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
657 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
658 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
659 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
660 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
661 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
665 case TGSI_OPCODE_DST
:
666 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
667 tmp0
= bld
->base
.one
;
668 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
670 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
671 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
672 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
673 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
674 emit_store( bld
, inst
, 0, CHAN_Y
, tmp0
);
676 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
677 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
678 emit_store( bld
, inst
, 0, CHAN_Z
, tmp0
);
680 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
681 tmp0
= emit_fetch( bld
, inst
, 1, CHAN_W
);
682 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
686 case TGSI_OPCODE_MIN
:
687 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
688 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
689 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
690 dst0
= lp_build_min( &bld
->base
, src0
, src1
);
691 emit_store( bld
, inst
, 0, chan_index
, dst0
);
695 case TGSI_OPCODE_MAX
:
696 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
697 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
698 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
699 dst0
= lp_build_max( &bld
->base
, src0
, src1
);
700 emit_store( bld
, inst
, 0, chan_index
, dst0
);
704 case TGSI_OPCODE_SLT
:
705 /* TGSI_OPCODE_SETLT */
706 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
707 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
708 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
709 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
710 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
711 emit_store( bld
, inst
, 0, chan_index
, dst0
);
715 case TGSI_OPCODE_SGE
:
716 /* TGSI_OPCODE_SETGE */
717 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
718 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
719 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
720 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
721 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
722 emit_store( bld
, inst
, 0, chan_index
, dst0
);
726 case TGSI_OPCODE_MAD
:
727 /* TGSI_OPCODE_MADD */
728 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
729 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
730 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
731 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
732 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
733 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
734 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
738 case TGSI_OPCODE_SUB
:
739 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
740 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
741 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
742 tmp0
= lp_build_sub( &bld
->base
, tmp0
, tmp1
);
743 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
747 case TGSI_OPCODE_LRP
:
748 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
749 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
750 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
751 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
752 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
753 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
754 dst0
= lp_build_add( &bld
->base
, tmp0
, src2
);
755 emit_store( bld
, inst
, 0, chan_index
, dst0
);
759 case TGSI_OPCODE_CND
:
763 case TGSI_OPCODE_CND0
:
767 case TGSI_OPCODE_DP2A
:
768 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
769 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
770 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
771 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
772 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
773 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
774 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
775 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
776 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
777 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
778 emit_store( bld
, inst
, 0, chan_index
, tmp0
); /* dest[ch] = xmm0 */
783 case TGSI_OPCODE_FRC
:
784 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
785 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
786 emit_frc( bld
, 0, 0 );
787 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
791 case TGSI_OPCODE_CLAMP
:
795 case TGSI_OPCODE_FLR
:
796 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
797 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
798 emit_flr( bld
, 0, 0 );
799 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
803 case TGSI_OPCODE_ROUND
:
804 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
805 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
806 emit_rnd( bld
, 0, 0 );
807 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
812 case TGSI_OPCODE_EX2
: {
813 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
814 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
815 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
816 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
821 case TGSI_OPCODE_LG2
:
822 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
823 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
824 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
825 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
829 case TGSI_OPCODE_POW
:
830 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
831 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
832 dst0
= lp_build_pow( &bld
->base
, src0
, src1
);
833 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
834 emit_store( bld
, inst
, 0, chan_index
, dst0
);
838 case TGSI_OPCODE_XPD
:
839 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
840 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
841 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
842 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
844 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
845 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
846 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
847 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
849 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
851 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
853 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
854 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
855 emit_store( bld
, inst
, 0, CHAN_X
, tmp2
);
857 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
858 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
859 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
860 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
862 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
863 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
864 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
865 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
866 emit_store( bld
, inst
, 0, CHAN_Y
, tmp3
);
868 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
869 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
870 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
871 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
872 emit_store( bld
, inst
, 0, CHAN_Z
, tmp5
);
874 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
875 tmp0
= bld
->base
.one
;
876 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
880 case TGSI_OPCODE_ABS
:
881 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
882 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
883 tmp0
= lp_build_abs( &bld
->base
, tmp0
) ;
884 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
888 case TGSI_OPCODE_RCC
:
892 case TGSI_OPCODE_DPH
:
893 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
894 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
895 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
896 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
897 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
898 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
899 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
900 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
901 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
902 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
903 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
904 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
905 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
906 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
907 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
911 case TGSI_OPCODE_COS
:
912 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
913 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
914 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
915 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
919 case TGSI_OPCODE_DDX
:
923 case TGSI_OPCODE_DDY
:
928 case TGSI_OPCODE_KILP
:
929 /* predicated kill */
931 return 0; /* XXX fix me */
935 case TGSI_OPCODE_KIL
:
936 /* conditional kill */
937 emit_kil( bld
, inst
);
940 case TGSI_OPCODE_PK2H
:
944 case TGSI_OPCODE_PK2US
:
948 case TGSI_OPCODE_PK4B
:
952 case TGSI_OPCODE_PK4UB
:
956 case TGSI_OPCODE_RFL
:
960 case TGSI_OPCODE_SEQ
:
961 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
962 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
963 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
964 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
965 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
966 emit_store( bld
, inst
, 0, chan_index
, dst0
);
970 case TGSI_OPCODE_SFL
:
974 case TGSI_OPCODE_SGT
:
975 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
976 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
977 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
978 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
979 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
980 emit_store( bld
, inst
, 0, chan_index
, dst0
);
984 case TGSI_OPCODE_SIN
:
985 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
986 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
987 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
988 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
992 case TGSI_OPCODE_SLE
:
993 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
994 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
995 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
996 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
997 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
998 emit_store( bld
, inst
, 0, chan_index
, dst0
);
1002 case TGSI_OPCODE_SNE
:
1003 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1004 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1005 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1006 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1007 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1008 emit_store( bld
, inst
, 0, chan_index
, dst0
);
1012 case TGSI_OPCODE_STR
:
1016 case TGSI_OPCODE_TEX
:
1017 emit_tex( bld
, inst
, FALSE
, FALSE
);
1020 case TGSI_OPCODE_TXD
:
1024 case TGSI_OPCODE_UP2H
:
1028 case TGSI_OPCODE_UP2US
:
1032 case TGSI_OPCODE_UP4B
:
1036 case TGSI_OPCODE_UP4UB
:
1040 case TGSI_OPCODE_X2D
:
1044 case TGSI_OPCODE_ARA
:
1049 case TGSI_OPCODE_ARR
:
1050 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1051 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1052 emit_rnd( bld
, 0, 0 );
1053 emit_f2it( bld
, 0 );
1054 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
1059 case TGSI_OPCODE_BRA
:
1063 case TGSI_OPCODE_CAL
:
1068 case TGSI_OPCODE_RET
:
1073 case TGSI_OPCODE_END
:
1077 case TGSI_OPCODE_SSG
:
1078 /* TGSI_OPCODE_SGN */
1079 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1080 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1081 emit_sgn( bld
, 0, 0 );
1082 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
1087 case TGSI_OPCODE_CMP
:
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 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1092 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1093 dst0
= lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1094 emit_store( bld
, inst
, 0, chan_index
, dst0
);
1098 case TGSI_OPCODE_SCS
:
1099 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1100 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1101 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1102 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
1104 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1105 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1106 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1107 emit_store( bld
, inst
, 0, CHAN_Y
, tmp0
);
1109 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1110 tmp0
= bld
->base
.zero
;
1111 emit_store( bld
, inst
, 0, CHAN_Z
, tmp0
);
1113 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1114 tmp0
= bld
->base
.one
;
1115 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
1119 case TGSI_OPCODE_TXB
:
1120 emit_tex( bld
, inst
, TRUE
, FALSE
);
1123 case TGSI_OPCODE_NRM
:
1125 case TGSI_OPCODE_NRM4
:
1126 /* 3 or 4-component normalization */
1128 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1130 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1131 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1132 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1133 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1135 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1138 /* xmm0 = src.x * src.x */
1139 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1140 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1143 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1146 /* xmm0 = xmm0 + src.y * src.y */
1147 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1148 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1151 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1152 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1155 /* xmm0 = xmm0 + src.z * src.z */
1156 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1157 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1160 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1161 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1165 /* xmm0 = xmm0 + src.w * src.w */
1166 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1167 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1170 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1171 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1174 /* xmm1 = 1 / sqrt(xmm0) */
1175 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1177 /* dst.x = xmm1 * src.x */
1178 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1179 tmp4
= lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1180 emit_store(bld
, inst
, 0, CHAN_X
, tmp4
);
1183 /* dst.y = xmm1 * src.y */
1184 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1185 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1186 emit_store(bld
, inst
, 0, CHAN_Y
, tmp5
);
1189 /* dst.z = xmm1 * src.z */
1190 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1191 tmp6
= lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1192 emit_store(bld
, inst
, 0, CHAN_Z
, tmp6
);
1195 /* dst.w = xmm1 * src.w */
1196 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1197 tmp7
= lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1198 emit_store(bld
, inst
, 0, CHAN_W
, tmp7
);
1203 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1204 tmp0
= bld
->base
.one
;
1205 emit_store(bld
, inst
, 0, CHAN_W
, tmp0
);
1210 case TGSI_OPCODE_DIV
:
1214 case TGSI_OPCODE_DP2
:
1215 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1216 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1217 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1218 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1219 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1220 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1221 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1222 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1223 emit_store( bld
, inst
, 0, chan_index
, tmp0
); /* dest[ch] = xmm0 */
1227 case TGSI_OPCODE_TXL
:
1228 emit_tex( bld
, inst
, TRUE
, FALSE
);
1231 case TGSI_OPCODE_TXP
:
1232 emit_tex( bld
, inst
, FALSE
, TRUE
);
1235 case TGSI_OPCODE_BRK
:
1239 case TGSI_OPCODE_IF
:
1243 case TGSI_OPCODE_LOOP
:
1247 case TGSI_OPCODE_REP
:
1251 case TGSI_OPCODE_ELSE
:
1255 case TGSI_OPCODE_ENDIF
:
1259 case TGSI_OPCODE_ENDLOOP
:
1263 case TGSI_OPCODE_ENDREP
:
1267 case TGSI_OPCODE_PUSHA
:
1271 case TGSI_OPCODE_POPA
:
1275 case TGSI_OPCODE_CEIL
:
1279 case TGSI_OPCODE_I2F
:
1283 case TGSI_OPCODE_NOT
:
1288 case TGSI_OPCODE_TRUNC
:
1289 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1290 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1291 emit_f2it( bld
, 0 );
1293 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
1298 case TGSI_OPCODE_SHL
:
1302 case TGSI_OPCODE_SHR
:
1306 case TGSI_OPCODE_AND
:
1310 case TGSI_OPCODE_OR
:
1314 case TGSI_OPCODE_MOD
:
1318 case TGSI_OPCODE_XOR
:
1322 case TGSI_OPCODE_SAD
:
1326 case TGSI_OPCODE_TXF
:
1330 case TGSI_OPCODE_TXQ
:
1334 case TGSI_OPCODE_CONT
:
1338 case TGSI_OPCODE_EMIT
:
1342 case TGSI_OPCODE_ENDPRIM
:
1355 struct lp_build_tgsi_soa_context
*bld
,
1356 struct tgsi_full_declaration
*decl
)
1358 if( decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
1359 LLVMBuilderRef builder
= bld
->base
.builder
;
1360 unsigned first
, last
, mask
;
1361 unsigned attrib
, chan
;
1363 first
= decl
->DeclarationRange
.First
;
1364 last
= decl
->DeclarationRange
.Last
;
1365 mask
= decl
->Declaration
.UsageMask
;
1367 for( attrib
= first
; attrib
<= last
; attrib
++ ) {
1368 for( chan
= 0; chan
< NUM_CHANNELS
; chan
++ ) {
1369 LLVMValueRef input
= bld
->base
.undef
;
1371 if( mask
& (1 << chan
) ) {
1372 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), (1 + attrib
)*NUM_CHANNELS
+ chan
, 0);
1377 switch( decl
->Declaration
.Interpolate
) {
1378 case TGSI_INTERPOLATE_PERSPECTIVE
:
1381 case TGSI_INTERPOLATE_LINEAR
: {
1382 LLVMValueRef dadx_ptr
= LLVMBuildGEP(builder
, bld
->dadx_ptr
, &index
, 1, "");
1383 LLVMValueRef dady_ptr
= LLVMBuildGEP(builder
, bld
->dady_ptr
, &index
, 1, "");
1384 dadx
= LLVMBuildLoad(builder
, dadx_ptr
, "");
1385 dady
= LLVMBuildLoad(builder
, dady_ptr
, "");
1386 dadx
= lp_build_broadcast_scalar(&bld
->base
, dadx
);
1387 dady
= lp_build_broadcast_scalar(&bld
->base
, dady
);
1388 lp_build_name(dadx
, "dadx_%u.%c", attrib
, "xyzw"[chan
]);
1389 lp_build_name(dady
, "dady_%u.%c", attrib
, "xyzw"[chan
]);
1393 case TGSI_INTERPOLATE_CONSTANT
: {
1394 LLVMValueRef a0_ptr
= LLVMBuildGEP(builder
, bld
->a0_ptr
, &index
, 1, "");
1395 a0
= LLVMBuildLoad(builder
, a0_ptr
, "");
1396 a0
= lp_build_broadcast_scalar(&bld
->base
, a0
);
1397 lp_build_name(a0
, "a0_%u.%c", attrib
, "xyzw"[chan
]);
1408 if (decl
->Declaration
.Interpolate
!= TGSI_INTERPOLATE_CONSTANT
) {
1409 input
= lp_build_add(&bld
->base
, input
, lp_build_mul(&bld
->base
, bld
->x
, dadx
));
1410 input
= lp_build_add(&bld
->base
, input
, lp_build_mul(&bld
->base
, bld
->y
, dady
));
1413 if (decl
->Declaration
.Interpolate
== TGSI_INTERPOLATE_PERSPECTIVE
) {
1415 bld
->oow
= lp_build_rcp(&bld
->base
, bld
->w
);
1416 input
= lp_build_mul(&bld
->base
, input
, bld
->oow
);
1419 lp_build_name(input
, "input%u.%c", attrib
, "xyzw"[chan
]);
1422 bld
->inputs
[attrib
][chan
] = input
;
1430 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1431 const struct tgsi_token
*tokens
,
1433 struct lp_build_mask_context
*mask
,
1435 LLVMValueRef a0_ptr
,
1436 LLVMValueRef dadx_ptr
,
1437 LLVMValueRef dady_ptr
,
1438 LLVMValueRef consts_ptr
,
1439 LLVMValueRef (*outputs
)[4],
1440 LLVMValueRef samplers_ptr
)
1442 struct lp_build_tgsi_soa_context bld
;
1443 struct tgsi_parse_context parse
;
1444 uint num_immediates
= 0;
1447 /* Setup build context */
1448 memset(&bld
, 0, sizeof bld
);
1449 lp_build_context_init(&bld
.base
, builder
, type
);
1454 bld
.a0_ptr
= a0_ptr
;
1455 bld
.dadx_ptr
= dadx_ptr
;
1456 bld
.dady_ptr
= dady_ptr
;
1457 bld
.outputs
= outputs
;
1458 bld
.consts_ptr
= consts_ptr
;
1459 bld
.samplers_ptr
= samplers_ptr
;
1461 tgsi_parse_init( &parse
, tokens
);
1463 while( !tgsi_parse_end_of_tokens( &parse
) ) {
1464 tgsi_parse_token( &parse
);
1466 switch( parse
.FullToken
.Token
.Type
) {
1467 case TGSI_TOKEN_TYPE_DECLARATION
:
1468 if (parse
.FullHeader
.Processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
) {
1469 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
1473 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1474 if (!emit_instruction( &bld
, &parse
.FullToken
.FullInstruction
)) {
1475 unsigned opcode
= parse
.FullToken
.FullInstruction
.Instruction
.Opcode
;
1476 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info(opcode
);
1477 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1478 info
? info
->mnemonic
: "<invalid>");
1482 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1483 /* simply copy the immediate values into the next immediates[] slot */
1485 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1487 assert(num_immediates
< LP_MAX_IMMEDIATES
);
1488 for( i
= 0; i
< size
; ++i
)
1489 bld
.immediates
[num_immediates
][i
] =
1490 lp_build_const_scalar(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
1491 for( i
= size
; i
< 4; ++i
)
1492 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
1502 tgsi_parse_free( &parse
);