summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baierl <ichgeh@imkreisrum.de>2019-04-26 15:01:43 +0200
committerQiang Yu <yuq825@gmail.com>2019-04-29 02:46:44 +0000
commitb82de2b4d702789620f84c70577aa3e106d6ae4f (patch)
tree59133cbb3891d8a87dadcb45dde3bcb6e28ada6f
parent7050eccd77f6f435ab4c8ab4a31706e42b53817b (diff)
nir: add rcp(w) lowering for gl_FragCoord
On some hardware (e.g. Mali400) the shader needs to apply some transformations for correct gl_FragCoord handling. The lowering actions look like the following in pseudocode: gl_FragCoord.xyz = gl_FragCoord_orig.xyz gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w Add this lowering as a nir pass in preparation for using it in the driver. Signed-off-by: Andreas Baierl <ichgeh@imkreisrum.de> Reviewed-by: Qiang Yu <yuq825@gmail.com> Reviewed-by: Eric Anholt <eric@anholt.net>
-rw-r--r--src/compiler/Makefile.sources1
-rw-r--r--src/compiler/nir/meson.build1
-rw-r--r--src/compiler/nir/nir.h1
-rw-r--r--src/compiler/nir/nir_lower_fragcoord_wtrans.c81
4 files changed, 84 insertions, 0 deletions
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 35dd7ff1abe..a8e5f6280db 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -243,6 +243,7 @@ NIR_FILES = \
nir/nir_lower_constant_initializers.c \
nir/nir_lower_double_ops.c \
nir/nir_lower_drawpixels.c \
+ nir/nir_lower_fragcoord_wtrans.c \
nir/nir_lower_frexp.c \
nir/nir_lower_global_vars_to_local.c \
nir/nir_lower_gs_intrinsics.c \
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index d6e7b2cc167..625f1508850 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -123,6 +123,7 @@ files_libnir = files(
'nir_lower_constant_initializers.c',
'nir_lower_double_ops.c',
'nir_lower_drawpixels.c',
+ 'nir_lower_fragcoord_wtrans.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 e878a63409d..0ba90caad38 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -3159,6 +3159,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);
+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_fragcoord_wtrans.c b/src/compiler/nir/nir_lower_fragcoord_wtrans.c
new file mode 100644
index 00000000000..b8c0b0b24e5
--- /dev/null
+++ b/src/compiler/nir/nir_lower_fragcoord_wtrans.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2019 Andreas Baierl
+ *
+ * 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.
+ */
+
+#include "nir.h"
+#include "nir_builder.h"
+
+/* Lower gl_FragCoord and transform the w component
+ * according to the following pseudocode:
+ *
+ * gl_FragCoord.xyz = gl_FragCoord_orig.xyz
+ * gl_FragCoord.w = 1.0 / gl_FragCoord_orig.w
+ *
+ * To trigger the transformation, gl_FragCoord currently has to be treated
+ * as a system value with PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL enabled.
+ */
+
+static void
+lower_fragcoord_wtrans(nir_builder *b, nir_intrinsic_instr *intr)
+{
+ assert(intr->dest.is_ssa);
+
+ b->cursor = nir_before_instr(&intr->instr);
+
+ nir_ssa_def *fragcoord_in = nir_load_frag_coord(b);
+ nir_ssa_def *w_rcp = nir_frcp(b, nir_channel(b, fragcoord_in, 3));
+ nir_ssa_def *fragcoord_wtrans = nir_vec4(b,
+ nir_channel(b, fragcoord_in, 0),
+ nir_channel(b, fragcoord_in, 1),
+ nir_channel(b, fragcoord_in, 2),
+ w_rcp);
+ nir_ssa_def_rewrite_uses(&intr->dest.ssa,
+ nir_src_for_ssa(fragcoord_wtrans));
+}
+
+void
+nir_lower_fragcoord_wtrans(nir_shader *shader)
+{
+ assert(shader->info.stage == MESA_SHADER_FRAGMENT);
+
+ nir_foreach_function(func, shader) {
+ if (!func->impl)
+ continue;
+
+ nir_builder b;
+ nir_builder_init(&b, func->impl);
+ nir_foreach_block(block, func->impl) {
+ nir_foreach_instr_safe(instr, block) {
+ if (instr->type != nir_instr_type_intrinsic)
+ continue;
+
+ nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+ if (intr->intrinsic != nir_intrinsic_load_frag_coord)
+ continue;
+
+ lower_fragcoord_wtrans(&b, intr);
+ }
+ }
+ nir_metadata_preserve(func->impl, nir_metadata_block_index |
+ nir_metadata_dominance);
+ }
+}