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 **************************************************************************/
29 #include "pipe/p_config.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "util/u_debug.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "tgsi/tgsi_info.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_util.h"
37 #include "tgsi/tgsi_exec.h"
38 #include "lp_bld_type.h"
39 #include "lp_bld_const.h"
40 #include "lp_bld_intr.h"
41 #include "lp_bld_arit.h"
42 #include "lp_bld_logic.h"
43 #include "lp_bld_swizzle.h"
44 #include "lp_bld_flow.h"
45 #include "lp_bld_tgsi.h"
46 #include "lp_bld_debug.h"
49 #define LP_MAX_TEMPS 256
50 #define LP_MAX_IMMEDIATES 256
53 #define FOR_EACH_CHANNEL( CHAN )\
54 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
56 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
57 ((INST)->FullDstRegisters[0].DstRegister.WriteMask & (1 << (CHAN)))
59 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
60 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
62 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
63 FOR_EACH_CHANNEL( CHAN )\
64 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
72 struct lp_build_tgsi_soa_context
74 struct lp_build_context base
;
78 LLVMValueRef dadx_ptr
;
79 LLVMValueRef dady_ptr
;
81 LLVMValueRef consts_ptr
;
82 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
83 LLVMValueRef samplers_ptr
;
87 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
];
89 LLVMValueRef immediates
[LP_MAX_IMMEDIATES
][NUM_CHANNELS
];
90 LLVMValueRef temps
[LP_MAX_TEMPS
][NUM_CHANNELS
];
92 struct lp_build_mask_context
*mask
;
94 /** Coords/texels store */
95 LLVMValueRef store_ptr
;
104 struct lp_build_tgsi_soa_context
*bld
,
105 const struct tgsi_full_instruction
*inst
,
107 const unsigned chan_index
)
109 const struct tgsi_full_src_register
*reg
= &inst
->FullSrcRegisters
[index
];
110 unsigned swizzle
= tgsi_util_get_full_src_register_extswizzle( reg
, chan_index
);
114 case TGSI_EXTSWIZZLE_X
:
115 case TGSI_EXTSWIZZLE_Y
:
116 case TGSI_EXTSWIZZLE_Z
:
117 case TGSI_EXTSWIZZLE_W
:
119 switch (reg
->SrcRegister
.File
) {
120 case TGSI_FILE_CONSTANT
: {
121 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), reg
->SrcRegister
.Index
*4 + swizzle
, 0);
122 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
, &index
, 1, "");
123 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
124 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
128 case TGSI_FILE_IMMEDIATE
:
129 res
= bld
->immediates
[reg
->SrcRegister
.Index
][swizzle
];
133 case TGSI_FILE_INPUT
:
134 res
= bld
->inputs
[reg
->SrcRegister
.Index
][swizzle
];
138 case TGSI_FILE_TEMPORARY
:
139 res
= bld
->temps
[reg
->SrcRegister
.Index
][swizzle
];
141 return bld
->base
.undef
;
146 return bld
->base
.undef
;
150 case TGSI_EXTSWIZZLE_ZERO
:
151 res
= bld
->base
.zero
;
154 case TGSI_EXTSWIZZLE_ONE
:
160 return bld
->base
.undef
;
163 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
164 case TGSI_UTIL_SIGN_CLEAR
:
165 res
= lp_build_abs( &bld
->base
, res
);
168 case TGSI_UTIL_SIGN_SET
:
169 res
= lp_build_abs( &bld
->base
, res
);
170 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
173 case TGSI_UTIL_SIGN_TOGGLE
:
174 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
177 case TGSI_UTIL_SIGN_KEEP
:
190 struct lp_build_tgsi_soa_context
*bld
,
191 const struct tgsi_full_instruction
*inst
,
196 const struct tgsi_full_dst_register
*reg
= &inst
->FullDstRegisters
[index
];
198 switch( inst
->Instruction
.Saturate
) {
202 case TGSI_SAT_ZERO_ONE
:
203 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
204 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
207 case TGSI_SAT_MINUS_PLUS_ONE
:
208 value
= lp_build_max(&bld
->base
, value
, lp_build_const_uni(bld
->base
.type
, -1.0));
209 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
216 switch( reg
->DstRegister
.File
) {
217 case TGSI_FILE_OUTPUT
:
218 bld
->outputs
[reg
->DstRegister
.Index
][chan_index
] = value
;
221 case TGSI_FILE_TEMPORARY
:
222 bld
->temps
[reg
->DstRegister
.Index
][chan_index
] = value
;
225 case TGSI_FILE_ADDRESS
:
237 lp_build_tgsi_fetch_texel_soa( struct tgsi_sampler
**samplers
,
241 struct tgsi_sampler
*sampler
= samplers
[unit
];
246 debug_printf("%s sampler: %p (%p) store: %p\n",
251 debug_printf("lodbias %f\n", store
[12]);
253 for (j
= 0; j
< 4; j
++)
254 debug_printf("sample %d texcoord %f %f\n",
261 float rgba
[NUM_CHANNELS
][QUAD_SIZE
];
262 sampler
->get_samples(sampler
,
266 0.0f
, /*store[12], lodbias */
268 memcpy(store
, rgba
, sizeof rgba
);
272 for (j
= 0; j
< 4; j
++)
273 debug_printf("sample %d result %f %f %f %f\n",
283 * High-level instruction translators.
287 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
288 const struct tgsi_full_instruction
*inst
,
289 boolean apply_lodbias
,
292 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
293 const uint unit
= inst
->FullSrcRegisters
[1].SrcRegister
.Index
;
294 LLVMValueRef lodbias
;
296 LLVMValueRef args
[3];
300 switch (inst
->InstructionExtTexture
.Texture
) {
301 case TGSI_TEXTURE_1D
:
302 case TGSI_TEXTURE_SHADOW1D
:
305 case TGSI_TEXTURE_2D
:
306 case TGSI_TEXTURE_RECT
:
307 case TGSI_TEXTURE_SHADOW2D
:
308 case TGSI_TEXTURE_SHADOWRECT
:
311 case TGSI_TEXTURE_3D
:
312 case TGSI_TEXTURE_CUBE
:
321 lodbias
= emit_fetch( bld
, inst
, 0, 3 );
323 lodbias
= bld
->base
.zero
;
326 bld
->store_ptr
= LLVMBuildArrayAlloca(bld
->base
.builder
,
328 LLVMConstInt(LLVMInt32Type(), 4, 0),
332 oow
= emit_fetch( bld
, inst
, 0, 3 );
333 oow
= lp_build_rcp(&bld
->base
, oow
);
336 for (i
= 0; i
< count
; i
++) {
337 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
338 LLVMValueRef coord_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->store_ptr
, &index
, 1, "");
341 coord
= emit_fetch( bld
, inst
, 0, i
);
344 coord
= lp_build_mul(&bld
->base
, coord
, oow
);
346 LLVMBuildStore(bld
->base
.builder
, coord
, coord_ptr
);
349 args
[0] = bld
->samplers_ptr
;
350 args
[1] = LLVMConstInt(LLVMInt32Type(), unit
, 0);
351 args
[2] = bld
->store_ptr
;
353 lp_build_intrinsic(bld
->base
.builder
, "fetch_texel", LLVMVoidType(), args
, 3);
355 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, i
) {
356 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
357 LLVMValueRef res_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->store_ptr
, &index
, 1, "");
358 LLVMValueRef res
= LLVMBuildLoad(bld
->base
.builder
, res_ptr
, "");
359 emit_store( bld
, inst
, 0, i
, res
);
366 struct lp_build_tgsi_soa_context
*bld
,
367 const struct tgsi_full_instruction
*inst
)
369 const struct tgsi_full_src_register
*reg
= &inst
->FullSrcRegisters
[0];
370 LLVMValueRef terms
[NUM_CHANNELS
];
374 memset(&terms
, 0, sizeof terms
);
376 FOR_EACH_CHANNEL( chan_index
) {
379 /* Unswizzle channel */
380 swizzle
= tgsi_util_get_full_src_register_extswizzle( reg
, chan_index
);
382 /* Note that we test if the value is less than zero, so 1.0 and 0.0 need
383 * not to be tested. */
384 if(swizzle
== TGSI_EXTSWIZZLE_ZERO
|| swizzle
== TGSI_EXTSWIZZLE_ONE
)
387 /* Check if the component has not been already tested. */
388 assert(swizzle
< NUM_CHANNELS
);
389 if( !terms
[swizzle
] )
390 /* TODO: change the comparison operator instead of setting the sign */
391 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
395 FOR_EACH_CHANNEL( chan_index
) {
396 if(terms
[chan_index
]) {
397 LLVMValueRef chan_mask
;
399 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
402 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
409 lp_build_mask_update(bld
->mask
, mask
);
415 struct lp_build_tgsi_soa_context
*bld
)
417 /* XXX todo / fix me */
422 * Check if inst src/dest regs use indirect addressing into temporary
426 indirect_temp_reference(const struct tgsi_full_instruction
*inst
)
429 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
430 const struct tgsi_full_src_register
*reg
= &inst
->FullSrcRegisters
[i
];
431 if (reg
->SrcRegister
.File
== TGSI_FILE_TEMPORARY
&&
432 reg
->SrcRegister
.Indirect
)
435 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
436 const struct tgsi_full_dst_register
*reg
= &inst
->FullDstRegisters
[i
];
437 if (reg
->DstRegister
.File
== TGSI_FILE_TEMPORARY
&&
438 reg
->DstRegister
.Indirect
)
447 struct lp_build_tgsi_soa_context
*bld
,
448 struct tgsi_full_instruction
*inst
)
451 LLVMValueRef src0
, src1
, src2
;
452 LLVMValueRef tmp0
, tmp1
, tmp2
, tmp3
, tmp4
, tmp5
, tmp6
, tmp7
;
455 /* we can't handle indirect addressing into temp register file yet */
456 if (indirect_temp_reference(inst
))
459 switch (inst
->Instruction
.Opcode
) {
461 case TGSI_OPCODE_ARL
:
462 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
463 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
466 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
471 case TGSI_OPCODE_MOV
:
472 case TGSI_OPCODE_SWZ
:
473 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
474 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
475 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
479 case TGSI_OPCODE_LIT
:
480 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
481 emit_store( bld
, inst
, 0, CHAN_X
, bld
->base
.one
);
483 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
484 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
485 dst0
= lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
486 emit_store( bld
, inst
, 0, CHAN_Y
, dst0
);
488 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
489 /* XMM[1] = SrcReg[0].yyyy */
490 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
491 /* XMM[1] = max(XMM[1], 0) */
492 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
493 /* XMM[2] = SrcReg[0].wwww */
494 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
495 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
496 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
497 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
498 dst0
= lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
499 emit_store( bld
, inst
, 0, CHAN_Z
, dst0
);
501 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
502 emit_store( bld
, inst
, 0, CHAN_W
, bld
->base
.one
);
506 case TGSI_OPCODE_RCP
:
507 /* TGSI_OPCODE_RECIP */
508 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
509 dst0
= lp_build_rcp(&bld
->base
, src0
);
510 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
511 emit_store( bld
, inst
, 0, chan_index
, dst0
);
515 case TGSI_OPCODE_RSQ
:
516 /* TGSI_OPCODE_RECIPSQRT */
517 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
518 src0
= lp_build_abs(&bld
->base
, src0
);
519 dst0
= lp_build_rsqrt(&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_EXP
:
526 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
527 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
528 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
529 LLVMValueRef
*p_exp2_int_part
= NULL
;
530 LLVMValueRef
*p_frac_part
= NULL
;
531 LLVMValueRef
*p_exp2
= NULL
;
533 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
535 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
536 p_exp2_int_part
= &tmp0
;
537 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
539 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
542 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
544 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
545 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
546 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
547 emit_store( bld
, inst
, 0, CHAN_Y
, tmp1
);
548 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
549 emit_store( bld
, inst
, 0, CHAN_Z
, tmp2
);
552 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
553 tmp0
= bld
->base
.one
;
554 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
558 case TGSI_OPCODE_LOG
:
559 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
560 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
561 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
562 LLVMValueRef
*p_floor_log2
;
564 LLVMValueRef
*p_log2
;
566 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
567 src0
= lp_build_abs( &bld
->base
, src0
);
569 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
570 p_floor_log2
= &tmp0
;
571 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
573 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
576 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
578 /* dst.x = floor(lg2(abs(src.x))) */
579 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
580 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
581 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
582 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
583 tmp1
= lp_build_div( &bld
->base
, src0
, tmp1
);
584 emit_store( bld
, inst
, 0, CHAN_Y
, tmp1
);
586 /* dst.z = lg2(abs(src.x)) */
587 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
588 emit_store( bld
, inst
, 0, CHAN_Z
, tmp2
);
591 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
592 tmp0
= bld
->base
.one
;
593 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
597 case TGSI_OPCODE_MUL
:
598 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
599 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
600 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
601 dst0
= lp_build_mul(&bld
->base
, src0
, src1
);
602 emit_store( bld
, inst
, 0, chan_index
, dst0
);
606 case TGSI_OPCODE_ADD
:
607 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
608 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
609 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
610 dst0
= lp_build_add(&bld
->base
, src0
, src1
);
611 emit_store( bld
, inst
, 0, chan_index
, dst0
);
615 case TGSI_OPCODE_DP3
:
616 /* TGSI_OPCODE_DOT3 */
617 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
618 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
619 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
620 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
621 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
622 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
623 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
624 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
625 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
626 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
627 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
628 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
629 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
633 case TGSI_OPCODE_DP4
:
634 /* TGSI_OPCODE_DOT4 */
635 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
636 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
637 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
638 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
639 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
640 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
641 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
642 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
643 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
644 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
645 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
646 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
647 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
648 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
649 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
650 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
651 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
655 case TGSI_OPCODE_DST
:
656 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
657 tmp0
= bld
->base
.one
;
658 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
660 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
661 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
662 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
663 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
664 emit_store( bld
, inst
, 0, CHAN_Y
, tmp0
);
666 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
667 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
668 emit_store( bld
, inst
, 0, CHAN_Z
, tmp0
);
670 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
671 tmp0
= emit_fetch( bld
, inst
, 1, CHAN_W
);
672 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
676 case TGSI_OPCODE_MIN
:
677 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
678 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
679 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
680 dst0
= lp_build_min( &bld
->base
, src0
, src1
);
681 emit_store( bld
, inst
, 0, chan_index
, dst0
);
685 case TGSI_OPCODE_MAX
:
686 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
687 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
688 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
689 dst0
= lp_build_max( &bld
->base
, src0
, src1
);
690 emit_store( bld
, inst
, 0, chan_index
, dst0
);
694 case TGSI_OPCODE_SLT
:
695 /* TGSI_OPCODE_SETLT */
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 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
700 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
701 emit_store( bld
, inst
, 0, chan_index
, dst0
);
705 case TGSI_OPCODE_SGE
:
706 /* TGSI_OPCODE_SETGE */
707 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
708 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
709 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
710 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
711 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
712 emit_store( bld
, inst
, 0, chan_index
, dst0
);
716 case TGSI_OPCODE_MAD
:
717 /* TGSI_OPCODE_MADD */
718 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
719 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
720 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
721 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
722 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
723 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
724 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
728 case TGSI_OPCODE_SUB
:
729 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
730 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
731 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
732 tmp0
= lp_build_sub( &bld
->base
, tmp0
, tmp1
);
733 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
737 case TGSI_OPCODE_LRP
:
738 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
739 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
740 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
741 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
742 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
743 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
744 dst0
= lp_build_add( &bld
->base
, tmp0
, src2
);
745 emit_store( bld
, inst
, 0, chan_index
, dst0
);
749 case TGSI_OPCODE_CND
:
753 case TGSI_OPCODE_CND0
:
757 case TGSI_OPCODE_DP2A
:
758 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
759 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
760 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
761 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
762 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
763 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
764 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
765 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
766 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
767 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
768 emit_store( bld
, inst
, 0, chan_index
, tmp0
); /* dest[ch] = xmm0 */
773 case TGSI_OPCODE_FRC
:
774 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
775 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
776 emit_frc( bld
, 0, 0 );
777 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
781 case TGSI_OPCODE_CLAMP
:
785 case TGSI_OPCODE_FLR
:
786 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
787 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
788 emit_flr( bld
, 0, 0 );
789 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
793 case TGSI_OPCODE_ROUND
:
794 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
795 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
796 emit_rnd( bld
, 0, 0 );
797 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
802 case TGSI_OPCODE_EX2
: {
803 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
804 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
805 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
806 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
811 case TGSI_OPCODE_LG2
:
812 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
813 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
814 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
815 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
819 case TGSI_OPCODE_POW
:
820 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
821 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
822 dst0
= lp_build_pow( &bld
->base
, src0
, src1
);
823 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
824 emit_store( bld
, inst
, 0, chan_index
, dst0
);
828 case TGSI_OPCODE_XPD
:
829 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
830 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
831 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
832 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
834 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
835 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
836 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
837 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
839 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
841 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
843 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
844 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
845 emit_store( bld
, inst
, 0, CHAN_X
, tmp2
);
847 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
848 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
849 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
850 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
852 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
853 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
854 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
855 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
856 emit_store( bld
, inst
, 0, CHAN_Y
, tmp3
);
858 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
859 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
860 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
861 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
862 emit_store( bld
, inst
, 0, CHAN_Z
, tmp5
);
864 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
865 tmp0
= bld
->base
.one
;
866 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
870 case TGSI_OPCODE_ABS
:
871 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
872 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
873 tmp0
= lp_build_abs( &bld
->base
, tmp0
) ;
874 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
878 case TGSI_OPCODE_RCC
:
882 case TGSI_OPCODE_DPH
:
883 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
884 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
885 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
886 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
887 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
888 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
889 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
890 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
891 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
892 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
893 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
894 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
895 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
896 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
897 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
901 case TGSI_OPCODE_COS
:
902 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
903 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
904 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
905 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
909 case TGSI_OPCODE_DDX
:
913 case TGSI_OPCODE_DDY
:
918 case TGSI_OPCODE_KILP
:
919 /* predicated kill */
921 return 0; /* XXX fix me */
925 case TGSI_OPCODE_KIL
:
926 /* conditional kill */
927 emit_kil( bld
, inst
);
930 case TGSI_OPCODE_PK2H
:
934 case TGSI_OPCODE_PK2US
:
938 case TGSI_OPCODE_PK4B
:
942 case TGSI_OPCODE_PK4UB
:
946 case TGSI_OPCODE_RFL
:
950 case TGSI_OPCODE_SEQ
:
951 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
952 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
953 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
954 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
955 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
956 emit_store( bld
, inst
, 0, chan_index
, dst0
);
960 case TGSI_OPCODE_SFL
:
964 case TGSI_OPCODE_SGT
:
965 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
966 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
967 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
968 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
969 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
970 emit_store( bld
, inst
, 0, chan_index
, dst0
);
974 case TGSI_OPCODE_SIN
:
975 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
976 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
977 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
978 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
982 case TGSI_OPCODE_SLE
:
983 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
984 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
985 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
986 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
987 dst0
= lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
988 emit_store( bld
, inst
, 0, chan_index
, dst0
);
992 case TGSI_OPCODE_SNE
:
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_NOTEQUAL
, 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_STR
:
1006 case TGSI_OPCODE_TEX
:
1007 emit_tex( bld
, inst
, FALSE
, FALSE
);
1010 case TGSI_OPCODE_TXD
:
1014 case TGSI_OPCODE_UP2H
:
1018 case TGSI_OPCODE_UP2US
:
1022 case TGSI_OPCODE_UP4B
:
1026 case TGSI_OPCODE_UP4UB
:
1030 case TGSI_OPCODE_X2D
:
1034 case TGSI_OPCODE_ARA
:
1039 case TGSI_OPCODE_ARR
:
1040 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1041 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1042 emit_rnd( bld
, 0, 0 );
1043 emit_f2it( bld
, 0 );
1044 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
1049 case TGSI_OPCODE_BRA
:
1053 case TGSI_OPCODE_CAL
:
1058 case TGSI_OPCODE_RET
:
1063 case TGSI_OPCODE_END
:
1067 case TGSI_OPCODE_SSG
:
1068 /* TGSI_OPCODE_SGN */
1069 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1070 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1071 emit_sgn( bld
, 0, 0 );
1072 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
1077 case TGSI_OPCODE_CMP
:
1078 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1079 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1080 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1081 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1082 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1083 dst0
= lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1084 emit_store( bld
, inst
, 0, chan_index
, dst0
);
1088 case TGSI_OPCODE_SCS
:
1089 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1090 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1091 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1092 emit_store( bld
, inst
, 0, CHAN_X
, tmp0
);
1094 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1095 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1096 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1097 emit_store( bld
, inst
, 0, CHAN_Y
, tmp0
);
1099 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1100 tmp0
= bld
->base
.zero
;
1101 emit_store( bld
, inst
, 0, CHAN_Z
, tmp0
);
1103 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1104 tmp0
= bld
->base
.one
;
1105 emit_store( bld
, inst
, 0, CHAN_W
, tmp0
);
1109 case TGSI_OPCODE_TXB
:
1110 emit_tex( bld
, inst
, TRUE
, FALSE
);
1113 case TGSI_OPCODE_NRM
:
1115 case TGSI_OPCODE_NRM4
:
1116 /* 3 or 4-component normalization */
1118 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1120 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1121 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1122 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1123 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1125 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1128 /* xmm0 = src.x * src.x */
1129 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1130 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1133 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1136 /* xmm0 = xmm0 + src.y * src.y */
1137 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1138 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1141 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1142 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1145 /* xmm0 = xmm0 + src.z * src.z */
1146 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1147 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1150 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1151 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1155 /* xmm0 = xmm0 + src.w * src.w */
1156 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1157 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1160 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1161 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1164 /* xmm1 = 1 / sqrt(xmm0) */
1165 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1167 /* dst.x = xmm1 * src.x */
1168 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1169 tmp4
= lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1170 emit_store(bld
, inst
, 0, CHAN_X
, tmp4
);
1173 /* dst.y = xmm1 * src.y */
1174 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1175 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1176 emit_store(bld
, inst
, 0, CHAN_Y
, tmp5
);
1179 /* dst.z = xmm1 * src.z */
1180 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1181 tmp6
= lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1182 emit_store(bld
, inst
, 0, CHAN_Z
, tmp6
);
1185 /* dst.w = xmm1 * src.w */
1186 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1187 tmp7
= lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1188 emit_store(bld
, inst
, 0, CHAN_W
, tmp7
);
1193 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1194 tmp0
= bld
->base
.one
;
1195 emit_store(bld
, inst
, 0, CHAN_W
, tmp0
);
1200 case TGSI_OPCODE_DIV
:
1204 case TGSI_OPCODE_DP2
:
1205 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1206 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1207 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1208 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1209 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1210 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1211 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1212 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1213 emit_store( bld
, inst
, 0, chan_index
, tmp0
); /* dest[ch] = xmm0 */
1217 case TGSI_OPCODE_TXL
:
1218 emit_tex( bld
, inst
, TRUE
, FALSE
);
1221 case TGSI_OPCODE_TXP
:
1222 emit_tex( bld
, inst
, FALSE
, TRUE
);
1225 case TGSI_OPCODE_BRK
:
1229 case TGSI_OPCODE_IF
:
1233 case TGSI_OPCODE_LOOP
:
1237 case TGSI_OPCODE_REP
:
1241 case TGSI_OPCODE_ELSE
:
1245 case TGSI_OPCODE_ENDIF
:
1249 case TGSI_OPCODE_ENDLOOP
:
1253 case TGSI_OPCODE_ENDREP
:
1257 case TGSI_OPCODE_PUSHA
:
1261 case TGSI_OPCODE_POPA
:
1265 case TGSI_OPCODE_CEIL
:
1269 case TGSI_OPCODE_I2F
:
1273 case TGSI_OPCODE_NOT
:
1278 case TGSI_OPCODE_TRUNC
:
1279 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1280 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1281 emit_f2it( bld
, 0 );
1283 emit_store( bld
, inst
, 0, chan_index
, tmp0
);
1288 case TGSI_OPCODE_SHL
:
1292 case TGSI_OPCODE_SHR
:
1296 case TGSI_OPCODE_AND
:
1300 case TGSI_OPCODE_OR
:
1304 case TGSI_OPCODE_MOD
:
1308 case TGSI_OPCODE_XOR
:
1312 case TGSI_OPCODE_SAD
:
1316 case TGSI_OPCODE_TXF
:
1320 case TGSI_OPCODE_TXQ
:
1324 case TGSI_OPCODE_CONT
:
1328 case TGSI_OPCODE_EMIT
:
1332 case TGSI_OPCODE_ENDPRIM
:
1345 struct lp_build_tgsi_soa_context
*bld
,
1346 struct tgsi_full_declaration
*decl
)
1348 if( decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
1349 LLVMBuilderRef builder
= bld
->base
.builder
;
1350 unsigned first
, last
, mask
;
1351 unsigned attrib
, chan
;
1353 first
= decl
->DeclarationRange
.First
;
1354 last
= decl
->DeclarationRange
.Last
;
1355 mask
= decl
->Declaration
.UsageMask
;
1357 for( attrib
= first
; attrib
<= last
; attrib
++ ) {
1358 for( chan
= 0; chan
< NUM_CHANNELS
; chan
++ ) {
1359 LLVMValueRef input
= bld
->base
.undef
;
1361 if( mask
& (1 << chan
) ) {
1362 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), (1 + attrib
)*NUM_CHANNELS
+ chan
, 0);
1367 switch( decl
->Declaration
.Interpolate
) {
1368 case TGSI_INTERPOLATE_PERSPECTIVE
:
1371 case TGSI_INTERPOLATE_LINEAR
: {
1372 LLVMValueRef dadx_ptr
= LLVMBuildGEP(builder
, bld
->dadx_ptr
, &index
, 1, "");
1373 LLVMValueRef dady_ptr
= LLVMBuildGEP(builder
, bld
->dady_ptr
, &index
, 1, "");
1374 dadx
= LLVMBuildLoad(builder
, dadx_ptr
, "");
1375 dady
= LLVMBuildLoad(builder
, dady_ptr
, "");
1376 dadx
= lp_build_broadcast_scalar(&bld
->base
, dadx
);
1377 dady
= lp_build_broadcast_scalar(&bld
->base
, dady
);
1378 lp_build_name(dadx
, "dadx_%u.%c", attrib
, "xyzw"[chan
]);
1379 lp_build_name(dady
, "dady_%u.%c", attrib
, "xyzw"[chan
]);
1383 case TGSI_INTERPOLATE_CONSTANT
: {
1384 LLVMValueRef a0_ptr
= LLVMBuildGEP(builder
, bld
->a0_ptr
, &index
, 1, "");
1385 a0
= LLVMBuildLoad(builder
, a0_ptr
, "");
1386 a0
= lp_build_broadcast_scalar(&bld
->base
, a0
);
1387 lp_build_name(a0
, "a0_%u.%c", attrib
, "xyzw"[chan
]);
1398 if (decl
->Declaration
.Interpolate
!= TGSI_INTERPOLATE_CONSTANT
) {
1399 input
= lp_build_add(&bld
->base
, input
, lp_build_mul(&bld
->base
, bld
->x
, dadx
));
1400 input
= lp_build_add(&bld
->base
, input
, lp_build_mul(&bld
->base
, bld
->y
, dady
));
1403 if (decl
->Declaration
.Interpolate
== TGSI_INTERPOLATE_PERSPECTIVE
) {
1405 bld
->oow
= lp_build_rcp(&bld
->base
, bld
->w
);
1406 input
= lp_build_mul(&bld
->base
, input
, bld
->oow
);
1409 lp_build_name(input
, "input%u.%c", attrib
, "xyzw"[chan
]);
1412 bld
->inputs
[attrib
][chan
] = input
;
1420 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1421 const struct tgsi_token
*tokens
,
1423 struct lp_build_mask_context
*mask
,
1425 LLVMValueRef a0_ptr
,
1426 LLVMValueRef dadx_ptr
,
1427 LLVMValueRef dady_ptr
,
1428 LLVMValueRef consts_ptr
,
1429 LLVMValueRef (*outputs
)[4],
1430 LLVMValueRef samplers_ptr
)
1432 struct lp_build_tgsi_soa_context bld
;
1433 struct tgsi_parse_context parse
;
1434 uint num_immediates
= 0;
1437 /* Setup build context */
1438 memset(&bld
, 0, sizeof bld
);
1439 lp_build_context_init(&bld
.base
, builder
, type
);
1444 bld
.a0_ptr
= a0_ptr
;
1445 bld
.dadx_ptr
= dadx_ptr
;
1446 bld
.dady_ptr
= dady_ptr
;
1447 bld
.outputs
= outputs
;
1448 bld
.consts_ptr
= consts_ptr
;
1449 bld
.samplers_ptr
= samplers_ptr
;
1451 tgsi_parse_init( &parse
, tokens
);
1453 while( !tgsi_parse_end_of_tokens( &parse
) ) {
1454 tgsi_parse_token( &parse
);
1456 switch( parse
.FullToken
.Token
.Type
) {
1457 case TGSI_TOKEN_TYPE_DECLARATION
:
1458 if (parse
.FullHeader
.Processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
) {
1459 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
1463 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1464 if (!emit_instruction( &bld
, &parse
.FullToken
.FullInstruction
)) {
1465 unsigned opcode
= parse
.FullToken
.FullInstruction
.Instruction
.Opcode
;
1466 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info(opcode
);
1467 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1468 info
? info
->mnemonic
: "<invalid>");
1472 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1473 /* simply copy the immediate values into the next immediates[] slot */
1475 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1477 assert(num_immediates
< LP_MAX_IMMEDIATES
);
1478 for( i
= 0; i
< size
; ++i
)
1479 bld
.immediates
[num_immediates
][i
] =
1480 lp_build_const_uni(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
1481 for( i
= size
; i
< 4; ++i
)
1482 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
1492 tgsi_parse_free( &parse
);