[v2,1/2] nir: Add nir_lower_viewport_transform

Submitted by Alyssa Rosenzweig on April 8, 2019, 4:31 a.m.

Details

Message ID 20190408043200.6417-2-alyssa@rosenzweig.io
State New
Headers show
Series "NIR viewport lowering" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Alyssa Rosenzweig April 8, 2019, 4:31 a.m.
On Mali hardware (supported by Panfrost and Lima), the fixed-function
transformation from world-space to screen-space coordinates is done in
the vertex shader prior to writing out the gl_Position varying, rather
than in dedicated hardware. This commit adds a shared NIR pass for
implementing coordinate transformation and lowering gl_Position writes
into screen-space gl_Position writes.

v2: Run directly on derefs before io/vars are lowered to cleanup the
code substantially. Thank you to Qiang for this suggestion!

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Suggested-by: Qiang Yu <yuq825@gmail.com>
Cc: Jason Ekstrand <jason@jlekstrand.net>
Cc: Eric Anholt <eric@anholt.net>
---
 src/compiler/nir/meson.build                  |  1 +
 src/compiler/nir/nir.h                        |  1 +
 .../nir/nir_lower_viewport_transform.c        | 98 +++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 src/compiler/nir/nir_lower_viewport_transform.c

Patch hide | download patch | download mbox

diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index c65f2ff62ff..c274361bdc4 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -151,6 +151,7 @@  files_libnir = files(
   'nir_lower_vars_to_ssa.c',
   'nir_lower_var_copies.c',
   'nir_lower_vec_to_movs.c',
+  'nir_lower_viewport_transform.c',
   'nir_lower_wpos_center.c',
   'nir_lower_wpos_ytransform.c',
   'nir_lower_bit_size.c',
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index bc72d8f83f5..0f6ed734efa 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -3124,6 +3124,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_viewport_transform(nir_shader *shader);
 bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
 
 typedef struct nir_lower_subgroups_options {
diff --git a/src/compiler/nir/nir_lower_viewport_transform.c b/src/compiler/nir/nir_lower_viewport_transform.c
new file mode 100644
index 00000000000..9646b72c053
--- /dev/null
+++ b/src/compiler/nir/nir_lower_viewport_transform.c
@@ -0,0 +1,98 @@ 
+/*
+ * Copyright (C) 2019 Alyssa Rosenzweig
+ *
+ * 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.
+ */
+
+/* On some hardware (particularly, all current versions of Mali GPUs),
+ * vertex shaders do not output gl_Position in world-space. Instead, they
+ * output gl_Position in transformed screen space via the "pseudo"
+ * position varying. Thus, this pass finds writes to gl_Position and
+ * changes them to transformed writes, still to gl_Position. The
+ * outputted screen space is still written back to VARYING_SLOT_POS,
+ * which is semantically ambiguous but nevertheless a good match for
+ * Gallium/NIR/Mali.
+ *
+ * Implements coordinate transformation as defined in section 12.5
+ * "Coordinate Transformation" of the OpenGL ES 3.2 full specification.
+ *
+ * This pass must run before lower_vars/lower_io such that derefs are
+ * still in place.
+ */
+
+#include "nir/nir.h"
+#include "nir/nir_builder.h"
+
+void
+nir_lower_viewport_transform(nir_shader *shader)
+{
+   assert(shader->info.stage == MESA_SHADER_VERTEX);
+
+   nir_foreach_function(func, shader) {
+      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_store_deref) continue;
+
+            nir_variable *var = nir_intrinsic_get_var(intr, 0);
+            if (var->data.location != VARYING_SLOT_POS) continue;
+
+            nir_builder b;
+            nir_builder_init(&b, func->impl);
+            b.cursor = nir_before_instr(instr);
+
+            /* Grab the source and viewport */
+            nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);
+            nir_ssa_def *scale = nir_load_viewport_scale(&b);
+            nir_ssa_def *offset = nir_load_viewport_offset(&b);
+
+            /* World space to normalised device coordinates to screen space */
+
+            nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));
+
+            nir_ssa_def *ndc_point = nir_fmul(&b,
+                  nir_channels(&b, input_point, 0x7), w_recip);
+
+            nir_ssa_def *screen = nir_fadd(&b,
+                  nir_fmul(&b, ndc_point, scale), offset);
+
+            /* gl_Position will be written out in screenspace xyz, with w set to
+             * the reciprocal we computed earlier. The transformed w component is
+             * then used for perspective-correct varying interpolation. The
+             * transformed w component must preserve its original sign; this is
+             * used in depth clipping computations */
+
+            nir_ssa_def *screen_space = nir_vec4(&b,
+                             nir_channel(&b, screen, 0),
+                             nir_channel(&b, screen, 1),
+                             nir_channel(&b, screen, 2),
+                             w_recip);
+
+            nir_instr_rewrite_src(instr, &intr->src[1],
+                                  nir_src_for_ssa(screen_space));
+         }
+      }
+
+      nir_metadata_preserve(func->impl, nir_metadata_block_index |
+                            nir_metadata_dominance);
+   }
+}

