Use C-style system headers in C++ code to avoid issues with std:: namespace
[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
33 extern "C" struct gl_shader *
34 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
35
36 extern "C" void
37 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
38 struct gl_shader *sh);
39
40 /* Copied from shader_api.c for the stand-alone compiler.
41 */
42 void
43 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
44 struct gl_shader *sh)
45 {
46 *ptr = sh;
47 }
48
49 struct gl_shader *
50 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
51 {
52 struct gl_shader *shader;
53
54 (void) ctx;
55
56 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
57 shader = rzalloc(NULL, struct gl_shader);
58 if (shader) {
59 shader->Type = type;
60 shader->Name = name;
61 shader->RefCount = 1;
62 }
63 return shader;
64 }
65
66 static void
67 initialize_context(struct gl_context *ctx, gl_api api)
68 {
69 memset(ctx, 0, sizeof(*ctx));
70
71 ctx->API = api;
72
73 ctx->Extensions.ARB_ES2_compatibility = GL_TRUE;
74 ctx->Extensions.ARB_draw_buffers = GL_TRUE;
75 ctx->Extensions.ARB_draw_instanced = GL_TRUE;
76 ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
77 ctx->Extensions.EXT_texture_array = GL_TRUE;
78 ctx->Extensions.NV_texture_rectangle = GL_TRUE;
79
80 /* GLSL 1.30 isn't fully supported, but we need to advertise 1.30 so that
81 * the built-in functions for 1.30 can be built.
82 */
83 ctx->Const.GLSLVersion = 130;
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 ralloc owner. */
109 static char *
110 load_text_file(void *ctx, const char *file_name)
111 {
112 char *text = NULL;
113 size_t size;
114 size_t total_read = 0;
115 FILE *fp = fopen(file_name, "rb");
116
117 if (!fp) {
118 return NULL;
119 }
120
121 fseek(fp, 0L, SEEK_END);
122 size = ftell(fp);
123 fseek(fp, 0L, SEEK_SET);
124
125 text = (char *) ralloc_size(ctx, size + 1);
126 if (text != NULL) {
127 do {
128 size_t bytes = fread(text + total_read,
129 1, size - total_read, fp);
130 if (bytes < size - total_read) {
131 free(text);
132 text = NULL;
133 break;
134 }
135
136 if (bytes == 0) {
137 break;
138 }
139
140 total_read += bytes;
141 } while (total_read < size);
142
143 text[total_read] = '\0';
144 }
145
146 fclose(fp);
147
148 return text;
149 }
150
151 int glsl_es = 0;
152 int dump_ast = 0;
153 int dump_hir = 0;
154 int dump_lir = 0;
155 int do_link = 0;
156
157 const struct option compiler_opts[] = {
158 { "glsl-es", 0, &glsl_es, 1 },
159 { "dump-ast", 0, &dump_ast, 1 },
160 { "dump-hir", 0, &dump_hir, 1 },
161 { "dump-lir", 0, &dump_lir, 1 },
162 { "link", 0, &do_link, 1 },
163 { NULL, 0, NULL, 0 }
164 };
165
166 /**
167 * \brief Print proper usage and exit with failure.
168 */
169 void
170 usage_fail(const char *name)
171 {
172
173 const char *header =
174 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
175 "\n"
176 "Possible options are:\n";
177 printf(header, name, name);
178 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
179 printf(" --%s\n", o->name);
180 }
181 exit(EXIT_FAILURE);
182 }
183
184
185 void
186 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
187 {
188 struct _mesa_glsl_parse_state *state =
189 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
190
191 const char *source = shader->Source;
192 state->error = preprocess(state, &source, &state->info_log,
193 state->extensions, ctx->API) != 0;
194
195 if (!state->error) {
196 _mesa_glsl_lexer_ctor(state, source);
197 _mesa_glsl_parse(state);
198 _mesa_glsl_lexer_dtor(state);
199 }
200
201 if (dump_ast) {
202 foreach_list_const(n, &state->translation_unit) {
203 ast_node *ast = exec_node_data(ast_node, n, link);
204 ast->print();
205 }
206 printf("\n\n");
207 }
208
209 shader->ir = new(shader) exec_list;
210 if (!state->error && !state->translation_unit.is_empty())
211 _mesa_ast_to_hir(shader->ir, state);
212
213 /* Print out the unoptimized IR. */
214 if (!state->error && dump_hir) {
215 validate_ir_tree(shader->ir);
216 _mesa_print_ir(shader->ir, state);
217 }
218
219 /* Optimization passes */
220 if (!state->error && !shader->ir->is_empty()) {
221 bool progress;
222 do {
223 progress = do_common_optimization(shader->ir, false, 32);
224 } while (progress);
225
226 validate_ir_tree(shader->ir);
227 }
228
229
230 /* Print out the resulting IR */
231 if (!state->error && dump_lir) {
232 _mesa_print_ir(shader->ir, state);
233 }
234
235 shader->symbols = state->symbols;
236 shader->CompileStatus = !state->error;
237 shader->Version = state->language_version;
238 memcpy(shader->builtins_to_link, state->builtins_to_link,
239 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
240 shader->num_builtins_to_link = state->num_builtins_to_link;
241
242 if (shader->InfoLog)
243 ralloc_free(shader->InfoLog);
244
245 shader->InfoLog = state->info_log;
246
247 /* Retain any live IR, but trash the rest. */
248 reparent_ir(shader->ir, shader);
249
250 ralloc_free(state);
251
252 return;
253 }
254
255 int
256 main(int argc, char **argv)
257 {
258 int status = EXIT_SUCCESS;
259 struct gl_context local_ctx;
260 struct gl_context *ctx = &local_ctx;
261
262 int c;
263 int idx = 0;
264 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
265 /* empty */ ;
266
267
268 if (argc <= optind)
269 usage_fail(argv[0]);
270
271 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL);
272
273 struct gl_shader_program *whole_program;
274
275 whole_program = rzalloc (NULL, struct gl_shader_program);
276 assert(whole_program != NULL);
277
278 for (/* empty */; argc > optind; optind++) {
279 whole_program->Shaders =
280 reralloc(whole_program, whole_program->Shaders,
281 struct gl_shader *, whole_program->NumShaders + 1);
282 assert(whole_program->Shaders != NULL);
283
284 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
285
286 whole_program->Shaders[whole_program->NumShaders] = shader;
287 whole_program->NumShaders++;
288
289 const unsigned len = strlen(argv[optind]);
290 if (len < 6)
291 usage_fail(argv[0]);
292
293 const char *const ext = & argv[optind][len - 5];
294 if (strncmp(".vert", ext, 5) == 0)
295 shader->Type = GL_VERTEX_SHADER;
296 else if (strncmp(".geom", ext, 5) == 0)
297 shader->Type = GL_GEOMETRY_SHADER;
298 else if (strncmp(".frag", ext, 5) == 0)
299 shader->Type = GL_FRAGMENT_SHADER;
300 else
301 usage_fail(argv[0]);
302
303 shader->Source = load_text_file(whole_program, argv[optind]);
304 if (shader->Source == NULL) {
305 printf("File \"%s\" does not exist.\n", argv[optind]);
306 exit(EXIT_FAILURE);
307 }
308
309 compile_shader(ctx, shader);
310
311 if (!shader->CompileStatus) {
312 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
313 status = EXIT_FAILURE;
314 break;
315 }
316 }
317
318 if ((status == EXIT_SUCCESS) && do_link) {
319 link_shaders(ctx, whole_program);
320 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
321
322 if (strlen(whole_program->InfoLog) > 0)
323 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
324 }
325
326 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
327 ralloc_free(whole_program->_LinkedShaders[i]);
328
329 ralloc_free(whole_program);
330 _mesa_glsl_release_types();
331 _mesa_glsl_release_functions();
332
333 return status;
334 }