glsl: move to compiler/
[mesa.git] / src / compiler / 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 "program.h"
38 #include "program/hash_table.h"
39 #include "loop_analysis.h"
40 #include "standalone_scaffolding.h"
41
42 static int glsl_version = 330;
43
44 static void
45 initialize_context(struct gl_context *ctx, gl_api api)
46 {
47 initialize_context_to_defaults(ctx, api);
48
49 /* The standalone compiler needs to claim support for almost
50 * everything in order to compile the built-in functions.
51 */
52 ctx->Const.GLSLVersion = glsl_version;
53 ctx->Extensions.ARB_ES3_compatibility = true;
54 ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
55 ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
56 ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
57 ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
58 ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
59 ctx->Const.MaxComputeWorkGroupSize[2] = 64;
60 ctx->Const.MaxComputeWorkGroupInvocations = 1024;
61 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
62 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
63 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
64 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
65
66 switch (ctx->Const.GLSLVersion) {
67 case 100:
68 ctx->Const.MaxClipPlanes = 0;
69 ctx->Const.MaxCombinedTextureImageUnits = 8;
70 ctx->Const.MaxDrawBuffers = 2;
71 ctx->Const.MinProgramTexelOffset = 0;
72 ctx->Const.MaxProgramTexelOffset = 0;
73 ctx->Const.MaxLights = 0;
74 ctx->Const.MaxTextureCoordUnits = 0;
75 ctx->Const.MaxTextureUnits = 8;
76
77 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
78 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
79 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
80 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
81 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
82
83 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
84 ctx->Const.MaxCombinedTextureImageUnits;
85 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
86 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
87 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
88 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
89
90 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
91 break;
92 case 110:
93 case 120:
94 ctx->Const.MaxClipPlanes = 6;
95 ctx->Const.MaxCombinedTextureImageUnits = 2;
96 ctx->Const.MaxDrawBuffers = 1;
97 ctx->Const.MinProgramTexelOffset = 0;
98 ctx->Const.MaxProgramTexelOffset = 0;
99 ctx->Const.MaxLights = 8;
100 ctx->Const.MaxTextureCoordUnits = 2;
101 ctx->Const.MaxTextureUnits = 2;
102
103 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
104 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
105 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
106 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
107 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
108
109 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
110 ctx->Const.MaxCombinedTextureImageUnits;
111 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
112 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
113 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
114 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
115
116 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
117 break;
118 case 130:
119 case 140:
120 ctx->Const.MaxClipPlanes = 8;
121 ctx->Const.MaxCombinedTextureImageUnits = 16;
122 ctx->Const.MaxDrawBuffers = 8;
123 ctx->Const.MinProgramTexelOffset = -8;
124 ctx->Const.MaxProgramTexelOffset = 7;
125 ctx->Const.MaxLights = 8;
126 ctx->Const.MaxTextureCoordUnits = 8;
127 ctx->Const.MaxTextureUnits = 2;
128
129 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
130 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
131 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
132 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
133 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
134
135 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
136 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
137 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
138 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
139 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
140
141 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
142 break;
143 case 150:
144 case 330:
145 ctx->Const.MaxClipPlanes = 8;
146 ctx->Const.MaxDrawBuffers = 8;
147 ctx->Const.MinProgramTexelOffset = -8;
148 ctx->Const.MaxProgramTexelOffset = 7;
149 ctx->Const.MaxLights = 8;
150 ctx->Const.MaxTextureCoordUnits = 8;
151 ctx->Const.MaxTextureUnits = 2;
152
153 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
154 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
155 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
156 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
157 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
158
159 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
160 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024;
161 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents =
162 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
163 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
164
165 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
166 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
167 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
168 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
169 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
170
171 ctx->Const.MaxCombinedTextureImageUnits =
172 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits
173 + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits
174 + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
175
176 ctx->Const.MaxGeometryOutputVertices = 256;
177 ctx->Const.MaxGeometryTotalOutputComponents = 1024;
178
179 ctx->Const.MaxVarying = 60 / 4;
180 break;
181 case 300:
182 ctx->Const.MaxClipPlanes = 8;
183 ctx->Const.MaxCombinedTextureImageUnits = 32;
184 ctx->Const.MaxDrawBuffers = 4;
185 ctx->Const.MinProgramTexelOffset = -8;
186 ctx->Const.MaxProgramTexelOffset = 7;
187 ctx->Const.MaxLights = 0;
188 ctx->Const.MaxTextureCoordUnits = 0;
189 ctx->Const.MaxTextureUnits = 0;
190
191 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
192 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
193 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
194 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
195 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
196
197 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
198 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
199 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
200 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
201
202 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
203 break;
204 }
205
206 ctx->Const.GenerateTemporaryNames = true;
207 ctx->Const.MaxPatchVertices = 32;
208
209 ctx->Driver.NewShader = _mesa_new_shader;
210 }
211
212 /* Returned string will have 'ctx' as its ralloc owner. */
213 static char *
214 load_text_file(void *ctx, const char *file_name)
215 {
216 char *text = NULL;
217 size_t size;
218 size_t total_read = 0;
219 FILE *fp = fopen(file_name, "rb");
220
221 if (!fp) {
222 return NULL;
223 }
224
225 fseek(fp, 0L, SEEK_END);
226 size = ftell(fp);
227 fseek(fp, 0L, SEEK_SET);
228
229 text = (char *) ralloc_size(ctx, size + 1);
230 if (text != NULL) {
231 do {
232 size_t bytes = fread(text + total_read,
233 1, size - total_read, fp);
234 if (bytes < size - total_read) {
235 free(text);
236 text = NULL;
237 goto error;
238 }
239
240 if (bytes == 0) {
241 break;
242 }
243
244 total_read += bytes;
245 } while (total_read < size);
246
247 text[total_read] = '\0';
248 error:;
249 }
250
251 fclose(fp);
252
253 return text;
254 }
255
256 int dump_ast = 0;
257 int dump_hir = 0;
258 int dump_lir = 0;
259 int do_link = 0;
260
261 const struct option compiler_opts[] = {
262 { "dump-ast", no_argument, &dump_ast, 1 },
263 { "dump-hir", no_argument, &dump_hir, 1 },
264 { "dump-lir", no_argument, &dump_lir, 1 },
265 { "link", no_argument, &do_link, 1 },
266 { "version", required_argument, NULL, 'v' },
267 { NULL, 0, NULL, 0 }
268 };
269
270 /**
271 * \brief Print proper usage and exit with failure.
272 */
273 void
274 usage_fail(const char *name)
275 {
276
277 const char *header =
278 "usage: %s [options] <file.vert | file.tesc | file.tese | file.geom | file.frag | file.comp>\n"
279 "\n"
280 "Possible options are:\n";
281 printf(header, name);
282 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
283 printf(" --%s\n", o->name);
284 }
285 exit(EXIT_FAILURE);
286 }
287
288
289 void
290 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
291 {
292 struct _mesa_glsl_parse_state *state =
293 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
294
295 _mesa_glsl_compile_shader(ctx, shader, dump_ast, dump_hir);
296
297 /* Print out the resulting IR */
298 if (!state->error && dump_lir) {
299 _mesa_print_ir(stdout, shader->ir, state);
300 }
301
302 return;
303 }
304
305 int
306 main(int argc, char **argv)
307 {
308 int status = EXIT_SUCCESS;
309 struct gl_context local_ctx;
310 struct gl_context *ctx = &local_ctx;
311 bool glsl_es = false;
312
313 int c;
314 int idx = 0;
315 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) {
316 switch (c) {
317 case 'v':
318 glsl_version = strtol(optarg, NULL, 10);
319 switch (glsl_version) {
320 case 100:
321 case 300:
322 glsl_es = true;
323 break;
324 case 110:
325 case 120:
326 case 130:
327 case 140:
328 case 150:
329 case 330:
330 glsl_es = false;
331 break;
332 default:
333 fprintf(stderr, "Unrecognized GLSL version `%s'\n", optarg);
334 usage_fail(argv[0]);
335 break;
336 }
337 break;
338 default:
339 break;
340 }
341 }
342
343
344 if (argc <= optind)
345 usage_fail(argv[0]);
346
347 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT);
348
349 struct gl_shader_program *whole_program;
350
351 whole_program = rzalloc (NULL, struct gl_shader_program);
352 assert(whole_program != NULL);
353 whole_program->InfoLog = ralloc_strdup(whole_program, "");
354
355 /* Created just to avoid segmentation faults */
356 whole_program->AttributeBindings = new string_to_uint_map;
357 whole_program->FragDataBindings = new string_to_uint_map;
358 whole_program->FragDataIndexBindings = new string_to_uint_map;
359
360 for (/* empty */; argc > optind; optind++) {
361 whole_program->Shaders =
362 reralloc(whole_program, whole_program->Shaders,
363 struct gl_shader *, whole_program->NumShaders + 1);
364 assert(whole_program->Shaders != NULL);
365
366 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
367
368 whole_program->Shaders[whole_program->NumShaders] = shader;
369 whole_program->NumShaders++;
370
371 const unsigned len = strlen(argv[optind]);
372 if (len < 6)
373 usage_fail(argv[0]);
374
375 const char *const ext = & argv[optind][len - 5];
376 if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
377 shader->Type = GL_VERTEX_SHADER;
378 else if (strncmp(".tesc", ext, 5) == 0)
379 shader->Type = GL_TESS_CONTROL_SHADER;
380 else if (strncmp(".tese", ext, 5) == 0)
381 shader->Type = GL_TESS_EVALUATION_SHADER;
382 else if (strncmp(".geom", ext, 5) == 0)
383 shader->Type = GL_GEOMETRY_SHADER;
384 else if (strncmp(".frag", ext, 5) == 0)
385 shader->Type = GL_FRAGMENT_SHADER;
386 else if (strncmp(".comp", ext, 5) == 0)
387 shader->Type = GL_COMPUTE_SHADER;
388 else
389 usage_fail(argv[0]);
390 shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
391
392 shader->Source = load_text_file(whole_program, argv[optind]);
393 if (shader->Source == NULL) {
394 printf("File \"%s\" does not exist.\n", argv[optind]);
395 exit(EXIT_FAILURE);
396 }
397
398 compile_shader(ctx, shader);
399
400 if (strlen(shader->InfoLog) > 0)
401 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
402
403 if (!shader->CompileStatus) {
404 status = EXIT_FAILURE;
405 break;
406 }
407 }
408
409 if ((status == EXIT_SUCCESS) && do_link) {
410 _mesa_clear_shader_program_data(whole_program);
411
412 link_shaders(ctx, whole_program);
413 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
414
415 if (strlen(whole_program->InfoLog) > 0)
416 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
417 }
418
419 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++)
420 ralloc_free(whole_program->_LinkedShaders[i]);
421
422 delete whole_program->AttributeBindings;
423 delete whole_program->FragDataBindings;
424 delete whole_program->FragDataIndexBindings;
425
426 ralloc_free(whole_program);
427 _mesa_glsl_release_types();
428 _mesa_glsl_release_builtin_functions();
429
430 return status;
431 }