summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
blob: 94838416ece5bac2e386b994c439db3ea7ba5952 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * 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 <stdio.h>
#include <err.h>

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

#include "ir3/ir3_compiler.h"
#include "ir3/ir3_gallium.h"
#include "ir3/ir3_nir.h"
#include "ir3/instr-a3xx.h"
#include "ir3/ir3.h"

#include "compiler/glsl/standalone.h"
#include "compiler/glsl/glsl_to_nir.h"
#include "compiler/glsl/gl_nir.h"
#include "compiler/nir_types.h"
#include "compiler/spirv/nir_spirv.h"

static void dump_info(struct ir3_shader_variant *so, const char *str)
{
	uint32_t *bin;
	const char *type = ir3_shader_stage(so->shader);
	bin = ir3_shader_assemble(so, so->shader->compiler->gpu_id);
	debug_printf("; %s: %s\n", type, str);
	ir3_shader_disasm(so, bin, stdout);
	free(bin);
}

static void
insert_sorted(struct exec_list *var_list, nir_variable *new_var)
{
	nir_foreach_variable(var, var_list) {
		if (var->data.location > new_var->data.location) {
			exec_node_insert_node_before(&var->node, &new_var->node);
			return;
		}
	}
	exec_list_push_tail(var_list, &new_var->node);
}

static void
sort_varyings(struct exec_list *var_list)
{
	struct exec_list new_list;
	exec_list_make_empty(&new_list);
	nir_foreach_variable_safe(var, var_list) {
		exec_node_remove(&var->node);
		insert_sorted(&new_list, var);
	}
	exec_list_move_nodes_to(&new_list, var_list);
}

static void
fixup_varying_slots(struct exec_list *var_list)
{
	nir_foreach_variable(var, var_list) {
		if (var->data.location >= VARYING_SLOT_VAR0) {
			var->data.location += 9;
		} else if ((var->data.location >= VARYING_SLOT_TEX0) &&
				(var->data.location <= VARYING_SLOT_TEX7)) {
			var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
		}
	}
}

static struct ir3_compiler *compiler;

static nir_shader *
load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
{
	static const struct standalone_options options = {
			.glsl_version = 460,
			.do_link = true,
	};
	struct gl_shader_program *prog;
	const nir_shader_compiler_options *nir_options =
			ir3_get_compiler_options(compiler);

	prog = standalone_compile_shader(&options, num_files, files);
	if (!prog)
		errx(1, "couldn't parse `%s'", files[0]);

	nir_shader *nir = glsl_to_nir(prog, stage, nir_options);

	/* required NIR passes: */
	if (nir_options->lower_all_io_to_temps ||
			nir->info.stage == MESA_SHADER_VERTEX ||
			nir->info.stage == MESA_SHADER_GEOMETRY) {
		NIR_PASS_V(nir, nir_lower_io_to_temporaries,
				nir_shader_get_entrypoint(nir),
				true, true);
	} else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
		NIR_PASS_V(nir, nir_lower_io_to_temporaries,
				nir_shader_get_entrypoint(nir),
				true, false);
	}

	NIR_PASS_V(nir, nir_lower_global_vars_to_local);
	NIR_PASS_V(nir, nir_split_var_copies);
	NIR_PASS_V(nir, nir_lower_var_copies);

	NIR_PASS_V(nir, nir_split_var_copies);
	NIR_PASS_V(nir, nir_lower_var_copies);
	nir_print_shader(nir, stdout);
	NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true);
	NIR_PASS_V(nir, nir_lower_atomics_to_ssbo, 8);
	nir_print_shader(nir, stdout);

	switch (stage) {
	case MESA_SHADER_VERTEX:
		nir_assign_var_locations(&nir->inputs,
				&nir->num_inputs,
				ir3_glsl_type_size);

		/* Re-lower global vars, to deal with any dead VS inputs. */
		NIR_PASS_V(nir, nir_lower_global_vars_to_local);

		sort_varyings(&nir->outputs);
		nir_assign_var_locations(&nir->outputs,
				&nir->num_outputs,
				ir3_glsl_type_size);
		fixup_varying_slots(&nir->outputs);
		break;
	case MESA_SHADER_FRAGMENT:
		sort_varyings(&nir->inputs);
		nir_assign_var_locations(&nir->inputs,
				&nir->num_inputs,
				ir3_glsl_type_size);
		fixup_varying_slots(&nir->inputs);
		nir_assign_var_locations(&nir->outputs,
				&nir->num_outputs,
				ir3_glsl_type_size);
		break;
	case MESA_SHADER_COMPUTE:
		break;
	default:
		errx(1, "unhandled shader stage: %d", stage);
	}

	nir_assign_var_locations(&nir->uniforms,
			&nir->num_uniforms,
			ir3_glsl_type_size);

	NIR_PASS_V(nir, nir_lower_system_values);
	NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size, 0);
	NIR_PASS_V(nir, gl_nir_lower_samplers, prog);

	return nir;
}

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 debug_func(void *priv, enum nir_spirv_debug_level level,
		size_t spirv_offset, const char *message)
{
//	printf("%s\n", message);
}

