2 * Copyright (C) 2018 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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:
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
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
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"
32 bool c_do_mat_op_to_vec(struct exec_list
*instructions
);
35 finalise_to_disk(const char *filename
, struct util_dynarray
*data
)
38 fp
= fopen(filename
, "wb");
39 fwrite(data
->data
, 1, data
->size
, fp
);
42 util_dynarray_fini(data
);
46 compile_shader(char **argv
)
48 struct gl_shader_program
*prog
;
51 struct standalone_options options
= {
56 prog
= standalone_compile_shader(&options
, 2, argv
);
57 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->info
.stage
= MESA_SHADER_FRAGMENT
;
59 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; ++i
) {
60 if (prog
->_LinkedShaders
[i
] == NULL
)
63 c_do_mat_op_to_vec(prog
->_LinkedShaders
[i
]->ir
);
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
);
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
);
77 compile_blend(char **argv
)
79 struct gl_shader_program
*prog
;
82 struct standalone_options options
= {
86 prog
= standalone_compile_shader(&options
, 1, argv
);
87 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
->info
.stage
= MESA_SHADER_FRAGMENT
;
89 midgard_program program
;
90 nir
= glsl_to_nir(prog
, MESA_SHADER_FRAGMENT
, &midgard_nir_options
);
91 midgard_compile_shader_nir(nir
, &program
, true);
92 finalise_to_disk("blend.bin", &program
.compiled
);
96 disassemble(const char *filename
)
98 FILE *fp
= fopen(filename
, "rb");
101 fseek(fp
, 0, SEEK_END
);
102 int filesize
= ftell(fp
);
105 unsigned char *code
= malloc(filesize
);
106 fread(code
, 1, filesize
, fp
);
109 disassemble_midgard(code
, filesize
);
114 main(int argc
, char **argv
)
117 fprintf(stderr
, "Usage: midgard_compiler command [args]\n");
118 fprintf(stderr
, "midgard_compiler compile program.vert program.frag\n");
119 fprintf(stderr
, "midgard_compiler blend program.blend\n");
120 fprintf(stderr
, "midgard_compiler disasm binary.bin\n");
124 if (strcmp(argv
[1], "compile") == 0) {
125 compile_shader(&argv
[2]);
126 } else if (strcmp(argv
[1], "blend") == 0) {
127 compile_blend(&argv
[2]);
128 } else if (strcmp(argv
[1], "disasm") == 0) {
129 disassemble(argv
[2]);
131 fprintf(stderr
, "Unknown command\n");