Comments

On Mon, Apr 8, 2019 at 12:30 PM Alyssa Rosenzweig <alyssa@rosenzweig.io> wrote:
>
> On Mali hardware (supported by Panfrost and Lima), the fixed-function
> transformation from world-space to screen-space coordinates is done in
> the vertex shader prior to writing out the gl_Position varying, rather
> than in dedicated hardware. This commit adds a shared NIR pass for
> implementing coordinate transformation and lowering gl_Position writes
> into screen-space gl_Position writes.
>
> v2: Run directly on derefs before io/vars are lowered to cleanup the
> code substantially. Thank you to Qiang for this suggestion!
>
> Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
> Suggested-by: Qiang Yu <yuq825@gmail.com>
> Cc: Jason Ekstrand <jason@jlekstrand.net>
> Cc: Eric Anholt <eric@anholt.net>
> ---
>  src/compiler/nir/meson.build                  |  1 +
>  src/compiler/nir/nir.h                        |  1 +
>  .../nir/nir_lower_viewport_transform.c        | 98 +++++++++++++++++++
>  3 files changed, 100 insertions(+)
>  create mode 100644 src/compiler/nir/nir_lower_viewport_transform.c
>
> diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
> index c65f2ff62ff..c274361bdc4 100644
> --- a/src/compiler/nir/meson.build
> +++ b/src/compiler/nir/meson.build
> @@ -151,6 +151,7 @@ files_libnir = files(
>    'nir_lower_vars_to_ssa.c',
>    'nir_lower_var_copies.c',
>    'nir_lower_vec_to_movs.c',
> +  'nir_lower_viewport_transform.c',
>    'nir_lower_wpos_center.c',
>    'nir_lower_wpos_ytransform.c',
>    'nir_lower_bit_size.c',
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
> index bc72d8f83f5..0f6ed734efa 100644
> --- a/src/compiler/nir/nir.h
> +++ b/src/compiler/nir/nir.h
> @@ -3124,6 +3124,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_viewport_transform(nir_shader *shader);
>  bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
>
>  typedef struct nir_lower_subgroups_options {
> diff --git a/src/compiler/nir/nir_lower_viewport_transform.c b/src/compiler/nir/nir_lower_viewport_transform.c
> new file mode 100644
> index 00000000000..9646b72c053
> --- /dev/null
> +++ b/src/compiler/nir/nir_lower_viewport_transform.c
> @@ -0,0 +1,98 @@
> +/*
> + * Copyright (C) 2019 Alyssa Rosenzweig
> + *
> + * 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.
> + */
> +
> +/* On some hardware (particularly, all current versions of Mali GPUs),
> + * vertex shaders do not output gl_Position in world-space. Instead, they
> + * output gl_Position in transformed screen space via the "pseudo"
> + * position varying. Thus, this pass finds writes to gl_Position and
> + * changes them to transformed writes, still to gl_Position. The
> + * outputted screen space is still written back to VARYING_SLOT_POS,
> + * which is semantically ambiguous but nevertheless a good match for
> + * Gallium/NIR/Mali.
> + *
> + * Implements coordinate transformation as defined in section 12.5
> + * "Coordinate Transformation" of the OpenGL ES 3.2 full specification.
> + *
> + * This pass must run before lower_vars/lower_io such that derefs are
> + * still in place.
> + */
> +
> +#include "nir/nir.h"
> +#include "nir/nir_builder.h"
> +
> +void
> +nir_lower_viewport_transform(nir_shader *shader)
> +{
> +   assert(shader->info.stage == MESA_SHADER_VERTEX);
> +
> +   nir_foreach_function(func, shader) {
> +      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_store_deref) continue;
> +
> +            nir_variable *var = nir_intrinsic_get_var(intr, 0);
> +            if (var->data.location != VARYING_SLOT_POS) continue;
> +
> +            nir_builder b;
> +            nir_builder_init(&b, func->impl);

This should be done per nir function, and I suggest to divide this function
into more sub-functions to follow conventions of other nir_lower_*
implementation.

Regards,
Qiang

> +            b.cursor = nir_before_instr(instr);
> +
> +            /* Grab the source and viewport */
> +            nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);
> +            nir_ssa_def *scale = nir_load_viewport_scale(&b);
> +            nir_ssa_def *offset = nir_load_viewport_offset(&b);
> +
> +            /* World space to normalised device coordinates to screen space */
> +
> +            nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));
> +
> +            nir_ssa_def *ndc_point = nir_fmul(&b,
> +                  nir_channels(&b, input_point, 0x7), w_recip);
> +
> +            nir_ssa_def *screen = nir_fadd(&b,
> +                  nir_fmul(&b, ndc_point, scale), offset);
> +
> +            /* gl_Position will be written out in screenspace xyz, with w set to
> +             * the reciprocal we computed earlier. The transformed w component is
> +             * then used for perspective-correct varying interpolation. The
> +             * transformed w component must preserve its original sign; this is
> +             * used in depth clipping computations */
> +
> +            nir_ssa_def *screen_space = nir_vec4(&b,
> +                             nir_channel(&b, screen, 0),
> +                             nir_channel(&b, screen, 1),
> +                             nir_channel(&b, screen, 2),
> +                             w_recip);
> +
> +            nir_instr_rewrite_src(instr, &intr->src[1],
> +                                  nir_src_for_ssa(screen_space));
> +         }
> +      }
> +
> +      nir_metadata_preserve(func->impl, nir_metadata_block_index |
> +                            nir_metadata_dominance);
> +   }
> +}
> --
> 2.20.1
>
man. 8. apr. 2019 kl. 06:30 skrev Alyssa Rosenzweig <alyssa@rosenzweig.io>:
>
> On Mali hardware (supported by Panfrost and Lima), the fixed-function
> transformation from world-space to screen-space coordinates is done in
> the vertex shader prior to writing out the gl_Position varying, rather
> than in dedicated hardware. This commit adds a shared NIR pass for
> implementing coordinate transformation and lowering gl_Position writes
> into screen-space gl_Position writes.
>
> v2: Run directly on derefs before io/vars are lowered to cleanup the
> code substantially. Thank you to Qiang for this suggestion!
>
> Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
> Suggested-by: Qiang Yu <yuq825@gmail.com>
> Cc: Jason Ekstrand <jason@jlekstrand.net>
> Cc: Eric Anholt <eric@anholt.net>
> ---
>  src/compiler/nir/meson.build                  |  1 +
>  src/compiler/nir/nir.h                        |  1 +
>  .../nir/nir_lower_viewport_transform.c        | 98 +++++++++++++++++++
>  3 files changed, 100 insertions(+)
>  create mode 100644 src/compiler/nir/nir_lower_viewport_transform.c
>
> diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
> index c65f2ff62ff..c274361bdc4 100644
> --- a/src/compiler/nir/meson.build
> +++ b/src/compiler/nir/meson.build
> @@ -151,6 +151,7 @@ files_libnir = files(
>    'nir_lower_vars_to_ssa.c',
>    'nir_lower_var_copies.c',
>    'nir_lower_vec_to_movs.c',
> +  'nir_lower_viewport_transform.c',
>    'nir_lower_wpos_center.c',
>    'nir_lower_wpos_ytransform.c',
>    'nir_lower_bit_size.c',
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
> index bc72d8f83f5..0f6ed734efa 100644
> --- a/src/compiler/nir/nir.h
> +++ b/src/compiler/nir/nir.h
> @@ -3124,6 +3124,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_viewport_transform(nir_shader *shader);
>  bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
>
>  typedef struct nir_lower_subgroups_options {
> diff --git a/src/compiler/nir/nir_lower_viewport_transform.c b/src/compiler/nir/nir_lower_viewport_transform.c
> new file mode 100644
> index 00000000000..9646b72c053
> --- /dev/null
> +++ b/src/compiler/nir/nir_lower_viewport_transform.c
> @@ -0,0 +1,98 @@
> +/*
> + * Copyright (C) 2019 Alyssa Rosenzweig
> + *
> + * 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.
> + */
> +
> +/* On some hardware (particularly, all current versions of Mali GPUs),
> + * vertex shaders do not output gl_Position in world-space. Instead, they
> + * output gl_Position in transformed screen space via the "pseudo"
> + * position varying. Thus, this pass finds writes to gl_Position and
> + * changes them to transformed writes, still to gl_Position. The
> + * outputted screen space is still written back to VARYING_SLOT_POS,
> + * which is semantically ambiguous but nevertheless a good match for
> + * Gallium/NIR/Mali.
> + *
> + * Implements coordinate transformation as defined in section 12.5
> + * "Coordinate Transformation" of the OpenGL ES 3.2 full specification.
> + *
> + * This pass must run before lower_vars/lower_io such that derefs are
> + * still in place.
> + */
> +
> +#include "nir/nir.h"
> +#include "nir/nir_builder.h"
> +
> +void
> +nir_lower_viewport_transform(nir_shader *shader)
> +{
> +   assert(shader->info.stage == MESA_SHADER_VERTEX);
> +
> +   nir_foreach_function(func, shader) {
> +      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_store_deref) continue;
> +
> +            nir_variable *var = nir_intrinsic_get_var(intr, 0);
> +            if (var->data.location != VARYING_SLOT_POS) continue;
> +

I believe it's agreement in mesa that the if ( ... ) should be on one line,
and the continue, return, etc should be on a new one.

Regards,
Thomas Helland

> +            nir_builder b;
> +            nir_builder_init(&b, func->impl);
> +            b.cursor = nir_before_instr(instr);
> +
> +            /* Grab the source and viewport */
> +            nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);
> +            nir_ssa_def *scale = nir_load_viewport_scale(&b);
> +            nir_ssa_def *offset = nir_load_viewport_offset(&b);
> +
> +            /* World space to normalised device coordinates to screen space */
> +
> +            nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));
> +
> +            nir_ssa_def *ndc_point = nir_fmul(&b,
> +                  nir_channels(&b, input_point, 0x7), w_recip);
> +
> +            nir_ssa_def *screen = nir_fadd(&b,
> +                  nir_fmul(&b, ndc_point, scale), offset);
> +
> +            /* gl_Position will be written out in screenspace xyz, with w set to
> +             * the reciprocal we computed earlier. The transformed w component is
> +             * then used for perspective-correct varying interpolation. The
> +             * transformed w component must preserve its original sign; this is
> +             * used in depth clipping computations */
> +
> +            nir_ssa_def *screen_space = nir_vec4(&b,
> +                             nir_channel(&b, screen, 0),
> +                             nir_channel(&b, screen, 1),
> +                             nir_channel(&b, screen, 2),
> +                             w_recip);
> +
> +            nir_instr_rewrite_src(instr, &intr->src[1],
> +                                  nir_src_for_ssa(screen_space));
> +         }
> +      }
> +
> +      nir_metadata_preserve(func->impl, nir_metadata_block_index |
> +                            nir_metadata_dominance);
> +   }
> +}
> --
> 2.20.1
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
> This should be done per nir function, and I suggest to divide this function
> into more sub-functions to follow conventions of other nir_lower_*
> implementation.

