nir: add missing type to type_size_vec4()
[mesa.git] / src / glsl / nir / nir_lower_io.c
index 6a043fdedd1d0cba22a6ca27fe8d50634e360257..71bfd347c1affd254ce67dd6107ac7da26549e61 100644 (file)
 /*
  * This lowering pass converts references to input/output variables with
  * loads/stores to actual input/output intrinsics.
- *
- * NOTE: This pass really only works for scalar backends at the moment due
- * to the way it packes the input/output data.
  */
 
 #include "nir.h"
 
 struct lower_io_state {
    void *mem_ctx;
+   bool is_scalar;
 };
 
+static int
+type_size_vec4(const struct glsl_type *type)
+{
+   unsigned int i;
+   int size;
+
+   switch (glsl_get_base_type(type)) {
+   case GLSL_TYPE_UINT:
+   case GLSL_TYPE_INT:
+   case GLSL_TYPE_FLOAT:
+   case GLSL_TYPE_BOOL:
+      if (glsl_type_is_matrix(type)) {
+         return glsl_get_matrix_columns(type);
+      } else {
+         return 1;
+      }
+   case GLSL_TYPE_ARRAY:
+      return type_size_vec4(glsl_get_array_element(type)) * glsl_get_length(type);
+   case GLSL_TYPE_STRUCT:
+      size = 0;
+      for (i = 0; i <  glsl_get_length(type); i++) {
+         size += type_size_vec4(glsl_get_struct_field(type, i));
+      }
+      return size;
+   case GLSL_TYPE_SUBROUTINE:
+      return 1;
+   case GLSL_TYPE_SAMPLER:
+      return 0;
+   case GLSL_TYPE_ATOMIC_UINT:
+      return 0;
+   case GLSL_TYPE_IMAGE:
+   case GLSL_TYPE_VOID:
+   case GLSL_TYPE_DOUBLE:
+   case GLSL_TYPE_ERROR:
+   case GLSL_TYPE_INTERFACE:
+      unreachable("not reached");
+   }
+
+   return 0;
+}
+
 static unsigned
-type_size(const struct glsl_type *type)
+type_size_scalar(const struct glsl_type *type)
 {
    unsigned int size, i;
 
@@ -52,13 +91,15 @@ type_size(const struct glsl_type *type)
    case GLSL_TYPE_BOOL:
       return glsl_get_components(type);
    case GLSL_TYPE_ARRAY:
-      return type_size(glsl_get_array_element(type)) * glsl_get_length(type);
+      return type_size_scalar(glsl_get_array_element(type)) * glsl_get_length(type);
    case GLSL_TYPE_STRUCT:
       size = 0;
       for (i = 0; i < glsl_get_length(type); i++) {
-         size += type_size(glsl_get_struct_field(type, i));
+         size += type_size_scalar(glsl_get_struct_field(type, i));
       }
       return size;
+   case GLSL_TYPE_SUBROUTINE:
+      return 1;
    case GLSL_TYPE_SAMPLER:
       return 0;
    case GLSL_TYPE_ATOMIC_UINT:
@@ -69,43 +110,43 @@ type_size(const struct glsl_type *type)
       return 0;
    case GLSL_TYPE_VOID:
    case GLSL_TYPE_ERROR:
+   case GLSL_TYPE_DOUBLE:
       unreachable("not reached");
    }
 
    return 0;
 }
 
-static void
-assign_var_locations(struct hash_table *ht, unsigned *size)
+static unsigned
+type_size(const struct glsl_type *type, bool is_scalar)
 {
-   unsigned location = 0;
+   if (is_scalar)
+      return type_size_scalar(type);
+   else
+      return type_size_vec4(type);
+}
 
-   struct hash_entry *entry;
-   hash_table_foreach(ht, entry) {
-      nir_variable *var = (nir_variable *) entry->data;
+void
+nir_assign_var_locations(struct exec_list *var_list, unsigned *size, bool is_scalar)
+{
+   unsigned location = 0;
 
+   foreach_list_typed(nir_variable, var, node, var_list) {
       /*
        * UBO's have their own address spaces, so don't count them towards the
        * number of global uniforms
        */
-      if (var->data.mode == nir_var_uniform && var->interface_type != NULL)
+      if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
+          var->interface_type != NULL)
          continue;
 
       var->data.driver_location = location;
-      location += type_size(var->type);
+      location += type_size(var->type, is_scalar);
    }
 
    *size = location;
 }
 
