From 3d7611e9a6c63a5ec03d3f9a7f90fbf286ec72a3 Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Wed, 9 May 2018 14:06:43 +1000 Subject: [PATCH] st/nir: use NIR for asm programs MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Tested-by: Eric Anholt Reviewed-by: Timothy Arceri --- src/mesa/program/prog_to_nir.c | 2 +- src/mesa/state_tracker/st_program.c | 60 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/mesa/program/prog_to_nir.c b/src/mesa/program/prog_to_nir.c index 9d4f6d63dcc..312b299361e 100644 --- a/src/mesa/program/prog_to_nir.c +++ b/src/mesa/program/prog_to_nir.c @@ -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); diff --git a/src/mesa/state_tracker/st_program.c b/src/mesa/state_tracker/st_program.c index 4d017d8c61d..c2daa4d2ffa 100644 --- a/src/mesa/state_tracker/st_program.c +++ b/src/mesa/state_tracker/st_program.c @@ -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. */ -- 2.30.2