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 struct lp_type type
= bld
->base
.type
;
155 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
159 assert(!reg
->Register
.Indirect
);
162 * Fetch the from the register file.
165 switch (reg
->Register
.File
) {
166 case TGSI_FILE_CONSTANT
:
168 * Get the constants components
171 res
= bld
->base
.undef
;
172 for (chan
= 0; chan
< 4; ++chan
) {
174 LLVMValueRef scalar_ptr
;
176 LLVMValueRef swizzle
;
178 index
= LLVMConstInt(LLVMInt32Type(),
179 reg
->Register
.Index
*4 + chan
,
182 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
185 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
187 lp_build_name(scalar
, "const[%u].%c", reg
->Register
.Index
, "xyzw"[chan
]);
190 * NOTE: constants array is always assumed to be RGBA
193 swizzle
= LLVMConstInt(LLVMInt32Type(), chan
, 0);
195 res
= LLVMBuildInsertElement(bld
->base
.builder
, res
, scalar
, swizzle
, "");
199 * Broadcast the first quaternion to all others.
201 * XXX: could be factored into a reusable function.
204 if (type
.length
> 4) {
205 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
208 for (chan
= 0; chan
< 4; ++chan
) {
209 shuffles
[chan
] = LLVMConstInt(LLVMInt32Type(), chan
, 0);
212 for (i
= 4; i
< type
.length
; ++i
) {
213 shuffles
[i
] = shuffles
[i
% 4];
216 res
= LLVMBuildShuffleVector(bld
->base
.builder
,
217 res
, bld
->base
.undef
,
218 LLVMConstVector(shuffles
, type
.length
),
223 case TGSI_FILE_IMMEDIATE
:
224 res
= bld
->immediates
[reg
->Register
.Index
];
228 case TGSI_FILE_INPUT
:
229 res
= bld
->inputs
[reg
->Register
.Index
];
233 case TGSI_FILE_TEMPORARY
:
235 LLVMValueRef temp_ptr
;
236 temp_ptr
= bld
->temps
[reg
->Register
.Index
];
237 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
239 return bld
->base
.undef
;
244 assert(0 && "invalid src register in emit_fetch()");
245 return bld
->base
.undef
;
249 * Apply sign modifier.
252 if (reg
->Register
.Absolute
) {
253 res
= lp_build_abs(&bld
->base
, res
);
256 if(reg
->Register
.Negate
) {
257 res
= lp_build_negate(&bld
->base
, res
);
261 * Swizzle the argument
264 res
= swizzle_aos(bld
, res
,
265 reg
->Register
.SwizzleX
,
266 reg
->Register
.SwizzleY
,
267 reg
->Register
.SwizzleZ
,
268 reg
->Register
.SwizzleW
);
279 struct lp_build_tgsi_aos_context
*bld
,
280 const struct tgsi_full_instruction
*inst
,
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
.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(bld
->base
.builder
,
348 bld
->preds
[inst
->Predicate
.Index
], "");
351 * Convert the value to an integer mask.
353 pred
= lp_build_compare(bld
->base
.builder
,
359 if (inst
->Predicate
.Negate
) {
360 pred
= LLVMBuildNot(bld
->base
.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(bld
->base
.builder
, mask
, pred
, "");
380 if (reg
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
) {
381 LLVMValueRef writemask
;
383 writemask
= lp_build_const_mask_aos(bld
->base
.type
, reg
->Register
.WriteMask
);
386 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, writemask
, "");
393 LLVMValueRef orig_value
;
395 orig_value
= LLVMBuildLoad(bld
->base
.builder
, ptr
, "");
396 value
= lp_build_select(&bld
->base
,
397 mask
, value
, orig_value
);
400 LLVMBuildStore(bld
->base
.builder
, value
, ptr
);
405 * High-level instruction translators.
409 emit_tex(struct lp_build_tgsi_aos_context
*bld
,
410 const struct tgsi_full_instruction
*inst
,
411 enum lp_build_tex_modifier modifier
)
420 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
421 return bld
->base
.undef
;
424 target
= inst
->Texture
.Texture
;
426 coords
= emit_fetch( bld
, inst
, 0 );
428 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
429 ddx
= emit_fetch( bld
, inst
, 1 );
430 ddy
= emit_fetch( bld
, inst
, 2 );
431 unit
= inst
->Src
[3].Register
.Index
;
434 ddx
= lp_build_ddx( &bld
->base
, coords
);
435 ddy
= lp_build_ddy( &bld
->base
, coords
);
441 unit
= inst
->Src
[1].Register
.Index
;
444 return bld
->sampler
->emit_fetch_texel(bld
->sampler
,
454 struct lp_build_tgsi_aos_context
*bld
,
455 const struct tgsi_full_declaration
*decl
)
457 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
459 unsigned first
= decl
->Range
.First
;
460 unsigned last
= decl
->Range
.Last
;
463 for (idx
= first
; idx
<= last
; ++idx
) {
464 switch (decl
->Declaration
.File
) {
465 case TGSI_FILE_TEMPORARY
:
466 assert(idx
< LP_MAX_TGSI_TEMPS
);
467 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
468 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
470 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
471 vec_type
, array_size
, "");
473 bld
->temps
[idx
] = lp_build_alloca(bld
->base
.builder
,
478 case TGSI_FILE_OUTPUT
:
479 bld
->outputs
[idx
] = lp_build_alloca(bld
->base
.builder
,
483 case TGSI_FILE_ADDRESS
:
484 assert(idx
< LP_MAX_TGSI_ADDRS
);
485 bld
->addr
[idx
] = lp_build_alloca(bld
->base
.builder
,
489 case TGSI_FILE_PREDICATE
:
490 assert(idx
< LP_MAX_TGSI_PREDS
);
491 bld
->preds
[idx
] = lp_build_alloca(bld
->base
.builder
,
496 /* don't need to declare other vars */
504 * Emit LLVM for one TGSI instruction.
505 * \param return TRUE for success, FALSE otherwise
509 struct lp_build_tgsi_aos_context
*bld
,
510 const struct tgsi_full_instruction
*inst
,
511 const struct tgsi_opcode_info
*info
,
514 LLVMValueRef src0
, src1
, src2
;
515 LLVMValueRef tmp0
, tmp1
;
516 LLVMValueRef dst0
= NULL
;
519 * Stores and write masks are handled in a general fashion after the long
520 * instruction opcode switch statement.
522 * Although not stricitly necessary, we avoid generating instructions for
523 * channels which won't be stored, in cases where's that easy. For some
524 * complex instructions, like texture sampling, it is more convenient to
525 * assume a full writemask and then let LLVM optimization passes eliminate
531 assert(info
->num_dst
<= 1);
533 dst0
= bld
->base
.undef
;
536 switch (inst
->Instruction
.Opcode
) {
537 case TGSI_OPCODE_ARL
:
538 src0
= emit_fetch(bld
, inst
, 0);
539 dst0
= lp_build_floor(&bld
->base
, src0
);
542 case TGSI_OPCODE_MOV
:
543 dst0
= emit_fetch(bld
, inst
, 0);
546 case TGSI_OPCODE_LIT
:
549 case TGSI_OPCODE_RCP
:
550 /* TGSI_OPCODE_RECIP */
551 src0
= emit_fetch(bld
, inst
, 0);
552 dst0
= lp_build_rcp(&bld
->base
, src0
);
555 case TGSI_OPCODE_RSQ
:
556 /* TGSI_OPCODE_RECIPSQRT */
557 src0
= emit_fetch(bld
, inst
, 0);
558 tmp0
= lp_build_abs(&bld
->base
, src0
);
559 dst0
= lp_build_rsqrt(&bld
->base
, tmp0
);
562 case TGSI_OPCODE_EXP
:
565 case TGSI_OPCODE_LOG
:
568 case TGSI_OPCODE_MUL
:
569 src0
= emit_fetch(bld
, inst
, 0);
570 src1
= emit_fetch(bld
, inst
, 1);
571 dst0
= lp_build_mul(&bld
->base
, src0
, src1
);
574 case TGSI_OPCODE_ADD
:
575 src0
= emit_fetch(bld
, inst
, 0);
576 src1
= emit_fetch(bld
, inst
, 1);
577 dst0
= lp_build_add(&bld
->base
, src0
, src1
);
580 case TGSI_OPCODE_DP3
:
581 /* TGSI_OPCODE_DOT3 */
584 case TGSI_OPCODE_DP4
:
585 /* TGSI_OPCODE_DOT4 */
588 case TGSI_OPCODE_DST
:
591 case TGSI_OPCODE_MIN
:
592 src0
= emit_fetch(bld
, inst
, 0);
593 src1
= emit_fetch(bld
, inst
, 1);
594 dst0
= lp_build_max(&bld
->base
, src0
, src1
);
597 case TGSI_OPCODE_MAX
:
598 src0
= emit_fetch(bld
, inst
, 0);
599 src1
= emit_fetch(bld
, inst
, 1);
600 dst0
= lp_build_max(&bld
->base
, src0
, src1
);
603 case TGSI_OPCODE_SLT
:
604 /* TGSI_OPCODE_SETLT */
605 src0
= emit_fetch(bld
, inst
, 0);
606 src1
= emit_fetch(bld
, inst
, 1);
607 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
608 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
611 case TGSI_OPCODE_SGE
:
612 /* TGSI_OPCODE_SETGE */
613 src0
= emit_fetch(bld
, inst
, 0);
614 src1
= emit_fetch(bld
, inst
, 1);
615 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
616 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
619 case TGSI_OPCODE_MAD
:
620 /* TGSI_OPCODE_MADD */
621 src0
= emit_fetch(bld
, inst
, 0);
622 src1
= emit_fetch(bld
, inst
, 1);
623 src2
= emit_fetch(bld
, inst
, 2);
624 tmp0
= lp_build_mul(&bld
->base
, src0
, src1
);
625 dst0
= lp_build_add(&bld
->base
, tmp0
, src2
);
628 case TGSI_OPCODE_SUB
:
629 src0
= emit_fetch(bld
, inst
, 0);
630 src1
= emit_fetch(bld
, inst
, 1);
631 dst0
= lp_build_sub(&bld
->base
, src0
, src1
);
634 case TGSI_OPCODE_LRP
:
635 src0
= emit_fetch(bld
, inst
, 0);
636 src1
= emit_fetch(bld
, inst
, 1);
637 src2
= emit_fetch(bld
, inst
, 2);
638 tmp0
= lp_build_sub(&bld
->base
, src1
, src2
);
639 tmp0
= lp_build_mul(&bld
->base
, src0
, tmp0
);
640 dst0
= lp_build_add(&bld
->base
, tmp0
, src2
);
643 case TGSI_OPCODE_CND
:
644 src0
= emit_fetch(bld
, inst
, 0);
645 src1
= emit_fetch(bld
, inst
, 1);
646 src2
= emit_fetch(bld
, inst
, 2);
647 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
648 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
649 dst0
= lp_build_select(&bld
->base
, tmp0
, src0
, src1
);
652 case TGSI_OPCODE_DP2A
:
655 case TGSI_OPCODE_FRC
:
656 src0
= emit_fetch(bld
, inst
, 0);
657 tmp0
= lp_build_floor(&bld
->base
, src0
);
658 dst0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
661 case TGSI_OPCODE_CLAMP
:
662 src0
= emit_fetch(bld
, inst
, 0);
663 src1
= emit_fetch(bld
, inst
, 1);
664 src2
= emit_fetch(bld
, inst
, 2);
665 tmp0
= lp_build_max(&bld
->base
, src0
, src1
);
666 dst0
= lp_build_min(&bld
->base
, tmp0
, src2
);
669 case TGSI_OPCODE_FLR
:
670 src0
= emit_fetch(bld
, inst
, 0);
671 dst0
= lp_build_floor(&bld
->base
, src0
);
674 case TGSI_OPCODE_ROUND
:
675 src0
= emit_fetch(bld
, inst
, 0);
676 dst0
= lp_build_round(&bld
->base
, src0
);
679 case TGSI_OPCODE_EX2
:
680 src0
= emit_fetch(bld
, inst
, 0);
681 tmp0
= lp_build_swizzle_scalar_aos(&bld
->base
, src0
, TGSI_SWIZZLE_X
);
682 dst0
= lp_build_exp2(&bld
->base
, tmp0
);
685 case TGSI_OPCODE_LG2
:
686 src0
= emit_fetch(bld
, inst
, 0);
687 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
688 dst0
= lp_build_log2(&bld
->base
, tmp0
);
691 case TGSI_OPCODE_POW
:
692 src0
= emit_fetch(bld
, inst
, 0);
693 src0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
694 src1
= emit_fetch(bld
, inst
, 1);
695 src1
= swizzle_scalar_aos(bld
, src1
, TGSI_SWIZZLE_X
);
696 dst0
= lp_build_pow(&bld
->base
, src0
, src1
);
699 case TGSI_OPCODE_XPD
:
702 case TGSI_OPCODE_ABS
:
703 src0
= emit_fetch(bld
, inst
, 0);
704 dst0
= lp_build_abs(&bld
->base
, src0
);
707 case TGSI_OPCODE_RCC
:
712 case TGSI_OPCODE_DPH
:
715 case TGSI_OPCODE_COS
:
716 src0
= emit_fetch(bld
, inst
, 0);
717 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
718 dst0
= lp_build_cos(&bld
->base
, tmp0
);
721 case TGSI_OPCODE_DDX
:
724 case TGSI_OPCODE_DDY
:
727 case TGSI_OPCODE_KILP
:
728 /* predicated kill */
731 case TGSI_OPCODE_KIL
:
732 /* conditional kill */
735 case TGSI_OPCODE_PK2H
:
739 case TGSI_OPCODE_PK2US
:
743 case TGSI_OPCODE_PK4B
:
747 case TGSI_OPCODE_PK4UB
:
750 case TGSI_OPCODE_RFL
:
753 case TGSI_OPCODE_SEQ
:
754 src0
= emit_fetch(bld
, inst
, 0);
755 src1
= emit_fetch(bld
, inst
, 1);
756 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
757 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
760 case TGSI_OPCODE_SFL
:
761 dst0
= bld
->base
.zero
;
764 case TGSI_OPCODE_SGT
:
765 src0
= emit_fetch(bld
, inst
, 0);
766 src1
= emit_fetch(bld
, inst
, 1);
767 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
768 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
771 case TGSI_OPCODE_SIN
:
772 src0
= emit_fetch(bld
, inst
, 0);
773 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
774 dst0
= lp_build_sin(&bld
->base
, tmp0
);
777 case TGSI_OPCODE_SLE
:
778 src0
= emit_fetch(bld
, inst
, 0);
779 src1
= emit_fetch(bld
, inst
, 1);
780 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
781 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
784 case TGSI_OPCODE_SNE
:
785 src0
= emit_fetch(bld
, inst
, 0);
786 src1
= emit_fetch(bld
, inst
, 1);
787 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
788 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
791 case TGSI_OPCODE_STR
:
792 dst0
= bld
->base
.one
;
795 case TGSI_OPCODE_TEX
:
796 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_NONE
);
799 case TGSI_OPCODE_TXD
:
800 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
);
803 case TGSI_OPCODE_UP2H
:
809 case TGSI_OPCODE_UP2US
:
815 case TGSI_OPCODE_UP4B
:
821 case TGSI_OPCODE_UP4UB
:
827 case TGSI_OPCODE_X2D
:
833 case TGSI_OPCODE_ARA
:
839 case TGSI_OPCODE_ARR
:
840 src0
= emit_fetch(bld
, inst
, 0);
841 dst0
= lp_build_round(&bld
->base
, src0
);
844 case TGSI_OPCODE_BRA
:
850 case TGSI_OPCODE_CAL
:
853 case TGSI_OPCODE_RET
:
856 case TGSI_OPCODE_END
:
860 case TGSI_OPCODE_SSG
:
861 /* TGSI_OPCODE_SGN */
862 tmp0
= emit_fetch(bld
, inst
, 0);
863 dst0
= lp_build_sgn(&bld
->base
, tmp0
);
866 case TGSI_OPCODE_CMP
:
867 src0
= emit_fetch(bld
, inst
, 0);
868 src1
= emit_fetch(bld
, inst
, 1);
869 src2
= emit_fetch(bld
, inst
, 2);
870 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
871 dst0
= lp_build_select(&bld
->base
, tmp0
, src1
, src2
);
874 case TGSI_OPCODE_SCS
:
877 case TGSI_OPCODE_TXB
:
878 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
);
881 case TGSI_OPCODE_NRM
:
883 case TGSI_OPCODE_NRM4
:
886 case TGSI_OPCODE_DIV
:
892 case TGSI_OPCODE_DP2
:
895 case TGSI_OPCODE_TXL
:
896 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
);
899 case TGSI_OPCODE_TXP
:
900 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_PROJECTED
);
903 case TGSI_OPCODE_BRK
:
909 case TGSI_OPCODE_BGNLOOP
:
912 case TGSI_OPCODE_BGNSUB
:
915 case TGSI_OPCODE_ELSE
:
918 case TGSI_OPCODE_ENDIF
:
921 case TGSI_OPCODE_ENDLOOP
:
924 case TGSI_OPCODE_ENDSUB
:
927 case TGSI_OPCODE_PUSHA
:
933 case TGSI_OPCODE_POPA
:
939 case TGSI_OPCODE_CEIL
:
940 src0
= emit_fetch(bld
, inst
, 0);
941 dst0
= lp_build_ceil(&bld
->base
, src0
);
944 case TGSI_OPCODE_I2F
:
950 case TGSI_OPCODE_NOT
:
956 case TGSI_OPCODE_TRUNC
:
957 src0
= emit_fetch(bld
, inst
, 0);
958 dst0
= lp_build_trunc(&bld
->base
, src0
);
961 case TGSI_OPCODE_SHL
:
967 case TGSI_OPCODE_ISHR
:
973 case TGSI_OPCODE_AND
:
985 case TGSI_OPCODE_MOD
:
991 case TGSI_OPCODE_XOR
:
997 case TGSI_OPCODE_SAD
:
1003 case TGSI_OPCODE_TXF
:
1009 case TGSI_OPCODE_TXQ
:
1015 case TGSI_OPCODE_CONT
:
1018 case TGSI_OPCODE_EMIT
:
1022 case TGSI_OPCODE_ENDPRIM
:
1026 case TGSI_OPCODE_NOP
:
1033 if (info
->num_dst
) {
1034 emit_store(bld
, inst
, 0, dst0
);
1042 lp_build_tgsi_aos(LLVMBuilderRef builder
,
1043 const struct tgsi_token
*tokens
,
1044 struct lp_type type
,
1045 const unsigned char swizzles
[4],
1046 LLVMValueRef consts_ptr
,
1047 const LLVMValueRef
*inputs
,
1048 LLVMValueRef
*outputs
,
1049 struct lp_build_sampler_aos
*sampler
,
1050 const struct tgsi_shader_info
*info
)
1052 struct lp_build_tgsi_aos_context bld
;
1053 struct tgsi_parse_context parse
;
1054 uint num_immediates
= 0;
1055 uint num_instructions
= 0;
1059 /* Setup build context */
1060 memset(&bld
, 0, sizeof bld
);
1061 lp_build_context_init(&bld
.base
, builder
, type
);
1062 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
1064 for (chan
= 0; chan
< 4; ++chan
) {
1065 bld
.swizzles
[chan
] = swizzles
[chan
];
1066 bld
.inv_swizzles
[swizzles
[chan
]] = chan
;
1069 bld
.inputs
= inputs
;
1070 bld
.outputs
= outputs
;
1071 bld
.consts_ptr
= consts_ptr
;
1072 bld
.sampler
= sampler
;
1073 bld
.indirect_files
= info
->indirect_files
;
1074 bld
.instructions
= (struct tgsi_full_instruction
*)
1075 MALLOC(LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
));
1076 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
1078 if (!bld
.instructions
) {
1082 tgsi_parse_init(&parse
, tokens
);
1084 while (!tgsi_parse_end_of_tokens(&parse
)) {
1085 tgsi_parse_token(&parse
);
1087 switch(parse
.FullToken
.Token
.Type
) {
1088 case TGSI_TOKEN_TYPE_DECLARATION
:
1089 /* Inputs already interpolated */
1090 emit_declaration(&bld
, &parse
.FullToken
.FullDeclaration
);
1093 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1095 /* save expanded instruction */
1096 if (num_instructions
== bld
.max_instructions
) {
1097 struct tgsi_full_instruction
*instructions
;
1098 instructions
= REALLOC(bld
.instructions
,
1099 bld
.max_instructions
1100 * sizeof(struct tgsi_full_instruction
),
1101 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
1102 * sizeof(struct tgsi_full_instruction
));
1103 if (!instructions
) {
1106 bld
.instructions
= instructions
;
1107 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
1110 memcpy(bld
.instructions
+ num_instructions
,
1111 &parse
.FullToken
.FullInstruction
,
1112 sizeof(bld
.instructions
[0]));
1119 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1120 /* simply copy the immediate values into the next immediates[] slot */
1122 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1125 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1126 for (chan
= 0; chan
< 4; ++chan
) {
1129 for (chan
= 0; chan
< size
; ++chan
) {
1130 unsigned swizzle
= bld
.swizzles
[chan
];
1131 imm
[swizzle
] = parse
.FullToken
.FullImmediate
.u
[chan
].Float
;
1133 bld
.immediates
[num_immediates
] =
1134 lp_build_const_aos(type
,
1135 imm
[0], imm
[1], imm
[2], imm
[3],
1141 case TGSI_TOKEN_TYPE_PROPERTY
:
1150 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
1151 const struct tgsi_opcode_info
*opcode_info
=
1152 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
1153 if (!emit_instruction(&bld
, instr
, opcode_info
, &pc
))
1154 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1155 opcode_info
->mnemonic
);
1159 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
1160 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
1161 debug_printf("11111111111111111111111111111 \n");
1162 tgsi_dump(tokens
, 0);
1163 lp_debug_dump_value(function
);
1164 debug_printf("2222222222222222222222222222 \n");
1166 tgsi_parse_free(&parse
);
1169 LLVMModuleRef module
= LLVMGetGlobalParent(
1170 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
1171 LLVMDumpModule(module
);
1174 FREE(bld
.instructions
);