aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
blob: afad792abe649658906c1c9a94a130a1df380fea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <err.h>

#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_text.h"
#include "tgsi/tgsi_dump.h"

#include "freedreno_util.h"

#include "ir3_compiler.h"
#include "instr-a3xx.h"
#include "ir3.h"

static void dump_reg(const char *name, uint32_t r)
{
	if (r != regid(63,0))
		debug_printf("; %s: r%d.%c\n", name, r >> 2, "xyzw"[r & 0x3]);
}

static void dump_semantic(struct ir3_shader_variant *so,
		unsigned sem, const char *name)
{
	uint32_t regid;
	regid = ir3_find_output_regid(so, ir3_semantic_name(sem, 0));
	dump_reg(name, regid);
}

static void dump_info(struct ir3_shader_variant *so, const char *str)
{
	uint32_t *bin;
	const char *type = (so->type == SHADER_VERTEX) ? "VERT" : "FRAG";

	// for debug, dump some before/after info:
	// TODO make gpu_id configurable on cmdline
	bin = ir3_shader_assemble(so, 320);
	if (fd_mesa_debug & FD_DBG_DISASM) {
		struct ir3_block *block = so->ir->block;
		struct ir3_register *reg;
		uint8_t regid;
		unsigned i;

		debug_printf("; %s: %s\n", type, str);

if (block) {
		for (i = 0; i < block->ninputs; i++) {
			if (!block->inputs[i]) {
				debug_printf("; in%d unused\n", i);
				continue;
			}
			reg = block->inputs[i]->regs[0];
			regid = reg->num;
			debug_printf("@in(%sr%d.%c)\tin%d\n",
					(reg->flags & IR3_REG_HALF) ? "h" : "",
					(regid >> 2), "xyzw"[regid & 0x3], i);
		}

		for (i = 0; i < block->noutputs; i++) {
			if (!block->outputs[i]) {
				debug_printf("; out%d unused\n", i);
				continue;
			}
			/* kill shows up as a virtual output.. skip it! */
			if (is_kill(block->outputs[i]))
				continue;
			reg = block->outputs[i]->regs[0];
			regid = reg->num;
			debug_printf("@out(%sr%d.%c)\tout%d\n",
					(reg->flags & IR3_REG_HALF) ? "h" : "",
					(regid >> 2), "xyzw"[regid & 0x3], i);
		}
} else {
		/* hack to deal w/ old compiler:
		 * TODO maybe we can just keep this path?  I guess should
		 * be good enough (other than not able to deal w/ half)
		 */
		for (i = 0; i < so->inputs_count; i++) {
			unsigned j, regid = so->inputs[i].regid;
			for (j = 0; j < so->inputs[i].ncomp; j++) {
				debug_printf("@in(r%d.%c)\tin%d\n",
						(regid >> 2), "xyzw"[regid & 0x3], (i * 4) + j);
				regid++;
			}
		}
		for (i = 0; i < so->outputs_count; i++) {
			unsigned j, regid = so->outputs[i].regid;
			for (j = 0; j < 4; j++) {
				debug_printf("@out(r%d.%c)\tout%d\n",
						(regid >> 2), "xyzw"[regid & 0x3], (i * 4) + j);
				regid++;
			}
		}
}

		disasm_a3xx(bin, so->info.sizedwords, 0, so->type);

		debug_printf("; %s: outputs:", type);
		for (i = 0; i < so->outputs_count; i++) {
			uint8_t regid = so->outputs[i].regid;
			ir3_semantic sem = so->outputs[i].semantic;
			debug_printf(" r%d.%c (%u:%u)",
					(regid >> 2), "xyzw"[regid & 0x3],
					sem2name(sem), sem2idx(sem));
		}
		debug_printf("\n");
		debug_printf("; %s: inputs:", type);
		for (i = 0; i < so->inputs_count; i++) {
			uint8_t regid = so->inputs[i].regid;
			ir3_semantic sem = so->inputs[i].semantic;
			debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
					(regid >> 2), "xyzw"[regid & 0x3],
					sem2name(sem), sem2idx(sem),
					so->inputs[i].compmask,
					so->inputs[i].inloc,
					so->inputs[i].bary);
		}
		debug_printf("\n");
	}

	/* print generic shader info: */
	debug_printf("; %s: %u instructions, %d half, %d full\n", type,
			so->info.instrs_count,
			so->info.max_half_reg + 1,
			so->info.max_reg + 1);

	/* print shader type specific info: */
	switch (so->type) {
	case SHADER_VERTEX:
		dump_semantic(so, TGSI_SEMANTIC_POSITION, "pos");
		dump_semantic(so, TGSI_SEMANTIC_PSIZE, "psize");
		break;
	case SHADER_FRAGMENT:
		dump_reg("pos (bary)", so->pos_regid);
		dump_semantic(so, TGSI_SEMANTIC_POSITION, "posz");
		dump_semantic(so, TGSI_SEMANTIC_COLOR, "color");
		/* these two are hard-coded since we don't know how to
		 * program them to anything but all 0's...
		 */
		if (so->frag_coord)
			debug_printf("; fragcoord: r0.x\n");
		if (so->frag_face)
			debug_printf("; fragface: hr0.x\n");
		break;
	case SHADER_COMPUTE:
		break;
	}
	free(bin);

	debug_printf("\n");
}


