ir3: Plumb through bindless support
[mesa.git] / src / compiler / nir / nir_gather_info.c
index 44d3c523d09d6f8fe5f0f70cf9bfea0ff6ae4374..e80a65f19fd618dc8333ce476c3f562fde21f600 100644 (file)
  */
 
 #include "nir.h"
+#include "nir_deref.h"
 #include "main/menums.h"
 
+static void
+get_deref_info(nir_shader *shader, nir_variable *var, nir_deref_instr *deref,
+               bool *cross_invocation, bool *indirect)
+{
+   *cross_invocation = false;
+   *indirect = false;
+
+   const bool per_vertex = nir_is_per_vertex_io(var, shader->info.stage);
+
+   nir_deref_path path;
+   nir_deref_path_init(&path, deref, NULL);
+   assert(path.path[0]->deref_type == nir_deref_type_var);
+   nir_deref_instr **p = &path.path[1];
+
+   /* Vertex index is the outermost array index. */
+   if (per_vertex) {
+      assert((*p)->deref_type == nir_deref_type_array);
+      nir_instr *vertex_index_instr = (*p)->arr.index.ssa->parent_instr;
+      *cross_invocation =
+         vertex_index_instr->type != nir_instr_type_intrinsic ||
+         nir_instr_as_intrinsic(vertex_index_instr)->intrinsic !=
+            nir_intrinsic_load_invocation_id;
+      p++;
+   }
+
+   /* We always lower indirect dereferences for "compact" array vars. */
+   if (!path.path[0]->var->data.compact) {
+      /* Non-compact array vars: find out if they are indirect. */
+      for (; *p; p++) {
+         if ((*p)->deref_type == nir_deref_type_array) {
+            *indirect |= !nir_src_is_const((*p)->arr.index);
+         } else if ((*p)->deref_type == nir_deref_type_struct) {
+            /* Struct indices are always constant. */
+         } else {
+            unreachable("Unsupported deref type");
+         }
+      }
+   }
+
+   nir_deref_path_finish(&path);
+}
+
 static void
 set_io_mask(nir_shader *shader, nir_variable *var, int offset, int len,
-            bool is_output_read)
+            nir_deref_instr *deref, bool is_output_read)
 {
    for (int i = 0; i < len; i++) {
       assert(var->data.location != -1);
@@ -48,11 +91,23 @@ set_io_mask(nir_shader *shader, nir_variable *var, int offset, int len,
          bitfield = BITFIELD64_BIT(idx);
       }
 
+      bool cross_invocation;
+      bool indirect;
+      get_deref_info(shader, var, deref, &cross_invocation, &indirect);
+
       if (var->data.mode == nir_var_shader_in) {
-         if (is_patch_generic)
+         if (is_patch_generic) {
             shader->info.patch_inputs_read |= bitfield;
-         else
+            if (indirect)
+               shader->info.patch_inputs_read_indirectly |= bitfield;
+         } else {
             shader->info.inputs_read |= bitfield;
+            if (indirect)
+               shader->info.inputs_read_indirectly |= bitfield;
+         }
+
+         if (cross_invocation && shader->info.stage == MESA_SHADER_TESS_CTRL)
+            shader->info.tess.tcs_cross_invocation_inputs_read |= bitfield;
 
          if (shader->info.stage == MESA_SHADER_FRAGMENT) {
             shader->info.fs.uses_sample_qualifier |= var->data.sample;
@@ -62,16 +117,27 @@ set_io_mask(nir_shader *shader, nir_variable *var, int offset, int len,
          if (is_output_read) {
             if (is_patch_generic) {
                shader->info.patch_outputs_read |= bitfield;
+               if (indirect)
+                  shader->info.patch_outputs_accessed_indirectly |= bitfield;
             } else {
                shader->info.outputs_read |= bitfield;
+               if (indirect)
+                  shader->info.outputs_accessed_indirectly |= bitfield;
             }
+
+            if (cross_invocation && shader->info.stage == MESA_SHADER_TESS_CTRL)
+               shader->info.tess.tcs_cross_invocation_outputs_read |= bitfield;
          } else {
-           if (is_patch_generic) {
-              shader->info.patch_outputs_written |= bitfield;
-           } else if (!var->data.read_only) {
-              shader->info.outputs_written |= bitfield;
-           }
-        }
+            if (is_patch_generic) {
+               shader->info.patch_outputs_written |= bitfield;
+               if (indirect)
+                  shader->info.patch_outputs_accessed_indirectly |= bitfield;
+            } else if (!var->data.read_only) {
+               shader->info.outputs_written |= bitfield;
+               if (indirect)
+                  shader->info.outputs_accessed_indirectly |= bitfield;
+            }
+         }
 
 
          if (var->data.fb_fetch_output)
@@ -85,7 +151,8 @@ set_io_mask(nir_shader *shader, nir_variable *var, int offset, int len,
  * represents a shader input or output.
  */
 static void
-mark_whole_variable(nir_shader *shader, nir_variable *var, bool is_output_read)
+mark_whole_variable(nir_shader *shader, nir_variable *var,
+                    nir_deref_instr *deref, bool is_output_read)
 {
    const struct glsl_type *type = var->type;
 
@@ -94,11 +161,22 @@ mark_whole_variable(nir_shader *shader, nir_variable *var, bool is_output_read)
       type = glsl_get_array_element(type);
    }
 
+   if (var->data.per_view) {
+      /* TODO: Per view and Per Vertex are not currently used together.  When
+       * they start to be used (e.g. when adding Primitive Replication for GS
+       * on Intel), verify that "peeling" the type twice is correct.  This
+       * assert ensures we remember it.
+       */
+      assert(!nir_is_per_vertex_io(var, shader->info.stage));
+      assert(glsl_type_is_array(type));
+      type = glsl_get_array_element(type);
+   }
+
    const unsigned slots =
       var->data.compact ? DIV_ROUND_UP(glsl_get_length(type), 4)
                         : glsl_count_attribute_slots(type, false);
 
-   set_io_mask(shader, var, 0, slots, is_output_read);
+   set_io_mask(shader, var, 0, slots, deref, is_output_read);
 }
 
 static unsigned
@@ -142,6 +220,10 @@ try_mask_partial_io(nir_shader *shader, nir_variable *var,
       type = glsl_get_array_element(type);
    }
 
+   /* Per view variables will be considered as a whole. */
+   if (var->data.per_view)
+      return false;
+
    /* The code below only handles:
     *
     * - Indexing into matrices
@@ -193,7 +275,7 @@ try_mask_partial_io(nir_shader *shader, nir_variable *var,
       return false;
    }
 
-   set_io_mask(shader, var, offset, elem_width, is_output_read);
+   set_io_mask(shader, var, offset, elem_width, deref, is_output_read);
    return true;
 }
 
@@ -228,7 +310,7 @@ gather_intrinsic_info(nir_intrinsic_instr *instr, nir_shader *shader,
             is_output_read = true;
 
          if (!try_mask_partial_io(shader, var, deref, is_output_read))
-            mark_whole_variable(shader, var, is_output_read);
+            mark_whole_variable(shader, var, deref, is_output_read);
 
          /* We need to track which input_reads bits correspond to a
           * dvec3/dvec4 input attribute */
@@ -462,6 +544,7 @@ nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint)
    shader->info.num_textures = 0;
    shader->info.num_images = 0;
    shader->info.last_msaa_image = -1;
+
    nir_foreach_variable(var, &shader->uniforms) {
       /* Bindless textures and images don't use non-bindless slots. */
       if (var->data.bindless)
@@ -483,6 +566,11 @@ nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint)
    shader->info.patch_inputs_read = 0;
    shader->info.patch_outputs_written = 0;
    shader->info.system_values_read = 0;
+   shader->info.inputs_read_indirectly = 0;
+   shader->info.outputs_accessed_indirectly = 0;
+   shader->info.patch_inputs_read_indirectly = 0;
+   shader->info.patch_outputs_accessed_indirectly = 0;
+
    if (shader->info.stage == MESA_SHADER_VERTEX) {
       shader->info.vs.double_inputs = 0;
    }
@@ -492,6 +580,11 @@ nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint)
       shader->info.fs.uses_demote = false;
       shader->info.fs.needs_helper_invocations = false;
    }
+   if (shader->info.stage == MESA_SHADER_TESS_CTRL) {
+      shader->info.tess.tcs_cross_invocation_inputs_read = 0;
+      shader->info.tess.tcs_cross_invocation_outputs_read = 0;
+   }
+
    shader->info.writes_memory = shader->info.has_transform_feedback_varyings;
 
    void *dead_ctx = ralloc_context(NULL);