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
) {
484 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
485 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
487 LLVMValueRef vec4
= lp_build_const_int_vec(bld
->int_bld
.type
, 4);
489 assert(bld
->has_indirect_addressing
);
491 addr_vec
= LLVMBuildLoad(bld
->base
.builder
,
492 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
495 /* for indexing we want integers */
496 addr_vec
= LLVMBuildFPToSI(bld
->base
.builder
, addr_vec
,
499 /* addr_vec = addr_vec * 4 */
500 addr_vec
= lp_build_mul(&bld
->base
, addr_vec
, vec4
);
503 switch (reg
->Register
.File
) {
504 case TGSI_FILE_CONSTANT
:
506 if (reg
->Register
.Indirect
) {
507 LLVMValueRef index_vec
; /* index into the const buffer */
509 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
510 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
511 reg
->Register
.Index
* 4 + swizzle
);
513 /* index_vec = index_vec + addr_vec */
514 index_vec
= lp_build_add(&bld
->base
, index_vec
, addr_vec
);
516 /* Gather values from the constant buffer */
517 res
= build_gather(bld
, bld
->consts_ptr
, index_vec
);
520 LLVMValueRef index
; /* index into the const buffer */
521 LLVMValueRef scalar
, scalar_ptr
;
523 index
= lp_build_const_int32(reg
->Register
.Index
*4 + swizzle
);
525 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
527 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
529 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
534 case TGSI_FILE_IMMEDIATE
:
535 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
539 case TGSI_FILE_INPUT
:
540 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
544 case TGSI_FILE_TEMPORARY
:
545 if (reg
->Register
.Indirect
) {
546 LLVMValueRef vec_len
=
547 lp_build_const_int_vec(bld
->int_bld
.type
, bld
->base
.type
.length
);
548 LLVMValueRef index_vec
; /* index into the const buffer */
549 LLVMValueRef temps_array
;
550 LLVMTypeRef float4_ptr_type
;
552 assert(bld
->has_indirect_addressing
);
554 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
555 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
556 reg
->Register
.Index
* 4 + swizzle
);
558 /* index_vec += addr_vec */
559 index_vec
= lp_build_add(&bld
->int_bld
, index_vec
, addr_vec
);
561 /* index_vec *= vector_length */
562 index_vec
= lp_build_mul(&bld
->int_bld
, index_vec
, vec_len
);
564 /* cast temps_array pointer to float* */
565 float4_ptr_type
= LLVMPointerType(LLVMFloatType(), 0);
566 temps_array
= LLVMBuildBitCast(bld
->int_bld
.builder
, bld
->temps_array
,
567 float4_ptr_type
, "");
569 /* Gather values from the temporary register array */
570 res
= build_gather(bld
, temps_array
, index_vec
);
573 LLVMValueRef temp_ptr
;
574 temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
, swizzle
);
575 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
577 return bld
->base
.undef
;
582 assert(0 && "invalid src register in emit_fetch()");
583 return bld
->base
.undef
;
586 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
587 case TGSI_UTIL_SIGN_CLEAR
:
588 res
= lp_build_abs( &bld
->base
, res
);
591 case TGSI_UTIL_SIGN_SET
:
592 /* TODO: Use bitwese OR for floating point */
593 res
= lp_build_abs( &bld
->base
, res
);
594 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
597 case TGSI_UTIL_SIGN_TOGGLE
:
598 res
= LLVMBuildNeg( bld
->base
.builder
, res
, "" );
601 case TGSI_UTIL_SIGN_KEEP
:
610 * Register fetch with derivatives.
614 struct lp_build_tgsi_soa_context
*bld
,
615 const struct tgsi_full_instruction
*inst
,
617 const unsigned chan_index
,
624 src
= emit_fetch(bld
, inst
, index
, chan_index
);
629 /* TODO: use interpolation coeffs for inputs */
632 *ddx
= lp_build_ddx(&bld
->base
, src
);
635 *ddy
= lp_build_ddy(&bld
->base
, src
);
643 emit_fetch_predicate(
644 struct lp_build_tgsi_soa_context
*bld
,
645 const struct tgsi_full_instruction
*inst
,
649 unsigned char swizzles
[4];
650 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
654 if (!inst
->Instruction
.Predicate
) {
655 FOR_EACH_CHANNEL( chan
) {
661 swizzles
[0] = inst
->Predicate
.SwizzleX
;
662 swizzles
[1] = inst
->Predicate
.SwizzleY
;
663 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
664 swizzles
[3] = inst
->Predicate
.SwizzleW
;
666 index
= inst
->Predicate
.Index
;
667 assert(index
< LP_MAX_TGSI_PREDS
);
669 FOR_EACH_CHANNEL( chan
) {
670 unsigned swizzle
= swizzles
[chan
];
673 * Only fetch the predicate register channels that are actually listed
676 if (!unswizzled
[swizzle
]) {
677 value
= LLVMBuildLoad(bld
->base
.builder
,
678 bld
->preds
[index
][swizzle
], "");
681 * Convert the value to an integer mask.
683 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
684 * is needlessly causing two comparisons due to storing the intermediate
685 * result as float vector instead of an integer mask vector.
687 value
= lp_build_compare(bld
->base
.builder
,
692 if (inst
->Predicate
.Negate
) {
693 value
= LLVMBuildNot(bld
->base
.builder
, value
, "");
696 unswizzled
[swizzle
] = value
;
698 value
= unswizzled
[swizzle
];
711 struct lp_build_tgsi_soa_context
*bld
,
712 const struct tgsi_full_instruction
*inst
,
718 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
719 LLVMValueRef addr
= NULL
;
721 switch( inst
->Instruction
.Saturate
) {
725 case TGSI_SAT_ZERO_ONE
:
726 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
727 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
730 case TGSI_SAT_MINUS_PLUS_ONE
:
731 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
732 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
739 if (reg
->Register
.Indirect
) {
740 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
741 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
742 addr
= LLVMBuildLoad(bld
->base
.builder
,
743 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
745 /* for indexing we want integers */
746 addr
= LLVMBuildFPToSI(bld
->base
.builder
, addr
,
748 addr
= LLVMBuildExtractElement(bld
->base
.builder
,
749 addr
, LLVMConstInt(LLVMInt32Type(), 0, 0),
751 addr
= lp_build_mul(&bld
->base
, addr
, LLVMConstInt(LLVMInt32Type(), 4, 0));
754 switch( reg
->Register
.File
) {
755 case TGSI_FILE_OUTPUT
:
756 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
757 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
760 case TGSI_FILE_TEMPORARY
:
761 if (reg
->Register
.Indirect
) {
762 /* XXX not done yet */
763 debug_printf("WARNING: LLVM scatter store of temp regs"
764 " not implemented\n");
767 LLVMValueRef temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
769 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, temp_ptr
);
773 case TGSI_FILE_ADDRESS
:
774 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
775 bld
->addr
[reg
->Indirect
.Index
][chan_index
]);
778 case TGSI_FILE_PREDICATE
:
779 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
780 bld
->preds
[index
][chan_index
]);
790 * High-level instruction translators.
794 TEX_MODIFIER_NONE
= 0,
795 TEX_MODIFIER_PROJECTED
,
796 TEX_MODIFIER_LOD_BIAS
,
797 TEX_MODIFIER_EXPLICIT_LOD
,
798 TEX_MODIFIER_EXPLICIT_DERIV
802 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
803 const struct tgsi_full_instruction
*inst
,
804 enum tex_modifier modifier
,
808 LLVMValueRef lod_bias
, explicit_lod
;
809 LLVMValueRef oow
= NULL
;
810 LLVMValueRef coords
[3];
817 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
818 for (i
= 0; i
< 4; i
++) {
819 texel
[i
] = bld
->base
.undef
;
824 switch (inst
->Texture
.Texture
) {
825 case TGSI_TEXTURE_1D
:
828 case TGSI_TEXTURE_2D
:
829 case TGSI_TEXTURE_RECT
:
832 case TGSI_TEXTURE_SHADOW1D
:
833 case TGSI_TEXTURE_SHADOW2D
:
834 case TGSI_TEXTURE_SHADOWRECT
:
835 case TGSI_TEXTURE_3D
:
836 case TGSI_TEXTURE_CUBE
:
844 if (modifier
== TEX_MODIFIER_LOD_BIAS
) {
845 lod_bias
= emit_fetch( bld
, inst
, 0, 3 );
848 else if (modifier
== TEX_MODIFIER_EXPLICIT_LOD
) {
850 explicit_lod
= emit_fetch( bld
, inst
, 0, 3 );
857 if (modifier
== TEX_MODIFIER_PROJECTED
) {
858 oow
= emit_fetch( bld
, inst
, 0, 3 );
859 oow
= lp_build_rcp(&bld
->base
, oow
);
862 for (i
= 0; i
< num_coords
; i
++) {
863 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
864 if (modifier
== TEX_MODIFIER_PROJECTED
)
865 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
867 for (i
= num_coords
; i
< 3; i
++) {
868 coords
[i
] = bld
->base
.undef
;
871 if (modifier
== TEX_MODIFIER_EXPLICIT_DERIV
) {
872 for (i
= 0; i
< num_coords
; i
++) {
873 ddx
[i
] = emit_fetch( bld
, inst
, 1, i
);
874 ddy
[i
] = emit_fetch( bld
, inst
, 2, i
);
876 unit
= inst
->Src
[3].Register
.Index
;
878 for (i
= 0; i
< num_coords
; i
++) {
879 ddx
[i
] = lp_build_ddx( &bld
->base
, coords
[i
] );
880 ddy
[i
] = lp_build_ddy( &bld
->base
, coords
[i
] );
882 unit
= inst
->Src
[1].Register
.Index
;
884 for (i
= num_coords
; i
< 3; i
++) {
885 ddx
[i
] = bld
->base
.undef
;
886 ddy
[i
] = bld
->base
.undef
;
889 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
892 unit
, num_coords
, coords
,
894 lod_bias
, explicit_lod
,
900 * Kill fragment if any of the src register values are negative.
904 struct lp_build_tgsi_soa_context
*bld
,
905 const struct tgsi_full_instruction
*inst
)
907 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
908 LLVMValueRef terms
[NUM_CHANNELS
];
912 memset(&terms
, 0, sizeof terms
);
914 FOR_EACH_CHANNEL( chan_index
) {
917 /* Unswizzle channel */
918 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
920 /* Check if the component has not been already tested. */
921 assert(swizzle
< NUM_CHANNELS
);
922 if( !terms
[swizzle
] )
923 /* TODO: change the comparison operator instead of setting the sign */
924 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
928 FOR_EACH_CHANNEL( chan_index
) {
929 if(terms
[chan_index
]) {
930 LLVMValueRef chan_mask
;
933 * If term < 0 then mask = 0 else mask = ~0.
935 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
938 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
945 lp_build_mask_update(bld
->mask
, mask
);
950 * Predicated fragment kill.
951 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
952 * The only predication is the execution mask which will apply if
953 * we're inside a loop or conditional.
956 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
957 const struct tgsi_full_instruction
*inst
)
961 /* For those channels which are "alive", disable fragment shader
964 if (bld
->exec_mask
.has_mask
) {
965 mask
= LLVMBuildNot(bld
->base
.builder
, bld
->exec_mask
.exec_mask
, "kilp");
968 mask
= bld
->base
.zero
;
971 lp_build_mask_update(bld
->mask
, mask
);
976 struct lp_build_tgsi_soa_context
*bld
,
977 const struct tgsi_full_declaration
*decl
)
979 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
981 unsigned first
= decl
->Range
.First
;
982 unsigned last
= decl
->Range
.Last
;
985 for (idx
= first
; idx
<= last
; ++idx
) {
986 switch (decl
->Declaration
.File
) {
987 case TGSI_FILE_TEMPORARY
:
988 assert(idx
< LP_MAX_TGSI_TEMPS
);
989 if (bld
->has_indirect_addressing
) {
990 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
992 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
993 vec_type
, array_size
, "");
995 for (i
= 0; i
< NUM_CHANNELS
; i
++)
996 bld
->temps
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1001 case TGSI_FILE_OUTPUT
:
1002 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1003 bld
->outputs
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1007 case TGSI_FILE_ADDRESS
:
1008 assert(idx
< LP_MAX_TGSI_ADDRS
);
1009 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1010 bld
->addr
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1014 case TGSI_FILE_PREDICATE
:
1015 assert(idx
< LP_MAX_TGSI_PREDS
);
1016 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1017 bld
->preds
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1022 /* don't need to declare other vars */
1030 * Emit LLVM for one TGSI instruction.
1031 * \param return TRUE for success, FALSE otherwise
1035 struct lp_build_tgsi_soa_context
*bld
,
1036 const struct tgsi_full_instruction
*inst
,
1037 const struct tgsi_opcode_info
*info
,
1040 unsigned chan_index
;
1041 LLVMValueRef src0
, src1
, src2
;
1042 LLVMValueRef tmp0
, tmp1
, tmp2
;
1043 LLVMValueRef tmp3
= NULL
;
1044 LLVMValueRef tmp4
= NULL
;
1045 LLVMValueRef tmp5
= NULL
;
1046 LLVMValueRef tmp6
= NULL
;
1047 LLVMValueRef tmp7
= NULL
;
1049 LLVMValueRef dst0
[NUM_CHANNELS
];
1052 * Stores and write masks are handled in a general fashion after the long
1053 * instruction opcode switch statement.
1055 * Although not stricitly necessary, we avoid generating instructions for
1056 * channels which won't be stored, in cases where's that easy. For some
1057 * complex instructions, like texture sampling, it is more convenient to
1058 * assume a full writemask and then let LLVM optimization passes eliminate
1064 assert(info
->num_dst
<= 1);
1065 if (info
->num_dst
) {
1066 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1067 dst0
[chan_index
] = bld
->base
.undef
;
1071 switch (inst
->Instruction
.Opcode
) {
1072 case TGSI_OPCODE_ARL
:
1073 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1074 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1075 tmp0
= lp_build_floor(&bld
->base
, tmp0
);
1076 dst0
[chan_index
] = tmp0
;
1080 case TGSI_OPCODE_MOV
:
1081 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1082 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
1086 case TGSI_OPCODE_LIT
:
1087 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
1088 dst0
[CHAN_X
] = bld
->base
.one
;
1090 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1091 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1092 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
1094 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1095 /* XMM[1] = SrcReg[0].yyyy */
1096 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1097 /* XMM[1] = max(XMM[1], 0) */
1098 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
1099 /* XMM[2] = SrcReg[0].wwww */
1100 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1101 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
1102 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1103 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
1104 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
1106 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
1107 dst0
[CHAN_W
] = bld
->base
.one
;
1111 case TGSI_OPCODE_RCP
:
1112 /* TGSI_OPCODE_RECIP */
1113 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1114 res
= lp_build_rcp(&bld
->base
, src0
);
1115 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1116 dst0
[chan_index
] = res
;
1120 case TGSI_OPCODE_RSQ
:
1121 /* TGSI_OPCODE_RECIPSQRT */
1122 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1123 src0
= lp_build_abs(&bld
->base
, src0
);
1124 res
= lp_build_rsqrt(&bld
->base
, src0
);
1125 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1126 dst0
[chan_index
] = res
;
1130 case TGSI_OPCODE_EXP
:
1131 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1132 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1133 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1134 LLVMValueRef
*p_exp2_int_part
= NULL
;
1135 LLVMValueRef
*p_frac_part
= NULL
;
1136 LLVMValueRef
*p_exp2
= NULL
;
1138 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1140 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1141 p_exp2_int_part
= &tmp0
;
1142 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1143 p_frac_part
= &tmp1
;
1144 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1147 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
1149 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1150 dst0
[CHAN_X
] = tmp0
;
1151 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1152 dst0
[CHAN_Y
] = tmp1
;
1153 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1154 dst0
[CHAN_Z
] = tmp2
;
1157 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1158 dst0
[CHAN_W
] = bld
->base
.one
;
1162 case TGSI_OPCODE_LOG
:
1163 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1164 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1165 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1166 LLVMValueRef
*p_floor_log2
= NULL
;
1167 LLVMValueRef
*p_exp
= NULL
;
1168 LLVMValueRef
*p_log2
= NULL
;
1170 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1171 src0
= lp_build_abs( &bld
->base
, src0
);
1173 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1174 p_floor_log2
= &tmp0
;
1175 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1177 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1180 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
1182 /* dst.x = floor(lg2(abs(src.x))) */
1183 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1184 dst0
[CHAN_X
] = tmp0
;
1185 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1186 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
1187 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
1189 /* dst.z = lg2(abs(src.x)) */
1190 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1191 dst0
[CHAN_Z
] = tmp2
;
1194 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1195 dst0
[CHAN_W
] = bld
->base
.one
;
1199 case TGSI_OPCODE_MUL
:
1200 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1201 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1202 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1203 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
1207 case TGSI_OPCODE_ADD
:
1208 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1209 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1210 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1211 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
1215 case TGSI_OPCODE_DP3
:
1216 /* TGSI_OPCODE_DOT3 */
1217 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1218 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1219 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1220 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1221 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1222 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1223 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1224 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1225 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1226 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1227 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1228 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1229 dst0
[chan_index
] = tmp0
;
1233 case TGSI_OPCODE_DP4
:
1234 /* TGSI_OPCODE_DOT4 */
1235 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1236 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1237 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1238 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1239 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1240 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1241 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1242 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1243 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
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_W
);
1247 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1248 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1249 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1250 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1251 dst0
[chan_index
] = tmp0
;
1255 case TGSI_OPCODE_DST
:
1256 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1257 dst0
[CHAN_X
] = bld
->base
.one
;
1259 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1260 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1261 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1262 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1264 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1265 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
1267 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1268 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
1272 case TGSI_OPCODE_MIN
:
1273 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1274 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1275 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1276 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
1280 case TGSI_OPCODE_MAX
:
1281 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1282 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1283 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1284 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
1288 case TGSI_OPCODE_SLT
:
1289 /* TGSI_OPCODE_SETLT */
1290 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1291 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1292 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1293 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
1294 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1298 case TGSI_OPCODE_SGE
:
1299 /* TGSI_OPCODE_SETGE */
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 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1304 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1308 case TGSI_OPCODE_MAD
:
1309 /* TGSI_OPCODE_MADD */
1310 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1311 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1312 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1313 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1314 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1315 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1316 dst0
[chan_index
] = tmp0
;
1320 case TGSI_OPCODE_SUB
:
1321 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1322 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1323 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1324 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1328 case TGSI_OPCODE_LRP
:
1329 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1330 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1331 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1332 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1333 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1334 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1335 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1339 case TGSI_OPCODE_CND
:
1340 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1341 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1342 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1343 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1344 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
1345 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1346 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1350 case TGSI_OPCODE_DP2A
:
1351 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1352 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1353 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1354 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1355 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1356 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1357 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1358 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1359 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1360 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1361 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1365 case TGSI_OPCODE_FRC
:
1366 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1367 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1368 tmp0
= lp_build_floor(&bld
->base
, src0
);
1369 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1370 dst0
[chan_index
] = tmp0
;
1374 case TGSI_OPCODE_CLAMP
:
1375 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1376 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1377 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1378 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1379 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1380 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1381 dst0
[chan_index
] = tmp0
;
1385 case TGSI_OPCODE_FLR
:
1386 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1387 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1388 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1392 case TGSI_OPCODE_ROUND
:
1393 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1394 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1395 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1399 case TGSI_OPCODE_EX2
: {
1400 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1401 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1402 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1403 dst0
[chan_index
] = tmp0
;
1408 case TGSI_OPCODE_LG2
:
1409 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1410 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1411 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1412 dst0
[chan_index
] = tmp0
;
1416 case TGSI_OPCODE_POW
:
1417 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1418 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1419 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1420 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1421 dst0
[chan_index
] = res
;
1425 case TGSI_OPCODE_XPD
:
1426 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1427 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1428 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1429 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1431 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1432 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1433 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1434 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1436 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1438 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1440 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1441 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1442 dst0
[CHAN_X
] = tmp2
;
1444 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1445 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1446 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1447 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1449 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1450 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1451 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1452 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1453 dst0
[CHAN_Y
] = tmp3
;
1455 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1456 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1457 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1458 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1459 dst0
[CHAN_Z
] = tmp5
;
1461 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1462 dst0
[CHAN_W
] = bld
->base
.one
;
1466 case TGSI_OPCODE_ABS
:
1467 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1468 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1469 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1473 case TGSI_OPCODE_RCC
:
1478 case TGSI_OPCODE_DPH
:
1479 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1480 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1481 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1482 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1483 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1484 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1485 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1486 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1487 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1488 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1489 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1490 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1491 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1492 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1493 dst0
[chan_index
] = tmp0
;
1497 case TGSI_OPCODE_COS
:
1498 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1499 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1500 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1501 dst0
[chan_index
] = tmp0
;
1505 case TGSI_OPCODE_DDX
:
1506 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1507 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1511 case TGSI_OPCODE_DDY
:
1512 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1513 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1517 case TGSI_OPCODE_KILP
:
1518 /* predicated kill */
1519 emit_kilp( bld
, inst
);
1522 case TGSI_OPCODE_KIL
:
1523 /* conditional kill */
1524 emit_kil( bld
, inst
);
1527 case TGSI_OPCODE_PK2H
:
1531 case TGSI_OPCODE_PK2US
:
1535 case TGSI_OPCODE_PK4B
:
1539 case TGSI_OPCODE_PK4UB
:
1543 case TGSI_OPCODE_RFL
:
1547 case TGSI_OPCODE_SEQ
:
1548 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1549 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1550 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1551 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1552 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1556 case TGSI_OPCODE_SFL
:
1557 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1558 dst0
[chan_index
] = bld
->base
.zero
;
1562 case TGSI_OPCODE_SGT
:
1563 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1564 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1565 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1566 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1567 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1571 case TGSI_OPCODE_SIN
:
1572 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1573 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1574 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1575 dst0
[chan_index
] = tmp0
;
1579 case TGSI_OPCODE_SLE
:
1580 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1581 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1582 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1583 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1584 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1588 case TGSI_OPCODE_SNE
:
1589 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1590 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1591 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1592 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_NOTEQUAL
, src0
, src1
);
1593 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1597 case TGSI_OPCODE_STR
:
1598 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1599 dst0
[chan_index
] = bld
->base
.one
;
1603 case TGSI_OPCODE_TEX
:
1604 emit_tex( bld
, inst
, TEX_MODIFIER_NONE
, dst0
);
1607 case TGSI_OPCODE_TXD
:
1608 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_DERIV
, dst0
);
1611 case TGSI_OPCODE_UP2H
:
1617 case TGSI_OPCODE_UP2US
:
1623 case TGSI_OPCODE_UP4B
:
1629 case TGSI_OPCODE_UP4UB
:
1635 case TGSI_OPCODE_X2D
:
1641 case TGSI_OPCODE_ARA
:
1647 case TGSI_OPCODE_ARR
:
1648 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1649 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1650 tmp0
= lp_build_round(&bld
->base
, tmp0
);
1651 dst0
[chan_index
] = tmp0
;
1655 case TGSI_OPCODE_BRA
:
1661 case TGSI_OPCODE_CAL
:
1662 lp_exec_mask_call(&bld
->exec_mask
,
1668 case TGSI_OPCODE_RET
:
1669 lp_exec_mask_ret(&bld
->exec_mask
, pc
);
1672 case TGSI_OPCODE_END
:
1676 case TGSI_OPCODE_SSG
:
1677 /* TGSI_OPCODE_SGN */
1678 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1679 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1680 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1684 case TGSI_OPCODE_CMP
:
1685 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1686 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1687 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1688 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1689 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1690 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1694 case TGSI_OPCODE_SCS
:
1695 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1696 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1697 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1699 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1700 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1701 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1703 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1704 dst0
[CHAN_Z
] = bld
->base
.zero
;
1706 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1707 dst0
[CHAN_W
] = bld
->base
.one
;
1711 case TGSI_OPCODE_TXB
:
1712 emit_tex( bld
, inst
, TEX_MODIFIER_LOD_BIAS
, dst0
);
1715 case TGSI_OPCODE_NRM
:
1717 case TGSI_OPCODE_NRM4
:
1718 /* 3 or 4-component normalization */
1720 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1722 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1723 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1724 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1725 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1727 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1730 /* xmm0 = src.x * src.x */
1731 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1732 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1735 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1738 /* xmm0 = xmm0 + src.y * src.y */
1739 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1740 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1743 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1744 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1747 /* xmm0 = xmm0 + src.z * src.z */
1748 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1749 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1752 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1753 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1757 /* xmm0 = xmm0 + src.w * src.w */
1758 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1759 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1762 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1763 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1766 /* xmm1 = 1 / sqrt(xmm0) */
1767 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1769 /* dst.x = xmm1 * src.x */
1770 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1771 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1774 /* dst.y = xmm1 * src.y */
1775 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1776 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1779 /* dst.z = xmm1 * src.z */
1780 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1781 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1784 /* dst.w = xmm1 * src.w */
1785 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1786 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1791 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1792 dst0
[CHAN_W
] = bld
->base
.one
;
1797 case TGSI_OPCODE_DIV
:
1803 case TGSI_OPCODE_DP2
:
1804 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1805 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1806 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1807 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1808 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1809 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1810 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1811 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1812 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1816 case TGSI_OPCODE_TXL
:
1817 emit_tex( bld
, inst
, TEX_MODIFIER_EXPLICIT_LOD
, dst0
);
1820 case TGSI_OPCODE_TXP
:
1821 emit_tex( bld
, inst
, TEX_MODIFIER_PROJECTED
, dst0
);
1824 case TGSI_OPCODE_BRK
:
1825 lp_exec_break(&bld
->exec_mask
);
1828 case TGSI_OPCODE_IF
:
1829 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1830 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
1831 tmp0
, bld
->base
.zero
);
1832 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1835 case TGSI_OPCODE_BGNLOOP
:
1836 lp_exec_bgnloop(&bld
->exec_mask
);
1839 case TGSI_OPCODE_BGNSUB
:
1840 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1843 case TGSI_OPCODE_ELSE
:
1844 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1847 case TGSI_OPCODE_ENDIF
:
1848 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1851 case TGSI_OPCODE_ENDLOOP
:
1852 lp_exec_endloop(&bld
->exec_mask
);
1855 case TGSI_OPCODE_ENDSUB
:
1856 lp_exec_mask_endsub(&bld
->exec_mask
, pc
);
1859 case TGSI_OPCODE_PUSHA
:
1865 case TGSI_OPCODE_POPA
:
1871 case TGSI_OPCODE_CEIL
:
1872 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1873 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1874 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1878 case TGSI_OPCODE_I2F
:
1884 case TGSI_OPCODE_NOT
:
1890 case TGSI_OPCODE_TRUNC
:
1891 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1892 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1893 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1897 case TGSI_OPCODE_SHL
:
1903 case TGSI_OPCODE_ISHR
:
1909 case TGSI_OPCODE_AND
:
1915 case TGSI_OPCODE_OR
:
1921 case TGSI_OPCODE_MOD
:
1927 case TGSI_OPCODE_XOR
:
1933 case TGSI_OPCODE_SAD
:
1939 case TGSI_OPCODE_TXF
:
1945 case TGSI_OPCODE_TXQ
:
1951 case TGSI_OPCODE_CONT
:
1952 lp_exec_continue(&bld
->exec_mask
);
1955 case TGSI_OPCODE_EMIT
:
1959 case TGSI_OPCODE_ENDPRIM
:
1963 case TGSI_OPCODE_NOP
:
1971 LLVMValueRef pred
[NUM_CHANNELS
];
1973 emit_fetch_predicate( bld
, inst
, pred
);
1975 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1976 emit_store( bld
, inst
, 0, chan_index
, pred
[chan_index
], dst0
[chan_index
]);
1985 lp_build_tgsi_soa(LLVMBuilderRef builder
,
1986 const struct tgsi_token
*tokens
,
1987 struct lp_type type
,
1988 struct lp_build_mask_context
*mask
,
1989 LLVMValueRef consts_ptr
,
1990 const LLVMValueRef
*pos
,
1991 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
1992 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1993 struct lp_build_sampler_soa
*sampler
,
1994 const struct tgsi_shader_info
*info
)
1996 struct lp_build_tgsi_soa_context bld
;
1997 struct tgsi_parse_context parse
;
1998 uint num_immediates
= 0;
1999 uint num_instructions
= 0;
2003 /* Setup build context */
2004 memset(&bld
, 0, sizeof bld
);
2005 lp_build_context_init(&bld
.base
, builder
, type
);
2006 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
2009 bld
.inputs
= inputs
;
2010 bld
.outputs
= outputs
;
2011 bld
.consts_ptr
= consts_ptr
;
2012 bld
.sampler
= sampler
;
2013 bld
.has_indirect_addressing
= info
->opcode_count
[TGSI_OPCODE_ARR
] > 0 ||
2014 info
->opcode_count
[TGSI_OPCODE_ARL
] > 0;
2015 bld
.instructions
= (struct tgsi_full_instruction
*)
2016 MALLOC( LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
) );
2017 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
2019 if (!bld
.instructions
) {
2023 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
2025 tgsi_parse_init( &parse
, tokens
);
2027 while( !tgsi_parse_end_of_tokens( &parse
) ) {
2028 tgsi_parse_token( &parse
);
2030 switch( parse
.FullToken
.Token
.Type
) {
2031 case TGSI_TOKEN_TYPE_DECLARATION
:
2032 /* Inputs already interpolated */
2033 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
2036 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2038 /* save expanded instruction */
2039 if (num_instructions
== bld
.max_instructions
) {
2040 bld
.instructions
= REALLOC(bld
.instructions
,
2041 bld
.max_instructions
2042 * sizeof(struct tgsi_full_instruction
),
2043 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
2044 * sizeof(struct tgsi_full_instruction
));
2045 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
2048 memcpy(bld
.instructions
+ num_instructions
,
2049 &parse
.FullToken
.FullInstruction
,
2050 sizeof(bld
.instructions
[0]));
2057 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2058 /* simply copy the immediate values into the next immediates[] slot */
2060 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
2062 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
2063 for( i
= 0; i
< size
; ++i
)
2064 bld
.immediates
[num_immediates
][i
] =
2065 lp_build_const_vec(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
2066 for( i
= size
; i
< 4; ++i
)
2067 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
2072 case TGSI_TOKEN_TYPE_PROPERTY
:
2081 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
2082 const struct tgsi_opcode_info
*opcode_info
=
2083 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
2084 if (!emit_instruction( &bld
, instr
, opcode_info
, &pc
))
2085 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2086 opcode_info
->mnemonic
);
2090 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
2091 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2092 debug_printf("11111111111111111111111111111 \n");
2093 tgsi_dump(tokens
, 0);
2094 lp_debug_dump_value(function
);
2095 debug_printf("2222222222222222222222222222 \n");
2097 tgsi_parse_free( &parse
);
2100 LLVMModuleRef module
= LLVMGetGlobalParent(
2101 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
2102 LLVMDumpModule(module
);
2106 FREE( bld
.instructions
);