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(GLcontext
*ctx
, GLuint name
, GLenum type
);
43 /* Copied from shader_api.c for the stand-alone compiler.
46 _mesa_new_shader(GLcontext
*ctx
, GLuint name
, GLenum type
)
48 struct gl_shader
*shader
;
52 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
);
53 shader
= talloc_zero(NULL
, struct gl_shader
);
63 initialize_context(GLcontext
*ctx
, gl_api api
)
65 memset(ctx
, 0, sizeof(*ctx
));
69 ctx
->Extensions
.ARB_draw_buffers
= GL_TRUE
;
70 ctx
->Extensions
.ARB_fragment_coord_conventions
= GL_TRUE
;
71 ctx
->Extensions
.EXT_texture_array
= GL_TRUE
;
72 ctx
->Extensions
.NV_texture_rectangle
= GL_TRUE
;
75 ctx
->Const
.MaxLights
= 8;
76 ctx
->Const
.MaxClipPlanes
= 8;
77 ctx
->Const
.MaxTextureUnits
= 2;
79 /* More than the 1.10 minimum to appease parser tests taken from
80 * apps that (hopefully) already checked the number of coords.
82 ctx
->Const
.MaxTextureCoordUnits
= 4;
84 ctx
->Const
.VertexProgram
.MaxAttribs
= 16;
85 ctx
->Const
.VertexProgram
.MaxUniformComponents
= 512;
86 ctx
->Const
.MaxVarying
= 8;
87 ctx
->Const
.MaxVertexTextureImageUnits
= 0;
88 ctx
->Const
.MaxCombinedTextureImageUnits
= 2;
89 ctx
->Const
.MaxTextureImageUnits
= 2;
90 ctx
->Const
.FragmentProgram
.MaxUniformComponents
= 64;
92 ctx
->Const
.MaxDrawBuffers
= 2;
94 ctx
->Driver
.NewShader
= _mesa_new_shader
;
97 /* Returned string will have 'ctx' as its talloc owner. */
99 load_text_file(void *ctx
, const char *file_name
)
103 ssize_t total_read
= 0;
104 int fd
= open(file_name
, O_RDONLY
);
110 if (fstat(fd
, & st
) == 0) {
111 text
= (char *) talloc_size(ctx
, st
.st_size
+ 1);
114 ssize_t bytes
= read(fd
, text
+ total_read
,
115 st
.st_size
- total_read
);
127 } while (total_read
< st
.st_size
);
129 text
[total_read
] = '\0';
140 usage_fail(const char *name
)
142 printf("%s <filename.frag|filename.vert>\n", name
);
153 const struct option compiler_opts
[] = {
154 { "glsl-es", 0, &glsl_es
, 1 },
155 { "dump-ast", 0, &dump_ast
, 1 },
156 { "dump-hir", 0, &dump_hir
, 1 },
157 { "dump-lir", 0, &dump_lir
, 1 },
158 { "link", 0, &do_link
, 1 },
163 compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
165 struct _mesa_glsl_parse_state
*state
=
166 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
168 const char *source
= shader
->Source
;
169 state
->error
= preprocess(state
, &source
, &state
->info_log
,
170 state
->extensions
, ctx
->API
);
173 _mesa_glsl_lexer_ctor(state
, source
);
174 _mesa_glsl_parse(state
);
175 _mesa_glsl_lexer_dtor(state
);
179 foreach_list_const(n
, &state
->translation_unit
) {
180 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
186 shader
->ir
= new(shader
) exec_list
;
187 if (!state
->error
&& !state
->translation_unit
.is_empty())
188 _mesa_ast_to_hir(shader
->ir
, state
);
190 /* Print out the unoptimized IR. */
191 if (!state
->error
&& dump_hir
) {
192 validate_ir_tree(shader
->ir
);
193 _mesa_print_ir(shader
->ir
, state
);
196 /* Optimization passes */
197 if (!state
->error
&& !shader
->ir
->is_empty()) {
202 progress
= do_function_inlining(shader
->ir
) || progress
;
203 progress
= do_if_simplification(shader
->ir
) || progress
;
204 progress
= do_copy_propagation(shader
->ir
) || progress
;
205 progress
= do_dead_code_local(shader
->ir
) || progress
;
206 progress
= do_dead_code_unlinked(shader
->ir
) || progress
;
207 progress
= do_tree_grafting(shader
->ir
) || progress
;
208 progress
= do_constant_propagation(shader
->ir
) || progress
;
209 progress
= do_constant_variable_unlinked(shader
->ir
) || progress
;
210 progress
= do_constant_folding(shader
->ir
) || progress
;
211 progress
= do_algebraic(shader
->ir
) || progress
;
212 progress
= do_vec_index_to_swizzle(shader
->ir
) || progress
;
213 progress
= do_vec_index_to_cond_assign(shader
->ir
) || progress
;
214 progress
= do_swizzle_swizzle(shader
->ir
) || progress
;
216 loop_state
*ls
= analyze_loop_variables(shader
->ir
);
217 progress
= set_loop_controls(shader
->ir
, ls
) || progress
;
218 progress
= unroll_loops(shader
->ir
, ls
, 32) || progress
;
222 validate_ir_tree(shader
->ir
);
226 /* Print out the resulting IR */
227 if (!state
->error
&& dump_lir
) {
228 _mesa_print_ir(shader
->ir
, state
);
231 shader
->symbols
= state
->symbols
;
232 shader
->CompileStatus
= !state
->error
;
233 shader
->Version
= state
->language_version
;
234 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
235 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
236 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
239 talloc_free(shader
->InfoLog
);
241 shader
->InfoLog
= state
->info_log
;
243 /* Retain any live IR, but trash the rest. */
244 reparent_ir(shader
->ir
, shader
);
252 main(int argc
, char **argv
)
254 int status
= EXIT_SUCCESS
;
256 GLcontext
*ctx
= &local_ctx
;
260 while ((c
= getopt_long(argc
, argv
, "", compiler_opts
, &idx
)) != -1)
267 initialize_context(ctx
, (glsl_es
) ? API_OPENGLES2
: API_OPENGL
);
269 struct gl_shader_program
*whole_program
;
271 whole_program
= talloc_zero (NULL
, struct gl_shader_program
);
272 assert(whole_program
!= NULL
);
274 for (/* empty */; argc
> optind
; optind
++) {
275 whole_program
->Shaders
= (struct gl_shader
**)
276 talloc_realloc(whole_program
, whole_program
->Shaders
,
277 struct gl_shader
*, whole_program
->NumShaders
+ 1);
278 assert(whole_program
->Shaders
!= NULL
);
280 struct gl_shader
*shader
= talloc_zero(whole_program
, gl_shader
);
282 whole_program
->Shaders
[whole_program
->NumShaders
] = shader
;
283 whole_program
->NumShaders
++;
285 const unsigned len
= strlen(argv
[optind
]);
289 const char *const ext
= & argv
[optind
][len
- 5];
290 if (strncmp(".vert", ext
, 5) == 0)
291 shader
->Type
= GL_VERTEX_SHADER
;
292 else if (strncmp(".geom", ext
, 5) == 0)
293 shader
->Type
= GL_GEOMETRY_SHADER
;
294 else if (strncmp(".frag", ext
, 5) == 0)
295 shader
->Type
= GL_FRAGMENT_SHADER
;
299 shader
->Source
= load_text_file(whole_program
, argv
[optind
]);
300 if (shader
->Source
== NULL
) {
301 printf("File \"%s\" does not exist.\n", argv
[optind
]);
305 compile_shader(ctx
, shader
);
307 if (!shader
->CompileStatus
) {
308 printf("Info log for %s:\n%s\n", argv
[optind
], shader
->InfoLog
);
309 status
= EXIT_FAILURE
;
314 if ((status
== EXIT_SUCCESS
) && do_link
) {
315 link_shaders(ctx
, whole_program
);
316 status
= (whole_program
->LinkStatus
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
318 if (strlen(whole_program
->InfoLog
) > 0)
319 printf("Info log for linking:\n%s\n", whole_program
->InfoLog
);
322 for (unsigned i
= 0; i
< whole_program
->_NumLinkedShaders
; i
++)
323 talloc_free(whole_program
->_LinkedShaders
[i
]);
325 talloc_free(whole_program
);
326 _mesa_glsl_release_types();
327 _mesa_glsl_release_functions();