intel: Add a batch flush between front-buffer downsample and X protocol.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vs.c
index c0a3baedaf22d172cab0c10554df3627ad09c80e..43abd891358a706840fee4580503921f7e59d0ec 100644 (file)
@@ -61,14 +61,6 @@ void
 brw_compute_vue_map(struct brw_context *brw, struct brw_vue_map *vue_map,
                     GLbitfield64 slots_valid, bool userclip_active)
 {
-   const struct intel_context *intel = &brw->intel;
-
-   /* Prior to Gen6, don't assign a slot for VARYING_SLOT_CLIP_VERTEX, since
-    * it is unsupported.
-    */
-   if (intel->gen < 6)
-      slots_valid &= ~VARYING_BIT_CLIP_VERTEX;
-
    vue_map->slots_valid = slots_valid;
    int i;
 
@@ -89,36 +81,19 @@ brw_compute_vue_map(struct brw_context *brw, struct brw_vue_map *vue_map,
    /* VUE header: format depends on chip generation and whether clipping is
     * enabled.
     */
-   switch (intel->gen) {
+   switch (brw->gen) {
    case 4:
+   case 5:
       /* There are 8 dwords in VUE header pre-Ironlake:
        * dword 0-3 is indices, point width, clip flags.
        * dword 4-7 is ndc position
        * dword 8-11 is the first vertex data.
-       */
-      assign_vue_slot(vue_map, VARYING_SLOT_PSIZ);
-      assign_vue_slot(vue_map, BRW_VARYING_SLOT_NDC);
-      assign_vue_slot(vue_map, VARYING_SLOT_POS);
-      break;
-   case 5:
-      /* There are 20 DWs (D0-D19) in VUE header on Ironlake:
-       * dword 0-3 of the header is indices, point width, clip flags.
-       * dword 4-7 is the ndc position
-       * dword 8-11 of the vertex header is the 4D space position
-       * dword 12-19 of the vertex header is the user clip distance.
-       * dword 20-23 is a pad so that the vertex element data is aligned
-       * dword 24-27 is the first vertex data we fill.
        *
-       * Note: future pipeline stages expect 4D space position to be
-       * contiguous with the other varyings, so we make dword 24-27 a
-       * duplicate copy of the 4D space position.
+       * On Ironlake the VUE header is nominally 20 dwords, but the hardware
+       * will accept the same header layout as Gen4 [and should be a bit faster]
        */
       assign_vue_slot(vue_map, VARYING_SLOT_PSIZ);
       assign_vue_slot(vue_map, BRW_VARYING_SLOT_NDC);
-      assign_vue_slot(vue_map, BRW_VARYING_SLOT_POS_DUPLICATE);
-      assign_vue_slot(vue_map, VARYING_SLOT_CLIP_DIST0);
-      assign_vue_slot(vue_map, VARYING_SLOT_CLIP_DIST1);
-      assign_vue_slot(vue_map, BRW_VARYING_SLOT_PAD);
       assign_vue_slot(vue_map, VARYING_SLOT_POS);
       break;
    case 6:
@@ -197,6 +172,7 @@ gl_clip_plane *brw_select_clip_planes(struct gl_context *ctx)
    }
 }
 
+
 bool
 brw_vs_prog_data_compare(const void *in_a, const void *in_b,
                          int aux_size, const void *in_key)
@@ -204,15 +180,16 @@ brw_vs_prog_data_compare(const void *in_a, const void *in_b,
    const struct brw_vs_prog_data *a = in_a;
    const struct brw_vs_prog_data *b = in_b;
 
-   /* Compare all the struct up to the pointers. */
-   if (memcmp(a, b, offsetof(struct brw_vs_prog_data, param)))
-      return false;
-
-   if (memcmp(a->param, b->param, a->nr_params * sizeof(void *)))
+   /* Compare the base vec4 structure. */
+   if (!brw_vec4_prog_data_compare(&a->base, &b->base))
       return false;
 
-   if (memcmp(a->pull_param, b->pull_param, a->nr_pull_params * sizeof(void *)))
+   /* Compare the rest of the struct. */
+   const unsigned offset = sizeof(struct brw_vec4_prog_data);
+   if (memcmp(((char *) &a) + offset, ((char *) &b) + offset,
+              sizeof(struct brw_vs_prog_data) - offset)) {
       return false;
+   }
 
    return true;
 }
@@ -223,7 +200,6 @@ do_vs_prog(struct brw_context *brw,
           struct brw_vertex_program *vp,
           struct brw_vs_prog_key *key)
 {
-   struct intel_context *intel = &brw->intel;
    GLuint program_size;
    const GLuint *program;
    struct brw_vs_compile c;
@@ -258,11 +234,13 @@ do_vs_prog(struct brw_context *brw,
    } else {
       param_count = vp->program.Base.Parameters->NumParameters * 4;
    }
-   /* We also upload clip plane data as uniforms */
-   param_count += MAX_CLIP_PLANES * 4;
+   /* vec4_visitor::setup_uniform_clipplane_values() also uploads user clip
+    * planes as uniforms.
+    */
+   param_count += c.key.base.nr_userclip_plane_consts * 4;
 
-   prog_data.param = rzalloc_array(NULL, const float *, param_count);
-   prog_data.pull_param = rzalloc_array(NULL, const float *, param_count);
+   prog_data.base.param = rzalloc_array(NULL, const float *, param_count);
+   prog_data.base.pull_param = rzalloc_array(NULL, const float *, param_count);
 
    GLbitfield64 outputs_written = vp->program.Base.OutputsWritten;
    prog_data.inputs_read = vp->program.Base.InputsRead;
@@ -272,7 +250,7 @@ do_vs_prog(struct brw_context *brw,
       prog_data.inputs_read |= VERT_BIT_EDGEFLAG;
    }
 
-   if (intel->gen < 6) {
+   if (brw->gen < 6) {
       /* Put dummy slots into the VUE for the SF to put the replaced
        * point sprite coords in.  We shouldn't need these dummy slots,
        * which take up precious URB space, but it would mean that the SF
@@ -283,10 +261,21 @@ do_vs_prog(struct brw_context *brw,
          if (c.key.point_coord_replace & (1 << i))
             outputs_written |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + i);
       }
+
+      /* if back colors are written, allocate slots for front colors too */
+      if (outputs_written & BITFIELD64_BIT(VARYING_SLOT_BFC0))
+         outputs_written |= BITFIELD64_BIT(VARYING_SLOT_COL0);
+      if (outputs_written & BITFIELD64_BIT(VARYING_SLOT_BFC1))
+         outputs_written |= BITFIELD64_BIT(VARYING_SLOT_COL1);
+
+      if (c.key.base.userclip_active) {
+         outputs_written |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0);
+         outputs_written |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1);
+      }
    }
 
-   brw_compute_vue_map(brw, &prog_data.vue_map, outputs_written,
-                       c.key.userclip_active);
+   brw_compute_vue_map(brw, &prog_data.base.vue_map, outputs_written,
+                       c.key.base.userclip_active);
 
    if (0) {
       _mesa_fprint_program_opt(stdout, &c.vp->program.Base, PROG_PRINT_DEBUG,
@@ -301,26 +290,17 @@ do_vs_prog(struct brw_context *brw,
       return false;
    }
 
-   if (prog_data.nr_pull_params)
-      prog_data.num_surfaces = 1;
-   if (c.vp->program.Base.SamplersUsed)
-      prog_data.num_surfaces = SURF_INDEX_VS_TEXTURE(BRW_MAX_TEX_UNIT);
-   if (prog &&
-       prog->_LinkedShaders[MESA_SHADER_VERTEX]->NumUniformBlocks) {
-      prog_data.num_surfaces =
-        SURF_INDEX_VS_UBO(prog->_LinkedShaders[MESA_SHADER_VERTEX]->NumUniformBlocks);
-   }
-
    /* Scratch space is used for register spilling */
    if (c.base.last_scratch) {
       perf_debug("Vertex shader triggered register spilling.  "
                  "Try reducing the number of live vec4 values to "
                  "improve performance.\n");
 
-      prog_data.total_scratch = brw_get_scratch_size(c.base.last_scratch*REG_SIZE);
+      prog_data.base.total_scratch
+         = brw_get_scratch_size(c.base.last_scratch*REG_SIZE);
 
-      brw_get_scratch_bo(intel, &brw->vs.scratch_bo,
-                        prog_data.total_scratch * brw->max_vs_threads);
+      brw_get_scratch_bo(brw, &brw->vs.scratch_bo,
+                        prog_data.base.total_scratch * brw->max_vs_threads);
    }
 
    brw_upload_cache(&brw->cache, BRW_VS_PROG,
@@ -334,7 +314,7 @@ do_vs_prog(struct brw_context *brw,
 }
 
 static bool
-key_debug(struct intel_context *intel, const char *name, int a, int b)
+key_debug(struct brw_context *brw, const char *name, int a, int b)
 {
    if (a != b) {
       perf_debug("  %s %d->%d\n", name, a, b);
@@ -348,7 +328,6 @@ brw_vs_debug_recompile(struct brw_context *brw,
                        struct gl_shader_program *prog,
                        const struct brw_vs_prog_key *key)
 {
-   struct intel_context *intel = &brw->intel;
    struct brw_cache_item *c = NULL;
    const struct brw_vs_prog_key *old_key = NULL;
    bool found = false;
@@ -360,7 +339,7 @@ brw_vs_debug_recompile(struct brw_context *brw,
          if (c->cache_id == BRW_VS_PROG) {
             old_key = c->key;
 
-            if (old_key->program_string_id == key->program_string_id)
+            if (old_key->base.program_string_id == key->base.program_string_id)
                break;
          }
       }
@@ -375,41 +354,55 @@ brw_vs_debug_recompile(struct brw_context *brw,
    }
 
    for (unsigned int i = 0; i < VERT_ATTRIB_MAX; i++) {
-      found |= key_debug(intel, "Vertex attrib w/a flags",
+      found |= key_debug(brw, "Vertex attrib w/a flags",
                          old_key->gl_attrib_wa_flags[i],
                          key->gl_attrib_wa_flags[i]);
    }
 
-   found |= key_debug(intel, "user clip flags",
-                      old_key->userclip_active, key->userclip_active);
+   found |= key_debug(brw, "user clip flags",
+                      old_key->base.userclip_active, key->base.userclip_active);
 
-   found |= key_debug(intel, "user clipping planes as push constants",
-                      old_key->nr_userclip_plane_consts,
-                      key->nr_userclip_plane_consts);
+   found |= key_debug(brw, "user clipping planes as push constants",
+                      old_key->base.nr_userclip_plane_consts,
+                      key->base.nr_userclip_plane_consts);
 
-   found |= key_debug(intel, "clip distance enable",
-                      old_key->uses_clip_distance, key->uses_clip_distance);
-   found |= key_debug(intel, "clip plane enable bitfield",
-                      old_key->userclip_planes_enabled_gen_4_5,
-                      key->userclip_planes_enabled_gen_4_5);
-   found |= key_debug(intel, "copy edgeflag",
+   found |= key_debug(brw, "clip distance enable",
+                      old_key->base.uses_clip_distance, key->base.uses_clip_distance);
+   found |= key_debug(brw, "copy edgeflag",
                       old_key->copy_edgeflag, key->copy_edgeflag);
-   found |= key_debug(intel, "PointCoord replace",
+   found |= key_debug(brw, "PointCoord replace",
                       old_key->point_coord_replace, key->point_coord_replace);
-   found |= key_debug(intel, "vertex color clamping",
-                      old_key->clamp_vertex_color, key->clamp_vertex_color);
+   found |= key_debug(brw, "vertex color clamping",
+                      old_key->base.clamp_vertex_color, key->base.clamp_vertex_color);
 
-   found |= brw_debug_recompile_sampler_key(intel, &old_key->tex, &key->tex);
+   found |= brw_debug_recompile_sampler_key(brw, &old_key->base.tex,
+                                            &key->base.tex);
 
    if (!found) {
       perf_debug("  Something else\n");
    }
 }
 
+
+void
+brw_setup_vec4_key_clip_info(struct brw_context *brw,
+                             struct brw_vec4_prog_key *key,
+                             bool program_uses_clip_distance)
+{
+   struct gl_context *ctx = &brw->ctx;
+
+   key->userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
+   key->uses_clip_distance = program_uses_clip_distance;
+   if (key->userclip_active && !key->uses_clip_distance) {
+      key->nr_userclip_plane_consts
+         = _mesa_logbase2(ctx->Transform.ClipPlanesEnabled) + 1;
+   }
+}
+
+
 static void brw_upload_vs_prog(struct brw_context *brw)
 {
-   struct intel_context *intel = &brw->intel;
-   struct gl_context *ctx = &intel->ctx;
+   struct gl_context *ctx = &brw->ctx;
    struct brw_vs_prog_key key;
    /* BRW_NEW_VERTEX_PROGRAM */
    struct brw_vertex_program *vp = 
@@ -422,32 +415,20 @@ static void brw_upload_vs_prog(struct brw_context *brw)
    /* Just upload the program verbatim for now.  Always send it all
     * the inputs it asks for, whether they are varying or not.
     */
-   key.program_string_id = vp->id;
-   key.userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
-   key.uses_clip_distance = vp->program.UsesClipDistance;
-   if (key.userclip_active && !key.uses_clip_distance) {
-      if (intel->gen < 6) {
-         key.nr_userclip_plane_consts
-            = _mesa_bitcount_64(ctx->Transform.ClipPlanesEnabled);
-         key.userclip_planes_enabled_gen_4_5
-            = ctx->Transform.ClipPlanesEnabled;
-      } else {
-         key.nr_userclip_plane_consts
-            = _mesa_logbase2(ctx->Transform.ClipPlanesEnabled) + 1;
-      }
-   }
+   key.base.program_string_id = vp->id;
+   brw_setup_vec4_key_clip_info(brw, &key.base, vp->program.UsesClipDistance);
 
    /* _NEW_POLYGON */
-   if (intel->gen < 6) {
+   if (brw->gen < 6) {
       key.copy_edgeflag = (ctx->Polygon.FrontMode != GL_FILL ||
                            ctx->Polygon.BackMode != GL_FILL);
    }
 
    /* _NEW_LIGHT | _NEW_BUFFERS */
-   key.clamp_vertex_color = ctx->Light._ClampVertexColor;
+   key.base.clamp_vertex_color = ctx->Light._ClampVertexColor;
 
    /* _NEW_POINT */
-   if (intel->gen < 6 && ctx->Point.PointSprite) {
+   if (brw->gen < 6 && ctx->Point.PointSprite) {
       for (i = 0; i < 8; i++) {
         if (ctx->Point.CoordReplace[i])
            key.point_coord_replace |= (1 << i);
@@ -455,10 +436,11 @@ static void brw_upload_vs_prog(struct brw_context *brw)
    }
 
    /* _NEW_TEXTURE */
-   brw_populate_sampler_prog_key_data(ctx, prog, &key.tex);
+   brw_populate_sampler_prog_key_data(ctx, prog, brw->vs.sampler_count,
+                                      &key.base.tex);
 
    /* BRW_NEW_VERTICES */
-   if (intel->gen < 8 && !intel->is_haswell) {
+   if (brw->gen < 8 && !brw->is_haswell) {
       /* Prior to Haswell, the hardware can't natively support GL_FIXED or
        * 2_10_10_10_REV vertex formats.  Set appropriate workaround flags.
        */
@@ -499,12 +481,18 @@ static void brw_upload_vs_prog(struct brw_context *brw)
                         &brw->vs.prog_offset, &brw->vs.prog_data)) {
       bool success = do_vs_prog(brw, ctx->Shader.CurrentVertexProgram,
                                vp, &key);
-
+      (void) success;
       assert(success);
    }
-   if (memcmp(&brw->vs.prog_data->vue_map, &brw->vue_map_geom_out,
+   if (memcmp(&brw->vs.prog_data->base.vue_map, &brw->vue_map_geom_out,
               sizeof(brw->vue_map_geom_out)) != 0) {
-      brw->vue_map_geom_out = brw->vs.prog_data->vue_map;
+      brw->vue_map_vs = brw->vs.prog_data->base.vue_map;
+      brw->state.dirty.brw |= BRW_NEW_VUE_MAP_VS;
+
+      /* No geometry shader support yet, so the VS VUE map is the VUE map for
+       * the output of the "geometry" portion of the pipeline.
+       */
+      brw->vue_map_geom_out = brw->vue_map_vs;
       brw->state.dirty.brw |= BRW_NEW_VUE_MAP_GEOM_OUT;
    }
 }
@@ -541,17 +529,18 @@ brw_vs_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
 
    memset(&key, 0, sizeof(key));
 
-   key.program_string_id = bvp->id;
-   key.clamp_vertex_color = true;
+   key.base.program_string_id = bvp->id;
+   key.base.clamp_vertex_color = ctx->API == API_OPENGL_COMPAT;
 
-   for (int i = 0; i < MAX_SAMPLERS; i++) {
+   unsigned sampler_count = _mesa_fls(vp->Base.SamplersUsed);
+   for (unsigned i = 0; i < sampler_count; i++) {
       if (vp->Base.ShadowSamplers & (1 << i)) {
          /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
-         key.tex.swizzles[i] =
+         key.base.tex.swizzles[i] =
             MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
       } else {
          /* Color sampler: assume no swizzling. */
-         key.tex.swizzles[i] = SWIZZLE_XYZW;
+         key.base.tex.swizzles[i] = SWIZZLE_XYZW;
       }
    }
 
@@ -563,11 +552,11 @@ brw_vs_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
    return success;
 }
 
+
 void
 brw_vs_prog_data_free(const void *in_prog_data)
 {
    const struct brw_vs_prog_data *prog_data = in_prog_data;
 
-   ralloc_free((void *)prog_data->param);
-   ralloc_free((void *)prog_data->pull_param);
+   brw_vec4_prog_data_free(&prog_data->base);
 }