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