gallium/draw: remove always-true conditional
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_clip.c
index 0ac3a240e5ed856f08adcd7d7707d37744b9e6da..e51a9da76551763117d1e934f6a65e6769e98a23 100644 (file)
  */
 
 
-#include "pipe/p_util.h"
+#include "util/u_memory.h"
+#include "util/u_math.h"
+
 #include "pipe/p_shader_tokens.h"
 
-#include "draw_context.h"
 #include "draw_vs.h"
+#include "draw_pipe.h"
 
 
 #ifndef IS_NEGATIVE
@@ -112,14 +114,15 @@ static void interp( const struct clipper *clip,
                    const struct vertex_header *out, 
                    const struct vertex_header *in )
 {
-   const unsigned nr_attrs = clip->stage.draw->num_vs_outputs;
+   const unsigned nr_attrs = draw_current_shader_outputs(clip->stage.draw);
+   const unsigned pos_attr = draw_current_shader_position_output(clip->stage.draw);
    unsigned j;
 
    /* Vertex header.
     */
    {
       dst->clipmask = 0;
-      dst->edgeflag = 0;
+      dst->edgeflag = 0;        /* will get overwritten later */
       dst->pad = 0;
       dst->vertex_id = UNDEFINED_VERTEX_ID;
    }
@@ -138,18 +141,17 @@ static void interp( const struct clipper *clip,
       const float *trans = clip->stage.draw->viewport.translate;
       const float oow = 1.0f / pos[3];
 
-      dst->data[0][0] = pos[0] * oow * scale[0] + trans[0];
-      dst->data[0][1] = pos[1] * oow * scale[1] + trans[1];
-      dst->data[0][2] = pos[2] * oow * scale[2] + trans[2];
-      dst->data[0][3] = oow;
+      dst->data[pos_attr][0] = pos[0] * oow * scale[0] + trans[0];
+      dst->data[pos_attr][1] = pos[1] * oow * scale[1] + trans[1];
+      dst->data[pos_attr][2] = pos[2] * oow * scale[2] + trans[2];
+      dst->data[pos_attr][3] = oow;
    }
 
    /* Other attributes
-    * Note: start at 1 to skip winpos (data[0]) since we just computed
-    * it above.
     */
-   for (j = 1; j < nr_attrs; j++) {
-      interp_attr(dst->data[j], t, in->data[j], out->data[j]);
+   for (j = 0; j < nr_attrs; j++) {
+      if (j != pos_attr)
+         interp_attr(dst->data[j], t, in->data[j], out->data[j]);
    }
 }
 
@@ -162,49 +164,50 @@ static void emit_poly( struct draw_stage *stage,
    struct prim_header header;
    unsigned i;
 
+   const ushort edge_first  = DRAW_PIPE_EDGE_FLAG_2;
+   const ushort edge_middle = DRAW_PIPE_EDGE_FLAG_0;
+   const ushort edge_last   = DRAW_PIPE_EDGE_FLAG_1;
+
    /* later stages may need the determinant, but only the sign matters */
    header.det = origPrim->det;
+   header.flags = DRAW_PIPE_RESET_STIPPLE | edge_first | edge_middle;
+   header.pad = 0;
 
-   for (i = 2; i < n; i++) {
+   for (i = 2; i < n; i++, header.flags = edge_middle) {
       header.v[0] = inlist[i-1];
       header.v[1] = inlist[i];
       header.v[2] = inlist[0]; /* keep in v[2] for flatshading */
-       
-      {
-        unsigned tmp1 = header.v[1]->edgeflag;
-        unsigned tmp2 = header.v[2]->edgeflag;
-
-        if (i != n-1) header.v[1]->edgeflag = 0;
-        if (i != 2)   header.v[2]->edgeflag = 0;
-
-         header.edgeflags = ((header.v[0]->edgeflag << 0) | 
-                             (header.v[1]->edgeflag << 1) | 
-                             (header.v[2]->edgeflag << 2));
-
-         if (0) {
-            const struct draw_vertex_shader *vs = stage->draw->vertex_shader;
-            uint j, k;
-            debug_printf("Clipped tri:\n");
-            for (j = 0; j < 3; j++) {
-               for (k = 0; k < vs->info.num_outputs; k++) {
-                  debug_printf("  Vert %d: Attr %d:  %f %f %f %f\n", j, k,
-                         header.v[j]->data[k][0],
-                         header.v[j]->data[k][1],
-                         header.v[j]->data[k][2],
-                         header.v[j]->data[k][3]);
-               }
+
+      if (i == n-1)
+        header.flags |= edge_last;
+
+      if (0) {
+         const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
+         uint j, k;
+         debug_printf("Clipped tri:\n");
+         for (j = 0; j < 3; j++) {
+            for (k = 0; k < vs->info.num_outputs; k++) {
+               debug_printf("  Vert %d: Attr %d:  %f %f %f %f\n", j, k,
+                            header.v[j]->data[k][0],
+                            header.v[j]->data[k][1],
+                            header.v[j]->data[k][2],
+                            header.v[j]->data[k][3]);
             }
          }
-
-        stage->next->tri( stage->next, &header );
-
-        header.v[1]->edgeflag = tmp1;
-        header.v[2]->edgeflag = tmp2;
       }
+
+      stage->next->tri( stage->next, &header );
    }
 }
 
-
+static INLINE float
+dot4(const float *a, const float *b)
+{
+   return (a[0]*b[0] +
+           a[1]*b[1] +
+           a[2]*b[2] +
+           a[3]*b[3]);
+}
 
 
 /* Clip a triangle against the viewport and user clip planes.
@@ -288,9 +291,7 @@ do_clip_tri( struct draw_stage *stage,
    /* If flat-shading, copy color to new provoking vertex.
     */
    if (clipper->flat && inlist[0] != header->v[2]) {
-      if (1) {
-        inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
-      }
+      inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
 
       copy_colors(stage, inlist[0], header->v[2]);
    }
@@ -424,7 +425,7 @@ clip_init_state( struct draw_stage *stage )
    clipper->flat = stage->draw->rasterizer->flatshade ? TRUE : FALSE;
 
    if (clipper->flat) {
-      const struct draw_vertex_shader *vs = stage->draw->vertex_shader;
+      const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
       uint i;
 
       clipper->num_color_attribs = 0;
@@ -486,10 +487,14 @@ static void clip_destroy( struct draw_stage *stage )
 struct draw_stage *draw_clip_stage( struct draw_context *draw )
 {
    struct clipper *clipper = CALLOC_STRUCT(clipper);
+   if (clipper == NULL)
+      goto fail;
 
-   draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 );
+   if (!draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 ))
+      goto fail;
 
    clipper->stage.draw = draw;
+   clipper->stage.name = "clipper";
    clipper->stage.point = clip_point;
    clipper->stage.line = clip_first_line;
    clipper->stage.tri = clip_first_tri;
@@ -500,4 +505,10 @@ struct draw_stage *draw_clip_stage( struct draw_context *draw )
    clipper->plane = draw->plane;
 
    return &clipper->stage;
+
+ fail:
+   if (clipper)
+      clipper->stage.destroy( &clipper->stage );
+
+   return NULL;
 }