Path: blob/21.2-virgl/src/gallium/drivers/lima/standalone/lima_compiler_cmdline.c
4574 views
/*1* Copyright (c) 2017 Lima Project2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sub license,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the11* next paragraph) shall be included in all copies or substantial portions12* of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*22*/2324#include <err.h>25#include <stdio.h>26#include <string.h>2728#include "main/mtypes.h"2930#include "compiler/glsl/standalone.h"31#include "compiler/glsl/glsl_to_nir.h"32#include "compiler/glsl/gl_nir.h"33#include "compiler/nir_types.h"3435#include "lima_context.h"36#include "lima_program.h"37#include "ir/lima_ir.h"38#include "standalone/glsl.h"3940static void41print_usage(void)42{43printf("Usage: lima_compiler [OPTIONS]... FILE\n");44printf(" --help - show this message\n");45}4647static void48insert_sorted(struct exec_list *var_list, nir_variable *new_var)49{50nir_foreach_variable_in_list(var, var_list) {51if (var->data.location > new_var->data.location &&52new_var->data.location >= 0) {53exec_node_insert_node_before(&var->node, &new_var->node);54return;55}56}57exec_list_push_tail(var_list, &new_var->node);58}5960static void61sort_varyings(nir_shader *nir, nir_variable_mode mode)62{63struct exec_list new_list;64exec_list_make_empty(&new_list);65nir_foreach_variable_with_modes_safe(var, nir, mode) {66exec_node_remove(&var->node);67insert_sorted(&new_list, var);68}69exec_list_append(&nir->variables, &new_list);70}7172static void73fixup_varying_slots(nir_shader *nir, nir_variable_mode mode)74{75nir_foreach_variable_with_modes(var, nir, mode) {76if (var->data.location >= VARYING_SLOT_VAR0) {77var->data.location += 9;78} else if ((var->data.location >= VARYING_SLOT_TEX0) &&79(var->data.location <= VARYING_SLOT_TEX7)) {80var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;81}82}83}8485static nir_shader *86load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)87{88static const struct standalone_options options = {89.glsl_version = 110,90.do_link = false,91};92unsigned shader = 0;93switch (stage) {94case MESA_SHADER_FRAGMENT:95shader = PIPE_SHADER_FRAGMENT;96break;97case MESA_SHADER_VERTEX:98shader = PIPE_SHADER_VERTEX;99break;100default:101unreachable("bad stage");102}103struct gl_shader_program *prog;104const nir_shader_compiler_options *nir_options =105lima_program_get_compiler_options(shader);106static struct gl_context local_ctx;107108prog = standalone_compile_shader(&options, num_files, files, &local_ctx);109if (!prog)110errx(1, "couldn't parse `%s'", files[0]);111112lima_do_glsl_optimizations(prog->_LinkedShaders[stage]->ir);113114nir_shader *nir = glsl_to_nir(&local_ctx, prog, stage, nir_options);115116/* required NIR passes: */117if (nir_options->lower_all_io_to_temps ||118nir->info.stage == MESA_SHADER_VERTEX ||119nir->info.stage == MESA_SHADER_GEOMETRY) {120NIR_PASS_V(nir, nir_lower_io_to_temporaries,121nir_shader_get_entrypoint(nir),122true, true);123} else if (nir->info.stage == MESA_SHADER_FRAGMENT) {124NIR_PASS_V(nir, nir_lower_io_to_temporaries,125nir_shader_get_entrypoint(nir),126true, false);127}128129NIR_PASS_V(nir, nir_lower_global_vars_to_local);130NIR_PASS_V(nir, nir_split_var_copies);131NIR_PASS_V(nir, nir_lower_var_copies);132133NIR_PASS_V(nir, nir_split_var_copies);134NIR_PASS_V(nir, nir_lower_var_copies);135nir_print_shader(nir, stdout);136NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true);137NIR_PASS_V(nir, nir_lower_atomics_to_ssbo);138nir_print_shader(nir, stdout);139140switch (stage) {141case MESA_SHADER_VERTEX:142nir_assign_var_locations(nir, nir_var_shader_in, &nir->num_inputs,143st_glsl_type_size);144145/* Re-lower global vars, to deal with any dead VS inputs. */146NIR_PASS_V(nir, nir_lower_global_vars_to_local);147148sort_varyings(nir, nir_var_shader_out);149nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,150st_glsl_type_size);151fixup_varying_slots(nir, nir_var_shader_out);152break;153case MESA_SHADER_FRAGMENT:154sort_varyings(nir, nir_var_shader_in);155nir_assign_var_locations(nir, nir_var_shader_in, &nir->num_inputs,156st_glsl_type_size);157fixup_varying_slots(nir, nir_var_shader_in);158nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,159st_glsl_type_size);160break;161default:162errx(1, "unhandled shader stage: %d", stage);163}164165nir_assign_var_locations(nir, nir_var_uniform,166&nir->num_uniforms,167st_glsl_type_size);168169NIR_PASS_V(nir, nir_lower_system_values);170NIR_PASS_V(nir, nir_lower_frexp);171NIR_PASS_V(nir, gl_nir_lower_samplers, prog);172173return nir;174}175176int177main(int argc, char **argv)178{179int n;180181lima_debug = LIMA_DEBUG_GP | LIMA_DEBUG_PP;182183if (argc < 2) {184print_usage();185return 1;186}187188for (n = 1; n < argc; n++) {189if (!strcmp(argv[n], "--help")) {190print_usage();191return 1;192}193194break;195}196197char *filename[10] = {0};198filename[0] = argv[n];199200char *ext = rindex(filename[0], '.');201unsigned stage = 0;202203if (!strcmp(ext, ".frag")) {204stage = MESA_SHADER_FRAGMENT;205}206else if (!strcmp(ext, ".vert")) {207stage = MESA_SHADER_VERTEX;208}209else {210print_usage();211return -1;212}213214struct nir_lower_tex_options tex_options = {215.lower_txp = ~0u,216};217218nir_shader *nir = load_glsl(1, filename, stage);219220switch (stage) {221case MESA_SHADER_VERTEX:222lima_program_optimize_vs_nir(nir);223224nir_print_shader(nir, stdout);225226struct lima_vs_compiled_shader *vs = ralloc(nir, struct lima_vs_compiled_shader);227gpir_compile_nir(vs, nir, NULL);228break;229case MESA_SHADER_FRAGMENT:230lima_program_optimize_fs_nir(nir, &tex_options);231232nir_print_shader(nir, stdout);233234struct lima_fs_compiled_shader *so = rzalloc(NULL, struct lima_fs_compiled_shader);235struct ra_regs *ra = ppir_regalloc_init(NULL);236ppir_compile_nir(so, nir, ra, NULL);237break;238default:239errx(1, "unhandled shader stage: %d", stage);240}241242ralloc_free(nir);243return 0;244}245246247