glsl/freedreno/panfrost: pass gl_context to the standalone compiler
[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 static struct gl_context local_ctx;
57
58 prog = standalone_compile_shader(&options, 2, argv, &local_ctx);
59 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;
60
61 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
62 if (prog->_LinkedShaders[i] == NULL)
63 continue;
64
65 c_do_mat_op_to_vec(prog->_LinkedShaders[i]->ir);
66 }
67
68 midgard_program compiled;
69 nir = glsl_to_nir(prog, MESA_SHADER_VERTEX, &midgard_nir_options);
70 midgard_compile_shader_nir(nir, &compiled, false);
71 finalise_to_disk("vertex.bin", &compiled.compiled);
72
73 nir = glsl_to_nir(prog, MESA_SHADER_FRAGMENT, &midgard_nir_options);
74 midgard_compile_shader_nir(nir, &compiled, false);
75 finalise_to_disk("fragment.bin", &compiled.compiled);
76 }
77
78 static void
79 compile_blend(char **argv)
80 {
81 struct gl_shader_program *prog;
82 nir_shader *nir;
83
84 struct standalone_options options = {
85 .glsl_version = 140,
86 };
87
88 static struct gl_context local_ctx;
89
90 prog = standalone_compile_shader(&options, 1, argv, &local_ctx);
91 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->info.stage = MESA_SHADER_FRAGMENT;
92
93 midgard_program program;
94 nir = glsl_to_nir(prog, MESA_SHADER_FRAGMENT, &midgard_nir_options);
95 midgard_compile_shader_nir(nir, &program, true);
96 finalise_to_disk("blend.bin", &program.compiled);
97 }
98
99 static void
100 disassemble(const char *filename)
101 {
102 FILE *fp = fopen(filename, "rb");
103 assert(fp);
104
105 fseek(fp, 0, SEEK_END);
106 int filesize = ftell(fp);
107 rewind(fp);
108
109 unsigned char *code = malloc(filesize);
110 fread(code, 1, filesize, fp);
111 fclose(fp);
112
113 disassemble_midgard(code, filesize);
114 free(code);
115 }
116
117 int
118 main(int argc, char **argv)
119 {
120 if (argc < 2) {
121 fprintf(stderr, "Usage: midgard_compiler command [args]\n");
122 fprintf(stderr, "midgard_compiler compile program.vert program.frag\n");
123 fprintf(stderr, "midgard_compiler blend program.blend\n");
124 fprintf(stderr, "midgard_compiler disasm binary.bin\n");
125 exit(1);
126 }
127
128 if (strcmp(argv[1], "compile") == 0) {
129 compile_shader(&argv[2]);
130 } else if (strcmp(argv[1], "blend") == 0) {
131 compile_blend(&argv[2]);
132 } else if (strcmp(argv[1], "disasm") == 0) {
133 disassemble(argv[2]);
134 } else {
135 fprintf(stderr, "Unknown command\n");
136 exit(1);
137 }
138 }