Path: blob/21.2-virgl/src/panfrost/bifrost/cmdline.c
4564 views
/*1* Copyright (C) 2019 Ryan Houdek <[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*/2223#include "disassemble.h"24#include "compiler.h"2526#include "main/mtypes.h"27#include "compiler/glsl/standalone.h"28#include "compiler/glsl/glsl_to_nir.h"29#include "compiler/glsl/gl_nir.h"30#include "compiler/nir_types.h"31#include "util/u_dynarray.h"32#include "bifrost_compile.h"3334static void35compile_shader(char **argv, bool vertex_only)36{37struct gl_shader_program *prog;38nir_shader *nir[2];39unsigned shader_types[2] = {40MESA_SHADER_VERTEX,41MESA_SHADER_FRAGMENT,42};4344struct standalone_options options = {45.glsl_version = 300, /* ES - needed for precision */46.do_link = true,47.lower_precision = true48};4950static struct gl_context local_ctx;5152prog = standalone_compile_shader(&options, 2, argv, &local_ctx);53prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;5455struct util_dynarray binary;5657util_dynarray_init(&binary, NULL);5859for (unsigned i = 0; i < 2; ++i) {60nir[i] = glsl_to_nir(&local_ctx, prog, shader_types[i], &bifrost_nir_options);61NIR_PASS_V(nir[i], nir_lower_global_vars_to_local);62NIR_PASS_V(nir[i], nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir[i]), true, i == 0);63NIR_PASS_V(nir[i], nir_split_var_copies);64NIR_PASS_V(nir[i], nir_lower_var_copies);6566/* before buffers and vars_to_ssa */67NIR_PASS_V(nir[i], gl_nir_lower_images, true);6869NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog);70NIR_PASS_V(nir[i], nir_opt_constant_folding);7172struct panfrost_compile_inputs inputs = {73.gpu_id = 0x7212, /* Mali G52 */74};75struct pan_shader_info info;7677util_dynarray_clear(&binary);78bifrost_compile_shader_nir(nir[i], &inputs, &binary, &info);7980if (vertex_only)81break;82}8384util_dynarray_fini(&binary);85}8687#define BI_FOURCC(ch0, ch1, ch2, ch3) ( \88(uint32_t)(ch0) | (uint32_t)(ch1) << 8 | \89(uint32_t)(ch2) << 16 | (uint32_t)(ch3) << 24)9091static void92disassemble(const char *filename, bool verbose)93{94FILE *fp = fopen(filename, "rb");95assert(fp);9697fseek(fp, 0, SEEK_END);98unsigned filesize = ftell(fp);99rewind(fp);100101uint32_t *code = malloc(filesize);102unsigned res = fread(code, 1, filesize, fp);103if (res != filesize) {104printf("Couldn't read full file\n");105}106fclose(fp);107108if (filesize && code[0] == BI_FOURCC('M', 'B', 'S', '2')) {109for (int i = 0; i < filesize / 4; ++i) {110if (code[i] != BI_FOURCC('O', 'B', 'J', 'C'))111continue;112113unsigned size = code[i + 1];114unsigned offset = i + 2;115116disassemble_bifrost(stdout, (uint8_t*)(code + offset), size, verbose);117}118} else {119disassemble_bifrost(stdout, (uint8_t*)code, filesize, verbose);120}121122free(code);123}124125static int126bi_tests()127{128#ifndef NDEBUG129bi_test_scheduler();130bi_test_packing();131bi_test_packing_formats();132printf("Pass.\n");133return 0;134#else135printf("Tests omitted in release mode.");136return 1;137#endif138}139140int141main(int argc, char **argv)142{143if (argc < 2) {144printf("Pass a command\n");145exit(1);146}147148if (strcmp(argv[1], "compile") == 0)149compile_shader(&argv[2], false);150else if (strcmp(argv[1], "disasm") == 0)151disassemble(argv[2], false);152else if (strcmp(argv[1], "disasm-verbose") == 0)153disassemble(argv[2], true);154else if (strcmp(argv[1], "test") == 0)155bi_tests();156else157unreachable("Unknown command. Valid: compile/disasm");158159return 0;160}161162163