i965: Make the userclip flag for the VUE map come from VS prog data.
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_sf_state.c
index 0f97cea652dfe32cf80f8fa9fd743a04a4324f74..49460b2802d1ebf84f98a9627a92fcfc9b66c05b 100644 (file)
@@ -33,43 +33,62 @@ upload_sbe_state(struct brw_context *brw)
 {
    struct intel_context *intel = &brw->intel;
    struct gl_context *ctx = &intel->ctx;
-   /* CACHE_NEW_VS_PROG */
-   uint32_t num_inputs = brw_count_bits(brw->vs.prog_data->outputs_written);
+   struct brw_vue_map vue_map;
+   uint32_t urb_entry_read_length;
    /* BRW_NEW_FRAGMENT_PROGRAM */
-   uint32_t num_outputs = brw_count_bits(brw->fragment_program->Base.InputsRead);
+   uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
+   /* _NEW_LIGHT */
+   bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
    uint32_t dw1, dw10, dw11;
    int i;
    int attr = 0, input_index = 0;
-   /* _NEW_TRANSFORM */
-   int urb_start = ctx->Transform.ClipPlanesEnabled ? 2 : 1;
-   /* _NEW_LIGHT */
-   int two_side_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
+   int urb_entry_read_offset = 1;
    uint16_t attr_overrides[FRAG_ATTRIB_MAX];
+   /* _NEW_BUFFERS */
+   bool render_to_fbo = ctx->DrawBuffer->Name != 0;
+   uint32_t point_sprite_origin;
+
+   /* CACHE_NEW_VS_PROG */
+   brw_compute_vue_map(&vue_map, intel, brw->vs.prog_data);
+   urb_entry_read_length = (vue_map.num_slots + 1)/2 - urb_entry_read_offset;
+   if (urb_entry_read_length == 0) {
+      /* Setting the URB entry read length to 0 causes undefined behavior, so
+       * if we have no URB data to read, set it to 1.
+       */
+      urb_entry_read_length = 1;
+   }
 
    /* FINISHME: Attribute Swizzle Control Mode? */
    dw1 =
       GEN7_SBE_SWIZZLE_ENABLE |
       num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT |
-      (num_inputs + 1) / 2 << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
-      urb_start << GEN7_SBE_URB_ENTRY_READ_OFFSET_SHIFT;
+      urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
+      urb_entry_read_offset << GEN7_SBE_URB_ENTRY_READ_OFFSET_SHIFT;
 
-   /* _NEW_POINT */
-   if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
-      dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
+   /* _NEW_POINT
+    *
+    * Window coordinates in an FBO are inverted, which means point
+    * sprite origin must be inverted.
+    */
+   if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
+      point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
+   } else {
+      point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
+   }
+   dw1 |= point_sprite_origin;
 
-   dw10 = 0;
 
-   /* _NEW_LIGHT (flat shading) */
+   dw10 = 0;
    dw11 = 0;
-   if (ctx->Light.ShadeModel == GL_FLAT) {
-       dw11 |= ((brw->fragment_program->Base.InputsRead & (FRAG_BIT_COL0 | FRAG_BIT_COL1)) >>
-                ((brw->fragment_program->Base.InputsRead & FRAG_BIT_WPOS) ? 0 : 1));
-   }
 
    /* Create the mapping from the FS inputs we produce to the VS outputs
     * they source from.
     */
    for (; attr < FRAG_ATTRIB_MAX; attr++) {
+      enum glsl_interp_qualifier interp_qualifier =
+         brw->fragment_program->InterpQualifier[attr];
+      bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
+
       if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
         continue;
 
@@ -82,6 +101,12 @@ upload_sbe_state(struct brw_context *brw)
       if (attr == FRAG_ATTRIB_PNTC)
         dw10 |= (1 << input_index);
 
+      /* flat shading */
+      if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
+          (shade_model_flat && is_gl_Color &&
+           interp_qualifier == INTERP_QUALIFIER_NONE))
+         dw11 |= (1 << input_index);
+
       /* The hardware can only do the overrides on 16 overrides at a
        * time, and the other up to 16 have to be lined up so that the
        * input index = the output index.  We'll need to do some
@@ -89,12 +114,14 @@ upload_sbe_state(struct brw_context *brw)
        */
       assert(input_index < 16 || attr == input_index);
 
-      attr_overrides[input_index++] = get_attr_override(brw, attr,
-                                                       two_side_color);
+      /* _NEW_LIGHT | _NEW_PROGRAM */
+      attr_overrides[input_index++] =
+         get_attr_override(&vue_map, urb_entry_read_offset, attr,
+                           ctx->VertexProgram._TwoSideEnabled);
    }
 
-   for (; attr < FRAG_ATTRIB_MAX; attr++)
-      attr_overrides[input_index++] = 0;
+   for (; input_index < FRAG_ATTRIB_MAX; input_index++)
+      attr_overrides[input_index] = 0;
 
    BEGIN_BATCH(14);
    OUT_BATCH(_3DSTATE_SBE << 16 | (14 - 2));
@@ -116,7 +143,7 @@ const struct brw_tracked_state gen7_sbe_state = {
    .dirty = {
       .mesa  = (_NEW_LIGHT |
                _NEW_POINT |
-               _NEW_TRANSFORM),
+               _NEW_PROGRAM),
       .brw   = (BRW_NEW_CONTEXT |
                BRW_NEW_FRAGMENT_PROGRAM),
       .cache = CACHE_NEW_VS_PROG
@@ -134,10 +161,11 @@ upload_sf_state(struct brw_context *brw)
    /* _NEW_BUFFERS */
    bool render_to_fbo = brw->intel.ctx.DrawBuffer->Name != 0;
 
-   dw1 = GEN6_SF_STATISTICS_ENABLE | GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
+   dw1 = GEN6_SF_STATISTICS_ENABLE |
+         GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
 
    /* _NEW_BUFFERS */
-   dw1 |= (gen7_depth_format(brw) << GEN7_SF_DEPTH_BUFFER_SURFACE_FORMAT_SHIFT);
+   dw1 |= (brw_depthbuffer_format(brw) << GEN7_SF_DEPTH_BUFFER_SURFACE_FORMAT_SHIFT);
 
    /* _NEW_POLYGON */
    if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
@@ -228,7 +256,7 @@ upload_sf_state(struct brw_context *brw)
 
    dw3 = 0;
 
-   /* _NEW_POINT */
+   /* _NEW_PROGRAM | _NEW_POINT */
    if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
       dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
 
@@ -262,12 +290,13 @@ upload_sf_state(struct brw_context *brw)
 const struct brw_tracked_state gen7_sf_state = {
    .dirty = {
       .mesa  = (_NEW_LIGHT |
+               _NEW_PROGRAM |
                _NEW_POLYGON |
                _NEW_LINE |
                _NEW_SCISSOR |
                _NEW_BUFFERS |
                _NEW_POINT),
-      .brw   = (BRW_NEW_CONTEXT),
+      .brw   = BRW_NEW_CONTEXT,
       .cache = CACHE_NEW_VS_PROG
    },
    .emit = upload_sf_state,