blob: 982562c4bbda42531dfb20cb39f936e07a84e9b0 [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 Romanick8df2dbf2010-08-26 16:45:22 -070038#include "loop_analysis.h"
Ian Romanick61d4aa02010-06-14 14:46:09 -070039
Ian Romanick3fb87872010-07-09 14:09:34 -070040extern "C" struct gl_shader *
41_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
42
43/* Copied from shader_api.c for the stand-alone compiler.
44 */
45struct gl_shader *
46_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
47{
48 struct gl_shader *shader;
Ian Romanick7ffe4052010-08-02 11:46:22 -070049
50 (void) ctx;
51
Ian Romanick3fb87872010-07-09 14:09:34 -070052 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
53 shader = talloc_zero(NULL, struct gl_shader);
54 if (shader) {
55 shader->Type = type;
56 shader->Name = name;
57 shader->RefCount = 1;
58 }
59 return shader;
60}
61
Chia-I Wudc754582010-09-08 18:48:12 +080062static void
63initialize_context(GLcontext *ctx, gl_api api)
64{
65 memset(ctx, 0, sizeof(*ctx));
66
67 ctx->API = api;
68
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;
73
74 /* 1.10 minimums. */
75 ctx->Const.MaxLights = 8;
76 ctx->Const.MaxClipPlanes = 8;
77 ctx->Const.MaxTextureUnits = 2;
78
79 /* More than the 1.10 minimum to appease parser tests taken from
80 * apps that (hopefully) already checked the number of coords.
81 */
82 ctx->Const.MaxTextureCoordUnits = 4;
83
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;
91
92 ctx->Const.MaxDrawBuffers = 2;
93
94 ctx->Driver.NewShader = _mesa_new_shader;
95}
96
Carl Wortha9696e72010-06-18 17:37:02 -070097/* Returned string will have 'ctx' as its talloc owner. */
Ian Romanick61d4aa02010-06-14 14:46:09 -070098static char *
Eric Anholt16b68b12010-06-30 11:05:43 -070099load_text_file(void *ctx, const char *file_name)
Ian Romanick61d4aa02010-06-14 14:46:09 -0700100{
101 char *text = NULL;
102 struct stat st;
103 ssize_t total_read = 0;
104 int fd = open(file_name, O_RDONLY);
105
Ian Romanick61d4aa02010-06-14 14:46:09 -0700106 if (fd < 0) {
107 return NULL;
108 }
109
110 if (fstat(fd, & st) == 0) {
Carl Wortha9696e72010-06-18 17:37:02 -0700111 text = (char *) talloc_size(ctx, st.st_size + 1);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700112 if (text != NULL) {
113 do {
114 ssize_t bytes = read(fd, text + total_read,
115 st.st_size - total_read);
116 if (bytes < 0) {
117 free(text);
118 text = NULL;
119 break;
120 }
121
122 if (bytes == 0) {
123 break;
124 }
125
126 total_read += bytes;
127 } while (total_read < st.st_size);
128
129 text[total_read] = '\0';
Ian Romanick61d4aa02010-06-14 14:46:09 -0700130 }
131 }
132
133 close(fd);
134
135 return text;
136}
137
138
Ian Romanick2b368952010-06-15 12:00:37 -0700139void
140usage_fail(const char *name)
141{
142 printf("%s <filename.frag|filename.vert>\n", name);
143 exit(EXIT_FAILURE);
144}
145
146
Ian Romanick7babbdb2010-06-15 12:47:07 -0700147int dump_ast = 0;
Ian Romanick22c23de2010-06-25 15:27:47 -0700148int dump_hir = 0;
Ian Romanick81e17472010-06-15 12:51:38 -0700149int dump_lir = 0;
Ian Romanickc648a122010-06-17 19:51:48 -0700150int do_link = 0;
Ian Romanick7babbdb2010-06-15 12:47:07 -0700151
152const struct option compiler_opts[] = {
153 { "dump-ast", 0, &dump_ast, 1 },
Ian Romanick22c23de2010-06-25 15:27:47 -0700154 { "dump-hir", 0, &dump_hir, 1 },
Ian Romanick81e17472010-06-15 12:51:38 -0700155 { "dump-lir", 0, &dump_lir, 1 },
Ian Romanickc648a122010-06-17 19:51:48 -0700156 { "link", 0, &do_link, 1 },
Ian Romanick7babbdb2010-06-15 12:47:07 -0700157 { NULL, 0, NULL, 0 }
158};
159
Ian Romanick8ce55db2010-06-17 12:01:18 -0700160void
Chia-I Wudc754582010-09-08 18:48:12 +0800161compile_shader(GLcontext *ctx, struct gl_shader *shader)
Ian Romanick61d4aa02010-06-14 14:46:09 -0700162{
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -0700163 struct _mesa_glsl_parse_state *state =
Chia-I Wudc754582010-09-08 18:48:12 +0800164 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
Eric Anholtf8946692010-07-20 14:03:35 -0700165
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700166 const char *source = shader->Source;
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -0700167 state->error = preprocess(state, &source, &state->info_log,
Chia-I Wudc754582010-09-08 18:48:12 +0800168 state->extensions, ctx->API);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700169
Carl Worth2d2561e2010-06-23 15:43:38 -0700170 if (!state->error) {
171 _mesa_glsl_lexer_ctor(state, source);
172 _mesa_glsl_parse(state);
173 _mesa_glsl_lexer_dtor(state);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700174 }
Ian Romanick61d4aa02010-06-14 14:46:09 -0700175
Ian Romanick7babbdb2010-06-15 12:47:07 -0700176 if (dump_ast) {
Carl Worth2d2561e2010-06-23 15:43:38 -0700177 foreach_list_const(n, &state->translation_unit) {
Ian Romanick7babbdb2010-06-15 12:47:07 -0700178 ast_node *ast = exec_node_data(ast_node, n, link);
179 ast->print();
180 }
181 printf("\n\n");
Ian Romanick61d4aa02010-06-14 14:46:09 -0700182 }
183
Eric Anholt16b68b12010-06-30 11:05:43 -0700184 shader->ir = new(shader) exec_list;
Carl Worth2d2561e2010-06-23 15:43:38 -0700185 if (!state->error && !state->translation_unit.is_empty())
Eric Anholt16b68b12010-06-30 11:05:43 -0700186 _mesa_ast_to_hir(shader->ir, state);
Ian Romanick54992c32010-06-14 14:47:26 -0700187
Ian Romanick22c23de2010-06-25 15:27:47 -0700188 /* Print out the unoptimized IR. */
189 if (!state->error && dump_hir) {
Ian Romanick5a2e0b82010-07-20 11:37:45 -0700190 validate_ir_tree(shader->ir);
Eric Anholt16b68b12010-06-30 11:05:43 -0700191 _mesa_print_ir(shader->ir, state);
Ian Romanick22c23de2010-06-25 15:27:47 -0700192 }
193
Ian Romanick61d4aa02010-06-14 14:46:09 -0700194 /* Optimization passes */
Eric Anholt16b68b12010-06-30 11:05:43 -0700195 if (!state->error && !shader->ir->is_empty()) {
Ian Romanick61d4aa02010-06-14 14:46:09 -0700196 bool progress;
197 do {
198 progress = false;
199
Eric Anholt16b68b12010-06-30 11:05:43 -0700200 progress = do_function_inlining(shader->ir) || progress;
201 progress = do_if_simplification(shader->ir) || progress;
202 progress = do_copy_propagation(shader->ir) || progress;
203 progress = do_dead_code_local(shader->ir) || progress;
Eric Anholt66d4c652010-07-27 11:28:26 -0700204 progress = do_dead_code_unlinked(shader->ir) || progress;
Eric Anholt78469542010-07-30 17:04:49 -0700205 progress = do_tree_grafting(shader->ir) || progress;
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700206 progress = do_constant_propagation(shader->ir) || progress;
Eric Anholt16b68b12010-06-30 11:05:43 -0700207 progress = do_constant_variable_unlinked(shader->ir) || progress;
208 progress = do_constant_folding(shader->ir) || progress;
Eric Anholt832aad92010-07-26 22:50:29 -0700209 progress = do_algebraic(shader->ir) || progress;
Eric Anholt16b68b12010-06-30 11:05:43 -0700210 progress = do_vec_index_to_swizzle(shader->ir) || progress;
Eric Anholta36334b2010-07-06 17:53:32 -0700211 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
Eric Anholt16b68b12010-06-30 11:05:43 -0700212 progress = do_swizzle_swizzle(shader->ir) || progress;
Ian Romanick8df2dbf2010-08-26 16:45:22 -0700213
214 loop_state *ls = analyze_loop_variables(shader->ir);
Ian Romanickde7c3fe2010-08-27 13:59:49 -0700215 progress = set_loop_controls(shader->ir, ls) || progress;
216 progress = unroll_loops(shader->ir, ls) || progress;
Ian Romanick8df2dbf2010-08-26 16:45:22 -0700217 delete ls;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700218 } while (progress);
Ian Romanick5a2e0b82010-07-20 11:37:45 -0700219
220 validate_ir_tree(shader->ir);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700221 }
222
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700223
Ian Romanick61d4aa02010-06-14 14:46:09 -0700224 /* Print out the resulting IR */
Carl Worth2d2561e2010-06-23 15:43:38 -0700225 if (!state->error && dump_lir) {
Eric Anholt16b68b12010-06-30 11:05:43 -0700226 _mesa_print_ir(shader->ir, state);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700227 }
228
Carl Worth2d2561e2010-06-23 15:43:38 -0700229 shader->symbols = state->symbols;
230 shader->CompileStatus = !state->error;
Ian Romanick25f51d32010-07-16 15:51:50 -0700231 shader->Version = state->language_version;
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700232 memcpy(shader->builtins_to_link, state->builtins_to_link,
233 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
234 shader->num_builtins_to_link = state->num_builtins_to_link;
Ian Romanickca97bd32010-06-19 01:39:14 -0700235
236 if (shader->InfoLog)
237 talloc_free(shader->InfoLog);
238
Carl Worth2d2561e2010-06-23 15:43:38 -0700239 shader->InfoLog = state->info_log;
240
Kenneth Graunke116f1d42010-06-25 14:10:01 -0700241 /* Retain any live IR, but trash the rest. */
Ian Romanick60e2d062010-07-20 11:27:38 -0700242 reparent_ir(shader->ir, shader);
Kenneth Graunke116f1d42010-06-25 14:10:01 -0700243
Carl Worth2d2561e2010-06-23 15:43:38 -0700244 talloc_free(state);
Ian Romanickca97bd32010-06-19 01:39:14 -0700245
Ian Romanick8ce55db2010-06-17 12:01:18 -0700246 return;
247}
Ian Romanick61d4aa02010-06-14 14:46:09 -0700248
Ian Romanick8ce55db2010-06-17 12:01:18 -0700249int
250main(int argc, char **argv)
251{
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700252 int status = EXIT_SUCCESS;
Eric Anholt5d0f4302010-08-18 12:02:35 -0700253 GLcontext local_ctx;
254 GLcontext *ctx = &local_ctx;
255
Ian Romanick8ce55db2010-06-17 12:01:18 -0700256 int c;
257 int idx = 0;
258 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
259 /* empty */ ;
260
261
262 if (argc <= optind)
263 usage_fail(argv[0]);
264
Chia-I Wudc754582010-09-08 18:48:12 +0800265 initialize_context(ctx, API_OPENGL);
266
Eric Anholt849e1812010-06-30 11:49:17 -0700267 struct gl_shader_program *whole_program;
Carl Worthbe83eb82010-06-23 13:34:05 -0700268
Eric Anholt849e1812010-06-30 11:49:17 -0700269 whole_program = talloc_zero (NULL, struct gl_shader_program);
Carl Worthbe83eb82010-06-23 13:34:05 -0700270 assert(whole_program != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700271
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700272 for (/* empty */; argc > optind; optind++) {
Eric Anholt16b68b12010-06-30 11:05:43 -0700273 whole_program->Shaders = (struct gl_shader **)
Carl Worth26bbfb72010-06-23 19:09:56 -0700274 talloc_realloc(whole_program, whole_program->Shaders,
Eric Anholt16b68b12010-06-30 11:05:43 -0700275 struct gl_shader *, whole_program->NumShaders + 1);
Carl Worthbe83eb82010-06-23 13:34:05 -0700276 assert(whole_program->Shaders != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700277
Eric Anholt16b68b12010-06-30 11:05:43 -0700278 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700279
Carl Worthbe83eb82010-06-23 13:34:05 -0700280 whole_program->Shaders[whole_program->NumShaders] = shader;
281 whole_program->NumShaders++;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700282
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700283 const unsigned len = strlen(argv[optind]);
284 if (len < 6)
285 usage_fail(argv[0]);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700286
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700287 const char *const ext = & argv[optind][len - 5];
288 if (strncmp(".vert", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700289 shader->Type = GL_VERTEX_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700290 else if (strncmp(".geom", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700291 shader->Type = GL_GEOMETRY_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700292 else if (strncmp(".frag", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700293 shader->Type = GL_FRAGMENT_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700294 else
295 usage_fail(argv[0]);
296
Eric Anholt16b68b12010-06-30 11:05:43 -0700297 shader->Source = load_text_file(whole_program, argv[optind]);
Kenneth Graunke2848c4c2010-06-16 12:10:55 -0700298 if (shader->Source == NULL) {
299 printf("File \"%s\" does not exist.\n", argv[optind]);
300 exit(EXIT_FAILURE);
301 }
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700302
Chia-I Wudc754582010-09-08 18:48:12 +0800303 compile_shader(ctx, shader);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700304
Kenneth Graunke29e60872010-06-17 15:28:34 -0700305 if (!shader->CompileStatus) {
Kenneth Graunkef3eb42d2010-06-19 11:31:01 -0700306 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700307 status = EXIT_FAILURE;
308 break;
309 }
310 }
311
Ian Romanickc648a122010-06-17 19:51:48 -0700312 if ((status == EXIT_SUCCESS) && do_link) {
Eric Anholt5d0f4302010-08-18 12:02:35 -0700313 link_shaders(ctx, whole_program);
Carl Worthbe83eb82010-06-23 13:34:05 -0700314 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
Ian Romanick4d962e62010-07-02 14:43:01 -0700315
316 if (strlen(whole_program->InfoLog) > 0)
317 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
Ian Romanick832dfa52010-06-17 15:04:20 -0700318 }
319
Ian Romanick3fb87872010-07-09 14:09:34 -0700320 for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
321 talloc_free(whole_program->_LinkedShaders[i]);
322
Carl Worthbe83eb82010-06-23 13:34:05 -0700323 talloc_free(whole_program);
Ian Romanickefc15f82010-06-28 13:17:18 -0700324 _mesa_glsl_release_types();
Ian Romanickd5be2ac2010-07-20 11:29:46 -0700325 _mesa_glsl_release_functions();
Carl Worthbe83eb82010-06-23 13:34:05 -0700326
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700327 return status;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700328}