Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * on the rights to use, copy, modify, merge, publish, distribute, sub
  8.  * license, and/or sell copies of the Software, and to permit persons to whom
  9.  * the Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18.  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  19.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  20.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  21.  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  */
  23. #include "r600_sq.h"
  24. #include "r600_llvm.h"
  25. #include "r600_formats.h"
  26. #include "r600_opcodes.h"
  27. #include "r600_shader.h"
  28. #include "r600d.h"
  29.  
  30. #include "sb/sb_public.h"
  31.  
  32. #include "pipe/p_shader_tokens.h"
  33. #include "tgsi/tgsi_info.h"
  34. #include "tgsi/tgsi_parse.h"
  35. #include "tgsi/tgsi_scan.h"
  36. #include "tgsi/tgsi_dump.h"
  37. #include "util/u_memory.h"
  38. #include "util/u_math.h"
  39. #include <stdio.h>
  40. #include <errno.h>
  41.  
  42. /* CAYMAN notes
  43. Why CAYMAN got loops for lots of instructions is explained here.
  44.  
  45. -These 8xx t-slot only ops are implemented in all vector slots.
  46. MUL_LIT, FLT_TO_UINT, INT_TO_FLT, UINT_TO_FLT
  47. These 8xx t-slot only opcodes become vector ops, with all four
  48. slots expecting the arguments on sources a and b. Result is
  49. broadcast to all channels.
  50. MULLO_INT, MULHI_INT, MULLO_UINT, MULHI_UINT
  51. These 8xx t-slot only opcodes become vector ops in the z, y, and
  52. x slots.
  53. EXP_IEEE, LOG_IEEE/CLAMPED, RECIP_IEEE/CLAMPED/FF/INT/UINT/_64/CLAMPED_64
  54. RECIPSQRT_IEEE/CLAMPED/FF/_64/CLAMPED_64
  55. SQRT_IEEE/_64
  56. SIN/COS
  57. The w slot may have an independent co-issued operation, or if the
  58. result is required to be in the w slot, the opcode above may be
  59. issued in the w slot as well.
  60. The compiler must issue the source argument to slots z, y, and x
  61. */
  62.  
  63. static int r600_shader_from_tgsi(struct r600_context *rctx,
  64.                                  struct r600_pipe_shader *pipeshader,
  65.                                  struct r600_shader_key key);
  66.  
  67.  
  68. static void r600_add_gpr_array(struct r600_shader *ps, int start_gpr,
  69.                            int size, unsigned comp_mask) {
  70.  
  71.         if (!size)
  72.                 return;
  73.  
  74.         if (ps->num_arrays == ps->max_arrays) {
  75.                 ps->max_arrays += 64;
  76.                 ps->arrays = realloc(ps->arrays, ps->max_arrays *
  77.                                      sizeof(struct r600_shader_array));
  78.         }
  79.  
  80.         int n = ps->num_arrays;
  81.         ++ps->num_arrays;
  82.  
  83.         ps->arrays[n].comp_mask = comp_mask;
  84.         ps->arrays[n].gpr_start = start_gpr;
  85.         ps->arrays[n].gpr_count = size;
  86. }
  87.  
  88. static void r600_dump_streamout(struct pipe_stream_output_info *so)
  89. {
  90.         unsigned i;
  91.  
  92.         fprintf(stderr, "STREAMOUT\n");
  93.         for (i = 0; i < so->num_outputs; i++) {
  94.                 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
  95.                                 so->output[i].start_component;
  96.                 fprintf(stderr, "  %i: MEM_STREAM0_BUF%i[%i..%i] <- OUT[%i].%s%s%s%s%s\n",
  97.                         i, so->output[i].output_buffer,
  98.                         so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
  99.                         so->output[i].register_index,
  100.                         mask & 1 ? "x" : "",
  101.                         mask & 2 ? "y" : "",
  102.                         mask & 4 ? "z" : "",
  103.                         mask & 8 ? "w" : "",
  104.                         so->output[i].dst_offset < so->output[i].start_component ? " (will lower)" : "");
  105.         }
  106. }
  107.  
  108. static int store_shader(struct pipe_context *ctx,
  109.                         struct r600_pipe_shader *shader)
  110. {
  111.         struct r600_context *rctx = (struct r600_context *)ctx;
  112.         uint32_t *ptr, i;
  113.  
  114.         if (shader->bo == NULL) {
  115.                 shader->bo = (struct r600_resource*)
  116.                         pipe_buffer_create(ctx->screen, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, shader->shader.bc.ndw * 4);
  117.                 if (shader->bo == NULL) {
  118.                         return -ENOMEM;
  119.                 }
  120.                 ptr = r600_buffer_map_sync_with_rings(&rctx->b, shader->bo, PIPE_TRANSFER_WRITE);
  121.                 if (R600_BIG_ENDIAN) {
  122.                         for (i = 0; i < shader->shader.bc.ndw; ++i) {
  123.                                 ptr[i] = util_cpu_to_le32(shader->shader.bc.bytecode[i]);
  124.                         }
  125.                 } else {
  126.                         memcpy(ptr, shader->shader.bc.bytecode, shader->shader.bc.ndw * sizeof(*ptr));
  127.                 }
  128.                 rctx->b.ws->buffer_unmap(shader->bo->cs_buf);
  129.         }
  130.  
  131.         return 0;
  132. }
  133.  
  134. int r600_pipe_shader_create(struct pipe_context *ctx,
  135.                             struct r600_pipe_shader *shader,
  136.                             struct r600_shader_key key)
  137. {
  138.         struct r600_context *rctx = (struct r600_context *)ctx;
  139.         struct r600_pipe_shader_selector *sel = shader->selector;
  140.         int r;
  141.         bool dump = r600_can_dump_shader(&rctx->screen->b, sel->tokens);
  142.         unsigned use_sb = !(rctx->screen->b.debug_flags & DBG_NO_SB);
  143.         unsigned sb_disasm = use_sb || (rctx->screen->b.debug_flags & DBG_SB_DISASM);
  144.         unsigned export_shader = key.vs_as_es;
  145.  
  146.         shader->shader.bc.isa = rctx->isa;
  147.  
  148.         if (dump) {
  149.                 fprintf(stderr, "--------------------------------------------------------------\n");
  150.                 tgsi_dump(sel->tokens, 0);
  151.  
  152.                 if (sel->so.num_outputs) {
  153.                         r600_dump_streamout(&sel->so);
  154.                 }
  155.         }
  156.         r = r600_shader_from_tgsi(rctx, shader, key);
  157.         if (r) {
  158.                 R600_ERR("translation from TGSI failed !\n");
  159.                 goto error;
  160.         }
  161.  
  162.     /* disable SB for geom shaders on R6xx/R7xx due to some mysterious gs piglit regressions with it enabled. */
  163.     if (rctx->b.chip_class <= R700) {
  164.             use_sb &= (shader->shader.processor_type != TGSI_PROCESSOR_GEOMETRY);
  165.     }
  166.         /* disable SB for shaders using CF_INDEX_0/1 (sampler/ubo array indexing) as it doesn't handle those currently */
  167.         use_sb &= !shader->shader.uses_index_registers;
  168.  
  169.         /* Check if the bytecode has already been built.  When using the llvm
  170.          * backend, r600_shader_from_tgsi() will take care of building the
  171.          * bytecode.
  172.          */
  173.         if (!shader->shader.bc.bytecode) {
  174.                 r = r600_bytecode_build(&shader->shader.bc);
  175.                 if (r) {
  176.                         R600_ERR("building bytecode failed !\n");
  177.                         goto error;
  178.                 }
  179.         }
  180.  
  181.         if (dump && !sb_disasm) {
  182.                 fprintf(stderr, "--------------------------------------------------------------\n");
  183.                 r600_bytecode_disasm(&shader->shader.bc);
  184.                 fprintf(stderr, "______________________________________________________________\n");
  185.         } else if ((dump && sb_disasm) || use_sb) {
  186.                 r = r600_sb_bytecode_process(rctx, &shader->shader.bc, &shader->shader,
  187.                                              dump, use_sb);
  188.                 if (r) {
  189.                         R600_ERR("r600_sb_bytecode_process failed !\n");
  190.                         goto error;
  191.                 }
  192.         }
  193.  
  194.         if (shader->gs_copy_shader) {
  195.                 if (dump) {
  196.                         // dump copy shader
  197.                         r = r600_sb_bytecode_process(rctx, &shader->gs_copy_shader->shader.bc,
  198.                                                      &shader->gs_copy_shader->shader, dump, 0);
  199.                         if (r)
  200.                                 goto error;
  201.                 }
  202.  
  203.                 if ((r = store_shader(ctx, shader->gs_copy_shader)))
  204.                         goto error;
  205.         }
  206.  
  207.         /* Store the shader in a buffer. */
  208.         if ((r = store_shader(ctx, shader)))
  209.                 goto error;
  210.  
  211.         /* Build state. */
  212.         switch (shader->shader.processor_type) {
  213.         case TGSI_PROCESSOR_GEOMETRY:
  214.                 if (rctx->b.chip_class >= EVERGREEN) {
  215.                         evergreen_update_gs_state(ctx, shader);
  216.                         evergreen_update_vs_state(ctx, shader->gs_copy_shader);
  217.                 } else {
  218.                         r600_update_gs_state(ctx, shader);
  219.                         r600_update_vs_state(ctx, shader->gs_copy_shader);
  220.                 }
  221.                 break;
  222.         case TGSI_PROCESSOR_VERTEX:
  223.                 if (rctx->b.chip_class >= EVERGREEN) {
  224.                         if (export_shader)
  225.                                 evergreen_update_es_state(ctx, shader);
  226.                         else
  227.                                 evergreen_update_vs_state(ctx, shader);
  228.                 } else {
  229.                         if (export_shader)
  230.                                 r600_update_es_state(ctx, shader);
  231.                         else
  232.                                 r600_update_vs_state(ctx, shader);
  233.                 }
  234.                 break;
  235.         case TGSI_PROCESSOR_FRAGMENT:
  236.                 if (rctx->b.chip_class >= EVERGREEN) {
  237.                         evergreen_update_ps_state(ctx, shader);
  238.                 } else {
  239.                         r600_update_ps_state(ctx, shader);
  240.                 }
  241.                 break;
  242.         default:
  243.                 r = -EINVAL;
  244.                 goto error;
  245.         }
  246.         return 0;
  247.  
  248. error:
  249.         r600_pipe_shader_destroy(ctx, shader);
  250.         return r;
  251. }
  252.  
  253. void r600_pipe_shader_destroy(struct pipe_context *ctx, struct r600_pipe_shader *shader)
  254. {
  255.         pipe_resource_reference((struct pipe_resource**)&shader->bo, NULL);
  256.         r600_bytecode_clear(&shader->shader.bc);
  257.         r600_release_command_buffer(&shader->command_buffer);
  258. }
  259.  
  260. /*
  261.  * tgsi -> r600 shader
  262.  */
  263. struct r600_shader_tgsi_instruction;
  264.  
  265. struct r600_shader_src {
  266.         unsigned                                sel;
  267.         unsigned                                swizzle[4];
  268.         unsigned                                neg;
  269.         unsigned                                abs;
  270.         unsigned                                rel;
  271.         unsigned                                kc_bank;
  272.         boolean                                 kc_rel; /* true if cache bank is indexed */
  273.         uint32_t                                value[4];
  274. };
  275.  
  276. struct eg_interp {
  277.         boolean                                 enabled;
  278.         unsigned                                ij_index;
  279. };
  280.  
  281. struct r600_shader_ctx {
  282.         struct tgsi_shader_info                 info;
  283.         struct tgsi_parse_context               parse;
  284.         const struct tgsi_token                 *tokens;
  285.         unsigned                                type;
  286.         unsigned                                file_offset[TGSI_FILE_COUNT];
  287.         unsigned                                temp_reg;
  288.         const struct r600_shader_tgsi_instruction       *inst_info;
  289.         struct r600_bytecode                    *bc;
  290.         struct r600_shader                      *shader;
  291.         struct r600_shader_src                  src[4];
  292.         uint32_t                                *literals;
  293.         uint32_t                                nliterals;
  294.         uint32_t                                max_driver_temp_used;
  295.         boolean use_llvm;
  296.         /* needed for evergreen interpolation */
  297.         struct eg_interp                eg_interpolators[6]; // indexed by Persp/Linear * 3 + sample/center/centroid
  298.         /* evergreen/cayman also store sample mask in face register */
  299.         int                                     face_gpr;
  300.         /* sample id is .w component stored in fixed point position register */
  301.         int                                     fixed_pt_position_gpr;
  302.         int                                     colors_used;
  303.         boolean                 clip_vertex_write;
  304.         unsigned                cv_output;
  305.         unsigned                edgeflag_output;
  306.         int                                     fragcoord_input;
  307.         int                                     native_integers;
  308.         int                                     next_ring_offset;
  309.         int                                     gs_out_ring_offset;
  310.         int                                     gs_next_vertex;
  311.         struct r600_shader      *gs_for_vs;
  312.         int                                     gs_export_gpr_treg;
  313. };
  314.  
  315. struct r600_shader_tgsi_instruction {
  316.         unsigned        op;
  317.         int (*process)(struct r600_shader_ctx *ctx);
  318. };
  319.  
  320. static int emit_gs_ring_writes(struct r600_shader_ctx *ctx, bool ind);
  321. static const struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[], eg_shader_tgsi_instruction[], cm_shader_tgsi_instruction[];
  322. static int tgsi_helper_tempx_replicate(struct r600_shader_ctx *ctx);
  323. static inline void callstack_push(struct r600_shader_ctx *ctx, unsigned reason);
  324. static void fc_pushlevel(struct r600_shader_ctx *ctx, int type);
  325. static int tgsi_else(struct r600_shader_ctx *ctx);
  326. static int tgsi_endif(struct r600_shader_ctx *ctx);
  327. static int tgsi_bgnloop(struct r600_shader_ctx *ctx);
  328. static int tgsi_endloop(struct r600_shader_ctx *ctx);
  329. static int tgsi_loop_brk_cont(struct r600_shader_ctx *ctx);
  330. static int tgsi_fetch_rel_const(struct r600_shader_ctx *ctx,
  331.                                 unsigned int cb_idx, unsigned cb_rel, unsigned int offset, unsigned ar_chan,
  332.                                 unsigned int dst_reg);
  333. static void r600_bytecode_src(struct r600_bytecode_alu_src *bc_src,
  334.                         const struct r600_shader_src *shader_src,
  335.                         unsigned chan);
  336.  
  337. static int tgsi_is_supported(struct r600_shader_ctx *ctx)
  338. {
  339.         struct tgsi_full_instruction *i = &ctx->parse.FullToken.FullInstruction;
  340.         int j;
  341.  
  342.         if (i->Instruction.NumDstRegs > 1) {
  343.                 R600_ERR("too many dst (%d)\n", i->Instruction.NumDstRegs);
  344.                 return -EINVAL;
  345.         }
  346.         if (i->Instruction.Predicate) {
  347.                 R600_ERR("predicate unsupported\n");
  348.                 return -EINVAL;
  349.         }
  350. #if 0
  351.         if (i->Instruction.Label) {
  352.                 R600_ERR("label unsupported\n");
  353.                 return -EINVAL;
  354.         }
  355. #endif
  356.         for (j = 0; j < i->Instruction.NumSrcRegs; j++) {
  357.                 if (i->Src[j].Register.Dimension) {
  358.                    switch (i->Src[j].Register.File) {
  359.                    case TGSI_FILE_CONSTANT:
  360.                            break;
  361.                    case TGSI_FILE_INPUT:
  362.                            if (ctx->type == TGSI_PROCESSOR_GEOMETRY)
  363.                                    break;
  364.                    default:
  365.                            R600_ERR("unsupported src %d (dimension %d)\n", j,
  366.                                     i->Src[j].Register.Dimension);
  367.                            return -EINVAL;
  368.                    }
  369.                 }
  370.         }
  371.         for (j = 0; j < i->Instruction.NumDstRegs; j++) {
  372.                 if (i->Dst[j].Register.Dimension) {
  373.                         R600_ERR("unsupported dst (dimension)\n");
  374.                         return -EINVAL;
  375.                 }
  376.         }
  377.         return 0;
  378. }
  379.  
  380. int eg_get_interpolator_index(unsigned interpolate, unsigned location)
  381. {
  382.         if (interpolate == TGSI_INTERPOLATE_COLOR ||
  383.                 interpolate == TGSI_INTERPOLATE_LINEAR ||
  384.                 interpolate == TGSI_INTERPOLATE_PERSPECTIVE)
  385.         {
  386.                 int is_linear = interpolate == TGSI_INTERPOLATE_LINEAR;
  387.                 int loc;
  388.  
  389.                 switch(location) {
  390.                 case TGSI_INTERPOLATE_LOC_CENTER:
  391.                         loc = 1;
  392.                         break;
  393.                 case TGSI_INTERPOLATE_LOC_CENTROID:
  394.                         loc = 2;
  395.                         break;
  396.                 case TGSI_INTERPOLATE_LOC_SAMPLE:
  397.                 default:
  398.                         loc = 0; break;
  399.                 }
  400.  
  401.                 return is_linear * 3 + loc;
  402.         }
  403.  
  404.         return -1;
  405. }
  406.  
  407. static void evergreen_interp_assign_ij_index(struct r600_shader_ctx *ctx,
  408.                 int input)
  409. {
  410.         int i = eg_get_interpolator_index(
  411.                 ctx->shader->input[input].interpolate,
  412.                 ctx->shader->input[input].interpolate_location);
  413.         assert(i >= 0);
  414.         ctx->shader->input[input].ij_index = ctx->eg_interpolators[i].ij_index;
  415. }
  416.  
  417. static int evergreen_interp_alu(struct r600_shader_ctx *ctx, int input)
  418. {
  419.         int i, r;
  420.         struct r600_bytecode_alu alu;
  421.         int gpr = 0, base_chan = 0;
  422.         int ij_index = ctx->shader->input[input].ij_index;
  423.  
  424.         /* work out gpr and base_chan from index */
  425.         gpr = ij_index / 2;
  426.         base_chan = (2 * (ij_index % 2)) + 1;
  427.  
  428.         for (i = 0; i < 8; i++) {
  429.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  430.  
  431.                 if (i < 4)
  432.                         alu.op = ALU_OP2_INTERP_ZW;
  433.                 else
  434.                         alu.op = ALU_OP2_INTERP_XY;
  435.  
  436.                 if ((i > 1) && (i < 6)) {
  437.                         alu.dst.sel = ctx->shader->input[input].gpr;
  438.                         alu.dst.write = 1;
  439.                 }
  440.  
  441.                 alu.dst.chan = i % 4;
  442.  
  443.                 alu.src[0].sel = gpr;
  444.                 alu.src[0].chan = (base_chan - (i % 2));
  445.  
  446.                 alu.src[1].sel = V_SQ_ALU_SRC_PARAM_BASE + ctx->shader->input[input].lds_pos;
  447.  
  448.                 alu.bank_swizzle_force = SQ_ALU_VEC_210;
  449.                 if ((i % 4) == 3)
  450.                         alu.last = 1;
  451.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  452.                 if (r)
  453.                         return r;
  454.         }
  455.         return 0;
  456. }
  457.  
  458. static int evergreen_interp_flat(struct r600_shader_ctx *ctx, int input)
  459. {
  460.         int i, r;
  461.         struct r600_bytecode_alu alu;
  462.  
  463.         for (i = 0; i < 4; i++) {
  464.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  465.  
  466.                 alu.op = ALU_OP1_INTERP_LOAD_P0;
  467.  
  468.                 alu.dst.sel = ctx->shader->input[input].gpr;
  469.                 alu.dst.write = 1;
  470.  
  471.                 alu.dst.chan = i;
  472.  
  473.                 alu.src[0].sel = V_SQ_ALU_SRC_PARAM_BASE + ctx->shader->input[input].lds_pos;
  474.                 alu.src[0].chan = i;
  475.  
  476.                 if (i == 3)
  477.                         alu.last = 1;
  478.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  479.                 if (r)
  480.                         return r;
  481.         }
  482.         return 0;
  483. }
  484.  
  485. /*
  486.  * Special export handling in shaders
  487.  *
  488.  * shader export ARRAY_BASE for EXPORT_POS:
  489.  * 60 is position
  490.  * 61 is misc vector
  491.  * 62, 63 are clip distance vectors
  492.  *
  493.  * The use of the values exported in 61-63 are controlled by PA_CL_VS_OUT_CNTL:
  494.  * VS_OUT_MISC_VEC_ENA - enables the use of all fields in export 61
  495.  * USE_VTX_POINT_SIZE - point size in the X channel of export 61
  496.  * USE_VTX_EDGE_FLAG - edge flag in the Y channel of export 61
  497.  * USE_VTX_RENDER_TARGET_INDX - render target index in the Z channel of export 61
  498.  * USE_VTX_VIEWPORT_INDX - viewport index in the W channel of export 61
  499.  * USE_VTX_KILL_FLAG - kill flag in the Z channel of export 61 (mutually
  500.  * exclusive from render target index)
  501.  * VS_OUT_CCDIST0_VEC_ENA/VS_OUT_CCDIST1_VEC_ENA - enable clip distance vectors
  502.  *
  503.  *
  504.  * shader export ARRAY_BASE for EXPORT_PIXEL:
  505.  * 0-7 CB targets
  506.  * 61 computed Z vector
  507.  *
  508.  * The use of the values exported in the computed Z vector are controlled
  509.  * by DB_SHADER_CONTROL:
  510.  * Z_EXPORT_ENABLE - Z as a float in RED
  511.  * STENCIL_REF_EXPORT_ENABLE - stencil ref as int in GREEN
  512.  * COVERAGE_TO_MASK_ENABLE - alpha to mask in ALPHA
  513.  * MASK_EXPORT_ENABLE - pixel sample mask in BLUE
  514.  * DB_SOURCE_FORMAT - export control restrictions
  515.  *
  516.  */
  517.  
  518.  
  519. /* Map name/sid pair from tgsi to the 8-bit semantic index for SPI setup */
  520. static int r600_spi_sid(struct r600_shader_io * io)
  521. {
  522.         int index, name = io->name;
  523.  
  524.         /* These params are handled differently, they don't need
  525.          * semantic indices, so we'll use 0 for them.
  526.          */
  527.         if (name == TGSI_SEMANTIC_POSITION ||
  528.             name == TGSI_SEMANTIC_PSIZE ||
  529.             name == TGSI_SEMANTIC_EDGEFLAG ||
  530.             name == TGSI_SEMANTIC_FACE ||
  531.             name == TGSI_SEMANTIC_SAMPLEMASK)
  532.                 index = 0;
  533.         else {
  534.                 if (name == TGSI_SEMANTIC_GENERIC) {
  535.                         /* For generic params simply use sid from tgsi */
  536.                         index = io->sid;
  537.                 } else {
  538.                         /* For non-generic params - pack name and sid into 8 bits */
  539.                         index = 0x80 | (name<<3) | (io->sid);
  540.                 }
  541.  
  542.                 /* Make sure that all really used indices have nonzero value, so
  543.                  * we can just compare it to 0 later instead of comparing the name
  544.                  * with different values to detect special cases. */
  545.                 index++;
  546.         }
  547.  
  548.         return index;
  549. };
  550.  
  551. /* turn input into interpolate on EG */
  552. static int evergreen_interp_input(struct r600_shader_ctx *ctx, int index)
  553. {
  554.         int r = 0;
  555.  
  556.         if (ctx->shader->input[index].spi_sid) {
  557.                 ctx->shader->input[index].lds_pos = ctx->shader->nlds++;
  558.                 if (ctx->shader->input[index].interpolate > 0) {
  559.                         evergreen_interp_assign_ij_index(ctx, index);
  560.                         if (!ctx->use_llvm)
  561.                                 r = evergreen_interp_alu(ctx, index);
  562.                 } else {
  563.                         if (!ctx->use_llvm)
  564.                                 r = evergreen_interp_flat(ctx, index);
  565.                 }
  566.         }
  567.         return r;
  568. }
  569.  
  570. static int select_twoside_color(struct r600_shader_ctx *ctx, int front, int back)
  571. {
  572.         struct r600_bytecode_alu alu;
  573.         int i, r;
  574.         int gpr_front = ctx->shader->input[front].gpr;
  575.         int gpr_back = ctx->shader->input[back].gpr;
  576.  
  577.         for (i = 0; i < 4; i++) {
  578.                 memset(&alu, 0, sizeof(alu));
  579.                 alu.op = ALU_OP3_CNDGT;
  580.                 alu.is_op3 = 1;
  581.                 alu.dst.write = 1;
  582.                 alu.dst.sel = gpr_front;
  583.                 alu.src[0].sel = ctx->face_gpr;
  584.                 alu.src[1].sel = gpr_front;
  585.                 alu.src[2].sel = gpr_back;
  586.  
  587.                 alu.dst.chan = i;
  588.                 alu.src[1].chan = i;
  589.                 alu.src[2].chan = i;
  590.                 alu.last = (i==3);
  591.  
  592.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  593.                         return r;
  594.         }
  595.  
  596.         return 0;
  597. }
  598.  
  599. static int vs_add_primid_output(struct r600_shader_ctx *ctx, int prim_id_sid)
  600. {
  601.         int i;
  602.         i = ctx->shader->noutput++;
  603.         ctx->shader->output[i].name = TGSI_SEMANTIC_PRIMID;
  604.         ctx->shader->output[i].sid = 0;
  605.         ctx->shader->output[i].gpr = 0;
  606.         ctx->shader->output[i].interpolate = TGSI_INTERPOLATE_CONSTANT;
  607.         ctx->shader->output[i].write_mask = 0x4;
  608.         ctx->shader->output[i].spi_sid = prim_id_sid;
  609.  
  610.         return 0;
  611. }
  612.  
  613. static int tgsi_declaration(struct r600_shader_ctx *ctx)
  614. {
  615.         struct tgsi_full_declaration *d = &ctx->parse.FullToken.FullDeclaration;
  616.         int r, i, j, count = d->Range.Last - d->Range.First + 1;
  617.  
  618.         switch (d->Declaration.File) {
  619.         case TGSI_FILE_INPUT:
  620.                 i = ctx->shader->ninput;
  621.                 assert(i < Elements(ctx->shader->input));
  622.                 ctx->shader->ninput += count;
  623.                 ctx->shader->input[i].name = d->Semantic.Name;
  624.                 ctx->shader->input[i].sid = d->Semantic.Index;
  625.                 ctx->shader->input[i].interpolate = d->Interp.Interpolate;
  626.                 ctx->shader->input[i].interpolate_location = d->Interp.Location;
  627.                 ctx->shader->input[i].gpr = ctx->file_offset[TGSI_FILE_INPUT] + d->Range.First;
  628.                 if (ctx->type == TGSI_PROCESSOR_FRAGMENT) {
  629.                         ctx->shader->input[i].spi_sid = r600_spi_sid(&ctx->shader->input[i]);
  630.                         switch (ctx->shader->input[i].name) {
  631.                         case TGSI_SEMANTIC_FACE:
  632.                                 if (ctx->face_gpr != -1)
  633.                                         ctx->shader->input[i].gpr = ctx->face_gpr; /* already allocated by allocate_system_value_inputs */
  634.                                 else
  635.                                         ctx->face_gpr = ctx->shader->input[i].gpr;
  636.                                 break;
  637.                         case TGSI_SEMANTIC_COLOR:
  638.                                 ctx->colors_used++;
  639.                                 break;
  640.                         case TGSI_SEMANTIC_POSITION:
  641.                                 ctx->fragcoord_input = i;
  642.                                 break;
  643.                         case TGSI_SEMANTIC_PRIMID:
  644.                                 /* set this for now */
  645.                                 ctx->shader->gs_prim_id_input = true;
  646.                                 ctx->shader->ps_prim_id_input = i;
  647.                                 break;
  648.                         }
  649.                         if (ctx->bc->chip_class >= EVERGREEN) {
  650.                                 if ((r = evergreen_interp_input(ctx, i)))
  651.                                         return r;
  652.                         }
  653.                 } else if (ctx->type == TGSI_PROCESSOR_GEOMETRY) {
  654.                         /* FIXME probably skip inputs if they aren't passed in the ring */
  655.                         ctx->shader->input[i].ring_offset = ctx->next_ring_offset;
  656.                         ctx->next_ring_offset += 16;
  657.                         if (ctx->shader->input[i].name == TGSI_SEMANTIC_PRIMID)
  658.                                 ctx->shader->gs_prim_id_input = true;
  659.                 }
  660.                 for (j = 1; j < count; ++j) {
  661.                         ctx->shader->input[i + j] = ctx->shader->input[i];
  662.                         ctx->shader->input[i + j].gpr += j;
  663.                 }
  664.                 break;
  665.         case TGSI_FILE_OUTPUT:
  666.                 i = ctx->shader->noutput++;
  667.                 assert(i < Elements(ctx->shader->output));
  668.                 ctx->shader->output[i].name = d->Semantic.Name;
  669.                 ctx->shader->output[i].sid = d->Semantic.Index;
  670.                 ctx->shader->output[i].gpr = ctx->file_offset[TGSI_FILE_OUTPUT] + d->Range.First;
  671.                 ctx->shader->output[i].interpolate = d->Interp.Interpolate;
  672.                 ctx->shader->output[i].write_mask = d->Declaration.UsageMask;
  673.                 if (ctx->type == TGSI_PROCESSOR_VERTEX ||
  674.                                 ctx->type == TGSI_PROCESSOR_GEOMETRY) {
  675.                         ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);
  676.                         switch (d->Semantic.Name) {
  677.                         case TGSI_SEMANTIC_CLIPDIST:
  678.                                 ctx->shader->clip_dist_write |= d->Declaration.UsageMask << (d->Semantic.Index << 2);
  679.                                 break;
  680.                         case TGSI_SEMANTIC_PSIZE:
  681.                                 ctx->shader->vs_out_misc_write = 1;
  682.                                 ctx->shader->vs_out_point_size = 1;
  683.                                 break;
  684.                         case TGSI_SEMANTIC_EDGEFLAG:
  685.                                 ctx->shader->vs_out_misc_write = 1;
  686.                                 ctx->shader->vs_out_edgeflag = 1;
  687.                                 ctx->edgeflag_output = i;
  688.                                 break;
  689.                         case TGSI_SEMANTIC_VIEWPORT_INDEX:
  690.                                 ctx->shader->vs_out_misc_write = 1;
  691.                                 ctx->shader->vs_out_viewport = 1;
  692.                                 break;
  693.                         case TGSI_SEMANTIC_LAYER:
  694.                                 ctx->shader->vs_out_misc_write = 1;
  695.                                 ctx->shader->vs_out_layer = 1;
  696.                                 break;
  697.                         case TGSI_SEMANTIC_CLIPVERTEX:
  698.                                 ctx->clip_vertex_write = TRUE;
  699.                                 ctx->cv_output = i;
  700.                                 break;
  701.                         }
  702.                         if (ctx->type == TGSI_PROCESSOR_GEOMETRY) {
  703.                                 ctx->gs_out_ring_offset += 16;
  704.                         }
  705.                 } else if (ctx->type == TGSI_PROCESSOR_FRAGMENT) {
  706.                         switch (d->Semantic.Name) {
  707.                         case TGSI_SEMANTIC_COLOR:
  708.                                 ctx->shader->nr_ps_max_color_exports++;
  709.                                 break;
  710.                         }
  711.                 }
  712.                 break;
  713.         case TGSI_FILE_TEMPORARY:
  714.                 if (ctx->info.indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
  715.                         if (d->Array.ArrayID) {
  716.                                 r600_add_gpr_array(ctx->shader,
  717.                                                ctx->file_offset[TGSI_FILE_TEMPORARY] +
  718.                                                                    d->Range.First,
  719.                                                d->Range.Last - d->Range.First + 1, 0x0F);
  720.                         }
  721.                 }
  722.                 break;
  723.  
  724.         case TGSI_FILE_CONSTANT:
  725.         case TGSI_FILE_SAMPLER:
  726.         case TGSI_FILE_ADDRESS:
  727.                 break;
  728.  
  729.         case TGSI_FILE_SYSTEM_VALUE:
  730.                 if (d->Semantic.Name == TGSI_SEMANTIC_SAMPLEMASK ||
  731.                         d->Semantic.Name == TGSI_SEMANTIC_SAMPLEID ||
  732.                         d->Semantic.Name == TGSI_SEMANTIC_SAMPLEPOS) {
  733.                         break; /* Already handled from allocate_system_value_inputs */
  734.                 } else if (d->Semantic.Name == TGSI_SEMANTIC_INSTANCEID) {
  735.                         if (!ctx->native_integers) {
  736.                                 struct r600_bytecode_alu alu;
  737.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  738.  
  739.                                 alu.op = ALU_OP1_INT_TO_FLT;
  740.                                 alu.src[0].sel = 0;
  741.                                 alu.src[0].chan = 3;
  742.  
  743.                                 alu.dst.sel = 0;
  744.                                 alu.dst.chan = 3;
  745.                                 alu.dst.write = 1;
  746.                                 alu.last = 1;
  747.  
  748.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  749.                                         return r;
  750.                         }
  751.                         break;
  752.                 } else if (d->Semantic.Name == TGSI_SEMANTIC_VERTEXID)
  753.                         break;
  754.                 else if (d->Semantic.Name == TGSI_SEMANTIC_INVOCATIONID)
  755.                         break;
  756.         default:
  757.                 R600_ERR("unsupported file %d declaration\n", d->Declaration.File);
  758.                 return -EINVAL;
  759.         }
  760.         return 0;
  761. }
  762.  
  763. static int r600_get_temp(struct r600_shader_ctx *ctx)
  764. {
  765.         return ctx->temp_reg + ctx->max_driver_temp_used++;
  766. }
  767.  
  768. static int allocate_system_value_inputs(struct r600_shader_ctx *ctx, int gpr_offset)
  769. {
  770.         struct tgsi_parse_context parse;
  771.         struct {
  772.                 boolean enabled;
  773.                 int *reg;
  774.                 unsigned name, alternate_name;
  775.         } inputs[2] = {
  776.                 { false, &ctx->face_gpr, TGSI_SEMANTIC_SAMPLEMASK, ~0u }, /* lives in Front Face GPR.z */
  777.  
  778.                 { false, &ctx->fixed_pt_position_gpr, TGSI_SEMANTIC_SAMPLEID, TGSI_SEMANTIC_SAMPLEPOS } /* SAMPLEID is in Fixed Point Position GPR.w */
  779.         };
  780.         int i, k, num_regs = 0;
  781.  
  782.         if (tgsi_parse_init(&parse, ctx->tokens) != TGSI_PARSE_OK) {
  783.                 return 0;
  784.         }
  785.  
  786.         /* need to scan shader for system values and interpolateAtSample/Offset/Centroid */
  787.         while (!tgsi_parse_end_of_tokens(&parse)) {
  788.                 tgsi_parse_token(&parse);
  789.  
  790.                 if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION) {
  791.                         const struct tgsi_full_instruction *inst = &parse.FullToken.FullInstruction;
  792.                         if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE ||
  793.                                 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
  794.                                 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_CENTROID)
  795.                         {
  796.                                 int interpolate, location, k;
  797.  
  798.                                 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  799.                                         location = TGSI_INTERPOLATE_LOC_CENTER;
  800.                                         inputs[1].enabled = true; /* needs SAMPLEID */
  801.                                 } else if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET) {
  802.                                         location = TGSI_INTERPOLATE_LOC_CENTER;
  803.                                         /* Needs sample positions, currently those are always available */
  804.                                 } else {
  805.                                         location = TGSI_INTERPOLATE_LOC_CENTROID;
  806.                                 }
  807.  
  808.                                 interpolate = ctx->info.input_interpolate[inst->Src[0].Register.Index];
  809.                                 k = eg_get_interpolator_index(interpolate, location);
  810.                                 ctx->eg_interpolators[k].enabled = true;
  811.                         }
  812.                 } else if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
  813.                         struct tgsi_full_declaration *d = &parse.FullToken.FullDeclaration;
  814.                         if (d->Declaration.File == TGSI_FILE_SYSTEM_VALUE) {
  815.                                 for (k = 0; k < Elements(inputs); k++) {
  816.                                         if (d->Semantic.Name == inputs[k].name ||
  817.                                                 d->Semantic.Name == inputs[k].alternate_name) {
  818.                                                 inputs[k].enabled = true;
  819.                                         }
  820.                                 }
  821.                         }
  822.                 }
  823.         }
  824.  
  825.         tgsi_parse_free(&parse);
  826.  
  827.         for (i = 0; i < Elements(inputs); i++) {
  828.                 boolean enabled = inputs[i].enabled;
  829.                 int *reg = inputs[i].reg;
  830.                 unsigned name = inputs[i].name;
  831.  
  832.                 if (enabled) {
  833.                         int gpr = gpr_offset + num_regs++;
  834.  
  835.                         // add to inputs, allocate a gpr
  836.                         k = ctx->shader->ninput ++;
  837.                         ctx->shader->input[k].name = name;
  838.                         ctx->shader->input[k].sid = 0;
  839.                         ctx->shader->input[k].interpolate = TGSI_INTERPOLATE_CONSTANT;
  840.                         ctx->shader->input[k].interpolate_location = TGSI_INTERPOLATE_LOC_CENTER;
  841.                         *reg = ctx->shader->input[k].gpr = gpr;
  842.                 }
  843.         }
  844.  
  845.         return gpr_offset + num_regs;
  846. }
  847.  
  848. /*
  849.  * for evergreen we need to scan the shader to find the number of GPRs we need to
  850.  * reserve for interpolation and system values
  851.  *
  852.  * we need to know if we are going to emit
  853.  * any sample or centroid inputs
  854.  * if perspective and linear are required
  855. */
  856. static int evergreen_gpr_count(struct r600_shader_ctx *ctx)
  857. {
  858.         int i;
  859.         int num_baryc;
  860.         struct tgsi_parse_context parse;
  861.  
  862.         memset(&ctx->eg_interpolators, 0, sizeof(ctx->eg_interpolators));
  863.  
  864.         for (i = 0; i < ctx->info.num_inputs; i++) {
  865.                 int k;
  866.                 /* skip position/face/mask/sampleid */
  867.                 if (ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_POSITION ||
  868.                     ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_FACE ||
  869.                     ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_SAMPLEMASK ||
  870.                     ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_SAMPLEID)
  871.                         continue;
  872.  
  873.                 k = eg_get_interpolator_index(
  874.                         ctx->info.input_interpolate[i],
  875.                         ctx->info.input_interpolate_loc[i]);
  876.                 if (k >= 0)
  877.                         ctx->eg_interpolators[k].enabled = TRUE;
  878.         }
  879.  
  880.         if (tgsi_parse_init(&parse, ctx->tokens) != TGSI_PARSE_OK) {
  881.                 return 0;
  882.         }
  883.  
  884.         /* need to scan shader for system values and interpolateAtSample/Offset/Centroid */
  885.         while (!tgsi_parse_end_of_tokens(&parse)) {
  886.                 tgsi_parse_token(&parse);
  887.  
  888.                 if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION) {
  889.                         const struct tgsi_full_instruction *inst = &parse.FullToken.FullInstruction;
  890.                         if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE ||
  891.                                 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
  892.                                 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_CENTROID)
  893.                         {
  894.                                 int interpolate, location, k;
  895.  
  896.                                 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  897.                                         location = TGSI_INTERPOLATE_LOC_CENTER;
  898.                                 } else if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET) {
  899.                                         location = TGSI_INTERPOLATE_LOC_CENTER;
  900.                                 } else {
  901.                                         location = TGSI_INTERPOLATE_LOC_CENTROID;
  902.                                 }
  903.  
  904.                                 interpolate = ctx->info.input_interpolate[inst->Src[0].Register.Index];
  905.                                 k = eg_get_interpolator_index(interpolate, location);
  906.                                 ctx->eg_interpolators[k].enabled = true;
  907.                         }
  908.                 }
  909.         }
  910.  
  911.         tgsi_parse_free(&parse);
  912.  
  913.         /* assign gpr to each interpolator according to priority */
  914.         num_baryc = 0;
  915.         for (i = 0; i < Elements(ctx->eg_interpolators); i++) {
  916.                 if (ctx->eg_interpolators[i].enabled) {
  917.                         ctx->eg_interpolators[i].ij_index = num_baryc;
  918.                         num_baryc ++;
  919.                 }
  920.         }
  921.  
  922.         /* XXX PULL MODEL and LINE STIPPLE */
  923.  
  924.         num_baryc = (num_baryc + 1) >> 1;
  925.         return allocate_system_value_inputs(ctx, num_baryc);
  926. }
  927.  
  928. /* sample_id_sel == NULL means fetch for current sample */
  929. static int load_sample_position(struct r600_shader_ctx *ctx, struct r600_shader_src *sample_id, int chan_sel)
  930. {
  931.         struct r600_bytecode_vtx vtx;
  932.         int r, t1;
  933.  
  934.         assert(ctx->fixed_pt_position_gpr != -1);
  935.  
  936.         t1 = r600_get_temp(ctx);
  937.  
  938.         memset(&vtx, 0, sizeof(struct r600_bytecode_vtx));
  939.         vtx.op = FETCH_OP_VFETCH;
  940.         vtx.buffer_id = R600_SAMPLE_POSITIONS_CONST_BUFFER;
  941.         vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
  942.         if (sample_id == NULL) {
  943.                 vtx.src_gpr = ctx->fixed_pt_position_gpr; // SAMPLEID is in .w;
  944.                 vtx.src_sel_x = 3;
  945.         }
  946.         else {
  947.                 struct r600_bytecode_alu alu;
  948.  
  949.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  950.                 alu.op = ALU_OP1_MOV;
  951.                 r600_bytecode_src(&alu.src[0], sample_id, chan_sel);
  952.                 alu.dst.sel = t1;
  953.                 alu.dst.write = 1;
  954.                 alu.last = 1;
  955.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  956.                 if (r)
  957.                         return r;
  958.  
  959.                 vtx.src_gpr = t1;
  960.                 vtx.src_sel_x = 0;
  961.         }
  962.         vtx.mega_fetch_count = 16;
  963.         vtx.dst_gpr = t1;
  964.         vtx.dst_sel_x = 0;
  965.         vtx.dst_sel_y = 1;
  966.         vtx.dst_sel_z = 2;
  967.         vtx.dst_sel_w = 3;
  968.         vtx.data_format = FMT_32_32_32_32_FLOAT;
  969.         vtx.num_format_all = 2;
  970.         vtx.format_comp_all = 1;
  971.         vtx.use_const_fields = 0;
  972.         vtx.offset = 1; // first element is size of buffer
  973.         vtx.endian = r600_endian_swap(32);
  974.         vtx.srf_mode_all = 1; /* SRF_MODE_NO_ZERO */
  975.  
  976.         r = r600_bytecode_add_vtx(ctx->bc, &vtx);
  977.         if (r)
  978.                 return r;
  979.  
  980.         return t1;
  981. }
  982.  
  983. static void tgsi_src(struct r600_shader_ctx *ctx,
  984.                      const struct tgsi_full_src_register *tgsi_src,
  985.                      struct r600_shader_src *r600_src)
  986. {
  987.         memset(r600_src, 0, sizeof(*r600_src));
  988.         r600_src->swizzle[0] = tgsi_src->Register.SwizzleX;
  989.         r600_src->swizzle[1] = tgsi_src->Register.SwizzleY;
  990.         r600_src->swizzle[2] = tgsi_src->Register.SwizzleZ;
  991.         r600_src->swizzle[3] = tgsi_src->Register.SwizzleW;
  992.         r600_src->neg = tgsi_src->Register.Negate;
  993.         r600_src->abs = tgsi_src->Register.Absolute;
  994.  
  995.         if (tgsi_src->Register.File == TGSI_FILE_IMMEDIATE) {
  996.                 int index;
  997.                 if ((tgsi_src->Register.SwizzleX == tgsi_src->Register.SwizzleY) &&
  998.                         (tgsi_src->Register.SwizzleX == tgsi_src->Register.SwizzleZ) &&
  999.                         (tgsi_src->Register.SwizzleX == tgsi_src->Register.SwizzleW)) {
  1000.  
  1001.                         index = tgsi_src->Register.Index * 4 + tgsi_src->Register.SwizzleX;
  1002.                         r600_bytecode_special_constants(ctx->literals[index], &r600_src->sel, &r600_src->neg);
  1003.                         if (r600_src->sel != V_SQ_ALU_SRC_LITERAL)
  1004.                                 return;
  1005.                 }
  1006.                 index = tgsi_src->Register.Index;
  1007.                 r600_src->sel = V_SQ_ALU_SRC_LITERAL;
  1008.                 memcpy(r600_src->value, ctx->literals + index * 4, sizeof(r600_src->value));
  1009.         } else if (tgsi_src->Register.File == TGSI_FILE_SYSTEM_VALUE) {
  1010.                 if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_SAMPLEMASK) {
  1011.                         r600_src->swizzle[0] = 2; // Z value
  1012.                         r600_src->swizzle[1] = 2;
  1013.                         r600_src->swizzle[2] = 2;
  1014.                         r600_src->swizzle[3] = 2;
  1015.                         r600_src->sel = ctx->face_gpr;
  1016.                 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_SAMPLEID) {
  1017.                         r600_src->swizzle[0] = 3; // W value
  1018.                         r600_src->swizzle[1] = 3;
  1019.                         r600_src->swizzle[2] = 3;
  1020.                         r600_src->swizzle[3] = 3;
  1021.                         r600_src->sel = ctx->fixed_pt_position_gpr;
  1022.                 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_SAMPLEPOS) {
  1023.                         r600_src->swizzle[0] = 0;
  1024.                         r600_src->swizzle[1] = 1;
  1025.                         r600_src->swizzle[2] = 4;
  1026.                         r600_src->swizzle[3] = 4;
  1027.                         r600_src->sel = load_sample_position(ctx, NULL, -1);
  1028.                 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_INSTANCEID) {
  1029.                         r600_src->swizzle[0] = 3;
  1030.                         r600_src->swizzle[1] = 3;
  1031.                         r600_src->swizzle[2] = 3;
  1032.                         r600_src->swizzle[3] = 3;
  1033.                         r600_src->sel = 0;
  1034.                 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_VERTEXID) {
  1035.                         r600_src->swizzle[0] = 0;
  1036.                         r600_src->swizzle[1] = 0;
  1037.                         r600_src->swizzle[2] = 0;
  1038.                         r600_src->swizzle[3] = 0;
  1039.                         r600_src->sel = 0;
  1040.                 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_INVOCATIONID) {
  1041.                         r600_src->swizzle[0] = 3;
  1042.                         r600_src->swizzle[1] = 3;
  1043.                         r600_src->swizzle[2] = 3;
  1044.                         r600_src->swizzle[3] = 3;
  1045.                         r600_src->sel = 1;
  1046.                 }
  1047.         } else {
  1048.                 if (tgsi_src->Register.Indirect)
  1049.                         r600_src->rel = V_SQ_REL_RELATIVE;
  1050.                 r600_src->sel = tgsi_src->Register.Index;
  1051.                 r600_src->sel += ctx->file_offset[tgsi_src->Register.File];
  1052.         }
  1053.         if (tgsi_src->Register.File == TGSI_FILE_CONSTANT) {
  1054.                 if (tgsi_src->Register.Dimension) {
  1055.                         r600_src->kc_bank = tgsi_src->Dimension.Index;
  1056.                         if (tgsi_src->Dimension.Indirect) {
  1057.                                 r600_src->kc_rel = 1;
  1058.                         }
  1059.                 }
  1060.         }
  1061. }
  1062.  
  1063. static int tgsi_fetch_rel_const(struct r600_shader_ctx *ctx,
  1064.                                 unsigned int cb_idx, unsigned cb_rel, unsigned int offset, unsigned ar_chan,
  1065.                                 unsigned int dst_reg)
  1066. {
  1067.         struct r600_bytecode_vtx vtx;
  1068.         unsigned int ar_reg;
  1069.         int r;
  1070.  
  1071.         if (offset) {
  1072.                 struct r600_bytecode_alu alu;
  1073.  
  1074.                 memset(&alu, 0, sizeof(alu));
  1075.  
  1076.                 alu.op = ALU_OP2_ADD_INT;
  1077.                 alu.src[0].sel = ctx->bc->ar_reg;
  1078.                 alu.src[0].chan = ar_chan;
  1079.  
  1080.                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  1081.                 alu.src[1].value = offset;
  1082.  
  1083.                 alu.dst.sel = dst_reg;
  1084.                 alu.dst.chan = ar_chan;
  1085.                 alu.dst.write = 1;
  1086.                 alu.last = 1;
  1087.  
  1088.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  1089.                         return r;
  1090.  
  1091.                 ar_reg = dst_reg;
  1092.         } else {
  1093.                 ar_reg = ctx->bc->ar_reg;
  1094.         }
  1095.  
  1096.         memset(&vtx, 0, sizeof(vtx));
  1097.         vtx.buffer_id = cb_idx;
  1098.         vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
  1099.         vtx.src_gpr = ar_reg;
  1100.         vtx.src_sel_x = ar_chan;
  1101.         vtx.mega_fetch_count = 16;
  1102.         vtx.dst_gpr = dst_reg;
  1103.         vtx.dst_sel_x = 0;              /* SEL_X */
  1104.         vtx.dst_sel_y = 1;              /* SEL_Y */
  1105.         vtx.dst_sel_z = 2;              /* SEL_Z */
  1106.         vtx.dst_sel_w = 3;              /* SEL_W */
  1107.         vtx.data_format = FMT_32_32_32_32_FLOAT;
  1108.         vtx.num_format_all = 2;         /* NUM_FORMAT_SCALED */
  1109.         vtx.format_comp_all = 1;        /* FORMAT_COMP_SIGNED */
  1110.         vtx.endian = r600_endian_swap(32);
  1111.         vtx.buffer_index_mode = cb_rel; // cb_rel ? V_SQ_CF_INDEX_0 : V_SQ_CF_INDEX_NONE;
  1112.  
  1113.         if ((r = r600_bytecode_add_vtx(ctx->bc, &vtx)))
  1114.                 return r;
  1115.  
  1116.         return 0;
  1117. }
  1118.  
  1119. static int fetch_gs_input(struct r600_shader_ctx *ctx, struct tgsi_full_src_register *src, unsigned int dst_reg)
  1120. {
  1121.         struct r600_bytecode_vtx vtx;
  1122.         int r;
  1123.         unsigned index = src->Register.Index;
  1124.         unsigned vtx_id = src->Dimension.Index;
  1125.         int offset_reg = vtx_id / 3;
  1126.         int offset_chan = vtx_id % 3;
  1127.  
  1128.         /* offsets of per-vertex data in ESGS ring are passed to GS in R0.x, R0.y,
  1129.          * R0.w, R1.x, R1.y, R1.z (it seems R0.z is used for PrimitiveID) */
  1130.  
  1131.         if (offset_reg == 0 && offset_chan == 2)
  1132.                 offset_chan = 3;
  1133.  
  1134.         if (src->Dimension.Indirect) {
  1135.                 int treg[3];
  1136.                 int t2;
  1137.                 struct r600_bytecode_alu alu;
  1138.                 int r, i;
  1139.  
  1140.                 /* you have got to be shitting me -
  1141.                    we have to put the R0.x/y/w into Rt.x Rt+1.x Rt+2.x then index reg from Rt.
  1142.                    at least this is what fglrx seems to do. */
  1143.                 for (i = 0; i < 3; i++) {
  1144.                         treg[i] = r600_get_temp(ctx);
  1145.                 }
  1146.                 r600_add_gpr_array(ctx->shader, treg[0], 3, 0x0F);
  1147.  
  1148.                 t2 = r600_get_temp(ctx);
  1149.                 for (i = 0; i < 3; i++) {
  1150.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  1151.                         alu.op = ALU_OP1_MOV;
  1152.                         alu.src[0].sel = 0;
  1153.                         alu.src[0].chan = i == 2 ? 3 : i;
  1154.                         alu.dst.sel = treg[i];
  1155.                         alu.dst.chan = 0;
  1156.                         alu.dst.write = 1;
  1157.                         alu.last = 1;
  1158.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  1159.                         if (r)
  1160.                                 return r;
  1161.                 }
  1162.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  1163.                 alu.op = ALU_OP1_MOV;
  1164.                 alu.src[0].sel = treg[0];
  1165.                 alu.src[0].rel = 1;
  1166.                 alu.dst.sel = t2;
  1167.                 alu.dst.write = 1;
  1168.                 alu.last = 1;
  1169.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  1170.                 if (r)
  1171.                         return r;
  1172.                 offset_reg = t2;
  1173.         }
  1174.  
  1175.  
  1176.         memset(&vtx, 0, sizeof(vtx));
  1177.         vtx.buffer_id = R600_GS_RING_CONST_BUFFER;
  1178.         vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
  1179.         vtx.src_gpr = offset_reg;
  1180.         vtx.src_sel_x = offset_chan;
  1181.         vtx.offset = index * 16; /*bytes*/
  1182.         vtx.mega_fetch_count = 16;
  1183.         vtx.dst_gpr = dst_reg;
  1184.         vtx.dst_sel_x = 0;              /* SEL_X */
  1185.         vtx.dst_sel_y = 1;              /* SEL_Y */
  1186.         vtx.dst_sel_z = 2;              /* SEL_Z */
  1187.         vtx.dst_sel_w = 3;              /* SEL_W */
  1188.         if (ctx->bc->chip_class >= EVERGREEN) {
  1189.                 vtx.use_const_fields = 1;
  1190.         } else {
  1191.                 vtx.data_format = FMT_32_32_32_32_FLOAT;
  1192.         }
  1193.  
  1194.         if ((r = r600_bytecode_add_vtx(ctx->bc, &vtx)))
  1195.                 return r;
  1196.  
  1197.         return 0;
  1198. }
  1199.  
  1200. static int tgsi_split_gs_inputs(struct r600_shader_ctx *ctx)
  1201. {
  1202.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  1203.         int i;
  1204.  
  1205.         for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
  1206.                 struct tgsi_full_src_register *src = &inst->Src[i];
  1207.  
  1208.                 if (src->Register.File == TGSI_FILE_INPUT) {
  1209.                         if (ctx->shader->input[src->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
  1210.                                 /* primitive id is in R0.z */
  1211.                                 ctx->src[i].sel = 0;
  1212.                                 ctx->src[i].swizzle[0] = 2;
  1213.                         }
  1214.                 }
  1215.                 if (src->Register.File == TGSI_FILE_INPUT && src->Register.Dimension) {
  1216.                         int treg = r600_get_temp(ctx);
  1217.  
  1218.                         fetch_gs_input(ctx, src, treg);
  1219.                         ctx->src[i].sel = treg;
  1220.                 }
  1221.         }
  1222.         return 0;
  1223. }
  1224.  
  1225. static int tgsi_split_constant(struct r600_shader_ctx *ctx)
  1226. {
  1227.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  1228.         struct r600_bytecode_alu alu;
  1229.         int i, j, k, nconst, r;
  1230.  
  1231.         for (i = 0, nconst = 0; i < inst->Instruction.NumSrcRegs; i++) {
  1232.                 if (inst->Src[i].Register.File == TGSI_FILE_CONSTANT) {
  1233.                         nconst++;
  1234.                 }
  1235.                 tgsi_src(ctx, &inst->Src[i], &ctx->src[i]);
  1236.         }
  1237.         for (i = 0, j = nconst - 1; i < inst->Instruction.NumSrcRegs; i++) {
  1238.                 if (inst->Src[i].Register.File != TGSI_FILE_CONSTANT) {
  1239.                         continue;
  1240.                 }
  1241.  
  1242.                 if (ctx->src[i].kc_rel)
  1243.                         ctx->shader->uses_index_registers = true;
  1244.  
  1245.                 if (ctx->src[i].rel) {
  1246.                         int chan = inst->Src[i].Indirect.Swizzle;
  1247.                         int treg = r600_get_temp(ctx);
  1248.                         if ((r = tgsi_fetch_rel_const(ctx, ctx->src[i].kc_bank, ctx->src[i].kc_rel, ctx->src[i].sel - 512, chan, treg)))
  1249.                                 return r;
  1250.  
  1251.                         ctx->src[i].kc_bank = 0;
  1252.                         ctx->src[i].kc_rel = 0;
  1253.                         ctx->src[i].sel = treg;
  1254.                         ctx->src[i].rel = 0;
  1255.                         j--;
  1256.                 } else if (j > 0) {
  1257.                         int treg = r600_get_temp(ctx);
  1258.                         for (k = 0; k < 4; k++) {
  1259.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  1260.                                 alu.op = ALU_OP1_MOV;
  1261.                                 alu.src[0].sel = ctx->src[i].sel;
  1262.                                 alu.src[0].chan = k;
  1263.                                 alu.src[0].rel = ctx->src[i].rel;
  1264.                                 alu.src[0].kc_bank = ctx->src[i].kc_bank;
  1265.                                 alu.src[0].kc_rel = ctx->src[i].kc_rel;
  1266.                                 alu.dst.sel = treg;
  1267.                                 alu.dst.chan = k;
  1268.                                 alu.dst.write = 1;
  1269.                                 if (k == 3)
  1270.                                         alu.last = 1;
  1271.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  1272.                                 if (r)
  1273.                                         return r;
  1274.                         }
  1275.                         ctx->src[i].sel = treg;
  1276.                         ctx->src[i].rel =0;
  1277.                         j--;
  1278.                 }
  1279.         }
  1280.         return 0;
  1281. }
  1282.  
  1283. /* need to move any immediate into a temp - for trig functions which use literal for PI stuff */
  1284. static int tgsi_split_literal_constant(struct r600_shader_ctx *ctx)
  1285. {
  1286.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  1287.         struct r600_bytecode_alu alu;
  1288.         int i, j, k, nliteral, r;
  1289.  
  1290.         for (i = 0, nliteral = 0; i < inst->Instruction.NumSrcRegs; i++) {
  1291.                 if (ctx->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
  1292.                         nliteral++;
  1293.                 }
  1294.         }
  1295.         for (i = 0, j = nliteral - 1; i < inst->Instruction.NumSrcRegs; i++) {
  1296.                 if (j > 0 && ctx->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
  1297.                         int treg = r600_get_temp(ctx);
  1298.                         for (k = 0; k < 4; k++) {
  1299.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  1300.                                 alu.op = ALU_OP1_MOV;
  1301.                                 alu.src[0].sel = ctx->src[i].sel;
  1302.                                 alu.src[0].chan = k;
  1303.                                 alu.src[0].value = ctx->src[i].value[k];
  1304.                                 alu.dst.sel = treg;
  1305.                                 alu.dst.chan = k;
  1306.                                 alu.dst.write = 1;
  1307.                                 if (k == 3)
  1308.                                         alu.last = 1;
  1309.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  1310.                                 if (r)
  1311.                                         return r;
  1312.                         }
  1313.                         ctx->src[i].sel = treg;
  1314.                         j--;
  1315.                 }
  1316.         }
  1317.         return 0;
  1318. }
  1319.  
  1320. static int process_twoside_color_inputs(struct r600_shader_ctx *ctx)
  1321. {
  1322.         int i, r, count = ctx->shader->ninput;
  1323.  
  1324.         for (i = 0; i < count; i++) {
  1325.                 if (ctx->shader->input[i].name == TGSI_SEMANTIC_COLOR) {
  1326.                         r = select_twoside_color(ctx, i, ctx->shader->input[i].back_color_input);
  1327.                         if (r)
  1328.                                 return r;
  1329.                 }
  1330.         }
  1331.         return 0;
  1332. }
  1333.  
  1334. static int emit_streamout(struct r600_shader_ctx *ctx, struct pipe_stream_output_info *so)
  1335. {
  1336.         unsigned so_gpr[PIPE_MAX_SHADER_OUTPUTS];
  1337.         int i, j, r;
  1338.  
  1339.         /* Sanity checking. */
  1340.         if (so->num_outputs > PIPE_MAX_SHADER_OUTPUTS) {
  1341.                 R600_ERR("Too many stream outputs: %d\n", so->num_outputs);
  1342.                 r = -EINVAL;
  1343.                 goto out_err;
  1344.         }
  1345.         for (i = 0; i < so->num_outputs; i++) {
  1346.                 if (so->output[i].output_buffer >= 4) {
  1347.                         R600_ERR("Exceeded the max number of stream output buffers, got: %d\n",
  1348.                                  so->output[i].output_buffer);
  1349.                         r = -EINVAL;
  1350.                         goto out_err;
  1351.                 }
  1352.         }
  1353.  
  1354.         /* Initialize locations where the outputs are stored. */
  1355.         for (i = 0; i < so->num_outputs; i++) {
  1356.                 so_gpr[i] = ctx->shader->output[so->output[i].register_index].gpr;
  1357.  
  1358.                 /* Lower outputs with dst_offset < start_component.
  1359.                  *
  1360.                  * We can only output 4D vectors with a write mask, e.g. we can
  1361.                  * only output the W component at offset 3, etc. If we want
  1362.                  * to store Y, Z, or W at buffer offset 0, we need to use MOV
  1363.                  * to move it to X and output X. */
  1364.                 if (so->output[i].dst_offset < so->output[i].start_component) {
  1365.                         unsigned tmp = r600_get_temp(ctx);
  1366.  
  1367.                         for (j = 0; j < so->output[i].num_components; j++) {
  1368.                                 struct r600_bytecode_alu alu;
  1369.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  1370.                                 alu.op = ALU_OP1_MOV;
  1371.                                 alu.src[0].sel = so_gpr[i];
  1372.                                 alu.src[0].chan = so->output[i].start_component + j;
  1373.  
  1374.                                 alu.dst.sel = tmp;
  1375.                                 alu.dst.chan = j;
  1376.                                 alu.dst.write = 1;
  1377.                                 if (j == so->output[i].num_components - 1)
  1378.                                         alu.last = 1;
  1379.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  1380.                                 if (r)
  1381.                                         return r;
  1382.                         }
  1383.                         so->output[i].start_component = 0;
  1384.                         so_gpr[i] = tmp;
  1385.                 }
  1386.         }
  1387.  
  1388.         /* Write outputs to buffers. */
  1389.         for (i = 0; i < so->num_outputs; i++) {
  1390.                 struct r600_bytecode_output output;
  1391.  
  1392.                 memset(&output, 0, sizeof(struct r600_bytecode_output));
  1393.                 output.gpr = so_gpr[i];
  1394.                 output.elem_size = so->output[i].num_components;
  1395.                 output.array_base = so->output[i].dst_offset - so->output[i].start_component;
  1396.                 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE;
  1397.                 output.burst_count = 1;
  1398.                 /* array_size is an upper limit for the burst_count
  1399.                  * with MEM_STREAM instructions */
  1400.                 output.array_size = 0xFFF;
  1401.                 output.comp_mask = ((1 << so->output[i].num_components) - 1) << so->output[i].start_component;
  1402.                 if (ctx->bc->chip_class >= EVERGREEN) {
  1403.                         switch (so->output[i].output_buffer) {
  1404.                         case 0:
  1405.                                 output.op = CF_OP_MEM_STREAM0_BUF0;
  1406.                                 break;
  1407.                         case 1:
  1408.                                 output.op = CF_OP_MEM_STREAM0_BUF1;
  1409.                                 break;
  1410.                         case 2:
  1411.                                 output.op = CF_OP_MEM_STREAM0_BUF2;
  1412.                                 break;
  1413.                         case 3:
  1414.                                 output.op = CF_OP_MEM_STREAM0_BUF3;
  1415.                                 break;
  1416.                         }
  1417.                 } else {
  1418.                         switch (so->output[i].output_buffer) {
  1419.                         case 0:
  1420.                                 output.op = CF_OP_MEM_STREAM0;
  1421.                                 break;
  1422.                         case 1:
  1423.                                 output.op = CF_OP_MEM_STREAM1;
  1424.                                 break;
  1425.                         case 2:
  1426.                                 output.op = CF_OP_MEM_STREAM2;
  1427.                                 break;
  1428.                         case 3:
  1429.                                 output.op = CF_OP_MEM_STREAM3;
  1430.                                         break;
  1431.                         }
  1432.                 }
  1433.                 r = r600_bytecode_add_output(ctx->bc, &output);
  1434.                 if (r)
  1435.                         goto out_err;
  1436.         }
  1437.         return 0;
  1438. out_err:
  1439.         return r;
  1440. }
  1441.  
  1442. static void convert_edgeflag_to_int(struct r600_shader_ctx *ctx)
  1443. {
  1444.         struct r600_bytecode_alu alu;
  1445.         unsigned reg;
  1446.  
  1447.         if (!ctx->shader->vs_out_edgeflag)
  1448.                 return;
  1449.  
  1450.         reg = ctx->shader->output[ctx->edgeflag_output].gpr;
  1451.  
  1452.         /* clamp(x, 0, 1) */
  1453.         memset(&alu, 0, sizeof(alu));
  1454.         alu.op = ALU_OP1_MOV;
  1455.         alu.src[0].sel = reg;
  1456.         alu.dst.sel = reg;
  1457.         alu.dst.write = 1;
  1458.         alu.dst.clamp = 1;
  1459.         alu.last = 1;
  1460.         r600_bytecode_add_alu(ctx->bc, &alu);
  1461.  
  1462.         memset(&alu, 0, sizeof(alu));
  1463.         alu.op = ALU_OP1_FLT_TO_INT;
  1464.         alu.src[0].sel = reg;
  1465.         alu.dst.sel = reg;
  1466.         alu.dst.write = 1;
  1467.         alu.last = 1;
  1468.         r600_bytecode_add_alu(ctx->bc, &alu);
  1469. }
  1470.  
  1471. static int generate_gs_copy_shader(struct r600_context *rctx,
  1472.                                    struct r600_pipe_shader *gs,
  1473.                                    struct pipe_stream_output_info *so)
  1474. {
  1475.         struct r600_shader_ctx ctx = {};
  1476.         struct r600_shader *gs_shader = &gs->shader;
  1477.         struct r600_pipe_shader *cshader;
  1478.         int ocnt = gs_shader->noutput;
  1479.         struct r600_bytecode_alu alu;
  1480.         struct r600_bytecode_vtx vtx;
  1481.         struct r600_bytecode_output output;
  1482.         struct r600_bytecode_cf *cf_jump, *cf_pop,
  1483.                 *last_exp_pos = NULL, *last_exp_param = NULL;
  1484.         int i, next_clip_pos = 61, next_param = 0;
  1485.  
  1486.         cshader = calloc(1, sizeof(struct r600_pipe_shader));
  1487.         if (!cshader)
  1488.                 return 0;
  1489.  
  1490.         memcpy(cshader->shader.output, gs_shader->output, ocnt *
  1491.                sizeof(struct r600_shader_io));
  1492.  
  1493.         cshader->shader.noutput = ocnt;
  1494.  
  1495.         ctx.shader = &cshader->shader;
  1496.         ctx.bc = &ctx.shader->bc;
  1497.         ctx.type = ctx.bc->type = TGSI_PROCESSOR_VERTEX;
  1498.  
  1499.         r600_bytecode_init(ctx.bc, rctx->b.chip_class, rctx->b.family,
  1500.                            rctx->screen->has_compressed_msaa_texturing);
  1501.  
  1502.         ctx.bc->isa = rctx->isa;
  1503.  
  1504.         /* R0.x = R0.x & 0x3fffffff */
  1505.         memset(&alu, 0, sizeof(alu));
  1506.         alu.op = ALU_OP2_AND_INT;
  1507.         alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  1508.         alu.src[1].value = 0x3fffffff;
  1509.         alu.dst.write = 1;
  1510.         r600_bytecode_add_alu(ctx.bc, &alu);
  1511.  
  1512.         /* R0.y = R0.x >> 30 */
  1513.         memset(&alu, 0, sizeof(alu));
  1514.         alu.op = ALU_OP2_LSHR_INT;
  1515.         alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  1516.         alu.src[1].value = 0x1e;
  1517.         alu.dst.chan = 1;
  1518.         alu.dst.write = 1;
  1519.         alu.last = 1;
  1520.         r600_bytecode_add_alu(ctx.bc, &alu);
  1521.  
  1522.         /* PRED_SETE_INT __, R0.y, 0 */
  1523.         memset(&alu, 0, sizeof(alu));
  1524.         alu.op = ALU_OP2_PRED_SETE_INT;
  1525.         alu.src[0].chan = 1;
  1526.         alu.src[1].sel = V_SQ_ALU_SRC_0;
  1527.         alu.execute_mask = 1;
  1528.         alu.update_pred = 1;
  1529.         alu.last = 1;
  1530.         r600_bytecode_add_alu_type(ctx.bc, &alu, CF_OP_ALU_PUSH_BEFORE);
  1531.  
  1532.         r600_bytecode_add_cfinst(ctx.bc, CF_OP_JUMP);
  1533.         cf_jump = ctx.bc->cf_last;
  1534.  
  1535.         /* fetch vertex data from GSVS ring */
  1536.         for (i = 0; i < ocnt; ++i) {
  1537.                 struct r600_shader_io *out = &ctx.shader->output[i];
  1538.                 out->gpr = i + 1;
  1539.                 out->ring_offset = i * 16;
  1540.  
  1541.                 memset(&vtx, 0, sizeof(vtx));
  1542.                 vtx.op = FETCH_OP_VFETCH;
  1543.                 vtx.buffer_id = R600_GS_RING_CONST_BUFFER;
  1544.                 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
  1545.                 vtx.offset = out->ring_offset;
  1546.                 vtx.dst_gpr = out->gpr;
  1547.                 vtx.dst_sel_x = 0;
  1548.                 vtx.dst_sel_y = 1;
  1549.                 vtx.dst_sel_z = 2;
  1550.                 vtx.dst_sel_w = 3;
  1551.                 if (rctx->b.chip_class >= EVERGREEN) {
  1552.                         vtx.use_const_fields = 1;
  1553.                 } else {
  1554.                         vtx.data_format = FMT_32_32_32_32_FLOAT;
  1555.                 }
  1556.  
  1557.                 r600_bytecode_add_vtx(ctx.bc, &vtx);
  1558.         }
  1559.  
  1560.         /* XXX handle clipvertex, streamout? */
  1561.         emit_streamout(&ctx, so);
  1562.  
  1563.         /* export vertex data */
  1564.         /* XXX factor out common code with r600_shader_from_tgsi ? */
  1565.         for (i = 0; i < ocnt; ++i) {
  1566.                 struct r600_shader_io *out = &ctx.shader->output[i];
  1567.  
  1568.                 if (out->name == TGSI_SEMANTIC_CLIPVERTEX)
  1569.                         continue;
  1570.  
  1571.                 memset(&output, 0, sizeof(output));
  1572.                 output.gpr = out->gpr;
  1573.                 output.elem_size = 3;
  1574.                 output.swizzle_x = 0;
  1575.                 output.swizzle_y = 1;
  1576.                 output.swizzle_z = 2;
  1577.                 output.swizzle_w = 3;
  1578.                 output.burst_count = 1;
  1579.                 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
  1580.                 output.op = CF_OP_EXPORT;
  1581.                 switch (out->name) {
  1582.                 case TGSI_SEMANTIC_POSITION:
  1583.                         output.array_base = 60;
  1584.                         output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  1585.                         break;
  1586.  
  1587.                 case TGSI_SEMANTIC_PSIZE:
  1588.                         output.array_base = 61;
  1589.                         if (next_clip_pos == 61)
  1590.                                 next_clip_pos = 62;
  1591.                         output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  1592.                         output.swizzle_y = 7;
  1593.                         output.swizzle_z = 7;
  1594.                         output.swizzle_w = 7;
  1595.                         ctx.shader->vs_out_misc_write = 1;
  1596.                         ctx.shader->vs_out_point_size = 1;
  1597.                         break;
  1598.                 case TGSI_SEMANTIC_LAYER:
  1599.                         if (out->spi_sid) {
  1600.                                 /* duplicate it as PARAM to pass to the pixel shader */
  1601.                                 output.array_base = next_param++;
  1602.                                 r600_bytecode_add_output(ctx.bc, &output);
  1603.                                 last_exp_param = ctx.bc->cf_last;
  1604.                         }
  1605.                         output.array_base = 61;
  1606.                         if (next_clip_pos == 61)
  1607.                                 next_clip_pos = 62;
  1608.                         output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  1609.                         output.swizzle_x = 7;
  1610.                         output.swizzle_y = 7;
  1611.                         output.swizzle_z = 0;
  1612.                         output.swizzle_w = 7;
  1613.                         ctx.shader->vs_out_misc_write = 1;
  1614.                         ctx.shader->vs_out_layer = 1;
  1615.                         break;
  1616.                 case TGSI_SEMANTIC_VIEWPORT_INDEX:
  1617.                         if (out->spi_sid) {
  1618.                                 /* duplicate it as PARAM to pass to the pixel shader */
  1619.                                 output.array_base = next_param++;
  1620.                                 r600_bytecode_add_output(ctx.bc, &output);
  1621.                                 last_exp_param = ctx.bc->cf_last;
  1622.                         }
  1623.                         output.array_base = 61;
  1624.                         if (next_clip_pos == 61)
  1625.                                 next_clip_pos = 62;
  1626.                         output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  1627.                         ctx.shader->vs_out_misc_write = 1;
  1628.                         ctx.shader->vs_out_viewport = 1;
  1629.                         output.swizzle_x = 7;
  1630.                         output.swizzle_y = 7;
  1631.                         output.swizzle_z = 7;
  1632.                         output.swizzle_w = 0;
  1633.                         break;
  1634.                 case TGSI_SEMANTIC_CLIPDIST:
  1635.                         /* spi_sid is 0 for clipdistance outputs that were generated
  1636.                          * for clipvertex - we don't need to pass them to PS */
  1637.                         ctx.shader->clip_dist_write = gs->shader.clip_dist_write;
  1638.                         if (out->spi_sid) {
  1639.                                 /* duplicate it as PARAM to pass to the pixel shader */
  1640.                                 output.array_base = next_param++;
  1641.                                 r600_bytecode_add_output(ctx.bc, &output);
  1642.                                 last_exp_param = ctx.bc->cf_last;
  1643.                         }
  1644.                         output.array_base = next_clip_pos++;
  1645.                         output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  1646.                         break;
  1647.                 case TGSI_SEMANTIC_FOG:
  1648.                         output.swizzle_y = 4; /* 0 */
  1649.                         output.swizzle_z = 4; /* 0 */
  1650.                         output.swizzle_w = 5; /* 1 */
  1651.                         break;
  1652.                 default:
  1653.                         output.array_base = next_param++;
  1654.                         break;
  1655.                 }
  1656.                 r600_bytecode_add_output(ctx.bc, &output);
  1657.                 if (output.type == V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM)
  1658.                         last_exp_param = ctx.bc->cf_last;
  1659.                 else
  1660.                         last_exp_pos = ctx.bc->cf_last;
  1661.         }
  1662.  
  1663.         if (!last_exp_pos) {
  1664.                 memset(&output, 0, sizeof(output));
  1665.                 output.gpr = 0;
  1666.                 output.elem_size = 3;
  1667.                 output.swizzle_x = 7;
  1668.                 output.swizzle_y = 7;
  1669.                 output.swizzle_z = 7;
  1670.                 output.swizzle_w = 7;
  1671.                 output.burst_count = 1;
  1672.                 output.type = 2;
  1673.                 output.op = CF_OP_EXPORT;
  1674.                 output.array_base = 60;
  1675.                 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  1676.                 r600_bytecode_add_output(ctx.bc, &output);
  1677.                 last_exp_pos = ctx.bc->cf_last;
  1678.         }
  1679.  
  1680.         if (!last_exp_param) {
  1681.                 memset(&output, 0, sizeof(output));
  1682.                 output.gpr = 0;
  1683.                 output.elem_size = 3;
  1684.                 output.swizzle_x = 7;
  1685.                 output.swizzle_y = 7;
  1686.                 output.swizzle_z = 7;
  1687.                 output.swizzle_w = 7;
  1688.                 output.burst_count = 1;
  1689.                 output.type = 2;
  1690.                 output.op = CF_OP_EXPORT;
  1691.                 output.array_base = next_param++;
  1692.                 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
  1693.                 r600_bytecode_add_output(ctx.bc, &output);
  1694.                 last_exp_param = ctx.bc->cf_last;
  1695.         }
  1696.  
  1697.         last_exp_pos->op = CF_OP_EXPORT_DONE;
  1698.         last_exp_param->op = CF_OP_EXPORT_DONE;
  1699.  
  1700.         r600_bytecode_add_cfinst(ctx.bc, CF_OP_POP);
  1701.         cf_pop = ctx.bc->cf_last;
  1702.  
  1703.         cf_jump->cf_addr = cf_pop->id + 2;
  1704.         cf_jump->pop_count = 1;
  1705.         cf_pop->cf_addr = cf_pop->id + 2;
  1706.         cf_pop->pop_count = 1;
  1707.  
  1708.         if (ctx.bc->chip_class == CAYMAN)
  1709.                 cm_bytecode_add_cf_end(ctx.bc);
  1710.         else {
  1711.                 r600_bytecode_add_cfinst(ctx.bc, CF_OP_NOP);
  1712.                 ctx.bc->cf_last->end_of_program = 1;
  1713.         }
  1714.  
  1715.         gs->gs_copy_shader = cshader;
  1716.  
  1717.         ctx.bc->nstack = 1;
  1718.         cshader->shader.ring_item_size = ocnt * 16;
  1719.  
  1720.         return r600_bytecode_build(ctx.bc);
  1721. }
  1722.  
  1723. static int emit_gs_ring_writes(struct r600_shader_ctx *ctx, bool ind)
  1724. {
  1725.         struct r600_bytecode_output output;
  1726.         int i, k, ring_offset;
  1727.  
  1728.         for (i = 0; i < ctx->shader->noutput; i++) {
  1729.                 if (ctx->gs_for_vs) {
  1730.                         /* for ES we need to lookup corresponding ring offset expected by GS
  1731.                          * (map this output to GS input by name and sid) */
  1732.                         /* FIXME precompute offsets */
  1733.                         ring_offset = -1;
  1734.                         for(k = 0; k < ctx->gs_for_vs->ninput; ++k) {
  1735.                                 struct r600_shader_io *in = &ctx->gs_for_vs->input[k];
  1736.                                 struct r600_shader_io *out = &ctx->shader->output[i];
  1737.                                 if (in->name == out->name && in->sid == out->sid)
  1738.                                         ring_offset = in->ring_offset;
  1739.                         }
  1740.  
  1741.                         if (ring_offset == -1)
  1742.                                 continue;
  1743.                 } else
  1744.                         ring_offset = i * 16;
  1745.  
  1746.                 /* next_ring_offset after parsing input decls contains total size of
  1747.                  * single vertex data, gs_next_vertex - current vertex index */
  1748.                 if (!ind)
  1749.                         ring_offset += ctx->gs_out_ring_offset * ctx->gs_next_vertex;
  1750.  
  1751.                 /* get a temp and add the ring offset to the next vertex base in the shader */
  1752.                 memset(&output, 0, sizeof(struct r600_bytecode_output));
  1753.                 output.gpr = ctx->shader->output[i].gpr;
  1754.                 output.elem_size = 3;
  1755.                 output.comp_mask = 0xF;
  1756.                 output.burst_count = 1;
  1757.  
  1758.                 if (ind)
  1759.                         output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE_IND;
  1760.                 else
  1761.                         output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE;
  1762.                 output.op = CF_OP_MEM_RING;
  1763.  
  1764.  
  1765.                 if (ind) {
  1766.                         output.array_base = ring_offset >> 2; /* in dwords */
  1767.                         output.array_size = 0xfff;
  1768.                         output.index_gpr = ctx->gs_export_gpr_treg;
  1769.                 } else
  1770.                         output.array_base = ring_offset >> 2; /* in dwords */
  1771.                 r600_bytecode_add_output(ctx->bc, &output);
  1772.         }
  1773.  
  1774.         if (ind) {
  1775.                 struct r600_bytecode_alu alu;
  1776.                 int r;
  1777.  
  1778.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  1779.                 alu.op = ALU_OP2_ADD_INT;
  1780.                 alu.src[0].sel = ctx->gs_export_gpr_treg;
  1781.                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  1782.                 alu.src[1].value = ctx->gs_out_ring_offset >> 4;
  1783.                 alu.dst.sel = ctx->gs_export_gpr_treg;
  1784.                 alu.dst.write = 1;
  1785.                 alu.last = 1;
  1786.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  1787.                 if (r)
  1788.                         return r;
  1789.         }
  1790.         ++ctx->gs_next_vertex;
  1791.         return 0;
  1792. }
  1793.  
  1794. static int r600_shader_from_tgsi(struct r600_context *rctx,
  1795.                                  struct r600_pipe_shader *pipeshader,
  1796.                                  struct r600_shader_key key)
  1797. {
  1798.         struct r600_screen *rscreen = rctx->screen;
  1799.         struct r600_shader *shader = &pipeshader->shader;
  1800.         struct tgsi_token *tokens = pipeshader->selector->tokens;
  1801.         struct pipe_stream_output_info so = pipeshader->selector->so;
  1802.         struct tgsi_full_immediate *immediate;
  1803.         struct tgsi_full_property *property;
  1804.         struct r600_shader_ctx ctx;
  1805.         struct r600_bytecode_output output[32];
  1806.         unsigned output_done, noutput;
  1807.         unsigned opcode;
  1808.         int i, j, k, r = 0;
  1809.         int next_param_base = 0, next_clip_base;
  1810.         int max_color_exports = MAX2(key.nr_cbufs, 1);
  1811.         /* Declarations used by llvm code */
  1812.         bool use_llvm = false;
  1813.         bool indirect_gprs;
  1814.         bool ring_outputs = false;
  1815.         bool pos_emitted = false;
  1816.  
  1817. #ifdef R600_USE_LLVM
  1818.         use_llvm = rscreen->b.debug_flags & DBG_LLVM;
  1819. #endif
  1820.         ctx.bc = &shader->bc;
  1821.         ctx.shader = shader;
  1822.         ctx.native_integers = true;
  1823.  
  1824.         shader->vs_as_gs_a = key.vs_as_gs_a;
  1825.         shader->vs_as_es = key.vs_as_es;
  1826.  
  1827.         r600_bytecode_init(ctx.bc, rscreen->b.chip_class, rscreen->b.family,
  1828.                            rscreen->has_compressed_msaa_texturing);
  1829.         ctx.tokens = tokens;
  1830.         tgsi_scan_shader(tokens, &ctx.info);
  1831.         shader->indirect_files = ctx.info.indirect_files;
  1832.         indirect_gprs = ctx.info.indirect_files & ~(1 << TGSI_FILE_CONSTANT);
  1833.         tgsi_parse_init(&ctx.parse, tokens);
  1834.         ctx.type = ctx.parse.FullHeader.Processor.Processor;
  1835.         shader->processor_type = ctx.type;
  1836.         ctx.bc->type = shader->processor_type;
  1837.  
  1838.         ring_outputs = key.vs_as_es || (ctx.type == TGSI_PROCESSOR_GEOMETRY);
  1839.  
  1840.         if (key.vs_as_es) {
  1841.                 ctx.gs_for_vs = &rctx->gs_shader->current->shader;
  1842.         } else {
  1843.                 ctx.gs_for_vs = NULL;
  1844.         }
  1845.  
  1846.         ctx.next_ring_offset = 0;
  1847.         ctx.gs_out_ring_offset = 0;
  1848.         ctx.gs_next_vertex = 0;
  1849.  
  1850.         shader->uses_index_registers = false;
  1851.         ctx.face_gpr = -1;
  1852.         ctx.fixed_pt_position_gpr = -1;
  1853.         ctx.fragcoord_input = -1;
  1854.         ctx.colors_used = 0;
  1855.         ctx.clip_vertex_write = 0;
  1856.  
  1857.         shader->nr_ps_color_exports = 0;
  1858.         shader->nr_ps_max_color_exports = 0;
  1859.  
  1860.         shader->two_side = key.color_two_side;
  1861.  
  1862.         /* register allocations */
  1863.         /* Values [0,127] correspond to GPR[0..127].
  1864.          * Values [128,159] correspond to constant buffer bank 0
  1865.          * Values [160,191] correspond to constant buffer bank 1
  1866.          * Values [256,511] correspond to cfile constants c[0..255]. (Gone on EG)
  1867.          * Values [256,287] correspond to constant buffer bank 2 (EG)
  1868.          * Values [288,319] correspond to constant buffer bank 3 (EG)
  1869.          * Other special values are shown in the list below.
  1870.          * 244  ALU_SRC_1_DBL_L: special constant 1.0 double-float, LSW. (RV670+)
  1871.          * 245  ALU_SRC_1_DBL_M: special constant 1.0 double-float, MSW. (RV670+)
  1872.          * 246  ALU_SRC_0_5_DBL_L: special constant 0.5 double-float, LSW. (RV670+)
  1873.          * 247  ALU_SRC_0_5_DBL_M: special constant 0.5 double-float, MSW. (RV670+)
  1874.          * 248  SQ_ALU_SRC_0: special constant 0.0.
  1875.          * 249  SQ_ALU_SRC_1: special constant 1.0 float.
  1876.          * 250  SQ_ALU_SRC_1_INT: special constant 1 integer.
  1877.          * 251  SQ_ALU_SRC_M_1_INT: special constant -1 integer.
  1878.          * 252  SQ_ALU_SRC_0_5: special constant 0.5 float.
  1879.          * 253  SQ_ALU_SRC_LITERAL: literal constant.
  1880.          * 254  SQ_ALU_SRC_PV: previous vector result.
  1881.          * 255  SQ_ALU_SRC_PS: previous scalar result.
  1882.          */
  1883.         for (i = 0; i < TGSI_FILE_COUNT; i++) {
  1884.                 ctx.file_offset[i] = 0;
  1885.         }
  1886.  
  1887. #ifdef R600_USE_LLVM
  1888.         if (use_llvm && ctx.info.indirect_files && (ctx.info.indirect_files & (1 << TGSI_FILE_CONSTANT)) != ctx.info.indirect_files) {
  1889.                 fprintf(stderr, "Warning: R600 LLVM backend does not support "
  1890.                                 "indirect adressing.  Falling back to TGSI "
  1891.                                 "backend.\n");
  1892.                 use_llvm = 0;
  1893.         }
  1894. #endif
  1895.         if (ctx.type == TGSI_PROCESSOR_VERTEX) {
  1896.                 ctx.file_offset[TGSI_FILE_INPUT] = 1;
  1897.                 if (!use_llvm) {
  1898.                         r600_bytecode_add_cfinst(ctx.bc, CF_OP_CALL_FS);
  1899.                 }
  1900.         }
  1901.         if (ctx.type == TGSI_PROCESSOR_FRAGMENT) {
  1902.                 if (ctx.bc->chip_class >= EVERGREEN)
  1903.                         ctx.file_offset[TGSI_FILE_INPUT] = evergreen_gpr_count(&ctx);
  1904.                 else
  1905.                         ctx.file_offset[TGSI_FILE_INPUT] = allocate_system_value_inputs(&ctx, ctx.file_offset[TGSI_FILE_INPUT]);
  1906.         }
  1907.         if (ctx.type == TGSI_PROCESSOR_GEOMETRY) {
  1908.                 /* FIXME 1 would be enough in some cases (3 or less input vertices) */
  1909.                 ctx.file_offset[TGSI_FILE_INPUT] = 2;
  1910.         }
  1911.         ctx.use_llvm = use_llvm;
  1912.  
  1913.         if (use_llvm) {
  1914.                 ctx.file_offset[TGSI_FILE_OUTPUT] =
  1915.                         ctx.file_offset[TGSI_FILE_INPUT];
  1916.         } else {
  1917.            ctx.file_offset[TGSI_FILE_OUTPUT] =
  1918.                         ctx.file_offset[TGSI_FILE_INPUT] +
  1919.                         ctx.info.file_max[TGSI_FILE_INPUT] + 1;
  1920.         }
  1921.         ctx.file_offset[TGSI_FILE_TEMPORARY] = ctx.file_offset[TGSI_FILE_OUTPUT] +
  1922.                                                 ctx.info.file_max[TGSI_FILE_OUTPUT] + 1;
  1923.  
  1924.         /* Outside the GPR range. This will be translated to one of the
  1925.          * kcache banks later. */
  1926.         ctx.file_offset[TGSI_FILE_CONSTANT] = 512;
  1927.  
  1928.         ctx.file_offset[TGSI_FILE_IMMEDIATE] = V_SQ_ALU_SRC_LITERAL;
  1929.         ctx.bc->ar_reg = ctx.file_offset[TGSI_FILE_TEMPORARY] +
  1930.                         ctx.info.file_max[TGSI_FILE_TEMPORARY] + 1;
  1931.         if (ctx.type == TGSI_PROCESSOR_GEOMETRY) {
  1932.                 ctx.gs_export_gpr_treg = ctx.bc->ar_reg + 1;
  1933.                 ctx.temp_reg = ctx.bc->ar_reg + 2;
  1934.                 ctx.bc->index_reg[0] = ctx.bc->ar_reg + 3;
  1935.                 ctx.bc->index_reg[1] = ctx.bc->ar_reg + 4;
  1936.         } else {
  1937.                 ctx.temp_reg = ctx.bc->ar_reg + 1;
  1938.                 ctx.bc->index_reg[0] = ctx.bc->ar_reg + 2;
  1939.                 ctx.bc->index_reg[1] = ctx.bc->ar_reg + 3;
  1940.         }
  1941.  
  1942.         shader->max_arrays = 0;
  1943.         shader->num_arrays = 0;
  1944.         if (indirect_gprs) {
  1945.  
  1946.                 if (ctx.info.indirect_files & (1 << TGSI_FILE_INPUT)) {
  1947.                         r600_add_gpr_array(shader, ctx.file_offset[TGSI_FILE_INPUT],
  1948.                                            ctx.file_offset[TGSI_FILE_OUTPUT] -
  1949.                                            ctx.file_offset[TGSI_FILE_INPUT],
  1950.                                            0x0F);
  1951.                 }
  1952.                 if (ctx.info.indirect_files & (1 << TGSI_FILE_OUTPUT)) {
  1953.                         r600_add_gpr_array(shader, ctx.file_offset[TGSI_FILE_OUTPUT],
  1954.                                            ctx.file_offset[TGSI_FILE_TEMPORARY] -
  1955.                                            ctx.file_offset[TGSI_FILE_OUTPUT],
  1956.                                            0x0F);
  1957.                 }
  1958.         }
  1959.  
  1960.         ctx.nliterals = 0;
  1961.         ctx.literals = NULL;
  1962.         shader->fs_write_all = FALSE;
  1963.  
  1964.         if (shader->vs_as_gs_a)
  1965.                 vs_add_primid_output(&ctx, key.vs_prim_id_out);
  1966.  
  1967.         while (!tgsi_parse_end_of_tokens(&ctx.parse)) {
  1968.                 tgsi_parse_token(&ctx.parse);
  1969.                 switch (ctx.parse.FullToken.Token.Type) {
  1970.                 case TGSI_TOKEN_TYPE_IMMEDIATE:
  1971.                         immediate = &ctx.parse.FullToken.FullImmediate;
  1972.                         ctx.literals = realloc(ctx.literals, (ctx.nliterals + 1) * 16);
  1973.                         if(ctx.literals == NULL) {
  1974.                                 r = -ENOMEM;
  1975.                                 goto out_err;
  1976.                         }
  1977.                         ctx.literals[ctx.nliterals * 4 + 0] = immediate->u[0].Uint;
  1978.                         ctx.literals[ctx.nliterals * 4 + 1] = immediate->u[1].Uint;
  1979.                         ctx.literals[ctx.nliterals * 4 + 2] = immediate->u[2].Uint;
  1980.                         ctx.literals[ctx.nliterals * 4 + 3] = immediate->u[3].Uint;
  1981.                         ctx.nliterals++;
  1982.                         break;
  1983.                 case TGSI_TOKEN_TYPE_DECLARATION:
  1984.                         r = tgsi_declaration(&ctx);
  1985.                         if (r)
  1986.                                 goto out_err;
  1987.                         break;
  1988.                 case TGSI_TOKEN_TYPE_INSTRUCTION:
  1989.                         break;
  1990.                 case TGSI_TOKEN_TYPE_PROPERTY:
  1991.                         property = &ctx.parse.FullToken.FullProperty;
  1992.                         switch (property->Property.PropertyName) {
  1993.                         case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS:
  1994.                                 if (property->u[0].Data == 1)
  1995.                                         shader->fs_write_all = TRUE;
  1996.                                 break;
  1997.                         case TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION:
  1998.                                 if (property->u[0].Data == 1)
  1999.                                         shader->vs_position_window_space = TRUE;
  2000.                                 break;
  2001.                         case TGSI_PROPERTY_VS_PROHIBIT_UCPS:
  2002.                                 /* we don't need this one */
  2003.                                 break;
  2004.                         case TGSI_PROPERTY_GS_INPUT_PRIM:
  2005.                                 shader->gs_input_prim = property->u[0].Data;
  2006.                                 break;
  2007.                         case TGSI_PROPERTY_GS_OUTPUT_PRIM:
  2008.                                 shader->gs_output_prim = property->u[0].Data;
  2009.                                 break;
  2010.                         case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
  2011.                                 shader->gs_max_out_vertices = property->u[0].Data;
  2012.                                 break;
  2013.                         case TGSI_PROPERTY_GS_INVOCATIONS:
  2014.                                 shader->gs_num_invocations = property->u[0].Data;
  2015.                                 break;
  2016.                         }
  2017.                         break;
  2018.                 default:
  2019.                         R600_ERR("unsupported token type %d\n", ctx.parse.FullToken.Token.Type);
  2020.                         r = -EINVAL;
  2021.                         goto out_err;
  2022.                 }
  2023.         }
  2024.        
  2025.         shader->ring_item_size = ctx.next_ring_offset;
  2026.  
  2027.         /* Process two side if needed */
  2028.         if (shader->two_side && ctx.colors_used) {
  2029.                 int i, count = ctx.shader->ninput;
  2030.                 unsigned next_lds_loc = ctx.shader->nlds;
  2031.  
  2032.                 /* additional inputs will be allocated right after the existing inputs,
  2033.                  * we won't need them after the color selection, so we don't need to
  2034.                  * reserve these gprs for the rest of the shader code and to adjust
  2035.                  * output offsets etc. */
  2036.                 int gpr = ctx.file_offset[TGSI_FILE_INPUT] +
  2037.                                 ctx.info.file_max[TGSI_FILE_INPUT] + 1;
  2038.  
  2039.                 /* if two sided and neither face or sample mask is used by shader, ensure face_gpr is emitted */
  2040.                 if (ctx.face_gpr == -1) {
  2041.                         i = ctx.shader->ninput++;
  2042.                         ctx.shader->input[i].name = TGSI_SEMANTIC_FACE;
  2043.                         ctx.shader->input[i].spi_sid = 0;
  2044.                         ctx.shader->input[i].gpr = gpr++;
  2045.                         ctx.face_gpr = ctx.shader->input[i].gpr;
  2046.                 }
  2047.  
  2048.                 for (i = 0; i < count; i++) {
  2049.                         if (ctx.shader->input[i].name == TGSI_SEMANTIC_COLOR) {
  2050.                                 int ni = ctx.shader->ninput++;
  2051.                                 memcpy(&ctx.shader->input[ni],&ctx.shader->input[i], sizeof(struct r600_shader_io));
  2052.                                 ctx.shader->input[ni].name = TGSI_SEMANTIC_BCOLOR;
  2053.                                 ctx.shader->input[ni].spi_sid = r600_spi_sid(&ctx.shader->input[ni]);
  2054.                                 ctx.shader->input[ni].gpr = gpr++;
  2055.                                 // TGSI to LLVM needs to know the lds position of inputs.
  2056.                                 // Non LLVM path computes it later (in process_twoside_color)
  2057.                                 ctx.shader->input[ni].lds_pos = next_lds_loc++;
  2058.                                 ctx.shader->input[i].back_color_input = ni;
  2059.                                 if (ctx.bc->chip_class >= EVERGREEN) {
  2060.                                         if ((r = evergreen_interp_input(&ctx, ni)))
  2061.                                                 return r;
  2062.                                 }
  2063.                         }
  2064.                 }
  2065.         }
  2066.  
  2067. /* LLVM backend setup */
  2068. #ifdef R600_USE_LLVM
  2069.         if (use_llvm) {
  2070.                 struct radeon_llvm_context radeon_llvm_ctx;
  2071.                 LLVMModuleRef mod;
  2072.                 bool dump = r600_can_dump_shader(&rscreen->b, tokens);
  2073.                 boolean use_kill = false;
  2074.  
  2075.                 memset(&radeon_llvm_ctx, 0, sizeof(radeon_llvm_ctx));
  2076.                 radeon_llvm_ctx.type = ctx.type;
  2077.                 radeon_llvm_ctx.two_side = shader->two_side;
  2078.                 radeon_llvm_ctx.face_gpr = ctx.face_gpr;
  2079.                 radeon_llvm_ctx.inputs_count = ctx.shader->ninput + 1;
  2080.                 radeon_llvm_ctx.r600_inputs = ctx.shader->input;
  2081.                 radeon_llvm_ctx.r600_outputs = ctx.shader->output;
  2082.                 radeon_llvm_ctx.color_buffer_count = max_color_exports;
  2083.                 radeon_llvm_ctx.chip_class = ctx.bc->chip_class;
  2084.                 radeon_llvm_ctx.fs_color_all = shader->fs_write_all && (rscreen->b.chip_class >= EVERGREEN);
  2085.                 radeon_llvm_ctx.stream_outputs = &so;
  2086.                 radeon_llvm_ctx.clip_vertex = ctx.cv_output;
  2087.                 radeon_llvm_ctx.alpha_to_one = key.alpha_to_one;
  2088.                 radeon_llvm_ctx.has_compressed_msaa_texturing =
  2089.                         ctx.bc->has_compressed_msaa_texturing;
  2090.                 mod = r600_tgsi_llvm(&radeon_llvm_ctx, tokens);
  2091.                 ctx.shader->has_txq_cube_array_z_comp = radeon_llvm_ctx.has_txq_cube_array_z_comp;
  2092.                 ctx.shader->uses_tex_buffers = radeon_llvm_ctx.uses_tex_buffers;
  2093.  
  2094.                 if (r600_llvm_compile(mod, rscreen->b.family, ctx.bc, &use_kill, dump)) {
  2095.                         radeon_llvm_dispose(&radeon_llvm_ctx);
  2096.                         use_llvm = 0;
  2097.                         fprintf(stderr, "R600 LLVM backend failed to compile "
  2098.                                 "shader.  Falling back to TGSI\n");
  2099.                 } else {
  2100.                         ctx.file_offset[TGSI_FILE_OUTPUT] =
  2101.                                         ctx.file_offset[TGSI_FILE_INPUT];
  2102.                 }
  2103.                 if (use_kill)
  2104.                         ctx.shader->uses_kill = use_kill;
  2105.                 radeon_llvm_dispose(&radeon_llvm_ctx);
  2106.         }
  2107. #endif
  2108. /* End of LLVM backend setup */
  2109.  
  2110.         if (shader->fs_write_all && rscreen->b.chip_class >= EVERGREEN)
  2111.                 shader->nr_ps_max_color_exports = 8;
  2112.  
  2113.         if (!use_llvm) {
  2114.                 if (ctx.fragcoord_input >= 0) {
  2115.                         if (ctx.bc->chip_class == CAYMAN) {
  2116.                                 for (j = 0 ; j < 4; j++) {
  2117.                                         struct r600_bytecode_alu alu;
  2118.                                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2119.                                         alu.op = ALU_OP1_RECIP_IEEE;
  2120.                                         alu.src[0].sel = shader->input[ctx.fragcoord_input].gpr;
  2121.                                         alu.src[0].chan = 3;
  2122.  
  2123.                                         alu.dst.sel = shader->input[ctx.fragcoord_input].gpr;
  2124.                                         alu.dst.chan = j;
  2125.                                         alu.dst.write = (j == 3);
  2126.                                         alu.last = 1;
  2127.                                         if ((r = r600_bytecode_add_alu(ctx.bc, &alu)))
  2128.                                                 return r;
  2129.                                 }
  2130.                         } else {
  2131.                                 struct r600_bytecode_alu alu;
  2132.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2133.                                 alu.op = ALU_OP1_RECIP_IEEE;
  2134.                                 alu.src[0].sel = shader->input[ctx.fragcoord_input].gpr;
  2135.                                 alu.src[0].chan = 3;
  2136.  
  2137.                                 alu.dst.sel = shader->input[ctx.fragcoord_input].gpr;
  2138.                                 alu.dst.chan = 3;
  2139.                                 alu.dst.write = 1;
  2140.                                 alu.last = 1;
  2141.                                 if ((r = r600_bytecode_add_alu(ctx.bc, &alu)))
  2142.                                         return r;
  2143.                         }
  2144.                 }
  2145.  
  2146.                 if (ctx.type == TGSI_PROCESSOR_GEOMETRY) {
  2147.                         struct r600_bytecode_alu alu;
  2148.                         int r;
  2149.  
  2150.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2151.                         alu.op = ALU_OP1_MOV;
  2152.                         alu.src[0].sel = V_SQ_ALU_SRC_LITERAL;
  2153.                         alu.src[0].value = 0;
  2154.                         alu.dst.sel = ctx.gs_export_gpr_treg;
  2155.                         alu.dst.write = 1;
  2156.                         alu.last = 1;
  2157.                         r = r600_bytecode_add_alu(ctx.bc, &alu);
  2158.                         if (r)
  2159.                                 return r;
  2160.                 }
  2161.                 if (shader->two_side && ctx.colors_used) {
  2162.                         if ((r = process_twoside_color_inputs(&ctx)))
  2163.                                 return r;
  2164.                 }
  2165.  
  2166.                 tgsi_parse_init(&ctx.parse, tokens);
  2167.                 while (!tgsi_parse_end_of_tokens(&ctx.parse)) {
  2168.                         tgsi_parse_token(&ctx.parse);
  2169.                         switch (ctx.parse.FullToken.Token.Type) {
  2170.                         case TGSI_TOKEN_TYPE_INSTRUCTION:
  2171.                                 r = tgsi_is_supported(&ctx);
  2172.                                 if (r)
  2173.                                         goto out_err;
  2174.                                 ctx.max_driver_temp_used = 0;
  2175.                                 /* reserve first tmp for everyone */
  2176.                                 r600_get_temp(&ctx);
  2177.  
  2178.                                 opcode = ctx.parse.FullToken.FullInstruction.Instruction.Opcode;
  2179.                                 if ((r = tgsi_split_constant(&ctx)))
  2180.                                         goto out_err;
  2181.                                 if ((r = tgsi_split_literal_constant(&ctx)))
  2182.                                         goto out_err;
  2183.                                 if (ctx.type == TGSI_PROCESSOR_GEOMETRY)
  2184.                                         if ((r = tgsi_split_gs_inputs(&ctx)))
  2185.                                                 goto out_err;
  2186.                                 if (ctx.bc->chip_class == CAYMAN)
  2187.                                         ctx.inst_info = &cm_shader_tgsi_instruction[opcode];
  2188.                                 else if (ctx.bc->chip_class >= EVERGREEN)
  2189.                                         ctx.inst_info = &eg_shader_tgsi_instruction[opcode];
  2190.                                 else
  2191.                                         ctx.inst_info = &r600_shader_tgsi_instruction[opcode];
  2192.                                 r = ctx.inst_info->process(&ctx);
  2193.                                 if (r)
  2194.                                         goto out_err;
  2195.                                 break;
  2196.                         default:
  2197.                                 break;
  2198.                         }
  2199.                 }
  2200.         }
  2201.  
  2202.         /* Reset the temporary register counter. */
  2203.         ctx.max_driver_temp_used = 0;
  2204.  
  2205.         noutput = shader->noutput;
  2206.  
  2207.         if (!ring_outputs && ctx.clip_vertex_write) {
  2208.                 unsigned clipdist_temp[2];
  2209.  
  2210.                 clipdist_temp[0] = r600_get_temp(&ctx);
  2211.                 clipdist_temp[1] = r600_get_temp(&ctx);
  2212.  
  2213.                 /* need to convert a clipvertex write into clipdistance writes and not export
  2214.                    the clip vertex anymore */
  2215.  
  2216.                 memset(&shader->output[noutput], 0, 2*sizeof(struct r600_shader_io));
  2217.                 shader->output[noutput].name = TGSI_SEMANTIC_CLIPDIST;
  2218.                 shader->output[noutput].gpr = clipdist_temp[0];
  2219.                 noutput++;
  2220.                 shader->output[noutput].name = TGSI_SEMANTIC_CLIPDIST;
  2221.                 shader->output[noutput].gpr = clipdist_temp[1];
  2222.                 noutput++;
  2223.  
  2224.                 /* reset spi_sid for clipvertex output to avoid confusing spi */
  2225.                 shader->output[ctx.cv_output].spi_sid = 0;
  2226.  
  2227.                 shader->clip_dist_write = 0xFF;
  2228.  
  2229.                 for (i = 0; i < 8; i++) {
  2230.                         int oreg = i >> 2;
  2231.                         int ochan = i & 3;
  2232.  
  2233.                         for (j = 0; j < 4; j++) {
  2234.                                 struct r600_bytecode_alu alu;
  2235.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2236.                                 alu.op = ALU_OP2_DOT4;
  2237.                                 alu.src[0].sel = shader->output[ctx.cv_output].gpr;
  2238.                                 alu.src[0].chan = j;
  2239.  
  2240.                                 alu.src[1].sel = 512 + i;
  2241.                                 alu.src[1].kc_bank = R600_UCP_CONST_BUFFER;
  2242.                                 alu.src[1].chan = j;
  2243.  
  2244.                                 alu.dst.sel = clipdist_temp[oreg];
  2245.                                 alu.dst.chan = j;
  2246.                                 alu.dst.write = (j == ochan);
  2247.                                 if (j == 3)
  2248.                                         alu.last = 1;
  2249.                                 if (!use_llvm)
  2250.                                         r = r600_bytecode_add_alu(ctx.bc, &alu);
  2251.                                 if (r)
  2252.                                         return r;
  2253.                         }
  2254.                 }
  2255.         }
  2256.  
  2257.         /* Add stream outputs. */
  2258.         if (!ring_outputs && ctx.type == TGSI_PROCESSOR_VERTEX &&
  2259.             so.num_outputs && !use_llvm)
  2260.                 emit_streamout(&ctx, &so);
  2261.  
  2262.         convert_edgeflag_to_int(&ctx);
  2263.  
  2264.         if (ring_outputs) {
  2265.                 if (key.vs_as_es)
  2266.                         emit_gs_ring_writes(&ctx, FALSE);
  2267.         } else {
  2268.                 /* Export output */
  2269.                 next_clip_base = shader->vs_out_misc_write ? 62 : 61;
  2270.  
  2271.                 for (i = 0, j = 0; i < noutput; i++, j++) {
  2272.                         memset(&output[j], 0, sizeof(struct r600_bytecode_output));
  2273.                         output[j].gpr = shader->output[i].gpr;
  2274.                         output[j].elem_size = 3;
  2275.                         output[j].swizzle_x = 0;
  2276.                         output[j].swizzle_y = 1;
  2277.                         output[j].swizzle_z = 2;
  2278.                         output[j].swizzle_w = 3;
  2279.                         output[j].burst_count = 1;
  2280.                         output[j].type = -1;
  2281.                         output[j].op = CF_OP_EXPORT;
  2282.                         switch (ctx.type) {
  2283.                         case TGSI_PROCESSOR_VERTEX:
  2284.                                 switch (shader->output[i].name) {
  2285.                                 case TGSI_SEMANTIC_POSITION:
  2286.                                         output[j].array_base = 60;
  2287.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  2288.                                         pos_emitted = true;
  2289.                                         break;
  2290.  
  2291.                                 case TGSI_SEMANTIC_PSIZE:
  2292.                                         output[j].array_base = 61;
  2293.                                         output[j].swizzle_y = 7;
  2294.                                         output[j].swizzle_z = 7;
  2295.                                         output[j].swizzle_w = 7;
  2296.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  2297.                                         pos_emitted = true;
  2298.                                         break;
  2299.                                 case TGSI_SEMANTIC_EDGEFLAG:
  2300.                                         output[j].array_base = 61;
  2301.                                         output[j].swizzle_x = 7;
  2302.                                         output[j].swizzle_y = 0;
  2303.                                         output[j].swizzle_z = 7;
  2304.                                         output[j].swizzle_w = 7;
  2305.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  2306.                                         pos_emitted = true;
  2307.                                         break;
  2308.                                 case TGSI_SEMANTIC_LAYER:
  2309.                                         /* spi_sid is 0 for outputs that are
  2310.                                          * not consumed by PS */
  2311.                                         if (shader->output[i].spi_sid) {
  2312.                                                 output[j].array_base = next_param_base++;
  2313.                                                 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
  2314.                                                 j++;
  2315.                                                 memcpy(&output[j], &output[j-1], sizeof(struct r600_bytecode_output));
  2316.                                         }
  2317.                                         output[j].array_base = 61;
  2318.                                         output[j].swizzle_x = 7;
  2319.                                         output[j].swizzle_y = 7;
  2320.                                         output[j].swizzle_z = 0;
  2321.                                         output[j].swizzle_w = 7;
  2322.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  2323.                                         pos_emitted = true;
  2324.                                         break;
  2325.                                 case TGSI_SEMANTIC_VIEWPORT_INDEX:
  2326.                                         /* spi_sid is 0 for outputs that are
  2327.                                          * not consumed by PS */
  2328.                                         if (shader->output[i].spi_sid) {
  2329.                                                 output[j].array_base = next_param_base++;
  2330.                                                 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
  2331.                                                 j++;
  2332.                                                 memcpy(&output[j], &output[j-1], sizeof(struct r600_bytecode_output));
  2333.                                         }
  2334.                                         output[j].array_base = 61;
  2335.                                         output[j].swizzle_x = 7;
  2336.                                         output[j].swizzle_y = 7;
  2337.                                         output[j].swizzle_z = 7;
  2338.                                         output[j].swizzle_w = 0;
  2339.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  2340.                                         pos_emitted = true;
  2341.                                         break;
  2342.                                 case TGSI_SEMANTIC_CLIPVERTEX:
  2343.                                         j--;
  2344.                                         break;
  2345.                                 case TGSI_SEMANTIC_CLIPDIST:
  2346.                                         output[j].array_base = next_clip_base++;
  2347.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  2348.                                         pos_emitted = true;
  2349.                                         /* spi_sid is 0 for clipdistance outputs that were generated
  2350.                                          * for clipvertex - we don't need to pass them to PS */
  2351.                                         if (shader->output[i].spi_sid) {
  2352.                                                 j++;
  2353.                                                 /* duplicate it as PARAM to pass to the pixel shader */
  2354.                                                 memcpy(&output[j], &output[j-1], sizeof(struct r600_bytecode_output));
  2355.                                                 output[j].array_base = next_param_base++;
  2356.                                                 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
  2357.                                         }
  2358.                                         break;
  2359.                                 case TGSI_SEMANTIC_FOG:
  2360.                                         output[j].swizzle_y = 4; /* 0 */
  2361.                                         output[j].swizzle_z = 4; /* 0 */
  2362.                                         output[j].swizzle_w = 5; /* 1 */
  2363.                                         break;
  2364.                                 case TGSI_SEMANTIC_PRIMID:
  2365.                                         output[j].swizzle_x = 2;
  2366.                                         output[j].swizzle_y = 4; /* 0 */
  2367.                                         output[j].swizzle_z = 4; /* 0 */
  2368.                                         output[j].swizzle_w = 4; /* 0 */
  2369.                                         break;
  2370.                                 }
  2371.  
  2372.                                 break;
  2373.                         case TGSI_PROCESSOR_FRAGMENT:
  2374.                                 if (shader->output[i].name == TGSI_SEMANTIC_COLOR) {
  2375.                                         /* never export more colors than the number of CBs */
  2376.                                         if (shader->output[i].sid >= max_color_exports) {
  2377.                                                 /* skip export */
  2378.                                                 j--;
  2379.                                                 continue;
  2380.                                         }
  2381.                                         output[j].swizzle_w = key.alpha_to_one ? 5 : 3;
  2382.                                         output[j].array_base = shader->output[i].sid;
  2383.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
  2384.                                         shader->nr_ps_color_exports++;
  2385.                                         if (shader->fs_write_all && (rscreen->b.chip_class >= EVERGREEN)) {
  2386.                                                 for (k = 1; k < max_color_exports; k++) {
  2387.                                                         j++;
  2388.                                                         memset(&output[j], 0, sizeof(struct r600_bytecode_output));
  2389.                                                         output[j].gpr = shader->output[i].gpr;
  2390.                                                         output[j].elem_size = 3;
  2391.                                                         output[j].swizzle_x = 0;
  2392.                                                         output[j].swizzle_y = 1;
  2393.                                                         output[j].swizzle_z = 2;
  2394.                                                         output[j].swizzle_w = key.alpha_to_one ? 5 : 3;
  2395.                                                         output[j].burst_count = 1;
  2396.                                                         output[j].array_base = k;
  2397.                                                         output[j].op = CF_OP_EXPORT;
  2398.                                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
  2399.                                                         shader->nr_ps_color_exports++;
  2400.                                                 }
  2401.                                         }
  2402.                                 } else if (shader->output[i].name == TGSI_SEMANTIC_POSITION) {
  2403.                                         output[j].array_base = 61;
  2404.                                         output[j].swizzle_x = 2;
  2405.                                         output[j].swizzle_y = 7;
  2406.                                         output[j].swizzle_z = output[j].swizzle_w = 7;
  2407.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
  2408.                                 } else if (shader->output[i].name == TGSI_SEMANTIC_STENCIL) {
  2409.                                         output[j].array_base = 61;
  2410.                                         output[j].swizzle_x = 7;
  2411.                                         output[j].swizzle_y = 1;
  2412.                                         output[j].swizzle_z = output[j].swizzle_w = 7;
  2413.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
  2414.                                 } else if (shader->output[i].name == TGSI_SEMANTIC_SAMPLEMASK) {
  2415.                                         output[j].array_base = 61;
  2416.                                         output[j].swizzle_x = 7;
  2417.                                         output[j].swizzle_y = 7;
  2418.                                         output[j].swizzle_z = 0;
  2419.                                         output[j].swizzle_w = 7;
  2420.                                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
  2421.                                 } else {
  2422.                                         R600_ERR("unsupported fragment output name %d\n", shader->output[i].name);
  2423.                                         r = -EINVAL;
  2424.                                         goto out_err;
  2425.                                 }
  2426.                                 break;
  2427.                         default:
  2428.                                 R600_ERR("unsupported processor type %d\n", ctx.type);
  2429.                                 r = -EINVAL;
  2430.                                 goto out_err;
  2431.                         }
  2432.  
  2433.                         if (output[j].type==-1) {
  2434.                                 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
  2435.                                 output[j].array_base = next_param_base++;
  2436.                         }
  2437.                 }
  2438.  
  2439.                 /* add fake position export */
  2440.                 if (ctx.type == TGSI_PROCESSOR_VERTEX && pos_emitted == false) {
  2441.                         memset(&output[j], 0, sizeof(struct r600_bytecode_output));
  2442.                         output[j].gpr = 0;
  2443.                         output[j].elem_size = 3;
  2444.                         output[j].swizzle_x = 7;
  2445.                         output[j].swizzle_y = 7;
  2446.                         output[j].swizzle_z = 7;
  2447.                         output[j].swizzle_w = 7;
  2448.                         output[j].burst_count = 1;
  2449.                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
  2450.                         output[j].array_base = 60;
  2451.                         output[j].op = CF_OP_EXPORT;
  2452.                         j++;
  2453.                 }
  2454.  
  2455.                 /* add fake param output for vertex shader if no param is exported */
  2456.                 if (ctx.type == TGSI_PROCESSOR_VERTEX && next_param_base == 0) {
  2457.                         memset(&output[j], 0, sizeof(struct r600_bytecode_output));
  2458.                         output[j].gpr = 0;
  2459.                         output[j].elem_size = 3;
  2460.                         output[j].swizzle_x = 7;
  2461.                         output[j].swizzle_y = 7;
  2462.                         output[j].swizzle_z = 7;
  2463.                         output[j].swizzle_w = 7;
  2464.                         output[j].burst_count = 1;
  2465.                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
  2466.                         output[j].array_base = 0;
  2467.                         output[j].op = CF_OP_EXPORT;
  2468.                         j++;
  2469.                 }
  2470.  
  2471.                 /* add fake pixel export */
  2472.                 if (ctx.type == TGSI_PROCESSOR_FRAGMENT && shader->nr_ps_color_exports == 0) {
  2473.                         memset(&output[j], 0, sizeof(struct r600_bytecode_output));
  2474.                         output[j].gpr = 0;
  2475.                         output[j].elem_size = 3;
  2476.                         output[j].swizzle_x = 7;
  2477.                         output[j].swizzle_y = 7;
  2478.                         output[j].swizzle_z = 7;
  2479.                         output[j].swizzle_w = 7;
  2480.                         output[j].burst_count = 1;
  2481.                         output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
  2482.                         output[j].array_base = 0;
  2483.                         output[j].op = CF_OP_EXPORT;
  2484.                         j++;
  2485.                 }
  2486.  
  2487.                 noutput = j;
  2488.  
  2489.                 /* set export done on last export of each type */
  2490.                 for (i = noutput - 1, output_done = 0; i >= 0; i--) {
  2491.                         if (!(output_done & (1 << output[i].type))) {
  2492.                                 output_done |= (1 << output[i].type);
  2493.                                 output[i].op = CF_OP_EXPORT_DONE;
  2494.                         }
  2495.                 }
  2496.                 /* add output to bytecode */
  2497.                 if (!use_llvm) {
  2498.                         for (i = 0; i < noutput; i++) {
  2499.                                 r = r600_bytecode_add_output(ctx.bc, &output[i]);
  2500.                                 if (r)
  2501.                                         goto out_err;
  2502.                         }
  2503.                 }
  2504.         }
  2505.  
  2506.         /* add program end */
  2507.         if (!use_llvm) {
  2508.                 if (ctx.bc->chip_class == CAYMAN)
  2509.                         cm_bytecode_add_cf_end(ctx.bc);
  2510.                 else {
  2511.                         const struct cf_op_info *last = NULL;
  2512.  
  2513.                         if (ctx.bc->cf_last)
  2514.                                 last = r600_isa_cf(ctx.bc->cf_last->op);
  2515.  
  2516.                         /* alu clause instructions don't have EOP bit, so add NOP */
  2517.                         if (!last || last->flags & CF_ALU || ctx.bc->cf_last->op == CF_OP_LOOP_END || ctx.bc->cf_last->op == CF_OP_CALL_FS)
  2518.                                 r600_bytecode_add_cfinst(ctx.bc, CF_OP_NOP);
  2519.  
  2520.                         ctx.bc->cf_last->end_of_program = 1;
  2521.                 }
  2522.         }
  2523.  
  2524.         /* check GPR limit - we have 124 = 128 - 4
  2525.          * (4 are reserved as alu clause temporary registers) */
  2526.         if (ctx.bc->ngpr > 124) {
  2527.                 R600_ERR("GPR limit exceeded - shader requires %d registers\n", ctx.bc->ngpr);
  2528.                 r = -ENOMEM;
  2529.                 goto out_err;
  2530.         }
  2531.  
  2532.         if (ctx.type == TGSI_PROCESSOR_GEOMETRY) {
  2533.                 if ((r = generate_gs_copy_shader(rctx, pipeshader, &so)))
  2534.                         return r;
  2535.         }
  2536.  
  2537.         free(ctx.literals);
  2538.         tgsi_parse_free(&ctx.parse);
  2539.         return 0;
  2540. out_err:
  2541.         free(ctx.literals);
  2542.         tgsi_parse_free(&ctx.parse);
  2543.         return r;
  2544. }
  2545.  
  2546. static int tgsi_unsupported(struct r600_shader_ctx *ctx)
  2547. {
  2548.         const unsigned tgsi_opcode =
  2549.                 ctx->parse.FullToken.FullInstruction.Instruction.Opcode;
  2550.         R600_ERR("%s tgsi opcode unsupported\n",
  2551.                  tgsi_get_opcode_name(tgsi_opcode));
  2552.         return -EINVAL;
  2553. }
  2554.  
  2555. static int tgsi_end(struct r600_shader_ctx *ctx)
  2556. {
  2557.         return 0;
  2558. }
  2559.  
  2560. static void r600_bytecode_src(struct r600_bytecode_alu_src *bc_src,
  2561.                         const struct r600_shader_src *shader_src,
  2562.                         unsigned chan)
  2563. {
  2564.         bc_src->sel = shader_src->sel;
  2565.         bc_src->chan = shader_src->swizzle[chan];
  2566.         bc_src->neg = shader_src->neg;
  2567.         bc_src->abs = shader_src->abs;
  2568.         bc_src->rel = shader_src->rel;
  2569.         bc_src->value = shader_src->value[bc_src->chan];
  2570.         bc_src->kc_bank = shader_src->kc_bank;
  2571.         bc_src->kc_rel = shader_src->kc_rel;
  2572. }
  2573.  
  2574. static void r600_bytecode_src_set_abs(struct r600_bytecode_alu_src *bc_src)
  2575. {
  2576.         bc_src->abs = 1;
  2577.         bc_src->neg = 0;
  2578. }
  2579.  
  2580. static void r600_bytecode_src_toggle_neg(struct r600_bytecode_alu_src *bc_src)
  2581. {
  2582.         bc_src->neg = !bc_src->neg;
  2583. }
  2584.  
  2585. static void tgsi_dst(struct r600_shader_ctx *ctx,
  2586.                      const struct tgsi_full_dst_register *tgsi_dst,
  2587.                      unsigned swizzle,
  2588.                      struct r600_bytecode_alu_dst *r600_dst)
  2589. {
  2590.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2591.  
  2592.         r600_dst->sel = tgsi_dst->Register.Index;
  2593.         r600_dst->sel += ctx->file_offset[tgsi_dst->Register.File];
  2594.         r600_dst->chan = swizzle;
  2595.         r600_dst->write = 1;
  2596.         if (tgsi_dst->Register.Indirect)
  2597.                 r600_dst->rel = V_SQ_REL_RELATIVE;
  2598.         if (inst->Instruction.Saturate) {
  2599.                 r600_dst->clamp = 1;
  2600.         }
  2601. }
  2602.  
  2603. static int tgsi_last_instruction(unsigned writemask)
  2604. {
  2605.         int i, lasti = 0;
  2606.  
  2607.         for (i = 0; i < 4; i++) {
  2608.                 if (writemask & (1 << i)) {
  2609.                         lasti = i;
  2610.                 }
  2611.         }
  2612.         return lasti;
  2613. }
  2614.  
  2615. static int tgsi_op2_s(struct r600_shader_ctx *ctx, int swap, int trans_only)
  2616. {
  2617.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2618.         struct r600_bytecode_alu alu;
  2619.         unsigned write_mask = inst->Dst[0].Register.WriteMask;
  2620.         int i, j, r, lasti = tgsi_last_instruction(write_mask);
  2621.         /* use temp register if trans_only and more than one dst component */
  2622.         int use_tmp = trans_only && (write_mask ^ (1 << lasti));
  2623.  
  2624.         for (i = 0; i <= lasti; i++) {
  2625.                 if (!(write_mask & (1 << i)))
  2626.                         continue;
  2627.  
  2628.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2629.                 if (use_tmp) {
  2630.                         alu.dst.sel = ctx->temp_reg;
  2631.                         alu.dst.chan = i;
  2632.                         alu.dst.write = 1;
  2633.                 } else
  2634.                         tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2635.  
  2636.                 alu.op = ctx->inst_info->op;
  2637.                 if (!swap) {
  2638.                         for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
  2639.                                 r600_bytecode_src(&alu.src[j], &ctx->src[j], i);
  2640.                         }
  2641.                 } else {
  2642.                         r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  2643.                         r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  2644.                 }
  2645.                 /* handle some special cases */
  2646.                 switch (inst->Instruction.Opcode) {
  2647.                 case TGSI_OPCODE_SUB:
  2648.                         r600_bytecode_src_toggle_neg(&alu.src[1]);
  2649.                         break;
  2650.                 case TGSI_OPCODE_ABS:
  2651.                         r600_bytecode_src_set_abs(&alu.src[0]);
  2652.                         break;
  2653.                 default:
  2654.                         break;
  2655.                 }
  2656.                 if (i == lasti || trans_only) {
  2657.                         alu.last = 1;
  2658.                 }
  2659.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  2660.                 if (r)
  2661.                         return r;
  2662.         }
  2663.  
  2664.         if (use_tmp) {
  2665.                 /* move result from temp to dst */
  2666.                 for (i = 0; i <= lasti; i++) {
  2667.                         if (!(write_mask & (1 << i)))
  2668.                                 continue;
  2669.  
  2670.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2671.                         alu.op = ALU_OP1_MOV;
  2672.                         tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2673.                         alu.src[0].sel = ctx->temp_reg;
  2674.                         alu.src[0].chan = i;
  2675.                         alu.last = (i == lasti);
  2676.  
  2677.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  2678.                         if (r)
  2679.                                 return r;
  2680.                 }
  2681.         }
  2682.         return 0;
  2683. }
  2684.  
  2685. static int tgsi_op2(struct r600_shader_ctx *ctx)
  2686. {
  2687.         return tgsi_op2_s(ctx, 0, 0);
  2688. }
  2689.  
  2690. static int tgsi_op2_swap(struct r600_shader_ctx *ctx)
  2691. {
  2692.         return tgsi_op2_s(ctx, 1, 0);
  2693. }
  2694.  
  2695. static int tgsi_op2_trans(struct r600_shader_ctx *ctx)
  2696. {
  2697.         return tgsi_op2_s(ctx, 0, 1);
  2698. }
  2699.  
  2700. static int tgsi_ineg(struct r600_shader_ctx *ctx)
  2701. {
  2702.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2703.         struct r600_bytecode_alu alu;
  2704.         int i, r;
  2705.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  2706.  
  2707.         for (i = 0; i < lasti + 1; i++) {
  2708.  
  2709.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  2710.                         continue;
  2711.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2712.                 alu.op = ctx->inst_info->op;
  2713.  
  2714.                 alu.src[0].sel = V_SQ_ALU_SRC_0;
  2715.  
  2716.                 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  2717.  
  2718.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2719.  
  2720.                 if (i == lasti) {
  2721.                         alu.last = 1;
  2722.                 }
  2723.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  2724.                 if (r)
  2725.                         return r;
  2726.         }
  2727.         return 0;
  2728.  
  2729. }
  2730.  
  2731. static int cayman_emit_float_instr(struct r600_shader_ctx *ctx)
  2732. {
  2733.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2734.         int i, j, r;
  2735.         struct r600_bytecode_alu alu;
  2736.         int last_slot = (inst->Dst[0].Register.WriteMask & 0x8) ? 4 : 3;
  2737.        
  2738.         for (i = 0 ; i < last_slot; i++) {
  2739.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2740.                 alu.op = ctx->inst_info->op;
  2741.                 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
  2742.                         r600_bytecode_src(&alu.src[j], &ctx->src[j], 0);
  2743.  
  2744.                         /* RSQ should take the absolute value of src */
  2745.                         if (inst->Instruction.Opcode == TGSI_OPCODE_RSQ) {
  2746.                                 r600_bytecode_src_set_abs(&alu.src[j]);
  2747.                         }
  2748.                 }
  2749.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2750.                 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
  2751.  
  2752.                 if (i == last_slot - 1)
  2753.                         alu.last = 1;
  2754.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  2755.                 if (r)
  2756.                         return r;
  2757.         }
  2758.         return 0;
  2759. }
  2760.  
  2761. static int cayman_mul_int_instr(struct r600_shader_ctx *ctx)
  2762. {
  2763.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2764.         int i, j, k, r;
  2765.         struct r600_bytecode_alu alu;
  2766.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  2767.         int t1 = ctx->temp_reg;
  2768.  
  2769.         for (k = 0; k <= lasti; k++) {
  2770.                 if (!(inst->Dst[0].Register.WriteMask & (1 << k)))
  2771.                         continue;
  2772.  
  2773.                 for (i = 0 ; i < 4; i++) {
  2774.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2775.                         alu.op = ctx->inst_info->op;
  2776.                         for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
  2777.                                 r600_bytecode_src(&alu.src[j], &ctx->src[j], k);
  2778.                         }
  2779.                         alu.dst.sel = t1;
  2780.                         alu.dst.chan = i;
  2781.                         alu.dst.write = (i == k);
  2782.                         if (i == 3)
  2783.                                 alu.last = 1;
  2784.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  2785.                         if (r)
  2786.                                 return r;
  2787.                 }
  2788.         }
  2789.  
  2790.         for (i = 0 ; i <= lasti; i++) {
  2791.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  2792.                         continue;
  2793.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2794.                 alu.op = ALU_OP1_MOV;
  2795.                 alu.src[0].sel = t1;
  2796.                 alu.src[0].chan = i;
  2797.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2798.                 alu.dst.write = 1;
  2799.                 if (i == lasti)
  2800.                         alu.last = 1;
  2801.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  2802.                 if (r)
  2803.                         return r;
  2804.         }
  2805.  
  2806.         return 0;
  2807. }
  2808.  
  2809. /*
  2810.  * r600 - trunc to -PI..PI range
  2811.  * r700 - normalize by dividing by 2PI
  2812.  * see fdo bug 27901
  2813.  */
  2814. static int tgsi_setup_trig(struct r600_shader_ctx *ctx)
  2815. {
  2816.         static float half_inv_pi = 1.0 /(3.1415926535 * 2);
  2817.         static float double_pi = 3.1415926535 * 2;
  2818.         static float neg_pi = -3.1415926535;
  2819.  
  2820.         int r;
  2821.         struct r600_bytecode_alu alu;
  2822.  
  2823.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2824.         alu.op = ALU_OP3_MULADD;
  2825.         alu.is_op3 = 1;
  2826.  
  2827.         alu.dst.chan = 0;
  2828.         alu.dst.sel = ctx->temp_reg;
  2829.         alu.dst.write = 1;
  2830.  
  2831.         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  2832.  
  2833.         alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  2834.         alu.src[1].chan = 0;
  2835.         alu.src[1].value = *(uint32_t *)&half_inv_pi;
  2836.         alu.src[2].sel = V_SQ_ALU_SRC_0_5;
  2837.         alu.src[2].chan = 0;
  2838.         alu.last = 1;
  2839.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  2840.         if (r)
  2841.                 return r;
  2842.  
  2843.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2844.         alu.op = ALU_OP1_FRACT;
  2845.  
  2846.         alu.dst.chan = 0;
  2847.         alu.dst.sel = ctx->temp_reg;
  2848.         alu.dst.write = 1;
  2849.  
  2850.         alu.src[0].sel = ctx->temp_reg;
  2851.         alu.src[0].chan = 0;
  2852.         alu.last = 1;
  2853.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  2854.         if (r)
  2855.                 return r;
  2856.  
  2857.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2858.         alu.op = ALU_OP3_MULADD;
  2859.         alu.is_op3 = 1;
  2860.  
  2861.         alu.dst.chan = 0;
  2862.         alu.dst.sel = ctx->temp_reg;
  2863.         alu.dst.write = 1;
  2864.  
  2865.         alu.src[0].sel = ctx->temp_reg;
  2866.         alu.src[0].chan = 0;
  2867.  
  2868.         alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  2869.         alu.src[1].chan = 0;
  2870.         alu.src[2].sel = V_SQ_ALU_SRC_LITERAL;
  2871.         alu.src[2].chan = 0;
  2872.  
  2873.         if (ctx->bc->chip_class == R600) {
  2874.                 alu.src[1].value = *(uint32_t *)&double_pi;
  2875.                 alu.src[2].value = *(uint32_t *)&neg_pi;
  2876.         } else {
  2877.                 alu.src[1].sel = V_SQ_ALU_SRC_1;
  2878.                 alu.src[2].sel = V_SQ_ALU_SRC_0_5;
  2879.                 alu.src[2].neg = 1;
  2880.         }
  2881.  
  2882.         alu.last = 1;
  2883.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  2884.         if (r)
  2885.                 return r;
  2886.         return 0;
  2887. }
  2888.  
  2889. static int cayman_trig(struct r600_shader_ctx *ctx)
  2890. {
  2891.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2892.         struct r600_bytecode_alu alu;
  2893.         int last_slot = (inst->Dst[0].Register.WriteMask & 0x8) ? 4 : 3;
  2894.         int i, r;
  2895.  
  2896.         r = tgsi_setup_trig(ctx);
  2897.         if (r)
  2898.                 return r;
  2899.  
  2900.  
  2901.         for (i = 0; i < last_slot; i++) {
  2902.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2903.                 alu.op = ctx->inst_info->op;
  2904.                 alu.dst.chan = i;
  2905.  
  2906.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2907.                 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
  2908.  
  2909.                 alu.src[0].sel = ctx->temp_reg;
  2910.                 alu.src[0].chan = 0;
  2911.                 if (i == last_slot - 1)
  2912.                         alu.last = 1;
  2913.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  2914.                 if (r)
  2915.                         return r;
  2916.         }
  2917.         return 0;
  2918. }
  2919.  
  2920. static int tgsi_trig(struct r600_shader_ctx *ctx)
  2921. {
  2922.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2923.         struct r600_bytecode_alu alu;
  2924.         int i, r;
  2925.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  2926.  
  2927.         r = tgsi_setup_trig(ctx);
  2928.         if (r)
  2929.                 return r;
  2930.  
  2931.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2932.         alu.op = ctx->inst_info->op;
  2933.         alu.dst.chan = 0;
  2934.         alu.dst.sel = ctx->temp_reg;
  2935.         alu.dst.write = 1;
  2936.  
  2937.         alu.src[0].sel = ctx->temp_reg;
  2938.         alu.src[0].chan = 0;
  2939.         alu.last = 1;
  2940.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  2941.         if (r)
  2942.                 return r;
  2943.  
  2944.         /* replicate result */
  2945.         for (i = 0; i < lasti + 1; i++) {
  2946.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  2947.                         continue;
  2948.  
  2949.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2950.                 alu.op = ALU_OP1_MOV;
  2951.  
  2952.                 alu.src[0].sel = ctx->temp_reg;
  2953.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2954.                 if (i == lasti)
  2955.                         alu.last = 1;
  2956.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  2957.                 if (r)
  2958.                         return r;
  2959.         }
  2960.         return 0;
  2961. }
  2962.  
  2963. static int tgsi_scs(struct r600_shader_ctx *ctx)
  2964. {
  2965.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  2966.         struct r600_bytecode_alu alu;
  2967.         int i, r;
  2968.  
  2969.         /* We'll only need the trig stuff if we are going to write to the
  2970.          * X or Y components of the destination vector.
  2971.          */
  2972.         if (likely(inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY)) {
  2973.                 r = tgsi_setup_trig(ctx);
  2974.                 if (r)
  2975.                         return r;
  2976.         }
  2977.  
  2978.         /* dst.x = COS */
  2979.         if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
  2980.                 if (ctx->bc->chip_class == CAYMAN) {
  2981.                         for (i = 0 ; i < 3; i++) {
  2982.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  2983.                                 alu.op = ALU_OP1_COS;
  2984.                                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  2985.  
  2986.                                 if (i == 0)
  2987.                                         alu.dst.write = 1;
  2988.                                 else
  2989.                                         alu.dst.write = 0;
  2990.                                 alu.src[0].sel = ctx->temp_reg;
  2991.                                 alu.src[0].chan = 0;
  2992.                                 if (i == 2)
  2993.                                         alu.last = 1;
  2994.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  2995.                                 if (r)
  2996.                                         return r;
  2997.                         }
  2998.                 } else {
  2999.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3000.                         alu.op = ALU_OP1_COS;
  3001.                         tgsi_dst(ctx, &inst->Dst[0], 0, &alu.dst);
  3002.  
  3003.                         alu.src[0].sel = ctx->temp_reg;
  3004.                         alu.src[0].chan = 0;
  3005.                         alu.last = 1;
  3006.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3007.                         if (r)
  3008.                                 return r;
  3009.                 }
  3010.         }
  3011.  
  3012.         /* dst.y = SIN */
  3013.         if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
  3014.                 if (ctx->bc->chip_class == CAYMAN) {
  3015.                         for (i = 0 ; i < 3; i++) {
  3016.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3017.                                 alu.op = ALU_OP1_SIN;
  3018.                                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  3019.                                 if (i == 1)
  3020.                                         alu.dst.write = 1;
  3021.                                 else
  3022.                                         alu.dst.write = 0;
  3023.                                 alu.src[0].sel = ctx->temp_reg;
  3024.                                 alu.src[0].chan = 0;
  3025.                                 if (i == 2)
  3026.                                         alu.last = 1;
  3027.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3028.                                 if (r)
  3029.                                         return r;
  3030.                         }
  3031.                 } else {
  3032.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3033.                         alu.op = ALU_OP1_SIN;
  3034.                         tgsi_dst(ctx, &inst->Dst[0], 1, &alu.dst);
  3035.  
  3036.                         alu.src[0].sel = ctx->temp_reg;
  3037.                         alu.src[0].chan = 0;
  3038.                         alu.last = 1;
  3039.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3040.                         if (r)
  3041.                                 return r;
  3042.                 }
  3043.         }
  3044.  
  3045.         /* dst.z = 0.0; */
  3046.         if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
  3047.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3048.  
  3049.                 alu.op = ALU_OP1_MOV;
  3050.  
  3051.                 tgsi_dst(ctx, &inst->Dst[0], 2, &alu.dst);
  3052.  
  3053.                 alu.src[0].sel = V_SQ_ALU_SRC_0;
  3054.                 alu.src[0].chan = 0;
  3055.  
  3056.                 alu.last = 1;
  3057.  
  3058.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3059.                 if (r)
  3060.                         return r;
  3061.         }
  3062.  
  3063.         /* dst.w = 1.0; */
  3064.         if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
  3065.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3066.  
  3067.                 alu.op = ALU_OP1_MOV;
  3068.  
  3069.                 tgsi_dst(ctx, &inst->Dst[0], 3, &alu.dst);
  3070.  
  3071.                 alu.src[0].sel = V_SQ_ALU_SRC_1;
  3072.                 alu.src[0].chan = 0;
  3073.  
  3074.                 alu.last = 1;
  3075.  
  3076.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3077.                 if (r)
  3078.                         return r;
  3079.         }
  3080.  
  3081.         return 0;
  3082. }
  3083.  
  3084. static int tgsi_kill(struct r600_shader_ctx *ctx)
  3085. {
  3086.         const struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  3087.         struct r600_bytecode_alu alu;
  3088.         int i, r;
  3089.  
  3090.         for (i = 0; i < 4; i++) {
  3091.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3092.                 alu.op = ctx->inst_info->op;
  3093.  
  3094.                 alu.dst.chan = i;
  3095.  
  3096.                 alu.src[0].sel = V_SQ_ALU_SRC_0;
  3097.  
  3098.                 if (inst->Instruction.Opcode == TGSI_OPCODE_KILL) {
  3099.                         alu.src[1].sel = V_SQ_ALU_SRC_1;
  3100.                         alu.src[1].neg = 1;
  3101.                 } else {
  3102.                         r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  3103.                 }
  3104.                 if (i == 3) {
  3105.                         alu.last = 1;
  3106.                 }
  3107.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3108.                 if (r)
  3109.                         return r;
  3110.         }
  3111.  
  3112.         /* kill must be last in ALU */
  3113.         ctx->bc->force_add_cf = 1;
  3114.         ctx->shader->uses_kill = TRUE;
  3115.         return 0;
  3116. }
  3117.  
  3118. static int tgsi_lit(struct r600_shader_ctx *ctx)
  3119. {
  3120.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  3121.         struct r600_bytecode_alu alu;
  3122.         int r;
  3123.  
  3124.         /* tmp.x = max(src.y, 0.0) */
  3125.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3126.         alu.op = ALU_OP2_MAX;
  3127.         r600_bytecode_src(&alu.src[0], &ctx->src[0], 1);
  3128.         alu.src[1].sel  = V_SQ_ALU_SRC_0; /*0.0*/
  3129.         alu.src[1].chan = 1;
  3130.  
  3131.         alu.dst.sel = ctx->temp_reg;
  3132.         alu.dst.chan = 0;
  3133.         alu.dst.write = 1;
  3134.  
  3135.         alu.last = 1;
  3136.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3137.         if (r)
  3138.                 return r;
  3139.  
  3140.         if (inst->Dst[0].Register.WriteMask & (1 << 2))
  3141.         {
  3142.                 int chan;
  3143.                 int sel;
  3144.                 int i;
  3145.  
  3146.                 if (ctx->bc->chip_class == CAYMAN) {
  3147.                         for (i = 0; i < 3; i++) {
  3148.                                 /* tmp.z = log(tmp.x) */
  3149.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3150.                                 alu.op = ALU_OP1_LOG_CLAMPED;
  3151.                                 alu.src[0].sel = ctx->temp_reg;
  3152.                                 alu.src[0].chan = 0;
  3153.                                 alu.dst.sel = ctx->temp_reg;
  3154.                                 alu.dst.chan = i;
  3155.                                 if (i == 2) {
  3156.                                         alu.dst.write = 1;
  3157.                                         alu.last = 1;
  3158.                                 } else
  3159.                                         alu.dst.write = 0;
  3160.                                
  3161.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3162.                                 if (r)
  3163.                                         return r;
  3164.                         }
  3165.                 } else {
  3166.                         /* tmp.z = log(tmp.x) */
  3167.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3168.                         alu.op = ALU_OP1_LOG_CLAMPED;
  3169.                         alu.src[0].sel = ctx->temp_reg;
  3170.                         alu.src[0].chan = 0;
  3171.                         alu.dst.sel = ctx->temp_reg;
  3172.                         alu.dst.chan = 2;
  3173.                         alu.dst.write = 1;
  3174.                         alu.last = 1;
  3175.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3176.                         if (r)
  3177.                                 return r;
  3178.                 }
  3179.  
  3180.                 chan = alu.dst.chan;
  3181.                 sel = alu.dst.sel;
  3182.  
  3183.                 /* tmp.x = amd MUL_LIT(tmp.z, src.w, src.x ) */
  3184.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3185.                 alu.op = ALU_OP3_MUL_LIT;
  3186.                 alu.src[0].sel  = sel;
  3187.                 alu.src[0].chan = chan;
  3188.                 r600_bytecode_src(&alu.src[1], &ctx->src[0], 3);
  3189.                 r600_bytecode_src(&alu.src[2], &ctx->src[0], 0);
  3190.                 alu.dst.sel = ctx->temp_reg;
  3191.                 alu.dst.chan = 0;
  3192.                 alu.dst.write = 1;
  3193.                 alu.is_op3 = 1;
  3194.                 alu.last = 1;
  3195.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3196.                 if (r)
  3197.                         return r;
  3198.  
  3199.                 if (ctx->bc->chip_class == CAYMAN) {
  3200.                         for (i = 0; i < 3; i++) {
  3201.                                 /* dst.z = exp(tmp.x) */
  3202.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3203.                                 alu.op = ALU_OP1_EXP_IEEE;
  3204.                                 alu.src[0].sel = ctx->temp_reg;
  3205.                                 alu.src[0].chan = 0;
  3206.                                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  3207.                                 if (i == 2) {
  3208.                                         alu.dst.write = 1;
  3209.                                         alu.last = 1;
  3210.                                 } else
  3211.                                         alu.dst.write = 0;
  3212.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3213.                                 if (r)
  3214.                                         return r;
  3215.                         }
  3216.                 } else {
  3217.                         /* dst.z = exp(tmp.x) */
  3218.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3219.                         alu.op = ALU_OP1_EXP_IEEE;
  3220.                         alu.src[0].sel = ctx->temp_reg;
  3221.                         alu.src[0].chan = 0;
  3222.                         tgsi_dst(ctx, &inst->Dst[0], 2, &alu.dst);
  3223.                         alu.last = 1;
  3224.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3225.                         if (r)
  3226.                                 return r;
  3227.                 }
  3228.         }
  3229.  
  3230.         /* dst.x, <- 1.0  */
  3231.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3232.         alu.op = ALU_OP1_MOV;
  3233.         alu.src[0].sel  = V_SQ_ALU_SRC_1; /*1.0*/
  3234.         alu.src[0].chan = 0;
  3235.         tgsi_dst(ctx, &inst->Dst[0], 0, &alu.dst);
  3236.         alu.dst.write = (inst->Dst[0].Register.WriteMask >> 0) & 1;
  3237.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3238.         if (r)
  3239.                 return r;
  3240.  
  3241.         /* dst.y = max(src.x, 0.0) */
  3242.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3243.         alu.op = ALU_OP2_MAX;
  3244.         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  3245.         alu.src[1].sel  = V_SQ_ALU_SRC_0; /*0.0*/
  3246.         alu.src[1].chan = 0;
  3247.         tgsi_dst(ctx, &inst->Dst[0], 1, &alu.dst);
  3248.         alu.dst.write = (inst->Dst[0].Register.WriteMask >> 1) & 1;
  3249.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3250.         if (r)
  3251.                 return r;
  3252.  
  3253.         /* dst.w, <- 1.0  */
  3254.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3255.         alu.op = ALU_OP1_MOV;
  3256.         alu.src[0].sel  = V_SQ_ALU_SRC_1;
  3257.         alu.src[0].chan = 0;
  3258.         tgsi_dst(ctx, &inst->Dst[0], 3, &alu.dst);
  3259.         alu.dst.write = (inst->Dst[0].Register.WriteMask >> 3) & 1;
  3260.         alu.last = 1;
  3261.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3262.         if (r)
  3263.                 return r;
  3264.  
  3265.         return 0;
  3266. }
  3267.  
  3268. static int tgsi_rsq(struct r600_shader_ctx *ctx)
  3269. {
  3270.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  3271.         struct r600_bytecode_alu alu;
  3272.         int i, r;
  3273.  
  3274.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3275.  
  3276.         /* XXX:
  3277.          * For state trackers other than OpenGL, we'll want to use
  3278.          * _RECIPSQRT_IEEE instead.
  3279.          */
  3280.         alu.op = ALU_OP1_RECIPSQRT_CLAMPED;
  3281.  
  3282.         for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
  3283.                 r600_bytecode_src(&alu.src[i], &ctx->src[i], 0);
  3284.                 r600_bytecode_src_set_abs(&alu.src[i]);
  3285.         }
  3286.         alu.dst.sel = ctx->temp_reg;
  3287.         alu.dst.write = 1;
  3288.         alu.last = 1;
  3289.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3290.         if (r)
  3291.                 return r;
  3292.         /* replicate result */
  3293.         return tgsi_helper_tempx_replicate(ctx);
  3294. }
  3295.  
  3296. static int tgsi_helper_tempx_replicate(struct r600_shader_ctx *ctx)
  3297. {
  3298.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  3299.         struct r600_bytecode_alu alu;
  3300.         int i, r;
  3301.  
  3302.         for (i = 0; i < 4; i++) {
  3303.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3304.                 alu.src[0].sel = ctx->temp_reg;
  3305.                 alu.op = ALU_OP1_MOV;
  3306.                 alu.dst.chan = i;
  3307.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  3308.                 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
  3309.                 if (i == 3)
  3310.                         alu.last = 1;
  3311.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3312.                 if (r)
  3313.                         return r;
  3314.         }
  3315.         return 0;
  3316. }
  3317.  
  3318. static int tgsi_trans_srcx_replicate(struct r600_shader_ctx *ctx)
  3319. {
  3320.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  3321.         struct r600_bytecode_alu alu;
  3322.         int i, r;
  3323.  
  3324.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3325.         alu.op = ctx->inst_info->op;
  3326.         for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
  3327.                 r600_bytecode_src(&alu.src[i], &ctx->src[i], 0);
  3328.         }
  3329.         alu.dst.sel = ctx->temp_reg;
  3330.         alu.dst.write = 1;
  3331.         alu.last = 1;
  3332.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3333.         if (r)
  3334.                 return r;
  3335.         /* replicate result */
  3336.         return tgsi_helper_tempx_replicate(ctx);
  3337. }
  3338.  
  3339. static int cayman_pow(struct r600_shader_ctx *ctx)
  3340. {
  3341.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  3342.         int i, r;
  3343.         struct r600_bytecode_alu alu;
  3344.         int last_slot = (inst->Dst[0].Register.WriteMask & 0x8) ? 4 : 3;
  3345.  
  3346.         for (i = 0; i < 3; i++) {
  3347.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3348.                 alu.op = ALU_OP1_LOG_IEEE;
  3349.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  3350.                 alu.dst.sel = ctx->temp_reg;
  3351.                 alu.dst.chan = i;
  3352.                 alu.dst.write = 1;
  3353.                 if (i == 2)
  3354.                         alu.last = 1;
  3355.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3356.                 if (r)
  3357.                         return r;
  3358.         }
  3359.  
  3360.         /* b * LOG2(a) */
  3361.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3362.         alu.op = ALU_OP2_MUL;
  3363.         r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
  3364.         alu.src[1].sel = ctx->temp_reg;
  3365.         alu.dst.sel = ctx->temp_reg;
  3366.         alu.dst.write = 1;
  3367.         alu.last = 1;
  3368.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3369.         if (r)
  3370.                 return r;
  3371.  
  3372.         for (i = 0; i < last_slot; i++) {
  3373.                 /* POW(a,b) = EXP2(b * LOG2(a))*/
  3374.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3375.                 alu.op = ALU_OP1_EXP_IEEE;
  3376.                 alu.src[0].sel = ctx->temp_reg;
  3377.  
  3378.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  3379.                 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
  3380.                 if (i == last_slot - 1)
  3381.                         alu.last = 1;
  3382.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  3383.                 if (r)
  3384.                         return r;
  3385.         }
  3386.         return 0;
  3387. }
  3388.  
  3389. static int tgsi_pow(struct r600_shader_ctx *ctx)
  3390. {
  3391.         struct r600_bytecode_alu alu;
  3392.         int r;
  3393.  
  3394.         /* LOG2(a) */
  3395.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3396.         alu.op = ALU_OP1_LOG_IEEE;
  3397.         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  3398.         alu.dst.sel = ctx->temp_reg;
  3399.         alu.dst.write = 1;
  3400.         alu.last = 1;
  3401.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3402.         if (r)
  3403.                 return r;
  3404.         /* b * LOG2(a) */
  3405.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3406.         alu.op = ALU_OP2_MUL;
  3407.         r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
  3408.         alu.src[1].sel = ctx->temp_reg;
  3409.         alu.dst.sel = ctx->temp_reg;
  3410.         alu.dst.write = 1;
  3411.         alu.last = 1;
  3412.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3413.         if (r)
  3414.                 return r;
  3415.         /* POW(a,b) = EXP2(b * LOG2(a))*/
  3416.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3417.         alu.op = ALU_OP1_EXP_IEEE;
  3418.         alu.src[0].sel = ctx->temp_reg;
  3419.         alu.dst.sel = ctx->temp_reg;
  3420.         alu.dst.write = 1;
  3421.         alu.last = 1;
  3422.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3423.         if (r)
  3424.                 return r;
  3425.         return tgsi_helper_tempx_replicate(ctx);
  3426. }
  3427.  
  3428. static int tgsi_divmod(struct r600_shader_ctx *ctx, int mod, int signed_op)
  3429. {
  3430.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  3431.         struct r600_bytecode_alu alu;
  3432.         int i, r, j;
  3433.         unsigned write_mask = inst->Dst[0].Register.WriteMask;
  3434.         int tmp0 = ctx->temp_reg;
  3435.         int tmp1 = r600_get_temp(ctx);
  3436.         int tmp2 = r600_get_temp(ctx);
  3437.         int tmp3 = r600_get_temp(ctx);
  3438.         /* Unsigned path:
  3439.          *
  3440.          * we need to represent src1 as src2*q + r, where q - quotient, r - remainder
  3441.          *
  3442.          * 1. tmp0.x = rcp (src2)     = 2^32/src2 + e, where e is rounding error
  3443.          * 2. tmp0.z = lo (tmp0.x * src2)
  3444.          * 3. tmp0.w = -tmp0.z
  3445.          * 4. tmp0.y = hi (tmp0.x * src2)
  3446.          * 5. tmp0.z = (tmp0.y == 0 ? tmp0.w : tmp0.z)      = abs(lo(rcp*src2))
  3447.          * 6. tmp0.w = hi (tmp0.z * tmp0.x)    = e, rounding error
  3448.          * 7. tmp1.x = tmp0.x - tmp0.w
  3449.          * 8. tmp1.y = tmp0.x + tmp0.w
  3450.          * 9. tmp0.x = (tmp0.y == 0 ? tmp1.y : tmp1.x)
  3451.          * 10. tmp0.z = hi(tmp0.x * src1)     = q
  3452.          * 11. tmp0.y = lo (tmp0.z * src2)     = src2*q = src1 - r
  3453.          *
  3454.          * 12. tmp0.w = src1 - tmp0.y       = r
  3455.          * 13. tmp1.x = tmp0.w >= src2          = r >= src2 (uint comparison)
  3456.          * 14. tmp1.y = src1 >= tmp0.y      = r >= 0 (uint comparison)
  3457.          *
  3458.          * if DIV
  3459.          *
  3460.          *   15. tmp1.z = tmp0.z + 1                    = q + 1
  3461.          *   16. tmp1.w = tmp0.z - 1                    = q - 1
  3462.          *
  3463.          * else MOD
  3464.          *
  3465.          *   15. tmp1.z = tmp0.w - src2                 = r - src2
  3466.          *   16. tmp1.w = tmp0.w + src2                 = r + src2
  3467.          *
  3468.          * endif
  3469.          *
  3470.          * 17. tmp1.x = tmp1.x & tmp1.y
  3471.          *
  3472.          * DIV: 18. tmp0.z = tmp1.x==0 ? tmp0.z : tmp1.z
  3473.          * MOD: 18. tmp0.z = tmp1.x==0 ? tmp0.w : tmp1.z
  3474.          *
  3475.          * 19. tmp0.z = tmp1.y==0 ? tmp1.w : tmp0.z
  3476.          * 20. dst = src2==0 ? MAX_UINT : tmp0.z
  3477.          *
  3478.          * Signed path:
  3479.          *
  3480.          * Same as unsigned, using abs values of the operands,
  3481.          * and fixing the sign of the result in the end.
  3482.          */
  3483.  
  3484.         for (i = 0; i < 4; i++) {
  3485.                 if (!(write_mask & (1<<i)))
  3486.                         continue;
  3487.  
  3488.                 if (signed_op) {
  3489.  
  3490.                         /* tmp2.x = -src0 */
  3491.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3492.                         alu.op = ALU_OP2_SUB_INT;
  3493.  
  3494.                         alu.dst.sel = tmp2;
  3495.                         alu.dst.chan = 0;
  3496.                         alu.dst.write = 1;
  3497.  
  3498.                         alu.src[0].sel = V_SQ_ALU_SRC_0;
  3499.  
  3500.                         r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  3501.  
  3502.                         alu.last = 1;
  3503.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3504.                                 return r;
  3505.  
  3506.                         /* tmp2.y = -src1 */
  3507.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3508.                         alu.op = ALU_OP2_SUB_INT;
  3509.  
  3510.                         alu.dst.sel = tmp2;
  3511.                         alu.dst.chan = 1;
  3512.                         alu.dst.write = 1;
  3513.  
  3514.                         alu.src[0].sel = V_SQ_ALU_SRC_0;
  3515.  
  3516.                         r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  3517.  
  3518.                         alu.last = 1;
  3519.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3520.                                 return r;
  3521.  
  3522.                         /* tmp2.z sign bit is set if src0 and src2 signs are different */
  3523.                         /* it will be a sign of the quotient */
  3524.                         if (!mod) {
  3525.  
  3526.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3527.                                 alu.op = ALU_OP2_XOR_INT;
  3528.  
  3529.                                 alu.dst.sel = tmp2;
  3530.                                 alu.dst.chan = 2;
  3531.                                 alu.dst.write = 1;
  3532.  
  3533.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  3534.                                 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  3535.  
  3536.                                 alu.last = 1;
  3537.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3538.                                         return r;
  3539.                         }
  3540.  
  3541.                         /* tmp2.x = |src0| */
  3542.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3543.                         alu.op = ALU_OP3_CNDGE_INT;
  3544.                         alu.is_op3 = 1;
  3545.  
  3546.                         alu.dst.sel = tmp2;
  3547.                         alu.dst.chan = 0;
  3548.                         alu.dst.write = 1;
  3549.  
  3550.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  3551.                         r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  3552.                         alu.src[2].sel = tmp2;
  3553.                         alu.src[2].chan = 0;
  3554.  
  3555.                         alu.last = 1;
  3556.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3557.                                 return r;
  3558.  
  3559.                         /* tmp2.y = |src1| */
  3560.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3561.                         alu.op = ALU_OP3_CNDGE_INT;
  3562.                         alu.is_op3 = 1;
  3563.  
  3564.                         alu.dst.sel = tmp2;
  3565.                         alu.dst.chan = 1;
  3566.                         alu.dst.write = 1;
  3567.  
  3568.                         r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  3569.                         r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  3570.                         alu.src[2].sel = tmp2;
  3571.                         alu.src[2].chan = 1;
  3572.  
  3573.                         alu.last = 1;
  3574.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3575.                                 return r;
  3576.  
  3577.                 }
  3578.  
  3579.                 /* 1. tmp0.x = rcp_u (src2)     = 2^32/src2 + e, where e is rounding error */
  3580.                 if (ctx->bc->chip_class == CAYMAN) {
  3581.                         /* tmp3.x = u2f(src2) */
  3582.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3583.                         alu.op = ALU_OP1_UINT_TO_FLT;
  3584.  
  3585.                         alu.dst.sel = tmp3;
  3586.                         alu.dst.chan = 0;
  3587.                         alu.dst.write = 1;
  3588.  
  3589.                         if (signed_op) {
  3590.                                 alu.src[0].sel = tmp2;
  3591.                                 alu.src[0].chan = 1;
  3592.                         } else {
  3593.                                 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  3594.                         }
  3595.  
  3596.                         alu.last = 1;
  3597.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3598.                                 return r;
  3599.  
  3600.                         /* tmp0.x = recip(tmp3.x) */
  3601.                         for (j = 0 ; j < 3; j++) {
  3602.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3603.                                 alu.op = ALU_OP1_RECIP_IEEE;
  3604.  
  3605.                                 alu.dst.sel = tmp0;
  3606.                                 alu.dst.chan = j;
  3607.                                 alu.dst.write = (j == 0);
  3608.  
  3609.                                 alu.src[0].sel = tmp3;
  3610.                                 alu.src[0].chan = 0;
  3611.  
  3612.                                 if (j == 2)
  3613.                                         alu.last = 1;
  3614.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3615.                                         return r;
  3616.                         }
  3617.  
  3618.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3619.                         alu.op = ALU_OP2_MUL;
  3620.  
  3621.                         alu.src[0].sel = tmp0;
  3622.                         alu.src[0].chan = 0;
  3623.  
  3624.                         alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  3625.                         alu.src[1].value = 0x4f800000;
  3626.  
  3627.                         alu.dst.sel = tmp3;
  3628.                         alu.dst.write = 1;
  3629.                         alu.last = 1;
  3630.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  3631.                         if (r)
  3632.                                 return r;
  3633.  
  3634.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3635.                         alu.op = ALU_OP1_FLT_TO_UINT;
  3636.                  
  3637.                         alu.dst.sel = tmp0;
  3638.                         alu.dst.chan = 0;
  3639.                         alu.dst.write = 1;
  3640.  
  3641.                         alu.src[0].sel = tmp3;
  3642.                         alu.src[0].chan = 0;
  3643.  
  3644.                         alu.last = 1;
  3645.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3646.                                 return r;
  3647.  
  3648.                 } else {
  3649.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3650.                         alu.op = ALU_OP1_RECIP_UINT;
  3651.  
  3652.                         alu.dst.sel = tmp0;
  3653.                         alu.dst.chan = 0;
  3654.                         alu.dst.write = 1;
  3655.  
  3656.                         if (signed_op) {
  3657.                                 alu.src[0].sel = tmp2;
  3658.                                 alu.src[0].chan = 1;
  3659.                         } else {
  3660.                                 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  3661.                         }
  3662.  
  3663.                         alu.last = 1;
  3664.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3665.                                 return r;
  3666.                 }
  3667.  
  3668.                 /* 2. tmp0.z = lo (tmp0.x * src2) */
  3669.                 if (ctx->bc->chip_class == CAYMAN) {
  3670.                         for (j = 0 ; j < 4; j++) {
  3671.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3672.                                 alu.op = ALU_OP2_MULLO_UINT;
  3673.  
  3674.                                 alu.dst.sel = tmp0;
  3675.                                 alu.dst.chan = j;
  3676.                                 alu.dst.write = (j == 2);
  3677.  
  3678.                                 alu.src[0].sel = tmp0;
  3679.                                 alu.src[0].chan = 0;
  3680.                                 if (signed_op) {
  3681.                                         alu.src[1].sel = tmp2;
  3682.                                         alu.src[1].chan = 1;
  3683.                                 } else {
  3684.                                         r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  3685.                                 }
  3686.  
  3687.                                 alu.last = (j == 3);
  3688.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3689.                                         return r;
  3690.                         }
  3691.                 } else {
  3692.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3693.                         alu.op = ALU_OP2_MULLO_UINT;
  3694.  
  3695.                         alu.dst.sel = tmp0;
  3696.                         alu.dst.chan = 2;
  3697.                         alu.dst.write = 1;
  3698.  
  3699.                         alu.src[0].sel = tmp0;
  3700.                         alu.src[0].chan = 0;
  3701.                         if (signed_op) {
  3702.                                 alu.src[1].sel = tmp2;
  3703.                                 alu.src[1].chan = 1;
  3704.                         } else {
  3705.                                 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  3706.                         }
  3707.                        
  3708.                         alu.last = 1;
  3709.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3710.                                 return r;
  3711.                 }
  3712.  
  3713.                 /* 3. tmp0.w = -tmp0.z */
  3714.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3715.                 alu.op = ALU_OP2_SUB_INT;
  3716.  
  3717.                 alu.dst.sel = tmp0;
  3718.                 alu.dst.chan = 3;
  3719.                 alu.dst.write = 1;
  3720.  
  3721.                 alu.src[0].sel = V_SQ_ALU_SRC_0;
  3722.                 alu.src[1].sel = tmp0;
  3723.                 alu.src[1].chan = 2;
  3724.  
  3725.                 alu.last = 1;
  3726.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3727.                         return r;
  3728.  
  3729.                 /* 4. tmp0.y = hi (tmp0.x * src2) */
  3730.                 if (ctx->bc->chip_class == CAYMAN) {
  3731.                         for (j = 0 ; j < 4; j++) {
  3732.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3733.                                 alu.op = ALU_OP2_MULHI_UINT;
  3734.  
  3735.                                 alu.dst.sel = tmp0;
  3736.                                 alu.dst.chan = j;
  3737.                                 alu.dst.write = (j == 1);
  3738.  
  3739.                                 alu.src[0].sel = tmp0;
  3740.                                 alu.src[0].chan = 0;
  3741.  
  3742.                                 if (signed_op) {
  3743.                                         alu.src[1].sel = tmp2;
  3744.                                         alu.src[1].chan = 1;
  3745.                                 } else {
  3746.                                         r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  3747.                                 }
  3748.                                 alu.last = (j == 3);
  3749.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3750.                                         return r;
  3751.                         }
  3752.                 } else {
  3753.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3754.                         alu.op = ALU_OP2_MULHI_UINT;
  3755.  
  3756.                         alu.dst.sel = tmp0;
  3757.                         alu.dst.chan = 1;
  3758.                         alu.dst.write = 1;
  3759.  
  3760.                         alu.src[0].sel = tmp0;
  3761.                         alu.src[0].chan = 0;
  3762.  
  3763.                         if (signed_op) {
  3764.                                 alu.src[1].sel = tmp2;
  3765.                                 alu.src[1].chan = 1;
  3766.                         } else {
  3767.                                 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  3768.                         }
  3769.  
  3770.                         alu.last = 1;
  3771.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3772.                                 return r;
  3773.                 }
  3774.  
  3775.                 /* 5. tmp0.z = (tmp0.y == 0 ? tmp0.w : tmp0.z)      = abs(lo(rcp*src)) */
  3776.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3777.                 alu.op = ALU_OP3_CNDE_INT;
  3778.                 alu.is_op3 = 1;
  3779.  
  3780.                 alu.dst.sel = tmp0;
  3781.                 alu.dst.chan = 2;
  3782.                 alu.dst.write = 1;
  3783.  
  3784.                 alu.src[0].sel = tmp0;
  3785.                 alu.src[0].chan = 1;
  3786.                 alu.src[1].sel = tmp0;
  3787.                 alu.src[1].chan = 3;
  3788.                 alu.src[2].sel = tmp0;
  3789.                 alu.src[2].chan = 2;
  3790.  
  3791.                 alu.last = 1;
  3792.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3793.                         return r;
  3794.  
  3795.                 /* 6. tmp0.w = hi (tmp0.z * tmp0.x)    = e, rounding error */
  3796.                 if (ctx->bc->chip_class == CAYMAN) {
  3797.                         for (j = 0 ; j < 4; j++) {
  3798.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3799.                                 alu.op = ALU_OP2_MULHI_UINT;
  3800.  
  3801.                                 alu.dst.sel = tmp0;
  3802.                                 alu.dst.chan = j;
  3803.                                 alu.dst.write = (j == 3);
  3804.  
  3805.                                 alu.src[0].sel = tmp0;
  3806.                                 alu.src[0].chan = 2;
  3807.  
  3808.                                 alu.src[1].sel = tmp0;
  3809.                                 alu.src[1].chan = 0;
  3810.  
  3811.                                 alu.last = (j == 3);
  3812.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3813.                                         return r;
  3814.                         }
  3815.                 } else {
  3816.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3817.                         alu.op = ALU_OP2_MULHI_UINT;
  3818.  
  3819.                         alu.dst.sel = tmp0;
  3820.                         alu.dst.chan = 3;
  3821.                         alu.dst.write = 1;
  3822.  
  3823.                         alu.src[0].sel = tmp0;
  3824.                         alu.src[0].chan = 2;
  3825.  
  3826.                         alu.src[1].sel = tmp0;
  3827.                         alu.src[1].chan = 0;
  3828.  
  3829.                         alu.last = 1;
  3830.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3831.                                 return r;
  3832.                 }
  3833.  
  3834.                 /* 7. tmp1.x = tmp0.x - tmp0.w */
  3835.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3836.                 alu.op = ALU_OP2_SUB_INT;
  3837.  
  3838.                 alu.dst.sel = tmp1;
  3839.                 alu.dst.chan = 0;
  3840.                 alu.dst.write = 1;
  3841.  
  3842.                 alu.src[0].sel = tmp0;
  3843.                 alu.src[0].chan = 0;
  3844.                 alu.src[1].sel = tmp0;
  3845.                 alu.src[1].chan = 3;
  3846.  
  3847.                 alu.last = 1;
  3848.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3849.                         return r;
  3850.  
  3851.                 /* 8. tmp1.y = tmp0.x + tmp0.w */
  3852.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3853.                 alu.op = ALU_OP2_ADD_INT;
  3854.  
  3855.                 alu.dst.sel = tmp1;
  3856.                 alu.dst.chan = 1;
  3857.                 alu.dst.write = 1;
  3858.  
  3859.                 alu.src[0].sel = tmp0;
  3860.                 alu.src[0].chan = 0;
  3861.                 alu.src[1].sel = tmp0;
  3862.                 alu.src[1].chan = 3;
  3863.  
  3864.                 alu.last = 1;
  3865.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3866.                         return r;
  3867.  
  3868.                 /* 9. tmp0.x = (tmp0.y == 0 ? tmp1.y : tmp1.x) */
  3869.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3870.                 alu.op = ALU_OP3_CNDE_INT;
  3871.                 alu.is_op3 = 1;
  3872.  
  3873.                 alu.dst.sel = tmp0;
  3874.                 alu.dst.chan = 0;
  3875.                 alu.dst.write = 1;
  3876.  
  3877.                 alu.src[0].sel = tmp0;
  3878.                 alu.src[0].chan = 1;
  3879.                 alu.src[1].sel = tmp1;
  3880.                 alu.src[1].chan = 1;
  3881.                 alu.src[2].sel = tmp1;
  3882.                 alu.src[2].chan = 0;
  3883.  
  3884.                 alu.last = 1;
  3885.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3886.                         return r;
  3887.  
  3888.                 /* 10. tmp0.z = hi(tmp0.x * src1)     = q */
  3889.                 if (ctx->bc->chip_class == CAYMAN) {
  3890.                         for (j = 0 ; j < 4; j++) {
  3891.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3892.                                 alu.op = ALU_OP2_MULHI_UINT;
  3893.  
  3894.                                 alu.dst.sel = tmp0;
  3895.                                 alu.dst.chan = j;
  3896.                                 alu.dst.write = (j == 2);
  3897.  
  3898.                                 alu.src[0].sel = tmp0;
  3899.                                 alu.src[0].chan = 0;
  3900.  
  3901.                                 if (signed_op) {
  3902.                                         alu.src[1].sel = tmp2;
  3903.                                         alu.src[1].chan = 0;
  3904.                                 } else {
  3905.                                         r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  3906.                                 }
  3907.  
  3908.                                 alu.last = (j == 3);
  3909.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3910.                                         return r;
  3911.                         }
  3912.                 } else {
  3913.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3914.                         alu.op = ALU_OP2_MULHI_UINT;
  3915.  
  3916.                         alu.dst.sel = tmp0;
  3917.                         alu.dst.chan = 2;
  3918.                         alu.dst.write = 1;
  3919.  
  3920.                         alu.src[0].sel = tmp0;
  3921.                         alu.src[0].chan = 0;
  3922.  
  3923.                         if (signed_op) {
  3924.                                 alu.src[1].sel = tmp2;
  3925.                                 alu.src[1].chan = 0;
  3926.                         } else {
  3927.                                 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  3928.                         }
  3929.  
  3930.                         alu.last = 1;
  3931.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3932.                                 return r;
  3933.                 }
  3934.  
  3935.                 /* 11. tmp0.y = lo (src2 * tmp0.z)     = src2*q = src1 - r */
  3936.                 if (ctx->bc->chip_class == CAYMAN) {
  3937.                         for (j = 0 ; j < 4; j++) {
  3938.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3939.                                 alu.op = ALU_OP2_MULLO_UINT;
  3940.  
  3941.                                 alu.dst.sel = tmp0;
  3942.                                 alu.dst.chan = j;
  3943.                                 alu.dst.write = (j == 1);
  3944.  
  3945.                                 if (signed_op) {
  3946.                                         alu.src[0].sel = tmp2;
  3947.                                         alu.src[0].chan = 1;
  3948.                                 } else {
  3949.                                         r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  3950.                                 }
  3951.  
  3952.                                 alu.src[1].sel = tmp0;
  3953.                                 alu.src[1].chan = 2;
  3954.  
  3955.                                 alu.last = (j == 3);
  3956.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3957.                                         return r;
  3958.                         }
  3959.                 } else {
  3960.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3961.                         alu.op = ALU_OP2_MULLO_UINT;
  3962.  
  3963.                         alu.dst.sel = tmp0;
  3964.                         alu.dst.chan = 1;
  3965.                         alu.dst.write = 1;
  3966.  
  3967.                         if (signed_op) {
  3968.                                 alu.src[0].sel = tmp2;
  3969.                                 alu.src[0].chan = 1;
  3970.                         } else {
  3971.                                 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  3972.                         }
  3973.                        
  3974.                         alu.src[1].sel = tmp0;
  3975.                         alu.src[1].chan = 2;
  3976.  
  3977.                         alu.last = 1;
  3978.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  3979.                                 return r;
  3980.                 }
  3981.  
  3982.                 /* 12. tmp0.w = src1 - tmp0.y       = r */
  3983.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  3984.                 alu.op = ALU_OP2_SUB_INT;
  3985.  
  3986.                 alu.dst.sel = tmp0;
  3987.                 alu.dst.chan = 3;
  3988.                 alu.dst.write = 1;
  3989.  
  3990.                 if (signed_op) {
  3991.                         alu.src[0].sel = tmp2;
  3992.                         alu.src[0].chan = 0;
  3993.                 } else {
  3994.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  3995.                 }
  3996.  
  3997.                 alu.src[1].sel = tmp0;
  3998.                 alu.src[1].chan = 1;
  3999.  
  4000.                 alu.last = 1;
  4001.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4002.                         return r;
  4003.  
  4004.                 /* 13. tmp1.x = tmp0.w >= src2          = r >= src2 */
  4005.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4006.                 alu.op = ALU_OP2_SETGE_UINT;
  4007.  
  4008.                 alu.dst.sel = tmp1;
  4009.                 alu.dst.chan = 0;
  4010.                 alu.dst.write = 1;
  4011.  
  4012.                 alu.src[0].sel = tmp0;
  4013.                 alu.src[0].chan = 3;
  4014.                 if (signed_op) {
  4015.                         alu.src[1].sel = tmp2;
  4016.                         alu.src[1].chan = 1;
  4017.                 } else {
  4018.                         r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  4019.                 }
  4020.  
  4021.                 alu.last = 1;
  4022.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4023.                         return r;
  4024.  
  4025.                 /* 14. tmp1.y = src1 >= tmp0.y       = r >= 0 */
  4026.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4027.                 alu.op = ALU_OP2_SETGE_UINT;
  4028.  
  4029.                 alu.dst.sel = tmp1;
  4030.                 alu.dst.chan = 1;
  4031.                 alu.dst.write = 1;
  4032.  
  4033.                 if (signed_op) {
  4034.                         alu.src[0].sel = tmp2;
  4035.                         alu.src[0].chan = 0;
  4036.                 } else {
  4037.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  4038.                 }
  4039.  
  4040.                 alu.src[1].sel = tmp0;
  4041.                 alu.src[1].chan = 1;
  4042.  
  4043.                 alu.last = 1;
  4044.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4045.                         return r;
  4046.  
  4047.                 if (mod) { /* UMOD */
  4048.  
  4049.                         /* 15. tmp1.z = tmp0.w - src2                   = r - src2 */
  4050.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4051.                         alu.op = ALU_OP2_SUB_INT;
  4052.  
  4053.                         alu.dst.sel = tmp1;
  4054.                         alu.dst.chan = 2;
  4055.                         alu.dst.write = 1;
  4056.  
  4057.                         alu.src[0].sel = tmp0;
  4058.                         alu.src[0].chan = 3;
  4059.  
  4060.                         if (signed_op) {
  4061.                                 alu.src[1].sel = tmp2;
  4062.                                 alu.src[1].chan = 1;
  4063.                         } else {
  4064.                                 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  4065.                         }
  4066.  
  4067.                         alu.last = 1;
  4068.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4069.                                 return r;
  4070.  
  4071.                         /* 16. tmp1.w = tmp0.w + src2                   = r + src2 */
  4072.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4073.                         alu.op = ALU_OP2_ADD_INT;
  4074.  
  4075.                         alu.dst.sel = tmp1;
  4076.                         alu.dst.chan = 3;
  4077.                         alu.dst.write = 1;
  4078.  
  4079.                         alu.src[0].sel = tmp0;
  4080.                         alu.src[0].chan = 3;
  4081.                         if (signed_op) {
  4082.                                 alu.src[1].sel = tmp2;
  4083.                                 alu.src[1].chan = 1;
  4084.                         } else {
  4085.                                 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  4086.                         }
  4087.  
  4088.                         alu.last = 1;
  4089.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4090.                                 return r;
  4091.  
  4092.                 } else { /* UDIV */
  4093.  
  4094.                         /* 15. tmp1.z = tmp0.z + 1       = q + 1       DIV */
  4095.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4096.                         alu.op = ALU_OP2_ADD_INT;
  4097.  
  4098.                         alu.dst.sel = tmp1;
  4099.                         alu.dst.chan = 2;
  4100.                         alu.dst.write = 1;
  4101.  
  4102.                         alu.src[0].sel = tmp0;
  4103.                         alu.src[0].chan = 2;
  4104.                         alu.src[1].sel = V_SQ_ALU_SRC_1_INT;
  4105.  
  4106.                         alu.last = 1;
  4107.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4108.                                 return r;
  4109.  
  4110.                         /* 16. tmp1.w = tmp0.z - 1                      = q - 1 */
  4111.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4112.                         alu.op = ALU_OP2_ADD_INT;
  4113.  
  4114.                         alu.dst.sel = tmp1;
  4115.                         alu.dst.chan = 3;
  4116.                         alu.dst.write = 1;
  4117.  
  4118.                         alu.src[0].sel = tmp0;
  4119.                         alu.src[0].chan = 2;
  4120.                         alu.src[1].sel = V_SQ_ALU_SRC_M_1_INT;
  4121.  
  4122.                         alu.last = 1;
  4123.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4124.                                 return r;
  4125.  
  4126.                 }
  4127.  
  4128.                 /* 17. tmp1.x = tmp1.x & tmp1.y */
  4129.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4130.                 alu.op = ALU_OP2_AND_INT;
  4131.  
  4132.                 alu.dst.sel = tmp1;
  4133.                 alu.dst.chan = 0;
  4134.                 alu.dst.write = 1;
  4135.  
  4136.                 alu.src[0].sel = tmp1;
  4137.                 alu.src[0].chan = 0;
  4138.                 alu.src[1].sel = tmp1;
  4139.                 alu.src[1].chan = 1;
  4140.  
  4141.                 alu.last = 1;
  4142.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4143.                         return r;
  4144.  
  4145.                 /* 18. tmp0.z = tmp1.x==0 ? tmp0.z : tmp1.z    DIV */
  4146.                 /* 18. tmp0.z = tmp1.x==0 ? tmp0.w : tmp1.z    MOD */
  4147.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4148.                 alu.op = ALU_OP3_CNDE_INT;
  4149.                 alu.is_op3 = 1;
  4150.  
  4151.                 alu.dst.sel = tmp0;
  4152.                 alu.dst.chan = 2;
  4153.                 alu.dst.write = 1;
  4154.  
  4155.                 alu.src[0].sel = tmp1;
  4156.                 alu.src[0].chan = 0;
  4157.                 alu.src[1].sel = tmp0;
  4158.                 alu.src[1].chan = mod ? 3 : 2;
  4159.                 alu.src[2].sel = tmp1;
  4160.                 alu.src[2].chan = 2;
  4161.  
  4162.                 alu.last = 1;
  4163.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4164.                         return r;
  4165.  
  4166.                 /* 19. tmp0.z = tmp1.y==0 ? tmp1.w : tmp0.z */
  4167.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4168.                 alu.op = ALU_OP3_CNDE_INT;
  4169.                 alu.is_op3 = 1;
  4170.  
  4171.                 if (signed_op) {
  4172.                         alu.dst.sel = tmp0;
  4173.                         alu.dst.chan = 2;
  4174.                         alu.dst.write = 1;
  4175.                 } else {
  4176.                         tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4177.                 }
  4178.  
  4179.                 alu.src[0].sel = tmp1;
  4180.                 alu.src[0].chan = 1;
  4181.                 alu.src[1].sel = tmp1;
  4182.                 alu.src[1].chan = 3;
  4183.                 alu.src[2].sel = tmp0;
  4184.                 alu.src[2].chan = 2;
  4185.  
  4186.                 alu.last = 1;
  4187.                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4188.                         return r;
  4189.  
  4190.                 if (signed_op) {
  4191.  
  4192.                         /* fix the sign of the result */
  4193.  
  4194.                         if (mod) {
  4195.  
  4196.                                 /* tmp0.x = -tmp0.z */
  4197.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4198.                                 alu.op = ALU_OP2_SUB_INT;
  4199.  
  4200.                                 alu.dst.sel = tmp0;
  4201.                                 alu.dst.chan = 0;
  4202.                                 alu.dst.write = 1;
  4203.  
  4204.                                 alu.src[0].sel = V_SQ_ALU_SRC_0;
  4205.                                 alu.src[1].sel = tmp0;
  4206.                                 alu.src[1].chan = 2;
  4207.  
  4208.                                 alu.last = 1;
  4209.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4210.                                         return r;
  4211.  
  4212.                                 /* sign of the remainder is the same as the sign of src0 */
  4213.                                 /* tmp0.x = src0>=0 ? tmp0.z : tmp0.x */
  4214.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4215.                                 alu.op = ALU_OP3_CNDGE_INT;
  4216.                                 alu.is_op3 = 1;
  4217.  
  4218.                                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4219.  
  4220.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  4221.                                 alu.src[1].sel = tmp0;
  4222.                                 alu.src[1].chan = 2;
  4223.                                 alu.src[2].sel = tmp0;
  4224.                                 alu.src[2].chan = 0;
  4225.  
  4226.                                 alu.last = 1;
  4227.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4228.                                         return r;
  4229.  
  4230.                         } else {
  4231.  
  4232.                                 /* tmp0.x = -tmp0.z */
  4233.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4234.                                 alu.op = ALU_OP2_SUB_INT;
  4235.  
  4236.                                 alu.dst.sel = tmp0;
  4237.                                 alu.dst.chan = 0;
  4238.                                 alu.dst.write = 1;
  4239.  
  4240.                                 alu.src[0].sel = V_SQ_ALU_SRC_0;
  4241.                                 alu.src[1].sel = tmp0;
  4242.                                 alu.src[1].chan = 2;
  4243.  
  4244.                                 alu.last = 1;
  4245.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4246.                                         return r;
  4247.  
  4248.                                 /* fix the quotient sign (same as the sign of src0*src1) */
  4249.                                 /* tmp0.x = tmp2.z>=0 ? tmp0.z : tmp0.x */
  4250.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4251.                                 alu.op = ALU_OP3_CNDGE_INT;
  4252.                                 alu.is_op3 = 1;
  4253.  
  4254.                                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4255.  
  4256.                                 alu.src[0].sel = tmp2;
  4257.                                 alu.src[0].chan = 2;
  4258.                                 alu.src[1].sel = tmp0;
  4259.                                 alu.src[1].chan = 2;
  4260.                                 alu.src[2].sel = tmp0;
  4261.                                 alu.src[2].chan = 0;
  4262.  
  4263.                                 alu.last = 1;
  4264.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  4265.                                         return r;
  4266.                         }
  4267.                 }
  4268.         }
  4269.         return 0;
  4270. }
  4271.  
  4272. static int tgsi_udiv(struct r600_shader_ctx *ctx)
  4273. {
  4274.         return tgsi_divmod(ctx, 0, 0);
  4275. }
  4276.  
  4277. static int tgsi_umod(struct r600_shader_ctx *ctx)
  4278. {
  4279.         return tgsi_divmod(ctx, 1, 0);
  4280. }
  4281.  
  4282. static int tgsi_idiv(struct r600_shader_ctx *ctx)
  4283. {
  4284.         return tgsi_divmod(ctx, 0, 1);
  4285. }
  4286.  
  4287. static int tgsi_imod(struct r600_shader_ctx *ctx)
  4288. {
  4289.         return tgsi_divmod(ctx, 1, 1);
  4290. }
  4291.  
  4292.  
  4293. static int tgsi_f2i(struct r600_shader_ctx *ctx)
  4294. {
  4295.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4296.         struct r600_bytecode_alu alu;
  4297.         int i, r;
  4298.         unsigned write_mask = inst->Dst[0].Register.WriteMask;
  4299.         int last_inst = tgsi_last_instruction(write_mask);
  4300.  
  4301.         for (i = 0; i < 4; i++) {
  4302.                 if (!(write_mask & (1<<i)))
  4303.                         continue;
  4304.  
  4305.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4306.                 alu.op = ALU_OP1_TRUNC;
  4307.  
  4308.                 alu.dst.sel = ctx->temp_reg;
  4309.                 alu.dst.chan = i;
  4310.                 alu.dst.write = 1;
  4311.  
  4312.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  4313.                 if (i == last_inst)
  4314.                         alu.last = 1;
  4315.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4316.                 if (r)
  4317.                         return r;
  4318.         }
  4319.  
  4320.         for (i = 0; i < 4; i++) {
  4321.                 if (!(write_mask & (1<<i)))
  4322.                         continue;
  4323.  
  4324.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4325.                 alu.op = ctx->inst_info->op;
  4326.  
  4327.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4328.  
  4329.                 alu.src[0].sel = ctx->temp_reg;
  4330.                 alu.src[0].chan = i;
  4331.  
  4332.                 if (i == last_inst || alu.op == ALU_OP1_FLT_TO_UINT)
  4333.                         alu.last = 1;
  4334.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4335.                 if (r)
  4336.                         return r;
  4337.         }
  4338.  
  4339.         return 0;
  4340. }
  4341.  
  4342. static int tgsi_iabs(struct r600_shader_ctx *ctx)
  4343. {
  4344.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4345.         struct r600_bytecode_alu alu;
  4346.         int i, r;
  4347.         unsigned write_mask = inst->Dst[0].Register.WriteMask;
  4348.         int last_inst = tgsi_last_instruction(write_mask);
  4349.  
  4350.         /* tmp = -src */
  4351.         for (i = 0; i < 4; i++) {
  4352.                 if (!(write_mask & (1<<i)))
  4353.                         continue;
  4354.  
  4355.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4356.                 alu.op = ALU_OP2_SUB_INT;
  4357.  
  4358.                 alu.dst.sel = ctx->temp_reg;
  4359.                 alu.dst.chan = i;
  4360.                 alu.dst.write = 1;
  4361.  
  4362.                 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  4363.                 alu.src[0].sel = V_SQ_ALU_SRC_0;
  4364.  
  4365.                 if (i == last_inst)
  4366.                         alu.last = 1;
  4367.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4368.                 if (r)
  4369.                         return r;
  4370.         }
  4371.  
  4372.         /* dst = (src >= 0 ? src : tmp) */
  4373.         for (i = 0; i < 4; i++) {
  4374.                 if (!(write_mask & (1<<i)))
  4375.                         continue;
  4376.  
  4377.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4378.                 alu.op = ALU_OP3_CNDGE_INT;
  4379.                 alu.is_op3 = 1;
  4380.                 alu.dst.write = 1;
  4381.  
  4382.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4383.  
  4384.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  4385.                 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  4386.                 alu.src[2].sel = ctx->temp_reg;
  4387.                 alu.src[2].chan = i;
  4388.  
  4389.                 if (i == last_inst)
  4390.                         alu.last = 1;
  4391.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4392.                 if (r)
  4393.                         return r;
  4394.         }
  4395.         return 0;
  4396. }
  4397.  
  4398. static int tgsi_issg(struct r600_shader_ctx *ctx)
  4399. {
  4400.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4401.         struct r600_bytecode_alu alu;
  4402.         int i, r;
  4403.         unsigned write_mask = inst->Dst[0].Register.WriteMask;
  4404.         int last_inst = tgsi_last_instruction(write_mask);
  4405.  
  4406.         /* tmp = (src >= 0 ? src : -1) */
  4407.         for (i = 0; i < 4; i++) {
  4408.                 if (!(write_mask & (1<<i)))
  4409.                         continue;
  4410.  
  4411.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4412.                 alu.op = ALU_OP3_CNDGE_INT;
  4413.                 alu.is_op3 = 1;
  4414.  
  4415.                 alu.dst.sel = ctx->temp_reg;
  4416.                 alu.dst.chan = i;
  4417.                 alu.dst.write = 1;
  4418.  
  4419.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  4420.                 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  4421.                 alu.src[2].sel = V_SQ_ALU_SRC_M_1_INT;
  4422.  
  4423.                 if (i == last_inst)
  4424.                         alu.last = 1;
  4425.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4426.                 if (r)
  4427.                         return r;
  4428.         }
  4429.  
  4430.         /* dst = (tmp > 0 ? 1 : tmp) */
  4431.         for (i = 0; i < 4; i++) {
  4432.                 if (!(write_mask & (1<<i)))
  4433.                         continue;
  4434.  
  4435.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4436.                 alu.op = ALU_OP3_CNDGT_INT;
  4437.                 alu.is_op3 = 1;
  4438.                 alu.dst.write = 1;
  4439.  
  4440.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4441.  
  4442.                 alu.src[0].sel = ctx->temp_reg;
  4443.                 alu.src[0].chan = i;
  4444.  
  4445.                 alu.src[1].sel = V_SQ_ALU_SRC_1_INT;
  4446.  
  4447.                 alu.src[2].sel = ctx->temp_reg;
  4448.                 alu.src[2].chan = i;
  4449.  
  4450.                 if (i == last_inst)
  4451.                         alu.last = 1;
  4452.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4453.                 if (r)
  4454.                         return r;
  4455.         }
  4456.         return 0;
  4457. }
  4458.  
  4459.  
  4460.  
  4461. static int tgsi_ssg(struct r600_shader_ctx *ctx)
  4462. {
  4463.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4464.         struct r600_bytecode_alu alu;
  4465.         int i, r;
  4466.  
  4467.         /* tmp = (src > 0 ? 1 : src) */
  4468.         for (i = 0; i < 4; i++) {
  4469.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4470.                 alu.op = ALU_OP3_CNDGT;
  4471.                 alu.is_op3 = 1;
  4472.  
  4473.                 alu.dst.sel = ctx->temp_reg;
  4474.                 alu.dst.chan = i;
  4475.  
  4476.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  4477.                 alu.src[1].sel = V_SQ_ALU_SRC_1;
  4478.                 r600_bytecode_src(&alu.src[2], &ctx->src[0], i);
  4479.  
  4480.                 if (i == 3)
  4481.                         alu.last = 1;
  4482.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4483.                 if (r)
  4484.                         return r;
  4485.         }
  4486.  
  4487.         /* dst = (-tmp > 0 ? -1 : tmp) */
  4488.         for (i = 0; i < 4; i++) {
  4489.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4490.                 alu.op = ALU_OP3_CNDGT;
  4491.                 alu.is_op3 = 1;
  4492.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4493.  
  4494.                 alu.src[0].sel = ctx->temp_reg;
  4495.                 alu.src[0].chan = i;
  4496.                 alu.src[0].neg = 1;
  4497.  
  4498.                 alu.src[1].sel = V_SQ_ALU_SRC_1;
  4499.                 alu.src[1].neg = 1;
  4500.  
  4501.                 alu.src[2].sel = ctx->temp_reg;
  4502.                 alu.src[2].chan = i;
  4503.  
  4504.                 if (i == 3)
  4505.                         alu.last = 1;
  4506.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4507.                 if (r)
  4508.                         return r;
  4509.         }
  4510.         return 0;
  4511. }
  4512.  
  4513. static int tgsi_bfi(struct r600_shader_ctx *ctx)
  4514. {
  4515.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4516.         struct r600_bytecode_alu alu;
  4517.         int i, r, t1, t2;
  4518.  
  4519.         unsigned write_mask = inst->Dst[0].Register.WriteMask;
  4520.         int last_inst = tgsi_last_instruction(write_mask);
  4521.  
  4522.         t1 = ctx->temp_reg;
  4523.  
  4524.         for (i = 0; i < 4; i++) {
  4525.                 if (!(write_mask & (1<<i)))
  4526.                         continue;
  4527.  
  4528.                 /* create mask tmp */
  4529.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4530.                 alu.op = ALU_OP2_BFM_INT;
  4531.                 alu.dst.sel = t1;
  4532.                 alu.dst.chan = i;
  4533.                 alu.dst.write = 1;
  4534.                 alu.last = i == last_inst;
  4535.  
  4536.                 r600_bytecode_src(&alu.src[0], &ctx->src[3], i);
  4537.                 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
  4538.  
  4539.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4540.                 if (r)
  4541.                         return r;
  4542.         }
  4543.  
  4544.         t2 = r600_get_temp(ctx);
  4545.  
  4546.         for (i = 0; i < 4; i++) {
  4547.                 if (!(write_mask & (1<<i)))
  4548.                         continue;
  4549.  
  4550.                 /* shift insert left */
  4551.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4552.                 alu.op = ALU_OP2_LSHL_INT;
  4553.                 alu.dst.sel = t2;
  4554.                 alu.dst.chan = i;
  4555.                 alu.dst.write = 1;
  4556.                 alu.last = i == last_inst;
  4557.  
  4558.                 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  4559.                 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
  4560.  
  4561.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4562.                 if (r)
  4563.                         return r;
  4564.         }
  4565.  
  4566.         for (i = 0; i < 4; i++) {
  4567.                 if (!(write_mask & (1<<i)))
  4568.                         continue;
  4569.  
  4570.                 /* actual bitfield insert */
  4571.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4572.                 alu.op = ALU_OP3_BFI_INT;
  4573.                 alu.is_op3 = 1;
  4574.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4575.                 alu.dst.chan = i;
  4576.                 alu.dst.write = 1;
  4577.                 alu.last = i == last_inst;
  4578.  
  4579.                 alu.src[0].sel = t1;
  4580.                 alu.src[0].chan = i;
  4581.                 alu.src[1].sel = t2;
  4582.                 alu.src[1].chan = i;
  4583.                 r600_bytecode_src(&alu.src[2], &ctx->src[0], i);
  4584.  
  4585.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4586.                 if (r)
  4587.                         return r;
  4588.         }
  4589.  
  4590.         return 0;
  4591. }
  4592.  
  4593. static int tgsi_msb(struct r600_shader_ctx *ctx)
  4594. {
  4595.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4596.         struct r600_bytecode_alu alu;
  4597.         int i, r, t1, t2;
  4598.  
  4599.         unsigned write_mask = inst->Dst[0].Register.WriteMask;
  4600.         int last_inst = tgsi_last_instruction(write_mask);
  4601.  
  4602.         assert(ctx->inst_info->op == ALU_OP1_FFBH_INT ||
  4603.                 ctx->inst_info->op == ALU_OP1_FFBH_UINT);
  4604.  
  4605.         t1 = ctx->temp_reg;
  4606.  
  4607.         /* bit position is indexed from lsb by TGSI, and from msb by the hardware */
  4608.         for (i = 0; i < 4; i++) {
  4609.                 if (!(write_mask & (1<<i)))
  4610.                         continue;
  4611.  
  4612.                 /* t1 = FFBH_INT / FFBH_UINT */
  4613.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4614.                 alu.op = ctx->inst_info->op;
  4615.                 alu.dst.sel = t1;
  4616.                 alu.dst.chan = i;
  4617.                 alu.dst.write = 1;
  4618.                 alu.last = i == last_inst;
  4619.  
  4620.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  4621.  
  4622.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4623.                 if (r)
  4624.                         return r;
  4625.         }
  4626.  
  4627.         t2 = r600_get_temp(ctx);
  4628.  
  4629.         for (i = 0; i < 4; i++) {
  4630.                 if (!(write_mask & (1<<i)))
  4631.                         continue;
  4632.  
  4633.                 /* t2 = 31 - t1 */
  4634.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4635.                 alu.op = ALU_OP2_SUB_INT;
  4636.                 alu.dst.sel = t2;
  4637.                 alu.dst.chan = i;
  4638.                 alu.dst.write = 1;
  4639.                 alu.last = i == last_inst;
  4640.  
  4641.                 alu.src[0].sel = V_SQ_ALU_SRC_LITERAL;
  4642.                 alu.src[0].value = 31;
  4643.                 alu.src[1].sel = t1;
  4644.                 alu.src[1].chan = i;
  4645.  
  4646.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4647.                 if (r)
  4648.                         return r;
  4649.         }
  4650.  
  4651.         for (i = 0; i < 4; i++) {
  4652.                 if (!(write_mask & (1<<i)))
  4653.                         continue;
  4654.  
  4655.                 /* result = t1 >= 0 ? t2 : t1 */
  4656.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4657.                 alu.op = ALU_OP3_CNDGE_INT;
  4658.                 alu.is_op3 = 1;
  4659.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4660.                 alu.dst.chan = i;
  4661.                 alu.dst.write = 1;
  4662.                 alu.last = i == last_inst;
  4663.  
  4664.                 alu.src[0].sel = t1;
  4665.                 alu.src[0].chan = i;
  4666.                 alu.src[1].sel = t2;
  4667.                 alu.src[1].chan = i;
  4668.                 alu.src[2].sel = t1;
  4669.                 alu.src[2].chan = i;
  4670.  
  4671.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4672.                 if (r)
  4673.                         return r;
  4674.         }
  4675.  
  4676.         return 0;
  4677. }
  4678.  
  4679. static int tgsi_interp_egcm(struct r600_shader_ctx *ctx)
  4680. {
  4681.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4682.         struct r600_bytecode_alu alu;
  4683.         int r, i = 0, k, interp_gpr, interp_base_chan, tmp, lasti;
  4684.         unsigned location;
  4685.         int input;
  4686.  
  4687.         assert(inst->Src[0].Register.File == TGSI_FILE_INPUT);
  4688.  
  4689.         input = inst->Src[0].Register.Index;
  4690.  
  4691.         /* Interpolators have been marked for use already by allocate_system_value_inputs */
  4692.         if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
  4693.                 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  4694.                 location = TGSI_INTERPOLATE_LOC_CENTER; /* sample offset will be added explicitly */
  4695.         }
  4696.         else {
  4697.                 location = TGSI_INTERPOLATE_LOC_CENTROID;
  4698.         }
  4699.  
  4700.         k = eg_get_interpolator_index(ctx->shader->input[input].interpolate, location);
  4701.         if (k < 0)
  4702.                 k = 0;
  4703.         interp_gpr = ctx->eg_interpolators[k].ij_index / 2;
  4704.         interp_base_chan = 2 * (ctx->eg_interpolators[k].ij_index % 2);
  4705.  
  4706.         /* NOTE: currently offset is not perspective correct */
  4707.         if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
  4708.                 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  4709.                 int sample_gpr = -1;
  4710.                 int gradientsH, gradientsV;
  4711.                 struct r600_bytecode_tex tex;
  4712.  
  4713.                 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  4714.                         sample_gpr = load_sample_position(ctx, &ctx->src[1], ctx->src[1].swizzle[0]);
  4715.                 }
  4716.  
  4717.                 gradientsH = r600_get_temp(ctx);
  4718.                 gradientsV = r600_get_temp(ctx);
  4719.                 for (i = 0; i < 2; i++) {
  4720.                         memset(&tex, 0, sizeof(struct r600_bytecode_tex));
  4721.                         tex.op = i == 0 ? FETCH_OP_GET_GRADIENTS_H : FETCH_OP_GET_GRADIENTS_V;
  4722.                         tex.src_gpr = interp_gpr;
  4723.                         tex.src_sel_x = interp_base_chan + 0;
  4724.                         tex.src_sel_y = interp_base_chan + 1;
  4725.                         tex.src_sel_z = 0;
  4726.                         tex.src_sel_w = 0;
  4727.                         tex.dst_gpr = i == 0 ? gradientsH : gradientsV;
  4728.                         tex.dst_sel_x = 0;
  4729.                         tex.dst_sel_y = 1;
  4730.                         tex.dst_sel_z = 7;
  4731.                         tex.dst_sel_w = 7;
  4732.                         tex.inst_mod = 1; // Use per pixel gradient calculation
  4733.                         tex.sampler_id = 0;
  4734.                         tex.resource_id = tex.sampler_id;
  4735.                         r = r600_bytecode_add_tex(ctx->bc, &tex);
  4736.                         if (r)
  4737.                                 return r;
  4738.                 }
  4739.  
  4740.                 for (i = 0; i < 2; i++) {
  4741.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4742.                         alu.op = ALU_OP3_MULADD;
  4743.                         alu.is_op3 = 1;
  4744.                         alu.src[0].sel = gradientsH;
  4745.                         alu.src[0].chan = i;
  4746.                         if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  4747.                                 alu.src[1].sel = sample_gpr;
  4748.                                 alu.src[1].chan = 2;
  4749.                         }
  4750.                         else {
  4751.                                 r600_bytecode_src(&alu.src[1], &ctx->src[1], 0);
  4752.                         }
  4753.                         alu.src[2].sel = interp_gpr;
  4754.                         alu.src[2].chan = interp_base_chan + i;
  4755.                         alu.dst.sel = ctx->temp_reg;
  4756.                         alu.dst.chan = i;
  4757.                         alu.last = i == 1;
  4758.  
  4759.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  4760.                         if (r)
  4761.                                 return r;
  4762.                 }
  4763.  
  4764.                 for (i = 0; i < 2; i++) {
  4765.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4766.                         alu.op = ALU_OP3_MULADD;
  4767.                         alu.is_op3 = 1;
  4768.                         alu.src[0].sel = gradientsV;
  4769.                         alu.src[0].chan = i;
  4770.                         if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  4771.                                 alu.src[1].sel = sample_gpr;
  4772.                                 alu.src[1].chan = 3;
  4773.                         }
  4774.                         else {
  4775.                                 r600_bytecode_src(&alu.src[1], &ctx->src[1], 1);
  4776.                         }
  4777.                         alu.src[2].sel = ctx->temp_reg;
  4778.                         alu.src[2].chan = i;
  4779.                         alu.dst.sel = ctx->temp_reg;
  4780.                         alu.dst.chan = i;
  4781.                         alu.last = i == 1;
  4782.  
  4783.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  4784.                         if (r)
  4785.                                 return r;
  4786.                 }
  4787.         }
  4788.  
  4789.         tmp = r600_get_temp(ctx);
  4790.         for (i = 0; i < 8; i++) {
  4791.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4792.                 alu.op = i < 4 ? ALU_OP2_INTERP_ZW : ALU_OP2_INTERP_XY;
  4793.  
  4794.                 alu.dst.sel = tmp;
  4795.                 if ((i > 1 && i < 6)) {
  4796.                         alu.dst.write = 1;
  4797.                 }
  4798.                 else {
  4799.                         alu.dst.write = 0;
  4800.                 }
  4801.                 alu.dst.chan = i % 4;
  4802.  
  4803.                 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
  4804.                         inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
  4805.                         alu.src[0].sel = ctx->temp_reg;
  4806.                         alu.src[0].chan = 1 - (i % 2);
  4807.                 } else {
  4808.                         alu.src[0].sel = interp_gpr;
  4809.                         alu.src[0].chan = interp_base_chan + 1 - (i % 2);
  4810.                 }
  4811.                 alu.src[1].sel = V_SQ_ALU_SRC_PARAM_BASE + ctx->shader->input[input].lds_pos;
  4812.                 alu.src[1].chan = 0;
  4813.  
  4814.                 alu.last = i % 4 == 3;
  4815.                 alu.bank_swizzle_force = SQ_ALU_VEC_210;
  4816.  
  4817.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4818.                 if (r)
  4819.                         return r;
  4820.         }
  4821.  
  4822.         // INTERP can't swizzle dst
  4823.         lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  4824.         for (i = 0; i <= lasti; i++) {
  4825.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  4826.                         continue;
  4827.  
  4828.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4829.                 alu.op = ALU_OP1_MOV;
  4830.                 alu.src[0].sel = tmp;
  4831.                 alu.src[0].chan = ctx->src[0].swizzle[i];
  4832.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4833.                 alu.dst.write = 1;
  4834.                 alu.last = i == lasti;
  4835.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4836.                 if (r)
  4837.                         return r;
  4838.         }
  4839.  
  4840.         return 0;
  4841. }
  4842.  
  4843.  
  4844. static int tgsi_helper_copy(struct r600_shader_ctx *ctx, struct tgsi_full_instruction *inst)
  4845. {
  4846.         struct r600_bytecode_alu alu;
  4847.         int i, r;
  4848.  
  4849.         for (i = 0; i < 4; i++) {
  4850.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4851.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i))) {
  4852.                         alu.op = ALU_OP0_NOP;
  4853.                         alu.dst.chan = i;
  4854.                 } else {
  4855.                         alu.op = ALU_OP1_MOV;
  4856.                         tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4857.                         alu.src[0].sel = ctx->temp_reg;
  4858.                         alu.src[0].chan = i;
  4859.                 }
  4860.                 if (i == 3) {
  4861.                         alu.last = 1;
  4862.                 }
  4863.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4864.                 if (r)
  4865.                         return r;
  4866.         }
  4867.         return 0;
  4868. }
  4869.  
  4870. static int tgsi_make_src_for_op3(struct r600_shader_ctx *ctx,
  4871.                                  unsigned temp, int chan,
  4872.                                  struct r600_bytecode_alu_src *bc_src,
  4873.                                  const struct r600_shader_src *shader_src)
  4874. {
  4875.         struct r600_bytecode_alu alu;
  4876.         int r;
  4877.  
  4878.         r600_bytecode_src(bc_src, shader_src, chan);
  4879.  
  4880.         /* op3 operands don't support abs modifier */
  4881.         if (bc_src->abs) {
  4882.                 assert(temp!=0);      /* we actually need the extra register, make sure it is allocated. */
  4883.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4884.                 alu.op = ALU_OP1_MOV;
  4885.                 alu.dst.sel = temp;
  4886.                 alu.dst.chan = chan;
  4887.                 alu.dst.write = 1;
  4888.  
  4889.                 alu.src[0] = *bc_src;
  4890.                 alu.last = true; // sufficient?
  4891.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4892.                 if (r)
  4893.                         return r;
  4894.  
  4895.                 memset(bc_src, 0, sizeof(*bc_src));
  4896.                 bc_src->sel = temp;
  4897.                 bc_src->chan = chan;
  4898.         }
  4899.         return 0;
  4900. }
  4901.  
  4902. static int tgsi_op3(struct r600_shader_ctx *ctx)
  4903. {
  4904.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4905.         struct r600_bytecode_alu alu;
  4906.         int i, j, r;
  4907.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  4908.         int temp_regs[4];
  4909.  
  4910.         for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
  4911.                 temp_regs[j] = 0;
  4912.                 if (ctx->src[j].abs)
  4913.                         temp_regs[j] = r600_get_temp(ctx);
  4914.         }
  4915.         for (i = 0; i < lasti + 1; i++) {
  4916.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  4917.                         continue;
  4918.  
  4919.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4920.                 alu.op = ctx->inst_info->op;
  4921.                 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
  4922.                         r = tgsi_make_src_for_op3(ctx, temp_regs[j], i, &alu.src[j], &ctx->src[j]);
  4923.                         if (r)
  4924.                                 return r;
  4925.                 }
  4926.  
  4927.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4928.                 alu.dst.chan = i;
  4929.                 alu.dst.write = 1;
  4930.                 alu.is_op3 = 1;
  4931.                 if (i == lasti) {
  4932.                         alu.last = 1;
  4933.                 }
  4934.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4935.                 if (r)
  4936.                         return r;
  4937.         }
  4938.         return 0;
  4939. }
  4940.  
  4941. static int tgsi_dp(struct r600_shader_ctx *ctx)
  4942. {
  4943.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4944.         struct r600_bytecode_alu alu;
  4945.         int i, j, r;
  4946.  
  4947.         for (i = 0; i < 4; i++) {
  4948.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  4949.                 alu.op = ctx->inst_info->op;
  4950.                 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
  4951.                         r600_bytecode_src(&alu.src[j], &ctx->src[j], i);
  4952.                 }
  4953.  
  4954.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  4955.                 alu.dst.chan = i;
  4956.                 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
  4957.                 /* handle some special cases */
  4958.                 switch (inst->Instruction.Opcode) {
  4959.                 case TGSI_OPCODE_DP2:
  4960.                         if (i > 1) {
  4961.                                 alu.src[0].sel = alu.src[1].sel = V_SQ_ALU_SRC_0;
  4962.                                 alu.src[0].chan = alu.src[1].chan = 0;
  4963.                         }
  4964.                         break;
  4965.                 case TGSI_OPCODE_DP3:
  4966.                         if (i > 2) {
  4967.                                 alu.src[0].sel = alu.src[1].sel = V_SQ_ALU_SRC_0;
  4968.                                 alu.src[0].chan = alu.src[1].chan = 0;
  4969.                         }
  4970.                         break;
  4971.                 case TGSI_OPCODE_DPH:
  4972.                         if (i == 3) {
  4973.                                 alu.src[0].sel = V_SQ_ALU_SRC_1;
  4974.                                 alu.src[0].chan = 0;
  4975.                                 alu.src[0].neg = 0;
  4976.                         }
  4977.                         break;
  4978.                 default:
  4979.                         break;
  4980.                 }
  4981.                 if (i == 3) {
  4982.                         alu.last = 1;
  4983.                 }
  4984.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  4985.                 if (r)
  4986.                         return r;
  4987.         }
  4988.         return 0;
  4989. }
  4990.  
  4991. static inline boolean tgsi_tex_src_requires_loading(struct r600_shader_ctx *ctx,
  4992.                                                     unsigned index)
  4993. {
  4994.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  4995.         return  (inst->Src[index].Register.File != TGSI_FILE_TEMPORARY &&
  4996.                 inst->Src[index].Register.File != TGSI_FILE_INPUT &&
  4997.                 inst->Src[index].Register.File != TGSI_FILE_OUTPUT) ||
  4998.                 ctx->src[index].neg || ctx->src[index].abs ||
  4999.                 (inst->Src[index].Register.File == TGSI_FILE_INPUT && ctx->type == TGSI_PROCESSOR_GEOMETRY);
  5000. }
  5001.  
  5002. static inline unsigned tgsi_tex_get_src_gpr(struct r600_shader_ctx *ctx,
  5003.                                         unsigned index)
  5004. {
  5005.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  5006.         return ctx->file_offset[inst->Src[index].Register.File] + inst->Src[index].Register.Index;
  5007. }
  5008.  
  5009. static int do_vtx_fetch_inst(struct r600_shader_ctx *ctx, boolean src_requires_loading)
  5010. {
  5011.         struct r600_bytecode_vtx vtx;
  5012.         struct r600_bytecode_alu alu;
  5013.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  5014.         int src_gpr, r, i;
  5015.         int id = tgsi_tex_get_src_gpr(ctx, 1);
  5016.  
  5017.         src_gpr = tgsi_tex_get_src_gpr(ctx, 0);
  5018.         if (src_requires_loading) {
  5019.                 for (i = 0; i < 4; i++) {
  5020.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5021.                         alu.op = ALU_OP1_MOV;
  5022.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  5023.                         alu.dst.sel = ctx->temp_reg;
  5024.                         alu.dst.chan = i;
  5025.                         if (i == 3)
  5026.                                 alu.last = 1;
  5027.                         alu.dst.write = 1;
  5028.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5029.                         if (r)
  5030.                                 return r;
  5031.                 }
  5032.                 src_gpr = ctx->temp_reg;
  5033.         }
  5034.  
  5035.         memset(&vtx, 0, sizeof(vtx));
  5036.         vtx.op = FETCH_OP_VFETCH;
  5037.         vtx.buffer_id = id + R600_MAX_CONST_BUFFERS;
  5038.         vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
  5039.         vtx.src_gpr = src_gpr;
  5040.         vtx.mega_fetch_count = 16;
  5041.         vtx.dst_gpr = ctx->file_offset[inst->Dst[0].Register.File] + inst->Dst[0].Register.Index;
  5042.         vtx.dst_sel_x = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;          /* SEL_X */
  5043.         vtx.dst_sel_y = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;          /* SEL_Y */
  5044.         vtx.dst_sel_z = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7;          /* SEL_Z */
  5045.         vtx.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;          /* SEL_W */
  5046.         vtx.use_const_fields = 1;
  5047.  
  5048.         if ((r = r600_bytecode_add_vtx(ctx->bc, &vtx)))
  5049.                 return r;
  5050.  
  5051.         if (ctx->bc->chip_class >= EVERGREEN)
  5052.                 return 0;
  5053.  
  5054.         for (i = 0; i < 4; i++) {
  5055.                 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  5056.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  5057.                         continue;
  5058.  
  5059.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5060.                 alu.op = ALU_OP2_AND_INT;
  5061.  
  5062.                 alu.dst.chan = i;
  5063.                 alu.dst.sel = vtx.dst_gpr;
  5064.                 alu.dst.write = 1;
  5065.  
  5066.                 alu.src[0].sel = vtx.dst_gpr;
  5067.                 alu.src[0].chan = i;
  5068.  
  5069.                 alu.src[1].sel = 512 + (id * 2);
  5070.                 alu.src[1].chan = i % 4;
  5071.                 alu.src[1].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
  5072.  
  5073.                 if (i == lasti)
  5074.                         alu.last = 1;
  5075.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5076.                 if (r)
  5077.                         return r;
  5078.         }
  5079.  
  5080.         if (inst->Dst[0].Register.WriteMask & 3) {
  5081.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5082.                 alu.op = ALU_OP2_OR_INT;
  5083.  
  5084.                 alu.dst.chan = 3;
  5085.                 alu.dst.sel = vtx.dst_gpr;
  5086.                 alu.dst.write = 1;
  5087.  
  5088.                 alu.src[0].sel = vtx.dst_gpr;
  5089.                 alu.src[0].chan = 3;
  5090.  
  5091.                 alu.src[1].sel = 512 + (id * 2) + 1;
  5092.                 alu.src[1].chan = 0;
  5093.                 alu.src[1].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
  5094.  
  5095.                 alu.last = 1;
  5096.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5097.                 if (r)
  5098.                         return r;
  5099.         }
  5100.         return 0;
  5101. }
  5102.  
  5103. static int r600_do_buffer_txq(struct r600_shader_ctx *ctx)
  5104. {
  5105.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  5106.         struct r600_bytecode_alu alu;
  5107.         int r;
  5108.         int id = tgsi_tex_get_src_gpr(ctx, 1);
  5109.  
  5110.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5111.         alu.op = ALU_OP1_MOV;
  5112.  
  5113.         if (ctx->bc->chip_class >= EVERGREEN) {
  5114.                 /* channel 0 or 2 of each word */
  5115.                 alu.src[0].sel = 512 + (id / 2);
  5116.                 alu.src[0].chan = (id % 2) * 2;
  5117.         } else {
  5118.                 /* r600 we have them at channel 2 of the second dword */
  5119.                 alu.src[0].sel = 512 + (id * 2) + 1;
  5120.                 alu.src[0].chan = 1;
  5121.         }
  5122.         alu.src[0].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
  5123.         tgsi_dst(ctx, &inst->Dst[0], 0, &alu.dst);
  5124.         alu.last = 1;
  5125.         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5126.         if (r)
  5127.                 return r;
  5128.         return 0;
  5129. }
  5130.  
  5131. static int tgsi_tex(struct r600_shader_ctx *ctx)
  5132. {
  5133.         static float one_point_five = 1.5f;
  5134.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  5135.         struct r600_bytecode_tex tex;
  5136.         struct r600_bytecode_alu alu;
  5137.         unsigned src_gpr;
  5138.         int r, i, j;
  5139.         int opcode;
  5140.         bool read_compressed_msaa = ctx->bc->has_compressed_msaa_texturing &&
  5141.                                     inst->Instruction.Opcode == TGSI_OPCODE_TXF &&
  5142.                                     (inst->Texture.Texture == TGSI_TEXTURE_2D_MSAA ||
  5143.                                      inst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY_MSAA);
  5144.  
  5145.         bool txf_add_offsets = inst->Texture.NumOffsets &&
  5146.                              inst->Instruction.Opcode == TGSI_OPCODE_TXF &&
  5147.                              inst->Texture.Texture != TGSI_TEXTURE_BUFFER;
  5148.  
  5149.         /* Texture fetch instructions can only use gprs as source.
  5150.          * Also they cannot negate the source or take the absolute value */
  5151.         const boolean src_requires_loading = (inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ &&
  5152.                                               tgsi_tex_src_requires_loading(ctx, 0)) ||
  5153.                                              read_compressed_msaa || txf_add_offsets;
  5154.  
  5155.         boolean src_loaded = FALSE;
  5156.         unsigned sampler_src_reg = inst->Instruction.Opcode == TGSI_OPCODE_TXQ_LZ ? 0 : 1;
  5157.         int8_t offset_x = 0, offset_y = 0, offset_z = 0;
  5158.         boolean has_txq_cube_array_z = false;
  5159.         unsigned sampler_index_mode;
  5160.  
  5161.         if (inst->Instruction.Opcode == TGSI_OPCODE_TXQ &&
  5162.             ((inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
  5163.               inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY)))
  5164.                 if (inst->Dst[0].Register.WriteMask & 4) {
  5165.                         ctx->shader->has_txq_cube_array_z_comp = true;
  5166.                         has_txq_cube_array_z = true;
  5167.                 }
  5168.  
  5169.         if (inst->Instruction.Opcode == TGSI_OPCODE_TEX2 ||
  5170.             inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
  5171.             inst->Instruction.Opcode == TGSI_OPCODE_TXL2 ||
  5172.             inst->Instruction.Opcode == TGSI_OPCODE_TG4)
  5173.                 sampler_src_reg = 2;
  5174.  
  5175.         /* TGSI moves the sampler to src reg 3 for TXD */
  5176.         if (inst->Instruction.Opcode == TGSI_OPCODE_TXD)
  5177.                 sampler_src_reg = 3;
  5178.  
  5179.         sampler_index_mode = inst->Src[sampler_src_reg].Indirect.Index == 2 ? 2 : 0; // CF_INDEX_1 : CF_INDEX_NONE
  5180.         if (sampler_index_mode)
  5181.                 ctx->shader->uses_index_registers = true;
  5182.  
  5183.         src_gpr = tgsi_tex_get_src_gpr(ctx, 0);
  5184.  
  5185.         if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
  5186.                 if (inst->Instruction.Opcode == TGSI_OPCODE_TXQ) {
  5187.                         ctx->shader->uses_tex_buffers = true;
  5188.                         return r600_do_buffer_txq(ctx);
  5189.                 }
  5190.                 else if (inst->Instruction.Opcode == TGSI_OPCODE_TXF) {
  5191.                         if (ctx->bc->chip_class < EVERGREEN)
  5192.                                 ctx->shader->uses_tex_buffers = true;
  5193.                         return do_vtx_fetch_inst(ctx, src_requires_loading);
  5194.                 }
  5195.         }
  5196.  
  5197.         if (inst->Instruction.Opcode == TGSI_OPCODE_TXP) {
  5198.                 int out_chan;
  5199.                 /* Add perspective divide */
  5200.                 if (ctx->bc->chip_class == CAYMAN) {
  5201.                         out_chan = 2;
  5202.                         for (i = 0; i < 3; i++) {
  5203.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5204.                                 alu.op = ALU_OP1_RECIP_IEEE;
  5205.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
  5206.  
  5207.                                 alu.dst.sel = ctx->temp_reg;
  5208.                                 alu.dst.chan = i;
  5209.                                 if (i == 2)
  5210.                                         alu.last = 1;
  5211.                                 if (out_chan == i)
  5212.                                         alu.dst.write = 1;
  5213.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5214.                                 if (r)
  5215.                                         return r;
  5216.                         }
  5217.  
  5218.                 } else {
  5219.                         out_chan = 3;
  5220.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5221.                         alu.op = ALU_OP1_RECIP_IEEE;
  5222.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
  5223.  
  5224.                         alu.dst.sel = ctx->temp_reg;
  5225.                         alu.dst.chan = out_chan;
  5226.                         alu.last = 1;
  5227.                         alu.dst.write = 1;
  5228.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5229.                         if (r)
  5230.                                 return r;
  5231.                 }
  5232.  
  5233.                 for (i = 0; i < 3; i++) {
  5234.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5235.                         alu.op = ALU_OP2_MUL;
  5236.                         alu.src[0].sel = ctx->temp_reg;
  5237.                         alu.src[0].chan = out_chan;
  5238.                         r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  5239.                         alu.dst.sel = ctx->temp_reg;
  5240.                         alu.dst.chan = i;
  5241.                         alu.dst.write = 1;
  5242.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5243.                         if (r)
  5244.                                 return r;
  5245.                 }
  5246.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5247.                 alu.op = ALU_OP1_MOV;
  5248.                 alu.src[0].sel = V_SQ_ALU_SRC_1;
  5249.                 alu.src[0].chan = 0;
  5250.                 alu.dst.sel = ctx->temp_reg;
  5251.                 alu.dst.chan = 3;
  5252.                 alu.last = 1;
  5253.                 alu.dst.write = 1;
  5254.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5255.                 if (r)
  5256.                         return r;
  5257.                 src_loaded = TRUE;
  5258.                 src_gpr = ctx->temp_reg;
  5259.         }
  5260.  
  5261.  
  5262.         if ((inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
  5263.              inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
  5264.              inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
  5265.              inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
  5266.             inst->Instruction.Opcode != TGSI_OPCODE_TXQ &&
  5267.             inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ) {
  5268.  
  5269.                 static const unsigned src0_swizzle[] = {2, 2, 0, 1};
  5270.                 static const unsigned src1_swizzle[] = {1, 0, 2, 2};
  5271.  
  5272.                 /* tmp1.xyzw = CUBE(R0.zzxy, R0.yxzz) */
  5273.                 for (i = 0; i < 4; i++) {
  5274.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5275.                         alu.op = ALU_OP2_CUBE;
  5276.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], src0_swizzle[i]);
  5277.                         r600_bytecode_src(&alu.src[1], &ctx->src[0], src1_swizzle[i]);
  5278.                         alu.dst.sel = ctx->temp_reg;
  5279.                         alu.dst.chan = i;
  5280.                         if (i == 3)
  5281.                                 alu.last = 1;
  5282.                         alu.dst.write = 1;
  5283.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5284.                         if (r)
  5285.                                 return r;
  5286.                 }
  5287.  
  5288.                 /* tmp1.z = RCP_e(|tmp1.z|) */
  5289.                 if (ctx->bc->chip_class == CAYMAN) {
  5290.                         for (i = 0; i < 3; i++) {
  5291.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5292.                                 alu.op = ALU_OP1_RECIP_IEEE;
  5293.                                 alu.src[0].sel = ctx->temp_reg;
  5294.                                 alu.src[0].chan = 2;
  5295.                                 alu.src[0].abs = 1;
  5296.                                 alu.dst.sel = ctx->temp_reg;
  5297.                                 alu.dst.chan = i;
  5298.                                 if (i == 2)
  5299.                                         alu.dst.write = 1;
  5300.                                 if (i == 2)
  5301.                                         alu.last = 1;
  5302.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5303.                                 if (r)
  5304.                                         return r;
  5305.                         }
  5306.                 } else {
  5307.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5308.                         alu.op = ALU_OP1_RECIP_IEEE;
  5309.                         alu.src[0].sel = ctx->temp_reg;
  5310.                         alu.src[0].chan = 2;
  5311.                         alu.src[0].abs = 1;
  5312.                         alu.dst.sel = ctx->temp_reg;
  5313.                         alu.dst.chan = 2;
  5314.                         alu.dst.write = 1;
  5315.                         alu.last = 1;
  5316.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5317.                         if (r)
  5318.                                 return r;
  5319.                 }
  5320.  
  5321.                 /* MULADD R0.x,  R0.x,  PS1,  (0x3FC00000, 1.5f).x
  5322.                  * MULADD R0.y,  R0.y,  PS1,  (0x3FC00000, 1.5f).x
  5323.                  * muladd has no writemask, have to use another temp
  5324.                  */
  5325.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5326.                 alu.op = ALU_OP3_MULADD;
  5327.                 alu.is_op3 = 1;
  5328.  
  5329.                 alu.src[0].sel = ctx->temp_reg;
  5330.                 alu.src[0].chan = 0;
  5331.                 alu.src[1].sel = ctx->temp_reg;
  5332.                 alu.src[1].chan = 2;
  5333.  
  5334.                 alu.src[2].sel = V_SQ_ALU_SRC_LITERAL;
  5335.                 alu.src[2].chan = 0;
  5336.                 alu.src[2].value = *(uint32_t *)&one_point_five;
  5337.  
  5338.                 alu.dst.sel = ctx->temp_reg;
  5339.                 alu.dst.chan = 0;
  5340.                 alu.dst.write = 1;
  5341.  
  5342.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5343.                 if (r)
  5344.                         return r;
  5345.  
  5346.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5347.                 alu.op = ALU_OP3_MULADD;
  5348.                 alu.is_op3 = 1;
  5349.  
  5350.                 alu.src[0].sel = ctx->temp_reg;
  5351.                 alu.src[0].chan = 1;
  5352.                 alu.src[1].sel = ctx->temp_reg;
  5353.                 alu.src[1].chan = 2;
  5354.  
  5355.                 alu.src[2].sel = V_SQ_ALU_SRC_LITERAL;
  5356.                 alu.src[2].chan = 0;
  5357.                 alu.src[2].value = *(uint32_t *)&one_point_five;
  5358.  
  5359.                 alu.dst.sel = ctx->temp_reg;
  5360.                 alu.dst.chan = 1;
  5361.                 alu.dst.write = 1;
  5362.  
  5363.                 alu.last = 1;
  5364.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5365.                 if (r)
  5366.                         return r;
  5367.                 /* write initial compare value into Z component
  5368.                   - W src 0 for shadow cube
  5369.                   - X src 1 for shadow cube array */
  5370.                 if (inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
  5371.                     inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
  5372.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5373.                         alu.op = ALU_OP1_MOV;
  5374.                         if (inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
  5375.                                 r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
  5376.                         else
  5377.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
  5378.                         alu.dst.sel = ctx->temp_reg;
  5379.                         alu.dst.chan = 2;
  5380.                         alu.dst.write = 1;
  5381.                         alu.last = 1;
  5382.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5383.                         if (r)
  5384.                                 return r;
  5385.                 }
  5386.  
  5387.                 if (inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
  5388.                     inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
  5389.                         if (ctx->bc->chip_class >= EVERGREEN) {
  5390.                                 int mytmp = r600_get_temp(ctx);
  5391.                                 static const float eight = 8.0f;
  5392.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5393.                                 alu.op = ALU_OP1_MOV;
  5394.                                 alu.src[0].sel = ctx->temp_reg;
  5395.                                 alu.src[0].chan = 3;
  5396.                                 alu.dst.sel = mytmp;
  5397.                                 alu.dst.chan = 0;
  5398.                                 alu.dst.write = 1;
  5399.                                 alu.last = 1;
  5400.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5401.                                 if (r)
  5402.                                         return r;
  5403.  
  5404.                                 /* have to multiply original layer by 8 and add to face id (temp.w) in Z */
  5405.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5406.                                 alu.op = ALU_OP3_MULADD;
  5407.                                 alu.is_op3 = 1;
  5408.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
  5409.                                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  5410.                                 alu.src[1].chan = 0;
  5411.                                 alu.src[1].value = *(uint32_t *)&eight;
  5412.                                 alu.src[2].sel = mytmp;
  5413.                                 alu.src[2].chan = 0;
  5414.                                 alu.dst.sel = ctx->temp_reg;
  5415.                                 alu.dst.chan = 3;
  5416.                                 alu.dst.write = 1;
  5417.                                 alu.last = 1;
  5418.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5419.                                 if (r)
  5420.                                         return r;
  5421.                         } else if (ctx->bc->chip_class < EVERGREEN) {
  5422.                                 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
  5423.                                 tex.op = FETCH_OP_SET_CUBEMAP_INDEX;
  5424.                                 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
  5425.                                 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
  5426.                                 tex.src_gpr = r600_get_temp(ctx);
  5427.                                 tex.src_sel_x = 0;
  5428.                                 tex.src_sel_y = 0;
  5429.                                 tex.src_sel_z = 0;
  5430.                                 tex.src_sel_w = 0;
  5431.                                 tex.dst_sel_x = tex.dst_sel_y = tex.dst_sel_z = tex.dst_sel_w = 7;
  5432.                                 tex.coord_type_x = 1;
  5433.                                 tex.coord_type_y = 1;
  5434.                                 tex.coord_type_z = 1;
  5435.                                 tex.coord_type_w = 1;
  5436.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5437.                                 alu.op = ALU_OP1_MOV;
  5438.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
  5439.                                 alu.dst.sel = tex.src_gpr;
  5440.                                 alu.dst.chan = 0;
  5441.                                 alu.last = 1;
  5442.                                 alu.dst.write = 1;
  5443.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5444.                                 if (r)
  5445.                                         return r;
  5446.                                        
  5447.                                 r = r600_bytecode_add_tex(ctx->bc, &tex);
  5448.                                 if (r)
  5449.                                         return r;
  5450.                         }
  5451.  
  5452.                 }
  5453.  
  5454.                 /* for cube forms of lod and bias we need to route things */
  5455.                 if (inst->Instruction.Opcode == TGSI_OPCODE_TXB ||
  5456.                     inst->Instruction.Opcode == TGSI_OPCODE_TXL ||
  5457.                     inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
  5458.                     inst->Instruction.Opcode == TGSI_OPCODE_TXL2) {
  5459.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5460.                         alu.op = ALU_OP1_MOV;
  5461.                         if (inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
  5462.                             inst->Instruction.Opcode == TGSI_OPCODE_TXL2)
  5463.                                 r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
  5464.                         else
  5465.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
  5466.                         alu.dst.sel = ctx->temp_reg;
  5467.                         alu.dst.chan = 2;
  5468.                         alu.last = 1;
  5469.                         alu.dst.write = 1;
  5470.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5471.                         if (r)
  5472.                                 return r;
  5473.                 }
  5474.  
  5475.                 src_loaded = TRUE;
  5476.                 src_gpr = ctx->temp_reg;
  5477.         }
  5478.  
  5479.         if (inst->Instruction.Opcode == TGSI_OPCODE_TXD) {
  5480.                 int temp_h = 0, temp_v = 0;
  5481.                 int start_val = 0;
  5482.  
  5483.                 /* if we've already loaded the src (i.e. CUBE don't reload it). */
  5484.                 if (src_loaded == TRUE)
  5485.                         start_val = 1;
  5486.                 else
  5487.                         src_loaded = TRUE;
  5488.                 for (i = start_val; i < 3; i++) {
  5489.                         int treg = r600_get_temp(ctx);
  5490.  
  5491.                         if (i == 0)
  5492.                                 src_gpr = treg;
  5493.                         else if (i == 1)
  5494.                                 temp_h = treg;
  5495.                         else
  5496.                                 temp_v = treg;
  5497.  
  5498.                         for (j = 0; j < 4; j++) {
  5499.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5500.                                 alu.op = ALU_OP1_MOV;
  5501.                                 r600_bytecode_src(&alu.src[0], &ctx->src[i], j);
  5502.                                 alu.dst.sel = treg;
  5503.                                 alu.dst.chan = j;
  5504.                                 if (j == 3)
  5505.                                    alu.last = 1;
  5506.                                 alu.dst.write = 1;
  5507.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5508.                                 if (r)
  5509.                                     return r;
  5510.                         }
  5511.                 }
  5512.                 for (i = 1; i < 3; i++) {
  5513.                         /* set gradients h/v */
  5514.                         memset(&tex, 0, sizeof(struct r600_bytecode_tex));
  5515.                         tex.op = (i == 1) ? FETCH_OP_SET_GRADIENTS_H :
  5516.                                 FETCH_OP_SET_GRADIENTS_V;
  5517.                         tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
  5518.                         tex.sampler_index_mode = sampler_index_mode;
  5519.                         tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
  5520.                         tex.resource_index_mode = sampler_index_mode;
  5521.  
  5522.                         tex.src_gpr = (i == 1) ? temp_h : temp_v;
  5523.                         tex.src_sel_x = 0;
  5524.                         tex.src_sel_y = 1;
  5525.                         tex.src_sel_z = 2;
  5526.                         tex.src_sel_w = 3;
  5527.  
  5528.                         tex.dst_gpr = r600_get_temp(ctx); /* just to avoid confusing the asm scheduler */
  5529.                         tex.dst_sel_x = tex.dst_sel_y = tex.dst_sel_z = tex.dst_sel_w = 7;
  5530.                         if (inst->Texture.Texture != TGSI_TEXTURE_RECT) {
  5531.                                 tex.coord_type_x = 1;
  5532.                                 tex.coord_type_y = 1;
  5533.                                 tex.coord_type_z = 1;
  5534.                                 tex.coord_type_w = 1;
  5535.                         }
  5536.                         r = r600_bytecode_add_tex(ctx->bc, &tex);
  5537.                         if (r)
  5538.                                 return r;
  5539.                 }
  5540.         }
  5541.  
  5542.         if (src_requires_loading && !src_loaded) {
  5543.                 for (i = 0; i < 4; i++) {
  5544.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5545.                         alu.op = ALU_OP1_MOV;
  5546.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  5547.                         alu.dst.sel = ctx->temp_reg;
  5548.                         alu.dst.chan = i;
  5549.                         if (i == 3)
  5550.                                 alu.last = 1;
  5551.                         alu.dst.write = 1;
  5552.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5553.                         if (r)
  5554.                                 return r;
  5555.                 }
  5556.                 src_loaded = TRUE;
  5557.                 src_gpr = ctx->temp_reg;
  5558.         }
  5559.  
  5560.         /* get offset values */
  5561.         if (inst->Texture.NumOffsets) {
  5562.                 assert(inst->Texture.NumOffsets == 1);
  5563.  
  5564.                 /* The texture offset feature doesn't work with the TXF instruction
  5565.                  * and must be emulated by adding the offset to the texture coordinates. */
  5566.                 if (txf_add_offsets) {
  5567.                         const struct tgsi_texture_offset *off = inst->TexOffsets;
  5568.  
  5569.                         switch (inst->Texture.Texture) {
  5570.                         case TGSI_TEXTURE_3D:
  5571.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5572.                                 alu.op = ALU_OP2_ADD_INT;
  5573.                                 alu.src[0].sel = src_gpr;
  5574.                                 alu.src[0].chan = 2;
  5575.                                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  5576.                                 alu.src[1].value = ctx->literals[4 * off[0].Index + off[0].SwizzleZ];
  5577.                                 alu.dst.sel = src_gpr;
  5578.                                 alu.dst.chan = 2;
  5579.                                 alu.dst.write = 1;
  5580.                                 alu.last = 1;
  5581.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5582.                                 if (r)
  5583.                                         return r;
  5584.                                 /* fall through */
  5585.  
  5586.                         case TGSI_TEXTURE_2D:
  5587.                         case TGSI_TEXTURE_SHADOW2D:
  5588.                         case TGSI_TEXTURE_RECT:
  5589.                         case TGSI_TEXTURE_SHADOWRECT:
  5590.                         case TGSI_TEXTURE_2D_ARRAY:
  5591.                         case TGSI_TEXTURE_SHADOW2D_ARRAY:
  5592.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5593.                                 alu.op = ALU_OP2_ADD_INT;
  5594.                                 alu.src[0].sel = src_gpr;
  5595.                                 alu.src[0].chan = 1;
  5596.                                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  5597.                                 alu.src[1].value = ctx->literals[4 * off[0].Index + off[0].SwizzleY];
  5598.                                 alu.dst.sel = src_gpr;
  5599.                                 alu.dst.chan = 1;
  5600.                                 alu.dst.write = 1;
  5601.                                 alu.last = 1;
  5602.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5603.                                 if (r)
  5604.                                         return r;
  5605.                                 /* fall through */
  5606.  
  5607.                         case TGSI_TEXTURE_1D:
  5608.                         case TGSI_TEXTURE_SHADOW1D:
  5609.                         case TGSI_TEXTURE_1D_ARRAY:
  5610.                         case TGSI_TEXTURE_SHADOW1D_ARRAY:
  5611.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5612.                                 alu.op = ALU_OP2_ADD_INT;
  5613.                                 alu.src[0].sel = src_gpr;
  5614.                                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  5615.                                 alu.src[1].value = ctx->literals[4 * off[0].Index + off[0].SwizzleX];
  5616.                                 alu.dst.sel = src_gpr;
  5617.                                 alu.dst.write = 1;
  5618.                                 alu.last = 1;
  5619.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5620.                                 if (r)
  5621.                                         return r;
  5622.                                 break;
  5623.                                 /* texture offsets do not apply to other texture targets */
  5624.                         }
  5625.                 } else {
  5626.                         switch (inst->Texture.Texture) {
  5627.                         case TGSI_TEXTURE_3D:
  5628.                                 offset_z = ctx->literals[4 * inst->TexOffsets[0].Index + inst->TexOffsets[0].SwizzleZ] << 1;
  5629.                                 /* fallthrough */
  5630.                         case TGSI_TEXTURE_2D:
  5631.                         case TGSI_TEXTURE_SHADOW2D:
  5632.                         case TGSI_TEXTURE_RECT:
  5633.                         case TGSI_TEXTURE_SHADOWRECT:
  5634.                         case TGSI_TEXTURE_2D_ARRAY:
  5635.                         case TGSI_TEXTURE_SHADOW2D_ARRAY:
  5636.                                 offset_y = ctx->literals[4 * inst->TexOffsets[0].Index + inst->TexOffsets[0].SwizzleY] << 1;
  5637.                                 /* fallthrough */
  5638.                         case TGSI_TEXTURE_1D:
  5639.                         case TGSI_TEXTURE_SHADOW1D:
  5640.                         case TGSI_TEXTURE_1D_ARRAY:
  5641.                         case TGSI_TEXTURE_SHADOW1D_ARRAY:
  5642.                                 offset_x = ctx->literals[4 * inst->TexOffsets[0].Index + inst->TexOffsets[0].SwizzleX] << 1;
  5643.                         }
  5644.                 }
  5645.         }
  5646.  
  5647.         /* Obtain the sample index for reading a compressed MSAA color texture.
  5648.          * To read the FMASK, we use the ldfptr instruction, which tells us
  5649.          * where the samples are stored.
  5650.          * For uncompressed 8x MSAA surfaces, ldfptr should return 0x76543210,
  5651.          * which is the identity mapping. Each nibble says which physical sample
  5652.          * should be fetched to get that sample.
  5653.          *
  5654.          * Assume src.z contains the sample index. It should be modified like this:
  5655.          *   src.z = (ldfptr() >> (src.z * 4)) & 0xF;
  5656.          * Then fetch the texel with src.
  5657.          */
  5658.         if (read_compressed_msaa) {
  5659.                 unsigned sample_chan = 3;
  5660.                 unsigned temp = r600_get_temp(ctx);
  5661.                 assert(src_loaded);
  5662.  
  5663.                 /* temp.w = ldfptr() */
  5664.                 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
  5665.                 tex.op = FETCH_OP_LD;
  5666.                 tex.inst_mod = 1; /* to indicate this is ldfptr */
  5667.                 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
  5668.                 tex.sampler_index_mode = sampler_index_mode;
  5669.                 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
  5670.                 tex.resource_index_mode = sampler_index_mode;
  5671.                 tex.src_gpr = src_gpr;
  5672.                 tex.dst_gpr = temp;
  5673.                 tex.dst_sel_x = 7; /* mask out these components */
  5674.                 tex.dst_sel_y = 7;
  5675.                 tex.dst_sel_z = 7;
  5676.                 tex.dst_sel_w = 0; /* store X */
  5677.                 tex.src_sel_x = 0;
  5678.                 tex.src_sel_y = 1;
  5679.                 tex.src_sel_z = 2;
  5680.                 tex.src_sel_w = 3;
  5681.                 tex.offset_x = offset_x;
  5682.                 tex.offset_y = offset_y;
  5683.                 tex.offset_z = offset_z;
  5684.                 r = r600_bytecode_add_tex(ctx->bc, &tex);
  5685.                 if (r)
  5686.                         return r;
  5687.  
  5688.                 /* temp.x = sample_index*4 */
  5689.                 if (ctx->bc->chip_class == CAYMAN) {
  5690.                         for (i = 0 ; i < 4; i++) {
  5691.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5692.                                 alu.op = ALU_OP2_MULLO_INT;
  5693.                                 alu.src[0].sel = src_gpr;
  5694.                                 alu.src[0].chan = sample_chan;
  5695.                                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  5696.                                 alu.src[1].value = 4;
  5697.                                 alu.dst.sel = temp;
  5698.                                 alu.dst.chan = i;
  5699.                                 alu.dst.write = i == 0;
  5700.                                 if (i == 3)
  5701.                                         alu.last = 1;
  5702.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5703.                                 if (r)
  5704.                                         return r;
  5705.                         }
  5706.                 } else {
  5707.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5708.                         alu.op = ALU_OP2_MULLO_INT;
  5709.                         alu.src[0].sel = src_gpr;
  5710.                         alu.src[0].chan = sample_chan;
  5711.                         alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  5712.                         alu.src[1].value = 4;
  5713.                         alu.dst.sel = temp;
  5714.                         alu.dst.chan = 0;
  5715.                         alu.dst.write = 1;
  5716.                         alu.last = 1;
  5717.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5718.                         if (r)
  5719.                                 return r;
  5720.                 }
  5721.  
  5722.                 /* sample_index = temp.w >> temp.x */
  5723.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5724.                 alu.op = ALU_OP2_LSHR_INT;
  5725.                 alu.src[0].sel = temp;
  5726.                 alu.src[0].chan = 3;
  5727.                 alu.src[1].sel = temp;
  5728.                 alu.src[1].chan = 0;
  5729.                 alu.dst.sel = src_gpr;
  5730.                 alu.dst.chan = sample_chan;
  5731.                 alu.dst.write = 1;
  5732.                 alu.last = 1;
  5733.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5734.                 if (r)
  5735.                         return r;
  5736.  
  5737.                 /* sample_index & 0xF */
  5738.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5739.                 alu.op = ALU_OP2_AND_INT;
  5740.                 alu.src[0].sel = src_gpr;
  5741.                 alu.src[0].chan = sample_chan;
  5742.                 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
  5743.                 alu.src[1].value = 0xF;
  5744.                 alu.dst.sel = src_gpr;
  5745.                 alu.dst.chan = sample_chan;
  5746.                 alu.dst.write = 1;
  5747.                 alu.last = 1;
  5748.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5749.                 if (r)
  5750.                         return r;
  5751. #if 0
  5752.                 /* visualize the FMASK */
  5753.                 for (i = 0; i < 4; i++) {
  5754.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5755.                         alu.op = ALU_OP1_INT_TO_FLT;
  5756.                         alu.src[0].sel = src_gpr;
  5757.                         alu.src[0].chan = sample_chan;
  5758.                         alu.dst.sel = ctx->file_offset[inst->Dst[0].Register.File] + inst->Dst[0].Register.Index;
  5759.                         alu.dst.chan = i;
  5760.                         alu.dst.write = 1;
  5761.                         alu.last = 1;
  5762.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  5763.                         if (r)
  5764.                                 return r;
  5765.                 }
  5766.                 return 0;
  5767. #endif
  5768.         }
  5769.  
  5770.         /* does this shader want a num layers from TXQ for a cube array? */
  5771.         if (has_txq_cube_array_z) {
  5772.                 int id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
  5773.                
  5774.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  5775.                 alu.op = ALU_OP1_MOV;
  5776.  
  5777.                 if (ctx->bc->chip_class >= EVERGREEN) {
  5778.                         /* channel 1 or 3 of each word */
  5779.                         alu.src[0].sel = 512 + (id / 2);
  5780.                         alu.src[0].chan = ((id % 2) * 2) + 1;
  5781.                 } else {
  5782.                         /* r600 we have them at channel 2 of the second dword */
  5783.                         alu.src[0].sel = 512 + (id * 2) + 1;
  5784.                         alu.src[0].chan = 2;
  5785.                 }
  5786.                 alu.src[0].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
  5787.                 tgsi_dst(ctx, &inst->Dst[0], 2, &alu.dst);
  5788.                 alu.last = 1;
  5789.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  5790.                 if (r)
  5791.                         return r;
  5792.                 /* disable writemask from texture instruction */
  5793.                 inst->Dst[0].Register.WriteMask &= ~4;
  5794.         }
  5795.  
  5796.         opcode = ctx->inst_info->op;
  5797.         if (opcode == FETCH_OP_GATHER4 &&
  5798.                 inst->TexOffsets[0].File != TGSI_FILE_NULL &&
  5799.                 inst->TexOffsets[0].File != TGSI_FILE_IMMEDIATE) {
  5800.                 opcode = FETCH_OP_GATHER4_O;
  5801.  
  5802.                 /* GATHER4_O/GATHER4_C_O use offset values loaded by
  5803.                    SET_TEXTURE_OFFSETS instruction. The immediate offset values
  5804.                    encoded in the instruction are ignored. */
  5805.                 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
  5806.                 tex.op = FETCH_OP_SET_TEXTURE_OFFSETS;
  5807.                 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
  5808.                 tex.sampler_index_mode = sampler_index_mode;
  5809.                 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
  5810.                 tex.resource_index_mode = sampler_index_mode;
  5811.  
  5812.                 tex.src_gpr = ctx->file_offset[inst->TexOffsets[0].File] + inst->TexOffsets[0].Index;
  5813.                 tex.src_sel_x = inst->TexOffsets[0].SwizzleX;
  5814.                 tex.src_sel_y = inst->TexOffsets[0].SwizzleY;
  5815.                 tex.src_sel_z = inst->TexOffsets[0].SwizzleZ;
  5816.                 tex.src_sel_w = 4;
  5817.  
  5818.                 tex.dst_sel_x = 7;
  5819.                 tex.dst_sel_y = 7;
  5820.                 tex.dst_sel_z = 7;
  5821.                 tex.dst_sel_w = 7;
  5822.  
  5823.                 r = r600_bytecode_add_tex(ctx->bc, &tex);
  5824.                 if (r)
  5825.                         return r;
  5826.         }
  5827.  
  5828.         if (inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D ||
  5829.             inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D ||
  5830.             inst->Texture.Texture == TGSI_TEXTURE_SHADOWRECT ||
  5831.             inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
  5832.             inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D_ARRAY ||
  5833.             inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D_ARRAY ||
  5834.             inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
  5835.                 switch (opcode) {
  5836.                 case FETCH_OP_SAMPLE:
  5837.                         opcode = FETCH_OP_SAMPLE_C;
  5838.                         break;
  5839.                 case FETCH_OP_SAMPLE_L:
  5840.                         opcode = FETCH_OP_SAMPLE_C_L;
  5841.                         break;
  5842.                 case FETCH_OP_SAMPLE_LB:
  5843.                         opcode = FETCH_OP_SAMPLE_C_LB;
  5844.                         break;
  5845.                 case FETCH_OP_SAMPLE_G:
  5846.                         opcode = FETCH_OP_SAMPLE_C_G;
  5847.                         break;
  5848.                 /* Texture gather variants */
  5849.                 case FETCH_OP_GATHER4:
  5850.                         opcode = FETCH_OP_GATHER4_C;
  5851.                         break;
  5852.                 case FETCH_OP_GATHER4_O:
  5853.                         opcode = FETCH_OP_GATHER4_C_O;
  5854.                         break;
  5855.                 }
  5856.         }
  5857.  
  5858.         memset(&tex, 0, sizeof(struct r600_bytecode_tex));
  5859.         tex.op = opcode;
  5860.  
  5861.         tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
  5862.         tex.sampler_index_mode = sampler_index_mode;
  5863.         tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
  5864.         tex.resource_index_mode = sampler_index_mode;
  5865.         tex.src_gpr = src_gpr;
  5866.         tex.dst_gpr = ctx->file_offset[inst->Dst[0].Register.File] + inst->Dst[0].Register.Index;
  5867.  
  5868.         if (inst->Instruction.Opcode == TGSI_OPCODE_DDX_FINE ||
  5869.                 inst->Instruction.Opcode == TGSI_OPCODE_DDY_FINE) {
  5870.                 tex.inst_mod = 1; /* per pixel gradient calculation instead of per 2x2 quad */
  5871.         }
  5872.  
  5873.         if (inst->Instruction.Opcode == TGSI_OPCODE_TG4) {
  5874.                 int8_t texture_component_select = ctx->literals[4 * inst->Src[1].Register.Index + inst->Src[1].Register.SwizzleX];
  5875.                 tex.inst_mod = texture_component_select;
  5876.  
  5877.                 if (ctx->bc->chip_class == CAYMAN) {
  5878.                 /* GATHER4 result order is different from TGSI TG4 */
  5879.                         tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 2) ? 0 : 7;
  5880.                         tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 4) ? 1 : 7;
  5881.                         tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 1) ? 2 : 7;
  5882.                         tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
  5883.                 } else {
  5884.                         tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
  5885.                         tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7;
  5886.                         tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;
  5887.                         tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
  5888.                 }
  5889.         }
  5890.         else if (inst->Instruction.Opcode == TGSI_OPCODE_LODQ) {
  5891.                 tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
  5892.                 tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;
  5893.                 tex.dst_sel_z = 7;
  5894.                 tex.dst_sel_w = 7;
  5895.         }
  5896.         else {
  5897.                 tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;
  5898.                 tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
  5899.                 tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7;
  5900.                 tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
  5901.         }
  5902.  
  5903.  
  5904.         if (inst->Instruction.Opcode == TGSI_OPCODE_TXQ_LZ) {
  5905.                 tex.src_sel_x = 4;
  5906.                 tex.src_sel_y = 4;
  5907.                 tex.src_sel_z = 4;
  5908.                 tex.src_sel_w = 4;
  5909.         } else if (src_loaded) {
  5910.                 tex.src_sel_x = 0;
  5911.                 tex.src_sel_y = 1;
  5912.                 tex.src_sel_z = 2;
  5913.                 tex.src_sel_w = 3;
  5914.         } else {
  5915.                 tex.src_sel_x = ctx->src[0].swizzle[0];
  5916.                 tex.src_sel_y = ctx->src[0].swizzle[1];
  5917.                 tex.src_sel_z = ctx->src[0].swizzle[2];
  5918.                 tex.src_sel_w = ctx->src[0].swizzle[3];
  5919.                 tex.src_rel = ctx->src[0].rel;
  5920.         }
  5921.  
  5922.         if (inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
  5923.             inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
  5924.             inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
  5925.             inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
  5926.                 tex.src_sel_x = 1;
  5927.                 tex.src_sel_y = 0;
  5928.                 tex.src_sel_z = 3;
  5929.                 tex.src_sel_w = 2; /* route Z compare or Lod value into W */
  5930.         }
  5931.  
  5932.         if (inst->Texture.Texture != TGSI_TEXTURE_RECT &&
  5933.             inst->Texture.Texture != TGSI_TEXTURE_SHADOWRECT) {
  5934.                 tex.coord_type_x = 1;
  5935.                 tex.coord_type_y = 1;
  5936.         }
  5937.         tex.coord_type_z = 1;
  5938.         tex.coord_type_w = 1;
  5939.  
  5940.         tex.offset_x = offset_x;
  5941.         tex.offset_y = offset_y;
  5942.         if (inst->Instruction.Opcode == TGSI_OPCODE_TG4 &&
  5943.                 (inst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY ||
  5944.                  inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D_ARRAY)) {
  5945.                 tex.offset_z = 0;
  5946.         }
  5947.         else {
  5948.                 tex.offset_z = offset_z;
  5949.         }
  5950.  
  5951.         /* Put the depth for comparison in W.
  5952.          * TGSI_TEXTURE_SHADOW2D_ARRAY already has the depth in W.
  5953.          * Some instructions expect the depth in Z. */
  5954.         if ((inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D ||
  5955.              inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D ||
  5956.              inst->Texture.Texture == TGSI_TEXTURE_SHADOWRECT ||
  5957.              inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D_ARRAY) &&
  5958.             opcode != FETCH_OP_SAMPLE_C_L &&
  5959.             opcode != FETCH_OP_SAMPLE_C_LB) {
  5960.                 tex.src_sel_w = tex.src_sel_z;
  5961.         }
  5962.  
  5963.         if (inst->Texture.Texture == TGSI_TEXTURE_1D_ARRAY ||
  5964.             inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D_ARRAY) {
  5965.                 if (opcode == FETCH_OP_SAMPLE_C_L ||
  5966.                     opcode == FETCH_OP_SAMPLE_C_LB) {
  5967.                         /* the array index is read from Y */
  5968.                         tex.coord_type_y = 0;
  5969.                 } else {
  5970.                         /* the array index is read from Z */
  5971.                         tex.coord_type_z = 0;
  5972.                         tex.src_sel_z = tex.src_sel_y;
  5973.                 }
  5974.         } else if (inst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY ||
  5975.                    inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D_ARRAY ||
  5976.                    ((inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
  5977.                     inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
  5978.                     (ctx->bc->chip_class >= EVERGREEN)))
  5979.                 /* the array index is read from Z */
  5980.                 tex.coord_type_z = 0;
  5981.  
  5982.         /* mask unused source components */
  5983.         if (opcode == FETCH_OP_SAMPLE || opcode == FETCH_OP_GATHER4) {
  5984.                 switch (inst->Texture.Texture) {
  5985.                 case TGSI_TEXTURE_2D:
  5986.                 case TGSI_TEXTURE_RECT:
  5987.                         tex.src_sel_z = 7;
  5988.                         tex.src_sel_w = 7;
  5989.                         break;
  5990.                 case TGSI_TEXTURE_1D_ARRAY:
  5991.                         tex.src_sel_y = 7;
  5992.                         tex.src_sel_w = 7;
  5993.                         break;
  5994.                 case TGSI_TEXTURE_1D:
  5995.                         tex.src_sel_y = 7;
  5996.                         tex.src_sel_z = 7;
  5997.                         tex.src_sel_w = 7;
  5998.                         break;
  5999.                 }
  6000.         }
  6001.  
  6002.         r = r600_bytecode_add_tex(ctx->bc, &tex);
  6003.         if (r)
  6004.                 return r;
  6005.  
  6006.         /* add shadow ambient support  - gallium doesn't do it yet */
  6007.         return 0;
  6008. }
  6009.  
  6010. static int tgsi_lrp(struct r600_shader_ctx *ctx)
  6011. {
  6012.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6013.         struct r600_bytecode_alu alu;
  6014.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  6015.         unsigned i, temp_regs[2];
  6016.         int r;
  6017.  
  6018.         /* optimize if it's just an equal balance */
  6019.         if (ctx->src[0].sel == V_SQ_ALU_SRC_0_5) {
  6020.                 for (i = 0; i < lasti + 1; i++) {
  6021.                         if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  6022.                                 continue;
  6023.  
  6024.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6025.                         alu.op = ALU_OP2_ADD;
  6026.                         r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
  6027.                         r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
  6028.                         alu.omod = 3;
  6029.                         tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  6030.                         alu.dst.chan = i;
  6031.                         if (i == lasti) {
  6032.                                 alu.last = 1;
  6033.                         }
  6034.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6035.                         if (r)
  6036.                                 return r;
  6037.                 }
  6038.                 return 0;
  6039.         }
  6040.  
  6041.         /* 1 - src0 */
  6042.         for (i = 0; i < lasti + 1; i++) {
  6043.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  6044.                         continue;
  6045.  
  6046.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6047.                 alu.op = ALU_OP2_ADD;
  6048.                 alu.src[0].sel = V_SQ_ALU_SRC_1;
  6049.                 alu.src[0].chan = 0;
  6050.                 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
  6051.                 r600_bytecode_src_toggle_neg(&alu.src[1]);
  6052.                 alu.dst.sel = ctx->temp_reg;
  6053.                 alu.dst.chan = i;
  6054.                 if (i == lasti) {
  6055.                         alu.last = 1;
  6056.                 }
  6057.                 alu.dst.write = 1;
  6058.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6059.                 if (r)
  6060.                         return r;
  6061.         }
  6062.  
  6063.         /* (1 - src0) * src2 */
  6064.         for (i = 0; i < lasti + 1; i++) {
  6065.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  6066.                         continue;
  6067.  
  6068.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6069.                 alu.op = ALU_OP2_MUL;
  6070.                 alu.src[0].sel = ctx->temp_reg;
  6071.                 alu.src[0].chan = i;
  6072.                 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
  6073.                 alu.dst.sel = ctx->temp_reg;
  6074.                 alu.dst.chan = i;
  6075.                 if (i == lasti) {
  6076.                         alu.last = 1;
  6077.                 }
  6078.                 alu.dst.write = 1;
  6079.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6080.                 if (r)
  6081.                         return r;
  6082.         }
  6083.  
  6084.         /* src0 * src1 + (1 - src0) * src2 */
  6085.         if (ctx->src[0].abs)
  6086.                 temp_regs[0] = r600_get_temp(ctx);
  6087.         else
  6088.                 temp_regs[0] = 0;
  6089.         if (ctx->src[1].abs)
  6090.                 temp_regs[1] = r600_get_temp(ctx);
  6091.         else
  6092.                 temp_regs[1] = 0;
  6093.  
  6094.         for (i = 0; i < lasti + 1; i++) {
  6095.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  6096.                         continue;
  6097.  
  6098.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6099.                 alu.op = ALU_OP3_MULADD;
  6100.                 alu.is_op3 = 1;
  6101.                 r = tgsi_make_src_for_op3(ctx, temp_regs[0], i, &alu.src[0], &ctx->src[0]);
  6102.                 if (r)
  6103.                         return r;
  6104.                 r = tgsi_make_src_for_op3(ctx, temp_regs[1], i, &alu.src[1], &ctx->src[1]);
  6105.                 if (r)
  6106.                         return r;
  6107.                 alu.src[2].sel = ctx->temp_reg;
  6108.                 alu.src[2].chan = i;
  6109.  
  6110.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  6111.                 alu.dst.chan = i;
  6112.                 if (i == lasti) {
  6113.                         alu.last = 1;
  6114.                 }
  6115.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6116.                 if (r)
  6117.                         return r;
  6118.         }
  6119.         return 0;
  6120. }
  6121.  
  6122. static int tgsi_cmp(struct r600_shader_ctx *ctx)
  6123. {
  6124.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6125.         struct r600_bytecode_alu alu;
  6126.         int i, r, j;
  6127.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  6128.         int temp_regs[3];
  6129.  
  6130.         for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
  6131.                 temp_regs[j] = 0;
  6132.                 if (ctx->src[j].abs)
  6133.                         temp_regs[j] = r600_get_temp(ctx);
  6134.         }
  6135.  
  6136.         for (i = 0; i < lasti + 1; i++) {
  6137.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  6138.                         continue;
  6139.  
  6140.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6141.                 alu.op = ALU_OP3_CNDGE;
  6142.                 r = tgsi_make_src_for_op3(ctx, temp_regs[0], i, &alu.src[0], &ctx->src[0]);
  6143.                 if (r)
  6144.                         return r;
  6145.                 r = tgsi_make_src_for_op3(ctx, temp_regs[1], i, &alu.src[1], &ctx->src[2]);
  6146.                 if (r)
  6147.                         return r;
  6148.                 r = tgsi_make_src_for_op3(ctx, temp_regs[2], i, &alu.src[2], &ctx->src[1]);
  6149.                 if (r)
  6150.                         return r;
  6151.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  6152.                 alu.dst.chan = i;
  6153.                 alu.dst.write = 1;
  6154.                 alu.is_op3 = 1;
  6155.                 if (i == lasti)
  6156.                         alu.last = 1;
  6157.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6158.                 if (r)
  6159.                         return r;
  6160.         }
  6161.         return 0;
  6162. }
  6163.  
  6164. static int tgsi_ucmp(struct r600_shader_ctx *ctx)
  6165. {
  6166.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6167.         struct r600_bytecode_alu alu;
  6168.         int i, r;
  6169.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  6170.  
  6171.         for (i = 0; i < lasti + 1; i++) {
  6172.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  6173.                         continue;
  6174.  
  6175.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6176.                 alu.op = ALU_OP3_CNDE_INT;
  6177.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  6178.                 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
  6179.                 r600_bytecode_src(&alu.src[2], &ctx->src[1], i);
  6180.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  6181.                 alu.dst.chan = i;
  6182.                 alu.dst.write = 1;
  6183.                 alu.is_op3 = 1;
  6184.                 if (i == lasti)
  6185.                         alu.last = 1;
  6186.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6187.                 if (r)
  6188.                         return r;
  6189.         }
  6190.         return 0;
  6191. }
  6192.  
  6193. static int tgsi_xpd(struct r600_shader_ctx *ctx)
  6194. {
  6195.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6196.         static const unsigned int src0_swizzle[] = {2, 0, 1};
  6197.         static const unsigned int src1_swizzle[] = {1, 2, 0};
  6198.         struct r600_bytecode_alu alu;
  6199.         uint32_t use_temp = 0;
  6200.         int i, r;
  6201.  
  6202.         if (inst->Dst[0].Register.WriteMask != 0xf)
  6203.                 use_temp = 1;
  6204.  
  6205.         for (i = 0; i < 4; i++) {
  6206.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6207.                 alu.op = ALU_OP2_MUL;
  6208.                 if (i < 3) {
  6209.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], src0_swizzle[i]);
  6210.                         r600_bytecode_src(&alu.src[1], &ctx->src[1], src1_swizzle[i]);
  6211.                 } else {
  6212.                         alu.src[0].sel = V_SQ_ALU_SRC_0;
  6213.                         alu.src[0].chan = i;
  6214.                         alu.src[1].sel = V_SQ_ALU_SRC_0;
  6215.                         alu.src[1].chan = i;
  6216.                 }
  6217.  
  6218.                 alu.dst.sel = ctx->temp_reg;
  6219.                 alu.dst.chan = i;
  6220.                 alu.dst.write = 1;
  6221.  
  6222.                 if (i == 3)
  6223.                         alu.last = 1;
  6224.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6225.                 if (r)
  6226.                         return r;
  6227.         }
  6228.  
  6229.         for (i = 0; i < 4; i++) {
  6230.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6231.                 alu.op = ALU_OP3_MULADD;
  6232.  
  6233.                 if (i < 3) {
  6234.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], src1_swizzle[i]);
  6235.                         r600_bytecode_src(&alu.src[1], &ctx->src[1], src0_swizzle[i]);
  6236.                 } else {
  6237.                         alu.src[0].sel = V_SQ_ALU_SRC_0;
  6238.                         alu.src[0].chan = i;
  6239.                         alu.src[1].sel = V_SQ_ALU_SRC_0;
  6240.                         alu.src[1].chan = i;
  6241.                 }
  6242.  
  6243.                 alu.src[2].sel = ctx->temp_reg;
  6244.                 alu.src[2].neg = 1;
  6245.                 alu.src[2].chan = i;
  6246.  
  6247.                 if (use_temp)
  6248.                         alu.dst.sel = ctx->temp_reg;
  6249.                 else
  6250.                         tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  6251.                 alu.dst.chan = i;
  6252.                 alu.dst.write = 1;
  6253.                 alu.is_op3 = 1;
  6254.                 if (i == 3)
  6255.                         alu.last = 1;
  6256.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6257.                 if (r)
  6258.                         return r;
  6259.         }
  6260.         if (use_temp)
  6261.                 return tgsi_helper_copy(ctx, inst);
  6262.         return 0;
  6263. }
  6264.  
  6265. static int tgsi_exp(struct r600_shader_ctx *ctx)
  6266. {
  6267.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6268.         struct r600_bytecode_alu alu;
  6269.         int r;
  6270.         int i;
  6271.  
  6272.         /* result.x = 2^floor(src); */
  6273.         if (inst->Dst[0].Register.WriteMask & 1) {
  6274.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6275.  
  6276.                 alu.op = ALU_OP1_FLOOR;
  6277.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6278.  
  6279.                 alu.dst.sel = ctx->temp_reg;
  6280.                 alu.dst.chan = 0;
  6281.                 alu.dst.write = 1;
  6282.                 alu.last = 1;
  6283.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6284.                 if (r)
  6285.                         return r;
  6286.  
  6287.                 if (ctx->bc->chip_class == CAYMAN) {
  6288.                         for (i = 0; i < 3; i++) {
  6289.                                 alu.op = ALU_OP1_EXP_IEEE;
  6290.                                 alu.src[0].sel = ctx->temp_reg;
  6291.                                 alu.src[0].chan = 0;
  6292.  
  6293.                                 alu.dst.sel = ctx->temp_reg;
  6294.                                 alu.dst.chan = i;
  6295.                                 alu.dst.write = i == 0;
  6296.                                 alu.last = i == 2;
  6297.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6298.                                 if (r)
  6299.                                         return r;
  6300.                         }
  6301.                 } else {
  6302.                         alu.op = ALU_OP1_EXP_IEEE;
  6303.                         alu.src[0].sel = ctx->temp_reg;
  6304.                         alu.src[0].chan = 0;
  6305.  
  6306.                         alu.dst.sel = ctx->temp_reg;
  6307.                         alu.dst.chan = 0;
  6308.                         alu.dst.write = 1;
  6309.                         alu.last = 1;
  6310.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6311.                         if (r)
  6312.                                 return r;
  6313.                 }
  6314.         }
  6315.  
  6316.         /* result.y = tmp - floor(tmp); */
  6317.         if ((inst->Dst[0].Register.WriteMask >> 1) & 1) {
  6318.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6319.  
  6320.                 alu.op = ALU_OP1_FRACT;
  6321.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6322.  
  6323.                 alu.dst.sel = ctx->temp_reg;
  6324. #if 0
  6325.                 r = tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  6326.                 if (r)
  6327.                         return r;
  6328. #endif
  6329.                 alu.dst.write = 1;
  6330.                 alu.dst.chan = 1;
  6331.  
  6332.                 alu.last = 1;
  6333.  
  6334.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6335.                 if (r)
  6336.                         return r;
  6337.         }
  6338.  
  6339.         /* result.z = RoughApprox2ToX(tmp);*/
  6340.         if ((inst->Dst[0].Register.WriteMask >> 2) & 0x1) {
  6341.                 if (ctx->bc->chip_class == CAYMAN) {
  6342.                         for (i = 0; i < 3; i++) {
  6343.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6344.                                 alu.op = ALU_OP1_EXP_IEEE;
  6345.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6346.  
  6347.                                 alu.dst.sel = ctx->temp_reg;
  6348.                                 alu.dst.chan = i;
  6349.                                 if (i == 2) {
  6350.                                         alu.dst.write = 1;
  6351.                                         alu.last = 1;
  6352.                                 }
  6353.  
  6354.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6355.                                 if (r)
  6356.                                         return r;
  6357.                         }
  6358.                 } else {
  6359.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6360.                         alu.op = ALU_OP1_EXP_IEEE;
  6361.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6362.  
  6363.                         alu.dst.sel = ctx->temp_reg;
  6364.                         alu.dst.write = 1;
  6365.                         alu.dst.chan = 2;
  6366.  
  6367.                         alu.last = 1;
  6368.  
  6369.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6370.                         if (r)
  6371.                                 return r;
  6372.                 }
  6373.         }
  6374.  
  6375.         /* result.w = 1.0;*/
  6376.         if ((inst->Dst[0].Register.WriteMask >> 3) & 0x1) {
  6377.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6378.  
  6379.                 alu.op = ALU_OP1_MOV;
  6380.                 alu.src[0].sel = V_SQ_ALU_SRC_1;
  6381.                 alu.src[0].chan = 0;
  6382.  
  6383.                 alu.dst.sel = ctx->temp_reg;
  6384.                 alu.dst.chan = 3;
  6385.                 alu.dst.write = 1;
  6386.                 alu.last = 1;
  6387.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6388.                 if (r)
  6389.                         return r;
  6390.         }
  6391.         return tgsi_helper_copy(ctx, inst);
  6392. }
  6393.  
  6394. static int tgsi_log(struct r600_shader_ctx *ctx)
  6395. {
  6396.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6397.         struct r600_bytecode_alu alu;
  6398.         int r;
  6399.         int i;
  6400.  
  6401.         /* result.x = floor(log2(|src|)); */
  6402.         if (inst->Dst[0].Register.WriteMask & 1) {
  6403.                 if (ctx->bc->chip_class == CAYMAN) {
  6404.                         for (i = 0; i < 3; i++) {
  6405.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6406.  
  6407.                                 alu.op = ALU_OP1_LOG_IEEE;
  6408.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6409.                                 r600_bytecode_src_set_abs(&alu.src[0]);
  6410.                        
  6411.                                 alu.dst.sel = ctx->temp_reg;
  6412.                                 alu.dst.chan = i;
  6413.                                 if (i == 0)
  6414.                                         alu.dst.write = 1;
  6415.                                 if (i == 2)
  6416.                                         alu.last = 1;
  6417.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6418.                                 if (r)
  6419.                                         return r;
  6420.                         }
  6421.  
  6422.                 } else {
  6423.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6424.  
  6425.                         alu.op = ALU_OP1_LOG_IEEE;
  6426.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6427.                         r600_bytecode_src_set_abs(&alu.src[0]);
  6428.                        
  6429.                         alu.dst.sel = ctx->temp_reg;
  6430.                         alu.dst.chan = 0;
  6431.                         alu.dst.write = 1;
  6432.                         alu.last = 1;
  6433.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6434.                         if (r)
  6435.                                 return r;
  6436.                 }
  6437.  
  6438.                 alu.op = ALU_OP1_FLOOR;
  6439.                 alu.src[0].sel = ctx->temp_reg;
  6440.                 alu.src[0].chan = 0;
  6441.  
  6442.                 alu.dst.sel = ctx->temp_reg;
  6443.                 alu.dst.chan = 0;
  6444.                 alu.dst.write = 1;
  6445.                 alu.last = 1;
  6446.  
  6447.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6448.                 if (r)
  6449.                         return r;
  6450.         }
  6451.  
  6452.         /* result.y = |src.x| / (2 ^ floor(log2(|src.x|))); */
  6453.         if ((inst->Dst[0].Register.WriteMask >> 1) & 1) {
  6454.  
  6455.                 if (ctx->bc->chip_class == CAYMAN) {
  6456.                         for (i = 0; i < 3; i++) {
  6457.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6458.  
  6459.                                 alu.op = ALU_OP1_LOG_IEEE;
  6460.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6461.                                 r600_bytecode_src_set_abs(&alu.src[0]);
  6462.  
  6463.                                 alu.dst.sel = ctx->temp_reg;
  6464.                                 alu.dst.chan = i;
  6465.                                 if (i == 1)
  6466.                                         alu.dst.write = 1;
  6467.                                 if (i == 2)
  6468.                                         alu.last = 1;
  6469.                                
  6470.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6471.                                 if (r)
  6472.                                         return r;      
  6473.                         }
  6474.                 } else {
  6475.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6476.  
  6477.                         alu.op = ALU_OP1_LOG_IEEE;
  6478.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6479.                         r600_bytecode_src_set_abs(&alu.src[0]);
  6480.  
  6481.                         alu.dst.sel = ctx->temp_reg;
  6482.                         alu.dst.chan = 1;
  6483.                         alu.dst.write = 1;
  6484.                         alu.last = 1;
  6485.  
  6486.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6487.                         if (r)
  6488.                                 return r;
  6489.                 }
  6490.  
  6491.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6492.  
  6493.                 alu.op = ALU_OP1_FLOOR;
  6494.                 alu.src[0].sel = ctx->temp_reg;
  6495.                 alu.src[0].chan = 1;
  6496.  
  6497.                 alu.dst.sel = ctx->temp_reg;
  6498.                 alu.dst.chan = 1;
  6499.                 alu.dst.write = 1;
  6500.                 alu.last = 1;
  6501.  
  6502.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6503.                 if (r)
  6504.                         return r;
  6505.  
  6506.                 if (ctx->bc->chip_class == CAYMAN) {
  6507.                         for (i = 0; i < 3; i++) {
  6508.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6509.                                 alu.op = ALU_OP1_EXP_IEEE;
  6510.                                 alu.src[0].sel = ctx->temp_reg;
  6511.                                 alu.src[0].chan = 1;
  6512.  
  6513.                                 alu.dst.sel = ctx->temp_reg;
  6514.                                 alu.dst.chan = i;
  6515.                                 if (i == 1)
  6516.                                         alu.dst.write = 1;
  6517.                                 if (i == 2)
  6518.                                         alu.last = 1;
  6519.  
  6520.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6521.                                 if (r)
  6522.                                         return r;
  6523.                         }
  6524.                 } else {
  6525.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6526.                         alu.op = ALU_OP1_EXP_IEEE;
  6527.                         alu.src[0].sel = ctx->temp_reg;
  6528.                         alu.src[0].chan = 1;
  6529.  
  6530.                         alu.dst.sel = ctx->temp_reg;
  6531.                         alu.dst.chan = 1;
  6532.                         alu.dst.write = 1;
  6533.                         alu.last = 1;
  6534.  
  6535.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6536.                         if (r)
  6537.                                 return r;
  6538.                 }
  6539.  
  6540.                 if (ctx->bc->chip_class == CAYMAN) {
  6541.                         for (i = 0; i < 3; i++) {
  6542.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6543.                                 alu.op = ALU_OP1_RECIP_IEEE;
  6544.                                 alu.src[0].sel = ctx->temp_reg;
  6545.                                 alu.src[0].chan = 1;
  6546.  
  6547.                                 alu.dst.sel = ctx->temp_reg;
  6548.                                 alu.dst.chan = i;
  6549.                                 if (i == 1)
  6550.                                         alu.dst.write = 1;
  6551.                                 if (i == 2)
  6552.                                         alu.last = 1;
  6553.                                
  6554.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6555.                                 if (r)
  6556.                                         return r;
  6557.                         }
  6558.                 } else {
  6559.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6560.                         alu.op = ALU_OP1_RECIP_IEEE;
  6561.                         alu.src[0].sel = ctx->temp_reg;
  6562.                         alu.src[0].chan = 1;
  6563.  
  6564.                         alu.dst.sel = ctx->temp_reg;
  6565.                         alu.dst.chan = 1;
  6566.                         alu.dst.write = 1;
  6567.                         alu.last = 1;
  6568.  
  6569.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6570.                         if (r)
  6571.                                 return r;
  6572.                 }
  6573.  
  6574.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6575.  
  6576.                 alu.op = ALU_OP2_MUL;
  6577.  
  6578.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6579.                 r600_bytecode_src_set_abs(&alu.src[0]);
  6580.  
  6581.                 alu.src[1].sel = ctx->temp_reg;
  6582.                 alu.src[1].chan = 1;
  6583.  
  6584.                 alu.dst.sel = ctx->temp_reg;
  6585.                 alu.dst.chan = 1;
  6586.                 alu.dst.write = 1;
  6587.                 alu.last = 1;
  6588.  
  6589.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6590.                 if (r)
  6591.                         return r;
  6592.         }
  6593.  
  6594.         /* result.z = log2(|src|);*/
  6595.         if ((inst->Dst[0].Register.WriteMask >> 2) & 1) {
  6596.                 if (ctx->bc->chip_class == CAYMAN) {
  6597.                         for (i = 0; i < 3; i++) {
  6598.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6599.  
  6600.                                 alu.op = ALU_OP1_LOG_IEEE;
  6601.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6602.                                 r600_bytecode_src_set_abs(&alu.src[0]);
  6603.  
  6604.                                 alu.dst.sel = ctx->temp_reg;
  6605.                                 if (i == 2)
  6606.                                         alu.dst.write = 1;
  6607.                                 alu.dst.chan = i;
  6608.                                 if (i == 2)
  6609.                                         alu.last = 1;
  6610.  
  6611.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6612.                                 if (r)
  6613.                                         return r;
  6614.                         }
  6615.                 } else {
  6616.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6617.  
  6618.                         alu.op = ALU_OP1_LOG_IEEE;
  6619.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6620.                         r600_bytecode_src_set_abs(&alu.src[0]);
  6621.  
  6622.                         alu.dst.sel = ctx->temp_reg;
  6623.                         alu.dst.write = 1;
  6624.                         alu.dst.chan = 2;
  6625.                         alu.last = 1;
  6626.  
  6627.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  6628.                         if (r)
  6629.                                 return r;
  6630.                 }
  6631.         }
  6632.  
  6633.         /* result.w = 1.0; */
  6634.         if ((inst->Dst[0].Register.WriteMask >> 3) & 1) {
  6635.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6636.  
  6637.                 alu.op = ALU_OP1_MOV;
  6638.                 alu.src[0].sel = V_SQ_ALU_SRC_1;
  6639.                 alu.src[0].chan = 0;
  6640.  
  6641.                 alu.dst.sel = ctx->temp_reg;
  6642.                 alu.dst.chan = 3;
  6643.                 alu.dst.write = 1;
  6644.                 alu.last = 1;
  6645.  
  6646.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6647.                 if (r)
  6648.                         return r;
  6649.         }
  6650.  
  6651.         return tgsi_helper_copy(ctx, inst);
  6652. }
  6653.  
  6654. static int tgsi_eg_arl(struct r600_shader_ctx *ctx)
  6655. {
  6656.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6657.         struct r600_bytecode_alu alu;
  6658.         int r;
  6659.         int i, lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  6660.         unsigned reg = inst->Dst[0].Register.Index > 0 ? ctx->bc->index_reg[inst->Dst[0].Register.Index - 1] : ctx->bc->ar_reg;
  6661.  
  6662.         assert(inst->Dst[0].Register.Index < 3);
  6663.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6664.  
  6665.         switch (inst->Instruction.Opcode) {
  6666.         case TGSI_OPCODE_ARL:
  6667.                 alu.op = ALU_OP1_FLT_TO_INT_FLOOR;
  6668.                 break;
  6669.         case TGSI_OPCODE_ARR:
  6670.                 alu.op = ALU_OP1_FLT_TO_INT;
  6671.                 break;
  6672.         case TGSI_OPCODE_UARL:
  6673.                 alu.op = ALU_OP1_MOV;
  6674.                 break;
  6675.         default:
  6676.                 assert(0);
  6677.                 return -1;
  6678.         }
  6679.  
  6680.         for (i = 0; i <= lasti; ++i) {
  6681.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  6682.                         continue;
  6683.                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  6684.                 alu.last = i == lasti;
  6685.                 alu.dst.sel = reg;
  6686.                 alu.dst.chan = i;
  6687.                 alu.dst.write = 1;
  6688.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6689.                 if (r)
  6690.                         return r;
  6691.         }
  6692.  
  6693.         if (inst->Dst[0].Register.Index > 0)
  6694.                 ctx->bc->index_loaded[inst->Dst[0].Register.Index - 1] = 0;
  6695.         else
  6696.                 ctx->bc->ar_loaded = 0;
  6697.  
  6698.         return 0;
  6699. }
  6700. static int tgsi_r600_arl(struct r600_shader_ctx *ctx)
  6701. {
  6702.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6703.         struct r600_bytecode_alu alu;
  6704.         int r;
  6705.         int i, lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  6706.  
  6707.         switch (inst->Instruction.Opcode) {
  6708.         case TGSI_OPCODE_ARL:
  6709.                 memset(&alu, 0, sizeof(alu));
  6710.                 alu.op = ALU_OP1_FLOOR;
  6711.                 alu.dst.sel = ctx->bc->ar_reg;
  6712.                 alu.dst.write = 1;
  6713.                 for (i = 0; i <= lasti; ++i) {
  6714.                         if (inst->Dst[0].Register.WriteMask & (1 << i))  {
  6715.                                 alu.dst.chan = i;
  6716.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  6717.                                 alu.last = i == lasti;
  6718.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  6719.                                         return r;
  6720.                         }
  6721.                 }
  6722.  
  6723.                 memset(&alu, 0, sizeof(alu));
  6724.                 alu.op = ALU_OP1_FLT_TO_INT;
  6725.                 alu.src[0].sel = ctx->bc->ar_reg;
  6726.                 alu.dst.sel = ctx->bc->ar_reg;
  6727.                 alu.dst.write = 1;
  6728.                 /* FLT_TO_INT is trans-only on r600/r700 */
  6729.                 alu.last = TRUE;
  6730.                 for (i = 0; i <= lasti; ++i) {
  6731.                         alu.dst.chan = i;
  6732.                         alu.src[0].chan = i;
  6733.                         if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  6734.                                 return r;
  6735.                 }
  6736.                 break;
  6737.         case TGSI_OPCODE_ARR:
  6738.                 memset(&alu, 0, sizeof(alu));
  6739.                 alu.op = ALU_OP1_FLT_TO_INT;
  6740.                 alu.dst.sel = ctx->bc->ar_reg;
  6741.                 alu.dst.write = 1;
  6742.                 /* FLT_TO_INT is trans-only on r600/r700 */
  6743.                 alu.last = TRUE;
  6744.                 for (i = 0; i <= lasti; ++i) {
  6745.                         if (inst->Dst[0].Register.WriteMask & (1 << i)) {
  6746.                                 alu.dst.chan = i;
  6747.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  6748.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  6749.                                         return r;
  6750.                         }
  6751.                 }
  6752.                 break;
  6753.         case TGSI_OPCODE_UARL:
  6754.                 memset(&alu, 0, sizeof(alu));
  6755.                 alu.op = ALU_OP1_MOV;
  6756.                 alu.dst.sel = ctx->bc->ar_reg;
  6757.                 alu.dst.write = 1;
  6758.                 for (i = 0; i <= lasti; ++i) {
  6759.                         if (inst->Dst[0].Register.WriteMask & (1 << i)) {
  6760.                                 alu.dst.chan = i;
  6761.                                 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  6762.                                 alu.last = i == lasti;
  6763.                                 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
  6764.                                         return r;
  6765.                         }
  6766.                 }
  6767.                 break;
  6768.         default:
  6769.                 assert(0);
  6770.                 return -1;
  6771.         }
  6772.  
  6773.         ctx->bc->ar_loaded = 0;
  6774.         return 0;
  6775. }
  6776.  
  6777. static int tgsi_opdst(struct r600_shader_ctx *ctx)
  6778. {
  6779.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  6780.         struct r600_bytecode_alu alu;
  6781.         int i, r = 0;
  6782.  
  6783.         for (i = 0; i < 4; i++) {
  6784.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6785.  
  6786.                 alu.op = ALU_OP2_MUL;
  6787.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  6788.  
  6789.                 if (i == 0 || i == 3) {
  6790.                         alu.src[0].sel = V_SQ_ALU_SRC_1;
  6791.                 } else {
  6792.                         r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
  6793.                 }
  6794.  
  6795.                 if (i == 0 || i == 2) {
  6796.                         alu.src[1].sel = V_SQ_ALU_SRC_1;
  6797.                 } else {
  6798.                         r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
  6799.                 }
  6800.                 if (i == 3)
  6801.                         alu.last = 1;
  6802.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  6803.                 if (r)
  6804.                         return r;
  6805.         }
  6806.         return 0;
  6807. }
  6808.  
  6809. static int emit_logic_pred(struct r600_shader_ctx *ctx, int opcode, int alu_type)
  6810. {
  6811.         struct r600_bytecode_alu alu;
  6812.         int r;
  6813.  
  6814.         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  6815.         alu.op = opcode;
  6816.         alu.execute_mask = 1;
  6817.         alu.update_pred = 1;
  6818.  
  6819.         alu.dst.sel = ctx->temp_reg;
  6820.         alu.dst.write = 1;
  6821.         alu.dst.chan = 0;
  6822.  
  6823.         r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
  6824.         alu.src[1].sel = V_SQ_ALU_SRC_0;
  6825.         alu.src[1].chan = 0;
  6826.  
  6827.         alu.last = 1;
  6828.  
  6829.         r = r600_bytecode_add_alu_type(ctx->bc, &alu, alu_type);
  6830.         if (r)
  6831.                 return r;
  6832.         return 0;
  6833. }
  6834.  
  6835. static int pops(struct r600_shader_ctx *ctx, int pops)
  6836. {
  6837.         unsigned force_pop = ctx->bc->force_add_cf;
  6838.  
  6839.         if (!force_pop) {
  6840.                 int alu_pop = 3;
  6841.                 if (ctx->bc->cf_last) {
  6842.                         if (ctx->bc->cf_last->op == CF_OP_ALU)
  6843.                                 alu_pop = 0;
  6844.                         else if (ctx->bc->cf_last->op == CF_OP_ALU_POP_AFTER)
  6845.                                 alu_pop = 1;
  6846.                 }
  6847.                 alu_pop += pops;
  6848.                 if (alu_pop == 1) {
  6849.                         ctx->bc->cf_last->op = CF_OP_ALU_POP_AFTER;
  6850.                         ctx->bc->force_add_cf = 1;
  6851.                 } else if (alu_pop == 2) {
  6852.                         ctx->bc->cf_last->op = CF_OP_ALU_POP2_AFTER;
  6853.                         ctx->bc->force_add_cf = 1;
  6854.                 } else {
  6855.                         force_pop = 1;
  6856.                 }
  6857.         }
  6858.  
  6859.         if (force_pop) {
  6860.                 r600_bytecode_add_cfinst(ctx->bc, CF_OP_POP);
  6861.                 ctx->bc->cf_last->pop_count = pops;
  6862.                 ctx->bc->cf_last->cf_addr = ctx->bc->cf_last->id + 2;
  6863.         }
  6864.  
  6865.         return 0;
  6866. }
  6867.  
  6868. static inline void callstack_update_max_depth(struct r600_shader_ctx *ctx,
  6869.                                               unsigned reason)
  6870. {
  6871.         struct r600_stack_info *stack = &ctx->bc->stack;
  6872.         unsigned elements, entries;
  6873.  
  6874.         unsigned entry_size = stack->entry_size;
  6875.  
  6876.         elements = (stack->loop + stack->push_wqm ) * entry_size;
  6877.         elements += stack->push;
  6878.  
  6879.         switch (ctx->bc->chip_class) {
  6880.         case R600:
  6881.         case R700:
  6882.                 /* pre-r8xx: if any non-WQM PUSH instruction is invoked, 2 elements on
  6883.                  * the stack must be reserved to hold the current active/continue
  6884.                  * masks */
  6885.                 if (reason == FC_PUSH_VPM) {
  6886.                         elements += 2;
  6887.                 }
  6888.                 break;
  6889.  
  6890.         case CAYMAN:
  6891.                 /* r9xx: any stack operation on empty stack consumes 2 additional
  6892.                  * elements */
  6893.                 elements += 2;
  6894.  
  6895.                 /* fallthrough */
  6896.                 /* FIXME: do the two elements added above cover the cases for the
  6897.                  * r8xx+ below? */
  6898.  
  6899.         case EVERGREEN:
  6900.                 /* r8xx+: 2 extra elements are not always required, but one extra
  6901.                  * element must be added for each of the following cases:
  6902.                  * 1. There is an ALU_ELSE_AFTER instruction at the point of greatest
  6903.                  *    stack usage.
  6904.                  *    (Currently we don't use ALU_ELSE_AFTER.)
  6905.                  * 2. There are LOOP/WQM frames on the stack when any flavor of non-WQM
  6906.                  *    PUSH instruction executed.
  6907.                  *
  6908.                  *    NOTE: it seems we also need to reserve additional element in some
  6909.                  *    other cases, e.g. when we have 4 levels of PUSH_VPM in the shader,
  6910.                  *    then STACK_SIZE should be 2 instead of 1 */
  6911.                 if (reason == FC_PUSH_VPM) {
  6912.                         elements += 1;
  6913.                 }
  6914.                 break;
  6915.  
  6916.         default:
  6917.                 assert(0);
  6918.                 break;
  6919.         }
  6920.  
  6921.         /* NOTE: it seems STACK_SIZE is interpreted by hw as if entry_size is 4
  6922.          * for all chips, so we use 4 in the final formula, not the real entry_size
  6923.          * for the chip */
  6924.         entry_size = 4;
  6925.  
  6926.         entries = (elements + (entry_size - 1)) / entry_size;
  6927.  
  6928.         if (entries > stack->max_entries)
  6929.                 stack->max_entries = entries;
  6930. }
  6931.  
  6932. static inline void callstack_pop(struct r600_shader_ctx *ctx, unsigned reason)
  6933. {
  6934.         switch(reason) {
  6935.         case FC_PUSH_VPM:
  6936.                 --ctx->bc->stack.push;
  6937.                 assert(ctx->bc->stack.push >= 0);
  6938.                 break;
  6939.         case FC_PUSH_WQM:
  6940.                 --ctx->bc->stack.push_wqm;
  6941.                 assert(ctx->bc->stack.push_wqm >= 0);
  6942.                 break;
  6943.         case FC_LOOP:
  6944.                 --ctx->bc->stack.loop;
  6945.                 assert(ctx->bc->stack.loop >= 0);
  6946.                 break;
  6947.         default:
  6948.                 assert(0);
  6949.                 break;
  6950.         }
  6951. }
  6952.  
  6953. static inline void callstack_push(struct r600_shader_ctx *ctx, unsigned reason)
  6954. {
  6955.         switch (reason) {
  6956.         case FC_PUSH_VPM:
  6957.                 ++ctx->bc->stack.push;
  6958.                 break;
  6959.         case FC_PUSH_WQM:
  6960.                 ++ctx->bc->stack.push_wqm;
  6961.         case FC_LOOP:
  6962.                 ++ctx->bc->stack.loop;
  6963.                 break;
  6964.         default:
  6965.                 assert(0);
  6966.         }
  6967.  
  6968.         callstack_update_max_depth(ctx, reason);
  6969. }
  6970.  
  6971. static void fc_set_mid(struct r600_shader_ctx *ctx, int fc_sp)
  6972. {
  6973.         struct r600_cf_stack_entry *sp = &ctx->bc->fc_stack[fc_sp];
  6974.  
  6975.         sp->mid = realloc((void *)sp->mid,
  6976.                                                 sizeof(struct r600_bytecode_cf *) * (sp->num_mid + 1));
  6977.         sp->mid[sp->num_mid] = ctx->bc->cf_last;
  6978.         sp->num_mid++;
  6979. }
  6980.  
  6981. static void fc_pushlevel(struct r600_shader_ctx *ctx, int type)
  6982. {
  6983.         ctx->bc->fc_sp++;
  6984.         ctx->bc->fc_stack[ctx->bc->fc_sp].type = type;
  6985.         ctx->bc->fc_stack[ctx->bc->fc_sp].start = ctx->bc->cf_last;
  6986. }
  6987.  
  6988. static void fc_poplevel(struct r600_shader_ctx *ctx)
  6989. {
  6990.         struct r600_cf_stack_entry *sp = &ctx->bc->fc_stack[ctx->bc->fc_sp];
  6991.         free(sp->mid);
  6992.         sp->mid = NULL;
  6993.         sp->num_mid = 0;
  6994.         sp->start = NULL;
  6995.         sp->type = 0;
  6996.         ctx->bc->fc_sp--;
  6997. }
  6998.  
  6999. #if 0
  7000. static int emit_return(struct r600_shader_ctx *ctx)
  7001. {
  7002.         r600_bytecode_add_cfinst(ctx->bc, CF_OP_RETURN));
  7003.         return 0;
  7004. }
  7005.  
  7006. static int emit_jump_to_offset(struct r600_shader_ctx *ctx, int pops, int offset)
  7007. {
  7008.  
  7009.         r600_bytecode_add_cfinst(ctx->bc, CF_OP_JUMP));
  7010.         ctx->bc->cf_last->pop_count = pops;
  7011.         /* XXX work out offset */
  7012.         return 0;
  7013. }
  7014.  
  7015. static int emit_setret_in_loop_flag(struct r600_shader_ctx *ctx, unsigned flag_value)
  7016. {
  7017.         return 0;
  7018. }
  7019.  
  7020. static void emit_testflag(struct r600_shader_ctx *ctx)
  7021. {
  7022.  
  7023. }
  7024.  
  7025. static void emit_return_on_flag(struct r600_shader_ctx *ctx, unsigned ifidx)
  7026. {
  7027.         emit_testflag(ctx);
  7028.         emit_jump_to_offset(ctx, 1, 4);
  7029.         emit_setret_in_loop_flag(ctx, V_SQ_ALU_SRC_0);
  7030.         pops(ctx, ifidx + 1);
  7031.         emit_return(ctx);
  7032. }
  7033.  
  7034. static void break_loop_on_flag(struct r600_shader_ctx *ctx, unsigned fc_sp)
  7035. {
  7036.         emit_testflag(ctx);
  7037.  
  7038.         r600_bytecode_add_cfinst(ctx->bc, ctx->inst_info->op);
  7039.         ctx->bc->cf_last->pop_count = 1;
  7040.  
  7041.         fc_set_mid(ctx, fc_sp);
  7042.  
  7043.         pops(ctx, 1);
  7044. }
  7045. #endif
  7046.  
  7047. static int emit_if(struct r600_shader_ctx *ctx, int opcode)
  7048. {
  7049.         int alu_type = CF_OP_ALU_PUSH_BEFORE;
  7050.  
  7051.         /* There is a hardware bug on Cayman where a BREAK/CONTINUE followed by
  7052.          * LOOP_STARTxxx for nested loops may put the branch stack into a state
  7053.          * such that ALU_PUSH_BEFORE doesn't work as expected. Workaround this
  7054.          * by replacing the ALU_PUSH_BEFORE with a PUSH + ALU */
  7055.         if (ctx->bc->chip_class == CAYMAN && ctx->bc->stack.loop > 1) {
  7056.                 r600_bytecode_add_cfinst(ctx->bc, CF_OP_PUSH);
  7057.                 ctx->bc->cf_last->cf_addr = ctx->bc->cf_last->id + 2;
  7058.                 alu_type = CF_OP_ALU;
  7059.         }
  7060.  
  7061.         emit_logic_pred(ctx, opcode, alu_type);
  7062.  
  7063.         r600_bytecode_add_cfinst(ctx->bc, CF_OP_JUMP);
  7064.  
  7065.         fc_pushlevel(ctx, FC_IF);
  7066.  
  7067.         callstack_push(ctx, FC_PUSH_VPM);
  7068.         return 0;
  7069. }
  7070.  
  7071. static int tgsi_if(struct r600_shader_ctx *ctx)
  7072. {
  7073.         return emit_if(ctx, ALU_OP2_PRED_SETNE);
  7074. }
  7075.  
  7076. static int tgsi_uif(struct r600_shader_ctx *ctx)
  7077. {
  7078.         return emit_if(ctx, ALU_OP2_PRED_SETNE_INT);
  7079. }
  7080.  
  7081. static int tgsi_else(struct r600_shader_ctx *ctx)
  7082. {
  7083.         r600_bytecode_add_cfinst(ctx->bc, CF_OP_ELSE);
  7084.         ctx->bc->cf_last->pop_count = 1;
  7085.  
  7086.         fc_set_mid(ctx, ctx->bc->fc_sp);
  7087.         ctx->bc->fc_stack[ctx->bc->fc_sp].start->cf_addr = ctx->bc->cf_last->id;
  7088.         return 0;
  7089. }
  7090.  
  7091. static int tgsi_endif(struct r600_shader_ctx *ctx)
  7092. {
  7093.         pops(ctx, 1);
  7094.         if (ctx->bc->fc_stack[ctx->bc->fc_sp].type != FC_IF) {
  7095.                 R600_ERR("if/endif unbalanced in shader\n");
  7096.                 return -1;
  7097.         }
  7098.  
  7099.         if (ctx->bc->fc_stack[ctx->bc->fc_sp].mid == NULL) {
  7100.                 ctx->bc->fc_stack[ctx->bc->fc_sp].start->cf_addr = ctx->bc->cf_last->id + 2;
  7101.                 ctx->bc->fc_stack[ctx->bc->fc_sp].start->pop_count = 1;
  7102.         } else {
  7103.                 ctx->bc->fc_stack[ctx->bc->fc_sp].mid[0]->cf_addr = ctx->bc->cf_last->id + 2;
  7104.         }
  7105.         fc_poplevel(ctx);
  7106.  
  7107.         callstack_pop(ctx, FC_PUSH_VPM);
  7108.         return 0;
  7109. }
  7110.  
  7111. static int tgsi_bgnloop(struct r600_shader_ctx *ctx)
  7112. {
  7113.         /* LOOP_START_DX10 ignores the LOOP_CONFIG* registers, so it is not
  7114.          * limited to 4096 iterations, like the other LOOP_* instructions. */
  7115.         r600_bytecode_add_cfinst(ctx->bc, CF_OP_LOOP_START_DX10);
  7116.  
  7117.         fc_pushlevel(ctx, FC_LOOP);
  7118.  
  7119.         /* check stack depth */
  7120.         callstack_push(ctx, FC_LOOP);
  7121.         return 0;
  7122. }
  7123.  
  7124. static int tgsi_endloop(struct r600_shader_ctx *ctx)
  7125. {
  7126.         int i;
  7127.  
  7128.         r600_bytecode_add_cfinst(ctx->bc, CF_OP_LOOP_END);
  7129.  
  7130.         if (ctx->bc->fc_stack[ctx->bc->fc_sp].type != FC_LOOP) {
  7131.                 R600_ERR("loop/endloop in shader code are not paired.\n");
  7132.                 return -EINVAL;
  7133.         }
  7134.  
  7135.         /* fixup loop pointers - from r600isa
  7136.            LOOP END points to CF after LOOP START,
  7137.            LOOP START point to CF after LOOP END
  7138.            BRK/CONT point to LOOP END CF
  7139.         */
  7140.         ctx->bc->cf_last->cf_addr = ctx->bc->fc_stack[ctx->bc->fc_sp].start->id + 2;
  7141.  
  7142.         ctx->bc->fc_stack[ctx->bc->fc_sp].start->cf_addr = ctx->bc->cf_last->id + 2;
  7143.  
  7144.         for (i = 0; i < ctx->bc->fc_stack[ctx->bc->fc_sp].num_mid; i++) {
  7145.                 ctx->bc->fc_stack[ctx->bc->fc_sp].mid[i]->cf_addr = ctx->bc->cf_last->id;
  7146.         }
  7147.         /* XXX add LOOPRET support */
  7148.         fc_poplevel(ctx);
  7149.         callstack_pop(ctx, FC_LOOP);
  7150.         return 0;
  7151. }
  7152.  
  7153. static int tgsi_loop_breakc(struct r600_shader_ctx *ctx)
  7154. {
  7155.         int r;
  7156.         unsigned int fscp;
  7157.  
  7158.         for (fscp = ctx->bc->fc_sp; fscp > 0; fscp--)
  7159.         {
  7160.                 if (FC_LOOP == ctx->bc->fc_stack[fscp].type)
  7161.                         break;
  7162.         }
  7163.         if (fscp == 0) {
  7164.                 R600_ERR("BREAKC not inside loop/endloop pair\n");
  7165.                 return -EINVAL;
  7166.         }
  7167.  
  7168.         if (ctx->bc->chip_class == EVERGREEN &&
  7169.             ctx->bc->family != CHIP_CYPRESS &&
  7170.             ctx->bc->family != CHIP_JUNIPER) {
  7171.                 /* HW bug: ALU_BREAK does not save the active mask correctly */
  7172.                 r = tgsi_uif(ctx);
  7173.                 if (r)
  7174.                         return r;
  7175.  
  7176.                 r = r600_bytecode_add_cfinst(ctx->bc, CF_OP_LOOP_BREAK);
  7177.                 if (r)
  7178.                         return r;
  7179.                 fc_set_mid(ctx, fscp);
  7180.  
  7181.                 return tgsi_endif(ctx);
  7182.         } else {
  7183.                 r = emit_logic_pred(ctx, ALU_OP2_PRED_SETE_INT, CF_OP_ALU_BREAK);
  7184.                 if (r)
  7185.                         return r;
  7186.                 fc_set_mid(ctx, fscp);
  7187.         }
  7188.  
  7189.         return 0;
  7190. }
  7191.  
  7192. static int tgsi_loop_brk_cont(struct r600_shader_ctx *ctx)
  7193. {
  7194.         unsigned int fscp;
  7195.  
  7196.         for (fscp = ctx->bc->fc_sp; fscp > 0; fscp--)
  7197.         {
  7198.                 if (FC_LOOP == ctx->bc->fc_stack[fscp].type)
  7199.                         break;
  7200.         }
  7201.  
  7202.         if (fscp == 0) {
  7203.                 R600_ERR("Break not inside loop/endloop pair\n");
  7204.                 return -EINVAL;
  7205.         }
  7206.  
  7207.         r600_bytecode_add_cfinst(ctx->bc, ctx->inst_info->op);
  7208.  
  7209.         fc_set_mid(ctx, fscp);
  7210.  
  7211.         return 0;
  7212. }
  7213.  
  7214. static int tgsi_gs_emit(struct r600_shader_ctx *ctx)
  7215. {
  7216.         if (ctx->inst_info->op == CF_OP_EMIT_VERTEX)
  7217.                 emit_gs_ring_writes(ctx, TRUE);
  7218.  
  7219.         return r600_bytecode_add_cfinst(ctx->bc, ctx->inst_info->op);
  7220. }
  7221.  
  7222. static int tgsi_umad(struct r600_shader_ctx *ctx)
  7223. {
  7224.         struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
  7225.         struct r600_bytecode_alu alu;
  7226.         int i, j, k, r;
  7227.         int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
  7228.  
  7229.         /* src0 * src1 */
  7230.         for (i = 0; i < lasti + 1; i++) {
  7231.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  7232.                         continue;
  7233.  
  7234.                 if (ctx->bc->chip_class == CAYMAN) {
  7235.                         for (j = 0 ; j < 4; j++) {
  7236.                                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  7237.  
  7238.                                 alu.op = ALU_OP2_MULLO_UINT;
  7239.                                 for (k = 0; k < inst->Instruction.NumSrcRegs; k++) {
  7240.                                         r600_bytecode_src(&alu.src[k], &ctx->src[k], i);
  7241.                                 }
  7242.                                 alu.dst.chan = j;
  7243.                                 alu.dst.sel = ctx->temp_reg;
  7244.                                 alu.dst.write = (j == i);
  7245.                                 if (j == 3)
  7246.                                         alu.last = 1;
  7247.                                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  7248.                                 if (r)
  7249.                                         return r;
  7250.                         }
  7251.                 } else {
  7252.                         memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  7253.  
  7254.                         alu.dst.chan = i;
  7255.                         alu.dst.sel = ctx->temp_reg;
  7256.                         alu.dst.write = 1;
  7257.  
  7258.                         alu.op = ALU_OP2_MULLO_UINT;
  7259.                         for (j = 0; j < 2; j++) {
  7260.                                 r600_bytecode_src(&alu.src[j], &ctx->src[j], i);
  7261.                         }
  7262.  
  7263.                         alu.last = 1;
  7264.                         r = r600_bytecode_add_alu(ctx->bc, &alu);
  7265.                         if (r)
  7266.                                 return r;
  7267.                 }
  7268.         }
  7269.  
  7270.  
  7271.         for (i = 0; i < lasti + 1; i++) {
  7272.                 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
  7273.                         continue;
  7274.  
  7275.                 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
  7276.                 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
  7277.  
  7278.                 alu.op = ALU_OP2_ADD_INT;
  7279.  
  7280.                 alu.src[0].sel = ctx->temp_reg;
  7281.                 alu.src[0].chan = i;
  7282.                
  7283.                 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
  7284.                 if (i == lasti) {
  7285.                         alu.last = 1;
  7286.                 }
  7287.                 r = r600_bytecode_add_alu(ctx->bc, &alu);
  7288.                 if (r)
  7289.                         return r;
  7290.         }
  7291.         return 0;
  7292. }
  7293.  
  7294. static const struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[] = {
  7295.         [TGSI_OPCODE_ARL]       = { ALU_OP0_NOP, tgsi_r600_arl},
  7296.         [TGSI_OPCODE_MOV]       = { ALU_OP1_MOV, tgsi_op2},
  7297.         [TGSI_OPCODE_LIT]       = { ALU_OP0_NOP, tgsi_lit},
  7298.  
  7299.         /* XXX:
  7300.          * For state trackers other than OpenGL, we'll want to use
  7301.          * _RECIP_IEEE instead.
  7302.          */
  7303.         [TGSI_OPCODE_RCP]       = { ALU_OP1_RECIP_CLAMPED, tgsi_trans_srcx_replicate},
  7304.  
  7305.         [TGSI_OPCODE_RSQ]       = { ALU_OP0_NOP, tgsi_rsq},
  7306.         [TGSI_OPCODE_EXP]       = { ALU_OP0_NOP, tgsi_exp},
  7307.         [TGSI_OPCODE_LOG]       = { ALU_OP0_NOP, tgsi_log},
  7308.         [TGSI_OPCODE_MUL]       = { ALU_OP2_MUL, tgsi_op2},
  7309.         [TGSI_OPCODE_ADD]       = { ALU_OP2_ADD, tgsi_op2},
  7310.         [TGSI_OPCODE_DP3]       = { ALU_OP2_DOT4, tgsi_dp},
  7311.         [TGSI_OPCODE_DP4]       = { ALU_OP2_DOT4, tgsi_dp},
  7312.         [TGSI_OPCODE_DST]       = { ALU_OP0_NOP, tgsi_opdst},
  7313.         [TGSI_OPCODE_MIN]       = { ALU_OP2_MIN, tgsi_op2},
  7314.         [TGSI_OPCODE_MAX]       = { ALU_OP2_MAX, tgsi_op2},
  7315.         [TGSI_OPCODE_SLT]       = { ALU_OP2_SETGT, tgsi_op2_swap},
  7316.         [TGSI_OPCODE_SGE]       = { ALU_OP2_SETGE, tgsi_op2},
  7317.         [TGSI_OPCODE_MAD]       = { ALU_OP3_MULADD, tgsi_op3},
  7318.         [TGSI_OPCODE_SUB]       = { ALU_OP2_ADD, tgsi_op2},
  7319.         [TGSI_OPCODE_LRP]       = { ALU_OP0_NOP, tgsi_lrp},
  7320.         [TGSI_OPCODE_FMA]       = { ALU_OP0_NOP, tgsi_unsupported},
  7321.         [TGSI_OPCODE_SQRT]      = { ALU_OP1_SQRT_IEEE, tgsi_trans_srcx_replicate},
  7322.         [TGSI_OPCODE_DP2A]      = { ALU_OP0_NOP, tgsi_unsupported},
  7323.         [22]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7324.         [23]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7325.         [TGSI_OPCODE_FRC]       = { ALU_OP1_FRACT, tgsi_op2},
  7326.         [TGSI_OPCODE_CLAMP]     = { ALU_OP0_NOP, tgsi_unsupported},
  7327.         [TGSI_OPCODE_FLR]       = { ALU_OP1_FLOOR, tgsi_op2},
  7328.         [TGSI_OPCODE_ROUND]     = { ALU_OP1_RNDNE, tgsi_op2},
  7329.         [TGSI_OPCODE_EX2]       = { ALU_OP1_EXP_IEEE, tgsi_trans_srcx_replicate},
  7330.         [TGSI_OPCODE_LG2]       = { ALU_OP1_LOG_IEEE, tgsi_trans_srcx_replicate},
  7331.         [TGSI_OPCODE_POW]       = { ALU_OP0_NOP, tgsi_pow},
  7332.         [TGSI_OPCODE_XPD]       = { ALU_OP0_NOP, tgsi_xpd},
  7333.         [32]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7334.         [TGSI_OPCODE_ABS]       = { ALU_OP1_MOV, tgsi_op2},
  7335.         [34]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7336.         [TGSI_OPCODE_DPH]       = { ALU_OP2_DOT4, tgsi_dp},
  7337.         [TGSI_OPCODE_COS]       = { ALU_OP1_COS, tgsi_trig},
  7338.         [TGSI_OPCODE_DDX]       = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
  7339.         [TGSI_OPCODE_DDY]       = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
  7340.         [TGSI_OPCODE_KILL]      = { ALU_OP2_KILLGT, tgsi_kill},  /* unconditional kill */
  7341.         [TGSI_OPCODE_PK2H]      = { ALU_OP0_NOP, tgsi_unsupported},
  7342.         [TGSI_OPCODE_PK2US]     = { ALU_OP0_NOP, tgsi_unsupported},
  7343.         [TGSI_OPCODE_PK4B]      = { ALU_OP0_NOP, tgsi_unsupported},
  7344.         [TGSI_OPCODE_PK4UB]     = { ALU_OP0_NOP, tgsi_unsupported},
  7345.         [44]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7346.         [TGSI_OPCODE_SEQ]       = { ALU_OP2_SETE, tgsi_op2},
  7347.         [46]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7348.         [TGSI_OPCODE_SGT]       = { ALU_OP2_SETGT, tgsi_op2},
  7349.         [TGSI_OPCODE_SIN]       = { ALU_OP1_SIN, tgsi_trig},
  7350.         [TGSI_OPCODE_SLE]       = { ALU_OP2_SETGE, tgsi_op2_swap},
  7351.         [TGSI_OPCODE_SNE]       = { ALU_OP2_SETNE, tgsi_op2},
  7352.         [51]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7353.         [TGSI_OPCODE_TEX]       = { FETCH_OP_SAMPLE, tgsi_tex},
  7354.         [TGSI_OPCODE_TXD]       = { FETCH_OP_SAMPLE_G, tgsi_tex},
  7355.         [TGSI_OPCODE_TXP]       = { FETCH_OP_SAMPLE, tgsi_tex},
  7356.         [TGSI_OPCODE_UP2H]      = { ALU_OP0_NOP, tgsi_unsupported},
  7357.         [TGSI_OPCODE_UP2US]     = { ALU_OP0_NOP, tgsi_unsupported},
  7358.         [TGSI_OPCODE_UP4B]      = { ALU_OP0_NOP, tgsi_unsupported},
  7359.         [TGSI_OPCODE_UP4UB]     = { ALU_OP0_NOP, tgsi_unsupported},
  7360.         [59]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7361.         [60]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7362.         [TGSI_OPCODE_ARR]       = { ALU_OP0_NOP, tgsi_r600_arl},
  7363.         [62]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7364.         [TGSI_OPCODE_CAL]       = { ALU_OP0_NOP, tgsi_unsupported},
  7365.         [TGSI_OPCODE_RET]       = { ALU_OP0_NOP, tgsi_unsupported},
  7366.         [TGSI_OPCODE_SSG]       = { ALU_OP0_NOP, tgsi_ssg},
  7367.         [TGSI_OPCODE_CMP]       = { ALU_OP0_NOP, tgsi_cmp},
  7368.         [TGSI_OPCODE_SCS]       = { ALU_OP0_NOP, tgsi_scs},
  7369.         [TGSI_OPCODE_TXB]       = { FETCH_OP_SAMPLE_LB, tgsi_tex},
  7370.         [69]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7371.         [TGSI_OPCODE_DIV]       = { ALU_OP0_NOP, tgsi_unsupported},
  7372.         [TGSI_OPCODE_DP2]       = { ALU_OP2_DOT4, tgsi_dp},
  7373.         [TGSI_OPCODE_TXL]       = { FETCH_OP_SAMPLE_L, tgsi_tex},
  7374.         [TGSI_OPCODE_BRK]       = { CF_OP_LOOP_BREAK, tgsi_loop_brk_cont},
  7375.         [TGSI_OPCODE_IF]        = { ALU_OP0_NOP, tgsi_if},
  7376.         [TGSI_OPCODE_UIF]       = { ALU_OP0_NOP, tgsi_uif},
  7377.         [76]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7378.         [TGSI_OPCODE_ELSE]      = { ALU_OP0_NOP, tgsi_else},
  7379.         [TGSI_OPCODE_ENDIF]     = { ALU_OP0_NOP, tgsi_endif},
  7380.         [TGSI_OPCODE_DDX_FINE]  = { ALU_OP0_NOP, tgsi_unsupported},
  7381.         [TGSI_OPCODE_DDY_FINE]  = { ALU_OP0_NOP, tgsi_unsupported},
  7382.         [TGSI_OPCODE_PUSHA]     = { ALU_OP0_NOP, tgsi_unsupported},
  7383.         [TGSI_OPCODE_POPA]      = { ALU_OP0_NOP, tgsi_unsupported},
  7384.         [TGSI_OPCODE_CEIL]      = { ALU_OP1_CEIL, tgsi_op2},
  7385.         [TGSI_OPCODE_I2F]       = { ALU_OP1_INT_TO_FLT, tgsi_op2_trans},
  7386.         [TGSI_OPCODE_NOT]       = { ALU_OP1_NOT_INT, tgsi_op2},
  7387.         [TGSI_OPCODE_TRUNC]     = { ALU_OP1_TRUNC, tgsi_op2},
  7388.         [TGSI_OPCODE_SHL]       = { ALU_OP2_LSHL_INT, tgsi_op2_trans},
  7389.         [88]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7390.         [TGSI_OPCODE_AND]       = { ALU_OP2_AND_INT, tgsi_op2},
  7391.         [TGSI_OPCODE_OR]        = { ALU_OP2_OR_INT, tgsi_op2},
  7392.         [TGSI_OPCODE_MOD]       = { ALU_OP0_NOP, tgsi_imod},
  7393.         [TGSI_OPCODE_XOR]       = { ALU_OP2_XOR_INT, tgsi_op2},
  7394.         [TGSI_OPCODE_SAD]       = { ALU_OP0_NOP, tgsi_unsupported},
  7395.         [TGSI_OPCODE_TXF]       = { FETCH_OP_LD, tgsi_tex},
  7396.         [TGSI_OPCODE_TXQ]       = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
  7397.         [TGSI_OPCODE_CONT]      = { CF_OP_LOOP_CONTINUE, tgsi_loop_brk_cont},
  7398.         [TGSI_OPCODE_EMIT]      = { CF_OP_EMIT_VERTEX, tgsi_gs_emit},
  7399.         [TGSI_OPCODE_ENDPRIM]   = { CF_OP_CUT_VERTEX, tgsi_gs_emit},
  7400.         [TGSI_OPCODE_BGNLOOP]   = { ALU_OP0_NOP, tgsi_bgnloop},
  7401.         [TGSI_OPCODE_BGNSUB]    = { ALU_OP0_NOP, tgsi_unsupported},
  7402.         [TGSI_OPCODE_ENDLOOP]   = { ALU_OP0_NOP, tgsi_endloop},
  7403.         [TGSI_OPCODE_ENDSUB]    = { ALU_OP0_NOP, tgsi_unsupported},
  7404.         [TGSI_OPCODE_TXQ_LZ]    = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
  7405.         [104]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7406.         [105]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7407.         [106]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7408.         [TGSI_OPCODE_NOP]       = { ALU_OP0_NOP, tgsi_unsupported},
  7409.         [TGSI_OPCODE_FSEQ]      = { ALU_OP2_SETE_DX10, tgsi_op2},
  7410.         [TGSI_OPCODE_FSGE]      = { ALU_OP2_SETGE_DX10, tgsi_op2},
  7411.         [TGSI_OPCODE_FSLT]      = { ALU_OP2_SETGT_DX10, tgsi_op2_swap},
  7412.         [TGSI_OPCODE_FSNE]      = { ALU_OP2_SETNE_DX10, tgsi_op2_swap},
  7413.         [112]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7414.         [TGSI_OPCODE_CALLNZ]    = { ALU_OP0_NOP, tgsi_unsupported},
  7415.         [114]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7416.         [TGSI_OPCODE_BREAKC]    = { ALU_OP0_NOP, tgsi_loop_breakc},
  7417.         [TGSI_OPCODE_KILL_IF]   = { ALU_OP2_KILLGT, tgsi_kill},  /* conditional kill */
  7418.         [TGSI_OPCODE_END]       = { ALU_OP0_NOP, tgsi_end},  /* aka HALT */
  7419.         [118]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7420.         [TGSI_OPCODE_F2I]       = { ALU_OP1_FLT_TO_INT, tgsi_op2_trans},
  7421.         [TGSI_OPCODE_IDIV]      = { ALU_OP0_NOP, tgsi_idiv},
  7422.         [TGSI_OPCODE_IMAX]      = { ALU_OP2_MAX_INT, tgsi_op2},
  7423.         [TGSI_OPCODE_IMIN]      = { ALU_OP2_MIN_INT, tgsi_op2},
  7424.         [TGSI_OPCODE_INEG]      = { ALU_OP2_SUB_INT, tgsi_ineg},
  7425.         [TGSI_OPCODE_ISGE]      = { ALU_OP2_SETGE_INT, tgsi_op2},
  7426.         [TGSI_OPCODE_ISHR]      = { ALU_OP2_ASHR_INT, tgsi_op2_trans},
  7427.         [TGSI_OPCODE_ISLT]      = { ALU_OP2_SETGT_INT, tgsi_op2_swap},
  7428.         [TGSI_OPCODE_F2U]       = { ALU_OP1_FLT_TO_UINT, tgsi_op2_trans},
  7429.         [TGSI_OPCODE_U2F]       = { ALU_OP1_UINT_TO_FLT, tgsi_op2_trans},
  7430.         [TGSI_OPCODE_UADD]      = { ALU_OP2_ADD_INT, tgsi_op2},
  7431.         [TGSI_OPCODE_UDIV]      = { ALU_OP0_NOP, tgsi_udiv},
  7432.         [TGSI_OPCODE_UMAD]      = { ALU_OP0_NOP, tgsi_umad},
  7433.         [TGSI_OPCODE_UMAX]      = { ALU_OP2_MAX_UINT, tgsi_op2},
  7434.         [TGSI_OPCODE_UMIN]      = { ALU_OP2_MIN_UINT, tgsi_op2},
  7435.         [TGSI_OPCODE_UMOD]      = { ALU_OP0_NOP, tgsi_umod},
  7436.         [TGSI_OPCODE_UMUL]      = { ALU_OP2_MULLO_UINT, tgsi_op2_trans},
  7437.         [TGSI_OPCODE_USEQ]      = { ALU_OP2_SETE_INT, tgsi_op2},
  7438.         [TGSI_OPCODE_USGE]      = { ALU_OP2_SETGE_UINT, tgsi_op2},
  7439.         [TGSI_OPCODE_USHR]      = { ALU_OP2_LSHR_INT, tgsi_op2_trans},
  7440.         [TGSI_OPCODE_USLT]      = { ALU_OP2_SETGT_UINT, tgsi_op2_swap},
  7441.         [TGSI_OPCODE_USNE]      = { ALU_OP2_SETNE_INT, tgsi_op2_swap},
  7442.         [TGSI_OPCODE_SWITCH]    = { ALU_OP0_NOP, tgsi_unsupported},
  7443.         [TGSI_OPCODE_CASE]      = { ALU_OP0_NOP, tgsi_unsupported},
  7444.         [TGSI_OPCODE_DEFAULT]   = { ALU_OP0_NOP, tgsi_unsupported},
  7445.         [TGSI_OPCODE_ENDSWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
  7446.         [TGSI_OPCODE_SAMPLE]    = { 0, tgsi_unsupported},
  7447.         [TGSI_OPCODE_SAMPLE_I]  = { 0, tgsi_unsupported},
  7448.         [TGSI_OPCODE_SAMPLE_I_MS]       = { 0, tgsi_unsupported},
  7449.         [TGSI_OPCODE_SAMPLE_B]  = { 0, tgsi_unsupported},
  7450.         [TGSI_OPCODE_SAMPLE_C]  = { 0, tgsi_unsupported},
  7451.         [TGSI_OPCODE_SAMPLE_C_LZ]       = { 0, tgsi_unsupported},
  7452.         [TGSI_OPCODE_SAMPLE_D]  = { 0, tgsi_unsupported},
  7453.         [TGSI_OPCODE_SAMPLE_L]  = { 0, tgsi_unsupported},
  7454.         [TGSI_OPCODE_GATHER4]   = { 0, tgsi_unsupported},
  7455.         [TGSI_OPCODE_SVIEWINFO] = { 0, tgsi_unsupported},
  7456.         [TGSI_OPCODE_SAMPLE_POS]        = { 0, tgsi_unsupported},
  7457.         [TGSI_OPCODE_SAMPLE_INFO]       = { 0, tgsi_unsupported},
  7458.         [TGSI_OPCODE_UARL]      = { ALU_OP1_MOVA_INT, tgsi_r600_arl},
  7459.         [TGSI_OPCODE_UCMP]      = { ALU_OP0_NOP, tgsi_ucmp},
  7460.         [TGSI_OPCODE_IABS]      = { 0, tgsi_iabs},
  7461.         [TGSI_OPCODE_ISSG]      = { 0, tgsi_issg},
  7462.         [TGSI_OPCODE_LOAD]      = { ALU_OP0_NOP, tgsi_unsupported},
  7463.         [TGSI_OPCODE_STORE]     = { ALU_OP0_NOP, tgsi_unsupported},
  7464.         [TGSI_OPCODE_MFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7465.         [TGSI_OPCODE_LFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7466.         [TGSI_OPCODE_SFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7467.         [TGSI_OPCODE_BARRIER]   = { ALU_OP0_NOP, tgsi_unsupported},
  7468.         [TGSI_OPCODE_ATOMUADD]  = { ALU_OP0_NOP, tgsi_unsupported},
  7469.         [TGSI_OPCODE_ATOMXCHG]  = { ALU_OP0_NOP, tgsi_unsupported},
  7470.         [TGSI_OPCODE_ATOMCAS]   = { ALU_OP0_NOP, tgsi_unsupported},
  7471.         [TGSI_OPCODE_ATOMAND]   = { ALU_OP0_NOP, tgsi_unsupported},
  7472.         [TGSI_OPCODE_ATOMOR]    = { ALU_OP0_NOP, tgsi_unsupported},
  7473.         [TGSI_OPCODE_ATOMXOR]   = { ALU_OP0_NOP, tgsi_unsupported},
  7474.         [TGSI_OPCODE_ATOMUMIN]  = { ALU_OP0_NOP, tgsi_unsupported},
  7475.         [TGSI_OPCODE_ATOMUMAX]  = { ALU_OP0_NOP, tgsi_unsupported},
  7476.         [TGSI_OPCODE_ATOMIMIN]  = { ALU_OP0_NOP, tgsi_unsupported},
  7477.         [TGSI_OPCODE_ATOMIMAX]  = { ALU_OP0_NOP, tgsi_unsupported},
  7478.         [TGSI_OPCODE_TEX2]      = { FETCH_OP_SAMPLE, tgsi_tex},
  7479.         [TGSI_OPCODE_TXB2]      = { FETCH_OP_SAMPLE_LB, tgsi_tex},
  7480.         [TGSI_OPCODE_TXL2]      = { FETCH_OP_SAMPLE_L, tgsi_tex},
  7481.         [TGSI_OPCODE_IMUL_HI]   = { ALU_OP2_MULHI_INT, tgsi_op2_trans},
  7482.         [TGSI_OPCODE_UMUL_HI]   = { ALU_OP2_MULHI_UINT, tgsi_op2_trans},
  7483.         [TGSI_OPCODE_TG4]       = { FETCH_OP_GATHER4, tgsi_unsupported},
  7484.         [TGSI_OPCODE_LODQ]      = { FETCH_OP_GET_LOD, tgsi_unsupported},
  7485.         [TGSI_OPCODE_IBFE]      = { ALU_OP3_BFE_INT, tgsi_unsupported},
  7486.         [TGSI_OPCODE_UBFE]      = { ALU_OP3_BFE_UINT, tgsi_unsupported},
  7487.         [TGSI_OPCODE_BFI]       = { ALU_OP0_NOP, tgsi_unsupported},
  7488.         [TGSI_OPCODE_BREV]      = { ALU_OP1_BFREV_INT, tgsi_unsupported},
  7489.         [TGSI_OPCODE_POPC]      = { ALU_OP1_BCNT_INT, tgsi_unsupported},
  7490.         [TGSI_OPCODE_LSB]       = { ALU_OP1_FFBL_INT, tgsi_unsupported},
  7491.         [TGSI_OPCODE_IMSB]      = { ALU_OP1_FFBH_INT, tgsi_unsupported},
  7492.         [TGSI_OPCODE_UMSB]      = { ALU_OP1_FFBH_UINT, tgsi_unsupported},
  7493.         [TGSI_OPCODE_INTERP_CENTROID]   = { ALU_OP0_NOP, tgsi_unsupported},
  7494.         [TGSI_OPCODE_INTERP_SAMPLE]     = { ALU_OP0_NOP, tgsi_unsupported},
  7495.         [TGSI_OPCODE_INTERP_OFFSET]     = { ALU_OP0_NOP, tgsi_unsupported},
  7496.         [TGSI_OPCODE_LAST]      = { ALU_OP0_NOP, tgsi_unsupported},
  7497. };
  7498.  
  7499. static const struct r600_shader_tgsi_instruction eg_shader_tgsi_instruction[] = {
  7500.         [TGSI_OPCODE_ARL]       = { ALU_OP0_NOP, tgsi_eg_arl},
  7501.         [TGSI_OPCODE_MOV]       = { ALU_OP1_MOV, tgsi_op2},
  7502.         [TGSI_OPCODE_LIT]       = { ALU_OP0_NOP, tgsi_lit},
  7503.         [TGSI_OPCODE_RCP]       = { ALU_OP1_RECIP_IEEE, tgsi_trans_srcx_replicate},
  7504.         [TGSI_OPCODE_RSQ]       = { ALU_OP1_RECIPSQRT_IEEE, tgsi_rsq},
  7505.         [TGSI_OPCODE_EXP]       = { ALU_OP0_NOP, tgsi_exp},
  7506.         [TGSI_OPCODE_LOG]       = { ALU_OP0_NOP, tgsi_log},
  7507.         [TGSI_OPCODE_MUL]       = { ALU_OP2_MUL, tgsi_op2},
  7508.         [TGSI_OPCODE_ADD]       = { ALU_OP2_ADD, tgsi_op2},
  7509.         [TGSI_OPCODE_DP3]       = { ALU_OP2_DOT4, tgsi_dp},
  7510.         [TGSI_OPCODE_DP4]       = { ALU_OP2_DOT4, tgsi_dp},
  7511.         [TGSI_OPCODE_DST]       = { ALU_OP0_NOP, tgsi_opdst},
  7512.         [TGSI_OPCODE_MIN]       = { ALU_OP2_MIN, tgsi_op2},
  7513.         [TGSI_OPCODE_MAX]       = { ALU_OP2_MAX, tgsi_op2},
  7514.         [TGSI_OPCODE_SLT]       = { ALU_OP2_SETGT, tgsi_op2_swap},
  7515.         [TGSI_OPCODE_SGE]       = { ALU_OP2_SETGE, tgsi_op2},
  7516.         [TGSI_OPCODE_MAD]       = { ALU_OP3_MULADD, tgsi_op3},
  7517.         [TGSI_OPCODE_SUB]       = { ALU_OP2_ADD, tgsi_op2},
  7518.         [TGSI_OPCODE_LRP]       = { ALU_OP0_NOP, tgsi_lrp},
  7519.         [TGSI_OPCODE_FMA]       = { ALU_OP0_NOP, tgsi_unsupported},
  7520.         [TGSI_OPCODE_SQRT]      = { ALU_OP1_SQRT_IEEE, tgsi_trans_srcx_replicate},
  7521.         [TGSI_OPCODE_DP2A]      = { ALU_OP0_NOP, tgsi_unsupported},
  7522.         [22]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7523.         [23]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7524.         [TGSI_OPCODE_FRC]       = { ALU_OP1_FRACT, tgsi_op2},
  7525.         [TGSI_OPCODE_CLAMP]     = { ALU_OP0_NOP, tgsi_unsupported},
  7526.         [TGSI_OPCODE_FLR]       = { ALU_OP1_FLOOR, tgsi_op2},
  7527.         [TGSI_OPCODE_ROUND]     = { ALU_OP1_RNDNE, tgsi_op2},
  7528.         [TGSI_OPCODE_EX2]       = { ALU_OP1_EXP_IEEE, tgsi_trans_srcx_replicate},
  7529.         [TGSI_OPCODE_LG2]       = { ALU_OP1_LOG_IEEE, tgsi_trans_srcx_replicate},
  7530.         [TGSI_OPCODE_POW]       = { ALU_OP0_NOP, tgsi_pow},
  7531.         [TGSI_OPCODE_XPD]       = { ALU_OP0_NOP, tgsi_xpd},
  7532.         [32]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7533.         [TGSI_OPCODE_ABS]       = { ALU_OP1_MOV, tgsi_op2},
  7534.         [34]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7535.         [TGSI_OPCODE_DPH]       = { ALU_OP2_DOT4, tgsi_dp},
  7536.         [TGSI_OPCODE_COS]       = { ALU_OP1_COS, tgsi_trig},
  7537.         [TGSI_OPCODE_DDX]       = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
  7538.         [TGSI_OPCODE_DDY]       = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
  7539.         [TGSI_OPCODE_KILL]      = { ALU_OP2_KILLGT, tgsi_kill},  /* unconditional kill */
  7540.         [TGSI_OPCODE_PK2H]      = { ALU_OP0_NOP, tgsi_unsupported},
  7541.         [TGSI_OPCODE_PK2US]     = { ALU_OP0_NOP, tgsi_unsupported},
  7542.         [TGSI_OPCODE_PK4B]      = { ALU_OP0_NOP, tgsi_unsupported},
  7543.         [TGSI_OPCODE_PK4UB]     = { ALU_OP0_NOP, tgsi_unsupported},
  7544.         [44]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7545.         [TGSI_OPCODE_SEQ]       = { ALU_OP2_SETE, tgsi_op2},
  7546.         [46]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7547.         [TGSI_OPCODE_SGT]       = { ALU_OP2_SETGT, tgsi_op2},
  7548.         [TGSI_OPCODE_SIN]       = { ALU_OP1_SIN, tgsi_trig},
  7549.         [TGSI_OPCODE_SLE]       = { ALU_OP2_SETGE, tgsi_op2_swap},
  7550.         [TGSI_OPCODE_SNE]       = { ALU_OP2_SETNE, tgsi_op2},
  7551.         [51]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7552.         [TGSI_OPCODE_TEX]       = { FETCH_OP_SAMPLE, tgsi_tex},
  7553.         [TGSI_OPCODE_TXD]       = { FETCH_OP_SAMPLE_G, tgsi_tex},
  7554.         [TGSI_OPCODE_TXP]       = { FETCH_OP_SAMPLE, tgsi_tex},
  7555.         [TGSI_OPCODE_UP2H]      = { ALU_OP0_NOP, tgsi_unsupported},
  7556.         [TGSI_OPCODE_UP2US]     = { ALU_OP0_NOP, tgsi_unsupported},
  7557.         [TGSI_OPCODE_UP4B]      = { ALU_OP0_NOP, tgsi_unsupported},
  7558.         [TGSI_OPCODE_UP4UB]     = { ALU_OP0_NOP, tgsi_unsupported},
  7559.         [59]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7560.         [60]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7561.         [TGSI_OPCODE_ARR]       = { ALU_OP0_NOP, tgsi_eg_arl},
  7562.         [62]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7563.         [TGSI_OPCODE_CAL]       = { ALU_OP0_NOP, tgsi_unsupported},
  7564.         [TGSI_OPCODE_RET]       = { ALU_OP0_NOP, tgsi_unsupported},
  7565.         [TGSI_OPCODE_SSG]       = { ALU_OP0_NOP, tgsi_ssg},
  7566.         [TGSI_OPCODE_CMP]       = { ALU_OP0_NOP, tgsi_cmp},
  7567.         [TGSI_OPCODE_SCS]       = { ALU_OP0_NOP, tgsi_scs},
  7568.         [TGSI_OPCODE_TXB]       = { FETCH_OP_SAMPLE_LB, tgsi_tex},
  7569.         [69]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7570.         [TGSI_OPCODE_DIV]       = { ALU_OP0_NOP, tgsi_unsupported},
  7571.         [TGSI_OPCODE_DP2]       = { ALU_OP2_DOT4, tgsi_dp},
  7572.         [TGSI_OPCODE_TXL]       = { FETCH_OP_SAMPLE_L, tgsi_tex},
  7573.         [TGSI_OPCODE_BRK]       = { CF_OP_LOOP_BREAK, tgsi_loop_brk_cont},
  7574.         [TGSI_OPCODE_IF]        = { ALU_OP0_NOP, tgsi_if},
  7575.         [TGSI_OPCODE_UIF]       = { ALU_OP0_NOP, tgsi_uif},
  7576.         [76]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7577.         [TGSI_OPCODE_ELSE]      = { ALU_OP0_NOP, tgsi_else},
  7578.         [TGSI_OPCODE_ENDIF]     = { ALU_OP0_NOP, tgsi_endif},
  7579.         [TGSI_OPCODE_DDX_FINE]  = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
  7580.         [TGSI_OPCODE_DDY_FINE]  = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
  7581.         [TGSI_OPCODE_PUSHA]     = { ALU_OP0_NOP, tgsi_unsupported},
  7582.         [TGSI_OPCODE_POPA]      = { ALU_OP0_NOP, tgsi_unsupported},
  7583.         [TGSI_OPCODE_CEIL]      = { ALU_OP1_CEIL, tgsi_op2},
  7584.         [TGSI_OPCODE_I2F]       = { ALU_OP1_INT_TO_FLT, tgsi_op2_trans},
  7585.         [TGSI_OPCODE_NOT]       = { ALU_OP1_NOT_INT, tgsi_op2},
  7586.         [TGSI_OPCODE_TRUNC]     = { ALU_OP1_TRUNC, tgsi_op2},
  7587.         [TGSI_OPCODE_SHL]       = { ALU_OP2_LSHL_INT, tgsi_op2},
  7588.         [88]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7589.         [TGSI_OPCODE_AND]       = { ALU_OP2_AND_INT, tgsi_op2},
  7590.         [TGSI_OPCODE_OR]        = { ALU_OP2_OR_INT, tgsi_op2},
  7591.         [TGSI_OPCODE_MOD]       = { ALU_OP0_NOP, tgsi_imod},
  7592.         [TGSI_OPCODE_XOR]       = { ALU_OP2_XOR_INT, tgsi_op2},
  7593.         [TGSI_OPCODE_SAD]       = { ALU_OP0_NOP, tgsi_unsupported},
  7594.         [TGSI_OPCODE_TXF]       = { FETCH_OP_LD, tgsi_tex},
  7595.         [TGSI_OPCODE_TXQ]       = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
  7596.         [TGSI_OPCODE_CONT]      = { CF_OP_LOOP_CONTINUE, tgsi_loop_brk_cont},
  7597.         [TGSI_OPCODE_EMIT]      = { CF_OP_EMIT_VERTEX, tgsi_gs_emit},
  7598.         [TGSI_OPCODE_ENDPRIM]   = { CF_OP_CUT_VERTEX, tgsi_gs_emit},
  7599.         [TGSI_OPCODE_BGNLOOP]   = { ALU_OP0_NOP, tgsi_bgnloop},
  7600.         [TGSI_OPCODE_BGNSUB]    = { ALU_OP0_NOP, tgsi_unsupported},
  7601.         [TGSI_OPCODE_ENDLOOP]   = { ALU_OP0_NOP, tgsi_endloop},
  7602.         [TGSI_OPCODE_ENDSUB]    = { ALU_OP0_NOP, tgsi_unsupported},
  7603.         [TGSI_OPCODE_TXQ_LZ]    = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
  7604.         [104]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7605.         [105]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7606.         [106]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7607.         [TGSI_OPCODE_NOP]       = { ALU_OP0_NOP, tgsi_unsupported},
  7608.         [TGSI_OPCODE_FSEQ]      = { ALU_OP2_SETE_DX10, tgsi_op2},
  7609.         [TGSI_OPCODE_FSGE]      = { ALU_OP2_SETGE_DX10, tgsi_op2},
  7610.         [TGSI_OPCODE_FSLT]      = { ALU_OP2_SETGT_DX10, tgsi_op2_swap},
  7611.         [TGSI_OPCODE_FSNE]      = { ALU_OP2_SETNE_DX10, tgsi_op2_swap},
  7612.         [112]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7613.         [TGSI_OPCODE_CALLNZ]    = { ALU_OP0_NOP, tgsi_unsupported},
  7614.         [114]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7615.         [TGSI_OPCODE_BREAKC]    = { ALU_OP0_NOP, tgsi_unsupported},
  7616.         [TGSI_OPCODE_KILL_IF]   = { ALU_OP2_KILLGT, tgsi_kill},  /* conditional kill */
  7617.         [TGSI_OPCODE_END]       = { ALU_OP0_NOP, tgsi_end},  /* aka HALT */
  7618.         [118]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7619.         [TGSI_OPCODE_F2I]       = { ALU_OP1_FLT_TO_INT, tgsi_f2i},
  7620.         [TGSI_OPCODE_IDIV]      = { ALU_OP0_NOP, tgsi_idiv},
  7621.         [TGSI_OPCODE_IMAX]      = { ALU_OP2_MAX_INT, tgsi_op2},
  7622.         [TGSI_OPCODE_IMIN]      = { ALU_OP2_MIN_INT, tgsi_op2},
  7623.         [TGSI_OPCODE_INEG]      = { ALU_OP2_SUB_INT, tgsi_ineg},
  7624.         [TGSI_OPCODE_ISGE]      = { ALU_OP2_SETGE_INT, tgsi_op2},
  7625.         [TGSI_OPCODE_ISHR]      = { ALU_OP2_ASHR_INT, tgsi_op2},
  7626.         [TGSI_OPCODE_ISLT]      = { ALU_OP2_SETGT_INT, tgsi_op2_swap},
  7627.         [TGSI_OPCODE_F2U]       = { ALU_OP1_FLT_TO_UINT, tgsi_f2i},
  7628.         [TGSI_OPCODE_U2F]       = { ALU_OP1_UINT_TO_FLT, tgsi_op2_trans},
  7629.         [TGSI_OPCODE_UADD]      = { ALU_OP2_ADD_INT, tgsi_op2},
  7630.         [TGSI_OPCODE_UDIV]      = { ALU_OP0_NOP, tgsi_udiv},
  7631.         [TGSI_OPCODE_UMAD]      = { ALU_OP0_NOP, tgsi_umad},
  7632.         [TGSI_OPCODE_UMAX]      = { ALU_OP2_MAX_UINT, tgsi_op2},
  7633.         [TGSI_OPCODE_UMIN]      = { ALU_OP2_MIN_UINT, tgsi_op2},
  7634.         [TGSI_OPCODE_UMOD]      = { ALU_OP0_NOP, tgsi_umod},
  7635.         [TGSI_OPCODE_UMUL]      = { ALU_OP2_MULLO_UINT, tgsi_op2_trans},
  7636.         [TGSI_OPCODE_USEQ]      = { ALU_OP2_SETE_INT, tgsi_op2},
  7637.         [TGSI_OPCODE_USGE]      = { ALU_OP2_SETGE_UINT, tgsi_op2},
  7638.         [TGSI_OPCODE_USHR]      = { ALU_OP2_LSHR_INT, tgsi_op2},
  7639.         [TGSI_OPCODE_USLT]      = { ALU_OP2_SETGT_UINT, tgsi_op2_swap},
  7640.         [TGSI_OPCODE_USNE]      = { ALU_OP2_SETNE_INT, tgsi_op2},
  7641.         [TGSI_OPCODE_SWITCH]    = { ALU_OP0_NOP, tgsi_unsupported},
  7642.         [TGSI_OPCODE_CASE]      = { ALU_OP0_NOP, tgsi_unsupported},
  7643.         [TGSI_OPCODE_DEFAULT]   = { ALU_OP0_NOP, tgsi_unsupported},
  7644.         [TGSI_OPCODE_ENDSWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
  7645.         [TGSI_OPCODE_SAMPLE]    = { 0, tgsi_unsupported},
  7646.         [TGSI_OPCODE_SAMPLE_I]  = { 0, tgsi_unsupported},
  7647.         [TGSI_OPCODE_SAMPLE_I_MS]       = { 0, tgsi_unsupported},
  7648.         [TGSI_OPCODE_SAMPLE_B]  = { 0, tgsi_unsupported},
  7649.         [TGSI_OPCODE_SAMPLE_C]  = { 0, tgsi_unsupported},
  7650.         [TGSI_OPCODE_SAMPLE_C_LZ]       = { 0, tgsi_unsupported},
  7651.         [TGSI_OPCODE_SAMPLE_D]  = { 0, tgsi_unsupported},
  7652.         [TGSI_OPCODE_SAMPLE_L]  = { 0, tgsi_unsupported},
  7653.         [TGSI_OPCODE_GATHER4]   = { 0, tgsi_unsupported},
  7654.         [TGSI_OPCODE_SVIEWINFO] = { 0, tgsi_unsupported},
  7655.         [TGSI_OPCODE_SAMPLE_POS]        = { 0, tgsi_unsupported},
  7656.         [TGSI_OPCODE_SAMPLE_INFO]       = { 0, tgsi_unsupported},
  7657.         [TGSI_OPCODE_UARL]      = { ALU_OP1_MOVA_INT, tgsi_eg_arl},
  7658.         [TGSI_OPCODE_UCMP]      = { ALU_OP0_NOP, tgsi_ucmp},
  7659.         [TGSI_OPCODE_IABS]      = { 0, tgsi_iabs},
  7660.         [TGSI_OPCODE_ISSG]      = { 0, tgsi_issg},
  7661.         [TGSI_OPCODE_LOAD]      = { ALU_OP0_NOP, tgsi_unsupported},
  7662.         [TGSI_OPCODE_STORE]     = { ALU_OP0_NOP, tgsi_unsupported},
  7663.         [TGSI_OPCODE_MFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7664.         [TGSI_OPCODE_LFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7665.         [TGSI_OPCODE_SFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7666.         [TGSI_OPCODE_BARRIER]   = { ALU_OP0_NOP, tgsi_unsupported},
  7667.         [TGSI_OPCODE_ATOMUADD]  = { ALU_OP0_NOP, tgsi_unsupported},
  7668.         [TGSI_OPCODE_ATOMXCHG]  = { ALU_OP0_NOP, tgsi_unsupported},
  7669.         [TGSI_OPCODE_ATOMCAS]   = { ALU_OP0_NOP, tgsi_unsupported},
  7670.         [TGSI_OPCODE_ATOMAND]   = { ALU_OP0_NOP, tgsi_unsupported},
  7671.         [TGSI_OPCODE_ATOMOR]    = { ALU_OP0_NOP, tgsi_unsupported},
  7672.         [TGSI_OPCODE_ATOMXOR]   = { ALU_OP0_NOP, tgsi_unsupported},
  7673.         [TGSI_OPCODE_ATOMUMIN]  = { ALU_OP0_NOP, tgsi_unsupported},
  7674.         [TGSI_OPCODE_ATOMUMAX]  = { ALU_OP0_NOP, tgsi_unsupported},
  7675.         [TGSI_OPCODE_ATOMIMIN]  = { ALU_OP0_NOP, tgsi_unsupported},
  7676.         [TGSI_OPCODE_ATOMIMAX]  = { ALU_OP0_NOP, tgsi_unsupported},
  7677.         [TGSI_OPCODE_TEX2]      = { FETCH_OP_SAMPLE, tgsi_tex},
  7678.         [TGSI_OPCODE_TXB2]      = { FETCH_OP_SAMPLE_LB, tgsi_tex},
  7679.         [TGSI_OPCODE_TXL2]      = { FETCH_OP_SAMPLE_L, tgsi_tex},
  7680.         [TGSI_OPCODE_IMUL_HI]   = { ALU_OP2_MULHI_INT, tgsi_op2_trans},
  7681.         [TGSI_OPCODE_UMUL_HI]   = { ALU_OP2_MULHI_UINT, tgsi_op2_trans},
  7682.         [TGSI_OPCODE_TG4]       = { FETCH_OP_GATHER4, tgsi_tex},
  7683.         [TGSI_OPCODE_LODQ]      = { FETCH_OP_GET_LOD, tgsi_tex},
  7684.         [TGSI_OPCODE_IBFE]      = { ALU_OP3_BFE_INT, tgsi_op3},
  7685.         [TGSI_OPCODE_UBFE]      = { ALU_OP3_BFE_UINT, tgsi_op3},
  7686.         [TGSI_OPCODE_BFI]       = { ALU_OP0_NOP, tgsi_bfi},
  7687.         [TGSI_OPCODE_BREV]      = { ALU_OP1_BFREV_INT, tgsi_op2},
  7688.         [TGSI_OPCODE_POPC]      = { ALU_OP1_BCNT_INT, tgsi_op2},
  7689.         [TGSI_OPCODE_LSB]       = { ALU_OP1_FFBL_INT, tgsi_op2},
  7690.         [TGSI_OPCODE_IMSB]      = { ALU_OP1_FFBH_INT, tgsi_msb},
  7691.         [TGSI_OPCODE_UMSB]      = { ALU_OP1_FFBH_UINT, tgsi_msb},
  7692.         [TGSI_OPCODE_INTERP_CENTROID]   = { ALU_OP0_NOP, tgsi_interp_egcm},
  7693.         [TGSI_OPCODE_INTERP_SAMPLE]     = { ALU_OP0_NOP, tgsi_interp_egcm},
  7694.         [TGSI_OPCODE_INTERP_OFFSET]     = { ALU_OP0_NOP, tgsi_interp_egcm},
  7695.         [TGSI_OPCODE_LAST]      = { ALU_OP0_NOP, tgsi_unsupported},
  7696. };
  7697.  
  7698. static const struct r600_shader_tgsi_instruction cm_shader_tgsi_instruction[] = {
  7699.         [TGSI_OPCODE_ARL]       = { ALU_OP0_NOP, tgsi_eg_arl},
  7700.         [TGSI_OPCODE_MOV]       = { ALU_OP1_MOV, tgsi_op2},
  7701.         [TGSI_OPCODE_LIT]       = { ALU_OP0_NOP, tgsi_lit},
  7702.         [TGSI_OPCODE_RCP]       = { ALU_OP1_RECIP_IEEE, cayman_emit_float_instr},
  7703.         [TGSI_OPCODE_RSQ]       = { ALU_OP1_RECIPSQRT_IEEE, cayman_emit_float_instr},
  7704.         [TGSI_OPCODE_EXP]       = { ALU_OP0_NOP, tgsi_exp},
  7705.         [TGSI_OPCODE_LOG]       = { ALU_OP0_NOP, tgsi_log},
  7706.         [TGSI_OPCODE_MUL]       = { ALU_OP2_MUL, tgsi_op2},
  7707.         [TGSI_OPCODE_ADD]       = { ALU_OP2_ADD, tgsi_op2},
  7708.         [TGSI_OPCODE_DP3]       = { ALU_OP2_DOT4, tgsi_dp},
  7709.         [TGSI_OPCODE_DP4]       = { ALU_OP2_DOT4, tgsi_dp},
  7710.         [TGSI_OPCODE_DST]       = { ALU_OP0_NOP, tgsi_opdst},
  7711.         [TGSI_OPCODE_MIN]       = { ALU_OP2_MIN, tgsi_op2},
  7712.         [TGSI_OPCODE_MAX]       = { ALU_OP2_MAX, tgsi_op2},
  7713.         [TGSI_OPCODE_SLT]       = { ALU_OP2_SETGT, tgsi_op2_swap},
  7714.         [TGSI_OPCODE_SGE]       = { ALU_OP2_SETGE, tgsi_op2},
  7715.         [TGSI_OPCODE_MAD]       = { ALU_OP3_MULADD, tgsi_op3},
  7716.         [TGSI_OPCODE_SUB]       = { ALU_OP2_ADD, tgsi_op2},
  7717.         [TGSI_OPCODE_LRP]       = { ALU_OP0_NOP, tgsi_lrp},
  7718.         [TGSI_OPCODE_FMA]       = { ALU_OP0_NOP, tgsi_unsupported},
  7719.         [TGSI_OPCODE_SQRT]      = { ALU_OP1_SQRT_IEEE, cayman_emit_float_instr},
  7720.         [TGSI_OPCODE_DP2A]      = { ALU_OP0_NOP, tgsi_unsupported},
  7721.         [22]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7722.         [23]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7723.         [TGSI_OPCODE_FRC]       = { ALU_OP1_FRACT, tgsi_op2},
  7724.         [TGSI_OPCODE_CLAMP]     = { ALU_OP0_NOP, tgsi_unsupported},
  7725.         [TGSI_OPCODE_FLR]       = { ALU_OP1_FLOOR, tgsi_op2},
  7726.         [TGSI_OPCODE_ROUND]     = { ALU_OP1_RNDNE, tgsi_op2},
  7727.         [TGSI_OPCODE_EX2]       = { ALU_OP1_EXP_IEEE, cayman_emit_float_instr},
  7728.         [TGSI_OPCODE_LG2]       = { ALU_OP1_LOG_IEEE, cayman_emit_float_instr},
  7729.         [TGSI_OPCODE_POW]       = { ALU_OP0_NOP, cayman_pow},
  7730.         [TGSI_OPCODE_XPD]       = { ALU_OP0_NOP, tgsi_xpd},
  7731.         [32]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7732.         [TGSI_OPCODE_ABS]       = { ALU_OP1_MOV, tgsi_op2},
  7733.         [34]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7734.         [TGSI_OPCODE_DPH]       = { ALU_OP2_DOT4, tgsi_dp},
  7735.         [TGSI_OPCODE_COS]       = { ALU_OP1_COS, cayman_trig},
  7736.         [TGSI_OPCODE_DDX]       = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
  7737.         [TGSI_OPCODE_DDY]       = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
  7738.         [TGSI_OPCODE_KILL]      = { ALU_OP2_KILLGT, tgsi_kill},  /* unconditional kill */
  7739.         [TGSI_OPCODE_PK2H]      = { ALU_OP0_NOP, tgsi_unsupported},
  7740.         [TGSI_OPCODE_PK2US]     = { ALU_OP0_NOP, tgsi_unsupported},
  7741.         [TGSI_OPCODE_PK4B]      = { ALU_OP0_NOP, tgsi_unsupported},
  7742.         [TGSI_OPCODE_PK4UB]     = { ALU_OP0_NOP, tgsi_unsupported},
  7743.         [44]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7744.         [TGSI_OPCODE_SEQ]       = { ALU_OP2_SETE, tgsi_op2},
  7745.         [46]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7746.         [TGSI_OPCODE_SGT]       = { ALU_OP2_SETGT, tgsi_op2},
  7747.         [TGSI_OPCODE_SIN]       = { ALU_OP1_SIN, cayman_trig},
  7748.         [TGSI_OPCODE_SLE]       = { ALU_OP2_SETGE, tgsi_op2_swap},
  7749.         [TGSI_OPCODE_SNE]       = { ALU_OP2_SETNE, tgsi_op2},
  7750.         [51]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7751.         [TGSI_OPCODE_TEX]       = { FETCH_OP_SAMPLE, tgsi_tex},
  7752.         [TGSI_OPCODE_TXD]       = { FETCH_OP_SAMPLE_G, tgsi_tex},
  7753.         [TGSI_OPCODE_TXP]       = { FETCH_OP_SAMPLE, tgsi_tex},
  7754.         [TGSI_OPCODE_UP2H]      = { ALU_OP0_NOP, tgsi_unsupported},
  7755.         [TGSI_OPCODE_UP2US]     = { ALU_OP0_NOP, tgsi_unsupported},
  7756.         [TGSI_OPCODE_UP4B]      = { ALU_OP0_NOP, tgsi_unsupported},
  7757.         [TGSI_OPCODE_UP4UB]     = { ALU_OP0_NOP, tgsi_unsupported},
  7758.         [59]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7759.         [60]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7760.         [TGSI_OPCODE_ARR]       = { ALU_OP0_NOP, tgsi_eg_arl},
  7761.         [62]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7762.         [TGSI_OPCODE_CAL]       = { ALU_OP0_NOP, tgsi_unsupported},
  7763.         [TGSI_OPCODE_RET]       = { ALU_OP0_NOP, tgsi_unsupported},
  7764.         [TGSI_OPCODE_SSG]       = { ALU_OP0_NOP, tgsi_ssg},
  7765.         [TGSI_OPCODE_CMP]       = { ALU_OP0_NOP, tgsi_cmp},
  7766.         [TGSI_OPCODE_SCS]       = { ALU_OP0_NOP, tgsi_scs},
  7767.         [TGSI_OPCODE_TXB]       = { FETCH_OP_SAMPLE_LB, tgsi_tex},
  7768.         [69]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7769.         [TGSI_OPCODE_DIV]       = { ALU_OP0_NOP, tgsi_unsupported},
  7770.         [TGSI_OPCODE_DP2]       = { ALU_OP2_DOT4, tgsi_dp},
  7771.         [TGSI_OPCODE_TXL]       = { FETCH_OP_SAMPLE_L, tgsi_tex},
  7772.         [TGSI_OPCODE_BRK]       = { CF_OP_LOOP_BREAK, tgsi_loop_brk_cont},
  7773.         [TGSI_OPCODE_IF]        = { ALU_OP0_NOP, tgsi_if},
  7774.         [TGSI_OPCODE_UIF]       = { ALU_OP0_NOP, tgsi_uif},
  7775.         [76]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7776.         [TGSI_OPCODE_ELSE]      = { ALU_OP0_NOP, tgsi_else},
  7777.         [TGSI_OPCODE_ENDIF]     = { ALU_OP0_NOP, tgsi_endif},
  7778.         [TGSI_OPCODE_DDX_FINE]  = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
  7779.         [TGSI_OPCODE_DDY_FINE]  = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
  7780.         [TGSI_OPCODE_PUSHA]     = { ALU_OP0_NOP, tgsi_unsupported},
  7781.         [TGSI_OPCODE_POPA]      = { ALU_OP0_NOP, tgsi_unsupported},
  7782.         [TGSI_OPCODE_CEIL]      = { ALU_OP1_CEIL, tgsi_op2},
  7783.         [TGSI_OPCODE_I2F]       = { ALU_OP1_INT_TO_FLT, tgsi_op2},
  7784.         [TGSI_OPCODE_NOT]       = { ALU_OP1_NOT_INT, tgsi_op2},
  7785.         [TGSI_OPCODE_TRUNC]     = { ALU_OP1_TRUNC, tgsi_op2},
  7786.         [TGSI_OPCODE_SHL]       = { ALU_OP2_LSHL_INT, tgsi_op2},
  7787.         [88]                    = { ALU_OP0_NOP, tgsi_unsupported},
  7788.         [TGSI_OPCODE_AND]       = { ALU_OP2_AND_INT, tgsi_op2},
  7789.         [TGSI_OPCODE_OR]        = { ALU_OP2_OR_INT, tgsi_op2},
  7790.         [TGSI_OPCODE_MOD]       = { ALU_OP0_NOP, tgsi_imod},
  7791.         [TGSI_OPCODE_XOR]       = { ALU_OP2_XOR_INT, tgsi_op2},
  7792.         [TGSI_OPCODE_SAD]       = { ALU_OP0_NOP, tgsi_unsupported},
  7793.         [TGSI_OPCODE_TXF]       = { FETCH_OP_LD, tgsi_tex},
  7794.         [TGSI_OPCODE_TXQ]       = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
  7795.         [TGSI_OPCODE_CONT]      = { CF_OP_LOOP_CONTINUE, tgsi_loop_brk_cont},
  7796.         [TGSI_OPCODE_EMIT]      = { CF_OP_EMIT_VERTEX, tgsi_gs_emit},
  7797.         [TGSI_OPCODE_ENDPRIM]   = { CF_OP_CUT_VERTEX, tgsi_gs_emit},
  7798.         [TGSI_OPCODE_BGNLOOP]   = { ALU_OP0_NOP, tgsi_bgnloop},
  7799.         [TGSI_OPCODE_BGNSUB]    = { ALU_OP0_NOP, tgsi_unsupported},
  7800.         [TGSI_OPCODE_ENDLOOP]   = { ALU_OP0_NOP, tgsi_endloop},
  7801.         [TGSI_OPCODE_ENDSUB]    = { ALU_OP0_NOP, tgsi_unsupported},
  7802.         [TGSI_OPCODE_TXQ_LZ]    = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
  7803.         [104]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7804.         [105]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7805.         [106]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7806.         [TGSI_OPCODE_NOP]       = { ALU_OP0_NOP, tgsi_unsupported},
  7807.         [TGSI_OPCODE_FSEQ]      = { ALU_OP2_SETE_DX10, tgsi_op2},
  7808.         [TGSI_OPCODE_FSGE]      = { ALU_OP2_SETGE_DX10, tgsi_op2},
  7809.         [TGSI_OPCODE_FSLT]      = { ALU_OP2_SETGT_DX10, tgsi_op2_swap},
  7810.         [TGSI_OPCODE_FSNE]      = { ALU_OP2_SETNE_DX10, tgsi_op2_swap},
  7811.         [112]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7812.         [TGSI_OPCODE_CALLNZ]    = { ALU_OP0_NOP, tgsi_unsupported},
  7813.         [114]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7814.         [TGSI_OPCODE_BREAKC]    = { ALU_OP0_NOP, tgsi_unsupported},
  7815.         [TGSI_OPCODE_KILL_IF]   = { ALU_OP2_KILLGT, tgsi_kill},  /* conditional kill */
  7816.         [TGSI_OPCODE_END]       = { ALU_OP0_NOP, tgsi_end},  /* aka HALT */
  7817.         [118]                   = { ALU_OP0_NOP, tgsi_unsupported},
  7818.         [TGSI_OPCODE_F2I]       = { ALU_OP1_FLT_TO_INT, tgsi_op2},
  7819.         [TGSI_OPCODE_IDIV]      = { ALU_OP0_NOP, tgsi_idiv},
  7820.         [TGSI_OPCODE_IMAX]      = { ALU_OP2_MAX_INT, tgsi_op2},
  7821.         [TGSI_OPCODE_IMIN]      = { ALU_OP2_MIN_INT, tgsi_op2},
  7822.         [TGSI_OPCODE_INEG]      = { ALU_OP2_SUB_INT, tgsi_ineg},
  7823.         [TGSI_OPCODE_ISGE]      = { ALU_OP2_SETGE_INT, tgsi_op2},
  7824.         [TGSI_OPCODE_ISHR]      = { ALU_OP2_ASHR_INT, tgsi_op2},
  7825.         [TGSI_OPCODE_ISLT]      = { ALU_OP2_SETGT_INT, tgsi_op2_swap},
  7826.         [TGSI_OPCODE_F2U]       = { ALU_OP1_FLT_TO_UINT, tgsi_op2},
  7827.         [TGSI_OPCODE_U2F]       = { ALU_OP1_UINT_TO_FLT, tgsi_op2},
  7828.         [TGSI_OPCODE_UADD]      = { ALU_OP2_ADD_INT, tgsi_op2},
  7829.         [TGSI_OPCODE_UDIV]      = { ALU_OP0_NOP, tgsi_udiv},
  7830.         [TGSI_OPCODE_UMAD]      = { ALU_OP0_NOP, tgsi_umad},
  7831.         [TGSI_OPCODE_UMAX]      = { ALU_OP2_MAX_UINT, tgsi_op2},
  7832.         [TGSI_OPCODE_UMIN]      = { ALU_OP2_MIN_UINT, tgsi_op2},
  7833.         [TGSI_OPCODE_UMOD]      = { ALU_OP0_NOP, tgsi_umod},
  7834.         [TGSI_OPCODE_UMUL]      = { ALU_OP2_MULLO_INT, cayman_mul_int_instr},
  7835.         [TGSI_OPCODE_USEQ]      = { ALU_OP2_SETE_INT, tgsi_op2},
  7836.         [TGSI_OPCODE_USGE]      = { ALU_OP2_SETGE_UINT, tgsi_op2},
  7837.         [TGSI_OPCODE_USHR]      = { ALU_OP2_LSHR_INT, tgsi_op2},
  7838.         [TGSI_OPCODE_USLT]      = { ALU_OP2_SETGT_UINT, tgsi_op2_swap},
  7839.         [TGSI_OPCODE_USNE]      = { ALU_OP2_SETNE_INT, tgsi_op2},
  7840.         [TGSI_OPCODE_SWITCH]    = { ALU_OP0_NOP, tgsi_unsupported},
  7841.         [TGSI_OPCODE_CASE]      = { ALU_OP0_NOP, tgsi_unsupported},
  7842.         [TGSI_OPCODE_DEFAULT]   = { ALU_OP0_NOP, tgsi_unsupported},
  7843.         [TGSI_OPCODE_ENDSWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
  7844.         [TGSI_OPCODE_SAMPLE]    = { 0, tgsi_unsupported},
  7845.         [TGSI_OPCODE_SAMPLE_I]  = { 0, tgsi_unsupported},
  7846.         [TGSI_OPCODE_SAMPLE_I_MS]       = { 0, tgsi_unsupported},
  7847.         [TGSI_OPCODE_SAMPLE_B]  = { 0, tgsi_unsupported},
  7848.         [TGSI_OPCODE_SAMPLE_C]  = { 0, tgsi_unsupported},
  7849.         [TGSI_OPCODE_SAMPLE_C_LZ]       = { 0, tgsi_unsupported},
  7850.         [TGSI_OPCODE_SAMPLE_D]  = { 0, tgsi_unsupported},
  7851.         [TGSI_OPCODE_SAMPLE_L]  = { 0, tgsi_unsupported},
  7852.         [TGSI_OPCODE_GATHER4]   = { 0, tgsi_unsupported},
  7853.         [TGSI_OPCODE_SVIEWINFO] = { 0, tgsi_unsupported},
  7854.         [TGSI_OPCODE_SAMPLE_POS]        = { 0, tgsi_unsupported},
  7855.         [TGSI_OPCODE_SAMPLE_INFO]       = { 0, tgsi_unsupported},
  7856.         [TGSI_OPCODE_UARL]      = { ALU_OP1_MOVA_INT, tgsi_eg_arl},
  7857.         [TGSI_OPCODE_UCMP]      = { ALU_OP0_NOP, tgsi_ucmp},
  7858.         [TGSI_OPCODE_IABS]      = { 0, tgsi_iabs},
  7859.         [TGSI_OPCODE_ISSG]      = { 0, tgsi_issg},
  7860.         [TGSI_OPCODE_LOAD]      = { ALU_OP0_NOP, tgsi_unsupported},
  7861.         [TGSI_OPCODE_STORE]     = { ALU_OP0_NOP, tgsi_unsupported},
  7862.         [TGSI_OPCODE_MFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7863.         [TGSI_OPCODE_LFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7864.         [TGSI_OPCODE_SFENCE]    = { ALU_OP0_NOP, tgsi_unsupported},
  7865.         [TGSI_OPCODE_BARRIER]   = { ALU_OP0_NOP, tgsi_unsupported},
  7866.         [TGSI_OPCODE_ATOMUADD]  = { ALU_OP0_NOP, tgsi_unsupported},
  7867.         [TGSI_OPCODE_ATOMXCHG]  = { ALU_OP0_NOP, tgsi_unsupported},
  7868.         [TGSI_OPCODE_ATOMCAS]   = { ALU_OP0_NOP, tgsi_unsupported},
  7869.         [TGSI_OPCODE_ATOMAND]   = { ALU_OP0_NOP, tgsi_unsupported},
  7870.         [TGSI_OPCODE_ATOMOR]    = { ALU_OP0_NOP, tgsi_unsupported},
  7871.         [TGSI_OPCODE_ATOMXOR]   = { ALU_OP0_NOP, tgsi_unsupported},
  7872.         [TGSI_OPCODE_ATOMUMIN]  = { ALU_OP0_NOP, tgsi_unsupported},
  7873.         [TGSI_OPCODE_ATOMUMAX]  = { ALU_OP0_NOP, tgsi_unsupported},
  7874.         [TGSI_OPCODE_ATOMIMIN]  = { ALU_OP0_NOP, tgsi_unsupported},
  7875.         [TGSI_OPCODE_ATOMIMAX]  = { ALU_OP0_NOP, tgsi_unsupported},
  7876.         [TGSI_OPCODE_TEX2]      = { FETCH_OP_SAMPLE, tgsi_tex},
  7877.         [TGSI_OPCODE_TXB2]      = { FETCH_OP_SAMPLE_LB, tgsi_tex},
  7878.         [TGSI_OPCODE_TXL2]      = { FETCH_OP_SAMPLE_L, tgsi_tex},
  7879.         [TGSI_OPCODE_IMUL_HI]   = { ALU_OP2_MULHI_INT, cayman_mul_int_instr},
  7880.         [TGSI_OPCODE_UMUL_HI]   = { ALU_OP2_MULHI_UINT, cayman_mul_int_instr},
  7881.         [TGSI_OPCODE_TG4]       = { FETCH_OP_GATHER4, tgsi_tex},
  7882.         [TGSI_OPCODE_LODQ]      = { FETCH_OP_GET_LOD, tgsi_tex},
  7883.         [TGSI_OPCODE_IBFE]      = { ALU_OP3_BFE_INT, tgsi_op3},
  7884.         [TGSI_OPCODE_UBFE]      = { ALU_OP3_BFE_UINT, tgsi_op3},
  7885.         [TGSI_OPCODE_BFI]       = { ALU_OP0_NOP, tgsi_bfi},
  7886.         [TGSI_OPCODE_BREV]      = { ALU_OP1_BFREV_INT, tgsi_op2},
  7887.         [TGSI_OPCODE_POPC]      = { ALU_OP1_BCNT_INT, tgsi_op2},
  7888.         [TGSI_OPCODE_LSB]       = { ALU_OP1_FFBL_INT, tgsi_op2},
  7889.         [TGSI_OPCODE_IMSB]      = { ALU_OP1_FFBH_INT, tgsi_msb},
  7890.         [TGSI_OPCODE_UMSB]      = { ALU_OP1_FFBH_UINT, tgsi_msb},
  7891.         [TGSI_OPCODE_INTERP_CENTROID]   = { ALU_OP0_NOP, tgsi_interp_egcm},
  7892.         [TGSI_OPCODE_INTERP_SAMPLE]     = { ALU_OP0_NOP, tgsi_interp_egcm},
  7893.         [TGSI_OPCODE_INTERP_OFFSET]     = { ALU_OP0_NOP, tgsi_interp_egcm},
  7894.         [TGSI_OPCODE_LAST]      = { ALU_OP0_NOP, tgsi_unsupported},
  7895. };
  7896.