Mesa (master): nir: add lowering pass for fragcolor -> fragdata

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Jul 8 16:03:10 UTC 2020


Module: Mesa
Branch: master
Commit: 1fd356302590b30524fb190360247a6c45e1d96c
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=1fd356302590b30524fb190360247a6c45e1d96c

Author: Mike Blumenkrantz <michael.blumenkrantz at gmail.com>
Date:   Mon Jul  6 08:56:16 2020 -0400

nir: add lowering pass for fragcolor -> fragdata

this is needed for zink and other drivers which can support fragcolor but
not fragdata and want to correctly handle EXT_multiview_draw_buffers

Reviewed-by: Erik Faye-Lund <erik.faye-lund at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5687>

---

 src/compiler/nir/meson.build           |   1 +
 src/compiler/nir/nir.h                 |   1 +
 src/compiler/nir/nir_lower_fragcolor.c | 109 +++++++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+)

diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index 2ca803563d6..d4203e34ac9 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -135,6 +135,7 @@ files_libnir = files(
   'nir_lower_flatshade.c',
   'nir_lower_flrp.c',
   'nir_lower_fragcoord_wtrans.c',
+  'nir_lower_fragcolor.c',
   'nir_lower_frexp.c',
   'nir_lower_global_vars_to_local.c',
   'nir_lower_gs_intrinsics.c',
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 03058993243..9f2a90831f0 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -4139,6 +4139,7 @@ void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask);
 void nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask);
 bool nir_lower_io_to_vector(nir_shader *shader, nir_variable_mode mask);
 
+bool nir_lower_fragcolor(nir_shader *shader);
 void nir_lower_fragcoord_wtrans(nir_shader *shader);
 void nir_lower_viewport_transform(nir_shader *shader);
 bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
diff --git a/src/compiler/nir/nir_lower_fragcolor.c b/src/compiler/nir/nir_lower_fragcolor.c
new file mode 100644
index 00000000000..08a281d0991
--- /dev/null
+++ b/src/compiler/nir/nir_lower_fragcolor.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright © 2020 Mike Blumenkrantz
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ * 
+ * Authors:
+ *    Mike Blumenkrantz <michael.blumenkrantz at gmail.com>
+ */
+
+#include "nir.h"
+#include "nir_builder.h"
+
+/**
+ * This pass splits gl_FragColor into gl_FragData[0-7] for drivers which handle
+ * the former but not the latter, e.g., zink.
+ */
+
+/*
+ If a fragment shader writes to "gl_FragColor", DrawBuffersIndexedEXT
+ specifies a set of draw buffers into which the color written to
+ "gl_FragColor" is written. If a fragment shader writes to
+ gl_FragData, DrawBuffersIndexedEXT specifies a set of draw buffers
+ into which each of the multiple output colors defined by these
+ variables are separately written. If a fragment shader writes to
+ neither gl_FragColor nor gl_FragData, the values of the fragment
+ colors following shader execution are undefined, and may differ
+ for each fragment color.
+
+ - EXT_multiview_draw_buffers
+
+ */
+
+static bool
+lower_fragcolor_instr(nir_intrinsic_instr *instr, nir_builder *b)
+{
+   nir_variable *out;
+   if (instr->intrinsic != nir_intrinsic_store_deref)
+      return false;
+
+   out = nir_deref_instr_get_variable(nir_src_as_deref(instr->src[0]));
+   if (out->data.location != FRAG_RESULT_COLOR || out->data.mode != nir_var_shader_out)
+      return false;
+   b->cursor = nir_after_instr(&instr->instr);
+
+   nir_ssa_def *frag_color = nir_load_var(b, out);
+   ralloc_free(out->name);
+   out->name = ralloc_strdup(out, "gl_FragData[0]");
+
+   /* translate gl_FragColor -> gl_FragData since this is already handled */
+   out->data.location = FRAG_RESULT_DATA0;
+   nir_component_mask_t writemask = nir_intrinsic_write_mask(instr);
+
+   for (unsigned i = 1; i < 8; i++) {
+      char name[16];
+      snprintf(name, sizeof(name), "gl_FragData[%u]", i);
+      nir_variable *out_color = nir_variable_create(b->shader, nir_var_shader_out,
+                                                   glsl_vec4_type(),
+                                                   name);
+      out_color->data.location = FRAG_RESULT_DATA0 + i;
+      out_color->data.driver_location = i;
+      out_color->data.index = out->data.index;
+      nir_store_var(b, out_color, frag_color, writemask);
+   }
+   return true;
+}
+
+bool
+nir_lower_fragcolor(nir_shader *shader)
+{
+   bool progress = false;
+
+   if (shader->info.stage != MESA_SHADER_FRAGMENT)
+      return false;
+
+   nir_foreach_function(function, shader) {
+      if (function->impl) {
+         nir_builder builder;
+         nir_builder_init(&builder, function->impl);
+         nir_foreach_block(block, function->impl) {
+            nir_foreach_instr_safe(instr, block) {
+               if (instr->type == nir_instr_type_intrinsic)
+                  progress |= lower_fragcolor_instr(nir_instr_as_intrinsic(instr),
+                                                    &builder);
+            }
+         }
+
+         nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
+      }
+   }
+
+   return progress;
+}



More information about the mesa-commit mailing list