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 (at) freedesktop.org> 25 * Christian Gmeiner <christian.gmeiner (at) 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 42 static const struct etna_specs specs_gc2000 = { 43 .vs_need_z_div = 0, 44 .has_sin_cos_sqrt = 1, 45 .has_sign_floor_ceil = 1, 46 .vertex_sampler_offset = 8, 47 .vertex_output_buffer_size = 512, 48 .vertex_cache_size = 16, 49 .shader_core_count = 4, 50 .max_instructions = 512, 51 .max_varyings = 12, 52 .max_registers = 64, 53 .max_vs_uniforms = 168, 54 .max_ps_uniforms = 128, 55 .num_constants = 168, 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 87 print_usage(void) 88 { 89 printf("Usage: etnaviv_compiler [OPTIONS]... FILE\n"); 90 printf(" --verbose - verbose compiler/debug messages\n"); 91 printf(" --help - show this message\n"); 92 } 93 94 int 95 main(int argc, char **argv) 96 { 97 int ret = 0, n = 1; 98 const char *filename; 99 struct tgsi_token toks[65536]; 100 struct tgsi_parse_context parse; 101 struct etna_shader *shader_obj; 102 void *ptr; 103 size_t size; 104 105 etna_mesa_debug = ETNA_DBG_MSGS; 106 107 while (n < argc) { 108 if (!strcmp(argv[n], "--verbose")) { 109 etna_mesa_debug |= ETNA_DBG_COMPILER_MSGS; 110 n++; 111 continue; 112 } 113 114 if (!strcmp(argv[n], "--help")) { 115 print_usage(); 116 return 0; 117 } 118 119 break; 120 } 121 122 filename = argv[n]; 123 124 ret = read_file(filename, &ptr, &size); 125 if (ret) { 126 print_usage(); 127 return ret; 128 } 129 130 debug_printf("%s\n", (char *)ptr); 131 132 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks))) 133 errx(1, "could not parse `%s'", filename); 134 135 tgsi_parse_init(&parse, toks); 136 137 shader_obj = etna_compile_shader(&specs_gc2000, toks); 138 139 if (shader_obj == NULL) { 140 fprintf(stderr, "compiler failed!\n"); 141 return 1; 142 } 143 144 etna_dump_shader(shader_obj); 145 etna_destroy_shader(shader_obj); 146 } 147