turnip: implement subpass input attachments
[mesa.git] / src / freedreno / vulkan / tu_shader.c
index 37edf82642baeaa9fb1e87d3d96a99acd1c3b2b2..9a904e61d070a60c711069f9841e39e45c4b9c67 100644 (file)
@@ -38,6 +38,7 @@ tu_spirv_to_nir(struct ir3_compiler *compiler,
 {
    /* TODO these are made-up */
    const struct spirv_to_nir_options spirv_options = {
+      .frag_coord_is_sysval = true,
       .lower_ubo_ssbo_access_to_offsets = true,
       .caps = { false },
    };
@@ -108,7 +109,7 @@ tu_sort_variables_by_location(struct exec_list *variables)
 }
 
 static unsigned
-map_add(struct tu_descriptor_map *map, int set, int binding)
+map_add(struct tu_descriptor_map *map, int set, int binding, int value)
 {
    unsigned index;
    for (index = 0; index < map->num; index++) {
@@ -120,50 +121,102 @@ map_add(struct tu_descriptor_map *map, int set, int binding)
 
    map->set[index] = set;
    map->binding[index] = binding;
+   map->value[index] = value;
    map->num = MAX2(map->num, index + 1);
    return index;
 }
 
 static void
-lower_tex_src_to_offset(nir_tex_instr *instr, unsigned src_idx,
-                        struct tu_shader *shader, bool is_sampler)
+lower_tex_src_to_offset(nir_builder *b, nir_tex_instr *instr, unsigned src_idx,
+                        struct tu_shader *shader)
 {
-   nir_deref_instr *deref =
-      nir_instr_as_deref(instr->src[src_idx].src.ssa->parent_instr);
+   nir_ssa_def *index = NULL;
+   unsigned base_index = 0;
+   unsigned array_elements = 1;
+   nir_tex_src *src = &instr->src[src_idx];
+   bool is_sampler = src->src_type == nir_tex_src_sampler_deref;
+
+   /* We compute first the offsets */
+   nir_deref_instr *deref = nir_instr_as_deref(src->src.ssa->parent_instr);
+   while (deref->deref_type != nir_deref_type_var) {
+      assert(deref->parent.is_ssa);
+      nir_deref_instr *parent =
+         nir_instr_as_deref(deref->parent.ssa->parent_instr);
+
+      assert(deref->deref_type == nir_deref_type_array);
+
+      if (nir_src_is_const(deref->arr.index) && index == NULL) {
+         /* We're still building a direct index */
+         base_index += nir_src_as_uint(deref->arr.index) * array_elements;
+      } else {
+         if (index == NULL) {
+            /* We used to be direct but not anymore */
+            index = nir_imm_int(b, base_index);
+            base_index = 0;
+         }
 
-   if (deref->deref_type != nir_deref_type_var) {
-      tu_finishme("sampler array");
-      return;
+         index = nir_iadd(b, index,
+                          nir_imul(b, nir_imm_int(b, array_elements),
+                                   nir_ssa_for_src(b, deref->arr.index, 1)));
+      }
+
+      array_elements *= glsl_get_length(parent->type);
+
+      deref = parent;
+   }
+
+   if (index)
+      index = nir_umin(b, index, nir_imm_int(b, array_elements - 1));
+
+   /* We have the offsets, we apply them, rewriting the source or removing
+    * instr if needed
+    */
+   if (index) {
+      nir_instr_rewrite_src(&instr->instr, &src->src,
+                            nir_src_for_ssa(index));
+
+      src->src_type = is_sampler ?
+         nir_tex_src_sampler_offset :
+         nir_tex_src_texture_offset;
+
+      instr->texture_array_size = array_elements;
+   } else {
+      nir_tex_instr_remove_src(instr, src_idx);
    }
 
+   if (array_elements > 1)
+      tu_finishme("texture/sampler array");
+
    if (is_sampler) {
       instr->sampler_index = map_add(&shader->sampler_map,
                                      deref->var->data.descriptor_set,
-                                     deref->var->data.binding);
+                                     deref->var->data.binding,
+                                     0);
+      instr->sampler_index += base_index;
    } else {
       instr->texture_index = map_add(&shader->texture_map,
                                      deref->var->data.descriptor_set,
-                                     deref->var->data.binding);
-      instr->texture_array_size = 1;
+                                     deref->var->data.binding,
+                                     deref->var->data.index);
+      instr->texture_index += base_index;
+      instr->texture_array_size = array_elements;
    }
-
-   nir_tex_instr_remove_src(instr, src_idx);
 }
 
 static bool
-lower_sampler(nir_tex_instr *instr, struct tu_shader *shader)
+lower_sampler(nir_builder *b, nir_tex_instr *instr, struct tu_shader *shader)
 {
    int texture_idx =
       nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
 
    if (texture_idx >= 0)
-      lower_tex_src_to_offset(instr, texture_idx, shader, false);
+      lower_tex_src_to_offset(b, instr, texture_idx, shader);
 
    int sampler_idx =
       nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
 
    if (sampler_idx >= 0)
-      lower_tex_src_to_offset(instr, sampler_idx, shader, true);
+      lower_tex_src_to_offset(b, instr, sampler_idx, shader);
 
    if (texture_idx < 0 && sampler_idx < 0)
       return false;
@@ -175,28 +228,63 @@ static bool
 lower_intrinsic(nir_builder *b, nir_intrinsic_instr *instr,
                 struct tu_shader *shader)
 {
+   /* TODO: remove this when layered rendering is implemented */
+   if (instr->intrinsic == nir_intrinsic_load_layer_id) {
+      nir_ssa_def_rewrite_uses(&instr->dest.ssa,
+                               nir_src_for_ssa(nir_imm_int(b, 0)));
+      nir_instr_remove(&instr->instr);
+      return true;
+   }
+
+   if (instr->intrinsic == nir_intrinsic_load_push_constant) {
+      /* note: ir3 wants load_ubo, not load_uniform */
+      assert(nir_intrinsic_base(instr) == 0);
+
+      nir_intrinsic_instr *load =
+         nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
+      load->num_components = instr->num_components;
+      load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
+      load->src[1] = instr->src[0];
+      nir_ssa_dest_init(&load->instr, &load->dest,
+                        load->num_components, instr->dest.ssa.bit_size,
+                        instr->dest.ssa.name);
+      nir_builder_instr_insert(b, &load->instr);
+      nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
+
+      nir_instr_remove(&instr->instr);
+
+      return true;
+   }
+
    if (instr->intrinsic != nir_intrinsic_vulkan_resource_index)
       return false;
 
    nir_const_value *const_val = nir_src_as_const_value(instr->src[0]);
-   if (!const_val || const_val->u32 != 0) {
+   if (!const_val || const_val->u32 != 0)
       tu_finishme("non-zero vulkan_resource_index array index");
-      return false;
-   }
 
-   if (nir_intrinsic_desc_type(instr) != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) {
-      tu_finishme("non-ubo vulkan_resource_index");
-      return false;
-   }
 
-   unsigned index = map_add(&shader->ubo_map,
-                            nir_intrinsic_desc_set(instr),
-                            nir_intrinsic_binding(instr));
+   unsigned set = nir_intrinsic_desc_set(instr);
+   unsigned binding = nir_intrinsic_binding(instr);
+   unsigned index = 0;
+
+   switch (nir_intrinsic_desc_type(instr)) {
+   case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
+   case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
+      /* skip index 0 which is used for push constants */
+      index = map_add(&shader->ubo_map, set, binding, 0) + 1;
+      break;
+   case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
+   case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
+      index = map_add(&shader->ssbo_map, set, binding, 0);
+      break;
+   default:
+      tu_finishme("unsupported desc_type for vulkan_resource_index");
+      break;
+   }
 
-   b->cursor = nir_before_instr(&instr->instr);
-   /* skip index 0 because ir3 treats it differently */
    nir_ssa_def_rewrite_uses(&instr->dest.ssa,
-                            nir_src_for_ssa(nir_imm_int(b, index + 1)));
+                            nir_src_for_ssa(nir_imm_int(b, index)));
    nir_instr_remove(&instr->instr);
 
    return true;
@@ -211,9 +299,10 @@ lower_impl(nir_function_impl *impl, struct tu_shader *shader)
 
    nir_foreach_block(block, impl) {
       nir_foreach_instr_safe(instr, block) {
+         b.cursor = nir_before_instr(instr);
          switch (instr->type) {
          case nir_instr_type_tex:
-            progress |= lower_sampler(nir_instr_as_tex(instr), shader);
+            progress |= lower_sampler(&b, nir_instr_as_tex(instr), shader);
             break;
          case nir_instr_type_intrinsic:
             progress |= lower_intrinsic(&b, nir_instr_as_intrinsic(instr), shader);
@@ -273,7 +362,40 @@ tu_shader_create(struct tu_device *dev,
       nir_print_shader(nir, stderr);
    }
 
-   /* TODO what needs to happen? */
+   /* multi step inlining procedure */
+   NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_function_temp);
+   NIR_PASS_V(nir, nir_lower_returns);
+   NIR_PASS_V(nir, nir_inline_functions);
+   NIR_PASS_V(nir, nir_opt_deref);
+   foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
+      if (!func->is_entrypoint)
+         exec_node_remove(&func->node);
+   }
+   assert(exec_list_length(&nir->functions) == 1);
+   NIR_PASS_V(nir, nir_lower_constant_initializers, ~nir_var_function_temp);
+
+   /* Split member structs.  We do this before lower_io_to_temporaries so that
+    * it doesn't lower system values to temporaries by accident.
+    */
+   NIR_PASS_V(nir, nir_split_var_copies);
+   NIR_PASS_V(nir, nir_split_per_member_structs);
+
+   NIR_PASS_V(nir, nir_remove_dead_variables,
+              nir_var_shader_in | nir_var_shader_out | nir_var_system_value | nir_var_mem_shared);
+
+   NIR_PASS_V(nir, nir_propagate_invariant);
+
+   NIR_PASS_V(nir, nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir), true, true);
+
+   NIR_PASS_V(nir, nir_lower_global_vars_to_local);
+   NIR_PASS_V(nir, nir_split_var_copies);
+   NIR_PASS_V(nir, nir_lower_var_copies);
+
+   NIR_PASS_V(nir, nir_opt_copy_prop_vars);
+   NIR_PASS_V(nir, nir_opt_combine_stores, nir_var_all);
+
+   /* ir3 doesn't support indirect input/output */
+   NIR_PASS_V(nir, nir_lower_indirect_derefs, nir_var_shader_in | nir_var_shader_out);
 
    switch (stage) {
    case MESA_SHADER_VERTEX:
@@ -295,22 +417,36 @@ tu_shader_create(struct tu_device *dev,
       break;
    }
 
-   nir_assign_var_locations(&nir->inputs, &nir->num_inputs,
-                            ir3_glsl_type_size);
-   nir_assign_var_locations(&nir->outputs, &nir->num_outputs,
-                            ir3_glsl_type_size);
-   nir_assign_var_locations(&nir->uniforms, &nir->num_uniforms,
-                            ir3_glsl_type_size);
+   nir_assign_io_var_locations(&nir->inputs, &nir->num_inputs, stage);
+   nir_assign_io_var_locations(&nir->outputs, &nir->num_outputs, stage);
 
    NIR_PASS_V(nir, nir_lower_system_values);
    NIR_PASS_V(nir, nir_lower_frexp);
 
+   if (stage == MESA_SHADER_FRAGMENT)
+      NIR_PASS_V(nir, nir_lower_input_attachments, true);
+
    NIR_PASS_V(nir, tu_lower_io, shader);
 
    NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size, 0);
 
+   if (stage == MESA_SHADER_FRAGMENT) {
+      /* NOTE: lower load_barycentric_at_sample first, since it
+       * produces load_barycentric_at_offset:
+       */
+      NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_sample);
+      NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_offset);
+
+      NIR_PASS_V(nir, ir3_nir_move_varying_inputs);
+   }
+
+   NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
+
    nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
 
+   /* num_uniforms only used by ir3 for size of ubo 0 (push constants) */
+   nir->num_uniforms = MAX_PUSH_CONSTANTS_SIZE / 16;
+
    shader->ir3_shader.compiler = dev->compiler;
    shader->ir3_shader.type = stage;
    shader->ir3_shader.nir = nir;
@@ -349,8 +485,10 @@ tu_shader_compile_options_init(
    *options = (struct tu_shader_compile_options) {
       /* TODO ir3_key */
 
-      .optimize = !(pipeline_info->flags &
-                    VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT),
+      /* TODO: VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT
+       * some optimizations need to happen otherwise shader might not compile
+       */
+      .optimize = true,
       .include_binning_pass = true,
    };
 }