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