<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Nov 14, 2016 at 5:41 PM, Kenneth Graunke <span dir="ltr"><<a href="mailto:kenneth@whitecape.org" target="_blank">kenneth@whitecape.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
---<br>
 src/compiler/Makefile.sources                      |   1 +<br>
 src/compiler/nir/nir.h                             |   1 +<br>
 .../nir/nir_lower_clip_cull_<wbr>distance_arrays.c      | 187 +++++++++++++++++++++<br>
 3 files changed, 189 insertions(+)<br>
 create mode 100644 src/compiler/nir/nir_lower_<wbr>clip_cull_distance_arrays.c<br>
<br>
diff --git a/src/compiler/Makefile.<wbr>sources b/src/compiler/Makefile.<wbr>sources<br>
index 08d93e0..6c331b0 100644<br>
--- a/src/compiler/Makefile.<wbr>sources<br>
+++ b/src/compiler/Makefile.<wbr>sources<br>
@@ -202,6 +202,7 @@ NIR_FILES = \<br>
        nir/nir_lower_bitmap.c \<br>
        nir/nir_lower_clamp_color_<wbr>outputs.c \<br>
        nir/nir_lower_clip.c \<br>
+       nir/nir_lower_clip_cull_<wbr>distance_arrays.c \<br>
        nir/nir_lower_double_ops.c \<br>
        nir/nir_lower_double_packing.c \<br>
        nir/nir_lower_drawpixels.c \<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 0b78242..f3aadb9 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2441,6 +2441,7 @@ bool nir_lower_idiv(nir_shader *shader);<br>
<br>
 void nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables);<br>
 void nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables);<br>
+void nir_lower_clip_cull_distance_<wbr>arrays(nir_shader *nir);<br>
<br>
 void nir_lower_two_sided_color(nir_<wbr>shader *shader);<br>
