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_debug.h"
59 #include "lp_bld_sample.h"
63 * Wrapper around lp_build_swizzle_aos which translates swizzles to another
67 swizzle_aos(struct lp_build_tgsi_context
*bld_base
,
74 unsigned char swizzles
[4];
75 struct lp_build_tgsi_aos_context
*bld
= lp_aos_context(bld_base
);
77 assert(swizzle_x
< 4);
78 assert(swizzle_y
< 4);
79 assert(swizzle_z
< 4);
80 assert(swizzle_w
< 4);
82 swizzles
[bld
->inv_swizzles
[0]] = bld
->swizzles
[swizzle_x
];
83 swizzles
[bld
->inv_swizzles
[1]] = bld
->swizzles
[swizzle_y
];
84 swizzles
[bld
->inv_swizzles
[2]] = bld
->swizzles
[swizzle_z
];
85 swizzles
[bld
->inv_swizzles
[3]] = bld
->swizzles
[swizzle_w
];
87 return lp_build_swizzle_aos(&bld
->bld_base
.base
, a
, swizzles
);
92 swizzle_scalar_aos(struct lp_build_tgsi_aos_context
*bld
,
96 chan
= bld
->swizzles
[chan
];
97 return lp_build_swizzle_scalar_aos(&bld
->bld_base
.base
, a
, chan
, 4);
103 struct lp_build_tgsi_context
* bld_base
,
104 const struct tgsi_full_src_register
* reg
,
105 enum tgsi_opcode_type stype
,
108 struct lp_build_tgsi_aos_context
* bld
= lp_aos_context(bld_base
);
109 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
110 struct lp_type type
= bld_base
->base
.type
;
114 assert(!reg
->Register
.Indirect
);
117 * Get the constants components
120 res
= bld
->bld_base
.base
.undef
;
121 for (chan
= 0; chan
< 4; ++chan
) {
123 LLVMValueRef scalar_ptr
;
125 LLVMValueRef swizzle
;
127 index
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
,
128 reg
->Register
.Index
* 4 + chan
);
130 scalar_ptr
= LLVMBuildGEP(builder
, bld
->consts_ptr
, &index
, 1, "");
132 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
134 lp_build_name(scalar
, "const[%u].%c", reg
->Register
.Index
, "xyzw"[chan
]);
137 * NOTE: constants array is always assumed to be RGBA
140 swizzle
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
,
141 bld
->swizzles
[chan
]);
143 res
= LLVMBuildInsertElement(builder
, res
, scalar
, swizzle
, "");
147 * Broadcast the first quaternion to all others.
149 * XXX: could be factored into a reusable function.
152 if (type
.length
> 4) {
153 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
156 for (chan
= 0; chan
< 4; ++chan
) {
157 shuffles
[chan
] = lp_build_const_int32(bld
->bld_base
.base
.gallivm
, chan
);
160 for (i
= 4; i
< type
.length
; ++i
) {
161 shuffles
[i
] = shuffles
[i
% 4];
164 res
= LLVMBuildShuffleVector(builder
,
165 res
, bld
->bld_base
.base
.undef
,
166 LLVMConstVector(shuffles
, type
.length
),
173 emit_fetch_immediate(
174 struct lp_build_tgsi_context
* bld_base
,
175 const struct tgsi_full_src_register
* reg
,
176 enum tgsi_opcode_type stype
,
179 struct lp_build_tgsi_aos_context
* bld
= lp_aos_context(bld_base
);
180 LLVMValueRef res
= bld
->immediates
[reg
->Register
.Index
];
187 struct lp_build_tgsi_context
* bld_base
,
188 const struct tgsi_full_src_register
* reg
,
189 enum tgsi_opcode_type stype
,
192 struct lp_build_tgsi_aos_context
* bld
= lp_aos_context(bld_base
);
193 LLVMValueRef res
= bld
->inputs
[reg
->Register
.Index
];
194 assert(!reg
->Register
.Indirect
);
200 emit_fetch_temporary(
201 struct lp_build_tgsi_context
* bld_base
,
202 const struct tgsi_full_src_register
* reg
,
203 enum tgsi_opcode_type stype
,
206 struct lp_build_tgsi_aos_context
* bld
= lp_aos_context(bld_base
);
207 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
208 LLVMValueRef temp_ptr
= bld
->temps
[reg
->Register
.Index
];
209 LLVMValueRef res
= LLVMBuildLoad(builder
, temp_ptr
, "");
210 assert(!reg
->Register
.Indirect
);
212 return bld
->bld_base
.base
.undef
;
222 struct lp_build_tgsi_aos_context
*bld
,
223 const struct tgsi_full_instruction
*inst
,
227 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
228 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
229 LLVMValueRef mask
= NULL
;
236 switch (inst
->Instruction
.Saturate
) {
240 case TGSI_SAT_ZERO_ONE
:
241 value
= lp_build_max(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.zero
);
242 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
245 case TGSI_SAT_MINUS_PLUS_ONE
:
246 value
= lp_build_max(&bld
->bld_base
.base
, value
, lp_build_const_vec(bld
->bld_base
.base
.gallivm
, bld
->bld_base
.base
.type
, -1.0));
247 value
= lp_build_min(&bld
->bld_base
.base
, value
, bld
->bld_base
.base
.one
);
255 * Translate the register file
258 assert(!reg
->Register
.Indirect
);
260 switch (reg
->Register
.File
) {
261 case TGSI_FILE_OUTPUT
:
262 ptr
= bld
->outputs
[reg
->Register
.Index
];
265 case TGSI_FILE_TEMPORARY
:
266 ptr
= bld
->temps
[reg
->Register
.Index
];
269 case TGSI_FILE_ADDRESS
:
270 ptr
= bld
->addr
[reg
->Indirect
.Index
];
273 case TGSI_FILE_PREDICATE
:
274 ptr
= bld
->preds
[reg
->Register
.Index
];
288 if (inst
->Instruction
.Predicate
) {
291 assert(inst
->Predicate
.Index
< LP_MAX_TGSI_PREDS
);
293 pred
= LLVMBuildLoad(builder
,
294 bld
->preds
[inst
->Predicate
.Index
], "");
297 * Convert the value to an integer mask.
299 pred
= lp_build_compare(bld
->bld_base
.base
.gallivm
,
300 bld
->bld_base
.base
.type
,
303 bld
->bld_base
.base
.zero
);
305 if (inst
->Predicate
.Negate
) {
306 pred
= LLVMBuildNot(builder
, pred
, "");
309 pred
= bld
->bld_base
.emit_swizzle(&bld
->bld_base
, pred
,
310 inst
->Predicate
.SwizzleX
,
311 inst
->Predicate
.SwizzleY
,
312 inst
->Predicate
.SwizzleZ
,
313 inst
->Predicate
.SwizzleW
);
316 mask
= LLVMBuildAnd(builder
, mask
, pred
, "");
326 if (reg
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
) {
327 LLVMValueRef writemask
;
329 writemask
= lp_build_const_mask_aos_swizzled(bld
->bld_base
.base
.gallivm
,
330 bld
->bld_base
.base
.type
,
331 reg
->Register
.WriteMask
,
336 mask
= LLVMBuildAnd(builder
, mask
, writemask
, "");
343 LLVMValueRef orig_value
;
345 orig_value
= LLVMBuildLoad(builder
, ptr
, "");
346 value
= lp_build_select(&bld
->bld_base
.base
,
347 mask
, value
, orig_value
);
350 LLVMBuildStore(builder
, value
, ptr
);
355 * High-level instruction translators.
359 emit_tex(struct lp_build_tgsi_aos_context
*bld
,
360 const struct tgsi_full_instruction
*inst
,
361 enum lp_build_tex_modifier modifier
)
366 struct lp_derivatives derivs
;
369 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
370 return bld
->bld_base
.base
.undef
;
373 target
= inst
->Texture
.Texture
;
375 coords
= lp_build_emit_fetch( &bld
->bld_base
, inst
, 0 , LP_CHAN_ALL
);
377 if (0 && modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
378 lp_build_emit_fetch( &bld
->bld_base
, inst
, 1 , LP_CHAN_ALL
);
379 lp_build_emit_fetch( &bld
->bld_base
, inst
, 2 , LP_CHAN_ALL
);
380 unit
= inst
->Src
[3].Register
.Index
;
383 ddx
= lp_build_ddx( &bld
->bld_base
.base
, coords
);
384 ddy
= lp_build_ddy( &bld
->bld_base
.base
, coords
);
387 derivs
.ddx_ddy
[0] = bld
->bld_base
.base
.one
;
388 derivs
.ddx_ddy
[1] = bld
->bld_base
.base
.one
;
390 unit
= inst
->Src
[1].Register
.Index
;
393 return bld
->sampler
->emit_fetch_texel(bld
->sampler
,
402 lp_emit_declaration_aos(
403 struct lp_build_tgsi_aos_context
*bld
,
404 const struct tgsi_full_declaration
*decl
)
406 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
407 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->bld_base
.base
.gallivm
, bld
->bld_base
.base
.type
);
409 unsigned first
= decl
->Range
.First
;
410 unsigned last
= decl
->Range
.Last
;
413 for (idx
= first
; idx
<= last
; ++idx
) {
414 switch (decl
->Declaration
.File
) {
415 case TGSI_FILE_TEMPORARY
:
416 assert(idx
< LP_MAX_TGSI_TEMPS
);
417 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
418 LLVMValueRef array_size
= lp_build_const_int32(gallivm
, last
+ 1);
419 bld
->temps_array
= lp_build_array_alloca(bld
->bld_base
.base
.gallivm
,
420 vec_type
, array_size
, "");
422 bld
->temps
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
426 case TGSI_FILE_OUTPUT
:
427 bld
->outputs
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
430 case TGSI_FILE_ADDRESS
:
431 assert(idx
< LP_MAX_TGSI_ADDRS
);
432 bld
->addr
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
435 case TGSI_FILE_PREDICATE
:
436 assert(idx
< LP_MAX_TGSI_PREDS
);
437 bld
->preds
[idx
] = lp_build_alloca(gallivm
, vec_type
, "");
441 /* don't need to declare other vars */
449 * Emit LLVM for one TGSI instruction.
450 * \param return TRUE for success, FALSE otherwise
453 lp_emit_instruction_aos(
454 struct lp_build_tgsi_aos_context
*bld
,
455 const struct tgsi_full_instruction
*inst
,
456 const struct tgsi_opcode_info
*info
,
459 LLVMValueRef src0
, src1
, src2
;
460 LLVMValueRef tmp0
, tmp1
;
461 LLVMValueRef dst0
= NULL
;
464 * Stores and write masks are handled in a general fashion after the long
465 * instruction opcode switch statement.
467 * Although not stricitly necessary, we avoid generating instructions for
468 * channels which won't be stored, in cases where's that easy. For some
469 * complex instructions, like texture sampling, it is more convenient to
470 * assume a full writemask and then let LLVM optimization passes eliminate
476 assert(info
->num_dst
<= 1);
478 dst0
= bld
->bld_base
.base
.undef
;
481 switch (inst
->Instruction
.Opcode
) {
482 case TGSI_OPCODE_ARL
:
483 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
484 dst0
= lp_build_floor(&bld
->bld_base
.base
, src0
);
487 case TGSI_OPCODE_MOV
:
488 dst0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
491 case TGSI_OPCODE_LIT
:
494 case TGSI_OPCODE_RCP
:
495 /* TGSI_OPCODE_RECIP */
496 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
497 dst0
= lp_build_rcp(&bld
->bld_base
.base
, src0
);
500 case TGSI_OPCODE_RSQ
:
501 /* TGSI_OPCODE_RECIPSQRT */
502 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
503 tmp0
= lp_build_emit_llvm_unary(&bld
->bld_base
, TGSI_OPCODE_ABS
, src0
);
504 dst0
= lp_build_rsqrt(&bld
->bld_base
.base
, tmp0
);
507 case TGSI_OPCODE_EXP
:
510 case TGSI_OPCODE_LOG
:
513 case TGSI_OPCODE_MUL
:
514 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
515 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
516 dst0
= lp_build_mul(&bld
->bld_base
.base
, src0
, src1
);
519 case TGSI_OPCODE_ADD
:
520 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
521 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
522 dst0
= lp_build_add(&bld
->bld_base
.base
, src0
, src1
);
525 case TGSI_OPCODE_DP3
:
526 /* TGSI_OPCODE_DOT3 */
529 case TGSI_OPCODE_DP4
:
530 /* TGSI_OPCODE_DOT4 */
533 case TGSI_OPCODE_DST
:
536 case TGSI_OPCODE_MIN
:
537 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
538 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
539 dst0
= lp_build_min(&bld
->bld_base
.base
, src0
, src1
);
542 case TGSI_OPCODE_MAX
:
543 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
544 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
545 dst0
= lp_build_max(&bld
->bld_base
.base
, src0
, src1
);
548 case TGSI_OPCODE_SLT
:
549 /* TGSI_OPCODE_SETLT */
550 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
551 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
552 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_LESS
, src0
, src1
);
553 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, bld
->bld_base
.base
.one
, bld
->bld_base
.base
.zero
);
556 case TGSI_OPCODE_SGE
:
557 /* TGSI_OPCODE_SETGE */
558 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
559 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
560 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
561 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, bld
->bld_base
.base
.one
, bld
->bld_base
.base
.zero
);
564 case TGSI_OPCODE_MAD
:
565 /* TGSI_OPCODE_MADD */
566 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
567 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
568 src2
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 2, LP_CHAN_ALL
);
569 tmp0
= lp_build_mul(&bld
->bld_base
.base
, src0
, src1
);
570 dst0
= lp_build_add(&bld
->bld_base
.base
, tmp0
, src2
);
573 case TGSI_OPCODE_SUB
:
574 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
575 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
576 dst0
= lp_build_sub(&bld
->bld_base
.base
, src0
, src1
);
579 case TGSI_OPCODE_LRP
:
580 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
581 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
582 src2
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 2, LP_CHAN_ALL
);
583 tmp0
= lp_build_sub(&bld
->bld_base
.base
, src1
, src2
);
584 tmp0
= lp_build_mul(&bld
->bld_base
.base
, src0
, tmp0
);
585 dst0
= lp_build_add(&bld
->bld_base
.base
, tmp0
, src2
);
588 case TGSI_OPCODE_CND
:
589 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
590 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
591 src2
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 2, LP_CHAN_ALL
);
592 tmp1
= lp_build_const_vec(bld
->bld_base
.base
.gallivm
, bld
->bld_base
.base
.type
, 0.5);
593 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
594 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, src0
, src1
);
597 case TGSI_OPCODE_DP2A
:
600 case TGSI_OPCODE_FRC
:
601 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
602 tmp0
= lp_build_floor(&bld
->bld_base
.base
, src0
);
603 dst0
= lp_build_sub(&bld
->bld_base
.base
, src0
, tmp0
);
606 case TGSI_OPCODE_CLAMP
:
607 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
608 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
609 src2
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 2, LP_CHAN_ALL
);
610 tmp0
= lp_build_max(&bld
->bld_base
.base
, src0
, src1
);
611 dst0
= lp_build_min(&bld
->bld_base
.base
, tmp0
, src2
);
614 case TGSI_OPCODE_FLR
:
615 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
616 dst0
= lp_build_floor(&bld
->bld_base
.base
, src0
);
619 case TGSI_OPCODE_ROUND
:
620 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
621 dst0
= lp_build_round(&bld
->bld_base
.base
, src0
);
624 case TGSI_OPCODE_EX2
:
625 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
626 tmp0
= lp_build_swizzle_scalar_aos(&bld
->bld_base
.base
, src0
, TGSI_SWIZZLE_X
, TGSI_NUM_CHANNELS
);
627 dst0
= lp_build_exp2(&bld
->bld_base
.base
, tmp0
);
630 case TGSI_OPCODE_LG2
:
631 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
632 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
633 dst0
= lp_build_log2(&bld
->bld_base
.base
, tmp0
);
636 case TGSI_OPCODE_POW
:
637 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
638 src0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
639 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
640 src1
= swizzle_scalar_aos(bld
, src1
, TGSI_SWIZZLE_X
);
641 dst0
= lp_build_pow(&bld
->bld_base
.base
, src0
, src1
);
644 case TGSI_OPCODE_XPD
:
647 case TGSI_OPCODE_RCC
:
652 case TGSI_OPCODE_DPH
:
655 case TGSI_OPCODE_COS
:
656 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
657 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
658 dst0
= lp_build_cos(&bld
->bld_base
.base
, tmp0
);
661 case TGSI_OPCODE_DDX
:
664 case TGSI_OPCODE_DDY
:
667 case TGSI_OPCODE_KILP
:
668 /* predicated kill */
671 case TGSI_OPCODE_KIL
:
672 /* conditional kill */
675 case TGSI_OPCODE_PK2H
:
679 case TGSI_OPCODE_PK2US
:
683 case TGSI_OPCODE_PK4B
:
687 case TGSI_OPCODE_PK4UB
:
690 case TGSI_OPCODE_RFL
:
693 case TGSI_OPCODE_SEQ
:
694 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
695 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
696 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_EQUAL
, src0
, src1
);
697 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, bld
->bld_base
.base
.one
, bld
->bld_base
.base
.zero
);
700 case TGSI_OPCODE_SFL
:
701 dst0
= bld
->bld_base
.base
.zero
;
704 case TGSI_OPCODE_SGT
:
705 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
706 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
707 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GREATER
, src0
, src1
);
708 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, bld
->bld_base
.base
.one
, bld
->bld_base
.base
.zero
);
711 case TGSI_OPCODE_SIN
:
712 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
713 tmp0
= swizzle_scalar_aos(bld
, src0
, TGSI_SWIZZLE_X
);
714 dst0
= lp_build_sin(&bld
->bld_base
.base
, tmp0
);
717 case TGSI_OPCODE_SLE
:
718 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
719 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
720 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
721 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, bld
->bld_base
.base
.one
, bld
->bld_base
.base
.zero
);
724 case TGSI_OPCODE_SNE
:
725 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
726 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
727 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
728 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, bld
->bld_base
.base
.one
, bld
->bld_base
.base
.zero
);
731 case TGSI_OPCODE_STR
:
732 dst0
= bld
->bld_base
.base
.one
;
735 case TGSI_OPCODE_TEX
:
736 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_NONE
);
739 case TGSI_OPCODE_TXD
:
740 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
);
743 case TGSI_OPCODE_UP2H
:
749 case TGSI_OPCODE_UP2US
:
755 case TGSI_OPCODE_UP4B
:
761 case TGSI_OPCODE_UP4UB
:
767 case TGSI_OPCODE_X2D
:
773 case TGSI_OPCODE_ARA
:
779 case TGSI_OPCODE_ARR
:
780 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
781 dst0
= lp_build_round(&bld
->bld_base
.base
, src0
);
784 case TGSI_OPCODE_BRA
:
790 case TGSI_OPCODE_CAL
:
793 case TGSI_OPCODE_RET
:
796 case TGSI_OPCODE_END
:
800 case TGSI_OPCODE_SSG
:
801 /* TGSI_OPCODE_SGN */
802 tmp0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
803 dst0
= lp_build_sgn(&bld
->bld_base
.base
, tmp0
);
806 case TGSI_OPCODE_CMP
:
807 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
808 src1
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, LP_CHAN_ALL
);
809 src2
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 2, LP_CHAN_ALL
);
810 tmp0
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_LESS
, src0
, bld
->bld_base
.base
.zero
);
811 dst0
= lp_build_select(&bld
->bld_base
.base
, tmp0
, src1
, src2
);
814 case TGSI_OPCODE_SCS
:
817 case TGSI_OPCODE_TXB
:
818 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
);
821 case TGSI_OPCODE_NRM
:
823 case TGSI_OPCODE_NRM4
:
826 case TGSI_OPCODE_DIV
:
832 case TGSI_OPCODE_DP2
:
835 case TGSI_OPCODE_TXL
:
836 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
);
839 case TGSI_OPCODE_TXP
:
840 dst0
= emit_tex(bld
, inst
, LP_BLD_TEX_MODIFIER_PROJECTED
);
843 case TGSI_OPCODE_BRK
:
849 case TGSI_OPCODE_BGNLOOP
:
852 case TGSI_OPCODE_BGNSUB
:
855 case TGSI_OPCODE_ELSE
:
858 case TGSI_OPCODE_ENDIF
:
861 case TGSI_OPCODE_ENDLOOP
:
864 case TGSI_OPCODE_ENDSUB
:
867 case TGSI_OPCODE_PUSHA
:
873 case TGSI_OPCODE_POPA
:
879 case TGSI_OPCODE_CEIL
:
880 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
881 dst0
= lp_build_ceil(&bld
->bld_base
.base
, src0
);
884 case TGSI_OPCODE_I2F
:
890 case TGSI_OPCODE_NOT
:
896 case TGSI_OPCODE_TRUNC
:
897 src0
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, LP_CHAN_ALL
);
898 dst0
= lp_build_trunc(&bld
->bld_base
.base
, src0
);
901 case TGSI_OPCODE_SHL
:
907 case TGSI_OPCODE_ISHR
:
913 case TGSI_OPCODE_AND
:
925 case TGSI_OPCODE_MOD
:
931 case TGSI_OPCODE_XOR
:
937 case TGSI_OPCODE_SAD
:
943 case TGSI_OPCODE_TXF
:
949 case TGSI_OPCODE_TXQ
:
955 case TGSI_OPCODE_CONT
:
958 case TGSI_OPCODE_EMIT
:
962 case TGSI_OPCODE_ENDPRIM
:
966 case TGSI_OPCODE_NOP
:
974 lp_emit_store_aos(bld
, inst
, 0, dst0
);
982 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
983 const struct tgsi_token
*tokens
,
985 const unsigned char swizzles
[4],
986 LLVMValueRef consts_ptr
,
987 const LLVMValueRef
*inputs
,
988 LLVMValueRef
*outputs
,
989 struct lp_build_sampler_aos
*sampler
,
990 const struct tgsi_shader_info
*info
)
992 struct lp_build_tgsi_aos_context bld
;
993 struct tgsi_parse_context parse
;
994 uint num_immediates
= 0;
998 /* Setup build context */
999 memset(&bld
, 0, sizeof bld
);
1000 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
1001 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
1002 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
1003 lp_build_context_init(&bld
.int_bld
, gallivm
, lp_int_type(type
));
1005 for (chan
= 0; chan
< 4; ++chan
) {
1006 bld
.swizzles
[chan
] = swizzles
[chan
];
1007 bld
.inv_swizzles
[swizzles
[chan
]] = chan
;
1010 bld
.inputs
= inputs
;
1011 bld
.outputs
= outputs
;
1012 bld
.consts_ptr
= consts_ptr
;
1013 bld
.sampler
= sampler
;
1014 bld
.indirect_files
= info
->indirect_files
;
1015 bld
.bld_base
.emit_swizzle
= swizzle_aos
;
1016 bld
.bld_base
.info
= info
;
1018 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
1019 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
1020 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
1021 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
1023 /* Set opcode actions */
1024 lp_set_default_actions_cpu(&bld
.bld_base
);
1026 if (!lp_bld_tgsi_list_init(&bld
.bld_base
)) {
1030 tgsi_parse_init(&parse
, tokens
);
1032 while (!tgsi_parse_end_of_tokens(&parse
)) {
1033 tgsi_parse_token(&parse
);
1035 switch(parse
.FullToken
.Token
.Type
) {
1036 case TGSI_TOKEN_TYPE_DECLARATION
:
1037 /* Inputs already interpolated */
1038 lp_emit_declaration_aos(&bld
, &parse
.FullToken
.FullDeclaration
);
1041 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1042 /* save expanded instruction */
1043 lp_bld_tgsi_add_instruction(&bld
.bld_base
,
1044 &parse
.FullToken
.FullInstruction
);
1047 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1048 /* simply copy the immediate values into the next immediates[] slot */
1050 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
1053 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
1054 for (chan
= 0; chan
< 4; ++chan
) {
1057 for (chan
= 0; chan
< size
; ++chan
) {
1058 unsigned swizzle
= bld
.swizzles
[chan
];
1059 imm
[swizzle
] = parse
.FullToken
.FullImmediate
.u
[chan
].Float
;
1061 bld
.immediates
[num_immediates
] =
1062 lp_build_const_aos(gallivm
, type
,
1063 imm
[0], imm
[1], imm
[2], imm
[3],
1069 case TGSI_TOKEN_TYPE_PROPERTY
:
1078 struct tgsi_full_instruction
*instr
= bld
.bld_base
.instructions
+ pc
;
1079 const struct tgsi_opcode_info
*opcode_info
=
1080 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
1081 if (!lp_emit_instruction_aos(&bld
, instr
, opcode_info
, &pc
))
1082 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1083 opcode_info
->mnemonic
);
1087 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
1088 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
1089 debug_printf("11111111111111111111111111111 \n");
1090 tgsi_dump(tokens
, 0);
1091 lp_debug_dump_value(function
);
1092 debug_printf("2222222222222222222222222222 \n");
1094 tgsi_parse_free(&parse
);
1095 FREE(bld
.bld_base
.instructions
);
1098 LLVMModuleRef module
= LLVMGetGlobalParent(
1099 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
1100 LLVMDumpModule(module
);