2 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
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.
35 #include "glsl_parser_extras.h"
36 #include "ir_optimization.h"
38 #include "loop_analysis.h"
39 #include "standalone_scaffolding.h"
41 static int glsl_version
= 330;
44 initialize_context(struct gl_context
*ctx
, gl_api api
)
46 initialize_context_to_defaults(ctx
, api
);
48 /* The standalone compiler needs to claim support for almost
49 * everything in order to compile the built-in functions.
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 */
58 switch (ctx
->Const
.GLSLVersion
) {
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;
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;
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 */
82 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4;
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;
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;
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 */
108 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4;
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;
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;
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 */
133 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4;
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;
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;
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;
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 */
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
;
168 ctx
->Const
.MaxGeometryOutputVertices
= 256;
169 ctx
->Const
.MaxGeometryTotalOutputComponents
= 1024;
171 // ctx->Const.MaxGeometryVaryingComponents = 64;
173 ctx
->Const
.MaxVarying
= 60 / 4;
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;
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;
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 */
196 ctx
->Const
.MaxVarying
= ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4;
200 ctx
->Driver
.NewShader
= _mesa_new_shader
;
203 /* Returned string will have 'ctx' as its ralloc owner. */
205 load_text_file(void *ctx
, const char *file_name
)
209 size_t total_read
= 0;
210 FILE *fp
= fopen(file_name
, "rb");
216 fseek(fp
, 0L, SEEK_END
);
218 fseek(fp
, 0L, SEEK_SET
);
220 text
= (char *) ralloc_size(ctx
, size
+ 1);
223 size_t bytes
= fread(text
+ total_read
,
224 1, size
- total_read
, fp
);
225 if (bytes
< size
- total_read
) {
236 } while (total_read
< size
);
238 text
[total_read
] = '\0';
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' },
261 * \brief Print proper usage and exit with failure.
264 usage_fail(const char *name
)
268 "usage: %s [options] <file.vert | file.geom | file.frag>\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
);
280 compile_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
282 struct _mesa_glsl_parse_state
*state
=
283 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Stage
, shader
);
285 _mesa_glsl_compile_shader(ctx
, shader
, dump_ast
, dump_hir
);
287 /* Print out the resulting IR */
288 if (!state
->error
&& dump_lir
) {
289 _mesa_print_ir(shader
->ir
, state
);
296 main(int argc
, char **argv
)
298 int status
= EXIT_SUCCESS
;
299 struct gl_context local_ctx
;
300 struct gl_context
*ctx
= &local_ctx
;
301 bool glsl_es
= false;
305 while ((c
= getopt_long(argc
, argv
, "", compiler_opts
, &idx
)) != -1) {
308 glsl_version
= strtol(optarg
, NULL
, 10);
309 switch (glsl_version
) {
323 fprintf(stderr
, "Unrecognized GLSL version `%s'\n", optarg
);
337 initialize_context(ctx
, (glsl_es
) ? API_OPENGLES2
: API_OPENGL_COMPAT
);
339 struct gl_shader_program
*whole_program
;
341 whole_program
= rzalloc (NULL
, struct gl_shader_program
);
342 assert(whole_program
!= NULL
);
343 whole_program
->InfoLog
= ralloc_strdup(whole_program
, "");
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
);
351 struct gl_shader
*shader
= rzalloc(whole_program
, gl_shader
);
353 whole_program
->Shaders
[whole_program
->NumShaders
] = shader
;
354 whole_program
->NumShaders
++;
356 const unsigned len
= strlen(argv
[optind
]);
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 if (strncmp(".comp", ext
, 5) == 0)
368 shader
->Type
= GL_COMPUTE_SHADER
;
371 shader
->Stage
= _mesa_shader_enum_to_shader_stage(shader
->Type
);
373 shader
->Source
= load_text_file(whole_program
, argv
[optind
]);
374 if (shader
->Source
== NULL
) {
375 printf("File \"%s\" does not exist.\n", argv
[optind
]);
379 compile_shader(ctx
, shader
);
381 if (strlen(shader
->InfoLog
) > 0)
382 printf("Info log for %s:\n%s\n", argv
[optind
], shader
->InfoLog
);
384 if (!shader
->CompileStatus
) {
385 status
= EXIT_FAILURE
;
390 if ((status
== EXIT_SUCCESS
) && do_link
) {
391 link_shaders(ctx
, whole_program
);
392 status
= (whole_program
->LinkStatus
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
394 if (strlen(whole_program
->InfoLog
) > 0)
395 printf("Info log for linking:\n%s\n", whole_program
->InfoLog
);
398 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++)
399 ralloc_free(whole_program
->_LinkedShaders
[i
]);
401 ralloc_free(whole_program
);
402 _mesa_glsl_release_types();
403 _mesa_glsl_release_builtin_functions();