glsl: add support for system values and GL_ARB_draw_instanced
[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 <cstdlib>
24 #include <cstdio>
25 #include <getopt.h>
26
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"
37 #include "program.h"
38 #include "loop_analysis.h"
39
40 extern "C" struct gl_shader *
41 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
42
43 extern "C" void
44 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
45 struct gl_shader *sh);
46
47 /* Copied from shader_api.c for the stand-alone compiler.
48 */
49 void
50 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
51 struct gl_shader *sh)
52 {
53 *ptr = sh;
54 }
55
56 struct gl_shader *
57 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
58 {
59 struct gl_shader *shader;
60
61 (void) ctx;
62
63 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
64 shader = talloc_zero(NULL, struct gl_shader);
65 if (shader) {
66 shader->Type = type;
67 shader->Name = name;
68 shader->RefCount = 1;
69 }
70 return shader;
71 }
72
73 static void
74 initialize_context(struct gl_context *ctx, gl_api api)
75 {
76 memset(ctx, 0, sizeof(*ctx));
77
78 ctx->API = api;
79
80 ctx->Extensions.ARB_draw_buffers = GL_TRUE;
81 ctx->Extensions.ARB_draw_instanced = GL_TRUE;
82 ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
83 ctx->Extensions.EXT_texture_array = GL_TRUE;
84 ctx->Extensions.NV_texture_rectangle = GL_TRUE;
85
86 /* 1.10 minimums. */
87 ctx->Const.MaxLights = 8;
88 ctx->Const.MaxClipPlanes = 8;
89 ctx->Const.MaxTextureUnits = 2;
90
91 /* More than the 1.10 minimum to appease parser tests taken from
92 * apps that (hopefully) already checked the number of coords.
93 */
94 ctx->Const.MaxTextureCoordUnits = 4;
95
96 ctx->Const.VertexProgram.MaxAttribs = 16;
97 ctx->Const.VertexProgram.MaxUniformComponents = 512;
98 ctx->Const.MaxVarying = 8;
99 ctx->Const.MaxVertexTextureImageUnits = 0;
100 ctx->Const.MaxCombinedTextureImageUnits = 2;
101 ctx->Const.MaxTextureImageUnits = 2;
102 ctx->Const.FragmentProgram.MaxUniformComponents = 64;
103
104 ctx->Const.MaxDrawBuffers = 2;
105
106 ctx->Driver.NewShader = _mesa_new_shader;
107 }
108
109 /* Returned string will have 'ctx' as its talloc owner. */
110 static char *
111 load_text_file(void *ctx, const char *file_name)
112 {
113 char *text = NULL;
114 struct stat st;
115 ssize_t total_read = 0;
116 int fd = open(file_name, O_RDONLY);
117
118 if (fd < 0) {
119 return NULL;
120 }
121
122 if (fstat(fd, & st) == 0) {
123 text = (char *) talloc_size(ctx, st.st_size + 1);
124 if (text != NULL) {
125 do {
126 ssize_t bytes = read(fd, text + total_read,
127 st.st_size - total_read);
128 if (bytes < 0) {
129 free(text);
130 text = NULL;
131 break;
132 }
133
134 if (bytes == 0) {
135 break;
136 }
137
138 total_read += bytes;
139 } while (total_read < st.st_size);
140
141 text[total_read] = '\0';
142 }
143 }
144
145 close(fd);
146
147 return text;
148 }
149
150 int glsl_es = 0;
151 int dump_ast = 0;
152 int dump_hir = 0;
153 int dump_lir = 0;
154 int do_link = 0;
155
156 const struct option compiler_opts[] = {
157 { "glsl-es", 0, &glsl_es, 1 },
158 { "dump-ast", 0, &dump_ast, 1 },
159 { "dump-hir", 0, &dump_hir, 1 },
160 { "dump-lir", 0, &dump_lir, 1 },
161 { "link", 0, &do_link, 1 },
162 { NULL, 0, NULL, 0 }
163 };
164
165 /**
166 * \brief Print proper usage and exit with failure.
167 */
168 void
169 usage_fail(const char *name)
170 {
171
172 const char *header =
173 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
174 "\n"
175 "Possible options are:\n";
176 printf(header, name, name);
177 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
178 printf(" --%s\n", o->name);
179 }
180 exit(EXIT_FAILURE);
181 }
182
183
184 void
185 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
186 {
187 struct _mesa_glsl_parse_state *state =
188 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
189
190 const char *source = shader->Source;
191 state->error = preprocess(state, &source, &state->info_log,
192 state->extensions, ctx->API);
193
194 if (!state->error) {
195 _mesa_glsl_lexer_ctor(state, source);
196 _mesa_glsl_parse(state);
197 _mesa_glsl_lexer_dtor(state);
198 }
199
200 if (dump_ast) {
201 foreach_list_const(n, &state->translation_unit) {
202 ast_node *ast = exec_node_data(ast_node, n, link);
203 ast->print();
204 }
205 printf("\n\n");
206 }
207
208 shader->ir = new(shader) exec_list;
209 if (!state->error && !state->translation_unit.is_empty())
210 _mesa_ast_to_hir(shader->ir, state);
211
212 /* Print out the unoptimized IR. */
213 if (!state->error && dump_hir) {
214 validate_ir_tree(shader->ir);
215 _mesa_print_ir(shader->ir, state);
216 }
217
218 /* Optimization passes */
219 if (!state->error && !shader->ir->is_empty()) {
220 bool progress;
221 do {
222 progress = false;
223
224 progress = do_function_inlining(shader->ir) || progress;
225 progress = do_if_simplification(shader->ir) || progress;
226 progress = do_copy_propagation(shader->ir) || progress;
227 progress = do_dead_code_local(shader->ir) || progress;
228 progress = do_dead_code_unlinked(shader->ir) || progress;
229 progress = do_tree_grafting(shader->ir) || progress;
230 progress = do_constant_propagation(shader->ir) || progress;
231 progress = do_constant_variable_unlinked(shader->ir) || progress;
232 progress = do_constant_folding(shader->ir) || progress;
233 progress = do_algebraic(shader->ir) || progress;
234 progress = do_vec_index_to_swizzle(shader->ir) || progress;
235 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
236 progress = do_swizzle_swizzle(shader->ir) || progress;
237
238 loop_state *ls = analyze_loop_variables(shader->ir);
239 progress = set_loop_controls(shader->ir, ls) || progress;
240 progress = unroll_loops(shader->ir, ls, 32) || progress;
241 delete ls;
242 } while (progress);
243
244 validate_ir_tree(shader->ir);
245 }
246
247
248 /* Print out the resulting IR */
249 if (!state->error && dump_lir) {
250 _mesa_print_ir(shader->ir, state);
251 }
252
253 shader->symbols = state->symbols;
254 shader->CompileStatus = !state->error;
255 shader->Version = state->language_version;
256 memcpy(shader->builtins_to_link, state->builtins_to_link,
257 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
258 shader->num_builtins_to_link = state->num_builtins_to_link;
259
260 if (shader->InfoLog)
261 talloc_free(shader->InfoLog);
262
263 shader->InfoLog = state->info_log;
264
265 /* Retain any live IR, but trash the rest. */
266 reparent_ir(shader->ir, shader);
267
268 talloc_free(state);
269
270 return;
271 }
272
273 int
274 main(int argc, char **argv)
275 {
276 int status = EXIT_SUCCESS;
277 struct gl_context local_ctx;
278 struct gl_context *ctx = &local_ctx;
279
280 int c;
281 int idx = 0;
282 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
283 /* empty */ ;
284
285
286 if (argc <= optind)
287 usage_fail(argv[0]);
288
289 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL);
290
291 struct gl_shader_program *whole_program;
292
293 whole_program = talloc_zero (NULL, struct gl_shader_program);
294 assert(whole_program != NULL);
295
296 for (/* empty */; argc > optind; optind++) {
297 whole_program->Shaders = (struct gl_shader **)
298 talloc_realloc(whole_program, whole_program->Shaders,
299 struct gl_shader *, whole_program->NumShaders + 1);
300 assert(whole_program->Shaders != NULL);
301
302 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
303
304 whole_program->Shaders[whole_program->NumShaders] = shader;
305 whole_program->NumShaders++;
306
307 const unsigned len = strlen(argv[optind]);
308 if (len < 6)
309 usage_fail(argv[0]);
310
311 const char *const ext = & argv[optind][len - 5];
312 if (strncmp(".vert", ext, 5) == 0)
313 shader->Type = GL_VERTEX_SHADER;
314 else if (strncmp(".geom", ext, 5) == 0)
315 shader->Type = GL_GEOMETRY_SHADER;
316 else if (strncmp(".frag", ext, 5) == 0)
317 shader->Type = GL_FRAGMENT_SHADER;
318 else
319 usage_fail(argv[0]);
320
321 shader->Source = load_text_file(whole_program, argv[optind]);
322 if (shader->Source == NULL) {
323 printf("File \"%s\" does not exist.\n", argv[optind]);
324 exit(EXIT_FAILURE);
325 }
326
327 compile_shader(ctx, shader);
328
329 if (!shader->CompileStatus) {
330 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
331 status = EXIT_FAILURE;
332 break;
333 }
334 }
335
336 if ((status == EXIT_SUCCESS) && do_link) {
337 link_shaders(ctx, whole_program);
338 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
339
340 if (strlen(whole_program->InfoLog) > 0)
341 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
342 }
343
344 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
345 talloc_free(whole_program->_LinkedShaders[i]);
346
347 talloc_free(whole_program);
348 _mesa_glsl_release_types();
349 _mesa_glsl_release_functions();
350
351 return status;
352 }