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_dump.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_text.h"
37 
38 #include "etnaviv_compiler.h"
39 #include "etnaviv_debug.h"
40 #include "etnaviv_internal.h"
41 #include "etnaviv_shader.h"
42 
43 #include "util/u_memory.h"
44 
45 static const struct etna_specs specs_gc2000 = {
46    .vs_need_z_div = 0,
47    .has_sin_cos_sqrt = 1,
48    .has_sign_floor_ceil = 1,
49    .vertex_sampler_offset = 8,
50    .vertex_output_buffer_size = 512,
51    .vertex_cache_size = 16,
52    .shader_core_count = 4,
53    .max_instructions = 512,
54    .max_varyings = 12,
55    .max_registers = 64,
56    .max_vs_uniforms = 168,
57    .max_ps_uniforms = 128,
58    .num_constants = 168,
59 };
60 
61 static int
read_file(const char * filename,void ** ptr,size_t * size)62 read_file(const char *filename, void **ptr, size_t *size)
63 {
64    int fd, ret;
65    struct stat st;
66 
67    *ptr = MAP_FAILED;
68 
69    fd = open(filename, O_RDONLY);
70    if (fd == -1) {
71       warnx("couldn't open `%s'", filename);
72       return 1;
73    }
74 
75    ret = fstat(fd, &st);
76    if (ret)
77       errx(1, "couldn't stat `%s'", filename);
78 
79    *size = st.st_size;
80    *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
81    if (*ptr == MAP_FAILED)
82       errx(1, "couldn't map `%s'", filename);
83 
84    close(fd);
85 
86    return 0;
87 }
88 
89 static void
print_usage(void)90 print_usage(void)
91 {
92    printf("Usage: etnaviv_compiler [OPTIONS]... FILE\n");
93    printf("    --verbose         - verbose compiler/debug messages\n");
94    printf("    --frag-rb-swap    - swap rb in color output (FRAG)\n");
95    printf("    --help            - show this message\n");
96 }
97 
98 int
main(int argc,char ** argv)99 main(int argc, char **argv)
100 {
101    int ret = 0, n = 1;
102    const char *filename;
103    struct tgsi_token toks[65536];
104    struct tgsi_parse_context parse;
105    struct etna_shader s = {};
106    struct etna_shader_key key = {};
107    void *ptr;
108    size_t size;
109 
110    struct etna_shader_variant *v = CALLOC_STRUCT(etna_shader_variant);
111    if (!v) {
112       fprintf(stderr, "malloc failed!\n");
113       return 1;
114    }
115 
116    etna_mesa_debug = ETNA_DBG_MSGS;
117 
118    while (n < argc) {
119       if (!strcmp(argv[n], "--verbose")) {
120          etna_mesa_debug |= ETNA_DBG_COMPILER_MSGS;
121          n++;
122          continue;
123       }
124 
125       if (!strcmp(argv[n], "--frag-rb-swap")) {
126          debug_printf(" %s", argv[n]);
127          key.frag_rb_swap = true;
128          n++;
129          continue;
130       }
131 
132       if (!strcmp(argv[n], "--help")) {
133          print_usage();
134          return 0;
135       }
136 
137       break;
138    }
139 
140    filename = argv[n];
141 
142    ret = read_file(filename, &ptr, &size);
143    if (ret) {
144       print_usage();
145       return ret;
146    }
147 
148    debug_printf("%s\n", (char *)ptr);
149 
150    if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
151       errx(1, "could not parse `%s'", filename);
152 
153    tgsi_parse_init(&parse, toks);
154 
155    s.specs = &specs_gc2000;
156    s.tokens = toks;
157 
158    v->shader = &s;
159    v->key = key;
160 
161    if (!etna_compile_shader(v)) {
162       fprintf(stderr, "compiler failed!\n");
163       return 1;
164    }
165 
166    etna_dump_shader(v);
167    etna_destroy_shader(v);
168 }
169