st/mesa: implement EXT_transform_feedback and ARB_transform_feedback2
[mesa.git] / src / glsl / main.cpp
1 /*
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 <getopt.h>
24
25 #include "ast.h"
26 #include "glsl_parser_extras.h"
27 #include "ir_optimization.h"
28 #include "ir_print_visitor.h"
29 #include "program.h"
30 #include "loop_analysis.h"
31 #include "standalone_scaffolding.h"
32
33 static void
34 initialize_context(struct gl_context *ctx, gl_api api)
35 {
36 initialize_context_to_defaults(ctx, api);
37
38 /* GLSL 1.30 isn't fully supported, but we need to advertise 1.30 so that
39 * the built-in functions for 1.30 can be built.
40 */
41 ctx->Const.GLSLVersion = 130;
42
43 ctx->Const.MaxClipPlanes = 8;
44 ctx->Const.MaxDrawBuffers = 2;
45
46 /* More than the 1.10 minimum to appease parser tests taken from
47 * apps that (hopefully) already checked the number of coords.
48 */
49 ctx->Const.MaxTextureCoordUnits = 4;
50
51 ctx->Driver.NewShader = _mesa_new_shader;
52 }
53
54 /* Returned string will have 'ctx' as its ralloc owner. */
55 static char *
56 load_text_file(void *ctx, const char *file_name)
57 {
58 char *text = NULL;
59 size_t size;
60 size_t total_read = 0;
61 FILE *fp = fopen(file_name, "rb");
62
63 if (!fp) {
64 return NULL;
65 }
66
67 fseek(fp, 0L, SEEK_END);
68 size = ftell(fp);
69 fseek(fp, 0L, SEEK_SET);
70
71 text = (char *) ralloc_size(ctx, size + 1);
72 if (text != NULL) {
73 do {
74 size_t bytes = fread(text + total_read,
75 1, size - total_read, fp);
76 if (bytes < size - total_read) {
77 free(text);
78 text = NULL;
79 break;
80 }
81
82 if (bytes == 0) {
83 break;
84 }
85
86 total_read += bytes;
87 } while (total_read < size);
88
89 text[total_read] = '\0';
90 }
91
92 fclose(fp);
93
94 return text;
95 }
96
97 int glsl_es = 0;
98 int dump_ast = 0;
99 int dump_hir = 0;
100 int dump_lir = 0;
101 int do_link = 0;
102
103 const struct option compiler_opts[] = {
104 { "glsl-es", 0, &glsl_es, 1 },
105 { "dump-ast", 0, &dump_ast, 1 },
106 { "dump-hir", 0, &dump_hir, 1 },
107 { "dump-lir", 0, &dump_lir, 1 },
108 { "link", 0, &do_link, 1 },
109 { NULL, 0, NULL, 0 }
110 };
111
112 /**
113 * \brief Print proper usage and exit with failure.
114 */
115 void
116 usage_fail(const char *name)
117 {
118
119 const char *header =
120 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
121 "\n"
122 "Possible options are:\n";
123 printf(header, name, name);
124 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
125 printf(" --%s\n", o->name);
126 }
127 exit(EXIT_FAILURE);
128 }
129
130
131 void
132 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
133 {
134 struct _mesa_glsl_parse_state *state =
135 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
136
137 const char *source = shader->Source;
138 state->error = preprocess(state, &source, &state->info_log,
139 state->extensions, ctx->API) != 0;
140
141 if (!state->error) {
142 _mesa_glsl_lexer_ctor(state, source);
143 _mesa_glsl_parse(state);
144 _mesa_glsl_lexer_dtor(state);
145 }
146
147 if (dump_ast) {
148 foreach_list_const(n, &state->translation_unit) {
149 ast_node *ast = exec_node_data(ast_node, n, link);
150 ast->print();
151 }
152 printf("\n\n");
153 }
154
155 shader->ir = new(shader) exec_list;
156 if (!state->error && !state->translation_unit.is_empty())
157 _mesa_ast_to_hir(shader->ir, state);
158
159 /* Print out the unoptimized IR. */
160 if (!state->error && dump_hir) {
161 validate_ir_tree(shader->ir);
162 _mesa_print_ir(shader->ir, state);
163 }
164
165 /* Optimization passes */
166 if (!state->error && !shader->ir->is_empty()) {
167 bool progress;
168 do {
169 progress = do_common_optimization(shader->ir, false, false, 32);
170 } while (progress);
171
172 validate_ir_tree(shader->ir);
173 }
174
175
176 /* Print out the resulting IR */
177 if (!state->error && dump_lir) {
178 _mesa_print_ir(shader->ir, state);
179 }
180
181 shader->symbols = state->symbols;
182 shader->CompileStatus = !state->error;
183 shader->Version = state->language_version;
184 memcpy(shader->builtins_to_link, state->builtins_to_link,
185 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
186 shader->num_builtins_to_link = state->num_builtins_to_link;
187
188 if (shader->InfoLog)
189 ralloc_free(shader->InfoLog);
190
191 shader->InfoLog = state->info_log;
192
193 /* Retain any live IR, but trash the rest. */
194 reparent_ir(shader->ir, shader);
195
196 ralloc_free(state);
197
198 return;
199 }
200
201 int
202 main(int argc, char **argv)
203 {
204 int status = EXIT_SUCCESS;
205 struct gl_context local_ctx;
206 struct gl_context *ctx = &local_ctx;
207
208 int c;
209 int idx = 0;
210 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
211 /* empty */ ;
212
213
214 if (argc <= optind)
215 usage_fail(argv[0]);
216
217 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL);
218
219 struct gl_shader_program *whole_program;
220
221 whole_program = rzalloc (NULL, struct gl_shader_program);
222 assert(whole_program != NULL);
223 whole_program->InfoLog = ralloc_strdup(whole_program, "");
224
225 for (/* empty */; argc > optind; optind++) {
226 whole_program->Shaders =
227 reralloc(whole_program, whole_program->Shaders,
228 struct gl_shader *, whole_program->NumShaders + 1);
229 assert(whole_program->Shaders != NULL);
230
231 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
232
233 whole_program->Shaders[whole_program->NumShaders] = shader;
234 whole_program->NumShaders++;
235
236 const unsigned len = strlen(argv[optind]);
237 if (len < 6)
238 usage_fail(argv[0]);
239
240 const char *const ext = & argv[optind][len - 5];
241 if (strncmp(".vert", ext, 5) == 0)
242 shader->Type = GL_VERTEX_SHADER;
243 else if (strncmp(".geom", ext, 5) == 0)
244 shader->Type = GL_GEOMETRY_SHADER;
245 else if (strncmp(".frag", ext, 5) == 0)
246 shader->Type = GL_FRAGMENT_SHADER;
247 else
248 usage_fail(argv[0]);
249
250 shader->Source = load_text_file(whole_program, argv[optind]);
251 if (shader->Source == NULL) {
252 printf("File \"%s\" does not exist.\n", argv[optind]);
253 exit(EXIT_FAILURE);
254 }
255
256 compile_shader(ctx, shader);
257
258 if (!shader->CompileStatus) {
259 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
260 status = EXIT_FAILURE;
261 break;
262 }
263 }
264
265 if ((status == EXIT_SUCCESS) && do_link) {
266 link_shaders(ctx, whole_program);
267 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
268
269 if (strlen(whole_program->InfoLog) > 0)
270 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
271 }
272
273 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
274 ralloc_free(whole_program->_LinkedShaders[i]);
275
276 ralloc_free(whole_program);
277 _mesa_glsl_release_types();
278 _mesa_glsl_release_functions();
279
280 return status;
281 }