freedreno/ir3: fix cmdline compiler
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_cmdline.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <fcntl.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <err.h>
35
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_text.h"
38 #include "tgsi/tgsi_dump.h"
39
40 #include "freedreno_util.h"
41
42 #include "ir3_compiler.h"
43 #include "ir3_nir.h"
44 #include "instr-a3xx.h"
45 #include "ir3.h"
46
47 #include "compiler/glsl/standalone.h"
48 #include "compiler/glsl/glsl_to_nir.h"
49
50 static void dump_info(struct ir3_shader_variant *so, const char *str)
51 {
52 uint32_t *bin;
53 const char *type = ir3_shader_stage(so->shader);
54 bin = ir3_shader_assemble(so, so->shader->compiler->gpu_id);
55 debug_printf("; %s: %s\n", type, str);
56 ir3_shader_disasm(so, bin);
57 free(bin);
58 }
59
60 int st_glsl_type_size(const struct glsl_type *type);
61
62 static void
63 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
64 {
65 nir_foreach_variable(var, var_list) {
66 if (var->data.location > new_var->data.location) {
67 exec_node_insert_node_before(&var->node, &new_var->node);
68 return;
69 }
70 }
71 exec_list_push_tail(var_list, &new_var->node);
72 }
73
74 static void
75 sort_varyings(struct exec_list *var_list)
76 {
77 struct exec_list new_list;
78 exec_list_make_empty(&new_list);
79 nir_foreach_variable_safe(var, var_list) {
80 exec_node_remove(&var->node);
81 insert_sorted(&new_list, var);
82 }
83 exec_list_move_nodes_to(&new_list, var_list);
84 }
85
86 static void
87 fixup_varying_slots(struct exec_list *var_list)
88 {
89 nir_foreach_variable(var, var_list) {
90 if (var->data.location >= VARYING_SLOT_VAR0) {
91 var->data.location += 9;
92 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
93 (var->data.location <= VARYING_SLOT_TEX7)) {
94 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
95 }
96 }
97 }
98
99 static struct ir3_compiler *compiler;
100
101 static nir_shader *
102 load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
103 {
104 static const struct standalone_options options = {
105 .glsl_version = 140,
106 .do_link = true,
107 };
108 struct gl_shader_program *prog;
109
110 prog = standalone_compile_shader(&options, num_files, files);
111 if (!prog)
112 errx(1, "couldn't parse `%s'", files[0]);
113
114 nir_shader *nir = glsl_to_nir(prog, stage, ir3_get_compiler_options(compiler));
115
116 /* required NIR passes: */
117 /* TODO cmdline args for some of the conditional lowering passes? */
118
119 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
120 nir_shader_get_entrypoint(nir),
121 true, true);
122 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
123 NIR_PASS_V(nir, nir_split_var_copies);
124 NIR_PASS_V(nir, nir_lower_var_copies);
125
126 NIR_PASS_V(nir, nir_split_var_copies);
127 NIR_PASS_V(nir, nir_lower_var_copies);
128 NIR_PASS_V(nir, nir_lower_io_types);
129
130 switch (stage) {
131 case MESA_SHADER_VERTEX:
132 nir_assign_var_locations(&nir->inputs,
133 &nir->num_inputs,
134 st_glsl_type_size);
135
136 /* Re-lower global vars, to deal with any dead VS inputs. */
137 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
138
139 sort_varyings(&nir->outputs);
140 nir_assign_var_locations(&nir->outputs,
141 &nir->num_outputs,
142 st_glsl_type_size);
143 fixup_varying_slots(&nir->outputs);
144 break;
145 case MESA_SHADER_FRAGMENT:
146 sort_varyings(&nir->inputs);
147 nir_assign_var_locations(&nir->inputs,
148 &nir->num_inputs,
149 st_glsl_type_size);
150 fixup_varying_slots(&nir->inputs);
151 nir_assign_var_locations(&nir->outputs,
152 &nir->num_outputs,
153 st_glsl_type_size);
154 break;
155 default:
156 errx(1, "unhandled shader stage: %d", stage);
157 }
158
159 nir_assign_var_locations(&nir->uniforms,
160 &nir->num_uniforms,
161 st_glsl_type_size);
162
163 NIR_PASS_V(nir, nir_lower_system_values);
164 NIR_PASS_V(nir, nir_lower_io, nir_var_all, st_glsl_type_size, 0);
165 NIR_PASS_V(nir, nir_lower_samplers, prog);
166
167 return nir;
168 }
169
170 static int
171 read_file(const char *filename, void **ptr, size_t *size)
172 {
173 int fd, ret;
174 struct stat st;
175
176 *ptr = MAP_FAILED;
177
178 fd = open(filename, O_RDONLY);
179 if (fd == -1) {
180 warnx("couldn't open `%s'", filename);
181 return 1;
182 }
183
184 ret = fstat(fd, &st);
185 if (ret)
186 errx(1, "couldn't stat `%s'", filename);
187
188 *size = st.st_size;
189 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
190 if (*ptr == MAP_FAILED)
191 errx(1, "couldn't map `%s'", filename);
192
193 close(fd);
194
195 return 0;
196 }
197
198 static void print_usage(void)
199 {
200 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | (file.vert | file.frag)*>\n");
201 printf(" --verbose - verbose compiler/debug messages\n");
202 printf(" --binning-pass - generate binning pass shader (VERT)\n");
203 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
204 printf(" --half-precision - use half-precision\n");
205 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
206 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
207 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
208 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
209 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
210 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
211 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
212 printf(" --help - show this message\n");
213 }
214
215 int main(int argc, char **argv)
216 {
217 int ret = 0, n = 1;
218 char *filenames[2];
219 int num_files = 0;
220 unsigned stage = 0;
221 struct ir3_shader_variant v;
222 struct ir3_shader s;
223 struct ir3_shader_key key = {};
224 /* TODO cmdline option to target different gpus: */
225 unsigned gpu_id = 320;
226 const char *info;
227 void *ptr;
228 size_t size;
229
230 memset(&s, 0, sizeof(s));
231 memset(&v, 0, sizeof(v));
232
233 /* cmdline args which impact shader variant get spit out in a
234 * comment on the first line.. a quick/dirty way to preserve
235 * that info so when ir3test recompiles the shader with a new
236 * compiler version, we use the same shader-key settings:
237 */
238 debug_printf("; options:");
239
240 while (n < argc) {
241 if (!strcmp(argv[n], "--verbose")) {
242 fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS | FD_DBG_DISASM;
243 n++;
244 continue;
245 }
246
247 if (!strcmp(argv[n], "--binning-pass")) {
248 debug_printf(" %s", argv[n]);
249 key.binning_pass = true;
250 n++;
251 continue;
252 }
253
254 if (!strcmp(argv[n], "--color-two-side")) {
255 debug_printf(" %s", argv[n]);
256 key.color_two_side = true;
257 n++;
258 continue;
259 }
260
261 if (!strcmp(argv[n], "--half-precision")) {
262 debug_printf(" %s", argv[n]);
263 key.half_precision = true;
264 n++;
265 continue;
266 }
267
268 if (!strcmp(argv[n], "--saturate-s")) {
269 debug_printf(" %s %s", argv[n], argv[n+1]);
270 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
271 n += 2;
272 continue;
273 }
274
275 if (!strcmp(argv[n], "--saturate-t")) {
276 debug_printf(" %s %s", argv[n], argv[n+1]);
277 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
278 n += 2;
279 continue;
280 }
281
282 if (!strcmp(argv[n], "--saturate-r")) {
283 debug_printf(" %s %s", argv[n], argv[n+1]);
284 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
285 n += 2;
286 continue;
287 }
288
289 if (!strcmp(argv[n], "--astc-srgb")) {
290 debug_printf(" %s %s", argv[n], argv[n+1]);
291 key.vastc_srgb = key.fastc_srgb = strtol(argv[n+1], NULL, 0);
292 n += 2;
293 continue;
294 }
295
296 if (!strcmp(argv[n], "--stream-out")) {
297 struct pipe_stream_output_info *so = &s.stream_output;
298 debug_printf(" %s", argv[n]);
299 /* TODO more dynamic config based on number of outputs, etc
300 * rather than just hard-code for first output:
301 */
302 so->num_outputs = 1;
303 so->stride[0] = 4;
304 so->output[0].register_index = 0;
305 so->output[0].start_component = 0;
306 so->output[0].num_components = 4;
307 so->output[0].output_buffer = 0;
308 so->output[0].dst_offset = 2;
309 so->output[0].stream = 0;
310 n++;
311 continue;
312 }
313
314 if (!strcmp(argv[n], "--ucp")) {
315 debug_printf(" %s %s", argv[n], argv[n+1]);
316 key.ucp_enables = strtol(argv[n+1], NULL, 0);
317 n += 2;
318 continue;
319 }
320
321 if (!strcmp(argv[n], "--gpu")) {
322 debug_printf(" %s %s", argv[n], argv[n+1]);
323 gpu_id = strtol(argv[n+1], NULL, 0);
324 n += 2;
325 continue;
326 }
327
328 if (!strcmp(argv[n], "--help")) {
329 print_usage();
330 return 0;
331 }
332
333 break;
334 }
335 debug_printf("\n");
336
337 while (n < argc) {
338 char *filename = argv[n];
339 char *ext = rindex(filename, '.');
340
341 if (strcmp(ext, ".tgsi") == 0) {
342 if (num_files != 0)
343 errx(1, "in TGSI mode, only a single file may be specified");
344 s.from_tgsi = true;
345 } else if (strcmp(ext, ".frag") == 0) {
346 if (s.from_tgsi)
347 errx(1, "cannot mix GLSL and TGSI");
348 if (num_files >= ARRAY_SIZE(filenames))
349 errx(1, "too many GLSL files");
350 stage = MESA_SHADER_FRAGMENT;
351 } else if (strcmp(ext, ".vert") == 0) {
352 if (s.from_tgsi)
353 errx(1, "cannot mix GLSL and TGSI");
354 if (num_files >= ARRAY_SIZE(filenames))
355 errx(1, "too many GLSL files");
356 stage = MESA_SHADER_VERTEX;
357 } else {
358 print_usage();
359 return -1;
360 }
361
362 filenames[num_files++] = filename;
363
364 n++;
365 }
366
367 nir_shader *nir;
368
369 compiler = ir3_compiler_create(NULL, gpu_id);
370
371 if (s.from_tgsi) {
372 struct tgsi_token toks[65536];
373
374 ret = read_file(filenames[0], &ptr, &size);
375 if (ret) {
376 print_usage();
377 return ret;
378 }
379
380 if (fd_mesa_debug & FD_DBG_OPTMSGS)
381 debug_printf("%s\n", (char *)ptr);
382
383 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
384 errx(1, "could not parse `%s'", filenames[0]);
385
386 if (fd_mesa_debug & FD_DBG_OPTMSGS)
387 tgsi_dump(toks, 0);
388
389 nir = ir3_tgsi_to_nir(toks);
390 } else if (num_files > 0) {
391 nir = load_glsl(num_files, filenames, stage);
392 } else {
393 print_usage();
394 return -1;
395 }
396
397 s.compiler = compiler;
398 s.nir = ir3_optimize_nir(&s, nir, NULL);
399
400 v.key = key;
401 v.shader = &s;
402
403 switch (nir->stage) {
404 case MESA_SHADER_FRAGMENT:
405 s.type = v.type = SHADER_FRAGMENT;
406 break;
407 case MESA_SHADER_VERTEX:
408 s.type = v.type = SHADER_VERTEX;
409 break;
410 case MESA_SHADER_COMPUTE:
411 s.type = v.type = SHADER_COMPUTE;
412 break;
413 default:
414 errx(1, "unhandled shader stage: %d", nir->stage);
415 }
416
417 info = "NIR compiler";
418 ret = ir3_compile_shader_nir(s.compiler, &v);
419 if (ret) {
420 fprintf(stderr, "compiler failed!\n");
421 return ret;
422 }
423 dump_info(&v, info);
424 }