zink: use nir_lower_uniforms_to_ubo
authorErik Faye-Lund <erik.faye-lund@collabora.com>
Fri, 24 Apr 2020 15:13:45 +0000 (17:13 +0200)
committerMarge Bot <eric+marge@anholt.net>
Tue, 5 May 2020 09:17:52 +0000 (09:17 +0000)
Instead of open-coding uniform -> UBO lowering, let's instead use the
one that already exists. This should make things a bit simpler going
forward.

Reviewed-by: Alyssa Rosenzweig <alyssa@collabora.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4734>

src/gallium/drivers/zink/zink_compiler.c

index 399e2cd4071b1bda3b8775e4bc37dfe4fff0851c..3d9a44ae1695d4ce43473f047ce97db5741b1d6f 100644 (file)
 
 #include "util/u_memory.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, 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);
-      assert(instr->dest.ssa.bit_size >= 8);
-      nir_intrinsic_set_align(load, instr->dest.ssa.bit_size / 8, 0);
-      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;
-}
-
-static bool
-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);
-      }
-   }
-
-   if (progress) {
-      assert(shader->num_uniforms > 0);
-      const struct glsl_type *type = glsl_array_type(glsl_vec4_type(),
-                                                     shader->num_uniforms, 0);
-      nir_variable *ubo = nir_variable_create(shader, nir_var_mem_ubo, type,
-                                              "uniform_0");
-      ubo->data.binding = 0;
-
-      struct glsl_struct_field field = {
-         .type = type,
-         .name = "data",
-         .location = -1,
-      };
-      ubo->interface_type =
-            glsl_interface_type(&field, 1, GLSL_INTERFACE_PACKING_STD430,
-                                false, "__ubo0_interface");
-   }
-
-   return progress;
-}
-
 static bool
 lower_discard_if_instr(nir_intrinsic_instr *instr, nir_builder *b)
 {
@@ -217,7 +135,7 @@ zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir)
 {
    struct zink_shader *ret = CALLOC_STRUCT(zink_shader);
 
-   NIR_PASS_V(nir, lower_uniforms_to_ubo);
+   NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 1);
    NIR_PASS_V(nir, nir_lower_clip_halfz);
    NIR_PASS_V(nir, nir_lower_regs_to_ssa);
    optimize_nir(nir);