From d243bf10326b4eb5163e4f1ae35ca8692a0f6839 Mon Sep 17 00:00:00 2001 From: Connor Abbott Date: Wed, 1 Jul 2020 17:16:01 +0200 Subject: [PATCH] nir/lower_input_attachments: Support loading layer id as an input Part-of: --- src/amd/vulkan/radv_shader.c | 1 + src/compiler/nir/nir.h | 1 + .../nir/nir_lower_input_attachments.c | 25 +++++++++++++++++-- src/freedreno/vulkan/tu_shader.c | 1 + src/intel/vulkan/anv_pipeline.c | 1 + 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/amd/vulkan/radv_shader.c b/src/amd/vulkan/radv_shader.c index 160ec595ab0..e0e5507e49f 100644 --- a/src/amd/vulkan/radv_shader.c +++ b/src/amd/vulkan/radv_shader.c @@ -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, diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 7a109cb95cb..6cf704c533b 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -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, diff --git a/src/compiler/nir/nir_lower_input_attachments.c b/src/compiler/nir/nir_lower_input_attachments.c index 2e6e56806ee..8533eb4a1f2 100644 --- a/src/compiler/nir/nir_lower_input_attachments.c +++ b/src/compiler/nir/nir_lower_input_attachments.c @@ -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); diff --git a/src/freedreno/vulkan/tu_shader.c b/src/freedreno/vulkan/tu_shader.c index 48b686a7329..3820f8877ee 100644 --- a/src/freedreno/vulkan/tu_shader.c +++ b/src/freedreno/vulkan/tu_shader.c @@ -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, }); } diff --git a/src/intel/vulkan/anv_pipeline.c b/src/intel/vulkan/anv_pipeline.c index cfae90f10a8..cafe5668f1f 100644 --- a/src/intel/vulkan/anv_pipeline.c +++ b/src/intel/vulkan/anv_pipeline.c @@ -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, }); } -- 2.30.2