Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
  2.  
  3. /*
  4.  * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice (including the next
  14.  * paragraph) shall be included in all copies or substantial portions of the
  15.  * Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * Authors:
  26.  *    Rob Clark <robclark@freedesktop.org>
  27.  */
  28.  
  29. #include <stdarg.h>
  30.  
  31. #include "pipe/p_state.h"
  32. #include "util/u_string.h"
  33. #include "util/u_memory.h"
  34. #include "util/u_inlines.h"
  35. #include "tgsi/tgsi_parse.h"
  36. #include "tgsi/tgsi_ureg.h"
  37. #include "tgsi/tgsi_info.h"
  38. #include "tgsi/tgsi_strings.h"
  39. #include "tgsi/tgsi_dump.h"
  40. #include "tgsi/tgsi_scan.h"
  41.  
  42. #include "fd3_compiler.h"
  43. #include "fd3_program.h"
  44. #include "fd3_util.h"
  45.  
  46. #include "instr-a3xx.h"
  47. #include "ir-a3xx.h"
  48.  
  49. /* ************************************************************************* */
  50. /* split the out or find some helper to use.. like main/bitset.h.. */
  51.  
  52. #define MAX_REG 256
  53.  
  54. typedef uint8_t regmask_t[2 * MAX_REG / 8];
  55.  
  56. static unsigned regmask_idx(struct ir3_register *reg)
  57. {
  58.         unsigned num = reg->num;
  59.         assert(num < MAX_REG);
  60.         if (reg->flags & IR3_REG_HALF)
  61.                 num += MAX_REG;
  62.         return num;
  63. }
  64.  
  65. static void regmask_set(regmask_t regmask, struct ir3_register *reg)
  66. {
  67.         unsigned idx = regmask_idx(reg);
  68.         regmask[idx / 8] |= 1 << (idx % 8);
  69. }
  70.  
  71. static unsigned regmask_get(regmask_t regmask, struct ir3_register *reg)
  72. {
  73.         unsigned idx = regmask_idx(reg);
  74.         return regmask[idx / 8] & (1 << (idx % 8));
  75. }
  76.  
  77. /* ************************************************************************* */
  78.  
  79. struct fd3_compile_context {
  80.         const struct tgsi_token *tokens;
  81.         struct ir3_shader *ir;
  82.         struct fd3_shader_stateobj *so;
  83.  
  84.         struct tgsi_parse_context parser;
  85.         unsigned type;
  86.  
  87.         struct tgsi_shader_info info;
  88.  
  89.         /* last input dst (for setting (ei) flag): */
  90.         struct ir3_register *last_input;
  91.  
  92.         unsigned next_inloc;
  93.         unsigned num_internal_temps;
  94.  
  95.         /* track registers which need to synchronize w/ "complex alu" cat3
  96.          * instruction pipeline:
  97.          */
  98.         regmask_t needs_ss;
  99.  
  100.         /* track registers which need to synchronize with texture fetch
  101.          * pipeline:
  102.          */
  103.         regmask_t needs_sy;
  104.  
  105.         /* inputs start at r0, temporaries start after last input, and
  106.          * outputs start after last temporary.
  107.          *
  108.          * We could be more clever, because this is not a hw restriction,
  109.          * but probably best just to implement an optimizing pass to
  110.          * reduce the # of registers used and get rid of redundant mov's
  111.          * (to output register).
  112.          */
  113.         unsigned base_reg[TGSI_FILE_COUNT];
  114.  
  115.         /* idx/slot for last compiler generated immediate */
  116.         unsigned immediate_idx;
  117.  
  118.         /* stack of branch instructions that start (potentially nested)
  119.          * branch instructions, so that we can fix up the branch targets
  120.          * so that we can fix up the branch target on the corresponding
  121.          * END instruction
  122.          */
  123.         struct ir3_instruction *branch[16];
  124.         unsigned int branch_count;
  125.  
  126.         /* used when dst is same as one of the src, to avoid overwriting a
  127.          * src element before the remaining scalar instructions that make
  128.          * up the vector operation
  129.          */
  130.         struct tgsi_dst_register tmp_dst;
  131.         struct tgsi_src_register tmp_src;
  132. };
  133.  
  134. static unsigned
  135. compile_init(struct fd3_compile_context *ctx, struct fd3_shader_stateobj *so,
  136.                 const struct tgsi_token *tokens)
  137. {
  138.         unsigned ret;
  139.  
  140.         ctx->tokens = tokens;
  141.         ctx->ir = so->ir;
  142.         ctx->so = so;
  143.         ctx->last_input = NULL;
  144.         ctx->next_inloc = 8;
  145.         ctx->num_internal_temps = 0;
  146.         ctx->branch_count = 0;
  147.  
  148.         memset(ctx->needs_ss, 0, sizeof(ctx->needs_ss));
  149.         memset(ctx->needs_sy, 0, sizeof(ctx->needs_sy));
  150.         memset(ctx->base_reg, 0, sizeof(ctx->base_reg));
  151.  
  152.         tgsi_scan_shader(tokens, &ctx->info);
  153.  
  154.         /* Immediates go after constants: */
  155.         ctx->base_reg[TGSI_FILE_CONSTANT]  = 0;
  156.         ctx->base_reg[TGSI_FILE_IMMEDIATE] =
  157.                         ctx->info.file_count[TGSI_FILE_CONSTANT];
  158.  
  159.         /* Temporaries after outputs after inputs: */
  160.         ctx->base_reg[TGSI_FILE_INPUT]     = 0;
  161.         ctx->base_reg[TGSI_FILE_OUTPUT]    =
  162.                         ctx->info.file_count[TGSI_FILE_INPUT];
  163.         ctx->base_reg[TGSI_FILE_TEMPORARY] =
  164.                         ctx->info.file_count[TGSI_FILE_INPUT] +
  165.                         ctx->info.file_count[TGSI_FILE_OUTPUT];
  166.  
  167.         so->first_immediate = ctx->base_reg[TGSI_FILE_IMMEDIATE];
  168.         ctx->immediate_idx = 4 * (ctx->info.file_count[TGSI_FILE_CONSTANT] +
  169.                         ctx->info.file_count[TGSI_FILE_IMMEDIATE]);
  170.  
  171.         ret = tgsi_parse_init(&ctx->parser, tokens);
  172.         if (ret != TGSI_PARSE_OK)
  173.                 return ret;
  174.  
  175.         ctx->type = ctx->parser.FullHeader.Processor.Processor;
  176.  
  177.         return ret;
  178. }
  179.  
  180. static void
  181. compile_free(struct fd3_compile_context *ctx)
  182. {
  183.         tgsi_parse_free(&ctx->parser);
  184. }
  185.  
  186. struct instr_translater {
  187.         void (*fxn)(const struct instr_translater *t,
  188.                         struct fd3_compile_context *ctx,
  189.                         struct tgsi_full_instruction *inst);
  190.         unsigned tgsi_opc;
  191.         opc_t opc;
  192.         opc_t hopc;    /* opc to use for half_precision mode, if different */
  193.         unsigned arg;
  194. };
  195.  
  196. static struct ir3_register *
  197. add_dst_reg(struct fd3_compile_context *ctx, struct ir3_instruction *instr,
  198.                 const struct tgsi_dst_register *dst, unsigned chan)
  199. {
  200.         unsigned flags = 0, num = 0;
  201.  
  202.         switch (dst->File) {
  203.         case TGSI_FILE_OUTPUT:
  204.         case TGSI_FILE_TEMPORARY:
  205.                 num = dst->Index + ctx->base_reg[dst->File];
  206.                 break;
  207.         default:
  208.                 DBG("unsupported dst register file: %s",
  209.                         tgsi_file_name(dst->File));
  210.                 assert(0);
  211.                 break;
  212.         }
  213.  
  214.         if (ctx->so->half_precision)
  215.                 flags |= IR3_REG_HALF;
  216.  
  217.         return ir3_reg_create(instr, regid(num, chan), flags);
  218. }
  219.  
  220. static struct ir3_register *
  221. add_src_reg(struct fd3_compile_context *ctx, struct ir3_instruction *instr,
  222.                 const struct tgsi_src_register *src, unsigned chan)
  223. {
  224.         unsigned flags = 0, num = 0;
  225.         struct ir3_register *reg;
  226.  
  227.         switch (src->File) {
  228.         case TGSI_FILE_IMMEDIATE:
  229.                 /* TODO if possible, use actual immediate instead of const.. but
  230.                  * TGSI has vec4 immediates, we can only embed scalar (of limited
  231.                  * size, depending on instruction..)
  232.                  */
  233.         case TGSI_FILE_CONSTANT:
  234.                 flags |= IR3_REG_CONST;
  235.                 num = src->Index + ctx->base_reg[src->File];
  236.                 break;
  237.         case TGSI_FILE_INPUT:
  238.         case TGSI_FILE_TEMPORARY:
  239.                 num = src->Index + ctx->base_reg[src->File];
  240.                 break;
  241.         default:
  242.                 DBG("unsupported src register file: %s",
  243.                         tgsi_file_name(src->File));
  244.                 assert(0);
  245.                 break;
  246.         }
  247.  
  248.         if (src->Absolute)
  249.                 flags |= IR3_REG_ABS;
  250.         if (src->Negate)
  251.                 flags |= IR3_REG_NEGATE;
  252.         if (ctx->so->half_precision)
  253.                 flags |= IR3_REG_HALF;
  254.  
  255.         reg = ir3_reg_create(instr, regid(num, chan), flags);
  256.  
  257.         if (regmask_get(ctx->needs_ss, reg)) {
  258.                 instr->flags |= IR3_INSTR_SS;
  259.                 memset(ctx->needs_ss, 0, sizeof(ctx->needs_ss));
  260.         }
  261.  
  262.         if (regmask_get(ctx->needs_sy, reg)) {
  263.                 instr->flags |= IR3_INSTR_SY;
  264.                 memset(ctx->needs_sy, 0, sizeof(ctx->needs_sy));
  265.         }
  266.  
  267.         return reg;
  268. }
  269.  
  270. static void
  271. src_from_dst(struct tgsi_src_register *src, struct tgsi_dst_register *dst)
  272. {
  273.         src->File      = dst->File;
  274.         src->Indirect  = dst->Indirect;
  275.         src->Dimension = dst->Dimension;
  276.         src->Index     = dst->Index;
  277.         src->Absolute  = 0;
  278.         src->Negate    = 0;
  279.         src->SwizzleX  = TGSI_SWIZZLE_X;
  280.         src->SwizzleY  = TGSI_SWIZZLE_Y;
  281.         src->SwizzleZ  = TGSI_SWIZZLE_Z;
  282.         src->SwizzleW  = TGSI_SWIZZLE_W;
  283. }
  284.  
  285. /* Get internal-temp src/dst to use for a sequence of instructions
  286.  * generated by a single TGSI op.
  287.  */
  288. static void
  289. get_internal_temp(struct fd3_compile_context *ctx,
  290.                 struct tgsi_dst_register *tmp_dst,
  291.                 struct tgsi_src_register *tmp_src)
  292. {
  293.         int n;
  294.  
  295.         tmp_dst->File      = TGSI_FILE_TEMPORARY;
  296.         tmp_dst->WriteMask = TGSI_WRITEMASK_XYZW;
  297.         tmp_dst->Indirect  = 0;
  298.         tmp_dst->Dimension = 0;
  299.  
  300.         /* assign next temporary: */
  301.         n = ctx->num_internal_temps++;
  302.  
  303.         tmp_dst->Index = ctx->info.file_count[TGSI_FILE_TEMPORARY] + n;
  304.  
  305.         src_from_dst(tmp_src, tmp_dst);
  306. }
  307.  
  308. /* same as get_internal_temp, but w/ src.xxxx (for instructions that
  309.  * replicate their results)
  310.  */
  311. static void
  312. get_internal_temp_repl(struct fd3_compile_context *ctx,
  313.                 struct tgsi_dst_register *tmp_dst,
  314.                 struct tgsi_src_register *tmp_src)
  315. {
  316.         get_internal_temp(ctx, tmp_dst, tmp_src);
  317.         tmp_src->SwizzleX = tmp_src->SwizzleY =
  318.                 tmp_src->SwizzleZ = tmp_src->SwizzleW = TGSI_SWIZZLE_X;
  319. }
  320.  
  321. static void
  322. get_immediate(struct fd3_compile_context *ctx,
  323.                 struct tgsi_src_register *reg, uint32_t val)
  324. {
  325.         unsigned neg, swiz, idx, i;
  326.         /* actually maps 1:1 currently.. not sure if that is safe to rely on: */
  327.         static const unsigned swiz2tgsi[] = {
  328.                         TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_W,
  329.         };
  330.  
  331.         for (i = 0; i < ctx->immediate_idx; i++) {
  332.                 swiz = i % 4;
  333.                 idx  = i / 4;
  334.  
  335.                 if (ctx->so->immediates[idx].val[swiz] == val) {
  336.                         neg = 0;
  337.                         break;
  338.                 }
  339.  
  340.                 if (ctx->so->immediates[idx].val[swiz] == -val) {
  341.                         neg = 1;
  342.                         break;
  343.                 }
  344.         }
  345.  
  346.         if (i == ctx->immediate_idx) {
  347.                 /* need to generate a new immediate: */
  348.                 swiz = i % 4;
  349.                 idx  = i / 4;
  350.                 neg  = 0;
  351.                 ctx->so->immediates[idx].val[swiz] = val;
  352.                 ctx->so->immediates_count = idx + 1;
  353.                 ctx->immediate_idx++;
  354.         }
  355.  
  356.         reg->File      = TGSI_FILE_IMMEDIATE;
  357.         reg->Indirect  = 0;
  358.         reg->Dimension = 0;
  359.         reg->Index     = idx;
  360.         reg->Absolute  = 0;
  361.         reg->Negate    = neg;
  362.         reg->SwizzleX  = swiz2tgsi[swiz];
  363.         reg->SwizzleY  = swiz2tgsi[swiz];
  364.         reg->SwizzleZ  = swiz2tgsi[swiz];
  365.         reg->SwizzleW  = swiz2tgsi[swiz];
  366. }
  367.  
  368. static type_t
  369. get_type(struct fd3_compile_context *ctx)
  370. {
  371.         return ctx->so->half_precision ? TYPE_F16 : TYPE_F32;
  372. }
  373.  
  374. static unsigned
  375. src_swiz(struct tgsi_src_register *src, int chan)
  376. {
  377.         switch (chan) {
  378.         case 0: return src->SwizzleX;
  379.         case 1: return src->SwizzleY;
  380.         case 2: return src->SwizzleZ;
  381.         case 3: return src->SwizzleW;
  382.         }
  383.         assert(0);
  384.         return 0;
  385. }
  386.  
  387. static void
  388. create_mov(struct fd3_compile_context *ctx, struct tgsi_dst_register *dst,
  389.                 struct tgsi_src_register *src)
  390. {
  391.         type_t type_mov = get_type(ctx);
  392.         unsigned i;
  393.  
  394.         for (i = 0; i < 4; i++) {
  395.                 /* move to destination: */
  396.                 if (dst->WriteMask & (1 << i)) {
  397.                         struct ir3_instruction *instr =
  398.                                         ir3_instr_create(ctx->ir, 1, 0);
  399.                         instr->cat1.src_type = type_mov;
  400.                         instr->cat1.dst_type = type_mov;
  401.                         add_dst_reg(ctx, instr, dst, i);
  402.                         add_src_reg(ctx, instr, src, src_swiz(src, i));
  403.                 } else {
  404.                         ir3_instr_create(ctx->ir, 0, OPC_NOP);
  405.                 }
  406.         }
  407.  
  408. }
  409.  
  410. static struct tgsi_dst_register *
  411. get_dst(struct fd3_compile_context *ctx, struct tgsi_full_instruction *inst)
  412. {
  413.         struct tgsi_dst_register *dst = &inst->Dst[0].Register;
  414.         unsigned i;
  415.         for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
  416.                 struct tgsi_src_register *src = &inst->Src[i].Register;
  417.                 if ((src->File == dst->File) && (src->Index == dst->Index)) {
  418.                         get_internal_temp(ctx, &ctx->tmp_dst, &ctx->tmp_src);
  419.                         ctx->tmp_dst.WriteMask = dst->WriteMask;
  420.                         dst = &ctx->tmp_dst;
  421.                         break;
  422.                 }
  423.         }
  424.         return dst;
  425. }
  426.  
  427. static void
  428. put_dst(struct fd3_compile_context *ctx, struct tgsi_full_instruction *inst,
  429.                 struct tgsi_dst_register *dst)
  430. {
  431.         /* if necessary, add mov back into original dst: */
  432.         if (dst != &inst->Dst[0].Register) {
  433.                 create_mov(ctx, &inst->Dst[0].Register, &ctx->tmp_src);
  434.         }
  435. }
  436.  
  437. /* helper to generate the necessary repeat and/or additional instructions
  438.  * to turn a scalar instruction into a vector operation:
  439.  */
  440. static void
  441. vectorize(struct fd3_compile_context *ctx, struct ir3_instruction *instr,
  442.                 struct tgsi_dst_register *dst, int nsrcs, ...)
  443. {
  444.         va_list ap;
  445.         int i, j, n = 0;
  446.  
  447.         add_dst_reg(ctx, instr, dst, 0);
  448.  
  449.         va_start(ap, nsrcs);
  450.         for (j = 0; j < nsrcs; j++) {
  451.                 struct tgsi_src_register *src =
  452.                                 va_arg(ap, struct tgsi_src_register *);
  453.                 unsigned flags = va_arg(ap, unsigned);
  454.                 add_src_reg(ctx, instr, src, 0)->flags |= flags;
  455.         }
  456.         va_end(ap);
  457.  
  458.         for (i = 0; i < 4; i++) {
  459.                 if (dst->WriteMask & (1 << i)) {
  460.                         struct ir3_instruction *cur;
  461.  
  462.                         if (n++ == 0) {
  463.                                 cur = instr;
  464.                         } else {
  465.                                 cur = ir3_instr_clone(instr);
  466.                                 cur->flags &= ~(IR3_INSTR_SY | IR3_INSTR_SS | IR3_INSTR_JP);
  467.                         }
  468.  
  469.                         /* fix-up dst register component: */
  470.                         cur->regs[0]->num = regid(cur->regs[0]->num >> 2, i);
  471.  
  472.                         /* fix-up src register component: */
  473.                         va_start(ap, nsrcs);
  474.                         for (j = 0; j < nsrcs; j++) {
  475.                                 struct tgsi_src_register *src =
  476.                                                 va_arg(ap, struct tgsi_src_register *);
  477.                                 (void)va_arg(ap, unsigned);
  478.                                 cur->regs[j+1]->num =
  479.                                         regid(cur->regs[j+1]->num >> 2,
  480.                                                 src_swiz(src, i));
  481.                         }
  482.                         va_end(ap);
  483.                 }
  484.         }
  485.  
  486.         /* pad w/ nop's.. at least until we are clever enough to
  487.          * figure out if we really need to..
  488.          */
  489.         for (; n < 4; n++) {
  490.                 ir3_instr_create(instr->shader, 0, OPC_NOP);
  491.         }
  492. }
  493.  
  494. /*
  495.  * Handlers for TGSI instructions which do not have a 1:1 mapping to
  496.  * native instructions:
  497.  */
  498.  
  499. static void
  500. trans_dotp(const struct instr_translater *t,
  501.                 struct fd3_compile_context *ctx,
  502.                 struct tgsi_full_instruction *inst)
  503. {
  504.         struct ir3_instruction *instr;
  505.         struct tgsi_dst_register tmp_dst;
  506.         struct tgsi_src_register tmp_src;
  507.         struct tgsi_dst_register *dst  = &inst->Dst[0].Register;
  508.         struct tgsi_src_register *src0 = &inst->Src[0].Register;
  509.         struct tgsi_src_register *src1 = &inst->Src[1].Register;
  510.         unsigned swiz0[] = { src0->SwizzleX, src0->SwizzleY, src0->SwizzleZ, src0->SwizzleW };
  511.         unsigned swiz1[] = { src1->SwizzleX, src1->SwizzleY, src1->SwizzleZ, src1->SwizzleW };
  512.         opc_t opc_mad    = ctx->so->half_precision ? OPC_MAD_F16 : OPC_MAD_F32;
  513.         unsigned n = t->arg;     /* number of components */
  514.         unsigned i;
  515.  
  516.         get_internal_temp_repl(ctx, &tmp_dst, &tmp_src);
  517.  
  518.         /* Blob compiler never seems to use a const in src1 position for
  519.          * mad.*, although there does seem (according to disassembler
  520.          * hidden in libllvm-a3xx.so) to be a bit to indicate that src1
  521.          * is a const.  Not sure if this is a hw bug, or simply that the
  522.          * disassembler lies.
  523.          */
  524.         if ((src1->File == TGSI_FILE_IMMEDIATE) ||
  525.                         (src1->File == TGSI_FILE_CONSTANT)) {
  526.  
  527.                 /* the mov to tmp unswizzles src1, so now we have tmp.xyzw:
  528.                  */
  529.                 for (i = 0; i < 4; i++)
  530.                         swiz1[i] = i;
  531.  
  532.                 /* the first mul.f will clobber tmp.x, but that is ok
  533.                  * because after that point we no longer need tmp.x:
  534.                  */
  535.                 create_mov(ctx, &tmp_dst, src1);
  536.                 src1 = &tmp_src;
  537.         }
  538.  
  539.         instr = ir3_instr_create(ctx->ir, 2, OPC_MUL_F);
  540.         add_dst_reg(ctx, instr, &tmp_dst, 0);
  541.         add_src_reg(ctx, instr, src0, swiz0[0]);
  542.         add_src_reg(ctx, instr, src1, swiz1[0]);
  543.  
  544.         for (i = 1; i < n; i++) {
  545.                 ir3_instr_create(ctx->ir, 0, OPC_NOP);
  546.  
  547.                 instr = ir3_instr_create(ctx->ir, 3, opc_mad);
  548.                 add_dst_reg(ctx, instr, &tmp_dst, 0);
  549.                 add_src_reg(ctx, instr, src0, swiz0[i]);
  550.                 add_src_reg(ctx, instr, src1, swiz1[i]);
  551.                 add_src_reg(ctx, instr, &tmp_src, 0);
  552.         }
  553.  
  554.         /* DPH(a,b) = (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + b.w */
  555.         if (t->tgsi_opc == TGSI_OPCODE_DPH) {
  556.                 ir3_instr_create(ctx->ir, 0, OPC_NOP);
  557.  
  558.                 instr = ir3_instr_create(ctx->ir, 2, OPC_ADD_F);
  559.                 add_dst_reg(ctx, instr, &tmp_dst, 0);
  560.                 add_src_reg(ctx, instr, src1, swiz1[i]);
  561.                 add_src_reg(ctx, instr, &tmp_src, 0);
  562.  
  563.                 n++;
  564.         }
  565.  
  566.         ir3_instr_create(ctx->ir, 0, OPC_NOP);
  567.  
  568.         /* pad out to multiple of 4 scalar instructions: */
  569.         for (i = 2 * n; i % 4; i++) {
  570.                 ir3_instr_create(ctx->ir, 0, OPC_NOP);
  571.         }
  572.  
  573.         create_mov(ctx, dst, &tmp_src);
  574. }
  575.  
  576. /* LRP(a,b,c) = (a * b) + ((1 - a) * c) */
  577. static void
  578. trans_lrp(const struct instr_translater *t,
  579.                 struct fd3_compile_context *ctx,
  580.                 struct tgsi_full_instruction *inst)
  581. {
  582.         struct ir3_instruction *instr;
  583.         struct tgsi_dst_register tmp_dst1, tmp_dst2;
  584.         struct tgsi_src_register tmp_src1, tmp_src2;
  585.         struct tgsi_src_register tmp_const;
  586.  
  587.         get_internal_temp(ctx, &tmp_dst1, &tmp_src1);
  588.         get_internal_temp(ctx, &tmp_dst2, &tmp_src2);
  589.  
  590.         get_immediate(ctx, &tmp_const, fui(1.0));
  591.  
  592.         /* tmp1 = (a * b) */
  593.         instr = ir3_instr_create(ctx->ir, 2, OPC_MUL_F);
  594.         vectorize(ctx, instr, &tmp_dst1, 2,
  595.                         &inst->Src[0].Register, 0,
  596.                         &inst->Src[1].Register, 0);
  597.  
  598.         /* tmp2 = (1 - a) */
  599.         instr = ir3_instr_create(ctx->ir, 2, OPC_ADD_F);
  600.         vectorize(ctx, instr, &tmp_dst2, 2,
  601.                         &tmp_const, 0,
  602.                         &inst->Src[0].Register, IR3_REG_NEGATE);
  603.  
  604.         /* tmp2 = tmp2 * c */
  605.         instr = ir3_instr_create(ctx->ir, 2, OPC_MUL_F);
  606.         vectorize(ctx, instr, &tmp_dst2, 2,
  607.                         &tmp_src2, 0,
  608.                         &inst->Src[2].Register, 0);
  609.  
  610.         /* dst = tmp1 + tmp2 */
  611.         instr = ir3_instr_create(ctx->ir, 2, OPC_ADD_F);
  612.         vectorize(ctx, instr, &inst->Dst[0].Register, 2,
  613.                         &tmp_src1, 0,
  614.                         &tmp_src2, 0);
  615. }
  616.  
  617. /* FRC(x) = x - FLOOR(x) */
  618. static void
  619. trans_frac(const struct instr_translater *t,
  620.                 struct fd3_compile_context *ctx,
  621.                 struct tgsi_full_instruction *inst)
  622. {
  623.         struct ir3_instruction *instr;
  624.         struct tgsi_dst_register tmp_dst;
  625.         struct tgsi_src_register tmp_src;
  626.  
  627.         get_internal_temp(ctx, &tmp_dst, &tmp_src);
  628.  
  629.         /* tmp = FLOOR(x) */
  630.         instr = ir3_instr_create(ctx->ir, 2, OPC_FLOOR_F);
  631.         vectorize(ctx, instr, &tmp_dst, 1,
  632.                         &inst->Src[0].Register, 0);
  633.  
  634.         /* dst = x - tmp */
  635.         instr = ir3_instr_create(ctx->ir, 2, OPC_ADD_F);
  636.         vectorize(ctx, instr, &inst->Dst[0].Register, 2,
  637.                         &inst->Src[0].Register, 0,
  638.                         &tmp_src, IR3_REG_NEGATE);
  639. }
  640.  
  641. /* POW(a,b) = EXP2(b * LOG2(a)) */
  642. static void
  643. trans_pow(const struct instr_translater *t,
  644.                 struct fd3_compile_context *ctx,
  645.                 struct tgsi_full_instruction *inst)
  646. {
  647.         struct ir3_instruction *instr;
  648.         struct ir3_register *r;
  649.         struct tgsi_dst_register tmp_dst;
  650.         struct tgsi_src_register tmp_src;
  651.         struct tgsi_dst_register *dst  = &inst->Dst[0].Register;
  652.         struct tgsi_src_register *src0 = &inst->Src[0].Register;
  653.         struct tgsi_src_register *src1 = &inst->Src[1].Register;
  654.  
  655.         get_internal_temp_repl(ctx, &tmp_dst, &tmp_src);
  656.  
  657.         /* log2 Rtmp, Rsrc0 */
  658.         ir3_instr_create(ctx->ir, 0, OPC_NOP)->repeat = 5;
  659.         instr = ir3_instr_create(ctx->ir, 4, OPC_LOG2);
  660.         r = add_dst_reg(ctx, instr, &tmp_dst, 0);
  661.         add_src_reg(ctx, instr, src0, src0->SwizzleX);
  662.         regmask_set(ctx->needs_ss, r);
  663.  
  664.         /* mul.f Rtmp, Rtmp, Rsrc1 */
  665.         instr = ir3_instr_create(ctx->ir, 2, OPC_MUL_F);
  666.         add_dst_reg(ctx, instr, &tmp_dst, 0);
  667.         add_src_reg(ctx, instr, &tmp_src, 0);
  668.         add_src_reg(ctx, instr, src1, src1->SwizzleX);
  669.  
  670.         /* blob compiler seems to ensure there are at least 6 instructions
  671.          * between a "simple" (non-cat4) instruction and a dependent cat4..
  672.          * probably we need to handle this in some other places too.
  673.          */
  674.         ir3_instr_create(ctx->ir, 0, OPC_NOP)->repeat = 5;
  675.  
  676.         /* exp2 Rdst, Rtmp */
  677.         instr = ir3_instr_create(ctx->ir, 4, OPC_EXP2);
  678.         r = add_dst_reg(ctx, instr, &tmp_dst, 0);
  679.         add_src_reg(ctx, instr, &tmp_src, 0);
  680.         regmask_set(ctx->needs_ss, r);
  681.  
  682.         create_mov(ctx, dst, &tmp_src);
  683. }
  684.  
  685. /* texture fetch/sample instructions: */
  686. static void
  687. trans_samp(const struct instr_translater *t,
  688.                 struct fd3_compile_context *ctx,
  689.                 struct tgsi_full_instruction *inst)
  690. {
  691.         struct ir3_register *r;
  692.         struct ir3_instruction *instr;
  693.         struct tgsi_dst_register tmp_dst;
  694.         struct tgsi_src_register tmp_src;
  695.         struct tgsi_src_register *coord = &inst->Src[0].Register;
  696.         struct tgsi_src_register *samp  = &inst->Src[1].Register;
  697.         unsigned tex = inst->Texture.Texture;
  698.         int8_t *order;
  699.         unsigned i, j, flags = 0;
  700.  
  701.         switch (t->arg) {
  702.         case TGSI_OPCODE_TEX:
  703.                 order = (tex == TGSI_TEXTURE_2D) ?
  704.                                 (int8_t[4]){ 0,  1, -1, -1 } :  /* 2D */
  705.                                 (int8_t[4]){ 0,  1,  2, -1 };   /* 3D */
  706.                 break;
  707.         case TGSI_OPCODE_TXP:
  708.                 order = (tex == TGSI_TEXTURE_2D) ?
  709.                                 (int8_t[4]){ 0,  1,  3, -1 } :  /* 2D */
  710.                                 (int8_t[4]){ 0,  1,  2,  3 };   /* 3D */
  711.                 flags |= IR3_INSTR_P;
  712.                 break;
  713.         default:
  714.                 assert(0);
  715.                 break;
  716.         }
  717.  
  718.         if (tex == TGSI_TEXTURE_3D)
  719.                 flags |= IR3_INSTR_3D;
  720.  
  721.         /* The texture sample instructions need to coord in successive
  722.          * registers/components (ie. src.xy but not src.yx).  And TXP
  723.          * needs the .w component in .z for 2D..  so in some cases we
  724.          * might need to emit some mov instructions to shuffle things
  725.          * around:
  726.          */
  727.         for (i = 1; (i < 4) && (order[i] >= 0); i++) {
  728.                 if (src_swiz(coord, i) != (src_swiz(coord, 0) + order[i])) {
  729.                         type_t type_mov = get_type(ctx);
  730.  
  731.                         /* need to move things around: */
  732.                         get_internal_temp(ctx, &tmp_dst, &tmp_src);
  733.  
  734.                         for (j = 0; (j < 4) && (order[j] >= 0); j++) {
  735.                                 instr = ir3_instr_create(ctx->ir, 1, 0);
  736.                                 instr->cat1.src_type = type_mov;
  737.                                 instr->cat1.dst_type = type_mov;
  738.                                 add_dst_reg(ctx, instr, &tmp_dst, j);
  739.                                 add_src_reg(ctx, instr, coord,
  740.                                                 src_swiz(coord, order[j]));
  741.                         }
  742.  
  743.                         coord = &tmp_src;
  744.  
  745.                         if (j < 4)
  746.                                 ir3_instr_create(ctx->ir, 0, OPC_NOP)->repeat = 4 - j - 1;
  747.  
  748.                         break;
  749.                 }
  750.         }
  751.  
  752.         instr = ir3_instr_create(ctx->ir, 5, t->opc);
  753.         instr->cat5.type = get_type(ctx);
  754.         instr->cat5.samp = samp->Index;
  755.         instr->cat5.tex  = samp->Index;
  756.         instr->flags |= flags;
  757.  
  758.         r = add_dst_reg(ctx, instr, &inst->Dst[0].Register, 0);
  759.         r->wrmask = inst->Dst[0].Register.WriteMask;
  760.  
  761.         add_src_reg(ctx, instr, coord, coord->SwizzleX);
  762.  
  763.         regmask_set(ctx->needs_sy, r);
  764. }
  765.  
  766. /* CMP(a,b,c) = (a < 0) ? b : c */
  767. static void
  768. trans_cmp(const struct instr_translater *t,
  769.                 struct fd3_compile_context *ctx,
  770.                 struct tgsi_full_instruction *inst)
  771. {
  772.         struct ir3_instruction *instr;
  773.         struct tgsi_dst_register tmp_dst;
  774.         struct tgsi_src_register tmp_src;
  775.         struct tgsi_src_register constval;
  776.         /* final instruction uses original src1 and src2, so we need get_dst() */
  777.         struct tgsi_dst_register *dst = get_dst(ctx, inst);
  778.  
  779.         get_internal_temp(ctx, &tmp_dst, &tmp_src);
  780.  
  781.         /* cmps.f.ge tmp, src0, 0.0 */
  782.         instr = ir3_instr_create(ctx->ir, 2, OPC_CMPS_F);
  783.         instr->cat2.condition = IR3_COND_GE;
  784.         get_immediate(ctx, &constval, fui(0.0));
  785.         vectorize(ctx, instr, &tmp_dst, 2,
  786.                         &inst->Src[0].Register, 0,
  787.                         &constval, 0);
  788.  
  789.         /* add.s tmp, tmp, -1 */
  790.         instr = ir3_instr_create(ctx->ir, 2, OPC_ADD_S);
  791.         instr->repeat = 3;
  792.         add_dst_reg(ctx, instr, &tmp_dst, 0);
  793.         add_src_reg(ctx, instr, &tmp_src, 0);
  794.         ir3_reg_create(instr, 0, IR3_REG_IMMED)->iim_val = -1;
  795.  
  796.         /* sel.{f32,f16} dst, src2, tmp, src1 */
  797.         instr = ir3_instr_create(ctx->ir, 3, ctx->so->half_precision ?
  798.                         OPC_SEL_F16 : OPC_SEL_F32);
  799.         vectorize(ctx, instr, &inst->Dst[0].Register, 3,
  800.                         &inst->Src[2].Register, 0,
  801.                         &tmp_src, 0,
  802.                         &inst->Src[1].Register, 0);
  803.  
  804.         put_dst(ctx, inst, dst);
  805. }
  806.  
  807. /*
  808.  * Conditional / Flow control
  809.  */
  810.  
  811. static unsigned
  812. find_instruction(struct fd3_compile_context *ctx, struct ir3_instruction *instr)
  813. {
  814.         unsigned i;
  815.         for (i = 0; i < ctx->ir->instrs_count; i++)
  816.                 if (ctx->ir->instrs[i] == instr)
  817.                         return i;
  818.         return ~0;
  819. }
  820.  
  821. static void
  822. push_branch(struct fd3_compile_context *ctx, struct ir3_instruction *instr)
  823. {
  824.         ctx->branch[ctx->branch_count++] = instr;
  825. }
  826.  
  827. static void
  828. pop_branch(struct fd3_compile_context *ctx)
  829. {
  830.         struct ir3_instruction *instr;
  831.  
  832.         /* if we were clever enough, we'd patch this up after the fact,
  833.          * and set (jp) flag on whatever the next instruction was, rather
  834.          * than inserting an extra nop..
  835.          */
  836.         instr = ir3_instr_create(ctx->ir, 0, OPC_NOP);
  837.         instr->flags |= IR3_INSTR_JP;
  838.  
  839.         /* pop the branch instruction from the stack and fix up branch target: */
  840.         instr = ctx->branch[--ctx->branch_count];
  841.         instr->cat0.immed = ctx->ir->instrs_count - find_instruction(ctx, instr) - 1;
  842. }
  843.  
  844. /* We probably don't really want to translate if/else/endif into branches..
  845.  * the blob driver evaluates both legs of the if and then uses the sel
  846.  * instruction to pick which sides of the branch to "keep".. but figuring
  847.  * that out will take somewhat more compiler smarts.  So hopefully branches
  848.  * don't kill performance too badly.
  849.  */
  850. static void
  851. trans_if(const struct instr_translater *t,
  852.                 struct fd3_compile_context *ctx,
  853.                 struct tgsi_full_instruction *inst)
  854. {
  855.         struct ir3_instruction *instr;
  856.         struct tgsi_src_register *src = &inst->Src[0].Register;
  857.         struct tgsi_src_register constval;
  858.  
  859.         get_immediate(ctx, &constval, fui(0.0));
  860.  
  861.         instr = ir3_instr_create(ctx->ir, 2, OPC_CMPS_F);
  862.         ir3_reg_create(instr, regid(REG_P0, 0), 0);
  863.         add_src_reg(ctx, instr, &constval, constval.SwizzleX);
  864.         add_src_reg(ctx, instr, src, src->SwizzleX);
  865.         instr->cat2.condition = IR3_COND_EQ;
  866.  
  867.         instr = ir3_instr_create(ctx->ir, 0, OPC_BR);
  868.         push_branch(ctx, instr);
  869. }
  870.  
  871. static void
  872. trans_else(const struct instr_translater *t,
  873.                 struct fd3_compile_context *ctx,
  874.                 struct tgsi_full_instruction *inst)
  875. {
  876.         struct ir3_instruction *instr;
  877.  
  878.         /* for first half of if/else/endif, generate a jump past the else: */
  879.         instr = ir3_instr_create(ctx->ir, 0, OPC_JUMP);
  880.  
  881.         pop_branch(ctx);
  882.         push_branch(ctx, instr);
  883. }
  884.  
  885. static void
  886. trans_endif(const struct instr_translater *t,
  887.                 struct fd3_compile_context *ctx,
  888.                 struct tgsi_full_instruction *inst)
  889. {
  890.         pop_branch(ctx);
  891. }
  892.  
  893. /*
  894.  * Handlers for TGSI instructions which do have 1:1 mapping to native
  895.  * instructions:
  896.  */
  897.  
  898. static void
  899. instr_cat0(const struct instr_translater *t,
  900.                 struct fd3_compile_context *ctx,
  901.                 struct tgsi_full_instruction *inst)
  902. {
  903.         ir3_instr_create(ctx->ir, 0, t->opc);
  904. }
  905.  
  906. static void
  907. instr_cat1(const struct instr_translater *t,
  908.                 struct fd3_compile_context *ctx,
  909.                 struct tgsi_full_instruction *inst)
  910. {
  911.         struct tgsi_dst_register *dst = get_dst(ctx, inst);
  912.         struct tgsi_src_register *src = &inst->Src[0].Register;
  913.  
  914.         /* mov instructions can't handle a negate on src: */
  915.         if (src->Negate) {
  916.                 struct tgsi_src_register constval;
  917.                 struct ir3_instruction *instr;
  918.  
  919.                 /* since right now, we are using uniformly either TYPE_F16 or
  920.                  * TYPE_F32, and we don't utilize the conversion possibilities
  921.                  * of mov instructions, we can get away with substituting an
  922.                  * add.f which can handle negate.  Might need to revisit this
  923.                  * in the future if we start supporting widening/narrowing or
  924.                  * conversion to/from integer..
  925.                  */
  926.                 instr = ir3_instr_create(ctx->ir, 2, OPC_ADD_F);
  927.                 get_immediate(ctx, &constval, fui(0.0));
  928.                 vectorize(ctx, instr, dst, 2, src, 0, &constval, 0);
  929.         } else {
  930.                 create_mov(ctx, dst, src);
  931.                 /* create_mov() generates vector sequence, so no vectorize() */
  932.         }
  933.         put_dst(ctx, inst, dst);
  934. }
  935.  
  936. static void
  937. instr_cat2(const struct instr_translater *t,
  938.                 struct fd3_compile_context *ctx,
  939.                 struct tgsi_full_instruction *inst)
  940. {
  941.         struct tgsi_dst_register *dst = get_dst(ctx, inst);
  942.         struct ir3_instruction *instr;
  943.         unsigned src0_flags = 0;
  944.  
  945.         instr = ir3_instr_create(ctx->ir, 2, t->opc);
  946.  
  947.         switch (t->tgsi_opc) {
  948.         case TGSI_OPCODE_SLT:
  949.         case TGSI_OPCODE_SGE:
  950.                 instr->cat2.condition = t->arg;
  951.                 break;
  952.         case TGSI_OPCODE_ABS:
  953.                 src0_flags = IR3_REG_ABS;
  954.                 break;
  955.         }
  956.  
  957.         switch (t->opc) {
  958.         case OPC_ABSNEG_F:
  959.         case OPC_ABSNEG_S:
  960.         case OPC_CLZ_B:
  961.         case OPC_CLZ_S:
  962.         case OPC_SIGN_F:
  963.         case OPC_FLOOR_F:
  964.         case OPC_CEIL_F:
  965.         case OPC_RNDNE_F:
  966.         case OPC_RNDAZ_F:
  967.         case OPC_TRUNC_F:
  968.         case OPC_NOT_B:
  969.         case OPC_BFREV_B:
  970.         case OPC_SETRM:
  971.         case OPC_CBITS_B:
  972.                 /* these only have one src reg */
  973.                 vectorize(ctx, instr, dst, 1,
  974.                                 &inst->Src[0].Register, src0_flags);
  975.                 break;
  976.         default:
  977.                 vectorize(ctx, instr, dst, 2,
  978.                                 &inst->Src[0].Register, src0_flags,
  979.                                 &inst->Src[1].Register, 0);
  980.                 break;
  981.         }
  982.  
  983.         put_dst(ctx, inst, dst);
  984. }
  985.  
  986. static void
  987. instr_cat3(const struct instr_translater *t,
  988.                 struct fd3_compile_context *ctx,
  989.                 struct tgsi_full_instruction *inst)
  990. {
  991.         struct tgsi_dst_register *dst = get_dst(ctx, inst);
  992.         struct tgsi_src_register *src1 = &inst->Src[1].Register;
  993.         struct tgsi_dst_register tmp_dst;
  994.         struct tgsi_src_register tmp_src;
  995.         struct ir3_instruction *instr;
  996.  
  997.         /* Blob compiler never seems to use a const in src1 position..
  998.          * although there does seem (according to disassembler hidden
  999.          * in libllvm-a3xx.so) to be a bit to indicate that src1 is a
  1000.          * const.  Not sure if this is a hw bug, or simply that the
  1001.          * disassembler lies.
  1002.          */
  1003.         if ((src1->File == TGSI_FILE_CONSTANT) ||
  1004.                         (src1->File == TGSI_FILE_IMMEDIATE)) {
  1005.                 get_internal_temp(ctx, &tmp_dst, &tmp_src);
  1006.                 create_mov(ctx, &tmp_dst, src1);
  1007.                 src1 = &tmp_src;
  1008.         }
  1009.  
  1010.         instr = ir3_instr_create(ctx->ir, 3,
  1011.                         ctx->so->half_precision ? t->hopc : t->opc);
  1012.         vectorize(ctx, instr, dst, 3,
  1013.                         &inst->Src[0].Register, 0,
  1014.                         src1, 0,
  1015.                         &inst->Src[2].Register, 0);
  1016.         put_dst(ctx, inst, dst);
  1017. }
  1018.  
  1019. static void
  1020. instr_cat4(const struct instr_translater *t,
  1021.                 struct fd3_compile_context *ctx,
  1022.                 struct tgsi_full_instruction *inst)
  1023. {
  1024.         struct tgsi_dst_register *dst = get_dst(ctx, inst);
  1025.         struct ir3_instruction *instr;
  1026.  
  1027.         ir3_instr_create(ctx->ir, 0, OPC_NOP)->repeat = 5;
  1028.         instr = ir3_instr_create(ctx->ir, 4, t->opc);
  1029.  
  1030.         vectorize(ctx, instr, dst, 1,
  1031.                         &inst->Src[0].Register, 0);
  1032.  
  1033.         regmask_set(ctx->needs_ss, instr->regs[0]);
  1034.  
  1035.         put_dst(ctx, inst, dst);
  1036. }
  1037.  
  1038. static const struct instr_translater translaters[TGSI_OPCODE_LAST] = {
  1039. #define INSTR(n, f, ...) \
  1040.         [TGSI_OPCODE_ ## n] = { .fxn = (f), .tgsi_opc = TGSI_OPCODE_ ## n, ##__VA_ARGS__ }
  1041.  
  1042.         INSTR(MOV,          instr_cat1),
  1043.         INSTR(RCP,          instr_cat4, .opc = OPC_RCP),
  1044.         INSTR(RSQ,          instr_cat4, .opc = OPC_RSQ),
  1045.         INSTR(SQRT,         instr_cat4, .opc = OPC_SQRT),
  1046.         INSTR(MUL,          instr_cat2, .opc = OPC_MUL_F),
  1047.         INSTR(ADD,          instr_cat2, .opc = OPC_ADD_F),
  1048.         INSTR(DP2,          trans_dotp, .arg = 2),
  1049.         INSTR(DP3,          trans_dotp, .arg = 3),
  1050.         INSTR(DP4,          trans_dotp, .arg = 4),
  1051.         INSTR(DPH,          trans_dotp, .arg = 3),   /* almost like DP3 */
  1052.         INSTR(MIN,          instr_cat2, .opc = OPC_MIN_F),
  1053.         INSTR(MAX,          instr_cat2, .opc = OPC_MAX_F),
  1054.         INSTR(SLT,          instr_cat2, .opc = OPC_CMPS_F, .arg = IR3_COND_LT),
  1055.         INSTR(SGE,          instr_cat2, .opc = OPC_CMPS_F, .arg = IR3_COND_GE),
  1056.         INSTR(MAD,          instr_cat3, .opc = OPC_MAD_F32, .hopc = OPC_MAD_F16),
  1057.         INSTR(LRP,          trans_lrp),
  1058.         INSTR(FRC,          trans_frac),
  1059.         INSTR(FLR,          instr_cat2, .opc = OPC_FLOOR_F),
  1060.         INSTR(EX2,          instr_cat4, .opc = OPC_EXP2),
  1061.         INSTR(LG2,          instr_cat4, .opc = OPC_LOG2),
  1062.         INSTR(POW,          trans_pow),
  1063.         INSTR(ABS,          instr_cat2, .opc = OPC_ABSNEG_F),
  1064.         INSTR(COS,          instr_cat4, .opc = OPC_SIN),
  1065.         INSTR(SIN,          instr_cat4, .opc = OPC_COS),
  1066.         INSTR(TEX,          trans_samp, .opc = OPC_SAM, .arg = TGSI_OPCODE_TEX),
  1067.         INSTR(TXP,          trans_samp, .opc = OPC_SAM, .arg = TGSI_OPCODE_TXP),
  1068.         INSTR(CMP,          trans_cmp),
  1069.         INSTR(IF,           trans_if),
  1070.         INSTR(ELSE,         trans_else),
  1071.         INSTR(ENDIF,        trans_endif),
  1072.         INSTR(END,          instr_cat0, .opc = OPC_END),
  1073. };
  1074.  
  1075. static int
  1076. decl_in(struct fd3_compile_context *ctx, struct tgsi_full_declaration *decl)
  1077. {
  1078.         struct fd3_shader_stateobj *so = ctx->so;
  1079.         unsigned base = ctx->base_reg[TGSI_FILE_INPUT];
  1080.         unsigned i, flags = 0;
  1081.         int nop = 0;
  1082.  
  1083.         if (ctx->so->half_precision)
  1084.                 flags |= IR3_REG_HALF;
  1085.  
  1086.         for (i = decl->Range.First; i <= decl->Range.Last; i++) {
  1087.                 unsigned n = so->inputs_count++;
  1088.                 unsigned r = regid(i + base, 0);
  1089.                 unsigned ncomp;
  1090.  
  1091.                 /* TODO use ctx->info.input_usage_mask[decl->Range.n] to figure out ncomp: */
  1092.                 ncomp = 4;
  1093.  
  1094.                 DBG("decl in -> r%d", i + base);   // XXX
  1095.  
  1096.                 so->inputs[n].compmask = (1 << ncomp) - 1;
  1097.                 so->inputs[n].regid = r;
  1098.                 so->inputs[n].inloc = ctx->next_inloc;
  1099.                 ctx->next_inloc += ncomp;
  1100.  
  1101.                 so->total_in += ncomp;
  1102.  
  1103.                 /* for frag shaders, we need to generate the corresponding bary instr: */
  1104.                 if (ctx->type == TGSI_PROCESSOR_FRAGMENT) {
  1105.                         struct ir3_instruction *instr;
  1106.  
  1107.                         instr = ir3_instr_create(ctx->ir, 2, OPC_BARY_F);
  1108.                         instr->repeat = ncomp - 1;
  1109.  
  1110.                         /* dst register: */
  1111.                         ctx->last_input = ir3_reg_create(instr, r, flags);
  1112.  
  1113.                         /* input position: */
  1114.                         ir3_reg_create(instr, 0, IR3_REG_IMMED | IR3_REG_R)->iim_val =
  1115.                                         so->inputs[n].inloc - 8;
  1116.  
  1117.                         /* input base (always r0.x): */
  1118.                         ir3_reg_create(instr, regid(0,0), 0);
  1119.  
  1120.                         nop = 6;
  1121.                 }
  1122.         }
  1123.  
  1124.         return nop;
  1125. }
  1126.  
  1127. static void
  1128. decl_out(struct fd3_compile_context *ctx, struct tgsi_full_declaration *decl)
  1129. {
  1130.         struct fd3_shader_stateobj *so = ctx->so;
  1131.         unsigned base = ctx->base_reg[TGSI_FILE_OUTPUT];
  1132.         unsigned name = decl->Semantic.Name;
  1133.         unsigned i;
  1134.  
  1135.         assert(decl->Declaration.Semantic);  // TODO is this ever not true?
  1136.  
  1137.         DBG("decl out[%d] -> r%d", name, decl->Range.First + base);   // XXX
  1138.  
  1139.         if (ctx->type == TGSI_PROCESSOR_VERTEX) {
  1140.                 switch (name) {
  1141.                 case TGSI_SEMANTIC_POSITION:
  1142.                         so->pos_regid = regid(decl->Range.First + base, 0);
  1143.                         break;
  1144.                 case TGSI_SEMANTIC_PSIZE:
  1145.                         so->psize_regid = regid(decl->Range.First + base, 0);
  1146.                         break;
  1147.                 case TGSI_SEMANTIC_COLOR:
  1148.                 case TGSI_SEMANTIC_GENERIC:
  1149.                 case TGSI_SEMANTIC_FOG:
  1150.                 case TGSI_SEMANTIC_TEXCOORD:
  1151.                         for (i = decl->Range.First; i <= decl->Range.Last; i++)
  1152.                                 so->outputs[so->outputs_count++].regid = regid(i + base, 0);
  1153.                         break;
  1154.                 default:
  1155.                         DBG("unknown VS semantic name: %s",
  1156.                                         tgsi_semantic_names[name]);
  1157.                         assert(0);
  1158.                 }
  1159.         } else {
  1160.                 switch (name) {
  1161.                 case TGSI_SEMANTIC_COLOR:
  1162.                         so->color_regid = regid(decl->Range.First + base, 0);
  1163.                         break;
  1164.                 default:
  1165.                         DBG("unknown VS semantic name: %s",
  1166.                                         tgsi_semantic_names[name]);
  1167.                         assert(0);
  1168.                 }
  1169.         }
  1170. }
  1171.  
  1172. static void
  1173. decl_samp(struct fd3_compile_context *ctx, struct tgsi_full_declaration *decl)
  1174. {
  1175.         ctx->so->samplers_count++;
  1176. }
  1177.  
  1178. static void
  1179. compile_instructions(struct fd3_compile_context *ctx)
  1180. {
  1181.         struct ir3_shader *ir = ctx->ir;
  1182.         int nop = 0;
  1183.  
  1184.         while (!tgsi_parse_end_of_tokens(&ctx->parser)) {
  1185.                 tgsi_parse_token(&ctx->parser);
  1186.  
  1187.                 switch (ctx->parser.FullToken.Token.Type) {
  1188.                 case TGSI_TOKEN_TYPE_DECLARATION: {
  1189.                         struct tgsi_full_declaration *decl =
  1190.                                         &ctx->parser.FullToken.FullDeclaration;
  1191.                         if (decl->Declaration.File == TGSI_FILE_OUTPUT) {
  1192.                                 decl_out(ctx, decl);
  1193.                         } else if (decl->Declaration.File == TGSI_FILE_INPUT) {
  1194.                                 nop = decl_in(ctx, decl);
  1195.                         } else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
  1196.                                 decl_samp(ctx, decl);
  1197.                         }
  1198.                         break;
  1199.                 }
  1200.                 case TGSI_TOKEN_TYPE_IMMEDIATE: {
  1201.                         /* TODO: if we know the immediate is small enough, and only
  1202.                          * used with instructions that can embed an immediate, we
  1203.                          * can skip this:
  1204.                          */
  1205.                         struct tgsi_full_immediate *imm =
  1206.                                         &ctx->parser.FullToken.FullImmediate;
  1207.                         unsigned n = ctx->so->immediates_count++;
  1208.                         memcpy(ctx->so->immediates[n].val, imm->u, 16);
  1209.                         break;
  1210.                 }
  1211.                 case TGSI_TOKEN_TYPE_INSTRUCTION: {
  1212.                         struct tgsi_full_instruction *inst =
  1213.                                         &ctx->parser.FullToken.FullInstruction;
  1214.                         unsigned opc = inst->Instruction.Opcode;
  1215.                         const struct instr_translater *t = &translaters[opc];
  1216.  
  1217.                         if (nop) {
  1218.                                 ir3_instr_create(ctx->ir, 0, OPC_NOP)->repeat = nop - 1;
  1219.                                 nop = 0;
  1220.                         }
  1221.  
  1222.                         if (t->fxn) {
  1223.                                 t->fxn(t, ctx, inst);
  1224.                                 ctx->num_internal_temps = 0;
  1225.                         } else {
  1226.                                 debug_printf("unknown TGSI opc: %s\n",
  1227.                                                 tgsi_get_opcode_name(opc));
  1228.                                 tgsi_dump(ctx->tokens, 0);
  1229.                                 assert(0);
  1230.                         }
  1231.  
  1232.                         break;
  1233.                 }
  1234.                 default:
  1235.                         break;
  1236.                 }
  1237.         }
  1238.  
  1239.         if (ir->instrs_count > 0)
  1240.                 ir->instrs[0]->flags |= IR3_INSTR_SS | IR3_INSTR_SY;
  1241.  
  1242.         if (ctx->last_input)
  1243.                 ctx->last_input->flags |= IR3_REG_EI;
  1244. }
  1245.  
  1246. int
  1247. fd3_compile_shader(struct fd3_shader_stateobj *so,
  1248.                 const struct tgsi_token *tokens)
  1249. {
  1250.         struct fd3_compile_context ctx;
  1251.  
  1252.         assert(!so->ir);
  1253.  
  1254.         so->ir = ir3_shader_create();
  1255.  
  1256.         so->color_regid = regid(63,0);
  1257.         so->pos_regid   = regid(63,0);
  1258.         so->psize_regid = regid(63,0);
  1259.  
  1260.         if (compile_init(&ctx, so, tokens) != TGSI_PARSE_OK)
  1261.                 return -1;
  1262.  
  1263.         compile_instructions(&ctx);
  1264.  
  1265.         compile_free(&ctx);
  1266.  
  1267.         return 0;
  1268. }
  1269.