r5xx: Dump shader constants when dumping program assembly.
[mesa.git] / src / mesa / drivers / dri / r300 / r500_fragprog.c
index f55c8560c752197081f68a2e79a156d29a12950b..f8321fd12366b5dfec063619977637b0b2e0d480 100644 (file)
 
 /* Writemasks */
 #define R500_WRITEMASK_G 0x2
+#define R500_WRITEMASK_RGB 0x7
 #define R500_WRITEMASK_A 0x8
 #define R500_WRITEMASK_AR 0x9
 #define R500_WRITEMASK_AG 0xA
@@ -138,6 +139,19 @@ static inline GLuint make_rgb_swizzle(struct prog_src_register src) {
        return swiz;
 }
 
+static inline GLuint make_rgba_swizzle(GLuint src) {
+       GLuint swiz = 0x0;
+       GLuint temp;
+       int i;
+       for (i = 0; i < 4; i++) {
+               temp = GET_SWZ(src, i);
+               /* Fix SWIZZLE_ONE */
+               if (temp == 5) temp++;
+               swiz |= temp << i*3;
+       }
+       return swiz;
+}
+
 static inline GLuint make_alpha_swizzle(struct prog_src_register src) {
        GLuint swiz = GET_SWZ(src.Swizzle, 3);
 
@@ -170,7 +184,7 @@ static int get_temp(struct r500_fragment_program *fp, int slot) {
 
        COMPILE_STATE;
 
-       int r = cs->temp_in_use + 1 + slot;
+       int r = fp->temp_reg_offset + cs->temp_in_use + slot;
 
        if (r > R500_US_NUM_TEMP_REGS) {
                ERROR("Too many temporary registers requested, can't compile!\n");
@@ -358,21 +372,24 @@ static void emit_alu(struct r500_fragment_program *fp, int counter, struct prog_
        fp->inst[counter].inst5 = 0x0;
 }
 
-static void emit_mov(struct r500_fragment_program *fp, int counter, struct prog_src_register src, GLuint dest) {
+static void emit_mov(struct r500_fragment_program *fp, int counter, struct prog_instruction *fpi, GLuint src_reg, GLuint swizzle, GLuint dest) {
        /* The r3xx shader uses MAD to implement MOV. We are using CMP, since
         * it is technically more accurate and recommended by ATI/AMD. */
-       GLuint src_reg = make_src(fp, src);
+       emit_alu(fp, counter, fpi);
        fp->inst[counter].inst1 = R500_RGB_ADDR0(src_reg);
        fp->inst[counter].inst2 = R500_ALPHA_ADDR0(src_reg);
+       /* (De)mangle the swizzle from Mesa to R500. */
+       swizzle = make_rgba_swizzle(swizzle);
+       /* 0x1FF is 9 bits, size of an RGB swizzle. */
        fp->inst[counter].inst3 = R500_ALU_RGB_SEL_A_SRC0
-               | MAKE_SWIZ_RGB_A(make_rgb_swizzle(src))
+               | MAKE_SWIZ_RGB_A((swizzle & 0x1ff))
                | R500_ALU_RGB_SEL_B_SRC0
-               | MAKE_SWIZ_RGB_B(make_rgb_swizzle(src))
+               | MAKE_SWIZ_RGB_B((swizzle & 0x1ff))
                | R500_ALU_RGB_OMOD_DISABLE;
        fp->inst[counter].inst4 |= R500_ALPHA_OP_CMP
                | R500_ALPHA_ADDRD(dest)
-               | R500_ALPHA_SEL_A_SRC0 | MAKE_SWIZ_ALPHA_A(make_alpha_swizzle(src))
-               | R500_ALPHA_SEL_B_SRC0 | MAKE_SWIZ_ALPHA_B(make_alpha_swizzle(src))
+               | R500_ALPHA_SEL_A_SRC0 | MAKE_SWIZ_ALPHA_A(GET_SWZ(swizzle, 3))
+               | R500_ALPHA_SEL_B_SRC0 | MAKE_SWIZ_ALPHA_B(GET_SWZ(swizzle, 3))
                | R500_ALPHA_OMOD_DISABLE;
        fp->inst[counter].inst5 = R500_ALU_RGBA_OP_CMP
                | R500_ALU_RGBA_ADDRD(dest)
@@ -481,8 +498,7 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
 
                switch (fpi->Opcode) {
                        case OPCODE_ABS:
-                               emit_alu(fp, counter, fpi);
-                               emit_mov(fp, counter, fpi->SrcReg[0], dest);
+                               emit_mov(fp, counter, fpi, make_src(fp, fpi->SrcReg[0]), fpi->SrcReg[0].Swizzle, dest);
                                fp->inst[counter].inst3 |= R500_ALU_RGB_MOD_A_ABS
                                        | R500_ALU_RGB_MOD_B_ABS;
                                fp->inst[counter].inst4 |= R500_ALPHA_MOD_A_ABS
@@ -705,8 +721,8 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
                                counter++;
                                /* Second instruction */
                                fp->inst[counter].inst0 = R500_INST_TYPE_ALU | (R500_WRITEMASK_AB << 11);
-                               fp->inst[counter].inst1 = R500_RGB_ADDR0(get_temp(fp, 0));
-                               fp->inst[counter].inst2 = R500_ALPHA_ADDR0(get_temp(fp, 0)) | R500_ALPHA_ADDR1(src[1]);
+                               fp->inst[counter].inst1 = R500_RGB_ADDR0(get_temp(fp, 0)) | R500_RGB_ADDR1(src[1]);
+                               fp->inst[counter].inst2 = R500_ALPHA_ADDR0(get_temp(fp, 0));
                                /* Select [z, z, z, y] */
                                temp_swiz = 2 | (2 << 3) | (2 << 6);
                                fp->inst[counter].inst3 = R500_ALU_RGB_SEL_A_SRC0
@@ -731,8 +747,8 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
                                        | MAKE_SWIZ_RGB_B(R500_SWIZ_RGB_ONE);
                                fp->inst[counter].inst4 = R500_ALPHA_OP_MAD
                                        | R500_ALPHA_ADDRD(get_temp(fp, 1))
-                                       | R500_ALPHA_SEL_A_SRC0 | R500_ALPHA_SWIZ_A_B
-                                       | R500_ALPHA_SEL_B_SRC0 | R500_ALPHA_SWIZ_B_A;
+                                       | R500_ALPHA_SEL_A_SRC0 | R500_ALPHA_SWIZ_A_A
+                                       | R500_ALPHA_SEL_B_SRC0 | R500_ALPHA_SWIZ_B_B;
                                fp->inst[counter].inst5 = R500_ALU_RGBA_OP_MAD
                                        | R500_ALU_RGBA_ADDRD(get_temp(fp, 1))
                                        | MAKE_SWIZ_RGBA_C(R500_SWIZ_RGB_ZERO)
@@ -773,10 +789,7 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
                                        | MAKE_SWIZ_ALPHA_C(R500_SWIZZLE_ZERO);
                                counter++;
                                /* Final instruction */
-                               emit_alu(fp, counter, fpi);
-                               fpi->SrcReg[0].Index = get_temp(fp, 0);
-                               fpi->SrcReg[0].Swizzle = 1672;
-                               emit_mov(fp, counter, fpi->SrcReg[0], dest);
+                               emit_mov(fp, counter, fpi, get_temp(fp, 0), SWIZZLE_NOOP, dest);
                                break;
                        case OPCODE_LRP:
                                /* src0 * src1 + INV(src0) * src2
@@ -863,8 +876,7 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
                                        | R500_ALU_RGBA_ADDRD(dest);
                                break;
                        case OPCODE_MOV:
-                               emit_alu(fp, counter, fpi);
-                               emit_mov(fp, counter, fpi->SrcReg[0], dest);
+                               emit_mov(fp, counter, fpi, make_src(fp, fpi->SrcReg[0]), fpi->SrcReg[0].Swizzle, dest);
                                break;
                        case OPCODE_MUL:
                                /* Variation on MAD: src0*src1+0 */
@@ -1138,8 +1150,66 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
                                break;
                        case OPCODE_SWZ:
                                /* TODO: The rarer negation masks! */
+                               emit_mov(fp, counter, fpi, make_src(fp, fpi->SrcReg[0]), fpi->SrcReg[0].Swizzle, dest);
+                               break;
+                       case OPCODE_XPD:
+                               /* src0 * src1 - src1 * src0
+                                * 1) MUL temp.xyz, src0.yzx, src1.zxy
+                                * 2) MAD src0.zxy, src1.yzx, -temp.xyz */
+                               src[0] = make_src(fp, fpi->SrcReg[0]);
+                               src[1] = make_src(fp, fpi->SrcReg[1]);
+                               fp->inst[counter].inst0 = R500_INST_TYPE_ALU | R500_INST_TEX_SEM_WAIT
+                                       | (R500_WRITEMASK_RGB << 11);
+                               fp->inst[counter].inst1 = R500_RGB_ADDR0(src[0])
+                                       | R500_RGB_ADDR1(src[1]);
+                               fp->inst[counter].inst2 = R500_ALPHA_ADDR0(src[0])
+                                       | R500_ALPHA_ADDR1(src[1]);
+                               /* Select [y, z, x] */
+                               temp_swiz = make_rgb_swizzle(fpi->SrcReg[0]);
+                               temp_swiz = (GET_SWZ(temp_swiz, 1) << 0) | (GET_SWZ(temp_swiz, 2) << 3) | (GET_SWZ(temp_swiz, 0) << 6);
+                               fp->inst[counter].inst3 = R500_ALU_RGB_SEL_A_SRC0
+                                       | MAKE_SWIZ_RGB_A(temp_swiz);
+                               /* Select [z, x, y] */
+                               temp_swiz = make_rgb_swizzle(fpi->SrcReg[1]);
+                               temp_swiz = (GET_SWZ(temp_swiz, 2) << 0) | (GET_SWZ(temp_swiz, 0) << 3) | (GET_SWZ(temp_swiz, 1) << 6);
+                               fp->inst[counter].inst3 |= R500_ALU_RGB_SEL_B_SRC1
+                                       | MAKE_SWIZ_RGB_B(temp_swiz);
+                               fp->inst[counter].inst4 = R500_ALPHA_OP_MAD
+                                       | R500_ALPHA_ADDRD(get_temp(fp, 0))
+                                       | R500_ALPHA_SEL_A_SRC0 | MAKE_SWIZ_ALPHA_A(make_alpha_swizzle(fpi->SrcReg[0]))
+                                       | R500_ALPHA_SEL_B_SRC1 | MAKE_SWIZ_ALPHA_B(make_alpha_swizzle(fpi->SrcReg[1]));
+                               fp->inst[counter].inst5 = R500_ALU_RGBA_OP_MAD
+                                       | R500_ALU_RGBA_ADDRD(get_temp(fp, 0))
+                                       | MAKE_SWIZ_RGBA_C(R500_SWIZ_RGB_ZERO)
+                                       | MAKE_SWIZ_ALPHA_C(R500_SWIZZLE_ZERO);
+                               counter++;
                                emit_alu(fp, counter, fpi);
-                               emit_mov(fp, counter, fpi->SrcReg[0], dest);
+                               fp->inst[counter].inst1 = R500_RGB_ADDR0(src[0])
+                                       | R500_RGB_ADDR1(src[1])
+                                       | R500_RGB_ADDR2(get_temp(fp, 0));
+                               fp->inst[counter].inst2 = R500_ALPHA_ADDR0(src[0])
+                                       | R500_ALPHA_ADDR1(src[1])
+                                       | R500_ALPHA_ADDR2(get_temp(fp, 0));
+                               /* Select [z, x, y] */
+                               temp_swiz = make_rgb_swizzle(fpi->SrcReg[0]);
+                               temp_swiz = (GET_SWZ(temp_swiz, 2) << 0) | (GET_SWZ(temp_swiz, 0) << 3) | (GET_SWZ(temp_swiz, 1) << 6);
+                               fp->inst[counter].inst3 = R500_ALU_RGB_SEL_A_SRC0
+                                       | MAKE_SWIZ_RGB_A(temp_swiz);
+                               /* Select [y, z, x] */
+                               temp_swiz = make_rgb_swizzle(fpi->SrcReg[1]);
+                               temp_swiz = (GET_SWZ(temp_swiz, 1) << 0) | (GET_SWZ(temp_swiz, 2) << 3) | (GET_SWZ(temp_swiz, 0) << 6);
+                               fp->inst[counter].inst3 |= R500_ALU_RGB_SEL_B_SRC1
+                                       | MAKE_SWIZ_RGB_B(temp_swiz);
+                               fp->inst[counter].inst4 |= R500_ALPHA_OP_MAD
+                                       | R500_ALPHA_ADDRD(dest)
+                                       | R500_ALPHA_SWIZ_A_1
+                                       | R500_ALPHA_SWIZ_B_1;
+                               fp->inst[counter].inst5 = R500_ALU_RGBA_OP_MAD
+                                       | R500_ALU_RGBA_ADDRD(dest)
+                                       | R500_ALU_RGBA_SEL_C_SRC2
+                                       | MAKE_SWIZ_RGBA_C(R500_SWIZ_RGB_RGB)
+                                       | R500_ALU_RGBA_MOD_C_NEG
+                                       | R500_ALU_RGBA_A_SWIZ_0;
                                break;
                        case OPCODE_KIL:
                        case OPCODE_TEX:
@@ -1196,15 +1266,11 @@ static void init_program(r300ContextPtr r300, struct r500_fragment_program *fp)
        fp->translated = GL_FALSE;
        fp->error = GL_FALSE;
        fp->cs = cs = &(R300_CONTEXT(fp->ctx)->state.pfs_compile);
-       fp->cur_node = 0;
-       fp->first_node_has_tex = 0;
        fp->const_nr = 0;
        /* Size of pixel stack, plus 1. */
        fp->max_temp_idx = 1;
        /* Temp register offset. */
        fp->temp_reg_offset = 0;
-       fp->node[0].alu_end = -1;
-       fp->node[0].tex_end = -1;
 
        _mesa_memset(cs, 0, sizeof(*fp->cs));
        for (i = 0; i < PFS_MAX_ALU_INST; i++) {
@@ -1277,15 +1343,18 @@ static void init_program(r300ContextPtr r300, struct r500_fragment_program *fp)
        for (fpi = mp->Base.Instructions; fpi->Opcode != OPCODE_END; fpi++) {
                for (i = 0; i < 3; i++) {
                        if (fpi->SrcReg[i].File == PROGRAM_TEMPORARY) {
-                               if (fpi->SrcReg[i].Index > temps_used)
-                                       temps_used = fpi->SrcReg[i].Index;
+                               if (fpi->SrcReg[i].Index >= temps_used)
+                                       temps_used = fpi->SrcReg[i].Index + 1;
                        }
                }
        }
 
-       cs->temp_in_use = temps_used;
+       cs->temp_in_use = temps_used + 1;
 
-       fp->max_temp_idx = fp->temp_reg_offset + cs->temp_in_use + 1;
+       fp->max_temp_idx = fp->temp_reg_offset + cs->temp_in_use;
+
+       if (RADEON_DEBUG & DEBUG_PIXEL)
+               fprintf(stderr, "FP temp indices: fp->max_temp_idx: %d cs->temp_in_use: %d\n", fp->max_temp_idx, cs->temp_in_use);
 }
 
 static void update_params(struct r500_fragment_program *fp)
@@ -1426,7 +1495,7 @@ static char *toswiz(int swiz_val) {
 
 static char *toop(int op_val)
 {
-  char *str;
+  char *str = NULL;
   switch (op_val) {
   case 0: str = "MAD"; break;
   case 1: str = "DP3"; break;
@@ -1509,12 +1578,24 @@ static char *to_texop(int val)
 
 static void dump_program(struct r500_fragment_program *fp)
 {
-  int pc = 0;
+
+  fprintf(stderr, "R500 Fragment Program:\n--------\n");
+
   int n;
   uint32_t inst;
   uint32_t inst0;
   char *str = NULL;
 
+  if (fp->const_nr) {
+    fprintf(stderr, "--------\nConstants:\n");
+    for (n = 0; n < fp->const_nr; n++) {
+      fprintf(stderr, "Constant %d: %f %f\n\t %f %f\n", n,
+        fp->constant[n][0], fp->constant[n][1], fp->constant[n][2],
+        fp->constant[n][3]);
+    }
+    fprintf(stderr, "--------\n");
+  }
+
   for (n = 0; n < fp->inst_end+1; n++) {
     inst0 = inst = fp->inst[n].inst0;
     fprintf(stderr,"%d\t0:CMN_INST   0x%08x:", n, inst);