nir,radv/aco: add and use pass to lower make available/visible barriers
[mesa.git] / src / compiler / nir / nir_lower_clip.c
index 1e4915e881b5958ef2b0e25c3a8179c2516ef86e..e0679c65240220903c62e2d88fcba9e2e2a3c03c 100644 (file)
 
 
 static nir_variable *
-create_clipdist_var(nir_shader *shader, unsigned drvloc,
+create_clipdist_var(nir_shader *shader,
                     bool output, gl_varying_slot slot, unsigned array_size)
 {
    nir_variable *var = rzalloc(shader, nir_variable);
 
-   var->data.driver_location = drvloc;
-   var->data.mode = output ? nir_var_shader_out : nir_var_shader_in;
-   var->name = ralloc_asprintf(var, "clipdist_%d", drvloc);
+   /* TODO use type_size() for num_inputs/outputs */
+   if (output) {
+      var->data.driver_location = shader->num_outputs++;
+      var->data.mode = nir_var_shader_out;
+   } else {
+      var->data.driver_location = shader->num_inputs++;
+      var->data.mode = nir_var_shader_in;
+   }
+   var->name = ralloc_asprintf(var, "clipdist_%d", var->data.driver_location);
    var->data.index = 0;
    var->data.location = slot;
 
@@ -61,33 +67,31 @@ create_clipdist_var(nir_shader *shader, unsigned drvloc,
 
    if (output) {
       exec_list_push_tail(&shader->outputs, &var->node);
-      shader->num_outputs++; /* TODO use type_size() */
    }
    else {
       exec_list_push_tail(&shader->inputs, &var->node);
-      shader->num_inputs++;  /* TODO use type_size() */
    }
    return var;
 }
 
 static void
 create_clipdist_vars(nir_shader *shader, nir_variable **io_vars,
-                     unsigned ucp_enables, int *drvloc, bool output,
+                     unsigned ucp_enables, bool output,
                      bool use_clipdist_array)
 {
    if (use_clipdist_array) {
       io_vars[0] =
-         create_clipdist_var(shader, ++(*drvloc), true,
+         create_clipdist_var(shader, output,
                              VARYING_SLOT_CLIP_DIST0,
                              util_last_bit(ucp_enables));
    } else {
       if (ucp_enables & 0x0f)
          io_vars[0] =
-            create_clipdist_var(shader, ++(*drvloc), output,
+            create_clipdist_var(shader, output,
                                 VARYING_SLOT_CLIP_DIST0, 0);
       if (ucp_enables & 0xf0)
          io_vars[1] =
-            create_clipdist_var(shader, ++(*drvloc), output,
+            create_clipdist_var(shader, output,
                                 VARYING_SLOT_CLIP_DIST1, 0);
    }
 }
@@ -108,13 +112,14 @@ store_clipdist_output(nir_builder *b, nir_variable *out, nir_ssa_def **val)
 }
 
 static void
-load_clipdist_input(nir_builder *b, nir_variable *in, nir_ssa_def **val)
+load_clipdist_input(nir_builder *b, nir_variable *in, int location_offset,
+                    nir_ssa_def **val)
 {
    nir_intrinsic_instr *load;
 
    load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
    load->num_components = 4;
-   nir_intrinsic_set_base(load, in->data.driver_location);
+   nir_intrinsic_set_base(load, in->data.driver_location + location_offset);
    load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
    nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
    nir_builder_instr_insert(b, &load->instr);
@@ -309,7 +314,6 @@ nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables, bool use_vars,
 {
    nir_function_impl *impl = nir_shader_get_entrypoint(shader);
    nir_builder b;
-   int maxloc = -1;
    nir_variable *position = NULL;
    nir_variable *clipvertex = NULL;
    nir_variable *out[2] = { NULL };
@@ -317,17 +321,6 @@ nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables, bool use_vars,
    if (!ucp_enables)
       return false;
 
-   /* find clipvertex/position outputs: */
-   nir_foreach_variable(var, &shader->outputs) {
-      int loc = var->data.driver_location;
-
-      /* keep track of last used driver-location.. we'll be
-       * appending CLIP_DIST0/CLIP_DIST1 after last existing
-       * output:
-       */
-      maxloc = MAX2(maxloc, loc);
-   }
-
    nir_builder_init(&b, impl);
 
    /* NIR should ensure that, even in case of loops/if-else, there
@@ -347,7 +340,7 @@ nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables, bool use_vars,
       return false;
 
    /* insert CLIPDIST outputs */
-   create_clipdist_vars(shader, out, ucp_enables, &maxloc, true,
+   create_clipdist_vars(shader, out, ucp_enables, true,
                         use_clipdist_array);
 
    lower_clip_outputs(&b, position, clipvertex, out, ucp_enables, use_vars,
@@ -394,7 +387,6 @@ nir_lower_clip_gs(nir_shader *shader, unsigned ucp_enables,
 {
    nir_function_impl *impl = nir_shader_get_entrypoint(shader);
    nir_builder b;
-   int maxloc = -1;
    nir_variable *position = NULL;
    nir_variable *clipvertex = NULL;
    nir_variable *out[2] = { NULL };
@@ -407,7 +399,7 @@ nir_lower_clip_gs(nir_shader *shader, unsigned ucp_enables,
       return false;
 
    /* insert CLIPDIST outputs */
-   create_clipdist_vars(shader, out, ucp_enables, &maxloc, true,
+   create_clipdist_vars(shader, out, ucp_enables, true,
                         use_clipdist_array);
 
    nir_builder_init(&b, impl);
@@ -428,7 +420,7 @@ nir_lower_clip_gs(nir_shader *shader, unsigned ucp_enables,
 
 static void
 lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables,
-              nir_variable **in)
+              nir_variable **in, bool use_clipdist_array)
 {
    nir_ssa_def *clipdist[MAX_CLIP_PLANES];
    nir_builder b;
@@ -436,10 +428,17 @@ lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables,
    nir_builder_init(&b, impl);
    b.cursor = nir_before_cf_list(&impl->body);
 
-   if (ucp_enables & 0x0f)
-      load_clipdist_input(&b, in[0], &clipdist[0]);
-   if (ucp_enables & 0xf0)
-      load_clipdist_input(&b, in[1], &clipdist[4]);
+   if (!use_clipdist_array) {
+      if (ucp_enables & 0x0f)
+         load_clipdist_input(&b, in[0], 0, &clipdist[0]);
+      if (ucp_enables & 0xf0)
+         load_clipdist_input(&b, in[1], 0, &clipdist[4]);
+   } else {
+      if (ucp_enables & 0x0f)
+         load_clipdist_input(&b, in[0], 0, &clipdist[0]);
+      if (ucp_enables & 0xf0)
+         load_clipdist_input(&b, in[0], 1, &clipdist[4]);
+   }
 
    for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) {
       if (ucp_enables & (1 << plane)) {
@@ -460,6 +459,25 @@ lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables,
    nir_metadata_preserve(impl, nir_metadata_dominance);
 }
 
+static bool
+fs_has_clip_dist_input_var(nir_shader *shader, nir_variable **io_vars,
+                            unsigned *ucp_enables)
+{
+   assert(shader->info.stage == MESA_SHADER_FRAGMENT);
+   nir_foreach_variable(var, &shader->inputs) {
+      switch (var->data.location) {
+      case VARYING_SLOT_CLIP_DIST0:
+         assert(var->data.compact);
+         io_vars[0] = var;
+         *ucp_enables &= (1 << glsl_get_length(var->type)) - 1;
+         return true;
+      default:
+         break;
+      }
+   }
+   return false;
+}
+
 /* insert conditional kill based on interpolated CLIPDIST
  */
 bool
@@ -467,31 +485,22 @@ nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables,
                   bool use_clipdist_array)
 {
    nir_variable *in[2] = {0};
-   int maxloc = -1;
 
    if (!ucp_enables)
       return false;
 
-   nir_foreach_variable(var, &shader->inputs) {
-      int loc = var->data.driver_location;
-
-      /* keep track of last used driver-location.. we'll be
-       * appending CLIP_DIST0/CLIP_DIST1 after last existing
-       * input:
-       */
-      maxloc = MAX2(maxloc, loc);
-   }
-
-   /* The shader won't normally have CLIPDIST inputs, so we
-    * must add our own:
+   /* Fragment shaders can't read gl_ClipDistance[] in OpenGL so it will not
+    * have the variable defined, but Vulkan allows this, in which case the
+    * SPIR-V compiler would have already added it as a compact array.
     */
-   /* insert CLIPDIST inputs */
-   create_clipdist_vars(shader, in, ucp_enables, &maxloc, false,
-                        use_clipdist_array);
+   if (!fs_has_clip_dist_input_var(shader, in, &ucp_enables))
+      create_clipdist_vars(shader, in, ucp_enables, false, use_clipdist_array);
+   else
+      assert(use_clipdist_array);
 
    nir_foreach_function(function, shader) {
       if (!strcmp(function->name, "main"))
-         lower_clip_fs(function->impl, ucp_enables, in);
+         lower_clip_fs(function->impl, ucp_enables, in, use_clipdist_array);
    }
 
    return true;