blob: 24d6076d07bebef9835fa64c8aa4a8c31c320627 [file] [log] [blame]
Ian Romanick61d4aa02010-06-14 14:46:09 -07001/*
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 <cstdlib>
24#include <cstdio>
Ian Romanick7babbdb2010-06-15 12:47:07 -070025#include <getopt.h>
Ian Romanick61d4aa02010-06-14 14:46:09 -070026
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <unistd.h>
31
32#include "ast.h"
33#include "glsl_parser_extras.h"
34#include "glsl_parser.h"
35#include "ir_optimization.h"
36#include "ir_print_visitor.h"
Ian Romanick8ce55db2010-06-17 12:01:18 -070037#include "program.h"
Ian Romanick61d4aa02010-06-14 14:46:09 -070038
Ian Romanick3fb87872010-07-09 14:09:34 -070039extern "C" struct gl_shader *
40_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
41
42/* Copied from shader_api.c for the stand-alone compiler.
43 */
44struct gl_shader *
45_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
46{
47 struct gl_shader *shader;
Ian Romanick7ffe4052010-08-02 11:46:22 -070048
49 (void) ctx;
50
Ian Romanick3fb87872010-07-09 14:09:34 -070051 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
52 shader = talloc_zero(NULL, struct gl_shader);
53 if (shader) {
54 shader->Type = type;
55 shader->Name = name;
56 shader->RefCount = 1;
57 }
58 return shader;
59}
60
Carl Wortha9696e72010-06-18 17:37:02 -070061/* Returned string will have 'ctx' as its talloc owner. */
Ian Romanick61d4aa02010-06-14 14:46:09 -070062static char *
Eric Anholt16b68b12010-06-30 11:05:43 -070063load_text_file(void *ctx, const char *file_name)
Ian Romanick61d4aa02010-06-14 14:46:09 -070064{
65 char *text = NULL;
66 struct stat st;
67 ssize_t total_read = 0;
68 int fd = open(file_name, O_RDONLY);
69
Ian Romanick61d4aa02010-06-14 14:46:09 -070070 if (fd < 0) {
71 return NULL;
72 }
73
74 if (fstat(fd, & st) == 0) {
Carl Wortha9696e72010-06-18 17:37:02 -070075 text = (char *) talloc_size(ctx, st.st_size + 1);
Ian Romanick61d4aa02010-06-14 14:46:09 -070076 if (text != NULL) {
77 do {
78 ssize_t bytes = read(fd, text + total_read,
79 st.st_size - total_read);
80 if (bytes < 0) {
81 free(text);
82 text = NULL;
83 break;
84 }
85
86 if (bytes == 0) {
87 break;
88 }
89
90 total_read += bytes;
91 } while (total_read < st.st_size);
92
93 text[total_read] = '\0';
Ian Romanick61d4aa02010-06-14 14:46:09 -070094 }
95 }
96
97 close(fd);
98
99 return text;
100}
101
102
Ian Romanick2b368952010-06-15 12:00:37 -0700103void
104usage_fail(const char *name)
105{
106 printf("%s <filename.frag|filename.vert>\n", name);
107 exit(EXIT_FAILURE);
108}
109
110
Ian Romanick7babbdb2010-06-15 12:47:07 -0700111int dump_ast = 0;
Ian Romanick22c23de2010-06-25 15:27:47 -0700112int dump_hir = 0;
Ian Romanick81e17472010-06-15 12:51:38 -0700113int dump_lir = 0;
Ian Romanickc648a122010-06-17 19:51:48 -0700114int do_link = 0;
Ian Romanick7babbdb2010-06-15 12:47:07 -0700115
116const struct option compiler_opts[] = {
117 { "dump-ast", 0, &dump_ast, 1 },
Ian Romanick22c23de2010-06-25 15:27:47 -0700118 { "dump-hir", 0, &dump_hir, 1 },
Ian Romanick81e17472010-06-15 12:51:38 -0700119 { "dump-lir", 0, &dump_lir, 1 },
Ian Romanickc648a122010-06-17 19:51:48 -0700120 { "link", 0, &do_link, 1 },
Ian Romanick7babbdb2010-06-15 12:47:07 -0700121 { NULL, 0, NULL, 0 }
122};
123
Ian Romanick8ce55db2010-06-17 12:01:18 -0700124void
Eric Anholt16b68b12010-06-30 11:05:43 -0700125compile_shader(struct gl_shader *shader)
Ian Romanick61d4aa02010-06-14 14:46:09 -0700126{
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -0700127 struct _mesa_glsl_parse_state *state =
128 new(shader) _mesa_glsl_parse_state(NULL, shader->Type, shader);
Eric Anholtf8946692010-07-20 14:03:35 -0700129
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700130 const char *source = shader->Source;
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -0700131 state->error = preprocess(state, &source, &state->info_log,
132 state->extensions);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700133
Carl Worth2d2561e2010-06-23 15:43:38 -0700134 if (!state->error) {
135 _mesa_glsl_lexer_ctor(state, source);
136 _mesa_glsl_parse(state);
137 _mesa_glsl_lexer_dtor(state);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700138 }
Ian Romanick61d4aa02010-06-14 14:46:09 -0700139
Ian Romanick7babbdb2010-06-15 12:47:07 -0700140 if (dump_ast) {
Carl Worth2d2561e2010-06-23 15:43:38 -0700141 foreach_list_const(n, &state->translation_unit) {
Ian Romanick7babbdb2010-06-15 12:47:07 -0700142 ast_node *ast = exec_node_data(ast_node, n, link);
143 ast->print();
144 }
145 printf("\n\n");
Ian Romanick61d4aa02010-06-14 14:46:09 -0700146 }
147
Eric Anholt16b68b12010-06-30 11:05:43 -0700148 shader->ir = new(shader) exec_list;
Carl Worth2d2561e2010-06-23 15:43:38 -0700149 if (!state->error && !state->translation_unit.is_empty())
Eric Anholt16b68b12010-06-30 11:05:43 -0700150 _mesa_ast_to_hir(shader->ir, state);
Ian Romanick54992c32010-06-14 14:47:26 -0700151
Ian Romanick22c23de2010-06-25 15:27:47 -0700152 /* Print out the unoptimized IR. */
153 if (!state->error && dump_hir) {
Ian Romanick5a2e0b82010-07-20 11:37:45 -0700154 validate_ir_tree(shader->ir);
Eric Anholt16b68b12010-06-30 11:05:43 -0700155 _mesa_print_ir(shader->ir, state);
Ian Romanick22c23de2010-06-25 15:27:47 -0700156 }
157
Ian Romanick61d4aa02010-06-14 14:46:09 -0700158 /* Optimization passes */
Eric Anholt16b68b12010-06-30 11:05:43 -0700159 if (!state->error && !shader->ir->is_empty()) {
Ian Romanick61d4aa02010-06-14 14:46:09 -0700160 bool progress;
161 do {
162 progress = false;
163
Eric Anholt16b68b12010-06-30 11:05:43 -0700164 progress = do_function_inlining(shader->ir) || progress;
165 progress = do_if_simplification(shader->ir) || progress;
166 progress = do_copy_propagation(shader->ir) || progress;
167 progress = do_dead_code_local(shader->ir) || progress;
Eric Anholt66d4c652010-07-27 11:28:26 -0700168 progress = do_dead_code_unlinked(shader->ir) || progress;
Eric Anholt78469542010-07-30 17:04:49 -0700169 progress = do_tree_grafting(shader->ir) || progress;
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700170 progress = do_constant_propagation(shader->ir) || progress;
Eric Anholt16b68b12010-06-30 11:05:43 -0700171 progress = do_constant_variable_unlinked(shader->ir) || progress;
172 progress = do_constant_folding(shader->ir) || progress;
Eric Anholt832aad92010-07-26 22:50:29 -0700173 progress = do_algebraic(shader->ir) || progress;
Eric Anholt16b68b12010-06-30 11:05:43 -0700174 progress = do_vec_index_to_swizzle(shader->ir) || progress;
Eric Anholta36334b2010-07-06 17:53:32 -0700175 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
Eric Anholt16b68b12010-06-30 11:05:43 -0700176 progress = do_swizzle_swizzle(shader->ir) || progress;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700177 } while (progress);
Ian Romanick5a2e0b82010-07-20 11:37:45 -0700178
179 validate_ir_tree(shader->ir);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700180 }
181
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700182
Ian Romanick61d4aa02010-06-14 14:46:09 -0700183 /* Print out the resulting IR */
Carl Worth2d2561e2010-06-23 15:43:38 -0700184 if (!state->error && dump_lir) {
Eric Anholt16b68b12010-06-30 11:05:43 -0700185 _mesa_print_ir(shader->ir, state);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700186 }
187
Carl Worth2d2561e2010-06-23 15:43:38 -0700188 shader->symbols = state->symbols;
189 shader->CompileStatus = !state->error;
Ian Romanick25f51d32010-07-16 15:51:50 -0700190 shader->Version = state->language_version;
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700191 memcpy(shader->builtins_to_link, state->builtins_to_link,
192 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
193 shader->num_builtins_to_link = state->num_builtins_to_link;
Ian Romanickca97bd32010-06-19 01:39:14 -0700194
195 if (shader->InfoLog)
196 talloc_free(shader->InfoLog);
197
Carl Worth2d2561e2010-06-23 15:43:38 -0700198 shader->InfoLog = state->info_log;
199
Kenneth Graunke116f1d42010-06-25 14:10:01 -0700200 /* Retain any live IR, but trash the rest. */
Ian Romanick60e2d062010-07-20 11:27:38 -0700201 reparent_ir(shader->ir, shader);
Kenneth Graunke116f1d42010-06-25 14:10:01 -0700202
Carl Worth2d2561e2010-06-23 15:43:38 -0700203 talloc_free(state);
Ian Romanickca97bd32010-06-19 01:39:14 -0700204
Ian Romanick8ce55db2010-06-17 12:01:18 -0700205 return;
206}
Ian Romanick61d4aa02010-06-14 14:46:09 -0700207
Ian Romanick8ce55db2010-06-17 12:01:18 -0700208int
209main(int argc, char **argv)
210{
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700211 int status = EXIT_SUCCESS;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700212
213 int c;
214 int idx = 0;
215 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
216 /* empty */ ;
217
218
219 if (argc <= optind)
220 usage_fail(argv[0]);
221
Eric Anholt849e1812010-06-30 11:49:17 -0700222 struct gl_shader_program *whole_program;
Carl Worthbe83eb82010-06-23 13:34:05 -0700223
Eric Anholt849e1812010-06-30 11:49:17 -0700224 whole_program = talloc_zero (NULL, struct gl_shader_program);
Carl Worthbe83eb82010-06-23 13:34:05 -0700225 assert(whole_program != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700226
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700227 for (/* empty */; argc > optind; optind++) {
Eric Anholt16b68b12010-06-30 11:05:43 -0700228 whole_program->Shaders = (struct gl_shader **)
Carl Worth26bbfb72010-06-23 19:09:56 -0700229 talloc_realloc(whole_program, whole_program->Shaders,
Eric Anholt16b68b12010-06-30 11:05:43 -0700230 struct gl_shader *, whole_program->NumShaders + 1);
Carl Worthbe83eb82010-06-23 13:34:05 -0700231 assert(whole_program->Shaders != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700232
Eric Anholt16b68b12010-06-30 11:05:43 -0700233 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700234
Carl Worthbe83eb82010-06-23 13:34:05 -0700235 whole_program->Shaders[whole_program->NumShaders] = shader;
236 whole_program->NumShaders++;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700237
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700238 const unsigned len = strlen(argv[optind]);
239 if (len < 6)
240 usage_fail(argv[0]);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700241
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700242 const char *const ext = & argv[optind][len - 5];
243 if (strncmp(".vert", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700244 shader->Type = GL_VERTEX_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700245 else if (strncmp(".geom", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700246 shader->Type = GL_GEOMETRY_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700247 else if (strncmp(".frag", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700248 shader->Type = GL_FRAGMENT_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700249 else
250 usage_fail(argv[0]);
251
Eric Anholt16b68b12010-06-30 11:05:43 -0700252 shader->Source = load_text_file(whole_program, argv[optind]);
Kenneth Graunke2848c4c2010-06-16 12:10:55 -0700253 if (shader->Source == NULL) {
254 printf("File \"%s\" does not exist.\n", argv[optind]);
255 exit(EXIT_FAILURE);
256 }
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700257
Kenneth Graunke29e60872010-06-17 15:28:34 -0700258 compile_shader(shader);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700259
Kenneth Graunke29e60872010-06-17 15:28:34 -0700260 if (!shader->CompileStatus) {
Kenneth Graunkef3eb42d2010-06-19 11:31:01 -0700261 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700262 status = EXIT_FAILURE;
263 break;
264 }
265 }
266
Ian Romanickc648a122010-06-17 19:51:48 -0700267 if ((status == EXIT_SUCCESS) && do_link) {
Carl Worthbe83eb82010-06-23 13:34:05 -0700268 link_shaders(whole_program);
269 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
Ian Romanick4d962e62010-07-02 14:43:01 -0700270
271 if (strlen(whole_program->InfoLog) > 0)
272 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
Ian Romanick832dfa52010-06-17 15:04:20 -0700273 }
274
Ian Romanick3fb87872010-07-09 14:09:34 -0700275 for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
276 talloc_free(whole_program->_LinkedShaders[i]);
277
Carl Worthbe83eb82010-06-23 13:34:05 -0700278 talloc_free(whole_program);
Ian Romanickefc15f82010-06-28 13:17:18 -0700279 _mesa_glsl_release_types();
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700280 _mesa_glsl_release_functions();
Carl Worthbe83eb82010-06-23 13:34:05 -0700281
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700282 return status;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700283}