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 int 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
!= ~0u && 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
,
757 LLVMValueRef dst_ptr
)
759 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
760 LLVMValueRef exec_mask
= mask
->has_mask
? mask
->exec_mask
: NULL
;
762 assert(lp_check_value(bld_store
->type
, val
));
763 assert(LLVMGetTypeKind(LLVMTypeOf(dst_ptr
)) == LLVMPointerTypeKind
);
764 assert(LLVMGetElementType(LLVMTypeOf(dst_ptr
)) == LLVMTypeOf(val
));
767 LLVMValueRef res
, dst
;
769 dst
= LLVMBuildLoad(builder
, dst_ptr
, "");
770 res
= lp_build_select(bld_store
, exec_mask
, val
, dst
);
771 LLVMBuildStore(builder
, res
, dst_ptr
);
773 LLVMBuildStore(builder
, val
, dst_ptr
);
776 static void lp_exec_mask_call(struct lp_exec_mask
*mask
,
780 if (mask
->function_stack_size
>= LP_MAX_NUM_FUNCS
) {
784 lp_exec_mask_function_init(mask
, mask
->function_stack_size
);
785 mask
->function_stack
[mask
->function_stack_size
].pc
= *pc
;
786 mask
->function_stack
[mask
->function_stack_size
].ret_mask
= mask
->ret_mask
;
787 mask
->function_stack_size
++;
791 static void lp_exec_mask_ret(struct lp_exec_mask
*mask
, int *pc
)
793 LLVMBuilderRef builder
= mask
->bld
->gallivm
->builder
;
794 struct function_ctx
*ctx
= func_ctx(mask
);
795 LLVMValueRef exec_mask
;
797 if (ctx
->cond_stack_size
== 0 &&
798 ctx
->loop_stack_size
== 0 &&
799 ctx
->switch_stack_size
== 0 &&
800 mask
->function_stack_size
== 1) {
801 /* returning from main() */
806 if (mask
->function_stack_size
== 1) {
808 * This requires special handling since we need to ensure
809 * we don't drop the mask even if we have no call stack
810 * (e.g. after a ret in a if clause after the endif)
812 mask
->ret_in_main
= TRUE
;
815 exec_mask
= LLVMBuildNot(builder
,
819 mask
->ret_mask
= LLVMBuildAnd(builder
,
821 exec_mask
, "ret_full");
823 lp_exec_mask_update(mask
);
826 static void lp_exec_mask_bgnsub(struct lp_exec_mask
*mask
)
830 static void lp_exec_mask_endsub(struct lp_exec_mask
*mask
, int *pc
)
832 struct function_ctx
*ctx
;
834 assert(mask
->function_stack_size
> 1);
835 assert(mask
->function_stack_size
<= LP_MAX_NUM_FUNCS
);
837 ctx
= func_ctx(mask
);
838 mask
->function_stack_size
--;
841 mask
->ret_mask
= ctx
->ret_mask
;
843 lp_exec_mask_update(mask
);
848 get_file_ptr(struct lp_build_tgsi_soa_context
*bld
,
853 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
854 LLVMValueRef (*array_of_vars
)[TGSI_NUM_CHANNELS
];
855 LLVMValueRef var_of_array
;
858 case TGSI_FILE_TEMPORARY
:
859 array_of_vars
= bld
->temps
;
860 var_of_array
= bld
->temps_array
;
862 case TGSI_FILE_OUTPUT
:
863 array_of_vars
= bld
->outputs
;
864 var_of_array
= bld
->outputs_array
;
873 if (bld
->indirect_files
& (1 << file
)) {
874 LLVMValueRef lindex
= lp_build_const_int32(bld
->bld_base
.base
.gallivm
, index
* 4 + chan
);
875 return LLVMBuildGEP(builder
, var_of_array
, &lindex
, 1, "");
878 assert(index
<= bld
->bld_base
.info
->file_max
[file
]);
879 return array_of_vars
[index
][chan
];
885 * Return pointer to a temporary register channel (src or dest).
886 * Note that indirect addressing cannot be handled here.
887 * \param index which temporary register
888 * \param chan which channel of the temp register.
891 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context
*bld
,
895 return get_file_ptr(bld
, TGSI_FILE_TEMPORARY
, index
, chan
);
899 * Return pointer to a output register channel (src or dest).
900 * Note that indirect addressing cannot be handled here.
901 * \param index which output register
902 * \param chan which channel of the output register.
905 lp_get_output_ptr(struct lp_build_tgsi_soa_context
*bld
,
909 return get_file_ptr(bld
, TGSI_FILE_OUTPUT
, index
, chan
);
913 * If we have indirect addressing in outputs copy our alloca array
914 * to the outputs slots specified by the caller to make sure
915 * our outputs are delivered consistently via the same interface.
918 gather_outputs(struct lp_build_tgsi_soa_context
* bld
)
920 if ((bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
921 unsigned index
, chan
;
922 assert(bld
->bld_base
.info
->num_outputs
<=
923 bld
->bld_base
.info
->file_max
[TGSI_FILE_OUTPUT
] + 1);
924 for (index
= 0; index
< bld
->bld_base
.info
->num_outputs
; ++index
) {
925 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
926 bld
->outputs
[index
][chan
] = lp_get_output_ptr(bld
, index
, chan
);
934 * XXX the lp_build_gather() function should be capable of doing this
935 * with a little work.
938 build_gather(struct lp_build_tgsi_context
*bld_base
,
939 LLVMValueRef base_ptr
,
940 LLVMValueRef indexes
,
941 LLVMValueRef overflow_mask
,
942 LLVMValueRef indexes2
)
944 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
945 LLVMBuilderRef builder
= gallivm
->builder
;
946 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
947 struct lp_build_context
*bld
= &bld_base
->base
;
952 res
= LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), bld_base
->base
.type
.length
* 2));
956 * overflow_mask is a vector telling us which channels
957 * in the vector overflowed. We use the overflow behavior for
958 * constant buffers which is defined as:
959 * Out of bounds access to constant buffer returns 0 in all
960 * components. Out of bounds behavior is always with respect
961 * to the size of the buffer bound at that slot.
966 * We avoid per-element control flow here (also due to llvm going crazy,
967 * though I suspect it's better anyway since overflow is likely rare).
968 * Note that since we still fetch from buffers even if num_elements was
969 * zero (in this case we'll fetch from index zero) the jit func callers
970 * MUST provide valid fake constant buffers of size 4x32 (the values do
971 * not matter), otherwise we'd still need (not per element though)
974 indexes
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes
);
976 indexes2
= lp_build_select(uint_bld
, overflow_mask
, uint_bld
->zero
, indexes2
);
980 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
982 for (i
= 0; i
< bld
->type
.length
* (indexes2
? 2 : 1); i
++) {
985 LLVMValueRef scalar_ptr
, scalar
;
987 di
= lp_build_const_int32(bld
->gallivm
, i
);
989 si
= lp_build_const_int32(bld
->gallivm
, i
>> 1);
993 if (indexes2
&& (i
& 1)) {
994 index
= LLVMBuildExtractElement(builder
,
997 index
= LLVMBuildExtractElement(builder
,
1000 scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
,
1001 &index
, 1, "gather_ptr");
1002 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
1004 res
= LLVMBuildInsertElement(builder
, res
, scalar
, di
, "");
1007 if (overflow_mask
) {
1009 res
= LLVMBuildBitCast(builder
, res
, bld_base
->dbl_bld
.vec_type
, "");
1010 overflow_mask
= LLVMBuildSExt(builder
, overflow_mask
,
1011 bld_base
->dbl_bld
.int_vec_type
, "");
1012 res
= lp_build_select(&bld_base
->dbl_bld
, overflow_mask
,
1013 bld_base
->dbl_bld
.zero
, res
);
1015 res
= lp_build_select(bld
, overflow_mask
, bld
->zero
, res
);
1023 * Scatter/store vector.
1026 emit_mask_scatter(struct lp_build_tgsi_soa_context
*bld
,
1027 LLVMValueRef base_ptr
,
1028 LLVMValueRef indexes
,
1029 LLVMValueRef values
,
1030 struct lp_exec_mask
*mask
)
1032 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1033 LLVMBuilderRef builder
= gallivm
->builder
;
1035 LLVMValueRef pred
= mask
->has_mask
? mask
->exec_mask
: NULL
;
1038 * Loop over elements of index_vec, store scalar value.
1040 for (i
= 0; i
< bld
->bld_base
.base
.type
.length
; i
++) {
1041 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1042 LLVMValueRef index
= LLVMBuildExtractElement(builder
, indexes
, ii
, "");
1043 LLVMValueRef scalar_ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "scatter_ptr");
1044 LLVMValueRef val
= LLVMBuildExtractElement(builder
, values
, ii
, "scatter_val");
1045 LLVMValueRef scalar_pred
= pred
?
1046 LLVMBuildExtractElement(builder
, pred
, ii
, "scatter_pred") : NULL
;
1049 lp_build_printf(gallivm
, "scatter %d: val %f at %d %p\n",
1050 ii
, val
, index
, scalar_ptr
);
1053 LLVMValueRef real_val
, dst_val
;
1054 dst_val
= LLVMBuildLoad(builder
, scalar_ptr
, "");
1055 real_val
= lp_build_select(&bld
->elem_bld
, scalar_pred
, val
, dst_val
);
1056 LLVMBuildStore(builder
, real_val
, scalar_ptr
);
1059 LLVMBuildStore(builder
, val
, scalar_ptr
);
1066 * Read the current value of the ADDR register, convert the floats to
1067 * ints, add the base index and return the vector of offsets.
1068 * The offsets will be used to index into the constant buffer or
1069 * temporary register file.
1072 get_indirect_index(struct lp_build_tgsi_soa_context
*bld
,
1073 unsigned reg_file
, unsigned reg_index
,
1074 const struct tgsi_ind_register
*indirect_reg
)
1076 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
1077 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
1078 /* always use X component of address register */
1079 unsigned swizzle
= indirect_reg
->Swizzle
;
1082 LLVMValueRef max_index
;
1085 assert(bld
->indirect_files
& (1 << reg_file
));
1087 base
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
, uint_bld
->type
, reg_index
);
1089 assert(swizzle
< 4);
1090 switch (indirect_reg
->File
) {
1091 case TGSI_FILE_ADDRESS
:
1092 rel
= LLVMBuildLoad(builder
,
1093 bld
->addr
[indirect_reg
->Index
][swizzle
],
1095 /* ADDR LLVM values already have LLVM integer type. */
1097 case TGSI_FILE_TEMPORARY
:
1098 rel
= lp_get_temp_ptr_soa(bld
, indirect_reg
->Index
, swizzle
);
1099 rel
= LLVMBuildLoad(builder
, rel
, "load temp reg");
1100 /* TEMP LLVM values always have LLVM float type, but for indirection, the
1101 * value actually stored is expected to be an integer */
1102 rel
= LLVMBuildBitCast(builder
, rel
, uint_bld
->vec_type
, "");
1106 rel
= uint_bld
->zero
;
1109 index
= lp_build_add(uint_bld
, base
, rel
);
1112 * emit_fetch_constant handles constant buffer overflow so this code
1113 * is pointless for them.
1114 * Furthermore the D3D10 spec in section 6.5 says:
1115 * If the constant buffer bound to a slot is larger than the size
1116 * declared in the shader for that slot, implementations are allowed
1117 * to return incorrect data (not necessarily 0) for indices that are
1118 * larger than the declared size but smaller than the buffer size.
1120 if (reg_file
!= TGSI_FILE_CONSTANT
) {
1121 max_index
= lp_build_const_int_vec(bld
->bld_base
.base
.gallivm
,
1123 bld
->bld_base
.info
->file_max
[reg_file
]);
1125 assert(!uint_bld
->type
.sign
);
1126 index
= lp_build_min(uint_bld
, index
, max_index
);
1132 static struct lp_build_context
*
1133 stype_to_fetch(struct lp_build_tgsi_context
* bld_base
,
1134 enum tgsi_opcode_type stype
)
1136 struct lp_build_context
*bld_fetch
;
1139 case TGSI_TYPE_FLOAT
:
1140 case TGSI_TYPE_UNTYPED
:
1141 bld_fetch
= &bld_base
->base
;
1143 case TGSI_TYPE_UNSIGNED
:
1144 bld_fetch
= &bld_base
->uint_bld
;
1146 case TGSI_TYPE_SIGNED
:
1147 bld_fetch
= &bld_base
->int_bld
;
1149 case TGSI_TYPE_DOUBLE
:
1150 bld_fetch
= &bld_base
->dbl_bld
;
1152 case TGSI_TYPE_UNSIGNED64
:
1153 bld_fetch
= &bld_base
->uint64_bld
;
1155 case TGSI_TYPE_SIGNED64
:
1156 bld_fetch
= &bld_base
->int64_bld
;
1158 case TGSI_TYPE_VOID
:
1168 get_soa_array_offsets(struct lp_build_context
*uint_bld
,
1169 LLVMValueRef indirect_index
,
1170 unsigned chan_index
,
1171 boolean need_perelement_offset
)
1173 struct gallivm_state
*gallivm
= uint_bld
->gallivm
;
1174 LLVMValueRef chan_vec
=
1175 lp_build_const_int_vec(uint_bld
->gallivm
, uint_bld
->type
, chan_index
);
1176 LLVMValueRef length_vec
=
1177 lp_build_const_int_vec(gallivm
, uint_bld
->type
, uint_bld
->type
.length
);
1178 LLVMValueRef index_vec
;
1180 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1181 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1182 index_vec
= lp_build_add(uint_bld
, index_vec
, chan_vec
);
1183 index_vec
= lp_build_mul(uint_bld
, index_vec
, length_vec
);
1185 if (need_perelement_offset
) {
1186 LLVMValueRef pixel_offsets
;
1188 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1189 pixel_offsets
= uint_bld
->undef
;
1190 for (i
= 0; i
< uint_bld
->type
.length
; i
++) {
1191 LLVMValueRef ii
= lp_build_const_int32(gallivm
, i
);
1192 pixel_offsets
= LLVMBuildInsertElement(gallivm
->builder
, pixel_offsets
,
1195 index_vec
= lp_build_add(uint_bld
, index_vec
, pixel_offsets
);
1201 emit_fetch_constant(
1202 struct lp_build_tgsi_context
* bld_base
,
1203 const struct tgsi_full_src_register
* reg
,
1204 enum tgsi_opcode_type stype
,
1207 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1208 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1209 LLVMBuilderRef builder
= gallivm
->builder
;
1210 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1211 unsigned dimension
= 0;
1212 LLVMValueRef consts_ptr
;
1213 LLVMValueRef num_consts
;
1216 /* XXX: Handle fetching xyzw components as a vector */
1217 assert(swizzle
!= ~0u);
1219 if (reg
->Register
.Dimension
) {
1220 assert(!reg
->Dimension
.Indirect
);
1221 dimension
= reg
->Dimension
.Index
;
1222 assert(dimension
< LP_MAX_TGSI_CONST_BUFFERS
);
1225 consts_ptr
= bld
->consts
[dimension
];
1226 num_consts
= bld
->consts_sizes
[dimension
];
1228 if (reg
->Register
.Indirect
) {
1229 LLVMValueRef indirect_index
;
1230 LLVMValueRef swizzle_vec
=
1231 lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
);
1232 LLVMValueRef index_vec
; /* index into the const buffer */
1233 LLVMValueRef overflow_mask
;
1234 LLVMValueRef index_vec2
= NULL
;
1236 indirect_index
= get_indirect_index(bld
,
1238 reg
->Register
.Index
,
1241 /* All fetches are from the same constant buffer, so
1242 * we need to propagate the size to a vector to do a
1243 * vector comparison */
1244 num_consts
= lp_build_broadcast_scalar(uint_bld
, num_consts
);
1245 /* Construct a boolean vector telling us which channels
1246 * overflow the bound constant buffer */
1247 overflow_mask
= lp_build_compare(gallivm
, uint_bld
->type
, PIPE_FUNC_GEQUAL
,
1248 indirect_index
, num_consts
);
1250 /* index_vec = indirect_index * 4 + swizzle */
1251 index_vec
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1252 index_vec
= lp_build_add(uint_bld
, index_vec
, swizzle_vec
);
1254 if (tgsi_type_is_64bit(stype
)) {
1255 LLVMValueRef swizzle_vec2
;
1256 swizzle_vec2
= lp_build_const_int_vec(gallivm
, uint_bld
->type
, swizzle
+ 1);
1257 index_vec2
= lp_build_shl_imm(uint_bld
, indirect_index
, 2);
1258 index_vec2
= lp_build_add(uint_bld
, index_vec2
, swizzle_vec2
);
1260 /* Gather values from the constant buffer */
1261 res
= build_gather(bld_base
, consts_ptr
, index_vec
, overflow_mask
, index_vec2
);
1264 LLVMValueRef index
; /* index into the const buffer */
1265 LLVMValueRef scalar
, scalar_ptr
;
1266 struct lp_build_context
*bld_broad
= &bld_base
->base
;
1267 index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
* 4 + swizzle
);
1269 scalar_ptr
= LLVMBuildGEP(builder
, consts_ptr
,
1271 if (stype
== TGSI_TYPE_DOUBLE
) {
1272 LLVMTypeRef dptr_type
= LLVMPointerType(LLVMDoubleTypeInContext(gallivm
->context
), 0);
1273 scalar_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, dptr_type
, "");
1274 bld_broad
= &bld_base
->dbl_bld
;
1275 } else if (stype
== TGSI_TYPE_UNSIGNED64
) {
1276 LLVMTypeRef u64ptr_type
= LLVMPointerType(LLVMInt64TypeInContext(gallivm
->context
), 0);
1277 scalar_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, u64ptr_type
, "");
1278 bld_broad
= &bld_base
->uint64_bld
;
1279 } else if (stype
== TGSI_TYPE_SIGNED64
) {
1280 LLVMTypeRef i64ptr_type
= LLVMPointerType(LLVMInt64TypeInContext(gallivm
->context
), 0);
1281 scalar_ptr
= LLVMBuildBitCast(builder
, scalar_ptr
, i64ptr_type
, "");
1282 bld_broad
= &bld_base
->int64_bld
;
1284 scalar
= LLVMBuildLoad(builder
, scalar_ptr
, "");
1285 res
= lp_build_broadcast_scalar(bld_broad
, scalar
);
1288 if (stype
== TGSI_TYPE_SIGNED
|| stype
== TGSI_TYPE_UNSIGNED
|| stype
== TGSI_TYPE_DOUBLE
|| stype
== TGSI_TYPE_SIGNED64
|| stype
== TGSI_TYPE_UNSIGNED64
) {
1289 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1290 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1297 * Fetch 64-bit values from two separate channels.
1298 * 64-bit values are stored split across two channels, like xy and zw.
1299 * This function creates a set of 16 floats,
1300 * extracts the values from the two channels,
1301 * puts them in the correct place, then casts to 8 64-bits.
1305 struct lp_build_tgsi_context
* bld_base
,
1306 enum tgsi_opcode_type stype
,
1308 LLVMValueRef input2
)
1310 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1311 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1312 LLVMBuilderRef builder
= gallivm
->builder
;
1314 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1316 LLVMValueRef shuffles
[16];
1317 int len
= bld_base
->base
.type
.length
* 2;
1320 for (i
= 0; i
< bld_base
->base
.type
.length
* 2; i
+=2) {
1321 shuffles
[i
] = lp_build_const_int32(gallivm
, i
/ 2);
1322 shuffles
[i
+ 1] = lp_build_const_int32(gallivm
, i
/ 2 + bld_base
->base
.type
.length
);
1324 res
= LLVMBuildShuffleVector(builder
, input
, input2
, LLVMConstVector(shuffles
, len
), "");
1326 return LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1330 emit_fetch_immediate(
1331 struct lp_build_tgsi_context
* bld_base
,
1332 const struct tgsi_full_src_register
* reg
,
1333 enum tgsi_opcode_type stype
,
1336 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1337 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1338 LLVMBuilderRef builder
= gallivm
->builder
;
1339 LLVMValueRef res
= NULL
;
1341 if (bld
->use_immediates_array
|| reg
->Register
.Indirect
) {
1342 LLVMValueRef imms_array
;
1343 LLVMTypeRef fptr_type
;
1345 /* cast imms_array pointer to float* */
1346 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1347 imms_array
= LLVMBuildBitCast(builder
, bld
->imms_array
, fptr_type
, "");
1349 if (reg
->Register
.Indirect
) {
1350 LLVMValueRef indirect_index
;
1351 LLVMValueRef index_vec
; /* index into the immediate register array */
1352 LLVMValueRef index_vec2
= NULL
;
1353 indirect_index
= get_indirect_index(bld
,
1355 reg
->Register
.Index
,
1358 * Unlike for other reg classes, adding pixel offsets is unnecessary -
1359 * immediates are stored as full vectors (FIXME??? - might be better
1360 * to store them the same as constants) but all elements are the same
1363 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1367 if (tgsi_type_is_64bit(stype
))
1368 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
1372 /* Gather values from the immediate register array */
1373 res
= build_gather(bld_base
, imms_array
, index_vec
, NULL
, index_vec2
);
1375 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
1376 reg
->Register
.Index
* 4 + swizzle
);
1377 LLVMValueRef imms_ptr
= LLVMBuildGEP(builder
,
1378 bld
->imms_array
, &lindex
, 1, "");
1379 res
= LLVMBuildLoad(builder
, imms_ptr
, "");
1381 if (tgsi_type_is_64bit(stype
)) {
1382 LLVMValueRef lindex1
;
1383 LLVMValueRef imms_ptr2
;
1386 lindex1
= lp_build_const_int32(gallivm
,
1387 reg
->Register
.Index
* 4 + swizzle
+ 1);
1388 imms_ptr2
= LLVMBuildGEP(builder
,
1389 bld
->imms_array
, &lindex1
, 1, "");
1390 res2
= LLVMBuildLoad(builder
, imms_ptr2
, "");
1391 res
= emit_fetch_64bit(bld_base
, stype
, res
, res2
);
1396 res
= bld
->immediates
[reg
->Register
.Index
][swizzle
];
1397 if (tgsi_type_is_64bit(stype
))
1398 res
= emit_fetch_64bit(bld_base
, stype
, res
, bld
->immediates
[reg
->Register
.Index
][swizzle
+ 1]);
1401 if (stype
== TGSI_TYPE_SIGNED
|| stype
== TGSI_TYPE_UNSIGNED
|| tgsi_type_is_64bit(stype
)) {
1402 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1403 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1410 struct lp_build_tgsi_context
* bld_base
,
1411 const struct tgsi_full_src_register
* reg
,
1412 enum tgsi_opcode_type stype
,
1415 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1416 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1417 LLVMBuilderRef builder
= gallivm
->builder
;
1420 if (reg
->Register
.Indirect
) {
1421 LLVMValueRef indirect_index
;
1422 LLVMValueRef index_vec
; /* index into the input reg array */
1423 LLVMValueRef index_vec2
= NULL
;
1424 LLVMValueRef inputs_array
;
1425 LLVMTypeRef fptr_type
;
1427 indirect_index
= get_indirect_index(bld
,
1429 reg
->Register
.Index
,
1432 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1436 if (tgsi_type_is_64bit(stype
)) {
1437 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
1442 /* cast inputs_array pointer to float* */
1443 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1444 inputs_array
= LLVMBuildBitCast(builder
, bld
->inputs_array
, fptr_type
, "");
1446 /* Gather values from the input register array */
1447 res
= build_gather(bld_base
, inputs_array
, index_vec
, NULL
, index_vec2
);
1449 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
)) {
1450 LLVMValueRef lindex
= lp_build_const_int32(gallivm
,
1451 reg
->Register
.Index
* 4 + swizzle
);
1452 LLVMValueRef input_ptr
= LLVMBuildGEP(builder
,
1453 bld
->inputs_array
, &lindex
, 1, "");
1455 res
= LLVMBuildLoad(builder
, input_ptr
, "");
1456 if (tgsi_type_is_64bit(stype
)) {
1457 LLVMValueRef lindex1
;
1458 LLVMValueRef input_ptr2
;
1461 lindex1
= lp_build_const_int32(gallivm
,
1462 reg
->Register
.Index
* 4 + swizzle
+ 1);
1463 input_ptr2
= LLVMBuildGEP(builder
,
1464 bld
->inputs_array
, &lindex1
, 1, "");
1465 res2
= LLVMBuildLoad(builder
, input_ptr2
, "");
1466 res
= emit_fetch_64bit(bld_base
, stype
, res
, res2
);
1470 res
= bld
->inputs
[reg
->Register
.Index
][swizzle
];
1471 if (tgsi_type_is_64bit(stype
))
1472 res
= emit_fetch_64bit(bld_base
, stype
, res
, bld
->inputs
[reg
->Register
.Index
][swizzle
+ 1]);
1478 if (stype
== TGSI_TYPE_SIGNED
|| stype
== TGSI_TYPE_UNSIGNED
|| tgsi_type_is_64bit(stype
)) {
1479 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1480 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1488 emit_fetch_gs_input(
1489 struct lp_build_tgsi_context
* bld_base
,
1490 const struct tgsi_full_src_register
* reg
,
1491 enum tgsi_opcode_type stype
,
1494 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1495 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1496 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
1497 LLVMBuilderRef builder
= gallivm
->builder
;
1498 LLVMValueRef attrib_index
= NULL
;
1499 LLVMValueRef vertex_index
= NULL
;
1500 LLVMValueRef swizzle_index
= lp_build_const_int32(gallivm
, swizzle
);
1503 if (info
->input_semantic_name
[reg
->Register
.Index
] == TGSI_SEMANTIC_PRIMID
) {
1504 /* This is really a system value not a regular input */
1505 assert(!reg
->Register
.Indirect
);
1506 assert(!reg
->Dimension
.Indirect
);
1507 res
= bld
->system_values
.prim_id
;
1508 if (stype
!= TGSI_TYPE_UNSIGNED
&& stype
!= TGSI_TYPE_SIGNED
) {
1509 res
= LLVMBuildBitCast(builder
, res
, bld_base
->base
.vec_type
, "");
1514 if (reg
->Register
.Indirect
) {
1515 attrib_index
= get_indirect_index(bld
,
1517 reg
->Register
.Index
,
1520 attrib_index
= lp_build_const_int32(gallivm
, reg
->Register
.Index
);
1523 if (reg
->Dimension
.Indirect
) {
1524 vertex_index
= get_indirect_index(bld
,
1526 reg
->Dimension
.Index
,
1529 vertex_index
= lp_build_const_int32(gallivm
, reg
->Dimension
.Index
);
1532 res
= bld
->gs_iface
->fetch_input(bld
->gs_iface
, bld_base
,
1533 reg
->Dimension
.Indirect
,
1535 reg
->Register
.Indirect
,
1540 if (tgsi_type_is_64bit(stype
)) {
1541 LLVMValueRef swizzle_index
= lp_build_const_int32(gallivm
, swizzle
+ 1);
1543 res2
= bld
->gs_iface
->fetch_input(bld
->gs_iface
, bld_base
,
1544 reg
->Dimension
.Indirect
,
1546 reg
->Register
.Indirect
,
1550 res
= emit_fetch_64bit(bld_base
, stype
, res
, res2
);
1551 } else if (stype
== TGSI_TYPE_UNSIGNED
) {
1552 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
1553 } else if (stype
== TGSI_TYPE_SIGNED
) {
1554 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
1561 emit_fetch_temporary(
1562 struct lp_build_tgsi_context
* bld_base
,
1563 const struct tgsi_full_src_register
* reg
,
1564 enum tgsi_opcode_type stype
,
1567 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1568 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1569 LLVMBuilderRef builder
= gallivm
->builder
;
1572 if (reg
->Register
.Indirect
) {
1573 LLVMValueRef indirect_index
;
1574 LLVMValueRef index_vec
, index_vec2
= NULL
; /* index into the temp reg array */
1575 LLVMValueRef temps_array
;
1576 LLVMTypeRef fptr_type
;
1578 indirect_index
= get_indirect_index(bld
,
1580 reg
->Register
.Index
,
1583 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1587 if (tgsi_type_is_64bit(stype
)) {
1588 index_vec2
= get_soa_array_offsets(&bld_base
->uint_bld
,
1594 /* cast temps_array pointer to float* */
1595 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1596 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
, fptr_type
, "");
1598 /* Gather values from the temporary register array */
1599 res
= build_gather(bld_base
, temps_array
, index_vec
, NULL
, index_vec2
);
1602 LLVMValueRef temp_ptr
;
1603 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
);
1604 res
= LLVMBuildLoad(builder
, temp_ptr
, "");
1606 if (tgsi_type_is_64bit(stype
)) {
1607 LLVMValueRef temp_ptr2
, res2
;
1609 temp_ptr2
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, swizzle
+ 1);
1610 res2
= LLVMBuildLoad(builder
, temp_ptr2
, "");
1611 res
= emit_fetch_64bit(bld_base
, stype
, res
, res2
);
1615 if (stype
== TGSI_TYPE_SIGNED
||
1616 stype
== TGSI_TYPE_UNSIGNED
||
1617 stype
== TGSI_TYPE_DOUBLE
||
1618 stype
== TGSI_TYPE_SIGNED64
||
1619 stype
== TGSI_TYPE_UNSIGNED64
) {
1620 struct lp_build_context
*bld_fetch
= stype_to_fetch(bld_base
, stype
);
1621 res
= LLVMBuildBitCast(builder
, res
, bld_fetch
->vec_type
, "");
1628 emit_fetch_system_value(
1629 struct lp_build_tgsi_context
* bld_base
,
1630 const struct tgsi_full_src_register
* reg
,
1631 enum tgsi_opcode_type stype
,
1634 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1635 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
1636 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
1637 LLVMBuilderRef builder
= gallivm
->builder
;
1639 enum tgsi_opcode_type atype
; // Actual type of the value
1641 assert(!reg
->Register
.Indirect
);
1643 switch (info
->system_value_semantic_name
[reg
->Register
.Index
]) {
1644 case TGSI_SEMANTIC_INSTANCEID
:
1645 res
= lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.instance_id
);
1646 atype
= TGSI_TYPE_UNSIGNED
;
1649 case TGSI_SEMANTIC_VERTEXID
:
1650 res
= bld
->system_values
.vertex_id
;
1651 atype
= TGSI_TYPE_UNSIGNED
;
1654 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1655 res
= bld
->system_values
.vertex_id_nobase
;
1656 atype
= TGSI_TYPE_UNSIGNED
;
1659 case TGSI_SEMANTIC_BASEVERTEX
:
1660 res
= bld
->system_values
.basevertex
;
1661 atype
= TGSI_TYPE_UNSIGNED
;
1664 case TGSI_SEMANTIC_PRIMID
:
1665 res
= bld
->system_values
.prim_id
;
1666 atype
= TGSI_TYPE_UNSIGNED
;
1669 case TGSI_SEMANTIC_INVOCATIONID
:
1670 res
= lp_build_broadcast_scalar(&bld_base
->uint_bld
, bld
->system_values
.invocation_id
);
1671 atype
= TGSI_TYPE_UNSIGNED
;
1675 assert(!"unexpected semantic in emit_fetch_system_value");
1676 res
= bld_base
->base
.zero
;
1677 atype
= TGSI_TYPE_FLOAT
;
1681 if (atype
!= stype
) {
1682 if (stype
== TGSI_TYPE_FLOAT
) {
1683 res
= LLVMBuildBitCast(builder
, res
, bld_base
->base
.vec_type
, "");
1684 } else if (stype
== TGSI_TYPE_UNSIGNED
) {
1685 res
= LLVMBuildBitCast(builder
, res
, bld_base
->uint_bld
.vec_type
, "");
1686 } else if (stype
== TGSI_TYPE_SIGNED
) {
1687 res
= LLVMBuildBitCast(builder
, res
, bld_base
->int_bld
.vec_type
, "");
1695 * Register fetch with derivatives.
1699 struct lp_build_tgsi_soa_context
*bld
,
1708 /* TODO: use interpolation coeffs for inputs */
1711 *ddx
= lp_build_ddx(&bld
->bld_base
.base
, src
);
1714 *ddy
= lp_build_ddy(&bld
->bld_base
.base
, src
);
1718 * store an array of 8 64-bit into two arrays of 8 floats
1720 * value is d0, d1, d2, d3 etc.
1721 * each 64-bit has high and low pieces x, y
1722 * so gets stored into the separate channels as:
1723 * chan_ptr = d0.x, d1.x, d2.x, d3.x
1724 * chan_ptr2 = d0.y, d1.y, d2.y, d3.y
1727 emit_store_64bit_chan(struct lp_build_tgsi_context
*bld_base
,
1728 LLVMValueRef chan_ptr
, LLVMValueRef chan_ptr2
,
1731 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1732 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1733 LLVMBuilderRef builder
= gallivm
->builder
;
1734 struct lp_build_context
*float_bld
= &bld_base
->base
;
1736 LLVMValueRef temp
, temp2
;
1737 LLVMValueRef shuffles
[8];
1738 LLVMValueRef shuffles2
[8];
1740 for (i
= 0; i
< bld_base
->base
.type
.length
; i
++) {
1741 shuffles
[i
] = lp_build_const_int32(gallivm
, i
* 2);
1742 shuffles2
[i
] = lp_build_const_int32(gallivm
, (i
* 2) + 1);
1745 temp
= LLVMBuildShuffleVector(builder
, value
,
1746 LLVMGetUndef(LLVMTypeOf(value
)),
1747 LLVMConstVector(shuffles
,
1748 bld_base
->base
.type
.length
),
1750 temp2
= LLVMBuildShuffleVector(builder
, value
,
1751 LLVMGetUndef(LLVMTypeOf(value
)),
1752 LLVMConstVector(shuffles2
,
1753 bld_base
->base
.type
.length
),
1756 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, temp
, chan_ptr
);
1757 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, temp2
, chan_ptr2
);
1765 struct lp_build_tgsi_context
*bld_base
,
1766 const struct tgsi_full_instruction
*inst
,
1768 unsigned chan_index
,
1771 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1772 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1773 LLVMBuilderRef builder
= gallivm
->builder
;
1774 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
1775 struct lp_build_context
*float_bld
= &bld_base
->base
;
1776 struct lp_build_context
*int_bld
= &bld_base
->int_bld
;
1777 LLVMValueRef indirect_index
= NULL
;
1778 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
1783 * It is always assumed to be float.
1785 if (inst
->Instruction
.Saturate
) {
1786 assert(dtype
== TGSI_TYPE_FLOAT
||
1787 dtype
== TGSI_TYPE_UNTYPED
);
1788 value
= LLVMBuildBitCast(builder
, value
, float_bld
->vec_type
, "");
1789 value
= lp_build_clamp_zero_one_nanzero(float_bld
, value
);
1792 if (reg
->Register
.Indirect
) {
1794 * Currently the mesa/st doesn't generate indirect stores
1795 * to 64-bit values, it normally uses MOV to do indirect stores.
1797 assert(!tgsi_type_is_64bit(dtype
));
1798 indirect_index
= get_indirect_index(bld
,
1800 reg
->Register
.Index
,
1803 assert(reg
->Register
.Index
<=
1804 bld_base
->info
->file_max
[reg
->Register
.File
]);
1807 if (DEBUG_EXECUTION
) {
1808 emit_dump_reg(gallivm
, reg
->Register
.File
, reg
->Register
.Index
, chan_index
, value
);
1811 switch( reg
->Register
.File
) {
1812 case TGSI_FILE_OUTPUT
:
1813 /* Outputs are always stored as floats */
1814 value
= LLVMBuildBitCast(builder
, value
, float_bld
->vec_type
, "");
1816 if (reg
->Register
.Indirect
) {
1817 LLVMValueRef index_vec
; /* indexes into the output registers */
1818 LLVMValueRef outputs_array
;
1819 LLVMTypeRef fptr_type
;
1821 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1826 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1827 outputs_array
= LLVMBuildBitCast(builder
, bld
->outputs_array
, fptr_type
, "");
1829 /* Scatter store values into output registers */
1830 emit_mask_scatter(bld
, outputs_array
, index_vec
, value
,
1834 LLVMValueRef out_ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
1837 if (tgsi_type_is_64bit(dtype
)) {
1838 LLVMValueRef out_ptr2
= lp_get_output_ptr(bld
, reg
->Register
.Index
,
1840 emit_store_64bit_chan(bld_base
, out_ptr
, out_ptr2
,
1843 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, value
, out_ptr
);
1847 case TGSI_FILE_TEMPORARY
:
1848 /* Temporaries are always stored as floats */
1849 if (!tgsi_type_is_64bit(dtype
))
1850 value
= LLVMBuildBitCast(builder
, value
, float_bld
->vec_type
, "");
1852 value
= LLVMBuildBitCast(builder
, value
, LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), bld_base
->base
.type
.length
* 2), "");
1854 if (reg
->Register
.Indirect
) {
1855 LLVMValueRef index_vec
; /* indexes into the temp registers */
1856 LLVMValueRef temps_array
;
1857 LLVMTypeRef fptr_type
;
1859 index_vec
= get_soa_array_offsets(&bld_base
->uint_bld
,
1864 fptr_type
= LLVMPointerType(LLVMFloatTypeInContext(gallivm
->context
), 0);
1865 temps_array
= LLVMBuildBitCast(builder
, bld
->temps_array
, fptr_type
, "");
1867 /* Scatter store values into temp registers */
1868 emit_mask_scatter(bld
, temps_array
, index_vec
, value
,
1872 LLVMValueRef temp_ptr
;
1873 temp_ptr
= lp_get_temp_ptr_soa(bld
, reg
->Register
.Index
, chan_index
);
1875 if (tgsi_type_is_64bit(dtype
)) {
1876 LLVMValueRef temp_ptr2
= lp_get_temp_ptr_soa(bld
,
1877 reg
->Register
.Index
,
1879 emit_store_64bit_chan(bld_base
, temp_ptr
, temp_ptr2
,
1883 lp_exec_mask_store(&bld
->exec_mask
, float_bld
, value
, temp_ptr
);
1887 case TGSI_FILE_ADDRESS
:
1888 assert(dtype
== TGSI_TYPE_SIGNED
);
1889 assert(LLVMTypeOf(value
) == int_bld
->vec_type
);
1890 value
= LLVMBuildBitCast(builder
, value
, int_bld
->vec_type
, "");
1891 lp_exec_mask_store(&bld
->exec_mask
, int_bld
, value
,
1892 bld
->addr
[reg
->Register
.Index
][chan_index
]);
1903 * Called at the beginning of the translation of each TGSI instruction, to
1904 * emit some debug code.
1908 struct lp_build_tgsi_context
* bld_base
,
1909 const struct tgsi_full_instruction
* inst
,
1910 const struct tgsi_opcode_info
* info
)
1913 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
1915 if (DEBUG_EXECUTION
) {
1917 * Dump the TGSI instruction.
1920 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1924 tgsi_dump_instruction_str(inst
, bld_base
->pc
, &buf
[2], sizeof buf
- 2);
1925 lp_build_printf(gallivm
, buf
);
1927 /* Dump the execution mask.
1929 if (bld
->exec_mask
.has_mask
) {
1930 lp_build_print_value(gallivm
, " mask = ", bld
->exec_mask
.exec_mask
);
1937 struct lp_build_tgsi_context
* bld_base
,
1938 const struct tgsi_full_instruction
* inst
,
1939 const struct tgsi_opcode_info
* info
,
1940 LLVMValueRef dst
[4])
1943 unsigned chan_index
;
1944 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
1947 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
1949 if (tgsi_type_is_64bit(dtype
) && (chan_index
== 1 || chan_index
== 3))
1951 emit_store_chan(bld_base
, inst
, 0, chan_index
, dst
[chan_index
]);
1957 tgsi_to_pipe_tex_target(unsigned tgsi_target
)
1959 switch (tgsi_target
) {
1960 case TGSI_TEXTURE_BUFFER
:
1962 case TGSI_TEXTURE_1D
:
1963 case TGSI_TEXTURE_SHADOW1D
:
1964 return PIPE_TEXTURE_1D
;
1965 case TGSI_TEXTURE_2D
:
1966 case TGSI_TEXTURE_SHADOW2D
:
1967 case TGSI_TEXTURE_2D_MSAA
:
1968 return PIPE_TEXTURE_2D
;
1969 case TGSI_TEXTURE_3D
:
1970 return PIPE_TEXTURE_3D
;
1971 case TGSI_TEXTURE_CUBE
:
1972 case TGSI_TEXTURE_SHADOWCUBE
:
1973 return PIPE_TEXTURE_CUBE
;
1974 case TGSI_TEXTURE_RECT
:
1975 case TGSI_TEXTURE_SHADOWRECT
:
1976 return PIPE_TEXTURE_RECT
;
1977 case TGSI_TEXTURE_1D_ARRAY
:
1978 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1979 return PIPE_TEXTURE_1D_ARRAY
;
1980 case TGSI_TEXTURE_2D_ARRAY
:
1981 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1982 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
1983 return PIPE_TEXTURE_2D_ARRAY
;
1984 case TGSI_TEXTURE_CUBE_ARRAY
:
1985 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1986 return PIPE_TEXTURE_CUBE_ARRAY
;
1994 static enum lp_sampler_lod_property
1995 lp_build_lod_property(
1996 struct lp_build_tgsi_context
*bld_base
,
1997 const struct tgsi_full_instruction
*inst
,
2000 const struct tgsi_full_src_register
*reg
= &inst
->Src
[src_op
];
2001 enum lp_sampler_lod_property lod_property
;
2004 * Not much we can do here. We could try catching inputs declared
2005 * with constant interpolation but not sure it's worth it - since for
2006 * TEX opcodes as well as FETCH/LD the lod comes from same reg as
2007 * the coords, so it could only work for SAMPLE/TXQ/SVIEWINFO), just
2008 * like the constant/immediate recognition below.
2009 * What seems to be of more value would be to recognize temps holding
2010 * broadcasted scalars but no way we can do it.
2011 * Tried asking llvm but without any success (using LLVMIsConstant
2012 * even though this isn't exactly what we'd need), even as simple as
2013 * IMM[0] UINT32 (0,-1,0,0)
2014 * MOV TEMP[0] IMM[0].yyyy
2015 * SVIEWINFO TEMP[1], TEMP[0].xxxx, SVIEWINFO[0]
2017 * This means there's ZERO chance this will ever catch a scalar lod
2018 * with traditional tex opcodes as well as texel fetches, since the lod
2019 * comes from the same reg as coords (except some test shaders using
2020 * constant coords maybe).
2021 * There's at least hope for sample opcodes as well as size queries.
2023 if (reg
->Register
.File
== TGSI_FILE_CONSTANT
||
2024 reg
->Register
.File
== TGSI_FILE_IMMEDIATE
) {
2025 lod_property
= LP_SAMPLER_LOD_SCALAR
;
2027 else if (bld_base
->info
->processor
== PIPE_SHADER_FRAGMENT
) {
2028 if (gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) {
2029 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2032 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
2036 /* never use scalar (per-quad) lod the results are just too wrong. */
2037 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2039 return lod_property
;
2044 * High-level instruction translators.
2048 emit_tex( struct lp_build_tgsi_soa_context
*bld
,
2049 const struct tgsi_full_instruction
*inst
,
2050 enum lp_build_tex_modifier modifier
,
2051 LLVMValueRef
*texel
,
2052 unsigned sampler_reg
,
2053 enum lp_sampler_op_type sampler_op
)
2055 unsigned unit
= inst
->Src
[sampler_reg
].Register
.Index
;
2056 LLVMValueRef oow
= NULL
;
2057 LLVMValueRef lod
= NULL
;
2058 LLVMValueRef coords
[5];
2059 LLVMValueRef offsets
[3] = { NULL
};
2060 struct lp_derivatives derivs
;
2061 struct lp_sampler_params params
;
2062 enum lp_sampler_lod_property lod_property
= LP_SAMPLER_LOD_SCALAR
;
2063 unsigned num_derivs
, num_offsets
, i
;
2064 unsigned shadow_coord
= 0;
2065 unsigned layer_coord
= 0;
2066 unsigned sample_key
= sampler_op
<< LP_SAMPLER_OP_TYPE_SHIFT
;
2068 memset(¶ms
, 0, sizeof(params
));
2070 if (!bld
->sampler
) {
2071 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2072 for (i
= 0; i
< 4; i
++) {
2073 texel
[i
] = bld
->bld_base
.base
.undef
;
2078 switch (inst
->Texture
.Texture
) {
2079 case TGSI_TEXTURE_1D_ARRAY
:
2082 case TGSI_TEXTURE_1D
:
2086 case TGSI_TEXTURE_2D_ARRAY
:
2089 case TGSI_TEXTURE_2D
:
2090 case TGSI_TEXTURE_RECT
:
2094 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2097 case TGSI_TEXTURE_SHADOW1D
:
2102 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2108 case TGSI_TEXTURE_SHADOW2D
:
2109 case TGSI_TEXTURE_SHADOWRECT
:
2114 case TGSI_TEXTURE_CUBE
:
2118 case TGSI_TEXTURE_3D
:
2122 case TGSI_TEXTURE_SHADOWCUBE
:
2127 case TGSI_TEXTURE_CUBE_ARRAY
:
2132 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
2136 shadow_coord
= 4; /* shadow coord special different reg */
2138 case TGSI_TEXTURE_2D_MSAA
:
2139 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
2145 /* Note lod and especially projected are illegal in a LOT of cases */
2146 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
||
2147 modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2148 if (inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWCUBE
||
2149 inst
->Texture
.Texture
== TGSI_TEXTURE_CUBE_ARRAY
) {
2150 /* note that shadow cube array with bias/explicit lod does not exist */
2151 lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, 0);
2154 lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 3);
2156 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
2157 sample_key
|= LP_SAMPLER_LOD_BIAS
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2159 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2160 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2162 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2165 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
) {
2166 oow
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 3);
2167 oow
= lp_build_rcp(&bld
->bld_base
.base
, oow
);
2170 for (i
= 0; i
< num_derivs
; i
++) {
2171 coords
[i
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, i
);
2172 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
2173 coords
[i
] = lp_build_mul(&bld
->bld_base
.base
, coords
[i
], oow
);
2175 for (i
= num_derivs
; i
< 5; i
++) {
2176 coords
[i
] = bld
->bld_base
.base
.undef
;
2179 /* Layer coord always goes into 3rd slot, except for cube map arrays */
2181 if (layer_coord
== 3) {
2182 coords
[3] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2185 coords
[2] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2187 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
2188 coords
[2] = lp_build_mul(&bld
->bld_base
.base
, coords
[2], oow
);
2190 /* Shadow coord occupies always 5th slot. */
2192 sample_key
|= LP_SAMPLER_SHADOW
;
2193 if (shadow_coord
== 4) {
2194 coords
[4] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, 0);
2197 coords
[4] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, shadow_coord
);
2199 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
)
2200 coords
[4] = lp_build_mul(&bld
->bld_base
.base
, coords
[4], oow
);
2203 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
2205 sample_key
|= LP_SAMPLER_LOD_DERIVATIVES
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2206 for (dim
= 0; dim
< num_derivs
; ++dim
) {
2207 derivs
.ddx
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 1, dim
);
2208 derivs
.ddy
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 2, dim
);
2210 params
.derivs
= &derivs
;
2212 * could also check all src regs if constant but I doubt such
2213 * cases exist in practice.
2215 if (bld
->bld_base
.info
->processor
== PIPE_SHADER_FRAGMENT
) {
2216 if (gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) {
2217 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2220 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
2224 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2227 sample_key
|= lod_property
<< LP_SAMPLER_LOD_PROPERTY_SHIFT
;
2229 /* we don't handle the 4 offset version of tg4 */
2230 if (inst
->Texture
.NumOffsets
== 1) {
2232 sample_key
|= LP_SAMPLER_OFFSETS
;
2233 for (dim
= 0; dim
< num_offsets
; dim
++) {
2234 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
2238 params
.type
= bld
->bld_base
.base
.type
;
2239 params
.sample_key
= sample_key
;
2240 params
.texture_index
= unit
;
2241 params
.sampler_index
= unit
;
2242 params
.context_ptr
= bld
->context_ptr
;
2243 params
.thread_data_ptr
= bld
->thread_data_ptr
;
2244 params
.coords
= coords
;
2245 params
.offsets
= offsets
;
2247 params
.texel
= texel
;
2249 bld
->sampler
->emit_tex_sample(bld
->sampler
,
2250 bld
->bld_base
.base
.gallivm
,
2255 emit_sample(struct lp_build_tgsi_soa_context
*bld
,
2256 const struct tgsi_full_instruction
*inst
,
2257 enum lp_build_tex_modifier modifier
,
2259 LLVMValueRef
*texel
)
2261 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
2262 unsigned texture_unit
, sampler_unit
;
2263 LLVMValueRef lod
= NULL
;
2264 LLVMValueRef coords
[5];
2265 LLVMValueRef offsets
[3] = { NULL
};
2266 struct lp_derivatives derivs
;
2267 struct lp_sampler_params params
;
2268 enum lp_sampler_lod_property lod_property
= LP_SAMPLER_LOD_SCALAR
;
2270 unsigned num_offsets
, num_derivs
, i
;
2271 unsigned layer_coord
= 0;
2272 unsigned sample_key
= LP_SAMPLER_OP_TEXTURE
<< LP_SAMPLER_OP_TYPE_SHIFT
;
2274 memset(¶ms
, 0, sizeof(params
));
2276 if (!bld
->sampler
) {
2277 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2278 for (i
= 0; i
< 4; i
++) {
2279 texel
[i
] = bld
->bld_base
.base
.undef
;
2285 * unlike old-style tex opcodes the texture/sampler indices
2286 * always come from src1 and src2 respectively.
2288 texture_unit
= inst
->Src
[1].Register
.Index
;
2289 sampler_unit
= inst
->Src
[2].Register
.Index
;
2292 * Note inst->Texture.Texture will contain the number of offsets,
2293 * however the target information is NOT there and comes from the
2294 * declared sampler views instead.
2296 switch (bld
->sv
[texture_unit
].Resource
) {
2297 case TGSI_TEXTURE_1D
:
2301 case TGSI_TEXTURE_1D_ARRAY
:
2306 case TGSI_TEXTURE_2D
:
2307 case TGSI_TEXTURE_RECT
:
2311 case TGSI_TEXTURE_2D_ARRAY
:
2316 case TGSI_TEXTURE_CUBE
:
2320 case TGSI_TEXTURE_3D
:
2324 case TGSI_TEXTURE_CUBE_ARRAY
:
2334 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
||
2335 modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2336 lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 3, 0);
2337 if (modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
) {
2338 sample_key
|= LP_SAMPLER_LOD_BIAS
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2340 else if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
2341 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2343 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2345 else if (modifier
== LP_BLD_TEX_MODIFIER_LOD_ZERO
) {
2346 /* XXX might be better to explicitly pass the level zero information */
2347 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2348 lod
= lp_build_const_vec(gallivm
, bld
->bld_base
.base
.type
, 0.0F
);
2351 for (i
= 0; i
< num_derivs
; i
++) {
2352 coords
[i
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, i
);
2354 for (i
= num_derivs
; i
< 5; i
++) {
2355 coords
[i
] = bld
->bld_base
.base
.undef
;
2358 /* Layer coord always goes into 3rd slot, except for cube map arrays */
2360 if (layer_coord
== 3)
2361 coords
[3] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2363 coords
[2] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2365 /* Shadow coord occupies always 5th slot. */
2367 sample_key
|= LP_SAMPLER_SHADOW
;
2368 coords
[4] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 3, 0);
2371 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
2373 sample_key
|= LP_SAMPLER_LOD_DERIVATIVES
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2374 for (dim
= 0; dim
< num_derivs
; ++dim
) {
2375 derivs
.ddx
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 3, dim
);
2376 derivs
.ddy
[dim
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 4, dim
);
2378 params
.derivs
= &derivs
;
2380 * could also check all src regs if constant but I doubt such
2381 * cases exist in practice.
2383 if (bld
->bld_base
.info
->processor
== PIPE_SHADER_FRAGMENT
) {
2384 if (gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) {
2385 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2388 lod_property
= LP_SAMPLER_LOD_PER_QUAD
;
2392 lod_property
= LP_SAMPLER_LOD_PER_ELEMENT
;
2396 /* some advanced gather instructions (txgo) would require 4 offsets */
2397 if (inst
->Texture
.NumOffsets
== 1) {
2399 sample_key
|= LP_SAMPLER_OFFSETS
;
2400 for (dim
= 0; dim
< num_offsets
; dim
++) {
2401 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
2404 sample_key
|= lod_property
<< LP_SAMPLER_LOD_PROPERTY_SHIFT
;
2406 params
.type
= bld
->bld_base
.base
.type
;
2407 params
.sample_key
= sample_key
;
2408 params
.texture_index
= texture_unit
;
2409 params
.sampler_index
= sampler_unit
;
2410 params
.context_ptr
= bld
->context_ptr
;
2411 params
.thread_data_ptr
= bld
->thread_data_ptr
;
2412 params
.coords
= coords
;
2413 params
.offsets
= offsets
;
2415 params
.texel
= texel
;
2417 bld
->sampler
->emit_tex_sample(bld
->sampler
,
2418 bld
->bld_base
.base
.gallivm
,
2421 if (inst
->Src
[1].Register
.SwizzleX
!= PIPE_SWIZZLE_X
||
2422 inst
->Src
[1].Register
.SwizzleY
!= PIPE_SWIZZLE_Y
||
2423 inst
->Src
[1].Register
.SwizzleZ
!= PIPE_SWIZZLE_Z
||
2424 inst
->Src
[1].Register
.SwizzleW
!= PIPE_SWIZZLE_W
) {
2425 unsigned char swizzles
[4];
2426 swizzles
[0] = inst
->Src
[1].Register
.SwizzleX
;
2427 swizzles
[1] = inst
->Src
[1].Register
.SwizzleY
;
2428 swizzles
[2] = inst
->Src
[1].Register
.SwizzleZ
;
2429 swizzles
[3] = inst
->Src
[1].Register
.SwizzleW
;
2431 lp_build_swizzle_soa_inplace(&bld
->bld_base
.base
, texel
, swizzles
);
2436 emit_fetch_texels( struct lp_build_tgsi_soa_context
*bld
,
2437 const struct tgsi_full_instruction
*inst
,
2438 LLVMValueRef
*texel
,
2441 unsigned unit
, target
;
2442 LLVMValueRef coord_undef
= LLVMGetUndef(bld
->bld_base
.base
.int_vec_type
);
2443 LLVMValueRef explicit_lod
= NULL
;
2444 LLVMValueRef coords
[5];
2445 LLVMValueRef offsets
[3] = { NULL
};
2446 struct lp_sampler_params params
;
2447 enum lp_sampler_lod_property lod_property
= LP_SAMPLER_LOD_SCALAR
;
2449 unsigned layer_coord
= 0;
2450 unsigned sample_key
= LP_SAMPLER_OP_FETCH
<< LP_SAMPLER_OP_TYPE_SHIFT
;
2452 memset(¶ms
, 0, sizeof(params
));
2454 if (!bld
->sampler
) {
2455 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2456 for (i
= 0; i
< 4; i
++) {
2457 texel
[i
] = coord_undef
;
2462 unit
= inst
->Src
[1].Register
.Index
;
2465 target
= bld
->sv
[unit
].Resource
;
2468 target
= inst
->Texture
.Texture
;
2472 case TGSI_TEXTURE_1D
:
2473 case TGSI_TEXTURE_BUFFER
:
2476 case TGSI_TEXTURE_1D_ARRAY
:
2480 case TGSI_TEXTURE_2D
:
2481 case TGSI_TEXTURE_RECT
:
2482 case TGSI_TEXTURE_2D_MSAA
:
2485 case TGSI_TEXTURE_2D_ARRAY
:
2486 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
2490 case TGSI_TEXTURE_3D
:
2498 /* always have lod except for buffers and msaa targets ? */
2499 if (target
!= TGSI_TEXTURE_BUFFER
&&
2500 target
!= TGSI_TEXTURE_2D_MSAA
&&
2501 target
!= TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2502 sample_key
|= LP_SAMPLER_LOD_EXPLICIT
<< LP_SAMPLER_LOD_CONTROL_SHIFT
;
2503 explicit_lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 3);
2504 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2507 * XXX: for real msaa support, the w component (or src2.x for sample_i_ms)
2508 * would be the sample index.
2511 for (i
= 0; i
< dims
; i
++) {
2512 coords
[i
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, i
);
2514 /* never use more than 3 coords here but emit_fetch_texel copies all 5 anyway */
2515 for (i
= dims
; i
< 5; i
++) {
2516 coords
[i
] = coord_undef
;
2519 coords
[2] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, layer_coord
);
2521 if (inst
->Texture
.NumOffsets
== 1) {
2523 sample_key
|= LP_SAMPLER_OFFSETS
;
2524 for (dim
= 0; dim
< dims
; dim
++) {
2525 offsets
[dim
] = lp_build_emit_fetch_texoffset(&bld
->bld_base
, inst
, 0, dim
);
2528 sample_key
|= lod_property
<< LP_SAMPLER_LOD_PROPERTY_SHIFT
;
2530 params
.type
= bld
->bld_base
.base
.type
;
2531 params
.sample_key
= sample_key
;
2532 params
.texture_index
= unit
;
2534 * sampler not actually used, set to 0 so it won't exceed PIPE_MAX_SAMPLERS
2535 * and trigger some assertions with d3d10 where the sampler view number
2538 params
.sampler_index
= 0;
2539 params
.context_ptr
= bld
->context_ptr
;
2540 params
.thread_data_ptr
= bld
->thread_data_ptr
;
2541 params
.coords
= coords
;
2542 params
.offsets
= offsets
;
2543 params
.derivs
= NULL
;
2544 params
.lod
= explicit_lod
;
2545 params
.texel
= texel
;
2547 bld
->sampler
->emit_tex_sample(bld
->sampler
,
2548 bld
->bld_base
.base
.gallivm
,
2552 (inst
->Src
[1].Register
.SwizzleX
!= PIPE_SWIZZLE_X
||
2553 inst
->Src
[1].Register
.SwizzleY
!= PIPE_SWIZZLE_Y
||
2554 inst
->Src
[1].Register
.SwizzleZ
!= PIPE_SWIZZLE_Z
||
2555 inst
->Src
[1].Register
.SwizzleW
!= PIPE_SWIZZLE_W
)) {
2556 unsigned char swizzles
[4];
2557 swizzles
[0] = inst
->Src
[1].Register
.SwizzleX
;
2558 swizzles
[1] = inst
->Src
[1].Register
.SwizzleY
;
2559 swizzles
[2] = inst
->Src
[1].Register
.SwizzleZ
;
2560 swizzles
[3] = inst
->Src
[1].Register
.SwizzleW
;
2562 lp_build_swizzle_soa_inplace(&bld
->bld_base
.base
, texel
, swizzles
);
2567 emit_size_query( struct lp_build_tgsi_soa_context
*bld
,
2568 const struct tgsi_full_instruction
*inst
,
2569 LLVMValueRef
*sizes_out
,
2570 boolean is_sviewinfo
)
2572 LLVMValueRef explicit_lod
;
2573 enum lp_sampler_lod_property lod_property
;
2576 unsigned unit
= inst
->Src
[1].Register
.Index
;
2577 unsigned target
, pipe_target
;
2578 struct lp_sampler_size_query_params params
;
2581 target
= bld
->sv
[unit
].Resource
;
2584 target
= inst
->Texture
.Texture
;
2587 case TGSI_TEXTURE_BUFFER
:
2588 case TGSI_TEXTURE_RECT
:
2589 case TGSI_TEXTURE_SHADOWRECT
:
2597 if (!bld
->sampler
) {
2598 _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
2599 for (i
= 0; i
< 4; i
++)
2600 sizes_out
[i
] = bld
->bld_base
.int_bld
.undef
;
2605 explicit_lod
= lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, 0);
2606 lod_property
= lp_build_lod_property(&bld
->bld_base
, inst
, 0);
2609 explicit_lod
= NULL
;
2610 lod_property
= LP_SAMPLER_LOD_SCALAR
;
2614 pipe_target
= tgsi_to_pipe_tex_target(target
);
2616 params
.int_type
= bld
->bld_base
.int_bld
.type
;
2617 params
.texture_unit
= unit
;
2618 params
.target
= pipe_target
;
2619 params
.context_ptr
= bld
->context_ptr
;
2620 params
.is_sviewinfo
= TRUE
;
2621 params
.lod_property
= lod_property
;
2622 params
.explicit_lod
= explicit_lod
;
2623 params
.sizes_out
= sizes_out
;
2625 bld
->sampler
->emit_size_query(bld
->sampler
,
2626 bld
->bld_base
.base
.gallivm
,
2631 near_end_of_shader(struct lp_build_tgsi_soa_context
*bld
,
2636 for (i
= 0; i
< 5; i
++) {
2639 if (pc
+ i
>= bld
->bld_base
.info
->num_instructions
)
2642 opcode
= bld
->bld_base
.instructions
[pc
+ i
].Instruction
.Opcode
;
2644 if (opcode
== TGSI_OPCODE_END
)
2647 if (opcode
== TGSI_OPCODE_TEX
||
2648 opcode
== TGSI_OPCODE_TXP
||
2649 opcode
== TGSI_OPCODE_TXD
||
2650 opcode
== TGSI_OPCODE_TXB
||
2651 opcode
== TGSI_OPCODE_TXL
||
2652 opcode
== TGSI_OPCODE_TXF
||
2653 opcode
== TGSI_OPCODE_TXQ
||
2654 opcode
== TGSI_OPCODE_TEX2
||
2655 opcode
== TGSI_OPCODE_TXB2
||
2656 opcode
== TGSI_OPCODE_TXL2
||
2657 opcode
== TGSI_OPCODE_SAMPLE
||
2658 opcode
== TGSI_OPCODE_SAMPLE_B
||
2659 opcode
== TGSI_OPCODE_SAMPLE_C
||
2660 opcode
== TGSI_OPCODE_SAMPLE_C_LZ
||
2661 opcode
== TGSI_OPCODE_SAMPLE_D
||
2662 opcode
== TGSI_OPCODE_SAMPLE_I
||
2663 opcode
== TGSI_OPCODE_SAMPLE_I_MS
||
2664 opcode
== TGSI_OPCODE_SAMPLE_L
||
2665 opcode
== TGSI_OPCODE_SVIEWINFO
||
2666 opcode
== TGSI_OPCODE_CAL
||
2667 opcode
== TGSI_OPCODE_CALLNZ
||
2668 opcode
== TGSI_OPCODE_IF
||
2669 opcode
== TGSI_OPCODE_UIF
||
2670 opcode
== TGSI_OPCODE_BGNLOOP
||
2671 opcode
== TGSI_OPCODE_SWITCH
)
2681 * Kill fragment if any of the src register values are negative.
2685 struct lp_build_tgsi_soa_context
*bld
,
2686 const struct tgsi_full_instruction
*inst
,
2689 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
2690 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
2691 LLVMValueRef terms
[TGSI_NUM_CHANNELS
];
2693 unsigned chan_index
;
2695 memset(&terms
, 0, sizeof terms
);
2697 TGSI_FOR_EACH_CHANNEL( chan_index
) {
2700 /* Unswizzle channel */
2701 swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
2703 /* Check if the component has not been already tested. */
2704 assert(swizzle
< TGSI_NUM_CHANNELS
);
2705 if( !terms
[swizzle
] )
2706 /* TODO: change the comparison operator instead of setting the sign */
2707 terms
[swizzle
] = lp_build_emit_fetch(&bld
->bld_base
, inst
, 0, chan_index
);
2711 TGSI_FOR_EACH_CHANNEL( chan_index
) {
2712 if(terms
[chan_index
]) {
2713 LLVMValueRef chan_mask
;
2716 * If term < 0 then mask = 0 else mask = ~0.
2718 chan_mask
= lp_build_cmp(&bld
->bld_base
.base
, PIPE_FUNC_GEQUAL
, terms
[chan_index
], bld
->bld_base
.base
.zero
);
2721 mask
= LLVMBuildAnd(builder
, mask
, chan_mask
, "");
2727 if (bld
->exec_mask
.has_mask
) {
2728 LLVMValueRef invmask
;
2729 invmask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
2730 mask
= LLVMBuildOr(builder
, mask
, invmask
, "");
2733 lp_build_mask_update(bld
->mask
, mask
);
2734 if (!near_end_of_shader(bld
, pc
))
2735 lp_build_mask_check(bld
->mask
);
2740 * Unconditional fragment kill.
2741 * The only predication is the execution mask which will apply if
2742 * we're inside a loop or conditional.
2745 emit_kill(struct lp_build_tgsi_soa_context
*bld
,
2748 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
2751 /* For those channels which are "alive", disable fragment shader
2754 if (bld
->exec_mask
.has_mask
) {
2755 mask
= LLVMBuildNot(builder
, bld
->exec_mask
.exec_mask
, "kilp");
2758 LLVMValueRef zero
= LLVMConstNull(bld
->bld_base
.base
.int_vec_type
);
2762 lp_build_mask_update(bld
->mask
, mask
);
2764 if (!near_end_of_shader(bld
, pc
))
2765 lp_build_mask_check(bld
->mask
);
2770 * Emit code which will dump the value of all the temporary registers
2774 emit_dump_file(struct lp_build_tgsi_soa_context
*bld
,
2777 const struct tgsi_shader_info
*info
= bld
->bld_base
.info
;
2778 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
2779 LLVMBuilderRef builder
= gallivm
->builder
;
2780 LLVMValueRef reg_ptr
;
2782 int max_index
= info
->file_max
[file
];
2785 * Some register files, particularly constants, can be very large,
2786 * and dumping everything could make this unusably slow.
2788 max_index
= MIN2(max_index
, 32);
2790 for (index
= 0; index
<= max_index
; index
++) {
2795 if (index
< 8 * sizeof(unsigned) &&
2796 (info
->file_mask
[file
] & (1u << index
)) == 0) {
2797 /* This was not declared.*/
2801 if (file
== TGSI_FILE_INPUT
) {
2802 mask
= info
->input_usage_mask
[index
];
2804 mask
= TGSI_WRITEMASK_XYZW
;
2807 for (chan
= 0; chan
< 4; chan
++) {
2808 if ((mask
& (1 << chan
)) == 0) {
2809 /* This channel is not used.*/
2813 if (file
== TGSI_FILE_CONSTANT
) {
2814 struct tgsi_full_src_register reg
;
2815 memset(®
, 0, sizeof reg
);
2816 reg
.Register
.File
= file
;
2817 reg
.Register
.Index
= index
;
2818 reg
.Register
.SwizzleX
= 0;
2819 reg
.Register
.SwizzleY
= 1;
2820 reg
.Register
.SwizzleZ
= 2;
2821 reg
.Register
.SwizzleW
= 3;
2823 res
= bld
->bld_base
.emit_fetch_funcs
[file
](&bld
->bld_base
, ®
, TGSI_TYPE_FLOAT
, chan
);
2827 } else if (file
== TGSI_FILE_INPUT
) {
2828 res
= bld
->inputs
[index
][chan
];
2832 } else if (file
== TGSI_FILE_TEMPORARY
) {
2833 reg_ptr
= lp_get_temp_ptr_soa(bld
, index
, chan
);
2835 res
= LLVMBuildLoad(builder
, reg_ptr
, "");
2836 } else if (file
== TGSI_FILE_OUTPUT
) {
2837 reg_ptr
= lp_get_output_ptr(bld
, index
, chan
);
2839 res
= LLVMBuildLoad(builder
, reg_ptr
, "");
2845 emit_dump_reg(gallivm
, file
, index
, chan
, res
);
2853 lp_emit_declaration_soa(
2854 struct lp_build_tgsi_context
*bld_base
,
2855 const struct tgsi_full_declaration
*decl
)
2857 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
2858 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
2859 LLVMTypeRef vec_type
= bld
->bld_base
.base
.vec_type
;
2860 const unsigned first
= decl
->Range
.First
;
2861 const unsigned last
= decl
->Range
.Last
;
2864 assert(last
<= bld
->bld_base
.info
->file_max
[decl
->Declaration
.File
]);
2866 switch (decl
->Declaration
.File
) {
2867 case TGSI_FILE_TEMPORARY
:
2868 if (!(bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
))) {
2869 assert(last
< LP_MAX_INLINED_TEMPS
);
2870 for (idx
= first
; idx
<= last
; ++idx
) {
2871 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
2872 bld
->temps
[idx
][i
] = lp_build_alloca(gallivm
, vec_type
, "temp");
2877 case TGSI_FILE_OUTPUT
:
2878 if (!(bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
))) {
2879 for (idx
= first
; idx
<= last
; ++idx
) {
2880 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
2881 bld
->outputs
[idx
][i
] = lp_build_alloca(gallivm
,
2882 vec_type
, "output");
2887 case TGSI_FILE_ADDRESS
:
2888 /* ADDR registers are only allocated with an integer LLVM IR type,
2889 * as they are guaranteed to always have integers.
2890 * XXX: Not sure if this exception is worthwhile (or the whole idea of
2891 * an ADDR register for that matter).
2893 assert(last
< LP_MAX_TGSI_ADDRS
);
2894 for (idx
= first
; idx
<= last
; ++idx
) {
2895 assert(idx
< LP_MAX_TGSI_ADDRS
);
2896 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++)
2897 bld
->addr
[idx
][i
] = lp_build_alloca(gallivm
, bld_base
->base
.int_vec_type
, "addr");
2901 case TGSI_FILE_SAMPLER_VIEW
:
2903 * The target stored here MUST match whatever there actually
2904 * is in the set sampler views (what about return type?).
2906 assert(last
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
2907 for (idx
= first
; idx
<= last
; ++idx
) {
2908 bld
->sv
[idx
] = decl
->SamplerView
;
2912 case TGSI_FILE_CONSTANT
:
2915 * We could trivially fetch the per-buffer pointer when fetching the
2916 * constant, relying on llvm to figure out it's always the same pointer
2917 * anyway. However, doing so results in a huge (more than factor of 10)
2918 * slowdown in llvm compilation times for some (but not all) shaders
2919 * (more specifically, the IR optimization spends way more time in
2920 * DominatorTree::dominates). At least with llvm versions 3.1, 3.3.
2922 unsigned idx2D
= decl
->Dim
.Index2D
;
2923 LLVMValueRef index2D
= lp_build_const_int32(gallivm
, idx2D
);
2924 assert(idx2D
< LP_MAX_TGSI_CONST_BUFFERS
);
2925 bld
->consts
[idx2D
] =
2926 lp_build_array_get(gallivm
, bld
->consts_ptr
, index2D
);
2927 bld
->consts_sizes
[idx2D
] =
2928 lp_build_array_get(gallivm
, bld
->const_sizes_ptr
, index2D
);
2933 /* don't need to declare other vars */
2939 void lp_emit_immediate_soa(
2940 struct lp_build_tgsi_context
*bld_base
,
2941 const struct tgsi_full_immediate
*imm
)
2943 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
2944 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2945 LLVMValueRef imms
[4];
2947 const uint size
= imm
->Immediate
.NrTokens
- 1;
2949 switch (imm
->Immediate
.DataType
) {
2950 case TGSI_IMM_FLOAT32
:
2951 for( i
= 0; i
< size
; ++i
)
2953 lp_build_const_vec(gallivm
, bld_base
->base
.type
, imm
->u
[i
].Float
);
2956 case TGSI_IMM_FLOAT64
:
2957 case TGSI_IMM_UINT64
:
2958 case TGSI_IMM_INT64
:
2959 case TGSI_IMM_UINT32
:
2960 for( i
= 0; i
< size
; ++i
) {
2961 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->uint_bld
.type
, imm
->u
[i
].Uint
);
2962 imms
[i
] = LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
2966 case TGSI_IMM_INT32
:
2967 for( i
= 0; i
< size
; ++i
) {
2968 LLVMValueRef tmp
= lp_build_const_vec(gallivm
, bld_base
->int_bld
.type
, imm
->u
[i
].Int
);
2969 imms
[i
] = LLVMConstBitCast(tmp
, bld_base
->base
.vec_type
);
2974 for( i
= size
; i
< 4; ++i
)
2975 imms
[i
] = bld_base
->base
.undef
;
2977 if (bld
->use_immediates_array
) {
2978 unsigned index
= bld
->num_immediates
;
2979 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
2980 LLVMBuilderRef builder
= gallivm
->builder
;
2982 assert(bld
->indirect_files
& (1 << TGSI_FILE_IMMEDIATE
));
2983 for (i
= 0; i
< 4; ++i
) {
2984 LLVMValueRef lindex
= lp_build_const_int32(
2985 bld
->bld_base
.base
.gallivm
, index
* 4 + i
);
2986 LLVMValueRef imm_ptr
= LLVMBuildGEP(builder
,
2987 bld
->imms_array
, &lindex
, 1, "");
2988 LLVMBuildStore(builder
, imms
[i
], imm_ptr
);
2991 /* simply copy the immediate values into the next immediates[] slot */
2993 assert(imm
->Immediate
.NrTokens
- 1 <= 4);
2994 assert(bld
->num_immediates
< LP_MAX_INLINED_IMMEDIATES
);
2996 for(i
= 0; i
< 4; ++i
)
2997 bld
->immediates
[bld
->num_immediates
][i
] = imms
[i
];
2999 if (bld
->indirect_files
& (1 << TGSI_FILE_IMMEDIATE
)) {
3000 unsigned index
= bld
->num_immediates
;
3001 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
3002 LLVMBuilderRef builder
= gallivm
->builder
;
3003 for (i
= 0; i
< 4; ++i
) {
3004 LLVMValueRef lindex
= lp_build_const_int32(
3005 bld
->bld_base
.base
.gallivm
, index
* 4 + i
);
3006 LLVMValueRef imm_ptr
= LLVMBuildGEP(builder
,
3007 bld
->imms_array
, &lindex
, 1, "");
3008 LLVMBuildStore(builder
,
3009 bld
->immediates
[index
][i
],
3015 bld
->num_immediates
++;
3020 const struct lp_build_tgsi_action
* action
,
3021 struct lp_build_tgsi_context
* bld_base
,
3022 struct lp_build_emit_data
* emit_data
)
3024 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3026 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
,
3027 &emit_data
->output
[emit_data
->chan
], NULL
);
3032 const struct lp_build_tgsi_action
* action
,
3033 struct lp_build_tgsi_context
* bld_base
,
3034 struct lp_build_emit_data
* emit_data
)
3036 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3038 emit_fetch_deriv(bld
, emit_data
->args
[0], NULL
, NULL
,
3039 &emit_data
->output
[emit_data
->chan
]);
3044 const struct lp_build_tgsi_action
* action
,
3045 struct lp_build_tgsi_context
* bld_base
,
3046 struct lp_build_emit_data
* emit_data
)
3048 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3050 emit_kill(bld
, bld_base
->pc
- 1);
3055 const struct lp_build_tgsi_action
* action
,
3056 struct lp_build_tgsi_context
* bld_base
,
3057 struct lp_build_emit_data
* emit_data
)
3059 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3061 emit_kill_if(bld
, emit_data
->inst
, bld_base
->pc
- 1);
3066 const struct lp_build_tgsi_action
* action
,
3067 struct lp_build_tgsi_context
* bld_base
,
3068 struct lp_build_emit_data
* emit_data
)
3070 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3072 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3073 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3078 const struct lp_build_tgsi_action
* action
,
3079 struct lp_build_tgsi_context
* bld_base
,
3080 struct lp_build_emit_data
* emit_data
)
3082 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3084 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3085 emit_data
->output
, 2, LP_SAMPLER_OP_TEXTURE
);
3090 const struct lp_build_tgsi_action
* action
,
3091 struct lp_build_tgsi_context
* bld_base
,
3092 struct lp_build_emit_data
* emit_data
)
3094 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3096 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
3097 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3102 const struct lp_build_tgsi_action
* action
,
3103 struct lp_build_tgsi_context
* bld_base
,
3104 struct lp_build_emit_data
* emit_data
)
3106 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3108 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
3109 emit_data
->output
, 2, LP_SAMPLER_OP_TEXTURE
);
3114 const struct lp_build_tgsi_action
* action
,
3115 struct lp_build_tgsi_context
* bld_base
,
3116 struct lp_build_emit_data
* emit_data
)
3118 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3120 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
3121 emit_data
->output
, 3, LP_SAMPLER_OP_TEXTURE
);
3126 const struct lp_build_tgsi_action
* action
,
3127 struct lp_build_tgsi_context
* bld_base
,
3128 struct lp_build_emit_data
* emit_data
)
3130 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3132 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
3133 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3138 const struct lp_build_tgsi_action
* action
,
3139 struct lp_build_tgsi_context
* bld_base
,
3140 struct lp_build_emit_data
* emit_data
)
3142 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3144 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
3145 emit_data
->output
, 2, LP_SAMPLER_OP_TEXTURE
);
3150 const struct lp_build_tgsi_action
* action
,
3151 struct lp_build_tgsi_context
* bld_base
,
3152 struct lp_build_emit_data
* emit_data
)
3154 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3156 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_PROJECTED
,
3157 emit_data
->output
, 1, LP_SAMPLER_OP_TEXTURE
);
3162 const struct lp_build_tgsi_action
* action
,
3163 struct lp_build_tgsi_context
* bld_base
,
3164 struct lp_build_emit_data
* emit_data
)
3166 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3168 emit_tex(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3169 emit_data
->output
, 2, LP_SAMPLER_OP_GATHER
);
3174 const struct lp_build_tgsi_action
* action
,
3175 struct lp_build_tgsi_context
* bld_base
,
3176 struct lp_build_emit_data
* emit_data
)
3178 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3180 emit_size_query(bld
, emit_data
->inst
, emit_data
->output
, FALSE
);
3185 const struct lp_build_tgsi_action
* action
,
3186 struct lp_build_tgsi_context
* bld_base
,
3187 struct lp_build_emit_data
* emit_data
)
3189 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3191 emit_fetch_texels(bld
, emit_data
->inst
, emit_data
->output
, FALSE
);
3196 const struct lp_build_tgsi_action
* action
,
3197 struct lp_build_tgsi_context
* bld_base
,
3198 struct lp_build_emit_data
* emit_data
)
3200 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3202 emit_fetch_texels(bld
, emit_data
->inst
, emit_data
->output
, TRUE
);
3207 const struct lp_build_tgsi_action
* action
,
3208 struct lp_build_tgsi_context
* bld_base
,
3209 struct lp_build_emit_data
* emit_data
)
3211 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3213 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3214 FALSE
, emit_data
->output
);
3219 const struct lp_build_tgsi_action
* action
,
3220 struct lp_build_tgsi_context
* bld_base
,
3221 struct lp_build_emit_data
* emit_data
)
3223 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3225 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
,
3226 FALSE
, emit_data
->output
);
3231 const struct lp_build_tgsi_action
* action
,
3232 struct lp_build_tgsi_context
* bld_base
,
3233 struct lp_build_emit_data
* emit_data
)
3235 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3237 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_NONE
,
3238 TRUE
, emit_data
->output
);
3243 const struct lp_build_tgsi_action
* action
,
3244 struct lp_build_tgsi_context
* bld_base
,
3245 struct lp_build_emit_data
* emit_data
)
3247 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3249 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_LOD_ZERO
,
3250 TRUE
, emit_data
->output
);
3255 const struct lp_build_tgsi_action
* action
,
3256 struct lp_build_tgsi_context
* bld_base
,
3257 struct lp_build_emit_data
* emit_data
)
3259 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3261 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
3262 FALSE
, emit_data
->output
);
3267 const struct lp_build_tgsi_action
* action
,
3268 struct lp_build_tgsi_context
* bld_base
,
3269 struct lp_build_emit_data
* emit_data
)
3271 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3273 emit_sample(bld
, emit_data
->inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
3274 FALSE
, emit_data
->output
);
3279 const struct lp_build_tgsi_action
* action
,
3280 struct lp_build_tgsi_context
* bld_base
,
3281 struct lp_build_emit_data
* emit_data
)
3283 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3285 emit_size_query(bld
, emit_data
->inst
, emit_data
->output
, TRUE
);
3289 mask_vec(struct lp_build_tgsi_context
*bld_base
)
3291 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3292 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3293 struct lp_exec_mask
*exec_mask
= &bld
->exec_mask
;
3295 if (!exec_mask
->has_mask
) {
3296 return lp_build_mask_value(bld
->mask
);
3298 return LLVMBuildAnd(builder
, lp_build_mask_value(bld
->mask
),
3299 exec_mask
->exec_mask
, "");
3303 increment_vec_ptr_by_mask(struct lp_build_tgsi_context
* bld_base
,
3307 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3308 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
3310 current_vec
= LLVMBuildSub(builder
, current_vec
, mask
, "");
3312 LLVMBuildStore(builder
, current_vec
, ptr
);
3316 clear_uint_vec_ptr_from_mask(struct lp_build_tgsi_context
* bld_base
,
3320 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3321 LLVMValueRef current_vec
= LLVMBuildLoad(builder
, ptr
, "");
3323 current_vec
= lp_build_select(&bld_base
->uint_bld
,
3325 bld_base
->uint_bld
.zero
,
3328 LLVMBuildStore(builder
, current_vec
, ptr
);
3332 clamp_mask_to_max_output_vertices(struct lp_build_tgsi_soa_context
* bld
,
3333 LLVMValueRef current_mask_vec
,
3334 LLVMValueRef total_emitted_vertices_vec
)
3336 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3337 struct lp_build_context
*int_bld
= &bld
->bld_base
.int_bld
;
3338 LLVMValueRef max_mask
= lp_build_cmp(int_bld
, PIPE_FUNC_LESS
,
3339 total_emitted_vertices_vec
,
3340 bld
->max_output_vertices_vec
);
3342 return LLVMBuildAnd(builder
, current_mask_vec
, max_mask
, "");
3347 const struct lp_build_tgsi_action
* action
,
3348 struct lp_build_tgsi_context
* bld_base
,
3349 struct lp_build_emit_data
* emit_data
)
3351 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3352 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3354 if (bld
->gs_iface
->emit_vertex
) {
3355 LLVMValueRef mask
= mask_vec(bld_base
);
3356 LLVMValueRef total_emitted_vertices_vec
=
3357 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
3358 mask
= clamp_mask_to_max_output_vertices(bld
, mask
,
3359 total_emitted_vertices_vec
);
3360 gather_outputs(bld
);
3361 bld
->gs_iface
->emit_vertex(bld
->gs_iface
, &bld
->bld_base
,
3363 total_emitted_vertices_vec
);
3364 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
3366 increment_vec_ptr_by_mask(bld_base
, bld
->total_emitted_vertices_vec_ptr
,
3369 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3370 " +++ emit vertex masked ones = ",
3372 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3373 " +++ emit vertex emitted = ",
3374 total_emitted_vertices_vec
);
3381 end_primitive_masked(struct lp_build_tgsi_context
* bld_base
,
3384 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3385 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
3387 if (bld
->gs_iface
->end_primitive
) {
3388 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3389 LLVMValueRef emitted_vertices_vec
=
3390 LLVMBuildLoad(builder
, bld
->emitted_vertices_vec_ptr
, "");
3391 LLVMValueRef emitted_prims_vec
=
3392 LLVMBuildLoad(builder
, bld
->emitted_prims_vec_ptr
, "");
3394 LLVMValueRef emitted_mask
= lp_build_cmp(uint_bld
, PIPE_FUNC_NOTEQUAL
,
3395 emitted_vertices_vec
,
3397 /* We need to combine the current execution mask with the mask
3398 telling us which, if any, execution slots actually have
3399 unemitted primitives, this way we make sure that end_primitives
3400 executes only on the paths that have unflushed vertices */
3401 mask
= LLVMBuildAnd(builder
, mask
, emitted_mask
, "");
3403 bld
->gs_iface
->end_primitive(bld
->gs_iface
, &bld
->bld_base
,
3404 emitted_vertices_vec
,
3408 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3409 " +++ end prim masked ones = ",
3411 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3412 " +++ end prim emitted verts1 = ",
3413 emitted_vertices_vec
);
3414 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3415 " +++ end prim emitted prims1 = ",
3416 LLVMBuildLoad(builder
,
3417 bld
->emitted_prims_vec_ptr
, ""));
3419 increment_vec_ptr_by_mask(bld_base
, bld
->emitted_prims_vec_ptr
,
3421 clear_uint_vec_ptr_from_mask(bld_base
, bld
->emitted_vertices_vec_ptr
,
3424 lp_build_print_value(bld
->bld_base
.base
.gallivm
,
3425 " +++ end prim emitted verts2 = ",
3426 LLVMBuildLoad(builder
,
3427 bld
->emitted_vertices_vec_ptr
, ""));
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
);
3441 if (bld
->gs_iface
->end_primitive
) {
3442 LLVMValueRef mask
= mask_vec(bld_base
);
3443 end_primitive_masked(bld_base
, mask
);
3449 const struct lp_build_tgsi_action
* action
,
3450 struct lp_build_tgsi_context
* bld_base
,
3451 struct lp_build_emit_data
* emit_data
)
3453 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3455 lp_exec_mask_call(&bld
->exec_mask
, emit_data
->inst
->Label
.Label
,
3461 const struct lp_build_tgsi_action
* action
,
3462 struct lp_build_tgsi_context
* bld_base
,
3463 struct lp_build_emit_data
* emit_data
)
3465 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3467 lp_exec_mask_ret(&bld
->exec_mask
, &bld_base
->pc
);
3472 const struct lp_build_tgsi_action
* action
,
3473 struct lp_build_tgsi_context
* bld_base
,
3474 struct lp_build_emit_data
* emit_data
)
3476 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3478 lp_exec_break(&bld
->exec_mask
, bld_base
);
3483 const struct lp_build_tgsi_action
* action
,
3484 struct lp_build_tgsi_context
* bld_base
,
3485 struct lp_build_emit_data
* emit_data
)
3487 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3488 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3489 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3490 LLVMValueRef unsigned_cond
=
3491 LLVMBuildBitCast(builder
, emit_data
->args
[0], uint_bld
->vec_type
, "");
3492 LLVMValueRef cond
= lp_build_cmp(uint_bld
, PIPE_FUNC_NOTEQUAL
,
3496 lp_exec_break_condition(&bld
->exec_mask
, cond
);
3501 const struct lp_build_tgsi_action
* action
,
3502 struct lp_build_tgsi_context
* bld_base
,
3503 struct lp_build_emit_data
* emit_data
)
3506 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3508 tmp
= lp_build_cmp(&bld_base
->base
, PIPE_FUNC_NOTEQUAL
,
3509 emit_data
->args
[0], bld
->bld_base
.base
.zero
);
3510 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
3515 const struct lp_build_tgsi_action
* action
,
3516 struct lp_build_tgsi_context
* bld_base
,
3517 struct lp_build_emit_data
* emit_data
)
3520 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3521 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3523 tmp
= lp_build_cmp(uint_bld
, PIPE_FUNC_NOTEQUAL
,
3524 emit_data
->args
[0], uint_bld
->zero
);
3525 lp_exec_mask_cond_push(&bld
->exec_mask
, tmp
);
3530 const struct lp_build_tgsi_action
* action
,
3531 struct lp_build_tgsi_context
* bld_base
,
3532 struct lp_build_emit_data
* emit_data
)
3534 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3536 lp_exec_case(&bld
->exec_mask
, emit_data
->args
[0]);
3541 const struct lp_build_tgsi_action
* action
,
3542 struct lp_build_tgsi_context
* bld_base
,
3543 struct lp_build_emit_data
* emit_data
)
3545 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3547 lp_exec_default(&bld
->exec_mask
, bld_base
);
3552 const struct lp_build_tgsi_action
* action
,
3553 struct lp_build_tgsi_context
* bld_base
,
3554 struct lp_build_emit_data
* emit_data
)
3556 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3558 lp_exec_switch(&bld
->exec_mask
, emit_data
->args
[0]);
3563 const struct lp_build_tgsi_action
* action
,
3564 struct lp_build_tgsi_context
* bld_base
,
3565 struct lp_build_emit_data
* emit_data
)
3567 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3569 lp_exec_endswitch(&bld
->exec_mask
, bld_base
);
3574 const struct lp_build_tgsi_action
* action
,
3575 struct lp_build_tgsi_context
* bld_base
,
3576 struct lp_build_emit_data
* emit_data
)
3578 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3580 lp_exec_bgnloop(&bld
->exec_mask
);
3585 const struct lp_build_tgsi_action
* action
,
3586 struct lp_build_tgsi_context
* bld_base
,
3587 struct lp_build_emit_data
* emit_data
)
3589 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3591 lp_exec_mask_bgnsub(&bld
->exec_mask
);
3596 const struct lp_build_tgsi_action
* action
,
3597 struct lp_build_tgsi_context
* bld_base
,
3598 struct lp_build_emit_data
* emit_data
)
3600 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3602 lp_exec_mask_cond_invert(&bld
->exec_mask
);
3607 const struct lp_build_tgsi_action
* action
,
3608 struct lp_build_tgsi_context
* bld_base
,
3609 struct lp_build_emit_data
* emit_data
)
3611 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3613 lp_exec_mask_cond_pop(&bld
->exec_mask
);
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_endloop(bld_base
->base
.gallivm
, &bld
->exec_mask
);
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_mask_endsub(&bld
->exec_mask
, &bld_base
->pc
);
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_continue(&bld
->exec_mask
);
3649 static void emit_prologue(struct lp_build_tgsi_context
* bld_base
)
3651 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3652 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3654 if (bld
->indirect_files
& (1 << TGSI_FILE_TEMPORARY
)) {
3655 LLVMValueRef array_size
=
3656 lp_build_const_int32(gallivm
,
3657 bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] * 4 + 4);
3658 bld
->temps_array
= lp_build_array_alloca(gallivm
,
3659 bld_base
->base
.vec_type
, array_size
,
3663 if (bld
->indirect_files
& (1 << TGSI_FILE_OUTPUT
)) {
3664 LLVMValueRef array_size
=
3665 lp_build_const_int32(gallivm
,
3666 bld_base
->info
->file_max
[TGSI_FILE_OUTPUT
] * 4 + 4);
3667 bld
->outputs_array
= lp_build_array_alloca(gallivm
,
3668 bld_base
->base
.vec_type
, array_size
,
3672 if (bld
->indirect_files
& (1 << TGSI_FILE_IMMEDIATE
)) {
3673 LLVMValueRef array_size
=
3674 lp_build_const_int32(gallivm
,
3675 bld_base
->info
->file_max
[TGSI_FILE_IMMEDIATE
] * 4 + 4);
3676 bld
->imms_array
= lp_build_array_alloca(gallivm
,
3677 bld_base
->base
.vec_type
, array_size
,
3681 /* If we have indirect addressing in inputs we need to copy them into
3682 * our alloca array to be able to iterate over them */
3683 if (bld
->indirect_files
& (1 << TGSI_FILE_INPUT
) && !bld
->gs_iface
) {
3684 unsigned index
, chan
;
3685 LLVMTypeRef vec_type
= bld_base
->base
.vec_type
;
3686 LLVMValueRef array_size
= lp_build_const_int32(gallivm
,
3687 bld_base
->info
->file_max
[TGSI_FILE_INPUT
]*4 + 4);
3688 bld
->inputs_array
= lp_build_array_alloca(gallivm
,
3689 vec_type
, array_size
,
3692 assert(bld_base
->info
->num_inputs
3693 <= bld_base
->info
->file_max
[TGSI_FILE_INPUT
] + 1);
3695 for (index
= 0; index
< bld_base
->info
->num_inputs
; ++index
) {
3696 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
3697 LLVMValueRef lindex
=
3698 lp_build_const_int32(gallivm
, index
* 4 + chan
);
3699 LLVMValueRef input_ptr
=
3700 LLVMBuildGEP(gallivm
->builder
, bld
->inputs_array
,
3702 LLVMValueRef value
= bld
->inputs
[index
][chan
];
3704 LLVMBuildStore(gallivm
->builder
, value
, input_ptr
);
3709 if (bld
->gs_iface
) {
3710 struct lp_build_context
*uint_bld
= &bld
->bld_base
.uint_bld
;
3711 bld
->emitted_prims_vec_ptr
=
3712 lp_build_alloca(gallivm
,
3714 "emitted_prims_ptr");
3715 bld
->emitted_vertices_vec_ptr
=
3716 lp_build_alloca(gallivm
,
3718 "emitted_vertices_ptr");
3719 bld
->total_emitted_vertices_vec_ptr
=
3720 lp_build_alloca(gallivm
,
3722 "total_emitted_vertices_ptr");
3724 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
3725 bld
->emitted_prims_vec_ptr
);
3726 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
3727 bld
->emitted_vertices_vec_ptr
);
3728 LLVMBuildStore(gallivm
->builder
, uint_bld
->zero
,
3729 bld
->total_emitted_vertices_vec_ptr
);
3732 if (DEBUG_EXECUTION
) {
3733 lp_build_printf(gallivm
, "\n");
3734 emit_dump_file(bld
, TGSI_FILE_CONSTANT
);
3736 emit_dump_file(bld
, TGSI_FILE_INPUT
);
3740 static void emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
3742 struct lp_build_tgsi_soa_context
* bld
= lp_soa_context(bld_base
);
3743 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3745 if (DEBUG_EXECUTION
) {
3748 emit_dump_file(bld
, TGSI_FILE_TEMPORARY
);
3750 emit_dump_file(bld
, TGSI_FILE_OUTPUT
);
3751 lp_build_printf(bld_base
->base
.gallivm
, "\n");
3754 /* If we have indirect addressing in outputs we need to copy our alloca array
3755 * to the outputs slots specified by the caller */
3756 if (bld
->gs_iface
) {
3757 LLVMValueRef total_emitted_vertices_vec
;
3758 LLVMValueRef emitted_prims_vec
;
3759 /* implicit end_primitives, needed in case there are any unflushed
3760 vertices in the cache. Note must not call end_primitive here
3761 since the exec_mask is not valid at this point. */
3762 end_primitive_masked(bld_base
, lp_build_mask_value(bld
->mask
));
3764 total_emitted_vertices_vec
=
3765 LLVMBuildLoad(builder
, bld
->total_emitted_vertices_vec_ptr
, "");
3767 LLVMBuildLoad(builder
, bld
->emitted_prims_vec_ptr
, "");
3769 bld
->gs_iface
->gs_epilogue(bld
->gs_iface
,
3771 total_emitted_vertices_vec
,
3774 gather_outputs(bld
);
3779 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
3780 const struct tgsi_token
*tokens
,
3781 struct lp_type type
,
3782 struct lp_build_mask_context
*mask
,
3783 LLVMValueRef consts_ptr
,
3784 LLVMValueRef const_sizes_ptr
,
3785 const struct lp_bld_tgsi_system_values
*system_values
,
3786 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
3787 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
3788 LLVMValueRef context_ptr
,
3789 LLVMValueRef thread_data_ptr
,
3790 struct lp_build_sampler_soa
*sampler
,
3791 const struct tgsi_shader_info
*info
,
3792 const struct lp_build_tgsi_gs_iface
*gs_iface
)
3794 struct lp_build_tgsi_soa_context bld
;
3796 struct lp_type res_type
;
3798 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
3799 memset(&res_type
, 0, sizeof res_type
);
3800 res_type
.width
= type
.width
;
3801 res_type
.length
= type
.length
;
3804 /* Setup build context */
3805 memset(&bld
, 0, sizeof bld
);
3806 lp_build_context_init(&bld
.bld_base
.base
, gallivm
, type
);
3807 lp_build_context_init(&bld
.bld_base
.uint_bld
, gallivm
, lp_uint_type(type
));
3808 lp_build_context_init(&bld
.bld_base
.int_bld
, gallivm
, lp_int_type(type
));
3809 lp_build_context_init(&bld
.elem_bld
, gallivm
, lp_elem_type(type
));
3811 struct lp_type dbl_type
;
3813 dbl_type
.width
*= 2;
3814 lp_build_context_init(&bld
.bld_base
.dbl_bld
, gallivm
, dbl_type
);
3817 struct lp_type uint64_type
;
3818 uint64_type
= lp_uint_type(type
);
3819 uint64_type
.width
*= 2;
3820 lp_build_context_init(&bld
.bld_base
.uint64_bld
, gallivm
, uint64_type
);
3823 struct lp_type int64_type
;
3824 int64_type
= lp_int_type(type
);
3825 int64_type
.width
*= 2;
3826 lp_build_context_init(&bld
.bld_base
.int64_bld
, gallivm
, int64_type
);
3829 bld
.inputs
= inputs
;
3830 bld
.outputs
= outputs
;
3831 bld
.consts_ptr
= consts_ptr
;
3832 bld
.const_sizes_ptr
= const_sizes_ptr
;
3833 bld
.sampler
= sampler
;
3834 bld
.bld_base
.info
= info
;
3835 bld
.indirect_files
= info
->indirect_files
;
3836 bld
.context_ptr
= context_ptr
;
3837 bld
.thread_data_ptr
= thread_data_ptr
;
3840 * If the number of temporaries is rather large then we just
3841 * allocate them as an array right from the start and treat
3842 * like indirect temporaries.
3844 if (info
->file_max
[TGSI_FILE_TEMPORARY
] >= LP_MAX_INLINED_TEMPS
) {
3845 bld
.indirect_files
|= (1 << TGSI_FILE_TEMPORARY
);
3848 * For performance reason immediates are always backed in a static
3849 * array, but if their number is too great, we have to use just
3850 * a dynamically allocated array.
3852 bld
.use_immediates_array
=
3853 (info
->file_max
[TGSI_FILE_IMMEDIATE
] >= LP_MAX_INLINED_IMMEDIATES
);
3854 if (bld
.use_immediates_array
) {
3855 bld
.indirect_files
|= (1 << TGSI_FILE_IMMEDIATE
);
3859 bld
.bld_base
.soa
= TRUE
;
3860 bld
.bld_base
.emit_debug
= emit_debug
;
3861 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = emit_fetch_constant
;
3862 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = emit_fetch_immediate
;
3863 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_input
;
3864 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = emit_fetch_temporary
;
3865 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = emit_fetch_system_value
;
3866 bld
.bld_base
.emit_store
= emit_store
;
3868 bld
.bld_base
.emit_declaration
= lp_emit_declaration_soa
;
3869 bld
.bld_base
.emit_immediate
= lp_emit_immediate_soa
;
3871 bld
.bld_base
.emit_prologue
= emit_prologue
;
3872 bld
.bld_base
.emit_epilogue
= emit_epilogue
;
3874 /* Set opcode actions */
3875 lp_set_default_actions_cpu(&bld
.bld_base
);
3877 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
3878 bld
.bld_base
.op_actions
[TGSI_OPCODE_BGNSUB
].emit
= bgnsub_emit
;
3879 bld
.bld_base
.op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
3880 bld
.bld_base
.op_actions
[TGSI_OPCODE_BREAKC
].emit
= breakc_emit
;
3881 bld
.bld_base
.op_actions
[TGSI_OPCODE_CAL
].emit
= cal_emit
;
3882 bld
.bld_base
.op_actions
[TGSI_OPCODE_CASE
].emit
= case_emit
;
3883 bld
.bld_base
.op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
3884 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDX
].emit
= ddx_emit
;
3885 bld
.bld_base
.op_actions
[TGSI_OPCODE_DDY
].emit
= ddy_emit
;
3886 bld
.bld_base
.op_actions
[TGSI_OPCODE_DEFAULT
].emit
= default_emit
;
3887 bld
.bld_base
.op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
3888 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
3889 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
3890 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSUB
].emit
= endsub_emit
;
3891 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDSWITCH
].emit
= endswitch_emit
;
3892 bld
.bld_base
.op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
3893 bld
.bld_base
.op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
3894 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILL_IF
].emit
= kill_if_emit
;
3895 bld
.bld_base
.op_actions
[TGSI_OPCODE_KILL
].emit
= kill_emit
;
3896 bld
.bld_base
.op_actions
[TGSI_OPCODE_RET
].emit
= ret_emit
;
3897 bld
.bld_base
.op_actions
[TGSI_OPCODE_SWITCH
].emit
= switch_emit
;
3898 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX
].emit
= tex_emit
;
3899 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB
].emit
= txb_emit
;
3900 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXD
].emit
= txd_emit
;
3901 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL
].emit
= txl_emit
;
3902 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXP
].emit
= txp_emit
;
3903 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
3904 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXF
].emit
= txf_emit
;
3905 bld
.bld_base
.op_actions
[TGSI_OPCODE_TEX2
].emit
= tex2_emit
;
3906 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXB2
].emit
= txb2_emit
;
3907 bld
.bld_base
.op_actions
[TGSI_OPCODE_TXL2
].emit
= txl2_emit
;
3908 bld
.bld_base
.op_actions
[TGSI_OPCODE_TG4
].emit
= tg4_emit
;
3909 /* DX10 sampling ops */
3910 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE
].emit
= sample_emit
;
3911 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_B
].emit
= sample_b_emit
;
3912 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_C
].emit
= sample_c_emit
;
3913 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_C_LZ
].emit
= sample_c_lz_emit
;
3914 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_D
].emit
= sample_d_emit
;
3915 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_I
].emit
= sample_i_emit
;
3916 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_I_MS
].emit
= sample_i_emit
;
3917 bld
.bld_base
.op_actions
[TGSI_OPCODE_SAMPLE_L
].emit
= sample_l_emit
;
3918 bld
.bld_base
.op_actions
[TGSI_OPCODE_SVIEWINFO
].emit
= sviewinfo_emit
;
3921 /* There's no specific value for this because it should always
3922 * be set, but apps using ext_geometry_shader4 quite often
3923 * were forgetting so we're using MAX_VERTEX_VARYING from
3924 * that spec even though we could debug_assert if it's not
3925 * set, but that's a lot uglier. */
3926 uint max_output_vertices
;
3928 /* inputs are always indirect with gs */
3929 bld
.indirect_files
|= (1 << TGSI_FILE_INPUT
);
3930 bld
.gs_iface
= gs_iface
;
3931 bld
.bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = emit_fetch_gs_input
;
3932 bld
.bld_base
.op_actions
[TGSI_OPCODE_EMIT
].emit
= emit_vertex
;
3933 bld
.bld_base
.op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= end_primitive
;
3935 max_output_vertices
=
3936 info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
3937 if (!max_output_vertices
)
3938 max_output_vertices
= 32;
3940 bld
.max_output_vertices_vec
=
3941 lp_build_const_int_vec(gallivm
, bld
.bld_base
.int_bld
.type
,
3942 max_output_vertices
);
3945 lp_exec_mask_init(&bld
.exec_mask
, &bld
.bld_base
.int_bld
);
3947 bld
.system_values
= *system_values
;
3949 lp_build_tgsi_llvm(&bld
.bld_base
, tokens
);
3952 LLVMBasicBlockRef block
= LLVMGetInsertBlock(gallivm
->builder
);
3953 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
3954 debug_printf("11111111111111111111111111111 \n");
3955 tgsi_dump(tokens
, 0);
3956 lp_debug_dump_value(function
);
3957 debug_printf("2222222222222222222222222222 \n");
3961 LLVMModuleRef module
= LLVMGetGlobalParent(
3962 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm
->builder
)));
3963 LLVMDumpModule(module
);
3966 lp_exec_mask_fini(&bld
.exec_mask
);