You only should be writing gl_Position once anyway; why reinit for
functions that likely won't use them?

lower_alpha_test does it this way; it's fine.
> I believe it's agreement in mesa that the if ( ... ) should be on one line,
> and the continue, return, etc should be on a new one.

Fixed in v3.
On 4/8/19 5:34 AM, Thomas Helland wrote:
> man. 8. apr. 2019 kl. 06:30 skrev Alyssa Rosenzweig <alyssa@rosenzweig.io>:
>>
>> On Mali hardware (supported by Panfrost and Lima), the fixed-function
>> transformation from world-space to screen-space coordinates is done in
>> the vertex shader prior to writing out the gl_Position varying, rather
>> than in dedicated hardware. This commit adds a shared NIR pass for
>> implementing coordinate transformation and lowering gl_Position writes
>> into screen-space gl_Position writes.
>>
>> v2: Run directly on derefs before io/vars are lowered to cleanup the
>> code substantially. Thank you to Qiang for this suggestion!
>>
>> Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
>> Suggested-by: Qiang Yu <yuq825@gmail.com>
>> Cc: Jason Ekstrand <jason@jlekstrand.net>
>> Cc: Eric Anholt <eric@anholt.net>
>> ---
>>  src/compiler/nir/meson.build                  |  1 +
>>  src/compiler/nir/nir.h                        |  1 +
>>  .../nir/nir_lower_viewport_transform.c        | 98 +++++++++++++++++++
>>  3 files changed, 100 insertions(+)
>>  create mode 100644 src/compiler/nir/nir_lower_viewport_transform.c
>>
>> diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
>> index c65f2ff62ff..c274361bdc4 100644
>> --- a/src/compiler/nir/meson.build
>> +++ b/src/compiler/nir/meson.build
>> @@ -151,6 +151,7 @@ files_libnir = files(
>>    'nir_lower_vars_to_ssa.c',
>>    'nir_lower_var_copies.c',
>>    'nir_lower_vec_to_movs.c',
>> +  'nir_lower_viewport_transform.c',
>>    'nir_lower_wpos_center.c',
>>    'nir_lower_wpos_ytransform.c',
>>    'nir_lower_bit_size.c',
>> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
>> index bc72d8f83f5..0f6ed734efa 100644
>> --- a/src/compiler/nir/nir.h
>> +++ b/src/compiler/nir/nir.h
>> @@ -3124,6 +3124,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_viewport_transform(nir_shader *shader);
>>  bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
>>
>>  typedef struct nir_lower_subgroups_options {
>> diff --git a/src/compiler/nir/nir_lower_viewport_transform.c b/src/compiler/nir/nir_lower_viewport_transform.c
>> new file mode 100644
>> index 00000000000..9646b72c053
>> --- /dev/null
>> +++ b/src/compiler/nir/nir_lower_viewport_transform.c
>> @@ -0,0 +1,98 @@
>> +/*
>> + * Copyright (C) 2019 Alyssa Rosenzweig
>> + *
>> + * 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.
>> + */
>> +
>> +/* On some hardware (particularly, all current versions of Mali GPUs),
>> + * vertex shaders do not output gl_Position in world-space. Instead, they
>> + * output gl_Position in transformed screen space via the "pseudo"
>> + * position varying. Thus, this pass finds writes to gl_Position and
>> + * changes them to transformed writes, still to gl_Position. The
>> + * outputted screen space is still written back to VARYING_SLOT_POS,
>> + * which is semantically ambiguous but nevertheless a good match for
>> + * Gallium/NIR/Mali.
>> + *
>> + * Implements coordinate transformation as defined in section 12.5
>> + * "Coordinate Transformation" of the OpenGL ES 3.2 full specification.
>> + *
>> + * This pass must run before lower_vars/lower_io such that derefs are
>> + * still in place.
>> + */
>> +
>> +#include "nir/nir.h"
>> +#include "nir/nir_builder.h"
>> +
>> +void
>> +nir_lower_viewport_transform(nir_shader *shader)
>> +{
>> +   assert(shader->info.stage == MESA_SHADER_VERTEX);
>> +
>> +   nir_foreach_function(func, shader) {
>> +      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_store_deref) continue;
>> +
>> +            nir_variable *var = nir_intrinsic_get_var(intr, 0);
>> +            if (var->data.location != VARYING_SLOT_POS) continue;
>> +
> 
> I believe it's agreement in mesa that the if ( ... ) should be on one line,
> and the continue, return, etc should be on a new one.
> 
> Regards,
> Thomas Helland
> 
>> +            nir_builder b;
>> +            nir_builder_init(&b, func->impl);
>> +            b.cursor = nir_before_instr(instr);
>> +
>> +            /* Grab the source and viewport */
>> +            nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);
>> +            nir_ssa_def *scale = nir_load_viewport_scale(&b);
>> +            nir_ssa_def *offset = nir_load_viewport_offset(&b);
>> +
>> +            /* World space to normalised device coordinates to screen space */
>> +
>> +            nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));
>> +
>> +            nir_ssa_def *ndc_point = nir_fmul(&b,
>> +                  nir_channels(&b, input_point, 0x7), w_recip);
>> +
>> +            nir_ssa_def *screen = nir_fadd(&b,
>> +                  nir_fmul(&b, ndc_point, scale), offset);
>> +
>> +            /* gl_Position will be written out in screenspace xyz, with w set to
>> +             * the reciprocal we computed earlier. The transformed w component is
>> +             * then used for perspective-correct varying interpolation. The
>> +             * transformed w component must preserve its original sign; this is
>> +             * used in depth clipping computations */

