From 17fd191eed53d0b2e599dabe38ce8e503161853c Mon Sep 17 00:00:00 2001 From: Iago Toral Quiroga Date: Tue, 21 Jul 2020 12:21:39 +0200 Subject: [PATCH] nir/lower_clip: make the pass compatible with Vulkan semantics MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Vulkan allows fragment shaders to read gl_ClipDistance[], in which case the SPIR-V compiler inserts a single compact array variable for VARYING_SLOW_CLIP_DIST0 and the lowering should not try to inject its own variables, but instead work in terms of the existing one. Vulkan drivers are expected to call this with use_clipdist_array set to true to be consistent with this setup. Reviewed-by: Alejandro Piñeiro Part-of: --- src/compiler/nir/nir_lower_clip.c | 56 ++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/src/compiler/nir/nir_lower_clip.c b/src/compiler/nir/nir_lower_clip.c index 9fdb56ed610..e0679c65240 100644 --- a/src/compiler/nir/nir_lower_clip.c +++ b/src/compiler/nir/nir_lower_clip.c @@ -81,7 +81,7 @@ create_clipdist_vars(nir_shader *shader, nir_variable **io_vars, { if (use_clipdist_array) { io_vars[0] = - create_clipdist_var(shader, true, + create_clipdist_var(shader, output, VARYING_SLOT_CLIP_DIST0, util_last_bit(ucp_enables)); } else { @@ -112,13 +112,14 @@ store_clipdist_output(nir_builder *b, nir_variable *out, nir_ssa_def **val) } static void -load_clipdist_input(nir_builder *b, nir_variable *in, nir_ssa_def **val) +load_clipdist_input(nir_builder *b, nir_variable *in, int location_offset, + nir_ssa_def **val) { nir_intrinsic_instr *load; load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input); load->num_components = 4; - nir_intrinsic_set_base(load, in->data.driver_location); + nir_intrinsic_set_base(load, in->data.driver_location + location_offset); load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0)); nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL); nir_builder_instr_insert(b, &load->instr); @@ -419,7 +420,7 @@ nir_lower_clip_gs(nir_shader *shader, unsigned ucp_enables, static void lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables, - nir_variable **in) + nir_variable **in, bool use_clipdist_array) { nir_ssa_def *clipdist[MAX_CLIP_PLANES]; nir_builder b; @@ -427,10 +428,17 @@ lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables, nir_builder_init(&b, impl); b.cursor = nir_before_cf_list(&impl->body); - if (ucp_enables & 0x0f) - load_clipdist_input(&b, in[0], &clipdist[0]); - if (ucp_enables & 0xf0) - load_clipdist_input(&b, in[1], &clipdist[4]); + if (!use_clipdist_array) { + if (ucp_enables & 0x0f) + load_clipdist_input(&b, in[0], 0, &clipdist[0]); + if (ucp_enables & 0xf0) + load_clipdist_input(&b, in[1], 0, &clipdist[4]); + } else { + if (ucp_enables & 0x0f) + load_clipdist_input(&b, in[0], 0, &clipdist[0]); + if (ucp_enables & 0xf0) + load_clipdist_input(&b, in[0], 1, &clipdist[4]); + } for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) { if (ucp_enables & (1 << plane)) { @@ -451,6 +459,25 @@ lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables, nir_metadata_preserve(impl, nir_metadata_dominance); } +static bool +fs_has_clip_dist_input_var(nir_shader *shader, nir_variable **io_vars, + unsigned *ucp_enables) +{ + assert(shader->info.stage == MESA_SHADER_FRAGMENT); + nir_foreach_variable(var, &shader->inputs) { + switch (var->data.location) { + case VARYING_SLOT_CLIP_DIST0: + assert(var->data.compact); + io_vars[0] = var; + *ucp_enables &= (1 << glsl_get_length(var->type)) - 1; + return true; + default: + break; + } + } + return false; +} + /* insert conditional kill based on interpolated CLIPDIST */ bool @@ -462,15 +489,18 @@ nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables, if (!ucp_enables) return false; - /* The shader won't normally have CLIPDIST inputs, so we - * must add our own: + /* Fragment shaders can't read gl_ClipDistance[] in OpenGL so it will not + * have the variable defined, but Vulkan allows this, in which case the + * SPIR-V compiler would have already added it as a compact array. */ - /* insert CLIPDIST inputs */ - create_clipdist_vars(shader, in, ucp_enables, false, use_clipdist_array); + if (!fs_has_clip_dist_input_var(shader, in, &ucp_enables)) + create_clipdist_vars(shader, in, ucp_enables, false, use_clipdist_array); + else + assert(use_clipdist_array); nir_foreach_function(function, shader) { if (!strcmp(function->name, "main")) - lower_clip_fs(function->impl, ucp_enables, in); + lower_clip_fs(function->impl, ucp_enables, in, use_clipdist_array); } return true; -- 2.30.2