st/nir/radeonsi: move nir_lower_uniforms_to_ubo() to the state tracker
authorTimothy Arceri <tarceri@itsqueeze.com>
Fri, 9 Mar 2018 00:57:52 +0000 (11:57 +1100)
committerTimothy Arceri <tarceri@itsqueeze.com>
Tue, 20 Mar 2018 03:17:34 +0000 (14:17 +1100)
This will only ever be used by gallium drivers so it probably doesn't
belong in the nir toolkit. Also we want to pass it some non NIR
things in the following patch.

To avoid regressions we wrap the lowering calls that have been moved
to st_glsl_to_nir with a quick hack so that they are only called for
radeonsi, we will replace the hack with a check for uniform packing
in a following patch.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
src/compiler/Makefile.sources
src/compiler/nir/meson.build
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_uniforms_to_ubo.c [deleted file]
src/gallium/drivers/radeonsi/si_shader_nir.c
src/mesa/Makefile.sources
src/mesa/meson.build
src/mesa/state_tracker/st_glsl_to_nir.cpp
src/mesa/state_tracker/st_nir.h
src/mesa/state_tracker/st_nir_lower_uniforms_to_ubo.c [new file with mode: 0644]

index 55143dbc66a639cae69c3818db0ea5022ca27fa1..b231f2fa972ecb6cc7294e510bdd9ceb32b8b761 100644 (file)
@@ -244,7 +244,6 @@ NIR_FILES = \
        nir/nir_lower_tex.c \
        nir/nir_lower_to_source_mods.c \
        nir/nir_lower_two_sided_color.c \
-       nir/nir_lower_uniforms_to_ubo.c \
        nir/nir_lower_vars_to_ssa.c \
        nir/nir_lower_var_copies.c \
        nir/nir_lower_vec_to_movs.c \
index 289bb9ea782e4efd238c51056c543f000cf5caf4..e97ce0d1e2d49bc5305801d4beb253bf351d08e7 100644 (file)
@@ -138,7 +138,6 @@ files_libnir = files(
   'nir_lower_tex.c',
   'nir_lower_to_source_mods.c',
   'nir_lower_two_sided_color.c',
-  'nir_lower_uniforms_to_ubo.c',
   'nir_lower_vars_to_ssa.c',
   'nir_lower_var_copies.c',
   'nir_lower_vec_to_movs.c',
index 7ad19b42c178cfdac64a84309e6193bf3ba66fff..d7baabd6f6ee4d9672665b0fb76ae09654fb96c2 100644 (file)
@@ -2712,7 +2712,6 @@ void nir_lower_bitmap(nir_shader *shader, const nir_lower_bitmap_options *option
 bool nir_lower_atomics(nir_shader *shader,
                        const struct gl_shader_program *shader_program);
 bool nir_lower_atomics_to_ssbo(nir_shader *shader, unsigned ssbo_offset);
-bool nir_lower_uniforms_to_ubo(nir_shader *shader);
 bool nir_lower_to_source_mods(nir_shader *shader);
 
 bool nir_lower_gs_intrinsics(nir_shader *shader);
diff --git a/src/compiler/nir/nir_lower_uniforms_to_ubo.c b/src/compiler/nir/nir_lower_uniforms_to_ubo.c
deleted file mode 100644 (file)
index 15bf0d9..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright 2017 Advanced Micro Devices, Inc.
- *
- * 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
- * on 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 AUTHOR(S) AND/OR THEIR SUPPLIERS 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.
- */
-
-/*
- * Remap load_uniform intrinsics to UBO accesses of UBO binding point 0. Both
- * the base and the offset are interpreted as 16-byte units.
- *
- * Simultaneously, remap existing UBO accesses by increasing their binding
- * point by 1.
- */
-
-#include "nir.h"
-#include "nir_builder.h"
-
-static bool
-lower_instr(nir_intrinsic_instr *instr, nir_builder *b)
-{
-   b->cursor = nir_before_instr(&instr->instr);
-
-   if (instr->intrinsic == nir_intrinsic_load_ubo) {
-      nir_ssa_def *old_idx = nir_ssa_for_src(b, instr->src[0], 1);
-      nir_ssa_def *new_idx = nir_iadd(b, old_idx, nir_imm_int(b, 1));
-      nir_instr_rewrite_src(&instr->instr, &instr->src[0],
-                            nir_src_for_ssa(new_idx));
-      return true;
-   }
-
-   if (instr->intrinsic == nir_intrinsic_load_uniform) {
-      nir_ssa_def *ubo_idx = nir_imm_int(b, 0);
-      nir_ssa_def *ubo_offset =
-         nir_imul(b, nir_imm_int(b, 16),
-                  nir_iadd(b, nir_imm_int(b, nir_intrinsic_base(instr)),
-                           nir_ssa_for_src(b, instr->src[0], 1)));
-
-      nir_intrinsic_instr *load =
-         nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
-      load->num_components = instr->num_components;
-      load->src[0] = nir_src_for_ssa(ubo_idx);
-      load->src[1] = nir_src_for_ssa(ubo_offset);
-      nir_ssa_dest_init(&load->instr, &load->dest,
-                        load->num_components, instr->dest.ssa.bit_size,
-                        instr->dest.ssa.name);
-      nir_builder_instr_insert(b, &load->instr);
-      nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
-
-      nir_instr_remove(&instr->instr);
-      return true;
-   }
-
-   return false;
-}
-
-bool
-nir_lower_uniforms_to_ubo(nir_shader *shader)
-{
-   bool progress = 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_instr(nir_instr_as_intrinsic(instr),
-                                          &builder);
-            }
-         }
-
-         nir_metadata_preserve(function->impl, nir_metadata_block_index |
-                                               nir_metadata_dominance);
-      }
-   }
-
-   return progress;
-}
-
index acb796b331c6ebd66f8e1ab96de846bdd1766d35..7f17affa4d3ac30fcdc19e897521257c08a774dd 100644 (file)
 #include "compiler/nir_types.h"
 
 
