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/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
137 * set in the indirect_files field.
138 * The temps[] array above is unused then.
140 LLVMValueRef temps_array
;
142 /** bitmask indicating which register files are accessed indirectly */
143 unsigned indirect_files
;
145 struct lp_build_mask_context
*mask
;
146 struct lp_exec_mask exec_mask
;
148 struct tgsi_full_instruction
*instructions
;
149 uint max_instructions
;
152 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
155 mask
->has_mask
= FALSE
;
156 mask
->cond_stack_size
= 0;
157 mask
->loop_stack_size
= 0;
158 mask
->call_stack_size
= 0;
160 mask
->int_vec_type
= lp_build_int_vec_type(mask
->bld
->type
);
161 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
= mask
->cond_mask
=
162 LLVMConstAllOnes(mask
->int_vec_type
);
165 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
167 if (mask
->loop_stack_size
) {
168 /*for loops we need to update the entire mask at runtime */
170 assert(mask
->break_mask
);
171 tmp
= LLVMBuildAnd(mask
->bld
->builder
,
175 mask
->exec_mask
= LLVMBuildAnd(mask
->bld
->builder
,
180 mask
->exec_mask
= mask
->cond_mask
;
182 if (mask
->call_stack_size
) {
183 mask
->exec_mask
= LLVMBuildAnd(mask
->bld
->builder
,
189 mask
->has_mask
= (mask
->cond_stack_size
> 0 ||
190 mask
->loop_stack_size
> 0 ||
191 mask
->call_stack_size
> 0);
194 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
197 assert(mask
->cond_stack_size
< LP_MAX_TGSI_NESTING
);
198 if (mask
->cond_stack_size
== 0) {
199 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
201 mask
->cond_stack
[mask
->cond_stack_size
++] = mask
->cond_mask
;
202 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
203 mask
->cond_mask
= val
;
205 lp_exec_mask_update(mask
);
208 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
210 LLVMValueRef prev_mask
;
211 LLVMValueRef inv_mask
;
213 assert(mask
->cond_stack_size
);
214 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
215 if (mask
->cond_stack_size
== 1) {
216 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
219 inv_mask
= LLVMBuildNot(mask
->bld
->builder
, mask
->cond_mask
, "");
221 mask
->cond_mask
= LLVMBuildAnd(mask
->bld
->builder
,
224 lp_exec_mask_update(mask
);
227 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
229 assert(mask
->cond_stack_size
);
230 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
231 lp_exec_mask_update(mask
);
234 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
236 if (mask
->loop_stack_size
== 0) {
237 assert(mask
->loop_block
== NULL
);
238 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
239 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
240 assert(mask
->break_var
== NULL
);
243 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
245 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
246 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
247 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
248 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
249 ++mask
->loop_stack_size
;
251 mask
->break_var
= lp_build_alloca(mask
->bld
->builder
, mask
->int_vec_type
, "");
252 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
254 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->builder
, "bgnloop");
255 LLVMBuildBr(mask
->bld
->builder
, mask
->loop_block
);
256 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, mask
->loop_block
);
258 mask
->break_mask
= LLVMBuildLoad(mask
->bld
->builder
, mask
->break_var
, "");
260 lp_exec_mask_update(mask
);
263 static void lp_exec_break(struct lp_exec_mask
*mask
)
265 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
269 mask
->break_mask
= LLVMBuildAnd(mask
->bld
->builder
,
271 exec_mask
, "break_full");
273 lp_exec_mask_update(mask
);
276 static void lp_exec_continue(struct lp_exec_mask
*mask
)
278 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
282 mask
->cont_mask
= LLVMBuildAnd(mask
->bld
->builder
,
286 lp_exec_mask_update(mask
);
290 static void lp_exec_endloop(struct lp_exec_mask
*mask
)
292 LLVMBasicBlockRef endloop
;
293 LLVMTypeRef reg_type
= LLVMIntType(mask
->bld
->type
.width
*
294 mask
->bld
->type
.length
);
297 assert(mask
->break_mask
);
300 * Restore the cont_mask, but don't pop
302 assert(mask
->loop_stack_size
);
303 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
304 lp_exec_mask_update(mask
);
307 * Unlike the continue mask, the break_mask must be preserved across loop
310 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
312 /* i1cond = (mask == 0) */
313 i1cond
= LLVMBuildICmp(
316 LLVMBuildBitCast(mask
->bld
->builder
, mask
->exec_mask
, reg_type
, ""),
317 LLVMConstNull(reg_type
), "");
319 endloop
= lp_build_insert_new_block(mask
->bld
->builder
, "endloop");
321 LLVMBuildCondBr(mask
->bld
->builder
,
322 i1cond
, mask
->loop_block
, endloop
);
324 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, endloop
);
326 assert(mask
->loop_stack_size
);
327 --mask
->loop_stack_size
;
328 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
329 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
330 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
331 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
333 lp_exec_mask_update(mask
);
336 /* stores val into an address pointed to by dst.
337 * mask->exec_mask is used to figure out which bits of val
338 * should be stored into the address
339 * (0 means don't store this bit, 1 means do store).
341 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
346 /* Mix the predicate and execution mask */
347 if (mask
->has_mask
) {
349 pred
= LLVMBuildAnd(mask
->bld
->builder
, pred
, mask
->exec_mask
, "");
351 pred
= mask
->exec_mask
;
356 LLVMValueRef real_val
, dst_val
;
358 dst_val
= LLVMBuildLoad(mask
->bld
->builder
, dst
, "");
359 real_val
= lp_build_select(mask
->bld
,
363 LLVMBuildStore(mask
->bld
->builder
, real_val
, dst
);
365 LLVMBuildStore(mask
->bld
->builder
, val
, dst
);
368 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
372 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
373 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
374 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
375 mask
->call_stack_size
++;
379 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
381 LLVMValueRef exec_mask
;
383 if (mask
->call_stack_size
== 0) {
384 /* returning from main() */
388 exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
392 mask
->ret_mask
= LLVMBuildAnd(mask
->bld
->builder
,
394 exec_mask
, "ret_full");
396 lp_exec_mask_update(mask
);
399 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
403 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
405 assert(mask
->call_stack_size
);
406 mask
->call_stack_size
--;
407 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
408 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
409 lp_exec_mask_update(mask
);
414 * Return pointer to a temporary register channel (src or dest).
415 * Note that indirect addressing cannot be handled here.
416 * \param index which temporary register
417 * \param chan which channel of the temp register.
420 get_temp_ptr(struct lp_build_tgsi_soa_context
*bld
,
425 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
426 LLVMValueRef lindex
= lp_build_const_int32(index
* 4 + chan
);
427 return LLVMBuildGEP(bld
->base
.builder
, bld
->temps_array
, &lindex
, 1, "");
430 return bld
->temps
[index
][chan
];
437 * XXX the lp_build_gather() function should be capable of doing this
438 * with a little work.
441 build_gather(struct lp_build_tgsi_soa_context
*bld
,
442 LLVMValueRef base_ptr
,
443 LLVMValueRef indexes
)
445 LLVMValueRef res
= bld
->base
.undef
;
449 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
451 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
452 LLVMValueRef ii
= LLVMConstInt(LLVMInt32Type(), i
, 0);
453 LLVMValueRef index
= LLVMBuildExtractElement(bld
->base
.builder
,
455 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, base_ptr
,
457 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
459 res
= LLVMBuildInsertElement(bld
->base
.builder
, res
, scalar
, ii
, "");
467 * Read the current value of the ADDR register, convert the floats to
468 * ints, multiply by four and return the vector of offsets.
469 * The offsets will be used to index into the constant buffer or
470 * temporary register file.
473 get_indirect_offsets(struct lp_build_tgsi_soa_context
*bld
,
474 const struct tgsi_src_register
*indirect_reg
)
476 /* always use X component of address register */
477 const int x
= indirect_reg
->SwizzleX
;
478 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
479 uint swizzle
= tgsi_util_get_src_register_swizzle(indirect_reg
, x
);
480 LLVMValueRef vec4
= lp_build_const_int_vec(bld
->int_bld
.type
, 4);
481 LLVMValueRef addr_vec
;
483 addr_vec
= LLVMBuildLoad(bld
->base
.builder
,
484 bld
->addr
[indirect_reg
->Index
][swizzle
],
487 /* for indexing we want integers */
488 addr_vec
= LLVMBuildFPToSI(bld
->base
.builder
, addr_vec
,
491 /* addr_vec = addr_vec * 4 */
492 addr_vec
= lp_build_mul(&bld
->int_bld
, addr_vec
, vec4
);
503 struct lp_build_tgsi_soa_context
*bld
,
504 const struct tgsi_full_instruction
*inst
,
506 const unsigned chan_index
)
508 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
509 const unsigned swizzle
=
510 tgsi_util_get_full_src_register_swizzle(reg
, chan_index
);
512 LLVMValueRef addr_vec
= NULL
;
515 assert(0 && "invalid swizzle in emit_fetch()");
516 return bld
->base
.undef
;
519 if (reg
->Register
.Indirect
) {
520 assert(bld
->indirect_files
);
521 addr_vec
= get_indirect_offsets(bld
, ®
->Indirect
);
524 switch (reg
->Register
.File
) {
525 case TGSI_FILE_CONSTANT
:
526 if (reg
->Register
.Indirect
) {
527 LLVMValueRef index_vec
; /* index into the const buffer */
529 assert(bld
->indirect_files
& (1 << TGSI_FILE_CONSTANT
));
531 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
532 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
533 reg
->Register
.Index
* 4 + swizzle
);
535 /* index_vec = index_vec + addr_vec */
536 index_vec
= lp_build_add(&bld
->int_bld
, index_vec
, addr_vec
);
538 /* Gather values from the constant buffer */
539 res
= build_gather(bld
, bld
->consts_ptr
, index_vec
);
542 LLVMValueRef index
; /* index into the const buffer */
543 LLVMValueRef scalar
, scalar_ptr
;
545 index
= lp_build_const_int32(reg
->Register
.Index
*4 + swizzle
);
547 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
549 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
551 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
555 case TGSI_FILE_IMMEDIATE
:
556 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
560 case TGSI_FILE_INPUT
:
561 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
565 case TGSI_FILE_TEMPORARY
:
566 if (reg
->Register
.Indirect
) {
567 LLVMValueRef vec_len
=
568 lp_build_const_int_vec(bld
->int_bld
.type
, bld
->base
.type
.length
);
569 LLVMValueRef index_vec
; /* index into the const buffer */
570 LLVMValueRef temps_array
;
571 LLVMTypeRef float4_ptr_type
;
573 assert(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
));
575 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
576 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
577 reg
->Register
.Index
* 4 + swizzle
);
579 /* index_vec += addr_vec */
580 index_vec
= lp_build_add(&bld
->int_bld
, index_vec
, addr_vec
);
582 /* index_vec *= vector_length */
583 index_vec
= lp_build_mul(&bld
->int_bld
, index_vec
, vec_len
);
585 /* cast temps_array pointer to float* */
586 float4_ptr_type
= LLVMPointerType(LLVMFloatType(), 0);
587 temps_array
= LLVMBuildBitCast(bld
->int_bld
.builder
, bld
->temps_array
,
588 float4_ptr_type
, "");
590 /* Gather values from the temporary register array */
591 res
= build_gather(bld
, temps_array
, index_vec
);
594 LLVMValueRef temp_ptr
;
595 temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
, swizzle
);
596 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
598 return bld
->base
.undef
;
603 assert(0 && "invalid src register in emit_fetch()");
604 return bld
->base
.undef
;
607 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
608 case TGSI_UTIL_SIGN_CLEAR
:
609 res
= lp_build_abs( &bld
->base
, res
);
612 case TGSI_UTIL_SIGN_SET
:
613 /* TODO: Use bitwese OR for floating point */
614 res
= lp_build_abs( &bld
->base
, res
);
615 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
618 case TGSI_UTIL_SIGN_TOGGLE
:
619 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
622 case TGSI_UTIL_SIGN_KEEP
:
631 * Register fetch with derivatives.
635 struct lp_build_tgsi_soa_context
*bld
,
636 const struct tgsi_full_instruction
*inst
,
638 const unsigned chan_index
,
645 src
= emit_fetch(bld
, inst
, index
, chan_index
);
650 /* TODO: use interpolation coeffs for inputs */
653 *ddx
= lp_build_ddx(&bld
->base
, src
);
656 *ddy
= lp_build_ddy(&bld
->base
, src
);
664 emit_fetch_predicate(
665 struct lp_build_tgsi_soa_context
*bld
,
666 const struct tgsi_full_instruction
*inst
,
670 unsigned char swizzles
[4];
671 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
675 if (!inst
->Instruction
.Predicate
) {
676 FOR_EACH_CHANNEL( chan
) {
682 swizzles
[0] = inst
->Predicate
.SwizzleX
;
683 swizzles
[1] = inst
->Predicate
.SwizzleY
;
684 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
685 swizzles
[3] = inst
->Predicate
.SwizzleW
;
687 index
= inst
->Predicate
.Index
;
688 assert(index
< LP_MAX_TGSI_PREDS
);
690 FOR_EACH_CHANNEL( chan
) {
691 unsigned swizzle
= swizzles
[chan
];
694 * Only fetch the predicate register channels that are actually listed
697 if (!unswizzled
[swizzle
]) {
698 value
= LLVMBuildLoad(bld
->base
.builder
,
699 bld
->preds
[index
][swizzle
], "");
702 * Convert the value to an integer mask.
704 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
705 * is needlessly causing two comparisons due to storing the intermediate
706 * result as float vector instead of an integer mask vector.
708 value
= lp_build_compare(bld
->base
.builder
,
713 if (inst
->Predicate
.Negate
) {
714 value
= LLVMBuildNot(bld
->base
.builder
, value
, "");
717 unswizzled
[swizzle
] = value
;
719 value
= unswizzled
[swizzle
];
732 struct lp_build_tgsi_soa_context
*bld
,
733 const struct tgsi_full_instruction
*inst
,
739 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
740 LLVMValueRef addr
= NULL
;
742 switch( inst
->Instruction
.Saturate
) {
746 case TGSI_SAT_ZERO_ONE
:
747 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
748 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
751 case TGSI_SAT_MINUS_PLUS_ONE
:
752 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
753 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
760 if (reg
->Register
.Indirect
) {
761 /* XXX use get_indirect_offsets() here eventually */
762 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
763 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
765 assert(bld
->indirect_files
);
767 addr
= LLVMBuildLoad(bld
->base
.builder
,
768 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
770 /* for indexing we want integers */
771 addr
= LLVMBuildFPToSI(bld
->base
.builder
, addr
,
773 addr
= LLVMBuildExtractElement(bld
->base
.builder
,
774 addr
, LLVMConstInt(LLVMInt32Type(), 0, 0),
776 addr
= LLVMBuildMul(bld
->base
.builder
,
777 addr
, LLVMConstInt(LLVMInt32Type(), 4, 0),
781 switch( reg
->Register
.File
) {
782 case TGSI_FILE_OUTPUT
:
783 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
784 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
787 case TGSI_FILE_TEMPORARY
:
788 if (reg
->Register
.Indirect
) {
789 /* XXX not done yet */
790 debug_printf("WARNING: LLVM scatter store of temp regs"
791 " not implemented\n");
794 LLVMValueRef temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
796 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, temp_ptr
);
800 case TGSI_FILE_ADDRESS
:
801 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
802 bld
->addr
[reg
->Indirect
.Index
][chan_index
]);
805 case TGSI_FILE_PREDICATE
:
806 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
807 bld
->preds
[index
][chan_index
]);
817 * High-level instruction translators.
821 TEX_MODIFIER_NONE
= 0,
822 TEX_MODIFIER_PROJECTED
,
823 TEX_MODIFIER_LOD_BIAS
,
824 TEX_MODIFIER_EXPLICIT_LOD
,
825 TEX_MODIFIER_EXPLICIT_DERIV
829 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
830 const struct tgsi_full_instruction
*inst
,
831 enum tex_modifier modifier
,
835 LLVMValueRef lod_bias
, explicit_lod
;
836 LLVMValueRef oow
= NULL
;
837 LLVMValueRef coords
[3];
844 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
845 for (i
= 0; i
< 4; i
++) {
846 texel
[i
] = bld
->base
.undef
;
851 switch (inst
->Texture
.Texture
) {
852 case TGSI_TEXTURE_1D
:
855 case TGSI_TEXTURE_2D
:
856 case TGSI_TEXTURE_RECT
:
859 case TGSI_TEXTURE_SHADOW1D
:
860 case TGSI_TEXTURE_SHADOW2D
:
861 case TGSI_TEXTURE_SHADOWRECT
:
862 case TGSI_TEXTURE_3D
:
863 case TGSI_TEXTURE_CUBE
:
871 if (modifier
== TEX_MODIFIER_LOD_BIAS
) {
872 lod_bias
= emit_fetch( bld
, inst
, 0, 3 );
875 else if (modifier
== TEX_MODIFIER_EXPLICIT_LOD
) {
877 explicit_lod
= emit_fetch( bld
, inst
, 0, 3 );
884 if (modifier
== TEX_MODIFIER_PROJECTED
) {
885 oow
= emit_fetch( bld
, inst
, 0, 3 );
886 oow
= lp_build_rcp(&bld
->base
, oow
);
889 for (i
= 0; i
< num_coords
; i
++) {
890 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
891 if (modifier
== TEX_MODIFIER_PROJECTED
)
892 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
894 for (i
= num_coords
; i
< 3; i
++) {
895 coords
[i
] = bld
->base
.undef
;
898 if (modifier
== TEX_MODIFIER_EXPLICIT_DERIV
) {
899 for (i
= 0; i
< num_coords
; i
++) {
900 ddx
[i
] = emit_fetch( bld
, inst
, 1, i
);
901 ddy
[i
] = emit_fetch( bld
, inst
, 2, i
);
903 unit
= inst
->Src
[3].Register
.Index
;
905 for (i
= 0; i
< num_coords
; i
++) {
906 ddx
[i
] = lp_build_ddx( &bld
->base
, coords
[i
] );
907 ddy
[i
] = lp_build_ddy( &bld
->base
, coords
[i
] );
909 unit
= inst
->Src
[1].Register
.Index
;
911 for (i
= num_coords
; i
< 3; i
++) {
912 ddx
[i
] = bld
->base
.undef
;
913 ddy
[i
] = bld
->base
.undef
;
916 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
919 unit
, num_coords
, coords
,
921 lod_bias
, explicit_lod
,
927 * Kill fragment if any of the src register values are negative.
931 struct lp_build_tgsi_soa_context
*bld
,
932 const struct tgsi_full_instruction
*inst
)
934 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
935 LLVMValueRef terms
[NUM_CHANNELS
];
939 memset(&terms
, 0, sizeof terms
);
941 FOR_EACH_CHANNEL( chan_index
) {
944 /* Unswizzle channel */
945 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
947 /* Check if the component has not been already tested. */
948 assert(swizzle
< NUM_CHANNELS
);
949 if( !terms
[swizzle
] )
950 /* TODO: change the comparison operator instead of setting the sign */
951 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
955 FOR_EACH_CHANNEL( chan_index
) {
956 if(terms
[chan_index
]) {
957 LLVMValueRef chan_mask
;
960 * If term < 0 then mask = 0 else mask = ~0.
962 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
965 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
972 lp_build_mask_update(bld
->mask
, mask
);
977 * Predicated fragment kill.
978 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
979 * The only predication is the execution mask which will apply if
980 * we're inside a loop or conditional.
983 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
984 const struct tgsi_full_instruction
*inst
)
988 /* For those channels which are "alive", disable fragment shader
991 if (bld
->exec_mask
.has_mask
) {
992 mask
= LLVMBuildNot(bld
->base
.builder
, bld
->exec_mask
.exec_mask
, "kilp");
995 mask
= bld
->base
.zero
;
998 lp_build_mask_update(bld
->mask
, mask
);
1003 struct lp_build_tgsi_soa_context
*bld
,
1004 const struct tgsi_full_declaration
*decl
)
1006 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
1008 unsigned first
= decl
->Range
.First
;
1009 unsigned last
= decl
->Range
.Last
;
1012 for (idx
= first
; idx
<= last
; ++idx
) {
1013 switch (decl
->Declaration
.File
) {
1014 case TGSI_FILE_TEMPORARY
:
1015 assert(idx
< LP_MAX_TGSI_TEMPS
);
1016 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
1017 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
1019 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
1020 vec_type
, array_size
, "");
1022 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1023 bld
->temps
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1028 case TGSI_FILE_OUTPUT
:
1029 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1030 bld
->outputs
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1034 case TGSI_FILE_ADDRESS
:
1035 assert(idx
< LP_MAX_TGSI_ADDRS
);
1036 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1037 bld
->addr
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1041 case TGSI_FILE_PREDICATE
:
1042 assert(idx
< LP_MAX_TGSI_PREDS
);
1043 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1044 bld
->preds
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1049 /* don't need to declare other vars */
1057 * Emit LLVM for one TGSI instruction.
1058 * \param return TRUE for success, FALSE otherwise
1062 struct lp_build_tgsi_soa_context
*bld
,
1063 const struct tgsi_full_instruction
*inst
,
1064 const struct tgsi_opcode_info
*info
,
1067 unsigned chan_index
;
1068 LLVMValueRef src0
, src1
, src2
;
1069 LLVMValueRef tmp0
, tmp1
, tmp2
;
1070 LLVMValueRef tmp3
= NULL
;
1071 LLVMValueRef tmp4
= NULL
;
1072 LLVMValueRef tmp5
= NULL
;
1073 LLVMValueRef tmp6
= NULL
;
1074 LLVMValueRef tmp7
= NULL
;
1076 LLVMValueRef dst0
[NUM_CHANNELS
];
1079 * Stores and write masks are handled in a general fashion after the long
1080 * instruction opcode switch statement.
1082 * Although not stricitly necessary, we avoid generating instructions for
1083 * channels which won't be stored, in cases where's that easy. For some
1084 * complex instructions, like texture sampling, it is more convenient to
1085 * assume a full writemask and then let LLVM optimization passes eliminate
1091 assert(info
->num_dst
<= 1);
1092 if (info
->num_dst
) {
1093 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1094 dst0
[chan_index
] = bld
->base
.undef
;
1098 switch (inst
->Instruction
.Opcode
) {
1099 case TGSI_OPCODE_ARL
:
1100 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1101 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1102 tmp0
= lp_build_floor(&bld
->base
, tmp0
);
1103 dst0
[chan_index
] = tmp0
;
1107 case TGSI_OPCODE_MOV
:
1108 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1109 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
1113 case TGSI_OPCODE_LIT
:
1114 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
1115 dst0
[CHAN_X
] = bld
->base
.one
;
1117 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1118 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1119 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
1121 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1122 /* XMM[1] = SrcReg[0].yyyy */
1123 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1124 /* XMM[1] = max(XMM[1], 0) */
1125 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
1126 /* XMM[2] = SrcReg[0].wwww */
1127 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1128 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
1129 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1130 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
1131 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
1133 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
1134 dst0
[CHAN_W
] = bld
->base
.one
;
1138 case TGSI_OPCODE_RCP
:
1139 /* TGSI_OPCODE_RECIP */
1140 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1141 res
= lp_build_rcp(&bld
->base
, src0
);
1142 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1143 dst0
[chan_index
] = res
;
1147 case TGSI_OPCODE_RSQ
:
1148 /* TGSI_OPCODE_RECIPSQRT */
1149 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1150 src0
= lp_build_abs(&bld
->base
, src0
);
1151 res
= lp_build_rsqrt(&bld
->base
, src0
);
1152 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1153 dst0
[chan_index
] = res
;
1157 case TGSI_OPCODE_EXP
:
1158 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1159 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1160 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1161 LLVMValueRef
*p_exp2_int_part
= NULL
;
1162 LLVMValueRef
*p_frac_part
= NULL
;
1163 LLVMValueRef
*p_exp2
= NULL
;
1165 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1167 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1168 p_exp2_int_part
= &tmp0
;
1169 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1170 p_frac_part
= &tmp1
;
1171 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1174 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
1176 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1177 dst0
[CHAN_X
] = tmp0
;
1178 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1179 dst0
[CHAN_Y
] = tmp1
;
1180 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1181 dst0
[CHAN_Z
] = tmp2
;
1184 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1185 dst0
[CHAN_W
] = bld
->base
.one
;
1189 case TGSI_OPCODE_LOG
:
1190 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1191 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1192 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1193 LLVMValueRef
*p_floor_log2
= NULL
;
1194 LLVMValueRef
*p_exp
= NULL
;
1195 LLVMValueRef
*p_log2
= NULL
;
1197 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1198 src0
= lp_build_abs( &bld
->base
, src0
);
1200 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1201 p_floor_log2
= &tmp0
;
1202 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1204 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1207 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
1209 /* dst.x = floor(lg2(abs(src.x))) */
1210 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1211 dst0
[CHAN_X
] = tmp0
;
1212 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1213 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
1214 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
1216 /* dst.z = lg2(abs(src.x)) */
1217 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1218 dst0
[CHAN_Z
] = tmp2
;
1221 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1222 dst0
[CHAN_W
] = bld
->base
.one
;
1226 case TGSI_OPCODE_MUL
:
1227 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1228 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1229 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1230 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
1234 case TGSI_OPCODE_ADD
:
1235 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1236 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1237 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1238 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
1242 case TGSI_OPCODE_DP3
:
1243 /* TGSI_OPCODE_DOT3 */
1244 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1245 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1246 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1247 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1248 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1249 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1250 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1251 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1252 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1253 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1254 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1255 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1256 dst0
[chan_index
] = tmp0
;
1260 case TGSI_OPCODE_DP4
:
1261 /* TGSI_OPCODE_DOT4 */
1262 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1263 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1264 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1265 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1266 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1267 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1268 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1269 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1270 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1271 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1272 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1273 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1274 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1275 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1276 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1277 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1278 dst0
[chan_index
] = tmp0
;
1282 case TGSI_OPCODE_DST
:
1283 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1284 dst0
[CHAN_X
] = bld
->base
.one
;
1286 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1287 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1288 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1289 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1291 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1292 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
1294 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1295 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
1299 case TGSI_OPCODE_MIN
:
1300 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1301 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1302 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1303 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
1307 case TGSI_OPCODE_MAX
:
1308 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1309 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1310 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1311 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
1315 case TGSI_OPCODE_SLT
:
1316 /* TGSI_OPCODE_SETLT */
1317 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1318 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1319 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1320 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
1321 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1325 case TGSI_OPCODE_SGE
:
1326 /* TGSI_OPCODE_SETGE */
1327 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1328 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1329 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1330 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1331 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1335 case TGSI_OPCODE_MAD
:
1336 /* TGSI_OPCODE_MADD */
1337 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1338 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1339 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1340 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1341 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1342 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1343 dst0
[chan_index
] = tmp0
;
1347 case TGSI_OPCODE_SUB
:
1348 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1349 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1350 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1351 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1355 case TGSI_OPCODE_LRP
:
1356 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1357 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1358 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1359 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1360 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1361 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1362 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1366 case TGSI_OPCODE_CND
:
1367 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1368 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1369 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1370 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1371 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
1372 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1373 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1377 case TGSI_OPCODE_DP2A
:
1378 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1379 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1380 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1381 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1382 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1383 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1384 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1385 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1386 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1387 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1388 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1392 case TGSI_OPCODE_FRC
:
1393 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1394 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1395 tmp0
= lp_build_floor(&bld
->base
, src0
);
1396 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1397 dst0
[chan_index
] = tmp0
;
1401 case TGSI_OPCODE_CLAMP
:
1402 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1403 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1404 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1405 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1406 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1407 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1408 dst0
[chan_index
] = tmp0
;
1412 case TGSI_OPCODE_FLR
:
1413 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1414 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1415 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1419 case TGSI_OPCODE_ROUND
:
1420 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1421 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1422 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1426 case TGSI_OPCODE_EX2
: {
1427 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1428 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1429 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1430 dst0
[chan_index
] = tmp0
;
1435 case TGSI_OPCODE_LG2
:
1436 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1437 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1438 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1439 dst0
[chan_index
] = tmp0
;
1443 case TGSI_OPCODE_POW
:
1444 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1445 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1446 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1447 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1448 dst0
[chan_index
] = res
;
1452 case TGSI_OPCODE_XPD
:
1453 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1454 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1455 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1456 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1458 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1459 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1460 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1461 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1463 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1465 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1467 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1468 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1469 dst0
[CHAN_X
] = tmp2
;
1471 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1472 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1473 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1474 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1476 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1477 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1478 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1479 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1480 dst0
[CHAN_Y
] = tmp3
;
1482 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1483 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1484 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1485 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1486 dst0
[CHAN_Z
] = tmp5
;
1488 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1489 dst0
[CHAN_W
] = bld
->base
.one
;
1493 case TGSI_OPCODE_ABS
:
1494 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1495 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1496 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1500 case TGSI_OPCODE_RCC
:
1505 case TGSI_OPCODE_DPH
:
1506 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1507 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1508 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1509 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1510 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1511 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1512 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1513 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1514 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1515 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1516 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1517 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1518 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1519 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1520 dst0
[chan_index
] = tmp0
;
1524 case TGSI_OPCODE_COS
:
1525 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1526 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1527 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1528 dst0
[chan_index
] = tmp0
;
1532 case TGSI_OPCODE_DDX
:
1533 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1534 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1538 case TGSI_OPCODE_DDY
:
1539 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1540 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1544 case TGSI_OPCODE_KILP
:
1545 /* predicated kill */
1546 emit_kilp( bld
, inst
);
1549 case TGSI_OPCODE_KIL
:
1550 /* conditional kill */
1551 emit_kil( bld
, inst
);
1554 case TGSI_OPCODE_PK2H
:
1558 case TGSI_OPCODE_PK2US
:
1562 case TGSI_OPCODE_PK4B
:
1566 case TGSI_OPCODE_PK4UB
:
1570 case TGSI_OPCODE_RFL
:
1574 case TGSI_OPCODE_SEQ
:
1575 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1576 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1577 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1578 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1579 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1583 case TGSI_OPCODE_SFL
:
1584 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1585 dst0
[chan_index
] = bld
->base
.zero
;
1589 case TGSI_OPCODE_SGT
:
1590 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1591 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1592 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1593 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1594 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1598 case TGSI_OPCODE_SIN
:
1599 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1600 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1601 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1602 dst0
[chan_index
] = tmp0
;
1606 case TGSI_OPCODE_SLE
:
1607 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1608 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1609 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1610 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1611 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1615 case TGSI_OPCODE_SNE
:
1616 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1617 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1618 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1619 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1620 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1624 case TGSI_OPCODE_STR
:
1625 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1626 dst0
[chan_index
] = bld
->base
.one
;
1630 case TGSI_OPCODE_TEX
:
1631 emit_tex( bld
, inst
, TEX_MODIFIER_NONE
, dst0
);
1634 case TGSI_OPCODE_TXD
:
1635 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_DERIV
, dst0
);
1638 case TGSI_OPCODE_UP2H
:
1644 case TGSI_OPCODE_UP2US
:
1650 case TGSI_OPCODE_UP4B
:
1656 case TGSI_OPCODE_UP4UB
:
1662 case TGSI_OPCODE_X2D
:
1668 case TGSI_OPCODE_ARA
:
1674 case TGSI_OPCODE_ARR
:
1675 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1676 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1677 tmp0
= lp_build_round(&bld
->base
, tmp0
);
1678 dst0
[chan_index
] = tmp0
;
1682 case TGSI_OPCODE_BRA
:
1688 case TGSI_OPCODE_CAL
:
1689 lp_exec_mask_call(&bld
->exec_mask
,
1695 case TGSI_OPCODE_RET
:
1696 lp_exec_mask_ret(&bld
->exec_mask
, pc
);
1699 case TGSI_OPCODE_END
:
1703 case TGSI_OPCODE_SSG
:
1704 /* TGSI_OPCODE_SGN */
1705 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1706 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1707 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1711 case TGSI_OPCODE_CMP
:
1712 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1713 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1714 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1715 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1716 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1717 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1721 case TGSI_OPCODE_SCS
:
1722 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1723 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1724 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1726 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1727 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1728 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1730 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1731 dst0
[CHAN_Z
] = bld
->base
.zero
;
1733 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1734 dst0
[CHAN_W
] = bld
->base
.one
;
1738 case TGSI_OPCODE_TXB
:
1739 emit_tex( bld
, inst
, TEX_MODIFIER_LOD_BIAS
, dst0
);
1742 case TGSI_OPCODE_NRM
:
1744 case TGSI_OPCODE_NRM4
:
1745 /* 3 or 4-component normalization */
1747 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1749 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1750 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1751 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1752 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1754 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1757 /* xmm0 = src.x * src.x */
1758 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1759 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1762 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1765 /* xmm0 = xmm0 + src.y * src.y */
1766 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1767 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1770 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1771 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1774 /* xmm0 = xmm0 + src.z * src.z */
1775 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1776 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1779 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1780 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1784 /* xmm0 = xmm0 + src.w * src.w */
1785 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1786 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1789 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1790 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1793 /* xmm1 = 1 / sqrt(xmm0) */
1794 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1796 /* dst.x = xmm1 * src.x */
1797 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1798 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1801 /* dst.y = xmm1 * src.y */
1802 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1803 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1806 /* dst.z = xmm1 * src.z */
1807 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1808 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1811 /* dst.w = xmm1 * src.w */
1812 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1813 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1818 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1819 dst0
[CHAN_W
] = bld
->base
.one
;
1824 case TGSI_OPCODE_DIV
:
1830 case TGSI_OPCODE_DP2
:
1831 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1832 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1833 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1834 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1835 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1836 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1837 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1838 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1839 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1843 case TGSI_OPCODE_TXL
:
1844 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_LOD
, dst0
);
1847 case TGSI_OPCODE_TXP
:
1848 emit_tex( bld
, inst
, TEX_MODIFIER_PROJECTED
, dst0
);
1851 case TGSI_OPCODE_BRK
:
1852 lp_exec_break(&bld
->exec_mask
);
1855 case TGSI_OPCODE_IF
:
1856 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1857 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
1858 tmp0
, bld
->base
.zero
);
1859 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1862 case TGSI_OPCODE_BGNLOOP
:
1863 lp_exec_bgnloop(&bld
->exec_mask
);
1866 case TGSI_OPCODE_BGNSUB
:
1867 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1870 case TGSI_OPCODE_ELSE
:
1871 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1874 case TGSI_OPCODE_ENDIF
:
1875 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1878 case TGSI_OPCODE_ENDLOOP
:
1879 lp_exec_endloop(&bld
->exec_mask
);
1882 case TGSI_OPCODE_ENDSUB
:
1883 lp_exec_mask_endsub(&bld
->exec_mask
, pc
);
1886 case TGSI_OPCODE_PUSHA
:
1892 case TGSI_OPCODE_POPA
:
1898 case TGSI_OPCODE_CEIL
:
1899 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1900 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1901 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1905 case TGSI_OPCODE_I2F
:
1911 case TGSI_OPCODE_NOT
:
1917 case TGSI_OPCODE_TRUNC
:
1918 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1919 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1920 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1924 case TGSI_OPCODE_SHL
:
1930 case TGSI_OPCODE_ISHR
:
1936 case TGSI_OPCODE_AND
:
1942 case TGSI_OPCODE_OR
:
1948 case TGSI_OPCODE_MOD
:
1954 case TGSI_OPCODE_XOR
:
1960 case TGSI_OPCODE_SAD
:
1966 case TGSI_OPCODE_TXF
:
1972 case TGSI_OPCODE_TXQ
:
1978 case TGSI_OPCODE_CONT
:
1979 lp_exec_continue(&bld
->exec_mask
);
1982 case TGSI_OPCODE_EMIT
:
1986 case TGSI_OPCODE_ENDPRIM
:
1990 case TGSI_OPCODE_NOP
:
1998 LLVMValueRef pred
[NUM_CHANNELS
];
2000 emit_fetch_predicate( bld
, inst
, pred
);
2002 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2003 emit_store( bld
, inst
, 0, chan_index
, pred
[chan_index
], dst0
[chan_index
]);
2012 lp_build_tgsi_soa(LLVMBuilderRef builder
,
2013 const struct tgsi_token
*tokens
,
2014 struct lp_type type
,
2015 struct lp_build_mask_context
*mask
,
2016 LLVMValueRef consts_ptr
,
2017 const LLVMValueRef
*pos
,
2018 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
2019 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
2020 struct lp_build_sampler_soa
*sampler
,
2021 const struct tgsi_shader_info
*info
)
2023 struct lp_build_tgsi_soa_context bld
;
2024 struct tgsi_parse_context parse
;
2025 uint num_immediates
= 0;
2026 uint num_instructions
= 0;
2030 /* Setup build context */
2031 memset(&bld
, 0, sizeof bld
);
2032 lp_build_context_init(&bld
.base
, builder
, type
);
2033 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
2036 bld
.inputs
= inputs
;
2037 bld
.outputs
= outputs
;
2038 bld
.consts_ptr
= consts_ptr
;
2039 bld
.sampler
= sampler
;
2040 bld
.indirect_files
= info
->indirect_files
;
2041 bld
.instructions
= (struct tgsi_full_instruction
*)
2042 MALLOC( LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
) );
2043 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
2045 if (!bld
.instructions
) {
2049 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
2051 tgsi_parse_init( &parse
, tokens
);
2053 while( !tgsi_parse_end_of_tokens( &parse
) ) {
2054 tgsi_parse_token( &parse
);
2056 switch( parse
.FullToken
.Token
.Type
) {
2057 case TGSI_TOKEN_TYPE_DECLARATION
:
2058 /* Inputs already interpolated */
2059 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
2062 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2064 /* save expanded instruction */
2065 if (num_instructions
== bld
.max_instructions
) {
2066 bld
.instructions
= REALLOC(bld
.instructions
,
2067 bld
.max_instructions
2068 * sizeof(struct tgsi_full_instruction
),
2069 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
2070 * sizeof(struct tgsi_full_instruction
));
2071 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
2074 memcpy(bld
.instructions
+ num_instructions
,
2075 &parse
.FullToken
.FullInstruction
,
2076 sizeof(bld
.instructions
[0]));
2083 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2084 /* simply copy the immediate values into the next immediates[] slot */
2086 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
2088 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
2089 for( i
= 0; i
< size
; ++i
)
2090 bld
.immediates
[num_immediates
][i
] =
2091 lp_build_const_vec(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
2092 for( i
= size
; i
< 4; ++i
)
2093 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
2098 case TGSI_TOKEN_TYPE_PROPERTY
:
2107 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
2108 const struct tgsi_opcode_info
*opcode_info
=
2109 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
2110 if (!emit_instruction( &bld
, instr
, opcode_info
, &pc
))
2111 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2112 opcode_info
->mnemonic
);
2116 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
2117 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2118 debug_printf("11111111111111111111111111111 \n");
2119 tgsi_dump(tokens
, 0);
2120 lp_debug_dump_value(function
);
2121 debug_printf("2222222222222222222222222222 \n");
2123 tgsi_parse_free( &parse
);
2126 LLVMModuleRef module
= LLVMGetGlobalParent(
2127 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
2128 LLVMDumpModule(module
);
2132 FREE( bld
.instructions
);