1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * TGSI to LLVM IR translation -- AoS.
33 * - No control flow support: the existing control flow code should be factored
34 * out into from the SoA code into a common module and shared.
35 * - No derivatives. Derivate logic should be pluggable, just like the samplers.
37 * @author Jose Fonseca <jfonseca@vmware.com>
40 #include "pipe/p_config.h"
41 #include "pipe/p_shader_tokens.h"
42 #include "util/u_debug.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
45 #include "tgsi/tgsi_dump.h"
46 #include "tgsi/tgsi_info.h"
47 #include "tgsi/tgsi_parse.h"
48 #include "tgsi/tgsi_util.h"
49 #include "tgsi/tgsi_scan.h"
50 #include "lp_bld_type.h"
51 #include "lp_bld_const.h"
52 #include "lp_bld_arit.h"
53 #include "lp_bld_logic.h"
54 #include "lp_bld_swizzle.h"
55 #include "lp_bld_flow.h"
56 #include "lp_bld_quad.h"
57 #include "lp_bld_tgsi.h"
58 #include "lp_bld_limits.h"
59 #include "lp_bld_debug.h"
62 #define LP_MAX_INSTRUCTIONS 256
65 struct lp_build_tgsi_aos_context
67 struct lp_build_context base
;
69 /* Builder for integer masks and indices */
70 struct lp_build_context int_bld
;
74 * - swizzles[0] = red index
75 * - swizzles[1] = green index
76 * - swizzles[2] = blue index
77 * - swizzles[3] = alpha index
79 unsigned char swizzles
[4];
80 unsigned char inv_swizzles
[4];
82 LLVMValueRef consts_ptr
;
83 const LLVMValueRef
*inputs
;
84 LLVMValueRef
*outputs
;
86 struct lp_build_sampler_aos
*sampler
;
88 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
89 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
90 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
91 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
93 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
94 * set in the indirect_files field.
95 * The temps[] array above is unused then.
97 LLVMValueRef temps_array
;
99 /** bitmask indicating which register files are accessed indirectly */
100 unsigned indirect_files
;
102 struct tgsi_full_instruction
*instructions
;
103 uint max_instructions
;
108 * Wrapper around lp_build_swizzle_aos which translates swizzles to another
112 swizzle_aos(struct lp_build_tgsi_aos_context
*bld
,
119 unsigned char swizzles
[4];
121 assert(swizzle_x
< 4);
122 assert(swizzle_y
< 4);
123 assert(swizzle_z
< 4);
124 assert(swizzle_w
< 4);
126 swizzles
[bld
->inv_swizzles
[0]] = bld
->swizzles
[swizzle_x
];
127 swizzles
[bld
->inv_swizzles
[1]] = bld
->swizzles
[swizzle_y
];
128 swizzles
[bld
->inv_swizzles
[2]] = bld
->swizzles
[swizzle_z
];
129 swizzles
[bld
->inv_swizzles
[3]] = bld
->swizzles
[swizzle_w
];
131 return lp_build_swizzle_aos(&bld
->base
, a
, swizzles
);
136 swizzle_scalar_aos(struct lp_build_tgsi_aos_context
*bld
,
140 chan
= bld
->swizzles
[chan
];
141 return lp_build_swizzle_scalar_aos(&bld
->base
, a
, chan
);
150 struct lp_build_tgsi_aos_context
*bld
,
151 const struct tgsi_full_instruction
*inst
,
154 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
155 struct lp_type type
= bld
->base
.type
;
156 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
160 assert(!reg
->Register
.Indirect
);
163 * Fetch the from the register file.
166 switch (reg
->Register
.File
) {
167 case TGSI_FILE_CONSTANT
:
169 * Get the constants components
172 res
= bld
->base
.undef
;
173 for (chan
= 0; chan
< 4; ++chan
) {
175 LLVMValueRef scalar_ptr
;
177 LLVMValueRef swizzle
;
179 index
= lp_build_const_int32(bld
->base
.gallivm
, reg
->Register
.Index
* 4 + chan
);
181 scalar_ptr
= LLVMBuildGEP(builder
, bld
->consts_ptr
,
184 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
186 lp_build_name(scalar
, "const[%u].%c", reg
->Register
.Index
, "xyzw"[chan
]);
189 * NOTE: constants array is always assumed to be RGBA
192 swizzle
= lp_build_const_int32(bld
->base
.gallivm
, chan
);
194 res
= LLVMBuildInsertElement(builder
, res
, scalar
, swizzle
, "");
198 * Broadcast the first quaternion to all others.
200 * XXX: could be factored into a reusable function.
203 if (type
.length
> 4) {
204 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
207 for (chan
= 0; chan
< 4; ++chan
) {
208 shuffles
[chan
] = lp_build_const_int32(bld
->base
.gallivm
, chan
);
211 for (i
= 4; i
< type
.length
; ++i
) {
212 shuffles
[i
] = shuffles
[i
% 4];
215 res
= LLVMBuildShuffleVector(builder
,
216 res
, bld
->base
.undef
,
217 LLVMConstVector(shuffles
, type
.length
),
222 case TGSI_FILE_IMMEDIATE
:
223 res
= bld
->immediates
[reg
->Register
.Index
];
227 case TGSI_FILE_INPUT
:
228 res
= bld
->inputs
[reg
->Register
.Index
];
232 case TGSI_FILE_TEMPORARY
:
234 LLVMValueRef temp_ptr
;
235 temp_ptr
= bld
->temps
[reg
->Register
.Index
];
236 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
238 return bld
->base
.undef
;
243 assert(0 && "invalid src register in emit_fetch()");
244 return bld
->base
.undef
;
248 * Apply sign modifier.
251 if (reg
->Register
.Absolute
) {
252 res
= lp_build_abs(&bld
->base
, res
);
255 if(reg
->Register
.Negate
) {
256 res
= lp_build_negate(&bld
->base
, res
);
260 * Swizzle the argument
263 res
= swizzle_aos(bld
, res
,
264 reg
->Register
.SwizzleX
,
265 reg
->Register
.SwizzleY
,
266 reg
->Register
.SwizzleZ
,
267 reg
->Register
.SwizzleW
);
278 struct lp_build_tgsi_aos_context
*bld
,
279 const struct tgsi_full_instruction
*inst
,
283 LLVMBuilderRef builder
= bld
->base
.gallivm
->builder
;
284 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
285 LLVMValueRef mask
= NULL
;
292 switch (inst
->Instruction
.Saturate
) {
296 case TGSI_SAT_ZERO_ONE
:
297 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
298 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
301 case TGSI_SAT_MINUS_PLUS_ONE
:
302 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.gallivm
, bld
->base
.type
, -1.0));
303 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
311 * Translate the register file
314 assert(!reg
->Register
.Indirect
);
316 switch (reg
->Register
.File
) {
317 case TGSI_FILE_OUTPUT
:
318 ptr
= bld
->outputs
[reg
->Register
.Index
];
321 case TGSI_FILE_TEMPORARY
:
322 ptr
= bld
->temps
[reg
->Register
.Index
];
325 case TGSI_FILE_ADDRESS
:
326 ptr
= bld
->addr
[reg
->Indirect
.Index
];
329 case TGSI_FILE_PREDICATE
:
330 ptr
= bld
->preds
[reg
->Register
.Index
];
342 if (inst
->Instruction
.Predicate
) {
345 assert(inst
->Predicate
.Index
< LP_MAX_TGSI_PREDS
);
347 pred
= LLVMBuildLoad(builder
,
348 bld
->preds
[inst
->Predicate
.Index
], "");
351 * Convert the value to an integer mask.
353 pred
= lp_build_compare(bld
->base
.gallivm
,
359 if (inst
->Predicate
.Negate
) {
360 pred
= LLVMBuildNot(builder
, pred
, "");
363 pred
= swizzle_aos(bld
, pred
,
364 inst
->Predicate
.SwizzleX
,
365 inst
->Predicate
.SwizzleY
,
366 inst
->Predicate
.SwizzleZ
,
367 inst
->Predicate
.SwizzleW
);
370 mask
= LLVMBuildAnd(builder
, mask
, pred
, "");
380 if (reg
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
) {
381 LLVMValueRef writemask
;
383 writemask
= lp_build_const_mask_aos(bld
->base
.gallivm
, bld
->base
.type
,
384 reg
->Register
.WriteMask
);
387 mask
= LLVMBuildAnd(builder
, mask
, writemask
, "");
394 LLVMValueRef orig_value
;
396 orig_value
= LLVMBuildLoad(builder
, ptr
, "");
397 value
= lp_build_select(&bld
->base
,
398 mask
, value
, orig_value
);
401 LLVMBuildStore(builder
, value
, ptr
);
406 * High-level instruction translators.
410 emit_tex(struct lp_build_tgsi_aos_context
*bld
,
411 const struct tgsi_full_instruction
*inst
,
412 enum lp_build_tex_modifier modifier
)
421 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
422 return bld
->base
.undef
;
425 target
= inst
->Texture
.Texture
;
427 coords
= emit_fetch( bld
, inst
, 0 );
429 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
430 ddx
= emit_fetch( bld
, inst
, 1 );
431 ddy
= emit_fetch( bld
, inst
, 2 );
432 unit
= inst
->Src
[3].Register
.Index
;
435 ddx
= lp_build_ddx( &bld
->base
, coords
);
436 ddy
= lp_build_ddy( &bld
->base
, coords
);
442 unit
= inst
->Src
[1].Register
.Index
;
445 return bld
->sampler
->emit_fetch_texel(bld
->sampler
,
455 struct lp_build_tgsi_aos_context
*bld
,
456 const struct tgsi_full_declaration
*decl
)
458 struct gallivm_state
*gallivm
= bld
->base
.gallivm
;
459 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.gallivm
, bld
->base
.type
);
461 unsigned first
= decl
->Range
.First
;
462 unsigned last
= decl
->Range
.Last
;
465 for (idx
= first
; idx
<= last
; ++idx
) {
466 switch (decl
->Declaration
.File
) {
467 case TGSI_FILE_TEMPORARY
:
468 assert(idx
< LP_MAX_TGSI_TEMPS
);
469 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
470 LLVMValueRef array_size
= lp_build_const_int32(gallivm
, last
+ 1);
471 bld
->temps_array
= lp_build_array_alloca(bld
->base
.gallivm
,
472 vec_type
, array_size
, "");
474 bld
->temps
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
478 case TGSI_FILE_OUTPUT
:
479 bld
->outputs
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
482 case TGSI_FILE_ADDRESS
:
483 assert(idx
< LP_MAX_TGSI_ADDRS
);
484 bld
->addr
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
487 case TGSI_FILE_PREDICATE
:
488 assert(idx
< LP_MAX_TGSI_PREDS
);
489 bld
->preds
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
493 /* don't need to declare other vars */
501 * Emit LLVM for one TGSI instruction.
502 * \param return TRUE for success, FALSE otherwise
506 struct lp_build_tgsi_aos_context
*bld
,
507 const struct tgsi_full_instruction
*inst
,
508 const struct tgsi_opcode_info
*info
,
511 LLVMValueRef src0
, src1
, src2
;
512 LLVMValueRef tmp0
, tmp1
;
513 LLVMValueRef dst0
= NULL
;
516 * Stores and write masks are handled in a general fashion after the long
517 * instruction opcode switch statement.
519 * Although not stricitly necessary, we avoid generating instructions for
520 * channels which won't be stored, in cases where's that easy. For some
521 * complex instructions, like texture sampling, it is more convenient to
522 * assume a full writemask and then let LLVM optimization passes eliminate
528 assert(info
->num_dst
<= 1);
530 dst0
= bld
->base
.undef
;
533 switch (inst
->Instruction
.Opcode
) {
534 case TGSI_OPCODE_ARL
:
535 src0
= emit_fetch(bld
, inst
, 0);
536 dst0
= lp_build_floor(&bld
->base
, src0
);
539 case TGSI_OPCODE_MOV
:
540 dst0
= emit_fetch(bld
, inst
, 0);
543 case TGSI_OPCODE_LIT
:
546 case TGSI_OPCODE_RCP
:
547 /* TGSI_OPCODE_RECIP */
548 src0
= emit_fetch(bld
, inst
, 0);
549 dst0
= lp_build_rcp(&bld
->base
, src0
);
552 case TGSI_OPCODE_RSQ
:
553 /* TGSI_OPCODE_RECIPSQRT */
554 src0
= emit_fetch(bld
, inst
, 0);
555 tmp0
= lp_build_abs(&bld
->base
, src0
);
556 dst0
= lp_build_rsqrt(&bld
->base
, tmp0
);
559 case TGSI_OPCODE_EXP
:
562 case TGSI_OPCODE_LOG
:
565 case TGSI_OPCODE_MUL
:
566 src0
= emit_fetch(bld
, inst
, 0);
567 src1
= emit_fetch(bld
, inst
, 1);
568 dst0
= lp_build_mul(&bld
->base
, src0
, src1
);
571 case TGSI_OPCODE_ADD
:
572 src0
= emit_fetch(bld
, inst
, 0);
573 src1
= emit_fetch(bld
, inst
, 1);
574 dst0
= lp_build_add(&bld
->base
, src0
, src1
);
577 case TGSI_OPCODE_DP3
:
578 /* TGSI_OPCODE_DOT3 */
581 case TGSI_OPCODE_DP4
:
582 /* TGSI_OPCODE_DOT4 */
585 case TGSI_OPCODE_DST
:
588 case TGSI_OPCODE_MIN
:
589 src0
= emit_fetch(bld
, inst
, 0);
590 src1
= emit_fetch(bld
, inst
, 1);
591 dst0
= lp_build_max(&bld
->base
, src0
, src1
);
594 case TGSI_OPCODE_MAX
:
595 src0
= emit_fetch(bld
, inst
, 0);
596 src1
= emit_fetch(bld
, inst
, 1);
597 dst0
= lp_build_max(&bld
->base
, src0
, src1
);
600 case TGSI_OPCODE_SLT
:
601 /* TGSI_OPCODE_SETLT */
602 src0
= emit_fetch(bld
, inst
, 0);
603 src1
= emit_fetch(bld
, inst
, 1);
604 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
605 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
608 case TGSI_OPCODE_SGE
:
609 /* TGSI_OPCODE_SETGE */
610 src0
= emit_fetch(bld
, inst
, 0);
611 src1
= emit_fetch(bld
, inst
, 1);
612 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
613 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
616 case TGSI_OPCODE_MAD
:
617 /* TGSI_OPCODE_MADD */
618 src0
= emit_fetch(bld
, inst
, 0);
619 src1
= emit_fetch(bld
, inst
, 1);
620 src2
= emit_fetch(bld
, inst
, 2);
621 tmp0
= lp_build_mul(&bld
->base
, src0
, src1
);
622 dst0
= lp_build_add(&bld
->base
, tmp0
, src2
);
625 case TGSI_OPCODE_SUB
:
626 src0
= emit_fetch(bld
, inst
, 0);
627 src1
= emit_fetch(bld
, inst
, 1);
628 dst0
= lp_build_sub(&bld
->base
, src0
, src1
);
631 case TGSI_OPCODE_LRP
:
632 src0
= emit_fetch(bld
, inst
, 0);
633 src1
= emit_fetch(bld
, inst
, 1);
634 src2
= emit_fetch(bld
, inst
, 2);
635 tmp0
= lp_build_sub(&bld
->base
, src1
, src2
);
636 tmp0
= lp_build_mul(&bld
->base
, src0
, tmp0
);
637 dst0
= lp_build_add(&bld
->base
, tmp0
, src2
);
640 case TGSI_OPCODE_CND
:
641 src0
= emit_fetch(bld
, inst
, 0);
642 src1
= emit_fetch(bld
, inst
, 1);
643 src2
= emit_fetch(bld
, inst
, 2);
644 tmp1
= lp_build_const_vec(bld
->base
.gallivm
, bld
->base
.type
, 0.5);
645 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
646 dst0
= lp_build_select(&bld
->base
, tmp0
, src0
, src1
);
649 case TGSI_OPCODE_DP2A
:
652 case TGSI_OPCODE_FRC
:
653 src0
= emit_fetch(bld
, inst
, 0);
654 tmp0
= lp_build_floor(&bld
->base
, src0
);
655 dst0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
658 case TGSI_OPCODE_CLAMP
:
659 src0
= emit_fetch(bld
, inst
, 0);
660 src1
= emit_fetch(bld
, inst
, 1);
661 src2
= emit_fetch(bld
, inst
, 2);
662 tmp0
= lp_build_max(&bld
->base
, src0
, src1
);
663 dst0
= lp_build_min(&bld
->base
, tmp0
, src2
);
666 case TGSI_OPCODE_FLR
:
667 src0
= emit_fetch(bld
, inst
, 0);
668 dst0
= lp_build_floor(&bld
->base
, src0
);
671 case TGSI_OPCODE_ROUND
:
672 src0
= emit_fetch(bld
, inst
, 0);
673 dst0
= lp_build_round(&bld
->base
, src0
);
676 case TGSI_OPCODE_EX2
:
677 src0
= emit_fetch(bld
, inst
, 0);
678 tmp0
= lp_build_swizzle_scalar_aos(&bld
->base
, src0
, TGSI_SWIZZLE_X
);
679 dst0
= lp_build_exp2(&bld
->base
, tmp0
);
682 case TGSI_OPCODE_LG2
:
683 src0
= emit_fetch(bld
, inst
, 0);
684 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
685 dst0
= lp_build_log2(&bld
->base
, tmp0
);
688 case TGSI_OPCODE_POW
:
689 src0
= emit_fetch(bld
, inst
, 0);
690 src0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
691 src1
= emit_fetch(bld
, inst
, 1);
692 src1
= swizzle_scalar_aos(bld
, src1
, TGSI_SWIZZLE_X
);
693 dst0
= lp_build_pow(&bld
->base
, src0
, src1
);
696 case TGSI_OPCODE_XPD
:
699 case TGSI_OPCODE_ABS
:
700 src0
= emit_fetch(bld
, inst
, 0);
701 dst0
= lp_build_abs(&bld
->base
, src0
);
704 case TGSI_OPCODE_RCC
:
709 case TGSI_OPCODE_DPH
:
712 case TGSI_OPCODE_COS
:
713 src0
= emit_fetch(bld
, inst
, 0);
714 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
715 dst0
= lp_build_cos(&bld
->base
, tmp0
);
718 case TGSI_OPCODE_DDX
:
721 case TGSI_OPCODE_DDY
:
724 case TGSI_OPCODE_KILP
:
725 /* predicated kill */
728 case TGSI_OPCODE_KIL
:
729 /* conditional kill */
732 case TGSI_OPCODE_PK2H
:
736 case TGSI_OPCODE_PK2US
:
740 case TGSI_OPCODE_PK4B
:
744 case TGSI_OPCODE_PK4UB
:
747 case TGSI_OPCODE_RFL
:
750 case TGSI_OPCODE_SEQ
:
751 src0
= emit_fetch(bld
, inst
, 0);
752 src1
= emit_fetch(bld
, inst
, 1);
753 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
754 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
757 case TGSI_OPCODE_SFL
:
758 dst0
= bld
->base
.zero
;
761 case TGSI_OPCODE_SGT
:
762 src0
= emit_fetch(bld
, inst
, 0);
763 src1
= emit_fetch(bld
, inst
, 1);
764 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
765 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
768 case TGSI_OPCODE_SIN
:
769 src0
= emit_fetch(bld
, inst
, 0);
770 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
771 dst0
= lp_build_sin(&bld
->base
, tmp0
);
774 case TGSI_OPCODE_SLE
:
775 src0
= emit_fetch(bld
, inst
, 0);
776 src1
= emit_fetch(bld
, inst
, 1);
777 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
778 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
781 case TGSI_OPCODE_SNE
:
782 src0
= emit_fetch(bld
, inst
, 0);
783 src1
= emit_fetch(bld
, inst
, 1);
784 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
785 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
788 case TGSI_OPCODE_STR
:
789 dst0
= bld
->base
.one
;
792 case TGSI_OPCODE_TEX
:
793 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_NONE
);
796 case TGSI_OPCODE_TXD
:
797 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
);
800 case TGSI_OPCODE_UP2H
:
806 case TGSI_OPCODE_UP2US
:
812 case TGSI_OPCODE_UP4B
:
818 case TGSI_OPCODE_UP4UB
:
824 case TGSI_OPCODE_X2D
:
830 case TGSI_OPCODE_ARA
:
836 case TGSI_OPCODE_ARR
:
837 src0
= emit_fetch(bld
, inst
, 0);
838 dst0
= lp_build_round(&bld
->base
, src0
);
841 case TGSI_OPCODE_BRA
:
847 case TGSI_OPCODE_CAL
:
850 case TGSI_OPCODE_RET
:
853 case TGSI_OPCODE_END
:
857 case TGSI_OPCODE_SSG
:
858 /* TGSI_OPCODE_SGN */
859 tmp0
= emit_fetch(bld
, inst
, 0);
860 dst0
= lp_build_sgn(&bld
->base
, tmp0
);
863 case TGSI_OPCODE_CMP
:
864 src0
= emit_fetch(bld
, inst
, 0);
865 src1
= emit_fetch(bld
, inst
, 1);
866 src2
= emit_fetch(bld
, inst
, 2);
867 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
868 dst0
= lp_build_select(&bld
->base
, tmp0
, src1
, src2
);
871 case TGSI_OPCODE_SCS
:
874 case TGSI_OPCODE_TXB
:
875 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
);
878 case TGSI_OPCODE_NRM
:
880 case TGSI_OPCODE_NRM4
:
883 case TGSI_OPCODE_DIV
:
889 case TGSI_OPCODE_DP2
:
892 case TGSI_OPCODE_TXL
:
893 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
);
896 case TGSI_OPCODE_TXP
:
897 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_PROJECTED
);
900 case TGSI_OPCODE_BRK
:
906 case TGSI_OPCODE_BGNLOOP
:
909 case TGSI_OPCODE_BGNSUB
:
912 case TGSI_OPCODE_ELSE
:
915 case TGSI_OPCODE_ENDIF
:
918 case TGSI_OPCODE_ENDLOOP
:
921 case TGSI_OPCODE_ENDSUB
:
924 case TGSI_OPCODE_PUSHA
:
930 case TGSI_OPCODE_POPA
:
936 case TGSI_OPCODE_CEIL
:
937 src0
= emit_fetch(bld
, inst
, 0);
938 dst0
= lp_build_ceil(&bld
->base
, src0
);
941 case TGSI_OPCODE_I2F
:
947 case TGSI_OPCODE_NOT
:
953 case TGSI_OPCODE_TRUNC
:
954 src0
= emit_fetch(bld
, inst
, 0);
955 dst0
= lp_build_trunc(&bld
->base
, src0
);
958 case TGSI_OPCODE_SHL
:
964 case TGSI_OPCODE_ISHR
:
970 case TGSI_OPCODE_AND
:
982 case TGSI_OPCODE_MOD
:
988 case TGSI_OPCODE_XOR
:
994 case TGSI_OPCODE_SAD
:
1000 case TGSI_OPCODE_TXF
:
1006 case TGSI_OPCODE_TXQ
:
1012 case TGSI_OPCODE_CONT
:
1015 case TGSI_OPCODE_EMIT
:
1019 case TGSI_OPCODE_ENDPRIM
:
1023 case TGSI_OPCODE_NOP
:
1030 if (info
->num_dst
) {
1031 emit_store(bld
, inst
, 0, dst0
);
1039 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
1040 const struct tgsi_token
*tokens
,
1041 struct lp_type type
,
1042 const unsigned char swizzles
[4],
1043 LLVMValueRef consts_ptr
,
1044 const LLVMValueRef
*inputs
,
1045 LLVMValueRef
*outputs
,
1046 struct lp_build_sampler_aos
*sampler
,
1047 const struct tgsi_shader_info
*info
)
1049 struct lp_build_tgsi_aos_context bld
;
1050 struct tgsi_parse_context parse
;
1051 uint num_immediates
= 0;
1052 uint num_instructions
= 0;
1056 /* Setup build context */
1057 memset(&bld
, 0, sizeof bld
);
1058 lp_build_context_init(&bld
.base
, gallivm
, type
);
1059 lp_build_context_init(&bld
.int_bld
, gallivm
, lp_int_type(type
));
1061 for (chan
= 0; chan
< 4; ++chan
) {
1062 bld
.swizzles
[chan
] = swizzles
[chan
];
1063 bld
.inv_swizzles
[swizzles
[chan
]] = chan
;
1066 bld
.inputs
= inputs
;
1067 bld
.outputs
= outputs
;
1068 bld
.consts_ptr
= consts_ptr
;
1069 bld
.sampler
= sampler
;
1070 bld
.indirect_files
= info
->indirect_files
;
1071 bld
.instructions
= (struct tgsi_full_instruction
*)
1072 MALLOC(LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
));
1073 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
1075 if (!bld
.instructions
) {
1079 tgsi_parse_init(&parse
, tokens
);
1081 while (!tgsi_parse_end_of_tokens(&parse
)) {
1082 tgsi_parse_token(&parse
);
1084 switch(parse
.FullToken
.Token
.Type
) {
1085 case TGSI_TOKEN_TYPE_DECLARATION
:
1086 /* Inputs already interpolated */
1087 emit_declaration(&bld
, &parse
.FullToken
.FullDeclaration
);
1090 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1092 /* save expanded instruction */
1093 if (num_instructions
== bld
.max_instructions
) {
1094 struct tgsi_full_instruction
*instructions
;
1095 instructions
= REALLOC(bld
.instructions
,
1096 bld
.max_instructions
1097 * sizeof(struct tgsi_full_instruction
),
1098 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
1099 * sizeof(struct tgsi_full_instruction
));
1100 if (!instructions
) {
1103 bld
.instructions
= instructions
;
1104 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
1107 memcpy(bld
.instructions
+ num_instructions
,
1108 &parse
.FullToken
.FullInstruction
,
1109 sizeof(bld
.instructions
[0]));
1116 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1117 /* simply copy the immediate values into the next immediates[] slot */
1119 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1122 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1123 for (chan
= 0; chan
< 4; ++chan
) {
1126 for (chan
= 0; chan
< size
; ++chan
) {
1127 unsigned swizzle
= bld
.swizzles
[chan
];
1128 imm
[swizzle
] = parse
.FullToken
.FullImmediate
.u
[chan
].Float
;
1130 bld
.immediates
[num_immediates
] =
1131 lp_build_const_aos(gallivm
, type
,
1132 imm
[0], imm
[1], imm
[2], imm
[3],
1138 case TGSI_TOKEN_TYPE_PROPERTY
:
1147 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
1148 const struct tgsi_opcode_info
*opcode_info
=
1149 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
1150 if (!emit_instruction(&bld
, instr
, opcode_info
, &pc
))
1151 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1152 opcode_info
->mnemonic
);
1156 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
1157 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
1158 debug_printf("11111111111111111111111111111 \n");
1159 tgsi_dump(tokens
, 0);
1160 lp_debug_dump_value(function
);
1161 debug_printf("2222222222222222222222222222 \n");
1163 tgsi_parse_free(&parse
);
1166 LLVMModuleRef module
= LLVMGetGlobalParent(
1167 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
1168 LLVMDumpModule(module
);
1171 FREE(bld
.instructions
);