From ecceb076e5c99cea94b853a9600947fe78b3ca74 Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Wed, 4 Apr 2018 16:01:21 +1000 Subject: [PATCH] st/glsl: start spilling out common st glsl conversion code The NIR and TGSI paths are currently intertwined which makes it not only hard to follow but also makes it hard to take advantage of the differences in IR. Here we take the first step to splitting that path apart. With this we take the opportunity to no longer call the GLSL IR optimisation passes after the final lowering calls for NIR. We can instead just use the NIR passes which can produce better code and should also result in faster compile times. The speed-up can be measured in some dolphin uber shaders due to no longer calling lower_if_to_cond_assign() for example dolphin/ubershaders/120.shader_test goes from ~1.63 -> ~1.53 seconds on my machine. There are some code changes as a result of not calling lower_if_to_cond_assign(), this is because it flattens ifs that contain UBOs where as NIR's peephole select doesn't. This is were most of the regressions in Max Waves happens with shader-db. shader-db results (VEGA): Totals from affected shaders: SGPRS: 2349056 -> 2349640 (0.02 %) VGPRS: 1322160 -> 1323300 (0.09 %) Spilled SGPRs: 21190 -> 21527 (1.59 %) Spilled VGPRs: 99 -> 99 (0.00 %) Private memory VGPRs: 0 -> 0 (0.00 %) Scratch size: 72 -> 72 (0.00 %) dwords per thread Code Size: 57260904 -> 57270932 (0.02 %) bytes Compile Time: 1107186 -> 1022942 (-7.61 %) milliseconds LDS: 786 -> 786 (0.00 %) blocks Max Waves: 391932 -> 391619 (-0.08 %) Wait states: 0 -> 0 (0.00 %) Reviewed-by: Eric Anholt --- src/mesa/Makefile.sources | 2 + src/mesa/meson.build | 2 + src/mesa/state_tracker/st_cb_program.c | 2 +- src/mesa/state_tracker/st_glsl_to_ir.cpp | 172 +++++++++++++++++++++ src/mesa/state_tracker/st_glsl_to_ir.h | 36 +++++ src/mesa/state_tracker/st_glsl_to_tgsi.cpp | 127 +-------------- src/mesa/state_tracker/st_glsl_to_tgsi.h | 3 +- 7 files changed, 222 insertions(+), 122 deletions(-) create mode 100644 src/mesa/state_tracker/st_glsl_to_ir.cpp create mode 100644 src/mesa/state_tracker/st_glsl_to_ir.h diff --git a/src/mesa/Makefile.sources b/src/mesa/Makefile.sources index f482963a1ac..d554408d01b 100644 --- a/src/mesa/Makefile.sources +++ b/src/mesa/Makefile.sources @@ -521,6 +521,8 @@ STATETRACKER_FILES = \ state_tracker/st_gen_mipmap.c \ state_tracker/st_gen_mipmap.h \ state_tracker/st_gl_api.h \ + state_tracker/st_glsl_to_ir.cpp \ + state_tracker/st_glsl_to_ir.h \ state_tracker/st_glsl_to_nir.cpp \ state_tracker/st_glsl_to_tgsi.cpp \ state_tracker/st_glsl_to_tgsi.h \ diff --git a/src/mesa/meson.build b/src/mesa/meson.build index 517506b12c4..05a8bb5e100 100644 --- a/src/mesa/meson.build +++ b/src/mesa/meson.build @@ -565,6 +565,8 @@ files_libmesa_gallium = files( 'state_tracker/st_gen_mipmap.c', 'state_tracker/st_gen_mipmap.h', 'state_tracker/st_gl_api.h', + 'state_tracker/st_glsl_to_ir.cpp', + 'state_tracker/st_glsl_to_ir.h', 'state_tracker/st_glsl_to_nir.cpp', 'state_tracker/st_glsl_to_tgsi.cpp', 'state_tracker/st_glsl_to_tgsi.h', diff --git a/src/mesa/state_tracker/st_cb_program.c b/src/mesa/state_tracker/st_cb_program.c index 555fc5d5ad9..825a09d7fda 100644 --- a/src/mesa/state_tracker/st_cb_program.c +++ b/src/mesa/state_tracker/st_cb_program.c @@ -45,7 +45,7 @@ #include "st_program.h" #include "st_mesa_to_tgsi.h" #include "st_cb_program.h" -#include "st_glsl_to_tgsi.h" +#include "st_glsl_to_ir.h" #include "st_atifs_to_tgsi.h" diff --git a/src/mesa/state_tracker/st_glsl_to_ir.cpp b/src/mesa/state_tracker/st_glsl_to_ir.cpp new file mode 100644 index 00000000000..ea56bf432a6 --- /dev/null +++ b/src/mesa/state_tracker/st_glsl_to_ir.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2005-2007 Brian Paul All Rights Reserved. + * Copyright (C) 2008 VMware, Inc. All Rights Reserved. + * Copyright © 2010 Intel Corporation + * Copyright © 2011 Bryan Cain + * + * 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 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * 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 NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS 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. + */ + +#include "compiler/glsl/glsl_parser_extras.h" +#include "compiler/glsl/ir_optimization.h" +#include "compiler/glsl/program.h" + +#include "st_nir.h" +#include "st_shader_cache.h" +#include "st_glsl_to_tgsi.h" + +#include "tgsi/tgsi_from_mesa.h" + +extern "C" { + +/** + * Link a shader. + * Called via ctx->Driver.LinkShader() + * This is a shared function that branches off to either GLSL IR -> TGSI or + * GLSL IR -> NIR + */ +GLboolean +st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog) +{ + struct pipe_screen *pscreen = ctx->st->pipe->screen; + + enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir) + pscreen->get_shader_param(pscreen, PIPE_SHADER_VERTEX, + PIPE_SHADER_CAP_PREFERRED_IR); + bool use_nir = preferred_ir == PIPE_SHADER_IR_NIR; + + /* Return early if we are loading the shader from on-disk cache */ + if (st_load_ir_from_disk_cache(ctx, prog, use_nir)) { + return GL_TRUE; + } + + assert(prog->data->LinkStatus); + + for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { + if (prog->_LinkedShaders[i] == NULL) + continue; + + struct gl_linked_shader *shader = prog->_LinkedShaders[i]; + exec_list *ir = shader->ir; + gl_shader_stage stage = shader->Stage; + const struct gl_shader_compiler_options *options = + &ctx->Const.ShaderCompilerOptions[stage]; + + /* If there are forms of indirect addressing that the driver + * cannot handle, perform the lowering pass. + */ + if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput || + options->EmitNoIndirectTemp || options->EmitNoIndirectUniform) { + lower_variable_index_to_cond_assign(stage, ir, + options->EmitNoIndirectInput, + options->EmitNoIndirectOutput, + options->EmitNoIndirectTemp, + options->EmitNoIndirectUniform); + } + + enum pipe_shader_type ptarget = pipe_shader_type_from_mesa(stage); + bool have_dround = pscreen->get_shader_param(pscreen, ptarget, + PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED); + bool have_dfrexp = pscreen->get_shader_param(pscreen, ptarget, + PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED); + bool have_ldexp = pscreen->get_shader_param(pscreen, ptarget, + PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED); + + if (!pscreen->get_param(pscreen, PIPE_CAP_INT64_DIVMOD)) + lower_64bit_integer_instructions(ir, DIV64 | MOD64); + + if (ctx->Extensions.ARB_shading_language_packing) { + unsigned lower_inst = LOWER_PACK_SNORM_2x16 | + LOWER_UNPACK_SNORM_2x16 | + LOWER_PACK_UNORM_2x16 | + LOWER_UNPACK_UNORM_2x16 | + LOWER_PACK_SNORM_4x8 | + LOWER_UNPACK_SNORM_4x8 | + LOWER_UNPACK_UNORM_4x8 | + LOWER_PACK_UNORM_4x8; + + if (ctx->Extensions.ARB_gpu_shader5) + lower_inst |= LOWER_PACK_USE_BFI | + LOWER_PACK_USE_BFE; + if (!ctx->st->has_half_float_packing) + lower_inst |= LOWER_PACK_HALF_2x16 | + LOWER_UNPACK_HALF_2x16; + + lower_packing_builtins(ir, lower_inst); + } + + if (!pscreen->get_param(pscreen, PIPE_CAP_TEXTURE_GATHER_OFFSETS)) + lower_offset_arrays(ir); + do_mat_op_to_vec(ir); + + if (stage == MESA_SHADER_FRAGMENT) + lower_blend_equation_advanced( + shader, ctx->Extensions.KHR_blend_equation_advanced_coherent); + + lower_instructions(ir, + MOD_TO_FLOOR | + FDIV_TO_MUL_RCP | + EXP_TO_EXP2 | + LOG_TO_LOG2 | + MUL64_TO_MUL_AND_MUL_HIGH | + (have_ldexp ? 0 : LDEXP_TO_ARITH) | + (have_dfrexp ? 0 : DFREXP_DLDEXP_TO_ARITH) | + CARRY_TO_ARITH | + BORROW_TO_ARITH | + (have_dround ? 0 : DOPS_TO_DFRAC) | + (options->EmitNoPow ? POW_TO_EXP2 : 0) | + (!ctx->Const.NativeIntegers ? INT_DIV_TO_MUL_RCP : 0) | + (options->EmitNoSat ? SAT_TO_CLAMP : 0) | + (ctx->Const.ForceGLSLAbsSqrt ? SQRT_TO_ABS_SQRT : 0) | + /* Assume that if ARB_gpu_shader5 is not supported + * then all of the extended integer functions need + * lowering. It may be necessary to add some caps + * for individual instructions. + */ + (!ctx->Extensions.ARB_gpu_shader5 + ? BIT_COUNT_TO_MATH | + EXTRACT_TO_SHIFTS | + INSERT_TO_SHIFTS | + REVERSE_TO_SHIFTS | + FIND_LSB_TO_FLOAT_CAST | + FIND_MSB_TO_FLOAT_CAST | + IMUL_HIGH_TO_MUL + : 0)); + + do_vec_index_to_cond_assign(ir); + lower_vector_insert(ir, true); + lower_quadop_vector(ir, false); + lower_noise(ir); + if (options->MaxIfDepth == 0) { + lower_discard(ir); + } + + validate_ir_tree(ir); + } + + build_program_resource_list(ctx, prog); + + if (use_nir) + return st_link_nir(ctx, prog); + else + return st_link_tgsi(ctx, prog); +} + +} /* extern "C" */ diff --git a/src/mesa/state_tracker/st_glsl_to_ir.h b/src/mesa/state_tracker/st_glsl_to_ir.h new file mode 100644 index 00000000000..1239c66e907 --- /dev/null +++ b/src/mesa/state_tracker/st_glsl_to_ir.h @@ -0,0 +1,36 @@ +/* + * Copyright © 2010 Intel Corporation + * Copyright © 2011 Bryan Cain + * + * 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 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * 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 NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS 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. + */ + +#include "main/mtypes.h" + +#ifdef __cplusplus +extern "C" { +#endif + +GLboolean +st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog); + +#ifdef __cplusplus +} +#endif diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp index 5b7203542f3..f8771125f45 100644 --- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp +++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp @@ -53,8 +53,6 @@ #include "st_program.h" #include "st_mesa_to_tgsi.h" #include "st_format.h" -#include "st_nir.h" -#include "st_shader_cache.h" #include "st_glsl_to_tgsi_temprename.h" #include "util/hash_table.h" @@ -7287,131 +7285,29 @@ has_unsupported_control_flow(exec_list *ir, return visitor.unsupported; } -extern "C" { - /** * Link a shader. - * Called via ctx->Driver.LinkShader() * This actually involves converting GLSL IR into an intermediate TGSI-like IR * with code lowering and other optimizations. */ GLboolean -st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog) +st_link_tgsi(struct gl_context *ctx, struct gl_shader_program *prog) { struct pipe_screen *pscreen = ctx->st->pipe->screen; - enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir) - pscreen->get_shader_param(pscreen, PIPE_SHADER_VERTEX, - PIPE_SHADER_CAP_PREFERRED_IR); - bool use_nir = preferred_ir == PIPE_SHADER_IR_NIR; - - /* Return early if we are loading the shader from on-disk cache */ - if (st_load_ir_from_disk_cache(ctx, prog, use_nir)) { - return GL_TRUE; - } - - assert(prog->data->LinkStatus); - for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { - if (prog->_LinkedShaders[i] == NULL) + struct gl_linked_shader *shader = prog->_LinkedShaders[i]; + if (shader == NULL) continue; - struct gl_linked_shader *shader = prog->_LinkedShaders[i]; exec_list *ir = shader->ir; gl_shader_stage stage = shader->Stage; + enum pipe_shader_type ptarget = pipe_shader_type_from_mesa(stage); const struct gl_shader_compiler_options *options = &ctx->Const.ShaderCompilerOptions[stage]; - enum pipe_shader_type ptarget = pipe_shader_type_from_mesa(stage); - bool have_dround = pscreen->get_shader_param(pscreen, ptarget, - PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED); - bool have_dfrexp = pscreen->get_shader_param(pscreen, ptarget, - PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED); - bool have_ldexp = pscreen->get_shader_param(pscreen, ptarget, - PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED); + unsigned if_threshold = pscreen->get_shader_param(pscreen, ptarget, PIPE_SHADER_CAP_LOWER_IF_THRESHOLD); - - /* If there are forms of indirect addressing that the driver - * cannot handle, perform the lowering pass. - */ - if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput || - options->EmitNoIndirectTemp || options->EmitNoIndirectUniform) { - lower_variable_index_to_cond_assign(stage, ir, - options->EmitNoIndirectInput, - options->EmitNoIndirectOutput, - options->EmitNoIndirectTemp, - options->EmitNoIndirectUniform); - } - - if (!pscreen->get_param(pscreen, PIPE_CAP_INT64_DIVMOD)) - lower_64bit_integer_instructions(ir, DIV64 | MOD64); - - if (ctx->Extensions.ARB_shading_language_packing) { - unsigned lower_inst = LOWER_PACK_SNORM_2x16 | - LOWER_UNPACK_SNORM_2x16 | - LOWER_PACK_UNORM_2x16 | - LOWER_UNPACK_UNORM_2x16 | - LOWER_PACK_SNORM_4x8 | - LOWER_UNPACK_SNORM_4x8 | - LOWER_UNPACK_UNORM_4x8 | - LOWER_PACK_UNORM_4x8; - - if (ctx->Extensions.ARB_gpu_shader5) - lower_inst |= LOWER_PACK_USE_BFI | - LOWER_PACK_USE_BFE; - if (!ctx->st->has_half_float_packing) - lower_inst |= LOWER_PACK_HALF_2x16 | - LOWER_UNPACK_HALF_2x16; - - lower_packing_builtins(ir, lower_inst); - } - - if (!pscreen->get_param(pscreen, PIPE_CAP_TEXTURE_GATHER_OFFSETS)) - lower_offset_arrays(ir); - do_mat_op_to_vec(ir); - - if (stage == MESA_SHADER_FRAGMENT) - lower_blend_equation_advanced( - shader, ctx->Extensions.KHR_blend_equation_advanced_coherent); - - lower_instructions(ir, - MOD_TO_FLOOR | - FDIV_TO_MUL_RCP | - EXP_TO_EXP2 | - LOG_TO_LOG2 | - MUL64_TO_MUL_AND_MUL_HIGH | - (have_ldexp ? 0 : LDEXP_TO_ARITH) | - (have_dfrexp ? 0 : DFREXP_DLDEXP_TO_ARITH) | - CARRY_TO_ARITH | - BORROW_TO_ARITH | - (have_dround ? 0 : DOPS_TO_DFRAC) | - (options->EmitNoPow ? POW_TO_EXP2 : 0) | - (!ctx->Const.NativeIntegers ? INT_DIV_TO_MUL_RCP : 0) | - (options->EmitNoSat ? SAT_TO_CLAMP : 0) | - (ctx->Const.ForceGLSLAbsSqrt ? SQRT_TO_ABS_SQRT : 0) | - /* Assume that if ARB_gpu_shader5 is not supported - * then all of the extended integer functions need - * lowering. It may be necessary to add some caps - * for individual instructions. - */ - (!ctx->Extensions.ARB_gpu_shader5 - ? BIT_COUNT_TO_MATH | - EXTRACT_TO_SHIFTS | - INSERT_TO_SHIFTS | - REVERSE_TO_SHIFTS | - FIND_LSB_TO_FLOAT_CAST | - FIND_MSB_TO_FLOAT_CAST | - IMUL_HIGH_TO_MUL - : 0)); - - do_vec_index_to_cond_assign(ir); - lower_vector_insert(ir, true); - lower_quadop_vector(ir, false); - lower_noise(ir); - if (options->MaxIfDepth == 0) { - lower_discard(ir); - } - if (ctx->Const.GLSLOptimizeConservatively) { /* Do it once and repeat only if there's unsupported control flow. */ do { @@ -7437,17 +7333,6 @@ st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog) do_vec_index_to_cond_assign(ir); validate_ir_tree(ir); - } - - build_program_resource_list(ctx, prog); - - if (use_nir) - return st_link_nir(ctx, prog); - - for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { - struct gl_linked_shader *shader = prog->_LinkedShaders[i]; - if (shader == NULL) - continue; struct gl_program *linked_prog = get_mesa_program_tgsi(ctx, prog, shader); @@ -7466,6 +7351,8 @@ st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog) return GL_TRUE; } +extern "C" { + void st_translate_stream_output_info(struct gl_transform_feedback_info *info, const ubyte outputMapping[], diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.h b/src/mesa/state_tracker/st_glsl_to_tgsi.h index c8a87abca41..fb89c39fb88 100644 --- a/src/mesa/state_tracker/st_glsl_to_tgsi.h +++ b/src/mesa/state_tracker/st_glsl_to_tgsi.h @@ -58,7 +58,8 @@ enum pipe_error st_translate_program( void free_glsl_to_tgsi_visitor(struct glsl_to_tgsi_visitor *v); -GLboolean st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog); +GLboolean +st_link_tgsi(struct gl_context *ctx, struct gl_shader_program *prog); void st_translate_stream_output_info(struct gl_transform_feedback_info *info, -- 2.30.2