[Mesa-dev] [PATCH 01/14] st/glsl_to_nir: add st_nir_assign_var_locations() helper

Timothy Arceri tarceri at itsqueeze.com
Tue Nov 21 03:37:22 UTC 2017


This avoids packed varyings being assigned different driver locations.
---
 src/mesa/state_tracker/st_glsl_to_nir.cpp | 43 ++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp
index dd4b05d7b9..d0375dd3aa 100644
--- a/src/mesa/state_tracker/st_glsl_to_nir.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp
@@ -114,20 +114,48 @@ st_nir_assign_vs_in_locations(struct gl_program *prog, nir_shader *nir)
           * inputs array and expecting to find inputs with a driver_location
           * set.
           */
          exec_node_remove(&var->node);
          var->data.mode = nir_var_global;
          exec_list_push_tail(&nir->globals, &var->node);
       }
    }
 }
 
+static void
+st_nir_assign_var_locations(struct exec_list *var_list, unsigned *size)
+{
+   unsigned location = 0;
+   unsigned assigned_locations[VARYING_SLOT_MAX];
+   uint64_t processed_locs = 0;
+
+   nir_foreach_variable(var, var_list) {
+      /* Because component packing allows varyings to share the same location
+       * we may have already have processed this location.
+       */
+      if (var->data.location >= VARYING_SLOT_VAR0 &&
+          processed_locs & ((uint64_t)1 << var->data.location)) {
+         var->data.driver_location = assigned_locations[var->data.location];
+         *size += type_size(var->type);
+         continue;
+      }
+
+      assigned_locations[var->data.location] = location;
+      var->data.driver_location = location;
+      location += type_size(var->type);
+
+      processed_locs |= ((uint64_t)1 << var->data.location);
+   }
+
+   *size += location;
+}
+
 static int
 st_nir_lookup_parameter_index(const struct gl_program_parameter_list *params,
                               const char *name)
 {
    int loc = _mesa_lookup_parameter_index(params, name);
 
    /* is there a better way to do this?  If we have something like:
     *
     *    struct S {
     *           float f;
@@ -372,33 +400,30 @@ st_finalize_nir(struct st_context *st, struct gl_program *prog,
    NIR_PASS_V(nir, nir_lower_var_copies);
    NIR_PASS_V(nir, nir_lower_io_types);
 
    if (nir->info.stage == MESA_SHADER_VERTEX) {
       /* Needs special handling so drvloc matches the vbo state: */
       st_nir_assign_vs_in_locations(prog, nir);
       /* Re-lower global vars, to deal with any dead VS inputs. */
       NIR_PASS_V(nir, nir_lower_global_vars_to_local);
 
       sort_varyings(&nir->outputs);
-      nir_assign_var_locations(&nir->outputs,
-                               &nir->num_outputs,
-                               type_size);
+      st_nir_assign_var_locations(&nir->outputs,
+                                  &nir->num_outputs);
       st_nir_fixup_varying_slots(st, &nir->outputs);
    } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
       sort_varyings(&nir->inputs);
-      nir_assign_var_locations(&nir->inputs,
-                               &nir->num_inputs,
-                               type_size);
+      st_nir_assign_var_locations(&nir->inputs,
+                                  &nir->num_inputs);
       st_nir_fixup_varying_slots(st, &nir->inputs);
-      nir_assign_var_locations(&nir->outputs,
-                               &nir->num_outputs,
-                               type_size);
+      st_nir_assign_var_locations(&nir->outputs,
+                                  &nir->num_outputs);
    } else if (nir->info.stage == MESA_SHADER_COMPUTE) {
        /* TODO? */
    } else {
       unreachable("invalid shader type for tgsi bypass\n");
    }
 
    NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
          st->ctx->Const.Program[nir->info.stage].MaxAtomicBuffers);
 
    st_nir_assign_uniform_locations(prog, shader_program,
-- 
2.14.3



More information about the mesa-dev mailing list