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