cdc784cf0df356d6e87d7fcffc4f3deee85582a5
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_tgsi_soa.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * TGSI to LLVM IR translation -- SoA.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 *
35 * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
36 * Brian Paul, and others.
37 */
38
39 #include "pipe/p_config.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "util/u_debug.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "tgsi/tgsi_dump.h"
45 #include "tgsi/tgsi_exec.h"
46 #include "tgsi/tgsi_info.h"
47 #include "tgsi/tgsi_parse.h"
48 #include "tgsi/tgsi_util.h"
49 #include "tgsi/tgsi_scan.h"
50 #include "lp_bld_tgsi_action.h"
51 #include "lp_bld_type.h"
52 #include "lp_bld_const.h"
53 #include "lp_bld_arit.h"
54 #include "lp_bld_bitarit.h"
55 #include "lp_bld_gather.h"
56 #include "lp_bld_init.h"
57 #include "lp_bld_logic.h"
58 #include "lp_bld_swizzle.h"
59 #include "lp_bld_flow.h"
60 #include "lp_bld_quad.h"
61 #include "lp_bld_tgsi.h"
62 #include "lp_bld_limits.h"
63 #include "lp_bld_debug.h"
64 #include "lp_bld_printf.h"
65 #include "lp_bld_sample.h"
66
67
68 static void lp_exec_mask_init(struct lp_exec_mask *mask, struct lp_build_context *bld)
69 {
70 LLVMTypeRef int_type = LLVMInt32TypeInContext(bld->gallivm->context);
71 LLVMBuilderRef builder = bld->gallivm->builder;
72
73 mask->bld = bld;
74 mask->has_mask = FALSE;
75 mask->cond_stack_size = 0;
76 mask->loop_stack_size = 0;
77 mask->call_stack_size = 0;
78
79 mask->int_vec_type = lp_build_int_vec_type(bld->gallivm, mask->bld->type);
80 mask->exec_mask = mask->ret_mask = mask->break_mask = mask->cont_mask = mask->cond_mask =
81 LLVMConstAllOnes(mask->int_vec_type);
82
83 mask->loop_limiter = lp_build_alloca(bld->gallivm, int_type, "looplimiter");
84
85 LLVMBuildStore(
86 builder,
87 LLVMConstInt(int_type, LP_MAX_TGSI_LOOP_ITERATIONS, false),
88 mask->loop_limiter);
89 }
90
91 static void lp_exec_mask_update(struct lp_exec_mask *mask)
92 {
93 LLVMBuilderRef builder = mask->bld->gallivm->builder;
94
95 if (mask->loop_stack_size) {
96 /*for loops we need to update the entire mask at runtime */
97 LLVMValueRef tmp;
98 assert(mask->break_mask);
99 tmp = LLVMBuildAnd(builder,
100 mask->cont_mask,
101 mask->break_mask,
102 "maskcb");
103 mask->exec_mask = LLVMBuildAnd(builder,
104 mask->cond_mask,
105 tmp,
106 "maskfull");
107 } else
108 mask->exec_mask = mask->cond_mask;
109
110 if (mask->call_stack_size) {
111 mask->exec_mask = LLVMBuildAnd(builder,
112 mask->exec_mask,
113 mask->ret_mask,
114 "callmask");
115 }
116
117 mask->has_mask = (mask->cond_stack_size > 0 ||
118 mask->loop_stack_size > 0 ||
119 mask->call_stack_size > 0);
120 }
121
122 static void lp_exec_mask_cond_push(struct lp_exec_mask *mask,
123 LLVMValueRef val)
124 {
125 LLVMBuilderRef builder = mask->bld->gallivm->builder;
126
127 assert(mask->cond_stack_size < LP_MAX_TGSI_NESTING);
128 if (mask->cond_stack_size == 0) {
129 assert(mask->cond_mask == LLVMConstAllOnes(mask->int_vec_type));
130 }
131 mask->cond_stack[mask->cond_stack_size++] = mask->cond_mask;
132 assert(LLVMTypeOf(val) == mask->int_vec_type);
133 mask->cond_mask = LLVMBuildAnd(builder,
134 mask->cond_mask,
135 val,
136 "");
137 lp_exec_mask_update(mask);
138 }
139
140 static void lp_exec_mask_cond_invert(struct lp_exec_mask *mask)
141 {
142 LLVMBuilderRef builder = mask->bld->gallivm->builder;
143 LLVMValueRef prev_mask;
144 LLVMValueRef inv_mask;
145
146 assert(mask->cond_stack_size);
147 prev_mask = mask->cond_stack[mask->cond_stack_size - 1];
148 if (mask->cond_stack_size == 1) {
149 assert(prev_mask == LLVMConstAllOnes(mask->int_vec_type));
150 }
151
152 inv_mask = LLVMBuildNot(builder, mask->cond_mask, "");
153
154 mask->cond_mask = LLVMBuildAnd(builder,
155 inv_mask,
156 prev_mask, "");
157 lp_exec_mask_update(mask);
158 }
159
160 static void lp_exec_mask_cond_pop(struct lp_exec_mask *mask)
161 {
162 assert(mask->cond_stack_size);
163 mask->cond_mask = mask->cond_stack[--mask->cond_stack_size];
164 lp_exec_mask_update(mask);
165 }
166
167 static void lp_exec_bgnloop(struct lp_exec_mask *mask)
168 {
169 LLVMBuilderRef builder = mask->bld->gallivm->builder;
170
171 if (mask->loop_stack_size == 0) {
172 assert(mask->loop_block == NULL);
173 assert(mask->cont_mask == LLVMConstAllOnes(mask->int_vec_type));
174 assert(mask->break_mask == LLVMConstAllOnes(mask->int_vec_type));
175 assert(mask->break_var == NULL);
176 }
177
178 assert(mask->loop_stack_size < LP_MAX_TGSI_NESTING);
179
180 mask->loop_stack[mask->loop_stack_size].loop_block = mask->loop_block;
181 mask->loop_stack[mask->loop_stack_size].cont_mask = mask->cont_mask;
182 mask->loop_stack[mask->loop_stack_size].break_mask = mask->break_mask;
183 mask->loop_stack[mask->loop_stack_size].break_var = mask->break_var;
184 ++mask->loop_stack_size;
185
186 mask->break_var = lp_build_alloca(mask->bld->gallivm, mask->int_vec_type, "");
187 LLVMBuildStore(builder, mask->break_mask, mask->break_var);
188
189 mask->loop_block = lp_build_insert_new_block(mask->bld->gallivm, "bgnloop");
190
191 LLVMBuildBr(builder, mask->loop_block);
192 LLVMPositionBuilderAtEnd(builder, mask->loop_block);
193
194 mask->break_mask = LLVMBuildLoad(builder, mask->break_var, "");
195
196 lp_exec_mask_update(mask);
197 }
198
199 static void lp_exec_break(struct lp_exec_mask *mask)
200 {
201 LLVMBuilderRef builder = mask->bld->gallivm->builder;
202 LLVMValueRef exec_mask = LLVMBuildNot(builder,
203 mask->exec_mask,
204 "break");
205
206 mask->break_mask = LLVMBuildAnd(builder,
207 mask->break_mask,
208 exec_mask, "break_full");
209
210 lp_exec_mask_update(mask);
211 }
212
213 static void lp_exec_continue(struct lp_exec_mask *mask)
214 {
215 LLVMBuilderRef builder = mask->bld->gallivm->builder;
216 LLVMValueRef exec_mask = LLVMBuildNot(builder,
217 mask->exec_mask,
218 "");
219
220 mask->cont_mask = LLVMBuildAnd(builder,
221 mask->cont_mask,
222 exec_mask, "");
223
224 lp_exec_mask_update(mask);
225 }
226
227
228 static void lp_exec_endloop(struct gallivm_state *gallivm,
229 struct lp_exec_mask *mask)
230 {
231 LLVMBuilderRef builder = mask->bld->gallivm->builder;
232 LLVMBasicBlockRef endloop;
233 LLVMTypeRef int_type = LLVMInt32TypeInContext(mask->bld->gallivm->context);
234 LLVMTypeRef reg_type = LLVMIntTypeInContext(gallivm->context,
235 mask->bld->type.width *
236 mask->bld->type.length);
237 LLVMValueRef i1cond, i2cond, icond, limiter;
238
239 assert(mask->break_mask);
240
241 /*
242 * Restore the cont_mask, but don't pop
243 */
244 assert(mask->loop_stack_size);
245 mask->cont_mask = mask->loop_stack[mask->loop_stack_size - 1].cont_mask;
246 lp_exec_mask_update(mask);
247
248 /*
249 * Unlike the continue mask, the break_mask must be preserved across loop
250 * iterations
251 */
252 LLVMBuildStore(builder, mask->break_mask, mask->break_var);
253
254 /* Decrement the loop limiter */
255 limiter = LLVMBuildLoad(builder, mask->loop_limiter, "");
256
257 limiter = LLVMBuildSub(
258 builder,
259 limiter,
260 LLVMConstInt(int_type, 1, false),
261 "");
262
263 LLVMBuildStore(builder, limiter, mask->loop_limiter);
264
265 /* i1cond = (mask != 0) */
266 i1cond = LLVMBuildICmp(
267 builder,
268 LLVMIntNE,
269 LLVMBuildBitCast(builder, mask->exec_mask, reg_type, ""),
270 LLVMConstNull(reg_type), "");
271
272 /* i2cond = (looplimiter > 0) */
273 i2cond = LLVMBuildICmp(
274 builder,
275 LLVMIntSGT,
276 limiter,
277 LLVMConstNull(int_type), "");
278
279 /* if( i1cond && i2cond ) */
280 icond = LLVMBuildAnd(builder, i1cond, i2cond, "");
281
282 endloop = lp_build_insert_new_block(mask->bld->gallivm, "endloop");
283
284 LLVMBuildCondBr(builder,
285 icond, mask->loop_block, endloop);
286
287 LLVMPositionBuilderAtEnd(builder, endloop);
288
289 assert(mask->loop_stack_size);
290 --mask->loop_stack_size;
291 mask->loop_block = mask->loop_stack[mask->loop_stack_size].loop_block;
292 mask->cont_mask = mask->loop_stack[mask->loop_stack_size].cont_mask;
293 mask->break_mask = mask->loop_stack[mask->loop_stack_size].break_mask;
294 mask->break_var = mask->loop_stack[mask->loop_stack_size].break_var;
295
296 lp_exec_mask_update(mask);
297 }
298
299 /* stores val into an address pointed to by dst.
300 * mask->exec_mask is used to figure out which bits of val
301 * should be stored into the address
302 * (0 means don't store this bit, 1 means do store).
303 */
304 static void lp_exec_mask_store(struct lp_exec_mask *mask,
305 struct lp_build_context *bld_store,
306 LLVMValueRef pred,
307 LLVMValueRef val,
308 LLVMValueRef dst)
309 {
310 LLVMBuilderRef builder = mask->bld->gallivm->builder;
311
312 /* Mix the predicate and execution mask */
313 if (mask->has_mask) {
314 if (pred) {
315 pred = LLVMBuildAnd(builder, pred, mask->exec_mask, "");
316 } else {
317 pred = mask->exec_mask;
318 }
319 }
320
321 if (pred) {
322 LLVMValueRef real_val, dst_val;
323
324 dst_val = LLVMBuildLoad(builder, dst, "");
325 real_val = lp_build_select(bld_store,
326 pred,
327 val, dst_val);
328
329 LLVMBuildStore(builder, real_val, dst);
330 } else
331 LLVMBuildStore(builder, val, dst);
332 }
333
334 static void lp_exec_mask_call(struct lp_exec_mask *mask,
335 int func,
336 int *pc)
337 {
338 assert(mask->call_stack_size < LP_MAX_TGSI_NESTING);
339 mask->call_stack[mask->call_stack_size].pc = *pc;
340 mask->call_stack[mask->call_stack_size].ret_mask = mask->ret_mask;
341 mask->call_stack_size++;
342 *pc = func;
343 }
344
345 static void lp_exec_mask_ret(struct lp_exec_mask *mask, int *pc)
346 {
347 LLVMBuilderRef builder = mask->bld->gallivm->builder;
348 LLVMValueRef exec_mask;
349
350 if (mask->call_stack_size == 0) {
351 /* returning from main() */
352 *pc = -1;
353 return;
354 }
355 exec_mask = LLVMBuildNot(builder,
356 mask->exec_mask,
357 "ret");
358
359 mask->ret_mask = LLVMBuildAnd(builder,
360 mask->ret_mask,
361 exec_mask, "ret_full");
362
363 lp_exec_mask_update(mask);
364 }
365
366 static void lp_exec_mask_bgnsub(struct lp_exec_mask *mask)
367 {
368 }
369
370 static void lp_exec_mask_endsub(struct lp_exec_mask *mask, int *pc)
371 {
372 assert(mask->call_stack_size);
373 mask->call_stack_size--;
374 *pc = mask->call_stack[mask->call_stack_size].pc;
375 mask->ret_mask = mask->call_stack[mask->call_stack_size].ret_mask;
376 lp_exec_mask_update(mask);
377 }
378
379
380 /**
381 * Return pointer to a temporary register channel (src or dest).
382 * Note that indirect addressing cannot be handled here.
383 * \param index which temporary register
384 * \param chan which channel of the temp register.
385 */
386 LLVMValueRef
387 lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context *bld,
388 unsigned index,
389 unsigned chan)
390 {
391 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
392 assert(chan < 4);
393 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
394 LLVMValueRef lindex = lp_build_const_int32(bld->bld_base.base.gallivm, index * 4 + chan);
395 return LLVMBuildGEP(builder, bld->temps_array, &lindex, 1, "");
396 }
397 else {
398 return bld->temps[index][chan];
399 }
400 }
401
402 /**
403 * Return pointer to a output register channel (src or dest).
404 * Note that indirect addressing cannot be handled here.
405 * \param index which output register
406 * \param chan which channel of the output register.
407 */
408 LLVMValueRef
409 lp_get_output_ptr(struct lp_build_tgsi_soa_context *bld,
410 unsigned index,
411 unsigned chan)
412 {
413 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
414 assert(chan < 4);
415 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) {
416 LLVMValueRef lindex = lp_build_const_int32(bld->bld_base.base.gallivm,
417 index * 4 + chan);
418 return LLVMBuildGEP(builder, bld->outputs_array, &lindex, 1, "");
419 }
420 else {
421 return bld->outputs[index][chan];
422 }
423 }
424
425 /**
426 * Gather vector.
427 * XXX the lp_build_gather() function should be capable of doing this
428 * with a little work.
429 */
430 static LLVMValueRef
431 build_gather(struct lp_build_context *bld,
432 LLVMValueRef base_ptr,
433 LLVMValueRef indexes)
434 {
435 LLVMBuilderRef builder = bld->gallivm->builder;
436 LLVMValueRef res = bld->undef;
437 unsigned i;
438
439 /*
440 * Loop over elements of index_vec, load scalar value, insert it into 'res'.
441 */
442 for (i = 0; i < bld->type.length; i++) {
443 LLVMValueRef ii = lp_build_const_int32(bld->gallivm, i);
444 LLVMValueRef index = LLVMBuildExtractElement(builder,
445 indexes, ii, "");
446 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr,
447 &index, 1, "gather_ptr");
448 LLVMValueRef scalar = LLVMBuildLoad(builder, scalar_ptr, "");
449
450 res = LLVMBuildInsertElement(builder, res, scalar, ii, "");
451 }
452
453 return res;
454 }
455
456
457 /**
458 * Scatter/store vector.
459 */
460 static void
461 emit_mask_scatter(struct lp_build_tgsi_soa_context *bld,
462 LLVMValueRef base_ptr,
463 LLVMValueRef indexes,
464 LLVMValueRef values,
465 struct lp_exec_mask *mask,
466 LLVMValueRef pred)
467 {
468 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
469 LLVMBuilderRef builder = gallivm->builder;
470 unsigned i;
471
472 /* Mix the predicate and execution mask */
473 if (mask->has_mask) {
474 if (pred) {
475 pred = LLVMBuildAnd(builder, pred, mask->exec_mask, "");
476 }
477 else {
478 pred = mask->exec_mask;
479 }
480 }
481
482 /*
483 * Loop over elements of index_vec, store scalar value.
484 */
485 for (i = 0; i < bld->bld_base.base.type.length; i++) {
486 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
487 LLVMValueRef index = LLVMBuildExtractElement(builder, indexes, ii, "");
488 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr, &index, 1, "scatter_ptr");
489 LLVMValueRef val = LLVMBuildExtractElement(builder, values, ii, "scatter_val");
490 LLVMValueRef scalar_pred = pred ?
491 LLVMBuildExtractElement(builder, pred, ii, "scatter_pred") : NULL;
492
493 if (0)
494 lp_build_printf(gallivm, "scatter %d: val %f at %d %p\n",
495 ii, val, index, scalar_ptr);
496
497 if (scalar_pred) {
498 LLVMValueRef real_val, dst_val;
499 dst_val = LLVMBuildLoad(builder, scalar_ptr, "");
500 real_val = lp_build_select(&bld->elem_bld, scalar_pred, val, dst_val);
501 LLVMBuildStore(builder, real_val, scalar_ptr);
502 }
503 else {
504 LLVMBuildStore(builder, val, scalar_ptr);
505 }
506 }
507 }
508
509
510 /**
511 * Read the current value of the ADDR register, convert the floats to
512 * ints, add the base index and return the vector of offsets.
513 * The offsets will be used to index into the constant buffer or
514 * temporary register file.
515 */
516 static LLVMValueRef
517 get_indirect_index(struct lp_build_tgsi_soa_context *bld,
518 unsigned reg_file, unsigned reg_index,
519 const struct tgsi_src_register *indirect_reg)
520 {
521 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
522 struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;
523 /* always use X component of address register */
524 unsigned swizzle = indirect_reg->SwizzleX;
525 LLVMValueRef base;
526 LLVMValueRef rel;
527 LLVMValueRef max_index;
528 LLVMValueRef index;
529
530 assert(bld->indirect_files & (1 << reg_file));
531
532 base = lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, reg_index);
533
534 assert(swizzle < 4);
535 rel = LLVMBuildLoad(builder,
536 bld->addr[indirect_reg->Index][swizzle],
537 "load addr reg");
538
539 index = lp_build_add(uint_bld, base, rel);
540
541 max_index = lp_build_const_int_vec(bld->bld_base.base.gallivm,
542 uint_bld->type,
543 bld->bld_base.info->file_max[reg_file]);
544
545 assert(!uint_bld->type.sign);
546 index = lp_build_min(uint_bld, index, max_index);
547
548 return index;
549 }
550
551 static struct lp_build_context *
552 stype_to_fetch(struct lp_build_tgsi_context * bld_base,
553 enum tgsi_opcode_type stype)
554 {
555 struct lp_build_context *bld_fetch;
556
557 switch (stype) {
558 case TGSI_TYPE_FLOAT:
559 case TGSI_TYPE_UNTYPED:
560 bld_fetch = &bld_base->base;
561 break;
562 case TGSI_TYPE_UNSIGNED:
563 bld_fetch = &bld_base->uint_bld;
564 break;
565 case TGSI_TYPE_SIGNED:
566 bld_fetch = &bld_base->int_bld;
567 break;
568 case TGSI_TYPE_VOID:
569 case TGSI_TYPE_DOUBLE:
570 default:
571 assert(0);
572 bld_fetch = NULL;
573 break;
574 }
575 return bld_fetch;
576 }
577
578 static LLVMValueRef
579 emit_fetch_constant(
580 struct lp_build_tgsi_context * bld_base,
581 const struct tgsi_full_src_register * reg,
582 enum tgsi_opcode_type stype,
583 unsigned swizzle)
584 {
585 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
586 struct gallivm_state *gallivm = bld_base->base.gallivm;
587 LLVMBuilderRef builder = gallivm->builder;
588 struct lp_build_context *uint_bld = &bld_base->uint_bld;
589 LLVMValueRef indirect_index = NULL;
590 struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype);
591
592 /* XXX: Handle fetching xyzw components as a vector */
593 assert(swizzle != ~0);
594
595 if (reg->Register.Indirect) {
596 indirect_index = get_indirect_index(bld,
597 reg->Register.File,
598 reg->Register.Index,
599 &reg->Indirect);
600 }
601
602 if (reg->Register.Indirect) {
603 LLVMValueRef swizzle_vec =
604 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, swizzle);
605 LLVMValueRef index_vec; /* index into the const buffer */
606
607 /* index_vec = indirect_index * 4 + swizzle */
608 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
609 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
610
611 /* Gather values from the constant buffer */
612 return build_gather(bld_fetch, bld->consts_ptr, index_vec);
613 }
614 else {
615 LLVMValueRef index; /* index into the const buffer */
616 LLVMValueRef scalar, scalar_ptr;
617
618 index = lp_build_const_int32(gallivm, reg->Register.Index*4 + swizzle);
619
620 scalar_ptr = LLVMBuildGEP(builder, bld->consts_ptr,
621 &index, 1, "");
622
623 if (stype != TGSI_TYPE_FLOAT && stype != TGSI_TYPE_UNTYPED) {
624 LLVMTypeRef ivtype = LLVMPointerType(LLVMInt32TypeInContext(gallivm->context), 0);
625 LLVMValueRef temp_ptr;
626 temp_ptr = LLVMBuildBitCast(builder, scalar_ptr, ivtype, "");
627 scalar = LLVMBuildLoad(builder, temp_ptr, "");
628 } else
629 scalar = LLVMBuildLoad(builder, scalar_ptr, "");
630
631 return lp_build_broadcast_scalar(bld_fetch, scalar);
632 }
633 }
634
635 static LLVMValueRef
636 emit_fetch_immediate(
637 struct lp_build_tgsi_context * bld_base,
638 const struct tgsi_full_src_register * reg,
639 enum tgsi_opcode_type stype,
640 unsigned swizzle)
641 {
642 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
643 LLVMValueRef res = bld->immediates[reg->Register.Index][swizzle];
644 assert(res);
645
646 if (stype == TGSI_TYPE_UNSIGNED) {
647 res = LLVMConstBitCast(res, bld_base->uint_bld.vec_type);
648 } else if (stype == TGSI_TYPE_SIGNED) {
649 res = LLVMConstBitCast(res, bld_base->int_bld.vec_type);
650 }
651 return res;
652 }
653
654 static LLVMValueRef
655 emit_fetch_input(
656 struct lp_build_tgsi_context * bld_base,
657 const struct tgsi_full_src_register * reg,
658 enum tgsi_opcode_type stype,
659 unsigned swizzle)
660 {
661 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
662 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
663 LLVMBuilderRef builder = gallivm->builder;
664 struct lp_build_context *uint_bld = &bld_base->uint_bld;
665 LLVMValueRef indirect_index = NULL;
666 LLVMValueRef res;
667
668 if (reg->Register.Indirect) {
669 indirect_index = get_indirect_index(bld,
670 reg->Register.File,
671 reg->Register.Index,
672 &reg->Indirect);
673 }
674
675 if (reg->Register.Indirect) {
676 LLVMValueRef swizzle_vec =
677 lp_build_const_int_vec(gallivm, uint_bld->type, swizzle);
678 LLVMValueRef length_vec =
679 lp_build_const_int_vec(gallivm, uint_bld->type, bld->bld_base.base.type.length);
680 LLVMValueRef index_vec; /* index into the const buffer */
681 LLVMValueRef inputs_array;
682 LLVMTypeRef float4_ptr_type;
683
684 /* index_vec = (indirect_index * 4 + swizzle) * length */
685 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
686 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
687 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
688
689 /* cast inputs_array pointer to float* */
690 float4_ptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
691 inputs_array = LLVMBuildBitCast(builder, bld->inputs_array,
692 float4_ptr_type, "");
693
694 /* Gather values from the temporary register array */
695 res = build_gather(&bld_base->base, inputs_array, index_vec);
696 } else {
697 if (bld->indirect_files & (1 << TGSI_FILE_INPUT)) {
698 LLVMValueRef lindex = lp_build_const_int32(gallivm,
699 reg->Register.Index * 4 + swizzle);
700 LLVMValueRef input_ptr = LLVMBuildGEP(builder,
701 bld->inputs_array, &lindex, 1, "");
702 res = LLVMBuildLoad(builder, input_ptr, "");
703 }
704 else {
705 res = bld->inputs[reg->Register.Index][swizzle];
706 }
707 }
708
709 assert(res);
710
711 if (stype == TGSI_TYPE_UNSIGNED) {
712 res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
713 } else if (stype == TGSI_TYPE_SIGNED) {
714 res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
715 }
716
717 return res;
718 }
719
720 static LLVMValueRef
721 emit_fetch_temporary(
722 struct lp_build_tgsi_context * bld_base,
723 const struct tgsi_full_src_register * reg,
724 enum tgsi_opcode_type stype,
725 unsigned swizzle)
726 {
727 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
728 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
729 LLVMBuilderRef builder = gallivm->builder;
730 struct lp_build_context *uint_bld = &bld_base->uint_bld;
731 LLVMValueRef indirect_index = NULL;
732 LLVMValueRef res;
733
734 if (reg->Register.Indirect) {
735 indirect_index = get_indirect_index(bld,
736 reg->Register.File,
737 reg->Register.Index,
738 &reg->Indirect);
739 }
740
741 if (reg->Register.Indirect) {
742 LLVMValueRef swizzle_vec =
743 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, swizzle);
744 LLVMValueRef length_vec =
745 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type,
746 bld->bld_base.base.type.length);
747 LLVMValueRef index_vec; /* index into the const buffer */
748 LLVMValueRef temps_array;
749 LLVMTypeRef float4_ptr_type;
750
751 /* index_vec = (indirect_index * 4 + swizzle) * length */
752 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
753 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec);
754 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
755
756 /* cast temps_array pointer to float* */
757 float4_ptr_type = LLVMPointerType(LLVMFloatTypeInContext(bld->bld_base.base.gallivm->context), 0);
758 temps_array = LLVMBuildBitCast(builder, bld->temps_array,
759 float4_ptr_type, "");
760
761 /* Gather values from the temporary register array */
762 res = build_gather(&bld_base->base, temps_array, index_vec);
763 }
764 else {
765 LLVMValueRef temp_ptr;
766 if (stype != TGSI_TYPE_FLOAT && stype != TGSI_TYPE_UNTYPED) {
767 LLVMTypeRef itype = LLVMPointerType(bld->bld_base.int_bld.vec_type, 0);
768 LLVMValueRef tint_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index,
769 swizzle);
770 temp_ptr = LLVMBuildBitCast(builder, tint_ptr, itype, "");
771 } else
772 temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, swizzle);
773 res = LLVMBuildLoad(builder, temp_ptr, "");
774 if (!res)
775 return bld->bld_base.base.undef;
776 }
777
778 return res;
779 }
780
781 static LLVMValueRef
782 emit_fetch_system_value(
783 struct lp_build_tgsi_context * bld_base,
784 const struct tgsi_full_src_register * reg,
785 enum tgsi_opcode_type stype,
786 unsigned swizzle)
787 {
788 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
789 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
790 const struct tgsi_shader_info *info = bld->bld_base.info;
791 LLVMBuilderRef builder = gallivm->builder;
792 LLVMValueRef res;
793 enum tgsi_opcode_type atype; // Actual type of the value
794
795 assert(!reg->Register.Indirect);
796
797 switch (info->system_value_semantic_name[reg->Register.Index]) {
798 case TGSI_SEMANTIC_INSTANCEID:
799 res = lp_build_broadcast_scalar(&bld_base->uint_bld, bld->system_values.instance_id);
800 atype = TGSI_TYPE_UNSIGNED;
801 break;
802
803 case TGSI_SEMANTIC_VERTEXID:
804 res = bld->system_values.vertex_id;
805 atype = TGSI_TYPE_UNSIGNED;
806 break;
807
808 default:
809 assert(!"unexpected semantic in emit_fetch_system_value");
810 res = bld_base->base.zero;
811 atype = TGSI_TYPE_FLOAT;
812 break;
813 }
814
815 if (atype != stype) {
816 if (stype == TGSI_TYPE_FLOAT) {
817 res = LLVMBuildBitCast(builder, res, bld_base->base.vec_type, "");
818 } else if (stype == TGSI_TYPE_UNSIGNED) {
819 res = LLVMBuildBitCast(builder, res, bld_base->uint_bld.vec_type, "");
820 } else if (stype == TGSI_TYPE_SIGNED) {
821 res = LLVMBuildBitCast(builder, res, bld_base->int_bld.vec_type, "");
822 }
823 }
824
825 return res;
826 }
827
828 /**
829 * Register fetch with derivatives.
830 */
831 static void
832 emit_fetch_deriv(
833 struct lp_build_tgsi_soa_context *bld,
834 LLVMValueRef src,
835 LLVMValueRef *res,
836 LLVMValueRef *ddx,
837 LLVMValueRef *ddy)
838 {
839 if(res)
840 *res = src;
841
842 /* TODO: use interpolation coeffs for inputs */
843
844 if(ddx)
845 *ddx = lp_build_ddx(&bld->bld_base.base, src);
846
847 if(ddy)
848 *ddy = lp_build_ddy(&bld->bld_base.base, src);
849 }
850
851
852 /**
853 * Predicate.
854 */
855 static void
856 emit_fetch_predicate(
857 struct lp_build_tgsi_soa_context *bld,
858 const struct tgsi_full_instruction *inst,
859 LLVMValueRef *pred)
860 {
861 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
862 unsigned index;
863 unsigned char swizzles[4];
864 LLVMValueRef unswizzled[4] = {NULL, NULL, NULL, NULL};
865 LLVMValueRef value;
866 unsigned chan;
867
868 if (!inst->Instruction.Predicate) {
869 TGSI_FOR_EACH_CHANNEL( chan ) {
870 pred[chan] = NULL;
871 }
872 return;
873 }
874
875 swizzles[0] = inst->Predicate.SwizzleX;
876 swizzles[1] = inst->Predicate.SwizzleY;
877 swizzles[2] = inst->Predicate.SwizzleZ;
878 swizzles[3] = inst->Predicate.SwizzleW;
879
880 index = inst->Predicate.Index;
881 assert(index < LP_MAX_TGSI_PREDS);
882
883 TGSI_FOR_EACH_CHANNEL( chan ) {
884 unsigned swizzle = swizzles[chan];
885
886 /*
887 * Only fetch the predicate register channels that are actually listed
888 * in the swizzles
889 */
890 if (!unswizzled[swizzle]) {
891 value = LLVMBuildLoad(builder,
892 bld->preds[index][swizzle], "");
893
894 /*
895 * Convert the value to an integer mask.
896 *
897 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions
898 * is needlessly causing two comparisons due to storing the intermediate
899 * result as float vector instead of an integer mask vector.
900 */
901 value = lp_build_compare(bld->bld_base.base.gallivm,
902 bld->bld_base.base.type,
903 PIPE_FUNC_NOTEQUAL,
904 value,
905 bld->bld_base.base.zero);
906 if (inst->Predicate.Negate) {
907 value = LLVMBuildNot(builder, value, "");
908 }
909
910 unswizzled[swizzle] = value;
911 } else {
912 value = unswizzled[swizzle];
913 }
914
915 pred[chan] = value;
916 }
917 }
918
919 /**
920 * Register store.
921 */
922 static void
923 emit_store_chan(
924 struct lp_build_tgsi_context *bld_base,
925 const struct tgsi_full_instruction *inst,
926 unsigned index,
927 unsigned chan_index,
928 LLVMValueRef pred,
929 LLVMValueRef value)
930 {
931 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
932 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
933 LLVMBuilderRef builder = gallivm->builder;
934 const struct tgsi_full_dst_register *reg = &inst->Dst[index];
935 struct lp_build_context *uint_bld = &bld_base->uint_bld;
936 LLVMValueRef indirect_index = NULL;
937 struct lp_build_context *bld_store;
938 enum tgsi_opcode_type dtype = tgsi_opcode_infer_dst_type(inst->Instruction.Opcode);
939
940 switch (dtype) {
941 default:
942 case TGSI_TYPE_FLOAT:
943 case TGSI_TYPE_UNTYPED:
944 bld_store = &bld_base->base;
945 break;
946 case TGSI_TYPE_UNSIGNED:
947 bld_store = &bld_base->uint_bld;
948 break;
949 case TGSI_TYPE_SIGNED:
950 bld_store = &bld_base->int_bld;
951 break;
952 case TGSI_TYPE_DOUBLE:
953 case TGSI_TYPE_VOID:
954 assert(0);
955 bld_store = NULL;
956 break;
957 }
958
959 switch( inst->Instruction.Saturate ) {
960 case TGSI_SAT_NONE:
961 break;
962
963 case TGSI_SAT_ZERO_ONE:
964 value = lp_build_max(&bld->bld_base.base, value, bld->bld_base.base.zero);
965 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one);
966 break;
967
968 case TGSI_SAT_MINUS_PLUS_ONE:
969 value = lp_build_max(&bld->bld_base.base, value, lp_build_const_vec(bld->bld_base.base.gallivm, bld->bld_base.base.type, -1.0));
970 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one);
971 break;
972
973 default:
974 assert(0);
975 }
976
977 if (reg->Register.Indirect) {
978 indirect_index = get_indirect_index(bld,
979 reg->Register.File,
980 reg->Register.Index,
981 &reg->Indirect);
982 } else {
983 assert(reg->Register.Index <=
984 bld->bld_base.info->file_max[reg->Register.File]);
985 }
986
987 switch( reg->Register.File ) {
988 case TGSI_FILE_OUTPUT:
989 if (reg->Register.Indirect) {
990 LLVMValueRef chan_vec =
991 lp_build_const_int_vec(gallivm, uint_bld->type, chan_index);
992 LLVMValueRef length_vec =
993 lp_build_const_int_vec(gallivm, uint_bld->type, bld->bld_base.base.type.length);
994 LLVMValueRef index_vec; /* indexes into the temp registers */
995 LLVMValueRef outputs_array;
996 LLVMValueRef pixel_offsets;
997 LLVMTypeRef float_ptr_type;
998 int i;
999
1000 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1001 pixel_offsets = uint_bld->undef;
1002 for (i = 0; i < bld->bld_base.base.type.length; i++) {
1003 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
1004 pixel_offsets = LLVMBuildInsertElement(builder, pixel_offsets,
1005 ii, ii, "");
1006 }
1007
1008 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1009 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
1010 index_vec = lp_build_add(uint_bld, index_vec, chan_vec);
1011 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
1012 index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets);
1013
1014 float_ptr_type =
1015 LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1016 outputs_array = LLVMBuildBitCast(builder, bld->outputs_array,
1017 float_ptr_type, "");
1018
1019 /* Scatter store values into temp registers */
1020 emit_mask_scatter(bld, outputs_array, index_vec, value,
1021 &bld->exec_mask, pred);
1022 }
1023 else {
1024 LLVMValueRef out_ptr = lp_get_output_ptr(bld, reg->Register.Index,
1025 chan_index);
1026 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, out_ptr);
1027 }
1028 break;
1029
1030 case TGSI_FILE_TEMPORARY:
1031 if (reg->Register.Indirect) {
1032 LLVMValueRef chan_vec =
1033 lp_build_const_int_vec(gallivm, uint_bld->type, chan_index);
1034 LLVMValueRef length_vec =
1035 lp_build_const_int_vec(gallivm, uint_bld->type,
1036 bld->bld_base.base.type.length);
1037 LLVMValueRef index_vec; /* indexes into the temp registers */
1038 LLVMValueRef temps_array;
1039 LLVMValueRef pixel_offsets;
1040 LLVMTypeRef float_ptr_type;
1041 int i;
1042
1043 /* build pixel offset vector: {0, 1, 2, 3, ...} */
1044 pixel_offsets = uint_bld->undef;
1045 for (i = 0; i < bld->bld_base.base.type.length; i++) {
1046 LLVMValueRef ii = lp_build_const_int32(gallivm, i);
1047 pixel_offsets = LLVMBuildInsertElement(builder, pixel_offsets,
1048 ii, ii, "");
1049 }
1050
1051 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */
1052 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2);
1053 index_vec = lp_build_add(uint_bld, index_vec, chan_vec);
1054 index_vec = lp_build_mul(uint_bld, index_vec, length_vec);
1055 index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets);
1056
1057 float_ptr_type =
1058 LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0);
1059 temps_array = LLVMBuildBitCast(builder, bld->temps_array,
1060 float_ptr_type, "");
1061
1062 /* Scatter store values into temp registers */
1063 emit_mask_scatter(bld, temps_array, index_vec, value,
1064 &bld->exec_mask, pred);
1065 }
1066 else {
1067 LLVMValueRef temp_ptr;
1068
1069 switch (dtype) {
1070 case TGSI_TYPE_UNSIGNED:
1071 case TGSI_TYPE_SIGNED: {
1072 LLVMTypeRef itype = bld_base->int_bld.vec_type;
1073 LLVMTypeRef ivtype = LLVMPointerType(itype, 0);
1074 LLVMValueRef tint_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index,
1075 chan_index);
1076 LLVMValueRef temp_value_ptr;
1077
1078 temp_ptr = LLVMBuildBitCast(builder, tint_ptr, ivtype, "");
1079 temp_value_ptr = LLVMBuildBitCast(builder, value, itype, "");
1080 value = temp_value_ptr;
1081 break;
1082 }
1083 default:
1084 case TGSI_TYPE_FLOAT:
1085 case TGSI_TYPE_UNTYPED:
1086 temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index,
1087 chan_index);
1088 break;
1089 }
1090
1091 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, temp_ptr);
1092 }
1093 break;
1094
1095 case TGSI_FILE_ADDRESS:
1096 assert(dtype == TGSI_TYPE_SIGNED);
1097 assert(LLVMTypeOf(value) == bld_base->base.int_vec_type);
1098 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value,
1099 bld->addr[reg->Register.Index][chan_index]);
1100 break;
1101
1102 case TGSI_FILE_PREDICATE:
1103 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value,
1104 bld->preds[reg->Register.Index][chan_index]);
1105 break;
1106
1107 default:
1108 assert( 0 );
1109 }
1110 }
1111
1112 static void
1113 emit_store(
1114 struct lp_build_tgsi_context * bld_base,
1115 const struct tgsi_full_instruction * inst,
1116 const struct tgsi_opcode_info * info,
1117 LLVMValueRef dst[4])
1118
1119 {
1120 unsigned chan_index;
1121 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1122
1123 if(info->num_dst) {
1124 LLVMValueRef pred[TGSI_NUM_CHANNELS];
1125
1126 emit_fetch_predicate( bld, inst, pred );
1127
1128 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
1129 emit_store_chan(bld_base, inst, 0, chan_index, pred[chan_index], dst[chan_index]);
1130 }
1131 }
1132 }
1133
1134 /**
1135 * High-level instruction translators.
1136 */
1137
1138 static void
1139 emit_tex( struct lp_build_tgsi_soa_context *bld,
1140 const struct tgsi_full_instruction *inst,
1141 enum lp_build_tex_modifier modifier,
1142 LLVMValueRef *texel)
1143 {
1144 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1145 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1146 unsigned unit;
1147 LLVMValueRef lod_bias, explicit_lod;
1148 LLVMValueRef oow = NULL;
1149 LLVMValueRef coords[4];
1150 LLVMValueRef offsets[3] = { NULL };
1151 struct lp_derivatives derivs;
1152 unsigned num_coords;
1153 unsigned dims;
1154 unsigned i;
1155
1156 if (!bld->sampler) {
1157 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1158 for (i = 0; i < 4; i++) {
1159 texel[i] = bld->bld_base.base.undef;
1160 }
1161 return;
1162 }
1163
1164 derivs.ddx_ddy[0] = bld->bld_base.base.undef;
1165 derivs.ddx_ddy[1] = bld->bld_base.base.undef;
1166
1167 switch (inst->Texture.Texture) {
1168 case TGSI_TEXTURE_1D:
1169 num_coords = 1;
1170 dims = 1;
1171 break;
1172 case TGSI_TEXTURE_1D_ARRAY:
1173 num_coords = 2;
1174 dims = 1;
1175 break;
1176 case TGSI_TEXTURE_2D:
1177 case TGSI_TEXTURE_RECT:
1178 num_coords = 2;
1179 dims = 2;
1180 break;
1181 case TGSI_TEXTURE_SHADOW1D:
1182 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1183 num_coords = 3;
1184 dims = 1;
1185 break;
1186 case TGSI_TEXTURE_SHADOW2D:
1187 case TGSI_TEXTURE_SHADOWRECT:
1188 case TGSI_TEXTURE_2D_ARRAY:
1189 case TGSI_TEXTURE_CUBE:
1190 num_coords = 3;
1191 dims = 2;
1192 break;
1193 case TGSI_TEXTURE_3D:
1194 num_coords = 3;
1195 dims = 3;
1196 break;
1197 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1198 case TGSI_TEXTURE_SHADOWCUBE:
1199 num_coords = 4;
1200 dims = 2;
1201 break;
1202 default:
1203 assert(0);
1204 return;
1205 }
1206
1207 /* Note lod and especially projected are illegal in a LOT of cases */
1208 if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS) {
1209 assert(num_coords < 4);
1210 lod_bias = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1211 explicit_lod = NULL;
1212 }
1213 else if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
1214 assert(num_coords < 4);
1215 lod_bias = NULL;
1216 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1217 }
1218 else {
1219 lod_bias = NULL;
1220 explicit_lod = NULL;
1221 }
1222
1223 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED) {
1224 assert(num_coords < 4);
1225 oow = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1226 oow = lp_build_rcp(&bld->bld_base.base, oow);
1227 }
1228
1229 for (i = 0; i < num_coords; i++) {
1230 coords[i] = lp_build_emit_fetch( &bld->bld_base, inst, 0, i );
1231 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED)
1232 coords[i] = lp_build_mul(&bld->bld_base.base, coords[i], oow);
1233 }
1234 for (i = num_coords; i < 4; i++) {
1235 coords[i] = bld->bld_base.base.undef;
1236 }
1237
1238 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
1239 LLVMValueRef i32undef = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1240 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
1241 LLVMValueRef ddxdyonec[3];
1242 unsigned length = bld->bld_base.base.type.length;
1243 unsigned num_quads = length / 4;
1244 unsigned dim;
1245 unsigned quad;
1246
1247 for (dim = 0; dim < dims; ++dim) {
1248 LLVMValueRef srcx = lp_build_emit_fetch( &bld->bld_base, inst, 1, dim );
1249 LLVMValueRef srcy = lp_build_emit_fetch( &bld->bld_base, inst, 2, dim );
1250 for (quad = 0; quad < num_quads; ++quad) {
1251 unsigned s1 = 4*quad;
1252 unsigned s2 = 4*quad + length;
1253 shuffles[4*quad + 0] = lp_build_const_int32(gallivm, s1);
1254 shuffles[4*quad + 1] = lp_build_const_int32(gallivm, s2);
1255 shuffles[4*quad + 2] = i32undef;
1256 shuffles[4*quad + 3] = i32undef;
1257 }
1258 ddxdyonec[dim] = LLVMBuildShuffleVector(builder, srcx, srcy,
1259 LLVMConstVector(shuffles, length), "");
1260 }
1261 if (dims == 1) {
1262 derivs.ddx_ddy[0] = ddxdyonec[0];
1263 }
1264 else if (dims >= 2) {
1265 for (quad = 0; quad < num_quads; ++quad) {
1266 unsigned s1 = 4*quad;
1267 unsigned s2 = 4*quad + length;
1268 shuffles[4*quad + 0] = lp_build_const_int32(gallivm, s1);
1269 shuffles[4*quad + 1] = lp_build_const_int32(gallivm, s1 + 1);
1270 shuffles[4*quad + 2] = lp_build_const_int32(gallivm, s2);
1271 shuffles[4*quad + 3] = lp_build_const_int32(gallivm, s2 + 1);
1272 }
1273 derivs.ddx_ddy[0] = LLVMBuildShuffleVector(builder, ddxdyonec[0], ddxdyonec[1],
1274 LLVMConstVector(shuffles, length), "");
1275 if (dims == 3) {
1276 derivs.ddx_ddy[1] = ddxdyonec[2];
1277 }
1278 }
1279 unit = inst->Src[3].Register.Index;
1280 } else {
1281 if (dims == 1) {
1282 derivs.ddx_ddy[0] = lp_build_packed_ddx_ddy_onecoord(&bld->bld_base.base, coords[0]);
1283 }
1284 else if (dims >= 2) {
1285 derivs.ddx_ddy[0] = lp_build_packed_ddx_ddy_twocoord(&bld->bld_base.base,
1286 coords[0], coords[1]);
1287 if (dims == 3) {
1288 derivs.ddx_ddy[1] = lp_build_packed_ddx_ddy_onecoord(&bld->bld_base.base, coords[2]);
1289 }
1290 }
1291 unit = inst->Src[1].Register.Index;
1292 }
1293
1294 /* some advanced gather instructions (txgo) would require 4 offsets */
1295 if (inst->Texture.NumOffsets == 1) {
1296 unsigned dim;
1297 for (dim = 0; dim < dims; dim++) {
1298 offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim );
1299 }
1300 }
1301
1302 bld->sampler->emit_fetch_texel(bld->sampler,
1303 bld->bld_base.base.gallivm,
1304 bld->bld_base.base.type,
1305 FALSE,
1306 unit, coords,
1307 offsets,
1308 &derivs,
1309 lod_bias, explicit_lod,
1310 texel);
1311 }
1312
1313 static void
1314 emit_txf( struct lp_build_tgsi_soa_context *bld,
1315 const struct tgsi_full_instruction *inst,
1316 LLVMValueRef *texel)
1317 {
1318 unsigned unit;
1319 LLVMValueRef coord_undef = LLVMGetUndef(bld->bld_base.base.int_vec_type);
1320 LLVMValueRef explicit_lod = NULL;
1321 LLVMValueRef coords[3];
1322 LLVMValueRef offsets[3] = { NULL };
1323 struct lp_derivatives derivs;
1324 unsigned num_coords;
1325 unsigned dims;
1326 unsigned i;
1327
1328 if (!bld->sampler) {
1329 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
1330 for (i = 0; i < 4; i++) {
1331 texel[i] = coord_undef;
1332 }
1333 return;
1334 }
1335
1336 derivs.ddx_ddy[0] = coord_undef;
1337 derivs.ddx_ddy[1] = coord_undef;
1338
1339 switch (inst->Texture.Texture) {
1340 case TGSI_TEXTURE_1D:
1341 case TGSI_TEXTURE_BUFFER:
1342 num_coords = 1;
1343 dims = 1;
1344 break;
1345 case TGSI_TEXTURE_1D_ARRAY:
1346 num_coords = 2;
1347 dims = 1;
1348 break;
1349 case TGSI_TEXTURE_2D:
1350 case TGSI_TEXTURE_RECT:
1351 num_coords = 2;
1352 dims = 2;
1353 break;
1354 case TGSI_TEXTURE_2D_ARRAY:
1355 num_coords = 3;
1356 dims = 2;
1357 break;
1358 case TGSI_TEXTURE_3D:
1359 num_coords = 3;
1360 dims = 3;
1361 break;
1362 default:
1363 assert(0);
1364 return;
1365 }
1366
1367 /* always have lod except for buffers ? */
1368 if (inst->Texture.Texture != TGSI_TEXTURE_BUFFER) {
1369 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 );
1370 }
1371
1372 for (i = 0; i < num_coords; i++) {
1373 coords[i] = lp_build_emit_fetch( &bld->bld_base, inst, 0, i );
1374 }
1375 for (i = num_coords; i < 3; i++) {
1376 coords[i] = coord_undef;
1377 }
1378
1379 unit = inst->Src[1].Register.Index;
1380
1381 if (inst->Texture.NumOffsets == 1) {
1382 unsigned dim;
1383 for (dim = 0; dim < dims; dim++) {
1384 offsets[dim] = lp_build_emit_fetch_texoffset(&bld->bld_base, inst, 0, dim );
1385 }
1386 }
1387
1388 bld->sampler->emit_fetch_texel(bld->sampler,
1389 bld->bld_base.base.gallivm,
1390 bld->bld_base.base.type,
1391 TRUE,
1392 unit, coords,
1393 offsets,
1394 &derivs,
1395 NULL, explicit_lod,
1396 texel);
1397 }
1398
1399 static void
1400 emit_txq( struct lp_build_tgsi_soa_context *bld,
1401 const struct tgsi_full_instruction *inst,
1402 LLVMValueRef *sizes_out)
1403 {
1404 LLVMValueRef explicit_lod;
1405 unsigned num_coords, has_lod;
1406 unsigned i;
1407
1408 switch (inst->Texture.Texture) {
1409 case TGSI_TEXTURE_1D:
1410 case TGSI_TEXTURE_SHADOW1D:
1411 num_coords = 1;
1412 has_lod = 1;
1413 break;
1414 case TGSI_TEXTURE_2D:
1415 case TGSI_TEXTURE_SHADOW2D:
1416 case TGSI_TEXTURE_CUBE:
1417 case TGSI_TEXTURE_SHADOWCUBE:
1418 case TGSI_TEXTURE_1D_ARRAY:
1419 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1420 num_coords = 2;
1421 has_lod = 1;
1422 break;
1423 case TGSI_TEXTURE_3D:
1424 // case TGSI_TEXTURE_CUBE_ARRAY:
1425 // case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1426 case TGSI_TEXTURE_2D_ARRAY:
1427 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1428 num_coords = 3;
1429 has_lod = 1;
1430 break;
1431
1432 case TGSI_TEXTURE_BUFFER:
1433 num_coords = 1;
1434 has_lod = 0;
1435 break;
1436
1437 case TGSI_TEXTURE_RECT:
1438 case TGSI_TEXTURE_SHADOWRECT:
1439 // case TGSI_TEXTURE_2D_MS:
1440 num_coords = 2;
1441 has_lod = 0;
1442 break;
1443
1444 // case TGSI_TEXTURE_2D_MS_ARRAY:
1445 // num_coords = 3;
1446 // has_lod = 0;
1447 // break;
1448
1449 default:
1450 assert(0);
1451 return;
1452 }
1453
1454 if (!bld->sampler) {
1455 _debug_printf("warning: found texture query instruction but no sampler generator supplied\n");
1456 for (i = 0; i < num_coords; i++)
1457 sizes_out[i] = bld->bld_base.base.undef;
1458 return;
1459 }
1460
1461 if (has_lod)
1462 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 0, 2 );
1463 else
1464 explicit_lod = NULL;
1465
1466 bld->sampler->emit_size_query(bld->sampler,
1467 bld->bld_base.base.gallivm,
1468 bld->bld_base.int_bld.type,
1469 inst->Src[1].Register.Index,
1470 explicit_lod,
1471 sizes_out);
1472 }
1473
1474 static boolean
1475 near_end_of_shader(struct lp_build_tgsi_soa_context *bld,
1476 int pc)
1477 {
1478 int i;
1479
1480 for (i = 0; i < 5; i++) {
1481 unsigned opcode;
1482
1483 if (pc + i >= bld->bld_base.info->num_instructions)
1484 return TRUE;
1485
1486 opcode = bld->bld_base.instructions[pc + i].Instruction.Opcode;
1487
1488 if (opcode == TGSI_OPCODE_END)
1489 return TRUE;
1490
1491 if (opcode == TGSI_OPCODE_TEX ||
1492 opcode == TGSI_OPCODE_TXP ||
1493 opcode == TGSI_OPCODE_TXD ||
1494 opcode == TGSI_OPCODE_TXB ||
1495 opcode == TGSI_OPCODE_TXL ||
1496 opcode == TGSI_OPCODE_TXF ||
1497 opcode == TGSI_OPCODE_TXQ ||
1498 opcode == TGSI_OPCODE_CAL ||
1499 opcode == TGSI_OPCODE_CALLNZ ||
1500 opcode == TGSI_OPCODE_IF ||
1501 opcode == TGSI_OPCODE_IFC ||
1502 opcode == TGSI_OPCODE_BGNLOOP ||
1503 opcode == TGSI_OPCODE_SWITCH)
1504 return FALSE;
1505 }
1506
1507 return TRUE;
1508 }
1509
1510
1511
1512 /**
1513 * Kill fragment if any of the src register values are negative.
1514 */
1515 static void
1516 emit_kil(
1517 struct lp_build_tgsi_soa_context *bld,
1518 const struct tgsi_full_instruction *inst,
1519 int pc)
1520 {
1521 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1522 const struct tgsi_full_src_register *reg = &inst->Src[0];
1523 LLVMValueRef terms[TGSI_NUM_CHANNELS];
1524 LLVMValueRef mask;
1525 unsigned chan_index;
1526
1527 memset(&terms, 0, sizeof terms);
1528
1529 TGSI_FOR_EACH_CHANNEL( chan_index ) {
1530 unsigned swizzle;
1531
1532 /* Unswizzle channel */
1533 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
1534
1535 /* Check if the component has not been already tested. */
1536 assert(swizzle < TGSI_NUM_CHANNELS);
1537 if( !terms[swizzle] )
1538 /* TODO: change the comparison operator instead of setting the sign */
1539 terms[swizzle] = lp_build_emit_fetch(&bld->bld_base, inst, 0, chan_index );
1540 }
1541
1542 mask = NULL;
1543 TGSI_FOR_EACH_CHANNEL( chan_index ) {
1544 if(terms[chan_index]) {
1545 LLVMValueRef chan_mask;
1546
1547 /*
1548 * If term < 0 then mask = 0 else mask = ~0.
1549 */
1550 chan_mask = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->bld_base.base.zero);
1551
1552 if(mask)
1553 mask = LLVMBuildAnd(builder, mask, chan_mask, "");
1554 else
1555 mask = chan_mask;
1556 }
1557 }
1558
1559 if(mask) {
1560 lp_build_mask_update(bld->mask, mask);
1561
1562 if (!near_end_of_shader(bld, pc))
1563 lp_build_mask_check(bld->mask);
1564 }
1565 }
1566
1567
1568 /**
1569 * Predicated fragment kill.
1570 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
1571 * The only predication is the execution mask which will apply if
1572 * we're inside a loop or conditional.
1573 */
1574 static void
1575 emit_kilp(struct lp_build_tgsi_soa_context *bld,
1576 int pc)
1577 {
1578 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
1579 LLVMValueRef mask;
1580
1581 /* For those channels which are "alive", disable fragment shader
1582 * execution.
1583 */
1584 if (bld->exec_mask.has_mask) {
1585 mask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp");
1586 }
1587 else {
1588 LLVMValueRef zero = LLVMConstNull(bld->bld_base.base.int_vec_type);
1589 mask = zero;
1590 }
1591
1592 lp_build_mask_update(bld->mask, mask);
1593
1594 if (!near_end_of_shader(bld, pc))
1595 lp_build_mask_check(bld->mask);
1596 }
1597
1598
1599 /**
1600 * Emit code which will dump the value of all the temporary registers
1601 * to stdout.
1602 */
1603 static void
1604 emit_dump_temps(struct lp_build_tgsi_soa_context *bld)
1605 {
1606 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1607 LLVMBuilderRef builder = gallivm->builder;
1608 LLVMValueRef temp_ptr;
1609 LLVMValueRef i0 = lp_build_const_int32(gallivm, 0);
1610 LLVMValueRef i1 = lp_build_const_int32(gallivm, 1);
1611 LLVMValueRef i2 = lp_build_const_int32(gallivm, 2);
1612 LLVMValueRef i3 = lp_build_const_int32(gallivm, 3);
1613 int index;
1614 int n = bld->bld_base.info->file_max[TGSI_FILE_TEMPORARY];
1615
1616 for (index = 0; index < n; index++) {
1617 LLVMValueRef idx = lp_build_const_int32(gallivm, index);
1618 LLVMValueRef v[4][4], res;
1619 int chan;
1620
1621 lp_build_printf(gallivm, "TEMP[%d]:\n", idx);
1622
1623 for (chan = 0; chan < 4; chan++) {
1624 temp_ptr = lp_get_temp_ptr_soa(bld, index, chan);
1625 res = LLVMBuildLoad(builder, temp_ptr, "");
1626 v[chan][0] = LLVMBuildExtractElement(builder, res, i0, "");
1627 v[chan][1] = LLVMBuildExtractElement(builder, res, i1, "");
1628 v[chan][2] = LLVMBuildExtractElement(builder, res, i2, "");
1629 v[chan][3] = LLVMBuildExtractElement(builder, res, i3, "");
1630 }
1631
1632 lp_build_printf(gallivm, " X: %f %f %f %f\n",
1633 v[0][0], v[0][1], v[0][2], v[0][3]);
1634 lp_build_printf(gallivm, " Y: %f %f %f %f\n",
1635 v[1][0], v[1][1], v[1][2], v[1][3]);
1636 lp_build_printf(gallivm, " Z: %f %f %f %f\n",
1637 v[2][0], v[2][1], v[2][2], v[2][3]);
1638 lp_build_printf(gallivm, " W: %f %f %f %f\n",
1639 v[3][0], v[3][1], v[3][2], v[3][3]);
1640 }
1641 }
1642
1643
1644
1645 void
1646 lp_emit_declaration_soa(
1647 struct lp_build_tgsi_context *bld_base,
1648 const struct tgsi_full_declaration *decl)
1649 {
1650 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1651 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
1652 LLVMTypeRef vec_type = bld->bld_base.base.vec_type;
1653 const unsigned first = decl->Range.First;
1654 const unsigned last = decl->Range.Last;
1655 unsigned idx, i;
1656
1657 for (idx = first; idx <= last; ++idx) {
1658 assert(last <= bld->bld_base.info->file_max[decl->Declaration.File]);
1659 switch (decl->Declaration.File) {
1660 case TGSI_FILE_TEMPORARY:
1661 assert(idx < LP_MAX_TGSI_TEMPS);
1662 if (!(bld->indirect_files & (1 << TGSI_FILE_TEMPORARY))) {
1663 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1664 bld->temps[idx][i] = lp_build_alloca(gallivm, vec_type, "temp");
1665 }
1666 break;
1667
1668 case TGSI_FILE_OUTPUT:
1669 if (!(bld->indirect_files & (1 << TGSI_FILE_OUTPUT))) {
1670 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1671 bld->outputs[idx][i] = lp_build_alloca(gallivm,
1672 vec_type, "output");
1673 }
1674 break;
1675
1676 case TGSI_FILE_ADDRESS:
1677 /* ADDR registers are the only allocated with an integer LLVM IR type,
1678 * as they are guaranteed to always have integers.
1679 * XXX: Not sure if this exception is worthwhile (or the whole idea of
1680 * an ADDR register for that matter).
1681 */
1682 assert(idx < LP_MAX_TGSI_ADDRS);
1683 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1684 bld->addr[idx][i] = lp_build_alloca(gallivm, bld_base->base.int_vec_type, "addr");
1685 break;
1686
1687 case TGSI_FILE_PREDICATE:
1688 assert(idx < LP_MAX_TGSI_PREDS);
1689 for (i = 0; i < TGSI_NUM_CHANNELS; i++)
1690 bld->preds[idx][i] = lp_build_alloca(gallivm, vec_type,
1691 "predicate");
1692 break;
1693
1694 default:
1695 /* don't need to declare other vars */
1696 break;
1697 }
1698 }
1699 }
1700
1701
1702 void lp_emit_immediate_soa(
1703 struct lp_build_tgsi_context *bld_base,
1704 const struct tgsi_full_immediate *imm)
1705 {
1706 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1707 struct gallivm_state * gallivm = bld_base->base.gallivm;
1708
1709 /* simply copy the immediate values into the next immediates[] slot */
1710 unsigned i;
1711 const uint size = imm->Immediate.NrTokens - 1;
1712 assert(size <= 4);
1713 assert(bld->num_immediates < LP_MAX_TGSI_IMMEDIATES);
1714 switch (imm->Immediate.DataType) {
1715 case TGSI_IMM_FLOAT32:
1716 for( i = 0; i < size; ++i )
1717 bld->immediates[bld->num_immediates][i] =
1718 lp_build_const_vec(gallivm, bld_base->base.type, imm->u[i].Float);
1719
1720 break;
1721 case TGSI_IMM_UINT32:
1722 for( i = 0; i < size; ++i ) {
1723 LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->uint_bld.type, imm->u[i].Uint);
1724 bld->immediates[bld->num_immediates][i] =
1725 LLVMConstBitCast(tmp, bld_base->base.vec_type);
1726 }
1727
1728 break;
1729 case TGSI_IMM_INT32:
1730 for( i = 0; i < size; ++i ) {
1731 LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->int_bld.type, imm->u[i].Int);
1732 bld->immediates[bld->num_immediates][i] =
1733 LLVMConstBitCast(tmp, bld_base->base.vec_type);
1734 }
1735
1736 break;
1737 }
1738 for( i = size; i < 4; ++i )
1739 bld->immediates[bld->num_immediates][i] = bld_base->base.undef;
1740
1741 bld->num_immediates++;
1742 }
1743
1744 static void
1745 ddx_emit(
1746 const struct lp_build_tgsi_action * action,
1747 struct lp_build_tgsi_context * bld_base,
1748 struct lp_build_emit_data * emit_data)
1749 {
1750 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1751
1752 emit_fetch_deriv(bld, emit_data->args[0], NULL,
1753 &emit_data->output[emit_data->chan], NULL);
1754 }
1755
1756 static void
1757 ddy_emit(
1758 const struct lp_build_tgsi_action * action,
1759 struct lp_build_tgsi_context * bld_base,
1760 struct lp_build_emit_data * emit_data)
1761 {
1762 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1763
1764 emit_fetch_deriv(bld, emit_data->args[0], NULL, NULL,
1765 &emit_data->output[emit_data->chan]);
1766 }
1767
1768 static void
1769 kilp_emit(
1770 const struct lp_build_tgsi_action * action,
1771 struct lp_build_tgsi_context * bld_base,
1772 struct lp_build_emit_data * emit_data)
1773 {
1774 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1775
1776 emit_kilp(bld, bld_base->pc - 1);
1777 }
1778
1779 static void
1780 kil_emit(
1781 const struct lp_build_tgsi_action * action,
1782 struct lp_build_tgsi_context * bld_base,
1783 struct lp_build_emit_data * emit_data)
1784 {
1785 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1786
1787 emit_kil(bld, emit_data->inst, bld_base->pc - 1);
1788 }
1789
1790 static void
1791 tex_emit(
1792 const struct lp_build_tgsi_action * action,
1793 struct lp_build_tgsi_context * bld_base,
1794 struct lp_build_emit_data * emit_data)
1795 {
1796 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1797
1798 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE, emit_data->output);
1799 }
1800
1801 static void
1802 txb_emit(
1803 const struct lp_build_tgsi_action * action,
1804 struct lp_build_tgsi_context * bld_base,
1805 struct lp_build_emit_data * emit_data)
1806 {
1807 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1808
1809 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_BIAS,
1810 emit_data->output);
1811 }
1812
1813 static void
1814 txd_emit(
1815 const struct lp_build_tgsi_action * action,
1816 struct lp_build_tgsi_context * bld_base,
1817 struct lp_build_emit_data * emit_data)
1818 {
1819 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1820
1821 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
1822 emit_data->output);
1823 }
1824
1825 static void
1826 txl_emit(
1827 const struct lp_build_tgsi_action * action,
1828 struct lp_build_tgsi_context * bld_base,
1829 struct lp_build_emit_data * emit_data)
1830 {
1831 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1832
1833 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
1834 emit_data->output);
1835 }
1836
1837 static void
1838 txp_emit(
1839 const struct lp_build_tgsi_action * action,
1840 struct lp_build_tgsi_context * bld_base,
1841 struct lp_build_emit_data * emit_data)
1842 {
1843 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1844
1845 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_PROJECTED,
1846 emit_data->output);
1847 }
1848
1849 static void
1850 txq_emit(
1851 const struct lp_build_tgsi_action * action,
1852 struct lp_build_tgsi_context * bld_base,
1853 struct lp_build_emit_data * emit_data)
1854 {
1855 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1856
1857 emit_txq(bld, emit_data->inst, emit_data->output);
1858 }
1859
1860 static void
1861 txf_emit(
1862 const struct lp_build_tgsi_action * action,
1863 struct lp_build_tgsi_context * bld_base,
1864 struct lp_build_emit_data * emit_data)
1865 {
1866 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1867
1868 emit_txf(bld, emit_data->inst, emit_data->output);
1869 }
1870
1871 static void
1872 cal_emit(
1873 const struct lp_build_tgsi_action * action,
1874 struct lp_build_tgsi_context * bld_base,
1875 struct lp_build_emit_data * emit_data)
1876 {
1877 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1878
1879 lp_exec_mask_call(&bld->exec_mask, emit_data->inst->Label.Label,
1880 &bld_base->pc);
1881 }
1882
1883 static void
1884 ret_emit(
1885 const struct lp_build_tgsi_action * action,
1886 struct lp_build_tgsi_context * bld_base,
1887 struct lp_build_emit_data * emit_data)
1888 {
1889 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1890
1891 lp_exec_mask_ret(&bld->exec_mask, &bld_base->pc);
1892 }
1893
1894 static void
1895 brk_emit(
1896 const struct lp_build_tgsi_action * action,
1897 struct lp_build_tgsi_context * bld_base,
1898 struct lp_build_emit_data * emit_data)
1899 {
1900 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1901
1902 lp_exec_break(&bld->exec_mask);
1903 }
1904
1905 static void
1906 if_emit(
1907 const struct lp_build_tgsi_action * action,
1908 struct lp_build_tgsi_context * bld_base,
1909 struct lp_build_emit_data * emit_data)
1910 {
1911 LLVMValueRef tmp;
1912 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1913
1914 tmp = lp_build_cmp(&bld_base->base, PIPE_FUNC_NOTEQUAL,
1915 emit_data->args[0], bld->bld_base.base.zero);
1916 lp_exec_mask_cond_push(&bld->exec_mask, tmp);
1917 }
1918
1919 static void
1920 bgnloop_emit(
1921 const struct lp_build_tgsi_action * action,
1922 struct lp_build_tgsi_context * bld_base,
1923 struct lp_build_emit_data * emit_data)
1924 {
1925 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1926
1927 lp_exec_bgnloop(&bld->exec_mask);
1928 }
1929
1930 static void
1931 bgnsub_emit(
1932 const struct lp_build_tgsi_action * action,
1933 struct lp_build_tgsi_context * bld_base,
1934 struct lp_build_emit_data * emit_data)
1935 {
1936 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1937
1938 lp_exec_mask_bgnsub(&bld->exec_mask);
1939 }
1940
1941 static void
1942 else_emit(
1943 const struct lp_build_tgsi_action * action,
1944 struct lp_build_tgsi_context * bld_base,
1945 struct lp_build_emit_data * emit_data)
1946 {
1947 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1948
1949 lp_exec_mask_cond_invert(&bld->exec_mask);
1950 }
1951
1952 static void
1953 endif_emit(
1954 const struct lp_build_tgsi_action * action,
1955 struct lp_build_tgsi_context * bld_base,
1956 struct lp_build_emit_data * emit_data)
1957 {
1958 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1959
1960 lp_exec_mask_cond_pop(&bld->exec_mask);
1961 }
1962
1963 static void
1964 endloop_emit(
1965 const struct lp_build_tgsi_action * action,
1966 struct lp_build_tgsi_context * bld_base,
1967 struct lp_build_emit_data * emit_data)
1968 {
1969 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1970
1971 lp_exec_endloop(bld_base->base.gallivm, &bld->exec_mask);
1972 }
1973
1974 static void
1975 endsub_emit(
1976 const struct lp_build_tgsi_action * action,
1977 struct lp_build_tgsi_context * bld_base,
1978 struct lp_build_emit_data * emit_data)
1979 {
1980 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1981
1982 lp_exec_mask_endsub(&bld->exec_mask, &bld_base->pc);
1983 }
1984
1985 static void
1986 cont_emit(
1987 const struct lp_build_tgsi_action * action,
1988 struct lp_build_tgsi_context * bld_base,
1989 struct lp_build_emit_data * emit_data)
1990 {
1991 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
1992
1993 lp_exec_continue(&bld->exec_mask);
1994 }
1995
1996 /* XXX: Refactor and move it to lp_bld_tgsi_action.c
1997 *
1998 * XXX: What do the comments about xmm registers mean? Maybe they are left over
1999 * from old code, but there is no garauntee that LLVM will use those registers
2000 * for this code.
2001 *
2002 * XXX: There should be no calls to lp_build_emit_fetch in this function. This
2003 * should be handled by the emit_data->fetch_args function. */
2004 static void
2005 nrm_emit(
2006 const struct lp_build_tgsi_action * action,
2007 struct lp_build_tgsi_context * bld_base,
2008 struct lp_build_emit_data * emit_data)
2009 {
2010 LLVMValueRef tmp0, tmp1;
2011 LLVMValueRef tmp4 = NULL;
2012 LLVMValueRef tmp5 = NULL;
2013 LLVMValueRef tmp6 = NULL;
2014 LLVMValueRef tmp7 = NULL;
2015 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2016
2017 uint dims = (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_NRM) ? 3 : 4;
2018
2019 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X) ||
2020 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y) ||
2021 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z) ||
2022 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W) && dims == 4)) {
2023
2024 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
2025
2026 /* xmm4 = src.x */
2027 /* xmm0 = src.x * src.x */
2028 tmp0 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_X);
2029 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X)) {
2030 tmp4 = tmp0;
2031 }
2032 tmp0 = lp_build_mul( &bld->bld_base.base, tmp0, tmp0);
2033
2034 /* xmm5 = src.y */
2035 /* xmm0 = xmm0 + src.y * src.y */
2036 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_Y);
2037 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y)) {
2038 tmp5 = tmp1;
2039 }
2040 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1);
2041 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1);
2042
2043 /* xmm6 = src.z */
2044 /* xmm0 = xmm0 + src.z * src.z */
2045 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_Z);
2046 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z)) {
2047 tmp6 = tmp1;
2048 }
2049 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1);
2050 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1);
2051
2052 if (dims == 4) {
2053 /* xmm7 = src.w */
2054 /* xmm0 = xmm0 + src.w * src.w */
2055 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_W);
2056 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W)) {
2057 tmp7 = tmp1;
2058 }
2059 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1);
2060 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1);
2061 }
2062 /* xmm1 = 1 / sqrt(xmm0) */
2063 tmp1 = lp_build_rsqrt( &bld->bld_base.base, tmp0);
2064 /* dst.x = xmm1 * src.x */
2065 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X)) {
2066 emit_data->output[TGSI_CHAN_X] = lp_build_mul( &bld->bld_base.base, tmp4, tmp1);
2067 }
2068 /* dst.y = xmm1 * src.y */
2069 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y)) {
2070 emit_data->output[TGSI_CHAN_Y] = lp_build_mul( &bld->bld_base.base, tmp5, tmp1);
2071 }
2072
2073 /* dst.z = xmm1 * src.z */
2074 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z)) {
2075 emit_data->output[TGSI_CHAN_Z] = lp_build_mul( &bld->bld_base.base, tmp6, tmp1);
2076 }
2077 /* dst.w = xmm1 * src.w */
2078 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X) && dims == 4) {
2079 emit_data->output[TGSI_CHAN_W] = lp_build_mul( &bld->bld_base.base, tmp7, tmp1);
2080 }
2081 }
2082
2083 /* dst.w = 1.0 */
2084 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W) && dims == 3) {
2085 emit_data->output[TGSI_CHAN_W] = bld->bld_base.base.one;
2086 }
2087 }
2088
2089 static void emit_prologue(struct lp_build_tgsi_context * bld_base)
2090 {
2091 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2092 struct gallivm_state * gallivm = bld_base->base.gallivm;
2093
2094 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
2095 LLVMValueRef array_size =
2096 lp_build_const_int32(gallivm,
2097 bld_base->info->file_max[TGSI_FILE_TEMPORARY] * 4 + 4);
2098 bld->temps_array = lp_build_array_alloca(gallivm,
2099 bld_base->base.vec_type, array_size,
2100 "temp_array");
2101 }
2102
2103 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) {
2104 LLVMValueRef array_size =
2105 lp_build_const_int32(gallivm,
2106 bld_base->info->file_max[TGSI_FILE_OUTPUT] * 4 + 4);
2107 bld->outputs_array = lp_build_array_alloca(gallivm,
2108 bld_base->base.vec_type, array_size,
2109 "output_array");
2110 }
2111
2112 /* If we have indirect addressing in inputs we need to copy them into
2113 * our alloca array to be able to iterate over them */
2114 if (bld->indirect_files & (1 << TGSI_FILE_INPUT)) {
2115 unsigned index, chan;
2116 LLVMTypeRef vec_type = bld_base->base.vec_type;
2117 LLVMValueRef array_size = lp_build_const_int32(gallivm,
2118 bld_base->info->file_max[TGSI_FILE_INPUT]*4 + 4);
2119 bld->inputs_array = lp_build_array_alloca(gallivm,
2120 vec_type, array_size,
2121 "input_array");
2122
2123 assert(bld_base->info->num_inputs
2124 <= bld_base->info->file_max[TGSI_FILE_INPUT] + 1);
2125
2126 for (index = 0; index < bld_base->info->num_inputs; ++index) {
2127 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
2128 LLVMValueRef lindex =
2129 lp_build_const_int32(gallivm, index * 4 + chan);
2130 LLVMValueRef input_ptr =
2131 LLVMBuildGEP(gallivm->builder, bld->inputs_array,
2132 &lindex, 1, "");
2133 LLVMValueRef value = bld->inputs[index][chan];
2134 if (value)
2135 LLVMBuildStore(gallivm->builder, value, input_ptr);
2136 }
2137 }
2138 }
2139 }
2140
2141 static void emit_epilogue(struct lp_build_tgsi_context * bld_base)
2142 {
2143 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base);
2144
2145 if (0) {
2146 /* for debugging */
2147 emit_dump_temps(bld);
2148 }
2149
2150 /* If we have indirect addressing in outputs we need to copy our alloca array
2151 * to the outputs slots specified by the called */
2152 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) {
2153 unsigned index, chan;
2154 assert(bld_base->info->num_outputs <=
2155 bld_base->info->file_max[TGSI_FILE_OUTPUT] + 1);
2156 for (index = 0; index < bld_base->info->num_outputs; ++index) {
2157 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
2158 bld->outputs[index][chan] = lp_get_output_ptr(bld, index, chan);
2159 }
2160 }
2161 }
2162 }
2163
2164 void
2165 lp_build_tgsi_soa(struct gallivm_state *gallivm,
2166 const struct tgsi_token *tokens,
2167 struct lp_type type,
2168 struct lp_build_mask_context *mask,
2169 LLVMValueRef consts_ptr,
2170 const struct lp_bld_tgsi_system_values *system_values,
2171 const LLVMValueRef *pos,
2172 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS],
2173 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
2174 struct lp_build_sampler_soa *sampler,
2175 const struct tgsi_shader_info *info)
2176 {
2177 struct lp_build_tgsi_soa_context bld;
2178
2179 struct lp_type res_type;
2180
2181 assert(type.length <= LP_MAX_VECTOR_LENGTH);
2182 memset(&res_type, 0, sizeof res_type);
2183 res_type.width = type.width;
2184 res_type.length = type.length;
2185 res_type.sign = 1;
2186
2187 /* Setup build context */
2188 memset(&bld, 0, sizeof bld);
2189 lp_build_context_init(&bld.bld_base.base, gallivm, type);
2190 lp_build_context_init(&bld.bld_base.uint_bld, gallivm, lp_uint_type(type));
2191 lp_build_context_init(&bld.bld_base.int_bld, gallivm, lp_int_type(type));
2192 lp_build_context_init(&bld.elem_bld, gallivm, lp_elem_type(type));
2193 bld.mask = mask;
2194 bld.pos = pos;
2195 bld.inputs = inputs;
2196 bld.outputs = outputs;
2197 bld.consts_ptr = consts_ptr;
2198 bld.sampler = sampler;
2199 bld.bld_base.info = info;
2200 bld.indirect_files = info->indirect_files;
2201
2202 bld.bld_base.soa = TRUE;
2203 bld.bld_base.emit_fetch_funcs[TGSI_FILE_CONSTANT] = emit_fetch_constant;
2204 bld.bld_base.emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = emit_fetch_immediate;
2205 bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_input;
2206 bld.bld_base.emit_fetch_funcs[TGSI_FILE_TEMPORARY] = emit_fetch_temporary;
2207 bld.bld_base.emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = emit_fetch_system_value;
2208 bld.bld_base.emit_store = emit_store;
2209
2210 bld.bld_base.emit_declaration = lp_emit_declaration_soa;
2211 bld.bld_base.emit_immediate = lp_emit_immediate_soa;
2212
2213 bld.bld_base.emit_prologue = emit_prologue;
2214 bld.bld_base.emit_epilogue = emit_epilogue;
2215
2216 /* Set opcode actions */
2217 lp_set_default_actions_cpu(&bld.bld_base);
2218
2219 bld.bld_base.op_actions[TGSI_OPCODE_BGNLOOP].emit = bgnloop_emit;
2220 bld.bld_base.op_actions[TGSI_OPCODE_BGNSUB].emit = bgnsub_emit;
2221 bld.bld_base.op_actions[TGSI_OPCODE_BRK].emit = brk_emit;
2222 bld.bld_base.op_actions[TGSI_OPCODE_CAL].emit = cal_emit;
2223 bld.bld_base.op_actions[TGSI_OPCODE_CONT].emit = cont_emit;
2224 bld.bld_base.op_actions[TGSI_OPCODE_DDX].emit = ddx_emit;
2225 bld.bld_base.op_actions[TGSI_OPCODE_DDY].emit = ddy_emit;
2226 bld.bld_base.op_actions[TGSI_OPCODE_ELSE].emit = else_emit;
2227 bld.bld_base.op_actions[TGSI_OPCODE_ENDIF].emit = endif_emit;
2228 bld.bld_base.op_actions[TGSI_OPCODE_ENDLOOP].emit = endloop_emit;
2229 bld.bld_base.op_actions[TGSI_OPCODE_ENDSUB].emit = endsub_emit;
2230 bld.bld_base.op_actions[TGSI_OPCODE_IF].emit = if_emit;
2231 bld.bld_base.op_actions[TGSI_OPCODE_KIL].emit = kil_emit;
2232 bld.bld_base.op_actions[TGSI_OPCODE_KILP].emit = kilp_emit;
2233 bld.bld_base.op_actions[TGSI_OPCODE_NRM].emit = nrm_emit;
2234 bld.bld_base.op_actions[TGSI_OPCODE_NRM4].emit = nrm_emit;
2235 bld.bld_base.op_actions[TGSI_OPCODE_RET].emit = ret_emit;
2236 bld.bld_base.op_actions[TGSI_OPCODE_TEX].emit = tex_emit;
2237 bld.bld_base.op_actions[TGSI_OPCODE_TXB].emit = txb_emit;
2238 bld.bld_base.op_actions[TGSI_OPCODE_TXD].emit = txd_emit;
2239 bld.bld_base.op_actions[TGSI_OPCODE_TXL].emit = txl_emit;
2240 bld.bld_base.op_actions[TGSI_OPCODE_TXP].emit = txp_emit;
2241 bld.bld_base.op_actions[TGSI_OPCODE_TXQ].emit = txq_emit;
2242 bld.bld_base.op_actions[TGSI_OPCODE_TXF].emit = txf_emit;
2243
2244 lp_exec_mask_init(&bld.exec_mask, &bld.bld_base.base);
2245
2246 bld.system_values = *system_values;
2247
2248 lp_build_tgsi_llvm(&bld.bld_base, tokens);
2249
2250 if (0) {
2251 LLVMBasicBlockRef block = LLVMGetInsertBlock(gallivm->builder);
2252 LLVMValueRef function = LLVMGetBasicBlockParent(block);
2253 debug_printf("11111111111111111111111111111 \n");
2254 tgsi_dump(tokens, 0);
2255 lp_debug_dump_value(function);
2256 debug_printf("2222222222222222222222222222 \n");
2257 }
2258
2259 if (0) {
2260 LLVMModuleRef module = LLVMGetGlobalParent(
2261 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm->builder)));
2262 LLVMDumpModule(module);
2263
2264 }
2265 }