mesa: do not use a constant for MAX_COMPUTE_SHARED_SIZE
[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.MaxComputeSharedMemorySize = 32768;
62 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
63 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
64 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
65 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
66 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8;
67 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8;
68 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8;
69 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12;
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.MaxVarying = 60 / 4;
185 break;
186 case 300:
187 ctx->Const.MaxClipPlanes = 8;
188 ctx->Const.MaxCombinedTextureImageUnits = 32;
189 ctx->Const.MaxDrawBuffers = 4;
190 ctx->Const.MinProgramTexelOffset = -8;
191 ctx->Const.MaxProgramTexelOffset = 7;
192 ctx->Const.MaxLights = 0;
193 ctx->Const.MaxTextureCoordUnits = 0;
194 ctx->Const.MaxTextureUnits = 0;
195
196 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
197 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
198 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
199 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
200 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
201
202 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
203 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
204 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
205 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
206
207 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
208 break;
209 }
210
211 ctx->Const.GenerateTemporaryNames = true;
212 ctx->Const.MaxPatchVertices = 32;
213
214 ctx->Driver.NewShader = _mesa_new_shader;
215 }
216
217 /* Returned string will have 'ctx' as its ralloc owner. */
218 static char *
219 load_text_file(void *ctx, const char *file_name)
220 {
221 char *text = NULL;
222 size_t size;
223 size_t total_read = 0;
224 FILE *fp = fopen(file_name, "rb");
225
226 if (!fp) {
227 return NULL;
228 }
229
230 fseek(fp, 0L, SEEK_END);
231 size = ftell(fp);
232 fseek(fp, 0L, SEEK_SET);
233
234 text = (char *) ralloc_size(ctx, size + 1);
235 if (text != NULL) {
236 do {
237 size_t bytes = fread(text + total_read,
238 1, size - total_read, fp);
239 if (bytes < size - total_read) {
240 free(text);
241 text = NULL;
242 goto error;
243 }
244
245 if (bytes == 0) {
246 break;
247 }
248
249 total_read += bytes;
250 } while (total_read < size);
251
252 text[total_read] = '\0';
253 error:;
254 }
255
256 fclose(fp);
257
258 return text;
259 }
260
261 int dump_ast = 0;
262 int dump_hir = 0;
263 int dump_lir = 0;
264 int do_link = 0;
265
266 const struct option compiler_opts[] = {
267 { "dump-ast", no_argument, &dump_ast, 1 },
268 { "dump-hir", no_argument, &dump_hir, 1 },
269 { "dump-lir", no_argument, &dump_lir, 1 },
270 { "link", no_argument, &do_link, 1 },
271 { "version", required_argument, NULL, 'v' },
272 { NULL, 0, NULL, 0 }
273 };
274
275 /**
276 * \brief Print proper usage and exit with failure.
277 */
278 void
279 usage_fail(const char *name)
280 {
281
282 const char *header =
283 "usage: %s [options] <file.vert | file.tesc | file.tese | file.geom | file.frag | file.comp>\n"
284 "\n"
285 "Possible options are:\n";
286 printf(header, name);
287 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
288 printf(" --%s\n", o->name);
289 }
290 exit(EXIT_FAILURE);
291 }
292
293
294 void
295 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
296 {
297 struct _mesa_glsl_parse_state *state =
298 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
299
300 _mesa_glsl_compile_shader(ctx, shader, dump_ast, dump_hir);
301
302 /* Print out the resulting IR */
303 if (!state->error && dump_lir) {
304 _mesa_print_ir(stdout, shader->ir, state);
305 }
306
307 return;
308 }
309
310 int
311 main(int argc, char **argv)
312 {
313 int status = EXIT_SUCCESS;
314 struct gl_context local_ctx;
315 struct gl_context *ctx = &local_ctx;
316 bool glsl_es = false;
317
318 int c;
319 int idx = 0;
320 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) {
321 switch (c) {
322 case 'v':
323 glsl_version = strtol(optarg, NULL, 10);
324 switch (glsl_version) {
325 case 100:
326 case 300:
327 glsl_es = true;
328 break;
329 case 110:
330 case 120:
331 case 130:
332 case 140:
333 case 150:
334 case 330:
335 glsl_es = false;
336 break;
337 default:
338 fprintf(stderr, "Unrecognized GLSL version `%s'\n", optarg);
339 usage_fail(argv[0]);
340 break;
341 }
342 break;
343 default:
344 break;
345 }
346 }
347
348
349 if (argc <= optind)
350 usage_fail(argv[0]);
351
352 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT);
353
354 struct gl_shader_program *whole_program;
355
356 whole_program = rzalloc (NULL, struct gl_shader_program);
357 assert(whole_program != NULL);
358 whole_program->InfoLog = ralloc_strdup(whole_program, "");
359
360 /* Created just to avoid segmentation faults */
361 whole_program->AttributeBindings = new string_to_uint_map;
362 whole_program->FragDataBindings = new string_to_uint_map;
363 whole_program->FragDataIndexBindings = new string_to_uint_map;
364
365 for (/* empty */; argc > optind; optind++) {
366 whole_program->Shaders =
367 reralloc(whole_program, whole_program->Shaders,
368 struct gl_shader *, whole_program->NumShaders + 1);
369 assert(whole_program->Shaders != NULL);
370
371 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
372
373 whole_program->Shaders[whole_program->NumShaders] = shader;
374 whole_program->NumShaders++;
375
376 const unsigned len = strlen(argv[optind]);
377 if (len < 6)
378 usage_fail(argv[0]);
379
380 const char *const ext = & argv[optind][len - 5];
381 if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
382 shader->Type = GL_VERTEX_SHADER;
383 else if (strncmp(".tesc", ext, 5) == 0)
384 shader->Type = GL_TESS_CONTROL_SHADER;
385 else if (strncmp(".tese", ext, 5) == 0)
386 shader->Type = GL_TESS_EVALUATION_SHADER;
387 else if (strncmp(".geom", ext, 5) == 0)
388 shader->Type = GL_GEOMETRY_SHADER;
389 else if (strncmp(".frag", ext, 5) == 0)
390 shader->Type = GL_FRAGMENT_SHADER;
391 else if (strncmp(".comp", ext, 5) == 0)
392 shader->Type = GL_COMPUTE_SHADER;
393 else
394 usage_fail(argv[0]);
395 shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
396
397 shader->Source = load_text_file(whole_program, argv[optind]);
398 if (shader->Source == NULL) {
399 printf("File \"%s\" does not exist.\n", argv[optind]);
400 exit(EXIT_FAILURE);
401 }
402
403 compile_shader(ctx, shader);
404
405 if (strlen(shader->InfoLog) > 0)
406 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
407
408 if (!shader->CompileStatus) {
409 status = EXIT_FAILURE;
410 break;
411 }
412 }
413
414 if ((status == EXIT_SUCCESS) && do_link) {
415 _mesa_clear_shader_program_data(whole_program);
416
417 link_shaders(ctx, whole_program);
418 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
419
420 if (strlen(whole_program->InfoLog) > 0)
421 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
422 }
423
424 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++)
425 ralloc_free(whole_program->_LinkedShaders[i]);
426
427 delete whole_program->AttributeBindings;
428 delete whole_program->FragDataBindings;
429 delete whole_program->FragDataIndexBindings;
430
431 ralloc_free(whole_program);
432 _mesa_glsl_release_types();
433 _mesa_glsl_release_builtin_functions();
434
435 return status;
436 }