Merge ../mesa into vulkan
[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 static void dump_info(struct ir3_shader_variant *so, const char *str)
48 {
49 uint32_t *bin;
50 const char *type = ir3_shader_stage(so->shader);
51 // TODO make gpu_id configurable on cmdline
52 bin = ir3_shader_assemble(so, 320);
53 debug_printf("; %s: %s\n", type, str);
54 ir3_shader_disasm(so, bin);
55 free(bin);
56 }
57
58
59 static int
60 read_file(const char *filename, void **ptr, size_t *size)
61 {
62 int fd, ret;
63 struct stat st;
64
65 *ptr = MAP_FAILED;
66
67 fd = open(filename, O_RDONLY);
68 if (fd == -1) {
69 warnx("couldn't open `%s'", filename);
70 return 1;
71 }
72
73 ret = fstat(fd, &st);
74 if (ret)
75 errx(1, "couldn't stat `%s'", filename);
76
77 *size = st.st_size;
78 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
79 if (*ptr == MAP_FAILED)
80 errx(1, "couldn't map `%s'", filename);
81
82 close(fd);
83
84 return 0;
85 }
86
87 static void print_usage(void)
88 {
89 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
90 printf(" --verbose - verbose compiler/debug messages\n");
91 printf(" --binning-pass - generate binning pass shader (VERT)\n");
92 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
93 printf(" --half-precision - use half-precision\n");
94 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
95 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
96 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
97 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
98 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
99 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
100 printf(" --help - show this message\n");
101 }
102
103 int main(int argc, char **argv)
104 {
105 int ret = 0, n = 1;
106 const char *filename;
107 struct tgsi_token toks[65536];
108 struct tgsi_parse_context parse;
109 struct ir3_shader_variant v;
110 struct ir3_shader s;
111 struct ir3_shader_key key = {};
112 /* TODO cmdline option to target different gpus: */
113 unsigned gpu_id = 320;
114 const char *info;
115 void *ptr;
116 size_t size;
117
118 memset(&s, 0, sizeof(s));
119 memset(&v, 0, sizeof(v));
120
121 /* cmdline args which impact shader variant get spit out in a
122 * comment on the first line.. a quick/dirty way to preserve
123 * that info so when ir3test recompiles the shader with a new
124 * compiler version, we use the same shader-key settings:
125 */
126 debug_printf("; options:");
127
128 while (n < argc) {
129 if (!strcmp(argv[n], "--verbose")) {
130 fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS | FD_DBG_DISASM;
131 n++;
132 continue;
133 }
134
135 if (!strcmp(argv[n], "--binning-pass")) {
136 debug_printf(" %s", argv[n]);
137 key.binning_pass = true;
138 n++;
139 continue;
140 }
141
142 if (!strcmp(argv[n], "--color-two-side")) {
143 debug_printf(" %s", argv[n]);
144 key.color_two_side = true;
145 n++;
146 continue;
147 }
148
149 if (!strcmp(argv[n], "--half-precision")) {
150 debug_printf(" %s", argv[n]);
151 key.half_precision = true;
152 n++;
153 continue;
154 }
155
156 if (!strcmp(argv[n], "--saturate-s")) {
157 debug_printf(" %s %s", argv[n], argv[n+1]);
158 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
159 n += 2;
160 continue;
161 }
162
163 if (!strcmp(argv[n], "--saturate-t")) {
164 debug_printf(" %s %s", argv[n], argv[n+1]);
165 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
166 n += 2;
167 continue;
168 }
169
170 if (!strcmp(argv[n], "--saturate-r")) {
171 debug_printf(" %s %s", argv[n], argv[n+1]);
172 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
173 n += 2;
174 continue;
175 }
176
177 if (!strcmp(argv[n], "--stream-out")) {
178 struct pipe_stream_output_info *so = &s.stream_output;
179 debug_printf(" %s", argv[n]);
180 /* TODO more dynamic config based on number of outputs, etc
181 * rather than just hard-code for first output:
182 */
183 so->num_outputs = 1;
184 so->stride[0] = 4;
185 so->output[0].register_index = 0;
186 so->output[0].start_component = 0;
187 so->output[0].num_components = 4;
188 so->output[0].output_buffer = 0;
189 so->output[0].dst_offset = 2;
190 so->output[0].stream = 0;
191 n++;
192 continue;
193 }
194
195 if (!strcmp(argv[n], "--ucp")) {
196 debug_printf(" %s %s", argv[n], argv[n+1]);
197 key.ucp_enables = strtol(argv[n+1], NULL, 0);
198 n += 2;
199 continue;
200 }
201
202 if (!strcmp(argv[n], "--gpu")) {
203 debug_printf(" %s %s", argv[n], argv[n+1]);
204 gpu_id = strtol(argv[n+1], NULL, 0);
205 n += 2;
206 continue;
207 }
208
209 if (!strcmp(argv[n], "--help")) {
210 print_usage();
211 return 0;
212 }
213
214 break;
215 }
216 debug_printf("\n");
217
218 filename = argv[n];
219
220 ret = read_file(filename, &ptr, &size);
221 if (ret) {
222 print_usage();
223 return ret;
224 }
225
226 if (fd_mesa_debug & FD_DBG_OPTMSGS)
227 debug_printf("%s\n", (char *)ptr);
228
229 if (!tgsi_text_translate(ptr, toks, Elements(toks)))
230 errx(1, "could not parse `%s'", filename);
231
232 if (fd_mesa_debug & FD_DBG_OPTMSGS)
233 tgsi_dump(toks, 0);
234
235 nir_shader *nir = ir3_tgsi_to_nir(toks);
236 s.compiler = ir3_compiler_create(gpu_id);
237 s.nir = ir3_optimize_nir(&s, nir, NULL);
238
239 v.key = key;
240 v.shader = &s;
241
242 tgsi_parse_init(&parse, toks);
243 switch (parse.FullHeader.Processor.Processor) {
244 case TGSI_PROCESSOR_FRAGMENT:
245 s.type = v.type = SHADER_FRAGMENT;
246 break;
247 case TGSI_PROCESSOR_VERTEX:
248 s.type = v.type = SHADER_VERTEX;
249 break;
250 case TGSI_PROCESSOR_COMPUTE:
251 s.type = v.type = SHADER_COMPUTE;
252 break;
253 }
254
255 info = "NIR compiler";
256 ret = ir3_compile_shader_nir(s.compiler, &v);
257 if (ret) {
258 fprintf(stderr, "compiler failed!\n");
259 return ret;
260 }
261 dump_info(&v, info);
262 }