From cc78a425772a377c309fe1a9cb02186a834621a8 Mon Sep 17 00:00:00 2001 From: Connor Abbott Date: Fri, 3 Nov 2017 17:34:32 -0400 Subject: [PATCH] lima: Reintroduce the standalone compiler I used this to test things without needing to have a device handy. Acked-by: Qiang Yu --- meson_options.txt | 2 +- src/gallium/drivers/lima/lima_program.c | 4 +- src/gallium/drivers/lima/lima_program.h | 5 + src/gallium/drivers/lima/meson.build | 26 +++ src/gallium/drivers/lima/standalone/glsl.cpp | 41 ++++ src/gallium/drivers/lima/standalone/glsl.h | 36 +++ .../lima/standalone/lima_compiler_cmdline.c | 241 +++++++++++++++++++++ 7 files changed, 352 insertions(+), 3 deletions(-) create mode 100644 src/gallium/drivers/lima/standalone/glsl.cpp create mode 100644 src/gallium/drivers/lima/standalone/glsl.h create mode 100644 src/gallium/drivers/lima/standalone/lima_compiler_cmdline.c diff --git a/meson_options.txt b/meson_options.txt index 2a549c7..5cbb856 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -310,7 +310,7 @@ option( 'tools', type : 'array', value : [], - choices : ['etnaviv', 'freedreno', 'glsl', 'intel', 'intel-ui', 'nir', 'nouveau', 'xvmc', 'all'], + choices : ['etnaviv', 'freedreno', 'glsl', 'intel', 'intel-ui', 'nir', 'nouveau', 'xvmc', 'lima', 'all'], description : 'List of tools to build. (Note: `intel-ui` selects `intel`)', ) option( diff --git a/src/gallium/drivers/lima/lima_program.c b/src/gallium/drivers/lima/lima_program.c index 5e92726..cd056fb 100644 --- a/src/gallium/drivers/lima/lima_program.c +++ b/src/gallium/drivers/lima/lima_program.c @@ -86,7 +86,7 @@ type_size(const struct glsl_type *type, bool bindless) return glsl_count_attribute_slots(type, false); } -static void +void lima_program_optimize_vs_nir(struct nir_shader *s) { bool progress; @@ -130,7 +130,7 @@ lima_program_optimize_vs_nir(struct nir_shader *s) nir_sweep(s); } -static void +void lima_program_optimize_fs_nir(struct nir_shader *s) { bool progress; diff --git a/src/gallium/drivers/lima/lima_program.h b/src/gallium/drivers/lima/lima_program.h index 9b5e676..acbc18d 100644 --- a/src/gallium/drivers/lima/lima_program.h +++ b/src/gallium/drivers/lima/lima_program.h @@ -31,5 +31,10 @@ const void *lima_program_get_compiler_options(enum pipe_shader_type shader); bool lima_update_vs_state(struct lima_context *ctx); bool lima_update_fs_state(struct lima_context *ctx); +struct nir_shader; + +void lima_program_optimize_vs_nir(struct nir_shader *s); + +void lima_program_optimize_fs_nir(struct nir_shader *s); #endif diff --git a/src/gallium/drivers/lima/meson.build b/src/gallium/drivers/lima/meson.build index b6a197f..1db8c7e 100644 --- a/src/gallium/drivers/lima/meson.build +++ b/src/gallium/drivers/lima/meson.build @@ -86,3 +86,29 @@ driver_lima = declare_dependency( link_with : [liblima, liblimawinsys, libpanfrost_shared], dependencies : idep_nir, ) + + +lima_compiler = executable( + 'lima_compiler', + files( + 'standalone/lima_compiler_cmdline.c', + 'standalone/glsl.cpp' + ), + include_directories : [ + inc_src, inc_include, inc_gallium, inc_gallium_aux, inc_gallium_drivers, inc_mesa, inc_mapi, inc_compiler, + ], + dependencies : [ + dep_thread, + idep_nir, + ], + link_with : [ + liblima, + libgallium, + libglsl_standalone, + libmesa_util, + libpanfrost_shared, + ], + build_by_default : with_tools.contains('lima'), + install : with_tools.contains('lima'), +) + diff --git a/src/gallium/drivers/lima/standalone/glsl.cpp b/src/gallium/drivers/lima/standalone/glsl.cpp new file mode 100644 index 0000000..3cef682 --- /dev/null +++ b/src/gallium/drivers/lima/standalone/glsl.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017 Lima Project + * + * 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, sub license, + * 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 NON-INFRINGEMENT. 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 "standalone/glsl.h" +#include "compiler/glsl/ir.h" +#include "compiler/glsl/ir_optimization.h" + +void +lima_do_glsl_optimizations(struct exec_list *ir) +{ + _mesa_print_ir(stdout, ir, NULL); + do_mat_op_to_vec(ir); + _mesa_print_ir(stdout, ir, NULL); +} + +int +st_glsl_type_size(const glsl_type *type, bool bindless) +{ + return type->count_attribute_slots(false); +} diff --git a/src/gallium/drivers/lima/standalone/glsl.h b/src/gallium/drivers/lima/standalone/glsl.h new file mode 100644 index 0000000..63a272b --- /dev/null +++ b/src/gallium/drivers/lima/standalone/glsl.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017 Lima Project + * + * 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, sub license, + * 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 NON-INFRINGEMENT. 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. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +void lima_do_glsl_optimizations(struct exec_list *ir); + +int st_glsl_type_size(const struct glsl_type *type, bool bindless); + +#ifdef __cplusplus +} +#endif + diff --git a/src/gallium/drivers/lima/standalone/lima_compiler_cmdline.c b/src/gallium/drivers/lima/standalone/lima_compiler_cmdline.c new file mode 100644 index 0000000..7013504 --- /dev/null +++ b/src/gallium/drivers/lima/standalone/lima_compiler_cmdline.c @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2017 Lima Project + * + * 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, sub license, + * 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 NON-INFRINGEMENT. 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 +#include +#include + +#include "main/mtypes.h" + +#include "compiler/glsl/standalone.h" +#include "compiler/glsl/glsl_to_nir.h" +#include "compiler/glsl/gl_nir.h" +#include "compiler/nir_types.h" + +#include "lima_program.h" +#include "lima_context.h" +#include "ir/lima_ir.h" +#include "standalone/glsl.h" + +static void +print_usage(void) +{ + printf("Usage: lima_compiler [OPTIONS]... FILE\n"); + printf(" --help - show this message\n"); +} + +static void +insert_sorted(struct exec_list *var_list, nir_variable *new_var) +{ + nir_foreach_variable(var, var_list) { + if (var->data.location > new_var->data.location && + new_var->data.location >= 0) { + exec_node_insert_node_before(&var->node, &new_var->node); + return; + } + } + exec_list_push_tail(var_list, &new_var->node); +} + +static void +sort_varyings(struct exec_list *var_list) +{ + struct exec_list new_list; + exec_list_make_empty(&new_list); + nir_foreach_variable_safe(var, var_list) { + exec_node_remove(&var->node); + insert_sorted(&new_list, var); + } + exec_list_move_nodes_to(&new_list, var_list); +} + +static void +fixup_varying_slots(struct exec_list *var_list) +{ + nir_foreach_variable(var, var_list) { + if (var->data.location >= VARYING_SLOT_VAR0) { + var->data.location += 9; + } else if ((var->data.location >= VARYING_SLOT_TEX0) && + (var->data.location <= VARYING_SLOT_TEX7)) { + var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0; + } + } +} + +static nir_shader * +load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage) +{ + static const struct standalone_options options = { + .glsl_version = 110, + .do_link = false, + }; + unsigned shader = 0; + switch (stage) { + case MESA_SHADER_FRAGMENT: + shader = PIPE_SHADER_FRAGMENT; + break; + case MESA_SHADER_VERTEX: + shader = PIPE_SHADER_VERTEX; + break; + default: + unreachable("bad stage"); + } + struct gl_shader_program *prog; + const nir_shader_compiler_options *nir_options = + lima_program_get_compiler_options(shader); + static struct gl_context local_ctx; + + prog = standalone_compile_shader(&options, num_files, files, &local_ctx); + if (!prog) + errx(1, "couldn't parse `%s'", files[0]); + + lima_do_glsl_optimizations(prog->_LinkedShaders[stage]->ir); + + nir_shader *nir = glsl_to_nir(&local_ctx, prog, stage, nir_options); + + /* required NIR passes: */ + if (nir_options->lower_all_io_to_temps || + nir->info.stage == MESA_SHADER_VERTEX || + nir->info.stage == MESA_SHADER_GEOMETRY) { + NIR_PASS_V(nir, nir_lower_io_to_temporaries, + nir_shader_get_entrypoint(nir), + true, true); + } else if (nir->info.stage == MESA_SHADER_FRAGMENT) { + NIR_PASS_V(nir, nir_lower_io_to_temporaries, + nir_shader_get_entrypoint(nir), + true, false); + } + + NIR_PASS_V(nir, nir_lower_global_vars_to_local); + NIR_PASS_V(nir, nir_split_var_copies); + NIR_PASS_V(nir, nir_lower_var_copies); + + NIR_PASS_V(nir, nir_split_var_copies); + NIR_PASS_V(nir, nir_lower_var_copies); + nir_print_shader(nir, stdout); + NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true); + NIR_PASS_V(nir, nir_lower_atomics_to_ssbo, 8); + nir_print_shader(nir, stdout); + + switch (stage) { + case MESA_SHADER_VERTEX: + nir_assign_var_locations(&nir->inputs, &nir->num_inputs, + st_glsl_type_size); + + /* Re-lower global vars, to deal with any dead VS inputs. */ + NIR_PASS_V(nir, nir_lower_global_vars_to_local); + + sort_varyings(&nir->outputs); + nir_assign_var_locations(&nir->outputs, &nir->num_outputs, + st_glsl_type_size); + fixup_varying_slots(&nir->outputs); + break; + case MESA_SHADER_FRAGMENT: + sort_varyings(&nir->inputs); + nir_assign_var_locations(&nir->inputs, &nir->num_inputs, + st_glsl_type_size); + fixup_varying_slots(&nir->inputs); + nir_assign_var_locations(&nir->outputs, &nir->num_outputs, + st_glsl_type_size); + break; + default: + errx(1, "unhandled shader stage: %d", stage); + } + + nir_assign_var_locations(&nir->uniforms, + &nir->num_uniforms, + st_glsl_type_size); + + NIR_PASS_V(nir, nir_lower_system_values); + NIR_PASS_V(nir, nir_lower_frexp); + NIR_PASS_V(nir, gl_nir_lower_samplers, prog); + + return nir; +} + +int +main(int argc, char **argv) +{ + int n; + + lima_debug = LIMA_DEBUG_GP | LIMA_DEBUG_PP; + + if (argc < 2) { + print_usage(); + return 1; + } + + for (n = 1; n < argc; n++) { + if (!strcmp(argv[n], "--help")) { + print_usage(); + return 1; + } + + break; + } + + char *filename[10] = {0}; + filename[0] = argv[n]; + + char *ext = rindex(filename[0], '.'); + unsigned stage = 0; + + if (!strcmp(ext, ".frag")) { + stage = MESA_SHADER_FRAGMENT; + } + else if (!strcmp(ext, ".vert")) { + stage = MESA_SHADER_VERTEX; + } + else { + print_usage(); + return -1; + } + + nir_shader *nir = load_glsl(1, filename, stage); + + switch (stage) { + case MESA_SHADER_VERTEX: + lima_program_optimize_vs_nir(nir); + + nir_print_shader(nir, stdout); + + struct lima_vs_shader_state *vs = ralloc(nir, struct lima_vs_shader_state); + gpir_compile_nir(vs, nir); + break; + case MESA_SHADER_FRAGMENT: + lima_program_optimize_fs_nir(nir); + + nir_print_shader(nir, stdout); + + struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state); + struct ra_regs *ra = ppir_regalloc_init(NULL); + ppir_compile_nir(so, nir, ra); + break; + default: + errx(1, "unhandled shader stage: %d", stage); + } + + ralloc_free(nir); + return 0; +} -- 2.7.4