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_gather.h"
54 #include "lp_bld_logic.h"
55 #include "lp_bld_swizzle.h"
56 #include "lp_bld_flow.h"
57 #include "lp_bld_quad.h"
58 #include "lp_bld_tgsi.h"
59 #include "lp_bld_limits.h"
60 #include "lp_bld_debug.h"
63 #define LP_MAX_INSTRUCTIONS 256
66 struct lp_build_tgsi_aos_context
68 struct lp_build_context base
;
70 /* Builder for integer masks and indices */
71 struct lp_build_context int_bld
;
75 * - swizzles[0] = red index
76 * - swizzles[1] = green index
77 * - swizzles[2] = blue index
78 * - swizzles[3] = alpha index
80 unsigned char swizzles
[4];
81 unsigned char inv_swizzles
[4];
83 LLVMValueRef consts_ptr
;
84 const LLVMValueRef
*inputs
;
85 LLVMValueRef
*outputs
;
87 struct lp_build_sampler_aos
*sampler
;
89 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
90 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
91 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
92 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
94 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
95 * set in the indirect_files field.
96 * The temps[] array above is unused then.
98 LLVMValueRef temps_array
;
100 /** bitmask indicating which register files are accessed indirectly */
101 unsigned indirect_files
;
103 struct tgsi_full_instruction
*instructions
;
104 uint max_instructions
;
109 * Wrapper around lp_build_swizzle_aos which translates swizzles to another
113 swizzle_aos(struct lp_build_tgsi_aos_context
*bld
,
120 unsigned char swizzles
[4];
122 assert(swizzle_x
< 4);
123 assert(swizzle_y
< 4);
124 assert(swizzle_z
< 4);
125 assert(swizzle_w
< 4);
127 swizzles
[bld
->inv_swizzles
[0]] = bld
->swizzles
[swizzle_x
];
128 swizzles
[bld
->inv_swizzles
[1]] = bld
->swizzles
[swizzle_y
];
129 swizzles
[bld
->inv_swizzles
[2]] = bld
->swizzles
[swizzle_z
];
130 swizzles
[bld
->inv_swizzles
[3]] = bld
->swizzles
[swizzle_w
];
132 return lp_build_swizzle_aos(&bld
->base
, a
, swizzles
);
137 swizzle_scalar_aos(struct lp_build_tgsi_aos_context
*bld
,
141 chan
= bld
->swizzles
[chan
];
142 return lp_build_swizzle_scalar_aos(&bld
->base
, a
, chan
);
151 struct lp_build_tgsi_aos_context
*bld
,
152 const struct tgsi_full_instruction
*inst
,
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
= LLVMConstInt(LLVMInt32Type(),
180 reg
->Register
.Index
*4 + chan
,
183 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
186 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
188 lp_build_name(scalar
, "const[%u].%c", reg
->Register
.Index
, "xyzw"[chan
]);
191 * NOTE: constants array is always assumed to be RGBA
194 swizzle
= LLVMConstInt(LLVMInt32Type(), chan
, 0);
196 res
= LLVMBuildInsertElement(bld
->base
.builder
, res
, scalar
, swizzle
, "");
200 * Broadcast the first quaternion to all others.
202 * XXX: could be factored into a reusable function.
205 if (type
.length
> 4) {
206 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
209 for (chan
= 0; chan
< 4; ++chan
) {
210 shuffles
[chan
] = LLVMConstInt(LLVMInt32Type(), chan
, 0);
213 for (i
= 4; i
< type
.length
; ++i
) {
214 shuffles
[i
] = shuffles
[i
% 4];
217 res
= LLVMBuildShuffleVector(bld
->base
.builder
,
218 res
, bld
->base
.undef
,
219 LLVMConstVector(shuffles
, type
.length
),
224 case TGSI_FILE_IMMEDIATE
:
225 res
= bld
->immediates
[reg
->Register
.Index
];
229 case TGSI_FILE_INPUT
:
230 res
= bld
->inputs
[reg
->Register
.Index
];
234 case TGSI_FILE_TEMPORARY
:
236 LLVMValueRef temp_ptr
;
237 temp_ptr
= bld
->temps
[reg
->Register
.Index
];
238 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
240 return bld
->base
.undef
;
245 assert(0 && "invalid src register in emit_fetch()");
246 return bld
->base
.undef
;
250 * Apply sign modifier.
253 if (reg
->Register
.Absolute
) {
254 res
= lp_build_abs(&bld
->base
, res
);
257 if(reg
->Register
.Negate
) {
258 res
= lp_build_negate(&bld
->base
, res
);
262 * Swizzle the argument
265 res
= swizzle_aos(bld
, res
,
266 reg
->Register
.SwizzleX
,
267 reg
->Register
.SwizzleY
,
268 reg
->Register
.SwizzleZ
,
269 reg
->Register
.SwizzleW
);
280 struct lp_build_tgsi_aos_context
*bld
,
281 const struct tgsi_full_instruction
*inst
,
285 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
286 LLVMValueRef mask
= NULL
;
293 switch (inst
->Instruction
.Saturate
) {
297 case TGSI_SAT_ZERO_ONE
:
298 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
299 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
302 case TGSI_SAT_MINUS_PLUS_ONE
:
303 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
304 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
312 * Translate the register file
315 assert(!reg
->Register
.Indirect
);
317 switch (reg
->Register
.File
) {
318 case TGSI_FILE_OUTPUT
:
319 ptr
= bld
->outputs
[reg
->Register
.Index
];
322 case TGSI_FILE_TEMPORARY
:
323 ptr
= bld
->temps
[reg
->Register
.Index
];
326 case TGSI_FILE_ADDRESS
:
327 ptr
= bld
->addr
[reg
->Indirect
.Index
];
330 case TGSI_FILE_PREDICATE
:
331 ptr
= bld
->preds
[reg
->Register
.Index
];
343 if (inst
->Instruction
.Predicate
) {
346 assert(inst
->Predicate
.Index
< LP_MAX_TGSI_PREDS
);
348 pred
= LLVMBuildLoad(bld
->base
.builder
,
349 bld
->preds
[inst
->Predicate
.Index
], "");
352 * Convert the value to an integer mask.
354 pred
= lp_build_compare(bld
->base
.builder
,
360 if (inst
->Predicate
.Negate
) {
361 pred
= LLVMBuildNot(bld
->base
.builder
, pred
, "");
364 pred
= swizzle_aos(bld
, pred
,
365 inst
->Predicate
.SwizzleX
,
366 inst
->Predicate
.SwizzleY
,
367 inst
->Predicate
.SwizzleZ
,
368 inst
->Predicate
.SwizzleW
);
371 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, pred
, "");
381 if (reg
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
) {
382 LLVMValueRef writemask
;
384 writemask
= lp_build_const_mask_aos(bld
->base
.type
, reg
->Register
.WriteMask
);
387 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, writemask
, "");
394 LLVMValueRef orig_value
;
396 orig_value
= LLVMBuildLoad(bld
->base
.builder
, ptr
, "");
397 value
= lp_build_select(&bld
->base
,
398 mask
, value
, orig_value
);
401 LLVMBuildStore(bld
->base
.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 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
460 unsigned first
= decl
->Range
.First
;
461 unsigned last
= decl
->Range
.Last
;
464 for (idx
= first
; idx
<= last
; ++idx
) {
465 switch (decl
->Declaration
.File
) {
466 case TGSI_FILE_TEMPORARY
:
467 assert(idx
< LP_MAX_TGSI_TEMPS
);
468 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
469 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
471 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
472 vec_type
, array_size
, "");
474 bld
->temps
[idx
] = lp_build_alloca(bld
->base
.builder
,
479 case TGSI_FILE_OUTPUT
:
480 bld
->outputs
[idx
] = lp_build_alloca(bld
->base
.builder
,
484 case TGSI_FILE_ADDRESS
:
485 assert(idx
< LP_MAX_TGSI_ADDRS
);
486 bld
->addr
[idx
] = lp_build_alloca(bld
->base
.builder
,
490 case TGSI_FILE_PREDICATE
:
491 assert(idx
< LP_MAX_TGSI_PREDS
);
492 bld
->preds
[idx
] = lp_build_alloca(bld
->base
.builder
,
497 /* don't need to declare other vars */
505 * Emit LLVM for one TGSI instruction.
506 * \param return TRUE for success, FALSE otherwise
510 struct lp_build_tgsi_aos_context
*bld
,
511 const struct tgsi_full_instruction
*inst
,
512 const struct tgsi_opcode_info
*info
,
515 LLVMValueRef src0
, src1
, src2
;
516 LLVMValueRef tmp0
, tmp1
;
520 * Stores and write masks are handled in a general fashion after the long
521 * instruction opcode switch statement.
523 * Although not stricitly necessary, we avoid generating instructions for
524 * channels which won't be stored, in cases where's that easy. For some
525 * complex instructions, like texture sampling, it is more convenient to
526 * assume a full writemask and then let LLVM optimization passes eliminate
532 assert(info
->num_dst
<= 1);
534 dst0
= bld
->base
.undef
;
537 switch (inst
->Instruction
.Opcode
) {
538 case TGSI_OPCODE_ARL
:
539 src0
= emit_fetch(bld
, inst
, 0);
540 dst0
= lp_build_floor(&bld
->base
, src0
);
543 case TGSI_OPCODE_MOV
:
544 dst0
= emit_fetch(bld
, inst
, 0);
547 case TGSI_OPCODE_LIT
:
550 case TGSI_OPCODE_RCP
:
551 /* TGSI_OPCODE_RECIP */
552 src0
= emit_fetch(bld
, inst
, 0);
553 dst0
= lp_build_rcp(&bld
->base
, src0
);
556 case TGSI_OPCODE_RSQ
:
557 /* TGSI_OPCODE_RECIPSQRT */
558 src0
= emit_fetch(bld
, inst
, 0);
559 tmp0
= lp_build_abs(&bld
->base
, src0
);
560 dst0
= lp_build_rsqrt(&bld
->base
, tmp0
);
563 case TGSI_OPCODE_EXP
:
566 case TGSI_OPCODE_LOG
:
569 case TGSI_OPCODE_MUL
:
570 src0
= emit_fetch(bld
, inst
, 0);
571 src1
= emit_fetch(bld
, inst
, 1);
572 dst0
= lp_build_mul(&bld
->base
, src0
, src1
);
575 case TGSI_OPCODE_ADD
:
576 src0
= emit_fetch(bld
, inst
, 0);
577 src1
= emit_fetch(bld
, inst
, 1);
578 dst0
= lp_build_add(&bld
->base
, src0
, src1
);
581 case TGSI_OPCODE_DP3
:
582 /* TGSI_OPCODE_DOT3 */
585 case TGSI_OPCODE_DP4
:
586 /* TGSI_OPCODE_DOT4 */
589 case TGSI_OPCODE_DST
:
592 case TGSI_OPCODE_MIN
:
593 src0
= emit_fetch(bld
, inst
, 0);
594 src1
= emit_fetch(bld
, inst
, 1);
595 dst0
= lp_build_max(&bld
->base
, src0
, src1
);
598 case TGSI_OPCODE_MAX
:
599 src0
= emit_fetch(bld
, inst
, 0);
600 src1
= emit_fetch(bld
, inst
, 1);
601 dst0
= lp_build_max(&bld
->base
, src0
, src1
);
604 case TGSI_OPCODE_SLT
:
605 /* TGSI_OPCODE_SETLT */
606 src0
= emit_fetch(bld
, inst
, 0);
607 src1
= emit_fetch(bld
, inst
, 1);
608 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
609 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
612 case TGSI_OPCODE_SGE
:
613 /* TGSI_OPCODE_SETGE */
614 src0
= emit_fetch(bld
, inst
, 0);
615 src1
= emit_fetch(bld
, inst
, 1);
616 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
617 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
620 case TGSI_OPCODE_MAD
:
621 /* TGSI_OPCODE_MADD */
622 src0
= emit_fetch(bld
, inst
, 0);
623 src1
= emit_fetch(bld
, inst
, 1);
624 src2
= emit_fetch(bld
, inst
, 2);
625 tmp0
= lp_build_mul(&bld
->base
, src0
, src1
);
626 dst0
= lp_build_add(&bld
->base
, tmp0
, src2
);
629 case TGSI_OPCODE_SUB
:
630 src0
= emit_fetch(bld
, inst
, 0);
631 src1
= emit_fetch(bld
, inst
, 1);
632 dst0
= lp_build_sub(&bld
->base
, src0
, src1
);
635 case TGSI_OPCODE_LRP
:
636 src0
= emit_fetch(bld
, inst
, 0);
637 src1
= emit_fetch(bld
, inst
, 1);
638 src2
= emit_fetch(bld
, inst
, 2);
639 tmp0
= lp_build_sub(&bld
->base
, src1
, src2
);
640 tmp0
= lp_build_mul(&bld
->base
, src0
, tmp0
);
641 dst0
= lp_build_add(&bld
->base
, tmp0
, src2
);
644 case TGSI_OPCODE_CND
:
645 src0
= emit_fetch(bld
, inst
, 0);
646 src1
= emit_fetch(bld
, inst
, 1);
647 src2
= emit_fetch(bld
, inst
, 2);
648 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
649 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
650 dst0
= lp_build_select(&bld
->base
, tmp0
, src0
, src1
);
653 case TGSI_OPCODE_DP2A
:
656 case TGSI_OPCODE_FRC
:
657 src0
= emit_fetch(bld
, inst
, 0);
658 tmp0
= lp_build_floor(&bld
->base
, src0
);
659 dst0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
662 case TGSI_OPCODE_CLAMP
:
663 src0
= emit_fetch(bld
, inst
, 0);
664 src1
= emit_fetch(bld
, inst
, 1);
665 src2
= emit_fetch(bld
, inst
, 2);
666 tmp0
= lp_build_max(&bld
->base
, src0
, src1
);
667 dst0
= lp_build_min(&bld
->base
, tmp0
, src2
);
670 case TGSI_OPCODE_FLR
:
671 src0
= emit_fetch(bld
, inst
, 0);
672 dst0
= lp_build_floor(&bld
->base
, src0
);
675 case TGSI_OPCODE_ROUND
:
676 src0
= emit_fetch(bld
, inst
, 0);
677 dst0
= lp_build_round(&bld
->base
, src0
);
680 case TGSI_OPCODE_EX2
:
681 src0
= emit_fetch(bld
, inst
, 0);
682 tmp0
= lp_build_swizzle_scalar_aos(&bld
->base
, src0
, TGSI_SWIZZLE_X
);
683 dst0
= lp_build_exp2(&bld
->base
, tmp0
);
686 case TGSI_OPCODE_LG2
:
687 src0
= emit_fetch(bld
, inst
, 0);
688 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
689 dst0
= lp_build_log2(&bld
->base
, tmp0
);
692 case TGSI_OPCODE_POW
:
693 src0
= emit_fetch(bld
, inst
, 0);
694 src0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
695 src1
= emit_fetch(bld
, inst
, 1);
696 src1
= swizzle_scalar_aos(bld
, src1
, TGSI_SWIZZLE_X
);
697 dst0
= lp_build_pow(&bld
->base
, src0
, src1
);
700 case TGSI_OPCODE_XPD
:
703 case TGSI_OPCODE_ABS
:
704 src0
= emit_fetch(bld
, inst
, 0);
705 dst0
= lp_build_abs(&bld
->base
, src0
);
708 case TGSI_OPCODE_RCC
:
713 case TGSI_OPCODE_DPH
:
716 case TGSI_OPCODE_COS
:
717 src0
= emit_fetch(bld
, inst
, 0);
718 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
719 dst0
= lp_build_cos(&bld
->base
, tmp0
);
722 case TGSI_OPCODE_DDX
:
725 case TGSI_OPCODE_DDY
:
728 case TGSI_OPCODE_KILP
:
729 /* predicated kill */
732 case TGSI_OPCODE_KIL
:
733 /* conditional kill */
736 case TGSI_OPCODE_PK2H
:
740 case TGSI_OPCODE_PK2US
:
744 case TGSI_OPCODE_PK4B
:
748 case TGSI_OPCODE_PK4UB
:
751 case TGSI_OPCODE_RFL
:
754 case TGSI_OPCODE_SEQ
:
755 src0
= emit_fetch(bld
, inst
, 0);
756 src1
= emit_fetch(bld
, inst
, 1);
757 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
758 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
761 case TGSI_OPCODE_SFL
:
762 dst0
= bld
->base
.zero
;
765 case TGSI_OPCODE_SGT
:
766 src0
= emit_fetch(bld
, inst
, 0);
767 src1
= emit_fetch(bld
, inst
, 1);
768 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
769 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
772 case TGSI_OPCODE_SIN
:
773 src0
= emit_fetch(bld
, inst
, 0);
774 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
775 dst0
= lp_build_sin(&bld
->base
, tmp0
);
778 case TGSI_OPCODE_SLE
:
779 src0
= emit_fetch(bld
, inst
, 0);
780 src1
= emit_fetch(bld
, inst
, 1);
781 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
782 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
785 case TGSI_OPCODE_SNE
:
786 src0
= emit_fetch(bld
, inst
, 0);
787 src1
= emit_fetch(bld
, inst
, 1);
788 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
789 dst0
= lp_build_select(&bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
792 case TGSI_OPCODE_STR
:
793 dst0
= bld
->base
.one
;
796 case TGSI_OPCODE_TEX
:
797 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_NONE
);
800 case TGSI_OPCODE_TXD
:
801 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
);
804 case TGSI_OPCODE_UP2H
:
810 case TGSI_OPCODE_UP2US
:
816 case TGSI_OPCODE_UP4B
:
822 case TGSI_OPCODE_UP4UB
:
828 case TGSI_OPCODE_X2D
:
834 case TGSI_OPCODE_ARA
:
840 case TGSI_OPCODE_ARR
:
841 src0
= emit_fetch(bld
, inst
, 0);
842 dst0
= lp_build_round(&bld
->base
, src0
);
845 case TGSI_OPCODE_BRA
:
851 case TGSI_OPCODE_CAL
:
854 case TGSI_OPCODE_RET
:
857 case TGSI_OPCODE_END
:
861 case TGSI_OPCODE_SSG
:
862 /* TGSI_OPCODE_SGN */
863 tmp0
= emit_fetch(bld
, inst
, 0);
864 dst0
= lp_build_sgn(&bld
->base
, tmp0
);
867 case TGSI_OPCODE_CMP
:
868 src0
= emit_fetch(bld
, inst
, 0);
869 src1
= emit_fetch(bld
, inst
, 1);
870 src2
= emit_fetch(bld
, inst
, 2);
871 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
872 dst0
= lp_build_select(&bld
->base
, tmp0
, src1
, src2
);
875 case TGSI_OPCODE_SCS
:
878 case TGSI_OPCODE_TXB
:
879 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
);
882 case TGSI_OPCODE_NRM
:
884 case TGSI_OPCODE_NRM4
:
887 case TGSI_OPCODE_DIV
:
893 case TGSI_OPCODE_DP2
:
896 case TGSI_OPCODE_TXL
:
897 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
);
900 case TGSI_OPCODE_TXP
:
901 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_PROJECTED
);
904 case TGSI_OPCODE_BRK
:
910 case TGSI_OPCODE_BGNLOOP
:
913 case TGSI_OPCODE_BGNSUB
:
916 case TGSI_OPCODE_ELSE
:
919 case TGSI_OPCODE_ENDIF
:
922 case TGSI_OPCODE_ENDLOOP
:
925 case TGSI_OPCODE_ENDSUB
:
928 case TGSI_OPCODE_PUSHA
:
934 case TGSI_OPCODE_POPA
:
940 case TGSI_OPCODE_CEIL
:
941 src0
= emit_fetch(bld
, inst
, 0);
942 dst0
= lp_build_ceil(&bld
->base
, src0
);
945 case TGSI_OPCODE_I2F
:
951 case TGSI_OPCODE_NOT
:
957 case TGSI_OPCODE_TRUNC
:
958 src0
= emit_fetch(bld
, inst
, 0);
959 dst0
= lp_build_trunc(&bld
->base
, src0
);
962 case TGSI_OPCODE_SHL
:
968 case TGSI_OPCODE_ISHR
:
974 case TGSI_OPCODE_AND
:
986 case TGSI_OPCODE_MOD
:
992 case TGSI_OPCODE_XOR
:
998 case TGSI_OPCODE_SAD
:
1004 case TGSI_OPCODE_TXF
:
1010 case TGSI_OPCODE_TXQ
:
1016 case TGSI_OPCODE_CONT
:
1019 case TGSI_OPCODE_EMIT
:
1023 case TGSI_OPCODE_ENDPRIM
:
1027 case TGSI_OPCODE_NOP
:
1034 if (info
->num_dst
) {
1035 emit_store(bld
, inst
, 0, dst0
);
1043 lp_build_tgsi_aos(LLVMBuilderRef builder
,
1044 const struct tgsi_token
*tokens
,
1045 struct lp_type type
,
1046 const unsigned char swizzles
[4],
1047 LLVMValueRef consts_ptr
,
1048 const LLVMValueRef
*inputs
,
1049 LLVMValueRef
*outputs
,
1050 struct lp_build_sampler_aos
*sampler
,
1051 const struct tgsi_shader_info
*info
)
1053 struct lp_build_tgsi_aos_context bld
;
1054 struct tgsi_parse_context parse
;
1055 uint num_immediates
= 0;
1056 uint num_instructions
= 0;
1060 /* Setup build context */
1061 memset(&bld
, 0, sizeof bld
);
1062 lp_build_context_init(&bld
.base
, builder
, type
);
1063 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
1065 for (chan
= 0; chan
< 4; ++chan
) {
1066 bld
.swizzles
[chan
] = swizzles
[chan
];
1067 bld
.inv_swizzles
[swizzles
[chan
]] = chan
;
1070 bld
.inputs
= inputs
;
1071 bld
.outputs
= outputs
;
1072 bld
.consts_ptr
= consts_ptr
;
1073 bld
.sampler
= sampler
;
1074 bld
.indirect_files
= info
->indirect_files
;
1075 bld
.instructions
= (struct tgsi_full_instruction
*)
1076 MALLOC(LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
));
1077 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
1079 if (!bld
.instructions
) {
1083 tgsi_parse_init(&parse
, tokens
);
1085 while (!tgsi_parse_end_of_tokens(&parse
)) {
1086 tgsi_parse_token(&parse
);
1088 switch(parse
.FullToken
.Token
.Type
) {
1089 case TGSI_TOKEN_TYPE_DECLARATION
:
1090 /* Inputs already interpolated */
1091 emit_declaration(&bld
, &parse
.FullToken
.FullDeclaration
);
1094 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1096 /* save expanded instruction */
1097 if (num_instructions
== bld
.max_instructions
) {
1098 struct tgsi_full_instruction
*instructions
;
1099 instructions
= REALLOC(bld
.instructions
,
1100 bld
.max_instructions
1101 * sizeof(struct tgsi_full_instruction
),
1102 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
1103 * sizeof(struct tgsi_full_instruction
));
1104 if (!instructions
) {
1107 bld
.instructions
= instructions
;
1108 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
1111 memcpy(bld
.instructions
+ num_instructions
,
1112 &parse
.FullToken
.FullInstruction
,
1113 sizeof(bld
.instructions
[0]));
1120 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1121 /* simply copy the immediate values into the next immediates[] slot */
1123 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1126 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1127 for (chan
= 0; chan
< 4; ++chan
) {
1130 for (chan
= 0; chan
< size
; ++chan
) {
1131 unsigned swizzle
= bld
.swizzles
[chan
];
1132 imm
[swizzle
] = parse
.FullToken
.FullImmediate
.u
[chan
].Float
;
1134 bld
.immediates
[num_immediates
] =
1135 lp_build_const_aos(type
,
1136 imm
[0], imm
[1], imm
[2], imm
[3],
1142 case TGSI_TOKEN_TYPE_PROPERTY
:
1151 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
1152 const struct tgsi_opcode_info
*opcode_info
=
1153 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
1154 if (!emit_instruction(&bld
, instr
, opcode_info
, &pc
))
1155 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1156 opcode_info
->mnemonic
);
1160 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
1161 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
1162 debug_printf("11111111111111111111111111111 \n");
1163 tgsi_dump(tokens
, 0);
1164 lp_debug_dump_value(function
);
1165 debug_printf("2222222222222222222222222222 \n");
1167 tgsi_parse_free(&parse
);
1170 LLVMModuleRef module
= LLVMGetGlobalParent(
1171 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
1172 LLVMDumpModule(module
);
1175 FREE(bld
.instructions
);