pan/bi: Call nir_lower_io_to_temporaries in cmdline
[mesa.git] / src / panfrost / bifrost / cmdline.c
1 /*
2 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "disassemble.h"
25
26 #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"
33
34 static void
35 compile_shader(char **argv)
36 {
37 struct gl_shader_program *prog;
38 nir_shader *nir[2];
39 unsigned shader_types[2] = {
40 MESA_SHADER_VERTEX,
41 MESA_SHADER_FRAGMENT,
42 };
43
44 struct standalone_options options = {
45 .glsl_version = 430,
46 .do_link = true,
47 };
48
49 static struct gl_context local_ctx;
50
51 prog = standalone_compile_shader(&options, 2, argv, &local_ctx);
52 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;
53
54 bifrost_program compiled;
55 for (unsigned i = 0; i < 2; ++i) {
56 nir[i] = glsl_to_nir(&local_ctx, prog, shader_types[i], &bifrost_nir_options);
57 NIR_PASS_V(nir[i], nir_lower_global_vars_to_local);
58 NIR_PASS_V(nir[i], nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir[i]), true, i == 0);
59 NIR_PASS_V(nir[i], nir_split_var_copies);
60 NIR_PASS_V(nir[i], nir_lower_var_copies);
61
62 /* before buffers and vars_to_ssa */
63 NIR_PASS_V(nir[i], gl_nir_lower_images, true);
64
65 NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog);
66 NIR_PASS_V(nir[i], nir_opt_constant_folding);
67
68 unsigned product_id = 0x7212; /* Mali G52 */
69 bifrost_compile_shader_nir(nir[i], &compiled, product_id);
70 }
71 }
72
73 static void
74 disassemble(const char *filename)
75 {
76 FILE *fp = fopen(filename, "rb");
77 assert(fp);
78
79 fseek(fp, 0, SEEK_END);
80 unsigned filesize = ftell(fp);
81 rewind(fp);
82
83 unsigned char *code = malloc(filesize);
84 unsigned res = fread(code, 1, filesize, fp);
85 if (res != filesize) {
86 printf("Couldn't read full file\n");
87 }
88 fclose(fp);
89
90 disassemble_bifrost(stdout, code, filesize, false);
91 free(code);
92 }
93
94 int
95 main(int argc, char **argv)
96 {
97 if (argc < 2) {
98 printf("Pass a command\n");
99 exit(1);
100 }
101
102 if (strcmp(argv[1], "compile") == 0)
103 compile_shader(&argv[2]);
104 else if (strcmp(argv[1], "disasm") == 0)
105 disassemble(argv[2]);
106 else
107 unreachable("Unknown command. Valid: compile/disasm");
108
109 return 0;
110 }