<br>
diff --git a/src/compiler/nir/nir_lower_<wbr>clip_cull_distance_arrays.c b/src/compiler/nir/nir_lower_<wbr>clip_cull_distance_arrays.c<br>
new file mode 100644<br>
index 0000000..9f8a7de<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_lower_<wbr>clip_cull_distance_arrays.c<br>
@@ -0,0 +1,187 @@<br>
+/*<br>
+ * Copyright © 2015 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "nir.h"<br>
+#include "nir_builder.h"<br>
+<br>
+/**<br>
+ * @file<br>
+ *<br>
+ * This pass combines separate clip and cull distance arrays into a<br>
+ * single array that contains both.  Clip distances come first, then<br>
+ * cull distances.  It also populates nir_shader_info with the size<br>
+ * of the original arrays so the driver knows which are which.<br>
+ */<br>
+<br>
+/**<br>
+ * Get the length of the clip/cull distance array, looking past<br>
+ * any interface block arrays.<br>
+ */<br>
+static unsigned<br>
+get_unwrapped_array_length(<wbr>nir_variable *var)<br>
+{<br>
+   if (!var)<br>
+      return 0;<br>
+<br>
+   /* Unwrap GS input and TCS input/output interfaces.  We want the<br>
+    * underlying clip/cull distance array length, not the per-vertex<br>
+    * array length.<br>
+    */<br></blockquote><div><br></div><div>Probably not needed, but we could put some stage-based asserts in here.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+   const struct glsl_type *type = var->type;<br>
+   if (glsl_type_is_array_of_arrays(<wbr>type))<br>
+      type = glsl_get_array_element(type);<br>
+<br>
+   return glsl_get_length(type);<br>
+}<br>
+<br>
+/**<br>
+ * Get the type of the combined array (including interface block nesting).<br>
+ */<br>
+static const struct glsl_type *<br>
+get_new_array_type(const struct glsl_type *old_type, unsigned length)<br>
+{<br>
+   const struct glsl_type *type = glsl_array_type(glsl_float_<wbr>type(), length);<br>
+<br>
+   if (glsl_type_is_array_of_arrays(<wbr>old_type))<br>
+      type = glsl_array_type(type, glsl_get_length(type));<br>
+<br>
+   return type;<br>
+}<br>
+<br>
+/**<br>
+ * Rewrite any clip/cull distances to refer to the new combined array.<br>
+ */<br>
+static void<br>
+rewrite_references(nir_instr *instr,<br>
+                   nir_variable *combined,<br>
+                   unsigned cull_offset)<br>
+{<br>
+   if (instr->type != nir_instr_type_intrinsic)<br>
+      return;<br>
+<br>
+   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
+<br>
+   /* copy_var needs to be lowered to load/store before calling this pass */<br>
+   assert(intrin->intrinsic != nir_intrinsic_copy_var);<br></blockquote><div><br></div><div>Wouldn't be that hard to make it support it, but sure.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+   if (intrin->intrinsic != nir_intrinsic_load_var &&<br>
+       intrin->intrinsic != nir_intrinsic_store_var)<br>
+      return;<br>
+<br>
+   nir_deref_var *var_ref = intrin->variables[0];<br>
+   if (var_ref->var->data.mode != combined->data.mode)<br>
+      return;<br>
+<br>
+   if (var_ref->var->data.location != VARYING_SLOT_CLIP_DIST0 &&<br>
+       var_ref->var->data.location != VARYING_SLOT_CULL_DIST0)<br>
+      return;<br>
+<br>
+   /* Update types along the deref chain */<br>
+   const struct glsl_type *type = combined->type;<br>
+   nir_deref *deref = &var_ref->deref;<br>
+   while (deref) {<br>
+      deref->type = type;<br>
+      deref = deref->child;<br>
+      type = glsl_get_array_element(type);<br>
+   }<br>
+<br>
+   /* For cull distances, add an offset to the array index */<br>
+   if (var_ref->var->data.location == VARYING_SLOT_CULL_DIST0) {<br>
+      nir_deref *tail = nir_deref_tail(&intrin-><wbr>variables[0]->deref);<br>
+      nir_deref_array *array_ref = nir_deref_as_array(tail);<br>
+<br>
+      array_ref->base_offset += cull_offset;<br>
+   }<br>
+<br>
+   /* Point the deref at the combined array */<br>
+   var_ref->var = combined;<br>
+<br>
+   /* There's no need to update writemasks; it's a scalar array. */<br>
+}<br>
+<br>
+static void<br>
+combine_clip_cull(nir_shader *nir,<br>
+                  struct exec_list *vars,<br>
+                  bool store_info)<br>
+{<br>
+   nir_variable *cull = NULL;<br>
+   nir_variable *clip = NULL;<br>
+<br>
+   nir_foreach_variable(var, vars) {<br>
+      if (var->data.location == VARYING_SLOT_CLIP_DIST0)<br>
+         clip = var;<br>
+<br>
+      if (var->data.location == VARYING_SLOT_CULL_DIST0)<br>
+         cull = var;<br>
+   }<br>
+<br>
+   const unsigned clip_array_size = get_unwrapped_array_length(<wbr>clip);<br>
+   const unsigned cull_array_size = get_unwrapped_array_length(<wbr>cull);<br>
+<br>
+   if (store_info) {<br>
+      nir->info->clip_distance_<wbr>array_size = clip_array_size;<br>
+      nir->info->cull_distance_<wbr>array_size = cull_array_size;<br></blockquote><div><br></div><div>Why is this optional?  If the sizes here and in the info ever differ, you're going to have a problem.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+   }<br>
+<br>
+   if (clip)<br>
+      clip->data.compact = true;<br>
+<br>
+   if (cull)<br>
+      cull->data.compact = true;<br>
+<br>
+   if (cull_array_size > 0) {<br>
+      if (clip_array_size == 0) {<br>
+         /* No clip distances, just change the cull distance location */<br>
+         cull->data.location = VARYING_SLOT_CLIP_DIST0;<br>
+      } else {<br>
+         /* Turn the ClipDistance array into a combined one */<br>
+         clip->type = get_new_array_type(clip->type,<br>
+                                         clip_array_size + cull_array_size);<br>
+<br>
+         /* Rewrite CullDistance to reference the combined array */<br>
+         nir_foreach_function(function, nir) {<br>
+            if (function->impl) {<br>
+               nir_foreach_block(block, function->impl) {<br>
+                  nir_foreach_instr(instr, block) {<br>
+                     rewrite_references(instr, clip, clip_array_size);<br>
+                  }<br>
+               }<br>
+            }<br>
+         }<br>
+<br>
+         /* Delete the old CullDistance variable */<br>
+         exec_node_remove(&cull->node);<br>
+         ralloc_free(cull);<br>
+      }<br>
+   }<br>
+}<br>
+<br>
+void<br>
+nir_lower_clip_cull_distance_<wbr>arrays(nir_shader *nir)<br>
+{<br>
+   if (nir->stage <= MESA_SHADER_GEOMETRY)<br>
+      combine_clip_cull(nir, &nir->outputs, &nir->info);<br>
+<br>
+   if (nir->stage > MESA_SHADER_VERTEX)<br>
+      combine_clip_cull(nir, &nir->inputs, NULL);<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.10.2<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>