nir: Fix lower_two_sided_color when the face is an input
authorIcecream95 <ixn@keemail.me>
Tue, 14 Jul 2020 10:18:21 +0000 (22:18 +1200)
committerMarge Bot <eric+marge@anholt.net>
Fri, 17 Jul 2020 14:50:26 +0000 (14:50 +0000)
Fixes the two-sided-lighting and vertex-program-two-side piglit tests
on Panfrost.

v2: Use an existing variable for gl_FrontFacing if present.

Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Reviewed-by: Rob Clark <robdclark@chromium.org>
Tested-by: Urja Rannikko <urjaman@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5915>

src/compiler/nir/nir_lower_two_sided_color.c

index 1cab733f5d497bcbe61f957cf9e90f6839cbee1e..80cf27f6ffacd6dad981eef776ecb98690681adf 100644 (file)
 typedef struct {
    nir_builder   b;
    nir_shader   *shader;
+   bool face_sysval;
    struct {
       nir_variable *front;        /* COLn */
       nir_variable *back;         /* BFCn */
    } colors[MAX_COLORS];
+   nir_variable *face;
    int colors_count;
 } lower_2side_state;
 
@@ -64,6 +66,29 @@ create_input(nir_shader *shader, gl_varying_slot slot,
    return var;
 }
 
+static nir_variable *
+create_face_input(nir_shader *shader)
+{
+   nir_foreach_variable(var, &shader->inputs) {
+      if (var->data.location == VARYING_SLOT_FACE)
+         return var;
+   }
+
+   nir_variable *var = rzalloc(shader, nir_variable);
+
+   var->data.driver_location = shader->num_inputs++;
+   var->type = glsl_bool_type();
+   var->data.mode = nir_var_shader_in;
+   var->name = "gl_FrontFacing";
+   var->data.index = 0;
+   var->data.location = VARYING_SLOT_FACE;
+   var->data.interpolation = INTERP_MODE_FLAT;
+
+   exec_list_push_tail(&shader->inputs, &var->node);
+
+   return var;
+}
+
 static nir_ssa_def *
 load_input(nir_builder *b, nir_variable *in)
 {
@@ -112,6 +137,9 @@ setup_inputs(lower_2side_state *state)
             state->colors[i].front->data.interpolation);
    }
 
+   if (!state->face_sysval)
+      state->face = create_face_input(state->shader);
+
    return 0;
 }
 
@@ -160,7 +188,12 @@ nir_lower_two_sided_color_block(nir_block *block,
       /* gl_FrontFace is a boolean but the intrinsic constructor creates
        * 32-bit value by default.
        */
-      nir_ssa_def *face = nir_load_front_face(b, 1);
+      nir_ssa_def *face;
+      if (state->face_sysval)
+         face = nir_load_front_face(b, 1);
+      else
+         face = nir_load_var(b, state->face);
+
       nir_ssa_def *front, *back;
       if (intr->intrinsic == nir_intrinsic_load_deref) {
          front = nir_load_var(b, state->colors[idx].front);
@@ -199,6 +232,7 @@ nir_lower_two_sided_color(nir_shader *shader, bool face_sysval)
 {
    lower_2side_state state = {
       .shader = shader,
+      .face_sysval = face_sysval,
    };
 
    if (shader->info.stage != MESA_SHADER_FRAGMENT)