ir_to_mesa: Support texture rectangle targets
[mesa/nouveau-pmpeg.git] / src / glsl / main.cpp
blob24d6076d07bebef9835fa64c8aa4a8c31c320627
1 /*
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
13 * Software.
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.
23 #include <cstdlib>
24 #include <cstdio>
25 #include <getopt.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
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"
37 #include "program.h"
39 extern "C" struct gl_shader *
40 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
42 /* Copied from shader_api.c for the stand-alone compiler.
44 struct gl_shader *
45 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
47 struct gl_shader *shader;
49 (void) ctx;
51 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;
58 return shader;
61 /* Returned string will have 'ctx' as its talloc owner. */
62 static char *
63 load_text_file(void *ctx, const char *file_name)
65 char *text = NULL;
66 struct stat st;
67 ssize_t total_read = 0;
68 int fd = open(file_name, O_RDONLY);
70 if (fd < 0) {
71 return NULL;
74 if (fstat(fd, & st) == 0) {
75 text = (char *) talloc_size(ctx, st.st_size + 1);
76 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;
86 if (bytes == 0) {
87 break;
90 total_read += bytes;
91 } while (total_read < st.st_size);
93 text[total_read] = '\0';
97 close(fd);
99 return text;
103 void
104 usage_fail(const char *name)
106 printf("%s <filename.frag|filename.vert>\n", name);
107 exit(EXIT_FAILURE);
111 int dump_ast = 0;
112 int dump_hir = 0;
113 int dump_lir = 0;
114 int do_link = 0;
116 const struct option compiler_opts[] = {
117 { "dump-ast", 0, &dump_ast, 1 },
118 { "dump-hir", 0, &dump_hir, 1 },
119 { "dump-lir", 0, &dump_lir, 1 },
120 { "link", 0, &do_link, 1 },
121 { NULL, 0, NULL, 0 }
124 void
125 compile_shader(struct gl_shader *shader)
127 struct _mesa_glsl_parse_state *state =
128 new(shader) _mesa_glsl_parse_state(NULL, shader->Type, shader);
130 const char *source = shader->Source;
131 state->error = preprocess(state, &source, &state->info_log,
132 state->extensions);
134 if (!state->error) {
135 _mesa_glsl_lexer_ctor(state, source);
136 _mesa_glsl_parse(state);
137 _mesa_glsl_lexer_dtor(state);
140 if (dump_ast) {
141 foreach_list_const(n, &state->translation_unit) {
142 ast_node *ast = exec_node_data(ast_node, n, link);
143 ast->print();
145 printf("\n\n");
148 shader->ir = new(shader) exec_list;
149 if (!state->error && !state->translation_unit.is_empty())
150 _mesa_ast_to_hir(shader->ir, state);
152 /* Print out the unoptimized IR. */
153 if (!state->error && dump_hir) {
154 validate_ir_tree(shader->ir);
155 _mesa_print_ir(shader->ir, state);
158 /* Optimization passes */
159 if (!state->error && !shader->ir->is_empty()) {
160 bool progress;
161 do {
162 progress = false;
164 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;
168 progress = do_dead_code_unlinked(shader->ir) || progress;
169 progress = do_tree_grafting(shader->ir) || progress;
170 progress = do_constant_propagation(shader->ir) || progress;
171 progress = do_constant_variable_unlinked(shader->ir) || progress;
172 progress = do_constant_folding(shader->ir) || progress;
173 progress = do_algebraic(shader->ir) || progress;
174 progress = do_vec_index_to_swizzle(shader->ir) || progress;
175 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
176 progress = do_swizzle_swizzle(shader->ir) || progress;
177 } while (progress);
179 validate_ir_tree(shader->ir);
183 /* Print out the resulting IR */
184 if (!state->error && dump_lir) {
185 _mesa_print_ir(shader->ir, state);
188 shader->symbols = state->symbols;
189 shader->CompileStatus = !state->error;
190 shader->Version = state->language_version;
191 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;
195 if (shader->InfoLog)
196 talloc_free(shader->InfoLog);
198 shader->InfoLog = state->info_log;
200 /* Retain any live IR, but trash the rest. */
201 reparent_ir(shader->ir, shader);
203 talloc_free(state);
205 return;
209 main(int argc, char **argv)
211 int status = EXIT_SUCCESS;
213 int c;
214 int idx = 0;
215 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
216 /* empty */ ;
219 if (argc <= optind)
220 usage_fail(argv[0]);
222 struct gl_shader_program *whole_program;
224 whole_program = talloc_zero (NULL, struct gl_shader_program);
225 assert(whole_program != NULL);
227 for (/* empty */; argc > optind; optind++) {
228 whole_program->Shaders = (struct gl_shader **)
229 talloc_realloc(whole_program, whole_program->Shaders,
230 struct gl_shader *, whole_program->NumShaders + 1);
231 assert(whole_program->Shaders != NULL);
233 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
235 whole_program->Shaders[whole_program->NumShaders] = shader;
236 whole_program->NumShaders++;
238 const unsigned len = strlen(argv[optind]);
239 if (len < 6)
240 usage_fail(argv[0]);
242 const char *const ext = & argv[optind][len - 5];
243 if (strncmp(".vert", ext, 5) == 0)
244 shader->Type = GL_VERTEX_SHADER;
245 else if (strncmp(".geom", ext, 5) == 0)
246 shader->Type = GL_GEOMETRY_SHADER;
247 else if (strncmp(".frag", ext, 5) == 0)
248 shader->Type = GL_FRAGMENT_SHADER;
249 else
250 usage_fail(argv[0]);
252 shader->Source = load_text_file(whole_program, argv[optind]);
253 if (shader->Source == NULL) {
254 printf("File \"%s\" does not exist.\n", argv[optind]);
255 exit(EXIT_FAILURE);
258 compile_shader(shader);
260 if (!shader->CompileStatus) {
261 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
262 status = EXIT_FAILURE;
263 break;
267 if ((status == EXIT_SUCCESS) && do_link) {
268 link_shaders(whole_program);
269 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
271 if (strlen(whole_program->InfoLog) > 0)
272 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
275 for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
276 talloc_free(whole_program->_LinkedShaders[i]);
278 talloc_free(whole_program);
279 _mesa_glsl_release_types();
280 _mesa_glsl_release_functions();
282 return status;