<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 12, 2019 at 6:46 PM Alyssa Rosenzweig <<a href="mailto:alyssa@rosenzweig.io">alyssa@rosenzweig.io</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mali hardware (supported by Panfrost and Lima), the fixed-function<br>
transformation from world-space to screen-space coordinates is done in<br>
the vertex shader prior to writing out the gl_Position varying, rather<br>
than in dedicated hardware. This commit adds a shared NIR pass for<br>
implementing coordinate transformation and lowering gl_Position writes<br>
into screen-space gl_Position writes.<br>
<br>
v2: Run directly on derefs before io/vars are lowered to cleanup the<br>
code substantially. Thank you to Qiang for this suggestion!<br>
<br>
v3: Bikeshed continues.<br>
<br>
Signed-off-by: Alyssa Rosenzweig <<a href="mailto:alyssa@rosenzweig.io" target="_blank">alyssa@rosenzweig.io</a>><br>
Suggested-by: Qiang Yu <<a href="mailto:yuq825@gmail.com" target="_blank">yuq825@gmail.com</a>><br>
Cc: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>><br>
Cc: Eric Anholt <<a href="mailto:eric@anholt.net" target="_blank">eric@anholt.net</a>><br>
---<br>
src/compiler/nir/meson.build | 1 +<br>
src/compiler/nir/nir.h | 1 +<br>
.../nir/nir_lower_viewport_transform.c | 101 ++++++++++++++++++<br></blockquote><div><br></div><div>For some short period of time, we still build with autotools. Please add this to Makefile.sources. That's also required for Android (which may actually be applicable for Mali)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
3 files changed, 103 insertions(+)<br>
create mode 100644 src/compiler/nir/nir_lower_viewport_transform.c<br>
<br>
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build<br>
index c65f2ff62ff..c274361bdc4 100644<br>
--- a/src/compiler/nir/meson.build<br>
+++ b/src/compiler/nir/meson.build<br>
@@ -151,6 +151,7 @@ files_libnir = files(<br>
'nir_lower_vars_to_ssa.c',<br>
'nir_lower_var_copies.c',<br>
'nir_lower_vec_to_movs.c',<br>
+ 'nir_lower_viewport_transform.c',<br>
'nir_lower_wpos_center.c',<br>
'nir_lower_wpos_ytransform.c',<br>
'nir_lower_bit_size.c',<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index bc72d8f83f5..0f6ed734efa 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -3124,6 +3124,7 @@ void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask);<br>
void nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask);<br>
bool nir_lower_io_to_vector(nir_shader *shader, nir_variable_mode mask);<br>
<br>
+void nir_lower_viewport_transform(nir_shader *shader);<br>
bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);<br>
<br>
typedef struct nir_lower_subgroups_options {<br>
diff --git a/src/compiler/nir/nir_lower_viewport_transform.c b/src/compiler/nir/nir_lower_viewport_transform.c<br>
new file mode 100644<br>
index 00000000000..66085b8da5a<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_lower_viewport_transform.c<br>
@@ -0,0 +1,101 @@<br>
+/*<br>
+ * Copyright (C) 2019 Alyssa Rosenzweig<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>
+/* On some hardware (particularly, all current versions of Mali GPUs),<br>
+ * vertex shaders do not output gl_Position in world-space. Instead, they<br>
+ * output gl_Position in transformed screen space via the "pseudo"<br>
+ * position varying. Thus, this pass finds writes to gl_Position and<br>
+ * changes them to transformed writes, still to gl_Position. The<br>
+ * outputted screen space is still written back to VARYING_SLOT_POS,<br>
+ * which is semantically ambiguous but nevertheless a good match for<br>
+ * Gallium/NIR/Mali.<br>
+ *<br>
+ * Implements coordinate transformation as defined in section 12.5<br>
+ * "Coordinate Transformation" of the OpenGL ES 3.2 full specification.<br>
+ *<br>
+ * This pass must run before lower_vars/lower_io such that derefs are<br>
+ * still in place.<br>
+ */<br>
+<br>
+#include "nir/nir.h"<br>
+#include "nir/nir_builder.h"<br>
+<br>
+void<br>
+nir_lower_viewport_transform(nir_shader *shader)<br>
+{<br>
+ assert(shader->info.stage == MESA_SHADER_VERTEX);<br>
+<br>
+ nir_foreach_function(func, shader) {<br>
+ nir_foreach_block(block, func->impl) {<br>
+ nir_foreach_instr_safe(instr, block) {<br>
+ if (instr->type != nir_instr_type_intrinsic)<br>
+ continue;<br>
+<br>
+ nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);<br>
+ if (intr->intrinsic != nir_intrinsic_store_deref)<br>
+ continue;<br>
+<br>
+ nir_variable *var = nir_intrinsic_get_var(intr, 0);<br>
+ if (var->data.location != VARYING_SLOT_POS)<br>
+ continue;<br>
+<br>
+ nir_builder b;<br>
+ nir_builder_init(&b, func->impl);<br>
+ b.cursor = nir_before_instr(instr);<br>
+<br>
+ /* Grab the source and viewport */<br>
+ nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);<br>
+ nir_ssa_def *scale = nir_load_viewport_scale(&b);<br>
+ nir_ssa_def *offset = nir_load_viewport_offset(&b);<br>
+<br>
+ /* World space to normalised device coordinates to screen space */<br>
+<br>
+ nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));<br>
+<br>
+ nir_ssa_def *ndc_point = nir_fmul(&b,<br>
+ nir_channels(&b, input_point, 0x7), w_recip);<br>
+<br>
+ nir_ssa_def *screen = nir_fadd(&b,<br>
+ nir_fmul(&b, ndc_point, scale), offset);<br>
+<br>
+ /* gl_Position will be written out in screenspace xyz, with w set to<br>
+ * the reciprocal we computed earlier. The transformed w component is<br>
+ * then used for perspective-correct varying interpolation. The<br>
+ * transformed w component must preserve its original sign; this is<br>
+ * used in depth clipping computations */<br>
+<br>
+ nir_ssa_def *screen_space = nir_vec4(&b,<br>
+ nir_channel(&b, screen, 0),<br>
+ nir_channel(&b, screen, 1),<br>
+ nir_channel(&b, screen, 2),<br>
+ w_recip);<br>
+<br>
+ nir_instr_rewrite_src(instr, &intr->src[1],<br>
+ nir_src_for_ssa(screen_space));<br>
+ }<br>
+ }<br>
+<br>
+ nir_metadata_preserve(func->impl, nir_metadata_block_index |<br>
+ nir_metadata_dominance);<br>
+ }<br>
+}<br>
-- <br>
2.20.1<br>
<br>
</blockquote></div></div>