llvmpipe/gallivm/draw: introduce a buffer type.
[platform/upstream/mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_tgsi_soa.c
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * Copyright 2007-2008 VMware, Inc.
5  * All Rights Reserved.
6  *
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:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
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.
26  *
27  **************************************************************************/
28
29 /**
30  * @file
31  * TGSI to LLVM IR translation -- SoA.
32  *
33  * @author Jose Fonseca <jfonseca@vmware.com>
34  *
35  * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
36  * Brian Paul, and others.
37  */
38
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 "util/u_prim.h"
45 #include "tgsi/tgsi_dump.h"
46 #include "tgsi/tgsi_exec.h"
47 #include "tgsi/tgsi_info.h"
48 #include "tgsi/tgsi_parse.h"
49 #include "tgsi/tgsi_util.h"
50 #include "tgsi/tgsi_scan.h"
51 #include "tgsi/tgsi_strings.h"
52 #include "lp_bld_tgsi_action.h"
53 #include "lp_bld_type.h"
54 #include "lp_bld_const.h"
55 #include "lp_bld_arit.h"
56 #include "lp_bld_bitarit.h"
57 #include "lp_bld_gather.h"
58 #include "lp_bld_init.h"
59 #include "lp_bld_logic.h"
60 #include "lp_bld_misc.h"
61 #include "lp_bld_swizzle.h"
62 #include "lp_bld_flow.h"
63 #include "lp_bld_coro.h"
64 #include "lp_bld_quad.h"
65 #include "lp_bld_tgsi.h"
66 #include "lp_bld_limits.h"
67 #include "lp_bld_debug.h"
68 #include "lp_bld_printf.h"
69 #include "lp_bld_sample.h"
70 #include "lp_bld_struct.h"
71 #include "lp_bld_jit_types.h"
72
73 #define DUMP_GS_EMITS 0
74
75 /*
76  * If non-zero, the generated LLVM IR will print intermediate results on every TGSI
77  * instruction.
78  *
79  * TODO:
80  * - take execution masks in consideration
81  * - debug control-flow instructions
82  */
83 #define DEBUG_EXECUTION 0
84
85
86 /*
87  * Emit code to print a register value.
88  */
89 static void
90 emit_dump_reg(struct gallivm_state *gallivm,
91               unsigned file,
92               unsigned index,
93               unsigned chan,
94               LLVMValueRef value)
95 {
96    char buf[32];
97
98    snprintf(buf, sizeof buf, "    %s[%u].%c = ",
99             tgsi_file_name(file),
100             index, "xyzw"[chan]);
101
102    lp_build_print_value(gallivm, buf, value);
103 }
104
105 static inline struct function_ctx *
106 func_ctx(struct lp_exec_mask *mask)
107 {
108    assert(mask->function_stack_size > 0);
109    assert(mask->function_stack_size <= LP_MAX_NUM_FUNCS);
110    return &mask->function_stack[mask->function_stack_size - 1];
111 }
112
113 /*
114  * combine the execution mask if there is one with the current mask.
115  */
116 static LLVMValueRef
117 mask_vec(struct lp_build_tgsi_context *bld_base)
118 {
119    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
120    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
121    struct lp_exec_mask *exec_mask = &bld->exec_mask;
122    LLVMValueRef bld_mask = bld->mask ? lp_build_mask_value(bld->mask) : NULL;
123    if (!exec_mask->has_mask) {
124       return bld_mask;
125    }
126    if (!bld_mask)
127       return exec_mask->exec_mask;
128    return LLVMBuildAnd(builder, lp_build_mask_value(bld->mask),
129                        exec_mask->exec_mask, "");
130 }
131
132 static void lp_exec_tgsi_break(struct lp_exec_mask *mask,
133                           struct lp_build_tgsi_context * bld_base)
134 {
135    enum tgsi_opcode opcode =
136       bld_base->instructions[bld_base->pc + 1].Instruction.Opcode;
137    bool break_always = (opcode == TGSI_OPCODE_ENDSWITCH ||
138                         opcode == TGSI_OPCODE_CASE);
139    lp_exec_break(mask, &bld_base->pc, break_always);
140 }
141
142 static void lp_exec_switch(struct lp_exec_mask *mask,
143                            LLVMValueRef switchval)
144 {
145    struct function_ctx *ctx = func_ctx(mask);
146
147    if (ctx->switch_stack_size >= LP_MAX_TGSI_NESTING ||
148        ctx->loop_stack_size > LP_MAX_TGSI_NESTING) {
149       ctx->switch_stack_size++;
150       return;
151    }
152
153    ctx->break_type_stack[ctx->loop_stack_size + ctx->switch_stack_size] =
154       ctx->break_type;
155    ctx->break_type = LP_EXEC_MASK_BREAK_TYPE_SWITCH;
156
157    ctx->switch_stack[ctx->switch_stack_size].switch_mask = mask->switch_mask;
158    ctx->switch_stack[ctx->switch_stack_size].switch_val = ctx->switch_val;
159    ctx->switch_stack[ctx->switch_stack_size].switch_mask_default = ctx->switch_mask_default;
160    ctx->switch_stack[ctx->switch_stack_size].switch_in_default = ctx->switch_in_default;
161    ctx->switch_stack[ctx->switch_stack_size].switch_pc = ctx->switch_pc;
162    ctx->switch_stack_size++;
163
164    mask->switch_mask = LLVMConstNull(mask->int_vec_type);
165    ctx->switch_val = switchval;
166    ctx->switch_mask_default = LLVMConstNull(mask->int_vec_type);
167    ctx->switch_in_default = false;
168    ctx->switch_pc = 0;
169
170    lp_exec_mask_update(mask);
171 }
172
173 static void lp_exec_endswitch(struct lp_exec_mask *mask,
174                               struct lp_build_tgsi_context * bld_base)
175 {
176    LLVMBuilderRef builder = mask->bld->gallivm->builder;
177    struct function_ctx *ctx = func_ctx(mask);
178
179    if (ctx->switch_stack_size > LP_MAX_TGSI_NESTING) {
180       ctx->switch_stack_size--;
181       return;
182    }
183
184    /* check if there's deferred default if so do it now */
185    if (ctx->switch_pc && !ctx->switch_in_default) {
186       LLVMValueRef prevmask, defaultmask;
187       unsigned tmp_pc;
188       prevmask = ctx->switch_stack[ctx->switch_stack_size - 1].switch_mask;
189       defaultmask = LLVMBuildNot(builder, ctx->switch_mask_default, "sw_default_mask");
190       mask->switch_mask = LLVMBuildAnd(builder, prevmask, defaultmask, "sw_mask");
191       ctx->switch_in_default = true;
192
193       lp_exec_mask_update(mask);
194
195       assert(bld_base->instructions[ctx->switch_pc - 1].Instruction.Opcode ==
196              TGSI_OPCODE_DEFAULT);
197
198       tmp_pc = bld_base->pc;
199       bld_base->pc = ctx->switch_pc;
200       /*
201        * re-purpose switch_pc to point to here again, since we stop execution of
202        * the deferred default after next break.
203        */
204       ctx->switch_pc = tmp_pc - 1;
205
206       return;
207    }
208
209    else if (ctx->switch_pc && ctx->switch_in_default) {
210       assert(bld_base->pc == ctx->switch_pc + 1);
211    }
212
213    ctx->switch_stack_size--;
214    mask->switch_mask = ctx->switch_stack[ctx->switch_stack_size].switch_mask;
215    ctx->switch_val = ctx->switch_stack[ctx->switch_stack_size].switch_val;
216    ctx->switch_mask_default = ctx->switch_stack[ctx->switch_stack_size].switch_mask_default;
217    ctx->switch_in_default = ctx->switch_stack[ctx->switch_stack_size].switch_in_default;
218    ctx->switch_pc = ctx->switch_stack[ctx->switch_stack_size].switch_pc;
219
220    ctx->break_type = ctx->break_type_stack[ctx->loop_stack_size + ctx->switch_stack_size];
221
222    lp_exec_mask_update(mask);
223 }
224
225 static void lp_exec_case(struct lp_exec_mask *mask,
226                          LLVMValueRef caseval)
227 {
228    LLVMBuilderRef builder = mask->bld->gallivm->builder;
229    struct function_ctx *ctx = func_ctx(mask);
230
231    LLVMValueRef casemask, prevmask;
232
233    if (ctx->switch_stack_size > LP_MAX_TGSI_NESTING) {
234       return;
235    }
236
237    /* skipping case mask evaluation here is NOT optional (not in all cases anyway). */
238    if (!ctx->switch_in_default) {
239       prevmask = ctx->switch_stack[ctx->switch_stack_size - 1].switch_mask;
240       casemask = lp_build_cmp(mask->bld, PIPE_FUNC_EQUAL, caseval, ctx->switch_val);
241       ctx->switch_mask_default = LLVMBuildOr(builder, casemask,
242                                              ctx->switch_mask_default, "sw_default_mask");
243       casemask = LLVMBuildOr(builder, casemask, mask->switch_mask, "");
244       mask->switch_mask = LLVMBuildAnd(builder, casemask, prevmask, "sw_mask");
245
246       lp_exec_mask_update(mask);
247    }
248 }
249
250 /*
251  * Analyse default statement in a switch.
252  * \return true if default is last statement, false otherwise
253  * \param default_pc_start contains pc of instruction to jump to
254  *                         if default wasn't last but there's no
255  *                         fallthrough into default.
256  */
257 static boolean default_analyse_is_last(struct lp_exec_mask *mask,
258                                        struct lp_build_tgsi_context * bld_base,
259                                        int *default_pc_start)
260 {
261    unsigned pc = bld_base->pc;
262    struct function_ctx *ctx = func_ctx(mask);
263    int curr_switch_stack = ctx->switch_stack_size;
264
265    if (ctx->switch_stack_size > LP_MAX_TGSI_NESTING) {
266       return false;
267    }
268
269    /* skip over case statements which are together with default */
270    while (bld_base->instructions[pc].Instruction.Opcode == TGSI_OPCODE_CASE) {
271       pc++;
272    }
273
274    while (pc != ~0u && pc < bld_base->num_instructions) {
275       enum tgsi_opcode opcode = bld_base->instructions[pc].Instruction.Opcode;
276       switch (opcode) {
277       case TGSI_OPCODE_CASE:
278          if (curr_switch_stack == ctx->switch_stack_size) {
279             *default_pc_start = pc - 1;
280             return false;
281          }
282          break;
283       case TGSI_OPCODE_SWITCH:
284          curr_switch_stack++;
285          break;
286       case TGSI_OPCODE_ENDSWITCH:
287          if (curr_switch_stack == ctx->switch_stack_size) {
288             *default_pc_start = pc - 1;
289             return true;
290          }
291          curr_switch_stack--;
292          break;
293       default:
294          ; /* nothing */
295       }
296       pc++;
297    }
298    /* should never arrive here */
299    assert(0);
300    return true;
301 }
302
303 static void lp_exec_default(struct lp_exec_mask *mask,
304                             struct lp_build_tgsi_context * bld_base)
305 {
306    LLVMBuilderRef builder = mask->bld->gallivm->builder;
307    struct function_ctx *ctx = func_ctx(mask);
308
309    int default_exec_pc = 0;
310    boolean default_is_last;
311
312    if (ctx->switch_stack_size > LP_MAX_TGSI_NESTING) {
313       return;
314    }
315
316    /*
317     * This is a messy opcode, because it may not be always at the end and
318     * there can be fallthrough in and out of it.
319     */
320
321    default_is_last = default_analyse_is_last(mask, bld_base, &default_exec_pc);
322    /*
323     * If it is last statement in switch (note that case statements appearing
324     * "at the same time" as default don't change that) everything is just fine,
325     * update switch mask and go on. This means we can handle default with
326     * fallthrough INTO it without overhead, if it is last.
327     */
328    if (default_is_last) {
329       LLVMValueRef prevmask, defaultmask;
330       prevmask = ctx->switch_stack[ctx->switch_stack_size - 1].switch_mask;
331       defaultmask = LLVMBuildNot(builder, ctx->switch_mask_default, "sw_default_mask");
332       defaultmask = LLVMBuildOr(builder, defaultmask, mask->switch_mask, "");
333       mask->switch_mask = LLVMBuildAnd(builder, prevmask, defaultmask, "sw_mask");
334       ctx->switch_in_default = true;
335
336       lp_exec_mask_update(mask);
337    }
338    else {
339       /*
340        * Technically, "case" immediately before default isn't really a
341        * fallthrough, however we still have to count them as such as we
342        * already have updated the masks.
343        * If that happens in practice could add a switch optimizer pass
344        * which just gets rid of all case statements appearing together with
345        * default (or could do switch analysis at switch start time instead).
346        */
347       enum tgsi_opcode opcode =
348          bld_base->instructions[bld_base->pc - 1].Instruction.Opcode;
349       boolean ft_into = (opcode != TGSI_OPCODE_BRK &&
350                          opcode != TGSI_OPCODE_SWITCH);
351       /*
352        * If it is not last statement and there was no fallthrough into it,
353        * we record the PC and continue execution at next case (again, those
354        * case encountered at the same time don't count). At endswitch
355        * time, we update switchmask, and go back executing the code we skipped
356        * until the next break (possibly re-executing some code with changed mask
357        * if there was a fallthrough out of default).
358        * Finally, if it is not last statement and there was a fallthrough into it,
359        * do the same as with the former case, except instead of skipping the code
360        * just execute it without updating the mask, then go back and re-execute.
361        */
362       ctx->switch_pc = bld_base->pc;
363       if (!ft_into) {
364          bld_base->pc = default_exec_pc;
365       }
366    }
367 }
368
369
370 static void lp_exec_mask_call(struct lp_exec_mask *mask,
371                               int func,
372                               int *pc)
373 {
374    if (mask->function_stack_size >= LP_MAX_NUM_FUNCS) {
375       return;
376    }
377
378    lp_exec_mask_function_init(mask, mask->function_stack_size);
379    mask->function_stack[mask->function_stack_size].pc = *pc;
380    mask->function_stack[mask->function_stack_size].ret_mask = mask->ret_mask;
381    mask->function_stack_size++;
382    *pc = func;
383 }
384
385 static void lp_exec_mask_ret(struct lp_exec_mask *mask, int *pc)
386 {
387    LLVMBuilderRef builder = mask->bld->gallivm->builder;
388    struct function_ctx *ctx = func_ctx(mask);
389    LLVMValueRef exec_mask;
390
391    if (ctx->cond_stack_size == 0 &&
392        ctx->loop_stack_size == 0 &&
393        ctx->switch_stack_size == 0 &&
394        mask->function_stack_size == 1) {
395       /* returning from main() */
396       *pc = -1;
397       return;
398    }
399
400    if (mask->function_stack_size == 1) {
401       /*
402        * This requires special handling since we need to ensure
403        * we don't drop the mask even if we have no call stack
404        * (e.g. after a ret in a if clause after the endif)
405        */
406       mask->ret_in_main = TRUE;
407    }
408
409    exec_mask = LLVMBuildNot(builder,
410                             mask->exec_mask,
411                             "ret");
412
413    mask->ret_mask = LLVMBuildAnd(builder,
414                                  mask->ret_mask,
415                                  exec_mask, "ret_full");
416
417    lp_exec_mask_update(mask);
418 }
419
420 static void lp_exec_mask_bgnsub(struct lp_exec_mask *mask)
421 {
422 }
423
424 static void lp_exec_mask_endsub(struct lp_exec_mask *mask, int *pc)
425 {
426    struct function_ctx *ctx;
427
428    assert(mask->function_stack_size > 1);
429    assert(mask->function_stack_size <= LP_MAX_NUM_FUNCS);
430
431    ctx = func_ctx(mask);
432    mask->function_stack_size--;
433
434    *pc = ctx->pc;
435    mask->ret_mask = ctx->ret_mask;
436
437    lp_exec_mask_update(mask);
438 }
439
440
441 static LLVMValueRef
442 get_file_ptr(struct lp_build_tgsi_soa_context *bld,
443              unsigned file,
444              int index,
445              unsigned chan)
446 {
447    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
448    LLVMValueRef (*array_of_vars)[TGSI_NUM_CHANNELS];
449    LLVMValueRef var_of_array;
450
451    switch (file) {
452    case TGSI_FILE_TEMPORARY:
453       array_of_vars = bld->temps;
454       var_of_array = bld->temps_array;
455       break;
456    case TGSI_FILE_OUTPUT:
457       array_of_vars = bld->outputs;
458       var_of_array = bld->outputs_array;
459       break;
460    default:
461       assert(0);
462       return NULL;
463    }
464
465    assert(chan < 4);
466
467    if (bld->indirect_files & (1 << file)) {
468       LLVMValueRef lindex = lp_build_const_int32(bld->bld_base.base.gallivm, index * 4 + chan);
469       if (LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(var_of_array))) == LLVMArrayTypeKind) {
470          LLVMValueRef gep[2];
471          gep[0] = lp_build_const_int32(bld->bld_base.base.gallivm, 0);
472          gep[1] = lindex;
473          return LLVMBuildGEP(builder, var_of_array, gep, 2, "");
474       } else {
475          return LLVMBuildGEP(builder, var_of_array, &lindex, 1, "");
476       }
477    }
478    else {
479       assert(index <= bld->bld_base.info->file_max[file]);
480       return array_of_vars[index][chan];
481    }
482 }
483
484
485 /**
486  * Return pointer to a temporary register channel (src or dest).
487  * Note that indirect addressing cannot be handled here.
488  * \param index  which temporary register
489  * \param chan  which channel of the temp register.
490  */
491 LLVMValueRef
492 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context *bld,
493              unsigned index,
494              unsigned chan)
495 {
496    return get_file_ptr(bld, TGSI_FILE_TEMPORARY, index, chan);
497 }
498
499 /**
500  * Return pointer to a output register channel (src or dest).
501  * Note that indirect addressing cannot be handled here.
502  * \param index  which output register
503  * \param chan  which channel of the output register.
504  */
505 LLVMValueRef
506 lp_get_output_ptr(struct lp_build_tgsi_soa_context *bld,
507                unsigned index,
508                unsigned chan)
509 {
510    return get_file_ptr(bld, TGSI_FILE_OUTPUT, index, chan);
511 }
512
513 /*
514  * If we have indirect addressing in outputs copy our alloca array
515  * to the outputs slots specified by the caller to make sure
516  * our outputs are delivered consistently via the same interface.
517  */
518 static void
519 gather_outputs(struct lp_build_tgsi_soa_context * bld)
520 {
521    if ((bld->indirect_files & (1 << TGSI_FILE_OUTPUT))) {
522       unsigned index, chan;
523       assert(bld->bld_base.info->num_outputs <=
524              bld->bld_base.info->file_max[TGSI_FILE_OUTPUT] + 1);
525       for (index = 0; index < bld->bld_base.info->num_outputs; ++index) {
526          for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
527             bld->outputs[index][chan] = lp_get_output_ptr(bld, index, chan);
528          }
529       }
530    }
531 }
532
533 /**
534  * Gather vector.
535  * XXX the lp_build_gather() function should be capable of doing this
536  * with a little work.
537  */
538 static LLVMValueRef
539 build_gather(struct lp_build_tgsi_context *bld_base,
540              LLVMValueRef base_ptr,
541              LLVMValueRef indexes,
542              LLVMValueRef overflow_mask,
543              LLVMValueRef indexes2)
544 {
545    struct gallivm_state *gallivm = bld_base->base.gallivm;
546    LLVMBuilderRef builder = gallivm->builder;
547    struct lp_build_context *uint_bld = &bld_base->uint_bld;
548    struct lp_build_context *bld = &bld_base->base;
549    LLVMValueRef res;
550    unsigned i;
551
552    if (indexes2)
553       res = LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), bld_base->base.type.length * 2));
554    else
555       res = bld->undef;
556    /*
557     * overflow_mask is a vector telling us which channels
558     * in the vector overflowed. We use the overflow behavior for
559     * constant buffers which is defined as:
560     * Out of bounds access to constant buffer returns 0 in all
561     * components. Out of bounds behavior is always with respect
562     * to the size of the buffer bound at that slot.
563     */
564
565    if (overflow_mask) {
566       /*
567        * We avoid per-element control flow here (also due to llvm going crazy,
568        * though I suspect it's better anyway since overflow is likely rare).
569        * Note that since we still fetch from buffers even if num_elements was
570        * zero (in this case we'll fetch from index zero) the jit func callers
571        * MUST provide valid fake constant buffers of size 4x32 (the values do
572        * not matter), otherwise we'd still need (not per element though)
573        * control flow.
574        */
575       indexes = lp_build_select(uint_bld, overflow_mask, uint_bld->zero, indexes);
576       if (indexes2)
577          indexes2 = lp_build_select(uint_bld, overflow_mask, uint_bld->zero, indexes2);
578    }
579
580    /*
581     * Loop over elements of index_vec, load scalar value, insert it into 'res'.
582     */
583    for (i = 0; i < bld->type.length * (indexes2 ? 2 : 1); i++) {
584       LLVMValueRef si, di;
585       LLVMValueRef index;
586       LLVMValueRef scalar_ptr, scalar;
587
588       di = lp_build_const_int32(bld->gallivm, i);
589       if (indexes2)
590          si = lp_build_const_int32(bld->gallivm, i >> 1);
591       else
592          si = di;
593
594       if (indexes2 && (i & 1)) {
595          index = LLVMBuildExtractElement(builder,
596                                          indexes2, si, "");
597       } else {
598          index = LLVMBuildExtractElement(builder,
599                                          indexes, si, "");
600       }
601       scalar_ptr = LLVMBuildGEP(builder, base_ptr,
602                                 &index, 1, "gather_ptr");
603       scalar = LLVMBuildLoad(builder, scalar_ptr, "");
604
605       res = LLVMBuildInsertElement(builder, res, scalar, di, "");
606    }
607
608    if (overflow_mask) {
609       if (indexes2) {
610          res = LLVMBuildBitCast(builder, res, bld_base->dbl_bld.vec_type, "");
611          overflow_mask = LLVMBuildSExt(builder, overflow_mask,
612                                        bld_base->dbl_bld.int_vec_type, "");
613          res = lp_build_select(&bld_base->dbl_bld, overflow_mask,
614                                bld_base->dbl_bld.zero, res);
615       } else
616          res = lp_build_select(bld, overflow_mask, bld->zero, res);
617    }
618
619    return res;
620 }
621
622
623 /**
624  * Scatter/store vector.
625  */
626 static void
627 emit_mask_scatter(struct lp_build_tgsi_soa_context *bld,
628                   LLVMValueRef base_ptr,
629                   LLVMValueRef indexes,
630                   LLVMValueRef values,
631                   struct lp_exec_mask *mask)
632 {
633    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
634    LLVMBuilderRef builder = gallivm->builder;
635    unsigned i;
636    LLVMValueRef pred = mask->has_mask ? mask->exec_mask : NULL;
637
638    /*
639     * Loop over elements of index_vec, store scalar value.
640     */
641    for (i = 0; i < bld->bld_base.base.type.length; i++) {
642       LLVMValueRef ii = lp_build_const_int32(gallivm, i);
643       LLVMValueRef index = LLVMBuildExtractElement(builder, indexes, ii, "");
644       LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr, &index, 1, "scatter_ptr");
645       LLVMValueRef val = LLVMBuildExtractElement(builder, values, ii, "scatter_val");
646       LLVMValueRef scalar_pred = pred ?
647          LLVMBuildExtractElement(builder, pred, ii, "scatter_pred") : NULL;
648
649       if (0)
650          lp_build_printf(gallivm, "scatter %d: val %f at %d %p\n",
651                          ii, val, index, scalar_ptr);
652
653       if (scalar_pred) {
654          LLVMValueRef real_val, dst_val;
655          dst_val = LLVMBuildLoad(builder, scalar_ptr, "");
656          real_val = lp_build_select(&bld->elem_bld, scalar_pred, val, dst_val);
657          LLVMBuildStore(builder, real_val, scalar_ptr);
658       }
659       else {
660          LLVMBuildStore(builder, val, scalar_ptr);
661       }
662    }
663 }
664
665
666 /**
667  * Read the current value of the ADDR register, convert the floats to
668  * ints, add the base index and return the vector of offsets.
669  * The offsets will be used to index into the constant buffer or
670  * temporary register file.
671  */
672 static LLVMValueRef
673 get_indirect_index(struct lp_build_tgsi_soa_context *bld,
674                    unsigned reg_file, unsigned reg_index,
675                    const struct tgsi_ind_register *indirect_reg,
676                    int index_limit)
677 {
678    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
679    struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;
680    /* always use X component of address register */
681    unsigned swizzle = indirect_reg->Swizzle;
682    LLVMValueRef base;
683    LLVMValueRef rel;
684    LLVMValueRef max_index;
685    LLVMValueRef index;
686
687    assert(bld->indirect_files & (1 << reg_file));
688
689    base = lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, reg_index);
690
691    assert(swizzle < 4);
692    switch (indirect_reg->File) {
693    case TGSI_FILE_ADDRESS:
694       rel = LLVMBuildLoad(builder,
695                           bld->addr[indirect_reg->Index][swizzle],
696                           "load addr reg");
697       /* ADDR LLVM values already have LLVM integer type. */
698       break;
699    case TGSI_FILE_TEMPORARY:
700       rel = lp_get_temp_ptr_soa(bld, indirect_reg->Index, swizzle);
701       rel = LLVMBuildLoad(builder, rel, "load temp reg");
702       /* TEMP LLVM values always have LLVM float type, but for indirection, the
703        * value actually stored is expected to be an integer */
704       rel = LLVMBuildBitCast(builder, rel, uint_bld->vec_type, "");
705       break;
706    default:
707       assert(0);
708       rel = uint_bld->zero;
709    }
710
711    index = lp_build_add(uint_bld, base, rel);
712
713    /*
714     * emit_fetch_constant handles constant buffer overflow so this code
715     * is pointless for them.
716     * Furthermore the D3D10 spec in section 6.5 says:
717     * If the constant buffer bound to a slot is larger than the size
718     * declared in the shader for that slot, implementations are allowed
719     * to return incorrect data (not necessarily 0) for indices that are
720     * larger than the declared size but smaller than the buffer size.
721     */
722    if (reg_file != TGSI_FILE_CONSTANT) {
723       assert(index_limit >= 0);
724       max_index = lp_build_const_int_vec(bld->bld_base.base.gallivm,
725                                          uint_bld->type, index_limit);
726
727       assert(!uint_bld->type.sign);
728       index = lp_build_min(uint_bld, index, max_index);
729    }
730
731    return index;
732 }
733
734 static struct lp_build_context *
735 stype_to_fetch(struct lp_build_tgsi_context * bld_base,
736                enum tgsi_opcode_type stype)
737 {
738    struct lp_build_context *bld_fetch;
739
740    switch (stype) {
741    case TGSI_TYPE_FLOAT:
742    case TGSI_TYPE_UNTYPED:
743       bld_fetch = &bld_base->base;
744       break;
745    case TGSI_TYPE_UNSIGNED:
746       bld_fetch = &bld_base->uint_bld;
747       break;
748    case TGSI_TYPE_SIGNED:
749       bld_fetch = &bld_base->int_bld;
750       break;
751    case TGSI_TYPE_DOUBLE:
752       bld_fetch = &bld_base->dbl_bld;
753       break;
754    case TGSI_TYPE_UNSIGNED64:
755       bld_fetch = &bld_base->uint64_bld;
756       break;
757    case TGSI_TYPE_SIGNED64:
758       bld_fetch = &bld_base->int64_bld;
759       break;
760    case TGSI_TYPE_VOID:
761    default:
762       assert(0);
763       bld_fetch = NULL;
764       break;
765    }
766    return bld_fetch;
767 }
768
769 static LLVMValueRef
770 get_soa_array_offsets(struct lp_build_context *uint_bld,
771                       LLVMValueRef indirect_index,
772                       unsigned chan_index,
773                       boolean need_perelement_offset)
774 {
775    struct gallivm_state *gallivm = uint_bld->gallivm;
776    LLVMValueRef chan_vec =
777       lp_build_const_int_vec(uint_bld->gallivm, uint_bld->type, chan_index);
778    LLVMValueRef length_vec =
779       lp_build_const_int_vec(gallivm, uint_bld->type, uint_bld->type.length);
780    LLVMValueRef index_vec;
781
782    /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
783    index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
784    index_vec = lp_build_add(uint_bld, index_vec, chan_vec);
785    index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
786
787    if (need_perelement_offset) {
788       LLVMValueRef pixel_offsets;
789       unsigned i;
790      /* build pixel offset vector: {0, 1, 2, 3, ...} */
791       pixel_offsets = uint_bld->undef;
792       for (i = 0; i < uint_bld->type.length; i++) {
793          LLVMValueRef ii = lp_build_const_int32(gallivm, i);
794          pixel_offsets = LLVMBuildInsertElement(gallivm->builder, pixel_offsets,
795                                                 ii, ii, "");
796       }
797       index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets);
798    }
799    return index_vec;
800 }
801
802 static LLVMValueRef
803 emit_fetch_constant(
804    struct lp_build_tgsi_context * bld_base,
805    const struct tgsi_full_src_register * reg,
806    enum tgsi_opcode_type stype,
807    unsigned swizzle_in)
808 {
809    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
810    struct gallivm_state *gallivm = bld_base->base.gallivm;
811    LLVMBuilderRef builder = gallivm->builder;
812    struct lp_build_context *uint_bld = &bld_base->uint_bld;
813    unsigned dimension = 0;
814    LLVMValueRef consts_ptr;
815    LLVMValueRef num_consts;
816    LLVMValueRef res;
817    unsigned swizzle = swizzle_in & 0xffff;
818
819    /* XXX: Handle fetching xyzw components as a vector */
820    assert(swizzle != ~0u);
821
822    if (reg->Register.Dimension) {
823       assert(!reg->Dimension.Indirect);
824       dimension = reg->Dimension.Index;
825       assert(dimension < LP_MAX_TGSI_CONST_BUFFERS);
826    }
827
828    consts_ptr = bld->consts[dimension];
829    num_consts = bld->consts_sizes[dimension];
830
831    if (reg->Register.Indirect) {
832       LLVMValueRef indirect_index;
833       LLVMValueRef swizzle_vec =
834          lp_build_const_int_vec(gallivm, uint_bld->type, swizzle);
835       LLVMValueRef index_vec;  /* index into the const buffer */
836       LLVMValueRef overflow_mask;
837       LLVMValueRef index_vec2 = NULL;
838
839       indirect_index = get_indirect_index(bld,
840                                           reg->Register.File,
841                                           reg->Register.Index,
842                                           &reg->Indirect,
843                                           bld->bld_base.info->file_max[reg->Register.File]);
844
845       /* All fetches are from the same constant buffer, so
846        * we need to propagate the size to a vector to do a
847        * vector comparison */
848       num_consts = lp_build_broadcast_scalar(uint_bld, num_consts);
849       /* Construct a boolean vector telling us which channels
850        * overflow the bound constant buffer */
851       overflow_mask = lp_build_compare(gallivm, uint_bld->type, PIPE_FUNC_GEQUAL,
852                                        indirect_index, num_consts);
853
854       /* index_vec = indirect_index * 4 + swizzle */
855       index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
856       index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
857
858       if (tgsi_type_is_64bit(stype)) {
859          LLVMValueRef swizzle_vec2;
860          swizzle_vec2 = lp_build_const_int_vec(gallivm, uint_bld->type, swizzle_in >> 16);
861          index_vec2 = lp_build_shl_imm(uint_bld, indirect_index, 2);
862          index_vec2 = lp_build_add(uint_bld, index_vec2, swizzle_vec2);
863       }
864       /* Gather values from the constant buffer */
865       res = build_gather(bld_base, consts_ptr, index_vec, overflow_mask, index_vec2);
866    }
867    else {
868       LLVMValueRef index;  /* index into the const buffer */
869       LLVMValueRef scalar, scalar_ptr;
870       struct lp_build_context *bld_broad = &bld_base->base;
871       index = lp_build_const_int32(gallivm, reg->Register.Index * 4 + swizzle);
872
873       scalar_ptr = LLVMBuildGEP(builder, consts_ptr,
874                                 &index, 1, "");
875
876       if (tgsi_type_is_64bit(stype) && ((swizzle_in >> 16) != swizzle + 1)) {
877
878          LLVMValueRef scalar2, scalar2_ptr;
879          LLVMValueRef shuffles[2];
880          index = lp_build_const_int32(gallivm, reg->Register.Index * 4 + (swizzle_in >> 16));
881
882          scalar2_ptr = LLVMBuildGEP(builder, consts_ptr,
883                                     &index, 1, "");
884
885          scalar = LLVMBuildLoad(builder, scalar_ptr, "");
886          scalar2 = LLVMBuildLoad(builder, scalar2_ptr, "");
887          shuffles[0] = lp_build_const_int32(gallivm, 0);
888          shuffles[1] = lp_build_const_int32(gallivm, 1);
889
890          res = LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), bld_base->base.type.length * 2));
891          res = LLVMBuildInsertElement(builder, res, scalar, shuffles[0], "");
892          res = LLVMBuildInsertElement(builder, res, scalar2, shuffles[1], "");
893       } else {
894         if (stype == TGSI_TYPE_DOUBLE) {
895            LLVMTypeRef dptr_type = LLVMPointerType(LLVMDoubleTypeInContext(gallivm->context), 0);
896            scalar_ptr = LLVMBuildBitCast(builder, scalar_ptr, dptr_type, "");
897            bld_broad = &bld_base->dbl_bld;
898         } else if (stype == TGSI_TYPE_UNSIGNED64) {
899            LLVMTypeRef u64ptr_type = LLVMPointerType(LLVMInt64TypeInContext(gallivm->context), 0);
900            scalar_ptr = LLVMBuildBitCast(builder, scalar_ptr, u64ptr_type, "");
901            bld_broad = &bld_base->uint64_bld;
902         } else if (stype == TGSI_TYPE_SIGNED64) {
903            LLVMTypeRef i64ptr_type = LLVMPointerType(LLVMInt64TypeInContext(gallivm->context), 0);
904            scalar_ptr = LLVMBuildBitCast(builder, scalar_ptr, i64ptr_type, "");
905            bld_broad = &bld_base->int64_bld;
906         }
907         scalar = LLVMBuildLoad(builder, scalar_ptr, "");
908         res = lp_build_broadcast_scalar(bld_broad, scalar);
909       }
910
911    }
912
913    if (stype == TGSI_TYPE_SIGNED || stype == TGSI_TYPE_UNSIGNED || stype == TGSI_TYPE_DOUBLE || stype == TGSI_TYPE_SIGNED64 || stype == TGSI_TYPE_UNSIGNED64) {
914       struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
915       res = LLVMBuildBitCast(builder, res, bld_fetch->vec_type, "");
916    }
917
918    return res;
919 }
920
921 /**
922  * Fetch 64-bit values from two separate channels.
923  * 64-bit values are stored split across two channels, like xy and zw.
924  * This function creates a set of vec_length*2 floats,
925  * extracts the values from the two channels,
926  * puts them in the correct place, then casts to vec_length 64-bits.
927  */
928 static LLVMValueRef
929 emit_fetch_64bit(
930    struct lp_build_tgsi_context * bld_base,
931    enum tgsi_opcode_type stype,
932    LLVMValueRef input,
933    LLVMValueRef input2)
934 {
935    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
936    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
937    LLVMBuilderRef builder = gallivm->builder;
938    LLVMValueRef res;
939    struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
940    int i;
941    LLVMValueRef shuffles[2 * (LP_MAX_VECTOR_WIDTH/32)];
942    int len = bld_base->base.type.length * 2;
943    assert(len <= (2 * (LP_MAX_VECTOR_WIDTH/32)));
944
945    for (i = 0; i < bld_base->base.type.length * 2; i+=2) {
946       shuffles[i] = lp_build_const_int32(gallivm, i / 2);
947       shuffles[i + 1] = lp_build_const_int32(gallivm, i / 2 + bld_base->base.type.length);
948    }
949    res = LLVMBuildShuffleVector(builder, input, input2, LLVMConstVector(shuffles, len), "");
950
951    return LLVMBuildBitCast(builder, res, bld_fetch->vec_type, "");
952 }
953
954 static LLVMValueRef
955 emit_fetch_immediate(
956    struct lp_build_tgsi_context * bld_base,
957    const struct tgsi_full_src_register * reg,
958    enum tgsi_opcode_type stype,
959    unsigned swizzle_in)
960 {
961    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
962    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
963    LLVMBuilderRef builder = gallivm->builder;
964    LLVMValueRef res = NULL;
965    unsigned swizzle = swizzle_in & 0xffff;
966
967    if (bld->use_immediates_array || reg->Register.Indirect) {
968       LLVMValueRef imms_array;
969       LLVMTypeRef fptr_type;
970
971       /* cast imms_array pointer to float* */
972       fptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
973       imms_array = LLVMBuildBitCast(builder, bld->imms_array, fptr_type, "");
974
975       if (reg->Register.Indirect) {
976          LLVMValueRef indirect_index;
977          LLVMValueRef index_vec;  /* index into the immediate register array */
978          LLVMValueRef index_vec2 = NULL;
979          indirect_index = get_indirect_index(bld,
980                                              reg->Register.File,
981                                              reg->Register.Index,
982                                              &reg->Indirect,
983                                              bld->bld_base.info->file_max[reg->Register.File]);
984          /*
985           * Unlike for other reg classes, adding pixel offsets is unnecessary -
986           * immediates are stored as full vectors (FIXME??? - might be better
987           * to store them the same as constants) but all elements are the same
988           * in any case.
989           */
990          index_vec = get_soa_array_offsets(&bld_base->uint_bld,
991                                            indirect_index,
992                                            swizzle,
993                                            FALSE);
994          if (tgsi_type_is_64bit(stype))
995             index_vec2 = get_soa_array_offsets(&bld_base->uint_bld,
996                                               indirect_index,
997                                               swizzle_in >> 16,
998                                               FALSE);
999          /* Gather values from the immediate register array */
1000          res = build_gather(bld_base, imms_array, index_vec, NULL, index_vec2);
1001       } else {
1002          LLVMValueRef gep[2];
1003          gep[0] = lp_build_const_int32(gallivm, 0);
1004          gep[1] = lp_build_const_int32(gallivm, reg->Register.Index * 4 + swizzle);
1005          LLVMValueRef imms_ptr = LLVMBuildGEP(builder,
1006                                               bld->imms_array, gep, 2, "");
1007          res = LLVMBuildLoad(builder, imms_ptr, "");
1008
1009          if (tgsi_type_is_64bit(stype)) {
1010             LLVMValueRef imms_ptr2;
1011             LLVMValueRef res2;
1012             gep[1] = lp_build_const_int32(gallivm,
1013                                           reg->Register.Index * 4 + (swizzle_in >> 16));
1014             imms_ptr2 = LLVMBuildGEP(builder,
1015                                      bld->imms_array, gep, 2, "");
1016             res2 = LLVMBuildLoad(builder, imms_ptr2, "");
1017             res = emit_fetch_64bit(bld_base, stype, res, res2);
1018          }
1019       }
1020    }
1021    else {
1022       res = bld->immediates[reg->Register.Index][swizzle];
1023       if (tgsi_type_is_64bit(stype))
1024          res = emit_fetch_64bit(bld_base, stype, res, bld->immediates[reg->Register.Index][swizzle_in >> 16]);
1025    }
1026
1027    if (stype == TGSI_TYPE_SIGNED || stype == TGSI_TYPE_UNSIGNED || tgsi_type_is_64bit(stype)) {
1028       struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
1029       res = LLVMBuildBitCast(builder, res, bld_fetch->vec_type, "");
1030    }
1031    return res;
1032 }
1033
1034 static LLVMValueRef
1035 emit_fetch_input(
1036    struct lp_build_tgsi_context * bld_base,
1037    const struct tgsi_full_src_register * reg,
1038    enum tgsi_opcode_type stype,
1039    unsigned swizzle_in)
1040 {
1041    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1042    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1043    LLVMBuilderRef builder = gallivm->builder;
1044    LLVMValueRef res;
1045    unsigned swizzle = swizzle_in & 0xffff;
1046
1047    if (reg->Register.Indirect) {
1048       LLVMValueRef indirect_index;
1049       LLVMValueRef index_vec;  /* index into the input reg array */
1050       LLVMValueRef index_vec2 = NULL;
1051       LLVMValueRef inputs_array;
1052       LLVMTypeRef fptr_type;
1053
1054       indirect_index = get_indirect_index(bld,
1055                                           reg->Register.File,
1056                                           reg->Register.Index,
1057                                           &reg->Indirect,
1058                                           bld->bld_base.info->file_max[reg->Register.File]);
1059
1060       index_vec = get_soa_array_offsets(&bld_base->uint_bld,
1061                                         indirect_index,
1062                                         swizzle,
1063                                         TRUE);
1064       if (tgsi_type_is_64bit(stype)) {
1065          index_vec2 = get_soa_array_offsets(&bld_base->uint_bld,
1066                                            indirect_index,
1067                                            swizzle_in >> 16,
1068                                            TRUE);
1069       }
1070       /* cast inputs_array pointer to float* */
1071       fptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1072       inputs_array = LLVMBuildBitCast(builder, bld->inputs_array, fptr_type, "");
1073
1074       /* Gather values from the input register array */
1075       res = build_gather(bld_base, inputs_array, index_vec, NULL, index_vec2);
1076    } else {
1077       if (bld->indirect_files & (1 << TGSI_FILE_INPUT)) {
1078          LLVMValueRef lindex = lp_build_const_int32(gallivm,
1079                                         reg->Register.Index * 4 + swizzle);
1080          LLVMValueRef input_ptr = LLVMBuildGEP(builder,
1081                                                bld->inputs_array, &lindex, 1, "");
1082
1083          res = LLVMBuildLoad(builder, input_ptr, "");
1084          if (tgsi_type_is_64bit(stype)) {
1085             LLVMValueRef lindex1;
1086             LLVMValueRef input_ptr2;
1087             LLVMValueRef res2;
1088
1089             lindex1 = lp_build_const_int32(gallivm,
1090                                            reg->Register.Index * 4 + (swizzle_in >> 16));
1091             input_ptr2 = LLVMBuildGEP(builder,
1092                                       bld->inputs_array, &lindex1, 1, "");
1093             res2 = LLVMBuildLoad(builder, input_ptr2, "");
1094             res = emit_fetch_64bit(bld_base, stype, res, res2);
1095          }
1096       }
1097       else {
1098          res = bld->inputs[reg->Register.Index][swizzle];
1099          if (tgsi_type_is_64bit(stype))
1100             res = emit_fetch_64bit(bld_base, stype, res, bld->inputs[reg->Register.Index][swizzle_in >> 16]);
1101       }
1102    }
1103
1104    assert(res);
1105
1106    if (stype == TGSI_TYPE_SIGNED || stype == TGSI_TYPE_UNSIGNED || tgsi_type_is_64bit(stype)) {
1107       struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
1108       res = LLVMBuildBitCast(builder, res, bld_fetch->vec_type, "");
1109    }
1110
1111    return res;
1112 }
1113
1114
1115 static LLVMValueRef
1116 emit_fetch_gs_input(
1117    struct lp_build_tgsi_context * bld_base,
1118    const struct tgsi_full_src_register * reg,
1119    enum tgsi_opcode_type stype,
1120    unsigned swizzle_in)
1121 {
1122    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1123    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1124    const struct tgsi_shader_info *info = bld->bld_base.info;
1125    LLVMBuilderRef builder = gallivm->builder;
1126    LLVMValueRef attrib_index = NULL;
1127    LLVMValueRef vertex_index = NULL;
1128    unsigned swizzle = swizzle_in & 0xffff;
1129    LLVMValueRef swizzle_index = lp_build_const_int32(gallivm, swizzle);
1130    LLVMValueRef res;
1131
1132    if (info->input_semantic_name[reg->Register.Index] == TGSI_SEMANTIC_PRIMID) {
1133       /* This is really a system value not a regular input */
1134       assert(!reg->Register.Indirect);
1135       assert(!reg->Dimension.Indirect);
1136       res = bld->system_values.prim_id;
1137       if (stype != TGSI_TYPE_UNSIGNED && stype != TGSI_TYPE_SIGNED) {
1138          res = LLVMBuildBitCast(builder, res, bld_base->base.vec_type, "");
1139       }
1140       return res;
1141    }
1142
1143    if (reg->Register.Indirect) {
1144       /*
1145        * XXX: this is possibly not quite the right value, since file_max may be
1146        * larger than the max attrib index, due to it being the max of declared
1147        * inputs AND the max vertices per prim (which is 6 for tri adj).
1148        * It should however be safe to use (since we always allocate
1149        * PIPE_MAX_SHADER_INPUTS (80) for it, which is overallocated quite a bit).
1150        */
1151       int index_limit = info->file_max[reg->Register.File];
1152       attrib_index = get_indirect_index(bld,
1153                                         reg->Register.File,
1154                                         reg->Register.Index,
1155                                         &reg->Indirect,
1156                                         index_limit);
1157    } else {
1158       attrib_index = lp_build_const_int32(gallivm, reg->Register.Index);
1159    }
1160
1161    if (reg->Dimension.Indirect) {
1162       /*
1163        * A fixed 6 should do as well (which is what we allocate).
1164        */
1165       int index_limit = u_vertices_per_prim(info->properties[TGSI_PROPERTY_GS_INPUT_PRIM]);
1166       vertex_index = get_indirect_index(bld,
1167                                         reg->Register.File,
1168                                         reg->Dimension.Index,
1169                                         &reg->DimIndirect,
1170                                         index_limit);
1171    } else {
1172       vertex_index = lp_build_const_int32(gallivm, reg->Dimension.Index);
1173    }
1174
1175    res = bld->gs_iface->fetch_input(bld->gs_iface, &bld_base->base,
1176                                     reg->Dimension.Indirect,
1177                                     vertex_index,
1178                                     reg->Register.Indirect,
1179                                     attrib_index,
1180                                     swizzle_index);
1181
1182    assert(res);
1183    if (tgsi_type_is_64bit(stype)) {
1184       LLVMValueRef swizzle_index = lp_build_const_int32(gallivm, swizzle_in >> 16);
1185       LLVMValueRef res2;
1186       res2 = bld->gs_iface->fetch_input(bld->gs_iface, &bld_base->base,
1187                                         reg->Dimension.Indirect,
1188                                         vertex_index,
1189                                         reg->Register.Indirect,
1190                                         attrib_index,
1191                                         swizzle_index);
1192       assert(res2);
1193       res = emit_fetch_64bit(bld_base, stype, res, res2);
1194    } else if (stype == TGSI_TYPE_UNSIGNED) {
1195       res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
1196    } else if (stype == TGSI_TYPE_SIGNED) {
1197       res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
1198    }
1199
1200    return res;
1201 }
1202
1203 static LLVMValueRef
1204 emit_fetch_tcs_input(
1205    struct lp_build_tgsi_context * bld_base,
1206    const struct tgsi_full_src_register * reg,
1207    enum tgsi_opcode_type stype,
1208    unsigned swizzle_in)
1209 {
1210    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1211    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1212    const struct tgsi_shader_info *info = bld->bld_base.info;
1213    LLVMBuilderRef builder = gallivm->builder;
1214    LLVMValueRef attrib_index = NULL;
1215    LLVMValueRef vertex_index = NULL;
1216    unsigned swizzle = swizzle_in & 0xffff;
1217    LLVMValueRef swizzle_index = lp_build_const_int32(gallivm, swizzle);
1218    LLVMValueRef res;
1219
1220    if (info->input_semantic_name[reg->Register.Index] == TGSI_SEMANTIC_PRIMID) {
1221       /* This is really a system value not a regular input */
1222       assert(!reg->Register.Indirect);
1223       assert(!reg->Dimension.Indirect);
1224       res = bld->system_values.prim_id;
1225       if (stype != TGSI_TYPE_UNSIGNED && stype != TGSI_TYPE_SIGNED) {
1226          res = LLVMBuildBitCast(builder, res, bld_base->base.vec_type, "");
1227       }
1228       return res;
1229    }
1230
1231    if (reg->Register.Indirect) {
1232       int index_limit = info->file_max[reg->Register.File];
1233       attrib_index = get_indirect_index(bld,
1234                                         reg->Register.File,
1235                                         reg->Register.Index,
1236                                         &reg->Indirect,
1237                                         index_limit);
1238    } else {
1239       attrib_index = lp_build_const_int32(gallivm, reg->Register.Index);
1240    }
1241
1242    if (reg->Dimension.Indirect) {
1243       vertex_index = get_indirect_index(bld,
1244                                         reg->Register.File,
1245                                         reg->Dimension.Index,
1246                                         &reg->DimIndirect,
1247                                         PIPE_MAX_SHADER_INPUTS);
1248    } else {
1249       vertex_index = lp_build_const_int32(gallivm, reg->Dimension.Index);
1250    }
1251
1252    // TCS can read from its own outputs
1253    if (reg->Register.File == TGSI_FILE_OUTPUT) {
1254       res = bld->tcs_iface->emit_fetch_output(bld->tcs_iface, (struct lp_build_context*)bld_base,
1255                                               reg->Dimension.Indirect,
1256                                               vertex_index,
1257                                               reg->Register.Indirect,
1258                                               attrib_index,
1259                                               FALSE,
1260                                               swizzle_index,
1261                                               bld_base->info->output_semantic_name[reg->Register.Index]);
1262    } else {
1263       res = bld->tcs_iface->emit_fetch_input(bld->tcs_iface, (struct lp_build_context*)bld_base,
1264                                              reg->Dimension.Indirect,
1265                                              vertex_index,
1266                                              reg->Register.Indirect,
1267                                              attrib_index,
1268                                              FALSE,
1269                                              swizzle_index);
1270    }
1271
1272
1273    assert(res);
1274    if (tgsi_type_is_64bit(stype)) {
1275       LLVMValueRef swizzle_index = lp_build_const_int32(gallivm, swizzle_in >> 16);
1276       LLVMValueRef res2;
1277       if (reg->Register.File == TGSI_FILE_OUTPUT) {
1278          res2 = bld->tcs_iface->emit_fetch_output(bld->tcs_iface, (struct lp_build_context*)bld_base,
1279                                                   reg->Dimension.Indirect,
1280                                                   vertex_index,
1281                                                   reg->Register.Indirect,
1282                                                   attrib_index,
1283                                                   FALSE,
1284                                                   swizzle_index,
1285                                                   bld_base->info->output_semantic_name[reg->Register.Index]);
1286       } else {
1287          res2 = bld->tcs_iface->emit_fetch_input(bld->tcs_iface, (struct lp_build_context*)bld_base,
1288                                                  reg->Dimension.Indirect,
1289                                                  vertex_index,
1290                                                  reg->Register.Indirect,
1291                                                  attrib_index,
1292                                                  FALSE,
1293                                                  swizzle_index);
1294       }
1295       assert(res2);
1296       res = emit_fetch_64bit(bld_base, stype, res, res2);
1297    } else if (stype == TGSI_TYPE_UNSIGNED) {
1298       res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
1299    } else if (stype == TGSI_TYPE_SIGNED) {
1300       res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
1301    }
1302
1303    return res;
1304 }
1305
1306 static LLVMValueRef
1307 emit_fetch_tes_input(
1308    struct lp_build_tgsi_context * bld_base,
1309    const struct tgsi_full_src_register * reg,
1310    enum tgsi_opcode_type stype,
1311    unsigned swizzle_in)
1312 {
1313    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1314    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1315    const struct tgsi_shader_info *info = bld->bld_base.info;
1316    LLVMBuilderRef builder = gallivm->builder;
1317    LLVMValueRef attrib_index = NULL;
1318    LLVMValueRef vertex_index = NULL;
1319    unsigned swizzle = swizzle_in & 0xffff;
1320    LLVMValueRef swizzle_index = lp_build_const_int32(gallivm, swizzle);
1321    LLVMValueRef res;
1322
1323    if (info->input_semantic_name[reg->Register.Index] == TGSI_SEMANTIC_PRIMID) {
1324       /* This is really a system value not a regular input */
1325       assert(!reg->Register.Indirect);
1326       assert(!reg->Dimension.Indirect);
1327       res = bld->system_values.prim_id;
1328       if (stype != TGSI_TYPE_UNSIGNED && stype != TGSI_TYPE_SIGNED) {
1329          res = LLVMBuildBitCast(builder, res, bld_base->base.vec_type, "");
1330       }
1331       return res;
1332    }
1333
1334    if (reg->Register.Indirect) {
1335       int index_limit = info->file_max[reg->Register.File];
1336       attrib_index = get_indirect_index(bld,
1337                                         reg->Register.File,
1338                                         reg->Register.Index,
1339                                         &reg->Indirect,
1340                                         index_limit);
1341    } else {
1342       attrib_index = lp_build_const_int32(gallivm, reg->Register.Index);
1343    }
1344
1345    if (reg->Dimension.Indirect) {
1346       vertex_index = get_indirect_index(bld,
1347                                         reg->Register.File,
1348                                         reg->Dimension.Index,
1349                                         &reg->DimIndirect,
1350                                         PIPE_MAX_SHADER_INPUTS);
1351    } else {
1352       vertex_index = lp_build_const_int32(gallivm, reg->Dimension.Index);
1353    }
1354
1355    if (info->input_semantic_name[reg->Register.Index] == TGSI_SEMANTIC_PATCH) {
1356       res = bld->tes_iface->fetch_patch_input(bld->tes_iface, (struct lp_build_context*)bld_base,
1357                                      reg->Register.Indirect,
1358                                      attrib_index,
1359                                      swizzle_index);
1360    } else {
1361       res = bld->tes_iface->fetch_vertex_input(bld->tes_iface, (struct lp_build_context*)bld_base,
1362                                        reg->Dimension.Indirect,
1363                                        vertex_index,
1364                                        reg->Register.Indirect,
1365                                        attrib_index,
1366                                        FALSE,
1367                                        swizzle_index);
1368    }
1369
1370    assert(res);
1371    if (tgsi_type_is_64bit(stype)) {
1372       LLVMValueRef swizzle_index = lp_build_const_int32(gallivm, swizzle_in >> 16);
1373       LLVMValueRef res2;
1374       if (info->input_semantic_name[reg->Register.Index] == TGSI_SEMANTIC_PATCH) {
1375          res2 = bld->tes_iface->fetch_patch_input(bld->tes_iface, (struct lp_build_context*)bld_base,
1376                                     reg->Register.Indirect,
1377                                     attrib_index,
1378                                     swizzle_index);
1379       }
1380       else {
1381          res2 = bld->tes_iface->fetch_vertex_input(bld->tes_iface, (struct lp_build_context*)bld_base,
1382                                              reg->Dimension.Indirect,
1383                                              vertex_index,
1384                                              reg->Register.Indirect,
1385                                              attrib_index,
1386                                              FALSE,
1387                                              swizzle_index);
1388       }
1389       assert(res2);
1390       res = emit_fetch_64bit(bld_base, stype, res, res2);
1391    } else if (stype == TGSI_TYPE_UNSIGNED) {
1392       res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
1393    } else if (stype == TGSI_TYPE_SIGNED) {
1394       res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
1395    }
1396
1397    return res;
1398 }
1399
1400
1401
1402 static LLVMValueRef
1403 emit_fetch_temporary(
1404    struct lp_build_tgsi_context * bld_base,
1405    const struct tgsi_full_src_register * reg,
1406    enum tgsi_opcode_type stype,
1407    unsigned swizzle_in)
1408 {
1409    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1410    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1411    LLVMBuilderRef builder = gallivm->builder;
1412    LLVMValueRef res;
1413    unsigned swizzle = swizzle_in & 0xffff;
1414
1415    if (reg->Register.Indirect) {
1416       LLVMValueRef indirect_index;
1417       LLVMValueRef index_vec, index_vec2 = NULL;  /* index into the temp reg array */
1418       LLVMValueRef temps_array;
1419       LLVMTypeRef fptr_type;
1420
1421       indirect_index = get_indirect_index(bld,
1422                                           reg->Register.File,
1423                                           reg->Register.Index,
1424                                           &reg->Indirect,
1425                                           bld->bld_base.info->file_max[reg->Register.File]);
1426
1427       index_vec = get_soa_array_offsets(&bld_base->uint_bld,
1428                                         indirect_index,
1429                                         swizzle,
1430                                         TRUE);
1431       if (tgsi_type_is_64bit(stype)) {
1432                index_vec2 = get_soa_array_offsets(&bld_base->uint_bld,
1433                                                   indirect_index,
1434                                                   swizzle_in >> 16,
1435                                                   TRUE);
1436       }
1437
1438       /* cast temps_array pointer to float* */
1439       fptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1440       temps_array = LLVMBuildBitCast(builder, bld->temps_array, fptr_type, "");
1441
1442       /* Gather values from the temporary register array */
1443       res = build_gather(bld_base, temps_array, index_vec, NULL, index_vec2);
1444    }
1445    else {
1446       LLVMValueRef temp_ptr;
1447       temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, swizzle);
1448       res = LLVMBuildLoad(builder, temp_ptr, "");
1449
1450       if (tgsi_type_is_64bit(stype)) {
1451          LLVMValueRef temp_ptr2, res2;
1452
1453          temp_ptr2 = lp_get_temp_ptr_soa(bld, reg->Register.Index, swizzle_in >> 16);
1454          res2 = LLVMBuildLoad(builder, temp_ptr2, "");
1455          res = emit_fetch_64bit(bld_base, stype, res, res2);
1456       }
1457    }
1458
1459    if (stype == TGSI_TYPE_SIGNED ||
1460        stype == TGSI_TYPE_UNSIGNED ||
1461        stype == TGSI_TYPE_DOUBLE ||
1462        stype == TGSI_TYPE_SIGNED64 ||
1463        stype == TGSI_TYPE_UNSIGNED64) {
1464       struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
1465       res = LLVMBuildBitCast(builder, res, bld_fetch->vec_type, "");
1466    }
1467
1468    return res;
1469 }
1470
1471 static LLVMValueRef
1472 emit_fetch_system_value(
1473    struct lp_build_tgsi_context * bld_base,
1474    const struct tgsi_full_src_register * reg,
1475    enum tgsi_opcode_type stype,
1476    unsigned swizzle_in)
1477 {
1478    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1479    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1480    const struct tgsi_shader_info *info = bld->bld_base.info;
1481    LLVMBuilderRef builder = gallivm->builder;
1482    LLVMValueRef res;
1483    enum tgsi_opcode_type atype; // Actual type of the value
1484    unsigned swizzle = swizzle_in & 0xffff;
1485
1486    assert(!reg->Register.Indirect);
1487
1488    switch (info->system_value_semantic_name[reg->Register.Index]) {
1489    case TGSI_SEMANTIC_INSTANCEID:
1490       res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.instance_id);
1491       atype = TGSI_TYPE_UNSIGNED;
1492       break;
1493
1494    case TGSI_SEMANTIC_VERTEXID:
1495       res = bld->system_values.vertex_id;
1496       atype = TGSI_TYPE_UNSIGNED;
1497       break;
1498
1499    case TGSI_SEMANTIC_VERTEXID_NOBASE:
1500       res = bld->system_values.vertex_id_nobase;
1501       atype = TGSI_TYPE_UNSIGNED;
1502       break;
1503
1504    case TGSI_SEMANTIC_BASEVERTEX:
1505       res = bld->system_values.basevertex;
1506       atype = TGSI_TYPE_UNSIGNED;
1507       break;
1508
1509    case TGSI_SEMANTIC_BASEINSTANCE:
1510       res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.base_instance);
1511       atype = TGSI_TYPE_UNSIGNED;
1512       break;
1513
1514    case TGSI_SEMANTIC_PRIMID:
1515       res = bld->system_values.prim_id;
1516       atype = TGSI_TYPE_UNSIGNED;
1517       break;
1518
1519    case TGSI_SEMANTIC_INVOCATIONID:
1520       if (info->processor == PIPE_SHADER_TESS_CTRL)
1521          res = bld->system_values.invocation_id;
1522       else
1523          res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.invocation_id);
1524       atype = TGSI_TYPE_UNSIGNED;
1525       break;
1526
1527    case TGSI_SEMANTIC_HELPER_INVOCATION:
1528       res = LLVMBuildNot(gallivm->builder, lp_build_mask_value(bld->mask), "");
1529       atype = TGSI_TYPE_UNSIGNED;
1530       break;
1531
1532    case TGSI_SEMANTIC_THREAD_ID:
1533       res = LLVMBuildExtractValue(gallivm->builder, bld->system_values.thread_id, swizzle, "");
1534       atype = TGSI_TYPE_UNSIGNED;
1535       break;
1536
1537    case TGSI_SEMANTIC_BLOCK_ID:
1538       res = lp_build_extract_broadcast(gallivm, lp_type_int_vec(32, 96), bld_base->uint_bld.type, bld->system_values.block_id, lp_build_const_int32(gallivm, swizzle));
1539       atype = TGSI_TYPE_UNSIGNED;
1540       break;
1541
1542    case TGSI_SEMANTIC_GRID_SIZE:
1543       res = lp_build_extract_broadcast(gallivm, lp_type_int_vec(32, 96), bld_base->uint_bld.type, bld->system_values.grid_size, lp_build_const_int32(gallivm, swizzle));
1544       atype = TGSI_TYPE_UNSIGNED;
1545       break;
1546
1547    case TGSI_SEMANTIC_TESSCOORD:
1548       {
1549          LLVMValueRef index[] = { lp_build_const_int32(gallivm, 0), lp_build_const_int32(gallivm, swizzle_in) };
1550          LLVMValueRef array_indexed = LLVMBuildGEP(gallivm->builder, bld->system_values.tess_coord, index, 2, "tess_coord_array_indexed");
1551          res = LLVMBuildLoad(builder, array_indexed, "tess_coord");
1552       }
1553       atype = TGSI_TYPE_FLOAT;
1554       break;
1555
1556    case TGSI_SEMANTIC_FACE:
1557       res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.front_facing);
1558       atype = TGSI_TYPE_UNSIGNED;
1559       break;
1560
1561   case TGSI_SEMANTIC_DRAWID:
1562       res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.draw_id);
1563       atype = TGSI_TYPE_UNSIGNED;
1564       break;
1565
1566   case TGSI_SEMANTIC_SAMPLEID:
1567       res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.sample_id);
1568       atype = TGSI_TYPE_UNSIGNED;
1569       break;
1570
1571    case TGSI_SEMANTIC_TESSOUTER:
1572       res = lp_build_extract_broadcast(gallivm, lp_type_float_vec(32, 128), bld_base->base.type,
1573                                        bld->system_values.tess_outer,
1574                                        lp_build_const_int32(gallivm, swizzle_in));
1575       atype = TGSI_TYPE_FLOAT;
1576       break;
1577
1578    case TGSI_SEMANTIC_TESSINNER:
1579       res = lp_build_extract_broadcast(gallivm, lp_type_float_vec(32, 128), bld_base->base.type,
1580                                        bld->system_values.tess_inner,
1581                                        lp_build_const_int32(gallivm, swizzle_in));
1582       atype = TGSI_TYPE_FLOAT;
1583       break;
1584
1585    case TGSI_SEMANTIC_VERTICESIN:
1586       res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.vertices_in);
1587       atype = TGSI_TYPE_UNSIGNED;
1588       break;
1589
1590    default:
1591       assert(!"unexpected semantic in emit_fetch_system_value");
1592       res = bld_base->base.zero;
1593       atype = TGSI_TYPE_FLOAT;
1594       break;
1595    }
1596
1597    if (atype != stype) {
1598       if (stype == TGSI_TYPE_FLOAT) {
1599          res = LLVMBuildBitCast(builder, res, bld_base->base.vec_type, "");
1600       } else if (stype == TGSI_TYPE_UNSIGNED) {
1601          res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
1602       } else if (stype == TGSI_TYPE_SIGNED) {
1603          res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
1604       }
1605    }
1606
1607    return res;
1608 }
1609
1610 /**
1611  * Register fetch with derivatives.
1612  */
1613 static void
1614 emit_fetch_deriv(
1615    struct lp_build_tgsi_soa_context *bld,
1616    LLVMValueRef src,
1617    LLVMValueRef *res,
1618    LLVMValueRef *ddx,
1619    LLVMValueRef *ddy)
1620 {
1621    if (res)
1622       *res = src;
1623
1624    /* TODO: use interpolation coeffs for inputs */
1625
1626    if (ddx)
1627       *ddx = lp_build_ddx(&bld->bld_base.base, src);
1628
1629    if (ddy)
1630       *ddy = lp_build_ddy(&bld->bld_base.base, src);
1631 }
1632
1633 /**
1634  * store an array of vec-length 64-bit into two arrays of vec_length floats
1635  * i.e.
1636  * value is d0, d1, d2, d3 etc.
1637  * each 64-bit has high and low pieces x, y
1638  * so gets stored into the separate channels as:
1639  * chan_ptr = d0.x, d1.x, d2.x, d3.x
1640  * chan_ptr2 = d0.y, d1.y, d2.y, d3.y
1641  */
1642 static void
1643 emit_store_64bit_chan(struct lp_build_tgsi_context *bld_base,
1644                       LLVMValueRef chan_ptr, LLVMValueRef chan_ptr2,
1645                       LLVMValueRef value)
1646 {
1647    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1648    struct gallivm_state *gallivm = bld_base->base.gallivm;
1649    LLVMBuilderRef builder = gallivm->builder;
1650    struct lp_build_context *float_bld = &bld_base->base;
1651    unsigned i;
1652    LLVMValueRef temp, temp2;
1653    LLVMValueRef shuffles[LP_MAX_VECTOR_WIDTH/32];
1654    LLVMValueRef shuffles2[LP_MAX_VECTOR_WIDTH/32];
1655
1656    for (i = 0; i < bld_base->base.type.length; i++) {
1657       shuffles[i] = lp_build_const_int32(gallivm, i * 2);
1658       shuffles2[i] = lp_build_const_int32(gallivm, (i * 2) + 1);
1659    }
1660
1661    temp = LLVMBuildShuffleVector(builder, value,
1662                                  LLVMGetUndef(LLVMTypeOf(value)),
1663                                  LLVMConstVector(shuffles,
1664                                                  bld_base->base.type.length),
1665                                  "");
1666    temp2 = LLVMBuildShuffleVector(builder, value,
1667                                   LLVMGetUndef(LLVMTypeOf(value)),
1668                                   LLVMConstVector(shuffles2,
1669                                                   bld_base->base.type.length),
1670                                   "");
1671
1672    lp_exec_mask_store(&bld->exec_mask, float_bld, temp, chan_ptr);
1673    lp_exec_mask_store(&bld->exec_mask, float_bld, temp2, chan_ptr2);
1674 }
1675
1676 static void
1677 emit_store_output(struct lp_build_tgsi_context *bld_base,
1678                   enum tgsi_opcode_type dtype,
1679                   const struct tgsi_full_dst_register *reg,
1680                   unsigned index,
1681                   unsigned chan_index,
1682                   LLVMValueRef indirect_index,
1683                   LLVMValueRef value)
1684 {
1685    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1686    struct gallivm_state *gallivm = bld_base->base.gallivm;
1687    LLVMBuilderRef builder = gallivm->builder;
1688    struct lp_build_context *float_bld = &bld_base->base;
1689
1690    /* Outputs are always stored as floats */
1691    value = LLVMBuildBitCast(builder, value, float_bld->vec_type, "");
1692
1693    if (reg->Register.Indirect) {
1694       LLVMValueRef index_vec;  /* indexes into the output registers */
1695       LLVMValueRef outputs_array;
1696       LLVMTypeRef fptr_type;
1697
1698       index_vec = get_soa_array_offsets(&bld_base->uint_bld,
1699                                           indirect_index,
1700                                           chan_index,
1701                                           TRUE);
1702
1703       fptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1704       outputs_array = LLVMBuildBitCast(builder, bld->outputs_array, fptr_type, "");
1705
1706       /* Scatter store values into output registers */
1707       emit_mask_scatter(bld, outputs_array, index_vec, value,
1708                         &bld->exec_mask);
1709    }
1710    else {
1711       assert(LLVMTypeOf(value) == float_bld->vec_type);
1712       LLVMValueRef out_ptr = lp_get_output_ptr(bld, reg->Register.Index,
1713                                                 chan_index);
1714
1715       if (tgsi_type_is_64bit(dtype)) {
1716          LLVMValueRef out_ptr2 = lp_get_output_ptr(bld, reg->Register.Index,
1717                                                    chan_index + 1);
1718          emit_store_64bit_chan(bld_base, out_ptr, out_ptr2,
1719                                  value);
1720       } else
1721          lp_exec_mask_store(&bld->exec_mask, float_bld, value, out_ptr);
1722    }
1723 }
1724
1725 static void
1726 emit_store_tcs_output(struct lp_build_tgsi_context *bld_base,
1727                       enum tgsi_opcode_type dtype,
1728                       const struct tgsi_full_dst_register *reg,
1729                       unsigned index,
1730                       unsigned chan_index,
1731                       LLVMValueRef indirect_index,
1732                       LLVMValueRef value)
1733 {
1734    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1735    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1736    const struct tgsi_shader_info *info = bld->bld_base.info;
1737    LLVMValueRef attrib_index = NULL;
1738    LLVMValueRef vertex_index = NULL;
1739    LLVMValueRef channel_index = NULL;
1740
1741    if (reg->Register.Indirect) {
1742       /*
1743        * XXX: this is possibly not quite the right value, since file_max may be
1744        * larger than the max attrib index, due to it being the max of declared
1745        * inputs AND the max vertices per prim (which is 6 for tri adj).
1746        * It should however be safe to use (since we always allocate
1747        * PIPE_MAX_SHADER_INPUTS (80) for it, which is overallocated quite a bit).
1748        */
1749       int index_limit = info->file_max[reg->Register.File];
1750       attrib_index = get_indirect_index(bld,
1751                                         reg->Register.File,
1752                                         reg->Register.Index,
1753                                         &reg->Indirect,
1754                                         index_limit);
1755    } else {
1756       attrib_index = lp_build_const_int32(gallivm, reg->Register.Index);
1757    }
1758
1759    if (reg->Dimension.Indirect) {
1760       vertex_index = get_indirect_index(bld,
1761                                         reg->Register.File,
1762                                         reg->Dimension.Index,
1763                                         &reg->DimIndirect,
1764                                         PIPE_MAX_SHADER_OUTPUTS);
1765    } else {
1766       vertex_index = lp_build_const_int32(gallivm, reg->Dimension.Index);
1767    }
1768
1769    channel_index = lp_build_const_int32(gallivm, chan_index);
1770
1771    assert(bld->tcs_iface->emit_store_output);
1772    bld->tcs_iface->emit_store_output(bld->tcs_iface, (struct lp_build_context*)bld_base,
1773                                           bld_base->info->output_semantic_name[reg->Register.Index],
1774                                           reg->Dimension.Indirect,
1775                                           vertex_index,
1776                                           reg->Register.Indirect,
1777                                           attrib_index,
1778                                           false,
1779                                           channel_index,
1780                                           value,
1781                                           mask_vec(bld_base));
1782 }
1783
1784 static void
1785 emit_store_temp(struct lp_build_tgsi_context *bld_base,
1786                   enum tgsi_opcode_type dtype,
1787                   const struct tgsi_full_dst_register *reg,
1788                   unsigned index,
1789                   unsigned chan_index,
1790                   LLVMValueRef indirect_index,
1791                   LLVMValueRef value)
1792 {
1793    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1794    struct gallivm_state *gallivm = bld_base->base.gallivm;
1795    LLVMBuilderRef builder = gallivm->builder;
1796    struct lp_build_context *float_bld = &bld_base->base;
1797
1798    /* Temporaries are always stored as floats */
1799    if (!tgsi_type_is_64bit(dtype))
1800       value = LLVMBuildBitCast(builder, value, float_bld->vec_type, "");
1801    else
1802       value = LLVMBuildBitCast(builder, value,  LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), bld_base->base.type.length * 2), "");
1803
1804    if (reg->Register.Indirect) {
1805       LLVMValueRef index_vec;  /* indexes into the temp registers */
1806       LLVMValueRef temps_array;
1807       LLVMTypeRef fptr_type;
1808
1809       index_vec = get_soa_array_offsets(&bld_base->uint_bld,
1810                                           indirect_index,
1811                                           chan_index,
1812                                           TRUE);
1813
1814       fptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1815       temps_array = LLVMBuildBitCast(builder, bld->temps_array, fptr_type, "");
1816
1817       /* Scatter store values into temp registers */
1818       emit_mask_scatter(bld, temps_array, index_vec, value,
1819                         &bld->exec_mask);
1820    }
1821    else {
1822       LLVMValueRef temp_ptr;
1823       temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, chan_index);
1824
1825       if (tgsi_type_is_64bit(dtype)) {
1826          LLVMValueRef temp_ptr2 = lp_get_temp_ptr_soa(bld,
1827                                                       reg->Register.Index,
1828                                                       chan_index + 1);
1829          emit_store_64bit_chan(bld_base, temp_ptr, temp_ptr2,
1830                                  value);
1831       }
1832       else
1833          lp_exec_mask_store(&bld->exec_mask, float_bld, value, temp_ptr);
1834    }
1835 }
1836
1837 static void
1838 emit_store_address(struct lp_build_tgsi_context *bld_base,
1839                    enum tgsi_opcode_type dtype,
1840                    const struct tgsi_full_dst_register *reg,
1841                    unsigned index,
1842                    unsigned chan_index,
1843                    LLVMValueRef indirect_index,
1844                    LLVMValueRef value)
1845 {
1846    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1847    struct gallivm_state *gallivm = bld_base->base.gallivm;
1848    LLVMBuilderRef builder = gallivm->builder;
1849    struct lp_build_context *int_bld = &bld_base->int_bld;
1850
1851    assert(dtype == TGSI_TYPE_SIGNED);
1852    assert(LLVMTypeOf(value) == int_bld->vec_type);
1853    value = LLVMBuildBitCast(builder, value, int_bld->vec_type, "");
1854    lp_exec_mask_store(&bld->exec_mask, int_bld, value,
1855                         bld->addr[reg->Register.Index][chan_index]);
1856 }
1857
1858 /**
1859  * Register store.
1860  */
1861 static void
1862 emit_store_chan(
1863    struct lp_build_tgsi_context *bld_base,
1864    const struct tgsi_full_instruction *inst,
1865    unsigned index,
1866    unsigned chan_index,
1867    LLVMValueRef value)
1868 {
1869    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1870    struct gallivm_state *gallivm = bld_base->base.gallivm;
1871    LLVMBuilderRef builder = gallivm->builder;
1872    const struct tgsi_full_dst_register *reg = &inst->Dst[index];
1873    struct lp_build_context *float_bld = &bld_base->base;
1874    LLVMValueRef indirect_index = NULL;
1875    enum tgsi_opcode_type dtype = tgsi_opcode_infer_dst_type(inst->Instruction.Opcode, index);
1876
1877    /*
1878     * Apply saturation.
1879     *
1880     * It is always assumed to be float.
1881     */
1882    if (inst->Instruction.Saturate) {
1883       assert(dtype == TGSI_TYPE_FLOAT ||
1884              dtype == TGSI_TYPE_UNTYPED);
1885       value = LLVMBuildBitCast(builder, value, float_bld->vec_type, "");
1886       value = lp_build_clamp_zero_one_nanzero(float_bld, value);
1887    }
1888
1889    if (reg->Register.Indirect) {
1890       /*
1891        * Currently the mesa/st doesn't generate indirect stores
1892        * to 64-bit values, it normally uses MOV to do indirect stores.
1893        */
1894       assert(!tgsi_type_is_64bit(dtype));
1895       indirect_index = get_indirect_index(bld,
1896                                           reg->Register.File,
1897                                           reg->Register.Index,
1898                                           &reg->Indirect,
1899                                           bld->bld_base.info->file_max[reg->Register.File]);
1900    } else {
1901       assert(reg->Register.Index <=
1902                              bld_base->info->file_max[reg->Register.File]);
1903    }
1904
1905    if (DEBUG_EXECUTION) {
1906       emit_dump_reg(gallivm, reg->Register.File, reg->Register.Index, chan_index, value);
1907    }
1908
1909    assert(bld_base->emit_store_reg_funcs[reg->Register.File]);
1910    bld_base->emit_store_reg_funcs[reg->Register.File](bld_base,
1911                                                       dtype,
1912                                                       reg,
1913                                                       index,
1914                                                       chan_index,
1915                                                       indirect_index,
1916                                                       value);
1917
1918    (void)dtype;
1919 }
1920
1921 /*
1922  * Called at the beginning of the translation of each TGSI instruction, to
1923  * emit some debug code.
1924  */
1925 static void
1926 emit_debug(
1927    struct lp_build_tgsi_context * bld_base,
1928    const struct tgsi_full_instruction * inst,
1929    const struct tgsi_opcode_info * info)
1930
1931 {
1932    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1933
1934    if (DEBUG_EXECUTION) {
1935       /*
1936        * Dump the TGSI instruction.
1937        */
1938
1939       struct gallivm_state *gallivm = bld_base->base.gallivm;
1940       char buf[512];
1941       buf[0] = '$';
1942       buf[1] = ' ';
1943       tgsi_dump_instruction_str(inst, bld_base->pc, &buf[2], sizeof buf - 2);
1944       lp_build_printf(gallivm, buf);
1945
1946       /* Dump the execution mask.
1947        */
1948       if (bld->exec_mask.has_mask) {
1949          lp_build_print_value(gallivm, "    mask = ", bld->exec_mask.exec_mask);
1950       }
1951    }
1952 }
1953
1954 static void
1955 emit_store(
1956    struct lp_build_tgsi_context * bld_base,
1957    const struct tgsi_full_instruction * inst,
1958    const struct tgsi_opcode_info * info,
1959    unsigned index,
1960    LLVMValueRef dst[4])
1961
1962 {
1963    enum tgsi_opcode_type dtype = tgsi_opcode_infer_dst_type(inst->Instruction.Opcode, index);
1964
1965    unsigned writemask = inst->Dst[index].Register.WriteMask;
1966    while (writemask) {
1967       unsigned chan_index = u_bit_scan(&writemask);
1968       if (tgsi_type_is_64bit(dtype) && (chan_index == 1 || chan_index == 3))
1969           continue;
1970       emit_store_chan(bld_base, inst, index, chan_index, dst[chan_index]);
1971    }
1972 }
1973
1974 static unsigned
1975 tgsi_to_pipe_tex_target(enum tgsi_texture_type tgsi_target)
1976 {
1977    switch (tgsi_target) {
1978    case TGSI_TEXTURE_BUFFER:
1979       return PIPE_BUFFER;
1980    case TGSI_TEXTURE_1D:
1981    case TGSI_TEXTURE_SHADOW1D:
1982       return PIPE_TEXTURE_1D;
1983    case TGSI_TEXTURE_2D:
1984    case TGSI_TEXTURE_SHADOW2D:
1985    case TGSI_TEXTURE_2D_MSAA:
1986       return PIPE_TEXTURE_2D;
1987    case TGSI_TEXTURE_3D:
1988       return PIPE_TEXTURE_3D;
1989    case TGSI_TEXTURE_CUBE:
1990    case TGSI_TEXTURE_SHADOWCUBE:
1991       return PIPE_TEXTURE_CUBE;
1992    case TGSI_TEXTURE_RECT:
1993    case TGSI_TEXTURE_SHADOWRECT:
1994       return PIPE_TEXTURE_RECT;
1995    case TGSI_TEXTURE_1D_ARRAY:
1996    case TGSI_TEXTURE_SHADOW1D_ARRAY:
1997       return PIPE_TEXTURE_1D_ARRAY;
1998    case TGSI_TEXTURE_2D_ARRAY:
1999    case TGSI_TEXTURE_SHADOW2D_ARRAY:
2000    case TGSI_TEXTURE_2D_ARRAY_MSAA:
2001       return PIPE_TEXTURE_2D_ARRAY;
2002    case TGSI_TEXTURE_CUBE_ARRAY:
2003    case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
2004       return PIPE_TEXTURE_CUBE_ARRAY;
2005    default:
2006       assert(0);
2007       return PIPE_BUFFER;
2008    }
2009 }
2010
2011
2012 static enum lp_sampler_lod_property
2013 lp_build_lod_property(
2014    struct lp_build_tgsi_context *bld_base,
2015    const struct tgsi_full_instruction *inst,
2016    unsigned src_op)
2017 {
2018    const struct tgsi_full_src_register *reg = &inst->Src[src_op];
2019    enum lp_sampler_lod_property lod_property;
2020
2021    /*
2022     * Not much we can do here. We could try catching inputs declared
2023     * with constant interpolation but not sure it's worth it - since for
2024     * TEX opcodes as well as FETCH/LD the lod comes from same reg as
2025     * the coords, so it could only work for SAMPLE/TXQ/SVIEWINFO), just
2026     * like the constant/immediate recognition below.
2027     * What seems to be of more value would be to recognize temps holding
2028     * broadcasted scalars but no way we can do it.
2029     * Tried asking llvm but without any success (using LLVMIsConstant
2030     * even though this isn't exactly what we'd need), even as simple as
2031     * IMM[0] UINT32 (0,-1,0,0)
2032     * MOV TEMP[0] IMM[0].yyyy
2033     * SVIEWINFO TEMP[1], TEMP[0].xxxx, SVIEWINFO[0]
2034     * doesn't work.
2035     * This means there's ZERO chance this will ever catch a scalar lod
2036     * with traditional tex opcodes as well as texel fetches, since the lod
2037     * comes from the same reg as coords (except some test shaders using
2038     * constant coords maybe).
2039     * There's at least hope for sample opcodes as well as size queries.
2040     */
2041    if (inst->Instruction.Opcode == TGSI_OPCODE_TEX_LZ ||
2042        reg->Register.File == TGSI_FILE_CONSTANT ||
2043        reg->Register.File == TGSI_FILE_IMMEDIATE) {
2044       lod_property = LP_SAMPLER_LOD_SCALAR;
2045    }
2046    else if (bld_base->info->processor == PIPE_SHADER_FRAGMENT) {
2047       if (gallivm_perf & GALLIVM_PERF_NO_QUAD_LOD) {
2048          lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2049       }
2050       else {
2051          lod_property = LP_SAMPLER_LOD_PER_QUAD;
2052       }
2053    }
2054    else {
2055       /* never use scalar (per-quad) lod the results are just too wrong. */
2056       lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2057    }
2058    return lod_property;
2059 }
2060
2061
2062 /**
2063  * High-level instruction translators.
2064  */
2065
2066 static void
2067 emit_tex( struct lp_build_tgsi_soa_context *bld,
2068           const struct tgsi_full_instruction *inst,
2069           enum lp_build_tex_modifier modifier,
2070           LLVMValueRef *texel,
2071           unsigned sampler_reg,
2072           enum lp_sampler_op_type sampler_op)
2073 {
2074    unsigned unit = inst->Src[sampler_reg].Register.Index;
2075    LLVMValueRef oow = NULL;
2076    LLVMValueRef lod = NULL;
2077    LLVMValueRef coords[5];
2078    LLVMValueRef offsets[3] = { NULL };
2079    struct lp_derivatives derivs;
2080    struct lp_sampler_params params;
2081    enum lp_sampler_lod_property lod_property = LP_SAMPLER_LOD_SCALAR;
2082    unsigned num_derivs, num_offsets, i;
2083    unsigned shadow_coord = 0;
2084    unsigned layer_coord = 0;
2085    unsigned sample_key = sampler_op << LP_SAMPLER_OP_TYPE_SHIFT;
2086
2087    memset(&params, 0, sizeof(params));
2088
2089    if (!bld->sampler) {
2090       _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2091       for (i = 0; i < 4; i++) {
2092          texel[i] = bld->bld_base.base.undef;
2093       }
2094       return;
2095    }
2096
2097    switch (inst->Texture.Texture) {
2098    case TGSI_TEXTURE_1D_ARRAY:
2099       layer_coord = 1;
2100       FALLTHROUGH;
2101    case TGSI_TEXTURE_1D:
2102       num_offsets = 1;
2103       num_derivs = 1;
2104       break;
2105    case TGSI_TEXTURE_2D_ARRAY:
2106       layer_coord = 2;
2107       FALLTHROUGH;
2108    case TGSI_TEXTURE_2D:
2109    case TGSI_TEXTURE_RECT:
2110       num_offsets = 2;
2111       num_derivs = 2;
2112       break;
2113    case TGSI_TEXTURE_SHADOW1D_ARRAY:
2114       layer_coord = 1;
2115       FALLTHROUGH;
2116    case TGSI_TEXTURE_SHADOW1D:
2117       shadow_coord = 2;
2118       num_offsets = 1;
2119       num_derivs = 1;
2120       break;
2121    case TGSI_TEXTURE_SHADOW2D_ARRAY:
2122       layer_coord = 2;
2123       shadow_coord = 3;
2124       num_offsets = 2;
2125       num_derivs = 2;
2126       break;
2127    case TGSI_TEXTURE_SHADOW2D:
2128    case TGSI_TEXTURE_SHADOWRECT:
2129       shadow_coord = 2;
2130       num_offsets = 2;
2131       num_derivs = 2;
2132       break;
2133    case TGSI_TEXTURE_CUBE:
2134       num_offsets = 2;
2135       num_derivs = 3;
2136       break;
2137    case TGSI_TEXTURE_3D:
2138       num_offsets = 3;
2139       num_derivs = 3;
2140       break;
2141    case TGSI_TEXTURE_SHADOWCUBE:
2142       shadow_coord = 3;
2143       num_offsets = 2;
2144       num_derivs = 3;
2145       break;
2146    case TGSI_TEXTURE_CUBE_ARRAY:
2147       num_offsets = 2;
2148       num_derivs = 3;
2149       layer_coord = 3;
2150       break;
2151    case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
2152       num_offsets = 2;
2153       num_derivs = 3;
2154       layer_coord = 3;
2155       shadow_coord = 4; /* shadow coord special different reg */
2156       break;
2157    case TGSI_TEXTURE_2D_MSAA:
2158    case TGSI_TEXTURE_2D_ARRAY_MSAA:
2159    default:
2160       assert(0);
2161       return;
2162    }
2163
2164    /* Note lod and especially projected are illegal in a LOT of cases */
2165    if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS ||
2166        modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
2167       if (inst->Instruction.Opcode == TGSI_OPCODE_TEX_LZ) {
2168          lod = bld->bld_base.base.zero;
2169       } else if (inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
2170                  inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY) {
2171          /* note that shadow cube array with bias/explicit lod does not exist */
2172          lod = lp_build_emit_fetch(&bld->bld_base, inst, 1, 0);
2173       }
2174       else {
2175          lod = lp_build_emit_fetch(&bld->bld_base, inst, 0, 3);
2176       }
2177       if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS) {
2178          sample_key |= LP_SAMPLER_LOD_BIAS << LP_SAMPLER_LOD_CONTROL_SHIFT;
2179       }
2180       else if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
2181          sample_key |= LP_SAMPLER_LOD_EXPLICIT << LP_SAMPLER_LOD_CONTROL_SHIFT;
2182       }
2183       lod_property = lp_build_lod_property(&bld->bld_base, inst, 0);
2184    }
2185
2186    if (sampler_op == LP_SAMPLER_OP_GATHER) {
2187       uint32_t comp_val = inst->Src[sampler_reg].Register.SwizzleX;
2188       sample_key |= (comp_val << LP_SAMPLER_GATHER_COMP_SHIFT);
2189    }
2190    if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED) {
2191       oow = lp_build_emit_fetch(&bld->bld_base, inst, 0, 3);
2192       oow = lp_build_rcp(&bld->bld_base.base, oow);
2193    }
2194
2195    for (i = 0; i < num_derivs; i++) {
2196       coords[i] = lp_build_emit_fetch(&bld->bld_base, inst, 0, i);
2197       if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED)
2198          coords[i] = lp_build_mul(&bld->bld_base.base, coords[i], oow);
2199    }
2200    for (i = num_derivs; i < 5; i++) {
2201       coords[i] = bld->bld_base.base.undef;
2202    }
2203
2204    /* Layer coord always goes into 3rd slot, except for cube map arrays */
2205    if (layer_coord) {
2206       if (layer_coord == 3) {
2207          coords[3] = lp_build_emit_fetch(&bld->bld_base, inst, 0, layer_coord);
2208       }
2209       else {
2210          coords[2] = lp_build_emit_fetch(&bld->bld_base, inst, 0, layer_coord);
2211       }
2212       if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED)
2213          coords[2] = lp_build_mul(&bld->bld_base.base, coords[2], oow);
2214    }
2215    /* Shadow coord occupies always 5th slot. */
2216    if (shadow_coord) {
2217       sample_key |= LP_SAMPLER_SHADOW;
2218       if (shadow_coord == 4) {
2219          coords[4] = lp_build_emit_fetch(&bld->bld_base, inst, 1, 0);
2220       }
2221       else {
2222          coords[4] = lp_build_emit_fetch(&bld->bld_base, inst, 0, shadow_coord);
2223       }
2224       if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED)
2225          coords[4] = lp_build_mul(&bld->bld_base.base, coords[4], oow);
2226    }
2227
2228    if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
2229       unsigned dim;
2230       sample_key |= LP_SAMPLER_LOD_DERIVATIVES << LP_SAMPLER_LOD_CONTROL_SHIFT;
2231       for (dim = 0; dim < num_derivs; ++dim) {
2232          derivs.ddx[dim] = lp_build_emit_fetch(&bld->bld_base, inst, 1, dim);
2233          derivs.ddy[dim] = lp_build_emit_fetch(&bld->bld_base, inst, 2, dim);
2234       }
2235       params.derivs = &derivs;
2236       /*
2237        * could also check all src regs if constant but I doubt such
2238        * cases exist in practice.
2239        */
2240       if (bld->bld_base.info->processor == PIPE_SHADER_FRAGMENT) {
2241          if (gallivm_perf & GALLIVM_PERF_NO_QUAD_LOD) {
2242             lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2243          }
2244          else {
2245             lod_property = LP_SAMPLER_LOD_PER_QUAD;
2246          }
2247       }
2248       else {
2249          lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2250       }
2251    }
2252    sample_key |= lod_property << LP_SAMPLER_LOD_PROPERTY_SHIFT;
2253
2254    /* we don't handle the 4 offset version of tg4 */
2255    if (inst->Texture.NumOffsets == 1) {
2256       unsigned dim;
2257       sample_key |= LP_SAMPLER_OFFSETS;
2258       for (dim = 0; dim < num_offsets; dim++) {
2259          offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim);
2260       }
2261    }
2262
2263    params.type = bld->bld_base.base.type;
2264    params.sample_key = sample_key;
2265    params.texture_index = unit;
2266    params.sampler_index = unit;
2267    params.context_ptr = bld->context_ptr;
2268    params.thread_data_ptr = bld->thread_data_ptr;
2269    params.coords = coords;
2270    params.offsets = offsets;
2271    params.lod = lod;
2272    params.texel = texel;
2273
2274    bld->sampler->emit_tex_sample(bld->sampler,
2275                                  bld->bld_base.base.gallivm,
2276                                  &params);
2277 }
2278
2279 static void
2280 emit_sample(struct lp_build_tgsi_soa_context *bld,
2281             const struct tgsi_full_instruction *inst,
2282             enum lp_build_tex_modifier modifier,
2283             boolean compare,
2284             enum lp_sampler_op_type sample_type,
2285             LLVMValueRef *texel)
2286 {
2287    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
2288    unsigned texture_unit, sampler_unit;
2289    LLVMValueRef lod = NULL;
2290    LLVMValueRef coords[5];
2291    LLVMValueRef offsets[3] = { NULL };
2292    struct lp_derivatives derivs;
2293    struct lp_sampler_params params;
2294    enum lp_sampler_lod_property lod_property = LP_SAMPLER_LOD_SCALAR;
2295
2296    unsigned num_offsets, num_derivs, i;
2297    unsigned layer_coord = 0;
2298    unsigned sample_key = sample_type << LP_SAMPLER_OP_TYPE_SHIFT;
2299
2300    memset(&params, 0, sizeof(params));
2301
2302    if (!bld->sampler) {
2303       _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2304       for (i = 0; i < 4; i++) {
2305          texel[i] = bld->bld_base.base.undef;
2306       }
2307       return;
2308    }
2309
2310    /*
2311     * unlike old-style tex opcodes the texture/sampler indices
2312     * always come from src1 and src2 respectively.
2313     */
2314    texture_unit = inst->Src[1].Register.Index;
2315    sampler_unit = inst->Src[2].Register.Index;
2316
2317    /*
2318     * Note inst->Texture.Texture will contain the number of offsets,
2319     * however the target information is NOT there and comes from the
2320     * declared sampler views instead.
2321     */
2322    switch (bld->sv[texture_unit].Resource) {
2323    case TGSI_TEXTURE_1D:
2324       num_offsets = 1;
2325       num_derivs = 1;
2326       break;
2327    case TGSI_TEXTURE_1D_ARRAY:
2328       layer_coord = 1;
2329       num_offsets = 1;
2330       num_derivs = 1;
2331       break;
2332    case TGSI_TEXTURE_2D:
2333    case TGSI_TEXTURE_RECT:
2334       num_offsets = 2;
2335       num_derivs = 2;
2336       break;
2337    case TGSI_TEXTURE_2D_ARRAY:
2338       layer_coord = 2;
2339       num_offsets = 2;
2340       num_derivs = 2;
2341       break;
2342    case TGSI_TEXTURE_CUBE:
2343       num_offsets = 2;
2344       num_derivs = 3;
2345       break;
2346    case TGSI_TEXTURE_3D:
2347       num_offsets = 3;
2348       num_derivs = 3;
2349       break;
2350    case TGSI_TEXTURE_CUBE_ARRAY:
2351       layer_coord = 3;
2352       num_offsets = 2;
2353       num_derivs = 3;
2354       break;
2355    default:
2356       assert(0);
2357       return;
2358    }
2359
2360    if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS ||
2361        modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
2362       lod = lp_build_emit_fetch(&bld->bld_base, inst, 3, 0);
2363       if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS) {
2364          sample_key |= LP_SAMPLER_LOD_BIAS << LP_SAMPLER_LOD_CONTROL_SHIFT;
2365       }
2366       else if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
2367          sample_key |= LP_SAMPLER_LOD_EXPLICIT << LP_SAMPLER_LOD_CONTROL_SHIFT;
2368       }
2369       lod_property = lp_build_lod_property(&bld->bld_base, inst, 0);
2370    }
2371    else if (modifier == LP_BLD_TEX_MODIFIER_LOD_ZERO) {
2372       /* XXX might be better to explicitly pass the level zero information */
2373       sample_key |= LP_SAMPLER_LOD_EXPLICIT << LP_SAMPLER_LOD_CONTROL_SHIFT;
2374       lod = lp_build_const_vec(gallivm, bld->bld_base.base.type, 0.0F);
2375    }
2376
2377    for (i = 0; i < num_derivs; i++) {
2378       coords[i] = lp_build_emit_fetch(&bld->bld_base, inst, 0, i);
2379    }
2380    for (i = num_derivs; i < 5; i++) {
2381       coords[i] = bld->bld_base.base.undef;
2382    }
2383
2384    /* Layer coord always goes into 3rd slot, except for cube map arrays */
2385    if (layer_coord) {
2386       if (layer_coord == 3)
2387          coords[3] = lp_build_emit_fetch(&bld->bld_base, inst, 0, layer_coord);
2388       else
2389          coords[2] = lp_build_emit_fetch(&bld->bld_base, inst, 0, layer_coord);
2390    }
2391    /* Shadow coord occupies always 5th slot. */
2392    if (compare) {
2393       sample_key |= LP_SAMPLER_SHADOW;
2394       coords[4] = lp_build_emit_fetch(&bld->bld_base, inst, 3, 0);
2395    }
2396
2397    if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
2398       unsigned dim;
2399       sample_key |= LP_SAMPLER_LOD_DERIVATIVES << LP_SAMPLER_LOD_CONTROL_SHIFT;
2400       for (dim = 0; dim < num_derivs; ++dim) {
2401          derivs.ddx[dim] = lp_build_emit_fetch(&bld->bld_base, inst, 3, dim);
2402          derivs.ddy[dim] = lp_build_emit_fetch(&bld->bld_base, inst, 4, dim);
2403       }
2404       params.derivs = &derivs;
2405       /*
2406        * could also check all src regs if constant but I doubt such
2407        * cases exist in practice.
2408        */
2409       if (bld->bld_base.info->processor == PIPE_SHADER_FRAGMENT) {
2410          if (gallivm_perf & GALLIVM_PERF_NO_QUAD_LOD) {
2411             lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2412          }
2413          else {
2414             lod_property = LP_SAMPLER_LOD_PER_QUAD;
2415          }
2416       }
2417       else {
2418          lod_property = LP_SAMPLER_LOD_PER_ELEMENT;
2419       }
2420    }
2421
2422    /* some advanced gather instructions (txgo) would require 4 offsets */
2423    if (inst->Texture.NumOffsets == 1) {
2424       unsigned dim;
2425       sample_key |= LP_SAMPLER_OFFSETS;
2426       for (dim = 0; dim < num_offsets; dim++) {
2427          offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim);
2428       }
2429    }
2430    sample_key |= lod_property << LP_SAMPLER_LOD_PROPERTY_SHIFT;
2431
2432    params.type = bld->bld_base.base.type;
2433    params.sample_key = sample_key;
2434    params.texture_index = texture_unit;
2435    params.sampler_index = sampler_unit;
2436    params.context_ptr = bld->context_ptr;
2437    params.thread_data_ptr = bld->thread_data_ptr;
2438    params.coords = coords;
2439    params.offsets = offsets;
2440    params.lod = lod;
2441    params.texel = texel;
2442
2443    bld->sampler->emit_tex_sample(bld->sampler,
2444                                  bld->bld_base.base.gallivm,
2445                                  &params);
2446
2447    if (inst->Src[1].Register.SwizzleX != PIPE_SWIZZLE_X ||
2448        inst->Src[1].Register.SwizzleY != PIPE_SWIZZLE_Y ||
2449        inst->Src[1].Register.SwizzleZ != PIPE_SWIZZLE_Z ||
2450        inst->Src[1].Register.SwizzleW != PIPE_SWIZZLE_W) {
2451       unsigned char swizzles[4];
2452       swizzles[0] = inst->Src[1].Register.SwizzleX;
2453       swizzles[1] = inst->Src[1].Register.SwizzleY;
2454       swizzles[2] = inst->Src[1].Register.SwizzleZ;
2455       swizzles[3] = inst->Src[1].Register.SwizzleW;
2456
2457       lp_build_swizzle_soa_inplace(&bld->bld_base.base, texel, swizzles);
2458    }
2459 }
2460
2461 static void
2462 emit_fetch_texels( struct lp_build_tgsi_soa_context *bld,
2463                    const struct tgsi_full_instruction *inst,
2464                    LLVMValueRef *texel,
2465                    boolean is_samplei)
2466 {
2467    unsigned unit, target;
2468    LLVMValueRef coord_undef = LLVMGetUndef(bld->bld_base.base.int_vec_type);
2469    LLVMValueRef explicit_lod = NULL;
2470    LLVMValueRef coords[5];
2471    LLVMValueRef offsets[3] = { NULL };
2472    LLVMValueRef ms_index = NULL;
2473    struct lp_sampler_params params;
2474    enum lp_sampler_lod_property lod_property = LP_SAMPLER_LOD_SCALAR;
2475    unsigned dims, i;
2476    unsigned layer_coord = 0;
2477    unsigned sample_key = LP_SAMPLER_OP_FETCH << LP_SAMPLER_OP_TYPE_SHIFT;
2478
2479    memset(&params, 0, sizeof(params));
2480
2481    if (!bld->sampler) {
2482       _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
2483       for (i = 0; i < 4; i++) {
2484          texel[i] = coord_undef;
2485       }
2486       return;
2487    }
2488
2489    unit = inst->Src[1].Register.Index;
2490
2491    if (is_samplei) {
2492       target = bld->sv[unit].Resource;
2493    }
2494    else {
2495       target = inst->Texture.Texture;
2496    }
2497
2498    switch (target) {
2499    case TGSI_TEXTURE_1D:
2500    case TGSI_TEXTURE_BUFFER:
2501       dims = 1;
2502       break;
2503    case TGSI_TEXTURE_1D_ARRAY:
2504       layer_coord = 1;
2505       dims = 1;
2506       break;
2507    case TGSI_TEXTURE_2D:
2508    case TGSI_TEXTURE_RECT:
2509    case TGSI_TEXTURE_2D_MSAA:
2510       dims = 2;
2511       break;
2512    case TGSI_TEXTURE_2D_ARRAY:
2513    case TGSI_TEXTURE_2D_ARRAY_MSAA:
2514       layer_coord = 2;
2515       dims = 2;
2516       break;
2517    case TGSI_TEXTURE_3D:
2518       dims = 3;
2519       break;
2520    default:
2521       assert(0);
2522       return;
2523    }
2524
2525    /* always have lod except for buffers and msaa targets ? */
2526    if (target != TGSI_TEXTURE_BUFFER &&
2527        target != TGSI_TEXTURE_2D_MSAA &&
2528        target != TGSI_TEXTURE_2D_ARRAY_MSAA &&
2529        inst->Instruction.Opcode != TGSI_OPCODE_TXF_LZ) {
2530       sample_key |= LP_SAMPLER_LOD_EXPLICIT << LP_SAMPLER_LOD_CONTROL_SHIFT;
2531       explicit_lod = lp_build_emit_fetch(&bld->bld_base, inst, 0, 3);
2532       lod_property = lp_build_lod_property(&bld->bld_base, inst, 0);
2533    }
2534
2535    if (target == TGSI_TEXTURE_2D_MSAA ||
2536        target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
2537       sample_key |= LP_SAMPLER_FETCH_MS;
2538       ms_index = lp_build_emit_fetch(&bld->bld_base, inst, 0, 3);
2539    }
2540
2541    /*
2542     * XXX: for real msaa support, the w component (or src2.x for sample_i_ms)
2543     * would be the sample index.
2544     */
2545
2546    for (i = 0; i < dims; i++) {
2547       coords[i] = lp_build_emit_fetch(&bld->bld_base, inst, 0, i);
2548    }
2549    /* never use more than 3 coords here but emit_fetch_texel copies all 5 anyway */
2550    for (i = dims; i < 5; i++) {
2551       coords[i] = coord_undef;
2552    }
2553    if (layer_coord)
2554       coords[2] = lp_build_emit_fetch(&bld->bld_base, inst, 0, layer_coord);
2555
2556    if (inst->Texture.NumOffsets == 1) {
2557       unsigned dim;
2558       sample_key |= LP_SAMPLER_OFFSETS;
2559       for (dim = 0; dim < dims; dim++) {
2560          offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim);
2561       }
2562    }
2563    sample_key |= lod_property << LP_SAMPLER_LOD_PROPERTY_SHIFT;
2564
2565    params.type = bld->bld_base.base.type;
2566    params.sample_key = sample_key;
2567    params.texture_index = unit;
2568    /*
2569     * sampler not actually used, set to 0 so it won't exceed PIPE_MAX_SAMPLERS
2570     * and trigger some assertions with d3d10 where the sampler view number
2571     * can exceed this.
2572     */
2573    params.sampler_index = 0;
2574    params.context_ptr = bld->context_ptr;
2575    params.thread_data_ptr = bld->thread_data_ptr;
2576    params.coords = coords;
2577    params.offsets = offsets;
2578    params.derivs = NULL;
2579    params.lod = explicit_lod;
2580    params.texel = texel;
2581    params.ms_index = ms_index;
2582
2583    bld->sampler->emit_tex_sample(bld->sampler,
2584                                  bld->bld_base.base.gallivm,
2585                                  &params);
2586
2587    if (is_samplei &&
2588        (inst->Src[1].Register.SwizzleX != PIPE_SWIZZLE_X ||
2589         inst->Src[1].Register.SwizzleY != PIPE_SWIZZLE_Y ||
2590         inst->Src[1].Register.SwizzleZ != PIPE_SWIZZLE_Z ||
2591         inst->Src[1].Register.SwizzleW != PIPE_SWIZZLE_W)) {
2592       unsigned char swizzles[4];
2593       swizzles[0] = inst->Src[1].Register.SwizzleX;
2594       swizzles[1] = inst->Src[1].Register.SwizzleY;
2595       swizzles[2] = inst->Src[1].Register.SwizzleZ;
2596       swizzles[3] = inst->Src[1].Register.SwizzleW;
2597
2598       lp_build_swizzle_soa_inplace(&bld->bld_base.base, texel, swizzles);
2599    }
2600 }
2601
2602 static void
2603 emit_size_query( struct lp_build_tgsi_soa_context *bld,
2604                  const struct tgsi_full_instruction *inst,
2605                  LLVMValueRef *sizes_out,
2606                  boolean is_sviewinfo)
2607 {
2608    LLVMValueRef explicit_lod;
2609    enum lp_sampler_lod_property lod_property;
2610    unsigned has_lod;
2611    unsigned i;
2612    unsigned unit = inst->Src[1].Register.Index;
2613    enum tgsi_texture_type target;
2614    enum pipe_texture_target pipe_target;
2615    struct lp_sampler_size_query_params params;
2616
2617    if (is_sviewinfo) {
2618       target = bld->sv[unit].Resource;
2619    }
2620    else {
2621       target = inst->Texture.Texture;
2622    }
2623    switch (target) {
2624    case TGSI_TEXTURE_BUFFER:
2625    case TGSI_TEXTURE_RECT:
2626    case TGSI_TEXTURE_SHADOWRECT:
2627       has_lod = 0;
2628       break;
2629    default:
2630       has_lod = 1;
2631       break;
2632    }
2633
2634    if (!bld->sampler) {
2635       _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
2636       for (i = 0; i < 4; i++)
2637          sizes_out[i] = bld->bld_base.int_bld.undef;
2638       return;
2639    }
2640
2641    if (has_lod) {
2642       explicit_lod = lp_build_emit_fetch(&bld->bld_base, inst, 0, 0);
2643       lod_property = lp_build_lod_property(&bld->bld_base, inst, 0);
2644    }
2645    else {
2646       explicit_lod = NULL;
2647       lod_property = LP_SAMPLER_LOD_SCALAR;
2648    }
2649
2650
2651    pipe_target = tgsi_to_pipe_tex_target(target);
2652
2653    params.int_type = bld->bld_base.int_bld.type;
2654    params.texture_unit = unit;
2655    params.target = pipe_target;
2656    params.context_ptr = bld->context_ptr;
2657    params.is_sviewinfo = TRUE;
2658    params.lod_property = lod_property;
2659    params.explicit_lod = explicit_lod;
2660    params.sizes_out = sizes_out;
2661    params.samples_only = false;
2662
2663    bld->sampler->emit_size_query(bld->sampler,
2664                                  bld->bld_base.base.gallivm,
2665                                  &params);
2666 }
2667
2668 static boolean
2669 near_end_of_shader(struct lp_build_tgsi_soa_context *bld,
2670                    int pc)
2671 {
2672    unsigned i;
2673
2674    for (i = 0; i < 5; i++) {
2675       enum tgsi_opcode opcode;
2676
2677       if (pc + i >= bld->bld_base.info->num_instructions)
2678          return TRUE;
2679
2680       opcode = bld->bld_base.instructions[pc + i].Instruction.Opcode;
2681
2682       if (opcode == TGSI_OPCODE_END)
2683          return TRUE;
2684
2685       if (opcode == TGSI_OPCODE_TEX ||
2686          opcode == TGSI_OPCODE_TXP ||
2687          opcode == TGSI_OPCODE_TXD ||
2688          opcode == TGSI_OPCODE_TXB ||
2689          opcode == TGSI_OPCODE_TXL ||
2690          opcode == TGSI_OPCODE_TXF ||
2691          opcode == TGSI_OPCODE_TXQ ||
2692          opcode == TGSI_OPCODE_TEX2 ||
2693          opcode == TGSI_OPCODE_TXB2 ||
2694          opcode == TGSI_OPCODE_TXL2 ||
2695          opcode == TGSI_OPCODE_SAMPLE ||
2696          opcode == TGSI_OPCODE_SAMPLE_B ||
2697          opcode == TGSI_OPCODE_SAMPLE_C ||
2698          opcode == TGSI_OPCODE_SAMPLE_C_LZ ||
2699          opcode == TGSI_OPCODE_SAMPLE_D ||
2700          opcode == TGSI_OPCODE_SAMPLE_I ||
2701          opcode == TGSI_OPCODE_SAMPLE_I_MS ||
2702          opcode == TGSI_OPCODE_SAMPLE_L ||
2703          opcode == TGSI_OPCODE_SVIEWINFO ||
2704          opcode == TGSI_OPCODE_CAL ||
2705          opcode == TGSI_OPCODE_IF ||
2706          opcode == TGSI_OPCODE_UIF ||
2707          opcode == TGSI_OPCODE_BGNLOOP ||
2708          opcode == TGSI_OPCODE_SWITCH)
2709          return FALSE;
2710    }
2711
2712    return TRUE;
2713 }
2714
2715
2716
2717 /**
2718  * Kill fragment if any of the src register values are negative.
2719  */
2720 static void
2721 emit_kill_if(
2722    struct lp_build_tgsi_soa_context *bld,
2723    const struct tgsi_full_instruction *inst,
2724    int pc)
2725 {
2726    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
2727    const struct tgsi_full_src_register *reg = &inst->Src[0];
2728    LLVMValueRef terms[TGSI_NUM_CHANNELS];
2729    LLVMValueRef mask;
2730    unsigned chan_index;
2731
2732    memset(&terms, 0, sizeof terms);
2733
2734    TGSI_FOR_EACH_CHANNEL( chan_index ) {
2735       unsigned swizzle;
2736
2737       /* Unswizzle channel */
2738       swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
2739
2740       /* Check if the component has not been already tested. */
2741       assert(swizzle < TGSI_NUM_CHANNELS);
2742       if( !terms[swizzle] )
2743          /* TODO: change the comparison operator instead of setting the sign */
2744          terms[swizzle] =  lp_build_emit_fetch(&bld->bld_base, inst, 0, chan_index );
2745    }
2746
2747    mask = NULL;
2748    TGSI_FOR_EACH_CHANNEL( chan_index ) {
2749       if(terms[chan_index]) {
2750          LLVMValueRef chan_mask;
2751
2752          /*
2753           * If term < 0 then mask = 0 else mask = ~0.
2754           */
2755          chan_mask = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->bld_base.base.zero);
2756
2757          if(mask)
2758             mask = LLVMBuildAnd(builder, mask, chan_mask, "");
2759          else
2760             mask = chan_mask;
2761       }
2762    }
2763
2764    if (bld->exec_mask.has_mask) {
2765       LLVMValueRef invmask;
2766       invmask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
2767       mask = LLVMBuildOr(builder, mask, invmask, "");
2768    }
2769
2770    lp_build_mask_update(bld->mask, mask);
2771    if (!near_end_of_shader(bld, pc))
2772       lp_build_mask_check(bld->mask);
2773 }
2774
2775
2776 /**
2777  * Unconditional fragment kill.
2778  * The only predication is the execution mask which will apply if
2779  * we're inside a loop or conditional.
2780  */
2781 static void
2782 emit_kill(struct lp_build_tgsi_soa_context *bld,
2783           int pc)
2784 {
2785    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
2786    LLVMValueRef mask;
2787
2788    /* For those channels which are "alive", disable fragment shader
2789     * execution.
2790     */
2791    if (bld->exec_mask.has_mask) {
2792       mask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
2793    }
2794    else {
2795       LLVMValueRef zero = LLVMConstNull(bld->bld_base.base.int_vec_type);
2796       mask = zero;
2797    }
2798
2799    lp_build_mask_update(bld->mask, mask);
2800
2801    if (!near_end_of_shader(bld, pc))
2802       lp_build_mask_check(bld->mask);
2803 }
2804
2805
2806 /**
2807  * Emit code which will dump the value of all the temporary registers
2808  * to stdout.
2809  */
2810 static void
2811 emit_dump_file(struct lp_build_tgsi_soa_context *bld,
2812                unsigned file)
2813 {
2814    const struct tgsi_shader_info *info = bld->bld_base.info;
2815    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
2816    LLVMBuilderRef builder = gallivm->builder;
2817    LLVMValueRef reg_ptr;
2818    int index;
2819    int max_index = info->file_max[file];
2820
2821    /*
2822     * Some register files, particularly constants, can be very large,
2823     * and dumping everything could make this unusably slow.
2824     */
2825    max_index = MIN2(max_index, 32);
2826
2827    for (index = 0; index <= max_index; index++) {
2828       LLVMValueRef res;
2829       unsigned mask;
2830       int chan;
2831
2832       if (index < 8 * sizeof(unsigned) &&
2833           (info->file_mask[file] & (1u << index)) == 0)  {
2834          /* This was not declared.*/
2835          continue;
2836       }
2837
2838       if (file == TGSI_FILE_INPUT) {
2839          mask = info->input_usage_mask[index];
2840       } else {
2841          mask = TGSI_WRITEMASK_XYZW;
2842       }
2843
2844       for (chan = 0; chan < 4; chan++) {
2845          if ((mask & (1 << chan)) == 0) {
2846             /* This channel is not used.*/
2847             continue;
2848          }
2849
2850          if (file == TGSI_FILE_CONSTANT) {
2851             struct tgsi_full_src_register reg;
2852             memset(&reg, 0, sizeof reg);
2853             reg.Register.File = file;
2854             reg.Register.Index = index;
2855             reg.Register.SwizzleX = 0;
2856             reg.Register.SwizzleY = 1;
2857             reg.Register.SwizzleZ = 2;
2858             reg.Register.SwizzleW = 3;
2859
2860             res = bld->bld_base.emit_fetch_funcs[file](&bld->bld_base, &reg, TGSI_TYPE_FLOAT, chan);
2861             if (!res) {
2862                continue;
2863             }
2864          } else if (file == TGSI_FILE_INPUT) {
2865             res = bld->inputs[index][chan];
2866             if (!res) {
2867                continue;
2868             }
2869          } else if (file == TGSI_FILE_TEMPORARY) {
2870             reg_ptr = lp_get_temp_ptr_soa(bld, index, chan);
2871             assert(reg_ptr);
2872             res = LLVMBuildLoad(builder, reg_ptr, "");
2873          } else if (file == TGSI_FILE_OUTPUT) {
2874             reg_ptr = lp_get_output_ptr(bld, index, chan);
2875             assert(reg_ptr);
2876             res = LLVMBuildLoad(builder, reg_ptr, "");
2877          } else {
2878             assert(0);
2879             continue;
2880          }
2881
2882          emit_dump_reg(gallivm, file, index, chan, res);
2883       }
2884    }
2885 }
2886
2887
2888
2889 void
2890 lp_emit_declaration_soa(
2891    struct lp_build_tgsi_context *bld_base,
2892    const struct tgsi_full_declaration *decl)
2893 {
2894    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
2895    struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
2896    LLVMTypeRef vec_type = bld->bld_base.base.vec_type;
2897    const unsigned first = decl->Range.First;
2898    const unsigned last = decl->Range.Last;
2899    unsigned idx, i;
2900
2901    assert(last <= bld->bld_base.info->file_max[decl->Declaration.File]);
2902
2903    switch (decl->Declaration.File) {
2904    case TGSI_FILE_TEMPORARY:
2905       if (!(bld->indirect_files & (1 << TGSI_FILE_TEMPORARY))) {
2906          assert(last < LP_MAX_INLINED_TEMPS);
2907          for (idx = first; idx <= last; ++idx) {
2908             for (i = 0; i < TGSI_NUM_CHANNELS; i++)
2909                bld->temps[idx][i] = lp_build_alloca(gallivm, vec_type, "temp");
2910          }
2911       }
2912       break;
2913
2914    case TGSI_FILE_OUTPUT:
2915       if (!(bld->indirect_files & (1 << TGSI_FILE_OUTPUT))) {
2916          for (idx = first; idx <= last; ++idx) {
2917             for (i = 0; i < TGSI_NUM_CHANNELS; i++)
2918                bld->outputs[idx][i] = lp_build_alloca(gallivm,
2919                                                       vec_type, "output");
2920          }
2921       }
2922       break;
2923
2924    case TGSI_FILE_ADDRESS:
2925       /* ADDR registers are only allocated with an integer LLVM IR type,
2926        * as they are guaranteed to always have integers.
2927        * XXX: Not sure if this exception is worthwhile (or the whole idea of
2928        * an ADDR register for that matter).
2929        */
2930       assert(last < LP_MAX_TGSI_ADDRS);
2931       for (idx = first; idx <= last; ++idx) {
2932          assert(idx < LP_MAX_TGSI_ADDRS);
2933          for (i = 0; i < TGSI_NUM_CHANNELS; i++)
2934             bld->addr[idx][i] = lp_build_alloca(gallivm, bld_base->base.int_vec_type, "addr");
2935       }
2936       break;
2937
2938    case TGSI_FILE_SAMPLER_VIEW:
2939       /*
2940        * The target stored here MUST match whatever there actually
2941        * is in the set sampler views (what about return type?).
2942        */
2943       assert(last < PIPE_MAX_SHADER_SAMPLER_VIEWS);
2944       for (idx = first; idx <= last; ++idx) {
2945          bld->sv[idx] = decl->SamplerView;
2946       }
2947       break;
2948
2949    case TGSI_FILE_CONSTANT:
2950    {
2951       /*
2952        * We could trivially fetch the per-buffer pointer when fetching the
2953        * constant, relying on llvm to figure out it's always the same pointer
2954        * anyway. However, doing so results in a huge (more than factor of 10)
2955        * slowdown in llvm compilation times for some (but not all) shaders
2956        * (more specifically, the IR optimization spends way more time in
2957        * DominatorTree::dominates). At least with llvm versions 3.1, 3.3.
2958        */
2959       unsigned idx2D = decl->Dim.Index2D;
2960       LLVMValueRef index2D = lp_build_const_int32(gallivm, idx2D);
2961       assert(idx2D < LP_MAX_TGSI_CONST_BUFFERS);
2962       bld->consts[idx2D] = lp_llvm_buffer_base(gallivm, bld->consts_ptr,
2963                                                index2D, LP_MAX_TGSI_CONST_BUFFERS);
2964       bld->consts[idx2D] = LLVMBuildBitCast(gallivm->builder, bld->consts[idx2D], LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0), "");
2965       bld->consts_sizes[idx2D] = lp_llvm_buffer_num_elements(gallivm, bld->consts_ptr,
2966                                                              index2D, LP_MAX_TGSI_CONST_BUFFERS);
2967    }
2968    break;
2969    case TGSI_FILE_BUFFER:
2970    {
2971       unsigned idx = decl->Range.First;
2972       LLVMValueRef index = lp_build_const_int32(gallivm, idx);
2973       assert(idx < LP_MAX_TGSI_SHADER_BUFFERS);
2974       bld->ssbos[idx] =
2975          lp_llvm_buffer_base(gallivm, bld->ssbo_ptr,
2976                              index, LP_MAX_TGSI_SHADER_BUFFERS);
2977       bld->ssbo_sizes[idx] =
2978          lp_llvm_buffer_num_elements(gallivm, bld->ssbo_ptr,
2979                              index, LP_MAX_TGSI_SHADER_BUFFERS);
2980
2981    }
2982    break;
2983    case TGSI_FILE_MEMORY:
2984       break;
2985    default:
2986       /* don't need to declare other vars */
2987       break;
2988    }
2989 }
2990
2991
2992 void lp_emit_immediate_soa(
2993    struct lp_build_tgsi_context *bld_base,
2994    const struct tgsi_full_immediate *imm)
2995 {
2996    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
2997    struct gallivm_state * gallivm = bld_base->base.gallivm;
2998    LLVMValueRef imms[4];
2999    unsigned i;
3000    const uint size = imm->Immediate.NrTokens - 1;
3001    assert(size <= 4);
3002    switch (imm->Immediate.DataType) {
3003    case TGSI_IMM_FLOAT32:
3004       for( i = 0; i < size; ++i )
3005          imms[i] =
3006                lp_build_const_vec(gallivm, bld_base->base.type, imm->u[i].Float);
3007
3008       break;
3009    case TGSI_IMM_FLOAT64:
3010    case TGSI_IMM_UINT64:
3011    case TGSI_IMM_INT64:
3012    case TGSI_IMM_UINT32:
3013       for( i = 0; i < size; ++i ) {
3014          LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->uint_bld.type, imm->u[i].Uint);
3015          imms[i] = LLVMConstBitCast(tmp, bld_base->base.vec_type);
3016       }
3017
3018       break;
3019    case TGSI_IMM_INT32:
3020       for( i = 0; i < size; ++i ) {
3021          LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->int_bld.type, imm->u[i].Int);
3022          imms[i] = LLVMConstBitCast(tmp, bld_base->base.vec_type);
3023       }
3024
3025       break;
3026    }
3027    for( i = size; i < 4; ++i )
3028       imms[i] = bld_base->base.undef;
3029
3030    if (bld->use_immediates_array) {
3031       unsigned index = bld->num_immediates;
3032       struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
3033       LLVMBuilderRef builder = gallivm->builder;
3034       LLVMValueRef gep[2];
3035       gep[0] = lp_build_const_int32(gallivm, 0);
3036
3037       assert(bld->indirect_files & (1 << TGSI_FILE_IMMEDIATE));
3038       for (i = 0; i < 4; ++i ) {
3039          gep[1] = lp_build_const_int32(gallivm, index * 4 + i);
3040          LLVMValueRef imm_ptr = LLVMBuildGEP(builder,
3041                                              bld->imms_array, gep, 2, "");
3042          LLVMBuildStore(builder, imms[i], imm_ptr);
3043       }
3044    } else {
3045       /* simply copy the immediate values into the next immediates[] slot */
3046       unsigned i;
3047       assert(imm->Immediate.NrTokens - 1 <= 4);
3048       assert(bld->num_immediates < LP_MAX_INLINED_IMMEDIATES);
3049
3050       for(i = 0; i < 4; ++i )
3051          bld->immediates[bld->num_immediates][i] = imms[i];
3052
3053       if (bld->indirect_files & (1 << TGSI_FILE_IMMEDIATE)) {
3054          unsigned index = bld->num_immediates;
3055          struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
3056          LLVMBuilderRef builder = gallivm->builder;
3057          LLVMValueRef gep[2];
3058          gep[0] = lp_build_const_int32(gallivm, 0);
3059          for (i = 0; i < 4; ++i ) {
3060             gep[1] = lp_build_const_int32(gallivm, index * 4 + i);
3061             LLVMValueRef imm_ptr = LLVMBuildGEP(builder,
3062                                                 bld->imms_array, gep, 2, "");
3063             LLVMBuildStore(builder,
3064                            bld->immediates[index][i],
3065                            imm_ptr);
3066          }
3067       }
3068    }
3069
3070    bld->num_immediates++;
3071 }
3072
3073 static void
3074 ddx_emit(
3075    const struct lp_build_tgsi_action * action,
3076    struct lp_build_tgsi_context * bld_base,
3077    struct lp_build_emit_data * emit_data)
3078 {
3079    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3080
3081    emit_fetch_deriv(bld, emit_data->args[0], NULL,
3082                     &emit_data->output[emit_data->chan], NULL);
3083 }
3084
3085 static void
3086 ddy_emit(
3087    const struct lp_build_tgsi_action * action,
3088    struct lp_build_tgsi_context * bld_base,
3089    struct lp_build_emit_data * emit_data)
3090 {
3091    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3092
3093    emit_fetch_deriv(bld, emit_data->args[0], NULL, NULL,
3094                     &emit_data->output[emit_data->chan]);
3095 }
3096
3097 static void
3098 kill_emit(
3099    const struct lp_build_tgsi_action * action,
3100    struct lp_build_tgsi_context * bld_base,
3101    struct lp_build_emit_data * emit_data)
3102 {
3103    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3104
3105    emit_kill(bld, bld_base->pc - 1);
3106 }
3107
3108 static void
3109 kill_if_emit(
3110    const struct lp_build_tgsi_action * action,
3111    struct lp_build_tgsi_context * bld_base,
3112    struct lp_build_emit_data * emit_data)
3113 {
3114    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3115
3116    emit_kill_if(bld, emit_data->inst, bld_base->pc - 1);
3117 }
3118
3119 static void
3120 tex_emit(
3121    const struct lp_build_tgsi_action * action,
3122    struct lp_build_tgsi_context * bld_base,
3123    struct lp_build_emit_data * emit_data)
3124 {
3125    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3126
3127    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3128             emit_data->output, 1, LP_SAMPLER_OP_TEXTURE);
3129 }
3130
3131 static void
3132 tex2_emit(
3133    const struct lp_build_tgsi_action * action,
3134    struct lp_build_tgsi_context * bld_base,
3135    struct lp_build_emit_data * emit_data)
3136 {
3137    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3138
3139    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3140             emit_data->output, 2, LP_SAMPLER_OP_TEXTURE);
3141 }
3142
3143 static void
3144 txb_emit(
3145    const struct lp_build_tgsi_action * action,
3146    struct lp_build_tgsi_context * bld_base,
3147    struct lp_build_emit_data * emit_data)
3148 {
3149    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3150
3151    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_BIAS,
3152             emit_data->output, 1, LP_SAMPLER_OP_TEXTURE);
3153 }
3154
3155 static void
3156 txb2_emit(
3157    const struct lp_build_tgsi_action * action,
3158    struct lp_build_tgsi_context * bld_base,
3159    struct lp_build_emit_data * emit_data)
3160 {
3161    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3162
3163    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_BIAS,
3164             emit_data->output, 2, LP_SAMPLER_OP_TEXTURE);
3165 }
3166
3167 static void
3168 txd_emit(
3169    const struct lp_build_tgsi_action * action,
3170    struct lp_build_tgsi_context * bld_base,
3171    struct lp_build_emit_data * emit_data)
3172 {
3173    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3174
3175    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
3176             emit_data->output, 3, LP_SAMPLER_OP_TEXTURE);
3177 }
3178
3179 static void
3180 txl_emit(
3181    const struct lp_build_tgsi_action * action,
3182    struct lp_build_tgsi_context * bld_base,
3183    struct lp_build_emit_data * emit_data)
3184 {
3185    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3186
3187    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
3188             emit_data->output, 1, LP_SAMPLER_OP_TEXTURE);
3189 }
3190
3191 static void
3192 txl2_emit(
3193    const struct lp_build_tgsi_action * action,
3194    struct lp_build_tgsi_context * bld_base,
3195    struct lp_build_emit_data * emit_data)
3196 {
3197    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3198
3199    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
3200             emit_data->output, 2, LP_SAMPLER_OP_TEXTURE);
3201 }
3202
3203 static void
3204 txp_emit(
3205    const struct lp_build_tgsi_action * action,
3206    struct lp_build_tgsi_context * bld_base,
3207    struct lp_build_emit_data * emit_data)
3208 {
3209    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3210
3211    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_PROJECTED,
3212             emit_data->output, 1, LP_SAMPLER_OP_TEXTURE);
3213 }
3214
3215 static void
3216 tg4_emit(
3217    const struct lp_build_tgsi_action * action,
3218    struct lp_build_tgsi_context * bld_base,
3219    struct lp_build_emit_data * emit_data)
3220 {
3221    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3222
3223    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3224             emit_data->output, 2, LP_SAMPLER_OP_GATHER);
3225 }
3226
3227 static void
3228 lodq_emit(
3229    const struct lp_build_tgsi_action * action,
3230    struct lp_build_tgsi_context * bld_base,
3231    struct lp_build_emit_data * emit_data)
3232 {
3233    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3234
3235    emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3236             emit_data->output, 1, LP_SAMPLER_OP_LODQ);
3237 }
3238
3239 static void
3240 txq_emit(
3241    const struct lp_build_tgsi_action * action,
3242    struct lp_build_tgsi_context * bld_base,
3243    struct lp_build_emit_data * emit_data)
3244 {
3245    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3246
3247    emit_size_query(bld, emit_data->inst, emit_data->output, FALSE);
3248 }
3249
3250 static void
3251 txf_emit(
3252    const struct lp_build_tgsi_action * action,
3253    struct lp_build_tgsi_context * bld_base,
3254    struct lp_build_emit_data * emit_data)
3255 {
3256    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3257
3258    emit_fetch_texels(bld, emit_data->inst, emit_data->output, FALSE);
3259 }
3260
3261 static void
3262 sample_i_emit(
3263    const struct lp_build_tgsi_action * action,
3264    struct lp_build_tgsi_context * bld_base,
3265    struct lp_build_emit_data * emit_data)
3266 {
3267    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3268
3269    emit_fetch_texels(bld, emit_data->inst, emit_data->output, TRUE);
3270 }
3271
3272 static void
3273 sample_emit(
3274    const struct lp_build_tgsi_action * action,
3275    struct lp_build_tgsi_context * bld_base,
3276    struct lp_build_emit_data * emit_data)
3277 {
3278    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3279
3280    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3281                FALSE, LP_SAMPLER_OP_TEXTURE, emit_data->output);
3282 }
3283
3284 static void
3285 sample_b_emit(
3286    const struct lp_build_tgsi_action * action,
3287    struct lp_build_tgsi_context * bld_base,
3288    struct lp_build_emit_data * emit_data)
3289 {
3290    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3291
3292    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_BIAS,
3293                FALSE, LP_SAMPLER_OP_TEXTURE, emit_data->output);
3294 }
3295
3296 static void
3297 sample_c_emit(
3298    const struct lp_build_tgsi_action * action,
3299    struct lp_build_tgsi_context * bld_base,
3300    struct lp_build_emit_data * emit_data)
3301 {
3302    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3303
3304    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3305                TRUE, LP_SAMPLER_OP_TEXTURE, emit_data->output);
3306 }
3307
3308 static void
3309 sample_c_lz_emit(
3310    const struct lp_build_tgsi_action * action,
3311    struct lp_build_tgsi_context * bld_base,
3312    struct lp_build_emit_data * emit_data)
3313 {
3314    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3315
3316    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_ZERO,
3317                TRUE, LP_SAMPLER_OP_TEXTURE, emit_data->output);
3318 }
3319
3320 static void
3321 sample_d_emit(
3322    const struct lp_build_tgsi_action * action,
3323    struct lp_build_tgsi_context * bld_base,
3324    struct lp_build_emit_data * emit_data)
3325 {
3326    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3327
3328    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
3329                FALSE, LP_SAMPLER_OP_TEXTURE, emit_data->output);
3330 }
3331
3332 static void
3333 sample_l_emit(
3334    const struct lp_build_tgsi_action * action,
3335    struct lp_build_tgsi_context * bld_base,
3336    struct lp_build_emit_data * emit_data)
3337 {
3338    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3339
3340    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
3341                FALSE, LP_SAMPLER_OP_TEXTURE, emit_data->output);
3342 }
3343
3344 static void
3345 gather4_emit(
3346    const struct lp_build_tgsi_action * action,
3347    struct lp_build_tgsi_context * bld_base,
3348    struct lp_build_emit_data * emit_data)
3349 {
3350    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3351
3352    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3353                FALSE, LP_SAMPLER_OP_GATHER, emit_data->output);
3354 }
3355
3356 static void
3357 sviewinfo_emit(
3358    const struct lp_build_tgsi_action * action,
3359    struct lp_build_tgsi_context * bld_base,
3360    struct lp_build_emit_data * emit_data)
3361 {
3362    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3363
3364    emit_size_query(bld, emit_data->inst, emit_data->output, TRUE);
3365 }
3366
3367 static void
3368 lod_emit(
3369    const struct lp_build_tgsi_action * action,
3370    struct lp_build_tgsi_context * bld_base,
3371    struct lp_build_emit_data * emit_data)
3372 {
3373    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3374
3375    emit_sample(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE,
3376                FALSE, LP_SAMPLER_OP_LODQ, emit_data->output);
3377 }
3378
3379 static void
3380 target_to_dims_layer(enum tgsi_texture_type target,
3381                      unsigned *dims,
3382                      unsigned *layer_coord)
3383 {
3384    *layer_coord = 0;
3385    switch (target) {
3386    case TGSI_TEXTURE_1D:
3387    case TGSI_TEXTURE_BUFFER:
3388       *dims = 1;
3389       break;
3390    case TGSI_TEXTURE_1D_ARRAY:
3391       *layer_coord = 1;
3392       *dims = 1;
3393       break;
3394    case TGSI_TEXTURE_2D:
3395    case TGSI_TEXTURE_RECT:
3396       *dims = 2;
3397       break;
3398    case TGSI_TEXTURE_2D_ARRAY:
3399       *layer_coord = 2;
3400       *dims = 2;
3401       break;
3402    case TGSI_TEXTURE_3D:
3403    case TGSI_TEXTURE_CUBE:
3404    case TGSI_TEXTURE_CUBE_ARRAY:
3405       *dims = 3;
3406       break;
3407    default:
3408       assert(0);
3409       *dims = 0;
3410       return;
3411    }
3412 }
3413
3414 static void
3415 img_load_emit(
3416    const struct lp_build_tgsi_action * action,
3417    struct lp_build_tgsi_context * bld_base,
3418    struct lp_build_emit_data * emit_data)
3419 {
3420    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3421    struct lp_img_params params;
3422    LLVMValueRef coords[5];
3423    LLVMValueRef coord_undef = LLVMGetUndef(bld->bld_base.base.int_vec_type);
3424    unsigned dims;
3425    enum tgsi_texture_type target = emit_data->inst->Memory.Texture;
3426    unsigned layer_coord;
3427
3428    target_to_dims_layer(target, &dims, &layer_coord);
3429
3430    for (unsigned i = 0; i < dims; i++) {
3431       coords[i] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, i);
3432    }
3433    for (unsigned i = dims; i < 5; i++) {
3434       coords[i] = coord_undef;
3435    }
3436    if (layer_coord)
3437       coords[2] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, layer_coord);
3438
3439    memset(&params, 0, sizeof(params));
3440
3441    params.type = bld->bld_base.base.type;
3442    params.context_ptr = bld->context_ptr;
3443    params.thread_data_ptr = bld->thread_data_ptr;
3444    params.coords = coords;
3445    params.outdata = emit_data->output;
3446    params.target = tgsi_to_pipe_tex_target(target);
3447    params.image_index = emit_data->inst->Src[0].Register.Index;
3448    params.img_op = LP_IMG_LOAD;
3449    bld->image->emit_op(bld->image,
3450                          bld->bld_base.base.gallivm,
3451                          &params);
3452 }
3453
3454 static void
3455 load_emit(
3456    const struct lp_build_tgsi_action * action,
3457    struct lp_build_tgsi_context * bld_base,
3458    struct lp_build_emit_data * emit_data)
3459 {
3460    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3461    struct gallivm_state * gallivm = bld_base->base.gallivm;
3462    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
3463    const struct tgsi_full_src_register *bufreg = &emit_data->inst->Src[0];
3464    unsigned buf = bufreg->Register.Index;
3465    assert(bufreg->Register.File == TGSI_FILE_BUFFER ||
3466           bufreg->Register.File == TGSI_FILE_IMAGE ||
3467           bufreg->Register.File == TGSI_FILE_MEMORY ||
3468           bufreg->Register.File == TGSI_FILE_CONSTBUF);
3469    bool is_shared = bufreg->Register.File == TGSI_FILE_MEMORY;
3470    struct lp_build_context *uint_bld = &bld_base->uint_bld;
3471
3472    if (bufreg->Register.File == TGSI_FILE_IMAGE) {
3473       img_load_emit(action, bld_base, emit_data);
3474    } else if (bufreg->Register.File == TGSI_FILE_CONSTBUF) {
3475       LLVMValueRef consts_ptr = bld->consts[buf];
3476       LLVMValueRef num_consts = bld->consts_sizes[buf];
3477
3478       LLVMValueRef indirect_index;
3479       LLVMValueRef overflow_mask;
3480
3481       indirect_index = lp_build_emit_fetch(bld_base, emit_data->inst, 1, 0);
3482       indirect_index = lp_build_shr_imm(uint_bld, indirect_index, 4);
3483
3484       /* All fetches are from the same constant buffer, so
3485        * we need to propagate the size to a vector to do a
3486        * vector comparison */
3487       num_consts = lp_build_broadcast_scalar(uint_bld, num_consts);
3488
3489       /* Gather values from the constant buffer */
3490       unsigned chan_index;
3491       TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(emit_data->inst, chan_index) {
3492          /* Construct a boolean vector telling us which channels
3493           * overflow the bound constant buffer */
3494          overflow_mask = lp_build_compare(gallivm, uint_bld->type, PIPE_FUNC_GEQUAL,
3495                                           indirect_index, num_consts);
3496
3497          /* index_vec = indirect_index * 4 */
3498          LLVMValueRef index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
3499          index_vec = lp_build_add(uint_bld, index_vec,
3500                                   lp_build_const_int_vec(gallivm, uint_bld->type, chan_index));
3501
3502          emit_data->output[chan_index] = build_gather(bld_base, consts_ptr, index_vec, overflow_mask, NULL);
3503       }
3504    } else if (0) {
3505       /* for indirect support with ARB_gpu_shader5 */
3506    } else {
3507       LLVMValueRef index;
3508       LLVMValueRef scalar, scalar_ptr;
3509       unsigned chan_index;
3510
3511       index = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, 0);
3512       index = lp_build_shr_imm(uint_bld, index, 2);
3513
3514       scalar_ptr = is_shared ? bld->shared_ptr : bld->ssbos[buf];
3515
3516       LLVMValueRef ssbo_limit = NULL;
3517
3518       if (!is_shared) {
3519          ssbo_limit = LLVMBuildAShr(gallivm->builder, bld->ssbo_sizes[buf], lp_build_const_int32(gallivm, 2), "");
3520          ssbo_limit = lp_build_broadcast_scalar(uint_bld, ssbo_limit);
3521       }
3522
3523       TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(emit_data->inst, chan_index) {
3524          LLVMValueRef loop_index = lp_build_add(uint_bld, index, lp_build_const_int_vec(gallivm, uint_bld->type, chan_index));
3525
3526          LLVMValueRef exec_mask = mask_vec(bld_base);
3527          if (!is_shared) {
3528             LLVMValueRef ssbo_oob_cmp = lp_build_cmp(uint_bld, PIPE_FUNC_LESS, loop_index, ssbo_limit);
3529             exec_mask = LLVMBuildAnd(builder, exec_mask, ssbo_oob_cmp, "");
3530          }
3531
3532          LLVMValueRef result = lp_build_alloca(gallivm, uint_bld->vec_type, "");
3533          struct lp_build_loop_state loop_state;
3534          lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
3535
3536          struct lp_build_if_state ifthen;
3537          LLVMValueRef cond, temp_res;
3538
3539          loop_index = LLVMBuildExtractElement(gallivm->builder, loop_index,
3540                                               loop_state.counter, "");
3541
3542          cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
3543          cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
3544
3545          lp_build_if(&ifthen, gallivm, cond);
3546          scalar = lp_build_pointer_get(builder, scalar_ptr, loop_index);
3547
3548          temp_res = LLVMBuildLoad(builder, result, "");
3549          temp_res = LLVMBuildInsertElement(builder, temp_res, scalar, loop_state.counter, "");
3550          LLVMBuildStore(builder, temp_res, result);
3551          lp_build_else(&ifthen);
3552          temp_res = LLVMBuildLoad(builder, result, "");
3553          temp_res = LLVMBuildInsertElement(builder, temp_res, lp_build_const_int32(gallivm, 0), loop_state.counter, "");
3554          LLVMBuildStore(builder, temp_res, result);
3555          lp_build_endif(&ifthen);
3556          lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
3557                                 NULL, LLVMIntUGE);
3558          emit_data->output[chan_index] = LLVMBuildLoad(gallivm->builder, result, "");
3559       }
3560    }
3561 }
3562
3563 static void
3564 img_store_emit(
3565    const struct lp_build_tgsi_action * action,
3566    struct lp_build_tgsi_context * bld_base,
3567    struct lp_build_emit_data * emit_data)
3568 {
3569    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3570    struct lp_img_params params;
3571    LLVMValueRef coords[5];
3572    LLVMValueRef coord_undef = LLVMGetUndef(bld->bld_base.base.int_vec_type);
3573    unsigned dims;
3574    enum tgsi_texture_type target = emit_data->inst->Memory.Texture;
3575    unsigned layer_coord;
3576
3577    target_to_dims_layer(target, &dims, &layer_coord);
3578    for (unsigned i = 0; i < dims; i++) {
3579       coords[i] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, i);
3580    }
3581    for (unsigned i = dims; i < 5; i++) {
3582       coords[i] = coord_undef;
3583    }
3584    if (layer_coord)
3585       coords[2] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, layer_coord);
3586    memset(&params, 0, sizeof(params));
3587
3588    params.type = bld->bld_base.base.type;
3589    params.context_ptr = bld->context_ptr;
3590    params.thread_data_ptr = bld->thread_data_ptr;
3591    params.coords = coords;
3592    params.outdata = NULL;
3593    params.exec_mask = mask_vec(bld_base);
3594    params.target = tgsi_to_pipe_tex_target(target);
3595    params.image_index = emit_data->inst->Dst[0].Register.Index;
3596    params.img_op = LP_IMG_STORE;
3597    for (unsigned i = 0; i < 4; i++)
3598       params.indata[i] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, i);
3599
3600    bld->image->emit_op(bld->image,
3601                        bld->bld_base.base.gallivm,
3602                        &params);
3603 }
3604
3605 static void
3606 store_emit(
3607    const struct lp_build_tgsi_action * action,
3608    struct lp_build_tgsi_context * bld_base,
3609    struct lp_build_emit_data * emit_data)
3610 {
3611    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3612    struct gallivm_state * gallivm = bld_base->base.gallivm;
3613    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
3614    struct lp_build_context *uint_bld = &bld_base->uint_bld;
3615    const struct tgsi_full_dst_register *bufreg = &emit_data->inst->Dst[0];
3616    unsigned buf = bufreg->Register.Index;
3617    assert(bufreg->Register.File == TGSI_FILE_BUFFER || bufreg->Register.File == TGSI_FILE_IMAGE || bufreg->Register.File == TGSI_FILE_MEMORY);
3618    bool is_shared = bufreg->Register.File == TGSI_FILE_MEMORY;
3619
3620    if (bufreg->Register.File == TGSI_FILE_IMAGE) {
3621       img_store_emit(action, bld_base, emit_data);
3622    } else if (0) {
3623
3624    } else {
3625       LLVMValueRef index;  /* index into the const buffer */
3626       LLVMValueRef scalar_ptr;
3627       LLVMValueRef value;
3628       unsigned chan_index;
3629
3630       index = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, 0);
3631       index = lp_build_shr_imm(uint_bld, index, 2);
3632
3633       scalar_ptr = is_shared ? bld->shared_ptr : bld->ssbos[buf];
3634
3635       LLVMValueRef ssbo_limit = NULL;
3636
3637       if (!is_shared) {
3638          ssbo_limit = LLVMBuildAShr(gallivm->builder, bld->ssbo_sizes[buf], lp_build_const_int32(gallivm, 2), "");
3639          ssbo_limit = lp_build_broadcast_scalar(uint_bld, ssbo_limit);
3640       }
3641
3642       TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(emit_data->inst, chan_index) {
3643          LLVMValueRef loop_index = lp_build_add(uint_bld, index, lp_build_const_int_vec(gallivm, uint_bld->type, chan_index));
3644
3645          value = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, chan_index);
3646
3647          LLVMValueRef exec_mask = mask_vec(bld_base);
3648          if (!is_shared) {
3649             LLVMValueRef ssbo_oob_cmp = lp_build_cmp(uint_bld, PIPE_FUNC_LESS, loop_index, ssbo_limit);
3650             exec_mask = LLVMBuildAnd(builder, exec_mask, ssbo_oob_cmp, "");
3651          }
3652
3653          struct lp_build_loop_state loop_state;
3654          lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
3655
3656          LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, value,
3657                                                           loop_state.counter, "");
3658          value_ptr = LLVMBuildBitCast(gallivm->builder, value_ptr, uint_bld->elem_type, "");
3659
3660          struct lp_build_if_state ifthen;
3661          LLVMValueRef cond;
3662
3663          loop_index = LLVMBuildExtractElement(gallivm->builder, loop_index,
3664                                               loop_state.counter, "");
3665
3666          cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
3667          cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
3668          lp_build_if(&ifthen, gallivm, cond);
3669
3670          lp_build_pointer_set(builder, scalar_ptr, loop_index, value_ptr);
3671
3672          lp_build_endif(&ifthen);
3673          lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
3674                                 NULL, LLVMIntUGE);
3675       }
3676    }
3677 }
3678
3679 static void
3680 resq_emit(
3681    const struct lp_build_tgsi_action * action,
3682    struct lp_build_tgsi_context * bld_base,
3683    struct lp_build_emit_data * emit_data)
3684 {
3685    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3686    struct lp_build_context *uint_bld = &bld_base->uint_bld;
3687    const struct tgsi_full_src_register *bufreg = &emit_data->inst->Src[0];
3688
3689    unsigned buf = bufreg->Register.Index;
3690    assert(bufreg->Register.File == TGSI_FILE_BUFFER || bufreg->Register.File == TGSI_FILE_IMAGE);
3691
3692    if (bufreg->Register.File == TGSI_FILE_IMAGE) {
3693       enum tgsi_texture_type target = emit_data->inst->Memory.Texture;
3694       struct lp_sampler_size_query_params params = { 0 };
3695       params.int_type = bld->bld_base.int_bld.type;
3696       params.texture_unit = buf;
3697       params.target = tgsi_to_pipe_tex_target(target);
3698       params.context_ptr = bld->context_ptr;
3699       params.sizes_out = emit_data->output;
3700
3701       bld->image->emit_size_query(bld->image,
3702                                   bld->bld_base.base.gallivm,
3703                                   &params);
3704    } else {
3705       LLVMValueRef num_ssbo = bld->ssbo_sizes[buf];
3706
3707       emit_data->output[emit_data->chan] = lp_build_broadcast_scalar(uint_bld, num_ssbo);
3708    }
3709 }
3710
3711 static void
3712 img_atomic_emit(
3713    const struct lp_build_tgsi_action * action,
3714    struct lp_build_tgsi_context * bld_base,
3715    struct lp_build_emit_data * emit_data,
3716    LLVMAtomicRMWBinOp op)
3717 {
3718    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3719    struct lp_img_params params;
3720    LLVMValueRef coords[5];
3721    LLVMValueRef coord_undef = LLVMGetUndef(bld->bld_base.base.int_vec_type);
3722    unsigned dims;
3723    unsigned layer_coord;
3724    enum tgsi_texture_type target = emit_data->inst->Memory.Texture;
3725
3726    target_to_dims_layer(target, &dims, &layer_coord);
3727
3728    for (unsigned i = 0; i < dims; i++) {
3729       coords[i] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, i);
3730    }
3731    for (unsigned i = dims; i < 5; i++) {
3732       coords[i] = coord_undef;
3733    }
3734    if (layer_coord)
3735       coords[2] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, layer_coord);
3736    memset(&params, 0, sizeof(params));
3737
3738    params.type = bld->bld_base.base.type;
3739    params.context_ptr = bld->context_ptr;
3740    params.thread_data_ptr = bld->thread_data_ptr;
3741    params.exec_mask = mask_vec(bld_base);
3742    params.image_index = emit_data->inst->Src[0].Register.Index;
3743    params.coords = coords;
3744    params.target = tgsi_to_pipe_tex_target(target);
3745    params.op = op;
3746    params.outdata = emit_data->output;
3747    params.img_op = (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) ? LP_IMG_ATOMIC_CAS : LP_IMG_ATOMIC;
3748
3749    for (unsigned i = 0; i < 4; i++)
3750       params.indata[i] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 2, i);
3751    if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
3752       for (unsigned i = 0; i < 4; i++)
3753          params.indata2[i] = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 3, i);
3754    }
3755    bld->image->emit_op(bld->image,
3756                        bld->bld_base.base.gallivm,
3757                        &params);
3758 }
3759
3760 static void
3761 atomic_emit(
3762    const struct lp_build_tgsi_action * action,
3763    struct lp_build_tgsi_context * bld_base,
3764    struct lp_build_emit_data * emit_data)
3765 {
3766    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3767    struct gallivm_state * gallivm = bld_base->base.gallivm;
3768    LLVMBuilderRef builder = gallivm->builder;
3769    struct lp_build_context *uint_bld = &bld_base->uint_bld;
3770    const struct tgsi_full_src_register *bufreg = &emit_data->inst->Src[0];
3771
3772    assert(bufreg->Register.File == TGSI_FILE_BUFFER || bufreg->Register.File == TGSI_FILE_IMAGE || bufreg->Register.File == TGSI_FILE_MEMORY);
3773    unsigned buf = bufreg->Register.Index;
3774    bool is_shared = bufreg->Register.File == TGSI_FILE_MEMORY;
3775
3776    LLVMAtomicRMWBinOp op = -1;
3777    switch (emit_data->inst->Instruction.Opcode) {
3778    case TGSI_OPCODE_ATOMUADD:
3779       op = LLVMAtomicRMWBinOpAdd;
3780       break;
3781    case TGSI_OPCODE_ATOMXCHG:
3782       op = LLVMAtomicRMWBinOpXchg;
3783       break;
3784    case TGSI_OPCODE_ATOMAND:
3785       op = LLVMAtomicRMWBinOpAnd;
3786       break;
3787    case TGSI_OPCODE_ATOMOR:
3788       op = LLVMAtomicRMWBinOpOr;
3789       break;
3790    case TGSI_OPCODE_ATOMXOR:
3791       op = LLVMAtomicRMWBinOpXor;
3792       break;
3793    case TGSI_OPCODE_ATOMUMIN:
3794       op = LLVMAtomicRMWBinOpUMin;
3795       break;
3796    case TGSI_OPCODE_ATOMUMAX:
3797       op = LLVMAtomicRMWBinOpUMax;
3798       break;
3799    case TGSI_OPCODE_ATOMIMIN:
3800       op = LLVMAtomicRMWBinOpMin;
3801       break;
3802    case TGSI_OPCODE_ATOMIMAX:
3803       op = LLVMAtomicRMWBinOpMax;
3804       break;
3805    case TGSI_OPCODE_ATOMCAS:
3806       break;
3807    default:
3808       assert(0);
3809       return;
3810    }
3811
3812    if (bufreg->Register.File == TGSI_FILE_IMAGE) {
3813       img_atomic_emit(action, bld_base, emit_data, op);
3814    } else if (0) {
3815    } else {
3816       LLVMValueRef index;  /* index into the const buffer */
3817       LLVMValueRef scalar, scalar_ptr;
3818       LLVMValueRef value;
3819
3820       index = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 1, 0);
3821       value = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 2, 0);
3822
3823       index = lp_build_shr_imm(uint_bld, index, 2);
3824
3825       if (!is_shared) {
3826          index = lp_build_add(uint_bld, index, lp_build_const_int_vec(gallivm, uint_bld->type, emit_data->chan));
3827          scalar_ptr = bld->ssbos[buf];
3828       } else
3829          scalar_ptr = bld->shared_ptr;
3830
3831       LLVMValueRef atom_res = lp_build_alloca(gallivm,
3832                                               uint_bld->vec_type, "");
3833
3834       LLVMValueRef ssbo_limit;
3835       if (!is_shared) {
3836          ssbo_limit = LLVMBuildAShr(gallivm->builder, bld->ssbo_sizes[buf], lp_build_const_int32(gallivm, 2), "");
3837          ssbo_limit = lp_build_broadcast_scalar(uint_bld, ssbo_limit);
3838       }
3839
3840       LLVMValueRef exec_mask = mask_vec(bld_base);
3841
3842       if (!is_shared) {
3843          LLVMValueRef ssbo_oob_cmp = lp_build_cmp(uint_bld, PIPE_FUNC_LESS, index, ssbo_limit);
3844          exec_mask = LLVMBuildAnd(builder, exec_mask, ssbo_oob_cmp, "");
3845       }
3846
3847       struct lp_build_loop_state loop_state;
3848       lp_build_loop_begin(&loop_state, gallivm, lp_build_const_int32(gallivm, 0));
3849
3850       LLVMValueRef value_ptr = LLVMBuildExtractElement(gallivm->builder, value,
3851                                                        loop_state.counter, "");
3852       value_ptr = LLVMBuildBitCast(gallivm->builder, value_ptr, uint_bld->elem_type, "");
3853
3854       index = LLVMBuildExtractElement(gallivm->builder, index,
3855                                       loop_state.counter, "");
3856
3857       scalar_ptr = LLVMBuildGEP(builder, scalar_ptr,
3858                                 &index, 1, "");
3859
3860       struct lp_build_if_state ifthen;
3861       LLVMValueRef cond, temp_res;
3862
3863       cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, "");
3864       cond = LLVMBuildExtractElement(gallivm->builder, cond, loop_state.counter, "");
3865       lp_build_if(&ifthen, gallivm, cond);
3866
3867       if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
3868          LLVMValueRef cas_src = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 3, 0);
3869          LLVMValueRef cas_src_ptr = LLVMBuildExtractElement(gallivm->builder, cas_src,
3870                                                             loop_state.counter, "");
3871          cas_src_ptr = LLVMBuildBitCast(gallivm->builder, cas_src_ptr, uint_bld->elem_type, "");
3872          scalar = LLVMBuildAtomicCmpXchg(builder, scalar_ptr, value_ptr,
3873                                          cas_src_ptr,
3874                                          LLVMAtomicOrderingSequentiallyConsistent,
3875                                          LLVMAtomicOrderingSequentiallyConsistent,
3876                                          false);
3877          scalar = LLVMBuildExtractValue(gallivm->builder, scalar, 0, "");
3878       } else {
3879          scalar = LLVMBuildAtomicRMW(builder, op,
3880                                      scalar_ptr, value_ptr,
3881                                      LLVMAtomicOrderingSequentiallyConsistent,
3882                                      false);
3883       }
3884       temp_res = LLVMBuildLoad(builder, atom_res, "");
3885       temp_res = LLVMBuildInsertElement(builder, temp_res, scalar, loop_state.counter, "");
3886       LLVMBuildStore(builder, temp_res, atom_res);
3887       lp_build_else(&ifthen);
3888       temp_res = LLVMBuildLoad(builder, atom_res, "");
3889       temp_res = LLVMBuildInsertElement(builder, temp_res, lp_build_const_int32(gallivm, 0), loop_state.counter, "");
3890       LLVMBuildStore(builder, temp_res, atom_res);
3891       lp_build_endif(&ifthen);
3892
3893       lp_build_loop_end_cond(&loop_state, lp_build_const_int32(gallivm, uint_bld->type.length),
3894                              NULL, LLVMIntUGE);
3895       emit_data->output[emit_data->chan] = LLVMBuildLoad(gallivm->builder, atom_res, "");
3896    }
3897 }
3898
3899 static void
3900 barrier_emit(
3901    const struct lp_build_tgsi_action * action,
3902    struct lp_build_tgsi_context * bld_base,
3903    struct lp_build_emit_data * emit_data)
3904 {
3905    struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
3906    struct gallivm_state * gallivm = bld_base->base.gallivm;
3907
3908    LLVMBasicBlockRef resume = lp_build_insert_new_block(gallivm, "resume");
3909
3910    lp_build_coro_suspend_switch(gallivm, bld->coro, resume, false);
3911    LLVMPositionBuilderAtEnd(gallivm->builder, resume);
3912 }
3913
3914 static void
3915 membar_emit(
3916    const struct lp_build_tgsi_action * action,
3917    struct lp_build_tgsi_context * bld_base,
3918    struct lp_build_emit_data * emit_data)
3919 {
3920    LLVMBuilderRef builder = bld_base->base.gallivm->builder;
3921    LLVMBuildFence(builder, LLVMAtomicOrderingSequentiallyConsistent, false, "");
3922 }
3923
3924 static void
3925 increment_vec_ptr_by_mask(struct lp_build_tgsi_context * bld_base,
3926                           LLVMValueRef ptr,
3927                           LLVMValueRef mask)
3928 {
3929    LLVMBuilderRef builder = bld_base->base.gallivm->builder;
3930    LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
3931
3932    current_vec = LLVMBuildSub(builder, current_vec, mask, "");
3933
3934    LLVMBuildStore(builder, current_vec, ptr);
3935 }
3936
3937 static void
3938 clear_uint_vec_ptr_from_mask(struct lp_build_tgsi_context * bld_base,
3939                              LLVMValueRef ptr,
3940                              LLVMValueRef mask)
3941 {
3942    LLVMBuilderRef builder = bld_base->base.gallivm->builder;
3943    LLVMValueRef current_vec = LLVMBuildLoad(builder, ptr, "");
3944
3945    current_vec = lp_build_select(&bld_base->uint_bld,
3946                                  mask,
3947                                  bld_base->uint_bld.zero,
3948                                  current_vec);
3949
3950    LLVMBuildStore(builder, current_vec, ptr);
3951 }
3952
3953 static LLVMValueRef
3954 clamp_mask_to_max_output_vertices(struct lp_build_tgsi_soa_context * bld,
3955                                   LLVMValueRef current_mask_vec,
3956                                   LLVMValueRef total_emitted_vertices_vec)
3957 {
3958    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
3959    struct lp_build_context *int_bld = &bld->bld_base.int_bld;
3960    LLVMValueRef max_mask = lp_build_cmp(int_bld, PIPE_FUNC_LESS,
3961                                         total_emitted_vertices_vec,
3962                                         bld->max_output_vertices_vec);
3963
3964    return LLVMBuildAnd(builder, current_mask_vec, max_mask, "");
3965 }
3966
3967 static void
3968 emit_vertex(
3969    const struct lp_build_tgsi_action * action,
3970    struct lp_build_tgsi_context * bld_base,
3971    struct lp_build_emit_data * emit_data)
3972 {
3973    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
3974    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
3975
3976    if (bld->gs_iface->emit_vertex) {
3977       LLVMValueRef stream_id = emit_fetch_immediate(bld_base, &emit_data->inst->Src[0],
3978                                                     TGSI_TYPE_UNSIGNED,
3979                                                     emit_data->inst->Src[0].Register.SwizzleX);
3980       LLVMValueRef mask = mask_vec(bld_base);
3981       LLVMValueRef total_emitted_vertices_vec =
3982          LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr, "");
3983
3984       mask = clamp_mask_to_max_output_vertices(bld, mask,
3985                                                total_emitted_vertices_vec);
3986       gather_outputs(bld);
3987       bld->gs_iface->emit_vertex(bld->gs_iface, &bld->bld_base.base,
3988                                  bld->outputs,
3989                                  total_emitted_vertices_vec,
3990                                  mask,
3991                                  stream_id);
3992       increment_vec_ptr_by_mask(bld_base, bld->emitted_vertices_vec_ptr,
3993                                 mask);
3994       increment_vec_ptr_by_mask(bld_base, bld->total_emitted_vertices_vec_ptr,
3995                                 mask);
3996 #if DUMP_GS_EMITS
3997       lp_build_print_value(bld->bld_base.base.gallivm,
3998                            " +++ emit vertex masked ones = ",
3999                            mask);
4000       lp_build_print_value(bld->bld_base.base.gallivm,
4001                            " +++ emit vertex emitted = ",
4002                            total_emitted_vertices_vec);
4003 #endif
4004    }
4005 }
4006
4007
4008 static void
4009 end_primitive_masked(struct lp_build_tgsi_context * bld_base,
4010                      LLVMValueRef mask)
4011 {
4012    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4013    LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
4014
4015    if (bld->gs_iface->end_primitive) {
4016       struct lp_build_context *uint_bld = &bld_base->uint_bld;
4017       LLVMValueRef emitted_vertices_vec =
4018          LLVMBuildLoad(builder, bld->emitted_vertices_vec_ptr, "");
4019       LLVMValueRef emitted_prims_vec =
4020          LLVMBuildLoad(builder, bld->emitted_prims_vec_ptr, "");
4021       LLVMValueRef total_emitted_vertices_vec =
4022          LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr, "");
4023       LLVMValueRef emitted_mask = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL,
4024                                                emitted_vertices_vec,
4025                                                uint_bld->zero);
4026       /* We need to combine the current execution mask with the mask
4027          telling us which, if any, execution slots actually have
4028          unemitted primitives, this way we make sure that end_primitives
4029          executes only on the paths that have unflushed vertices */
4030       mask = LLVMBuildAnd(builder, mask, emitted_mask, "");
4031
4032       bld->gs_iface->end_primitive(bld->gs_iface, &bld->bld_base.base,
4033                                    total_emitted_vertices_vec,
4034                                    emitted_vertices_vec,
4035                                    emitted_prims_vec,
4036                                    mask_vec(bld_base), 0);
4037
4038 #if DUMP_GS_EMITS
4039       lp_build_print_value(bld->bld_base.base.gallivm,
4040                            " +++ end prim masked ones = ",
4041                            mask);
4042       lp_build_print_value(bld->bld_base.base.gallivm,
4043                            " +++ end prim emitted verts1 = ",
4044                            emitted_vertices_vec);
4045       lp_build_print_value(bld->bld_base.base.gallivm,
4046                            " +++ end prim emitted prims1 = ",
4047                            LLVMBuildLoad(builder,
4048                                          bld->emitted_prims_vec_ptr, ""));
4049 #endif
4050       increment_vec_ptr_by_mask(bld_base, bld->emitted_prims_vec_ptr,
4051                                 mask);
4052       clear_uint_vec_ptr_from_mask(bld_base, bld->emitted_vertices_vec_ptr,
4053                                    mask);
4054 #if DUMP_GS_EMITS
4055       lp_build_print_value(bld->bld_base.base.gallivm,
4056                            " +++ end prim emitted verts2 = ",
4057                            LLVMBuildLoad(builder,
4058                                          bld->emitted_vertices_vec_ptr, ""));
4059 #endif
4060    }
4061
4062 }
4063
4064 static void
4065 end_primitive(
4066    const struct lp_build_tgsi_action * action,
4067    struct lp_build_tgsi_context * bld_base,
4068    struct lp_build_emit_data * emit_data)
4069 {
4070    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4071
4072    if (bld->gs_iface->end_primitive) {
4073       LLVMValueRef mask = mask_vec(bld_base);
4074       end_primitive_masked(bld_base, mask);
4075    }
4076 }
4077
4078 static void
4079 barrier_emit_tcs(
4080    const struct lp_build_tgsi_action * action,
4081    struct lp_build_tgsi_context * bld_base,
4082    struct lp_build_emit_data * emit_data)
4083 {
4084    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4085
4086    if (bld->tcs_iface->emit_barrier) {
4087       bld->tcs_iface->emit_barrier((struct lp_build_context*)bld_base);
4088    }
4089 }
4090
4091
4092 static void
4093 cal_emit(
4094    const struct lp_build_tgsi_action * action,
4095    struct lp_build_tgsi_context * bld_base,
4096    struct lp_build_emit_data * emit_data)
4097 {
4098    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4099
4100    lp_exec_mask_call(&bld->exec_mask, emit_data->inst->Label.Label,
4101                      &bld_base->pc);
4102 }
4103
4104 static void
4105 ret_emit(
4106    const struct lp_build_tgsi_action * action,
4107    struct lp_build_tgsi_context * bld_base,
4108    struct lp_build_emit_data * emit_data)
4109 {
4110    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4111
4112    lp_exec_mask_ret(&bld->exec_mask, &bld_base->pc);
4113 }
4114
4115 static void
4116 brk_emit(
4117    const struct lp_build_tgsi_action * action,
4118    struct lp_build_tgsi_context * bld_base,
4119    struct lp_build_emit_data * emit_data)
4120 {
4121    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4122
4123    lp_exec_tgsi_break(&bld->exec_mask, bld_base);
4124 }
4125
4126 static void
4127 if_emit(
4128    const struct lp_build_tgsi_action * action,
4129    struct lp_build_tgsi_context * bld_base,
4130    struct lp_build_emit_data * emit_data)
4131 {
4132    LLVMValueRef tmp;
4133    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4134
4135    tmp = lp_build_cmp(&bld_base->base, PIPE_FUNC_NOTEQUAL,
4136                       emit_data->args[0], bld->bld_base.base.zero);
4137    lp_exec_mask_cond_push(&bld->exec_mask, tmp);
4138 }
4139
4140 static void
4141 uif_emit(
4142    const struct lp_build_tgsi_action * action,
4143    struct lp_build_tgsi_context * bld_base,
4144    struct lp_build_emit_data * emit_data)
4145 {
4146    LLVMValueRef tmp;
4147    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4148    struct lp_build_context *uint_bld = &bld_base->uint_bld;
4149
4150    tmp = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL,
4151                       emit_data->args[0], uint_bld->zero);
4152    lp_exec_mask_cond_push(&bld->exec_mask, tmp);
4153 }
4154
4155 static void
4156 case_emit(
4157    const struct lp_build_tgsi_action * action,
4158    struct lp_build_tgsi_context * bld_base,
4159    struct lp_build_emit_data * emit_data)
4160 {
4161    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4162
4163    lp_exec_case(&bld->exec_mask, emit_data->args[0]);
4164 }
4165
4166 static void
4167 default_emit(
4168    const struct lp_build_tgsi_action * action,
4169    struct lp_build_tgsi_context * bld_base,
4170    struct lp_build_emit_data * emit_data)
4171 {
4172    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4173
4174    lp_exec_default(&bld->exec_mask, bld_base);
4175 }
4176
4177 static void
4178 switch_emit(
4179    const struct lp_build_tgsi_action * action,
4180    struct lp_build_tgsi_context * bld_base,
4181    struct lp_build_emit_data * emit_data)
4182 {
4183    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4184
4185    lp_exec_switch(&bld->exec_mask, emit_data->args[0]);
4186 }
4187
4188 static void
4189 endswitch_emit(
4190    const struct lp_build_tgsi_action * action,
4191    struct lp_build_tgsi_context * bld_base,
4192    struct lp_build_emit_data * emit_data)
4193 {
4194    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4195
4196    lp_exec_endswitch(&bld->exec_mask, bld_base);
4197 }
4198
4199 static void
4200 bgnloop_emit(
4201    const struct lp_build_tgsi_action * action,
4202    struct lp_build_tgsi_context * bld_base,
4203    struct lp_build_emit_data * emit_data)
4204 {
4205    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4206
4207    lp_exec_bgnloop(&bld->exec_mask, true);
4208 }
4209
4210 static void
4211 bgnsub_emit(
4212    const struct lp_build_tgsi_action * action,
4213    struct lp_build_tgsi_context * bld_base,
4214    struct lp_build_emit_data * emit_data)
4215 {
4216    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4217
4218    lp_exec_mask_bgnsub(&bld->exec_mask);
4219 }
4220
4221 static void
4222 else_emit(
4223    const struct lp_build_tgsi_action * action,
4224    struct lp_build_tgsi_context * bld_base,
4225    struct lp_build_emit_data * emit_data)
4226 {
4227    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4228
4229    lp_exec_mask_cond_invert(&bld->exec_mask);
4230 }
4231
4232 static void
4233 endif_emit(
4234    const struct lp_build_tgsi_action * action,
4235    struct lp_build_tgsi_context * bld_base,
4236    struct lp_build_emit_data * emit_data)
4237 {
4238    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4239
4240    lp_exec_mask_cond_pop(&bld->exec_mask);
4241 }
4242
4243 static void
4244 endloop_emit(
4245    const struct lp_build_tgsi_action * action,
4246    struct lp_build_tgsi_context * bld_base,
4247    struct lp_build_emit_data * emit_data)
4248 {
4249    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4250
4251    lp_exec_endloop(bld_base->base.gallivm, &bld->exec_mask);
4252 }
4253
4254 static void
4255 endsub_emit(
4256    const struct lp_build_tgsi_action * action,
4257    struct lp_build_tgsi_context * bld_base,
4258    struct lp_build_emit_data * emit_data)
4259 {
4260    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4261
4262    lp_exec_mask_endsub(&bld->exec_mask, &bld_base->pc);
4263 }
4264
4265 static void
4266 cont_emit(
4267    const struct lp_build_tgsi_action * action,
4268    struct lp_build_tgsi_context * bld_base,
4269    struct lp_build_emit_data * emit_data)
4270 {
4271    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4272
4273    lp_exec_continue(&bld->exec_mask);
4274 }
4275
4276 static void emit_prologue(struct lp_build_tgsi_context * bld_base)
4277 {
4278    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4279    struct gallivm_state * gallivm = bld_base->base.gallivm;
4280
4281    if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
4282       unsigned array_size = bld_base->info->file_max[TGSI_FILE_TEMPORARY] * 4 + 4;
4283       bld->temps_array = lp_build_alloca_undef(gallivm,
4284                                                LLVMArrayType(bld_base->base.vec_type, array_size),
4285                                                "temp_array");
4286    }
4287
4288    if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) {
4289       LLVMValueRef array_size =
4290          lp_build_const_int32(gallivm,
4291                             bld_base->info->file_max[TGSI_FILE_OUTPUT] * 4 + 4);
4292       bld->outputs_array = lp_build_array_alloca(gallivm,
4293                                                 bld_base->base.vec_type, array_size,
4294                                                 "output_array");
4295    }
4296
4297    if (bld->indirect_files & (1 << TGSI_FILE_IMMEDIATE)) {
4298       unsigned array_size = bld_base->info->file_max[TGSI_FILE_IMMEDIATE] * 4 + 4;
4299       bld->imms_array = lp_build_alloca_undef(gallivm,
4300                                               LLVMArrayType(bld_base->base.vec_type, array_size),
4301                                               "imms_array");
4302    }
4303
4304    /* If we have indirect addressing in inputs we need to copy them into
4305     * our alloca array to be able to iterate over them */
4306    if (bld->indirect_files & (1 << TGSI_FILE_INPUT) &&
4307        !bld->gs_iface && !bld->tes_iface && !bld->tcs_iface) {
4308       unsigned index, chan;
4309       LLVMTypeRef vec_type = bld_base->base.vec_type;
4310       LLVMValueRef array_size = lp_build_const_int32(gallivm,
4311             bld_base->info->file_max[TGSI_FILE_INPUT]*4 + 4);
4312       bld->inputs_array = lp_build_array_alloca(gallivm,
4313                                                vec_type, array_size,
4314                                                "input_array");
4315
4316       assert(bld_base->info->num_inputs
4317                         <= bld_base->info->file_max[TGSI_FILE_INPUT] + 1);
4318
4319       for (index = 0; index < bld_base->info->num_inputs; ++index) {
4320          for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
4321             LLVMValueRef lindex =
4322                lp_build_const_int32(gallivm, index * 4 + chan);
4323             LLVMValueRef input_ptr =
4324                LLVMBuildGEP(gallivm->builder, bld->inputs_array,
4325                             &lindex, 1, "");
4326             LLVMValueRef value = bld->inputs[index][chan];
4327             if (value)
4328                LLVMBuildStore(gallivm->builder, value, input_ptr);
4329          }
4330       }
4331    }
4332
4333    if (bld->gs_iface) {
4334       struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;
4335       bld->emitted_prims_vec_ptr =
4336          lp_build_alloca(gallivm,
4337                          uint_bld->vec_type,
4338                          "emitted_prims_ptr");
4339       bld->emitted_vertices_vec_ptr =
4340          lp_build_alloca(gallivm,
4341                          uint_bld->vec_type,
4342                          "emitted_vertices_ptr");
4343       bld->total_emitted_vertices_vec_ptr =
4344          lp_build_alloca(gallivm,
4345                          uint_bld->vec_type,
4346                          "total_emitted_vertices_ptr");
4347
4348       LLVMBuildStore(gallivm->builder, uint_bld->zero,
4349                      bld->emitted_prims_vec_ptr);
4350       LLVMBuildStore(gallivm->builder, uint_bld->zero,
4351                      bld->emitted_vertices_vec_ptr);
4352       LLVMBuildStore(gallivm->builder, uint_bld->zero,
4353                      bld->total_emitted_vertices_vec_ptr);
4354    }
4355
4356    if (DEBUG_EXECUTION) {
4357       lp_build_printf(gallivm, "\n");
4358       emit_dump_file(bld, TGSI_FILE_CONSTANT);
4359       if (!bld->gs_iface)
4360          emit_dump_file(bld, TGSI_FILE_INPUT);
4361    }
4362 }
4363
4364 static void emit_prologue_post_decl(struct lp_build_tgsi_context * bld_base)
4365 {
4366    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4367
4368    if (bld->tcs_iface && bld->tcs_iface->emit_prologue) {
4369       bld->tcs_iface->emit_prologue((struct lp_build_context*)bld_base);
4370    }
4371 }
4372
4373 static void emit_epilogue(struct lp_build_tgsi_context * bld_base)
4374 {
4375    struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
4376    LLVMBuilderRef builder = bld_base->base.gallivm->builder;
4377
4378    if (DEBUG_EXECUTION) {
4379       /* for debugging */
4380       if (0) {
4381          emit_dump_file(bld, TGSI_FILE_TEMPORARY);
4382       }
4383       emit_dump_file(bld, TGSI_FILE_OUTPUT);
4384       lp_build_printf(bld_base->base.gallivm, "\n");
4385    }
4386
4387    if (bld->tcs_iface && bld->tcs_iface->emit_epilogue) {
4388       bld->tcs_iface->emit_epilogue((struct lp_build_context*)bld_base);
4389    }
4390
4391    /* If we have indirect addressing in outputs we need to copy our alloca array
4392     * to the outputs slots specified by the caller */
4393    if (bld->gs_iface) {
4394       LLVMValueRef total_emitted_vertices_vec;
4395       LLVMValueRef emitted_prims_vec;
4396       /* implicit end_primitives, needed in case there are any unflushed
4397          vertices in the cache. Note must not call end_primitive here
4398          since the exec_mask is not valid at this point. */
4399       end_primitive_masked(bld_base, lp_build_mask_value(bld->mask));
4400
4401       total_emitted_vertices_vec =
4402          LLVMBuildLoad(builder, bld->total_emitted_vertices_vec_ptr, "");
4403       emitted_prims_vec =
4404          LLVMBuildLoad(builder, bld->emitted_prims_vec_ptr, "");
4405
4406       bld->gs_iface->gs_epilogue(bld->gs_iface,
4407                                  total_emitted_vertices_vec,
4408                                  emitted_prims_vec, 0);
4409    } else {
4410       gather_outputs(bld);
4411    }
4412 }
4413
4414 void
4415 lp_build_tgsi_soa(struct gallivm_state *gallivm,
4416                   const struct tgsi_token *tokens,
4417                   const struct lp_build_tgsi_params *params,
4418                   LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS])
4419 {
4420    struct lp_build_tgsi_soa_context bld;
4421    struct lp_type type = params->type;
4422    struct lp_type res_type;
4423
4424    assert(type.length <= LP_MAX_VECTOR_LENGTH);
4425    memset(&res_type, 0, sizeof res_type);
4426    res_type.width = type.width;
4427    res_type.length = type.length;
4428    res_type.sign = 1;
4429
4430    /* Setup build context */
4431    memset(&bld, 0, sizeof bld);
4432    lp_build_context_init(&bld.bld_base.base, gallivm, type);
4433    lp_build_context_init(&bld.bld_base.uint_bld, gallivm, lp_uint_type(type));
4434    lp_build_context_init(&bld.bld_base.int_bld, gallivm, lp_int_type(type));
4435    lp_build_context_init(&bld.elem_bld, gallivm, lp_elem_type(type));
4436    {
4437       struct lp_type dbl_type;
4438       dbl_type = type;
4439       dbl_type.width *= 2;
4440       lp_build_context_init(&bld.bld_base.dbl_bld, gallivm, dbl_type);
4441    }
4442    {
4443       struct lp_type uint64_type;
4444       uint64_type = lp_uint_type(type);
4445       uint64_type.width *= 2;
4446       lp_build_context_init(&bld.bld_base.uint64_bld, gallivm, uint64_type);
4447    }
4448    {
4449       struct lp_type int64_type;
4450       int64_type = lp_int_type(type);
4451       int64_type.width *= 2;
4452       lp_build_context_init(&bld.bld_base.int64_bld, gallivm, int64_type);
4453    }
4454    bld.mask = params->mask;
4455    bld.inputs = params->inputs;
4456    bld.outputs = outputs;
4457    bld.consts_ptr = params->consts_ptr;
4458    bld.ssbo_ptr = params->ssbo_ptr;
4459    bld.sampler = params->sampler;
4460    bld.bld_base.info = params->info;
4461    bld.indirect_files = params->info->indirect_files;
4462    bld.context_ptr = params->context_ptr;
4463    bld.thread_data_ptr = params->thread_data_ptr;
4464    bld.image = params->image;
4465    bld.shared_ptr = params->shared_ptr;
4466    bld.coro = params->coro;
4467
4468    /*
4469     * If the number of temporaries is rather large then we just
4470     * allocate them as an array right from the start and treat
4471     * like indirect temporaries.
4472     */
4473    if (params->info->file_max[TGSI_FILE_TEMPORARY] >= LP_MAX_INLINED_TEMPS) {
4474       bld.indirect_files |= (1 << TGSI_FILE_TEMPORARY);
4475    }
4476    /*
4477     * For performance reason immediates are always backed in a static
4478     * array, but if their number is too great, we have to use just
4479     * a dynamically allocated array.
4480     */
4481    bld.use_immediates_array =
4482          (params->info->file_max[TGSI_FILE_IMMEDIATE] >= LP_MAX_INLINED_IMMEDIATES);
4483    if (bld.use_immediates_array) {
4484       bld.indirect_files |= (1 << TGSI_FILE_IMMEDIATE);
4485    }
4486
4487
4488    bld.bld_base.soa = TRUE;
4489    bld.bld_base.emit_debug = emit_debug;
4490    bld.bld_base.emit_fetch_funcs[TGSI_FILE_CONSTANT] = emit_fetch_constant;
4491    bld.bld_base.emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = emit_fetch_immediate;
4492    bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_input;
4493    bld.bld_base.emit_fetch_funcs[TGSI_FILE_TEMPORARY] = emit_fetch_temporary;
4494    bld.bld_base.emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = emit_fetch_system_value;
4495
4496    bld.bld_base.emit_store = emit_store;
4497    bld.bld_base.emit_store_reg_funcs[TGSI_FILE_OUTPUT] = emit_store_output;
4498    bld.bld_base.emit_store_reg_funcs[TGSI_FILE_TEMPORARY] = emit_store_temp;
4499    bld.bld_base.emit_store_reg_funcs[TGSI_FILE_ADDRESS] = emit_store_address;
4500
4501    bld.bld_base.emit_declaration = lp_emit_declaration_soa;
4502    bld.bld_base.emit_immediate = lp_emit_immediate_soa;
4503
4504    bld.bld_base.emit_prologue = emit_prologue;
4505    bld.bld_base.emit_prologue_post_decl = emit_prologue_post_decl;
4506    bld.bld_base.emit_epilogue = emit_epilogue;
4507
4508    /* Set opcode actions */
4509    lp_set_default_actions_cpu(&bld.bld_base);
4510
4511    bld.bld_base.op_actions[TGSI_OPCODE_BGNLOOP].emit = bgnloop_emit;
4512    bld.bld_base.op_actions[TGSI_OPCODE_BGNSUB].emit = bgnsub_emit;
4513    bld.bld_base.op_actions[TGSI_OPCODE_BRK].emit = brk_emit;
4514    bld.bld_base.op_actions[TGSI_OPCODE_CAL].emit = cal_emit;
4515    bld.bld_base.op_actions[TGSI_OPCODE_CASE].emit = case_emit;
4516    bld.bld_base.op_actions[TGSI_OPCODE_CONT].emit = cont_emit;
4517    bld.bld_base.op_actions[TGSI_OPCODE_DDX].emit = ddx_emit;
4518    bld.bld_base.op_actions[TGSI_OPCODE_DDY].emit = ddy_emit;
4519    bld.bld_base.op_actions[TGSI_OPCODE_DEFAULT].emit = default_emit;
4520    bld.bld_base.op_actions[TGSI_OPCODE_ELSE].emit = else_emit;
4521    bld.bld_base.op_actions[TGSI_OPCODE_ENDIF].emit = endif_emit;
4522    bld.bld_base.op_actions[TGSI_OPCODE_ENDLOOP].emit = endloop_emit;
4523    bld.bld_base.op_actions[TGSI_OPCODE_ENDSUB].emit = endsub_emit;
4524    bld.bld_base.op_actions[TGSI_OPCODE_ENDSWITCH].emit = endswitch_emit;
4525    bld.bld_base.op_actions[TGSI_OPCODE_IF].emit = if_emit;
4526    bld.bld_base.op_actions[TGSI_OPCODE_UIF].emit = uif_emit;
4527    bld.bld_base.op_actions[TGSI_OPCODE_KILL_IF].emit = kill_if_emit;
4528    bld.bld_base.op_actions[TGSI_OPCODE_KILL].emit = kill_emit;
4529    bld.bld_base.op_actions[TGSI_OPCODE_RET].emit = ret_emit;
4530    bld.bld_base.op_actions[TGSI_OPCODE_SWITCH].emit = switch_emit;
4531    bld.bld_base.op_actions[TGSI_OPCODE_TEX].emit = tex_emit;
4532    bld.bld_base.op_actions[TGSI_OPCODE_TXB].emit = txb_emit;
4533    bld.bld_base.op_actions[TGSI_OPCODE_TXD].emit = txd_emit;
4534    bld.bld_base.op_actions[TGSI_OPCODE_TXL].emit = txl_emit;
4535    bld.bld_base.op_actions[TGSI_OPCODE_TEX_LZ].emit = txl_emit;
4536    bld.bld_base.op_actions[TGSI_OPCODE_TXP].emit = txp_emit;
4537    bld.bld_base.op_actions[TGSI_OPCODE_TXQ].emit = txq_emit;
4538    bld.bld_base.op_actions[TGSI_OPCODE_TXF].emit = txf_emit;
4539    bld.bld_base.op_actions[TGSI_OPCODE_TXF_LZ].emit = txf_emit;
4540    bld.bld_base.op_actions[TGSI_OPCODE_TEX2].emit = tex2_emit;
4541    bld.bld_base.op_actions[TGSI_OPCODE_TXB2].emit = txb2_emit;
4542    bld.bld_base.op_actions[TGSI_OPCODE_TXL2].emit = txl2_emit;
4543    bld.bld_base.op_actions[TGSI_OPCODE_TG4].emit = tg4_emit;
4544    bld.bld_base.op_actions[TGSI_OPCODE_LODQ].emit = lodq_emit;
4545    /* DX10 sampling ops */
4546    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE].emit = sample_emit;
4547    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_B].emit = sample_b_emit;
4548    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_C].emit = sample_c_emit;
4549    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_C_LZ].emit = sample_c_lz_emit;
4550    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_D].emit = sample_d_emit;
4551    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_I].emit = sample_i_emit;
4552    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_I_MS].emit = sample_i_emit;
4553    bld.bld_base.op_actions[TGSI_OPCODE_SAMPLE_L].emit = sample_l_emit;
4554    bld.bld_base.op_actions[TGSI_OPCODE_GATHER4].emit = gather4_emit;
4555    bld.bld_base.op_actions[TGSI_OPCODE_SVIEWINFO].emit = sviewinfo_emit;
4556    bld.bld_base.op_actions[TGSI_OPCODE_LOD].emit = lod_emit;
4557
4558    bld.bld_base.op_actions[TGSI_OPCODE_LOAD].emit = load_emit;
4559    bld.bld_base.op_actions[TGSI_OPCODE_STORE].emit = store_emit;
4560    bld.bld_base.op_actions[TGSI_OPCODE_RESQ].emit = resq_emit;
4561
4562    bld.bld_base.op_actions[TGSI_OPCODE_ATOMUADD].emit = atomic_emit;
4563    bld.bld_base.op_actions[TGSI_OPCODE_ATOMXCHG].emit = atomic_emit;
4564    bld.bld_base.op_actions[TGSI_OPCODE_ATOMCAS].emit = atomic_emit;
4565    bld.bld_base.op_actions[TGSI_OPCODE_ATOMAND].emit = atomic_emit;
4566    bld.bld_base.op_actions[TGSI_OPCODE_ATOMOR].emit = atomic_emit;
4567    bld.bld_base.op_actions[TGSI_OPCODE_ATOMXOR].emit = atomic_emit;
4568    bld.bld_base.op_actions[TGSI_OPCODE_ATOMUMIN].emit = atomic_emit;
4569    bld.bld_base.op_actions[TGSI_OPCODE_ATOMUMAX].emit = atomic_emit;
4570    bld.bld_base.op_actions[TGSI_OPCODE_ATOMIMIN].emit = atomic_emit;
4571    bld.bld_base.op_actions[TGSI_OPCODE_ATOMIMAX].emit = atomic_emit;
4572
4573    bld.bld_base.op_actions[TGSI_OPCODE_MEMBAR].emit = membar_emit;
4574    bld.bld_base.op_actions[TGSI_OPCODE_BARRIER].emit = barrier_emit;
4575
4576    if (params->gs_iface) {
4577       /* There's no specific value for this because it should always
4578        * be set, but apps using ext_geometry_shader4 quite often
4579        * were forgetting so we're using MAX_VERTEX_VARYING from
4580        * that spec even though we could assert if it's not
4581        * set, but that's a lot uglier. */
4582       uint max_output_vertices;
4583
4584       /* inputs are always indirect with gs */
4585       bld.indirect_files |= (1 << TGSI_FILE_INPUT);
4586       bld.gs_iface = params->gs_iface;
4587       bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_gs_input;
4588       bld.bld_base.op_actions[TGSI_OPCODE_EMIT].emit = emit_vertex;
4589       bld.bld_base.op_actions[TGSI_OPCODE_ENDPRIM].emit = end_primitive;
4590
4591       max_output_vertices =
4592          params->info->properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES];
4593       if (!max_output_vertices)
4594          max_output_vertices = 32;
4595
4596       bld.max_output_vertices_vec =
4597          lp_build_const_int_vec(gallivm, bld.bld_base.int_bld.type,
4598                                 max_output_vertices);
4599    }
4600
4601    if (params->tes_iface) {
4602       /* inputs are always indirect with tes */
4603       bld.indirect_files |= (1 << TGSI_FILE_INPUT);
4604       bld.tes_iface = params->tes_iface;
4605       bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_tes_input;
4606    }
4607
4608    if (params->tcs_iface) {
4609       bld.tcs_iface = params->tcs_iface;
4610       /* outputs and inputs are always indirect with tcs */
4611       bld.indirect_files |= (1 << TGSI_FILE_OUTPUT);
4612       bld.bld_base.emit_store_reg_funcs[TGSI_FILE_OUTPUT] = emit_store_tcs_output;
4613       bld.indirect_files |= (1 << TGSI_FILE_INPUT);
4614       bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_tcs_input;
4615       bld.bld_base.emit_fetch_funcs[TGSI_FILE_OUTPUT] = emit_fetch_tcs_input;
4616       bld.bld_base.op_actions[TGSI_OPCODE_BARRIER].emit = barrier_emit_tcs;
4617    }
4618
4619    lp_exec_mask_init(&bld.exec_mask, &bld.bld_base.int_bld);
4620
4621    bld.system_values = *params->system_values;
4622
4623    lp_build_tgsi_llvm(&bld.bld_base, tokens);
4624
4625    if (0) {
4626       LLVMBasicBlockRef block = LLVMGetInsertBlock(gallivm->builder);
4627       LLVMValueRef function = LLVMGetBasicBlockParent(block);
4628       debug_printf("11111111111111111111111111111 \n");
4629       tgsi_dump(tokens, 0);
4630       lp_debug_dump_value(function);
4631       debug_printf("2222222222222222222222222222 \n");
4632    }
4633
4634    if (0) {
4635       LLVMModuleRef module = LLVMGetGlobalParent(
4636          LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm->builder)));
4637       LLVMDumpModule(module);
4638
4639    }
4640    lp_exec_mask_fini(&bld.exec_mask);
4641 }