r600g: the code to check whether a new vertex shader is needed was wrong
[mesa.git] / src / gallium / drivers / r600 / r600_shader.c
index 523b6d24515610e217b846a44d69eeddbac35c45..21b9180582408abb88f0bf6116c09d6dc4c0bdd5 100644 (file)
@@ -25,9 +25,7 @@
 #include "tgsi/tgsi_scan.h"
 #include "tgsi/tgsi_dump.h"
 #include "util/u_format.h"
-#include "r600_screen.h"
-#include "r600_context.h"
-#include "r600_shader.h"
+#include "r600_pipe.h"
 #include "r600_asm.h"
 #include "r600_sq.h"
 #include "r600_opcodes.h"
 #include <stdio.h>
 #include <errno.h>
 
+static void r600_pipe_shader_vs(struct pipe_context *ctx, struct r600_pipe_shader *shader)
+{
+       struct r600_pipe_state *rstate = &shader->rstate;
+       struct r600_shader *rshader = &shader->shader;
+       unsigned spi_vs_out_id[10];
+       unsigned i, tmp;
+
+       /* clear previous register */
+       rstate->nregs = 0;
+
+       /* so far never got proper semantic id from tgsi */
+       for (i = 0; i < 10; i++) {
+               spi_vs_out_id[i] = 0;
+       }
+       for (i = 0; i < 32; i++) {
+               tmp = i << ((i & 3) * 8);
+               spi_vs_out_id[i / 4] |= tmp;
+       }
+       for (i = 0; i < 10; i++) {
+               r600_pipe_state_add_reg(rstate,
+                                       R_028614_SPI_VS_OUT_ID_0 + i * 4,
+                                       spi_vs_out_id[i], 0xFFFFFFFF, NULL);
+       }
+
+       r600_pipe_state_add_reg(rstate,
+                       R_0286C4_SPI_VS_OUT_CONFIG,
+                       S_0286C4_VS_EXPORT_COUNT(rshader->noutput - 2),
+                       0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                       R_028868_SQ_PGM_RESOURCES_VS,
+                       S_028868_NUM_GPRS(rshader->bc.ngpr) |
+                       S_028868_STACK_SIZE(rshader->bc.nstack),
+                       0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                       R_0288A4_SQ_PGM_RESOURCES_FS,
+                       0x00000000, 0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                       R_0288D0_SQ_PGM_CF_OFFSET_VS,
+                       0x00000000, 0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                       R_0288DC_SQ_PGM_CF_OFFSET_FS,
+                       0x00000000, 0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                       R_028858_SQ_PGM_START_VS,
+                       0x00000000, 0xFFFFFFFF, shader->bo);
+       r600_pipe_state_add_reg(rstate,
+                       R_028894_SQ_PGM_START_FS,
+                       0x00000000, 0xFFFFFFFF, shader->bo);
+
+       r600_pipe_state_add_reg(rstate,
+                               R_03E200_SQ_LOOP_CONST_0 + (32 * 4), 0x01000FFF,
+                               0xFFFFFFFF, NULL);
 
-struct r600_shader_tgsi_instruction;
+}
 
-struct r600_shader_ctx {
-       struct tgsi_shader_info                 info;
-       struct tgsi_parse_context               parse;
-       const struct tgsi_token                 *tokens;
-       unsigned                                type;
-       unsigned                                file_offset[TGSI_FILE_COUNT];
-       unsigned                                temp_reg;
-       struct r600_shader_tgsi_instruction     *inst_info;
-       struct r600_bc                          *bc;
-       struct r600_shader                      *shader;
-       u32                                     value[4];
-       u32                                     *literals;
-       u32                                     nliterals;
-       u32                                     max_driver_temp_used;
-};
+int r600_find_vs_semantic_index(struct r600_shader *vs,
+                               struct r600_shader *ps, int id)
+{
+       struct r600_shader_io *input = &ps->input[id];
 
-struct r600_shader_tgsi_instruction {
-       unsigned        tgsi_opcode;
-       unsigned        is_op3;
-       unsigned        r600_opcode;
-       int (*process)(struct r600_shader_ctx *ctx);
-};
+       for (int i = 0; i < vs->noutput; i++) {
+               if (input->name == vs->output[i].name &&
+                       input->sid == vs->output[i].sid) {
+                       return i - 1;
+               }
+       }
+       return 0;
+}
 
