[Mesa-dev] [PATCH 3/4] nir: add nir_lower_viewport_transform

Qiang Yu yuq825 at gmail.com
Wed Mar 27 03:54:31 UTC 2019


Signed-off-by: Qiang Yu <yuq825 at gmail.com>
---
 src/compiler/Makefile.sources                 |   1 +
 src/compiler/nir/meson.build                  |   1 +
 src/compiler/nir/nir.h                        |   8 +
 .../nir/nir_lower_viewport_transform.c        | 142 ++++++++++++++++++
 4 files changed, 152 insertions(+)
 create mode 100644 src/compiler/nir/nir_lower_viewport_transform.c

diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 722cfbb25a8..589a4f92602 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -269,6 +269,7 @@ NIR_FILES = \
 	nir/nir_lower_vars_to_ssa.c \
 	nir/nir_lower_var_copies.c \
 	nir/nir_lower_vec_to_movs.c \
+	nir/nir_lower_viewport_transform.c \
 	nir/nir_lower_wpos_center.c \
 	nir/nir_lower_wpos_ytransform.c \
 	nir/nir_metadata.c \
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index 4f1efb5c6d3..fd6678beb4d 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -149,6 +149,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 1da9874060b..688d006b646 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -3273,6 +3273,14 @@ bool nir_lower_wpos_ytransform(nir_shader *shader,
                                const nir_lower_wpos_ytransform_options *options);
 bool nir_lower_wpos_center(nir_shader *shader, const bool for_sample_shading);
 
+typedef struct nir_lower_viewport_transform_options {
+   gl_state_index16 scale[STATE_LENGTH];
+   gl_state_index16 translate[STATE_LENGTH];
+} nir_lower_viewport_transform_options;
+
+void nir_lower_viewport_transform(nir_shader *shader,
+                                  const nir_lower_viewport_transform_options *options);
+
 typedef struct nir_lower_drawpixels_options {
    gl_state_index16 texcoord_state_tokens[STATE_LENGTH];
    gl_state_index16 scale_state_tokens[STATE_LENGTH];
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..4a8102be1e0
--- /dev/null
+++ b/src/compiler/nir/nir_lower_viewport_transform.c
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2017-2019 Qiang Yu <yuq825 at gmail.com>
+ *
+ * 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, sub license,
+ * 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 NON-INFRINGEMENT. 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 "program/prog_statevars.h"
+#include "nir_builder.h"
+
+/* lower viewport transform into vertex shader
+ *
+ * This is needed for GPU like Mali Utgard and Midgard which has no viewport
+ * transform hw. Declare viewport transform parameters in uniform and use them
+ * to apply on the gl_Position varying output.
+ */
+
+typedef struct {
+   const nir_lower_viewport_transform_options *options;
+   nir_shader   *shader;
+   nir_builder   b;
+   nir_variable *scale, *translate;
+} lower_viewport_transform_state;
+
+static nir_variable *
+create_uniform(nir_shader *shader, const char *name, const gl_state_index16 *tokens)
+{
+   nir_variable *var = nir_variable_create(
+      shader, nir_var_uniform, glsl_vec_type(3), name);
+
+   var->num_state_slots = 1;
+   var->state_slots = ralloc_array(var, nir_state_slot, 1);
+   memcpy(var->state_slots[0].tokens, tokens,
+          sizeof(var->state_slots[0].tokens));
+   return var;
+}
+
+static nir_ssa_def *
+get_scale(lower_viewport_transform_state *state)
+{
+   if (!state->scale)
+      state->scale = create_uniform(state->shader, "gl_viewportScale",
+                                    state->options->scale);
+
+   return nir_load_var(&state->b, state->scale);
+}
+
+static nir_ssa_def *
+get_translate(lower_viewport_transform_state *state)
+{
+   if (!state->translate)
+      state->translate = create_uniform(state->shader, "gl_viewportTranslate",
+                                        state->options->translate);
+
+   return nir_load_var(&state->b, state->translate);
+}
+
+static void
+lower_viewport_transform_block(lower_viewport_transform_state *state, nir_block *block)
+{
+   nir_foreach_instr_safe(instr, block) {
+      if (instr->type == nir_instr_type_intrinsic) {
+         nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+
+         if (intr->intrinsic == nir_intrinsic_store_deref) {
+            nir_variable *var = nir_intrinsic_get_var(intr, 0);
+
+            if (var->data.mode == nir_var_shader_out &&
+                var->data.location == VARYING_SLOT_POS) {
+               assert(intr->num_components == 4);
+
+               state->b.cursor = nir_before_instr(instr);
+
+               nir_ssa_def *pos = nir_ssa_for_src(&state->b, intr->src[1], intr->num_components);
+               nir_ssa_def *xyz = nir_channels(&state->b, pos, 0x7);
+               nir_ssa_def *rcpw = nir_frcp(&state->b, nir_channel(&state->b, pos, 3));
+
+               /* homogenization */
+               xyz = nir_fmul(&state->b, xyz, rcpw);
+
+               /* viewport transform*/
+               xyz = nir_fmul(&state->b, xyz, get_scale(state));
+               xyz = nir_fadd(&state->b, xyz, get_translate(state));
+
+               pos = nir_vec4(&state->b,
+                              nir_channel(&state->b, xyz, 0),
+                              nir_channel(&state->b, xyz, 1),
+                              nir_channel(&state->b, xyz, 2),
+                              rcpw);
+
+               nir_instr_rewrite_src(instr, &intr->src[1], nir_src_for_ssa(pos));
+            }
+         }
+      }
+   }
+}
+
+static void
+lower_viewport_transform_impl(lower_viewport_transform_state *state, nir_function_impl *impl)
+{
+   nir_builder_init(&state->b, impl);
+
+   nir_foreach_block(block, impl) {
+      lower_viewport_transform_block(state, block);
+   }
+   nir_metadata_preserve(impl, nir_metadata_block_index |
+                               nir_metadata_dominance);
+}
+
+void
+nir_lower_viewport_transform(nir_shader *shader,
+                             const nir_lower_viewport_transform_options *options)
+{
+   lower_viewport_transform_state state = {
+      .options = options,
+      .shader = shader,
+   };
+
+   assert(shader->info.stage == MESA_SHADER_VERTEX);
+
+   nir_foreach_function(function, shader) {
+      if (function->impl)
+         lower_viewport_transform_impl(&state, function->impl);
+   }
+}
-- 
2.17.1



More information about the mesa-dev mailing list