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
= LLVMBuildAnd(mask
->bld
->builder
,
207 lp_exec_mask_update(mask
);
210 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
212 LLVMValueRef prev_mask
;
213 LLVMValueRef inv_mask
;
215 assert(mask
->cond_stack_size
);
216 prev_mask
= mask
->cond_stack
[mask
->cond_stack_size
- 1];
217 if (mask
->cond_stack_size
== 1) {
218 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
221 inv_mask
= LLVMBuildNot(mask
->bld
->builder
, mask
->cond_mask
, "");
223 mask
->cond_mask
= LLVMBuildAnd(mask
->bld
->builder
,
226 lp_exec_mask_update(mask
);
229 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
231 assert(mask
->cond_stack_size
);
232 mask
->cond_mask
= mask
->cond_stack
[--mask
->cond_stack_size
];
233 lp_exec_mask_update(mask
);
236 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
238 if (mask
->loop_stack_size
== 0) {
239 assert(mask
->loop_block
== NULL
);
240 assert(mask
->cont_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
241 assert(mask
->break_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
242 assert(mask
->break_var
== NULL
);
245 assert(mask
->loop_stack_size
< LP_MAX_TGSI_NESTING
);
247 mask
->loop_stack
[mask
->loop_stack_size
].loop_block
= mask
->loop_block
;
248 mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
249 mask
->loop_stack
[mask
->loop_stack_size
].break_mask
= mask
->break_mask
;
250 mask
->loop_stack
[mask
->loop_stack_size
].break_var
= mask
->break_var
;
251 ++mask
->loop_stack_size
;
253 mask
->break_var
= lp_build_alloca(mask
->bld
->builder
, mask
->int_vec_type
, "");
254 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
256 mask
->loop_block
= lp_build_insert_new_block(mask
->bld
->builder
, "bgnloop");
257 LLVMBuildBr(mask
->bld
->builder
, mask
->loop_block
);
258 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, mask
->loop_block
);
260 mask
->break_mask
= LLVMBuildLoad(mask
->bld
->builder
, mask
->break_var
, "");
262 lp_exec_mask_update(mask
);
265 static void lp_exec_break(struct lp_exec_mask
*mask
)
267 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
271 mask
->break_mask
= LLVMBuildAnd(mask
->bld
->builder
,
273 exec_mask
, "break_full");
275 lp_exec_mask_update(mask
);
278 static void lp_exec_continue(struct lp_exec_mask
*mask
)
280 LLVMValueRef exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
284 mask
->cont_mask
= LLVMBuildAnd(mask
->bld
->builder
,
288 lp_exec_mask_update(mask
);
292 static void lp_exec_endloop(struct lp_exec_mask
*mask
)
294 LLVMBasicBlockRef endloop
;
295 LLVMTypeRef reg_type
= LLVMIntType(mask
->bld
->type
.width
*
296 mask
->bld
->type
.length
);
299 assert(mask
->break_mask
);
302 * Restore the cont_mask, but don't pop
304 assert(mask
->loop_stack_size
);
305 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
- 1].cont_mask
;
306 lp_exec_mask_update(mask
);
309 * Unlike the continue mask, the break_mask must be preserved across loop
312 LLVMBuildStore(mask
->bld
->builder
, mask
->break_mask
, mask
->break_var
);
314 /* i1cond = (mask == 0) */
315 i1cond
= LLVMBuildICmp(
318 LLVMBuildBitCast(mask
->bld
->builder
, mask
->exec_mask
, reg_type
, ""),
319 LLVMConstNull(reg_type
), "");
321 endloop
= lp_build_insert_new_block(mask
->bld
->builder
, "endloop");
323 LLVMBuildCondBr(mask
->bld
->builder
,
324 i1cond
, mask
->loop_block
, endloop
);
326 LLVMPositionBuilderAtEnd(mask
->bld
->builder
, endloop
);
328 assert(mask
->loop_stack_size
);
329 --mask
->loop_stack_size
;
330 mask
->loop_block
= mask
->loop_stack
[mask
->loop_stack_size
].loop_block
;
331 mask
->cont_mask
= mask
->loop_stack
[mask
->loop_stack_size
].cont_mask
;
332 mask
->break_mask
= mask
->loop_stack
[mask
->loop_stack_size
].break_mask
;
333 mask
->break_var
= mask
->loop_stack
[mask
->loop_stack_size
].break_var
;
335 lp_exec_mask_update(mask
);
338 /* stores val into an address pointed to by dst.
339 * mask->exec_mask is used to figure out which bits of val
340 * should be stored into the address
341 * (0 means don't store this bit, 1 means do store).
343 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
348 /* Mix the predicate and execution mask */
349 if (mask
->has_mask
) {
351 pred
= LLVMBuildAnd(mask
->bld
->builder
, pred
, mask
->exec_mask
, "");
353 pred
= mask
->exec_mask
;
358 LLVMValueRef real_val
, dst_val
;
360 dst_val
= LLVMBuildLoad(mask
->bld
->builder
, dst
, "");
361 real_val
= lp_build_select(mask
->bld
,
365 LLVMBuildStore(mask
->bld
->builder
, real_val
, dst
);
367 LLVMBuildStore(mask
->bld
->builder
, val
, dst
);
370 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
374 assert(mask
->call_stack_size
< LP_MAX_TGSI_NESTING
);
375 mask
->call_stack
[mask
->call_stack_size
].pc
= *pc
;
376 mask
->call_stack
[mask
->call_stack_size
].ret_mask
= mask
->ret_mask
;
377 mask
->call_stack_size
++;
381 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
383 LLVMValueRef exec_mask
;
385 if (mask
->call_stack_size
== 0) {
386 /* returning from main() */
390 exec_mask
= LLVMBuildNot(mask
->bld
->builder
,
394 mask
->ret_mask
= LLVMBuildAnd(mask
->bld
->builder
,
396 exec_mask
, "ret_full");
398 lp_exec_mask_update(mask
);
401 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
405 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
407 assert(mask
->call_stack_size
);
408 mask
->call_stack_size
--;
409 *pc
= mask
->call_stack
[mask
->call_stack_size
].pc
;
410 mask
->ret_mask
= mask
->call_stack
[mask
->call_stack_size
].ret_mask
;
411 lp_exec_mask_update(mask
);
416 * Return pointer to a temporary register channel (src or dest).
417 * Note that indirect addressing cannot be handled here.
418 * \param index which temporary register
419 * \param chan which channel of the temp register.
422 get_temp_ptr(struct lp_build_tgsi_soa_context
*bld
,
427 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
428 LLVMValueRef lindex
= lp_build_const_int32(index
* 4 + chan
);
429 return LLVMBuildGEP(bld
->base
.builder
, bld
->temps_array
, &lindex
, 1, "");
432 return bld
->temps
[index
][chan
];
439 * XXX the lp_build_gather() function should be capable of doing this
440 * with a little work.
443 build_gather(struct lp_build_tgsi_soa_context
*bld
,
444 LLVMValueRef base_ptr
,
445 LLVMValueRef indexes
)
447 LLVMValueRef res
= bld
->base
.undef
;
451 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
453 for (i
= 0; i
< bld
->base
.type
.length
; i
++) {
454 LLVMValueRef ii
= LLVMConstInt(LLVMInt32Type(), i
, 0);
455 LLVMValueRef index
= LLVMBuildExtractElement(bld
->base
.builder
,
457 LLVMValueRef scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, base_ptr
,
459 LLVMValueRef scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
461 res
= LLVMBuildInsertElement(bld
->base
.builder
, res
, scalar
, ii
, "");
469 * Read the current value of the ADDR register, convert the floats to
470 * ints, multiply by four and return the vector of offsets.
471 * The offsets will be used to index into the constant buffer or
472 * temporary register file.
475 get_indirect_offsets(struct lp_build_tgsi_soa_context
*bld
,
476 const struct tgsi_src_register
*indirect_reg
)
478 /* always use X component of address register */
479 const int x
= indirect_reg
->SwizzleX
;
480 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
481 uint swizzle
= tgsi_util_get_src_register_swizzle(indirect_reg
, x
);
482 LLVMValueRef vec4
= lp_build_const_int_vec(bld
->int_bld
.type
, 4);
483 LLVMValueRef addr_vec
;
485 addr_vec
= LLVMBuildLoad(bld
->base
.builder
,
486 bld
->addr
[indirect_reg
->Index
][swizzle
],
489 /* for indexing we want integers */
490 addr_vec
= LLVMBuildFPToSI(bld
->base
.builder
, addr_vec
,
493 /* addr_vec = addr_vec * 4 */
494 addr_vec
= lp_build_mul(&bld
->int_bld
, addr_vec
, vec4
);
505 struct lp_build_tgsi_soa_context
*bld
,
506 const struct tgsi_full_instruction
*inst
,
508 const unsigned chan_index
)
510 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
511 const unsigned swizzle
=
512 tgsi_util_get_full_src_register_swizzle(reg
, chan_index
);
514 LLVMValueRef addr_vec
= NULL
;
517 assert(0 && "invalid swizzle in emit_fetch()");
518 return bld
->base
.undef
;
521 if (reg
->Register
.Indirect
) {
522 assert(bld
->indirect_files
);
523 addr_vec
= get_indirect_offsets(bld
, ®
->Indirect
);
526 switch (reg
->Register
.File
) {
527 case TGSI_FILE_CONSTANT
:
528 if (reg
->Register
.Indirect
) {
529 LLVMValueRef index_vec
; /* index into the const buffer */
531 assert(bld
->indirect_files
& (1 << TGSI_FILE_CONSTANT
));
533 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
534 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
535 reg
->Register
.Index
* 4 + swizzle
);
537 /* index_vec = index_vec + addr_vec */
538 index_vec
= lp_build_add(&bld
->int_bld
, index_vec
, addr_vec
);
540 /* Gather values from the constant buffer */
541 res
= build_gather(bld
, bld
->consts_ptr
, index_vec
);
544 LLVMValueRef index
; /* index into the const buffer */
545 LLVMValueRef scalar
, scalar_ptr
;
547 index
= lp_build_const_int32(reg
->Register
.Index
*4 + swizzle
);
549 scalar_ptr
= LLVMBuildGEP(bld
->base
.builder
, bld
->consts_ptr
,
551 scalar
= LLVMBuildLoad(bld
->base
.builder
, scalar_ptr
, "");
553 res
= lp_build_broadcast_scalar(&bld
->base
, scalar
);
557 case TGSI_FILE_IMMEDIATE
:
558 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
562 case TGSI_FILE_INPUT
:
563 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
567 case TGSI_FILE_TEMPORARY
:
568 if (reg
->Register
.Indirect
) {
569 LLVMValueRef vec_len
=
570 lp_build_const_int_vec(bld
->int_bld
.type
, bld
->base
.type
.length
);
571 LLVMValueRef index_vec
; /* index into the const buffer */
572 LLVMValueRef temps_array
;
573 LLVMTypeRef float4_ptr_type
;
575 assert(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
));
577 /* index_vec = broadcast(reg->Register.Index * 4 + swizzle) */
578 index_vec
= lp_build_const_int_vec(bld
->int_bld
.type
,
579 reg
->Register
.Index
* 4 + swizzle
);
581 /* index_vec += addr_vec */
582 index_vec
= lp_build_add(&bld
->int_bld
, index_vec
, addr_vec
);
584 /* index_vec *= vector_length */
585 index_vec
= lp_build_mul(&bld
->int_bld
, index_vec
, vec_len
);
587 /* cast temps_array pointer to float* */
588 float4_ptr_type
= LLVMPointerType(LLVMFloatType(), 0);
589 temps_array
= LLVMBuildBitCast(bld
->int_bld
.builder
, bld
->temps_array
,
590 float4_ptr_type
, "");
592 /* Gather values from the temporary register array */
593 res
= build_gather(bld
, temps_array
, index_vec
);
596 LLVMValueRef temp_ptr
;
597 temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
, swizzle
);
598 res
= LLVMBuildLoad(bld
->base
.builder
, temp_ptr
, "");
600 return bld
->base
.undef
;
605 assert(0 && "invalid src register in emit_fetch()");
606 return bld
->base
.undef
;
609 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
610 case TGSI_UTIL_SIGN_CLEAR
:
611 res
= lp_build_abs( &bld
->base
, res
);
614 case TGSI_UTIL_SIGN_SET
:
615 res
= lp_build_abs( &bld
->base
, res
);
617 case TGSI_UTIL_SIGN_TOGGLE
:
618 res
= lp_build_negate( &bld
->base
, res
);
621 case TGSI_UTIL_SIGN_KEEP
:
630 * Register fetch with derivatives.
634 struct lp_build_tgsi_soa_context
*bld
,
635 const struct tgsi_full_instruction
*inst
,
637 const unsigned chan_index
,
644 src
= emit_fetch(bld
, inst
, index
, chan_index
);
649 /* TODO: use interpolation coeffs for inputs */
652 *ddx
= lp_build_ddx(&bld
->base
, src
);
655 *ddy
= lp_build_ddy(&bld
->base
, src
);
663 emit_fetch_predicate(
664 struct lp_build_tgsi_soa_context
*bld
,
665 const struct tgsi_full_instruction
*inst
,
669 unsigned char swizzles
[4];
670 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
674 if (!inst
->Instruction
.Predicate
) {
675 FOR_EACH_CHANNEL( chan
) {
681 swizzles
[0] = inst
->Predicate
.SwizzleX
;
682 swizzles
[1] = inst
->Predicate
.SwizzleY
;
683 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
684 swizzles
[3] = inst
->Predicate
.SwizzleW
;
686 index
= inst
->Predicate
.Index
;
687 assert(index
< LP_MAX_TGSI_PREDS
);
689 FOR_EACH_CHANNEL( chan
) {
690 unsigned swizzle
= swizzles
[chan
];
693 * Only fetch the predicate register channels that are actually listed
696 if (!unswizzled
[swizzle
]) {
697 value
= LLVMBuildLoad(bld
->base
.builder
,
698 bld
->preds
[index
][swizzle
], "");
701 * Convert the value to an integer mask.
703 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
704 * is needlessly causing two comparisons due to storing the intermediate
705 * result as float vector instead of an integer mask vector.
707 value
= lp_build_compare(bld
->base
.builder
,
712 if (inst
->Predicate
.Negate
) {
713 value
= LLVMBuildNot(bld
->base
.builder
, value
, "");
716 unswizzled
[swizzle
] = value
;
718 value
= unswizzled
[swizzle
];
731 struct lp_build_tgsi_soa_context
*bld
,
732 const struct tgsi_full_instruction
*inst
,
738 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
739 LLVMValueRef addr
= NULL
;
741 switch( inst
->Instruction
.Saturate
) {
745 case TGSI_SAT_ZERO_ONE
:
746 value
= lp_build_max(&bld
->base
, value
, bld
->base
.zero
);
747 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
750 case TGSI_SAT_MINUS_PLUS_ONE
:
751 value
= lp_build_max(&bld
->base
, value
, lp_build_const_vec(bld
->base
.type
, -1.0));
752 value
= lp_build_min(&bld
->base
, value
, bld
->base
.one
);
759 if (reg
->Register
.Indirect
) {
760 /* XXX use get_indirect_offsets() here eventually */
761 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->base
.type
);
762 unsigned swizzle
= tgsi_util_get_src_register_swizzle( ®
->Indirect
, chan_index
);
764 assert(bld
->indirect_files
);
766 addr
= LLVMBuildLoad(bld
->base
.builder
,
767 bld
->addr
[reg
->Indirect
.Index
][swizzle
],
769 /* for indexing we want integers */
770 addr
= LLVMBuildFPToSI(bld
->base
.builder
, addr
,
772 addr
= LLVMBuildExtractElement(bld
->base
.builder
,
773 addr
, LLVMConstInt(LLVMInt32Type(), 0, 0),
775 addr
= LLVMBuildMul(bld
->base
.builder
,
776 addr
, LLVMConstInt(LLVMInt32Type(), 4, 0),
780 switch( reg
->Register
.File
) {
781 case TGSI_FILE_OUTPUT
:
782 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
783 bld
->outputs
[reg
->Register
.Index
][chan_index
]);
786 case TGSI_FILE_TEMPORARY
:
787 if (reg
->Register
.Indirect
) {
788 /* XXX not done yet */
789 debug_printf("WARNING: LLVM scatter store of temp regs"
790 " not implemented\n");
793 LLVMValueRef temp_ptr
= get_temp_ptr(bld
, reg
->Register
.Index
,
795 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
, temp_ptr
);
799 case TGSI_FILE_ADDRESS
:
800 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
801 bld
->addr
[reg
->Indirect
.Index
][chan_index
]);
804 case TGSI_FILE_PREDICATE
:
805 lp_exec_mask_store(&bld
->exec_mask
, pred
, value
,
806 bld
->preds
[reg
->Register
.Index
][chan_index
]);
816 * High-level instruction translators.
820 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
821 const struct tgsi_full_instruction
*inst
,
822 enum lp_build_tex_modifier modifier
,
826 LLVMValueRef lod_bias
, explicit_lod
;
827 LLVMValueRef oow
= NULL
;
828 LLVMValueRef coords
[3];
835 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
836 for (i
= 0; i
< 4; i
++) {
837 texel
[i
] = bld
->base
.undef
;
842 switch (inst
->Texture
.Texture
) {
843 case TGSI_TEXTURE_1D
:
846 case TGSI_TEXTURE_2D
:
847 case TGSI_TEXTURE_RECT
:
850 case TGSI_TEXTURE_SHADOW1D
:
851 case TGSI_TEXTURE_SHADOW2D
:
852 case TGSI_TEXTURE_SHADOWRECT
:
853 case TGSI_TEXTURE_3D
:
854 case TGSI_TEXTURE_CUBE
:
862 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
863 lod_bias
= emit_fetch( bld
, inst
, 0, 3 );
866 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
868 explicit_lod
= emit_fetch( bld
, inst
, 0, 3 );
875 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
876 oow
= emit_fetch( bld
, inst
, 0, 3 );
877 oow
= lp_build_rcp(&bld
->base
, oow
);
880 for (i
= 0; i
< num_coords
; i
++) {
881 coords
[i
] = emit_fetch( bld
, inst
, 0, i
);
882 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
883 coords
[i
] = lp_build_mul(&bld
->base
, coords
[i
], oow
);
885 for (i
= num_coords
; i
< 3; i
++) {
886 coords
[i
] = bld
->base
.undef
;
889 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
890 for (i
= 0; i
< num_coords
; i
++) {
891 ddx
[i
] = emit_fetch( bld
, inst
, 1, i
);
892 ddy
[i
] = emit_fetch( bld
, inst
, 2, i
);
894 unit
= inst
->Src
[3].Register
.Index
;
896 for (i
= 0; i
< num_coords
; i
++) {
897 ddx
[i
] = lp_build_ddx( &bld
->base
, coords
[i
] );
898 ddy
[i
] = lp_build_ddy( &bld
->base
, coords
[i
] );
900 unit
= inst
->Src
[1].Register
.Index
;
902 for (i
= num_coords
; i
< 3; i
++) {
903 ddx
[i
] = bld
->base
.undef
;
904 ddy
[i
] = bld
->base
.undef
;
907 bld
->sampler
->emit_fetch_texel(bld
->sampler
,
910 unit
, num_coords
, coords
,
912 lod_bias
, explicit_lod
,
918 * Kill fragment if any of the src register values are negative.
922 struct lp_build_tgsi_soa_context
*bld
,
923 const struct tgsi_full_instruction
*inst
)
925 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
926 LLVMValueRef terms
[NUM_CHANNELS
];
930 memset(&terms
, 0, sizeof terms
);
932 FOR_EACH_CHANNEL( chan_index
) {
935 /* Unswizzle channel */
936 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
938 /* Check if the component has not been already tested. */
939 assert(swizzle
< NUM_CHANNELS
);
940 if( !terms
[swizzle
] )
941 /* TODO: change the comparison operator instead of setting the sign */
942 terms
[swizzle
] = emit_fetch(bld
, inst
, 0, chan_index
);
946 FOR_EACH_CHANNEL( chan_index
) {
947 if(terms
[chan_index
]) {
948 LLVMValueRef chan_mask
;
951 * If term < 0 then mask = 0 else mask = ~0.
953 chan_mask
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->base
.zero
);
956 mask
= LLVMBuildAnd(bld
->base
.builder
, mask
, chan_mask
, "");
963 lp_build_mask_update(bld
->mask
, mask
);
968 * Predicated fragment kill.
969 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
970 * The only predication is the execution mask which will apply if
971 * we're inside a loop or conditional.
974 emit_kilp(struct lp_build_tgsi_soa_context
*bld
,
975 const struct tgsi_full_instruction
*inst
)
979 /* For those channels which are "alive", disable fragment shader
982 if (bld
->exec_mask
.has_mask
) {
983 mask
= LLVMBuildNot(bld
->base
.builder
, bld
->exec_mask
.exec_mask
, "kilp");
986 mask
= bld
->base
.zero
;
989 lp_build_mask_update(bld
->mask
, mask
);
994 struct lp_build_tgsi_soa_context
*bld
,
995 const struct tgsi_full_declaration
*decl
)
997 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->base
.type
);
999 unsigned first
= decl
->Range
.First
;
1000 unsigned last
= decl
->Range
.Last
;
1003 for (idx
= first
; idx
<= last
; ++idx
) {
1004 switch (decl
->Declaration
.File
) {
1005 case TGSI_FILE_TEMPORARY
:
1006 assert(idx
< LP_MAX_TGSI_TEMPS
);
1007 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
1008 LLVMValueRef array_size
= LLVMConstInt(LLVMInt32Type(),
1010 bld
->temps_array
= lp_build_array_alloca(bld
->base
.builder
,
1011 vec_type
, array_size
, "");
1013 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1014 bld
->temps
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1019 case TGSI_FILE_OUTPUT
:
1020 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1021 bld
->outputs
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1025 case TGSI_FILE_ADDRESS
:
1026 assert(idx
< LP_MAX_TGSI_ADDRS
);
1027 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1028 bld
->addr
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1032 case TGSI_FILE_PREDICATE
:
1033 assert(idx
< LP_MAX_TGSI_PREDS
);
1034 for (i
= 0; i
< NUM_CHANNELS
; i
++)
1035 bld
->preds
[idx
][i
] = lp_build_alloca(bld
->base
.builder
,
1040 /* don't need to declare other vars */
1048 * Emit LLVM for one TGSI instruction.
1049 * \param return TRUE for success, FALSE otherwise
1053 struct lp_build_tgsi_soa_context
*bld
,
1054 const struct tgsi_full_instruction
*inst
,
1055 const struct tgsi_opcode_info
*info
,
1058 unsigned chan_index
;
1059 LLVMValueRef src0
, src1
, src2
;
1060 LLVMValueRef tmp0
, tmp1
, tmp2
;
1061 LLVMValueRef tmp3
= NULL
;
1062 LLVMValueRef tmp4
= NULL
;
1063 LLVMValueRef tmp5
= NULL
;
1064 LLVMValueRef tmp6
= NULL
;
1065 LLVMValueRef tmp7
= NULL
;
1067 LLVMValueRef dst0
[NUM_CHANNELS
];
1070 * Stores and write masks are handled in a general fashion after the long
1071 * instruction opcode switch statement.
1073 * Although not stricitly necessary, we avoid generating instructions for
1074 * channels which won't be stored, in cases where's that easy. For some
1075 * complex instructions, like texture sampling, it is more convenient to
1076 * assume a full writemask and then let LLVM optimization passes eliminate
1082 assert(info
->num_dst
<= 1);
1083 if (info
->num_dst
) {
1084 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1085 dst0
[chan_index
] = bld
->base
.undef
;
1089 switch (inst
->Instruction
.Opcode
) {
1090 case TGSI_OPCODE_ARL
:
1091 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1092 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1093 tmp0
= lp_build_floor(&bld
->base
, tmp0
);
1094 dst0
[chan_index
] = tmp0
;
1098 case TGSI_OPCODE_MOV
:
1099 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1100 dst0
[chan_index
] = emit_fetch( bld
, inst
, 0, chan_index
);
1104 case TGSI_OPCODE_LIT
:
1105 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ) {
1106 dst0
[CHAN_X
] = bld
->base
.one
;
1108 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1109 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1110 dst0
[CHAN_Y
] = lp_build_max( &bld
->base
, src0
, bld
->base
.zero
);
1112 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1113 /* XMM[1] = SrcReg[0].yyyy */
1114 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1115 /* XMM[1] = max(XMM[1], 0) */
1116 tmp1
= lp_build_max( &bld
->base
, tmp1
, bld
->base
.zero
);
1117 /* XMM[2] = SrcReg[0].wwww */
1118 tmp2
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1119 tmp1
= lp_build_pow( &bld
->base
, tmp1
, tmp2
);
1120 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1121 tmp2
= lp_build_cmp(&bld
->base
, PIPE_FUNC_GREATER
, tmp0
, bld
->base
.zero
);
1122 dst0
[CHAN_Z
] = lp_build_select(&bld
->base
, tmp2
, tmp1
, bld
->base
.zero
);
1124 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) ) {
1125 dst0
[CHAN_W
] = bld
->base
.one
;
1129 case TGSI_OPCODE_RCP
:
1130 /* TGSI_OPCODE_RECIP */
1131 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1132 res
= lp_build_rcp(&bld
->base
, src0
);
1133 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1134 dst0
[chan_index
] = res
;
1138 case TGSI_OPCODE_RSQ
:
1139 /* TGSI_OPCODE_RECIPSQRT */
1140 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1141 src0
= lp_build_abs(&bld
->base
, src0
);
1142 res
= lp_build_rsqrt(&bld
->base
, src0
);
1143 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1144 dst0
[chan_index
] = res
;
1148 case TGSI_OPCODE_EXP
:
1149 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1150 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1151 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1152 LLVMValueRef
*p_exp2_int_part
= NULL
;
1153 LLVMValueRef
*p_frac_part
= NULL
;
1154 LLVMValueRef
*p_exp2
= NULL
;
1156 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1158 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1159 p_exp2_int_part
= &tmp0
;
1160 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1161 p_frac_part
= &tmp1
;
1162 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1165 lp_build_exp2_approx(&bld
->base
, src0
, p_exp2_int_part
, p_frac_part
, p_exp2
);
1167 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1168 dst0
[CHAN_X
] = tmp0
;
1169 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1170 dst0
[CHAN_Y
] = tmp1
;
1171 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1172 dst0
[CHAN_Z
] = tmp2
;
1175 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1176 dst0
[CHAN_W
] = bld
->base
.one
;
1180 case TGSI_OPCODE_LOG
:
1181 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1182 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1183 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
)) {
1184 LLVMValueRef
*p_floor_log2
= NULL
;
1185 LLVMValueRef
*p_exp
= NULL
;
1186 LLVMValueRef
*p_log2
= NULL
;
1188 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1189 src0
= lp_build_abs( &bld
->base
, src0
);
1191 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1192 p_floor_log2
= &tmp0
;
1193 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
))
1195 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1198 lp_build_log2_approx(&bld
->base
, src0
, p_exp
, p_floor_log2
, p_log2
);
1200 /* dst.x = floor(lg2(abs(src.x))) */
1201 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
))
1202 dst0
[CHAN_X
] = tmp0
;
1203 /* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1204 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
)) {
1205 dst0
[CHAN_Y
] = lp_build_div( &bld
->base
, src0
, tmp1
);
1207 /* dst.z = lg2(abs(src.x)) */
1208 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
))
1209 dst0
[CHAN_Z
] = tmp2
;
1212 if (IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
)) {
1213 dst0
[CHAN_W
] = bld
->base
.one
;
1217 case TGSI_OPCODE_MUL
:
1218 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1219 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1220 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1221 dst0
[chan_index
] = lp_build_mul(&bld
->base
, src0
, src1
);
1225 case TGSI_OPCODE_ADD
:
1226 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1227 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1228 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1229 dst0
[chan_index
] = lp_build_add(&bld
->base
, src0
, src1
);
1233 case TGSI_OPCODE_DP3
:
1234 /* TGSI_OPCODE_DOT3 */
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 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1247 dst0
[chan_index
] = tmp0
;
1251 case TGSI_OPCODE_DP4
:
1252 /* TGSI_OPCODE_DOT4 */
1253 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1254 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1255 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1256 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1257 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1258 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1259 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1260 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1261 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1262 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1263 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1264 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_W
);
1265 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1266 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1267 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1268 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1269 dst0
[chan_index
] = tmp0
;
1273 case TGSI_OPCODE_DST
:
1274 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1275 dst0
[CHAN_X
] = bld
->base
.one
;
1277 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1278 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1279 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1280 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1282 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1283 dst0
[CHAN_Z
] = emit_fetch( bld
, inst
, 0, CHAN_Z
);
1285 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1286 dst0
[CHAN_W
] = emit_fetch( bld
, inst
, 1, CHAN_W
);
1290 case TGSI_OPCODE_MIN
:
1291 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1292 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1293 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1294 dst0
[chan_index
] = lp_build_min( &bld
->base
, src0
, src1
);
1298 case TGSI_OPCODE_MAX
:
1299 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1300 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1301 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1302 dst0
[chan_index
] = lp_build_max( &bld
->base
, src0
, src1
);
1306 case TGSI_OPCODE_SLT
:
1307 /* TGSI_OPCODE_SETLT */
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 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, src1
);
1312 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1316 case TGSI_OPCODE_SGE
:
1317 /* TGSI_OPCODE_SETGE */
1318 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1319 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1320 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1321 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GEQUAL
, src0
, src1
);
1322 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1326 case TGSI_OPCODE_MAD
:
1327 /* TGSI_OPCODE_MADD */
1328 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1329 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1330 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1331 tmp2
= emit_fetch( bld
, inst
, 2, chan_index
);
1332 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1333 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp2
);
1334 dst0
[chan_index
] = tmp0
;
1338 case TGSI_OPCODE_SUB
:
1339 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1340 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1341 tmp1
= emit_fetch( bld
, inst
, 1, chan_index
);
1342 dst0
[chan_index
] = lp_build_sub( &bld
->base
, tmp0
, tmp1
);
1346 case TGSI_OPCODE_LRP
:
1347 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1348 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1349 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1350 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1351 tmp0
= lp_build_sub( &bld
->base
, src1
, src2
);
1352 tmp0
= lp_build_mul( &bld
->base
, src0
, tmp0
);
1353 dst0
[chan_index
] = lp_build_add( &bld
->base
, tmp0
, src2
);
1357 case TGSI_OPCODE_CND
:
1358 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1359 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1360 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1361 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1362 tmp1
= lp_build_const_vec(bld
->base
.type
, 0.5);
1363 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src2
, tmp1
);
1364 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src0
, src1
);
1368 case TGSI_OPCODE_DP2A
:
1369 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1370 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1371 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1372 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1373 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1374 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1375 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1376 tmp1
= emit_fetch( bld
, inst
, 2, CHAN_X
); /* xmm1 = src[2].x */
1377 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1378 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1379 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1383 case TGSI_OPCODE_FRC
:
1384 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1385 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1386 tmp0
= lp_build_floor(&bld
->base
, src0
);
1387 tmp0
= lp_build_sub(&bld
->base
, src0
, tmp0
);
1388 dst0
[chan_index
] = tmp0
;
1392 case TGSI_OPCODE_CLAMP
:
1393 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1394 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1395 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1396 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1397 tmp0
= lp_build_max(&bld
->base
, tmp0
, src1
);
1398 tmp0
= lp_build_min(&bld
->base
, tmp0
, src2
);
1399 dst0
[chan_index
] = tmp0
;
1403 case TGSI_OPCODE_FLR
:
1404 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1405 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1406 dst0
[chan_index
] = lp_build_floor(&bld
->base
, tmp0
);
1410 case TGSI_OPCODE_ROUND
:
1411 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1412 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1413 dst0
[chan_index
] = lp_build_round(&bld
->base
, tmp0
);
1417 case TGSI_OPCODE_EX2
: {
1418 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1419 tmp0
= lp_build_exp2( &bld
->base
, tmp0
);
1420 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1421 dst0
[chan_index
] = tmp0
;
1426 case TGSI_OPCODE_LG2
:
1427 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1428 tmp0
= lp_build_log2( &bld
->base
, tmp0
);
1429 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1430 dst0
[chan_index
] = tmp0
;
1434 case TGSI_OPCODE_POW
:
1435 src0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1436 src1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1437 res
= lp_build_pow( &bld
->base
, src0
, src1
);
1438 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1439 dst0
[chan_index
] = res
;
1443 case TGSI_OPCODE_XPD
:
1444 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1445 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ) {
1446 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1447 tmp3
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1449 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) ||
1450 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1451 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1452 tmp4
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1454 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1456 tmp2
= lp_build_mul( &bld
->base
, tmp2
, tmp1
);
1458 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1459 tmp2
= lp_build_sub( &bld
->base
, tmp2
, tmp5
);
1460 dst0
[CHAN_X
] = tmp2
;
1462 if( IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) ||
1463 IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) ) {
1464 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1465 tmp5
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1467 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1468 tmp3
= lp_build_mul( &bld
->base
, tmp3
, tmp2
);
1469 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp5
);
1470 tmp3
= lp_build_sub( &bld
->base
, tmp3
, tmp1
);
1471 dst0
[CHAN_Y
] = tmp3
;
1473 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1474 tmp5
= lp_build_mul( &bld
->base
, tmp5
, tmp4
);
1475 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp2
);
1476 tmp5
= lp_build_sub( &bld
->base
, tmp5
, tmp0
);
1477 dst0
[CHAN_Z
] = tmp5
;
1479 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1480 dst0
[CHAN_W
] = bld
->base
.one
;
1484 case TGSI_OPCODE_ABS
:
1485 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1486 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1487 dst0
[chan_index
] = lp_build_abs( &bld
->base
, tmp0
);
1491 case TGSI_OPCODE_RCC
:
1496 case TGSI_OPCODE_DPH
:
1497 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1498 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
);
1499 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
);
1500 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
);
1501 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
);
1502 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1503 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1504 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Z
);
1505 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Z
);
1506 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
);
1507 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1508 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_W
);
1509 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1510 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1511 dst0
[chan_index
] = tmp0
;
1515 case TGSI_OPCODE_COS
:
1516 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1517 tmp0
= lp_build_cos( &bld
->base
, tmp0
);
1518 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1519 dst0
[chan_index
] = tmp0
;
1523 case TGSI_OPCODE_DDX
:
1524 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1525 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, &dst0
[chan_index
], NULL
);
1529 case TGSI_OPCODE_DDY
:
1530 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1531 emit_fetch_deriv( bld
, inst
, 0, chan_index
, NULL
, NULL
, &dst0
[chan_index
]);
1535 case TGSI_OPCODE_KILP
:
1536 /* predicated kill */
1537 emit_kilp( bld
, inst
);
1540 case TGSI_OPCODE_KIL
:
1541 /* conditional kill */
1542 emit_kil( bld
, inst
);
1545 case TGSI_OPCODE_PK2H
:
1549 case TGSI_OPCODE_PK2US
:
1553 case TGSI_OPCODE_PK4B
:
1557 case TGSI_OPCODE_PK4UB
:
1561 case TGSI_OPCODE_RFL
:
1565 case TGSI_OPCODE_SEQ
:
1566 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1567 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1568 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1569 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_EQUAL
, src0
, src1
);
1570 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1574 case TGSI_OPCODE_SFL
:
1575 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1576 dst0
[chan_index
] = bld
->base
.zero
;
1580 case TGSI_OPCODE_SGT
:
1581 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1582 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1583 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1584 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_GREATER
, src0
, src1
);
1585 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1589 case TGSI_OPCODE_SIN
:
1590 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1591 tmp0
= lp_build_sin( &bld
->base
, tmp0
);
1592 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1593 dst0
[chan_index
] = tmp0
;
1597 case TGSI_OPCODE_SLE
:
1598 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1599 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1600 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1601 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LEQUAL
, src0
, src1
);
1602 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1606 case TGSI_OPCODE_SNE
:
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_NOTEQUAL
, src0
, src1
);
1611 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, bld
->base
.one
, bld
->base
.zero
);
1615 case TGSI_OPCODE_STR
:
1616 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1617 dst0
[chan_index
] = bld
->base
.one
;
1621 case TGSI_OPCODE_TEX
:
1622 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_NONE
, dst0
);
1625 case TGSI_OPCODE_TXD
:
1626 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
, dst0
);
1629 case TGSI_OPCODE_UP2H
:
1635 case TGSI_OPCODE_UP2US
:
1641 case TGSI_OPCODE_UP4B
:
1647 case TGSI_OPCODE_UP4UB
:
1653 case TGSI_OPCODE_X2D
:
1659 case TGSI_OPCODE_ARA
:
1665 case TGSI_OPCODE_ARR
:
1666 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1667 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1668 tmp0
= lp_build_round(&bld
->base
, tmp0
);
1669 dst0
[chan_index
] = tmp0
;
1673 case TGSI_OPCODE_BRA
:
1679 case TGSI_OPCODE_CAL
:
1680 lp_exec_mask_call(&bld
->exec_mask
,
1686 case TGSI_OPCODE_RET
:
1687 lp_exec_mask_ret(&bld
->exec_mask
, pc
);
1690 case TGSI_OPCODE_END
:
1694 case TGSI_OPCODE_SSG
:
1695 /* TGSI_OPCODE_SGN */
1696 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1697 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1698 dst0
[chan_index
] = lp_build_sgn( &bld
->base
, tmp0
);
1702 case TGSI_OPCODE_CMP
:
1703 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1704 src0
= emit_fetch( bld
, inst
, 0, chan_index
);
1705 src1
= emit_fetch( bld
, inst
, 1, chan_index
);
1706 src2
= emit_fetch( bld
, inst
, 2, chan_index
);
1707 tmp0
= lp_build_cmp( &bld
->base
, PIPE_FUNC_LESS
, src0
, bld
->base
.zero
);
1708 dst0
[chan_index
] = lp_build_select( &bld
->base
, tmp0
, src1
, src2
);
1712 case TGSI_OPCODE_SCS
:
1713 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_X
) {
1714 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1715 dst0
[CHAN_X
] = lp_build_cos( &bld
->base
, tmp0
);
1717 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Y
) {
1718 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
);
1719 dst0
[CHAN_Y
] = lp_build_sin( &bld
->base
, tmp0
);
1721 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_Z
) {
1722 dst0
[CHAN_Z
] = bld
->base
.zero
;
1724 IF_IS_DST0_CHANNEL_ENABLED( inst
, CHAN_W
) {
1725 dst0
[CHAN_W
] = bld
->base
.one
;
1729 case TGSI_OPCODE_TXB
:
1730 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
, dst0
);
1733 case TGSI_OPCODE_NRM
:
1735 case TGSI_OPCODE_NRM4
:
1736 /* 3 or 4-component normalization */
1738 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
1740 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) ||
1741 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
) ||
1742 IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
) ||
1743 (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 4)) {
1745 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
1748 /* xmm0 = src.x * src.x */
1749 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1750 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1753 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp0
);
1756 /* xmm0 = xmm0 + src.y * src.y */
1757 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Y
);
1758 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1761 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1762 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1765 /* xmm0 = xmm0 + src.z * src.z */
1766 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_Z
);
1767 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1770 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1771 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1775 /* xmm0 = xmm0 + src.w * src.w */
1776 tmp1
= emit_fetch(bld
, inst
, 0, CHAN_W
);
1777 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
)) {
1780 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp1
);
1781 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
);
1784 /* xmm1 = 1 / sqrt(xmm0) */
1785 tmp1
= lp_build_rsqrt( &bld
->base
, tmp0
);
1787 /* dst.x = xmm1 * src.x */
1788 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
)) {
1789 dst0
[CHAN_X
] = lp_build_mul( &bld
->base
, tmp4
, tmp1
);
1792 /* dst.y = xmm1 * src.y */
1793 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Y
)) {
1794 dst0
[CHAN_Y
] = lp_build_mul( &bld
->base
, tmp5
, tmp1
);
1797 /* dst.z = xmm1 * src.z */
1798 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_Z
)) {
1799 dst0
[CHAN_Z
] = lp_build_mul( &bld
->base
, tmp6
, tmp1
);
1802 /* dst.w = xmm1 * src.w */
1803 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_X
) && dims
== 4) {
1804 dst0
[CHAN_W
] = lp_build_mul( &bld
->base
, tmp7
, tmp1
);
1809 if (IS_DST0_CHANNEL_ENABLED(inst
, CHAN_W
) && dims
== 3) {
1810 dst0
[CHAN_W
] = bld
->base
.one
;
1815 case TGSI_OPCODE_DIV
:
1821 case TGSI_OPCODE_DP2
:
1822 tmp0
= emit_fetch( bld
, inst
, 0, CHAN_X
); /* xmm0 = src[0].x */
1823 tmp1
= emit_fetch( bld
, inst
, 1, CHAN_X
); /* xmm1 = src[1].x */
1824 tmp0
= lp_build_mul( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 * xmm1 */
1825 tmp1
= emit_fetch( bld
, inst
, 0, CHAN_Y
); /* xmm1 = src[0].y */
1826 tmp2
= emit_fetch( bld
, inst
, 1, CHAN_Y
); /* xmm2 = src[1].y */
1827 tmp1
= lp_build_mul( &bld
->base
, tmp1
, tmp2
); /* xmm1 = xmm1 * xmm2 */
1828 tmp0
= lp_build_add( &bld
->base
, tmp0
, tmp1
); /* xmm0 = xmm0 + xmm1 */
1829 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1830 dst0
[chan_index
] = tmp0
; /* dest[ch] = xmm0 */
1834 case TGSI_OPCODE_TXL
:
1835 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
, dst0
);
1838 case TGSI_OPCODE_TXP
:
1839 emit_tex( bld
, inst
, LP_BLD_TEX_MODIFIER_PROJECTED
, dst0
);
1842 case TGSI_OPCODE_BRK
:
1843 lp_exec_break(&bld
->exec_mask
);
1846 case TGSI_OPCODE_IF
:
1847 tmp0
= emit_fetch(bld
, inst
, 0, CHAN_X
);
1848 tmp0
= lp_build_cmp(&bld
->base
, PIPE_FUNC_NOTEQUAL
,
1849 tmp0
, bld
->base
.zero
);
1850 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp0
);
1853 case TGSI_OPCODE_BGNLOOP
:
1854 lp_exec_bgnloop(&bld
->exec_mask
);
1857 case TGSI_OPCODE_BGNSUB
:
1858 lp_exec_mask_bgnsub(&bld
->exec_mask
);
1861 case TGSI_OPCODE_ELSE
:
1862 lp_exec_mask_cond_invert(&bld
->exec_mask
);
1865 case TGSI_OPCODE_ENDIF
:
1866 lp_exec_mask_cond_pop(&bld
->exec_mask
);
1869 case TGSI_OPCODE_ENDLOOP
:
1870 lp_exec_endloop(&bld
->exec_mask
);
1873 case TGSI_OPCODE_ENDSUB
:
1874 lp_exec_mask_endsub(&bld
->exec_mask
, pc
);
1877 case TGSI_OPCODE_PUSHA
:
1883 case TGSI_OPCODE_POPA
:
1889 case TGSI_OPCODE_CEIL
:
1890 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1891 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1892 dst0
[chan_index
] = lp_build_ceil(&bld
->base
, tmp0
);
1896 case TGSI_OPCODE_I2F
:
1902 case TGSI_OPCODE_NOT
:
1908 case TGSI_OPCODE_TRUNC
:
1909 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1910 tmp0
= emit_fetch( bld
, inst
, 0, chan_index
);
1911 dst0
[chan_index
] = lp_build_trunc(&bld
->base
, tmp0
);
1915 case TGSI_OPCODE_SHL
:
1921 case TGSI_OPCODE_ISHR
:
1927 case TGSI_OPCODE_AND
:
1933 case TGSI_OPCODE_OR
:
1939 case TGSI_OPCODE_MOD
:
1945 case TGSI_OPCODE_XOR
:
1951 case TGSI_OPCODE_SAD
:
1957 case TGSI_OPCODE_TXF
:
1963 case TGSI_OPCODE_TXQ
:
1969 case TGSI_OPCODE_CONT
:
1970 lp_exec_continue(&bld
->exec_mask
);
1973 case TGSI_OPCODE_EMIT
:
1977 case TGSI_OPCODE_ENDPRIM
:
1981 case TGSI_OPCODE_NOP
:
1989 LLVMValueRef pred
[NUM_CHANNELS
];
1991 emit_fetch_predicate( bld
, inst
, pred
);
1993 FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1994 emit_store( bld
, inst
, 0, chan_index
, pred
[chan_index
], dst0
[chan_index
]);
2003 lp_build_tgsi_soa(LLVMBuilderRef builder
,
2004 const struct tgsi_token
*tokens
,
2005 struct lp_type type
,
2006 struct lp_build_mask_context
*mask
,
2007 LLVMValueRef consts_ptr
,
2008 const LLVMValueRef
*pos
,
2009 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
2010 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
2011 struct lp_build_sampler_soa
*sampler
,
2012 const struct tgsi_shader_info
*info
)
2014 struct lp_build_tgsi_soa_context bld
;
2015 struct tgsi_parse_context parse
;
2016 uint num_immediates
= 0;
2017 uint num_instructions
= 0;
2021 /* Setup build context */
2022 memset(&bld
, 0, sizeof bld
);
2023 lp_build_context_init(&bld
.base
, builder
, type
);
2024 lp_build_context_init(&bld
.int_bld
, builder
, lp_int_type(type
));
2027 bld
.inputs
= inputs
;
2028 bld
.outputs
= outputs
;
2029 bld
.consts_ptr
= consts_ptr
;
2030 bld
.sampler
= sampler
;
2031 bld
.indirect_files
= info
->indirect_files
;
2032 bld
.instructions
= (struct tgsi_full_instruction
*)
2033 MALLOC( LP_MAX_INSTRUCTIONS
* sizeof(struct tgsi_full_instruction
) );
2034 bld
.max_instructions
= LP_MAX_INSTRUCTIONS
;
2036 if (!bld
.instructions
) {
2040 lp_exec_mask_init(&bld
.exec_mask
, &bld
.base
);
2042 tgsi_parse_init( &parse
, tokens
);
2044 while( !tgsi_parse_end_of_tokens( &parse
) ) {
2045 tgsi_parse_token( &parse
);
2047 switch( parse
.FullToken
.Token
.Type
) {
2048 case TGSI_TOKEN_TYPE_DECLARATION
:
2049 /* Inputs already interpolated */
2050 emit_declaration( &bld
, &parse
.FullToken
.FullDeclaration
);
2053 case TGSI_TOKEN_TYPE_INSTRUCTION
:
2055 /* save expanded instruction */
2056 if (num_instructions
== bld
.max_instructions
) {
2057 struct tgsi_full_instruction
*instructions
;
2058 instructions
= REALLOC(bld
.instructions
,
2059 bld
.max_instructions
2060 * sizeof(struct tgsi_full_instruction
),
2061 (bld
.max_instructions
+ LP_MAX_INSTRUCTIONS
)
2062 * sizeof(struct tgsi_full_instruction
));
2063 if (!instructions
) {
2066 bld
.instructions
= instructions
;
2067 bld
.max_instructions
+= LP_MAX_INSTRUCTIONS
;
2070 memcpy(bld
.instructions
+ num_instructions
,
2071 &parse
.FullToken
.FullInstruction
,
2072 sizeof(bld
.instructions
[0]));
2079 case TGSI_TOKEN_TYPE_IMMEDIATE
:
2080 /* simply copy the immediate values into the next immediates[] slot */
2082 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
2084 assert(num_immediates
< LP_MAX_TGSI_IMMEDIATES
);
2085 for( i
= 0; i
< size
; ++i
)
2086 bld
.immediates
[num_immediates
][i
] =
2087 lp_build_const_vec(type
, parse
.FullToken
.FullImmediate
.u
[i
].Float
);
2088 for( i
= size
; i
< 4; ++i
)
2089 bld
.immediates
[num_immediates
][i
] = bld
.base
.undef
;
2094 case TGSI_TOKEN_TYPE_PROPERTY
:
2103 struct tgsi_full_instruction
*instr
= bld
.instructions
+ pc
;
2104 const struct tgsi_opcode_info
*opcode_info
=
2105 tgsi_get_opcode_info(instr
->Instruction
.Opcode
);
2106 if (!emit_instruction( &bld
, instr
, opcode_info
, &pc
))
2107 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
2108 opcode_info
->mnemonic
);
2112 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
2113 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
2114 debug_printf("11111111111111111111111111111 \n");
2115 tgsi_dump(tokens
, 0);
2116 lp_debug_dump_value(function
);
2117 debug_printf("2222222222222222222222222222 \n");
2119 tgsi_parse_free( &parse
);
2122 LLVMModuleRef module
= LLVMGetGlobalParent(
2123 LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld
.base
.builder
)));
2124 LLVMDumpModule(module
);
2128 FREE( bld
.instructions
);