gallium/draw: use a local var to simplify some code
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_wide_point.c
index e1af9e56a24f0b10c41d8d878bfe31458a794745..30116f4925f70de43882d9b937f2db21ae016082 100644 (file)
 /* Authors:  Keith Whitwell <keith@tungstengraphics.com>
  */
 
+/**
+ * Notes on wide points and sprite mode:
+ *
+ * In wide point/sprite mode we effectively need to convert each incoming
+ * vertex into four outgoing vertices specifying the corners of a quad.
+ * Since we don't (yet) have geometry shaders, we have to handle this here
+ * in the draw module.
+ *
+ * For sprites, it also means that this is where we have to handle texcoords
+ * for the vertices of the quad.  OpenGL's GL_COORD_REPLACE state specifies
+ * if/how enabled texcoords are automatically generated for sprites.  We pass
+ * that info through gallium in the pipe_rasterizer_state::sprite_coord_mode
+ * array.
+ *
+ * Additionally, GLSL's gl_PointCoord fragment attribute has to be handled
+ * here as well.  This is basically an additional texture/generic attribute
+ * that varies .x from 0 to 1 horizontally across the point and varies .y
+ * vertically from 0 to 1 down the sprite.
+ *
+ * With geometry shaders, the state tracker could create a GS to do
+ * most/all of this.
+ */
+
+
+#include "pipe/p_context.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 #include "pipe/p_defines.h"
@@ -40,19 +65,18 @@ struct widepoint_stage {
    struct draw_stage stage;
 
    float half_point_size;
-   float point_size_min;
-   float point_size_max;
 
    float xbias;
    float ybias;
 
    uint texcoord_slot[PIPE_MAX_SHADER_OUTPUTS];
-   uint texcoord_mode[PIPE_MAX_SHADER_OUTPUTS];
+   uint texcoord_enable[PIPE_MAX_SHADER_OUTPUTS];
    uint num_texcoords;
+   uint texcoord_mode;
 
    int psize_slot;
 
-   int point_coord_fs_input;  /**< input for pointcoord (and fog) */
+   int point_coord_fs_input;  /**< input for pointcoord */
 };
 
 
@@ -64,8 +88,6 @@ widepoint_stage( struct draw_stage *stage )
 }
 
 
-
-
 /**
  * Set the vertex texcoords for sprite mode.
  * Coords may be left untouched or set to a right-side-up or upside-down
@@ -76,10 +98,10 @@ static void set_texcoords(const struct widepoint_stage *wide,
 {
    uint i;
    for (i = 0; i < wide->num_texcoords; i++) {
-      if (wide->texcoord_mode[i] != PIPE_SPRITE_COORD_NONE) {
+      if (wide->texcoord_enable[i]) {
          uint j = wide->texcoord_slot[i];
          v->data[j][0] = tc[0];
-         if (wide->texcoord_mode[i] == PIPE_SPRITE_COORD_LOWER_LEFT)
+         if (wide->texcoord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
             v->data[j][1] = 1.0f - tc[1];
          else
             v->data[j][1] = tc[1];
@@ -89,10 +111,15 @@ static void set_texcoords(const struct widepoint_stage *wide,
    }
 
    if (wide->point_coord_fs_input >= 0) {
-      /* put gl_PointCoord into extra vertex output's zw components */
-      uint k = wide->stage.draw->extra_vp_outputs.slot;
-      v->data[k][2] = tc[0];
-      v->data[k][3] = tc[1];
+      /* put gl_PointCoord into the extra vertex slot */
+      uint slot = wide->stage.draw->extra_shader_outputs.slot;
+      v->data[slot][0] = tc[0];
+      if (wide->texcoord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
+         v->data[slot][1] = 1.0f - tc[1];
+      else
+         v->data[slot][1] = tc[1];
+      v->data[slot][2] = 0.0F;
+      v->data[slot][3] = 1.0F;
    }
 }
 
