d7e35bcb3a2916071a964d3ce8ddca246544879f
[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 <getopt.h>
24
25 /** @file main.cpp
26 *
27 * This file is the main() routine and scaffolding for producing
28 * builtin_compiler (which doesn't include builtins itself and is used
29 * to generate the profile information for builtin_function.cpp), and
30 * for glsl_compiler (which does include builtins and can be used to
31 * offline compile GLSL code and examine the resulting GLSL IR.
32 */
33
34 #include "ast.h"
35 #include "glsl_parser_extras.h"
36 #include "ir_optimization.h"
37 #include "ir_print_visitor.h"
38 #include "program.h"
39 #include "loop_analysis.h"
40 #include "standalone_scaffolding.h"
41
42 static void
43 initialize_context(struct gl_context *ctx, gl_api api)
44 {
45 initialize_context_to_defaults(ctx, api);
46
47 /* The standalone compiler needs to claim support for almost
48 * everything in order to compile the built-in functions.
49 */
50 ctx->Const.GLSLVersion = 150;
51 ctx->Extensions.ARB_ES3_compatibility = true;
52
53 ctx->Const.MaxClipPlanes = 8;
54 ctx->Const.MaxDrawBuffers = 2;
55
56 /* More than the 1.10 minimum to appease parser tests taken from
57 * apps that (hopefully) already checked the number of coords.
58 */
59 ctx->Const.MaxTextureCoordUnits = 4;
60
61 ctx->Driver.NewShader = _mesa_new_shader;
62 }
63
64 /* Returned string will have 'ctx' as its ralloc owner. */
65 static char *
66 load_text_file(void *ctx, const char *file_name)
67 {
68 char *text = NULL;
69 size_t size;
70 size_t total_read = 0;
71 FILE *fp = fopen(file_name, "rb");
72
73 if (!fp) {
74 return NULL;
75 }
76
77 fseek(fp, 0L, SEEK_END);
78 size = ftell(fp);
79 fseek(fp, 0L, SEEK_SET);
80
81 text = (char *) ralloc_size(ctx, size + 1);
82 if (text != NULL) {
83 do {
84 size_t bytes = fread(text + total_read,
85 1, size - total_read, fp);
86 if (bytes < size - total_read) {
87 free(text);
88 text = NULL;
89 break;
90 }
91
92 if (bytes == 0) {
93 break;
94 }
95
96 total_read += bytes;
97 } while (total_read < size);
98
99 text[total_read] = '\0';
100 }
101
102 fclose(fp);
103
104 return text;
105 }
106
107 int glsl_es = 0;
108 int dump_ast = 0;
109 int dump_hir = 0;
110 int dump_lir = 0;
111 int do_link = 0;
112
113 const struct option compiler_opts[] = {
114 { "glsl-es", 0, &glsl_es, 1 },
115 { "dump-ast", 0, &dump_ast, 1 },
116 { "dump-hir", 0, &dump_hir, 1 },
117 { "dump-lir", 0, &dump_lir, 1 },
118 { "link", 0, &do_link, 1 },
119 { NULL, 0, NULL, 0 }
120 };
121
122 /**
123 * \brief Print proper usage and exit with failure.
124 */
125 void
126 usage_fail(const char *name)
127 {
128
129 const char *header =
130 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
131 "\n"
132 "Possible options are:\n";
133 printf(header, name, name);
134 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
135 printf(" --%s\n", o->name);
136 }
137 exit(EXIT_FAILURE);
138 }
139
140
141 void
142 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
143 {
144 struct _mesa_glsl_parse_state *state =
145 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
146
147 const char *source = shader->Source;
148 state->error = glcpp_preprocess(state, &source, &state->info_log,
149 state->extensions, ctx) != 0;
150
151 if (!state->error) {
152 _mesa_glsl_lexer_ctor(state, source);
153 _mesa_glsl_parse(state);
154 _mesa_glsl_lexer_dtor(state);
155 }
156
157 if (dump_ast) {
158 foreach_list_const(n, &state->translation_unit) {
159 ast_node *ast = exec_node_data(ast_node, n, link);
160 ast->print();
161 }
162 printf("\n\n");
163 }
164
165 shader->ir = new(shader) exec_list;
166 if (!state->error && !state->translation_unit.is_empty())
167 _mesa_ast_to_hir(shader->ir, state);
168
169 /* Print out the unoptimized IR. */
170 if (!state->error && dump_hir) {
171 validate_ir_tree(shader->ir);
172 _mesa_print_ir(shader->ir, state);
173 }
174
175 /* Optimization passes */
176 if (!state->error && !shader->ir->is_empty()) {
177 const struct gl_shader_compiler_options *opts =
178 &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
179 bool progress;
180 do {
181 progress = do_common_optimization(shader->ir, false, false, 32, opts);
182 } while (progress);
183
184 validate_ir_tree(shader->ir);
185 }
186
187
188 /* Print out the resulting IR */
189 if (!state->error && dump_lir) {
190 _mesa_print_ir(shader->ir, state);
191 }
192
193 shader->symbols = state->symbols;
194 shader->CompileStatus = !state->error;
195 shader->Version = state->language_version;
196 shader->IsES = state->es_shader;
197 memcpy(shader->builtins_to_link, state->builtins_to_link,
198 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
199 shader->num_builtins_to_link = state->num_builtins_to_link;
200
201 if (shader->InfoLog)
202 ralloc_free(shader->InfoLog);
203
204 shader->InfoLog = state->info_log;
205
206 /* Retain any live IR, but trash the rest. */
207 reparent_ir(shader->ir, shader);
208
209 ralloc_free(state);
210
211 return;
212 }
213
214 int
215 main(int argc, char **argv)
216 {
217 int status = EXIT_SUCCESS;
218 struct gl_context local_ctx;
219 struct gl_context *ctx = &local_ctx;
220
221 int c;
222 int idx = 0;
223 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
224 /* empty */ ;
225
226
227 if (argc <= optind)
228 usage_fail(argv[0]);
229
230 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT);
231
232 struct gl_shader_program *whole_program;
233
234 whole_program = rzalloc (NULL, struct gl_shader_program);
235 assert(whole_program != NULL);
236 whole_program->InfoLog = ralloc_strdup(whole_program, "");
237
238 for (/* empty */; argc > optind; optind++) {
239 whole_program->Shaders =
240 reralloc(whole_program, whole_program->Shaders,
241 struct gl_shader *, whole_program->NumShaders + 1);
242 assert(whole_program->Shaders != NULL);
243
244 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
245
246 whole_program->Shaders[whole_program->NumShaders] = shader;
247 whole_program->NumShaders++;
248
249 const unsigned len = strlen(argv[optind]);
250 if (len < 6)
251 usage_fail(argv[0]);
252
253 const char *const ext = & argv[optind][len - 5];
254 if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
255 shader->Type = GL_VERTEX_SHADER;
256 else if (strncmp(".geom", ext, 5) == 0)
257 shader->Type = GL_GEOMETRY_SHADER;
258 else if (strncmp(".frag", ext, 5) == 0)
259 shader->Type = GL_FRAGMENT_SHADER;
260 else
261 usage_fail(argv[0]);
262
263 shader->Source = load_text_file(whole_program, argv[optind]);
264 if (shader->Source == NULL) {
265 printf("File \"%s\" does not exist.\n", argv[optind]);
266 exit(EXIT_FAILURE);
267 }
268
269 compile_shader(ctx, shader);
270
271 if (!shader->CompileStatus) {
272 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
273 status = EXIT_FAILURE;
274 break;
275 }
276 }
277
278 if ((status == EXIT_SUCCESS) && do_link) {
279 link_shaders(ctx, whole_program);
280 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
281
282 if (strlen(whole_program->InfoLog) > 0)
283 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
284 }
285
286 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
287 ralloc_free(whole_program->_LinkedShaders[i]);
288
289 ralloc_free(whole_program);
290 _mesa_glsl_release_types();
291 _mesa_glsl_release_functions();
292
293 return status;
294 }