-static struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[], eg_shader_tgsi_instruction[];
-static int tgsi_helper_tempx_replicate(struct r600_shader_ctx *ctx);
+static void r600_pipe_shader_ps(struct pipe_context *ctx, struct r600_pipe_shader *shader)
+{
+       struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
+       struct r600_pipe_state *rstate = &shader->rstate;
+       struct r600_shader *rshader = &shader->shader;
+       unsigned i, tmp, exports_ps, num_cout, spi_ps_in_control_0, spi_input_z;
+       boolean have_pos = FALSE, have_face = FALSE;
+
+       /* clear previous register */
+       rstate->nregs = 0;
+
+       for (i = 0; i < rshader->ninput; i++) {
+               tmp = S_028644_SEMANTIC(r600_find_vs_semantic_index(&rctx->vs_shader->shader, rshader, i));
+               tmp |= S_028644_SEL_CENTROID(1);
+               if (rshader->input[i].name == TGSI_SEMANTIC_POSITION)
+                       have_pos = TRUE;
+               if (rshader->input[i].name == TGSI_SEMANTIC_COLOR ||
+                   rshader->input[i].name == TGSI_SEMANTIC_BCOLOR ||
+                   rshader->input[i].name == TGSI_SEMANTIC_POSITION) {
+                       tmp |= S_028644_FLAT_SHADE(rshader->flat_shade);
+               }
+               if (rshader->input[i].name == TGSI_SEMANTIC_FACE)
+                       have_face = TRUE;
+               if (rshader->input[i].name == TGSI_SEMANTIC_GENERIC &&
+                       rctx->sprite_coord_enable & (1 << rshader->input[i].sid)) {
+                       tmp |= S_028644_PT_SPRITE_TEX(1);
+               }
+               r600_pipe_state_add_reg(rstate, R_028644_SPI_PS_INPUT_CNTL_0 + i * 4, tmp, 0xFFFFFFFF, NULL);
+       }
+       for (i = 0; i < rshader->noutput; i++) {
+               if (rshader->input[i].name == TGSI_SEMANTIC_POSITION)
+                       r600_pipe_state_add_reg(rstate,
+                                               R_02880C_DB_SHADER_CONTROL,
+                                               S_02880C_Z_EXPORT_ENABLE(1),
+                                               S_02880C_Z_EXPORT_ENABLE(1), NULL);
+       }
+
+       exports_ps = 0;
+       num_cout = 0;
+       for (i = 0; i < rshader->noutput; i++) {
+               if (rshader->output[i].name == TGSI_SEMANTIC_POSITION)
+                       exports_ps |= 1;
+               else if (rshader->output[i].name == TGSI_SEMANTIC_COLOR) {
+                       num_cout++;
+               }
+       }
+       exports_ps |= S_028854_EXPORT_COLORS(num_cout);
+       if (!exports_ps) {
+               /* always at least export 1 component per pixel */
+               exports_ps = 2;
+       }
+
+       spi_ps_in_control_0 = S_0286CC_NUM_INTERP(rshader->ninput) |
+                               S_0286CC_PERSP_GRADIENT_ENA(1);
+       spi_input_z = 0;
+       if (have_pos) {
+               spi_ps_in_control_0 |=  S_0286CC_POSITION_ENA(1) |
+                                       S_0286CC_BARYC_SAMPLE_CNTL(1);
+               spi_input_z |= 1;
+       }
+       r600_pipe_state_add_reg(rstate, R_0286CC_SPI_PS_IN_CONTROL_0, spi_ps_in_control_0, 0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate, R_0286D0_SPI_PS_IN_CONTROL_1, S_0286D0_FRONT_FACE_ENA(have_face), 0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate, R_0286D8_SPI_INPUT_Z, spi_input_z, 0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                               R_028840_SQ_PGM_START_PS,
+                               0x00000000, 0xFFFFFFFF, shader->bo);
+       r600_pipe_state_add_reg(rstate,
+                               R_028850_SQ_PGM_RESOURCES_PS,
+                               S_028868_NUM_GPRS(rshader->bc.ngpr) |
+                               S_028868_STACK_SIZE(rshader->bc.nstack),
+                               0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                               R_028854_SQ_PGM_EXPORTS_PS,
+                               exports_ps, 0xFFFFFFFF, NULL);
+       r600_pipe_state_add_reg(rstate,
+                               R_0288CC_SQ_PGM_CF_OFFSET_PS,
+                               0x00000000, 0xFFFFFFFF, NULL);
+
+       if (rshader->uses_kill) {
+               /* only set some bits here, the other bits are set in the dsa state */
+               r600_pipe_state_add_reg(rstate,
+                                       R_02880C_DB_SHADER_CONTROL,
+                                       S_02880C_KILL_ENABLE(1),
+                                       S_02880C_KILL_ENABLE(1), NULL);
+       }
+       r600_pipe_state_add_reg(rstate,
+                               R_03E200_SQ_LOOP_CONST_0, 0x01000FFF,
+                               0xFFFFFFFF, NULL);
+}
+
+static int r600_pipe_shader(struct pipe_context *ctx, struct r600_pipe_shader *shader)
+{
+       struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
+       struct r600_shader *rshader = &shader->shader;
+       void *ptr;
+
+       /* copy new shader */
+       if (shader->bo == NULL) {
+               shader->bo = radeon_ws_bo(rctx->radeon, rshader->bc.ndw * 4, 4096, 0);
+               if (shader->bo == NULL) {
+                       return -ENOMEM;
+               }
+               ptr = radeon_ws_bo_map(rctx->radeon, shader->bo, 0, NULL);
+               memcpy(ptr, rshader->bc.bytecode, rshader->bc.ndw * 4);
+               radeon_ws_bo_unmap(rctx->radeon, shader->bo);
+       }
+       /* build state */
+       rshader->flat_shade = rctx->flatshade;
+       switch (rshader->processor_type) {
+       case TGSI_PROCESSOR_VERTEX:
+               if (rshader->family >= CHIP_CEDAR) {
+                       evergreen_pipe_shader_vs(ctx, shader);
+               } else {
+                       r600_pipe_shader_vs(ctx, shader);
+               }
+               break;
+       case TGSI_PROCESSOR_FRAGMENT:
+               if (rshader->family >= CHIP_CEDAR) {
+                       evergreen_pipe_shader_ps(ctx, shader);
+               } else {
+                       r600_pipe_shader_ps(ctx, shader);
+               }
+               break;
+       default:
+               return -EINVAL;
+       }
+       r600_context_pipe_state_set(&rctx->ctx, &shader->rstate);
+       return 0;
+}
 