@@ -105,9 +132,10 @@ static void set_texcoords(const struct widepoint_stage *wide,
 static void widepoint_point( struct draw_stage *stage,
                              struct prim_header *header )
 {
+   /* XXX should take point_quad_rasterization into account? */
    const struct widepoint_stage *wide = widepoint_stage(stage);
-   const unsigned pos = stage->draw->vs.position_output;
-   const boolean sprite = (boolean) stage->draw->rasterizer->point_sprite;
+   const unsigned pos = draw_current_shader_position_output(stage->draw);
+   const boolean sprite = (boolean) stage->draw->rasterizer->sprite_coord_enable;
    float half_size;
    float left_adj, right_adj, bot_adj, top_adj;
 
@@ -127,13 +155,6 @@ static void widepoint_point( struct draw_stage *stage,
    /* point size is either per-vertex or fixed size */
    if (wide->psize_slot >= 0) {
       half_size = header->v[0]->data[wide->psize_slot][0];
-
-      /* XXX: temporary -- do this in the vertex shader??
-       */
-      half_size = CLAMP(half_size,
-                        wide->point_size_min,
-                        wide->point_size_max);
-      
       half_size *= 0.5f; 
    }
    else {
@@ -181,59 +202,77 @@ static void widepoint_point( struct draw_stage *stage,
 }
 
 
+static int
+find_pntc_input_attrib(struct draw_context *draw)
+{
+   /* Scan the fragment program's input decls to find the pointcoord
+    * attribute.  The xy components will store the point coord.
+    */
+   return 0; /* XXX fix this */
+}
+
+
 static void widepoint_first_point( struct draw_stage *stage, 
                              struct prim_header *header )
 {
    struct widepoint_stage *wide = widepoint_stage(stage);
    struct draw_context *draw = stage->draw;
+   struct pipe_context *pipe = draw->pipe;
+   const struct pipe_rasterizer_state *rast = draw->rasterizer;
+   void *r;
 
-   wide->half_point_size = 0.5f * draw->rasterizer->point_size;
-   wide->point_size_min = draw->rasterizer->point_size_min;
-   wide->point_size_max = draw->rasterizer->point_size_max;
+   wide->half_point_size = 0.5f * rast->point_size;
    wide->xbias = 0.0;
    wide->ybias = 0.0;
 
-   if (draw->rasterizer->gl_rasterization_rules) {
+   if (rast->gl_rasterization_rules) {
       wide->xbias = 0.125;
    }
 
+   /* Disable triangle culling, stippling, unfilled mode etc. */
+   r = draw_get_rasterizer_no_cull(draw, rast->scissor, rast->flatshade);
+   draw->suspend_flushing = TRUE;
+   pipe->bind_rasterizer_state(pipe, r);
+   draw->suspend_flushing = FALSE;
+
    /* XXX we won't know the real size if it's computed by the vertex shader! */
-   if ((draw->rasterizer->point_size > draw->pipeline.wide_point_threshold) ||
-       (draw->rasterizer->point_sprite && draw->pipeline.point_sprite)) {
+   if ((rast->point_size > draw->pipeline.wide_point_threshold) ||
+       (rast->sprite_coord_enable && draw->pipeline.point_sprite)) {
       stage->point = widepoint_point;
    }
    else {
       stage->point = draw_pipe_passthrough_point;
    }
 
-   if (draw->rasterizer->point_sprite) {
+   if (rast->sprite_coord_enable) {
       /* find vertex shader texcoord outputs */
       const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
       uint i, j = 0;
+      wide->texcoord_mode = rast->sprite_coord_mode;
       for (i = 0; i < vs->info.num_outputs; i++) {
          if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
             wide->texcoord_slot[j] = i;
-            wide->texcoord_mode[j] = draw->rasterizer->sprite_coord_mode[j];
+            wide->texcoord_enable[j] = (rast->sprite_coord_enable >> j) & 1;
             j++;
          }
       }
       wide->num_texcoords = j;
 
-      /* find fragment shader PointCoord/Fog input */
-      wide->point_coord_fs_input = 0; /* XXX fix this! */
+      /* find fragment shader PointCoord input */
+      wide->point_coord_fs_input = find_pntc_input_attrib(draw);
 
       /* setup extra vp output (point coord implemented as a texcoord) */
-      draw->extra_vp_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
-      draw->extra_vp_outputs.semantic_index = 0;
-      draw->extra_vp_outputs.slot = draw->vs.num_vs_outputs;
+      draw->extra_shader_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
+      draw->extra_shader_outputs.semantic_index = 0;
+      draw->extra_shader_outputs.slot = draw_current_shader_outputs(draw);
    }
    else {
       wide->point_coord_fs_input = -1;
-      draw->extra_vp_outputs.slot = 0;
+      draw->extra_shader_outputs.slot = 0;
    }
 
    wide->psize_slot = -1;
-   if (draw->rasterizer->point_size_per_vertex) {
+   if (rast->point_size_per_vertex) {
       /* find PSIZ vertex output */
       const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
       uint i;
@@ -251,8 +290,19 @@ static void widepoint_first_point( struct draw_stage *stage,
 
 static void widepoint_flush( struct draw_stage *stage, unsigned flags )
 {
+   struct draw_context *draw = stage->draw;
+   struct pipe_context *pipe = draw->pipe;
+
    stage->point = widepoint_first_point;
    stage->next->flush( stage->next, flags );
+   stage->draw->extra_shader_outputs.slot = 0;
+
+   /* restore original rasterizer state */
+   if (draw->rast_handle) {
+      draw->suspend_flushing = TRUE;
+      pipe->bind_rasterizer_state(pipe, draw->rast_handle);
+      draw->suspend_flushing = FALSE;
+   }
 }
 
 
@@ -279,6 +329,7 @@ struct draw_stage *draw_wide_point_stage( struct draw_context *draw )
       goto fail;
 
    wide->stage.draw = draw;
+   wide->stage.name = "wide-point";
    wide->stage.next = NULL;
    wide->stage.point = widepoint_first_point;
    wide->stage.line = draw_pipe_passthrough_line;