1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * TGSI to LLVM IR translation -- SoA.
33 * @author Jose Fonseca <jfonseca@vmware.com>
35 * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
36 * Brian Paul, and others.
39 #include "pipe/p_config.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "util/u_debug.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "tgsi/tgsi_dump.h"
45 #include "tgsi/tgsi_info.h"
46 #include "tgsi/tgsi_parse.h"
47 #include "tgsi/tgsi_util.h"
48 #include "tgsi/tgsi_scan.h"
49 #include "lp_bld_type.h"
50 #include "lp_bld_const.h"
51 #include "lp_bld_arit.h"
52 #include "lp_bld_gather.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 FOR_EACH_CHANNEL( CHAN )\
63 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
65 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
66 ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
68 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
69 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
71 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
72 FOR_EACH_CHANNEL( CHAN )\
73 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
79 #define NUM_CHANNELS 4
81 #define LP_MAX_INSTRUCTIONS 256
85 struct lp_build_context
*bld
;
89 LLVMTypeRef int_vec_type
;
91 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
93 LLVMValueRef cond_mask
;
95 LLVMBasicBlockRef loop_block
;
96 LLVMValueRef cont_mask
;
97 LLVMValueRef break_mask
;
98 LLVMValueRef break_var
;
100 LLVMBasicBlockRef loop_block
;
101 LLVMValueRef cont_mask
;
102 LLVMValueRef break_mask
;
103 LLVMValueRef break_var
;
104 } loop_stack
[LP_MAX_TGSI_NESTING
];
107 LLVMValueRef ret_mask
;
110 LLVMValueRef ret_mask
;
111 } call_stack
[LP_MAX_TGSI_NESTING
];
114 LLVMValueRef exec_mask
;
117 struct lp_build_tgsi_soa_context
119 struct lp_build_context base
;
121 /* Builder for integer masks and indices */
122 struct lp_build_context int_bld
;
124 LLVMValueRef consts_ptr
;
125 const LLVMValueRef
*pos
;
126 const LLVMValueRef (*inputs
)[NUM_CHANNELS
];
127 LLVMValueRef (*outputs
)[NUM_CHANNELS
];
129 const struct lp_build_sampler_soa
*sampler
;
131 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][NUM_CHANNELS
];
132 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][NUM_CHANNELS
];
133 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][NUM_CHANNELS
];
134 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][NUM_CHANNELS
];
136 /* we allocate an array of temps if we have indirect
137 * addressing and then the temps above is unused */
138 LLVMValueRef temps_array
;
139 boolean has_indirect_addressing
;
141 struct lp_build_mask_context
*mask
;
142 struct lp_exec_mask exec_mask
;
144 struct tgsi_full_instruction
*instructions
;
145 uint max_instructions
;
148 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
151 mask
->has_mask
= FALSE
;
152 mask
->cond_stack_size
= 0;
153 mask
->loop_stack_size
= 0;
154 mask
->call_stack_size
= 0;
156 mask
->int_vec_type
= lp_build_int_vec_type(mask
->bld
->type
);
157 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
158 LLVMConstAllOnes(mask
->int_vec_type
);
161 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
163 if (mask
->loop_stack_size
) {
164 /*for loops we need to update the entire mask at runtime */
166 assert(mask
->break_mask
);
167 tmp
= LLVMBuildAnd(mask
->bld
->builder
,
171 mask
->exec_mask
= LLVMBuildAnd(mask
->bld
->builder
,
176 mask
->exec_mask
= mask
->cond_mask
;
178 if (mask
->call_stack_size
) {
179 mask
->exec_mask
= LLVMBuildAnd(mask
->bld
->builder
,
185 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
186 mask
->loop_stack_size
> 0 ||
187 mask
->call_stack_size
> 0);
190 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
193 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
194 if (mask
->cond_stack_size
== 0) {
195 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
197 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
198 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
199 mask
->cond_mask
= val
;
201 lp_exec_mask_update(mask
);
204 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
206 LLVMValueRef prev_mask
;
207 LLVMValueRef inv_mask
;
209 assert(mask
->cond_stack_size
);
210 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
211 if (mask
->cond_stack_size
== 1) {
212 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
215 inv_mask
= LLVMBuildNot(mask
->bld
->builder
, mask
->cond_mask
, "");
217 mask
->cond_mask
= LLVMBuildAnd(mask
->bld
->builder
,
220 lp_exec_mask_update(mask
);
223 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
225 assert(mask
->cond_stack_size
);
226 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
227 lp_exec_mask_update(mask
);
230 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
232 if (mask
->loop_stack_size
== 0) {
233 assert(mask
->loop_block
== NULL
);
234 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
235 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
236 assert(mask
->break_var
== NULL
);
239 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
241 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
242 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
243 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
244 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
245 ++mask
->loop_stack_size
;
247 mask
->break_var
= lp_build_alloca(mask
->bld
->builder
, mask
->int_vec_type
, "");
248 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
250 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->builder
, "bgnloop");
251 LLVMBuildBr(mask
->bld
->builder
, mask
->loop_block
);
252 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, mask
->loop_block
);
254 mask
->break_mask
= LLVMBuildLoad(mask
->bld
->builder
, mask
->break_var
, "");
256 lp_exec_mask_update(mask
);
259 static void lp_exec_break(struct lp_exec_mask
*mask
)
261 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
265 mask
->break_mask
= LLVMBuildAnd(mask
->bld
->builder
,
267 exec_mask
, "break_full");
269 lp_exec_mask_update(mask
);
272 static void lp_exec_continue(struct lp_exec_mask
*mask
)
274 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
278 mask
->cont_mask
= LLVMBuildAnd(mask
->bld
->builder
,
282 lp_exec_mask_update(mask
);
286 static void lp_exec_endloop(struct lp_exec_mask
*mask
)
288 LLVMBasicBlockRef endloop
;
289 LLVMTypeRef reg_type
= LLVMIntType(mask
->bld
->type
.width
*
290 mask
->bld
->type
.length
);
293 assert(mask
->break_mask
);
296 * Restore the cont_mask, but don't pop
298 assert(mask
->loop_stack_size
);
299 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
300 lp_exec_mask_update(mask
);
303 * Unlike the continue mask, the break_mask must be preserved across loop
306 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
308 /* i1cond = (mask == 0) */
309 i1cond
= LLVMBuildICmp(
312 LLVMBuildBitCast(mask
->bld
->builder
, mask
->exec_mask
, reg_type
, ""),
313 LLVMConstNull(reg_type
), "");
315 endloop
= lp_build_insert_new_block(mask
->bld
->builder
, "endloop");
317 LLVMBuildCondBr(mask
->bld
->builder
,
318 i1cond
, mask
->loop_block
, endloop
);
320 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, endloop
);
322 assert(mask
->loop_stack_size
);
323 --mask
->loop_stack_size
;
324 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
325 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
326 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
327 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
329 lp_exec_mask_update(mask
);
332 /* stores val into an address pointed to by dst.
333 * mask->exec_mask is used to figure out which bits of val
334 * should be stored into the address
335 * (0 means don't store this bit, 1 means do store).
337 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
342 /* Mix the predicate and execution mask */
343 if (mask
->has_mask
) {
345 pred
= LLVMBuildAnd(mask
->bld
->builder
, pred
, mask
->exec_mask
, "");
347 pred
= mask
->exec_mask
;
352 LLVMValueRef real_val
, dst_val
;
354 dst_val
= LLVMBuildLoad(mask
->bld
->builder
, dst
, "");
355 real_val
= lp_build_select(mask
->bld
,
359 LLVMBuildStore(mask
->bld
->builder
, real_val
, dst
);
361 LLVMBuildStore(mask
->bld
->builder
, val
, dst
);
364 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
368 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
369 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
370 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
371 mask
->call_stack_size
++;
375 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
377 LLVMValueRef exec_mask
;
379 if (mask
->call_stack_size
== 0) {
380 /* returning from main() */
384 exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
388 mask
->ret_mask
= LLVMBuildAnd(mask
->bld
->builder
,
390 exec_mask
, "ret_full");
392 lp_exec_mask_update(mask
);
395 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
399 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
401 assert(mask
->call_stack_size
);
402 mask
->call_stack_size
--;
403 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
404 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
405 lp_exec_mask_update(mask
);
410 * Return pointer to a temporary register channel (src or dest).
411 * Note that indirect addressing cannot be handled here.
412 * \param index which temporary register
413 * \param chan which channel of the temp register.
416 get_temp_ptr(struct lp_build_tgsi_soa_context
*bld
,
421 if (bld
->has_indirect_addressing
) {
422 LLVMValueRef lindex
= lp_build_const_int32(index
* 4 + chan
);
423 return LLVMBuildGEP(bld
->base
.builder
, bld
->temps_array
, &lindex
, 1, "");
426 return bld
->temps
[index
][chan
];
433 * XXX the lp_build_gather() function should be capable of doing this
434 * with a little work.
437 build_gather(struct lp_build_tgsi_soa_context
*bld
,
438 LLVMValueRef base_ptr
,
439 LLVMValueRef indexes
)
441 LLVMValueRef res
= bld
->base
.undef
;
445 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
447 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
448 LLVMValueRef ii
= LLVMConstInt(LLVMInt32Type(), i
, 0);
449 LLVMValueRef index
= LLVMBuildExtractElement(bld
->base
.builder
,
451 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, base_ptr
,
453 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
455 res
= LLVMBuildInsertElement(bld
->base
.builder
, res
, scalar
, ii
, "");
467 struct lp_build_tgsi_soa_context
*bld
,
468 const struct tgsi_full_instruction
*inst
,
470 const unsigned chan_index
)
472 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
473 const unsigned swizzle
=
474 tgsi_util_get_full_src_register_swizzle(reg
, chan_index
);
476 LLVMValueRef addr_vec
= NULL
;
479 assert(0 && "invalid swizzle in emit_fetch()");
480 return bld
->base
.undef
;
483 if (reg
->Register
.Indirect
) {
485 * Compute addr_vec: a vector of offsets into the register file
486 * from which we need to gather elements. Recall that the ADDR
487 * register's elements can all be different.
490 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
491 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
493 LLVMValueRef vec4
= lp_build_const_int_vec(bld
->int_bld
.type
, 4);
495 assert(bld
->has_indirect_addressing
);
497 addr_vec
= LLVMBuildLoad(bld
->base
.builder
,
498 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
501 /* for indexing we want integers */
502 addr_vec
= LLVMBuildFPToSI(bld
->base
.builder
, addr_vec
,
505 /* addr_vec = addr_vec * 4 */
506 addr_vec
= lp_build_mul(&bld
->base
, addr_vec
, vec4
);
509 switch (reg
->Register
.File
) {
510 case TGSI_FILE_CONSTANT
:
511 if (reg
->Register
.Indirect
) {
512 LLVMValueRef index_vec
; /* index into the const buffer */
514 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
515 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
516 reg
->Register
.Index
* 4 + swizzle
);
518 /* index_vec = index_vec + addr_vec */
519 index_vec
= lp_build_add(&bld
->base
, index_vec
, addr_vec
);
521 /* Gather values from the constant buffer */
522 res
= build_gather(bld
, bld
->consts_ptr
, index_vec
);
525 LLVMValueRef index
; /* index into the const buffer */
526 LLVMValueRef scalar
, scalar_ptr
;
528 index
= lp_build_const_int32(reg
->Register
.Index
*4 + swizzle
);
530 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
532 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
534 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
538 case TGSI_FILE_IMMEDIATE
:
539 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
543 case TGSI_FILE_INPUT
:
544 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
548 case TGSI_FILE_TEMPORARY
:
549 if (reg
->Register
.Indirect
) {
550 LLVMValueRef vec_len
=
551 lp_build_const_int_vec(bld
->int_bld
.type
, bld
->base
.type
.length
);
552 LLVMValueRef index_vec
; /* index into the const buffer */
553 LLVMValueRef temps_array
;
554 LLVMTypeRef float4_ptr_type
;
556 assert(bld
->has_indirect_addressing
);
558 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
559 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
560 reg
->Register
.Index
* 4 + swizzle
);
562 /* index_vec += addr_vec */
563 index_vec
= lp_build_add(&bld
->int_bld
, index_vec
, addr_vec
);
565 /* index_vec *= vector_length */
566 index_vec
= lp_build_mul(&bld
->int_bld
, index_vec
, vec_len
);
568 /* cast temps_array pointer to float* */
569 float4_ptr_type
= LLVMPointerType(LLVMFloatType(), 0);
570 temps_array
= LLVMBuildBitCast(bld
->int_bld
.builder
, bld
->temps_array
,
571 float4_ptr_type
, "");
573 /* Gather values from the temporary register array */
574 res
= build_gather(bld
, temps_array
, index_vec
);
577 LLVMValueRef temp_ptr
;
578 temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
, swizzle
);
579 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
581 return bld
->base
.undef
;
586 assert(0 && "invalid src register in emit_fetch()");
587 return bld
->base
.undef
;
590 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
591 case TGSI_UTIL_SIGN_CLEAR
:
592 res
= lp_build_abs( &bld
->base
, res
);
595 case TGSI_UTIL_SIGN_SET
:
596 /* TODO: Use bitwese OR for floating point */
597 res
= lp_build_abs( &bld
->base
, res
);
598 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
601 case TGSI_UTIL_SIGN_TOGGLE
:
602 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
605 case TGSI_UTIL_SIGN_KEEP
:
614 * Register fetch with derivatives.
618 struct lp_build_tgsi_soa_context
*bld
,
619 const struct tgsi_full_instruction
*inst
,
621 const unsigned chan_index
,
628 src
= emit_fetch(bld
, inst
, index
, chan_index
);
633 /* TODO: use interpolation coeffs for inputs */
636 *ddx
= lp_build_ddx(&bld
->base
, src
);
639 *ddy
= lp_build_ddy(&bld
->base
, src
);
647 emit_fetch_predicate(
648 struct lp_build_tgsi_soa_context
*bld
,
649 const struct tgsi_full_instruction
*inst
,
653 unsigned char swizzles
[4];
654 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
658 if (!inst
->Instruction
.Predicate
) {
659 FOR_EACH_CHANNEL( chan
) {
665 swizzles
[0] = inst
->Predicate
.SwizzleX
;
666 swizzles
[1] = inst
->Predicate
.SwizzleY
;
667 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
668 swizzles
[3] = inst
->Predicate
.SwizzleW
;
670 index
= inst
->Predicate
.Index
;
671 assert(index
< LP_MAX_TGSI_PREDS
);
673 FOR_EACH_CHANNEL( chan
) {
674 unsigned swizzle
= swizzles
[chan
];
677 * Only fetch the predicate register channels that are actually listed
680 if (!unswizzled
[swizzle
]) {
681 value
= LLVMBuildLoad(bld
->base
.builder
,
682 bld
->preds
[index
][swizzle
], "");
685 * Convert the value to an integer mask.
687 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
688 * is needlessly causing two comparisons due to storing the intermediate
689 * result as float vector instead of an integer mask vector.
691 value
= lp_build_compare(bld
->base
.builder
,
696 if (inst
->Predicate
.Negate
) {
697 value
= LLVMBuildNot(bld
->base
.builder
, value
, "");
700 unswizzled
[swizzle
] = value
;
702 value
= unswizzled
[swizzle
];
715 struct lp_build_tgsi_soa_context
*bld
,
716 const struct tgsi_full_instruction
*inst
,
722 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
723 LLVMValueRef addr
= NULL
;
725 switch( inst
->Instruction
.Saturate
) {
729 case TGSI_SAT_ZERO_ONE
:
730 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
731 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
734 case TGSI_SAT_MINUS_PLUS_ONE
:
735 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
736 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
743 if (reg
->Register
.Indirect
) {
744 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
745 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
746 addr
= LLVMBuildLoad(bld
->base
.builder
,
747 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
749 /* for indexing we want integers */
750 addr
= LLVMBuildFPToSI(bld
->base
.builder
, addr
,
752 addr
= LLVMBuildExtractElement(bld
->base
.builder
,
753 addr
, LLVMConstInt(LLVMInt32Type(), 0, 0),
755 addr
= lp_build_mul(&bld
->base
, addr
, LLVMConstInt(LLVMInt32Type(), 4, 0));
758 switch( reg
->Register
.File
) {
759 case TGSI_FILE_OUTPUT
:
760 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
761 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
764 case TGSI_FILE_TEMPORARY
:
765 if (reg
->Register
.Indirect
) {
766 /* XXX not done yet */
767 debug_printf("WARNING: LLVM scatter store of temp regs"
768 " not implemented\n");
771 LLVMValueRef temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
773 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, temp_ptr
);
777 case TGSI_FILE_ADDRESS
:
778 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
779 bld
->addr
[reg
->Indirect
.Index
][chan_index
]);
782 case TGSI_FILE_PREDICATE
:
783 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
784 bld
->preds
[index
][chan_index
]);
794 * High-level instruction translators.
798 TEX_MODIFIER_NONE
= 0,
799 TEX_MODIFIER_PROJECTED
,
800 TEX_MODIFIER_LOD_BIAS
,
801 TEX_MODIFIER_EXPLICIT_LOD
,
802 TEX_MODIFIER_EXPLICIT_DERIV
806 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
807 const struct tgsi_full_instruction
*inst
,
808 enum tex_modifier modifier
,
812 LLVMValueRef lod_bias
, explicit_lod
;
813 LLVMValueRef oow
= NULL
;
814 LLVMValueRef coords
[3];
821 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
822 for (i
= 0; i
< 4; i
++) {
823 texel
[i
] = bld
->base
.undef
;
828 switch (inst
->Texture
.Texture
) {
829 case TGSI_TEXTURE_1D
:
832 case TGSI_TEXTURE_2D
:
833 case TGSI_TEXTURE_RECT
:
836 case TGSI_TEXTURE_SHADOW1D
:
837 case TGSI_TEXTURE_SHADOW2D
:
838 case TGSI_TEXTURE_SHADOWRECT
:
839 case TGSI_TEXTURE_3D
:
840 case TGSI_TEXTURE_CUBE
:
848 if (modifier
== TEX_MODIFIER_LOD_BIAS
) {
849 lod_bias
= emit_fetch( bld
, inst
, 0, 3 );
852 else if (modifier
== TEX_MODIFIER_EXPLICIT_LOD
) {
854 explicit_lod
= emit_fetch( bld
, inst
, 0, 3 );
861 if (modifier
== TEX_MODIFIER_PROJECTED
) {
862 oow
= emit_fetch( bld
, inst
, 0, 3 );
863 oow
= lp_build_rcp(&bld
->base
, oow
);
866 for (i
= 0; i
< num_coords
; i
++) {
867 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
868 if (modifier
== TEX_MODIFIER_PROJECTED
)
869 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
871 for (i
= num_coords
; i
< 3; i
++) {
872 coords
[i
] = bld
->base
.undef
;
875 if (modifier
== TEX_MODIFIER_EXPLICIT_DERIV
) {
876 for (i
= 0; i
< num_coords
; i
++) {
877 ddx
[i
] = emit_fetch( bld
, inst
, 1, i
);
878 ddy
[i
] = emit_fetch( bld
, inst
, 2, i
);
880 unit
= inst
->Src
[3].Register
.Index
;
882 for (i
= 0; i
< num_coords
; i
++) {
883 ddx
[i
] = lp_build_ddx( &bld
->base
, coords
[i
] );
884 ddy
[i
] = lp_build_ddy( &bld
->base
, coords
[i
] );
886 unit
= inst
->Src
[1].Register
.Index
;
888 for (i
= num_coords
; i
< 3; i
++) {
889 ddx
[i
] = bld
->base
.undef
;
890 ddy
[i
] = bld
->base
.undef
;
893 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
896 unit
, num_coords
, coords
,
898 lod_bias
, explicit_lod
,
904 * Kill fragment if any of the src register values are negative.
908 struct lp_build_tgsi_soa_context
*bld
,
909 const struct tgsi_full_instruction
*inst
)
911 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
912 LLVMValueRef terms
[NUM_CHANNELS
];
916 memset(&terms
, 0, sizeof terms
);
918 FOR_EACH_CHANNEL( chan_index
) {
921 /* Unswizzle channel */
922 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
924 /* Check if the component has not been already tested. */
925 assert(swizzle
< NUM_CHANNELS
);
926 if( !terms
[swizzle
] )
927 /* TODO: change the comparison operator instead of setting the sign */
928 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
932 FOR_EACH_CHANNEL( chan_index
) {
933 if(terms
[chan_index
]) {
934 LLVMValueRef chan_mask
;
937 * If term < 0 then mask = 0 else mask = ~0.
939 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
942 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
949 lp_build_mask_update(bld
->mask
, mask
);
954 * Predicated fragment kill.
955 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
956 * The only predication is the execution mask which will apply if
957 * we're inside a loop or conditional.
960 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
961 const struct tgsi_full_instruction
*inst
)
965 /* For those channels which are "alive", disable fragment shader
968 if (bld
->exec_mask
.has_mask
) {
969 mask
= LLVMBuildNot(bld
->base
.builder
, bld
->exec_mask
.exec_mask
, "kilp");
972 mask
= bld
->base
.zero
;
975 lp_build_mask_update(bld
->mask
, mask
);
980 struct lp_build_tgsi_soa_context
*bld
,
981 const struct tgsi_full_declaration
*decl
)
983 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
985 unsigned first
= decl
->Range
.First
;
986 unsigned last
= decl
->Range
.Last
;
989 for (idx
= first
; idx
<= last
; ++idx
) {
990 switch (decl
->Declaration
.File
) {
991 case TGSI_FILE_TEMPORARY
:
992 assert(idx
< LP_MAX_TGSI_TEMPS
);
993 if (bld
->has_indirect_addressing
) {
994 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
996 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
997 vec_type
, array_size
, "");
999 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1000 bld
->temps
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1005 case TGSI_FILE_OUTPUT
:
1006 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1007 bld
->outputs
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1011 case TGSI_FILE_ADDRESS
:
1012 assert(idx
< LP_MAX_TGSI_ADDRS
);
1013 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1014 bld
->addr
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1018 case TGSI_FILE_PREDICATE
:
1019 assert(idx
< LP_MAX_TGSI_PREDS
);
1020 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1021 bld
->preds
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1026 /* don't need to declare other vars */
1034 * Emit LLVM for one TGSI instruction.
1035 * \param return TRUE for success, FALSE otherwise
1039 struct lp_build_tgsi_soa_context
*bld
,
1040 const struct tgsi_full_instruction
*inst
,
1041 const struct tgsi_opcode_info
*info
,
1044 unsigned chan_index
;
1045 LLVMValueRef src0
, src1
, src2
;
1046 LLVMValueRef tmp0
, tmp1
, tmp2
;
1047 LLVMValueRef tmp3
= NULL
;
1048 LLVMValueRef tmp4
= NULL
;
1049 LLVMValueRef tmp5
= NULL
;
1050 LLVMValueRef tmp6
= NULL
;
1051 LLVMValueRef tmp7
= NULL
;
1053 LLVMValueRef dst0
[NUM_CHANNELS
];
1056 * Stores and write masks are handled in a general fashion after the long
1057 * instruction opcode switch statement.
1059 * Although not stricitly necessary, we avoid generating instructions for
1060 * channels which won't be stored, in cases where's that easy. For some
1061 * complex instructions, like texture sampling, it is more convenient to
1062 * assume a full writemask and then let LLVM optimization passes eliminate
1068 assert(info
->num_dst
<= 1);
1069 if (info
->num_dst
) {
1070 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1071 dst0
[chan_index
] = bld
->base
.undef
;
1075 switch (inst
->Instruction
.Opcode
) {
1076 case TGSI_OPCODE_ARL
:
1077 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1078 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1079 tmp0
= lp_build_floor(&bld
->base
, tmp0
);
1080 dst0
[chan_index
] = tmp0
;
1084 case TGSI_OPCODE_MOV
:
1085 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1086 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
1090 case TGSI_OPCODE_LIT
:
1091 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
1092 dst0
[CHAN_X
] = bld
->base
.one
;
1094 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1095 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1096 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
1098 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1099 /* XMM[1] = SrcReg[0].yyyy */
1100 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1101 /* XMM[1] = max(XMM[1], 0) */
1102 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
1103 /* XMM[2] = SrcReg[0].wwww */
1104 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1105 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
1106 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1107 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
1108 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
1110 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
1111 dst0
[CHAN_W
] = bld
->base
.one
;
1115 case TGSI_OPCODE_RCP
:
1116 /* TGSI_OPCODE_RECIP */
1117 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1118 res
= lp_build_rcp(&bld
->base
, src0
);
1119 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1120 dst0
[chan_index
] = res
;
1124 case TGSI_OPCODE_RSQ
:
1125 /* TGSI_OPCODE_RECIPSQRT */
1126 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1127 src0
= lp_build_abs(&bld
->base
, src0
);
1128 res
= lp_build_rsqrt(&bld
->base
, src0
);
1129 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1130 dst0
[chan_index
] = res
;
1134 case TGSI_OPCODE_EXP
:
1135 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1136 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1137 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1138 LLVMValueRef
*p_exp2_int_part
= NULL
;
1139 LLVMValueRef
*p_frac_part
= NULL
;
1140 LLVMValueRef
*p_exp2
= NULL
;
1142 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1144 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1145 p_exp2_int_part
= &tmp0
;
1146 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1147 p_frac_part
= &tmp1
;
1148 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1151 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
1153 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1154 dst0
[CHAN_X
] = tmp0
;
1155 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1156 dst0
[CHAN_Y
] = tmp1
;
1157 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1158 dst0
[CHAN_Z
] = tmp2
;
1161 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1162 dst0
[CHAN_W
] = bld
->base
.one
;
1166 case TGSI_OPCODE_LOG
:
1167 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1168 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1169 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1170 LLVMValueRef
*p_floor_log2
= NULL
;
1171 LLVMValueRef
*p_exp
= NULL
;
1172 LLVMValueRef
*p_log2
= NULL
;
1174 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1175 src0
= lp_build_abs( &bld
->base
, src0
);
1177 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1178 p_floor_log2
= &tmp0
;
1179 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1181 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1184 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
1186 /* dst.x = floor(lg2(abs(src.x))) */
1187 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1188 dst0
[CHAN_X
] = tmp0
;
1189 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1190 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
1191 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
1193 /* dst.z = lg2(abs(src.x)) */
1194 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1195 dst0
[CHAN_Z
] = tmp2
;
1198 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1199 dst0
[CHAN_W
] = bld
->base
.one
;
1203 case TGSI_OPCODE_MUL
:
1204 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1205 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1206 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1207 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
1211 case TGSI_OPCODE_ADD
:
1212 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1213 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1214 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1215 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
1219 case TGSI_OPCODE_DP3
:
1220 /* TGSI_OPCODE_DOT3 */
1221 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1222 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1223 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1224 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1225 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1226 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1227 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1228 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1229 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1230 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1231 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1232 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1233 dst0
[chan_index
] = tmp0
;
1237 case TGSI_OPCODE_DP4
:
1238 /* TGSI_OPCODE_DOT4 */
1239 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1240 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1241 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1242 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1243 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1244 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1245 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1246 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1247 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1248 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1249 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1250 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1251 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1252 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1253 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1254 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1255 dst0
[chan_index
] = tmp0
;
1259 case TGSI_OPCODE_DST
:
1260 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1261 dst0
[CHAN_X
] = bld
->base
.one
;
1263 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1264 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1265 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1266 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1268 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1269 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
1271 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1272 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
1276 case TGSI_OPCODE_MIN
:
1277 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1278 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1279 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1280 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
1284 case TGSI_OPCODE_MAX
:
1285 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1286 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1287 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1288 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
1292 case TGSI_OPCODE_SLT
:
1293 /* TGSI_OPCODE_SETLT */
1294 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1295 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1296 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1297 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
1298 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1302 case TGSI_OPCODE_SGE
:
1303 /* TGSI_OPCODE_SETGE */
1304 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1305 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1306 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1307 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1308 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1312 case TGSI_OPCODE_MAD
:
1313 /* TGSI_OPCODE_MADD */
1314 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1315 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1316 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1317 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1318 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1319 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1320 dst0
[chan_index
] = tmp0
;
1324 case TGSI_OPCODE_SUB
:
1325 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1326 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1327 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1328 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1332 case TGSI_OPCODE_LRP
:
1333 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1334 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1335 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1336 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1337 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1338 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1339 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1343 case TGSI_OPCODE_CND
:
1344 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1345 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1346 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1347 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1348 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
1349 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1350 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1354 case TGSI_OPCODE_DP2A
:
1355 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1356 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1357 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1358 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1359 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1360 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1361 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1362 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1363 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1364 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1365 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1369 case TGSI_OPCODE_FRC
:
1370 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1371 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1372 tmp0
= lp_build_floor(&bld
->base
, src0
);
1373 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1374 dst0
[chan_index
] = tmp0
;
1378 case TGSI_OPCODE_CLAMP
:
1379 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1380 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1381 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1382 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1383 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1384 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1385 dst0
[chan_index
] = tmp0
;
1389 case TGSI_OPCODE_FLR
:
1390 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1391 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1392 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1396 case TGSI_OPCODE_ROUND
:
1397 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1398 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1399 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1403 case TGSI_OPCODE_EX2
: {
1404 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1405 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1406 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1407 dst0
[chan_index
] = tmp0
;
1412 case TGSI_OPCODE_LG2
:
1413 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1414 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1415 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1416 dst0
[chan_index
] = tmp0
;
1420 case TGSI_OPCODE_POW
:
1421 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1422 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1423 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1424 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1425 dst0
[chan_index
] = res
;
1429 case TGSI_OPCODE_XPD
:
1430 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1431 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1432 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1433 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1435 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1436 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1437 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1438 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1440 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1442 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1444 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1445 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1446 dst0
[CHAN_X
] = tmp2
;
1448 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1449 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1450 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1451 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1453 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1454 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1455 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1456 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1457 dst0
[CHAN_Y
] = tmp3
;
1459 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1460 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1461 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1462 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1463 dst0
[CHAN_Z
] = tmp5
;
1465 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1466 dst0
[CHAN_W
] = bld
->base
.one
;
1470 case TGSI_OPCODE_ABS
:
1471 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1472 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1473 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1477 case TGSI_OPCODE_RCC
:
1482 case TGSI_OPCODE_DPH
:
1483 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1484 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1485 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1486 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1487 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1488 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1489 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1490 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1491 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1492 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1493 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1494 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1495 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1496 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1497 dst0
[chan_index
] = tmp0
;
1501 case TGSI_OPCODE_COS
:
1502 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1503 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1504 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1505 dst0
[chan_index
] = tmp0
;
1509 case TGSI_OPCODE_DDX
:
1510 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1511 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1515 case TGSI_OPCODE_DDY
:
1516 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1517 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1521 case TGSI_OPCODE_KILP
:
1522 /* predicated kill */
1523 emit_kilp( bld
, inst
);
1526 case TGSI_OPCODE_KIL
:
1527 /* conditional kill */
1528 emit_kil( bld
, inst
);
1531 case TGSI_OPCODE_PK2H
:
1535 case TGSI_OPCODE_PK2US
:
1539 case TGSI_OPCODE_PK4B
:
1543 case TGSI_OPCODE_PK4UB
:
1547 case TGSI_OPCODE_RFL
:
1551 case TGSI_OPCODE_SEQ
:
1552 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1553 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1554 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1555 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1556 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1560 case TGSI_OPCODE_SFL
:
1561 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1562 dst0
[chan_index
] = bld
->base
.zero
;
1566 case TGSI_OPCODE_SGT
:
1567 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1568 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1569 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1570 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1571 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1575 case TGSI_OPCODE_SIN
:
1576 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1577 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1578 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1579 dst0
[chan_index
] = tmp0
;
1583 case TGSI_OPCODE_SLE
:
1584 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1585 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1586 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1587 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1588 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1592 case TGSI_OPCODE_SNE
:
1593 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1594 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1595 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1596 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1597 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1601 case TGSI_OPCODE_STR
:
1602 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1603 dst0
[chan_index
] = bld
->base
.one
;
1607 case TGSI_OPCODE_TEX
:
1608 emit_tex( bld
, inst
, TEX_MODIFIER_NONE
, dst0
);
1611 case TGSI_OPCODE_TXD
:
1612 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_DERIV
, dst0
);
1615 case TGSI_OPCODE_UP2H
:
1621 case TGSI_OPCODE_UP2US
:
1627 case TGSI_OPCODE_UP4B
:
1633 case TGSI_OPCODE_UP4UB
:
1639 case TGSI_OPCODE_X2D
:
1645 case TGSI_OPCODE_ARA
:
1651 case TGSI_OPCODE_ARR
:
1652 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1653 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1654 tmp0
= lp_build_round(&bld
->base
, tmp0
);
1655 dst0
[chan_index
] = tmp0
;
1659 case TGSI_OPCODE_BRA
:
1665 case TGSI_OPCODE_CAL
:
1666 lp_exec_mask_call(&bld
->exec_mask
,
1672 case TGSI_OPCODE_RET
:
1673 lp_exec_mask_ret(&bld
->exec_mask
, pc
);
1676 case TGSI_OPCODE_END
:
1680 case TGSI_OPCODE_SSG
:
1681 /* TGSI_OPCODE_SGN */
1682 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1683 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1684 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1688 case TGSI_OPCODE_CMP
:
1689 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1690 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1691 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1692 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1693 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1694 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1698 case TGSI_OPCODE_SCS
:
1699 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1700 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1701 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1703 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1704 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1705 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1707 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1708 dst0
[CHAN_Z
] = bld
->base
.zero
;
1710 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1711 dst0
[CHAN_W
] = bld
->base
.one
;
1715 case TGSI_OPCODE_TXB
:
1716 emit_tex( bld
, inst
, TEX_MODIFIER_LOD_BIAS
, dst0
);
1719 case TGSI_OPCODE_NRM
:
1721 case TGSI_OPCODE_NRM4
:
1722 /* 3 or 4-component normalization */
1724 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1726 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1727 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1728 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1729 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1731 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1734 /* xmm0 = src.x * src.x */
1735 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1736 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1739 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1742 /* xmm0 = xmm0 + src.y * src.y */
1743 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1744 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1747 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1748 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1751 /* xmm0 = xmm0 + src.z * src.z */
1752 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1753 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1756 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1757 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1761 /* xmm0 = xmm0 + src.w * src.w */
1762 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1763 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1766 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1767 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1770 /* xmm1 = 1 / sqrt(xmm0) */
1771 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1773 /* dst.x = xmm1 * src.x */
1774 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1775 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1778 /* dst.y = xmm1 * src.y */
1779 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1780 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1783 /* dst.z = xmm1 * src.z */
1784 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1785 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1788 /* dst.w = xmm1 * src.w */
1789 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1790 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1795 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1796 dst0
[CHAN_W
] = bld
->base
.one
;
1801 case TGSI_OPCODE_DIV
:
1807 case TGSI_OPCODE_DP2
:
1808 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1809 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1810 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1811 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1812 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1813 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1814 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1815 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1816 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1820 case TGSI_OPCODE_TXL
:
1821 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_LOD
, dst0
);
1824 case TGSI_OPCODE_TXP
:
1825 emit_tex( bld
, inst
, TEX_MODIFIER_PROJECTED
, dst0
);
1828 case TGSI_OPCODE_BRK
:
1829 lp_exec_break(&bld
->exec_mask
);
1832 case TGSI_OPCODE_IF
:
1833 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1834 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
1835 tmp0
, bld
->base
.zero
);
1836 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1839 case TGSI_OPCODE_BGNLOOP
:
1840 lp_exec_bgnloop(&bld
->exec_mask
);
1843 case TGSI_OPCODE_BGNSUB
:
1844 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1847 case TGSI_OPCODE_ELSE
:
1848 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1851 case TGSI_OPCODE_ENDIF
:
1852 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1855 case TGSI_OPCODE_ENDLOOP
:
1856 lp_exec_endloop(&bld
->exec_mask
);
1859 case TGSI_OPCODE_ENDSUB
:
1860 lp_exec_mask_endsub(&bld
->exec_mask
, pc
);
1863 case TGSI_OPCODE_PUSHA
:
1869 case TGSI_OPCODE_POPA
:
1875 case TGSI_OPCODE_CEIL
:
1876 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1877 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1878 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1882 case TGSI_OPCODE_I2F
:
1888 case TGSI_OPCODE_NOT
:
1894 case TGSI_OPCODE_TRUNC
:
1895 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1896 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1897 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1901 case TGSI_OPCODE_SHL
:
1907 case TGSI_OPCODE_ISHR
:
1913 case TGSI_OPCODE_AND
:
1919 case TGSI_OPCODE_OR
:
1925 case TGSI_OPCODE_MOD
:
1931 case TGSI_OPCODE_XOR
:
1937 case TGSI_OPCODE_SAD
:
1943 case TGSI_OPCODE_TXF
:
1949 case TGSI_OPCODE_TXQ
:
1955 case TGSI_OPCODE_CONT
:
1956 lp_exec_continue(&bld
->exec_mask
);
1959 case TGSI_OPCODE_EMIT
:
1963 case TGSI_OPCODE_ENDPRIM
:
1967 case TGSI_OPCODE_NOP
:
1975 LLVMValueRef pred
[NUM_CHANNELS
];
1977 emit_fetch_predicate( bld
, inst
, pred
);
1979 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1980 emit_store( bld
, inst
, 0, chan_index
, pred
[chan_index
], dst0
[chan_index
]);
1989 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1990 const struct tgsi_token
*tokens
,
1991 struct lp_type type
,
1992 struct lp_build_mask_context
*mask
,
1993 LLVMValueRef consts_ptr
,
1994 const LLVMValueRef
*pos
,
1995 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
1996 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1997 struct lp_build_sampler_soa
*sampler
,
1998 const struct tgsi_shader_info
*info
)
2000 struct lp_build_tgsi_soa_context bld
;
2001 struct tgsi_parse_context parse
;
2002 uint num_immediates
= 0;
2003 uint num_instructions
= 0;
2007 /* Setup build context */
2008 memset(&bld
, 0, sizeof bld
);
2009 lp_build_context_init(&bld
.base
, builder
, type
);
2010 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
2013 bld
.inputs
= inputs
;
2014 bld
.outputs
= outputs
;
2015 bld
.consts_ptr
= consts_ptr
;
2016 bld
.sampler
= sampler
;
2017 bld
.has_indirect_addressing
= info
->opcode_count
[TGSI_OPCODE_ARR
] > 0 ||
2018 info
->opcode_count
[TGSI_OPCODE_ARL
] > 0;
2019 bld
.instructions
= (struct tgsi_full_instruction
*)
2020 MALLOC( LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
) );
2021 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
2023 if (!bld
.instructions
) {
2027 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
2029 tgsi_parse_init( &parse
, tokens
);
2031 while( !tgsi_parse_end_of_tokens( &parse
) ) {
2032 tgsi_parse_token( &parse
);
2034 switch( parse
.FullToken
.Token
.Type
) {
2035 case TGSI_TOKEN_TYPE_DECLARATION
:
2036 /* Inputs already interpolated */
2037 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
2040 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2042 /* save expanded instruction */
2043 if (num_instructions
== bld
.max_instructions
) {
2044 bld
.instructions
= REALLOC(bld
.instructions
,
2045 bld
.max_instructions
2046 * sizeof(struct tgsi_full_instruction
),
2047 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
2048 * sizeof(struct tgsi_full_instruction
));
2049 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
2052 memcpy(bld
.instructions
+ num_instructions
,
2053 &parse
.FullToken
.FullInstruction
,
2054 sizeof(bld
.instructions
[0]));
2061 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2062 /* simply copy the immediate values into the next immediates[] slot */
2064 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
2066 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
2067 for( i
= 0; i
< size
; ++i
)
2068 bld
.immediates
[num_immediates
][i
] =
2069 lp_build_const_vec(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
2070 for( i
= size
; i
< 4; ++i
)
2071 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
2076 case TGSI_TOKEN_TYPE_PROPERTY
:
2085 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
2086 const struct tgsi_opcode_info
*opcode_info
=
2087 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
2088 if (!emit_instruction( &bld
, instr
, opcode_info
, &pc
))
2089 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2090 opcode_info
->mnemonic
);
2094 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
2095 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2096 debug_printf("11111111111111111111111111111 \n");
2097 tgsi_dump(tokens
, 0);
2098 lp_debug_dump_value(function
);
2099 debug_printf("2222222222222222222222222222 \n");
2101 tgsi_parse_free( &parse
);
2104 LLVMModuleRef module
= LLVMGetGlobalParent(
2105 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
2106 LLVMDumpModule(module
);
2110 FREE( bld
.instructions
);