-static int r600_shader_update(struct pipe_context *ctx, struct r600_shader *shader)
+static int r600_shader_update(struct pipe_context *ctx, struct r600_pipe_shader *rshader)
 {
-       struct r600_context *rctx = r600_context(ctx);
+       struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
+       struct r600_shader *shader = &rshader->shader;
        const struct util_format_description *desc;
        enum pipe_format resource_format[160];
        unsigned i, nresources = 0;
@@ -76,9 +245,16 @@ static int r600_shader_update(struct pipe_context *ctx, struct r600_shader *shad
 
        if (shader->processor_type != TGSI_PROCESSOR_VERTEX)
                return 0;
+       /* doing a full memcmp fell over the refcount */
+       if ((rshader->vertex_elements.count == rctx->vertex_elements->count) &&
+           (!memcmp(&rshader->vertex_elements.elements, &rctx->vertex_elements->elements, 32 * sizeof(struct pipe_vertex_element)))) {
+               return 0;
+       }
+       rshader->vertex_elements = *rctx->vertex_elements;
        for (i = 0; i < rctx->vertex_elements->count; i++) {
                resource_format[nresources++] = rctx->vertex_elements->elements[i].src_format;
        }
+       radeon_ws_bo_reference(rctx->radeon, &rshader->bo, NULL);
        LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {
                switch (cf->inst) {
                case V_SQ_CF_WORD1_SQ_CF_INST_VTX:
@@ -102,25 +278,40 @@ static int r600_shader_update(struct pipe_context *ctx, struct r600_shader *shad
        return r600_bc_build(&shader->bc);
 }
 
-int r600_pipe_shader_create(struct pipe_context *ctx,
-                       struct r600_context_state *rpshader,
-                       const struct tgsi_token *tokens)
+int r600_pipe_shader_update(struct pipe_context *ctx, struct r600_pipe_shader *shader)
 {
-       struct r600_screen *rscreen = r600_screen(ctx->screen);
+       struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
+       int r;
+
+       if (shader == NULL)
+               return -EINVAL;
+       /* there should be enough input */
+       if (rctx->vertex_elements->count < shader->shader.bc.nresource) {
+               R600_ERR("%d resources provided, expecting %d\n",
+                       rctx->vertex_elements->count, shader->shader.bc.nresource);
+               return -EINVAL;
+       }
+       r = r600_shader_update(ctx, shader);
+       if (r)
+               return r;
+       return r600_pipe_shader(ctx, shader);
+}
+
+int r600_shader_from_tgsi(const struct tgsi_token *tokens, struct r600_shader *shader);
+int r600_pipe_shader_create(struct pipe_context *ctx, struct r600_pipe_shader *shader, const struct tgsi_token *tokens)
+{
+       struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
        int r;
 
 //fprintf(stderr, "--------------------------------------------------------------\n");
 //tgsi_dump(tokens, 0);
-       if (rpshader == NULL)
-               return -ENOMEM;
-       rpshader->shader.family = radeon_get_family(rscreen->rw);
-       rpshader->shader.use_mem_constant = rscreen->use_mem_constant;
-       r = r600_shader_from_tgsi(tokens, &rpshader->shader);
+       shader->shader.family = r600_get_family(rctx->radeon);
+       r = r600_shader_from_tgsi(tokens, &shader->shader);
        if (r) {
                R600_ERR("translation from TGSI failed !\n");
                return r;
        }
-       r = r600_bc_build(&rpshader->shader.bc);
+       r = r600_bc_build(&shader->shader.bc);
        if (r) {
                R600_ERR("building bytecode failed !\n");
                return r;
@@ -129,81 +320,36 @@ int r600_pipe_shader_create(struct pipe_context *ctx,
        return 0;
 }
 
-static int r600_pipe_shader_vs(struct pipe_context *ctx, struct r600_context_state *rpshader)
-{
-       struct r600_context *rctx = r600_context(ctx);
-       struct radeon_state *state;
-
-       state = &rpshader->rstate[0];
-       radeon_state_fini(&rpshader->rstate[0]);
-
-       return rctx->vtbl->vs_shader(rctx, rpshader, state);
-}
-
-static int r600_pipe_shader_ps(struct pipe_context *ctx, struct r600_context_state *rpshader)
-{
-       struct r600_context *rctx = r600_context(ctx);
-       struct radeon_state *state;
-
-       state = &rpshader->rstate[0];
-       radeon_state_fini(state);
-
-       return rctx->vtbl->ps_shader(rctx, rpshader, state);
-}
-
-static int r600_pipe_shader(struct pipe_context *ctx, struct r600_context_state *rpshader)
-{
-       struct r600_screen *rscreen = r600_screen(ctx->screen);
-       struct r600_context *rctx = r600_context(ctx);
-       struct r600_shader *rshader = &rpshader->shader;
-       int r;
-       void *data;
+/*
+ * tgsi -> r600 shader
+ */
+struct r600_shader_tgsi_instruction;
 
-       /* copy new shader */
-       radeon_ws_bo_reference(rscreen->rw, &rpshader->bo, NULL);
-       rpshader->bo = NULL;
-       rpshader->bo = radeon_ws_bo(rscreen->rw, rshader->bc.ndw * 4,
-                                   4096, 0);
-       if (rpshader->bo == NULL) {
-               return -ENOMEM;
-       }
-       data = radeon_ws_bo_map(rscreen->rw, rpshader->bo, 0, rctx);
-       memcpy(data, rshader->bc.bytecode, rshader->bc.ndw * 4);
-       radeon_ws_bo_unmap(rscreen->rw, rpshader->bo);
-       /* build state */
-       rshader->flat_shade = rctx->flat_shade;
-       switch (rshader->processor_type) {
-       case TGSI_PROCESSOR_VERTEX:
-               r = r600_pipe_shader_vs(ctx, rpshader);
-               break;
-       case TGSI_PROCESSOR_FRAGMENT:
-               r = r600_pipe_shader_ps(ctx, rpshader);
-               break;
-       default:
-               r = -EINVAL;
-               break;
-       }
-       return r;
-}
+struct r600_shader_ctx {
+       struct tgsi_shader_info                 info;
+       struct tgsi_parse_context               parse;
+       const struct tgsi_token                 *tokens;
+       unsigned                                type;
+       unsigned                                file_offset[TGSI_FILE_COUNT];
+       unsigned                                temp_reg;
+       struct r600_shader_tgsi_instruction     *inst_info;
+       struct r600_bc                          *bc;
+       struct r600_shader                      *shader;
+       u32                                     value[4];
+       u32                                     *literals;
+       u32                                     nliterals;
+       u32                                     max_driver_temp_used;
+};
 
-int r600_pipe_shader_update(struct pipe_context *ctx, struct r600_context_state *rpshader)
-{
-       struct r600_context *rctx = r600_context(ctx);
-       int r;
+struct r600_shader_tgsi_instruction {
+       unsigned        tgsi_opcode;
+       unsigned        is_op3;
+       unsigned        r600_opcode;
+       int (*process)(struct r600_shader_ctx *ctx);
+};
 
-       if (rpshader == NULL)
-               return -EINVAL;
-       /* there should be enough input */
-       if (rctx->vertex_elements->count < rpshader->shader.bc.nresource) {
-               R600_ERR("%d resources provided, expecting %d\n",
-                       rctx->vertex_elements->count, rpshader->shader.bc.nresource);
-               return -EINVAL;
-       }
-       r = r600_shader_update(ctx, &rpshader->shader);
-       if (r)
-               return r;
-       return r600_pipe_shader(ctx, rpshader);
-}
+static struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[], eg_shader_tgsi_instruction[];
+static int tgsi_helper_tempx_replicate(struct r600_shader_ctx *ctx);
 
 static int tgsi_is_supported(struct r600_shader_ctx *ctx)
 {
@@ -304,6 +450,7 @@ static int tgsi_declaration(struct r600_shader_ctx *ctx)
                        vtx.dst_sel_y = 1;
                        vtx.dst_sel_z = 2;
                        vtx.dst_sel_w = 3;
+                       vtx.use_const_fields = 1;
                        r = r600_bc_add_vtx(ctx->bc, &vtx);
                        if (r)
                                return r;
@@ -642,12 +789,12 @@ static int tgsi_split_constant(struct r600_shader_ctx *ctx, struct r600_bc_alu_s
                }
        }
        for (i = 0, j = nconst - 1; i < inst->Instruction.NumSrcRegs; i++) {
-               if (inst->Src[j].Register.File == TGSI_FILE_CONSTANT && j > 0) {
+               if (j > 0 && inst->Src[i].Register.File == TGSI_FILE_CONSTANT) {
                        int treg = r600_get_temp(ctx);
                        for (k = 0; k < 4; k++) {
                                memset(&alu, 0, sizeof(struct r600_bc_alu));
                                alu.inst = CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOV);
-                               alu.src[0].sel = r600_src[j].sel;
+                               alu.src[0].sel = r600_src[i].sel;
                                alu.src[0].chan = k;
                                alu.dst.sel = treg;
                                alu.dst.chan = k;
@@ -658,7 +805,7 @@ static int tgsi_split_constant(struct r600_shader_ctx *ctx, struct r600_bc_alu_s
                                if (r)
                                        return r;
                        }
-                       r600_src[j].sel = treg;
+                       r600_src[i].sel = treg;
                        j--;
                }
        }
@@ -677,13 +824,13 @@ static int tgsi_split_literal_constant(struct r600_shader_ctx *ctx, struct r600_
                        nliteral++;
                }
        }
-       for (i = 0, j = 0; i < inst->Instruction.NumSrcRegs; i++) {
-               if (inst->Src[j].Register.File == TGSI_FILE_IMMEDIATE) {
+       for (i = 0, j = nliteral - 1; i < inst->Instruction.NumSrcRegs; i++) {
+               if (j > 0 && inst->Src[i].Register.File == TGSI_FILE_IMMEDIATE) {
                        int treg = r600_get_temp(ctx);
                        for (k = 0; k < 4; k++) {
                                memset(&alu, 0, sizeof(struct r600_bc_alu));
                                alu.inst = CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOV);
-                               alu.src[0].sel = r600_src[j].sel;
+                               alu.src[0].sel = r600_src[i].sel;
                                alu.src[0].chan = k;
                                alu.dst.sel = treg;
                                alu.dst.chan = k;
@@ -694,11 +841,11 @@ static int tgsi_split_literal_constant(struct r600_shader_ctx *ctx, struct r600_
                                if (r)
                                        return r;
                        }
-                       r = r600_bc_add_literal(ctx->bc, ctx->value);
+                       r = r600_bc_add_literal(ctx->bc, &ctx->literals[inst->Src[i].Register.Index * 4]);
                        if (r)
                                return r;
-                       r600_src[j].sel = treg;
-                       j++;
+                       r600_src[i].sel = treg;
+                       j--;
                }
        }
        return 0;
@@ -719,6 +866,9 @@ static int tgsi_op2_s(struct r600_shader_ctx *ctx, int swap)
        }
 
        r = tgsi_split_constant(ctx, r600_src);
+       if (r)
+               return r;
+       r = tgsi_split_literal_constant(ctx, r600_src);
        if (r)
                return r;
        for (i = 0; i < lasti + 1; i++) {
@@ -789,6 +939,9 @@ static int tgsi_setup_trig(struct r600_shader_ctx *ctx,
        
        memset(lit_vals, 0, 4*4);
        r = tgsi_split_constant(ctx, r600_src);
+       if (r)
+               return r;
+       r = tgsi_split_literal_constant(ctx, r600_src);
        if (r)
                return r;
 
@@ -967,6 +1120,54 @@ static int tgsi_scs(struct r600_shader_ctx *ctx)
                        return r;
        }
 
+       /* dst.z = 0.0; */
+       if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
+               memset(&alu, 0, sizeof(struct r600_bc_alu));
+
+               alu.inst = CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOV);
+
+               r = tgsi_dst(ctx, &inst->Dst[0], 2, &alu.dst);
+               if (r)
+                       return r;
+
+               alu.src[0].sel = V_SQ_ALU_SRC_0;
+               alu.src[0].chan = 0;
+
+               alu.last = 1;
+
+               r = r600_bc_add_alu(ctx->bc, &alu);
+               if (r)
+                       return r;
+
+               r = r600_bc_add_literal(ctx->bc, ctx->value);
+               if (r)
+                       return r;
+       }
+
+       /* dst.w = 1.0; */
+       if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
+               memset(&alu, 0, sizeof(struct r600_bc_alu));
+
+               alu.inst = CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOV);
+
+               r = tgsi_dst(ctx, &inst->Dst[0], 3, &alu.dst);
+               if (r)
+                       return r;
+
+               alu.src[0].sel = V_SQ_ALU_SRC_1;
+               alu.src[0].chan = 0;
+
+               alu.last = 1;
+
+               r = r600_bc_add_alu(ctx->bc, &alu);
+               if (r)
+                       return r;
+
+               r = r600_bc_add_literal(ctx->bc, ctx->value);
+               if (r)
+                       return r;
+       }
+
        return 0;
 }
 
@@ -1166,34 +1367,6 @@ static int tgsi_rsq(struct r600_shader_ctx *ctx)
        return tgsi_helper_tempx_replicate(ctx);
 }
 
-static int tgsi_trans(struct r600_shader_ctx *ctx)
-{
-       struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
-       struct r600_bc_alu alu;
-       int i, j, r;
-
-       for (i = 0; i < 4; i++) {
-               memset(&alu, 0, sizeof(struct r600_bc_alu));
-               if (inst->Dst[0].Register.WriteMask & (1 << i)) {
-                       alu.inst = ctx->inst_info->r600_opcode;
-                       for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
-                               r = tgsi_src(ctx, &inst->Src[j], &alu.src[j]);
-                               if (r)
-                                       return r;
-                               alu.src[j].chan = tgsi_chan(&inst->Src[j], i);
-                       }
-                       r = tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
-                       if (r)
-                               return r;
-                       alu.last = 1;
-                       r = r600_bc_add_alu(ctx->bc, &alu);
-                       if (r)
-                               return r;
-               }
-       }
-       return 0;
-}
-
 static int tgsi_helper_tempx_replicate(struct r600_shader_ctx *ctx)
 {
        struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
@@ -1308,6 +1481,9 @@ static int tgsi_ssg(struct r600_shader_ctx *ctx)
        int i, r;
 
        r = tgsi_split_constant(ctx, r600_src);
+       if (r)
+               return r;
+       r = tgsi_split_literal_constant(ctx, r600_src);
        if (r)
                return r;
 
@@ -1404,6 +1580,9 @@ static int tgsi_op3(struct r600_shader_ctx *ctx)
        int i, j, r;
 
        r = tgsi_split_constant(ctx, r600_src);
+       if (r)
+               return r;
+       r = tgsi_split_literal_constant(ctx, r600_src);
        if (r)
                return r;
        /* do it in 2 step as op3 doesn't support writemask */
@@ -1436,6 +1615,9 @@ static int tgsi_dp(struct r600_shader_ctx *ctx)
        int i, j, r;
 
        r = tgsi_split_constant(ctx, r600_src);
+       if (r)
+               return r;
+       r = tgsi_split_literal_constant(ctx, r600_src);
        if (r)
                return r;
        for (i = 0; i < 4; i++) {
@@ -1511,7 +1693,7 @@ static int tgsi_tex(struct r600_shader_ctx *ctx)
                r = r600_bc_add_alu(ctx->bc, &alu);
                if (r)
                        return r;
-               
+
                for (i = 0; i < 3; i++) {
                        memset(&alu, 0, sizeof(struct r600_bc_alu));
                        alu.inst = CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MUL);
@@ -1567,6 +1749,11 @@ static int tgsi_tex(struct r600_shader_ctx *ctx)
                                src_chan = 1;
                                src2_chan = 2;
                                break;
+                       default:
+                               assert(0);
+                               src_chan = 0;
+                               src2_chan = 0;
+                               break;
                        }
                        r = tgsi_src(ctx, &inst->Src[0], &alu.src[0]);
                        if (r)
@@ -1683,10 +1870,10 @@ static int tgsi_tex(struct r600_shader_ctx *ctx)
        tex.sampler_id = tex.resource_id;
        tex.src_gpr = src_gpr;
        tex.dst_gpr = ctx->file_offset[inst->Dst[0].Register.File] + inst->Dst[0].Register.Index;
-       tex.dst_sel_x = 0;
-       tex.dst_sel_y = 1;
-       tex.dst_sel_z = 2;
-       tex.dst_sel_w = 3;
+       tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;
+       tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
+       tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7;
+       tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
        tex.src_sel_x = 0;
        tex.src_sel_y = 1;
        tex.src_sel_z = 2;
@@ -1727,6 +1914,9 @@ static int tgsi_lrp(struct r600_shader_ctx *ctx)
        int r;
 
        r = tgsi_split_constant(ctx, r600_src);
+       if (r)
+               return r;
+       r = tgsi_split_literal_constant(ctx, r600_src);
        if (r)
                return r;
        /* 1 - src0 */
@@ -1806,6 +1996,9 @@ static int tgsi_cmp(struct r600_shader_ctx *ctx)
        int i, r;
 
        r = tgsi_split_constant(ctx, r600_src);
+       if (r)
+               return r;
+       r = tgsi_split_literal_constant(ctx, r600_src);
        if (r)
                return r;
 
@@ -1859,7 +2052,10 @@ static int tgsi_xpd(struct r600_shader_ctx *ctx)
        r = tgsi_split_constant(ctx, r600_src);
        if (r)
                return r;
-       
+       r = tgsi_split_literal_constant(ctx, r600_src);
+       if (r)
+               return r;
+
        for (i = 0; i < 4; i++) {
                memset(&alu, 0, sizeof(struct r600_bc_alu));
                alu.inst = CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MUL);