-static void
-assign_var_locations_shader(nir_shader *shader)
-{
-   assign_var_locations(shader->inputs, &shader->num_inputs);
-   assign_var_locations(shader->outputs, &shader->num_outputs);
-   assign_var_locations(shader->uniforms, &shader->num_uniforms);
-}
-
 static bool
 deref_has_indirect(nir_deref_var *deref)
 {
@@ -120,6 +161,80 @@ deref_has_indirect(nir_deref_var *deref)
    return false;
 }
 
+static bool
+mark_indirect_uses_block(nir_block *block, void *void_state)
+{
+   struct set *indirect_set = void_state;
+
+   nir_foreach_instr(block, instr) {
+      if (instr->type != nir_instr_type_intrinsic)
+         continue;
+
+      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+
+      for (unsigned i = 0;
+           i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) {
+         if (deref_has_indirect(intrin->variables[i]))
+            _mesa_set_add(indirect_set, intrin->variables[i]->var);
+      }
+   }
+
+   return true;
+}
+
+/* Identical to nir_assign_var_locations_packed except that it assigns
+ * locations to the variables that are used 100% directly first and then
+ * assigns locations to variables that are used indirectly.
+ */
+void
+nir_assign_var_locations_direct_first(nir_shader *shader,
+                                      struct exec_list *var_list,
+                                      unsigned *direct_size,
+                                      unsigned *size,
+                                      bool is_scalar)
+{
+   struct set *indirect_set = _mesa_set_create(NULL, _mesa_hash_pointer,
+                                               _mesa_key_pointer_equal);
+
+   nir_foreach_overload(shader, overload) {
+      if (overload->impl)
+         nir_foreach_block(overload->impl, mark_indirect_uses_block,
+                           indirect_set);
+   }
+
+   unsigned location = 0;
+
+   foreach_list_typed(nir_variable, var, node, var_list) {
+      if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
+          var->interface_type != NULL)
+         continue;
+
+      if (_mesa_set_search(indirect_set, var))
+         continue;
+
+      var->data.driver_location = location;
+      location += type_size(var->type, is_scalar);
+   }
+
+   *direct_size = location;
+
+   foreach_list_typed(nir_variable, var, node, var_list) {
+      if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
+          var->interface_type != NULL)
+         continue;
+
+      if (!_mesa_set_search(indirect_set, var))
+         continue;
+
+      var->data.driver_location = location;
+      location += type_size(var->type, is_scalar);
+   }
+
+   *size = location;
+
+   _mesa_set_destroy(indirect_set, NULL);
+}
+
 static unsigned
 get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
               struct lower_io_state *state)
@@ -134,29 +249,24 @@ get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
 
       if (tail->deref_type == nir_deref_type_array) {
          nir_deref_array *deref_array = nir_deref_as_array(tail);
-         unsigned size = type_size(tail->type);
+         unsigned size = type_size(tail->type, state->is_scalar);
 
          base_offset += size * deref_array->base_offset;
 
          if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
             nir_load_const_instr *load_const =
-               nir_load_const_instr_create(state->mem_ctx);
-            load_const->num_components = 1;
+               nir_load_const_instr_create(state->mem_ctx, 1);
             load_const->value.u[0] = size;
-            load_const->dest.is_ssa = true;
-            nir_ssa_def_init(&load_const->instr, &load_const->dest.ssa,
-                             1, NULL);
             nir_instr_insert_before(instr, &load_const->instr);
 
             nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx,
                                                       nir_op_imul);
             mul->src[0].src.is_ssa = true;
-            mul->src[0].src.ssa = &load_const->dest.ssa;
-            mul->src[1].src = nir_src_copy(deref_array->indirect,
-                                           state->mem_ctx);
+            mul->src[0].src.ssa = &load_const->def;
+            nir_src_copy(&mul->src[1].src, &deref_array->indirect,
+                         state->mem_ctx);
             mul->dest.write_mask = 1;
