Complain and exit if the given shader file doesn't exist.
[mesa.git] / main.cpp
1 /*
2 * Copyright © 2008, 2009 Intel Corporation
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, sublicense,
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 next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 NONINFRINGEMENT. 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 #include <cstdlib>
24 #include <cstdio>
25 #include <getopt.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 extern "C" {
33 #include <talloc.h>
34 }
35
36 #include "ast.h"
37 #include "glsl_parser_extras.h"
38 #include "glsl_parser.h"
39 #include "ir_optimization.h"
40 #include "ir_print_visitor.h"
41 #include "program.h"
42
43
44 static char *
45 load_text_file(const char *file_name, size_t *size)
46 {
47 char *text = NULL;
48 struct stat st;
49 ssize_t total_read = 0;
50 int fd = open(file_name, O_RDONLY);
51
52 *size = 0;
53 if (fd < 0) {
54 return NULL;
55 }
56
57 if (fstat(fd, & st) == 0) {
58 text = (char *) malloc(st.st_size + 1);
59 if (text != NULL) {
60 do {
61 ssize_t bytes = read(fd, text + total_read,
62 st.st_size - total_read);
63 if (bytes < 0) {
64 free(text);
65 text = NULL;
66 break;
67 }
68
69 if (bytes == 0) {
70 break;
71 }
72
73 total_read += bytes;
74 } while (total_read < st.st_size);
75
76 text[total_read] = '\0';
77 *size = total_read;
78 }
79 }
80
81 close(fd);
82
83 return text;
84 }
85
86
87 void
88 usage_fail(const char *name)
89 {
90 printf("%s <filename.frag|filename.vert>\n", name);
91 exit(EXIT_FAILURE);
92 }
93
94
95 int dump_ast = 0;
96 int dump_lir = 0;
97 int do_link = 0;
98
99 const struct option compiler_opts[] = {
100 { "dump-ast", 0, &dump_ast, 1 },
101 { "dump-lir", 0, &dump_lir, 1 },
102 { "link", 0, &do_link, 1 },
103 { NULL, 0, NULL, 0 }
104 };
105
106 void
107 compile_shader(struct glsl_shader *shader)
108 {
109 struct _mesa_glsl_parse_state state;
110
111 memset(& state, 0, sizeof(state));
112 switch (shader->Type) {
113 case GL_VERTEX_SHADER: state.target = vertex_shader; break;
114 case GL_FRAGMENT_SHADER: state.target = fragment_shader; break;
115 case GL_GEOMETRY_SHADER: state.target = geometry_shader; break;
116 }
117
118 state.scanner = NULL;
119 state.translation_unit.make_empty();
120 state.symbols = new glsl_symbol_table;
121 state.info_log = talloc_strdup(shader, "");
122 state.error = false;
123 state.temp_index = 0;
124 state.loop_or_switch_nesting = NULL;
125 state.ARB_texture_rectangle_enable = true;
126
127 _mesa_glsl_lexer_ctor(& state, shader->Source, shader->SourceLen);
128 _mesa_glsl_parse(& state);
129 _mesa_glsl_lexer_dtor(& state);
130
131 if (dump_ast) {
132 foreach_list_const(n, &state.translation_unit) {
133 ast_node *ast = exec_node_data(ast_node, n, link);
134 ast->print();
135 }
136 printf("\n\n");
137 }
138
139 shader->ir.make_empty();
140 if (!state.error && !state.translation_unit.is_empty())
141 _mesa_ast_to_hir(&shader->ir, &state);
142
143 /* Optimization passes */
144 if (!state.error && !shader->ir.is_empty()) {
145 bool progress;
146 do {
147 progress = false;
148
149 progress = do_function_inlining(&shader->ir) || progress;
150 progress = do_if_simplification(&shader->ir) || progress;
151 progress = do_copy_propagation(&shader->ir) || progress;
152 progress = do_dead_code_local(&shader->ir) || progress;
153 progress = do_dead_code_unlinked(&shader->ir) || progress;
154 progress = do_constant_variable_unlinked(&shader->ir) || progress;
155 progress = do_constant_folding(&shader->ir) || progress;
156 progress = do_vec_index_to_swizzle(&shader->ir) || progress;
157 progress = do_swizzle_swizzle(&shader->ir) || progress;
158 } while (progress);
159 }
160
161 /* Print out the resulting IR */
162 if (!state.error && dump_lir) {
163 _mesa_print_ir(&shader->ir, &state);
164 }
165
166 shader->symbols = state.symbols;
167 shader->CompileStatus = !state.error;
168
169 if (shader->InfoLog)
170 talloc_free(shader->InfoLog);
171
172 shader->InfoLog = state.info_log;
173
174 return;
175 }
176
177 int
178 main(int argc, char **argv)
179 {
180 int status = EXIT_SUCCESS;
181
182 int c;
183 int idx = 0;
184 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
185 /* empty */ ;
186
187
188 if (argc <= optind)
189 usage_fail(argv[0]);
190
191 struct glsl_program whole_program;
192 memset(&whole_program, 0, sizeof(whole_program));
193
194 for (/* empty */; argc > optind; optind++) {
195 whole_program.Shaders = (struct glsl_shader **)
196 realloc(whole_program.Shaders,
197 sizeof(struct glsl_shader *) * (whole_program.NumShaders + 1));
198 assert(whole_program.Shaders != NULL);
199
200 /* talloc context should probably be whole_program */
201 struct glsl_shader *shader = talloc_zero(NULL, glsl_shader);
202
203 whole_program.Shaders[whole_program.NumShaders] = shader;
204 whole_program.NumShaders++;
205
206 const unsigned len = strlen(argv[optind]);
207 if (len < 6)
208 usage_fail(argv[0]);
209
210 const char *const ext = & argv[optind][len - 5];
211 if (strncmp(".vert", ext, 5) == 0)
212 shader->Type = GL_VERTEX_SHADER;
213 else if (strncmp(".geom", ext, 5) == 0)
214 shader->Type = GL_GEOMETRY_SHADER;
215 else if (strncmp(".frag", ext, 5) == 0)
216 shader->Type = GL_FRAGMENT_SHADER;
217 else
218 usage_fail(argv[0]);
219
220 shader->Source = load_text_file(argv[optind], &shader->SourceLen);
221 if (shader->Source == NULL) {
222 printf("File \"%s\" does not exist.\n", argv[optind]);
223 exit(EXIT_FAILURE);
224 }
225
226 compile_shader(shader);
227
228 if (!shader->CompileStatus) {
229 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
230 status = EXIT_FAILURE;
231 break;
232 }
233 }
234
235 if ((status == EXIT_SUCCESS) && do_link) {
236 link_shaders(&whole_program);
237 status = (whole_program.LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
238 }
239
240 return status;
241 }