Path: blob/21.2-virgl/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
4574 views
/*1* Copyright (C) 2014 Rob Clark <[email protected]>2*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, sublicense,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 the next11* paragraph) shall be included in all copies or substantial portions of the12* 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 NONINFRINGEMENT. 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, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include <err.h>27#include <fcntl.h>28#include <getopt.h>29#include <stdint.h>30#include <stdio.h>31#include <stdlib.h>32#include <sys/mman.h>33#include <sys/stat.h>34#include <sys/types.h>3536#include "nir/tgsi_to_nir.h"37#include "tgsi/tgsi_dump.h"38#include "tgsi/tgsi_parse.h"39#include "tgsi/tgsi_text.h"4041#include "ir3/instr-a3xx.h"42#include "ir3/ir3.h"43#include "ir3/ir3_compiler.h"44#include "ir3/ir3_gallium.h"45#include "ir3/ir3_nir.h"4647#include "main/mtypes.h"4849#include "compiler/glsl/gl_nir.h"50#include "compiler/glsl/glsl_to_nir.h"51#include "compiler/glsl/standalone.h"52#include "compiler/nir_types.h"53#include "compiler/spirv/nir_spirv.h"5455#include "pipe/p_context.h"5657static void58dump_info(struct ir3_shader_variant *so, const char *str)59{60uint32_t *bin;61const char *type = ir3_shader_stage(so);62bin = ir3_shader_assemble(so);63printf("; %s: %s\n", type, str);64ir3_shader_disasm(so, bin, stdout);65}6667static void68insert_sorted(struct exec_list *var_list, nir_variable *new_var)69{70nir_foreach_variable_in_list (var, var_list) {71if (var->data.location > new_var->data.location) {72exec_node_insert_node_before(&var->node, &new_var->node);73return;74}75}76exec_list_push_tail(var_list, &new_var->node);77}7879static void80sort_varyings(nir_shader *nir, nir_variable_mode mode)81{82struct exec_list new_list;83exec_list_make_empty(&new_list);84nir_foreach_variable_with_modes_safe (var, nir, mode) {85exec_node_remove(&var->node);86insert_sorted(&new_list, var);87}88exec_list_append(&nir->variables, &new_list);89}9091static void92fixup_varying_slots(nir_shader *nir, nir_variable_mode mode)93{94nir_foreach_variable_with_modes (var, nir, mode) {95if (var->data.location >= VARYING_SLOT_VAR0) {96var->data.location += 9;97} else if ((var->data.location >= VARYING_SLOT_TEX0) &&98(var->data.location <= VARYING_SLOT_TEX7)) {99var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;100}101}102}103104static struct ir3_compiler *compiler;105106static nir_shader *107load_glsl(unsigned num_files, char *const *files, gl_shader_stage stage)108{109static const struct standalone_options options = {110.glsl_version = 310,111.do_link = true,112.lower_precision = true,113};114struct gl_shader_program *prog;115const nir_shader_compiler_options *nir_options =116ir3_get_compiler_options(compiler);117static struct gl_context local_ctx;118119prog = standalone_compile_shader(&options, num_files, files, &local_ctx);120if (!prog)121errx(1, "couldn't parse `%s'", files[0]);122123nir_shader *nir = glsl_to_nir(&local_ctx, prog, stage, nir_options);124125/* required NIR passes: */126if (nir_options->lower_all_io_to_temps ||127nir->info.stage == MESA_SHADER_VERTEX ||128nir->info.stage == MESA_SHADER_GEOMETRY) {129NIR_PASS_V(nir, nir_lower_io_to_temporaries,130nir_shader_get_entrypoint(nir), true, true);131} else if (nir->info.stage == MESA_SHADER_FRAGMENT) {132NIR_PASS_V(nir, nir_lower_io_to_temporaries,133nir_shader_get_entrypoint(nir), true, false);134}135136NIR_PASS_V(nir, nir_lower_global_vars_to_local);137NIR_PASS_V(nir, nir_split_var_copies);138NIR_PASS_V(nir, nir_lower_var_copies);139140NIR_PASS_V(nir, nir_split_var_copies);141NIR_PASS_V(nir, nir_lower_var_copies);142nir_print_shader(nir, stdout);143NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true);144NIR_PASS_V(nir, gl_nir_lower_buffers, prog);145NIR_PASS_V(nir, nir_lower_atomics_to_ssbo);146nir_print_shader(nir, stdout);147148switch (stage) {149case MESA_SHADER_VERTEX:150nir_assign_var_locations(nir, nir_var_shader_in, &nir->num_inputs,151ir3_glsl_type_size);152153/* Re-lower global vars, to deal with any dead VS inputs. */154NIR_PASS_V(nir, nir_lower_global_vars_to_local);155156sort_varyings(nir, nir_var_shader_out);157nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,158ir3_glsl_type_size);159fixup_varying_slots(nir, nir_var_shader_out);160break;161case MESA_SHADER_FRAGMENT:162sort_varyings(nir, nir_var_shader_in);163nir_assign_var_locations(nir, nir_var_shader_in, &nir->num_inputs,164ir3_glsl_type_size);165fixup_varying_slots(nir, nir_var_shader_in);166nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,167ir3_glsl_type_size);168break;169case MESA_SHADER_COMPUTE:170case MESA_SHADER_KERNEL:171break;172default:173errx(1, "unhandled shader stage: %d", stage);174}175176nir_assign_var_locations(nir, nir_var_uniform, &nir->num_uniforms,177ir3_glsl_type_size);178179NIR_PASS_V(nir, nir_lower_system_values);180NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);181182NIR_PASS_V(nir, nir_lower_frexp);183NIR_PASS_V(nir, nir_lower_io,184nir_var_shader_in | nir_var_shader_out | nir_var_uniform,185ir3_glsl_type_size, (nir_lower_io_options)0);186NIR_PASS_V(nir, gl_nir_lower_samplers, prog);187188return nir;189}190191static int192read_file(const char *filename, void **ptr, size_t *size)193{194int fd, ret;195struct stat st;196197*ptr = MAP_FAILED;198199fd = open(filename, O_RDONLY);200if (fd == -1) {201warnx("couldn't open `%s'", filename);202return 1;203}204205ret = fstat(fd, &st);206if (ret)207errx(1, "couldn't stat `%s'", filename);208209*size = st.st_size;210*ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);211if (*ptr == MAP_FAILED)212errx(1, "couldn't map `%s'", filename);213214close(fd);215216return 0;217}218219static void220debug_func(void *priv, enum nir_spirv_debug_level level, size_t spirv_offset,221const char *message)222{223// printf("%s\n", message);224}225226static nir_shader *227load_spirv(const char *filename, const char *entry, gl_shader_stage stage)228{229const struct spirv_to_nir_options spirv_options = {230/* these caps are just make-believe */231.caps = {232.draw_parameters = true,233.float64 = true,234.image_read_without_format = true,235.image_write_without_format = true,236.int64 = true,237.variable_pointers = true,238},239.debug = {240.func = debug_func,241}242};243nir_shader *nir;244void *buf;245size_t size;246247read_file(filename, &buf, &size);248249nir = spirv_to_nir(buf, size / 4, NULL, 0, /* spec_entries */250stage, entry, &spirv_options,251ir3_get_compiler_options(compiler));252253nir_print_shader(nir, stdout);254255return nir;256}257258static const char *shortopts = "g:hv";259260static const struct option longopts[] = {261{"gpu", required_argument, 0, 'g'},262{"help", no_argument, 0, 'h'},263{"verbose", no_argument, 0, 'v'},264};265266static void267print_usage(void)268{269printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point "270"| (file.vert | file.frag)*>\n");271printf(" -g, --gpu GPU_ID - specify gpu-id (default 320)\n");272printf(" -h, --help - show this message\n");273printf(" -v, --verbose - verbose compiler/debug messages\n");274}275276int277main(int argc, char **argv)278{279int ret = 0, opt;280char *filenames[2];281int num_files = 0;282unsigned stage = 0;283struct ir3_shader_key key = {};284unsigned gpu_id = 320;285const char *info;286const char *spirv_entry = NULL;287void *ptr;288bool from_tgsi = false;289size_t size;290291while ((opt = getopt_long_only(argc, argv, shortopts, longopts, NULL)) !=292-1) {293switch (opt) {294case 'g':295gpu_id = strtol(optarg, NULL, 0);296break;297case 'v':298ir3_shader_debug |= IR3_DBG_OPTMSGS | IR3_DBG_DISASM;299break;300default:301printf("unrecognized arg: %c\n", opt);302FALLTHROUGH;303case 'h':304print_usage();305return 0;306}307}308309if (optind >= argc) {310fprintf(stderr, "no file specified!\n");311print_usage();312return 0;313}314315unsigned n = optind;316while (n < argc) {317char *filename = argv[n];318char *ext = strrchr(filename, '.');319320if (strcmp(ext, ".tgsi") == 0) {321if (num_files != 0)322errx(1, "in TGSI mode, only a single file may be specified");323from_tgsi = true;324} else if (strcmp(ext, ".spv") == 0) {325if (num_files != 0)326errx(1, "in SPIR-V mode, only a single file may be specified");327stage = MESA_SHADER_COMPUTE;328filenames[num_files++] = filename;329n++;330if (n == argc)331errx(1, "in SPIR-V mode, an entry point must be specified");332spirv_entry = argv[n];333n++;334} else if (strcmp(ext, ".comp") == 0) {335if (from_tgsi || spirv_entry)336errx(1, "cannot mix GLSL/TGSI/SPIRV");337if (num_files >= ARRAY_SIZE(filenames))338errx(1, "too many GLSL files");339stage = MESA_SHADER_COMPUTE;340} else if (strcmp(ext, ".frag") == 0) {341if (from_tgsi || spirv_entry)342errx(1, "cannot mix GLSL/TGSI/SPIRV");343if (num_files >= ARRAY_SIZE(filenames))344errx(1, "too many GLSL files");345stage = MESA_SHADER_FRAGMENT;346} else if (strcmp(ext, ".vert") == 0) {347if (from_tgsi)348errx(1, "cannot mix GLSL and TGSI");349if (num_files >= ARRAY_SIZE(filenames))350errx(1, "too many GLSL files");351stage = MESA_SHADER_VERTEX;352} else {353print_usage();354return -1;355}356357filenames[num_files++] = filename;358359n++;360}361362nir_shader *nir;363364compiler = ir3_compiler_create(NULL, gpu_id, false);365366if (from_tgsi) {367struct tgsi_token toks[65536];368const nir_shader_compiler_options *nir_options =369ir3_get_compiler_options(compiler);370371ret = read_file(filenames[0], &ptr, &size);372if (ret) {373print_usage();374return ret;375}376377if (ir3_shader_debug & IR3_DBG_OPTMSGS)378printf("%s\n", (char *)ptr);379380if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))381errx(1, "could not parse `%s'", filenames[0]);382383if (ir3_shader_debug & IR3_DBG_OPTMSGS)384tgsi_dump(toks, 0);385386nir = tgsi_to_nir_noscreen(toks, nir_options);387NIR_PASS_V(nir, nir_lower_global_vars_to_local);388} else if (spirv_entry) {389nir = load_spirv(filenames[0], spirv_entry, stage);390391NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,392ir3_glsl_type_size, (nir_lower_io_options)0);393394/* TODO do this somewhere else */395nir_lower_int64(nir);396nir_lower_system_values(nir);397nir_lower_compute_system_values(nir, NULL);398} else if (num_files > 0) {399nir = load_glsl(num_files, filenames, stage);400} else {401print_usage();402return -1;403}404405ir3_nir_lower_io_to_temporaries(nir);406ir3_finalize_nir(compiler, nir);407ir3_nir_post_finalize(compiler, nir);408409struct ir3_shader *shader = rzalloc_size(NULL, sizeof(*shader));410shader->compiler = compiler;411shader->type = stage;412shader->nir = nir;413414struct ir3_shader_variant *v = rzalloc_size(shader, sizeof(*v));415v->type = shader->type;416v->shader = shader;417v->key = key;418v->const_state = rzalloc_size(v, sizeof(*v->const_state));419420shader->variants = v;421shader->variant_count = 1;422423ir3_nir_lower_variant(v, nir);424425info = "NIR compiler";426ret = ir3_compile_shader_nir(compiler, v);427if (ret) {428fprintf(stderr, "compiler failed!\n");429return ret;430}431dump_info(v, info);432433return 0;434}435436437