Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / drivers / softpipe / sp_quad_fs.c
index cf1b1eff75c66918d82ee48016affbf90d492c1f..8c88c192f8fe2523da032ed6387b0d8e71af2fce 100644 (file)
@@ -53,7 +53,6 @@ struct quad_shade_stage
    struct tgsi_sampler samplers[PIPE_MAX_SAMPLERS];
    struct tgsi_exec_machine machine;
    struct tgsi_exec_vector *inputs, *outputs;
-   int colorOutSlot, depthOutSlot;
 };
 
 
@@ -77,7 +76,8 @@ shade_quad(
    struct quad_shade_stage *qss = quad_shade_stage( qs );
    struct softpipe_context *softpipe = qs->softpipe;
    struct tgsi_exec_machine *machine = &qss->machine;
-
+   boolean z_written;
+   
    /* Consts do not require 16 byte alignment. */
    machine->Consts = softpipe->mapped_constants[PIPE_SHADER_FRAGMENT];
 
@@ -88,40 +88,49 @@ shade_quad(
                                    &qss->machine,
                                    quad );
 
-   /* store result color */
-   if (qss->colorOutSlot >= 0) {
-      /* XXX need to handle multiple color outputs someday */
-      assert(qss->stage.softpipe->fs->shader.output_semantic_name[qss->colorOutSlot]
-             == TGSI_SEMANTIC_COLOR);
-      memcpy(
-             quad->outputs.color,
-             &machine->Outputs[qss->colorOutSlot].xyzw[0].f[0],
-             sizeof( quad->outputs.color ) );
-   }
-
-   /*
-    * XXX the following code for updating quad->outputs.depth
-    * isn't really needed if we did early z testing.
-    */
-
-   /* store result Z */
-   if (qss->depthOutSlot >= 0) {
-      /* output[slot] is new Z */
+   /* store outputs */
+   z_written = FALSE;
+   {
+      const ubyte *sem_name = softpipe->fs->info.output_semantic_name;
+      const ubyte *sem_index = softpipe->fs->info.output_semantic_index;
+      const uint n = qss->stage.softpipe->fs->info.num_outputs;
       uint i;
-      for (i = 0; i < 4; i++) {
-         quad->outputs.depth[i] = machine->Outputs[0].xyzw[2].f[i];
+      for (i = 0; i < n; i++) {
+         switch (sem_name[i]) {
+         case TGSI_SEMANTIC_COLOR:
+            {
+               uint cbuf = sem_index[i];
+               memcpy(quad->outputs.color[cbuf],
+                      &machine->Outputs[i].xyzw[0].f[0],
+                      sizeof(quad->outputs.color[0]) );
+            }
+            break;
+         case TGSI_SEMANTIC_POSITION:
+            {
+               uint j;
+               for (j = 0; j < 4; j++) {
+                  quad->outputs.depth[j] = machine->Outputs[0].xyzw[2].f[j];
+               }
+               z_written = TRUE;
+            }
+            break;
+         }
       }
    }
-   else {
-      /* copy input Z (which was interpolated by the executor) to output Z */
-      uint i;
-      for (i = 0; i < 4; i++) {
-         quad->outputs.depth[i] = machine->Inputs[0].xyzw[2].f[i];
-         /* XXX not sure the above line is always correct.  The following
-          * might be better:
-         quad->outputs.depth[i] = machine->QuadPos.xyzw[2].f[i];
-          */
-      }
+
+   if (!z_written) {
+      /* compute Z values now, as in the quad earlyz stage */
+      /* XXX we should really only do this if the earlyz stage is not used */
+      const float fx = (float) quad->x0;
+      const float fy = (float) quad->y0;
+      const float dzdx = quad->posCoef->dadx[2];
+      const float dzdy = quad->posCoef->dady[2];
+      const float z0 = quad->posCoef->a0[2] + dzdx * fx + dzdy * fy;
+
+      quad->outputs.depth[0] = z0;
+      quad->outputs.depth[1] = z0 + dzdx;
+      quad->outputs.depth[2] = z0 + dzdy;
+      quad->outputs.depth[3] = z0 + dzdx + dzdy;
    }
 
    /* shader may cull fragments */
@@ -138,27 +147,14 @@ static void shade_begin(struct quad_stage *qs)
    struct quad_shade_stage *qss = quad_shade_stage(qs);
    struct softpipe_context *softpipe = qs->softpipe;
    unsigned i;
+   unsigned num = MAX2(softpipe->num_textures, softpipe->num_samplers);
 
    /* set TGSI sampler state that varies */
-   for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
+   for (i = 0; i < num; i++) {
       qss->samplers[i].state = softpipe->sampler[i];
-      qss->samplers[i].texture = &softpipe->texture[i]->base;
+      qss->samplers[i].texture = softpipe->texture[i];
    }
 
-   /* find output slots for depth, color */
-   qss->colorOutSlot = -1;
-   qss->depthOutSlot = -1;
-   for (i = 0; i < qss->stage.softpipe->fs->shader.num_outputs; i++) {
-      switch (qss->stage.softpipe->fs->shader.output_semantic_name[i]) {
-      case TGSI_SEMANTIC_POSITION:
-         qss->depthOutSlot = i;
-         break;
-      case TGSI_SEMANTIC_COLOR:
-         qss->colorOutSlot = i;
-         break;
-      }
-   }
-   
    softpipe->fs->prepare( softpipe->fs, 
                          &qss->machine,
                          qss->samplers );
@@ -184,8 +180,8 @@ struct quad_stage *sp_quad_shade_stage( struct softpipe_context *softpipe )
    uint i;
 
    /* allocate storage for program inputs/outputs, aligned to 16 bytes */
-   qss->inputs = MALLOC(PIPE_ATTRIB_MAX * sizeof(*qss->inputs) + 16);
-   qss->outputs = MALLOC(PIPE_ATTRIB_MAX * sizeof(*qss->outputs) + 16);
+   qss->inputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->inputs) + 16);
+   qss->outputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->outputs) + 16);
    qss->machine.Inputs = align16(qss->inputs);
    qss->machine.Outputs = align16(qss->outputs);