-static int
-type_size(const struct glsl_type *type)
-{
-   return glsl_count_attribute_slots(type, false);
-}
-
 static void scan_instruction(struct tgsi_shader_info *info,
                             nir_instr *instr)
 {
@@ -650,10 +644,6 @@ si_lower_nir(struct si_shader_selector* sel)
         * - ensure constant offsets for texture instructions are folded
         *   and copy-propagated
         */
-       NIR_PASS_V(sel->nir, nir_lower_io, nir_var_uniform, type_size,
-                  (nir_lower_io_options)0);
-       NIR_PASS_V(sel->nir, nir_lower_uniforms_to_ubo);
-
        NIR_PASS_V(sel->nir, nir_lower_returns);
        NIR_PASS_V(sel->nir, nir_lower_vars_to_ssa);
        NIR_PASS_V(sel->nir, nir_lower_alu_to_scalar);
index 0a9aad52d0f728c303ec8f6b1d455ac9ac411342..0446078136d90944db4e80b40003ad7e550b8b3a 100644 (file)
@@ -532,6 +532,7 @@ STATETRACKER_FILES = \
        state_tracker/st_nir.h \
        state_tracker/st_nir_lower_builtin.c \
        state_tracker/st_nir_lower_tex_src_plane.c \
+       state_tracker/st_nir_lower_uniforms_to_ubo.c \
        state_tracker/st_pbo.c \
        state_tracker/st_pbo.h \
        state_tracker/st_program.c \
index aa27d5926414b2a0162f19faea494689b142668e..b74d1693771bc9eab2bbb70fe4e4252cbbed11b9 100644 (file)
@@ -579,6 +579,7 @@ files_libmesa_gallium = files(
   'state_tracker/st_nir.h',
   'state_tracker/st_nir_lower_builtin.c',
   'state_tracker/st_nir_lower_tex_src_plane.c',
+  'state_tracker/st_nir_lower_uniforms_to_ubo.c',
   'state_tracker/st_pbo.c',
   'state_tracker/st_pbo.h',
   'state_tracker/st_program.c',
index 1fd553fdf8b3a416fdc70ce34fcc7dcf46e0a514..2ca64231e0d7d52f236c41a381c32e91cea8d2c2 100644 (file)
@@ -37,6 +37,7 @@
 #include "main/uniforms.h"
 
 #include "st_context.h"
+#include "st_glsl_types.h"
 #include "st_program.h"
 
 #include "compiler/nir/nir.h"
@@ -752,6 +753,18 @@ st_finalize_nir(struct st_context *st, struct gl_program *prog,
    st_nir_assign_uniform_locations(st->ctx, prog, shader_program,
                                    &nir->uniforms, &nir->num_uniforms);
 
+   /* Below is a quick hack so that uniform lowering only runs on radeonsi
+    * (the only NIR backend that currently supports tess) once we enable
+    * uniform packing support we will just use
+    * ctx->Const.PackedDriverUniformStorage for this check.
+    */
+   if (screen->get_shader_param(screen, PIPE_SHADER_TESS_CTRL,
+                                PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
+      NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, type_size,
+                 (nir_lower_io_options)0);
+      NIR_PASS_V(nir, st_nir_lower_uniforms_to_ubo);
+   }
+
    if (screen->get_param(screen, PIPE_CAP_NIR_SAMPLERS_AS_DEREF))
       NIR_PASS_V(nir, nir_lower_samplers_as_deref, shader_program);
    else
index 8c81c3f2d8632db7d52ff8b3657873c59b58e855..1c2e32a5e6f3a152deaa3903b87cd04daad5e26f 100644 (file)
@@ -36,6 +36,7 @@ struct nir_shader;
 void st_nir_lower_builtin(struct nir_shader *shader);
 void st_nir_lower_tex_src_plane(struct nir_shader *shader, unsigned free_slots,
                                 unsigned lower_2plane, unsigned lower_3plane);
+bool st_nir_lower_uniforms_to_ubo(struct nir_shader *shader);
 
 void st_finalize_nir(struct st_context *st, struct gl_program *prog,
                      struct gl_shader_program *shader_program,
diff --git a/src/mesa/state_tracker/st_nir_lower_uniforms_to_ubo.c b/src/mesa/state_tracker/st_nir_lower_uniforms_to_ubo.c
new file mode 100644 (file)
index 0000000..1d2cca0
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2017 Advanced Micro Devices, Inc.
+ *
+ * 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
+ * on 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 AUTHOR(S) AND/OR THEIR SUPPLIERS 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.
+ */
+
+/*
+ * Remap load_uniform intrinsics to UBO accesses of UBO binding point 0. Both
+ * the base and the offset are interpreted as 16-byte units.
+ *
+ * Simultaneously, remap existing UBO accesses by increasing their binding
+ * point by 1.
+ */
+
+#include "nir.h"
+#include "nir_builder.h"
+#include "st_nir.h"
+
+static bool
+lower_instr(nir_intrinsic_instr *instr, nir_builder *b)
+{
+   b->cursor = nir_before_instr(&instr->instr);
+
+   if (instr->intrinsic == nir_intrinsic_load_ubo) {
+      nir_ssa_def *old_idx = nir_ssa_for_src(b, instr->src[0], 1);
+      nir_ssa_def *new_idx = nir_iadd(b, old_idx, nir_imm_int(b, 1));
+      nir_instr_rewrite_src(&instr->instr, &instr->src[0],
+                            nir_src_for_ssa(new_idx));
+      return true;
+   }
+
+   if (instr->intrinsic == nir_intrinsic_load_uniform) {
+      nir_ssa_def *ubo_idx = nir_imm_int(b, 0);
+      nir_ssa_def *ubo_offset =
+         nir_imul(b, nir_imm_int(b, 16),
+                  nir_iadd(b, nir_imm_int(b, nir_intrinsic_base(instr)),
+                           nir_ssa_for_src(b, instr->src[0], 1)));
+
+      nir_intrinsic_instr *load =
+         nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
+      load->num_components = instr->num_components;
+      load->src[0] = nir_src_for_ssa(ubo_idx);
+      load->src[1] = nir_src_for_ssa(ubo_offset);
+      nir_ssa_dest_init(&load->instr, &load->dest,
+                        load->num_components, instr->dest.ssa.bit_size,
+                        instr->dest.ssa.name);
+      nir_builder_instr_insert(b, &load->instr);
+      nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
+
+      nir_instr_remove(&instr->instr);
+      return true;
+   }
+
+   return false;
+}
+
+bool
+st_nir_lower_uniforms_to_ubo(nir_shader *shader)
+{
+   bool progress = 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_instr(nir_instr_as_intrinsic(instr),
+                                          &builder);
+            }
+         }
+
+         nir_metadata_preserve(function->impl, nir_metadata_block_index |
+                                               nir_metadata_dominance);
+      }
+   }
+
+   return progress;
+}
+