Mesa (main): microsoft/compiler: Load synthesized sysvals via lowered io

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Dec 20 16:44:24 UTC 2021


Module: Mesa
Branch: main
Commit: 64991d44a830b66f05643d9aae42e924330e616c
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=64991d44a830b66f05643d9aae42e924330e616c

Author: Jesse Natalie <jenatali at microsoft.com>
Date:   Sat Dec 11 10:49:21 2021 -0800

microsoft/compiler: Load synthesized sysvals via lowered io

Reviewed-by: Enrico Galli <enrico.galli at intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14175>

---

 src/microsoft/compiler/dxil_nir.c    | 40 ++++++++++++++++++++++++++++++
 src/microsoft/compiler/dxil_nir.h    |  1 +
 src/microsoft/compiler/nir_to_dxil.c | 48 +++---------------------------------
 3 files changed, 44 insertions(+), 45 deletions(-)

diff --git a/src/microsoft/compiler/dxil_nir.c b/src/microsoft/compiler/dxil_nir.c
index 88cfdb08a8c..9c5a1a2d66d 100644
--- a/src/microsoft/compiler/dxil_nir.c
+++ b/src/microsoft/compiler/dxil_nir.c
@@ -1617,6 +1617,46 @@ dxil_nir_lower_bool_input(struct nir_shader *s)
                                         lower_bool_input_impl, NULL);
 }
 
+static bool
+lower_sysval_to_load_input_impl(nir_builder *b, nir_instr *instr, void *data)
+{
+   if (instr->type != nir_instr_type_intrinsic)
+      return false;
+
+   nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+   gl_system_value sysval = SYSTEM_VALUE_MAX;
+   switch (intr->intrinsic) {
+   case nir_intrinsic_load_front_face:
+      sysval = SYSTEM_VALUE_FRONT_FACE;
+      break;
+   case nir_intrinsic_load_instance_id:
+      sysval = SYSTEM_VALUE_INSTANCE_ID;
+      break;
+   case nir_intrinsic_load_vertex_id_zero_base:
+      sysval = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
+      break;
+   default:
+      return false;
+   }
+
+   nir_variable **sysval_vars = (nir_variable **)data;
+   nir_variable *var = sysval_vars[sysval];
+   assert(var);
+
+   b->cursor = nir_before_instr(instr);
+   nir_ssa_def *result = nir_build_load_input(b, intr->dest.ssa.num_components, intr->dest.ssa.bit_size, nir_imm_int(b, 0),
+      .base = var->data.driver_location, .dest_type = nir_get_nir_type_for_glsl_type(var->type));
+   nir_ssa_def_rewrite_uses(&intr->dest.ssa, result);
+   return true;
+}
+
+bool
+dxil_nir_lower_sysval_to_load_input(nir_shader *s, nir_variable **sysval_vars)
+{
+   return nir_shader_instructions_pass(s, lower_sysval_to_load_input_impl,
+      nir_metadata_block_index | nir_metadata_dominance, sysval_vars);
+}
+
 /* Comparison function to sort io values so that first come normal varyings,
  * then system values, and then system generated values.
  */
diff --git a/src/microsoft/compiler/dxil_nir.h b/src/microsoft/compiler/dxil_nir.h
index 45fc7fb7524..f71568b2e8e 100644
--- a/src/microsoft/compiler/dxil_nir.h
+++ b/src/microsoft/compiler/dxil_nir.h
@@ -50,6 +50,7 @@ bool dxil_nir_lower_system_values_to_zero(nir_shader *shader,
                                           uint32_t count);
 bool dxil_nir_split_typed_samplers(nir_shader *shader);
 bool dxil_nir_lower_bool_input(struct nir_shader *s);
+bool dxil_nir_lower_sysval_to_load_input(nir_shader *s, nir_variable **sysval_vars);
 
 nir_ssa_def *
 build_load_ubo_dxil(nir_builder *b, nir_ssa_def *buffer,
diff --git a/src/microsoft/compiler/nir_to_dxil.c b/src/microsoft/compiler/nir_to_dxil.c
index 319940a9409..12662dad8e1 100644
--- a/src/microsoft/compiler/nir_to_dxil.c
+++ b/src/microsoft/compiler/nir_to_dxil.c
@@ -2728,42 +2728,6 @@ emit_store_output_via_intrinsic(struct ntd_context *ctx, nir_intrinsic_instr *in
    return success;
 }
 
-static bool
-emit_load_input_interpolated(struct ntd_context *ctx, nir_intrinsic_instr *intr, nir_variable *var)
-{
-   assert(var);
-   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_LOAD_INPUT);
-   const struct dxil_value *input_id = dxil_module_get_int32_const(&ctx->mod, var->data.driver_location);
-   const struct dxil_value *row = dxil_module_get_int32_const(&ctx->mod, 0);
-   const struct dxil_type *int32_type = dxil_module_get_int_type(&ctx->mod, 32);
-   const struct dxil_value *vertex_id = dxil_module_get_undef(&ctx->mod, int32_type);
-
-   if (!opcode || !input_id || !row || !int32_type || !vertex_id)
-      return false;
-
-   nir_alu_type out_type = nir_get_nir_type_for_glsl_base_type(glsl_get_base_type(var->type));
-   enum overload_type overload = get_overload(out_type, glsl_get_bit_size(var->type));
-
-   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.loadInput", overload);
-
-   if (!func)
-      return false;
-
-   for (unsigned i = 0; i < nir_dest_num_components(intr->dest); ++i) {
-      const struct dxil_value *comp = dxil_module_get_int8_const(&ctx->mod, i);
-
-      const struct dxil_value *args[] = {
-         opcode, input_id, row, comp, vertex_id
-      };
-
-      const struct dxil_value *retval = dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
-      if (!retval)
-         return false;
-      store_dest(ctx, &intr->dest, i, retval, out_type);
-   }
-   return true;
-}
-
 static bool
 emit_load_input_via_intrinsic(struct ntd_context *ctx, nir_intrinsic_instr *intr)
 {
@@ -3460,15 +3424,6 @@ emit_intrinsic(struct ntd_context *ctx, nir_intrinsic_instr *intr)
       return emit_load_ubo(ctx, intr);
    case nir_intrinsic_load_ubo_dxil:
       return emit_load_ubo_dxil(ctx, intr);
-   case nir_intrinsic_load_front_face:
-      return emit_load_input_interpolated(ctx, intr,
-                                          ctx->system_value[SYSTEM_VALUE_FRONT_FACE]);
-   case nir_intrinsic_load_vertex_id_zero_base:
-      return emit_load_input_interpolated(ctx, intr,
-                                          ctx->system_value[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE]);
-   case nir_intrinsic_load_instance_id:
-      return emit_load_input_interpolated(ctx, intr,
-                                          ctx->system_value[SYSTEM_VALUE_INSTANCE_ID]);
    case nir_intrinsic_load_primitive_id:
       return emit_load_unary_external_function(ctx, intr, "dx.op.primitiveID",
                                                DXIL_INTR_PRIMITIVE_ID);
@@ -4778,6 +4733,9 @@ nir_to_dxil(struct nir_shader *s, const struct nir_to_dxil_options *opts,
    if (!allocate_sysvalues(ctx))
       return false;
 
+   NIR_PASS_V(s, dxil_nir_lower_sysval_to_load_input, ctx->system_value);
+   NIR_PASS_V(s, nir_opt_dce);
+
    if (debug_dxil & DXIL_DEBUG_VERBOSE)
       nir_print_shader(s, stderr);
 



More information about the mesa-commit mailing list