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