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 #include <sys/types.h>
33 #include "glsl_parser_extras.h"
34 #include "glsl_parser.h"
35 #include "ir_optimization.h"
36 #include "ir_print_visitor.h"
38 #include "loop_analysis.h"
40 extern "C" struct gl_shader
*
41 _mesa_new_shader(struct gl_context
*ctx
, GLuint name
, GLenum type
);
44 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
45 struct gl_shader
*sh
);
47 /* Copied from shader_api.c for the stand-alone compiler.
50 _mesa_reference_shader(struct gl_context
*ctx
, struct gl_shader
**ptr
,
57 _mesa_new_shader(struct gl_context
*ctx
, GLuint name
, GLenum type
)
59 struct gl_shader
*shader
;
63 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
);
64 shader
= talloc_zero(NULL
, struct gl_shader
);
74 initialize_context(struct gl_context
*ctx
, gl_api api
)
76 memset(ctx
, 0, sizeof(*ctx
));
80 ctx
->Extensions
.ARB_draw_buffers
= GL_TRUE
;
81 ctx
->Extensions
.ARB_fragment_coord_conventions
= GL_TRUE
;
82 ctx
->Extensions
.EXT_texture_array
= GL_TRUE
;
83 ctx
->Extensions
.NV_texture_rectangle
= GL_TRUE
;
86 ctx
->Const
.MaxLights
= 8;
87 ctx
->Const
.MaxClipPlanes
= 8;
88 ctx
->Const
.MaxTextureUnits
= 2;
90 /* More than the 1.10 minimum to appease parser tests taken from
91 * apps that (hopefully) already checked the number of coords.
93 ctx
->Const
.MaxTextureCoordUnits
= 4;
95 ctx
->Const
.VertexProgram
.MaxAttribs
= 16;
96 ctx
->Const
.VertexProgram
.MaxUniformComponents
= 512;
97 ctx
->Const
.MaxVarying
= 8;
98 ctx
->Const
.MaxVertexTextureImageUnits
= 0;
99 ctx
->Const
.MaxCombinedTextureImageUnits
= 2;
100 ctx
->Const
.MaxTextureImageUnits
= 2;
101 ctx
->Const
.FragmentProgram
.MaxUniformComponents
= 64;
103 ctx
->Const
.MaxDrawBuffers
= 2;
105 ctx
->Driver
.NewShader
= _mesa_new_shader
;
108 /* Returned string will have 'ctx' as its talloc owner. */
110 load_text_file(void *ctx
, const char *file_name
)
114 ssize_t total_read
= 0;
115 int fd
= open(file_name
, O_RDONLY
);
121 if (fstat(fd
, & st
) == 0) {
122 text
= (char *) talloc_size(ctx
, st
.st_size
+ 1);
125 ssize_t bytes
= read(fd
, text
+ total_read
,
126 st
.st_size
- total_read
);
138 } while (total_read
< st
.st_size
);
140 text
[total_read
] = '\0';
155 const struct option compiler_opts
[] = {
156 { "glsl-es", 0, &glsl_es
, 1 },
157 { "dump-ast", 0, &dump_ast
, 1 },
158 { "dump-hir", 0, &dump_hir
, 1 },
159 { "dump-lir", 0, &dump_lir
, 1 },
160 { "link", 0, &do_link
, 1 },
165 * \brief Print proper usage and exit with failure.
168 usage_fail(const char *name
)
172 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
174 "Possible options are:\n";
175 printf(header
, name
, name
);
176 for (const struct option
*o
= compiler_opts
; o
->name
!= 0; ++o
) {
177 printf(" --%s\n", o
->name
);
184 compile_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
186 struct _mesa_glsl_parse_state
*state
=
187 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
189 const char *source
= shader
->Source
;
190 state
->error
= preprocess(state
, &source
, &state
->info_log
,
191 state
->extensions
, ctx
->API
);
194 _mesa_glsl_lexer_ctor(state
, source
);
195 _mesa_glsl_parse(state
);
196 _mesa_glsl_lexer_dtor(state
);
200 foreach_list_const(n
, &state
->translation_unit
) {
201 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
207 shader
->ir
= new(shader
) exec_list
;
208 if (!state
->error
&& !state
->translation_unit
.is_empty())
209 _mesa_ast_to_hir(shader
->ir
, state
);
211 /* Print out the unoptimized IR. */
212 if (!state
->error
&& dump_hir
) {
213 validate_ir_tree(shader
->ir
);
214 _mesa_print_ir(shader
->ir
, state
);
217 /* Optimization passes */
218 if (!state
->error
&& !shader
->ir
->is_empty()) {
221 progress
= do_common_optimization(shader
->ir
, false, 32);
224 validate_ir_tree(shader
->ir
);
228 /* Print out the resulting IR */
229 if (!state
->error
&& dump_lir
) {
230 _mesa_print_ir(shader
->ir
, state
);
233 shader
->symbols
= state
->symbols
;
234 shader
->CompileStatus
= !state
->error
;
235 shader
->Version
= state
->language_version
;
236 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
237 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
238 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
241 talloc_free(shader
->InfoLog
);
243 shader
->InfoLog
= state
->info_log
;
245 /* Retain any live IR, but trash the rest. */
246 reparent_ir(shader
->ir
, shader
);
254 main(int argc
, char **argv
)
256 int status
= EXIT_SUCCESS
;
257 struct gl_context local_ctx
;
258 struct gl_context
*ctx
= &local_ctx
;
262 while ((c
= getopt_long(argc
, argv
, "", compiler_opts
, &idx
)) != -1)
269 initialize_context(ctx
, (glsl_es
) ? API_OPENGLES2
: API_OPENGL
);
271 struct gl_shader_program
*whole_program
;
273 whole_program
= talloc_zero (NULL
, struct gl_shader_program
);
274 assert(whole_program
!= NULL
);
276 for (/* empty */; argc
> optind
; optind
++) {
277 whole_program
->Shaders
= (struct gl_shader
**)
278 talloc_realloc(whole_program
, whole_program
->Shaders
,
279 struct gl_shader
*, whole_program
->NumShaders
+ 1);
280 assert(whole_program
->Shaders
!= NULL
);
282 struct gl_shader
*shader
= talloc_zero(whole_program
, gl_shader
);
284 whole_program
->Shaders
[whole_program
->NumShaders
] = shader
;
285 whole_program
->NumShaders
++;
287 const unsigned len
= strlen(argv
[optind
]);
291 const char *const ext
= & argv
[optind
][len
- 5];
292 if (strncmp(".vert", ext
, 5) == 0)
293 shader
->Type
= GL_VERTEX_SHADER
;
294 else if (strncmp(".geom", ext
, 5) == 0)
295 shader
->Type
= GL_GEOMETRY_SHADER
;
296 else if (strncmp(".frag", ext
, 5) == 0)
297 shader
->Type
= GL_FRAGMENT_SHADER
;
301 shader
->Source
= load_text_file(whole_program
, argv
[optind
]);
302 if (shader
->Source
== NULL
) {
303 printf("File \"%s\" does not exist.\n", argv
[optind
]);
307 compile_shader(ctx
, shader
);
309 if (!shader
->CompileStatus
) {
310 printf("Info log for %s:\n%s\n", argv
[optind
], shader
->InfoLog
);
311 status
= EXIT_FAILURE
;
316 if ((status
== EXIT_SUCCESS
) && do_link
) {
317 link_shaders(ctx
, whole_program
);
318 status
= (whole_program
->LinkStatus
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
320 if (strlen(whole_program
->InfoLog
) > 0)
321 printf("Info log for linking:\n%s\n", whole_program
->InfoLog
);
324 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++)
325 talloc_free(whole_program
->_LinkedShaders
[i
]);
327 talloc_free(whole_program
);
328 _mesa_glsl_release_types();
329 _mesa_glsl_release_functions();