1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 VMware, Inc.
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 VMWARE 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_exec.h"
46 #include "tgsi/tgsi_info.h"
47 #include "tgsi/tgsi_parse.h"
48 #include "tgsi/tgsi_util.h"
49 #include "tgsi/tgsi_scan.h"
50 #include "tgsi/tgsi_strings.h"
51 #include "lp_bld_tgsi_action.h"
52 #include "lp_bld_type.h"
53 #include "lp_bld_const.h"
54 #include "lp_bld_arit.h"
55 #include "lp_bld_bitarit.h"
56 #include "lp_bld_gather.h"
57 #include "lp_bld_init.h"
58 #include "lp_bld_logic.h"
59 #include "lp_bld_swizzle.h"
60 #include "lp_bld_flow.h"
61 #include "lp_bld_quad.h"
62 #include "lp_bld_tgsi.h"
63 #include "lp_bld_limits.h"
64 #include "lp_bld_debug.h"
65 #include "lp_bld_printf.h"
66 #include "lp_bld_sample.h"
67 #include "lp_bld_struct.h"
69 /* SM 4.0 says that subroutines can nest 32 deep and
70 * we need one more for our main function */
71 #define LP_MAX_NUM_FUNCS 33
73 #define DUMP_GS_EMITS 0
76 * If non-zero, the generated LLVM IR will print intermediate results on every TGSI
80 * - take execution masks in consideration
81 * - debug control-flow instructions
83 #define DEBUG_EXECUTION 0
87 * Emit code to print a register value.
90 emit_dump_reg(struct gallivm_state
*gallivm
,
98 util_snprintf(buf
, sizeof buf
, " %s[%u].%c = ",
100 index
, "xyzw"[chan
]);
102 lp_build_print_value(gallivm
, buf
, value
);
106 * Return the context for the current function.
107 * (always 'main', if shader doesn't do any function calls)
109 static inline struct function_ctx
*
110 func_ctx(struct lp_exec_mask
*mask
)
112 assert(mask
->function_stack_size
> 0);
113 assert(mask
->function_stack_size
<= LP_MAX_NUM_FUNCS
);
114 return &mask
->function_stack
[mask
->function_stack_size
- 1];
118 * Returns true if we're in a loop.
119 * It's global, meaning that it returns true even if there's
120 * no loop inside the current function, but we were inside
121 * a loop inside another function, from which this one was called.
123 static inline boolean
124 mask_has_loop(struct lp_exec_mask
*mask
)
127 for (i
= mask
->function_stack_size
- 1; i
>= 0; --i
) {
128 const struct function_ctx
*ctx
= &mask
->function_stack
[i
];
129 if (ctx
->loop_stack_size
> 0)
136 * Returns true if we're inside a switch statement.
137 * It's global, meaning that it returns true even if there's
138 * no switch in the current function, but we were inside
139 * a switch inside another function, from which this one was called.
141 static inline boolean
142 mask_has_switch(struct lp_exec_mask
*mask
)
145 for (i
= mask
->function_stack_size
- 1; i
>= 0; --i
) {
146 const struct function_ctx
*ctx
= &mask
->function_stack
[i
];
147 if (ctx
->switch_stack_size
> 0)
154 * Returns true if we're inside a conditional.
155 * It's global, meaning that it returns true even if there's
156 * no conditional in the current function, but we were inside
157 * a conditional inside another function, from which this one was called.
159 static inline boolean
160 mask_has_cond(struct lp_exec_mask
*mask
)
163 for (i
= mask
->function_stack_size
- 1; i
>= 0; --i
) {
164 const struct function_ctx
*ctx
= &mask
->function_stack
[i
];
165 if (ctx
->cond_stack_size
> 0)
173 * Initialize a function context at the specified index.
176 lp_exec_mask_function_init(struct lp_exec_mask
*mask
, int function_idx
)
178 LLVMTypeRef int_type
= LLVMInt32TypeInContext(mask
->bld
->gallivm
->context
);
179 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
180 struct function_ctx
*ctx
= &mask
->function_stack
[function_idx
];
182 ctx
->cond_stack_size
= 0;
183 ctx
->loop_stack_size
= 0;
184 ctx
->switch_stack_size
= 0;
186 if (function_idx
== 0) {
187 ctx
->ret_mask
= mask
->ret_mask
;
190 ctx
->loop_limiter
= lp_build_alloca(mask
->bld
->gallivm
,
191 int_type
, "looplimiter");
194 LLVMConstInt(int_type
, LP_MAX_TGSI_LOOP_ITERATIONS
, false),
198 static void lp_exec_mask_init(struct lp_exec_mask
*mask
, struct lp_build_context
*bld
)
201 mask
->has_mask
= FALSE
;
202 mask
->ret_in_main
= FALSE
;
203 /* For the main function */
204 mask
->function_stack_size
= 1;
206 mask
->int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, mask
->bld
->type
);
207 mask
->exec_mask
= mask
->ret_mask
= mask
->break_mask
= mask
->cont_mask
=
208 mask
->cond_mask
= mask
->switch_mask
=
209 LLVMConstAllOnes(mask
->int_vec_type
);
211 mask
->function_stack
= CALLOC(LP_MAX_NUM_FUNCS
,
212 sizeof(mask
->function_stack
[0]));
213 lp_exec_mask_function_init(mask
, 0);
217 lp_exec_mask_fini(struct lp_exec_mask
*mask
)
219 FREE(mask
->function_stack
);
222 static void lp_exec_mask_update(struct lp_exec_mask
*mask
)
224 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
225 boolean has_loop_mask
= mask_has_loop(mask
);
226 boolean has_cond_mask
= mask_has_cond(mask
);
227 boolean has_switch_mask
= mask_has_switch(mask
);
228 boolean has_ret_mask
= mask
->function_stack_size
> 1 ||
232 /*for loops we need to update the entire mask at runtime */
234 assert(mask
->break_mask
);
235 tmp
= LLVMBuildAnd(builder
,
239 mask
->exec_mask
= LLVMBuildAnd(builder
,
244 mask
->exec_mask
= mask
->cond_mask
;
246 if (has_switch_mask
) {
247 mask
->exec_mask
= LLVMBuildAnd(builder
,
254 mask
->exec_mask
= LLVMBuildAnd(builder
,
260 mask
->has_mask
= (has_cond_mask
||
266 static void lp_exec_mask_cond_push(struct lp_exec_mask
*mask
,
269 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
270 struct function_ctx
*ctx
= func_ctx(mask
);
272 if (ctx
->cond_stack_size
>= LP_MAX_TGSI_NESTING
) {
273 ctx
->cond_stack_size
++;
276 if (ctx
->cond_stack_size
== 0 && mask
->function_stack_size
== 1) {
277 assert(mask
->cond_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
279 ctx
->cond_stack
[ctx
->cond_stack_size
++] = mask
->cond_mask
;
280 assert(LLVMTypeOf(val
) == mask
->int_vec_type
);
281 mask
->cond_mask
= LLVMBuildAnd(builder
,
285 lp_exec_mask_update(mask
);
288 static void lp_exec_mask_cond_invert(struct lp_exec_mask
*mask
)
290 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
291 struct function_ctx
*ctx
= func_ctx(mask
);
292 LLVMValueRef prev_mask
;
293 LLVMValueRef inv_mask
;
295 assert(ctx
->cond_stack_size
);
296 if (ctx
->cond_stack_size
>= LP_MAX_TGSI_NESTING
)
298 prev_mask
= ctx
->cond_stack
[ctx
->cond_stack_size
- 1];
299 if (ctx
->cond_stack_size
== 1 && mask
->function_stack_size
== 1) {
300 assert(prev_mask
== LLVMConstAllOnes(mask
->int_vec_type
));
303 inv_mask
= LLVMBuildNot(builder
, mask
->cond_mask
, "");
305 mask
->cond_mask
= LLVMBuildAnd(builder
,
308 lp_exec_mask_update(mask
);
311 static void lp_exec_mask_cond_pop(struct lp_exec_mask
*mask
)
313 struct function_ctx
*ctx
= func_ctx(mask
);
314 assert(ctx
->cond_stack_size
);
315 --ctx
->cond_stack_size
;
316 if (ctx
->cond_stack_size
>= LP_MAX_TGSI_NESTING
)
318 mask
->cond_mask
= ctx
->cond_stack
[ctx
->cond_stack_size
];
319 lp_exec_mask_update(mask
);
322 static void lp_exec_bgnloop(struct lp_exec_mask
*mask
)
324 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
325 struct function_ctx
*ctx
= func_ctx(mask
);
327 if (ctx
->loop_stack_size
>= LP_MAX_TGSI_NESTING
) {
328 ++ctx
->loop_stack_size
;
332 ctx
->break_type_stack
[ctx
->loop_stack_size
+ ctx
->switch_stack_size
] =
334 ctx
->break_type
= LP_EXEC_MASK_BREAK_TYPE_LOOP
;
336 ctx
->loop_stack
[ctx
->loop_stack_size
].loop_block
= ctx
->loop_block
;
337 ctx
->loop_stack
[ctx
->loop_stack_size
].cont_mask
= mask
->cont_mask
;
338 ctx
->loop_stack
[ctx
->loop_stack_size
].break_mask
= mask
->break_mask
;
339 ctx
->loop_stack
[ctx
->loop_stack_size
].break_var
= ctx
->break_var
;
340 ++ctx
->loop_stack_size
;
342 ctx
->break_var
= lp_build_alloca(mask
->bld
->gallivm
, mask
->int_vec_type
, "");
343 LLVMBuildStore(builder
, mask
->break_mask
, ctx
->break_var
);
345 ctx
->loop_block
= lp_build_insert_new_block(mask
->bld
->gallivm
, "bgnloop");
347 LLVMBuildBr(builder
, ctx
->loop_block
);
348 LLVMPositionBuilderAtEnd(builder
, ctx
->loop_block
);
350 mask
->break_mask
= LLVMBuildLoad(builder
, ctx
->break_var
, "");
352 lp_exec_mask_update(mask
);
355 static void lp_exec_break(struct lp_exec_mask
*mask
,
356 struct lp_build_tgsi_context
* bld_base
)
358 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
359 struct function_ctx
*ctx
= func_ctx(mask
);
361 if (ctx
->break_type
== LP_EXEC_MASK_BREAK_TYPE_LOOP
) {
362 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
366 mask
->break_mask
= LLVMBuildAnd(builder
,
368 exec_mask
, "break_full");
371 unsigned opcode
= bld_base
->instructions
[bld_base
->pc
+ 1].Instruction
.Opcode
;
372 boolean break_always
= (opcode
== TGSI_OPCODE_ENDSWITCH
||
373 opcode
== TGSI_OPCODE_CASE
);
376 if (ctx
->switch_in_default
) {
378 * stop default execution but only if this is an unconditional switch.
379 * (The condition here is not perfect since dead code after break is
380 * allowed but should be sufficient since false negatives are just
381 * unoptimized - so we don't have to pre-evaluate that).
383 if(break_always
&& ctx
->switch_pc
) {
384 bld_base
->pc
= ctx
->switch_pc
;
390 mask
->switch_mask
= LLVMConstNull(mask
->bld
->int_vec_type
);
393 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
396 mask
->switch_mask
= LLVMBuildAnd(builder
,
398 exec_mask
, "break_switch");
402 lp_exec_mask_update(mask
);
405 static void lp_exec_break_condition(struct lp_exec_mask
*mask
,
408 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
409 struct function_ctx
*ctx
= func_ctx(mask
);
410 LLVMValueRef cond_mask
= LLVMBuildAnd(builder
,
413 cond_mask
= LLVMBuildNot(builder
, cond_mask
, "break_cond");
415 if (ctx
->break_type
== LP_EXEC_MASK_BREAK_TYPE_LOOP
) {
416 mask
->break_mask
= LLVMBuildAnd(builder
,
418 cond_mask
, "breakc_full");
421 mask
->switch_mask
= LLVMBuildAnd(builder
,
423 cond_mask
, "breakc_switch");
426 lp_exec_mask_update(mask
);
429 static void lp_exec_continue(struct lp_exec_mask
*mask
)
431 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
432 LLVMValueRef exec_mask
= LLVMBuildNot(builder
,
436 mask
->cont_mask
= LLVMBuildAnd(builder
,
440 lp_exec_mask_update(mask
);
444 static void lp_exec_endloop(struct gallivm_state
*gallivm
,
445 struct lp_exec_mask
*mask
)
447 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
448 struct function_ctx
*ctx
= func_ctx(mask
);
449 LLVMBasicBlockRef endloop
;
450 LLVMTypeRef int_type
= LLVMInt32TypeInContext(mask
->bld
->gallivm
->context
);
451 LLVMTypeRef reg_type
= LLVMIntTypeInContext(gallivm
->context
,
452 mask
->bld
->type
.width
*
453 mask
->bld
->type
.length
);
454 LLVMValueRef i1cond
, i2cond
, icond
, limiter
;
456 assert(mask
->break_mask
);
459 assert(ctx
->loop_stack_size
);
460 if (ctx
->loop_stack_size
> LP_MAX_TGSI_NESTING
) {
461 --ctx
->loop_stack_size
;
466 * Restore the cont_mask, but don't pop
468 mask
->cont_mask
= ctx
->loop_stack
[ctx
->loop_stack_size
- 1].cont_mask
;
469 lp_exec_mask_update(mask
);
472 * Unlike the continue mask, the break_mask must be preserved across loop
475 LLVMBuildStore(builder
, mask
->break_mask
, ctx
->break_var
);
477 /* Decrement the loop limiter */
478 limiter
= LLVMBuildLoad(builder
, ctx
->loop_limiter
, "");
480 limiter
= LLVMBuildSub(
483 LLVMConstInt(int_type
, 1, false),
486 LLVMBuildStore(builder
, limiter
, ctx
->loop_limiter
);
488 /* i1cond = (mask != 0) */
489 i1cond
= LLVMBuildICmp(
492 LLVMBuildBitCast(builder
, mask
->exec_mask
, reg_type
, ""),
493 LLVMConstNull(reg_type
), "i1cond");
495 /* i2cond = (looplimiter > 0) */
496 i2cond
= LLVMBuildICmp(
500 LLVMConstNull(int_type
), "i2cond");
502 /* if( i1cond && i2cond ) */
503 icond
= LLVMBuildAnd(builder
, i1cond
, i2cond
, "");
505 endloop
= lp_build_insert_new_block(mask
->bld
->gallivm
, "endloop");
507 LLVMBuildCondBr(builder
,
508 icond
, ctx
->loop_block
, endloop
);
510 LLVMPositionBuilderAtEnd(builder
, endloop
);
512 assert(ctx
->loop_stack_size
);
513 --ctx
->loop_stack_size
;
514 mask
->cont_mask
= ctx
->loop_stack
[ctx
->loop_stack_size
].cont_mask
;
515 mask
->break_mask
= ctx
->loop_stack
[ctx
->loop_stack_size
].break_mask
;
516 ctx
->loop_block
= ctx
->loop_stack
[ctx
->loop_stack_size
].loop_block
;
517 ctx
->break_var
= ctx
->loop_stack
[ctx
->loop_stack_size
].break_var
;
518 ctx
->break_type
= ctx
->break_type_stack
[ctx
->loop_stack_size
+
519 ctx
->switch_stack_size
];
521 lp_exec_mask_update(mask
);
524 static void lp_exec_switch(struct lp_exec_mask
*mask
,
525 LLVMValueRef switchval
)
527 struct function_ctx
*ctx
= func_ctx(mask
);
529 if (ctx
->switch_stack_size
>= LP_MAX_TGSI_NESTING
||
530 ctx
->loop_stack_size
> LP_MAX_TGSI_NESTING
) {
531 ctx
->switch_stack_size
++;
535 ctx
->break_type_stack
[ctx
->loop_stack_size
+ ctx
->switch_stack_size
] =
537 ctx
->break_type
= LP_EXEC_MASK_BREAK_TYPE_SWITCH
;
539 ctx
->switch_stack
[ctx
->switch_stack_size
].switch_mask
= mask
->switch_mask
;
540 ctx
->switch_stack
[ctx
->switch_stack_size
].switch_val
= ctx
->switch_val
;
541 ctx
->switch_stack
[ctx
->switch_stack_size
].switch_mask_default
= ctx
->switch_mask_default
;
542 ctx
->switch_stack
[ctx
->switch_stack_size
].switch_in_default
= ctx
->switch_in_default
;
543 ctx
->switch_stack
[ctx
->switch_stack_size
].switch_pc
= ctx
->switch_pc
;
544 ctx
->switch_stack_size
++;
546 mask
->switch_mask
= LLVMConstNull(mask
->int_vec_type
);
547 ctx
->switch_val
= switchval
;
548 ctx
->switch_mask_default
= LLVMConstNull(mask
->int_vec_type
);
549 ctx
->switch_in_default
= false;
552 lp_exec_mask_update(mask
);
555 static void lp_exec_endswitch(struct lp_exec_mask
*mask
,
556 struct lp_build_tgsi_context
* bld_base
)
558 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
559 struct function_ctx
*ctx
= func_ctx(mask
);
561 if (ctx
->switch_stack_size
> LP_MAX_TGSI_NESTING
) {
562 ctx
->switch_stack_size
--;
566 /* check if there's deferred default if so do it now */
567 if (ctx
->switch_pc
&& !ctx
->switch_in_default
) {
568 LLVMValueRef prevmask
, defaultmask
;
570 prevmask
= ctx
->switch_stack
[ctx
->switch_stack_size
- 1].switch_mask
;
571 defaultmask
= LLVMBuildNot(builder
, ctx
->switch_mask_default
, "sw_default_mask");
572 mask
->switch_mask
= LLVMBuildAnd(builder
, prevmask
, defaultmask
, "sw_mask");
573 ctx
->switch_in_default
= true;
575 lp_exec_mask_update(mask
);
577 assert(bld_base
->instructions
[ctx
->switch_pc
- 1].Instruction
.Opcode
==
578 TGSI_OPCODE_DEFAULT
);
580 tmp_pc
= bld_base
->pc
;
581 bld_base
->pc
= ctx
->switch_pc
;
583 * re-purpose switch_pc to point to here again, since we stop execution of
584 * the deferred default after next break.
586 ctx
->switch_pc
= tmp_pc
- 1;
591 else if (ctx
->switch_pc
&& ctx
->switch_in_default
) {
592 assert(bld_base
->pc
== ctx
->switch_pc
+ 1);
595 ctx
->switch_stack_size
--;
596 mask
->switch_mask
= ctx
->switch_stack
[ctx
->switch_stack_size
].switch_mask
;
597 ctx
->switch_val
= ctx
->switch_stack
[ctx
->switch_stack_size
].switch_val
;
598 ctx
->switch_mask_default
= ctx
->switch_stack
[ctx
->switch_stack_size
].switch_mask_default
;
599 ctx
->switch_in_default
= ctx
->switch_stack
[ctx
->switch_stack_size
].switch_in_default
;
600 ctx
->switch_pc
= ctx
->switch_stack
[ctx
->switch_stack_size
].switch_pc
;
602 ctx
->break_type
= ctx
->break_type_stack
[ctx
->loop_stack_size
+ ctx
->switch_stack_size
];
604 lp_exec_mask_update(mask
);
607 static void lp_exec_case(struct lp_exec_mask
*mask
,
608 LLVMValueRef caseval
)
610 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
611 struct function_ctx
*ctx
= func_ctx(mask
);
613 LLVMValueRef casemask
, prevmask
;
615 if (ctx
->switch_stack_size
> LP_MAX_TGSI_NESTING
) {
619 /* skipping case mask evaluation here is NOT optional (not in all cases anyway). */
620 if (!ctx
->switch_in_default
) {
621 prevmask
= ctx
->switch_stack
[ctx
->switch_stack_size
- 1].switch_mask
;
622 casemask
= lp_build_cmp(mask
->bld
, PIPE_FUNC_EQUAL
, caseval
, ctx
->switch_val
);
623 ctx
->switch_mask_default
= LLVMBuildOr(builder
, casemask
,
624 ctx
->switch_mask_default
, "sw_default_mask");
625 casemask
= LLVMBuildOr(builder
, casemask
, mask
->switch_mask
, "");
626 mask
->switch_mask
= LLVMBuildAnd(builder
, casemask
, prevmask
, "sw_mask");
628 lp_exec_mask_update(mask
);
633 * Analyse default statement in a switch.
634 * \return true if default is last statement, false otherwise
635 * \param default_pc_start contains pc of instruction to jump to
636 * if default wasn't last but there's no
637 * fallthrough into default.
639 static boolean
default_analyse_is_last(struct lp_exec_mask
*mask
,
640 struct lp_build_tgsi_context
* bld_base
,
641 int *default_pc_start
)
643 unsigned pc
= bld_base
->pc
;
644 struct function_ctx
*ctx
= func_ctx(mask
);
645 unsigned curr_switch_stack
= ctx
->switch_stack_size
;
647 if (ctx
->switch_stack_size
> LP_MAX_TGSI_NESTING
) {
651 /* skip over case statements which are together with default */
652 while (bld_base
->instructions
[pc
].Instruction
.Opcode
== TGSI_OPCODE_CASE
) {
656 while (pc
!= -1 && pc
< bld_base
->num_instructions
) {
657 unsigned opcode
= bld_base
->instructions
[pc
].Instruction
.Opcode
;
659 case TGSI_OPCODE_CASE
:
660 if (curr_switch_stack
== ctx
->switch_stack_size
) {
661 *default_pc_start
= pc
- 1;
665 case TGSI_OPCODE_SWITCH
:
668 case TGSI_OPCODE_ENDSWITCH
:
669 if (curr_switch_stack
== ctx
->switch_stack_size
) {
670 *default_pc_start
= pc
- 1;
678 /* should never arrive here */
683 static void lp_exec_default(struct lp_exec_mask
*mask
,
684 struct lp_build_tgsi_context
* bld_base
)
686 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
687 struct function_ctx
*ctx
= func_ctx(mask
);
690 boolean default_is_last
;
692 if (ctx
->switch_stack_size
> LP_MAX_TGSI_NESTING
) {
697 * This is a messy opcode, because it may not be always at the end and
698 * there can be fallthrough in and out of it.
701 default_is_last
= default_analyse_is_last(mask
, bld_base
, &default_exec_pc
);
703 * If it is last statement in switch (note that case statements appearing
704 * "at the same time" as default don't change that) everything is just fine,
705 * update switch mask and go on. This means we can handle default with
706 * fallthrough INTO it without overhead, if it is last.
708 if (default_is_last
) {
709 LLVMValueRef prevmask
, defaultmask
;
710 prevmask
= ctx
->switch_stack
[ctx
->switch_stack_size
- 1].switch_mask
;
711 defaultmask
= LLVMBuildNot(builder
, ctx
->switch_mask_default
, "sw_default_mask");
712 defaultmask
= LLVMBuildOr(builder
, defaultmask
, mask
->switch_mask
, "");
713 mask
->switch_mask
= LLVMBuildAnd(builder
, prevmask
, defaultmask
, "sw_mask");
714 ctx
->switch_in_default
= true;
716 lp_exec_mask_update(mask
);
720 * Technically, "case" immediately before default isn't really a
721 * fallthrough, however we still have to count them as such as we
722 * already have updated the masks.
723 * If that happens in practice could add a switch optimizer pass
724 * which just gets rid of all case statements appearing together with
725 * default (or could do switch analysis at switch start time instead).
727 unsigned opcode
= bld_base
->instructions
[bld_base
->pc
- 1].Instruction
.Opcode
;
728 boolean ft_into
= (opcode
!= TGSI_OPCODE_BRK
&&
729 opcode
!= TGSI_OPCODE_SWITCH
);
731 * If it is not last statement and there was no fallthrough into it,
732 * we record the PC and continue execution at next case (again, those
733 * case encountered at the same time don't count). At endswitch
734 * time, we update switchmask, and go back executing the code we skipped
735 * until the next break (possibly re-executing some code with changed mask
736 * if there was a fallthrough out of default).
737 * Finally, if it is not last statement and there was a fallthrough into it,
738 * do the same as with the former case, except instead of skipping the code
739 * just execute it without updating the mask, then go back and re-execute.
741 ctx
->switch_pc
= bld_base
->pc
;
743 bld_base
->pc
= default_exec_pc
;
749 /* stores val into an address pointed to by dst_ptr.
750 * mask->exec_mask is used to figure out which bits of val
751 * should be stored into the address
752 * (0 means don't store this bit, 1 means do store).
754 static void lp_exec_mask_store(struct lp_exec_mask
*mask
,
755 struct lp_build_context
*bld_store
,
758 LLVMValueRef dst_ptr
)
760 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
762 assert(lp_check_value(bld_store
->type
, val
));
763 assert(LLVMGetTypeKind(LLVMTypeOf(dst_ptr
)) == LLVMPointerTypeKind
);
764 assert(LLVMGetElementType(LLVMTypeOf(dst_ptr
)) == LLVMTypeOf(val
));
766 /* Mix the predicate and execution mask */
767 if (mask
->has_mask
) {
769 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
771 pred
= mask
->exec_mask
;
776 LLVMValueRef res
, dst
;
778 dst
= LLVMBuildLoad(builder
, dst_ptr
, "");
779 res
= lp_build_select(bld_store
, pred
, val
, dst
);
780 LLVMBuildStore(builder
, res
, dst_ptr
);
782 LLVMBuildStore(builder
, val
, dst_ptr
);
785 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
789 if (mask
->function_stack_size
>= LP_MAX_NUM_FUNCS
) {
793 lp_exec_mask_function_init(mask
, mask
->function_stack_size
);
794 mask
->function_stack
[mask
->function_stack_size
].pc
= *pc
;
795 mask
->function_stack
[mask
->function_stack_size
].ret_mask
= mask
->ret_mask
;
796 mask
->function_stack_size
++;
800 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
802 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
803 struct function_ctx
*ctx
= func_ctx(mask
);
804 LLVMValueRef exec_mask
;
806 if (ctx
->cond_stack_size
== 0 &&
807 ctx
->loop_stack_size
== 0 &&
808 ctx
->switch_stack_size
== 0 &&
809 mask
->function_stack_size
== 1) {
810 /* returning from main() */
815 if (mask
->function_stack_size
== 1) {
817 * This requires special handling since we need to ensure
818 * we don't drop the mask even if we have no call stack
819 * (e.g. after a ret in a if clause after the endif)
821 mask
->ret_in_main
= TRUE
;
824 exec_mask
= LLVMBuildNot(builder
,
828 mask
->ret_mask
= LLVMBuildAnd(builder
,
830 exec_mask
, "ret_full");
832 lp_exec_mask_update(mask
);
835 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
839 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
841 struct function_ctx
*ctx
;
843 assert(mask
->function_stack_size
> 1);
844 assert(mask
->function_stack_size
<= LP_MAX_NUM_FUNCS
);
846 ctx
= func_ctx(mask
);
847 mask
->function_stack_size
--;
850 mask
->ret_mask
= ctx
->ret_mask
;
852 lp_exec_mask_update(mask
);
857 get_file_ptr(struct lp_build_tgsi_soa_context
*bld
,
862 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
863 LLVMValueRef (*array_of_vars
)[TGSI_NUM_CHANNELS
];
864 LLVMValueRef var_of_array
;
867 case TGSI_FILE_TEMPORARY
:
868 array_of_vars
= bld
->temps
;
869 var_of_array
= bld
->temps_array
;
871 case TGSI_FILE_OUTPUT
:
872 array_of_vars
= bld
->outputs
;
873 var_of_array
= bld
->outputs_array
;
882 if (bld
->indirect_files
& (1 << file
)) {
883 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, index
* 4 + chan
);
884 return LLVMBuildGEP(builder
, var_of_array
, &lindex
, 1, "");
887 assert(index
<= bld
->bld_base
.info
->file_max
[file
]);
888 return array_of_vars
[index
][chan
];
894 * Return pointer to a temporary register channel (src or dest).
895 * Note that indirect addressing cannot be handled here.
896 * \param index which temporary register
897 * \param chan which channel of the temp register.
900 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context
*bld
,
904 return get_file_ptr(bld
, TGSI_FILE_TEMPORARY
, index
, chan
);
908 * Return pointer to a output register channel (src or dest).
909 * Note that indirect addressing cannot be handled here.
910 * \param index which output register
911 * \param chan which channel of the output register.
914 lp_get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
918 return get_file_ptr(bld
, TGSI_FILE_OUTPUT
, index
, chan
);
922 * If we have indirect addressing in outputs copy our alloca array
923 * to the outputs slots specified by the caller to make sure
924 * our outputs are delivered consistently via the same interface.
927 gather_outputs(struct lp_build_tgsi_soa_context
* bld
)
929 if ((bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
930 unsigned index
, chan
;
931 assert(bld
->bld_base
.info
->num_outputs
<=
932 bld
->bld_base
.info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
933 for (index
= 0; index
< bld
->bld_base
.info
->num_outputs
; ++index
) {
934 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
935 bld
->outputs
[index
][chan
] = lp_get_output_ptr(bld
, index
, chan
);
943 * XXX the lp_build_gather() function should be capable of doing this
944 * with a little work.
947 build_gather(struct lp_build_tgsi_context
*bld_base
,
948 LLVMValueRef base_ptr
,
949 LLVMValueRef indexes
,
950 LLVMValueRef overflow_mask
,
951 LLVMValueRef indexes2
)
953 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
954 LLVMBuilderRef builder
= gallivm
->builder
;
955 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
956 struct lp_build_context
*bld
= &bld_base
->base
;
961 res
= LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), bld_base
->base
.type
.length
* 2));
965 * overflow_mask is a vector telling us which channels
966 * in the vector overflowed. We use the overflow behavior for
967 * constant buffers which is defined as:
968 * Out of bounds access to constant buffer returns 0 in all
969 * components. Out of bounds behavior is always with respect
970 * to the size of the buffer bound at that slot.
975 * We avoid per-element control flow here (also due to llvm going crazy,
976 * though I suspect it's better anyway since overflow is likely rare).
977 * Note that since we still fetch from buffers even if num_elements was
978 * zero (in this case we'll fetch from index zero) the jit func callers
979 * MUST provide valid fake constant buffers of size 4x32 (the values do
980 * not matter), otherwise we'd still need (not per element though)
983 indexes
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes
);
985 indexes2
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes2
);
989 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
991 for (i
= 0; i
< bld
->type
.length
* (indexes2
? 2 : 1); i
++) {
994 LLVMValueRef scalar_ptr
, scalar
;
996 di
= lp_build_const_int32(bld
->gallivm
, i
);
998 si
= lp_build_const_int32(bld
->gallivm
, i
>> 1);
1002 if (indexes2
&& (i
& 1)) {
1003 index
= LLVMBuildExtractElement(builder
,
1006 index
= LLVMBuildExtractElement(builder
,
1009 scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
1010 &index
, 1, "gather_ptr");
1011 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
1013 res
= LLVMBuildInsertElement(builder
, res
, scalar
, di
, "");
1016 if (overflow_mask
) {
1018 res
= LLVMBuildBitCast(builder
, res
, bld_base
->dbl_bld
.vec_type
, "");
1019 overflow_mask
= LLVMBuildSExt(builder
, overflow_mask
,
1020 bld_base
->dbl_bld
.int_vec_type
, "");
1021 res
= lp_build_select(&bld_base
->dbl_bld
, overflow_mask
,
1022 bld_base
->dbl_bld
.zero
, res
);
1024 res
= lp_build_select(bld
, overflow_mask
, bld
->zero
, res
);
1032 * Scatter/store vector.
1035 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
1036 LLVMValueRef base_ptr
,
1037 LLVMValueRef indexes
,
1038 LLVMValueRef values
,
1039 struct lp_exec_mask
*mask
,
1042 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1043 LLVMBuilderRef builder
= gallivm
->builder
;
1046 /* Mix the predicate and execution mask */
1047 if (mask
->has_mask
) {
1049 pred
= LLVMBuildAnd(builder
, pred
, mask
->exec_mask
, "");
1052 pred
= mask
->exec_mask
;
1057 * Loop over elements of index_vec, store scalar value.
1059 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
1060 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1061 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
1062 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
1063 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
1064 LLVMValueRef scalar_pred
= pred
?
1065 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
1068 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
1069 ii
, val
, index
, scalar_ptr
);
1072 LLVMValueRef real_val
, dst_val
;
1073 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
1074 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
1075 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
1078 LLVMBuildStore(builder
, val
, scalar_ptr
);
1085 * Read the current value of the ADDR register, convert the floats to
1086 * ints, add the base index and return the vector of offsets.
1087 * The offsets will be used to index into the constant buffer or
1088 * temporary register file.
1091 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
1092 unsigned reg_file
, unsigned reg_index
,
1093 const struct tgsi_ind_register
*indirect_reg
)
1095 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1096 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
1097 /* always use X component of address register */
1098 unsigned swizzle
= indirect_reg
->Swizzle
;
1101 LLVMValueRef max_index
;
1104 assert(bld
->indirect_files
& (1 << reg_file
));
1106 base
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, reg_index
);
1108 assert(swizzle
< 4);
1109 switch (indirect_reg
->File
) {
1110 case TGSI_FILE_ADDRESS
:
1111 rel
= LLVMBuildLoad(builder
,
1112 bld
->addr
[indirect_reg
->Index
][swizzle
],
1114 /* ADDR LLVM values already have LLVM integer type. */
1116 case TGSI_FILE_TEMPORARY
:
1117 rel
= lp_get_temp_ptr_soa(bld
, indirect_reg
->Index
, swizzle
);
1118 rel
= LLVMBuildLoad(builder
, rel
, "load temp reg");
1119 /* TEMP LLVM values always have LLVM float type, but for indirection, the
1120 * value actually stored is expected to be an integer */
1121 rel
= LLVMBuildBitCast(builder
, rel
, uint_bld
->vec_type
, "");
1125 rel
= uint_bld
->zero
;
1128 index
= lp_build_add(uint_bld
, base
, rel
);
1131 * emit_fetch_constant handles constant buffer overflow so this code
1132 * is pointless for them.
1133 * Furthermore the D3D10 spec in section 6.5 says:
1134 * If the constant buffer bound to a slot is larger than the size
1135 * declared in the shader for that slot, implementations are allowed
1136 * to return incorrect data (not necessarily 0) for indices that are
1137 * larger than the declared size but smaller than the buffer size.
1139 if (reg_file
!= TGSI_FILE_CONSTANT
) {
1140 max_index
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
,
1142 bld
->bld_base
.info
->file_max
[reg_file
]);
1144 assert(!uint_bld
->type
.sign
);
1145 index
= lp_build_min(uint_bld
, index
, max_index
);
1151 static struct lp_build_context
*
1152 stype_to_fetch(struct lp_build_tgsi_context
* bld_base
,
1153 enum tgsi_opcode_type stype
)
1155 struct lp_build_context
*bld_fetch
;
1158 case TGSI_TYPE_FLOAT
:
1159 case TGSI_TYPE_UNTYPED
:
1160 bld_fetch
= &bld_base
->base
;
1162 case TGSI_TYPE_UNSIGNED
:
1163 bld_fetch
= &bld_base
->uint_bld
;
1165 case TGSI_TYPE_SIGNED
:
1166 bld_fetch
= &bld_base
->int_bld
;
1168 case TGSI_TYPE_DOUBLE
:
1169 bld_fetch
= &bld_base
->dbl_bld
;
1171 case TGSI_TYPE_VOID
:
1181 get_soa_array_offsets(struct lp_build_context
*uint_bld
,
1182 LLVMValueRef indirect_index
,
1183 unsigned chan_index
,
1184 boolean need_perelement_offset
)
1186 struct gallivm_state
*gallivm
= uint_bld
->gallivm
;
1187 LLVMValueRef chan_vec
=
1188 lp_build_const_int_vec(uint_bld
->gallivm
, uint_bld
->type
, chan_index
);
1189 LLVMValueRef length_vec
=
1190 lp_build_const_int_vec(gallivm
, uint_bld
->type
, uint_bld
->type
.length
);
1191 LLVMValueRef index_vec
;
1193 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1194 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1195 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1196 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1198 if (need_perelement_offset
) {
1199 LLVMValueRef pixel_offsets
;
1201 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1202 pixel_offsets
= uint_bld
->undef
;
1203 for (i
= 0; i
< uint_bld
->type
.length
; i
++) {
1204 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1205 pixel_offsets
= LLVMBuildInsertElement(gallivm
->builder
, pixel_offsets
,
1208 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1214 emit_fetch_constant(
1215 struct lp_build_tgsi_context
* bld_base
,
1216 const struct tgsi_full_src_register
* reg
,
1217 enum tgsi_opcode_type stype
,
1220 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1221 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1222 LLVMBuilderRef builder
= gallivm
->builder
;
1223 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1224 unsigned dimension
= 0;
1225 LLVMValueRef consts_ptr
;
1226 LLVMValueRef num_consts
;
1229 /* XXX: Handle fetching xyzw components as a vector */
1230 assert(swizzle
!= ~0);
1232 if (reg
->Register
.Dimension
) {
1233 assert(!reg
->Dimension
.Indirect
);
1234 dimension
= reg
->Dimension
.Index
;
1235 assert(dimension
< LP_MAX_TGSI_CONST_BUFFERS
);
1238 consts_ptr
= bld
->consts
[dimension
];
1239 num_consts
= bld
->consts_sizes
[dimension
];
1241 if (reg
->Register
.Indirect
) {
1242 LLVMValueRef indirect_index
;
1243 LLVMValueRef swizzle_vec
=
1244 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
1245 LLVMValueRef index_vec
; /* index into the const buffer */
1246 LLVMValueRef overflow_mask
;
1247 LLVMValueRef index_vec2
= NULL
;
1249 indirect_index
= get_indirect_index(bld
,
1251 reg
->Register
.Index
,
1254 /* All fetches are from the same constant buffer, so
1255 * we need to propagate the size to a vector to do a
1256 * vector comparison */
1257 num_consts
= lp_build_broadcast_scalar(uint_bld
, num_consts
);
1258 /* Construct a boolean vector telling us which channels
1259 * overflow the bound constant buffer */
1260 overflow_mask
= lp_build_compare(gallivm
, uint_bld
->type
, PIPE_FUNC_GEQUAL
,
1261 indirect_index
, num_consts
);
1263 /* index_vec = indirect_index * 4 + swizzle */
1264 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1265 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
1267 if (stype
== TGSI_TYPE_DOUBLE
) {
1268 LLVMValueRef swizzle_vec2
;
1269 swizzle_vec2
= lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
+ 1);
1270 index_vec2
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1271 index_vec2
= lp_build_add(uint_bld
, index_vec2
, swizzle_vec2
);
1273 /* Gather values from the constant buffer */
1274 res
= build_gather(bld_base
, consts_ptr
, index_vec
, overflow_mask
, index_vec2
);
1277 LLVMValueRef index
; /* index into the const buffer */
1278 LLVMValueRef scalar
, scalar_ptr
;
1279 struct lp_build_context
*bld_broad
= &bld_base
->base
;
1280 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
* 4 + swizzle
);
1282 scalar_ptr
= LLVMBuildGEP(builder
, consts_ptr
,
1284 if (stype
== TGSI_TYPE_DOUBLE
) {
1285 LLVMTypeRef dptr_type
= LLVMPointerType(LLVMDoubleTypeInContext(gallivm
->context
), 0);
1286 scalar_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, dptr_type
, "");
1287 bld_broad
= &bld_base
->dbl_bld
;
1289 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
1290 res
= lp_build_broadcast_scalar(bld_broad
, scalar
);
1293 if (stype
== TGSI_TYPE_SIGNED
|| stype
== TGSI_TYPE_UNSIGNED
|| stype
== TGSI_TYPE_DOUBLE
) {
1294 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1295 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1302 * Fetch double values from two separate channels.
1303 * Doubles are stored split across two channels, like xy and zw.
1304 * This function creates a set of 16 floats,
1305 * extracts the values from the two channels,
1306 * puts them in the correct place, then casts to 8 doubles.
1310 struct lp_build_tgsi_context
* bld_base
,
1311 enum tgsi_opcode_type stype
,
1313 LLVMValueRef input2
)
1315 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1316 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1317 LLVMBuilderRef builder
= gallivm
->builder
;
1319 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1321 LLVMValueRef shuffles
[16];
1322 int len
= bld_base
->base
.type
.length
* 2;
1325 for (i
= 0; i
< bld_base
->base
.type
.length
* 2; i
+=2) {
1326 shuffles
[i
] = lp_build_const_int32(gallivm
, i
/ 2);
1327 shuffles
[i
+ 1] = lp_build_const_int32(gallivm
, i
/ 2 + bld_base
->base
.type
.length
);
1329 res
= LLVMBuildShuffleVector(builder
, input
, input2
, LLVMConstVector(shuffles
, len
), "");
1331 return LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1335 emit_fetch_immediate(
1336 struct lp_build_tgsi_context
* bld_base
,
1337 const struct tgsi_full_src_register
* reg
,
1338 enum tgsi_opcode_type stype
,
1341 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1342 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1343 LLVMBuilderRef builder
= gallivm
->builder
;
1344 LLVMValueRef res
= NULL
;
1346 if (bld
->use_immediates_array
|| reg
->Register
.Indirect
) {
1347 LLVMValueRef imms_array
;
1348 LLVMTypeRef fptr_type
;
1350 /* cast imms_array pointer to float* */
1351 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1352 imms_array
= LLVMBuildBitCast(builder
, bld
->imms_array
, fptr_type
, "");
1354 if (reg
->Register
.Indirect
) {
1355 LLVMValueRef indirect_index
;
1356 LLVMValueRef index_vec
; /* index into the immediate register array */
1357 LLVMValueRef index_vec2
= NULL
;
1358 indirect_index
= get_indirect_index(bld
,
1360 reg
->Register
.Index
,
1363 * Unlike for other reg classes, adding pixel offsets is unnecessary -
1364 * immediates are stored as full vectors (FIXME??? - might be better
1365 * to store them the same as constants) but all elements are the same
1368 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1372 if (stype
== TGSI_TYPE_DOUBLE
)
1373 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
1377 /* Gather values from the immediate register array */
1378 res
= build_gather(bld_base
, imms_array
, index_vec
, NULL
, index_vec2
);
1380 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
1381 reg
->Register
.Index
* 4 + swizzle
);
1382 LLVMValueRef imms_ptr
= LLVMBuildGEP(builder
,
1383 bld
->imms_array
, &lindex
, 1, "");
1384 res
= LLVMBuildLoad(builder
, imms_ptr
, "");
1386 if (stype
== TGSI_TYPE_DOUBLE
) {
1387 LLVMValueRef lindex1
;
1388 LLVMValueRef imms_ptr2
;
1391 lindex1
= lp_build_const_int32(gallivm
,
1392 reg
->Register
.Index
* 4 + swizzle
+ 1);
1393 imms_ptr2
= LLVMBuildGEP(builder
,
1394 bld
->imms_array
, &lindex1
, 1, "");
1395 res2
= LLVMBuildLoad(builder
, imms_ptr2
, "");
1396 res
= emit_fetch_double(bld_base
, stype
, res
, res2
);
1401 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
1402 if (stype
== TGSI_TYPE_DOUBLE
)
1403 res
= emit_fetch_double(bld_base
, stype
, res
, bld
->immediates
[reg
->Register
.Index
][swizzle
+ 1]);
1406 if (stype
== TGSI_TYPE_UNSIGNED
) {
1407 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
1408 } else if (stype
== TGSI_TYPE_SIGNED
) {
1409 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
1410 } else if (stype
== TGSI_TYPE_DOUBLE
) {
1411 res
= LLVMBuildBitCast(builder
, res
, bld_base
->dbl_bld
.vec_type
, "");
1418 struct lp_build_tgsi_context
* bld_base
,
1419 const struct tgsi_full_src_register
* reg
,
1420 enum tgsi_opcode_type stype
,
1423 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1424 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1425 LLVMBuilderRef builder
= gallivm
->builder
;
1428 if (reg
->Register
.Indirect
) {
1429 LLVMValueRef indirect_index
;
1430 LLVMValueRef index_vec
; /* index into the input reg array */
1431 LLVMValueRef index_vec2
= NULL
;
1432 LLVMValueRef inputs_array
;
1433 LLVMTypeRef fptr_type
;
1435 indirect_index
= get_indirect_index(bld
,
1437 reg
->Register
.Index
,
1440 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1444 if (stype
== TGSI_TYPE_DOUBLE
) {
1445 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
1450 /* cast inputs_array pointer to float* */
1451 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1452 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
, fptr_type
, "");
1454 /* Gather values from the input register array */
1455 res
= build_gather(bld_base
, inputs_array
, index_vec
, NULL
, index_vec2
);
1457 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
1458 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
1459 reg
->Register
.Index
* 4 + swizzle
);
1460 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
1461 bld
->inputs_array
, &lindex
, 1, "");
1463 res
= LLVMBuildLoad(builder
, input_ptr
, "");
1464 if (stype
== TGSI_TYPE_DOUBLE
) {
1465 LLVMValueRef lindex1
;
1466 LLVMValueRef input_ptr2
;
1469 lindex1
= lp_build_const_int32(gallivm
,
1470 reg
->Register
.Index
* 4 + swizzle
+ 1);
1471 input_ptr2
= LLVMBuildGEP(builder
,
1472 bld
->inputs_array
, &lindex1
, 1, "");
1473 res2
= LLVMBuildLoad(builder
, input_ptr2
, "");
1474 res
= emit_fetch_double(bld_base
, stype
, res
, res2
);
1478 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
1479 if (stype
== TGSI_TYPE_DOUBLE
)
1480 res
= emit_fetch_double(bld_base
, stype
, res
, bld
->inputs
[reg
->Register
.Index
][swizzle
+ 1]);
1486 if (stype
== TGSI_TYPE_UNSIGNED
) {
1487 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
1488 } else if (stype
== TGSI_TYPE_SIGNED
) {
1489 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
1490 } else if (stype
== TGSI_TYPE_DOUBLE
) {
1491 res
= LLVMBuildBitCast(builder
, res
, bld_base
->dbl_bld
.vec_type
, "");
1499 emit_fetch_gs_input(
1500 struct lp_build_tgsi_context
* bld_base
,
1501 const struct tgsi_full_src_register
* reg
,
1502 enum tgsi_opcode_type stype
,
1505 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1506 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1507 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
1508 LLVMBuilderRef builder
= gallivm
->builder
;
1509 LLVMValueRef attrib_index
= NULL
;
1510 LLVMValueRef vertex_index
= NULL
;
1511 LLVMValueRef swizzle_index
= lp_build_const_int32(gallivm
, swizzle
);
1514 if (info
->input_semantic_name
[reg
->Register
.Index
] == TGSI_SEMANTIC_PRIMID
) {
1515 /* This is really a system value not a regular input */
1516 assert(!reg
->Register
.Indirect
);
1517 assert(!reg
->Dimension
.Indirect
);
1518 res
= bld
->system_values
.prim_id
;
1519 if (stype
!= TGSI_TYPE_UNSIGNED
&& stype
!= TGSI_TYPE_SIGNED
) {
1520 res
= LLVMBuildBitCast(builder
, res
, bld_base
->base
.vec_type
, "");
1525 if (reg
->Register
.Indirect
) {
1526 attrib_index
= get_indirect_index(bld
,
1528 reg
->Register
.Index
,
1531 attrib_index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
);
1534 if (reg
->Dimension
.Indirect
) {
1535 vertex_index
= get_indirect_index(bld
,
1537 reg
->Dimension
.Index
,
1540 vertex_index
= lp_build_const_int32(gallivm
, reg
->Dimension
.Index
);
1543 res
= bld
->gs_iface
->fetch_input(bld
->gs_iface
, bld_base
,
1544 reg
->Dimension
.Indirect
,
1546 reg
->Register
.Indirect
,
1551 if (stype
== TGSI_TYPE_DOUBLE
) {
1552 LLVMValueRef swizzle_index
= lp_build_const_int32(gallivm
, swizzle
+ 1);
1554 res2
= bld
->gs_iface
->fetch_input(bld
->gs_iface
, bld_base
,
1555 reg
->Dimension
.Indirect
,
1557 reg
->Register
.Indirect
,
1561 res
= emit_fetch_double(bld_base
, stype
, res
, res2
);
1562 } else if (stype
== TGSI_TYPE_UNSIGNED
) {
1563 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
1564 } else if (stype
== TGSI_TYPE_SIGNED
) {
1565 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
1572 emit_fetch_temporary(
1573 struct lp_build_tgsi_context
* bld_base
,
1574 const struct tgsi_full_src_register
* reg
,
1575 enum tgsi_opcode_type stype
,
1578 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1579 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1580 LLVMBuilderRef builder
= gallivm
->builder
;
1583 if (reg
->Register
.Indirect
) {
1584 LLVMValueRef indirect_index
;
1585 LLVMValueRef index_vec
, index_vec2
= NULL
; /* index into the temp reg array */
1586 LLVMValueRef temps_array
;
1587 LLVMTypeRef fptr_type
;
1589 indirect_index
= get_indirect_index(bld
,
1591 reg
->Register
.Index
,
1594 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1598 if (stype
== TGSI_TYPE_DOUBLE
) {
1599 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
1605 /* cast temps_array pointer to float* */
1606 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1607 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
, fptr_type
, "");
1609 /* Gather values from the temporary register array */
1610 res
= build_gather(bld_base
, temps_array
, index_vec
, NULL
, index_vec2
);
1613 LLVMValueRef temp_ptr
;
1614 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
1615 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1617 if (stype
== TGSI_TYPE_DOUBLE
) {
1618 LLVMValueRef temp_ptr2
, res2
;
1620 temp_ptr2
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
+ 1);
1621 res2
= LLVMBuildLoad(builder
, temp_ptr2
, "");
1622 res
= emit_fetch_double(bld_base
, stype
, res
, res2
);
1626 if (stype
== TGSI_TYPE_SIGNED
|| stype
== TGSI_TYPE_UNSIGNED
|| stype
== TGSI_TYPE_DOUBLE
) {
1627 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1628 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1635 emit_fetch_system_value(
1636 struct lp_build_tgsi_context
* bld_base
,
1637 const struct tgsi_full_src_register
* reg
,
1638 enum tgsi_opcode_type stype
,
1641 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1642 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1643 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
1644 LLVMBuilderRef builder
= gallivm
->builder
;
1646 enum tgsi_opcode_type atype
; // Actual type of the value
1648 assert(!reg
->Register
.Indirect
);
1650 switch (info
->system_value_semantic_name
[reg
->Register
.Index
]) {
1651 case TGSI_SEMANTIC_INSTANCEID
:
1652 res
= lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.instance_id
);
1653 atype
= TGSI_TYPE_UNSIGNED
;
1656 case TGSI_SEMANTIC_VERTEXID
:
1657 res
= bld
->system_values
.vertex_id
;
1658 atype
= TGSI_TYPE_UNSIGNED
;
1661 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1662 res
= bld
->system_values
.vertex_id_nobase
;
1663 atype
= TGSI_TYPE_UNSIGNED
;
1666 case TGSI_SEMANTIC_BASEVERTEX
:
1667 res
= bld
->system_values
.basevertex
;
1668 atype
= TGSI_TYPE_UNSIGNED
;
1671 case TGSI_SEMANTIC_PRIMID
:
1672 res
= bld
->system_values
.prim_id
;
1673 atype
= TGSI_TYPE_UNSIGNED
;
1676 case TGSI_SEMANTIC_INVOCATIONID
:
1677 res
= lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.invocation_id
);
1678 atype
= TGSI_TYPE_UNSIGNED
;
1682 assert(!"unexpected semantic in emit_fetch_system_value");
1683 res
= bld_base
->base
.zero
;
1684 atype
= TGSI_TYPE_FLOAT
;
1688 if (atype
!= stype
) {
1689 if (stype
== TGSI_TYPE_FLOAT
) {
1690 res
= LLVMBuildBitCast(builder
, res
, bld_base
->base
.vec_type
, "");
1691 } else if (stype
== TGSI_TYPE_UNSIGNED
) {
1692 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
1693 } else if (stype
== TGSI_TYPE_SIGNED
) {
1694 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
1702 * Register fetch with derivatives.
1706 struct lp_build_tgsi_soa_context
*bld
,
1715 /* TODO: use interpolation coeffs for inputs */
1718 *ddx
= lp_build_ddx(&bld
->bld_base
.base
, src
);
1721 *ddy
= lp_build_ddy(&bld
->bld_base
.base
, src
);
1729 emit_fetch_predicate(
1730 struct lp_build_tgsi_soa_context
*bld
,
1731 const struct tgsi_full_instruction
*inst
,
1734 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1736 unsigned char swizzles
[4];
1737 LLVMValueRef unswizzled
[4] = {NULL
, NULL
, NULL
, NULL
};
1741 if (!inst
->Instruction
.Predicate
) {
1742 TGSI_FOR_EACH_CHANNEL( chan
) {
1748 swizzles
[0] = inst
->Predicate
.SwizzleX
;
1749 swizzles
[1] = inst
->Predicate
.SwizzleY
;
1750 swizzles
[2] = inst
->Predicate
.SwizzleZ
;
1751 swizzles
[3] = inst
->Predicate
.SwizzleW
;
1753 index
= inst
->Predicate
.Index
;
1754 assert(index
< LP_MAX_TGSI_PREDS
);
1756 TGSI_FOR_EACH_CHANNEL( chan
) {
1757 unsigned swizzle
= swizzles
[chan
];
1760 * Only fetch the predicate register channels that are actually listed
1763 if (!unswizzled
[swizzle
]) {
1764 value
= LLVMBuildLoad(builder
,
1765 bld
->preds
[index
][swizzle
], "");
1768 * Convert the value to an integer mask.
1770 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
1771 * is needlessly causing two comparisons due to storing the intermediate
1772 * result as float vector instead of an integer mask vector.
1774 value
= lp_build_compare(bld
->bld_base
.base
.gallivm
,
1775 bld
->bld_base
.base
.type
,
1778 bld
->bld_base
.base
.zero
);
1779 if (inst
->Predicate
.Negate
) {
1780 value
= LLVMBuildNot(builder
, value
, "");
1783 unswizzled
[swizzle
] = value
;
1785 value
= unswizzled
[swizzle
];
1793 * store an array of 8 doubles into two arrays of 8 floats
1795 * value is d0, d1, d2, d3 etc.
1796 * each double has high and low pieces x, y
1797 * so gets stored into the separate channels as:
1798 * chan_ptr = d0.x, d1.x, d2.x, d3.x
1799 * chan_ptr2 = d0.y, d1.y, d2.y, d3.y
1802 emit_store_double_chan(struct lp_build_tgsi_context
*bld_base
,
1804 LLVMValueRef chan_ptr
, LLVMValueRef chan_ptr2
,
1808 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1809 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1810 LLVMBuilderRef builder
= gallivm
->builder
;
1811 struct lp_build_context
*float_bld
= &bld_base
->base
;
1813 LLVMValueRef temp
, temp2
;
1814 LLVMValueRef shuffles
[8];
1815 LLVMValueRef shuffles2
[8];
1817 for (i
= 0; i
< bld_base
->base
.type
.length
; i
++) {
1818 shuffles
[i
] = lp_build_const_int32(gallivm
, i
* 2);
1819 shuffles2
[i
] = lp_build_const_int32(gallivm
, (i
* 2) + 1);
1822 temp
= LLVMBuildShuffleVector(builder
, value
,
1823 LLVMGetUndef(LLVMTypeOf(value
)),
1824 LLVMConstVector(shuffles
,
1825 bld_base
->base
.type
.length
),
1827 temp2
= LLVMBuildShuffleVector(builder
, value
,
1828 LLVMGetUndef(LLVMTypeOf(value
)),
1829 LLVMConstVector(shuffles2
,
1830 bld_base
->base
.type
.length
),
1833 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, pred
, temp
, chan_ptr
);
1834 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, pred
, temp2
, chan_ptr2
);
1842 struct lp_build_tgsi_context
*bld_base
,
1843 const struct tgsi_full_instruction
*inst
,
1845 unsigned chan_index
,
1849 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1850 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1851 LLVMBuilderRef builder
= gallivm
->builder
;
1852 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
1853 struct lp_build_context
*float_bld
= &bld_base
->base
;
1854 struct lp_build_context
*int_bld
= &bld_base
->int_bld
;
1855 LLVMValueRef indirect_index
= NULL
;
1856 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
1861 * It is always assumed to be float.
1863 if (inst
->Instruction
.Saturate
) {
1864 assert(dtype
== TGSI_TYPE_FLOAT
||
1865 dtype
== TGSI_TYPE_UNTYPED
);
1866 value
= LLVMBuildBitCast(builder
, value
, float_bld
->vec_type
, "");
1867 value
= lp_build_clamp_zero_one_nanzero(float_bld
, value
);
1870 if (reg
->Register
.Indirect
) {
1872 * Currently the mesa/st doesn't generate indirect stores
1873 * to doubles, it normally uses MOV to do indirect stores.
1875 assert(dtype
!= TGSI_TYPE_DOUBLE
);
1876 indirect_index
= get_indirect_index(bld
,
1878 reg
->Register
.Index
,
1881 assert(reg
->Register
.Index
<=
1882 bld_base
->info
->file_max
[reg
->Register
.File
]);
1885 if (DEBUG_EXECUTION
) {
1886 emit_dump_reg(gallivm
, reg
->Register
.File
, reg
->Register
.Index
, chan_index
, value
);
1889 switch( reg
->Register
.File
) {
1890 case TGSI_FILE_OUTPUT
:
1891 /* Outputs are always stored as floats */
1892 value
= LLVMBuildBitCast(builder
, value
, float_bld
->vec_type
, "");
1894 if (reg
->Register
.Indirect
) {
1895 LLVMValueRef index_vec
; /* indexes into the output registers */
1896 LLVMValueRef outputs_array
;
1897 LLVMTypeRef fptr_type
;
1899 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1904 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1905 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
, fptr_type
, "");
1907 /* Scatter store values into output registers */
1908 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
1909 &bld
->exec_mask
, pred
);
1912 LLVMValueRef out_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
1915 if (dtype
== TGSI_TYPE_DOUBLE
) {
1916 LLVMValueRef out_ptr2
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
1918 emit_store_double_chan(bld_base
, dtype
, out_ptr
, out_ptr2
,
1921 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, pred
, value
, out_ptr
);
1925 case TGSI_FILE_TEMPORARY
:
1926 /* Temporaries are always stored as floats */
1927 if (dtype
!= TGSI_TYPE_DOUBLE
)
1928 value
= LLVMBuildBitCast(builder
, value
, float_bld
->vec_type
, "");
1930 value
= LLVMBuildBitCast(builder
, value
, LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), bld_base
->base
.type
.length
* 2), "");
1932 if (reg
->Register
.Indirect
) {
1933 LLVMValueRef index_vec
; /* indexes into the temp registers */
1934 LLVMValueRef temps_array
;
1935 LLVMTypeRef fptr_type
;
1937 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1942 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1943 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
, fptr_type
, "");
1945 /* Scatter store values into temp registers */
1946 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
1947 &bld
->exec_mask
, pred
);
1950 LLVMValueRef temp_ptr
;
1951 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, chan_index
);
1953 if (dtype
== TGSI_TYPE_DOUBLE
) {
1954 LLVMValueRef temp_ptr2
= lp_get_temp_ptr_soa(bld
,
1955 reg
->Register
.Index
,
1957 emit_store_double_chan(bld_base
, dtype
, temp_ptr
, temp_ptr2
,
1961 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, pred
, value
, temp_ptr
);
1965 case TGSI_FILE_ADDRESS
:
1966 assert(dtype
== TGSI_TYPE_SIGNED
);
1967 assert(LLVMTypeOf(value
) == int_bld
->vec_type
);
1968 value
= LLVMBuildBitCast(builder
, value
, int_bld
->vec_type
, "");
1969 lp_exec_mask_store(&bld
->exec_mask
, int_bld
, pred
, value
,
1970 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1973 case TGSI_FILE_PREDICATE
:
1974 assert(LLVMTypeOf(value
) == float_bld
->vec_type
);
1975 value
= LLVMBuildBitCast(builder
, value
, float_bld
->vec_type
, "");
1976 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, pred
, value
,
1977 bld
->preds
[reg
->Register
.Index
][chan_index
]);
1988 * Called at the beginning of the translation of each TGSI instruction, to
1989 * emit some debug code.
1993 struct lp_build_tgsi_context
* bld_base
,
1994 const struct tgsi_full_instruction
* inst
,
1995 const struct tgsi_opcode_info
* info
)
1998 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2000 if (DEBUG_EXECUTION
) {
2002 * Dump the TGSI instruction.
2005 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2009 tgsi_dump_instruction_str(inst
, bld_base
->pc
, &buf
[2], sizeof buf
- 2);
2010 lp_build_printf(gallivm
, buf
);
2012 /* Dump the execution mask.
2014 if (bld
->exec_mask
.has_mask
) {
2015 lp_build_print_value(gallivm
, " mask = ", bld
->exec_mask
.exec_mask
);
2022 struct lp_build_tgsi_context
* bld_base
,
2023 const struct tgsi_full_instruction
* inst
,
2024 const struct tgsi_opcode_info
* info
,
2025 LLVMValueRef dst
[4])
2028 unsigned chan_index
;
2029 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
2030 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
2032 LLVMValueRef pred
[TGSI_NUM_CHANNELS
];
2034 emit_fetch_predicate( bld
, inst
, pred
);
2036 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
2038 if (dtype
== TGSI_TYPE_DOUBLE
&& (chan_index
== 1 || chan_index
== 3))
2040 emit_store_chan(bld_base
, inst
, 0, chan_index
, pred
[chan_index
], dst
[chan_index
]);
2046 tgsi_to_pipe_tex_target(unsigned tgsi_target
)
2048 switch (tgsi_target
) {
2049 case TGSI_TEXTURE_BUFFER
:
2051 case TGSI_TEXTURE_1D
:
2052 case TGSI_TEXTURE_SHADOW1D
:
2053 return PIPE_TEXTURE_1D
;
2054 case TGSI_TEXTURE_2D
:
2055 case TGSI_TEXTURE_SHADOW2D
:
2056 case TGSI_TEXTURE_2D_MSAA
:
2057 return PIPE_TEXTURE_2D
;
2058 case TGSI_TEXTURE_3D
:
2059 return PIPE_TEXTURE_3D
;
2060 case TGSI_TEXTURE_CUBE
:
2061 case TGSI_TEXTURE_SHADOWCUBE
:
2062 return PIPE_TEXTURE_CUBE
;
2063 case TGSI_TEXTURE_RECT
:
2064 case TGSI_TEXTURE_SHADOWRECT
:
2065 return PIPE_TEXTURE_RECT
;
2066 case TGSI_TEXTURE_1D_ARRAY
:
2067 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2068 return PIPE_TEXTURE_1D_ARRAY
;
2069 case TGSI_TEXTURE_2D_ARRAY
:
2070 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2071 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
2072 return PIPE_TEXTURE_2D_ARRAY
;
2073 case TGSI_TEXTURE_CUBE_ARRAY
:
2074 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
2075 return PIPE_TEXTURE_CUBE_ARRAY
;
2083 static enum lp_sampler_lod_property
2084 lp_build_lod_property(
2085 struct lp_build_tgsi_context
*bld_base
,
2086 const struct tgsi_full_instruction
*inst
,
2089 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
2090 enum lp_sampler_lod_property lod_property
;
2093 * Not much we can do here. We could try catching inputs declared
2094 * with constant interpolation but not sure it's worth it - since for
2095 * TEX opcodes as well as FETCH/LD the lod comes from same reg as
2096 * the coords, so it could only work for SAMPLE/TXQ/SVIEWINFO), just
2097 * like the constant/immediate recognition below.
2098 * What seems to be of more value would be to recognize temps holding
2099 * broadcasted scalars but no way we can do it.
2100 * Tried asking llvm but without any success (using LLVMIsConstant
2101 * even though this isn't exactly what we'd need), even as simple as
2102 * IMM[0] UINT32 (0,-1,0,0)
2103 * MOV TEMP[0] IMM[0].yyyy
2104 * SVIEWINFO TEMP[1], TEMP[0].xxxx, SVIEWINFO[0]
2106 * This means there's ZERO chance this will ever catch a scalar lod
2107 * with traditional tex opcodes as well as texel fetches, since the lod
2108 * comes from the same reg as coords (except some test shaders using
2109 * constant coords maybe).
2110 * There's at least hope for sample opcodes as well as size queries.
2112 if (reg
->Register
.File
== TGSI_FILE_CONSTANT
||
2113 reg
->Register
.File
== TGSI_FILE_IMMEDIATE
) {
2114 lod_property
= LP_SAMPLER_LOD_SCALAR
;
2116 else if (bld_base
->info
->processor
== TGSI_PROCESSOR_FRAGMENT
) {
2117 if (gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) {
2118 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2121 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
2125 /* never use scalar (per-quad) lod the results are just too wrong. */
2126 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2128 return lod_property
;
2133 * High-level instruction translators.
2137 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
2138 const struct tgsi_full_instruction
*inst
,
2139 enum lp_build_tex_modifier modifier
,
2140 LLVMValueRef
*texel
,
2141 unsigned sampler_reg
,
2142 enum lp_sampler_op_type sampler_op
)
2144 unsigned unit
= inst
->Src
[sampler_reg
].Register
.Index
;
2145 LLVMValueRef oow
= NULL
;
2146 LLVMValueRef lod
= NULL
;
2147 LLVMValueRef coords
[5];
2148 LLVMValueRef offsets
[3] = { NULL
};
2149 struct lp_derivatives derivs
;
2150 struct lp_sampler_params params
;
2151 enum lp_sampler_lod_property lod_property
= LP_SAMPLER_LOD_SCALAR
;
2152 unsigned num_derivs
, num_offsets
, i
;
2153 unsigned shadow_coord
= 0;
2154 unsigned layer_coord
= 0;
2155 unsigned sample_key
= sampler_op
<< LP_SAMPLER_OP_TYPE_SHIFT
;
2157 memset(¶ms
, 0, sizeof(params
));
2159 if (!bld
->sampler
) {
2160 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2161 for (i
= 0; i
< 4; i
++) {
2162 texel
[i
] = bld
->bld_base
.base
.undef
;
2167 switch (inst
->Texture
.Texture
) {
2168 case TGSI_TEXTURE_1D_ARRAY
:
2171 case TGSI_TEXTURE_1D
:
2175 case TGSI_TEXTURE_2D_ARRAY
:
2178 case TGSI_TEXTURE_2D
:
2179 case TGSI_TEXTURE_RECT
:
2183 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2186 case TGSI_TEXTURE_SHADOW1D
:
2191 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2197 case TGSI_TEXTURE_SHADOW2D
:
2198 case TGSI_TEXTURE_SHADOWRECT
:
2203 case TGSI_TEXTURE_CUBE
:
2207 case TGSI_TEXTURE_3D
:
2211 case TGSI_TEXTURE_SHADOWCUBE
:
2216 case TGSI_TEXTURE_CUBE_ARRAY
:
2221 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
2225 shadow_coord
= 4; /* shadow coord special different reg */
2227 case TGSI_TEXTURE_2D_MSAA
:
2228 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
2234 /* Note lod and especially projected are illegal in a LOT of cases */
2235 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
||
2236 modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2237 if (inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE
||
2238 inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE_ARRAY
) {
2239 /* note that shadow cube array with bias/explicit lod does not exist */
2240 lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, 0);
2243 lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 3);
2245 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
2246 sample_key
|= LP_SAMPLER_LOD_BIAS
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2248 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2249 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2251 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2254 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
2255 oow
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 3);
2256 oow
= lp_build_rcp(&bld
->bld_base
.base
, oow
);
2259 for (i
= 0; i
< num_derivs
; i
++) {
2260 coords
[i
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, i
);
2261 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
2262 coords
[i
] = lp_build_mul(&bld
->bld_base
.base
, coords
[i
], oow
);
2264 for (i
= num_derivs
; i
< 5; i
++) {
2265 coords
[i
] = bld
->bld_base
.base
.undef
;
2268 /* Layer coord always goes into 3rd slot, except for cube map arrays */
2270 if (layer_coord
== 3) {
2271 coords
[3] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2274 coords
[2] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2276 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
2277 coords
[2] = lp_build_mul(&bld
->bld_base
.base
, coords
[2], oow
);
2279 /* Shadow coord occupies always 5th slot. */
2281 sample_key
|= LP_SAMPLER_SHADOW
;
2282 if (shadow_coord
== 4) {
2283 coords
[4] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, 0);
2286 coords
[4] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, shadow_coord
);
2288 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
2289 coords
[4] = lp_build_mul(&bld
->bld_base
.base
, coords
[4], oow
);
2292 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
2294 sample_key
|= LP_SAMPLER_LOD_DERIVATIVES
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2295 for (dim
= 0; dim
< num_derivs
; ++dim
) {
2296 derivs
.ddx
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, dim
);
2297 derivs
.ddy
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 2, dim
);
2299 params
.derivs
= &derivs
;
2301 * could also check all src regs if constant but I doubt such
2302 * cases exist in practice.
2304 if (bld
->bld_base
.info
->processor
== TGSI_PROCESSOR_FRAGMENT
) {
2305 if (gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) {
2306 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2309 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
2313 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2316 sample_key
|= lod_property
<< LP_SAMPLER_LOD_PROPERTY_SHIFT
;
2318 /* we don't handle the 4 offset version of tg4 */
2319 if (inst
->Texture
.NumOffsets
== 1) {
2321 sample_key
|= LP_SAMPLER_OFFSETS
;
2322 for (dim
= 0; dim
< num_offsets
; dim
++) {
2323 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
2327 params
.type
= bld
->bld_base
.base
.type
;
2328 params
.sample_key
= sample_key
;
2329 params
.texture_index
= unit
;
2330 params
.sampler_index
= unit
;
2331 params
.context_ptr
= bld
->context_ptr
;
2332 params
.thread_data_ptr
= bld
->thread_data_ptr
;
2333 params
.coords
= coords
;
2334 params
.offsets
= offsets
;
2336 params
.texel
= texel
;
2338 bld
->sampler
->emit_tex_sample(bld
->sampler
,
2339 bld
->bld_base
.base
.gallivm
,
2344 emit_sample(struct lp_build_tgsi_soa_context
*bld
,
2345 const struct tgsi_full_instruction
*inst
,
2346 enum lp_build_tex_modifier modifier
,
2348 LLVMValueRef
*texel
)
2350 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
2351 unsigned texture_unit
, sampler_unit
;
2352 LLVMValueRef lod
= NULL
;
2353 LLVMValueRef coords
[5];
2354 LLVMValueRef offsets
[3] = { NULL
};
2355 struct lp_derivatives derivs
;
2356 struct lp_sampler_params params
;
2357 enum lp_sampler_lod_property lod_property
= LP_SAMPLER_LOD_SCALAR
;
2359 unsigned num_offsets
, num_derivs
, i
;
2360 unsigned layer_coord
= 0;
2361 unsigned sample_key
= LP_SAMPLER_OP_TEXTURE
<< LP_SAMPLER_OP_TYPE_SHIFT
;
2363 memset(¶ms
, 0, sizeof(params
));
2365 if (!bld
->sampler
) {
2366 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2367 for (i
= 0; i
< 4; i
++) {
2368 texel
[i
] = bld
->bld_base
.base
.undef
;
2374 * unlike old-style tex opcodes the texture/sampler indices
2375 * always come from src1 and src2 respectively.
2377 texture_unit
= inst
->Src
[1].Register
.Index
;
2378 sampler_unit
= inst
->Src
[2].Register
.Index
;
2381 * Note inst->Texture.Texture will contain the number of offsets,
2382 * however the target information is NOT there and comes from the
2383 * declared sampler views instead.
2385 switch (bld
->sv
[texture_unit
].Resource
) {
2386 case TGSI_TEXTURE_1D
:
2390 case TGSI_TEXTURE_1D_ARRAY
:
2395 case TGSI_TEXTURE_2D
:
2396 case TGSI_TEXTURE_RECT
:
2400 case TGSI_TEXTURE_2D_ARRAY
:
2405 case TGSI_TEXTURE_CUBE
:
2409 case TGSI_TEXTURE_3D
:
2413 case TGSI_TEXTURE_CUBE_ARRAY
:
2423 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
||
2424 modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2425 lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 3, 0);
2426 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
2427 sample_key
|= LP_SAMPLER_LOD_BIAS
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2429 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2430 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2432 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2434 else if (modifier
== LP_BLD_TEX_MODIFIER_LOD_ZERO
) {
2435 /* XXX might be better to explicitly pass the level zero information */
2436 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2437 lod
= lp_build_const_vec(gallivm
, bld
->bld_base
.base
.type
, 0.0F
);
2440 for (i
= 0; i
< num_derivs
; i
++) {
2441 coords
[i
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, i
);
2443 for (i
= num_derivs
; i
< 5; i
++) {
2444 coords
[i
] = bld
->bld_base
.base
.undef
;
2447 /* Layer coord always goes into 3rd slot, except for cube map arrays */
2449 if (layer_coord
== 3)
2450 coords
[3] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2452 coords
[2] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2454 /* Shadow coord occupies always 5th slot. */
2456 sample_key
|= LP_SAMPLER_SHADOW
;
2457 coords
[4] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 3, 0);
2460 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
2462 sample_key
|= LP_SAMPLER_LOD_DERIVATIVES
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2463 for (dim
= 0; dim
< num_derivs
; ++dim
) {
2464 derivs
.ddx
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 3, dim
);
2465 derivs
.ddy
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 4, dim
);
2467 params
.derivs
= &derivs
;
2469 * could also check all src regs if constant but I doubt such
2470 * cases exist in practice.
2472 if (bld
->bld_base
.info
->processor
== TGSI_PROCESSOR_FRAGMENT
) {
2473 if (gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) {
2474 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2477 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
2481 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2485 /* some advanced gather instructions (txgo) would require 4 offsets */
2486 if (inst
->Texture
.NumOffsets
== 1) {
2488 sample_key
|= LP_SAMPLER_OFFSETS
;
2489 for (dim
= 0; dim
< num_offsets
; dim
++) {
2490 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
2493 sample_key
|= lod_property
<< LP_SAMPLER_LOD_PROPERTY_SHIFT
;
2495 params
.type
= bld
->bld_base
.base
.type
;
2496 params
.sample_key
= sample_key
;
2497 params
.texture_index
= texture_unit
;
2498 params
.sampler_index
= sampler_unit
;
2499 params
.context_ptr
= bld
->context_ptr
;
2500 params
.thread_data_ptr
= bld
->thread_data_ptr
;
2501 params
.coords
= coords
;
2502 params
.offsets
= offsets
;
2504 params
.texel
= texel
;
2506 bld
->sampler
->emit_tex_sample(bld
->sampler
,
2507 bld
->bld_base
.base
.gallivm
,
2510 if (inst
->Src
[1].Register
.SwizzleX
!= PIPE_SWIZZLE_RED
||
2511 inst
->Src
[1].Register
.SwizzleY
!= PIPE_SWIZZLE_GREEN
||
2512 inst
->Src
[1].Register
.SwizzleZ
!= PIPE_SWIZZLE_BLUE
||
2513 inst
->Src
[1].Register
.SwizzleW
!= PIPE_SWIZZLE_ALPHA
) {
2514 unsigned char swizzles
[4];
2515 swizzles
[0] = inst
->Src
[1].Register
.SwizzleX
;
2516 swizzles
[1] = inst
->Src
[1].Register
.SwizzleY
;
2517 swizzles
[2] = inst
->Src
[1].Register
.SwizzleZ
;
2518 swizzles
[3] = inst
->Src
[1].Register
.SwizzleW
;
2520 lp_build_swizzle_soa_inplace(&bld
->bld_base
.base
, texel
, swizzles
);
2525 emit_fetch_texels( struct lp_build_tgsi_soa_context
*bld
,
2526 const struct tgsi_full_instruction
*inst
,
2527 LLVMValueRef
*texel
,
2530 unsigned unit
, target
;
2531 LLVMValueRef coord_undef
= LLVMGetUndef(bld
->bld_base
.base
.int_vec_type
);
2532 LLVMValueRef explicit_lod
= NULL
;
2533 LLVMValueRef coords
[5];
2534 LLVMValueRef offsets
[3] = { NULL
};
2535 struct lp_sampler_params params
;
2536 enum lp_sampler_lod_property lod_property
= LP_SAMPLER_LOD_SCALAR
;
2538 unsigned layer_coord
= 0;
2539 unsigned sample_key
= LP_SAMPLER_OP_FETCH
<< LP_SAMPLER_OP_TYPE_SHIFT
;
2541 memset(¶ms
, 0, sizeof(params
));
2543 if (!bld
->sampler
) {
2544 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2545 for (i
= 0; i
< 4; i
++) {
2546 texel
[i
] = coord_undef
;
2551 unit
= inst
->Src
[1].Register
.Index
;
2554 target
= bld
->sv
[unit
].Resource
;
2557 target
= inst
->Texture
.Texture
;
2561 case TGSI_TEXTURE_1D
:
2562 case TGSI_TEXTURE_BUFFER
:
2565 case TGSI_TEXTURE_1D_ARRAY
:
2569 case TGSI_TEXTURE_2D
:
2570 case TGSI_TEXTURE_RECT
:
2571 case TGSI_TEXTURE_2D_MSAA
:
2574 case TGSI_TEXTURE_2D_ARRAY
:
2575 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
2579 case TGSI_TEXTURE_3D
:
2587 /* always have lod except for buffers and msaa targets ? */
2588 if (target
!= TGSI_TEXTURE_BUFFER
&&
2589 target
!= TGSI_TEXTURE_2D_MSAA
&&
2590 target
!= TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2591 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2592 explicit_lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 3);
2593 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2595 /* XXX: for real msaa support, the w component would be the sample index. */
2597 for (i
= 0; i
< dims
; i
++) {
2598 coords
[i
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, i
);
2600 /* never use more than 3 coords here but emit_fetch_texel copies all 5 anyway */
2601 for (i
= dims
; i
< 5; i
++) {
2602 coords
[i
] = coord_undef
;
2605 coords
[2] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2607 if (inst
->Texture
.NumOffsets
== 1) {
2609 sample_key
|= LP_SAMPLER_OFFSETS
;
2610 for (dim
= 0; dim
< dims
; dim
++) {
2611 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
2614 sample_key
|= lod_property
<< LP_SAMPLER_LOD_PROPERTY_SHIFT
;
2616 params
.type
= bld
->bld_base
.base
.type
;
2617 params
.sample_key
= sample_key
;
2618 params
.texture_index
= unit
;
2620 * sampler not actually used, set to 0 so it won't exceed PIPE_MAX_SAMPLERS
2621 * and trigger some assertions with d3d10 where the sampler view number
2624 params
.sampler_index
= 0;
2625 params
.context_ptr
= bld
->context_ptr
;
2626 params
.thread_data_ptr
= bld
->thread_data_ptr
;
2627 params
.coords
= coords
;
2628 params
.offsets
= offsets
;
2629 params
.derivs
= NULL
;
2630 params
.lod
= explicit_lod
;
2631 params
.texel
= texel
;
2633 bld
->sampler
->emit_tex_sample(bld
->sampler
,
2634 bld
->bld_base
.base
.gallivm
,
2638 (inst
->Src
[1].Register
.SwizzleX
!= PIPE_SWIZZLE_RED
||
2639 inst
->Src
[1].Register
.SwizzleY
!= PIPE_SWIZZLE_GREEN
||
2640 inst
->Src
[1].Register
.SwizzleZ
!= PIPE_SWIZZLE_BLUE
||
2641 inst
->Src
[1].Register
.SwizzleW
!= PIPE_SWIZZLE_ALPHA
)) {
2642 unsigned char swizzles
[4];
2643 swizzles
[0] = inst
->Src
[1].Register
.SwizzleX
;
2644 swizzles
[1] = inst
->Src
[1].Register
.SwizzleY
;
2645 swizzles
[2] = inst
->Src
[1].Register
.SwizzleZ
;
2646 swizzles
[3] = inst
->Src
[1].Register
.SwizzleW
;
2648 lp_build_swizzle_soa_inplace(&bld
->bld_base
.base
, texel
, swizzles
);
2653 emit_size_query( struct lp_build_tgsi_soa_context
*bld
,
2654 const struct tgsi_full_instruction
*inst
,
2655 LLVMValueRef
*sizes_out
,
2656 boolean is_sviewinfo
)
2658 LLVMValueRef explicit_lod
;
2659 enum lp_sampler_lod_property lod_property
;
2662 unsigned unit
= inst
->Src
[1].Register
.Index
;
2663 unsigned target
, pipe_target
;
2666 target
= bld
->sv
[unit
].Resource
;
2669 target
= inst
->Texture
.Texture
;
2672 case TGSI_TEXTURE_BUFFER
:
2673 case TGSI_TEXTURE_RECT
:
2674 case TGSI_TEXTURE_SHADOWRECT
:
2682 if (!bld
->sampler
) {
2683 _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
2684 for (i
= 0; i
< 4; i
++)
2685 sizes_out
[i
] = bld
->bld_base
.int_bld
.undef
;
2690 explicit_lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 0);
2691 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2694 explicit_lod
= NULL
;
2695 lod_property
= LP_SAMPLER_LOD_SCALAR
;
2699 pipe_target
= tgsi_to_pipe_tex_target(target
);
2701 bld
->sampler
->emit_size_query(bld
->sampler
,
2702 bld
->bld_base
.base
.gallivm
,
2703 bld
->bld_base
.int_bld
.type
,
2713 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
2718 for (i
= 0; i
< 5; i
++) {
2721 if (pc
+ i
>= bld
->bld_base
.info
->num_instructions
)
2724 opcode
= bld
->bld_base
.instructions
[pc
+ i
].Instruction
.Opcode
;
2726 if (opcode
== TGSI_OPCODE_END
)
2729 if (opcode
== TGSI_OPCODE_TEX
||
2730 opcode
== TGSI_OPCODE_TXP
||
2731 opcode
== TGSI_OPCODE_TXD
||
2732 opcode
== TGSI_OPCODE_TXB
||
2733 opcode
== TGSI_OPCODE_TXL
||
2734 opcode
== TGSI_OPCODE_TXF
||
2735 opcode
== TGSI_OPCODE_TXQ
||
2736 opcode
== TGSI_OPCODE_TEX2
||
2737 opcode
== TGSI_OPCODE_TXB2
||
2738 opcode
== TGSI_OPCODE_TXL2
||
2739 opcode
== TGSI_OPCODE_SAMPLE
||
2740 opcode
== TGSI_OPCODE_SAMPLE_B
||
2741 opcode
== TGSI_OPCODE_SAMPLE_C
||
2742 opcode
== TGSI_OPCODE_SAMPLE_C_LZ
||
2743 opcode
== TGSI_OPCODE_SAMPLE_D
||
2744 opcode
== TGSI_OPCODE_SAMPLE_I
||
2745 opcode
== TGSI_OPCODE_SAMPLE_L
||
2746 opcode
== TGSI_OPCODE_SVIEWINFO
||
2747 opcode
== TGSI_OPCODE_CAL
||
2748 opcode
== TGSI_OPCODE_CALLNZ
||
2749 opcode
== TGSI_OPCODE_IF
||
2750 opcode
== TGSI_OPCODE_UIF
||
2751 opcode
== TGSI_OPCODE_BGNLOOP
||
2752 opcode
== TGSI_OPCODE_SWITCH
)
2762 * Kill fragment if any of the src register values are negative.
2766 struct lp_build_tgsi_soa_context
*bld
,
2767 const struct tgsi_full_instruction
*inst
,
2770 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
2771 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
2772 LLVMValueRef terms
[TGSI_NUM_CHANNELS
];
2774 unsigned chan_index
;
2776 memset(&terms
, 0, sizeof terms
);
2778 TGSI_FOR_EACH_CHANNEL( chan_index
) {
2781 /* Unswizzle channel */
2782 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
2784 /* Check if the component has not been already tested. */
2785 assert(swizzle
< TGSI_NUM_CHANNELS
);
2786 if( !terms
[swizzle
] )
2787 /* TODO: change the comparison operator instead of setting the sign */
2788 terms
[swizzle
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, chan_index
);
2792 TGSI_FOR_EACH_CHANNEL( chan_index
) {
2793 if(terms
[chan_index
]) {
2794 LLVMValueRef chan_mask
;
2797 * If term < 0 then mask = 0 else mask = ~0.
2799 chan_mask
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->bld_base
.base
.zero
);
2802 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
2808 if (bld
->exec_mask
.has_mask
) {
2809 LLVMValueRef invmask
;
2810 invmask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
2811 mask
= LLVMBuildOr(builder
, mask
, invmask
, "");
2814 lp_build_mask_update(bld
->mask
, mask
);
2815 if (!near_end_of_shader(bld
, pc
))
2816 lp_build_mask_check(bld
->mask
);
2821 * Unconditional fragment kill.
2822 * The only predication is the execution mask which will apply if
2823 * we're inside a loop or conditional.
2826 emit_kill(struct lp_build_tgsi_soa_context
*bld
,
2829 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
2832 /* For those channels which are "alive", disable fragment shader
2835 if (bld
->exec_mask
.has_mask
) {
2836 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
2839 LLVMValueRef zero
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
2843 lp_build_mask_update(bld
->mask
, mask
);
2845 if (!near_end_of_shader(bld
, pc
))
2846 lp_build_mask_check(bld
->mask
);
2851 * Emit code which will dump the value of all the temporary registers
2855 emit_dump_file(struct lp_build_tgsi_soa_context
*bld
,
2858 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
2859 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
2860 LLVMBuilderRef builder
= gallivm
->builder
;
2861 LLVMValueRef reg_ptr
;
2863 int max_index
= info
->file_max
[file
];
2866 * Some register files, particularly constants, can be very large,
2867 * and dumping everything could make this unusably slow.
2869 max_index
= MIN2(max_index
, 32);
2871 for (index
= 0; index
<= max_index
; index
++) {
2876 if (index
< 8 * sizeof(unsigned) &&
2877 (info
->file_mask
[file
] & (1 << index
)) == 0) {
2878 /* This was not declared.*/
2882 if (file
== TGSI_FILE_INPUT
) {
2883 mask
= info
->input_usage_mask
[index
];
2885 mask
= TGSI_WRITEMASK_XYZW
;
2888 for (chan
= 0; chan
< 4; chan
++) {
2889 if ((mask
& (1 << chan
)) == 0) {
2890 /* This channel is not used.*/
2894 if (file
== TGSI_FILE_CONSTANT
) {
2895 struct tgsi_full_src_register reg
;
2896 memset(®
, 0, sizeof reg
);
2897 reg
.Register
.File
= file
;
2898 reg
.Register
.Index
= index
;
2899 reg
.Register
.SwizzleX
= 0;
2900 reg
.Register
.SwizzleY
= 1;
2901 reg
.Register
.SwizzleZ
= 2;
2902 reg
.Register
.SwizzleW
= 3;
2904 res
= bld
->bld_base
.emit_fetch_funcs
[file
](&bld
->bld_base
, ®
, TGSI_TYPE_FLOAT
, chan
);
2908 } else if (file
== TGSI_FILE_INPUT
) {
2909 res
= bld
->inputs
[index
][chan
];
2913 } else if (file
== TGSI_FILE_TEMPORARY
) {
2914 reg_ptr
= lp_get_temp_ptr_soa(bld
, index
, chan
);
2916 res
= LLVMBuildLoad(builder
, reg_ptr
, "");
2917 } else if (file
== TGSI_FILE_OUTPUT
) {
2918 reg_ptr
= lp_get_output_ptr(bld
, index
, chan
);
2920 res
= LLVMBuildLoad(builder
, reg_ptr
, "");
2926 emit_dump_reg(gallivm
, file
, index
, chan
, res
);
2934 lp_emit_declaration_soa(
2935 struct lp_build_tgsi_context
*bld_base
,
2936 const struct tgsi_full_declaration
*decl
)
2938 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
2939 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
2940 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
2941 const unsigned first
= decl
->Range
.First
;
2942 const unsigned last
= decl
->Range
.Last
;
2945 assert(last
<= bld
->bld_base
.info
->file_max
[decl
->Declaration
.File
]);
2947 switch (decl
->Declaration
.File
) {
2948 case TGSI_FILE_TEMPORARY
:
2949 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
2950 assert(last
< LP_MAX_INLINED_TEMPS
);
2951 for (idx
= first
; idx
<= last
; ++idx
) {
2952 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
2953 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
2958 case TGSI_FILE_OUTPUT
:
2959 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
2960 for (idx
= first
; idx
<= last
; ++idx
) {
2961 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
2962 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
2963 vec_type
, "output");
2968 case TGSI_FILE_ADDRESS
:
2969 /* ADDR registers are only allocated with an integer LLVM IR type,
2970 * as they are guaranteed to always have integers.
2971 * XXX: Not sure if this exception is worthwhile (or the whole idea of
2972 * an ADDR register for that matter).
2974 assert(last
< LP_MAX_TGSI_ADDRS
);
2975 for (idx
= first
; idx
<= last
; ++idx
) {
2976 assert(idx
< LP_MAX_TGSI_ADDRS
);
2977 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
2978 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, bld_base
->base
.int_vec_type
, "addr");
2982 case TGSI_FILE_PREDICATE
:
2983 assert(last
< LP_MAX_TGSI_PREDS
);
2984 for (idx
= first
; idx
<= last
; ++idx
) {
2985 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
2986 bld
->preds
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
,
2991 case TGSI_FILE_SAMPLER_VIEW
:
2993 * The target stored here MUST match whatever there actually
2994 * is in the set sampler views (what about return type?).
2996 assert(last
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
2997 for (idx
= first
; idx
<= last
; ++idx
) {
2998 bld
->sv
[idx
] = decl
->SamplerView
;
3002 case TGSI_FILE_CONSTANT
:
3005 * We could trivially fetch the per-buffer pointer when fetching the
3006 * constant, relying on llvm to figure out it's always the same pointer
3007 * anyway. However, doing so results in a huge (more than factor of 10)
3008 * slowdown in llvm compilation times for some (but not all) shaders
3009 * (more specifically, the IR optimization spends way more time in
3010 * DominatorTree::dominates). At least with llvm versions 3.1, 3.3.
3012 unsigned idx2D
= decl
->Dim
.Index2D
;
3013 LLVMValueRef index2D
= lp_build_const_int32(gallivm
, idx2D
);
3014 assert(idx2D
< LP_MAX_TGSI_CONST_BUFFERS
);
3015 bld
->consts
[idx2D
] =
3016 lp_build_array_get(gallivm
, bld
->consts_ptr
, index2D
);
3017 bld
->consts_sizes
[idx2D
] =
3018 lp_build_array_get(gallivm
, bld
->const_sizes_ptr
, index2D
);
3023 /* don't need to declare other vars */
3029 void lp_emit_immediate_soa(
3030 struct lp_build_tgsi_context
*bld_base
,
3031 const struct tgsi_full_immediate
*imm
)
3033 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
3034 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3035 LLVMValueRef imms
[4];
3037 const uint size
= imm
->Immediate
.NrTokens
- 1;
3039 switch (imm
->Immediate
.DataType
) {
3040 case TGSI_IMM_FLOAT32
:
3041 for( i
= 0; i
< size
; ++i
)
3043 lp_build_const_vec(gallivm
, bld_base
->base
.type
, imm
->u
[i
].Float
);
3046 case TGSI_IMM_FLOAT64
:
3047 case TGSI_IMM_UINT32
:
3048 for( i
= 0; i
< size
; ++i
) {
3049 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->uint_bld
.type
, imm
->u
[i
].Uint
);
3050 imms
[i
] = LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
3054 case TGSI_IMM_INT32
:
3055 for( i
= 0; i
< size
; ++i
) {
3056 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->int_bld
.type
, imm
->u
[i
].Int
);
3057 imms
[i
] = LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
3062 for( i
= size
; i
< 4; ++i
)
3063 imms
[i
] = bld_base
->base
.undef
;
3065 if (bld
->use_immediates_array
) {
3066 unsigned index
= bld
->num_immediates
;
3067 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
3068 LLVMBuilderRef builder
= gallivm
->builder
;
3070 assert(bld
->indirect_files
& (1 << TGSI_FILE_IMMEDIATE
));
3071 for (i
= 0; i
< 4; ++i
) {
3072 LLVMValueRef lindex
= lp_build_const_int32(
3073 bld
->bld_base
.base
.gallivm
, index
* 4 + i
);
3074 LLVMValueRef imm_ptr
= LLVMBuildGEP(builder
,
3075 bld
->imms_array
, &lindex
, 1, "");
3076 LLVMBuildStore(builder
, imms
[i
], imm_ptr
);
3079 /* simply copy the immediate values into the next immediates[] slot */
3081 assert(imm
->Immediate
.NrTokens
- 1 <= 4);
3082 assert(bld
->num_immediates
< LP_MAX_INLINED_IMMEDIATES
);
3084 for(i
= 0; i
< 4; ++i
)
3085 bld
->immediates
[bld
->num_immediates
][i
] = imms
[i
];
3087 if (bld
->indirect_files
& (1 << TGSI_FILE_IMMEDIATE
)) {
3088 unsigned index
= bld
->num_immediates
;
3089 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
3090 LLVMBuilderRef builder
= gallivm
->builder
;
3091 for (i
= 0; i
< 4; ++i
) {
3092 LLVMValueRef lindex
= lp_build_const_int32(
3093 bld
->bld_base
.base
.gallivm
, index
* 4 + i
);
3094 LLVMValueRef imm_ptr
= LLVMBuildGEP(builder
,
3095 bld
->imms_array
, &lindex
, 1, "");
3096 LLVMBuildStore(builder
,
3097 bld
->immediates
[index
][i
],
3103 bld
->num_immediates
++;
3108 const struct lp_build_tgsi_action
* action
,
3109 struct lp_build_tgsi_context
* bld_base
,
3110 struct lp_build_emit_data
* emit_data
)
3112 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3114 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
,
3115 &emit_data
->output
[emit_data
->chan
], NULL
);
3120 const struct lp_build_tgsi_action
* action
,
3121 struct lp_build_tgsi_context
* bld_base
,
3122 struct lp_build_emit_data
* emit_data
)
3124 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3126 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
, NULL
,
3127 &emit_data
->output
[emit_data
->chan
]);
3132 const struct lp_build_tgsi_action
* action
,
3133 struct lp_build_tgsi_context
* bld_base
,
3134 struct lp_build_emit_data
* emit_data
)
3136 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3138 emit_kill(bld
, bld_base
->pc
- 1);
3143 const struct lp_build_tgsi_action
* action
,
3144 struct lp_build_tgsi_context
* bld_base
,
3145 struct lp_build_emit_data
* emit_data
)
3147 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3149 emit_kill_if(bld
, emit_data
->inst
, bld_base
->pc
- 1);
3154 const struct lp_build_tgsi_action
* action
,
3155 struct lp_build_tgsi_context
* bld_base
,
3156 struct lp_build_emit_data
* emit_data
)
3158 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3160 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3161 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3166 const struct lp_build_tgsi_action
* action
,
3167 struct lp_build_tgsi_context
* bld_base
,
3168 struct lp_build_emit_data
* emit_data
)
3170 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3172 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3173 emit_data
->output
, 2, LP_SAMPLER_OP_TEXTURE
);
3178 const struct lp_build_tgsi_action
* action
,
3179 struct lp_build_tgsi_context
* bld_base
,
3180 struct lp_build_emit_data
* emit_data
)
3182 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3184 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
3185 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3190 const struct lp_build_tgsi_action
* action
,
3191 struct lp_build_tgsi_context
* bld_base
,
3192 struct lp_build_emit_data
* emit_data
)
3194 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3196 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
3197 emit_data
->output
, 2, LP_SAMPLER_OP_TEXTURE
);
3202 const struct lp_build_tgsi_action
* action
,
3203 struct lp_build_tgsi_context
* bld_base
,
3204 struct lp_build_emit_data
* emit_data
)
3206 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3208 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
3209 emit_data
->output
, 3, LP_SAMPLER_OP_TEXTURE
);
3214 const struct lp_build_tgsi_action
* action
,
3215 struct lp_build_tgsi_context
* bld_base
,
3216 struct lp_build_emit_data
* emit_data
)
3218 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3220 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
3221 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3226 const struct lp_build_tgsi_action
* action
,
3227 struct lp_build_tgsi_context
* bld_base
,
3228 struct lp_build_emit_data
* emit_data
)
3230 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3232 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
3233 emit_data
->output
, 2, LP_SAMPLER_OP_TEXTURE
);
3238 const struct lp_build_tgsi_action
* action
,
3239 struct lp_build_tgsi_context
* bld_base
,
3240 struct lp_build_emit_data
* emit_data
)
3242 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3244 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_PROJECTED
,
3245 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3250 const struct lp_build_tgsi_action
* action
,
3251 struct lp_build_tgsi_context
* bld_base
,
3252 struct lp_build_emit_data
* emit_data
)
3254 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3256 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3257 emit_data
->output
, 2, LP_SAMPLER_OP_GATHER
);
3262 const struct lp_build_tgsi_action
* action
,
3263 struct lp_build_tgsi_context
* bld_base
,
3264 struct lp_build_emit_data
* emit_data
)
3266 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3268 emit_size_query(bld
, emit_data
->inst
, emit_data
->output
, FALSE
);
3273 const struct lp_build_tgsi_action
* action
,
3274 struct lp_build_tgsi_context
* bld_base
,
3275 struct lp_build_emit_data
* emit_data
)
3277 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3279 emit_fetch_texels(bld
, emit_data
->inst
, emit_data
->output
, FALSE
);
3284 const struct lp_build_tgsi_action
* action
,
3285 struct lp_build_tgsi_context
* bld_base
,
3286 struct lp_build_emit_data
* emit_data
)
3288 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3290 emit_fetch_texels(bld
, emit_data
->inst
, emit_data
->output
, TRUE
);
3295 const struct lp_build_tgsi_action
* action
,
3296 struct lp_build_tgsi_context
* bld_base
,
3297 struct lp_build_emit_data
* emit_data
)
3299 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3301 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3302 FALSE
, emit_data
->output
);
3307 const struct lp_build_tgsi_action
* action
,
3308 struct lp_build_tgsi_context
* bld_base
,
3309 struct lp_build_emit_data
* emit_data
)
3311 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3313 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
3314 FALSE
, emit_data
->output
);
3319 const struct lp_build_tgsi_action
* action
,
3320 struct lp_build_tgsi_context
* bld_base
,
3321 struct lp_build_emit_data
* emit_data
)
3323 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3325 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3326 TRUE
, emit_data
->output
);
3331 const struct lp_build_tgsi_action
* action
,
3332 struct lp_build_tgsi_context
* bld_base
,
3333 struct lp_build_emit_data
* emit_data
)
3335 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3337 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_ZERO
,
3338 TRUE
, emit_data
->output
);
3343 const struct lp_build_tgsi_action
* action
,
3344 struct lp_build_tgsi_context
* bld_base
,
3345 struct lp_build_emit_data
* emit_data
)
3347 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3349 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
3350 FALSE
, emit_data
->output
);
3355 const struct lp_build_tgsi_action
* action
,
3356 struct lp_build_tgsi_context
* bld_base
,
3357 struct lp_build_emit_data
* emit_data
)
3359 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3361 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
3362 FALSE
, emit_data
->output
);
3367 const struct lp_build_tgsi_action
* action
,
3368 struct lp_build_tgsi_context
* bld_base
,
3369 struct lp_build_emit_data
* emit_data
)
3371 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3373 emit_size_query(bld
, emit_data
->inst
, emit_data
->output
, TRUE
);
3377 mask_vec(struct lp_build_tgsi_context
*bld_base
)
3379 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3380 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3381 struct lp_exec_mask
*exec_mask
= &bld
->exec_mask
;
3383 if (!exec_mask
->has_mask
) {
3384 return lp_build_mask_value(bld
->mask
);
3386 return LLVMBuildAnd(builder
, lp_build_mask_value(bld
->mask
),
3387 exec_mask
->exec_mask
, "");
3391 increment_vec_ptr_by_mask(struct lp_build_tgsi_context
* bld_base
,
3395 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3396 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
3398 current_vec
= LLVMBuildSub(builder
, current_vec
, mask
, "");
3400 LLVMBuildStore(builder
, current_vec
, ptr
);
3404 clear_uint_vec_ptr_from_mask(struct lp_build_tgsi_context
* bld_base
,
3408 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3409 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
3411 current_vec
= lp_build_select(&bld_base
->uint_bld
,
3413 bld_base
->uint_bld
.zero
,
3416 LLVMBuildStore(builder
, current_vec
, ptr
);
3420 clamp_mask_to_max_output_vertices(struct lp_build_tgsi_soa_context
* bld
,
3421 LLVMValueRef current_mask_vec
,
3422 LLVMValueRef total_emitted_vertices_vec
)
3424 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3425 struct lp_build_context
*int_bld
= &bld
->bld_base
.int_bld
;
3426 LLVMValueRef max_mask
= lp_build_cmp(int_bld
, PIPE_FUNC_LESS
,
3427 total_emitted_vertices_vec
,
3428 bld
->max_output_vertices_vec
);
3430 return LLVMBuildAnd(builder
, current_mask_vec
, max_mask
, "");
3435 const struct lp_build_tgsi_action
* action
,
3436 struct lp_build_tgsi_context
* bld_base
,
3437 struct lp_build_emit_data
* emit_data
)
3439 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3440 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3442 if (bld
->gs_iface
->emit_vertex
) {
3443 LLVMValueRef mask
= mask_vec(bld_base
);
3444 LLVMValueRef total_emitted_vertices_vec
=
3445 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
3446 mask
= clamp_mask_to_max_output_vertices(bld
, mask
,
3447 total_emitted_vertices_vec
);
3448 gather_outputs(bld
);
3449 bld
->gs_iface
->emit_vertex(bld
->gs_iface
, &bld
->bld_base
,
3451 total_emitted_vertices_vec
);
3452 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
3454 increment_vec_ptr_by_mask(bld_base
, bld
->total_emitted_vertices_vec_ptr
,
3457 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3458 " +++ emit vertex masked ones = ",
3460 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3461 " +++ emit vertex emitted = ",
3462 total_emitted_vertices_vec
);
3469 end_primitive_masked(struct lp_build_tgsi_context
* bld_base
,
3472 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3473 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3475 if (bld
->gs_iface
->end_primitive
) {
3476 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3477 LLVMValueRef emitted_vertices_vec
=
3478 LLVMBuildLoad(builder
, bld
->emitted_vertices_vec_ptr
, "");
3479 LLVMValueRef emitted_prims_vec
=
3480 LLVMBuildLoad(builder
, bld
->emitted_prims_vec_ptr
, "");
3482 LLVMValueRef emitted_mask
= lp_build_cmp(uint_bld
, PIPE_FUNC_NOTEQUAL
,
3483 emitted_vertices_vec
,
3485 /* We need to combine the current execution mask with the mask
3486 telling us which, if any, execution slots actually have
3487 unemitted primitives, this way we make sure that end_primitives
3488 executes only on the paths that have unflushed vertices */
3489 mask
= LLVMBuildAnd(builder
, mask
, emitted_mask
, "");
3491 bld
->gs_iface
->end_primitive(bld
->gs_iface
, &bld
->bld_base
,
3492 emitted_vertices_vec
,
3496 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3497 " +++ end prim masked ones = ",
3499 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3500 " +++ end prim emitted verts1 = ",
3501 emitted_vertices_vec
);
3502 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3503 " +++ end prim emitted prims1 = ",
3504 LLVMBuildLoad(builder
,
3505 bld
->emitted_prims_vec_ptr
, ""));
3507 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_prims_vec_ptr
,
3509 clear_uint_vec_ptr_from_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
3512 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3513 " +++ end prim emitted verts2 = ",
3514 LLVMBuildLoad(builder
,
3515 bld
->emitted_vertices_vec_ptr
, ""));
3523 const struct lp_build_tgsi_action
* action
,
3524 struct lp_build_tgsi_context
* bld_base
,
3525 struct lp_build_emit_data
* emit_data
)
3527 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3529 if (bld
->gs_iface
->end_primitive
) {
3530 LLVMValueRef mask
= mask_vec(bld_base
);
3531 end_primitive_masked(bld_base
, mask
);
3537 const struct lp_build_tgsi_action
* action
,
3538 struct lp_build_tgsi_context
* bld_base
,
3539 struct lp_build_emit_data
* emit_data
)
3541 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3543 lp_exec_mask_call(&bld
->exec_mask
, emit_data
->inst
->Label
.Label
,
3549 const struct lp_build_tgsi_action
* action
,
3550 struct lp_build_tgsi_context
* bld_base
,
3551 struct lp_build_emit_data
* emit_data
)
3553 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3555 lp_exec_mask_ret(&bld
->exec_mask
, &bld_base
->pc
);
3560 const struct lp_build_tgsi_action
* action
,
3561 struct lp_build_tgsi_context
* bld_base
,
3562 struct lp_build_emit_data
* emit_data
)
3564 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3566 lp_exec_break(&bld
->exec_mask
, bld_base
);
3571 const struct lp_build_tgsi_action
* action
,
3572 struct lp_build_tgsi_context
* bld_base
,
3573 struct lp_build_emit_data
* emit_data
)
3575 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3576 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3577 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3578 LLVMValueRef unsigned_cond
=
3579 LLVMBuildBitCast(builder
, emit_data
->args
[0], uint_bld
->vec_type
, "");
3580 LLVMValueRef cond
= lp_build_cmp(uint_bld
, PIPE_FUNC_NOTEQUAL
,
3584 lp_exec_break_condition(&bld
->exec_mask
, cond
);
3589 const struct lp_build_tgsi_action
* action
,
3590 struct lp_build_tgsi_context
* bld_base
,
3591 struct lp_build_emit_data
* emit_data
)
3594 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3596 tmp
= lp_build_cmp(&bld_base
->base
, PIPE_FUNC_NOTEQUAL
,
3597 emit_data
->args
[0], bld
->bld_base
.base
.zero
);
3598 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
3603 const struct lp_build_tgsi_action
* action
,
3604 struct lp_build_tgsi_context
* bld_base
,
3605 struct lp_build_emit_data
* emit_data
)
3608 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3609 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3611 tmp
= lp_build_cmp(uint_bld
, PIPE_FUNC_NOTEQUAL
,
3612 emit_data
->args
[0], uint_bld
->zero
);
3613 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
3618 const struct lp_build_tgsi_action
* action
,
3619 struct lp_build_tgsi_context
* bld_base
,
3620 struct lp_build_emit_data
* emit_data
)
3622 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3624 lp_exec_case(&bld
->exec_mask
, emit_data
->args
[0]);
3629 const struct lp_build_tgsi_action
* action
,
3630 struct lp_build_tgsi_context
* bld_base
,
3631 struct lp_build_emit_data
* emit_data
)
3633 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3635 lp_exec_default(&bld
->exec_mask
, bld_base
);
3640 const struct lp_build_tgsi_action
* action
,
3641 struct lp_build_tgsi_context
* bld_base
,
3642 struct lp_build_emit_data
* emit_data
)
3644 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3646 lp_exec_switch(&bld
->exec_mask
, emit_data
->args
[0]);
3651 const struct lp_build_tgsi_action
* action
,
3652 struct lp_build_tgsi_context
* bld_base
,
3653 struct lp_build_emit_data
* emit_data
)
3655 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3657 lp_exec_endswitch(&bld
->exec_mask
, bld_base
);
3662 const struct lp_build_tgsi_action
* action
,
3663 struct lp_build_tgsi_context
* bld_base
,
3664 struct lp_build_emit_data
* emit_data
)
3666 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3668 lp_exec_bgnloop(&bld
->exec_mask
);
3673 const struct lp_build_tgsi_action
* action
,
3674 struct lp_build_tgsi_context
* bld_base
,
3675 struct lp_build_emit_data
* emit_data
)
3677 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3679 lp_exec_mask_bgnsub(&bld
->exec_mask
);
3684 const struct lp_build_tgsi_action
* action
,
3685 struct lp_build_tgsi_context
* bld_base
,
3686 struct lp_build_emit_data
* emit_data
)
3688 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3690 lp_exec_mask_cond_invert(&bld
->exec_mask
);
3695 const struct lp_build_tgsi_action
* action
,
3696 struct lp_build_tgsi_context
* bld_base
,
3697 struct lp_build_emit_data
* emit_data
)
3699 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3701 lp_exec_mask_cond_pop(&bld
->exec_mask
);
3706 const struct lp_build_tgsi_action
* action
,
3707 struct lp_build_tgsi_context
* bld_base
,
3708 struct lp_build_emit_data
* emit_data
)
3710 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3712 lp_exec_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
3717 const struct lp_build_tgsi_action
* action
,
3718 struct lp_build_tgsi_context
* bld_base
,
3719 struct lp_build_emit_data
* emit_data
)
3721 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3723 lp_exec_mask_endsub(&bld
->exec_mask
, &bld_base
->pc
);
3728 const struct lp_build_tgsi_action
* action
,
3729 struct lp_build_tgsi_context
* bld_base
,
3730 struct lp_build_emit_data
* emit_data
)
3732 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3734 lp_exec_continue(&bld
->exec_mask
);
3737 static void emit_prologue(struct lp_build_tgsi_context
* bld_base
)
3739 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3740 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3742 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
3743 LLVMValueRef array_size
=
3744 lp_build_const_int32(gallivm
,
3745 bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
3746 bld
->temps_array
= lp_build_array_alloca(gallivm
,
3747 bld_base
->base
.vec_type
, array_size
,
3751 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
3752 LLVMValueRef array_size
=
3753 lp_build_const_int32(gallivm
,
3754 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
3755 bld
->outputs_array
= lp_build_array_alloca(gallivm
,
3756 bld_base
->base
.vec_type
, array_size
,
3760 if (bld
->indirect_files
& (1 << TGSI_FILE_IMMEDIATE
)) {
3761 LLVMValueRef array_size
=
3762 lp_build_const_int32(gallivm
,
3763 bld_base
->info
->file_max
[TGSI_FILE_IMMEDIATE
] * 4 + 4);
3764 bld
->imms_array
= lp_build_array_alloca(gallivm
,
3765 bld_base
->base
.vec_type
, array_size
,
3769 /* If we have indirect addressing in inputs we need to copy them into
3770 * our alloca array to be able to iterate over them */
3771 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
) && !bld
->gs_iface
) {
3772 unsigned index
, chan
;
3773 LLVMTypeRef vec_type
= bld_base
->base
.vec_type
;
3774 LLVMValueRef array_size
= lp_build_const_int32(gallivm
,
3775 bld_base
->info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
3776 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
3777 vec_type
, array_size
,
3780 assert(bld_base
->info
->num_inputs
3781 <= bld_base
->info
->file_max
[TGSI_FILE_INPUT
] + 1);
3783 for (index
= 0; index
< bld_base
->info
->num_inputs
; ++index
) {
3784 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
3785 LLVMValueRef lindex
=
3786 lp_build_const_int32(gallivm
, index
* 4 + chan
);
3787 LLVMValueRef input_ptr
=
3788 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
3790 LLVMValueRef value
= bld
->inputs
[index
][chan
];
3792 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
3797 if (bld
->gs_iface
) {
3798 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
3799 bld
->emitted_prims_vec_ptr
=
3800 lp_build_alloca(gallivm
,
3802 "emitted_prims_ptr");
3803 bld
->emitted_vertices_vec_ptr
=
3804 lp_build_alloca(gallivm
,
3806 "emitted_vertices_ptr");
3807 bld
->total_emitted_vertices_vec_ptr
=
3808 lp_build_alloca(gallivm
,
3810 "total_emitted_vertices_ptr");
3812 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
3813 bld
->emitted_prims_vec_ptr
);
3814 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
3815 bld
->emitted_vertices_vec_ptr
);
3816 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
3817 bld
->total_emitted_vertices_vec_ptr
);
3820 if (DEBUG_EXECUTION
) {
3821 lp_build_printf(gallivm
, "\n");
3822 emit_dump_file(bld
, TGSI_FILE_CONSTANT
);
3824 emit_dump_file(bld
, TGSI_FILE_INPUT
);
3828 static void emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
3830 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3831 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3833 if (DEBUG_EXECUTION
) {
3836 emit_dump_file(bld
, TGSI_FILE_TEMPORARY
);
3838 emit_dump_file(bld
, TGSI_FILE_OUTPUT
);
3839 lp_build_printf(bld_base
->base
.gallivm
, "\n");
3842 /* If we have indirect addressing in outputs we need to copy our alloca array
3843 * to the outputs slots specified by the caller */
3844 if (bld
->gs_iface
) {
3845 LLVMValueRef total_emitted_vertices_vec
;
3846 LLVMValueRef emitted_prims_vec
;
3847 /* implicit end_primitives, needed in case there are any unflushed
3848 vertices in the cache. Note must not call end_primitive here
3849 since the exec_mask is not valid at this point. */
3850 end_primitive_masked(bld_base
, lp_build_mask_value(bld
->mask
));
3852 total_emitted_vertices_vec
=
3853 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
3855 LLVMBuildLoad(builder
, bld
->emitted_prims_vec_ptr
, "");
3857 bld
->gs_iface
->gs_epilogue(bld
->gs_iface
,
3859 total_emitted_vertices_vec
,
3862 gather_outputs(bld
);
3867 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
3868 const struct tgsi_token
*tokens
,
3869 struct lp_type type
,
3870 struct lp_build_mask_context
*mask
,
3871 LLVMValueRef consts_ptr
,
3872 LLVMValueRef const_sizes_ptr
,
3873 const struct lp_bld_tgsi_system_values
*system_values
,
3874 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
3875 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
3876 LLVMValueRef context_ptr
,
3877 LLVMValueRef thread_data_ptr
,
3878 struct lp_build_sampler_soa
*sampler
,
3879 const struct tgsi_shader_info
*info
,
3880 const struct lp_build_tgsi_gs_iface
*gs_iface
)
3882 struct lp_build_tgsi_soa_context bld
;
3884 struct lp_type res_type
;
3886 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
3887 memset(&res_type
, 0, sizeof res_type
);
3888 res_type
.width
= type
.width
;
3889 res_type
.length
= type
.length
;
3892 /* Setup build context */
3893 memset(&bld
, 0, sizeof bld
);
3894 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
3895 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
3896 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
3897 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
3899 struct lp_type dbl_type
;
3901 dbl_type
.width
*= 2;
3902 lp_build_context_init(&bld
.bld_base
.dbl_bld
, gallivm
, dbl_type
);
3905 bld
.inputs
= inputs
;
3906 bld
.outputs
= outputs
;
3907 bld
.consts_ptr
= consts_ptr
;
3908 bld
.const_sizes_ptr
= const_sizes_ptr
;
3909 bld
.sampler
= sampler
;
3910 bld
.bld_base
.info
= info
;
3911 bld
.indirect_files
= info
->indirect_files
;
3912 bld
.context_ptr
= context_ptr
;
3913 bld
.thread_data_ptr
= thread_data_ptr
;
3916 * If the number of temporaries is rather large then we just
3917 * allocate them as an array right from the start and treat
3918 * like indirect temporaries.
3920 if (info
->file_max
[TGSI_FILE_TEMPORARY
] >= LP_MAX_INLINED_TEMPS
) {
3921 bld
.indirect_files
|= (1 << TGSI_FILE_TEMPORARY
);
3924 * For performance reason immediates are always backed in a static
3925 * array, but if their number is too great, we have to use just
3926 * a dynamically allocated array.
3928 bld
.use_immediates_array
=
3929 (info
->file_max
[TGSI_FILE_IMMEDIATE
] >= LP_MAX_INLINED_IMMEDIATES
);
3930 if (bld
.use_immediates_array
) {
3931 bld
.indirect_files
|= (1 << TGSI_FILE_IMMEDIATE
);
3935 bld
.bld_base
.soa
= TRUE
;
3936 bld
.bld_base
.emit_debug
= emit_debug
;
3937 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
3938 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
3939 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
3940 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
3941 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = emit_fetch_system_value
;
3942 bld
.bld_base
.emit_store
= emit_store
;
3944 bld
.bld_base
.emit_declaration
= lp_emit_declaration_soa
;
3945 bld
.bld_base
.emit_immediate
= lp_emit_immediate_soa
;
3947 bld
.bld_base
.emit_prologue
= emit_prologue
;
3948 bld
.bld_base
.emit_epilogue
= emit_epilogue
;
3950 /* Set opcode actions */
3951 lp_set_default_actions_cpu(&bld
.bld_base
);
3953 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
3954 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNSUB
].emit
= bgnsub_emit
;
3955 bld
.bld_base
.op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
3956 bld
.bld_base
.op_actions
[TGSI_OPCODE_BREAKC
].emit
= breakc_emit
;
3957 bld
.bld_base
.op_actions
[TGSI_OPCODE_CAL
].emit
= cal_emit
;
3958 bld
.bld_base
.op_actions
[TGSI_OPCODE_CASE
].emit
= case_emit
;
3959 bld
.bld_base
.op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
3960 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDX
].emit
= ddx_emit
;
3961 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDY
].emit
= ddy_emit
;
3962 bld
.bld_base
.op_actions
[TGSI_OPCODE_DEFAULT
].emit
= default_emit
;
3963 bld
.bld_base
.op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
3964 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
3965 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
3966 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSUB
].emit
= endsub_emit
;
3967 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSWITCH
].emit
= endswitch_emit
;
3968 bld
.bld_base
.op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
3969 bld
.bld_base
.op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
3970 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILL_IF
].emit
= kill_if_emit
;
3971 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILL
].emit
= kill_emit
;
3972 bld
.bld_base
.op_actions
[TGSI_OPCODE_RET
].emit
= ret_emit
;
3973 bld
.bld_base
.op_actions
[TGSI_OPCODE_SWITCH
].emit
= switch_emit
;
3974 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX
].emit
= tex_emit
;
3975 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB
].emit
= txb_emit
;
3976 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXD
].emit
= txd_emit
;
3977 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL
].emit
= txl_emit
;
3978 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXP
].emit
= txp_emit
;
3979 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
3980 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXF
].emit
= txf_emit
;
3981 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX2
].emit
= tex2_emit
;
3982 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB2
].emit
= txb2_emit
;
3983 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL2
].emit
= txl2_emit
;
3984 bld
.bld_base
.op_actions
[TGSI_OPCODE_TG4
].emit
= tg4_emit
;
3985 /* DX10 sampling ops */
3986 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE
].emit
= sample_emit
;
3987 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_B
].emit
= sample_b_emit
;
3988 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_C
].emit
= sample_c_emit
;
3989 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_C_LZ
].emit
= sample_c_lz_emit
;
3990 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_D
].emit
= sample_d_emit
;
3991 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_I
].emit
= sample_i_emit
;
3992 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_L
].emit
= sample_l_emit
;
3993 bld
.bld_base
.op_actions
[TGSI_OPCODE_SVIEWINFO
].emit
= sviewinfo_emit
;
3996 /* There's no specific value for this because it should always
3997 * be set, but apps using ext_geometry_shader4 quite often
3998 * were forgetting so we're using MAX_VERTEX_VARYING from
3999 * that spec even though we could debug_assert if it's not
4000 * set, but that's a lot uglier. */
4001 uint max_output_vertices
;
4003 /* inputs are always indirect with gs */
4004 bld
.indirect_files
|= (1 << TGSI_FILE_INPUT
);
4005 bld
.gs_iface
= gs_iface
;
4006 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_gs_input
;
4007 bld
.bld_base
.op_actions
[TGSI_OPCODE_EMIT
].emit
= emit_vertex
;
4008 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= end_primitive
;
4010 max_output_vertices
=
4011 info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
4012 if (!max_output_vertices
)
4013 max_output_vertices
= 32;
4015 bld
.max_output_vertices_vec
=
4016 lp_build_const_int_vec(gallivm
, bld
.bld_base
.int_bld
.type
,
4017 max_output_vertices
);
4020 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.int_bld
);
4022 bld
.system_values
= *system_values
;
4024 lp_build_tgsi_llvm(&bld
.bld_base
, tokens
);
4027 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
4028 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
4029 debug_printf("11111111111111111111111111111 \n");
4030 tgsi_dump(tokens
, 0);
4031 lp_debug_dump_value(function
);
4032 debug_printf("2222222222222222222222222222 \n");
4036 LLVMModuleRef module
= LLVMGetGlobalParent(
4037 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
4038 LLVMDumpModule(module
);
4041 lp_exec_mask_fini(&bld
.exec_mask
);