2 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
26 #include "glsl_parser_extras.h"
27 #include "ir_optimization.h"
28 #include "ir_print_visitor.h"
30 #include "loop_analysis.h"
31 #include "standalone_scaffolding.h"
34 initialize_context(struct gl_context *ctx, gl_api api)
36 initialize_context_to_defaults(ctx, api);
38 /* GLSL 1.30 isn't fully supported, but we need to advertise 1.30 so that
39 * the built-in functions for 1.30 can be built.
41 ctx->Const.GLSLVersion = 130;
43 ctx->Const.MaxClipPlanes = 8;
44 ctx->Const.MaxDrawBuffers = 2;
46 /* More than the 1.10 minimum to appease parser tests taken from
47 * apps that (hopefully) already checked the number of coords.
49 ctx->Const.MaxTextureCoordUnits = 4;
51 ctx->Driver.NewShader = _mesa_new_shader;
54 /* Returned string will have 'ctx' as its ralloc owner. */
56 load_text_file(void *ctx, const char *file_name)
60 size_t total_read = 0;
61 FILE *fp = fopen(file_name, "rb");
67 fseek(fp, 0L, SEEK_END);
69 fseek(fp, 0L, SEEK_SET);
71 text = (char *) ralloc_size(ctx, size + 1);
74 size_t bytes = fread(text + total_read,
75 1, size - total_read, fp);
76 if (bytes < size - total_read) {
87 } while (total_read < size);
89 text[total_read] = '\0';
103 const struct option compiler_opts[] = {
104 { "glsl-es", 0, &glsl_es, 1 },
105 { "dump-ast", 0, &dump_ast, 1 },
106 { "dump-hir", 0, &dump_hir, 1 },
107 { "dump-lir", 0, &dump_lir, 1 },
108 { "link", 0, &do_link, 1 },
113 * \brief Print proper usage and exit with failure.
116 usage_fail(const char *name)
120 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
122 "Possible options are:\n";
123 printf(header, name, name);
124 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
125 printf(" --%s\n", o->name);
132 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
134 struct _mesa_glsl_parse_state *state =
135 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
137 const char *source = shader->Source;
138 state->error = preprocess(state, &source, &state->info_log,
139 state->extensions, ctx->API) != 0;
142 _mesa_glsl_lexer_ctor(state, source);
143 _mesa_glsl_parse(state);
144 _mesa_glsl_lexer_dtor(state);
148 foreach_list_const(n, &state->translation_unit) {
149 ast_node *ast = exec_node_data(ast_node, n, link);
155 shader->ir = new(shader) exec_list;
156 if (!state->error && !state->translation_unit.is_empty())
157 _mesa_ast_to_hir(shader->ir, state);
159 /* Print out the unoptimized IR. */
160 if (!state->error && dump_hir) {
161 validate_ir_tree(shader->ir);
162 _mesa_print_ir(shader->ir, state);
165 /* Optimization passes */
166 if (!state->error && !shader->ir->is_empty()) {
169 progress = do_common_optimization(shader->ir, false, false, 32);
172 validate_ir_tree(shader->ir);
176 /* Print out the resulting IR */
177 if (!state->error && dump_lir) {
178 _mesa_print_ir(shader->ir, state);
181 shader->symbols = state->symbols;
182 shader->CompileStatus = !state->error;
183 shader->Version = state->language_version;
184 memcpy(shader->builtins_to_link, state->builtins_to_link,
185 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
186 shader->num_builtins_to_link = state->num_builtins_to_link;
189 ralloc_free(shader->InfoLog);
191 shader->InfoLog = state->info_log;
193 /* Retain any live IR, but trash the rest. */
194 reparent_ir(shader->ir, shader);
202 main(int argc, char **argv)
204 int status = EXIT_SUCCESS;
205 struct gl_context local_ctx;
206 struct gl_context *ctx = &local_ctx;
210 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
217 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL);
219 struct gl_shader_program *whole_program;
221 whole_program = rzalloc (NULL, struct gl_shader_program);
222 assert(whole_program != NULL);
223 whole_program->InfoLog = ralloc_strdup(whole_program, "");
225 for (/* empty */; argc > optind; optind++) {
226 whole_program->Shaders =
227 reralloc(whole_program, whole_program->Shaders,
228 struct gl_shader *, whole_program->NumShaders + 1);
229 assert(whole_program->Shaders != NULL);
231 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
233 whole_program->Shaders[whole_program->NumShaders] = shader;
234 whole_program->NumShaders++;
236 const unsigned len = strlen(argv[optind]);
240 const char *const ext = & argv[optind][len - 5];
241 if (strncmp(".vert", ext, 5) == 0)
242 shader->Type = GL_VERTEX_SHADER;
243 else if (strncmp(".geom", ext, 5) == 0)
244 shader->Type = GL_GEOMETRY_SHADER;
245 else if (strncmp(".frag", ext, 5) == 0)
246 shader->Type = GL_FRAGMENT_SHADER;
250 shader->Source = load_text_file(whole_program, argv[optind]);
251 if (shader->Source == NULL) {
252 printf("File \"%s\" does not exist.\n", argv[optind]);
256 compile_shader(ctx, shader);
258 if (!shader->CompileStatus) {
259 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
260 status = EXIT_FAILURE;
265 if ((status == EXIT_SUCCESS) && do_link) {
266 link_shaders(ctx, whole_program);
267 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
269 if (strlen(whole_program->InfoLog) > 0)
270 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
273 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
274 ralloc_free(whole_program->_LinkedShaders[i]);
276 ralloc_free(whole_program);
277 _mesa_glsl_release_types();
278 _mesa_glsl_release_functions();