-            mul->dest.dest.is_ssa = true;
-            nir_ssa_def_init(&mul->instr, &mul->dest.dest.ssa, 1, NULL);
+            nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
             nir_instr_insert_before(instr, &mul->instr);
 
             if (found_indirect) {
@@ -166,8 +276,7 @@ get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
                add->src[1].src.is_ssa = true;
                add->src[1].src.ssa = &mul->dest.dest.ssa;
                add->dest.write_mask = 1;
-               add->dest.dest.is_ssa = true;
-               nir_ssa_def_init(&add->instr, &add->dest.dest.ssa, 1, NULL);
+               nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
                nir_instr_insert_before(instr, &add->instr);
 
                indirect->is_ssa = true;
@@ -182,7 +291,8 @@ get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
          nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
 
          for (unsigned i = 0; i < deref_struct->index; i++)
-            base_offset += type_size(glsl_get_struct_field(parent_type, i));
+            base_offset += type_size(glsl_get_struct_field(parent_type, i),
+                                     state->is_scalar);
       }
    }
 
@@ -208,25 +318,21 @@ nir_lower_io_block(nir_block *block, void *void_state)
 
          bool has_indirect = deref_has_indirect(intrin->variables[0]);
 
+         /* Figure out the opcode */
          nir_intrinsic_op load_op;
          switch (mode) {
          case nir_var_shader_in:
-            if (has_indirect) {
-               load_op = nir_intrinsic_load_input_indirect;
-            } else {
-               load_op = nir_intrinsic_load_input;
-            }
+            load_op = has_indirect ? nir_intrinsic_load_input_indirect :
+                                     nir_intrinsic_load_input;
             break;
          case nir_var_uniform:
-            if (has_indirect) {
-               load_op = nir_intrinsic_load_uniform_indirect;
-            } else {
-               load_op = nir_intrinsic_load_uniform;
-            }
+            load_op = has_indirect ? nir_intrinsic_load_uniform_indirect :
+                                     nir_intrinsic_load_uniform;
             break;
          default:
             unreachable("Unknown variable mode");
          }
+
          nir_intrinsic_instr *load = nir_intrinsic_instr_create(state->mem_ctx,
                                                                 load_op);
          load->num_components = intrin->num_components;
@@ -237,25 +343,18 @@ nir_lower_io_block(nir_block *block, void *void_state)
          offset += intrin->variables[0]->var->data.driver_location;
 
          load->const_index[0] = offset;
-         load->const_index[1] = 1;
 
          if (has_indirect)
             load->src[0] = indirect;
 
          if (intrin->dest.is_ssa) {
-            load->dest.is_ssa = true;
-            nir_ssa_def_init(&load->instr, &load->dest.ssa,
-                             intrin->num_components, NULL);
-
-            nir_src new_src = {
-               .is_ssa = true,
-               .ssa = &load->dest.ssa,
-            };
-
-            nir_ssa_def_rewrite_uses(&intrin->dest.ssa, new_src,
+            nir_ssa_dest_init(&load->instr, &load->dest,
+                              intrin->num_components, NULL);
+            nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
+                                     nir_src_for_ssa(&load->dest.ssa),
                                      state->mem_ctx);
          } else {
-            load->dest = nir_dest_copy(intrin->dest, state->mem_ctx);
+            nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
          }
 
          nir_instr_insert_before(&intrin->instr, &load->instr);
@@ -286,9 +385,8 @@ nir_lower_io_block(nir_block *block, void *void_state)
          offset += intrin->variables[0]->var->data.driver_location;
 
          store->const_index[0] = offset;
-         store->const_index[1] = 1;
 
-         store->src[0] = nir_src_copy(intrin->src[0], state->mem_ctx);
+         nir_src_copy(&store->src[0], &intrin->src[0], state->mem_ctx);
 
          if (has_indirect)
             store->src[1] = indirect;
@@ -307,22 +405,24 @@ nir_lower_io_block(nir_block *block, void *void_state)
 }
 
 static void
-nir_lower_io_impl(nir_function_impl *impl)
+nir_lower_io_impl(nir_function_impl *impl, bool is_scalar)
 {
    struct lower_io_state state;
 
    state.mem_ctx = ralloc_parent(impl);
+   state.is_scalar = is_scalar;
 
    nir_foreach_block(impl, nir_lower_io_block, &state);
+
+   nir_metadata_preserve(impl, nir_metadata_block_index |
+                               nir_metadata_dominance);
 }
 
 void
-nir_lower_io(nir_shader *shader)
+nir_lower_io(nir_shader *shader, bool is_scalar)
 {
-   assign_var_locations_shader(shader);
-
    nir_foreach_overload(shader, overload) {
       if (overload->impl)
-         nir_lower_io_impl(overload->impl);
+         nir_lower_io_impl(overload->impl, is_scalar);
    }
 }