r5xx: Dump shader constants when dumping program assembly.
[mesa.git] / src / mesa / drivers / dri / r300 / r500_fragprog.c
index 0e85a2d7fb5390fae5fb9fbeb7b2a7c211710ec8..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);
 
@@ -364,6 +378,8 @@ static void emit_mov(struct r500_fragment_program *fp, int counter, struct prog_
        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((swizzle & 0x1ff))
@@ -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,7 +789,7 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
                                        | MAKE_SWIZ_ALPHA_C(R500_SWIZZLE_ZERO);
                                counter++;
                                /* Final instruction */
-                               emit_mov(fp, counter, fpi, get_temp(fp, 0), 1672, dest);
+                               emit_mov(fp, counter, fpi, get_temp(fp, 0), SWIZZLE_NOOP, dest);
                                break;
                        case OPCODE_LRP:
                                /* src0 * src1 + INV(src0) * src2
@@ -1136,6 +1152,65 @@ static GLboolean parse_program(struct r500_fragment_program *fp)
                                /* 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);
+                               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:
                        case OPCODE_TXB:
@@ -1191,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++) {
@@ -1402,9 +1473,6 @@ void r500TranslateFragmentShader(r300ContextPtr r300,
 
 
                r300UpdateStateParameters(fp->ctx, _NEW_PROGRAM);
-       } else {
-               fp->inst_offset = 0;
-               fp->inst_end = fp->cs->nrslots - 1;
        }
 
        update_params(fp);
@@ -1427,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;
@@ -1510,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);