static nir_shader *
load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
{
	const struct spirv_to_nir_options spirv_options = {
		/* these caps are just make-believe */
		.caps = {
			.draw_parameters = true,
			.float64 = true,
			.image_read_without_format = true,
			.image_write_without_format = true,
			.int64 = true,
			.variable_pointers = true,
		},
		.lower_workgroup_access_to_offsets = true,
		.lower_ubo_ssbo_access_to_offsets = true,
		.debug = {
			.func = debug_func,
		}
	};
	nir_function *entry_point;
	void *buf;
	size_t size;

	read_file(filename, &buf, &size);

	entry_point = spirv_to_nir(buf, size / 4,
			NULL, 0, /* spec_entries */
			stage, entry,
			&spirv_options,
			ir3_get_compiler_options(compiler));

	nir_print_shader(entry_point->shader, stdout);

	return entry_point->shader;
}

static void print_usage(void)
{
	printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\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("    --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("    --astc-srgb MASK  - bitmask of samplers to enable astc-srgb workaround\n");
	printf("    --stream-out      - enable stream-out (aka transform feedback)\n");
	printf("    --ucp MASK        - bitmask of enabled user-clip-planes\n");
	printf("    --gpu GPU_ID      - specify gpu-id (default 320)\n");
	printf("    --help            - show this message\n");
}

int main(int argc, char **argv)
{
	int ret = 0, n = 1;
	char *filenames[2];
	int num_files = 0;
	unsigned stage = 0;
	struct ir3_shader_variant v;
	struct ir3_shader s;
	struct ir3_shader_key key = {};
	/* TODO cmdline option to target different gpus: */
	unsigned gpu_id = 320;
	const char *info;
	const char *entry;
	void *ptr;
	bool from_spirv = false;
	size_t size;

	memset(&s, 0, sizeof(s));
	memset(&v, 0, sizeof(v));

	/* 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")) {
			ir3_shader_debug |= IR3_DBG_OPTMSGS | IR3_DBG_DISASM;
			n++;
			continue;
		}

		if (!strcmp(argv[n], "--binning-pass")) {
			debug_printf(" %s", argv[n]);
			v.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], "--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], "--astc-srgb")) {
			debug_printf(" %s %s", argv[n], argv[n+1]);
			key.vastc_srgb = key.fastc_srgb = strtol(argv[n+1], NULL, 0);
			n += 2;
			continue;
		}

		if (!strcmp(argv[n], "--stream-out")) {
			struct ir3_stream_output_info *so = &s.stream_output;
			debug_printf(" %s", argv[n]);
			/* TODO more dynamic config based on number of outputs, etc
			 * rather than just hard-code for first output:
			 */
			so->num_outputs = 1;
			so->stride[0] = 4;
			so->output[0].register_index = 0;
			so->output[0].start_component = 0;
			so->output[0].num_components = 4;
			so->output[0].output_buffer = 0;
			so->output[0].dst_offset = 2;
			so->output[0].stream = 0;
			n++;
			continue;
		}

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

		if (!strcmp(argv[n], "--gpu")) {
			debug_printf(" %s %s", argv[n], argv[n+1]);
			gpu_id = strtol(argv[n+1], NULL, 0);
			n += 2;
			continue;
		}

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

		break;
	}
	debug_printf("\n");

	while (n < argc) {
		char *filename = argv[n];
		char *ext = strrchr(filename, '.');

		if (strcmp(ext, ".tgsi") == 0) {
			if (num_files != 0)
				errx(1, "in TGSI mode, only a single file may be specified");
			s.from_tgsi = true;
		} else if (strcmp(ext, ".spv") == 0) {
			if (num_files != 0)
				errx(1, "in SPIR-V mode, only a single file may be specified");
			stage = MESA_SHADER_COMPUTE;
			from_spirv = true;
			filenames[num_files++] = filename;
			n++;
			if (n == argc)
				errx(1, "in SPIR-V mode, an entry point must be specified");
			entry = argv[n];
			n++;
		} else if (strcmp(ext, ".comp") == 0) {
			if (s.from_tgsi || from_spirv)
				errx(1, "cannot mix GLSL/TGSI/SPIRV");
			if (num_files >= ARRAY_SIZE(filenames))
				errx(1, "too many GLSL files");
			stage = MESA_SHADER_COMPUTE;
		} else if (strcmp(ext, ".frag") == 0) {
			if (s.from_tgsi || from_spirv)
				errx(1, "cannot mix GLSL/TGSI/SPIRV");
			if (num_files >= ARRAY_SIZE(filenames))
				errx(1, "too many GLSL files");
			stage = MESA_SHADER_FRAGMENT;
		} else if (strcmp(ext, ".vert") == 0) {
			if (s.from_tgsi)
				errx(1, "cannot mix GLSL and TGSI");
			if (num_files >= ARRAY_SIZE(filenames))
				errx(1, "too many GLSL files");
			stage = MESA_SHADER_VERTEX;
		} else {
			print_usage();
			return -1;
		}

		filenames[num_files++] = filename;

		n++;
	}

	nir_shader *nir;

	compiler = ir3_compiler_create(NULL, gpu_id);

	if (s.from_tgsi) {
		struct tgsi_token toks[65536];

		ret = read_file(filenames[0], &ptr, &size);
		if (ret) {
			print_usage();
			return ret;
		}

		if (ir3_shader_debug & IR3_DBG_OPTMSGS)
			debug_printf("%s\n", (char *)ptr);

		if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
			errx(1, "could not parse `%s'", filenames[0]);

		if (ir3_shader_debug & IR3_DBG_OPTMSGS)
			tgsi_dump(toks, 0);

		nir = ir3_tgsi_to_nir(compiler, toks);
		NIR_PASS_V(nir, nir_lower_global_vars_to_local);
	} else if (from_spirv) {
		nir = load_spirv(filenames[0], entry, stage);

		NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size,
				(nir_lower_io_options)0);

		/* TODO do this somewhere else */
		nir_lower_int64(nir, ~0);
		nir_lower_system_values(nir);
	} else if (num_files > 0) {
		nir = load_glsl(num_files, filenames, stage);
	} else {
		print_usage();
		return -1;
	}

	s.compiler = compiler;
	s.nir = ir3_optimize_nir(&s, nir, NULL);

	v.key = key;
	v.shader = &s;
	s.type = v.type = nir->info.stage;

	info = "NIR compiler";
	ret = ir3_compile_shader_nir(s.compiler, &v);
	if (ret) {
		fprintf(stderr, "compiler failed!\n");
		return ret;
	}
	dump_info(&v, info);
}