nir/lower_input_attachments: Support loading layer id as an input
authorConnor Abbott <cwabbott0@gmail.com>
Wed, 1 Jul 2020 15:16:01 +0000 (17:16 +0200)
committerMarge Bot <eric+marge@anholt.net>
Wed, 19 Aug 2020 16:36:43 +0000 (16:36 +0000)
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5719>

src/amd/vulkan/radv_shader.c
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_input_attachments.c
src/freedreno/vulkan/tu_shader.c
src/intel/vulkan/anv_pipeline.c

index 160ec595ab0154fab05a7e1eb23244a8adabc115..e0e5507e49fe6d722c47b9b09f7c9188edce4e4a 100644 (file)
@@ -508,6 +508,7 @@ radv_shader_compile_to_nir(struct radv_device *device,
                        NIR_PASS_V(nir, nir_lower_input_attachments,
                                   &(nir_input_attachment_options) {
                                        .use_fragcoord_sysval = true,
+                                       .use_layer_id_sysval = true,
                                   });
 
                NIR_PASS_V(nir, nir_remove_dead_variables,
index 7a109cb95cb080b1c9533dacc2037c06fe3fd820..6cf704c533bc865f7d7dea9bb8637897e744f6ac 100644 (file)
@@ -4460,6 +4460,7 @@ bool nir_lower_idiv(nir_shader *shader, enum nir_lower_idiv_path path);
 
 typedef struct nir_input_attachment_options {
    bool use_fragcoord_sysval;
+   bool use_layer_id_sysval;
 } nir_input_attachment_options;
 
 bool nir_lower_input_attachments(nir_shader *shader,
index 2e6e56806ee6bf82e078a597c88b0315987ed275..8533eb4a1f2fde3f005f19b9716914929de3c368 100644 (file)
@@ -50,6 +50,27 @@ load_frag_coord(const nir_input_attachment_options *options, nir_builder *b)
    return nir_load_var(b, pos);
 }
 
+static nir_ssa_def *
+load_layer_id(const nir_input_attachment_options *options, nir_builder *b)
+{
+   if (options->use_layer_id_sysval)
+      return nir_load_layer_id(b);
+
+   nir_variable *layer_id =
+      nir_find_variable_with_location(b->shader, nir_var_shader_in,
+                                      VARYING_SLOT_LAYER);
+
+   if (layer_id == NULL) {
+      layer_id = nir_variable_create(b->shader, nir_var_shader_in,
+                                     glsl_int_type(), NULL);
+      layer_id->data.location = VARYING_SLOT_LAYER;
+      layer_id->data.interpolation = INTERP_MODE_FLAT;
+      layer_id->data.driver_location = b->shader->num_inputs++;
+   }
+
+   return nir_load_var(b, layer_id);
+}
+
 static bool
 try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
                      const nir_input_attachment_options *options)
@@ -73,7 +94,7 @@ try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
    nir_ssa_def *offset = nir_ssa_for_src(&b, load->src[1], 2);
    nir_ssa_def *pos = nir_iadd(&b, frag_coord, offset);
 
-   nir_ssa_def *layer = nir_load_layer_id(&b);
+   nir_ssa_def *layer = load_layer_id(options, &b);
    nir_ssa_def *coord =
       nir_vec3(&b, nir_channel(&b, pos, 0), nir_channel(&b, pos, 1), layer);
 
@@ -144,7 +165,7 @@ try_lower_input_texop(nir_function_impl *impl, nir_tex_instr *tex,
    nir_ssa_def *frag_coord = load_frag_coord(options, &b);
    frag_coord = nir_f2i32(&b, frag_coord);
 
-   nir_ssa_def *layer = nir_load_layer_id(&b);
+   nir_ssa_def *layer = load_layer_id(options, &b);
    nir_ssa_def *coord = nir_vec3(&b, nir_channel(&b, frag_coord, 0),
                                      nir_channel(&b, frag_coord, 1), layer);
 
index 48b686a732979770d824a5ed4a4046f6b4b8c72e..3820f8877eec4ca605c294d5faaca93394d0970b 100644 (file)
@@ -774,6 +774,7 @@ tu_shader_create(struct tu_device *dev,
       NIR_PASS_V(nir, nir_lower_input_attachments,
                  &(nir_input_attachment_options) {
                      .use_fragcoord_sysval = true,
+                     .use_layer_id_sysval = true,
                  });
    }
 
index cfae90f10a8340c8c93818ead76948805e05ecac..cafe5668f1f45440d7123cc67337889679a55376 100644 (file)
@@ -747,6 +747,7 @@ anv_pipeline_lower_nir(struct anv_pipeline *pipeline,
       NIR_PASS_V(nir, nir_lower_input_attachments,
                  &(nir_input_attachment_options) {
                      .use_fragcoord_sysval = true,
+                     .use_layer_id_sysval = true,
                  });
    }