anv: More carefully dirty state in BindPipeline
[mesa.git] / src / intel / vulkan / anv_nir_lower_multiview.c
index f40e1111492c61452e9ac6e066eb15a19b787b0b..ffe9e7bb9724cbc1c4ef4799ae433c5c902492e1 100644 (file)
@@ -44,7 +44,7 @@ struct lower_multiview_state {
 static nir_ssa_def *
 build_instance_id(struct lower_multiview_state *state)
 {
-   assert(state->builder.shader->stage == MESA_SHADER_VERTEX);
+   assert(state->builder.shader->info.stage == MESA_SHADER_VERTEX);
 
    if (state->instance_id == NULL) {
       nir_builder *b = &state->builder;
@@ -57,7 +57,7 @@ build_instance_id(struct lower_multiview_state *state)
        */
       state->instance_id =
          nir_idiv(b, nir_load_instance_id(b),
-                     nir_imm_int(b, _mesa_bitcount(state->view_mask)));
+                     nir_imm_int(b, util_bitcount(state->view_mask)));
    }
 
    return state->instance_id;
@@ -72,9 +72,10 @@ build_view_index(struct lower_multiview_state *state)
       b->cursor = nir_before_block(nir_start_block(b->impl));
 
       assert(state->view_mask != 0);
-      if (0 && _mesa_bitcount(state->view_mask) == 1) {
+      if (util_bitcount(state->view_mask) == 1) {
+         /* Set the view index directly. */
          state->view_index = nir_imm_int(b, ffs(state->view_mask) - 1);
-      } else if (state->builder.shader->stage == MESA_SHADER_VERTEX) {
+      } else if (state->builder.shader->info.stage == MESA_SHADER_VERTEX) {
          /* We only support 16 viewports */
          assert((state->view_mask & 0xffff0000) == 0);
 
@@ -84,9 +85,9 @@ build_view_index(struct lower_multiview_state *state)
           */
          nir_ssa_def *compacted =
             nir_umod(b, nir_load_instance_id(b),
-                        nir_imm_int(b, _mesa_bitcount(state->view_mask)));
+                        nir_imm_int(b, util_bitcount(state->view_mask)));
 
-         if (0 && util_is_power_of_two(state->view_mask + 1)) {
+         if (util_is_power_of_two_or_zero(state->view_mask + 1)) {
             /* If we have a full view mask, then compacted is what we want */
             state->view_index = compacted;
          } else {
@@ -122,29 +123,22 @@ build_view_index(struct lower_multiview_state *state)
          }
       } else {
          const struct glsl_type *type = glsl_int_type();
-         if (b->shader->stage == MESA_SHADER_TESS_CTRL ||
-             b->shader->stage == MESA_SHADER_GEOMETRY)
-            type = glsl_array_type(type, 1);
+         if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
+             b->shader->info.stage == MESA_SHADER_GEOMETRY)
+            type = glsl_array_type(type, 1, 0);
 
          nir_variable *idx_var =
             nir_variable_create(b->shader, nir_var_shader_in,
                                 type, "view index");
          idx_var->data.location = VARYING_SLOT_VIEW_INDEX;
-         if (b->shader->stage == MESA_SHADER_FRAGMENT)
+         if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
             idx_var->data.interpolation = INTERP_MODE_FLAT;
 
-         if (glsl_type_is_array(type)) {
-            nir_deref_var *deref = nir_deref_var_create(b->shader, idx_var);
-            nir_deref_array *arr = nir_deref_array_create(b->shader);
-            arr->deref.type = glsl_int_type();
-            arr->deref_array_type = nir_deref_array_type_direct;
-            arr->base_offset = 0;
-            deref->deref.child = &arr->deref;
+         nir_deref_instr *deref = nir_build_deref_var(b, idx_var);
+         if (glsl_type_is_array(type))
+            deref = nir_build_deref_array_imm(b, deref, 0);
 
-            state->view_index = nir_load_deref_var(b, deref);
-         } else {
-            state->view_index = nir_load_var(b, idx_var);
-         }
+         state->view_index = nir_load_deref(b, deref);
       }
    }
 
@@ -154,7 +148,7 @@ build_view_index(struct lower_multiview_state *state)
 bool
 anv_nir_lower_multiview(nir_shader *shader, uint32_t view_mask)
 {
-   assert(shader->stage != MESA_SHADER_COMPUTE);
+   assert(shader->info.stage != MESA_SHADER_COMPUTE);
 
    /* If multiview isn't enabled, we have nothing to do. */
    if (view_mask == 0)
@@ -202,7 +196,7 @@ anv_nir_lower_multiview(nir_shader *shader, uint32_t view_mask)
     * available in the VS.  If it's not a fragment shader, we need to pass
     * the view index on to the next stage.
     */
-   if (shader->stage != MESA_SHADER_FRAGMENT) {
+   if (shader->info.stage != MESA_SHADER_FRAGMENT) {
       nir_ssa_def *view_index = build_view_index(&state);
 
       nir_builder *b = &state.builder;
@@ -210,11 +204,15 @@ anv_nir_lower_multiview(nir_shader *shader, uint32_t view_mask)
       assert(view_index->parent_instr->block == nir_start_block(entrypoint));
       b->cursor = nir_after_instr(view_index->parent_instr);
 
-      nir_variable *view_index_out =
-         nir_variable_create(shader, nir_var_shader_out,
-                             glsl_int_type(), "view index");
-      view_index_out->data.location = VARYING_SLOT_VIEW_INDEX;
-      nir_store_var(b, view_index_out, view_index, 0x1);
+      /* Unless there is only one possible view index (that would be set
+       * directly), pass it to the next stage. */
+      if (util_bitcount(state.view_mask) != 1) {
+         nir_variable *view_index_out =
+            nir_variable_create(shader, nir_var_shader_out,
+                                glsl_int_type(), "view index");
+         view_index_out->data.location = VARYING_SLOT_VIEW_INDEX;
+         nir_store_var(b, view_index_out, view_index, 0x1);
+      }
 
       nir_variable *layer_id_out =
          nir_variable_create(shader, nir_var_shader_out,