Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2012-2013 LunarG, Inc.
  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 shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22.  * DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors:
  25.  *    Chia-I Wu <olv@lunarg.com>
  26.  */
  27.  
  28. #include "tgsi/tgsi_dump.h"
  29. #include "tgsi/tgsi_util.h"
  30. #include "toy_compiler.h"
  31. #include "toy_tgsi.h"
  32. #include "toy_legalize.h"
  33. #include "toy_optimize.h"
  34. #include "toy_helpers.h"
  35. #include "ilo_context.h"
  36. #include "ilo_shader_internal.h"
  37.  
  38. struct vs_compile_context {
  39.    struct ilo_shader *shader;
  40.    const struct ilo_shader_variant *variant;
  41.  
  42.    struct toy_compiler tc;
  43.    struct toy_tgsi tgsi;
  44.    enum brw_message_target const_cache;
  45.  
  46.    int output_map[PIPE_MAX_SHADER_OUTPUTS];
  47.  
  48.    int num_grf_per_vrf;
  49.    int first_const_grf;
  50.    int first_vue_grf;
  51.    int first_free_grf;
  52.    int last_free_grf;
  53.  
  54.    int first_free_mrf;
  55.    int last_free_mrf;
  56. };
  57.  
  58. static void
  59. vs_lower_opcode_tgsi_in(struct vs_compile_context *vcc,
  60.                         struct toy_dst dst, int dim, int idx)
  61. {
  62.    struct toy_compiler *tc = &vcc->tc;
  63.    int slot;
  64.  
  65.    assert(!dim);
  66.  
  67.    slot = toy_tgsi_find_input(&vcc->tgsi, idx);
  68.    if (slot >= 0) {
  69.       const int first_in_grf = vcc->first_vue_grf +
  70.          (vcc->shader->in.count - vcc->tgsi.num_inputs);
  71.       const int grf = first_in_grf + vcc->tgsi.inputs[slot].semantic_index;
  72.       const struct toy_src src = tsrc(TOY_FILE_GRF, grf, 0);
  73.  
  74.       tc_MOV(tc, dst, src);
  75.    }
  76.    else {
  77.       /* undeclared input */
  78.       tc_MOV(tc, dst, tsrc_imm_f(0.0f));
  79.    }
  80. }
  81.  
  82. static void
  83. vs_lower_opcode_tgsi_const_gen6(struct vs_compile_context *vcc,
  84.                                 struct toy_dst dst, int dim,
  85.                                 struct toy_src idx)
  86. {
  87.    const struct toy_dst header =
  88.       tdst_ud(tdst(TOY_FILE_MRF, vcc->first_free_mrf, 0));
  89.    const struct toy_dst block_offsets =
  90.       tdst_ud(tdst(TOY_FILE_MRF, vcc->first_free_mrf + 1, 0));
  91.    const struct toy_src r0 = tsrc_ud(tsrc(TOY_FILE_GRF, 0, 0));
  92.    struct toy_compiler *tc = &vcc->tc;
  93.    unsigned msg_type, msg_ctrl, msg_len;
  94.    struct toy_inst *inst;
  95.    struct toy_src desc;
  96.  
  97.    /* set message header */
  98.    inst = tc_MOV(tc, header, r0);
  99.    inst->mask_ctrl = BRW_MASK_DISABLE;
  100.  
  101.    /* set block offsets */
  102.    tc_MOV(tc, block_offsets, idx);
  103.  
  104.    msg_type = GEN6_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
  105.    msg_ctrl = BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD << 8;;
  106.    msg_len = 2;
  107.  
  108.    desc = tsrc_imm_mdesc_data_port(tc, false, msg_len, 1, true, false,
  109.          msg_type, msg_ctrl, ILO_VS_CONST_SURFACE(dim));
  110.  
  111.    tc_SEND(tc, dst, tsrc_from(header), desc, vcc->const_cache);
  112. }
  113.  
  114. static void
  115. vs_lower_opcode_tgsi_const_gen7(struct vs_compile_context *vcc,
  116.                                 struct toy_dst dst, int dim,
  117.                                 struct toy_src idx)
  118. {
  119.    struct toy_compiler *tc = &vcc->tc;
  120.    const struct toy_dst offset =
  121.       tdst_ud(tdst(TOY_FILE_MRF, vcc->first_free_mrf, 0));
  122.    struct toy_src desc;
  123.  
  124.    /*
  125.     * In 259b65e2e7938de4aab323033cfe2b33369ddb07, pull constant load was
  126.     * changed from OWord Dual Block Read to ld to increase performance in the
  127.     * classic driver.  Since we use the constant cache instead of the data
  128.     * cache, I wonder if we still want to follow the classic driver.
  129.     */
  130.  
  131.    /* set offset */
  132.    tc_MOV(tc, offset, idx);
  133.  
  134.    desc = tsrc_imm_mdesc_sampler(tc, 1, 1, false,
  135.          BRW_SAMPLER_SIMD_MODE_SIMD4X2,
  136.          GEN5_SAMPLER_MESSAGE_SAMPLE_LD,
  137.          0,
  138.          ILO_VS_CONST_SURFACE(dim));
  139.  
  140.    tc_SEND(tc, dst, tsrc_from(offset), desc, BRW_SFID_SAMPLER);
  141. }
  142.  
  143. static void
  144. vs_lower_opcode_tgsi_imm(struct vs_compile_context *vcc,
  145.                          struct toy_dst dst, int idx)
  146. {
  147.    const uint32_t *imm;
  148.    int ch;
  149.  
  150.    imm = toy_tgsi_get_imm(&vcc->tgsi, idx, NULL);
  151.  
  152.    for (ch = 0; ch < 4; ch++) {
  153.       /* raw moves */
  154.       tc_MOV(&vcc->tc,
  155.             tdst_writemask(tdst_ud(dst), 1 << ch),
  156.             tsrc_imm_ud(imm[ch]));
  157.    }
  158. }
  159.  
  160.  
  161. static void
  162. vs_lower_opcode_tgsi_sv(struct vs_compile_context *vcc,
  163.                         struct toy_dst dst, int dim, int idx)
  164. {
  165.    struct toy_compiler *tc = &vcc->tc;
  166.    const struct toy_tgsi *tgsi = &vcc->tgsi;
  167.    int slot;
  168.  
  169.    assert(!dim);
  170.  
  171.    slot = toy_tgsi_find_system_value(tgsi, idx);
  172.    if (slot < 0)
  173.       return;
  174.  
  175.    switch (tgsi->system_values[slot].semantic_name) {
  176.    case TGSI_SEMANTIC_INSTANCEID:
  177.    case TGSI_SEMANTIC_VERTEXID:
  178.       /*
  179.        * In 3DSTATE_VERTEX_ELEMENTS, we prepend an extra vertex element for
  180.        * the generated IDs, with VID in the X channel and IID in the Y
  181.        * channel.
  182.        */
  183.       {
  184.          const int grf = vcc->first_vue_grf;
  185.          const struct toy_src src = tsrc(TOY_FILE_GRF, grf, 0);
  186.          const enum toy_swizzle swizzle =
  187.             (tgsi->system_values[slot].semantic_name ==
  188.              TGSI_SEMANTIC_INSTANCEID) ? TOY_SWIZZLE_Y : TOY_SWIZZLE_X;
  189.  
  190.          tc_MOV(tc, tdst_d(dst), tsrc_d(tsrc_swizzle1(src, swizzle)));
  191.       }
  192.       break;
  193.    case TGSI_SEMANTIC_PRIMID:
  194.    default:
  195.       tc_fail(tc, "unhandled system value");
  196.       tc_MOV(tc, dst, tsrc_imm_d(0));
  197.       break;
  198.    }
  199. }
  200.  
  201. static void
  202. vs_lower_opcode_tgsi_direct(struct vs_compile_context *vcc,
  203.                             struct toy_inst *inst)
  204. {
  205.    struct toy_compiler *tc = &vcc->tc;
  206.    int dim, idx;
  207.  
  208.    assert(inst->src[0].file == TOY_FILE_IMM);
  209.    dim = inst->src[0].val32;
  210.  
  211.    assert(inst->src[1].file == TOY_FILE_IMM);
  212.    idx = inst->src[1].val32;
  213.  
  214.    switch (inst->opcode) {
  215.    case TOY_OPCODE_TGSI_IN:
  216.       vs_lower_opcode_tgsi_in(vcc, inst->dst, dim, idx);
  217.       break;
  218.    case TOY_OPCODE_TGSI_CONST:
  219.       if (tc->dev->gen >= ILO_GEN(7))
  220.          vs_lower_opcode_tgsi_const_gen7(vcc, inst->dst, dim, inst->src[1]);
  221.       else
  222.          vs_lower_opcode_tgsi_const_gen6(vcc, inst->dst, dim, inst->src[1]);
  223.       break;
  224.    case TOY_OPCODE_TGSI_SV:
  225.       vs_lower_opcode_tgsi_sv(vcc, inst->dst, dim, idx);
  226.       break;
  227.    case TOY_OPCODE_TGSI_IMM:
  228.       assert(!dim);
  229.       vs_lower_opcode_tgsi_imm(vcc, inst->dst, idx);
  230.       break;
  231.    default:
  232.       tc_fail(tc, "unhandled TGSI fetch");
  233.       break;
  234.    }
  235.  
  236.    tc_discard_inst(tc, inst);
  237. }
  238.  
  239. static void
  240. vs_lower_opcode_tgsi_indirect(struct vs_compile_context *vcc,
  241.                               struct toy_inst *inst)
  242. {
  243.    struct toy_compiler *tc = &vcc->tc;
  244.    enum tgsi_file_type file;
  245.    int dim, idx;
  246.    struct toy_src indirect_dim, indirect_idx;
  247.  
  248.    assert(inst->src[0].file == TOY_FILE_IMM);
  249.    file = inst->src[0].val32;
  250.  
  251.    assert(inst->src[1].file == TOY_FILE_IMM);
  252.    dim = inst->src[1].val32;
  253.    indirect_dim = inst->src[2];
  254.  
  255.    assert(inst->src[3].file == TOY_FILE_IMM);
  256.    idx = inst->src[3].val32;
  257.    indirect_idx = inst->src[4];
  258.  
  259.    /* no dimension indirection */
  260.    assert(indirect_dim.file == TOY_FILE_IMM);
  261.    dim += indirect_dim.val32;
  262.  
  263.    switch (inst->opcode) {
  264.    case TOY_OPCODE_TGSI_INDIRECT_FETCH:
  265.       if (file == TGSI_FILE_CONSTANT) {
  266.          if (idx) {
  267.             struct toy_dst tmp = tc_alloc_tmp(tc);
  268.  
  269.             tc_ADD(tc, tmp, indirect_idx, tsrc_imm_d(idx));
  270.             indirect_idx = tsrc_from(tmp);
  271.          }
  272.  
  273.          if (tc->dev->gen >= ILO_GEN(7))
  274.             vs_lower_opcode_tgsi_const_gen7(vcc, inst->dst, dim, indirect_idx);
  275.          else
  276.             vs_lower_opcode_tgsi_const_gen6(vcc, inst->dst, dim, indirect_idx);
  277.          break;
  278.       }
  279.       /* fall through */
  280.    case TOY_OPCODE_TGSI_INDIRECT_STORE:
  281.    default:
  282.       tc_fail(tc, "unhandled TGSI indirection");
  283.       break;
  284.    }
  285.  
  286.    tc_discard_inst(tc, inst);
  287. }
  288.  
  289. /**
  290.  * Emit instructions to move sampling parameters to the message registers.
  291.  */
  292. static int
  293. vs_add_sampler_params(struct toy_compiler *tc, int msg_type, int base_mrf,
  294.                       struct toy_src coords, int num_coords,
  295.                       struct toy_src bias_or_lod, struct toy_src ref_or_si,
  296.                       struct toy_src ddx, struct toy_src ddy, int num_derivs)
  297. {
  298.    const unsigned coords_writemask = (1 << num_coords) - 1;
  299.    struct toy_dst m[3];
  300.    int num_params, i;
  301.  
  302.    assert(num_coords <= 4);
  303.    assert(num_derivs <= 3 && num_derivs <= num_coords);
  304.  
  305.    for (i = 0; i < Elements(m); i++)
  306.       m[i] = tdst(TOY_FILE_MRF, base_mrf + i, 0);
  307.  
  308.    switch (msg_type) {
  309.    case GEN5_SAMPLER_MESSAGE_SAMPLE_LOD:
  310.       tc_MOV(tc, tdst_writemask(m[0], coords_writemask), coords);
  311.       tc_MOV(tc, tdst_writemask(m[1], TOY_WRITEMASK_X), bias_or_lod);
  312.       num_params = 5;
  313.       break;
  314.    case GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS:
  315.       tc_MOV(tc, tdst_writemask(m[0], coords_writemask), coords);
  316.       tc_MOV(tc, tdst_writemask(m[1], TOY_WRITEMASK_XZ),
  317.             tsrc_swizzle(ddx, 0, 0, 1, 1));
  318.       tc_MOV(tc, tdst_writemask(m[1], TOY_WRITEMASK_YW),
  319.             tsrc_swizzle(ddy, 0, 0, 1, 1));
  320.       if (num_derivs > 2) {
  321.          tc_MOV(tc, tdst_writemask(m[2], TOY_WRITEMASK_X),
  322.                tsrc_swizzle1(ddx, 2));
  323.          tc_MOV(tc, tdst_writemask(m[2], TOY_WRITEMASK_Y),
  324.                tsrc_swizzle1(ddy, 2));
  325.       }
  326.       num_params = 4 + num_derivs * 2;
  327.       break;
  328.    case GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE:
  329.       tc_MOV(tc, tdst_writemask(m[0], coords_writemask), coords);
  330.       tc_MOV(tc, tdst_writemask(m[1], TOY_WRITEMASK_X), ref_or_si);
  331.       tc_MOV(tc, tdst_writemask(m[1], TOY_WRITEMASK_Y), bias_or_lod);
  332.       num_params = 6;
  333.       break;
  334.    case GEN5_SAMPLER_MESSAGE_SAMPLE_LD:
  335.       assert(num_coords <= 3);
  336.       tc_MOV(tc, tdst_writemask(tdst_d(m[0]), coords_writemask), coords);
  337.       tc_MOV(tc, tdst_writemask(tdst_d(m[0]), TOY_WRITEMASK_W), bias_or_lod);
  338.       if (tc->dev->gen >= ILO_GEN(7)) {
  339.          num_params = 4;
  340.       }
  341.       else {
  342.          tc_MOV(tc, tdst_writemask(tdst_d(m[1]), TOY_WRITEMASK_X), ref_or_si);
  343.          num_params = 5;
  344.       }
  345.       break;
  346.    case GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO:
  347.       tc_MOV(tc, tdst_writemask(tdst_d(m[0]), TOY_WRITEMASK_X), bias_or_lod);
  348.       num_params = 1;
  349.       break;
  350.    default:
  351.       tc_fail(tc, "unknown sampler opcode");
  352.       num_params = 0;
  353.       break;
  354.    }
  355.  
  356.    return (num_params + 3) / 4;
  357. }
  358.  
  359. /**
  360.  * Set up message registers and return the message descriptor for sampling.
  361.  */
  362. static struct toy_src
  363. vs_prepare_tgsi_sampling(struct toy_compiler *tc, const struct toy_inst *inst,
  364.                          int base_mrf, unsigned *ret_sampler_index)
  365. {
  366.    unsigned simd_mode, msg_type, msg_len, sampler_index, binding_table_index;
  367.    struct toy_src coords, ddx, ddy, bias_or_lod, ref_or_si;
  368.    int num_coords, ref_pos, num_derivs;
  369.    int sampler_src;
  370.  
  371.    simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD4X2;
  372.  
  373.    coords = inst->src[0];
  374.    ddx = tsrc_null();
  375.    ddy = tsrc_null();
  376.    bias_or_lod = tsrc_null();
  377.    ref_or_si = tsrc_null();
  378.    num_derivs = 0;
  379.    sampler_src = 1;
  380.  
  381.    num_coords = tgsi_util_get_texture_coord_dim(inst->tex.target, &ref_pos);
  382.  
  383.    /* extract the parameters */
  384.    switch (inst->opcode) {
  385.    case TOY_OPCODE_TGSI_TXD:
  386.       if (ref_pos >= 0)
  387.          tc_fail(tc, "TXD with shadow sampler not supported");
  388.  
  389.       msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS;
  390.       ddx = inst->src[1];
  391.       ddy = inst->src[2];
  392.       num_derivs = num_coords;
  393.       sampler_src = 3;
  394.       break;
  395.    case TOY_OPCODE_TGSI_TXL:
  396.       if (ref_pos >= 0) {
  397.          assert(ref_pos < 3);
  398.  
  399.          msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;
  400.          ref_or_si = tsrc_swizzle1(coords, ref_pos);
  401.       }
  402.       else {
  403.          msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
  404.       }
  405.  
  406.       bias_or_lod = tsrc_swizzle1(coords, TOY_SWIZZLE_W);
  407.       break;
  408.    case TOY_OPCODE_TGSI_TXF:
  409.       msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LD;
  410.  
  411.       switch (inst->tex.target) {
  412.       case TGSI_TEXTURE_2D_MSAA:
  413.       case TGSI_TEXTURE_2D_ARRAY_MSAA:
  414.          assert(ref_pos >= 0 && ref_pos < 4);
  415.          /* lod is always 0 */
  416.          bias_or_lod = tsrc_imm_d(0);
  417.          ref_or_si = tsrc_swizzle1(coords, ref_pos);
  418.          break;
  419.       default:
  420.          bias_or_lod = tsrc_swizzle1(coords, TOY_SWIZZLE_W);
  421.          break;
  422.       }
  423.  
  424.       /* offset the coordinates */
  425.       if (!tsrc_is_null(inst->tex.offsets[0])) {
  426.          struct toy_dst tmp;
  427.  
  428.          tmp = tc_alloc_tmp(tc);
  429.          tc_ADD(tc, tmp, coords, inst->tex.offsets[0]);
  430.          coords = tsrc_from(tmp);
  431.       }
  432.  
  433.       sampler_src = 1;
  434.       break;
  435.    case TOY_OPCODE_TGSI_TXQ:
  436.       msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
  437.       num_coords = 0;
  438.       bias_or_lod = tsrc_swizzle1(coords, TOY_SWIZZLE_X);
  439.       break;
  440.    case TOY_OPCODE_TGSI_TXQ_LZ:
  441.       msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO;
  442.       num_coords = 0;
  443.       sampler_src = 0;
  444.       break;
  445.    case TOY_OPCODE_TGSI_TXL2:
  446.       if (ref_pos >= 0) {
  447.          assert(ref_pos < 4);
  448.  
  449.          msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;
  450.          ref_or_si = tsrc_swizzle1(coords, ref_pos);
  451.       }
  452.       else {
  453.          msg_type = GEN5_SAMPLER_MESSAGE_SAMPLE_LOD;
  454.       }
  455.  
  456.       bias_or_lod = tsrc_swizzle1(inst->src[1], TOY_SWIZZLE_X);
  457.       sampler_src = 2;
  458.       break;
  459.    default:
  460.       assert(!"unhandled sampling opcode");
  461.       if (ret_sampler_index)
  462.          *ret_sampler_index = 0;
  463.       return tsrc_null();
  464.       break;
  465.    }
  466.  
  467.    assert(inst->src[sampler_src].file == TOY_FILE_IMM);
  468.    sampler_index = inst->src[sampler_src].val32;
  469.    binding_table_index = ILO_VS_TEXTURE_SURFACE(sampler_index);
  470.  
  471.    /*
  472.     * From the Sandy Bridge PRM, volume 4 part 1, page 18:
  473.     *
  474.     *     "Note that the (cube map) coordinates delivered to the sampling
  475.     *      engine must already have been divided by the component with the
  476.     *      largest absolute value."
  477.     */
  478.    switch (inst->tex.target) {
  479.    case TGSI_TEXTURE_CUBE:
  480.    case TGSI_TEXTURE_SHADOWCUBE:
  481.    case TGSI_TEXTURE_CUBE_ARRAY:
  482.    case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
  483.       /* TXQ does not need coordinates */
  484.       if (num_coords >= 3) {
  485.          struct toy_dst tmp, max;
  486.          struct toy_src abs_coords[3];
  487.          int i;
  488.  
  489.          tmp = tc_alloc_tmp(tc);
  490.          max = tdst_writemask(tmp, TOY_WRITEMASK_W);
  491.  
  492.          for (i = 0; i < 3; i++)
  493.             abs_coords[i] = tsrc_absolute(tsrc_swizzle1(coords, i));
  494.  
  495.          tc_SEL(tc, max, abs_coords[0], abs_coords[0], BRW_CONDITIONAL_GE);
  496.          tc_SEL(tc, max, tsrc_from(max), abs_coords[0], BRW_CONDITIONAL_GE);
  497.          tc_INV(tc, max, tsrc_from(max));
  498.  
  499.          for (i = 0; i < 3; i++)
  500.             tc_MUL(tc, tdst_writemask(tmp, 1 << i), coords, tsrc_from(max));
  501.  
  502.          coords = tsrc_from(tmp);
  503.       }
  504.       break;
  505.    }
  506.  
  507.    /* set up sampler parameters */
  508.    msg_len = vs_add_sampler_params(tc, msg_type, base_mrf,
  509.          coords, num_coords, bias_or_lod, ref_or_si, ddx, ddy, num_derivs);
  510.  
  511.    /*
  512.     * From the Sandy Bridge PRM, volume 4 part 1, page 136:
  513.     *
  514.     *     "The maximum message length allowed to the sampler is 11. This would
  515.     *      disallow sample_d, sample_b_c, and sample_l_c with a SIMD Mode of
  516.     *      SIMD16."
  517.     */
  518.    if (msg_len > 11)
  519.       tc_fail(tc, "maximum length for messages to the sampler is 11");
  520.  
  521.    if (ret_sampler_index)
  522.       *ret_sampler_index = sampler_index;
  523.  
  524.    return tsrc_imm_mdesc_sampler(tc, msg_len, 1,
  525.          false, simd_mode, msg_type, sampler_index, binding_table_index);
  526. }
  527.  
  528. static void
  529. vs_lower_opcode_tgsi_sampling(struct vs_compile_context *vcc,
  530.                               struct toy_inst *inst)
  531. {
  532.    struct toy_compiler *tc = &vcc->tc;
  533.    struct toy_src desc;
  534.    struct toy_dst dst, tmp;
  535.    unsigned sampler_index;
  536.    int swizzles[4], i;
  537.    unsigned swizzle_zero_mask, swizzle_one_mask, swizzle_normal_mask;
  538.    bool need_filter;
  539.  
  540.    desc = vs_prepare_tgsi_sampling(tc, inst,
  541.          vcc->first_free_mrf, &sampler_index);
  542.  
  543.    switch (inst->opcode) {
  544.    case TOY_OPCODE_TGSI_TXF:
  545.    case TOY_OPCODE_TGSI_TXQ:
  546.    case TOY_OPCODE_TGSI_TXQ_LZ:
  547.       need_filter = false;
  548.       break;
  549.    default:
  550.       need_filter = true;
  551.       break;
  552.    }
  553.  
  554.    toy_compiler_lower_to_send(tc, inst, false, BRW_SFID_SAMPLER);
  555.    inst->src[0] = tsrc(TOY_FILE_MRF, vcc->first_free_mrf, 0);
  556.    inst->src[1] = desc;
  557.  
  558.    /* write to a temp first */
  559.    tmp = tc_alloc_tmp(tc);
  560.    tmp.type = inst->dst.type;
  561.    dst = inst->dst;
  562.    inst->dst = tmp;
  563.  
  564.    tc_move_inst(tc, inst);
  565.  
  566.    if (need_filter) {
  567.       assert(sampler_index < vcc->variant->num_sampler_views);
  568.       swizzles[0] = vcc->variant->sampler_view_swizzles[sampler_index].r;
  569.       swizzles[1] = vcc->variant->sampler_view_swizzles[sampler_index].g;
  570.       swizzles[2] = vcc->variant->sampler_view_swizzles[sampler_index].b;
  571.       swizzles[3] = vcc->variant->sampler_view_swizzles[sampler_index].a;
  572.    }
  573.    else {
  574.       swizzles[0] = PIPE_SWIZZLE_RED;
  575.       swizzles[1] = PIPE_SWIZZLE_GREEN;
  576.       swizzles[2] = PIPE_SWIZZLE_BLUE;
  577.       swizzles[3] = PIPE_SWIZZLE_ALPHA;
  578.    }
  579.  
  580.    swizzle_zero_mask = 0;
  581.    swizzle_one_mask = 0;
  582.    swizzle_normal_mask = 0;
  583.    for (i = 0; i < 4; i++) {
  584.       switch (swizzles[i]) {
  585.       case PIPE_SWIZZLE_ZERO:
  586.          swizzle_zero_mask |= 1 << i;
  587.          swizzles[i] = i;
  588.          break;
  589.       case PIPE_SWIZZLE_ONE:
  590.          swizzle_one_mask |= 1 << i;
  591.          swizzles[i] = i;
  592.          break;
  593.       default:
  594.          swizzle_normal_mask |= 1 << i;
  595.          break;
  596.       }
  597.    }
  598.  
  599.    /* swizzle the results */
  600.    if (swizzle_normal_mask) {
  601.       tc_MOV(tc, tdst_writemask(dst, swizzle_normal_mask),
  602.             tsrc_swizzle(tsrc_from(tmp), swizzles[0],
  603.                swizzles[1], swizzles[2], swizzles[3]));
  604.    }
  605.    if (swizzle_zero_mask)
  606.       tc_MOV(tc, tdst_writemask(dst, swizzle_zero_mask), tsrc_imm_f(0.0f));
  607.    if (swizzle_one_mask)
  608.       tc_MOV(tc, tdst_writemask(dst, swizzle_one_mask), tsrc_imm_f(1.0f));
  609. }
  610.  
  611. static void
  612. vs_lower_opcode_urb_write(struct toy_compiler *tc, struct toy_inst *inst)
  613. {
  614.    /* vs_write_vue() has set up the message registers */
  615.    toy_compiler_lower_to_send(tc, inst, false, BRW_SFID_URB);
  616. }
  617.  
  618. static void
  619. vs_lower_virtual_opcodes(struct vs_compile_context *vcc)
  620. {
  621.    struct toy_compiler *tc = &vcc->tc;
  622.    struct toy_inst *inst;
  623.  
  624.    tc_head(tc);
  625.    while ((inst = tc_next(tc)) != NULL) {
  626.       switch (inst->opcode) {
  627.       case TOY_OPCODE_TGSI_IN:
  628.       case TOY_OPCODE_TGSI_CONST:
  629.       case TOY_OPCODE_TGSI_SV:
  630.       case TOY_OPCODE_TGSI_IMM:
  631.          vs_lower_opcode_tgsi_direct(vcc, inst);
  632.          break;
  633.       case TOY_OPCODE_TGSI_INDIRECT_FETCH:
  634.       case TOY_OPCODE_TGSI_INDIRECT_STORE:
  635.          vs_lower_opcode_tgsi_indirect(vcc, inst);
  636.          break;
  637.       case TOY_OPCODE_TGSI_TEX:
  638.       case TOY_OPCODE_TGSI_TXB:
  639.       case TOY_OPCODE_TGSI_TXD:
  640.       case TOY_OPCODE_TGSI_TXL:
  641.       case TOY_OPCODE_TGSI_TXP:
  642.       case TOY_OPCODE_TGSI_TXF:
  643.       case TOY_OPCODE_TGSI_TXQ:
  644.       case TOY_OPCODE_TGSI_TXQ_LZ:
  645.       case TOY_OPCODE_TGSI_TEX2:
  646.       case TOY_OPCODE_TGSI_TXB2:
  647.       case TOY_OPCODE_TGSI_TXL2:
  648.       case TOY_OPCODE_TGSI_SAMPLE:
  649.       case TOY_OPCODE_TGSI_SAMPLE_I:
  650.       case TOY_OPCODE_TGSI_SAMPLE_I_MS:
  651.       case TOY_OPCODE_TGSI_SAMPLE_B:
  652.       case TOY_OPCODE_TGSI_SAMPLE_C:
  653.       case TOY_OPCODE_TGSI_SAMPLE_C_LZ:
  654.       case TOY_OPCODE_TGSI_SAMPLE_D:
  655.       case TOY_OPCODE_TGSI_SAMPLE_L:
  656.       case TOY_OPCODE_TGSI_GATHER4:
  657.       case TOY_OPCODE_TGSI_SVIEWINFO:
  658.       case TOY_OPCODE_TGSI_SAMPLE_POS:
  659.       case TOY_OPCODE_TGSI_SAMPLE_INFO:
  660.          vs_lower_opcode_tgsi_sampling(vcc, inst);
  661.          break;
  662.       case TOY_OPCODE_INV:
  663.       case TOY_OPCODE_LOG:
  664.       case TOY_OPCODE_EXP:
  665.       case TOY_OPCODE_SQRT:
  666.       case TOY_OPCODE_RSQ:
  667.       case TOY_OPCODE_SIN:
  668.       case TOY_OPCODE_COS:
  669.       case TOY_OPCODE_FDIV:
  670.       case TOY_OPCODE_POW:
  671.       case TOY_OPCODE_INT_DIV_QUOTIENT:
  672.       case TOY_OPCODE_INT_DIV_REMAINDER:
  673.          toy_compiler_lower_math(tc, inst);
  674.          break;
  675.       case TOY_OPCODE_URB_WRITE:
  676.          vs_lower_opcode_urb_write(tc, inst);
  677.          break;
  678.       default:
  679.          if (inst->opcode > 127)
  680.             tc_fail(tc, "unhandled virtual opcode");
  681.          break;
  682.       }
  683.    }
  684. }
  685.  
  686. /**
  687.  * Compile the shader.
  688.  */
  689. static bool
  690. vs_compile(struct vs_compile_context *vcc)
  691. {
  692.    struct toy_compiler *tc = &vcc->tc;
  693.    struct ilo_shader *sh = vcc->shader;
  694.  
  695.    vs_lower_virtual_opcodes(vcc);
  696.    toy_compiler_legalize_for_ra(tc);
  697.    toy_compiler_optimize(tc);
  698.    toy_compiler_allocate_registers(tc,
  699.          vcc->first_free_grf,
  700.          vcc->last_free_grf,
  701.          vcc->num_grf_per_vrf);
  702.    toy_compiler_legalize_for_asm(tc);
  703.  
  704.    if (tc->fail) {
  705.       ilo_err("failed to legalize VS instructions: %s\n", tc->reason);
  706.       return false;
  707.    }
  708.  
  709.    if (ilo_debug & ILO_DEBUG_VS) {
  710.       ilo_printf("legalized instructions:\n");
  711.       toy_compiler_dump(tc);
  712.       ilo_printf("\n");
  713.    }
  714.  
  715.    if (true) {
  716.       sh->kernel = toy_compiler_assemble(tc, &sh->kernel_size);
  717.    }
  718.    else {
  719.       static const uint32_t microcode[] = {
  720.          /* fill in the microcode here */
  721.          0x0, 0x0, 0x0, 0x0,
  722.       };
  723.       const bool swap = true;
  724.  
  725.       sh->kernel_size = sizeof(microcode);
  726.       sh->kernel = MALLOC(sh->kernel_size);
  727.  
  728.       if (sh->kernel) {
  729.          const int num_dwords = sizeof(microcode) / 4;
  730.          const uint32_t *src = microcode;
  731.          uint32_t *dst = (uint32_t *) sh->kernel;
  732.          int i;
  733.  
  734.          for (i = 0; i < num_dwords; i += 4) {
  735.             if (swap) {
  736.                dst[i + 0] = src[i + 3];
  737.                dst[i + 1] = src[i + 2];
  738.                dst[i + 2] = src[i + 1];
  739.                dst[i + 3] = src[i + 0];
  740.             }
  741.             else {
  742.                memcpy(dst, src, 16);
  743.             }
  744.          }
  745.       }
  746.    }
  747.  
  748.    if (!sh->kernel) {
  749.       ilo_err("failed to compile VS: %s\n", tc->reason);
  750.       return false;
  751.    }
  752.  
  753.    if (ilo_debug & ILO_DEBUG_VS) {
  754.       ilo_printf("disassembly:\n");
  755.       toy_compiler_disassemble(tc, sh->kernel, sh->kernel_size);
  756.       ilo_printf("\n");
  757.    }
  758.  
  759.    return true;
  760. }
  761.  
  762. /**
  763.  * Collect the toy registers to be written to the VUE.
  764.  */
  765. static int
  766. vs_collect_outputs(struct vs_compile_context *vcc, struct toy_src *outs)
  767. {
  768.    const struct toy_tgsi *tgsi = &vcc->tgsi;
  769.    int i;
  770.  
  771.    for (i = 0; i < vcc->shader->out.count; i++) {
  772.       const int slot = vcc->output_map[i];
  773.       const int vrf = (slot >= 0) ? toy_tgsi_get_vrf(tgsi,
  774.             TGSI_FILE_OUTPUT, 0, tgsi->outputs[slot].index) : -1;
  775.       struct toy_src src;
  776.  
  777.       if (vrf >= 0) {
  778.          struct toy_dst dst;
  779.  
  780.          dst = tdst(TOY_FILE_VRF, vrf, 0);
  781.          src = tsrc_from(dst);
  782.  
  783.          if (i == 0) {
  784.             /* PSIZE is at channel W */
  785.             tc_MOV(&vcc->tc, tdst_writemask(dst, TOY_WRITEMASK_W),
  786.                   tsrc_swizzle1(src, TOY_SWIZZLE_X));
  787.  
  788.             /* the other channels are for the header */
  789.             dst = tdst_d(dst);
  790.             tc_MOV(&vcc->tc, tdst_writemask(dst, TOY_WRITEMASK_XYZ),
  791.                   tsrc_imm_d(0));
  792.          }
  793.          else {
  794.             /* initialize unused channels to 0.0f */
  795.             if (tgsi->outputs[slot].undefined_mask) {
  796.                dst = tdst_writemask(dst, tgsi->outputs[slot].undefined_mask);
  797.                tc_MOV(&vcc->tc, dst, tsrc_imm_f(0.0f));
  798.             }
  799.          }
  800.       }
  801.       else {
  802.          /* XXX this is too ugly */
  803.          if (vcc->shader->out.semantic_names[i] == TGSI_SEMANTIC_CLIPDIST &&
  804.              slot < 0) {
  805.             /* ok, we need to compute clip distance */
  806.             int clipvert_slot = -1, clipvert_vrf, j;
  807.  
  808.             for (j = 0; j < tgsi->num_outputs; j++) {
  809.                if (tgsi->outputs[j].semantic_name ==
  810.                      TGSI_SEMANTIC_CLIPVERTEX) {
  811.                   clipvert_slot = j;
  812.                   break;
  813.                }
  814.                else if (tgsi->outputs[j].semantic_name ==
  815.                      TGSI_SEMANTIC_POSITION) {
  816.                   /* remember pos, but keep looking */
  817.                   clipvert_slot = j;
  818.                }
  819.             }
  820.  
  821.             clipvert_vrf = (clipvert_slot >= 0) ? toy_tgsi_get_vrf(tgsi,
  822.                   TGSI_FILE_OUTPUT, 0, tgsi->outputs[clipvert_slot].index) : -1;
  823.             if (clipvert_vrf >= 0) {
  824.                struct toy_dst tmp = tc_alloc_tmp(&vcc->tc);
  825.                struct toy_src clipvert = tsrc(TOY_FILE_VRF, clipvert_vrf, 0);
  826.                int first_ucp, last_ucp;
  827.  
  828.                if (vcc->shader->out.semantic_indices[i]) {
  829.                   first_ucp = 4;
  830.                   last_ucp = MIN2(7, vcc->variant->u.vs.num_ucps - 1);
  831.                }
  832.                else {
  833.                   first_ucp = 0;
  834.                   last_ucp = MIN2(3, vcc->variant->u.vs.num_ucps - 1);
  835.                }
  836.  
  837.                for (j = first_ucp; j <= last_ucp; j++) {
  838.                   const int plane_grf = vcc->first_const_grf + j / 2;
  839.                   const int plane_subreg = (j & 1) * 16;
  840.                   const struct toy_src plane = tsrc_rect(tsrc(TOY_FILE_GRF,
  841.                            plane_grf, plane_subreg), TOY_RECT_041);
  842.                   const unsigned writemask = 1 << ((j >= 4) ? j - 4 : j);
  843.  
  844.                   tc_DP4(&vcc->tc, tdst_writemask(tmp, writemask),
  845.                         clipvert, plane);
  846.                }
  847.  
  848.                src = tsrc_from(tmp);
  849.             }
  850.             else {
  851.                src = tsrc_imm_f(0.0f);
  852.             }
  853.          }
  854.          else {
  855.             src = (i == 0) ? tsrc_imm_d(0) : tsrc_imm_f(0.0f);
  856.          }
  857.       }
  858.  
  859.       outs[i] = src;
  860.    }
  861.  
  862.    return i;
  863. }
  864.  
  865. /**
  866.  * Emit instructions to write the VUE.
  867.  */
  868. static void
  869. vs_write_vue(struct vs_compile_context *vcc)
  870. {
  871.    struct toy_compiler *tc = &vcc->tc;
  872.    struct toy_src outs[PIPE_MAX_SHADER_OUTPUTS];
  873.    struct toy_dst header;
  874.    struct toy_src r0;
  875.    struct toy_inst *inst;
  876.    int sent_attrs, total_attrs;
  877.  
  878.    header = tdst_ud(tdst(TOY_FILE_MRF, vcc->first_free_mrf, 0));
  879.    r0 = tsrc_ud(tsrc(TOY_FILE_GRF, 0, 0));
  880.    inst = tc_MOV(tc, header, r0);
  881.    inst->mask_ctrl = BRW_MASK_DISABLE;
  882.  
  883.    if (tc->dev->gen >= ILO_GEN(7)) {
  884.       inst = tc_OR(tc, tdst_offset(header, 0, 5),
  885.             tsrc_rect(tsrc_offset(r0, 0, 5), TOY_RECT_010),
  886.             tsrc_rect(tsrc_imm_ud(0xff00), TOY_RECT_010));
  887.       inst->exec_size = BRW_EXECUTE_1;
  888.       inst->access_mode = BRW_ALIGN_1;
  889.       inst->mask_ctrl = BRW_MASK_DISABLE;
  890.    }
  891.  
  892.    total_attrs = vs_collect_outputs(vcc, outs);
  893.    sent_attrs = 0;
  894.    while (sent_attrs < total_attrs) {
  895.       struct toy_src desc;
  896.       int mrf = vcc->first_free_mrf + 1, avail_mrf_for_attrs;
  897.       int num_attrs, msg_len, i;
  898.       bool eot;
  899.  
  900.       num_attrs = total_attrs - sent_attrs;
  901.       eot = true;
  902.  
  903.       /* see if we need another message */
  904.       avail_mrf_for_attrs = vcc->last_free_mrf - mrf + 1;
  905.       if (num_attrs > avail_mrf_for_attrs) {
  906.          /*
  907.           * From the Sandy Bridge PRM, volume 4 part 2, page 22:
  908.           *
  909.           *     "Offset. This field specifies a destination offset (in 256-bit
  910.           *      units) from the start of the URB entry(s), as referenced by
  911.           *      URB Return Handle n, at which the data (if any) will be
  912.           *      written."
  913.           *
  914.           * As we need to offset the following messages, we must make sure
  915.           * this one writes an even number of attributes.
  916.           */
  917.          num_attrs = avail_mrf_for_attrs & ~1;
  918.          eot = false;
  919.       }
  920.  
  921.       if (tc->dev->gen >= ILO_GEN(7)) {
  922.          /* do not forget about the header */
  923.          msg_len = 1 + num_attrs;
  924.       }
  925.       else {
  926.          /*
  927.           * From the Sandy Bridge PRM, volume 4 part 2, page 26:
  928.           *
  929.           *     "At least 256 bits per vertex (512 bits total, M1 & M2) must
  930.           *      be written.  Writing only 128 bits per vertex (256 bits
  931.           *      total, M1 only) results in UNDEFINED operation."
  932.           *
  933.           *     "[DevSNB] Interleave writes must be in multiples of 256 per
  934.           *      vertex."
  935.           *
  936.           * That is, we must write or appear to write an even number of
  937.           * attributes, starting from two.
  938.           */
  939.          if (num_attrs % 2 && num_attrs == avail_mrf_for_attrs) {
  940.             num_attrs--;
  941.             eot = false;
  942.          }
  943.  
  944.          msg_len = 1 + align(num_attrs, 2);
  945.       }
  946.  
  947.       for (i = 0; i < num_attrs; i++)
  948.          tc_MOV(tc, tdst(TOY_FILE_MRF, mrf++, 0), outs[sent_attrs + i]);
  949.  
  950.       assert(sent_attrs % 2 == 0);
  951.       desc = tsrc_imm_mdesc_urb(tc, eot, msg_len, 0,
  952.             eot, true, false, BRW_URB_SWIZZLE_INTERLEAVE, sent_attrs / 2, 0);
  953.  
  954.       tc_add2(tc, TOY_OPCODE_URB_WRITE, tdst_null(), tsrc_from(header), desc);
  955.  
  956.       sent_attrs += num_attrs;
  957.    }
  958. }
  959.  
  960. /**
  961.  * Set up shader inputs for fixed-function units.
  962.  */
  963. static void
  964. vs_setup_shader_in(struct ilo_shader *sh, const struct toy_tgsi *tgsi)
  965. {
  966.    int num_attrs, i;
  967.  
  968.    /* vertex/instance id is the first VE if exists */
  969.    for (i = 0; i < tgsi->num_system_values; i++) {
  970.       bool found = false;
  971.  
  972.       switch (tgsi->system_values[i].semantic_name) {
  973.       case TGSI_SEMANTIC_INSTANCEID:
  974.       case TGSI_SEMANTIC_VERTEXID:
  975.          found = true;
  976.          break;
  977.       default:
  978.          break;
  979.       }
  980.  
  981.       if (found) {
  982.          sh->in.semantic_names[sh->in.count] =
  983.             tgsi->system_values[i].semantic_name;
  984.          sh->in.semantic_indices[sh->in.count] =
  985.             tgsi->system_values[i].semantic_index;
  986.          sh->in.interp[sh->in.count] = TGSI_INTERPOLATE_CONSTANT;
  987.          sh->in.centroid[sh->in.count] = false;
  988.  
  989.          sh->in.count++;
  990.          break;
  991.       }
  992.    }
  993.  
  994.    num_attrs = 0;
  995.    for (i = 0; i < tgsi->num_inputs; i++) {
  996.       assert(tgsi->inputs[i].semantic_name == TGSI_SEMANTIC_GENERIC);
  997.       if (tgsi->inputs[i].semantic_index >= num_attrs)
  998.          num_attrs = tgsi->inputs[i].semantic_index + 1;
  999.    }
  1000.    assert(num_attrs <= PIPE_MAX_ATTRIBS);
  1001.  
  1002.    /* VF cannot remap VEs.  VE[i] must be used as GENERIC[i]. */
  1003.    for (i = 0; i < num_attrs; i++) {
  1004.       sh->in.semantic_names[sh->in.count + i] = TGSI_SEMANTIC_GENERIC;
  1005.       sh->in.semantic_indices[sh->in.count + i] = i;
  1006.       sh->in.interp[sh->in.count + i] = TGSI_INTERPOLATE_CONSTANT;
  1007.       sh->in.centroid[sh->in.count + i] = false;
  1008.    }
  1009.  
  1010.    sh->in.count += num_attrs;
  1011.  
  1012.    sh->in.has_pos = false;
  1013.    sh->in.has_linear_interp = false;
  1014.    sh->in.barycentric_interpolation_mode = 0;
  1015. }
  1016.  
  1017. /**
  1018.  * Set up shader outputs for fixed-function units.
  1019.  */
  1020. static void
  1021. vs_setup_shader_out(struct ilo_shader *sh, const struct toy_tgsi *tgsi,
  1022.                     bool output_clipdist, int *output_map)
  1023. {
  1024.    int psize_slot = -1, pos_slot = -1;
  1025.    int clipdist_slot[2] = { -1, -1 };
  1026.    int color_slot[4] = { -1, -1, -1, -1 };
  1027.    int num_outs, i;
  1028.  
  1029.    /* find out the slots of outputs that need special care */
  1030.    for (i = 0; i < tgsi->num_outputs; i++) {
  1031.       switch (tgsi->outputs[i].semantic_name) {
  1032.       case TGSI_SEMANTIC_PSIZE:
  1033.          psize_slot = i;
  1034.          break;
  1035.       case TGSI_SEMANTIC_POSITION:
  1036.          pos_slot = i;
  1037.          break;
  1038.       case TGSI_SEMANTIC_CLIPDIST:
  1039.          if (tgsi->outputs[i].semantic_index)
  1040.             clipdist_slot[1] = i;
  1041.          else
  1042.             clipdist_slot[0] = i;
  1043.          break;
  1044.       case TGSI_SEMANTIC_COLOR:
  1045.          if (tgsi->outputs[i].semantic_index)
  1046.             color_slot[2] = i;
  1047.          else
  1048.             color_slot[0] = i;
  1049.          break;
  1050.       case TGSI_SEMANTIC_BCOLOR:
  1051.          if (tgsi->outputs[i].semantic_index)
  1052.             color_slot[3] = i;
  1053.          else
  1054.             color_slot[1] = i;
  1055.          break;
  1056.       default:
  1057.          break;
  1058.       }
  1059.    }
  1060.  
  1061.    /* the first two VUEs are always PSIZE and POSITION */
  1062.    num_outs = 2;
  1063.    output_map[0] = psize_slot;
  1064.    output_map[1] = pos_slot;
  1065.  
  1066.    sh->out.register_indices[0] =
  1067.       (psize_slot >= 0) ? tgsi->outputs[psize_slot].index : -1;
  1068.    sh->out.semantic_names[0] = TGSI_SEMANTIC_PSIZE;
  1069.    sh->out.semantic_indices[0] = 0;
  1070.  
  1071.    sh->out.register_indices[1] =
  1072.       (pos_slot >= 0) ? tgsi->outputs[pos_slot].index : -1;
  1073.    sh->out.semantic_names[1] = TGSI_SEMANTIC_POSITION;
  1074.    sh->out.semantic_indices[1] = 0;
  1075.  
  1076.    sh->out.has_pos = true;
  1077.  
  1078.    /* followed by optional clip distances */
  1079.    if (output_clipdist) {
  1080.       sh->out.register_indices[num_outs] =
  1081.          (clipdist_slot[0] >= 0) ? tgsi->outputs[clipdist_slot[0]].index : -1;
  1082.       sh->out.semantic_names[num_outs] = TGSI_SEMANTIC_CLIPDIST;
  1083.       sh->out.semantic_indices[num_outs] = 0;
  1084.       output_map[num_outs++] = clipdist_slot[0];
  1085.  
  1086.       sh->out.register_indices[num_outs] =
  1087.          (clipdist_slot[1] >= 0) ? tgsi->outputs[clipdist_slot[1]].index : -1;
  1088.       sh->out.semantic_names[num_outs] = TGSI_SEMANTIC_CLIPDIST;
  1089.       sh->out.semantic_indices[num_outs] = 1;
  1090.       output_map[num_outs++] = clipdist_slot[1];
  1091.    }
  1092.  
  1093.    /*
  1094.     * make BCOLOR follow COLOR so that we can make use of
  1095.     * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING in 3DSTATE_SF
  1096.     */
  1097.    for (i = 0; i < 4; i++) {
  1098.       const int slot = color_slot[i];
  1099.  
  1100.       if (slot < 0)
  1101.          continue;
  1102.  
  1103.       sh->out.register_indices[num_outs] = tgsi->outputs[slot].index;
  1104.       sh->out.semantic_names[num_outs] = tgsi->outputs[slot].semantic_name;
  1105.       sh->out.semantic_indices[num_outs] = tgsi->outputs[slot].semantic_index;
  1106.  
  1107.       output_map[num_outs++] = slot;
  1108.    }
  1109.  
  1110.    /* add the rest of the outputs */
  1111.    for (i = 0; i < tgsi->num_outputs; i++) {
  1112.       switch (tgsi->outputs[i].semantic_name) {
  1113.       case TGSI_SEMANTIC_PSIZE:
  1114.       case TGSI_SEMANTIC_POSITION:
  1115.       case TGSI_SEMANTIC_CLIPDIST:
  1116.       case TGSI_SEMANTIC_COLOR:
  1117.       case TGSI_SEMANTIC_BCOLOR:
  1118.          break;
  1119.       default:
  1120.          sh->out.register_indices[num_outs] = tgsi->outputs[i].index;
  1121.          sh->out.semantic_names[num_outs] = tgsi->outputs[i].semantic_name;
  1122.          sh->out.semantic_indices[num_outs] = tgsi->outputs[i].semantic_index;
  1123.          output_map[num_outs++] = i;
  1124.          break;
  1125.       }
  1126.    }
  1127.  
  1128.    sh->out.count = num_outs;
  1129. }
  1130.  
  1131. /**
  1132.  * Translate the TGSI tokens.
  1133.  */
  1134. static bool
  1135. vs_setup_tgsi(struct toy_compiler *tc, const struct tgsi_token *tokens,
  1136.               struct toy_tgsi *tgsi)
  1137. {
  1138.    if (ilo_debug & ILO_DEBUG_VS) {
  1139.       ilo_printf("dumping vertex shader\n");
  1140.       ilo_printf("\n");
  1141.  
  1142.       tgsi_dump(tokens, 0);
  1143.       ilo_printf("\n");
  1144.    }
  1145.  
  1146.    toy_compiler_translate_tgsi(tc, tokens, true, tgsi);
  1147.    if (tc->fail) {
  1148.       ilo_err("failed to translate VS TGSI tokens: %s\n", tc->reason);
  1149.       return false;
  1150.    }
  1151.  
  1152.    if (ilo_debug & ILO_DEBUG_VS) {
  1153.       ilo_printf("TGSI translator:\n");
  1154.       toy_tgsi_dump(tgsi);
  1155.       ilo_printf("\n");
  1156.       toy_compiler_dump(tc);
  1157.       ilo_printf("\n");
  1158.    }
  1159.  
  1160.    return true;
  1161. }
  1162.  
  1163. /**
  1164.  * Set up VS compile context.  This includes translating the TGSI tokens.
  1165.  */
  1166. static bool
  1167. vs_setup(struct vs_compile_context *vcc,
  1168.          const struct ilo_shader_state *state,
  1169.          const struct ilo_shader_variant *variant)
  1170. {
  1171.    int num_consts;
  1172.  
  1173.    memset(vcc, 0, sizeof(*vcc));
  1174.  
  1175.    vcc->shader = CALLOC_STRUCT(ilo_shader);
  1176.    if (!vcc->shader)
  1177.       return false;
  1178.  
  1179.    vcc->variant = variant;
  1180.  
  1181.    toy_compiler_init(&vcc->tc, state->info.dev);
  1182.    vcc->tc.templ.access_mode = BRW_ALIGN_16;
  1183.    vcc->tc.templ.exec_size = BRW_EXECUTE_8;
  1184.    vcc->tc.rect_linear_width = 4;
  1185.  
  1186.    /*
  1187.     * The classic driver uses the sampler cache (gen6) or the data cache
  1188.     * (gen7).  Why?
  1189.     */
  1190.    vcc->const_cache = GEN6_SFID_DATAPORT_CONSTANT_CACHE;
  1191.  
  1192.    if (!vs_setup_tgsi(&vcc->tc, state->info.tokens, &vcc->tgsi)) {
  1193.       toy_compiler_cleanup(&vcc->tc);
  1194.       FREE(vcc->shader);
  1195.       return false;
  1196.    }
  1197.  
  1198.    vs_setup_shader_in(vcc->shader, &vcc->tgsi);
  1199.    vs_setup_shader_out(vcc->shader, &vcc->tgsi,
  1200.          (vcc->variant->u.vs.num_ucps > 0), vcc->output_map);
  1201.  
  1202.    /* fit each pair of user clip planes into a register */
  1203.    num_consts = (vcc->variant->u.vs.num_ucps + 1) / 2;
  1204.  
  1205.    /* r0 is reserved for payload header */
  1206.    vcc->first_const_grf = 1;
  1207.    vcc->first_vue_grf = vcc->first_const_grf + num_consts;
  1208.    vcc->first_free_grf = vcc->first_vue_grf + vcc->shader->in.count;
  1209.    vcc->last_free_grf = 127;
  1210.  
  1211.    /* m0 is reserved for system routines */
  1212.    vcc->first_free_mrf = 1;
  1213.    vcc->last_free_mrf = 15;
  1214.  
  1215.    vcc->num_grf_per_vrf = 1;
  1216.  
  1217.    if (vcc->tc.dev->gen >= ILO_GEN(7)) {
  1218.       vcc->last_free_grf -= 15;
  1219.       vcc->first_free_mrf = vcc->last_free_grf + 1;
  1220.       vcc->last_free_mrf = vcc->first_free_mrf + 14;
  1221.    }
  1222.  
  1223.    vcc->shader->in.start_grf = vcc->first_const_grf;
  1224.    vcc->shader->pcb.clip_state_size =
  1225.       vcc->variant->u.vs.num_ucps * (sizeof(float) * 4);
  1226.  
  1227.    return true;
  1228. }
  1229.  
  1230. /**
  1231.  * Compile the vertex shader.
  1232.  */
  1233. struct ilo_shader *
  1234. ilo_shader_compile_vs(const struct ilo_shader_state *state,
  1235.                       const struct ilo_shader_variant *variant)
  1236. {
  1237.    struct vs_compile_context vcc;
  1238.    bool need_gs;
  1239.  
  1240.    if (!vs_setup(&vcc, state, variant))
  1241.       return NULL;
  1242.  
  1243.    if (vcc.tc.dev->gen >= ILO_GEN(7)) {
  1244.       need_gs = false;
  1245.    }
  1246.    else {
  1247.       need_gs = variant->u.vs.rasterizer_discard ||
  1248.                 state->info.stream_output.num_outputs;
  1249.    }
  1250.  
  1251.    vs_write_vue(&vcc);
  1252.  
  1253.    if (!vs_compile(&vcc)) {
  1254.       FREE(vcc.shader);
  1255.       vcc.shader = NULL;
  1256.    }
  1257.  
  1258.    toy_tgsi_cleanup(&vcc.tgsi);
  1259.    toy_compiler_cleanup(&vcc.tc);
  1260.  
  1261.    if (need_gs) {
  1262.       int so_mapping[PIPE_MAX_SHADER_OUTPUTS];
  1263.       int i, j;
  1264.  
  1265.       for (i = 0; i < vcc.tgsi.num_outputs; i++) {
  1266.          int attr = 0;
  1267.  
  1268.          for (j = 0; j < vcc.shader->out.count; j++) {
  1269.             if (vcc.tgsi.outputs[i].semantic_name ==
  1270.                   vcc.shader->out.semantic_names[j] &&
  1271.                 vcc.tgsi.outputs[i].semantic_index ==
  1272.                   vcc.shader->out.semantic_indices[j]) {
  1273.                attr = j;
  1274.                break;
  1275.             }
  1276.          }
  1277.  
  1278.          so_mapping[i] = attr;
  1279.       }
  1280.  
  1281.       if (!ilo_shader_compile_gs_passthrough(state, variant,
  1282.                so_mapping, vcc.shader)) {
  1283.          ilo_shader_destroy_kernel(vcc.shader);
  1284.          vcc.shader = NULL;
  1285.       }
  1286.    }
  1287.  
  1288.    return vcc.shader;
  1289. }
  1290.