Debian Codegraph @

src/gallium/auxiliary/gallivm/lp_bld_tgsi_soa.c

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