nir: Move nir_lower_uniforms_to_ubo to compiler/nir.
authorTimur Kristóf <timur.kristof@gmail.com>
Fri, 8 Feb 2019 21:36:37 +0000 (22:36 +0100)
committerEric Anholt <eric@anholt.net>
Tue, 5 Mar 2019 19:13:27 +0000 (19:13 +0000)
The nir_lower_uniforms_to_ubo function is useful outside of
mesa/state_tracker, and in fact is needed to produce NIR for
drivers that have the PIPE_CAP_PACKED_UNIFORMS capability.

Signed-Off-By: Timur Kristóf <timur.kristof@gmail.com>
Tested-by: Andre Heider <a.heider@gmail.com>
Tested-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
src/compiler/Makefile.sources
src/compiler/nir/meson.build
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_uniforms_to_ubo.c [new file with mode: 0644]
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_builtins.c
src/mesa/state_tracker/st_nir_lower_uniforms_to_ubo.c [deleted file]

index 489022a22a1246ba17de12549ab1b5c7914d6735..85c216fef1de58c6f3d61c42a3b528a56fdc2a26 100644 (file)
@@ -262,6 +262,7 @@ 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 20a26a262558f557abb17c98907a4bc4c74ca4c4..d7f88f391f467819cc9bbe0ba7547b703212d8a2 100644 (file)
@@ -150,6 +150,7 @@ files_libnir = files(
   'nir_lower_wpos_center.c',
   'nir_lower_wpos_ytransform.c',
   'nir_lower_bit_size.c',
+  'nir_lower_uniforms_to_ubo.c',
   'nir_metadata.c',
   'nir_move_load_const.c',
   'nir_move_vec_src_uses_to_dest.c',
index 76ee81ec0748c30d84af39c9ede728871bd2189a..a0b6db80b7669115c4b905838693b8b2a9fc3f5c 100644 (file)
@@ -3036,6 +3036,8 @@ void nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
 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_uniforms_to_ubo(nir_shader *shader);
+
 typedef struct nir_lower_subgroups_options {
    uint8_t subgroup_size;
    uint8_t ballot_bit_size;
diff --git a/src/compiler/nir/nir_lower_uniforms_to_ubo.c b/src/compiler/nir/nir_lower_uniforms_to_ubo.c
new file mode 100644 (file)
index 0000000..b54c994
--- /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"
+
+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_iadd(b, nir_imm_int(b, 4 * nir_intrinsic_base(instr)),
+                  nir_imul(b, nir_imm_int(b, 4),
+                           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 1e25f47e5093e987971df73c2401f05d7411159b..f482963a1acfc3b1e8cb35ac198effe956200054 100644 (file)
@@ -540,7 +540,6 @@ STATETRACKER_FILES = \
        state_tracker/st_nir_builtins.c \
        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 d8a5682f6a0b84134ae7d9c2320d9d89764a0dac..517506b12c4ace062664e345815d64b1bad37981 100644 (file)
@@ -584,7 +584,6 @@ files_libmesa_gallium = files(
   'state_tracker/st_nir_builtins.c',
   '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 a1e3b6233c6fe239fef706c2ec73d4a2a19aea15..84638fd88a51af77e02fa4fa69a6d96519233519 100644 (file)
@@ -991,7 +991,7 @@ st_finalize_nir(struct st_context *st, struct gl_program *prog,
    if (st->ctx->Const.PackedDriverUniformStorage) {
       NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,
                  (nir_lower_io_options)0);
-      NIR_PASS_V(nir, st_nir_lower_uniforms_to_ubo);
+      NIR_PASS_V(nir, nir_lower_uniforms_to_ubo);
    }
 
    st_nir_lower_samplers(screen, nir, shader_program, prog);
index d45ab3c4474fcfdca3ab1de8bfeb01bc9139249b..94eae84402e27717579d2e87ceb0c3466d122d5e 100644 (file)
@@ -36,7 +36,6 @@ 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_nir_lower_wpos_ytransform(struct nir_shader *nir,
                                   struct gl_program *prog,
index 3826d96a88820819844a56cff59dbf0c39bfa81c..ce00b5e16719217d28c8a05a13d0aef313442fd7 100644 (file)
@@ -66,7 +66,7 @@ st_nir_finish_builtin_shader(struct st_context *st,
    if (st->ctx->Const.PackedDriverUniformStorage) {
       NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,
                  (nir_lower_io_options)0);
-      NIR_PASS_V(nir, st_nir_lower_uniforms_to_ubo);
+      NIR_PASS_V(nir, nir_lower_uniforms_to_ubo);
    }
 
    struct pipe_shader_state state = {
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
deleted file mode 100644 (file)
index 1727f82..0000000
+++ /dev/null
@@ -1,100 +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 "compiler/nir/nir.h"
-#include "compiler/nir/nir_builder.h"
-#include "st_nir.h"
-
-#include "program/prog_parameter.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_iadd(b, nir_imm_int(b, 4 * nir_intrinsic_base(instr)),
-                  nir_imul(b, nir_imm_int(b, 4),
-                           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;
-}
-