15d2bbde16a3f1f29685eda7bc276a16b045a745
[mesa.git] / src / gallium / drivers / panfrost / midgard / cmdline.c
1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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 "compiler/glsl/standalone.h"
25 #include "compiler/glsl/glsl_to_nir.h"
26 #include "compiler/nir_types.h"
27 #include "midgard_compile.h"
28 #include "disassemble.h"
29 #include "util/u_dynarray.h"
30 #include "main/mtypes.h"
31
32 bool c_do_mat_op_to_vec(struct exec_list *instructions);
33
34 static void
35 finalise_to_disk(const char *filename, struct util_dynarray *data)
36 {
37 FILE *fp;
38 fp = fopen(filename, "wb");
39 fwrite(data->data, 1, data->size, fp);
40 fclose(fp);
41
42 util_dynarray_fini(data);
43 }
44
45 static void
46 compile_shader(char **argv)
47 {
48 struct gl_shader_program *prog;
49 nir_shader *nir;
50
51 struct standalone_options options = {
52 .glsl_version = 140,
53 .do_link = true,
54 };
55
56 prog = standalone_compile_shader(&options, 2, argv);
57 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;
58
59 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
60 if (prog->_LinkedShaders[i] == NULL)
61 continue;
62
63 c_do_mat_op_to_vec(prog->_LinkedShaders[i]->ir);
64 }
65
66 midgard_program compiled;
67 nir = glsl_to_nir(prog, MESA_SHADER_VERTEX, &midgard_nir_options);
68 midgard_compile_shader_nir(nir, &compiled, false);
69 finalise_to_disk("vertex.bin", &compiled.compiled);
70
71 nir = glsl_to_nir(prog, MESA_SHADER_FRAGMENT, &midgard_nir_options);
72 midgard_compile_shader_nir(nir, &compiled, false);
73 finalise_to_disk("fragment.bin", &compiled.compiled);
74 }
75
76 static void
77 compile_blend(char **argv)
78 {
79 struct gl_shader_program *prog;
80 nir_shader *nir;
81
82 struct standalone_options options = {
83 .glsl_version = 140,
84 };
85
86 prog = standalone_compile_shader(&options, 1, argv);
87 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;
88
89 #if 0
90
91 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
92 if (prog->_LinkedShaders[i] == NULL)
93 continue;
94
95 c_do_mat_op_to_vec(prog->_LinkedShaders[i]->ir);
96 }
97
98 #endif
99
100 midgard_program program;
101 nir = glsl_to_nir(prog, MESA_SHADER_FRAGMENT, &midgard_nir_options);
102 midgard_compile_shader_nir(nir, &program, true);
103 finalise_to_disk("blend.bin", &program.compiled);
104 }
105
106 static void
107 disassemble(const char *filename)
108 {
109 FILE *fp = fopen(filename, "rb");
110 assert(fp);
111
112 fseek(fp, 0, SEEK_END);
113 int filesize = ftell(fp);
114 rewind(fp);
115
116 unsigned char *code = malloc(filesize);
117 fread(code, 1, filesize, fp);
118 fclose(fp);
119
120 disassemble_midgard(code, filesize);
121 free(code);
122 }
123
124 int
125 main(int argc, char **argv)
126 {
127 if (argc < 2) {
128 fprintf(stderr, "Usage: midgard_compiler command [args]\n");
129 fprintf(stderr, "midgard_compiler compile program.vert program.frag\n");
130 fprintf(stderr, "midgard_compiler blend program.blend\n");
131 fprintf(stderr, "midgard_compiler disasm binary.bin\n");
132 exit(1);
133 }
134
135 if (strcmp(argv[1], "compile") == 0) {
136 compile_shader(&argv[2]);
137 } else if (strcmp(argv[1], "blend") == 0) {
138 compile_blend(&argv[2]);
139 } else if (strcmp(argv[1], "disasm") == 0) {
140 disassemble(argv[2]);
141 } else {
142 fprintf(stderr, "Unknown command\n");
143 exit(1);
144 }
145 }