1 /*
2 * Copyright (c) 2015 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 * Christian Gmeiner <christian.gmeiner@gmail.com>
26 */
27
28 #include <err.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include "tgsi/tgsi_parse.h"
35 #include "tgsi/tgsi_text.h"
36
37 #include "nir/tgsi_to_nir.h"
38
39 #include "etnaviv_compiler.h"
40 #include "etnaviv_debug.h"
41 #include "etnaviv_internal.h"
42 #include "etnaviv_shader.h"
43
44 #include "util/u_memory.h"
45
46 static const struct etna_specs specs_gc2000 = {
47 .vs_need_z_div = 0,
48 .has_sin_cos_sqrt = 1,
49 .has_sign_floor_ceil = 1,
50 .vertex_sampler_offset = 8,
51 .vertex_output_buffer_size = 512,
52 .vertex_cache_size = 16,
53 .shader_core_count = 4,
54 .max_instructions = 512,
55 .max_varyings = 12,
56 .max_registers = 64,
57 .max_vs_uniforms = 168,
58 .max_ps_uniforms = 128,
59 .num_constants = 168,
60 };
61
62 static int
read_file(const char * filename,void ** ptr,size_t * size)63 read_file(const char *filename, void **ptr, size_t *size)
64 {
65 int fd, ret;
66 struct stat st;
67
68 *ptr = MAP_FAILED;
69
70 fd = open(filename, O_RDONLY);
71 if (fd == -1) {
72 warnx("couldn't open `%s'", filename);
73 return 1;
74 }
75
76 ret = fstat(fd, &st);
77 if (ret)
78 errx(1, "couldn't stat `%s'", filename);
79
80 *size = st.st_size;
81 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
82 if (*ptr == MAP_FAILED)
83 errx(1, "couldn't map `%s'", filename);
84
85 close(fd);
86
87 return 0;
88 }
89
90 static void
print_usage(void)91 print_usage(void)
92 {
93 printf("Usage: etnaviv_compiler [OPTIONS]... FILE\n");
94 printf(" --verbose - verbose compiler/debug messages\n");
95 printf(" --frag-rb-swap - swap rb in color output (FRAG)\n");
96 printf(" --help - show this message\n");
97 }
98
99 int
main(int argc,char ** argv)100 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 etna_shader_key key = {};
107 void *ptr;
108 size_t size;
109
110 etna_mesa_debug = ETNA_DBG_MSGS | ETNA_DBG_NOCACHE;
111
112 while (n < argc) {
113 if (!strcmp(argv[n], "--verbose")) {
114 etna_mesa_debug |= ETNA_DBG_COMPILER_MSGS;
115 n++;
116 continue;
117 }
118
119 if (!strcmp(argv[n], "--frag-rb-swap")) {
120 debug_printf(" %s", argv[n]);
121 key.frag_rb_swap = true;
122 n++;
123 continue;
124 }
125
126 if (!strcmp(argv[n], "--help")) {
127 print_usage();
128 return 0;
129 }
130
131 break;
132 }
133
134 filename = argv[n];
135
136 ret = read_file(filename, &ptr, &size);
137 if (ret) {
138 print_usage();
139 return ret;
140 }
141
142 debug_printf("%s\n", (char *)ptr);
143
144 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
145 errx(1, "could not parse `%s'", filename);
146
147 tgsi_parse_init(&parse, toks);
148 glsl_type_singleton_init_or_ref();
149
150 struct etna_compiler *compiler = etna_compiler_create(NULL, &specs_gc2000);
151
152 struct etna_shader shader = {};
153 shader.nir = tgsi_to_nir_noscreen(toks, &compiler->options);
154 shader.specs = &specs_gc2000;
155 shader.compiler = compiler;
156
157 struct util_debug_callback debug = {}; // TODO: proper debug callback
158 struct etna_shader_variant *v = etna_shader_variant(&shader, &key, &debug, false);
159 if (!v) {
160 fprintf(stderr, "shader variant creation failed!\n");
161 return 1;
162 }
163
164 if (!etna_compile_shader(v)) {
165 fprintf(stderr, "compiler failed!\n");
166 return 1;
167 }
168
169 etna_dump_shader(v);
170 etna_destroy_shader(v);
171 glsl_type_singleton_decref();
172 }
173