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