static int
read_file(const char *filename, void **ptr, size_t *size)
{
	int fd, ret;
	struct stat st;

	*ptr = MAP_FAILED;

	fd = open(filename, O_RDONLY);
	if (fd == -1) {
		warnx("couldn't open `%s'", filename);
		return 1;
	}

	ret = fstat(fd, &st);
	if (ret)
		errx(1, "couldn't stat `%s'", filename);

	*size = st.st_size;
	*ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	if (*ptr == MAP_FAILED)
		errx(1, "couldn't map `%s'", filename);

	close(fd);

	return 0;
}

static void reset_variant(struct ir3_shader_variant *v, const char *msg)
{
	printf("; %s\n", msg);
	v->inputs_count = 0;
	v->outputs_count = 0;
	v->total_in = 0;
	v->has_samp = false;
	v->immediates_count = 0;
}

static void print_usage(void)
{
	printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
	printf("    --verbose         - verbose compiler/debug messages\n");
	printf("    --binning-pass    - generate binning pass shader (VERT)\n");
	printf("    --color-two-side  - emulate two-sided color (FRAG)\n");
	printf("    --half-precision  - use half-precision\n");
	printf("    --alpha           - generate render-to-alpha shader (FRAG)\n");
	printf("    --saturate-s MASK - bitmask of samplers to saturate S coord\n");
	printf("    --saturate-t MASK - bitmask of samplers to saturate T coord\n");
	printf("    --saturate-r MASK - bitmask of samplers to saturate R coord\n");
	printf("    --nocp            - disable copy propagation\n");
	printf("    --help            - show this message\n");
}

int main(int argc, char **argv)
{
	int ret = 0, n = 1;
	const char *filename;
	struct tgsi_token toks[65536];
	struct tgsi_parse_context parse;
	struct ir3_shader_variant v;
	struct ir3_shader_key key = {};
	const char *info;
	void *ptr;
	size_t size;

	fd_mesa_debug |= FD_DBG_DISASM;

	/* cmdline args which impact shader variant get spit out in a
	 * comment on the first line..  a quick/dirty way to preserve
	 * that info so when ir3test recompiles the shader with a new
	 * compiler version, we use the same shader-key settings:
	 */
	debug_printf("; options:");

	while (n < argc) {
		if (!strcmp(argv[n], "--verbose")) {
			fd_mesa_debug |=  FD_DBG_OPTDUMP | FD_DBG_MSGS | FD_DBG_OPTMSGS;
			n++;
			continue;
		}

		if (!strcmp(argv[n], "--binning-pass")) {
			debug_printf(" %s", argv[n]);
			key.binning_pass = true;
			n++;
			continue;
		}

		if (!strcmp(argv[n], "--color-two-side")) {
			debug_printf(" %s", argv[n]);
			key.color_two_side = true;
			n++;
			continue;
		}

		if (!strcmp(argv[n], "--half-precision")) {
			debug_printf(" %s", argv[n]);
			key.half_precision = true;
			n++;
			continue;
		}

		if (!strcmp(argv[n], "--alpha")) {
			debug_printf(" %s", argv[n]);
			key.alpha = true;
			n++;
			continue;
		}

		if (!strcmp(argv[n], "--saturate-s")) {
			debug_printf(" %s %s", argv[n], argv[n+1]);
			key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
			n += 2;
			continue;
		}

		if (!strcmp(argv[n], "--saturate-t")) {
			debug_printf(" %s %s", argv[n], argv[n+1]);
			key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
			n += 2;
			continue;
		}

		if (!strcmp(argv[n], "--saturate-r")) {
			debug_printf(" %s %s", argv[n], argv[n+1]);
			key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
			n += 2;
			continue;
		}

		if (!strcmp(argv[n], "--nocp")) {
			fd_mesa_debug |= FD_DBG_NOCP;
			n++;
			continue;
		}

		if (!strcmp(argv[n], "--help")) {
			print_usage();
			return 0;
		}

		break;
	}
	debug_printf("\n");

	filename = argv[n];

	memset(&v, 0, sizeof(v));
	v.key = key;

	ret = read_file(filename, &ptr, &size);
	if (ret) {
		print_usage();
		return ret;
	}

	if (fd_mesa_debug & FD_DBG_OPTMSGS)
		debug_printf("%s\n", (char *)ptr);

	if (!tgsi_text_translate(ptr, toks, Elements(toks)))
		errx(1, "could not parse `%s'", filename);

	tgsi_parse_init(&parse, toks);
	switch (parse.FullHeader.Processor.Processor) {
	case TGSI_PROCESSOR_FRAGMENT:
		v.type = SHADER_FRAGMENT;
		break;
	case TGSI_PROCESSOR_VERTEX:
		v.type = SHADER_VERTEX;
		break;
	case TGSI_PROCESSOR_COMPUTE:
		v.type = SHADER_COMPUTE;
		break;
	}

	if (!(fd_mesa_debug & FD_DBG_NOOPT)) {
		/* with new compiler: */
		info = "new compiler";
		ret = ir3_compile_shader(&v, toks, key, true);

		if (ret) {
			reset_variant(&v, "new compiler failed, trying without copy propagation!");
			info = "new compiler (no copy propagation)";
			ret = ir3_compile_shader(&v, toks, key, false);
			if (ret)
				reset_variant(&v, "new compiler failed, trying fallback!\n");
		}
	}

	if (ret) {
		info = "old compiler";
		ret = ir3_compile_shader_old(&v, toks, key);
	}

	if (ret) {
		fprintf(stderr, "old compiler failed!\n");
		return ret;
	}
	dump_info(&v, info);
}