glsl: Require a context in _mesa_glsl_parse_state.
[mesa.git] / src / glsl / 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 #include "ast.h"
33 #include "glsl_parser_extras.h"
34 #include "glsl_parser.h"
35 #include "ir_optimization.h"
36 #include "ir_print_visitor.h"
37 #include "program.h"
38 #include "loop_analysis.h"
39
40 extern "C" struct gl_shader *
41 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
42
43 /* Copied from shader_api.c for the stand-alone compiler.
44 */
45 struct gl_shader *
46 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
47 {
48 struct gl_shader *shader;
49
50 (void) ctx;
51
52 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
53 shader = talloc_zero(NULL, struct gl_shader);
54 if (shader) {
55 shader->Type = type;
56 shader->Name = name;
57 shader->RefCount = 1;
58 }
59 return shader;
60 }
61
62 static void
63 initialize_context(GLcontext *ctx, gl_api api)
64 {
65 memset(ctx, 0, sizeof(*ctx));
66
67 ctx->API = api;
68
69 ctx->Extensions.ARB_draw_buffers = GL_TRUE;
70 ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
71 ctx->Extensions.EXT_texture_array = GL_TRUE;
72 ctx->Extensions.NV_texture_rectangle = GL_TRUE;
73
74 /* 1.10 minimums. */
75 ctx->Const.MaxLights = 8;
76 ctx->Const.MaxClipPlanes = 8;
77 ctx->Const.MaxTextureUnits = 2;
78
79 /* More than the 1.10 minimum to appease parser tests taken from
80 * apps that (hopefully) already checked the number of coords.
81 */
82 ctx->Const.MaxTextureCoordUnits = 4;
83
84 ctx->Const.VertexProgram.MaxAttribs = 16;
85 ctx->Const.VertexProgram.MaxUniformComponents = 512;
86 ctx->Const.MaxVarying = 8;
87 ctx->Const.MaxVertexTextureImageUnits = 0;
88 ctx->Const.MaxCombinedTextureImageUnits = 2;
89 ctx->Const.MaxTextureImageUnits = 2;
90 ctx->Const.FragmentProgram.MaxUniformComponents = 64;
91
92 ctx->Const.MaxDrawBuffers = 2;
93
94 ctx->Driver.NewShader = _mesa_new_shader;
95 }
96
97 /* Returned string will have 'ctx' as its talloc owner. */
98 static char *
99 load_text_file(void *ctx, const char *file_name)
100 {
101 char *text = NULL;
102 struct stat st;
103 ssize_t total_read = 0;
104 int fd = open(file_name, O_RDONLY);
105
106 if (fd < 0) {
107 return NULL;
108 }
109
110 if (fstat(fd, & st) == 0) {
111 text = (char *) talloc_size(ctx, st.st_size + 1);
112 if (text != NULL) {
113 do {
114 ssize_t bytes = read(fd, text + total_read,
115 st.st_size - total_read);
116 if (bytes < 0) {
117 free(text);
118 text = NULL;
119 break;
120 }
121
122 if (bytes == 0) {
123 break;
124 }
125
126 total_read += bytes;
127 } while (total_read < st.st_size);
128
129 text[total_read] = '\0';
130 }
131 }
132
133 close(fd);
134
135 return text;
136 }
137
138
139 void
140 usage_fail(const char *name)
141 {
142 printf("%s <filename.frag|filename.vert>\n", name);
143 exit(EXIT_FAILURE);
144 }
145
146
147 int dump_ast = 0;
148 int dump_hir = 0;
149 int dump_lir = 0;
150 int do_link = 0;
151
152 const struct option compiler_opts[] = {
153 { "dump-ast", 0, &dump_ast, 1 },
154 { "dump-hir", 0, &dump_hir, 1 },
155 { "dump-lir", 0, &dump_lir, 1 },
156 { "link", 0, &do_link, 1 },
157 { NULL, 0, NULL, 0 }
158 };
159
160 void
161 compile_shader(GLcontext *ctx, struct gl_shader *shader)
162 {
163 struct _mesa_glsl_parse_state *state =
164 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
165
166 const char *source = shader->Source;
167 state->error = preprocess(state, &source, &state->info_log,
168 state->extensions, ctx->API);
169
170 if (!state->error) {
171 _mesa_glsl_lexer_ctor(state, source);
172 _mesa_glsl_parse(state);
173 _mesa_glsl_lexer_dtor(state);
174 }
175
176 if (dump_ast) {
177 foreach_list_const(n, &state->translation_unit) {
178 ast_node *ast = exec_node_data(ast_node, n, link);
179 ast->print();
180 }
181 printf("\n\n");
182 }
183
184 shader->ir = new(shader) exec_list;
185 if (!state->error && !state->translation_unit.is_empty())
186 _mesa_ast_to_hir(shader->ir, state);
187
188 /* Print out the unoptimized IR. */
189 if (!state->error && dump_hir) {
190 validate_ir_tree(shader->ir);
191 _mesa_print_ir(shader->ir, state);
192 }
193
194 /* Optimization passes */
195 if (!state->error && !shader->ir->is_empty()) {
196 bool progress;
197 do {
198 progress = false;
199
200 progress = do_function_inlining(shader->ir) || progress;
201 progress = do_if_simplification(shader->ir) || progress;
202 progress = do_copy_propagation(shader->ir) || progress;
203 progress = do_dead_code_local(shader->ir) || progress;
204 progress = do_dead_code_unlinked(shader->ir) || progress;
205 progress = do_tree_grafting(shader->ir) || progress;
206 progress = do_constant_propagation(shader->ir) || progress;
207 progress = do_constant_variable_unlinked(shader->ir) || progress;
208 progress = do_constant_folding(shader->ir) || progress;
209 progress = do_algebraic(shader->ir) || progress;
210 progress = do_vec_index_to_swizzle(shader->ir) || progress;
211 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
212 progress = do_swizzle_swizzle(shader->ir) || progress;
213
214 loop_state *ls = analyze_loop_variables(shader->ir);
215 progress = set_loop_controls(shader->ir, ls) || progress;
216 progress = unroll_loops(shader->ir, ls) || progress;
217 delete ls;
218 } while (progress);
219
220 validate_ir_tree(shader->ir);
221 }
222
223
224 /* Print out the resulting IR */
225 if (!state->error && dump_lir) {
226 _mesa_print_ir(shader->ir, state);
227 }
228
229 shader->symbols = state->symbols;
230 shader->CompileStatus = !state->error;
231 shader->Version = state->language_version;
232 memcpy(shader->builtins_to_link, state->builtins_to_link,
233 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
234 shader->num_builtins_to_link = state->num_builtins_to_link;
235
236 if (shader->InfoLog)
237 talloc_free(shader->InfoLog);
238
239 shader->InfoLog = state->info_log;
240
241 /* Retain any live IR, but trash the rest. */
242 reparent_ir(shader->ir, shader);
243
244 talloc_free(state);
245
246 return;
247 }
248
249 int
250 main(int argc, char **argv)
251 {
252 int status = EXIT_SUCCESS;
253 GLcontext local_ctx;
254 GLcontext *ctx = &local_ctx;
255
256 int c;
257 int idx = 0;
258 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
259 /* empty */ ;
260
261
262 if (argc <= optind)
263 usage_fail(argv[0]);
264
265 initialize_context(ctx, API_OPENGL);
266
267 struct gl_shader_program *whole_program;
268
269 whole_program = talloc_zero (NULL, struct gl_shader_program);
270 assert(whole_program != NULL);
271
272 for (/* empty */; argc > optind; optind++) {
273 whole_program->Shaders = (struct gl_shader **)
274 talloc_realloc(whole_program, whole_program->Shaders,
275 struct gl_shader *, whole_program->NumShaders + 1);
276 assert(whole_program->Shaders != NULL);
277
278 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
279
280 whole_program->Shaders[whole_program->NumShaders] = shader;
281 whole_program->NumShaders++;
282
283 const unsigned len = strlen(argv[optind]);
284 if (len < 6)
285 usage_fail(argv[0]);
286
287 const char *const ext = & argv[optind][len - 5];
288 if (strncmp(".vert", ext, 5) == 0)
289 shader->Type = GL_VERTEX_SHADER;
290 else if (strncmp(".geom", ext, 5) == 0)
291 shader->Type = GL_GEOMETRY_SHADER;
292 else if (strncmp(".frag", ext, 5) == 0)
293 shader->Type = GL_FRAGMENT_SHADER;
294 else
295 usage_fail(argv[0]);
296
297 shader->Source = load_text_file(whole_program, argv[optind]);
298 if (shader->Source == NULL) {
299 printf("File \"%s\" does not exist.\n", argv[optind]);
300 exit(EXIT_FAILURE);
301 }
302
303 compile_shader(ctx, shader);
304
305 if (!shader->CompileStatus) {
306 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
307 status = EXIT_FAILURE;
308 break;
309 }
310 }
311
312 if ((status == EXIT_SUCCESS) && do_link) {
313 link_shaders(ctx, whole_program);
314 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
315
316 if (strlen(whole_program->InfoLog) > 0)
317 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
318 }
319
320 for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
321 talloc_free(whole_program->_LinkedShaders[i]);
322
323 talloc_free(whole_program);
324 _mesa_glsl_release_types();
325 _mesa_glsl_release_functions();
326
327 return status;
328 }