As an even smaller nit to pick... the */ of a multi-line comment should
be on its own line.

>> +
>> +            nir_ssa_def *screen_space = nir_vec4(&b,
>> +                             nir_channel(&b, screen, 0),
>> +                             nir_channel(&b, screen, 1),
>> +                             nir_channel(&b, screen, 2),
>> +                             w_recip);
>> +
>> +            nir_instr_rewrite_src(instr, &intr->src[1],
>> +                                  nir_src_for_ssa(screen_space));
>> +         }
>> +      }
>> +
>> +      nir_metadata_preserve(func->impl, nir_metadata_block_index |
>> +                            nir_metadata_dominance);
>> +   }
>> +}
>> --
>> 2.20.1
>>
>> _______________________________________________
>> mesa-dev mailing list
>> mesa-dev@lists.freedesktop.org
>> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
> _______________________________________________
> mesa-dev mailing list
> mesa-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>
On 4/12/19 5:11 PM, Ian Romanick wrote:
> On 4/8/19 5:34 AM, Thomas Helland wrote:
>> man. 8. apr. 2019 kl. 06:30 skrev Alyssa Rosenzweig <alyssa@rosenzweig.io>:
>>>
>>> On Mali hardware (supported by Panfrost and Lima), the fixed-function
>>> transformation from world-space to screen-space coordinates is done in
>>> the vertex shader prior to writing out the gl_Position varying, rather
>>> than in dedicated hardware. This commit adds a shared NIR pass for
>>> implementing coordinate transformation and lowering gl_Position writes
>>> into screen-space gl_Position writes.
>>>
>>> v2: Run directly on derefs before io/vars are lowered to cleanup the
>>> code substantially. Thank you to Qiang for this suggestion!
>>>
>>> Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
>>> Suggested-by: Qiang Yu <yuq825@gmail.com>
>>> Cc: Jason Ekstrand <jason@jlekstrand.net>
>>> Cc: Eric Anholt <eric@anholt.net>
>>> ---
>>>  src/compiler/nir/meson.build                  |  1 +
>>>  src/compiler/nir/nir.h                        |  1 +
>>>  .../nir/nir_lower_viewport_transform.c        | 98 +++++++++++++++++++
>>>  3 files changed, 100 insertions(+)
>>>  create mode 100644 src/compiler/nir/nir_lower_viewport_transform.c
>>>
>>> diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
>>> index c65f2ff62ff..c274361bdc4 100644
>>> --- a/src/compiler/nir/meson.build
>>> +++ b/src/compiler/nir/meson.build
>>> @@ -151,6 +151,7 @@ files_libnir = files(
>>>    'nir_lower_vars_to_ssa.c',
>>>    'nir_lower_var_copies.c',
>>>    'nir_lower_vec_to_movs.c',
>>> +  'nir_lower_viewport_transform.c',
>>>    'nir_lower_wpos_center.c',
>>>    'nir_lower_wpos_ytransform.c',
>>>    'nir_lower_bit_size.c',
>>> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
>>> index bc72d8f83f5..0f6ed734efa 100644
>>> --- a/src/compiler/nir/nir.h
>>> +++ b/src/compiler/nir/nir.h
>>> @@ -3124,6 +3124,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_viewport_transform(nir_shader *shader);
>>>  bool nir_lower_uniforms_to_ubo(nir_shader *shader, int multiplier);
>>>
>>>  typedef struct nir_lower_subgroups_options {
>>> diff --git a/src/compiler/nir/nir_lower_viewport_transform.c b/src/compiler/nir/nir_lower_viewport_transform.c
>>> new file mode 100644
>>> index 00000000000..9646b72c053
>>> --- /dev/null
>>> +++ b/src/compiler/nir/nir_lower_viewport_transform.c
>>> @@ -0,0 +1,98 @@
>>> +/*
>>> + * Copyright (C) 2019 Alyssa Rosenzweig
>>> + *
>>> + * 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.
>>> + */
>>> +
>>> +/* On some hardware (particularly, all current versions of Mali GPUs),
>>> + * vertex shaders do not output gl_Position in world-space. Instead, they
>>> + * output gl_Position in transformed screen space via the "pseudo"
>>> + * position varying. Thus, this pass finds writes to gl_Position and
>>> + * changes them to transformed writes, still to gl_Position. The
>>> + * outputted screen space is still written back to VARYING_SLOT_POS,
>>> + * which is semantically ambiguous but nevertheless a good match for
>>> + * Gallium/NIR/Mali.
>>> + *
>>> + * Implements coordinate transformation as defined in section 12.5
>>> + * "Coordinate Transformation" of the OpenGL ES 3.2 full specification.
>>> + *
>>> + * This pass must run before lower_vars/lower_io such that derefs are
>>> + * still in place.
>>> + */
>>> +
>>> +#include "nir/nir.h"
>>> +#include "nir/nir_builder.h"
>>> +
>>> +void
>>> +nir_lower_viewport_transform(nir_shader *shader)
>>> +{
>>> +   assert(shader->info.stage == MESA_SHADER_VERTEX);
>>> +
>>> +   nir_foreach_function(func, shader) {
>>> +      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_store_deref) continue;
>>> +
>>> +            nir_variable *var = nir_intrinsic_get_var(intr, 0);
>>> +            if (var->data.location != VARYING_SLOT_POS) continue;
>>> +
>>
>> I believe it's agreement in mesa that the if ( ... ) should be on one line,
>> and the continue, return, etc should be on a new one.
>>
>> Regards,
>> Thomas Helland
>>
>>> +            nir_builder b;
>>> +            nir_builder_init(&b, func->impl);
>>> +            b.cursor = nir_before_instr(instr);
>>> +
>>> +            /* Grab the source and viewport */
>>> +            nir_ssa_def *input_point = nir_ssa_for_src(&b, intr->src[1], 4);
>>> +            nir_ssa_def *scale = nir_load_viewport_scale(&b);
>>> +            nir_ssa_def *offset = nir_load_viewport_offset(&b);
>>> +
>>> +            /* World space to normalised device coordinates to screen space */
>>> +
>>> +            nir_ssa_def *w_recip = nir_frcp(&b, nir_channel(&b, input_point, 3));
>>> +
>>> +            nir_ssa_def *ndc_point = nir_fmul(&b,
>>> +                  nir_channels(&b, input_point, 0x7), w_recip);
>>> +
>>> +            nir_ssa_def *screen = nir_fadd(&b,
>>> +                  nir_fmul(&b, ndc_point, scale), offset);
>>> +
>>> +            /* gl_Position will be written out in screenspace xyz, with w set to
>>> +             * the reciprocal we computed earlier. The transformed w component is
>>> +             * then used for perspective-correct varying interpolation. The
>>> +             * transformed w component must preserve its original sign; this is
>>> +             * used in depth clipping computations */
> 
> As an even smaller nit to pick... the */ of a multi-line comment should
> be on its own line.

But I see v3 hit the list about half hour ago... Don't worry about this
unless you need a v4 for some other reason.

>>> +
>>> +            nir_ssa_def *screen_space = nir_vec4(&b,
>>> +                             nir_channel(&b, screen, 0),
>>> +                             nir_channel(&b, screen, 1),
>>> +                             nir_channel(&b, screen, 2),
>>> +                             w_recip);
>>> +
>>> +            nir_instr_rewrite_src(instr, &intr->src[1],
>>> +                                  nir_src_for_ssa(screen_space));
>>> +         }
>>> +      }
>>> +
>>> +      nir_metadata_preserve(func->impl, nir_metadata_block_index |
>>> +                            nir_metadata_dominance);
>>> +   }
>>> +}
>>> --
>>> 2.20.1
>>>
>>> _______________________________________________
>>> mesa-dev mailing list
>>> mesa-dev@lists.freedesktop.org
>>> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>> _______________________________________________
>> mesa-dev mailing list
>> mesa-dev@lists.freedesktop.org
>> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>>
>