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