invalid_stream_id(0),
         invalid_stream_id_from_emit_vertex(false),
         end_primitive_found(false),
-        uses_non_zero_stream(false)
+        used_streams(0)
    {
       /* empty */
    }
          return visit_stop;
       }
 
-      if (stream_id != 0)
-         uses_non_zero_stream = true;
+      used_streams |= 1 << stream_id;
 
       return visit_continue;
    }
          return visit_stop;
       }
 
-      if (stream_id != 0)
-         uses_non_zero_stream = true;
+      used_streams |= 1 << stream_id;
 
       return visit_continue;
    }
       return invalid_stream_id;
    }
 
-   bool uses_streams()
+   unsigned active_stream_mask()
    {
-      return uses_non_zero_stream;
+      return used_streams;
    }
 
    bool uses_end_primitive()
    int invalid_stream_id;
    bool invalid_stream_id_from_emit_vertex;
    bool end_primitive_found;
-   bool uses_non_zero_stream;
+   unsigned used_streams;
 };
 
 /* Class that finds array derefs and check if indexes are dynamic. */
                       emit_vertex.error_stream(),
                       ctx->Const.MaxVertexStreams - 1);
       }
-      prog->Geom.UsesStreams = emit_vertex.uses_streams();
+      prog->Geom.ActiveStreamMask = emit_vertex.active_stream_mask();
       prog->Geom.UsesEndPrimitive = emit_vertex.uses_end_primitive();
 
       /* From the ARB_gpu_shader5 spec:
        * Since we can call EmitVertex() and EndPrimitive() when we output
        * primitives other than points, calling EmitStreamVertex(0) or
        * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
-       * does. Currently we only set prog->Geom.UsesStreams to TRUE when
-       * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
+       * does. We can use prog->Geom.ActiveStreamMask to check whether only the
+       * first (zero) stream is active.
        * stream.
        */
-      if (prog->Geom.UsesStreams &&
+      if (prog->Geom.ActiveStreamMask & ~(1 << 0) &&
           sh->Program->info.gs.output_primitive != GL_POINTS) {
          linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
                       "with n>0 requires point output\n");
 
 
    case nir_intrinsic_emit_vertex:
    case nir_intrinsic_emit_vertex_with_counter:
-      if (nir_intrinsic_stream_id(instr) > 0)
-         shader->info.gs.uses_streams = true;
+      shader->info.gs.active_stream_mask |= 1 << nir_intrinsic_stream_id(instr);
 
       break;
 
 
 
    /* Load the vertex count */
    b->cursor = nir_before_instr(&intrin->instr);
+   assert(state->vertex_count_vars[stream] != NULL);
    nir_ssa_def *count = nir_load_var(b, state->vertex_count_vars[stream]);
 
    nir_ssa_def *max_vertices =
    unsigned stream = nir_intrinsic_stream_id(intrin);
 
    b->cursor = nir_before_instr(&intrin->instr);
+   assert(state->vertex_count_vars[stream] != NULL);
    nir_ssa_def *count = nir_load_var(b, state->vertex_count_vars[stream]);
 
    nir_intrinsic_instr *lowered =
 
    /* Create the counter variables */
    b.cursor = nir_before_cf_list(&impl->body);
-   unsigned num_counters = per_stream && shader->info.gs.uses_streams ?
-                           NIR_MAX_XFB_STREAMS : 1;
-   for (unsigned i = 0; i < num_counters; i++) {
-      state.vertex_count_vars[i] =
-         nir_local_variable_create(impl, glsl_uint_type(), "vertex_count");
-      /* initialize to 0 */
-      nir_store_var(&b, state.vertex_count_vars[i], nir_imm_int(&b, 0), 0x1);
+   for (unsigned i = 0; i < NIR_MAX_XFB_STREAMS; i++) {
+      if (per_stream && !(shader->info.gs.active_stream_mask & (1 << i)))
+         continue;
+
+      if (i == 0 || per_stream) {
+         state.vertex_count_vars[i] =
+            nir_local_variable_create(impl, glsl_uint_type(), "vertex_count");
+         /* initialize to 0 */
+         nir_store_var(&b, state.vertex_count_vars[i], nir_imm_int(&b, 0), 0x1);
+      } else {
+         /* If per_stream is false, we only have one counter which we want to use
+          * for all streams.  Duplicate the counter pointer so all streams use the
+          * same counter.
+          */
+         state.vertex_count_vars[i] = state.vertex_count_vars[0];
+      }
    }
-   /* If per_stream is false, we only have one counter which we want to use
-    * for all streams.  Duplicate the counter pointer so all streams use the
-    * same counter.
-    */
-   for (unsigned i = num_counters; i < NIR_MAX_XFB_STREAMS; i++)
-      state.vertex_count_vars[i] = state.vertex_count_vars[0];
 
    nir_foreach_block_safe(block, impl)
       rewrite_intrinsics(block, &state);
 
          /** Whether or not this shader uses EndPrimitive */
          bool uses_end_primitive:1;
 
-         /** Whether or not this shader uses non-zero streams */
-         bool uses_streams:1;
+         /** The streams used in this shaders (max. 4) */
+         uint8_t active_stream_mask:4;
       } gs;
 
       struct {
 
           */
          prog_data->control_data_format = GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID;
 
-         /* We only have to emit control bits if we are using streams */
-         if (shader->info.gs.uses_streams)
+         /* We only have to emit control bits if we are using non-zero streams */
+         if (shader->info.gs.active_stream_mask != (1 << 0))
             c.control_data_bits_per_vertex = 2;
          else
             c.control_data_bits_per_vertex = 0;
 
       GLint VerticesIn;
 
       bool UsesEndPrimitive;
-      bool UsesStreams;
+      unsigned ActiveStreamMask;
    } Geom;
 
    /**
 
    case MESA_SHADER_GEOMETRY: {
       dst->info.gs.vertices_in = src->Geom.VerticesIn;
       dst->info.gs.uses_end_primitive = src->Geom.UsesEndPrimitive;
-      dst->info.gs.uses_streams = src->Geom.UsesStreams;
+      dst->info.gs.active_stream_mask = src->Geom.ActiveStreamMask;
       break;
    }
    case MESA_SHADER_FRAGMENT: {
 
    prog->FragDataIndexBindings = string_to_uint_map_ctor();
 
    prog->Geom.UsesEndPrimitive = false;
-   prog->Geom.UsesStreams = false;
+   prog->Geom.ActiveStreamMask = 0;
 
    prog->TransformFeedback.BufferMode = GL_INTERLEAVED_ATTRIBS;