gallium: Drop unused BRA opcode.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_tgsi_aos.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * TGSI to LLVM IR translation -- AoS.
31 *
32 * FIXME:
33 * - No control flow support: the existing control flow code should be factored
34 * out into from the SoA code into a common module and shared.
35 * - No derivatives. Derivate logic should be pluggable, just like the samplers.
36 *
37 * @author Jose Fonseca <jfonseca@vmware.com>
38 */
39
40 #include "pipe/p_config.h"
41 #include "pipe/p_shader_tokens.h"
42 #include "util/u_debug.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
45 #include "tgsi/tgsi_dump.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_type.h"
51 #include "lp_bld_const.h"
52 #include "lp_bld_arit.h"
53 #include "lp_bld_logic.h"
54 #include "lp_bld_swizzle.h"
55 #include "lp_bld_flow.h"
56 #include "lp_bld_quad.h"
57 #include "lp_bld_tgsi.h"
58 #include "lp_bld_debug.h"
59 #include "lp_bld_sample.h"
60
61
62 /**
63 * Wrapper around lp_build_swizzle_aos which translates swizzles to another
64 * ordering.
65 */
66 static LLVMValueRef
67 swizzle_aos(struct lp_build_tgsi_context *bld_base,
68 LLVMValueRef a,
69 unsigned swizzle_x,
70 unsigned swizzle_y,
71 unsigned swizzle_z,
72 unsigned swizzle_w)
73 {
74 unsigned char swizzles[4];
75 struct lp_build_tgsi_aos_context *bld = lp_aos_context(bld_base);
76
77 assert(swizzle_x < 4);
78 assert(swizzle_y < 4);
79 assert(swizzle_z < 4);
80 assert(swizzle_w < 4);
81
82 swizzles[bld->inv_swizzles[0]] = bld->swizzles[swizzle_x];
83 swizzles[bld->inv_swizzles[1]] = bld->swizzles[swizzle_y];
84 swizzles[bld->inv_swizzles[2]] = bld->swizzles[swizzle_z];
85 swizzles[bld->inv_swizzles[3]] = bld->swizzles[swizzle_w];
86
87 return lp_build_swizzle_aos(&bld->bld_base.base, a, swizzles);
88 }
89
90
91 static LLVMValueRef
92 swizzle_scalar_aos(struct lp_build_tgsi_aos_context *bld,
93 LLVMValueRef a,
94 unsigned chan)
95 {
96 chan = bld->swizzles[chan];
97 return lp_build_swizzle_scalar_aos(&bld->bld_base.base, a, chan, 4);
98 }
99
100
101 static LLVMValueRef
102 emit_fetch_constant(
103 struct lp_build_tgsi_context * bld_base,
104 const struct tgsi_full_src_register * reg,
105 enum tgsi_opcode_type stype,
106 unsigned swizzle)
107 {
108 struct lp_build_tgsi_aos_context * bld = lp_aos_context(bld_base);
109 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
110 struct lp_type type = bld_base->base.type;
111 LLVMValueRef res;
112 unsigned chan;
113
114 assert(!reg->Register.Indirect);
115
116 /*
117 * Get the constants components
118 */
119
120 res = bld->bld_base.base.undef;
121 for (chan = 0; chan < 4; ++chan) {
122 LLVMValueRef index;
123 LLVMValueRef scalar_ptr;
124 LLVMValueRef scalar;
125 LLVMValueRef swizzle;
126
127 index = lp_build_const_int32(bld->bld_base.base.gallivm,
128 reg->Register.Index * 4 + chan);
129
130 scalar_ptr = LLVMBuildGEP(builder, bld->consts_ptr, &index, 1, "");
131
132 scalar = LLVMBuildLoad(builder, scalar_ptr, "");
133
134 lp_build_name(scalar, "const[%u].%c", reg->Register.Index, "xyzw"[chan]);
135
136 /*
137 * NOTE: constants array is always assumed to be RGBA
138 */
139
140 swizzle = lp_build_const_int32(bld->bld_base.base.gallivm,
141 bld->swizzles[chan]);
142
143 res = LLVMBuildInsertElement(builder, res, scalar, swizzle, "");
144 }
145
146 /*
147 * Broadcast the first quaternion to all others.
148 *
149 * XXX: could be factored into a reusable function.
150 */
151
152 if (type.length > 4) {
153 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
154 unsigned i;
155
156 for (chan = 0; chan < 4; ++chan) {
157 shuffles[chan] = lp_build_const_int32(bld->bld_base.base.gallivm, chan);
158 }
159
160 for (i = 4; i < type.length; ++i) {
161 shuffles[i] = shuffles[i % 4];
162 }
163
164 res = LLVMBuildShuffleVector(builder,
165 res, bld->bld_base.base.undef,
166 LLVMConstVector(shuffles, type.length),
167 "");
168 }
169 return res;
170 }
171
172 static LLVMValueRef
173 emit_fetch_immediate(
174 struct lp_build_tgsi_context * bld_base,
175 const struct tgsi_full_src_register * reg,
176 enum tgsi_opcode_type stype,
177 unsigned swizzle)
178 {
179 struct lp_build_tgsi_aos_context * bld = lp_aos_context(bld_base);
180 LLVMValueRef res = bld->immediates[reg->Register.Index];
181 assert(res);
182 return res;
183 }
184
185 static LLVMValueRef
186 emit_fetch_input(
187 struct lp_build_tgsi_context * bld_base,
188 const struct tgsi_full_src_register * reg,
189 enum tgsi_opcode_type stype,
190 unsigned swizzle)
191 {
192 struct lp_build_tgsi_aos_context * bld = lp_aos_context(bld_base);
193 LLVMValueRef res = bld->inputs[reg->Register.Index];
194 assert(!reg->Register.Indirect);
195 assert(res);
196 return res;
197 }
198
199 static LLVMValueRef
200 emit_fetch_temporary(
201 struct lp_build_tgsi_context * bld_base,
202 const struct tgsi_full_src_register * reg,
203 enum tgsi_opcode_type stype,
204 unsigned swizzle)
205 {
206 struct lp_build_tgsi_aos_context * bld = lp_aos_context(bld_base);
207 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
208 LLVMValueRef temp_ptr = bld->temps[reg->Register.Index];
209 LLVMValueRef res = LLVMBuildLoad(builder, temp_ptr, "");
210 assert(!reg->Register.Indirect);
211 if (!res)
212 return bld->bld_base.base.undef;
213
214 return res;
215 }
216
217 /**
218 * Register store.
219 */
220 void
221 lp_emit_store_aos(
222 struct lp_build_tgsi_aos_context *bld,
223 const struct tgsi_full_instruction *inst,
224 unsigned index,
225 LLVMValueRef value)
226 {
227 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
228 const struct tgsi_full_dst_register *reg = &inst->Dst[index];
229 LLVMValueRef mask = NULL;
230 LLVMValueRef ptr;
231
232 /*
233 * Saturate the value
234 */
235
236 switch (inst->Instruction.Saturate) {
237 case TGSI_SAT_NONE:
238 break;
239
240 case TGSI_SAT_ZERO_ONE:
241 value = lp_build_max(&bld->bld_base.base, value, bld->bld_base.base.zero);
242 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one);
243 break;
244
245 case TGSI_SAT_MINUS_PLUS_ONE:
246 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));
247 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one);
248 break;
249
250 default:
251 assert(0);
252 }
253
254 /*
255 * Translate the register file
256 */
257
258 assert(!reg->Register.Indirect);
259
260 switch (reg->Register.File) {
261 case TGSI_FILE_OUTPUT:
262 ptr = bld->outputs[reg->Register.Index];
263 break;
264
265 case TGSI_FILE_TEMPORARY:
266 ptr = bld->temps[reg->Register.Index];
267 break;
268
269 case TGSI_FILE_ADDRESS:
270 ptr = bld->addr[reg->Indirect.Index];
271 break;
272
273 case TGSI_FILE_PREDICATE:
274 ptr = bld->preds[reg->Register.Index];
275 break;
276
277 default:
278 assert(0);
279 return;
280 }
281
282 if (!ptr)
283 return;
284 /*
285 * Predicate
286 */
287
288 if (inst->Instruction.Predicate) {
289 LLVMValueRef pred;
290
291 assert(inst->Predicate.Index < LP_MAX_TGSI_PREDS);
292
293 pred = LLVMBuildLoad(builder,
294 bld->preds[inst->Predicate.Index], "");
295
296 /*
297 * Convert the value to an integer mask.
298 */
299 pred = lp_build_compare(bld->bld_base.base.gallivm,
300 bld->bld_base.base.type,
301 PIPE_FUNC_NOTEQUAL,
302 pred,
303 bld->bld_base.base.zero);
304
305 if (inst->Predicate.Negate) {
306 pred = LLVMBuildNot(builder, pred, "");
307 }
308
309 pred = bld->bld_base.emit_swizzle(&bld->bld_base, pred,
310 inst->Predicate.SwizzleX,
311 inst->Predicate.SwizzleY,
312 inst->Predicate.SwizzleZ,
313 inst->Predicate.SwizzleW);
314
315 if (mask) {
316 mask = LLVMBuildAnd(builder, mask, pred, "");
317 } else {
318 mask = pred;
319 }
320 }
321
322 /*
323 * Writemask
324 */
325
326 if (reg->Register.WriteMask != TGSI_WRITEMASK_XYZW) {
327 LLVMValueRef writemask;
328
329 writemask = lp_build_const_mask_aos_swizzled(bld->bld_base.base.gallivm,
330 bld->bld_base.base.type,
331 reg->Register.WriteMask,
332 TGSI_NUM_CHANNELS,
333 bld->swizzles);
334
335 if (mask) {
336 mask = LLVMBuildAnd(builder, mask, writemask, "");
337 } else {
338 mask = writemask;
339 }
340 }
341
342 if (mask) {
343 LLVMValueRef orig_value;
344
345 orig_value = LLVMBuildLoad(builder, ptr, "");
346 value = lp_build_select(&bld->bld_base.base,
347 mask, value, orig_value);
348 }
349
350 LLVMBuildStore(builder, value, ptr);
351 }
352
353
354 /**
355 * High-level instruction translators.
356 */
357
358 static LLVMValueRef
359 emit_tex(struct lp_build_tgsi_aos_context *bld,
360 const struct tgsi_full_instruction *inst,
361 enum lp_build_tex_modifier modifier)
362 {
363 unsigned target;
364 unsigned unit;
365 LLVMValueRef coords;
366 struct lp_derivatives derivs = { {NULL}, {NULL} };
367
368 if (!bld->sampler) {
369 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
370 return bld->bld_base.base.undef;
371 }
372
373 target = inst->Texture.Texture;
374
375 coords = lp_build_emit_fetch( &bld->bld_base, inst, 0 , LP_CHAN_ALL);
376
377 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
378 /* probably not going to work */
379 derivs.ddx[0] = lp_build_emit_fetch( &bld->bld_base, inst, 1 , LP_CHAN_ALL);
380 derivs.ddy[0] = lp_build_emit_fetch( &bld->bld_base, inst, 2 , LP_CHAN_ALL);
381 unit = inst->Src[3].Register.Index;
382 }
383 else {
384 unit = inst->Src[1].Register.Index;
385 }
386 return bld->sampler->emit_fetch_texel(bld->sampler,
387 &bld->bld_base.base,
388 target, unit,
389 coords, derivs,
390 modifier);
391 }
392
393
394 static LLVMValueRef
395 emit_sample(struct lp_build_tgsi_aos_context *bld,
396 const struct tgsi_full_instruction *inst,
397 enum lp_build_tex_modifier modifier)
398 {
399 unsigned target;
400 unsigned unit;
401 LLVMValueRef coords;
402 struct lp_derivatives derivs = { {NULL}, {NULL} };
403
404 if (!bld->sampler) {
405 _debug_printf("warning: found texture instruction but no sampler generator supplied\n");
406 return bld->bld_base.base.undef;
407 }
408
409 coords = lp_build_emit_fetch( &bld->bld_base, inst, 0 , LP_CHAN_ALL);
410
411 /* ignore modifiers, can't handle different sampler / sampler view, etc... */
412 unit = inst->Src[1].Register.Index;
413 assert(inst->Src[2].Register.Index == unit);
414
415 target = bld->sv[unit].Resource;
416
417 return bld->sampler->emit_fetch_texel(bld->sampler,
418 &bld->bld_base.base,
419 target, unit,
420 coords, derivs,
421 modifier);
422 }
423
424
425 void
426 lp_emit_declaration_aos(
427 struct lp_build_tgsi_aos_context *bld,
428 const struct tgsi_full_declaration *decl)
429 {
430 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
431 LLVMTypeRef vec_type = lp_build_vec_type(bld->bld_base.base.gallivm, bld->bld_base.base.type);
432
433 unsigned first = decl->Range.First;
434 unsigned last = decl->Range.Last;
435 unsigned idx;
436
437 for (idx = first; idx <= last; ++idx) {
438 switch (decl->Declaration.File) {
439 case TGSI_FILE_TEMPORARY:
440 assert(idx < LP_MAX_INLINED_TEMPS);
441 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
442 LLVMValueRef array_size = lp_build_const_int32(gallivm, last + 1);
443 bld->temps_array = lp_build_array_alloca(bld->bld_base.base.gallivm,
444 vec_type, array_size, "");
445 } else {
446 bld->temps[idx] = lp_build_alloca(gallivm, vec_type, "");
447 }
448 break;
449
450 case TGSI_FILE_OUTPUT:
451 bld->outputs[idx] = lp_build_alloca(gallivm, vec_type, "");
452 break;
453
454 case TGSI_FILE_ADDRESS:
455 assert(idx < LP_MAX_TGSI_ADDRS);
456 bld->addr[idx] = lp_build_alloca(gallivm, vec_type, "");
457 break;
458
459 case TGSI_FILE_PREDICATE:
460 assert(idx < LP_MAX_TGSI_PREDS);
461 bld->preds[idx] = lp_build_alloca(gallivm, vec_type, "");
462 break;
463
464 case TGSI_FILE_SAMPLER_VIEW:
465 /*
466 * The target stored here MUST match whatever there actually
467 * is in the set sampler views (what about return type?).
468 */
469 assert(last < PIPE_MAX_SHADER_SAMPLER_VIEWS);
470 for (idx = first; idx <= last; ++idx) {
471 bld->sv[idx] = decl->SamplerView;
472 }
473 break;
474
475 default:
476 /* don't need to declare other vars */
477 break;
478 }
479 }
480 }
481
482
483 /**
484 * Emit LLVM for one TGSI instruction.
485 * \param return TRUE for success, FALSE otherwise
486 */
487 boolean
488 lp_emit_instruction_aos(
489 struct lp_build_tgsi_aos_context *bld,
490 const struct tgsi_full_instruction *inst,
491 const struct tgsi_opcode_info *info,
492 int *pc)
493 {
494 LLVMValueRef src0, src1, src2;
495 LLVMValueRef tmp0, tmp1;
496 LLVMValueRef dst0 = NULL;
497
498 /*
499 * Stores and write masks are handled in a general fashion after the long
500 * instruction opcode switch statement.
501 *
502 * Although not stricitly necessary, we avoid generating instructions for
503 * channels which won't be stored, in cases where's that easy. For some
504 * complex instructions, like texture sampling, it is more convenient to
505 * assume a full writemask and then let LLVM optimization passes eliminate
506 * redundant code.
507 */
508
509 (*pc)++;
510
511 assert(info->num_dst <= 1);
512 if (info->num_dst) {
513 dst0 = bld->bld_base.base.undef;
514 }
515
516 switch (inst->Instruction.Opcode) {
517 case TGSI_OPCODE_ARL:
518 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
519 dst0 = lp_build_floor(&bld->bld_base.base, src0);
520 break;
521
522 case TGSI_OPCODE_MOV:
523 dst0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
524 break;
525
526 case TGSI_OPCODE_LIT:
527 return FALSE;
528
529 case TGSI_OPCODE_RCP:
530 /* TGSI_OPCODE_RECIP */
531 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
532 dst0 = lp_build_rcp(&bld->bld_base.base, src0);
533 break;
534
535 case TGSI_OPCODE_RSQ:
536 /* TGSI_OPCODE_RECIPSQRT */
537 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
538 tmp0 = lp_build_emit_llvm_unary(&bld->bld_base, TGSI_OPCODE_ABS, src0);
539 dst0 = lp_build_rsqrt(&bld->bld_base.base, tmp0);
540 break;
541
542 case TGSI_OPCODE_EXP:
543 return FALSE;
544
545 case TGSI_OPCODE_LOG:
546 return FALSE;
547
548 case TGSI_OPCODE_MUL:
549 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
550 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
551 dst0 = lp_build_mul(&bld->bld_base.base, src0, src1);
552 break;
553
554 case TGSI_OPCODE_ADD:
555 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
556 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
557 dst0 = lp_build_add(&bld->bld_base.base, src0, src1);
558 break;
559
560 case TGSI_OPCODE_DP3:
561 /* TGSI_OPCODE_DOT3 */
562 return FALSE;
563
564 case TGSI_OPCODE_DP4:
565 /* TGSI_OPCODE_DOT4 */
566 return FALSE;
567
568 case TGSI_OPCODE_DST:
569 return FALSE;
570
571 case TGSI_OPCODE_MIN:
572 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
573 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
574 dst0 = lp_build_min(&bld->bld_base.base, src0, src1);
575 break;
576
577 case TGSI_OPCODE_MAX:
578 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
579 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
580 dst0 = lp_build_max(&bld->bld_base.base, src0, src1);
581 break;
582
583 case TGSI_OPCODE_SLT:
584 /* TGSI_OPCODE_SETLT */
585 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
586 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
587 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_LESS, src0, src1);
588 dst0 = lp_build_select(&bld->bld_base.base, tmp0, bld->bld_base.base.one, bld->bld_base.base.zero);
589 break;
590
591 case TGSI_OPCODE_SGE:
592 /* TGSI_OPCODE_SETGE */
593 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
594 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
595 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_GEQUAL, src0, src1);
596 dst0 = lp_build_select(&bld->bld_base.base, tmp0, bld->bld_base.base.one, bld->bld_base.base.zero);
597 break;
598
599 case TGSI_OPCODE_MAD:
600 /* TGSI_OPCODE_MADD */
601 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
602 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
603 src2 = lp_build_emit_fetch(&bld->bld_base, inst, 2, LP_CHAN_ALL);
604 tmp0 = lp_build_mul(&bld->bld_base.base, src0, src1);
605 dst0 = lp_build_add(&bld->bld_base.base, tmp0, src2);
606 break;
607
608 case TGSI_OPCODE_SUB:
609 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
610 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
611 dst0 = lp_build_sub(&bld->bld_base.base, src0, src1);
612 break;
613
614 case TGSI_OPCODE_LRP:
615 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
616 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
617 src2 = lp_build_emit_fetch(&bld->bld_base, inst, 2, LP_CHAN_ALL);
618 tmp0 = lp_build_sub(&bld->bld_base.base, src1, src2);
619 tmp0 = lp_build_mul(&bld->bld_base.base, src0, tmp0);
620 dst0 = lp_build_add(&bld->bld_base.base, tmp0, src2);
621 break;
622
623 case TGSI_OPCODE_CND:
624 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
625 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
626 src2 = lp_build_emit_fetch(&bld->bld_base, inst, 2, LP_CHAN_ALL);
627 tmp1 = lp_build_const_vec(bld->bld_base.base.gallivm, bld->bld_base.base.type, 0.5);
628 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_GREATER, src2, tmp1);
629 dst0 = lp_build_select(&bld->bld_base.base, tmp0, src0, src1);
630 break;
631
632 case TGSI_OPCODE_DP2A:
633 return FALSE;
634
635 case TGSI_OPCODE_FRC:
636 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
637 tmp0 = lp_build_floor(&bld->bld_base.base, src0);
638 dst0 = lp_build_sub(&bld->bld_base.base, src0, tmp0);
639 break;
640
641 case TGSI_OPCODE_CLAMP:
642 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
643 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
644 src2 = lp_build_emit_fetch(&bld->bld_base, inst, 2, LP_CHAN_ALL);
645 tmp0 = lp_build_max(&bld->bld_base.base, src0, src1);
646 dst0 = lp_build_min(&bld->bld_base.base, tmp0, src2);
647 break;
648
649 case TGSI_OPCODE_FLR:
650 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
651 dst0 = lp_build_floor(&bld->bld_base.base, src0);
652 break;
653
654 case TGSI_OPCODE_ROUND:
655 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
656 dst0 = lp_build_round(&bld->bld_base.base, src0);
657 break;
658
659 case TGSI_OPCODE_EX2:
660 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
661 tmp0 = lp_build_swizzle_scalar_aos(&bld->bld_base.base, src0, TGSI_SWIZZLE_X, TGSI_NUM_CHANNELS);
662 dst0 = lp_build_exp2(&bld->bld_base.base, tmp0);
663 break;
664
665 case TGSI_OPCODE_LG2:
666 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
667 tmp0 = swizzle_scalar_aos(bld, src0, TGSI_SWIZZLE_X);
668 dst0 = lp_build_log2(&bld->bld_base.base, tmp0);
669 break;
670
671 case TGSI_OPCODE_POW:
672 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
673 src0 = swizzle_scalar_aos(bld, src0, TGSI_SWIZZLE_X);
674 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
675 src1 = swizzle_scalar_aos(bld, src1, TGSI_SWIZZLE_X);
676 dst0 = lp_build_pow(&bld->bld_base.base, src0, src1);
677 break;
678
679 case TGSI_OPCODE_XPD:
680 return FALSE;
681
682 case TGSI_OPCODE_DPH:
683 return FALSE;
684
685 case TGSI_OPCODE_COS:
686 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
687 tmp0 = swizzle_scalar_aos(bld, src0, TGSI_SWIZZLE_X);
688 dst0 = lp_build_cos(&bld->bld_base.base, tmp0);
689 break;
690
691 case TGSI_OPCODE_DDX:
692 return FALSE;
693
694 case TGSI_OPCODE_DDY:
695 return FALSE;
696
697 case TGSI_OPCODE_KILL:
698 return FALSE;
699
700 case TGSI_OPCODE_KILL_IF:
701 return FALSE;
702
703 case TGSI_OPCODE_PK2H:
704 return FALSE;
705 break;
706
707 case TGSI_OPCODE_PK2US:
708 return FALSE;
709 break;
710
711 case TGSI_OPCODE_PK4B:
712 return FALSE;
713 break;
714
715 case TGSI_OPCODE_PK4UB:
716 return FALSE;
717
718 case TGSI_OPCODE_SEQ:
719 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
720 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
721 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_EQUAL, src0, src1);
722 dst0 = lp_build_select(&bld->bld_base.base, tmp0, bld->bld_base.base.one, bld->bld_base.base.zero);
723 break;
724
725 case TGSI_OPCODE_SGT:
726 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
727 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
728 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_GREATER, src0, src1);
729 dst0 = lp_build_select(&bld->bld_base.base, tmp0, bld->bld_base.base.one, bld->bld_base.base.zero);
730 break;
731
732 case TGSI_OPCODE_SIN:
733 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
734 tmp0 = swizzle_scalar_aos(bld, src0, TGSI_SWIZZLE_X);
735 dst0 = lp_build_sin(&bld->bld_base.base, tmp0);
736 break;
737
738 case TGSI_OPCODE_SLE:
739 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
740 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
741 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_LEQUAL, src0, src1);
742 dst0 = lp_build_select(&bld->bld_base.base, tmp0, bld->bld_base.base.one, bld->bld_base.base.zero);
743 break;
744
745 case TGSI_OPCODE_SNE:
746 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
747 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
748 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_NOTEQUAL, src0, src1);
749 dst0 = lp_build_select(&bld->bld_base.base, tmp0, bld->bld_base.base.one, bld->bld_base.base.zero);
750 break;
751
752 case TGSI_OPCODE_TEX:
753 dst0 = emit_tex(bld, inst, LP_BLD_TEX_MODIFIER_NONE);
754 break;
755
756 case TGSI_OPCODE_TXD:
757 dst0 = emit_tex(bld, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV);
758 break;
759
760 case TGSI_OPCODE_UP2H:
761 /* deprecated */
762 assert (0);
763 return FALSE;
764 break;
765
766 case TGSI_OPCODE_UP2US:
767 /* deprecated */
768 assert(0);
769 return FALSE;
770 break;
771
772 case TGSI_OPCODE_UP4B:
773 /* deprecated */
774 assert(0);
775 return FALSE;
776 break;
777
778 case TGSI_OPCODE_UP4UB:
779 /* deprecated */
780 assert(0);
781 return FALSE;
782 break;
783
784 case TGSI_OPCODE_ARR:
785 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
786 dst0 = lp_build_round(&bld->bld_base.base, src0);
787 break;
788
789 case TGSI_OPCODE_CAL:
790 return FALSE;
791
792 case TGSI_OPCODE_RET:
793 /* safe to ignore at end */
794 break;
795
796 case TGSI_OPCODE_END:
797 *pc = -1;
798 break;
799
800 case TGSI_OPCODE_SSG:
801 /* TGSI_OPCODE_SGN */
802 tmp0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
803 dst0 = lp_build_sgn(&bld->bld_base.base, tmp0);
804 break;
805
806 case TGSI_OPCODE_CMP:
807 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
808 src1 = lp_build_emit_fetch(&bld->bld_base, inst, 1, LP_CHAN_ALL);
809 src2 = lp_build_emit_fetch(&bld->bld_base, inst, 2, LP_CHAN_ALL);
810 tmp0 = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_LESS, src0, bld->bld_base.base.zero);
811 dst0 = lp_build_select(&bld->bld_base.base, tmp0, src1, src2);
812 break;
813
814 case TGSI_OPCODE_SCS:
815 return FALSE;
816
817 case TGSI_OPCODE_TXB:
818 dst0 = emit_tex(bld, inst, LP_BLD_TEX_MODIFIER_LOD_BIAS);
819 break;
820
821 case TGSI_OPCODE_DIV:
822 assert(0);
823 return FALSE;
824 break;
825
826 case TGSI_OPCODE_DP2:
827 return FALSE;
828
829 case TGSI_OPCODE_TXL:
830 dst0 = emit_tex(bld, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD);
831 break;
832
833 case TGSI_OPCODE_TXP:
834 dst0 = emit_tex(bld, inst, LP_BLD_TEX_MODIFIER_PROJECTED);
835 break;
836
837 case TGSI_OPCODE_BRK:
838 return FALSE;
839
840 case TGSI_OPCODE_IF:
841 case TGSI_OPCODE_UIF:
842 return FALSE;
843
844 case TGSI_OPCODE_BGNLOOP:
845 return FALSE;
846
847 case TGSI_OPCODE_BGNSUB:
848 return FALSE;
849
850 case TGSI_OPCODE_ELSE:
851 return FALSE;
852
853 case TGSI_OPCODE_ENDIF:
854 return FALSE;
855
856 case TGSI_OPCODE_ENDLOOP:
857 return FALSE;
858
859 case TGSI_OPCODE_ENDSUB:
860 return FALSE;
861
862 case TGSI_OPCODE_PUSHA:
863 /* deprecated? */
864 assert(0);
865 return FALSE;
866 break;
867
868 case TGSI_OPCODE_POPA:
869 /* deprecated? */
870 assert(0);
871 return FALSE;
872 break;
873
874 case TGSI_OPCODE_CEIL:
875 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
876 dst0 = lp_build_ceil(&bld->bld_base.base, src0);
877 break;
878
879 case TGSI_OPCODE_I2F:
880 assert(0);
881 return FALSE;
882 break;
883
884 case TGSI_OPCODE_NOT:
885 assert(0);
886 return FALSE;
887 break;
888
889 case TGSI_OPCODE_TRUNC:
890 src0 = lp_build_emit_fetch(&bld->bld_base, inst, 0, LP_CHAN_ALL);
891 dst0 = lp_build_trunc(&bld->bld_base.base, src0);
892 break;
893
894 case TGSI_OPCODE_SHL:
895 assert(0);
896 return FALSE;
897 break;
898
899 case TGSI_OPCODE_ISHR:
900 assert(0);
901 return FALSE;
902 break;
903
904 case TGSI_OPCODE_AND:
905 assert(0);
906 return FALSE;
907 break;
908
909 case TGSI_OPCODE_OR:
910 assert(0);
911 return FALSE;
912 break;
913
914 case TGSI_OPCODE_MOD:
915 assert(0);
916 return FALSE;
917 break;
918
919 case TGSI_OPCODE_XOR:
920 assert(0);
921 return FALSE;
922 break;
923
924 case TGSI_OPCODE_SAD:
925 assert(0);
926 return FALSE;
927 break;
928
929 case TGSI_OPCODE_TXF:
930 assert(0);
931 return FALSE;
932 break;
933
934 case TGSI_OPCODE_TXQ:
935 assert(0);
936 return FALSE;
937 break;
938
939 case TGSI_OPCODE_CONT:
940 return FALSE;
941
942 case TGSI_OPCODE_EMIT:
943 return FALSE;
944 break;
945
946 case TGSI_OPCODE_ENDPRIM:
947 return FALSE;
948 break;
949
950 case TGSI_OPCODE_NOP:
951 break;
952
953 case TGSI_OPCODE_SAMPLE:
954 dst0 = emit_sample(bld, inst, LP_BLD_TEX_MODIFIER_NONE);
955 break;
956
957 default:
958 return FALSE;
959 }
960
961 if (info->num_dst) {
962 lp_emit_store_aos(bld, inst, 0, dst0);
963 }
964
965 return TRUE;
966 }
967
968
969 void
970 lp_build_tgsi_aos(struct gallivm_state *gallivm,
971 const struct tgsi_token *tokens,
972 struct lp_type type,
973 const unsigned char swizzles[4],
974 LLVMValueRef consts_ptr,
975 const LLVMValueRef *inputs,
976 LLVMValueRef *outputs,
977 struct lp_build_sampler_aos *sampler,
978 const struct tgsi_shader_info *info)
979 {
980 struct lp_build_tgsi_aos_context bld;
981 struct tgsi_parse_context parse;
982 uint num_immediates = 0;
983 unsigned chan;
984 int pc = 0;
985
986 /* Setup build context */
987 memset(&bld, 0, sizeof bld);
988 lp_build_context_init(&bld.bld_base.base, gallivm, type);
989 lp_build_context_init(&bld.bld_base.uint_bld, gallivm, lp_uint_type(type));
990 lp_build_context_init(&bld.bld_base.int_bld, gallivm, lp_int_type(type));
991 lp_build_context_init(&bld.int_bld, gallivm, lp_int_type(type));
992
993 for (chan = 0; chan < 4; ++chan) {
994 bld.swizzles[chan] = swizzles[chan];
995 bld.inv_swizzles[swizzles[chan]] = chan;
996 }
997
998 bld.inputs = inputs;
999 bld.outputs = outputs;
1000 bld.consts_ptr = consts_ptr;
1001 bld.sampler = sampler;
1002 bld.indirect_files = info->indirect_files;
1003 bld.bld_base.emit_swizzle = swizzle_aos;
1004 bld.bld_base.info = info;
1005
1006 bld.bld_base.emit_fetch_funcs[TGSI_FILE_CONSTANT] = emit_fetch_constant;
1007 bld.bld_base.emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = emit_fetch_immediate;
1008 bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_input;
1009 bld.bld_base.emit_fetch_funcs[TGSI_FILE_TEMPORARY] = emit_fetch_temporary;
1010
1011 /* Set opcode actions */
1012 lp_set_default_actions_cpu(&bld.bld_base);
1013
1014 if (!lp_bld_tgsi_list_init(&bld.bld_base)) {
1015 return;
1016 }
1017
1018 tgsi_parse_init(&parse, tokens);
1019
1020 while (!tgsi_parse_end_of_tokens(&parse)) {
1021 tgsi_parse_token(&parse);
1022
1023 switch(parse.FullToken.Token.Type) {
1024 case TGSI_TOKEN_TYPE_DECLARATION:
1025 /* Inputs already interpolated */
1026 lp_emit_declaration_aos(&bld, &parse.FullToken.FullDeclaration);
1027 break;
1028
1029 case TGSI_TOKEN_TYPE_INSTRUCTION:
1030 /* save expanded instruction */
1031 lp_bld_tgsi_add_instruction(&bld.bld_base,
1032 &parse.FullToken.FullInstruction);
1033 break;
1034
1035 case TGSI_TOKEN_TYPE_IMMEDIATE:
1036 /* simply copy the immediate values into the next immediates[] slot */
1037 {
1038 const uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
1039 float imm[4];
1040 assert(size <= 4);
1041 assert(num_immediates < LP_MAX_INLINED_IMMEDIATES);
1042 for (chan = 0; chan < 4; ++chan) {
1043 imm[chan] = 0.0f;
1044 }
1045 for (chan = 0; chan < size; ++chan) {
1046 unsigned swizzle = bld.swizzles[chan];
1047 imm[swizzle] = parse.FullToken.FullImmediate.u[chan].Float;
1048 }
1049 bld.immediates[num_immediates] =
1050 lp_build_const_aos(gallivm, type,
1051 imm[0], imm[1], imm[2], imm[3],
1052 NULL);
1053 num_immediates++;
1054 }
1055 break;
1056
1057 case TGSI_TOKEN_TYPE_PROPERTY:
1058 break;
1059
1060 default:
1061 assert(0);
1062 }
1063 }
1064
1065 while (pc != -1) {
1066 struct tgsi_full_instruction *instr = bld.bld_base.instructions + pc;
1067 const struct tgsi_opcode_info *opcode_info =
1068 tgsi_get_opcode_info(instr->Instruction.Opcode);
1069 if (!lp_emit_instruction_aos(&bld, instr, opcode_info, &pc))
1070 _debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
1071 opcode_info->mnemonic);
1072 }
1073
1074 if (0) {
1075 LLVMBasicBlockRef block = LLVMGetInsertBlock(gallivm->builder);
1076 LLVMValueRef function = LLVMGetBasicBlockParent(block);
1077 debug_printf("11111111111111111111111111111 \n");
1078 tgsi_dump(tokens, 0);
1079 lp_debug_dump_value(function);
1080 debug_printf("2222222222222222222222222222 \n");
1081 }
1082 tgsi_parse_free(&parse);
1083 FREE(bld.bld_base.instructions);
1084
1085 if (0) {
1086 LLVMModuleRef module = LLVMGetGlobalParent(
1087 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm->builder)));
1088 LLVMDumpModule(module);
1089 }
1090
1091 }
1092