st/nir: use NIR for asm programs
authorTimothy Arceri <tarceri@itsqueeze.com>
Wed, 9 May 2018 04:06:43 +0000 (14:06 +1000)
committerKenneth Graunke <kenneth@whitecape.org>
Tue, 19 Feb 2019 23:56:26 +0000 (15:56 -0800)
This uses prog_to_nir to translate ARB assembly programs to NIR.

Co-authored by Tim Arceri, Dave Airlie, and Ken Graunke:

 - [Tim Arceri]: original patch
 - [Dave Airlie]: fix crashes with parameter names
 - [Ken Graunke]:
   - Rebase on SCALAR_ISA cap, lower wpos_ytransform too.
   - Rebase on streamout fixes.
   - Lower system values for fragcoord support.
   - Don't try to use prog_to_nir for ATI_fragment_shader programs.
   - Create TGSI for fixed-function or ARB vertex shaders even if the
     driver prefers NIR, so we can create draw module shaders for
     feedback/select emulation, which rely on TGSI.

Tested on:
- iris (Intel Skylake/Kabylake): Piglit & GL CTS - Ken Graunke
- radeonsi (AMD Vega 64): Piglit - Ken Graunke
- vc4/v3d - Piglit - Eric Anholt
- freedreno - dEQP - Kristian Høgsberg

Fixes lit_degenerate_case on vc4 and v3d, and vp-address-01,
vp-arl-constant-array-huge-offset-neg, and vp-arl-neg-array on v3d.
No Piglit regressions on radeonsi; no dEQP regressions on freedreno.

Acked-by: Eric Anholt <eric@anholt.net>
Tested-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
src/mesa/program/prog_to_nir.c
src/mesa/state_tracker/st_program.c

index 9d4f6d63dccf4cf976782afb1dcb4acef0dcb193..312b299361ed0498ce6f2f609497ef7bfa60ddb6 100644 (file)
@@ -1024,7 +1024,7 @@ prog_to_nir(const struct gl_program *prog,
       c->parameters = rzalloc(s, nir_variable);
       c->parameters->type =
          glsl_array_type(glsl_vec4_type(), prog->Parameters->NumParameters, 0);
-      c->parameters->name = "parameters";
+      c->parameters->name = strdup(prog->Parameters->Parameters[0].Name);
       c->parameters->data.read_only = true;
       c->parameters->data.mode = nir_var_uniform;
       exec_list_push_tail(&s->uniforms, &c->parameters->node);
index 4d017d8c61d9fd9bbe6bc6f98251ac8c5ae57719..c2daa4d2ffa6b784495f4d88529666e420d6a33c 100644 (file)
@@ -37,6 +37,7 @@
 #include "main/mtypes.h"
 #include "program/prog_parameter.h"
 #include "program/prog_print.h"
+#include "program/prog_to_nir.h"
 #include "program/programopt.h"
 
 #include "compiler/nir/nir.h"
@@ -377,6 +378,37 @@ st_release_cp_variants(struct st_context *st, struct st_compute_program *stcp)
    }
 }
 
+/**
+ * Translate ARB (asm) program to NIR
+ */
+static nir_shader *
+st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
+                         gl_shader_stage stage)
+{
+   enum pipe_shader_type p_stage = stage; /* valid for VS/FS */
+   const bool is_scalar =
+      st->pipe->screen->get_shader_param(st->pipe->screen, p_stage,
+                                         PIPE_SHADER_CAP_SCALAR_ISA);
+
+   const struct gl_shader_compiler_options *options =
+      &st->ctx->Const.ShaderCompilerOptions[stage];
+
+   /* Translate to NIR */
+   nir_shader *nir = prog_to_nir(prog, options->NirOptions);
+   NIR_PASS_V(nir, nir_lower_regs_to_ssa); /* turn registers into SSA */
+   nir_validate_shader(nir, "after st/ptn lower_regs_to_ssa");
+
+   NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, st->pipe->screen);
+   NIR_PASS_V(nir, nir_lower_system_values);
+
+   /* Optimise NIR */
+   NIR_PASS_V(nir, nir_opt_constant_folding);
+   st_nir_opts(nir, is_scalar);
+   nir_validate_shader(nir, "after st/ptn NIR opts");
+
+   return nir;
+}
+
 /**
  * Translate a vertex program.
  */
@@ -539,6 +571,19 @@ st_translate_vertex_program(struct st_context *st,
       st_store_ir_in_disk_cache(st, &stvp->Base, false);
    }
 
+   bool use_nir = PIPE_SHADER_IR_NIR ==
+      st->pipe->screen->get_shader_param(st->pipe->screen, PIPE_SHADER_VERTEX,
+                                         PIPE_SHADER_CAP_PREFERRED_IR);
+
+   if (use_nir) {
+      nir_shader *nir =
+         st_translate_prog_to_nir(st, &stvp->Base, MESA_SHADER_VERTEX);
+
+      stvp->tgsi.type = PIPE_SHADER_IR_NIR;
+      stvp->tgsi.ir.nir = nir;
+      return true;
+   }
+
    return stvp->tgsi.tokens != NULL;
 }
 
@@ -708,6 +753,21 @@ st_translate_fragment_program(struct st_context *st,
       }
    }
 
+
+   bool use_nir = PIPE_SHADER_IR_NIR ==
+      st->pipe->screen->get_shader_param(st->pipe->screen,
+                                         PIPE_SHADER_FRAGMENT,
+                                         PIPE_SHADER_CAP_PREFERRED_IR);
+
+   if (use_nir && !stfp->ati_fs) {
+      nir_shader *nir =
+         st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT);
+
+      stfp->tgsi.type = PIPE_SHADER_IR_NIR;
+      stfp->tgsi.ir.nir = nir;
+      return true;
+   }
+
    /*
     * Convert Mesa program inputs to TGSI input register semantics.
     */