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